1 /*  cdrdao - write audio CD-Rs in disc-at-once mode
2  *
3  *  Copyright (C) 1998-2002 Andreas Mueller <andreas@daneb.de>
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 2 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, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #ifndef __GENERIC_MMC_H__
21 #define __GENERIC_MMC_H__
22 
23 #include "CdrDriver.h"
24 
25 class Toc;
26 class Track;
27 class CdTextEncoder;
28 
29 /*! \def OPT_MMC_USE_PQ
30     \brief use PQ sub-channel data for scanning
31 */
32 #define OPT_MMC_USE_PQ       0x0001
33 /*! \def OPT_MMC_PQ_BCD
34     \brief PQ sub-channel contains BCD numbers
35 */
36 #define OPT_MMC_PQ_BCD       0x0002
37 /*! \def OPT_MMC_READ_ISRC
38     \brief force reading of ISRC code with
39     READ SUB CHANNEL instead taking it from
40     the sub-channel data
41 */
42 #define OPT_MMC_READ_ISRC    0x0004
43 /*! \def OPT_MMC_SCAN_MCN
44     \brief take MCN from the sub-channel data
45     instead using READ SUB CHANNEL
46 */
47 #define OPT_MMC_SCAN_MCN     0x0008
48 /*! \def OPT_MMC_CD_TEXT
49     \brief drive supports CD-TEXT writing
50 */
51 #define OPT_MMC_CD_TEXT      0x0010
52 /*! \def OPT_MMC_NO_SUBCHAN
53     \brief drive does not support
54     reading sub-channel data
55 */
56 #define OPT_MMC_NO_SUBCHAN   0x0020
57 /*! \def OPT_MMC_NO_BURNPROOF
58     \brief disable BURN-Proof
59 */
60 #define OPT_MMC_NO_BURNPROOF 0x0040
61 /*! \def OPT_MMC_NO_RW_PACKED
62     \brief drive does not support the packed R-W
63     sub-channel writing mode
64 */
65 #define OPT_MMC_NO_RW_PACKED 0x0080
66 /*! \def OPT_MMC_USE_RAW_RW
67     \brief use RW sub-channel data for scanning
68 */
69 #define OPT_MMC_USE_RAW_RW   0x0100
70 
71 #define OPT_MMC_YAMAHA_FORCE_SPEED 0x0200
72                                     // drive supports Yamaha's Force Speed
73                                     // feature
74 
75 /*! \brief Driver for Generic MMC units
76 */
77 class GenericMMC : public CdrDriver {
78 public:
79 /*! \brief Default constructor
80     \param scsiIf As obtained from ScsiIf::ScsiIf
81     \param options Binary AND of options like OPT_MMC_USE_PQ and subsequent
82     \sa OPT_MMC_USE_PQ
83 */
84   GenericMMC(ScsiIf *scsiIf, unsigned long options);
85 /*! \brief Default destructor */
86   ~GenericMMC();
87 /*! \brief Static constructor
88     \sa GenericMMC::GenericMMC
89 */
90   static CdrDriver *instance(ScsiIf *scsiIf, unsigned long options);
91 
92 /*! \brief See what subchannel reading modes are available
93 
94     For each data/audio track it tries PQ subchannel (using GenericMMC::readCdTest);
95     it may result in:
96         - not supported
97         - BCD format
98         - HEX format
99         - unknown (error printout)
100 
101     Then it tries
102         - Raw PW
103         - Cooked PW
104 
105     You may skip some tests if you have initialized the driver with something
106     like OPT_MMC_USE_PQ.
107     \param CdToc* Toc of the disc, used to compute track lengths
108     \param int Number of tracks to use for the test
109     \return Binary OR of the capabilities.
110     \sa CDR_READ_CAP_DATA_PW_RAW and similar.
111 */
112   unsigned long getReadCapabilities(const CdToc *, int) const;
113 
114   /*! \brief MMC compatible drives take little endian samples
115       \return 0
116   */
bigEndianSamples()117   int bigEndianSamples() const { return 0; }
118 
119   /*! \brief Sanity checking of the TOC
120 
121     A first check is done using CdrDriver::checkToc.
122     Then, if CD TEXT capabilities are available, it checks CD TEXT
123     using Toc::checkCdTextData.
124 
125     \return 0 if OK, 1 if some warning, 2 on error
126   */
127   int checkToc(const Toc *);
128 
129   /*! \brief Sets read/write speed
130 
131     Uses SET CD SPEED command 0xBB.
132     Speeds are specified as multipliers, using 0 as means max value.
133     Write speed is set from protected variable GenericMMC::speed_.
134     \param int Read speed as multiplier.
135     \return 0 on success, 1 if SCSI command error
136   */
137   int speed(int);
138 
139   /*! \brief Returns current write speed
140 
141     Rebuilds the GenericMMC::driveInfo_ structure by reading
142     mode page 0x2A. Information is extracted from this structure.
143     \return Write speed (as multiplier) or 0 if SCSI error occurred.
144   */
145   int speed();
146 
147   bool rspeed(int);
148   int rspeed();
149 
150   /*! \brief Gathers various info about inserted disk.
151 
152     - Issues READ DISK INFORMATION 0x51. Obtains number of tracks and
153         sessions and disk completion status.
154 
155     - Issues READ TOC 0x43 with parameter 1 to get session info.
156 
157     - Reads ATIP data (READ TOC 0x43, parameter 4)
158     \return Pointer to protected attribute GenericMMC::diskInfo_
159   */
160   DiskInfo *diskInfo();
161 
162   /*! \brief Loads or ejects tray
163 
164     Uses START/STOP unit command 0x1B.
165     \param int 0 to load tray, 1 to eject
166     \return 0 on success, 1 on error
167   */
168   int loadUnload(int) const;
169 
170   /*! \brief Blanks a CDRW
171 
172     First the drive is put in simulation or write mode, as specified in
173     GenericMMC::simulate_ attribute. Then the disk is blanked with BLANK 0xA1.
174     The IMMED bit is not set, and the call will
175     block until the operation is completed. Drive status will be polled
176     every 2 seconds.
177     \param BlankingMode Either BLANK_FULL or BLANK_MINIMAL
178     \return 0 on success or 1 on error
179   */
180   int blankDisk(BlankingMode);
181 
182   /*! \brief Initializes internal stuff before a write session.
183 
184     - Setup CD-TEXT encoder if specified in GenericMMC::options_
185 
186     - Gets inserted disk info with GenericMMC::diskInfo
187 
188     - Sets write speed with GenericMMC::selectSpeed
189 
190     - Allocates new data buffer into GenericMMC::zeroBuffer_
191 
192     \param Toc* TOC of the disk you want to write
193     \return 0 on success, 1 on error
194   */
195   int initDao(const Toc *);
196 
197   /*! \brief Begins write process
198 
199     Sets write parameters (MP 0x05), performs power calibration if not
200     in simulation mode, sends cue sheet and writes leadin. The session LBA
201     is always -150. First 150 sectors are zeroed.
202     \return 0 on success, 1 on error
203   */
204   int startDao();
205 
206   /*! \brief Finishes a writing
207 
208     Flush device cache and wait for device to become ready. Since some drives
209     do not return to ready after writing a leadout the cache flush is done twice,
210     before and after the wait unit ready.
211     Then the CD-TEXT encoder and the zero buffer are deleted.
212     \return 0 on success, 1 on error
213   */
214   int finishDao();
215 
216   /*! \brief Aborts a writing
217 
218     Flush device cache and delete CD-TEXT encoder.
219   */
220   void abortDao();
221 
222   /*! \brief Writes data to target
223 
224     The block length depends on the actual writing. Writing is done using
225     WRITE10 (0x2A) command. Command is retried if device buffer is full.
226     \param TrackData::Mode Main channel writing mode
227     \param TrackData::SubChannelMode Subchannel writing mode
228     \param lba specifies the next logical block address for writing and is updated
229         by this function.
230     \param buf Data to be written
231     \param len Number of blocks to write
232     \return 0 if OK, 1 if WRITE command failed
233   */
234   int writeData(TrackData::Mode, TrackData::SubChannelMode, long &lba,
235 		const char *buf, long len);
236 
237   /*! \brief Gets useful parameters about the device.
238 
239     Gets mode page 0x2A and info about
240     - Burnproof
241     - Accurate audio stream
242     - Maximum read speed
243     - Current read speed
244     - Maximum write speed
245     - Maximum read speed
246     \param showErrorMsg If true print out errors
247     \return Pointer to the protected variable driveInfo_
248   */
249   const DriveInfo *driveInfo(bool showErrorMsg);
250 
251     /*!
252     Returns 0 or 1 depending on the SubChannelMode parameter.
253     - 0 if SUBCHAN_NONE
254     - 0 if SUBCHAN_RW
255     - 1 if SUBCHAN_RW_RAW
256     \param SubChannelMode One of TrackData::SubChannelMode
257     \return 0 or 1 (see above)
258   */
259   int subChannelEncodingMode(TrackData::SubChannelMode) const;
260 
261 protected:
262   /*! \brief Timeout for commands on the target */
263   int scsiTimeout_;
264   /*! \brief  Start of lead-in */
265   Msf leadInStart_;
266   /*! \brief Length of lead-in */
267   long leadInLen_;
268   /*! \brief Length if lead-out */
269   long leadOutLen_;
270   /*! \brief Various info about the inserted media
271       \sa diskInfo
272   */
273   DiskInfo diskInfo_;
274   /*! \brief Various info about the device
275       \sa driveInfo
276   */
277   DriveInfo *driveInfo_;
278   /*! Called by GenericMMC::initDao if needed. */
279   CdTextEncoder *cdTextEncoder_;
280 
281   /*! \brief Determines start and length of lead-in and length of lead-out.
282 
283     Uses READ DISK INFORMATION cmd 0x51.
284     \return 0 on success, 1 on SCSI error
285   */
286   virtual int getSessionInfo();
287   /*! \brief Gets next writable address
288 
289     Uses READ TRACK INFORMATION 0x52 on incomplete track 0xFF. Must return
290     RT=1 and NWA_V=1. LBA of next writable address is taken from bytes 12-15.
291     \param long* LBA of next writable address.
292     \return 0 on success, 1 on SCSI error
293   */
294   virtual int getNWA(long *);
295   /*! \brief Determines first writable address of data area of current empty session.
296 
297     Places the device in Session At Once write type and calls GenericMMC::getNWA
298     \param long* LBA of the beginning of data area
299     \return 0 on success, 1 on SCSI error.
300   */
301   virtual int getStartOfSession(long *);
302   /*! \brief Still unused */
303   virtual int getFeature(unsigned int feature, unsigned char *buf,
304 			 unsigned long bufLen, int showMsg);
305 
306   /*! \brief Reads Media Catalog Number
307 
308     If option OPT_MMC_SCAN_MCN is specified uses CdrDriver::readCatalogScan to
309     scan subchannels of sectors from startlba to endlba to find MCN and (if valid)
310     saves it into Toc. If OPT_MMC_SCAN_MCN isn't asserted uses proper command
311     READ SUBCHANNEL 0x42 with parameter 0x02 to do the same (and startlba, endlba
312     are unused).
313     \param Toc* Toc of the disk. MCN can be saved here
314     \param startLba Start subchannel scan here
315     \param endLba End subchannel scan here
316     \return 1 if valid MCN found, else 0
317   */
318   int readCatalog(Toc *, long startLba, long endLba);
319   /*! \brief Reads media ISRC
320 
321     Uses READ SUBCHANNEL 0x42 with parameter 0x03. Track is specified
322     by the int parameter.
323     If TCVAL == 1 ISRC is valid and is copied to char* parameter.
324     \param int Track to read ISRC of.
325     \param char* Pre-allocated buffer to store ISRC (12 bytes)
326     \return Always 0
327   */
328   int readIsrc(int, char *);
329 
330   /*! \brief Sets read/write speed
331 
332     Sets the read speed from readSpeed parameter, and write
333     speed from GenericMMC::speed_ protected attribute.
334     Both can be specified as 0 for maximum or as multiplier.
335     \param readSpeed Read speed to set: use 0 for max or multiplier
336     \todo This documentation needs to be updated
337     \return 1 on SCSI error, 0 on success
338   */
339   virtual int selectSpeed();
340 
341   /*! \brief Sets write parameters in page 0x05
342 
343     - Write type is 0x02 (SAO).
344     - Simulation is set from GenericMMC::simulate_ protected attrib.
345     - BurnFree is set if drive supports it, unless explicitly disabled.
346     - Next session opened if GenericMMC::multiSession_
347     - Data block type ranges from 0 to 3, depending on the CD-TEXT encoder.
348     \param variant Ranges from 0 to 3, defining modes 0-3 of Data Block Type
349         Codes in Write Mode Page 0x05.
350     \return 0 on success, 1 on SCSI error.
351   */
352   virtual int setWriteParameters(unsigned long variant);
353 
354   /*! \brief Puts drive in simulation/write mode
355 
356     Based on GenericMMC::simulate_ attrib.
357     \param showMessage If asserted print out errors.
358     \return 0 on success, 1 on SCSI errors
359   */
360   int setSimulationMode(int showMessage);
361 
362   /*! \brief Asks unit to perform power calibration
363 
364     Uses SEND OPC INFORMATION (0x54) command with DoOpc set to 1.
365     \return 0 if command not supported or power calibration successful,
366         1 on error
367   */
368   int performPowerCalibration();
369 
370   /*! \brief Free data bytes available in the device's buffer.
371 
372     Uses READ BUFFER CAPACITY 0x5C command.
373     \param capacity Will hold number of bytes.
374     \todo This documentation needs to be updated
375     \return 0 on success, 1 on SCSI command error.
376   */
377   bool readBufferCapacity(long *capacity, long *available);
378 
379   /*! \brief Creates cue sheet for current toc
380 
381     Called by GenericMMC::sendCueSheet to build cuesheets.
382     \param variant Ranges from 0 to 3, defining modes 0-3 of Data Block Type
383         Codes in Write Mode Page 0x05.
384     \param cueSheetLen Filled with cue sheet length in bytes
385     \return Newly allocated cue sheet buffer or 'NULL' on error
386   */
387   unsigned char *createCueSheet(unsigned long variant, long *cueSheetLen);
388 
389   /*! \brief Builds and sends cuesheet
390 
391     For each of the 4 available variants builds (using GenericMMC::sendCueSheet)
392     a cuesheet and sends to the device using SEND CUE SHEET 0x5D.
393     Stops when device accepts a variant.
394     \return 0 on success, 1 on error (no variants accepted)
395   */
396   int sendCueSheet();
397 
398   /*! \brief Returns subchannel SCSI standard code for cuesheet
399 
400     Used in GenericMMC::createCueSheet to obtain code for cuesheet.
401     \param TrackData::SubChannelMode One of SUBCHAN_NONE,SUBCHAN_RW,SUBCHAN_RW_RAW
402     \param encodingMode 0 or 1
403     \return int
404 		- 0 if SUBCHAN_NONE
405 		- 0x40 if encodingMode==0 and SUBCHAN_RW(_RAW)
406 		- 0x40 if encodingMode==1 and SUBCHAN_RW(_RAW)
407   */
408   unsigned char subChannelDataForm(TrackData::SubChannelMode,
409 				   int encodingMode);
410 
411   /*! \brief Writes the subchannel data in the leadin section, used in CD-TEXT
412 
413 	Issues WRITE (0x2A) commands to write PW subchannels starting
414 	at LBA = -150 - leadInLen_
415 	\return int
416 		- 0 if cdTextEncoder_ == NULL or everything OK
417 		- 1 if some error occurred (message output)
418   */
419   int writeCdTextLeadIn();
420 
421   int analyzeTrack(TrackData::Mode, int trackNr, long startLba, long endLba,
422 		   Msf *index,
423 		   int *indexCnt, long *pregap, char *isrcCode,
424 		   unsigned char *ctl);
425 
426   int getTrackIndex(long lba, int *trackNr, int *indexNr, unsigned char *ctl);
427 
428   int readSubChannels(TrackData::SubChannelMode, long lba, long len,
429 		      SubChannel ***, Sample *);
430 
431   /*!
432   \brief retrieve mode of the track that starts at the specified trackStartLba.
433 
434   Uses READ CD (0xBE) command with byte 9 == 0xF8 (SYNC + Header + Userdata + EDC_ECC + C2 and block errors,
435   No subchannels) to retrieve the sector addressed as trackStartLba. Uses
436   CdrDriver::determineSectorMode to identify track mode.
437   \return TrackData::Mode
438   */
439   TrackData::Mode getTrackMode(int, long trackStartLba);
440 
441   CdRawToc *getRawToc(int sessionNr, int *len);
442 
443   long readTrackData(TrackData::Mode, TrackData::SubChannelMode,
444 		     long lba, long len, unsigned char *buf);
445 
446   int readAudioRange(ReadDiskInfo *, int fd, long start, long end,
447 		     int startTrack, int endTrack, TrackInfo *);
448 
449   int readCdTest(long lba, long len, int subChanMode) const;
450 
451   /*!
452   \brief Checks for ready status of the drive after a write operation. Disc must be inserted.
453 
454   Unit should return successfully from TEST UNIT READY command, and from READ DISC INFORMATION.
455   \return int
456 	- 0 drive ready
457 	- 1 error occured at os level, no sense data
458 	- 2 NOT READY,LONG WRITE IN PROGRESS
459   */
460   int checkDriveReady() const;
461 
462   int RicohGetWriteOptions();
463   int RicohSetWriteOptions(const DriveInfo *);
464 };
465 
466 #endif
467