Search
lxdream.org :: lxdream/src/drivers/cd_linux.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cd_linux.c
changeset 788:eb238a77f3c0
prev777:60431d09605c
next806:6ef1ce4a9dbc
author nkeynes
date Mon Aug 04 05:58:53 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Fix typo/array overrun
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Linux cd-rom device driver. 
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    18 #include <unistd.h>
    19 #include <stdlib.h>
    20 #include <stdio.h>
    21 #include <string.h>
    22 #include <errno.h>
    23 #include <linux/cdrom.h>
    24 #include <sys/stat.h>
    25 #include <sys/ioctl.h>
    26 #include <fstab.h>
    27 #include <fcntl.h>
    29 #include "gdrom/gddriver.h"
    30 #include "gdrom/packet.h"
    31 #include "dream.h"
    33 #define MAXTOCENTRIES 600  /* This is a fairly generous overestimate really */
    34 #define MAXTOCSIZE 4 + (MAXTOCENTRIES*11)
    35 #define MAX_SECTORS_PER_CALL 1
    37 static uint32_t inline lbatomsf( uint32_t lba ) {
    38     union cdrom_addr addr;
    39     lba = lba + CD_MSF_OFFSET;
    40     addr.msf.frame = lba % CD_FRAMES;
    41     int seconds = lba / CD_FRAMES;
    42     addr.msf.second = seconds % CD_SECS;
    43     addr.msf.minute = seconds / CD_SECS;
    44     return addr.lba;
    45 }
    47 #define LBATOMSF( lba ) lbatomsf(lba)
    50 static gboolean linux_image_is_valid( FILE *f );
    51 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
    52 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc );
    53 static gdrom_error_t linux_identify_drive( int fd, unsigned char *buf, int buflen );
    54 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
    55                                         int mode, unsigned char *buf, uint32_t *length );
    56 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, uint32_t *buflen,
    57                                          int direction );
    58 static int linux_drive_status( gdrom_disc_t disc );
    60 struct gdrom_image_class cdrom_device_class = { "Linux", NULL,
    61         linux_image_is_valid, linux_open_device };
    62 GList *cdrom_get_native_devices(void)
    63 {
    64     GList *list = NULL;
    65     struct fstab *ent;
    66     struct stat st;
    67     setfsent();
    68     while( (ent = getfsent()) != NULL ) {
    69         if( (stat(ent->fs_spec, &st) != -1) && 
    70                 S_ISBLK(st.st_mode) ) {
    71             /* Got a valid block device - is it a CDROM? */
    72             int fd = open(ent->fs_spec, O_RDONLY|O_NONBLOCK);
    73             if( fd == -1 )
    74                 continue;
    75             int caps = ioctl(fd, CDROM_GET_CAPABILITY);
    76             if( caps != -1 ) {
    77                 /* Appears to support CDROM functions */
    78                 char buf[32];
    79                 linux_identify_drive( fd, buf, sizeof(buf) );
    80                 list = g_list_append( list, gdrom_device_new(ent->fs_spec, buf));
    81             }
    82             close(fd);
    83         }
    84     }
    85     return list;
    86 }
    88 gdrom_disc_t cdrom_open_device( const gchar *method, const gchar *path )
    89 {
    90     return NULL;
    91 }
    93 static gboolean linux_image_is_valid( FILE *f )
    94 {
    95     struct stat st;
    96     struct cdrom_tochdr tochdr;
    98     if( fstat(fileno(f), &st) == -1 ) {
    99         return FALSE; /* can't stat device? */
   100     }
   101     if( !S_ISBLK(st.st_mode) ) {
   102         return FALSE; /* Not a block device */
   103     }
   105     int caps = ioctl(fileno(f), CDROM_GET_CAPABILITY);
   106     if( caps == -1 ) {
   107         /* Quick check that this is really a CD device */
   108         return FALSE;
   109     }
   111     return TRUE;
   112 }
   114 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
   115 {
   116     gdrom_disc_t disc;
   118     disc = gdrom_image_new(filename, f);
   119     if( disc == NULL ) {
   120         ERROR("Unable to allocate memory!");
   121         return NULL;
   122     }
   124     int status = ioctl(fileno(f), CDROM_DRIVE_STATUS, CDSL_CURRENT);
   125     if( status == CDS_DISC_OK ) {
   126         status = linux_read_disc_toc( (gdrom_image_t)disc );
   127         if( status != 0 ) {
   128             gdrom_image_destroy_no_close(disc);
   129             if( status == 0xFFFF ) {
   130                 ERROR("Unable to load disc table of contents (%s)", strerror(errno));
   131             } else {
   132                 ERROR("Unable to load disc table of contents (sense %d,%d)",
   133                     status &0xFF, status >> 8 );
   134             }
   135             return NULL;
   136         }
   137     } else {
   138         ((gdrom_image_t)disc)->disc_type = IDE_DISC_NONE;
   139     }
   140     disc->read_sector = linux_read_sector;
   141     disc->drive_status = linux_drive_status;
   142     return disc;
   143 }
   145 static int linux_drive_status( gdrom_disc_t disc )
   146 {
   147     int fd = fileno(((gdrom_image_t)disc)->file);
   148     int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
   149     if( status == CDS_DISC_OK ) {
   150         status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
   151         if( status != 0 ) {
   152             linux_read_disc_toc( (gdrom_image_t)disc);
   153         }
   154         return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
   155     } else {
   156         return IDE_DISC_NONE;
   157     }
   158 }
   159 /**
   160  * Read the full table of contents into the disc from the device.
   161  */
   162 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
   163 {
   164     int fd = fileno(disc->file);
   165     unsigned char buf[MAXTOCSIZE];
   166     uint32_t buflen = sizeof(buf);
   167     char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   169     cmd[7] = (sizeof(buf))>>8;
   170     cmd[8] = (sizeof(buf))&0xFF;
   171     memset( buf, 0, sizeof(buf) );
   172     gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
   173     if( status != 0 ) {
   174         return status;
   175     }
   176     mmc_parse_toc2( disc, buf );
   177     return 0;
   178 }
   180 gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
   181 {
   182     int fd = fileno( ((gdrom_image_t)disc)->file );
   183     uint32_t real_sector = lba - CD_MSF_OFFSET;
   184     uint32_t length = endlba - lba;
   185     uint32_t buflen = 0;
   186     char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
   187     cmd[2] = (real_sector >> 24) & 0xFF;
   188     cmd[3] = (real_sector >> 16) & 0xFF;
   189     cmd[4] = (real_sector >> 8) & 0xFF;
   190     cmd[5] = real_sector & 0xFF;
   191     cmd[6] = (length >> 24) & 0xFF;
   192     cmd[7] = (length >> 16) & 0xFF;
   193     cmd[8] = (length >> 8) & 0xFF;
   194     cmd[9] = length & 0xFF;
   196     return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
   197 }
   199 gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
   200 {
   201     int fd = fileno( ((gdrom_image_t)disc)->file );
   202     uint32_t buflen = 0;
   203     char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
   204     return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
   205 }
   207 static unsigned char *trim( unsigned char *src )
   208 {
   209     unsigned char *p = src + strlen(src)-1;
   210     while( isspace(*src) ) 
   211         src++;
   212     while( p >= src && isspace(*p) )
   213         *p-- = '\0';
   214     return src;
   215 }
   216 static gdrom_error_t linux_identify_drive( int fd, unsigned char *buf, int buflen )
   217 {
   218     unsigned char ident[256];
   219     uint32_t identlen = 256;
   220     char cmd[12] = {0x12,0,0,0, 0xFF,0,0,0, 0,0,0,0};
   221     gdrom_error_t status = 
   222         linux_send_command( fd, cmd, ident, &identlen, CGC_DATA_READ );
   223     if( status == 0 ) {
   224         char vendorid[9];
   225         char productid[17];
   226         char productrev[5];
   227         memcpy( vendorid, ident+8, 8 ); vendorid[8] = 0;
   228         memcpy( productid, ident+16, 16 ); productid[16] = 0;
   229         memcpy( productrev, ident+32, 4 ); productrev[4] = 0;
   231         snprintf( buf, buflen, "%.8s %.16s %.4s", trim(vendorid), 
   232                   trim(productid), trim(productrev) );
   233     }
   234     return status;
   235 }
   237 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
   238                                         int mode, unsigned char *buf, uint32_t *length )
   239 {
   240     gdrom_image_t image = (gdrom_image_t)disc;
   241     int fd = fileno(image->file);
   242     uint32_t real_sector = sector - CD_MSF_OFFSET;
   243     uint32_t sector_size = MAX_SECTOR_SIZE;
   244     char cmd[12];
   246     mmc_make_read_cd_cmd( cmd, real_sector, mode );
   248     gdrom_error_t status = linux_send_command( fd, cmd, buf, &sector_size, CGC_DATA_READ );
   249     if( status != 0 ) {
   250         return status;
   251     }
   252     *length = 2048;
   253     return 0;
   254 }
   256 /**
   257  * Send a packet command to the device and wait for a response. 
   258  * @return 0 on success, -1 on an operating system error, or a sense error
   259  * code on a device error.
   260  */
   261 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, uint32_t *buflen,
   262                                          int direction )
   263 {
   264     struct request_sense sense;
   265     struct cdrom_generic_command cgc;
   267     memset( &cgc, 0, sizeof(cgc) );
   268     memset( &sense, 0, sizeof(sense) );
   269     memcpy( cgc.cmd, cmd, 12 );
   270     cgc.buffer = buffer;
   271     cgc.buflen = *buflen;
   272     cgc.sense = &sense;
   273     cgc.data_direction = direction;
   275     if( ioctl(fd, CDROM_SEND_PACKET, &cgc) < 0 ) {
   276         if( sense.sense_key == 0 ) {
   277             return -1; 
   278         } else {
   279             /* TODO: Map newer codes back to the ones used by the gd-rom. */
   280             return sense.sense_key | (sense.asc<<8);
   281         }
   282     } else {
   283         *buflen = cgc.buflen;
   284         return 0;
   285     }
   286 }
.