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