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