4 * SCSI/MMC device interface (depends on lower-level SCSI transport)
6 * Copyright (c) 2009 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.
21 #include <glib/gstrfuncs.h>
24 #include "drivers/cdrom/cdimpl.h"
26 #define MAXTOCENTRIES 600 /* This is a fairly generous overestimate really */
27 #define MAXTOCSIZE 4 + (MAXTOCENTRIES*11)
28 #define MAX_SECTORS_PER_CALL 1
31 * Parse the TOC (format 2) into the cdrom_disc structure
33 void mmc_parse_toc2( cdrom_disc_t disc, unsigned char *buf )
39 int len = (buf[0] << 8) | buf[1];
40 int session_type = -1;
42 for( i = 4; i<len; i+=11 ) {
44 int adr = buf[i+1] >> 4;
46 if( adr == 0x01 && point > 0 && point < 100 ) {
48 int trackno = point-1;
49 if( point > max_track ) {
52 if( session > max_session ) {
53 max_session = session;
55 disc->track[trackno].trackno = point;
56 disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
57 disc->track[trackno].sessionno = session;
58 disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
60 } else switch( (adr << 8) | point ) {
61 case 0x1A0: /* session info */
62 if( buf[i+9] == 0x20 ) {
63 session_type = CDROM_DISC_XA;
65 session_type = CDROM_DISC_NONXA;
67 disc->disc_type = session_type;
69 case 0x1A2: /* leadout */
70 disc->leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
74 disc->track_count = max_track;
75 disc->session_count = max_session;
79 const char *mmc_parse_inquiry( unsigned char *buf )
84 memcpy( vendorid, buf+8, 8 ); vendorid[8] = 0;
85 memcpy( productid, buf+16, 16 ); productid[16] = 0;
86 memcpy( productrev, buf+32, 4 ); productrev[4] = 0;
87 return g_strdup_printf( "%.8s %.16s %.4s", g_strstrip(vendorid),
88 g_strstrip(productid), g_strstrip(productrev) );
92 * Construct a drive indentification string based on the response to the
93 * INQUIRY command. On success, returns the disc identification as a newly
94 * allocated string, otherwise NULL.
96 const char *cdrom_disc_scsi_identify_drive( cdrom_disc_t disc )
98 unsigned char ident[256];
99 uint32_t identlen = 256;
100 char cmd[12] = {0x12,0,0,0, 0xFF,0,0,0, 0,0,0,0};
101 cdrom_error_t status = SCSI_TRANSPORT(disc)->packet_read( disc, cmd, ident, &identlen );
102 if( status == CDROM_ERROR_OK ) {
103 return mmc_parse_inquiry(ident);
109 static cdrom_error_t cdrom_disc_scsi_read_sectors( sector_source_t source, cdrom_lba_t lba,
110 cdrom_count_t count, cdrom_read_mode_t mode, unsigned char *buf, size_t *length )
112 assert( IS_SECTOR_SOURCE_TYPE(source,DISC_SECTOR_SOURCE) );
113 cdrom_disc_t disc = (cdrom_disc_t)source;
114 uint32_t sector_size = CDROM_MAX_SECTOR_SIZE;
118 cmd[1] = CDROM_READ_TYPE(mode);
119 cmd[2] = (lba >> 24) & 0xFF;
120 cmd[3] = (lba >> 16) & 0xFF;
121 cmd[4] = (lba >> 8) & 0xFF;
123 cmd[6] = (count>>16) & 0xFF;
124 cmd[7] = (count>>8) & 0xFF;
125 cmd[8] = count & 0xFF;
126 cmd[9] = CDROM_READ_FIELDS(mode)>>8;
130 cdrom_error_t status = SCSI_TRANSPORT(disc)->packet_read( disc, cmd, buf, §or_size );
140 * Read the full table of contents into the disc from the device.
142 gboolean cdrom_disc_scsi_read_toc( cdrom_disc_t disc, ERROR *err )
144 unsigned char buf[MAXTOCSIZE];
145 uint32_t buflen = sizeof(buf);
146 char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
148 cmd[7] = (sizeof(buf))>>8;
149 cmd[8] = (sizeof(buf))&0xFF;
150 memset( buf, 0, sizeof(buf) );
151 cdrom_error_t status = SCSI_TRANSPORT(disc)->packet_read(disc, cmd, buf, &buflen );
152 if( status == CDROM_ERROR_OK ) {
153 mmc_parse_toc2( disc, buf );
156 if( (status & 0xFF) != 0x02 ) {
157 /* Sense key 2 == Not Ready (ie temporary failure). Just ignore and
158 * consider the drive empty for now, but warn about any other errors
160 WARN( _("Unable to read disc table of contents (error %04x)"), status );
162 cdrom_disc_clear_toc(disc);
167 static gboolean cdrom_disc_scsi_check_media( cdrom_disc_t disc )
169 if( SCSI_TRANSPORT(disc)->media_changed(disc) ) {
170 cdrom_disc_scsi_read_toc(disc, NULL);
177 static cdrom_error_t cdrom_disc_scsi_play_audio( cdrom_disc_t disc, cdrom_lba_t lba, cdrom_count_t length )
179 char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
180 cmd[2] = (lba >> 24) & 0xFF;
181 cmd[3] = (lba >> 16) & 0xFF;
182 cmd[4] = (lba >> 8) & 0xFF;
184 cmd[6] = (length >> 24) & 0xFF;
185 cmd[7] = (length >> 16) & 0xFF;
186 cmd[8] = (length >> 8) & 0xFF;
187 cmd[9] = length & 0xFF;
189 return SCSI_TRANSPORT(disc)->packet_cmd( disc, cmd );
193 static cdrom_error_t cdrom_disc_scsi_stop_audio( cdrom_disc_t disc )
196 char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
198 return SCSI_TRANSPORT(disc)->packet_cmd( disc, cmd );
201 void cdrom_disc_scsi_init( cdrom_disc_t disc, cdrom_scsi_transport_t scsi )
203 disc->impl_data = scsi;
204 disc->source.read_sectors = cdrom_disc_scsi_read_sectors;
205 disc->read_toc = cdrom_disc_scsi_read_toc;
206 disc->check_media = cdrom_disc_scsi_check_media;
207 disc->play_audio = cdrom_disc_scsi_play_audio;
208 disc->stop_audio = cdrom_disc_scsi_stop_audio;
211 cdrom_disc_t cdrom_disc_scsi_new( const char *filename, cdrom_scsi_transport_t scsi, ERROR *err )
213 cdrom_disc_t disc = cdrom_disc_new(filename, err);
216 cdrom_disc_scsi_init( disc, scsi );
217 cdrom_disc_read_toc(disc, err);
222 cdrom_disc_t cdrom_disc_scsi_new_file( FILE *f, const char *filename, cdrom_scsi_transport_t scsi, ERROR *err )
224 cdrom_disc_t disc = cdrom_disc_new(filename, err);
227 disc->base_source = file_sector_source_new( f, SECTOR_UNKNOWN, 0, 0, TRUE );
228 if( disc->base_source != NULL ) {
229 sector_source_ref( disc->base_source );
230 cdrom_disc_scsi_init( disc, scsi );
231 cdrom_disc_read_toc(disc, err);
233 cdrom_disc_unref(disc);
.