2 * $Id: linux.c,v 1.4 2007-02-11 10:11:05 nkeynes Exp $
4 * Linux cd-rom device driver.
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
22 #include <linux/cdrom.h>
24 #include <sys/ioctl.h>
26 #include "gdrom/gdrom.h"
27 #include "gdrom/packet.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;
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, char *buf, uint32_t *length );
54 static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t *buflen,
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 )
64 struct cdrom_tochdr tochdr;
66 if( fstat(fileno(f), &st) == -1 ) {
67 return FALSE; /* can't stat device? */
69 if( !S_ISBLK(st.st_mode) ) {
70 return FALSE; /* Not a block device */
73 if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
74 /* Quick check that this is really a CD */
81 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f )
86 disc = gdrom_image_new(f);
88 ERROR("Unable to allocate memory!");
92 gdrom_error_t status = linux_read_disc_toc( (gdrom_image_t)disc );
95 if( status == 0xFFFF ) {
96 ERROR("Unable to load disc table of contents (%s)", strerror(errno));
98 ERROR("Unable to load disc table of contents (sense %d,%d)",
99 status &0xFF, status >> 8 );
103 disc->read_sector = linux_read_sector;
104 disc->drive_status = linux_drive_status;
105 ((gdrom_image_t)disc)->disc_type = IDE_DISC_CDROM;
109 static int linux_drive_status( gdrom_disc_t disc )
111 int fd = fileno(((gdrom_image_t)disc)->file);
112 int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
113 if( status == CDS_DISC_OK ) {
114 status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
116 linux_read_disc_toc(disc);
118 return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
120 return IDE_DISC_NONE;
124 * Read the full table of contents into the disc from the device.
126 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
128 int fd = fileno(disc->file);
129 unsigned char buf[MAXTOCSIZE];
130 int buflen = sizeof(buf);
131 char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
133 cmd[7] = (sizeof(buf))>>8;
134 cmd[8] = (sizeof(buf))&0xFF;
135 memset( buf, 0, sizeof(buf) );
136 gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
144 int len = (buf[0] << 8) | buf[1];
145 int session_type = GDROM_MODE1;
147 for( i = 4; i<len; i+=11 ) {
148 int session = buf[i];
149 int adr = buf[i+1] >> 4;
150 int point = buf[i+3];
151 if( adr == 0x01 && point > 0 && point < 100 ) {
153 int trackno = point-1;
154 if( point > max_track ) {
157 disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
158 disc->track[trackno].session = session - 1;
159 disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
160 if( disc->track[trackno].flags & TRACK_DATA ) {
161 disc->track[trackno].mode = GDROM_MODE1;
163 disc->track[trackno].mode = GDROM_CDDA;
165 if( last_track != -1 ) {
166 disc->track[last_track].sector_count = disc->track[trackno].lba -
167 disc->track[last_track].lba;
169 last_track = trackno;
170 } else switch( (adr << 8) | point ) {
171 case 0x1A0: /* session info */
172 if( buf[i+9] == 0x20 ) {
173 session_type = GDROM_MODE2;
175 session_type = GDROM_MODE1;
177 case 0x1A2: /* leadout */
178 leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
182 disc->track_count = max_track;
184 if( leadout != -1 && last_track != -1 ) {
185 disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
190 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
191 int mode, char *buf, uint32_t *length )
193 gdrom_image_t image = (gdrom_image_t)disc;
194 int fd = fileno(image->file);
195 uint32_t real_sector = sector - CD_MSF_OFFSET;
196 uint32_t sector_size = MAX_SECTOR_SIZE;
198 char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
200 cmd[1] = (mode & 0x0E) << 1;
201 cmd[2] = (real_sector >> 24) & 0xFF;
202 cmd[3] = (real_sector >> 16) & 0xFF;
203 cmd[4] = (real_sector >> 8) & 0xFF;
204 cmd[5] = real_sector & 0xFF;
210 gdrom_error_t status = linux_send_command( fd, cmd, buf, §or_size, CGC_DATA_READ );
219 * Send a packet command to the device and wait for a response.
220 * @return 0 on success, -1 on an operating system error, or a sense error
221 * code on a device error.
223 static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t *buflen,
226 struct request_sense sense;
227 struct cdrom_generic_command cgc;
229 memset( &cgc, 0, sizeof(cgc) );
230 memset( &sense, 0, sizeof(sense) );
231 memcpy( cgc.cmd, cmd, 12 );
233 cgc.buflen = *buflen;
235 cgc.data_direction = direction;
237 if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
238 if( sense.sense_key == 0 ) {
241 /* TODO: Map newer codes back to the ones used by the gd-rom. */
242 return sense.sense_key | (sense.asc<<8);
245 *buflen = cgc.buflen;
.