1 /*
2 Copyright (C) 2003-2005, 2008-2009, 2011-2012, 2016-2017
3 Rocky Bernstein <rocky@gnu.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* Internal routines for CD I/O drivers. */
20
21
22 #ifndef CDIO_DRIVER_PRIVATE_H_
23 #define CDIO_DRIVER_PRIVATE_H_
24
25 #if defined(HAVE_CONFIG_H) && !defined(LIBCDIO_CONFIG_H)
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
35
36 #include <cdio/cdio.h>
37 #include <cdio/audio.h>
38 #include <cdio/cdtext.h>
39 #include "mmc/mmc_private.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif /* __cplusplus */
44
45 #ifndef HAVE_STRNDUP
46 #undef strndup
47 #define strndup libcdio_strndup
libcdio_strndup(const char * s,size_t n)48 static inline char *libcdio_strndup(const char *s, size_t n)
49 {
50 char *result;
51 size_t len = strlen (s);
52 if (n < len)
53 len = n;
54 result = (char *) malloc (len + 1);
55 if (!result)
56 return 0;
57 result[len] = '\0';
58 return (char *) strncpy (result, s, len);
59 }
60 #endif /*HAVE_STRNDUP*/
61
62 /*!
63 Get directory name from file name.
64
65 Callers must free return value after use.
66 */
67 extern char *cdio_dirname(const char *fname);
68
69 /*!
70 Construct an absolute file name from path and file name.
71
72 Callers must free return value after use.
73 */
74 extern char *cdio_abspath(const char *cwd, const char *fname);
75
76 /* Opaque type */
77 typedef struct _CdioDataSource CdioDataSource_t;
78
79 #ifdef __cplusplus
80 }
81
82 #endif /* __cplusplus */
83
84 #include "generic.h"
85
86 #ifdef __cplusplus
87 extern "C" {
88 #endif /* __cplusplus */
89
90
91 typedef struct {
92
93 /*!
94 Get volume of an audio CD.
95
96 @param p_env the CD object to be acted upon.
97
98 */
99 driver_return_code_t (*audio_get_volume)
100 (void *p_env, /*out*/ cdio_audio_volume_t *p_volume);
101
102 /*!
103 Pause playing CD through analog output
104
105 @param p_env the CD object to be acted upon.
106 */
107 driver_return_code_t (*audio_pause) (void *p_env);
108
109 /*!
110 Playing CD through analog output
111
112 @param p_env the CD object to be acted upon.
113 */
114 driver_return_code_t (*audio_play_msf) ( void *p_env,
115 msf_t *p_start_msf,
116 msf_t *p_end_msf );
117
118 /*!
119 Playing CD through analog output
120
121 @param p_env the CD object to be acted upon.
122 */
123 driver_return_code_t (*audio_play_track_index)
124 ( void *p_env, cdio_track_index_t *p_track_index );
125
126 /*!
127 Get subchannel information.
128
129 @param p_env the CD object to be acted upon.
130 */
131 driver_return_code_t (*audio_read_subchannel)
132 ( void *p_env, cdio_subchannel_t *subchannel );
133
134 /*!
135 Resume playing an audio CD.
136
137 @param p_env the CD object to be acted upon.
138
139 */
140 driver_return_code_t (*audio_resume) ( void *p_env );
141
142 /*!
143 Set volume of an audio CD.
144
145 @param p_env the CD object to be acted upon.
146
147 */
148 driver_return_code_t (*audio_set_volume)
149 ( void *p_env, cdio_audio_volume_t *p_volume );
150
151 /*!
152 Stop playing an audio CD.
153
154 @param p_env the CD object to be acted upon.
155
156 */
157 driver_return_code_t (*audio_stop) ( void *p_env );
158
159 /*!
160 Eject media in CD drive. If successful, as a side effect we
161 also free p_env.
162
163 @param p_env the CD object to be acted upon.
164 If the CD is ejected *p_env is freed and p_env set to NULL.
165 */
166 driver_return_code_t (*eject_media) ( void *p_env );
167
168 /*!
169 Release and free resources associated with cd.
170 */
171 void (*free) (void *p_env);
172
173 /*!
174 Return the value associated with the key "arg".
175 */
176 const char * (*get_arg) (void *p_env, const char key[]);
177
178 /*!
179 Get the block size for subsequest read requests, via a SCSI MMC
180 MODE_SENSE 6 command.
181 */
182 int (*get_blocksize) ( void *p_env );
183
184 /*!
185 Get cdtext information for a CdIo object.
186
187 @param obj the CD object that may contain CD-TEXT information.
188 @return the CD-TEXT object or NULL if obj is NULL
189 or CD-TEXT information does not exist.
190 */
191 cdtext_t * (*get_cdtext) ( void *p_env );
192
193 /*!
194 Get raw cdtext information as on the disc for a CdIo object
195
196 @param obj the CD object that may contain CD-TEXT information.
197 @return pointer to the raw CD-TEXT data or NULL if obj is NULL
198 or no CD-TEXT information present on the disc.
199
200 free when done and not NULL.
201 */
202 uint8_t * (*get_cdtext_raw) ( void *p_env );
203
204 /*!
205 Return an array of device names. if CdIo is NULL (we haven't
206 initialized a specific device driver), then find a suitable device
207 driver.
208
209 NULL is returned if we couldn't return a list of devices.
210 */
211 char ** (*get_devices) ( void );
212
213 /*!
214 Get the default CD device.
215
216 @return a string containing the default CD device or NULL is
217 if we couldn't get a default device.
218
219 In some situations of drivers or OS's we can't find a CD device if
220 there is no media in it and it is possible for this routine to return
221 NULL even though there may be a hardware CD-ROM.
222 */
223 char * (*get_default_device) ( void );
224
225 /*!
226 Return the size of the CD in logical block address (LBA) units.
227 @return the lsn. On error 0 or CDIO_INVALD_LSN.
228 */
229 lsn_t (*get_disc_last_lsn) ( void *p_env );
230
231 /*!
232 Get disc mode associated with cd_obj.
233 */
234 discmode_t (*get_discmode) ( void *p_env );
235
236 /*!
237 Return the what kind of device we've got.
238
239 See cd_types.h for a list of bitmasks for the drive type;
240 */
241 void (*get_drive_cap) (const void *p_env,
242 cdio_drive_read_cap_t *p_read_cap,
243 cdio_drive_write_cap_t *p_write_cap,
244 cdio_drive_misc_cap_t *p_misc_cap);
245 /*!
246 Return the number of of the first track.
247 CDIO_INVALID_TRACK is returned on error.
248 */
249 track_t (*get_first_track_num) ( void *p_env );
250
251 /*!
252 Get the CD-ROM hardware info via a SCSI MMC INQUIRY command.
253 False is returned if we had an error getting the information.
254 */
255 bool (*get_hwinfo)
256 ( const CdIo_t *p_cdio, /* out*/ cdio_hwinfo_t *p_hw_info );
257
258 /*! Get the LSN of the first track of the last session of
259 on the CD.
260
261 @param p_cdio the CD object to be acted upon.
262 @param i_last_session pointer to the session number to be returned.
263 */
264 driver_return_code_t (*get_last_session)
265 ( void *p_env, /*out*/ lsn_t *i_last_session );
266
267 /*!
268 Find out if media has changed since the last call.
269 @param p_env the CD object to be acted upon.
270 @return 1 if media has changed since last call, 0 if not. Error
271 return codes are the same as driver_return_code_t
272 */
273 int (*get_media_changed) ( const void *p_env );
274
275 /*!
276 Return the media catalog number MCN from the CD or NULL if
277 there is none or we don't have the ability to get it.
278 */
279 char * (*get_mcn) ( const void *p_env );
280
281 /*!
282 Return the number of tracks in the current medium.
283 CDIO_INVALID_TRACK is returned on error.
284 */
285 track_t (*get_num_tracks) ( void *p_env );
286
287 /*! Return number of channels in track: 2 or 4; -2 if not
288 implemented or -1 for error.
289 Not meaningful if track is not an audio track.
290 */
291 int (*get_track_channels) ( const void *p_env, track_t i_track );
292
293 /*! Return 0 if track is copy protected, 1 if not, or -1 for error
294 or -2 if not implimented (yet). Is this meaningful if not an
295 audio track?
296 */
297 track_flag_t (*get_track_copy_permit) ( void *p_env, track_t i_track );
298
299 /*!
300 Return the starting LBA for track number
301 i_track in p_env. Tracks numbers start at 1.
302 The "leadout" track is specified either by
303 using track_num LEADOUT_TRACK or the total tracks+1.
304 CDIO_INVALID_LBA is returned on error.
305 */
306 lba_t (*get_track_lba) ( void *p_env, track_t i_track );
307
308 /*!
309 Return the starting LBA for the pregap for track number
310 i_track in p_env. Tracks numbers start at 1.
311 CDIO_INVALID_LBA is returned on error.
312 */
313 lba_t (*get_track_pregap_lba) ( const void *p_env, track_t i_track );
314
315 /*!
316 Return the International Standard Recording Code (ISRC) for track number
317 i_track in p_cdio. Track numbers start at 1.
318
319 Note: string is malloc'd so caller has to free() the returned
320 string when done with it.
321 */
322 char * (*get_track_isrc) ( const void *p_env, track_t i_track );
323
324 /*!
325 Get format of track.
326 */
327 track_format_t (*get_track_format) ( void *p_env, track_t i_track );
328
329 /*!
330 Return true if we have XA data (green, mode2 form1) or
331 XA data (green, mode2 form2). That is track begins:
332 sync - header - subheader
333 12 4 - 8
334
335 FIXME: there's gotta be a better design for this and get_track_format?
336 */
337 bool (*get_track_green) ( void *p_env, track_t i_track );
338
339 /*!
340 Return the starting MSF (minutes/secs/frames) for track number
341 i_track in p_env. Tracks numbers start at 1.
342 The "leadout" track is specified either by
343 using i_track LEADOUT_TRACK or the total tracks+1.
344 False is returned on error.
345 */
346 bool (*get_track_msf) ( void *p_env, track_t i_track, msf_t *p_msf );
347
348 /*! Return 1 if track has pre-emphasis, 0 if not, or -1 for error
349 or -2 if not implimented (yet). Is this meaningful if not an
350 audio track?
351 */
352 track_flag_t (*get_track_preemphasis)
353 ( const void *p_env, track_t i_track );
354
355 /*!
356 lseek - reposition read/write file offset
357 Returns (off_t) -1 on error.
358 Similar to libc's lseek()
359 */
360 off_t (*lseek) ( void *p_env, off_t offset, int whence );
361
362 /*!
363 Reads into buf the next size bytes.
364 Returns -1 on error.
365 Similar to libc's read()
366 */
367 ssize_t (*read) ( void *p_env, void *p_buf, size_t i_size );
368
369 /*!
370 Reads a single mode2 sector from cd device into buf starting
371 from lsn. Returns 0 if no error.
372 */
373 int (*read_audio_sectors) ( void *p_env, void *p_buf, lsn_t i_lsn,
374 unsigned int i_blocks );
375
376 /*!
377 Read a data sector
378
379 @param p_env environment to read from
380
381 @param p_buf place to read data into. The caller should make sure
382 this location can store at least CDIO_CD_FRAMESIZE,
383 M2RAW_SECTOR_SIZE, or M2F2_SECTOR_SIZE depending
384 on the kind of sector getting read. If you don't
385 know whether you have a Mode 1/2, Form 1/ Form 2/Formless
386 sector best to reserve space for the maximum,
387 M2RAW_SECTOR_SIZE.
388
389 @param i_lsn sector to read
390 @param i_blocksize size of block. Should be either CDIO_CD_FRAMESIZE,
391 M2RAW_SECTOR_SIZE, or M2F2_SECTOR_SIZE. See comment above under p_buf.
392 */
393 driver_return_code_t (*read_data_sectors)
394 ( void *p_env, void *p_buf, lsn_t i_lsn, uint16_t i_blocksize,
395 uint32_t i_blocks );
396
397 /*!
398 Reads a single mode2 sector from cd device into buf starting
399 from lsn. Returns 0 if no error.
400 */
401 int (*read_mode2_sector)
402 ( void *p_env, void *p_buf, lsn_t i_lsn, bool b_mode2_form2 );
403
404 /*!
405 Reads i_blocks of mode2 sectors from cd device into data starting
406 from lsn.
407 Returns 0 if no error.
408 */
409 int (*read_mode2_sectors)
410 ( void *p_env, void *p_buf, lsn_t i_lsn, bool b_mode2_form2,
411 unsigned int i_blocks );
412
413 /*!
414 Reads a single mode1 sector from cd device into buf starting
415 from lsn. Returns 0 if no error.
416 */
417 int (*read_mode1_sector)
418 ( void *p_env, void *p_buf, lsn_t i_lsn, bool mode1_form2 );
419
420 /*!
421 Reads i_blocks of mode1 sectors from cd device into data starting
422 from lsn.
423 Returns 0 if no error.
424 */
425 int (*read_mode1_sectors)
426 ( void *p_env, void *p_buf, lsn_t i_lsn, bool mode1_form2,
427 unsigned int i_blocks );
428
429 bool (*read_toc) ( void *p_env ) ;
430
431 /*!
432 Run a SCSI MMC command.
433
434 cdio CD structure set by cdio_open().
435 i_timeout_ms time in milliseconds we will wait for the command
436 to complete.
437 cdb_len number of bytes in cdb (6, 10, or 12).
438 cdb CDB bytes. All values that are needed should be set on
439 input.
440 b_return_data TRUE if the command expects data to be returned in
441 the buffer
442 len Size of buffer
443 buf Buffer for data, both sending and receiving
444
445 Returns 0 if command completed successfully.
446 */
447 mmc_run_cmd_fn_t run_mmc_cmd;
448
449 /*!
450 Set the arg "key" with "value" in the source device.
451 */
452 int (*set_arg) ( void *p_env, const char key[], const char value[] );
453
454 /*!
455 Set the blocksize for subsequent reads.
456 */
457 driver_return_code_t (*set_blocksize) ( void *p_env,
458 uint16_t i_blocksize );
459
460 /*!
461 Set the drive speed.
462
463 @return 0 if everything went okay, -1 if we had an error. is -2
464 returned if this is not implemented for the current driver.
465 */
466 int (*set_speed) ( void *p_env, int i_speed );
467
468 } cdio_funcs_t;
469
470
471 /*! Implementation of CdIo type */
472 struct _CdIo {
473 driver_id_t driver_id; /**< Particular driver opened. */
474 cdio_funcs_t op; /**< driver-specific routines handling
475 implementation*/
476 void *env; /**< environment. Passed to routine above. */
477 };
478
479 /* This is used in drivers that must keep their own internal
480 position pointer for doing seeks. Stream-based drivers (like bincue,
481 nrg, toc, network) would use this.
482 */
483 typedef struct
484 {
485 off_t buff_offset; /* buffer offset in disk-image seeks. */
486 track_t index; /* Current track index in tocent. */
487 lba_t lba; /* Current LBA */
488 } internal_position_t;
489
490 CdIo_t * cdio_new (generic_img_private_t *p_env, cdio_funcs_t *p_funcs);
491
492 /* The below structure describes a specific CD Input driver */
493 typedef struct
494 {
495 driver_id_t id;
496 unsigned int flags;
497 const char *name;
498 const char *describe;
499 bool (*have_driver) (void);
500 CdIo_t *(*driver_open) (const char *psz_source_name);
501 CdIo_t *(*driver_open_am) (const char *psz_source_name,
502 const char *psz_access_mode);
503 char *(*get_default_device) (void);
504 bool (*is_device) (const char *psz_source_name);
505 char **(*get_devices) (void);
506 driver_return_code_t (*close_tray) (const char *psz_device);
507 } CdIo_driver_t;
508
509 /* The below array gives of the drivers that are currently available for
510 on a particular host. */
511 extern CdIo_driver_t CdIo_driver[];
512
513 /* The last valid entry of Cdio_driver. -1 means uninitialzed. -2
514 means some sort of error.
515 */
516 extern int CdIo_last_driver;
517
518 /* The below array gives all drivers that can possibly appear.
519 on a particular host. */
520 extern CdIo_driver_t CdIo_all_drivers[];
521
522 /*!
523 Add/allocate a drive to the end of drives.
524 Use cdio_free_device_list() to free this device_list.
525 */
526 void cdio_add_device_list(char **device_list[], const char *psz_drive,
527 unsigned int *i_drives);
528
529 driver_return_code_t close_tray_bsdi (const char *psz_drive);
530 driver_return_code_t close_tray_freebsd (const char *psz_drive);
531 driver_return_code_t close_tray_linux (const char *psz_drive);
532 driver_return_code_t close_tray_netbsd (const char *psz_drive);
533 driver_return_code_t close_tray_osx (const char *psz_drive);
534 driver_return_code_t close_tray_solaris (const char *psz_drive);
535 driver_return_code_t close_tray_win32 (const char *psz_drive);
536
537 bool cdio_have_netbsd(void);
538 CdIo_t * cdio_open_netbsd (const char *psz_source);
539 char * cdio_get_default_device_netbsd(void);
540 char **cdio_get_devices_netbsd(void);
541 /*! Set up CD-ROM for reading using the NetBSD driver. The device_name is
542 the some sort of device name.
543
544 NULL is returned on error or there is no FreeBSD driver.
545
546 @see cdio_open_cd, cdio_open
547 */
548 CdIo_t * cdio_open_am_netbsd (const char *psz_source,
549 const char *psz_access_mode);
550
551 /*! DEPRICATED: use cdio_have_driver().
552 True if AIX driver is available. */
553 bool cdio_have_aix (void);
554
555 /*! DEPRICATED: use cdio_have_driver().
556 True if BSDI driver is available. */
557 bool cdio_have_bsdi (void);
558
559 /*! DEPRICATED: use cdio_have_driver().
560 True if FreeBSD driver is available. */
561 bool cdio_have_freebsd (void);
562
563 /*! DEPRICATED: use cdio_have_driver().
564 True if GNU/Linux driver is available. */
565 bool cdio_have_linux (void);
566
567 /*! DEPRICATED: use cdio_have_driver().
568 True if Sun Solaris driver is available. */
569 bool cdio_have_solaris (void);
570
571 /*! DEPRICATED: use cdio_have_driver().
572 True if IBM OS2 driver is available. */
573 bool cdio_have_os2 (void);
574
575 /*! DEPRICATED: use cdio_have_driver().
576 True if Apple OSX driver is available. */
577 bool cdio_have_osx (void);
578
579 /*! DEPRICATED: use cdio_have_driver().
580 True if Microsoft Windows driver is available. */
581 bool cdio_have_win32 (void);
582
583 /*! True if Nero driver is available. */
584 bool cdio_have_nrg (void);
585
586 /*! True if BIN/CUE driver is available. */
587 bool cdio_have_bincue (void);
588
589 /*! True if cdrdao CDRDAO driver is available. */
590 bool cdio_have_cdrdao (void);
591
592 #ifdef __cplusplus
593 }
594 #endif /* __cplusplus */
595
596 #endif /* CDIO_DRIVER_PRIVATE_H_ */
597