Search
lxdream.org :: lxdream/src/drivers/cdrom/cd_mmc.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/cd_mmc.c
changeset 1298:d0eb2307b847
prev1296:30ecee61f811
author nkeynes
date Wed Feb 04 08:38:23 2015 +1000 (5 years ago)
permissions -rw-r--r--
last change Fix assorted compile warnings reported by Clang
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * SCSI/MMC device interface (depends on lower-level SCSI transport)
     5  *
     6  * Copyright (c) 2009 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  */
    19 #include <assert.h>
    20 #include <string.h>
    21 #include <glib.h>
    22 #include "lxdream.h"
    23 #include "gettext.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
    30 /**
    31  * Parse the TOC (format 2) into the cdrom_disc structure
    32  */
    33 void mmc_parse_toc2( cdrom_disc_t disc, unsigned char *buf )
    34 {
    35     int max_track = 0;
    36     int max_session = 0;
    37     int last_track = -1;
    38     int len = (buf[0] << 8) | buf[1];
    39     int session_type = -1;
    40     int i;
    41     for( i = 4; i<len; i+=11 ) {
    42         int session = buf[i];
    43         int adr = buf[i+1] >> 4;
    44         int point = buf[i+3];
    45         if( adr == 0x01 && point > 0 && point < 100 ) {
    46             /* Track info */
    47             int trackno = point-1;
    48             if( point > max_track ) {
    49                 max_track = point;
    50             }
    51             if( session > max_session ) {
    52                 max_session = session;
    53             }
    54             disc->track[trackno].trackno = point;
    55             disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
    56             disc->track[trackno].sessionno = session;
    57             disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
    58             last_track = trackno;
    59         } else switch( (adr << 8) | point ) {
    60         case 0x1A0: /* session info */
    61             if( buf[i+9] == 0x20 ) {
    62                 session_type = CDROM_DISC_XA;
    63             } else {
    64                 session_type = CDROM_DISC_NONXA;
    65             }
    66             disc->disc_type = session_type;
    67             break;
    68         case 0x1A2: /* leadout */
    69             disc->leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
    70             break;
    71         }
    72     }
    73     disc->track_count = max_track;
    74     disc->session_count = max_session;
    75 }
    78 const char *mmc_parse_inquiry( unsigned char *buf )
    79 {
    80     char vendorid[9];
    81     char productid[17];
    82     char productrev[5];
    83     memcpy( vendorid, buf+8, 8 ); vendorid[8] = 0;
    84     memcpy( productid, buf+16, 16 ); productid[16] = 0;
    85     memcpy( productrev, buf+32, 4 ); productrev[4] = 0;
    86     return g_strdup_printf( "%.8s %.16s %.4s", g_strstrip(vendorid),
    87               g_strstrip(productid), g_strstrip(productrev) );
    88 }
    90 /**
    91  * Construct a drive indentification string based on the response to the
    92  * INQUIRY command. On success, returns the disc identification as a newly
    93  * allocated string, otherwise NULL.
    94  */
    95 const char *cdrom_disc_scsi_identify_drive( cdrom_disc_t disc )
    96 {
    97     unsigned char ident[256];
    98     uint32_t identlen = 256;
    99     char cmd[12] = {0x12,0,0,0, 0xFF,0,0,0, 0,0,0,0};
   100     cdrom_error_t status = SCSI_TRANSPORT(disc)->packet_read( disc, cmd, ident, &identlen );
   101     if( status == CDROM_ERROR_OK ) {
   102         return mmc_parse_inquiry(ident);
   103     }
   104     return NULL;
   105 }
   108 static cdrom_error_t cdrom_disc_scsi_read_sectors( sector_source_t source, cdrom_lba_t lba,
   109         cdrom_count_t count, cdrom_read_mode_t mode, unsigned char *buf, size_t *length )
   110 {
   111     assert( IS_SECTOR_SOURCE_TYPE(source,DISC_SECTOR_SOURCE) );
   112     cdrom_disc_t disc = (cdrom_disc_t)source;
   113     uint32_t sector_size = CDROM_MAX_SECTOR_SIZE;
   114     char cmd[12];
   116     cmd[0] = 0xBE;
   117     cmd[1] = CDROM_READ_TYPE(mode);
   118     cmd[2] = (lba >> 24) & 0xFF;
   119     cmd[3] = (lba >> 16) & 0xFF;
   120     cmd[4] = (lba >> 8) & 0xFF;
   121     cmd[5] = lba & 0xFF;
   122     cmd[6] = (count>>16) & 0xFF;
   123     cmd[7] = (count>>8) & 0xFF;
   124     cmd[8] = count & 0xFF;
   125     cmd[9] = CDROM_READ_FIELDS(mode)>>8;
   126     cmd[10]= 0;
   127     cmd[11]= 0;
   129     cdrom_error_t status = SCSI_TRANSPORT(disc)->packet_read( disc, cmd, buf, &sector_size );
   130     if( status != 0 ) {
   131         return status;
   132     }
   133     /* FIXME */
   134     *length = 2048;
   135     return 0;
   136 }
   138 /**
   139  * Read the full table of contents into the disc from the device.
   140  */
   141 gboolean cdrom_disc_scsi_read_toc( cdrom_disc_t disc, ERROR *err )
   142 {
   143     unsigned char buf[MAXTOCSIZE];
   144     uint32_t buflen = sizeof(buf);
   145     char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   147     cmd[7] = (sizeof(buf))>>8;
   148     cmd[8] = (sizeof(buf))&0xFF;
   149     memset( buf, 0, sizeof(buf) );
   150     cdrom_error_t status = SCSI_TRANSPORT(disc)->packet_read(disc, cmd, buf, &buflen );
   151     if( status == CDROM_ERROR_OK ) {
   152 	    mmc_parse_toc2( disc, buf );
   153 	    return TRUE;
   154     } else {
   155     	if( (status & 0xFF) != 0x02 ) {
   156     	    /* Sense key 2 == Not Ready (ie temporary failure). Just ignore and
   157     	     * consider the drive empty for now, but warn about any other errors
   158     	     * we get. */
   159     	    WARN( _("Unable to read disc table of contents (error %04x)"), status );
   160     	}
   161     	cdrom_disc_clear_toc(disc);
   162     	return FALSE;
   163     }
   164 }
   166 static gboolean cdrom_disc_scsi_check_media( cdrom_disc_t disc )
   167 {
   168 	if( SCSI_TRANSPORT(disc)->media_changed(disc) ) {
   169 		cdrom_disc_scsi_read_toc(disc, NULL);
   170 		return TRUE;
   171 	} else {
   172 		return FALSE;
   173 	}
   174 }
   176 static cdrom_error_t cdrom_disc_scsi_play_audio( cdrom_disc_t disc, cdrom_lba_t lba, cdrom_count_t length )
   177 {
   178     char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
   179     cmd[2] = (lba >> 24) & 0xFF;
   180     cmd[3] = (lba >> 16) & 0xFF;
   181     cmd[4] = (lba >> 8) & 0xFF;
   182     cmd[5] = lba & 0xFF;
   183     cmd[6] = (length >> 24) & 0xFF;
   184     cmd[7] = (length >> 16) & 0xFF;
   185     cmd[8] = (length >> 8) & 0xFF;
   186     cmd[9] = length & 0xFF;
   188     return SCSI_TRANSPORT(disc)->packet_cmd( disc, cmd );
   189 }
   192 static cdrom_error_t cdrom_disc_scsi_stop_audio( cdrom_disc_t disc )
   193 {
   194     char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
   196     return SCSI_TRANSPORT(disc)->packet_cmd( disc, cmd );
   197 }
   199 void cdrom_disc_scsi_init( cdrom_disc_t disc, cdrom_scsi_transport_t scsi )
   200 {
   201     disc->impl_data = scsi;
   202     disc->source.read_sectors = cdrom_disc_scsi_read_sectors;
   203     disc->read_toc = cdrom_disc_scsi_read_toc;
   204     disc->check_media = cdrom_disc_scsi_check_media;
   205     disc->play_audio = cdrom_disc_scsi_play_audio;
   206     disc->stop_audio = cdrom_disc_scsi_stop_audio;
   207 }
   209 cdrom_disc_t cdrom_disc_scsi_new( const char *filename, cdrom_scsi_transport_t scsi, ERROR *err )
   210 {
   211     cdrom_disc_t disc = cdrom_disc_new(filename, err);
   212     if( disc != NULL ) {
   213         /* Initialize */
   214         cdrom_disc_scsi_init( disc, scsi );
   215         cdrom_disc_read_toc(disc, err);
   216     }
   217     return disc;
   218 } 
   220 cdrom_disc_t cdrom_disc_scsi_new_file( FILE *f, const char *filename, cdrom_scsi_transport_t scsi, ERROR *err )
   221 {
   222     cdrom_disc_t disc = cdrom_disc_new(filename, err);
   223     if( disc != NULL ) {
   224         /* Initialize */
   225         disc->base_source = file_sector_source_new( f, SECTOR_UNKNOWN, 0, 0, TRUE );
   226         if( disc->base_source != NULL ) {
   227             sector_source_ref( disc->base_source );
   228             cdrom_disc_scsi_init( disc, scsi );
   229             cdrom_disc_read_toc(disc, err);
   230         } else {
   231             cdrom_disc_unref(disc);
   232             disc = NULL;
   233         }
   234     }
   235     return disc;
   236 }
.