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