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