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 767:53dc977f8f25
prev736:a02d1475ccfd
next777:60431d09605c
author nkeynes
date Thu Jul 24 02:31:56 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix to correctly mount empty CD drives
file annotate diff log raw
nkeynes@520
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@520
     3
 *
nkeynes@520
     4
 * Linux cd-rom device driver. 
nkeynes@520
     5
 *
nkeynes@520
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@520
     7
 *
nkeynes@520
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@520
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@520
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@520
    11
 * (at your option) any later version.
nkeynes@520
    12
 *
nkeynes@520
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@520
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@520
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@520
    16
 * GNU General Public License for more details.
nkeynes@520
    17
 */
nkeynes@520
    18
#include <unistd.h>
nkeynes@520
    19
#include <stdlib.h>
nkeynes@520
    20
#include <stdio.h>
nkeynes@520
    21
#include <string.h>
nkeynes@520
    22
#include <errno.h>
nkeynes@520
    23
#include <linux/cdrom.h>
nkeynes@520
    24
#include <sys/stat.h>
nkeynes@520
    25
#include <sys/ioctl.h>
nkeynes@520
    26
#include <fstab.h>
nkeynes@520
    27
#include <fcntl.h>
nkeynes@520
    28
nkeynes@678
    29
#include "gdrom/gddriver.h"
nkeynes@520
    30
#include "gdrom/packet.h"
nkeynes@520
    31
#include "dream.h"
nkeynes@520
    32
nkeynes@520
    33
#define MAXTOCENTRIES 600  /* This is a fairly generous overestimate really */
nkeynes@520
    34
#define MAXTOCSIZE 4 + (MAXTOCENTRIES*11)
nkeynes@520
    35
#define MAX_SECTORS_PER_CALL 1
nkeynes@520
    36
nkeynes@520
    37
static uint32_t inline lbatomsf( uint32_t lba ) {
nkeynes@520
    38
    union cdrom_addr addr;
nkeynes@520
    39
    lba = lba + CD_MSF_OFFSET;
nkeynes@520
    40
    addr.msf.frame = lba % CD_FRAMES;
nkeynes@520
    41
    int seconds = lba / CD_FRAMES;
nkeynes@520
    42
    addr.msf.second = seconds % CD_SECS;
nkeynes@520
    43
    addr.msf.minute = seconds / CD_SECS;
nkeynes@520
    44
    return addr.lba;
nkeynes@520
    45
}
nkeynes@520
    46
nkeynes@520
    47
#define LBATOMSF( lba ) lbatomsf(lba)
nkeynes@520
    48
nkeynes@520
    49
nkeynes@520
    50
static gboolean linux_image_is_valid( FILE *f );
nkeynes@520
    51
static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
nkeynes@520
    52
static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc );
nkeynes@520
    53
static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
nkeynes@736
    54
                                        int mode, unsigned char *buf, uint32_t *length );
nkeynes@520
    55
static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
nkeynes@736
    56
                                         int direction );
nkeynes@520
    57
static int linux_drive_status( gdrom_disc_t disc );
nkeynes@520
    58
nkeynes@520
    59
struct gdrom_image_class cdrom_device_class = { "Linux", NULL,
nkeynes@736
    60
        linux_image_is_valid, linux_open_device };
nkeynes@709
    61
GList *cdrom_get_native_devices(void)
nkeynes@520
    62
{
nkeynes@520
    63
    GList *list = NULL;
nkeynes@520
    64
    struct fstab *ent;
nkeynes@520
    65
    struct stat st;
nkeynes@520
    66
    setfsent();
nkeynes@520
    67
    while( (ent = getfsent()) != NULL ) {
nkeynes@736
    68
        if( (stat(ent->fs_spec, &st) != -1) && 
nkeynes@736
    69
                S_ISBLK(st.st_mode) ) {
nkeynes@736
    70
            /* Got a valid block device - is it a CDROM? */
nkeynes@736
    71
            int fd = open(ent->fs_spec, O_RDONLY|O_NONBLOCK);
nkeynes@736
    72
            if( fd == -1 )
nkeynes@736
    73
                continue;
nkeynes@736
    74
            int caps = ioctl(fd, CDROM_GET_CAPABILITY);
nkeynes@736
    75
            if( caps != -1 ) {
nkeynes@736
    76
                /* Appears to support CDROM functions */
nkeynes@736
    77
                list = g_list_append( list, gdrom_device_new(ent->fs_spec, ent->fs_spec));
nkeynes@736
    78
            }
nkeynes@736
    79
            close(fd);
nkeynes@736
    80
        }
nkeynes@520
    81
    }
nkeynes@520
    82
    return list;
nkeynes@520
    83
}
nkeynes@520
    84
nkeynes@709
    85
gdrom_disc_t cdrom_open_device( const gchar *method, const gchar *path )
nkeynes@709
    86
{
nkeynes@709
    87
    return NULL;
nkeynes@709
    88
}
nkeynes@709
    89
nkeynes@520
    90
static gboolean linux_image_is_valid( FILE *f )
nkeynes@520
    91
{
nkeynes@520
    92
    struct stat st;
nkeynes@520
    93
    struct cdrom_tochdr tochdr;
nkeynes@520
    94
nkeynes@520
    95
    if( fstat(fileno(f), &st) == -1 ) {
nkeynes@736
    96
        return FALSE; /* can't stat device? */
nkeynes@520
    97
    }
nkeynes@520
    98
    if( !S_ISBLK(st.st_mode) ) {
nkeynes@736
    99
        return FALSE; /* Not a block device */
nkeynes@520
   100
    }
nkeynes@520
   101
nkeynes@767
   102
    int caps = ioctl(fileno(f), CDROM_GET_CAPABILITY);
nkeynes@767
   103
    if( caps == -1 ) {
nkeynes@767
   104
        /* Quick check that this is really a CD device */
nkeynes@736
   105
        return FALSE;
nkeynes@520
   106
    }
nkeynes@520
   107
nkeynes@520
   108
    return TRUE;
nkeynes@520
   109
}
nkeynes@520
   110
nkeynes@520
   111
static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
nkeynes@520
   112
{
nkeynes@520
   113
    gdrom_disc_t disc;
nkeynes@520
   114
nkeynes@520
   115
    disc = gdrom_image_new(filename, f);
nkeynes@520
   116
    if( disc == NULL ) {
nkeynes@736
   117
        ERROR("Unable to allocate memory!");
nkeynes@736
   118
        return NULL;
nkeynes@520
   119
    }
nkeynes@520
   120
nkeynes@767
   121
    int status = ioctl(fileno(f), CDROM_DRIVE_STATUS, CDSL_CURRENT);
nkeynes@767
   122
    if( status == CDS_DISC_OK ) {
nkeynes@767
   123
        status = linux_read_disc_toc( (gdrom_image_t)disc );
nkeynes@767
   124
        if( status != 0 ) {
nkeynes@767
   125
            gdrom_image_destroy_no_close(disc);
nkeynes@767
   126
            if( status == 0xFFFF ) {
nkeynes@767
   127
                ERROR("Unable to load disc table of contents (%s)", strerror(errno));
nkeynes@767
   128
            } else {
nkeynes@767
   129
                ERROR("Unable to load disc table of contents (sense %d,%d)",
nkeynes@736
   130
                    status &0xFF, status >> 8 );
nkeynes@767
   131
            }
nkeynes@767
   132
            return NULL;
nkeynes@736
   133
        }
nkeynes@767
   134
    } else {
nkeynes@767
   135
        ((gdrom_image_t)disc)->disc_type = IDE_DISC_NONE;
nkeynes@520
   136
    }
nkeynes@520
   137
    disc->read_sector = linux_read_sector;
nkeynes@520
   138
    disc->drive_status = linux_drive_status;
nkeynes@520
   139
    return disc;
nkeynes@520
   140
}
nkeynes@520
   141
nkeynes@520
   142
static int linux_drive_status( gdrom_disc_t disc )
nkeynes@520
   143
{
nkeynes@520
   144
    int fd = fileno(((gdrom_image_t)disc)->file);
nkeynes@520
   145
    int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
nkeynes@520
   146
    if( status == CDS_DISC_OK ) {
nkeynes@736
   147
        status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
nkeynes@736
   148
        if( status != 0 ) {
nkeynes@736
   149
            linux_read_disc_toc( (gdrom_image_t)disc);
nkeynes@736
   150
        }
nkeynes@736
   151
        return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
nkeynes@520
   152
    } else {
nkeynes@736
   153
        return IDE_DISC_NONE;
nkeynes@520
   154
    }
nkeynes@520
   155
}
nkeynes@520
   156
/**
nkeynes@520
   157
 * Read the full table of contents into the disc from the device.
nkeynes@520
   158
 */
nkeynes@520
   159
static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
nkeynes@520
   160
{
nkeynes@520
   161
    int fd = fileno(disc->file);
nkeynes@520
   162
    unsigned char buf[MAXTOCSIZE];
nkeynes@520
   163
    size_t buflen = sizeof(buf);
nkeynes@520
   164
    char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nkeynes@736
   165
nkeynes@520
   166
    cmd[7] = (sizeof(buf))>>8;
nkeynes@520
   167
    cmd[8] = (sizeof(buf))&0xFF;
nkeynes@520
   168
    memset( buf, 0, sizeof(buf) );
nkeynes@520
   169
    gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
nkeynes@520
   170
    if( status != 0 ) {
nkeynes@720
   171
        return status;
nkeynes@520
   172
    }
nkeynes@720
   173
    mmc_parse_toc2( disc, buf );
nkeynes@520
   174
    return 0;
nkeynes@520
   175
}
nkeynes@520
   176
nkeynes@736
   177
gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
nkeynes@520
   178
{
nkeynes@520
   179
    int fd = fileno( ((gdrom_image_t)disc)->file );
nkeynes@520
   180
    uint32_t real_sector = lba - CD_MSF_OFFSET;
nkeynes@520
   181
    uint32_t length = endlba - lba;
nkeynes@520
   182
    uint32_t buflen = 0;
nkeynes@520
   183
    char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
nkeynes@520
   184
    cmd[2] = (real_sector >> 24) & 0xFF;
nkeynes@520
   185
    cmd[3] = (real_sector >> 16) & 0xFF;
nkeynes@520
   186
    cmd[4] = (real_sector >> 8) & 0xFF;
nkeynes@520
   187
    cmd[5] = real_sector & 0xFF;
nkeynes@520
   188
    cmd[6] = (length >> 24) & 0xFF;
nkeynes@520
   189
    cmd[7] = (length >> 16) & 0xFF;
nkeynes@520
   190
    cmd[8] = (length >> 8) & 0xFF;
nkeynes@520
   191
    cmd[9] = length & 0xFF;
nkeynes@736
   192
nkeynes@520
   193
    return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
nkeynes@520
   194
}
nkeynes@520
   195
nkeynes@520
   196
gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
nkeynes@520
   197
{
nkeynes@520
   198
    int fd = fileno( ((gdrom_image_t)disc)->file );
nkeynes@520
   199
    uint32_t buflen = 0;
nkeynes@520
   200
    char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
nkeynes@520
   201
    return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
nkeynes@520
   202
}
nkeynes@520
   203
nkeynes@520
   204
static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
nkeynes@736
   205
                                        int mode, unsigned char *buf, uint32_t *length )
nkeynes@520
   206
{
nkeynes@520
   207
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@520
   208
    int fd = fileno(image->file);
nkeynes@520
   209
    uint32_t real_sector = sector - CD_MSF_OFFSET;
nkeynes@520
   210
    uint32_t sector_size = MAX_SECTOR_SIZE;
nkeynes@720
   211
    char cmd[12];
nkeynes@736
   212
nkeynes@720
   213
    mmc_make_read_cd_cmd( cmd, real_sector, mode );
nkeynes@736
   214
nkeynes@520
   215
    gdrom_error_t status = linux_send_command( fd, cmd, buf, &sector_size, CGC_DATA_READ );
nkeynes@520
   216
    if( status != 0 ) {
nkeynes@720
   217
        return status;
nkeynes@520
   218
    }
nkeynes@520
   219
    *length = 2048;
nkeynes@520
   220
    return 0;
nkeynes@520
   221
}
nkeynes@520
   222
nkeynes@520
   223
/**
nkeynes@520
   224
 * Send a packet command to the device and wait for a response. 
nkeynes@520
   225
 * @return 0 on success, -1 on an operating system error, or a sense error
nkeynes@520
   226
 * code on a device error.
nkeynes@520
   227
 */
nkeynes@520
   228
static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
nkeynes@736
   229
                                         int direction )
nkeynes@520
   230
{
nkeynes@520
   231
    struct request_sense sense;
nkeynes@520
   232
    struct cdrom_generic_command cgc;
nkeynes@520
   233
nkeynes@520
   234
    memset( &cgc, 0, sizeof(cgc) );
nkeynes@520
   235
    memset( &sense, 0, sizeof(sense) );
nkeynes@520
   236
    memcpy( cgc.cmd, cmd, 12 );
nkeynes@520
   237
    cgc.buffer = buffer;
nkeynes@520
   238
    cgc.buflen = *buflen;
nkeynes@520
   239
    cgc.sense = &sense;
nkeynes@520
   240
    cgc.data_direction = direction;
nkeynes@736
   241
nkeynes@644
   242
    if( ioctl(fd, CDROM_SEND_PACKET, &cgc) < 0 ) {
nkeynes@736
   243
        if( sense.sense_key == 0 ) {
nkeynes@736
   244
            return -1; 
nkeynes@736
   245
        } else {
nkeynes@736
   246
            /* TODO: Map newer codes back to the ones used by the gd-rom. */
nkeynes@736
   247
            return sense.sense_key | (sense.asc<<8);
nkeynes@736
   248
        }
nkeynes@520
   249
    } else {
nkeynes@736
   250
        *buflen = cgc.buflen;
nkeynes@736
   251
        return 0;
nkeynes@520
   252
    }
nkeynes@520
   253
}
.