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