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