filename | src/gdrom/linux.c |
changeset | 237:6f1a429c9d12 |
next | 259:7c6881790cc2 |
author | nkeynes |
date | Wed Dec 20 11:24:16 2006 +0000 (17 years ago) |
permissions | -rw-r--r-- |
last change | Tidy up the core IDE test case - now passes (usually) on the real thing. Main surprise was that it seems to always fail the first packet command after a reset with code 06,29 (drive reset), no matter how long its been since the reset. |
view | annotate | diff | log | raw |
1 /**
2 * $Id: linux.c,v 1.1 2006-12-14 12:31:38 nkeynes Exp $
3 *
4 * Linux cd-rom device driver
5 *
6 * Copyright (c) 2005 Nathan Keynes.
7 *
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.
12 *
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.
17 */
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <linux/cdrom.h>
23 #include <sys/stat.h>
24 #include <sys/ioctl.h>
26 #include "gdrom/gdrom.h"
27 #include "gdrom/packet.h"
28 #include "dream.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;
42 return addr.lba;
43 }
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,
53 uint32_t *length );
54 static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t buflen,
55 int direction );
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 )
62 {
63 struct stat st;
64 struct cdrom_tochdr tochdr;
66 if( fstat(fileno(f), &st) == -1 ) {
67 return FALSE; /* can't stat device? */
68 }
69 if( !S_ISBLK(st.st_mode) ) {
70 return FALSE; /* Not a block device */
71 }
73 if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
74 /* Quick check that this is really a CD */
75 return FALSE;
76 }
78 return TRUE;
79 }
81 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f )
82 {
83 gdrom_disc_t disc;
84 int fd = fileno(f);
86 disc = gdrom_image_new(f);
87 if( disc == NULL ) {
88 ERROR("Unable to allocate memory!");
89 return NULL;
90 }
92 gdrom_error_t status = linux_read_disc_toc( disc );
93 if( status != 0 ) {
94 free(disc);
95 if( status == 0xFFFF ) {
96 ERROR("Unable to load disc table of contents (%s)", strerror(errno));
97 } else {
98 ERROR("Unable to load disc table of contents (sense %d,%d)",
99 status &0xFF, status >> 8 );
100 }
101 return NULL;
102 }
103 disc->read_sectors = linux_read_sectors;
104 disc->disc_type = IDE_DISC_CDROM;
105 return disc;
106 }
108 /**
109 * Read the full table of contents into the disc from the device.
110 */
111 static gdrom_error_t linux_read_disc_toc( gdrom_disc_t disc )
112 {
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 );
121 if( status != 0 ) {
122 return status;
123 }
125 int max_track = 0;
126 int last_track = -1;
127 int leadout = -1;
128 int len = (buf[0] << 8) | buf[1];
129 int session_type = GDROM_MODE1;
130 int i;
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 ) {
136 /* Track info */
137 int trackno = point-1;
138 if( point > max_track ) {
139 max_track = point;
140 }
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;
146 } else {
147 disc->track[trackno].mode = GDROM_CDDA;
148 }
149 if( last_track != -1 ) {
150 disc->track[last_track].sector_count = disc->track[trackno].lba -
151 disc->track[last_track].lba;
152 }
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;
158 } else {
159 session_type = GDROM_MODE1;
160 }
161 case 0x1A2: /* leadout */
162 leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
163 break;
164 }
165 }
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;
170 }
171 return 0;
172 }
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,
176 uint32_t *length )
177 {
178 int fd = fileno(disc->file);
179 int call;
180 struct cdrom_read read;
181 read.cdread_lba = LBATOMSF(sector);
182 read.cdread_bufaddr = buf;
183 switch(mode) {
184 case GDROM_MODE1:
185 call = CDROMREADMODE1;
186 read.cdread_buflen = sector_count * 2048;
187 break;
188 case GDROM_MODE2:
189 call = CDROMREADMODE2;
190 read.cdread_buflen = sector_count * 2336;
191 break;
192 default:
193 return PKT_ERR_BADREADMODE;
194 }
196 if( ioctl( fd, call, &read ) == -1 ) {
197 ERROR( "Error reading disc (%s)", strerror(errno) );
198 return PKT_ERR_BADREAD;
199 }
200 *length = read.cdread_buflen;
201 return PKT_ERR_OK;
202 }
204 static gdrom_error_t linux_read_sectors( gdrom_disc_t disc, uint32_t sector,
205 uint32_t sector_count, int mode, char *buf,
206 uint32_t *length )
207 {
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;
219 cmd[9] = 0x10;
221 gdrom_error_t status = linux_send_command( fd, cmd, buf, buflen, CGC_DATA_READ );
222 if( status == 0 ) {
223 *length = buflen;
224 }
225 return status;
226 }
228 /**
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.
232 */
233 static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t buflen,
234 int direction )
235 {
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 );
242 cgc.buffer = buffer;
243 cgc.buflen = buflen;
244 cgc.sense = &sense;
245 cgc.data_direction = direction;
247 if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
248 if( sense.sense_key == 0 ) {
249 return -1;
250 } else {
251 return sense.sense_key | (sense.asc<<8);
252 }
253 } else {
254 return 0;
255 }
256 }
.