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.
23 #include <linux/cdrom.h>
25 #include <sys/ioctl.h>
29 #include "gdrom/gddriver.h"
30 #include "gdrom/packet.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;
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,
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 *cdrom_get_native_devices(void)
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);
76 int caps = ioctl(fd, CDROM_GET_CAPABILITY);
78 /* Appears to support CDROM functions */
79 list = g_list_append( list, gdrom_device_new(ent->fs_spec, ent->fs_spec));
87 gdrom_disc_t cdrom_open_device( const gchar *method, const gchar *path )
92 static gboolean linux_image_is_valid( FILE *f )
95 struct cdrom_tochdr tochdr;
97 if( fstat(fileno(f), &st) == -1 ) {
98 return FALSE; /* can't stat device? */
100 if( !S_ISBLK(st.st_mode) ) {
101 return FALSE; /* Not a block device */
104 if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
105 /* Quick check that this is really a CD */
112 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f )
116 disc = gdrom_image_new(filename, f);
118 ERROR("Unable to allocate memory!");
122 gdrom_error_t status = linux_read_disc_toc( (gdrom_image_t)disc );
124 gdrom_image_destroy_no_close(disc);
125 if( status == 0xFFFF ) {
126 ERROR("Unable to load disc table of contents (%s)", strerror(errno));
128 ERROR("Unable to load disc table of contents (sense %d,%d)",
129 status &0xFF, status >> 8 );
133 disc->read_sector = linux_read_sector;
134 disc->drive_status = linux_drive_status;
138 static int linux_drive_status( gdrom_disc_t disc )
140 int fd = fileno(((gdrom_image_t)disc)->file);
141 int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
142 if( status == CDS_DISC_OK ) {
143 status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
145 linux_read_disc_toc( (gdrom_image_t)disc);
147 return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
149 return IDE_DISC_NONE;
153 * Read the full table of contents into the disc from the device.
155 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
157 int fd = fileno(disc->file);
158 unsigned char buf[MAXTOCSIZE];
159 size_t buflen = sizeof(buf);
160 char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
162 cmd[7] = (sizeof(buf))>>8;
163 cmd[8] = (sizeof(buf))&0xFF;
164 memset( buf, 0, sizeof(buf) );
165 gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
173 int len = (buf[0] << 8) | buf[1];
174 int session_type = -1;
176 for( i = 4; i<len; i+=11 ) {
177 int session = buf[i];
178 int adr = buf[i+1] >> 4;
179 int point = buf[i+3];
180 if( adr == 0x01 && point > 0 && point < 100 ) {
182 int trackno = point-1;
183 if( point > max_track ) {
186 disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
187 disc->track[trackno].session = session - 1;
188 disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
189 if( disc->track[trackno].flags & TRACK_DATA ) {
190 disc->track[trackno].mode = GDROM_MODE1;
192 disc->track[trackno].mode = GDROM_CDDA;
194 if( last_track != -1 ) {
195 disc->track[last_track].sector_count = disc->track[trackno].lba -
196 disc->track[last_track].lba;
198 last_track = trackno;
199 } else switch( (adr << 8) | point ) {
200 case 0x1A0: /* session info */
201 if( buf[i+9] == 0x20 ) {
202 session_type = IDE_DISC_CDROMXA;
204 session_type = IDE_DISC_CDROM;
206 disc->disc_type = session_type;
208 case 0x1A2: /* leadout */
209 leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
213 disc->track_count = max_track;
215 if( leadout != -1 && last_track != -1 ) {
216 disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
221 gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
223 int fd = fileno( ((gdrom_image_t)disc)->file );
224 uint32_t real_sector = lba - CD_MSF_OFFSET;
225 uint32_t length = endlba - lba;
227 char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
228 cmd[2] = (real_sector >> 24) & 0xFF;
229 cmd[3] = (real_sector >> 16) & 0xFF;
230 cmd[4] = (real_sector >> 8) & 0xFF;
231 cmd[5] = real_sector & 0xFF;
232 cmd[6] = (length >> 24) & 0xFF;
233 cmd[7] = (length >> 16) & 0xFF;
234 cmd[8] = (length >> 8) & 0xFF;
235 cmd[9] = length & 0xFF;
237 return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
240 gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
242 int fd = fileno( ((gdrom_image_t)disc)->file );
244 char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
245 return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
248 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
249 int mode, unsigned char *buf, uint32_t *length )
251 gdrom_image_t image = (gdrom_image_t)disc;
252 int fd = fileno(image->file);
253 uint32_t real_sector = sector - CD_MSF_OFFSET;
254 uint32_t sector_size = MAX_SECTOR_SIZE;
255 char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
257 cmd[1] = (mode & 0x0E) << 1;
258 cmd[2] = (real_sector >> 24) & 0xFF;
259 cmd[3] = (real_sector >> 16) & 0xFF;
260 cmd[4] = (real_sector >> 8) & 0xFF;
261 cmd[5] = real_sector & 0xFF;
266 if( READ_CD_RAW(mode) ) {
269 if( READ_CD_HEADER(mode) ) {
272 if( READ_CD_SUBHEAD(mode) ) {
275 if( READ_CD_DATA(mode) ) {
280 gdrom_error_t status = linux_send_command( fd, cmd, buf, §or_size, CGC_DATA_READ );
289 * Send a packet command to the device and wait for a response.
290 * @return 0 on success, -1 on an operating system error, or a sense error
291 * code on a device error.
293 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
296 struct request_sense sense;
297 struct cdrom_generic_command cgc;
299 memset( &cgc, 0, sizeof(cgc) );
300 memset( &sense, 0, sizeof(sense) );
301 memcpy( cgc.cmd, cmd, 12 );
303 cgc.buflen = *buflen;
305 cgc.data_direction = direction;
307 if( ioctl(fd, CDROM_SEND_PACKET, &cgc) < 0 ) {
308 if( sense.sense_key == 0 ) {
311 /* TODO: Map newer codes back to the ones used by the gd-rom. */
312 return sense.sense_key | (sense.asc<<8);
315 *buflen = cgc.buflen;
.