Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
/*
* Sony CDU-31A CDROM interface device driver.
*
* Corey Minyard (minyard@wf-rch.cirr.com)
*
* Colossians 3:17
*
*  See Documentation/cdrom/cdu31a for additional details about this driver.
* 
* The Sony interface device driver handles Sony interface CDROM
* drives and provides a complete block-level interface as well as an
* ioctl() interface compatible with the Sun (as specified in
* include/linux/cdrom.h).  With this interface, CDROMs can be
* accessed and standard audio CDs can be played back normally.
*
* WARNING - 	All autoprobes have been removed from the driver.
*		You MUST configure the CDU31A via a LILO config
*		at boot time or in lilo.conf.  I have the
*		following in my lilo.conf:
*
*                append="cdu31a=0x1f88,0,PAS"
*
*		The first number is the I/O base address of the
*		card.  The second is the interrupt (0 means none).
 *		The third should be "PAS" if on a Pro-Audio
 *		spectrum, or nothing if on something else.
 *
 * This interface is (unfortunately) a polled interface.  This is
 * because most Sony interfaces are set up with DMA and interrupts
 * disables.  Some (like mine) do not even have the capability to
 * handle interrupts or DMA.  For this reason you will see a lot of
 * the following:
 *
 *   retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
 *   while (time_before(jiffies, retry_count) && (! <some condition to wait for))
 *   {
 *      while (handle_sony_cd_attention())
 *         ;
 *
 *      sony_sleep();
 *   }
 *   if (the condition not met)
 *   {
 *      return an error;
 *   }
 *
 * This ugly hack waits for something to happen, sleeping a little
 * between every try.  it also handles attentions, which are
 * asynchronous events from the drive informing the driver that a disk
 * has been inserted, removed, etc.
 *
 * NEWS FLASH - The driver now supports interrupts but they are
 * turned off by default.  Use of interrupts is highly encouraged, it
 * cuts CPU usage down to a reasonable level.  I had DMA in for a while
 * but PC DMA is just too slow.  Better to just insb() it.
 *
 * One thing about these drives: They talk in MSF (Minute Second Frame) format.
 * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
 * disk.  The funny thing is that these are sent to the drive in BCD, but the
 * interface wants to see them in decimal.  A lot of conversion goes on.
 *
 * DRIVER SPECIAL FEATURES
 * -----------------------
 *
 * This section describes features beyond the normal audio and CD-ROM
 * functions of the drive.
 *
 * 2048 byte buffer mode
 *
 * If a disk is mounted with -o block=2048, data is copied straight
 * from the drive data port to the buffer.  Otherwise, the readahead
 * buffer must be involved to hold the other 1K of data when a 1K
 * block operation is done.  Note that with 2048 byte blocks you
 * cannot execute files from the CD.
 *
 * XA compatibility
 *
 * The driver should support XA disks for both the CDU31A and CDU33A.
 * It does this transparently, the using program doesn't need to set it.
 *
 * Multi-Session
 *
 * A multi-session disk looks just like a normal disk to the user.
 * Just mount one normally, and all the data should be there.
 * A special thanks to Koen for help with this!
 * 
 * Raw sector I/O
 *
 * Using the CDROMREADAUDIO it is possible to read raw audio and data
 * tracks.  Both operations return 2352 bytes per sector.  On the data
 * tracks, the first 12 bytes is not returned by the drive and the value
 * of that data is indeterminate.
 *
 *
 *  Copyright (C) 1993  Corey Minyard
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * TODO: 
 *       CDs with form1 and form2 sectors cause problems
 *       with current read-ahead strategy.
 *
 * Credits:
 *    Heiko Eissfeldt <heiko@colossus.escape.de>
 *         For finding abug in the return of the track numbers.
 *         TOC processing redone for proper multisession support.
 *
 *
 *  It probably a little late to be adding a history, but I guess I
 *  will start.
 *
 *  10/24/95 - Added support for disabling the eject button when the
 *             drive is open.  Note that there is a small problem
 *             still here, if the eject button is pushed while the
 *             drive light is flashing, the drive will return a bad
 *             status and be reset.  It recovers, though.
 *
 *  03/07/97 - Fixed a problem with timers.
 *
 *
 *  18 Spetember 1997 -- Ported to Uniform CD-ROM driver by 
 *                 Heiko Eissfeldt <heiko@colossus.escape.de> with additional
 *                 changes by Erik Andersen <andersee@debian.org>
 *
 *  24 January 1998 -- Removed the scd_disc_status() function, which was now
 *                     just dead code left over from the port.
 *                          Erik Andersen <andersee@debian.org>
 *
 *  16 July 1998 -- Drive donated to Erik Andersen by John Kodis
 *                   <kodis@jagunet.com>.  Work begun on fixing driver to
 *                   work under 2.1.X.  Added temporary extra printks
 *                   which seem to slow it down enough to work.
 *
 *  9 November 1999 -- Make kernel-parameter implementation work with 2.3.x 
 *	               Removed init_module & cleanup_module in favor of 
 *		       module_init & module_exit.
 *		       Torben Mathiasen <tmm@image.dk>
*/

#include <linux/major.h>

#include <linux/module.h>

#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/hdreg.h>
#include <linux/genhd.h>
#include <linux/ioport.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/interrupt.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/dma.h>

#include <linux/cdrom.h>
#include "cdu31a.h"

#define MAJOR_NR CDU31A_CDROM_MAJOR
#include <linux/blk.h>

#define CDU31A_READAHEAD 4	/* 128 sector, 64kB, 32 reads read-ahead */
#define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10

#define DEBUG 0

/* Define the following if you have data corruption problems. */
#undef SONY_POLL_EACH_BYTE

/*
** Edit the following data to change interrupts, DMA channels, etc.
** Default is polled and no DMA.  DMA is not recommended for double-speed
** drives.
*/
static struct {
	unsigned short base;	/* I/O Base Address */
	short int_num;		/* Interrupt Number (-1 means scan for it,
				   0 means don't use) */
} cdu31a_addresses[] __initdata = {
	{0}
};

static int handle_sony_cd_attention(void);
static int read_subcode(void);
static void sony_get_toc(void);
static int scd_spinup(void);
/*static int scd_open(struct inode *inode, struct file *filp);*/
static int scd_open(struct cdrom_device_info *, int);
static void do_sony_cd_cmd(unsigned char cmd,
			   unsigned char *params,
			   unsigned int num_params,
			   unsigned char *result_buffer,
			   unsigned int *result_size);
static void size_to_buf(unsigned int size, unsigned char *buf);

/* Parameters for the read-ahead. */
static unsigned int sony_next_block;	/* Next 512 byte block offset */
static unsigned int sony_blocks_left = 0;	/* Number of 512 byte blocks left
						   in the current read command. */


/* The base I/O address of the Sony Interface.  This is a variable (not a
   #define) so it can be easily changed via some future ioctl() */
static unsigned int cdu31a_port = 0;
MODULE_PARM(cdu31a_port, "i");

/*
 * The following are I/O addresses of the various registers for the drive.  The
 * comment for the base address also applies here.
 */
static volatile unsigned short sony_cd_cmd_reg;
static volatile unsigned short sony_cd_param_reg;
static volatile unsigned short sony_cd_write_reg;
static volatile unsigned short sony_cd_control_reg;
static volatile unsigned short sony_cd_status_reg;
static volatile unsigned short sony_cd_result_reg;
static volatile unsigned short sony_cd_read_reg;
static volatile unsigned short sony_cd_fifost_reg;


static int sony_spun_up = 0;	/* Has the drive been spun up? */

static int sony_speed = 0;	/* Last wanted speed */

static int sony_xa_mode = 0;	/* Is an XA disk in the drive
				   and the drive a CDU31A? */

static int sony_raw_data_mode = 1;	/* 1 if data tracks, 0 if audio.
					   For raw data reads. */

static unsigned int sony_usage = 0;	/* How many processes have the
					   drive open. */

static int sony_pas_init = 0;	/* Initialize the Pro-Audio
				   Spectrum card? */

static struct s_sony_session_toc single_toc;	/* Holds the
						   table of
						   contents. */

static struct s_all_sessions_toc sony_toc;	/* entries gathered from all
						   sessions */

static int sony_toc_read = 0;	/* Has the TOC been read for
				   the drive? */

static struct s_sony_subcode last_sony_subcode;	/* Points to the last
						   subcode address read */

static volatile int sony_inuse = 0;	/* Is the drive in use?  Only one operation
					   at a time allowed */

static DECLARE_WAIT_QUEUE_HEAD(sony_wait);	/* Things waiting for the drive */

static struct task_struct *has_cd_task = NULL;	/* The task that is currently
						   using the CDROM drive, or
						   NULL if none. */

static int is_double_speed = 0;	/* does the drive support double speed ? */
static int is_a_cdu31a = 1;	/* Is the drive a CDU31A? */

static int is_auto_eject = 1;	/* Door has been locked? 1=No/0=Yes */

/*
 * The audio status uses the values from read subchannel data as specified
 * in include/linux/cdrom.h.
 */
static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;

/*
 * The following are a hack for pausing and resuming audio play.  The drive
 * does not work as I would expect it, if you stop it then start it again,
 * the drive seeks back to the beginning and starts over.  This holds the
 * position during a pause so a resume can restart it.  It uses the
 * audio status variable above to tell if it is paused.
 */
static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };

/* What IRQ is the drive using?  0 if none. */
static int cdu31a_irq = 0;
MODULE_PARM(cdu31a_irq, "i");

/* The interrupt handler will wake this queue up when it gets an
   interrupts. */
DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait);

static int curr_control_reg = 0;	/* Current value of the control register */

/* A disk changed variable.  When a disk change is detected, it will
   all be set to TRUE.  As the upper layers ask for disk_changed status
   it will be cleared. */
static char disk_changed;

/* Variable for using the readahead buffer.  The readahead buffer
   is used for raw sector reads and for blocksizes that are smaller
   than 2048 bytes. */
static char readahead_buffer[CD_FRAMESIZE_RAW];
static int readahead_dataleft = 0;
static int readahead_bad = 0;

/* Used to time a short period to abort an operation after the
   drive has been idle for a while.  This keeps the light on
   the drive from flashing for very long. */
static struct timer_list cdu31a_abort_timer;

/* Marks if the timeout has started an abort read.  This is used
   on entry to the drive to tell the code to read out the status
   from the abort read. */
static int abort_read_started = 0;


/*
 * This routine returns 1 if the disk has been changed since the last
 * check or 0 if it hasn't.
 */
static int scd_disk_change(kdev_t full_dev)
{
	int retval;

	retval = disk_changed;
	disk_changed = 0;

	return retval;
}

/*
 * Uniform cdrom interface function
 * report back, if disc has changed from time of last request.
 */
static int scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
{
	return scd_disk_change(cdi->dev);
}

/*
 * Uniform cdrom interface function
 * report back, if drive is ready
 */
static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
{
	if (CDSL_CURRENT != slot_nr) {
		/* we have no changer support */
		return -EINVAL;
	}
	if (scd_spinup() == 0) {
		sony_spun_up = 1;
	}
	return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;
}

static inline void enable_interrupts(void)
{
	curr_control_reg |= (SONY_ATTN_INT_EN_BIT
			     | SONY_RES_RDY_INT_EN_BIT
			     | SONY_DATA_RDY_INT_EN_BIT);
	outb(curr_control_reg, sony_cd_control_reg);
}

static inline void disable_interrupts(void)
{
	curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
			      | SONY_RES_RDY_INT_EN_BIT
			      | SONY_DATA_RDY_INT_EN_BIT);
	outb(curr_control_reg, sony_cd_control_reg);
}

/*
 * Wait a little while (used for polling the drive).  If in initialization,
 * setting a timeout doesn't work, so just loop for a while.
 */
static inline void sony_sleep(void)
{
	unsigned long flags;

	if (cdu31a_irq <= 0) {
		current->state = TASK_INTERRUPTIBLE;
		schedule_timeout(0);
	} else {		/* Interrupt driven */

		save_flags(flags);
		cli();
		enable_interrupts();
		interruptible_sleep_on(&cdu31a_irq_wait);
		restore_flags(flags);
	}
}


/*
 * The following are convenience routine to read various status and set
 * various conditions in the drive.
 */
static inline int is_attention(void)
{
	return ((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
}

static inline int is_busy(void)
{
	return ((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
}

static inline int is_data_ready(void)
{
	return ((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
}

static inline int is_data_requested(void)
{
	return ((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
}

static inline int is_result_ready(void)
{
	return ((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
}

static inline int is_param_write_rdy(void)
{
	return ((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
}

static inline int is_result_reg_not_empty(void)
{
	return ((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0);
}

static inline void reset_drive(void)
{
	curr_control_reg = 0;
	readahead_dataleft = 0;
	sony_toc_read = 0;
	outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
}

/*
 * Uniform cdrom interface function
 * reset drive and return when it is ready
 */
static int scd_reset(struct cdrom_device_info *cdi)
{
	int retry_count;

	reset_drive();

	retry_count = jiffies + SONY_RESET_TIMEOUT;
	while (time_before(jiffies, retry_count) && (!is_attention())) {
		sony_sleep();
	}

	return 0;
}

static inline void clear_attention(void)
{
	outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
}

static inline void clear_result_ready(void)
{
	outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
}

static inline void clear_data_ready(void)
{
	outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT,
	     sony_cd_control_reg);
}

static inline void clear_param_reg(void)
{
	outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
}

static inline unsigned char read_status_register(void)
{
	return (inb(sony_cd_status_reg));
}

static inline unsigned char read_result_register(void)
{
	return (inb(sony_cd_result_reg));
}

static inline unsigned char read_data_register(void)
{
	return (inb(sony_cd_read_reg));
}

static inline void write_param(unsigned char param)
{
	outb(param, sony_cd_param_reg);
}

static inline void write_cmd(unsigned char cmd)
{
	outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT,
	     sony_cd_control_reg);
	outb(cmd, sony_cd_cmd_reg);
}

static void cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	unsigned char val;

	if (abort_read_started) {
		/* We might be waiting for an abort to finish.  Don't
		   disable interrupts yet, though, because we handle
		   this one here. */
		/* Clear out the result registers. */
		while (is_result_reg_not_empty()) {
			val = read_result_register();
		}
		clear_data_ready();
		clear_result_ready();

		/* Clear out the data */
		while (is_data_requested()) {
			val = read_data_register();
		}
		abort_read_started = 0;

		/* If something was waiting, wake it up now. */
		if (waitqueue_active(&cdu31a_irq_wait)) {
			disable_interrupts();
			wake_up(&cdu31a_irq_wait);
		}
	} else if (waitqueue_active(&cdu31a_irq_wait)) {
		disable_interrupts();
		wake_up(&cdu31a_irq_wait);
	} else {
		disable_interrupts();
		printk
		    ("CDU31A: Got an interrupt but nothing was waiting\n");
	}
}

/*
 * give more verbose error messages
 */
static unsigned char *translate_error(unsigned char err_code)
{
	static unsigned char errbuf[80];

	switch (err_code) {
	case 0x10:
		return "illegal command ";
	case 0x11:
		return "illegal parameter ";

	case 0x20:
		return "not loaded ";
	case 0x21:
		return "no disc ";
	case 0x22:
		return "not spinning ";
	case 0x23:
		return "spinning ";
	case 0x25:
		return "spindle servo ";
	case 0x26:
		return "focus servo ";
	case 0x29:
		return "eject mechanism ";
	case 0x2a:
		return "audio playing ";
	case 0x2c:
		return "emergency eject ";

	case 0x30:
		return "focus ";
	case 0x31:
		return "frame sync ";
	case 0x32:
		return "subcode address ";
	case 0x33:
		return "block sync ";
	case 0x34:
		return "header address ";

	case 0x40:
		return "illegal track read ";
	case 0x41:
		return "mode 0 read ";
	case 0x42:
		return "illegal mode read ";
	case 0x43:
		return "illegal block size read ";
	case 0x44:
		return "mode read ";
	case 0x45:
		return "form read ";
	case 0x46:
		return "leadout read ";
	case 0x47:
		return "buffer overrun ";

	case 0x53:
		return "unrecoverable CIRC ";
	case 0x57:
		return "unrecoverable LECC ";

	case 0x60:
		return "no TOC ";
	case 0x61:
		return "invalid subcode data ";
	case 0x63:
		return "focus on TOC read ";
	case 0x64:
		return "frame sync on TOC read ";
	case 0x65:
		return "TOC data ";

	case 0x70:
		return "hardware failure ";
	case 0x91:
		return "leadin ";
	case 0x92:
		return "leadout ";
	case 0x93:
		return "data track ";
	}
	sprintf(errbuf, "unknown 0x%02x ", err_code);
	return errbuf;
}

/*
 * Set the drive parameters so the drive will auto-spin-up when a
 * disk is inserted.
 */
static void set_drive_params(int want_doublespeed)
{
	unsigned char res_reg[12];
	unsigned int res_size;
	unsigned char params[3];


	params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
	params[1] = 0x00;	/* Never spin down the drive. */
	do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
		       params, 2, res_reg, &res_size);
	if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
		printk("  Unable to set spin-down time: 0x%2.2x\n",
		       res_reg[1]);
	}

	params[0] = SONY_SD_MECH_CONTROL;
	params[1] = SONY_AUTO_SPIN_UP_BIT;	/* Set auto spin up */

	if (is_auto_eject)
		params[1] |= SONY_AUTO_EJECT_BIT;

	if (is_double_speed && want_doublespeed) {
		params[1] |= SONY_DOUBLE_SPEED_BIT;	/* Set the drive to double speed if 
							   possible */
	}
	do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
		       params, 2, res_reg, &res_size);
	if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
		printk("  Unable to set mechanical parameters: 0x%2.2x\n",
		       res_reg[1]);
	}
}

/*
 * Uniform cdrom interface function
 * select reading speed for data access
 */
static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
{
	if (speed == 0)
		sony_speed = 1;
	else
		sony_speed = speed - 1;

	set_drive_params(sony_speed);
	return 0;
}

/*
 * Uniform cdrom interface function
 * lock or unlock eject button
 */
static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
{
	if (lock == 0 && sony_usage == 1) {
		/* Unlock the door, only if nobody is using the drive */
		is_auto_eject = 1;
	} else {
		is_auto_eject = 0;
	}
	set_drive_params(sony_speed);
	return 0;
}

/*
 * This code will reset the drive and attempt to restore sane parameters.
 */
static void restart_on_error(void)
{
	unsigned char res_reg[12];
	unsigned int res_size;
	unsigned int retry_count;


	printk("cdu31a: Resetting drive on error\n");
	reset_drive();
	retry_count = jiffies + SONY_RESET_TIMEOUT;
	while (time_before(jiffies, retry_count) && (!is_attention())) {
		sony_sleep();
	}
	set_drive_params(sony_speed);
	do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
	if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
		printk("cdu31a: Unable to spin up drive: 0x%2.2x\n",
		       res_reg[1]);
	}

	current->state = TASK_INTERRUPTIBLE;
	schedule_timeout(2 * HZ);

	sony_get_toc();
}

/*
 * This routine writes data to the parameter register.  Since this should
 * happen fairly fast, it is polled with no OS waits between.
 */
static int write_params(unsigned char *params, int num_params)
{
	unsigned int retry_count;


	retry_count = SONY_READY_RETRIES;
	while ((retry_count > 0) && (!is_param_write_rdy())) {
		retry_count--;
	}
	if (!is_param_write_rdy()) {
		return -EIO;
	}

	while (num_params > 0) {
		write_param(*params);
		params++;
		num_params--;
	}

	return 0;
}


/*
 * The following reads data from the command result register.  It is a
 * fairly complex routine, all status info flows back through this
 * interface.  The algorithm is stolen directly from the flowcharts in
 * the drive manual.
 */
static void
get_result(unsigned char *result_buffer, unsigned int *result_size)
{
	unsigned char a, b;
	int i;
	unsigned int retry_count;


	while (handle_sony_cd_attention());
	/* Wait for the result data to be ready */
	retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
	while (time_before(jiffies, retry_count)
	       && (is_busy() || (!(is_result_ready())))) {
		sony_sleep();

		while (handle_sony_cd_attention());
	}
	if (is_busy() || (!(is_result_ready()))) {
#if DEBUG
		printk("CDU31A timeout out %d\n", __LINE__);
#endif
		result_buffer[0] = 0x20;
		result_buffer[1] = SONY_TIMEOUT_OP_ERR;
		*result_size = 2;
		return;
	}

	/*
	 * Get the first two bytes.  This determines what else needs
	 * to be done.
	 */
	clear_result_ready();
	a = read_result_register();
	*result_buffer = a;
	result_buffer++;

	/* Check for block error status result. */
	if ((a & 0xf0) == 0x50) {
		*result_size = 1;
		return;
	}

	b = read_result_register();
	*result_buffer = b;
	result_buffer++;
	*result_size = 2;

	/*
	 * 0x20 means an error occurred.  Byte 2 will have the error code.
	 * Otherwise, the command succeeded, byte 2 will have the count of
	 * how many more status bytes are coming.
	 *
	 * The result register can be read 10 bytes at a time, a wait for
	 * result ready to be asserted must be done between every 10 bytes.
	 */
	if ((a & 0xf0) != 0x20) {
		if (b > 8) {
			for (i = 0; i < 8; i++) {
				*result_buffer = read_result_register();
				result_buffer++;
				(*result_size)++;
			}
			b = b - 8;

			while (b > 10) {
				retry_count = SONY_READY_RETRIES;
				while ((retry_count > 0)
				       && (!is_result_ready())) {
					retry_count--;
				}
				if (!is_result_ready()) {
#if DEBUG
					printk("CDU31A timeout out %d\n",
					       __LINE__);
#endif
					result_buffer[0] = 0x20;
					result_buffer[1] =
					    SONY_TIMEOUT_OP_ERR;
					*result_size = 2;
					return;
				}

				clear_result_ready();

				for (i = 0; i < 10; i++) {
					*result_buffer =
					    read_result_register();
					result_buffer++;
					(*result_size)++;
				}
				b = b - 10;
			}

			if (b > 0) {
				retry_count = SONY_READY_RETRIES;
				while ((retry_count > 0)
				       && (!is_result_ready())) {
					retry_count--;
				}
				if (!is_result_ready()) {
#if DEBUG
					printk("CDU31A timeout out %d\n",
					       __LINE__);
#endif
					result_buffer[0] = 0x20;
					result_buffer[1] =
					    SONY_TIMEOUT_OP_ERR;
					*result_size = 2;
					return;
				}
			}
		}

		while (b > 0) {
			*result_buffer = read_result_register();
			result_buffer++;
			(*result_size)++;
			b--;
		}
	}
}

/*
 * Do a command that does not involve data transfer.  This routine must
 * be re-entrant from the same task to support being called from the
 * data operation code when an error occurs.
 */
static void
do_sony_cd_cmd(unsigned char cmd,
	       unsigned char *params,
	       unsigned int num_params,
	       unsigned char *result_buffer, unsigned int *result_size)
{
	unsigned int retry_count;
	int num_retries;
	int recursive_call;
	unsigned long flags;


	save_flags(flags);
	cli();
	if (current != has_cd_task) {	/* Allow recursive calls to this routine */
		while (sony_inuse) {
			interruptible_sleep_on(&sony_wait);
			if (signal_pending(current)) {
				result_buffer[0] = 0x20;
				result_buffer[1] = SONY_SIGNAL_OP_ERR;
				*result_size = 2;
				restore_flags(flags);
				return;
			}
		}
		sony_inuse = 1;
		has_cd_task = current;
		recursive_call = 0;
	} else {
		recursive_call = 1;
	}

	num_retries = 0;
retry_cd_operation:

	while (handle_sony_cd_attention());

	sti();

	retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
	while (time_before(jiffies, retry_count) && (is_busy())) {
		sony_sleep();

		while (handle_sony_cd_attention());
	}
	if (is_busy()) {
#if DEBUG
		printk("CDU31A timeout out %d\n", __LINE__);
#endif
		result_buffer[0] = 0x20;
		result_buffer[1] = SONY_TIMEOUT_OP_ERR;
		*result_size = 2;
	} else {
		clear_result_ready();
		clear_param_reg();

		write_params(params, num_params);
		write_cmd(cmd);

		get_result(result_buffer, result_size);
	}

	if (((result_buffer[0] & 0xf0) == 0x20)
	    && (num_retries < MAX_CDU31A_RETRIES)) {
		num_retries++;
		current->state = TASK_INTERRUPTIBLE;
		schedule_timeout(HZ / 10);	/* Wait .1 seconds on retries */
		goto retry_cd_operation;
	}

	if (!recursive_call) {
		has_cd_task = NULL;
		sony_inuse = 0;
		wake_up_interruptible(&sony_wait);
	}

	restore_flags(flags);
}


/*
 * Handle an attention from the drive.  This will return 1 if it found one
 * or 0 if not (if one is found, the caller might want to call again).
 *
 * This routine counts the number of consecutive times it is called
 * (since this is always called from a while loop until it returns
 * a 0), and returns a 0 if it happens too many times.  This will help
 * prevent a lockup.
 */
static int handle_sony_cd_attention(void)
{
	unsigned char atten_code;
	static int num_consecutive_attentions = 0;
	volatile int val;


#if 0*DEBUG
	printk("Entering handle_sony_cd_attention\n");
#endif
	if (is_attention()) {
		if (num_consecutive_attentions >
		    CDU31A_MAX_CONSECUTIVE_ATTENTIONS) {
			printk
			    ("cdu31a: Too many consecutive attentions: %d\n",
			     num_consecutive_attentions);
			num_consecutive_attentions = 0;
#if DEBUG
			printk("Leaving handle_sony_cd_attention at %d\n",
			       __LINE__);
#endif
			return (0);
		}

		clear_attention();
		atten_code = read_result_register();

		switch (atten_code) {
			/* Someone changed the CD.  Mark it as changed */
		case SONY_MECH_LOADED_ATTN:
			disk_changed = 1;
			sony_toc_read = 0;
			sony_audio_status = CDROM_AUDIO_NO_STATUS;
			sony_blocks_left = 0;
			break;

		case SONY_SPIN_DOWN_COMPLETE_ATTN:
			/* Mark the disk as spun down. */
			sony_spun_up = 0;
			break;

		case SONY_AUDIO_PLAY_DONE_ATTN:
			sony_audio_status = CDROM_AUDIO_COMPLETED;
			read_subcode();
			break;

		case SONY_EJECT_PUSHED_ATTN:
			if (is_auto_eject) {
				sony_audio_status = CDROM_AUDIO_INVALID;
			}
			break;

		case SONY_LEAD_IN_ERR_ATTN:
		case SONY_LEAD_OUT_ERR_ATTN:
		case SONY_DATA_TRACK_ERR_ATTN:
		case SONY_AUDIO_PLAYBACK_ERR_ATTN:
			sony_audio_status = CDROM_AUDIO_ERROR;
			break;
		}

		num_consecutive_attentions++;
#if DEBUG
		printk("Leaving handle_sony_cd_attention at %d\n",
		       __LINE__);
#endif
		return (1);
	} else if (abort_read_started) {
		while (is_result_reg_not_empty()) {
			val = read_result_register();
		}
		clear_data_ready();
		clear_result_ready();
		/* Clear out the data */
		while (is_data_requested()) {
			val = read_data_register();
		}
		abort_read_started = 0;
#if DEBUG
		printk("Leaving handle_sony_cd_attention at %d\n",
		       __LINE__);
#endif
		return (1);
	}

	num_consecutive_attentions = 0;
#if 0*DEBUG
	printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
#endif
	return (0);
}


/* Convert from an integer 0-99 to BCD */
static inline unsigned int int_to_bcd(unsigned int val)
{
	int retval;


	retval = (val / 10) << 4;
	retval = retval | val % 10;
	return (retval);
}


/* Convert from BCD to an integer from 0-99 */
static unsigned int bcd_to_int(unsigned int bcd)
{
	return ((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
}


/*
 * Convert a logical sector value (like the OS would want to use for
 * a block device) to an MSF format.
 */
static void log_to_msf(unsigned int log, unsigned char *msf)
{
	log = log + LOG_START_OFFSET;
	msf[0] = int_to_bcd(log / 4500);
	log = log % 4500;
	msf[1] = int_to_bcd(log / 75);
	msf[2] = int_to_bcd(log % 75);
}


/*
 * Convert an MSF format to a logical sector.
 */
static unsigned int msf_to_log(unsigned char *msf)
{
	unsigned int log;


	log = msf[2];
	log += msf[1] * 75;
	log += msf[0] * 4500;
	log = log - LOG_START_OFFSET;

	return log;
}


/*
 * Take in integer size value and put it into a buffer like
 * the drive would want to see a number-of-sector value.
 */
static void size_to_buf(unsigned int size, unsigned char *buf)
{
	buf[0] = size / 65536;
	size = size % 65536;
	buf[1] = size / 256;
	buf[2] = size % 256;
}

/* Starts a read operation. Returns 0 on success and 1 on failure. 
   The read operation used here allows multiple sequential sectors 
   to be read and status returned for each sector.  The driver will
   read the output one at a time as the requests come and abort the
   operation if the requested sector is not the next one from the
   drive. */
static int
start_request(unsigned int sector, unsigned int nsect, int read_nsect_only)
{
	unsigned char params[6];
	unsigned int read_size;
	unsigned int retry_count;


#if DEBUG
	printk("Entering start_request\n");
#endif
	log_to_msf(sector, params);
	/* If requested, read exactly what was asked. */
	if (read_nsect_only) {
		read_size = nsect;
	}
	/*
	 * If the full read-ahead would go beyond the end of the media, trim
	 * it back to read just till the end of the media.
	 */
	else if ((sector + nsect) >= sony_toc.lead_out_start_lba) {
		read_size = sony_toc.lead_out_start_lba - sector;
	}
	/* Read the full readahead amount. */
	else {
		read_size = CDU31A_READAHEAD / 4;
	}
	size_to_buf(read_size, &params[3]);

	/*
	 * Clear any outstanding attentions and wait for the drive to
	 * complete any pending operations.
	 */
	while (handle_sony_cd_attention());

	retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
	while (time_before(jiffies, retry_count) && (is_busy())) {
		sony_sleep();

		while (handle_sony_cd_attention());
	}

	if (is_busy()) {
		printk("CDU31A: Timeout while waiting to issue command\n");
#if DEBUG
		printk("Leaving start_request at %d\n", __LINE__);
#endif
		return (1);
	} else {
		/* Issue the command */
		clear_result_ready();
		clear_param_reg();

		write_params(params, 6);
		write_cmd(SONY_READ_BLKERR_STAT_CMD);

		sony_blocks_left = read_size * 4;
		sony_next_block = sector * 4;
		readahead_dataleft = 0;
		readahead_bad = 0;
#if DEBUG
		printk("Leaving start_request at %d\n", __LINE__);
#endif
		return (0);
	}
#if DEBUG
	printk("Leaving start_request at %d\n", __LINE__);
#endif
}

/* Abort a pending read operation.  Clear all the drive status and
   readahead variables. */
static void abort_read(void)
{
	unsigned char result_reg[2];
	int result_size;
	volatile int val;


	do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
	if ((result_reg[0] & 0xf0) == 0x20) {
		printk("CDU31A: Error aborting read, %s error\n",
		       translate_error(result_reg[1]));
	}

	while (is_result_reg_not_empty()) {
		val = read_result_register();
	}
	clear_data_ready();
	clear_result_ready();
	/* Clear out the data */
	while (is_data_requested()) {
		val = read_data_register();
	}

	sony_blocks_left = 0;
	readahead_dataleft = 0;
	readahead_bad = 0;
}

/* Called when the timer times out.  This will abort the
   pending read operation. */
static void handle_abort_timeout(unsigned long data)
{
	unsigned long flags;

#if DEBUG
	printk("Entering handle_abort_timeout\n");
#endif
	save_flags(flags);
	cli();
	/* If it is in use, ignore it. */
	if (!sony_inuse) {
		/* We can't use abort_read(), because it will sleep
		   or schedule in the timer interrupt.  Just start
		   the operation, finish it on the next access to
		   the drive. */
		clear_result_ready();
		clear_param_reg();
		write_cmd(SONY_ABORT_CMD);

		sony_blocks_left = 0;
		readahead_dataleft = 0;
		readahead_bad = 0;
		abort_read_started = 1;
	}
	restore_flags(flags);
#if DEBUG
	printk("Leaving handle_abort_timeout\n");
#endif
}

/* Actually get data and status from the drive. */
static void
input_data(char *buffer,
	   unsigned int bytesleft,
	   unsigned int nblocks, unsigned int offset, unsigned int skip)
{
	int i;
	volatile unsigned char val;


#if DEBUG
	printk("Entering input_data\n");
#endif
	/* If an XA disk on a CDU31A, skip the first 12 bytes of data from
	   the disk.  The real data is after that. */
	if (sony_xa_mode) {
		for (i = 0; i < CD_XA_HEAD; i++) {
			val = read_data_register();
		}
	}

	clear_data_ready();

	if (bytesleft == 2048) {	/* 2048 byte direct buffer transfer */
		insb(sony_cd_read_reg, buffer, 2048);
		readahead_dataleft = 0;
	} else {
		/* If the input read did not align with the beginning of the block,
		   skip the necessary bytes. */
		if (skip != 0) {
			insb(sony_cd_read_reg, readahead_buffer, skip);
		}

		/* Get the data into the buffer. */
		insb(sony_cd_read_reg, &buffer[offset], bytesleft);

		/* Get the rest of the data into the readahead buffer at the
		   proper location. */
		readahead_dataleft = (2048 - skip) - bytesleft;
		insb(sony_cd_read_reg,
		     readahead_buffer + bytesleft, readahead_dataleft);
	}
	sony_blocks_left -= nblocks;
	sony_next_block += nblocks;

	/* If an XA disk, we have to clear out the rest of the unused
	   error correction data. */
	if (sony_xa_mode) {
		for (i = 0; i < CD_XA_TAIL; i++) {
			val = read_data_register();
		}
	}
#if DEBUG
	printk("Leaving input_data at %d\n", __LINE__);
#endif
}

/* read data from the drive.  Note the nsect must be <= 4. */
static void
read_data_block(char *buffer,
		unsigned int block,
		unsigned int nblocks,
		unsigned char res_reg[], int *res_size)
{
	unsigned int retry_count;
	unsigned int bytesleft;
	unsigned int offset;
	unsigned int skip;


#if DEBUG
	printk("Entering read_data_block\n");
#endif

	res_reg[0] = 0;
	res_reg[1] = 0;
	*res_size = 0;
	bytesleft = nblocks * 512;
	offset = 0;

	/* If the data in the read-ahead does not match the block offset,
	   then fix things up. */
	if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048)) {
		sony_next_block += block % 4;
		sony_blocks_left -= block % 4;
		skip = (block % 4) * 512;
	} else {
		skip = 0;
	}

	/* We have readahead data in the buffer, get that first before we
	   decide if a read is necessary. */
	if (readahead_dataleft != 0) {
		if (bytesleft > readahead_dataleft) {
			/* The readahead will not fill the requested buffer, but
			   get the data out of the readahead into the buffer. */
			memcpy(buffer,
			       readahead_buffer + (2048 -
						   readahead_dataleft),
			       readahead_dataleft);
			readahead_dataleft = 0;
			bytesleft -= readahead_dataleft;
			offset += readahead_dataleft;
		} else {
			/* The readahead will fill the whole buffer, get the data
			   and return. */
			memcpy(buffer,
			       readahead_buffer + (2048 -
						   readahead_dataleft),
			       bytesleft);
			readahead_dataleft -= bytesleft;
			bytesleft = 0;
			sony_blocks_left -= nblocks;
			sony_next_block += nblocks;

			/* If the data in the readahead is bad, return an error so the
			   driver will abort the buffer. */
			if (readahead_bad) {
				res_reg[0] = 0x20;
				res_reg[1] = SONY_BAD_DATA_ERR;
				*res_size = 2;
			}

			if (readahead_dataleft == 0) {
				readahead_bad = 0;
			}

			/* Final transfer is done for read command, get final result. */
			if (sony_blocks_left == 0) {
				get_result(res_reg, res_size);
			}
#if DEBUG
			printk("Leaving read_data_block at %d\n",
			       __LINE__);
#endif
			return;
		}
	}

	/* Wait for the drive to tell us we have something */
	retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
	while (time_before(jiffies, retry_count) && !(is_data_ready())) {
		while (handle_sony_cd_attention());

		sony_sleep();
	}
	if (!(is_data_ready())) {
		if (is_result_ready()) {
			get_result(res_reg, res_size);
			if ((res_reg[0] & 0xf0) != 0x20) {
				printk
				    ("CDU31A: Got result that should have been error: %d\n",
				     res_reg[0]);
				res_reg[0] = 0x20;
				res_reg[1] = SONY_BAD_DATA_ERR;
				*res_size = 2;
			}
			abort_read();
		} else {
#if DEBUG
			printk("CDU31A timeout out %d\n", __LINE__);
#endif
			res_reg[0] = 0x20;
			res_reg[1] = SONY_TIMEOUT_OP_ERR;
			*res_size = 2;
			abort_read();
		}
	} else {
		input_data(buffer, bytesleft, nblocks, offset, skip);

		/* Wait for the status from the drive. */
		retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
		while (time_before(jiffies, retry_count)
		       && !(is_result_ready())) {
			while (handle_sony_cd_attention());

			sony_sleep();
		}

		if (!is_result_ready()) {
#if DEBUG
			printk("CDU31A timeout out %d\n", __LINE__);
#endif
			res_reg[0] = 0x20;
			res_reg[1] = SONY_TIMEOUT_OP_ERR;
			*res_size = 2;
			abort_read();
		} else {
			get_result(res_reg, res_size);

			/* If we got a buffer status, handle that. */
			if ((res_reg[0] & 0xf0) == 0x50) {

				if ((res_reg[0] ==
				     SONY_NO_CIRC_ERR_BLK_STAT)
				    || (res_reg[0] ==
					SONY_NO_LECC_ERR_BLK_STAT)
				    || (res_reg[0] ==
					SONY_RECOV_LECC_ERR_BLK_STAT)) {
					/* The data was successful, but if data was read from
					   the readahead  and it was bad, set the whole
					   buffer as bad. */
					if (readahead_bad) {
						readahead_bad = 0;
						res_reg[0] = 0x20;
						res_reg[1] =
						    SONY_BAD_DATA_ERR;
						*res_size = 2;
					}
				} else {
					printk
					    ("CDU31A: Data block error: 0x%x\n",
					     res_reg[0]);
					res_reg[0] = 0x20;
					res_reg[1] = SONY_BAD_DATA_ERR;
					*res_size = 2;

					/* Data is in the readahead buffer but an error was returned.
					   Make sure future requests don't use the data. */
					if (bytesleft != 2048) {
						readahead_bad = 1;
					}
				}

				/* Final transfer is done for read command, get final result. */
				if (sony_blocks_left == 0) {
					get_result(res_reg, res_size);
				}
			} else if ((res_reg[0] & 0xf0) != 0x20) {
				/* The drive gave me bad status, I don't know what to do.
				   Reset the driver and return an error. */
				printk
				    ("CDU31A: Invalid block status: 0x%x\n",
				     res_reg[0]);
				restart_on_error();
				res_reg[0] = 0x20;
				res_reg[1] = SONY_BAD_DATA_ERR;
				*res_size = 2;
			}
		}
	}
#if DEBUG
	printk("Leaving read_data_block at %d\n", __LINE__);
#endif
}


/*
 * The OS calls this to perform a read or write operation to the drive.
 * Write obviously fail.  Reads to a read ahead of sony_buffer_size
 * bytes to help speed operations.  This especially helps since the OS
 * uses 1024 byte blocks and the drive uses 2048 byte blocks.  Since most
 * data access on a CD is done sequentially, this saves a lot of operations.
 */
static void do_cdu31a_request(request_queue_t * q)
{
	int block;
	int nblock;
	unsigned char res_reg[12];
	unsigned int res_size;
	int num_retries;
	unsigned long flags;


#if DEBUG
	printk("Entering do_cdu31a_request\n");
#endif

	/* 
	 * Make sure no one else is using the driver; wait for them
	 * to finish if it is so.
	 */
	save_flags(flags);
	cli();
	while (sony_inuse) {
		interruptible_sleep_on(&sony_wait);
		if (signal_pending(current)) {
			restore_flags(flags);
			if (!QUEUE_EMPTY
			    && CURRENT->rq_status != RQ_INACTIVE) {
				end_request(0);
			}
			restore_flags(flags);
#if DEBUG
			printk("Leaving do_cdu31a_request at %d\n",
			       __LINE__);
#endif
			return;
		}
	}
	sony_inuse = 1;
	has_cd_task = current;

	/* Get drive status before doing anything. */
	while (handle_sony_cd_attention());

	/* Make sure we have a valid TOC. */
	sony_get_toc();

	spin_unlock_irq(&io_request_lock);

	/* Make sure the timer is cancelled. */
	del_timer(&cdu31a_abort_timer);

	while (1) {
	      cdu31a_request_startover:
		/*
		 * The beginning here is stolen from the hard disk driver.  I hope
		 * it's right.
		 */
		if (QUEUE_EMPTY || CURRENT->rq_status == RQ_INACTIVE) {
			goto end_do_cdu31a_request;
		}

		if (!sony_spun_up) {
			scd_spinup();
		}

		/* I don't use INIT_REQUEST because it calls return, which would
		   return without unlocking the device.  It shouldn't matter,
		   but just to be safe... */
		if (MAJOR(CURRENT->rq_dev) != MAJOR_NR) {
			panic(DEVICE_NAME ": request list destroyed");
		}
		if (CURRENT->bh) {
			if (!buffer_locked(CURRENT->bh)) {
				panic(DEVICE_NAME ": block not locked");
			}
		}

		block = CURRENT->sector;
		nblock = CURRENT->nr_sectors;

		if (!sony_toc_read) {
			printk("CDU31A: TOC not read\n");
			end_request(0);
			goto cdu31a_request_startover;
		}

		switch (CURRENT->cmd) {
		case READ:
			/*
			 * If the block address is invalid or the request goes beyond the end of
			 * the media, return an error.
			 */
#if 0
			if ((block / 4) < sony_toc.start_track_lba) {
				printk
				    ("CDU31A: Request before beginning of media\n");
				end_request(0);
				goto cdu31a_request_startover;
			}
#endif
			if ((block / 4) >= sony_toc.lead_out_start_lba) {
				printk
				    ("CDU31A: Request past end of media\n");
				end_request(0);
				goto cdu31a_request_startover;
			}
			if (((block + nblock) / 4) >=
			    sony_toc.lead_out_start_lba) {
				printk
				    ("CDU31A: Request past end of media\n");
				end_request(0);
				goto cdu31a_request_startover;
			}

			num_retries = 0;

		      try_read_again:
			while (handle_sony_cd_attention());

			if (!sony_toc_read) {
				printk("CDU31A: TOC not read\n");
				end_request(0);
				goto cdu31a_request_startover;
			}

			/* If no data is left to be read from the drive, start the
			   next request. */
			if (sony_blocks_left == 0) {
				if (start_request
				    (block / 4, CDU31A_READAHEAD / 4, 0)) {
					end_request(0);
					goto cdu31a_request_startover;
				}
			}
			/* If the requested block is not the next one waiting in
			   the driver, abort the current operation and start a
			   new one. */
			else if (block != sony_next_block) {
#if DEBUG
				printk
				    ("CDU31A Warning: Read for block %d, expected %d\n",
				     block, sony_next_block);
#endif
				abort_read();
				if (!sony_toc_read) {
					printk("CDU31A: TOC not read\n");
					end_request(0);
					goto cdu31a_request_startover;
				}
				if (start_request
				    (block / 4, CDU31A_READAHEAD / 4, 0)) {
					printk
					    ("CDU31a: start request failed\n");
					end_request(0);
					goto cdu31a_request_startover;
				}
			}

			read_data_block(CURRENT->buffer, block, nblock,
					res_reg, &res_size);
			if (res_reg[0] == 0x20) {
				if (num_retries > MAX_CDU31A_RETRIES) {
					end_request(0);
					goto cdu31a_request_startover;
				}

				num_retries++;
				if (res_reg[1] == SONY_NOT_SPIN_ERR) {
					do_sony_cd_cmd(SONY_SPIN_UP_CMD,
						       NULL, 0, res_reg,
						       &res_size);
				} else {
					printk
					    ("CDU31A: %s error for block %d, nblock %d\n",
					     translate_error(res_reg[1]),
					     block, nblock);
				}
				goto try_read_again;
			} else {
				end_request(1);
			}
			break;

		case WRITE:
			end_request(0);
			break;

		default:
			panic("CDU31A: Unknown cmd");
		}
	}

      end_do_cdu31a_request:
	spin_lock_irq(&io_request_lock);
#if 0
	/* After finished, cancel any pending operations. */
	abort_read();
#else
	/* Start a timer to time out after a while to disable
	   the read. */
	cdu31a_abort_timer.expires = jiffies + 2 * HZ;	/* Wait 2 seconds */
	add_timer(&cdu31a_abort_timer);
#endif

	has_cd_task = NULL;
	sony_inuse = 0;
	wake_up_interruptible(&sony_wait);
	restore_flags(flags);
#if DEBUG
	printk("Leaving do_cdu31a_request at %d\n", __LINE__);
#endif
}


/*
 * Read the table of contents from the drive and set up TOC if
 * successful.
 */
static void sony_get_toc(void)
{
	unsigned char res_reg[2];
	unsigned int res_size;
	unsigned char parms[1];
	int session;
	int num_spin_ups;
	int totaltracks = 0;
	int mint = 99;
	int maxt = 0;

#if DEBUG
	printk("Entering sony_get_toc\n");
#endif

	num_spin_ups = 0;
	if (!sony_toc_read) {
	      respinup_on_gettoc:
		/* Ignore the result, since it might error if spinning already. */
		do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
			       &res_size);

		do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg,
			       &res_size);

		/* The drive sometimes returns error 0.  I don't know why, but ignore
		   it.  It seems to mean the drive has already done the operation. */
		if ((res_size < 2)
		    || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
			/* If the drive is already playing, it's ok.  */
			if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
			    || (res_reg[1] == 0)) {
				goto gettoc_drive_spinning;
			}

			/* If the drive says it is not spun up (even though we just did it!)
			   then retry the operation at least a few times. */
			if ((res_reg[1] == SONY_NOT_SPIN_ERR)
			    && (num_spin_ups < MAX_CDU31A_RETRIES)) {
				num_spin_ups++;
				goto respinup_on_gettoc;
			}

			printk("cdu31a: Error reading TOC: %x %s\n",
			       res_reg[0], translate_error(res_reg[1]));
			return;
		}

	      gettoc_drive_spinning:

		/* The idea here is we keep asking for sessions until the command
		   fails.  Then we know what the last valid session on the disk is.
		   No need to check session 0, since session 0 is the same as session
		   1; the command returns different information if you give it 0. 
		 */
#if DEBUG
		memset(&sony_toc, 0x0e, sizeof(sony_toc));
		memset(&single_toc, 0x0f, sizeof(single_toc));
#endif
		session = 1;
		while (1) {
/* This seems to slow things down enough to make it work.  This
 * appears to be a problem in do_sony_cd_cmd.  This printk seems 
 * to address the symptoms...  -Erik */
#if 1
			printk("cdu31a: Trying session %d\n", session);
#endif
			parms[0] = session;
			do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
				       parms, 1, res_reg, &res_size);

#if DEBUG
			printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
#endif

			if ((res_size < 2)
			    || ((res_reg[0] & 0xf0) == 0x20)) {
				/* An error reading the TOC, this must be past the last session. */
				if (session == 1)
					printk
					    ("Yikes! Couldn't read any sessions!");
				break;
			}
#if DEBUG
			printk("Reading session %d\n", session);
#endif

			parms[0] = session;
			do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
				       parms,
				       1,
				       (unsigned char *) &single_toc,
				       &res_size);
			if ((res_size < 2)
			    || ((single_toc.exec_status[0] & 0xf0) ==
				0x20)) {
				printk
				    ("cdu31a: Error reading session %d: %x %s\n",
				     session, single_toc.exec_status[0],
				     translate_error(single_toc.
						     exec_status[1]));
				/* An error reading the TOC.  Return without sony_toc_read
				   set. */
				return;
			}
#if DEBUG
			printk
			    ("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n",
			     single_toc.address0, single_toc.control0,
			     single_toc.point0,
			     bcd_to_int(single_toc.first_track_num),
			     single_toc.disk_type, single_toc.dummy0);
			printk
			    ("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n",
			     single_toc.address1, single_toc.control1,
			     single_toc.point1,
			     bcd_to_int(single_toc.last_track_num),
			     single_toc.dummy1, single_toc.dummy2);
			printk
			    ("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n",
			     single_toc.address2, single_toc.control2,
			     single_toc.point2,
			     bcd_to_int(single_toc.lead_out_start_msf[0]),
			     bcd_to_int(single_toc.lead_out_start_msf[1]),
			     bcd_to_int(single_toc.lead_out_start_msf[2]));
			if (res_size > 18 && single_toc.pointb0 > 0xaf)
				printk
				    ("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
				     "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
				     single_toc.addressb0,
				     single_toc.controlb0,
				     single_toc.pointb0,
				     bcd_to_int(single_toc.
						next_poss_prog_area_msf
						[0]),
				     bcd_to_int(single_toc.
						next_poss_prog_area_msf
						[1]),
				     bcd_to_int(single_toc.
						next_poss_prog_area_msf
						[2]),
				     single_toc.num_mode_5_pointers,
				     bcd_to_int(single_toc.
						max_start_outer_leadout_msf
						[0]),
				     bcd_to_int(single_toc.
						max_start_outer_leadout_msf
						[1]),
				     bcd_to_int(single_toc.
						max_start_outer_leadout_msf
						[2]));
			if (res_size > 27 && single_toc.pointb1 > 0xaf)
				printk
				    ("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
				     single_toc.addressb1,
				     single_toc.controlb1,
				     single_toc.pointb1,
				     single_toc.dummyb0_1[0],
				     single_toc.dummyb0_1[1],
				     single_toc.dummyb0_1[2],
				     single_toc.dummyb0_1[3],
				     single_toc.num_skip_interval_pointers,
				     single_toc.num_skip_track_assignments,
				     single_toc.dummyb0_2);
			if (res_size > 36 && single_toc.pointb2 > 0xaf)
				printk
				    ("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
				     single_toc.addressb2,
				     single_toc.controlb2,
				     single_toc.pointb2,
				     single_toc.tracksb2[0],
				     single_toc.tracksb2[1],
				     single_toc.tracksb2[2],
				     single_toc.tracksb2[3],
				     single_toc.tracksb2[4],
				     single_toc.tracksb2[5],
				     single_toc.tracksb2[6]);
			if (res_size > 45 && single_toc.pointb3 > 0xaf)
				printk
				    ("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
				     single_toc.addressb3,
				     single_toc.controlb3,
				     single_toc.pointb3,
				     single_toc.tracksb3[0],
				     single_toc.tracksb3[1],
				     single_toc.tracksb3[2],
				     single_toc.tracksb3[3],
				     single_toc.tracksb3[4],
				     single_toc.tracksb3[5],
				     single_toc.tracksb3[6]);
			if (res_size > 54 && single_toc.pointb4 > 0xaf)
				printk
				    ("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
				     single_toc.addressb4,
				     single_toc.controlb4,
				     single_toc.pointb4,
				     single_toc.tracksb4[0],
				     single_toc.tracksb4[1],
				     single_toc.tracksb4[2],
				     single_toc.tracksb4[3],
				     single_toc.tracksb4[4],
				     single_toc.tracksb4[5],
				     single_toc.tracksb4[6]);
			if (res_size > 63 && single_toc.pointc0 > 0xaf)
				printk
				    ("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
				     single_toc.addressc0,
				     single_toc.controlc0,
				     single_toc.pointc0,
				     single_toc.dummyc0[0],
				     single_toc.dummyc0[1],
				     single_toc.dummyc0[2],
				     single_toc.dummyc0[3],
				     single_toc.dummyc0[4],
				     single_toc.dummyc0[5],
				     single_toc.dummyc0[6]);
#endif
#undef DEBUG
#define DEBUG 0

			sony_toc.lead_out_start_msf[0] =
			    bcd_to_int(single_toc.lead_out_start_msf[0]);
			sony_toc.lead_out_start_msf[1] =
			    bcd_to_int(single_toc.lead_out_start_msf[1]);
			sony_toc.lead_out_start_msf[2] =
			    bcd_to_int(single_toc.lead_out_start_msf[2]);
			sony_toc.lead_out_start_lba =
			    single_toc.lead_out_start_lba =
			    msf_to_log(sony_toc.lead_out_start_msf);

			/* For points that do not exist, move the data over them
			   to the right location. */
			if (single_toc.pointb0 != 0xb0) {
				memmove(((char *) &single_toc) + 27,
					((char *) &single_toc) + 18,
					res_size - 18);
				res_size += 9;
			} else if (res_size > 18) {
				sony_toc.lead_out_start_msf[0] =
				    bcd_to_int(single_toc.
					       max_start_outer_leadout_msf
					       [0]);
				sony_toc.lead_out_start_msf[1] =
				    bcd_to_int(single_toc.
					       max_start_outer_leadout_msf
					       [1]);
				sony_toc.lead_out_start_msf[2] =
				    bcd_to_int(single_toc.
					       max_start_outer_leadout_msf
					       [2]);
				sony_toc.lead_out_start_lba =
				    msf_to_log(sony_toc.
					       lead_out_start_msf);
			}
			if (single_toc.pointb1 != 0xb1) {
				memmove(((char *) &single_toc) + 36,
					((char *) &single_toc) + 27,
					res_size - 27);
				res_size += 9;
			}
			if (single_toc.pointb2 != 0xb2) {
				memmove(((char *) &single_toc) + 45,
					((char *) &single_toc) + 36,
					res_size - 36);
				res_size += 9;
			}
			if (single_toc.pointb3 != 0xb3) {
				memmove(((char *) &single_toc) + 54,
					((char *) &single_toc) + 45,
					res_size - 45);
				res_size += 9;
			}
			if (single_toc.pointb4 != 0xb4) {
				memmove(((char *) &single_toc) + 63,
					((char *) &single_toc) + 54,
					res_size - 54);
				res_size += 9;
			}
			if (single_toc.pointc0 != 0xc0) {
				memmove(((char *) &single_toc) + 72,
					((char *) &single_toc) + 63,
					res_size - 63);
				res_size += 9;
			}
#if DEBUG
			printk
			    ("start track lba %u,  leadout start lba %u\n",
			     single_toc.start_track_lba,
			     single_toc.lead_out_start_lba);
			{
				int i;
				for (i = 0;
				     i <
				     1 +
				     bcd_to_int(single_toc.last_track_num)
				     -
				     bcd_to_int(single_toc.
						first_track_num); i++) {
					printk
					    ("trk %02d: add 0x%01x, con 0x%01x,  track %02d, start min %02d, sec %02d, frame %02d\n",
					     i,
					     single_toc.tracks[i].address,
					     single_toc.tracks[i].control,
					     bcd_to_int(single_toc.
							tracks[i].track),
					     bcd_to_int(single_toc.
							tracks[i].
							track_start_msf
							[0]),
					     bcd_to_int(single_toc.
							tracks[i].
							track_start_msf
							[1]),
					     bcd_to_int(single_toc.
							tracks[i].
							track_start_msf
							[2]));
					if (mint >
					    bcd_to_int(single_toc.
						       tracks[i].track))
						mint =
						    bcd_to_int(single_toc.
							       tracks[i].
							       track);
					if (maxt <
					    bcd_to_int(single_toc.
						       tracks[i].track))
						maxt =
						    bcd_to_int(single_toc.
							       tracks[i].
							       track);
				}
				printk
				    ("min track number %d,   max track number %d\n",
				     mint, maxt);
			}
#endif

			/* prepare a special table of contents for a CD-I disc. They don't have one. */
			if (single_toc.disk_type == 0x10 &&
			    single_toc.first_track_num == 2 &&
			    single_toc.last_track_num == 2 /* CD-I */ ) {
				sony_toc.tracks[totaltracks].address = 1;
				sony_toc.tracks[totaltracks].control = 4;	/* force data tracks */
				sony_toc.tracks[totaltracks].track = 1;
				sony_toc.tracks[totaltracks].
				    track_start_msf[0] = 0;
				sony_toc.tracks[totaltracks].
				    track_start_msf[1] = 2;
				sony_toc.tracks[totaltracks].
				    track_start_msf[2] = 0;
				mint = maxt = 1;
				totaltracks++;
			} else
				/* gather track entries from this session */
			{
				int i;
				for (i = 0;
				     i <
				     1 +
				     bcd_to_int(single_toc.last_track_num)
				     -
				     bcd_to_int(single_toc.
						first_track_num);
				     i++, totaltracks++) {
					sony_toc.tracks[totaltracks].
					    address =
					    single_toc.tracks[i].address;
					sony_toc.tracks[totaltracks].
					    control =
					    single_toc.tracks[i].control;
					sony_toc.tracks[totaltracks].
					    track =
					    bcd_to_int(single_toc.
						       tracks[i].track);
					sony_toc.tracks[totaltracks].
					    track_start_msf[0] =
					    bcd_to_int(single_toc.
						       tracks[i].
						       track_start_msf[0]);
					sony_toc.tracks[totaltracks].
					    track_start_msf[1] =
					    bcd_to_int(single_toc.
						       tracks[i].
						       track_start_msf[1]);
					sony_toc.tracks[totaltracks].
					    track_start_msf[2] =
					    bcd_to_int(single_toc.
						       tracks[i].
						       track_start_msf[2]);
					if (i == 0)
						single_toc.
						    start_track_lba =
						    msf_to_log(sony_toc.
							       tracks
							       [totaltracks].
							       track_start_msf);
					if (mint >
					    sony_toc.tracks[totaltracks].
					    track)
						mint =
						    sony_toc.
						    tracks[totaltracks].
						    track;
					if (maxt <
					    sony_toc.tracks[totaltracks].
					    track)
						maxt =
						    sony_toc.
						    tracks[totaltracks].
						    track;
				}
			}
			sony_toc.first_track_num = mint;
			sony_toc.last_track_num = maxt;
			/* Disk type of last session wins. For example:
			   CD-Extra has disk type 0 for the first session, so
			   a dumb HiFi CD player thinks it is a plain audio CD.
			   We are interested in the disk type of the last session,
			   which is 0x20 (XA) for CD-Extra, so we can access the
			   data track ... */
			sony_toc.disk_type = single_toc.disk_type;
			sony_toc.sessions = session;

			/* don't believe everything :-) */
			if (session == 1)
				single_toc.start_track_lba = 0;
			sony_toc.start_track_lba =
			    single_toc.start_track_lba;

			if (session > 1 && single_toc.pointb0 == 0xb0 &&
			    sony_toc.lead_out_start_lba ==
			    single_toc.lead_out_start_lba) {
				break;
			}

			/* Let's not get carried away... */
			if (session > 40) {
				printk("cdu31a: too many sessions: %d\n",
				       session);
				break;
			}
			session++;
		}
		sony_toc.track_entries = totaltracks;
		/* add one entry for the LAST track with track number CDROM_LEADOUT */
		sony_toc.tracks[totaltracks].address = single_toc.address2;
		sony_toc.tracks[totaltracks].control = single_toc.control2;
		sony_toc.tracks[totaltracks].track = CDROM_LEADOUT;
		sony_toc.tracks[totaltracks].track_start_msf[0] =
		    sony_toc.lead_out_start_msf[0];
		sony_toc.tracks[totaltracks].track_start_msf[1] =
		    sony_toc.lead_out_start_msf[1];
		sony_toc.tracks[totaltracks].track_start_msf[2] =
		    sony_toc.lead_out_start_msf[2];

		sony_toc_read = 1;
#undef DEBUG
#if DEBUG
		printk
		    ("Disk session %d, start track: %d, stop track: %d\n",
		     session, single_toc.start_track_lba,
		     single_toc.lead_out_start_lba);
#endif
	}
#if DEBUG
	printk("Leaving sony_get_toc\n");
#endif
}


/*
 * Uniform cdrom interface function
 * return multisession offset and sector information
 */
static int scd_get_last_session(struct cdrom_device_info *cdi,
				struct cdrom_multisession *ms_info)
{
	if (ms_info == NULL)
		return 1;

	if (!sony_toc_read)
		sony_get_toc();

	ms_info->addr_format = CDROM_LBA;
	ms_info->addr.lba = sony_toc.start_track_lba;
	ms_info->xa_flag = sony_toc.disk_type == SONY_XA_DISK_TYPE ||
	    sony_toc.disk_type == 0x10 /* CDI */ ;

	return 0;
}

/*
 * Search for a specific track in the table of contents.
 */
static int find_track(int track)
{
	int i;

	for (i = 0; i <= sony_toc.track_entries; i++) {
		if (sony_toc.tracks[i].track == track) {
			return i;
		}
	}

	return -1;
}


/*
 * Read the subcode and put it in last_sony_subcode for future use.
 */
static int read_subcode(void)
{
	unsigned int res_size;


	do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
		       NULL,
		       0, (unsigned char *) &last_sony_subcode, &res_size);
	if ((res_size < 2)
	    || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20)) {
		printk("Sony CDROM error %s (read_subcode)\n",
		       translate_error(last_sony_subcode.exec_status[1]));
		return -EIO;
	}

	last_sony_subcode.track_num =
	    bcd_to_int(last_sony_subcode.track_num);
	last_sony_subcode.index_num =
	    bcd_to_int(last_sony_subcode.index_num);
	last_sony_subcode.abs_msf[0] =
	    bcd_to_int(last_sony_subcode.abs_msf[0]);
	last_sony_subcode.abs_msf[1] =
	    bcd_to_int(last_sony_subcode.abs_msf[1]);
	last_sony_subcode.abs_msf[2] =
	    bcd_to_int(last_sony_subcode.abs_msf[2]);

	last_sony_subcode.rel_msf[0] =
	    bcd_to_int(last_sony_subcode.rel_msf[0]);
	last_sony_subcode.rel_msf[1] =
	    bcd_to_int(last_sony_subcode.rel_msf[1]);
	last_sony_subcode.rel_msf[2] =
	    bcd_to_int(last_sony_subcode.rel_msf[2]);
	return 0;
}

/*
 * Uniform cdrom interface function
 * return the media catalog number found on some older audio cds
 */
static int
scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
{
	unsigned char resbuffer[2 + 14];
	unsigned char *mcnp = mcn->medium_catalog_number;
	unsigned char *resp = resbuffer + 3;
	unsigned int res_size;

	memset(mcn->medium_catalog_number, 0, 14);
	do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD,
		       NULL, 0, resbuffer, &res_size);
	if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20));
	else {
		/* packed bcd to single ASCII digits */
		*mcnp++ = (*resp >> 4) + '0';
		*mcnp++ = (*resp++ & 0x0f) + '0';
		*mcnp++ = (*resp >> 4) + '0';
		*mcnp++ = (*resp++ & 0x0f) + '0';
		*mcnp++ = (*resp >> 4) + '0';
		*mcnp++ = (*resp++ & 0x0f) + '0';
		*mcnp++ = (*resp >> 4) + '0';
		*mcnp++ = (*resp++ & 0x0f) + '0';
		*mcnp++ = (*resp >> 4) + '0';
		*mcnp++ = (*resp++ & 0x0f) + '0';
		*mcnp++ = (*resp >> 4) + '0';
		*mcnp++ = (*resp++ & 0x0f) + '0';
		*mcnp++ = (*resp >> 4) + '0';
	}
	*mcnp = '\0';
	return 0;
}


/*
 * Get the subchannel info like the CDROMSUBCHNL command wants to see it.  If
 * the drive is playing, the subchannel needs to be read (since it would be
 * changing).  If the drive is paused or completed, the subcode information has
 * already been stored, just use that.  The ioctl call wants things in decimal
 * (not BCD), so all the conversions are done.
 */
static int sony_get_subchnl_info(struct cdrom_subchnl *schi)
{
	/* Get attention stuff */
	while (handle_sony_cd_attention());

	sony_get_toc();
	if (!sony_toc_read) {
		return -EIO;
	}

	switch (sony_audio_status) {
	case CDROM_AUDIO_NO_STATUS:
	case CDROM_AUDIO_PLAY:
		if (read_subcode() < 0) {
			return -EIO;
		}
		break;

	case CDROM_AUDIO_PAUSED:
	case CDROM_AUDIO_COMPLETED:
		break;

#if 0
	case CDROM_AUDIO_NO_STATUS:
		schi->cdsc_audiostatus = sony_audio_status;
		return 0;
		break;
#endif
	case CDROM_AUDIO_INVALID:
	case CDROM_AUDIO_ERROR:
	default:
		return -EIO;
	}

	schi->cdsc_audiostatus = sony_audio_status;
	schi->cdsc_adr = last_sony_subcode.address;
	schi->cdsc_ctrl = last_sony_subcode.control;
	schi->cdsc_trk = last_sony_subcode.track_num;
	schi->cdsc_ind = last_sony_subcode.index_num;
	if (schi->cdsc_format == CDROM_MSF) {
		schi->cdsc_absaddr.msf.minute =
		    last_sony_subcode.abs_msf[0];
		schi->cdsc_absaddr.msf.second =
		    last_sony_subcode.abs_msf[1];
		schi->cdsc_absaddr.msf.frame =
		    last_sony_subcode.abs_msf[2];

		schi->cdsc_reladdr.msf.minute =
		    last_sony_subcode.rel_msf[0];
		schi->cdsc_reladdr.msf.second =
		    last_sony_subcode.rel_msf[1];
		schi->cdsc_reladdr.msf.frame =
		    last_sony_subcode.rel_msf[2];
	} else if (schi->cdsc_format == CDROM_LBA) {
		schi->cdsc_absaddr.lba =
		    msf_to_log(last_sony_subcode.abs_msf);
		schi->cdsc_reladdr.lba =
		    msf_to_log(last_sony_subcode.rel_msf);
	}

	return 0;
}

/* Get audio data from the drive.  This is fairly complex because I
   am looking for status and data at the same time, but if I get status
   then I just look for data.  I need to get the status immediately so
   the switch from audio to data tracks will happen quickly. */
static void
read_audio_data(char *buffer, unsigned char res_reg[], int *res_size)
{
	unsigned int retry_count;
	int result_read;


	res_reg[0] = 0;
	res_reg[1] = 0;
	*res_size = 0;
	result_read = 0;

	/* Wait for the drive to tell us we have something */
	retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
      continue_read_audio_wait:
	while (time_before(jiffies, retry_count) && !(is_data_ready())
	       && !(is_result_ready() || result_read)) {
		while (handle_sony_cd_attention());

		sony_sleep();
	}
	if (!(is_data_ready())) {
		if (is_result_ready() && !result_read) {
			get_result(res_reg, res_size);

			/* Read block status and continue waiting for data. */
			if ((res_reg[0] & 0xf0) == 0x50) {
				result_read = 1;
				goto continue_read_audio_wait;
			}
			/* Invalid data from the drive.  Shut down the operation. */
			else if ((res_reg[0] & 0xf0) != 0x20) {
				printk
				    ("CDU31A: Got result that should have been error: %d\n",
				     res_reg[0]);
				res_reg[0] = 0x20;
				res_reg[1] = SONY_BAD_DATA_ERR;
				*res_size = 2;
			}
			abort_read();
		} else {
#if DEBUG
			printk("CDU31A timeout out %d\n", __LINE__);
#endif
			res_reg[0] = 0x20;
			res_reg[1] = SONY_TIMEOUT_OP_ERR;
			*res_size = 2;
			abort_read();
		}
	} else {
		clear_data_ready();

		/* If data block, then get 2340 bytes offset by 12. */
		if (sony_raw_data_mode) {
			insb(sony_cd_read_reg, buffer + CD_XA_HEAD,
			     CD_FRAMESIZE_RAW1);
		} else {
			/* Audio gets the whole 2352 bytes. */
			insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
		}

		/* If I haven't already gotten the result, get it now. */
		if (!result_read) {
			/* Wait for the drive to tell us we have something */
			retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
			while (time_before(jiffies, retry_count)
			       && !(is_result_ready())) {
				while (handle_sony_cd_attention());

				sony_sleep();
			}

			if (!is_result_ready()) {
#if DEBUG
				printk("CDU31A timeout out %d\n",
				       __LINE__);
#endif
				res_reg[0] = 0x20;
				res_reg[1] = SONY_TIMEOUT_OP_ERR;
				*res_size = 2;
				abort_read();
				return;
			} else {
				get_result(res_reg, res_size);
			}
		}

		if ((res_reg[0] & 0xf0) == 0x50) {
			if ((res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
			    || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
			    || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
			    || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT)) {
				/* Ok, nothing to do. */
			} else {
				printk("CDU31A: Data block error: 0x%x\n",
				       res_reg[0]);
				res_reg[0] = 0x20;
				res_reg[1] = SONY_BAD_DATA_ERR;
				*res_size = 2;
			}
		} else if ((res_reg[0] & 0xf0) != 0x20) {
			/* The drive gave me bad status, I don't know what to do.
			   Reset the driver and return an error. */
			printk("CDU31A: Invalid block status: 0x%x\n",
			       res_reg[0]);
			restart_on_error();
			res_reg[0] = 0x20;
			res_reg[1] = SONY_BAD_DATA_ERR;
			*res_size = 2;
		}
	}
}

/* Perform a raw data read.  This will automatically detect the
   track type and read the proper data (audio or data). */
static int read_audio(struct cdrom_read_audio *ra)
{
	int retval;
	unsigned char params[2];
	unsigned char res_reg[12];
	unsigned int res_size;
	unsigned int cframe;
	unsigned long flags;

	/* 
	 * Make sure no one else is using the driver; wait for them
	 * to finish if it is so.
	 */
	save_flags(flags);
	cli();
	while (sony_inuse) {
		interruptible_sleep_on(&sony_wait);
		if (signal_pending(current)) {
			restore_flags(flags);
			return -EAGAIN;
		}
	}
	sony_inuse = 1;
	has_cd_task = current;
	restore_flags(flags);

	if (!sony_spun_up) {
		scd_spinup();
	}

	/* Set the drive to do raw operations. */
	params[0] = SONY_SD_DECODE_PARAM;
	params[1] = 0x06 | sony_raw_data_mode;
	do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
		       params, 2, res_reg, &res_size);
	if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
		printk("CDU31A: Unable to set decode params: 0x%2.2x\n",
		       res_reg[1]);
		return -EIO;
	}

	/* From here down, we have to goto exit_read_audio instead of returning
	   because the drive parameters have to be set back to data before
	   return. */

	retval = 0;
	/* start_request clears out any readahead data, so it should be safe. */
	if (start_request(ra->addr.lba, ra->nframes, 1)) {
		retval = -EIO;
		goto exit_read_audio;
	}

	/* For every requested frame. */
	cframe = 0;
	while (cframe < ra->nframes) {
		read_audio_data(readahead_buffer, res_reg, &res_size);
		if ((res_reg[0] & 0xf0) == 0x20) {
			if (res_reg[1] == SONY_BAD_DATA_ERR) {
				printk
				    ("CDU31A: Data error on audio sector %d\n",
				     ra->addr.lba + cframe);
			} else if (res_reg[1] == SONY_ILL_TRACK_R_ERR) {
				/* Illegal track type, change track types and start over. */
				sony_raw_data_mode =
				    (sony_raw_data_mode) ? 0 : 1;

				/* Set the drive mode. */
				params[0] = SONY_SD_DECODE_PARAM;
				params[1] = 0x06 | sony_raw_data_mode;
				do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
					       params,
					       2, res_reg, &res_size);
				if ((res_size < 2)
				    || ((res_reg[0] & 0xf0) == 0x20)) {
					printk
					    ("CDU31A: Unable to set decode params: 0x%2.2x\n",
					     res_reg[1]);
					retval = -EIO;
					goto exit_read_audio;
				}

				/* Restart the request on the current frame. */
				if (start_request
				    (ra->addr.lba + cframe,
				     ra->nframes - cframe, 1)) {
					retval = -EIO;
					goto exit_read_audio;
				}

				/* Don't go back to the top because don't want to get into
				   and infinite loop.  A lot of code gets duplicated, but
				   that's no big deal, I don't guess. */
				read_audio_data(readahead_buffer, res_reg,
						&res_size);
				if ((res_reg[0] & 0xf0) == 0x20) {
					if (res_reg[1] ==
					    SONY_BAD_DATA_ERR) {
						printk
						    ("CDU31A: Data error on audio sector %d\n",
						     ra->addr.lba +
						     cframe);
					} else {
						printk
						    ("CDU31A: Error reading audio data on sector %d: %s\n",
						     ra->addr.lba + cframe,
						     translate_error
						     (res_reg[1]));
						retval = -EIO;
						goto exit_read_audio;
					}
				} else {
					copy_to_user((char *) (ra->buf +
							       (CD_FRAMESIZE_RAW
								* cframe)),
						     (char *)
						     readahead_buffer,
						     CD_FRAMESIZE_RAW);
				}
			} else {
				printk
				    ("CDU31A: Error reading audio data on sector %d: %s\n",
				     ra->addr.lba + cframe,
				     translate_error(res_reg[1]));
				retval = -EIO;
				goto exit_read_audio;
			}
		} else {
			copy_to_user((char *) (ra->buf +
					       (CD_FRAMESIZE_RAW *
						cframe)),
				     (char *) readahead_buffer,
				     CD_FRAMESIZE_RAW);
		}

		cframe++;
	}

	get_result(res_reg, &res_size);
	if ((res_reg[0] & 0xf0) == 0x20) {
		printk("CDU31A: Error return from audio read: %s\n",
		       translate_error(res_reg[1]));
		retval = -EIO;
		goto exit_read_audio;
	}

      exit_read_audio:

	/* Set the drive mode back to the proper one for the disk. */
	params[0] = SONY_SD_DECODE_PARAM;
	if (!sony_xa_mode) {
		params[1] = 0x0f;
	} else {
		params[1] = 0x07;
	}
	do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
		       params, 2, res_reg, &res_size);
	if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
		printk("CDU31A: Unable to reset decode params: 0x%2.2x\n",
		       res_reg[1]);
		return -EIO;
	}

	has_cd_task = NULL;
	sony_inuse = 0;
	wake_up_interruptible(&sony_wait);

	return (retval);
}

static int
do_sony_cd_cmd_chk(const char *name,
		   unsigned char cmd,
		   unsigned char *params,
		   unsigned int num_params,
		   unsigned char *result_buffer, unsigned int *result_size)
{
	do_sony_cd_cmd(cmd, params, num_params, result_buffer,
		       result_size);
	if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20)) {
		printk("Sony CDROM error %s (CDROM%s)\n",
		       translate_error(result_buffer[1]), name);
		return -EIO;
	}
	return 0;
}

/*
 * Uniform cdrom interface function
 * open the tray
 */
static int scd_tray_move(struct cdrom_device_info *cdi, int position)
{
	if (position == 1 /* open tray */ ) {
		unsigned char res_reg[12];
		unsigned int res_size;

		do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
			       &res_size);
		do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
			       &res_size);

		sony_audio_status = CDROM_AUDIO_INVALID;
		return do_sony_cd_cmd_chk("EJECT", SONY_EJECT_CMD, NULL, 0,
					  res_reg, &res_size);
	} else {
		if (0 == scd_spinup())
			sony_spun_up = 1;
		return 0;
	}
}

/*
 * The big ugly ioctl handler.
 */
static int scd_audio_ioctl(struct cdrom_device_info *cdi,
			   unsigned int cmd, void *arg)
{
	unsigned char res_reg[12];
	unsigned int res_size;
	unsigned char params[7];
	int i;


	switch (cmd) {
	case CDROMSTART:	/* Spin up the drive */
		return do_sony_cd_cmd_chk("START", SONY_SPIN_UP_CMD, NULL,
					  0, res_reg, &res_size);
		break;

	case CDROMSTOP:	/* Spin down the drive */
		do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
			       &res_size);

		/*
		 * Spin the drive down, ignoring the error if the disk was
		 * already not spinning.
		 */
		sony_audio_status = CDROM_AUDIO_NO_STATUS;
		return do_sony_cd_cmd_chk("STOP", SONY_SPIN_DOWN_CMD, NULL,
					  0, res_reg, &res_size);

	case CDROMPAUSE:	/* Pause the drive */
		if (do_sony_cd_cmd_chk
		    ("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
		     &res_size))
			return -EIO;
		/* Get the current position and save it for resuming */
		if (read_subcode() < 0) {
			return -EIO;
		}
		cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
		cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
		cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
		sony_audio_status = CDROM_AUDIO_PAUSED;
		return 0;
		break;

	case CDROMRESUME:	/* Start the drive after being paused */
		if (sony_audio_status != CDROM_AUDIO_PAUSED) {
			return -EINVAL;
		}

		do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
			       &res_size);

		/* Start the drive at the saved position. */
		params[1] = int_to_bcd(cur_pos_msf[0]);
		params[2] = int_to_bcd(cur_pos_msf[1]);
		params[3] = int_to_bcd(cur_pos_msf[2]);
		params[4] = int_to_bcd(final_pos_msf[0]);
		params[5] = int_to_bcd(final_pos_msf[1]);
		params[6] = int_to_bcd(final_pos_msf[2]);
		params[0] = 0x03;
		if (do_sony_cd_cmd_chk
		    ("RESUME", SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg,
		     &res_size) < 0)
			return -EIO;
		sony_audio_status = CDROM_AUDIO_PLAY;
		return 0;

	case CDROMPLAYMSF:	/* Play starting at the given MSF address. */
		do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
			       &res_size);

		/* The parameters are given in int, must be converted */
		for (i = 1; i < 7; i++) {
			params[i] =
			    int_to_bcd(((unsigned char *) arg)[i - 1]);
		}
		params[0] = 0x03;
		if (do_sony_cd_cmd_chk
		    ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD, params, 7,
		     res_reg, &res_size) < 0)
			return -EIO;

		/* Save the final position for pauses and resumes */
		final_pos_msf[0] = bcd_to_int(params[4]);
		final_pos_msf[1] = bcd_to_int(params[5]);
		final_pos_msf[2] = bcd_to_int(params[6]);
		sony_audio_status = CDROM_AUDIO_PLAY;
		return 0;

	case CDROMREADTOCHDR:	/* Read the table of contents header */
		{
			struct cdrom_tochdr *hdr;

			sony_get_toc();
			if (!sony_toc_read) {
				return -EIO;
			}

			hdr = (struct cdrom_tochdr *) arg;
			hdr->cdth_trk0 = sony_toc.first_track_num;
			hdr->cdth_trk1 = sony_toc.last_track_num;
		}
		return 0;

	case CDROMREADTOCENTRY:	/* Read a given table of contents entry */
		{
			struct cdrom_tocentry *entry;
			int track_idx;
			unsigned char *msf_val = NULL;

			sony_get_toc();
			if (!sony_toc_read) {
				return -EIO;
			}

			entry = (struct cdrom_tocentry *) arg;

			track_idx = find_track(entry->cdte_track);
			if (track_idx < 0) {
				return -EINVAL;
			}

			entry->cdte_adr =
			    sony_toc.tracks[track_idx].address;
			entry->cdte_ctrl =
			    sony_toc.tracks[track_idx].control;
			msf_val =
			    sony_toc.tracks[track_idx].track_start_msf;

			/* Logical buffer address or MSF format requested? */
			if (entry->cdte_format == CDROM_LBA) {
				entry->cdte_addr.lba = msf_to_log(msf_val);
			} else if (entry->cdte_format == CDROM_MSF) {
				entry->cdte_addr.msf.minute = *msf_val;
				entry->cdte_addr.msf.second =
				    *(msf_val + 1);
				entry->cdte_addr.msf.frame =
				    *(msf_val + 2);
			}
		}
		return 0;
		break;

	case CDROMPLAYTRKIND:	/* Play a track.  This currently ignores index. */
		{
			struct cdrom_ti *ti = (struct cdrom_ti *) arg;
			int track_idx;

			sony_get_toc();
			if (!sony_toc_read) {
				return -EIO;
			}

			if ((ti->cdti_trk0 < sony_toc.first_track_num)
			    || (ti->cdti_trk0 > sony_toc.last_track_num)
			    || (ti->cdti_trk1 < ti->cdti_trk0)) {
				return -EINVAL;
			}

			track_idx = find_track(ti->cdti_trk0);
			if (track_idx < 0) {
				return -EINVAL;
			}
			params[1] =
			    int_to_bcd(sony_toc.tracks[track_idx].
				       track_start_msf[0]);
			params[2] =
			    int_to_bcd(sony_toc.tracks[track_idx].
				       track_start_msf[1]);
			params[3] =
			    int_to_bcd(sony_toc.tracks[track_idx].
				       track_start_msf[2]);

			/*
			 * If we want to stop after the last track, use the lead-out
			 * MSF to do that.
			 */
			if (ti->cdti_trk1 >= sony_toc.last_track_num) {
				track_idx = find_track(CDROM_LEADOUT);
			} else {
				track_idx = find_track(ti->cdti_trk1 + 1);
			}
			if (track_idx < 0) {
				return -EINVAL;
			}
			params[4] =
			    int_to_bcd(sony_toc.tracks[track_idx].
				       track_start_msf[0]);
			params[5] =
			    int_to_bcd(sony_toc.tracks[track_idx].
				       track_start_msf[1]);
			params[6] =
			    int_to_bcd(sony_toc.tracks[track_idx].
				       track_start_msf[2]);
			params[0] = 0x03;

			do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
				       &res_size);

			do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7,
				       res_reg, &res_size);

			if ((res_size < 2)
			    || ((res_reg[0] & 0xf0) == 0x20)) {
				printk("Params: %x %x %x %x %x %x %x\n",
				       params[0], params[1], params[2],
				       params[3], params[4], params[5],
				       params[6]);
				printk
				    ("Sony CDROM error %s (CDROMPLAYTRKIND)\n",
				     translate_error(res_reg[1]));
				return -EIO;
			}

			/* Save the final position for pauses and resumes */
			final_pos_msf[0] = bcd_to_int(params[4]);
			final_pos_msf[1] = bcd_to_int(params[5]);
			final_pos_msf[2] = bcd_to_int(params[6]);
			sony_audio_status = CDROM_AUDIO_PLAY;
			return 0;
		}

	case CDROMVOLCTRL:	/* Volume control.  What volume does this change, anyway? */
		{
			struct cdrom_volctrl *volctrl =
			    (struct cdrom_volctrl *) arg;

			params[0] = SONY_SD_AUDIO_VOLUME;
			params[1] = volctrl->channel0;
			params[2] = volctrl->channel1;
			return do_sony_cd_cmd_chk("VOLCTRL",
						  SONY_SET_DRIVE_PARAM_CMD,
						  params, 3, res_reg,
						  &res_size);
		}
	case CDROMSUBCHNL:	/* Get subchannel info */
		return sony_get_subchnl_info((struct cdrom_subchnl *) arg);

	default:
		return -EINVAL;
	}
}

static int scd_dev_ioctl(struct cdrom_device_info *cdi,
			 unsigned int cmd, unsigned long arg)
{
	int i;

	switch (cmd) {
	case CDROMREADAUDIO:	/* Read 2352 byte audio tracks and 2340 byte
				   raw data tracks. */
		{
			struct cdrom_read_audio ra;


			sony_get_toc();
			if (!sony_toc_read) {
				return -EIO;
			}

			if (copy_from_user(&ra, (char *) arg, sizeof(ra)))
				return -EFAULT;

			if (ra.nframes == 0) {
				return 0;
			}

			i = verify_area(VERIFY_WRITE, ra.buf,
					CD_FRAMESIZE_RAW * ra.nframes);
			if (i < 0)
				return i;

			if (ra.addr_format == CDROM_LBA) {
				if ((ra.addr.lba >=
				     sony_toc.lead_out_start_lba)
				    || (ra.addr.lba + ra.nframes >=
					sony_toc.lead_out_start_lba)) {
					return -EINVAL;
				}
			} else if (ra.addr_format == CDROM_MSF) {
				if ((ra.addr.msf.minute >= 75)
				    || (ra.addr.msf.second >= 60)
				    || (ra.addr.msf.frame >= 75)) {
					return -EINVAL;
				}

				ra.addr.lba = ((ra.addr.msf.minute * 4500)
					       + (ra.addr.msf.second * 75)
					       + ra.addr.msf.frame);
				if ((ra.addr.lba >=
				     sony_toc.lead_out_start_lba)
				    || (ra.addr.lba + ra.nframes >=
					sony_toc.lead_out_start_lba)) {
					return -EINVAL;
				}

				/* I know, this can go negative on an unsigned.  However,
				   the first thing done to the data is to add this value,
				   so this should compensate and allow direct msf access. */
				ra.addr.lba -= LOG_START_OFFSET;
			} else {
				return -EINVAL;
			}

			return (read_audio(&ra));
		}
		return 0;
		break;

	default:
		return -EINVAL;
	}
}

static int scd_spinup(void)
{
	unsigned char res_reg[12];
	unsigned int res_size;
	int num_spin_ups;

	num_spin_ups = 0;

      respinup_on_open:
	do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);

	/* The drive sometimes returns error 0.  I don't know why, but ignore
	   it.  It seems to mean the drive has already done the operation. */
	if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
		printk("Sony CDROM %s error (scd_open, spin up)\n",
		       translate_error(res_reg[1]));
		return 1;
	}

	do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);

	/* The drive sometimes returns error 0.  I don't know why, but ignore
	   it.  It seems to mean the drive has already done the operation. */
	if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
		/* If the drive is already playing, it's ok.  */
		if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
		    || (res_reg[1] == 0)) {
			return 0;
		}

		/* If the drive says it is not spun up (even though we just did it!)
		   then retry the operation at least a few times. */
		if ((res_reg[1] == SONY_NOT_SPIN_ERR)
		    && (num_spin_ups < MAX_CDU31A_RETRIES)) {
			num_spin_ups++;
			goto respinup_on_open;
		}

		printk("Sony CDROM error %s (scd_open, read toc)\n",
		       translate_error(res_reg[1]));
		do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
			       &res_size);
		return 1;
	}
	return 0;
}

/*
 * Open the drive for operations.  Spin the drive up and read the table of
 * contents if these have not already been done.
 */
static int scd_open(struct cdrom_device_info *cdi, int openmode)
{
	unsigned char res_reg[12];
	unsigned int res_size;
	unsigned char params[2];

	MOD_INC_USE_COUNT;
	if (sony_usage == 0) {
		if (scd_spinup() != 0) {
			MOD_DEC_USE_COUNT;
			return -EIO;
		}
		sony_get_toc();
		if (!sony_toc_read) {
			do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0,
				       res_reg, &res_size);
			MOD_DEC_USE_COUNT;
			return -EIO;
		}

		/* For XA on the CDU31A only, we have to do special reads.
		   The CDU33A handles XA automagically. */
		/* if (   (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
		if ((sony_toc.disk_type != 0x00)
		    && (!is_double_speed)) {
			params[0] = SONY_SD_DECODE_PARAM;
			params[1] = 0x07;
			do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
				       params, 2, res_reg, &res_size);
			if ((res_size < 2)
			    || ((res_reg[0] & 0xf0) == 0x20)) {
				printk
				    ("CDU31A: Unable to set XA params: 0x%2.2x\n",
				     res_reg[1]);
			}
			sony_xa_mode = 1;
		}
		/* A non-XA disk.  Set the parms back if necessary. */
		else if (sony_xa_mode) {
			params[0] = SONY_SD_DECODE_PARAM;
			params[1] = 0x0f;
			do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
				       params, 2, res_reg, &res_size);
			if ((res_size < 2)
			    || ((res_reg[0] & 0xf0) == 0x20)) {
				printk
				    ("CDU31A: Unable to reset XA params: 0x%2.2x\n",
				     res_reg[1]);
			}
			sony_xa_mode = 0;
		}

		sony_spun_up = 1;
	}

	sony_usage++;

	return 0;
}


/*
 * Close the drive.  Spin it down if no task is using it.  The spin
 * down will fail if playing audio, so audio play is OK.
 */
static void scd_release(struct cdrom_device_info *cdi)
{
	if (sony_usage == 1) {
		unsigned char res_reg[12];
		unsigned int res_size;

		do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
			       &res_size);

		sony_spun_up = 0;
	}
	sony_usage--;
	MOD_DEC_USE_COUNT;
}

static struct cdrom_device_ops scd_dops = {
	open:scd_open,
	release:scd_release,
	drive_status:scd_drive_status,
	media_changed:scd_media_changed,
	tray_move:scd_tray_move,
	lock_door:scd_lock_door,
	select_speed:scd_select_speed,
	get_last_session:scd_get_last_session,
	get_mcn:scd_get_mcn,
	reset:scd_reset,
	audio_ioctl:scd_audio_ioctl,
	dev_ioctl:scd_dev_ioctl,
	capability:CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK |
	    CDC_SELECT_SPEED | CDC_MULTI_SESSION |
	    CDC_MULTI_SESSION | CDC_MCN |
	    CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
	    CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS,
	n_minors:1,
};

static struct cdrom_device_info scd_info = {
	ops:&scd_dops,
	speed:2,
	capacity:1,
	name:"cdu31a"
};

/* The different types of disc loading mechanisms supported */
static char *load_mech[] __initdata =
    { "caddy", "tray", "pop-up", "unknown" };

static void __init
get_drive_configuration(unsigned short base_io,
			unsigned char res_reg[], unsigned int *res_size)
{
	int retry_count;


	/* Set the base address */
	cdu31a_port = base_io;

	/* Set up all the register locations */
	sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
	sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
	sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
	sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
	sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
	sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
	sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
	sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;

	/*
	 * Check to see if anything exists at the status register location.
	 * I don't know if this is a good way to check, but it seems to work
	 * ok for me.
	 */
	if (read_status_register() != 0xff) {
		/*
		 * Reset the drive and wait for attention from it (to say it's reset).
		 * If you don't wait, the next operation will probably fail.
		 */
		reset_drive();
		retry_count = jiffies + SONY_RESET_TIMEOUT;
		while (time_before(jiffies, retry_count)
		       && (!is_attention())) {
			sony_sleep();
		}

#if 0
		/* If attention is never seen probably not a CDU31a present */
		if (!is_attention()) {
			res_reg[0] = 0x20;
			return;
		}
#endif

		/*
		 * Get the drive configuration.
		 */
		do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
			       NULL,
			       0, (unsigned char *) res_reg, res_size);
		return;
	}

	/* Return an error */
	res_reg[0] = 0x20;
}

#ifndef MODULE
/*
 * Set up base I/O and interrupts, called from main.c.
 
 */

static int __init cdu31a_setup(char *strings)
{
	int ints[4];

	(void) get_options(strings, ARRAY_SIZE(ints), ints);

	if (ints[0] > 0) {
		cdu31a_port = ints[1];
	}
	if (ints[0] > 1) {
		cdu31a_irq = ints[2];
	}
	if ((strings != NULL) && (*strings != '\0')) {
		if (strcmp(strings, "PAS") == 0) {
			sony_pas_init = 1;
		} else {
			printk("CDU31A: Unknown interface type: %s\n",
			       strings);
		}
	}

	return 1;
}

__setup("cdu31a=", cdu31a_setup);

#endif

static int cdu31a_block_size;

/*
 * Initialize the driver.
 */
int __init cdu31a_init(void)
{
	struct s_sony_drive_config drive_config;
	unsigned int res_size;
	char msg[255];
	char buf[40];
	int i;
	int drive_found;
	int tmp_irq;


	/*
	 * According to Alex Freed (freed@europa.orion.adobe.com), this is
	 * required for the Fusion CD-16 package.  If the sound driver is
	 * loaded, it should work fine, but just in case...
	 *
	 * The following turn on the CD-ROM interface for a Fusion CD-16.
	 */
	if (sony_pas_init) {
		outb(0xbc, 0x9a01);
		outb(0xe2, 0x9a01);
	}

	drive_found = 0;

	/* Setting the base I/O address to 0xffff will disable it. */
	if (cdu31a_port == 0xffff) {
	} else if (cdu31a_port != 0) {
		tmp_irq = cdu31a_irq;	/* Need IRQ 0 because we can't sleep here. */
		cdu31a_irq = 0;

		get_drive_configuration(cdu31a_port,
					drive_config.exec_status,
					&res_size);
		if ((res_size > 2)
		    && ((drive_config.exec_status[0] & 0xf0) == 0x00)) {
			drive_found = 1;
		}

		cdu31a_irq = tmp_irq;
	} else {
		cdu31a_irq = 0;
		i = 0;
		while ((cdu31a_addresses[i].base != 0)
		       && (!drive_found)) {
			if (check_region(cdu31a_addresses[i].base, 4)) {
				i++;
				continue;
			}
			get_drive_configuration(cdu31a_addresses[i].base,
						drive_config.exec_status,
						&res_size);
			if ((res_size > 2)
			    && ((drive_config.exec_status[0] & 0xf0) ==
				0x00)) {
				drive_found = 1;
				cdu31a_irq = cdu31a_addresses[i].int_num;
			} else {
				i++;
			}
		}
	}

	if (drive_found) {
		int deficiency = 0;

		request_region(cdu31a_port, 4, "cdu31a");

		if (devfs_register_blkdev(MAJOR_NR, "cdu31a", &cdrom_fops)) {
			printk("Unable to get major %d for CDU-31a\n",
			       MAJOR_NR);
			goto errout2;
		}

		if (SONY_HWC_DOUBLE_SPEED(drive_config)) {
			is_double_speed = 1;
		}

		tmp_irq = cdu31a_irq;	/* Need IRQ 0 because we can't sleep here. */
		cdu31a_irq = 0;

		set_drive_params(sony_speed);

		cdu31a_irq = tmp_irq;

		if (cdu31a_irq > 0) {
			if (request_irq
			    (cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT,
			     "cdu31a", NULL)) {
				printk
				    ("Unable to grab IRQ%d for the CDU31A driver\n",
				     cdu31a_irq);
				cdu31a_irq = 0;
			}
		}

		sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
			drive_config.vendor_id,
			drive_config.product_id,
			drive_config.product_rev_level);
		sprintf(buf, "  Capabilities: %s",
			load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
		strcat(msg, buf);
		if (SONY_HWC_AUDIO_PLAYBACK(drive_config)) {
			strcat(msg, ", audio");
		} else
			deficiency |= CDC_PLAY_AUDIO;
		if (SONY_HWC_EJECT(drive_config)) {
			strcat(msg, ", eject");
		} else
			deficiency |= CDC_OPEN_TRAY;
		if (SONY_HWC_LED_SUPPORT(drive_config)) {
			strcat(msg, ", LED");
		}
		if (SONY_HWC_ELECTRIC_VOLUME(drive_config)) {
			strcat(msg, ", elec. Vol");
		}
		if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config)) {
			strcat(msg, ", sep. Vol");
		}
		if (is_double_speed) {
			strcat(msg, ", double speed");
		} else
			deficiency |= CDC_SELECT_SPEED;
		if (cdu31a_irq > 0) {
			sprintf(buf, ", irq %d", cdu31a_irq);
			strcat(msg, buf);
		}
		strcat(msg, "\n");

		is_a_cdu31a =
		    strcmp("CD-ROM CDU31A", drive_config.product_id) == 0;

		blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR),
			       DEVICE_REQUEST);
		read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
		cdu31a_block_size = 1024;	/* 1kB default block size */
		/* use 'mount -o block=2048' */
		blksize_size[MAJOR_NR] = &cdu31a_block_size;

		init_timer(&cdu31a_abort_timer);
		cdu31a_abort_timer.function = handle_abort_timeout;

		scd_info.dev = MKDEV(MAJOR_NR, 0);
		scd_info.mask = deficiency;
		strncpy(scd_info.name, "cdu31a", sizeof(scd_info.name));

		if (register_cdrom(&scd_info)) {
			goto errout0;
		}
	}


	disk_changed = 1;

	if (drive_found) {
		return (0);
	} else {
		goto errout3;
	}
      errout0:
	printk("Unable to register CDU-31a with Uniform cdrom driver\n");
	blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
	if (devfs_unregister_blkdev(MAJOR_NR, "cdu31a")) {
		printk("Can't unregister block device for cdu31a\n");
	}
      errout2:
	release_region(cdu31a_port, 4);
      errout3:
	return -EIO;
}


void __exit cdu31a_exit(void)
{
	if (unregister_cdrom(&scd_info)) {
		printk
		    ("Can't unregister cdu31a from Uniform cdrom driver\n");
		return;
	}
	if ((devfs_unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL)) {
		printk("Can't unregister cdu31a\n");
		return;
	}

	blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));

	if (cdu31a_irq > 0)
		free_irq(cdu31a_irq, NULL);

	release_region(cdu31a_port, 4);
	printk(KERN_INFO "cdu31a module released.\n");
}

#ifdef MODULE
module_init(cdu31a_init);
#endif
module_exit(cdu31a_exit);

MODULE_LICENSE("GPL");
EXPORT_NO_SYMBOLS;