Search
lxdream.org :: lxdream/src/gdrom/gddriver.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gddriver.h
changeset 837:4eae2ddccf9c
prev782:1af8b5ce627c
next1023:264e2fd90be8
author nkeynes
date Thu Aug 28 01:39:51 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Extract the disc title (where available) and display in the title bar
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@720
    35
#define CD_FRAMES_PER_SECOND 75
nkeynes@720
    36
#define CD_SECONDS_PER_MINUTE 60
nkeynes@720
    37
#define CD_FRAMES_PER_MINUTE (CD_FRAMES_PER_SECOND*CD_SECONDS_PER_MINUTE)
nkeynes@709
    38
nkeynes@678
    39
struct gdrom_toc {
nkeynes@678
    40
    uint32_t track[99];
nkeynes@678
    41
    uint32_t first, last, leadout;
nkeynes@678
    42
};
nkeynes@678
    43
nkeynes@678
    44
#define GDROM_PREGAP 150  /* Sectors */
nkeynes@678
    45
nkeynes@678
    46
extern uint32_t gdrom_sector_size[];
nkeynes@678
    47
#define GDROM_SECTOR_SIZE(x) gdrom_sector_size[x]
nkeynes@678
    48
/**
nkeynes@678
    49
 * Track data type enumeration for cd images and devices. This somewhat
nkeynes@678
    50
 * conflates the real track mode with the format of the image file, but
nkeynes@678
    51
 * it manages to make sense so far.
nkeynes@678
    52
 */
nkeynes@678
    53
typedef enum {
nkeynes@678
    54
    GDROM_MODE0,          // Mode 0 - should never actually see this
nkeynes@736
    55
    /* Data-only modes (image file contains only the user data) */
nkeynes@678
    56
    GDROM_MODE1,          // Standard CD-Rom Mode 1 data track
nkeynes@678
    57
    GDROM_MODE2_FORMLESS, // Mode 2 data track with no sub-structure (rare)
nkeynes@678
    58
    GDROM_MODE2_FORM1,    // Mode 2/Form 1 data track (standard for multisession)
nkeynes@678
    59
    GDROM_MODE2_FORM2,    // Mode 2/Form 2 data track (also fairly uncommon).
nkeynes@678
    60
    GDROM_CDDA,           // Standard audio track
nkeynes@678
    61
nkeynes@736
    62
    /* This one is somewhat special - the image file contains the 2336 bytes of
nkeynes@736
    63
     * "extended user data", which in turn contains either a form 1 or form 2
nkeynes@736
    64
     * sector. In other words it's a raw mode2 XA sector without the 16-byte header.
nkeynes@736
    65
     */
nkeynes@678
    66
    GDROM_SEMIRAW_MODE2,
nkeynes@736
    67
    /* Raw modes (image contains the full 2352-byte sector). Split into XA/Non-XA
nkeynes@736
    68
     * here for convenience, although it's really a session level flag. */
nkeynes@678
    69
    GDROM_RAW_XA,
nkeynes@678
    70
    GDROM_RAW_NONXA,
nkeynes@678
    71
} gdrom_track_mode_t;
nkeynes@678
    72
nkeynes@678
    73
/* The disc register indicates the current contents of the drive. When open
nkeynes@678
    74
 * contains 0x06.
nkeynes@678
    75
 */
nkeynes@678
    76
#define IDE_DISC_READY 0x01 /* ored with above */
nkeynes@678
    77
#define IDE_DISC_IDLE  0x02 /* ie spun-down */
nkeynes@678
    78
#define IDE_DISC_NONE  0x06
nkeynes@678
    79
nkeynes@678
    80
#define IDE_DISC_AUDIO   0x00
nkeynes@678
    81
#define IDE_DISC_CDROM   0x10
nkeynes@678
    82
#define IDE_DISC_CDROMXA 0x20
nkeynes@678
    83
#define IDE_DISC_GDROM   0x80
nkeynes@678
    84
nkeynes@678
    85
#define TRACK_PRE_EMPHASIS   0x10
nkeynes@678
    86
#define TRACK_COPY_PERMITTED 0x20
nkeynes@678
    87
#define TRACK_DATA           0x40
nkeynes@678
    88
#define TRACK_FOUR_CHANNEL   0x80
nkeynes@678
    89
nkeynes@678
    90
typedef struct gdrom_track {
nkeynes@678
    91
    gdrom_track_mode_t mode;
nkeynes@678
    92
    uint8_t flags;        /* Track flags */
nkeynes@678
    93
    int      session;     /* session # containing this track */
nkeynes@678
    94
    uint32_t lba;         /* start sector address */
nkeynes@678
    95
    uint32_t sector_size; /* For convenience, determined by mode */
nkeynes@678
    96
    uint32_t sector_count;
nkeynes@678
    97
    uint32_t offset; /* File offset of start of track - image files only */
nkeynes@678
    98
    FILE *file;
nkeynes@678
    99
} *gdrom_track_t;
nkeynes@678
   100
nkeynes@678
   101
struct gdrom_disc {
nkeynes@678
   102
    /**
nkeynes@678
   103
     * Read a single sector from the disc at the specified logical address.
nkeynes@678
   104
     * @param disc pointer to the disc structure
nkeynes@678
   105
     * @param lba logical address to read from
nkeynes@678
   106
     * @param mode mode field from the read command
nkeynes@678
   107
     * @param buf buffer to receive data (at least MAX_SECTOR_SIZE bytes)
nkeynes@678
   108
     * @param length unsigned int to receive the number of bytes actually read.
nkeynes@678
   109
     * @return PKT_ERR_OK on success, or another PKT_ERR_* code on failure.
nkeynes@678
   110
     */
nkeynes@678
   111
    gdrom_error_t (*read_sector)( struct gdrom_disc *disc,
nkeynes@736
   112
            uint32_t lba, int mode, 
nkeynes@736
   113
            unsigned char *buf, uint32_t *length );
nkeynes@736
   114
nkeynes@678
   115
    /**
nkeynes@678
   116
     * Read the TOC from the disc and write it into the specified buffer.
nkeynes@678
   117
     * The method is responsible for returning the data in gd-rom
nkeynes@678
   118
     * format.
nkeynes@678
   119
     * @param disc pointer to the disc structure
nkeynes@678
   120
     * @param buf buffer to receive data (0x198 bytes long)
nkeynes@678
   121
     */
nkeynes@678
   122
    gdrom_error_t (*read_toc)(struct gdrom_disc *disc, unsigned char *buf);
nkeynes@678
   123
nkeynes@678
   124
    /**
nkeynes@678
   125
     * Read the information for the specified sector and return it in the
nkeynes@678
   126
     * supplied buffer. 
nkeynes@678
   127
     * @param disc pointer to the disc structure
nkeynes@678
   128
     * @param session of interest. If 0, return end of disc information.
nkeynes@678
   129
     * @param buf buffer to receive data (6 bytes)
nkeynes@678
   130
     */
nkeynes@678
   131
    gdrom_error_t (*read_session)(struct gdrom_disc *disc, int session, unsigned char *buf);
nkeynes@678
   132
nkeynes@678
   133
    /**
nkeynes@678
   134
     * Read the position information (subchannel) for the specified sector
nkeynes@678
   135
     * and return it in the supplied buffer. This method does not need to
nkeynes@678
   136
     * write the first 4 bytes of the buffer.
nkeynes@678
   137
     * @param disc pointer to the disc structure
nkeynes@678
   138
     * @param lba sector to get position information for
nkeynes@678
   139
     * @param buf buffer to receive data (14 bytes)
nkeynes@678
   140
     */
nkeynes@678
   141
    gdrom_error_t (*read_position)(struct gdrom_disc *disc, uint32_t lba, unsigned char *buf);
nkeynes@678
   142
nkeynes@678
   143
    /**
nkeynes@678
   144
     * Return the current disc status, expressed as a combination of the 
nkeynes@678
   145
     * IDE_DISC_* flags above.
nkeynes@678
   146
     * @param disc pointer to the disc structure
nkeynes@678
   147
     * @return an integer status value.
nkeynes@678
   148
     */
nkeynes@678
   149
    int (*drive_status)(struct gdrom_disc *disc);
nkeynes@678
   150
nkeynes@678
   151
    /**
nkeynes@678
   152
     * Begin playing audio from the given lba address on the disc.
nkeynes@678
   153
     */
nkeynes@678
   154
    gdrom_error_t (*play_audio)(struct gdrom_disc *disc, uint32_t lba, uint32_t endlba);
nkeynes@678
   155
nkeynes@678
   156
    /**
nkeynes@678
   157
     * Executed once per time slice to perform house-keeping operations 
nkeynes@678
   158
     * (checking disc status, media changed, etc).
nkeynes@678
   159
     */
nkeynes@678
   160
    uint32_t (*run_time_slice)( struct gdrom_disc *disc, uint32_t nanosecs );
nkeynes@678
   161
nkeynes@678
   162
    /**
nkeynes@678
   163
     * Close the disc and release any storage or resources allocated including
nkeynes@678
   164
     * the disc structure itself.
nkeynes@678
   165
     */
nkeynes@678
   166
    void (*close)( struct gdrom_disc *disc );
nkeynes@678
   167
    const gchar *name; /* Device name / Image filename */
nkeynes@837
   168
    char title[129]; /* Disc title (if any) */
nkeynes@678
   169
};
nkeynes@678
   170
nkeynes@678
   171
typedef struct gdrom_image {
nkeynes@678
   172
    struct gdrom_disc disc;
nkeynes@678
   173
    int disc_type;
nkeynes@678
   174
    int track_count;
nkeynes@678
   175
    struct gdrom_track track[99];
nkeynes@678
   176
    gchar mcn[14]; /* Media catalogue number */
nkeynes@678
   177
    FILE *file; /* Open file stream */
nkeynes@720
   178
    void *private; /* image private data */
nkeynes@678
   179
} *gdrom_image_t;
nkeynes@678
   180
nkeynes@678
   181
/**
nkeynes@678
   182
 *
nkeynes@678
   183
 */
nkeynes@678
   184
typedef struct gdrom_image_class {
nkeynes@678
   185
    const gchar *name;
nkeynes@678
   186
    const gchar *extension;
nkeynes@678
   187
    gboolean (*is_valid_file)(FILE *f);
nkeynes@678
   188
    gdrom_disc_t (*open_image_file)(const gchar *filename, FILE *f);
nkeynes@678
   189
} *gdrom_image_class_t;
nkeynes@678
   190
nkeynes@678
   191
extern struct gdrom_image_class nrg_image_class;
nkeynes@678
   192
extern struct gdrom_image_class cdi_image_class;
nkeynes@678
   193
extern struct gdrom_image_class gdi_image_class;
nkeynes@678
   194
extern struct gdrom_image_class cdrom_device_class;
nkeynes@678
   195
nkeynes@678
   196
/**
nkeynes@678
   197
 * Construct a new image file using the default methods.
nkeynes@678
   198
 */
nkeynes@678
   199
gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f );
nkeynes@678
   200
nkeynes@678
   201
/**
nkeynes@678
   202
 * Destroy an image data structure without closing the file
nkeynes@678
   203
 * (Intended for use from image loaders only)
nkeynes@678
   204
 */
nkeynes@678
   205
void gdrom_image_destroy_no_close( gdrom_disc_t d );
nkeynes@678
   206
nkeynes@782
   207
/**
nkeynes@782
   208
 * Determine the track number containing the specified sector by lba.
nkeynes@782
   209
 */
nkeynes@782
   210
int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba );
nkeynes@678
   211
nkeynes@678
   212
/**
nkeynes@678
   213
 * Given a base filename (eg for a .cue file), generate the path for the given
nkeynes@678
   214
 * find_name relative to the original file. 
nkeynes@678
   215
 * @return a newly allocated string.
nkeynes@678
   216
 */
nkeynes@678
   217
gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *find_name );
nkeynes@678
   218
nkeynes@709
   219
gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name );
nkeynes@709
   220
nkeynes@709
   221
void gdrom_device_destroy( gdrom_device_t dev );
nkeynes@709
   222
nkeynes@720
   223
/************* Host-native support functions ***************/
nkeynes@720
   224
nkeynes@720
   225
/**
nkeynes@782
   226
 * Given a raw (2352 byte) data sector, extract the requested components into the 
nkeynes@782
   227
 * target buffer. length will also be updated with the length of the copied
nkeynes@782
   228
 * data
nkeynes@782
   229
 */
nkeynes@782
   230
void gdrom_extract_raw_data_sector( char *sector_data, int mode, unsigned char *buf, uint32_t *length );
nkeynes@782
   231
nkeynes@782
   232
/**
nkeynes@720
   233
 * Parse a format 2 TOC, and write the results into the supplied disc structure.
nkeynes@720
   234
 */
nkeynes@720
   235
void mmc_parse_toc2( gdrom_image_t disc, unsigned char *buf );
nkeynes@720
   236
nkeynes@720
   237
/**
nkeynes@720
   238
 * Construct a Read CD command for the given sector + mode
nkeynes@720
   239
 */
nkeynes@720
   240
void mmc_make_read_cd_cmd( char *cmd, uint32_t sector, int mode );
nkeynes@736
   241
nkeynes@736
   242
#endif /* !lxdream_gddriver_H */
.