Remove the roll-your-own disklabel from the ATA CD driver. Use the
[dragonfly.git] / sys / dev / disk / ata / atapi-cd.c
1 /*-
2  * Copyright (c) 1998,1999,2000,2001,2002 Søren Schmidt <sos@FreeBSD.org>
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: src/sys/dev/ata/atapi-cd.c,v 1.48.2.20 2002/11/25 05:30:31 njl Exp $
29  * $DragonFly: src/sys/dev/disk/ata/atapi-cd.c,v 1.33 2007/05/17 17:44:27 dillon Exp $
30  */
31
32 #include "opt_ata.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ata.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/proc.h>
39 #include <sys/buf.h>
40 #include <sys/bus.h>
41 #include <sys/disk.h>
42 #include <sys/disklabel.h>
43 #include <sys/diskslice.h>
44 #include <sys/devicestat.h>
45 #include <sys/cdio.h>
46 #include <sys/cdrio.h>
47 #include <sys/dvdio.h>
48 #include <sys/fcntl.h>
49 #include <sys/conf.h>
50 #include <sys/ctype.h>
51 #include <sys/buf2.h>
52 #include <sys/thread2.h>
53
54 #include "ata-all.h"
55 #include "atapi-all.h"
56 #include "atapi-cd.h"
57
58 /* device structures */
59 static d_open_t         acdopen;
60 static d_close_t        acdclose;
61 static d_ioctl_t        acdioctl;
62 static d_strategy_t     acdstrategy;
63
64 static struct dev_ops acd_ops = {
65         { "acd", 117, D_DISK | D_TRACKCLOSE },
66         .d_open =       acdopen,
67         .d_close =      acdclose,
68         .d_read =       physread,
69         .d_write =      physwrite,
70         .d_ioctl =      acdioctl,
71         .d_strategy =   acdstrategy,
72 };
73
74 /* prototypes */
75 static struct acd_softc *acd_init_lun(struct ata_device *);
76 static void acd_make_dev(struct acd_softc *);
77 static void acd_set_ioparm(struct acd_softc *);
78 static void acd_describe(struct acd_softc *);
79 static void lba2msf(u_int32_t, u_int8_t *, u_int8_t *, u_int8_t *);
80 static u_int32_t msf2lba(u_int8_t, u_int8_t, u_int8_t);
81 static int acd_done(struct atapi_request *);
82 static void acd_read_toc(struct acd_softc *);
83 static int acd_play(struct acd_softc *, int, int);
84 static int acd_setchan(struct acd_softc *, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
85 static void acd_select_slot(struct acd_softc *);
86 static int acd_init_writer(struct acd_softc *, int);
87 static int acd_fixate(struct acd_softc *, int);
88 static int acd_init_track(struct acd_softc *, struct cdr_track *);
89 static int acd_flush(struct acd_softc *);
90 static int acd_read_track_info(struct acd_softc *, int32_t, struct acd_track_info *);
91 static int acd_get_progress(struct acd_softc *, int *);
92 static int acd_send_cue(struct acd_softc *, struct cdr_cuesheet *);
93 static int acd_report_key(struct acd_softc *, struct dvd_authinfo *);
94 static int acd_send_key(struct acd_softc *, struct dvd_authinfo *);
95 static int acd_read_structure(struct acd_softc *, struct dvd_struct *);
96 static int acd_eject(struct acd_softc *, int);
97 static int acd_blank(struct acd_softc *, int);
98 static int acd_prevent_allow(struct acd_softc *, int);
99 static int acd_start_stop(struct acd_softc *, int);
100 static int acd_pause_resume(struct acd_softc *, int);
101 static int acd_mode_sense(struct acd_softc *, int, caddr_t, int);
102 static int acd_mode_select(struct acd_softc *, caddr_t, int);
103 static int acd_set_speed(struct acd_softc *, int, int);
104 static void acd_get_cap(struct acd_softc *);
105
106 /* internal vars */
107 static u_int32_t acd_lun_map = 0;
108 static MALLOC_DEFINE(M_ACD, "ACD driver", "ATAPI CD driver buffers");
109
110 int
111 acdattach(struct ata_device *atadev)
112 {
113     struct acd_softc *cdp;
114     struct changer *chp;
115
116     if ((cdp = acd_init_lun(atadev)) == NULL) {
117         ata_prtdev(atadev, "acd: out of memory\n");
118         return 0;
119     }
120
121     ata_set_name(atadev, "acd", cdp->lun);
122     ata_command(atadev, ATA_C_ATAPI_RESET, 0, 0, 0, ATA_IMMEDIATE);
123     acd_get_cap(cdp);
124
125     /* if this is a changer device, allocate the neeeded lun's */
126     if (cdp->cap.mech == MST_MECH_CHANGER) {
127         int8_t ccb[16] = { ATAPI_MECH_STATUS, 0, 0, 0, 0, 0, 0, 0, 
128                            sizeof(struct changer)>>8, sizeof(struct changer),
129                            0, 0, 0, 0, 0, 0 };
130
131         chp = kmalloc(sizeof(struct changer), M_ACD, M_WAITOK | M_ZERO);
132         if (!atapi_queue_cmd(cdp->device, ccb, (caddr_t)chp, 
133                              sizeof(struct changer),
134                              ATPR_F_READ, 60, NULL, NULL)) {
135             struct acd_softc *tmpcdp = cdp;
136             struct acd_softc **cdparr;
137             char *name;
138             int count;
139
140             chp->table_length = htons(chp->table_length);
141             cdparr = kmalloc(sizeof(struct acd_softc) * chp->slots,
142                                   M_ACD, M_WAITOK);
143             for (count = 0; count < chp->slots; count++) {
144                 if (count > 0) {
145                     tmpcdp = acd_init_lun(atadev);
146                     if (!tmpcdp) {
147                         ata_prtdev(atadev, "out of memory\n");
148                         break;
149                     }
150                 }
151                 cdparr[count] = tmpcdp;
152                 tmpcdp->driver = cdparr;
153                 tmpcdp->slot = count;
154                 tmpcdp->changer_info = chp;
155                 acd_make_dev(tmpcdp);
156                 devstat_add_entry(tmpcdp->stats, "acd", tmpcdp->lun, DEV_BSIZE,
157                                   DEVSTAT_NO_ORDERED_TAGS,
158                                   DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
159                                   DEVSTAT_PRIORITY_CD);
160             }
161             name = kmalloc(strlen(atadev->name) + 2, M_ACD, M_WAITOK);
162             strcpy(name, atadev->name);
163             strcat(name, "-");
164             ata_free_name(atadev);
165             ata_set_name(atadev, name, cdp->lun + cdp->changer_info->slots - 1);
166             kfree(name, M_ACD);
167         }
168     }
169     else {
170         acd_make_dev(cdp);
171         devstat_add_entry(cdp->stats, "acd", cdp->lun, DEV_BSIZE,
172                           DEVSTAT_NO_ORDERED_TAGS,
173                           DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
174                           DEVSTAT_PRIORITY_CD);
175     }
176     acd_describe(cdp);
177     atadev->driver = cdp;
178     return 1;
179 }
180
181 void
182 acddetach(struct ata_device *atadev)
183 {   
184     struct acd_softc *cdp = atadev->driver;
185     struct acd_devlist *entry;
186     struct bio *bio;
187     int subdev;
188     
189     if (cdp->changer_info) {
190         for (subdev = 0; subdev < cdp->changer_info->slots; subdev++) {
191             if (cdp->driver[subdev] == cdp)
192                 continue;
193             while ((bio = bioq_first(&cdp->driver[subdev]->bio_queue))) {
194                 bioq_remove(&cdp->driver[subdev]->bio_queue, bio);
195                 bio->bio_buf->b_flags |= B_ERROR;
196                 bio->bio_buf->b_error = ENXIO;
197                 biodone(bio);
198             }
199             release_dev(cdp->driver[subdev]->dev);
200             while ((entry = TAILQ_FIRST(&cdp->driver[subdev]->dev_list))) {
201                 release_dev(entry->dev);
202                 TAILQ_REMOVE(&cdp->driver[subdev]->dev_list, entry, chain);
203                 kfree(entry, M_ACD);
204             }
205             devstat_remove_entry(cdp->driver[subdev]->stats);
206             kfree(cdp->driver[subdev]->stats, M_ACD);
207             ata_free_lun(&acd_lun_map, cdp->driver[subdev]->lun);
208             kfree(cdp->driver[subdev], M_ACD);
209         }
210         kfree(cdp->driver, M_ACD);
211         kfree(cdp->changer_info, M_ACD);
212     }
213     while ((bio = bioq_first(&cdp->bio_queue))) {
214         bio->bio_buf->b_flags |= B_ERROR;
215         bio->bio_buf->b_error = ENXIO;
216         biodone(bio);
217     }
218     while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
219         release_dev(entry->dev);
220         TAILQ_REMOVE(&cdp->dev_list, entry, chain);
221         kfree(entry, M_ACD);
222     }
223     disk_invalidate(&cdp->disk);
224     disk_destroy(&cdp->disk);
225     devstat_remove_entry(cdp->stats);
226     kfree(cdp->stats, M_ACD);
227     ata_free_name(atadev);
228     ata_free_lun(&acd_lun_map, cdp->lun);
229     kfree(cdp, M_ACD);
230     atadev->driver = NULL;
231 }
232
233 static struct acd_softc *
234 acd_init_lun(struct ata_device *atadev)
235 {
236     struct acd_softc *cdp;
237
238     cdp = kmalloc(sizeof(struct acd_softc), M_ACD, M_WAITOK | M_ZERO);
239     TAILQ_INIT(&cdp->dev_list);
240     bioq_init(&cdp->bio_queue);
241     cdp->device = atadev;
242     cdp->lun = ata_get_lun(&acd_lun_map);
243     cdp->block_size = 2048;
244     cdp->slot = -1;
245     cdp->changer_info = NULL;
246     cdp->stats = kmalloc(sizeof(struct devstat), M_ACD, M_WAITOK | M_ZERO);
247     return cdp;
248 }
249
250 static void
251 acd_make_dev(struct acd_softc *cdp)
252 {
253     cdev_t dev;
254
255     dev = disk_create(cdp->lun, &cdp->disk, &acd_ops);
256     dev->si_drv1 = cdp;
257     cdp->dev = dev;
258     cdp->device->flags |= ATA_D_MEDIA_CHANGED;
259     acd_set_ioparm(cdp);
260 }
261
262 static void
263 acd_set_ioparm(struct acd_softc *cdp)
264 {
265     struct disk_info info;
266
267     cdp->dev->si_iosize_max = 
268                 ((256*DEV_BSIZE)/cdp->block_size)*cdp->block_size;
269     cdp->dev->si_bsize_phys = cdp->block_size;
270     bzero(&info, sizeof(info));
271     info.d_media_blksize = cdp->block_size;
272     info.d_media_blocks = cdp->disk_size;
273     info.d_secpertrack = 100;
274     info.d_nheads = 1;
275     info.d_ncylinders = cdp->disk_size / info.d_secpertrack / info.d_nheads + 1;
276     info.d_secpercyl = info.d_secpertrack * info.d_nheads;
277     info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL | DSO_COMPATPARTA;
278     disk_setdiskinfo(&cdp->disk, &info);
279 }
280
281 static void 
282 acd_describe(struct acd_softc *cdp)
283 {
284     int comma = 0;
285     char *mechanism;
286
287     if (bootverbose) {
288         ata_prtdev(cdp->device, "<%.40s/%.8s> %s drive at ata%d as %s\n",
289                    cdp->device->param->model, cdp->device->param->revision,
290                    (cdp->cap.write_dvdr) ? "DVD-R" : 
291                     (cdp->cap.write_dvdram) ? "DVD-RAM" : 
292                      (cdp->cap.write_cdrw) ? "CD-RW" :
293                       (cdp->cap.write_cdr) ? "CD-R" : 
294                        (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM",
295                    device_get_unit(cdp->device->channel->dev),
296                    (cdp->device->unit == ATA_MASTER) ? "master" : "slave");
297
298         ata_prtdev(cdp->device, "%s", "");
299         if (cdp->cap.cur_read_speed) {
300             kprintf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
301             if (cdp->cap.max_read_speed) 
302                 kprintf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
303             if ((cdp->cap.cur_write_speed) &&
304                 (cdp->cap.write_cdr || cdp->cap.write_cdrw || 
305                  cdp->cap.write_dvdr || cdp->cap.write_dvdram)) {
306                 kprintf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
307                 if (cdp->cap.max_write_speed)
308                     kprintf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
309             }
310             comma = 1;
311         }
312         if (cdp->cap.buf_size) {
313             kprintf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
314             comma = 1;
315         }
316         kprintf("%s %s\n", comma ? "," : "", ata_mode2str(cdp->device->mode));
317
318         ata_prtdev(cdp->device, "Reads:");
319         comma = 0;
320         if (cdp->cap.read_cdr) {
321             kprintf(" CD-R"); comma = 1;
322         }
323         if (cdp->cap.read_cdrw) {
324             kprintf("%s CD-RW", comma ? "," : ""); comma = 1;
325         }
326         if (cdp->cap.cd_da) {
327             if (cdp->cap.cd_da_stream)
328                 kprintf("%s CD-DA stream", comma ? "," : "");
329             else
330                 kprintf("%s CD-DA", comma ? "," : "");
331             comma = 1;
332         }
333         if (cdp->cap.read_dvdrom) {
334             kprintf("%s DVD-ROM", comma ? "," : ""); comma = 1;
335         }
336         if (cdp->cap.read_dvdr) {
337             kprintf("%s DVD-R", comma ? "," : ""); comma = 1;
338         }
339         if (cdp->cap.read_dvdram) {
340             kprintf("%s DVD-RAM", comma ? "," : ""); comma = 1;
341         }
342         if (cdp->cap.read_packet)
343             kprintf("%s packet", comma ? "," : "");
344
345         kprintf("\n");
346         ata_prtdev(cdp->device, "Writes:");
347         if (cdp->cap.write_cdr || cdp->cap.write_cdrw || 
348             cdp->cap.write_dvdr || cdp->cap.write_dvdram) {
349             comma = 0;
350             if (cdp->cap.write_cdr) {
351                 kprintf(" CD-R" ); comma = 1;
352             }
353             if (cdp->cap.write_cdrw) {
354                 kprintf("%s CD-RW", comma ? "," : ""); comma = 1;
355             }
356             if (cdp->cap.write_dvdr) {
357                 kprintf("%s DVD-R", comma ? "," : ""); comma = 1;
358             }
359             if (cdp->cap.write_dvdram) {
360                 kprintf("%s DVD-RAM", comma ? "," : ""); comma = 1; 
361             }
362             if (cdp->cap.test_write) {
363                 kprintf("%s test write", comma ? "," : ""); comma = 1;
364             }
365             if (cdp->cap.burnproof)
366                 kprintf("%s burnproof", comma ? "," : "");
367         }
368         kprintf("\n");
369         if (cdp->cap.audio_play) {
370             ata_prtdev(cdp->device, "Audio: ");
371             if (cdp->cap.audio_play)
372                 kprintf("play");
373             if (cdp->cap.max_vol_levels)
374                 kprintf(", %d volume levels", cdp->cap.max_vol_levels);
375             kprintf("\n");
376         }
377         ata_prtdev(cdp->device, "Mechanism: ");
378         switch (cdp->cap.mech) {
379         case MST_MECH_CADDY:
380             mechanism = "caddy"; break;
381         case MST_MECH_TRAY:
382             mechanism = "tray"; break;
383         case MST_MECH_POPUP:
384             mechanism = "popup"; break;
385         case MST_MECH_CHANGER:
386             mechanism = "changer"; break;
387         case MST_MECH_CARTRIDGE:
388             mechanism = "cartridge"; break;
389         default:
390             mechanism = 0; break;
391         }
392         if (mechanism)
393             kprintf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
394         else if (cdp->cap.eject)
395             kprintf("ejectable");
396
397         if (cdp->cap.lock)
398             kprintf(cdp->cap.locked ? ", locked" : ", unlocked");
399         if (cdp->cap.prevent)
400             kprintf(", lock protected");
401         kprintf("\n");
402
403         if (cdp->cap.mech != MST_MECH_CHANGER) {
404             ata_prtdev(cdp->device, "Medium: ");
405             switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
406             case MST_CDROM:
407                 kprintf("CD-ROM "); break;
408             case MST_CDR:
409                 kprintf("CD-R "); break;
410             case MST_CDRW:
411                 kprintf("CD-RW "); break;
412             case MST_DOOR_OPEN:
413                 kprintf("door open"); break;
414             case MST_NO_DISC:
415                 kprintf("no/blank disc"); break;
416             case MST_FMT_ERROR:
417                 kprintf("medium format error"); break;
418             }
419             if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
420                 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
421                 case MST_DATA_120:
422                     kprintf("120mm data disc"); break;
423                 case MST_AUDIO_120:
424                     kprintf("120mm audio disc"); break;
425                 case MST_COMB_120:
426                     kprintf("120mm data/audio disc"); break;
427                 case MST_PHOTO_120:
428                     kprintf("120mm photo disc"); break;
429                 case MST_DATA_80:
430                     kprintf("80mm data disc"); break;
431                 case MST_AUDIO_80:
432                     kprintf("80mm audio disc"); break;
433                 case MST_COMB_80:
434                     kprintf("80mm data/audio disc"); break;
435                 case MST_PHOTO_80:
436                     kprintf("80mm photo disc"); break;
437                 case MST_FMT_NONE:
438                     switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
439                     case MST_CDROM:
440                         kprintf("unknown"); break;
441                     case MST_CDR:
442                     case MST_CDRW:
443                         kprintf("blank"); break;
444                     }
445                     break;
446                 default:
447                     kprintf("unknown (0x%x)", cdp->cap.medium_type); break;
448                 }
449             }
450             kprintf("\n");
451         }
452     }
453     else {
454         ata_prtdev(cdp->device, "%s ",
455                    (cdp->cap.write_dvdr) ? "DVD-R" : 
456                     (cdp->cap.write_dvdram) ? "DVD-RAM" : 
457                      (cdp->cap.write_cdrw) ? "CD-RW" :
458                       (cdp->cap.write_cdr) ? "CD-R" : 
459                        (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM");
460
461         if (cdp->changer_info)
462             kprintf("with %d CD changer ", cdp->changer_info->slots);
463
464         kprintf("<%.40s> at ata%d-%s %s\n", cdp->device->param->model,
465                device_get_unit(cdp->device->channel->dev),
466                (cdp->device->unit == ATA_MASTER) ? "master" : "slave",
467                ata_mode2str(cdp->device->mode) );
468     }
469 }
470
471 static __inline void 
472 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
473 {
474     lba += 150;
475     lba &= 0xffffff;
476     *m = lba / (60 * 75);
477     lba %= (60 * 75);
478     *s = lba / 75;
479     *f = lba % 75;
480 }
481
482 static __inline u_int32_t 
483 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
484 {
485     return (m * 60 + s) * 75 + f - 150;
486 }
487
488 static int
489 acdopen(struct dev_open_args *ap)
490 {
491     cdev_t dev = ap->a_head.a_dev;
492     struct acd_softc *cdp = dev->si_drv1;
493     int timeout = 60;
494     
495     if (!cdp)
496         return ENXIO;
497
498     if (ap->a_oflags & FWRITE) {
499         if (count_dev(dev) > 1)
500             return EBUSY;
501     }
502
503     /* wait if drive is not finished loading the medium */
504     while (timeout--) {
505         struct atapi_reqsense *sense = cdp->device->result;
506
507         if (!atapi_test_ready(cdp->device))
508             break;
509         if (sense->sense_key == 2  && sense->asc == 4 && sense->ascq == 1)
510             tsleep(&timeout, 0, "acdld", hz / 2);
511         else
512             break;
513     }
514
515     if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
516         acd_select_slot(cdp);
517         tsleep(&cdp->changer_info, 0, "acdopn", 0);
518     }
519     acd_prevent_allow(cdp, 1);
520     cdp->flags |= F_LOCKED;
521     acd_read_toc(cdp);
522     return 0;
523 }
524
525 static int 
526 acdclose(struct dev_close_args *ap)
527 {
528     cdev_t dev = ap->a_head.a_dev;
529     struct acd_softc *cdp = dev->si_drv1;
530     
531     if (!cdp)
532         return ENXIO;
533
534     if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
535         acd_select_slot(cdp);
536         tsleep(&cdp->changer_info, 0, "acdclo", 0);
537     }
538     acd_prevent_allow(cdp, 0);
539     cdp->flags &= ~F_LOCKED;
540     return 0;
541 }
542
543 static int 
544 acdioctl(struct dev_ioctl_args *ap)
545 {
546     cdev_t dev = ap->a_head.a_dev;
547     struct acd_softc *cdp = dev->si_drv1;
548     int error = 0;
549
550     if (!cdp)
551         return ENXIO;
552
553     if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
554         acd_select_slot(cdp);
555         tsleep(&cdp->changer_info, 0, "acdctl", 0);
556     }
557     if (cdp->device->flags & ATA_D_MEDIA_CHANGED)
558         switch (ap->a_cmd) {
559         case CDIOCRESET:
560             atapi_test_ready(cdp->device);
561             break;
562            
563         default:
564             acd_read_toc(cdp);
565             acd_prevent_allow(cdp, 1);
566             cdp->flags |= F_LOCKED;
567             break;
568         }
569     switch (ap->a_cmd) {
570
571     case CDIOCRESUME:
572         error = acd_pause_resume(cdp, 1);
573         break;
574
575     case CDIOCPAUSE:
576         error = acd_pause_resume(cdp, 0);
577         break;
578
579     case CDIOCSTART:
580         error = acd_start_stop(cdp, 1);
581         break;
582
583     case CDIOCSTOP:
584         error = acd_start_stop(cdp, 0);
585         break;
586
587     case CDIOCALLOW:
588         error = acd_prevent_allow(cdp, 0);
589         cdp->flags &= ~F_LOCKED;
590         break;
591
592     case CDIOCPREVENT:
593         error = acd_prevent_allow(cdp, 1);
594         cdp->flags |= F_LOCKED;
595         break;
596
597     case CDIOCRESET:
598 ;       /* note: if no proc EPERM will be returned */
599         error = suser_cred(ap->a_cred, 0);
600         if (error)
601             break;
602         error = atapi_test_ready(cdp->device);
603         break;
604
605     case CDIOCEJECT:
606         if (count_dev(dev) > 1) {
607             error = EBUSY;
608             break;
609         }
610         error = acd_eject(cdp, 0);
611         break;
612
613     case CDIOCCLOSE:
614         if (count_dev(dev) > 1)
615             break;
616         error = acd_eject(cdp, 1);
617         break;
618
619     case CDIOREADTOCHEADER:
620         if (!cdp->toc.hdr.ending_track) {
621             error = EIO;
622             break;
623         }
624         bcopy(&cdp->toc.hdr, ap->a_data, sizeof(cdp->toc.hdr));
625         break;
626
627     case CDIOREADTOCENTRYS:
628         {
629             struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)ap->a_data;
630             struct toc *toc = &cdp->toc;
631             int starting_track = te->starting_track;
632             int len;
633
634             if (!toc->hdr.ending_track) {
635                 error = EIO;
636                 break;
637             }
638
639             if (te->data_len < sizeof(toc->tab[0]) || 
640                 (te->data_len % sizeof(toc->tab[0])) != 0 || 
641                 (te->address_format != CD_MSF_FORMAT &&
642                 te->address_format != CD_LBA_FORMAT)) {
643                 error = EINVAL;
644                 break;
645             }
646
647             if (!starting_track)
648                 starting_track = toc->hdr.starting_track;
649             else if (starting_track == 170) 
650                 starting_track = toc->hdr.ending_track + 1;
651             else if (starting_track < toc->hdr.starting_track ||
652                      starting_track > toc->hdr.ending_track + 1) {
653                 error = EINVAL;
654                 break;
655             }
656
657             len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
658                   sizeof(toc->tab[0]);
659             if (te->data_len < len)
660                 len = te->data_len;
661             if (len > sizeof(toc->tab)) {
662                 error = EINVAL;
663                 break;
664             }
665
666             if (te->address_format == CD_MSF_FORMAT) {
667                 struct cd_toc_entry *entry;
668
669                 toc = kmalloc(sizeof(struct toc), M_ACD, M_WAITOK | M_ZERO);
670                 bcopy(&cdp->toc, toc, sizeof(struct toc));
671                 entry = toc->tab + (toc->hdr.ending_track + 1 -
672                         toc->hdr.starting_track) + 1;
673                 while (--entry >= toc->tab)
674                     lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
675                             &entry->addr.msf.second, &entry->addr.msf.frame);
676             }
677             error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
678                             te->data, len);
679             if (te->address_format == CD_MSF_FORMAT)
680                 kfree(toc, M_ACD);
681             break;
682         }
683     case CDIOREADTOCENTRY:
684         {
685             struct ioc_read_toc_single_entry *te =
686                 (struct ioc_read_toc_single_entry *)ap->a_data;
687             struct toc *toc = &cdp->toc;
688             u_char track = te->track;
689
690             if (!toc->hdr.ending_track) {
691                 error = EIO;
692                 break;
693             }
694
695             if (te->address_format != CD_MSF_FORMAT && 
696                 te->address_format != CD_LBA_FORMAT) {
697                 error = EINVAL;
698                 break;
699             }
700
701             if (!track)
702                 track = toc->hdr.starting_track;
703             else if (track == 170)
704                 track = toc->hdr.ending_track + 1;
705             else if (track < toc->hdr.starting_track ||
706                      track > toc->hdr.ending_track + 1) {
707                 error = EINVAL;
708                 break;
709             }
710
711             if (te->address_format == CD_MSF_FORMAT) {
712                 struct cd_toc_entry *entry;
713
714                 toc = kmalloc(sizeof(struct toc), M_ACD, M_WAITOK | M_ZERO);
715                 bcopy(&cdp->toc, toc, sizeof(struct toc));
716
717                 entry = toc->tab + (track - toc->hdr.starting_track);
718                 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
719                         &entry->addr.msf.second, &entry->addr.msf.frame);
720             }
721             bcopy(toc->tab + track - toc->hdr.starting_track,
722                   &te->entry, sizeof(struct cd_toc_entry));
723             if (te->address_format == CD_MSF_FORMAT)
724                 kfree(toc, M_ACD);
725         }
726         break;
727
728     case CDIOCREADSUBCHANNEL:
729         {
730             struct ioc_read_subchannel *args =
731                 (struct ioc_read_subchannel *)ap->a_data;
732             u_int8_t format;
733             int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
734                                sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
735                                0, 0, 0, 0, 0, 0, 0 };
736
737             if (args->data_len > sizeof(struct cd_sub_channel_info) ||
738                 args->data_len < sizeof(struct cd_sub_channel_header)) {
739                 error = EINVAL;
740                 break;
741             }
742
743             format=args->data_format;
744             if ((format != CD_CURRENT_POSITION) &&
745                 (format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
746                 error = EINVAL;
747                 break;
748             }
749
750             ccb[1] = args->address_format & CD_MSF_FORMAT;
751
752             if ((error = atapi_queue_cmd(cdp->device,ccb,(caddr_t)&cdp->subchan,
753                                          sizeof(cdp->subchan), ATPR_F_READ, 10,
754                                          NULL, NULL)))
755                 break;
756
757             if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
758                 if (cdp->subchan.header.audio_status == 0x11) {
759                     error = EINVAL;
760                     break;
761                 }
762
763                 ccb[3] = format;
764                 if (format == CD_TRACK_INFO)
765                     ccb[6] = args->track;
766
767                 if ((error = atapi_queue_cmd(cdp->device, ccb,
768                                              (caddr_t)&cdp->subchan, 
769                                              sizeof(cdp->subchan), ATPR_F_READ,
770                                              10, NULL, NULL))) {
771                     break;
772                 }
773             }
774             error = copyout(&cdp->subchan, args->data, args->data_len);
775             break;
776         }
777
778     case CDIOCPLAYMSF:
779         {
780             struct ioc_play_msf *args = (struct ioc_play_msf *)ap->a_data;
781
782             error = 
783                 acd_play(cdp, 
784                          msf2lba(args->start_m, args->start_s, args->start_f),
785                          msf2lba(args->end_m, args->end_s, args->end_f));
786             break;
787         }
788
789     case CDIOCPLAYBLOCKS:
790         {
791             struct ioc_play_blocks *args = (struct ioc_play_blocks *)ap->a_data;
792
793             error = acd_play(cdp, args->blk, args->blk + args->len);
794             break;
795         }
796
797     case CDIOCPLAYTRACKS:
798         {
799             struct ioc_play_track *args = (struct ioc_play_track *)ap->a_data;
800             int t1, t2;
801
802             if (!cdp->toc.hdr.ending_track) {
803                 error = EIO;
804                 break;
805             }
806             if (args->end_track < cdp->toc.hdr.ending_track + 1)
807                 ++args->end_track;
808             if (args->end_track > cdp->toc.hdr.ending_track + 1)
809                 args->end_track = cdp->toc.hdr.ending_track + 1;
810             t1 = args->start_track - cdp->toc.hdr.starting_track;
811             t2 = args->end_track - cdp->toc.hdr.starting_track;
812             if (t1 < 0 || t2 < 0 ||
813                 t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
814                 error = EINVAL;
815                 break;
816             }
817             error = acd_play(cdp, ntohl(cdp->toc.tab[t1].addr.lba),
818                              ntohl(cdp->toc.tab[t2].addr.lba));
819             break;
820         }
821
822     case CDIOCREADAUDIO:
823         {
824             struct ioc_read_audio *args = (struct ioc_read_audio *)ap->a_data;
825             int32_t lba;
826             caddr_t buffer, ubuf = args->buffer;
827             int8_t ccb[16];
828             int frames;
829
830             if (!cdp->toc.hdr.ending_track) {
831                 error = EIO;
832                 break;
833             }
834                 
835             if ((frames = args->nframes) < 0) {
836                 error = EINVAL;
837                 break;
838             }
839
840             if (args->address_format == CD_LBA_FORMAT)
841                 lba = args->address.lba;
842             else if (args->address_format == CD_MSF_FORMAT)
843                 lba = msf2lba(args->address.msf.minute,
844                              args->address.msf.second,
845                              args->address.msf.frame);
846             else {
847                 error = EINVAL;
848                 break;
849             }
850
851 #ifndef CD_BUFFER_BLOCKS
852 #define CD_BUFFER_BLOCKS 13
853 #endif
854             if (!(buffer = kmalloc(CD_BUFFER_BLOCKS * 2352, M_ACD, M_WAITOK))){
855                 error = ENOMEM;
856                 break;
857             }
858             bzero(ccb, sizeof(ccb));
859             while (frames > 0) {
860                 int8_t blocks;
861                 int size;
862
863                 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
864                 size = blocks * 2352;
865
866                 ccb[0] = ATAPI_READ_CD;
867                 ccb[1] = 4;
868                 ccb[2] = lba>>24;
869                 ccb[3] = lba>>16;
870                 ccb[4] = lba>>8;
871                 ccb[5] = lba;
872                 ccb[8] = blocks;
873                 ccb[9] = 0xf0;
874                 if ((error = atapi_queue_cmd(cdp->device, ccb, buffer, size, 
875                                              ATPR_F_READ, 30, NULL,NULL)))
876                     break;
877
878                 if ((error = copyout(buffer, ubuf, size)))
879                     break;
880                     
881                 ubuf += size;
882                 frames -= blocks;
883                 lba += blocks;
884             }
885             kfree(buffer, M_ACD);
886             if (args->address_format == CD_LBA_FORMAT)
887                 args->address.lba = lba;
888             else if (args->address_format == CD_MSF_FORMAT)
889                 lba2msf(lba, &args->address.msf.minute,
890                              &args->address.msf.second,
891                              &args->address.msf.frame);
892             break;
893         }
894
895     case CDIOCGETVOL:
896         {
897             struct ioc_vol *arg = (struct ioc_vol *)ap->a_data;
898
899             if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
900                                         (caddr_t)&cdp->au, sizeof(cdp->au))))
901                 break;
902
903             if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
904                 error = EIO;
905                 break;
906             }
907             arg->vol[0] = cdp->au.port[0].volume;
908             arg->vol[1] = cdp->au.port[1].volume;
909             arg->vol[2] = cdp->au.port[2].volume;
910             arg->vol[3] = cdp->au.port[3].volume;
911             break;
912         }
913
914     case CDIOCSETVOL:
915         {
916             struct ioc_vol *arg = (struct ioc_vol *)ap->a_data;
917
918             if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
919                                         (caddr_t)&cdp->au, sizeof(cdp->au))))
920                 break;
921             if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
922                 error = EIO;
923                 break;
924             }
925             if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE_MASK,
926                                         (caddr_t)&cdp->aumask,
927                                         sizeof(cdp->aumask))))
928                 break;
929             cdp->au.data_length = 0;
930             cdp->au.port[0].channels = CHANNEL_0;
931             cdp->au.port[1].channels = CHANNEL_1;
932             cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
933             cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
934             cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
935             cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
936             error =  acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
937             break;
938         }
939     case CDIOCSETPATCH:
940         {
941             struct ioc_patch *arg = (struct ioc_patch *)ap->a_data;
942
943             error = acd_setchan(cdp, arg->patch[0], arg->patch[1],
944                                 arg->patch[2], arg->patch[3]);
945             break;
946         }
947
948     case CDIOCSETMONO:
949         error = acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
950         break;
951
952     case CDIOCSETSTEREO:
953         error = acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
954         break;
955
956     case CDIOCSETMUTE:
957         error = acd_setchan(cdp, 0, 0, 0, 0);
958         break;
959
960     case CDIOCSETLEFT:
961         error = acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
962         break;
963
964     case CDIOCSETRIGHT:
965         error = acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
966         break;
967
968     case CDRIOCBLANK:
969         error = acd_blank(cdp, (*(int *)ap->a_data));
970         break;
971
972     case CDRIOCNEXTWRITEABLEADDR:
973         {
974             struct acd_track_info track_info;
975
976             if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
977                 break;
978
979             if (!track_info.nwa_valid) {
980                 error = EINVAL;
981                 break;
982             }
983             *(int*)ap->a_data = track_info.next_writeable_addr;
984         }
985         break;
986  
987     case CDRIOCINITWRITER:
988         error = acd_init_writer(cdp, (*(int *)ap->a_data));
989         break;
990
991     case CDRIOCINITTRACK:
992         error = acd_init_track(cdp, (struct cdr_track *)ap->a_data);
993         break;
994
995     case CDRIOCFLUSH:
996         error = acd_flush(cdp);
997         break;
998
999     case CDRIOCFIXATE:
1000         error = acd_fixate(cdp, (*(int *)ap->a_data));
1001         break;
1002
1003     case CDRIOCREADSPEED:
1004         {
1005             int speed = *(int *)ap->a_data;
1006
1007             /* Preserve old behavior: units in multiples of CDROM speed */
1008             if (speed < 177)
1009                 speed *= 177;
1010             error = acd_set_speed(cdp, speed, CDR_MAX_SPEED);
1011         }
1012         break;
1013
1014     case CDRIOCWRITESPEED:
1015         {
1016             int speed = *(int *)ap->a_data;
1017
1018             if (speed < 177)
1019                 speed *= 177;
1020             error = acd_set_speed(cdp, CDR_MAX_SPEED, speed);
1021         }
1022         break;
1023
1024     case CDRIOCGETBLOCKSIZE:
1025         *(int *)ap->a_data = cdp->block_size;
1026         break;
1027
1028     case CDRIOCSETBLOCKSIZE:
1029         cdp->block_size = *(int *)ap->a_data;
1030         acd_set_ioparm(cdp);
1031         break;
1032
1033     case CDRIOCGETPROGRESS:
1034         error = acd_get_progress(cdp, (int *)ap->a_data);
1035         break;
1036
1037     case CDRIOCSENDCUE:
1038         error = acd_send_cue(cdp, (struct cdr_cuesheet *)ap->a_data);
1039         break;
1040
1041     case DVDIOCREPORTKEY:
1042         if (!cdp->cap.read_dvdrom)
1043             error = EINVAL;
1044         else
1045             error = acd_report_key(cdp, (struct dvd_authinfo *)ap->a_data);
1046         break;
1047
1048     case DVDIOCSENDKEY:
1049         if (!cdp->cap.read_dvdrom)
1050             error = EINVAL;
1051         else
1052             error = acd_send_key(cdp, (struct dvd_authinfo *)ap->a_data);
1053         break;
1054
1055     case DVDIOCREADSTRUCTURE:
1056         if (!cdp->cap.read_dvdrom)
1057             error = EINVAL;
1058         else
1059             error = acd_read_structure(cdp, (struct dvd_struct *)ap->a_data);
1060         break;
1061
1062     default:
1063         error = ENOTTY;
1064     }
1065     return error;
1066 }
1067
1068 static int 
1069 acdstrategy(struct dev_strategy_args *ap)
1070 {
1071     cdev_t dev = ap->a_head.a_dev;
1072     struct bio *bio = ap->a_bio;
1073     struct buf *bp = bio->bio_buf;
1074     struct acd_softc *cdp = dev->si_drv1;
1075
1076     if (cdp->device->flags & ATA_D_DETACHING) {
1077         bp->b_flags |= B_ERROR;
1078         bp->b_error = ENXIO;
1079         biodone(bio);
1080         return(0);
1081     }
1082
1083     /* if it's a null transfer, return immediatly. */
1084     if (bp->b_bcount == 0) {
1085         bp->b_resid = 0;
1086         biodone(bio);
1087         return(0);
1088     }
1089
1090     KKASSERT(bio->bio_offset != NOOFFSET);
1091     bio->bio_driver_info = dev;
1092     bp->b_resid = bp->b_bcount;
1093
1094     crit_enter();
1095     bioqdisksort(&cdp->bio_queue, bio);
1096     crit_exit();
1097     ata_start(cdp->device->channel);
1098     return(0);
1099 }
1100
1101 void 
1102 acd_start(struct ata_device *atadev)
1103 {
1104     struct acd_softc *cdp = atadev->driver;
1105     struct bio *bio = bioq_first(&cdp->bio_queue);
1106     struct buf *bp;
1107     cdev_t dev;
1108     u_int32_t lba, lastlba, count;
1109     int8_t ccb[16];
1110     int track, blocksize;
1111
1112     if (cdp->changer_info) {
1113         int i;
1114
1115         cdp = cdp->driver[cdp->changer_info->current_slot];
1116         bio = bioq_first(&cdp->bio_queue);
1117
1118         /* check for work pending on any other slot */
1119         for (i = 0; i < cdp->changer_info->slots; i++) {
1120             if (i == cdp->changer_info->current_slot)
1121                 continue;
1122             if (bioq_first(&(cdp->driver[i]->bio_queue))) {
1123                 if (bio == NULL || time_second > (cdp->timestamp + 10)) {
1124                     acd_select_slot(cdp->driver[i]);
1125                     return;
1126                 }
1127             }
1128         }
1129     }
1130     if (bio == NULL)
1131         return;
1132     bioq_remove(&cdp->bio_queue, bio);
1133     dev = bio->bio_driver_info;
1134     bp = bio->bio_buf;
1135
1136     /* reject all queued entries if media changed */
1137     if (cdp->device->flags & ATA_D_MEDIA_CHANGED) {
1138         bp->b_flags |= B_ERROR;
1139         bp->b_error = EIO;
1140         biodone(bio);
1141         return;
1142     }
1143
1144     bzero(ccb, sizeof(ccb));
1145
1146     track = (dev->si_uminor & 0x00ff0000) >> 16;
1147
1148     if (track) {
1149         blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
1150         lastlba = ntohl(cdp->toc.tab[track].addr.lba);
1151         lba = bio->bio_offset / blocksize;
1152         lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
1153     }
1154     else {
1155         blocksize = cdp->block_size;
1156         lastlba = cdp->disk_size;
1157         lba = bio->bio_offset / blocksize;
1158     }
1159
1160     if (bp->b_bcount % blocksize != 0) {
1161         bp->b_flags |= B_ERROR;
1162         bp->b_error = EINVAL;
1163         biodone(bio);
1164         return;
1165     }
1166     count = bp->b_bcount / blocksize;
1167
1168     if (bp->b_cmd == BUF_CMD_READ) {
1169         /* if transfer goes beyond range adjust it to be within limits */
1170         if (lba + count > lastlba) {
1171             /* if we are entirely beyond EOM return EOF */
1172             if (lastlba <= lba) {
1173                 bp->b_resid = bp->b_bcount;
1174                 biodone(bio);
1175                 return;
1176             }
1177             count = lastlba - lba;
1178         }
1179         switch (blocksize) {
1180         case 2048:
1181             ccb[0] = ATAPI_READ_BIG;
1182             break;
1183
1184         case 2352: 
1185             ccb[0] = ATAPI_READ_CD;
1186             ccb[9] = 0xf8;
1187             break;
1188
1189         default:
1190             ccb[0] = ATAPI_READ_CD;
1191             ccb[9] = 0x10;
1192         }
1193     }
1194     else 
1195         ccb[0] = ATAPI_WRITE_BIG;
1196     
1197     ccb[1] = 0;
1198     ccb[2] = lba>>24;
1199     ccb[3] = lba>>16;
1200     ccb[4] = lba>>8;
1201     ccb[5] = lba;
1202     ccb[6] = count>>16;
1203     ccb[7] = count>>8;
1204     ccb[8] = count;
1205
1206     devstat_start_transaction(cdp->stats);
1207     bio->bio_caller_info1.ptr = cdp;
1208     atapi_queue_cmd(cdp->device, ccb, bp->b_data, count * blocksize,
1209                     ((bp->b_cmd == BUF_CMD_READ) ? ATPR_F_READ : 0), 
1210                     (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30, acd_done, bio);
1211 }
1212
1213 static int 
1214 acd_done(struct atapi_request *request)
1215 {
1216     struct bio *bio = request->driver;
1217     struct buf *bp = bio->bio_buf;
1218     struct acd_softc *cdp = bio->bio_caller_info1.ptr;
1219     
1220     if (request->error) {
1221         bp->b_error = request->error;
1222         bp->b_flags |= B_ERROR;
1223     } else {
1224         bp->b_resid = bp->b_bcount - request->donecount;
1225     }
1226     devstat_end_transaction_buf(cdp->stats, bp);
1227     biodone(bio);
1228     return 0;
1229 }
1230
1231 static void 
1232 acd_read_toc(struct acd_softc *cdp)
1233 {
1234     struct acd_devlist *entry;
1235     int track, ntracks, len;
1236     u_int32_t sizes[2];
1237     int8_t ccb[16];
1238
1239     bzero(&cdp->toc, sizeof(cdp->toc));
1240     bzero(ccb, sizeof(ccb));
1241
1242     if (atapi_test_ready(cdp->device) != 0)
1243         return;
1244
1245     cdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
1246
1247     len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1248     ccb[0] = ATAPI_READ_TOC;
1249     ccb[7] = len>>8;
1250     ccb[8] = len;
1251     if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1252                         ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1253         bzero(&cdp->toc, sizeof(cdp->toc));
1254         return;
1255     }
1256     ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1257     if (ntracks <= 0 || ntracks > MAXTRK) {
1258         bzero(&cdp->toc, sizeof(cdp->toc));
1259         return;
1260     }
1261
1262     len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1263     bzero(ccb, sizeof(ccb));
1264     ccb[0] = ATAPI_READ_TOC;
1265     ccb[7] = len>>8;
1266     ccb[8] = len;
1267     if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1268                         ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1269         bzero(&cdp->toc, sizeof(cdp->toc));
1270         return;
1271     }
1272     cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1273
1274     cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1275     cdp->disk_size = 0;
1276     bzero(ccb, sizeof(ccb));
1277     ccb[0] = ATAPI_READ_CAPACITY;
1278     if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)sizes, sizeof(sizes),
1279                         ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1280         bzero(&cdp->toc, sizeof(cdp->toc));
1281         acd_set_ioparm(cdp);
1282         return;
1283     }
1284     cdp->disk_size = ntohl(sizes[0]) + 1;
1285     acd_set_ioparm(cdp);
1286
1287     while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
1288         destroy_dev(entry->dev);
1289         TAILQ_REMOVE(&cdp->dev_list, entry, chain);
1290         kfree(entry, M_ACD);
1291     }
1292     for (track = 1; track <= ntracks; track ++) {
1293         char name[16];
1294
1295         ksprintf(name, "acd%dt%d", cdp->lun, track);
1296         entry = kmalloc(sizeof(struct acd_devlist), M_ACD, M_WAITOK | M_ZERO);
1297         entry->dev = make_dev(&acd_ops, (cdp->lun << 3) | (track << 16),
1298                               0, 0, 0644, name, NULL);
1299         entry->dev->si_drv1 = cdp->dev->si_drv1;
1300         reference_dev(entry->dev);
1301         TAILQ_INSERT_TAIL(&cdp->dev_list, entry, chain);
1302     }
1303
1304 #ifdef ACD_DEBUG
1305     if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1306         ata_prtdev(cdp->device, "(%d sectors (%d bytes)), %d tracks ", 
1307                    cdp->disk_size, cdp->block_size,
1308                    cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1309         if (cdp->toc.tab[0].control & 4)
1310             kprintf("%dMB\n", cdp->disk_size / 512);
1311         else
1312             kprintf("%d:%d audio\n",
1313                    cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1314     }
1315 #endif
1316 }
1317
1318 static int
1319 acd_play(struct acd_softc *cdp, int start, int end)
1320 {
1321     int8_t ccb[16];
1322
1323     bzero(ccb, sizeof(ccb));
1324     ccb[0] = ATAPI_PLAY_MSF;
1325     lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1326     lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1327     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
1328 }
1329
1330 static int 
1331 acd_setchan(struct acd_softc *cdp,
1332             u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1333 {
1334     int error;
1335
1336     if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au, 
1337                                 sizeof(cdp->au))))
1338         return error;
1339     if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1340         return EIO;
1341     cdp->au.data_length = 0;
1342     cdp->au.port[0].channels = c0;
1343     cdp->au.port[1].channels = c1;
1344     cdp->au.port[2].channels = c2;
1345     cdp->au.port[3].channels = c3;
1346     return acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
1347 }
1348
1349 static int 
1350 acd_select_done1(struct atapi_request *request)
1351 {
1352     struct acd_softc *cdp = request->driver;
1353
1354     cdp->changer_info->current_slot = cdp->slot;
1355     cdp->driver[cdp->changer_info->current_slot]->timestamp = time_second;
1356     wakeup(&cdp->changer_info);
1357     return 0;
1358 }
1359
1360 static int 
1361 acd_select_done(struct atapi_request *request)
1362 {
1363     struct acd_softc *cdp = request->driver;
1364     int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, 0, 0, 0, 
1365                        cdp->slot, 0, 0, 0, 0, 0, 0, 0 };
1366
1367     /* load the wanted slot */
1368     atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30, 
1369                     acd_select_done1, cdp);
1370     return 0;
1371 }
1372
1373 static void 
1374 acd_select_slot(struct acd_softc *cdp)
1375 {
1376     int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, 0, 0, 0, 
1377                        cdp->changer_info->current_slot, 0, 0, 0, 0, 0, 0, 0 };
1378
1379     /* unload the current media from player */
1380     atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30, 
1381                     acd_select_done, cdp);
1382 }
1383
1384 static int
1385 acd_init_writer(struct acd_softc *cdp, int test_write)
1386 {
1387     int8_t ccb[16];
1388
1389     bzero(ccb, sizeof(ccb));
1390     ccb[0] = ATAPI_REZERO;
1391     atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60, NULL, NULL);
1392     ccb[0] = ATAPI_SEND_OPC_INFO;
1393     ccb[1] = 0x01;
1394     atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 30, NULL, NULL);
1395     return 0;
1396 }
1397
1398 static int
1399 acd_fixate(struct acd_softc *cdp, int multisession)
1400 {
1401     int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0, 
1402                        0, 0, 0, 0, 0, 0, 0, 0 };
1403     int timeout = 5*60*2;
1404     int error;
1405     struct write_param param;
1406
1407     if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1408                                 (caddr_t)&param, sizeof(param))))
1409         return error;
1410
1411     param.data_length = 0;
1412     if (multisession)
1413         param.session_type = CDR_SESS_MULTI;
1414     else
1415         param.session_type = CDR_SESS_NONE;
1416
1417     if ((error = acd_mode_select(cdp, (caddr_t)&param, param.page_length + 10)))
1418         return error;
1419   
1420     error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1421     if (error)
1422         return error;
1423
1424     /* some drives just return ready, wait for the expected fixate time */
1425     if ((error = atapi_test_ready(cdp->device)) != EBUSY) {
1426         timeout = timeout / (cdp->cap.cur_write_speed / 177);
1427         tsleep(&error, 0, "acdfix", timeout * hz / 2);
1428         return atapi_test_ready(cdp->device);
1429     }
1430
1431     while (timeout-- > 0) {
1432         if ((error = atapi_test_ready(cdp->device)) != EBUSY)
1433             return error;
1434         tsleep(&error, 0, "acdcld", hz/2);
1435     }
1436     return EIO;
1437 }
1438
1439 static int
1440 acd_init_track(struct acd_softc *cdp, struct cdr_track *track)
1441 {
1442     struct write_param param;
1443     int error;
1444
1445     if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1446                                 (caddr_t)&param, sizeof(param))))
1447         return error;
1448
1449     param.data_length = 0;
1450     param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1451     param.page_length = 0x32;
1452     param.test_write = track->test_write ? 1 : 0;
1453     param.write_type = CDR_WTYPE_TRACK;
1454     param.session_type = CDR_SESS_NONE;
1455     param.fp = 0;
1456     param.packet_size = 0;
1457
1458     if (cdp->cap.burnproof) 
1459         param.burnproof = 1;
1460
1461     switch (track->datablock_type) {
1462
1463     case CDR_DB_RAW:
1464         if (track->preemp)
1465             param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1466         else
1467             param.track_mode = CDR_TMODE_AUDIO;
1468         cdp->block_size = 2352;
1469         param.datablock_type = CDR_DB_RAW;
1470         param.session_format = CDR_SESS_CDROM;
1471         break;
1472
1473     case CDR_DB_ROM_MODE1:
1474         cdp->block_size = 2048;
1475         param.track_mode = CDR_TMODE_DATA;
1476         param.datablock_type = CDR_DB_ROM_MODE1;
1477         param.session_format = CDR_SESS_CDROM;
1478         break;
1479
1480     case CDR_DB_ROM_MODE2:
1481         cdp->block_size = 2336;
1482         param.track_mode = CDR_TMODE_DATA;
1483         param.datablock_type = CDR_DB_ROM_MODE2;
1484         param.session_format = CDR_SESS_CDROM;
1485         break;
1486
1487     case CDR_DB_XA_MODE1:
1488         cdp->block_size = 2048;
1489         param.track_mode = CDR_TMODE_DATA;
1490         param.datablock_type = CDR_DB_XA_MODE1;
1491         param.session_format = CDR_SESS_CDROM_XA;
1492         break;
1493
1494     case CDR_DB_XA_MODE2_F1:
1495         cdp->block_size = 2056;
1496         param.track_mode = CDR_TMODE_DATA;
1497         param.datablock_type = CDR_DB_XA_MODE2_F1;
1498         param.session_format = CDR_SESS_CDROM_XA;
1499         break;
1500
1501     case CDR_DB_XA_MODE2_F2:
1502         cdp->block_size = 2324;
1503         param.track_mode = CDR_TMODE_DATA;
1504         param.datablock_type = CDR_DB_XA_MODE2_F2;
1505         param.session_format = CDR_SESS_CDROM_XA;
1506         break;
1507
1508     case CDR_DB_XA_MODE2_MIX:
1509         cdp->block_size = 2332;
1510         param.track_mode = CDR_TMODE_DATA;
1511         param.datablock_type = CDR_DB_XA_MODE2_MIX;
1512         param.session_format = CDR_SESS_CDROM_XA;
1513         break;
1514     }
1515     acd_set_ioparm(cdp);
1516     return acd_mode_select(cdp, (caddr_t)&param, param.page_length + 10);
1517 }
1518
1519 static int
1520 acd_flush(struct acd_softc *cdp)
1521 {
1522     int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1523                        0, 0, 0, 0, 0, 0, 0, 0 };
1524
1525     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60,
1526                            NULL, NULL);
1527 }
1528
1529 static int
1530 acd_read_track_info(struct acd_softc *cdp,
1531                     int32_t lba, struct acd_track_info *info)
1532 {
1533     int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1534                      lba>>24, lba>>16, lba>>8, lba,
1535                      0,
1536                      sizeof(*info)>>8, sizeof(*info),
1537                      0, 0, 0, 0, 0, 0, 0 };
1538     int error;
1539
1540     if ((error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)info, sizeof(*info),
1541                                  ATPR_F_READ, 30, NULL, NULL)))
1542         return error;
1543     info->track_start_addr = ntohl(info->track_start_addr);
1544     info->next_writeable_addr = ntohl(info->next_writeable_addr);
1545     info->free_blocks = ntohl(info->free_blocks);
1546     info->fixed_packet_size = ntohl(info->fixed_packet_size);
1547     info->track_length = ntohl(info->track_length);
1548     return 0;
1549 }
1550
1551 static int
1552 acd_get_progress(struct acd_softc *cdp, int *finished)
1553 {
1554     int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,  
1555                        0, 0, 0, 0, 0, 0, 0, 0 };
1556     struct atapi_reqsense *sense = cdp->device->result;
1557     char tmp[8];
1558
1559     if (atapi_test_ready(cdp->device) != EBUSY) {
1560         if (atapi_queue_cmd(cdp->device, ccb, tmp, sizeof(tmp),
1561                             ATPR_F_READ, 30, NULL, NULL) != EBUSY) {
1562             *finished = 100;
1563             return 0;
1564         }
1565     }
1566     if (sense->sksv)
1567         *finished = 
1568             ((sense->sk_specific2 | (sense->sk_specific1 << 8)) * 100) / 65535;
1569     else
1570         *finished = 0;
1571     return 0;
1572 }
1573
1574 static int
1575 acd_send_cue(struct acd_softc *cdp, struct cdr_cuesheet *cuesheet)
1576 {
1577     struct write_param param;
1578     int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0, 
1579                        cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1580                        0, 0, 0, 0, 0, 0, 0 };
1581     int8_t *buffer;
1582     int32_t error;
1583 #ifdef ACD_DEBUG
1584     int i;
1585 #endif
1586
1587     if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1588                                 (caddr_t)&param, sizeof(param))))
1589         return error;
1590     param.data_length = 0;
1591     param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1592     param.page_length = 0x32;
1593     param.test_write = cuesheet->test_write ? 1 : 0;
1594     param.write_type = CDR_WTYPE_SESSION;
1595     param.session_type = cuesheet->session_type;
1596     param.fp = 0;
1597     param.packet_size = 0;
1598     param.track_mode = CDR_TMODE_AUDIO;
1599     param.datablock_type = CDR_DB_RAW;
1600     param.session_format = cuesheet->session_format;
1601     if (cdp->cap.burnproof) 
1602         param.burnproof = 1;
1603     if ((error = acd_mode_select(cdp, (caddr_t)&param, param.page_length + 10)))
1604         return error;
1605
1606     buffer = kmalloc(cuesheet->len, M_ACD, M_WAITOK);
1607     if (!buffer)
1608         return ENOMEM;
1609     if ((error = copyin(cuesheet->entries, buffer, cuesheet->len)))
1610         return error;
1611 #ifdef ACD_DEBUG
1612     kprintf("acd: cuesheet lenght = %d\n", cuesheet->len);
1613     for (i=0; i<cuesheet->len; i++)
1614         if (i%8)
1615             kprintf(" %02x", buffer[i]);
1616         else
1617             kprintf("\n%02x", buffer[i]);
1618     kprintf("\n");
1619 #endif
1620     error = atapi_queue_cmd(cdp->device, ccb, buffer, cuesheet->len, 0,
1621                             30, NULL, NULL);
1622     kfree(buffer, M_ACD);
1623     return error;
1624 }
1625
1626 static int
1627 acd_report_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1628 {
1629     struct dvd_miscauth *d;
1630     u_int32_t lba = 0;
1631     int16_t length;
1632     int8_t ccb[16];
1633     int error;
1634
1635     /* this is common even for ai->format == DVD_INVALIDATE_AGID */
1636     bzero(ccb, sizeof(ccb));
1637     ccb[0] = ATAPI_REPORT_KEY;
1638     ccb[2] = (lba >> 24) & 0xff;
1639     ccb[3] = (lba >> 16) & 0xff;
1640     ccb[4] = (lba >> 8) & 0xff;
1641     ccb[5] = lba & 0xff;
1642     ccb[10] = (ai->agid << 6) | ai->format;
1643
1644     switch (ai->format) {
1645     case DVD_REPORT_AGID:
1646     case DVD_REPORT_ASF:
1647     case DVD_REPORT_RPC:
1648         length = 8;
1649         break;
1650     case DVD_REPORT_KEY1:
1651         length = 12;
1652         break;
1653     case DVD_REPORT_TITLE_KEY:
1654         length = 12;
1655         lba = ai->lba;
1656         break;
1657     case DVD_REPORT_CHALLENGE:
1658         length = 16;
1659         break;
1660     case DVD_INVALIDATE_AGID:
1661         return(atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL));
1662     default:
1663         return EINVAL;
1664     }
1665
1666     ccb[8] = (length >> 8) & 0xff;
1667     ccb[9] = length & 0xff;
1668
1669     d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1670     d->length = htons(length - 2);
1671
1672     error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length,
1673                             ATPR_F_READ, 10, NULL, NULL);
1674     if (error) {
1675         kfree(d, M_ACD);
1676         return(error);
1677     }
1678
1679     switch (ai->format) {
1680     case DVD_REPORT_AGID:
1681         ai->agid = d->data[3] >> 6;
1682         break;
1683     
1684     case DVD_REPORT_CHALLENGE:
1685         bcopy(&d->data[0], &ai->keychal[0], 10);
1686         break;
1687     
1688     case DVD_REPORT_KEY1:
1689         bcopy(&d->data[0], &ai->keychal[0], 5);
1690         break;
1691     
1692     case DVD_REPORT_TITLE_KEY:
1693         ai->cpm = (d->data[0] >> 7);
1694         ai->cp_sec = (d->data[0] >> 6) & 0x1;
1695         ai->cgms = (d->data[0] >> 4) & 0x3;
1696         bcopy(&d->data[1], &ai->keychal[0], 5);
1697         break;
1698     
1699     case DVD_REPORT_ASF:
1700         ai->asf = d->data[3] & 1;
1701         break;
1702     
1703     case DVD_REPORT_RPC:
1704         ai->reg_type = (d->data[0] >> 6);
1705         ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1706         ai->user_rsts = d->data[0] & 0x7;
1707         ai->region = d->data[1];
1708         ai->rpc_scheme = d->data[2];
1709         break;
1710     
1711     case DVD_INVALIDATE_AGID:
1712         /* not reached */
1713         break;
1714
1715     default:
1716         error = EINVAL;
1717     }
1718     kfree(d, M_ACD);
1719     return error;
1720 }
1721
1722 static int
1723 acd_send_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1724 {
1725     struct dvd_miscauth *d;
1726     int16_t length;
1727     int8_t ccb[16];
1728     int error;
1729
1730     switch (ai->format) {
1731     case DVD_SEND_CHALLENGE:
1732         length = 16;
1733         d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1734         bcopy(ai->keychal, &d->data[0], 10);
1735         break;
1736
1737     case DVD_SEND_KEY2:
1738         length = 12;
1739         d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1740         bcopy(&ai->keychal[0], &d->data[0], 5);
1741         break;
1742     
1743     case DVD_SEND_RPC:
1744         length = 8;
1745         d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1746         d->data[0] = ai->region;
1747         break;
1748
1749     default:
1750         return EINVAL;
1751     }
1752
1753     bzero(ccb, sizeof(ccb));
1754     ccb[0] = ATAPI_SEND_KEY;
1755     ccb[8] = (length >> 8) & 0xff;
1756     ccb[9] = length & 0xff;
1757     ccb[10] = (ai->agid << 6) | ai->format;
1758     d->length = htons(length - 2);
1759     error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, 0,
1760                             10, NULL, NULL);
1761     kfree(d, M_ACD);
1762     return error;
1763 }
1764
1765 static int
1766 acd_read_structure(struct acd_softc *cdp, struct dvd_struct *s)
1767 {
1768     struct dvd_miscauth *d;
1769     u_int16_t length;
1770     int8_t ccb[16];
1771     int error = 0;
1772
1773     switch(s->format) {
1774     case DVD_STRUCT_PHYSICAL:
1775         length = 21;
1776         break;
1777
1778     case DVD_STRUCT_COPYRIGHT:
1779         length = 8;
1780         break;
1781
1782     case DVD_STRUCT_DISCKEY:
1783         length = 2052;
1784         break;
1785
1786     case DVD_STRUCT_BCA:
1787         length = 192;
1788         break;
1789
1790     case DVD_STRUCT_MANUFACT:
1791         length = 2052;
1792         break;
1793
1794     case DVD_STRUCT_DDS:
1795     case DVD_STRUCT_PRERECORDED:
1796     case DVD_STRUCT_UNIQUEID:
1797     case DVD_STRUCT_LIST:
1798     case DVD_STRUCT_CMI:
1799     case DVD_STRUCT_RMD_LAST:
1800     case DVD_STRUCT_RMD_RMA:
1801     case DVD_STRUCT_DCB:
1802         return ENOSYS;
1803
1804     default:
1805         return EINVAL;
1806     }
1807
1808     d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1809     d->length = htons(length - 2);
1810         
1811     bzero(ccb, sizeof(ccb));
1812     ccb[0] = ATAPI_READ_STRUCTURE;
1813     ccb[6] = s->layer_num;
1814     ccb[7] = s->format;
1815     ccb[8] = (length >> 8) & 0xff;
1816     ccb[9] = length & 0xff;
1817     ccb[10] = s->agid << 6;
1818     error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, ATPR_F_READ,
1819                             30, NULL, NULL);
1820     if (error) {
1821         kfree(d, M_ACD);
1822         return error;
1823     }
1824
1825     switch (s->format) {
1826     case DVD_STRUCT_PHYSICAL: {
1827         struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1828
1829         layer->book_type = d->data[0] >> 4;
1830         layer->book_version = d->data[0] & 0xf;
1831         layer->disc_size = d->data[1] >> 4;
1832         layer->max_rate = d->data[1] & 0xf;
1833         layer->nlayers = (d->data[2] >> 5) & 3;
1834         layer->track_path = (d->data[2] >> 4) & 1;
1835         layer->layer_type = d->data[2] & 0xf;
1836         layer->linear_density = d->data[3] >> 4;
1837         layer->track_density = d->data[3] & 0xf;
1838         layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1839         layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1840         layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1841         layer->bca = d->data[16] >> 7;
1842         break;
1843     }
1844
1845     case DVD_STRUCT_COPYRIGHT:
1846         s->cpst = d->data[0];
1847         s->rmi = d->data[0];
1848         break;
1849
1850     case DVD_STRUCT_DISCKEY:
1851         bcopy(&d->data[0], &s->data[0], 2048);
1852         break;
1853
1854     case DVD_STRUCT_BCA:
1855         s->length = ntohs(d->length);
1856         bcopy(&d->data[0], &s->data[0], s->length);
1857         break;
1858
1859     case DVD_STRUCT_MANUFACT:
1860         s->length = ntohs(d->length);
1861         bcopy(&d->data[0], &s->data[0], s->length);
1862         break;
1863                 
1864     default:
1865         error = EINVAL;
1866     }
1867     kfree(d, M_ACD);
1868     return error;
1869 }
1870
1871 static int 
1872 acd_eject(struct acd_softc *cdp, int close)
1873 {
1874     int error;
1875
1876     if ((error = acd_start_stop(cdp, 0)) == EBUSY) {
1877         if (!close)
1878             return 0;
1879         if ((error = acd_start_stop(cdp, 3)))
1880             return error;
1881         acd_read_toc(cdp);
1882         acd_prevent_allow(cdp, 1);
1883         cdp->flags |= F_LOCKED;
1884         return 0;
1885     }
1886     if (error)
1887         return error;
1888     if (close)
1889         return 0;
1890     acd_prevent_allow(cdp, 0);
1891     cdp->flags &= ~F_LOCKED;
1892     cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1893     return acd_start_stop(cdp, 2);
1894 }
1895
1896 static int
1897 acd_blank(struct acd_softc *cdp, int blanktype)
1898 {
1899     int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0, 
1900                        0, 0, 0, 0, 0, 0, 0, 0 };
1901
1902     cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1903     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1904 }
1905
1906 static int
1907 acd_prevent_allow(struct acd_softc *cdp, int lock)
1908 {
1909     int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1910                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1911
1912     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1913 }
1914
1915 static int
1916 acd_start_stop(struct acd_softc *cdp, int start)
1917 {
1918     int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1919                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1920
1921     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1922 }
1923
1924 static int
1925 acd_pause_resume(struct acd_softc *cdp, int pause)
1926 {
1927     int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1928                        0, 0, 0, 0, 0, 0, 0 };
1929
1930     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1931 }
1932
1933 static int
1934 acd_mode_sense(struct acd_softc *cdp, int page, caddr_t pagebuf, int pagesize)
1935 {
1936     int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1937                        pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1938     int error;
1939
1940     error = atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
1941                             10, NULL, NULL);
1942 #ifdef ACD_DEBUG
1943     atapi_dump("acd: mode sense ", pagebuf, pagesize);
1944 #endif
1945     return error;
1946 }
1947
1948 static int
1949 acd_mode_select(struct acd_softc *cdp, caddr_t pagebuf, int pagesize)
1950 {
1951     int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1952                      pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1953
1954 #ifdef ACD_DEBUG
1955     ata_prtdev(cdp->device,
1956                "modeselect pagesize=%d\n", pagesize);
1957     atapi_dump("mode select ", pagebuf, pagesize);
1958 #endif
1959     return atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, 0,
1960                            30, NULL, NULL);
1961 }
1962
1963 static int
1964 acd_set_speed(struct acd_softc *cdp, int rdspeed, int wrspeed)
1965 {
1966     int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed, 
1967                        wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1968     int error;
1969
1970     error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1971     if (!error)
1972         acd_get_cap(cdp);
1973     return error;
1974 }
1975
1976 static void
1977 acd_get_cap(struct acd_softc *cdp)
1978 {
1979     int retry = 5;
1980
1981     /* get drive capabilities, some drives needs this repeated */
1982     while (retry-- && acd_mode_sense(cdp, ATAPI_CDROM_CAP_PAGE,
1983                                      (caddr_t)&cdp->cap, sizeof(cdp->cap)))
1984
1985     cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
1986     cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
1987     cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
1988     cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed), 177);
1989     cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
1990     cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
1991 }