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