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