summaryrefslogtreecommitdiffstats
path: root/sys/dev/ata/atapi-cd.c
blob: 200b64871cd1ba86c40089cff86b7c4cf39551c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
/*-
 * Copyright (c) 1998,1999 Søren Schmidt
 * 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,
 *    without modification, immediately at the beginning of the file.
 * 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. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
 *
 *	$Id: atapi-cd.c,v 1.9 1999/05/30 16:51:14 phk Exp $
 */

#include "ata.h"
#include "atapicd.h"
#include "opt_devfs.h"

#if NATA > 0 && NATAPICD > 0

#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/devicestat.h>
#include <sys/cdio.h>
#include <sys/wormio.h>
#include <sys/fcntl.h>
#include <sys/conf.h>
#include <sys/stat.h>
#ifdef DEVFS
#include <sys/devfsext.h>
#endif
#include <pci/pcivar.h>
#include <dev/ata/ata-all.h>
#include <dev/ata/atapi-all.h>
#include <dev/ata/atapi-cd.h>

static d_open_t		acdopen;
static d_close_t	acdclose;
static d_ioctl_t	acdioctl;
static d_strategy_t	acdstrategy;

#define BDEV_MAJOR 31
#define CDEV_MAJOR 117
static struct cdevsw acd_cdevsw = {
	/* open */	acdopen,
	/* close */	acdclose,
	/* read */	physread,
	/* write */	physwrite,
	/* ioctl */	acdioctl,
	/* stop */	nostop,
	/* reset */	noreset,
	/* devtotty */	nodevtotty,
	/* poll */	nopoll,
	/* mmap */	nommap,
	/* strategy */	acdstrategy,
	/* name */	"acd",
	/* parms */	noparms,
	/* maj */	CDEV_MAJOR,
	/* dump */	nodump,
	/* psize */	nopsize,
	/* flags */	D_DISK,
	/* maxio */	0,
	/* bmaj */	BDEV_MAJOR
};

#define NUNIT	16			/* Max # of devices */

#define F_BOPEN         0x0001  	/* The block device is opened */
#define F_MEDIA_CHANGED 0x0002  	/* The media have changed since open */
#define F_LOCKED        0x0004 		/* This unit is locked (or should be) */
#define F_TRACK_PREP    0x0008  	/* Track should be prep'ed */
#define F_TRACK_PREPED  0x0010  	/* Track has been prep'ed */
#define F_DISK_PREPED   0x0020  	/* Disk has been prep'ed */
#define F_WRITTEN   	0x0040  	/* The medium has been written to */

static struct acd_softc *acdtab[NUNIT];
static int32_t acdnlun = 0;     	/* Number of configured drives */

int32_t acdattach(struct atapi_softc *);
static struct acd_softc *acd_init_lun(struct atapi_softc *, int, struct devstat *);
static void acd_start(struct acd_softc *);
static void acd_done(struct atapi_request *);
static int32_t acd_test_unit_ready (struct acd_softc *);
static int32_t acd_lock_device (struct acd_softc *, int32_t);
static int32_t acd_start_device (struct acd_softc *, int32_t);
static int32_t acd_pause_device (struct acd_softc *, int32_t);
static int32_t acd_mode_sense (struct acd_softc *, u_int8_t, void *, int32_t);
static int32_t acd_mode_select (struct acd_softc *, void *, int32_t);
static int32_t acd_read_toc(struct acd_softc *);
static void acd_describe(struct acd_softc *);
static int32_t acd_setchan(struct acd_softc *, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
static int32_t acd_eject(struct acd_softc *, int);
static void acd_select_slot(struct acd_softc *);
static int32_t acd_open_disk(struct acd_softc *, int);
static int32_t acd_open_track(struct acd_softc *, struct wormio_prepare_track *);
static int32_t acd_close_track(struct acd_softc *);
static int32_t acd_close_disk(struct acd_softc *);
static int32_t acd_read_track_info(struct acd_softc *, int, struct acd_track_info*);
static int32_t acd_blank_disk(struct acd_softc *);
static void lba2msf(int32_t, u_int8_t *, u_int8_t *, u_int8_t *);
static int32_t msf2lba(u_int8_t, u_int8_t, u_int8_t);
static void acd_drvinit(void *);

int
acdattach(struct atapi_softc *atp)
{
    struct acd_softc *cdp;
    struct changer *chp;
    int32_t error, count;

    if (acdnlun >= NUNIT) {
        printf("acd: too many units\n");
        return -1;
    }
    if ((cdp = acd_init_lun(atp, acdnlun, NULL)) == NULL) {
        printf("acd: out of memory\n");
        return -1;
    }

    /* Get drive capabilities, some drives needs this repeated */
    for (count = 0 ; count < 5 ; count++) {
	if (!(error = acd_mode_sense(cdp, ATAPI_CDROM_CAP_PAGE,
				     &cdp->cap, sizeof(cdp->cap))))
            break;
    }
    if (error) {
	free(cdp, M_TEMP);
	return -1;
    }
    cdp->cap.max_speed = ntohs(cdp->cap.max_speed);
    cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
    cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
    cdp->cap.cur_speed = ntohs(cdp->cap.cur_speed);
    acd_describe(cdp);

    /* If this is a changer device, allocate the neeeded lun's */
    if (cdp->cap.mech == MST_MECH_CHANGER) {
	int8_t ccb[16] = { ATAPI_MECH_STATUS,
                           0, 0, 0, 0, 0, 0, 0, 
			   sizeof(struct changer)>>8, sizeof(struct changer),
                           0, 0, 0, 0, 0, 0 };

        chp = malloc(sizeof(struct changer), M_TEMP, M_NOWAIT);
        if (chp == NULL) {
            printf("acd: out of memory\n");
            return 0;
        }
        bzero(chp, sizeof(struct changer));
        error = atapi_queue_cmd(cdp->atp, ccb, chp, sizeof(struct changer), 
				A_READ, NULL, NULL, NULL);

#ifdef ACD_DEBUG
        printf("error=%02x curr=%02x slots=%d len=%d\n",
               error, chp->current_slot, chp->slots, htons(chp->table_length));
#endif

        if (!error) {
    	    struct acd_softc *tmpcdp = cdp;
	    int32_t count;
	    int8_t string[16];

            chp->table_length = htons(chp->table_length);
            for (count = 0; count < chp->slots && acdnlun < NUNIT; count++) {
                if (count > 0) {
                    tmpcdp = acd_init_lun(atp, acdnlun, cdp->stats);
		    if (!tmpcdp) {
                        printf("acd: out of memory\n");
                        return -1;
                    }
                }
                tmpcdp->slot = count;
                tmpcdp->changer_info = chp;
                printf("acd%d: changer slot %d %s\n", acdnlun, count,
		       (chp->slot[count].present ? "CD present" : "empty"));
                acdtab[acdnlun++] = tmpcdp;
            }
            if (acdnlun >= NUNIT) {
                printf("acd: too many units\n");
                return 0;
            }
	    sprintf(string, "acd%d-", cdp->lun);
            devstat_add_entry(cdp->stats, string, tmpcdp->lun, DEV_BSIZE,
                              DEVSTAT_NO_ORDERED_TAGS,
                              DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
			      0x178);
        }
    }
    else {
        acdtab[acdnlun++] = cdp;
        devstat_add_entry(cdp->stats, "acd", cdp->lun, DEV_BSIZE,
                          DEVSTAT_NO_ORDERED_TAGS,
                          DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
			  0x178);
    }
    return 0;
}

static struct acd_softc *
acd_init_lun(struct atapi_softc *atp, int32_t lun, struct devstat *stats)
{
    struct acd_softc *acd;

    if (!(acd = malloc(sizeof(struct acd_softc), M_TEMP, M_NOWAIT)))
        return NULL;
    bzero(acd, sizeof(struct acd_softc));
    bufq_init(&acd->buf_queue);
    acd->atp = atp;
    acd->lun = lun;
    acd->flags = F_MEDIA_CHANGED;
    acd->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
    acd->block_size = 2048;
    acd->refcnt = 0;
    acd->slot = -1;
    acd->changer_info = NULL;
    if (stats == NULL) {
        if (!(acd->stats = malloc(sizeof(struct devstat), 
					 M_TEMP, M_NOWAIT)))
            return NULL;
	bzero(acd->stats, sizeof(struct devstat));
    }
    else
	acd->stats = stats;
#ifdef DEVFS
    acd->a_cdevfs_token = devfs_add_devswf(&acd_cdevsw, dkmakeminor(lun, 0, 0),
        				   DV_CHR, UID_ROOT, GID_OPERATOR, 0644,
        				   "racd%da", lun);
    acd->c_cdevfs_token = devfs_add_devswf(&acd_cdevsw, 
					   dkmakeminor(lun, 0, RAW_PART),
        				   DV_CHR, UID_ROOT, GID_OPERATOR, 0644,
        				   "racd%dc", lun);
    acd->a_bdevfs_token = devfs_add_devswf(&acd_cdevsw, dkmakeminor(lun, 0, 0),
        				   DV_BLK, UID_ROOT, GID_OPERATOR, 0644,
        				   "acd%da", lun);
    acd->c_bdevfs_token = devfs_add_devswf(&acd_cdevsw, 
					   dkmakeminor(lun, 0, RAW_PART),
        				   DV_BLK, UID_ROOT, GID_OPERATOR, 0644,
        				   "acd%dc", lun);
#endif
    return acd;
}

static void 
acd_describe(struct acd_softc *cdp)
{
    int32_t comma;
    int8_t *mechanism;
    int8_t model_buf[40+1];
    int8_t revision_buf[8+1];

    bpack(cdp->atp->atapi_parm->model, model_buf, sizeof(model_buf));
    bpack(cdp->atp->atapi_parm->revision, revision_buf, sizeof(revision_buf));
    printf("acd%d: <%s/%s> CDROM drive at ata%d as %s\n",
           cdp->lun, model_buf, revision_buf,
           cdp->atp->controller->lun,
           (cdp->atp->unit == ATA_MASTER) ? "master" : "slave ");

    printf("acd%d: drive speed ", cdp->lun);
    if (cdp->cap.cur_speed != cdp->cap.max_speed)
        printf("%d - ", cdp->cap.cur_speed * 1000 / 1024);
    printf("%dKB/sec", cdp->cap.max_speed * 1000 / 1024);
    if (cdp->cap.buf_size)
        printf(", %dKB cache", cdp->cap.buf_size);
    if (cdp->atp->flags & ATAPI_F_DMA_ENABLED)
	printf(", DMA");
    printf("\n");

    printf("acd%d: supported read types:", cdp->lun);
    comma = 0;
    if (cdp->cap.read_cdr) {
        printf(" CD-R"); comma = 1;
    }
    if (cdp->cap.read_cdrw) {
        printf("%s CD-RW", comma ? "," : ""); comma = 1;
    }
    if (cdp->cap.cd_da) {
        printf("%s CD-DA", comma ? "," : ""); comma = 1;
    }
    if (cdp->cap.method2)
        printf("%s packet track", comma ? "," : "");
    if (cdp->cap.write_cdr || cdp->cap.write_cdrw) {
    	printf("\nacd%d: supported write types:", cdp->lun);
        comma = 0;
    	if (cdp->cap.write_cdr) {
            printf(" CD-R" ); comma = 1;
	}
    	if (cdp->cap.write_cdrw) {
            printf("%s CD-RW", comma ? "," : ""); comma = 1;
	}
    	if (cdp->cap.test_write) {
            printf("%s test write", comma ? "," : ""); comma = 1;
	}
    }
    if (cdp->cap.audio_play) {
    	printf("\nacd%d: Audio: ", cdp->lun);
    	if (cdp->cap.audio_play)
            printf("play");
    	if (cdp->cap.max_vol_levels)
            printf(", %d volume levels", cdp->cap.max_vol_levels);
    }
    printf("\nacd%d: Mechanism: ", cdp->lun);
    switch (cdp->cap.mech) {
    case MST_MECH_CADDY:
        mechanism = "caddy"; break;
    case MST_MECH_TRAY:
        mechanism = "tray"; break;
    case MST_MECH_POPUP:
        mechanism = "popup"; break;
    case MST_MECH_CHANGER:
        mechanism = "changer"; break;
    case MST_MECH_CARTRIDGE:
        mechanism = "cartridge"; break;
    default:
        mechanism = 0; break;
    }
    if (mechanism)
        printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
    else if (cdp->cap.eject)
        printf("ejectable");

    if (cdp->cap.mech != MST_MECH_CHANGER) {
        printf("\nacd%d: Medium: ", cdp->lun);
        switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
        case MST_CDROM:
            printf("CD-ROM "); break;
        case MST_CDR:
            printf("CD-R "); break;
        case MST_CDRW:
            printf("CD-RW "); break;
        case MST_DOOR_OPEN:
            printf("door open"); break;
        case MST_NO_DISC:
            printf("no/blank disc inside"); break;
        case MST_FMT_ERROR:
            printf("medium format error"); break;
	}
        if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH) < MST_TYPE_MASK_HIGH) {
            switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
            case MST_DATA_120:
                printf("120mm data disc loaded"); break;
            case MST_AUDIO_120:
                printf("120mm audio disc loaded"); break;
            case MST_COMB_120:
                printf("120mm data/audio disc loaded"); break;
            case MST_PHOTO_120:
                printf("120mm photo disc loaded"); break;
            case MST_DATA_80:
                printf("80mm data disc loaded"); break;
            case MST_AUDIO_80:
                printf("80mm audio disc loaded"); break;
            case MST_COMB_80:
                printf("80mm data/audio disc loaded"); break;
            case MST_PHOTO_80:
                printf("80mm photo disc loaded"); break;
            case MST_FMT_NONE:
                switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
	        case MST_CDROM:
		    printf("unknown medium"); break;
                case MST_CDR:
                case MST_CDRW:
                    printf("blank medium"); break;
	        }
                break;
            default:
                printf("unknown type=0x%x", cdp->cap.medium_type); break;
            }
	}
    }
    if (cdp->cap.lock)
        printf(cdp->cap.locked ? ", locked" : ", unlocked");
    if (cdp->cap.prevent)
        printf(", lock protected");
    printf("\n");
}

static __inline void 
lba2msf(int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
{
    lba += 150;
    lba &= 0xffffff;
    *m = lba / (60 * 75);
    lba %= (60 * 75);
    *s = lba / 75;
    *f = lba % 75;
}

static __inline int32_t 
msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
{
    return (m * 60 + s) * 75 + f - 150;
}

static int
acdopen(dev_t dev, int32_t flags, int32_t fmt, struct proc *p)
{
    int32_t lun = dkunit(dev);
    struct acd_softc *cdp;

    if (lun >= acdnlun || !(cdp = acdtab[lun]))
        return ENXIO;

    if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) {
	acd_lock_device(cdp, 1); 	/* Prevent user eject */
        cdp->flags |= F_LOCKED;
    }
    if (fmt == S_IFBLK)
        cdp->flags |= F_BOPEN;
    else
        cdp->refcnt++;

    if (!(flags & O_NONBLOCK)) {
        if (acd_read_toc(cdp)) {
            if (!(flags & FWRITE)) {
                printf("acd%d: read_toc failed\n", lun);
                return EIO;
	    }
        }
    }
    return 0;
}

static int 
acdclose(dev_t dev, int32_t flags, int32_t fmt, struct proc *p)
{
    int32_t lun = dkunit(dev);
    struct acd_softc *cdp;
    
    if (lun >= acdnlun || !(cdp = acdtab[lun]))
        return ENXIO;

    if (fmt == S_IFBLK)
    	cdp->flags &= ~F_BOPEN;
    else
    	cdp->refcnt--;

    /* Are we the last open ?? */
    if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) {
	/* Yup, do we need to close any written tracks */
        if ((flags & FWRITE) != 0) {
            if ((cdp->flags & F_TRACK_PREPED) != 0) {
                acd_close_track(cdp);
                cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP);
            }
        }
	acd_lock_device(cdp, 0);	/* Allow the user eject */
    }
    cdp->flags &= ~F_LOCKED;
    return 0;
}

static int 
acdioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
{
    int32_t lun = dkunit(dev);
    struct acd_softc *cdp = acdtab[lun];
    int32_t error = 0;

    if (cdp->flags & F_MEDIA_CHANGED)
        switch (cmd) {
        case CDIOCRESET:
            break;
        default:
            acd_read_toc(cdp);
	    acd_lock_device(cdp, 1);
            cdp->flags |= F_LOCKED;
            break;
        }
    switch (cmd) {

    case CDIOCRESUME:
        return acd_pause_device(cdp, 1);

    case CDIOCPAUSE:
        return acd_pause_device(cdp, 0);

    case CDIOCSTART:
        return acd_start_device(cdp, 1);

    case CDIOCSTOP:
        return acd_start_device(cdp, 0);

    case CDIOCALLOW:
        acd_select_slot(cdp);
        cdp->flags &= ~F_LOCKED;
	return acd_lock_device(cdp, 0);

    case CDIOCPREVENT:
        acd_select_slot(cdp);
        cdp->flags |= F_LOCKED;
	return acd_lock_device(cdp, 1);

    case CDIOCRESET:
        error = suser(p);
        if (error)
            return error;
        return acd_test_unit_ready(cdp);

    case CDIOCEJECT:
        if ((cdp->flags & F_BOPEN) && cdp->refcnt)
            return EBUSY;
        return acd_eject(cdp, 0);

    case CDIOCCLOSE:
        if ((cdp->flags & F_BOPEN) && cdp->refcnt)
            return 0;
        return acd_eject(cdp, 1);

    case CDIOREADTOCHEADER:
        if (!cdp->toc.hdr.ending_track)
            return EIO;
        bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
        break;

    case CDIOREADTOCENTRYS:
	{
            struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
            struct toc *toc = &cdp->toc;
            struct toc buf;
            u_int32_t len;
            u_int8_t starting_track = te->starting_track;

            if (!cdp->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)
                starting_track = toc->hdr.starting_track;
            else if (starting_track == 170) 
                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;

            if (te->address_format == CD_MSF_FORMAT) {
                struct cd_toc_entry *entry;

                buf = cdp->toc;
                toc = &buf;
                entry = toc->tab + (toc->hdr.ending_track + 1 -
                    	toc->hdr.starting_track) + 1;
                while (--entry >= toc->tab)
                    lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
                            &entry->addr.msf.second, &entry->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 = &cdp->toc;
            struct toc buf;
            u_int8_t track = te->track;

            if (!cdp->toc.hdr.ending_track)
                return EIO;

            if (te->address_format != CD_MSF_FORMAT && 
		te->address_format != CD_LBA_FORMAT)
                return EINVAL;

            if (!track)
                track = toc->hdr.starting_track;
            else if (track == 170)
                track = toc->hdr.ending_track + 1;
            else if (track < toc->hdr.starting_track ||
                     track > toc->hdr.ending_track + 1)
                return EINVAL;

            if (te->address_format == CD_MSF_FORMAT) {
                struct cd_toc_entry *entry;

                buf = cdp->toc;
                toc = &buf;
                entry = toc->tab + (track - toc->hdr.starting_track);
                lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
                        &entry->addr.msf.second, &entry->addr.msf.frame);
            }
            bcopy(toc->tab + track - toc->hdr.starting_track,
                  &te->entry, sizeof(struct cd_toc_entry));
        }
	break;

    case CDIOCREADSUBCHANNEL:
	{
            struct ioc_read_subchannel *args =
            	(struct ioc_read_subchannel *)addr;
            struct cd_sub_channel_info data;
            u_int32_t len = args->data_len;
            int32_t abslba, rellba;
	    int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
			       sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
			       0, 0, 0, 0, 0, 0, 0 };

            if (len > sizeof(data) ||
                len < sizeof(struct cd_sub_channel_header))
                return EINVAL;

	    if (atapi_queue_cmd(cdp->atp, ccb, &cdp->subchan, 
				sizeof(cdp->subchan), A_READ, NULL, NULL, NULL))
                return EIO;
#ifdef ACD_DEBUG
            atapi_dump("acd: subchan", &cdp->subchan, sizeof(cdp->subchan));
#endif

            abslba = cdp->subchan.abslba;
            rellba = cdp->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 = cdp->subchan.audio_status;
            data.what.position.control = cdp->subchan.control & 0xf;
            data.what.position.addr_type = cdp->subchan.control >> 4;
            data.what.position.track_number = cdp->subchan.track;
            data.what.position.index_number = cdp->subchan.indx;
            return copyout(&data, args->data, len);
        }

    case CDIOCPLAYMSF:
	{
            struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
            int8_t ccb[16] = { 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, 0, 0, 0, 0 };

            return atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
        }

    case CDIOCPLAYBLOCKS:
	{
            struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
	    int8_t ccb[16]  = { ATAPI_PLAY_BIG, 0,
			        args->blk>>24, args->blk>>16, args->blk>>8,
				args->blk, args->len>>24, args->len>>16,
				args->len>>8, args->len,
			   	0, 0, 0, 0, 0, 0 };

            return atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
        }

    case CDIOCPLAYTRACKS:
	{
            struct ioc_play_track *args = (struct ioc_play_track *)addr;
            u_int32_t start, len;
            int32_t t1, t2;
	    int8_t ccb[16];

            if (!cdp->toc.hdr.ending_track)
                return EIO;

            if (args->end_track < cdp->toc.hdr.ending_track + 1)
                ++args->end_track;
            if (args->end_track > cdp->toc.hdr.ending_track + 1)
                args->end_track = cdp->toc.hdr.ending_track + 1;
            t1 = args->start_track - cdp->toc.hdr.starting_track;
            t2 = args->end_track - cdp->toc.hdr.starting_track;
            if (t1 < 0 || t2 < 0)
                return EINVAL;
            start = ntohl(cdp->toc.tab[t1].addr.lba);
            len = ntohl(cdp->toc.tab[t2].addr.lba) - start;

	    bzero(ccb, sizeof(ccb));
	    ccb[0] = ATAPI_PLAY_BIG;
	    ccb[2] = start>>24;
	    ccb[3] = start>>16;
	    ccb[4] = start>>8;
	    ccb[5] = start;
	    ccb[6] = len>>24;
	    ccb[7] = len>>16;
	    ccb[8] = len>>8;
	    ccb[9] = len;

            return atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
        }

    case CDIOCREADAUDIO:
	{
	    struct ioc_read_audio *args = (struct ioc_read_audio *)addr;
	    int32_t lba, frames, error = 0;
	    u_int8_t *buffer, *ubuf = args->buffer;
	    int8_t ccb[16];

	    if (!cdp->toc.hdr.ending_track)
		return EIO;
		
	    if ((frames = args->nframes) < 0)
		return EINVAL;

	    if (args->address_format == CD_LBA_FORMAT)
		lba = args->address.lba;
	    else if (args->address_format == CD_MSF_FORMAT)
	        lba = msf2lba(args->address.msf.minute,
			     args->address.msf.second,
			     args->address.msf.frame);
	    else
		return EINVAL;
#ifndef CD_BUFFER_BLOCKS
#define CD_BUFFER_BLOCKS 13
#endif
            if (!(buffer = malloc(CD_BUFFER_BLOCKS * 2352,
				  M_TEMP,M_NOWAIT)))
                return ENOMEM;
	    bzero(ccb, sizeof(ccb));
            while (frames > 0) {
                int32_t size;
                u_int8_t blocks;

                blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
                size = blocks * 2352;

		ccb[0] = ATAPI_READ_CD;
		ccb[1] = 4;
		ccb[2] = lba>>24;
		ccb[3] = lba>>16;
		ccb[4] = lba>>8;
		ccb[5] = lba;
		ccb[8] = blocks;
		ccb[9] = 0xf0;
		if ((error = atapi_queue_cmd(cdp->atp, ccb,  buffer, size,
					     A_READ, NULL, NULL, NULL)))
                    break;

                if ((error = copyout(buffer, ubuf, size)))
                    break;
                    
                ubuf += size;
                frames -= blocks;
                lba += blocks;
            }
            free(buffer, M_TEMP);
	    if (args->address_format == CD_LBA_FORMAT)
		args->address.lba = lba;
	    else if (args->address_format == CD_MSF_FORMAT)
	        lba2msf(lba, &args->address.msf.minute,
			     &args->address.msf.second,
			     &args->address.msf.frame);
            return error;
        }

    case CDIOCGETVOL:
	{
            struct ioc_vol *arg = (struct ioc_vol *)addr;

	    if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
				        &cdp->au, sizeof(cdp->au))))
                return error;
            if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
                return EIO;
            arg->vol[0] = cdp->au.port[0].volume;
            arg->vol[1] = cdp->au.port[1].volume;
            arg->vol[2] = cdp->au.port[2].volume;
            arg->vol[3] = cdp->au.port[3].volume;
        }
        break;

    case CDIOCSETVOL:
	{
            struct ioc_vol *arg = (struct ioc_vol *)addr;

	    if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
				        &cdp->au, sizeof(cdp->au))))
                return error;
            if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
                return EIO;
	    if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE_MASK,
				        &cdp->aumask, sizeof(cdp->aumask))))
                return error;
            cdp->au.data_length = 0;
            cdp->au.port[0].channels = CHANNEL_0;
            cdp->au.port[1].channels = CHANNEL_1;
            cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
            cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
            cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
            cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
	    return acd_mode_select(cdp, &cdp->au, sizeof(cdp->au));
        }
    case CDIOCSETPATCH:
	{
            struct ioc_patch *arg = (struct ioc_patch *)addr;

            return acd_setchan(cdp, arg->patch[0], arg->patch[1],
                	       arg->patch[2], arg->patch[3]);
        }

    case CDIOCSETMONO:
        return acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0, 0);

    case CDIOCSETSTEREO:
        return acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);

    case CDIOCSETMUTE:
        return acd_setchan(cdp, 0, 0, 0, 0);

    case CDIOCSETLEFT:
        return acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);

    case CDIOCSETRIGHT:
        return acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);

    case CDRIOCNEXTWRITEABLEADDR:
	{
	    struct acd_track_info track_info;

	    if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
		break;
	    if (!track_info.nwa_valid)
		return EINVAL;
	    cdp->next_writeable_lba = track_info.next_writeable_addr;
	    *(int*)addr = track_info.next_writeable_addr;
	}
	break;
 
    case WORMIOCPREPDISK:
        {
            struct wormio_prepare_disk *w = (struct wormio_prepare_disk *)addr;

            if (w->dummy != 0 && w->dummy != 1)
                error = EINVAL;
            else {
                error = acd_open_disk(cdp, w->dummy);
                if (error == 0) {
                    cdp->flags |= F_DISK_PREPED;
                    cdp->dummy = w->dummy;
                    cdp->speed = w->speed;
                }
            }
        }
        break;

    case WORMIOCPREPTRACK:
        {
            struct wormio_prepare_track *w =(struct wormio_prepare_track *)addr;

            if (w->audio != 0 && w->audio != 1)
                error = EINVAL;
            else if (w->audio == 0 && w->preemp)
                error = EINVAL;
            else if ((cdp->flags & F_DISK_PREPED) == 0) {
                error = EINVAL;
                printf("acd%d: sequence error (PREP_TRACK)\n", cdp->lun);
            } else {
                cdp->flags |= F_TRACK_PREP;
                cdp->preptrack = *w;
            }
        }
        break;

    case WORMIOCFINISHTRACK:
        if ((cdp->flags & F_TRACK_PREPED) != 0)
            error = acd_close_track(cdp);
        cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP);
        break;

    case WORMIOCFIXATION:
        {
            struct wormio_fixation *w =
            (struct wormio_fixation *)addr;

            if ((cdp->flags & F_WRITTEN) == 0)
                error = EINVAL;
            else if (w->toc_type < 0 /* WORM_TOC_TYPE_AUDIO */ ||
                w->toc_type > 4 /* WORM_TOC_TYPE_CDI */ )
                error = EINVAL;
            else if (w->onp != 0 && w->onp != 1)
                error = EINVAL;
            else {
                /* no fixation needed if dummy write */
                if (cdp->dummy == 0)
                    error = acd_close_disk(cdp);
                cdp->flags &=
                    ~(F_WRITTEN|F_DISK_PREPED|F_TRACK_PREP|F_TRACK_PREPED);
            }
        }
        break;

    case CDRIOCBLANK:
        return acd_blank_disk(cdp);

    default:
        return ENOTTY;
    }
    return error;
}

static void 
acdstrategy(struct buf *bp)
{
    int32_t lun = dkunit(bp->b_dev);
    struct acd_softc *cdp = acdtab[lun];
    int32_t x;

#ifdef NOTYET
    /* allow write only on CD-R/RW media */   /* all for now SOS */
    if (!(bp->b_flags & B_READ) && !(writeable_media)) {
        bp->b_error = EROFS;
        bp->b_flags |= B_ERROR;
        biodone(bp);
        return;
    }
#endif

    if (bp->b_bcount == 0) {
        bp->b_resid = 0;
        biodone(bp);
        return;
    }
    
    /* check for valid blocksize SOS */

    bp->b_pblkno = bp->b_blkno;
    bp->b_resid = bp->b_bcount;

    x = splbio();
    bufqdisksort(&cdp->buf_queue, bp);
    acd_start(cdp);
    splx(x);
}

static void 
acd_start(struct acd_softc *cdp)
{
    struct buf *bp = bufq_first(&cdp->buf_queue);
    u_int32_t lba, count;
    int8_t ccb[16];

    if (!bp)
        return;

    bufq_remove(&cdp->buf_queue, bp);

    /* Should reject all queued entries if media have changed. */
    if (cdp->flags & F_MEDIA_CHANGED) {
        bp->b_error = EIO;
        bp->b_flags |= B_ERROR;
        biodone(bp);
        return;
    }
    acd_select_slot(cdp);
    if ((bp->b_flags & B_READ) == B_WRITE) {
        if ((cdp->flags & F_TRACK_PREPED) == 0) {
            if ((cdp->flags & F_TRACK_PREP) == 0) {
                printf("acd%d: sequence error\n", cdp->lun);
                bp->b_error = EIO;
                bp->b_flags |= B_ERROR;
                biodone(bp);
                return;
            } else {
                if (acd_open_track(cdp, &cdp->preptrack) != 0) {
                    biodone(bp);
                    return;
                }
                cdp->flags |= F_TRACK_PREPED;
            }
        }
    }
    bzero(ccb, sizeof(ccb));
    if (bp->b_flags & B_READ) {
    	lba = bp->b_blkno / (cdp->block_size / DEV_BSIZE);
	ccb[0] = ATAPI_READ_BIG;
    }
    else {
	lba = cdp->next_writeable_lba + (bp->b_offset / cdp->block_size);
	ccb[0] = ATAPI_WRITE_BIG;
    }
    count = (bp->b_bcount + (cdp->block_size - 1)) / cdp->block_size;

#ifdef ACD_DEBUG
    printf("acd%d: lba=%d, count=%d\n", cdp->lun, lba, count);
#endif
    ccb[1] = 0;
    ccb[2] = lba>>24;
    ccb[3] = lba>>16;
    ccb[4] = lba>>8;
    ccb[5] = lba;
    ccb[7] = count>>8;
    ccb[8] = count;

    devstat_start_transaction(cdp->stats);

    atapi_queue_cmd(cdp->atp, ccb, bp->b_data, bp->b_bcount,
                    (bp->b_flags&B_READ)?A_READ : 0, acd_done, cdp, bp);
}

static void 
acd_done(struct atapi_request *request)
{
    struct buf *bp = request->bp;
    struct acd_softc *cdp = request->driver;
    
    devstat_end_transaction(cdp->stats, request->donecount,
                            DEVSTAT_TAG_NONE,
                            (bp->b_flags&B_READ) ? DEVSTAT_READ:DEVSTAT_WRITE);  
    if (request->result) {
        atapi_error(request->device, request->result);
        bp->b_error = EIO;
        bp->b_flags |= B_ERROR;
    }   
    else {
        bp->b_resid = request->bytecount;
        if ((bp->b_flags & B_READ) == B_WRITE)
            cdp->flags |= F_WRITTEN;
    }
    biodone(bp);
    acd_start(cdp);
}

static int32_t
acd_test_unit_ready(struct acd_softc *cdp)
{
    int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    return atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
}

static int32_t
acd_lock_device(struct acd_softc *cdp, int32_t lock)
{
    int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    return atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
}

static int32_t
acd_start_device(struct acd_softc *cdp, int32_t start)
{
    int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    return atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
}

static int32_t
acd_pause_device(struct acd_softc *cdp, int32_t pause)
{
    int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, 0, 0, 0, 0, pause,
		       0, 0, 0, 0, 0, 0, 0 };

    return atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
}

static int32_t
acd_mode_sense(struct acd_softc *cdp, u_int8_t page,
	       void *pagebuf, int32_t pagesize)
{
    int32_t error;
    int8_t ccb[16] = { ATAPI_MODE_SENSE, 0, page, 0, 0, 0, 0,
                     pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };

    error = atapi_queue_cmd(cdp->atp, ccb, pagebuf, pagesize, A_READ,
			    NULL, NULL, NULL);
#ifdef ACD_DEBUG
    atapi_dump("acd: mode sense ", pagebuf, pagesize);
#endif
    return error;
}

static int32_t
acd_mode_select(struct acd_softc *cdp, void *pagebuf, int32_t pagesize)
{
    int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, 0, 0, 0, 0,
                     pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };

#ifdef ACD_DEBUG
    printf("acd: modeselect pagesize=%d\n", pagesize);
    atapi_dump("acd: mode select ", pagebuf, pagesize);
#endif
    return atapi_queue_cmd(cdp->atp, ccb, pagebuf, pagesize, 0, 
			   NULL, NULL, NULL);
}

static int32_t 
acd_read_toc(struct acd_softc *cdp)
{
    int32_t error, ntracks, len;
    int8_t ccb[16];

    bzero(&cdp->toc, sizeof(cdp->toc));
    bzero(&cdp->info, sizeof(cdp->info));
    bzero(ccb, sizeof(ccb));

    acd_select_slot(cdp);

    error = acd_test_unit_ready(cdp);
    if ((error & ATAPI_SK_MASK) == ATAPI_SK_UNIT_ATTENTION) {
        cdp->flags |= F_MEDIA_CHANGED;
    	cdp->flags &= ~(F_WRITTEN | F_TRACK_PREP | F_TRACK_PREPED);
        error = acd_test_unit_ready(cdp);
    }

    if (error) {
        atapi_error(cdp->atp, error);
        return EIO;
    }

    cdp->flags &= ~F_MEDIA_CHANGED;

    len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
    ccb[0] = ATAPI_READ_TOC;
    ccb[7] = len>>8;
    ccb[8] = len;
    if (atapi_queue_cmd(cdp->atp, ccb, &cdp->toc, len, A_READ, NULL,NULL,NULL)){
        bzero(&cdp->toc, sizeof(cdp->toc));
        return 0;
    }
    ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
    if (ntracks <= 0 || ntracks > MAXTRK) {
        bzero(&cdp->toc, sizeof(cdp->toc));
        return 0;
    }

    len = sizeof(struct ioc_toc_header) + ntracks * sizeof(struct cd_toc_entry);
    bzero(ccb, sizeof(ccb));
    ccb[0] = ATAPI_READ_TOC;
    ccb[7] = len>>8;
    ccb[8] = len;
    if (atapi_queue_cmd(cdp->atp, ccb, &cdp->toc, len, A_READ, NULL,NULL,NULL)){
        bzero(&cdp->toc, sizeof(cdp->toc));
        return 0;
    }

    cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);

    bzero(ccb, sizeof(ccb));
    ccb[0] = ATAPI_READ_CAPACITY;
    if (atapi_queue_cmd(cdp->atp, ccb, &cdp->info, sizeof(cdp->info), 
			A_READ, NULL, NULL, NULL))
        bzero(&cdp->info, sizeof(cdp->info));

    cdp->toc.tab[ntracks].control = cdp->toc.tab[ntracks - 1].control;
    cdp->toc.tab[ntracks].addr_type = cdp->toc.tab[ntracks - 1].addr_type;
    cdp->toc.tab[ntracks].track = 170;
    cdp->toc.tab[ntracks].addr.lba = cdp->info.volsize;

    cdp->info.volsize = ntohl(cdp->info.volsize);
    cdp->info.blksize = ntohl(cdp->info.blksize);

#ifdef ACD_DEBUG
    if (cdp->info.volsize && cdp->toc.hdr.ending_track) {
        printf("acd%d: ", cdp->lun);
        if (cdp->toc.tab[0].control & 4)
            printf("%dMB ", cdp->info.volsize / 512);
        else
            printf("%d:%d audio ", cdp->info.volsize / 75 / 60,
                cdp->info.volsize / 75 % 60);
        printf("(%d sectors (%d bytes)), %d tracks\n", 
	    cdp->info.volsize, cdp->info.blksize,
            cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
    }
#endif
    return 0;
}

static int32_t 
acd_setchan(struct acd_softc *cdp,
	    u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
{
    int32_t error;

    if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, &cdp->au, 
				sizeof(cdp->au))))
        return error;
    if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
        return EIO;
    cdp->au.data_length = 0;
    cdp->au.port[0].channels = c0;
    cdp->au.port[1].channels = c1;
    cdp->au.port[2].channels = c2;
    cdp->au.port[3].channels = c3;
    return acd_mode_select(cdp, &cdp->au, sizeof(cdp->au));
}

static int32_t 
acd_eject(struct acd_softc *cdp, int32_t close)
{
    int32_t error;

    acd_select_slot(cdp);

    error = acd_start_device(cdp, 0);

    if ((error & ATAPI_SK_MASK) &&
        ((error & ATAPI_SK_MASK) == ATAPI_SK_NOT_READY ||
         (error & ATAPI_SK_MASK) == ATAPI_SK_UNIT_ATTENTION)) {

        if (!close)
            return 0;
	if ((error = acd_start_device(cdp, 3)))
	    return error;
        acd_read_toc(cdp);
	acd_lock_device(cdp, 1);
        cdp->flags |= F_LOCKED;
        return 0;
    }
    if (error) {
        atapi_error(cdp->atp, error);
        return EIO;
    }
    if (close)
        return 0;

    tsleep((caddr_t) &lbolt, PRIBIO, "acdej1", 0);
    tsleep((caddr_t) &lbolt, PRIBIO, "acdej2", 0);
    acd_lock_device(cdp, 0);
    cdp->flags &= ~F_LOCKED;
    cdp->flags |= F_MEDIA_CHANGED;
    cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
    return acd_start_device(cdp, 2);
}

static void
acd_select_slot(struct acd_softc *cdp)
{
    int8_t ccb[16];

    if (cdp->slot < 0 || cdp->changer_info->current_slot == cdp->slot)
        return;

    /* Unlock (might not be needed but its cheaper than asking) */
    acd_lock_device(cdp, 0);

    bzero(ccb, sizeof(ccb));
    /* Unload the current media from player */
    ccb[0] = ATAPI_LOAD_UNLOAD;
    ccb[4] = 2;
    ccb[8] = cdp->changer_info->current_slot;
    atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);

    /* load the wanted slot */
    ccb[0] = ATAPI_LOAD_UNLOAD;
    ccb[4] = 3;
    ccb[8] = cdp->slot;
    atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);

    cdp->changer_info->current_slot = cdp->slot;

    /* Lock the media if needed */
    if (cdp->flags & F_LOCKED)
        acd_lock_device(cdp, 1);
}

static int32_t
acd_open_disk(struct acd_softc *cdp, int32_t test)
{
    cdp->next_writeable_lba = 0;
    return 0;
}

static int32_t
acd_close_disk(struct acd_softc *cdp)
{
    int8_t ccb[16];

    bzero(ccb, sizeof(ccb));
    ccb[0] = ATAPI_CLOSE_TRACK;
    ccb[2] = 2;
    ccb[5] = 0; /* track to close (0 = last open) */
    return atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
}

static int32_t
acd_open_track(struct acd_softc *cdp, struct wormio_prepare_track *ptp)
{
    struct write_param param;
    int32_t error;

    if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
				&param, sizeof(param))))
        return error;
    param.page_code = 0x05;
    param.page_length = 0x32;
    param.test_write = cdp->dummy ? 1 : 0;
    param.write_type = CDR_WTYPE_TRACK;

    switch (ptp->audio) {
/*    switch (data_type) { */

    case 0:
/*    case CDR_DATA: */
	cdp->block_size = 2048;
    	param.track_mode = CDR_TMODE_DATA;
    	param.data_block_type = CDR_DB_ROM_MODE1;
    	param.session_format = CDR_SESS_CDROM;
	break;

    default:
/*    case CDR_AUDIO: */
	cdp->block_size = 2352;
	if (ptp->preemp)
    	    param.track_mode = CDR_TMODE_AUDIO;
	else
    	    param.track_mode = 0;
    	param.data_block_type = CDR_DB_RAW;
    	param.session_format = CDR_SESS_CDROM;
	break;

/*
    case CDR_MODE2:
    	param.track_mode = CDR_TMODE_DATA;
    	param.data_block_type = CDR_DB_ROM_MODE2;
    	param.session_format = CDR_SESS_CDROM;
	break;

    case CDR_XA1:
    	param.track_mode = CDR_TMODE_DATA;
    	param.data_block_type = CDR_DB_XA_MODE1;
    	param.session_format = CDR_SESS_CDROM_XA;
	break;

    case CDR_XA2:
    	param.track_mode = CDR_TMODE_DATA;
    	param.data_block_type = CDR_DB_XA_MODE2_F1;
    	param.session_format = CDR_SESS_CDROM_XA;
	break;

    case CDR_CDI:
    	param.track_mode = CDR_TMODE_DATA;
    	param.data_block_type = CDR_DB_XA_MODE2_F1;
    	param.session_format = CDR_SESS_CDI;
	break;
    }
*/
    }

    param.multi_session = CDR_MSES_NONE;
    param.fp = 0;
    param.packet_size = 0;
    return acd_mode_select(cdp, &param, sizeof(param));
}

static int32_t
acd_close_track(struct acd_softc *cdp)
{
    int8_t ccb[16];

    bzero(ccb, sizeof(ccb));
    ccb[0] = ATAPI_SYNCHRONIZE_CACHE;
    return atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
}

static int32_t
acd_read_track_info(struct acd_softc *cdp,
		    int32_t lba, struct acd_track_info *info)
{
    int32_t error;
    int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
		     lba>>24, lba>>16, lba>>8, lba,
		     0,
                     sizeof(*info)>>8, sizeof(*info),
                     0, 0, 0, 0, 0, 0, 0 };

    if ((error = atapi_queue_cmd(cdp->atp, ccb, info, sizeof(*info), 
			         A_READ, NULL, NULL, NULL)))
	return error;
    info->track_start_addr = ntohl(info->track_start_addr);
    info->next_writeable_addr = ntohl(info->next_writeable_addr);
    info->free_blocks = ntohl(info->free_blocks);
    info->fixed_packet_size = ntohl(info->fixed_packet_size);
    info->track_length = ntohl(info->track_length);
    return 0;
}

static int32_t
acd_blank_disk(struct acd_softc *cdp)
{
    int32_t error;
    int8_t ccb[16];

    bzero(ccb, sizeof(ccb));
    ccb[0] = ATAPI_BLANK;
    ccb[1] = 1;
    error = atapi_queue_cmd(cdp->atp, ccb, NULL, 0, 0, NULL, NULL, NULL);
    cdp->flags |= F_MEDIA_CHANGED;
    cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
    return error;
}

static void 
acd_drvinit(void *unused)
{
    static int32_t acd_devsw_installed = 0;

    if (!acd_devsw_installed) {
        cdevsw_add(&acd_cdevsw);
        acd_devsw_installed = 1;
    }
}
SYSINIT(acddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, acd_drvinit, NULL)
#endif /* NATA && NATAPICD */
OpenPOWER on IntegriCloud