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