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.
24 #include <linux/cdrom.h>
26 #include <sys/ioctl.h>
30 #include "gdrom/gddriver.h"
31 #include "gdrom/packet.h"
34 #define MAXTOCENTRIES 600 /* This is a fairly generous overestimate really */
35 #define MAXTOCSIZE 4 + (MAXTOCENTRIES*11)
36 #define MAX_SECTORS_PER_CALL 1
38 static uint32_t inline lbatomsf( uint32_t lba ) {
39 union cdrom_addr addr;
40 lba = lba + CD_MSF_OFFSET;
41 addr.msf.frame = lba % CD_FRAMES;
42 int seconds = lba / CD_FRAMES;
43 addr.msf.second = seconds % CD_SECS;
44 addr.msf.minute = seconds / CD_SECS;
48 #define LBATOMSF( lba ) lbatomsf(lba)
51 static gboolean linux_image_is_valid( FILE *f );
52 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
53 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc );
54 static gdrom_error_t linux_identify_drive( int fd, char *buf, int buflen );
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, uint32_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 */
80 linux_identify_drive( fd, buf, sizeof(buf) );
81 list = g_list_append( list, gdrom_device_new(ent->fs_spec, buf));
89 gdrom_disc_t cdrom_open_device( const gchar *method, const gchar *path )
94 static gboolean linux_image_is_valid( FILE *f )
97 struct cdrom_tochdr tochdr;
99 if( fstat(fileno(f), &st) == -1 ) {
100 return FALSE; /* can't stat device? */
102 if( !S_ISBLK(st.st_mode) ) {
103 return FALSE; /* Not a block device */
106 int caps = ioctl(fileno(f), CDROM_GET_CAPABILITY);
108 /* Quick check that this is really a CD device */
115 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f )
119 disc = gdrom_image_new(filename, f);
121 ERROR("Unable to allocate memory!");
125 int status = ioctl(fileno(f), CDROM_DRIVE_STATUS, CDSL_CURRENT);
126 if( status == CDS_DISC_OK ) {
127 status = linux_read_disc_toc( (gdrom_image_t)disc );
129 gdrom_image_destroy_no_close(disc);
130 if( status == 0xFFFF ) {
131 ERROR("Unable to load disc table of contents (%s)", strerror(errno));
133 ERROR("Unable to load disc table of contents (sense %d,%d)",
134 status &0xFF, status >> 8 );
139 ((gdrom_image_t)disc)->disc_type = IDE_DISC_NONE;
141 disc->read_sector = linux_read_sector;
142 disc->drive_status = linux_drive_status;
146 static int linux_drive_status( gdrom_disc_t disc )
148 int fd = fileno(((gdrom_image_t)disc)->file);
149 int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
150 if( status == CDS_DISC_OK ) {
151 status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
153 linux_read_disc_toc( (gdrom_image_t)disc);
155 return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
157 return IDE_DISC_NONE;
161 * Read the full table of contents into the disc from the device.
163 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
165 int fd = fileno(disc->file);
166 unsigned char buf[MAXTOCSIZE];
167 uint32_t buflen = sizeof(buf);
168 char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
170 cmd[7] = (sizeof(buf))>>8;
171 cmd[8] = (sizeof(buf))&0xFF;
172 memset( buf, 0, sizeof(buf) );
173 gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
177 mmc_parse_toc2( disc, buf );
181 gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
183 int fd = fileno( ((gdrom_image_t)disc)->file );
184 uint32_t real_sector = lba - CD_MSF_OFFSET;
185 uint32_t length = endlba - lba;
187 char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
188 cmd[2] = (real_sector >> 24) & 0xFF;
189 cmd[3] = (real_sector >> 16) & 0xFF;
190 cmd[4] = (real_sector >> 8) & 0xFF;
191 cmd[5] = real_sector & 0xFF;
192 cmd[6] = (length >> 24) & 0xFF;
193 cmd[7] = (length >> 16) & 0xFF;
194 cmd[8] = (length >> 8) & 0xFF;
195 cmd[9] = length & 0xFF;
197 return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
200 gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
202 int fd = fileno( ((gdrom_image_t)disc)->file );
204 char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
205 return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
208 static char *trim( char *src )
210 char *p = src + strlen(src)-1;
211 while( isspace(*src) )
213 while( p >= src && isspace(*p) )
217 static gdrom_error_t linux_identify_drive( int fd, char *buf, int buflen )
219 unsigned char ident[256];
220 uint32_t identlen = 256;
221 char cmd[12] = {0x12,0,0,0, 0xFF,0,0,0, 0,0,0,0};
222 gdrom_error_t status =
223 linux_send_command( fd, cmd, ident, &identlen, CGC_DATA_READ );
228 memcpy( vendorid, ident+8, 8 ); vendorid[8] = 0;
229 memcpy( productid, ident+16, 16 ); productid[16] = 0;
230 memcpy( productrev, ident+32, 4 ); productrev[4] = 0;
232 snprintf( buf, buflen, "%.8s %.16s %.4s", trim(vendorid),
233 trim(productid), trim(productrev) );
238 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
239 int mode, unsigned char *buf, uint32_t *length )
241 gdrom_image_t image = (gdrom_image_t)disc;
242 int fd = fileno(image->file);
243 uint32_t real_sector = sector - CD_MSF_OFFSET;
244 uint32_t sector_size = MAX_SECTOR_SIZE;
247 mmc_make_read_cd_cmd( cmd, real_sector, mode );
249 gdrom_error_t status = linux_send_command( fd, cmd, buf, §or_size, CGC_DATA_READ );
258 * Send a packet command to the device and wait for a response.
259 * @return 0 on success, -1 on an operating system error, or a sense error
260 * code on a device error.
262 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, uint32_t *buflen,
265 struct request_sense sense;
266 struct cdrom_generic_command cgc;
268 memset( &cgc, 0, sizeof(cgc) );
269 memset( &sense, 0, sizeof(sense) );
270 memcpy( cgc.cmd, cmd, 12 );
272 cgc.buflen = *buflen;
274 cgc.data_direction = direction;
276 if( ioctl(fd, CDROM_SEND_PACKET, &cgc) < 0 ) {
277 if( sense.sense_key == 0 ) {
280 /* TODO: Map newer codes back to the ones used by the gd-rom. */
281 return sense.sense_key | (sense.asc<<8);
284 *buflen = cgc.buflen;
.