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