Search
lxdream.org :: lxdream/src/gdrom/linux.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/linux.c
changeset 422:61a0598e07ff
prev353:b8569afb53fc
next464:8e099fad42a6
author nkeynes
date Sun Oct 21 05:20:15 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Add CDI V3.5 support
view annotate diff log raw
     1 /**
     2  * $Id: linux.c,v 1.5 2007-10-06 08:58:00 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>
    26 #include "gdrom/gdrom.h"
    27 #include "gdrom/packet.h"
    28 #include "dream.h"
    30 #define MAXTOCENTRIES 600  /* This is a fairly generous overestimate really */
    31 #define MAXTOCSIZE 4 + (MAXTOCENTRIES*11)
    32 #define MAX_SECTORS_PER_CALL 1
    34 #define MSFTOLBA( m,s,f ) (f + (s*CD_FRAMES) + (m*CD_FRAMES*CD_SECS))
    36 static uint32_t inline lbatomsf( uint32_t lba ) {
    37     union cdrom_addr addr;
    38     lba = lba + CD_MSF_OFFSET;
    39     addr.msf.frame = lba % CD_FRAMES;
    40     int seconds = lba / CD_FRAMES;
    41     addr.msf.second = seconds % CD_SECS;
    42     addr.msf.minute = seconds / CD_SECS;
    43     return addr.lba;
    44 }
    46 #define LBATOMSF( lba ) lbatomsf(lba)
    49 static gboolean linux_image_is_valid( FILE *f );
    50 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
    51 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc );
    52 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
    53 					int mode, unsigned char *buf, uint32_t *length );
    54 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
    55 					 int direction );
    56 static int linux_drive_status( gdrom_disc_t disc );
    58 struct gdrom_image_class linux_device_class = { "Linux", NULL,
    59 					     linux_image_is_valid, linux_open_device };
    61 static gboolean linux_image_is_valid( FILE *f )
    62 {
    63     struct stat st;
    64     struct cdrom_tochdr tochdr;
    66     if( fstat(fileno(f), &st) == -1 ) {
    67 	return FALSE; /* can't stat device? */
    68     }
    69     if( !S_ISBLK(st.st_mode) ) {
    70 	return FALSE; /* Not a block device */
    71     }
    73     if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
    74 	/* Quick check that this is really a CD */
    75 	return FALSE;
    76     }
    78     return TRUE;
    79 }
    81 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
    82 {
    83     gdrom_disc_t disc;
    85     disc = gdrom_image_new(f);
    86     if( disc == NULL ) {
    87 	ERROR("Unable to allocate memory!");
    88 	return NULL;
    89     }
    91     gdrom_error_t status = linux_read_disc_toc( (gdrom_image_t)disc );
    92     if( status != 0 ) {
    93 	disc->close(disc);
    94 	if( status == 0xFFFF ) {
    95 	    ERROR("Unable to load disc table of contents (%s)", strerror(errno));
    96 	} else {
    97 	    ERROR("Unable to load disc table of contents (sense %d,%d)",
    98 		  status &0xFF, status >> 8 );
    99 	}
   100 	return NULL;
   101     }
   102     disc->read_sector = linux_read_sector;
   103     disc->drive_status = linux_drive_status;
   104     ((gdrom_image_t)disc)->disc_type = IDE_DISC_CDROM;
   105     return disc;
   106 }
   108 static int linux_drive_status( gdrom_disc_t disc )
   109 {
   110     int fd = fileno(((gdrom_image_t)disc)->file);
   111     int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
   112     if( status == CDS_DISC_OK ) {
   113 	status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
   114 	if( status != 0 ) {
   115 	    linux_read_disc_toc( (gdrom_image_t)disc);
   116 	}
   117 	return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
   118     } else {
   119 	return IDE_DISC_NONE;
   120     }
   121 }
   122 /**
   123  * Read the full table of contents into the disc from the device.
   124  */
   125 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
   126 {
   127     int fd = fileno(disc->file);
   128     unsigned char buf[MAXTOCSIZE];
   129     size_t buflen = sizeof(buf);
   130     char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   132     cmd[7] = (sizeof(buf))>>8;
   133     cmd[8] = (sizeof(buf))&0xFF;
   134     memset( buf, 0, sizeof(buf) );
   135     gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
   136     if( status != 0 ) {
   137 	return status;
   138     }
   140     int max_track = 0;
   141     int last_track = -1;
   142     int leadout = -1;
   143     int len = (buf[0] << 8) | buf[1];
   144     int session_type = GDROM_MODE1;
   145     int i;
   146     for( i = 4; i<len; i+=11 ) {
   147 	int session = buf[i];
   148 	int adr = buf[i+1] >> 4;
   149 	int point = buf[i+3];
   150 	if( adr == 0x01 && point > 0 && point < 100 ) {
   151 	    /* Track info */
   152 	    int trackno = point-1;
   153 	    if( point > max_track ) {
   154 		max_track = point;
   155 	    }
   156 	    disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
   157 	    disc->track[trackno].session = session - 1;
   158 	    disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
   159 	    if( disc->track[trackno].flags & TRACK_DATA ) {
   160 		disc->track[trackno].mode = GDROM_MODE1;
   161 	    } else {
   162 		disc->track[trackno].mode = GDROM_CDDA;
   163 	    }
   164 	    if( last_track != -1 ) {
   165 		disc->track[last_track].sector_count = disc->track[trackno].lba -
   166 		    disc->track[last_track].lba;
   167 	    }
   168 	    last_track = trackno;
   169 	} else switch( (adr << 8) | point ) {
   170 	case 0x1A0: /* session info */
   171 	    if( buf[i+9] == 0x20 ) {
   172 		session_type = GDROM_MODE2;
   173 	    } else {
   174 		session_type = GDROM_MODE1;
   175 	    }
   176 	case 0x1A2: /* leadout */
   177 	    leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
   178 	    break;
   179 	}
   180     }
   181     disc->track_count = max_track;
   183     if( leadout != -1 && last_track != -1 ) {
   184 	disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
   185     }
   186     return 0;
   187 }
   189 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
   190 					int mode, unsigned char *buf, uint32_t *length )
   191 {
   192     gdrom_image_t image = (gdrom_image_t)disc;
   193     int fd = fileno(image->file);
   194     uint32_t real_sector = sector - CD_MSF_OFFSET;
   195     uint32_t sector_size = MAX_SECTOR_SIZE;
   196     char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   198     cmd[1] = (mode & 0x0E) << 1;
   199     cmd[2] = (real_sector >> 24) & 0xFF;
   200     cmd[3] = (real_sector >> 16) & 0xFF;
   201     cmd[4] = (real_sector >> 8) & 0xFF;
   202     cmd[5] = real_sector & 0xFF;
   203     cmd[6] = 0;
   204     cmd[7] = 0;
   205     cmd[8] = 1;
   206     cmd[9] = 0x10;
   208     gdrom_error_t status = linux_send_command( fd, cmd, buf, &sector_size, CGC_DATA_READ );
   209     if( status != 0 ) {
   210 	return status;
   211     }
   212     *length = 2048;
   213     return 0;
   214 }
   216 /**
   217  * Send a packet command to the device and wait for a response. 
   218  * @return 0 on success, -1 on an operating system error, or a sense error
   219  * code on a device error.
   220  */
   221 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
   222 					 int direction )
   223 {
   224     struct request_sense sense;
   225     struct cdrom_generic_command cgc;
   227     memset( &cgc, 0, sizeof(cgc) );
   228     memset( &sense, 0, sizeof(sense) );
   229     memcpy( cgc.cmd, cmd, 12 );
   230     cgc.buffer = buffer;
   231     cgc.buflen = *buflen;
   232     cgc.sense = &sense;
   233     cgc.data_direction = direction;
   235     if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
   236 	if( sense.sense_key == 0 ) {
   237 	    return -1; 
   238 	} else {
   239 	    /* TODO: Map newer codes back to the ones used by the gd-rom. */
   240 	    return sense.sense_key | (sense.asc<<8);
   241 	}
   242     } else {
   243 	*buflen = cgc.buflen;
   244 	return 0;
   245     }
   246 }
.