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