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