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 (14 years ago)
permissions -rw-r--r--
last change Fix signedness warnings
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_MSF_START 150 /* MSF numbering starts after the initial pregap */
    36 #define CD_FRAMES_PER_SECOND 75
    37 #define CD_SECONDS_PER_MINUTE 60
    38 #define CD_FRAMES_PER_MINUTE (CD_FRAMES_PER_SECOND*CD_SECONDS_PER_MINUTE)
    40 #define GDROM_PREGAP 150  /* Sectors */
    42 extern uint32_t gdrom_sector_size[];
    43 #define GDROM_SECTOR_SIZE(x) gdrom_sector_size[x]
    44 /**
    45  * Track data type enumeration for cd images and devices. This somewhat
    46  * conflates the real track mode with the format of the image file, but
    47  * it manages to make sense so far.
    48  */
    49 typedef enum {
    50     GDROM_MODE0,          // Mode 0 - should never actually see this
    51     /* Data-only modes (image file contains only the user data) */
    52     GDROM_MODE1,          // Standard CD-Rom Mode 1 data track
    53     GDROM_MODE2_FORMLESS, // Mode 2 data track with no sub-structure (rare)
    54     GDROM_MODE2_FORM1,    // Mode 2/Form 1 data track (standard for multisession)
    55     GDROM_MODE2_FORM2,    // Mode 2/Form 2 data track (also fairly uncommon).
    56     GDROM_CDDA,           // Standard audio track
    58     /* This one is somewhat special - the image file contains the 2336 bytes of
    59      * "extended user data", which in turn contains either a form 1 or form 2
    60      * sector. In other words it's a raw mode2 XA sector without the 16-byte header.
    61      */
    62     GDROM_SEMIRAW_MODE2,
    63     /* Raw modes (image contains the full 2352-byte sector). Split into XA/Non-XA
    64      * here for convenience, although it's really a session level flag. */
    65     GDROM_RAW_XA,
    66     GDROM_RAW_NONXA,
    67 } gdrom_track_mode_t;
    69 /* The disc register indicates the current contents of the drive. When open
    70  * contains 0x06.
    71  */
    72 #define IDE_DISC_READY 0x01 /* ored with above */
    73 #define IDE_DISC_IDLE  0x02 /* ie spun-down */
    75 #define IDE_DISC_NONE    0x06
    76 #define IDE_DISC_AUDIO   0x00
    77 #define IDE_DISC_CDROM   0x10
    78 #define IDE_DISC_CDROMXA 0x20
    79 #define IDE_DISC_GDROM   0x80
    81 #define TRACK_PRE_EMPHASIS   0x10
    82 #define TRACK_COPY_PERMITTED 0x20
    83 #define TRACK_DATA           0x40
    84 #define TRACK_FOUR_CHANNEL   0x80
    86 typedef struct gdrom_track {
    87     gdrom_track_mode_t mode;
    88     uint8_t flags;        /* Track flags */
    89     int      session;     /* session # containing this track */
    90     uint32_t lba;         /* start sector address */
    91     uint32_t sector_size; /* For convenience, determined by mode */
    92     uint32_t sector_count;
    93     uint32_t offset;      /* File offset of start of track (image files only) */
    94     FILE *file;           /* Per-track file handle (if any) */
    95 } *gdrom_track_t;
    97 struct gdrom_disc {
    98     int disc_type;     /* One of the IDE_DISC_* flags */
    99     const gchar *name; /* Device name / Image filename (owned) */
   100     const gchar *display_name; /* User-friendly device name, if any (owned) */
   101     gchar mcn[14]; /* Media catalogue number */
   102     char title[129]; /* Disc title (if any) from bootstrap */
   103     int track_count;
   104     struct gdrom_track track[99];
   105     FILE *file;       /* Image file / device handle */
   106     void *impl_data; /* Implementation private data */
   108 	/* Check for media change. If the media cannot change (ie image file)
   109 	 * or is notified asynchonously, this should be a no-op. In the event of
   110 	 * a change, this function should update the structure according to the
   111 	 * new media (including TOC), and return TRUE.
   112 	 * @return TRUE if the media has changed since the last check, otherwise
   113 	 * FALSE.
   114 	 */
   115 	gboolean (*check_status)( struct gdrom_disc *disc );
   117     /**
   118      * Read a single sector from the disc at the specified logical address.
   119      * @param disc pointer to the disc structure
   120      * @param lba logical address to read from
   121      * @param mode mode field from the read command
   122      * @param buf buffer to receive data (at least MAX_SECTOR_SIZE bytes)
   123      * @param length unsigned int to receive the number of bytes actually read.
   124      * @return PKT_ERR_OK on success, or another PKT_ERR_* code on failure.
   125      */
   126     gdrom_error_t (*read_sector)( struct gdrom_disc *disc,
   127             uint32_t lba, int mode, 
   128             unsigned char *buf, uint32_t *length );
   130     /**
   131      * Begin playing audio from the given lba address on the disc.
   132      */
   133     gdrom_error_t (*play_audio)(struct gdrom_disc *disc, uint32_t lba, uint32_t endlba);
   135 	/**
   136 	 * Stop audio playback
   137 	 */
   138 	gdrom_error_t (*stop_audio)(struct gdrom_disc *disc);
   140     /**
   141      * Executed once per time slice to perform house-keeping operations 
   142      * (checking disc status, media changed, etc).
   143      */
   144     uint32_t (*run_time_slice)( struct gdrom_disc *disc, uint32_t nanosecs );
   146 	/**
   147 	 * Release all memory and system resources, including the gdrom_disc itself.
   148 	 * (implicitly calls close() if not already closed. 
   149 	 * @param disc The disc to destroy
   150 	 * @param close_fh if TRUE, close the main file/device, otherwise leave open.
   151 	 * This is mainly used when the handle will be immediately reused.
   152 	 */
   153     void (*destroy)( struct gdrom_disc *disc, gboolean close_fh );
   154 };
   156 /**
   157  * Low-level SCSI transport provided to the main SCSI/MMC driver. When used
   158  * this will be set as the disc->impl_data field.
   159  * Note: For symmetry there should be a packet_write variant, but we don't
   160  * currently need it for anything. YAGNI, etc.
   161  */
   162 typedef struct gdrom_scsi_transport {
   163 	/* Execute a read command (ie a command that returns a block of data in
   164 	 * response, not necessarily a CD read). 
   165 	 * @param scsi The disc to execute the command
   166 	 * @param cmd  The 12-byte command packet
   167 	 * @param buf  The buffer to receive the read results
   168 	 * @param length On entry, the size of buf. Modified on exit to the number
   169 	 *        of bytes actually read.
   170 	 * @return PKT_ERR_OK on success, otherwise the host error code.
   171 	 */
   172 	gdrom_error_t (*packet_read)( struct gdrom_disc *disc,
   173 	                              char *cmd, unsigned char *buf,
   174 	                              unsigned int *length );
   176 	/* Execute a generic command that does not write or return any data.
   177 	 * (eg play audio).
   178 	 * @param scsi The disc to execute the command
   179 	 * @param cmd  The 12-byte command packet
   180 	 * @return PKT_ERR_OK on success, otherwise the host error code.
   181 	 */
   182 	gdrom_error_t (*packet_cmd)( struct gdrom_disc *disc,
   183 	                             char *cmd );
   185 	/* Return TRUE if the media has changed since the last call, otherwise
   186 	 * FALSE. This method is used to implement the disc-level check_status
   187 	 * and should have no side-effects.
   188 	 */
   189 	gboolean (*media_changed)( struct gdrom_disc *disc );
   190 } *gdrom_scsi_transport_t;
   192 /**
   193  * Allocate a new gdrom_disc_t and initialize the filename and file fields.
   194  * The disc is otherwise uninitialized - this is an internal method for use 
   195  * by the concrete implementations.
   196  */
   197 gdrom_disc_t gdrom_disc_new(const gchar *filename, FILE *f);
   199 /**
   200  * Construct a new SCSI/MMC disc using the supplied transport implementation.
   201  */
   202 gdrom_disc_t gdrom_scsi_disc_new(const gchar *filename, FILE *f, gdrom_scsi_transport_t transport);
   204 /**
   205  * Construct a new image file using the default methods.
   206  */
   207 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f );
   209 #define SCSI_TRANSPORT(disc)  ((gdrom_scsi_transport_t)disc->impl_data)
   211 /**
   212  *
   213  */
   214 typedef struct gdrom_image_class {
   215     const gchar *name;
   216     const gchar *extension;
   217     gboolean (*is_valid_file)(FILE *f);
   218     gdrom_disc_t (*open_image_file)(const gchar *filename, FILE *f);
   219 } *gdrom_image_class_t;
   221 extern struct gdrom_image_class nrg_image_class;
   222 extern struct gdrom_image_class cdi_image_class;
   223 extern struct gdrom_image_class gdi_image_class;
   224 extern struct gdrom_image_class cdrom_device_class;
   226 /**
   227  * Determine the track number containing the specified sector by lba.
   228  */
   229 int gdrom_disc_get_track_by_lba( gdrom_disc_t image, uint32_t lba );
   231 /**
   232  * Default disc destroy method, for chaining from subclasses
   233  */
   234 void gdrom_disc_destroy( gdrom_disc_t disc, gboolean close_fh );
   236 gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name );
   238 void gdrom_device_destroy( gdrom_device_t dev );
   240 /************* Host-native support functions ***************/
   242 /**
   243  * Given a raw (2352 byte) data sector, extract the requested components into the 
   244  * target buffer. length will also be updated with the length of the copied
   245  * data
   246  */
   247 void gdrom_extract_raw_data_sector( char *sector_data, int mode, unsigned char *buf, uint32_t *length );
   249 /**
   250  * Check the disc for a useable DC bootstrap, and update the disc
   251  * with the title accordingly.
   252  * @return TRUE if we found a bootstrap, otherwise FALSE.
   253  */
   254 gboolean gdrom_disc_read_title( gdrom_disc_t disc ); 
   256 /** 
   257  * Parse a TOC mode-2 result buffer into the gdrom_disc_t data structure
   258  */
   259 void mmc_parse_toc2( gdrom_disc_t disc, unsigned char *buf );
   261 #endif /* !lxdream_gddriver_H */
.