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