FreeBSD/Linux Kernel Cross Reference
sys/pc98/pc98/wd_cd.c
1 /*-
2 * Copyright (c) 1998, 1999 Sen Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: releng/5.0/sys/pc98/pc98/wd_cd.c 104516 2002-10-05 15:49:39Z phk $
29 */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/bio.h>
36 #include <sys/disklabel.h>
37 #include <sys/devicestat.h>
38 #include <sys/cdio.h>
39 #include <sys/fcntl.h>
40 #include <sys/conf.h>
41 #include <sys/stat.h>
42 #include <pc98/pc98/atapi.h>
43 #include <pc98/pc98/wormio.h>
44 #include <pc98/pc98/wd_cd.h>
45
46 static d_open_t acdopen;
47 static d_close_t acdclose;
48 static d_ioctl_t acdioctl;
49 static d_strategy_t acdstrategy;
50
51 #define CDEV_MAJOR 69
52
53 static struct cdevsw acd_cdevsw = {
54 /* open */ acdopen,
55 /* close */ acdclose,
56 /* read */ physread,
57 /* write */ physwrite,
58 /* ioctl */ acdioctl,
59 /* poll */ nopoll,
60 /* mmap */ nommap,
61 /* strategy */ acdstrategy,
62 /* name */ "wcd",
63 /* maj */ CDEV_MAJOR,
64 /* dump */ nodump,
65 /* psize */ nopsize,
66 /* flags */ D_DISK,
67 };
68
69 #define NUNIT 16 /* Max # of devices */
70
71 #define F_BOPEN 0x0001 /* The block device is opened */
72 #define F_MEDIA_CHANGED 0x0002 /* The media have changed since open */
73 #define F_DEBUG 0x0004 /* Print debug info */
74 #define F_LOCKED 0x0008 /* This unit is locked (or should be) */
75 #define F_TRACK_PREP 0x0010 /* Track should be prep'ed */
76 #define F_TRACK_PREPED 0x0020 /* Track has been prep'ed */
77 #define F_DISK_PREPED 0x0040 /* Disk has been prep'ed */
78 #define F_WRITTEN 0x0080 /* The medium has been written to */
79
80 static struct acd *acdtab[NUNIT];
81 static int acdnlun = 0; /* Number of configured drives */
82
83 int acdattach(struct atapi *, int, struct atapi_params *, int);
84 static struct acd *acd_init_lun(struct atapi *, int, struct atapi_params *, int,
85 struct devstat *);
86 static void acd_start(struct acd *);
87 static void acd_done(struct acd *, struct bio *, int, struct atapires);
88 static int acd_read_toc(struct acd *);
89 static int acd_request_wait(struct acd *, u_char, u_char, u_char, u_char, u_char, u_char, u_char, u_char, u_char, u_char, char *, int);
90 static void acd_describe(struct acd *);
91 static int acd_setchan(struct acd *, u_char, u_char, u_char, u_char);
92 static int acd_eject(struct acd *, int);
93 static void acd_select_slot(struct acd *);
94 static int acd_open_disk(struct acd *, int);
95 static int acd_open_track(struct acd *, struct wormio_prepare_track *);
96 static int acd_close_track(struct acd *);
97 static int acd_close_disk(struct acd *);
98 static int acd_read_track_info(struct acd *cdp, int lba, struct acd_track_info *info);
99 static int acd_blank_disk(struct acd *);
100 static void atapi_dump(int ctrlr, int lun, char *label, void *data, int len);
101 static void atapi_error(struct atapi *ata, int unit, struct atapires result);
102
103 struct acd *
104 acd_init_lun(struct atapi *ata, int unit, struct atapi_params *ap, int lun,
105 struct devstat *device_stats)
106 {
107 struct acd *ptr;
108 dev_t pdev;
109
110 if (!(ptr = malloc(sizeof(struct acd), M_TEMP, M_NOWAIT | M_ZERO)))
111 return NULL;
112 bioq_init(&ptr->bio_queue);
113 ptr->ata = ata;
114 ptr->unit = unit;
115 ptr->lun = lun;
116 ptr->param = ap;
117 ptr->flags = F_MEDIA_CHANGED;
118 ptr->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
119 ptr->block_size = 2048;
120 ptr->refcnt = 0;
121 ptr->slot = -1;
122 ptr->changer_info = NULL;
123 if (device_stats == NULL) {
124 if (!(ptr->device_stats = malloc(sizeof(struct devstat),
125 M_TEMP, M_NOWAIT | M_ZERO)))
126 return NULL;
127 }
128 else
129 ptr->device_stats = device_stats;
130
131 pdev = make_dev(&acd_cdevsw, dkmakeminor(lun, 0, 0),
132 UID_ROOT, GID_OPERATOR, 0640, "wcd%da", lun);
133 make_dev_alias(pdev, "rwcd%da", lun);
134 pdev->si_drv1 = ptr;
135
136 pdev = make_dev(&acd_cdevsw, dkmakeminor(lun, 0, RAW_PART),
137 UID_ROOT, GID_OPERATOR, 0640, "wcd%dc", lun);
138 make_dev_alias(pdev, "rwcd%dc", lun);
139 pdev->si_drv1 = ptr;
140
141 return ptr;
142 }
143
144 int
145 acdattach(struct atapi *ata, int unit, struct atapi_params *ap, int debug)
146 {
147 struct acd *cdp;
148 struct atapires result;
149 struct changer *chp;
150 int i, count;
151
152 if (acdnlun >= NUNIT) {
153 printf("wcd: too many units\n");
154 return 0;
155 }
156 if (!atapi_request_immediate) {
157 printf("wcd: configuration error, ATAPI code not present!\n");
158 return 0;
159 }
160 if ((cdp = acd_init_lun(ata, unit, ap, acdnlun, NULL)) == NULL) {
161 printf("wcd: out of memory\n");
162 return 0;
163 }
164 acdtab[acdnlun] = cdp;
165
166 if (debug) {
167 cdp->flags |= F_DEBUG;
168 atapi_dump(cdp->ata->ctrlr, cdp->lun, "info", ap, sizeof(*ap));
169 }
170
171 /* Get drive capabilities, some drives needs this repeated */
172 for (count = 0 ; count < 5 ; count++) {
173 result = atapi_request_immediate(ata, unit,
174 ATAPI_MODE_SENSE,
175 0, ATAPI_CDROM_CAP_PAGE,
176 0, 0, 0, 0,
177 sizeof(cdp->cap)>>8, sizeof(cdp->cap),
178 0, 0, 0, 0, 0, 0, 0,
179 (char *)&cdp->cap, sizeof(cdp->cap));
180 if (result.code == 0 || result.code == RES_UNDERRUN)
181 break;
182 }
183
184 /* Some drives have shorter capabilities page. */
185 if (result.code == RES_UNDERRUN)
186 result.code = 0;
187
188 if (result.code == 0) {
189 cdp->cap.max_speed = ntohs(cdp->cap.max_speed);
190 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
191 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
192 cdp->cap.cur_speed = ntohs(cdp->cap.cur_speed);
193 acd_describe(cdp);
194 if (cdp->flags & F_DEBUG)
195 atapi_dump(cdp->ata->ctrlr, cdp->lun, "cap", &cdp->cap,
196 sizeof(cdp->cap));
197 }
198 /* If this is a changer device, allocate the neeeded lun's */
199 if (cdp->cap.mech == MST_MECH_CHANGER) {
200 char string[16];
201 struct acd *tmpcdp = cdp;
202
203 chp = malloc(sizeof(struct changer), M_TEMP, M_NOWAIT | M_ZERO);
204 if (chp == NULL) {
205 printf("wcd: out of memory\n");
206 return 0;
207 }
208 result = atapi_request_immediate(ata, unit, ATAPI_MECH_STATUS,
209 0, 0, 0, 0, 0, 0, 0,
210 sizeof(struct changer)>>8,
211 sizeof(struct changer),
212 0, 0, 0, 0, 0, 0,
213 (char *)chp, sizeof(struct changer));
214 if (cdp->flags & F_DEBUG) {
215 printf("result.code=%d curr=%02x slots=%d len=%d\n",
216 result.code, chp->current_slot, chp->slots,
217 htons(chp->table_length));
218 }
219 if (result.code == RES_UNDERRUN)
220 result.code = 0;
221
222 if (result.code == 0) {
223 chp->table_length = htons(chp->table_length);
224 for (i = 0; i < chp->slots && acdnlun < NUNIT; i++) {
225 if (i > 0) {
226 tmpcdp = acd_init_lun(ata, unit, ap, acdnlun,
227 cdp->device_stats);
228 if (!tmpcdp) {
229 printf("wcd: out of memory\n");
230 return 0;
231 }
232 }
233 tmpcdp->slot = i;
234 tmpcdp->changer_info = chp;
235 printf("wcd%d: changer slot %d %s\n", acdnlun, i,
236 (chp->slot[i].present ? "disk present" : "no disk"));
237 acdtab[acdnlun++] = tmpcdp;
238 }
239 if (acdnlun >= NUNIT) {
240 printf("wcd: too many units\n");
241 return 0;
242 }
243 }
244 sprintf(string, "wcd%d-", cdp->lun);
245 devstat_add_entry(cdp->device_stats, string, tmpcdp->lun, DEV_BSIZE,
246 DEVSTAT_NO_ORDERED_TAGS,
247 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
248 DEVSTAT_PRIORITY_CD);
249 }
250 else {
251 acdnlun++;
252 devstat_add_entry(cdp->device_stats, "wcd", cdp->lun, DEV_BSIZE,
253 DEVSTAT_NO_ORDERED_TAGS,
254 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
255 DEVSTAT_PRIORITY_CD);
256 }
257 return 1;
258 }
259
260 void
261 acd_describe(struct acd *cdp)
262 {
263 int comma;
264 char *mechanism;
265
266 printf("wcd%d: drive speed ", cdp->lun);
267 if (cdp->cap.cur_speed != cdp->cap.max_speed)
268 printf("%d - ", cdp->cap.cur_speed * 1000 / 1024);
269 printf("%dKB/sec", cdp->cap.max_speed * 1000 / 1024);
270 if (cdp->cap.buf_size)
271 printf(", %dKB cache\n", cdp->cap.buf_size);
272
273 printf("wcd%d: supported read types:", cdp->lun);
274 comma = 0;
275 if (cdp->cap.read_cdr) {
276 printf(" CD-R"); comma = 1;
277 }
278 if (cdp->cap.read_cdrw) {
279 printf("%s CD-RW", comma ? "," : ""); comma = 1;
280 }
281 if (cdp->cap.cd_da) {
282 printf("%s CD-DA", comma ? "," : ""); comma = 1;
283 }
284 if (cdp->cap.method2)
285 printf("%s packet track", comma ? "," : "");
286 if (cdp->cap.write_cdr || cdp->cap.write_cdrw) {
287 printf("\nwcd%d: supported write types:", cdp->lun);
288 comma = 0;
289 if (cdp->cap.write_cdr) {
290 printf(" CD-R" ); comma = 1;
291 }
292 if (cdp->cap.write_cdrw) {
293 printf("%s CD-RW", comma ? "," : ""); comma = 1;
294 }
295 if (cdp->cap.test_write) {
296 printf("%s test write", comma ? "," : ""); comma = 1;
297 }
298 }
299 if (cdp->cap.audio_play) {
300 printf("\nwcd%d: Audio: ", cdp->lun);
301 if (cdp->cap.audio_play)
302 printf("play");
303 if (cdp->cap.max_vol_levels)
304 printf(", %d volume levels", cdp->cap.max_vol_levels);
305 }
306 printf("\nwcd%d: Mechanism: ", cdp->lun);
307 switch (cdp->cap.mech) {
308 case MST_MECH_CADDY:
309 mechanism = "caddy"; break;
310 case MST_MECH_TRAY:
311 mechanism = "tray"; break;
312 case MST_MECH_POPUP:
313 mechanism = "popup"; break;
314 case MST_MECH_CHANGER:
315 mechanism = "changer"; break;
316 case MST_MECH_CARTRIDGE:
317 mechanism = "cartridge"; break;
318 default:
319 mechanism = 0; break;
320 }
321 if (mechanism)
322 printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
323 else if (cdp->cap.eject)
324 printf("ejectable");
325
326 if (cdp->cap.mech != MST_MECH_CHANGER) {
327 printf("\nwcd%d: Medium: ", cdp->lun);
328 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
329 case MST_CDROM:
330 printf("CD-ROM "); break;
331 case MST_CDR:
332 printf("CD-R "); break;
333 case MST_CDRW:
334 printf("CD-RW "); break;
335 case MST_DOOR_OPEN:
336 printf("door open"); break;
337 case MST_NO_DISC:
338 printf("no/blank disc inside"); break;
339 case MST_FMT_ERROR:
340 printf("medium format error"); break;
341 }
342 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH) < MST_TYPE_MASK_HIGH) {
343 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
344 case MST_DATA_120:
345 printf("120mm data disc loaded"); break;
346 case MST_AUDIO_120:
347 printf("120mm audio disc loaded"); break;
348 case MST_COMB_120:
349 printf("120mm data/audio disc loaded"); break;
350 case MST_PHOTO_120:
351 printf("120mm photo disc loaded"); break;
352 case MST_DATA_80:
353 printf("80mm data disc loaded"); break;
354 case MST_AUDIO_80:
355 printf("80mm audio disc loaded"); break;
356 case MST_COMB_80:
357 printf("80mm data/audio disc loaded"); break;
358 case MST_PHOTO_80:
359 printf("80mm photo disc loaded"); break;
360 case MST_FMT_NONE:
361 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
362 case MST_CDROM:
363 printf("unknown medium"); break;
364 case MST_CDR:
365 case MST_CDRW:
366 printf("blank medium"); break;
367 }
368 break;
369 default:
370 printf("unknown type=0x%x", cdp->cap.medium_type); break;
371 }
372 }
373 }
374 if (cdp->cap.lock)
375 printf(cdp->cap.locked ? ", locked" : ", unlocked");
376 if (cdp->cap.prevent)
377 printf(", lock protected");
378 printf("\n");
379 }
380
381 static int
382 acdopen(dev_t dev, int flags, int fmt, struct thread *td)
383 {
384 struct acd *cdp;
385
386 cdp = dev->si_drv1;
387 if (cdp == NULL)
388 return ENXIO;
389
390 if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) {
391 /* Prevent user eject */
392 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
393 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
394 cdp->flags |= F_LOCKED;
395 }
396 if (fmt == S_IFBLK)
397 cdp->flags |= F_BOPEN;
398 else
399 ++cdp->refcnt;
400 dev->si_bsize_phys = cdp->block_size;
401 if (!(flags & O_NONBLOCK) && acd_read_toc(cdp) && !(flags & FWRITE))
402 printf("acd%d: read_toc failed\n", cdp->unit);
403 return 0;
404 }
405
406 int
407 acdclose(dev_t dev, int flags, int fmt, struct thread *td)
408 {
409 struct acd *cdp = dev->si_drv1;
410
411 if (fmt == S_IFBLK)
412 cdp->flags &= ~F_BOPEN;
413 else
414 --cdp->refcnt;
415
416 /* Are we the last open ?? */
417 if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) {
418 /* Yup, do we need to close any written tracks */
419 if ((flags & FWRITE) != 0) {
420 if ((cdp->flags & F_TRACK_PREPED) != 0) {
421 acd_close_track(cdp);
422 cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP);
423 }
424 }
425 /* Allow the user eject */
426 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
427 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
428 }
429 cdp->flags &= ~F_LOCKED;
430 return 0;
431 }
432
433 void
434 acdstrategy(struct bio *bp)
435 {
436 struct acd *cdp = bp->bio_dev->si_drv1;
437 int x;
438
439 #ifdef NOTYET
440 /* allow write only on CD-R/RW media */ /* all for now SOS */
441 if ((bp->bio_cmd == BIO_WRITE) && !(writeable_media)) {
442 biofinish(bp, NULL, EROFS);
443 return;
444 }
445 #endif
446
447 if (bp->bio_bcount == 0) {
448 bp->bio_resid = 0;
449 biodone(bp);
450 return;
451 }
452
453 bp->bio_pblkno = bp->bio_blkno;
454 bp->bio_resid = bp->bio_bcount;
455
456 x = splbio();
457 bioqdisksort(&cdp->bio_queue, bp);
458 acd_start(cdp);
459 splx(x);
460 }
461
462 static void
463 acd_start(struct acd *cdp)
464 {
465 struct bio *bp = bioq_first(&cdp->bio_queue);
466 u_long lba, blocks;
467 int cmd;
468 int count;
469
470 if (!bp)
471 return;
472
473 bioq_remove(&cdp->bio_queue, bp);
474
475 /* Should reject all queued entries if media have changed. */
476 if (cdp->flags & F_MEDIA_CHANGED) {
477 biofinish(bp, NULL, EIO);
478 return;
479 }
480
481 acd_select_slot(cdp);
482
483 if (bp->bio_cmd == BIO_WRITE) {
484 if ((cdp->flags & F_TRACK_PREPED) == 0) {
485 if ((cdp->flags & F_TRACK_PREP) == 0) {
486 printf("wcd%d: sequence error\n", cdp->lun);
487 biofinish(bp, NULL, EIO);
488 return;
489 } else {
490 if (acd_open_track(cdp, &cdp->preptrack) != 0) {
491 biodone(bp);
492 return;
493 }
494 cdp->flags |= F_TRACK_PREPED;
495 }
496 }
497 }
498
499 if (bp->bio_cmd == BIO_READ)
500 #ifdef NOTYET
501 lba = bp->bio_offset / cdp->block_size;
502 #else
503 lba = bp->bio_blkno / (cdp->block_size / DEV_BSIZE);
504 #endif
505 else
506 lba = cdp->next_writeable_lba + (bp->bio_offset / cdp->block_size);
507 blocks = (bp->bio_bcount + (cdp->block_size - 1)) / cdp->block_size;
508
509 if (bp->bio_cmd == BIO_WRITE) {
510 cmd = ATAPI_WRITE_BIG;
511 count = -bp->bio_bcount;
512 } else {
513 cmd = ATAPI_READ_BIG;
514 count = bp->bio_bcount;
515 }
516
517 devstat_start_transaction(cdp->device_stats);
518
519 atapi_request_callback(cdp->ata, cdp->unit, cmd, 0,
520 lba>>24, lba>>16, lba>>8, lba, 0,
521 blocks>>8, blocks, 0, 0, 0, 0, 0, 0, 0,
522 (u_char *)bp->bio_data, count,
523 (atapi_callback_t *)acd_done, cdp, bp);
524 }
525
526 static void
527 acd_done(struct acd *cdp, struct bio *bp, int resid, struct atapires result)
528 {
529
530 if (result.code) {
531 atapi_error(cdp->ata, cdp->unit, result);
532 bp->bio_error = EIO;
533 bp->bio_flags |= BIO_ERROR;
534 } else {
535 bp->bio_resid = resid;
536 if (bp->bio_cmd == BIO_WRITE)
537 cdp->flags |= F_WRITTEN;
538 }
539 biofinish(bp, cdp->device_stats, 0);
540 acd_start(cdp);
541 }
542
543 static int
544 acd_request_wait(struct acd *cdp, u_char cmd, u_char a1, u_char a2,
545 u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8,
546 u_char a9, char *addr, int count)
547 {
548 struct atapires result;
549
550 result = atapi_request_wait(cdp->ata, cdp->unit, cmd, a1, a2, a3, a4, a5,
551 a6, a7, a8, a9, 0, 0, 0, 0, 0, 0, addr, count);
552 if (result.code) {
553 atapi_error(cdp->ata, cdp->unit, result);
554 return EIO;
555 }
556 return 0;
557 }
558
559 static __inline void
560 lba2msf(int lba, u_char *m, u_char *s, u_char *f)
561 {
562 lba += 150;
563 lba &= 0xffffff;
564 *m = lba / (60 * 75);
565 lba %= (60 * 75);
566 *s = lba / 75;
567 *f = lba % 75;
568 }
569
570 static __inline int
571 msf2lba(u_char m, u_char s, u_char f)
572 {
573 return (m * 60 + s) * 75 + f - 150;
574 }
575
576 int
577 acdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
578 {
579 struct acd *cdp = dev->si_drv1;
580 int error = 0;
581
582 if (cdp->flags & F_MEDIA_CHANGED)
583 switch (cmd) {
584 case CDIOCRESET:
585 break;
586 default:
587 acd_read_toc(cdp);
588 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
589 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
590 cdp->flags |= F_LOCKED;
591 break;
592 }
593 switch (cmd) {
594 /*
595 case CDIOCRESUME:
596 bzero(cdb);
597 cdb->cmd = ATAPI_PAUSE;
598 cdb->b8 = 0x01;
599 return atapi_cmd_wait(cdp->ata, cdp->unit, cdb, 0, 0, timout, 0);
600 */
601 case CDIOCRESUME:
602 return acd_request_wait(cdp, ATAPI_PAUSE,
603 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0);
604
605 case CDIOCPAUSE:
606 return acd_request_wait(cdp, ATAPI_PAUSE,
607 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
608
609 case CDIOCSTART:
610 return acd_request_wait(cdp, ATAPI_START_STOP,
611 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
612
613 case CDIOCSTOP:
614 return acd_request_wait(cdp, ATAPI_START_STOP,
615 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
616
617 case CDIOCALLOW:
618 acd_select_slot(cdp);
619 cdp->flags &= ~F_LOCKED;
620 return acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
621 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
622
623 case CDIOCPREVENT:
624 acd_select_slot(cdp);
625 cdp->flags |= F_LOCKED;
626 return acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
627 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
628
629 case CDIOCRESET:
630 error = suser(td);
631 if (error)
632 return (error);
633 return acd_request_wait(cdp, ATAPI_TEST_UNIT_READY,
634 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
635
636 case CDIOCEJECT:
637 if ((cdp->flags & F_BOPEN) && cdp->refcnt)
638 return EBUSY;
639 return acd_eject(cdp, 0);
640
641 case CDIOCCLOSE:
642 if ((cdp->flags & F_BOPEN) && cdp->refcnt)
643 return 0;
644 return acd_eject(cdp, 1);
645
646 case CDIOREADTOCHEADER:
647 if (!cdp->toc.hdr.ending_track)
648 return EIO;
649 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
650 break;
651
652 case CDIOREADTOCENTRYS:
653 {
654 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
655 struct toc *toc = &cdp->toc;
656 struct toc buf;
657 u_long len;
658 u_char starting_track = te->starting_track;
659
660 if (!cdp->toc.hdr.ending_track)
661 return EIO;
662
663 if (te->data_len < sizeof(toc->tab[0]) ||
664 (te->data_len % sizeof(toc->tab[0])) != 0 ||
665 (te->address_format != CD_MSF_FORMAT &&
666 te->address_format != CD_LBA_FORMAT))
667 return EINVAL;
668
669 if (!starting_track)
670 starting_track = toc->hdr.starting_track;
671 else if (starting_track == 170)
672 starting_track = toc->hdr.ending_track + 1;
673 else if (starting_track < toc->hdr.starting_track ||
674 starting_track > toc->hdr.ending_track + 1)
675 return EINVAL;
676
677 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
678 sizeof(toc->tab[0]);
679 if (te->data_len < len)
680 len = te->data_len;
681 if (len > sizeof(toc->tab))
682 return EINVAL;
683
684 if (te->address_format == CD_MSF_FORMAT) {
685 struct cd_toc_entry *entry;
686
687 buf = cdp->toc;
688 toc = &buf;
689 entry = toc->tab + (toc->hdr.ending_track + 1 -
690 toc->hdr.starting_track) + 1;
691 while (--entry >= toc->tab)
692 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
693 &entry->addr.msf.second, &entry->addr.msf.frame);
694 }
695 return copyout(toc->tab + starting_track - toc->hdr.starting_track,
696 te->data, len);
697 }
698
699 case CDIOREADTOCENTRY:
700 {
701 struct ioc_read_toc_single_entry *te =
702 (struct ioc_read_toc_single_entry *)addr;
703 struct toc *toc = &cdp->toc;
704 struct toc buf;
705 u_char track = te->track;
706
707 if (!cdp->toc.hdr.ending_track)
708 return EIO;
709
710 if (te->address_format != CD_MSF_FORMAT &&
711 te->address_format != CD_LBA_FORMAT)
712 return EINVAL;
713
714 if (!track)
715 track = toc->hdr.starting_track;
716 else if (track == 170)
717 track = toc->hdr.ending_track + 1;
718 else if (track < toc->hdr.starting_track ||
719 track > toc->hdr.ending_track + 1)
720 return EINVAL;
721
722 if (te->address_format == CD_MSF_FORMAT) {
723 struct cd_toc_entry *entry;
724
725 buf = cdp->toc;
726 toc = &buf;
727 entry = toc->tab + (track - toc->hdr.starting_track);
728 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
729 &entry->addr.msf.second, &entry->addr.msf.frame);
730 }
731 bcopy(toc->tab + track - toc->hdr.starting_track,
732 &te->entry, sizeof(struct cd_toc_entry));
733 }
734 break;
735
736 case CDIOCREADSUBCHANNEL:
737 {
738 struct ioc_read_subchannel *args =
739 (struct ioc_read_subchannel *)addr;
740 struct cd_sub_channel_info data;
741 u_long len = args->data_len;
742 int abslba, rellba;
743
744 if (len > sizeof(data) ||
745 len < sizeof(struct cd_sub_channel_header))
746 return EINVAL;
747
748 if (acd_request_wait(cdp, ATAPI_READ_SUBCHANNEL,
749 0, 0x40, 1, 0, 0, 0,
750 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
751 0,
752 (char *)&cdp->subchan,
753 sizeof(cdp->subchan)) != 0)
754 return EIO;
755 if (cdp->flags & F_DEBUG)
756 atapi_dump(cdp->ata->ctrlr, cdp->lun, "subchan", &cdp->subchan,
757 sizeof(cdp->subchan));
758
759 abslba = cdp->subchan.abslba;
760 rellba = cdp->subchan.rellba;
761 if (args->address_format == CD_MSF_FORMAT) {
762 lba2msf(ntohl(abslba),
763 &data.what.position.absaddr.msf.minute,
764 &data.what.position.absaddr.msf.second,
765 &data.what.position.absaddr.msf.frame);
766 lba2msf(ntohl(rellba),
767 &data.what.position.reladdr.msf.minute,
768 &data.what.position.reladdr.msf.second,
769 &data.what.position.reladdr.msf.frame);
770 } else {
771 data.what.position.absaddr.lba = abslba;
772 data.what.position.reladdr.lba = rellba;
773 }
774 data.header.audio_status = cdp->subchan.audio_status;
775 data.what.position.control = cdp->subchan.control & 0xf;
776 data.what.position.addr_type = cdp->subchan.control >> 4;
777 data.what.position.track_number = cdp->subchan.track;
778 data.what.position.index_number = cdp->subchan.indx;
779 return copyout(&data, args->data, len);
780 }
781
782 case CDIOCPLAYMSF:
783 {
784 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
785
786 return acd_request_wait(cdp, ATAPI_PLAY_MSF, 0, 0,
787 args->start_m, args->start_s, args->start_f,
788 args->end_m, args->end_s, args->end_f,
789 0, 0, 0);
790 }
791
792 case CDIOCPLAYBLOCKS:
793 {
794 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
795
796 return acd_request_wait(cdp, ATAPI_PLAY_BIG, 0,
797 args->blk>>24 & 0xff, args->blk>>16 & 0xff,
798 args->blk>>8 & 0xff, args->blk & 0xff,
799 args->len>>24 & 0xff, args->len>>16 & 0xff,
800 args->len>>8 & 0xff, args->len & 0xff,
801 0, 0);
802 }
803
804 case CDIOCPLAYTRACKS:
805 {
806 struct ioc_play_track *args = (struct ioc_play_track *)addr;
807 u_long start, len;
808 int t1, t2;
809
810 if (!cdp->toc.hdr.ending_track)
811 return EIO;
812
813 if (args->end_track < cdp->toc.hdr.ending_track + 1)
814 ++args->end_track;
815 if (args->end_track > cdp->toc.hdr.ending_track + 1)
816 args->end_track = cdp->toc.hdr.ending_track + 1;
817 t1 = args->start_track - cdp->toc.hdr.starting_track;
818 t2 = args->end_track - cdp->toc.hdr.starting_track;
819 if (t1 < 0 || t2 < 0)
820 return EINVAL;
821 start = ntohl(cdp->toc.tab[t1].addr.lba);
822 len = ntohl(cdp->toc.tab[t2].addr.lba) - start;
823
824 return acd_request_wait(cdp, ATAPI_PLAY_BIG, 0,
825 start>>24 & 0xff, start>>16 & 0xff,
826 start>>8 & 0xff, start & 0xff,
827 len>>24 & 0xff, len>>16 & 0xff,
828 len>>8 & 0xff, len & 0xff, 0, 0);
829 }
830
831 case CDIOCREADAUDIO:
832 {
833 struct ioc_read_audio* args = (struct ioc_read_audio*) addr;
834 int lba, frames, result = 0;
835 u_char *buffer, *ubuf = args->buffer;
836
837 if (!cdp->toc.hdr.ending_track)
838 return EIO;
839
840 if ((frames = args->nframes) < 0)
841 return EINVAL;
842
843 if (args->address_format == CD_LBA_FORMAT)
844 lba = args->address.lba;
845 else if (args->address_format == CD_MSF_FORMAT)
846 lba = msf2lba(args->address.msf.minute,
847 args->address.msf.second,
848 args->address.msf.frame);
849 else
850 return EINVAL;
851 #ifndef CD_BUFFER_BLOCKS
852 #define CD_BUFFER_BLOCKS 8
853 #endif
854 if (!(buffer = malloc(CD_BUFFER_BLOCKS * 2352, M_TEMP, M_NOWAIT)))
855 return ENOMEM;
856
857 while (frames > 0) {
858 u_char blocks;
859 int size;
860
861 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
862 size = blocks * 2352;
863
864 result = acd_request_wait(cdp, ATAPI_READ_CD, 4,
865 lba>>24, (lba>>16)&0xff,
866 (lba>>8)&0xff, lba&0xff, 0, 0,
867 blocks, 0xf0, buffer, size);
868 if (result != 0)
869 break;
870
871 result = copyout(buffer, ubuf, size);
872 if (result != 0)
873 break;
874
875 ubuf += size;
876 frames -= blocks;
877 lba += blocks;
878 }
879
880 free(buffer, M_TEMP);
881 return result;
882 }
883
884 case CDIOCGETVOL:
885 {
886 struct ioc_vol *arg = (struct ioc_vol *)addr;
887
888 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE,
889 0, 0, 0, 0,
890 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
891 (char *)&cdp->au, sizeof(cdp->au));
892 if (error)
893 return error;
894 if (cdp->flags & F_DEBUG)
895 atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au,
896 sizeof(cdp->au));
897 if (cdp->au.page_code != CDROM_AUDIO_PAGE)
898 return EIO;
899 arg->vol[0] = cdp->au.port[0].volume;
900 arg->vol[1] = cdp->au.port[1].volume;
901 arg->vol[2] = cdp->au.port[2].volume;
902 arg->vol[3] = cdp->au.port[3].volume;
903 }
904 break;
905
906 case CDIOCSETVOL:
907 {
908 struct ioc_vol *arg = (struct ioc_vol *)addr;
909
910 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE,
911 0, 0, 0, 0,
912 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
913 (char *)&cdp->au, sizeof(cdp->au));
914 if (error)
915 return error;
916 if (cdp->flags & F_DEBUG)
917 atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au,
918 sizeof(cdp->au));
919 if (cdp->au.page_code != CDROM_AUDIO_PAGE)
920 return EIO;
921
922 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0,
923 CDROM_AUDIO_PAGE_MASK, 0, 0, 0, 0,
924 sizeof(cdp->aumask)>>8,sizeof(cdp->aumask),
925 0,
926 (char *)&cdp->aumask, sizeof(cdp->aumask));
927 if (error)
928 return error;
929 if (cdp->flags & F_DEBUG)
930 atapi_dump(cdp->ata->ctrlr, cdp->lun, "mask", &cdp->aumask,
931 sizeof(cdp->aumask));
932
933 cdp->au.data_length = 0;
934 cdp->au.port[0].channels = CHANNEL_0;
935 cdp->au.port[1].channels = CHANNEL_1;
936 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
937 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
938 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
939 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
940 return acd_request_wait(cdp, ATAPI_MODE_SELECT, 0x10,
941 0, 0, 0, 0, 0,
942 sizeof(cdp->au)>>8, sizeof(cdp->au),
943 0, (char *)&cdp->au, -sizeof(cdp->au));
944 }
945
946 case CDIOCSETPATCH:
947 {
948 struct ioc_patch *arg = (struct ioc_patch *)addr;
949
950 return acd_setchan(cdp, arg->patch[0], arg->patch[1],
951 arg->patch[2], arg->patch[3]);
952 }
953
954 case CDIOCSETMONO:
955 return acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0, 0);
956
957 case CDIOCSETSTEREO:
958 return acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
959
960 case CDIOCSETMUTE:
961 return acd_setchan(cdp, 0, 0, 0, 0);
962
963 case CDIOCSETLEFT:
964 return acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
965
966 case CDIOCSETRIGHT:
967 return acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
968
969 case CDRIOCNEXTWRITEABLEADDR:
970 {
971 struct acd_track_info track_info;
972
973 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
974 break;
975 if (!track_info.nwa_valid)
976 return EINVAL;
977 cdp->next_writeable_lba = track_info.next_writeable_addr;
978 *(int*)addr = track_info.next_writeable_addr;
979 }
980 break;
981
982 case WORMIOCPREPDISK:
983 {
984 struct wormio_prepare_disk *w = (struct wormio_prepare_disk *)addr;
985
986 if (w->dummy != 0 && w->dummy != 1)
987 error = EINVAL;
988 else {
989 error = acd_open_disk(cdp, w->dummy);
990 if (error == 0) {
991 cdp->flags |= F_DISK_PREPED;
992 cdp->dummy = w->dummy;
993 cdp->speed = w->speed;
994 }
995 }
996 }
997 break;
998
999 case WORMIOCPREPTRACK:
1000 {
1001 struct wormio_prepare_track *w =(struct wormio_prepare_track *)addr;
1002
1003 if (w->audio != 0 && w->audio != 1)
1004 error = EINVAL;
1005 else if (w->audio == 0 && w->preemp)
1006 error = EINVAL;
1007 else if ((cdp->flags & F_DISK_PREPED) == 0) {
1008 error = EINVAL;
1009 printf("wcd%d: sequence error (PREP_TRACK)\n", cdp->lun);
1010 } else {
1011 cdp->flags |= F_TRACK_PREP;
1012 cdp->preptrack = *w;
1013 }
1014 }
1015 break;
1016
1017 case WORMIOCFINISHTRACK:
1018 if ((cdp->flags & F_TRACK_PREPED) != 0)
1019 error = acd_close_track(cdp);
1020 cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP);
1021 break;
1022
1023 case WORMIOCFIXATION:
1024 {
1025 struct wormio_fixation *w =
1026 (struct wormio_fixation *)addr;
1027
1028 if ((cdp->flags & F_WRITTEN) == 0)
1029 error = EINVAL;
1030 else if (w->toc_type < 0 /* WORM_TOC_TYPE_AUDIO */ ||
1031 w->toc_type > 4 /* WORM_TOC_TYPE_CDI */ )
1032 error = EINVAL;
1033 else if (w->onp != 0 && w->onp != 1)
1034 error = EINVAL;
1035 else {
1036 /* no fixation needed if dummy write */
1037 if (cdp->dummy == 0)
1038 error = acd_close_disk(cdp);
1039 cdp->flags &=
1040 ~(F_WRITTEN|F_DISK_PREPED|F_TRACK_PREP|F_TRACK_PREPED);
1041 }
1042 }
1043 break;
1044
1045 case CDRIOCBLANK:
1046 return acd_blank_disk(cdp);
1047
1048 default:
1049 return ENOTTY;
1050 }
1051 return error;
1052 }
1053
1054 static int
1055 acd_read_toc(struct acd *cdp)
1056 {
1057 int ntracks, len;
1058 struct atapires result;
1059
1060 bzero(&cdp->toc, sizeof(cdp->toc));
1061 bzero(&cdp->info, sizeof(cdp->info));
1062
1063 acd_select_slot(cdp);
1064
1065 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_TEST_UNIT_READY,
1066 0, 0, 0, 0, 0, 0, 0, 0,
1067 0, 0, 0, 0, 0, 0, 0, 0, 0);
1068
1069 if (result.code == RES_ERR &&
1070 (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION) {
1071 cdp->flags |= F_MEDIA_CHANGED;
1072 cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
1073 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_TEST_UNIT_READY,
1074 0, 0, 0, 0, 0, 0, 0, 0,
1075 0, 0, 0, 0, 0, 0, 0, 0, 0);
1076 }
1077
1078 if (result.code) {
1079 atapi_error(cdp->ata, cdp->unit, result);
1080 return EIO;
1081 }
1082
1083 cdp->flags &= ~F_MEDIA_CHANGED;
1084
1085 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1086 if (acd_request_wait(cdp, ATAPI_READ_TOC, 0, 0, 0, 0, 0, 0,
1087 len>>8, len & 0xff, 0, (char *)&cdp->toc, len) != 0) {
1088 bzero(&cdp->toc, sizeof(cdp->toc));
1089 return 0;
1090 }
1091 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1092 if (ntracks <= 0 || ntracks > MAXTRK) {
1093 bzero(&cdp->toc, sizeof(cdp->toc));
1094 return 0;
1095 }
1096
1097 len = sizeof(struct ioc_toc_header) + ntracks * sizeof(struct cd_toc_entry);
1098 if (acd_request_wait(cdp, ATAPI_READ_TOC, 0, 0, 0, 0, 0, 0,
1099 len>>8, len & 0xff, 0, (char *)&cdp->toc, len) & 0xff){
1100 bzero(&cdp->toc, sizeof(cdp->toc));
1101 return 0;
1102 }
1103
1104 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1105
1106 if (acd_request_wait(cdp, ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1107 (char *)&cdp->info, sizeof(cdp->info)) != 0)
1108 bzero(&cdp->info, sizeof(cdp->info));
1109
1110 cdp->toc.tab[ntracks].control = cdp->toc.tab[ntracks - 1].control;
1111 cdp->toc.tab[ntracks].addr_type = cdp->toc.tab[ntracks - 1].addr_type;
1112 cdp->toc.tab[ntracks].track = 170;
1113 cdp->toc.tab[ntracks].addr.lba = cdp->info.volsize;
1114
1115 cdp->info.volsize = ntohl(cdp->info.volsize);
1116 cdp->info.blksize = ntohl(cdp->info.blksize);
1117
1118 if (cdp->info.volsize && cdp->toc.hdr.ending_track
1119 && (cdp->flags & F_DEBUG)) {
1120 printf("wcd%d: ", cdp->lun);
1121 if (cdp->toc.tab[0].control & 4)
1122 printf("%ldMB ", cdp->info.volsize / 512);
1123 else
1124 printf("%ld:%ld audio ", cdp->info.volsize / 75 / 60,
1125 cdp->info.volsize / 75 % 60);
1126 printf("(%ld sectors (%ld bytes)), %d tracks\n",
1127 cdp->info.volsize, cdp->info.blksize,
1128 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1129 }
1130 return 0;
1131 }
1132
1133 /*
1134 * Set up the audio channel masks.
1135 */
1136 static int
1137 acd_setchan(struct acd *cdp, u_char c0, u_char c1, u_char c2, u_char c3)
1138 {
1139 int error;
1140
1141 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE,
1142 0, 0, 0, 0,
1143 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
1144 (char *)&cdp->au, sizeof(cdp->au));
1145 if (error)
1146 return error;
1147 if (cdp->flags & F_DEBUG)
1148 atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au, sizeof(cdp->au));
1149 if (cdp->au.page_code != CDROM_AUDIO_PAGE)
1150 return EIO;
1151
1152 cdp->au.data_length = 0;
1153 cdp->au.port[0].channels = c0;
1154 cdp->au.port[1].channels = c1;
1155 cdp->au.port[2].channels = c2;
1156 cdp->au.port[3].channels = c3;
1157 return acd_request_wait(cdp, ATAPI_MODE_SELECT, 0x10,
1158 0, 0, 0, 0, 0,
1159 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
1160 (char *)&cdp->au, -sizeof(cdp->au));
1161 }
1162
1163 static int
1164 acd_eject(struct acd *cdp, int close)
1165 {
1166 struct atapires result;
1167
1168 acd_select_slot(cdp);
1169
1170 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_START_STOP, 1,
1171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1172
1173 if (result.code == RES_ERR &&
1174 ((result.error & AER_SKEY) == AER_SK_NOT_READY ||
1175 (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION)) {
1176 int err;
1177
1178 if (!close)
1179 return 0;
1180 err = acd_request_wait(cdp, ATAPI_START_STOP, 0, 0, 0, 3,
1181 0, 0, 0, 0, 0, 0, 0);
1182 if (err)
1183 return err;
1184
1185 acd_read_toc(cdp);
1186
1187 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 1,
1188 0, 0, 0, 0, 0, 0, 0);
1189 cdp->flags |= F_LOCKED;
1190 return 0;
1191 }
1192 if (result.code) {
1193 atapi_error(cdp->ata, cdp->unit, result);
1194 return EIO;
1195 }
1196 if (close)
1197 return 0;
1198
1199 tsleep((caddr_t) &lbolt, PRIBIO, "wcdej1", 0);
1200 tsleep((caddr_t) &lbolt, PRIBIO, "wcdej2", 0);
1201
1202 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1203 cdp->flags &= ~F_LOCKED;
1204
1205 cdp->flags |= F_MEDIA_CHANGED;
1206 cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
1207 return acd_request_wait(cdp, ATAPI_START_STOP, 0, 0, 0, 2,
1208 0, 0, 0, 0, 0, 0, 0);
1209 }
1210
1211 static void
1212 acd_select_slot(struct acd *cdp)
1213 {
1214 if (cdp->slot < 0 || cdp->changer_info->current_slot == cdp->slot)
1215 return;
1216
1217 /* Unlock (might not be needed but its cheaper than asking) */
1218 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1219
1220 /* Unload the current media from player */
1221 acd_request_wait(cdp, ATAPI_LOAD_UNLOAD, 0, 0, 0, 2,
1222 0, 0, 0, cdp->changer_info->current_slot, 0, 0, 0);
1223
1224 /* load the wanted slot */
1225 acd_request_wait(cdp, ATAPI_LOAD_UNLOAD, 0, 0, 0, 3,
1226 0, 0, 0, cdp->slot, 0, 0, 0);
1227
1228 cdp->changer_info->current_slot = cdp->slot;
1229
1230 /* Lock the media if needed */
1231 if (cdp->flags & F_LOCKED) {
1232 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 1,
1233 0, 0, 0, 0, 0, 0, 0);
1234 }
1235 }
1236
1237 static int
1238 acd_open_disk(struct acd *cdp, int test)
1239 {
1240 cdp->next_writeable_lba = 0;
1241 return 0;
1242 }
1243
1244 static int
1245 acd_close_disk(struct acd *cdp)
1246 {
1247 return acd_request_wait(cdp, ATAPI_CLOSE_TRACK, 0x00,
1248 0x02, 0, 0, 0/*track*/, 0, 0, 0, 0, 0, 0);
1249 }
1250
1251 static int
1252 acd_open_track(struct acd *cdp, struct wormio_prepare_track *ptp)
1253 {
1254 struct write_param param;
1255 struct atapires result;
1256
1257 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_MODE_SENSE,
1258 0, 0x05, 0, 0, 0, 0,
1259 sizeof(param)>>8, sizeof(param),
1260 0, 0, 0, 0, 0, 0, 0,
1261 (char *)¶m, sizeof(param));
1262
1263 if (cdp->flags & F_DEBUG)
1264 atapi_dump(cdp->ata->ctrlr, cdp->lun, "0x05", ¶m, sizeof(param));
1265
1266 if (result.code == RES_UNDERRUN)
1267 result.code = 0;
1268
1269 if (result.code) {
1270 atapi_error(cdp->ata, cdp->unit, result);
1271 return EIO;
1272 }
1273 param.page_code = 0x05;
1274 param.page_length = 0x32;
1275 param.test_write = cdp->dummy ? 1 : 0;
1276 param.write_type = CDR_WTYPE_TRACK;
1277
1278 switch (ptp->audio) {
1279 /* switch (data_type) { */
1280
1281 case 0:
1282 /* case CDR_DATA: */
1283 cdp->block_size = 2048;
1284 param.track_mode = CDR_TMODE_DATA;
1285 param.data_block_type = CDR_DB_ROM_MODE1;
1286 param.session_format = CDR_SESS_CDROM;
1287 break;
1288
1289 default:
1290 /* case CDR_AUDIO: */
1291 cdp->block_size = 2352;
1292 if (ptp->preemp)
1293 param.track_mode = CDR_TMODE_AUDIO;
1294 else
1295 param.track_mode = 0;
1296 param.data_block_type = CDR_DB_RAW;
1297 param.session_format = CDR_SESS_CDROM;
1298 break;
1299
1300 /*
1301 case CDR_MODE2:
1302 param.track_mode = CDR_TMODE_DATA;
1303 param.data_block_type = CDR_DB_ROM_MODE2;
1304 param.session_format = CDR_SESS_CDROM;
1305 break;
1306
1307 case CDR_XA1:
1308 param.track_mode = CDR_TMODE_DATA;
1309 param.data_block_type = CDR_DB_XA_MODE1;
1310 param.session_format = CDR_SESS_CDROM_XA;
1311 break;
1312
1313 case CDR_XA2:
1314 param.track_mode = CDR_TMODE_DATA;
1315 param.data_block_type = CDR_DB_XA_MODE2_F1;
1316 param.session_format = CDR_SESS_CDROM_XA;
1317 break;
1318
1319 case CDR_CDI:
1320 param.track_mode = CDR_TMODE_DATA;
1321 param.data_block_type = CDR_DB_XA_MODE2_F1;
1322 param.session_format = CDR_SESS_CDI;
1323 break;
1324 */
1325 }
1326
1327 param.multi_session = CDR_MSES_NONE;
1328 param.fp = 0;
1329 param.packet_size = 0;
1330
1331 if (cdp->flags & F_DEBUG)
1332 atapi_dump(cdp->ata->ctrlr, cdp->lun, "0x05", ¶m, sizeof(param));
1333
1334 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_MODE_SELECT,
1335 0x10, 0, 0, 0, 0, 0,
1336 sizeof(param)>>8, sizeof(param),
1337 0, 0, 0, 0, 0, 0, 0,
1338 (char *)¶m, -sizeof(param));
1339
1340 if (result.code == RES_UNDERRUN)
1341 result.code = 0;
1342
1343 if (result.code) {
1344 atapi_error(cdp->ata, cdp->unit, result);
1345 return EIO;
1346 }
1347 return 0;
1348 }
1349
1350 static int
1351 acd_close_track(struct acd *cdp)
1352 {
1353 return acd_request_wait(cdp, ATAPI_SYNCHRONIZE_CACHE, 0,
1354 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1355 }
1356
1357 static int
1358 acd_read_track_info(struct acd *cdp, int lba, struct acd_track_info *info)
1359 {
1360 int error;
1361
1362 error = acd_request_wait(cdp, ATAPI_READ_TRACK_INFO, 0x01,
1363 lba>>24, (lba>>16)&0xff,
1364 (lba>>8)&0xff, lba&0xff,
1365 0,
1366 sizeof(*info)>>8, sizeof(*info), 0,
1367 (char *)info, sizeof(*info));
1368 if (error)
1369 return error;
1370 info->track_start_addr = ntohl(info->track_start_addr);
1371 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1372 info->free_blocks = ntohl(info->free_blocks);
1373 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1374 info->track_length = ntohl(info->track_length);
1375 return 0;
1376 }
1377
1378 static int
1379 acd_blank_disk(struct acd *cdp)
1380 {
1381 int error;
1382
1383 error = acd_request_wait(cdp, 0xa1, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1384 cdp->flags |= F_MEDIA_CHANGED;
1385 cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
1386 return error;
1387 }
1388
1389 static void
1390 atapi_error(struct atapi *ata, int unit, struct atapires result)
1391 {
1392 if (result.code != RES_ERR) {
1393 printf("atapi%d:%d: ERROR %d, status=%b, error=%b\n",
1394 ata->ctrlr, unit, result.code, result.status,
1395 ARS_BITS, result.error, AER_BITS);
1396 return;
1397 }
1398 switch (result.error & AER_SKEY) {
1399 case AER_SK_NOT_READY:
1400 if (ata->debug)
1401 printf("atapi%d:%d: not ready\n", ata->ctrlr, unit);
1402 break;
1403
1404 case AER_SK_BLANK_CHECK:
1405 if (ata->debug)
1406 printf("atapi%d:%d: blank check\n", ata->ctrlr, unit);
1407 break;
1408
1409 case AER_SK_MEDIUM_ERROR:
1410 if (ata->debug)
1411 printf("atapi%d:%d: medium error\n", ata->ctrlr, unit);
1412 break;
1413
1414 case AER_SK_HARDWARE_ERROR:
1415 if (ata->debug)
1416 printf("atapi%d:%d: hardware error\n", ata->ctrlr, unit);
1417 break;
1418
1419 case AER_SK_ILLEGAL_REQUEST:
1420 if (ata->debug)
1421 printf("atapi%d:%d: illegal request\n", ata->ctrlr, unit);
1422 break;
1423
1424 case AER_SK_UNIT_ATTENTION:
1425 if (ata->debug)
1426 printf("atapi%d:%d: unit attention\n", ata->ctrlr, unit);
1427 break;
1428
1429 case AER_SK_DATA_PROTECT:
1430 if (ata->debug)
1431 printf("atapi%d:%d: reading protected data\n", ata->ctrlr, unit);
1432 break;
1433
1434 case AER_SK_ABORTED_COMMAND:
1435 if (ata->debug)
1436 printf("atapi%d:%d: command aborted\n", ata->ctrlr, unit);
1437 break;
1438
1439 case AER_SK_MISCOMPARE:
1440 if (ata->debug)
1441 printf("atapi%d:%d: data don't match medium\n", ata->ctrlr, unit);
1442 break;
1443
1444 default:
1445 if (ata->debug)
1446 printf("atapi%d:%d: unknown error, status=%b, error=%b\n",
1447 ata->ctrlr, unit, result.status, ARS_BITS,
1448 result.error, AER_BITS);
1449 }
1450 }
1451
1452 static void
1453 atapi_dump(int ctrlr, int lun, char *label, void *data, int len)
1454 {
1455 u_char *p = data;
1456
1457 printf ("atapi%d%d: %s %x", ctrlr, lun, label, *p++);
1458 while (--len > 0) printf ("-%x", *p++);
1459 printf ("\n");
1460 }
Cache object: b8afa0bfafc8eaa8af46c4f9bede2a21
|