1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
|
/*
* This file has been modified for the cdrkit suite.
*
* The behaviour and appearence of the program code below can differ to a major
* extent from the version distributed by the original author(s).
*
* For details, see Changelog file distributed with the cdrkit package. If you
* received this file from another source then ask the distributing person for
* a log of modifications.
*
*/
/* @(#)scsi_cdr.c 1.137 04/05/25 Copyright 1995-2004 J. Schilling */
/*
* SCSI command functions for cdrecord
* covering pre-MMC standard functions up to MMC-2
*
* Copyright (c) 1995-2004 J. Schilling
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; see the file COPYING. If not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* NOTICE: The Philips CDD 521 has several firmware bugs.
* One of them is not to respond to a SCSI selection
* within 200ms if the general load on the
* SCSI bus is high. To deal with this problem
* most of the SCSI commands are send with the
* SCG_CMD_RETRY flag enabled.
*/
#include <mconfig.h>
#include <stdio.h>
#include <standard.h>
#include <stdxlib.h>
#include <unixstd.h>
#include <fctldefs.h>
#include <errno.h>
#include <strdefs.h>
#include <timedefs.h>
#include <utypes.h>
#include <btorder.h>
#include <intcvt.h>
#include <schily.h>
#include <usal/usalcmd.h>
#include <usal/scsidefs.h>
#include <usal/scsireg.h>
#include <usal/scsitransp.h>
#include "scsimmc.h"
#include "wodim.h"
#include "scsi_scan.h"
#define strbeg(s1, s2) (strstr((s2), (s1)) == (s2))
BOOL unit_ready(SCSI *usalp);
BOOL wait_unit_ready(SCSI *usalp, int secs);
BOOL scsi_in_progress(SCSI *usalp);
BOOL cdr_underrun(SCSI *usalp);
int test_unit_ready(SCSI *usalp);
int rezero_unit(SCSI *usalp);
int request_sense(SCSI *usalp);
int request_sense_b(SCSI *usalp, caddr_t bp, int cnt);
int inquiry(SCSI *usalp, caddr_t, int);
int read_capacity(SCSI *usalp);
void print_capacity(SCSI *usalp, FILE *f);
int scsi_load_unload(SCSI *usalp, int);
int scsi_prevent_removal(SCSI *usalp, int);
int scsi_start_stop_unit(SCSI *usalp, int, int, BOOL immed);
int scsi_set_speed(SCSI *usalp, int readspeed, int writespeed, int rotctl);
int scsi_get_speed(SCSI *usalp, int *readspeedp, int *writespeedp);
int qic02(SCSI *usalp, int);
int write_xscsi(SCSI *usalp, caddr_t, long, long, int);
int write_xg0(SCSI *usalp, caddr_t, long, long, int);
int write_xg1(SCSI *usalp, caddr_t, long, long, int);
int write_xg5(SCSI *usalp, caddr_t, long, long, int);
int seek_scsi(SCSI *usalp, long addr);
int seek_g0(SCSI *usalp, long addr);
int seek_g1(SCSI *usalp, long addr);
int scsi_flush_cache(SCSI *usalp, BOOL immed);
int read_buffer(SCSI *usalp, caddr_t bp, int cnt, int mode);
int write_buffer(SCSI *usalp, char *buffer, long length, int mode,
int bufferid, long offset);
int read_subchannel(SCSI *usalp, caddr_t bp, int track, int cnt, int msf,
int subq, int fmt);
int read_toc(SCSI *usalp, caddr_t, int, int, int, int);
int read_toc_philips(SCSI *usalp, caddr_t, int, int, int, int);
int read_header(SCSI *usalp, caddr_t, long, int, int);
int read_disk_info(SCSI *usalp, caddr_t, int);
int read_track_info(SCSI *usalp, caddr_t, int type, int addr, int cnt);
int read_rzone_info(SCSI *usalp, caddr_t bp, int cnt);
int reserve_tr_rzone(SCSI *usalp, long size);
int read_dvd_structure(SCSI *usalp, caddr_t bp, int cnt, int addr, int layer,
int fmt);
int send_dvd_structure(SCSI *usalp, caddr_t bp, int cnt, int layer, int fmt);
int send_opc(SCSI *usalp, caddr_t, int cnt, int doopc);
int read_track_info_philips(SCSI *usalp, caddr_t, int, int);
int scsi_close_tr_session(SCSI *usalp, int type, int track, BOOL immed);
int read_master_cue(SCSI *usalp, caddr_t bp, int sheet, int cnt);
int send_cue_sheet(SCSI *usalp, caddr_t bp, long size);
int read_buff_cap(SCSI *usalp, long *, long *);
int scsi_blank(SCSI *usalp, long addr, int blanktype, BOOL immed);
int scsi_format(SCSI *usalp, caddr_t addr, int size, BOOL background);
int scsi_set_streaming(SCSI *usalp, caddr_t addr, int size);
BOOL allow_atapi(SCSI *usalp, BOOL new);
int mode_select(SCSI *usalp, Uchar *, int, int, int);
int mode_sense(SCSI *usalp, Uchar *dp, int cnt, int page, int pcf);
int mode_select_sg0(SCSI *usalp, Uchar *, int, int, int);
int mode_sense_sg0(SCSI *usalp, Uchar *dp, int cnt, int page, int pcf);
int mode_select_g0(SCSI *usalp, Uchar *, int, int, int);
int mode_select_g1(SCSI *usalp, Uchar *, int, int, int);
int mode_sense_g0(SCSI *usalp, Uchar *dp, int cnt, int page, int pcf);
int mode_sense_g1(SCSI *usalp, Uchar *dp, int cnt, int page, int pcf);
int read_tochdr(SCSI *usalp, cdr_t *, int *, int *);
int read_cdtext(SCSI *usalp);
int read_trackinfo(SCSI *usalp, int, long *, struct msf *, int *, int *,
int *);
int read_B0(SCSI *usalp, BOOL isbcd, long *b0p, long *lop);
int read_session_offset(SCSI *usalp, long *);
int read_session_offset_philips(SCSI *usalp, long *);
int sense_secsize(SCSI *usalp, int current);
int select_secsize(SCSI *usalp, int);
BOOL is_cddrive(SCSI *usalp);
BOOL is_unknown_dev(SCSI *usalp);
int read_scsi(SCSI *usalp, caddr_t, long, int);
int read_g0(SCSI *usalp, caddr_t, long, int);
int read_g1(SCSI *usalp, caddr_t, long, int);
BOOL getdev(SCSI *usalp, BOOL);
void printinq(SCSI *usalp, FILE *f);
void printdev(SCSI *usalp);
BOOL do_inquiry(SCSI *usalp, BOOL);
BOOL recovery_needed(SCSI *usalp, cdr_t *);
int scsi_load(SCSI *usalp, cdr_t *);
int scsi_unload(SCSI *usalp, cdr_t *);
int scsi_cdr_write(SCSI *usalp, caddr_t bp, long sectaddr, long size,
int blocks, BOOL islast);
struct cd_mode_page_2A * mmc_cap(SCSI *usalp, Uchar *modep);
void mmc_getval(struct cd_mode_page_2A *mp, BOOL *cdrrp, BOOL *cdwrp,
BOOL *cdrrwp, BOOL *cdwrwp, BOOL *dvdp, BOOL *dvdwp);
BOOL is_mmc(SCSI *usalp, BOOL *cdwp, BOOL *dvdwp);
BOOL mmc_check(SCSI *usalp, BOOL *cdrrp, BOOL *cdwrp, BOOL *cdrrwp,
BOOL *cdwrwp, BOOL *dvdp, BOOL *dvdwp);
static void print_speed(char *fmt, int val);
void print_capabilities(SCSI *usalp);
BOOL
unit_ready(SCSI *usalp)
{
register struct usal_cmd *scmd = usalp->scmd;
if (test_unit_ready(usalp) >= 0) /* alles OK */
return (TRUE);
else if (scmd->error >= SCG_FATAL) /* nicht selektierbar */
return (FALSE);
if (usal_sense_key(usalp) == SC_UNIT_ATTENTION) {
if (test_unit_ready(usalp) >= 0) /* alles OK */
return (TRUE);
}
if ((usal_cmd_status(usalp) & ST_BUSY) != 0) {
/*
* Busy/reservation_conflict
*/
usleep(500000);
if (test_unit_ready(usalp) >= 0) /* alles OK */
return (TRUE);
}
if (usal_sense_key(usalp) == -1) { /* non extended Sense */
if (usal_sense_code(usalp) == 4) /* NOT_READY */
return (FALSE);
return (TRUE);
}
/* FALSE wenn NOT_READY */
return (usal_sense_key(usalp) != SC_NOT_READY);
}
BOOL
wait_unit_ready(SCSI *usalp, int secs)
{
int i;
int c;
int k;
int ret;
usalp->silent++;
ret = test_unit_ready(usalp); /* eat up unit attention */
if (ret < 0)
ret = test_unit_ready(usalp); /* got power on condition? */
usalp->silent--;
if (ret >= 0) /* success that's enough */
return (TRUE);
usalp->silent++;
for (i = 0; i < secs && (ret = test_unit_ready(usalp)) < 0; i++) {
if (usalp->scmd->scb.busy != 0) {
sleep(1);
continue;
}
c = usal_sense_code(usalp);
k = usal_sense_key(usalp);
/*
* Abort quickly if it does not make sense to wait.
* 0x30 == Cannot read medium
* 0x3A == Medium not present
*/
if ((k == SC_NOT_READY && (c == 0x3A || c == 0x30)) ||
(k == SC_MEDIUM_ERROR)) {
if (usalp->silent <= 1)
usal_printerr(usalp);
usalp->silent--;
return (FALSE);
}
sleep(1);
}
usalp->silent--;
if (ret < 0)
return (FALSE);
return (TRUE);
}
BOOL
scsi_in_progress(SCSI *usalp)
{
if (usal_sense_key(usalp) == SC_NOT_READY &&
/*
* Logigal unit not ready operation/long_write in progress
*/
usal_sense_code(usalp) == 0x04 &&
(usal_sense_qual(usalp) == 0x04 || /* CyberDr. "format in progress"*/
usal_sense_qual(usalp) == 0x07 || /* "operation in progress" */
usal_sense_qual(usalp) == 0x08)) { /* "long write in progress" */
return (TRUE);
} else {
if (usalp->silent <= 1)
usal_printerr(usalp);
}
return (FALSE);
}
BOOL
cdr_underrun(SCSI *usalp)
{
if ((usal_sense_key(usalp) != SC_ILLEGAL_REQUEST &&
usal_sense_key(usalp) != SC_MEDIUM_ERROR))
return (FALSE);
if ((usal_sense_code(usalp) == 0x21 &&
(usal_sense_qual(usalp) == 0x00 || /* logical block address out of range */
usal_sense_qual(usalp) == 0x02)) || /* invalid address for write */
(usal_sense_code(usalp) == 0x0C &&
usal_sense_qual(usalp) == 0x09)) { /* write error - loss of streaming */
return (TRUE);
}
/*
* XXX Bei manchen Brennern kommt mach dem der Brennvorgang bereits
* XXX eine Weile gelaufen ist ein 5/24/0 Invalid field in CDB.
* XXX Daher sollte man testen ob schon geschrieben wurde...
*/
return (FALSE);
}
int
test_unit_ready(SCSI *usalp)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)0;
scmd->size = 0;
scmd->flags = SCG_DISRE_ENA | (usalp->silent ? SCG_SILENT:0);
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = SC_TEST_UNIT_READY;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
usalp->cmdname = "test unit ready";
return (usal_cmd(usalp));
}
int
rezero_unit(SCSI *usalp)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)0;
scmd->size = 0;
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = SC_REZERO_UNIT;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
usalp->cmdname = "rezero unit";
return (usal_cmd(usalp));
}
int
request_sense(SCSI *usalp)
{
char sensebuf[CCS_SENSE_LEN];
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = sensebuf;
scmd->size = sizeof (sensebuf);
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = SC_REQUEST_SENSE;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
scmd->cdb.g0_cdb.count = CCS_SENSE_LEN;
usalp->cmdname = "request_sense";
if (usal_cmd(usalp) < 0)
return (-1);
usal_prsense((Uchar *)sensebuf, CCS_SENSE_LEN - usal_getresid(usalp));
return (0);
}
int
request_sense_b(SCSI *usalp, caddr_t bp, int cnt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = SC_REQUEST_SENSE;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
scmd->cdb.g0_cdb.count = cnt;
usalp->cmdname = "request_sense";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
inquiry(SCSI *usalp, caddr_t bp, int cnt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes(bp, cnt, '\0');
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = SC_INQUIRY;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
scmd->cdb.g0_cdb.count = cnt;
usalp->cmdname = "inquiry";
if (usal_cmd(usalp) < 0)
return (-1);
if (usalp->verbose)
usal_prbytes("Inquiry Data :", (Uchar *)bp, cnt - usal_getresid(usalp));
return (0);
}
int
read_capacity(SCSI *usalp)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)usalp->cap;
scmd->size = sizeof (struct scsi_capacity);
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x25; /* Read Capacity */
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
g1_cdblen(&scmd->cdb.g1_cdb, 0); /* Full Media */
usalp->cmdname = "read capacity";
if (usal_cmd(usalp) < 0) {
return (-1);
} else {
long cbsize;
long cbaddr;
/*
* c_bsize & c_baddr are signed Int32_t
* so we use signed int conversion here.
*/
cbsize = a_to_4_byte(&usalp->cap->c_bsize);
cbaddr = a_to_4_byte(&usalp->cap->c_baddr);
usalp->cap->c_bsize = cbsize;
usalp->cap->c_baddr = cbaddr;
}
return (0);
}
void
print_capacity(SCSI *usalp, FILE *f)
{
long kb;
long mb;
long prmb;
double dkb;
dkb = (usalp->cap->c_baddr+1.0) * (usalp->cap->c_bsize/1024.0);
kb = dkb;
mb = dkb / 1024.0;
prmb = dkb / 1000.0 * 1.024;
fprintf(f, "Capacity: %ld Blocks = %ld kBytes = %ld MBytes = %ld prMB\n",
(long)usalp->cap->c_baddr+1, kb, mb, prmb);
fprintf(f, "Sectorsize: %ld Bytes\n", (long)usalp->cap->c_bsize);
}
int
scsi_load_unload(SCSI *usalp, int load)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G5_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g5_cdb.cmd = 0xA6;
scmd->cdb.g5_cdb.lun = usal_lun(usalp);
scmd->cdb.g5_cdb.addr[1] = load?3:2;
scmd->cdb.g5_cdb.count[2] = 0; /* slot # */
usalp->cmdname = "medium load/unload";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
scsi_prevent_removal(SCSI *usalp, int prevent)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = 0x1E;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
scmd->cdb.g0_cdb.count = prevent & 1;
usalp->cmdname = "prevent/allow medium removal";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
scsi_start_stop_unit(SCSI *usalp, int flg, int loej, BOOL immed)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = 0x1B; /* Start Stop Unit */
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
scmd->cdb.g0_cdb.count = (flg ? 1:0) | (loej ? 2:0);
if (immed)
scmd->cdb.cmd_cdb[1] |= 0x01;
usalp->cmdname = "start/stop unit";
return (usal_cmd(usalp));
}
int
scsi_set_streaming(SCSI *usalp, caddr_t perf_desc, int size)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = perf_desc;
scmd->size = size;
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G5_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g5_cdb.cmd = 0xB6;
scmd->cdb.cmd_cdb[11] = 0;
scmd->cdb.cmd_cdb[10] = size;
usalp->cmdname = "set streaming";
if(usalp->verbose)
fprintf(stderr, "scsi_set_streaming\n");
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
scsi_set_speed(SCSI *usalp, int readspeed, int writespeed, int rotctl)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G5_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g5_cdb.cmd = 0xBB;
scmd->cdb.g5_cdb.lun = usal_lun(usalp);
if (readspeed < 0)
i_to_2_byte(&scmd->cdb.g5_cdb.addr[0], 0xFFFF);
else
i_to_2_byte(&scmd->cdb.g5_cdb.addr[0], readspeed);
if (writespeed < 0)
i_to_2_byte(&scmd->cdb.g5_cdb.addr[2], 0xFFFF);
else
i_to_2_byte(&scmd->cdb.g5_cdb.addr[2], writespeed);
scmd->cdb.cmd_cdb[1] |= rotctl & 0x03;
usalp->cmdname = "set cd speed";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
scsi_get_speed(SCSI *usalp, int *readspeedp, int *writespeedp)
{
struct cd_mode_page_2A *mp;
Uchar m[256];
int val;
usalp->silent++;
mp = mmc_cap(usalp, m); /* Get MMC capabilities in allocated mp */
usalp->silent--;
if (mp == NULL)
return (-1); /* Pre SCSI-3/mmc drive */
val = a_to_u_2_byte(mp->cur_read_speed);
if (readspeedp)
*readspeedp = val;
if (mp->p_len >= 28)
val = a_to_u_2_byte(mp->v3_cur_write_speed);
else
val = a_to_u_2_byte(mp->cur_write_speed);
if (writespeedp)
*writespeedp = val;
return (0);
}
int
qic02(SCSI *usalp, int cmd)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)0;
scmd->size = 0;
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = DEF_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = 0x0D; /* qic02 Sysgen SC4000 */
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
scmd->cdb.g0_cdb.mid_addr = cmd;
usalp->cmdname = "qic 02";
return (usal_cmd(usalp));
}
#define G0_MAXADDR 0x1FFFFFL
int
write_xscsi(SCSI *usalp, caddr_t bp, long addr, long size, int cnt)
{
if (addr <= G0_MAXADDR)
return (write_xg0(usalp, bp, addr, size, cnt));
else
return (write_xg1(usalp, bp, addr, size, cnt));
}
int
write_xg0(SCSI *usalp,
caddr_t bp /* address of buffer */,
long addr /* disk address (sector) to put */,
long size /* number of bytes to transfer */,
int cnt /* sectorcount */)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = size;
scmd->flags = SCG_DISRE_ENA|SCG_CMD_RETRY;
/* scmd->flags = SCG_DISRE_ENA;*/
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = SC_WRITE;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
g0_cdbaddr(&scmd->cdb.g0_cdb, addr);
scmd->cdb.g0_cdb.count = cnt;
usalp->cmdname = "write_g0";
if (usal_cmd(usalp) < 0)
return (-1);
return (size - usal_getresid(usalp));
}
int
write_xg1(SCSI *usalp,
caddr_t bp /* address of buffer */,
long addr /* disk address (sector) to put */,
long size /* number of bytes to transfer */,
int cnt /* sectorcount */)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = size;
scmd->flags = SCG_DISRE_ENA|SCG_CMD_RETRY;
/* scmd->flags = SCG_DISRE_ENA;*/
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = SC_EWRITE;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
g1_cdbaddr(&scmd->cdb.g1_cdb, addr);
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "write_g1";
if (usal_cmd(usalp) < 0)
return (-1);
return (size - usal_getresid(usalp));
}
int
write_xg5(SCSI *usalp,
caddr_t bp /* address of buffer */,
long addr /* disk address (sector) to put */,
long size /* number of bytes to transfer */,
int cnt /* sectorcount */)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = size;
scmd->flags = SCG_DISRE_ENA|SCG_CMD_RETRY;
/* scmd->flags = SCG_DISRE_ENA;*/
scmd->cdb_len = SC_G5_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g5_cdb.cmd = 0xAA;
scmd->cdb.g5_cdb.lun = usal_lun(usalp);
g5_cdbaddr(&scmd->cdb.g5_cdb, addr);
g5_cdblen(&scmd->cdb.g5_cdb, cnt);
usalp->cmdname = "write_g5";
if (usal_cmd(usalp) < 0)
return (-1);
return (size - usal_getresid(usalp));
}
int
seek_scsi(SCSI *usalp, long addr)
{
if (addr <= G0_MAXADDR)
return (seek_g0(usalp, addr));
else
return (seek_g1(usalp, addr));
}
int
seek_g0(SCSI *usalp, long addr)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = 0x0B; /* Seek */
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
g0_cdbaddr(&scmd->cdb.g0_cdb, addr);
usalp->cmdname = "seek_g0";
return (usal_cmd(usalp));
}
int
seek_g1(SCSI *usalp, long addr)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x2B; /* Seek G1 */
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
g1_cdbaddr(&scmd->cdb.g1_cdb, addr);
usalp->cmdname = "seek_g1";
return (usal_cmd(usalp));
}
int
scsi_flush_cache(SCSI *usalp, BOOL immed)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 2 * 60; /* Max: sizeof (CDR-cache)/150KB/s */
scmd->cdb.g1_cdb.cmd = 0x35;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
if (immed)
scmd->cdb.cmd_cdb[1] |= 0x02;
usalp->cmdname = "flush cache";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_buffer(SCSI *usalp, caddr_t bp, int cnt, int mode)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->dma_read = 1;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x3C; /* Read Buffer */
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
scmd->cdb.cmd_cdb[1] |= (mode & 7);
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "read buffer";
return (usal_cmd(usalp));
}
int
write_buffer(SCSI *usalp, char *buffer, long length, int mode, int bufferid,
long offset)
{
register struct usal_cmd *scmd = usalp->scmd;
char *cdb;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = buffer;
scmd->size = length;
scmd->flags = SCG_DISRE_ENA|SCG_CMD_RETRY;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
cdb = (char *)scmd->cdb.cmd_cdb;
cdb[0] = 0x3B;
cdb[1] = mode & 7;
cdb[2] = bufferid;
cdb[3] = offset >> 16;
cdb[4] = (offset >> 8) & 0xff;
cdb[5] = offset & 0xff;
cdb[6] = length >> 16;
cdb[7] = (length >> 8) & 0xff;
cdb[8] = length & 0xff;
usalp->cmdname = "write_buffer";
if (usal_cmd(usalp) >= 0)
return (1);
return (0);
}
int
read_subchannel(SCSI *usalp, caddr_t bp, int track, int cnt, int msf, int subq,
int fmt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x42;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
if (msf)
scmd->cdb.g1_cdb.res = 1;
if (subq)
scmd->cdb.g1_cdb.addr[0] = 0x40;
scmd->cdb.g1_cdb.addr[1] = fmt;
scmd->cdb.g1_cdb.res6 = track;
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "read subchannel";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_toc(SCSI *usalp, caddr_t bp, int track, int cnt, int msf, int fmt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x43;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
if (msf)
scmd->cdb.g1_cdb.res = 1;
scmd->cdb.g1_cdb.addr[0] = fmt & 0x0F;
scmd->cdb.g1_cdb.res6 = track;
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "read toc";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_toc_philips(SCSI *usalp, caddr_t bp, int track, int cnt, int msf, int fmt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 4 * 60; /* May last 174s on a TEAC CD-R55S */
scmd->cdb.g1_cdb.cmd = 0x43;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
if (msf)
scmd->cdb.g1_cdb.res = 1;
scmd->cdb.g1_cdb.res6 = track;
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
if (fmt & 1)
scmd->cdb.g1_cdb.vu_96 = 1;
if (fmt & 2)
scmd->cdb.g1_cdb.vu_97 = 1;
usalp->cmdname = "read toc";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_header(SCSI *usalp, caddr_t bp, long addr, int cnt, int msf)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x44;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
if (msf)
scmd->cdb.g1_cdb.res = 1;
g1_cdbaddr(&scmd->cdb.g1_cdb, addr);
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "read header";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_disk_info(SCSI *usalp, caddr_t bp, int cnt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 4 * 60; /* Needs up to 2 minutes */
scmd->cdb.g1_cdb.cmd = 0x51;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "read disk info";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_track_info(SCSI *usalp, caddr_t bp, int type, int addr, int cnt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 4 * 60; /* Needs up to 2 minutes */
scmd->cdb.g1_cdb.cmd = 0x52;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
/* scmd->cdb.cmd_cdb[1] = type & 0x03;*/
scmd->cdb.cmd_cdb[1] = type;
g1_cdbaddr(&scmd->cdb.g1_cdb, addr); /* LBA/Track/Session */
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "read track info";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
reserve_track(SCSI *usalp, Ulong size)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof(*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x53;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
i_to_4_byte(&scmd->cdb.g1_cdb.addr[3], size);
usalp->cmdname = "reserve track";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_rzone_info(SCSI *usalp, caddr_t bp, int cnt)
{
return (read_track_info(usalp, bp, TI_TYPE_LBA, 0, cnt));
}
int
reserve_tr_rzone(SCSI *usalp, long size /* number of blocks */)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)0;
scmd->size = 0;
scmd->flags = SCG_DISRE_ENA|SCG_CMD_RETRY;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x53;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
i_to_4_byte(&scmd->cdb.g1_cdb.addr[3], size);
usalp->cmdname = "reserve_track_rzone";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_dvd_structure(SCSI *usalp, caddr_t bp, int cnt, int addr, int layer,
int fmt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G5_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 4 * 60; /* Needs up to 2 minutes ??? */
scmd->cdb.g5_cdb.cmd = 0xAD;
scmd->cdb.g5_cdb.lun = usal_lun(usalp);
g5_cdbaddr(&scmd->cdb.g5_cdb, addr);
g5_cdblen(&scmd->cdb.g5_cdb, cnt);
scmd->cdb.g5_cdb.count[0] = layer;
scmd->cdb.g5_cdb.count[1] = fmt;
usalp->cmdname = "read dvd structure";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
send_dvd_structure(SCSI *usalp, caddr_t bp, int cnt, int layer, int fmt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G5_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 4 * 60; /* Needs up to 2 minutes ??? */
scmd->cdb.g5_cdb.cmd = 0xBF;
scmd->cdb.g5_cdb.lun = usal_lun(usalp);
g5_cdblen(&scmd->cdb.g5_cdb, cnt);
scmd->cdb.cmd_cdb[7] = fmt;
usalp->cmdname = "send dvd structure";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
send_opc(SCSI *usalp, caddr_t bp, int cnt, int doopc)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 60;
scmd->cdb.g1_cdb.cmd = 0x54;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
scmd->cdb.g1_cdb.reladr = doopc?1:0;
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "send opc";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_track_info_philips(SCSI *usalp, caddr_t bp, int track, int cnt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0xE5;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
g1_cdbaddr(&scmd->cdb.g1_cdb, track);
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "read track info";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
scsi_close_tr_session(SCSI *usalp, int type, int track, BOOL immed)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 8 * 60; /* Needs up to 4 minutes */
scmd->cdb.g1_cdb.cmd = 0x5B;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
scmd->cdb.g1_cdb.addr[0] = type;
scmd->cdb.g1_cdb.addr[3] = track;
if (immed)
scmd->cdb.g1_cdb.reladr = 1;
/* scmd->cdb.cmd_cdb[1] |= 0x01;*/
#ifdef nono
scmd->cdb.g1_cdb.reladr = 1; /* IMM hack to test Mitsumi behaviour*/
#endif
usalp->cmdname = "close track/session";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
read_master_cue(SCSI *usalp, caddr_t bp, int sheet, int cnt)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x59; /* Read master cue */
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
scmd->cdb.g1_cdb.addr[2] = sheet;
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "read master cue";
if (usal_cmd(usalp) < 0)
return (-1);
return (0);
}
int
send_cue_sheet(SCSI *usalp, caddr_t bp, long size)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = size;
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x5D; /* Send CUE sheet */
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
g1_cdblen(&scmd->cdb.g1_cdb, size);
usalp->cmdname = "send_cue_sheet";
if (usal_cmd(usalp) < 0)
return (-1);
return (size - scmd->resid);
}
int
read_buff_cap(SCSI *usalp, long *sp, long *fp)
{
char resp[12];
Ulong freespace;
Ulong bufsize;
int per;
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)resp;
scmd->size = sizeof (resp);
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x5C; /* Read buffer cap */
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
g1_cdblen(&scmd->cdb.g1_cdb, sizeof (resp));
usalp->cmdname = "read buffer cap";
if (usal_cmd(usalp) < 0)
return (-1);
bufsize = a_to_u_4_byte(&resp[4]);
freespace = a_to_u_4_byte(&resp[8]);
if (sp)
*sp = bufsize;
if (fp)
*fp = freespace;
if (usalp->verbose || (sp == 0 && fp == 0))
printf("BFree: %ld K BSize: %ld K\n", freespace >> 10, bufsize >> 10);
if (bufsize == 0)
return (0);
per = (100 * (bufsize - freespace)) / bufsize;
if (per < 0)
return (0);
if (per > 100)
return (100);
return (per);
}
int
scsi_blank(SCSI *usalp, long addr, int blanktype, BOOL immed)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G5_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 160 * 60; /* full blank at 1x could take 80 minutes */
scmd->cdb.g5_cdb.cmd = 0xA1; /* Blank */
scmd->cdb.g0_cdb.high_addr = blanktype;
g1_cdbaddr(&scmd->cdb.g5_cdb, addr);
if (immed)
scmd->cdb.g5_cdb.res |= 8;
/* scmd->cdb.cmd_cdb[1] |= 0x10;*/
usalp->cmdname = "blank unit";
return (usal_cmd(usalp));
}
int
scsi_format(SCSI *usalp, caddr_t addr, int size, BOOL background)
{
register struct usal_cmd *scmd = usalp->scmd;
int progress=0, ret=-1, pid=-1;
unsigned char sense_table[18];
int i;
printf("scsi_format: preparing\n");
fillbytes((caddr_t)scmd, sizeof(*scmd), '\0');
scmd->addr = addr;
scmd->size = size;
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G5_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->timeout = 160 * 60; /* Do not know what to set */
scmd->cdb.g5_cdb.cmd = 0x04; /* Format Unit */
scmd->cdb.cmd_cdb[1] = 0x11; /* "FmtData" and "Format Code" */
scmd->cdb.cmd_cdb[5] = 0;
usalp->cmdname = "format unit";
printf("scsi_format: running\n");
ret = (usal_cmd(usalp));
printf("scsi_format: post processing %d\n", ret);
if (ret == -1) return ret;
if (background) {
if ((pid=fork()) == (pid_t)-1)
perror ("- [unable to fork()]");
else {
if (!pid) {
while (1) {
if (test_unit_ready(usalp) >= 0)
break;
sleep(1);
}
return ret;
}
}
}
printf("Formating in progress: 0.00 %% done.");
sleep(20);
i = 0;
while (progress < 0xfff0 && !(progress == 0 && i > 50)) {
test_unit_ready(usalp);
request_sense_b(usalp, (caddr_t)sense_table, 18);
progress = sense_table[16]<<8|sense_table[17];
printf("\rFormating in progress: %.2f %% done [%d]. ", (float)(progress*100)/0x10000,progress);
usleep(100000);
i++;
/*for (i=0; i < 18; i++) {
printf("%d ", sense_table[i]);
}*/
}
sleep(10);
printf("\rFormating in progress: 100.00 %% done. \n");
if (pid) exit (0);
return ret;
}
/*
* XXX First try to handle ATAPI:
* XXX ATAPI cannot handle SCSI 6 byte commands.
* XXX We try to simulate 6 byte mode sense/select.
*/
static BOOL is_atapi;
BOOL
allow_atapi(SCSI *usalp, BOOL new)
{
BOOL old = is_atapi;
Uchar mode[256];
if (new == old)
return (old);
usalp->silent++;
/*
* If a bad drive has been reset before, we may need to fire up two
* test unit ready commands to clear status.
*/
(void) unit_ready(usalp);
if (new &&
mode_sense_g1(usalp, mode, 8, 0x3F, 0) < 0) { /* All pages current */
new = FALSE;
}
usalp->silent--;
is_atapi = new;
return (old);
}
int
mode_select(SCSI *usalp, Uchar *dp, int cnt, int smp, int pf)
{
if (is_atapi)
return (mode_select_sg0(usalp, dp, cnt, smp, pf));
return (mode_select_g0(usalp, dp, cnt, smp, pf));
}
int
mode_sense(SCSI *usalp, Uchar *dp, int cnt, int page, int pcf)
{
if (is_atapi)
return (mode_sense_sg0(usalp, dp, cnt, page, pcf));
return (mode_sense_g0(usalp, dp, cnt, page, pcf));
}
/*
* Simulate mode select g0 with mode select g1.
*/
int
mode_select_sg0(SCSI *usalp, Uchar *dp, int cnt, int smp, int pf)
{
Uchar xmode[256+4];
int amt = cnt;
if (amt < 1 || amt > 255) {
/* XXX clear SCSI error codes ??? */
return (-1);
}
if (amt < 4) { /* Data length. medium type & VU */
amt += 1;
} else {
amt += 4;
movebytes(&dp[4], &xmode[8], cnt-4);
}
xmode[0] = 0;
xmode[1] = 0;
xmode[2] = dp[1];
xmode[3] = dp[2];
xmode[4] = 0;
xmode[5] = 0;
i_to_2_byte(&xmode[6], (unsigned int)dp[3]);
if (usalp->verbose) usal_prbytes("Mode Parameters (un-converted)", dp, cnt);
return (mode_select_g1(usalp, xmode, amt, smp, pf));
}
/*
* Simulate mode sense g0 with mode sense g1.
*/
int
mode_sense_sg0(SCSI *usalp, Uchar *dp, int cnt, int page, int pcf)
{
Uchar xmode[256+4];
int amt = cnt;
int len;
if (amt < 1 || amt > 255) {
/* XXX clear SCSI error codes ??? */
return (-1);
}
fillbytes((caddr_t)xmode, sizeof (xmode), '\0');
if (amt < 4) { /* Data length. medium type & VU */
amt += 1;
} else {
amt += 4;
}
if (mode_sense_g1(usalp, xmode, amt, page, pcf) < 0)
return (-1);
amt = cnt - usal_getresid(usalp);
/*
* For tests: Solaris 8 & LG CD-ROM always returns resid == amt
*/
/* amt = cnt;*/
if (amt > 4)
movebytes(&xmode[8], &dp[4], amt-4);
len = a_to_u_2_byte(xmode);
if (len == 0) {
dp[0] = 0;
} else if (len < 6) {
if (len > 2)
len = 2;
dp[0] = len;
} else {
dp[0] = len - 3;
}
dp[1] = xmode[2];
dp[2] = xmode[3];
len = a_to_u_2_byte(&xmode[6]);
dp[3] = len;
if (usalp->verbose) usal_prbytes("Mode Sense Data (converted)", dp, amt);
return (0);
}
int
mode_select_g0(SCSI *usalp, Uchar *dp, int cnt, int smp, int pf)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)dp;
scmd->size = cnt;
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = SC_MODE_SELECT;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
scmd->cdb.g0_cdb.high_addr = smp ? 1 : 0 | pf ? 0x10 : 0;
scmd->cdb.g0_cdb.count = cnt;
if (usalp->verbose) {
fprintf(stderr, "%s ", smp?"Save":"Set ");
usal_prbytes("Mode Parameters", dp, cnt);
}
usalp->cmdname = "mode select g0";
return (usal_cmd(usalp));
}
int
mode_select_g1(SCSI *usalp, Uchar *dp, int cnt, int smp, int pf)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)dp;
scmd->size = cnt;
scmd->flags = SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x55;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
scmd->cdb.g0_cdb.high_addr = smp ? 1 : 0 | pf ? 0x10 : 0;
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
if (usalp->verbose) {
printf("%s ", smp?"Save":"Set ");
usal_prbytes("Mode Parameters", dp, cnt);
}
usalp->cmdname = "mode select g1";
return (usal_cmd(usalp));
}
int
mode_sense_g0(SCSI *usalp, Uchar *dp, int cnt, int page, int pcf)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)dp;
scmd->size = 0xFF;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = SC_MODE_SENSE;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
#ifdef nonono
scmd->cdb.g0_cdb.high_addr = 1<<4; /* DBD Disable Block desc. */
#endif
scmd->cdb.g0_cdb.mid_addr = (page&0x3F) | ((pcf<<6)&0xC0);
scmd->cdb.g0_cdb.count = page ? 0xFF : 24;
scmd->cdb.g0_cdb.count = cnt;
usalp->cmdname = "mode sense g0";
if (usal_cmd(usalp) < 0)
return (-1);
if (usalp->verbose) usal_prbytes("Mode Sense Data", dp, cnt - usal_getresid(usalp));
return (0);
}
int
mode_sense_g1(SCSI *usalp, Uchar *dp, int cnt, int page, int pcf)
{
register struct usal_cmd *scmd = usalp->scmd;
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = (caddr_t)dp;
scmd->size = cnt;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = 0x5A;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
#ifdef nonono
scmd->cdb.g0_cdb.high_addr = 1<<4; /* DBD Disable Block desc. */
#endif
scmd->cdb.g1_cdb.addr[0] = (page&0x3F) | ((pcf<<6)&0xC0);
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "mode sense g1";
if (usal_cmd(usalp) < 0)
return (-1);
if (usalp->verbose) usal_prbytes("Mode Sense Data", dp, cnt - usal_getresid(usalp));
return (0);
}
struct trackdesc {
Uchar res0;
#if defined(_BIT_FIELDS_LTOH) /* Intel byteorder */
Ucbit control : 4;
Ucbit adr : 4;
#else /* Motorola byteorder */
Ucbit adr : 4;
Ucbit control : 4;
#endif
Uchar track;
Uchar res3;
Uchar addr[4];
};
struct diskinfo {
struct tocheader hd;
struct trackdesc desc[1];
};
struct siheader {
Uchar len[2];
Uchar finished;
Uchar unfinished;
};
struct sidesc {
Uchar sess_number;
Uchar res1;
Uchar track;
Uchar res3;
Uchar addr[4];
};
struct sinfo {
struct siheader hd;
struct sidesc desc[1];
};
struct trackheader {
Uchar mode;
Uchar res[3];
Uchar addr[4];
};
#define TRM_ZERO 0
#define TRM_USER_ECC 1 /* 2048 bytes user data + 288 Bytes ECC/EDC */
#define TRM_USER 2 /* All user data (2336 bytes) */
int
read_tochdr(SCSI *usalp, cdr_t *dp, int *fp, int *lp)
{
struct tocheader *tp;
char xb[256];
int len;
tp = (struct tocheader *)xb;
fillbytes((caddr_t)xb, sizeof (xb), '\0');
if (read_toc(usalp, xb, 0, sizeof (struct tocheader), 0, FMT_TOC) < 0) {
if (usalp->silent == 0)
errmsgno(EX_BAD, "Cannot read TOC header\n");
return (-1);
}
len = a_to_u_2_byte(tp->len) + sizeof (struct tocheader)-2;
if (len >= 4) {
if (fp)
*fp = tp->first;
if (lp)
*lp = tp->last;
return (0);
}
return (-1);
}
int
read_cdtext(SCSI *usalp)
{
struct tocheader *tp;
char xb[256];
int len;
char xxb[10000];
tp = (struct tocheader *)xb;
fillbytes((caddr_t)xb, sizeof (xb), '\0');
if (read_toc(usalp, xb, 0, sizeof (struct tocheader), 0, FMT_CDTEXT) < 0) {
if (usalp->silent == 0 || usalp->verbose > 0)
errmsgno(EX_BAD, "Cannot read CD-Text header\n");
return (-1);
}
len = a_to_u_2_byte(tp->len) + sizeof (struct tocheader)-2;
printf("CD-Text len: %d\n", len);
if (read_toc(usalp, xxb, 0, len, 0, FMT_CDTEXT) < 0) {
if (usalp->silent == 0)
errmsgno(EX_BAD, "Cannot read CD-Text\n");
return (-1);
}
{
FILE *f = fileopen("cdtext.dat", "wctb");
filewrite(f, xxb, len);
}
return (0);
}
int
read_trackinfo(SCSI *usalp, int track, long *offp, struct msf *msfp, int *adrp,
int *controlp, int *modep)
{
struct diskinfo *dp;
char xb[256];
int len;
dp = (struct diskinfo *)xb;
fillbytes((caddr_t)xb, sizeof (xb), '\0');
if (read_toc(usalp, xb, track, sizeof (struct diskinfo), 0, FMT_TOC) < 0) {
if (usalp->silent <= 0)
errmsgno(EX_BAD, "Cannot read TOC\n");
return (-1);
}
len = a_to_u_2_byte(dp->hd.len) + sizeof (struct tocheader)-2;
if (len < (int)sizeof (struct diskinfo))
return (-1);
if (offp)
*offp = a_to_4_byte(dp->desc[0].addr);
if (adrp)
*adrp = dp->desc[0].adr;
if (controlp)
*controlp = dp->desc[0].control;
if (msfp) {
usalp->silent++;
if (read_toc(usalp, xb, track, sizeof (struct diskinfo), 1, FMT_TOC)
>= 0) {
msfp->msf_min = dp->desc[0].addr[1];
msfp->msf_sec = dp->desc[0].addr[2];
msfp->msf_frame = dp->desc[0].addr[3];
} else if (read_toc(usalp, xb, track, sizeof (struct diskinfo), 0, FMT_TOC)
>= 0) {
/*
* Some drives (e.g. the Philips CDD-522) don't support
* to read the TOC in MSF mode.
*/
long off = a_to_4_byte(dp->desc[0].addr);
lba_to_msf(off, msfp);
} else {
msfp->msf_min = 0;
msfp->msf_sec = 0;
msfp->msf_frame = 0;
}
usalp->silent--;
}
if (modep == NULL)
return (0);
if (track == 0xAA) {
*modep = -1;
return (0);
}
fillbytes((caddr_t)xb, sizeof (xb), '\0');
usalp->silent++;
if (read_header(usalp, xb, *offp, 8, 0) >= 0) {
*modep = xb[0];
} else if (read_track_info_philips(usalp, xb, track, 14) >= 0) {
*modep = xb[0xb] & 0xF;
} else {
*modep = -1;
}
usalp->silent--;
return (0);
}
int
read_B0(SCSI *usalp, BOOL isbcd, long *b0p, long *lop)
{
struct fdiskinfo *dp;
struct ftrackdesc *tp;
char xb[8192];
char *pe;
int len;
long l;
dp = (struct fdiskinfo *)xb;
fillbytes((caddr_t)xb, sizeof (xb), '\0');
if (read_toc_philips(usalp, xb, 1, sizeof (struct tocheader), 0, FMT_FULLTOC) < 0) {
return (-1);
}
len = a_to_u_2_byte(dp->hd.len) + sizeof (struct tocheader)-2;
if (len < (int)sizeof (struct fdiskinfo))
return (-1);
if (read_toc_philips(usalp, xb, 1, len, 0, FMT_FULLTOC) < 0) {
return (-1);
}
if (usalp->verbose) {
usal_prbytes("TOC data: ", (Uchar *)xb,
len > (int)sizeof (xb) - usal_getresid(usalp) ?
sizeof (xb) - usal_getresid(usalp) : len);
tp = &dp->desc[0];
pe = &xb[len];
while ((char *)tp < pe) {
usal_prbytes("ENT: ", (Uchar *)tp, 11);
tp++;
}
}
tp = &dp->desc[0];
pe = &xb[len];
for (; (char *)tp < pe; tp++) {
if (tp->sess_number != dp->hd.last)
continue;
if (tp->point != 0xB0)
continue;
if (usalp->verbose)
usal_prbytes("B0: ", (Uchar *)tp, 11);
if (isbcd) {
l = msf_to_lba(from_bcd(tp->amin),
from_bcd(tp->asec),
from_bcd(tp->aframe), TRUE);
} else {
l = msf_to_lba(tp->amin,
tp->asec,
tp->aframe, TRUE);
}
if (b0p)
*b0p = l;
if (usalp->verbose)
printf("B0 start: %ld\n", l);
if (isbcd) {
l = msf_to_lba(from_bcd(tp->pmin),
from_bcd(tp->psec),
from_bcd(tp->pframe), TRUE);
} else {
l = msf_to_lba(tp->pmin,
tp->psec,
tp->pframe, TRUE);
}
if (usalp->verbose)
printf("B0 lout: %ld\n", l);
if (lop)
*lop = l;
return (0);
}
return (-1);
}
/*
* Return address of first track in last session (SCSI-3/mmc version).
*/
int
read_session_offset(SCSI *usalp, long *offp)
{
struct diskinfo *dp;
char xb[256];
int len;
dp = (struct diskinfo *)xb;
fillbytes((caddr_t)xb, sizeof (xb), '\0');
if (read_toc(usalp, (caddr_t)xb, 0, sizeof (struct tocheader), 0, FMT_SINFO) < 0)
return (-1);
if (usalp->verbose)
usal_prbytes("tocheader: ",
(Uchar *)xb, sizeof (struct tocheader) - usal_getresid(usalp));
len = a_to_u_2_byte(dp->hd.len) + sizeof (struct tocheader)-2;
if (len > (int)sizeof (xb)) {
errmsgno(EX_BAD, "Session info too big.\n");
return (-1);
}
if (read_toc(usalp, (caddr_t)xb, 0, len, 0, FMT_SINFO) < 0)
return (-1);
if (usalp->verbose)
usal_prbytes("tocheader: ",
(Uchar *)xb, len - usal_getresid(usalp));
dp = (struct diskinfo *)xb;
if (offp)
*offp = a_to_u_4_byte(dp->desc[0].addr);
return (0);
}
/*
* Return address of first track in last session (pre SCSI-3 version).
*/
int
read_session_offset_philips(SCSI *usalp, long *offp)
{
struct sinfo *sp;
char xb[256];
int len;
sp = (struct sinfo *)xb;
fillbytes((caddr_t)xb, sizeof (xb), '\0');
if (read_toc_philips(usalp, (caddr_t)xb, 0, sizeof (struct siheader), 0, FMT_SINFO) < 0)
return (-1);
len = a_to_u_2_byte(sp->hd.len) + sizeof (struct siheader)-2;
if (len > (int)sizeof (xb)) {
errmsgno(EX_BAD, "Session info too big.\n");
return (-1);
}
if (read_toc_philips(usalp, (caddr_t)xb, 0, len, 0, FMT_SINFO) < 0)
return (-1);
/*
* Old drives return the number of finished sessions in first/finished
* a descriptor is returned for each session.
* New drives return the number of the first and last session
* one descriptor for the last finished session is returned
* as in SCSI-3
* In all cases the lowest session number is set to 1.
*/
sp = (struct sinfo *)xb;
if (offp)
*offp = a_to_u_4_byte(sp->desc[sp->hd.finished-1].addr);
return (0);
}
int
sense_secsize(SCSI *usalp, int current)
{
Uchar mode[0x100];
Uchar *p;
Uchar *ep;
int len;
int secsize = -1;
usalp->silent++;
(void) unit_ready(usalp);
usalp->silent--;
/* XXX Quick and dirty, musz verallgemeinert werden !!! */
fillbytes(mode, sizeof (mode), '\0');
usalp->silent++;
len = sizeof (struct scsi_mode_header) +
sizeof (struct scsi_mode_blockdesc);
/*
* Wenn wir hier get_mode_params() nehmen bekommen wir die Warnung:
* Warning: controller returns wrong page 1 for All pages page (3F).
*/
if (mode_sense(usalp, mode, len, 0x3F, current?0:2) < 0) {
fillbytes(mode, sizeof (mode), '\0');
if (mode_sense(usalp, mode, len, 0, current?0:2) < 0) { /* VU (block desc) */
usalp->silent--;
return (-1);
}
}
if (mode[3] == 8) {
if (usalp->debug) {
printf("Density: 0x%X\n", mode[4]);
printf("Blocks: %ld\n", a_to_u_3_byte(&mode[5]));
printf("Blocklen:%ld\n", a_to_u_3_byte(&mode[9]));
}
secsize = a_to_u_3_byte(&mode[9]);
}
fillbytes(mode, sizeof (mode), '\0');
/*
* The ACARD TECH AEC-7720 ATAPI<->SCSI adaptor
* chokes if we try to transfer more than 0x40 bytes with
* mode_sense of all pages. So try to avoid to run this
* command if possible.
*/
if (usalp->debug &&
mode_sense(usalp, mode, 0xFE, 0x3F, current?0:2) >= 0) { /* All Pages */
ep = mode+mode[0]; /* Points to last byte of data */
p = &mode[4];
p += mode[3];
printf("Pages: ");
while (p < ep) {
printf("0x%X ", *p&0x3F);
p += p[1]+2;
}
printf("\n");
}
usalp->silent--;
return (secsize);
}
int
select_secsize(SCSI *usalp, int secsize)
{
struct scsi_mode_data md;
int count = sizeof (struct scsi_mode_header) +
sizeof (struct scsi_mode_blockdesc);
(void) test_unit_ready(usalp); /* clear any error situation */
fillbytes((caddr_t)&md, sizeof (md), '\0');
md.header.blockdesc_len = 8;
i_to_3_byte(md.blockdesc.lblen, secsize);
return (mode_select(usalp, (Uchar *)&md, count, 0, usalp->inq->data_format >= 2));
}
BOOL
is_cddrive(SCSI *usalp)
{
return (usalp->inq->type == INQ_ROMD || usalp->inq->type == INQ_WORM);
}
BOOL
is_unknown_dev(SCSI *usalp)
{
return (usalp->dev == DEV_UNKNOWN);
}
#ifndef DEBUG
#define DEBUG
#endif
#ifdef DEBUG
int
read_scsi(SCSI *usalp, caddr_t bp, long addr, int cnt)
{
if (addr <= G0_MAXADDR && cnt < 256 && !is_atapi)
return (read_g0(usalp, bp, addr, cnt));
else
return (read_g1(usalp, bp, addr, cnt));
}
int
read_g0(SCSI *usalp, caddr_t bp, long addr, int cnt)
{
register struct usal_cmd *scmd = usalp->scmd;
if (usalp->cap->c_bsize <= 0)
raisecond("capacity_not_set", 0L);
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt*usalp->cap->c_bsize;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G0_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g0_cdb.cmd = SC_READ;
scmd->cdb.g0_cdb.lun = usal_lun(usalp);
g0_cdbaddr(&scmd->cdb.g0_cdb, addr);
scmd->cdb.g0_cdb.count = cnt;
/* scmd->cdb.g0_cdb.vu_56 = 1;*/
usalp->cmdname = "read_g0";
return (usal_cmd(usalp));
}
int
read_g1(SCSI *usalp, caddr_t bp, long addr, int cnt)
{
register struct usal_cmd *scmd = usalp->scmd;
if (usalp->cap->c_bsize <= 0)
raisecond("capacity_not_set", 0L);
fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
scmd->addr = bp;
scmd->size = cnt*usalp->cap->c_bsize;
scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
scmd->cdb_len = SC_G1_CDBLEN;
scmd->sense_len = CCS_SENSE_LEN;
scmd->cdb.g1_cdb.cmd = SC_EREAD;
scmd->cdb.g1_cdb.lun = usal_lun(usalp);
g1_cdbaddr(&scmd->cdb.g1_cdb, addr);
g1_cdblen(&scmd->cdb.g1_cdb, cnt);
usalp->cmdname = "read_g1";
return (usal_cmd(usalp));
}
#endif /* DEBUG */
BOOL
getdev(SCSI *usalp, BOOL print)
{
BOOL got_inquiry = TRUE;
char vendor_info[8+1];
char prod_ident[16+1];
char prod_revision[4+1];
int inq_len = 0;
register struct usal_cmd *scmd = usalp->scmd;
register struct scsi_inquiry *inq = usalp->inq;
fillbytes((caddr_t)inq, sizeof (*inq), '\0');
usalp->dev = DEV_UNKNOWN;
usalp->silent++;
(void) unit_ready(usalp);
if (scmd->error >= SCG_FATAL &&
!(scmd->scb.chk && scmd->sense_count > 0)) {
usalp->silent--;
return (FALSE);
}
/* if (scmd->error < SCG_FATAL || scmd->scb.chk && scmd->sense_count > 0){*/
if (inquiry(usalp, (caddr_t)inq, sizeof (*inq)) < 0) {
got_inquiry = FALSE;
} else {
inq_len = sizeof (*inq) - usal_getresid(usalp);
}
if (!got_inquiry) {
if (usalp->verbose) {
printf(
"error: %d scb.chk: %d sense_count: %d sense.code: 0x%x\n",
scmd->error, scmd->scb.chk,
scmd->sense_count, scmd->sense.code);
}
/*
* Folgende Kontroller kennen das Kommando
* INQUIRY nicht:
*
* ADAPTEC ACB-4000, ACB-4010, ACB 4070
* SYSGEN SC4000
*
* Leider reagieren ACB40X0 und ACB5500 identisch
* wenn drive not ready (code == not ready),
* sie sind dann nicht zu unterscheiden.
*/
if (scmd->scb.chk && scmd->sense_count == 4) {
/* Test auf SYSGEN */
(void) qic02(usalp, 0x12); /* soft lock on */
if (qic02(usalp, 1) < 0) { /* soft lock off */
usalp->dev = DEV_ACB40X0;
/* usalp->dev = acbdev();*/
} else {
usalp->dev = DEV_SC4000;
inq->type = INQ_SEQD;
inq->removable = 1;
}
}
} else if (usalp->verbose) {
int i;
int len = inq->add_len + 5;
Uchar ibuf[256+5];
Uchar *ip = (Uchar *)inq;
Uchar c;
if (len > (int)sizeof (*inq) &&
inquiry(usalp, (caddr_t)ibuf, inq->add_len+5) >= 0) {
len = inq->add_len+5 - usal_getresid(usalp);
ip = ibuf;
} else {
len = sizeof (*inq);
}
printf("Inquiry Data : ");
for (i = 0; i < len; i++) {
c = ip[i];
if (c >= ' ' && c < 0177)
printf("%c", c);
else
printf(".");
}
printf("\n");
}
strncpy(vendor_info, inq->vendor_info, sizeof (inq->vendor_info));
strncpy(prod_ident, inq->prod_ident, sizeof (inq->prod_ident));
strncpy(prod_revision, inq->prod_revision, sizeof (inq->prod_revision));
vendor_info[sizeof (inq->vendor_info)] = '\0';
prod_ident[sizeof (inq->prod_ident)] = '\0';
prod_revision[sizeof (inq->prod_revision)] = '\0';
switch (inq->type) {
case INQ_DASD:
if (inq->add_len == 0 && inq->vendor_info[0] != '\0') {
Uchar *p;
/*
* NT-4.0 creates fake inquiry data for IDE disks.
* Unfortunately, it does not set add_len wo we
* check if vendor_info, prod_ident and prod_revision
* contains valid chars for a CCS inquiry.
*/
if (inq_len >= 36)
inq->add_len = 31;
for (p = (Uchar *)&inq->vendor_info[0];
p < (Uchar *)&inq->prod_revision[4];
p++) {
if (*p < 0x20 || *p > 0x7E) {
inq->add_len = 0;
break;
}
}
}
if (inq->add_len == 0) {
if (usalp->dev == DEV_UNKNOWN && got_inquiry) {
usalp->dev = DEV_ACB5500;
strcpy(inq->vendor_info,
"ADAPTEC ACB-5500 FAKE");
} else switch (usalp->dev) {
case DEV_ACB40X0:
strcpy(inq->vendor_info,
"ADAPTEC ACB-40X0 FAKE");
break;
case DEV_ACB4000:
strcpy(inq->vendor_info,
"ADAPTEC ACB-4000 FAKE");
break;
case DEV_ACB4010:
strcpy(inq->vendor_info,
"ADAPTEC ACB-4010 FAKE");
break;
case DEV_ACB4070:
strcpy(inq->vendor_info,
"ADAPTEC ACB-4070 FAKE");
break;
}
} else if (inq->add_len < 31) {
usalp->dev = DEV_NON_CCS_DSK;
} else if (strbeg("EMULEX", vendor_info)) {
if (strbeg("MD21", prod_ident))
usalp->dev = DEV_MD21;
if (strbeg("MD23", prod_ident))
usalp->dev = DEV_MD23;
else
usalp->dev = DEV_CCS_GENDISK;
} else if (strbeg("ADAPTEC", vendor_info)) {
if (strbeg("ACB-4520", prod_ident))
usalp->dev = DEV_ACB4520A;
if (strbeg("ACB-4525", prod_ident))
usalp->dev = DEV_ACB4525;
else
usalp->dev = DEV_CCS_GENDISK;
} else if (strbeg("SONY", vendor_info) &&
strbeg("SMO-C501", prod_ident)) {
usalp->dev = DEV_SONY_SMO;
} else {
usalp->dev = DEV_CCS_GENDISK;
}
break;
case INQ_SEQD:
if (usalp->dev == DEV_SC4000) {
strcpy(inq->vendor_info,
"SYSGEN SC4000 FAKE");
} else if (inq->add_len == 0 &&
inq->removable &&
inq->ansi_version == 1) {
usalp->dev = DEV_MT02;
strcpy(inq->vendor_info,
"EMULEX MT02 FAKE");
}
break;
/* case INQ_OPTD:*/
case INQ_ROMD:
case INQ_WORM:
if (strbeg("RXT-800S", prod_ident))
usalp->dev = DEV_RXT800S;
/*
* Start of CD-Recorders:
*/
if (strbeg("ACER", vendor_info)) {
if (strbeg("CR-4020C", prod_ident))
usalp->dev = DEV_RICOH_RO_1420C;
} else if (strbeg("CREATIVE", vendor_info)) {
if (strbeg("CDR2000", prod_ident))
usalp->dev = DEV_RICOH_RO_1060C;
} else if (strbeg("GRUNDIG", vendor_info)) {
if (strbeg("CDR100IPW", prod_ident))
usalp->dev = DEV_CDD_2000;
} else if (strbeg("JVC", vendor_info)) {
if (strbeg("XR-W2001", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
else if (strbeg("XR-W2010", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
else if (strbeg("R2626", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
} else if (strbeg("MITSBISH", vendor_info)) {
#ifdef XXXX_REALLY
/* It's MMC compliant */
if (strbeg("CDRW226", prod_ident))
usalp->dev = DEV_MMC_CDRW;
#else
/* EMPTY */
#endif
} else if (strbeg("MITSUMI", vendor_info)) {
/* Don't know any product string */
usalp->dev = DEV_CDD_522;
} else if (strbeg("OPTIMA", vendor_info)) {
if (strbeg("CD-R 650", prod_ident))
usalp->dev = DEV_SONY_CDU_924;
} else if (strbeg("PHILIPS", vendor_info) ||
strbeg("IMS", vendor_info) ||
strbeg("KODAK", vendor_info) ||
strbeg("HP", vendor_info)) {
if (strbeg("CDD521/00", prod_ident))
usalp->dev = DEV_CDD_521_OLD;
else if (strbeg("CDD521/02", prod_ident))
usalp->dev = DEV_CDD_521_OLD; /* PCD 200R? */
else if (strbeg("CDD521", prod_ident))
usalp->dev = DEV_CDD_521;
if (strbeg("CDD522", prod_ident))
usalp->dev = DEV_CDD_522;
if (strbeg("PCD225", prod_ident))
usalp->dev = DEV_CDD_522;
if (strbeg("KHSW/OB", prod_ident)) /* PCD600 */
usalp->dev = DEV_PCD_600;
if (strbeg("CDR-240", prod_ident))
usalp->dev = DEV_CDD_2000;
if (strbeg("CDD20", prod_ident))
usalp->dev = DEV_CDD_2000;
if (strbeg("CDD26", prod_ident))
usalp->dev = DEV_CDD_2600;
if (strbeg("C4324/C4325", prod_ident))
usalp->dev = DEV_CDD_2000;
if (strbeg("CD-Writer 6020", prod_ident))
usalp->dev = DEV_CDD_2600;
} else if (strbeg("PINNACLE", vendor_info)) {
if (strbeg("RCD-1000", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
if (strbeg("RCD5020", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
if (strbeg("RCD5040", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
if (strbeg("RCD 4X4", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
} else if (strbeg("PIONEER", vendor_info)) {
if (strbeg("CD-WO DW-S114X", prod_ident))
usalp->dev = DEV_PIONEER_DW_S114X;
else if (strbeg("CD-WO DR-R504X", prod_ident)) /* Reoprt from philip@merge.com */
usalp->dev = DEV_PIONEER_DW_S114X;
else if (strbeg("DVD-R DVR-S101", prod_ident))
usalp->dev = DEV_PIONEER_DVDR_S101;
} else if (strbeg("PLASMON", vendor_info)) {
if (strbeg("RF4100", prod_ident))
usalp->dev = DEV_PLASMON_RF_4100;
else if (strbeg("CDR4220", prod_ident))
usalp->dev = DEV_CDD_2000;
} else if (strbeg("PLEXTOR", vendor_info)) {
if (strbeg("CD-R PX-R24CS", prod_ident))
usalp->dev = DEV_RICOH_RO_1420C;
} else if (strbeg("RICOH", vendor_info)) {
if (strbeg("RO-1420C", prod_ident))
usalp->dev = DEV_RICOH_RO_1420C;
if (strbeg("RO1060C", prod_ident))
usalp->dev = DEV_RICOH_RO_1060C;
} else if (strbeg("SAF", vendor_info)) { /* Smart & Friendly */
if (strbeg("CD-R2004", prod_ident) ||
strbeg("CD-R2006 ", prod_ident))
usalp->dev = DEV_SONY_CDU_924;
else if (strbeg("CD-R2006PLUS", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
else if (strbeg("CD-RW226", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
else if (strbeg("CD-R4012", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
} else if (strbeg("SANYO", vendor_info)) {
if (strbeg("CD-WO CRD-R24S", prod_ident))
usalp->dev = DEV_CDD_521;
} else if (strbeg("SONY", vendor_info)) {
if (strbeg("CD-R CDU92", prod_ident) ||
strbeg("CD-R CDU94", prod_ident))
usalp->dev = DEV_SONY_CDU_924;
} else if (strbeg("TEAC", vendor_info)) {
if (strbeg("CD-R50S", prod_ident) ||
strbeg("CD-R55S", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
} else if (strbeg("TRAXDATA", vendor_info) ||
strbeg("Traxdata", vendor_info)) {
if (strbeg("CDR4120", prod_ident))
usalp->dev = DEV_TEAC_CD_R50S;
} else if (strbeg("T.YUDEN", vendor_info)) {
if (strbeg("CD-WO EW-50", prod_ident))
usalp->dev = DEV_TYUDEN_EW50;
} else if (strbeg("WPI", vendor_info)) { /* Wearnes */
if (strbeg("CDR-632P", prod_ident))
usalp->dev = DEV_CDD_2600;
} else if (strbeg("YAMAHA", vendor_info)) {
if (strbeg("CDR10", prod_ident))
usalp->dev = DEV_YAMAHA_CDR_100;
if (strbeg("CDR200", prod_ident))
usalp->dev = DEV_YAMAHA_CDR_400;
if (strbeg("CDR400", prod_ident))
usalp->dev = DEV_YAMAHA_CDR_400;
} else if (strbeg("MATSHITA", vendor_info)) {
if (strbeg("CD-R CW-7501", prod_ident))
usalp->dev = DEV_MATSUSHITA_7501;
if (strbeg("CD-R CW-7502", prod_ident))
usalp->dev = DEV_MATSUSHITA_7502;
}
if (usalp->dev == DEV_UNKNOWN) {
/*
* We do not have Manufacturer strings for
* the following drives.
*/
if (strbeg("CDS615E", prod_ident)) /* Olympus */
usalp->dev = DEV_SONY_CDU_924;
}
if (usalp->dev == DEV_UNKNOWN && inq->type == INQ_ROMD) {
BOOL cdrr = FALSE;
BOOL cdwr = FALSE;
BOOL cdrrw = FALSE;
BOOL cdwrw = FALSE;
BOOL dvd = FALSE;
BOOL dvdwr = FALSE;
usalp->dev = DEV_CDROM;
if (mmc_check(usalp, &cdrr, &cdwr, &cdrrw, &cdwrw,
&dvd, &dvdwr))
usalp->dev = DEV_MMC_CDROM;
if (cdwr)
usalp->dev = DEV_MMC_CDR;
if (cdwrw)
usalp->dev = DEV_MMC_CDRW;
if (dvd)
usalp->dev = DEV_MMC_DVD;
if (dvdwr)
usalp->dev = DEV_MMC_DVD_WR;
}
break;
case INQ_PROCD:
if (strbeg("BERTHOLD", vendor_info)) {
if (strbeg("", prod_ident))
usalp->dev = DEV_HRSCAN;
}
break;
case INQ_SCAN:
usalp->dev = DEV_MS300A;
break;
}
usalp->silent--;
if (!print)
return (TRUE);
if (usalp->dev == DEV_UNKNOWN && !got_inquiry) {
#ifdef PRINT_INQ_ERR
usal_printerr(usalp);
#endif
return (FALSE);
}
printinq(usalp, stdout);
return (TRUE);
}
void
printinq(SCSI *usalp, FILE *f)
{
register struct scsi_inquiry *inq = usalp->inq;
fprintf(f, "Device type : ");
usal_fprintdev(f, inq);
fprintf(f, "Version : %d\n", inq->ansi_version);
fprintf(f, "Response Format: %d\n", inq->data_format);
if (inq->data_format >= 2) {
fprintf(f, "Capabilities : ");
if (inq->aenc) fprintf(f, "AENC ");
if (inq->termiop) fprintf(f, "TERMIOP ");
if (inq->reladr) fprintf(f, "RELADR ");
if (inq->wbus32) fprintf(f, "WBUS32 ");
if (inq->wbus16) fprintf(f, "WBUS16 ");
if (inq->sync) fprintf(f, "SYNC ");
if (inq->linked) fprintf(f, "LINKED ");
if (inq->cmdque) fprintf(f, "CMDQUE ");
if (inq->softreset) fprintf(f, "SOFTRESET ");
fprintf(f, "\n");
}
if (inq->add_len >= 31 ||
inq->vendor_info[0] ||
inq->prod_ident[0] ||
inq->prod_revision[0]) {
fprintf(f, "Vendor_info : '%.8s'\n", inq->vendor_info);
fprintf(f, "Identification : '%.16s'\n", inq->prod_ident);
fprintf(f, "Revision : '%.4s'\n", inq->prod_revision);
}
}
void
printdev(SCSI *usalp)
{
printf("Device seems to be: ");
switch (usalp->dev) {
case DEV_UNKNOWN: printf("unknown"); break;
case DEV_ACB40X0: printf("Adaptec 4000/4010/4070"); break;
case DEV_ACB4000: printf("Adaptec 4000"); break;
case DEV_ACB4010: printf("Adaptec 4010"); break;
case DEV_ACB4070: printf("Adaptec 4070"); break;
case DEV_ACB5500: printf("Adaptec 5500"); break;
case DEV_ACB4520A: printf("Adaptec 4520A"); break;
case DEV_ACB4525: printf("Adaptec 4525"); break;
case DEV_MD21: printf("Emulex MD21"); break;
case DEV_MD23: printf("Emulex MD23"); break;
case DEV_NON_CCS_DSK: printf("Generic NON CCS Disk"); break;
case DEV_CCS_GENDISK: printf("Generic CCS Disk"); break;
case DEV_SONY_SMO: printf("Sony SMO-C501"); break;
case DEV_MT02: printf("Emulex MT02"); break;
case DEV_SC4000: printf("Sysgen SC4000"); break;
case DEV_RXT800S: printf("Maxtor RXT800S"); break;
case DEV_HRSCAN: printf("Berthold HR-Scanner"); break;
case DEV_MS300A: printf("Microtek MS300A"); break;
case DEV_CDROM: printf("Generic CD-ROM"); break;
case DEV_MMC_CDROM: printf("Generic mmc CD-ROM"); break;
case DEV_MMC_CDR: printf("Generic mmc CD-R"); break;
case DEV_MMC_CDRW: printf("Generic mmc CD-RW"); break;
case DEV_MMC_DVD: printf("Generic mmc2 DVD-ROM"); break;
case DEV_MMC_DVD_WR: printf("Generic mmc2 DVD-R/DVD-RW"); break;
case DEV_CDD_521_OLD: printf("Philips old CDD-521"); break;
case DEV_CDD_521: printf("Philips CDD-521"); break;
case DEV_CDD_522: printf("Philips CDD-522"); break;
case DEV_PCD_600: printf("Kodak PCD-600"); break;
case DEV_CDD_2000: printf("Philips CDD-2000"); break;
case DEV_CDD_2600: printf("Philips CDD-2600"); break;
case DEV_YAMAHA_CDR_100:printf("Yamaha CDR-100"); break;
case DEV_YAMAHA_CDR_400:printf("Yamaha CDR-400"); break;
case DEV_PLASMON_RF_4100:printf("Plasmon RF-4100"); break;
case DEV_SONY_CDU_924: printf("Sony CDU-924S"); break;
case DEV_RICOH_RO_1060C:printf("Ricoh RO-1060C"); break;
case DEV_RICOH_RO_1420C:printf("Ricoh RO-1420C"); break;
case DEV_TEAC_CD_R50S: printf("Teac CD-R50S"); break;
case DEV_MATSUSHITA_7501:printf("Matsushita CW-7501"); break;
case DEV_MATSUSHITA_7502:printf("Matsushita CW-7502"); break;
case DEV_PIONEER_DW_S114X: printf("Pioneer DW-S114X"); break;
case DEV_PIONEER_DVDR_S101:printf("Pioneer DVD-R S101"); break;
default: printf("Missing Entry for dev %d",
usalp->dev); break;
}
printf(".\n");
}
BOOL
do_inquiry(SCSI *usalp, int print)
{
if (getdev(usalp, print)) {
if (print)
printdev(usalp);
return (TRUE);
} else {
return (FALSE);
}
}
BOOL
recovery_needed(SCSI *usalp, cdr_t *dp)
{
int err;
register struct usal_cmd *scmd = usalp->scmd;
usalp->silent++;
err = test_unit_ready(usalp);
usalp->silent--;
if (err >= 0)
return (FALSE);
else if (scmd->error >= SCG_FATAL) /* nicht selektierbar */
return (FALSE);
if (scmd->sense.code < 0x70) /* non extended Sense */
return (FALSE);
/* XXX Old Philips code */
return (((struct scsi_ext_sense *)&scmd->sense)->sense_code == 0xD0);
}
int
scsi_load(SCSI *usalp, cdr_t *dp)
{
int key;
int code;
if ((dp->cdr_flags & CDR_CADDYLOAD) == 0) {
if (scsi_start_stop_unit(usalp, 1, 1, dp && (dp->cdr_cmdflags&F_IMMED)) >= 0)
return (0);
}
if (wait_unit_ready(usalp, 60))
return (0);
key = usal_sense_key(usalp);
code = usal_sense_code(usalp);
if (key == SC_NOT_READY && (code == 0x3A || code == 0x30)) {
errmsgno(EX_BAD, "Cannot load media with %s drive!\n",
(dp->cdr_flags & CDR_CADDYLOAD) ? "caddy" : "this");
errmsgno(EX_BAD, "Try to load media by hand.\n");
}
return (-1);
}
int
scsi_unload(SCSI *usalp, cdr_t *dp)
{
return (scsi_start_stop_unit(usalp, 0, 1, dp && (dp->cdr_cmdflags&F_IMMED)));
}
int
scsi_cdr_write(SCSI *usalp,
caddr_t bp /* address of buffer */,
long sectaddr /* disk address (sector) to put */,
long size /* number of bytes to transfer */,
int blocks /* sector count */,
BOOL islast /* last write for track */)
{
return (write_xg1(usalp, bp, sectaddr, size, blocks));
}
struct cd_mode_page_2A *
mmc_cap(SCSI *usalp, Uchar *modep)
{
int len;
int val;
Uchar mode[0x100];
struct cd_mode_page_2A *mp;
struct cd_mode_page_2A *mp2;
retry:
fillbytes((caddr_t)mode, sizeof (mode), '\0');
if (!get_mode_params(usalp, 0x2A, "CD capabilities",
mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len)) {
if (usal_sense_key(usalp) == SC_NOT_READY) {
if (wait_unit_ready(usalp, 60))
goto retry;
}
return (NULL); /* Pre SCSI-3/mmc drive */
}
if (len == 0) /* Pre SCSI-3/mmc drive */
return (NULL);
mp = (struct cd_mode_page_2A *)
(mode + sizeof (struct scsi_mode_header) +
((struct scsi_mode_header *)mode)->blockdesc_len);
/*
* Do some heuristics against pre SCSI-3/mmc VU page 2A
* We should test for a minimum p_len of 0x14, but some
* buggy CD-ROM readers ommit the write speed values.
*/
if (mp->p_len < 0x10)
return (NULL);
val = a_to_u_2_byte(mp->max_read_speed);
if (val != 0 && val < 176)
return (NULL);
val = a_to_u_2_byte(mp->cur_read_speed);
if (val != 0 && val < 176)
return (NULL);
len -= sizeof (struct scsi_mode_header) +
((struct scsi_mode_header *)mode)->blockdesc_len;
if (modep)
mp2 = (struct cd_mode_page_2A *)modep;
else
mp2 = malloc(len);
if (mp2)
movebytes(mp, mp2, len);
return (mp2);
}
void
mmc_getval(struct cd_mode_page_2A *mp,
BOOL *cdrrp /* CD ROM */,
BOOL *cdwrp /* CD-R writer */,
BOOL *cdrrwp /* CD-RW reader */,
BOOL *cdwrwp /* CD-RW writer */,
BOOL *dvdp /* DVD reader */,
BOOL *dvdwp /* DVD writer */)
{
BOOL isdvd; /* Any DVD reader */
BOOL isdvd_wr; /* DVD writer (R / RAM) */
BOOL iscd_wr; /* CD writer */
iscd_wr = (mp->cd_r_write != 0) || /* SCSI-3/mmc CD-R */
(mp->cd_rw_write != 0); /* SCSI-3/mmc CD-RW */
if (cdrrp)
*cdrrp = (mp->cd_r_read != 0); /* SCSI-3/mmc CD */
if (cdwrp)
*cdwrp = (mp->cd_r_write != 0); /* SCSI-3/mmc CD-R */
if (cdrrwp)
*cdrrwp = (mp->cd_rw_read != 0); /* SCSI-3/mmc CD */
if (cdwrwp)
*cdwrwp = (mp->cd_rw_write != 0); /* SCSI-3/mmc CD-RW */
isdvd = /* SCSI-3/mmc2 DVD */
(mp->dvd_ram_read + mp->dvd_r_read +
mp->dvd_rom_read) != 0;
isdvd_wr = /* SCSI-3/mmc2 DVD writer*/
(mp->dvd_ram_write + mp->dvd_r_write) != 0;
if (dvdp)
*dvdp = isdvd;
if (dvdwp)
*dvdwp = isdvd_wr;
}
BOOL
is_mmc(SCSI *usalp, BOOL *cdwp, BOOL *dvdwp)
{
BOOL cdwr = FALSE;
BOOL cdwrw = FALSE;
if (cdwp)
*cdwp = FALSE;
if (dvdwp)
*dvdwp = FALSE;
if (!mmc_check(usalp, NULL, &cdwr, NULL, &cdwrw, NULL, dvdwp))
return (FALSE);
if (cdwp)
*cdwp = cdwr | cdwrw;
return (TRUE);
}
BOOL
mmc_check(SCSI *usalp,
BOOL *cdrrp /* CD ROM */,
BOOL *cdwrp /* CD-R writer */,
BOOL *cdrrwp /* CD-RW reader */,
BOOL *cdwrwp /* CD-RW writer */,
BOOL *dvdp /* DVD reader */,
BOOL *dvdwp /* DVD writer */)
{
Uchar mode[0x100];
BOOL was_atapi;
struct cd_mode_page_2A *mp;
if (usalp->inq->type != INQ_ROMD)
return (FALSE);
fillbytes((caddr_t)mode, sizeof (mode), '\0');
was_atapi = allow_atapi(usalp, TRUE);
usalp->silent++;
mp = mmc_cap(usalp, mode);
usalp->silent--;
allow_atapi(usalp, was_atapi);
if (mp == NULL)
return (FALSE);
mmc_getval(mp, cdrrp, cdwrp, cdrrwp, cdwrwp, dvdp, dvdwp);
return (TRUE); /* Generic SCSI-3/mmc CD */
}
static void
print_speed(char *fmt, int val)
{
printf(" %s: %5d kB/s", fmt, val);
printf(" (CD %3ux,", val/176);
printf(" DVD %2ux)\n", val/1385);
}
#define DOES(what, flag) printf(" Does %s%s\n", flag?"":"not ", what)
#define IS(what, flag) printf(" Is %s%s\n", flag?"":"not ", what)
#define VAL(what, val) printf(" %s: %d\n", what, val[0]*256 + val[1])
#define SVAL(what, val) printf(" %s: %s\n", what, val)
void
print_capabilities(SCSI *usalp)
{
BOOL was_atapi;
Uchar mode[0x100];
struct cd_mode_page_2A *mp;
static const char *bclk[4] = {"32", "16", "24", "24 (I2S)"};
static const char *load[8] = {"caddy", "tray", "pop-up", "reserved(3)",
"disc changer", "cartridge changer",
"reserved(6)", "reserved(7)" };
static const char *rotctl[4] = {"CLV/PCAV", "CAV", "reserved(2)", "reserved(3)"};
if (usalp->inq->type != INQ_ROMD)
return;
fillbytes((caddr_t)mode, sizeof (mode), '\0');
was_atapi = allow_atapi(usalp, TRUE); /* Try to switch to 10 byte mode cmds */
usalp->silent++;
mp = mmc_cap(usalp, mode);
usalp->silent--;
allow_atapi(usalp, was_atapi);
if (mp == NULL)
return;
printf("\nDrive capabilities, per");
if (mp->p_len >= 28)
printf(" MMC-3");
else if (mp->p_len >= 24)
printf(" MMC-2");
else
printf(" MMC");
printf(" page 2A:\n\n");
DOES("read CD-R media", mp->cd_r_read);
DOES("write CD-R media", mp->cd_r_write);
DOES("read CD-RW media", mp->cd_rw_read);
DOES("write CD-RW media", mp->cd_rw_write);
DOES("read DVD-ROM media", mp->dvd_rom_read);
DOES("read DVD-R media", mp->dvd_r_read);
DOES("write DVD-R media", mp->dvd_r_write);
DOES("read DVD-RAM media", mp->dvd_ram_read);
DOES("write DVD-RAM media", mp->dvd_ram_write);
DOES("support test writing", mp->test_write);
printf("\n");
DOES("read Mode 2 Form 1 blocks", mp->mode_2_form_1);
DOES("read Mode 2 Form 2 blocks", mp->mode_2_form_2);
DOES("read digital audio blocks", mp->cd_da_supported);
if (mp->cd_da_supported)
DOES("restart non-streamed digital audio reads accurately", mp->cd_da_accurate);
DOES("support Buffer-Underrun-Free recording", mp->BUF);
DOES("read multi-session CDs", mp->multi_session);
DOES("read fixed-packet CD media using Method 2", mp->method2);
DOES("read CD bar code", mp->read_bar_code);
DOES("read R-W subcode information", mp->rw_supported);
if (mp->rw_supported)
DOES("return R-W subcode de-interleaved and error-corrected", mp->rw_deint_corr);
DOES("read raw P-W subcode data from lead in", mp->pw_in_lead_in);
DOES("return CD media catalog number", mp->UPC);
DOES("return CD ISRC information", mp->ISRC);
DOES("support C2 error pointers", mp->c2_pointers);
DOES("deliver composite A/V data", mp->composite);
printf("\n");
DOES("play audio CDs", mp->audio_play);
if (mp->audio_play) {
VAL("Number of volume control levels", mp->num_vol_levels);
DOES("support individual volume control setting for each channel", mp->sep_chan_vol);
DOES("support independent mute setting for each channel", mp->sep_chan_mute);
DOES("support digital output on port 1", mp->digital_port_1);
DOES("support digital output on port 2", mp->digital_port_2);
if (mp->digital_port_1 || mp->digital_port_2) {
DOES("send digital data LSB-first", mp->LSBF);
DOES("set LRCK high for left-channel data", mp->RCK);
DOES("have valid data on falling edge of clock", mp->BCK);
SVAL("Length of data in BCLKs", bclk[mp->length]);
}
}
printf("\n");
SVAL("Loading mechanism type", load[mp->loading_type]);
DOES("support ejection of CD via START/STOP command", mp->eject);
DOES("lock media on power up via prevent jumper", mp->prevent_jumper);
DOES("allow media to be locked in the drive via PREVENT/ALLOW command", mp->lock);
IS("currently in a media-locked state", mp->lock_state);
DOES("support changing side of disk", mp->side_change);
DOES("have load-empty-slot-in-changer feature", mp->sw_slot_sel);
DOES("support Individual Disk Present feature", mp->disk_present_rep);
printf("\n");
print_speed("Maximum read speed", a_to_u_2_byte(mp->max_read_speed));
print_speed("Current read speed", a_to_u_2_byte(mp->cur_read_speed));
print_speed("Maximum write speed", a_to_u_2_byte(mp->max_write_speed));
if (mp->p_len >= 28)
print_speed("Current write speed", a_to_u_2_byte(mp->v3_cur_write_speed));
else
print_speed("Current write speed", a_to_u_2_byte(mp->cur_write_speed));
if (mp->p_len >= 28) {
SVAL("Rotational control selected", rotctl[mp->rot_ctl_sel]);
}
VAL("Buffer size in KB", mp->buffer_size);
if (mp->p_len >= 24) {
VAL("Copy management revision supported", mp->copy_man_rev);
}
if (mp->p_len >= 28) {
struct cd_wr_speed_performance *pp;
Uint ndesc;
Uint i;
Uint n;
ndesc = a_to_u_2_byte(mp->num_wr_speed_des);
pp = mp->wr_speed_des;
printf(" Number of supported write speeds: %d\n", ndesc);
for (i = 0; i < ndesc; i++, pp++) {
printf(" Write speed # %d:", i);
n = a_to_u_2_byte(pp->wr_speed_supp);
printf(" %5d kB/s", n);
printf(" %s", rotctl[pp->rot_ctl_sel]);
printf(" (CD %3ux,", n/176);
printf(" DVD %2ux)\n", n/1385);
}
}
/* Generic SCSI-3/mmc CD */
}
|