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
/*
* Sony CDU-31A CDROM interface device driver.
*
* Corey Minyard (minyard@wf-rch.cirr.com)
*
* Colossians 3:17
*
* 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.
*
* This interface is (unfortunatly) 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 ((retry_count > jiffies) && (! <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
* asyncronous events from the drive informing the driver that a disk
* has been inserted, removed, etc.
*
* 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.
*
* 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.
*
*/
#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 <asm/system.h>
#include <asm/io.h>
#include <asm/segment.h>
#include <linux/cdrom.h>
#include <linux/cdu31a.h>
#define MAJOR_NR CDU31A_CDROM_MAJOR
#include "blk.h"
#define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
static unsigned short cdu31a_addresses[] =
{
0x340, /* Standard configuration Sony Interface */
0x1f88, /* Fusion CD-16 */
0x230, /* SoundBlaster 16 card */
0x360, /* Secondary standard Sony Interface */
0x320, /* Secondary standard Sony Interface */
0x330, /* Secondary standard Sony Interface */
0
};
static int handle_sony_cd_attention(void);
static int read_subcode(void);
static void sony_get_toc(void);
static int scd_open(struct inode *inode, struct file *filp);
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);
/* 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 short sony_cd_base_io = 0;
/*
* 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_disc_changed = 1; /* Has the disk been changed
since the last check? */
static int sony_toc_read = 0; /* Has the table of contents been
read? */
static int sony_spun_up = 0; /* Has the drive been spun up? */
static unsigned int sony_buffer_size; /* Size in bytes of the read-ahead
buffer. */
static unsigned int sony_buffer_sectors; /* Size (in 2048 byte records) of
the read-ahead buffer. */
static unsigned int sony_usage = 0; /* How many processes have the
drive open. */
static volatile int sony_first_block = -1; /* First OS block (512 byte) in
the read-ahead buffer */
static volatile int sony_last_block = -1; /* Last OS block (512 byte) in
the read-ahead buffer */
static struct s_sony_toc *sony_toc; /* Points to the table of
contents. */
static struct s_sony_subcode * volatile last_sony_subcode; /* Points to the last
subcode address read */
static unsigned char * volatile sony_buffer; /* Points to the read-ahead
buffer */
static volatile int sony_inuse = 0; /* Is the drive in use? Only one operation at a time
allowed */
static struct wait_queue * sony_wait = NULL;
static struct task_struct *has_cd_task = NULL; /* The task that is currently using the
CDROM drive, or NULL if none. */
/*
* 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.
*/
unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
/*
* This routine returns 1 if the disk has been changed since the last
* check or 0 if it hasn't. Setting flag to 0 resets the changed flag.
*/
int
check_cdu31a_media_change(int full_dev, int flag)
{
int retval, target;
target = MINOR(full_dev);
if (target > 0) {
printk("Sony CD-ROM request error: invalid device.\n");
return 0;
}
retval = sony_disc_changed;
if (!flag)
{
sony_disc_changed = 0;
}
return retval;
}
/*
* 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)
{
current->state = TASK_INTERRUPTIBLE;
current->timeout = jiffies;
schedule();
}
/*
* 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 void
reset_drive(void)
{
outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
}
static inline void
clear_attention(void)
{
outb(SONY_ATTN_CLR_BIT, sony_cd_control_reg);
}
static inline void
clear_result_ready(void)
{
outb(SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
}
static inline void
clear_data_ready(void)
{
outb(SONY_DATA_RDY_CLR_BIT, sony_cd_control_reg);
}
static inline void
clear_param_reg(void)
{
outb(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(cmd, sony_cd_cmd_reg);
outb(SONY_RES_RDY_INT_EN_BIT, sony_cd_control_reg);
}
/*
* Set the drive parameters so the drive will auto-spin-up when a
* disk is inserted.
*/
static void
set_drive_params(void)
{
unsigned char res_reg[2];
unsigned int res_size;
unsigned char params[3];
params[0] = SONY_SD_MECH_CONTROL;
params[1] = 0x03;
do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
params,
2,
res_reg,
&res_size);
if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
{
printk(" Unable to set mechanical parameters: 0x%2.2x\n", res_reg[1]);
}
}
/*
* This code will reset the drive and attempt to restore sane parameters.
*/
static void
restart_on_error(void)
{
unsigned char res_reg[2];
unsigned int res_size;
unsigned int retry_count;
printk("cdu31a: Resetting drive on error\n");
reset_drive();
retry_count = jiffies + SONY_RESET_TIMEOUT;
while ((retry_count > jiffies) && (!is_attention()))
{
sony_sleep();
}
set_drive_params();
do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
{
printk("cdu31a: Unable to spin up drive: 0x%2.2x\n", res_reg[1]);
}
current->state = TASK_INTERRUPTIBLE;
current->timeout = jiffies + 200;
schedule();
do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
{
printk("cdu31a: Unable to read TOC: 0x%2.2x\n", res_reg[1]);
}
sony_get_toc();
if (!sony_toc_read)
{
printk("cdu31a: Unable to get TOC data\n");
}
}
/*
* 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 ((retry_count > jiffies) && (is_busy() || (!(is_result_ready()))))
{
sony_sleep();
while (handle_sony_cd_attention())
;
}
if (is_busy() || (!(is_result_ready())))
{
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++;
b = read_result_register();
*result_buffer = b;
result_buffer++;
*result_size = 2;
/*
* 0x20 means an error occured. Byte 2 will have the error code.
* Otherwise, the command succeded, 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())
{
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())
{
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--;
}
}
}
/*
* Read in a 2048 byte block of data.
*/
static void
read_data_block(unsigned char *data,
unsigned char *result_buffer,
unsigned int *result_size)
{
int i;
unsigned int retry_count;
for (i=0; i<2048; i++)
{
retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
while ((retry_count > jiffies) && (!is_data_requested()))
{
while (handle_sony_cd_attention())
;
sony_sleep();
}
if (!is_data_requested())
{
result_buffer[0] = 0x20;
result_buffer[1] = SONY_TIMEOUT_OP_ERR;
*result_size = 2;
return;
}
*data = read_data_register();
data++;
}
}
/*
* This routine issues a read data command and gets the data. I don't
* really like the way this is done (I would prefer for do_sony_cmd() to
* handle it automatically) but I found that the drive returns status
* when it finishes reading (not when the host has read all the data)
* or after it gets an error. This means that the status can be
* received at any time and should be handled immediately (at least
* between every 2048 byte block) to check for errors, we can't wait
* until all the data is read.
*
* This routine returns the total number of sectors read. It will
* not return an error if it reads at least one sector successfully.
*/
static unsigned int
get_data(unsigned char *orig_data,
unsigned char *params, /* 6 bytes with the MSF start address
and number of sectors to read. */
unsigned int orig_data_size,
unsigned char *result_buffer,
unsigned int *result_size)
{
unsigned int cur_offset;
unsigned int retry_count;
int result_read;
int num_retries;
unsigned int num_sectors_read = 0;
unsigned char *data = orig_data;
unsigned int data_size = orig_data_size;
cli();
while (sony_inuse)
{
interruptible_sleep_on(&sony_wait);
if (current->signal & ~current->blocked)
{
result_buffer[0] = 0x20;
result_buffer[1] = SONY_SIGNAL_OP_ERR;
*result_size = 2;
return 0;
}
}
sony_inuse = 1;
has_cd_task = current;
sti();
num_retries = 0;
retry_data_operation:
result_buffer[0] = 0;
result_buffer[1] = 0;
/*
* 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 ((retry_count > jiffies) && (is_busy()))
{
sony_sleep();
while (handle_sony_cd_attention())
;
}
if (is_busy())
{
result_buffer[0] = 0x20;
result_buffer[1] = SONY_TIMEOUT_OP_ERR;
*result_size = 2;
}
else
{
/* Issue the command */
clear_result_ready();
clear_param_reg();
write_params(params, 6);
write_cmd(SONY_READ_CMD);
/*
* Read the data from the drive one 2048 byte sector at a time. Handle
* any results received between sectors, if an error result is returned
* terminate the operation immediately.
*/
cur_offset = 0;
result_read = 0;
while ((data_size > 0) && (result_buffer[0] == 0))
{
/* Wait for the drive to tell us we have something */
retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
while ((retry_count > jiffies) && (!(is_result_ready() || is_data_ready())))
{
while (handle_sony_cd_attention())
;
sony_sleep();
}
if (!(is_result_ready() || is_data_ready()))
{
result_buffer[0] = 0x20;
result_buffer[1] = SONY_TIMEOUT_OP_ERR;
*result_size = 2;
}
/* Handle results first */
else if (is_result_ready())
{
result_read = 1;
get_result(result_buffer, result_size);
}
else /* Handle data next */
{
/*
* The drive has to be polled for status on a byte-by-byte basis
* to know if the data is ready. Yuck. I really wish I could use DMA.
*/
clear_data_ready();
read_data_block(data, result_buffer, result_size);
data += 2048;
data_size -= 2048;
cur_offset = cur_offset + 2048;
num_sectors_read++;
}
}
/* Make sure the result has been read */
if (!result_read)
{
get_result(result_buffer, result_size);
}
}
if ( ((result_buffer[0] & 0x20) == 0x20)
&& (result_buffer[1] != SONY_NOT_SPIN_ERR) /* No retry when not spin */
&& (num_retries < MAX_CDU31A_RETRIES))
{
/*
* If an error occurs, go back and only read one sector at the
* given location. Hopefully the error occurred on an unused
* sector after the first one. It is hard to say which sector
* the error occurred on because the drive returns status before
* the data transfer is finished and doesn't say which sector.
*/
data_size = 2048;
data = orig_data;
num_sectors_read = 0;
size_to_buf(1, ¶ms[3]);
num_retries++;
/* Issue a reset on an error (the second time), othersize just delay */
if (num_retries == 2)
{
restart_on_error();
}
else
{
current->state = TASK_INTERRUPTIBLE;
current->timeout = jiffies + 10;
schedule();
}
/* Restart the operation. */
goto retry_data_operation;
}
has_cd_task = NULL;
sony_inuse = 0;
wake_up_interruptible(&sony_wait);
return(num_sectors_read);
}
/*
* 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;
cli();
if (current != has_cd_task) /* Allow recursive calls to this routine */
{
while (sony_inuse)
{
interruptible_sleep_on(&sony_wait);
if (current->signal & ~current->blocked)
{
result_buffer[0] = 0x20;
result_buffer[1] = SONY_SIGNAL_OP_ERR;
*result_size = 2;
return;
}
}
sony_inuse = 1;
has_cd_task = current;
recursive_call = 0;
}
else
{
recursive_call = 1;
}
sti();
num_retries = 0;
retry_cd_operation:
while (handle_sony_cd_attention())
;
retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
while ((retry_count > jiffies) && (is_busy()))
{
sony_sleep();
while (handle_sony_cd_attention())
;
}
if (is_busy())
{
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] & 0x20) == 0x20)
&& (num_retries < MAX_CDU31A_RETRIES))
{
num_retries++;
current->state = TASK_INTERRUPTIBLE;
current->timeout = jiffies + 10; /* Wait .1 seconds on retries */
schedule();
goto retry_cd_operation;
}
if (!recursive_call)
{
has_cd_task = NULL;
sony_inuse = 0;
wake_up_interruptible(&sony_wait);
}
}
/*
* 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;
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;
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:
sony_disc_changed = 1;
sony_toc_read = 0;
sony_audio_status = CDROM_AUDIO_NO_STATUS;
sony_first_block = -1;
sony_last_block = -1;
break;
case SONY_AUDIO_PLAY_DONE_ATTN:
sony_audio_status = CDROM_AUDIO_COMPLETED;
read_subcode();
break;
case SONY_EJECT_PUSHED_ATTN:
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++;
return(1);
}
num_consecutive_attentions = 0;
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 = bcd_to_int(msf[2]);
log += bcd_to_int(msf[1]) * 75;
log += bcd_to_int(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;
}
/*
* 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(void)
{
int block;
unsigned int dev;
int nsect;
unsigned char params[10];
unsigned char res_reg[2];
unsigned int res_size;
int copyoff;
int spin_up_retry;
unsigned int read_size;
if (!sony_spun_up)
{
scd_open (NULL,NULL);
}
while (1)
{
cdu31a_request_startover:
/*
* The beginning here is stolen from the hard disk driver. I hope
* its right.
*/
if (!(CURRENT) || CURRENT->dev < 0)
{
return;
}
INIT_REQUEST;
dev = MINOR(CURRENT->dev);
block = CURRENT->sector;
nsect = CURRENT->nr_sectors;
if (dev != 0)
{
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 ((block / 4) >= sony_toc->lead_out_start_lba)
{
end_request(0);
goto cdu31a_request_startover;
}
if (((block + nsect) / 4) >= sony_toc->lead_out_start_lba)
{
end_request(0);
goto cdu31a_request_startover;
}
while (nsect > 0)
{
/*
* If the requested sector is not currently in the read-ahead buffer,
* it must be read in.
*/
if ((block < sony_first_block) || (block > sony_last_block))
{
sony_first_block = (block / 4) * 4;
log_to_msf(block/4, params);
/*
* 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.
*/
if (((block / 4) + sony_buffer_sectors) >= sony_toc->lead_out_start_lba)
{
read_size = sony_toc->lead_out_start_lba - (block / 4);
}
else
{
read_size = sony_buffer_sectors;
}
size_to_buf(read_size, ¶ms[3]);
/*
* Read the data. If the drive was not spinning, spin it up and try
* once more. I know, the goto is ugly, but I am too lazy to fix it.
*/
spin_up_retry = 0;
try_read_again:
sony_last_block = sony_first_block
+ (get_data(sony_buffer,
params,
(read_size * 2048),
res_reg,
&res_size) * 4) - 1;
if ((res_size < 2) || (res_reg[0] != 0))
{
if ((res_reg[1] == SONY_NOT_SPIN_ERR) && (!spin_up_retry))
{
do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
spin_up_retry = 1;
goto try_read_again;
}
printk("Sony CDROM Read error: 0x%2.2x\n", res_reg[1]);
sony_first_block = -1;
sony_last_block = -1;
end_request(0);
goto cdu31a_request_startover;
}
}
/*
* The data is in memory now, copy it to the buffer and advance to the
* next block to read.
*/
copyoff = (block - sony_first_block) * 512;
memcpy(CURRENT->buffer, sony_buffer+copyoff, 512);
block += 1;
nsect -= 1;
CURRENT->buffer += 512;
}
end_request(1);
break;
case WRITE:
end_request(0);
break;
default:
panic("Unkown SONY CD cmd");
}
}
}
/*
* Read the table of contents from the drive and set sony_toc_read if
* successful.
*/
static void
sony_get_toc(void)
{
unsigned int res_size;
if (!sony_toc_read)
{
do_sony_cd_cmd(SONY_REQ_TOC_DATA_CMD,
NULL,
0,
(unsigned char *) sony_toc,
&res_size);
if ((res_size < 2) || ((sony_toc->exec_status[0] & 0x20) == 0x20))
{
return;
}
sony_toc->lead_out_start_lba = msf_to_log(sony_toc->lead_out_start_msf);
sony_toc_read = 1;
}
}
/*
* Search for a specific track in the table of contents.
*/
static int
find_track(int track)
{
int i;
int num_tracks;
num_tracks = sony_toc->last_track_num + sony_toc->first_track_num + 1;
for (i = 0; i < num_tracks; i++)
{
if (sony_toc->tracks[i].track == track)
{
return i;
}
}
return -1;
}
/*
* Read the subcode and put it int 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] & 0x20) == 0x20))
{
printk("Sony CDROM error 0x%2.2x (read_subcode)\n",
last_sony_subcode->exec_status[1]);
return -EIO;
}
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(long arg)
{
struct cdrom_subchnl schi;
/* Get attention stuff */
while (handle_sony_cd_attention())
;
sony_get_toc();
if (!sony_toc_read)
{
return -EIO;
}
verify_area(VERIFY_READ, (char *) arg, sizeof(schi));
verify_area(VERIFY_WRITE, (char *) arg, sizeof(schi));
memcpy_fromfs(&schi, (char *) arg, sizeof(schi));
switch (sony_audio_status)
{
case CDROM_AUDIO_PLAY:
if (read_subcode() < 0)
{
return -EIO;
}
break;
case CDROM_AUDIO_PAUSED:
case CDROM_AUDIO_COMPLETED:
break;
case CDROM_AUDIO_NO_STATUS:
schi.cdsc_audiostatus = sony_audio_status;
memcpy_tofs((char *) arg, &schi, sizeof(schi));
return 0;
break;
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 = bcd_to_int(last_sony_subcode->track_num);
schi.cdsc_ind = bcd_to_int(last_sony_subcode->index_num);
if (schi.cdsc_format == CDROM_MSF)
{
schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode->abs_msf[0]);
schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode->abs_msf[1]);
schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode->abs_msf[2]);
schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode->rel_msf[0]);
schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode->rel_msf[1]);
schi.cdsc_reladdr.msf.frame = bcd_to_int(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);
}
memcpy_tofs((char *) arg, &schi, sizeof(schi));
return 0;
}
/*
* The big ugly ioctl handler.
*/
static int
scd_ioctl(struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
unsigned int dev;
unsigned char res_reg[2];
unsigned int res_size;
unsigned char params[7];
int i;
if (!inode)
{
return -EINVAL;
}
dev = MINOR(inode->i_rdev) >> 6;
if (dev != 0)
{
return -EINVAL;
}
switch (cmd)
{
case CDROMSTART: /* Spin up the drive */
do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
{
printk("Sony CDROM error 0x%2.2x (CDROMSTART)\n", res_reg[1]);
return -EIO;
}
return 0;
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;
do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
if ( ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
&& (res_reg[1] != SONY_NOT_SPIN_ERR))
{
printk("Sony CDROM error 0x%2.2x (CDROMSTOP)\n", res_reg[1]);
return -EIO;
}
return 0;
break;
case CDROMPAUSE: /* Pause the drive */
do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
{
printk("Sony CDROM error 0x%2.2x (CDROMPAUSE)\n", res_reg[1]);
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] = cur_pos_msf[0];
params[2] = cur_pos_msf[1];
params[3] = cur_pos_msf[2];
params[4] = final_pos_msf[0];
params[5] = final_pos_msf[1];
params[6] = final_pos_msf[2];
params[0] = 0x03;
do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
{
printk("Sony CDROM error 0x%2.2x (CDROMRESUME)\n", res_reg[1]);
return -EIO;
}
sony_audio_status = CDROM_AUDIO_PLAY;
return 0;
break;
case CDROMPLAYMSF: /* Play starting at the given MSF address. */
verify_area(VERIFY_READ, (char *) arg, 6);
do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
memcpy_fromfs(&(params[1]), (void *) arg, 6);
/* The parameters are given in int, must be converted */
for (i=1; i<7; i++)
{
params[i] = int_to_bcd(params[i]);
}
params[0] = 0x03;
do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
{
printk("Sony CDROM error 0x%2.2x (CDROMPLAYMSF)\n", res_reg[1]);
return -EIO;
}
/* Save the final position for pauses and resumes */
final_pos_msf[0] = params[4];
final_pos_msf[1] = params[5];
final_pos_msf[2] = params[6];
sony_audio_status = CDROM_AUDIO_PLAY;
return 0;
break;
case CDROMREADTOCHDR: /* Read the table of contents header */
{
struct cdrom_tochdr *hdr;
struct cdrom_tochdr loc_hdr;
sony_get_toc();
if (!sony_toc_read)
{
return -EIO;
}
hdr = (struct cdrom_tochdr *) arg;
verify_area(VERIFY_WRITE, hdr, sizeof(*hdr));
loc_hdr.cdth_trk0 = bcd_to_int(sony_toc->first_track_num);
loc_hdr.cdth_trk1 = bcd_to_int(sony_toc->last_track_num);
memcpy_tofs(hdr, &loc_hdr, sizeof(*hdr));
}
return 0;
break;
case CDROMREADTOCENTRY: /* Read a given table of contents entry */
{
struct cdrom_tocentry *entry;
struct cdrom_tocentry loc_entry;
int track_idx;
unsigned char *msf_val = NULL;
sony_get_toc();
if (!sony_toc_read)
{
return -EIO;
}
entry = (struct cdrom_tocentry *) arg;
verify_area(VERIFY_READ, entry, sizeof(*entry));
verify_area(VERIFY_WRITE, entry, sizeof(*entry));
memcpy_fromfs(&loc_entry, entry, sizeof(loc_entry));
/* Lead out is handled separately since it is special. */
if (loc_entry.cdte_track == CDROM_LEADOUT)
{
loc_entry.cdte_adr = sony_toc->address2;
loc_entry.cdte_ctrl = sony_toc->control2;
msf_val = sony_toc->lead_out_start_msf;
}
else
{
track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
if (track_idx < 0)
{
return -EINVAL;
}
loc_entry.cdte_adr = sony_toc->tracks[track_idx].address;
loc_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 (loc_entry.cdte_format == CDROM_LBA)
{
loc_entry.cdte_addr.lba = msf_to_log(msf_val);
}
else if (loc_entry.cdte_format == CDROM_MSF)
{
loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val+1));
loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val+2));
}
memcpy_tofs(entry, &loc_entry, sizeof(*entry));
}
return 0;
break;
case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
{
struct cdrom_ti ti;
int track_idx;
sony_get_toc();
if (!sony_toc_read)
{
return -EIO;
}
verify_area(VERIFY_READ, (char *) arg, sizeof(ti));
memcpy_fromfs(&ti, (char *) arg, sizeof(ti));
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(int_to_bcd(ti.cdti_trk0));
if (track_idx < 0)
{
return -EINVAL;
}
params[1] = sony_toc->tracks[track_idx].track_start_msf[0];
params[2] = sony_toc->tracks[track_idx].track_start_msf[1];
params[3] = 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 >= bcd_to_int(sony_toc->last_track_num))
{
log_to_msf(msf_to_log(sony_toc->lead_out_start_msf)-1,
&(params[4]));
}
else
{
track_idx = find_track(int_to_bcd(ti.cdti_trk1+1));
if (track_idx < 0)
{
return -EINVAL;
}
log_to_msf(msf_to_log(sony_toc->tracks[track_idx].track_start_msf)-1,
&(params[4]));
}
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] & 0x20) == 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 0x%2.2x (CDROMPLAYTRKIND\n", res_reg[1]);
return -EIO;
}
/* Save the final position for pauses and resumes */
final_pos_msf[0] = params[4];
final_pos_msf[1] = params[5];
final_pos_msf[2] = params[6];
sony_audio_status = CDROM_AUDIO_PLAY;
return 0;
}
case CDROMSUBCHNL: /* Get subchannel info */
return sony_get_subchnl_info(arg);
case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */
{
struct cdrom_volctrl volctrl;
verify_area(VERIFY_READ, (char *) arg, sizeof(volctrl));
memcpy_fromfs(&volctrl, (char *) arg, sizeof(volctrl));
params[0] = SONY_SD_AUDIO_VOLUME;
params[1] = volctrl.channel0;
params[2] = volctrl.channel1;
do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, params, 3, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
{
printk("Sony CDROM error 0x%2.2x (CDROMVOLCTRL)\n", res_reg[1]);
return -EIO;
}
}
return 0;
case CDROMEJECT: /* Eject the drive */
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;
do_sony_cd_cmd(SONY_EJECT_CMD, NULL, 0, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
{
printk("Sony CDROM error 0x%2.2x (CDROMEJECT)\n", res_reg[1]);
return -EIO;
}
return 0;
break;
default:
return -EINVAL;
}
}
/*
* 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 inode *inode,
struct file *filp)
{
unsigned char res_reg[2];
unsigned int res_size;
int num_spin_ups;
if (!sony_spun_up)
{
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 error 0x%2.2x (scd_open, spin up)\n", res_reg[1]);
return -EIO;
}
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, its ok. */
if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
{
goto 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_open;
}
printk("Sony CDROM error 0x%2.2x (scd_open, read toc)\n", res_reg[1]);
do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
return -EIO;
}
sony_get_toc();
if (!sony_toc_read)
{
do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
return -EIO;
}
sony_spun_up = 1;
}
drive_spinning:
if (inode)
{
check_disk_change(inode->i_rdev);
}
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 inode *inode,
struct file *filp)
{
unsigned char res_reg[2];
unsigned int res_size;
if (sony_usage > 0)
{
sony_usage--;
}
if (sony_usage == 0)
{
sync_dev(inode->i_rdev);
do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
sony_spun_up = 0;
}
}
static struct file_operations scd_fops = {
NULL, /* lseek - default */
block_read, /* read - general block-dev read */
block_write, /* write - general block-dev write */
NULL, /* readdir - bad */
NULL, /* select */
scd_ioctl, /* ioctl */
NULL, /* mmap */
scd_open, /* open */
scd_release, /* release */
NULL /* fsync */
};
/* The different types of disc loading mechanisms supported */
static char *load_mech[] = { "caddy", "tray", "pop-up", "unknown" };
/* Read-ahead buffer sizes for different drives. These are just arbitrary
values, I don't know what is really optimum. */
static unsigned int mem_size[] = { 16384, 16384, 16384, 2048 };
void
get_drive_configuration(unsigned short base_io,
unsigned char res_reg[],
unsigned int *res_size)
{
int retry_count;
/* Set the base address */
sony_cd_base_io = base_io;
/* Set up all the register locations */
sony_cd_cmd_reg = sony_cd_base_io + SONY_CMD_REG_OFFSET;
sony_cd_param_reg = sony_cd_base_io + SONY_PARAM_REG_OFFSET;
sony_cd_write_reg = sony_cd_base_io + SONY_WRITE_REG_OFFSET;
sony_cd_control_reg = sony_cd_base_io + SONY_CONTROL_REG_OFFSET;
sony_cd_status_reg = sony_cd_base_io + SONY_STATUS_REG_OFFSET;
sony_cd_result_reg = sony_cd_base_io + SONY_RESULT_REG_OFFSET;
sony_cd_read_reg = sony_cd_base_io + SONY_READ_REG_OFFSET;
sony_cd_fifost_reg = sony_cd_base_io + 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 its reset).
* If you don't wait, the next operation will probably fail.
*/
reset_drive();
retry_count = jiffies + SONY_RESET_TIMEOUT;
while ((retry_count > jiffies) && (!is_attention()))
{
sony_sleep();
}
/* If attention is never seen probably not a CDU31a present */
if (!is_attention())
{
res_reg[0] = 0x20;
return;
}
/*
* 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;
}
/*
* Initialize the driver.
*/
unsigned long
cdu31a_init(unsigned long mem_start, unsigned long mem_end)
{
struct s_sony_drive_config drive_config;
unsigned int res_size;
int i;
int drive_found;
/*
* 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.
*/
outb(0xbc, 0x9a01);
outb(0xe2, 0x9a01);
i = 0;
drive_found = 0;
while ( (cdu31a_addresses[i] != 0)
&& (!drive_found))
{
if (check_region(cdu31a_addresses[i], 4)) {
i++;
continue;
}
get_drive_configuration(cdu31a_addresses[i],
drive_config.exec_status,
&res_size);
if ((res_size > 2) && ((drive_config.exec_status[0] & 0x20) == 0x00))
{
drive_found = 1;
snarf_region(cdu31a_addresses[i], 4);
if (register_blkdev(MAJOR_NR,"cdu31a",&scd_fops))
{
printk("Unable to get major %d for CDU-31a\n", MAJOR_NR);
return mem_start;
}
sony_buffer_size = mem_size[SONY_HWC_GET_BUF_MEM_SIZE(drive_config)];
sony_buffer_sectors = sony_buffer_size / 2048;
printk("Sony I/F CDROM : %8.8s %16.16s %8.8s with %s load mechanism\n",
drive_config.vendor_id,
drive_config.product_id,
drive_config.product_rev_level,
load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
printk(" using %d byte buffer", sony_buffer_size);
if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
{
printk(", capable of audio playback");
}
printk("\n");
set_drive_params();
blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read-ahead */
sony_toc = (struct s_sony_toc *) mem_start;
mem_start += sizeof(*sony_toc);
last_sony_subcode = (struct s_sony_subcode *) mem_start;
mem_start += sizeof(*last_sony_subcode);
sony_buffer = (unsigned char *) mem_start;
mem_start += sony_buffer_size;
}
i++;
}
return mem_start;
}