summaryrefslogtreecommitdiff
path: root/wodim/drv_mmc.c
blob: b075889aef673fef73e84a0b1190f0ebaa410469 (plain)
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
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
/*
 * 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.
 *
 */

/* @(#)drv_mmc.c	1.163 06/01/12 Copyright 1997-2006 J. Schilling */
/*
 *	CDR device implementation for
 *	SCSI-3/mmc conforming drives
 *	e.g. Yamaha CDR-400, Ricoh MP6200
 *
 *	Copyright (c) 1997-2006 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.
 */

/*#define	DEBUG*/
#define	PRINT_ATIP
#include <mconfig.h>

#include <stdio.h>
#include <standard.h>
#include <fctldefs.h>
#include <errno.h>
#include <strdefs.h>
#include <stdxlib.h>
#include <unixstd.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 "mmcvendor.h"
#include "wodim.h"
#include "scsi_scan.h"

extern	char	*driveropts;

extern	int	debug;
extern	int	lverbose;
extern	int	xdebug;

static	int	curspeed = 1;

static	char	clv_to_speed[16] = {
/*		0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 */
		0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

static	char	hs_clv_to_speed[16] = {
/*		0  1  2  3  4  5  6  7   8  9 10 11 12 13 14 15 */
		0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

static	char	us_clv_to_speed[16] = {
/*		0  1  2  3  4  5   6  7  8   9   10  11 12 13 14 15 */
		0, 2, 4, 8, 0, 0, 16, 0, 24, 32, 40, 48, 0, 0, 0, 0
};

#ifdef	__needed__
static	int	mmc_load(SCSI *usalp, cdr_t *dp);
static	int	mmc_unload(SCSI *usalp, cdr_t *dp);
#endif
void				mmc_opthelp(cdr_t *dp, int excode);
char				*hasdrvopt(char *optstr, char *optname);
static	cdr_t	*identify_mmc(SCSI *usalp, cdr_t *, struct scsi_inquiry *);
static	int	attach_mmc(SCSI *usalp, cdr_t *);
static   int   attach_mdvd(SCSI *usalp, cdr_t *);
int				check_writemodes_mmc(SCSI *usalp, cdr_t *dp);
int     			check_writemodes_mdvd(SCSI *usalp, cdr_t *dp);
static	int	deflt_writemodes_mmc(SCSI *usalp, BOOL reset_dummy);
static   int   deflt_writemodes_mdvd(SCSI *usalp, BOOL reset_dummy);
static	int	get_diskinfo(SCSI *usalp, struct disk_info *dip);
static	void	di_to_dstat(struct disk_info *dip, dstat_t *dsp);
static	int	get_atip(SCSI *usalp, struct atipinfo *atp);
#ifdef	PRINT_ATIP
static	int	get_pma(SCSI *usalp);
#endif
static	int	init_mmc(SCSI *usalp, cdr_t *dp);
static	int	getdisktype_mmc(SCSI *usalp, cdr_t *dp);
static  int   getdisktype_mdvd(SCSI *usalp, cdr_t *dp);
static	int	speed_select_mmc(SCSI *usalp, cdr_t *dp, int *speedp);
static  int   speed_select_mdvd(SCSI *usalp, cdr_t *dp, int *speedp);
static	int	mmc_set_speed(SCSI *usalp, int readspeed, int writespeed, 
									  int rotctl);
static	int	next_wr_addr_mmc(SCSI *usalp, track_t *trackp, long *ap);
static  int   next_wr_addr_mdvd(SCSI *usalp, track_t *trackp, long *ap);
static	int	write_leadin_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp);
static	int	open_track_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp);
static  int   open_track_mdvd(SCSI *usalp, cdr_t *dp, track_t *trackp);
static	int	close_track_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp);
static  int   close_track_mdvd(SCSI *usalp, cdr_t *dp, track_t *trackp); 
static	int	open_session_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp);
static  int   open_session_mdvd(SCSI *usalp, cdr_t *dp, track_t *trackp);
static	int	waitfix_mmc(SCSI *usalp, int secs);
static	int	fixate_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp);
static  int   fixate_mdvd(SCSI *usalp, cdr_t *dp, track_t *trackp);
static	int	blank_mmc(SCSI *usalp, cdr_t *dp, long addr, int blanktype);
static	int	format_mdvd(SCSI *usalp, cdr_t *dp, int formattype);
static	int	send_opc_mmc(SCSI *usalp, caddr_t, int cnt, int doopc);
static	int	opt1_mmc(SCSI *usalp, cdr_t *dp);
static	int	opt1_mdvd(SCSI *usalp, cdr_t *dp);
static	int	opt2_mmc(SCSI *usalp, cdr_t *dp);
static	int	scsi_sony_write(SCSI *usalp, caddr_t bp, long sectaddr, long size, 
										 int blocks, BOOL islast);
static	int	gen_cue_mmc(track_t *trackp, void *vcuep, BOOL needgap);
static	void	fillcue(struct mmc_cue *cp, int ca, int tno, int idx, int dataform,
							 int scms, msf_t *mp);
static	int	send_cue_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp);
static 	int	stats_mmc(SCSI *usalp, cdr_t *dp);
static 	BOOL	mmc_isplextor(SCSI *usalp);
static 	BOOL	mmc_isyamaha(SCSI *usalp);
static 	void	do_varirec_plextor(SCSI *usalp);
static 	int	do_gigarec_plextor(SCSI *usalp);
static 	int	drivemode_plextor(SCSI *usalp, caddr_t bp, int cnt, int modecode, 
											void *modeval);
static 	int	drivemode2_plextor(SCSI *usalp, caddr_t bp, int cnt, int modecode, 
											 void *modeval);
static 	int	check_varirec_plextor(SCSI *usalp);
static 	int	check_gigarec_plextor(SCSI *usalp);
static 	int	varirec_plextor(SCSI *usalp, BOOL on, int val);
static 	int	gigarec_plextor(SCSI *usalp, int val);
static 	Int32_t	gigarec_mult(int code, Int32_t	val);
static 	int	check_ss_hide_plextor(SCSI *usalp);
static 	int	check_speed_rd_plextor(SCSI *usalp);
static 	int	check_powerrec_plextor(SCSI *usalp);
static 	int	ss_hide_plextor(SCSI *usalp, BOOL do_ss, BOOL do_hide);
static 	int	speed_rd_plextor(SCSI *usalp, BOOL do_speedrd);
static 	int	powerrec_plextor(SCSI *usalp, BOOL do_powerrec);
static 	int	get_speeds_plextor(SCSI *usalp, int *selp, int *maxp, int *lastp);
static 	int	bpc_plextor(SCSI *usalp, int mode, int *bpp);
static 	int	set_audiomaster_yamaha(SCSI *usalp, cdr_t *dp, BOOL keep_mode);

struct 	ricoh_mode_page_30 * get_justlink_ricoh(SCSI *usalp, Uchar *mode);
static 	int	force_speed_yamaha(SCSI *usalp, int readspeed, int writespeed);
static 	BOOL	get_tattoo_yamaha(SCSI *usalp, BOOL print, Int32_t *irp, 
										Int32_t *orp);
static 	int	do_tattoo_yamaha(SCSI *usalp, FILE *f);
static 	int	yamaha_write_buffer(SCSI *usalp, int mode, int bufferid, long offset,
										  long parlen, void *buffer, long buflen);
static	int	dvd_dual_layer_split(SCSI *usalp, cdr_t *dp, long tsize);

extern int 	reserve_track(SCSI *usalp, Ulong size); /* FIXME */
extern int 	scsi_format(SCSI *usalp, caddr_t addr, int size, BOOL background); /* FIXME */

#ifdef	__needed__
static int 
mmc_load(SCSI *usalp, cdr_t *dp)
{
	return (scsi_load_unload(usalp, 1));
}

static int 
mmc_unload(SCSI *usalp, cdr_t *dp)
{
	return (scsi_load_unload(usalp, 0));
}
#endif

/*
 * MMC CD-writer
 */
cdr_t	cdr_mmc = {
	0, 0,
	CDR_SWABAUDIO,
	CDR_CDRW_ALL,
	372, 372,
	"mmc_cdr",
	"generic SCSI-3/mmc   CD-R/CD-RW driver",
	0,
	(dstat_t *)0,
	identify_mmc,
	attach_mmc,
	init_mmc,
	getdisktype_mmc,
	scsi_load,
	scsi_unload,
	read_buff_cap,
	cmd_dummy,					/* check_recovery */
	(int(*)(SCSI *, cdr_t *, int))cmd_dummy,	/* recover	*/
	speed_select_mmc,
	select_secsize,
	next_wr_addr_mmc,
	(int(*)(SCSI *, Ulong))cmd_ill,	/* reserve_track	*/
	scsi_cdr_write,
	gen_cue_mmc,
	send_cue_mmc,
	write_leadin_mmc,
	open_track_mmc,
	close_track_mmc,
	open_session_mmc,
	cmd_dummy,
	cmd_dummy,					/* abort	*/
	read_session_offset,
	fixate_mmc,
	stats_mmc,
	blank_mmc,
	format_dummy,
	send_opc_mmc,
	opt1_mmc,
	opt2_mmc,
};

cdr_t   cdr_mdvd = {
	0, 0,
	CDR_SWABAUDIO,
	CDR_CDRW_ALL,
	370,370,
	"mmc_mdvd",
	"generic SCSI-3/mmc DVD-R(W) driver",
	0,
	(dstat_t *)0,
	identify_mmc,
	attach_mdvd,
	init_mmc,
	getdisktype_mdvd,
	scsi_load,
	scsi_unload,
	read_buff_cap,
	cmd_dummy,                                       /* check_recovery */
	(int(*)__PR((SCSI *, cdr_t *, int)))cmd_dummy,   /* recover     */
	speed_select_mdvd,
	select_secsize,
	next_wr_addr_mdvd,
	(int(*)(SCSI *, Ulong))cmd_ill,   /* reserve_track        */
	scsi_cdr_write,
	(int(*)__PR((track_t *, void *, BOOL)))cmd_dummy, /* gen_cue */
	(int(*)__PR((SCSI *usalp, cdr_t *, track_t *)))cmd_dummy, /* send_cue */
	write_leadin_mmc,
	open_track_mdvd,
	close_track_mdvd,
	open_session_mdvd,
	cmd_dummy,
	cmd_dummy,					/* abort	*/
	read_session_offset,
	fixate_mdvd,
	stats_mmc,
	blank_mmc,
	format_mdvd,
	send_opc_mmc,
	opt1_mdvd,
	opt2_mmc,
	dvd_dual_layer_split,
};

/*
 * Sony MMC CD-writer
 */
cdr_t	cdr_mmc_sony = {
	0, 0,
	CDR_SWABAUDIO,
	CDR_CDRW_ALL,
	372, 372,
	"mmc_cdr_sony",
	"generic SCSI-3/mmc   CD-R/CD-RW driver (Sony 928 variant)",
	0,
	(dstat_t *)0,
	identify_mmc,
	attach_mmc,
	init_mmc,
	getdisktype_mmc,
	scsi_load,
	scsi_unload,
	read_buff_cap,
	cmd_dummy,					/* check_recovery */
	(int(*)(SCSI *, cdr_t *, int))cmd_dummy,	/* recover	*/
	speed_select_mmc,
	select_secsize,
	next_wr_addr_mmc,
	(int(*)(SCSI *, Ulong))cmd_ill,	/* reserve_track	*/
	scsi_sony_write,
	gen_cue_mmc,
	send_cue_mmc,
	write_leadin_mmc,
	open_track_mmc,
	close_track_mmc,
	open_session_mmc,
	cmd_dummy,
	cmd_dummy,					/* abort	*/
	read_session_offset,
	fixate_mmc,
	cmd_dummy,				/* stats		*/
	blank_mmc,
	format_dummy,
	send_opc_mmc,
	opt1_mmc,
	opt2_mmc,
};

/*
 * SCSI-3/mmc conformant CD-ROM drive
 */
cdr_t	cdr_cd = {
	0, 0,
	CDR_ISREADER|CDR_SWABAUDIO,
	CDR_CDRW_NONE,
	372, 372,
	"mmc_cd",
	"generic SCSI-3/mmc   CD-ROM driver",
	0,
	(dstat_t *)0,
	identify_mmc,
	attach_mmc,
	cmd_dummy,
	drive_getdisktype,
	scsi_load,
	scsi_unload,
	read_buff_cap,
	cmd_dummy,					/* check_recovery */
	(int(*)(SCSI *, cdr_t *, int))cmd_dummy,	/* recover	*/
	speed_select_mmc,
	select_secsize,
	(int(*)(SCSI *usalp, track_t *, long *))cmd_ill,	/* next_wr_addr		*/
	(int(*)(SCSI *, Ulong))cmd_ill,	/* reserve_track	*/
	scsi_cdr_write,
	(int(*)(track_t *, void *, BOOL))cmd_dummy,	/* gen_cue */
	no_sendcue,
	(int(*)(SCSI *, cdr_t *, track_t *))cmd_dummy, /* leadin */
	open_track_mmc,
	close_track_mmc,
	(int(*)(SCSI *usalp, cdr_t *, track_t *))cmd_dummy,
	cmd_dummy,
	cmd_dummy,					/* abort	*/
	read_session_offset,
	(int(*)(SCSI *usalp, cdr_t *, track_t *))cmd_dummy,	/* fixation */
	cmd_dummy,					/* stats	*/
	blank_dummy,
	format_dummy,
	(int(*)(SCSI *, caddr_t, int, int))NULL,	/* no OPC	*/
	cmd_dummy,					/* opt1		*/
	cmd_dummy,					/* opt2		*/
};

/*
 * Old pre SCSI-3/mmc CD drive
 */
cdr_t	cdr_oldcd = {
	0, 0,
	CDR_ISREADER,
	CDR_CDRW_NONE,
	372, 372,
	"scsi2_cd",
	"generic SCSI-2       CD-ROM driver",
	0,
	(dstat_t *)0,
	identify_mmc,
	drive_attach,
	cmd_dummy,
	drive_getdisktype,
	scsi_load,
	scsi_unload,
	buf_dummy,
	cmd_dummy,					/* check_recovery */
	(int(*)(SCSI *, cdr_t *, int))cmd_dummy,	/* recover	*/
	speed_select_mmc,
	select_secsize,
	(int(*)(SCSI *usal, track_t *, long *))cmd_ill,	/* next_wr_addr		*/
	(int(*)(SCSI *, Ulong))cmd_ill,	/* reserve_track	*/
	scsi_cdr_write,
	(int(*)(track_t *, void *, BOOL))cmd_dummy,	/* gen_cue */
	no_sendcue,
	(int(*)(SCSI *, cdr_t *, track_t *))cmd_dummy, /* leadin */
	open_track_mmc,
	close_track_mmc,
	(int(*)(SCSI *usalp, cdr_t *, track_t *))cmd_dummy,
	cmd_dummy,
	cmd_dummy,					/* abort	*/
	read_session_offset_philips,
	(int(*)(SCSI *usalp, cdr_t *, track_t *))cmd_dummy,	/* fixation */
	cmd_dummy,					/* stats	*/
	blank_dummy,
	format_dummy,
	(int(*)(SCSI *, caddr_t, int, int))NULL,	/* no OPC	*/
	cmd_dummy,					/* opt1		*/
	cmd_dummy,					/* opt2		*/
};

/*
 * SCSI-3/mmc conformant CD or DVD writer
 * Checks the current medium and then returns either cdr_mmc or cdr_dvd
 */
cdr_t	cdr_cd_dvd = {
	0, 0,
	CDR_SWABAUDIO,
	CDR_CDRW_ALL,
	372, 372,
	"mmc_cd_dvd",
	"generic SCSI-3/mmc   CD/DVD driver (checks media)",
	0,
	(dstat_t *)0,
	identify_mmc,
	attach_mmc,
	cmd_dummy,
	drive_getdisktype,
	scsi_load,
	scsi_unload,
	read_buff_cap,
	cmd_dummy,					/* check_recovery */
	(int(*)(SCSI *, cdr_t *, int))cmd_dummy,	/* recover	*/
	speed_select_mmc,
	select_secsize,
	(int(*)(SCSI *usalp, track_t *, long *))cmd_ill,	/* next_wr_addr		*/
	(int(*)(SCSI *, Ulong))cmd_ill,	/* reserve_track	*/
	scsi_cdr_write,
	(int(*)(track_t *, void *, BOOL))cmd_dummy,	/* gen_cue */
	no_sendcue,
	(int(*)(SCSI *, cdr_t *, track_t *))cmd_dummy, /* leadin */
	open_track_mmc,
	close_track_mmc,
	(int(*)(SCSI *usalp, cdr_t *, track_t *))cmd_dummy,
	cmd_dummy,
	cmd_dummy,					/* abort	*/
	read_session_offset,
	(int(*)(SCSI *usalp, cdr_t *, track_t *))cmd_dummy,	/* fixation */
	cmd_dummy,					/* stats	*/
	blank_dummy,
	format_dummy,
	(int(*)(SCSI *, caddr_t, int, int))NULL,	/* no OPC	*/
	cmd_dummy,					/* opt1		*/
	cmd_dummy,					/* opt2		*/
};

void 
mmc_opthelp(cdr_t *dp, int excode)
{
	BOOL	haveopts = FALSE;

	fprintf(stderr, "Driver options:\n");
	if (dp->cdr_flags & CDR_BURNFREE) {
		fprintf(stderr, "burnfree	Prepare writer to use BURN-Free technology\n");
		fprintf(stderr, "noburnfree	Disable using BURN-Free technology\n");
		haveopts = TRUE;
	}
	if (dp->cdr_flags & CDR_VARIREC) {
		fprintf(stderr, "varirec=val	Set VariRec Laserpower to -2, -1, 0, 1, 2\n");
		fprintf(stderr, "		Only works for audio and if speed is set to 4\n");
		haveopts = TRUE;
	}
	if (dp->cdr_flags & CDR_GIGAREC) {
		fprintf(stderr, "gigarec=val	Set GigaRec capacity ratio to 0.6, 0.7, 0.8, 1.0, 1.2, 1.3, 1.4\n");
		haveopts = TRUE;
	}
	if (dp->cdr_flags & CDR_AUDIOMASTER) {
		fprintf(stderr, "audiomaster	Turn Audio Master feature on (SAO CD-R Audio/Data only)\n");
		haveopts = TRUE;
	}
	if (dp->cdr_flags & CDR_FORCESPEED) {
		fprintf(stderr, "forcespeed	Tell the drive to force speed even for low quality media\n");
		haveopts = TRUE;
	}
	if (dp->cdr_flags & CDR_SPEEDREAD) {
		fprintf(stderr, "speedread	Tell the drive to read as fast as possible\n");
		fprintf(stderr, "nospeedread	Disable to read as fast as possible\n");
		haveopts = TRUE;
	}
	if (dp->cdr_flags & CDR_DISKTATTOO) {
		fprintf(stderr, "tattooinfo	Print image size info for DiskT@2 feature\n");
		fprintf(stderr, "tattoofile=name	Use 'name' as DiskT@2 image file\n");
		haveopts = TRUE;
	}
	if (dp->cdr_flags & CDR_SINGLESESS) {
		fprintf(stderr, "singlesession	Tell the drive to behave as single session only drive\n");
		fprintf(stderr, "nosinglesession	Disable single session only mode\n");
		haveopts = TRUE;
	}
	if (dp->cdr_flags & CDR_HIDE_CDR) {
		fprintf(stderr, "hidecdr		Tell the drive to hide CD-R media\n");
		fprintf(stderr, "nohidecdr	Disable hiding CD-R media\n");
		haveopts = TRUE;
	}
	if (!haveopts) {
		fprintf(stderr, "None supported for this drive.\n");
	}
	exit(excode);
}

char *
hasdrvopt(char *optstr, char *optname)
{
	char	*ep;
	char	*np;
	char	*ret = NULL;
	int	optnamelen;
	int	optlen;
	BOOL	not = FALSE;

	if (optstr == NULL)
		return (ret);

	optnamelen = strlen(optname);

	while (*optstr) {
		not = FALSE;			/* Reset before every token */
		if ((ep = strchr(optstr, ',')) != NULL) {
			optlen = ep - optstr;
			np = &ep[1];
		} else {
			optlen = strlen(optstr);
			np = &optstr[optlen];
		}
		if ((ep = strchr(optstr, '=')) != NULL) {
			if (ep < np)
				optlen = ep - optstr;
		}
		if (optstr[0] == '!') {
			optstr++;
			optlen--;
			not = TRUE;
		}
		if (strncmp(optstr, "no", 2) == 0) {
			optstr += 2;
			optlen -= 2;
			not = TRUE;
		}
		if (strncmp(optstr, optname, optlen) == 0) {
			ret = &optstr[optlen];
			break;
		}
		optstr = np;
	}
	if (ret != NULL) {
		if (*ret == ',' || *ret == '\0') {
			if (not)
				return ("0");
			return ("1");
		}
		if (*ret == '=') {
			if (not)
				return (NULL);
			return (++ret);
		}
	}
	return (ret);
}

static cdr_t *
identify_mmc(SCSI *usalp, cdr_t *dp, struct scsi_inquiry *ip)
{
	BOOL	cdrr	 = FALSE;	/* Read CD-R	*/
	BOOL	cdwr	 = FALSE;	/* Write CD-R	*/
	BOOL	cdrrw	 = FALSE;	/* Read CD-RW	*/
	BOOL	cdwrw	 = FALSE;	/* Write CD-RW	*/
	BOOL	dvdwr	 = FALSE;	/* DVD writer	*/
	BOOL	is_dvd	 = FALSE;	/* use DVD driver*/
	Uchar	mode[0x100];
	struct	cd_mode_page_2A *mp;
	int	profile;

	if (ip->type != INQ_WORM && ip->type != INQ_ROMD)
		return ((cdr_t *)0);

	allow_atapi(usalp, TRUE); /* Try to switch to 10 byte mode cmds */

	usalp->silent++;
	mp = mmc_cap(usalp, mode);	/* Get MMC capabilities */
	usalp->silent--;
	if (mp == NULL)
		return (&cdr_oldcd);	/* Pre SCSI-3/mmc drive		*/

	/*
	 * At this point we know that we have a SCSI-3/mmc compliant drive.
	 * Unfortunately ATAPI drives violate the SCSI spec in returning
	 * a response data format of '1' which from the SCSI spec would
	 * tell us not to use the "PF" bit in mode select. As ATAPI drives
	 * require the "PF" bit to be set, we 'correct' the inquiry data.
	 *
	 * XXX xxx_identify() should not have any side_effects ??
	 */
	if (ip->data_format < 2)
		ip->data_format = 2;

	/*
	 * First handle exceptions....
	 */
	if (strncmp(ip->vendor_info, "SONY", 4) == 0 &&
	    strncmp(ip->prod_ident, "CD-R   CDU928E", 14) == 0) {
		return (&cdr_mmc_sony);
	}

	/*
	 * Now try to do it the MMC-3 way....
	 */
	profile = get_curprofile(usalp);
	if (xdebug)
		printf("Current profile: 0x%04X\n", profile);
	if (profile == 0) {
		if (xdebug)
			print_profiles(usalp);
		/*
		 * If the current profile is 0x0000, then the
		 * drive does not know about the media. First
		 * close the tray and then try to issue the
		 * get_curprofile() command again.
		 */
		usalp->silent++;
		load_media(usalp, dp, FALSE);
		usalp->silent--;
		profile = get_curprofile(usalp);
		scsi_prevent_removal(usalp, 0);
		if (xdebug)
			printf("Current profile: 0x%04X\n", profile);
	}
	if (profile >= 0) {
		if (lverbose)
			print_profiles(usalp);
		if (profile == 0 || (profile >= 0x10 && profile <= 0x15) || profile > 0x19) {
		    /*
		     * 10h DVD-ROM
		     * 11h DVD-R
		     * 12h DVD-RAM
		     * 13h DVD-RW (Restricted overwrite)
		     * 14h DVD-RW (Sequential recording)
		     * 1Ah DVD+RW
		     * 1Bh DVD+R
		     * 2Bh DVD+R DL
		     * 
		     */
		    if (profile == 0x11 || profile == 0x13 || profile == 0x14 || profile == 0x1A || profile == 0x1B || profile == 0x2B) {
			is_dvd = TRUE;
			dp = &cdr_mdvd;
		    } else {
			is_dvd = FALSE;
			dp = &cdr_cd;

			if (profile == 0) {		/* No Medium */
				BOOL	is_cdr = FALSE;

				/*
				 * Check for CD-writer
				 */
				get_wproflist(usalp, &is_cdr, NULL,
							NULL, NULL);
				if (is_cdr)
					return (&cdr_mmc);
				/*
				 * Other MMC-3 drive without media
				 */
				return (dp);
			} if (profile == 0x12) {	/* DVD-RAM */
				errmsgno(EX_BAD,
				"Found unsupported DVD-RAM media.\n");
				return (dp);
			}
		    }
		}
	} else {
		if (xdebug)
			printf("Drive is pre MMC-3\n");
	}

	mmc_getval(mp, &cdrr, &cdwr, &cdrrw, &cdwrw, NULL, &dvdwr);

	if (!cdwr && !cdwrw) {	/* SCSI-3/mmc CD drive		*/
		/*
		 * If the drive does not support to write CD's, we select the
		 * CD-ROM driver here. If we have DVD-R/DVD-RW support compiled
		 * in, we may later decide to switch to the DVD driver.
		 */
		dp = &cdr_cd;
	} else {
		/*
		 * We need to set the driver to cdr_mmc because we may come
		 * here with driver set to cdr_cd_dvd which is not a driver
		 * that may be used for actual CD/DVD writing.
		 */
		dp = &cdr_mmc;
	}

/*#define	DVD_DEBUG*/
#ifdef	DVD_DEBUG
	if (1) {	/* Always check for DVD media in debug mode */
#else
	if ((cdwr || cdwrw) && dvdwr) {
#endif
		char	xb[32];

#ifndef	DVD_DEBUG
		usalp->silent++;
#else
		fprintf(stderr, "identify_dvd: checking for DVD media\n");
#endif
		if (read_dvd_structure(usalp, (caddr_t)xb, 32, 0, 0, 0) >= 0) {
			/*
			 * If read DVD structure is supported and works, then
			 * we must have a DVD media in the drive. Signal to
			 * use the DVD driver.
			 */
			is_dvd = TRUE;
		} else {
			if (usal_sense_key(usalp) == SC_NOT_READY) {
				/*
				 * If the SCSI sense key is NOT READY, then the
				 * drive does not know about the media. First
				 * close the tray and then try to issue the
				 * read_dvd_structure() command again.
				 */
				load_media(usalp, dp, FALSE);
				if (read_dvd_structure(usalp, (caddr_t)xb, 32, 0, 0, 0) >= 0) {
					is_dvd = TRUE;
				}
				scsi_prevent_removal(usalp, 0);
			}
		}
#ifndef	DVD_DEBUG
		usalp->silent--;
#else
		fprintf(stderr, "identify_dvd: is_dvd: %d\n", is_dvd);
#endif
	}
	if (is_dvd) {
     if(lverbose>2) 
        fprintf(stderr, "Found DVD media: using cdr_mdvd.\n");  
     dp = &cdr_mdvd; 
	}
	dp->profile = profile;
	dp->is_dvd = is_dvd;
	return (dp);
}

static int 
attach_mmc(SCSI *usalp, cdr_t *dp)
{
	int	ret;
	Uchar	mode[0x100];
	struct	cd_mode_page_2A *mp;
	struct	ricoh_mode_page_30 *rp = NULL;

	allow_atapi(usalp, TRUE); /* Try to switch to 10 byte mode cmds */

	usalp->silent++;
	mp = mmc_cap(usalp, NULL); /* Get MMC capabilities in allocated mp */
	usalp->silent--;
	if (mp == NULL)
		return (-1);	/* Pre SCSI-3/mmc drive		*/

	dp->cdr_cdcap = mp;	/* Store MMC cap pointer	*/

	dp->cdr_dstat->ds_dr_max_rspeed = a_to_u_2_byte(mp->max_read_speed)/176;
	if (dp->cdr_dstat->ds_dr_max_rspeed == 0)
		dp->cdr_dstat->ds_dr_max_rspeed = 372;
	dp->cdr_dstat->ds_dr_cur_rspeed = a_to_u_2_byte(mp->cur_read_speed)/176;
	if (dp->cdr_dstat->ds_dr_cur_rspeed == 0)
		dp->cdr_dstat->ds_dr_cur_rspeed = 372;

	dp->cdr_dstat->ds_dr_max_wspeed = a_to_u_2_byte(mp->max_write_speed)/176;
	if (mp->p_len >= 28)
		dp->cdr_dstat->ds_dr_cur_wspeed = a_to_u_2_byte(mp->v3_cur_write_speed)/176;
	else
		dp->cdr_dstat->ds_dr_cur_wspeed = a_to_u_2_byte(mp->cur_write_speed)/176;

	if (dp->cdr_speedmax > dp->cdr_dstat->ds_dr_max_wspeed)
		dp->cdr_speedmax = dp->cdr_dstat->ds_dr_max_wspeed;

	if (dp->cdr_speeddef > dp->cdr_speedmax)
		dp->cdr_speeddef = dp->cdr_speedmax;

	rp = get_justlink_ricoh(usalp, mode);

	if (mp->p_len >= 28)
		dp->cdr_flags |= CDR_MMC3;
	if (mp->p_len >= 24)
		dp->cdr_flags |= CDR_MMC2;
	dp->cdr_flags |= CDR_MMC;

	if (mp->loading_type == LT_TRAY)
		dp->cdr_flags |= CDR_TRAYLOAD;
	else if (mp->loading_type == LT_CADDY)
		dp->cdr_flags |= CDR_CADDYLOAD;

	if (mp->BUF != 0) {
		dp->cdr_flags |= CDR_BURNFREE;
	} else if (rp) {
		if ((dp->cdr_cmdflags & F_DUMMY) && rp->TWBFS && rp->BUEFS)
			dp->cdr_flags |= CDR_BURNFREE;

		if (rp->BUEFS)
			dp->cdr_flags |= CDR_BURNFREE;
	}

	if (mmc_isplextor(usalp)) {
		if (check_varirec_plextor(usalp) >= 0)
			dp->cdr_flags |= CDR_VARIREC;

		if (check_gigarec_plextor(usalp) >= 0)
			dp->cdr_flags |= CDR_GIGAREC;

		if (check_ss_hide_plextor(usalp) >= 0)
			dp->cdr_flags |= CDR_SINGLESESS|CDR_HIDE_CDR;

		if (check_powerrec_plextor(usalp) >= 0)
			dp->cdr_flags |= CDR_FORCESPEED;

		if (check_speed_rd_plextor(usalp) >= 0)
			dp->cdr_flags |= CDR_SPEEDREAD;
	}
	if (mmc_isyamaha(usalp)) {
		if (set_audiomaster_yamaha(usalp, dp, FALSE) >= 0)
			dp->cdr_flags |= CDR_AUDIOMASTER;

		/*
		 * Starting with CRW 2200 / CRW 3200
		 */
		if ((mp->p_len+2) >= (unsigned)28)
			dp->cdr_flags |= CDR_FORCESPEED;

		if (get_tattoo_yamaha(usalp, FALSE, 0, 0))
			dp->cdr_flags |= CDR_DISKTATTOO;
	}

	if (rp && rp->AWSCS)
		dp->cdr_flags |= CDR_FORCESPEED;

#ifdef	FUTURE_ROTCTL
	if (mp->p_len >= 28) {
		int	val;

		val = dp->cdr_dstat->ds_dr_cur_wspeed;
		if (val == 0)
			val = 372;

		usalp->verbose++;
		if (scsi_set_speed(usalp, -1, val, ROTCTL_CAV) < 0) {
			fprintf(stderr, "XXX\n");
		}
		usalp->verbose--;
	}
#endif

	check_writemodes_mmc(usalp, dp);

	/* Enable Burnfree by default, it can be disabled later */
	if ((dp->cdr_flags & CDR_BURNFREE) != 0)
		dp->cdr_dstat->ds_cdrflags |= RF_BURNFREE;

	if (driveropts != NULL) {
		char	*p;

		if (strcmp(driveropts, "help") == 0) {
			mmc_opthelp(dp, 0);
		}

		p = hasdrvopt(driveropts, "varirec");
		if (p != NULL && (dp->cdr_flags & CDR_VARIREC) != 0) {
			dp->cdr_dstat->ds_cdrflags |= RF_VARIREC;
		}

		p = hasdrvopt(driveropts, "gigarec");
		if (p != NULL && (dp->cdr_flags & CDR_GIGAREC) != 0) {
			dp->cdr_dstat->ds_cdrflags |= RF_GIGAREC;
		}

		p = hasdrvopt(driveropts, "audiomaster");
		if (p != NULL && *p == '1' && (dp->cdr_flags & CDR_AUDIOMASTER) != 0) {
			dp->cdr_dstat->ds_cdrflags |= RF_AUDIOMASTER;
			dp->cdr_dstat->ds_cdrflags &= ~RF_BURNFREE;
		}
		p = hasdrvopt(driveropts, "forcespeed");
		if (p != NULL && *p == '1' && (dp->cdr_flags & CDR_FORCESPEED) != 0) {
			dp->cdr_dstat->ds_cdrflags |= RF_FORCESPEED;
		}
		p = hasdrvopt(driveropts, "tattooinfo");
		if (p != NULL && *p == '1' && (dp->cdr_flags & CDR_DISKTATTOO) != 0) {
			get_tattoo_yamaha(usalp, TRUE, 0, 0);
		}
		p = hasdrvopt(driveropts, "tattoofile");
		if (p != NULL && (dp->cdr_flags & CDR_DISKTATTOO) != 0) {
			FILE	*f;

			if ((f = fileopen(p, "rb")) == NULL)
				comerr("Cannot open '%s'.\n", p);

			if (do_tattoo_yamaha(usalp, f) < 0)
				errmsgno(EX_BAD, "Cannot do DiskT@2.\n");
			fclose(f);
		}
		p = hasdrvopt(driveropts, "singlesession");
		if (p != NULL && (dp->cdr_flags & CDR_SINGLESESS) != 0) {
			if (*p == '1') {
				dp->cdr_dstat->ds_cdrflags |= RF_SINGLESESS;
			} else if (*p == '0') {
				dp->cdr_dstat->ds_cdrflags &= ~RF_SINGLESESS;
			}
		}
		p = hasdrvopt(driveropts, "hidecdr");
		if (p != NULL && (dp->cdr_flags & CDR_HIDE_CDR) != 0) {
			if (*p == '1') {
				dp->cdr_dstat->ds_cdrflags |= RF_HIDE_CDR;
			} else if (*p == '0') {
				dp->cdr_dstat->ds_cdrflags &= ~RF_HIDE_CDR;
			}
		}
		p = hasdrvopt(driveropts, "speedread");
		if (p != NULL && (dp->cdr_flags & CDR_SPEEDREAD) != 0) {
			if (*p == '1') {
				dp->cdr_dstat->ds_cdrflags |= RF_SPEEDREAD;
			} else if (*p == '0') {
				dp->cdr_dstat->ds_cdrflags &= ~RF_SPEEDREAD;
			}
		}
	}

	if ((ret = get_supported_cdrw_media_types(usalp)) < 0) {
		dp->cdr_cdrw_support = CDR_CDRW_ALL;
		return (0);
	}
	dp->cdr_cdrw_support = ret;
	if (lverbose > 1)
		printf("Supported CD-RW media types: %02X\n", dp->cdr_cdrw_support);

	return (0);
}

static int 
attach_mdvd(SCSI *usalp, cdr_t *dp)
{
	struct  cd_mode_page_2A *mp;


	allow_atapi(usalp, TRUE);/* Try to switch to 10 byte mode cmds */

	usalp->silent++;
	mp = mmc_cap(usalp, NULL);/* Get MMC capabilities in allocated mp */
	usalp->silent--;
	if (mp == NULL)
		return (-1);    /* Pre SCSI-3/mmc drive         */

	dp->cdr_cdcap = mp;     /* Store MMC cap pointer        */

	dp->cdr_dstat->ds_dr_max_rspeed = a_to_u_2_byte(mp->max_read_speed)/1385;
	if (dp->cdr_dstat->ds_dr_max_rspeed == 0)
		dp->cdr_dstat->ds_dr_max_rspeed = 1385;
	dp->cdr_dstat->ds_dr_cur_rspeed = a_to_u_2_byte(mp->cur_read_speed)/1385;
	if (dp->cdr_dstat->ds_dr_cur_rspeed == 0)
		dp->cdr_dstat->ds_dr_cur_rspeed = 1385;

	dp->cdr_dstat->ds_dr_max_wspeed = a_to_u_2_byte(mp->max_write_speed)/1385;
	if (mp->p_len >= 28)
		dp->cdr_dstat->ds_dr_cur_wspeed = a_to_u_2_byte(mp->v3_cur_write_speed)/1385;
	else
		dp->cdr_dstat->ds_dr_cur_wspeed = a_to_u_2_byte(mp->cur_write_speed)/1385;

	if (dp->cdr_speedmax > dp->cdr_dstat->ds_dr_max_wspeed)
		dp->cdr_speedmax = dp->cdr_dstat->ds_dr_max_wspeed;

	if (dp->cdr_speeddef > dp->cdr_speedmax)
		dp->cdr_speeddef = dp->cdr_speedmax;


        if (mp->loading_type == LT_TRAY)
                dp->cdr_flags |= CDR_TRAYLOAD;
        else if (mp->loading_type == LT_CADDY)
                dp->cdr_flags |= CDR_CADDYLOAD;

        if (mp->BUF != 0)
                dp->cdr_flags |= CDR_BURNFREE;

        check_writemodes_mdvd(usalp, dp);

        if (driveropts != NULL) {
                if (strcmp(driveropts, "help") == 0) {
                        mmc_opthelp(dp, 0);
                }
        }

        return (0);
}

int 
check_writemodes_mmc(SCSI *usalp, cdr_t *dp)
{
	Uchar	mode[0x100];
	int	len;
	struct	cd_mode_page_05 *mp;

	if (xdebug)
		printf("Checking possible write modes: ");

	/*
	 * Reset mp->test_write (-dummy) here.
	 */
	deflt_writemodes_mmc(usalp, TRUE);

	fillbytes((caddr_t)mode, sizeof (mode), '\0');

	usalp->silent++;
	if (!get_mode_params(usalp, 0x05, "CD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len)) {
		usalp->silent--;
		return (-1);
	}
	if (len == 0) {
		usalp->silent--;
		return (-1);
	}

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof (struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);
#ifdef	DEBUG
	usal_prbytes("CD write parameter:", (Uchar *)mode, len);
#endif

	/*
	 * mp->test_write has already been reset in deflt_writemodes_mmc()
	 * Do not reset mp->test_write (-dummy) here. It should be set
	 * only at one place and only one time.
	 */

	mp->write_type = WT_TAO;
	mp->track_mode = TM_DATA;
	mp->dbtype = DB_ROM_MODE1;

	if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
		dp->cdr_flags |= CDR_TAO;
		if (xdebug)
			printf("TAO ");
	} else
		dp->cdr_flags &= ~CDR_TAO;

	mp->write_type = WT_PACKET;
	mp->track_mode |= TM_INCREMENTAL;
/*	mp->fp = (trackp->pktsize > 0) ? 1 : 0;*/
/*	i_to_4_byte(mp->packet_size, trackp->pktsize);*/
	mp->fp = 0;
	i_to_4_byte(mp->packet_size, 0);

	if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
		dp->cdr_flags |= CDR_PACKET;
		if (xdebug)
			printf("PACKET ");
	} else
		dp->cdr_flags &= ~CDR_PACKET;
	mp->fp = 0;
	i_to_4_byte(mp->packet_size, 0);
	mp->track_mode = TM_DATA;
	mp->write_type = WT_SAO;

	if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
		dp->cdr_flags |= CDR_SAO;
		if (xdebug)
			printf("SAO ");
	} else
		dp->cdr_flags &= ~CDR_SAO;

	if (dp->cdr_flags & CDR_SAO) {
		mp->dbtype = DB_RAW_PQ;

#ifdef	__needed__
		if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
			dp->cdr_flags |= CDR_SRAW16;
			if (xdebug)
				printf("SAO/R16 ");
		}
#endif

		mp->dbtype = DB_RAW_PW;

		if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
			dp->cdr_flags |= CDR_SRAW96P;
			if (xdebug)
				printf("SAO/R96P ");
		}

		mp->dbtype = DB_RAW_PW_R;

		if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
			dp->cdr_flags |= CDR_SRAW96R;
			if (xdebug)
				printf("SAO/R96R ");
		}
	}

	mp->write_type = WT_RAW;
	mp->dbtype = DB_RAW_PQ;

	if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
		dp->cdr_flags |= CDR_RAW;
		dp->cdr_flags |= CDR_RAW16;
		if (xdebug)
			printf("RAW/R16 ");
	}

	mp->dbtype = DB_RAW_PW;

	if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
		dp->cdr_flags |= CDR_RAW;
		dp->cdr_flags |= CDR_RAW96P;
		if (xdebug)
			printf("RAW/R96P ");
	}

	mp->dbtype = DB_RAW_PW_R;

	if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
		dp->cdr_flags |= CDR_RAW;
		dp->cdr_flags |= CDR_RAW96R;
		if (xdebug)
			printf("RAW/R96R ");
	}

	if (xdebug)
		printf("\n");

	/*
	 * Reset mp->test_write (-dummy) here.
	 */
	deflt_writemodes_mmc(usalp, TRUE);
	usalp->silent--;

	return (0);
}

int 
check_writemodes_mdvd(SCSI *usalp, cdr_t *dp)
{
	Uchar	mode[0x100];
	int	len;
	struct	cd_mode_page_05 *mp;

	if (xdebug)
		printf("Checking possible write modes: ");

	deflt_writemodes_mdvd(usalp, FALSE);

	fillbytes((caddr_t)mode, sizeof(mode), '\0');

	usalp->silent++;
	if (!get_mode_params(usalp, 0x05, "DVD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len)) {
		usalp->silent--;
		return (-1);
	}
	if (len == 0) {
		usalp->silent--;
		return (-1);
	}

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof(struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);

	mp->test_write = 0;

	/*We only check for PACKET and SAO since these are the only supported modes for DVD */
	/*XXX these checks are irrelevant because they are not medium sensitive. ie the device returns 
	  error only when it does not support a given mode for ALL mediums. It should check using 
	  GET CONFIGURATION command.*/

	mp->write_type = WT_PACKET;
	mp->fp = 0;
	i_to_4_byte(mp->packet_size, 0);

	if (set_mode_params(usalp, "DVD write parameter", mode, len, 0, -1)) {
		dp->cdr_flags |= CDR_PACKET;
		if (xdebug)
		  printf("PACKET ");
	} else
	  dp->cdr_flags &= ~CDR_PACKET;
	mp->fp = 0;
	i_to_4_byte(mp->packet_size, 0);
	mp->track_mode = TM_DATA; 


	mp->write_type = WT_SAO;

	if (set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
		dp->cdr_flags |= CDR_SAO;
		if (xdebug)
			printf("SAO ");
	} else
		dp->cdr_flags &= ~CDR_SAO;


	if (xdebug)
		printf("\n");

	deflt_writemodes_mdvd(usalp, TRUE);
	usalp->silent--;
	return (0);
}

static int 
deflt_writemodes_mmc(SCSI *usalp, BOOL reset_dummy)
{
	Uchar	mode[0x100];
	int	len;
	struct	cd_mode_page_05 *mp;

	fillbytes((caddr_t)mode, sizeof (mode), '\0');

	usalp->silent++;
	if (!get_mode_params(usalp, 0x05, "CD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len)) {
		usalp->silent--;
		return (-1);
	}
	if (len == 0) {
		usalp->silent--;
		return (-1);
	}

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof (struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);
#ifdef	DEBUG
	usal_prbytes("CD write parameter:", (Uchar *)mode, len);
	fprintf(stderr, "Audio pause len: %d\n", a_to_2_byte(mp->audio_pause_len));
#endif

	/*
	 * This is the only place where we reset mp->test_write (-dummy)
	 */
	if (reset_dummy)
		mp->test_write = 0;

	/*
	 * Set default values:
	 * Write type = 01 (track at once)
	 * Track mode = 04 (CD-ROM)
	 * Data block type = 08 (CD-ROM)
	 * Session format = 00 (CD-ROM)
	 *
	 * XXX Note:	the same code appears in check_writemodes_mmc() and
	 * XXX		in speed_select_mmc().
	 */
	mp->write_type = WT_TAO;
	mp->track_mode = TM_DATA;
	mp->dbtype = DB_ROM_MODE1;
	mp->session_format = SES_DA_ROM; /* Matsushita has illegal def. value */

	i_to_2_byte(mp->audio_pause_len, 150);	/* LG has illegal def. value */

#ifdef	DEBUG
	usal_prbytes("CD write parameter:", (Uchar *)mode, len);
#endif
	if (!set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {

		mp->write_type	= WT_SAO;
		mp->LS_V	= 0;
		mp->copy	= 0;
		mp->fp		= 0;
		mp->multi_session  = MS_NONE;
		mp->host_appl_code = 0;

		if (!set_mode_params(usalp, "CD write parameter", mode, len, 0, -1)) {
			usalp->silent--;
			return (-1);
		}
	}
	usalp->silent--;
	return (0);
}

static int 
deflt_writemodes_mdvd(SCSI *usalp, BOOL reset_dummy)
{
	Uchar	mode[0x100];
	int	len;
	struct	cd_mode_page_05 *mp;

	fillbytes((caddr_t)mode, sizeof(mode), '\0');

	usalp->silent++;
	if (!get_mode_params(usalp, 0x05, "DVD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len)) {
		usalp->silent--;
		return (-1);
	}
	if (len == 0) {
		usalp->silent--;
		return (-1);
	}

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof(struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);

	mp->test_write = 0;
	/*
	 * This is the only place where we reset mp->test_write (-dummy) for DVD
	 */
	if (reset_dummy)
		mp->test_write = 0;

	/*
	 * Set default values:
	 * Write type = 02 (session at once)
	 *
	 * XXX Note:	the same code appears in check_writemodes_mmc() and
	 * XXX		in speed_select_mmc().
	 */
	mp->write_type = WT_SAO;
	mp->track_mode = TM_DATA; 
	mp->dbtype = DB_ROM_MODE1;
	mp->session_format = SES_DA_ROM;


	if (set_mode_params(usalp, "DVD write parameter", mode, len, 0, -1) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;
	return (0);
}

#ifdef	PRINT_ATIP
static	void	print_di(struct disk_info *dip);
static	void	atip_printspeed(char *fmt, int speedindex, char speedtab[]);
static	void	print_atip(SCSI *usalp, struct atipinfo *atp);
#endif	/* PRINT_ATIP */

static int 
get_diskinfo(SCSI *usalp, struct disk_info *dip)
{
	int	len;
	int	ret;

	fillbytes((caddr_t)dip, sizeof (*dip), '\0');

	/*
	 * Used to be 2 instead of 4 (now). But some Y2k ATAPI drives as used
	 * by IOMEGA create a DMA overrun if we try to transfer only 2 bytes.
	 */
/*	if (read_disk_info(usalp, (caddr_t)dip, 2) < 0)*/
	if (read_disk_info(usalp, (caddr_t)dip, 4) < 0)
		return (-1);
	len = a_to_u_2_byte(dip->data_len);
	len += 2;
	ret = read_disk_info(usalp, (caddr_t)dip, len);

#ifdef	DEBUG
	usal_prbytes("Disk info:", (Uchar *)dip,
				len-usal_getresid(usalp));
#endif
	return (ret);
}

static void 
di_to_dstat(struct disk_info *dip, dstat_t *dsp)
{
	dsp->ds_diskid = a_to_u_4_byte(dip->disk_id);
	if (dip->did_v)
		dsp->ds_flags |= DSF_DID_V;
	dsp->ds_disktype = dip->disk_type;
	dsp->ds_diskstat = dip->disk_status;
	dsp->ds_sessstat = dip->sess_status;
	if (dip->erasable)
		dsp->ds_flags |= DSF_ERA;

	dsp->ds_trfirst	   = dip->first_track;
	dsp->ds_trlast	   = dip->last_track_ls;
	dsp->ds_trfirst_ls = dip->first_track_ls;

	dsp->ds_maxblocks = msf_to_lba(dip->last_lead_out[1],
					dip->last_lead_out[2],
					dip->last_lead_out[3], TRUE);
	/*
	 * Check for 0xFF:0xFF/0xFF which is an indicator for a complete disk
	 */
	if (dsp->ds_maxblocks == 1166730)
		dsp->ds_maxblocks = -1L;

	dsp->ds_first_leadin = msf_to_lba(dip->last_lead_in[1],
					dip->last_lead_in[2],
					dip->last_lead_in[3], FALSE);
	if (dsp->ds_first_leadin > 0)
		dsp->ds_first_leadin = 0;

	if (dsp->ds_last_leadout == 0 && dsp->ds_maxblocks >= 0)
		dsp->ds_last_leadout = dsp->ds_maxblocks;
	dsp->ds_trfirst=dip->first_track; 
	dsp->ds_trlast=dip->last_track_ls;
	dsp->ds_trfirst_ls=dip->first_track_ls;
}

static int 
get_atip(SCSI *usalp, struct atipinfo *atp)
{
	int	len;
	int	ret;

	fillbytes((caddr_t)atp, sizeof (*atp), '\0');

	/*
	 * Used to be 2 instead of sizeof (struct tocheader), but all
	 * other places in the code use sizeof (struct tocheader) too and
	 * some Y2k ATAPI drives as used by IOMEGA create a DMA overrun if we
	 * try to transfer only 2 bytes.
	 */
	if (read_toc(usalp, (caddr_t)atp, 0, sizeof (struct tocheader), 0, FMT_ATIP) < 0)
		return (-1);
	len = a_to_u_2_byte(atp->hd.len);
	len += 2;
	ret = read_toc(usalp, (caddr_t)atp, 0, len, 0, FMT_ATIP);

#ifdef	DEBUG
	usal_prbytes("ATIP info:", (Uchar *)atp,
				len-usal_getresid(usalp));
#endif
	/*
	 * Yamaha sometimes returns zeroed ATIP info for disks without ATIP
	 */
	if (atp->desc.lead_in[1] == 0 &&
			atp->desc.lead_in[2] == 0 &&
			atp->desc.lead_in[3] == 0 &&
			atp->desc.lead_out[1] == 0 &&
			atp->desc.lead_out[2] == 0 &&
			atp->desc.lead_out[3] == 0)
		return (-1);

	if (atp->desc.lead_in[1] >= 0x90 && debug) {
		/*
		 * Only makes sense with buggy Ricoh firmware.
		 */
		errmsgno(EX_BAD, "Converting ATIP from BCD\n");
		atp->desc.lead_in[1] = from_bcd(atp->desc.lead_in[1]);
		atp->desc.lead_in[2] = from_bcd(atp->desc.lead_in[2]);
		atp->desc.lead_in[3] = from_bcd(atp->desc.lead_in[3]);

		atp->desc.lead_out[1] = from_bcd(atp->desc.lead_out[1]);
		atp->desc.lead_out[2] = from_bcd(atp->desc.lead_out[2]);
		atp->desc.lead_out[3] = from_bcd(atp->desc.lead_out[3]);
	}

	return (ret);
}

#ifdef	PRINT_ATIP

static int 
get_pma(SCSI *usalp)
{
	int	len;
	int	ret;
	char	atp[1024];

	fillbytes((caddr_t)atp, sizeof (*atp), '\0');

	/*
	 * Used to be 2 instead of sizeof (struct tocheader), but all
	 * other places in the code use sizeof (struct tocheader) too and
	 * some Y2k ATAPI drives as used by IOMEGA create a DMA overrun if we
	 * try to transfer only 2 bytes.
	 */
/*	if (read_toc(usalp, (caddr_t)atp, 0, 2, 1, FMT_PMA) < 0)*/
/*	if (read_toc(usalp, (caddr_t)atp, 0, 2, 0, FMT_PMA) < 0)*/
	if (read_toc(usalp, (caddr_t)atp, 0, sizeof (struct tocheader), 0, FMT_PMA) < 0)
		return (-1);
/*	len = a_to_u_2_byte(atp->hd.len);*/
	len = a_to_u_2_byte(atp);
	len += 2;
/*	ret = read_toc(usalp, (caddr_t)atp, 0, len, 1, FMT_PMA);*/
	ret = read_toc(usalp, (caddr_t)atp, 0, len, 0, FMT_PMA);

#ifdef	DEBUG
	usal_prbytes("PMA:", (Uchar *)atp,
				len-usal_getresid(usalp));
#endif
	ret = read_toc(usalp, (caddr_t)atp, 0, len, 1, FMT_PMA);

#ifdef	DEBUG
	usal_prbytes("PMA:", (Uchar *)atp,
				len-usal_getresid(usalp));
#endif
	return (ret);
}

#endif	/* PRINT_ATIP */

static int 
init_mmc(SCSI *usalp, cdr_t *dp)
{
	return (speed_select_mmc(usalp, dp, NULL));
}

static int 
getdisktype_mdvd(SCSI *usalp, cdr_t *dp)
{
	int ret = 0;
	dstat_t	*dsp = dp->cdr_dstat;

	struct track_info track_info;
    if(lverbose)
        printf("HINT: use dvd+rw-mediainfo from dvd+rw-tools for information extraction.\n");
	/* if(getdisktype_mmc(usalp, dp)<0)
		return -1;
		*/

	/* read rzone info to get the space left on disk */
	/*ds_trlast is the last rzone on disk, can be invisible */
	if(read_rzone_info(usalp, (caddr_t)&track_info, sizeof(track_info))>=0)
		dsp->ds_maxblocks=a_to_u_4_byte(track_info.free_blocks)+a_to_4_byte(track_info.next_writable_addr);

	dsp->ds_disktype&= ~DT_CD;
	dsp->ds_disktype|= DT_DVD;

	return (ret);

}

static int 
getdisktype_mmc(SCSI *usalp, cdr_t *dp)
{
extern	char	*buf;
	dstat_t	*dsp = dp->cdr_dstat;
	struct disk_info *dip;
	Uchar	mode[0x100];
	msf_t	msf;
	BOOL	did_atip = FALSE;
	BOOL	did_dummy = FALSE;
	int 	rplus;

	msf.msf_min = msf.msf_sec = msf.msf_frame = 0;

	/*
	 * It seems that there are drives that do not support to
	 * read ATIP (e.g. HP 7100)
	 * Also if a NON CD-R media is inserted, this will never work.
	 * For this reason, make a failure non-fatal.
	 */
	usalp->silent++;
	if (get_atip(usalp, (struct atipinfo *)mode) >= 0) {
		struct atipinfo *atp = (struct atipinfo *)mode;

		msf.msf_min =		mode[8];
		msf.msf_sec =		mode[9];
		msf.msf_frame =		mode[10];
		if (atp->desc.erasable) {
			dsp->ds_flags |= DSF_ERA;
			if (atp->desc.sub_type == 1)
				dsp->ds_flags |= DSF_HIGHSP_ERA;
			else if (atp->desc.sub_type == 2)
				dsp->ds_flags |= DSF_ULTRASP_ERA;
			else if (atp->desc.sub_type == 3)
				dsp->ds_flags |= DSF_ULTRASP_ERA | DSF_ULTRASPP_ERA;
		}
		if (atp->desc.a1_v) {
			if (atp->desc.clv_low != 0)
				dsp->ds_at_min_speed = clv_to_speed[atp->desc.clv_low];
			if (atp->desc.clv_high != 0)
				dsp->ds_at_max_speed = clv_to_speed[atp->desc.clv_high];

			if (atp->desc.erasable && atp->desc.sub_type == 1) {
				if (atp->desc.clv_high != 0)
					dsp->ds_at_max_speed = hs_clv_to_speed[atp->desc.clv_high];
			}
		}
		if (atp->desc.a2_v && atp->desc.erasable && (atp->desc.sub_type == 2 || atp->desc.sub_type == 3)) {
			Uint	vlow;
			Uint	vhigh;

			vlow = (atp->desc.a2[0] >> 4) & 0x07;
			vhigh = atp->desc.a2[0] & 0x0F;
			if (vlow != 0)
				dsp->ds_at_min_speed = us_clv_to_speed[vlow];
			if (vhigh != 0)
				dsp->ds_at_max_speed = us_clv_to_speed[vhigh];
		}
		did_atip = TRUE;
	}
	usalp->silent--;

#ifdef	PRINT_ATIP
	if ((dp->cdr_dstat->ds_cdrflags & RF_PRATIP) != 0 && did_atip) {
		print_atip(usalp, (struct atipinfo *)mode);
		pr_manufacturer(&msf,
			((struct atipinfo *)mode)->desc.erasable,
			((struct atipinfo *)mode)->desc.uru);
	}
#endif
again:
	dip = (struct disk_info *)buf;
	if (get_diskinfo(usalp, dip) < 0)
		return (-1);

	/*
	 * Check for non writable disk first.
	 */
	
	/* DVD+RW does not need to be blanked */
	rplus = dsp->ds_cdrflags;
	if (dp->profile == 0x1A) rplus = RF_BLANK;
	
	if (dip->disk_status == DS_COMPLETE &&
			(rplus & dsp->ds_cdrflags & (RF_WRITE|RF_BLANK)) == RF_WRITE) {
		if (!did_dummy) {
			int	xspeed = 0xFFFF;
			int	oflags = dp->cdr_cmdflags;

			/*
			 * Try to clear the dummy bit to reset the virtual
			 * drive status. Not all drives support it even though
			 * it is mentioned in the MMC standard.
			 */
			if (lverbose)
				printf("Trying to clear drive status.\n");

			dp->cdr_cmdflags &= ~F_DUMMY;
			speed_select_mmc(usalp, dp, &xspeed);
			dp->cdr_cmdflags = oflags;
			did_dummy = TRUE;
			goto again;
		}
		/*
		 * Trying to clear drive status did not work...
		 */
		reload_media(usalp, dp);
	}
	if (get_diskinfo(usalp, dip) < 0)
		return (-1);
	di_to_dstat(dip, dsp);
	if (!did_atip && dsp->ds_first_leadin < 0)
		lba_to_msf(dsp->ds_first_leadin, &msf);

	if ((dp->cdr_dstat->ds_cdrflags & RF_PRATIP) != 0 && !did_atip) {
		print_min_atip(dsp->ds_first_leadin, dsp->ds_last_leadout);
		if (dsp->ds_first_leadin < 0)
				pr_manufacturer(&msf,
				dip->erasable,
				dip->uru);
	}
	dsp->ds_maxrblocks = disk_rcap(&msf, dsp->ds_maxblocks,
				dip->erasable,
				dip->uru);


#ifdef	PRINT_ATIP
#ifdef	DEBUG
	if (get_atip(usalp, (struct atipinfo *)mode) < 0)
		return (-1);
	/*
	 * Get pma gibt Ärger mit CW-7502
	 * Wenn Die Disk leer ist, dann stuerzt alles ab.
	 * Firmware 4.02 kann nicht get_pma
	 */
	if (dip->disk_status != DS_EMPTY) {
/*		get_pma();*/
	}
	printf("ATIP lead in:  %ld (%02d:%02d/%02d)\n",
		msf_to_lba(mode[8], mode[9], mode[10], FALSE),
		mode[8], mode[9], mode[10]);
	printf("ATIP lead out: %ld (%02d:%02d/%02d)\n",
		msf_to_lba(mode[12], mode[13], mode[14], TRUE),
		mode[12], mode[13], mode[14]);
	print_di(dip);
	print_atip(usalp, (struct atipinfo *)mode);
#endif
#endif	/* PRINT_ATIP */
	return (drive_getdisktype(usalp, dp));
}

#ifdef	PRINT_ATIP

#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);

static void 
print_di(struct disk_info *dip)
{
	static	char *ds_name[] = { "empty", "incomplete/appendable", "complete", "illegal" };
	static	char *ss_name[] = { "empty", "incomplete/appendable", "illegal", "complete", };

	IS("erasable", dip->erasable);
	printf("disk status: %s\n", ds_name[dip->disk_status]);
	printf("session status: %s\n", ss_name[dip->sess_status]);
	printf("first track: %d number of sessions: %d first track in last sess: %d last track in last sess: %d\n",
		dip->first_track,
		dip->numsess,
		dip->first_track_ls,
		dip->last_track_ls);
	IS("unrestricted", dip->uru);
	printf("Disk type: ");
	switch (dip->disk_type) {

	case SES_DA_ROM:	printf("CD-DA or CD-ROM");	break;
	case SES_CDI:		printf("CDI");			break;
	case SES_XA:		printf("CD-ROM XA");		break;
	case SES_UNDEF:		printf("undefined");		break;
	default:		printf("reserved");		break;
	}
	printf("\n");
	if (dip->did_v)
		printf("Disk id: 0x%lX\n", a_to_u_4_byte(dip->disk_id));

	printf("last start of lead in: %ld\n",
		msf_to_lba(dip->last_lead_in[1],
		dip->last_lead_in[2],
		dip->last_lead_in[3], FALSE));
	printf("last start of lead out: %ld\n",
		msf_to_lba(dip->last_lead_out[1],
		dip->last_lead_out[2],
		dip->last_lead_out[3], TRUE));

	if (dip->dbc_v)
		printf("Disk bar code: 0x%lX%lX\n",
			a_to_u_4_byte(dip->disk_barcode),
			a_to_u_4_byte(&dip->disk_barcode[4]));

	if (dip->num_opc_entries > 0) {
		printf("OPC table:\n");
	}
}

char	*cdr_subtypes[] = {
	"Normal Rewritable (CLV) media",
	"High speed Rewritable (CAV) media",
	"Medium Type A, low Beta category (A-)",
	"Medium Type A, high Beta category (A+)",
	"Medium Type B, low Beta category (B-)",
	"Medium Type B, high Beta category (B+)",
	"Medium Type C, low Beta category (C-)",
	"Medium Type C, high Beta category (C+)",
};

char	*cdrw_subtypes[] = {
	"Normal Rewritable (CLV) media",
	"High speed Rewritable (CAV) media",
	"Ultra High speed Rewritable media",
	"Ultra High speed+ Rewritable media",
	"Medium Type B, low Beta category (B-)",
	"Medium Type B, high Beta category (B+)",
	"Medium Type C, low Beta category (C-)",
	"Medium Type C, high Beta category (C+)",
};

static void 
atip_printspeed(char *fmt, int speedindex, char speedtab[])
{
	printf("%s:", fmt);
	if (speedtab[speedindex] == 0) {
		printf(" %2d (reserved val %2d)",
			speedtab[speedindex], speedindex);
	} else {
		printf(" %2d", speedtab[speedindex]);
	}
}

static void 
print_atip(SCSI *usalp, struct atipinfo *atp)
{
	char	*sub_type;
	char	*speedvtab = clv_to_speed;

	if (usalp->verbose)
		usal_prbytes("ATIP info: ", (Uchar *)atp, sizeof (*atp));

	printf("ATIP info from disk:\n");
	printf("  Indicated writing power: %d\n", atp->desc.ind_wr_power);
	if (atp->desc.erasable || atp->desc.ref_speed)
		printf("  Reference speed: %d\n", clv_to_speed[atp->desc.ref_speed]);
	IS("unrestricted", atp->desc.uru);
/*	printf("  Disk application code: %d\n", atp->desc.res5_05);*/
	IS("erasable", atp->desc.erasable);
	if (atp->desc.erasable)
		sub_type = cdrw_subtypes[atp->desc.sub_type];
	else
		sub_type = cdr_subtypes[atp->desc.sub_type];
	if (atp->desc.sub_type)
		printf("  Disk sub type: %s (%d)\n", sub_type, atp->desc.sub_type);
	printf("  ATIP start of lead in:  %ld (%02d:%02d/%02d)\n",
		msf_to_lba(atp->desc.lead_in[1],
		atp->desc.lead_in[2],
		atp->desc.lead_in[3], FALSE),
		atp->desc.lead_in[1],
		atp->desc.lead_in[2],
		atp->desc.lead_in[3]);
	printf("  ATIP start of lead out: %ld (%02d:%02d/%02d)\n",
		msf_to_lba(atp->desc.lead_out[1],
		atp->desc.lead_out[2],
		atp->desc.lead_out[3], TRUE),
		atp->desc.lead_out[1],
		atp->desc.lead_out[2],
		atp->desc.lead_out[3]);
	if (atp->desc.a1_v) {
		if (atp->desc.erasable && atp->desc.sub_type == 1) {
			speedvtab = hs_clv_to_speed;
		}
		if (atp->desc.a2_v && (atp->desc.sub_type == 2 || atp->desc.sub_type == 3)) {
			speedvtab = us_clv_to_speed;
		}
		if (atp->desc.clv_low != 0 || atp->desc.clv_high != 0) {
			atip_printspeed("  1T speed low",
				atp->desc.clv_low, speedvtab);
			atip_printspeed(" 1T speed high",
				atp->desc.clv_high, speedvtab);
			printf("\n");
		}
	}
	if (atp->desc.a2_v) {
		Uint	vlow;
		Uint	vhigh;

		vlow = (atp->desc.a2[0] >> 4) & 0x07;
		vhigh = atp->desc.a2[0] & 0x0F;

		if (vlow != 0 || vhigh != 0) {
			atip_printspeed("  2T speed low",
					vlow, speedvtab);
			atip_printspeed(" 2T speed high",
					vhigh, speedvtab);
			printf("\n");
		}
	}
	if (atp->desc.a1_v) {
		printf("  power mult factor: %d %d\n", atp->desc.power_mult, atp->desc.tgt_y_pow);
		if (atp->desc.erasable)
			printf("  recommended erase/write power: %d\n", atp->desc.rerase_pwr_ratio);
	}
	if (atp->desc.a1_v) {
		printf("  A1 values: %02X %02X %02X\n",
				(&atp->desc.res15)[1],
				(&atp->desc.res15)[2],
				(&atp->desc.res15)[3]);
	}
	if (atp->desc.a2_v) {
		printf("  A2 values: %02X %02X %02X\n",
				atp->desc.a2[0],
				atp->desc.a2[1],
				atp->desc.a2[2]);
	}
	if (atp->desc.a3_v) {
		printf("  A3 values: %02X %02X %02X\n",
				atp->desc.a3[0],
				atp->desc.a3[1],
				atp->desc.a3[2]);
	}
}
#endif	/* PRINT_ATIP */

static int 
speed_select_mmc(SCSI *usalp, cdr_t *dp, int *speedp)
{
	Uchar	mode[0x100];
	Uchar	moder[0x100];
	int	len;
	struct	cd_mode_page_05 *mp;
	struct	ricoh_mode_page_30 *rp = NULL;
	int	val;
	BOOL	forcespeed = FALSE;
	BOOL	dummy = (dp->cdr_cmdflags & F_DUMMY) != 0;

	if (speedp)
		curspeed = *speedp;

	/*
	 * Do not reset mp->test_write (-dummy) here.
	 */
	deflt_writemodes_mmc(usalp, FALSE);

	fillbytes((caddr_t)mode, sizeof (mode), '\0');

	if (!get_mode_params(usalp, 0x05, "CD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len))
		return (-1);
	if (len == 0)
		return (-1);

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof (struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);
#ifdef	DEBUG
	usal_prbytes("CD write parameter:", (Uchar *)mode, len);
#endif

    if(dummy) {
        mp->test_write = 1;
        /* but it does not work on DVD+RW and -RAM, also bail out on other
         * types that have not been tested yet */
        int profile=get_curprofile(usalp);
        switch(profile) {
            case(0x12):
            case(0x1a):
            case(0x2a):
            case(0x43):
            case(0x52):
                {
                    fprintf(stderr, 
                            "Dummy mode not possible with %s.\n",
                            mmc_obtain_profile_name(profile) );
                    exit(EXIT_FAILURE);
                }
        }
    }
    else
        mp->test_write = 0;

#ifdef	DEBUG
	usal_prbytes("CD write parameter:", (Uchar *)mode, len);
#endif
	if (!set_mode_params(usalp, "CD write parameter", mode, len, 0, -1))
		return (-1);

	/*
	 * Neither set nor get speed.
	 */
	if (speedp == 0)
		return (0);


	rp = get_justlink_ricoh(usalp, moder);
	if (mmc_isyamaha(usalp)) {
		forcespeed = FALSE;
	} else if (mmc_isplextor(usalp) && (dp->cdr_flags & CDR_FORCESPEED) != 0) {
		int	pwr;

		pwr = check_powerrec_plextor(usalp);
		if (pwr >= 0)
			forcespeed = (pwr == 0);
	} else if ((dp->cdr_flags & CDR_FORCESPEED) != 0) {
		forcespeed = rp && rp->AWSCD != 0;
	}

	if (lverbose && (dp->cdr_flags & CDR_FORCESPEED) != 0)
		printf("Forcespeed is %s.\n", forcespeed?"ON":"OFF");

	if (!forcespeed && (dp->cdr_dstat->ds_cdrflags & RF_FORCESPEED) != 0) {
		printf("Turning forcespeed on\n");
		forcespeed = TRUE;
	}
	if (forcespeed && (dp->cdr_dstat->ds_cdrflags & RF_FORCESPEED) == 0) {
		printf("Turning forcespeed off\n");
		forcespeed = FALSE;
	}
	if (mmc_isplextor(usalp) && (dp->cdr_flags & CDR_FORCESPEED) != 0) {
		powerrec_plextor(usalp, !forcespeed);
	}
	if (!mmc_isyamaha(usalp) && (dp->cdr_flags & CDR_FORCESPEED) != 0) {

		if (rp) {
			rp->AWSCD = forcespeed?1:0;
			set_mode_params(usalp, "Ricoh Vendor Page", moder, moder[0]+1, 0, -1);
			rp = get_justlink_ricoh(usalp, moder);
		}
	}

	/*
	 * 44100 * 2 * 2 =  176400 bytes/s
	 *
	 * The right formula would be:
	 * tmp = (((long)curspeed) * 1764) / 10;
	 *
	 * But the standard is rounding the wrong way.
	 * Furtunately rounding down is guaranteed.
	 */
	val = curspeed*177;
	if (val > 0xFFFF)
		val = 0xFFFF;
	if (mmc_isyamaha(usalp) && forcespeed) {
		if (force_speed_yamaha(usalp, -1, val) < 0)
			return (-1);
	} else if (mmc_set_speed(usalp, -1, val, ROTCTL_CLV) < 0) {
		return (-1);
	}

	if (scsi_get_speed(usalp, 0, &val) >= 0) {
		if (val > 0) {
		        fprintf(stderr, "Speed set to %d KB/s\n", val); 
			curspeed = val / 176;
			*speedp = curspeed;
		}
	}
	return (0);
}

/*
 * Some drives do not round up when writespeed is e.g. 1 and
 * the minimum write speed of the drive is higher. Try to increment
 * the write speed unti it gets accepted by the drive.
 */
static int 
mmc_set_speed(SCSI *usalp, int readspeed, int writespeed, int rotctl)
{
	int	rs;
	int	ws;
	int	ret = -1;
	int	c;
	int	k;

	if (scsi_get_speed(usalp, &rs, &ws) >= 0) {
		if (readspeed < 0)
			readspeed = rs;
		if (writespeed < 0)
			writespeed = ws;
	}
	if (writespeed < 0 || writespeed > 0xFFFF)
		return (ret);

	usalp->silent++;
	while (writespeed <= 0xFFFF) {
		ret = scsi_set_speed(usalp, readspeed, writespeed, rotctl);
		if (ret >= 0)
			break;
		c = usal_sense_code(usalp);
		k = usal_sense_key(usalp);
		/*
		 * Abort quickly if it does not make sense to repeat.
		 * 0x24 == Invalid field in cdb
		 * 0x24 means illegal speed.
		 */
		if ((k != SC_ILLEGAL_REQUEST) || (c != 0x24)) {
			if (usalp->silent <= 1)
				usal_printerr(usalp);
			usalp->silent--;
			return (-1);
		}
		writespeed += 177;
	}
	if (ret < 0 && usalp->silent <= 1)
		usal_printerr(usalp);
	usalp->silent--;

	return (ret);
}

static int 
speed_select_mdvd(SCSI *usalp, cdr_t *dp, int *speedp)
{
  int retcode;
  char perf_desc[28];
  int write_speed = *speedp * 1385;
   
  /* For the moment we just divide the CD speed by 7*/

  if(speedp!=NULL)
     (*speedp)=(*speedp)*8;
  
  memset(perf_desc, 0, sizeof(perf_desc));

  /* Write Rotation Control = ROTCTL_CLV 
   * | Restore Logical Unit Defaults = 0 
   * | Exact = 0 
   * | Random Access = 0) 
   */
  perf_desc[0]= ROTCTL_CLV << 3 | 0 << 2 | 0 << 1 | 0; 
  /* Start LBA to 0 */
  perf_desc[4] = 0;
  perf_desc[5] = 0;
  perf_desc[6] = 0;
  perf_desc[7] = 0;
  /* End LBA set to 0 (setting to 0xffffffff failed on my LG burner
   */
  perf_desc[8] = 0;
  perf_desc[9] = 0;
  perf_desc[10] = 0;
  perf_desc[11] = 0;
  /* Read Speed = 0xFFFF */
  perf_desc[12] = 0;
  perf_desc[13] = 0;
  perf_desc[14] = 0xFF;
  perf_desc[15] = 0xFF;
  /* Read Time = 1s */
  perf_desc[18] = 1000 >> 8;
  perf_desc[19] = 1000 & 0xFF;   
  /* Write Speed */
  perf_desc[20] = write_speed >> 24;
  perf_desc[21] = write_speed >> 16 & 0xFF;
  perf_desc[22] = write_speed >> 8 & 0xFF;
  perf_desc[23] = write_speed & 0xFF;
  /* Write Time = 1s */
  perf_desc[26] = 1000 >> 8;
  perf_desc[27] = 1000 & 0xFF;  
  
  /* retcode = scsi_set_streaming(usalp, NULL, 0); */
  retcode = scsi_set_streaming(usalp, perf_desc, sizeof(perf_desc));
  if (retcode == -1) return retcode;
  retcode = speed_select_mmc(usalp, dp, speedp);
  if(speedp!=NULL)
     (*speedp)=(*speedp)/7;
   return retcode;
}

static int 
next_wr_addr_mmc(SCSI *usalp, track_t *trackp, long *ap)
{
	struct	track_info	track_info;
	long	next_addr;
	int	result = -1;


	/*
	 * Reading info for current track may require doing the read_track_info
	 * with either the track number (if the track is currently being written)
	 * or with 0xFF (if the track hasn't been started yet and is invisible
	 */

	if (trackp != 0 && trackp->track > 0 && is_packet(trackp)) {
		usalp->silent++;
		result = read_track_info(usalp, (caddr_t)&track_info, TI_TYPE_TRACK,
							trackp->trackno,
							sizeof (track_info));
		usalp->silent--;
	}

	if (result < 0) {
		if (read_track_info(usalp, (caddr_t)&track_info, TI_TYPE_TRACK, 0xFF,
						sizeof (track_info)) < 0) {
			errmsgno(EX_BAD, "Cannot get next writable address for 'invisible' track.\n");
			errmsgno(EX_BAD, "This means that we are checking recorded media.\n");
			errmsgno(EX_BAD, "This media cannot be written in streaming mode anymore.\n");
			errmsgno(EX_BAD, "If you like to write to 'preformatted' RW media, try to blank the media first.\n");
			return (-1);
		}
	}
	if (usalp->verbose)
		usal_prbytes("track info:", (Uchar *)&track_info,
				sizeof (track_info)-usal_getresid(usalp));
	next_addr = a_to_4_byte(track_info.next_writable_addr);
	if (ap)
		*ap = next_addr;
	return (0);
}

static int 
write_leadin_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	Uint	i;
	long	startsec = 0L;

/*	if (flags & F_SAO) {*/
	if (wm_base(dp->cdr_dstat->ds_wrmode) == WM_SAO) {
		if (debug || lverbose) {
			printf("Sending CUE sheet...\n");
			flush();
		}
		if ((*dp->cdr_send_cue)(usalp, dp, trackp) < 0) {
			errmsgno(EX_BAD, "Cannot send CUE sheet.\n");
			return (-1);
		}

		(*dp->cdr_next_wr_address)(usalp, &trackp[0], &startsec);
		if (trackp[0].flags & TI_TEXT) {
			startsec = dp->cdr_dstat->ds_first_leadin;
			printf("SAO startsec: %ld\n", startsec);
		} else if (startsec <= 0 && startsec != -150) {
			if(lverbose>2)
				fprintf(stderr, "WARNING: Drive returns wrong startsec (%ld) using -150\n",
					startsec);
			startsec = -150;
		}
		if (debug)
			printf("SAO startsec: %ld\n", startsec);

		if (trackp[0].flags & TI_TEXT) {
			if (startsec > 0) {
				errmsgno(EX_BAD, "CD-Text must be in first session.\n");
				return (-1);
			}
			if (debug || lverbose)
				printf("Writing lead-in...\n");
			if (write_cdtext(usalp, dp, startsec) < 0)
				return (-1);

			dp->cdr_dstat->ds_cdrflags |= RF_LEADIN;
		} else for (i = 1; i <= trackp->tracks; i++) {
			trackp[i].trackstart += startsec +150;
		}
#ifdef	XXX
		if (debug || lverbose)
			printf("Writing lead-in...\n");

		pad_track(usalp, dp, &trackp[1], -150, (Llong)0,
					FALSE, 0);
#endif
	}
/*	if (flags & F_RAW) {*/
    if (wm_base(dp->cdr_dstat->ds_wrmode) == WM_RAW) {
        /*
		 * In RAW write mode, we now write the lead in (TOC).
		 */
		(*dp->cdr_next_wr_address)(usalp, &trackp[0], &startsec);
		if (startsec > -4500) {
			/*
			 * There must be at least 1 minute lead-in.
			 */
			errmsgno(EX_BAD, "WARNING: Drive returns wrong startsec (%ld) using %ld from ATIP\n",
					startsec, (long)dp->cdr_dstat->ds_first_leadin);
			startsec = dp->cdr_dstat->ds_first_leadin;
		}
		if (startsec > -4500) {
			errmsgno(EX_BAD, "Illegal startsec (%ld)\n", startsec);
			return (-1);
		}
		if (debug || lverbose)
			printf("Writing lead-in at sector %ld\n", startsec);
		if (write_leadin(usalp, dp, trackp, startsec) < 0)
			return (-1);
		dp->cdr_dstat->ds_cdrflags |= RF_LEADIN;
	}
	return (0);
}

int	st2mode[] = {
	0,		/* 0			*/
	TM_DATA,	/* 1 ST_ROM_MODE1	*/
	TM_DATA,	/* 2 ST_ROM_MODE2	*/
	0,		/* 3			*/
	0,		/* 4 ST_AUDIO_NOPRE	*/
	TM_PREEM,	/* 5 ST_AUDIO_PRE	*/
	0,		/* 6			*/
	0,		/* 7			*/
};

static int 
next_wr_addr_mdvd(SCSI *usalp, track_t *trackp, long *ap)
{
	int     track=0;
	struct	track_info	track_info;
	long	next_addr;
	int	result = -1;
	struct  disk_info disk_info;
	if (trackp){
	    track = trackp->trackno;
	}

	if (trackp != 0 && track > 0 && is_packet(trackp)) {
		usalp->silent++;
		result = read_track_info(usalp, (caddr_t)&track_info, TI_TYPE_SESS, track, sizeof(track_info));
		usalp->silent--;
		if (scsi_in_progress(usalp)){
		  return -1;
		}
		
	}

	if (result < 0) {
	  /* Get the last rzone*/
	        if(read_disk_info(usalp,(caddr_t)&disk_info,8)<0)
		  return (-1);
	     
		/* if (read_track_info(usalp, (caddr_t)&track_info, TI_TYPE_SESS, 0xFF, sizeof(track_info)) < 0) */
		    if (read_rzone_info(usalp, (caddr_t)&track_info, sizeof(track_info)) < 0)
			return (-1);
	}
	if (usalp->verbose)
		usal_prbytes("track info:", (Uchar *)&track_info,
				sizeof(track_info)-usal_getresid(usalp));
	next_addr = a_to_4_byte(track_info.next_writable_addr);
	if (ap)
		*ap = next_addr;
	return (0);
}

static int 
open_track_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	Uchar	mode[0x100];
	int	len;
	struct	cd_mode_page_05 *mp;

	if (!is_tao(trackp) && !is_packet(trackp)) {
		if (trackp->pregapsize > 0 && (trackp->flags & TI_PREGAP) == 0) {
			if (lverbose) {
				printf("Writing pregap for track %d at %ld\n",
					(int)trackp->trackno,
					trackp->trackstart-trackp->pregapsize);
			}
			/*
			 * XXX Do we need to check isecsize too?
			 */
			pad_track(usalp, dp, trackp,
				trackp->trackstart-trackp->pregapsize,
				(Llong)trackp->pregapsize*trackp->secsize,
					FALSE, 0);
		}
		return (0);
	}

	fillbytes((caddr_t)mode, sizeof (mode), '\0');

	if (!get_mode_params(usalp, 0x05, "CD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len))
		return (-1);
	if (len == 0)
		return (-1);

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof (struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);


/*	mp->track_mode = ???;*/
	mp->track_mode = st2mode[trackp->sectype & ST_MASK];
/*	mp->copy = ???;*/
	mp->dbtype = trackp->dbtype;

/*i_to_short(mp->audio_pause_len, 300);*/
/*i_to_short(mp->audio_pause_len, 150);*/
/*i_to_short(mp->audio_pause_len, 0);*/

	if (is_packet(trackp)) {
		mp->write_type = WT_PACKET;
		mp->track_mode |= TM_INCREMENTAL;
		mp->fp = (trackp->pktsize > 0) ? 1 : 0;
		i_to_4_byte(mp->packet_size, trackp->pktsize);
	} else if (is_tao(trackp)) {
		mp->write_type = WT_TAO;
		mp->fp = 0;
		i_to_4_byte(mp->packet_size, 0);
	} else {
		errmsgno(EX_BAD, "Unknown write mode.\n");
		return (-1);
	}
	if (trackp->isrc) {
		mp->ISRC[0] = 0x80;	/* Set ISRC valid */
		strncpy((char *)&mp->ISRC[1], trackp->isrc, 12);

	} else {
		fillbytes(&mp->ISRC[0], sizeof (mp->ISRC), '\0');
	}

#ifdef	DEBUG
	usal_prbytes("CD write parameter:", (Uchar *)mode, len);
#endif
	if (!set_mode_params(usalp, "CD write parameter", mode, len, 0, trackp->secsize))
		return (-1);

	return (0);
}

static int 
open_track_mdvd(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	Uchar	mode[0x100];
	int	len;
	struct	cd_mode_page_05 *mp;

	if (is_packet(trackp)) {
	       fillbytes((caddr_t)mode, sizeof(mode), '\0');
	  
	       if (!get_mode_params(usalp, 0x05, "DVD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len))
	              return (-1);
	       if (len == 0)
		      return (-1);

	        mp = (struct cd_mode_page_05 *)
	              (mode + sizeof(struct scsi_mode_header) +
		      ((struct scsi_mode_header *)mode)->blockdesc_len);

		mp->write_type = WT_PACKET;
		mp->LS_V = 1;
		/*For now we set the link size to 0x10(32k) because Pioneer-A03 only support this */
		mp->link_size=0x10;
		mp->fp = 1;
		i_to_4_byte(mp->packet_size, trackp->pktsize);
	} else {
	     return 0;
	}
 
	if (!set_mode_params(usalp, "CD write parameter", mode, len, 0, trackp->secsize))
		return (-1);

	return (0);
}

static int 
close_track_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	int	ret;

	if (!is_tao(trackp) && !is_packet(trackp))
		return (0);

	if (scsi_flush_cache(usalp, (dp->cdr_cmdflags&F_IMMED) != 0) < 0) {
		printf("Trouble flushing the cache\n");
		return (-1);
	}
	wait_unit_ready(usalp, 300);		/* XXX Wait for ATAPI */
	if (is_packet(trackp) && !is_noclose(trackp)) {
			/* close the incomplete track */
		ret = scsi_close_tr_session(usalp, CL_TYPE_TRACK, 0xFF,
				(dp->cdr_cmdflags&F_IMMED) != 0);
		wait_unit_ready(usalp, 300);	/* XXX Wait for ATAPI */
		return (ret);
	}
	return (0);
}

static int 
close_track_mdvd(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	int	ret;
	if (!is_packet(trackp))
	     return (0);

	if (scsi_flush_cache(usalp, (dp->cdr_cmdflags&F_IMMED) != 0) < 0) {
		printf("Trouble flushing the cache\n");
		return -1;
	}
	wait_unit_ready(usalp, 300);		/* XXX Wait for ATAPI */
	if (is_packet(trackp) && !is_noclose(trackp)) {
			/* close the incomplete track */
		ret = scsi_close_tr_session(usalp, 1, 0xFF, (dp->cdr_cmdflags&F_IMMED) != 0);
		wait_unit_ready(usalp, 300);	/* XXX Wait for ATAPI */
		return (ret);
	}
	return (0);
}

int	toc2sess[] = {
	SES_DA_ROM,	/* CD-DA		 */
	SES_DA_ROM,	/* CD-ROM		 */
	SES_XA,		/* CD-ROM XA mode 1	 */
	SES_XA,		/* CD-ROM XA MODE 2	 */
	SES_CDI,	/* CDI			 */
	SES_DA_ROM,	/* Invalid - use default */
	SES_DA_ROM,	/* Invalid - use default */
};

static int 
open_session_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	Uchar	mode[0x100];
	int	len;
	struct	cd_mode_page_05 *mp;

	fillbytes((caddr_t)mode, sizeof (mode), '\0');

	if (!get_mode_params(usalp, 0x05, "CD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len))
		return (-1);
	if (len == 0)
		return (-1);

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof (struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);

	mp->write_type = WT_TAO; /* fix to allow DAO later */
	/*
	 * We need to set the right dbtype here because Sony drives
	 * don't like multi session in to be set with DB_ROM_MODE1
	 * which is set by us at the beginning as default as some drives
	 * have illegal default values.
	 */
	mp->track_mode = st2mode[trackp[0].sectype & ST_MASK];
	mp->dbtype = trackp[0].dbtype;

	if (!is_tao(trackp) && !is_packet(trackp)) {
		mp->write_type = WT_SAO;
		if (dp->cdr_dstat->ds_cdrflags & RF_AUDIOMASTER)
			mp->write_type = 8;
		mp->track_mode = 0;
		mp->dbtype = DB_RAW;
	}
	if (is_raw(trackp)) {
		mp->write_type = WT_RAW;
		mp->track_mode = 0;

		if (is_raw16(trackp)) {
			mp->dbtype = DB_RAW_PQ;
		} else if (is_raw96r(trackp)) {
			mp->dbtype = DB_RAW_PW_R;
		} else {
			mp->dbtype = DB_RAW_PW;
		}
	}

	mp->multi_session = (track_base(trackp)->tracktype & TOCF_MULTI) ?
				MS_MULTI : MS_NONE;
	mp->session_format = toc2sess[track_base(trackp)->tracktype & TOC_MASK];

	if (trackp->isrc) {
		mp->media_cat_number[0] = 0x80;	/* Set MCN valid */
		strncpy((char *)&mp->media_cat_number[1], trackp->isrc, 13);

	} else {
		fillbytes(&mp->media_cat_number[0], sizeof (mp->media_cat_number), '\0');
	}
#ifdef	DEBUG
	usal_prbytes("CD write parameter:", (Uchar *)mode, len);
#endif
	if (!set_mode_params(usalp, "CD write parameter", mode, len, 0, -1))
		return (-1);

	return (0);
}

static int 
open_session_mdvd(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	Uchar	mode[0x100];
	int	tracks = trackp->tracks;

	int	len;
	struct	cd_mode_page_05 *mp;
	Ulong totalsize;
	int i;
	int profile;

	fillbytes((caddr_t)mode, sizeof(mode), '\0');

	if (!get_mode_params(usalp, 0x05, "DVD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len))
		return (-1);
	if (len == 0)
		return (-1);

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof(struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);
	if(is_packet(trackp)){
	  mp->write_type=WT_PACKET;
	  mp->fp=0;
	  mp->BUFE=1;
	  mp->track_mode=1;
	}else{
	  mp->write_type = WT_SAO; 
	}

	mp->multi_session = (track_base(trackp)->tracktype & TOCF_MULTI) ?
				MS_MULTI : MS_NONE;
	mp->session_format = toc2sess[track_base(trackp)->tracktype & TOC_MASK];

	/* Enable Burnfree by default, allow to disable. XXX Sucks, duplicated functionality. */
	if (dp->cdr_cdcap->BUF != 0) {
		if (lverbose > 2)
			fprintf(stderr, 
					"BURN-Free is %s.\n"
					"Turning BURN-Free on\n",
					mp->BUFE?"ON":"OFF");
		mp->BUFE = 1;
	}
	if (driveropts != NULL) {
		if ((strcmp(driveropts, "noburnproof") == 0 ||
					strcmp(driveropts, "noburnfree") == 0)) {
			if(lverbose>1)
				fprintf(stderr, "Turning BURN-Free off\n");
			mp->BUFE = 0;
		}
		else if ((strcmp(driveropts, "burnproof") == 0 ||
					strcmp(driveropts, "burnfree") == 0)) {
			/* a NOP, we enable burnfree by default */
			if(lverbose>2)
				fprintf(stderr, "Found burnproof/burnfree in driveropts, those options are enabled by default now.");
		}
		else if (strcmp(driveropts, "help") == 0) {
			mmc_opthelp(dp, 0);
		} 
		else {
			errmsgno(EX_BAD, "Bad driver opts '%s'.\n", driveropts);
			mmc_opthelp(dp, EX_BAD);
		}
	}


	if (!set_mode_params(usalp, "DVD write parameter", mode, len, 0, -1))
		return (-1);

		
	totalsize=0;
	for(i=1;i<=tracks;i++) {
	  totalsize+=trackp[i].tracksecs;
	}
       
	profile = get_curprofile(usalp);
	if(!is_packet(trackp) && profile != 0x1A){
	  /* in DAO mode we need to reserve space for the track*/
	  if(reserve_track(usalp, totalsize)<0)
	    return (-1);
	  }
	return (0);
}

static int 
waitfix_mmc(SCSI *usalp, int secs)
{
	char	dibuf[16];
	int	i;
	int	key;
#define	W_SLEEP	2

	usalp->silent++;
	for (i = 0; i < secs/W_SLEEP; i++) {
		if (read_disk_info(usalp, dibuf, sizeof (dibuf)) >= 0) {
			usalp->silent--;
			return (0);
		}
		key = usal_sense_key(usalp);
		if (key != SC_UNIT_ATTENTION && key != SC_NOT_READY)
			break;
		sleep(W_SLEEP);
	}
	usalp->silent--;
	return (-1);
#undef	W_SLEEP
}

static int 
fixate_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	int	ret = 0;
	int	key = 0;
	int	code = 0;
	struct timeval starttime;
	struct timeval stoptime;
	int	dummy = (track_base(trackp)->tracktype & TOCF_DUMMY) != 0;

	if(debug)
		 printf("fixate_mmc\n");
	starttime.tv_sec = 0;
	starttime.tv_usec = 0;
	stoptime = starttime;
	gettimeofday(&starttime, (struct timezone *)0);

	if (dummy && lverbose)
		printf("WARNING: Some drives don't like fixation in dummy mode.\n");

	usalp->silent++;
	if(debug)
		 printf("is_tao: %d,is_packet: %d\n", is_tao(trackp), is_packet(trackp));
	if (is_tao(trackp) || is_packet(trackp)) {
		ret = scsi_close_tr_session(usalp, CL_TYPE_SESSION, 0,
				(dp->cdr_cmdflags&F_IMMED) != 0);
	} else {
		if (scsi_flush_cache(usalp, (dp->cdr_cmdflags&F_IMMED) != 0) < 0) {
			if (!scsi_in_progress(usalp))
				printf("Trouble flushing the cache\n");
		}
	}
	usalp->silent--;
	key = usal_sense_key(usalp);
	code = usal_sense_code(usalp);

	usalp->silent++;
	if (debug && !unit_ready(usalp)) {
		fprintf(stderr, "Early return from fixating. Ret: %d Key: %d, Code: %d\n", ret, key, code);
	}
	usalp->silent--;

	if (ret >= 0) {
		wait_unit_ready(usalp, 420/curspeed);	/* XXX Wait for ATAPI */
		waitfix_mmc(usalp, 420/curspeed);	/* XXX Wait for ATAPI */
		return (ret);
	}

	if ((dummy != 0 && (key != SC_ILLEGAL_REQUEST)) ||
		/*
		 * Try to suppress messages from drives that don't like fixation
		 * in -dummy mode.
		 */
		((dummy == 0) &&
		(((key != SC_UNIT_ATTENTION) && (key != SC_NOT_READY)) ||
				((code != 0x2E) && (code != 0x04))))) {
		/*
		 * UNIT ATTENTION/2E seems to be a magic for old Mitsumi ATAPI drives
		 * NOT READY/ code 4 qual 7 (logical unit not ready, operation in progress)
		 * seems to be a magic for newer Mitsumi ATAPI drives
		 * NOT READY/ code 4 qual 8 (logical unit not ready, long write in progress)
		 * seems to be a magic for SONY drives
		 * when returning early from fixating.
		 * Try to supress the error message in this case to make
		 * simple minded users less confused.
		 */
		usal_printerr(usalp);
		usal_printresult(usalp);	/* XXX restore key/code in future */
	}

	if (debug && !unit_ready(usalp)) {
		fprintf(stderr, "Early return from fixating. Ret: %d Key: %d, Code: %d\n", ret, key, code);
	}

	wait_unit_ready(usalp, 420);	 /* XXX Wait for ATAPI */
	waitfix_mmc(usalp, 420/curspeed); /* XXX Wait for ATAPI */

	if (!dummy &&
		(ret >= 0 || (key == SC_UNIT_ATTENTION && code == 0x2E))) {
		/*
		 * Some ATAPI drives (e.g. Mitsumi) imply the
		 * IMMED bit in the SCSI cdb. As there seems to be no
		 * way to properly check for the real end of the
		 * fixating process we wait for the expected time.
		 */
		gettimeofday(&stoptime, (struct timezone *)0);
		timevaldiff(&starttime, &stoptime);
		if (stoptime.tv_sec < (220 / curspeed)) {
			unsigned secs;

			if (lverbose) {
				printf("Actual fixating time: %ld seconds\n",
							(long)stoptime.tv_sec);
			}
			secs = (280 / curspeed) - stoptime.tv_sec;
			if (lverbose) {
				printf("ATAPI early return: sleeping %d seconds.\n",
								secs);
			}
			sleep(secs);
		}
	}
	return (ret);
}

static int 
fixate_mdvd(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	int ret;
	if (scsi_flush_cache(usalp, (dp->cdr_cmdflags&F_IMMED) != 0) < 0) {
		printf("Trouble flushing the cache\n");
		return -1;
	}
	wait_unit_ready(usalp, 300);		/* XXX Wait for ATAPI */
      /*set a really BIG timeout and call fixate_mmc
	 The BIG timeout is needed in case there was a very short rzone to write at the 
	 beginning of the disk, because lead-out needs to be at some distance.
      */
	if(debug)
		printf("fixate_mdvd\n");
      usal_settimeout(usalp, 1000);
      if(is_packet(trackp) || dp->profile == 0x1B){
	  scsi_close_tr_session(usalp, CL_TYPE_SESSION, 0, FALSE);
      }
      ret = fixate_mmc(usalp, dp, trackp);
      if (dp->profile == 0x2B) {
	  scsi_close_tr_session(usalp, CL_TYPE_OPEN_SESSION, 0, FALSE);
	  scsi_close_tr_session(usalp, CL_TYPE_FINALISE_MINRAD, 0, FALSE);
      }
      usal_settimeout(usalp, 200);

      return ret;
}

char	*blank_types[] = {
	"entire disk",
	"PMA, TOC, pregap",
	"incomplete track",
	"reserved track",
	"tail of track",
	"closing of last session",
	"last session",
	"reserved blanking type",
};

char	*format_types[] = {
	"full format",
	"background format",
	"forced format",
};

static int 
blank_mmc(SCSI *usalp, cdr_t *dp, long addr, int blanktype)
{
	BOOL	cdrr	 = FALSE;	/* Read CD-R	*/
	BOOL	cdwr	 = FALSE;	/* Write CD-R	*/
	BOOL	cdrrw	 = FALSE;	/* Read CD-RW	*/
	BOOL	cdwrw	 = FALSE;	/* Write CD-RW	*/
	int	ret;

	mmc_check(usalp, &cdrr, &cdwr, &cdrrw, &cdwrw, NULL, NULL);
	if (!cdwrw)
		return (blank_dummy(usalp, dp, addr, blanktype));

	if (dp->profile == 0x1A) {
		printf("Error: this media does not support blanking, ignoring.\n");
		return (blank_dummy(usalp, dp, addr, blanktype));
	}
	if (lverbose) {
		printf("Blanking %s\n", blank_types[blanktype & 0x07]);
		flush();
	}

	ret = scsi_blank(usalp, addr, blanktype, (dp->cdr_cmdflags&F_IMMED) != 0);
	if (ret < 0)
		return (ret);

	wait_unit_ready(usalp, 90*60/curspeed);	/* XXX Wait for ATAPI */
	waitfix_mmc(usalp, 90*60/curspeed);	/* XXX Wait for ATAPI */
	return (ret);
}

static int format_mdvd(SCSI *usalp, cdr_t *dp, int formattype)
{
extern	char	*buf;
	BOOL	dvdwr	 = FALSE;	/* Write DVD	*/
	int	ret;
	int 	profile;
	char	addr[12];
	struct disk_info *dip;

	if (debug || lverbose > 2)
		printf("format_mdvd\n");
	mmc_check(usalp, NULL, NULL, NULL, NULL, NULL, &dvdwr);
	if (!dvdwr)
		return (format_dummy(usalp, dp, formattype));

	if (debug || lverbose > 2)
		printf("format_mdvd: drive is a dvd burner.\n");
	profile = get_curprofile(usalp);
	if (profile != 0x1A) {
		printf("Error: only support DVD+RW formating, ignoring.\n");
	        return (format_dummy(usalp, dp, formattype));
	}
	dip = (struct disk_info *)buf;
	if (get_diskinfo(usalp, dip) < 0)
		return -1;
	
	if (dip->disk_status & 3 && formattype != FORCE_FORMAT) {
		printf("Error: disk already formated, ignoring.\n");
	        return -1;
        }
	addr[0] = 0;           /* "Reserved" */
	addr[1] = 2;           /* "IMMED" flag */
	addr[2] = 0;           /* "Descriptor Length" (MSB) */
	addr[3] = 8;           /* "Descriptor Length" (LSB) */
	addr[4+0] = 0xff;
	addr[4+1] = 0xff;
	addr[4+2] = 0xff;
	addr[4+3] = 0xff;
	addr[4+4] = 0x26<<2;
	addr[4+5] = 0;
	addr[4+6] = 0;
	addr[4+7] = 0;
	if (formattype == FORCE_FORMAT) {
	    printf("format_mdvd: forcing reformat.\n"); 
	    formattype = FULL_FORMAT;
	    addr[4+0] = 0;
	    addr[4+1] = 0;
	    addr[4+2] = 0;
	    addr[4+3] = 0;
	    addr[4+7] = 1;
	} else {
	    printf("format_mdvd: media is unformated.\n"); 
	}

	if (lverbose) {
		printf("Formating %s\n", format_types[formattype & 0x07]);
		flush();
	}
	if (formattype == FULL_FORMAT) {
		ret = scsi_format(usalp, (caddr_t)&addr, sizeof(addr), FALSE);
	} else {
		ret = scsi_format(usalp, (caddr_t)&addr, sizeof(addr), TRUE);
	}
	if (ret < 0)
		return (ret);

	wait_unit_ready(usalp, 90*60/curspeed);	/* XXX Wait for ATAPI */
	waitfix_mmc(usalp, 90*60/curspeed);	/* XXX Wait for ATAPI */
	return (ret);
}

static int 
send_opc_mmc(SCSI *usalp, caddr_t bp, int cnt, int doopc)
{
	int	ret;

	usalp->silent++;
	ret = send_opc(usalp, bp, cnt, doopc);
	usalp->silent--;

	if (ret >= 0)
		return (ret);

	/* BEGIN CSTYLED */
	/*
	 * Hack for a mysterioys drive ....
	 * Device type    : Removable CD-ROM
	 * Version        : 0
	 * Response Format: 1
	 * Vendor_info    : 'RWD     '
	 * Identifikation : 'RW2224          '
	 * Revision       : '2.53'
	 * Device seems to be: Generic mmc CD-RW.
	 *
	 * Performing OPC...
	 * CDB:  54 01 00 00 00 00 00 00 00 00
	 * Sense Bytes: 70 00 06 00 00 00 00 0A 00 00 00 00 5A 03 00 00
	 * Sense Key: 0x6 Unit Attention, Segment 0
	 * Sense Code: 0x5A Qual 0x03 (operator selected write permit) Fru 0x0
	 * Sense flags: Blk 0 (not valid)
	 */
	/* END CSTYLED */
	if (usal_sense_key(usalp) == SC_UNIT_ATTENTION &&
	    usal_sense_code(usalp) == 0x5A &&
	    usal_sense_qual(usalp) == 0x03)
		return (0);

	/*
	 * Do not make the condition:
	 * "Power calibration area almost full" a fatal error.
	 * It just flags that we have a single and last chance to write now.
	 */
	if ((usal_sense_key(usalp) == SC_RECOVERABLE_ERROR ||
	    usal_sense_key(usalp) == SC_MEDIUM_ERROR) &&
	    usal_sense_code(usalp) == 0x73 &&
	    usal_sense_qual(usalp) == 0x01)
		return (0);

	/*
	 * Send OPC is optional.
	 */
	if (usal_sense_key(usalp) != SC_ILLEGAL_REQUEST) {
		if (usalp->silent <= 0)
			usal_printerr(usalp);
		return (ret);
	}
	return (0);
}

static int 
opt1_mmc(SCSI *usalp, cdr_t *dp)
{
	int	oflags = dp->cdr_dstat->ds_cdrflags;

	if ((dp->cdr_dstat->ds_cdrflags & RF_AUDIOMASTER) != 0) {
		printf("Turning Audio Master Q. R. on\n");
		if (set_audiomaster_yamaha(usalp, dp, TRUE) < 0)
			return (-1);
		if (!debug && lverbose <= 1)
			dp->cdr_dstat->ds_cdrflags &= ~RF_PRATIP;
		if (getdisktype_mmc(usalp, dp) < 0) {
			dp->cdr_dstat->ds_cdrflags = oflags;
			return (-1);
		}
		dp->cdr_dstat->ds_cdrflags = oflags;
		if (oflags & RF_PRATIP) {
			msf_t   msf;
			lba_to_msf(dp->cdr_dstat->ds_first_leadin, &msf);
			printf("New start of lead in: %ld (%02d:%02d/%02d)\n",
				(long)dp->cdr_dstat->ds_first_leadin,
				msf.msf_min,
				msf.msf_sec,
				msf.msf_frame);
			lba_to_msf(dp->cdr_dstat->ds_maxblocks, &msf);
			printf("New start of lead out: %ld (%02d:%02d/%02d)\n",
				(long)dp->cdr_dstat->ds_maxblocks,
				msf.msf_min,
				msf.msf_sec,
				msf.msf_frame);
		}
	}
	if (mmc_isplextor(usalp)) {
		int	gcode;

		if ((dp->cdr_flags & (CDR_SINGLESESS|CDR_HIDE_CDR)) != 0) {
			if (ss_hide_plextor(usalp,
			    (dp->cdr_dstat->ds_cdrflags & RF_SINGLESESS) != 0,
			    (dp->cdr_dstat->ds_cdrflags & RF_HIDE_CDR) != 0) < 0)
				return (-1);
		}

		if ((dp->cdr_flags & CDR_SPEEDREAD) != 0) {
			if (speed_rd_plextor(usalp,
			    (dp->cdr_dstat->ds_cdrflags & RF_SPEEDREAD) != 0) < 0)
				return (-1);
		}

		if ((dp->cdr_cmdflags & F_SETDROPTS) ||
		    (wm_base(dp->cdr_dstat->ds_wrmode) == WM_SAO) ||
		    (wm_base(dp->cdr_dstat->ds_wrmode) == WM_RAW))
			gcode = do_gigarec_plextor(usalp);
		else
			gcode = gigarec_plextor(usalp, 0);
		if (gcode != 0) {
			msf_t   msf;

			dp->cdr_dstat->ds_first_leadin =
					gigarec_mult(gcode, dp->cdr_dstat->ds_first_leadin);
			dp->cdr_dstat->ds_maxblocks =
					gigarec_mult(gcode, dp->cdr_dstat->ds_maxblocks);

			if (oflags & RF_PRATIP) {
				lba_to_msf(dp->cdr_dstat->ds_first_leadin, &msf);
				printf("New start of lead in: %ld (%02d:%02d/%02d)\n",
					(long)dp->cdr_dstat->ds_first_leadin,
					msf.msf_min,
					msf.msf_sec,
					msf.msf_frame);
				lba_to_msf(dp->cdr_dstat->ds_maxblocks, &msf);
				printf("New start of lead out: %ld (%02d:%02d/%02d)\n",
					(long)dp->cdr_dstat->ds_maxblocks,
					msf.msf_min,
					msf.msf_sec,
					msf.msf_frame);
			}
		}
	}
	return (0);
}

static int 
opt2_mmc(SCSI *usalp, cdr_t *dp)
{
	Uchar	mode[0x100];
	Uchar	moder[0x100];
	int	len;
	struct	cd_mode_page_05 *mp;
	struct	ricoh_mode_page_30 *rp = NULL;
	BOOL	burnfree = FALSE;

	fillbytes((caddr_t)mode, sizeof (mode), '\0');

	if (!get_mode_params(usalp, 0x05, "CD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len))
		return (-1);
	if (len == 0)
		return (-1);

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof (struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);


	rp = get_justlink_ricoh(usalp, moder);

	if (dp->cdr_cdcap->BUF != 0) {
		burnfree = (mp->BUFE != 0);
	} else if ((dp->cdr_flags & CDR_BURNFREE) != 0) {
		burnfree = (rp && (rp->BUEFE != 0));
	}

	if (lverbose>2 && (dp->cdr_flags & CDR_BURNFREE) != 0)
		printf("BURN-Free is %s.\n", burnfree?"ON":"OFF");

	if (!burnfree && (dp->cdr_dstat->ds_cdrflags & RF_BURNFREE) != 0) {
		if(lverbose>2)
			printf("Turning BURN-Free on\n");
		burnfree = TRUE;
	}
	if (burnfree && (dp->cdr_dstat->ds_cdrflags & RF_BURNFREE) == 0) {
		if(lverbose>2)
			printf("Turning BURN-Free off\n");
		burnfree = FALSE;
	}
	if (dp->cdr_cdcap->BUF != 0) {
		mp->BUFE = burnfree?1:0;
	} 
    else if ((dp->cdr_flags & CDR_BURNFREE) != 0) {

		if (rp)
			rp->BUEFE = burnfree?1:0;
	}
	if (rp) {
		/*
		 * Clear Just-Link counter
		 */
		i_to_2_byte(rp->link_counter, 0);
		if (xdebug)
			usal_prbytes("Mode Select Data ", moder, moder[0]+1);

		if (!set_mode_params(usalp, "Ricoh Vendor Page", moder, moder[0]+1, 0, -1))
			return (-1);
		rp = get_justlink_ricoh(usalp, moder);
	}

#ifdef	DEBUG
	usal_prbytes("CD write parameter:", (Uchar *)mode, len);
#endif
	if (!set_mode_params(usalp, "CD write parameter", mode, len, 0, -1))
		return (-1);

	if (mmc_isplextor(usalp)) {
		/*
		 * Clear Burn-Proof counter
		 */
		usalp->silent++;
		bpc_plextor(usalp, 1, NULL);
		usalp->silent--;

		do_varirec_plextor(usalp);
	}

	return (0);
}

static int 
opt1_mdvd(SCSI *usalp, cdr_t *dp)
{
	int	oflags = dp->cdr_dstat->ds_cdrflags;

	if ((dp->cdr_dstat->ds_cdrflags & RF_AUDIOMASTER) != 0) {
		printf("Turning Audio Master Q. R. on\n");
		if (set_audiomaster_yamaha(usalp, dp, TRUE) < 0)
			return (-1);
		if (!debug && lverbose <= 1)
			dp->cdr_dstat->ds_cdrflags &= ~RF_PRATIP;
		if (getdisktype_mdvd(usalp, dp) < 0) {
			dp->cdr_dstat->ds_cdrflags = oflags;
			return (-1);
		}
		dp->cdr_dstat->ds_cdrflags = oflags;
		if (oflags & RF_PRATIP) {
			msf_t   msf;
			lba_to_msf(dp->cdr_dstat->ds_first_leadin, &msf);
			printf("New start of lead in: %ld (%02d:%02d/%02d)\n",
				(long)dp->cdr_dstat->ds_first_leadin,
		                msf.msf_min,
        		        msf.msf_sec,
                		msf.msf_frame);
			lba_to_msf(dp->cdr_dstat->ds_maxblocks, &msf);
			printf("New start of lead out: %ld (%02d:%02d/%02d)\n",
				(long)dp->cdr_dstat->ds_maxblocks,
		                msf.msf_min,
        		        msf.msf_sec,
                		msf.msf_frame);
		}
	}
	return (0);
}

static int
scsi_sony_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_xg5(usalp, bp, sectaddr, size, blocks));
}

Uchar	db2df[] = {
	0x00,			/*  0 2352 bytes of raw data			*/
	0xFF,			/*  1 2368 bytes (raw data + P/Q Subchannel)	*/
	0xFF,			/*  2 2448 bytes (raw data + P-W Subchannel)	*/
	0xFF,			/*  3 2448 bytes (raw data + P-W raw Subchannel)*/
	0xFF,			/*  4 -    Reserved				*/
	0xFF,			/*  5 -    Reserved				*/
	0xFF,			/*  6 -    Reserved				*/
	0xFF,			/*  7 -    Vendor specific			*/
	0x10,			/*  8 2048 bytes Mode 1 (ISO/IEC 10149)		*/
	0x30,			/*  9 2336 bytes Mode 2 (ISO/IEC 10149)		*/
	0xFF,			/* 10 2048 bytes Mode 2! (CD-ROM XA form 1)	*/
	0xFF,			/* 11 2056 bytes Mode 2 (CD-ROM XA form 1)	*/
	0xFF,			/* 12 2324 bytes Mode 2 (CD-ROM XA form 2)	*/
	0xFF,			/* 13 2332 bytes Mode 2 (CD-ROM XA 1/2+subhdr)	*/
	0xFF,			/* 14 -    Reserved				*/
	0xFF,			/* 15 -    Vendor specific			*/
};

static int 
gen_cue_mmc(track_t *trackp, void *vcuep, BOOL needgap)
{
	int	tracks = trackp->tracks;
	int	i;
	struct mmc_cue	**cuep = vcuep;
	struct mmc_cue	*cue;
	struct mmc_cue	*cp;
	int	ncue = 0;
	int	icue = 0;
	int	pgsize;
	msf_t	m;
	int	ctl;
	int	df;
	int	scms;

	cue = malloc(1);

	for (i = 0; i <= tracks; i++) {
		ctl = (st2mode[trackp[i].sectype & ST_MASK]) << 4;
		if (is_copy(&trackp[i]))
			ctl |= TM_ALLOW_COPY << 4;
		if (is_quadro(&trackp[i]))
			ctl |= TM_QUADRO << 4;
		df = db2df[trackp[i].dbtype & 0x0F];
		if (trackp[i].tracktype == TOC_XA2 &&
		    trackp[i].sectype   == (SECT_MODE_2_MIX|ST_MODE_RAW)) {
			/*
			 * Hack for CUE with MODE2/CDI and
			 * trackp[i].dbtype == DB_RAW
			 */
			df = 0x21;
		}

		if (trackp[i].isrc) {	/* MCN or ISRC */
			ncue += 2;
			cue = realloc(cue, ncue * sizeof (*cue));
			cp = &cue[icue++];
			if (i == 0) {
				cp->cs_ctladr = 0x02;
				movebytes(&trackp[i].isrc[0], &cp->cs_tno, 7);
				cp = &cue[icue++];
				cp->cs_ctladr = 0x02;
				movebytes(&trackp[i].isrc[7], &cp->cs_tno, 7);
			} else {
				cp->cs_ctladr = 0x03;
				cp->cs_tno = i;
				movebytes(&trackp[i].isrc[0], &cp->cs_index, 6);
				cp = &cue[icue++];
				cp->cs_ctladr = 0x03;
				cp->cs_tno = i;
				movebytes(&trackp[i].isrc[6], &cp->cs_index, 6);
			}
		}
		if (i == 0) {	/* Lead in */
			df &= ~7;	/* Mask off data size & nonRAW subch */
			if (df < 0x10)
				df |= 1;
			else
				df |= 4;
			if (trackp[0].flags & TI_TEXT)	/* CD-Text in Lead-in*/
				df |= 0x40;
			lba_to_msf(-150, &m);
			cue = realloc(cue, ++ncue * sizeof (*cue));
			cp = &cue[icue++];
			fillcue(cp, ctl|0x01, i, 0, df, 0, &m);
		} else {
			scms = 0;

			if (is_scms(&trackp[i]))
				scms = 0x80;
			pgsize = trackp[i].pregapsize;
			if (pgsize == 0 && needgap)
				pgsize++;
			lba_to_msf(trackp[i].trackstart-pgsize, &m);
			cue = realloc(cue, ++ncue * sizeof (*cue));
			cp = &cue[icue++];
			fillcue(cp, ctl|0x01, i, 0, df, scms, &m);

			if (trackp[i].nindex == 1) {
				lba_to_msf(trackp[i].trackstart, &m);
				cue = realloc(cue, ++ncue * sizeof (*cue));
				cp = &cue[icue++];
				fillcue(cp, ctl|0x01, i, 1, df, scms, &m);
			} else {
				int	idx;
				long	*idxlist;

				ncue += trackp[i].nindex;
				idxlist = trackp[i].tindex;
				cue = realloc(cue, ncue * sizeof (*cue));

				for (idx = 1; idx <= trackp[i].nindex; idx++) {
					lba_to_msf(trackp[i].trackstart + idxlist[idx], &m);
					cp = &cue[icue++];
					fillcue(cp, ctl|0x01, i, idx, df, scms, &m);
				}
			}
		}
	}
	/* Lead out */
	ctl = (st2mode[trackp[tracks+1].sectype & ST_MASK]) << 4;
	if (is_copy(&trackp[i]))
		ctl |= TM_ALLOW_COPY << 4;
	if (is_quadro(&trackp[i]))
		ctl |= TM_QUADRO << 4;
	df = db2df[trackp[tracks+1].dbtype & 0x0F];
	if (trackp[i].tracktype == TOC_XA2 &&
	    trackp[i].sectype   == (SECT_MODE_2_MIX|ST_MODE_RAW)) {
		/*
		 * Hack for CUE with MODE2/CDI and
		 * trackp[i].dbtype == DB_RAW
		 */
		df = 0x21;
	}
	df &= ~7;	/* Mask off data size & nonRAW subch */
	if (df < 0x10)
		df |= 1;
	else
		df |= 4;
	lba_to_msf(trackp[tracks+1].trackstart, &m);
	cue = realloc(cue, ++ncue * sizeof (*cue));
	cp = &cue[icue++];
	fillcue(cp, ctl|0x01, 0xAA, 1, df, 0, &m);

	if (lverbose > 1) {
		for (i = 0; i < ncue; i++) {
			usal_prbytes("", (Uchar *)&cue[i], 8);
		}
	}
	if (cuep)
		*cuep = cue;
	else
		free(cue);
	return (ncue);
}

static void 
fillcue(struct mmc_cue *cp  /* The target cue entry */, 
        int ca              /* Control/adr for this entry */, 
        int tno             /* Track number for this entry */, 
        int idx             /* Index for this entry */, 
        int dataform        /* Data format for this entry */, 
        int scms            /* Serial copy management */, 
        msf_t *mp           /* MSF value for this entry */)
{
	cp->cs_ctladr = ca;		/* XXX wie lead in */
	cp->cs_tno = tno;
	cp->cs_index = idx;
	cp->cs_dataform = dataform;	/* XXX wie lead in */
	cp->cs_scms = scms;
	cp->cs_min = mp->msf_min;
	cp->cs_sec = mp->msf_sec;
	cp->cs_frame = mp->msf_frame;
}

static int 
send_cue_mmc(SCSI *usalp, cdr_t *dp, track_t *trackp)
{
	struct mmc_cue	*cp;
	int		ncue;
	int		ret;
	Uint		i;

	for (i = 1; i <= trackp->tracks; i++) {
		if (trackp[i].tracksize < (tsize_t)0) {
			errmsgno(EX_BAD, "Track %d has unknown length.\n", i);
			return (-1);
		}
	}
	ncue = (*dp->cdr_gen_cue)(trackp, &cp, FALSE);

	usalp->silent++;
	ret = send_cue_sheet(usalp, (caddr_t)cp, ncue*8);
	usalp->silent--;
	free(cp);
	if (ret < 0) {
		errmsgno(EX_BAD, "CUE sheet not accepted. Retrying with minimum pregapsize = 1.\n");
		ncue = (*dp->cdr_gen_cue)(trackp, &cp, TRUE);
		ret = send_cue_sheet(usalp, (caddr_t)cp, ncue*8);
		if (ret < 0) {
			errmsgno(EX_BAD,
			"CUE sheet still not accepted. Please try to write in RAW (-raw96r) mode.\n");
		}
		free(cp);
	}
	return (ret);
}

static int 
stats_mmc(SCSI *usalp, cdr_t *dp)
{
	Uchar mode[256];
	struct	ricoh_mode_page_30 *rp;
	UInt32_t count;

	if (mmc_isplextor(usalp) && lverbose) {
		int	sels;
		int	maxs;
		int	lasts;

		/*
		 * Run it in silent mode as old drives do not support it.
		 * As this function looks to be a part of the PowerRec
		 * features, we may want to check
		 * dp->cdr_flags & CDR_FORCESPEED
		 */
		usalp->silent++;
		if (get_speeds_plextor(usalp, &sels, &maxs, &lasts) >= 0) {
			printf("Last selected write speed: %dx\n",
						sels / 176);
			printf("Max media write speed:     %dx\n",
						maxs / 176);
			printf("Last actual write speed:   %dx\n",
						lasts / 176);
		}
		usalp->silent--;
	}

	if ((dp->cdr_dstat->ds_cdrflags & RF_BURNFREE) == 0)
		return (0);

	if (mmc_isplextor(usalp)) {
		int	i = 0;
		int	ret;

		/*
		 * Read Burn-Proof counter
		 */
		usalp->silent++;
		ret = bpc_plextor(usalp, 2, &i);
		usalp->silent--;
		if (ret < 0)
			return (-1);
		count = i;
		/*
		 * Clear Burn-Proof counter
		 */
		bpc_plextor(usalp, 1, NULL);
	} else {
		rp = get_justlink_ricoh(usalp, mode);
		if (rp)
			count = a_to_u_2_byte(rp->link_counter);
		else
			return (-1);
	}
	if (lverbose) {
		if (count == 0)
			printf("BURN-Free was never needed.\n");
		else
			printf("BURN-Free was %d times used.\n",
				(int)count);
	}
	return (0);
}
/*--------------------------------------------------------------------------*/
static BOOL 
mmc_isplextor(SCSI *usalp)
{
	if (usalp->inq != NULL &&
			strncmp(usalp->inq->vendor_info, "PLEXTOR", 7) == 0) {
		return (TRUE);
	}
	return (FALSE);
}

static BOOL 
mmc_isyamaha(SCSI *usalp)
{
	if (usalp->inq != NULL &&
			strncmp(usalp->inq->vendor_info, "YAMAHA", 6) == 0) {
		return (TRUE);
	}
	return (FALSE);
}

static void 
do_varirec_plextor(SCSI *usalp)
{
	char	*p;
	int	voff;

	p = hasdrvopt(driveropts, "varirec=");
	if (p == NULL || curspeed != 4) {
		if (check_varirec_plextor(usalp) >= 0)
			varirec_plextor(usalp, FALSE, 0);
	} else {
		if (*astoi(p, &voff) != '\0')
			comerrno(EX_BAD,
				"Bad varirec value '%s'.\n", p);
		if (check_varirec_plextor(usalp) < 0)
			comerrno(EX_BAD, "Drive does not support VariRec.\n");
		varirec_plextor(usalp, TRUE, voff);
	}
}

/*
 * GigaRec value table
 */
struct gr {
	Uchar	val;
	char	vadd;
	char	*name;
} gr[] = {
	{ 0x00,	0,  "off", },
	{ 0x00,	0,  "1.0", },
	{ 0x01,	2,  "1.2", },
	{ 0x02,	3,  "1.3", },
	{ 0x03,	4,  "1.4", },
	{ 0x81,	-2, "0.8", },
	{ 0x82,	-3, "0.7", },
	{ 0x83,	-4, "0.6", },
	{ 0x00,	0,  NULL, },
};

static int 
do_gigarec_plextor(SCSI *usalp)
{
	char	*p;
	int	val = 0;	/* Make silly GCC happy */

	p = hasdrvopt(driveropts, "gigarec=");
	if (p == NULL) {
		if (check_gigarec_plextor(usalp) >= 0)
			gigarec_plextor(usalp, 0);
	} else {
		struct gr *gp = gr;

		for (; gp->name != NULL; gp++) {
			if (streql(p, gp->name)) {
				val = gp->val;
				break;
			}
		}
		if (gp->name == NULL)
			comerrno(EX_BAD,
				"Bad gigarec value '%s'.\n", p);
		if (check_gigarec_plextor(usalp) < 0)
			comerrno(EX_BAD, "Drive does not support GigaRec.\n");
		return (gigarec_plextor(usalp, val));
	}
	return (0);
}

static int 
drivemode_plextor(SCSI *usalp, caddr_t bp, int cnt, int modecode, void *modeval)
{
	register struct	usal_cmd	*scmd = usalp->scmd;

	fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
	scmd->flags = SCG_DISRE_ENA;
	if (modeval == NULL) {
		scmd->flags |= SCG_RECV_DATA;
		scmd->addr = bp;
		scmd->size = cnt;
	} else {
		scmd->cdb.g5_cdb.res = 0x08;
	}
	scmd->cdb_len = SC_G5_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g5_cdb.cmd = 0xE9;
	scmd->cdb.g5_cdb.lun = usal_lun(usalp);
	scmd->cdb.g1_cdb.addr[0] = modecode;
	if (modeval)
		movebytes(modeval, &scmd->cdb.g1_cdb.addr[1], 6);
	else
		i_to_2_byte(&scmd->cdb.g1_cdb.count[2], cnt);

	usalp->cmdname = "plextor drive mode";

	if (usal_cmd(usalp) < 0)
		return (-1);
	return (0);
}

/*
 * #defines for drivemode_plextor()...
 */
#define	MODE_CODE_SH	0x01	/* Mode code for Single Session & Hide-CDR */
#define	MB1_SS		0x01	/* Single Session Mode			   */
#define	MB1_HIDE_CDR	0x02	/* Hide CDR Media			   */

#define	MODE_CODE_VREC	0x02	/* Mode code for Vari Rec		   */

#define	MODE_CODE_GREC	0x04	/* Mode code for Giga Rec		   */

#define	MODE_CODE_SPEED	0xbb	/* Mode code for Speed Read		   */
#define	MBbb_SPEAD_READ	0x01	/* Spead Read				   */
				/* Danach Speed auf 0xFFFF 0xFFFF setzen   */

static int 
drivemode2_plextor(SCSI *usalp, caddr_t bp, int cnt, int modecode, void *modeval)
{
	register struct	usal_cmd	*scmd = usalp->scmd;

	fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
	scmd->flags = SCG_DISRE_ENA;
	if (modeval == NULL) {
		scmd->flags |= SCG_RECV_DATA;
		scmd->addr = bp;
		scmd->size = cnt;
	} else {
		scmd->cdb.g5_cdb.res = 0x08;
	}
	scmd->cdb_len = SC_G5_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g5_cdb.cmd = 0xED;
	scmd->cdb.g5_cdb.lun = usal_lun(usalp);
	scmd->cdb.g1_cdb.addr[0] = modecode;
	if (modeval)
		scmd->cdb.g5_cdb.reladr = *(char *)modeval != 0 ? 1 : 0;
	else
		i_to_2_byte(&scmd->cdb.g1_cdb.count[1], cnt);

	usalp->cmdname = "plextor drive mode2";

	if (usal_cmd(usalp) < 0)
		return (-1);

	return (0);
}

static int 
check_varirec_plextor(SCSI *usalp)
{
	int	modecode = 2;
	Uchar	getmode[8];

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	return (0);
}

static int 
check_gigarec_plextor(SCSI *usalp)
{
	int	modecode = 4;
	Uchar	getmode[8];

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	return (0);
}

static int 
varirec_plextor(SCSI *usalp, BOOL on, int val)
{
	int	modecode = 2;
	Uchar	setmode[8];
	Uchar	getmode[8];

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));


	fillbytes(setmode, sizeof (setmode), '\0');
	setmode[0] = on?1:0;
	if (on) {
		if (val < -2 || val > 2)
			comerrno(EX_BAD, "Bad VariRec offset %d\n", val);
		printf("Turning Varirec on.\n");
		printf("Varirec offset is %d.\n", val);

		if (val > 0) {
			setmode[1] = val & 0x7F;
		} else {
			setmode[1] = (-val) & 0x7F;
			setmode[1] |= 0x80;
		}
	}

	if (drivemode_plextor(usalp, NULL, 0, modecode, setmode) < 0)
		return (-1);

	fillbytes(getmode, sizeof (getmode), '\0');
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0)
		return (-1);

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));

	return (0);
}

static int 
gigarec_plextor(SCSI *usalp, int val)
{
	int	modecode = 4;
	Uchar	setmode[8];
	Uchar	getmode[8];

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));


	fillbytes(setmode, sizeof (setmode), '\0');
	setmode[1] = val;

	if (drivemode_plextor(usalp, NULL, 0, modecode, setmode) < 0)
		return (-1);

	fillbytes(getmode, sizeof (getmode), '\0');
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0)
		return (-1);

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));

	{
		struct gr *gp = gr;

		for (; gp->name != NULL; gp++) {
			if (getmode[3] == gp->val)
				break;
		}
		if (gp->name == NULL)
			printf("Unknown GigaRec value 0x%X.\n", getmode[3]);
		else
			printf("GigaRec %sis %s.\n", gp->val?"value ":"", gp->name);
	}
	return (getmode[3]);
}

static Int32_t 
gigarec_mult(int code, Int32_t val)
{
	Int32_t	add;
	struct gr *gp = gr;

	for (; gp->name != NULL; gp++) {
		if (code == gp->val)
			break;
	}
	if (gp->vadd == 0)
		return (val);

	add = val * gp->vadd / 10;
	return (val + add);
}

static int 
check_ss_hide_plextor(SCSI *usalp)
{
	int	modecode = 1;
	Uchar	getmode[8];

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	return (getmode[2] & 0x03);
}

static int 
check_speed_rd_plextor(SCSI *usalp)
{
	int	modecode = 0xBB;
	Uchar	getmode[8];

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	return (getmode[2] & 0x01);
}

static int 
check_powerrec_plextor(SCSI *usalp)
{
	int	modecode = 0;
	Uchar	getmode[8];

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode2_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	if (getmode[2] & 1)
		return (1);

	return (0);
}

static int 
ss_hide_plextor(SCSI *usalp, BOOL do_ss, BOOL do_hide)
{
	int	modecode = 1;
	Uchar	setmode[8];
	Uchar	getmode[8];
	BOOL	is_ss;
	BOOL	is_hide;

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));


	is_ss = (getmode[2] & MB1_SS) != 0;
	is_hide = (getmode[2] & MB1_HIDE_CDR) != 0;

	if (lverbose > 0) {
		printf("Single session is %s.\n", is_ss ? "ON":"OFF");
		printf("Hide CDR is %s.\n", is_hide ? "ON":"OFF");
	}

	fillbytes(setmode, sizeof (setmode), '\0');
	setmode[0] = getmode[2];		/* Copy over old values */
	if (do_ss >= 0) {
		if (do_ss)
			setmode[0] |= MB1_SS;
		else
			setmode[0] &= ~MB1_SS;
	}
	if (do_hide >= 0) {
		if (do_hide)
			setmode[0] |= MB1_HIDE_CDR;
		else
			setmode[0] &= ~MB1_HIDE_CDR;
	}

	if (do_ss >= 0 && do_ss != is_ss)
		printf("Turning single session %s.\n", do_ss?"on":"off");
	if (do_hide >= 0 && do_hide != is_hide)
		printf("Turning hide CDR %s.\n", do_hide?"on":"off");

	if (drivemode_plextor(usalp, NULL, 0, modecode, setmode) < 0)
		return (-1);

	fillbytes(getmode, sizeof (getmode), '\0');
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0)
		return (-1);

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));

	return (0);
}

static int 
speed_rd_plextor(SCSI *usalp, BOOL do_speedrd)
{
	int	modecode = 0xBB;
	Uchar	setmode[8];
	Uchar	getmode[8];
	BOOL	is_speedrd;

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));


	is_speedrd = (getmode[2] & MBbb_SPEAD_READ) != 0;

	if (lverbose > 0)
		printf("Speed-Read is %s.\n", is_speedrd ? "ON":"OFF");

	fillbytes(setmode, sizeof (setmode), '\0');
	setmode[0] = getmode[2];		/* Copy over old values */
	if (do_speedrd >= 0) {
		if (do_speedrd)
			setmode[0] |= MBbb_SPEAD_READ;
		else
			setmode[0] &= ~MBbb_SPEAD_READ;
	}

	if (do_speedrd >= 0 && do_speedrd != is_speedrd)
		printf("Turning Speed-Read %s.\n", do_speedrd?"on":"off");

	if (drivemode_plextor(usalp, NULL, 0, modecode, setmode) < 0)
		return (-1);

	fillbytes(getmode, sizeof (getmode), '\0');
	if (drivemode_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0)
		return (-1);

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));

	/*
	 * Set current read speed to new max value.
	 */
	if (do_speedrd >= 0 && do_speedrd != is_speedrd)
		scsi_set_speed(usalp, 0xFFFF, -1, ROTCTL_CAV);

	return (0);
}

static int 
powerrec_plextor(SCSI *usalp, BOOL do_powerrec)
{
	int	modecode = 0;
	Uchar	setmode[8];
	Uchar	getmode[8];
	BOOL	is_powerrec;
	int	speed;

	fillbytes(getmode, sizeof (getmode), '\0');
	usalp->silent++;
	if (drivemode2_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0) {
		usalp->silent--;
		return (-1);
	}
	usalp->silent--;

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));


	is_powerrec = (getmode[2] & 1) != 0;

	speed = a_to_u_2_byte(&getmode[4]);

	if (lverbose > 0) {
		printf("Power-Rec is %s.\n", is_powerrec ? "ON":"OFF");
		printf("Power-Rec write speed:     %dx (recommended)\n", speed / 176);
	}

	fillbytes(setmode, sizeof (setmode), '\0');
	setmode[0] = getmode[2];		/* Copy over old values */
	if (do_powerrec >= 0) {
		if (do_powerrec)
			setmode[0] |= 1;
		else
			setmode[0] &= ~1;
	}

	if (do_powerrec >= 0 && do_powerrec != is_powerrec)
		printf("Turning Power-Rec %s.\n", do_powerrec?"on":"off");

	if (drivemode2_plextor(usalp, NULL, 0, modecode, setmode) < 0)
		return (-1);

	fillbytes(getmode, sizeof (getmode), '\0');
	if (drivemode2_plextor(usalp, (caddr_t)getmode, sizeof (getmode), modecode, NULL) < 0)
		return (-1);

	if (lverbose > 1)
		usal_prbytes("Modes", getmode, sizeof (getmode));

	return (0);
}

static int 
get_speeds_plextor(SCSI *usalp, int *selp, int *maxp, int *lastp)
{
	register struct	usal_cmd	*scmd = usalp->scmd;
	char	buf[10];
	int	i;

	fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
	fillbytes((caddr_t)buf, sizeof (buf), '\0');
	scmd->flags = SCG_DISRE_ENA;
	scmd->flags |= SCG_RECV_DATA;
	scmd->addr = buf;
	scmd->size = sizeof (buf);
	scmd->cdb_len = SC_G5_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g5_cdb.cmd = 0xEB;
	scmd->cdb.g5_cdb.lun = usal_lun(usalp);

	i_to_2_byte(&scmd->cdb.g1_cdb.count[1], sizeof (buf));

	usalp->cmdname = "plextor get speedlist";

	if (usal_cmd(usalp) < 0)
		return (-1);

	i = a_to_u_2_byte(&buf[4]);
	if (selp)
		*selp = i;

	i = a_to_u_2_byte(&buf[6]);
	if (maxp)
		*maxp = i;

	i = a_to_u_2_byte(&buf[8]);
	if (lastp)
		*lastp = i;

	return (0);
}

static int 
bpc_plextor(SCSI *usalp, int mode, int *bpp)
{
	register struct	usal_cmd	*scmd = usalp->scmd;
	char	buf[4];
	int	i;

	fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
	fillbytes((caddr_t)buf, sizeof (buf), '\0');
	scmd->flags = SCG_DISRE_ENA;
	scmd->flags |= SCG_RECV_DATA;
	scmd->addr = buf;
	scmd->size = sizeof (buf);
	scmd->cdb_len = SC_G5_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g5_cdb.cmd = 0xF5;
	scmd->cdb.g5_cdb.lun = usal_lun(usalp);

	scmd->cdb.g5_cdb.addr[1] = 0x08;
	scmd->cdb.g5_cdb.addr[2] = mode;

	i_to_2_byte(&scmd->cdb.g1_cdb.count[1], sizeof (buf));

	usalp->cmdname = "plextor read bpc";

	if (usal_cmd(usalp) < 0)
		return (-1);

	if (usal_getresid(usalp) > 2)
		return (0);

	i = a_to_u_2_byte(buf);
	if (bpp)
		*bpp = i;

	return (0);
}

static int 
set_audiomaster_yamaha(SCSI *usalp, cdr_t *dp, BOOL keep_mode)
{
	Uchar	mode[0x100];
	int	len;
	int	ret = 0;
	struct	cd_mode_page_05 *mp;

	if (xdebug && !keep_mode)
		printf("Checking for Yamaha Audio Master feature: ");

	/*
	 * Do not reset mp->test_write (-dummy) here.
	 */
	deflt_writemodes_mmc(usalp, FALSE);

	fillbytes((caddr_t)mode, sizeof (mode), '\0');

	usalp->silent++;
	if (!get_mode_params(usalp, 0x05, "CD write parameter",
			mode, (Uchar *)0, (Uchar *)0, (Uchar *)0, &len)) {
		usalp->silent--;
		return (-1);
	}
	if (len == 0) {
		usalp->silent--;
		return (-1);
	}

	mp = (struct cd_mode_page_05 *)
		(mode + sizeof (struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);
#ifdef	DEBUG
	usal_prbytes("CD write parameter:", (Uchar *)mode, len);
#endif

	/*
	 * Do not set mp->test_write (-dummy) here. It should be set
	 * only at one place and only one time.
	 */
	mp->BUFE = 0;

	mp->write_type = 8;
	mp->track_mode = 0;
	mp->dbtype = DB_RAW;

	if (!set_mode_params(usalp, "CD write parameter", mode, len, 0, -1))
		ret = -1;

	/*
	 * Do not reset mp->test_write (-dummy) here.
	 */
	if (!keep_mode || ret < 0)
		deflt_writemodes_mmc(usalp, FALSE);
	usalp->silent--;

	return (ret);
}

struct 
ricoh_mode_page_30 *get_justlink_ricoh(SCSI *usalp, Uchar *mode)
{
	Uchar	modec[0x100];
	int	len;
	struct	ricoh_mode_page_30 *mp;

	usalp->silent++;
	if (!get_mode_params(usalp, 0x30, "Ricoh Vendor Page", mode, modec, NULL, NULL, &len)) {
		usalp->silent--;
		return ((struct ricoh_mode_page_30 *)0);
	}
	usalp->silent--;

	/*
	 * SCSI mode header + 6 bytes mode page 30.
	 * This is including the Burn-Free counter.
	 */
	if (len < 10)
		return ((struct ricoh_mode_page_30 *)0);

	if (xdebug) {
		fprintf(stderr, "Mode len: %d\n", len);
		usal_prbytes("Mode Sense Data ", mode, len);
		usal_prbytes("Mode Sence CData", modec, len);
	}

	mp = (struct ricoh_mode_page_30 *)
		(mode + sizeof (struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);

	/*
	 * 6 bytes mode page 30.
	 * This is including the Burn-Free counter.
	 */
	if ((len - ((Uchar *)mp - mode) -1) < 5)
		return ((struct ricoh_mode_page_30 *)0);

	if (xdebug) {
		fprintf(stderr, "Burnfree counter: %d\n", a_to_u_2_byte(mp->link_counter));
	}
	return (mp);
}

static int 
force_speed_yamaha(SCSI *usalp, int readspeed, int writespeed)
{
	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[11] = 0x80;

	usalp->cmdname = "yamaha force cd speed";

	if (usal_cmd(usalp) < 0)
		return (-1);
	return (0);
}

static BOOL
get_tattoo_yamaha(SCSI *usalp, BOOL print, Int32_t *irp, Int32_t *orp)
{
	Uchar	mode[0x100];
	int	len;
	UInt32_t ival;
	UInt32_t oval;
	Uchar	*mp;

	usalp->silent++;
	if (!get_mode_params(usalp, 0x31, "Yamaha Tattoo Page", mode, NULL, NULL, NULL, &len)) {
		usalp->silent--;
		return (FALSE);
	}
	usalp->silent--;

	/*
	 * SCSI mode header + 16 bytes mode page 31.
	 * This is including the Burn-Free counter.
	 */
	if (len < 20)
		return (FALSE);

	mp = (Uchar *)
		(mode + sizeof (struct scsi_mode_header) +
		((struct scsi_mode_header *)mode)->blockdesc_len);

	/*
	 * 10 bytes mode page 31.
	 * This is including the Burn-Free counter.
	 */
	if ((len - ((Uchar *)mp - mode) -1) < 10)
		return (FALSE);

	ival = a_to_u_3_byte(&mp[4]);
	oval = a_to_u_3_byte(&mp[7]);

	if (irp)
		*irp = ival;
	if (orp)
		*orp = oval;

	if (print && ival > 0 && oval > 0) {
		printf("DiskT@2 inner r: %d\n", (int)ival);
		printf("DiskT@2 outer r: %d\n", (int)oval);
		printf("DiskT@2 image size: 3744 x %d pixel.\n",
						(int)(oval-ival)+1);
	}

	return (TRUE);
}

static int 
do_tattoo_yamaha(SCSI *usalp, FILE *f)
{
	Int32_t ival = 0;
	Int32_t oval = 0;
	Int32_t	lines;
	off_t	fsize;
	char	*buf = usalp->bufptr;
	long	bufsize = usalp->maxbuf;
	long	nsecs;
	long	amt;

	nsecs = bufsize / 2048;
	bufsize = nsecs * 2048;

	if (!get_tattoo_yamaha(usalp, FALSE, &ival, &oval)) {
		errmsgno(EX_BAD, "Cannot get DiskT@2 info.\n");
		return (-1);
	}

	if (ival == 0 || oval == 0) {
		errmsgno(EX_BAD, "DiskT@2 info not valid.\n");
		return (-1);
	}

	lines = oval - ival + 1;
	fsize = filesize(f);
	if ((fsize % 3744) != 0 || fsize < (lines*3744)) {
		errmsgno(EX_BAD, "Illegal DiskT@2 file size.\n");
		return (-1);
	}
	if (fsize > (lines*3744))
		fsize = lines*3744;

	if (lverbose)
		printf("Starting to write DiskT@2 data.\n");
	fillbytes(buf, bufsize, '\0');
	if ((amt = fileread(f, buf, bufsize)) <= 0) {
		errmsg("DiskT@2 file read error.\n");
		return (-1);
	}

	if (yamaha_write_buffer(usalp, 1, 0, ival, amt/2048, buf, amt) < 0) {
		errmsgno(EX_BAD, "DiskT@2 1st write error.\n");
		return (-1);
	}
	amt = (amt+2047) / 2048 * 2048;
	fsize -= amt;

	while (fsize > 0) {
		fillbytes(buf, bufsize, '\0');
		if ((amt = fileread(f, buf, bufsize)) <= 0) {
			errmsg("DiskT@2 file read error.\n");
			return (-1);
		}
		amt = (amt+2047) / 2048 * 2048;
		fsize -= amt;
		if (yamaha_write_buffer(usalp, 1, 0, 0, amt/2048, buf, amt) < 0) {
			errmsgno(EX_BAD, "DiskT@2 write error.\n");
			return (-1);
		}
	}

	if (yamaha_write_buffer(usalp, 1, 0, oval, 0, buf, 0) < 0) {
		errmsgno(EX_BAD, "DiskT@2 final error.\n");
		return (-1);
	}

	wait_unit_ready(usalp, 1000);	/* Wait for DiskT@2 */
	waitfix_mmc(usalp, 1000);	/* Wait for DiskT@2 */

	return (0);
}

/*
 * Yamaha specific version of 'write buffer' that offers an additional
 * Parameter Length 'parlen' parameter.
 */
static int 
yamaha_write_buffer(SCSI *usalp, int mode, int bufferid, long offset,
                    long parlen, void *buffer, long buflen)
{
	register struct	usal_cmd	*scmd = usalp->scmd;
		Uchar	*CDB;

	fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
	scmd->addr = buffer;
	scmd->size = buflen;
	scmd->flags = SCG_DISRE_ENA;
	scmd->cdb_len = SC_G1_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g1_cdb.cmd = 0x3B;

	CDB    = (Uchar *)scmd->cdb.cmd_cdb;
	CDB[1] = mode & 7;
	CDB[2] = bufferid;
	i_to_3_byte(&CDB[3], offset);
	i_to_3_byte(&CDB[6], parlen);

	usalp->cmdname = "write_buffer";

	if (usal_cmd(usalp) >= 0)
		return (1);
	return (0);
}

static int 
dvd_dual_layer_split(SCSI *usalp, cdr_t *dp, long tsize)
{
    unsigned char	xb[12];
    long 	l0_size;
    
    /* Get the Layer 0 defined data zone*/
    if (read_dvd_structure(usalp, (caddr_t)xb, 12, 0, 0, 0x20) >= 0) {
	if ((xb[1] | xb[0] << 8) < 13) {
	    fprintf(stderr, "dvd_dual_layer_split: read_dvd_structure returns invalid data\n");
	    return 1;
	}
	if (xb[4] & 0x80) {
	    printf("L0 zone size already set\n");
	    return 1;
	}
	l0_size = xb[11] | xb[10] << 8 | xb[9] << 16 | xb[8] << 24;
	if (tsize < l0_size) {
	    fprintf(stderr, "track size smaller than one layer, use --force to force burning.");
	    return 0;
	}
	printf("L0 size: %ld (track size %ld)\n", l0_size, tsize);
	l0_size = tsize / 2;
	l0_size = l0_size - 1 + 16 - (l0_size - 1) % 16;
	printf("New L0 size: %ld\n", l0_size);

	memset (xb, 0, sizeof(xb));
	xb[1]  = sizeof(xb) - 2;
	xb[8]  = l0_size >> 24;
	xb[9]  = l0_size >> 16;
	xb[10] = l0_size >> 8;
	xb[11] = l0_size;
	if (send_dvd_structure(usalp, (caddr_t)xb, 12, 0, 0x20)) {
	    fprintf(stderr, "dvd_dual_layer_split: send_dvd_structure failed, could not set middle zone location.\n");
	    return 0;
	}
    }
   return 1;
}