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