Deleted Added
sdiff udiff text old ( 104445 ) new ( 104519 )
full compact
1#include "opt_geom.h"
2#ifndef GEOM
3/*
4 * Copyright 1993 by Holger Veit (data part)
5 * Copyright 1993 by Brian Moore (audio part)
6 * Changes Copyright 1993 by Gary Clark II
7 * Changes Copyright (C) 1994-1995 by Andrey A. Chernov, Moscow, Russia
8 *
9 * Rewrote probe routine to work on newer Mitsumi drives.
10 * Additional changes (C) 1994 by Jordan K. Hubbard
11 *
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This software was developed by Holger Veit and Brian Moore
25 * for use with "386BSD" and similar operating systems.
26 * "Similar operating systems" includes mainly non-profit oriented
27 * systems for research and education, including but not restricted to
28 * "NetBSD", "FreeBSD", "Mach" (by CMU).
29 * 4. Neither the name of the developer(s) nor the name "386BSD"
30 * may be used to endorse or promote products derived from this
31 * software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
34 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
37 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
38 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
39 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
40 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
45 * $FreeBSD: head/sys/dev/mcd/mcd.c 104445 2002-10-04 07:14:19Z mdodd $
46 */
47static const char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/kernel.h>
52#include <sys/conf.h>
53#include <sys/fcntl.h>
54#include <sys/bio.h>
55#include <sys/cdio.h>
56#include <sys/disklabel.h>
57#include <sys/bus.h>
58
59#include <machine/bus_pio.h>
60#include <machine/bus.h>
61#include <machine/resource.h>
62#include <sys/rman.h>
63
64#include <isa/isavar.h>
65
66#include <dev/mcd/mcdreg.h>
67#include <dev/mcd/mcdvar.h>
68
69#define MCD_TRACE(format, args...) \
70{ \
71 if (sc->debug) { \
72 device_printf(sc->dev, "status=0x%02x: ", \
73 sc->data.status); \
74 printf(format, ## args); \
75 } \
76}
77
78#define mcd_part(dev) ((minor(dev)) & 7)
79#define mcd_unit(dev) (((minor(dev)) & 0x38) >> 3)
80#define mcd_phys(dev) (((minor(dev)) & 0x40) >> 6)
81#define RAW_PART 2
82
83/* flags */
84#define MCDVALID 0x0001 /* parameters loaded */
85#define MCDINIT 0x0002 /* device is init'd */
86#define MCDNEWMODEL 0x0004 /* device is new model */
87#define MCDLABEL 0x0008 /* label is read */
88#define MCDPROBING 0x0010 /* probing */
89#define MCDREADRAW 0x0020 /* read raw mode (2352 bytes) */
90#define MCDVOLINFO 0x0040 /* already read volinfo */
91#define MCDTOC 0x0080 /* already read toc */
92#define MCDMBXBSY 0x0100 /* local mbx is busy */
93
94/* status */
95#define MCDAUDIOBSY MCD_ST_AUDIOBSY /* playing audio */
96#define MCDDSKCHNG MCD_ST_DSKCHNG /* sensed change of disk */
97#define MCDDSKIN MCD_ST_DSKIN /* sensed disk in drive */
98#define MCDDOOROPEN MCD_ST_DOOROPEN /* sensed door open */
99
100/* These are apparently the different states a mitsumi can get up to */
101#define MCDCDABSENT 0x0030
102#define MCDCDPRESENT 0x0020
103#define MCDSCLOSED 0x0080
104#define MCDSOPEN 0x00a0
105
106#define MCD_MD_UNKNOWN (-1)
107
108#define MCD_TYPE_UNKNOWN 0
109#define MCD_TYPE_LU002S 1
110#define MCD_TYPE_LU005S 2
111#define MCD_TYPE_LU006S 3
112#define MCD_TYPE_FX001 4
113#define MCD_TYPE_FX001D 5
114
115/* reader state machine */
116#define MCD_S_BEGIN 0
117#define MCD_S_BEGIN1 1
118#define MCD_S_WAITSTAT 2
119#define MCD_S_WAITMODE 3
120#define MCD_S_WAITREAD 4
121
122/* prototypes */
123static void mcd_start(struct mcd_softc *);
124static int mcd_getdisklabel(struct mcd_softc *);
125#ifdef NOTYET
126static void mcd_configure(struct mcd_softc *sc);
127#endif
128static int mcd_get(struct mcd_softc *, char *buf, int nmax);
129static int mcd_setflags(struct mcd_softc *);
130static int mcd_getstat(struct mcd_softc *,int sflg);
131static int mcd_send(struct mcd_softc *, int cmd,int nretrys);
132static void hsg2msf(int hsg, bcd_t *msf);
133static int msf2hsg(bcd_t *msf, int relative);
134static int mcd_volinfo(struct mcd_softc *);
135static int mcd_waitrdy(struct mcd_softc *,int dly);
136static timeout_t mcd_timeout;
137static void mcd_doread(struct mcd_softc *, int state, struct mcd_mbx *mbxin);
138static void mcd_soft_reset(struct mcd_softc *);
139static int mcd_hard_reset(struct mcd_softc *);
140static int mcd_setmode(struct mcd_softc *, int mode);
141static int mcd_getqchan(struct mcd_softc *, struct mcd_qchninfo *q);
142static int mcd_subchan(struct mcd_softc *, struct ioc_read_subchannel *sc);
143static int mcd_toc_header(struct mcd_softc *, struct ioc_toc_header *th);
144static int mcd_read_toc(struct mcd_softc *);
145static int mcd_toc_entrys(struct mcd_softc *, struct ioc_read_toc_entry *te);
146#if 0
147static int mcd_toc_entry(struct mcd_softc *, struct ioc_read_toc_single_entry *te);
148#endif
149static int mcd_stop(struct mcd_softc *);
150static int mcd_eject(struct mcd_softc *);
151static int mcd_inject(struct mcd_softc *);
152static int mcd_playtracks(struct mcd_softc *, struct ioc_play_track *pt);
153static int mcd_play(struct mcd_softc *, struct mcd_read2 *pb);
154static int mcd_playmsf(struct mcd_softc *, struct ioc_play_msf *pt);
155static int mcd_playblocks(struct mcd_softc *, struct ioc_play_blocks *);
156static int mcd_pause(struct mcd_softc *);
157static int mcd_resume(struct mcd_softc *);
158static int mcd_lock_door(struct mcd_softc *, int lock);
159static int mcd_close_tray(struct mcd_softc *);
160
161static d_open_t mcdopen;
162static d_close_t mcdclose;
163static d_ioctl_t mcdioctl;
164static d_psize_t mcdsize;
165static d_strategy_t mcdstrategy;
166
167#define CDEV_MAJOR 29
168
169static struct cdevsw mcd_cdevsw = {
170 /* open */ mcdopen,
171 /* close */ mcdclose,
172 /* read */ physread,
173 /* write */ nowrite,
174 /* ioctl */ mcdioctl,
175 /* poll */ nopoll,
176 /* mmap */ nommap,
177 /* strategy */ mcdstrategy,
178 /* name */ "mcd",
179 /* maj */ CDEV_MAJOR,
180 /* dump */ nodump,
181 /* psize */ nopsize,
182 /* flags */ D_DISK,
183};
184
185#define MCD_RETRYS 5
186#define MCD_RDRETRYS 8
187
188#define CLOSE_TRAY_SECS 8
189#define DISK_SENSE_SECS 3
190#define WAIT_FRAC 4
191
192/* several delays */
193#define RDELAY_WAITSTAT 300
194#define RDELAY_WAITMODE 300
195#define RDELAY_WAITREAD 800
196
197#define MIN_DELAY 15
198#define DELAY_GETREPLY 5000000
199
200int
201mcd_attach(struct mcd_softc *sc)
202{
203 struct mcd_data *cd;
204 int unit;
205
206 cd = &sc->data;
207 unit = device_get_unit(sc->dev);
208
209 cd->flags |= MCDINIT;
210 mcd_soft_reset(sc);
211 bioq_init(&cd->head);
212
213#ifdef NOTYET
214 /* wire controller for interrupts and dma */
215 mcd_configure(sc);
216#endif
217 /* name filled in probe */
218 sc->mcd_dev_t[0] = make_dev(&mcd_cdevsw, 0,
219 UID_ROOT, GID_OPERATOR, 0640, "mcd%d", unit);
220 sc->mcd_dev_t[1] = make_dev_alias(sc->mcd_dev_t[0], "mcd%da", unit);
221 sc->mcd_dev_t[2] = make_dev_alias(sc->mcd_dev_t[0], "mcd%dc", unit);
222
223 sc->mcd_dev_t[0]->si_drv1 = sc->mcd_dev_t[1]->si_drv1 =
224 sc->mcd_dev_t[2]->si_drv1 = (void *)sc;
225
226 return 0;
227}
228
229static int
230mcdopen(dev_t dev, int flags, int fmt, struct thread *td)
231{
232 struct mcd_softc *sc;
233 struct mcd_data *cd;
234 int part,phys,r,retry;
235
236 sc = (struct mcd_softc *)dev->si_drv1;
237 cd = &sc->data;
238 part = mcd_part(dev);
239 phys = mcd_phys(dev);
240
241 /* not initialized*/
242 if (!(cd->flags & MCDINIT))
243 return ENXIO;
244
245 /* invalidated in the meantime? mark all open part's invalid */
246 if (!(cd->flags & MCDVALID) && cd->openflags)
247 return ENXIO;
248
249 if (mcd_getstat(sc, 1) == -1)
250 return EIO;
251
252 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
253 || !(cd->status & MCDDSKIN))
254 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
255 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdsn1", hz/WAIT_FRAC);
256 if ((r = mcd_getstat(sc, 1)) == -1)
257 return EIO;
258 if (r != -2)
259 break;
260 }
261
262 if (( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
263 || !(cd->status & MCDDSKIN)
264 )
265 && major(dev) == CDEV_MAJOR && part == RAW_PART
266 ) {
267 cd->openflags |= (1<<part);
268 if (phys)
269 cd->partflags[part] |= MCDREADRAW;
270 return 0;
271 }
272 if (cd->status & MCDDOOROPEN) {
273 device_printf(sc->dev, "door is open\n");
274 return ENXIO;
275 }
276 if (!(cd->status & MCDDSKIN)) {
277 device_printf(sc->dev, "no CD inside\n");
278 return ENXIO;
279 }
280 if (cd->status & MCDDSKCHNG) {
281 device_printf(sc->dev, "CD not sensed\n");
282 return ENXIO;
283 }
284
285 if (mcdsize(dev) < 0) {
286 if (major(dev) == CDEV_MAJOR && part == RAW_PART) {
287 cd->openflags |= (1<<part);
288 if (phys)
289 cd->partflags[part] |= MCDREADRAW;
290 return 0;
291 }
292 device_printf(sc->dev, "failed to get disk size\n");
293 return ENXIO;
294 } else
295 cd->flags |= MCDVALID;
296
297 /* XXX get a default disklabel */
298 mcd_getdisklabel(sc);
299
300MCD_TRACE("open: partition=%d, disksize = %ld, blksize=%d\n",
301 part, cd->disksize, cd->blksize);
302
303 dev->si_bsize_phys = cd->blksize;
304
305 if (part == RAW_PART ||
306 (part < cd->dlabel.d_npartitions &&
307 cd->dlabel.d_partitions[part].p_fstype != FS_UNUSED)) {
308 cd->openflags |= (1<<part);
309 if (part == RAW_PART && phys)
310 cd->partflags[part] |= MCDREADRAW;
311 (void) mcd_lock_door(sc, MCD_LK_LOCK);
312 if (!(cd->flags & MCDVALID))
313 return ENXIO;
314 return 0;
315 }
316
317 return ENXIO;
318}
319
320static int
321mcdclose(dev_t dev, int flags, int fmt, struct thread *td)
322{
323 struct mcd_softc *sc;
324 struct mcd_data *cd;
325 int part;
326
327 sc = (struct mcd_softc *)dev->si_drv1;
328 cd = &sc->data;
329 part = mcd_part(dev);
330
331 if (!(cd->flags & MCDINIT) || !(cd->openflags & (1<<part)))
332 return ENXIO;
333
334 MCD_TRACE("close: partition=%d\n", part);
335
336 (void) mcd_lock_door(sc, MCD_LK_UNLOCK);
337 cd->openflags &= ~(1<<part);
338 cd->partflags[part] &= ~MCDREADRAW;
339
340 return 0;
341}
342
343static void
344mcdstrategy(struct bio *bp)
345{
346 struct mcd_softc *sc;
347 struct mcd_data *cd;
348 int s;
349
350 int unit = mcd_unit(bp->bio_dev);
351
352 sc = (struct mcd_softc *)bp->bio_dev->si_drv1;
353 cd = &sc->data;
354
355 /* test validity */
356/*MCD_TRACE("strategy: buf=0x%lx, unit=%ld, block#=%ld bcount=%ld\n",
357 bp,unit,bp->bio_blkno,bp->bio_bcount);*/
358 if (bp->bio_blkno < 0) {
359 printf("mcdstrategy: unit = %d, blkno = %ld, bcount = %ld\n",
360 unit, (long)bp->bio_blkno, bp->bio_bcount);
361 printf("mcd: mcdstratregy failure");
362 bp->bio_error = EINVAL;
363 bp->bio_flags |= BIO_ERROR;
364 goto bad;
365 }
366
367 /* if device invalidated (e.g. media change, door open), error */
368 if (!(cd->flags & MCDVALID)) {
369MCD_TRACE("strategy: drive not valid\n");
370 bp->bio_error = EIO;
371 goto bad;
372 }
373
374 /* read only */
375 if (!(bp->bio_cmd == BIO_READ)) {
376 bp->bio_error = EROFS;
377 goto bad;
378 }
379
380 /* no data to read */
381 if (bp->bio_bcount == 0)
382 goto done;
383
384 /* for non raw access, check partition limits */
385 if (mcd_part(bp->bio_dev) != RAW_PART) {
386 if (!(cd->flags & MCDLABEL)) {
387 bp->bio_error = EIO;
388 goto bad;
389 }
390 /* adjust transfer if necessary */
391 if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0) {
392 goto done;
393 }
394 } else {
395 bp->bio_pblkno = bp->bio_blkno;
396 bp->bio_resid = 0;
397 }
398
399 /* queue it */
400 s = splbio();
401 bioqdisksort(&cd->head, bp);
402 splx(s);
403
404 /* now check whether we can perform processing */
405 mcd_start(sc);
406 return;
407
408bad:
409 bp->bio_flags |= BIO_ERROR;
410done:
411 bp->bio_resid = bp->bio_bcount;
412 biodone(bp);
413 return;
414}
415
416static void
417mcd_start(struct mcd_softc *sc)
418{
419 struct mcd_data *cd = &sc->data;
420 struct partition *p;
421 struct bio *bp;
422 int s = splbio();
423
424 if (cd->flags & MCDMBXBSY) {
425 splx(s);
426 return;
427 }
428
429 bp = bioq_first(&cd->head);
430 if (bp != 0) {
431 /* block found to process, dequeue */
432 /*MCD_TRACE("mcd_start: found block bp=0x%x\n",bp,0,0,0);*/
433 bioq_remove(&cd->head, bp);
434 splx(s);
435 } else {
436 /* nothing to do */
437 splx(s);
438 return;
439 }
440
441 /* changed media? */
442 if (!(cd->flags & MCDVALID)) {
443 MCD_TRACE("mcd_start: drive not valid\n");
444 return;
445 }
446
447 p = cd->dlabel.d_partitions + mcd_part(bp->bio_dev);
448
449 cd->flags |= MCDMBXBSY;
450 if (cd->partflags[mcd_part(bp->bio_dev)] & MCDREADRAW)
451 cd->flags |= MCDREADRAW;
452 cd->mbx.unit = device_get_unit(sc->dev);
453 cd->mbx.retry = MCD_RETRYS;
454 cd->mbx.bp = bp;
455 cd->mbx.p_offset = p->p_offset;
456
457 /* calling the read routine */
458 mcd_doread(sc, MCD_S_BEGIN,&(cd->mbx));
459 /* triggers mcd_start, when successful finished */
460 return;
461}
462
463static int
464mcdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
465{
466 struct mcd_softc *sc;
467 struct mcd_data *cd;
468 int unit,part,retry,r;
469
470 unit = mcd_unit(dev);
471 part = mcd_part(dev);
472 sc = (struct mcd_softc *)dev->si_drv1;
473 cd = &sc->data;
474
475 if (mcd_getstat(sc, 1) == -1) /* detect disk change too */
476 return EIO;
477MCD_TRACE("ioctl called 0x%lx\n", cmd);
478
479 switch (cmd) {
480 case CDIOCSETPATCH:
481 case CDIOCGETVOL:
482 case CDIOCSETVOL:
483 case CDIOCSETMONO:
484 case CDIOCSETSTERIO:
485 case CDIOCSETMUTE:
486 case CDIOCSETLEFT:
487 case CDIOCSETRIGHT:
488 return EINVAL;
489 case CDIOCEJECT:
490 return mcd_eject(sc);
491 case CDIOCSETDEBUG:
492 cd->debug = 1;
493 return 0;
494 case CDIOCCLRDEBUG:
495 cd->debug = 0;
496 return 0;
497 case CDIOCRESET:
498 return mcd_hard_reset(sc);
499 case CDIOCALLOW:
500 return mcd_lock_door(sc, MCD_LK_UNLOCK);
501 case CDIOCPREVENT:
502 return mcd_lock_door(sc, MCD_LK_LOCK);
503 case CDIOCCLOSE:
504 return mcd_inject(sc);
505 }
506
507 if (!(cd->flags & MCDVALID)) {
508 if ( major(dev) != CDEV_MAJOR
509 || part != RAW_PART
510 || !(cd->openflags & (1<<RAW_PART))
511 )
512 return ENXIO;
513 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
514 || !(cd->status & MCDDSKIN))
515 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
516 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdsn2", hz/WAIT_FRAC);
517 if ((r = mcd_getstat(sc, 1)) == -1)
518 return EIO;
519 if (r != -2)
520 break;
521 }
522 if ( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
523 || !(cd->status & MCDDSKIN)
524 || mcdsize(dev) < 0
525 )
526 return ENXIO;
527 cd->flags |= MCDVALID;
528 mcd_getdisklabel(sc);
529 if (mcd_phys(dev))
530 cd->partflags[part] |= MCDREADRAW;
531 (void) mcd_lock_door(sc, MCD_LK_LOCK);
532 if (!(cd->flags & MCDVALID))
533 return ENXIO;
534 }
535
536 switch (cmd) {
537 case DIOCGDINFO:
538 *(struct disklabel *) addr = cd->dlabel;
539 return 0;
540 /*
541 * a bit silly, but someone might want to test something on a
542 * section of cdrom.
543 */
544 case DIOCWDINFO:
545 case DIOCSDINFO:
546 if ((flags & FWRITE) == 0)
547 return EBADF;
548 else {
549 return setdisklabel(&cd->dlabel,
550 (struct disklabel *) addr,
551 0);
552 }
553 case DIOCWLABEL:
554 return EBADF;
555 case CDIOCPLAYTRACKS:
556 return mcd_playtracks(sc, (struct ioc_play_track *) addr);
557 case CDIOCPLAYBLOCKS:
558 return mcd_playblocks(sc, (struct ioc_play_blocks *) addr);
559 case CDIOCPLAYMSF:
560 return mcd_playmsf(sc, (struct ioc_play_msf *) addr);
561 case CDIOCREADSUBCHANNEL:
562 return mcd_subchan(sc, (struct ioc_read_subchannel *) addr);
563 case CDIOREADTOCHEADER:
564 return mcd_toc_header(sc, (struct ioc_toc_header *) addr);
565 case CDIOREADTOCENTRYS:
566 return mcd_toc_entrys(sc, (struct ioc_read_toc_entry *) addr);
567 case CDIOCRESUME:
568 return mcd_resume(sc);
569 case CDIOCPAUSE:
570 return mcd_pause(sc);
571 case CDIOCSTART:
572 if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
573 return EIO;
574 return 0;
575 case CDIOCSTOP:
576 return mcd_stop(sc);
577 default:
578 return ENOTTY;
579 }
580 /*NOTREACHED*/
581}
582
583/* this could have been taken from scsi/cd.c, but it is not clear
584 * whether the scsi cd driver is linked in
585 */
586static int
587mcd_getdisklabel(struct mcd_softc *sc)
588{
589 struct mcd_data *cd = &sc->data;
590
591 if (cd->flags & MCDLABEL)
592 return -1;
593
594 bzero(&cd->dlabel,sizeof(struct disklabel));
595 /* filled with spaces first */
596 strncpy(cd->dlabel.d_typename," ",
597 sizeof(cd->dlabel.d_typename));
598 strncpy(cd->dlabel.d_typename, cd->name,
599 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
600 strncpy(cd->dlabel.d_packname,"unknown ",
601 sizeof(cd->dlabel.d_packname));
602 cd->dlabel.d_secsize = cd->blksize;
603 cd->dlabel.d_nsectors = 100;
604 cd->dlabel.d_ntracks = 1;
605 cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
606 cd->dlabel.d_secpercyl = 100;
607 cd->dlabel.d_secperunit = cd->disksize;
608 cd->dlabel.d_rpm = 300;
609 cd->dlabel.d_interleave = 1;
610 cd->dlabel.d_flags = D_REMOVABLE;
611 cd->dlabel.d_npartitions= 1;
612 cd->dlabel.d_partitions[0].p_offset = 0;
613 cd->dlabel.d_partitions[0].p_size = cd->disksize;
614 cd->dlabel.d_partitions[0].p_fstype = 9;
615 cd->dlabel.d_magic = DISKMAGIC;
616 cd->dlabel.d_magic2 = DISKMAGIC;
617 cd->dlabel.d_checksum = dkcksum(&cd->dlabel);
618
619 cd->flags |= MCDLABEL;
620 return 0;
621}
622
623static int
624mcdsize(dev_t dev)
625{
626 struct mcd_softc *sc;
627 struct mcd_data *cd;
628 int size;
629 int unit;
630
631 unit = mcd_unit(dev);
632 sc = (struct mcd_softc *)dev->si_drv1;
633 cd = &sc->data;
634
635 if (mcd_volinfo(sc) == 0) {
636 cd->blksize = MCDBLK;
637 size = msf2hsg(cd->volinfo.vol_msf, 0);
638 cd->disksize = size * (MCDBLK/DEV_BSIZE);
639 return 0;
640 }
641 return -1;
642}
643
644/***************************************************************
645 * lower level of driver starts here
646 **************************************************************/
647
648#ifdef NOTDEF
649static char
650irqs[] = {
651 0x00,0x00,0x10,0x20,0x00,0x30,0x00,0x00,
652 0x00,0x10,0x40,0x50,0x00,0x00,0x00,0x00
653};
654
655static char
656drqs[] = {
657 0x00,0x01,0x00,0x03,0x00,0x05,0x06,0x07,
658};
659#endif
660
661#ifdef NOT_YET
662static void
663mcd_configure(struct mcd_softc *sc)
664{
665 MCD_WRITE(sc, MCD_REG_CONFIG, sc->data.config);
666}
667#endif
668
669/* Wait for non-busy - return 0 on timeout */
670static int
671twiddle_thumbs(struct mcd_softc *sc, int count, char *whine)
672{
673 int i;
674
675 for (i = 0; i < count; i++) {
676 if (!(MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
677 return 1;
678 }
679 if (bootverbose)
680 device_printf(sc->dev, "timeout %s\n", whine);
681 return 0;
682}
683
684/* check to see if a Mitsumi CD-ROM is attached to the ISA bus */
685
686int
687mcd_probe(struct mcd_softc *sc)
688{
689 int unit;
690 int i, j;
691 unsigned char stbytes[3];
692
693 unit = device_get_unit(sc->dev);
694 sc->data.flags = MCDPROBING;
695
696#ifdef NOTDEF
697 /* get irq/drq configuration word */
698 sc->data.config = irqs[dev->id_irq]; /* | drqs[dev->id_drq];*/
699#else
700 sc->data.config = 0;
701#endif
702
703 /* send a reset */
704 MCD_WRITE(sc, MCD_FLAGS, M_RESET);
705
706 /*
707 * delay awhile by getting any pending garbage (old data) and
708 * throwing it away.
709 */
710 for (i = 1000000; i != 0; i--)
711 (void)MCD_READ(sc, MCD_FLAGS);
712
713 /* Get status */
714 MCD_WRITE(sc, MCD_DATA, MCD_CMDGETSTAT);
715 if (!twiddle_thumbs(sc, 1000000, "getting status"))
716 return (ENXIO); /* Timeout */
717 /* Get version information */
718 MCD_WRITE(sc, MCD_DATA, MCD_CMDCONTINFO);
719 for (j = 0; j < 3; j++) {
720 if (!twiddle_thumbs(sc, 3000, "getting version info"))
721 return (ENXIO);
722 stbytes[j] = (MCD_READ(sc, MCD_DATA) & 0xFF);
723 }
724 if (stbytes[1] == stbytes[2])
725 return (ENXIO);
726 if (stbytes[2] >= 4 || stbytes[1] != 'M') {
727 MCD_WRITE(sc, MCD_CTRL, M_PICKLE);
728 sc->data.flags |= MCDNEWMODEL;
729 }
730 sc->data.read_command = MCD_CMDSINGLESPEEDREAD;
731 switch (stbytes[1]) {
732 case 'M':
733 if (stbytes[2] <= 2) {
734 sc->data.type = MCD_TYPE_LU002S;
735 sc->data.name = "Mitsumi LU002S";
736 } else if (stbytes[2] <= 5) {
737 sc->data.type = MCD_TYPE_LU005S;
738 sc->data.name = "Mitsumi LU005S";
739 } else {
740 sc->data.type = MCD_TYPE_LU006S;
741 sc->data.name = "Mitsumi LU006S";
742 }
743 break;
744 case 'F':
745 sc->data.type = MCD_TYPE_FX001;
746 sc->data.name = "Mitsumi FX001";
747 break;
748 case 'D':
749 sc->data.type = MCD_TYPE_FX001D;
750 sc->data.name = "Mitsumi FX001D";
751 sc->data.read_command = MCD_CMDDOUBLESPEEDREAD;
752 break;
753 default:
754 sc->data.type = MCD_TYPE_UNKNOWN;
755 sc->data.name = "Mitsumi ???";
756 break;
757 }
758
759 if (bootverbose)
760 device_printf(sc->dev, "type %s, version info: %c %x\n",
761 sc->data.name, stbytes[1], stbytes[2]);
762
763 return 0;
764}
765
766
767static int
768mcd_waitrdy(struct mcd_softc *sc, int dly)
769{
770 int i;
771
772 /* wait until flag port senses status ready */
773 for (i=0; i<dly; i+=MIN_DELAY) {
774 if (!(MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
775 return 0;
776 DELAY(MIN_DELAY);
777 }
778 return -1;
779}
780
781static int
782mcd_getreply(struct mcd_softc *sc, int dly)
783{
784
785 /* wait data to become ready */
786 if (mcd_waitrdy(sc, dly)<0) {
787 device_printf(sc->dev, "timeout getreply\n");
788 return -1;
789 }
790
791 /* get the data */
792 return MCD_READ(sc, MCD_REG_STATUS) & 0xFF;
793}
794
795static int
796mcd_getstat(struct mcd_softc *sc, int sflg)
797{
798 int i;
799 struct mcd_data *cd = &sc->data;
800
801 /* get the status */
802 if (sflg)
803 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDGETSTAT);
804 i = mcd_getreply(sc, DELAY_GETREPLY);
805 if (i<0 || (i & MCD_ST_CMDCHECK)) {
806 cd->curr_mode = MCD_MD_UNKNOWN;
807 return -1;
808 }
809
810 cd->status = i;
811
812 if (mcd_setflags(sc) < 0)
813 return -2;
814 return cd->status;
815}
816
817static int
818mcd_setflags(struct mcd_softc *sc)
819{
820 struct mcd_data *cd = &sc->data;
821
822 /* check flags */
823 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
824 || !(cd->status & MCDDSKIN)) {
825 MCD_TRACE("setflags: sensed DSKCHNG or DOOROPEN or !DSKIN\n");
826 mcd_soft_reset(sc);
827 return -1;
828 }
829
830 if (cd->status & MCDAUDIOBSY)
831 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
832 else if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
833 cd->audio_status = CD_AS_PLAY_COMPLETED;
834 return 0;
835}
836
837static int
838mcd_get(struct mcd_softc *sc, char *buf, int nmax)
839{
840 int i,k;
841
842 for (i=0; i<nmax; i++) {
843 /* wait for data */
844 if ((k = mcd_getreply(sc, DELAY_GETREPLY)) < 0) {
845 device_printf(sc->dev, "timeout mcd_get\n");
846 return -1;
847 }
848 buf[i] = k;
849 }
850 return i;
851}
852
853static int
854mcd_send(struct mcd_softc *sc, int cmd,int nretrys)
855{
856 int i,k=0;
857
858/*MCD_TRACE("mcd_send: command = 0x%02x\n",cmd,0,0,0);*/
859 for (i=0; i<nretrys; i++) {
860 MCD_WRITE(sc, MCD_REG_COMMAND, cmd);
861 if ((k=mcd_getstat(sc, 0)) != -1)
862 break;
863 }
864 if (k == -2) {
865 device_printf(sc->dev, "media changed\n");
866 return -1;
867 }
868 if (i == nretrys) {
869 device_printf(sc->dev, "mcd_send retry cnt exceeded\n");
870 return -1;
871 }
872/*MCD_TRACE("mcd_send: done\n",0,0,0,0);*/
873 return 0;
874}
875
876static void
877hsg2msf(int hsg, bcd_t *msf)
878{
879 hsg += 150;
880 F_msf(msf) = bin2bcd(hsg % 75);
881 hsg /= 75;
882 S_msf(msf) = bin2bcd(hsg % 60);
883 hsg /= 60;
884 M_msf(msf) = bin2bcd(hsg);
885}
886
887static int
888msf2hsg(bcd_t *msf, int relative)
889{
890 return (bcd2bin(M_msf(msf)) * 60 + bcd2bin(S_msf(msf))) * 75 +
891 bcd2bin(F_msf(msf)) - (!relative) * 150;
892}
893
894static int
895mcd_volinfo(struct mcd_softc *sc)
896{
897 struct mcd_data *cd = &sc->data;
898
899 /* Just return if we already have it */
900 if (cd->flags & MCDVOLINFO) return 0;
901
902/*MCD_TRACE("mcd_volinfo: enter\n",0,0,0,0);*/
903
904 /* send volume info command */
905 if (mcd_send(sc, MCD_CMDGETVOLINFO,MCD_RETRYS) < 0)
906 return EIO;
907
908 /* get data */
909 if (mcd_get(sc, (char*) &cd->volinfo,sizeof(struct mcd_volinfo)) < 0) {
910 device_printf(sc->dev, "mcd_volinfo: error read data\n");
911 return EIO;
912 }
913
914 if (cd->volinfo.trk_low > 0 &&
915 cd->volinfo.trk_high >= cd->volinfo.trk_low
916 ) {
917 cd->flags |= MCDVOLINFO; /* volinfo is OK */
918 return 0;
919 }
920
921 return EINVAL;
922}
923
924/* state machine to process read requests
925 * initialize with MCD_S_BEGIN: calculate sizes, and read status
926 * MCD_S_WAITSTAT: wait for status reply, set mode
927 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
928 * MCD_S_WAITREAD: wait for read ready, read data
929 */
930static void
931mcd_timeout(void *arg)
932{
933 struct mcd_softc *sc;
934
935 sc = (struct mcd_softc *)arg;
936
937 mcd_doread(sc, sc->ch_state, sc->ch_mbxsave);
938}
939
940static void
941mcd_doread(struct mcd_softc *sc, int state, struct mcd_mbx *mbxin)
942{
943 struct mcd_mbx *mbx;
944 struct bio *bp;
945 struct mcd_data *cd;
946 int rm, i, k;
947 struct mcd_read2 rbuf;
948 int blknum;
949 caddr_t addr;
950
951 mbx = (state!=MCD_S_BEGIN) ? sc->ch_mbxsave : mbxin;
952 bp = mbx->bp;
953 cd = &sc->data;
954
955loop:
956 switch (state) {
957 case MCD_S_BEGIN:
958 mbx = sc->ch_mbxsave = mbxin;
959
960 case MCD_S_BEGIN1:
961retry_status:
962 /* get status */
963 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDGETSTAT);
964 mbx->count = RDELAY_WAITSTAT;
965 sc->ch_state = MCD_S_WAITSTAT;
966 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */
967 return;
968 case MCD_S_WAITSTAT:
969 sc->ch_state = MCD_S_WAITSTAT;
970 untimeout(mcd_timeout,(caddr_t)sc, sc->ch);
971 if (mbx->count-- >= 0) {
972 if (MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
973 sc->ch_state = MCD_S_WAITSTAT;
974 timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */
975 return;
976 }
977 cd->status = MCD_READ(sc, MCD_REG_STATUS) & 0xFF;
978 if (cd->status & MCD_ST_CMDCHECK)
979 goto retry_status;
980 if (mcd_setflags(sc) < 0)
981 goto changed;
982 MCD_TRACE("got WAITSTAT delay=%d\n",
983 RDELAY_WAITSTAT-mbx->count);
984 /* reject, if audio active */
985 if (cd->status & MCDAUDIOBSY) {
986 device_printf(sc->dev, "audio is active\n");
987 goto readerr;
988 }
989
990retry_mode:
991 /* to check for raw/cooked mode */
992 if (cd->flags & MCDREADRAW) {
993 rm = MCD_MD_RAW;
994 mbx->sz = MCDRBLK;
995 } else {
996 rm = MCD_MD_COOKED;
997 mbx->sz = cd->blksize;
998 }
999
1000 if (rm == cd->curr_mode)
1001 goto modedone;
1002
1003 mbx->count = RDELAY_WAITMODE;
1004
1005 cd->curr_mode = MCD_MD_UNKNOWN;
1006 mbx->mode = rm;
1007 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDSETMODE);
1008 MCD_WRITE(sc, MCD_REG_COMMAND, rm);
1009
1010 sc->ch_state = MCD_S_WAITMODE;
1011 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */
1012 return;
1013 } else {
1014 device_printf(sc->dev, "timeout getstatus\n");
1015 goto readerr;
1016 }
1017
1018 case MCD_S_WAITMODE:
1019 sc->ch_state = MCD_S_WAITMODE;
1020 untimeout(mcd_timeout, (caddr_t)sc, sc->ch);
1021 if (mbx->count-- < 0) {
1022 device_printf(sc->dev, "timeout set mode\n");
1023 goto readerr;
1024 }
1025 if (MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1026 sc->ch_state = MCD_S_WAITMODE;
1027 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100);
1028 return;
1029 }
1030 cd->status = MCD_READ(sc, MCD_REG_STATUS) & 0xFF;
1031 if (cd->status & MCD_ST_CMDCHECK) {
1032 cd->curr_mode = MCD_MD_UNKNOWN;
1033 goto retry_mode;
1034 }
1035 if (mcd_setflags(sc) < 0)
1036 goto changed;
1037 cd->curr_mode = mbx->mode;
1038 MCD_TRACE("got WAITMODE delay=%d\n",
1039 RDELAY_WAITMODE-mbx->count);
1040modedone:
1041 /* for first block */
1042 mbx->nblk = (bp->bio_bcount + (mbx->sz-1)) / mbx->sz;
1043 mbx->skip = 0;
1044
1045nextblock:
1046 blknum = (bp->bio_blkno / (mbx->sz/DEV_BSIZE))
1047 + mbx->p_offset + mbx->skip/mbx->sz;
1048
1049 MCD_TRACE("mcd_doread: read blknum=%d for bp=%p\n",
1050 blknum, bp);
1051
1052 /* build parameter block */
1053 hsg2msf(blknum,rbuf.start_msf);
1054retry_read:
1055 /* send the read command */
1056 disable_intr();
1057 MCD_WRITE(sc, MCD_REG_COMMAND, cd->read_command);
1058 MCD_WRITE(sc, MCD_REG_COMMAND, rbuf.start_msf[0]);
1059 MCD_WRITE(sc, MCD_REG_COMMAND, rbuf.start_msf[1]);
1060 MCD_WRITE(sc, MCD_REG_COMMAND, rbuf.start_msf[2]);
1061 MCD_WRITE(sc, MCD_REG_COMMAND, 0);
1062 MCD_WRITE(sc, MCD_REG_COMMAND, 0);
1063 MCD_WRITE(sc, MCD_REG_COMMAND, 1);
1064 enable_intr();
1065
1066 /* Spin briefly (<= 2ms) to avoid missing next block */
1067 for (i = 0; i < 20; i++) {
1068 k = MCD_READ(sc, MCD_FLAGS);
1069 if (!(k & MFL_DATA_NOT_AVAIL))
1070 goto got_it;
1071 DELAY(100);
1072 }
1073
1074 mbx->count = RDELAY_WAITREAD;
1075 sc->ch_state = MCD_S_WAITREAD;
1076 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */
1077 return;
1078 case MCD_S_WAITREAD:
1079 sc->ch_state = MCD_S_WAITREAD;
1080 untimeout(mcd_timeout, (caddr_t)sc, sc->ch);
1081 if (mbx->count-- > 0) {
1082 k = MCD_READ(sc, MCD_FLAGS);
1083 if (!(k & MFL_DATA_NOT_AVAIL)) { /* XXX */
1084 MCD_TRACE("got data delay=%d\n",
1085 RDELAY_WAITREAD-mbx->count);
1086 got_it:
1087 /* data is ready */
1088 addr = bp->bio_data + mbx->skip;
1089
1090 MCD_WRITE(sc, MCD_REG_CTL2,0x04); /* XXX */
1091 for (i=0; i<mbx->sz; i++)
1092 *addr++ = MCD_READ(sc, MCD_REG_RDATA);
1093 MCD_WRITE(sc, MCD_REG_CTL2,0x0c); /* XXX */
1094
1095 k = MCD_READ(sc, MCD_FLAGS);
1096 /* If we still have some junk, read it too */
1097 if (!(k & MFL_DATA_NOT_AVAIL)) {
1098 MCD_WRITE(sc, MCD_REG_CTL2, 0x04); /* XXX */
1099 (void)MCD_READ(sc, MCD_REG_RDATA);
1100 (void)MCD_READ(sc, MCD_REG_RDATA);
1101 MCD_WRITE(sc, MCD_REG_CTL2, 0x0c); /* XXX */
1102 }
1103
1104 if (--mbx->nblk > 0) {
1105 mbx->skip += mbx->sz;
1106 goto nextblock;
1107 }
1108
1109 /* return buffer */
1110 bp->bio_resid = 0;
1111 biodone(bp);
1112
1113 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1114 mcd_start(sc);
1115 return;
1116 }
1117 if (!(k & MFL_STATUS_NOT_AVAIL)) {
1118 cd->status = MCD_READ(sc, MCD_REG_STATUS) & 0xFF;
1119 if (cd->status & MCD_ST_CMDCHECK)
1120 goto retry_read;
1121 if (mcd_setflags(sc) < 0)
1122 goto changed;
1123 }
1124 sc->ch_state = MCD_S_WAITREAD;
1125 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */
1126 return;
1127 } else {
1128 device_printf(sc->dev, "timeout read data\n");
1129 goto readerr;
1130 }
1131 }
1132
1133readerr:
1134 if (mbx->retry-- > 0) {
1135 device_printf(sc->dev, "retrying\n");
1136 state = MCD_S_BEGIN1;
1137 goto loop;
1138 }
1139harderr:
1140 /* invalidate the buffer */
1141 bp->bio_flags |= BIO_ERROR;
1142 bp->bio_resid = bp->bio_bcount;
1143 biodone(bp);
1144
1145 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1146 mcd_start(sc);
1147 return;
1148
1149changed:
1150 device_printf(sc->dev, "media changed\n");
1151 goto harderr;
1152
1153#ifdef NOTDEF
1154 device_printf(sc->dev, "unit timeout, resetting\n");
1155 MCD_WRITE(sc, MCD_REG_RESET, MCD_CMDRESET);
1156 DELAY(300000);
1157 (void)mcd_getstat(sc, 1);
1158 (void)mcd_getstat(sc, 1);
1159 /*cd->status &= ~MCDDSKCHNG; */
1160 cd->debug = 1; /* preventive set debug mode */
1161
1162#endif
1163
1164}
1165
1166static int
1167mcd_lock_door(struct mcd_softc *sc, int lock)
1168{
1169
1170 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDLOCKDRV);
1171 MCD_WRITE(sc, MCD_REG_COMMAND, lock);
1172 if (mcd_getstat(sc, 0) == -1)
1173 return EIO;
1174 return 0;
1175}
1176
1177static int
1178mcd_close_tray(struct mcd_softc *sc)
1179{
1180 struct mcd_data *cd = &sc->data;
1181 int retry, r;
1182
1183 if (mcd_getstat(sc, 1) == -1)
1184 return EIO;
1185 if (cd->status & MCDDOOROPEN) {
1186 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDCLOSETRAY);
1187 for (retry = 0; retry < CLOSE_TRAY_SECS * WAIT_FRAC; retry++) {
1188 if (MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL)
1189 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdcls", hz/WAIT_FRAC);
1190 else {
1191 if ((r = mcd_getstat(sc, 0)) == -1)
1192 return EIO;
1193 return 0;
1194 }
1195 }
1196 return ENXIO;
1197 }
1198 return 0;
1199}
1200
1201static int
1202mcd_eject(struct mcd_softc *sc)
1203{
1204 struct mcd_data *cd = &sc->data;
1205 int r;
1206
1207 if (mcd_getstat(sc, 1) == -1) /* detect disk change too */
1208 return EIO;
1209 if (cd->status & MCDDOOROPEN)
1210 return 0;
1211 if ((r = mcd_stop(sc)) == EIO)
1212 return r;
1213 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDEJECTDISK);
1214 if (mcd_getstat(sc, 0) == -1)
1215 return EIO;
1216 return 0;
1217}
1218
1219static int
1220mcd_inject(struct mcd_softc *sc)
1221{
1222 struct mcd_data *cd = &sc->data;
1223
1224 if (mcd_getstat(sc, 1) == -1) /* detect disk change too */
1225 return EIO;
1226 if (cd->status & MCDDOOROPEN)
1227 return mcd_close_tray(sc);
1228 return 0;
1229}
1230
1231static int
1232mcd_hard_reset(struct mcd_softc *sc)
1233{
1234 struct mcd_data *cd = &sc->data;
1235
1236 MCD_WRITE(sc, MCD_REG_RESET, MCD_CMDRESET);
1237 cd->curr_mode = MCD_MD_UNKNOWN;
1238 cd->audio_status = CD_AS_AUDIO_INVALID;
1239 return 0;
1240}
1241
1242static void
1243mcd_soft_reset(struct mcd_softc *sc)
1244{
1245 struct mcd_data *cd = &sc->data;
1246 int i;
1247
1248 cd->flags &= (MCDINIT|MCDPROBING|MCDNEWMODEL);
1249 cd->curr_mode = MCD_MD_UNKNOWN;
1250 for (i=0; i<MAXPARTITIONS; i++) cd->partflags[i] = 0;
1251 cd->audio_status = CD_AS_AUDIO_INVALID;
1252}
1253
1254static int
1255mcd_setmode(struct mcd_softc *sc, int mode)
1256{
1257 struct mcd_data *cd = &sc->data;
1258 int retry, st;
1259
1260 if (cd->curr_mode == mode)
1261 return 0;
1262 if (cd->debug)
1263 device_printf(sc->dev, "setting mode to %d\n", mode);
1264 for(retry=0; retry<MCD_RETRYS; retry++)
1265 {
1266 cd->curr_mode = MCD_MD_UNKNOWN;
1267 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDSETMODE);
1268 MCD_WRITE(sc, MCD_REG_COMMAND, mode);
1269 if ((st = mcd_getstat(sc, 0)) >= 0) {
1270 cd->curr_mode = mode;
1271 return 0;
1272 }
1273 if (st == -2) {
1274 device_printf(sc->dev, "media changed\n");
1275 break;
1276 }
1277 }
1278
1279 return -1;
1280}
1281
1282static int
1283mcd_toc_header(struct mcd_softc *sc, struct ioc_toc_header *th)
1284{
1285 struct mcd_data *cd = &sc->data;
1286 int r;
1287
1288 if ((r = mcd_volinfo(sc)) != 0)
1289 return r;
1290
1291 th->starting_track = bcd2bin(cd->volinfo.trk_low);
1292 th->ending_track = bcd2bin(cd->volinfo.trk_high);
1293 th->len = 2 * sizeof(u_char) /* start & end tracks */ +
1294 (th->ending_track + 1 - th->starting_track + 1) *
1295 sizeof(struct cd_toc_entry);
1296
1297 return 0;
1298}
1299
1300static int
1301mcd_read_toc(struct mcd_softc *sc)
1302{
1303 struct mcd_data *cd = &sc->data;
1304 struct ioc_toc_header th;
1305 struct mcd_qchninfo q;
1306 int rc, trk, idx, retry;
1307
1308 /* Only read TOC if needed */
1309 if (cd->flags & MCDTOC)
1310 return 0;
1311
1312 if (cd->debug)
1313 device_printf(sc->dev, "reading toc header\n");
1314
1315 if ((rc = mcd_toc_header(sc, &th)) != 0)
1316 return rc;
1317
1318 if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1319 return EIO;
1320
1321 if (mcd_setmode(sc, MCD_MD_TOC) != 0)
1322 return EIO;
1323
1324 if (cd->debug)
1325 device_printf(sc->dev, "get_toc reading qchannel info\n");
1326
1327 for(trk=th.starting_track; trk<=th.ending_track; trk++)
1328 cd->toc[trk].idx_no = 0;
1329 trk = th.ending_track - th.starting_track + 1;
1330 for(retry=0; retry<600 && trk>0; retry++)
1331 {
1332 if (mcd_getqchan(sc, &q) < 0) break;
1333 idx = bcd2bin(q.idx_no);
1334 if (idx>=th.starting_track && idx<=th.ending_track && q.trk_no==0) {
1335 if (cd->toc[idx].idx_no == 0) {
1336 cd->toc[idx] = q;
1337 trk--;
1338 }
1339 }
1340 }
1341
1342 if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
1343 return EIO;
1344
1345 if (trk != 0)
1346 return ENXIO;
1347
1348 /* add a fake last+1 */
1349 idx = th.ending_track + 1;
1350 cd->toc[idx].control = cd->toc[idx-1].control;
1351 cd->toc[idx].addr_type = cd->toc[idx-1].addr_type;
1352 cd->toc[idx].trk_no = 0;
1353 cd->toc[idx].idx_no = MCD_LASTPLUS1;
1354 cd->toc[idx].hd_pos_msf[0] = cd->volinfo.vol_msf[0];
1355 cd->toc[idx].hd_pos_msf[1] = cd->volinfo.vol_msf[1];
1356 cd->toc[idx].hd_pos_msf[2] = cd->volinfo.vol_msf[2];
1357
1358 if (cd->debug)
1359 { int i;
1360 for (i = th.starting_track; i <= idx; i++)
1361 device_printf(sc->dev, "trk %d idx %d pos %d %d %d\n",
1362 i,
1363 cd->toc[i].idx_no > 0x99 ? cd->toc[i].idx_no :
1364 bcd2bin(cd->toc[i].idx_no),
1365 bcd2bin(cd->toc[i].hd_pos_msf[0]),
1366 bcd2bin(cd->toc[i].hd_pos_msf[1]),
1367 bcd2bin(cd->toc[i].hd_pos_msf[2]));
1368 }
1369
1370 cd->flags |= MCDTOC;
1371
1372 return 0;
1373}
1374
1375#if 0
1376static int
1377mcd_toc_entry(struct mcd_softc *sc, struct ioc_read_toc_single_entry *te)
1378{
1379 struct mcd_data *cd = &sc->data;
1380 struct ioc_toc_header th;
1381 int rc, trk;
1382
1383 if (te->address_format != CD_MSF_FORMAT
1384 && te->address_format != CD_LBA_FORMAT)
1385 return EINVAL;
1386
1387 /* Copy the toc header */
1388 if ((rc = mcd_toc_header(sc, &th)) != 0)
1389 return rc;
1390
1391 /* verify starting track */
1392 trk = te->track;
1393 if (trk == 0)
1394 trk = th.starting_track;
1395 else if (trk == MCD_LASTPLUS1)
1396 trk = th.ending_track + 1;
1397 else if (trk < th.starting_track || trk > th.ending_track + 1)
1398 return EINVAL;
1399
1400 /* Make sure we have a valid toc */
1401 if ((rc=mcd_read_toc(sc)) != 0)
1402 return rc;
1403
1404 /* Copy the TOC data. */
1405 if (cd->toc[trk].idx_no == 0)
1406 return EIO;
1407
1408 te->entry.control = cd->toc[trk].control;
1409 te->entry.addr_type = cd->toc[trk].addr_type;
1410 te->entry.track =
1411 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1412 bcd2bin(cd->toc[trk].idx_no);
1413 switch (te->address_format) {
1414 case CD_MSF_FORMAT:
1415 te->entry.addr.msf.unused = 0;
1416 te->entry.addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1417 te->entry.addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1418 te->entry.addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1419 break;
1420 case CD_LBA_FORMAT:
1421 te->entry.addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1422 break;
1423 }
1424 return 0;
1425}
1426#endif
1427
1428static int
1429mcd_toc_entrys(struct mcd_softc *sc, struct ioc_read_toc_entry *te)
1430{
1431 struct mcd_data *cd = &sc->data;
1432 struct cd_toc_entry entries[MCD_MAXTOCS];
1433 struct ioc_toc_header th;
1434 int rc, n, trk, len;
1435
1436 if ( te->data_len < sizeof(entries[0])
1437 || (te->data_len % sizeof(entries[0])) != 0
1438 || (te->address_format != CD_MSF_FORMAT
1439 && te->address_format != CD_LBA_FORMAT)
1440 )
1441 return EINVAL;
1442
1443 /* Copy the toc header */
1444 if ((rc = mcd_toc_header(sc, &th)) != 0)
1445 return rc;
1446
1447 /* verify starting track */
1448 trk = te->starting_track;
1449 if (trk == 0)
1450 trk = th.starting_track;
1451 else if (trk == MCD_LASTPLUS1)
1452 trk = th.ending_track + 1;
1453 else if (trk < th.starting_track || trk > th.ending_track + 1)
1454 return EINVAL;
1455
1456 len = ((th.ending_track + 1 - trk) + 1) *
1457 sizeof(entries[0]);
1458 if (te->data_len < len)
1459 len = te->data_len;
1460 if (len > sizeof(entries))
1461 return EINVAL;
1462
1463 /* Make sure we have a valid toc */
1464 if ((rc=mcd_read_toc(sc)) != 0)
1465 return rc;
1466
1467 /* Copy the TOC data. */
1468 for (n = 0; len > 0 && trk <= th.ending_track + 1; trk++) {
1469 if (cd->toc[trk].idx_no == 0)
1470 continue;
1471 entries[n].control = cd->toc[trk].control;
1472 entries[n].addr_type = cd->toc[trk].addr_type;
1473 entries[n].track =
1474 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1475 bcd2bin(cd->toc[trk].idx_no);
1476 switch (te->address_format) {
1477 case CD_MSF_FORMAT:
1478 entries[n].addr.msf.unused = 0;
1479 entries[n].addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1480 entries[n].addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1481 entries[n].addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1482 break;
1483 case CD_LBA_FORMAT:
1484 entries[n].addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1485 break;
1486 }
1487 len -= sizeof(struct cd_toc_entry);
1488 n++;
1489 }
1490
1491 /* copy the data back */
1492 return copyout(entries, te->data, n * sizeof(struct cd_toc_entry));
1493}
1494
1495static int
1496mcd_stop(struct mcd_softc *sc)
1497{
1498 struct mcd_data *cd = &sc->data;
1499
1500 /* Verify current status */
1501 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1502 cd->audio_status != CD_AS_PLAY_PAUSED &&
1503 cd->audio_status != CD_AS_PLAY_COMPLETED) {
1504 if (cd->debug)
1505 device_printf(sc->dev,
1506 "stop attempted when not playing, audio status %d\n",
1507 cd->audio_status);
1508 return EINVAL;
1509 }
1510 if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
1511 if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1512 return EIO;
1513 cd->audio_status = CD_AS_PLAY_COMPLETED;
1514 return 0;
1515}
1516
1517static int
1518mcd_getqchan(struct mcd_softc *sc, struct mcd_qchninfo *q)
1519{
1520 struct mcd_data *cd = &sc->data;
1521
1522 if (mcd_send(sc, MCD_CMDGETQCHN, MCD_RETRYS) < 0)
1523 return -1;
1524 if (mcd_get(sc, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
1525 return -1;
1526 if (cd->debug) {
1527 device_printf(sc->dev,
1528 "getqchan control=0x%x addr_type=0x%x trk=%d ind=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
1529 q->control, q->addr_type,
1530 bcd2bin(q->trk_no),
1531 bcd2bin(q->idx_no),
1532 bcd2bin(q->trk_size_msf[0]),
1533 bcd2bin(q->trk_size_msf[1]),
1534 bcd2bin(q->trk_size_msf[2]),
1535 bcd2bin(q->hd_pos_msf[0]),
1536 bcd2bin(q->hd_pos_msf[1]),
1537 bcd2bin(q->hd_pos_msf[2]));
1538 }
1539 return 0;
1540}
1541
1542static int
1543mcd_subchan(struct mcd_softc *sc, struct ioc_read_subchannel *sch)
1544{
1545 struct mcd_data *cd = &sc->data;
1546 struct mcd_qchninfo q;
1547 struct cd_sub_channel_info data;
1548 int lba;
1549
1550 if (cd->debug)
1551 device_printf(sc->dev, "subchan af=%d, df=%d\n",
1552 sch->address_format,
1553 sch->data_format);
1554
1555 if (sch->address_format != CD_MSF_FORMAT &&
1556 sch->address_format != CD_LBA_FORMAT)
1557 return EINVAL;
1558
1559 if (sch->data_format != CD_CURRENT_POSITION &&
1560 sch->data_format != CD_MEDIA_CATALOG)
1561 return EINVAL;
1562
1563 if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
1564 return EIO;
1565
1566 if (mcd_getqchan(sc, &q) < 0)
1567 return EIO;
1568
1569 data.header.audio_status = cd->audio_status;
1570 data.what.position.data_format = sch->data_format;
1571
1572 switch (sch->data_format) {
1573 case CD_MEDIA_CATALOG:
1574 data.what.media_catalog.mc_valid = 1;
1575 data.what.media_catalog.mc_number[0] = '\0';
1576 break;
1577
1578 case CD_CURRENT_POSITION:
1579 data.what.position.control = q.control;
1580 data.what.position.addr_type = q.addr_type;
1581 data.what.position.track_number = bcd2bin(q.trk_no);
1582 data.what.position.index_number = bcd2bin(q.idx_no);
1583 switch (sch->address_format) {
1584 case CD_MSF_FORMAT:
1585 data.what.position.reladdr.msf.unused = 0;
1586 data.what.position.reladdr.msf.minute = bcd2bin(q.trk_size_msf[0]);
1587 data.what.position.reladdr.msf.second = bcd2bin(q.trk_size_msf[1]);
1588 data.what.position.reladdr.msf.frame = bcd2bin(q.trk_size_msf[2]);
1589 data.what.position.absaddr.msf.unused = 0;
1590 data.what.position.absaddr.msf.minute = bcd2bin(q.hd_pos_msf[0]);
1591 data.what.position.absaddr.msf.second = bcd2bin(q.hd_pos_msf[1]);
1592 data.what.position.absaddr.msf.frame = bcd2bin(q.hd_pos_msf[2]);
1593 break;
1594 case CD_LBA_FORMAT:
1595 lba = msf2hsg(q.trk_size_msf, 1);
1596 /*
1597 * Pre-gap has index number of 0, and decreasing MSF
1598 * address. Must be converted to negative LBA, per
1599 * SCSI spec.
1600 */
1601 if (data.what.position.index_number == 0)
1602 lba = -lba;
1603 data.what.position.reladdr.lba = htonl(lba);
1604 data.what.position.absaddr.lba = htonl(msf2hsg(q.hd_pos_msf, 0));
1605 break;
1606 }
1607 break;
1608 }
1609
1610 return copyout(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len));
1611}
1612
1613static int
1614mcd_playmsf(struct mcd_softc *sc, struct ioc_play_msf *p)
1615{
1616 struct mcd_data *cd = &sc->data;
1617 struct mcd_read2 pb;
1618
1619 if (cd->debug)
1620 device_printf(sc->dev, "playmsf: from %d:%d.%d to %d:%d.%d\n",
1621 p->start_m, p->start_s, p->start_f,
1622 p->end_m, p->end_s, p->end_f);
1623
1624 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1625 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) ||
1626 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) >
1627 M_msf(cd->volinfo.vol_msf) * 60 * 75 +
1628 S_msf(cd->volinfo.vol_msf) * 75 +
1629 F_msf(cd->volinfo.vol_msf))
1630 return EINVAL;
1631
1632 pb.start_msf[0] = bin2bcd(p->start_m);
1633 pb.start_msf[1] = bin2bcd(p->start_s);
1634 pb.start_msf[2] = bin2bcd(p->start_f);
1635 pb.end_msf[0] = bin2bcd(p->end_m);
1636 pb.end_msf[1] = bin2bcd(p->end_s);
1637 pb.end_msf[2] = bin2bcd(p->end_f);
1638
1639 if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
1640 return EIO;
1641
1642 return mcd_play(sc, &pb);
1643}
1644
1645static int
1646mcd_playtracks(struct mcd_softc *sc, struct ioc_play_track *pt)
1647{
1648 struct mcd_data *cd = &sc->data;
1649 struct mcd_read2 pb;
1650 int a = pt->start_track;
1651 int z = pt->end_track;
1652 int rc, i;
1653
1654 if ((rc = mcd_read_toc(sc)) != 0)
1655 return rc;
1656
1657 if (cd->debug)
1658 device_printf(sc->dev, "playtracks from %d:%d to %d:%d\n",
1659 a, pt->start_index, z, pt->end_index);
1660
1661 if ( a < bcd2bin(cd->volinfo.trk_low)
1662 || a > bcd2bin(cd->volinfo.trk_high)
1663 || a > z
1664 || z < bcd2bin(cd->volinfo.trk_low)
1665 || z > bcd2bin(cd->volinfo.trk_high))
1666 return EINVAL;
1667
1668 for (i = 0; i < 3; i++) {
1669 pb.start_msf[i] = cd->toc[a].hd_pos_msf[i];
1670 pb.end_msf[i] = cd->toc[z+1].hd_pos_msf[i];
1671 }
1672
1673 if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
1674 return EIO;
1675
1676 return mcd_play(sc, &pb);
1677}
1678
1679static int
1680mcd_playblocks(struct mcd_softc *sc, struct ioc_play_blocks *p)
1681{
1682 struct mcd_data *cd = &sc->data;
1683 struct mcd_read2 pb;
1684
1685 if (cd->debug)
1686 device_printf(sc->dev, "playblocks: blkno %d length %d\n",
1687 p->blk, p->len);
1688
1689 if (p->blk > cd->disksize || p->len > cd->disksize ||
1690 p->blk < 0 || p->len < 0 ||
1691 (p->blk + p->len) > cd->disksize)
1692 return EINVAL;
1693
1694 hsg2msf(p->blk, pb.start_msf);
1695 hsg2msf(p->blk + p->len, pb.end_msf);
1696
1697 if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
1698 return EIO;
1699
1700 return mcd_play(sc, &pb);
1701}
1702
1703static int
1704mcd_play(struct mcd_softc *sc, struct mcd_read2 *pb)
1705{
1706 struct mcd_data *cd = &sc->data;
1707 int retry, st = -1, status;
1708
1709 cd->lastpb = *pb;
1710 for(retry=0; retry<MCD_RETRYS; retry++) {
1711
1712 disable_intr();
1713 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDSINGLESPEEDREAD);
1714 MCD_WRITE(sc, MCD_REG_COMMAND, pb->start_msf[0]);
1715 MCD_WRITE(sc, MCD_REG_COMMAND, pb->start_msf[1]);
1716 MCD_WRITE(sc, MCD_REG_COMMAND, pb->start_msf[2]);
1717 MCD_WRITE(sc, MCD_REG_COMMAND, pb->end_msf[0]);
1718 MCD_WRITE(sc, MCD_REG_COMMAND, pb->end_msf[1]);
1719 MCD_WRITE(sc, MCD_REG_COMMAND, pb->end_msf[2]);
1720 enable_intr();
1721
1722 status=mcd_getstat(sc, 0);
1723 if (status == -1)
1724 continue;
1725 else if (status != -2)
1726 st = 0;
1727 break;
1728 }
1729
1730 if (status == -2) {
1731 device_printf(sc->dev, "media changed\n");
1732 return ENXIO;
1733 }
1734 if (cd->debug)
1735 device_printf(sc->dev,
1736 "mcd_play retry=%d, status=0x%02x\n", retry, status);
1737 if (st < 0)
1738 return ENXIO;
1739 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
1740 return 0;
1741}
1742
1743static int
1744mcd_pause(struct mcd_softc *sc)
1745{
1746 struct mcd_data *cd = &sc->data;
1747 struct mcd_qchninfo q;
1748 int rc;
1749
1750 /* Verify current status */
1751 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1752 cd->audio_status != CD_AS_PLAY_PAUSED) {
1753 if (cd->debug)
1754 device_printf(sc->dev,
1755 "pause attempted when not playing, audio status %d\n",
1756 cd->audio_status);
1757 return EINVAL;
1758 }
1759
1760 /* Get the current position */
1761 if (mcd_getqchan(sc, &q) < 0)
1762 return EIO;
1763
1764 /* Copy it into lastpb */
1765 cd->lastpb.start_msf[0] = q.hd_pos_msf[0];
1766 cd->lastpb.start_msf[1] = q.hd_pos_msf[1];
1767 cd->lastpb.start_msf[2] = q.hd_pos_msf[2];
1768
1769 /* Stop playing */
1770 if ((rc=mcd_stop(sc)) != 0)
1771 return rc;
1772
1773 /* Set the proper status and exit */
1774 cd->audio_status = CD_AS_PLAY_PAUSED;
1775 return 0;
1776}
1777
1778static int
1779mcd_resume(struct mcd_softc *sc)
1780{
1781 struct mcd_data *cd = &sc->data;
1782
1783 if (cd->audio_status != CD_AS_PLAY_PAUSED)
1784 return EINVAL;
1785 return mcd_play(sc, &cd->lastpb);
1786}
1787#endif /* GEOM */