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