Search
lxdream.org :: lxdream/src/gdrom/linux.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/linux.c
changeset 353:b8569afb53fc
prev342:850502f0e8de
next422:61a0598e07ff
author nkeynes
date Sun Feb 11 10:11:05 2007 +0000 (15 years ago)
permissions -rw-r--r--
last change Bug 19: Read real drive status, detect media change to reload toc
file annotate diff log raw
nkeynes@237
     1
/**
nkeynes@353
     2
 * $Id: linux.c,v 1.4 2007-02-11 10:11:05 nkeynes Exp $
nkeynes@237
     3
 *
nkeynes@342
     4
 * Linux cd-rom device driver. 
nkeynes@237
     5
 *
nkeynes@237
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@237
     7
 *
nkeynes@237
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@237
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@237
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@237
    11
 * (at your option) any later version.
nkeynes@237
    12
 *
nkeynes@237
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@237
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@237
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@237
    16
 * GNU General Public License for more details.
nkeynes@237
    17
 */
nkeynes@237
    18
#include <stdlib.h>
nkeynes@237
    19
#include <stdio.h>
nkeynes@237
    20
#include <string.h>
nkeynes@237
    21
#include <errno.h>
nkeynes@237
    22
#include <linux/cdrom.h>
nkeynes@237
    23
#include <sys/stat.h>
nkeynes@237
    24
#include <sys/ioctl.h>
nkeynes@237
    25
nkeynes@237
    26
#include "gdrom/gdrom.h"
nkeynes@237
    27
#include "gdrom/packet.h"
nkeynes@237
    28
#include "dream.h"
nkeynes@237
    29
nkeynes@237
    30
#define MAXTOCENTRIES 600  /* This is a fairly generous overestimate really */
nkeynes@237
    31
#define MAXTOCSIZE 4 + (MAXTOCENTRIES*11)
nkeynes@342
    32
#define MAX_SECTORS_PER_CALL 1
nkeynes@237
    33
nkeynes@237
    34
#define MSFTOLBA( m,s,f ) (f + (s*CD_FRAMES) + (m*CD_FRAMES*CD_SECS))
nkeynes@237
    35
nkeynes@237
    36
static uint32_t inline lbatomsf( uint32_t lba ) {
nkeynes@237
    37
    union cdrom_addr addr;
nkeynes@237
    38
    lba = lba + CD_MSF_OFFSET;
nkeynes@237
    39
    addr.msf.frame = lba % CD_FRAMES;
nkeynes@237
    40
    int seconds = lba / CD_FRAMES;
nkeynes@237
    41
    addr.msf.second = seconds % CD_SECS;
nkeynes@237
    42
    addr.msf.minute = seconds / CD_SECS;
nkeynes@237
    43
    return addr.lba;
nkeynes@237
    44
}
nkeynes@237
    45
nkeynes@237
    46
#define LBATOMSF( lba ) lbatomsf(lba)
nkeynes@237
    47
nkeynes@237
    48
nkeynes@237
    49
static gboolean linux_image_is_valid( FILE *f );
nkeynes@237
    50
static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
nkeynes@342
    51
static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc );
nkeynes@342
    52
static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
nkeynes@342
    53
					int mode, char *buf, uint32_t *length );
nkeynes@342
    54
static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t *buflen,
nkeynes@237
    55
					 int direction );
nkeynes@353
    56
static int linux_drive_status( gdrom_disc_t disc );
nkeynes@237
    57
nkeynes@237
    58
struct gdrom_image_class linux_device_class = { "Linux", NULL,
nkeynes@237
    59
					     linux_image_is_valid, linux_open_device };
nkeynes@237
    60
nkeynes@237
    61
static gboolean linux_image_is_valid( FILE *f )
nkeynes@237
    62
{
nkeynes@237
    63
    struct stat st;
nkeynes@237
    64
    struct cdrom_tochdr tochdr;
nkeynes@237
    65
nkeynes@237
    66
    if( fstat(fileno(f), &st) == -1 ) {
nkeynes@237
    67
	return FALSE; /* can't stat device? */
nkeynes@237
    68
    }
nkeynes@237
    69
    if( !S_ISBLK(st.st_mode) ) {
nkeynes@237
    70
	return FALSE; /* Not a block device */
nkeynes@237
    71
    }
nkeynes@237
    72
nkeynes@237
    73
    if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
nkeynes@237
    74
	/* Quick check that this is really a CD */
nkeynes@237
    75
	return FALSE;
nkeynes@237
    76
    }
nkeynes@237
    77
nkeynes@237
    78
    return TRUE;
nkeynes@237
    79
}
nkeynes@237
    80
nkeynes@237
    81
static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
nkeynes@237
    82
{
nkeynes@237
    83
    gdrom_disc_t disc;
nkeynes@237
    84
    int fd = fileno(f);
nkeynes@237
    85
nkeynes@237
    86
    disc = gdrom_image_new(f);
nkeynes@237
    87
    if( disc == NULL ) {
nkeynes@237
    88
	ERROR("Unable to allocate memory!");
nkeynes@237
    89
	return NULL;
nkeynes@237
    90
    }
nkeynes@237
    91
nkeynes@342
    92
    gdrom_error_t status = linux_read_disc_toc( (gdrom_image_t)disc );
nkeynes@237
    93
    if( status != 0 ) {
nkeynes@342
    94
	disc->close(disc);
nkeynes@237
    95
	if( status == 0xFFFF ) {
nkeynes@237
    96
	    ERROR("Unable to load disc table of contents (%s)", strerror(errno));
nkeynes@237
    97
	} else {
nkeynes@237
    98
	    ERROR("Unable to load disc table of contents (sense %d,%d)",
nkeynes@237
    99
		  status &0xFF, status >> 8 );
nkeynes@237
   100
	}
nkeynes@237
   101
	return NULL;
nkeynes@237
   102
    }
nkeynes@342
   103
    disc->read_sector = linux_read_sector;
nkeynes@353
   104
    disc->drive_status = linux_drive_status;
nkeynes@342
   105
    ((gdrom_image_t)disc)->disc_type = IDE_DISC_CDROM;
nkeynes@237
   106
    return disc;
nkeynes@237
   107
}
nkeynes@237
   108
nkeynes@353
   109
static int linux_drive_status( gdrom_disc_t disc )
nkeynes@353
   110
{
nkeynes@353
   111
    int fd = fileno(((gdrom_image_t)disc)->file);
nkeynes@353
   112
    int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
nkeynes@353
   113
    if( status == CDS_DISC_OK ) {
nkeynes@353
   114
	status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
nkeynes@353
   115
	if( status != 0 ) {
nkeynes@353
   116
	    linux_read_disc_toc(disc);
nkeynes@353
   117
	}
nkeynes@353
   118
	return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
nkeynes@353
   119
    } else {
nkeynes@353
   120
	return IDE_DISC_NONE;
nkeynes@353
   121
    }
nkeynes@353
   122
}
nkeynes@237
   123
/**
nkeynes@237
   124
 * Read the full table of contents into the disc from the device.
nkeynes@237
   125
 */
nkeynes@342
   126
static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
nkeynes@237
   127
{
nkeynes@237
   128
    int fd = fileno(disc->file);
nkeynes@237
   129
    unsigned char buf[MAXTOCSIZE];
nkeynes@342
   130
    int buflen = sizeof(buf);
nkeynes@237
   131
    char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nkeynes@237
   132
    
nkeynes@237
   133
    cmd[7] = (sizeof(buf))>>8;
nkeynes@237
   134
    cmd[8] = (sizeof(buf))&0xFF;
nkeynes@237
   135
    memset( buf, 0, sizeof(buf) );
nkeynes@342
   136
    gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
nkeynes@237
   137
    if( status != 0 ) {
nkeynes@237
   138
	return status;
nkeynes@237
   139
    }
nkeynes@237
   140
nkeynes@237
   141
    int max_track = 0;
nkeynes@237
   142
    int last_track = -1;
nkeynes@237
   143
    int leadout = -1;
nkeynes@237
   144
    int len = (buf[0] << 8) | buf[1];
nkeynes@237
   145
    int session_type = GDROM_MODE1;
nkeynes@237
   146
    int i;
nkeynes@237
   147
    for( i = 4; i<len; i+=11 ) {
nkeynes@237
   148
	int session = buf[i];
nkeynes@237
   149
	int adr = buf[i+1] >> 4;
nkeynes@237
   150
	int point = buf[i+3];
nkeynes@237
   151
	if( adr == 0x01 && point > 0 && point < 100 ) {
nkeynes@237
   152
	    /* Track info */
nkeynes@237
   153
	    int trackno = point-1;
nkeynes@237
   154
	    if( point > max_track ) {
nkeynes@237
   155
		max_track = point;
nkeynes@237
   156
	    }
nkeynes@237
   157
	    disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
nkeynes@237
   158
	    disc->track[trackno].session = session - 1;
nkeynes@237
   159
	    disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
nkeynes@237
   160
	    if( disc->track[trackno].flags & TRACK_DATA ) {
nkeynes@237
   161
		disc->track[trackno].mode = GDROM_MODE1;
nkeynes@237
   162
	    } else {
nkeynes@237
   163
		disc->track[trackno].mode = GDROM_CDDA;
nkeynes@237
   164
	    }
nkeynes@237
   165
	    if( last_track != -1 ) {
nkeynes@237
   166
		disc->track[last_track].sector_count = disc->track[trackno].lba -
nkeynes@237
   167
		    disc->track[last_track].lba;
nkeynes@237
   168
	    }
nkeynes@237
   169
	    last_track = trackno;
nkeynes@237
   170
	} else switch( (adr << 8) | point ) {
nkeynes@237
   171
	case 0x1A0: /* session info */
nkeynes@237
   172
	    if( buf[i+9] == 0x20 ) {
nkeynes@237
   173
		session_type = GDROM_MODE2;
nkeynes@237
   174
	    } else {
nkeynes@237
   175
		session_type = GDROM_MODE1;
nkeynes@237
   176
	    }
nkeynes@237
   177
	case 0x1A2: /* leadout */
nkeynes@237
   178
	    leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
nkeynes@237
   179
	    break;
nkeynes@237
   180
	}
nkeynes@237
   181
    }
nkeynes@237
   182
    disc->track_count = max_track;
nkeynes@237
   183
nkeynes@237
   184
    if( leadout != -1 && last_track != -1 ) {
nkeynes@237
   185
	disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
nkeynes@237
   186
    }
nkeynes@237
   187
    return 0;
nkeynes@237
   188
}
nkeynes@237
   189
nkeynes@342
   190
static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
nkeynes@342
   191
					int mode, char *buf, uint32_t *length )
nkeynes@237
   192
{
nkeynes@342
   193
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   194
    int fd = fileno(image->file);
nkeynes@237
   195
    uint32_t real_sector = sector - CD_MSF_OFFSET;
nkeynes@342
   196
    uint32_t sector_size = MAX_SECTOR_SIZE;
nkeynes@259
   197
    int i;
nkeynes@237
   198
    char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nkeynes@259
   199
nkeynes@342
   200
    cmd[1] = (mode & 0x0E) << 1;
nkeynes@342
   201
    cmd[2] = (real_sector >> 24) & 0xFF;
nkeynes@342
   202
    cmd[3] = (real_sector >> 16) & 0xFF;
nkeynes@342
   203
    cmd[4] = (real_sector >> 8) & 0xFF;
nkeynes@342
   204
    cmd[5] = real_sector & 0xFF;
nkeynes@342
   205
    cmd[6] = 0;
nkeynes@342
   206
    cmd[7] = 0;
nkeynes@342
   207
    cmd[8] = 1;
nkeynes@342
   208
    cmd[9] = 0x10;
nkeynes@237
   209
    
nkeynes@342
   210
    gdrom_error_t status = linux_send_command( fd, cmd, buf, &sector_size, CGC_DATA_READ );
nkeynes@342
   211
    if( status != 0 ) {
nkeynes@342
   212
	return status;
nkeynes@237
   213
    }
nkeynes@342
   214
    *length = 2048;
nkeynes@259
   215
    return 0;
nkeynes@237
   216
}
nkeynes@237
   217
nkeynes@237
   218
/**
nkeynes@237
   219
 * Send a packet command to the device and wait for a response. 
nkeynes@237
   220
 * @return 0 on success, -1 on an operating system error, or a sense error
nkeynes@237
   221
 * code on a device error.
nkeynes@237
   222
 */
nkeynes@342
   223
static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t *buflen,
nkeynes@237
   224
					 int direction )
nkeynes@237
   225
{
nkeynes@237
   226
    struct request_sense sense;
nkeynes@237
   227
    struct cdrom_generic_command cgc;
nkeynes@237
   228
nkeynes@237
   229
    memset( &cgc, 0, sizeof(cgc) );
nkeynes@237
   230
    memset( &sense, 0, sizeof(sense) );
nkeynes@237
   231
    memcpy( cgc.cmd, cmd, 12 );
nkeynes@237
   232
    cgc.buffer = buffer;
nkeynes@342
   233
    cgc.buflen = *buflen;
nkeynes@237
   234
    cgc.sense = &sense;
nkeynes@237
   235
    cgc.data_direction = direction;
nkeynes@237
   236
    
nkeynes@237
   237
    if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
nkeynes@237
   238
	if( sense.sense_key == 0 ) {
nkeynes@237
   239
	    return -1; 
nkeynes@237
   240
	} else {
nkeynes@342
   241
	    /* TODO: Map newer codes back to the ones used by the gd-rom. */
nkeynes@237
   242
	    return sense.sense_key | (sense.asc<<8);
nkeynes@237
   243
	}
nkeynes@237
   244
    } else {
nkeynes@342
   245
	*buflen = cgc.buflen;
nkeynes@237
   246
	return 0;
nkeynes@237
   247
    }
nkeynes@237
   248
}
.