1 /* @(#)scsi_cmds.c 1.57 19/04/02 Copyright 1998-2002,2015,2019 Heiko Eissfeldt, Copyright 2004-2019 J. Schilling */
2 #include "config.h"
3 #ifndef lint
4 static UConst char sccsid[] =
5 "@(#)scsi_cmds.c 1.57 19/04/02 Copyright 1998-2002,2015,2019 Heiko Eissfeldt, Copyright 2004-2019 J. Schilling";
6 #endif
7 /*
8 * file for all SCSI commands
9 * FUA (Force Unit Access) bit handling copied from Monty's cdparanoia.
10 */
11 /*
12 * The contents of this file are subject to the terms of the
13 * Common Development and Distribution License, Version 1.0 only
14 * (the "License"). You may not use this file except in compliance
15 * with the License.
16 *
17 * See the file CDDL.Schily.txt in this distribution for details.
18 * A copy of the CDDL is also available via the Internet at
19 * http://www.opensource.org/licenses/cddl1.txt
20 *
21 * When distributing Covered Code, include this CDDL HEADER in each
22 * file and include the License file CDDL.Schily.txt from this distribution.
23 */
24
25 #undef DEBUG_FULLTOC
26 #undef WARN_FULLTOC
27 #define TESTSUBQFALLBACK 0
28
29 #include "config.h"
30 #include <schily/stdio.h>
31 #include <schily/standard.h>
32 #include <schily/types.h>
33 #include <schily/stdlib.h>
34 #include <schily/string.h>
35 #include <schily/schily.h>
36 #include <schily/nlsdefs.h>
37
38 #include <schily/btorder.h>
39
40 #define g5x_cdblen(cdb, len) ((cdb)->count[0] = ((len) >> 16L)& 0xFF,\
41 (cdb)->count[1] = ((len) >> 8L) & 0xFF,\
42 (cdb)->count[2] = (len) & 0xFF)
43
44
45 #include <scg/scgcmd.h>
46 #include <scg/scsidefs.h>
47 #include <scg/scsireg.h>
48
49 #include <scg/scsitransp.h>
50
51 #include "mytype.h"
52 #include "cdda2wav.h"
53 #include "interface.h"
54 #include "byteorder.h"
55 #include "global.h"
56 #include "cdrecord.h"
57 #include "toc.h"
58 #include "scsi_cmds.h"
59 #include "exitcodes.h"
60
61 unsigned char *bufferTOC; /* Global TOC data buffer */
62 int bufTOCsize; /* Size of global TOC buffer */
63 subq_chnl *SubQbuffer; /* Global Sub-channel buffer */
64
65 static unsigned ReadFullTOCSony __PR((SCSI *scgp));
66 static unsigned ReadFullTOCMMC __PR((SCSI *scgp));
67
68
69 int
SCSI_emulated_ATAPI_on(scgp)70 SCSI_emulated_ATAPI_on(scgp)
71 SCSI *scgp;
72 {
73 /* return (is_atapi);*/
74 if (scg_isatapi(scgp) > 0)
75 return (TRUE);
76
77 (void) allow_atapi(scgp, TRUE);
78 return (allow_atapi(scgp, TRUE));
79 }
80
81 int
heiko_mmc(scgp)82 heiko_mmc(scgp)
83 SCSI *scgp;
84 {
85 unsigned char mode[0x100];
86 int was_atapi;
87 struct cd_mode_page_2A *mp;
88 int retval;
89
90 fillbytes((caddr_t)mode, sizeof (mode), '\0');
91
92 was_atapi = allow_atapi(scgp, 1);
93 scgp->silent++;
94 mp = mmc_cap(scgp, mode);
95 scgp->silent--;
96 allow_atapi(scgp, was_atapi);
97 if (mp == NULL)
98 return (0);
99
100 /*
101 * have a look at the capabilities
102 */
103 if (mp->cd_da_supported == 0) {
104 retval = -1;
105 } else {
106 retval = 1 + mp->cd_da_accurate;
107 }
108 return (retval);
109 }
110
111
112 int accepts_fua_bit; /* Use the Force Unit Access bit */
113 unsigned char density = 0;
114 unsigned char orgmode4 = 0;
115 unsigned char orgmode10, orgmode11;
116
117 /*
118 * get current sector size from SCSI cdrom drive
119 */
120 unsigned int
get_orig_sectorsize(scgp,m4,m10,m11)121 get_orig_sectorsize(scgp, m4, m10, m11)
122 SCSI *scgp;
123 unsigned char *m4;
124 unsigned char *m10;
125 unsigned char *m11;
126 {
127 /*
128 * first get current values for density, etc.
129 */
130 static unsigned char *modesense = NULL;
131
132 if (modesense == NULL) {
133 modesense = malloc(12);
134 if (modesense == NULL) {
135 errmsg(
136 _("Cannot allocate memory for mode sense command in line %d.\n"),
137 __LINE__);
138 return (0);
139 }
140 }
141
142 /*
143 * do the scsi cmd
144 */
145 if (scgp->verbose)
146 fprintf(stderr, _("\nget density and sector size..."));
147 if (mode_sense(scgp, modesense, 12, 0x01, 0) < 0)
148 fprintf(stderr, _("get_orig_sectorsize mode sense failed\n"));
149
150 /*
151 * FIXME: some drives dont deliver block descriptors !!!
152 */
153 if (modesense[3] == 0)
154 return (0);
155
156 #if 0
157 modesense[4] = 0x81;
158 modesense[10] = 0x08;
159 modesense[11] = 0x00;
160 #endif
161
162 if (m4 != NULL) /* density */
163 *m4 = modesense[4];
164 if (m10 != NULL) /* MSB sector size */
165 *m10 = modesense[10];
166 if (m11 != NULL) /* LSB sector size */
167 *m11 = modesense[11];
168
169 return ((modesense[10] << 8) + modesense[11]);
170 }
171
172
173 /*
174 * switch CDROM scsi drives to given sector size
175 */
176 int
set_sectorsize(scgp,secsize)177 set_sectorsize(scgp, secsize)
178 SCSI *scgp;
179 unsigned int secsize;
180 {
181 static unsigned char mode [4 + 8];
182 int retval;
183
184 if (orgmode4 == 0xff) {
185 get_orig_sectorsize(scgp, &orgmode4, &orgmode10, &orgmode11);
186 }
187 if (orgmode4 == 0x82 && secsize == 2048)
188 orgmode4 = 0x81;
189
190 /*
191 * prepare to read cds in the previous mode
192 */
193 fillbytes((caddr_t)mode, sizeof (mode), '\0');
194 mode[ 3] = 8; /* Block Descriptor Length */
195 mode[ 4] = orgmode4; /* normal density */
196 mode[10] = secsize >> 8; /* block length "msb" */
197 mode[11] = secsize & 0xFF; /* block length lsb */
198
199 if (scgp->verbose)
200 fprintf(stderr, _("\nset density and sector size..."));
201 /*
202 * do the scsi cmd
203 */
204 if ((retval = mode_select(scgp, mode, 12, 0,
205 scgp->inq->data_format >= 2)) < 0)
206 errmsgno(EX_BAD, _("Setting sector size failed.\n"));
207
208 return (retval);
209 }
210
211
212 /*
213 * switch Toshiba/DEC and HP drives from/to cdda density
214 */
215 void
EnableCddaModeSelect(scgp,fAudioMode,uSectorsize)216 EnableCddaModeSelect(scgp, fAudioMode, uSectorsize)
217 SCSI *scgp;
218 int fAudioMode;
219 unsigned uSectorsize;
220 {
221 /*
222 * reserved, Medium type=0, Dev spec Parm = 0,
223 * block descriptor len 0 oder 8,
224 * Density (cd format)
225 * (0=YellowBook, XA Mode 2=81h,
226 * XA Mode1=83h and raw audio via SCSI=82h),
227 * # blks msb, #blks, #blks lsb, reserved,
228 * blocksize, blocklen msb, blocklen lsb,
229 */
230
231 /*
232 * MODE_SELECT, page = SCSI-2 save page disabled, reserved, reserved,
233 * parm list len, flags
234 */
235 static unsigned char mode [4 + 8] = {
236 /*
237 * mode section
238 */
239 0,
240 0, 0,
241 8, /* Block Descriptor Length */
242 /*
243 * block descriptor
244 */
245 0, /* Density Code */
246 0, 0, 0, /* # of Blocks */
247 0, /* reserved */
248 0, 0, 0}; /* Blocklen */
249
250 if (orgmode4 == 0 && fAudioMode) {
251 if (0 == get_orig_sectorsize(scgp,
252 &orgmode4, &orgmode10, &orgmode11)) {
253 /*
254 * cannot retrieve density, sectorsize
255 */
256 orgmode10 = (CD_FRAMESIZE >> 8L);
257 orgmode11 = (CD_FRAMESIZE & 0xFF);
258 }
259 }
260
261 if (fAudioMode) {
262 /*
263 * prepare to read audio cdda
264 */
265 mode [4] = density; /* cdda density */
266 mode [10] = (uSectorsize >> 8L); /* block length "msb" */
267 mode [11] = (uSectorsize & 0xFF); /* block length "lsb" */
268 } else {
269 /*
270 * prepare to read cds in the previous mode
271 */
272 mode [4] = orgmode4; /* 0x00; normal density */
273 mode [10] = orgmode10; /* (CD_FRAMESIZE >> 8L); block length "msb" */
274 mode [11] = orgmode11; /* (CD_FRAMESIZE & 0xFF); block length lsb */
275 }
276
277 if (scgp->verbose) {
278 fprintf(stderr,
279 _("\nset density/sector size (EnableCddaModeSelect)...\n"));
280 }
281 /*
282 * do the scsi cmd
283 */
284 if (mode_select(scgp, mode, 12, 0, scgp->inq->data_format >= 2) < 0)
285 errmsgno(EX_BAD, _("Audio mode switch failed.\n"));
286 }
287
288
289 /*
290 * read CD Text information from the table of contents
291 */
292 void
ReadTocTextSCSIMMC(scgp)293 ReadTocTextSCSIMMC(scgp)
294 SCSI *scgp;
295 {
296 short datalength;
297
298 #if 1
299 /*
300 * READTOC, MSF, format, res, res, res, Start track/session, len msb,
301 * len lsb, control
302 */
303 unsigned char *p = (unsigned char *)global.buf;
304 register struct scg_cmd *scmd = scgp->scmd;
305
306 scgp->silent++;
307 unit_ready(scgp);
308 scgp->silent--;
309
310 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
311 scmd->addr = (caddr_t)global.buf;
312 scmd->size = 4;
313 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
314 scmd->cdb_len = SC_G1_CDBLEN;
315 scmd->sense_len = CCS_SENSE_LEN;
316 scmd->cdb.g1_cdb.cmd = 0x43; /* Read TOC command */
317 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
318 scmd->cdb.g1_cdb.addr[0] = 5; /* format field */
319 scmd->cdb.g1_cdb.res6 = 0; /* track/session is reserved */
320 g1_cdblen(&scmd->cdb.g1_cdb, 4);
321
322 scgp->silent++;
323 if (scgp->verbose)
324 fprintf(stderr, _("\nRead TOC CD Text size ..."));
325
326 scgp->cmdname = "read toc size (text)";
327
328 if (scg_cmd(scgp) < 0) {
329 scgp->silent--;
330 if (global.quiet != 1) {
331 errmsgno(EX_BAD,
332 _("Read TOC CD Text failed (probably not supported).\n"));
333 }
334 p[0] = p[1] = '\0';
335 return;
336 }
337 scgp->silent--;
338
339 datalength = (p[0] << 8) | (p[1]);
340 if (datalength <= 2)
341 return;
342 datalength += 2;
343 if ((datalength) > global.bufsize)
344 datalength = global.bufsize;
345
346 scgp->silent++;
347 unit_ready(scgp);
348 scgp->silent--;
349
350 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
351 scmd->addr = (caddr_t)global.buf;
352 scmd->size = datalength;
353 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
354 scmd->cdb_len = SC_G1_CDBLEN;
355 scmd->sense_len = CCS_SENSE_LEN;
356 scmd->cdb.g1_cdb.cmd = 0x43; /* Read TOC command */
357 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
358 scmd->cdb.g1_cdb.addr[0] = 5; /* format field */
359 scmd->cdb.g1_cdb.res6 = 0; /* track/session is reserved */
360 g1_cdblen(&scmd->cdb.g1_cdb, datalength);
361
362 scgp->silent++;
363 if (scgp->verbose) {
364 fprintf(stderr,
365 _("\nRead TOC CD Text data (length %d)..."), (int)datalength);
366 }
367 scgp->cmdname = "read toc data (text)";
368
369 if (scg_cmd(scgp) < 0) {
370 if (global.quiet != 1) {
371 errmsgno(EX_BAD,
372 _("Read TOC CD Text data failed (probably not supported).\n"));
373 }
374 p[0] = p[1] = '\0';
375 unit_ready(scgp);
376 scgp->silent--;
377 return;
378 }
379 scgp->silent--;
380 #else
381 {
382 FILE *fp;
383 int read_;
384
385 /*fp = fopen("PearlJam.cdtext", "rb");*/
386 /*fp = fopen("celine.cdtext", "rb");*/
387 fp = fopen("japan.cdtext", "rb");
388 if (fp == NULL) {
389 errmsg(_("Cannot open '%s'.\n"), "japan.cdtext");
390 return;
391 }
392 fillbytes(global.buf, global.bufsize, '\0');
393 read_ = fread(global.buf, 1, global.bufsize, fp);
394 fprintf(stderr, _("read %d bytes. sizeof(buffer)=%u\n"),
395 read_, global.bufsize);
396 datalength = ((global.buf[0] & 0xFF) << 8) | (global.buf[1] & 0xFF) + 2;
397 fclose(fp);
398 }
399 #endif
400 }
401
402 /*
403 * read the full TOC
404 */
405 static unsigned
ReadFullTOCSony(scgp)406 ReadFullTOCSony(scgp)
407 SCSI *scgp;
408 {
409 /*
410 * READTOC, MSF, format, res, res, res, Start track/session, len msb,
411 * len lsb, control
412 */
413 register struct scg_cmd *scmd = scgp->scmd;
414 unsigned tracks = 99;
415
416 scgp->silent++;
417 unit_ready(scgp);
418 scgp->silent--;
419
420 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
421 scmd->addr = (caddr_t)bufferTOC;
422 scmd->size = 4 + (3 + tracks + 6) * 11;
423 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
424 scmd->cdb_len = SC_G1_CDBLEN;
425 scmd->sense_len = CCS_SENSE_LEN;
426 scmd->cdb.g1_cdb.cmd = 0x43; /* Read TOC command */
427 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
428 scmd->cdb.g1_cdb.res6 = 1; /* session */
429 g1_cdblen(&scmd->cdb.g1_cdb, 4 + (3 + tracks + 6) * 11);
430 scmd->cdb.g1_cdb.vu_97 = 1; /* format */
431
432 scgp->silent++;
433 if (scgp->verbose)
434 fprintf(stderr, _("\nRead Full TOC Sony ..."));
435
436 scgp->cmdname = "read full toc sony";
437
438 if (scg_cmd(scgp) < 0) {
439 if (global.quiet != 1) {
440 errmsgno(EX_BAD,
441 _("Read Full TOC Sony failed (probably not supported).\n"));
442 }
443 unit_ready(scgp);
444 scgp->silent--;
445 return (0);
446 }
447 scgp->silent--;
448
449 return ((unsigned)((bufferTOC[0] << 8) | bufferTOC[1]));
450 }
451
452 struct msf_address {
453 unsigned char mins;
454 unsigned char secs;
455 unsigned char frame;
456 };
457
458 struct zmsf_address {
459 unsigned char zero;
460 unsigned char mins;
461 unsigned char secs;
462 unsigned char frame;
463 };
464
465 #ifdef WARN_FULLTOC
466 static unsigned lba __PR((struct msf_address *ad));
467
468 static unsigned
lba(ad)469 lba(ad)
470 struct msf_address *ad;
471 {
472 return (ad->mins*60*75 + ad->secs*75 + ad->frame);
473 }
474 #endif
475
476 static unsigned dvd_lba __PR((struct zmsf_address *ad));
477
478 static unsigned
dvd_lba(ad)479 dvd_lba(ad)
480 struct zmsf_address *ad;
481 {
482 return (ad->zero*1053696 + ad->mins*60*75 + ad->secs*75 + ad->frame);
483 }
484
485 struct tocdesc_old {
486 unsigned char session;
487 unsigned char adrctl;
488 unsigned char tno;
489 unsigned char point;
490 struct msf_address adr1;
491 struct zmsf_address padr2;
492 };
493
494 struct tocdesc {
495 unsigned char session;
496 unsigned char adrctl;
497 unsigned char tno;
498 unsigned char point;
499
500 unsigned char mins;
501 unsigned char secs;
502 unsigned char frame;
503 unsigned char zero;
504 unsigned char pmins;
505 unsigned char psecs;
506 unsigned char pframe;
507 };
508
509 #if 0
510 struct outer_old {
511 unsigned char len_msb;
512 unsigned char len_lsb;
513 unsigned char first_track;
514 unsigned char last_track;
515 struct tocdesc_old ent[1];
516 };
517 #endif
518
519 struct outer {
520 unsigned char len_msb;
521 unsigned char len_lsb;
522 unsigned char first_track;
523 unsigned char last_track;
524 struct tocdesc ent[1];
525 };
526
527 /*
528 * Do address computation and return the address of "struct tocdesc" for
529 * track #i.
530 *
531 * As struct tocdesc is 11 bytes long, this structure is tail padded to
532 * 12 bytes by MC-680x0 compilers, so we cannot give this task to the compiler.
533 */
534 #define toc_addr(op, i) ((struct tocdesc *)(((char *)&((op)->ent[0])) + (i)*11))
535
536 static unsigned long first_session_leadout = 0;
537
538 static unsigned collect_tracks __PR((struct outer *po, unsigned entries,
539 BOOL bcd_flag));
540
541 static unsigned
collect_tracks(po,entries,bcd_flag)542 collect_tracks(po, entries, bcd_flag)
543 struct outer *po;
544 unsigned entries;
545 BOOL bcd_flag;
546 {
547 unsigned tracks = 0;
548 int i;
549 unsigned session;
550 unsigned last_start;
551 unsigned leadout_start_orig;
552 unsigned leadout_start;
553 unsigned max_leadout = 0;
554 struct tocdesc *ep;
555
556 #ifdef DEBUG_FULLTOC
557 for (i = 0; i < entries; i++) {
558 fprintf(stderr,
559 "%3d: %d %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
560 i,
561 bufferTOC[4+0 + (i * 11)],
562 bufferTOC[4+1 + (i * 11)],
563 bufferTOC[4+2 + (i * 11)],
564 bufferTOC[4+3 + (i * 11)],
565 bufferTOC[4+4 + (i * 11)],
566 bufferTOC[4+5 + (i * 11)],
567 bufferTOC[4+6 + (i * 11)],
568 bufferTOC[4+7 + (i * 11)],
569 bufferTOC[4+8 + (i * 11)],
570 bufferTOC[4+9 + (i * 11)],
571 bufferTOC[4+10 + (i * 11)]);
572 }
573 #endif
574 /*
575 * reformat to standard toc format
576 */
577 bufferTOC[2] = 0;
578 bufferTOC[3] = 0;
579 session = 0;
580 last_start = 0;
581 leadout_start_orig = 0;
582 leadout_start = 0;
583
584 for (i = 0; i < entries; i++) {
585 ep = toc_addr(po, i); /* Get struct tocdesc * for Track #i */
586
587 #ifdef WARN_FULLTOC
588 if (ep->tno != 0) {
589 fprintf(stderr, _("entry %d, tno is not 0: %d!\n"),
590 i, ep->tno);
591 }
592 #endif
593 if (bcd_flag) {
594 ep->session = from_bcd(ep->session);
595 ep->mins = from_bcd(ep->mins);
596 ep->secs = from_bcd(ep->secs);
597 ep->frame = from_bcd(ep->frame);
598 ep->pmins = from_bcd(ep->pmins);
599 ep->psecs = from_bcd(ep->psecs);
600 ep->pframe = from_bcd(ep->pframe);
601 }
602 switch (ep->point) {
603 case 0xa0:
604
605 /*
606 * check if session is monotonous increasing
607 */
608 if (session+1 == ep->session) {
609 session = ep->session;
610 }
611 #ifdef WARN_FULLTOC
612 else {
613 fprintf(stderr,
614 _("entry %d, session anomaly %d != %d!\n"),
615 i, session+1, ep->session);
616 }
617 /*
618 * check the adrctl field
619 */
620 if (0x10 != (ep->adrctl & 0x10)) {
621 fprintf(stderr,
622 _("entry %d, incorrect adrctl field %x!\n"),
623 i, ep->adrctl);
624 }
625 #endif
626 /*
627 * first track number
628 */
629 if (bufferTOC[2] < ep->pmins &&
630 bufferTOC[3] < ep->pmins) {
631 bufferTOC[2] = ep->pmins;
632 }
633 #ifdef WARN_FULLTOC
634 else
635 fprintf(stderr,
636 _("entry %d, session %d: start tracknumber anomaly: %d <= %d,%d(last)!\n"),
637 i, session, ep->pmins,
638 bufferTOC[2], bufferTOC[3]);
639 #endif
640 break;
641
642 case 0xa1:
643 #ifdef WARN_FULLTOC
644 /*
645 * check if session is constant
646 */
647 if (session != ep->session) {
648 fprintf(stderr,
649 _("entry %d, session anomaly %d != %d!\n"),
650 i, session, ep->session);
651 }
652
653 /*
654 * check the adrctl field
655 */
656 if (0x10 != (ep->adrctl & 0x10)) {
657 fprintf(stderr,
658 _("entry %d, incorrect adrctl field %x!\n"),
659 i, ep->adrctl);
660 }
661 #endif
662 /*
663 * last track number
664 */
665 if (bufferTOC[2] <= ep->pmins &&
666 bufferTOC[3] < ep->pmins) {
667 bufferTOC[3] = ep->pmins;
668 }
669 #ifdef WARN_FULLTOC
670 else
671 fprintf(stderr,
672 _("entry %d, session %d: end tracknumber anomaly: %d <= %d,%d(last)!\n"),
673 i, session, ep->pmins,
674 bufferTOC[2], bufferTOC[3]);
675 #endif
676 break;
677
678 case 0xa2:
679 #ifdef WARN_FULLTOC
680 /*
681 * check if session is constant
682 */
683 if (session != ep->session) {
684 fprintf(stderr,
685 _("entry %d, session anomaly %d != %d!\n"),
686 i, session, ep->session);
687 }
688
689 /*
690 * check the adrctl field
691 */
692 if (0x10 != (ep->adrctl & 0x10)) {
693 fprintf(stderr,
694 _("entry %d, incorrect adrctl field %x!\n"),
695 i, ep->adrctl);
696 }
697 #endif
698 /*
699 * register leadout position
700 */
701 {
702 unsigned leadout_start_tmp =
703 dvd_lba((struct zmsf_address *)&ep->zero);
704
705 if (first_session_leadout == 0) {
706 first_session_leadout = leadout_start_tmp
707 - 150;
708 }
709 if (leadout_start_tmp > leadout_start) {
710 leadout_start_orig = leadout_start_tmp;
711 leadout_start = leadout_start_tmp;
712 }
713 #ifdef WARN_FULLTOC
714 else
715 fprintf(stderr,
716 _("entry %d, leadout position anomaly %u!\n"),
717 i, leadout_start_tmp);
718 #endif
719 }
720 break;
721
722 case 0xb0:
723 #ifdef WARN_FULLTOC
724 /*
725 * check if session is constant
726 */
727 if (session != ep->session) {
728 fprintf(stderr,
729 _("entry %d, session anomaly %d != %d!\n"),
730 i, session, ep->session);
731 }
732
733 /*
734 * check the adrctl field
735 */
736 if (0x50 != (ep->adrctl & 0x50)) {
737 fprintf(stderr,
738 _("entry %d, incorrect adrctl field %x!\n"),
739 i, ep->adrctl);
740 }
741
742 /*
743 * check the next program area
744 */
745 if (lba((struct msf_address *)&ep->mins) < 6750 + leadout_start) {
746 fprintf(stderr,
747 _("entry %d, next program area %u < leadout_start + 6750 = %u!\n"),
748 i, lba((struct msf_address *)&ep->mins),
749 6750 + leadout_start);
750 }
751
752 /*
753 * check the maximum leadout_start
754 */
755 if (max_leadout != 0 && dvd_lba((struct zmsf_address *)&ep->zero) !=
756 max_leadout) {
757 fprintf(stderr,
758 _("entry %d, max leadout_start %u != last max_leadout_start %u!\n"),
759 i, dvd_lba((struct zmsf_address *)&ep->zero),
760 max_leadout);
761 }
762 #endif
763 if (max_leadout == 0)
764 max_leadout = dvd_lba((struct zmsf_address *)&ep->zero);
765
766 break;
767 case 0xb1:
768 case 0xb2:
769 case 0xb3:
770 case 0xb4:
771 case 0xb5:
772 case 0xb6:
773 break;
774 case 0xc0:
775 case 0xc1:
776 break;
777 default:
778 /*
779 * check if session is constant
780 */
781 if (session != ep->session) {
782 #ifdef WARN_FULLTOC
783 fprintf(stderr,
784 _("entry %d, session anomaly %d != %d!\n"),
785 i, session, ep->session);
786 #endif
787 continue;
788 }
789
790 /*
791 * check tno
792 */
793 if (bcd_flag)
794 ep->point = from_bcd(ep->point);
795
796 if (ep->point < bufferTOC[2] ||
797 ep->point > bufferTOC[3]) {
798 #ifdef WARN_FULLTOC
799 fprintf(stderr,
800 _("entry %d, track number anomaly %d - %d - %d!\n"),
801 i, bufferTOC[2], ep->point,
802 bufferTOC[3]);
803 #endif
804 } else {
805 /*
806 * check start position
807 */
808 unsigned trackstart = dvd_lba((struct zmsf_address *)&ep->zero);
809
810 /*
811 * correct illegal leadouts
812 */
813 if (leadout_start < trackstart) {
814 leadout_start = trackstart+1;
815 }
816 if (trackstart < last_start ||
817 trackstart >= leadout_start) {
818 #ifdef WARN_FULLTOC
819 fprintf(stderr,
820 _("entry %d, track %d start position anomaly %d - %d - %d!\n"),
821 i, ep->point,
822 last_start,
823 trackstart, leadout_start);
824 #endif
825 } else {
826 last_start = trackstart;
827 memcpy(toc_addr(po, tracks), ep, 11);
828 tracks++;
829 }
830 }
831 } /* switch */
832 } /* for */
833
834 /*
835 * patch leadout track
836 */
837 ep = toc_addr(po, tracks); /* Get struct tocdesc * for lead-out */
838 ep->session = session;
839 ep->adrctl = 0x10;
840 ep->tno = 0;
841 ep->point = 0xAA;
842 ep->mins = 0;
843 ep->secs = 0;
844 ep->frame = 0;
845 ep->zero = leadout_start_orig / (1053696);
846 ep->pmins = (leadout_start_orig / (60*75)) % 100;
847 ep->psecs = (leadout_start_orig / 75) % 60;
848 ep->pframe = leadout_start_orig % 75;
849 tracks++;
850
851 /*
852 * length
853 */
854 bufferTOC[0] = ((tracks * 8) + 2) >> 8;
855 bufferTOC[1] = ((tracks * 8) + 2) & 0xff;
856
857
858 /*
859 * reformat 11 byte blocks to 8 byte entries
860 */
861
862 /*
863 * 1: Session \ / reserved
864 * 2: adr ctrl | | adr ctrl
865 * 3: TNO | | track number
866 * 4: Point | | reserved
867 * 5: Min +-->----+ 0
868 * 6: Sec | | Min
869 * 7: Frame | | Sec
870 * 8: Zero | \ Frame
871 * 9: PMin |
872 * 10: PSec |
873 * 11: PFrame /
874 */
875 for (i = 0; i < tracks; i++) {
876 bufferTOC[4+0 + (i << 3)] = 0;
877 bufferTOC[4+1 + (i << 3)] = bufferTOC[4+1 + (i*11)];
878 bufferTOC[4+1 + (i << 3)] = (bufferTOC[4+1 + (i << 3)] >> 4) |
879 (bufferTOC[4+1 + (i << 3)] << 4);
880 bufferTOC[4+2 + (i << 3)] = bufferTOC[4+3 + (i*11)];
881 bufferTOC[4+3 + (i << 3)] = 0;
882 bufferTOC[4+4 + (i << 3)] = bufferTOC[4+7 + (i*11)];
883 bufferTOC[4+5 + (i << 3)] = bufferTOC[4+8 + (i*11)];
884 bufferTOC[4+6 + (i << 3)] = bufferTOC[4+9 + (i*11)];
885 bufferTOC[4+7 + (i << 3)] = bufferTOC[4+10 + (i*11)];
886 #ifdef DEBUG_FULLTOC
887 fprintf(stderr, "%02x %02x %02x %02x %02x %02x\n",
888 bufferTOC[4+ 1 + i*8],
889 bufferTOC[4+ 2 + i*8],
890 bufferTOC[4+ 4 + i*8],
891 bufferTOC[4+ 5 + i*8],
892 bufferTOC[4+ 6 + i*8],
893 bufferTOC[4+ 7 + i*8]);
894 #endif
895 }
896
897 TOC_entries(tracks, NULL, bufferTOC+4, 0);
898 return (tracks);
899 }
900
901 /*
902 * read the table of contents from the cd and fill the TOC array
903 */
904 unsigned
ReadTocSony(scgp)905 ReadTocSony(scgp)
906 SCSI *scgp;
907 {
908 unsigned tracks = 0;
909 unsigned return_length;
910
911 struct outer *po = (struct outer *)bufferTOC;
912
913 return_length = ReadFullTOCSony(scgp);
914
915 /*
916 * Check if the format was understood
917 */
918 if ((return_length & 7) == 2 &&
919 (bufferTOC[3] - bufferTOC[2]) == (return_length >> 3)) {
920 /*
921 * The extended format seems not be understood, fallback to
922 * the classical format.
923 */
924 return (ReadTocSCSI(scgp));
925 }
926
927 tracks = collect_tracks(po, ((return_length - 2) / 11), TRUE);
928
929 return (--tracks); /* without lead-out */
930 }
931
932 /*
933 * read the start of the lead-out from the first session TOC
934 */
935 unsigned
ReadFirstSessionTOCSony(scgp)936 ReadFirstSessionTOCSony(scgp)
937 SCSI *scgp;
938 {
939 unsigned return_length;
940
941 if (first_session_leadout != 0)
942 return (first_session_leadout);
943
944 return_length = ReadFullTOCSony(scgp);
945 if (return_length >= 4 + (3 * 11) -2) {
946 unsigned off;
947
948 /*
949 * We want the entry with POINT = 0xA2,
950 * which has the start position
951 * of the first session lead out
952 */
953 off = 4 + 2 * 11 + 3;
954 if (bufferTOC[off-3] == 1 && bufferTOC[off] == 0xA2) {
955 unsigned retval;
956
957 off = 4 + 2 * 11 + 8;
958 retval = bufferTOC[off] >> 4;
959 retval *= 10; retval += bufferTOC[off] & 0xf;
960 retval *= 60;
961 off++;
962 retval += 10 * (bufferTOC[off] >> 4) +
963 (bufferTOC[off] & 0xf);
964 retval *= 75;
965 off++;
966 retval += 10 * (bufferTOC[off] >> 4) +
967 (bufferTOC[off] & 0xf);
968 retval -= 150;
969
970 return (retval);
971 }
972 }
973 return (0);
974 }
975
976 /*
977 * read the full TOC
978 */
979 static unsigned
ReadFullTOCMMC(scgp)980 ReadFullTOCMMC(scgp)
981 SCSI *scgp;
982 {
983 /*
984 * READTOC, MSF, format, res, res, res, Start track/session, len msb,
985 * len lsb, control
986 */
987 register struct scg_cmd *scmd = scgp->scmd;
988 int len;
989
990 scgp->silent++;
991 unit_ready(scgp);
992 scgp->silent--;
993
994 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
995 scmd->addr = (caddr_t)bufferTOC;
996 scmd->size = 0;
997 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
998 scmd->cdb_len = SC_G1_CDBLEN;
999 scmd->sense_len = CCS_SENSE_LEN;
1000 scmd->cdb.g1_cdb.cmd = 0x43; /* Read TOC command */
1001 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
1002 scmd->cdb.g1_cdb.addr[0] = 2; /* format */
1003 scmd->cdb.g1_cdb.res6 = 1; /* session */
1004 g1_cdblen(&scmd->cdb.g1_cdb, 0);
1005
1006 scgp->silent++;
1007 if (scgp->verbose)
1008 fprintf(stderr, _("\nRead Full TOC MMC..."));
1009
1010 scgp->cmdname = "read full toc mmc";
1011
1012 scmd->size = 4;
1013 g1_cdblen(&scmd->cdb.g1_cdb, 4);
1014 if (scg_cmd(scgp) < 0) {
1015 if (global.quiet != 1) {
1016 errmsgno(EX_BAD,
1017 _("Read Full TOC MMC failed (probably not supported).\n"));
1018 }
1019 /* XXX was ist das ??? */
1020 #ifdef B_BEOS_VERSION
1021 #else
1022 scgp->silent--;
1023 return (0);
1024 #endif
1025 }
1026 len = (unsigned)((bufferTOC[0] << 8) | bufferTOC[1]) + 2;
1027 /*
1028 * XXX there is a bug in some ASPI versions that
1029 * XXX cause a hang with odd transfer lengths.
1030 * XXX We should workaround the problem where it exists
1031 * XXX but the problem may exist elsewhere too.
1032 */
1033 if (len & 1)
1034 len++;
1035 scmd->size = len;
1036 g1_cdblen(&scmd->cdb.g1_cdb, len);
1037
1038 if (scg_cmd(scgp) < 0) {
1039 unit_ready(scgp);
1040 scgp->silent--;
1041 return (0);
1042 }
1043
1044 scgp->silent--;
1045
1046 return ((unsigned)((bufferTOC[0] << 8) | bufferTOC[1]));
1047 }
1048
1049 /*
1050 * read the start of the lead-out from the first session TOC
1051 */
1052 unsigned
ReadFirstSessionTOCMMC(scgp)1053 ReadFirstSessionTOCMMC(scgp)
1054 SCSI *scgp;
1055 {
1056 unsigned off;
1057 unsigned return_length;
1058
1059 if (first_session_leadout != 0)
1060 return (first_session_leadout);
1061
1062 return_length = ReadFullTOCMMC(scgp);
1063
1064 /*
1065 * We want the entry with POINT = 0xA2, which has the start position
1066 * of the first session lead out
1067 */
1068 off = 4 + 3;
1069 while (off < return_length && bufferTOC[off] != 0xA2) {
1070 off += 11;
1071 }
1072 if (off < return_length) {
1073 off += 5;
1074 return ((bufferTOC[off]*60 + bufferTOC[off+1])*75 +
1075 bufferTOC[off+2] - 150);
1076 }
1077 return (0);
1078 }
1079
1080 /*
1081 * read the table of contents from the cd and fill the TOC array
1082 */
1083 unsigned
ReadTocMMC(scgp)1084 ReadTocMMC(scgp)
1085 SCSI *scgp;
1086 {
1087 unsigned tracks = 0;
1088 unsigned return_length;
1089
1090 struct outer *po = (struct outer *)bufferTOC;
1091
1092 return_length = ReadFullTOCMMC(scgp);
1093 if (return_length - 2 < 4*11 || ((return_length - 2) % 11) != 0)
1094 return (ReadTocSCSI(scgp));
1095
1096 tracks = collect_tracks(po, ((return_length - 2) / 11), FALSE);
1097 return (--tracks); /* without lead-out */
1098 }
1099
1100 /*
1101 * read the table of contents from the cd and fill the TOC array
1102 */
1103 unsigned
ReadTocSCSI(scgp)1104 ReadTocSCSI(scgp)
1105 SCSI *scgp;
1106 {
1107 unsigned tracks;
1108 int result;
1109 unsigned char bufferTOCMSF[CD_FRAMESIZE];
1110
1111 /*
1112 * first read the first and last track number
1113 */
1114 /*
1115 * READTOC, MSF format flag, res, res, res, res, Start track, len msb,
1116 * len lsb, flags
1117 */
1118 register struct scg_cmd *scmd = scgp->scmd;
1119
1120 scgp->silent++;
1121 unit_ready(scgp);
1122 scgp->silent--;
1123
1124 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1125 scmd->addr = (caddr_t)bufferTOC;
1126 scmd->size = 4;
1127 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1128 scmd->cdb_len = SC_G1_CDBLEN;
1129 scmd->sense_len = CCS_SENSE_LEN;
1130 scmd->cdb.g1_cdb.cmd = 0x43; /* read TOC command */
1131 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
1132 scmd->cdb.g1_cdb.res6 = 1; /* start track */
1133 g1_cdblen(&scmd->cdb.g1_cdb, 4);
1134
1135 if (scgp->verbose)
1136 fprintf(stderr, _("\nRead TOC size (standard)..."));
1137
1138 /*
1139 * do the scsi cmd (read table of contents)
1140 */
1141
1142 scgp->cmdname = "read toc size";
1143 if (scg_cmd(scgp) < 0)
1144 FatalError(EX_BAD, _("Read TOC size failed.\n"));
1145
1146 scgp->silent++;
1147 unit_ready(scgp);
1148 scgp->silent--;
1149
1150 tracks = ((bufferTOC [3]) - bufferTOC [2] + 2);
1151 if (tracks > MAXTRK)
1152 return (0);
1153 if (tracks == 0)
1154 return (0);
1155
1156 memset(bufferTOCMSF, 0, sizeof (bufferTOCMSF));
1157 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1158 scmd->addr = (caddr_t)bufferTOCMSF;
1159 scmd->size = 4 + tracks * 8;
1160 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1161 scmd->cdb_len = SC_G1_CDBLEN;
1162 scmd->sense_len = CCS_SENSE_LEN;
1163 scmd->cdb.g1_cdb.cmd = 0x43; /* read TOC command */
1164 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
1165 scmd->cdb.g1_cdb.res = 1; /* MSF format */
1166 scmd->cdb.g1_cdb.res6 = 1; /* start track */
1167 g1_cdblen(&scmd->cdb.g1_cdb, 4 + tracks * 8);
1168
1169 if (scgp->verbose)
1170 fprintf(stderr, _("\nRead TOC tracks (standard MSF)..."));
1171 /*
1172 * do the scsi cmd (read table of contents)
1173 */
1174 scgp->cmdname = "read toc tracks ";
1175 result = scg_cmd(scgp);
1176
1177 if (result < 0) {
1178 /*
1179 * MSF format did not succeeded
1180 */
1181 memset(bufferTOCMSF, 0, sizeof (bufferTOCMSF));
1182
1183 scgp->silent++;
1184 unit_ready(scgp);
1185 scgp->silent--;
1186 } else {
1187 int i;
1188
1189 for (i = 0; i < tracks; i++) {
1190 bufferTOCMSF[4+1 + (i << 3)] = (bufferTOCMSF[4+1 +
1191 (i << 3)] >> 4) |
1192 (bufferTOCMSF[4+1 + (i << 3)] << 4);
1193 #if 0
1194 fprintf(stderr,
1195 "MSF %d %02x %02x %02x %02x %02x %02x %02x %02x\n",
1196 i,
1197 bufferTOCMSF[4+0 + (i * 8)],
1198 bufferTOCMSF[4+1 + (i * 8)],
1199 bufferTOCMSF[4+2 + (i * 8)],
1200 bufferTOCMSF[4+3 + (i * 8)],
1201 bufferTOCMSF[4+4 + (i * 8)],
1202 bufferTOCMSF[4+5 + (i * 8)],
1203 bufferTOCMSF[4+6 + (i * 8)],
1204 bufferTOCMSF[4+7 + (i * 8)]);
1205 #endif
1206 }
1207 }
1208
1209 /*
1210 * LBA format for cd burners like Philips CD-522
1211 */
1212 scgp->silent++;
1213 unit_ready(scgp);
1214 scgp->silent--;
1215
1216 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1217 scmd->addr = (caddr_t)bufferTOC;
1218 scmd->size = 4 + tracks * 8;
1219 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1220 scmd->cdb_len = SC_G1_CDBLEN;
1221 scmd->sense_len = CCS_SENSE_LEN;
1222 scmd->cdb.g1_cdb.cmd = 0x43; /* read TOC command */
1223 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
1224 scmd->cdb.g1_cdb.res = 0; /* LBA format */
1225 scmd->cdb.g1_cdb.res6 = 1; /* start track */
1226 g1_cdblen(&scmd->cdb.g1_cdb, 4 + tracks * 8);
1227
1228 if (scgp->verbose)
1229 fprintf(stderr, _("\nRead TOC tracks (standard LBA)..."));
1230 /*
1231 * do the scsi cmd (read table of contents)
1232 */
1233
1234 scgp->cmdname = "read toc tracks ";
1235 if (scg_cmd(scgp) < 0) {
1236 FatalError(EX_BAD, _("Read TOC tracks (lba) failed.\n"));
1237 }
1238 scgp->silent++;
1239 unit_ready(scgp);
1240 scgp->silent--;
1241
1242 {
1243 int i;
1244
1245 for (i = 0; i < tracks; i++) {
1246 bufferTOC[4+1 + (i << 3)] = (bufferTOC[4+1 + (i << 3)] >> 4) |
1247 (bufferTOC[4+1 + (i << 3)] << 4);
1248 #if 0
1249 fprintf(stderr,
1250 "LBA %d %02x %02x %02x %02x %02x %02x %02x %02x\n",
1251 i,
1252 bufferTOC[4+0 + (i * 8)],
1253 bufferTOC[4+1 + (i * 8)],
1254 bufferTOC[4+2 + (i * 8)],
1255 bufferTOC[4+3 + (i * 8)],
1256 bufferTOC[4+4 + (i * 8)],
1257 bufferTOC[4+5 + (i * 8)],
1258 bufferTOC[4+6 + (i * 8)],
1259 bufferTOC[4+7 + (i * 8)]);
1260 #endif
1261 }
1262 }
1263 TOC_entries(tracks, bufferTOC+4, bufferTOCMSF+4, result);
1264 return (--tracks); /* without lead-out */
1265 }
1266
1267 /* ---------------- Read methods ------------------------------ */
1268
1269 /*
1270 * Read max. SectorBurst of cdda sectors to buffer
1271 * via standard SCSI-2 Read(10) command
1272 */
1273 static int ReadStandardLowlevel __PR((SCSI *scgp, UINT4 *p, unsigned lSector,
1274 unsigned SectorBurstVal, unsigned secsize));
1275
1276 static int
ReadStandardLowlevel(scgp,p,lSector,SectorBurstVal,secsize)1277 ReadStandardLowlevel(scgp, p, lSector, SectorBurstVal, secsize)
1278 SCSI *scgp;
1279 UINT4 *p;
1280 unsigned lSector;
1281 unsigned SectorBurstVal;
1282 unsigned secsize;
1283 {
1284 /*
1285 * READ10, flags, block1 msb, block2, block3, block4 lsb, reserved,
1286 * transfer len msb, transfer len lsb, block addressing mode
1287 */
1288 register struct scg_cmd *scmd = scgp->scmd;
1289
1290 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1291 scmd->addr = (caddr_t)p;
1292 scmd->size = SectorBurstVal * secsize;
1293 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1294 scmd->cdb_len = SC_G1_CDBLEN;
1295 scmd->sense_len = CCS_SENSE_LEN;
1296 scmd->cdb.g1_cdb.cmd = 0x28; /* read 10 command */
1297 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
1298 scmd->cdb.g1_cdb.res |= (accepts_fua_bit == 1 ? 1 << 2 : 0);
1299 g1_cdbaddr(&scmd->cdb.g1_cdb, lSector);
1300 g1_cdblen(&scmd->cdb.g1_cdb, SectorBurstVal);
1301
1302 if (scgp->verbose) {
1303 fprintf(stderr, _("\nReadStandard10 %s (%u)..."),
1304 secsize > 2048 ? "CDDA" : "CD_DATA", secsize);
1305 }
1306 scgp->cmdname = "ReadStandard10";
1307
1308 if (scg_cmd(scgp))
1309 return (0);
1310
1311 /*
1312 * has all or something been read?
1313 */
1314 return (SectorBurstVal - scg_getresid(scgp)/secsize);
1315 }
1316
1317
1318 int
ReadStandard(scgp,p,lSector,SectorBurstVal)1319 ReadStandard(scgp, p, lSector, SectorBurstVal)
1320 SCSI *scgp;
1321 UINT4 *p;
1322 unsigned lSector;
1323 unsigned SectorBurstVal;
1324 {
1325 return (ReadStandardLowlevel(scgp, p, lSector, SectorBurstVal,
1326 CD_FRAMESIZE_RAW));
1327 }
1328
1329 int
ReadStandardData(scgp,p,lSector,SectorBurstVal)1330 ReadStandardData(scgp, p, lSector, SectorBurstVal)
1331 SCSI *scgp;
1332 UINT4 *p;
1333 unsigned lSector;
1334 unsigned SectorBurstVal;
1335 {
1336 return (ReadStandardLowlevel(scgp, p, lSector, SectorBurstVal,
1337 CD_FRAMESIZE));
1338 }
1339
1340 /*
1341 * Read max. SectorBurst of cdda sectors to buffer
1342 * via vendor-specific ReadCdda(10) command
1343 */
1344 int
ReadCdda10(scgp,p,lSector,SectorBurstVal)1345 ReadCdda10(scgp, p, lSector, SectorBurstVal)
1346 SCSI *scgp;
1347 UINT4 *p;
1348 unsigned lSector;
1349 unsigned SectorBurstVal;
1350 {
1351 /*
1352 * READ10, flags, block1 msb, block2, block3, block4 lsb, reserved,
1353 * transfer len msb, transfer len lsb, block addressing mode
1354 */
1355 register struct scg_cmd *scmd = scgp->scmd;
1356
1357 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1358 scmd->addr = (caddr_t)p;
1359 scmd->size = SectorBurstVal*CD_FRAMESIZE_RAW;
1360 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1361 scmd->cdb_len = SC_G1_CDBLEN;
1362 scmd->sense_len = CCS_SENSE_LEN;
1363 scmd->cdb.g1_cdb.cmd = 0xd4; /* Read audio command */
1364 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
1365 scmd->cdb.g1_cdb.res |= (accepts_fua_bit == 1 ? 1 << 2 : 0);
1366 g1_cdbaddr(&scmd->cdb.g1_cdb, lSector);
1367 g1_cdblen(&scmd->cdb.g1_cdb, SectorBurstVal);
1368 if (scgp->verbose)
1369 fprintf(stderr, _("\nReadNEC10 CDDA..."));
1370
1371 scgp->cmdname = "Read10 NEC";
1372
1373 if (scg_cmd(scgp))
1374 return (0);
1375
1376 /*
1377 * has all or something been read?
1378 */
1379 return (SectorBurstVal - scg_getresid(scgp)/CD_FRAMESIZE_RAW);
1380 }
1381
1382
1383 /*
1384 * Read max. SectorBurst of cdda sectors to buffer
1385 * via vendor-specific ReadCdda(12) command
1386 */
1387 int
ReadCdda12(scgp,p,lSector,SectorBurstVal)1388 ReadCdda12(scgp, p, lSector, SectorBurstVal)
1389 SCSI *scgp;
1390 UINT4 *p;
1391 unsigned lSector;
1392 unsigned SectorBurstVal;
1393 {
1394 register struct scg_cmd *scmd = scgp->scmd;
1395
1396 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1397 scmd->addr = (caddr_t)p;
1398 scmd->size = SectorBurstVal*CD_FRAMESIZE_RAW;
1399 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1400 scmd->cdb_len = SC_G5_CDBLEN;
1401 scmd->sense_len = CCS_SENSE_LEN;
1402 scmd->cdb.g5_cdb.cmd = 0xd8; /* read audio command */
1403 scmd->cdb.g5_cdb.lun = scg_lun(scgp);
1404 scmd->cdb.g5_cdb.res |= (accepts_fua_bit == 1 ? 1 << 2 : 0);
1405 g5_cdbaddr(&scmd->cdb.g5_cdb, lSector);
1406 g5_cdblen(&scmd->cdb.g5_cdb, SectorBurstVal);
1407
1408 if (scgp->verbose)
1409 fprintf(stderr, _("\nReadSony12 CDDA..."));
1410
1411 scgp->cmdname = "Read12";
1412
1413 if (scg_cmd(scgp) < 0) {
1414 scgp->silent++;
1415 unit_ready(scgp);
1416 scgp->silent--;
1417 return (0);
1418 }
1419
1420 /*
1421 * has all or something been read?
1422 */
1423 return (SectorBurstVal - scg_getresid(scgp)/CD_FRAMESIZE_RAW);
1424 }
1425
1426 /*
1427 * Read max. SectorBurst of cdda sectors to buffer
1428 * via vendor-specific ReadCdda(12) command
1429 */
1430 int
ReadCdda12_C2(scgp,p,lSector,SectorBurstVal)1431 ReadCdda12_C2(scgp, p, lSector, SectorBurstVal)
1432 SCSI *scgp;
1433 UINT4 *p;
1434 unsigned lSector;
1435 unsigned SectorBurstVal;
1436 {
1437 register struct scg_cmd *scmd = scgp->scmd;
1438
1439 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1440 scmd->addr = (caddr_t)p;
1441 scmd->size = SectorBurstVal*CD_FRAMESIZE_RAWER;
1442 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1443 scmd->cdb_len = SC_G5_CDBLEN;
1444 scmd->sense_len = CCS_SENSE_LEN;
1445 scmd->cdb.g5_cdb.cmd = 0xd8; /* read audio command */
1446 scmd->cdb.g5_cdb.lun = scg_lun(scgp);
1447 scmd->cdb.g5_cdb.res |= (accepts_fua_bit == 1 ? 1 << 2 : 0);
1448 scmd->cdb.g5_cdb.res10 = 0x04; /* With C2 errors */
1449 g5_cdbaddr(&scmd->cdb.g5_cdb, lSector);
1450 g5_cdblen(&scmd->cdb.g5_cdb, SectorBurstVal);
1451
1452 if (scgp->verbose)
1453 fprintf(stderr, _("\nReadSony12 CDDA C2..."));
1454
1455 scgp->cmdname = "Read12 C2";
1456
1457 if (scg_cmd(scgp) < 0) {
1458 scgp->silent++;
1459 unit_ready(scgp);
1460 scgp->silent--;
1461 return (0);
1462 }
1463
1464 /*
1465 * has all or something been read?
1466 */
1467 return (SectorBurstVal - scg_getresid(scgp)/CD_FRAMESIZE_RAWER);
1468 }
1469
1470 /*
1471 * Read max. SectorBurst of cdda sectors to buffer
1472 * via vendor-specific ReadCdda(12) command
1473 */
1474 /*
1475 * It uses a 12 Byte CDB with 0xd4 as opcode, the start sector is coded as
1476 * normal and the number of sectors is coded in Byte 8 and 9 (begining with 0).
1477 */
1478 int
ReadCdda12Matsushita(scgp,p,lSector,SectorBurstVal)1479 ReadCdda12Matsushita(scgp, p, lSector, SectorBurstVal)
1480 SCSI *scgp;
1481 UINT4 *p;
1482 unsigned lSector;
1483 unsigned SectorBurstVal;
1484 {
1485 register struct scg_cmd *scmd = scgp->scmd;
1486
1487 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1488 scmd->addr = (caddr_t)p;
1489 scmd->size = SectorBurstVal*CD_FRAMESIZE_RAW;
1490 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1491 scmd->cdb_len = SC_G5_CDBLEN;
1492 scmd->sense_len = CCS_SENSE_LEN;
1493 scmd->cdb.g5_cdb.cmd = 0xd4; /* read audio command */
1494 scmd->cdb.g5_cdb.lun = scg_lun(scgp);
1495 scmd->cdb.g5_cdb.res |= (accepts_fua_bit == 1 ? 1 << 2 : 0);
1496 g5_cdbaddr(&scmd->cdb.g5_cdb, lSector);
1497 g5_cdblen(&scmd->cdb.g5_cdb, SectorBurstVal);
1498
1499 if (scgp->verbose)
1500 fprintf(stderr, _("\nReadMatsushita12 CDDA..."));
1501
1502 scgp->cmdname = "Read12Matsushita";
1503
1504 if (scg_cmd(scgp))
1505 return (0);
1506
1507 /*
1508 * has all or something been read?
1509 */
1510 return (SectorBurstVal - scg_getresid(scgp)/CD_FRAMESIZE_RAW);
1511 }
1512
1513 /*
1514 * Read max. SectorBurst of cdda sectors to buffer
1515 * via MMC standard READ CD command
1516 */
1517 int
ReadCddaMMC12(scgp,p,lSector,SectorBurstVal)1518 ReadCddaMMC12(scgp, p, lSector, SectorBurstVal)
1519 SCSI *scgp;
1520 UINT4 *p;
1521 unsigned lSector;
1522 unsigned SectorBurstVal;
1523 {
1524 register struct scg_cmd *scmd;
1525
1526 scmd = scgp->scmd;
1527
1528 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1529 scmd->addr = (caddr_t)p;
1530 scmd->size = SectorBurstVal*CD_FRAMESIZE_RAW;
1531 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1532 scmd->cdb_len = SC_G5_CDBLEN;
1533 scmd->sense_len = CCS_SENSE_LEN;
1534 scmd->cdb.g5_cdb.cmd = 0xbe; /* read cd command */
1535 scmd->cdb.g5_cdb.lun = scg_lun(scgp);
1536 scmd->cdb.g5_cdb.res = 1 << 1; /* expected sector type field CDDA */
1537 g5_cdbaddr(&scmd->cdb.g5_cdb, lSector);
1538 g5x_cdblen(&scmd->cdb.g5_cdb, SectorBurstVal);
1539 scmd->cdb.g5_cdb.count[3] = 1 << 4; /* User data */
1540
1541 if (scgp->verbose)
1542 fprintf(stderr, _("\nReadMMC12 CDDA..."));
1543
1544 scgp->cmdname = "ReadCD MMC 12";
1545
1546 if (scg_cmd(scgp) < 0) {
1547 scgp->silent++;
1548 unit_ready(scgp);
1549 scgp->silent--;
1550 return (0);
1551 }
1552
1553 /*
1554 * has all or something been read?
1555 */
1556 return (SectorBurstVal - scg_getresid(scgp)/CD_FRAMESIZE_RAW);
1557 }
1558
1559 /*
1560 * Read max. SectorBurst of cdda sectors to buffer
1561 * via MMC standard READ CD command
1562 */
1563 int
ReadCddaMMC12_C2(scgp,p,lSector,SectorBurstVal)1564 ReadCddaMMC12_C2(scgp, p, lSector, SectorBurstVal)
1565 SCSI *scgp;
1566 UINT4 *p;
1567 unsigned lSector;
1568 unsigned SectorBurstVal;
1569 {
1570 register struct scg_cmd *scmd;
1571
1572 scmd = scgp->scmd;
1573
1574 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1575 scmd->addr = (caddr_t)p;
1576 scmd->size = SectorBurstVal*CD_FRAMESIZE_RAWER;
1577 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1578 scmd->cdb_len = SC_G5_CDBLEN;
1579 scmd->sense_len = CCS_SENSE_LEN;
1580 scmd->cdb.g5_cdb.cmd = 0xbe; /* read cd command */
1581 scmd->cdb.g5_cdb.lun = scg_lun(scgp);
1582 scmd->cdb.g5_cdb.res = 1 << 1; /* expected sector type field CDDA */
1583 g5_cdbaddr(&scmd->cdb.g5_cdb, lSector);
1584 g5x_cdblen(&scmd->cdb.g5_cdb, SectorBurstVal);
1585 scmd->cdb.g5_cdb.count[3] = 1 << 4; /* User data */
1586 scmd->cdb.g5_cdb.count[3] |= 1 << 1; /* C2 */
1587
1588 if (scgp->verbose)
1589 fprintf(stderr, _("\nReadMMC12 CDDA C2..."));
1590
1591 scgp->cmdname = "ReadCD MMC 12 C2";
1592
1593 scgp->silent++;
1594 if (scg_cmd(scgp) < 0) {
1595 /*
1596 * if the command is not available, disable this method
1597 * by setting the ReadCdRom_C2 pointer to NULL.
1598 * 0x05 0x24 0x00 is invalid field in cdb
1599 */
1600 if (scg_sense_key(scgp) == SC_ILLEGAL_REQUEST &&
1601 scg_sense_code(scgp) == 0x24 &&
1602 scg_sense_qual(scgp) == 0x00) {
1603 ReadCdRom_C2 = NULL;
1604 }
1605 unit_ready(scgp);
1606
1607 scgp->silent--;
1608 return (0);
1609 }
1610 scgp->silent--;
1611
1612 /*
1613 * has all or something been read?
1614 */
1615 return (SectorBurstVal - scg_getresid(scgp)/CD_FRAMESIZE_RAWER);
1616 }
1617
1618 int
ReadCddaFallbackMMC(scgp,p,lSector,SectorBurstVal)1619 ReadCddaFallbackMMC(scgp, p, lSector, SectorBurstVal)
1620 SCSI *scgp;
1621 UINT4 *p;
1622 unsigned lSector;
1623 unsigned SectorBurstVal;
1624 {
1625 static int ReadCdda12_unknown = 0;
1626 int retval = -999;
1627
1628 scgp->silent++;
1629 if (ReadCdda12_unknown ||
1630 ((retval = ReadCdda12(scgp, p, lSector, SectorBurstVal)) <= 0)) {
1631 /*
1632 * if the command is not available, use the regular
1633 * MMC ReadCd
1634 */
1635 if (retval <= 0 && scg_sense_key(scgp) == SC_ILLEGAL_REQUEST) {
1636 ReadCdda12_unknown = 1;
1637 }
1638 scgp->silent--;
1639 ReadCdRom = ReadCddaMMC12;
1640 ReadCdRomSub = ReadCddaSubMMC12;
1641 return (ReadCddaMMC12(scgp, p, lSector, SectorBurstVal));
1642 }
1643 scgp->silent--;
1644 return (retval);
1645 }
1646
1647 int
ReadCddaFallbackMMC_C2(scgp,p,lSector,SectorBurstVal)1648 ReadCddaFallbackMMC_C2(scgp, p, lSector, SectorBurstVal)
1649 SCSI *scgp;
1650 UINT4 *p;
1651 unsigned lSector;
1652 unsigned SectorBurstVal;
1653 {
1654 static int ReadCdda12_C2_unknown = 0;
1655 int retval = -999;
1656
1657 scgp->silent++;
1658 if (ReadCdda12_C2_unknown ||
1659 ((retval = ReadCdda12_C2(scgp, p, lSector, SectorBurstVal)) <= 0)) {
1660 /*
1661 * if the command is not available, use the regular
1662 * MMC ReadCd
1663 */
1664 if (retval <= 0 && scg_sense_key(scgp) == SC_ILLEGAL_REQUEST) {
1665 ReadCdda12_C2_unknown = 1;
1666 }
1667 scgp->silent--;
1668 ReadCdRom_C2 = ReadCddaMMC12_C2;
1669 return (ReadCddaMMC12_C2(scgp, p, lSector, SectorBurstVal));
1670 }
1671 scgp->silent--;
1672 return (retval);
1673 }
1674
1675 int
ReadCddaNoFallback_C2(scgp,p,lSector,SectorBurstVal)1676 ReadCddaNoFallback_C2(scgp, p, lSector, SectorBurstVal)
1677 SCSI *scgp;
1678 UINT4 *p;
1679 unsigned lSector;
1680 unsigned SectorBurstVal;
1681 {
1682 static int ReadCdda12_C2_unknown = 0;
1683 int retval = -999;
1684
1685 scgp->silent++;
1686 if (ReadCdda12_C2_unknown ||
1687 ((retval = ReadCdda12_C2(scgp, p, lSector, SectorBurstVal)) <= 0)) {
1688 /*
1689 * if the command is not available, disable this method
1690 * by setting the ReadCdRom_C2 pointer to NULL.
1691 * 0x05 0x24 0x00 is invalid field in cdb
1692 */
1693 if (retval <= 0 && scg_sense_key(scgp) == SC_ILLEGAL_REQUEST &&
1694 scg_sense_code(scgp) == 0x24 &&
1695 scg_sense_qual(scgp) == 0x00) {
1696 ReadCdda12_C2_unknown = 1;
1697 }
1698 ReadCdRom_C2 = NULL;
1699 }
1700 scgp->silent--;
1701 return (retval);
1702 }
1703
1704 /*
1705 * Read the Sub-Q-Channel to SubQbuffer. This is the method for
1706 * drives thp->sectsizeat do not support subchannel parameters.
1707 */
1708 #ifdef PROTOTYPES
1709 static subq_chnl *
ReadSubQFallback(SCSI * scgp,unsigned char sq_format,unsigned char track)1710 ReadSubQFallback(SCSI *scgp, unsigned char sq_format, unsigned char track)
1711 #else
1712 static subq_chnl *
1713 ReadSubQFallback(scgp, sq_format, track)
1714 SCSI *scgp;
1715 unsigned char sq_format;
1716 unsigned char track;
1717 #endif
1718 {
1719 register struct scg_cmd *scmd = scgp->scmd;
1720
1721 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1722 scmd->addr = (caddr_t)SubQbuffer;
1723 scmd->size = 24;
1724 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1725 scmd->cdb_len = SC_G1_CDBLEN;
1726 scmd->sense_len = CCS_SENSE_LEN;
1727 scmd->cdb.g1_cdb.cmd = 0x42; /* Read SubQChannel */
1728 /* use LBA */
1729 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
1730 scmd->cdb.g1_cdb.addr[0] = 0x40; /* SubQ info */
1731 scmd->cdb.g1_cdb.addr[1] = 0; /* parameter list: all */
1732 scmd->cdb.g1_cdb.res6 = track; /* track number */
1733 g1_cdblen(&scmd->cdb.g1_cdb, 24);
1734
1735 if (scgp->verbose)
1736 fprintf(stderr, _("\nRead Subchannel_dumb..."));
1737
1738 scgp->cmdname = "Read Subchannel_dumb";
1739
1740 if (scg_cmd(scgp) < 0) {
1741 errmsgno(EX_BAD, _("Read SubQ failed.\n"));
1742 }
1743
1744 /*
1745 * check, if the requested format is delivered
1746 */
1747 { unsigned char *p = (unsigned char *) SubQbuffer;
1748
1749 if ((((unsigned)p[2] << 8) | p[3]) /* LENGTH */ > ULONG_C(11) &&
1750 (p[5] >> 4) /* ADR */ == sq_format) {
1751 if (sq_format == GET_POSITIONDATA)
1752 p[5] = (p[5] << 4) | (p[5] >> 4);
1753 return (SubQbuffer);
1754 }
1755 }
1756
1757 /*
1758 * FIXME: we might actively search for the requested info ...
1759 */
1760 return (NULL);
1761 }
1762
1763 /*
1764 * Read the Sub-Q-Channel to SubQbuffer
1765 */
1766 #ifdef PROTOTYPES
1767 subq_chnl *
ReadSubQSCSI(SCSI * scgp,unsigned char sq_format,unsigned char track)1768 ReadSubQSCSI(SCSI *scgp, unsigned char sq_format, unsigned char track)
1769 #else
1770 subq_chnl *
1771 ReadSubQSCSI(scgp, sq_format, track)
1772 SCSI *scgp;
1773 unsigned char sq_format;
1774 unsigned char track;
1775 #endif
1776 {
1777 int resp_size;
1778 register struct scg_cmd *scmd = scgp->scmd;
1779
1780 switch (sq_format) {
1781 case GET_POSITIONDATA:
1782 resp_size = 16;
1783 track = 0;
1784 break;
1785
1786 case GET_CATALOGNUMBER:
1787 resp_size = 24;
1788 track = 0;
1789 break;
1790
1791 case GET_TRACK_ISRC:
1792 resp_size = 24;
1793 break;
1794
1795 default:
1796 fprintf(stderr,
1797 _("ReadSubQSCSI: unknown format %d\n"), sq_format);
1798 return (NULL);
1799 }
1800
1801 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1802 scmd->addr = (caddr_t)SubQbuffer;
1803 scmd->size = resp_size;
1804 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1805 scmd->cdb_len = SC_G1_CDBLEN;
1806 scmd->sense_len = CCS_SENSE_LEN;
1807 scmd->cdb.g1_cdb.cmd = 0x42;
1808 /* use LBA */
1809 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
1810 scmd->cdb.g1_cdb.addr[0] = 0x40; /* SubQ info */
1811 scmd->cdb.g1_cdb.addr[1] = sq_format; /* parameter list: all */
1812 scmd->cdb.g1_cdb.res6 = track; /* track number */
1813 g1_cdblen(&scmd->cdb.g1_cdb, resp_size);
1814
1815 if (scgp->verbose)
1816 fprintf(stderr, _("\nRead Subchannel..."));
1817
1818 scgp->cmdname = "Read Subchannel";
1819
1820 if (scg_cmd(scgp) < 0) {
1821 /*
1822 * in case of error do a fallback for dumb firmwares
1823 */
1824 return (ReadSubQFallback(scgp, sq_format, track));
1825 }
1826
1827 if (sq_format == GET_POSITIONDATA)
1828 SubQbuffer->control_adr = (SubQbuffer->control_adr << 4) |
1829 (SubQbuffer->control_adr >> 4);
1830 return (SubQbuffer);
1831 }
1832
1833 static subq_chnl sc;
1834
1835 static subq_chnl* fill_subchannel __PR((unsigned char bufferwithQ[]));
fill_subchannel(bufferwithQ)1836 static subq_chnl* fill_subchannel(bufferwithQ)
1837 unsigned char bufferwithQ[];
1838 {
1839 sc.subq_length = 0;
1840 sc.control_adr = bufferwithQ[CD_FRAMESIZE_RAW + 0];
1841 sc.track = bufferwithQ[CD_FRAMESIZE_RAW + 1];
1842 sc.index = bufferwithQ[CD_FRAMESIZE_RAW + 2];
1843 return (&sc);
1844 }
1845
1846 int
ReadCddaSubSony(scgp,p,lSector,SectorBurstVal)1847 ReadCddaSubSony(scgp, p, lSector, SectorBurstVal)
1848 SCSI *scgp;
1849 UINT4 *p;
1850 unsigned lSector;
1851 unsigned SectorBurstVal;
1852 {
1853 register struct scg_cmd *scmd = scgp->scmd;
1854
1855 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1856 scmd->addr = (caddr_t)p;
1857 scmd->size = SectorBurstVal*(CD_FRAMESIZE_RAW + 16);
1858 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1859 scmd->cdb_len = SC_G5_CDBLEN;
1860 scmd->sense_len = CCS_SENSE_LEN;
1861 scmd->cdb.g5_cdb.cmd = 0xd8; /* read audio command */
1862 scmd->cdb.g5_cdb.lun = scg_lun(scgp);
1863 scmd->cdb.g5_cdb.res |= (accepts_fua_bit == 1 ? 1 << 2 : 0);
1864 scmd->cdb.g5_cdb.res10 = 0x01; /* subcode 1 -> cdda + 16 * q sub */
1865 g5_cdbaddr(&scmd->cdb.g5_cdb, lSector);
1866 g5_cdblen(&scmd->cdb.g5_cdb, SectorBurstVal);
1867
1868 if (scgp->verbose)
1869 fprintf(stderr, _("\nReadSony12 CDDA + SubChannels..."));
1870
1871 scgp->cmdname = "Read12SubChannelsSony";
1872
1873 if (scg_cmd(scgp) < 0) {
1874 scgp->silent++;
1875 unit_ready(scgp);
1876 scgp->silent--;
1877 return (-1);
1878 }
1879
1880 /*
1881 * has all or something been read?
1882 */
1883 return (scg_getresid(scgp) != 0);
1884 }
1885
1886 int ReadCddaSub96Sony __PR((SCSI *scgp, UINT4 *p, unsigned lSector,
1887 unsigned SectorBurstVal));
1888
1889 int
ReadCddaSub96Sony(scgp,p,lSector,SectorBurstVal)1890 ReadCddaSub96Sony(scgp, p, lSector, SectorBurstVal)
1891 SCSI *scgp;
1892 UINT4 *p;
1893 unsigned lSector;
1894 unsigned SectorBurstVal;
1895 {
1896 register struct scg_cmd *scmd = scgp->scmd;
1897
1898 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1899 scmd->addr = (caddr_t)p;
1900 scmd->size = SectorBurstVal*(CD_FRAMESIZE_RAW + 96);
1901 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1902 scmd->cdb_len = SC_G5_CDBLEN;
1903 scmd->sense_len = CCS_SENSE_LEN;
1904 scmd->cdb.g5_cdb.cmd = 0xd8; /* read audio command */
1905 scmd->cdb.g5_cdb.lun = scg_lun(scgp);
1906 scmd->cdb.g5_cdb.res |= (accepts_fua_bit == 1 ? 1 << 2 : 0);
1907 scmd->cdb.g5_cdb.res10 = 0x02; /* subcode 2 -> cdda + 96 * q sub */
1908 g5_cdbaddr(&scmd->cdb.g5_cdb, lSector);
1909 g5_cdblen(&scmd->cdb.g5_cdb, SectorBurstVal);
1910
1911 if (scgp->verbose)
1912 fprintf(stderr, _("\nReadSony12 CDDA + 96 byte SubChannels..."));
1913
1914 scgp->cmdname = "Read12SubChannelsSony";
1915
1916 if (scg_cmd(scgp) < 0) {
1917 scgp->silent++;
1918 unit_ready(scgp);
1919 scgp->silent--;
1920 return (-1);
1921 }
1922
1923 /*
1924 * has all or something been read?
1925 */
1926 return (scg_getresid(scgp) != 0);
1927 }
1928
1929 subq_chnl *
ReadSubChannelsSony(scgp,lSector)1930 ReadSubChannelsSony(scgp, lSector)
1931 SCSI *scgp;
1932 unsigned lSector;
1933 {
1934 /*int retval = ReadCddaSub96Sony(scgp, (UINT4 *)bufferTOC, lSector, 1);*/
1935 int retval = ReadCddaSubSony(scgp, (UINT4 *)bufferTOC, lSector, 1);
1936
1937 if (retval != 0)
1938 return (NULL);
1939
1940 return (fill_subchannel(bufferTOC));
1941 }
1942
1943 /*
1944 * Read max. SectorBurst of cdda sectors to buffer
1945 * via MMC standard READ CD command
1946 */
1947 int
ReadCddaSubMMC12(scgp,p,lSector,SectorBurstVal)1948 ReadCddaSubMMC12(scgp, p, lSector, SectorBurstVal)
1949 SCSI *scgp;
1950 UINT4 *p;
1951 unsigned lSector;
1952 unsigned SectorBurstVal;
1953 {
1954 register struct scg_cmd *scmd;
1955
1956 scmd = scgp->scmd;
1957
1958 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
1959 scmd->addr = (caddr_t)p;
1960 scmd->size = SectorBurstVal*(CD_FRAMESIZE_RAW + 16);
1961 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
1962 scmd->cdb_len = SC_G5_CDBLEN;
1963 scmd->sense_len = CCS_SENSE_LEN;
1964 scmd->cdb.g5_cdb.cmd = 0xbe; /* read cd command */
1965 scmd->cdb.g5_cdb.lun = scg_lun(scgp);
1966 scmd->cdb.g5_cdb.res = 1 << 1; /* expected sector type field CDDA */
1967 g5_cdbaddr(&scmd->cdb.g5_cdb, lSector);
1968 g5x_cdblen(&scmd->cdb.g5_cdb, SectorBurstVal);
1969 scmd->cdb.g5_cdb.count[3] = 1 << 4; /* User data */
1970 scmd->cdb.g5_cdb.res10 = 0x02; /* subcode 2 -> cdda + 16 * q sub */
1971
1972 if (scgp->verbose)
1973 fprintf(stderr, _("\nReadMMC12 CDDA + SUB..."));
1974
1975 scgp->cmdname = "ReadCD Sub MMC 12";
1976
1977 if (scg_cmd(scgp) < 0) {
1978 scgp->silent++;
1979 unit_ready(scgp);
1980 scgp->silent--;
1981 return (-1);
1982 }
1983
1984 /*
1985 * has all or something been read?
1986 */
1987 return (scg_getresid(scgp) != 0);
1988 }
1989
1990 static subq_chnl *ReadSubChannelsMMC __PR((SCSI *scgp, unsigned lSector));
1991 static subq_chnl *
ReadSubChannelsMMC(scgp,lSector)1992 ReadSubChannelsMMC(scgp, lSector)
1993 SCSI *scgp;
1994 unsigned lSector;
1995 {
1996 int retval = ReadCddaSubMMC12(scgp, (UINT4 *)bufferTOC, lSector, 1);
1997
1998 if (retval != 0)
1999 return (NULL);
2000
2001 return (fill_subchannel(bufferTOC));
2002 }
2003
2004 subq_chnl *
ReadSubChannelsFallbackMMC(scgp,lSector)2005 ReadSubChannelsFallbackMMC(scgp, lSector)
2006 SCSI *scgp;
2007 unsigned lSector;
2008 {
2009 static int ReadSubSony_unknown = 0;
2010 subq_chnl *retval = NULL;
2011
2012 scgp->silent++;
2013 if (ReadSubSony_unknown ||
2014 ((retval = ReadSubChannelsSony(scgp, lSector)) == NULL)) {
2015 /*
2016 * if the command is not available, use the regular
2017 * MMC ReadCd
2018 */
2019 if (retval == NULL && scg_sense_key(scgp) == SC_ILLEGAL_REQUEST) {
2020 ReadSubSony_unknown = 1;
2021 }
2022 scgp->silent--;
2023 ReadSubChannels = ReadSubChannelsMMC;
2024 return (ReadSubChannelsMMC(scgp, lSector));
2025 }
2026 scgp->silent--;
2027 return (retval);
2028 }
2029
2030 subq_chnl *
ReadStandardSub(scgp,lSector)2031 ReadStandardSub(scgp, lSector)
2032 SCSI *scgp;
2033 unsigned lSector;
2034 {
2035 if (0 == ReadStandardLowlevel(scgp, (UINT4 *)bufferTOC, lSector, 1,
2036 CD_FRAMESIZE_RAW + 16)) {
2037 return (NULL);
2038 }
2039 #if 0
2040 fprintf(stderr, "Subchannel Sec %x: %02x %02x %02x %02x\n",
2041 lSector,
2042 bufferTOC[CD_FRAMESIZE_RAW + 0],
2043 bufferTOC[CD_FRAMESIZE_RAW + 1],
2044 bufferTOC[CD_FRAMESIZE_RAW + 2],
2045 bufferTOC[CD_FRAMESIZE_RAW + 3]);
2046 #endif
2047 sc.control_adr = (bufferTOC[CD_FRAMESIZE_RAW + 0] << 4)
2048 | bufferTOC[CD_FRAMESIZE_RAW + 1];
2049 sc.track = from_bcd(bufferTOC[CD_FRAMESIZE_RAW + 2]);
2050 sc.index = from_bcd(bufferTOC[CD_FRAMESIZE_RAW + 3]);
2051 return (&sc);
2052 }
2053 /* ******** non standardized speed selects ********************** */
2054
2055 void
SpeedSelectSCSIToshiba(scgp,speed)2056 SpeedSelectSCSIToshiba(scgp, speed)
2057 SCSI *scgp;
2058 unsigned speed;
2059 {
2060 static unsigned char mode [4 + 3];
2061 unsigned char *page = mode + 4;
2062
2063 fillbytes((caddr_t)mode, sizeof (mode), '\0');
2064 /*
2065 * the first 4 mode bytes are zero.
2066 */
2067 page[0] = 0x20;
2068 page[1] = 1;
2069 page[2] = speed; /* 0 for single speed, 1 for double speed (3401) */
2070
2071 if (scgp->verbose)
2072 fprintf(stderr, _("\nspeed select Toshiba..."));
2073
2074 scgp->silent++;
2075 /*
2076 * do the scsi cmd
2077 */
2078 if (mode_select(scgp, mode, 7, 0, scgp->inq->data_format >= 2) < 0)
2079 fprintf(stderr, _("speed select Toshiba failed\n"));
2080 scgp->silent--;
2081 }
2082
2083 void
SpeedSelectSCSINEC(scgp,speed)2084 SpeedSelectSCSINEC(scgp, speed)
2085 SCSI *scgp;
2086 unsigned speed;
2087 {
2088 static unsigned char mode [4 + 8];
2089 unsigned char *page = mode + 4;
2090 register struct scg_cmd *scmd = scgp->scmd;
2091
2092 fillbytes((caddr_t)mode, sizeof (mode), '\0');
2093 /*
2094 * the first 4 mode bytes are zero.
2095 */
2096 page [0] = 0x0f; /* page code */
2097 page [1] = 6; /* parameter length */
2098 /*
2099 * bit 5 == 1 for single speed, otherwise double speed
2100 */
2101 page [2] = speed == 1 ? 1 << 5 : 0;
2102
2103 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
2104 scmd->addr = (caddr_t)mode;
2105 scmd->size = 12;
2106 scmd->flags = SCG_DISRE_ENA;
2107 scmd->cdb_len = SC_G1_CDBLEN;
2108 scmd->sense_len = CCS_SENSE_LEN;
2109 scmd->cdb.g1_cdb.cmd = 0xC5;
2110 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
2111 scmd->cdb.g1_cdb.addr[0] = 0 ? 1 : 0 | 1 ? 0x10 : 0;
2112 g1_cdblen(&scmd->cdb.g1_cdb, 12);
2113
2114 if (scgp->verbose)
2115 fprintf(stderr, _("\nspeed select NEC..."));
2116
2117 /*
2118 * do the scsi cmd
2119 */
2120 scgp->cmdname = "speed select NEC";
2121
2122 if (scg_cmd(scgp) < 0)
2123 errmsgno(EX_BAD, _("Speed select NEC failed.\n"));
2124 }
2125
2126 void
SpeedSelectSCSIPhilipsCDD2600(scgp,speed)2127 SpeedSelectSCSIPhilipsCDD2600(scgp, speed)
2128 SCSI *scgp;
2129 unsigned speed;
2130 {
2131 /*
2132 * MODE_SELECT, page = SCSI-2 save page disabled, reserved, reserved,
2133 * parm list len, flags
2134 */
2135 static unsigned char mode [4 + 8];
2136 unsigned char *page = mode + 4;
2137
2138 fillbytes((caddr_t)mode, sizeof (mode), '\0');
2139 /*
2140 * the first 4 mode bytes are zero.
2141 */
2142 page[0] = 0x23;
2143 page[1] = 6;
2144 page[2] = page [4] = speed;
2145 page[3] = 1;
2146
2147 if (scgp->verbose)
2148 fprintf(stderr, _("\nspeed select Philips..."));
2149 /*
2150 * do the scsi cmd
2151 */
2152 if (mode_select(scgp, mode, 12, 0, scgp->inq->data_format >= 2) < 0)
2153 errmsgno(EX_BAD, _("Speed select PhilipsCDD2600 failed.\n"));
2154 }
2155
2156 void
SpeedSelectSCSISony(scgp,speed)2157 SpeedSelectSCSISony(scgp, speed)
2158 SCSI *scgp;
2159 unsigned speed;
2160 {
2161 static unsigned char mode [4 + 4];
2162 unsigned char *page = mode + 4;
2163
2164 fillbytes((caddr_t)mode, sizeof (mode), '\0');
2165 /*
2166 * the first 4 mode bytes are zero.
2167 */
2168 page[0] = 0x31;
2169 page[1] = 2;
2170 page[2] = speed;
2171
2172 if (scgp->verbose)
2173 fprintf(stderr, _("\nspeed select Sony..."));
2174 /*
2175 * do the scsi cmd
2176 */
2177 scgp->silent++;
2178 if (mode_select(scgp, mode, 8, 0, scgp->inq->data_format >= 2) < 0)
2179 errmsgno(EX_BAD, _("Speed select Sony failed.\n"));
2180 scgp->silent--;
2181 }
2182
2183 void
SpeedSelectSCSIYamaha(scgp,speed)2184 SpeedSelectSCSIYamaha(scgp, speed)
2185 SCSI *scgp;
2186 unsigned speed;
2187 {
2188 static unsigned char mode [4 + 4];
2189 unsigned char *page = mode + 4;
2190
2191 fillbytes((caddr_t)mode, sizeof (mode), '\0');
2192 /*
2193 * the first 4 mode bytes are zero.
2194 */
2195 page[0] = 0x31;
2196 page[1] = 2;
2197 page[2] = speed;
2198
2199 if (scgp->verbose)
2200 fprintf(stderr, _("\nspeed select Yamaha..."));
2201 /*
2202 * do the scsi cmd
2203 */
2204 if (mode_select(scgp, mode, 8, 0, scgp->inq->data_format >= 2) < 0)
2205 errmsgno(EX_BAD, _("Speed select Yamaha failed.\n"));
2206 }
2207
2208 void
SpeedSelectSCSIMMC(scgp,speed)2209 SpeedSelectSCSIMMC(scgp, speed)
2210 SCSI *scgp;
2211 unsigned speed;
2212 {
2213 int spd;
2214 register struct scg_cmd *scmd = scgp->scmd;
2215
2216 if (speed == 0 || speed == 0xFFFF) {
2217 spd = 0xFFFF;
2218 } else {
2219 spd = (1764 * speed) / 10;
2220 }
2221 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
2222 scmd->flags = SCG_DISRE_ENA;
2223 scmd->cdb_len = SC_G5_CDBLEN;
2224 scmd->sense_len = CCS_SENSE_LEN;
2225 scmd->cdb.g5_cdb.cmd = 0xBB;
2226 scmd->cdb.g5_cdb.lun = scg_lun(scgp);
2227 i_to_2_byte(&scmd->cdb.g5_cdb.addr[0], spd);
2228 i_to_2_byte(&scmd->cdb.g5_cdb.addr[2], 0xffff);
2229
2230 if (scgp->verbose)
2231 fprintf(stderr, _("\nspeed select MMC..."));
2232
2233 scgp->cmdname = "set cd speed";
2234
2235 scgp->silent++;
2236 if (scg_cmd(scgp) < 0) {
2237 if (scg_sense_key(scgp) == SC_ILLEGAL_REQUEST &&
2238 scg_sense_code(scgp) == 0x20 &&
2239 scg_sense_qual(scgp) == 0x00) {
2240 /*
2241 * this optional command is not implemented
2242 * 0x05 0x20 0x00 is invalid command operation code
2243 */
2244 } else {
2245 scg_printerr(scgp);
2246 errmsgno(EX_BAD, _("Speed select MMC failed.\n"));
2247 }
2248 }
2249 scgp->silent--;
2250 }
2251
2252 /*
2253 * request vendor brand and model
2254 */
2255 unsigned char *
ScsiInquiry(scgp)2256 ScsiInquiry(scgp)
2257 SCSI *scgp;
2258 {
2259 static unsigned char *Inqbuffer = NULL;
2260 register struct scg_cmd *scmd = scgp->scmd;
2261
2262 if (Inqbuffer == NULL) {
2263 Inqbuffer = malloc(36);
2264 if (Inqbuffer == NULL) {
2265 errmsg(
2266 _("Cannot allocate memory for inquiry command in line %d\n"),
2267 __LINE__);
2268 return (NULL);
2269 }
2270 }
2271
2272 fillbytes(Inqbuffer, 36, '\0');
2273 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
2274 scmd->addr = (caddr_t)Inqbuffer;
2275 scmd->size = 36;
2276 scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
2277 scmd->cdb_len = SC_G0_CDBLEN;
2278 scmd->sense_len = CCS_SENSE_LEN;
2279 scmd->cdb.g0_cdb.cmd = SC_INQUIRY;
2280 scmd->cdb.g0_cdb.lun = scg_lun(scgp);
2281 scmd->cdb.g0_cdb.count = 36;
2282
2283 scgp->cmdname = "inquiry";
2284
2285 if (scg_cmd(scgp) < 0)
2286 return (NULL);
2287
2288 /*
2289 * define structure with inquiry data
2290 */
2291 memcpy(scgp->inq, Inqbuffer, sizeof (*scgp->inq));
2292
2293 if (scgp->verbose) {
2294 scg_prbytes(_("Inquiry Data :"),
2295 (Uchar *)Inqbuffer, 22 - scmd->resid);
2296 }
2297 return (Inqbuffer);
2298 }
2299
2300 #define SC_CLASS_EXTENDED_SENSE 0x07
2301 #define TESTUNITREADY_CMD 0
2302 #define TESTUNITREADY_CMDLEN 6
2303
2304 #define ADD_SENSECODE 12
2305 #define ADD_SC_QUALIFIER 13
2306 #define NO_MEDIA_SC 0x3a
2307 #define NO_MEDIA_SCQ 0x00
2308
2309 int
TestForMedium(scgp)2310 TestForMedium(scgp)
2311 SCSI *scgp;
2312 {
2313 register struct scg_cmd *scmd = scgp->scmd;
2314
2315 if (interface != GENERIC_SCSI) {
2316 return (1);
2317 }
2318
2319 /*
2320 * request READY status
2321 */
2322 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
2323 scmd->addr = (caddr_t)0;
2324 scmd->size = 0;
2325 scmd->flags = SCG_DISRE_ENA | (1 ? SCG_SILENT:0);
2326 scmd->cdb_len = SC_G0_CDBLEN;
2327 scmd->sense_len = CCS_SENSE_LEN;
2328 scmd->cdb.g0_cdb.cmd = SC_TEST_UNIT_READY;
2329 scmd->cdb.g0_cdb.lun = scg_lun(scgp);
2330
2331 if (scgp->verbose)
2332 fprintf(stderr, _("\ntest unit ready..."));
2333 scgp->silent++;
2334
2335 scgp->cmdname = "test unit ready";
2336
2337 if (scg_cmd(scgp) >= 0) {
2338 scgp->silent--;
2339 return (1);
2340 }
2341 scgp->silent--;
2342
2343 if (scmd->sense.code >= SC_CLASS_EXTENDED_SENSE) {
2344 return (scmd->u_sense.cmd_sense[ADD_SENSECODE] !=
2345 NO_MEDIA_SC ||
2346 scmd->u_sense.cmd_sense[ADD_SC_QUALIFIER] != NO_MEDIA_SCQ);
2347 } else {
2348 /*
2349 * analyse status.
2350 * 'check condition' is interpreted as not ready.
2351 */
2352 return ((scmd->u_scb.cmd_scb[0] & 0x1e) != 0x02);
2353 }
2354 }
2355
2356 int
StopPlaySCSI(scgp)2357 StopPlaySCSI(scgp)
2358 SCSI *scgp;
2359 {
2360 register struct scg_cmd *scmd = scgp->scmd;
2361
2362 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
2363 scmd->addr = NULL;
2364 scmd->size = 0;
2365 scmd->flags = SCG_DISRE_ENA;
2366 scmd->cdb_len = SC_G0_CDBLEN;
2367 scmd->sense_len = CCS_SENSE_LEN;
2368 scmd->cdb.g0_cdb.cmd = 0x1b;
2369 scmd->cdb.g0_cdb.lun = scg_lun(scgp);
2370
2371 if (scgp->verbose)
2372 fprintf(stderr, _("\nstop audio play"));
2373
2374 /*
2375 * do the scsi cmd
2376 */
2377 scgp->cmdname = "stop audio play";
2378
2379 return (scg_cmd(scgp) >= 0 ? 0 : -1);
2380 }
2381
2382 int
Play_atSCSI(scgp,from_sector,sectors)2383 Play_atSCSI(scgp, from_sector, sectors)
2384 SCSI *scgp;
2385 unsigned int from_sector;
2386 unsigned int sectors;
2387 {
2388 register struct scg_cmd *scmd = scgp->scmd;
2389
2390 fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
2391 scmd->addr = NULL;
2392 scmd->size = 0;
2393 scmd->flags = SCG_DISRE_ENA;
2394 scmd->cdb_len = SC_G1_CDBLEN;
2395 scmd->sense_len = CCS_SENSE_LEN;
2396 scmd->cdb.g1_cdb.cmd = 0x47;
2397 scmd->cdb.g1_cdb.lun = scg_lun(scgp);
2398 scmd->cdb.g1_cdb.addr[1] = (from_sector + 150) / (60*75);
2399 scmd->cdb.g1_cdb.addr[2] = ((from_sector + 150) / 75) % 60;
2400 scmd->cdb.g1_cdb.addr[3] = (from_sector + 150) % 75;
2401 scmd->cdb.g1_cdb.res6 = (from_sector + 150 + sectors) / (60*75);
2402 scmd->cdb.g1_cdb.count[0] = ((from_sector + 150 + sectors) / 75) % 60;
2403 scmd->cdb.g1_cdb.count[1] = (from_sector + 150 + sectors) % 75;
2404
2405 if (scgp->verbose)
2406 fprintf(stderr, _("\nplay sectors..."));
2407
2408 /*
2409 * do the scsi cmd
2410 */
2411 scgp->cmdname = "play sectors";
2412
2413 return (scg_cmd(scgp) >= 0 ? 0 : -1);
2414 }
2415
2416 static caddr_t scsibuffer; /* page aligned scsi transfer buffer */
2417
2418 EXPORT void init_scsibuf __PR((SCSI *, long));
2419
2420 void
init_scsibuf(scgp,amt)2421 init_scsibuf(scgp, amt)
2422 SCSI *scgp;
2423 long amt;
2424 {
2425 if (scsibuffer != NULL) {
2426 errmsgno(EX_BAD,
2427 _("The SCSI transfer buffer has already been allocated!\n"));
2428 exit(SETUPSCSI_ERROR);
2429 }
2430 scsibuffer = scg_getbuf(scgp, amt);
2431 if (scsibuffer == NULL) {
2432 errmsg(_("Could not get SCSI transfer buffer!\n"));
2433 exit(SETUPSCSI_ERROR);
2434 }
2435 global.buf = scsibuffer;
2436 }
2437