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