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