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 736:a02d1475ccfd
prev720:b5594d1ac80a
next767:53dc977f8f25
author nkeynes
date Mon Jul 21 01:01:39 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Fix batch of -Wall warnings
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     if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
   103         /* Quick check that this is really a CD */
   104         return FALSE;
   105     }
   107     return TRUE;
   108 }
   110 static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
   111 {
   112     gdrom_disc_t disc;
   114     disc = gdrom_image_new(filename, f);
   115     if( disc == NULL ) {
   116         ERROR("Unable to allocate memory!");
   117         return NULL;
   118     }
   120     gdrom_error_t status = linux_read_disc_toc( (gdrom_image_t)disc );
   121     if( status != 0 ) {
   122         gdrom_image_destroy_no_close(disc);
   123         if( status == 0xFFFF ) {
   124             ERROR("Unable to load disc table of contents (%s)", strerror(errno));
   125         } else {
   126             ERROR("Unable to load disc table of contents (sense %d,%d)",
   127                     status &0xFF, status >> 8 );
   128         }
   129         return NULL;
   130     }
   131     disc->read_sector = linux_read_sector;
   132     disc->drive_status = linux_drive_status;
   133     return disc;
   134 }
   136 static int linux_drive_status( gdrom_disc_t disc )
   137 {
   138     int fd = fileno(((gdrom_image_t)disc)->file);
   139     int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
   140     if( status == CDS_DISC_OK ) {
   141         status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
   142         if( status != 0 ) {
   143             linux_read_disc_toc( (gdrom_image_t)disc);
   144         }
   145         return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
   146     } else {
   147         return IDE_DISC_NONE;
   148     }
   149 }
   150 /**
   151  * Read the full table of contents into the disc from the device.
   152  */
   153 static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
   154 {
   155     int fd = fileno(disc->file);
   156     unsigned char buf[MAXTOCSIZE];
   157     size_t buflen = sizeof(buf);
   158     char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   160     cmd[7] = (sizeof(buf))>>8;
   161     cmd[8] = (sizeof(buf))&0xFF;
   162     memset( buf, 0, sizeof(buf) );
   163     gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
   164     if( status != 0 ) {
   165         return status;
   166     }
   167     mmc_parse_toc2( disc, buf );
   168     return 0;
   169 }
   171 gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
   172 {
   173     int fd = fileno( ((gdrom_image_t)disc)->file );
   174     uint32_t real_sector = lba - CD_MSF_OFFSET;
   175     uint32_t length = endlba - lba;
   176     uint32_t buflen = 0;
   177     char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
   178     cmd[2] = (real_sector >> 24) & 0xFF;
   179     cmd[3] = (real_sector >> 16) & 0xFF;
   180     cmd[4] = (real_sector >> 8) & 0xFF;
   181     cmd[5] = real_sector & 0xFF;
   182     cmd[6] = (length >> 24) & 0xFF;
   183     cmd[7] = (length >> 16) & 0xFF;
   184     cmd[8] = (length >> 8) & 0xFF;
   185     cmd[9] = length & 0xFF;
   187     return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
   188 }
   190 gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
   191 {
   192     int fd = fileno( ((gdrom_image_t)disc)->file );
   193     uint32_t buflen = 0;
   194     char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
   195     return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
   196 }
   198 static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
   199                                         int mode, unsigned char *buf, uint32_t *length )
   200 {
   201     gdrom_image_t image = (gdrom_image_t)disc;
   202     int fd = fileno(image->file);
   203     uint32_t real_sector = sector - CD_MSF_OFFSET;
   204     uint32_t sector_size = MAX_SECTOR_SIZE;
   205     char cmd[12];
   207     mmc_make_read_cd_cmd( cmd, real_sector, mode );
   209     gdrom_error_t status = linux_send_command( fd, cmd, buf, &sector_size, CGC_DATA_READ );
   210     if( status != 0 ) {
   211         return status;
   212     }
   213     *length = 2048;
   214     return 0;
   215 }
   217 /**
   218  * Send a packet command to the device and wait for a response. 
   219  * @return 0 on success, -1 on an operating system error, or a sense error
   220  * code on a device error.
   221  */
   222 static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
   223                                          int direction )
   224 {
   225     struct request_sense sense;
   226     struct cdrom_generic_command cgc;
   228     memset( &cgc, 0, sizeof(cgc) );
   229     memset( &sense, 0, sizeof(sense) );
   230     memcpy( cgc.cmd, cmd, 12 );
   231     cgc.buffer = buffer;
   232     cgc.buflen = *buflen;
   233     cgc.sense = &sense;
   234     cgc.data_direction = direction;
   236     if( ioctl(fd, CDROM_SEND_PACKET, &cgc) < 0 ) {
   237         if( sense.sense_key == 0 ) {
   238             return -1; 
   239         } else {
   240             /* TODO: Map newer codes back to the ones used by the gd-rom. */
   241             return sense.sense_key | (sense.asc<<8);
   242         }
   243     } else {
   244         *buflen = cgc.buflen;
   245         return 0;
   246     }
   247 }
.