4 * Copyright (c) 2009 Nathan Keynes.
6 * Internal CD-ROM implementation header
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #ifndef cdrom_cdimpl_H
20 #define cdrom_cdimpl_H 1
22 #include "drivers/cdrom/cdrom.h"
23 #include "drivers/cdrom/drive.h"
31 * Disc factory implementation, to construct cdrom_disc_t objects from a file
34 typedef struct cdrom_disc_factory {
35 /* Human-readable name for the discs constructed by the factory */
36 const char *display_name;
37 /* Default file extension for supported discs */
38 const char *extension;
40 /* Test if the given file is facially valid for the factory
41 * (that is, it passes all file magic etc checks)
43 gboolean (*is_valid_file)(FILE *f);
45 /* Perform any additional initialization needed for the disc type
46 * (run after the default initialization). May be NULL if no init
48 * @return TRUE on success, FALSE on error.
50 gboolean (*init)(cdrom_disc_t disc, ERROR *err);
52 /* Read the table of contents from the given file, and update the disc
53 * accordingly. On error, set the err message and return FALSE.
55 gboolean (*read_toc)(cdrom_disc_t disc, ERROR *err);
57 } *cdrom_disc_factory_t;
60 * Low-level SCSI transport provided to the main SCSI/MMC driver. When used
61 * this will be set as the disc->impl_data field.
62 * Note: For symmetry there should be a packet_write variant, but we don't
63 * currently need it for anything. YAGNI, etc.
65 typedef struct cdrom_scsi_transport {
66 /* Execute a read command (ie a command that returns a block of data in
67 * response, not necessarily a CD read).
68 * @param disc The disc to execute the command
69 * @param cmd The 12-byte command packet
70 * @param buf The buffer to receive the read results
71 * @param length On entry, the size of buf. Modified on exit to the number
72 * of bytes actually read.
73 * @return PKT_ERR_OK on success, otherwise the host error code.
75 cdrom_error_t (*packet_read)( struct cdrom_disc *disc,
76 char *cmd, unsigned char *buf,
77 unsigned int *length );
79 /* Execute a generic command that does not write or return any data.
81 * @param scsi The disc to execute the command
82 * @param cmd The 12-byte command packet
83 * @return PKT_ERR_OK on success, otherwise the host error code.
85 cdrom_error_t (*packet_cmd)( struct cdrom_disc *disc,
88 /* Return TRUE if the media has changed since the last call, otherwise
89 * FALSE. This method is used to implement the disc-level check_status
90 * and should have no side-effects.
92 gboolean (*media_changed)( struct cdrom_disc *disc );
93 } *cdrom_scsi_transport_t;
95 #define SCSI_TRANSPORT(disc) ((cdrom_scsi_transport_t)disc->impl_data)
98 * Initialize a previously allocated cdrom_disc_t.
100 cdrom_disc_t cdrom_disc_init( cdrom_disc_t disc, const char *filename );
103 * Allocate and initialize a new cdrom_disc_t with the defaults for image files
105 cdrom_disc_t cdrom_disc_new( const char *name, ERROR *err );
108 * Read the table of contents from a scsi disc.
110 gboolean cdrom_disc_scsi_read_toc( cdrom_disc_t disc, ERROR *err );
113 * Allocate and initialize a new cdrom_disc_t using a scsi transport.
115 cdrom_disc_t cdrom_disc_scsi_new( const char *name, cdrom_scsi_transport_t transport, ERROR *err );
118 * Allocate and initialize a new cdrom_disc_t using a scsi transport and an
121 cdrom_disc_t cdrom_disc_scsi_new_file( FILE *f, const char *filename, cdrom_scsi_transport_t transport, ERROR *err );
124 void cdrom_disc_scsi_init( cdrom_disc_t disc, cdrom_scsi_transport_t scsi );
127 * Compute derived values for the TOC where they have not already been set
128 * - Determine disc leadout from end of the last track
129 * - Set the disc type to the based on the track types present.
131 void cdrom_disc_finalize_toc( cdrom_disc_t disc );
134 * Clear all TOC values in preparation for replacing with a new TOC
136 void cdrom_disc_clear_toc( cdrom_disc_t disc );
139 * Re-read the table of contents of the disc
141 gboolean cdrom_disc_read_toc( cdrom_disc_t disc, ERROR *err );
144 * track source for a host CD-ROM device, for use by host implementations
146 sector_source_t track_sector_source_new( cdrom_disc_t disc, sector_mode_t mode, cdrom_lba_t lba, cdrom_count_t count );
149 * Get the base file used by the cdrom, or NULL if there is no such file.
151 FILE *cdrom_disc_get_base_file( cdrom_disc_t disc );
153 #define cdrom_disc_get_base_fd(disc) fileno(cdrom_disc_get_base_file(disc))
156 * Default disc destructor method
158 void default_cdrom_disc_destroy( sector_source_t device );
160 /******************** Physical drive support *********************/
163 * Add a physical drive to the list.
164 * @return the new cdrom_drive_t entry. If the drive was already in the list,
165 * returns the existing entry instead and does not add a new one.
167 cdrom_drive_t cdrom_drive_add( const char *name, const char *display_name, cdrom_drive_open_fn_t open_fn );
170 * Remove a physical drive from the list, specified by name.
171 * @return TRUE if the drive was removed, FALSE if the drive was not in the list.
173 gboolean cdrom_drive_remove( const char *name );
176 * Clear the cdrom drive list.
178 void cdrom_drive_remove_all();
180 /************************* MMC support ***************************/
183 * Parse a standard MMC format-2 TOC into the disc structure.
185 void mmc_parse_toc2( cdrom_disc_t disc, unsigned char *buf );
188 * Read a standard MMC inquiry response, returning a newly allocated string
189 * of the form "<vendor> <product> <revision>"
191 const char *mmc_parse_inquiry( unsigned char *buf );
199 #endif /* !cdrom_cdimpl_H */
.