Search
lxdream.org :: lxdream/src/gdrom/linux.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/linux.c
changeset 489:45c8ddcf52cb
prev480:d28c2992f5ee
author nkeynes
date Thu Nov 15 08:16:10 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Add support for CDI V4
view annotate diff log raw
     1 /**
     2  * $Id: linux.c,v 1.9 2007-11-04 05:07:49 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 <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/gdrom.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 #define MSFTOLBA( m,s,f ) (f + (s*CD_FRAMES) + (m*CD_FRAMES*CD_SECS))
    39 static uint32_t inline lbatomsf( uint32_t lba ) {
    40     union cdrom_addr addr;
    41     lba = lba + CD_MSF_OFFSET;
    42     addr.msf.frame = lba % CD_FRAMES;
    43     int seconds = lba / CD_FRAMES;
    44     addr.msf.second = seconds % CD_SECS;
    45     addr.msf.minute = seconds / CD_SECS;
    46     return addr.lba;
    47 }
    49 #define LBATOMSF( lba ) lbatomsf(lba)
    52 static gboolean linux_image_is_valid( FILE *f );
    53 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
    54 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc );
    55 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
    56 					int mode, unsigned char *buf, uint32_t *length );
    57 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
    58 					 int direction );
    59 static int linux_drive_status( gdrom_disc_t disc );
    61 struct gdrom_image_class cdrom_device_class = { "Linux", NULL,
    62 						linux_image_is_valid, linux_open_device };
    63 GList *gdrom_get_native_devices(void)
    64 {
    65     GList *list = NULL;
    66     struct fstab *ent;
    67     struct stat st;
    68     setfsent();
    69     while( (ent = getfsent()) != NULL ) {
    70 	if( (stat(ent->fs_spec, &st) != -1) && 
    71 	    S_ISBLK(st.st_mode) ) {
    72 	    /* Got a valid block device - is it a CDROM? */
    73 	    int fd = open(ent->fs_spec, O_RDONLY|O_NONBLOCK);
    74 	    if( fd == -1 )
    75 		continue;
    76 	    int caps = ioctl(fd, CDROM_GET_CAPABILITY);
    77 	    if( caps != -1 ) {
    78 		/* Appears to support CDROM functions */
    79 		list = g_list_append( list, g_strdup(ent->fs_spec) );
    80 	    }
    81 	    close(fd);
    82 	}
    83     }
    84     return list;
    85 }
    87 static gboolean linux_image_is_valid( FILE *f )
    88 {
    89     struct stat st;
    90     struct cdrom_tochdr tochdr;
    92     if( fstat(fileno(f), &st) == -1 ) {
    93 	return FALSE; /* can't stat device? */
    94     }
    95     if( !S_ISBLK(st.st_mode) ) {
    96 	return FALSE; /* Not a block device */
    97     }
    99     if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
   100 	/* Quick check that this is really a CD */
   101 	return FALSE;
   102     }
   104     return TRUE;
   105 }
   107 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
   108 {
   109     gdrom_disc_t disc;
   111     disc = gdrom_image_new(filename, f);
   112     if( disc == NULL ) {
   113 	ERROR("Unable to allocate memory!");
   114 	return NULL;
   115     }
   117     gdrom_error_t status = linux_read_disc_toc( (gdrom_image_t)disc );
   118     if( status != 0 ) {
   119 	gdrom_image_destroy_no_close(disc);
   120 	if( status == 0xFFFF ) {
   121 	    ERROR("Unable to load disc table of contents (%s)", strerror(errno));
   122 	} else {
   123 	    ERROR("Unable to load disc table of contents (sense %d,%d)",
   124 		  status &0xFF, status >> 8 );
   125 	}
   126 	return NULL;
   127     }
   128     disc->read_sector = linux_read_sector;
   129     disc->drive_status = linux_drive_status;
   130     ((gdrom_image_t)disc)->disc_type = IDE_DISC_CDROM;
   131     return disc;
   132 }
   134 static int linux_drive_status( gdrom_disc_t disc )
   135 {
   136     int fd = fileno(((gdrom_image_t)disc)->file);
   137     int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
   138     if( status == CDS_DISC_OK ) {
   139 	status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
   140 	if( status != 0 ) {
   141 	    linux_read_disc_toc( (gdrom_image_t)disc);
   142 	}
   143 	return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
   144     } else {
   145 	return IDE_DISC_NONE;
   146     }
   147 }
   148 /**
   149  * Read the full table of contents into the disc from the device.
   150  */
   151 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
   152 {
   153     int fd = fileno(disc->file);
   154     unsigned char buf[MAXTOCSIZE];
   155     size_t buflen = sizeof(buf);
   156     char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   158     cmd[7] = (sizeof(buf))>>8;
   159     cmd[8] = (sizeof(buf))&0xFF;
   160     memset( buf, 0, sizeof(buf) );
   161     gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
   162     if( status != 0 ) {
   163 	return status;
   164     }
   166     int max_track = 0;
   167     int last_track = -1;
   168     int leadout = -1;
   169     int len = (buf[0] << 8) | buf[1];
   170     int session_type = GDROM_MODE1;
   171     int i;
   172     for( i = 4; i<len; i+=11 ) {
   173 	int session = buf[i];
   174 	int adr = buf[i+1] >> 4;
   175 	int point = buf[i+3];
   176 	if( adr == 0x01 && point > 0 && point < 100 ) {
   177 	    /* Track info */
   178 	    int trackno = point-1;
   179 	    if( point > max_track ) {
   180 		max_track = point;
   181 	    }
   182 	    disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
   183 	    disc->track[trackno].session = session - 1;
   184 	    disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
   185 	    if( disc->track[trackno].flags & TRACK_DATA ) {
   186 		disc->track[trackno].mode = GDROM_MODE1;
   187 	    } else {
   188 		disc->track[trackno].mode = GDROM_CDDA;
   189 	    }
   190 	    if( last_track != -1 ) {
   191 		disc->track[last_track].sector_count = disc->track[trackno].lba -
   192 		    disc->track[last_track].lba;
   193 	    }
   194 	    last_track = trackno;
   195 	} else switch( (adr << 8) | point ) {
   196 	case 0x1A0: /* session info */
   197 	    if( buf[i+9] == 0x20 ) {
   198 		session_type = GDROM_MODE2;
   199 	    } else {
   200 		session_type = GDROM_MODE1;
   201 	    }
   202 	case 0x1A2: /* leadout */
   203 	    leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
   204 	    break;
   205 	}
   206     }
   207     disc->track_count = max_track;
   209     if( leadout != -1 && last_track != -1 ) {
   210 	disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
   211     }
   212     return 0;
   213 }
   215  gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
   216 {
   217     int fd = fileno( ((gdrom_image_t)disc)->file );
   218     uint32_t real_sector = lba - CD_MSF_OFFSET;
   219     uint32_t length = endlba - lba;
   220     uint32_t buflen = 0;
   221     char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
   222     cmd[2] = (real_sector >> 24) & 0xFF;
   223     cmd[3] = (real_sector >> 16) & 0xFF;
   224     cmd[4] = (real_sector >> 8) & 0xFF;
   225     cmd[5] = real_sector & 0xFF;
   226     cmd[6] = (length >> 24) & 0xFF;
   227     cmd[7] = (length >> 16) & 0xFF;
   228     cmd[8] = (length >> 8) & 0xFF;
   229     cmd[9] = length & 0xFF;
   231     return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
   232 }
   234 gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
   235 {
   236     int fd = fileno( ((gdrom_image_t)disc)->file );
   237     uint32_t buflen = 0;
   238     char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
   239     return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
   240 }
   242 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
   243 					int mode, unsigned char *buf, uint32_t *length )
   244 {
   245     gdrom_image_t image = (gdrom_image_t)disc;
   246     int fd = fileno(image->file);
   247     uint32_t real_sector = sector - CD_MSF_OFFSET;
   248     uint32_t sector_size = MAX_SECTOR_SIZE;
   249     char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   251     cmd[1] = (mode & 0x0E) << 1;
   252     cmd[2] = (real_sector >> 24) & 0xFF;
   253     cmd[3] = (real_sector >> 16) & 0xFF;
   254     cmd[4] = (real_sector >> 8) & 0xFF;
   255     cmd[5] = real_sector & 0xFF;
   256     cmd[6] = 0;
   257     cmd[7] = 0;
   258     cmd[8] = 1;
   259     cmd[9] = 0x10;
   261     gdrom_error_t status = linux_send_command( fd, cmd, buf, &sector_size, CGC_DATA_READ );
   262     if( status != 0 ) {
   263 	return status;
   264     }
   265     *length = 2048;
   266     return 0;
   267 }
   269 /**
   270  * Send a packet command to the device and wait for a response. 
   271  * @return 0 on success, -1 on an operating system error, or a sense error
   272  * code on a device error.
   273  */
   274 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
   275 					 int direction )
   276 {
   277     struct request_sense sense;
   278     struct cdrom_generic_command cgc;
   280     memset( &cgc, 0, sizeof(cgc) );
   281     memset( &sense, 0, sizeof(sense) );
   282     memcpy( cgc.cmd, cmd, 12 );
   283     cgc.buffer = buffer;
   284     cgc.buflen = *buflen;
   285     cgc.sense = &sense;
   286     cgc.data_direction = direction;
   288     if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
   289 	if( sense.sense_key == 0 ) {
   290 	    return -1; 
   291 	} else {
   292 	    /* TODO: Map newer codes back to the ones used by the gd-rom. */
   293 	    return sense.sense_key | (sense.asc<<8);
   294 	}
   295     } else {
   296 	*buflen = cgc.buflen;
   297 	return 0;
   298     }
   299 }
.