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