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 static uint32_t inline lbatomsf( uint32_t lba ) {
38 union cdrom_addr addr;
39 lba = lba + CD_MSF_OFFSET;
40 addr.msf.frame = lba % CD_FRAMES;
41 int seconds = lba / CD_FRAMES;
42 addr.msf.second = seconds % CD_SECS;
43 addr.msf.minute = seconds / CD_SECS;
47 #define LBATOMSF( lba ) lbatomsf(lba)
50 static gboolean linux_image_is_valid( FILE *f );
51 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
52 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc );
53 static gdrom_error_t linux_identify_drive( int fd, unsigned char *buf, int buflen );
54 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
55 int mode, unsigned char *buf, uint32_t *length );
56 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, uint32_t *buflen,
58 static int linux_drive_status( gdrom_disc_t disc );
60 struct gdrom_image_class cdrom_device_class = { "Linux", NULL,
61 linux_image_is_valid, linux_open_device };
62 GList *cdrom_get_native_devices(void)
68 while( (ent = getfsent()) != NULL ) {
69 if( (stat(ent->fs_spec, &st) != -1) &&
70 S_ISBLK(st.st_mode) ) {
71 /* Got a valid block device - is it a CDROM? */
72 int fd = open(ent->fs_spec, O_RDONLY|O_NONBLOCK);
75 int caps = ioctl(fd, CDROM_GET_CAPABILITY);
77 /* Appears to support CDROM functions */
79 linux_identify_drive( fd, buf, sizeof(buf) );
80 list = g_list_append( list, gdrom_device_new(ent->fs_spec, buf));
88 gdrom_disc_t cdrom_open_device( const gchar *method, const gchar *path )
93 static gboolean linux_image_is_valid( FILE *f )
96 struct cdrom_tochdr tochdr;
98 if( fstat(fileno(f), &st) == -1 ) {
99 return FALSE; /* can't stat device? */
101 if( !S_ISBLK(st.st_mode) ) {
102 return FALSE; /* Not a block device */
105 int caps = ioctl(fileno(f), CDROM_GET_CAPABILITY);
107 /* Quick check that this is really a CD device */
114 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f )
118 disc = gdrom_image_new(filename, f);
120 ERROR("Unable to allocate memory!");
124 int status = ioctl(fileno(f), CDROM_DRIVE_STATUS, CDSL_CURRENT);
125 if( status == CDS_DISC_OK ) {
126 status = linux_read_disc_toc( (gdrom_image_t)disc );
128 gdrom_image_destroy_no_close(disc);
129 if( status == 0xFFFF ) {
130 ERROR("Unable to load disc table of contents (%s)", strerror(errno));
132 ERROR("Unable to load disc table of contents (sense %d,%d)",
133 status &0xFF, status >> 8 );
138 ((gdrom_image_t)disc)->disc_type = IDE_DISC_NONE;
140 disc->read_sector = linux_read_sector;
141 disc->drive_status = linux_drive_status;
145 static int linux_drive_status( gdrom_disc_t disc )
147 int fd = fileno(((gdrom_image_t)disc)->file);
148 int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
149 if( status == CDS_DISC_OK ) {
150 status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
152 linux_read_disc_toc( (gdrom_image_t)disc);
154 return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
156 return IDE_DISC_NONE;
160 * Read the full table of contents into the disc from the device.
162 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
164 int fd = fileno(disc->file);
165 unsigned char buf[MAXTOCSIZE];
166 uint32_t buflen = sizeof(buf);
167 char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
169 cmd[7] = (sizeof(buf))>>8;
170 cmd[8] = (sizeof(buf))&0xFF;
171 memset( buf, 0, sizeof(buf) );
172 gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
176 mmc_parse_toc2( disc, buf );
180 gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
182 int fd = fileno( ((gdrom_image_t)disc)->file );
183 uint32_t real_sector = lba - CD_MSF_OFFSET;
184 uint32_t length = endlba - lba;
186 char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
187 cmd[2] = (real_sector >> 24) & 0xFF;
188 cmd[3] = (real_sector >> 16) & 0xFF;
189 cmd[4] = (real_sector >> 8) & 0xFF;
190 cmd[5] = real_sector & 0xFF;
191 cmd[6] = (length >> 24) & 0xFF;
192 cmd[7] = (length >> 16) & 0xFF;
193 cmd[8] = (length >> 8) & 0xFF;
194 cmd[9] = length & 0xFF;
196 return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
199 gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
201 int fd = fileno( ((gdrom_image_t)disc)->file );
203 char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
204 return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
207 static unsigned char *trim( unsigned char *src )
209 unsigned char *p = src + strlen(src)-1;
210 while( isspace(*src) )
212 while( p >= src && isspace(*p) )
216 static gdrom_error_t linux_identify_drive( int fd, unsigned char *buf, int buflen )
218 unsigned char ident[256];
219 uint32_t identlen = 256;
220 char cmd[12] = {0x12,0,0,0, 0xFF,0,0,0, 0,0,0,0};
221 gdrom_error_t status =
222 linux_send_command( fd, cmd, ident, &identlen, CGC_DATA_READ );
227 memcpy( vendorid, ident+8, 8 ); vendorid[8] = 0;
228 memcpy( productid, ident+16, 16 ); productid[16] = 0;
229 memcpy( productrev, ident+32, 4 ); productrev[4] = 0;
231 snprintf( buf, buflen, "%.8s %.16s %.4s", trim(vendorid),
232 trim(productid), trim(productrev) );
237 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
238 int mode, unsigned char *buf, uint32_t *length )
240 gdrom_image_t image = (gdrom_image_t)disc;
241 int fd = fileno(image->file);
242 uint32_t real_sector = sector - CD_MSF_OFFSET;
243 uint32_t sector_size = MAX_SECTOR_SIZE;
246 mmc_make_read_cd_cmd( cmd, real_sector, mode );
248 gdrom_error_t status = linux_send_command( fd, cmd, buf, §or_size, CGC_DATA_READ );
257 * Send a packet command to the device and wait for a response.
258 * @return 0 on success, -1 on an operating system error, or a sense error
259 * code on a device error.
261 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, uint32_t *buflen,
264 struct request_sense sense;
265 struct cdrom_generic_command cgc;
267 memset( &cgc, 0, sizeof(cgc) );
268 memset( &sense, 0, sizeof(sense) );
269 memcpy( cgc.cmd, cmd, 12 );
271 cgc.buflen = *buflen;
273 cgc.data_direction = direction;
275 if( ioctl(fd, CDROM_SEND_PACKET, &cgc) < 0 ) {
276 if( sense.sense_key == 0 ) {
279 /* TODO: Map newer codes back to the ones used by the gd-rom. */
280 return sense.sense_key | (sense.asc<<8);
283 *buflen = cgc.buflen;
.