1 /*
2     Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012
3                   2016, 2019 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 /**
20    \file mmc.h
21 
22    \brief Common definitions for MMC (Multimedia Commands). Applications
23    include this for direct MMC access.
24 
25    The documents we make use of are described in several
26    specifications made by the SCSI committee T10
27    http://www.t10.org. In particular, SCSI Primary Commands (SPC),
28    SCSI Block Commands (SBC), and Multi-Media Commands (MMC). These
29    documents generally have a numeric level number appended. For
30    example SPC-3 refers to ``SCSI Primary Commands - 3'.
31 
32    In year 2010 the current versions were SPC-3, SBC-2, MMC-5.
33 
34 */
35 
36 #ifndef CDIO_MMC_H_
37 #define CDIO_MMC_H_
38 
39 #include <cdio/cdio.h>
40 #include <cdio/types.h>
41 #include <cdio/dvd.h>
42 #include <cdio/audio.h>
43 #include <cdio/mmc_util.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif /* __cplusplus */
48 
49 /* On GNU/Linux see <linux/byteorder/big_endian.h> and
50    <linux/byteorder/little_endian.h>
51 */
52 #ifdef WORDS_BIGENDIAN
53 #  if !defined(__LITTLE_ENDIAN_BITFIELD) && !defined(__BIG_ENDIAN_BITFIELD)
54 #  define __MMC_BIG_ENDIAN_BITFIELD
55 #  endif
56 #else
57 #  if !defined(__LITTLE_ENDIAN_BITFIELD) && !defined(__BIG_ENDIAN_BITFIELD)
58 #  define __MMC_LITTLE_ENDIAN_BITFIELD
59 #  endif
60 #endif
61 
62    /**
63       Structure of a SCSI/MMC sense reply.
64 
65       This has been adapted from GNU/Linux request_sense of <linux/cdrom.h>
66       include this for direct MMC access.
67       See SCSI Primary Commands-2 (SPC-3) table 26 page 38.
68     */
69     typedef struct cdio_mmc_request_sense {
70 #if defined(__MMC_BIG_ENDIAN_BITFIELD)
71         uint8_t valid           : 1;  /**< valid bit is 1 if info is valid */
72         uint8_t error_code      : 7;
73 #else
74         uint8_t error_code      : 7;
75         uint8_t valid           : 1;  /**< valid bit is 1 if info is valid */
76 #endif
77         uint8_t segment_number;
78 #if defined(__MMC_BIG_ENDIAN_BITFIELD)
79         uint8_t filemark        : 1; /**< manditory in sequential
80                                       * access devices */
81         uint8_t eom             : 1; /**< end of medium. manditory in
82                                       * sequential access and
83                                       * printer devices */
84         uint8_t ili             : 1; /**< incorrect length indicator */
85         uint8_t reserved1       : 1;
86         uint8_t sense_key       : 4;
87 #else
88         uint8_t sense_key       : 4;
89         uint8_t reserved1       : 1;
90         uint8_t ili             : 1; /**< incorrect length indicator */
91         uint8_t eom             : 1; /**< end of medium. manditory in
92                                       * sequential access and
93                                       * printer devices */
94         uint8_t filemark        : 1; /**< manditory in sequential
95                                       * access devices */
96 #endif
97         uint8_t information[4];
98         uint8_t additional_sense_len; /**< Additional sense length (n-7) */
99         uint8_t command_info[4];      /**< Command-specific information */
100         uint8_t asc;                  /**< Additional sense code */
101         uint8_t ascq;                 /**< Additional sense code qualifier */
102         uint8_t fruc;                 /**< Field replaceable unit code */
103         uint8_t sks[3];               /**< Sense-key specific */
104         uint8_t asb[46];              /**< Additional sense bytes */
105     } cdio_mmc_request_sense_t;
106 
107 
108     /**
109        Meanings of the values of mmc_request_sense.sense_key
110      */
111     typedef enum {
112         CDIO_MMC_SENSE_KEY_NO_SENSE        =  0,
113         CDIO_MMC_SENSE_KEY_RECOVERED_ERROR =  1,
114         CDIO_MMC_SENSE_KEY_NOT_READY       =  2,
115         CDIO_MMC_SENSE_KEY_MEDIUM_ERROR    =  3,
116         CDIO_MMC_SENSE_KEY_HARDWARE_ERROR  =  4,
117         CDIO_MMC_SENSE_KEY_ILLEGAL_REQUEST =  5,
118         CDIO_MMC_SENSE_KEY_UNIT_ATTENTION  =  6,
119         CDIO_MMC_SENSE_KEY_DATA_PROTECT    =  7,
120         CDIO_MMC_SENSE_KEY_BLANK_CHECK     =  8,
121         CDIO_MMC_SENSE_KEY_VENDOR_SPECIFIC =  9,
122         CDIO_MMC_SENSE_KEY_COPY_ABORTED    = 10,
123         CDIO_MMC_SENSE_KEY_ABORTED_COMMAND = 11,
124         CDIO_MMC_SENSE_KEY_OBSOLETE        = 12,
125     } cdio_mmc_sense_key_t;
126 
127     /**
128        \brief The opcode-portion (generic packet commands) of an MMC command.
129 
130        In general, those opcodes that end in 6 take a 6-byte command
131        descriptor, those that end in 10 take a 10-byte
132        descriptor and those that in in 12 take a 12-byte descriptor.
133 
134        (Not that you need to know that, but it seems to be a
135        big deal in the MMC specification.)
136 
137     */
138     typedef enum {
139   CDIO_MMC_GPCMD_TEST_UNIT_READY        = 0x00, /**< test if drive ready. */
140   CDIO_MMC_GPCMD_REQUEST_SENSE          = 0x03,
141   CDIO_MMC_GPCMD_FORMAT_UNIT            = 0x04,
142   CDIO_MMC_GPCMD_INQUIRY                = 0x12, /**< Request drive
143                                                    information. */
144   CDIO_MMC_GPCMD_MODE_SELECT_6          = 0x15, /**< Select medium
145                                                    (6 bytes). */
146   CDIO_MMC_GPCMD_MODE_SENSE_6           = 0x1a, /**< Get medium or device
147                                                  information. Should be issued
148                                                  before MODE SELECT to get
149                                                  mode support or save current
150                                                  settings. (6 bytes). */
151   CDIO_MMC_GPCMD_START_STOP_UNIT        = 0x1b, /**< Enable/disable Disc
152                                                      operations. (6 bytes). */
153   CDIO_MMC_GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
154                                         = 0x1e, /**< Enable/disable Disc
155                                                    removal. (6 bytes). */
156 
157   /**
158       Group 2 Commands (CDB's here are 10-bytes)
159   */
160   CDIO_MMC_GPCMD_READ_FORMAT_CAPACITIES = 0x23, /**< MMC-5 Section 6.24 */
161   CDIO_MMC_GPCMD_READ_CAPACITIY         = 0x25, /**< MMC-5 Section 6.19 */
162   CDIO_MMC_GPCMD_READ_10                = 0x28, /**< Read data from drive
163                                                    (10 bytes). */
164   CDIO_MMC_GPCMD_WRITE_10               = 0x2a,
165   CDIO_MMC_GPCMD_SEEK_10                = 0x2b,
166   CDIO_MMC_GPCMD_ERASE_10               = 0x2c, /**< MMC5 Section 6.4 */
167   CDIO_MMC_GPCMD_WRITE_AND_VERIFY_10    = 0x2e,
168   CDIO_MMC_GPCMD_VERIFY_10              = 0x2f,
169   CDIO_MMC_GPCMD_SYNCHRONIZE_CACHE      = 0x35,
170   CDIO_MMC_GPCMD_WRITE_BUFFER           = 0x3b,
171   CDIO_MMC_GPCMD_READ_BUFFER            = 0x3c,
172   CDIO_MMC_GPCMD_READ_SUBCHANNEL        = 0x42, /**< Read Sub-Channel data.
173                                                    (10 bytes). */
174   CDIO_MMC_GPCMD_READ_TOC               = 0x43, /**< READ TOC/PMA/ATIP.
175                                                    (10 bytes). */
176   CDIO_MMC_GPCMD_READ_HEADER            = 0x44,
177   CDIO_MMC_GPCMD_PLAY_AUDIO_10          = 0x45, /**< Begin audio playing at
178                                                    current position
179                                                    (10 bytes). */
180   CDIO_MMC_GPCMD_GET_CONFIGURATION      = 0x46, /**< Get drive Capabilities
181                                                    (10 bytes) */
182   CDIO_MMC_GPCMD_PLAY_AUDIO_MSF         = 0x47, /**< Begin audio playing at
183                                                    specified MSF (10
184                                                    bytes). */
185   CDIO_MMC_GPCMD_PLAY_AUDIO_TI          = 0x48,
186   CDIO_MMC_GPCMD_PLAY_TRACK_REL_10      = 0x49, /**< Play audio at the track
187                                                    relative LBA. (10 bytes).
188                                                    Doesn't seem to be part
189                                                    of MMC standards but is
190                                                    handled by Plextor drives.
191                                                 */
192 
193   CDIO_MMC_GPCMD_GET_EVENT_STATUS       = 0x4a, /**< Report events and
194                                                    Status. */
195   CDIO_MMC_GPCMD_PAUSE_RESUME           = 0x4b, /**< Stop or restart audio
196                                                    playback. (10 bytes).
197                                                    Used with a PLAY command. */
198 
199   CDIO_MMC_GPCMD_READ_DISC_INFORMATION  = 0x51, /**< Get CD information.
200                                                    (10 bytes). */
201   CDIO_MMC_GPCMD_READ_TRACK_INFORMATION = 0x52, /**< Information about a
202                                                    logical track. */
203   CDIO_MMC_GPCMD_RESERVE_TRACK          = 0x53,
204   CDIO_MMC_GPCMD_SEND_OPC_INFORMATION   = 0x54,
205   CDIO_MMC_GPCMD_MODE_SELECT_10         = 0x55, /**< Select medium
206                                                    (10-bytes). */
207   CDIO_MMC_GPCMD_REPAIR_TRACK           = 0x58,
208   CDIO_MMC_GPCMD_MODE_SENSE_10          = 0x5a, /**< Get medium or device
209                                                  information. Should be issued
210                                                  before MODE SELECT to get
211                                                  mode support or save current
212                                                  settings. (6 bytes). */
213   CDIO_MMC_GPCMD_CLOSE_TRACK_SESSION    = 0x5b,
214   CDIO_MMC_GPCMD_READ_BUFFER_CAPACITY   = 0x5c,
215   CDIO_MMC_GPCMD_SEND_CUE_SHEET         = 0x5d,
216 
217   /**
218       Group 5 Commands (CDB's here are 12-bytes)
219   */
220   CDIO_MMC_GPCMD_REPORT_LUNS            = 0xa0,
221   CDIO_MMC_GPCMD_BLANK                  = 0xa1,
222   CDIO_MMC_GPCMD_SECURITY_PROTOCOL_IN   = 0xa2,
223   CDIO_MMC_GPCMD_SEND_KEY               = 0xa3,
224   CDIO_MMC_GPCMD_REPORT_KEY             = 0xa4,
225   CDIO_MMC_GPCMD_PLAY_AUDIO_12          = 0xa5, /**< Begin audio playing at
226                                                    current position
227                                                  (12 bytes) */
228   CDIO_MMC_GPCMD_LOAD_UNLOAD            = 0xa6, /**< Load/unload a Disc
229                                                  (12 bytes) */
230   CDIO_MMC_GPCMD_SET_READ_AHEAD         = 0xa7,
231   CDIO_MMC_GPCMD_READ_12                = 0xa8, /**< Read data from drive
232                                                    (12 bytes). */
233   CDIO_MMC_GPCMD_PLAY_TRACK_REL_12      = 0xa9, /**< Play audio at the track
234                                                    relative LBA. (12 bytes).
235                                                    Doesn't seem to be part
236                                                    of MMC standards but is
237                                                    handled by Plextor drives.
238                                                 */
239   CDIO_MMC_GPCMD_WRITE_12               = 0xaa,
240   CDIO_MMC_GPCMD_READ_MEDIA_SERIAL_12   = 0xab, /**< MMC-5 Section 6.25 */
241   CDIO_MMC_GPCMD_GET_PERFORMANCE        = 0xac,
242   CDIO_MMC_GPCMD_READ_DVD_STRUCTURE     = 0xad, /**< Get DVD structure info
243                                                    from media (12 bytes). */
244   CDIO_MMC_GPCMD_SECURITY_PROTOCOL_OUT  = 0xb5,
245   CDIO_MMC_GPCMD_SET_STREAMING          = 0xb6,
246   CDIO_MMC_GPCMD_READ_MSF               = 0xb9, /**< Read almost any field
247                                                    of a CD sector at specified
248                                                    MSF. (12 bytes). */
249   CDIO_MMC_GPCMD_SET_SPEED              = 0xbb, /**< Set drive speed
250                                                    (12 bytes). This is listed
251                                                    as optional in ATAPI 2.6,
252                                                    but is (curiously)
253                                                    missing from Mt. Fuji,
254                                                    Table 57. It is mentioned
255                                                    in Mt. Fuji Table 377 as an
256                                                    MMC command for SCSI
257                                                    devices though...  Most
258                                                    ATAPI drives support it. */
259   CDIO_MMC_GPCMD_MECHANISM_STATUS       = 0xbd,
260   CDIO_MMC_GPCMD_READ_CD                = 0xbe, /**< Read almost any field
261                                                    of a CD sector at current
262                                                    location. (12 bytes). */
263   CDIO_MMC_GPCMD_SEND_DISC_STRUCTURE    = 0xbf,
264   /**
265       Vendor-unique Commands
266   */
267   CDIO_MMC_GPCMD_CD_PLAYBACK_STATUS  = 0xc4 /**< SONY unique  = command */,
268   CDIO_MMC_GPCMD_PLAYBACK_CONTROL    = 0xc9 /**< SONY unique  = command */,
269   CDIO_MMC_GPCMD_READ_CDDA           = 0xd8 /**< Vendor unique  = command */,
270   CDIO_MMC_GPCMD_READ_CDXA           = 0xdb /**< Vendor unique  = command */,
271   CDIO_MMC_GPCMD_READ_ALL_SUBCODES   = 0xdf /**< Vendor unique  = command */
272   } cdio_mmc_gpcmd_t;
273 
274   /**
275       Read Subchannel states
276   */
277   typedef enum {
278     CDIO_MMC_READ_SUB_ST_INVALID   = 0x00, /**< audio status not supported */
279     CDIO_MMC_READ_SUB_ST_PLAY      = 0x11, /**< audio play operation in
280                                               progress */
281     CDIO_MMC_READ_SUB_ST_PAUSED    = 0x12, /**< audio play operation paused */
282     CDIO_MMC_READ_SUB_ST_COMPLETED = 0x13, /**< audio play successfully
283                                               completed */
284     CDIO_MMC_READ_SUB_ST_ERROR     = 0x14, /**< audio play stopped due to
285                                               error */
286     CDIO_MMC_READ_SUB_ST_NO_STATUS = 0x15, /**< no current audio status to
287                                               return */
288   } cdio_mmc_read_sub_state_t;
289 
290   /** Level values that can go into READ_CD */
291   typedef enum {
292     CDIO_MMC_READ_TYPE_ANY  =  0,  /**< All types */
293     CDIO_MMC_READ_TYPE_CDDA =  1,  /**< Only CD-DA sectors */
294     CDIO_MMC_READ_TYPE_MODE1 = 2,  /**< mode1 sectors (user data = 2048) */
295     CDIO_MMC_READ_TYPE_MODE2 = 3,  /**< mode2 sectors form1 or form2 */
296     CDIO_MMC_READ_TYPE_M2F1 =  4,  /**< mode2 sectors form1 */
297     CDIO_MMC_READ_TYPE_M2F2 =  5   /**< mode2 sectors form2 */
298   } cdio_mmc_read_cd_type_t;
299 
300   /**
301       Format values for READ_TOC
302   */
303   typedef enum {
304     CDIO_MMC_READTOC_FMT_TOC     =  0,
305     CDIO_MMC_READTOC_FMT_SESSION =  1,
306     CDIO_MMC_READTOC_FMT_FULTOC  =  2,
307     CDIO_MMC_READTOC_FMT_PMA     =  3,  /**< Q subcode data */
308     CDIO_MMC_READTOC_FMT_ATIP    =  4,  /**< includes media type */
309     CDIO_MMC_READTOC_FMT_CDTEXT  =  5   /**< CD-TEXT info  */
310   } cdio_mmc_readtoc_t;
311 
312   /**
313      Page codes for MODE SENSE and MODE SET.
314   */
315   typedef enum {
316       CDIO_MMC_R_W_ERROR_PAGE     = 0x01,
317       CDIO_MMC_WRITE_PARMS_PAGE   = 0x05,
318       CDIO_MMC_CDR_PARMS_PAGE     = 0x0d,
319       CDIO_MMC_AUDIO_CTL_PAGE     = 0x0e,
320       CDIO_MMC_POWER_PAGE         = 0x1a,
321       CDIO_MMC_FAULT_FAIL_PAGE    = 0x1c,
322       CDIO_MMC_TO_PROTECT_PAGE    = 0x1d,
323       CDIO_MMC_CAPABILITIES_PAGE  = 0x2a,
324       CDIO_MMC_ALL_PAGES          = 0x3f,
325   } cdio_mmc_mode_page_t;
326 
327  /**
328     READ DISC INFORMATION Data Types
329  */
330   typedef enum {
331       CDIO_MMC_READ_DISC_INFO_STANDARD   = 0x0,
332       CDIO_MMC_READ_DISC_INFO_TRACK      = 0x1,
333       CDIO_MMC_READ_DISC_INFO_POW        = 0x2,
334   } cdio_mmc_read_disc_info_datatype_t;
335 
336 /* For backward compatibility. */
337 #define CDIO_MMC_GPCMD_READ_DISC_INFO CDIO_MMC_GPCMD_READ_DISC_INFORMATION
338 #define CDIO_MMC_GPCMD_READ_DISC_STRUCTURE CDIO_MMC_GPMD_READ_DVD_STRUCTURE
339 
340 
341 PRAGMA_BEGIN_PACKED
342   struct mmc_audio_volume_entry_s
343   {
344     uint8_t  selection; /* Only the lower 4 bits are used. */
345     uint8_t  volume;
346   } GNUC_PACKED;
347 
348   typedef struct mmc_audio_volume_entry_s mmc_audio_volume_entry_t;
349 
350   /**
351       This struct is used by cdio_audio_get_volume and cdio_audio_set_volume
352   */
353   struct mmc_audio_volume_s
354   {
355     mmc_audio_volume_entry_t port[4];
356   } GNUC_PACKED;
357 
358   typedef struct mmc_audio_volume_s mmc_audio_volume_t;
359 
360 PRAGMA_END_PACKED
361 
362 
363 /**
364     Return type codes for GET_CONFIGURATION.
365 */
366 typedef enum {
367   CDIO_MMC_GET_CONF_ALL_FEATURES     = 0,  /**< all features without regard
368                                               to currency. */
369   CDIO_MMC_GET_CONF_CURRENT_FEATURES = 1,  /**< features which are currently
370                                               in effect (e.g. based on
371                                               medium inserted). */
372   CDIO_MMC_GET_CONF_NAMED_FEATURE    = 2   /**< just the feature named in
373                                               the GET_CONFIGURATION cdb. */
374 } cdio_mmc_get_conf_t;
375 
376 
377 /**
378     FEATURE codes used in GET CONFIGURATION.
379 */
380 typedef enum {
381   CDIO_MMC_FEATURE_PROFILE_LIST     = 0x000, /**< Profile List Feature */
382   CDIO_MMC_FEATURE_CORE             = 0x001,
383   CDIO_MMC_FEATURE_MORPHING         = 0x002, /**< Report/prevent operational
384                                                   changes  */
385   CDIO_MMC_FEATURE_REMOVABLE_MEDIUM = 0x003, /**< Removable Medium Feature */
386   CDIO_MMC_FEATURE_WRITE_PROTECT    = 0x004, /**< Write Protect Feature */
387   CDIO_MMC_FEATURE_RANDOM_READABLE  = 0x010, /**< Random Readable Feature */
388   CDIO_MMC_FEATURE_MULTI_READ       = 0x01D, /**< Multi-Read Feature */
389   CDIO_MMC_FEATURE_CD_READ          = 0x01E, /**< CD Read Feature */
390   CDIO_MMC_FEATURE_DVD_READ         = 0x01F, /**< DVD Read Feature */
391   CDIO_MMC_FEATURE_RANDOM_WRITABLE  = 0x020, /**< Random Writable Feature */
392   CDIO_MMC_FEATURE_INCR_WRITE       = 0x021, /**< Incremental Streaming
393                                                 Writable Feature */
394   CDIO_MMC_FEATURE_SECTOR_ERASE     = 0x022, /**< Sector Erasable Feature */
395   CDIO_MMC_FEATURE_FORMATABLE       = 0x023, /**< Formattable Feature */
396   CDIO_MMC_FEATURE_DEFECT_MGMT      = 0x024, /**< Management Ability of the
397                                                 Logical Unit/media system to
398                                                 provide an apparently
399                                                 defect-free space.*/
400   CDIO_MMC_FEATURE_WRITE_ONCE       = 0x025, /**< Write Once
401                                                    Feature */
402   CDIO_MMC_FEATURE_RESTRICT_OVERW   = 0x026, /**< Restricted Overwrite
403                                                 Feature */
404   CDIO_MMC_FEATURE_CD_RW_CAV        = 0x027, /**< CD-RW CAV Write Feature */
405   CDIO_MMC_FEATURE_MRW              = 0x028, /**< MRW Feature */
406   CDIO_MMC_FEATURE_ENHANCED_DEFECT  = 0x029, /**< Enhanced Defect Reporting */
407   CDIO_MMC_FEATURE_DVD_PRW          = 0x02A, /**< DVD+RW Feature */
408   CDIO_MMC_FEATURE_DVD_PR           = 0x02B, /**< DVD+R Feature */
409   CDIO_MMC_FEATURE_RIGID_RES_OVERW  = 0x02C, /**< Rigid Restricted Overwrite */
410   CDIO_MMC_FEATURE_CD_TAO           = 0x02D, /**< CD Track at Once */
411   CDIO_MMC_FEATURE_CD_SAO           = 0x02E, /**< CD Mastering (Session at
412                                                 Once) */
413   CDIO_MMC_FEATURE_DVD_R_RW_WRITE   = 0x02F, /**< DVD-R/RW Write */
414   CDIO_MMC_FEATURE_CD_RW_MEDIA_WRITE= 0x037, /**< CD-RW Media Write Support */
415   CDIO_MMC_FEATURE_DVD_PR_2_LAYER   = 0x03B, /**< DVD+R Double Layer */
416   CDIO_MMC_FEATURE_POWER_MGMT       = 0x100, /**< Initiator and device directed
417                                                 power management */
418   CDIO_MMC_FEATURE_CDDA_EXT_PLAY    = 0x103, /**< Ability to play audio CDs
419                                                 via the Logical Unit's own
420                                                 analog output */
421   CDIO_MMC_FEATURE_MCODE_UPGRADE    = 0x104, /* Ability for the device to
422                                                 accept  new microcode via
423                                                 the interface */
424   CDIO_MMC_FEATURE_TIME_OUT         = 0x105, /**< Ability to respond to all
425                                                 commands within a specific
426                                                 time */
427   CDIO_MMC_FEATURE_DVD_CSS          = 0x106, /**< Ability to perform DVD
428                                                 CSS/CPPM authentication and
429                                                 RPC */
430   CDIO_MMC_FEATURE_RT_STREAMING     = 0x107, /**< Ability to read and write
431                                                 using Initiator requested
432                                                 performance parameters   */
433   CDIO_MMC_FEATURE_LU_SN            = 0x108, /**< The Logical Unit has a unique
434                                                 identifier. */
435   CDIO_MMC_FEATURE_FIRMWARE_DATE    = 0x1FF, /**< Firmware creation date
436                                                 report */
437 } cdio_mmc_feature_t;
438 
439 typedef enum {
440   CDIO_MMC_FEATURE_INTERFACE_UNSPECIFIED = 0,
441   CDIO_MMC_FEATURE_INTERFACE_SCSI        = 1,
442   CDIO_MMC_FEATURE_INTERFACE_ATAPI       = 2,
443   CDIO_MMC_FEATURE_INTERFACE_IEEE_1394   = 3,
444   CDIO_MMC_FEATURE_INTERFACE_IEEE_1394A  = 4,
445   CDIO_MMC_FEATURE_INTERFACE_FIBRE_CH    = 5
446 } cdio_mmc_feature_interface_t;
447 
448 
449 /**
450     The largest Command Descriptor Block (CDB) size.
451     The possible sizes are 6, 10, and 12 bytes.
452 */
453 #define MAX_CDB_LEN 12
454 
455 /**
456     \brief A Command Descriptor Block (CDB) used in sending MMC
457     commands.
458  */
459 typedef struct mmc_cdb_s {
460   uint8_t field[MAX_CDB_LEN];
461 } mmc_cdb_t;
462 
463   /**
464       \brief Format of header block in data returned from an MMC
465     GET_CONFIGURATION command.
466   */
467   typedef struct mmc_feature_list_header_s {
468     unsigned char length_msb;
469     unsigned char length_1sb;
470     unsigned char length_2sb;
471     unsigned char length_lsb;
472     unsigned char reserved1;
473     unsigned char reserved2;
474     unsigned char profile_msb;
475     unsigned char profile_lsb;
476   } cdio_mmc_feature_list_header_t;
477 
478   /**
479       An enumeration indicating whether an MMC command is sending
480       data, or getting data, or does none of both.
481   */
482   typedef enum mmc_direction_s {
483     SCSI_MMC_DATA_READ,
484     SCSI_MMC_DATA_WRITE,
485     SCSI_MMC_DATA_NONE
486   } cdio_mmc_direction_t;
487   /**
488      Indicate to applications that \p SCSI_MMC_DATA_NONE is available.
489      It has been added after version 0.82 and should be used with
490      commands that neither read nor write payload bytes. (On
491      Linux, at least, these work with \p SCSI_MMC_DATA_READ and \p
492      SCSI_MMC_DATA_WRITE, too.)
493   */
494 #define SCSI_MMC_HAS_DIR_NONE 1
495 
496   typedef struct mmc_subchannel_s
497   {
498     uint8_t       reserved;
499     uint8_t       audio_status;
500     uint16_t      data_length; /**< Really ISO 9660 7.2.2 */
501     uint8_t       format;
502     uint8_t       address:      4;
503     uint8_t       control:      4;
504     uint8_t       track;
505     uint8_t       index;
506     uint8_t       abs_addr[4];
507     uint8_t       rel_addr[4];
508   } cdio_mmc_subchannel_t;
509 
510 #define CDIO_MMC_SET_COMMAND(cdb, command)      \
511   cdb[0] = command
512 
513 #define CDIO_MMC_SET_READ_TYPE(cdb, sector_type) \
514   cdb[1] = (sector_type << 2)
515 
516 #define CDIO_MMC_GETPOS_LEN16(p, pos)           \
517   (p[pos]<<8) + p[pos+1]
518 
519 #define CDIO_MMC_GET_LEN16(p)                   \
520   (p[0]<<8) + p[1]
521 
522 #define CDIO_MMC_GET_LEN32(p) \
523   (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
524 
525 #define CDIO_MMC_SET_LEN16(cdb, pos, len)       \
526   cdb[pos  ] = (len >>  8) & 0xff;              \
527   cdb[pos+1] = (len      ) & 0xff
528 
529 #define CDIO_MMC_SET_READ_LBA(cdb, lba)         \
530   cdb[2] = (lba >> 24) & 0xff; \
531   cdb[3] = (lba >> 16) & 0xff; \
532   cdb[4] = (lba >>  8) & 0xff; \
533   cdb[5] = (lba      ) & 0xff
534 
535 #define CDIO_MMC_SET_START_TRACK(cdb, command) \
536   cdb[6] = command
537 
538 #define CDIO_MMC_SET_READ_LENGTH24(cdb, len) \
539   cdb[6] = (len >> 16) & 0xff; \
540   cdb[7] = (len >>  8) & 0xff; \
541   cdb[8] = (len      ) & 0xff
542 
543 #define CDIO_MMC_SET_READ_LENGTH16(cdb, len) \
544   CDIO_MMC_SET_LEN16(cdb, 7, len)
545 
546 #define CDIO_MMC_SET_READ_LENGTH8(cdb, len) \
547   cdb[8] = (len      ) & 0xff
548 
549 #define CDIO_MMC_MCSB_ALL_HEADERS 0xf
550 
551 #define CDIO_MMC_SET_MAIN_CHANNEL_SELECTION_BITS(cdb, val) \
552   cdb[9] = val << 3;
553 
554 /**
555    Get the output port volumes and port selections used on AUDIO PLAY
556    commands via a MMC \p MODE \p SENSE command using the CD Audio Control
557    Page.
558    @param p_cdio the CD object to be acted upon.
559    @param p_volume volume parameters retrieved
560    @return \p DRIVER_OP_SUCCESS if we ran the command ok.
561 */
562 driver_return_code_t mmc_audio_get_volume (CdIo_t *p_cdio,  /*out*/
563                                            mmc_audio_volume_t *p_volume);
564 
565   /**
566      Read Audio Subchannel information
567 
568      @param p_cdio the CD object to be acted upon.
569      @param p_subchannel place for returned subchannel information
570   */
571     driver_return_code_t
572     mmc_audio_read_subchannel (CdIo_t *p_cdio,
573                            /*out*/ cdio_subchannel_t *p_subchannel);
574 
575   /**
576     Return a string containing the name of the audio state as returned from
577     the Q_SUBCHANNEL.
578   */
579   const char *mmc_audio_state2str( uint8_t i_audio_state );
580 
581   /**
582      Get the block size used in read requests, via MMC (e.g. READ_10,
583      READ_MSF, ...)
584      @param p_cdio the CD object to be acted upon.
585      @return the blocksize if > 0; error if <= 0
586   */
587   int mmc_get_blocksize ( CdIo_t *p_cdio );
588 
589   /**
590     Return the length in bytes of the Command Descriptor
591     Buffer (CDB) for a given MMC command. The length will be
592     either 6, 10, or 12.
593   */
594   uint8_t mmc_get_cmd_len(uint8_t mmc_cmd);
595 
596   /**
597     Get the lsn of the end of the CD
598 
599     @param p_cdio the CD object to be acted upon.
600     @return the lsn. On error return CDIO_INVALID_LSN.
601   */
602   lsn_t mmc_get_disc_last_lsn( const CdIo_t *p_cdio );
603 
604   /**
605     Return the discmode as reported by the MMC Read (FULL) \p TOC
606     command.
607 
608     Information was obtained from Section 5.1.13 (Read TOC/PMA/ATIP)
609     pages 56-62 from the MMC draft specification, revision 10a
610     at http://www.t10.org/ftp/t10/drafts/mmc/mmc-r10a.pdf See
611     especially tables 72, 73 and 75.
612   */
613   discmode_t mmc_get_discmode( const CdIo_t *p_cdio );
614 
615 
616   typedef enum {
617     CDIO_MMC_LEVEL_WEIRD,
618     CDIO_MMC_LEVEL_1,
619     CDIO_MMC_LEVEL_2,
620     CDIO_MMC_LEVEL_3,
621     CDIO_MMC_LEVEL_NONE
622   } cdio_mmc_level_t;
623 
624   /**
625     Get the MMC level supported by the device.
626     @param p_cdio the CD object to be acted upon.
627     @return MMC level supported by the device.
628   */
629   cdio_mmc_level_t mmc_get_drive_mmc_cap(CdIo_t *p_cdio);
630 
631 
632   /**
633     Get the DVD type associated with cd object.
634 
635     @param p_cdio the CD object to be acted upon.
636     @param s location to store DVD information.
637     @return the DVD discmode.
638   */
639   discmode_t mmc_get_dvd_struct_physical ( const CdIo_t *p_cdio,
640                                            cdio_dvd_struct_t *s);
641 
642   /**
643     Find out if media tray is open or closed.
644     @param p_cdio the CD object to be acted upon.
645     @return 1 if media is open, 0 if closed. Error
646     return codes are the same as \p driver_return_code_t.
647   */
648   int mmc_get_tray_status ( const CdIo_t *p_cdio );
649 
650   /**
651     Get the CD-ROM hardware info via an MMC \p INQUIRY command.
652 
653     @param p_cdio the CD object to be acted upon.
654     @param p_hw_info place to store hardware information retrieved
655     @return true if we were able to get hardware info, false if we had
656     an error.
657   */
658   bool mmc_get_hwinfo ( const CdIo_t *p_cdio,
659                         /* out*/ cdio_hwinfo_t *p_hw_info );
660 
661 
662   /**
663     Find out if media has changed since the last call.
664     @param p_cdio the CD object to be acted upon.
665     @return 1 if media has changed since last call, 0 if not. Error
666     return codes are the same as \p driver_return_code_t.
667   */
668   int mmc_get_media_changed(const CdIo_t *p_cdio);
669 
670   /**
671     Get the media catalog number (\p MCN) from the CD via MMC.
672 
673     @param p_cdio the CD object to be acted upon.
674     @return the media catalog number r NULL if there is none or we
675     don't have the ability to get it.
676 
677     Note: The caller must free the returned string with cdio_free()
678     when done with it.
679 
680   */
681   char * mmc_get_mcn(const CdIo_t *p_cdio);
682 
683   /**
684     Get the international standard recording code (\p ISRC) of the track via MMC.
685 
686     @param p_cdio the CD object to be acted upon.
687     @param i_track the track to get the ISRC info for
688     @return international standard recording code or NULL if there is
689     none or we don't have the ability to get it.
690 
691     Note: The caller must free the returned string with cdio_free()
692     when done with it.
693 
694   */
695   char * mmc_get_track_isrc(const CdIo_t *p_cdio, track_t i_track);
696 
697   /**
698     Read cdtext information for a cdtext_t object.
699 
700     This is the raw SCSI/MMC reply as retrieved by mmc_read_toc_cdtext().
701     It consists of 4 header bytes and a variable number of text packs.
702 
703     The first two bytes of the header, a Big-Endian number, specifies
704     the number of following bytes. The count also includes the next two
705     header bytes which should be 0.
706 
707     See also information in mmc_read_toc_cdtext().
708 
709     Here is some code to parse the text packs into a \p cdtext_t object:
710 
711     @code
712       #include <cdio/mmc.h>
713       #include <cdio/cdtext.h>
714 
715       reply = mmc_read_cdtext(p_cdio);
716       if (NULL != reply)
717           cdtext_data_init(p_cdtext, reply + 4,
718                            (size_t) CDIO_MMC_GET_LEN16(reply) - 2);
719     @endcode
720 
721     @param p_cdio the CD object to be acted upon.
722     @return pointer to data on success, \p NULL on error or if CD-Text
723             information does not exist.
724 
725     Note: the caller must free the returned memory.
726 
727   */
728   uint8_t * mmc_read_cdtext (const CdIo_t *p_cdio);
729 
730   /**
731     Report if CD-ROM has a particular kind of interface (ATAPI, SCSCI, ...)
732     Is it possible for an interface to have several? If not this
733     routine could probably return the single \p mmc_feature_interface_t.
734     @param p_cdio the CD object to be acted upon.
735     @param e_interface
736     @return true if we have the interface and false if not.
737   */
738   bool_3way_t mmc_have_interface(CdIo_t *p_cdio,
739                                  cdio_mmc_feature_interface_t e_interface );
740 
741 
742   /**
743       Read just the user data part of some sort of data sector (via
744       mmc_read_cd).
745 
746       @param p_cdio object to read from
747 
748       @param p_buf place to read data into.  The caller should make
749              sure this location can store at least \p CDIO_CD_FRAMESIZE,
750              \p M2RAW_SECTOR_SIZE, or \p M2F2_SECTOR_SIZE depending on the
751              kind of sector getting read. If you don't know whether
752              you have a Mode 1/2, Form 1/ Form 2/Formless sector best
753              to reserve space for the maximum, \p M2RAW_SECTOR_SIZE.
754 
755       @param i_lsn sector to read
756       @param i_blocksize size of each block
757       @param i_blocks number of blocks to read
758 
759   */
760   driver_return_code_t mmc_read_data_sectors ( CdIo_t *p_cdio, void *p_buf,
761                                                lsn_t i_lsn,
762                                                uint16_t i_blocksize,
763                                                uint32_t i_blocks );
764 
765   /**
766       Read sectors using SCSI-MMC GPCMD_READ_CD.
767       Can read only up to 25 blocks.
768   */
769   driver_return_code_t mmc_read_sectors ( const CdIo_t *p_cdio, void *p_buf,
770                                           lsn_t i_lsn,  int read_sector_type,
771                                           uint32_t i_blocks);
772 
773   /**
774     Run a Multimedia command (MMC).
775 
776     @param p_cdio        CD structure set by cdio_open().
777     @param i_timeout_ms  time in milliseconds we will wait for the command
778                          to complete.
779     @param p_cdb         CDB bytes. All values that are needed should be set
780                          on input. We'll figure out what the right CDB length
781                          should be.
782     @param e_direction   direction the transfer is to go.
783     @param i_buf         Size of buffer
784     @param p_buf         Buffer for data, both sending and receiving.
785 
786     @return 0 if command completed successfully.
787   */
788   driver_return_code_t
789   mmc_run_cmd( const CdIo_t *p_cdio, unsigned int i_timeout_ms,
790                const mmc_cdb_t *p_cdb,
791                cdio_mmc_direction_t e_direction, unsigned int i_buf,
792                /*in/out*/ void *p_buf );
793 
794   /**
795     Run a Multimedia command (MMC) specifying the CDB length.
796     The motivation here is for example ot use in is an undocumented
797     debug command for LG drives (namely E7), whose length is being
798     miscalculated by mmc_get_cmd_len(); it doesn't follow the usual
799     code number to length conventions. Patch supplied by SukkoPera.
800 
801     @param p_cdio        CD structure set by cdio_open().
802     @param i_timeout_ms  time in milliseconds we will wait for the command
803                          to complete.
804     @param p_cdb         CDB bytes. All values that are needed should be set
805                          on input.
806     @param i_cdb         number of CDB bytes.
807     @param e_direction   direction the transfer is to go.
808     @param i_buf         Size of buffer
809     @param p_buf         Buffer for data, both sending and receiving.
810 
811     @return 0 if command completed successfully.
812   */
813   driver_return_code_t
814   mmc_run_cmd_len( const CdIo_t *p_cdio, unsigned int i_timeout_ms,
815                    const mmc_cdb_t *p_cdb, unsigned int i_cdb,
816                    cdio_mmc_direction_t e_direction, unsigned int i_buf,
817                    /*in/out*/ void *p_buf );
818 
819   /**
820       Obtain the SCSI sense reply of the most-recently-performed MMC command.
821       These bytes give an indication of possible problems which occured in
822       the drive while the command was performed. With some commands they tell
823       about the current state of the drive (e.g. 00h \p TEST \p UNIT \p READY).
824       @param p_cdio CD structure set by cdio_open().
825 
826       @param pp_sense returns the sense bytes received from the drive.
827       This is allocated memory or NULL if no sense bytes are
828       available. Dispose non-NULL pointers by cdio_free() when no longer
829       needed.  See SPC-3 4.5.3 Fixed format sense data.  SCSI error
830       codes as of SPC-3 Annex D, MMC-5 Annex F: sense[2]&15 = Key ,
831       sense[12] = \p ASC , sense[13] = \p ASCQ
832 
833       @return number of valid bytes in sense, 0 when no sense
834               bytes are available, and less than 0 when there is an internal error.
835   */
836   int mmc_last_cmd_sense ( const CdIo_t *p_cdio,
837                            cdio_mmc_request_sense_t **pp_sense);
838 
839   /**
840     Set the block size for subsequest read requests, via MMC.
841   */
842   driver_return_code_t mmc_set_blocksize ( const CdIo_t *p_cdio,
843                                            uint16_t i_blocksize);
844 
845   /**
846     Get string name for MMC command
847 
848     @param command cdio_mmc_gpcmd_t command value
849     @return string name of command
850 
851   */
852   const char *mmc_cmd2str(uint8_t command);
853 
854 
855 
856 #ifdef __cplusplus
857 }
858 #endif /* __cplusplus */
859 
860 /**
861     The below variables are trickery to force the above enum symbol
862     values to be recorded in debug symbol tables. They are used to
863     allow one to refer to the enumeration value names in the typedefs
864     above in a debugger and debugger expressions
865 */
866 extern cdio_mmc_feature_t           debug_cdio_mmc_feature;
867 extern cdio_mmc_feature_interface_t debug_cdio_mmc_feature_interface;
868 extern cdio_mmc_feature_profile_t   debug_cdio_mmc_feature_profile;
869 extern cdio_mmc_get_conf_t          debug_cdio_mmc_get_conf;
870 extern cdio_mmc_gpcmd_t             debug_cdio_mmc_gpcmd;
871 extern cdio_mmc_read_sub_state_t    debug_cdio_mmc_read_sub_state;
872 extern cdio_mmc_read_cd_type_t      debug_cdio_mmc_read_cd_type;
873 extern cdio_mmc_readtoc_t           debug_cdio_mmc_readtoc;
874 extern cdio_mmc_mode_page_t         debug_cdio_mmc_mode_page;
875 
876 #ifndef DO_NOT_WANT_OLD_MMC_COMPATIBILITY
877 #define CDIO_MMC_GPCMD_START_STOP CDIO_MMC_GPCMD_START_STOP_UNIT
878 #define CDIO_MMC_GPCMD_ALLOW_MEDIUM_REMOVAL  \
879     CDIO_MMC_GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
880 #endif /*DO_NOT_WANT_PARANOIA_COMPATIBILITY*/
881 
882 #endif /* CDIO_MMC_H_ */
883 
884 /*
885  * Local variables:
886  *  c-file-style: "ruby"
887  *  tab-width: 8
888  *  indent-tabs-mode: nil
889  * End:
890  */
891