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