Search
lxdream.org :: lxdream/src/gdrom/linux.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/linux.c
changeset 468:3a49695e081a
prev464:8e099fad42a6
next480:d28c2992f5ee
author nkeynes
date Sun Oct 28 07:23:46 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Fix CDI with multiple tracks/session
Fix file being closed too early
view annotate diff log raw
     1 /**
     2  * $Id: linux.c,v 1.7 2007-10-28 07:23:46 nkeynes Exp $
     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 <stdlib.h>
    19 #include <stdio.h>
    20 #include <string.h>
    21 #include <errno.h>
    22 #include <linux/cdrom.h>
    23 #include <sys/stat.h>
    24 #include <sys/ioctl.h>
    25 #include <fstab.h>
    26 #include <fcntl.h>
    28 #include "gdrom/gdrom.h"
    29 #include "gdrom/packet.h"
    30 #include "dream.h"
    32 #define MAXTOCENTRIES 600  /* This is a fairly generous overestimate really */
    33 #define MAXTOCSIZE 4 + (MAXTOCENTRIES*11)
    34 #define MAX_SECTORS_PER_CALL 1
    36 #define MSFTOLBA( m,s,f ) (f + (s*CD_FRAMES) + (m*CD_FRAMES*CD_SECS))
    38 static uint32_t inline lbatomsf( uint32_t lba ) {
    39     union cdrom_addr addr;
    40     lba = lba + CD_MSF_OFFSET;
    41     addr.msf.frame = lba % CD_FRAMES;
    42     int seconds = lba / CD_FRAMES;
    43     addr.msf.second = seconds % CD_SECS;
    44     addr.msf.minute = seconds / CD_SECS;
    45     return addr.lba;
    46 }
    48 #define LBATOMSF( lba ) lbatomsf(lba)
    51 static gboolean linux_image_is_valid( FILE *f );
    52 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
    53 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc );
    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, size_t *buflen,
    57 					 int direction );
    58 static int linux_drive_status( gdrom_disc_t disc );
    60 struct gdrom_image_class linux_device_class = { "Linux", NULL,
    61 					     linux_image_is_valid, linux_open_device };
    62 GList *gdrom_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 		list = g_list_append( list, g_strdup(ent->fs_spec) );
    79 	    }
    80 	    close(fd);
    81 	}
    82     }
    83     return list;
    84 }
    86 static gboolean linux_image_is_valid( FILE *f )
    87 {
    88     struct stat st;
    89     struct cdrom_tochdr tochdr;
    91     if( fstat(fileno(f), &st) == -1 ) {
    92 	return FALSE; /* can't stat device? */
    93     }
    94     if( !S_ISBLK(st.st_mode) ) {
    95 	return FALSE; /* Not a block device */
    96     }
    98     if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
    99 	/* Quick check that this is really a CD */
   100 	return FALSE;
   101     }
   103     return TRUE;
   104 }
   106 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
   107 {
   108     gdrom_disc_t disc;
   110     disc = gdrom_image_new(filename, f);
   111     if( disc == NULL ) {
   112 	ERROR("Unable to allocate memory!");
   113 	return NULL;
   114     }
   116     gdrom_error_t status = linux_read_disc_toc( (gdrom_image_t)disc );
   117     if( status != 0 ) {
   118 	gdrom_image_destroy_no_close(disc);
   119 	if( status == 0xFFFF ) {
   120 	    ERROR("Unable to load disc table of contents (%s)", strerror(errno));
   121 	} else {
   122 	    ERROR("Unable to load disc table of contents (sense %d,%d)",
   123 		  status &0xFF, status >> 8 );
   124 	}
   125 	return NULL;
   126     }
   127     disc->read_sector = linux_read_sector;
   128     disc->drive_status = linux_drive_status;
   129     ((gdrom_image_t)disc)->disc_type = IDE_DISC_CDROM;
   130     return disc;
   131 }
   133 static int linux_drive_status( gdrom_disc_t disc )
   134 {
   135     int fd = fileno(((gdrom_image_t)disc)->file);
   136     int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
   137     if( status == CDS_DISC_OK ) {
   138 	status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
   139 	if( status != 0 ) {
   140 	    linux_read_disc_toc( (gdrom_image_t)disc);
   141 	}
   142 	return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
   143     } else {
   144 	return IDE_DISC_NONE;
   145     }
   146 }
   147 /**
   148  * Read the full table of contents into the disc from the device.
   149  */
   150 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
   151 {
   152     int fd = fileno(disc->file);
   153     unsigned char buf[MAXTOCSIZE];
   154     size_t buflen = sizeof(buf);
   155     char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   157     cmd[7] = (sizeof(buf))>>8;
   158     cmd[8] = (sizeof(buf))&0xFF;
   159     memset( buf, 0, sizeof(buf) );
   160     gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
   161     if( status != 0 ) {
   162 	return status;
   163     }
   165     int max_track = 0;
   166     int last_track = -1;
   167     int leadout = -1;
   168     int len = (buf[0] << 8) | buf[1];
   169     int session_type = GDROM_MODE1;
   170     int i;
   171     for( i = 4; i<len; i+=11 ) {
   172 	int session = buf[i];
   173 	int adr = buf[i+1] >> 4;
   174 	int point = buf[i+3];
   175 	if( adr == 0x01 && point > 0 && point < 100 ) {
   176 	    /* Track info */
   177 	    int trackno = point-1;
   178 	    if( point > max_track ) {
   179 		max_track = point;
   180 	    }
   181 	    disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
   182 	    disc->track[trackno].session = session - 1;
   183 	    disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
   184 	    if( disc->track[trackno].flags & TRACK_DATA ) {
   185 		disc->track[trackno].mode = GDROM_MODE1;
   186 	    } else {
   187 		disc->track[trackno].mode = GDROM_CDDA;
   188 	    }
   189 	    if( last_track != -1 ) {
   190 		disc->track[last_track].sector_count = disc->track[trackno].lba -
   191 		    disc->track[last_track].lba;
   192 	    }
   193 	    last_track = trackno;
   194 	} else switch( (adr << 8) | point ) {
   195 	case 0x1A0: /* session info */
   196 	    if( buf[i+9] == 0x20 ) {
   197 		session_type = GDROM_MODE2;
   198 	    } else {
   199 		session_type = GDROM_MODE1;
   200 	    }
   201 	case 0x1A2: /* leadout */
   202 	    leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
   203 	    break;
   204 	}
   205     }
   206     disc->track_count = max_track;
   208     if( leadout != -1 && last_track != -1 ) {
   209 	disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
   210     }
   211     return 0;
   212 }
   214 static gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
   215 {
   216     int fd = fileno( ((gdrom_image_t)disc)->file );
   217     uint32_t real_sector = lba - CD_MSF_OFFSET;
   218     uint32_t length = endlba - lba;
   219     uint32_t buflen = 0;
   220     char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
   221     cmd[2] = (real_sector >> 24) & 0xFF;
   222     cmd[3] = (real_sector >> 16) & 0xFF;
   223     cmd[4] = (real_sector >> 8) & 0xFF;
   224     cmd[5] = real_sector & 0xFF;
   225     cmd[6] = (length >> 24) & 0xFF;
   226     cmd[7] = (length >> 16) & 0xFF;
   227     cmd[8] = (length >> 8) & 0xFF;
   228     cmd[9] = length & 0xFF;
   230     return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
   231 }
   233 static gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
   234 {
   235     int fd = fileno( ((gdrom_image_t)disc)->file );
   236     uint32_t buflen = 0;
   237     char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
   238     return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
   239 }
   241 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
   242 					int mode, unsigned char *buf, uint32_t *length )
   243 {
   244     gdrom_image_t image = (gdrom_image_t)disc;
   245     int fd = fileno(image->file);
   246     uint32_t real_sector = sector - CD_MSF_OFFSET;
   247     uint32_t sector_size = MAX_SECTOR_SIZE;
   248     char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   250     cmd[1] = (mode & 0x0E) << 1;
   251     cmd[2] = (real_sector >> 24) & 0xFF;
   252     cmd[3] = (real_sector >> 16) & 0xFF;
   253     cmd[4] = (real_sector >> 8) & 0xFF;
   254     cmd[5] = real_sector & 0xFF;
   255     cmd[6] = 0;
   256     cmd[7] = 0;
   257     cmd[8] = 1;
   258     cmd[9] = 0x10;
   260     gdrom_error_t status = linux_send_command( fd, cmd, buf, &sector_size, CGC_DATA_READ );
   261     if( status != 0 ) {
   262 	return status;
   263     }
   264     *length = 2048;
   265     return 0;
   266 }
   268 /**
   269  * Send a packet command to the device and wait for a response. 
   270  * @return 0 on success, -1 on an operating system error, or a sense error
   271  * code on a device error.
   272  */
   273 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
   274 					 int direction )
   275 {
   276     struct request_sense sense;
   277     struct cdrom_generic_command cgc;
   279     memset( &cgc, 0, sizeof(cgc) );
   280     memset( &sense, 0, sizeof(sense) );
   281     memcpy( cgc.cmd, cmd, 12 );
   282     cgc.buffer = buffer;
   283     cgc.buflen = *buflen;
   284     cgc.sense = &sense;
   285     cgc.data_direction = direction;
   287     if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
   288 	if( sense.sense_key == 0 ) {
   289 	    return -1; 
   290 	} else {
   291 	    /* TODO: Map newer codes back to the ones used by the gd-rom. */
   292 	    return sense.sense_key | (sense.asc<<8);
   293 	}
   294     } else {
   295 	*buflen = cgc.buflen;
   296 	return 0;
   297     }
   298 }
.