mcd.c revision 1.1 1 /*
2 * Copyright 1993 by Holger Veit (data part)
3 * Copyright 1993 by Brian Moore (audio part)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This software was developed by Holger Veit and Brian Moore
17 * for use with "386BSD" and similar operating systems.
18 * "Similar operating systems" includes mainly non-profit oriented
19 * systems for research and education, including but not restricted to
20 * "NetBSD", "FreeBSD", "Mach" (by CMU).
21 * 4. Neither the name of the developer(s) nor the name "386BSD"
22 * may be used to endorse or promote products derived from this
23 * software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
26 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * $Id: mcd.c,v 1.1 1993/11/04 09:13:07 cgd Exp $
38 */
39 static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";
40
41 #include "mcd.h"
42 #if NMCD > 0
43 #include "types.h"
44 #include "param.h"
45 #include "systm.h"
46 #include "conf.h"
47 #include "file.h"
48 #include "buf.h"
49 #include "stat.h"
50 #include "uio.h"
51 #include "ioctl.h"
52 #include "cdio.h"
53 #include "errno.h"
54 #include "dkbad.h"
55 #include "disklabel.h"
56 #include "i386/isa/isa.h"
57 #include "i386/isa/isa_device.h"
58 #include "mcdreg.h"
59
60 /* user definable options */
61 /*#define MCD_TO_WARNING_ON*/ /* define to get timeout messages */
62 /*#define MCDMINI*/ /* define for a mini configuration for boot kernel */
63
64
65 #ifdef MCDMINI
66 #define MCD_TRACE(fmt,a,b,c,d)
67 #ifdef MCD_TO_WARNING_ON
68 #undef MCD_TO_WARNING_ON
69 #endif
70 #else
71 #define MCD_TRACE(fmt,a,b,c,d) {if (mcd_data[unit].debug) {printf("mcd%d st=%02x: ",unit,mcd_data[unit].status); printf(fmt,a,b,c,d);}}
72 #endif
73
74 #define mcd_part(dev) ((minor(dev)) & 7)
75 #define mcd_unit(dev) (((minor(dev)) & 0x38) >> 3)
76 #define mcd_phys(dev) (((minor(dev)) & 0x40) >> 6)
77 #define RAW_PART 3
78
79 /* flags */
80 #define MCDOPEN 0x0001 /* device opened */
81 #define MCDVALID 0x0002 /* parameters loaded */
82 #define MCDINIT 0x0004 /* device is init'd */
83 #define MCDWAIT 0x0008 /* waiting for something */
84 #define MCDLABEL 0x0010 /* label is read */
85 #define MCDPROBING 0x0020 /* probing */
86 #define MCDREADRAW 0x0040 /* read raw mode (2352 bytes) */
87 #define MCDVOLINFO 0x0080 /* already read volinfo */
88 #define MCDTOC 0x0100 /* already read toc */
89 #define MCDMBXBSY 0x0200 /* local mbx is busy */
90
91 /* status */
92 #define MCDAUDIOBSY MCD_ST_AUDIOBSY /* playing audio */
93 #define MCDDSKCHNG MCD_ST_DSKCHNG /* sensed change of disk */
94 #define MCDDSKIN MCD_ST_DSKIN /* sensed disk in drive */
95 #define MCDDOOROPEN MCD_ST_DOOROPEN /* sensed door open */
96
97 /* toc */
98 #define MCD_MAXTOCS 104 /* from the Linux driver */
99 #define MCD_LASTPLUS1 170 /* special toc entry */
100
101 struct mcd_mbx {
102 short unit;
103 short port;
104 short retry;
105 short nblk;
106 int sz;
107 u_long skip;
108 struct buf *bp;
109 int p_offset;
110 short count;
111 };
112
113 struct mcd_data {
114 short config;
115 short flags;
116 short status;
117 int blksize;
118 u_long disksize;
119 int iobase;
120 struct disklabel dlabel;
121 int partflags[MAXPARTITIONS];
122 int openflags;
123 struct mcd_volinfo volinfo;
124 #ifndef MCDMINI
125 struct mcd_qchninfo toc[MCD_MAXTOCS];
126 short audio_status;
127 struct mcd_read2 lastpb;
128 #endif
129 short debug;
130 struct buf head; /* head of buf queue */
131 struct mcd_mbx mbx;
132 } mcd_data[NMCD];
133
134 /* reader state machine */
135 #define MCD_S_BEGIN 0
136 #define MCD_S_BEGIN1 1
137 #define MCD_S_WAITSTAT 2
138 #define MCD_S_WAITMODE 3
139 #define MCD_S_WAITREAD 4
140
141 /* prototypes */
142 int mcdopen(dev_t dev);
143 int mcdclose(dev_t dev);
144 int mcdstrategy(struct buf *bp);
145 int mcdioctl(dev_t dev, int cmd, caddr_t addr, int flags);
146 int mcdsize(dev_t dev);
147 static void mcd_done(struct mcd_mbx *mbx);
148 static void mcd_start(int unit);
149 static int mcd_getdisklabel(int unit);
150 static void mcd_configure(struct mcd_data *cd);
151 static int mcd_get(int unit, char *buf, int nmax);
152 static void mcd_setflags(int unit,struct mcd_data *cd);
153 static int mcd_getstat(int unit,int sflg);
154 static int mcd_send(int unit, int cmd,int nretrys);
155 static int bcd2bin(bcd_t b);
156 static bcd_t bin2bcd(int b);
157 static void hsg2msf(int hsg, bcd_t *msf);
158 static int msf2hsg(bcd_t *msf);
159 static int mcd_volinfo(int unit);
160 static int mcd_waitrdy(int port,int dly);
161 static void mcd_doread(int state, struct mcd_mbx *mbxin);
162 #ifndef MCDMINI
163 static int mcd_setmode(int unit, int mode);
164 static int mcd_getqchan(int unit, struct mcd_qchninfo *q);
165 static int mcd_subchan(int unit, struct ioc_read_subchannel *sc);
166 static int mcd_toc_header(int unit, struct ioc_toc_header *th);
167 static int mcd_read_toc(int unit);
168 static int mcd_toc_entry(int unit, struct ioc_read_toc_entry *te);
169 static int mcd_stop(int unit);
170 static int mcd_playtracks(int unit, struct ioc_play_track *pt);
171 static int mcd_play(int unit, struct mcd_read2 *pb);
172 static int mcd_pause(int unit);
173 static int mcd_resume(int unit);
174 #endif
175
176 extern int hz;
177 extern int mcd_probe(struct isa_device *dev);
178 extern int mcd_attach(struct isa_device *dev);
179 struct isa_driver mcddriver = { mcd_probe, mcd_attach, "mcd" };
180
181 #define mcd_put(port,byte) outb(port,byte)
182
183 #define MCD_RETRYS 5
184 #define MCD_RDRETRYS 8
185
186 #define MCDBLK 2048 /* for cooked mode */
187 #define MCDRBLK 2352 /* for raw mode */
188
189 /* several delays */
190 #define RDELAY_WAITSTAT 300
191 #define RDELAY_WAITMODE 300
192 #define RDELAY_WAITREAD 800
193
194 #define DELAY_STATUS 10000l /* 10000 * 1us */
195 #define DELAY_GETREPLY 200000l /* 200000 * 2us */
196 #define DELAY_SEEKREAD 20000l /* 20000 * 1us */
197 #define mcd_delay DELAY
198
199 int mcd_attach(struct isa_device *dev)
200 {
201 struct mcd_data *cd = mcd_data + dev->id_unit;
202 int i;
203
204 cd->iobase = dev->id_iobase;
205 cd->flags |= MCDINIT;
206 cd->openflags = 0;
207 for (i=0; i<MAXPARTITIONS; i++) cd->partflags[i] = 0;
208
209 #ifdef NOTYET
210 /* wire controller for interrupts and dma */
211 mcd_configure(cd);
212 #endif
213
214 return 1;
215 }
216
217 int mcdopen(dev_t dev)
218 {
219 int unit,part,phys;
220 struct mcd_data *cd;
221
222 unit = mcd_unit(dev);
223 if (unit >= NMCD)
224 return ENXIO;
225
226 cd = mcd_data + unit;
227 part = mcd_part(dev);
228 phys = mcd_phys(dev);
229
230 /* not initialized*/
231 if (!(cd->flags & MCDINIT))
232 return ENXIO;
233
234 /* invalidated in the meantime? mark all open part's invalid */
235 if (!(cd->flags & MCDVALID) && cd->openflags)
236 return ENXIO;
237
238 if (mcd_getstat(unit,1) < 0)
239 return ENXIO;
240
241 /* XXX get a default disklabel */
242 mcd_getdisklabel(unit);
243
244 if (mcdsize(dev) < 0) {
245 printf("mcd%d: failed to get disk size\n",unit);
246 return ENXIO;
247 } else
248 cd->flags |= MCDVALID;
249
250 MCD_TRACE("open: partition=%d, disksize = %d, blksize=%d\n",
251 part,cd->disksize,cd->blksize,0);
252
253 if (part == RAW_PART ||
254 (part < cd->dlabel.d_npartitions &&
255 cd->dlabel.d_partitions[part].p_fstype != FS_UNUSED)) {
256 cd->partflags[part] |= MCDOPEN;
257 cd->openflags |= (1<<part);
258 if (part == RAW_PART && phys != 0)
259 cd->partflags[part] |= MCDREADRAW;
260 return 0;
261 }
262
263 return ENXIO;
264 }
265
266 int mcdclose(dev_t dev)
267 {
268 int unit,part,phys;
269 struct mcd_data *cd;
270
271 unit = mcd_unit(dev);
272 if (unit >= NMCD)
273 return ENXIO;
274
275 cd = mcd_data + unit;
276 part = mcd_part(dev);
277 phys = mcd_phys(dev);
278
279 if (!(cd->flags & MCDINIT))
280 return ENXIO;
281
282 mcd_getstat(unit,1); /* get status */
283
284 /* close channel */
285 cd->partflags[part] &= ~(MCDOPEN|MCDREADRAW);
286 cd->openflags &= ~(1<<part);
287 MCD_TRACE("close: partition=%d\n",part,0,0,0);
288
289 return 0;
290 }
291
292 int mcdstrategy(struct buf *bp)
293 {
294 struct mcd_data *cd;
295 struct buf *qp;
296 int s;
297
298 int unit = mcd_unit(bp->b_dev);
299
300 cd = mcd_data + unit;
301
302 /* test validity */
303 /*MCD_TRACE("strategy: buf=0x%lx, unit=%ld, block#=%ld bcount=%ld\n",
304 bp,unit,bp->b_blkno,bp->b_bcount);*/
305 if (unit >= NMCD || bp->b_blkno < 0) {
306 printf("mcdstrategy: unit = %d, blkno = %d, bcount = %d\n",
307 unit, bp->b_blkno, bp->b_bcount);
308 pg("mcd: mcdstratregy failure");
309 bp->b_error = EINVAL;
310 bp->b_flags |= B_ERROR;
311 goto bad;
312 }
313
314 /* if device invalidated (e.g. media change, door open), error */
315 if (!(cd->flags & MCDVALID)) {
316 MCD_TRACE("strategy: drive not valid\n",0,0,0,0);
317 bp->b_error = EIO;
318 goto bad;
319 }
320
321 /* read only */
322 if (!(bp->b_flags & B_READ)) {
323 bp->b_error = EROFS;
324 goto bad;
325 }
326
327 /* no data to read */
328 if (bp->b_bcount == 0)
329 goto done;
330
331 /* for non raw access, check partition limits */
332 if (mcd_part(bp->b_dev) != RAW_PART) {
333 if (!(cd->flags & MCDLABEL)) {
334 bp->b_error = EIO;
335 goto bad;
336 }
337 /* adjust transfer if necessary */
338 if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0) {
339 goto done;
340 }
341 }
342
343 /* queue it */
344 qp = &cd->head;
345 s = splbio();
346 disksort(qp,bp);
347 splx(s);
348
349 /* now check whether we can perform processing */
350 mcd_start(unit);
351 return;
352
353 bad:
354 bp->b_flags |= B_ERROR;
355 done:
356 bp->b_resid = bp->b_bcount;
357 biodone(bp);
358 return;
359 }
360
361 static void mcd_start(int unit)
362 {
363 struct mcd_data *cd = mcd_data + unit;
364 struct buf *bp, *qp = &cd->head;
365 struct partition *p;
366 int part;
367 register s = splbio();
368
369 if (cd->flags & MCDMBXBSY)
370 return;
371
372 if ((bp = qp->b_actf) != 0) {
373 /* block found to process, dequeue */
374 /*MCD_TRACE("mcd_start: found block bp=0x%x\n",bp,0,0,0);*/
375 qp->b_actf = bp->av_forw;
376 splx(s);
377 } else {
378 /* nothing to do */
379 splx(s);
380 return;
381 }
382
383 /* changed media? */
384 if (!(cd->flags & MCDVALID)) {
385 MCD_TRACE("mcd_start: drive not valid\n",0,0,0,0);
386 return;
387 }
388
389 p = cd->dlabel.d_partitions + mcd_part(bp->b_dev);
390
391 cd->flags |= MCDMBXBSY;
392 cd->mbx.unit = unit;
393 cd->mbx.port = cd->iobase;
394 cd->mbx.retry = MCD_RETRYS;
395 cd->mbx.bp = bp;
396 cd->mbx.p_offset = p->p_offset;
397
398 /* calling the read routine */
399 mcd_doread(MCD_S_BEGIN,&(cd->mbx));
400 /* triggers mcd_start, when successful finished */
401 return;
402 }
403
404 int mcdioctl(dev_t dev, int cmd, caddr_t addr, int flags)
405 {
406 struct mcd_data *cd;
407 int unit,part;
408
409 unit = mcd_unit(dev);
410 part = mcd_part(dev);
411 cd = mcd_data + unit;
412
413 #ifdef MCDMINI
414 return ENOTTY;
415 #else
416 if (!(cd->flags & MCDVALID))
417 return EIO;
418 MCD_TRACE("ioctl called 0x%x\n",cmd,0,0,0);
419
420 switch (cmd) {
421 case DIOCSBAD:
422 return EINVAL;
423 case DIOCGDINFO:
424 case DIOCGPART:
425 case DIOCWDINFO:
426 case DIOCSDINFO:
427 case DIOCWLABEL:
428 return ENOTTY;
429 case CDIOCPLAYTRACKS:
430 return mcd_playtracks(unit, (struct ioc_play_track *) addr);
431 case CDIOCPLAYBLOCKS:
432 return mcd_play(unit, (struct mcd_read2 *) addr);
433 case CDIOCREADSUBCHANNEL:
434 return mcd_subchan(unit, (struct ioc_read_subchannel *) addr);
435 case CDIOREADTOCHEADER:
436 return mcd_toc_header(unit, (struct ioc_toc_header *) addr);
437 case CDIOREADTOCENTRYS:
438 return mcd_toc_entry(unit, (struct ioc_read_toc_entry *) addr);
439 case CDIOCSETPATCH:
440 case CDIOCGETVOL:
441 case CDIOCSETVOL:
442 case CDIOCSETMONO:
443 case CDIOCSETSTERIO:
444 case CDIOCSETMUTE:
445 case CDIOCSETLEFT:
446 case CDIOCSETRIGHT:
447 return EINVAL;
448 case CDIOCRESUME:
449 return mcd_resume(unit);
450 case CDIOCPAUSE:
451 return mcd_pause(unit);
452 case CDIOCSTART:
453 return EINVAL;
454 case CDIOCSTOP:
455 return mcd_stop(unit);
456 case CDIOCEJECT:
457 return EINVAL;
458 case CDIOCSETDEBUG:
459 cd->debug = 1;
460 return 0;
461 case CDIOCCLRDEBUG:
462 cd->debug = 0;
463 return 0;
464 case CDIOCRESET:
465 return EINVAL;
466 default:
467 return ENOTTY;
468 }
469 /*NOTREACHED*/
470 #endif /*!MCDMINI*/
471 }
472
473 /* this could have been taken from scsi/cd.c, but it is not clear
474 * whether the scsi cd driver is linked in
475 */
476 static int mcd_getdisklabel(int unit)
477 {
478 struct mcd_data *cd = mcd_data + unit;
479
480 if (cd->flags & MCDLABEL)
481 return -1;
482
483 bzero(&cd->dlabel,sizeof(struct disklabel));
484 strncpy(cd->dlabel.d_typename,"Mitsumi CD ROM ",16);
485 strncpy(cd->dlabel.d_packname,"unknown ",16);
486 cd->dlabel.d_secsize = cd->blksize;
487 cd->dlabel.d_nsectors = 100;
488 cd->dlabel.d_ntracks = 1;
489 cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
490 cd->dlabel.d_secpercyl = 100;
491 cd->dlabel.d_secperunit = cd->disksize;
492 cd->dlabel.d_rpm = 300;
493 cd->dlabel.d_interleave = 1;
494 cd->dlabel.d_flags = D_REMOVABLE;
495 cd->dlabel.d_npartitions= 1;
496 cd->dlabel.d_partitions[0].p_offset = 0;
497 cd->dlabel.d_partitions[0].p_size = cd->disksize;
498 cd->dlabel.d_partitions[0].p_fstype = 9;
499 cd->dlabel.d_magic = DISKMAGIC;
500 cd->dlabel.d_magic2 = DISKMAGIC;
501 cd->dlabel.d_checksum = dkcksum(&cd->dlabel);
502
503 cd->flags |= MCDLABEL;
504 return 0;
505 }
506
507 int mcdsize(dev_t dev)
508 {
509 int size;
510 int unit = mcd_unit(dev);
511 struct mcd_data *cd = mcd_data + unit;
512
513 if (mcd_volinfo(unit) >= 0) {
514 cd->blksize = MCDBLK;
515 size = msf2hsg(cd->volinfo.vol_msf);
516 cd->disksize = size * (MCDBLK/DEV_BSIZE);
517 return 0;
518 }
519 return -1;
520 }
521
522 /***************************************************************
523 * lower level of driver starts here
524 **************************************************************/
525
526 #ifdef NOTDEF
527 static char irqs[] = {
528 0x00,0x00,0x10,0x20,0x00,0x30,0x00,0x00,
529 0x00,0x10,0x40,0x50,0x00,0x00,0x00,0x00
530 };
531
532 static char drqs[] = {
533 0x00,0x01,0x00,0x03,0x00,0x05,0x06,0x07,
534 };
535 #endif
536
537 static void mcd_configure(struct mcd_data *cd)
538 {
539 outb(cd->iobase+mcd_config,cd->config);
540 }
541
542 /* check if there is a cdrom */
543 /* Heavly hacked by gclarkii (at) sugar.neosoft.com */
544
545 int mcd_probe(struct isa_device *dev)
546 {
547 int port = dev->id_iobase;
548 int unit = dev->id_unit;
549 int i;
550 int st;
551 int check;
552 int junk;
553
554 mcd_data[unit].flags = MCDPROBING;
555
556 #ifdef NOTDEF
557 /* get irq/drq configuration word */
558 mcd_data[unit].config = irqs[dev->id_irq]; /* | drqs[dev->id_drq];*/
559 #else
560 mcd_data[unit].config = 0;
561 #endif
562
563 /* send a reset */
564 outb(port+MCD_FLAGS,0);
565 DELAY(100000);
566 /* get any pending status and throw away...*/
567 for (i=10; i != 0; i--) {
568 inb(port+MCD_DATA);
569 }
570 DELAY(1000);
571
572 outb(port+MCD_DATA,MCD_CMDGETSTAT); /* Send get status command */
573
574 /* Loop looking for avail of status */
575 /* XXX May have to increase for fast machinces */
576 for (i = 1000; i != 0; i--) {
577 if ((inb(port+MCD_FLAGS) & 0xF ) == STATUS_AVAIL) {
578 break;
579 }
580 DELAY(10);
581 }
582 /* get status */
583
584 if (i == 0) {
585 #ifdef DEBUG
586 printf ("Mitsumi drive NOT detected\n");
587 #endif
588 return 0;
589 }
590
591 /*
592 * The following code uses the 0xDC command, it returns a M from the
593 * second byte and a number in the third. Does anyone know what the
594 * number is for? Better yet, how about someone thats REAL good in
595 * i80x86 asm looking at the Dos driver... Most of this info came
596 * from a friend of mine spending a whole weekend.....
597 */
598
599 DELAY (2000);
600 outb(port+MCD_DATA,MCD_CMDCONTINFO);
601 for (i = 0; i < 100000; i++) {
602 if ((inb(port+MCD_FLAGS) & 0xF) == STATUS_AVAIL)
603 break;
604 }
605 if (i > 100000) {
606 #ifdef DEBUG
607 printf ("Mitsumi drive error\n");
608 #endif
609 return 0;
610 }
611 DELAY (40000);
612 st = inb(port+MCD_DATA);
613 DELAY (500);
614 check = inb(port+MCD_DATA);
615 DELAY (500);
616 junk = inb(port+MCD_DATA); /* What is byte used for?!?!? */
617
618 if (check = 'M') {
619 #ifdef DEBUG
620 printf("Mitsumi drive detected\n");
621 #endif
622 return 4;
623 } else {
624 printf("Mitsumi drive NOT detected\n");
625 printf("Mitsumi drive error\n");
626 return 0;
627 }
628 }
629
630 static int mcd_waitrdy(int port,int dly)
631 {
632 int i;
633
634 /* wait until xfer port senses data ready */
635 for (i=0; i<dly; i++) {
636 if ((inb(port+mcd_xfer) & MCD_ST_BUSY)==0)
637 return 0;
638 mcd_delay(1);
639 }
640 return -1;
641 }
642
643 static int mcd_getreply(int unit,int dly)
644 {
645 int i;
646 struct mcd_data *cd = mcd_data + unit;
647 int port = cd->iobase;
648
649 /* wait data to become ready */
650 if (mcd_waitrdy(port,dly)<0) {
651 #ifdef MCD_TO_WARNING_ON
652 printf("mcd%d: timeout getreply\n",unit);
653 #endif
654 return -1;
655 }
656
657 /* get the data */
658 return inb(port+mcd_status) & 0xFF;
659 }
660
661 static int mcd_getstat(int unit,int sflg)
662 {
663 int i;
664 struct mcd_data *cd = mcd_data + unit;
665 int port = cd->iobase;
666
667 /* get the status */
668 if (sflg)
669 outb(port+mcd_command, MCD_CMDGETSTAT);
670 i = mcd_getreply(unit,DELAY_GETREPLY);
671 if (i<0) return -1;
672
673 cd->status = i;
674
675 mcd_setflags(unit,cd);
676 return cd->status;
677 }
678
679 static void mcd_setflags(int unit, struct mcd_data *cd)
680 {
681 /* check flags */
682 if (cd->status & (MCDDSKCHNG|MCDDOOROPEN)) {
683 MCD_TRACE("getstat: sensed DSKCHNG or DOOROPEN\n",0,0,0,0);
684 cd->flags &= ~MCDVALID;
685 }
686
687 #ifndef MCDMINI
688 if (cd->status & MCDAUDIOBSY)
689 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
690 else if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
691 cd->audio_status = CD_AS_PLAY_COMPLETED;
692 #endif
693 }
694
695 static int mcd_get(int unit, char *buf, int nmax)
696 {
697 int port = mcd_data[unit].iobase;
698 int i,k;
699
700 for (i=0; i<nmax; i++) {
701
702 /* wait for data */
703 if ((k = mcd_getreply(unit,DELAY_GETREPLY)) < 0) {
704 #ifdef MCD_TO_WARNING_ON
705 printf("mcd%d: timeout mcd_get\n",unit);
706 #endif
707 return -1;
708 }
709 buf[i] = k;
710 }
711 return i;
712 }
713
714 static int mcd_send(int unit, int cmd,int nretrys)
715 {
716 int i,k;
717 int port = mcd_data[unit].iobase;
718
719 /*MCD_TRACE("mcd_send: command = 0x%x\n",cmd,0,0,0);*/
720 for (i=0; i<nretrys; i++) {
721 outb(port+mcd_command, cmd);
722 if ((k=mcd_getstat(unit,0)) != -1)
723 break;
724 }
725 if (i == nretrys) {
726 printf("mcd%d: mcd_send retry cnt exceeded\n",unit);
727 return -1;
728 }
729 /*MCD_TRACE("mcd_send: status = 0x%x\n",k,0,0,0);*/
730 return 0;
731 }
732
733 static int bcd2bin(bcd_t b)
734 {
735 return (b >> 4) * 10 + (b & 15);
736 }
737
738 static bcd_t bin2bcd(int b)
739 {
740 return ((b / 10) << 4) | (b % 10);
741 }
742
743 static void hsg2msf(int hsg, bcd_t *msf)
744 {
745 hsg += 150;
746 M_msf(msf) = bin2bcd(hsg / 4500);
747 hsg %= 4500;
748 S_msf(msf) = bin2bcd(hsg / 75);
749 F_msf(msf) = bin2bcd(hsg % 75);
750 }
751
752 static int msf2hsg(bcd_t *msf)
753 {
754 return (bcd2bin(M_msf(msf)) * 60 +
755 bcd2bin(S_msf(msf))) * 75 +
756 bcd2bin(F_msf(msf)) - 150;
757 }
758
759 static int mcd_volinfo(int unit)
760 {
761 struct mcd_data *cd = mcd_data + unit;
762 int i;
763
764 /*MCD_TRACE("mcd_volinfo: enter\n",0,0,0,0);*/
765
766 /* Get the status, in case the disc has been changed */
767 if (mcd_getstat(unit, 1) < 0) return EIO;
768
769 /* Just return if we already have it */
770 if (cd->flags & MCDVOLINFO) return 0;
771
772 /* send volume info command */
773 if (mcd_send(unit,MCD_CMDGETVOLINFO,MCD_RETRYS) < 0)
774 return -1;
775
776 /* get data */
777 if (mcd_get(unit,(char*) &cd->volinfo,sizeof(struct mcd_volinfo)) < 0) {
778 printf("mcd%d: mcd_volinfo: error read data\n",unit);
779 return -1;
780 }
781
782 if (cd->volinfo.trk_low != 0 || cd->volinfo.trk_high != 0) {
783 cd->flags |= MCDVOLINFO; /* volinfo is OK */
784 return 0;
785 }
786
787 return -1;
788 }
789
790 int mcdintr(unit)
791 {
792 int port = mcd_data[unit].iobase;
793 u_int i;
794
795 MCD_TRACE("stray interrupt xfer=0x%x\n",inb(port+mcd_xfer),0,0,0);
796
797 /* just read out status and ignore the rest */
798 if ((inb(port+mcd_xfer)&0xFF) != 0xFF) {
799 i = inb(port+mcd_status);
800 }
801 }
802
803 /* state machine to process read requests
804 * initialize with MCD_S_BEGIN: calculate sizes, and read status
805 * MCD_S_WAITSTAT: wait for status reply, set mode
806 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
807 * MCD_S_WAITREAD: wait for read ready, read data
808 */
809 static struct mcd_mbx *mbxsave;
810
811 static void mcd_doread(int state, struct mcd_mbx *mbxin)
812 {
813 struct mcd_mbx *mbx = (state!=MCD_S_BEGIN) ? mbxsave : mbxin;
814 int unit = mbx->unit;
815 int port = mbx->port;
816 struct buf *bp = mbx->bp;
817 struct mcd_data *cd = mcd_data + unit;
818
819 int rm,i,k;
820 struct mcd_read2 rbuf;
821 int blknum;
822 caddr_t addr;
823
824 loop:
825 switch (state) {
826 case MCD_S_BEGIN:
827 mbx = mbxsave = mbxin;
828
829 case MCD_S_BEGIN1:
830 /* get status */
831 outb(port+mcd_command, MCD_CMDGETSTAT);
832 mbx->count = RDELAY_WAITSTAT;
833 timeout((timeout_t) mcd_doread,(caddr_t) MCD_S_WAITSTAT,hz/100);
834 return;
835 case MCD_S_WAITSTAT:
836 untimeout((timeout_t) mcd_doread,(caddr_t) MCD_S_WAITSTAT);
837 if (mbx->count-- >= 0) {
838 if (inb(port+mcd_xfer) & MCD_ST_BUSY) {
839 timeout((timeout_t) mcd_doread,
840 (caddr_t) MCD_S_WAITSTAT,hz/100);
841 return;
842 }
843 mcd_setflags(unit,cd);
844 MCD_TRACE("got WAITSTAT delay=%d\n",RDELAY_WAITSTAT-mbx->count,0,0,0);
845 /* reject, if audio active */
846 if (cd->status & MCDAUDIOBSY) {
847 printf("mcd%d: audio is active\n",unit);
848 goto readerr;
849 }
850
851 /* to check for raw/cooked mode */
852 if (cd->flags & MCDREADRAW) {
853 rm = MCD_MD_RAW;
854 mbx->sz = MCDRBLK;
855 } else {
856 rm = MCD_MD_COOKED;
857 mbx->sz = cd->blksize;
858 }
859
860 mbx->count = RDELAY_WAITMODE;
861
862 mcd_put(port+mcd_command, MCD_CMDSETMODE);
863 mcd_put(port+mcd_command, rm);
864 timeout((timeout_t) mcd_doread,
865 (caddr_t) MCD_S_WAITMODE,hz/100);
866 return;
867 } else {
868 #ifdef MCD_TO_WARNING_ON
869 printf("mcd%d: timeout getstatus\n",unit);
870 #endif
871 goto readerr;
872 }
873
874 case MCD_S_WAITMODE:
875 untimeout((timeout_t) mcd_doread,(caddr_t) MCD_S_WAITMODE);
876 if (mbx->count-- < 0) {
877 #ifdef MCD_TO_WARNING_ON
878 printf("mcd%d: timeout set mode\n",unit);
879 #endif
880 goto readerr;
881 }
882 if (inb(port+mcd_xfer) & MCD_ST_BUSY) {
883 timeout((timeout_t) mcd_doread,
884 (caddr_t) MCD_S_WAITMODE,hz/100);
885 return;
886 }
887 mcd_setflags(unit,cd);
888 MCD_TRACE("got WAITMODE delay=%d\n",RDELAY_WAITMODE-mbx->count,0,0,0);
889 /* for first block */
890 mbx->nblk = (bp->b_bcount + (mbx->sz-1)) / mbx->sz;
891 mbx->skip = 0;
892
893 nextblock:
894 blknum = (bp->b_blkno / (mbx->sz/DEV_BSIZE))
895 + mbx->p_offset + mbx->skip/mbx->sz;
896
897 MCD_TRACE("mcd_doread: read blknum=%d for bp=0x%x\n",blknum,bp,0,0);
898
899 /* build parameter block */
900 hsg2msf(blknum,rbuf.start_msf);
901
902 /* send the read command */
903 mcd_put(port+mcd_command,MCD_CMDREAD2);
904 mcd_put(port+mcd_command,rbuf.start_msf[0]);
905 mcd_put(port+mcd_command,rbuf.start_msf[1]);
906 mcd_put(port+mcd_command,rbuf.start_msf[2]);
907 mcd_put(port+mcd_command,0);
908 mcd_put(port+mcd_command,0);
909 mcd_put(port+mcd_command,1);
910 mbx->count = RDELAY_WAITREAD;
911 timeout((timeout_t) mcd_doread,(caddr_t) MCD_S_WAITREAD,hz/100);
912 return;
913 case MCD_S_WAITREAD:
914 untimeout((timeout_t) mcd_doread,(caddr_t) MCD_S_WAITREAD);
915 if (mbx->count-- > 0) {
916 k = inb(port+mcd_xfer);
917 if ((k & 2)==0) {
918 MCD_TRACE("got data delay=%d\n",RDELAY_WAITREAD-mbx->count,0,0,0);
919 /* data is ready */
920 addr = bp->b_un.b_addr + mbx->skip;
921 outb(port+mcd_ctl2,0x04); /* XXX */
922 for (i=0; i<mbx->sz; i++)
923 *addr++ = inb(port+mcd_rdata);
924 outb(port+mcd_ctl2,0x0c); /* XXX */
925
926 if (--mbx->nblk > 0) {
927 mbx->skip += mbx->sz;
928 goto nextblock;
929 }
930
931 /* return buffer */
932 bp->b_resid = 0;
933 biodone(bp);
934
935 cd->flags &= ~MCDMBXBSY;
936 mcd_start(mbx->unit);
937 return;
938 }
939 if ((k & 4)==0)
940 mcd_getstat(unit,0);
941 timeout((timeout_t) mcd_doread,
942 (caddr_t) MCD_S_WAITREAD,hz/100);
943 return;
944 } else {
945 #ifdef MCD_TO_WARNING_ON
946 printf("mcd%d: timeout read data\n",unit);
947 #endif
948 goto readerr;
949 }
950 }
951
952 readerr:
953 if (mbx->retry-- > 0) {
954 #ifdef MCD_TO_WARNING_ON
955 printf("mcd%d: retrying\n",unit);
956 #endif
957 state = MCD_S_BEGIN1;
958 goto loop;
959 }
960
961 /* invalidate the buffer */
962 bp->b_flags |= B_ERROR;
963 bp->b_resid = bp->b_bcount;
964 biodone(bp);
965 mcd_start(mbx->unit);
966 return;
967
968 #ifdef NOTDEF
969 printf("mcd%d: unit timeout, resetting\n",mbx->unit);
970 outb(mbx->port+mcd_reset,MCD_CMDRESET);
971 DELAY(300000);
972 (void)mcd_getstat(mbx->unit,1);
973 (void)mcd_getstat(mbx->unit,1);
974 /*cd->status &= ~MCDDSKCHNG; */
975 cd->debug = 1; /* preventive set debug mode */
976
977 #endif
978
979 }
980
981 #ifndef MCDMINI
982 static int mcd_setmode(int unit, int mode)
983 {
984 struct mcd_data *cd = mcd_data + unit;
985 int port = cd->iobase;
986 int retry;
987
988 printf("mcd%d: setting mode to %d\n", unit, mode);
989 for(retry=0; retry<MCD_RETRYS; retry++)
990 {
991 outb(port+mcd_command, MCD_CMDSETMODE);
992 outb(port+mcd_command, mode);
993 if (mcd_getstat(unit, 0) != -1) return 0;
994 }
995
996 return -1;
997 }
998
999 static int mcd_toc_header(int unit, struct ioc_toc_header *th)
1000 {
1001 struct mcd_data *cd = mcd_data + unit;
1002
1003 if (mcd_volinfo(unit) < 0)
1004 return ENXIO;
1005
1006 th->len = msf2hsg(cd->volinfo.vol_msf);
1007 th->starting_track = bcd2bin(cd->volinfo.trk_low);
1008 th->ending_track = bcd2bin(cd->volinfo.trk_high);
1009
1010 return 0;
1011 }
1012
1013 static int mcd_read_toc(int unit)
1014 {
1015 struct mcd_data *cd = mcd_data + unit;
1016 struct ioc_toc_header th;
1017 struct mcd_qchninfo q;
1018 int rc, trk, idx, retry;
1019
1020 /* Only read TOC if needed */
1021 if (cd->flags & MCDTOC) return 0;
1022
1023 printf("mcd%d: reading toc header\n", unit);
1024 if (mcd_toc_header(unit, &th) != 0)
1025 return ENXIO;
1026
1027 printf("mcd%d: stopping play\n", unit);
1028 if ((rc=mcd_stop(unit)) != 0)
1029 return rc;
1030
1031 /* try setting the mode twice */
1032 if (mcd_setmode(unit, MCD_MD_TOC) != 0)
1033 return EIO;
1034 if (mcd_setmode(unit, MCD_MD_TOC) != 0)
1035 return EIO;
1036
1037 printf("mcd%d: get_toc reading qchannel info\n",unit);
1038 for(trk=th.starting_track; trk<=th.ending_track; trk++)
1039 cd->toc[trk].idx_no = 0;
1040 trk = th.ending_track - th.starting_track + 1;
1041 for(retry=0; retry<300 && trk>0; retry++)
1042 {
1043 if (mcd_getqchan(unit, &q) < 0) break;
1044 idx = bcd2bin(q.idx_no);
1045 if (idx>0 && idx < MCD_MAXTOCS && q.trk_no==0)
1046 if (cd->toc[idx].idx_no == 0)
1047 {
1048 cd->toc[idx] = q;
1049 trk--;
1050 }
1051 }
1052
1053 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1054 return EIO;
1055
1056 if (trk != 0) return ENXIO;
1057
1058 /* add a fake last+1 */
1059 idx = th.ending_track + 1;
1060 cd->toc[idx].ctrl_adr = cd->toc[idx-1].ctrl_adr;
1061 cd->toc[idx].trk_no = 0;
1062 cd->toc[idx].idx_no = 0xAA;
1063 cd->toc[idx].hd_pos_msf[0] = cd->volinfo.vol_msf[0];
1064 cd->toc[idx].hd_pos_msf[1] = cd->volinfo.vol_msf[1];
1065 cd->toc[idx].hd_pos_msf[2] = cd->volinfo.vol_msf[2];
1066
1067 cd->flags |= MCDTOC;
1068
1069 return 0;
1070 }
1071
1072 static int mcd_toc_entry(int unit, struct ioc_read_toc_entry *te)
1073 {
1074 struct mcd_data *cd = mcd_data + unit;
1075 struct ret_toc
1076 {
1077 struct ioc_toc_header th;
1078 struct cd_toc_entry rt;
1079 } ret_toc;
1080 struct ioc_toc_header th;
1081 int rc, i;
1082
1083 /* Make sure we have a valid toc */
1084 if ((rc=mcd_read_toc(unit)) != 0)
1085 return rc;
1086
1087 /* find the toc to copy*/
1088 i = te->starting_track;
1089 if (i == MCD_LASTPLUS1)
1090 i = bcd2bin(cd->volinfo.trk_high) + 1;
1091
1092 /* verify starting track */
1093 if (i < bcd2bin(cd->volinfo.trk_low) ||
1094 i > bcd2bin(cd->volinfo.trk_high)+1)
1095 return EINVAL;
1096
1097 /* do we have room */
1098 if (te->data_len < sizeof(struct ioc_toc_header) +
1099 sizeof(struct cd_toc_entry)) return EINVAL;
1100
1101 /* Copy the toc header */
1102 if (mcd_toc_header(unit, &th) < 0) return EIO;
1103 ret_toc.th = th;
1104
1105 /* copy the toc data */
1106 ret_toc.rt.control = cd->toc[i].ctrl_adr;
1107 ret_toc.rt.addr_type = te->address_format;
1108 ret_toc.rt.track = i;
1109 if (te->address_format == CD_MSF_FORMAT)
1110 {
1111 ret_toc.rt.addr[1] = cd->toc[i].hd_pos_msf[0];
1112 ret_toc.rt.addr[2] = cd->toc[i].hd_pos_msf[1];
1113 ret_toc.rt.addr[3] = cd->toc[i].hd_pos_msf[2];
1114 }
1115
1116 /* copy the data back */
1117 copyout(&ret_toc, te->data, sizeof(struct cd_toc_entry)
1118 + sizeof(struct ioc_toc_header));
1119
1120 return 0;
1121 }
1122
1123 static int mcd_stop(int unit)
1124 {
1125 struct mcd_data *cd = mcd_data + unit;
1126
1127 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1128 return ENXIO;
1129 cd->audio_status = CD_AS_PLAY_COMPLETED;
1130 return 0;
1131 }
1132
1133 static int mcd_getqchan(int unit, struct mcd_qchninfo *q)
1134 {
1135 struct mcd_data *cd = mcd_data + unit;
1136
1137 if (mcd_send(unit, MCD_CMDGETQCHN, MCD_RETRYS) < 0)
1138 return -1;
1139 if (mcd_get(unit, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
1140 return -1;
1141 if (cd->debug)
1142 printf("mcd%d: qchannel ctl=%d, t=%d, i=%d, ttm=%d:%d.%d dtm=%d:%d.%d\n",
1143 unit,
1144 q->ctrl_adr, q->trk_no, q->idx_no,
1145 q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2],
1146 q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2]);
1147 return 0;
1148 }
1149
1150 static int mcd_subchan(int unit, struct ioc_read_subchannel *sc)
1151 {
1152 struct mcd_data *cd = mcd_data + unit;
1153 struct mcd_qchninfo q;
1154 struct cd_sub_channel_info data;
1155
1156 printf("mcd%d: subchan af=%d, df=%d\n", unit,
1157 sc->address_format,
1158 sc->data_format);
1159 if (sc->address_format != CD_MSF_FORMAT) return EIO;
1160 if (sc->data_format != CD_CURRENT_POSITION) return EIO;
1161
1162 if (mcd_getqchan(unit, &q) < 0) return EIO;
1163
1164 data.header.audio_status = cd->audio_status;
1165 data.what.position.data_format = CD_MSF_FORMAT;
1166 data.what.position.track_number = bcd2bin(q.trk_no);
1167
1168 if (copyout(&data, sc->data, sizeof(struct cd_sub_channel_info))!=0)
1169 return EFAULT;
1170 return 0;
1171 }
1172
1173 static int mcd_playtracks(int unit, struct ioc_play_track *pt)
1174 {
1175 struct mcd_data *cd = mcd_data + unit;
1176 struct mcd_read2 pb;
1177 int a = pt->start_track;
1178 int z = pt->end_track;
1179 int rc;
1180
1181 if ((rc = mcd_read_toc(unit)) != 0) return rc;
1182
1183 printf("mcd%d: playtracks from %d:%d to %d:%d\n", unit,
1184 a, pt->start_index, z, pt->end_index);
1185
1186 if (a < cd->volinfo.trk_low || a > cd->volinfo.trk_high || a > z ||
1187 z < cd->volinfo.trk_low || z > cd->volinfo.trk_high)
1188 return EINVAL;
1189
1190 pb.start_msf[0] = cd->toc[a].hd_pos_msf[0];
1191 pb.start_msf[1] = cd->toc[a].hd_pos_msf[1];
1192 pb.start_msf[2] = cd->toc[a].hd_pos_msf[2];
1193 pb.end_msf[0] = cd->toc[z+1].hd_pos_msf[0];
1194 pb.end_msf[1] = cd->toc[z+1].hd_pos_msf[1];
1195 pb.end_msf[2] = cd->toc[z+1].hd_pos_msf[2];
1196
1197 return mcd_play(unit, &pb);
1198 }
1199
1200 static int mcd_play(int unit, struct mcd_read2 *pb)
1201 {
1202 struct mcd_data *cd = mcd_data + unit;
1203 int port = cd->iobase;
1204 int retry, st;
1205
1206 cd->lastpb = *pb;
1207 for(retry=0; retry<MCD_RETRYS; retry++)
1208 {
1209 outb(port+mcd_command, MCD_CMDREAD2);
1210 outb(port+mcd_command, pb->start_msf[0]);
1211 outb(port+mcd_command, pb->start_msf[1]);
1212 outb(port+mcd_command, pb->start_msf[2]);
1213 outb(port+mcd_command, pb->end_msf[0]);
1214 outb(port+mcd_command, pb->end_msf[1]);
1215 outb(port+mcd_command, pb->end_msf[2]);
1216 if ((st=mcd_getstat(unit, 0)) != -1) break;
1217 }
1218
1219 if (cd->debug)
1220 printf("mcd%d: mcd_play retry=%d, status=%d\n", unit, retry, st);
1221 if (st == -1) return ENXIO;
1222 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
1223 return 0;
1224 }
1225
1226 static int mcd_pause(int unit)
1227 {
1228 struct mcd_data *cd = mcd_data + unit;
1229 struct mcd_qchninfo q;
1230 int rc;
1231
1232 /* Verify current status */
1233 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS)
1234 {
1235 printf("mcd%d: pause attempted when not playing\n", unit);
1236 return EINVAL;
1237 }
1238
1239 /* Get the current position */
1240 if (mcd_getqchan(unit, &q) < 0) return EIO;
1241
1242 /* Copy it into lastpb */
1243 cd->lastpb.start_msf[0] = q.hd_pos_msf[0];
1244 cd->lastpb.start_msf[1] = q.hd_pos_msf[1];
1245 cd->lastpb.start_msf[2] = q.hd_pos_msf[2];
1246
1247 /* Stop playing */
1248 if ((rc=mcd_stop(unit)) != 0) return rc;
1249
1250 /* Set the proper status and exit */
1251 cd->audio_status = CD_AS_PLAY_PAUSED;
1252 return 0;
1253 }
1254
1255 static int mcd_resume(int unit)
1256 {
1257 struct mcd_data *cd = mcd_data + unit;
1258
1259 if (cd->audio_status != CD_AS_PLAY_PAUSED) return EINVAL;
1260 return mcd_play(unit, &cd->lastpb);
1261 }
1262 #endif /*!MCDMINI*/
1263
1264 #endif /* NMCD > 0 */
1265