mcd.c revision 1.111 1 /* $NetBSD: mcd.c,v 1.111 2014/03/16 05:20:28 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles M. Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * Copyright 1993 by Holger Veit (data part)
21 * Copyright 1993 by Brian Moore (audio part)
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This software was developed by Holger Veit and Brian Moore
35 * for use with "386BSD" and similar operating systems.
36 * "Similar operating systems" includes mainly non-profit oriented
37 * systems for research and education, including but not restricted to
38 * "NetBSD", "FreeBSD", "Mach" (by CMU).
39 * 4. Neither the name of the developer(s) nor the name "386BSD"
40 * may be used to endorse or promote products derived from this
41 * software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
44 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
47 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
48 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
49 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
51 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56 /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
57
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: mcd.c,v 1.111 2014/03/16 05:20:28 dholland Exp $");
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/callout.h>
64 #include <sys/kernel.h>
65 #include <sys/proc.h>
66 #include <sys/conf.h>
67 #include <sys/file.h>
68 #include <sys/buf.h>
69 #include <sys/bufq.h>
70 #include <sys/stat.h>
71 #include <sys/uio.h>
72 #include <sys/ioctl.h>
73 #include <sys/cdio.h>
74 #include <sys/errno.h>
75 #include <sys/disklabel.h>
76 #include <sys/device.h>
77 #include <sys/disk.h>
78 #include <sys/mutex.h>
79 #include <sys/cpu.h>
80 #include <sys/intr.h>
81 #include <sys/bus.h>
82
83 #include <dev/isa/isavar.h>
84 #include <dev/isa/mcdreg.h>
85
86 #ifndef MCDDEBUG
87 #define MCD_TRACE(fmt,...)
88 #else
89 #define MCD_TRACE(fmt,...) {if (sc->debug) {printf("%s: st=%02x: ", device_xname(sc->sc_dev), sc->status); printf(fmt,__VA_ARGS__);}}
90 #endif
91
92 #define MCDPART(dev) DISKPART(dev)
93 #define MCDUNIT(dev) DISKUNIT(dev)
94
95 /* toc */
96 #define MCD_MAXTOCS 104 /* from the Linux driver */
97
98 /* control promiscuous match */
99 #include "opt_mcd_promisc.h"
100
101 #ifdef MCD_PROMISC
102 int mcd_promisc = 1;
103 #else
104 int mcd_promisc = 0;
105 #endif
106
107 struct mcd_mbx {
108 int retry, count;
109 struct buf *bp;
110 daddr_t blkno;
111 int nblk;
112 int sz;
113 u_long skip;
114 int state;
115 #define MCD_S_IDLE 0
116 #define MCD_S_BEGIN 1
117 #define MCD_S_WAITMODE 2
118 #define MCD_S_WAITREAD 3
119 int mode;
120 };
121
122 struct mcd_softc {
123 device_t sc_dev;
124 struct disk sc_dk;
125 kmutex_t sc_lock;
126 void *sc_ih;
127
128 callout_t sc_pintr_ch;
129
130 bus_space_tag_t sc_iot;
131 bus_space_handle_t sc_ioh;
132
133 int irq, drq;
134
135 const char *type;
136 int flags;
137 #define MCDF_WLABEL 0x04 /* label is writable */
138 #define MCDF_LABELLING 0x08 /* writing label */
139 #define MCDF_LOADED 0x10 /* parameters loaded */
140 short status;
141 short audio_status;
142 int blksize;
143 u_long disksize;
144 struct mcd_volinfo volinfo;
145 union mcd_qchninfo toc[MCD_MAXTOCS];
146 struct mcd_command lastpb;
147 struct mcd_mbx mbx;
148 int lastmode;
149 #define MCD_MD_UNKNOWN -1
150 int lastupc;
151 #define MCD_UPC_UNKNOWN -1
152 struct bufq_state *buf_queue;
153 int active;
154 u_char readcmd;
155 u_char debug;
156 u_char probe;
157 };
158
159 static int bcd2bin(bcd_t);
160 static bcd_t bin2bcd(int);
161 static void hsg2msf(int, bcd_t *);
162 static daddr_t msf2hsg(bcd_t *, int);
163
164 int mcd_playtracks(struct mcd_softc *, struct ioc_play_track *);
165 int mcd_playmsf(struct mcd_softc *, struct ioc_play_msf *);
166 int mcd_playblocks(struct mcd_softc *, struct ioc_play_blocks *);
167 int mcd_stop(struct mcd_softc *);
168 int mcd_eject(struct mcd_softc *);
169 int mcd_read_subchannel(struct mcd_softc *, struct ioc_read_subchannel *,
170 struct cd_sub_channel_info *);
171 int mcd_pause(struct mcd_softc *);
172 int mcd_resume(struct mcd_softc *);
173 int mcd_toc_header(struct mcd_softc *, struct ioc_toc_header *);
174 int mcd_toc_entries(struct mcd_softc *, struct ioc_read_toc_entry *,
175 struct cd_toc_entry *, int *);
176
177 int mcd_getreply(struct mcd_softc *);
178 int mcd_getstat(struct mcd_softc *);
179 int mcd_getresult(struct mcd_softc *, struct mcd_result *);
180 void mcd_setflags(struct mcd_softc *);
181 int mcd_get(struct mcd_softc *, char *, int);
182 int mcd_send(struct mcd_softc *, struct mcd_mbox *, int);
183 int mcdintr(void *);
184 void mcd_soft_reset(struct mcd_softc *);
185 int mcd_hard_reset(struct mcd_softc *);
186 int mcd_setmode(struct mcd_softc *, int);
187 int mcd_setupc(struct mcd_softc *, int);
188 int mcd_read_toc(struct mcd_softc *);
189 int mcd_getqchan(struct mcd_softc *, union mcd_qchninfo *, int);
190 int mcd_setlock(struct mcd_softc *, int);
191
192 int mcd_find(bus_space_tag_t, bus_space_handle_t, struct mcd_softc *);
193 int mcdprobe(device_t, cfdata_t, void *);
194 void mcdattach(device_t, device_t, void *);
195
196 CFATTACH_DECL_NEW(mcd, sizeof(struct mcd_softc),
197 mcdprobe, mcdattach, NULL, NULL);
198
199 extern struct cfdriver mcd_cd;
200
201 dev_type_open(mcdopen);
202 dev_type_close(mcdclose);
203 dev_type_read(mcdread);
204 dev_type_write(mcdwrite);
205 dev_type_ioctl(mcdioctl);
206 dev_type_strategy(mcdstrategy);
207 dev_type_dump(mcddump);
208 dev_type_size(mcdsize);
209
210 const struct bdevsw mcd_bdevsw = {
211 .d_open = mcdopen,
212 .d_close = mcdclose,
213 .d_strategy = mcdstrategy,
214 .d_ioctl = mcdioctl,
215 .d_dump = mcddump,
216 .d_psize = mcdsize,
217 .d_flag = D_DISK
218 };
219
220 const struct cdevsw mcd_cdevsw = {
221 .d_open = mcdopen,
222 .d_close = mcdclose,
223 .d_read = mcdread,
224 .d_write = mcdwrite,
225 .d_ioctl = mcdioctl,
226 .d_stop = nostop,
227 .d_tty = notty,
228 .d_poll = nopoll,
229 .d_mmap = nommap,
230 .d_kqfilter = nokqfilter,
231 .d_flag = D_DISK
232 };
233
234 void mcdgetdefaultlabel(struct mcd_softc *, struct disklabel *);
235 void mcdgetdisklabel(struct mcd_softc *);
236 int mcd_get_parms(struct mcd_softc *);
237 void mcdstart(struct mcd_softc *);
238 void mcd_pseudointr(void *);
239
240 struct dkdriver mcddkdriver = { mcdstrategy, NULL, };
241
242 #define MCD_RETRIES 3
243 #define MCD_RDRETRIES 3
244
245 /* several delays */
246 #define RDELAY_WAITMODE 300
247 #define RDELAY_WAITREAD 800
248
249 #define DELAY_GRANULARITY 25 /* 25us */
250 #define DELAY_GETREPLY 100000 /* 100000 * 25us */
251
252 void
253 mcdattach(device_t parent, device_t self, void *aux)
254 {
255 struct mcd_softc *sc = device_private(self);
256 struct isa_attach_args *ia = aux;
257 bus_space_tag_t iot = ia->ia_iot;
258 bus_space_handle_t ioh;
259 struct mcd_mbox mbx;
260
261 /* Map i/o space */
262 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh)) {
263 printf(": can't map i/o space\n");
264 return;
265 }
266
267 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
268
269 sc->sc_iot = iot;
270 sc->sc_ioh = ioh;
271
272 sc->probe = 0;
273 sc->debug = 0;
274
275 if (!mcd_find(iot, ioh, sc)) {
276 printf(": mcd_find failed\n");
277 return;
278 }
279
280 bufq_alloc(&sc->buf_queue, "disksort", BUFQ_SORT_RAWBLOCK);
281 callout_init(&sc->sc_pintr_ch, 0);
282
283 /*
284 * Initialize and attach the disk structure.
285 */
286 disk_init(&sc->sc_dk, device_xname(sc->sc_dev), &mcddkdriver);
287 disk_attach(&sc->sc_dk);
288
289 printf(": model %s\n", sc->type != 0 ? sc->type : "unknown");
290
291 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
292
293 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
294 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
295 mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE;
296 mbx.cmd.data.config.data1 = 0x01;
297 mbx.res.length = 0;
298 (void) mcd_send(sc, &mbx, 0);
299
300 mcd_soft_reset(sc);
301
302 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
303 IST_EDGE, IPL_BIO, mcdintr, sc);
304 }
305
306 int
307 mcdopen(dev_t dev, int flag, int fmt, struct lwp *l)
308 {
309 int error, part;
310 struct mcd_softc *sc;
311
312 sc = device_lookup_private(&mcd_cd, MCDUNIT(dev));
313 if (sc == NULL)
314 return ENXIO;
315
316 mutex_enter(&sc->sc_lock);
317
318 if (sc->sc_dk.dk_openmask != 0) {
319 /*
320 * If any partition is open, but the disk has been invalidated,
321 * disallow further opens.
322 */
323 if ((sc->flags & MCDF_LOADED) == 0) {
324 error = EIO;
325 goto bad3;
326 }
327 } else {
328 /*
329 * Lock the drawer. This will also notice any pending disk
330 * change or door open indicator and clear the MCDF_LOADED bit
331 * if necessary.
332 */
333 (void) mcd_setlock(sc, MCD_LK_LOCK);
334
335 if ((sc->flags & MCDF_LOADED) == 0) {
336 /* Partially reset the state. */
337 sc->lastmode = MCD_MD_UNKNOWN;
338 sc->lastupc = MCD_UPC_UNKNOWN;
339
340 sc->flags |= MCDF_LOADED;
341
342 /* Set the mode, causing the disk to spin up. */
343 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
344 goto bad2;
345
346 /* Load the physical device parameters. */
347 if (mcd_get_parms(sc) != 0) {
348 error = ENXIO;
349 goto bad2;
350 }
351
352 /* Read the table of contents. */
353 if ((error = mcd_read_toc(sc)) != 0)
354 goto bad2;
355
356 /* Fabricate a disk label. */
357 mcdgetdisklabel(sc);
358 }
359 }
360
361 part = MCDPART(dev);
362
363 MCD_TRACE("open: partition=%d disksize=%ld blksize=%d\n", part,
364 sc->disksize, sc->blksize);
365
366 /* Check that the partition exists. */
367 if (part != RAW_PART &&
368 (part >= sc->sc_dk.dk_label->d_npartitions ||
369 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
370 error = ENXIO;
371 goto bad;
372 }
373
374 /* Insure only one open at a time. */
375 switch (fmt) {
376 case S_IFCHR:
377 sc->sc_dk.dk_copenmask |= (1 << part);
378 break;
379 case S_IFBLK:
380 sc->sc_dk.dk_bopenmask |= (1 << part);
381 break;
382 }
383 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
384
385 mutex_exit(&sc->sc_lock);
386 return 0;
387
388 bad2:
389 sc->flags &= ~MCDF_LOADED;
390
391 bad:
392 if (sc->sc_dk.dk_openmask == 0) {
393 #if 0
394 (void) mcd_setmode(sc, MCD_MD_SLEEP);
395 #endif
396 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
397 }
398
399 bad3:
400 mutex_exit(&sc->sc_lock);
401 return error;
402 }
403
404 int
405 mcdclose(dev_t dev, int flag, int fmt, struct lwp *l)
406 {
407 struct mcd_softc *sc = device_lookup_private(&mcd_cd, MCDUNIT(dev));
408 int part = MCDPART(dev);
409
410 MCD_TRACE("close: partition=%d\n", part);
411
412 mutex_enter(&sc->sc_lock);
413
414 switch (fmt) {
415 case S_IFCHR:
416 sc->sc_dk.dk_copenmask &= ~(1 << part);
417 break;
418 case S_IFBLK:
419 sc->sc_dk.dk_bopenmask &= ~(1 << part);
420 break;
421 }
422 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
423
424 if (sc->sc_dk.dk_openmask == 0) {
425 /* XXXX Must wait for I/O to complete! */
426
427 #if 0
428 (void) mcd_setmode(sc, MCD_MD_SLEEP);
429 #endif
430 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
431 }
432
433 mutex_exit(&sc->sc_lock);
434 return 0;
435 }
436
437 void
438 mcdstrategy(struct buf *bp)
439 {
440 struct mcd_softc *sc;
441 struct disklabel *lp;
442 daddr_t blkno;
443 int s;
444
445 sc = device_lookup_private(&mcd_cd, MCDUNIT(bp->b_dev));
446 lp = sc->sc_dk.dk_label;
447
448 /* Test validity. */
449 MCD_TRACE("strategy: buf=0x%p blkno=%d bcount=%d\n", bp,
450 (int) bp->b_blkno, bp->b_bcount);
451 if (bp->b_blkno < 0 ||
452 (bp->b_bcount % sc->blksize) != 0) {
453 printf("%s: strategy: blkno = %" PRId64 " bcount = %d\n",
454 device_xname(sc->sc_dev), bp->b_blkno, bp->b_bcount);
455 bp->b_error = EINVAL;
456 goto done;
457 }
458
459 /* If device invalidated (e.g. media change, door open), error. */
460 if ((sc->flags & MCDF_LOADED) == 0) {
461 MCD_TRACE("strategy: drive not valid%s", "\n");
462 bp->b_error = EIO;
463 goto done;
464 }
465
466 /* No data to read. */
467 if (bp->b_bcount == 0)
468 goto done;
469
470 /*
471 * Do bounds checking, adjust transfer. if error, process.
472 * If end of partition, just return.
473 */
474 if (MCDPART(bp->b_dev) != RAW_PART &&
475 bounds_check_with_label(&sc->sc_dk, bp,
476 (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
477 goto done;
478
479 /*
480 * Now convert the block number to absolute and put it in
481 * terms of the device's logical block size.
482 */
483 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
484 if (MCDPART(bp->b_dev) != RAW_PART)
485 blkno += lp->d_partitions[MCDPART(bp->b_dev)].p_offset;
486
487 bp->b_rawblkno = blkno;
488
489 /* Queue it. */
490 s = splbio();
491 bufq_put(sc->buf_queue, bp);
492 splx(s);
493 if (!sc->active)
494 mcdstart(sc);
495 return;
496
497 done:
498 bp->b_resid = bp->b_bcount;
499 biodone(bp);
500 }
501
502 void
503 mcdstart(struct mcd_softc *sc)
504 {
505 struct buf *bp;
506 int s;
507
508 loop:
509 s = splbio();
510
511 if ((bp = bufq_get(sc->buf_queue)) == NULL) {
512 /* Nothing to do. */
513 sc->active = 0;
514 splx(s);
515 return;
516 }
517
518 /* Block found to process. */
519 MCD_TRACE("start: found block bp=0x%p\n", bp);
520 splx(s);
521
522 /* Changed media? */
523 if ((sc->flags & MCDF_LOADED) == 0) {
524 MCD_TRACE("start: drive not valid%s", "\n");
525 bp->b_error = EIO;
526 biodone(bp);
527 goto loop;
528 }
529
530 sc->active = 1;
531
532 /* Instrumentation. */
533 s = splbio();
534 disk_busy(&sc->sc_dk);
535 splx(s);
536
537 sc->mbx.retry = MCD_RDRETRIES;
538 sc->mbx.bp = bp;
539 sc->mbx.blkno = bp->b_rawblkno;
540 sc->mbx.nblk = bp->b_bcount / sc->blksize;
541 sc->mbx.sz = sc->blksize;
542 sc->mbx.skip = 0;
543 sc->mbx.state = MCD_S_BEGIN;
544 sc->mbx.mode = MCD_MD_COOKED;
545
546 s = splbio();
547 (void) mcdintr(sc);
548 splx(s);
549 }
550
551 int
552 mcdread(dev_t dev, struct uio *uio, int flags)
553 {
554
555 return (physio(mcdstrategy, NULL, dev, B_READ, minphys, uio));
556 }
557
558 int
559 mcdwrite(dev_t dev, struct uio *uio, int flags)
560 {
561
562 return (physio(mcdstrategy, NULL, dev, B_WRITE, minphys, uio));
563 }
564
565 int
566 mcdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
567 {
568 struct mcd_softc *sc = device_lookup_private(&mcd_cd, MCDUNIT(dev));
569 int error;
570 int part;
571 #ifdef __HAVE_OLD_DISKLABEL
572 struct disklabel newlabel;
573 #endif
574
575 MCD_TRACE("ioctl: cmd=0x%lx\n", cmd);
576
577 if ((sc->flags & MCDF_LOADED) == 0)
578 return EIO;
579
580 part = MCDPART(dev);
581 switch (cmd) {
582 case DIOCGDINFO:
583 *(struct disklabel *)addr = *(sc->sc_dk.dk_label);
584 return 0;
585 #ifdef __HAVE_OLD_DISKLABEL
586 case ODIOCGDINFO:
587 newlabel = *(sc->sc_dk.dk_label);
588 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
589 return ENOTTY;
590 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
591 return 0;
592 #endif
593
594 case DIOCGPART:
595 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
596 ((struct partinfo *)addr)->part =
597 &sc->sc_dk.dk_label->d_partitions[part];
598 return 0;
599
600 case DIOCWDINFO:
601 case DIOCSDINFO:
602 #ifdef __HAVE_OLD_DISKLABEL
603 case ODIOCWDINFO:
604 case ODIOCSDINFO:
605 #endif
606 {
607 struct disklabel *lp;
608
609 if ((flag & FWRITE) == 0)
610 return EBADF;
611
612 #ifdef __HAVE_OLD_DISKLABEL
613 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
614 memset(&newlabel, 0, sizeof newlabel);
615 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
616 lp = &newlabel;
617 } else
618 #endif
619 lp = addr;
620
621 mutex_enter(&sc->sc_lock);
622 sc->flags |= MCDF_LABELLING;
623
624 error = setdisklabel(sc->sc_dk.dk_label,
625 lp, /*sc->sc_dk.dk_openmask : */0,
626 sc->sc_dk.dk_cpulabel);
627 if (error == 0) {
628 }
629
630 sc->flags &= ~MCDF_LABELLING;
631 mutex_exit(&sc->sc_lock);
632 return error;
633 }
634
635 case DIOCWLABEL:
636 return EBADF;
637
638 case DIOCGDEFLABEL:
639 mcdgetdefaultlabel(sc, addr);
640 return 0;
641
642 #ifdef __HAVE_OLD_DISKLABEL
643 case ODIOCGDEFLABEL:
644 mcdgetdefaultlabel(sc, &newlabel);
645 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
646 return ENOTTY;
647 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
648 return 0;
649 #endif
650
651 case CDIOCPLAYTRACKS:
652 return mcd_playtracks(sc, addr);
653 case CDIOCPLAYMSF:
654 return mcd_playmsf(sc, addr);
655 case CDIOCPLAYBLOCKS:
656 return mcd_playblocks(sc, addr);
657 case CDIOCREADSUBCHANNEL: {
658 struct cd_sub_channel_info info;
659 error = mcd_read_subchannel(sc, addr, &info);
660 if (error != 0) {
661 struct ioc_read_subchannel *ch = addr;
662 error = copyout(&info, ch->data, ch->data_len);
663 }
664 return error;
665 }
666 case CDIOCREADSUBCHANNEL_BUF:
667 return mcd_read_subchannel(sc, addr,
668 &((struct ioc_read_subchannel_buf *)addr)->info);
669 case CDIOREADTOCHEADER:
670 return mcd_toc_header(sc, addr);
671 case CDIOREADTOCENTRYS: {
672 struct cd_toc_entry entries[MCD_MAXTOCS];
673 struct ioc_read_toc_entry *te = addr;
674 int count;
675 if (te->data_len > sizeof entries)
676 return EINVAL;
677 error = mcd_toc_entries(sc, te, entries, &count);
678 if (error == 0)
679 /* Copy the data back. */
680 error = copyout(entries, te->data, min(te->data_len,
681 count * sizeof(struct cd_toc_entry)));
682 return error;
683 }
684 case CDIOREADTOCENTRIES_BUF: {
685 struct ioc_read_toc_entry_buf *te = addr;
686 int count;
687 if (te->req.data_len > sizeof te->entry)
688 return EINVAL;
689 return mcd_toc_entries(sc, &te->req, te->entry, &count);
690 }
691 case CDIOCSETPATCH:
692 case CDIOCGETVOL:
693 case CDIOCSETVOL:
694 case CDIOCSETMONO:
695 case CDIOCSETSTEREO:
696 case CDIOCSETMUTE:
697 case CDIOCSETLEFT:
698 case CDIOCSETRIGHT:
699 return EINVAL;
700 case CDIOCRESUME:
701 return mcd_resume(sc);
702 case CDIOCPAUSE:
703 return mcd_pause(sc);
704 case CDIOCSTART:
705 return EINVAL;
706 case CDIOCSTOP:
707 return mcd_stop(sc);
708 case DIOCEJECT:
709 if (*(int *)addr == 0) {
710 /*
711 * Don't force eject: check that we are the only
712 * partition open. If so, unlock it.
713 */
714 if ((sc->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
715 sc->sc_dk.dk_bopenmask + sc->sc_dk.dk_copenmask ==
716 sc->sc_dk.dk_openmask) {
717 error = mcd_setlock(sc, MCD_LK_UNLOCK);
718 if (error)
719 return (error);
720 } else {
721 return (EBUSY);
722 }
723 }
724 /* FALLTHROUGH */
725 case CDIOCEJECT: /* FALLTHROUGH */
726 case ODIOCEJECT:
727 return mcd_eject(sc);
728 case CDIOCALLOW:
729 return mcd_setlock(sc, MCD_LK_UNLOCK);
730 case CDIOCPREVENT:
731 return mcd_setlock(sc, MCD_LK_LOCK);
732 case DIOCLOCK:
733 return mcd_setlock(sc,
734 (*(int *)addr) ? MCD_LK_LOCK : MCD_LK_UNLOCK);
735 case CDIOCSETDEBUG:
736 sc->debug = 1;
737 return 0;
738 case CDIOCCLRDEBUG:
739 sc->debug = 0;
740 return 0;
741 case CDIOCRESET:
742 return mcd_hard_reset(sc);
743
744 default:
745 return ENOTTY;
746 }
747
748 #ifdef DIAGNOSTIC
749 panic("mcdioctl: impossible");
750 #endif
751 }
752
753 void
754 mcdgetdefaultlabel(struct mcd_softc *sc, struct disklabel *lp)
755 {
756
757 memset(lp, 0, sizeof(struct disklabel));
758
759 lp->d_secsize = sc->blksize;
760 lp->d_ntracks = 1;
761 lp->d_nsectors = 100;
762 lp->d_ncylinders = (sc->disksize / 100) + 1;
763 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
764
765 strncpy(lp->d_typename, "Mitsumi CD-ROM", 16);
766 lp->d_type = 0; /* XXX */
767 strncpy(lp->d_packname, "fictitious", 16);
768 lp->d_secperunit = sc->disksize;
769 lp->d_rpm = 300;
770 lp->d_interleave = 1;
771 lp->d_flags = D_REMOVABLE;
772
773 lp->d_partitions[0].p_offset = 0;
774 lp->d_partitions[0].p_size =
775 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
776 lp->d_partitions[0].p_fstype = FS_ISO9660;
777 lp->d_partitions[RAW_PART].p_offset = 0;
778 lp->d_partitions[RAW_PART].p_size =
779 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
780 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
781 lp->d_npartitions = RAW_PART + 1;
782
783 lp->d_magic = DISKMAGIC;
784 lp->d_magic2 = DISKMAGIC;
785 lp->d_checksum = dkcksum(lp);
786 }
787
788 /*
789 * This could have been taken from scsi/cd.c, but it is not clear
790 * whether the scsi cd driver is linked in.
791 */
792 void
793 mcdgetdisklabel(struct mcd_softc *sc)
794 {
795 struct disklabel *lp = sc->sc_dk.dk_label;
796
797 memset(sc->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
798
799 mcdgetdefaultlabel(sc, lp);
800 }
801
802 int
803 mcd_get_parms(struct mcd_softc *sc)
804 {
805 struct mcd_mbox mbx;
806 daddr_t size;
807 int error;
808
809 /* Send volume info command. */
810 mbx.cmd.opcode = MCD_CMDGETVOLINFO;
811 mbx.cmd.length = 0;
812 mbx.res.length = sizeof(mbx.res.data.volinfo);
813 if ((error = mcd_send(sc, &mbx, 1)) != 0)
814 return error;
815
816 if (mbx.res.data.volinfo.trk_low == 0x00 &&
817 mbx.res.data.volinfo.trk_high == 0x00)
818 return EINVAL;
819
820 /* Volinfo is OK. */
821 sc->volinfo = mbx.res.data.volinfo;
822 sc->blksize = MCD_BLKSIZE_COOKED;
823 size = msf2hsg(sc->volinfo.vol_msf, 0);
824 sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
825 return 0;
826 }
827
828 int
829 mcdsize(dev_t dev)
830 {
831
832 /* CD-ROMs are read-only. */
833 return -1;
834 }
835
836 int
837 mcddump(dev_t dev, daddr_t blkno, void *va,
838 size_t size)
839 {
840
841 /* Not implemented. */
842 return ENXIO;
843 }
844
845 /*
846 * Find the board and fill in the softc.
847 */
848 int
849 mcd_find(bus_space_tag_t iot, bus_space_handle_t ioh, struct mcd_softc *sc)
850 {
851 int i;
852 struct mcd_mbox mbx;
853
854 sc->sc_iot = iot;
855 sc->sc_ioh = ioh;
856
857 /* Send a reset. */
858 bus_space_write_1(iot, ioh, MCD_RESET, 0);
859 delay(1000000);
860 /* Get any pending status and throw away. */
861 for (i = 10; i; i--)
862 bus_space_read_1(iot, ioh, MCD_STATUS);
863 delay(1000);
864
865 /* Send get status command. */
866 mbx.cmd.opcode = MCD_CMDGETSTAT;
867 mbx.cmd.length = 0;
868 mbx.res.length = 0;
869 if (mcd_send(sc, &mbx, 0) != 0)
870 return 0;
871
872 /* Get info about the drive. */
873 mbx.cmd.opcode = MCD_CMDCONTINFO;
874 mbx.cmd.length = 0;
875 mbx.res.length = sizeof(mbx.res.data.continfo);
876 if (mcd_send(sc, &mbx, 0) != 0)
877 return 0;
878
879 /*
880 * The following is code which is not guaranteed to work for all
881 * drives, because the meaning of the expected 'M' is not clear
882 * (M_itsumi is an obvious assumption, but I don't trust that).
883 * Also, the original hack had a bogus condition that always
884 * returned true.
885 *
886 * Note: Which models support interrupts? >=LU005S?
887 */
888 sc->readcmd = MCD_CMDREADSINGLESPEED;
889 switch (mbx.res.data.continfo.code) {
890 case 'M':
891 if (mbx.res.data.continfo.version <= 2)
892 sc->type = "LU002S";
893 else if (mbx.res.data.continfo.version <= 5)
894 sc->type = "LU005S";
895 else
896 sc->type = "LU006S";
897 break;
898 case 'F':
899 sc->type = "FX001";
900 break;
901 case 'D':
902 sc->type = "FX001D";
903 sc->readcmd = MCD_CMDREADDOUBLESPEED;
904 break;
905 default:
906 /*
907 * mcd_send() says the response looked OK but the
908 * drive type is unknown. If mcd_promisc, match anyway.
909 */
910 if (mcd_promisc != 0)
911 return 0;
912
913 #ifdef MCDDEBUG
914 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
915 device_xname(sc->sc_dev),
916 mbx.res.data.continfo.code, mbx.res.data.continfo.version);
917 #endif
918 sc->type = 0;
919 break;
920 }
921
922 return 1;
923
924 }
925
926 int
927 mcdprobe(device_t parent, cfdata_t match, void *aux)
928 {
929 struct isa_attach_args *ia = aux;
930 struct mcd_softc sc;
931 bus_space_tag_t iot = ia->ia_iot;
932 bus_space_handle_t ioh;
933 int rv;
934
935 if (ia->ia_nio < 1)
936 return (0);
937 if (ia->ia_nirq < 1)
938 return (0);
939
940 if (ISA_DIRECT_CONFIG(ia))
941 return (0);
942
943 /* Disallow wildcarded i/o address. */
944 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
945 return (0);
946 if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
947 return (0);
948
949 /* Map i/o space */
950 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh))
951 return 0;
952
953 sc.debug = 0;
954 sc.probe = 1;
955
956 rv = mcd_find(iot, ioh, &sc);
957
958 bus_space_unmap(iot, ioh, MCD_NPORT);
959
960 if (rv) {
961 ia->ia_nio = 1;
962 ia->ia_io[0].ir_size = MCD_NPORT;
963
964 ia->ia_nirq = 1;
965
966 ia->ia_niomem = 0;
967 ia->ia_ndrq = 0;
968 }
969
970 return (rv);
971 }
972
973 int
974 mcd_getreply(struct mcd_softc *sc)
975 {
976 bus_space_tag_t iot = sc->sc_iot;
977 bus_space_handle_t ioh = sc->sc_ioh;
978 int i;
979
980 /* Wait until xfer port senses data ready. */
981 for (i = DELAY_GETREPLY; i; i--) {
982 if ((bus_space_read_1(iot, ioh, MCD_XFER) &
983 MCD_XF_STATUSUNAVAIL) == 0)
984 break;
985 delay(DELAY_GRANULARITY);
986 }
987 if (!i)
988 return -1;
989
990 /* Get the data. */
991 return bus_space_read_1(iot, ioh, MCD_STATUS);
992 }
993
994 int
995 mcd_getstat(struct mcd_softc *sc)
996 {
997 struct mcd_mbox mbx;
998
999 mbx.cmd.opcode = MCD_CMDGETSTAT;
1000 mbx.cmd.length = 0;
1001 mbx.res.length = 0;
1002 return mcd_send(sc, &mbx, 1);
1003 }
1004
1005 int
1006 mcd_getresult(struct mcd_softc *sc, struct mcd_result *res)
1007 {
1008 int i, x;
1009
1010 if (sc->debug)
1011 printf("%s: mcd_getresult: %d", device_xname(sc->sc_dev),
1012 res->length);
1013
1014 if ((x = mcd_getreply(sc)) < 0) {
1015 if (sc->debug)
1016 printf(" timeout\n");
1017 else if (!sc->probe)
1018 printf("%s: timeout in getresult\n", device_xname(sc->sc_dev));
1019 return EIO;
1020 }
1021 if (sc->debug)
1022 printf(" %02x", (u_int)x);
1023 sc->status = x;
1024 mcd_setflags(sc);
1025
1026 if ((sc->status & MCD_ST_CMDCHECK) != 0)
1027 return EINVAL;
1028
1029 for (i = 0; i < res->length; i++) {
1030 if ((x = mcd_getreply(sc)) < 0) {
1031 if (sc->debug)
1032 printf(" timeout\n");
1033 else
1034 printf("%s: timeout in getresult\n", device_xname(sc->sc_dev));
1035 return EIO;
1036 }
1037 if (sc->debug)
1038 printf(" %02x", (u_int)x);
1039 res->data.raw.data[i] = x;
1040 }
1041
1042 if (sc->debug)
1043 printf(" succeeded\n");
1044
1045 #ifdef MCDDEBUG
1046 delay(10);
1047 while ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_XFER) &
1048 MCD_XF_STATUSUNAVAIL) == 0) {
1049 x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_STATUS);
1050 printf("%s: got extra byte %02x during getstatus\n",
1051 device_xname(sc->sc_dev), (u_int)x);
1052 delay(10);
1053 }
1054 #endif
1055
1056 return 0;
1057 }
1058
1059 void
1060 mcd_setflags(struct mcd_softc *sc)
1061 {
1062
1063 /* Check flags. */
1064 if ((sc->flags & MCDF_LOADED) != 0 &&
1065 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
1066 MCD_ST_DSKIN) {
1067 if ((sc->status & MCD_ST_DOOROPEN) != 0)
1068 printf("%s: door open\n", device_xname(sc->sc_dev));
1069 else if ((sc->status & MCD_ST_DSKIN) == 0)
1070 printf("%s: no disk present\n", device_xname(sc->sc_dev));
1071 else if ((sc->status & MCD_ST_DSKCHNG) != 0)
1072 printf("%s: media change\n", device_xname(sc->sc_dev));
1073 sc->flags &= ~MCDF_LOADED;
1074 }
1075
1076 if ((sc->status & MCD_ST_AUDIOBSY) != 0)
1077 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
1078 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
1079 sc->audio_status == CD_AS_AUDIO_INVALID)
1080 sc->audio_status = CD_AS_PLAY_COMPLETED;
1081 }
1082
1083 int
1084 mcd_send(struct mcd_softc *sc, struct mcd_mbox *mbx, int diskin)
1085 {
1086 int retry, i, error;
1087 bus_space_tag_t iot = sc->sc_iot;
1088 bus_space_handle_t ioh = sc->sc_ioh;
1089
1090 if (sc->debug) {
1091 printf("%s: mcd_send: %d %02x", device_xname(sc->sc_dev),
1092 mbx->cmd.length, (u_int)mbx->cmd.opcode);
1093 for (i = 0; i < mbx->cmd.length; i++)
1094 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
1095 printf("\n");
1096 }
1097
1098 for (retry = MCD_RETRIES; retry; retry--) {
1099 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.opcode);
1100 for (i = 0; i < mbx->cmd.length; i++)
1101 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.data.raw.data[i]);
1102 if ((error = mcd_getresult(sc, &mbx->res)) == 0)
1103 break;
1104 if (error == EINVAL)
1105 return error;
1106 }
1107 if (!retry)
1108 return error;
1109 if (diskin && (sc->flags & MCDF_LOADED) == 0)
1110 return EIO;
1111
1112 return 0;
1113 }
1114
1115 static int
1116 bcd2bin(bcd_t b)
1117 {
1118
1119 return (b >> 4) * 10 + (b & 15);
1120 }
1121
1122 static bcd_t
1123 bin2bcd(int b)
1124 {
1125
1126 return ((b / 10) << 4) | (b % 10);
1127 }
1128
1129 static void
1130 hsg2msf(int hsg, bcd_t *msf)
1131 {
1132
1133 hsg += 150;
1134 F_msf(msf) = bin2bcd(hsg % 75);
1135 hsg /= 75;
1136 S_msf(msf) = bin2bcd(hsg % 60);
1137 hsg /= 60;
1138 M_msf(msf) = bin2bcd(hsg);
1139 }
1140
1141 static daddr_t
1142 msf2hsg(bcd_t *msf, int relative)
1143 {
1144 daddr_t blkno;
1145
1146 blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
1147 bcd2bin(S_msf(msf)) * 75 +
1148 bcd2bin(F_msf(msf));
1149 if (!relative)
1150 blkno -= 150;
1151 return blkno;
1152 }
1153
1154 void
1155 mcd_pseudointr(void *v)
1156 {
1157 struct mcd_softc *sc = v;
1158 int s;
1159
1160 s = splbio();
1161 (void) mcdintr(sc);
1162 splx(s);
1163 }
1164
1165 /*
1166 * State machine to process read requests.
1167 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
1168 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
1169 * MCD_S_WAITREAD: wait for read ready, read data.
1170 */
1171 int
1172 mcdintr(void *arg)
1173 {
1174 struct mcd_softc *sc = arg;
1175 struct mcd_mbx *mbx = &sc->mbx;
1176 struct buf *bp = mbx->bp;
1177 bus_space_tag_t iot = sc->sc_iot;
1178 bus_space_handle_t ioh = sc->sc_ioh;
1179
1180 int i;
1181 u_char x;
1182 bcd_t msf[3];
1183
1184 switch (mbx->state) {
1185 case MCD_S_IDLE:
1186 return 0;
1187
1188 case MCD_S_BEGIN:
1189 tryagain:
1190 if (mbx->mode == sc->lastmode)
1191 goto firstblock;
1192
1193 sc->lastmode = MCD_MD_UNKNOWN;
1194 bus_space_write_1(iot, ioh, MCD_COMMAND, MCD_CMDSETMODE);
1195 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->mode);
1196
1197 mbx->count = RDELAY_WAITMODE;
1198 mbx->state = MCD_S_WAITMODE;
1199
1200 case MCD_S_WAITMODE:
1201 callout_stop(&sc->sc_pintr_ch);
1202 for (i = 20; i; i--) {
1203 x = bus_space_read_1(iot, ioh, MCD_XFER);
1204 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1205 break;
1206 delay(50);
1207 }
1208 if (i == 0)
1209 goto hold;
1210 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1211 mcd_setflags(sc);
1212 if ((sc->flags & MCDF_LOADED) == 0)
1213 goto changed;
1214 MCD_TRACE("doread: got WAITMODE delay=%d\n",
1215 RDELAY_WAITMODE - mbx->count);
1216
1217 sc->lastmode = mbx->mode;
1218
1219 firstblock:
1220 MCD_TRACE("doread: read blkno=%d for bp=0x%p\n",
1221 (int) mbx->blkno, bp);
1222
1223 /* Build parameter block. */
1224 hsg2msf(mbx->blkno, msf);
1225
1226 /* Send the read command. */
1227 bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd);
1228 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]);
1229 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]);
1230 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]);
1231 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1232 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1233 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk);
1234
1235 mbx->count = RDELAY_WAITREAD;
1236 mbx->state = MCD_S_WAITREAD;
1237
1238 case MCD_S_WAITREAD:
1239 callout_stop(&sc->sc_pintr_ch);
1240 nextblock:
1241 loop:
1242 for (i = 20; i; i--) {
1243 x = bus_space_read_1(iot, ioh, MCD_XFER);
1244 if ((x & MCD_XF_DATAUNAVAIL) == 0)
1245 goto gotblock;
1246 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1247 break;
1248 delay(50);
1249 }
1250 if (i == 0)
1251 goto hold;
1252 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1253 mcd_setflags(sc);
1254 if ((sc->flags & MCDF_LOADED) == 0)
1255 goto changed;
1256 #if 0
1257 printf("%s: got status byte %02x during read\n",
1258 device_xname(sc->sc_dev), (u_int)sc->status);
1259 #endif
1260 goto loop;
1261
1262 gotblock:
1263 MCD_TRACE("doread: got data delay=%d\n",
1264 RDELAY_WAITREAD - mbx->count);
1265
1266 /* Data is ready. */
1267 bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */
1268 bus_space_read_multi_1(iot, ioh, MCD_RDATA,
1269 (char *)bp->b_data + mbx->skip, mbx->sz);
1270 bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */
1271 mbx->blkno += 1;
1272 mbx->skip += mbx->sz;
1273 if (--mbx->nblk > 0)
1274 goto nextblock;
1275
1276 mbx->state = MCD_S_IDLE;
1277
1278 /* Return buffer. */
1279 bp->b_resid = 0;
1280 disk_unbusy(&sc->sc_dk, bp->b_bcount, (bp->b_flags & B_READ));
1281 biodone(bp);
1282
1283 mcdstart(sc);
1284 return 1;
1285
1286 hold:
1287 if (mbx->count-- < 0) {
1288 printf("%s: timeout in state %d",
1289 device_xname(sc->sc_dev), mbx->state);
1290 goto readerr;
1291 }
1292
1293 #if 0
1294 printf("%s: sleep in state %d\n", device_xname(sc->sc_dev),
1295 mbx->state);
1296 #endif
1297 callout_reset(&sc->sc_pintr_ch, hz / 100,
1298 mcd_pseudointr, sc);
1299 return -1;
1300 }
1301
1302 readerr:
1303 if (mbx->retry-- > 0) {
1304 printf("; retrying\n");
1305 goto tryagain;
1306 } else
1307 printf("; giving up\n");
1308
1309 changed:
1310 /* Invalidate the buffer. */
1311 bp->b_error = EIO;
1312 bp->b_resid = bp->b_bcount - mbx->skip;
1313 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid),
1314 (bp->b_flags & B_READ));
1315 biodone(bp);
1316
1317 mcdstart(sc);
1318 return -1;
1319
1320 #ifdef notyet
1321 printf("%s: unit timeout; resetting\n", device_xname(sc->sc_dev));
1322 bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET);
1323 delay(300000);
1324 (void) mcd_getstat(sc, 1);
1325 (void) mcd_getstat(sc, 1);
1326 /*sc->status &= ~MCD_ST_DSKCHNG; */
1327 sc->debug = 1; /* preventive set debug mode */
1328 #endif
1329 }
1330
1331 void
1332 mcd_soft_reset(struct mcd_softc *sc)
1333 {
1334
1335 sc->debug = 0;
1336 sc->flags = 0;
1337 sc->lastmode = MCD_MD_UNKNOWN;
1338 sc->lastupc = MCD_UPC_UNKNOWN;
1339 sc->audio_status = CD_AS_AUDIO_INVALID;
1340 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */
1341 }
1342
1343 int
1344 mcd_hard_reset(struct mcd_softc *sc)
1345 {
1346 struct mcd_mbox mbx;
1347
1348 mcd_soft_reset(sc);
1349
1350 mbx.cmd.opcode = MCD_CMDRESET;
1351 mbx.cmd.length = 0;
1352 mbx.res.length = 0;
1353 return mcd_send(sc, &mbx, 0);
1354 }
1355
1356 int
1357 mcd_setmode(struct mcd_softc *sc, int mode)
1358 {
1359 struct mcd_mbox mbx;
1360 int error;
1361
1362 if (sc->lastmode == mode)
1363 return 0;
1364 if (sc->debug)
1365 printf("%s: setting mode to %d\n", device_xname(sc->sc_dev), mode);
1366 sc->lastmode = MCD_MD_UNKNOWN;
1367
1368 mbx.cmd.opcode = MCD_CMDSETMODE;
1369 mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
1370 mbx.cmd.data.datamode.mode = mode;
1371 mbx.res.length = 0;
1372 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1373 return error;
1374
1375 sc->lastmode = mode;
1376 return 0;
1377 }
1378
1379 int
1380 mcd_setupc(struct mcd_softc *sc, int upc)
1381 {
1382 struct mcd_mbox mbx;
1383 int error;
1384
1385 if (sc->lastupc == upc)
1386 return 0;
1387 if (sc->debug)
1388 printf("%s: setting upc to %d\n", device_xname(sc->sc_dev), upc);
1389 sc->lastupc = MCD_UPC_UNKNOWN;
1390
1391 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
1392 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
1393 mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
1394 mbx.cmd.data.config.data1 = upc;
1395 mbx.res.length = 0;
1396 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1397 return error;
1398
1399 sc->lastupc = upc;
1400 return 0;
1401 }
1402
1403 int
1404 mcd_toc_header(struct mcd_softc *sc, struct ioc_toc_header *th)
1405 {
1406
1407 if (sc->debug)
1408 printf("%s: mcd_toc_header: reading toc header\n",
1409 device_xname(sc->sc_dev));
1410
1411 th->len = msf2hsg(sc->volinfo.vol_msf, 0);
1412 th->starting_track = bcd2bin(sc->volinfo.trk_low);
1413 th->ending_track = bcd2bin(sc->volinfo.trk_high);
1414
1415 return 0;
1416 }
1417
1418 int
1419 mcd_read_toc(struct mcd_softc *sc)
1420 {
1421 struct ioc_toc_header th;
1422 union mcd_qchninfo q;
1423 int error, trk, idx, retry;
1424
1425 if ((error = mcd_toc_header(sc, &th)) != 0)
1426 return error;
1427
1428 if ((error = mcd_stop(sc)) != 0)
1429 return error;
1430
1431 if (sc->debug)
1432 printf("%s: read_toc: reading qchannel info\n",
1433 device_xname(sc->sc_dev));
1434
1435 for (trk = th.starting_track; trk <= th.ending_track; trk++)
1436 sc->toc[trk].toc.idx_no = 0x00;
1437 trk = th.ending_track - th.starting_track + 1;
1438 for (retry = 300; retry && trk > 0; retry--) {
1439 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
1440 break;
1441 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
1442 continue;
1443 idx = bcd2bin(q.toc.idx_no);
1444 if (idx < MCD_MAXTOCS &&
1445 sc->toc[idx].toc.idx_no == 0x00) {
1446 sc->toc[idx] = q;
1447 trk--;
1448 }
1449 }
1450
1451 /* Inform the drive that we're finished so it turns off the light. */
1452 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1453 return error;
1454
1455 if (trk != 0)
1456 return EINVAL;
1457
1458 /* Add a fake last+1 for mcd_playtracks(). */
1459 idx = th.ending_track + 1;
1460 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
1461 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
1462 sc->toc[idx].toc.trk_no = 0x00;
1463 sc->toc[idx].toc.idx_no = 0xaa;
1464 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
1465 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
1466 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
1467
1468 return 0;
1469 }
1470
1471 int
1472 mcd_toc_entries(struct mcd_softc *sc, struct ioc_read_toc_entry *te, struct cd_toc_entry *entries, int *count)
1473 {
1474 int len = te->data_len;
1475 struct ioc_toc_header header;
1476 u_char trk;
1477 daddr_t lba;
1478 int error, n;
1479
1480 if (len < sizeof(struct cd_toc_entry))
1481 return EINVAL;
1482 if (te->address_format != CD_MSF_FORMAT &&
1483 te->address_format != CD_LBA_FORMAT)
1484 return EINVAL;
1485
1486 /* Copy the TOC header. */
1487 if ((error = mcd_toc_header(sc, &header)) != 0)
1488 return error;
1489
1490 /* Verify starting track. */
1491 trk = te->starting_track;
1492 if (trk == 0x00)
1493 trk = header.starting_track;
1494 else if (trk == 0xaa)
1495 trk = header.ending_track + 1;
1496 else if (trk < header.starting_track ||
1497 trk > header.ending_track + 1)
1498 return EINVAL;
1499
1500 /* Copy the TOC data. */
1501 for (n = 0; trk <= header.ending_track + 1; n++, trk++) {
1502 if (n * sizeof entries[0] > len)
1503 break;
1504 if (sc->toc[trk].toc.idx_no == 0x00)
1505 continue;
1506 entries[n].control = sc->toc[trk].toc.control;
1507 entries[n].addr_type = sc->toc[trk].toc.addr_type;
1508 entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
1509 switch (te->address_format) {
1510 case CD_MSF_FORMAT:
1511 entries[n].addr.addr[0] = 0;
1512 entries[n].addr.addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
1513 entries[n].addr.addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
1514 entries[n].addr.addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
1515 break;
1516 case CD_LBA_FORMAT:
1517 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
1518 entries[n].addr.addr[0] = lba >> 24;
1519 entries[n].addr.addr[1] = lba >> 16;
1520 entries[n].addr.addr[2] = lba >> 8;
1521 entries[n].addr.addr[3] = lba;
1522 break;
1523 }
1524 }
1525
1526 *count = n;
1527 return 0;
1528 }
1529
1530 int
1531 mcd_stop(struct mcd_softc *sc)
1532 {
1533 struct mcd_mbox mbx;
1534 int error;
1535
1536 if (sc->debug)
1537 printf("%s: mcd_stop: stopping play\n", device_xname(sc->sc_dev));
1538
1539 mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
1540 mbx.cmd.length = 0;
1541 mbx.res.length = 0;
1542 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1543 return error;
1544
1545 sc->audio_status = CD_AS_PLAY_COMPLETED;
1546 return 0;
1547 }
1548
1549 int
1550 mcd_getqchan(struct mcd_softc *sc, union mcd_qchninfo *q, int qchn)
1551 {
1552 struct mcd_mbox mbx;
1553 int error;
1554
1555 if (qchn == CD_TRACK_INFO) {
1556 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
1557 return error;
1558 } else {
1559 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1560 return error;
1561 }
1562 if (qchn == CD_MEDIA_CATALOG) {
1563 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
1564 return error;
1565 } else {
1566 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
1567 return error;
1568 }
1569
1570 mbx.cmd.opcode = MCD_CMDGETQCHN;
1571 mbx.cmd.length = 0;
1572 mbx.res.length = sizeof(mbx.res.data.qchninfo);
1573 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1574 return error;
1575
1576 *q = mbx.res.data.qchninfo;
1577 return 0;
1578 }
1579
1580 int
1581 mcd_read_subchannel(struct mcd_softc *sc, struct ioc_read_subchannel *ch, struct cd_sub_channel_info *info)
1582 {
1583 int len = ch->data_len;
1584 union mcd_qchninfo q;
1585 daddr_t lba;
1586 int error;
1587
1588 if (sc->debug)
1589 printf("%s: subchan: af=%d df=%d\n", device_xname(sc->sc_dev),
1590 ch->address_format, ch->data_format);
1591
1592 if (len > sizeof(*info) || len < sizeof(info->header))
1593 return EINVAL;
1594 if (ch->address_format != CD_MSF_FORMAT &&
1595 ch->address_format != CD_LBA_FORMAT)
1596 return EINVAL;
1597 if (ch->data_format != CD_CURRENT_POSITION &&
1598 ch->data_format != CD_MEDIA_CATALOG)
1599 return EINVAL;
1600
1601 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
1602 return error;
1603
1604 info->header.audio_status = sc->audio_status;
1605 info->what.media_catalog.data_format = ch->data_format;
1606
1607 switch (ch->data_format) {
1608 case CD_MEDIA_CATALOG:
1609 info->what.media_catalog.mc_valid = 1;
1610 #if 0
1611 info->what.media_catalog.mc_number =
1612 #endif
1613 break;
1614
1615 case CD_CURRENT_POSITION:
1616 info->what.position.track_number = bcd2bin(q.current.trk_no);
1617 info->what.position.index_number = bcd2bin(q.current.idx_no);
1618 switch (ch->address_format) {
1619 case CD_MSF_FORMAT:
1620 info->what.position.reladdr.addr[0] = 0;
1621 info->what.position.reladdr.addr[1] = bcd2bin(q.current.relative_pos[0]);
1622 info->what.position.reladdr.addr[2] = bcd2bin(q.current.relative_pos[1]);
1623 info->what.position.reladdr.addr[3] = bcd2bin(q.current.relative_pos[2]);
1624 info->what.position.absaddr.addr[0] = 0;
1625 info->what.position.absaddr.addr[1] = bcd2bin(q.current.absolute_pos[0]);
1626 info->what.position.absaddr.addr[2] = bcd2bin(q.current.absolute_pos[1]);
1627 info->what.position.absaddr.addr[3] = bcd2bin(q.current.absolute_pos[2]);
1628 break;
1629 case CD_LBA_FORMAT:
1630 lba = msf2hsg(q.current.relative_pos, 1);
1631 /*
1632 * Pre-gap has index number of 0, and decreasing MSF
1633 * address. Must be converted to negative LBA, per
1634 * SCSI spec.
1635 */
1636 if (info->what.position.index_number == 0x00)
1637 lba = -lba;
1638 info->what.position.reladdr.addr[0] = lba >> 24;
1639 info->what.position.reladdr.addr[1] = lba >> 16;
1640 info->what.position.reladdr.addr[2] = lba >> 8;
1641 info->what.position.reladdr.addr[3] = lba;
1642 lba = msf2hsg(q.current.absolute_pos, 0);
1643 info->what.position.absaddr.addr[0] = lba >> 24;
1644 info->what.position.absaddr.addr[1] = lba >> 16;
1645 info->what.position.absaddr.addr[2] = lba >> 8;
1646 info->what.position.absaddr.addr[3] = lba;
1647 break;
1648 }
1649 break;
1650 }
1651
1652 return 0;
1653 }
1654
1655 int
1656 mcd_playtracks(struct mcd_softc *sc, struct ioc_play_track *p)
1657 {
1658 struct mcd_mbox mbx;
1659 int a = p->start_track;
1660 int z = p->end_track;
1661 int error;
1662
1663 if (sc->debug)
1664 printf("%s: playtracks: from %d:%d to %d:%d\n",
1665 device_xname(sc->sc_dev),
1666 a, p->start_index, z, p->end_index);
1667
1668 if (a < bcd2bin(sc->volinfo.trk_low) ||
1669 a > bcd2bin(sc->volinfo.trk_high) ||
1670 a > z ||
1671 z < bcd2bin(sc->volinfo.trk_low) ||
1672 z > bcd2bin(sc->volinfo.trk_high))
1673 return EINVAL;
1674
1675 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1676 return error;
1677
1678 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1679 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1680 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
1681 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
1682 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
1683 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
1684 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
1685 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
1686 sc->lastpb = mbx.cmd;
1687 mbx.res.length = 0;
1688 return mcd_send(sc, &mbx, 1);
1689 }
1690
1691 int
1692 mcd_playmsf(struct mcd_softc *sc, struct ioc_play_msf *p)
1693 {
1694 struct mcd_mbox mbx;
1695 int error;
1696
1697 if (sc->debug)
1698 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
1699 device_xname(sc->sc_dev),
1700 p->start_m, p->start_s, p->start_f,
1701 p->end_m, p->end_s, p->end_f);
1702
1703 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1704 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
1705 return EINVAL;
1706
1707 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1708 return error;
1709
1710 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1711 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1712 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
1713 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
1714 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
1715 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
1716 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
1717 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
1718 sc->lastpb = mbx.cmd;
1719 mbx.res.length = 0;
1720 return mcd_send(sc, &mbx, 1);
1721 }
1722
1723 int
1724 mcd_playblocks(struct mcd_softc *sc, struct ioc_play_blocks *p)
1725 {
1726 struct mcd_mbox mbx;
1727 int error;
1728
1729 if (sc->debug)
1730 printf("%s: playblocks: blkno %d length %d\n",
1731 device_xname(sc->sc_dev), p->blk, p->len);
1732
1733 if (p->blk > sc->disksize || p->len > sc->disksize ||
1734 (p->blk + p->len) > sc->disksize)
1735 return 0;
1736
1737 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1738 return error;
1739
1740 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1741 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1742 hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
1743 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
1744 sc->lastpb = mbx.cmd;
1745 mbx.res.length = 0;
1746 return mcd_send(sc, &mbx, 1);
1747 }
1748
1749 int
1750 mcd_pause(struct mcd_softc *sc)
1751 {
1752 union mcd_qchninfo q;
1753 int error;
1754
1755 /* Verify current status. */
1756 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
1757 printf("%s: pause: attempted when not playing\n",
1758 device_xname(sc->sc_dev));
1759 return EINVAL;
1760 }
1761
1762 /* Get the current position. */
1763 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
1764 return error;
1765
1766 /* Copy it into lastpb. */
1767 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
1768 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
1769 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
1770
1771 /* Stop playing. */
1772 if ((error = mcd_stop(sc)) != 0)
1773 return error;
1774
1775 /* Set the proper status and exit. */
1776 sc->audio_status = CD_AS_PLAY_PAUSED;
1777 return 0;
1778 }
1779
1780 int
1781 mcd_resume(struct mcd_softc *sc)
1782 {
1783 struct mcd_mbox mbx;
1784 int error;
1785
1786 if (sc->audio_status != CD_AS_PLAY_PAUSED)
1787 return EINVAL;
1788
1789 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1790 return error;
1791
1792 mbx.cmd = sc->lastpb;
1793 mbx.res.length = 0;
1794 return mcd_send(sc, &mbx, 1);
1795 }
1796
1797 int
1798 mcd_eject(struct mcd_softc *sc)
1799 {
1800 struct mcd_mbox mbx;
1801
1802 mbx.cmd.opcode = MCD_CMDEJECTDISK;
1803 mbx.cmd.length = 0;
1804 mbx.res.length = 0;
1805 return mcd_send(sc, &mbx, 0);
1806 }
1807
1808 int
1809 mcd_setlock(struct mcd_softc *sc, int mode)
1810 {
1811 struct mcd_mbox mbx;
1812
1813 mbx.cmd.opcode = MCD_CMDSETLOCK;
1814 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
1815 mbx.cmd.data.lockmode.mode = mode;
1816 mbx.res.length = 0;
1817 return mcd_send(sc, &mbx, 1);
1818 }
1819