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