2 * $Id: linux.c,v 1.1 2006-12-14 12:31:38 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)
33 #define MSFTOLBA( m,s,f ) (f + (s*CD_FRAMES) + (m*CD_FRAMES*CD_SECS))
35 static uint32_t inline lbatomsf( uint32_t lba ) {
36 union cdrom_addr addr;
37 lba = lba + CD_MSF_OFFSET;
38 addr.msf.frame = lba % CD_FRAMES;
39 int seconds = lba / CD_FRAMES;
40 addr.msf.second = seconds % CD_SECS;
41 addr.msf.minute = seconds / CD_SECS;
45 #define LBATOMSF( lba ) lbatomsf(lba)
48 static gboolean linux_image_is_valid( FILE *f );
49 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
50 static gdrom_error_t linux_read_disc_toc( gdrom_disc_t disc );
51 static gdrom_error_t linux_read_sectors( gdrom_disc_t disc, uint32_t sector,
52 uint32_t sector_count, int mode, char *buf,
54 static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t buflen,
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( 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_sectors = linux_read_sectors;
104 disc->disc_type = IDE_DISC_CDROM;
109 * Read the full table of contents into the disc from the device.
111 static gdrom_error_t linux_read_disc_toc( gdrom_disc_t disc )
113 int fd = fileno(disc->file);
114 unsigned char buf[MAXTOCSIZE];
115 char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
117 cmd[7] = (sizeof(buf))>>8;
118 cmd[8] = (sizeof(buf))&0xFF;
119 memset( buf, 0, sizeof(buf) );
120 gdrom_error_t status = linux_send_command( fd, cmd, buf, sizeof(buf), CGC_DATA_READ );
128 int len = (buf[0] << 8) | buf[1];
129 int session_type = GDROM_MODE1;
131 for( i = 4; i<len; i+=11 ) {
132 int session = buf[i];
133 int adr = buf[i+1] >> 4;
134 int point = buf[i+3];
135 if( adr == 0x01 && point > 0 && point < 100 ) {
137 int trackno = point-1;
138 if( point > max_track ) {
141 disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
142 disc->track[trackno].session = session - 1;
143 disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
144 if( disc->track[trackno].flags & TRACK_DATA ) {
145 disc->track[trackno].mode = GDROM_MODE1;
147 disc->track[trackno].mode = GDROM_CDDA;
149 if( last_track != -1 ) {
150 disc->track[last_track].sector_count = disc->track[trackno].lba -
151 disc->track[last_track].lba;
153 last_track = trackno;
154 } else switch( (adr << 8) | point ) {
155 case 0x1A0: /* session info */
156 if( buf[i+9] == 0x20 ) {
157 session_type = GDROM_MODE2;
159 session_type = GDROM_MODE1;
161 case 0x1A2: /* leadout */
162 leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
166 disc->track_count = max_track;
168 if( leadout != -1 && last_track != -1 ) {
169 disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
174 static gdrom_error_t linux_read_sectors_ioctl( gdrom_disc_t disc, uint32_t sector,
175 uint32_t sector_count, int mode, char *buf,
178 int fd = fileno(disc->file);
180 struct cdrom_read read;
181 read.cdread_lba = LBATOMSF(sector);
182 read.cdread_bufaddr = buf;
185 call = CDROMREADMODE1;
186 read.cdread_buflen = sector_count * 2048;
189 call = CDROMREADMODE2;
190 read.cdread_buflen = sector_count * 2336;
193 return PKT_ERR_BADREADMODE;
196 if( ioctl( fd, call, &read ) == -1 ) {
197 ERROR( "Error reading disc (%s)", strerror(errno) );
198 return PKT_ERR_BADREAD;
200 *length = read.cdread_buflen;
204 static gdrom_error_t linux_read_sectors( gdrom_disc_t disc, uint32_t sector,
205 uint32_t sector_count, int mode, char *buf,
208 int fd = fileno(disc->file);
209 uint32_t real_sector = sector - CD_MSF_OFFSET;
210 int buflen = sector_count * 2048;
211 char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
212 cmd[2] = (real_sector >> 24) & 0xFF;
213 cmd[3] = (real_sector >> 16) & 0xFF;
214 cmd[4] = (real_sector >> 8) & 0xFF;
215 cmd[5] = real_sector & 0xFF;
216 cmd[6] = (sector_count >> 16) & 0xFF;
217 cmd[7] = (sector_count >> 8) & 0xFF;
218 cmd[8] = sector_count & 0xFF;
221 gdrom_error_t status = linux_send_command( fd, cmd, buf, buflen, CGC_DATA_READ );
229 * Send a packet command to the device and wait for a response.
230 * @return 0 on success, -1 on an operating system error, or a sense error
231 * code on a device error.
233 static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t buflen,
236 struct request_sense sense;
237 struct cdrom_generic_command cgc;
239 memset( &cgc, 0, sizeof(cgc) );
240 memset( &sense, 0, sizeof(sense) );
241 memcpy( cgc.cmd, cmd, 12 );
245 cgc.data_direction = direction;
247 if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
248 if( sense.sense_key == 0 ) {
251 return sense.sense_key | (sense.asc<<8);
.