]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/scd.c
This commit was generated by cvs2svn to compensate for changes in r75584,
[FreeBSD/FreeBSD.git] / sys / i386 / isa / scd.c
1 /*-
2  * Copyright (c) 1995 Mikael Hybsch
3  * All rights reserved.
4  *
5  * Portions of this file are copied from mcd.c
6  * which has the following copyrights:
7  *
8  *      Copyright 1993 by Holger Veit (data part)
9  *      Copyright 1993 by Brian Moore (audio part)
10  *      Changes Copyright 1993 by Gary Clark II
11  *      Changes Copyright (C) 1994 by Andrew A. Chernov
12  *
13  *      Rewrote probe routine to work on newer Mitsumi drives.
14  *      Additional changes (C) 1994 by Jordan K. Hubbard
15  *
16  *      All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer
23  *    in this position and unchanged.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 3. The name of the author may not be used to endorse or promote products
28  *    derived from this software withough specific prior written permission
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  */
42
43
44 /* $FreeBSD$ */
45
46 /* Please send any comments to micke@dynas.se */
47
48 #define SCD_DEBUG       0
49
50 #include "scd.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/conf.h>
55 #include <sys/bio.h>
56 #include <sys/cdio.h>
57 #include <sys/disklabel.h>
58 #include <sys/bus.h>
59
60 #include <machine/stdarg.h>
61
62 #include <i386/isa/isa_device.h>
63 #include <i386/isa/scdreg.h>
64
65
66 #define scd_part(dev)   ((minor(dev)) & 7)
67 #define scd_unit(dev)   (((minor(dev)) & 0x38) >> 3)
68 #define scd_phys(dev)   (((minor(dev)) & 0x40) >> 6)
69 #define RAW_PART        2
70
71 /* flags */
72 #define SCDOPEN         0x0001  /* device opened */
73 #define SCDVALID        0x0002  /* parameters loaded */
74 #define SCDINIT         0x0004  /* device is init'd */
75 #define SCDPROBING      0x0020  /* probing */
76 #define SCDTOC          0x0100  /* already read toc */
77 #define SCDMBXBSY       0x0200  /* local mbx is busy */
78 #define SCDSPINNING     0x0400  /* drive is spun up */
79
80 #define SCD_S_BEGIN     0
81 #define SCD_S_BEGIN1    1
82 #define SCD_S_WAITSTAT  2
83 #define SCD_S_WAITFIFO  3
84 #define SCD_S_WAITSPIN  4
85 #define SCD_S_WAITREAD  5
86 #define SCD_S_WAITPARAM 6
87
88 #define RDELAY_WAIT     300
89 #define RDELAY_WAITREAD 300
90
91 #define SCDBLKSIZE      2048
92
93 #ifdef SCD_DEBUG
94    static int scd_debuglevel = SCD_DEBUG;
95 #  define XDEBUG(level, data) {if (scd_debuglevel >= level) printf data;}
96 #else
97 #  define XDEBUG(level, data)
98 #endif
99
100 struct scd_mbx {
101         short           unit;
102         short           port;
103         short           retry;
104         short           nblk;
105         int             sz;
106         u_long          skip;
107         struct bio      *bp;
108         int             p_offset;
109         short           count;
110 };
111
112 static struct scd_data {
113         int     iobase;
114         char    double_speed;
115         char    *name;
116         short   flags;
117         int     blksize;
118         u_long  disksize;
119         struct disklabel dlabel;
120         int     openflag;
121         struct {
122                 unsigned int  adr :4;
123                 unsigned int  ctl :4; /* xcdplayer needs this */
124                 unsigned char start_msf[3];
125         } toc[MAX_TRACKS];
126         short   first_track;
127         short   last_track;
128         struct  ioc_play_msf last_play;
129
130         short   audio_status;
131         struct bio_queue_head head;             /* head of bio queue */
132         struct scd_mbx mbx;
133 } scd_data[NSCD];
134
135 /* prototypes */
136 static  void    hsg2msf(int hsg, bcd_t *msf);
137 static  int     msf2hsg(bcd_t *msf);
138
139 static void process_attention(unsigned unit);
140 static __inline void write_control(unsigned port, unsigned data);
141 static int waitfor_status_bits(int unit, int bits_set, int bits_clear);
142 static int send_cmd(u_int unit, u_char cmd, u_int nargs, ...);
143 static void init_drive(unsigned unit);
144 static int spin_up(unsigned unit);
145 static int read_toc(unsigned unit);
146 static int get_result(u_int unit, int result_len, u_char *result);
147 static void print_error(int unit, int errcode);
148
149 static void scd_start(int unit);
150 static timeout_t scd_timeout;
151 static void scd_doread(int state, struct scd_mbx *mbxin);
152
153 static int scd_eject(int unit);
154 static int scd_stop(int unit);
155 static int scd_pause(int unit);
156 static int scd_resume(int unit);
157 static int scd_playtracks(int unit, struct ioc_play_track *pt);
158 static int scd_playmsf(int unit, struct ioc_play_msf *msf);
159 static int scd_play(int unit, struct ioc_play_msf *msf);
160 static int scd_subchan(int unit, struct ioc_read_subchannel *sc);
161 static int read_subcode(int unit, struct sony_subchannel_position_data *sc);
162
163 /* for xcdplayer */
164 static int scd_toc_header(int unit, struct ioc_toc_header *th);
165 static int scd_toc_entrys(int unit, struct ioc_read_toc_entry *te);
166 static int scd_toc_entry(int unit, struct ioc_read_toc_single_entry *te);
167 #define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */
168
169 static int      scd_probe(struct isa_device *dev);
170 static int      scd_attach(struct isa_device *dev);
171 struct  isa_driver      scddriver = {
172         INTR_TYPE_BIO,
173         scd_probe,
174         scd_attach,
175         "scd"
176 };
177 COMPAT_ISA_DRIVER(scd, scddriver);
178
179 /* For canceling our timeout */
180 static struct callout_handle tohandle = CALLOUT_HANDLE_INITIALIZER(&tohanle);
181
182 static  d_open_t        scdopen;
183 static  d_close_t       scdclose;
184 static  d_ioctl_t       scdioctl;
185 static  d_strategy_t    scdstrategy;
186
187 #define CDEV_MAJOR 45
188
189 static struct cdevsw scd_cdevsw = {
190         /* open */      scdopen,
191         /* close */     scdclose,
192         /* read */      physread,
193         /* write */     nowrite,
194         /* ioctl */     scdioctl,
195         /* poll */      nopoll,
196         /* mmap */      nommap,
197         /* strategy */  scdstrategy,
198         /* name */      "scd",
199         /* maj */       CDEV_MAJOR,
200         /* dump */      nodump,
201         /* psize */     nopsize,
202         /* flags */     D_DISK,
203 };
204
205
206 static int
207 scd_attach(struct isa_device *dev)
208 {
209         int     unit = dev->id_unit;
210         struct scd_data *cd = scd_data + unit;
211
212         cd->iobase = dev->id_iobase;    /* Already set by probe, but ... */
213
214         /* name filled in probe */
215         printf("scd%d: <%s>\n", dev->id_unit, scd_data[dev->id_unit].name);
216
217         init_drive(dev->id_unit);
218
219         cd->flags = SCDINIT;
220         cd->audio_status = CD_AS_AUDIO_INVALID;
221         bioq_init(&cd->head);
222
223         make_dev(&scd_cdevsw, dkmakeminor(unit, 0, 0),
224             UID_ROOT, GID_OPERATOR, 0640, "rscd%da", unit);
225         make_dev(&scd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
226             UID_ROOT, GID_OPERATOR, 0640, "rscd%dc", unit);
227         make_dev(&scd_cdevsw, dkmakeminor(unit, 0, 0),
228             UID_ROOT, GID_OPERATOR, 0640, "scd%da", unit);
229         make_dev(&scd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
230             UID_ROOT, GID_OPERATOR, 0640, "scd%dc", unit);
231         return 1;
232 }
233
234 static  int
235 scdopen(dev_t dev, int flags, int fmt, struct proc *p)
236 {
237         int unit,part,phys;
238         int rc;
239         struct scd_data *cd;
240
241         unit = scd_unit(dev);
242         if (unit >= NSCD)
243                 return ENXIO;
244
245         cd = scd_data + unit;
246         part = scd_part(dev);
247         phys = scd_phys(dev);
248
249         /* not initialized*/
250         if (!(cd->flags & SCDINIT))
251                 return ENXIO;
252
253         /* invalidated in the meantime? mark all open part's invalid */
254         if (cd->openflag)
255                 return ENXIO;
256
257         XDEBUG(1,("scd%d: DEBUG: status = 0x%x\n", unit, inb(cd->iobase+IREG_STATUS)));
258
259         if ((rc = spin_up(unit)) != 0) {
260                 print_error(unit, rc);
261                 return EIO;
262         }
263         if (!(cd->flags & SCDTOC)) {
264                 int loop_count = 3;
265
266                 while (loop_count-- > 0 && (rc = read_toc(unit)) != 0) {
267                         if (rc == ERR_NOT_SPINNING) {
268                                 rc = spin_up(unit);
269                                 if (rc) {
270                                         print_error(unit, rc);\
271                                         return EIO;
272                                 }
273                                 continue;
274                         }
275                         printf("scd%d: TOC read error 0x%x\n", unit, rc);
276                         return EIO;
277                 }
278         }
279
280         dev->si_bsize_phys = cd->blksize;
281
282         cd->openflag = 1;
283         cd->flags |= SCDVALID;
284
285         return 0;
286 }
287
288 static  int
289 scdclose(dev_t dev, int flags, int fmt, struct proc *p)
290 {
291         int unit,part,phys;
292         struct scd_data *cd;
293
294         unit = scd_unit(dev);
295         if (unit >= NSCD)
296                 return ENXIO;
297
298         cd = scd_data + unit;
299         part = scd_part(dev);
300         phys = scd_phys(dev);
301
302         if (!(cd->flags & SCDINIT) || !cd->openflag)
303                 return ENXIO;
304
305         if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) {
306                 (void)send_cmd(unit, CMD_SPIN_DOWN, 0);
307                 cd->flags &= ~SCDSPINNING;
308         }
309
310
311         /* close channel */
312         cd->openflag = 0;
313
314         return 0;
315 }
316
317 static  void
318 scdstrategy(struct bio *bp)
319 {
320         struct scd_data *cd;
321         int s;
322         int unit = scd_unit(bp->bio_dev);
323
324         cd = scd_data + unit;
325
326         XDEBUG(2, ("scd%d: DEBUG: strategy: block=%ld, bcount=%ld\n",
327                 unit, (long)bp->bio_blkno, bp->bio_bcount));
328
329         if (unit >= NSCD || bp->bio_blkno < 0 || (bp->bio_bcount % SCDBLKSIZE)) {
330                 printf("scd%d: strategy failure: blkno = %ld, bcount = %ld\n",
331                         unit, (long)bp->bio_blkno, bp->bio_bcount);
332                 bp->bio_error = EINVAL;
333                 bp->bio_flags |= BIO_ERROR;
334                 goto bad;
335         }
336
337         /* if device invalidated (e.g. media change, door open), error */
338         if (!(cd->flags & SCDVALID)) {
339                 printf("scd%d: media changed\n", unit);
340                 bp->bio_error = EIO;
341                 goto bad;
342         }
343
344         /* read only */
345         if (!(bp->bio_cmd == BIO_READ)) {
346                 bp->bio_error = EROFS;
347                 goto bad;
348         }
349
350         /* no data to read */
351         if (bp->bio_bcount == 0)
352                 goto done;
353
354         if (!(cd->flags & SCDTOC)) {
355                 bp->bio_error = EIO;
356                 goto bad;
357         }
358         /* adjust transfer if necessary */
359         if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0)
360                 goto done;
361
362         bp->bio_pblkno = bp->bio_blkno;
363         bp->bio_resid = 0;
364
365         /* queue it */
366         s = splbio();
367         bioqdisksort(&cd->head, bp);
368         splx(s);
369
370         /* now check whether we can perform processing */
371         scd_start(unit);
372         return;
373
374 bad:
375         bp->bio_flags |= BIO_ERROR;
376 done:
377         bp->bio_resid = bp->bio_bcount;
378         biodone(bp);
379         return;
380 }
381
382 static void
383 scd_start(int unit)
384 {
385         struct scd_data *cd = scd_data + unit;
386         struct bio *bp;
387         struct partition *p;
388         int s = splbio();
389
390         if (cd->flags & SCDMBXBSY) {
391                 splx(s);
392                 return;
393         }
394
395         bp = bioq_first(&cd->head);
396         if (bp != 0) {
397                 /* block found to process, dequeue */
398                 bioq_remove(&cd->head, bp);
399                 cd->flags |= SCDMBXBSY;
400                 splx(s);
401         } else {
402                 /* nothing to do */
403                 splx(s);
404                 return;
405         }
406
407         p = cd->dlabel.d_partitions + scd_part(bp->bio_dev);
408
409         cd->mbx.unit = unit;
410         cd->mbx.port = cd->iobase;
411         cd->mbx.retry = 3;
412         cd->mbx.bp = bp;
413         cd->mbx.p_offset = p->p_offset;
414         splx(s);
415
416         scd_doread(SCD_S_BEGIN,&(cd->mbx));
417         return;
418 }
419
420 static  int
421 scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
422 {
423         struct scd_data *cd;
424         int unit,part;
425
426         unit = scd_unit(dev);
427         part = scd_part(dev);
428         cd = scd_data + unit;
429
430         XDEBUG(1, ("scd%d: ioctl: cmd=0x%lx\n", unit, cmd));
431
432         if (!(cd->flags & SCDVALID))
433                 return EIO;
434
435         switch (cmd) {
436         case DIOCGDINFO:
437                 *(struct disklabel *)addr = cd->dlabel;
438                 return 0;
439         case DIOCGPART:
440                 ((struct partinfo *)addr)->disklab = &cd->dlabel;
441                 ((struct partinfo *)addr)->part =
442                         &cd->dlabel.d_partitions[0];
443                 return 0;
444         case CDIOCPLAYTRACKS:
445                 return scd_playtracks(unit, (struct ioc_play_track *) addr);
446         case CDIOCPLAYBLOCKS:
447                 return EINVAL;
448         case CDIOCPLAYMSF:
449                 return scd_playmsf(unit, (struct ioc_play_msf *) addr);
450         case CDIOCREADSUBCHANNEL:
451                 return scd_subchan(unit, (struct ioc_read_subchannel *) addr);
452         case CDIOREADTOCHEADER:
453                 return scd_toc_header (unit, (struct ioc_toc_header *) addr);
454         case CDIOREADTOCENTRYS:
455                 return scd_toc_entrys (unit, (struct ioc_read_toc_entry*) addr);
456         case CDIOREADTOCENTRY:
457                 return scd_toc_entry (unit, (struct ioc_read_toc_single_entry*) addr);
458         case CDIOCSETPATCH:
459         case CDIOCGETVOL:
460         case CDIOCSETVOL:
461         case CDIOCSETMONO:
462         case CDIOCSETSTERIO:
463         case CDIOCSETMUTE:
464         case CDIOCSETLEFT:
465         case CDIOCSETRIGHT:
466                 return EINVAL;
467         case CDIOCRESUME:
468                 return scd_resume(unit);
469         case CDIOCPAUSE:
470                 return scd_pause(unit);
471         case CDIOCSTART:
472                 return EINVAL;
473         case CDIOCSTOP:
474                 return scd_stop(unit);
475         case CDIOCEJECT:
476                 return scd_eject(unit);
477         case CDIOCALLOW:
478                 return 0;
479         case CDIOCSETDEBUG:
480 #ifdef SCD_DEBUG
481                 scd_debuglevel++;
482 #endif
483                 return 0;
484         case CDIOCCLRDEBUG:
485 #ifdef SCD_DEBUG
486                 scd_debuglevel = 0;
487
488 #endif
489                 return 0;
490         default:
491                 printf("scd%d: unsupported ioctl (cmd=0x%lx)\n", unit, cmd);
492                 return ENOTTY;
493         }
494 }
495
496 /***************************************************************
497  * lower level of driver starts here
498  **************************************************************/
499
500 static int
501 scd_playtracks(int unit, struct ioc_play_track *pt)
502 {
503         struct scd_data *cd = scd_data + unit;
504         struct ioc_play_msf msf;
505         int a = pt->start_track;
506         int z = pt->end_track;
507         int rc;
508
509         if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
510                 if (rc == -ERR_NOT_SPINNING) {
511                         if (spin_up(unit) != 0)
512                                 return EIO;
513                         rc = read_toc(unit);
514                 }
515                 if (rc != 0) {
516                         print_error(unit, rc);
517                         return EIO;
518                 }
519         }
520
521         XDEBUG(1, ("scd%d: playtracks from %d:%d to %d:%d\n", unit,
522                 a, pt->start_index, z, pt->end_index));
523
524         if (   a < cd->first_track
525             || a > cd->last_track
526             || a > z
527             || z > cd->last_track)
528                 return EINVAL;
529
530         bcopy(cd->toc[a].start_msf, &msf.start_m, 3);
531         hsg2msf(msf2hsg(cd->toc[z+1].start_msf)-1, &msf.end_m);
532
533         return scd_play(unit, &msf);
534 }
535
536 /* The start/end msf is expected to be in bin format */
537 static int
538 scd_playmsf(int unit, struct ioc_play_msf *msfin)
539 {
540         struct ioc_play_msf msf;
541
542         msf.start_m = bin2bcd(msfin->start_m);
543         msf.start_s = bin2bcd(msfin->start_s);
544         msf.start_f = bin2bcd(msfin->start_f);
545         msf.end_m = bin2bcd(msfin->end_m);
546         msf.end_s = bin2bcd(msfin->end_s);
547         msf.end_f = bin2bcd(msfin->end_f);
548
549         return scd_play(unit, &msf);
550 }
551
552 /* The start/end msf is expected to be in bcd format */
553 static int
554 scd_play(int unit, struct ioc_play_msf *msf)
555 {
556         struct scd_data *cd = scd_data + unit;
557         int i, rc;
558
559         XDEBUG(1, ("scd%d: playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", unit,
560                 msf->start_m, msf->start_s, msf->start_f,
561                 msf->end_m, msf->end_s, msf->end_f));
562
563         for (i = 0; i < 2; i++) {
564                 rc = send_cmd(unit, CMD_PLAY_AUDIO, 7,
565                         0x03,
566                         msf->start_m, msf->start_s, msf->start_f,
567                         msf->end_m, msf->end_s, msf->end_f);
568                 if (rc == -ERR_NOT_SPINNING) {
569                         cd->flags &= ~SCDSPINNING;
570                         if (spin_up(unit) != 0)
571                                 return EIO;
572                 } else if (rc < 0) {
573                         print_error(unit, rc);
574                         return EIO;
575                 } else {
576                         break;
577                 }
578         }
579         cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
580         bcopy((char *)msf, (char *)&cd->last_play, sizeof(struct ioc_play_msf));
581         return 0;
582 }
583
584 static int
585 scd_stop(int unit)
586 {
587         struct scd_data *cd = scd_data + unit;
588
589         (void)send_cmd(unit, CMD_STOP_AUDIO, 0);
590         cd->audio_status = CD_AS_PLAY_COMPLETED;
591         return 0;
592 }
593
594 static int
595 scd_pause(int unit)
596 {
597         struct scd_data *cd = scd_data + unit;
598         struct sony_subchannel_position_data subpos;
599
600         if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS)
601                 return EINVAL;
602
603         if (read_subcode(unit, &subpos) != 0)
604                 return EIO;
605
606         if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0)
607                 return EIO;
608
609         cd->last_play.start_m = subpos.abs_msf[0];
610         cd->last_play.start_s = subpos.abs_msf[1];
611         cd->last_play.start_f = subpos.abs_msf[2];
612         cd->audio_status = CD_AS_PLAY_PAUSED;
613
614         XDEBUG(1, ("scd%d: pause @ %02x:%02x:%02x\n", unit,
615                 cd->last_play.start_m,
616                 cd->last_play.start_s,
617                 cd->last_play.start_f));
618
619         return 0;
620 }
621
622 static int
623 scd_resume(int unit)
624 {
625         if (scd_data[unit].audio_status != CD_AS_PLAY_PAUSED)
626                 return EINVAL;
627         return scd_play(unit, &scd_data[unit].last_play);
628 }
629
630 static int
631 scd_eject(int unit)
632 {
633         struct scd_data *cd = scd_data + unit;
634
635         cd->audio_status = CD_AS_AUDIO_INVALID;
636         cd->flags &= ~(SCDSPINNING|SCDTOC);
637
638         if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0 ||
639             send_cmd(unit, CMD_SPIN_DOWN, 0) != 0 ||
640             send_cmd(unit, CMD_EJECT, 0) != 0)
641         {
642                 return EIO;
643         }
644         return 0;
645 }
646
647 static int
648 scd_subchan(int unit, struct ioc_read_subchannel *sc)
649 {
650         struct scd_data *cd = scd_data + unit;
651         struct sony_subchannel_position_data q;
652         struct cd_sub_channel_info data;
653
654         XDEBUG(1, ("scd%d: subchan af=%d, df=%d\n", unit,
655                 sc->address_format,
656                 sc->data_format));
657
658         if (sc->address_format != CD_MSF_FORMAT)
659                 return EINVAL;
660
661         if (sc->data_format != CD_CURRENT_POSITION)
662                 return EINVAL;
663
664         if (read_subcode(unit, &q) != 0)
665                 return EIO;
666
667         data.header.audio_status = cd->audio_status;
668         data.what.position.data_format = CD_MSF_FORMAT;
669         data.what.position.track_number = bcd2bin(q.track_number);
670         data.what.position.reladdr.msf.unused = 0;
671         data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]);
672         data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]);
673         data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]);
674         data.what.position.absaddr.msf.unused = 0;
675         data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]);
676         data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]);
677         data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]);
678
679         if (copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len))!=0)
680                 return EFAULT;
681         return 0;
682 }
683
684 static __inline void
685 write_control(unsigned port, unsigned data)
686 {
687         outb(port + OREG_CONTROL, data);
688 }
689
690 static int
691 scd_probe(struct isa_device *dev)
692 {
693         struct sony_drive_configuration drive_config;
694         int unit = dev->id_unit;
695         int rc;
696         static char namebuf[8+16+8+3];
697         char *s = namebuf;
698         int loop_count = 0;
699         static int once;
700
701         if (!once++)
702                 cdevsw_add(&scd_cdevsw);
703
704         scd_data[unit].flags = SCDPROBING;
705         scd_data[unit].iobase = dev->id_iobase;
706
707         bzero(&drive_config, sizeof(drive_config));
708
709 again:
710         /* Reset drive */
711         write_control(dev->id_iobase, CBIT_RESET_DRIVE);
712
713         /* Calm down */
714         DELAY(300000);
715
716         /* Only the ATTENTION bit may be set */
717         if ((inb(dev->id_iobase+IREG_STATUS) & ~1) != 0) {
718                 XDEBUG(1, ("scd: too many bits set. probe failed.\n"));
719                 return 0;
720         }
721         rc = send_cmd(unit, CMD_GET_DRIVE_CONFIG, 0);
722         if (rc != sizeof(drive_config)) {
723                 /* Sometimes if the drive is playing audio I get */
724                 /* the bad result 82. Fix by repeating the reset */
725                 if (rc > 0 && loop_count++ == 0)
726                         goto again;
727                 return 0;
728         }
729         if (get_result(unit, rc, (u_char *)&drive_config) != 0)
730                 return 0;
731
732         bcopy(drive_config.vendor, namebuf, 8);
733         s = namebuf+8;
734         while (*(s-1) == ' ')   /* Strip trailing spaces */
735                 s--;
736         *s++ = ' ';
737         bcopy(drive_config.product, s, 16);
738         s += 16;
739         while (*(s-1) == ' ')
740                 s--;
741         *s++ = ' ';
742         bcopy(drive_config.revision, s, 8);
743         s += 8;
744         while (*(s-1) == ' ')
745                 s--;
746         *s = 0;
747
748         scd_data[unit].name = namebuf;
749
750         if (drive_config.config & 0x10)
751                 scd_data[unit].double_speed = 1;
752         else
753                 scd_data[unit].double_speed = 0;
754
755         return 4;
756 }
757
758 static int
759 read_subcode(int unit, struct sony_subchannel_position_data *sc)
760 {
761         int rc;
762
763         rc = send_cmd(unit, CMD_GET_SUBCHANNEL_DATA, 0);
764         if (rc < 0 || rc < sizeof(*sc))
765                 return EIO;
766         if (get_result(unit, rc, (u_char *)sc) != 0)
767                 return EIO;
768         return 0;
769 }
770
771 /* State machine copied from mcd.c */
772
773 /* This (and the code in mcd.c) will not work with more than one drive */
774 /* because there is only one mbxsave below. Should fix that some day. */
775 /* (mbxsave & state should probably be included in the scd_data struct and */
776 /*  the unit number used as first argument to scd_doread().) /Micke */
777
778 /* state machine to process read requests
779  * initialize with SCD_S_BEGIN: reset state machine
780  * SCD_S_WAITSTAT:  wait for ready (!busy)
781  * SCD_S_WAITSPIN:  wait for drive to spin up (if not spinning)
782  * SCD_S_WAITFIFO:  wait for param fifo to get ready, them exec. command.
783  * SCD_S_WAITREAD:  wait for data ready, read data
784  * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read.
785  */
786
787 static struct scd_mbx *mbxsave;
788
789 static void
790 scd_timeout(void *arg)
791 {
792         scd_doread((int)arg, mbxsave);
793 }
794
795 static void
796 scd_doread(int state, struct scd_mbx *mbxin)
797 {
798         struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? mbxsave : mbxin;
799         int     unit = mbx->unit;
800         int     port = mbx->port;
801         struct  bio *bp = mbx->bp;
802         struct  scd_data *cd = scd_data + unit;
803         int     reg,i;
804         int     blknum;
805         caddr_t addr;
806         static char sdata[3];   /* Must be preserved between calls to this function */
807
808 loop:
809         switch (state) {
810         case SCD_S_BEGIN:
811                 mbx = mbxsave = mbxin;
812
813         case SCD_S_BEGIN1:
814                 /* get status */
815                 mbx->count = RDELAY_WAIT;
816
817                 process_attention(unit);
818                 goto trystat;
819
820         case SCD_S_WAITSTAT:
821                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITSTAT, tohandle);
822                 if (mbx->count-- <= 0) {
823                         printf("scd%d: timeout. drive busy.\n",unit);
824                         goto harderr;
825                 }
826
827 trystat:
828                 if (IS_BUSY(port)) {
829                         tohandle = timeout(scd_timeout,
830                                            (caddr_t)SCD_S_WAITSTAT,hz/100); /* XXX */
831                         return;
832                 }
833
834                 process_attention(unit);
835
836                 /* reject, if audio active */
837                 if (cd->audio_status & CD_AS_PLAY_IN_PROGRESS) {
838                         printf("scd%d: audio is active\n",unit);
839                         goto harderr;
840                 }
841
842                 mbx->sz = cd->blksize;
843
844                 /* for first block */
845                 mbx->nblk = (bp->bio_bcount + (mbx->sz-1)) / mbx->sz;
846                 mbx->skip = 0;
847
848 nextblock:
849                 if (!(cd->flags & SCDVALID))
850                         goto changed;
851
852                 blknum  = (bp->bio_blkno / (mbx->sz/DEV_BSIZE))
853                         + mbx->p_offset + mbx->skip/mbx->sz;
854
855                 XDEBUG(2, ("scd%d: scd_doread: read blknum=%d\n", unit, blknum));
856
857                 /* build parameter block */
858                 hsg2msf(blknum, sdata);
859
860                 write_control(port, CBIT_RESULT_READY_CLEAR);
861                 write_control(port, CBIT_RPARAM_CLEAR);
862                 write_control(port, CBIT_DATA_READY_CLEAR);
863
864                 if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
865                         goto writeparam;
866
867                 mbx->count = 100;
868                 tohandle = timeout(scd_timeout,
869                                    (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */
870                 return;
871
872         case SCD_S_WAITSPIN:
873                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITSPIN, tohandle);
874                 if (mbx->count-- <= 0) {
875                         printf("scd%d: timeout waiting for drive to spin up.\n", unit);
876                         goto harderr;
877                 }
878                 if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
879                         tohandle = timeout(scd_timeout,
880                                            (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */
881                         return;
882                 }
883                 write_control(port, CBIT_RESULT_READY_CLEAR);
884                 switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
885                 case 0x20:
886                         i = inb(port+IREG_RESULT);
887                         print_error(unit, i);
888                         goto harderr;
889                 case 0x00:
890                         (void)inb(port+IREG_RESULT);
891                         cd->flags |= SCDSPINNING;
892                         break;
893                 }
894                 XDEBUG(1, ("scd%d: DEBUG: spin up complete\n", unit));
895
896                 state = SCD_S_BEGIN1;
897                 goto loop;
898
899         case SCD_S_WAITFIFO:
900                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITFIFO, tohandle);
901                 if (mbx->count-- <= 0) {
902                         printf("scd%d: timeout. write param not ready.\n",unit);
903                         goto harderr;
904                 }
905                 if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
906                         tohandle = timeout(scd_timeout,
907                                            (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */
908                         return;
909                 }
910                 XDEBUG(1, ("scd%d: mbx->count (writeparamwait) = %d(%d)\n", unit, mbx->count, 100));
911
912 writeparam:
913                 /* The reason this test isn't done 'till now is to make sure */
914                 /* that it is ok to send the SPIN_UP cmd below. */
915                 if (!(cd->flags & SCDSPINNING)) {
916                         XDEBUG(1, ("scd%d: spinning up drive ...\n", unit));
917                         outb(port+OREG_COMMAND, CMD_SPIN_UP);
918                         mbx->count = 300;
919                         tohandle = timeout(scd_timeout,
920                                            (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */
921                         return;
922                 }
923
924                 reg = port + OREG_WPARAMS;
925                 /* send the read command */
926                 disable_intr();
927                 outb(reg, sdata[0]);
928                 outb(reg, sdata[1]);
929                 outb(reg, sdata[2]);
930                 outb(reg, 0);
931                 outb(reg, 0);
932                 outb(reg, 1);
933                 outb(port+OREG_COMMAND, CMD_READ);
934                 enable_intr();
935
936                 mbx->count = RDELAY_WAITREAD;
937                 for (i = 0; i < 50; i++) {
938                         if (STATUS_BIT(port, SBIT_DATA_READY))
939                                 goto got_data;
940                         DELAY(100);
941                 }
942
943                 tohandle = timeout(scd_timeout,
944                                    (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */
945                 return;
946
947         case SCD_S_WAITREAD:
948                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITREAD, tohandle);
949                 if (mbx->count-- <= 0) {
950                         if (STATUS_BIT(port, SBIT_RESULT_READY))
951                                 goto got_param;
952                         printf("scd%d: timeout while reading data\n",unit);
953                         goto readerr;
954                 }
955                 if (!STATUS_BIT(port, SBIT_DATA_READY)) {
956                         process_attention(unit);
957                         if (!(cd->flags & SCDVALID))
958                                 goto changed;
959                         tohandle = timeout(scd_timeout,
960                                            (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */
961                         return;
962                 }
963                 XDEBUG(2, ("scd%d: mbx->count (after RDY_BIT) = %d(%d)\n", unit, mbx->count, RDELAY_WAITREAD));
964
965 got_data:
966                 /* data is ready */
967                 addr = bp->bio_data + mbx->skip;
968                 write_control(port, CBIT_DATA_READY_CLEAR);
969                 insb(port+IREG_DATA, addr, mbx->sz);
970
971                 mbx->count = 100;
972                 for (i = 0; i < 20; i++) {
973                         if (STATUS_BIT(port, SBIT_RESULT_READY))
974                                 goto waitfor_param;
975                         DELAY(100);
976                 }
977                 goto waitfor_param;
978
979         case SCD_S_WAITPARAM:
980                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITPARAM, tohandle);
981                 if (mbx->count-- <= 0) {
982                         printf("scd%d: timeout waiting for params\n",unit);
983                         goto readerr;
984                 }
985
986 waitfor_param:
987                 if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
988                         tohandle = timeout(scd_timeout,
989                                            (caddr_t)SCD_S_WAITPARAM,hz/100); /* XXX */
990                         return;
991                 }
992 #if SCD_DEBUG
993                 if (mbx->count < 100 && scd_debuglevel > 0)
994                         printf("scd%d: mbx->count (paramwait) = %d(%d)\n", unit, mbx->count, 100);
995 #endif
996
997 got_param:
998                 write_control(port, CBIT_RESULT_READY_CLEAR);
999                 switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
1000                 case 0x50:
1001                         switch (i) {
1002                         case ERR_FATAL_READ_ERROR1:
1003                         case ERR_FATAL_READ_ERROR2:
1004                                 printf("scd%d: unrecoverable read error 0x%x\n", unit, i);
1005                                 goto harderr;
1006                         }
1007                         break;
1008                 case 0x20:
1009                         i = inb(port+IREG_RESULT);
1010                         switch (i) {
1011                         case ERR_NOT_SPINNING:
1012                                 XDEBUG(1, ("scd%d: read error: drive not spinning\n", unit));
1013                                 if (mbx->retry-- > 0) {
1014                                         state = SCD_S_BEGIN1;
1015                                         cd->flags &= ~SCDSPINNING;
1016                                         goto loop;
1017                                 }
1018                                 goto harderr;
1019                         default:
1020                                 print_error(unit, i);
1021                                 goto readerr;
1022                         }
1023                 case 0x00:
1024                         i = inb(port+IREG_RESULT);
1025                         break;
1026                 }
1027
1028                 if (--mbx->nblk > 0) {
1029                         mbx->skip += mbx->sz;
1030                         goto nextblock;
1031                 }
1032
1033                 /* return buffer */
1034                 bp->bio_resid = 0;
1035                 biodone(bp);
1036
1037                 cd->flags &= ~SCDMBXBSY;
1038                 scd_start(mbx->unit);
1039                 return;
1040         }
1041
1042 readerr:
1043         if (mbx->retry-- > 0) {
1044                 printf("scd%d: retrying ...\n",unit);
1045                 state = SCD_S_BEGIN1;
1046                 goto loop;
1047         }
1048 harderr:
1049         /* invalidate the buffer */
1050         bp->bio_error = EIO;
1051         bp->bio_flags |= BIO_ERROR;
1052         bp->bio_resid = bp->bio_bcount;
1053         biodone(bp);
1054
1055         cd->flags &= ~SCDMBXBSY;
1056         scd_start(mbx->unit);
1057         return;
1058
1059 changed:
1060         printf("scd%d: media changed\n", unit);
1061         goto harderr;
1062 }
1063
1064 static void
1065 hsg2msf(int hsg, bcd_t *msf)
1066 {
1067         hsg += 150;
1068         M_msf(msf) = bin2bcd(hsg / 4500);
1069         hsg %= 4500;
1070         S_msf(msf) = bin2bcd(hsg / 75);
1071         F_msf(msf) = bin2bcd(hsg % 75);
1072 }
1073
1074 static int
1075 msf2hsg(bcd_t *msf)
1076 {
1077         return (bcd2bin(M_msf(msf)) * 60 +
1078                 bcd2bin(S_msf(msf))) * 75 +
1079                 bcd2bin(F_msf(msf)) - 150;
1080 }
1081
1082 static void
1083 process_attention(unsigned unit)
1084 {
1085         unsigned port = scd_data[unit].iobase;
1086         unsigned char code;
1087         int count = 0;
1088
1089         while (IS_ATTENTION(port) && count++ < 30) {
1090                 write_control(port, CBIT_ATTENTION_CLEAR);
1091                 code = inb(port+IREG_RESULT);
1092
1093 #if SCD_DEBUG
1094                 if (scd_debuglevel > 0) {
1095                         if (count == 1)
1096                                 printf("scd%d: DEBUG: ATTENTIONS = 0x%x", unit, code);
1097                         else
1098                                 printf(",0x%x", code);
1099                 }
1100 #endif
1101
1102                 switch (code) {
1103                 case ATTEN_SPIN_DOWN:
1104                         scd_data[unit].flags &= ~SCDSPINNING;
1105                         break;
1106
1107                 case ATTEN_SPIN_UP_DONE:
1108                         scd_data[unit].flags |= SCDSPINNING;
1109                         break;
1110
1111                 case ATTEN_AUDIO_DONE:
1112                         scd_data[unit].audio_status = CD_AS_PLAY_COMPLETED;
1113                         break;
1114
1115                 case ATTEN_DRIVE_LOADED:
1116                         scd_data[unit].flags &= ~(SCDTOC|SCDSPINNING|SCDVALID);
1117                         scd_data[unit].audio_status = CD_AS_AUDIO_INVALID;
1118                         break;
1119
1120                 case ATTEN_EJECT_PUSHED:
1121                         scd_data[unit].flags &= ~SCDVALID;
1122                         break;
1123                 }
1124                 DELAY(100);
1125         }
1126 #if SCD_DEBUG
1127         if (scd_debuglevel > 0 && count > 0)
1128                 printf("\n");
1129 #endif
1130 }
1131
1132 /* Returns 0 OR sony error code */
1133 static int
1134 spin_up(unsigned unit)
1135 {
1136         unsigned char res_reg[12];
1137         unsigned int res_size;
1138         int rc;
1139         int loop_count = 0;
1140
1141 again:
1142         rc = send_cmd(unit, CMD_SPIN_UP, 0, 0, res_reg, &res_size);
1143         if (rc != 0) {
1144                 XDEBUG(2, ("scd%d: CMD_SPIN_UP error 0x%x\n", unit, rc));
1145                 return rc;
1146         }
1147
1148         if (!(scd_data[unit].flags & SCDTOC)) {
1149                 rc = send_cmd(unit, CMD_READ_TOC, 0);
1150                 if (rc == ERR_NOT_SPINNING) {
1151                         if (loop_count++ < 3)
1152                                 goto again;
1153                         return rc;
1154                 }
1155                 if (rc != 0)
1156                         return rc;
1157         }
1158
1159         scd_data[unit].flags |= SCDSPINNING;
1160
1161         return 0;
1162 }
1163
1164 static struct sony_tracklist *
1165 get_tl(struct sony_toc *toc, int size)
1166 {
1167         struct sony_tracklist *tl = &toc->tracks[0];
1168
1169         if (tl->track != 0xb0)
1170                 return tl;
1171         (char *)tl += 9;
1172         if (tl->track != 0xb1)
1173                 return tl;
1174         (char *)tl += 9;
1175         if (tl->track != 0xb2)
1176                 return tl;
1177         (char *)tl += 9;
1178         if (tl->track != 0xb3)
1179                 return tl;
1180         (char *)tl += 9;
1181         if (tl->track != 0xb4)
1182                 return tl;
1183         (char *)tl += 9;
1184         if (tl->track != 0xc0)
1185                 return tl;
1186         (char *)tl += 9;
1187         return tl;
1188 }
1189
1190 static int
1191 read_toc(unsigned unit)
1192 {
1193         struct scd_data *cd;
1194         unsigned part = 0;      /* For now ... */
1195         struct sony_toc toc;
1196         struct sony_tracklist *tl;
1197         int rc, i, j;
1198         u_long first, last;
1199
1200         cd = scd_data + unit;
1201
1202         rc = send_cmd(unit, CMD_GET_TOC, 1, part+1);
1203         if (rc < 0)
1204                 return rc;
1205         if (rc > sizeof(toc)) {
1206                 printf("scd%d: program error: toc too large (%d)\n", unit, rc);
1207                 return EIO;
1208         }
1209         if (get_result(unit, rc, (u_char *)&toc) != 0)
1210                 return EIO;
1211
1212         XDEBUG(1, ("scd%d: toc read. len = %d, sizeof(toc) = %d\n", unit, rc, sizeof(toc)));
1213
1214         tl = get_tl(&toc, rc);
1215         first = msf2hsg(tl->start_msf);
1216         last = msf2hsg(toc.lead_out_start_msf);
1217         cd->blksize = SCDBLKSIZE;
1218         cd->disksize = last*cd->blksize/DEV_BSIZE;
1219
1220         XDEBUG(1, ("scd%d: firstsector = %ld, lastsector = %ld", unit,
1221                         first, last));
1222
1223         cd->first_track = bcd2bin(toc.first_track);
1224         cd->last_track = bcd2bin(toc.last_track);
1225         if (cd->last_track > (MAX_TRACKS-2))
1226                 cd->last_track = MAX_TRACKS-2;
1227         for (j = 0, i = cd->first_track; i <= cd->last_track; i++, j++) {
1228                 cd->toc[i].adr = tl[j].adr;
1229                 cd->toc[i].ctl = tl[j].ctl; /* for xcdplayer */
1230                 bcopy(tl[j].start_msf, cd->toc[i].start_msf, 3);
1231 #ifdef SCD_DEBUG
1232                 if (scd_debuglevel > 0) {
1233                         if ((j % 3) == 0)
1234                                 printf("\nscd%d: tracks ", unit);
1235                         printf("[%03d: %2d %2d %2d]  ", i,
1236                                 bcd2bin(cd->toc[i].start_msf[0]),
1237                                 bcd2bin(cd->toc[i].start_msf[1]),
1238                                 bcd2bin(cd->toc[i].start_msf[2]));
1239                 }
1240 #endif
1241         }
1242         bcopy(toc.lead_out_start_msf, cd->toc[cd->last_track+1].start_msf, 3);
1243 #ifdef SCD_DEBUG
1244         if (scd_debuglevel > 0) {
1245                 i = cd->last_track+1;
1246                 printf("[END: %2d %2d %2d]\n",
1247                         bcd2bin(cd->toc[i].start_msf[0]),
1248                         bcd2bin(cd->toc[i].start_msf[1]),
1249                         bcd2bin(cd->toc[i].start_msf[2]));
1250         }
1251 #endif
1252
1253         bzero(&cd->dlabel,sizeof(struct disklabel));
1254         /* filled with spaces first */
1255         strncpy(cd->dlabel.d_typename,"               ",
1256                 sizeof(cd->dlabel.d_typename));
1257         strncpy(cd->dlabel.d_typename, cd->name,
1258                 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
1259         strncpy(cd->dlabel.d_packname,"unknown        ",
1260                 sizeof(cd->dlabel.d_packname));
1261         cd->dlabel.d_secsize    = cd->blksize;
1262         cd->dlabel.d_nsectors   = 100;
1263         cd->dlabel.d_ntracks    = 1;
1264         cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
1265         cd->dlabel.d_secpercyl  = 100;
1266         cd->dlabel.d_secperunit = cd->disksize;
1267         cd->dlabel.d_rpm        = 300;
1268         cd->dlabel.d_interleave = 1;
1269         cd->dlabel.d_flags      = D_REMOVABLE;
1270         cd->dlabel.d_npartitions= 1;
1271         cd->dlabel.d_partitions[0].p_offset = 0;
1272         cd->dlabel.d_partitions[0].p_size = cd->disksize;
1273         cd->dlabel.d_partitions[0].p_fstype = 9;
1274         cd->dlabel.d_magic      = DISKMAGIC;
1275         cd->dlabel.d_magic2     = DISKMAGIC;
1276         cd->dlabel.d_checksum   = dkcksum(&cd->dlabel);
1277
1278         cd->flags |= SCDTOC;
1279
1280         return 0;
1281 }
1282
1283 static void
1284 init_drive(unsigned unit)
1285 {
1286         int rc;
1287
1288         rc = send_cmd(unit, CMD_SET_DRIVE_PARAM, 2,
1289                 0x05, 0x03 | ((scd_data[unit].double_speed) ? 0x04: 0));
1290         if (rc != 0)
1291                 printf("scd%d: Unable to set parameters. Errcode = 0x%x\n", unit, rc);
1292 }
1293
1294 /* Returns 0 or errno */
1295 static int
1296 get_result(u_int unit, int result_len, u_char *result)
1297 {
1298         unsigned int port = scd_data[unit].iobase;
1299         unsigned int res_reg = port + IREG_RESULT;
1300         int loop_index = 2; /* send_cmd() reads two bytes ... */
1301
1302         XDEBUG(1, ("scd%d: DEBUG: get_result: bytes=%d\n", unit, result_len));
1303
1304         while (result_len-- > 0) {
1305                 if (loop_index++ >= 10) {
1306                         loop_index = 1;
1307                         if (waitfor_status_bits(unit, SBIT_RESULT_READY, 0))
1308                                 return EIO;
1309                         write_control(port, CBIT_RESULT_READY_CLEAR);
1310                 }
1311                 if (result)
1312                         *result++ = inb(res_reg);
1313                 else
1314                         (void)inb(res_reg);
1315         }
1316         return 0;
1317 }
1318
1319 /* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */
1320 static int
1321 send_cmd(u_int unit, u_char cmd, u_int nargs, ...)
1322 {
1323         va_list ap;
1324         u_int port = scd_data[unit].iobase;
1325         u_int reg;
1326         u_char c;
1327         int rc;
1328         int i;
1329
1330         if (waitfor_status_bits(unit, 0, SBIT_BUSY)) {
1331                 printf("scd%d: drive busy\n", unit);
1332                 return -0x100;
1333         }
1334
1335         XDEBUG(1,("scd%d: DEBUG: send_cmd: cmd=0x%x nargs=%d", unit, cmd, nargs));
1336
1337         write_control(port, CBIT_RESULT_READY_CLEAR);
1338         write_control(port, CBIT_RPARAM_CLEAR);
1339
1340         for (i = 0; i < 100; i++)
1341                 if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
1342                         break;
1343         if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
1344                 XDEBUG(1, ("\nscd%d: wparam timeout\n", unit));
1345                 return -EIO;
1346         }
1347
1348         va_start(ap, nargs);
1349         reg = port + OREG_WPARAMS;
1350         for (i = 0; i < nargs; i++) {
1351                 c = (u_char)va_arg(ap, int);
1352                 outb(reg, c);
1353                 XDEBUG(1, (",{0x%x}", c));
1354         }
1355         va_end(ap);
1356         XDEBUG(1, ("\n"));
1357
1358         outb(port+OREG_COMMAND, cmd);
1359
1360         rc = waitfor_status_bits(unit, SBIT_RESULT_READY, SBIT_BUSY);
1361         if (rc)
1362                 return -0x100;
1363
1364         reg = port + IREG_RESULT;
1365         write_control(port, CBIT_RESULT_READY_CLEAR);
1366         switch ((rc = inb(reg)) & 0xf0) {
1367         case 0x20:
1368                 rc = inb(reg);
1369                 /* FALL TROUGH */
1370         case 0x50:
1371                 XDEBUG(1, ("scd%d: DEBUG: send_cmd: drive_error=0x%x\n", unit, rc));
1372                 return -rc;
1373         case 0x00:
1374         default:
1375                 rc = inb(reg);
1376                 XDEBUG(1, ("scd%d: DEBUG: send_cmd: result_len=%d\n", unit, rc));
1377                 return rc;
1378         }
1379 }
1380
1381 static void
1382 print_error(int unit, int errcode)
1383 {
1384         switch (errcode) {
1385         case -ERR_CD_NOT_LOADED:
1386                 printf("scd%d: door is open\n", unit);
1387                 break;
1388         case -ERR_NO_CD_INSIDE:
1389                 printf("scd%d: no cd inside\n", unit);
1390                 break;
1391         default:
1392                 if (errcode == -0x100 || errcode > 0)
1393                         printf("scd%d: device timeout\n", unit);
1394                 else
1395                         printf("scd%d: unexpected error 0x%x\n", unit, -errcode);
1396                 break;
1397         }
1398 }
1399
1400 /* Returns 0 or errno value */
1401 static int
1402 waitfor_status_bits(int unit, int bits_set, int bits_clear)
1403 {
1404         u_int port = scd_data[unit].iobase;
1405         u_int flags = scd_data[unit].flags;
1406         u_int reg = port + IREG_STATUS;
1407         u_int max_loop;
1408         u_char c = 0;
1409
1410         if (flags & SCDPROBING) {
1411                 max_loop = 0;
1412                 while (max_loop++ < 1000) {
1413                         c = inb(reg);
1414                         if (c == 0xff)
1415                                 return EIO;
1416                         if (c & SBIT_ATTENTION) {
1417                                 process_attention(unit);
1418                                 continue;
1419                         }
1420                         if ((c & bits_set) == bits_set &&
1421                             (c & bits_clear) == 0)
1422                         {
1423                                 break;
1424                         }
1425                         DELAY(10000);
1426                 }
1427         } else {
1428                 max_loop = 100;
1429                 while (max_loop-- > 0) {
1430                         c = inb(reg);
1431                         if (c & SBIT_ATTENTION) {
1432                                 process_attention(unit);
1433                                 continue;
1434                         }
1435                         if ((c & bits_set) == bits_set &&
1436                             (c & bits_clear) == 0)
1437                         {
1438                                 break;
1439                         }
1440                         tsleep(waitfor_status_bits, PZERO - 1, "waitfor", hz/10);
1441                 }
1442         }
1443         if ((c & bits_set) == bits_set &&
1444             (c & bits_clear) == 0)
1445         {
1446                 return 0;
1447         }
1448 #ifdef SCD_DEBUG
1449         if (scd_debuglevel > 0)
1450                 printf("scd%d: DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", unit, c, bits_set, bits_clear);
1451         else
1452 #endif
1453                 printf("scd%d: timeout.\n", unit);
1454         return EIO;
1455 }
1456
1457 /* these two routines for xcdplayer - "borrowed" from mcd.c */
1458 static int
1459 scd_toc_header (int unit, struct ioc_toc_header* th)
1460 {
1461         struct scd_data *cd = scd_data + unit;
1462         int rc;
1463
1464         if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1465                 print_error(unit, rc);
1466                 return EIO;
1467         }
1468
1469         th->starting_track = cd->first_track;
1470         th->ending_track = cd->last_track;
1471         th->len = 0; /* not used */
1472
1473         return 0;
1474 }
1475
1476 static int
1477 scd_toc_entrys (int unit, struct ioc_read_toc_entry *te)
1478 {
1479         struct scd_data *cd = scd_data + unit;
1480         struct cd_toc_entry toc_entry;
1481         int rc, i, len = te->data_len;
1482
1483         if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1484                 print_error(unit, rc);
1485                 return EIO;
1486         }
1487
1488         /* find the toc to copy*/
1489         i = te->starting_track;
1490         if (i == SCD_LASTPLUS1)
1491                 i = cd->last_track + 1;
1492
1493         /* verify starting track */
1494         if (i < cd->first_track || i > cd->last_track+1)
1495                 return EINVAL;
1496
1497         /* valid length ? */
1498         if (len < sizeof(struct cd_toc_entry)
1499             || (len % sizeof(struct cd_toc_entry)) != 0)
1500                 return EINVAL;
1501
1502         /* copy the toc data */
1503         toc_entry.control = cd->toc[i].ctl;
1504         toc_entry.addr_type = te->address_format;
1505         toc_entry.track = i;
1506         if (te->address_format == CD_MSF_FORMAT) {
1507                 toc_entry.addr.msf.unused = 0;
1508                 toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
1509                 toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
1510                 toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
1511         }
1512
1513         /* copy the data back */
1514         if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0)
1515                 return EFAULT;
1516
1517         return 0;
1518 }
1519
1520
1521 static int
1522 scd_toc_entry (int unit, struct ioc_read_toc_single_entry *te)
1523 {
1524         struct scd_data *cd = scd_data + unit;
1525         struct cd_toc_entry toc_entry;
1526         int rc, i;
1527
1528         if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1529                 print_error(unit, rc);
1530                 return EIO;
1531         }
1532
1533         /* find the toc to copy*/
1534         i = te->track;
1535         if (i == SCD_LASTPLUS1)
1536                 i = cd->last_track + 1;
1537
1538         /* verify starting track */
1539         if (i < cd->first_track || i > cd->last_track+1)
1540                 return EINVAL;
1541
1542         /* copy the toc data */
1543         toc_entry.control = cd->toc[i].ctl;
1544         toc_entry.addr_type = te->address_format;
1545         toc_entry.track = i;
1546         if (te->address_format == CD_MSF_FORMAT) {
1547                 toc_entry.addr.msf.unused = 0;
1548                 toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
1549                 toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
1550                 toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
1551         }
1552
1553         /* copy the data back */
1554         bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry));
1555
1556         return 0;
1557 }