1 /*
2    Wrappers for specific Multimedia Command (MMC) commands e.g., READ
3    DISC, START/STOP UNIT.
4 
5    Copyright (C) 2010-2012 Rocky Bernstein <rocky@gnu.org>
6 
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 # define __CDIO_CONFIG_H__ 1
24 #endif
25 
26 #ifdef HAVE_STDBOOL_H
27 # include <stdbool.h>
28 #endif
29 
30 #include <cdio/cdio.h>
31 #include <cdio/mmc_ll_cmds.h>
32 #include "cdio_private.h"
33 #include "mmc_cmd_helper.h"
34 
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
38 
39 /**
40    Get drive capabilities vis SCSI-MMC GET CONFIGURATION
41    @param p_cdio the CD object to be acted upon.
42 
43    @param p_buf pointer to location to store mode sense information
44    @param i_size number of bytes allocated to p_buf
45    @param i_return_type value in range 0..2 giving what kind of configuration
46    to return:
47 
48    : 0 Full Header and Full Descriptors;
49    : 1 Feature Headers and those with their Current Bit.
50    : 2 One Feature header and zero or one Feature Descriptors.
51 
52    @param i_starting_feature_number feature number from which to start
53    getting information.
54 
55    @param i_timeout_ms value in milliseconds to use on timeout. Setting
56    to 0 uses the default time-out value stored in
57    mmc_timeout_ms.
58 
59    @return DRIVER_OP_SUCCESS (0) if we got the status.
60    return codes are the same as driver_return_code_t
61 */
62 driver_return_code_t
mmc_get_configuration(const CdIo_t * p_cdio,void * p_buf,unsigned int i_size,unsigned int i_return_type,unsigned int i_starting_feature_number,unsigned int i_timeout_ms)63 mmc_get_configuration(const CdIo_t *p_cdio, void *p_buf,
64                       unsigned int i_size,
65                       unsigned int i_return_type,
66                       unsigned int i_starting_feature_number,
67                       unsigned int i_timeout_ms)
68 
69 {
70     MMC_CMD_SETUP(CDIO_MMC_GPCMD_GET_CONFIGURATION);
71     CDIO_MMC_SET_READ_LENGTH8(cdb.field, i_size);
72     if (0 == i_timeout_ms) i_timeout_ms = mmc_timeout_ms;
73     cdb.field[1] = i_return_type & 0x3;
74     CDIO_MMC_SET_LEN16(cdb.field, 2, i_starting_feature_number);
75     return MMC_RUN_CMD(SCSI_MMC_DATA_READ, i_timeout_ms);
76 }
77 
78 /**
79    Return results of media event status via SCSI-MMC GET EVENT STATUS
80 
81    @param p_cdio the CD object to be acted upon.
82 
83    @param out_buf media status code from operation
84 
85    @return DRIVER_OP_SUCCESS (0) if we got the status. Return codes
86    are the same as driver_return_code_t
87  */
88 driver_return_code_t
mmc_get_event_status(const CdIo_t * p_cdio,uint8_t out_buf[2])89 mmc_get_event_status(const CdIo_t *p_cdio, uint8_t out_buf[2])
90 {
91     uint8_t buf[8] = { 0, };
92     void   *p_buf  = &buf;
93     const unsigned int i_size = sizeof(buf);
94     driver_return_code_t i_status;
95 
96     MMC_CMD_SETUP_READ16(CDIO_MMC_GPCMD_GET_EVENT_STATUS);
97 
98     cdb.field[1] = 1;      /* We poll for info */
99     cdb.field[4] = 1 << 4; /* We want Media events */
100 
101     i_status = MMC_RUN_CMD(SCSI_MMC_DATA_READ, mmc_timeout_ms);
102     if (i_status == DRIVER_OP_SUCCESS) {
103         out_buf[0] = buf[4];
104         out_buf[1] = buf[5];
105     }
106     return i_status;
107 }
108 /**
109    Run a SCSI-MMC MODE SELECT (10-byte) command
110    and put the results in p_buf.
111 
112    @param p_cdio the CD object to be acted upon.
113 
114    @param p_buf pointer to location to store mode sense information
115 
116    @param i_size number of bytes allocated to p_buf
117 
118    @param page which "page" of the mode sense command we are interested in
119 
120    @param i_timeout_ms value in milliseconds to use on timeout. Setting
121           to 0 uses the default time-out value stored in
122           mmc_timeout_ms.
123 
124    @return DRIVER_OP_SUCCESS if we ran the command ok.
125 
126  */
127 driver_return_code_t
mmc_mode_select_10(CdIo_t * p_cdio,void * p_buf,unsigned int i_size,int page,unsigned int i_timeout_ms)128 mmc_mode_select_10(CdIo_t *p_cdio, /*out*/ void *p_buf, unsigned int i_size,
129                    int page, unsigned int i_timeout_ms)
130 {
131     MMC_CMD_SETUP_READ16(CDIO_MMC_GPCMD_MODE_SELECT_10);
132     if (0 == i_timeout_ms) i_timeout_ms = mmc_timeout_ms;
133     cdb.field[1] = page;
134     return MMC_RUN_CMD(SCSI_MMC_DATA_WRITE, i_timeout_ms);
135 }
136 
137 /**
138    Run a SCSI-MMC MODE SENSE command (10-byte version)
139    and put the results in p_buf
140    @param p_cdio the CD object to be acted upon.
141    @param p_buf pointer to location to store mode sense information
142    @param i_size number of bytes allocated to p_buf
143    @param page which "page" of the mode sense command we are interested in
144    @return DRIVER_OP_SUCCESS if we ran the command ok.
145 */
146 driver_return_code_t
mmc_mode_sense_10(CdIo_t * p_cdio,void * p_buf,unsigned int i_size,unsigned int page)147 mmc_mode_sense_10(CdIo_t *p_cdio, void *p_buf, unsigned int i_size,
148                   unsigned int page)
149 {
150     MMC_CMD_SETUP_READ16(CDIO_MMC_GPCMD_MODE_SENSE_10);
151     cdb.field[2] = CDIO_MMC_ALL_PAGES & page;
152     return MMC_RUN_CMD(SCSI_MMC_DATA_READ, mmc_timeout_ms);
153 }
154 
155 /**
156    Run a SCSI-MMC MODE SENSE command (6-byte version)
157    and put the results in p_buf
158    @param p_cdio the CD object to be acted upon.
159    @param p_buf pointer to location to store mode sense information
160    @param i_size number of bytes allocated to p_buf
161    @param page which "page" of the mode sense command we are interested in
162    @return DRIVER_OP_SUCCESS if we ran the command ok.
163 */
164 driver_return_code_t
mmc_mode_sense_6(CdIo_t * p_cdio,void * p_buf,unsigned int i_size,int page)165 mmc_mode_sense_6(CdIo_t *p_cdio, void *p_buf, unsigned int i_size, int page)
166 {
167     MMC_CMD_SETUP(CDIO_MMC_GPCMD_MODE_SENSE_6);
168     cdb.field[4] = i_size;
169 
170     cdb.field[2] = CDIO_MMC_ALL_PAGES & page;
171 
172     return MMC_RUN_CMD(SCSI_MMC_DATA_READ, mmc_timeout_ms);
173 }
174 
175 /**
176    Request preventing/allowing medium removal on a drive via
177    SCSI-MMC PREVENT/ALLOW MEDIUM REMOVAL.
178 
179    @param p_cdio the CD object to be acted upon.
180    @param b_prevent true of drive locked and false if unlocked
181    @param b_persisent make b_prevent state persistent
182 
183    @return DRIVER_OP_SUCCESS (0) if we got the status.
184    return codes are the same as driver_return_code_t
185 */
186 driver_return_code_t
mmc_prevent_allow_medium_removal(const CdIo_t * p_cdio,bool b_persistent,bool b_prevent,unsigned int i_timeout_ms)187 mmc_prevent_allow_medium_removal(const CdIo_t *p_cdio,
188                                  bool b_persistent, bool b_prevent,
189                                  unsigned int i_timeout_ms)
190 {
191     uint8_t buf[8] = { 0, };
192     void   *p_buf  = &buf;
193     const unsigned int i_size = 0;
194 
195     MMC_CMD_SETUP(CDIO_MMC_GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL);
196     if (0 == i_timeout_ms) i_timeout_ms = mmc_timeout_ms;
197     if (b_prevent)         cdb.field[4] |= 1;
198     if (b_persistent)      cdb.field[4] |= 2;
199 
200     return MMC_RUN_CMD(SCSI_MMC_DATA_WRITE, i_timeout_ms);
201 }
202 
203 /* Maximum blocks to retrieve. Would be nice to customize this based on
204    drive capabilities.
205 */
206 #define MAX_CD_READ_BLOCKS 16
207 
208 /**
209    Issue a MMC READ_CD command.
210 
211    @param p_cdio  object to read from
212 
213    @param p_buf1   Place to store data. The caller should ensure that
214    p_buf1 can hold at least i_blocksize * i_blocks  bytes.
215 
216    @param i_lsn   sector to read
217 
218    @param expected_sector_type restricts reading to a specific CD
219    sector type.  Only 3 bits with values 1-5 are used:
220    0 all sector types
221    1 CD-DA sectors only
222    2 Mode 1 sectors only
223    3 Mode 2 formless sectors only. Note in contrast to all other
224    values an MMC CD-ROM is not required to support this mode.
225    4 Mode 2 Form 1 sectors only
226    5 Mode 2 Form 2 sectors only
227 
228    @param b_digital_audio_play Control error concealment when the
229    data being read is CD-DA.  If the data being read is not CD-DA,
230    this parameter is ignored.  If the data being read is CD-DA and
231    DAP is false zero, then the user data returned should not be
232    modified by flaw obscuring mechanisms such as audio data mute and
233    interpolate.  If the data being read is CD-DA and DAP is true,
234    then the user data returned should be modified by flaw obscuring
235    mechanisms such as audio data mute and interpolate.
236 
237    b_sync_header return the sync header (which will probably have
238    the same value as CDIO_SECTOR_SYNC_HEADER of size
239    CDIO_CD_SYNC_SIZE).
240 
241    @param header_codes Header Codes refer to the sector header and
242    the sub-header that is present in mode 2 formed sectors:
243 
244    0 No header information is returned.
245    1 The 4-byte sector header of data sectors is be returned,
246    2 The 8-byte sector sub-header of mode 2 formed sectors is
247    returned.
248    3 Both sector header and sub-header (12 bytes) is returned.
249    The Header preceeds the rest of the bytes (e.g. user-data bytes)
250    that might get returned.
251 
252    @param b_user_data  Return user data if true.
253 
254    For CD-DA, the User Data is CDIO_CD_FRAMESIZE_RAW bytes.
255 
256    For Mode 1, The User Data is ISO_BLOCKSIZE bytes beginning at
257    offset CDIO_CD_HEADER_SIZE+CDIO_CD_SUBHEADER_SIZE.
258 
259    For Mode 2 formless, The User Data is M2RAW_SECTOR_SIZE bytes
260    beginning at offset CDIO_CD_HEADER_SIZE+CDIO_CD_SUBHEADER_SIZE.
261 
262    For data Mode 2, form 1, User Data is ISO_BLOCKSIZE bytes beginning at
263    offset CDIO_CD_XA_SYNC_HEADER.
264 
265    For data Mode 2, form 2, User Data is 2 324 bytes beginning at
266    offset CDIO_CD_XA_SYNC_HEADER.
267 
268    @param b_sync
269 
270    @param b_edc_ecc true if we return EDC/ECC error detection/correction bits.
271 
272    The presence and size of EDC redundancy or ECC parity is defined
273    according to sector type:
274 
275    CD-DA sectors have neither EDC redundancy nor ECC parity.
276 
277    Data Mode 1 sectors have 288 bytes of EDC redundancy, Pad, and
278    ECC parity beginning at offset 2064.
279 
280    Data Mode 2 formless sectors have neither EDC redundancy nor ECC
281    parity
282 
283    Data Mode 2 form 1 sectors have 280 bytes of EDC redundancy and
284    ECC parity beginning at offset 2072
285 
286    Data Mode 2 form 2 sectors optionally have 4 bytes of EDC
287    redundancy beginning at offset 2348.
288 
289    @param c2_error_information If true associate a bit with each
290    sector for C2 error The resulting bit field is ordered exactly as
291    the main channel bytes.  Each 8-bit boundary defines a byte of
292    flag bits.
293 
294    @param subchannel_selection subchannel-selection bits
295 
296    0  No Sub-channel data shall be returned.  (0 bytes)
297    1  RAW P-W Sub-channel data shall be returned.  (96 byte)
298    2  Formatted Q sub-channel data shall be transferred (16 bytes)
299    3  Reserved
300    4  Corrected and de-interleaved R-W sub-channel (96 bytes)
301    5-7  Reserved
302 
303    @param i_blocksize size of the a block expected to be returned
304 
305    @param i_blocks number of blocks expected to be returned.
306 
307    @return DRIVER_OP_SUCCESS if we ran the command ok.
308 */
309 driver_return_code_t
mmc_read_cd(const CdIo_t * p_cdio,void * p_buf1,lsn_t i_lsn,int read_sector_type,bool b_digital_audio_play,bool b_sync,uint8_t header_codes,bool b_user_data,bool b_edc_ecc,uint8_t c2_error_information,uint8_t subchannel_selection,uint16_t i_blocksize,uint32_t i_blocks)310 mmc_read_cd(const CdIo_t *p_cdio, void *p_buf1, lsn_t i_lsn,
311             int read_sector_type, bool b_digital_audio_play,
312             bool b_sync, uint8_t header_codes, bool b_user_data,
313             bool b_edc_ecc, uint8_t c2_error_information,
314             uint8_t subchannel_selection, uint16_t i_blocksize,
315             uint32_t i_blocks)
316 {
317     void *p_buf = p_buf1;
318     uint8_t cdb9 = 0;
319     const unsigned int i_timeout = mmc_timeout_ms * (MAX_CD_READ_BLOCKS/2);
320 
321     MMC_CMD_SETUP(CDIO_MMC_GPCMD_READ_CD);
322 
323     /* Catch what may be a common bug. */
324     if (NULL == p_buf) return DRIVER_OP_BAD_POINTER;
325 
326     CDIO_MMC_SET_READ_TYPE(cdb.field, read_sector_type);
327     if (b_digital_audio_play) cdb.field[1] |= 0x2;
328 
329     if (b_sync)      cdb9 |= 128;
330     if (b_user_data) cdb9 |=  16;
331     if (b_edc_ecc)   cdb9 |=   8;
332     cdb9 |= (header_codes & 3)         << 5;
333     cdb9 |= (c2_error_information & 3) << 1;
334     cdb.field[9]  = cdb9;
335     cdb.field[10] = (subchannel_selection & 7);
336 
337   {
338       unsigned int j = 0;
339       int i_status = DRIVER_OP_SUCCESS;
340 
341       while (i_blocks > 0) {
342           const unsigned i_blocks2 = (i_blocks > MAX_CD_READ_BLOCKS)
343               ? MAX_CD_READ_BLOCKS : i_blocks;
344 
345           const unsigned int i_size = i_blocksize * i_blocks2;
346 
347           p_buf = ((char *)p_buf1 ) + (j * i_blocksize);
348           CDIO_MMC_SET_READ_LBA     (cdb.field, (i_lsn+j));
349           CDIO_MMC_SET_READ_LENGTH24(cdb.field, i_blocks2);
350 
351           i_status = MMC_RUN_CMD(SCSI_MMC_DATA_READ, i_timeout);
352 
353           if (i_status) return i_status;
354 
355           i_blocks -= i_blocks2;
356           j += i_blocks2;
357       }
358       return i_status;
359   }
360 }
361 
362 /**
363   Request information about et drive capabilities vis SCSI-MMC READ
364   DISC INFORMATION
365 
366   @param p_cdio the CD object to be acted upon.
367 
368   @param p_buf pointer to location to store mode sense information
369 
370   @param i_size number of bytes allocated to p_buf
371 
372   @param data_type kind of information to retrieve.
373 
374   @return DRIVER_OP_SUCCESS (0) if we got the status.
375  */
376 driver_return_code_t
mmc_read_disc_information(const CdIo_t * p_cdio,void * p_buf,unsigned int i_size,cdio_mmc_read_disc_info_datatype_t data_type,unsigned int i_timeout_ms)377 mmc_read_disc_information(const CdIo_t *p_cdio, /*out*/ void *p_buf,
378                           unsigned int i_size,
379                           cdio_mmc_read_disc_info_datatype_t data_type,
380                           unsigned int i_timeout_ms)
381 {
382     MMC_CMD_SETUP(CDIO_MMC_GPCMD_READ_DISC_INFO);
383     CDIO_MMC_SET_READ_LENGTH8(cdb.field, i_size);
384     if (0 == i_timeout_ms) i_timeout_ms = mmc_timeout_ms;
385     cdb.field[1] = data_type & 0x7;
386     return MMC_RUN_CMD(SCSI_MMC_DATA_READ, i_timeout_ms);
387 }
388 
389 /**
390    Set the drive speed in K bytes per second using SCSI-MMC SET SPEED.
391    .
392 
393    @param p_cdio        CD structure set by cdio_open().
394    @param i_Kbs_speed   speed in K bytes per second. Note this is
395                         not in standard CD-ROM speed units, e.g.
396                         1x, 4x, 16x as it is in cdio_set_speed.
397                         To convert CD-ROM speed units to Kbs,
398                         multiply the number by 176 (for raw data)
399                         and by 150 (for filesystem data).
400                         Also note that ATAPI specs say that a value
401                         less than 176 will result in an error.
402                         On many CD-ROM drives,
403                         specifying a value too large will result in using
404                         the fastest speed.
405 
406     @return the drive speed if greater than 0. -1 if we had an
407     error. is -2 returned if this is not implemented for the current
408     driver.
409 
410     @see cdio_set_speed and mmc_set_drive_speed
411 
412    @return DRIVER_OP_SUCCESS if we ran the command ok.
413   */
414 int
mmc_set_speed(const CdIo_t * p_cdio,int i_Kbs_speed,unsigned int i_timeout_ms)415 mmc_set_speed(const CdIo_t *p_cdio, int i_Kbs_speed, unsigned int i_timeout_ms)
416 
417 {
418     uint8_t buf[14] = { 0, };
419     void * p_buf = &buf;
420     const unsigned int i_size = sizeof(buf);
421     MMC_CMD_SETUP(CDIO_MMC_GPCMD_SET_SPEED);
422 
423     if (0 == i_timeout_ms) i_timeout_ms = mmc_timeout_ms;
424 
425     /* If the requested speed is less than 1x 176 kb/s this command
426        will return an error - it's part of the ATAPI specs. Therefore,
427        test and stop early. */
428 
429     if ( i_Kbs_speed < 176 ) return -1;
430 
431     CDIO_MMC_SET_LEN16(cdb.field, 2, i_Kbs_speed);
432     /* Some drives like the Creative 24x CDRW require one to set a
433        nonzero write speed or else one gets an error back.  Some
434        specifications have setting the value 0xfffff indicate setting to
435        the maximum allowable speed.
436     */
437     CDIO_MMC_SET_LEN16(cdb.field, 4, 0xffff);
438     return MMC_RUN_CMD(SCSI_MMC_DATA_WRITE, i_timeout_ms);
439 }
440 
441 /**
442    Load or Unload media using a SCSI-MMC START/STOP UNIT command.
443 
444    @param p_cdio  the CD object to be acted upon.
445    @param b_eject eject if true and close tray if false
446    @param b_immediate wait or don't wait for operation to complete
447    @param power_condition Set CD-ROM to idle/standby/sleep. If nonzero,
448           eject/load is ignored, so set to 0 if you want to eject or load.
449 
450     @see mmc_eject_media or mmc_close_tray
451 */
452 driver_return_code_t
mmc_start_stop_unit(const CdIo_t * p_cdio,bool b_eject,bool b_immediate,uint8_t power_condition,unsigned int i_timeout_ms)453 mmc_start_stop_unit(const CdIo_t *p_cdio, bool b_eject, bool b_immediate,
454                     uint8_t power_condition, unsigned int i_timeout_ms)
455 {
456   uint8_t buf[1];
457   void * p_buf = &buf;
458   const unsigned int i_size = 0;
459 
460   MMC_CMD_SETUP_READ16(CDIO_MMC_GPCMD_START_STOP_UNIT);
461 
462   if (b_immediate) cdb.field[1] |= 1;
463 
464   if (power_condition)
465     cdb.field[4] = power_condition << 4;
466   else {
467     if (b_eject)
468       cdb.field[4] = 2; /* eject */
469     else
470       cdb.field[4] = 3; /* close tray for tray-type */
471   }
472 
473   return MMC_RUN_CMD(SCSI_MMC_DATA_WRITE, mmc_timeout_ms);
474 }
475 
476 /**
477    Check if drive is ready using SCSI-MMC TEST UNIT READY command.
478 
479    @param p_cdio  the CD object to be acted upon.
480 
481    @return DRIVER_OP_SUCCESS if we ran the command ok.
482  */
483 driver_return_code_t
mmc_test_unit_ready(const CdIo_t * p_cdio,unsigned int i_timeout_ms)484 mmc_test_unit_ready(const CdIo_t *p_cdio, unsigned int i_timeout_ms)
485 {
486     const unsigned int i_size = 0;
487     void  * p_buf = NULL;
488     MMC_CMD_SETUP_READ16(CDIO_MMC_GPCMD_TEST_UNIT_READY);
489 
490     if (0 == i_timeout_ms) i_timeout_ms = mmc_timeout_ms;
491     return MMC_RUN_CMD(SCSI_MMC_DATA_NONE, i_timeout_ms);
492 }
493 
494 /**
495    Issue a READ SUB-CHANNEL command to read current position, ISRC or
496    MCN from subchannel Q.
497    Note: READ SUB-CHANNEL is deprecated as of MMC-5
498          but the alternative requires manual parsing of the subchannel.
499 
500    @param p_cdio  the CD object to be acted upon.
501    @param i_track track number (only for ISRC)
502    @param sub_chan_param 2 for MCN, 3 for ISRC
503    @param i_length pointer to number of bytes to request.
504                    Will be overwritten by the number of bytes available.
505    @param p_buf  pointer to the location for the returned data
506 
507    @return DRIVER_OP_SUCCESS on success
508  */
509 driver_return_code_t
mmc_read_subchannel(const CdIo_t * p_cdio,track_t i_track,unsigned char sub_chan_param,unsigned int * i_length,char * p_buf,unsigned int i_timeout_ms)510 mmc_read_subchannel ( const CdIo_t *p_cdio,
511                              track_t i_track,
512                              unsigned char sub_chan_param,
513                              unsigned int *i_length,
514                              char *p_buf,
515                              unsigned int i_timeout_ms
516                             )
517 {
518   unsigned int i_size = *i_length;
519   mmc_cdb_t cdb = {{0, }};
520   driver_return_code_t i_status;
521 
522   if (i_size < 4)
523     return DRIVER_OP_BAD_PARAMETER;
524 
525   CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_SUBCHANNEL);
526   CDIO_MMC_SET_READ_LENGTH8(cdb.field, i_size);
527 
528   if(CDIO_SUBCHANNEL_CURRENT_POSITION == sub_chan_param)
529     cdb.field[1] = CDIO_CDROM_MSF;
530   else
531     cdb.field[1] = 0x0;
532   cdb.field[2] = 0x40;
533   cdb.field[3] = sub_chan_param;
534   if(CDIO_SUBCHANNEL_TRACK_ISRC == sub_chan_param)
535    cdb.field[6] = i_track;
536   else
537    cdb.field[6] = 0;
538 
539   memset(p_buf, 0, i_size);
540 
541   if (0 == i_timeout_ms) i_timeout_ms = mmc_timeout_ms;
542 
543   i_status = MMC_RUN_CMD(SCSI_MMC_DATA_READ, i_timeout_ms);
544 
545   if(i_status == DRIVER_OP_SUCCESS) {
546     *i_length = CDIO_MMC_GET_LEN16((p_buf+2)) + 4;
547   }
548   return i_status;
549 }
550 
551 /**
552    Issue a READ TOC/PMA/ATIP command to read the CD-TEXT from R-W sub-channel.
553 
554    @param p_cdio  the CD object to be acted upon.
555    @param i_length pointer to number of bytes to request.
556                    Will be overwritten by the number of bytes available.
557    @param p_buf  pointer to the location for the returned data
558 
559    @return DRIVER_OP_SUCCESS on success
560  */
561 driver_return_code_t
mmc_read_toc_cdtext(const CdIo_t * p_cdio,unsigned int * i_length,unsigned char * p_buf,unsigned int i_timeout_ms)562 mmc_read_toc_cdtext ( const CdIo_t *p_cdio, unsigned int *i_length,
563                       unsigned char *p_buf, unsigned int i_timeout_ms )
564 {
565   unsigned int i_size = *i_length;
566   mmc_cdb_t  cdb = {{0, }};
567   driver_return_code_t i_status;
568 
569   if (i_size < 4)
570     return DRIVER_OP_BAD_PARAMETER;
571 
572   CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_TOC);
573   CDIO_MMC_SET_READ_LENGTH16(cdb.field, i_size);
574 
575   memset(p_buf, 0, i_size);
576 
577   if (0 == i_timeout_ms) i_timeout_ms = mmc_timeout_ms;
578 
579   cdb.field[1] = CDIO_CDROM_MSF;
580   cdb.field[2] = CDIO_MMC_READTOC_FMT_CDTEXT;
581 
582   i_status = MMC_RUN_CMD(SCSI_MMC_DATA_READ, i_timeout_ms);
583 
584   if(i_status == DRIVER_OP_SUCCESS) {
585     *i_length = CDIO_MMC_GET_LEN16(p_buf) + 4;
586   }
587 
588   return i_status;
589 }
590