Search
lxdream.org :: lxdream/src/gdrom/linux.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/linux.c
changeset 237:6f1a429c9d12
next259:7c6881790cc2
author nkeynes
date Thu Dec 14 12:31:38 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change Add preliminary linux native CD driver support
file annotate diff log raw
nkeynes@237
     1
/**
nkeynes@237
     2
 * $Id: linux.c,v 1.1 2006-12-14 12:31:38 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@237
    32
nkeynes@237
    33
#define MSFTOLBA( m,s,f ) (f + (s*CD_FRAMES) + (m*CD_FRAMES*CD_SECS))
nkeynes@237
    34
nkeynes@237
    35
static uint32_t inline lbatomsf( uint32_t lba ) {
nkeynes@237
    36
    union cdrom_addr addr;
nkeynes@237
    37
    lba = lba + CD_MSF_OFFSET;
nkeynes@237
    38
    addr.msf.frame = lba % CD_FRAMES;
nkeynes@237
    39
    int seconds = lba / CD_FRAMES;
nkeynes@237
    40
    addr.msf.second = seconds % CD_SECS;
nkeynes@237
    41
    addr.msf.minute = seconds / CD_SECS;
nkeynes@237
    42
    return addr.lba;
nkeynes@237
    43
}
nkeynes@237
    44
nkeynes@237
    45
#define LBATOMSF( lba ) lbatomsf(lba)
nkeynes@237
    46
nkeynes@237
    47
nkeynes@237
    48
static gboolean linux_image_is_valid( FILE *f );
nkeynes@237
    49
static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
nkeynes@237
    50
static gdrom_error_t linux_read_disc_toc( gdrom_disc_t disc );
nkeynes@237
    51
static gdrom_error_t linux_read_sectors( gdrom_disc_t disc, uint32_t sector,
nkeynes@237
    52
					 uint32_t sector_count, int mode, char *buf,
nkeynes@237
    53
					 uint32_t *length );
nkeynes@237
    54
static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t buflen,
nkeynes@237
    55
					 int direction );
nkeynes@237
    56
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@237
    92
    gdrom_error_t status = linux_read_disc_toc( disc );
nkeynes@237
    93
    if( status != 0 ) {
nkeynes@237
    94
	free(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@237
   103
    disc->read_sectors = linux_read_sectors;
nkeynes@237
   104
    disc->disc_type = IDE_DISC_CDROM;
nkeynes@237
   105
    return disc;
nkeynes@237
   106
}
nkeynes@237
   107
nkeynes@237
   108
/**
nkeynes@237
   109
 * Read the full table of contents into the disc from the device.
nkeynes@237
   110
 */
nkeynes@237
   111
static gdrom_error_t linux_read_disc_toc( gdrom_disc_t disc )
nkeynes@237
   112
{
nkeynes@237
   113
    int fd = fileno(disc->file);
nkeynes@237
   114
    unsigned char buf[MAXTOCSIZE];
nkeynes@237
   115
    char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nkeynes@237
   116
    
nkeynes@237
   117
    cmd[7] = (sizeof(buf))>>8;
nkeynes@237
   118
    cmd[8] = (sizeof(buf))&0xFF;
nkeynes@237
   119
    memset( buf, 0, sizeof(buf) );
nkeynes@237
   120
    gdrom_error_t status = linux_send_command( fd, cmd, buf, sizeof(buf), CGC_DATA_READ );
nkeynes@237
   121
    if( status != 0 ) {
nkeynes@237
   122
	return status;
nkeynes@237
   123
    }
nkeynes@237
   124
nkeynes@237
   125
    int max_track = 0;
nkeynes@237
   126
    int last_track = -1;
nkeynes@237
   127
    int leadout = -1;
nkeynes@237
   128
    int len = (buf[0] << 8) | buf[1];
nkeynes@237
   129
    int session_type = GDROM_MODE1;
nkeynes@237
   130
    int i;
nkeynes@237
   131
    for( i = 4; i<len; i+=11 ) {
nkeynes@237
   132
	int session = buf[i];
nkeynes@237
   133
	int adr = buf[i+1] >> 4;
nkeynes@237
   134
	int point = buf[i+3];
nkeynes@237
   135
	if( adr == 0x01 && point > 0 && point < 100 ) {
nkeynes@237
   136
	    /* Track info */
nkeynes@237
   137
	    int trackno = point-1;
nkeynes@237
   138
	    if( point > max_track ) {
nkeynes@237
   139
		max_track = point;
nkeynes@237
   140
	    }
nkeynes@237
   141
	    disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
nkeynes@237
   142
	    disc->track[trackno].session = session - 1;
nkeynes@237
   143
	    disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
nkeynes@237
   144
	    if( disc->track[trackno].flags & TRACK_DATA ) {
nkeynes@237
   145
		disc->track[trackno].mode = GDROM_MODE1;
nkeynes@237
   146
	    } else {
nkeynes@237
   147
		disc->track[trackno].mode = GDROM_CDDA;
nkeynes@237
   148
	    }
nkeynes@237
   149
	    if( last_track != -1 ) {
nkeynes@237
   150
		disc->track[last_track].sector_count = disc->track[trackno].lba -
nkeynes@237
   151
		    disc->track[last_track].lba;
nkeynes@237
   152
	    }
nkeynes@237
   153
	    last_track = trackno;
nkeynes@237
   154
	} else switch( (adr << 8) | point ) {
nkeynes@237
   155
	case 0x1A0: /* session info */
nkeynes@237
   156
	    if( buf[i+9] == 0x20 ) {
nkeynes@237
   157
		session_type = GDROM_MODE2;
nkeynes@237
   158
	    } else {
nkeynes@237
   159
		session_type = GDROM_MODE1;
nkeynes@237
   160
	    }
nkeynes@237
   161
	case 0x1A2: /* leadout */
nkeynes@237
   162
	    leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
nkeynes@237
   163
	    break;
nkeynes@237
   164
	}
nkeynes@237
   165
    }
nkeynes@237
   166
    disc->track_count = max_track;
nkeynes@237
   167
nkeynes@237
   168
    if( leadout != -1 && last_track != -1 ) {
nkeynes@237
   169
	disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
nkeynes@237
   170
    }
nkeynes@237
   171
    return 0;
nkeynes@237
   172
}
nkeynes@237
   173
nkeynes@237
   174
static gdrom_error_t linux_read_sectors_ioctl( gdrom_disc_t disc, uint32_t sector,
nkeynes@237
   175
					 uint32_t sector_count, int mode, char *buf,
nkeynes@237
   176
					 uint32_t *length )
nkeynes@237
   177
{
nkeynes@237
   178
    int fd = fileno(disc->file);
nkeynes@237
   179
    int call;
nkeynes@237
   180
    struct cdrom_read read;
nkeynes@237
   181
    read.cdread_lba = LBATOMSF(sector);
nkeynes@237
   182
    read.cdread_bufaddr = buf;
nkeynes@237
   183
    switch(mode) {
nkeynes@237
   184
    case GDROM_MODE1:
nkeynes@237
   185
	call = CDROMREADMODE1;
nkeynes@237
   186
	read.cdread_buflen = sector_count * 2048;
nkeynes@237
   187
	break;
nkeynes@237
   188
    case GDROM_MODE2:
nkeynes@237
   189
	call = CDROMREADMODE2;
nkeynes@237
   190
	read.cdread_buflen = sector_count * 2336;
nkeynes@237
   191
	break;
nkeynes@237
   192
    default:
nkeynes@237
   193
	return PKT_ERR_BADREADMODE;
nkeynes@237
   194
    }
nkeynes@237
   195
nkeynes@237
   196
    if( ioctl( fd, call, &read ) == -1 ) {
nkeynes@237
   197
	ERROR( "Error reading disc (%s)", strerror(errno) );
nkeynes@237
   198
	return PKT_ERR_BADREAD;
nkeynes@237
   199
    }
nkeynes@237
   200
    *length = read.cdread_buflen;
nkeynes@237
   201
    return PKT_ERR_OK;
nkeynes@237
   202
}
nkeynes@237
   203
nkeynes@237
   204
static gdrom_error_t linux_read_sectors( gdrom_disc_t disc, uint32_t sector,
nkeynes@237
   205
					 uint32_t sector_count, int mode, char *buf,
nkeynes@237
   206
					 uint32_t *length )
nkeynes@237
   207
{
nkeynes@237
   208
    int fd = fileno(disc->file);
nkeynes@237
   209
    uint32_t real_sector = sector - CD_MSF_OFFSET;
nkeynes@237
   210
    int buflen = sector_count * 2048;
nkeynes@237
   211
    char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nkeynes@237
   212
    cmd[2] = (real_sector >> 24) & 0xFF;
nkeynes@237
   213
    cmd[3] = (real_sector >> 16) & 0xFF;
nkeynes@237
   214
    cmd[4] = (real_sector >> 8) & 0xFF;
nkeynes@237
   215
    cmd[5] = real_sector & 0xFF;
nkeynes@237
   216
    cmd[6] = (sector_count >> 16) & 0xFF;
nkeynes@237
   217
    cmd[7] = (sector_count >> 8) & 0xFF;
nkeynes@237
   218
    cmd[8] = sector_count & 0xFF;
nkeynes@237
   219
    cmd[9] = 0x10;
nkeynes@237
   220
    
nkeynes@237
   221
    gdrom_error_t status = linux_send_command( fd, cmd, buf, buflen, CGC_DATA_READ );
nkeynes@237
   222
    if( status == 0 ) {
nkeynes@237
   223
	*length = buflen;
nkeynes@237
   224
    }
nkeynes@237
   225
    return status;
nkeynes@237
   226
}
nkeynes@237
   227
nkeynes@237
   228
/**
nkeynes@237
   229
 * Send a packet command to the device and wait for a response. 
nkeynes@237
   230
 * @return 0 on success, -1 on an operating system error, or a sense error
nkeynes@237
   231
 * code on a device error.
nkeynes@237
   232
 */
nkeynes@237
   233
static gdrom_error_t linux_send_command( int fd, char *cmd, char *buffer, size_t buflen,
nkeynes@237
   234
					 int direction )
nkeynes@237
   235
{
nkeynes@237
   236
    struct request_sense sense;
nkeynes@237
   237
    struct cdrom_generic_command cgc;
nkeynes@237
   238
nkeynes@237
   239
    memset( &cgc, 0, sizeof(cgc) );
nkeynes@237
   240
    memset( &sense, 0, sizeof(sense) );
nkeynes@237
   241
    memcpy( cgc.cmd, cmd, 12 );
nkeynes@237
   242
    cgc.buffer = buffer;
nkeynes@237
   243
    cgc.buflen = buflen;
nkeynes@237
   244
    cgc.sense = &sense;
nkeynes@237
   245
    cgc.data_direction = direction;
nkeynes@237
   246
    
nkeynes@237
   247
    if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
nkeynes@237
   248
	if( sense.sense_key == 0 ) {
nkeynes@237
   249
	    return -1; 
nkeynes@237
   250
	} else {
nkeynes@237
   251
	    return sense.sense_key | (sense.asc<<8);
nkeynes@237
   252
	}
nkeynes@237
   253
    } else {
nkeynes@237
   254
	return 0;
nkeynes@237
   255
    }
nkeynes@237
   256
}
.