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