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