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
|
/*-
* Copyright 1993 by Holger Veit (data part)
* Copyright 1993 by Brian Moore (audio part)
* Changes Copyright 1993 by Gary Clark II
* Changes Copyright (C) 1994-1995 by Andrey A. Chernov, Moscow, Russia
*
* Rewrote probe routine to work on newer Mitsumi drives.
* Additional changes (C) 1994 by Jordan K. Hubbard
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This software was developed by Holger Veit and Brian Moore
* for use with "386BSD" and similar operating systems.
* "Similar operating systems" includes mainly non-profit oriented
* systems for research and education, including but not restricted to
* "NetBSD", "FreeBSD", "Mach" (by CMU).
* 4. Neither the name of the developer(s) nor the name "386BSD"
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
static const char __used COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <sys/bio.h>
#include <sys/cdio.h>
#include <sys/disk.h>
#include <sys/bus.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
#include <isa/isavar.h>
#include <dev/mcd/mcdreg.h>
#include <dev/mcd/mcdvar.h>
#define MCD_TRACE(format, args...) \
{ \
if (sc->debug) { \
device_printf(sc->dev, "status=0x%02x: ", \
sc->data.status); \
printf(format, ## args); \
} \
}
#define RAW_PART 2
/* flags */
#define MCDVALID 0x0001 /* parameters loaded */
#define MCDINIT 0x0002 /* device is init'd */
#define MCDNEWMODEL 0x0004 /* device is new model */
#define MCDLABEL 0x0008 /* label is read */
#define MCDPROBING 0x0010 /* probing */
#define MCDREADRAW 0x0020 /* read raw mode (2352 bytes) */
#define MCDVOLINFO 0x0040 /* already read volinfo */
#define MCDTOC 0x0080 /* already read toc */
#define MCDMBXBSY 0x0100 /* local mbx is busy */
/* status */
#define MCDAUDIOBSY MCD_ST_AUDIOBSY /* playing audio */
#define MCDDSKCHNG MCD_ST_DSKCHNG /* sensed change of disk */
#define MCDDSKIN MCD_ST_DSKIN /* sensed disk in drive */
#define MCDDOOROPEN MCD_ST_DOOROPEN /* sensed door open */
/* These are apparently the different states a mitsumi can get up to */
#define MCDCDABSENT 0x0030
#define MCDCDPRESENT 0x0020
#define MCDSCLOSED 0x0080
#define MCDSOPEN 0x00a0
#define MCD_MD_UNKNOWN (-1)
#define MCD_TYPE_UNKNOWN 0
#define MCD_TYPE_LU002S 1
#define MCD_TYPE_LU005S 2
#define MCD_TYPE_LU006S 3
#define MCD_TYPE_FX001 4
#define MCD_TYPE_FX001D 5
/* reader state machine */
#define MCD_S_BEGIN 0
#define MCD_S_BEGIN1 1
#define MCD_S_WAITSTAT 2
#define MCD_S_WAITMODE 3
#define MCD_S_WAITREAD 4
/* prototypes */
static void mcd_start(struct mcd_softc *);
#ifdef NOTYET
static void mcd_configure(struct mcd_softc *sc);
#endif
static int mcd_get(struct mcd_softc *, char *buf, int nmax);
static int mcd_setflags(struct mcd_softc *);
static int mcd_getstat(struct mcd_softc *,int sflg);
static int mcd_send(struct mcd_softc *, int cmd,int nretrys);
static void hsg2msf(int hsg, bcd_t *msf);
static int msf2hsg(bcd_t *msf, int relative);
static int mcd_volinfo(struct mcd_softc *);
static int mcd_waitrdy(struct mcd_softc *,int dly);
static void mcd_timeout(void *arg);
static void mcd_doread(struct mcd_softc *, int state, struct mcd_mbx *mbxin);
static void mcd_soft_reset(struct mcd_softc *);
static int mcd_hard_reset(struct mcd_softc *);
static int mcd_setmode(struct mcd_softc *, int mode);
static int mcd_getqchan(struct mcd_softc *, struct mcd_qchninfo *q);
static int mcd_subchan(struct mcd_softc *, struct ioc_read_subchannel *sc,
int nocopyout);
static int mcd_toc_header(struct mcd_softc *, struct ioc_toc_header *th);
static int mcd_read_toc(struct mcd_softc *);
static int mcd_toc_entrys(struct mcd_softc *, struct ioc_read_toc_entry *te);
#if 0
static int mcd_toc_entry(struct mcd_softc *, struct ioc_read_toc_single_entry *te);
#endif
static int mcd_stop(struct mcd_softc *);
static int mcd_eject(struct mcd_softc *);
static int mcd_inject(struct mcd_softc *);
static int mcd_playtracks(struct mcd_softc *, struct ioc_play_track *pt);
static int mcd_play(struct mcd_softc *, struct mcd_read2 *pb);
static int mcd_playmsf(struct mcd_softc *, struct ioc_play_msf *pt);
static int mcd_playblocks(struct mcd_softc *, struct ioc_play_blocks *);
static int mcd_pause(struct mcd_softc *);
static int mcd_resume(struct mcd_softc *);
static int mcd_lock_door(struct mcd_softc *, int lock);
static int mcd_close_tray(struct mcd_softc *);
static int mcd_size(struct cdev *dev);
static d_open_t mcdopen;
static d_close_t mcdclose;
static d_ioctl_t mcdioctl;
static d_strategy_t mcdstrategy;
static struct cdevsw mcd_cdevsw = {
.d_version = D_VERSION,
.d_open = mcdopen,
.d_close = mcdclose,
.d_read = physread,
.d_ioctl = mcdioctl,
.d_strategy = mcdstrategy,
.d_name = "mcd",
.d_flags = D_DISK,
};
#define MCD_RETRYS 5
#define MCD_RDRETRYS 8
#define CLOSE_TRAY_SECS 8
#define DISK_SENSE_SECS 3
#define WAIT_FRAC 4
/* several delays */
#define RDELAY_WAITSTAT 300
#define RDELAY_WAITMODE 300
#define RDELAY_WAITREAD 800
#define MIN_DELAY 15
#define DELAY_GETREPLY 5000000
int
mcd_attach(struct mcd_softc *sc)
{
int unit;
unit = device_get_unit(sc->dev);
MCD_LOCK(sc);
sc->data.flags |= MCDINIT;
mcd_soft_reset(sc);
bioq_init(&sc->data.head);
#ifdef NOTYET
/* wire controller for interrupts and dma */
mcd_configure(sc);
#endif
MCD_UNLOCK(sc);
/* name filled in probe */
sc->mcd_dev_t = make_dev(&mcd_cdevsw, 8 * unit,
UID_ROOT, GID_OPERATOR, 0640, "mcd%d", unit);
sc->mcd_dev_t->si_drv1 = (void *)sc;
callout_init_mtx(&sc->timer, &sc->mtx, 0);
device_printf(sc->dev,
"WARNING: This driver is deprecated and will be removed.\n");
return (0);
}
static int
mcdopen(struct cdev *dev, int flags, int fmt, struct thread *td)
{
struct mcd_softc *sc;
int r,retry;
sc = (struct mcd_softc *)dev->si_drv1;
/* invalidated in the meantime? mark all open part's invalid */
MCD_LOCK(sc);
if (!(sc->data.flags & MCDVALID) && sc->data.openflags) {
MCD_UNLOCK(sc);
return (ENXIO);
}
if (mcd_getstat(sc, 1) == -1) {
MCD_UNLOCK(sc);
return (EIO);
}
if ( (sc->data.status & (MCDDSKCHNG|MCDDOOROPEN))
|| !(sc->data.status & MCDDSKIN))
for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
(void) mtx_sleep(sc, &sc->mtx, PSOCK | PCATCH,
"mcdsn1", hz/WAIT_FRAC);
if ((r = mcd_getstat(sc, 1)) == -1) {
MCD_UNLOCK(sc);
return (EIO);
}
if (r != -2)
break;
}
if (sc->data.status & MCDDOOROPEN) {
MCD_UNLOCK(sc);
device_printf(sc->dev, "door is open\n");
return (ENXIO);
}
if (!(sc->data.status & MCDDSKIN)) {
MCD_UNLOCK(sc);
device_printf(sc->dev, "no CD inside\n");
return (ENXIO);
}
if (sc->data.status & MCDDSKCHNG) {
MCD_UNLOCK(sc);
device_printf(sc->dev, "CD not sensed\n");
return (ENXIO);
}
if (mcd_size(dev) < 0) {
MCD_UNLOCK(sc);
device_printf(sc->dev, "failed to get disk size\n");
return (ENXIO);
}
sc->data.openflags = 1;
sc->data.partflags |= MCDREADRAW;
sc->data.flags |= MCDVALID;
(void) mcd_lock_door(sc, MCD_LK_LOCK);
if (!(sc->data.flags & MCDVALID)) {
MCD_UNLOCK(sc);
return (ENXIO);
}
r = mcd_read_toc(sc);
MCD_UNLOCK(sc);
return (r);
}
static int
mcdclose(struct cdev *dev, int flags, int fmt, struct thread *td)
{
struct mcd_softc *sc;
sc = (struct mcd_softc *)dev->si_drv1;
MCD_LOCK(sc);
KASSERT(sc->data.openflags, ("device not open"));
(void) mcd_lock_door(sc, MCD_LK_UNLOCK);
sc->data.openflags = 0;
sc->data.partflags &= ~MCDREADRAW;
MCD_UNLOCK(sc);
return (0);
}
static void
mcdstrategy(struct bio *bp)
{
struct mcd_softc *sc;
sc = (struct mcd_softc *)bp->bio_dev->si_drv1;
/* if device invalidated (e.g. media change, door open), error */
MCD_LOCK(sc);
if (!(sc->data.flags & MCDVALID)) {
device_printf(sc->dev, "media changed\n");
bp->bio_error = EIO;
goto bad;
}
/* read only */
if (!(bp->bio_cmd == BIO_READ)) {
bp->bio_error = EROFS;
goto bad;
}
/* no data to read */
if (bp->bio_bcount == 0)
goto done;
if (!(sc->data.flags & MCDTOC)) {
bp->bio_error = EIO;
goto bad;
}
bp->bio_resid = 0;
/* queue it */
bioq_disksort(&sc->data.head, bp);
/* now check whether we can perform processing */
mcd_start(sc);
MCD_UNLOCK(sc);
return;
bad:
bp->bio_flags |= BIO_ERROR;
done:
MCD_UNLOCK(sc);
bp->bio_resid = bp->bio_bcount;
biodone(bp);
return;
}
static void
mcd_start(struct mcd_softc *sc)
{
struct bio *bp;
MCD_ASSERT_LOCKED(sc);
if (sc->data.flags & MCDMBXBSY) {
return;
}
bp = bioq_takefirst(&sc->data.head);
if (bp != 0) {
/* block found to process, dequeue */
/*MCD_TRACE("mcd_start: found block bp=0x%x\n",bp,0,0,0);*/
sc->data.flags |= MCDMBXBSY;
} else {
/* nothing to do */
return;
}
sc->data.mbx.retry = MCD_RETRYS;
sc->data.mbx.bp = bp;
mcd_doread(sc, MCD_S_BEGIN,&(sc->data.mbx));
return;
}
static int
mcdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
{
struct mcd_softc *sc;
int retry,r;
sc = (struct mcd_softc *)dev->si_drv1;
MCD_LOCK(sc);
if (mcd_getstat(sc, 1) == -1) { /* detect disk change too */
MCD_UNLOCK(sc);
return (EIO);
}
MCD_TRACE("ioctl called 0x%lx\n", cmd);
switch (cmd) {
case CDIOCSETPATCH:
case CDIOCGETVOL:
case CDIOCSETVOL:
case CDIOCSETMONO:
case CDIOCSETSTERIO:
case CDIOCSETMUTE:
case CDIOCSETLEFT:
case CDIOCSETRIGHT:
MCD_UNLOCK(sc);
return (EINVAL);
case CDIOCEJECT:
r = mcd_eject(sc);
MCD_UNLOCK(sc);
return (r);
case CDIOCSETDEBUG:
sc->data.debug = 1;
MCD_UNLOCK(sc);
return (0);
case CDIOCCLRDEBUG:
sc->data.debug = 0;
MCD_UNLOCK(sc);
return (0);
case CDIOCRESET:
r = mcd_hard_reset(sc);
MCD_UNLOCK(sc);
return (r);
case CDIOCALLOW:
r = mcd_lock_door(sc, MCD_LK_UNLOCK);
MCD_UNLOCK(sc);
return (r);
case CDIOCPREVENT:
r = mcd_lock_door(sc, MCD_LK_LOCK);
MCD_UNLOCK(sc);
return (r);
case CDIOCCLOSE:
r = mcd_inject(sc);
MCD_UNLOCK(sc);
return (r);
}
if (!(sc->data.flags & MCDVALID)) {
if ( (sc->data.status & (MCDDSKCHNG|MCDDOOROPEN))
|| !(sc->data.status & MCDDSKIN))
for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
(void) mtx_sleep(sc, &sc->mtx, PSOCK | PCATCH,
"mcdsn2", hz/WAIT_FRAC);
if ((r = mcd_getstat(sc, 1)) == -1) {
MCD_UNLOCK(sc);
return (EIO);
}
if (r != -2)
break;
}
if ( (sc->data.status & (MCDDOOROPEN|MCDDSKCHNG))
|| !(sc->data.status & MCDDSKIN)
|| mcd_size(dev) < 0
) {
MCD_UNLOCK(sc);
return (ENXIO);
}
sc->data.flags |= MCDVALID;
sc->data.partflags |= MCDREADRAW;
(void) mcd_lock_door(sc, MCD_LK_LOCK);
if (!(sc->data.flags & MCDVALID)) {
MCD_UNLOCK(sc);
return (ENXIO);
}
}
switch (cmd) {
case DIOCGMEDIASIZE:
*(off_t *)addr = (off_t)sc->data.disksize * sc->data.blksize;
r = 0;
break;
case DIOCGSECTORSIZE:
*(u_int *)addr = sc->data.blksize;
r = 0;
break;
case CDIOCPLAYTRACKS:
r = mcd_playtracks(sc, (struct ioc_play_track *) addr);
break;
case CDIOCPLAYBLOCKS:
r = mcd_playblocks(sc, (struct ioc_play_blocks *) addr);
break;
case CDIOCPLAYMSF:
r = mcd_playmsf(sc, (struct ioc_play_msf *) addr);
break;
case CDIOCREADSUBCHANNEL_SYSSPACE:
return mcd_subchan(sc, (struct ioc_read_subchannel *) addr, 1);
case CDIOCREADSUBCHANNEL:
return mcd_subchan(sc, (struct ioc_read_subchannel *) addr, 0);
case CDIOREADTOCHEADER:
r = mcd_toc_header(sc, (struct ioc_toc_header *) addr);
break;
case CDIOREADTOCENTRYS:
return mcd_toc_entrys(sc, (struct ioc_read_toc_entry *) addr);
case CDIOCRESUME:
r = mcd_resume(sc);
break;
case CDIOCPAUSE:
r = mcd_pause(sc);
break;
case CDIOCSTART:
if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
r = EIO;
else
r = 0;
break;
case CDIOCSTOP:
r = mcd_stop(sc);
break;
default:
r = ENOTTY;
}
MCD_UNLOCK(sc);
return (r);
}
static int
mcd_size(struct cdev *dev)
{
struct mcd_softc *sc;
int size;
sc = (struct mcd_softc *)dev->si_drv1;
if (mcd_volinfo(sc) == 0) {
sc->data.blksize = MCDBLK;
size = msf2hsg(sc->data.volinfo.vol_msf, 0);
sc->data.disksize = size * (MCDBLK/DEV_BSIZE);
return (0);
}
return (-1);
}
/***************************************************************
* lower level of driver starts here
**************************************************************/
#ifdef NOTDEF
static char
irqs[] = {
0x00,0x00,0x10,0x20,0x00,0x30,0x00,0x00,
0x00,0x10,0x40,0x50,0x00,0x00,0x00,0x00
};
static char
drqs[] = {
0x00,0x01,0x00,0x03,0x00,0x05,0x06,0x07,
};
#endif
#ifdef NOT_YET
static void
mcd_configure(struct mcd_softc *sc)
{
MCD_WRITE(sc, MCD_REG_CONFIG, sc->data.config);
}
#endif
/* Wait for non-busy - return 0 on timeout */
static int
twiddle_thumbs(struct mcd_softc *sc, int count, char *whine)
{
int i;
for (i = 0; i < count; i++) {
if (!(MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
return (1);
}
if (bootverbose)
device_printf(sc->dev, "timeout %s\n", whine);
return (0);
}
/* check to see if a Mitsumi CD-ROM is attached to the ISA bus */
int
mcd_probe(struct mcd_softc *sc)
{
int i, j;
unsigned char stbytes[3];
sc->data.flags = MCDPROBING;
#ifdef NOTDEF
/* get irq/drq configuration word */
sc->data.config = irqs[dev->id_irq]; /* | drqs[dev->id_drq];*/
#else
sc->data.config = 0;
#endif
/* send a reset */
MCD_WRITE(sc, MCD_FLAGS, M_RESET);
/*
* delay awhile by getting any pending garbage (old data) and
* throwing it away.
*/
for (i = 1000000; i != 0; i--)
(void)MCD_READ(sc, MCD_FLAGS);
/* Get status */
MCD_WRITE(sc, MCD_DATA, MCD_CMDGETSTAT);
if (!twiddle_thumbs(sc, 1000000, "getting status"))
return (ENXIO); /* Timeout */
/* Get version information */
MCD_WRITE(sc, MCD_DATA, MCD_CMDCONTINFO);
for (j = 0; j < 3; j++) {
if (!twiddle_thumbs(sc, 3000, "getting version info"))
return (ENXIO);
stbytes[j] = (MCD_READ(sc, MCD_DATA) & 0xFF);
}
if (stbytes[1] == stbytes[2])
return (ENXIO);
if (stbytes[2] >= 4 || stbytes[1] != 'M') {
MCD_WRITE(sc, MCD_CTRL, M_PICKLE);
sc->data.flags |= MCDNEWMODEL;
}
sc->data.read_command = MCD_CMDSINGLESPEEDREAD;
switch (stbytes[1]) {
case 'M':
if (stbytes[2] <= 2) {
sc->data.type = MCD_TYPE_LU002S;
sc->data.name = "Mitsumi LU002S";
} else if (stbytes[2] <= 5) {
sc->data.type = MCD_TYPE_LU005S;
sc->data.name = "Mitsumi LU005S";
} else {
sc->data.type = MCD_TYPE_LU006S;
sc->data.name = "Mitsumi LU006S";
}
break;
case 'F':
sc->data.type = MCD_TYPE_FX001;
sc->data.name = "Mitsumi FX001";
break;
case 'D':
sc->data.type = MCD_TYPE_FX001D;
sc->data.name = "Mitsumi FX001D";
sc->data.read_command = MCD_CMDDOUBLESPEEDREAD;
break;
default:
sc->data.type = MCD_TYPE_UNKNOWN;
sc->data.name = "Mitsumi ???";
break;
}
if (bootverbose)
device_printf(sc->dev, "type %s, version info: %c %x\n",
sc->data.name, stbytes[1], stbytes[2]);
return (0);
}
static int
mcd_waitrdy(struct mcd_softc *sc, int dly)
{
int i;
/* wait until flag port senses status ready */
for (i=0; i<dly; i+=MIN_DELAY) {
if (!(MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
return (0);
DELAY(MIN_DELAY);
}
return (-1);
}
static int
mcd_getreply(struct mcd_softc *sc, int dly)
{
/* wait data to become ready */
if (mcd_waitrdy(sc, dly)<0) {
device_printf(sc->dev, "timeout getreply\n");
return (-1);
}
/* get the data */
return (MCD_READ(sc, MCD_REG_STATUS) & 0xFF);
}
static int
mcd_getstat(struct mcd_softc *sc, int sflg)
{
int i;
/* get the status */
if (sflg)
MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDGETSTAT);
i = mcd_getreply(sc, DELAY_GETREPLY);
if (i<0 || (i & MCD_ST_CMDCHECK)) {
sc->data.curr_mode = MCD_MD_UNKNOWN;
return (-1);
}
sc->data.status = i;
if (mcd_setflags(sc) < 0)
return (-2);
return (sc->data.status);
}
static int
mcd_setflags(struct mcd_softc *sc)
{
/* check flags */
if ( (sc->data.status & (MCDDSKCHNG|MCDDOOROPEN))
|| !(sc->data.status & MCDDSKIN)) {
MCD_TRACE("setflags: sensed DSKCHNG or DOOROPEN or !DSKIN\n");
mcd_soft_reset(sc);
return (-1);
}
if (sc->data.status & MCDAUDIOBSY)
sc->data.audio_status = CD_AS_PLAY_IN_PROGRESS;
else if (sc->data.audio_status == CD_AS_PLAY_IN_PROGRESS)
sc->data.audio_status = CD_AS_PLAY_COMPLETED;
return (0);
}
static int
mcd_get(struct mcd_softc *sc, char *buf, int nmax)
{
int i,k;
for (i=0; i<nmax; i++) {
/* wait for data */
if ((k = mcd_getreply(sc, DELAY_GETREPLY)) < 0) {
device_printf(sc->dev, "timeout mcd_get\n");
return (-1);
}
buf[i] = k;
}
return (i);
}
static int
mcd_send(struct mcd_softc *sc, int cmd,int nretrys)
{
int i,k=0;
/*MCD_TRACE("mcd_send: command = 0x%02x\n",cmd,0,0,0);*/
for (i=0; i<nretrys; i++) {
MCD_WRITE(sc, MCD_REG_COMMAND, cmd);
if ((k=mcd_getstat(sc, 0)) != -1)
break;
}
if (k == -2) {
device_printf(sc->dev, "media changed\n");
return (-1);
}
if (i == nretrys) {
device_printf(sc->dev, "mcd_send retry cnt exceeded\n");
return (-1);
}
/*MCD_TRACE("mcd_send: done\n",0,0,0,0);*/
return (0);
}
static void
hsg2msf(int hsg, bcd_t *msf)
{
hsg += 150;
F_msf(msf) = bin2bcd(hsg % 75);
hsg /= 75;
S_msf(msf) = bin2bcd(hsg % 60);
hsg /= 60;
M_msf(msf) = bin2bcd(hsg);
}
static int
msf2hsg(bcd_t *msf, int relative)
{
return (bcd2bin(M_msf(msf)) * 60 + bcd2bin(S_msf(msf))) * 75 +
bcd2bin(F_msf(msf)) - (!relative) * 150;
}
static int
mcd_volinfo(struct mcd_softc *sc)
{
/* Just return if we already have it */
if (sc->data.flags & MCDVOLINFO) return (0);
/*MCD_TRACE("mcd_volinfo: enter\n",0,0,0,0);*/
/* send volume info command */
if (mcd_send(sc, MCD_CMDGETVOLINFO,MCD_RETRYS) < 0)
return (EIO);
/* get data */
if (mcd_get(sc, (char*) &sc->data.volinfo,sizeof(struct mcd_volinfo)) < 0) {
device_printf(sc->dev, "mcd_volinfo: error read data\n");
return (EIO);
}
if (sc->data.volinfo.trk_low > 0 &&
sc->data.volinfo.trk_high >= sc->data.volinfo.trk_low
) {
sc->data.flags |= MCDVOLINFO; /* volinfo is OK */
return (0);
}
return (EINVAL);
}
/* state machine to process read requests
* initialize with MCD_S_BEGIN: calculate sizes, and read status
* MCD_S_WAITSTAT: wait for status reply, set mode
* MCD_S_WAITMODE: waits for status reply from set mode, set read command
* MCD_S_WAITREAD: wait for read ready, read data
*/
static void
mcd_timeout(void *arg)
{
struct mcd_softc *sc;
sc = (struct mcd_softc *)arg;
MCD_ASSERT_LOCKED(sc);
mcd_doread(sc, sc->ch_state, sc->ch_mbxsave);
}
static void
mcd_doread(struct mcd_softc *sc, int state, struct mcd_mbx *mbxin)
{
struct mcd_mbx *mbx;
struct bio *bp;
int rm, i, k;
struct mcd_read2 rbuf;
int blknum;
caddr_t addr;
MCD_ASSERT_LOCKED(sc);
mbx = (state!=MCD_S_BEGIN) ? sc->ch_mbxsave : mbxin;
bp = mbx->bp;
loop:
switch (state) {
case MCD_S_BEGIN:
mbx = sc->ch_mbxsave = mbxin;
case MCD_S_BEGIN1:
retry_status:
/* get status */
MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDGETSTAT);
mbx->count = RDELAY_WAITSTAT;
sc->ch_state = MCD_S_WAITSTAT;
callout_reset(&sc->timer, hz/100, mcd_timeout, sc); /* XXX */
return;
case MCD_S_WAITSTAT:
sc->ch_state = MCD_S_WAITSTAT;
callout_stop(&sc->timer);
if (mbx->count-- >= 0) {
if (MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
sc->ch_state = MCD_S_WAITSTAT;
callout_reset(&sc->timer, hz/100,
mcd_timeout, sc); /* XXX */
return;
}
sc->data.status = MCD_READ(sc, MCD_REG_STATUS) & 0xFF;
if (sc->data.status & MCD_ST_CMDCHECK)
goto retry_status;
if (mcd_setflags(sc) < 0)
goto changed;
MCD_TRACE("got WAITSTAT delay=%d\n",
RDELAY_WAITSTAT-mbx->count);
/* reject, if audio active */
if (sc->data.status & MCDAUDIOBSY) {
device_printf(sc->dev, "audio is active\n");
goto readerr;
}
retry_mode:
/* to check for raw/cooked mode */
if (sc->data.flags & MCDREADRAW) {
rm = MCD_MD_RAW;
mbx->sz = MCDRBLK;
} else {
rm = MCD_MD_COOKED;
mbx->sz = sc->data.blksize;
}
if (rm == sc->data.curr_mode)
goto modedone;
mbx->count = RDELAY_WAITMODE;
sc->data.curr_mode = MCD_MD_UNKNOWN;
mbx->mode = rm;
MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDSETMODE);
MCD_WRITE(sc, MCD_REG_COMMAND, rm);
sc->ch_state = MCD_S_WAITMODE;
callout_reset(&sc->timer, hz / 100, mcd_timeout, sc); /* XXX */
return;
} else {
device_printf(sc->dev, "timeout getstatus\n");
goto readerr;
}
case MCD_S_WAITMODE:
sc->ch_state = MCD_S_WAITMODE;
callout_stop(&sc->timer);
if (mbx->count-- < 0) {
device_printf(sc->dev, "timeout set mode\n");
goto readerr;
}
if (MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
sc->ch_state = MCD_S_WAITMODE;
callout_reset(&sc->timer, hz / 100, mcd_timeout, sc);
return;
}
sc->data.status = MCD_READ(sc, MCD_REG_STATUS) & 0xFF;
if (sc->data.status & MCD_ST_CMDCHECK) {
sc->data.curr_mode = MCD_MD_UNKNOWN;
goto retry_mode;
}
if (mcd_setflags(sc) < 0)
goto changed;
sc->data.curr_mode = mbx->mode;
MCD_TRACE("got WAITMODE delay=%d\n",
RDELAY_WAITMODE-mbx->count);
modedone:
/* for first block */
mbx->nblk = howmany(bp->bio_bcount, mbx->sz);
mbx->skip = 0;
nextblock:
blknum = bp->bio_offset / mbx->sz + mbx->skip/mbx->sz;
MCD_TRACE("mcd_doread: read blknum=%d for bp=%p\n",
blknum, bp);
/* build parameter block */
hsg2msf(blknum,rbuf.start_msf);
retry_read:
/* send the read command */
MCD_WRITE(sc, MCD_REG_COMMAND, sc->data.read_command);
MCD_WRITE(sc, MCD_REG_COMMAND, rbuf.start_msf[0]);
MCD_WRITE(sc, MCD_REG_COMMAND, rbuf.start_msf[1]);
MCD_WRITE(sc, MCD_REG_COMMAND, rbuf.start_msf[2]);
MCD_WRITE(sc, MCD_REG_COMMAND, 0);
MCD_WRITE(sc, MCD_REG_COMMAND, 0);
MCD_WRITE(sc, MCD_REG_COMMAND, 1);
/* Spin briefly (<= 2ms) to avoid missing next block */
for (i = 0; i < 20; i++) {
k = MCD_READ(sc, MCD_FLAGS);
if (!(k & MFL_DATA_NOT_AVAIL))
goto got_it;
DELAY(100);
}
mbx->count = RDELAY_WAITREAD;
sc->ch_state = MCD_S_WAITREAD;
callout_reset(&sc->timer, hz / 100, mcd_timeout, sc); /* XXX */
return;
case MCD_S_WAITREAD:
sc->ch_state = MCD_S_WAITREAD;
callout_stop(&sc->timer);
if (mbx->count-- > 0) {
k = MCD_READ(sc, MCD_FLAGS);
if (!(k & MFL_DATA_NOT_AVAIL)) { /* XXX */
MCD_TRACE("got data delay=%d\n",
RDELAY_WAITREAD-mbx->count);
got_it:
/* data is ready */
addr = bp->bio_data + mbx->skip;
MCD_WRITE(sc, MCD_REG_CTL2,0x04); /* XXX */
for (i=0; i<mbx->sz; i++)
*addr++ = MCD_READ(sc, MCD_REG_RDATA);
MCD_WRITE(sc, MCD_REG_CTL2,0x0c); /* XXX */
k = MCD_READ(sc, MCD_FLAGS);
/* If we still have some junk, read it too */
if (!(k & MFL_DATA_NOT_AVAIL)) {
MCD_WRITE(sc, MCD_REG_CTL2, 0x04); /* XXX */
(void)MCD_READ(sc, MCD_REG_RDATA);
(void)MCD_READ(sc, MCD_REG_RDATA);
MCD_WRITE(sc, MCD_REG_CTL2, 0x0c); /* XXX */
}
if (--mbx->nblk > 0) {
mbx->skip += mbx->sz;
goto nextblock;
}
/* return buffer */
bp->bio_resid = 0;
biodone(bp);
sc->data.flags &= ~(MCDMBXBSY|MCDREADRAW);
mcd_start(sc);
return;
}
if (!(k & MFL_STATUS_NOT_AVAIL)) {
sc->data.status = MCD_READ(sc, MCD_REG_STATUS) & 0xFF;
if (sc->data.status & MCD_ST_CMDCHECK)
goto retry_read;
if (mcd_setflags(sc) < 0)
goto changed;
}
sc->ch_state = MCD_S_WAITREAD;
callout_reset(&sc->timer, hz / 100, mcd_timeout, sc); /* XXX */
return;
} else {
device_printf(sc->dev, "timeout read data\n");
goto readerr;
}
}
readerr:
if (mbx->retry-- > 0) {
device_printf(sc->dev, "retrying\n");
state = MCD_S_BEGIN1;
goto loop;
}
harderr:
/* invalidate the buffer */
bp->bio_flags |= BIO_ERROR;
bp->bio_resid = bp->bio_bcount;
biodone(bp);
sc->data.flags &= ~(MCDMBXBSY|MCDREADRAW);
mcd_start(sc);
return;
changed:
device_printf(sc->dev, "media changed\n");
goto harderr;
#ifdef NOTDEF
device_printf(sc->dev, "unit timeout, resetting\n");
MCD_WRITE(sc, MCD_REG_RESET, MCD_CMDRESET);
DELAY(300000);
(void)mcd_getstat(sc, 1);
(void)mcd_getstat(sc, 1);
/*sc->data.status &= ~MCDDSKCHNG; */
sc->data.debug = 1; /* preventive set debug mode */
#endif
}
static int
mcd_lock_door(struct mcd_softc *sc, int lock)
{
MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDLOCKDRV);
MCD_WRITE(sc, MCD_REG_COMMAND, lock);
if (mcd_getstat(sc, 0) == -1)
return (EIO);
return (0);
}
static int
mcd_close_tray(struct mcd_softc *sc)
{
int retry, r;
if (mcd_getstat(sc, 1) == -1)
return (EIO);
if (sc->data.status & MCDDOOROPEN) {
MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDCLOSETRAY);
for (retry = 0; retry < CLOSE_TRAY_SECS * WAIT_FRAC; retry++) {
if (MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL)
(void) mtx_sleep(sc, &sc->mtx, PSOCK | PCATCH,
"mcdcls", hz/WAIT_FRAC);
else {
if ((r = mcd_getstat(sc, 0)) == -1)
return (EIO);
return (0);
}
}
return (ENXIO);
}
return (0);
}
static int
mcd_eject(struct mcd_softc *sc)
{
int r;
if (mcd_getstat(sc, 1) == -1) /* detect disk change too */
return (EIO);
if (sc->data.status & MCDDOOROPEN)
return (0);
if ((r = mcd_stop(sc)) == EIO)
return (r);
MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDEJECTDISK);
if (mcd_getstat(sc, 0) == -1)
return (EIO);
return (0);
}
static int
mcd_inject(struct mcd_softc *sc)
{
if (mcd_getstat(sc, 1) == -1) /* detect disk change too */
return (EIO);
if (sc->data.status & MCDDOOROPEN)
return mcd_close_tray(sc);
return (0);
}
static int
mcd_hard_reset(struct mcd_softc *sc)
{
MCD_WRITE(sc, MCD_REG_RESET, MCD_CMDRESET);
sc->data.curr_mode = MCD_MD_UNKNOWN;
sc->data.audio_status = CD_AS_AUDIO_INVALID;
return (0);
}
static void
mcd_soft_reset(struct mcd_softc *sc)
{
sc->data.flags &= (MCDINIT|MCDPROBING|MCDNEWMODEL);
sc->data.curr_mode = MCD_MD_UNKNOWN;
sc->data.partflags = 0;
sc->data.audio_status = CD_AS_AUDIO_INVALID;
}
static int
mcd_setmode(struct mcd_softc *sc, int mode)
{
int retry, st;
if (sc->data.curr_mode == mode)
return (0);
if (sc->data.debug)
device_printf(sc->dev, "setting mode to %d\n", mode);
for(retry=0; retry<MCD_RETRYS; retry++)
{
sc->data.curr_mode = MCD_MD_UNKNOWN;
MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDSETMODE);
MCD_WRITE(sc, MCD_REG_COMMAND, mode);
if ((st = mcd_getstat(sc, 0)) >= 0) {
sc->data.curr_mode = mode;
return (0);
}
if (st == -2) {
device_printf(sc->dev, "media changed\n");
break;
}
}
return (-1);
}
static int
mcd_toc_header(struct mcd_softc *sc, struct ioc_toc_header *th)
{
int r;
if ((r = mcd_volinfo(sc)) != 0)
return (r);
th->starting_track = bcd2bin(sc->data.volinfo.trk_low);
th->ending_track = bcd2bin(sc->data.volinfo.trk_high);
th->len = 2 * sizeof(u_char) /* start & end tracks */ +
(th->ending_track + 1 - th->starting_track + 1) *
sizeof(struct cd_toc_entry);
return (0);
}
static int
mcd_read_toc(struct mcd_softc *sc)
{
struct ioc_toc_header th;
struct mcd_qchninfo q;
int rc, trk, idx, retry;
/* Only read TOC if needed */
if (sc->data.flags & MCDTOC)
return (0);
if (sc->data.debug)
device_printf(sc->dev, "reading toc header\n");
if ((rc = mcd_toc_header(sc, &th)) != 0)
return (rc);
if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
return (EIO);
if (mcd_setmode(sc, MCD_MD_TOC) != 0)
return (EIO);
if (sc->data.debug)
device_printf(sc->dev, "get_toc reading qchannel info\n");
for(trk=th.starting_track; trk<=th.ending_track; trk++)
sc->data.toc[trk].idx_no = 0;
trk = th.ending_track - th.starting_track + 1;
for(retry=0; retry<600 && trk>0; retry++)
{
if (mcd_getqchan(sc, &q) < 0) break;
idx = bcd2bin(q.idx_no);
if (idx>=th.starting_track && idx<=th.ending_track && q.trk_no==0) {
if (sc->data.toc[idx].idx_no == 0) {
sc->data.toc[idx] = q;
trk--;
}
}
}
if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
return (EIO);
if (trk != 0)
return (ENXIO);
/* add a fake last+1 */
idx = th.ending_track + 1;
sc->data.toc[idx].control = sc->data.toc[idx-1].control;
sc->data.toc[idx].addr_type = sc->data.toc[idx-1].addr_type;
sc->data.toc[idx].trk_no = 0;
sc->data.toc[idx].idx_no = MCD_LASTPLUS1;
sc->data.toc[idx].hd_pos_msf[0] = sc->data.volinfo.vol_msf[0];
sc->data.toc[idx].hd_pos_msf[1] = sc->data.volinfo.vol_msf[1];
sc->data.toc[idx].hd_pos_msf[2] = sc->data.volinfo.vol_msf[2];
if (sc->data.debug)
{ int i;
for (i = th.starting_track; i <= idx; i++)
device_printf(sc->dev, "trk %d idx %d pos %d %d %d\n",
i,
sc->data.toc[i].idx_no > 0x99 ? sc->data.toc[i].idx_no :
bcd2bin(sc->data.toc[i].idx_no),
bcd2bin(sc->data.toc[i].hd_pos_msf[0]),
bcd2bin(sc->data.toc[i].hd_pos_msf[1]),
bcd2bin(sc->data.toc[i].hd_pos_msf[2]));
}
sc->data.flags |= MCDTOC;
return (0);
}
#if 0
static int
mcd_toc_entry(struct mcd_softc *sc, struct ioc_read_toc_single_entry *te)
{
struct ioc_toc_header th;
int rc, trk;
if (te->address_format != CD_MSF_FORMAT
&& te->address_format != CD_LBA_FORMAT)
return (EINVAL);
/* Copy the toc header */
if ((rc = mcd_toc_header(sc, &th)) != 0)
return (rc);
/* verify starting track */
trk = te->track;
if (trk == 0)
trk = th.starting_track;
else if (trk == MCD_LASTPLUS1)
trk = th.ending_track + 1;
else if (trk < th.starting_track || trk > th.ending_track + 1)
return (EINVAL);
/* Make sure we have a valid toc */
if ((rc=mcd_read_toc(sc)) != 0)
return (rc);
/* Copy the TOC data. */
if (sc->data.toc[trk].idx_no == 0)
return (EIO);
te->entry.control = sc->data.toc[trk].control;
te->entry.addr_type = sc->data.toc[trk].addr_type;
te->entry.track =
sc->data.toc[trk].idx_no > 0x99 ? sc->data.toc[trk].idx_no :
bcd2bin(sc->data.toc[trk].idx_no);
switch (te->address_format) {
case CD_MSF_FORMAT:
te->entry.addr.msf.unused = 0;
te->entry.addr.msf.minute = bcd2bin(sc->data.toc[trk].hd_pos_msf[0]);
te->entry.addr.msf.second = bcd2bin(sc->data.toc[trk].hd_pos_msf[1]);
te->entry.addr.msf.frame = bcd2bin(sc->data.toc[trk].hd_pos_msf[2]);
break;
case CD_LBA_FORMAT:
te->entry.addr.lba = htonl(msf2hsg(sc->data.toc[trk].hd_pos_msf, 0));
break;
}
return (0);
}
#endif
static int
mcd_toc_entrys(struct mcd_softc *sc, struct ioc_read_toc_entry *te)
{
struct cd_toc_entry entries[MCD_MAXTOCS];
struct ioc_toc_header th;
int rc, n, trk, len;
if ( te->data_len < sizeof(entries[0])
|| (te->data_len % sizeof(entries[0])) != 0
|| (te->address_format != CD_MSF_FORMAT
&& te->address_format != CD_LBA_FORMAT)
)
return (EINVAL);
/* Copy the toc header */
if ((rc = mcd_toc_header(sc, &th)) != 0)
return (rc);
/* verify starting track */
trk = te->starting_track;
if (trk == 0)
trk = th.starting_track;
else if (trk == MCD_LASTPLUS1)
trk = th.ending_track + 1;
else if (trk < th.starting_track || trk > th.ending_track + 1)
return (EINVAL);
len = ((th.ending_track + 1 - trk) + 1) *
sizeof(entries[0]);
if (te->data_len < len)
len = te->data_len;
if (len > sizeof(entries))
return (EINVAL);
/* Make sure we have a valid toc */
if ((rc=mcd_read_toc(sc)) != 0)
return (rc);
/* Copy the TOC data. */
for (n = 0; len > 0 && trk <= th.ending_track + 1; trk++) {
if (sc->data.toc[trk].idx_no == 0)
continue;
entries[n].control = sc->data.toc[trk].control;
entries[n].addr_type = sc->data.toc[trk].addr_type;
entries[n].track =
sc->data.toc[trk].idx_no > 0x99 ? sc->data.toc[trk].idx_no :
bcd2bin(sc->data.toc[trk].idx_no);
switch (te->address_format) {
case CD_MSF_FORMAT:
entries[n].addr.msf.unused = 0;
entries[n].addr.msf.minute = bcd2bin(sc->data.toc[trk].hd_pos_msf[0]);
entries[n].addr.msf.second = bcd2bin(sc->data.toc[trk].hd_pos_msf[1]);
entries[n].addr.msf.frame = bcd2bin(sc->data.toc[trk].hd_pos_msf[2]);
break;
case CD_LBA_FORMAT:
entries[n].addr.lba = htonl(msf2hsg(sc->data.toc[trk].hd_pos_msf, 0));
break;
}
len -= sizeof(struct cd_toc_entry);
n++;
}
/* copy the data back */
MCD_UNLOCK(sc);
return copyout(entries, te->data, n * sizeof(struct cd_toc_entry));
}
static int
mcd_stop(struct mcd_softc *sc)
{
/* Verify current status */
if (sc->data.audio_status != CD_AS_PLAY_IN_PROGRESS &&
sc->data.audio_status != CD_AS_PLAY_PAUSED &&
sc->data.audio_status != CD_AS_PLAY_COMPLETED) {
if (sc->data.debug)
device_printf(sc->dev,
"stop attempted when not playing, audio status %d\n",
sc->data.audio_status);
return (EINVAL);
}
if (sc->data.audio_status == CD_AS_PLAY_IN_PROGRESS)
if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
return (EIO);
sc->data.audio_status = CD_AS_PLAY_COMPLETED;
return (0);
}
static int
mcd_getqchan(struct mcd_softc *sc, struct mcd_qchninfo *q)
{
if (mcd_send(sc, MCD_CMDGETQCHN, MCD_RETRYS) < 0)
return (-1);
if (mcd_get(sc, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
return (-1);
if (sc->data.debug) {
device_printf(sc->dev,
"getqchan control=0x%x addr_type=0x%x trk=%d ind=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
q->control, q->addr_type,
bcd2bin(q->trk_no),
bcd2bin(q->idx_no),
bcd2bin(q->trk_size_msf[0]),
bcd2bin(q->trk_size_msf[1]),
bcd2bin(q->trk_size_msf[2]),
bcd2bin(q->hd_pos_msf[0]),
bcd2bin(q->hd_pos_msf[1]),
bcd2bin(q->hd_pos_msf[2]));
}
return (0);
}
static int
mcd_subchan(struct mcd_softc *sc, struct ioc_read_subchannel *sch, int nocopyout)
{
struct mcd_qchninfo q;
struct cd_sub_channel_info data;
int lba;
if (sc->data.debug)
device_printf(sc->dev, "subchan af=%d, df=%d\n",
sch->address_format,
sch->data_format);
if (sch->address_format != CD_MSF_FORMAT &&
sch->address_format != CD_LBA_FORMAT)
return (EINVAL);
if (sch->data_format != CD_CURRENT_POSITION &&
sch->data_format != CD_MEDIA_CATALOG)
return (EINVAL);
if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
return (EIO);
if (mcd_getqchan(sc, &q) < 0)
return (EIO);
data.header.audio_status = sc->data.audio_status;
data.what.position.data_format = sch->data_format;
switch (sch->data_format) {
case CD_MEDIA_CATALOG:
data.what.media_catalog.mc_valid = 1;
data.what.media_catalog.mc_number[0] = '\0';
break;
case CD_CURRENT_POSITION:
data.what.position.control = q.control;
data.what.position.addr_type = q.addr_type;
data.what.position.track_number = bcd2bin(q.trk_no);
data.what.position.index_number = bcd2bin(q.idx_no);
switch (sch->address_format) {
case CD_MSF_FORMAT:
data.what.position.reladdr.msf.unused = 0;
data.what.position.reladdr.msf.minute = bcd2bin(q.trk_size_msf[0]);
data.what.position.reladdr.msf.second = bcd2bin(q.trk_size_msf[1]);
data.what.position.reladdr.msf.frame = bcd2bin(q.trk_size_msf[2]);
data.what.position.absaddr.msf.unused = 0;
data.what.position.absaddr.msf.minute = bcd2bin(q.hd_pos_msf[0]);
data.what.position.absaddr.msf.second = bcd2bin(q.hd_pos_msf[1]);
data.what.position.absaddr.msf.frame = bcd2bin(q.hd_pos_msf[2]);
break;
case CD_LBA_FORMAT:
lba = msf2hsg(q.trk_size_msf, 1);
/*
* Pre-gap has index number of 0, and decreasing MSF
* address. Must be converted to negative LBA, per
* SCSI spec.
*/
if (data.what.position.index_number == 0)
lba = -lba;
data.what.position.reladdr.lba = htonl(lba);
data.what.position.absaddr.lba = htonl(msf2hsg(q.hd_pos_msf, 0));
break;
}
break;
}
MCD_UNLOCK(sc);
if (nocopyout == 0)
return copyout(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len));
bcopy(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len));
return (0);
}
static int
mcd_playmsf(struct mcd_softc *sc, struct ioc_play_msf *p)
{
struct mcd_read2 pb;
if (sc->data.debug)
device_printf(sc->dev, "playmsf: from %d:%d.%d to %d:%d.%d\n",
p->start_m, p->start_s, p->start_f,
p->end_m, p->end_s, p->end_f);
if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
(p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) ||
(p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) >
M_msf(sc->data.volinfo.vol_msf) * 60 * 75 +
S_msf(sc->data.volinfo.vol_msf) * 75 +
F_msf(sc->data.volinfo.vol_msf))
return (EINVAL);
pb.start_msf[0] = bin2bcd(p->start_m);
pb.start_msf[1] = bin2bcd(p->start_s);
pb.start_msf[2] = bin2bcd(p->start_f);
pb.end_msf[0] = bin2bcd(p->end_m);
pb.end_msf[1] = bin2bcd(p->end_s);
pb.end_msf[2] = bin2bcd(p->end_f);
if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
return (EIO);
return mcd_play(sc, &pb);
}
static int
mcd_playtracks(struct mcd_softc *sc, struct ioc_play_track *pt)
{
struct mcd_read2 pb;
int a = pt->start_track;
int z = pt->end_track;
int rc, i;
if ((rc = mcd_read_toc(sc)) != 0)
return (rc);
if (sc->data.debug)
device_printf(sc->dev, "playtracks from %d:%d to %d:%d\n",
a, pt->start_index, z, pt->end_index);
if ( a < bcd2bin(sc->data.volinfo.trk_low)
|| a > bcd2bin(sc->data.volinfo.trk_high)
|| a > z
|| z < bcd2bin(sc->data.volinfo.trk_low)
|| z > bcd2bin(sc->data.volinfo.trk_high))
return (EINVAL);
for (i = 0; i < 3; i++) {
pb.start_msf[i] = sc->data.toc[a].hd_pos_msf[i];
pb.end_msf[i] = sc->data.toc[z+1].hd_pos_msf[i];
}
if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
return (EIO);
return mcd_play(sc, &pb);
}
static int
mcd_playblocks(struct mcd_softc *sc, struct ioc_play_blocks *p)
{
struct mcd_read2 pb;
if (sc->data.debug)
device_printf(sc->dev, "playblocks: blkno %d length %d\n",
p->blk, p->len);
if (p->blk > sc->data.disksize || p->len > sc->data.disksize ||
p->blk < 0 || p->len < 0 ||
(p->blk + p->len) > sc->data.disksize)
return (EINVAL);
hsg2msf(p->blk, pb.start_msf);
hsg2msf(p->blk + p->len, pb.end_msf);
if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
return (EIO);
return mcd_play(sc, &pb);
}
static int
mcd_play(struct mcd_softc *sc, struct mcd_read2 *pb)
{
int retry, st = -1, status;
sc->data.lastpb = *pb;
for(retry=0; retry<MCD_RETRYS; retry++) {
critical_enter();
MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDSINGLESPEEDREAD);
MCD_WRITE(sc, MCD_REG_COMMAND, pb->start_msf[0]);
MCD_WRITE(sc, MCD_REG_COMMAND, pb->start_msf[1]);
MCD_WRITE(sc, MCD_REG_COMMAND, pb->start_msf[2]);
MCD_WRITE(sc, MCD_REG_COMMAND, pb->end_msf[0]);
MCD_WRITE(sc, MCD_REG_COMMAND, pb->end_msf[1]);
MCD_WRITE(sc, MCD_REG_COMMAND, pb->end_msf[2]);
critical_exit();
status=mcd_getstat(sc, 0);
if (status == -1)
continue;
else if (status != -2)
st = 0;
break;
}
if (status == -2) {
device_printf(sc->dev, "media changed\n");
return (ENXIO);
}
if (sc->data.debug)
device_printf(sc->dev,
"mcd_play retry=%d, status=0x%02x\n", retry, status);
if (st < 0)
return (ENXIO);
sc->data.audio_status = CD_AS_PLAY_IN_PROGRESS;
return (0);
}
static int
mcd_pause(struct mcd_softc *sc)
{
struct mcd_qchninfo q;
int rc;
/* Verify current status */
if (sc->data.audio_status != CD_AS_PLAY_IN_PROGRESS &&
sc->data.audio_status != CD_AS_PLAY_PAUSED) {
if (sc->data.debug)
device_printf(sc->dev,
"pause attempted when not playing, audio status %d\n",
sc->data.audio_status);
return (EINVAL);
}
/* Get the current position */
if (mcd_getqchan(sc, &q) < 0)
return (EIO);
/* Copy it into lastpb */
sc->data.lastpb.start_msf[0] = q.hd_pos_msf[0];
sc->data.lastpb.start_msf[1] = q.hd_pos_msf[1];
sc->data.lastpb.start_msf[2] = q.hd_pos_msf[2];
/* Stop playing */
if ((rc=mcd_stop(sc)) != 0)
return (rc);
/* Set the proper status and exit */
sc->data.audio_status = CD_AS_PLAY_PAUSED;
return (0);
}
static int
mcd_resume(struct mcd_softc *sc)
{
if (sc->data.audio_status != CD_AS_PLAY_PAUSED)
return (EINVAL);
return mcd_play(sc, &sc->data.lastpb);
}
|