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