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