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