Search
lxdream.org :: lxdream/src/gdrom/gddriver.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gddriver.h
changeset 1025:f32183d273fb
prev1023:264e2fd90be8
next1030:864417a57662
author nkeynes
date Sat Jun 13 07:03:51 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix signedness warnings
file annotate diff log raw
nkeynes@678
     1
/**
nkeynes@729
     2
 * $Id$
nkeynes@678
     3
 *
nkeynes@678
     4
 * This file defines the structures and functions used by the GD-Rom
nkeynes@678
     5
 * disc drivers. (ie, the modules that supply a CD image to be used by the
nkeynes@678
     6
 * system).
nkeynes@678
     7
 *
nkeynes@678
     8
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@678
     9
 *
nkeynes@678
    10
 * This program is free software; you can redistribute it and/or modify
nkeynes@678
    11
 * it under the terms of the GNU General Public License as published by
nkeynes@678
    12
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@678
    13
 * (at your option) any later version.
nkeynes@678
    14
 *
nkeynes@678
    15
 * This program is distributed in the hope that it will be useful,
nkeynes@678
    16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@678
    17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@678
    18
 * GNU General Public License for more details.
nkeynes@678
    19
 */
nkeynes@678
    20
nkeynes@736
    21
#ifndef lxdream_gddriver_H
nkeynes@736
    22
#define lxdream_gddriver_H 1
nkeynes@678
    23
nkeynes@678
    24
#include <stdio.h>
nkeynes@678
    25
#include "lxdream.h"
nkeynes@678
    26
#include "gdrom/gdrom.h"
nkeynes@678
    27
#include <glib/gstrfuncs.h>
nkeynes@678
    28
nkeynes@736
    29
#ifdef __cplusplus
nkeynes@736
    30
extern "C" {
nkeynes@736
    31
#endif
nkeynes@736
    32
nkeynes@678
    33
#define MAX_SECTOR_SIZE 2352
nkeynes@678
    34
nkeynes@1023
    35
#define CD_MSF_START 150 /* MSF numbering starts after the initial pregap */
nkeynes@720
    36
#define CD_FRAMES_PER_SECOND 75
nkeynes@720
    37
#define CD_SECONDS_PER_MINUTE 60
nkeynes@720
    38
#define CD_FRAMES_PER_MINUTE (CD_FRAMES_PER_SECOND*CD_SECONDS_PER_MINUTE)
nkeynes@709
    39
nkeynes@678
    40
#define GDROM_PREGAP 150  /* Sectors */
nkeynes@678
    41
nkeynes@678
    42
extern uint32_t gdrom_sector_size[];
nkeynes@678
    43
#define GDROM_SECTOR_SIZE(x) gdrom_sector_size[x]
nkeynes@678
    44
/**
nkeynes@678
    45
 * Track data type enumeration for cd images and devices. This somewhat
nkeynes@678
    46
 * conflates the real track mode with the format of the image file, but
nkeynes@678
    47
 * it manages to make sense so far.
nkeynes@678
    48
 */
nkeynes@678
    49
typedef enum {
nkeynes@678
    50
    GDROM_MODE0,          // Mode 0 - should never actually see this
nkeynes@736
    51
    /* Data-only modes (image file contains only the user data) */
nkeynes@678
    52
    GDROM_MODE1,          // Standard CD-Rom Mode 1 data track
nkeynes@678
    53
    GDROM_MODE2_FORMLESS, // Mode 2 data track with no sub-structure (rare)
nkeynes@678
    54
    GDROM_MODE2_FORM1,    // Mode 2/Form 1 data track (standard for multisession)
nkeynes@678
    55
    GDROM_MODE2_FORM2,    // Mode 2/Form 2 data track (also fairly uncommon).
nkeynes@678
    56
    GDROM_CDDA,           // Standard audio track
nkeynes@678
    57
nkeynes@736
    58
    /* This one is somewhat special - the image file contains the 2336 bytes of
nkeynes@736
    59
     * "extended user data", which in turn contains either a form 1 or form 2
nkeynes@736
    60
     * sector. In other words it's a raw mode2 XA sector without the 16-byte header.
nkeynes@736
    61
     */
nkeynes@678
    62
    GDROM_SEMIRAW_MODE2,
nkeynes@736
    63
    /* Raw modes (image contains the full 2352-byte sector). Split into XA/Non-XA
nkeynes@736
    64
     * here for convenience, although it's really a session level flag. */
nkeynes@678
    65
    GDROM_RAW_XA,
nkeynes@678
    66
    GDROM_RAW_NONXA,
nkeynes@678
    67
} gdrom_track_mode_t;
nkeynes@678
    68
nkeynes@678
    69
/* The disc register indicates the current contents of the drive. When open
nkeynes@678
    70
 * contains 0x06.
nkeynes@678
    71
 */
nkeynes@678
    72
#define IDE_DISC_READY 0x01 /* ored with above */
nkeynes@678
    73
#define IDE_DISC_IDLE  0x02 /* ie spun-down */
nkeynes@678
    74
nkeynes@1023
    75
#define IDE_DISC_NONE    0x06
nkeynes@678
    76
#define IDE_DISC_AUDIO   0x00
nkeynes@678
    77
#define IDE_DISC_CDROM   0x10
nkeynes@678
    78
#define IDE_DISC_CDROMXA 0x20
nkeynes@678
    79
#define IDE_DISC_GDROM   0x80
nkeynes@678
    80
nkeynes@678
    81
#define TRACK_PRE_EMPHASIS   0x10
nkeynes@678
    82
#define TRACK_COPY_PERMITTED 0x20
nkeynes@678
    83
#define TRACK_DATA           0x40
nkeynes@678
    84
#define TRACK_FOUR_CHANNEL   0x80
nkeynes@678
    85
nkeynes@678
    86
typedef struct gdrom_track {
nkeynes@678
    87
    gdrom_track_mode_t mode;
nkeynes@678
    88
    uint8_t flags;        /* Track flags */
nkeynes@678
    89
    int      session;     /* session # containing this track */
nkeynes@678
    90
    uint32_t lba;         /* start sector address */
nkeynes@678
    91
    uint32_t sector_size; /* For convenience, determined by mode */
nkeynes@678
    92
    uint32_t sector_count;
nkeynes@1023
    93
    uint32_t offset;      /* File offset of start of track (image files only) */
nkeynes@1023
    94
    FILE *file;           /* Per-track file handle (if any) */
nkeynes@678
    95
} *gdrom_track_t;
nkeynes@678
    96
nkeynes@678
    97
struct gdrom_disc {
nkeynes@1023
    98
    int disc_type;     /* One of the IDE_DISC_* flags */
nkeynes@1023
    99
    const gchar *name; /* Device name / Image filename (owned) */
nkeynes@1023
   100
    const gchar *display_name; /* User-friendly device name, if any (owned) */
nkeynes@1023
   101
    gchar mcn[14]; /* Media catalogue number */
nkeynes@1023
   102
    char title[129]; /* Disc title (if any) from bootstrap */
nkeynes@1023
   103
    int track_count;
nkeynes@1023
   104
    struct gdrom_track track[99];
nkeynes@1023
   105
    FILE *file;       /* Image file / device handle */
nkeynes@1023
   106
    void *impl_data; /* Implementation private data */
nkeynes@1023
   107
nkeynes@1023
   108
	/* Check for media change. If the media cannot change (ie image file)
nkeynes@1023
   109
	 * or is notified asynchonously, this should be a no-op. In the event of
nkeynes@1023
   110
	 * a change, this function should update the structure according to the
nkeynes@1023
   111
	 * new media (including TOC), and return TRUE.
nkeynes@1023
   112
	 * @return TRUE if the media has changed since the last check, otherwise
nkeynes@1023
   113
	 * FALSE.
nkeynes@1023
   114
	 */
nkeynes@1023
   115
	gboolean (*check_status)( struct gdrom_disc *disc );
nkeynes@1023
   116
nkeynes@678
   117
    /**
nkeynes@678
   118
     * Read a single sector from the disc at the specified logical address.
nkeynes@678
   119
     * @param disc pointer to the disc structure
nkeynes@678
   120
     * @param lba logical address to read from
nkeynes@678
   121
     * @param mode mode field from the read command
nkeynes@678
   122
     * @param buf buffer to receive data (at least MAX_SECTOR_SIZE bytes)
nkeynes@678
   123
     * @param length unsigned int to receive the number of bytes actually read.
nkeynes@678
   124
     * @return PKT_ERR_OK on success, or another PKT_ERR_* code on failure.
nkeynes@678
   125
     */
nkeynes@678
   126
    gdrom_error_t (*read_sector)( struct gdrom_disc *disc,
nkeynes@736
   127
            uint32_t lba, int mode, 
nkeynes@736
   128
            unsigned char *buf, uint32_t *length );
nkeynes@736
   129
nkeynes@678
   130
    /**
nkeynes@678
   131
     * Begin playing audio from the given lba address on the disc.
nkeynes@678
   132
     */
nkeynes@678
   133
    gdrom_error_t (*play_audio)(struct gdrom_disc *disc, uint32_t lba, uint32_t endlba);
nkeynes@678
   134
nkeynes@1023
   135
	/**
nkeynes@1023
   136
	 * Stop audio playback
nkeynes@1023
   137
	 */
nkeynes@1023
   138
	gdrom_error_t (*stop_audio)(struct gdrom_disc *disc);
nkeynes@1023
   139
nkeynes@678
   140
    /**
nkeynes@678
   141
     * Executed once per time slice to perform house-keeping operations 
nkeynes@678
   142
     * (checking disc status, media changed, etc).
nkeynes@678
   143
     */
nkeynes@678
   144
    uint32_t (*run_time_slice)( struct gdrom_disc *disc, uint32_t nanosecs );
nkeynes@678
   145
nkeynes@1023
   146
	/**
nkeynes@1023
   147
	 * Release all memory and system resources, including the gdrom_disc itself.
nkeynes@1023
   148
	 * (implicitly calls close() if not already closed. 
nkeynes@1023
   149
	 * @param disc The disc to destroy
nkeynes@1023
   150
	 * @param close_fh if TRUE, close the main file/device, otherwise leave open.
nkeynes@1023
   151
	 * This is mainly used when the handle will be immediately reused.
nkeynes@1023
   152
	 */
nkeynes@1023
   153
    void (*destroy)( struct gdrom_disc *disc, gboolean close_fh );
nkeynes@678
   154
};
nkeynes@678
   155
nkeynes@1023
   156
/**
nkeynes@1023
   157
 * Low-level SCSI transport provided to the main SCSI/MMC driver. When used
nkeynes@1023
   158
 * this will be set as the disc->impl_data field.
nkeynes@1023
   159
 * Note: For symmetry there should be a packet_write variant, but we don't
nkeynes@1023
   160
 * currently need it for anything. YAGNI, etc.
nkeynes@1023
   161
 */
nkeynes@1023
   162
typedef struct gdrom_scsi_transport {
nkeynes@1023
   163
	/* Execute a read command (ie a command that returns a block of data in
nkeynes@1023
   164
	 * response, not necessarily a CD read). 
nkeynes@1023
   165
	 * @param scsi The disc to execute the command
nkeynes@1023
   166
	 * @param cmd  The 12-byte command packet
nkeynes@1023
   167
	 * @param buf  The buffer to receive the read results
nkeynes@1023
   168
	 * @param length On entry, the size of buf. Modified on exit to the number
nkeynes@1023
   169
	 *        of bytes actually read.
nkeynes@1023
   170
	 * @return PKT_ERR_OK on success, otherwise the host error code.
nkeynes@1023
   171
	 */
nkeynes@1023
   172
	gdrom_error_t (*packet_read)( struct gdrom_disc *disc,
nkeynes@1025
   173
	                              char *cmd, unsigned char *buf,
nkeynes@1023
   174
	                              unsigned int *length );
nkeynes@1023
   175
	                              
nkeynes@1023
   176
	/* Execute a generic command that does not write or return any data.
nkeynes@1023
   177
	 * (eg play audio).
nkeynes@1023
   178
	 * @param scsi The disc to execute the command
nkeynes@1023
   179
	 * @param cmd  The 12-byte command packet
nkeynes@1023
   180
	 * @return PKT_ERR_OK on success, otherwise the host error code.
nkeynes@1023
   181
	 */
nkeynes@1023
   182
	gdrom_error_t (*packet_cmd)( struct gdrom_disc *disc,
nkeynes@1025
   183
	                             char *cmd );
nkeynes@1023
   184
	
nkeynes@1023
   185
	/* Return TRUE if the media has changed since the last call, otherwise
nkeynes@1023
   186
	 * FALSE. This method is used to implement the disc-level check_status
nkeynes@1023
   187
	 * and should have no side-effects.
nkeynes@1023
   188
	 */
nkeynes@1023
   189
	gboolean (*media_changed)( struct gdrom_disc *disc );
nkeynes@1023
   190
} *gdrom_scsi_transport_t;
nkeynes@1023
   191
nkeynes@1023
   192
/**
nkeynes@1023
   193
 * Allocate a new gdrom_disc_t and initialize the filename and file fields.
nkeynes@1023
   194
 * The disc is otherwise uninitialized - this is an internal method for use 
nkeynes@1023
   195
 * by the concrete implementations.
nkeynes@1023
   196
 */
nkeynes@1023
   197
gdrom_disc_t gdrom_disc_new(const gchar *filename, FILE *f);
nkeynes@1023
   198
nkeynes@1023
   199
/**
nkeynes@1023
   200
 * Construct a new SCSI/MMC disc using the supplied transport implementation.
nkeynes@1023
   201
 */
nkeynes@1023
   202
gdrom_disc_t gdrom_scsi_disc_new(const gchar *filename, FILE *f, gdrom_scsi_transport_t transport);
nkeynes@1023
   203
nkeynes@1023
   204
/**
nkeynes@1023
   205
 * Construct a new image file using the default methods.
nkeynes@1023
   206
 */
nkeynes@1023
   207
gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f );
nkeynes@1023
   208
nkeynes@1023
   209
#define SCSI_TRANSPORT(disc)  ((gdrom_scsi_transport_t)disc->impl_data)
nkeynes@678
   210
nkeynes@678
   211
/**
nkeynes@678
   212
 *
nkeynes@678
   213
 */
nkeynes@678
   214
typedef struct gdrom_image_class {
nkeynes@678
   215
    const gchar *name;
nkeynes@678
   216
    const gchar *extension;
nkeynes@678
   217
    gboolean (*is_valid_file)(FILE *f);
nkeynes@678
   218
    gdrom_disc_t (*open_image_file)(const gchar *filename, FILE *f);
nkeynes@678
   219
} *gdrom_image_class_t;
nkeynes@678
   220
nkeynes@678
   221
extern struct gdrom_image_class nrg_image_class;
nkeynes@678
   222
extern struct gdrom_image_class cdi_image_class;
nkeynes@678
   223
extern struct gdrom_image_class gdi_image_class;
nkeynes@678
   224
extern struct gdrom_image_class cdrom_device_class;
nkeynes@678
   225
nkeynes@678
   226
/**
nkeynes@1023
   227
 * Determine the track number containing the specified sector by lba.
nkeynes@678
   228
 */
nkeynes@1023
   229
int gdrom_disc_get_track_by_lba( gdrom_disc_t image, uint32_t lba );
nkeynes@678
   230
nkeynes@678
   231
/**
nkeynes@1023
   232
 * Default disc destroy method, for chaining from subclasses
nkeynes@678
   233
 */
nkeynes@1023
   234
void gdrom_disc_destroy( gdrom_disc_t disc, gboolean close_fh );
nkeynes@678
   235
nkeynes@709
   236
gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name );
nkeynes@709
   237
nkeynes@709
   238
void gdrom_device_destroy( gdrom_device_t dev );
nkeynes@709
   239
nkeynes@720
   240
/************* Host-native support functions ***************/
nkeynes@720
   241
nkeynes@720
   242
/**
nkeynes@782
   243
 * Given a raw (2352 byte) data sector, extract the requested components into the 
nkeynes@782
   244
 * target buffer. length will also be updated with the length of the copied
nkeynes@782
   245
 * data
nkeynes@782
   246
 */
nkeynes@782
   247
void gdrom_extract_raw_data_sector( char *sector_data, int mode, unsigned char *buf, uint32_t *length );
nkeynes@782
   248
nkeynes@782
   249
/**
nkeynes@1023
   250
 * Check the disc for a useable DC bootstrap, and update the disc
nkeynes@1023
   251
 * with the title accordingly.
nkeynes@1023
   252
 * @return TRUE if we found a bootstrap, otherwise FALSE.
nkeynes@720
   253
 */
nkeynes@1023
   254
gboolean gdrom_disc_read_title( gdrom_disc_t disc ); 
nkeynes@736
   255
nkeynes@1025
   256
/** 
nkeynes@1025
   257
 * Parse a TOC mode-2 result buffer into the gdrom_disc_t data structure
nkeynes@1025
   258
 */
nkeynes@1025
   259
void mmc_parse_toc2( gdrom_disc_t disc, unsigned char *buf );
nkeynes@1025
   260
nkeynes@736
   261
#endif /* !lxdream_gddriver_H */
.