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