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
8 * Copyright (c) 2005 Nathan Keynes.
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.
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.
21 #ifndef lxdream_gdrom_driver_H
22 #define lxdream_gdrom_driver_H 1
26 #include "gdrom/gdrom.h"
27 #include <glib/gstrfuncs.h>
29 #define MAX_SECTOR_SIZE 2352
31 #define CD_FRAMES_PER_SECOND 75
32 #define CD_SECONDS_PER_MINUTE 60
33 #define CD_FRAMES_PER_MINUTE (CD_FRAMES_PER_SECOND*CD_SECONDS_PER_MINUTE)
37 uint32_t first, last, leadout;
40 #define GDROM_PREGAP 150 /* Sectors */
42 extern uint32_t gdrom_sector_size[];
43 #define GDROM_SECTOR_SIZE(x) gdrom_sector_size[x]
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.
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.
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. */
69 /* The disc register indicates the current contents of the drive. When open
72 #define IDE_DISC_READY 0x01 /* ored with above */
73 #define IDE_DISC_IDLE 0x02 /* ie spun-down */
74 #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 */
99 * Read a single sector from the disc at the specified logical address.
100 * @param disc pointer to the disc structure
101 * @param lba logical address to read from
102 * @param mode mode field from the read command
103 * @param buf buffer to receive data (at least MAX_SECTOR_SIZE bytes)
104 * @param length unsigned int to receive the number of bytes actually read.
105 * @return PKT_ERR_OK on success, or another PKT_ERR_* code on failure.
107 gdrom_error_t (*read_sector)( struct gdrom_disc *disc,
108 uint32_t lba, int mode,
109 unsigned char *buf, uint32_t *length );
112 * Read the TOC from the disc and write it into the specified buffer.
113 * The method is responsible for returning the data in gd-rom
115 * @param disc pointer to the disc structure
116 * @param buf buffer to receive data (0x198 bytes long)
118 gdrom_error_t (*read_toc)(struct gdrom_disc *disc, unsigned char *buf);
121 * Read the information for the specified sector and return it in the
123 * @param disc pointer to the disc structure
124 * @param session of interest. If 0, return end of disc information.
125 * @param buf buffer to receive data (6 bytes)
127 gdrom_error_t (*read_session)(struct gdrom_disc *disc, int session, unsigned char *buf);
130 * Read the position information (subchannel) for the specified sector
131 * and return it in the supplied buffer. This method does not need to
132 * write the first 4 bytes of the buffer.
133 * @param disc pointer to the disc structure
134 * @param lba sector to get position information for
135 * @param buf buffer to receive data (14 bytes)
137 gdrom_error_t (*read_position)(struct gdrom_disc *disc, uint32_t lba, unsigned char *buf);
140 * Return the current disc status, expressed as a combination of the
141 * IDE_DISC_* flags above.
142 * @param disc pointer to the disc structure
143 * @return an integer status value.
145 int (*drive_status)(struct gdrom_disc *disc);
148 * Begin playing audio from the given lba address on the disc.
150 gdrom_error_t (*play_audio)(struct gdrom_disc *disc, uint32_t lba, uint32_t endlba);
153 * Executed once per time slice to perform house-keeping operations
154 * (checking disc status, media changed, etc).
156 uint32_t (*run_time_slice)( struct gdrom_disc *disc, uint32_t nanosecs );
159 * Close the disc and release any storage or resources allocated including
160 * the disc structure itself.
162 void (*close)( struct gdrom_disc *disc );
163 const gchar *name; /* Device name / Image filename */
166 typedef struct gdrom_image {
167 struct gdrom_disc disc;
170 struct gdrom_track track[99];
171 gchar mcn[14]; /* Media catalogue number */
172 FILE *file; /* Open file stream */
173 void *private; /* image private data */
179 typedef struct gdrom_image_class {
181 const gchar *extension;
182 gboolean (*is_valid_file)(FILE *f);
183 gdrom_disc_t (*open_image_file)(const gchar *filename, FILE *f);
184 } *gdrom_image_class_t;
186 extern struct gdrom_image_class nrg_image_class;
187 extern struct gdrom_image_class cdi_image_class;
188 extern struct gdrom_image_class gdi_image_class;
189 extern struct gdrom_image_class cdrom_device_class;
192 * Construct a new image file using the default methods.
194 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f );
197 * Destroy an image data structure without closing the file
198 * (Intended for use from image loaders only)
200 void gdrom_image_destroy_no_close( gdrom_disc_t d );
204 * Given a base filename (eg for a .cue file), generate the path for the given
205 * find_name relative to the original file.
206 * @return a newly allocated string.
208 gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *find_name );
210 gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name );
212 void gdrom_device_destroy( gdrom_device_t dev );
214 /************* Host-native support functions ***************/
217 * Parse a format 2 TOC, and write the results into the supplied disc structure.
219 void mmc_parse_toc2( gdrom_image_t disc, unsigned char *buf );
222 * Construct a Read CD command for the given sector + mode
224 void mmc_make_read_cd_cmd( char *cmd, uint32_t sector, int mode );
.