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.
28 * $FreeBSD: src/sys/i386/isa/wd_cd.c,v 1.29 2000/01/29 16:00:33 peter Exp $
29 * $DragonFly: src/sys/dev/disk/wcd/Attic/wd_cd.c,v 1.4 2003/07/19 21:14:34 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <sys/disklabel.h>
39 #include <sys/devicestat.h>
41 #include <sys/wormio.h>
42 #include <sys/fcntl.h>
45 #include <i386/isa/atapi.h>
46 #include <i386/isa/wd_cd.h>
48 static d_open_t acdopen;
49 static d_close_t acdclose;
50 static d_ioctl_t acdioctl;
51 static d_strategy_t acdstrategy;
55 static struct cdevsw acd_cdevsw = {
59 /* write */ physwrite,
63 /* strategy */ acdstrategy,
72 #define NUNIT 16 /* Max # of devices */
74 #define F_BOPEN 0x0001 /* The block device is opened */
75 #define F_MEDIA_CHANGED 0x0002 /* The media have changed since open */
76 #define F_DEBUG 0x0004 /* Print debug info */
77 #define F_LOCKED 0x0008 /* This unit is locked (or should be) */
78 #define F_TRACK_PREP 0x0010 /* Track should be prep'ed */
79 #define F_TRACK_PREPED 0x0020 /* Track has been prep'ed */
80 #define F_DISK_PREPED 0x0040 /* Disk has been prep'ed */
81 #define F_WRITTEN 0x0080 /* The medium has been written to */
83 static struct acd *acdtab[NUNIT];
84 static int acdnlun = 0; /* Number of configured drives */
86 int acdattach(struct atapi *, int, struct atapi_params *, int);
87 static struct acd *acd_init_lun(struct atapi *, int, struct atapi_params *, int,
89 static void acd_start(struct acd *);
90 static void acd_done(struct acd *, struct buf *, int, struct atapires);
91 static int acd_read_toc(struct acd *);
92 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);
93 static void acd_describe(struct acd *);
94 static int acd_setchan(struct acd *, u_char, u_char, u_char, u_char);
95 static int acd_eject(struct acd *, int);
96 static void acd_select_slot(struct acd *);
97 static int acd_open_disk(struct acd *, int);
98 static int acd_open_track(struct acd *, struct wormio_prepare_track *);
99 static int acd_close_track(struct acd *);
100 static int acd_close_disk(struct acd *);
101 static int acd_read_track_info(struct acd *cdp, int lba, struct acd_track_info *info);
102 static int acd_blank_disk(struct acd *);
103 static void atapi_dump(int ctrlr, int lun, char *label, void *data, int len);
104 static void atapi_error(struct atapi *ata, int unit, struct atapires result);
107 acd_init_lun(struct atapi *ata, int unit, struct atapi_params *ap, int lun,
108 struct devstat *device_stats)
112 if (!(ptr = malloc(sizeof(struct acd), M_TEMP, M_NOWAIT)))
114 bzero(ptr, sizeof(struct acd));
115 bufq_init(&ptr->buf_queue);
120 ptr->flags = F_MEDIA_CHANGED;
121 ptr->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
122 ptr->block_size = 2048;
125 ptr->changer_info = NULL;
126 if (device_stats == NULL) {
127 if (!(ptr->device_stats = malloc(sizeof(struct devstat),
130 bzero(ptr->device_stats, sizeof(struct devstat));
133 ptr->device_stats = device_stats;
134 make_dev(&acd_cdevsw, dkmakeminor(lun, 0, 0),
135 UID_ROOT, GID_OPERATOR, 0640, "rwcd%da", lun);
136 make_dev(&acd_cdevsw, dkmakeminor(lun, 0, RAW_PART),
137 UID_ROOT, GID_OPERATOR, 0640, "rwcd%dc", lun);
138 make_dev(&acd_cdevsw, dkmakeminor(lun, 0, 0),
139 UID_ROOT, GID_OPERATOR, 0640, "wcd%da", lun);
140 make_dev(&acd_cdevsw, dkmakeminor(lun, 0, RAW_PART),
141 UID_ROOT, GID_OPERATOR, 0640, "wcd%dc", lun);
146 acdattach(struct atapi *ata, int unit, struct atapi_params *ap, int debug)
149 struct atapires result;
153 if (acdnlun >= NUNIT) {
154 printf("wcd: too many units\n");
157 if (!atapi_request_immediate) {
158 printf("wcd: configuration error, ATAPI code not present!\n");
161 if ((cdp = acd_init_lun(ata, unit, ap, acdnlun, NULL)) == NULL) {
162 printf("wcd: out of memory\n");
165 acdtab[acdnlun] = cdp;
168 cdp->flags |= F_DEBUG;
169 atapi_dump(cdp->ata->ctrlr, cdp->lun, "info", ap, sizeof(*ap));
172 /* Get drive capabilities, some drives needs this repeated */
173 for (count = 0 ; count < 5 ; count++) {
174 result = atapi_request_immediate(ata, unit,
176 0, ATAPI_CDROM_CAP_PAGE,
178 sizeof(cdp->cap)>>8, sizeof(cdp->cap),
180 (char *)&cdp->cap, sizeof(cdp->cap));
181 if (result.code == 0 || result.code == RES_UNDERRUN)
185 /* Some drives have shorter capabilities page. */
186 if (result.code == RES_UNDERRUN)
189 if (result.code == 0) {
190 cdp->cap.max_speed = ntohs(cdp->cap.max_speed);
191 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
192 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
193 cdp->cap.cur_speed = ntohs(cdp->cap.cur_speed);
195 if (cdp->flags & F_DEBUG)
196 atapi_dump(cdp->ata->ctrlr, cdp->lun, "cap", &cdp->cap,
199 /* If this is a changer device, allocate the neeeded lun's */
200 if (cdp->cap.mech == MST_MECH_CHANGER) {
202 struct acd *tmpcdp = cdp;
204 chp = malloc(sizeof(struct changer), M_TEMP, M_NOWAIT);
206 printf("wcd: out of memory\n");
209 bzero(chp, sizeof(struct changer));
210 result = atapi_request_immediate(ata, unit, ATAPI_MECH_STATUS,
212 sizeof(struct changer)>>8,
213 sizeof(struct changer),
215 (char *)chp, sizeof(struct changer));
216 if (cdp->flags & F_DEBUG) {
217 printf("result.code=%d curr=%02x slots=%d len=%d\n",
218 result.code, chp->current_slot, chp->slots,
219 htons(chp->table_length));
221 if (result.code == RES_UNDERRUN)
224 if (result.code == 0) {
225 chp->table_length = htons(chp->table_length);
226 for (i = 0; i < chp->slots && acdnlun < NUNIT; i++) {
228 tmpcdp = acd_init_lun(ata, unit, ap, acdnlun,
231 printf("wcd: out of memory\n");
236 tmpcdp->changer_info = chp;
237 printf("wcd%d: changer slot %d %s\n", acdnlun, i,
238 (chp->slot[i].present ? "disk present" : "no disk"));
239 acdtab[acdnlun++] = tmpcdp;
241 if (acdnlun >= NUNIT) {
242 printf("wcd: too many units\n");
246 sprintf(string, "wcd%d-", cdp->lun);
247 devstat_add_entry(cdp->device_stats, string, tmpcdp->lun, DEV_BSIZE,
248 DEVSTAT_NO_ORDERED_TAGS,
249 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
250 DEVSTAT_PRIORITY_CD);
254 devstat_add_entry(cdp->device_stats, "wcd", cdp->lun, DEV_BSIZE,
255 DEVSTAT_NO_ORDERED_TAGS,
256 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
257 DEVSTAT_PRIORITY_CD);
263 acd_describe(struct acd *cdp)
268 printf("wcd%d: drive speed ", cdp->lun);
269 if (cdp->cap.cur_speed != cdp->cap.max_speed)
270 printf("%d - ", cdp->cap.cur_speed * 1000 / 1024);
271 printf("%dKB/sec", cdp->cap.max_speed * 1000 / 1024);
272 if (cdp->cap.buf_size)
273 printf(", %dKB cache\n", cdp->cap.buf_size);
275 printf("wcd%d: supported read types:", cdp->lun);
277 if (cdp->cap.read_cdr) {
278 printf(" CD-R"); comma = 1;
280 if (cdp->cap.read_cdrw) {
281 printf("%s CD-RW", comma ? "," : ""); comma = 1;
283 if (cdp->cap.cd_da) {
284 printf("%s CD-DA", comma ? "," : ""); comma = 1;
286 if (cdp->cap.method2)
287 printf("%s packet track", comma ? "," : "");
288 if (cdp->cap.write_cdr || cdp->cap.write_cdrw) {
289 printf("\nwcd%d: supported write types:", cdp->lun);
291 if (cdp->cap.write_cdr) {
292 printf(" CD-R" ); comma = 1;
294 if (cdp->cap.write_cdrw) {
295 printf("%s CD-RW", comma ? "," : ""); comma = 1;
297 if (cdp->cap.test_write) {
298 printf("%s test write", comma ? "," : ""); comma = 1;
301 if (cdp->cap.audio_play) {
302 printf("\nwcd%d: Audio: ", cdp->lun);
303 if (cdp->cap.audio_play)
305 if (cdp->cap.max_vol_levels)
306 printf(", %d volume levels", cdp->cap.max_vol_levels);
308 printf("\nwcd%d: Mechanism: ", cdp->lun);
309 switch (cdp->cap.mech) {
311 mechanism = "caddy"; break;
313 mechanism = "tray"; break;
315 mechanism = "popup"; break;
316 case MST_MECH_CHANGER:
317 mechanism = "changer"; break;
318 case MST_MECH_CARTRIDGE:
319 mechanism = "cartridge"; break;
321 mechanism = 0; break;
324 printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
325 else if (cdp->cap.eject)
328 if (cdp->cap.mech != MST_MECH_CHANGER) {
329 printf("\nwcd%d: Medium: ", cdp->lun);
330 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
332 printf("CD-ROM "); break;
334 printf("CD-R "); break;
336 printf("CD-RW "); break;
338 printf("door open"); break;
340 printf("no/blank disc inside"); break;
342 printf("medium format error"); break;
344 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH) < MST_TYPE_MASK_HIGH) {
345 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
347 printf("120mm data disc loaded"); break;
349 printf("120mm audio disc loaded"); break;
351 printf("120mm data/audio disc loaded"); break;
353 printf("120mm photo disc loaded"); break;
355 printf("80mm data disc loaded"); break;
357 printf("80mm audio disc loaded"); break;
359 printf("80mm data/audio disc loaded"); break;
361 printf("80mm photo disc loaded"); break;
363 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
365 printf("unknown medium"); break;
368 printf("blank medium"); break;
372 printf("unknown type=0x%x", cdp->cap.medium_type); break;
377 printf(cdp->cap.locked ? ", locked" : ", unlocked");
378 if (cdp->cap.prevent)
379 printf(", lock protected");
384 acdopen(dev_t dev, int flags, int fmt, struct proc *p)
386 int lun = dkunit(dev);
389 if (lun >= acdnlun || !atapi_request_immediate)
393 if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) {
394 /* Prevent user eject */
395 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
396 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
397 cdp->flags |= F_LOCKED;
400 cdp->flags |= F_BOPEN;
403 dev->si_bsize_phys = cdp->block_size;
404 if (!(flags & O_NONBLOCK) && acd_read_toc(cdp) && !(flags & FWRITE))
405 printf("acd%d: read_toc failed\n", lun);
410 acdclose(dev_t dev, int flags, int fmt, struct proc *p)
412 struct acd *cdp = acdtab[dkunit(dev)];
415 cdp->flags &= ~F_BOPEN;
419 /* Are we the last open ?? */
420 if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) {
421 /* Yup, do we need to close any written tracks */
422 if ((flags & FWRITE) != 0) {
423 if ((cdp->flags & F_TRACK_PREPED) != 0) {
424 acd_close_track(cdp);
425 cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP);
428 /* Allow the user eject */
429 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
430 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
432 cdp->flags &= ~F_LOCKED;
437 acdstrategy(struct buf *bp)
439 int lun = dkunit(bp->b_dev);
440 struct acd *cdp = acdtab[lun];
444 /* allow write only on CD-R/RW media */ /* all for now SOS */
445 if (!(bp->b_flags & B_READ) && !(writeable_media)) {
447 bp->b_flags |= B_ERROR;
453 if (bp->b_bcount == 0) {
459 bp->b_pblkno = bp->b_blkno;
460 bp->b_resid = bp->b_bcount;
463 bufqdisksort(&cdp->buf_queue, bp);
469 acd_start(struct acd *cdp)
471 struct buf *bp = bufq_first(&cdp->buf_queue);
479 bufq_remove(&cdp->buf_queue, bp);
481 /* Should reject all queued entries if media have changed. */
482 if (cdp->flags & F_MEDIA_CHANGED) {
484 bp->b_flags |= B_ERROR;
489 acd_select_slot(cdp);
491 if ((bp->b_flags & B_READ) == B_WRITE) {
492 if ((cdp->flags & F_TRACK_PREPED) == 0) {
493 if ((cdp->flags & F_TRACK_PREP) == 0) {
494 printf("wcd%d: sequence error\n", cdp->lun);
496 bp->b_flags |= B_ERROR;
500 if (acd_open_track(cdp, &cdp->preptrack) != 0) {
504 cdp->flags |= F_TRACK_PREPED;
509 if (bp->b_flags & B_READ)
511 lba = bp->b_offset / cdp->block_size;
513 lba = bp->b_blkno / (cdp->block_size / DEV_BSIZE);
516 lba = cdp->next_writeable_lba + (bp->b_offset / cdp->block_size);
517 blocks = (bp->b_bcount + (cdp->block_size - 1)) / cdp->block_size;
519 if ((bp->b_flags & B_READ) == B_WRITE) {
520 cmd = ATAPI_WRITE_BIG;
521 count = -bp->b_bcount;
523 cmd = ATAPI_READ_BIG;
524 count = bp->b_bcount;
527 devstat_start_transaction(cdp->device_stats);
529 atapi_request_callback(cdp->ata, cdp->unit, cmd, 0,
530 lba>>24, lba>>16, lba>>8, lba, 0,
531 blocks>>8, blocks, 0, 0, 0, 0, 0, 0, 0,
532 (u_char *)bp->b_data, count,
533 (atapi_callback_t *)acd_done, cdp, bp);
537 acd_done(struct acd *cdp, struct buf *bp, int resid, struct atapires result)
541 atapi_error(cdp->ata, cdp->unit, result);
543 bp->b_flags |= B_ERROR;
546 if ((bp->b_flags & B_READ) == B_WRITE)
547 cdp->flags |= F_WRITTEN;
549 devstat_end_transaction_buf(cdp->device_stats, bp);
555 acd_request_wait(struct acd *cdp, u_char cmd, u_char a1, u_char a2,
556 u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8,
557 u_char a9, char *addr, int count)
559 struct atapires result;
561 result = atapi_request_wait(cdp->ata, cdp->unit, cmd, a1, a2, a3, a4, a5,
562 a6, a7, a8, a9, 0, 0, 0, 0, 0, 0, addr, count);
564 atapi_error(cdp->ata, cdp->unit, result);
571 lba2msf(int lba, u_char *m, u_char *s, u_char *f)
575 *m = lba / (60 * 75);
582 msf2lba(u_char m, u_char s, u_char f)
584 return (m * 60 + s) * 75 + f - 150;
588 acdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
590 int lun = dkunit(dev);
591 struct acd *cdp = acdtab[lun];
594 if (cdp->flags & F_MEDIA_CHANGED)
600 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
601 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
602 cdp->flags |= F_LOCKED;
609 cdb->cmd = ATAPI_PAUSE;
611 return atapi_cmd_wait(cdp->ata, cdp->unit, cdb, 0, 0, timout, 0);
614 return acd_request_wait(cdp, ATAPI_PAUSE,
615 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0);
618 return acd_request_wait(cdp, ATAPI_PAUSE,
619 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
622 return acd_request_wait(cdp, ATAPI_START_STOP,
623 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
626 return acd_request_wait(cdp, ATAPI_START_STOP,
627 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
630 acd_select_slot(cdp);
631 cdp->flags &= ~F_LOCKED;
632 return acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
633 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
636 acd_select_slot(cdp);
637 cdp->flags |= F_LOCKED;
638 return acd_request_wait(cdp, ATAPI_PREVENT_ALLOW,
639 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
645 return acd_request_wait(cdp, ATAPI_TEST_UNIT_READY,
646 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
649 if ((cdp->flags & F_BOPEN) && cdp->refcnt)
651 return acd_eject(cdp, 0);
654 if ((cdp->flags & F_BOPEN) && cdp->refcnt)
656 return acd_eject(cdp, 1);
658 case CDIOREADTOCHEADER:
659 if (!cdp->toc.hdr.ending_track)
661 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
664 case CDIOREADTOCENTRYS:
666 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
667 struct toc *toc = &cdp->toc;
670 u_char starting_track = te->starting_track;
672 if (!cdp->toc.hdr.ending_track)
675 if (te->data_len < sizeof(toc->tab[0]) ||
676 (te->data_len % sizeof(toc->tab[0])) != 0 ||
677 (te->address_format != CD_MSF_FORMAT &&
678 te->address_format != CD_LBA_FORMAT))
682 starting_track = toc->hdr.starting_track;
683 else if (starting_track == 170)
684 starting_track = toc->hdr.ending_track + 1;
685 else if (starting_track < toc->hdr.starting_track ||
686 starting_track > toc->hdr.ending_track + 1)
689 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
691 if (te->data_len < len)
693 if (len > sizeof(toc->tab))
696 if (te->address_format == CD_MSF_FORMAT) {
697 struct cd_toc_entry *entry;
701 entry = toc->tab + (toc->hdr.ending_track + 1 -
702 toc->hdr.starting_track) + 1;
703 while (--entry >= toc->tab)
704 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
705 &entry->addr.msf.second, &entry->addr.msf.frame);
707 return copyout(toc->tab + starting_track - toc->hdr.starting_track,
711 case CDIOREADTOCENTRY:
713 struct ioc_read_toc_single_entry *te =
714 (struct ioc_read_toc_single_entry *)addr;
715 struct toc *toc = &cdp->toc;
717 u_char track = te->track;
719 if (!cdp->toc.hdr.ending_track)
722 if (te->address_format != CD_MSF_FORMAT &&
723 te->address_format != CD_LBA_FORMAT)
727 track = toc->hdr.starting_track;
728 else if (track == 170)
729 track = toc->hdr.ending_track + 1;
730 else if (track < toc->hdr.starting_track ||
731 track > toc->hdr.ending_track + 1)
734 if (te->address_format == CD_MSF_FORMAT) {
735 struct cd_toc_entry *entry;
739 entry = toc->tab + (track - toc->hdr.starting_track);
740 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
741 &entry->addr.msf.second, &entry->addr.msf.frame);
743 bcopy(toc->tab + track - toc->hdr.starting_track,
744 &te->entry, sizeof(struct cd_toc_entry));
748 case CDIOCREADSUBCHANNEL:
750 struct ioc_read_subchannel *args =
751 (struct ioc_read_subchannel *)addr;
752 struct cd_sub_channel_info data;
753 u_long len = args->data_len;
756 if (len > sizeof(data) ||
757 len < sizeof(struct cd_sub_channel_header))
760 if (acd_request_wait(cdp, ATAPI_READ_SUBCHANNEL,
762 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
764 (char *)&cdp->subchan,
765 sizeof(cdp->subchan)) != 0)
767 if (cdp->flags & F_DEBUG)
768 atapi_dump(cdp->ata->ctrlr, cdp->lun, "subchan", &cdp->subchan,
769 sizeof(cdp->subchan));
771 abslba = cdp->subchan.abslba;
772 rellba = cdp->subchan.rellba;
773 if (args->address_format == CD_MSF_FORMAT) {
774 lba2msf(ntohl(abslba),
775 &data.what.position.absaddr.msf.minute,
776 &data.what.position.absaddr.msf.second,
777 &data.what.position.absaddr.msf.frame);
778 lba2msf(ntohl(rellba),
779 &data.what.position.reladdr.msf.minute,
780 &data.what.position.reladdr.msf.second,
781 &data.what.position.reladdr.msf.frame);
783 data.what.position.absaddr.lba = abslba;
784 data.what.position.reladdr.lba = rellba;
786 data.header.audio_status = cdp->subchan.audio_status;
787 data.what.position.control = cdp->subchan.control & 0xf;
788 data.what.position.addr_type = cdp->subchan.control >> 4;
789 data.what.position.track_number = cdp->subchan.track;
790 data.what.position.index_number = cdp->subchan.indx;
791 return copyout(&data, args->data, len);
796 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
798 return acd_request_wait(cdp, ATAPI_PLAY_MSF, 0, 0,
799 args->start_m, args->start_s, args->start_f,
800 args->end_m, args->end_s, args->end_f,
804 case CDIOCPLAYBLOCKS:
806 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
808 return acd_request_wait(cdp, ATAPI_PLAY_BIG, 0,
809 args->blk>>24 & 0xff, args->blk>>16 & 0xff,
810 args->blk>>8 & 0xff, args->blk & 0xff,
811 args->len>>24 & 0xff, args->len>>16 & 0xff,
812 args->len>>8 & 0xff, args->len & 0xff,
816 case CDIOCPLAYTRACKS:
818 struct ioc_play_track *args = (struct ioc_play_track *)addr;
822 if (!cdp->toc.hdr.ending_track)
825 if (args->end_track < cdp->toc.hdr.ending_track + 1)
827 if (args->end_track > cdp->toc.hdr.ending_track + 1)
828 args->end_track = cdp->toc.hdr.ending_track + 1;
829 t1 = args->start_track - cdp->toc.hdr.starting_track;
830 t2 = args->end_track - cdp->toc.hdr.starting_track;
831 if (t1 < 0 || t2 < 0)
833 start = ntohl(cdp->toc.tab[t1].addr.lba);
834 len = ntohl(cdp->toc.tab[t2].addr.lba) - start;
836 return acd_request_wait(cdp, ATAPI_PLAY_BIG, 0,
837 start>>24 & 0xff, start>>16 & 0xff,
838 start>>8 & 0xff, start & 0xff,
839 len>>24 & 0xff, len>>16 & 0xff,
840 len>>8 & 0xff, len & 0xff, 0, 0);
845 struct ioc_read_audio* args = (struct ioc_read_audio*) addr;
846 int lba, frames, result = 0;
847 u_char *buffer, *ubuf = args->buffer;
849 if (!cdp->toc.hdr.ending_track)
852 if ((frames = args->nframes) < 0)
855 if (args->address_format == CD_LBA_FORMAT)
856 lba = args->address.lba;
857 else if (args->address_format == CD_MSF_FORMAT)
858 lba = msf2lba(args->address.msf.minute,
859 args->address.msf.second,
860 args->address.msf.frame);
863 #ifndef CD_BUFFER_BLOCKS
864 #define CD_BUFFER_BLOCKS 8
866 if (!(buffer = malloc(CD_BUFFER_BLOCKS * 2352, M_TEMP, M_NOWAIT)))
873 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
874 size = blocks * 2352;
876 result = acd_request_wait(cdp, ATAPI_READ_CD, 4,
877 lba>>24, (lba>>16)&0xff,
878 (lba>>8)&0xff, lba&0xff, 0, 0,
879 blocks, 0xf0, buffer, size);
883 result = copyout(buffer, ubuf, size);
892 free(buffer, M_TEMP);
898 struct ioc_vol *arg = (struct ioc_vol *)addr;
900 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE,
902 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
903 (char *)&cdp->au, sizeof(cdp->au));
906 if (cdp->flags & F_DEBUG)
907 atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au,
909 if (cdp->au.page_code != CDROM_AUDIO_PAGE)
911 arg->vol[0] = cdp->au.port[0].volume;
912 arg->vol[1] = cdp->au.port[1].volume;
913 arg->vol[2] = cdp->au.port[2].volume;
914 arg->vol[3] = cdp->au.port[3].volume;
920 struct ioc_vol *arg = (struct ioc_vol *)addr;
922 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE,
924 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
925 (char *)&cdp->au, sizeof(cdp->au));
928 if (cdp->flags & F_DEBUG)
929 atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au,
931 if (cdp->au.page_code != CDROM_AUDIO_PAGE)
934 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0,
935 CDROM_AUDIO_PAGE_MASK, 0, 0, 0, 0,
936 sizeof(cdp->aumask)>>8,sizeof(cdp->aumask),
938 (char *)&cdp->aumask, sizeof(cdp->aumask));
941 if (cdp->flags & F_DEBUG)
942 atapi_dump(cdp->ata->ctrlr, cdp->lun, "mask", &cdp->aumask,
943 sizeof(cdp->aumask));
945 cdp->au.data_length = 0;
946 cdp->au.port[0].channels = CHANNEL_0;
947 cdp->au.port[1].channels = CHANNEL_1;
948 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
949 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
950 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
951 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
952 return acd_request_wait(cdp, ATAPI_MODE_SELECT, 0x10,
954 sizeof(cdp->au)>>8, sizeof(cdp->au),
955 0, (char *)&cdp->au, -sizeof(cdp->au));
960 struct ioc_patch *arg = (struct ioc_patch *)addr;
962 return acd_setchan(cdp, arg->patch[0], arg->patch[1],
963 arg->patch[2], arg->patch[3]);
967 return acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0, 0);
970 return acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
973 return acd_setchan(cdp, 0, 0, 0, 0);
976 return acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
979 return acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
981 case CDRIOCNEXTWRITEABLEADDR:
983 struct acd_track_info track_info;
985 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
987 if (!track_info.nwa_valid)
989 cdp->next_writeable_lba = track_info.next_writeable_addr;
990 *(int*)addr = track_info.next_writeable_addr;
994 case WORMIOCPREPDISK:
996 struct wormio_prepare_disk *w = (struct wormio_prepare_disk *)addr;
998 if (w->dummy != 0 && w->dummy != 1)
1001 error = acd_open_disk(cdp, w->dummy);
1003 cdp->flags |= F_DISK_PREPED;
1004 cdp->dummy = w->dummy;
1005 cdp->speed = w->speed;
1011 case WORMIOCPREPTRACK:
1013 struct wormio_prepare_track *w =(struct wormio_prepare_track *)addr;
1015 if (w->audio != 0 && w->audio != 1)
1017 else if (w->audio == 0 && w->preemp)
1019 else if ((cdp->flags & F_DISK_PREPED) == 0) {
1021 printf("wcd%d: sequence error (PREP_TRACK)\n", cdp->lun);
1023 cdp->flags |= F_TRACK_PREP;
1024 cdp->preptrack = *w;
1029 case WORMIOCFINISHTRACK:
1030 if ((cdp->flags & F_TRACK_PREPED) != 0)
1031 error = acd_close_track(cdp);
1032 cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP);
1035 case WORMIOCFIXATION:
1037 struct wormio_fixation *w =
1038 (struct wormio_fixation *)addr;
1040 if ((cdp->flags & F_WRITTEN) == 0)
1042 else if (w->toc_type < 0 /* WORM_TOC_TYPE_AUDIO */ ||
1043 w->toc_type > 4 /* WORM_TOC_TYPE_CDI */ )
1045 else if (w->onp != 0 && w->onp != 1)
1048 /* no fixation needed if dummy write */
1049 if (cdp->dummy == 0)
1050 error = acd_close_disk(cdp);
1052 ~(F_WRITTEN|F_DISK_PREPED|F_TRACK_PREP|F_TRACK_PREPED);
1058 return acd_blank_disk(cdp);
1067 acd_read_toc(struct acd *cdp)
1070 struct atapires result;
1072 bzero(&cdp->toc, sizeof(cdp->toc));
1073 bzero(&cdp->info, sizeof(cdp->info));
1075 acd_select_slot(cdp);
1077 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_TEST_UNIT_READY,
1078 0, 0, 0, 0, 0, 0, 0, 0,
1079 0, 0, 0, 0, 0, 0, 0, 0, 0);
1081 if (result.code == RES_ERR &&
1082 (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION) {
1083 cdp->flags |= F_MEDIA_CHANGED;
1084 cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
1085 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_TEST_UNIT_READY,
1086 0, 0, 0, 0, 0, 0, 0, 0,
1087 0, 0, 0, 0, 0, 0, 0, 0, 0);
1091 atapi_error(cdp->ata, cdp->unit, result);
1095 cdp->flags &= ~F_MEDIA_CHANGED;
1097 len = sizeof(struct ioc_toc_header) + 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) != 0) {
1100 bzero(&cdp->toc, sizeof(cdp->toc));
1103 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1104 if (ntracks <= 0 || ntracks > MAXTRK) {
1105 bzero(&cdp->toc, sizeof(cdp->toc));
1109 len = sizeof(struct ioc_toc_header) + ntracks * sizeof(struct cd_toc_entry);
1110 if (acd_request_wait(cdp, ATAPI_READ_TOC, 0, 0, 0, 0, 0, 0,
1111 len>>8, len & 0xff, 0, (char *)&cdp->toc, len) & 0xff){
1112 bzero(&cdp->toc, sizeof(cdp->toc));
1116 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1118 if (acd_request_wait(cdp, ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1119 (char *)&cdp->info, sizeof(cdp->info)) != 0)
1120 bzero(&cdp->info, sizeof(cdp->info));
1122 cdp->toc.tab[ntracks].control = cdp->toc.tab[ntracks - 1].control;
1123 cdp->toc.tab[ntracks].addr_type = cdp->toc.tab[ntracks - 1].addr_type;
1124 cdp->toc.tab[ntracks].track = 170;
1125 cdp->toc.tab[ntracks].addr.lba = cdp->info.volsize;
1127 cdp->info.volsize = ntohl(cdp->info.volsize);
1128 cdp->info.blksize = ntohl(cdp->info.blksize);
1130 if (cdp->info.volsize && cdp->toc.hdr.ending_track
1131 && (cdp->flags & F_DEBUG)) {
1132 printf("wcd%d: ", cdp->lun);
1133 if (cdp->toc.tab[0].control & 4)
1134 printf("%ldMB ", cdp->info.volsize / 512);
1136 printf("%ld:%ld audio ", cdp->info.volsize / 75 / 60,
1137 cdp->info.volsize / 75 % 60);
1138 printf("(%ld sectors (%ld bytes)), %d tracks\n",
1139 cdp->info.volsize, cdp->info.blksize,
1140 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1146 * Set up the audio channel masks.
1149 acd_setchan(struct acd *cdp, u_char c0, u_char c1, u_char c2, u_char c3)
1153 error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE,
1155 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
1156 (char *)&cdp->au, sizeof(cdp->au));
1159 if (cdp->flags & F_DEBUG)
1160 atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au, sizeof(cdp->au));
1161 if (cdp->au.page_code != CDROM_AUDIO_PAGE)
1164 cdp->au.data_length = 0;
1165 cdp->au.port[0].channels = c0;
1166 cdp->au.port[1].channels = c1;
1167 cdp->au.port[2].channels = c2;
1168 cdp->au.port[3].channels = c3;
1169 return acd_request_wait(cdp, ATAPI_MODE_SELECT, 0x10,
1171 sizeof(cdp->au)>>8, sizeof(cdp->au), 0,
1172 (char *)&cdp->au, -sizeof(cdp->au));
1176 acd_eject(struct acd *cdp, int close)
1178 struct atapires result;
1180 acd_select_slot(cdp);
1182 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_START_STOP, 1,
1183 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1185 if (result.code == RES_ERR &&
1186 ((result.error & AER_SKEY) == AER_SK_NOT_READY ||
1187 (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION)) {
1192 err = acd_request_wait(cdp, ATAPI_START_STOP, 0, 0, 0, 3,
1193 0, 0, 0, 0, 0, 0, 0);
1199 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 1,
1200 0, 0, 0, 0, 0, 0, 0);
1201 cdp->flags |= F_LOCKED;
1205 atapi_error(cdp->ata, cdp->unit, result);
1211 tsleep((caddr_t) &lbolt, 0, "wcdej1", 0);
1212 tsleep((caddr_t) &lbolt, 0, "wcdej2", 0);
1214 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1215 cdp->flags &= ~F_LOCKED;
1217 cdp->flags |= F_MEDIA_CHANGED;
1218 cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
1219 return acd_request_wait(cdp, ATAPI_START_STOP, 0, 0, 0, 2,
1220 0, 0, 0, 0, 0, 0, 0);
1224 acd_select_slot(struct acd *cdp)
1226 if (cdp->slot < 0 || cdp->changer_info->current_slot == cdp->slot)
1229 /* Unlock (might not be needed but its cheaper than asking) */
1230 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1232 /* Unload the current media from player */
1233 acd_request_wait(cdp, ATAPI_LOAD_UNLOAD, 0, 0, 0, 2,
1234 0, 0, 0, cdp->changer_info->current_slot, 0, 0, 0);
1236 /* load the wanted slot */
1237 acd_request_wait(cdp, ATAPI_LOAD_UNLOAD, 0, 0, 0, 3,
1238 0, 0, 0, cdp->slot, 0, 0, 0);
1240 cdp->changer_info->current_slot = cdp->slot;
1242 /* Lock the media if needed */
1243 if (cdp->flags & F_LOCKED) {
1244 acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 1,
1245 0, 0, 0, 0, 0, 0, 0);
1250 acd_open_disk(struct acd *cdp, int test)
1252 cdp->next_writeable_lba = 0;
1257 acd_close_disk(struct acd *cdp)
1259 return acd_request_wait(cdp, ATAPI_CLOSE_TRACK, 0x00,
1260 0x02, 0, 0, 0/*track*/, 0, 0, 0, 0, 0, 0);
1264 acd_open_track(struct acd *cdp, struct wormio_prepare_track *ptp)
1266 struct write_param param;
1267 struct atapires result;
1269 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_MODE_SENSE,
1270 0, 0x05, 0, 0, 0, 0,
1271 sizeof(param)>>8, sizeof(param),
1272 0, 0, 0, 0, 0, 0, 0,
1273 (char *)¶m, sizeof(param));
1275 if (cdp->flags & F_DEBUG)
1276 atapi_dump(cdp->ata->ctrlr, cdp->lun, "0x05", ¶m, sizeof(param));
1278 if (result.code == RES_UNDERRUN)
1282 atapi_error(cdp->ata, cdp->unit, result);
1285 param.page_code = 0x05;
1286 param.page_length = 0x32;
1287 param.test_write = cdp->dummy ? 1 : 0;
1288 param.write_type = CDR_WTYPE_TRACK;
1290 switch (ptp->audio) {
1291 /* switch (data_type) { */
1294 /* case CDR_DATA: */
1295 cdp->block_size = 2048;
1296 param.track_mode = CDR_TMODE_DATA;
1297 param.data_block_type = CDR_DB_ROM_MODE1;
1298 param.session_format = CDR_SESS_CDROM;
1302 /* case CDR_AUDIO: */
1303 cdp->block_size = 2352;
1305 param.track_mode = CDR_TMODE_AUDIO;
1307 param.track_mode = 0;
1308 param.data_block_type = CDR_DB_RAW;
1309 param.session_format = CDR_SESS_CDROM;
1314 param.track_mode = CDR_TMODE_DATA;
1315 param.data_block_type = CDR_DB_ROM_MODE2;
1316 param.session_format = CDR_SESS_CDROM;
1320 param.track_mode = CDR_TMODE_DATA;
1321 param.data_block_type = CDR_DB_XA_MODE1;
1322 param.session_format = CDR_SESS_CDROM_XA;
1326 param.track_mode = CDR_TMODE_DATA;
1327 param.data_block_type = CDR_DB_XA_MODE2_F1;
1328 param.session_format = CDR_SESS_CDROM_XA;
1332 param.track_mode = CDR_TMODE_DATA;
1333 param.data_block_type = CDR_DB_XA_MODE2_F1;
1334 param.session_format = CDR_SESS_CDI;
1339 param.multi_session = CDR_MSES_NONE;
1341 param.packet_size = 0;
1343 if (cdp->flags & F_DEBUG)
1344 atapi_dump(cdp->ata->ctrlr, cdp->lun, "0x05", ¶m, sizeof(param));
1346 result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_MODE_SELECT,
1347 0x10, 0, 0, 0, 0, 0,
1348 sizeof(param)>>8, sizeof(param),
1349 0, 0, 0, 0, 0, 0, 0,
1350 (char *)¶m, -sizeof(param));
1352 if (result.code == RES_UNDERRUN)
1356 atapi_error(cdp->ata, cdp->unit, result);
1363 acd_close_track(struct acd *cdp)
1365 return acd_request_wait(cdp, ATAPI_SYNCHRONIZE_CACHE, 0,
1366 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1370 acd_read_track_info(struct acd *cdp, int lba, struct acd_track_info *info)
1374 error = acd_request_wait(cdp, ATAPI_READ_TRACK_INFO, 0x01,
1375 lba>>24, (lba>>16)&0xff,
1376 (lba>>8)&0xff, lba&0xff,
1378 sizeof(*info)>>8, sizeof(*info), 0,
1379 (char *)info, sizeof(*info));
1382 info->track_start_addr = ntohl(info->track_start_addr);
1383 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1384 info->free_blocks = ntohl(info->free_blocks);
1385 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1386 info->track_length = ntohl(info->track_length);
1391 acd_blank_disk(struct acd *cdp)
1395 error = acd_request_wait(cdp, 0xa1, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1396 cdp->flags |= F_MEDIA_CHANGED;
1397 cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED);
1402 atapi_error(struct atapi *ata, int unit, struct atapires result)
1404 if (result.code != RES_ERR) {
1405 printf("atapi%d:%d: ERROR %d, status=%b, error=%b\n",
1406 ata->ctrlr, unit, result.code, result.status,
1407 ARS_BITS, result.error, AER_BITS);
1410 switch (result.error & AER_SKEY) {
1411 case AER_SK_NOT_READY:
1413 printf("atapi%d:%d: not ready\n", ata->ctrlr, unit);
1416 case AER_SK_BLANK_CHECK:
1418 printf("atapi%d:%d: blank check\n", ata->ctrlr, unit);
1421 case AER_SK_MEDIUM_ERROR:
1423 printf("atapi%d:%d: medium error\n", ata->ctrlr, unit);
1426 case AER_SK_HARDWARE_ERROR:
1428 printf("atapi%d:%d: hardware error\n", ata->ctrlr, unit);
1431 case AER_SK_ILLEGAL_REQUEST:
1433 printf("atapi%d:%d: illegal request\n", ata->ctrlr, unit);
1436 case AER_SK_UNIT_ATTENTION:
1438 printf("atapi%d:%d: unit attention\n", ata->ctrlr, unit);
1441 case AER_SK_DATA_PROTECT:
1443 printf("atapi%d:%d: reading protected data\n", ata->ctrlr, unit);
1446 case AER_SK_ABORTED_COMMAND:
1448 printf("atapi%d:%d: command aborted\n", ata->ctrlr, unit);
1451 case AER_SK_MISCOMPARE:
1453 printf("atapi%d:%d: data don't match medium\n", ata->ctrlr, unit);
1458 printf("atapi%d:%d: unknown error, status=%b, error=%b\n",
1459 ata->ctrlr, unit, result.status, ARS_BITS,
1460 result.error, AER_BITS);
1465 atapi_dump(int ctrlr, int lun, char *label, void *data, int len)
1469 printf ("atapi%d%d: %s %x", ctrlr, lun, label, *p++);
1470 while (--len > 0) printf ("-%x", *p++);
1475 acd_drvinit(void *unused)
1477 cdevsw_add(&acd_cdevsw);
1480 SYSINIT(acddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, acd_drvinit, NULL)