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
|
/*
* IDE CD-ROM driver for FreeBSD.
* Supports ATAPI-compatible drives.
*
* Copyright (C) 1995 Cronyx Ltd.
* Author Serge Vakulenko, <vak@cronyx.ru>
*
* This software is distributed with NO WARRANTIES, not even the implied
* warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Authors grant any other persons or organisations permission to use
* or modify this software as long as this message is kept with the software,
* all derivative works or modified versions.
*
* From: Version 1.9, Mon Oct 9 20:27:42 MSK 1995
* $Id: wcd.c,v 1.50 1998/01/24 02:54:27 eivind Exp $
*/
#include "wdc.h"
#include "wcd.h"
#include "opt_atapi.h"
#include "opt_devfs.h"
#if NWCD > 0 && NWDC > 0 && defined (ATAPI)
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/disklabel.h>
#include <sys/cdio.h>
#include <sys/conf.h>
#ifdef DEVFS
#include <sys/devfsext.h>
#endif /*DEVFS*/
#include <i386/isa/atapi.h>
static d_open_t wcdropen;
static d_open_t wcdbopen;
static d_close_t wcdrclose;
static d_close_t wcdbclose;
static d_ioctl_t wcdioctl;
static d_strategy_t wcdstrategy;
#define CDEV_MAJOR 69
#define BDEV_MAJOR 19
extern struct cdevsw wcd_cdevsw;
static struct bdevsw wcd_bdevsw =
{ wcdbopen, wcdbclose, wcdstrategy, wcdioctl, /*19*/
nodump, nopsize, 0, "wcd", &wcd_cdevsw, -1 };
static struct cdevsw wcd_cdevsw =
{ wcdropen, wcdrclose, rawread, nowrite, /*69*/
wcdioctl, nostop, nullreset, nodevtotty,/* atapi */
seltrue, nommap, wcdstrategy, "wcd",
&wcd_bdevsw, -1 };
#ifndef ATAPI_STATIC
static
#endif
int wcdattach(struct atapi*, int, struct atapi_params*, int);
#define NUNIT 16 /* Max. number of devices */
#define SECSIZE 2048 /* CD-ROM sector size in bytes */
#define F_BOPEN 0x0001 /* The block device is opened */
#define F_MEDIA_CHANGED 0x0002 /* The media have changed since open */
#define F_DEBUG 0x0004 /* Print debug info */
#define F_LOCKED 0x0008 /* This unit is locked (or should be) */
/*
* Disc table of contents.
*/
#define MAXTRK 99
struct toc {
struct ioc_toc_header hdr;
struct cd_toc_entry tab[MAXTRK+1]; /* One extra for the leadout */
};
/*
* Volume size info.
*/
static struct volinfo {
u_long volsize; /* Volume size in blocks */
u_long blksize; /* Block size in bytes */
} info;
/*
* Current subchannel status.
*/
struct subchan {
u_char void0;
u_char audio_status;
u_short data_length;
u_char data_format;
u_char control;
u_char track;
u_char indx;
u_long abslba;
u_long rellba;
};
/*
* Audio Control Parameters Page
*/
struct audiopage {
/* Mode data header */
u_short data_length;
u_char medium_type;
u_char reserved1[5];
/* Audio control page */
u_char page_code;
#define AUDIO_PAGE 0x0e
#define AUDIO_PAGE_MASK 0x4e /* changeable values */
u_char param_len;
u_char flags;
#define CD_PA_SOTC 0x02 /* mandatory */
#define CD_PA_IMMED 0x04 /* always 1 */
u_char reserved3[3];
u_short lb_per_sec;
struct port_control {
u_char channels : 4;
#define CHANNEL_0 1 /* mandatory */
#define CHANNEL_1 2 /* mandatory */
#define CHANNEL_2 4 /* optional */
#define CHANNEL_3 8 /* optional */
u_char volume;
} port[4];
};
/*
* CD-ROM Capabilities and Mechanical Status Page
*/
struct cappage {
/* Mode data header */
u_short data_length;
u_char medium_type;
#define MDT_UNKNOWN 0x00
#define MDT_DATA_120 0x01
#define MDT_AUDIO_120 0x02
#define MDT_COMB_120 0x03
#define MDT_PHOTO_120 0x04
#define MDT_DATA_80 0x05
#define MDT_AUDIO_80 0x06
#define MDT_COMB_80 0x07
#define MDT_PHOTO_80 0x08
#define MDT_NO_DISC 0x70
#define MDT_DOOR_OPEN 0x71
#define MDT_FMT_ERROR 0x72
u_char reserved1[5];
/* Capabilities page */
u_char page_code;
#define CAP_PAGE 0x2a
u_char param_len;
u_char reserved2[2];
u_char audio_play : 1; /* audio play supported */
u_char composite : 1; /* composite audio/video supported */
u_char dport1 : 1; /* digital audio on port 1 */
u_char dport2 : 1; /* digital audio on port 2 */
u_char mode2_form1 : 1; /* mode 2 form 1 (XA) read */
u_char mode2_form2 : 1; /* mode 2 form 2 format */
u_char multisession : 1; /* multi-session photo-CD */
u_char : 1;
u_char cd_da : 1; /* audio-CD read supported */
u_char cd_da_stream : 1; /* CD-DA streaming */
u_char rw : 1; /* combined R-W subchannels */
u_char rw_corr : 1; /* R-W subchannel data corrected */
u_char c2 : 1; /* C2 error pointers supported */
u_char isrc : 1; /* can return the ISRC info */
u_char upc : 1; /* can return the catalog number UPC */
u_char : 1;
u_char lock : 1; /* could be locked */
u_char locked : 1; /* current lock state */
u_char prevent : 1; /* prevent jumper installed */
u_char eject : 1; /* can eject */
u_char : 1;
u_char mech : 3; /* loading mechanism type */
#define MECH_CADDY 0
#define MECH_TRAY 1
#define MECH_POPUP 2
#define MECH_CHANGER 4
#define MECH_CARTRIDGE 5
u_char sep_vol : 1; /* independent volume of channels */
u_char sep_mute : 1; /* independent mute of channels */
u_char : 6;
u_short max_speed; /* max raw data rate in bytes/1000 */
u_short max_vol_levels; /* number of discrete volume levels */
u_short buf_size; /* internal buffer size in bytes/1024 */
u_short cur_speed; /* current data rate in bytes/1000 */
/* Digital drive output format description (optional?) */
u_char reserved3;
u_char bckf : 1; /* data valid on failing edge of BCK */
u_char rch : 1; /* high LRCK indicates left channel */
u_char lsbf : 1; /* set if LSB first */
u_char dlen: 2;
#define DLEN_32 0 /* 32 BCKs */
#define DLEN_16 1 /* 16 BCKs */
#define DLEN_24 2 /* 24 BCKs */
#define DLEN_24_I2S 3 /* 24 BCKs (I2S) */
u_char : 3;
u_char reserved4[2];
};
/*
* CDROM changer mechanism status structure
*/
struct changer {
u_char current_slot : 5; /* active changer slot */
u_char mech_state : 2; /* current changer state */
#define CH_READY 0
#define CH_LOADING 1
#define CH_UNLOADING 2
#define CH_INITIALIZING 3
u_char fault : 1; /* fault in last operation */
u_char reserved0 : 5;
u_char cd_state : 3; /* current mechanism state */
#define CD_IDLE 0
#define CD_AUDIO_ACTIVE 1
#define CD_AUDIO_SCAN 2
#define CD_HOST_ACTIVE 3
#define CD_NO_STATE 7
u_char current_lba[3]; /* current LBA */
u_char slots; /* number of available slots */
u_short table_length; /* slot table length */
struct {
u_char changed : 1; /* media has changed in this slot */
u_char unused : 6;
u_char present : 1; /* slot has a CD present */
u_char reserved0;
u_char reserved1;
u_char reserved2;
} slot[32];
};
struct wcd {
struct atapi *ata; /* Controller structure */
int unit; /* IDE bus drive unit */
int lun; /* Logical device unit */
int flags; /* Device state flags */
int refcnt; /* The number of raw opens */
struct buf_queue_head buf_queue;/* Queue of i/o requests */
struct atapi_params *param; /* Drive parameters table */
struct toc toc; /* Table of disc contents */
struct volinfo info; /* Volume size info */
struct audiopage au; /* Audio page info */
struct cappage cap; /* Capabilities page info */
struct audiopage aumask; /* Audio page mask */
struct subchan subchan; /* Subchannel info */
char description[80]; /* Device description */
struct changer *changer_info; /* changer info */
int slot; /* this lun's slot number */
#ifdef DEVFS
void *ra_devfs_token;
void *rc_devfs_token;
void *a_devfs_token;
void *c_devfs_token;
#endif
};
static struct wcd *wcdtab[NUNIT]; /* Drive info by unit number */
static int wcdnlun = 0; /* Number of configured drives */
static struct wcd *wcd_init_lun(struct atapi *ata, int unit,
struct atapi_params *ap, int lun);
static void wcd_start (struct wcd *t);
static void wcd_done (struct wcd *t, struct buf *bp, int resid,
struct atapires result);
static void wcd_error (struct wcd *t, struct atapires result);
static int wcd_read_toc (struct wcd *t);
static int wcd_request_wait (struct wcd *t, u_char cmd, u_char a1, u_char a2,
u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8,
u_char a9, char *addr, int count);
static void wcd_describe (struct wcd *t);
static int wcd_open(dev_t dev, int rawflag);
static int wcd_setchan (struct wcd *t,
u_char c0, u_char c1, u_char c2, u_char c3);
static int wcd_eject (struct wcd *t, int closeit);
static void wcd_select_slot(struct wcd *cdp);
/*
* Dump the array in hexadecimal format for debugging purposes.
*/
static void wcd_dump (int lun, char *label, void *data, int len)
{
u_char *p = data;
printf ("wcd%d: %s %x", lun, label, *p++);
while (--len > 0)
printf ("-%x", *p++);
printf ("\n");
}
struct wcd *
wcd_init_lun(struct atapi *ata, int unit, struct atapi_params *ap, int lun)
{
struct wcd *ptr;
ptr = malloc(sizeof(struct wcd), M_TEMP, M_NOWAIT);
if (!ptr)
return NULL;
bzero(ptr, sizeof(struct wcd));
bufq_init(&ptr->buf_queue);
ptr->ata = ata;
ptr->unit = unit;
ptr->lun = lun;
ptr->param = ap;
ptr->flags = F_MEDIA_CHANGED;
ptr->refcnt = 0;
ptr->slot = -1;
ptr->changer_info = NULL;
#ifdef DEVFS
ptr->ra_devfs_token =
devfs_add_devswf(&wcd_cdevsw, dkmakeminor(lun, 0, 0),
DV_CHR, UID_ROOT, GID_OPERATOR, 0640,
"rwcd%da", lun);
ptr->rc_devfs_token =
devfs_add_devswf(&wcd_cdevsw, dkmakeminor(lun, 0, RAW_PART),
DV_CHR, UID_ROOT, GID_OPERATOR, 0640,
"rwcd%dc", lun);
ptr->a_devfs_token =
devfs_add_devswf(&wcd_bdevsw, dkmakeminor(lun, 0, 0),
DV_BLK, UID_ROOT, GID_OPERATOR, 0640,
"wcd%da", lun);
ptr->c_devfs_token =
devfs_add_devswf(&wcd_bdevsw, dkmakeminor(lun, 0, RAW_PART),
DV_BLK, UID_ROOT, GID_OPERATOR, 0640,
"wcd%dc", lun);
#endif
return ptr;
}
#ifndef ATAPI_STATIC
static
#endif
int
wcdattach (struct atapi *ata, int unit, struct atapi_params *ap, int debug)
{
struct wcd *cdp;
struct atapires result;
struct changer *chp;
int i;
if (wcdnlun >= NUNIT) {
printf ("wcd: too many units\n");
return (0);
}
if (!atapi_request_immediate) {
printf("wcd: configuration error, ATAPI core code not present!\n");
printf("wcd: check `options ATAPI_STATIC' in your kernel config file!\n");
return (0);
}
if ((cdp = wcd_init_lun(ata, unit, ap, wcdnlun)) == NULL) {
printf("wcd: out of memory\n");
return 0;
}
wcdtab[wcdnlun] = cdp;
if (debug) {
cdp->flags |= F_DEBUG;
/* Print params. */
wcd_dump (cdp->lun, "info", ap, sizeof *ap);
}
/* Get drive capabilities. */
result = atapi_request_immediate (ata, unit, ATAPI_MODE_SENSE,
0, CAP_PAGE, 0, 0, 0, 0, sizeof (cdp->cap) >> 8, sizeof (cdp->cap),
0, 0, 0, 0, 0, 0, 0, (char*) &cdp->cap, sizeof (cdp->cap));
/* Do it twice to avoid the stale media changed state. */
if (result.code == RES_ERR &&
(result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION)
result = atapi_request_immediate (ata, unit, ATAPI_MODE_SENSE,
0, CAP_PAGE, 0, 0, 0, 0, sizeof (cdp->cap) >> 8,
sizeof (cdp->cap), 0, 0, 0, 0, 0, 0, 0,
(char*) &cdp->cap, sizeof (cdp->cap));
/* Some drives have shorter capabilities page. */
if (result.code == RES_UNDERRUN)
result.code = 0;
if (result.code == 0) {
wcd_describe (cdp);
if (cdp->flags & F_DEBUG)
wcd_dump (cdp->lun, "cap", &cdp->cap, sizeof(cdp->cap));
}
/* If this is a changer device, allocate the neeeded lun's */
if (cdp->cap.mech == MECH_CHANGER) {
chp = malloc(sizeof(struct changer), M_TEMP, M_NOWAIT);
if (chp == NULL) {
printf("wcd: out of memory\n");
return 0;
}
bzero(chp, sizeof(struct changer));
result = atapi_request_immediate(ata, unit, ATAPI_MECH_STATUS,
0, 0, 0, 0, 0, 0, 0,
sizeof(struct changer)> 8, sizeof(struct changer),
0, 0, 0, 0, 0, 0,
(char*) chp, sizeof(struct changer));
if (cdp->flags & F_DEBUG) {
printf("result.code=%d curr=%02x slots=%d len=%d\n",
result.code, chp->current_slot, chp->slots,
htons(chp->table_length));
}
if (result.code == RES_UNDERRUN)
result.code = 0;
if (result.code == 0) {
chp->table_length = htons(chp->table_length);
for (i=0; i<chp->slots && wcdnlun<NUNIT; i++) {
if (i>0) {
cdp = wcd_init_lun(ata,unit,ap,wcdnlun);
if (cdp == NULL) {
printf("wcd: out of memory\n");
return 0;
}
}
cdp->slot = i;
cdp->changer_info = chp;
printf("wcd%d: changer slot %d %s\n",
wcdnlun,
i, (chp->slot[i].present ?
"disk present" : "no disk"));
wcdtab[wcdnlun++] = cdp;
}
if (wcdnlun >= NUNIT) {
printf ("wcd: too many units\n");
return (0);
}
}
}
else
wcdnlun++;
return (1);
}
void wcd_describe (struct wcd *t)
{
char *m;
t->cap.max_speed = ntohs (t->cap.max_speed);
t->cap.max_vol_levels = ntohs (t->cap.max_vol_levels);
t->cap.buf_size = ntohs (t->cap.buf_size);
t->cap.cur_speed = ntohs (t->cap.cur_speed);
printf ("wcd%d: ", t->lun);
if (t->cap.cur_speed != t->cap.max_speed)
printf ("%d/", t->cap.cur_speed * 1000 / 1024);
printf ("%dKb/sec", t->cap.max_speed * 1000 / 1024);
if (t->cap.buf_size)
printf (", %dKb cache", t->cap.buf_size);
if (t->cap.audio_play)
printf (", audio play");
if (t->cap.max_vol_levels)
printf (", %d volume levels", t->cap.max_vol_levels);
switch (t->cap.mech) {
default: m = 0; break;
case MECH_CADDY: m = "caddy"; break;
case MECH_TRAY: m = "tray"; break;
case MECH_POPUP: m = "popup"; break;
case MECH_CHANGER: m = "changer"; break;
case MECH_CARTRIDGE: m = "cartridge"; break;
}
if (m)
printf (", %s%s", t->cap.eject ? "ejectable " : "", m);
else if (t->cap.eject)
printf (", eject");
printf ("\n");
if (t->cap.mech != MECH_CHANGER) {
printf ("wcd%d: ", t->lun);
switch (t->cap.medium_type) {
case MDT_UNKNOWN:
printf ("medium type unknown"); break;
case MDT_DATA_120:
printf ("120mm data disc loaded"); break;
case MDT_AUDIO_120:
printf ("120mm audio disc loaded"); break;
case MDT_COMB_120:
printf ("120mm data/audio disc loaded"); break;
case MDT_PHOTO_120:
printf ("120mm photo disc loaded"); break;
case MDT_DATA_80:
printf ("80mm data disc loaded"); break;
case MDT_AUDIO_80:
printf ("80mm audio disc loaded"); break;
case MDT_COMB_80:
printf ("80mm data/audio disc loaded"); break;
case MDT_PHOTO_80:
printf ("80mm photo disc loaded"); break;
case MDT_NO_DISC:
printf ("no disc inside"); break;
case MDT_DOOR_OPEN:
printf ("door open"); break;
case MDT_FMT_ERROR:
printf ("medium format error"); break;
default:
printf ("medium type=0x%x", t->cap.medium_type); break;
}
}
if (t->cap.lock)
printf (t->cap.locked ? ", locked" : ", unlocked");
if (t->cap.prevent)
printf (", lock protected");
printf ("\n");
}
static int
wcd_open (dev_t dev, int rawflag)
{
int lun = dkunit(dev);
struct wcd *t;
/* Check device number is legal and ATAPI driver is loaded. */
if (lun >= wcdnlun || ! atapi_request_immediate)
return (ENXIO);
t = wcdtab[lun];
/* On the first open, read the table of contents. */
if (! (t->flags & F_BOPEN) && ! t->refcnt) {
/* Read table of contents. */
if (wcd_read_toc (t) < 0)
return (EIO);
/* Lock the media. */
wcd_request_wait (t, ATAPI_PREVENT_ALLOW,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
t->flags |= F_LOCKED;
}
if (rawflag)
++t->refcnt;
else
t->flags |= F_BOPEN;
return (0);
}
int wcdbopen (dev_t dev, int flags, int fmt, struct proc *p)
{
return wcd_open (dev, 0);
}
int wcdropen (dev_t dev, int flags, int fmt, struct proc *p)
{
return wcd_open (dev, 1);
}
/*
* Close the device. Only called if we are the LAST
* occurence of an open device.
*/
int wcdbclose (dev_t dev, int flags, int fmt, struct proc *p)
{
int lun = dkunit(dev);
struct wcd *t = wcdtab[lun];
/* If we were the last open of the entire device, release it. */
if (! t->refcnt)
wcd_request_wait (t, ATAPI_PREVENT_ALLOW,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
t->flags &= ~(F_BOPEN|F_LOCKED);
return (0);
}
int wcdrclose (dev_t dev, int flags, int fmt, struct proc *p)
{
int lun = dkunit(dev);
struct wcd *t = wcdtab[lun];
/* If we were the last open of the entire device, release it. */
if (! (t->flags & F_BOPEN) && t->refcnt == 1)
wcd_request_wait (t, ATAPI_PREVENT_ALLOW,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
t->flags &= ~F_LOCKED;
--t->refcnt;
return (0);
}
/*
* Actually translate the requested transfer into one the physical driver can
* understand. The transfer is described by a buf and will include only one
* physical transfer.
*/
void wcdstrategy (struct buf *bp)
{
int lun = dkunit(bp->b_dev);
struct wcd *t = wcdtab[lun];
int x;
/* Can't ever write to a CD. */
if (! (bp->b_flags & B_READ)) {
bp->b_error = EROFS;
bp->b_flags |= B_ERROR;
biodone (bp);
return;
}
/* If it's a null transfer, return immediatly. */
if (bp->b_bcount == 0) {
bp->b_resid = 0;
biodone (bp);
return;
}
/* Process transfer request. */
bp->b_pblkno = bp->b_blkno;
bp->b_resid = bp->b_bcount;
x = splbio();
/* Place it in the queue of disk activities for this disk. */
bufqdisksort (&t->buf_queue, bp);
/* Tell the device to get going on the transfer if it's
* not doing anything, otherwise just wait for completion. */
wcd_start (t);
splx(x);
}
/*
* Look to see if there is a buf waiting for the device
* and that the device is not already busy. If both are true,
* It dequeues the buf and creates an ATAPI command to perform the
* transfer in the buf.
* The bufs are queued by the strategy routine (wcdstrategy).
* Must be called at the correct (splbio) level.
*/
static void wcd_start (struct wcd *t)
{
struct buf *bp = bufq_first(&t->buf_queue);
u_long blkno, nblk;
/* See if there is a buf to do and we are not already doing one. */
if (! bp)
return;
/* Unqueue the request. */
bufq_remove(&t->buf_queue, bp);
/* Should reject all queued entries if media have changed. */
if (t->flags & F_MEDIA_CHANGED) {
bp->b_error = EIO;
bp->b_flags |= B_ERROR;
biodone (bp);
return;
}
wcd_select_slot(t);
/* We have a buf, now we should make a command
* First, translate the block to absolute and put it in terms of the
* logical blocksize of the device.
* What if something asks for 512 bytes not on a 2k boundary? */
blkno = bp->b_blkno / (SECSIZE / 512);
nblk = (bp->b_bcount + (SECSIZE - 1)) / SECSIZE;
atapi_request_callback (t->ata, t->unit, ATAPI_READ_BIG, 0,
blkno>>24, blkno>>16, blkno>>8, blkno, 0, nblk>>8, nblk, 0, 0,
0, 0, 0, 0, 0, (u_char*) bp->b_data, bp->b_bcount,
wcd_done, t, bp);
}
static void wcd_done (struct wcd *t, struct buf *bp, int resid,
struct atapires result)
{
if (result.code) {
wcd_error (t, result);
bp->b_error = EIO;
bp->b_flags |= B_ERROR;
} else
bp->b_resid = resid;
biodone (bp);
wcd_start (t);
}
static void wcd_error (struct wcd *t, struct atapires result)
{
if (result.code != RES_ERR)
return;
switch (result.error & AER_SKEY) {
case AER_SK_NOT_READY:
if (result.error & ~AER_SKEY) {
/* Audio disc. */
printf ("wcd%d: cannot read audio disc\n", t->lun);
return;
}
/* Tray open. */
if (! (t->flags & F_MEDIA_CHANGED))
printf ("wcd%d: tray open\n", t->lun);
t->flags |= F_MEDIA_CHANGED;
return;
case AER_SK_UNIT_ATTENTION:
/* Media changed. */
if (! (t->flags & F_MEDIA_CHANGED))
printf ("wcd%d: media changed\n", t->lun);
t->flags |= F_MEDIA_CHANGED;
return;
case AER_SK_ILLEGAL_REQUEST:
/* Unknown command or invalid command arguments. */
if (t->flags & F_DEBUG)
printf ("wcd%d: invalid command\n", t->lun);
return;
}
printf ("wcd%d: i/o error, status=%b, error=%b\n", t->lun,
result.status, ARS_BITS, result.error, AER_BITS);
}
static int wcd_request_wait (struct wcd *t, u_char cmd, u_char a1, u_char a2,
u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8,
u_char a9, char *addr, int count)
{
struct atapires result;
result = atapi_request_wait (t->ata, t->unit, cmd,
a1, a2, a3, a4, a5, a6, a7, a8, a9, 0, 0, 0, 0, 0, 0,
addr, count);
if (result.code) {
wcd_error (t, result);
return (EIO);
}
return (0);
}
static inline void lba2msf (int lba, u_char *m, u_char *s, u_char *f)
{
lba += 150; /* offset of first logical frame */
lba &= 0xffffff; /* negative lbas use only 24 bits */
*m = lba / (60 * 75);
lba %= (60 * 75);
*s = lba / 75;
*f = lba % 75;
}
/*
* Perform special action on behalf of the user.
* Knows about the internals of this device
*/
int wcdioctl (dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
{
int lun = dkunit(dev);
struct wcd *t = wcdtab[lun];
int error = 0;
if (t->flags & F_MEDIA_CHANGED)
switch (cmd) {
case CDIOCSETDEBUG:
case CDIOCCLRDEBUG:
case CDIOCRESET:
/* These ops are media change transparent. */
break;
default:
/* Read table of contents. */
wcd_read_toc (t);
/* Lock the media. */
wcd_request_wait (t, ATAPI_PREVENT_ALLOW,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
t->flags |= F_LOCKED;
break;
}
switch (cmd) {
default:
return (ENOTTY);
case CDIOCSETDEBUG:
if (p->p_cred->pc_ucred->cr_uid)
return (EPERM);
t->flags |= F_DEBUG;
atapi_debug (t->ata, 1);
return 0;
case CDIOCCLRDEBUG:
if (p->p_cred->pc_ucred->cr_uid)
return (EPERM);
t->flags &= ~F_DEBUG;
atapi_debug (t->ata, 0);
return 0;
case CDIOCRESUME:
return wcd_request_wait (t, ATAPI_PAUSE,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0);
case CDIOCPAUSE:
return wcd_request_wait (t, ATAPI_PAUSE,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
case CDIOCSTART:
return wcd_request_wait (t, ATAPI_START_STOP,
1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
case CDIOCSTOP:
return wcd_request_wait (t, ATAPI_START_STOP,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
case CDIOCALLOW:
wcd_select_slot(t);
t->flags &= ~F_LOCKED;
return wcd_request_wait (t, ATAPI_PREVENT_ALLOW,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
case CDIOCPREVENT:
wcd_select_slot(t);
t->flags |= F_LOCKED;
return wcd_request_wait (t, ATAPI_PREVENT_ALLOW,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
case CDIOCRESET:
if (p->p_cred->pc_ucred->cr_uid)
return (EPERM);
return wcd_request_wait (t, ATAPI_TEST_UNIT_READY,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
case CDIOCEJECT:
/* Don't allow eject if the device is opened
* by somebody (not us) in block mode. */
if ((t->flags & F_BOPEN) && t->refcnt)
return (EBUSY);
return wcd_eject (t, 0);
case CDIOCCLOSE:
if ((t->flags & F_BOPEN) && t->refcnt)
return (0);
return wcd_eject (t, 1);
case CDIOREADTOCHEADER:
if (! t->toc.hdr.ending_track)
return (EIO);
bcopy (&t->toc.hdr, addr, sizeof t->toc.hdr);
break;
case CDIOREADTOCENTRYS: {
struct ioc_read_toc_entry *te =
(struct ioc_read_toc_entry*) addr;
struct toc *toc = &t->toc;
struct toc buf;
u_long len;
u_char starting_track = te->starting_track;
if (! t->toc.hdr.ending_track)
return (EIO);
if ( te->data_len < sizeof(toc->tab[0])
|| (te->data_len % sizeof(toc->tab[0])) != 0
|| te->address_format != CD_MSF_FORMAT
&& te->address_format != CD_LBA_FORMAT
)
return EINVAL;
if (starting_track == 0)
starting_track = toc->hdr.starting_track;
else if (starting_track == 170) /* Handle leadout request */
starting_track = toc->hdr.ending_track + 1;
else if (starting_track < toc->hdr.starting_track ||
starting_track > toc->hdr.ending_track + 1)
return (EINVAL);
len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
sizeof(toc->tab[0]);
if (te->data_len < len)
len = te->data_len;
if (len > sizeof(toc->tab))
return EINVAL;
/* Convert to MSF format, if needed. */
if (te->address_format == CD_MSF_FORMAT) {
struct cd_toc_entry *e;
buf = t->toc;
toc = &buf;
e = toc->tab + (toc->hdr.ending_track + 1 -
toc->hdr.starting_track) + 1;
while (--e >= toc->tab)
lba2msf (ntohl(e->addr.lba), &e->addr.msf.minute,
&e->addr.msf.second, &e->addr.msf.frame);
}
return copyout (toc->tab + starting_track -
toc->hdr.starting_track, te->data, len);
}
case CDIOREADTOCENTRY: {
struct ioc_read_toc_single_entry *te =
(struct ioc_read_toc_single_entry*) addr;
struct toc *toc = &t->toc;
struct toc buf;
u_char track = te->track;
if (! t->toc.hdr.ending_track)
return (EIO);
if (te->address_format != CD_MSF_FORMAT
&& te->address_format != CD_LBA_FORMAT)
return EINVAL;
if (track == 0)
track = toc->hdr.starting_track;
else if (track == 170) /* Handle leadout request */
track = toc->hdr.ending_track + 1;
else if (track < toc->hdr.starting_track ||
track > toc->hdr.ending_track + 1)
return (EINVAL);
/* Convert to MSF format, if needed. */
if (te->address_format == CD_MSF_FORMAT) {
struct cd_toc_entry *e;
buf = t->toc;
toc = &buf;
e = toc->tab + (track - toc->hdr.starting_track);
lba2msf (ntohl(e->addr.lba), &e->addr.msf.minute,
&e->addr.msf.second, &e->addr.msf.frame);
}
bcopy(toc->tab + track - toc->hdr.starting_track,
&te->entry, sizeof(struct cd_toc_entry));
}
case CDIOCREADSUBCHANNEL: {
struct ioc_read_subchannel *args =
(struct ioc_read_subchannel*) addr;
struct cd_sub_channel_info data;
u_long len = args->data_len;
int abslba, rellba;
if (len > sizeof(data) ||
len < sizeof(struct cd_sub_channel_header))
return (EINVAL);
if (wcd_request_wait (t, ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0,
0, 0, sizeof (t->subchan) >> 8, sizeof (t->subchan),
0, (char*)&t->subchan, sizeof (t->subchan)) != 0)
return (EIO);
if (t->flags & F_DEBUG)
wcd_dump (t->lun, "subchan", &t->subchan, sizeof t->subchan);
abslba = t->subchan.abslba;
rellba = t->subchan.rellba;
if (args->address_format == CD_MSF_FORMAT) {
lba2msf (ntohl(abslba),
&data.what.position.absaddr.msf.minute,
&data.what.position.absaddr.msf.second,
&data.what.position.absaddr.msf.frame);
lba2msf (ntohl(rellba),
&data.what.position.reladdr.msf.minute,
&data.what.position.reladdr.msf.second,
&data.what.position.reladdr.msf.frame);
} else {
data.what.position.absaddr.lba = abslba;
data.what.position.reladdr.lba = rellba;
}
data.header.audio_status = t->subchan.audio_status;
data.what.position.control = t->subchan.control & 0xf;
data.what.position.addr_type = t->subchan.control >> 4;
data.what.position.track_number = t->subchan.track;
data.what.position.index_number = t->subchan.indx;
return copyout (&data, args->data, len);
}
case CDIOCPLAYMSF: {
struct ioc_play_msf *args = (struct ioc_play_msf*) addr;
return wcd_request_wait (t, ATAPI_PLAY_MSF, 0, 0,
args->start_m, args->start_s, args->start_f,
args->end_m, args->end_s, args->end_f, 0, 0, 0);
}
case CDIOCPLAYBLOCKS: {
struct ioc_play_blocks *args = (struct ioc_play_blocks*) addr;
return wcd_request_wait (t, ATAPI_PLAY_BIG, 0,
args->blk >> 24 & 0xff, args->blk >> 16 & 0xff,
args->blk >> 8 & 0xff, args->blk & 0xff,
args->len >> 24 & 0xff, args->len >> 16 & 0xff,
args->len >> 8 & 0xff, args->len & 0xff, 0, 0);
}
case CDIOCPLAYTRACKS: {
struct ioc_play_track *args = (struct ioc_play_track*) addr;
u_long start, len;
int t1, t2;
if (! t->toc.hdr.ending_track)
return (EIO);
/* Ignore index fields,
* play from start_track to end_track inclusive. */
if (args->end_track < t->toc.hdr.ending_track+1)
++args->end_track;
if (args->end_track > t->toc.hdr.ending_track+1)
args->end_track = t->toc.hdr.ending_track+1;
t1 = args->start_track - t->toc.hdr.starting_track;
t2 = args->end_track - t->toc.hdr.starting_track;
if (t1 < 0 || t2 < 0)
return (EINVAL);
start = ntohl(t->toc.tab[t1].addr.lba);
len = ntohl(t->toc.tab[t2].addr.lba) - start;
return wcd_request_wait (t, ATAPI_PLAY_BIG, 0,
start >> 24 & 0xff, start >> 16 & 0xff,
start >> 8 & 0xff, start & 0xff,
len >> 24 & 0xff, len >> 16 & 0xff,
len >> 8 & 0xff, len & 0xff, 0, 0);
}
case CDIOCGETVOL: {
struct ioc_vol *arg = (struct ioc_vol*) addr;
error = wcd_request_wait (t, ATAPI_MODE_SENSE, 0, AUDIO_PAGE,
0, 0, 0, 0, sizeof (t->au) >> 8, sizeof (t->au), 0,
(char*) &t->au, sizeof (t->au));
if (error)
return (error);
if (t->flags & F_DEBUG)
wcd_dump (t->lun, "au", &t->au, sizeof t->au);
if (t->au.page_code != AUDIO_PAGE)
return (EIO);
arg->vol[0] = t->au.port[0].volume;
arg->vol[1] = t->au.port[1].volume;
arg->vol[2] = t->au.port[2].volume;
arg->vol[3] = t->au.port[3].volume;
break;
}
case CDIOCSETVOL: {
struct ioc_vol *arg = (struct ioc_vol*) addr;
error = wcd_request_wait (t, ATAPI_MODE_SENSE, 0, AUDIO_PAGE,
0, 0, 0, 0, sizeof (t->au) >> 8, sizeof (t->au), 0,
(char*) &t->au, sizeof (t->au));
if (error)
return (error);
if (t->flags & F_DEBUG)
wcd_dump (t->lun, "au", &t->au, sizeof t->au);
if (t->au.page_code != AUDIO_PAGE)
return (EIO);
error = wcd_request_wait (t, ATAPI_MODE_SENSE, 0,
AUDIO_PAGE_MASK, 0, 0, 0, 0, sizeof (t->aumask) >> 8,
sizeof (t->aumask), 0, (char*) &t->aumask,
sizeof (t->aumask));
if (error)
return (error);
if (t->flags & F_DEBUG)
wcd_dump (t->lun, "mask", &t->aumask, sizeof t->aumask);
/* Sony-55E requires the data length field to be zeroed. */
t->au.data_length = 0;
t->au.port[0].channels = CHANNEL_0;
t->au.port[1].channels = CHANNEL_1;
t->au.port[0].volume = arg->vol[0] & t->aumask.port[0].volume;
t->au.port[1].volume = arg->vol[1] & t->aumask.port[1].volume;
t->au.port[2].volume = arg->vol[2] & t->aumask.port[2].volume;
t->au.port[3].volume = arg->vol[3] & t->aumask.port[3].volume;
return wcd_request_wait (t, ATAPI_MODE_SELECT_BIG, 0x10,
0, 0, 0, 0, 0, sizeof (t->au) >> 8, sizeof (t->au),
0, (char*) &t->au, - sizeof (t->au));
}
case CDIOCSETPATCH: {
struct ioc_patch *arg = (struct ioc_patch*) addr;
return wcd_setchan (t, arg->patch[0], arg->patch[1],
arg->patch[2], arg->patch[3]);
}
case CDIOCSETMONO:
return wcd_setchan (t, CHANNEL_0 | CHANNEL_1,
CHANNEL_0 | CHANNEL_1, 0, 0);
case CDIOCSETSTERIO:
return wcd_setchan (t, CHANNEL_0, CHANNEL_1, 0, 0);
case CDIOCSETMUTE:
return wcd_setchan (t, 0, 0, 0, 0);
case CDIOCSETLEFT:
return wcd_setchan (t, CHANNEL_0, CHANNEL_0, 0, 0);
case CDIOCSETRIGHT:
return wcd_setchan (t, CHANNEL_1, CHANNEL_1, 0, 0);
}
return (error);
}
/*
* Read the entire TOC for the disc into our internal buffer.
*/
static int wcd_read_toc (struct wcd *t)
{
int ntracks, len;
struct atapires result;
bzero (&t->toc, sizeof (t->toc));
bzero (&t->info, sizeof (t->info));
wcd_select_slot(t);
/* Check for the media.
* Do it twice to avoid the stale media changed state. */
result = atapi_request_wait (t->ata, t->unit, ATAPI_TEST_UNIT_READY,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if (result.code == RES_ERR &&
(result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION) {
t->flags |= F_MEDIA_CHANGED;
result = atapi_request_wait (t->ata, t->unit,
ATAPI_TEST_UNIT_READY, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
}
if (result.code) {
wcd_error (t, result);
return (EIO);
}
t->flags &= ~F_MEDIA_CHANGED;
/* First read just the header, so we know how long the TOC is. */
len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
if (wcd_request_wait (t, ATAPI_READ_TOC, 0, 0, 0, 0, 0, 0,
len >> 8, len & 0xff, 0, (char*)&t->toc, len) != 0) {
err: bzero (&t->toc, sizeof (t->toc));
return (0);
}
ntracks = t->toc.hdr.ending_track - t->toc.hdr.starting_track + 1;
if (ntracks <= 0 || ntracks > MAXTRK)
goto err;
/* Now read the whole schmeer. */
len = sizeof(struct ioc_toc_header) +
ntracks * sizeof(struct cd_toc_entry);
if (wcd_request_wait (t, ATAPI_READ_TOC, 0, 0, 0, 0, 0, 0,
len >> 8, len & 0xff, 0, (char*)&t->toc, len) & 0xff)
goto err;
NTOHS(t->toc.hdr.len);
/* Read disc capacity. */
if (wcd_request_wait (t, ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0,
0, sizeof(t->info), 0, (char*)&t->info, sizeof(t->info)) != 0)
bzero (&t->info, sizeof (t->info));
/* make fake leadout entry */
t->toc.tab[ntracks].control = t->toc.tab[ntracks-1].control;
t->toc.tab[ntracks].addr_type = t->toc.tab[ntracks-1].addr_type;
t->toc.tab[ntracks].track = 170; /* magic */
t->toc.tab[ntracks].addr.lba = t->info.volsize;
NTOHL(t->info.volsize);
NTOHL(t->info.blksize);
/* Print the disc description string on every disc change.
* It would help to track the history of disc changes. */
if (t->info.volsize && t->toc.hdr.ending_track &&
(t->flags & F_MEDIA_CHANGED) && (t->flags & F_DEBUG)) {
printf ("wcd%d: ", t->lun);
if (t->toc.tab[0].control & 4)
printf ("%ldMB ", t->info.volsize / 512);
else
printf ("%ld:%ld audio ", t->info.volsize/75/60,
t->info.volsize/75%60);
printf ("(%ld sectors), %d tracks\n", t->info.volsize,
t->toc.hdr.ending_track - t->toc.hdr.starting_track + 1);
}
return (0);
}
/*
* Set up the audio channel masks.
*/
static int wcd_setchan (struct wcd *t,
u_char c0, u_char c1, u_char c2, u_char c3)
{
int error;
error = wcd_request_wait (t, ATAPI_MODE_SENSE, 0, AUDIO_PAGE,
0, 0, 0, 0, sizeof (t->au) >> 8, sizeof (t->au), 0,
(char*) &t->au, sizeof (t->au));
if (error)
return (error);
if (t->flags & F_DEBUG)
wcd_dump (t->lun, "au", &t->au, sizeof t->au);
if (t->au.page_code != AUDIO_PAGE)
return (EIO);
/* Sony-55E requires the data length field to be zeroed. */
t->au.data_length = 0;
t->au.port[0].channels = c0;
t->au.port[1].channels = c1;
t->au.port[2].channels = c2;
t->au.port[3].channels = c3;
return wcd_request_wait (t, ATAPI_MODE_SELECT_BIG, 0x10,
0, 0, 0, 0, 0, sizeof (t->au) >> 8, sizeof (t->au),
0, (char*) &t->au, - sizeof (t->au));
}
static int wcd_eject (struct wcd *t, int closeit)
{
struct atapires result;
wcd_select_slot(t);
/* Try to stop the disc. */
result = atapi_request_wait (t->ata, t->unit, ATAPI_START_STOP,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if (result.code == RES_ERR &&
((result.error & AER_SKEY) == AER_SK_NOT_READY ||
(result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION)) {
int err;
if (!closeit)
return (0);
/*
* The disc was unloaded.
* Load it (close tray).
* Read the table of contents.
*/
err = wcd_request_wait (t, ATAPI_START_STOP,
0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0);
if (err)
return (err);
/* Read table of contents. */
wcd_read_toc (t);
/* Lock the media. */
wcd_request_wait (t, ATAPI_PREVENT_ALLOW,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
t->flags |= F_LOCKED;
return (0);
}
if (result.code) {
wcd_error (t, result);
return (EIO);
}
if (closeit)
return (0);
/* Give it some time to stop spinning. */
tsleep ((caddr_t)&lbolt, PRIBIO, "wcdej1", 0);
tsleep ((caddr_t)&lbolt, PRIBIO, "wcdej2", 0);
/* Unlock. */
wcd_request_wait (t, ATAPI_PREVENT_ALLOW,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
t->flags &= ~F_LOCKED;
/* Eject. */
t->flags |= F_MEDIA_CHANGED;
return wcd_request_wait (t, ATAPI_START_STOP,
0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0);
}
static void
wcd_select_slot(struct wcd *cdp)
{
if (cdp->slot < 0 || cdp->changer_info->current_slot == cdp->slot)
return;
/* Unlock (might not be needed but its cheaper than asking) */
wcd_request_wait (cdp, ATAPI_PREVENT_ALLOW,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
/* Unload the current media from player */
wcd_request_wait (cdp, ATAPI_LOAD_UNLOAD,
0, 0, 0, 2, 0, 0, 0, cdp->changer_info->current_slot, 0, 0, 0);
/* load the wanted slot */
wcd_request_wait (cdp, ATAPI_LOAD_UNLOAD,
0, 0, 0, 3, 0, 0, 0, cdp->slot, 0, 0, 0);
cdp->changer_info->current_slot = cdp->slot;
/* Lock the media if needed */
if (cdp->flags & F_LOCKED) {
wcd_request_wait (cdp, ATAPI_PREVENT_ALLOW,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
}
}
#ifdef WCD_MODULE
/*
* Loadable ATAPI CD-ROM driver stubs.
*/
#include <sys/exec.h>
#include <sys/sysent.h>
#include <sys/lkm.h>
/*
* Construct lkm_dev structures (see lkm.h).
* Our bdevsw/cdevsw slot numbers are 19/69.
*/
MOD_DEV(wcd, LM_DT_BLOCK, BDEV_MAJOR, &wcd_bdevsw);
MOD_DEV(rwcd, LM_DT_CHAR, CDEV_MAJOR, &wcd_cdevsw);
/*
* Function called when loading the driver.
*/
int wcd_load (struct lkm_table *lkmtp, int cmd)
{
struct atapi *ata;
int n, u;
if (! atapi_start)
/* No ATAPI driver available. */
return EPROTONOSUPPORT;
n = 0;
for (ata=atapi_tab; ata<atapi_tab+2; ++ata)
if (ata->port)
for (u=0; u<2; ++u)
/* Probing controller ata->ctrlr, unit u. */
if (ata->params[u] && ! ata->attached[u] &&
wcdattach (ata, u, ata->params[u],
ata->debug) >= 0)
{
/* Drive found. */
ata->attached[u] = 1;
++n;
}
if (! n)
/* No IDE CD-ROMs found. */
return ENXIO;
return 0;
}
/*
* Function called when unloading the driver.
*/
int wcd_unload (struct lkm_table *lkmtp, int cmd)
{
struct wcd **t;
for (t=wcdtab; t<wcdtab+wcdnlun; ++t)
if (((*t)->flags & F_BOPEN) || (*t)->refcnt)
/* The device is opened, cannot unload the driver. */
return EBUSY;
for (t=wcdtab; t<wcdtab+wcdnlun; ++t) {
(*t)->ata->attached[(*t)->unit] = 0;
free (*t, M_TEMP);
}
wcdnlun = 0;
bzero (wcdtab, sizeof(wcdtab));
return 0;
}
/*
* Dispatcher function for the module (load/unload/stat).
*/
int wcd_mod (struct lkm_table *lkmtp, int cmd, int ver)
{
int err = 0;
if (ver != LKM_VERSION)
return EINVAL;
if (cmd == LKM_E_LOAD)
err = wcd_load (lkmtp, cmd);
else if (cmd == LKM_E_UNLOAD)
err = wcd_unload (lkmtp, cmd);
if (err)
return err;
/* XXX Poking around in the LKM internals like this is bad.
*/
/* Register the cdevsw entry. */
lkmtp->private.lkm_dev = & MOD_PRIVATE(rwcd);
err = lkmdispatch (lkmtp, cmd);
if (err)
return err;
/* Register the bdevsw entry. */
lkmtp->private.lkm_dev = & MOD_PRIVATE(wcd);
return lkmdispatch (lkmtp, cmd);
}
#endif /* WCD_MODULE */
static wcd_devsw_installed = 0;
static void wcd_drvinit(void *unused)
{
dev_t dev;
if( ! wcd_devsw_installed ) {
dev = makedev(CDEV_MAJOR, 0);
cdevsw_add(&dev,&wcd_cdevsw, NULL);
dev = makedev(BDEV_MAJOR, 0);
bdevsw_add(&dev,&wcd_bdevsw, NULL);
wcd_devsw_installed = 1;
}
}
SYSINIT(wcddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,wcd_drvinit,NULL)
#endif /* NWCD && NWDC && ATAPI */
|