2 * Copyright (c) 1998, 1999 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
34 #if NWCD > 0 && NWDC > 0
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/disklabel.h>
43 #include <sys/devicestat.h>
45 #include <sys/wormio.h>
46 #include <sys/fcntl.h>
49 #include <i386/isa/atapi.h>
50 #include <i386/isa/atapi-cd.h>
52 static d_open_t acdopen;
53 static d_close_t acdclose;
54 static d_ioctl_t acdioctl;
55 static d_strategy_t acdstrategy;
59 static struct cdevsw acd_cdevsw = {
63 /* write */ physwrite,
67 /* strategy */ acdstrategy,
76 #define NUNIT 16 /* Max # of devices */
78 #define F_BOPEN 0x0001 /* The block device is opened */
79 #define F_MEDIA_CHANGED 0x0002 /* The media have changed since open */
80 #define F_DEBUG 0x0004 /* Print debug info */
81 #define F_LOCKED 0x0008 /* This unit is locked (or should be) */
82 #define F_TRACK_PREP 0x0010 /* Track should be prep'ed */
83 #define F_TRACK_PREPED 0x0020 /* Track has been prep'ed */
84 #define F_DISK_PREPED 0x0040 /* Disk has been prep'ed */
85 #define F_WRITTEN 0x0080 /* The medium has been written to */
87 static struct acd *acdtab[NUNIT];
88 static int acdnlun = 0; /* Number of configured drives */
90 int acdattach(struct atapi *, int, struct atapi_params *, int);
91 static struct acd *acd_init_lun(struct atapi *, int, struct atapi_params *, int,
93 static void acd_start(struct acd *);
94 static void acd_done(struct acd *, struct buf *, int, struct atapires);
95 static int acd_read_toc(struct acd *);
96 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);
97 static void acd_describe(struct acd *);
98 static int acd_setchan(struct acd *, u_char, u_char, u_char, u_char);
99 static int acd_eject(struct acd *, int);
100 static void acd_select_slot(struct acd *);
101 static int acd_open_disk(struct acd *, int);
102 static int acd_open_track(struct acd *, struct wormio_prepare_track *);
103 static int acd_close_track(struct acd *);
104 static int acd_close_disk(struct acd *);
105 static int acd_read_track_info(struct acd *cdp, int lba, struct acd_track_info *info);
106 static int acd_blank_disk(struct acd *);
107 static void atapi_dump(int ctrlr, int lun, char *label, void *data, int len);
108 static void atapi_error(struct atapi *ata, int unit, struct atapires result);
111 acd_init_lun(struct atapi *ata, int unit, struct atapi_params *ap, int lun,
112 struct devstat *device_stats)
116 if (!(ptr = malloc(sizeof(struct acd), M_TEMP, M_NOWAIT)))
118 bzero(ptr, sizeof(struct acd));
119 bufq_init(&ptr->buf_queue);
124 ptr->flags = F_MEDIA_CHANGED;
125 ptr->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
126 ptr->block_size = 2048;
129 ptr->changer_info = NULL;
130 if (device_stats == NULL) {
131 if (!(ptr->device_stats = malloc(sizeof(struct devstat),
134 bzero(ptr->device_stats, sizeof(struct devstat));
137 ptr->device_stats = device_stats;
138 make_dev(&acd_cdevsw, dkmakeminor(lun, 0, 0),
139 UID_ROOT, GID_OPERATOR, 0640, "rwcd%da", lun);
140 make_dev(&acd_cdevsw, dkmakeminor(lun, 0, RAW_PART),
141 UID_ROOT, GID_OPERATOR, 0640, "rwcd%dc", lun);
142 make_dev(&acd_cdevsw, dkmakeminor(lun, 0, 0),
143 UID_ROOT, GID_OPERATOR, 0640, "wcd%da", lun);
144 make_dev(&acd_cdevsw, dkmakeminor(lun, 0, RAW_PART),
145 UID_ROOT, GID_OPERATOR, 0640, "wcd%dc", lun);
150 acdattach(struct atapi *ata, int unit, struct atapi_params *ap, int debug)
153 struct atapires result;
157 if (acdnlun >= NUNIT) {
158 printf("wcd: too many units\n");
161 if (!atapi_request_immediate) {
162 printf("wcd: configuration error, ATAPI code not present!\n");
165 if ((cdp = acd_init_lun(ata, unit, ap, acdnlun, NULL)) == NULL) {
166 printf("wcd: out of memory\n");
169 acdtab[acdnlun] = cdp;
172 cdp->flags |= F_DEBUG;
173 atapi_dump(cdp->ata->ctrlr, cdp->lun, "info", ap, sizeof(*ap));
176 /* Get drive capabilities, some drives needs this repeated */
177 for (count = 0 ; count < 5 ; count++) {
178 result = atapi_request_immediate(ata, unit,
180 0, ATAPI_CDROM_CAP_PAGE,
182 sizeof(cdp->cap)>>8, sizeof(cdp->cap),
184 (char *)&cdp->cap, sizeof(cdp->cap));
185 if (result.code == 0 || result.code == RES_UNDERRUN)
189 /* Some drives have shorter capabilities page. */
190 if (result.code == RES_UNDERRUN)
193 if (result.code == 0) {
194 cdp->cap.max_speed = ntohs(cdp->cap.max_speed);
195 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
196 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
197 cdp->cap.cur_speed = ntohs(cdp->cap.cur_speed);
199 if (cdp->flags & F_DEBUG)
200 atapi_dump(cdp->ata->ctrlr, cdp->lun, "cap", &cdp->cap,
203 /* If this is a changer device, allocate the neeeded lun's */
204 if (cdp->cap.mech == MST_MECH_CHANGER) {
206 struct acd *tmpcdp = cdp;
208 chp = malloc(sizeof(struct changer), M_TEMP, M_NOWAIT);
210 printf("wcd: out of memory\n");
213 bzero(chp, sizeof(struct changer));
214 result = atapi_request_immediate(ata, unit, ATAPI_MECH_STATUS,
216 sizeof(struct changer)>>8,
217 sizeof(struct changer),
219 (char *)chp, sizeof(struct changer));
220 if (cdp->flags & F_DEBUG) {
221 printf("result.code=%d curr=%02x slots=%d len=%d\n",
222 result.code, chp->current_slot, chp->slots,
223 htons(chp->table_length));
225 if (result.code == RES_UNDERRUN)
228 if (result.code == 0) {
229 chp->table_length = htons(chp->table_length);
230 for (i = 0; i < chp->slots && acdnlun < NUNIT; i++) {
232 tmpcdp = acd_init_lun(ata, unit, ap, acdnlun,
235 printf("wcd: out of memory\n");
240 tmpcdp->changer_info = chp;
241 printf("wcd%d: changer slot %d %s\n", acdnlun, i,
242 (chp->slot[i].present ? "disk present" : "no disk"));
243 acdtab[acdnlun++] = tmpcdp;
245 if (acdnlun >= NUNIT) {
246 printf("wcd: too many units\n");
250 sprintf(string, "wcd%d-", cdp->lun);
251 devstat_add_entry(cdp->device_stats, string, tmpcdp->lun, DEV_BSIZE,
252 DEVSTAT_NO_ORDERED_TAGS,
253 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
254 DEVSTAT_PRIORITY_WCD);
258 devstat_add_entry(cdp->device_stats, "wcd", cdp->lun, DEV_BSIZE,
259 DEVSTAT_NO_ORDERED_TAGS,
260 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
261 DEVSTAT_PRIORITY_WCD);
267 acd_describe(struct acd *cdp)
272 printf("wcd%d: drive speed ", cdp->lun);
273 if (cdp->cap.cur_speed != cdp->cap.max_speed)
274 printf("%d - ", cdp->cap.cur_speed * 1000 / 1024);
275 printf("%dKB/sec", cdp->cap.max_speed * 1000 / 1024);
276 if (cdp->cap.buf_size)
277 printf(", %dKB cache\n", cdp->cap.buf_size);
279 printf("wcd%d: supported read types:", cdp->lun);
281 if (cdp->cap.read_cdr) {
282 printf(" CD-R"); comma = 1;
284 if (cdp->cap.read_cdrw) {
285 printf("%s CD-RW", comma ? "," : ""); comma = 1;
287 if (cdp->cap.cd_da) {
288 printf("%s CD-DA", comma ? "," : ""); comma = 1;
290 if (cdp->cap.method2)
291 printf("%s packet track", comma ? "," : "");
292 if (cdp->cap.write_cdr || cdp->cap.write_cdrw) {
293 printf("\nwcd%d: supported write types:", cdp->lun);
295 if (cdp->cap.write_cdr) {
296 printf(" CD-R" ); comma = 1;
298 if (cdp->cap.write_cdrw) {
299 printf("%s CD-RW", comma ? "," : ""); comma = 1;
301 if (cdp->cap.test_write) {
302 printf("%s test write", comma ? "," : ""); comma = 1;
305 if (cdp->cap.audio_play) {
306 printf("\nwcd%d: Audio: ", cdp->lun);
307 if (cdp->cap.audio_play)
309 if (cdp->cap.max_vol_levels)
310 printf(", %d volume levels", cdp->cap.max_vol_levels);
312 printf("\nwcd%d: Mechanism: ", cdp->lun);
313 switch (cdp->cap.mech) {
315 mechanism = "caddy"; break;
317 mechanism = "tray"; break;
319 mechanism = "popup"; break;
320 case MST_MECH_CHANGER:
321 mechanism = "changer"; break;
322 case MST_MECH_CARTRIDGE:
323 mechanism = "cartridge"; break;
325 mechanism = 0; break;
328 printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
329 else if (cdp->cap.eject)
332 if (cdp->cap.mech != MST_MECH_CHANGER) {
333 printf("\nwcd%d: Medium: ", cdp->lun);
334 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
336 printf("CD-ROM "); break;
338 printf("CD-R "); break;
340 printf("CD-RW "); break;
342 printf("door open"); break;
344 printf("no/blank disc inside"); break;
346 printf("medium format error"); break;
348 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH) < MST_TYPE_MASK_HIGH) {
349 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
351 printf("120mm data disc loaded"); break;
353 printf("120mm audio disc loaded"); break;
355 printf("120mm data/audio disc loaded"); break;
357 printf("120mm photo disc loaded"); break;
359 printf("80mm data disc loaded"); break;
361 printf("80mm audio disc loaded"); break;
363 printf("80mm data/audio disc loaded"); break;
365 printf("80mm photo disc loaded"); break;
367 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
369 printf("unknown medium"); break;
372 printf("blank medium"); break;
376 printf("unknown type=0x%x", cdp->cap.medium_type); break;
381 printf(cdp->cap.locked ? ", locked" : ", unlocked");
382 if (cdp->cap.prevent)
383 printf(", lock protected");
388 acdopen(dev_t dev, int flags, int fmt, struct proc *p)
390 int lun = dkunit(dev);
393 if (lun >= acdnlun || !atapi_request_immediate)
397 if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) {
398 /* Prevent user eject */
399 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
400 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
401 cdp->flags |= F_LOCKED;
404 cdp->flags |= F_BOPEN;
407 dev->si_bsize_phys = cdp->block_size;
408 if (!(flags & O_NONBLOCK) && acd_read_toc(cdp) && !(flags & FWRITE))
409 printf("acd%d: read_toc failed\n", lun);
414 acdclose(dev_t dev, int flags, int fmt, struct proc *p)
416 struct acd *cdp = acdtab[dkunit(dev)];
419 cdp->flags &= ~F_BOPEN;
423 /* Are we the last open ?? */
424 if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) {
425 /* Yup, do we need to close any written tracks */
426 if ((flags & FWRITE) != 0) {
427 if ((cdp->flags & F_TRACK_PREPED) != 0) {
428 acd_close_track(cdp);
429 cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP);
432 /* Allow the user eject */
433 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
434 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
436 cdp->flags &= ~F_LOCKED;
441 acdstrategy(struct buf *bp)
443 int lun = dkunit(bp->b_dev);
444 struct acd *cdp = acdtab[lun];
448 /* allow write only on CD-R/RW media */ /* all for now SOS */
449 if (!(bp->b_flags & B_READ) && !(writeable_media)) {
451 bp->b_flags |= B_ERROR;
457 if (bp->b_bcount == 0) {
463 bp->b_pblkno = bp->b_blkno;
464 bp->b_resid = bp->b_bcount;
467 bufqdisksort(&cdp->buf_queue, bp);
473 acd_start(struct acd *cdp)
475 struct buf *bp = bufq_first(&cdp->buf_queue);
483 bufq_remove(&cdp->buf_queue, bp);
485 /* Should reject all queued entries if media have changed. */
486 if (cdp->flags & F_MEDIA_CHANGED) {
488 bp->b_flags |= B_ERROR;
493 acd_select_slot(cdp);
495 if ((bp->b_flags & B_READ) == B_WRITE) {
496 if ((cdp->flags & F_TRACK_PREPED) == 0) {
497 if ((cdp->flags & F_TRACK_PREP) == 0) {
498 printf("wcd%d: sequence error\n", cdp->lun);
500 bp->b_flags |= B_ERROR;
504 if (acd_open_track(cdp, &cdp->preptrack) != 0) {
508 cdp->flags |= F_TRACK_PREPED;
513 if (bp->b_flags & B_READ)
515 lba = bp->b_offset / cdp->block_size;
517 lba = bp->b_blkno / (cdp->block_size / DEV_BSIZE);
520 lba = cdp->next_writeable_lba + (bp->b_offset / cdp->block_size);
521 blocks = (bp->b_bcount + (cdp->block_size - 1)) / cdp->block_size;
523 if ((bp->b_flags & B_READ) == B_WRITE) {
524 cmd = ATAPI_WRITE_BIG;
525 count = -bp->b_bcount;
527 cmd = ATAPI_READ_BIG;
528 count = bp->b_bcount;
531 devstat_start_transaction(cdp->device_stats);
533 atapi_request_callback(cdp->ata, cdp->unit, cmd, 0,
534 lba>>24, lba>>16, lba>>8, lba, 0,
535 blocks>>8, blocks, 0, 0, 0, 0, 0, 0, 0,
536 (u_char *)bp->b_data, count,
537 (atapi_callback_t *)acd_done, cdp, bp);
541 acd_done(struct acd *cdp, struct buf *bp, int resid, struct atapires result)
545 atapi_error(cdp->ata, cdp->unit, result);
547 bp->b_flags |= B_ERROR;
550 if ((bp->b_flags & B_READ) == B_WRITE)
551 cdp->flags |= F_WRITTEN;
553 devstat_end_transaction_buf(cdp->device_stats, bp);
559 acd_request_wait(struct acd *cdp, u_char cmd, u_char a1, u_char a2,
560 u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8,
561 u_char a9, char *addr, int count)
563 struct atapires result;
565 result = atapi_request_wait(cdp->ata, cdp->unit, cmd, a1, a2, a3, a4, a5,
566 a6, a7, a8, a9, 0, 0, 0, 0, 0, 0, addr, count);
568 atapi_error(cdp->ata, cdp->unit, result);
575 lba2msf(int lba, u_char *m, u_char *s, u_char *f)
579 *m = lba / (60 * 75);
586 msf2lba(u_char m, u_char s, u_char f)
588 return (m * 60 + s) * 75 + f - 150;
592 acdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
594 int lun = dkunit(dev);
595 struct acd *cdp = acdtab[lun];
598 if (cdp->flags & F_MEDIA_CHANGED)
604 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
605 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
606 cdp->flags |= F_LOCKED;
613 cdb->cmd = ATAPI_PAUSE;
615 return atapi_cmd_wait(cdp->ata, cdp->unit, cdb, 0, 0, timout, 0);
618 return acd_request_wait(cdp, ATAPI_PAUSE,
619 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0);
622 return acd_request_wait(cdp, ATAPI_PAUSE,
623 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
626 return acd_request_wait(cdp, ATAPI_START_STOP,
627 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
630 return acd_request_wait(cdp, ATAPI_START_STOP,
631 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
634 acd_select_slot(cdp);
635 cdp->flags &= ~F_LOCKED;
636 return acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
637 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
640 acd_select_slot(cdp);
641 cdp->flags |= F_LOCKED;
642 return acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
643 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
649 return acd_request_wait(cdp, ATAPI_TEST_UNIT_READY,
650 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
653 if ((cdp->flags & F_BOPEN) && cdp->refcnt)
655 return acd_eject(cdp, 0);
658 if ((cdp->flags & F_BOPEN) && cdp->refcnt)
660 return acd_eject(cdp, 1);
662 case CDIOREADTOCHEADER:
663 if (!cdp->toc.hdr.ending_track)
665 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
668 case CDIOREADTOCENTRYS:
670 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
671 struct toc *toc = &cdp->toc;
674 u_char starting_track = te->starting_track;
676 if (!cdp->toc.hdr.ending_track)
679 if (te->data_len < sizeof(toc->tab[0]) ||
680 (te->data_len % sizeof(toc->tab[0])) != 0 ||
681 (te->address_format != CD_MSF_FORMAT &&
682 te->address_format != CD_LBA_FORMAT))
686 starting_track = toc->hdr.starting_track;
687 else if (starting_track == 170)
688 starting_track = toc->hdr.ending_track + 1;
689 else if (starting_track < toc->hdr.starting_track ||
690 starting_track > toc->hdr.ending_track + 1)
693 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
695 if (te->data_len < len)
697 if (len > sizeof(toc->tab))
700 if (te->address_format == CD_MSF_FORMAT) {
701 struct cd_toc_entry *entry;
705 entry = toc->tab + (toc->hdr.ending_track + 1 -
706 toc->hdr.starting_track) + 1;
707 while (--entry >= toc->tab)
708 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
709 &entry->addr.msf.second, &entry->addr.msf.frame);
711 return copyout(toc->tab + starting_track - toc->hdr.starting_track,
715 case CDIOREADTOCENTRY:
717 struct ioc_read_toc_single_entry *te =
718 (struct ioc_read_toc_single_entry *)addr;
719 struct toc *toc = &cdp->toc;
721 u_char track = te->track;
723 if (!cdp->toc.hdr.ending_track)
726 if (te->address_format != CD_MSF_FORMAT &&
727 te->address_format != CD_LBA_FORMAT)
731 track = toc->hdr.starting_track;
732 else if (track == 170)
733 track = toc->hdr.ending_track + 1;
734 else if (track < toc->hdr.starting_track ||
735 track > toc->hdr.ending_track + 1)
738 if (te->address_format == CD_MSF_FORMAT) {
739 struct cd_toc_entry *entry;
743 entry = toc->tab + (track - toc->hdr.starting_track);
744 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
745 &entry->addr.msf.second, &entry->addr.msf.frame);
747 bcopy(toc->tab + track - toc->hdr.starting_track,
748 &te->entry, sizeof(struct cd_toc_entry));
752 case CDIOCREADSUBCHANNEL:
754 struct ioc_read_subchannel *args =
755 (struct ioc_read_subchannel *)addr;
756 struct cd_sub_channel_info data;
757 u_long len = args->data_len;
760 if (len > sizeof(data) ||
761 len < sizeof(struct cd_sub_channel_header))
764 if (acd_request_wait(cdp, ATAPI_READ_SUBCHANNEL,
766 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
768 (char *)&cdp->subchan,
769 sizeof(cdp->subchan)) != 0)
771 if (cdp->flags & F_DEBUG)
772 atapi_dump(cdp->ata->ctrlr, cdp->lun, "subchan", &cdp->subchan,
773 sizeof(cdp->subchan));
775 abslba = cdp->subchan.abslba;
776 rellba = cdp->subchan.rellba;
777 if (args->address_format == CD_MSF_FORMAT) {
778 lba2msf(ntohl(abslba),
779 &data.what.position.absaddr.msf.minute,
780 &data.what.position.absaddr.msf.second,
781 &data.what.position.absaddr.msf.frame);
782 lba2msf(ntohl(rellba),
783 &data.what.position.reladdr.msf.minute,
784 &data.what.position.reladdr.msf.second,
785 &data.what.position.reladdr.msf.frame);
787 data.what.position.absaddr.lba = abslba;
788 data.what.position.reladdr.lba = rellba;
790 data.header.audio_status = cdp->subchan.audio_status;
791 data.what.position.control = cdp->subchan.control & 0xf;
792 data.what.position.addr_type = cdp->subchan.control >> 4;
793 data.what.position.track_number = cdp->subchan.track;
794 data.what.position.index_number = cdp->subchan.indx;
795 return copyout(&data, args->data, len);
800 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
802 return acd_request_wait(cdp, ATAPI_PLAY_MSF, 0, 0,
803 args->start_m, args->start_s, args->start_f,
804 args->end_m, args->end_s, args->end_f,
808 case CDIOCPLAYBLOCKS:
810 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
812 return acd_request_wait(cdp, ATAPI_PLAY_BIG, 0,
813 args->blk>>24 & 0xff, args->blk>>16 & 0xff,
814 args->blk>>8 & 0xff, args->blk & 0xff,
815 args->len>>24 & 0xff, args->len>>16 & 0xff,
816 args->len>>8 & 0xff, args->len & 0xff,
820 case CDIOCPLAYTRACKS:
822 struct ioc_play_track *args = (struct ioc_play_track *)addr;
826 if (!cdp->toc.hdr.ending_track)
829 if (args->end_track < cdp->toc.hdr.ending_track + 1)
831 if (args->end_track > cdp->toc.hdr.ending_track + 1)
832 args->end_track = cdp->toc.hdr.ending_track + 1;
833 t1 = args->start_track - cdp->toc.hdr.starting_track;
834 t2 = args->end_track - cdp->toc.hdr.starting_track;
835 if (t1 < 0 || t2 < 0)
837 start = ntohl(cdp->toc.tab[t1].addr.lba);
838 len = ntohl(cdp->toc.tab[t2].addr.lba) - start;
840 return acd_request_wait(cdp, ATAPI_PLAY_BIG, 0,
841 start>>24 & 0xff, start>>16 & 0xff,
842 start>>8 & 0xff, start & 0xff,
843 len>>24 & 0xff, len>>16 & 0xff,
844 len>>8 & 0xff, len & 0xff, 0, 0);
849 struct ioc_read_audio* args = (struct ioc_read_audio*) addr;
850 int lba, frames, result = 0;
851 u_char *buffer, *ubuf = args->buffer;
853 if (!cdp->toc.hdr.ending_track)
856 if ((frames = args->nframes) < 0)
859 if (args->address_format == CD_LBA_FORMAT)
860 lba = args->address.lba;
861 else if (args->address_format == CD_MSF_FORMAT)
862 lba = msf2lba(args->address.msf.minute,
863 args->address.msf.second,
864 args->address.msf.frame);
867 #ifndef CD_BUFFER_BLOCKS
868 #define CD_BUFFER_BLOCKS 8
870 if (!(buffer = malloc(CD_BUFFER_BLOCKS * 2352, M_TEMP, M_NOWAIT)))
877 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
878 size = blocks * 2352;
880 result = acd_request_wait(cdp, ATAPI_READ_CD, 4,
881 lba>>24, (lba>>16)&0xff,
882 (lba>>8)&0xff, lba&0xff, 0, 0,
883 blocks, 0xf0, buffer, size);
887 result = copyout(buffer, ubuf, size);
896 free(buffer, M_TEMP);
902 struct ioc_vol *arg = (struct ioc_vol *)addr;
904 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE,
906 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
907 (char *)&cdp->au, sizeof(cdp->au));
910 if (cdp->flags & F_DEBUG)
911 atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au,
913 if (cdp->au.page_code != CDROM_AUDIO_PAGE)
915 arg->vol[0] = cdp->au.port[0].volume;
916 arg->vol[1] = cdp->au.port[1].volume;
917 arg->vol[2] = cdp->au.port[2].volume;
918 arg->vol[3] = cdp->au.port[3].volume;
924 struct ioc_vol *arg = (struct ioc_vol *)addr;
926 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE,
928 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
929 (char *)&cdp->au, sizeof(cdp->au));
932 if (cdp->flags & F_DEBUG)
933 atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au,
935 if (cdp->au.page_code != CDROM_AUDIO_PAGE)
938 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0,
939 CDROM_AUDIO_PAGE_MASK, 0, 0, 0, 0,
940 sizeof(cdp->aumask)>>8,sizeof(cdp->aumask),
942 (char *)&cdp->aumask, sizeof(cdp->aumask));
945 if (cdp->flags & F_DEBUG)
946 atapi_dump(cdp->ata->ctrlr, cdp->lun, "mask", &cdp->aumask,
947 sizeof(cdp->aumask));
949 cdp->au.data_length = 0;
950 cdp->au.port[0].channels = CHANNEL_0;
951 cdp->au.port[1].channels = CHANNEL_1;
952 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
953 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
954 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
955 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
956 return acd_request_wait(cdp, ATAPI_MODE_SELECT, 0x10,
958 sizeof(cdp->au)>>8, sizeof(cdp->au),
959 0, (char *)&cdp->au, -sizeof(cdp->au));
964 struct ioc_patch *arg = (struct ioc_patch *)addr;
966 return acd_setchan(cdp, arg->patch[0], arg->patch[1],
967 arg->patch[2], arg->patch[3]);
971 return acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0, 0);
974 return acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
977 return acd_setchan(cdp, 0, 0, 0, 0);
980 return acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
983 return acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
985 case CDRIOCNEXTWRITEABLEADDR:
987 struct acd_track_info track_info;
989 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
991 if (!track_info.nwa_valid)
993 cdp->next_writeable_lba = track_info.next_writeable_addr;
994 *(int*)addr = track_info.next_writeable_addr;
998 case WORMIOCPREPDISK:
1000 struct wormio_prepare_disk *w = (struct wormio_prepare_disk *)addr;
1002 if (w->dummy != 0 && w->dummy != 1)
1005 error = acd_open_disk(cdp, w->dummy);
1007 cdp->flags |= F_DISK_PREPED;
1008 cdp->dummy = w->dummy;
1009 cdp->speed = w->speed;
1015 case WORMIOCPREPTRACK:
1017 struct wormio_prepare_track *w =(struct wormio_prepare_track *)addr;
1019 if (w->audio != 0 && w->audio != 1)
1021 else if (w->audio == 0 && w->preemp)
1023 else if ((cdp->flags & F_DISK_PREPED) == 0) {
1025 printf("wcd%d: sequence error (PREP_TRACK)\n", cdp->lun);
1027 cdp->flags |= F_TRACK_PREP;
1028 cdp->preptrack = *w;
1033 case WORMIOCFINISHTRACK:
1034 if ((cdp->flags & F_TRACK_PREPED) != 0)
1035 error = acd_close_track(cdp);
1036 cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP);
1039 case WORMIOCFIXATION:
1041 struct wormio_fixation *w =
1042 (struct wormio_fixation *)addr;
1044 if ((cdp->flags & F_WRITTEN) == 0)
1046 else if (w->toc_type < 0 /* WORM_TOC_TYPE_AUDIO */ ||
1047 w->toc_type > 4 /* WORM_TOC_TYPE_CDI */ )
1049 else if (w->onp != 0 && w->onp != 1)
1052 /* no fixation needed if dummy write */
1053 if (cdp->dummy == 0)
1054 error = acd_close_disk(cdp);
1056 ~(F_WRITTEN|F_DISK_PREPED|F_TRACK_PREP|F_TRACK_PREPED);
1062 return acd_blank_disk(cdp);
1071 acd_read_toc(struct acd *cdp)
1074 struct atapires result;
1076 bzero(&cdp->toc, sizeof(cdp->toc));
1077 bzero(&cdp->info, sizeof(cdp->info));
1079 acd_select_slot(cdp);
1081 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_TEST_UNIT_READY,
1082 0, 0, 0, 0, 0, 0, 0, 0,
1083 0, 0, 0, 0, 0, 0, 0, 0, 0);
1085 if (result.code == RES_ERR &&
1086 (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION) {
1087 cdp->flags |= F_MEDIA_CHANGED;
1088 cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
1089 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_TEST_UNIT_READY,
1090 0, 0, 0, 0, 0, 0, 0, 0,
1091 0, 0, 0, 0, 0, 0, 0, 0, 0);
1095 atapi_error(cdp->ata, cdp->unit, result);
1099 cdp->flags &= ~F_MEDIA_CHANGED;
1101 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1102 if (acd_request_wait(cdp, ATAPI_READ_TOC, 0, 0, 0, 0, 0, 0,
1103 len>>8, len & 0xff, 0, (char *)&cdp->toc, len) != 0) {
1104 bzero(&cdp->toc, sizeof(cdp->toc));
1107 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1108 if (ntracks <= 0 || ntracks > MAXTRK) {
1109 bzero(&cdp->toc, sizeof(cdp->toc));
1113 len = sizeof(struct ioc_toc_header) + ntracks * sizeof(struct cd_toc_entry);
1114 if (acd_request_wait(cdp, ATAPI_READ_TOC, 0, 0, 0, 0, 0, 0,
1115 len>>8, len & 0xff, 0, (char *)&cdp->toc, len) & 0xff){
1116 bzero(&cdp->toc, sizeof(cdp->toc));
1120 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1122 if (acd_request_wait(cdp, ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1123 (char *)&cdp->info, sizeof(cdp->info)) != 0)
1124 bzero(&cdp->info, sizeof(cdp->info));
1126 cdp->toc.tab[ntracks].control = cdp->toc.tab[ntracks - 1].control;
1127 cdp->toc.tab[ntracks].addr_type = cdp->toc.tab[ntracks - 1].addr_type;
1128 cdp->toc.tab[ntracks].track = 170;
1129 cdp->toc.tab[ntracks].addr.lba = cdp->info.volsize;
1131 cdp->info.volsize = ntohl(cdp->info.volsize);
1132 cdp->info.blksize = ntohl(cdp->info.blksize);
1134 if (cdp->info.volsize && cdp->toc.hdr.ending_track
1135 && (cdp->flags & F_DEBUG)) {
1136 printf("wcd%d: ", cdp->lun);
1137 if (cdp->toc.tab[0].control & 4)
1138 printf("%ldMB ", cdp->info.volsize / 512);
1140 printf("%ld:%ld audio ", cdp->info.volsize / 75 / 60,
1141 cdp->info.volsize / 75 % 60);
1142 printf("(%ld sectors (%ld bytes)), %d tracks\n",
1143 cdp->info.volsize, cdp->info.blksize,
1144 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1150 * Set up the audio channel masks.
1153 acd_setchan(struct acd *cdp, u_char c0, u_char c1, u_char c2, u_char c3)
1157 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE,
1159 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
1160 (char *)&cdp->au, sizeof(cdp->au));
1163 if (cdp->flags & F_DEBUG)
1164 atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au, sizeof(cdp->au));
1165 if (cdp->au.page_code != CDROM_AUDIO_PAGE)
1168 cdp->au.data_length = 0;
1169 cdp->au.port[0].channels = c0;
1170 cdp->au.port[1].channels = c1;
1171 cdp->au.port[2].channels = c2;
1172 cdp->au.port[3].channels = c3;
1173 return acd_request_wait(cdp, ATAPI_MODE_SELECT, 0x10,
1175 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
1176 (char *)&cdp->au, -sizeof(cdp->au));
1180 acd_eject(struct acd *cdp, int close)
1182 struct atapires result;
1184 acd_select_slot(cdp);
1186 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_START_STOP, 1,
1187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1189 if (result.code == RES_ERR &&
1190 ((result.error & AER_SKEY) == AER_SK_NOT_READY ||
1191 (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION)) {
1196 err = acd_request_wait(cdp, ATAPI_START_STOP, 0, 0, 0, 3,
1197 0, 0, 0, 0, 0, 0, 0);
1203 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 1,
1204 0, 0, 0, 0, 0, 0, 0);
1205 cdp->flags |= F_LOCKED;
1209 atapi_error(cdp->ata, cdp->unit, result);
1215 tsleep((caddr_t) &lbolt, PRIBIO, "wcdej1", 0);
1216 tsleep((caddr_t) &lbolt, PRIBIO, "wcdej2", 0);
1218 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1219 cdp->flags &= ~F_LOCKED;
1221 cdp->flags |= F_MEDIA_CHANGED;
1222 cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
1223 return acd_request_wait(cdp, ATAPI_START_STOP, 0, 0, 0, 2,
1224 0, 0, 0, 0, 0, 0, 0);
1228 acd_select_slot(struct acd *cdp)
1230 if (cdp->slot < 0 || cdp->changer_info->current_slot == cdp->slot)
1233 /* Unlock (might not be needed but its cheaper than asking) */
1234 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1236 /* Unload the current media from player */
1237 acd_request_wait(cdp, ATAPI_LOAD_UNLOAD, 0, 0, 0, 2,
1238 0, 0, 0, cdp->changer_info->current_slot, 0, 0, 0);
1240 /* load the wanted slot */
1241 acd_request_wait(cdp, ATAPI_LOAD_UNLOAD, 0, 0, 0, 3,
1242 0, 0, 0, cdp->slot, 0, 0, 0);
1244 cdp->changer_info->current_slot = cdp->slot;
1246 /* Lock the media if needed */
1247 if (cdp->flags & F_LOCKED) {
1248 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 1,
1249 0, 0, 0, 0, 0, 0, 0);
1254 acd_open_disk(struct acd *cdp, int test)
1256 cdp->next_writeable_lba = 0;
1261 acd_close_disk(struct acd *cdp)
1263 return acd_request_wait(cdp, ATAPI_CLOSE_TRACK, 0x00,
1264 0x02, 0, 0, 0/*track*/, 0, 0, 0, 0, 0, 0);
1268 acd_open_track(struct acd *cdp, struct wormio_prepare_track *ptp)
1270 struct write_param param;
1271 struct atapires result;
1273 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_MODE_SENSE,
1274 0, 0x05, 0, 0, 0, 0,
1275 sizeof(param)>>8, sizeof(param),
1276 0, 0, 0, 0, 0, 0, 0,
1277 (char *)¶m, sizeof(param));
1279 if (cdp->flags & F_DEBUG)
1280 atapi_dump(cdp->ata->ctrlr, cdp->lun, "0x05", ¶m, sizeof(param));
1282 if (result.code == RES_UNDERRUN)
1286 atapi_error(cdp->ata, cdp->unit, result);
1289 param.page_code = 0x05;
1290 param.page_length = 0x32;
1291 param.test_write = cdp->dummy ? 1 : 0;
1292 param.write_type = CDR_WTYPE_TRACK;
1294 switch (ptp->audio) {
1295 /* switch (data_type) { */
1298 /* case CDR_DATA: */
1299 cdp->block_size = 2048;
1300 param.track_mode = CDR_TMODE_DATA;
1301 param.data_block_type = CDR_DB_ROM_MODE1;
1302 param.session_format = CDR_SESS_CDROM;
1306 /* case CDR_AUDIO: */
1307 cdp->block_size = 2352;
1309 param.track_mode = CDR_TMODE_AUDIO;
1311 param.track_mode = 0;
1312 param.data_block_type = CDR_DB_RAW;
1313 param.session_format = CDR_SESS_CDROM;
1318 param.track_mode = CDR_TMODE_DATA;
1319 param.data_block_type = CDR_DB_ROM_MODE2;
1320 param.session_format = CDR_SESS_CDROM;
1324 param.track_mode = CDR_TMODE_DATA;
1325 param.data_block_type = CDR_DB_XA_MODE1;
1326 param.session_format = CDR_SESS_CDROM_XA;
1330 param.track_mode = CDR_TMODE_DATA;
1331 param.data_block_type = CDR_DB_XA_MODE2_F1;
1332 param.session_format = CDR_SESS_CDROM_XA;
1336 param.track_mode = CDR_TMODE_DATA;
1337 param.data_block_type = CDR_DB_XA_MODE2_F1;
1338 param.session_format = CDR_SESS_CDI;
1343 param.multi_session = CDR_MSES_NONE;
1345 param.packet_size = 0;
1347 if (cdp->flags & F_DEBUG)
1348 atapi_dump(cdp->ata->ctrlr, cdp->lun, "0x05", ¶m, sizeof(param));
1350 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_MODE_SELECT,
1351 0x10, 0, 0, 0, 0, 0,
1352 sizeof(param)>>8, sizeof(param),
1353 0, 0, 0, 0, 0, 0, 0,
1354 (char *)¶m, -sizeof(param));
1356 if (result.code == RES_UNDERRUN)
1360 atapi_error(cdp->ata, cdp->unit, result);
1367 acd_close_track(struct acd *cdp)
1369 return acd_request_wait(cdp, ATAPI_SYNCHRONIZE_CACHE, 0,
1370 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1374 acd_read_track_info(struct acd *cdp, int lba, struct acd_track_info *info)
1378 error = acd_request_wait(cdp, ATAPI_READ_TRACK_INFO, 0x01,
1379 lba>>24, (lba>>16)&0xff,
1380 (lba>>8)&0xff, lba&0xff,
1382 sizeof(*info)>>8, sizeof(*info), 0,
1383 (char *)info, sizeof(*info));
1386 info->track_start_addr = ntohl(info->track_start_addr);
1387 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1388 info->free_blocks = ntohl(info->free_blocks);
1389 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1390 info->track_length = ntohl(info->track_length);
1395 acd_blank_disk(struct acd *cdp)
1399 error = acd_request_wait(cdp, 0xa1, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1400 cdp->flags |= F_MEDIA_CHANGED;
1401 cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
1406 atapi_error(struct atapi *ata, int unit, struct atapires result)
1408 if (result.code != RES_ERR) {
1409 printf("atapi%d:%d: ERROR %d, status=%b, error=%b\n",
1410 ata->ctrlr, unit, result.code, result.status,
1411 ARS_BITS, result.error, AER_BITS);
1414 switch (result.error & AER_SKEY) {
1415 case AER_SK_NOT_READY:
1417 printf("atapi%d:%d: not ready\n", ata->ctrlr, unit);
1420 case AER_SK_BLANK_CHECK:
1422 printf("atapi%d:%d: blank check\n", ata->ctrlr, unit);
1425 case AER_SK_MEDIUM_ERROR:
1427 printf("atapi%d:%d: medium error\n", ata->ctrlr, unit);
1430 case AER_SK_HARDWARE_ERROR:
1432 printf("atapi%d:%d: hardware error\n", ata->ctrlr, unit);
1435 case AER_SK_ILLEGAL_REQUEST:
1437 printf("atapi%d:%d: illegal request\n", ata->ctrlr, unit);
1440 case AER_SK_UNIT_ATTENTION:
1442 printf("atapi%d:%d: unit attention\n", ata->ctrlr, unit);
1445 case AER_SK_DATA_PROTECT:
1447 printf("atapi%d:%d: reading protected data\n", ata->ctrlr, unit);
1450 case AER_SK_ABORTED_COMMAND:
1452 printf("atapi%d:%d: command aborted\n", ata->ctrlr, unit);
1455 case AER_SK_MISCOMPARE:
1457 printf("atapi%d:%d: data don't match medium\n", ata->ctrlr, unit);
1462 printf("atapi%d:%d: unknown error, status=%b, error=%b\n",
1463 ata->ctrlr, unit, result.status, ARS_BITS,
1464 result.error, AER_BITS);
1469 atapi_dump(int ctrlr, int lun, char *label, void *data, int len)
1473 printf ("atapi%d%d: %s %x", ctrlr, lun, label, *p++);
1474 while (--len > 0) printf ("-%x", *p++);
1479 acd_drvinit(void *unused)
1481 cdevsw_add(&acd_cdevsw);
1484 SYSINIT(acddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, acd_drvinit, NULL)
1485 #endif /* NWCD && NWDC */