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
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * This file defines the structures and functions used by the GD-Rom
     5  * disc drivers. (ie, the modules that supply a CD image to be used by the
     6  * system).
     7  *
     8  * Copyright (c) 2005 Nathan Keynes.
     9  *
    10  * This program is free software; you can redistribute it and/or modify
    11  * it under the terms of the GNU General Public License as published by
    12  * the Free Software Foundation; either version 2 of the License, or
    13  * (at your option) any later version.
    14  *
    15  * This program is distributed in the hope that it will be useful,
    16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    18  * GNU General Public License for more details.
    19  */
    21 #ifndef lxdream_gddriver_H
    22 #define lxdream_gddriver_H 1
    24 #include <stdio.h>
    25 #include "lxdream.h"
    26 #include "gdrom/gdrom.h"
    27 #include <glib/gstrfuncs.h>
    29 #ifdef __cplusplus
    30 extern "C" {
    31 #endif
    33 #define MAX_SECTOR_SIZE 2352
    35 #define CD_FRAMES_PER_SECOND 75
    36 #define CD_SECONDS_PER_MINUTE 60
    37 #define CD_FRAMES_PER_MINUTE (CD_FRAMES_PER_SECOND*CD_SECONDS_PER_MINUTE)
    39 struct gdrom_toc {
    40     uint32_t track[99];
    41     uint32_t first, last, leadout;
    42 };
    44 #define GDROM_PREGAP 150  /* Sectors */
    46 extern uint32_t gdrom_sector_size[];
    47 #define GDROM_SECTOR_SIZE(x) gdrom_sector_size[x]
    48 /**
    49  * Track data type enumeration for cd images and devices. This somewhat
    50  * conflates the real track mode with the format of the image file, but
    51  * it manages to make sense so far.
    52  */
    53 typedef enum {
    54     GDROM_MODE0,          // Mode 0 - should never actually see this
    55     /* Data-only modes (image file contains only the user data) */
    56     GDROM_MODE1,          // Standard CD-Rom Mode 1 data track
    57     GDROM_MODE2_FORMLESS, // Mode 2 data track with no sub-structure (rare)
    58     GDROM_MODE2_FORM1,    // Mode 2/Form 1 data track (standard for multisession)
    59     GDROM_MODE2_FORM2,    // Mode 2/Form 2 data track (also fairly uncommon).
    60     GDROM_CDDA,           // Standard audio track
    62     /* This one is somewhat special - the image file contains the 2336 bytes of
    63      * "extended user data", which in turn contains either a form 1 or form 2
    64      * sector. In other words it's a raw mode2 XA sector without the 16-byte header.
    65      */
    66     GDROM_SEMIRAW_MODE2,
    67     /* Raw modes (image contains the full 2352-byte sector). Split into XA/Non-XA
    68      * here for convenience, although it's really a session level flag. */
    69     GDROM_RAW_XA,
    70     GDROM_RAW_NONXA,
    71 } gdrom_track_mode_t;
    73 /* The disc register indicates the current contents of the drive. When open
    74  * contains 0x06.
    75  */
    76 #define IDE_DISC_READY 0x01 /* ored with above */
    77 #define IDE_DISC_IDLE  0x02 /* ie spun-down */
    78 #define IDE_DISC_NONE  0x06
    80 #define IDE_DISC_AUDIO   0x00
    81 #define IDE_DISC_CDROM   0x10
    82 #define IDE_DISC_CDROMXA 0x20
    83 #define IDE_DISC_GDROM   0x80
    85 #define TRACK_PRE_EMPHASIS   0x10
    86 #define TRACK_COPY_PERMITTED 0x20
    87 #define TRACK_DATA           0x40
    88 #define TRACK_FOUR_CHANNEL   0x80
    90 typedef struct gdrom_track {
    91     gdrom_track_mode_t mode;
    92     uint8_t flags;        /* Track flags */
    93     int      session;     /* session # containing this track */
    94     uint32_t lba;         /* start sector address */
    95     uint32_t sector_size; /* For convenience, determined by mode */
    96     uint32_t sector_count;
    97     uint32_t offset; /* File offset of start of track - image files only */
    98     FILE *file;
    99 } *gdrom_track_t;
   101 struct gdrom_disc {
   102     /**
   103      * Read a single sector from the disc at the specified logical address.
   104      * @param disc pointer to the disc structure
   105      * @param lba logical address to read from
   106      * @param mode mode field from the read command
   107      * @param buf buffer to receive data (at least MAX_SECTOR_SIZE bytes)
   108      * @param length unsigned int to receive the number of bytes actually read.
   109      * @return PKT_ERR_OK on success, or another PKT_ERR_* code on failure.
   110      */
   111     gdrom_error_t (*read_sector)( struct gdrom_disc *disc,
   112             uint32_t lba, int mode, 
   113             unsigned char *buf, uint32_t *length );
   115     /**
   116      * Read the TOC from the disc and write it into the specified buffer.
   117      * The method is responsible for returning the data in gd-rom
   118      * format.
   119      * @param disc pointer to the disc structure
   120      * @param buf buffer to receive data (0x198 bytes long)
   121      */
   122     gdrom_error_t (*read_toc)(struct gdrom_disc *disc, unsigned char *buf);
   124     /**
   125      * Read the information for the specified sector and return it in the
   126      * supplied buffer. 
   127      * @param disc pointer to the disc structure
   128      * @param session of interest. If 0, return end of disc information.
   129      * @param buf buffer to receive data (6 bytes)
   130      */
   131     gdrom_error_t (*read_session)(struct gdrom_disc *disc, int session, unsigned char *buf);
   133     /**
   134      * Read the position information (subchannel) for the specified sector
   135      * and return it in the supplied buffer. This method does not need to
   136      * write the first 4 bytes of the buffer.
   137      * @param disc pointer to the disc structure
   138      * @param lba sector to get position information for
   139      * @param buf buffer to receive data (14 bytes)
   140      */
   141     gdrom_error_t (*read_position)(struct gdrom_disc *disc, uint32_t lba, unsigned char *buf);
   143     /**
   144      * Return the current disc status, expressed as a combination of the 
   145      * IDE_DISC_* flags above.
   146      * @param disc pointer to the disc structure
   147      * @return an integer status value.
   148      */
   149     int (*drive_status)(struct gdrom_disc *disc);
   151     /**
   152      * Begin playing audio from the given lba address on the disc.
   153      */
   154     gdrom_error_t (*play_audio)(struct gdrom_disc *disc, uint32_t lba, uint32_t endlba);
   156     /**
   157      * Executed once per time slice to perform house-keeping operations 
   158      * (checking disc status, media changed, etc).
   159      */
   160     uint32_t (*run_time_slice)( struct gdrom_disc *disc, uint32_t nanosecs );
   162     /**
   163      * Close the disc and release any storage or resources allocated including
   164      * the disc structure itself.
   165      */
   166     void (*close)( struct gdrom_disc *disc );
   167     const gchar *name; /* Device name / Image filename */
   168     char title[129]; /* Disc title (if any) */
   169 };
   171 typedef struct gdrom_image {
   172     struct gdrom_disc disc;
   173     int disc_type;
   174     int track_count;
   175     struct gdrom_track track[99];
   176     gchar mcn[14]; /* Media catalogue number */
   177     FILE *file; /* Open file stream */
   178     void *private; /* image private data */
   179 } *gdrom_image_t;
   181 /**
   182  *
   183  */
   184 typedef struct gdrom_image_class {
   185     const gchar *name;
   186     const gchar *extension;
   187     gboolean (*is_valid_file)(FILE *f);
   188     gdrom_disc_t (*open_image_file)(const gchar *filename, FILE *f);
   189 } *gdrom_image_class_t;
   191 extern struct gdrom_image_class nrg_image_class;
   192 extern struct gdrom_image_class cdi_image_class;
   193 extern struct gdrom_image_class gdi_image_class;
   194 extern struct gdrom_image_class cdrom_device_class;
   196 /**
   197  * Construct a new image file using the default methods.
   198  */
   199 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f );
   201 /**
   202  * Destroy an image data structure without closing the file
   203  * (Intended for use from image loaders only)
   204  */
   205 void gdrom_image_destroy_no_close( gdrom_disc_t d );
   207 /**
   208  * Determine the track number containing the specified sector by lba.
   209  */
   210 int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba );
   212 /**
   213  * Given a base filename (eg for a .cue file), generate the path for the given
   214  * find_name relative to the original file. 
   215  * @return a newly allocated string.
   216  */
   217 gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *find_name );
   219 gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name );
   221 void gdrom_device_destroy( gdrom_device_t dev );
   223 /************* Host-native support functions ***************/
   225 /**
   226  * Given a raw (2352 byte) data sector, extract the requested components into the 
   227  * target buffer. length will also be updated with the length of the copied
   228  * data
   229  */
   230 void gdrom_extract_raw_data_sector( char *sector_data, int mode, unsigned char *buf, uint32_t *length );
   232 /**
   233  * Parse a format 2 TOC, and write the results into the supplied disc structure.
   234  */
   235 void mmc_parse_toc2( gdrom_image_t disc, unsigned char *buf );
   237 /**
   238  * Construct a Read CD command for the given sector + mode
   239  */
   240 void mmc_make_read_cd_cmd( char *cmd, uint32_t sector, int mode );
   242 #endif /* !lxdream_gddriver_H */
.