Merge from vendor branch GDB:
[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.22 2006/04/30 17:22:16 dillon Exp $
30  */
31
32 #include "opt_ata.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ata.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/proc.h>
39 #include <sys/buf.h>
40 #include <sys/bus.h>
41 #include <sys/disklabel.h>
42 #include <sys/devicestat.h>
43 #include <sys/cdio.h>
44 #include <sys/cdrio.h>
45 #include <sys/dvdio.h>
46 #include <sys/fcntl.h>
47 #include <sys/conf.h>
48 #include <sys/ctype.h>
49 #include <machine/bus.h>
50 #include <sys/buf2.h>
51 #include <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_offset != NOOFFSET);
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         lba = bio->bio_offset / blocksize;
1168         lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
1169     }
1170     else {
1171         blocksize = cdp->block_size;
1172         lastlba = cdp->disk_size;
1173         lba = bio->bio_offset / blocksize;
1174     }
1175
1176     if (bp->b_bcount % blocksize != 0) {
1177         bp->b_flags |= B_ERROR;
1178         bp->b_error = EINVAL;
1179         biodone(bio);
1180         return;
1181     }
1182     count = bp->b_bcount / blocksize;
1183
1184     if (bp->b_cmd == BUF_CMD_READ) {
1185         /* if transfer goes beyond range adjust it to be within limits */
1186         if (lba + count > lastlba) {
1187             /* if we are entirely beyond EOM return EOF */
1188             if (lastlba <= lba) {
1189                 bp->b_resid = bp->b_bcount;
1190                 biodone(bio);
1191                 return;
1192             }
1193             count = lastlba - lba;
1194         }
1195         switch (blocksize) {
1196         case 2048:
1197             ccb[0] = ATAPI_READ_BIG;
1198             break;
1199
1200         case 2352: 
1201             ccb[0] = ATAPI_READ_CD;
1202             ccb[9] = 0xf8;
1203             break;
1204
1205         default:
1206             ccb[0] = ATAPI_READ_CD;
1207             ccb[9] = 0x10;
1208         }
1209     }
1210     else 
1211         ccb[0] = ATAPI_WRITE_BIG;
1212     
1213     ccb[1] = 0;
1214     ccb[2] = lba>>24;
1215     ccb[3] = lba>>16;
1216     ccb[4] = lba>>8;
1217     ccb[5] = lba;
1218     ccb[6] = count>>16;
1219     ccb[7] = count>>8;
1220     ccb[8] = count;
1221
1222     devstat_start_transaction(cdp->stats);
1223     bio->bio_caller_info1.ptr = cdp;
1224     atapi_queue_cmd(cdp->device, ccb, bp->b_data, count * blocksize,
1225                     ((bp->b_cmd == BUF_CMD_READ) ? ATPR_F_READ : 0), 
1226                     (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30, acd_done, bio);
1227 }
1228
1229 static int 
1230 acd_done(struct atapi_request *request)
1231 {
1232     struct bio *bio = request->driver;
1233     struct buf *bp = bio->bio_buf;
1234     struct acd_softc *cdp = bio->bio_caller_info1.ptr;
1235     
1236     if (request->error) {
1237         bp->b_error = request->error;
1238         bp->b_flags |= B_ERROR;
1239     } else {
1240         bp->b_resid = bp->b_bcount - request->donecount;
1241     }
1242     devstat_end_transaction_buf(cdp->stats, bp);
1243     biodone(bio);
1244     return 0;
1245 }
1246
1247 static void 
1248 acd_read_toc(struct acd_softc *cdp)
1249 {
1250     struct acd_devlist *entry;
1251     int track, ntracks, len;
1252     u_int32_t sizes[2];
1253     int8_t ccb[16];
1254
1255     bzero(&cdp->toc, sizeof(cdp->toc));
1256     bzero(ccb, sizeof(ccb));
1257
1258     if (atapi_test_ready(cdp->device) != 0)
1259         return;
1260
1261     cdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
1262
1263     len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1264     ccb[0] = ATAPI_READ_TOC;
1265     ccb[7] = len>>8;
1266     ccb[8] = len;
1267     if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1268                         ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1269         bzero(&cdp->toc, sizeof(cdp->toc));
1270         return;
1271     }
1272     ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1273     if (ntracks <= 0 || ntracks > MAXTRK) {
1274         bzero(&cdp->toc, sizeof(cdp->toc));
1275         return;
1276     }
1277
1278     len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1279     bzero(ccb, sizeof(ccb));
1280     ccb[0] = ATAPI_READ_TOC;
1281     ccb[7] = len>>8;
1282     ccb[8] = len;
1283     if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1284                         ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1285         bzero(&cdp->toc, sizeof(cdp->toc));
1286         return;
1287     }
1288     cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1289
1290     cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1291     acd_set_ioparm(cdp);
1292     bzero(ccb, sizeof(ccb));
1293     ccb[0] = ATAPI_READ_CAPACITY;
1294     if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)sizes, sizeof(sizes),
1295                         ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1296         bzero(&cdp->toc, sizeof(cdp->toc));
1297         return;
1298     }
1299     cdp->disk_size = ntohl(sizes[0]) + 1;
1300
1301     bzero(&cdp->disklabel, sizeof(struct disklabel));
1302     strncpy(cdp->disklabel.d_typename, "               ", 
1303             sizeof(cdp->disklabel.d_typename));
1304     strncpy(cdp->disklabel.d_typename, cdp->device->name, 
1305             min(strlen(cdp->device->name),sizeof(cdp->disklabel.d_typename)-1));
1306     strncpy(cdp->disklabel.d_packname, "unknown        ", 
1307             sizeof(cdp->disklabel.d_packname));
1308     cdp->disklabel.d_secsize = cdp->block_size;
1309     cdp->disklabel.d_nsectors = 100;
1310     cdp->disklabel.d_ntracks = 1;
1311     cdp->disklabel.d_ncylinders = (cdp->disk_size / 100) + 1;
1312     cdp->disklabel.d_secpercyl = 100;
1313     cdp->disklabel.d_secperunit = cdp->disk_size;
1314     cdp->disklabel.d_rpm = 300;
1315     cdp->disklabel.d_interleave = 1;
1316     cdp->disklabel.d_flags = D_REMOVABLE;
1317     cdp->disklabel.d_npartitions = 1;
1318     cdp->disklabel.d_partitions[0].p_offset = 0;
1319     cdp->disklabel.d_partitions[0].p_size = cdp->disk_size;
1320     cdp->disklabel.d_partitions[0].p_fstype = FS_BSDFFS;
1321     cdp->disklabel.d_magic = DISKMAGIC;
1322     cdp->disklabel.d_magic2 = DISKMAGIC;
1323     cdp->disklabel.d_checksum = dkcksum(&cdp->disklabel);
1324
1325     while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
1326         destroy_dev(entry->dev);
1327         TAILQ_REMOVE(&cdp->dev_list, entry, chain);
1328         free(entry, M_ACD);
1329     }
1330     for (track = 1; track <= ntracks; track ++) {
1331         char name[16];
1332
1333         sprintf(name, "acd%dt%d", cdp->lun, track);
1334         entry = malloc(sizeof(struct acd_devlist), M_ACD, M_WAITOK | M_ZERO);
1335         entry->dev = make_dev(&acd_cdevsw, (cdp->lun << 3) | (track << 16),
1336                               0, 0, 0644, name, NULL);
1337         entry->dev->si_drv1 = cdp->dev->si_drv1;
1338         reference_dev(entry->dev);
1339         TAILQ_INSERT_TAIL(&cdp->dev_list, entry, chain);
1340     }
1341
1342 #ifdef ACD_DEBUG
1343     if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1344         ata_prtdev(cdp->device, "(%d sectors (%d bytes)), %d tracks ", 
1345                    cdp->disk_size, cdp->block_size,
1346                    cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1347         if (cdp->toc.tab[0].control & 4)
1348             printf("%dMB\n", cdp->disk_size / 512);
1349         else
1350             printf("%d:%d audio\n",
1351                    cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1352     }
1353 #endif
1354 }
1355
1356 static int
1357 acd_play(struct acd_softc *cdp, int start, int end)
1358 {
1359     int8_t ccb[16];
1360
1361     bzero(ccb, sizeof(ccb));
1362     ccb[0] = ATAPI_PLAY_MSF;
1363     lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1364     lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1365     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
1366 }
1367
1368 static int 
1369 acd_setchan(struct acd_softc *cdp,
1370             u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1371 {
1372     int error;
1373
1374     if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au, 
1375                                 sizeof(cdp->au))))
1376         return error;
1377     if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1378         return EIO;
1379     cdp->au.data_length = 0;
1380     cdp->au.port[0].channels = c0;
1381     cdp->au.port[1].channels = c1;
1382     cdp->au.port[2].channels = c2;
1383     cdp->au.port[3].channels = c3;
1384     return acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
1385 }
1386
1387 static int 
1388 acd_select_done1(struct atapi_request *request)
1389 {
1390     struct acd_softc *cdp = request->driver;
1391
1392     cdp->changer_info->current_slot = cdp->slot;
1393     cdp->driver[cdp->changer_info->current_slot]->timestamp = time_second;
1394     wakeup(&cdp->changer_info);
1395     return 0;
1396 }
1397
1398 static int 
1399 acd_select_done(struct atapi_request *request)
1400 {
1401     struct acd_softc *cdp = request->driver;
1402     int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, 0, 0, 0, 
1403                        cdp->slot, 0, 0, 0, 0, 0, 0, 0 };
1404
1405     /* load the wanted slot */
1406     atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30, 
1407                     acd_select_done1, cdp);
1408     return 0;
1409 }
1410
1411 static void 
1412 acd_select_slot(struct acd_softc *cdp)
1413 {
1414     int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, 0, 0, 0, 
1415                        cdp->changer_info->current_slot, 0, 0, 0, 0, 0, 0, 0 };
1416
1417     /* unload the current media from player */
1418     atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30, 
1419                     acd_select_done, cdp);
1420 }
1421
1422 static int
1423 acd_init_writer(struct acd_softc *cdp, int test_write)
1424 {
1425     int8_t ccb[16];
1426
1427     bzero(ccb, sizeof(ccb));
1428     ccb[0] = ATAPI_REZERO;
1429     atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60, NULL, NULL);
1430     ccb[0] = ATAPI_SEND_OPC_INFO;
1431     ccb[1] = 0x01;
1432     atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 30, NULL, NULL);
1433     return 0;
1434 }
1435
1436 static int
1437 acd_fixate(struct acd_softc *cdp, int multisession)
1438 {
1439     int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0, 
1440                        0, 0, 0, 0, 0, 0, 0, 0 };
1441     int timeout = 5*60*2;
1442     int error;
1443     struct write_param param;
1444
1445     if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1446                                 (caddr_t)&param, sizeof(param))))
1447         return error;
1448
1449     param.data_length = 0;
1450     if (multisession)
1451         param.session_type = CDR_SESS_MULTI;
1452     else
1453         param.session_type = CDR_SESS_NONE;
1454
1455     if ((error = acd_mode_select(cdp, (caddr_t)&param, param.page_length + 10)))
1456         return error;
1457   
1458     error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1459     if (error)
1460         return error;
1461
1462     /* some drives just return ready, wait for the expected fixate time */
1463     if ((error = atapi_test_ready(cdp->device)) != EBUSY) {
1464         timeout = timeout / (cdp->cap.cur_write_speed / 177);
1465         tsleep(&error, 0, "acdfix", timeout * hz / 2);
1466         return atapi_test_ready(cdp->device);
1467     }
1468
1469     while (timeout-- > 0) {
1470         if ((error = atapi_test_ready(cdp->device)) != EBUSY)
1471             return error;
1472         tsleep(&error, 0, "acdcld", hz/2);
1473     }
1474     return EIO;
1475 }
1476
1477 static int
1478 acd_init_track(struct acd_softc *cdp, struct cdr_track *track)
1479 {
1480     struct write_param param;
1481     int error;
1482
1483     if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1484                                 (caddr_t)&param, sizeof(param))))
1485         return error;
1486
1487     param.data_length = 0;
1488     param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1489     param.page_length = 0x32;
1490     param.test_write = track->test_write ? 1 : 0;
1491     param.write_type = CDR_WTYPE_TRACK;
1492     param.session_type = CDR_SESS_NONE;
1493     param.fp = 0;
1494     param.packet_size = 0;
1495
1496     if (cdp->cap.burnproof) 
1497         param.burnproof = 1;
1498
1499     switch (track->datablock_type) {
1500
1501     case CDR_DB_RAW:
1502         if (track->preemp)
1503             param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1504         else
1505             param.track_mode = CDR_TMODE_AUDIO;
1506         cdp->block_size = 2352;
1507         param.datablock_type = CDR_DB_RAW;
1508         param.session_format = CDR_SESS_CDROM;
1509         break;
1510
1511     case CDR_DB_ROM_MODE1:
1512         cdp->block_size = 2048;
1513         param.track_mode = CDR_TMODE_DATA;
1514         param.datablock_type = CDR_DB_ROM_MODE1;
1515         param.session_format = CDR_SESS_CDROM;
1516         break;
1517
1518     case CDR_DB_ROM_MODE2:
1519         cdp->block_size = 2336;
1520         param.track_mode = CDR_TMODE_DATA;
1521         param.datablock_type = CDR_DB_ROM_MODE2;
1522         param.session_format = CDR_SESS_CDROM;
1523         break;
1524
1525     case CDR_DB_XA_MODE1:
1526         cdp->block_size = 2048;
1527         param.track_mode = CDR_TMODE_DATA;
1528         param.datablock_type = CDR_DB_XA_MODE1;
1529         param.session_format = CDR_SESS_CDROM_XA;
1530         break;
1531
1532     case CDR_DB_XA_MODE2_F1:
1533         cdp->block_size = 2056;
1534         param.track_mode = CDR_TMODE_DATA;
1535         param.datablock_type = CDR_DB_XA_MODE2_F1;
1536         param.session_format = CDR_SESS_CDROM_XA;
1537         break;
1538
1539     case CDR_DB_XA_MODE2_F2:
1540         cdp->block_size = 2324;
1541         param.track_mode = CDR_TMODE_DATA;
1542         param.datablock_type = CDR_DB_XA_MODE2_F2;
1543         param.session_format = CDR_SESS_CDROM_XA;
1544         break;
1545
1546     case CDR_DB_XA_MODE2_MIX:
1547         cdp->block_size = 2332;
1548         param.track_mode = CDR_TMODE_DATA;
1549         param.datablock_type = CDR_DB_XA_MODE2_MIX;
1550         param.session_format = CDR_SESS_CDROM_XA;
1551         break;
1552     }
1553     acd_set_ioparm(cdp);
1554     return acd_mode_select(cdp, (caddr_t)&param, param.page_length + 10);
1555 }
1556
1557 static int
1558 acd_flush(struct acd_softc *cdp)
1559 {
1560     int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1561                        0, 0, 0, 0, 0, 0, 0, 0 };
1562
1563     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60,
1564                            NULL, NULL);
1565 }
1566
1567 static int
1568 acd_read_track_info(struct acd_softc *cdp,
1569                     int32_t lba, struct acd_track_info *info)
1570 {
1571     int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1572                      lba>>24, lba>>16, lba>>8, lba,
1573                      0,
1574                      sizeof(*info)>>8, sizeof(*info),
1575                      0, 0, 0, 0, 0, 0, 0 };
1576     int error;
1577
1578     if ((error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)info, sizeof(*info),
1579                                  ATPR_F_READ, 30, NULL, NULL)))
1580         return error;
1581     info->track_start_addr = ntohl(info->track_start_addr);
1582     info->next_writeable_addr = ntohl(info->next_writeable_addr);
1583     info->free_blocks = ntohl(info->free_blocks);
1584     info->fixed_packet_size = ntohl(info->fixed_packet_size);
1585     info->track_length = ntohl(info->track_length);
1586     return 0;
1587 }
1588
1589 static int
1590 acd_get_progress(struct acd_softc *cdp, int *finished)
1591 {
1592     int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,  
1593                        0, 0, 0, 0, 0, 0, 0, 0 };
1594     struct atapi_reqsense *sense = cdp->device->result;
1595     char tmp[8];
1596
1597     if (atapi_test_ready(cdp->device) != EBUSY) {
1598         if (atapi_queue_cmd(cdp->device, ccb, tmp, sizeof(tmp),
1599                             ATPR_F_READ, 30, NULL, NULL) != EBUSY) {
1600             *finished = 100;
1601             return 0;
1602         }
1603     }
1604     if (sense->sksv)
1605         *finished = 
1606             ((sense->sk_specific2 | (sense->sk_specific1 << 8)) * 100) / 65535;
1607     else
1608         *finished = 0;
1609     return 0;
1610 }
1611
1612 static int
1613 acd_send_cue(struct acd_softc *cdp, struct cdr_cuesheet *cuesheet)
1614 {
1615     struct write_param param;
1616     int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0, 
1617                        cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1618                        0, 0, 0, 0, 0, 0, 0 };
1619     int8_t *buffer;
1620     int32_t error;
1621 #ifdef ACD_DEBUG
1622     int i;
1623 #endif
1624
1625     if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1626                                 (caddr_t)&param, sizeof(param))))
1627         return error;
1628     param.data_length = 0;
1629     param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1630     param.page_length = 0x32;
1631     param.test_write = cuesheet->test_write ? 1 : 0;
1632     param.write_type = CDR_WTYPE_SESSION;
1633     param.session_type = cuesheet->session_type;
1634     param.fp = 0;
1635     param.packet_size = 0;
1636     param.track_mode = CDR_TMODE_AUDIO;
1637     param.datablock_type = CDR_DB_RAW;
1638     param.session_format = cuesheet->session_format;
1639     if (cdp->cap.burnproof) 
1640         param.burnproof = 1;
1641     if ((error = acd_mode_select(cdp, (caddr_t)&param, param.page_length + 10)))
1642         return error;
1643
1644     buffer = malloc(cuesheet->len, M_ACD, M_WAITOK);
1645     if (!buffer)
1646         return ENOMEM;
1647     if ((error = copyin(cuesheet->entries, buffer, cuesheet->len)))
1648         return error;
1649 #ifdef ACD_DEBUG
1650     printf("acd: cuesheet lenght = %d\n", cuesheet->len);
1651     for (i=0; i<cuesheet->len; i++)
1652         if (i%8)
1653             printf(" %02x", buffer[i]);
1654         else
1655             printf("\n%02x", buffer[i]);
1656     printf("\n");
1657 #endif
1658     error = atapi_queue_cmd(cdp->device, ccb, buffer, cuesheet->len, 0,
1659                             30, NULL, NULL);
1660     free(buffer, M_ACD);
1661     return error;
1662 }
1663
1664 static int
1665 acd_report_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1666 {
1667     struct dvd_miscauth *d;
1668     u_int32_t lba = 0;
1669     int16_t length;
1670     int8_t ccb[16];
1671     int error;
1672
1673     /* this is common even for ai->format == DVD_INVALIDATE_AGID */
1674     bzero(ccb, sizeof(ccb));
1675     ccb[0] = ATAPI_REPORT_KEY;
1676     ccb[2] = (lba >> 24) & 0xff;
1677     ccb[3] = (lba >> 16) & 0xff;
1678     ccb[4] = (lba >> 8) & 0xff;
1679     ccb[5] = lba & 0xff;
1680     ccb[10] = (ai->agid << 6) | ai->format;
1681
1682     switch (ai->format) {
1683     case DVD_REPORT_AGID:
1684     case DVD_REPORT_ASF:
1685     case DVD_REPORT_RPC:
1686         length = 8;
1687         break;
1688     case DVD_REPORT_KEY1:
1689         length = 12;
1690         break;
1691     case DVD_REPORT_TITLE_KEY:
1692         length = 12;
1693         lba = ai->lba;
1694         break;
1695     case DVD_REPORT_CHALLENGE:
1696         length = 16;
1697         break;
1698     case DVD_INVALIDATE_AGID:
1699         return(atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL));
1700     default:
1701         return EINVAL;
1702     }
1703
1704     ccb[8] = (length >> 8) & 0xff;
1705     ccb[9] = length & 0xff;
1706
1707     d = malloc(length, M_ACD, M_WAITOK | M_ZERO);
1708     d->length = htons(length - 2);
1709
1710     error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length,
1711                             ATPR_F_READ, 10, NULL, NULL);
1712     if (error) {
1713         free(d, M_ACD);
1714         return(error);
1715     }
1716
1717     switch (ai->format) {
1718     case DVD_REPORT_AGID:
1719         ai->agid = d->data[3] >> 6;
1720         break;
1721     
1722     case DVD_REPORT_CHALLENGE:
1723         bcopy(&d->data[0], &ai->keychal[0], 10);
1724         break;
1725     
1726     case DVD_REPORT_KEY1:
1727         bcopy(&d->data[0], &ai->keychal[0], 5);
1728         break;
1729     
1730     case DVD_REPORT_TITLE_KEY:
1731         ai->cpm = (d->data[0] >> 7);
1732         ai->cp_sec = (d->data[0] >> 6) & 0x1;
1733         ai->cgms = (d->data[0] >> 4) & 0x3;
1734         bcopy(&d->data[1], &ai->keychal[0], 5);
1735         break;
1736     
1737     case DVD_REPORT_ASF:
1738         ai->asf = d->data[3] & 1;
1739         break;
1740     
1741     case DVD_REPORT_RPC:
1742         ai->reg_type = (d->data[0] >> 6);
1743         ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1744         ai->user_rsts = d->data[0] & 0x7;
1745         ai->region = d->data[1];
1746         ai->rpc_scheme = d->data[2];
1747         break;
1748     
1749     case DVD_INVALIDATE_AGID:
1750         /* not reached */
1751         break;
1752
1753     default:
1754         error = EINVAL;
1755     }
1756     free(d, M_ACD);
1757     return error;
1758 }
1759
1760 static int
1761 acd_send_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1762 {
1763     struct dvd_miscauth *d;
1764     int16_t length;
1765     int8_t ccb[16];
1766     int error;
1767
1768     switch (ai->format) {
1769     case DVD_SEND_CHALLENGE:
1770         length = 16;
1771         d = malloc(length, M_ACD, M_WAITOK | M_ZERO);
1772         bcopy(ai->keychal, &d->data[0], 10);
1773         break;
1774
1775     case DVD_SEND_KEY2:
1776         length = 12;
1777         d = malloc(length, M_ACD, M_WAITOK | M_ZERO);
1778         bcopy(&ai->keychal[0], &d->data[0], 5);
1779         break;
1780     
1781     case DVD_SEND_RPC:
1782         length = 8;
1783         d = malloc(length, M_ACD, M_WAITOK | M_ZERO);
1784         d->data[0] = ai->region;
1785         break;
1786
1787     default:
1788         return EINVAL;
1789     }
1790
1791     bzero(ccb, sizeof(ccb));
1792     ccb[0] = ATAPI_SEND_KEY;
1793     ccb[8] = (length >> 8) & 0xff;
1794     ccb[9] = length & 0xff;
1795     ccb[10] = (ai->agid << 6) | ai->format;
1796     d->length = htons(length - 2);
1797     error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, 0,
1798                             10, NULL, NULL);
1799     free(d, M_ACD);
1800     return error;
1801 }
1802
1803 static int
1804 acd_read_structure(struct acd_softc *cdp, struct dvd_struct *s)
1805 {
1806     struct dvd_miscauth *d;
1807     u_int16_t length;
1808     int8_t ccb[16];
1809     int error = 0;
1810
1811     switch(s->format) {
1812     case DVD_STRUCT_PHYSICAL:
1813         length = 21;
1814         break;
1815
1816     case DVD_STRUCT_COPYRIGHT:
1817         length = 8;
1818         break;
1819
1820     case DVD_STRUCT_DISCKEY:
1821         length = 2052;
1822         break;
1823
1824     case DVD_STRUCT_BCA:
1825         length = 192;
1826         break;
1827
1828     case DVD_STRUCT_MANUFACT:
1829         length = 2052;
1830         break;
1831
1832     case DVD_STRUCT_DDS:
1833     case DVD_STRUCT_PRERECORDED:
1834     case DVD_STRUCT_UNIQUEID:
1835     case DVD_STRUCT_LIST:
1836     case DVD_STRUCT_CMI:
1837     case DVD_STRUCT_RMD_LAST:
1838     case DVD_STRUCT_RMD_RMA:
1839     case DVD_STRUCT_DCB:
1840         return ENOSYS;
1841
1842     default:
1843         return EINVAL;
1844     }
1845
1846     d = malloc(length, M_ACD, M_WAITOK | M_ZERO);
1847     d->length = htons(length - 2);
1848         
1849     bzero(ccb, sizeof(ccb));
1850     ccb[0] = ATAPI_READ_STRUCTURE;
1851     ccb[6] = s->layer_num;
1852     ccb[7] = s->format;
1853     ccb[8] = (length >> 8) & 0xff;
1854     ccb[9] = length & 0xff;
1855     ccb[10] = s->agid << 6;
1856     error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, ATPR_F_READ,
1857                             30, NULL, NULL);
1858     if (error) {
1859         free(d, M_ACD);
1860         return error;
1861     }
1862
1863     switch (s->format) {
1864     case DVD_STRUCT_PHYSICAL: {
1865         struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1866
1867         layer->book_type = d->data[0] >> 4;
1868         layer->book_version = d->data[0] & 0xf;
1869         layer->disc_size = d->data[1] >> 4;
1870         layer->max_rate = d->data[1] & 0xf;
1871         layer->nlayers = (d->data[2] >> 5) & 3;
1872         layer->track_path = (d->data[2] >> 4) & 1;
1873         layer->layer_type = d->data[2] & 0xf;
1874         layer->linear_density = d->data[3] >> 4;
1875         layer->track_density = d->data[3] & 0xf;
1876         layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1877         layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1878         layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1879         layer->bca = d->data[16] >> 7;
1880         break;
1881     }
1882
1883     case DVD_STRUCT_COPYRIGHT:
1884         s->cpst = d->data[0];
1885         s->rmi = d->data[0];
1886         break;
1887
1888     case DVD_STRUCT_DISCKEY:
1889         bcopy(&d->data[0], &s->data[0], 2048);
1890         break;
1891
1892     case DVD_STRUCT_BCA:
1893         s->length = ntohs(d->length);
1894         bcopy(&d->data[0], &s->data[0], s->length);
1895         break;
1896
1897     case DVD_STRUCT_MANUFACT:
1898         s->length = ntohs(d->length);
1899         bcopy(&d->data[0], &s->data[0], s->length);
1900         break;
1901                 
1902     default:
1903         error = EINVAL;
1904     }
1905     free(d, M_ACD);
1906     return error;
1907 }
1908
1909 static int 
1910 acd_eject(struct acd_softc *cdp, int close)
1911 {
1912     int error;
1913
1914     if ((error = acd_start_stop(cdp, 0)) == EBUSY) {
1915         if (!close)
1916             return 0;
1917         if ((error = acd_start_stop(cdp, 3)))
1918             return error;
1919         acd_read_toc(cdp);
1920         acd_prevent_allow(cdp, 1);
1921         cdp->flags |= F_LOCKED;
1922         return 0;
1923     }
1924     if (error)
1925         return error;
1926     if (close)
1927         return 0;
1928     acd_prevent_allow(cdp, 0);
1929     cdp->flags &= ~F_LOCKED;
1930     cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1931     return acd_start_stop(cdp, 2);
1932 }
1933
1934 static int
1935 acd_blank(struct acd_softc *cdp, int blanktype)
1936 {
1937     int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0, 
1938                        0, 0, 0, 0, 0, 0, 0, 0 };
1939
1940     cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1941     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1942 }
1943
1944 static int
1945 acd_prevent_allow(struct acd_softc *cdp, int lock)
1946 {
1947     int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1948                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1949
1950     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1951 }
1952
1953 static int
1954 acd_start_stop(struct acd_softc *cdp, int start)
1955 {
1956     int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1957                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1958
1959     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1960 }
1961
1962 static int
1963 acd_pause_resume(struct acd_softc *cdp, int pause)
1964 {
1965     int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1966                        0, 0, 0, 0, 0, 0, 0 };
1967
1968     return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1969 }
1970
1971 static int
1972 acd_mode_sense(struct acd_softc *cdp, int page, caddr_t pagebuf, int pagesize)
1973 {
1974     int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1975                        pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1976     int error;
1977
1978     error = atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
1979                             10, NULL, NULL);
1980 #ifdef ACD_DEBUG
1981     atapi_dump("acd: mode sense ", pagebuf, pagesize);
1982 #endif
1983     return error;
1984 }
1985
1986 static int
1987 acd_mode_select(struct acd_softc *cdp, caddr_t pagebuf, int pagesize)
1988 {
1989     int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1990                      pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1991
1992 #ifdef ACD_DEBUG
1993     ata_prtdev(cdp->device,
1994                "modeselect pagesize=%d\n", pagesize);
1995     atapi_dump("mode select ", pagebuf, pagesize);
1996 #endif
1997     return atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, 0,
1998                            30, NULL, NULL);
1999 }
2000
2001 static int
2002 acd_set_speed(struct acd_softc *cdp, int rdspeed, int wrspeed)
2003 {
2004     int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed, 
2005                        wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2006     int error;
2007
2008     error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
2009     if (!error)
2010         acd_get_cap(cdp);
2011     return error;
2012 }
2013
2014 static void
2015 acd_get_cap(struct acd_softc *cdp)
2016 {
2017     int retry = 5;
2018
2019     /* get drive capabilities, some drives needs this repeated */
2020     while (retry-- && acd_mode_sense(cdp, ATAPI_CDROM_CAP_PAGE,
2021                                      (caddr_t)&cdp->cap, sizeof(cdp->cap)))
2022
2023     cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
2024     cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
2025     cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
2026     cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed), 177);
2027     cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
2028     cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
2029 }