Search
lxdream.org :: lxdream/src/gdrom/linux.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/linux.c
changeset 259:7c6881790cc2
prev237:6f1a429c9d12
next342:850502f0e8de
author nkeynes
date Fri Dec 29 00:24:43 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change Limit number of sectors read at once (linux driver seems to throw a hissy
fit if you request too many at a time)
file annotate diff log raw
nkeynes@237
     1
/**
nkeynes@259
     2
 * $Id: linux.c,v 1.2 2006-12-29 00:24:43 nkeynes Exp $
nkeynes@237
     3
 *
nkeynes@237
     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@259
    32
#define MAX_SECTORS_PER_CALL 32
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@237
    51
static gdrom_error_t linux_read_disc_toc( gdrom_disc_t disc );
nkeynes@237
    52
static gdrom_error_t linux_read_sectors( gdrom_disc_t disc, uint32_t sector,
nkeynes@237
    53
					 uint32_t sector_count, int mode, char *buf,
nkeynes@237
    54
					 uint32_t *length );
nkeynes@237
    55
static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t buflen,
nkeynes@237
    56
					 int direction );
nkeynes@237
    57
nkeynes@237
    58
nkeynes@237
    59
struct gdrom_image_class linux_device_class = { "Linux", NULL,
nkeynes@237
    60
					     linux_image_is_valid, linux_open_device };
nkeynes@237
    61
nkeynes@237
    62
static gboolean linux_image_is_valid( FILE *f )
nkeynes@237
    63
{
nkeynes@237
    64
    struct stat st;
nkeynes@237
    65
    struct cdrom_tochdr tochdr;
nkeynes@237
    66
nkeynes@237
    67
    if( fstat(fileno(f), &st) == -1 ) {
nkeynes@237
    68
	return FALSE; /* can't stat device? */
nkeynes@237
    69
    }
nkeynes@237
    70
    if( !S_ISBLK(st.st_mode) ) {
nkeynes@237
    71
	return FALSE; /* Not a block device */
nkeynes@237
    72
    }
nkeynes@237
    73
nkeynes@237
    74
    if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
nkeynes@237
    75
	/* Quick check that this is really a CD */
nkeynes@237
    76
	return FALSE;
nkeynes@237
    77
    }
nkeynes@237
    78
nkeynes@237
    79
    return TRUE;
nkeynes@237
    80
}
nkeynes@237
    81
nkeynes@237
    82
static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
nkeynes@237
    83
{
nkeynes@237
    84
    gdrom_disc_t disc;
nkeynes@237
    85
    int fd = fileno(f);
nkeynes@237
    86
nkeynes@237
    87
    disc = gdrom_image_new(f);
nkeynes@237
    88
    if( disc == NULL ) {
nkeynes@237
    89
	ERROR("Unable to allocate memory!");
nkeynes@237
    90
	return NULL;
nkeynes@237
    91
    }
nkeynes@237
    92
nkeynes@237
    93
    gdrom_error_t status = linux_read_disc_toc( disc );
nkeynes@237
    94
    if( status != 0 ) {
nkeynes@237
    95
	free(disc);
nkeynes@237
    96
	if( status == 0xFFFF ) {
nkeynes@237
    97
	    ERROR("Unable to load disc table of contents (%s)", strerror(errno));
nkeynes@237
    98
	} else {
nkeynes@237
    99
	    ERROR("Unable to load disc table of contents (sense %d,%d)",
nkeynes@237
   100
		  status &0xFF, status >> 8 );
nkeynes@237
   101
	}
nkeynes@237
   102
	return NULL;
nkeynes@237
   103
    }
nkeynes@237
   104
    disc->read_sectors = linux_read_sectors;
nkeynes@237
   105
    disc->disc_type = IDE_DISC_CDROM;
nkeynes@237
   106
    return disc;
nkeynes@237
   107
}
nkeynes@237
   108
nkeynes@237
   109
/**
nkeynes@237
   110
 * Read the full table of contents into the disc from the device.
nkeynes@237
   111
 */
nkeynes@237
   112
static gdrom_error_t linux_read_disc_toc( gdrom_disc_t disc )
nkeynes@237
   113
{
nkeynes@237
   114
    int fd = fileno(disc->file);
nkeynes@237
   115
    unsigned char buf[MAXTOCSIZE];
nkeynes@237
   116
    char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nkeynes@237
   117
    
nkeynes@237
   118
    cmd[7] = (sizeof(buf))>>8;
nkeynes@237
   119
    cmd[8] = (sizeof(buf))&0xFF;
nkeynes@237
   120
    memset( buf, 0, sizeof(buf) );
nkeynes@237
   121
    gdrom_error_t status = linux_send_command( fd, cmd, buf, sizeof(buf), CGC_DATA_READ );
nkeynes@237
   122
    if( status != 0 ) {
nkeynes@237
   123
	return status;
nkeynes@237
   124
    }
nkeynes@237
   125
nkeynes@237
   126
    int max_track = 0;
nkeynes@237
   127
    int last_track = -1;
nkeynes@237
   128
    int leadout = -1;
nkeynes@237
   129
    int len = (buf[0] << 8) | buf[1];
nkeynes@237
   130
    int session_type = GDROM_MODE1;
nkeynes@237
   131
    int i;
nkeynes@237
   132
    for( i = 4; i<len; i+=11 ) {
nkeynes@237
   133
	int session = buf[i];
nkeynes@237
   134
	int adr = buf[i+1] >> 4;
nkeynes@237
   135
	int point = buf[i+3];
nkeynes@237
   136
	if( adr == 0x01 && point > 0 && point < 100 ) {
nkeynes@237
   137
	    /* Track info */
nkeynes@237
   138
	    int trackno = point-1;
nkeynes@237
   139
	    if( point > max_track ) {
nkeynes@237
   140
		max_track = point;
nkeynes@237
   141
	    }
nkeynes@237
   142
	    disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
nkeynes@237
   143
	    disc->track[trackno].session = session - 1;
nkeynes@237
   144
	    disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
nkeynes@237
   145
	    if( disc->track[trackno].flags & TRACK_DATA ) {
nkeynes@237
   146
		disc->track[trackno].mode = GDROM_MODE1;
nkeynes@237
   147
	    } else {
nkeynes@237
   148
		disc->track[trackno].mode = GDROM_CDDA;
nkeynes@237
   149
	    }
nkeynes@237
   150
	    if( last_track != -1 ) {
nkeynes@237
   151
		disc->track[last_track].sector_count = disc->track[trackno].lba -
nkeynes@237
   152
		    disc->track[last_track].lba;
nkeynes@237
   153
	    }
nkeynes@237
   154
	    last_track = trackno;
nkeynes@237
   155
	} else switch( (adr << 8) | point ) {
nkeynes@237
   156
	case 0x1A0: /* session info */
nkeynes@237
   157
	    if( buf[i+9] == 0x20 ) {
nkeynes@237
   158
		session_type = GDROM_MODE2;
nkeynes@237
   159
	    } else {
nkeynes@237
   160
		session_type = GDROM_MODE1;
nkeynes@237
   161
	    }
nkeynes@237
   162
	case 0x1A2: /* leadout */
nkeynes@237
   163
	    leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
nkeynes@237
   164
	    break;
nkeynes@237
   165
	}
nkeynes@237
   166
    }
nkeynes@237
   167
    disc->track_count = max_track;
nkeynes@237
   168
nkeynes@237
   169
    if( leadout != -1 && last_track != -1 ) {
nkeynes@237
   170
	disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
nkeynes@237
   171
    }
nkeynes@237
   172
    return 0;
nkeynes@237
   173
}
nkeynes@237
   174
nkeynes@237
   175
static gdrom_error_t linux_read_sectors_ioctl( gdrom_disc_t disc, uint32_t sector,
nkeynes@237
   176
					 uint32_t sector_count, int mode, char *buf,
nkeynes@237
   177
					 uint32_t *length )
nkeynes@237
   178
{
nkeynes@237
   179
    int fd = fileno(disc->file);
nkeynes@237
   180
    int call;
nkeynes@237
   181
    struct cdrom_read read;
nkeynes@237
   182
    read.cdread_lba = LBATOMSF(sector);
nkeynes@237
   183
    read.cdread_bufaddr = buf;
nkeynes@237
   184
    switch(mode) {
nkeynes@237
   185
    case GDROM_MODE1:
nkeynes@237
   186
	call = CDROMREADMODE1;
nkeynes@237
   187
	read.cdread_buflen = sector_count * 2048;
nkeynes@237
   188
	break;
nkeynes@237
   189
    case GDROM_MODE2:
nkeynes@237
   190
	call = CDROMREADMODE2;
nkeynes@237
   191
	read.cdread_buflen = sector_count * 2336;
nkeynes@237
   192
	break;
nkeynes@237
   193
    default:
nkeynes@237
   194
	return PKT_ERR_BADREADMODE;
nkeynes@237
   195
    }
nkeynes@237
   196
nkeynes@237
   197
    if( ioctl( fd, call, &read ) == -1 ) {
nkeynes@237
   198
	ERROR( "Error reading disc (%s)", strerror(errno) );
nkeynes@237
   199
	return PKT_ERR_BADREAD;
nkeynes@237
   200
    }
nkeynes@237
   201
    *length = read.cdread_buflen;
nkeynes@237
   202
    return PKT_ERR_OK;
nkeynes@237
   203
}
nkeynes@237
   204
nkeynes@237
   205
static gdrom_error_t linux_read_sectors( gdrom_disc_t disc, uint32_t sector,
nkeynes@237
   206
					 uint32_t sector_count, int mode, char *buf,
nkeynes@237
   207
					 uint32_t *length )
nkeynes@237
   208
{
nkeynes@237
   209
    int fd = fileno(disc->file);
nkeynes@237
   210
    uint32_t real_sector = sector - CD_MSF_OFFSET;
nkeynes@259
   211
    uint32_t sector_size = 2048;
nkeynes@259
   212
    int buflen = sector_count * sector_size;
nkeynes@259
   213
    int i;
nkeynes@237
   214
    char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nkeynes@259
   215
nkeynes@259
   216
    for( i=0; i<sector_count; i += MAX_SECTORS_PER_CALL ) {
nkeynes@259
   217
	int count = MIN(MAX_SECTORS_PER_CALL, sector_count);
nkeynes@259
   218
	cmd[2] = (real_sector >> 24) & 0xFF;
nkeynes@259
   219
	cmd[3] = (real_sector >> 16) & 0xFF;
nkeynes@259
   220
	cmd[4] = (real_sector >> 8) & 0xFF;
nkeynes@259
   221
	cmd[5] = real_sector & 0xFF;
nkeynes@259
   222
	cmd[6] = (count >> 16) & 0xFF;
nkeynes@259
   223
	cmd[7] = (count >> 8) & 0xFF;
nkeynes@259
   224
	cmd[8] = count & 0xFF;
nkeynes@259
   225
	cmd[9] = 0x10;
nkeynes@237
   226
    
nkeynes@259
   227
	gdrom_error_t status = linux_send_command( fd, cmd, buf, count * sector_size, CGC_DATA_READ );
nkeynes@259
   228
	if( status != 0 ) {
nkeynes@259
   229
	    return status;
nkeynes@259
   230
	}
nkeynes@259
   231
	real_sector += count;
nkeynes@259
   232
	buf += count * sector_size;
nkeynes@237
   233
    }
nkeynes@259
   234
    *length = buflen;
nkeynes@259
   235
    return 0;
nkeynes@237
   236
}
nkeynes@237
   237
nkeynes@237
   238
/**
nkeynes@237
   239
 * Send a packet command to the device and wait for a response. 
nkeynes@237
   240
 * @return 0 on success, -1 on an operating system error, or a sense error
nkeynes@237
   241
 * code on a device error.
nkeynes@237
   242
 */
nkeynes@237
   243
static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t buflen,
nkeynes@237
   244
					 int direction )
nkeynes@237
   245
{
nkeynes@237
   246
    struct request_sense sense;
nkeynes@237
   247
    struct cdrom_generic_command cgc;
nkeynes@237
   248
nkeynes@237
   249
    memset( &cgc, 0, sizeof(cgc) );
nkeynes@237
   250
    memset( &sense, 0, sizeof(sense) );
nkeynes@237
   251
    memcpy( cgc.cmd, cmd, 12 );
nkeynes@237
   252
    cgc.buffer = buffer;
nkeynes@237
   253
    cgc.buflen = buflen;
nkeynes@237
   254
    cgc.sense = &sense;
nkeynes@237
   255
    cgc.data_direction = direction;
nkeynes@237
   256
    
nkeynes@237
   257
    if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
nkeynes@237
   258
	if( sense.sense_key == 0 ) {
nkeynes@237
   259
	    return -1; 
nkeynes@237
   260
	} else {
nkeynes@237
   261
	    return sense.sense_key | (sense.asc<<8);
nkeynes@237
   262
	}
nkeynes@237
   263
    } else {
nkeynes@237
   264
	return 0;
nkeynes@237
   265
    }
nkeynes@237
   266
}
.