Search
lxdream.org :: lxdream/src/gdrom/linux.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/linux.c
changeset 468:3a49695e081a
prev464:8e099fad42a6
next480:d28c2992f5ee
author nkeynes
date Sun Oct 28 07:23:46 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Fix CDI with multiple tracks/session
Fix file being closed too early
file annotate diff log raw
nkeynes@237
     1
/**
nkeynes@468
     2
 * $Id: linux.c,v 1.7 2007-10-28 07:23:46 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@464
    25
#include <fstab.h>
nkeynes@464
    26
#include <fcntl.h>
nkeynes@237
    27
nkeynes@237
    28
#include "gdrom/gdrom.h"
nkeynes@237
    29
#include "gdrom/packet.h"
nkeynes@237
    30
#include "dream.h"
nkeynes@237
    31
nkeynes@237
    32
#define MAXTOCENTRIES 600  /* This is a fairly generous overestimate really */
nkeynes@237
    33
#define MAXTOCSIZE 4 + (MAXTOCENTRIES*11)
nkeynes@342
    34
#define MAX_SECTORS_PER_CALL 1
nkeynes@237
    35
nkeynes@237
    36
#define MSFTOLBA( m,s,f ) (f + (s*CD_FRAMES) + (m*CD_FRAMES*CD_SECS))
nkeynes@237
    37
nkeynes@237
    38
static uint32_t inline lbatomsf( uint32_t lba ) {
nkeynes@237
    39
    union cdrom_addr addr;
nkeynes@237
    40
    lba = lba + CD_MSF_OFFSET;
nkeynes@237
    41
    addr.msf.frame = lba % CD_FRAMES;
nkeynes@237
    42
    int seconds = lba / CD_FRAMES;
nkeynes@237
    43
    addr.msf.second = seconds % CD_SECS;
nkeynes@237
    44
    addr.msf.minute = seconds / CD_SECS;
nkeynes@237
    45
    return addr.lba;
nkeynes@237
    46
}
nkeynes@237
    47
nkeynes@237
    48
#define LBATOMSF( lba ) lbatomsf(lba)
nkeynes@237
    49
nkeynes@237
    50
nkeynes@237
    51
static gboolean linux_image_is_valid( FILE *f );
nkeynes@237
    52
static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f );
nkeynes@342
    53
static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc );
nkeynes@342
    54
static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
nkeynes@422
    55
					int mode, unsigned char *buf, uint32_t *length );
nkeynes@422
    56
static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
nkeynes@237
    57
					 int direction );
nkeynes@353
    58
static int linux_drive_status( gdrom_disc_t disc );
nkeynes@237
    59
nkeynes@237
    60
struct gdrom_image_class linux_device_class = { "Linux", NULL,
nkeynes@237
    61
					     linux_image_is_valid, linux_open_device };
nkeynes@464
    62
GList *gdrom_get_native_devices(void)
nkeynes@464
    63
{
nkeynes@464
    64
    GList *list = NULL;
nkeynes@464
    65
    struct fstab *ent;
nkeynes@464
    66
    struct stat st;
nkeynes@464
    67
    setfsent();
nkeynes@464
    68
    while( (ent = getfsent()) != NULL ) {
nkeynes@464
    69
	if( (stat(ent->fs_spec, &st) != -1) && 
nkeynes@464
    70
	    S_ISBLK(st.st_mode) ) {
nkeynes@464
    71
	    /* Got a valid block device - is it a CDROM? */
nkeynes@464
    72
	    int fd = open(ent->fs_spec, O_RDONLY|O_NONBLOCK);
nkeynes@464
    73
	    if( fd == -1 )
nkeynes@464
    74
		continue;
nkeynes@464
    75
	    int caps = ioctl(fd, CDROM_GET_CAPABILITY);
nkeynes@464
    76
	    if( caps != -1 ) {
nkeynes@464
    77
		/* Appears to support CDROM functions */
nkeynes@464
    78
		list = g_list_append( list, g_strdup(ent->fs_spec) );
nkeynes@464
    79
	    }
nkeynes@464
    80
	    close(fd);
nkeynes@464
    81
	}
nkeynes@464
    82
    }
nkeynes@464
    83
    return list;
nkeynes@464
    84
}
nkeynes@237
    85
nkeynes@237
    86
static gboolean linux_image_is_valid( FILE *f )
nkeynes@237
    87
{
nkeynes@237
    88
    struct stat st;
nkeynes@237
    89
    struct cdrom_tochdr tochdr;
nkeynes@237
    90
nkeynes@237
    91
    if( fstat(fileno(f), &st) == -1 ) {
nkeynes@237
    92
	return FALSE; /* can't stat device? */
nkeynes@237
    93
    }
nkeynes@237
    94
    if( !S_ISBLK(st.st_mode) ) {
nkeynes@237
    95
	return FALSE; /* Not a block device */
nkeynes@237
    96
    }
nkeynes@237
    97
nkeynes@237
    98
    if( ioctl(fileno(f), CDROMREADTOCHDR, &tochdr) == -1 ) {
nkeynes@237
    99
	/* Quick check that this is really a CD */
nkeynes@237
   100
	return FALSE;
nkeynes@237
   101
    }
nkeynes@237
   102
nkeynes@237
   103
    return TRUE;
nkeynes@237
   104
}
nkeynes@237
   105
nkeynes@237
   106
static gdrom_disc_t linux_open_device( const gchar *filename, FILE *f ) 
nkeynes@237
   107
{
nkeynes@237
   108
    gdrom_disc_t disc;
nkeynes@237
   109
nkeynes@464
   110
    disc = gdrom_image_new(filename, f);
nkeynes@237
   111
    if( disc == NULL ) {
nkeynes@237
   112
	ERROR("Unable to allocate memory!");
nkeynes@237
   113
	return NULL;
nkeynes@237
   114
    }
nkeynes@237
   115
nkeynes@342
   116
    gdrom_error_t status = linux_read_disc_toc( (gdrom_image_t)disc );
nkeynes@237
   117
    if( status != 0 ) {
nkeynes@468
   118
	gdrom_image_destroy_no_close(disc);
nkeynes@237
   119
	if( status == 0xFFFF ) {
nkeynes@237
   120
	    ERROR("Unable to load disc table of contents (%s)", strerror(errno));
nkeynes@237
   121
	} else {
nkeynes@237
   122
	    ERROR("Unable to load disc table of contents (sense %d,%d)",
nkeynes@237
   123
		  status &0xFF, status >> 8 );
nkeynes@237
   124
	}
nkeynes@237
   125
	return NULL;
nkeynes@237
   126
    }
nkeynes@342
   127
    disc->read_sector = linux_read_sector;
nkeynes@353
   128
    disc->drive_status = linux_drive_status;
nkeynes@342
   129
    ((gdrom_image_t)disc)->disc_type = IDE_DISC_CDROM;
nkeynes@237
   130
    return disc;
nkeynes@237
   131
}
nkeynes@237
   132
nkeynes@353
   133
static int linux_drive_status( gdrom_disc_t disc )
nkeynes@353
   134
{
nkeynes@353
   135
    int fd = fileno(((gdrom_image_t)disc)->file);
nkeynes@353
   136
    int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
nkeynes@353
   137
    if( status == CDS_DISC_OK ) {
nkeynes@353
   138
	status = ioctl(fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
nkeynes@353
   139
	if( status != 0 ) {
nkeynes@422
   140
	    linux_read_disc_toc( (gdrom_image_t)disc);
nkeynes@353
   141
	}
nkeynes@353
   142
	return ((gdrom_image_t)disc)->disc_type | IDE_DISC_READY;
nkeynes@353
   143
    } else {
nkeynes@353
   144
	return IDE_DISC_NONE;
nkeynes@353
   145
    }
nkeynes@353
   146
}
nkeynes@237
   147
/**
nkeynes@237
   148
 * Read the full table of contents into the disc from the device.
nkeynes@237
   149
 */
nkeynes@342
   150
static gdrom_error_t linux_read_disc_toc( gdrom_image_t disc )
nkeynes@237
   151
{
nkeynes@237
   152
    int fd = fileno(disc->file);
nkeynes@237
   153
    unsigned char buf[MAXTOCSIZE];
nkeynes@422
   154
    size_t buflen = sizeof(buf);
nkeynes@237
   155
    char cmd[12] = { 0x43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nkeynes@237
   156
    
nkeynes@237
   157
    cmd[7] = (sizeof(buf))>>8;
nkeynes@237
   158
    cmd[8] = (sizeof(buf))&0xFF;
nkeynes@237
   159
    memset( buf, 0, sizeof(buf) );
nkeynes@342
   160
    gdrom_error_t status = linux_send_command( fd, cmd, buf, &buflen, CGC_DATA_READ );
nkeynes@237
   161
    if( status != 0 ) {
nkeynes@237
   162
	return status;
nkeynes@237
   163
    }
nkeynes@237
   164
nkeynes@237
   165
    int max_track = 0;
nkeynes@237
   166
    int last_track = -1;
nkeynes@237
   167
    int leadout = -1;
nkeynes@237
   168
    int len = (buf[0] << 8) | buf[1];
nkeynes@237
   169
    int session_type = GDROM_MODE1;
nkeynes@237
   170
    int i;
nkeynes@237
   171
    for( i = 4; i<len; i+=11 ) {
nkeynes@237
   172
	int session = buf[i];
nkeynes@237
   173
	int adr = buf[i+1] >> 4;
nkeynes@237
   174
	int point = buf[i+3];
nkeynes@237
   175
	if( adr == 0x01 && point > 0 && point < 100 ) {
nkeynes@237
   176
	    /* Track info */
nkeynes@237
   177
	    int trackno = point-1;
nkeynes@237
   178
	    if( point > max_track ) {
nkeynes@237
   179
		max_track = point;
nkeynes@237
   180
	    }
nkeynes@237
   181
	    disc->track[trackno].flags = (buf[i+1] & 0x0F) << 4;
nkeynes@237
   182
	    disc->track[trackno].session = session - 1;
nkeynes@237
   183
	    disc->track[trackno].lba = MSFTOLBA(buf[i+8],buf[i+9],buf[i+10]);
nkeynes@237
   184
	    if( disc->track[trackno].flags & TRACK_DATA ) {
nkeynes@237
   185
		disc->track[trackno].mode = GDROM_MODE1;
nkeynes@237
   186
	    } else {
nkeynes@237
   187
		disc->track[trackno].mode = GDROM_CDDA;
nkeynes@237
   188
	    }
nkeynes@237
   189
	    if( last_track != -1 ) {
nkeynes@237
   190
		disc->track[last_track].sector_count = disc->track[trackno].lba -
nkeynes@237
   191
		    disc->track[last_track].lba;
nkeynes@237
   192
	    }
nkeynes@237
   193
	    last_track = trackno;
nkeynes@237
   194
	} else switch( (adr << 8) | point ) {
nkeynes@237
   195
	case 0x1A0: /* session info */
nkeynes@237
   196
	    if( buf[i+9] == 0x20 ) {
nkeynes@237
   197
		session_type = GDROM_MODE2;
nkeynes@237
   198
	    } else {
nkeynes@237
   199
		session_type = GDROM_MODE1;
nkeynes@237
   200
	    }
nkeynes@237
   201
	case 0x1A2: /* leadout */
nkeynes@237
   202
	    leadout = MSFTOLBA(buf[i+8], buf[i+9], buf[i+10]);
nkeynes@237
   203
	    break;
nkeynes@237
   204
	}
nkeynes@237
   205
    }
nkeynes@237
   206
    disc->track_count = max_track;
nkeynes@237
   207
nkeynes@237
   208
    if( leadout != -1 && last_track != -1 ) {
nkeynes@237
   209
	disc->track[last_track].sector_count = leadout - disc->track[last_track].lba;
nkeynes@237
   210
    }
nkeynes@237
   211
    return 0;
nkeynes@237
   212
}
nkeynes@237
   213
nkeynes@464
   214
static gdrom_error_t linux_play_audio( gdrom_disc_t disc, uint32_t lba, uint32_t endlba )
nkeynes@464
   215
{
nkeynes@464
   216
    int fd = fileno( ((gdrom_image_t)disc)->file );
nkeynes@464
   217
    uint32_t real_sector = lba - CD_MSF_OFFSET;
nkeynes@464
   218
    uint32_t length = endlba - lba;
nkeynes@464
   219
    uint32_t buflen = 0;
nkeynes@464
   220
    char cmd[12] = { 0xA5, 0,0,0, 0,0,0,0, 0,0,0,0 };
nkeynes@464
   221
    cmd[2] = (real_sector >> 24) & 0xFF;
nkeynes@464
   222
    cmd[3] = (real_sector >> 16) & 0xFF;
nkeynes@464
   223
    cmd[4] = (real_sector >> 8) & 0xFF;
nkeynes@464
   224
    cmd[5] = real_sector & 0xFF;
nkeynes@464
   225
    cmd[6] = (length >> 24) & 0xFF;
nkeynes@464
   226
    cmd[7] = (length >> 16) & 0xFF;
nkeynes@464
   227
    cmd[8] = (length >> 8) & 0xFF;
nkeynes@464
   228
    cmd[9] = length & 0xFF;
nkeynes@464
   229
    
nkeynes@464
   230
    return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
nkeynes@464
   231
}
nkeynes@464
   232
nkeynes@464
   233
static gdrom_error_t linux_stop_audio( gdrom_disc_t disc )
nkeynes@464
   234
{
nkeynes@464
   235
    int fd = fileno( ((gdrom_image_t)disc)->file );
nkeynes@464
   236
    uint32_t buflen = 0;
nkeynes@464
   237
    char cmd[12] = {0x4E,0,0,0, 0,0,0,0, 0,0,0,0};
nkeynes@464
   238
    return linux_send_command( fd, cmd, NULL, &buflen, CGC_DATA_NONE );
nkeynes@464
   239
}
nkeynes@464
   240
nkeynes@342
   241
static gdrom_error_t linux_read_sector( gdrom_disc_t disc, uint32_t sector,
nkeynes@422
   242
					int mode, unsigned char *buf, uint32_t *length )
nkeynes@237
   243
{
nkeynes@342
   244
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   245
    int fd = fileno(image->file);
nkeynes@237
   246
    uint32_t real_sector = sector - CD_MSF_OFFSET;
nkeynes@342
   247
    uint32_t sector_size = MAX_SECTOR_SIZE;
nkeynes@237
   248
    char cmd[12] = { 0xBE, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nkeynes@259
   249
nkeynes@342
   250
    cmd[1] = (mode & 0x0E) << 1;
nkeynes@342
   251
    cmd[2] = (real_sector >> 24) & 0xFF;
nkeynes@342
   252
    cmd[3] = (real_sector >> 16) & 0xFF;
nkeynes@342
   253
    cmd[4] = (real_sector >> 8) & 0xFF;
nkeynes@342
   254
    cmd[5] = real_sector & 0xFF;
nkeynes@342
   255
    cmd[6] = 0;
nkeynes@342
   256
    cmd[7] = 0;
nkeynes@342
   257
    cmd[8] = 1;
nkeynes@342
   258
    cmd[9] = 0x10;
nkeynes@237
   259
    
nkeynes@342
   260
    gdrom_error_t status = linux_send_command( fd, cmd, buf, &sector_size, CGC_DATA_READ );
nkeynes@342
   261
    if( status != 0 ) {
nkeynes@342
   262
	return status;
nkeynes@237
   263
    }
nkeynes@342
   264
    *length = 2048;
nkeynes@259
   265
    return 0;
nkeynes@237
   266
}
nkeynes@237
   267
nkeynes@237
   268
/**
nkeynes@237
   269
 * Send a packet command to the device and wait for a response. 
nkeynes@237
   270
 * @return 0 on success, -1 on an operating system error, or a sense error
nkeynes@237
   271
 * code on a device error.
nkeynes@237
   272
 */
nkeynes@422
   273
static gdrom_error_t linux_send_command( int fd, char *cmd, unsigned char *buffer, size_t *buflen,
nkeynes@237
   274
					 int direction )
nkeynes@237
   275
{
nkeynes@237
   276
    struct request_sense sense;
nkeynes@237
   277
    struct cdrom_generic_command cgc;
nkeynes@237
   278
nkeynes@237
   279
    memset( &cgc, 0, sizeof(cgc) );
nkeynes@237
   280
    memset( &sense, 0, sizeof(sense) );
nkeynes@237
   281
    memcpy( cgc.cmd, cmd, 12 );
nkeynes@237
   282
    cgc.buffer = buffer;
nkeynes@342
   283
    cgc.buflen = *buflen;
nkeynes@237
   284
    cgc.sense = &sense;
nkeynes@237
   285
    cgc.data_direction = direction;
nkeynes@237
   286
    
nkeynes@237
   287
    if( ioctl(fd, CDROM_SEND_PACKET, &cgc) == -1 ) {
nkeynes@237
   288
	if( sense.sense_key == 0 ) {
nkeynes@237
   289
	    return -1; 
nkeynes@237
   290
	} else {
nkeynes@342
   291
	    /* TODO: Map newer codes back to the ones used by the gd-rom. */
nkeynes@237
   292
	    return sense.sense_key | (sense.asc<<8);
nkeynes@237
   293
	}
nkeynes@237
   294
    } else {
nkeynes@342
   295
	*buflen = cgc.buflen;
nkeynes@237
   296
	return 0;
nkeynes@237
   297
    }
nkeynes@237
   298
}
.