1 /* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
2 
3 /* Copyright (c) 2004 - 2006 Derek Foreman, Ben Jansens
4    Copyright (c) 2006 - 2020 Thomas Schmitt <scdbackup@gmx.net>
5    Provided under GPL version 2 or later.
6 */
7 
8 
9 #ifndef __TRANSPORT
10 #define __TRANSPORT
11 
12 #include "libburn.h"
13 #include "os.h"
14 
15 #include <pthread.h>
16 /* sg data structures */
17 #include <sys/types.h>
18 
19 
20 /* see os.h for name of particular os-*.h where this is defined */
21 #define BUFFER_SIZE BURN_OS_TRANSPORT_BUFFER_SIZE
22 
23 
24 enum transfer_direction
25 { TO_DRIVE, FROM_DRIVE, NO_TRANSFER };
26 
27 /* end of sg data structures */
28 
29 /* generic 'drive' data structures */
30 
31 struct cue_sheet
32 {
33 	int count;
34 	unsigned char *data;
35 };
36 
37 struct params
38 {
39 	int speed;
40 	int retries;
41 };
42 
43 struct buffer
44 {
45 	/* ts A61219:
46 	   Added 4096 bytes reserve against possible buffer overflows.
47 	   (Changed in sector.c buffer flush test from >= to > BUFFER_SIZE .
48 	    This can at most cause a 1 sector overlap. Sometimes an offset
49 	    of 16 byte is applied to the output data (in some RAW mode). )
50 	    burn_write_opts.cdxa_conversion can imply an offset of 8 bytes.
51 	 */
52 	unsigned char data[BUFFER_SIZE + 4096];
53 	int sectors;
54 	int bytes;
55 };
56 
57 struct command
58 {
59 	unsigned char opcode[16];
60 	int oplen;
61 	int dir;
62 	int dxfer_len;
63 	unsigned char sense[128];
64 	int sense_len;
65 	int error;
66 	int retry;
67 	struct buffer *page;
68 	int timeout; /* milliseconds */
69 
70 	double start_time;
71 	double end_time;
72 
73 	int retry_count;
74 	int last_retry_key;
75 	int last_retry_asc;
76 	int last_retry_ascq;
77 };
78 
79 struct burn_scsi_inquiry_data
80 {
81 	char peripheral; /* bit0-4: device type should be 5
82 	                    bit5-7: qualifier must be 0 */
83 
84 	char version;    /* should be 3 (SPC-1) to 5 (SPC-3) (or higher ?)
85 	                    but is often 0. */
86 
87 	char vendor[9];
88 	char product[17];
89 	char revision[5];
90 
91 	int valid;
92 };
93 
94 
95 struct scsi_mode_data
96 {
97 	int p2a_valid;
98 	int buffer_size;
99 	int dvdram_read;
100 	int dvdram_write;
101 	int dvdr_read;
102 	int dvdr_write;
103 	int dvdrom_read;
104 	int cdrw_read;
105 	int cdrw_write;
106 	int cdr_read;
107 	int cdr_write;
108 	int simulate;
109 	int c2_pointers;
110 	int underrun_proof;
111 
112 	int max_read_speed;
113 	int cur_read_speed;
114 	int max_write_speed;
115 	int cur_write_speed;
116 
117 	/* ts A61021 */
118 	int min_write_speed;
119 
120 	/* ts A61225 : Results from ACh GET PERFORMANCE, Type 03h
121 	               Speed values go into *_*_speed */
122 	int min_end_lba;
123 	int max_end_lba;
124 	struct burn_speed_descriptor *speed_descriptors;
125 
126 	int retry_page_length;
127 	int retry_page_valid;
128 	int write_page_length;
129 	int write_page_valid;
130 };
131 
132 
133 /* ts A70112 : represents a single Formattable Capacity Descriptor as of
134                mmc5r03c.pdf 6.24.3.3 . There can at most be 32 of them. */
135 struct burn_format_descr {
136 	/* format type: e.g 0x00 is "Full", 0x15 is "Quick" */
137 	int type;
138 
139 	/* the size in bytes derived from Number of Blocks */
140 	off_t size;
141 
142 	/* the Type Dependent Parameter (usually the write alignment size) */
143 	unsigned int tdp;
144 };
145 
146 
147 /* ts B40106 : represents a Feature Descriptor as of mmc5r03c.pdf 5.2.2
148                There can be many of them. Thus a linked list.
149 */
150 struct burn_feature_descr {
151 	unsigned short feature_code;
152 
153 	unsigned char  flags; /* bit0= current
154                                  bit1= persistent
155                                  bit2-5= version
156                               */
157 
158         unsigned char  data_lenght;
159 
160 	/* Additional bytes after the first 4 bytes of the descriptor */
161         unsigned char *data;
162 
163         struct burn_feature_descr *next;
164 };
165 
166 
167 /** Gets initialized in enumerate_common() and burn_drive_register() */
168 struct burn_drive
169 {
170 	/* ts A70902:
171 		0=null-emulation
172 		1=MMC drive ,
173 		2=stdio random read-write
174 		3=stdio sequential write-only
175                 4=stdio random read-only
176                 5=stdio random write-only
177 	*/
178 	int drive_role;
179 
180 	int bus_no;
181 	int host;
182 	int id;
183 	int channel;
184 	int lun;
185 	char *devname;
186 
187 	/* ts A70302: mmc5r03c.pdf 5.3.2 Physical Interface Standard */
188 	int phys_if_std;   /* 1=SCSI, 2=ATAPI, 3,4,6=FireWire, 7=SATA, 8=USB */
189 	char phys_if_name[80];  /* MMC-5 5.3.2 table 91 , e.g. "SCSI Family" */
190 
191 	/* see os.h for name of particular os-*.h where this is defined */
192 	BURN_OS_TRANSPORT_DRIVE_ELEMENTS
193 
194 
195 	/* ts A60904 : ticket 62, contribution by elmom */
196 	/**
197 	    Tells the index in scanned burn_drive_info array.
198 	    -1 if fallen victim to burn_drive_info_forget()
199 	*/
200 	int global_index;
201 
202 	pthread_mutex_t access_lock;
203 
204 	enum burn_disc_status status;
205 	int erasable;
206 
207 	/* ts A61201 from 46h GET CONFIGURATION  */
208 	int current_profile;
209 	char current_profile_text[80];
210 	int current_is_cd_profile;
211 	int current_is_supported_profile;
212 	/* ts A90603 */
213 	int current_is_guessed_profile;
214 	/* ts A90815 */
215 	unsigned char all_profiles[256];
216 	int num_profiles;
217 
218 	/* ts B40106 : All feature descriptors as read from drive */
219 	struct burn_feature_descr *features;
220 
221 	/* ts A70128 : MMC-to-MMC feature info from 46h for DVD-RW.
222            Quite internal. Regard as opaque :)
223 	*/
224 	/* 1 = incremental recording available, 0 = not available */
225 	int current_has_feat21h;
226 
227 	/* Some drives announce feature 21h on fast-blanked DVD-RW
228 	   although they cannot write them in Incremental mode.
229 	   0= does not look like the recent write run failed due to
230 	      Incremental on fast blanked DVD-RW
231 	   1= it seems to have happened
232 	   2= it seems to have happened with write address 0
233 	*/
234 	int was_feat21h_failure;
235 
236 	/* Link Size item number 0 from feature 0021h descriptor */
237 	int current_feat21h_link_size;
238 
239 	/* Flags from feature 0023h for formatting BD mmc5r03c.pdf 5.3.13
240            Byte 4 BD-RE:
241              bit0= Cert   format 30h sub-type 10b
242              bit1= QCert  format 30h sub-type 11b
243              bit2= Expand format 01h
244              bit3= RENoSA format 31h
245            Byte 8 BD-R:
246              bit0= RRM    format 32h sub-type 10b
247         */
248 	int current_feat23h_byte4;
249 	int current_feat23h_byte8;
250 
251 
252 	/* Flags from feature 002Fh feature descriptor mmc5r03c.pdf 5.3.25 :
253 	     bit1= DVD-RW supported
254 	     bit2= Test Write available
255 	     bit3= DVD-R DL supported
256 	     bit6= Buffer Under-run Free recording available (page 05h BUFE)
257 	   Value -1 indicates that no 002Fh was current in the features list.
258 	*/
259 	int current_feat2fh_byte4;
260 
261 	/* ts B51016 : Result from feature 108h : Drive Serial Number
262 	*/
263 	char *drive_serial_number;
264 	int drive_serial_number_len;
265 
266 	/* ts B51016 : Result from command AB READ MEDIA SERIAL NUMBER
267 	*/
268 	char *media_serial_number;
269 	int media_serial_number_len;
270 
271 	/* ts B10524 : whether the damage bit was set for the future track.
272 	               bit0= damage bit , bit1= nwa valid bit
273 	*/
274 	int next_track_damaged;
275 
276 	/* ts A70114 : whether a DVD-RW media holds an incomplete session
277 	               (which could need closing after write) */
278 	int needs_close_session;
279 	/* ts A71003 : whether a random write operation was done and no
280 	               synchronize cache has happened yet */
281 	int needs_sync_cache;
282 
283 	/* ts A80412 : whether to use WRITE12 with Streaming bit set
284 	               rather than WRITE10. Speeds up DVD-RAM. Might help
285 	               with BD-RE */
286 	int do_stream_recording;
287 
288         /* ts A90227 : the LBA where stream recording shall start.
289                        Writing to lower LBA will be done without streaming.
290         */
291         int stream_recording_start;
292 
293 	/* ts A61218 from 51h READ DISC INFORMATION */
294 	int last_lead_in;
295 	int last_lead_out;
296 	int num_opc_tables;   /* ts A91104: -1= not yet known */
297 	int bg_format_status; /* 0=needs format start, 1=needs format restart*/
298 	int disc_type; /* 0="CD-DA or CD-ROM", 0x10="CD-I", 0x20="CD-ROM XA" */
299 	unsigned int disc_id; /* a "32 bit binary integer" */
300 	char disc_bar_code[9];
301 	int disc_app_code;
302 	int disc_info_valid;  /* bit0= disc_type ,     bit1= disc_id ,
303 				 bit2= disc_bar_code , bit3= disc_app_code
304 				 bit4= URU bit is set (= unrestricted use)
305 				 bit5= Erasable bit was set in reply
306 			       */
307 
308 	/* ts A70108 from 23h READ FORMAT CAPACITY mmc5r03c.pdf 6.24 */
309 	int format_descr_type;      /* 1=unformatted, 2=formatted, 3=unclear */
310 	off_t format_curr_max_size;  /* meaning depends on format_descr_type */
311 	unsigned int format_curr_blsas;  /* dito */
312 	int best_format_type;
313 	off_t best_format_size;
314 
315 	/* The complete list of format descriptors as read with 23h */
316 	int num_format_descr;
317 	struct burn_format_descr format_descriptors[32];
318 
319 
320 	volatile int released;
321 
322 	/* ts A61106 */
323 	/* 0= report errors
324 	   1= do not report errors
325 	   2= do not report errors which the libburn function indicates in
326 	      member .had_particular_error
327 	   3= report errors with severity DEBUG
328 	*/
329 	int silent_on_scsi_error;
330 
331 	/* ts B21023 */
332 	/* bit0= 5 64 00 occurred with READ10 in mmc_read_10()
333 	*/
334 	int had_particular_error;
335 
336 	int stdio_fd;
337 
338 	int nwa;		/* next writeable address */
339 	int alba;		/* absolute lba */
340 	int rlba;		/* relative lba in section */
341 	int start_lba;
342 	int end_lba;
343 
344 	/* ts B61116 */
345 	int do_simulate;
346 
347 	/* ts A70131 : from 51h READ DISC INFORMATION Number of Sessions (-1)*/
348 	int complete_sessions;
349 	/* ts A90107 */
350 	int state_of_last_session;
351 
352 #ifdef Libburn_disc_with_incomplete_sessioN
353 	/* ts B30112 */
354 	int incomplete_sessions;
355 #endif
356 
357 
358 	/* ts A70129 :
359 	   from 51h READ DISC INFORMATION Last Track Number in Last Session */
360 	int last_track_no;
361 
362 	/* ts B10730 : whether a default mode page 05 was already sent.
363 	*/
364 	int sent_default_page_05;
365 	/* ts A70212 : from various sources : free space on media (in bytes)
366 	               With CD this might change after particular write
367 	               parameters have been set and nwa has been inquired.
368 	               (e.g. by d->send_write_parameters() ; d->get_nwa()).
369 	*/
370 	off_t media_capacity_remaining;
371 	/* ts A70215 : if > 0 : first lba on media that is too high for write*/
372 	int media_lba_limit;
373 
374 	/* ts A81210 : Upper limit of readable data size,
375 	               0x7fffffff = unknown
376 	               0x7ffffff0 = 32 bit overflow, or unknown stdio size
377 	 */
378 	int media_read_capacity;
379 	/* ts B60305 : Whether READ CAPACITY of CD is credible:
380 	               -1= no READ CAPACITY yet , 0= untrusted READ CAPACITY
381 	               1= READ CAPACITY confirmed or corrected by other commands
382 	*/
383 	int mr_capacity_trusted;
384 
385 	/* ts B10314 : Next Writeable Address for drive_role == 5 */
386         int role_5_nwa;
387 
388 	/* ts B60730 */
389 	int do_no_immed;
390 
391 	int toc_temp;
392 	struct burn_disc *disc;	/* disc structure */
393 	int block_types[4];
394 	struct buffer *buffer;
395 	struct burn_progress progress;
396 
397 	/* To be used by mmc.c, sbc.c, spc.c for SCSI commands where the struct
398 	   content surely does not have to persist while another command gets
399 	   composed and executed.
400 	   (Inherently, sending SCSI commands to the same drive cannot be
401 	    thread-safe. But there are functions which send SCSI commands
402 	    and also call other such functions. These shall use own allocated
403 	    command structs and not this struct here.)
404 	*/
405 	struct command casual_command;
406 
407 	/* ts A70711 : keeping an eye on the drive buffer */
408 	off_t pessimistic_buffer_free;
409 	int pbf_altered;
410 	int wait_for_buffer_free;
411 	int nominal_write_speed;
412 	unsigned int wfb_min_usec;
413 	unsigned int wfb_max_usec;
414 	unsigned int wfb_timeout_sec;
415 	unsigned int wfb_min_percent;
416 	unsigned int wfb_max_percent;
417 	unsigned int pessimistic_writes;
418 	unsigned int waited_writes;
419 	unsigned int waited_tries;
420 	unsigned int waited_usec;
421 
422 	volatile int cancel;
423 	volatile enum burn_drive_status busy;
424 
425 	/* During write runs, this points to a copy of the applied
426 	   struct burn_write_opts. Only read this underneath
427 	   burn_disc_write_sync() which removes the copy when done.
428 	   Especially do not read it from outside the write thread.
429 	*/
430 	struct burn_write_opts *write_opts;
431 
432 	/* ts A70929 */
433 	pid_t thread_pid;
434 	int thread_pid_valid;
435 	/* ts B00225 */
436 	pthread_t thread_tid;
437 
438 	/* ts B90513 */
439 	unsigned int write_retry_count;
440 
441 	/* ts C00806 */
442 	/* 0=no change, 1=change, -1=already urged OS to revalidate medium */
443 	int medium_state_changed;
444 
445 	/* ts C00822 */
446 	/* If set, use Exact bit with SET STREAMING and use SET STREAMING
447 	   even if the medium is a CD.
448 	 */
449 	int set_streaming_exact_bit;
450 	int set_streaming_err;
451 
452 /* transport functions */
453 	int (*grab) (struct burn_drive *);
454 	int (*release) (struct burn_drive *);
455 
456 	/* ts A61021 */
457 	int (*drive_is_open) (struct burn_drive *);
458 
459 	int (*issue_command) (struct burn_drive *, struct command *);
460 
461 /* lower level functions */
462 	void (*erase) (struct burn_drive *, int);
463 	void (*getcaps) (struct burn_drive *);
464 
465 	/* ts A61021 */
466 	void (*read_atip) (struct burn_drive *);
467 
468 	int (*write) (struct burn_drive *, int, struct buffer *);
469 	void (*read_toc) (struct burn_drive *);
470 	void (*lock) (struct burn_drive *);
471 	void (*unlock) (struct burn_drive *);
472 	void (*eject) (struct burn_drive *);
473 	void (*load) (struct burn_drive *);
474 	int (*start_unit) (struct burn_drive *);
475 
476 	/* ts A90824 : Calming down noisy drives */
477 	int (*stop_unit) (struct burn_drive *);
478 	int is_stopped;
479 
480 	void (*read_disc_info) (struct burn_drive *);
481 	int (*read_cd) (struct burn_drive *, int start, int len,
482 	                int sec_type, int main_ch,
483 	                const struct burn_read_opts *, struct buffer *,
484 	                int flag);
485 	void (*perform_opc) (struct burn_drive *);
486 	void (*set_speed) (struct burn_drive *, int, int);
487 	void (*send_parameters) (struct burn_drive *,
488 				 const struct burn_read_opts *);
489 	void (*send_write_parameters) (struct burn_drive *,
490 				       struct burn_session *, int tno,
491 				       const struct burn_write_opts *);
492 	int (*send_cue_sheet) (struct burn_drive *, struct cue_sheet *);
493 
494 	/* ts A70205 : Announce size of a DVD-R[W] DAO session. */
495 	int (*reserve_track) (struct burn_drive *d, off_t size);
496 
497 	void (*sync_cache) (struct burn_drive *);
498 	int (*get_erase_progress) (struct burn_drive *);
499 	int (*get_nwa) (struct burn_drive *, int trackno, int *lba, int *nwa);
500 
501 	/* ts A70131 : obtain (possibly fake) TOC number and start lba of
502 			first track in last complete session */
503 	int (*read_multi_session_c1)(struct burn_drive *d,
504 				     int *trackno, int *start);
505 
506 	/* ts A61009 : removed d in favor of o->drive */
507 	/* void (*close_disc) (struct burn_drive * d,
508 				 struct burn_write_opts * o);
509 	   void (*close_session) (struct burn_drive * d,
510 			       struct burn_write_opts * o);
511 	*/
512 	void (*close_disc) (struct burn_write_opts * o);
513 	void (*close_session) ( struct burn_write_opts * o);
514 
515 	/* ts A61029 */
516 	void (*close_track_session) ( struct burn_drive *d,
517 				int session, int track);
518 
519 	int (*test_unit_ready) (struct burn_drive * d);
520 	void (*probe_write_modes) (struct burn_drive * d);
521 	struct params params;
522 	struct burn_scsi_inquiry_data *idata;
523 	struct scsi_mode_data *mdata;
524 	int toc_entries;
525 	struct burn_toc_entry *toc_entry;
526 
527 	/* ts A61023 : get size and free space of drive buffer */
528 	int (*read_buffer_capacity) (struct burn_drive *d);
529 
530 	/* ts A61220 : format media (e.g. DVD+RW) */
531 	int (*format_unit) (struct burn_drive *d, off_t size, int flag);
532 
533 	/* ts A70108 */
534 	/* mmc5r03c.pdf 6.24 : get list of available formats */
535 	int (*read_format_capacities) (struct burn_drive *d, int top_wanted);
536 
537 	/* ts A70812 */
538 	/* mmc5r03c.pdf 6.15 : read data sectors (start and amount in LBA) */
539 	int (*read_10) (struct burn_drive *d, int start, int amount,
540 	                struct buffer *buf);
541 
542 };
543 
544 /* end of generic 'drive' data structures */
545 
546 /* ts A80422 : centralizing this setting for debugging purposes
547 */
548 int burn_drive_set_media_capacity_remaining(struct burn_drive *d, off_t value);
549 
550 
551 #endif /* __TRANSPORT */
552