filename | src/drivers/cd_linux.c |
changeset | 767:53dc977f8f25 |
prev | 736:a02d1475ccfd |
next | 777:60431d09605c |
author | nkeynes |
date | Mon Jul 28 04:45:24 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Fix translations - need to include lxdream.h ahead of gettext.h (always the little things...) Install as .mo in the bundle |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
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 <unistd.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <linux/cdrom.h>
24 #include <sys/stat.h>
25 #include <sys/ioctl.h>
26 #include <fstab.h>
27 #include <fcntl.h>
29 #include "gdrom/gddriver.h"
30 #include "gdrom/packet.h"
31 #include "dream.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;
44 return addr.lba;
45 }
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_read_sector( gdrom_disc_t disc, uint32_t sector,
54 int mode, unsigned char *buf, uint32_t *length );
55 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
56 int direction );
57 static int linux_drive_status( gdrom_disc_t disc );
59 struct gdrom_image_class cdrom_device_class = { "Linux", NULL,
60 linux_image_is_valid, linux_open_device };
61 GList *cdrom_get_native_devices(void)
62 {
63 GList *list = NULL;
64 struct fstab *ent;
65 struct stat st;
66 setfsent();
67 while( (ent = getfsent()) != NULL ) {
68 if( (stat(ent->fs_spec, &st) != -1) &&
69 S_ISBLK(st.st_mode) ) {
70 /* Got a valid block device - is it a CDROM? */
71 int fd = open(ent->fs_spec, O_RDONLY|O_NONBLOCK);
72 if( fd == -1 )
73 continue;
74 int caps = ioctl(fd, CDROM_GET_CAPABILITY);
75 if( caps != -1 ) {
76 /* Appears to support CDROM functions */
77 list = g_list_append( list, gdrom_device_new(ent->fs_spec, ent->fs_spec));
78 }
79 close(fd);
80 }
81 }
82 return list;
83 }
85 gdrom_disc_t cdrom_open_device( const gchar *method, const gchar *path )
86 {
87 return NULL;
88 }
90 static gboolean linux_image_is_valid( FILE *f )
91 {
92 struct stat st;
93 struct cdrom_tochdr tochdr;
95 if( fstat(fileno(f), &st) == -1 ) {
96 return FALSE; /* can't stat device? */
97 }
98 if( !S_ISBLK(st.st_mode) ) {
99 return FALSE; /* Not a block device */
100 }
102 int caps = ioctl(fileno(f), CDROM_GET_CAPABILITY);
103 if( caps == -1 ) {
104 /* Quick check that this is really a CD device */
105 return FALSE;
106 }
108 return TRUE;
109 }
111 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f )
112 {
113 gdrom_disc_t disc;
115 disc = gdrom_image_new(filename, f);
116 if( disc == NULL ) {
117 ERROR("Unable to allocate memory!");
118 return NULL;
119 }
121 int status = ioctl(fileno(f), CDROM_DRIVE_STATUS, CDSL_CURRENT);
122 if( status == CDS_DISC_OK ) {
123 status = linux_read_disc_toc( (gdrom_image_t)disc );
124 if( status != 0 ) {
125 gdrom_image_destroy_no_close(disc);
126 if( status == 0xFFFF ) {
127 ERROR("Unable to load disc table of contents (%s)", strerror(errno));
128 } else {
129 ERROR("Unable to load disc table of contents (sense %d,%d)",
130 status &0xFF, status >> 8 );
131 }
132 return NULL;
133 }
134 } else {
135 ((gdrom_image_t)disc)->disc_type = IDE_DISC_NONE;
136 }
137 disc->read_sector = linux_read_sector;
138 disc->drive_status = linux_drive_status;
139 return disc;
140 }
142 static int linux_drive_status( gdrom_disc_t disc )
143 {
144 int fd = fileno(((gdrom_image_t)disc)->file);
145 int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
146 if( status == CDS_DISC_OK ) {
147 status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
148 if( status != 0 ) {
149 linux_read_disc_toc( (gdrom_image_t)disc);
150 }
151 return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
152 } else {
153 return IDE_DISC_NONE;
154 }
155 }
156 /**
157 * Read the full table of contents into the disc from the device.
158 */
159 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
160 {
161 int fd = fileno(disc->file);
162 unsigned char buf[MAXTOCSIZE];
163 size_t buflen = sizeof(buf);
164 char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
166 cmd[7] = (sizeof(buf))>>8;
167 cmd[8] = (sizeof(buf))&0xFF;
168 memset( buf, 0, sizeof(buf) );
169 gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
170 if( status != 0 ) {
171 return status;
172 }
173 mmc_parse_toc2( disc, buf );
174 return 0;
175 }
177 gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
178 {
179 int fd = fileno( ((gdrom_image_t)disc)->file );
180 uint32_t real_sector = lba - CD_MSF_OFFSET;
181 uint32_t length = endlba - lba;
182 uint32_t buflen = 0;
183 char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
184 cmd[2] = (real_sector >> 24) & 0xFF;
185 cmd[3] = (real_sector >> 16) & 0xFF;
186 cmd[4] = (real_sector >> 8) & 0xFF;
187 cmd[5] = real_sector & 0xFF;
188 cmd[6] = (length >> 24) & 0xFF;
189 cmd[7] = (length >> 16) & 0xFF;
190 cmd[8] = (length >> 8) & 0xFF;
191 cmd[9] = length & 0xFF;
193 return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
194 }
196 gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
197 {
198 int fd = fileno( ((gdrom_image_t)disc)->file );
199 uint32_t buflen = 0;
200 char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
201 return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
202 }
204 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
205 int mode, unsigned char *buf, uint32_t *length )
206 {
207 gdrom_image_t image = (gdrom_image_t)disc;
208 int fd = fileno(image->file);
209 uint32_t real_sector = sector - CD_MSF_OFFSET;
210 uint32_t sector_size = MAX_SECTOR_SIZE;
211 char cmd[12];
213 mmc_make_read_cd_cmd( cmd, real_sector, mode );
215 gdrom_error_t status = linux_send_command( fd, cmd, buf, §or_size, CGC_DATA_READ );
216 if( status != 0 ) {
217 return status;
218 }
219 *length = 2048;
220 return 0;
221 }
223 /**
224 * Send a packet command to the device and wait for a response.
225 * @return 0 on success, -1 on an operating system error, or a sense error
226 * code on a device error.
227 */
228 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
229 int direction )
230 {
231 struct request_sense sense;
232 struct cdrom_generic_command cgc;
234 memset( &cgc, 0, sizeof(cgc) );
235 memset( &sense, 0, sizeof(sense) );
236 memcpy( cgc.cmd, cmd, 12 );
237 cgc.buffer = buffer;
238 cgc.buflen = *buflen;
239 cgc.sense = &sense;
240 cgc.data_direction = direction;
242 if( ioctl(fd, CDROM_SEND_PACKET, &cgc) < 0 ) {
243 if( sense.sense_key == 0 ) {
244 return -1;
245 } else {
246 /* TODO: Map newer codes back to the ones used by the gd-rom. */
247 return sense.sense_key | (sense.asc<<8);
248 }
249 } else {
250 *buflen = cgc.buflen;
251 return 0;
252 }
253 }
.