Search
lxdream.org :: lxdream/src/drivers/cd_linux.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cd_linux.c
changeset 806:6ef1ce4a9dbc
prev788:eb238a77f3c0
next1023:264e2fd90be8
author nkeynes
date Thu Jan 15 04:15:11 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add support for the Intel ICC compiler (C only, icc doesn't support Obj-C)
- Rename Obj-C source to .m
- Separate paths.c into paths_unix.c and paths_osx.m
- Add configuration detection of ICC, along with specific opt flags
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 <ctype.h>
    24 #include <linux/cdrom.h>
    25 #include <sys/stat.h>
    26 #include <sys/ioctl.h>
    27 #include <fstab.h>
    28 #include <fcntl.h>
    30 #include "gdrom/gddriver.h"
    31 #include "gdrom/packet.h"
    32 #include "dream.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;
    45     return addr.lba;
    46 }
    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,
    58                                          int direction );
    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)
    64 {
    65     GList *list = NULL;
    66     struct fstab *ent;
    67     struct stat st;
    68     setfsent();
    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);
    74             if( fd == -1 )
    75                 continue;
    76             int caps = ioctl(fd, CDROM_GET_CAPABILITY);
    77             if( caps != -1 ) {
    78                 /* Appears to support CDROM functions */
    79                 char buf[32];
    80                 linux_identify_drive( fd, buf, sizeof(buf) );
    81                 list = g_list_append( list, gdrom_device_new(ent->fs_spec, buf));
    82             }
    83             close(fd);
    84         }
    85     }
    86     return list;
    87 }
    89 gdrom_disc_t cdrom_open_device( const gchar *method, const gchar *path )
    90 {
    91     return NULL;
    92 }
    94 static gboolean linux_image_is_valid( FILE *f )
    95 {
    96     struct stat st;
    97     struct cdrom_tochdr tochdr;
    99     if( fstat(fileno(f), &st) == -1 ) {
   100         return FALSE; /* can't stat device? */
   101     }
   102     if( !S_ISBLK(st.st_mode) ) {
   103         return FALSE; /* Not a block device */
   104     }
   106     int caps = ioctl(fileno(f), CDROM_GET_CAPABILITY);
   107     if( caps == -1 ) {
   108         /* Quick check that this is really a CD device */
   109         return FALSE;
   110     }
   112     return TRUE;
   113 }
   115 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
   116 {
   117     gdrom_disc_t disc;
   119     disc = gdrom_image_new(filename, f);
   120     if( disc == NULL ) {
   121         ERROR("Unable to allocate memory!");
   122         return NULL;
   123     }
   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 );
   128         if( status != 0 ) {
   129             gdrom_image_destroy_no_close(disc);
   130             if( status == 0xFFFF ) {
   131                 ERROR("Unable to load disc table of contents (%s)", strerror(errno));
   132             } else {
   133                 ERROR("Unable to load disc table of contents (sense %d,%d)",
   134                     status &0xFF, status >> 8 );
   135             }
   136             return NULL;
   137         }
   138     } else {
   139         ((gdrom_image_t)disc)->disc_type = IDE_DISC_NONE;
   140     }
   141     disc->read_sector = linux_read_sector;
   142     disc->drive_status = linux_drive_status;
   143     return disc;
   144 }
   146 static int linux_drive_status( gdrom_disc_t disc )
   147 {
   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);
   152         if( status != 0 ) {
   153             linux_read_disc_toc( (gdrom_image_t)disc);
   154         }
   155         return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
   156     } else {
   157         return IDE_DISC_NONE;
   158     }
   159 }
   160 /**
   161  * Read the full table of contents into the disc from the device.
   162  */
   163 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
   164 {
   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 );
   174     if( status != 0 ) {
   175         return status;
   176     }
   177     mmc_parse_toc2( disc, buf );
   178     return 0;
   179 }
   181 gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
   182 {
   183     int fd = fileno( ((gdrom_image_t)disc)->file );
   184     uint32_t real_sector = lba - CD_MSF_OFFSET;
   185     uint32_t length = endlba - lba;
   186     uint32_t buflen = 0;
   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 );
   198 }
   200 gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
   201 {
   202     int fd = fileno( ((gdrom_image_t)disc)->file );
   203     uint32_t buflen = 0;
   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 );
   206 }
   208 static char *trim( char *src )
   209 {
   210     char *p = src + strlen(src)-1;
   211     while( isspace(*src) ) 
   212         src++;
   213     while( p >= src && isspace(*p) )
   214         *p-- = '\0';
   215     return src;
   216 }
   217 static gdrom_error_t linux_identify_drive( int fd, char *buf, int buflen )
   218 {
   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 );
   224     if( status == 0 ) {
   225         char vendorid[9];
   226         char productid[17];
   227         char productrev[5];
   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) );
   234     }
   235     return status;
   236 }
   238 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
   239                                         int mode, unsigned char *buf, uint32_t *length )
   240 {
   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;
   245     char cmd[12];
   247     mmc_make_read_cd_cmd( cmd, real_sector, mode );
   249     gdrom_error_t status = linux_send_command( fd, cmd, buf, &sector_size, CGC_DATA_READ );
   250     if( status != 0 ) {
   251         return status;
   252     }
   253     *length = 2048;
   254     return 0;
   255 }
   257 /**
   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.
   261  */
   262 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, uint32_t *buflen,
   263                                          int direction )
   264 {
   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 );
   271     cgc.buffer = buffer;
   272     cgc.buflen = *buflen;
   273     cgc.sense = &sense;
   274     cgc.data_direction = direction;
   276     if( ioctl(fd, CDROM_SEND_PACKET, &cgc) < 0 ) {
   277         if( sense.sense_key == 0 ) {
   278             return -1; 
   279         } else {
   280             /* TODO: Map newer codes back to the ones used by the gd-rom. */
   281             return sense.sense_key | (sense.asc<<8);
   282         }
   283     } else {
   284         *buflen = cgc.buflen;
   285         return 0;
   286     }
   287 }
.