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