Remove the priority part of the priority|flags argument to tsleep(). Only
[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.3 2003/07/19 21:14:34 dillon Exp $ */
46
47 /* Please send any comments to micke@dynas.se */
48
49 #define SCD_DEBUG       0
50
51 #include "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
60 #include <machine/clock.h>
61 #include <machine/stdarg.h>
62
63 #include <i386/isa/isa_device.h>
64 #include <i386/isa/scdreg.h>
65
66
67 #define scd_part(dev)   ((minor(dev)) & 7)
68 #define scd_unit(dev)   (((minor(dev)) & 0x38) >> 3)
69 #define scd_phys(dev)   (((minor(dev)) & 0x40) >> 6)
70 #define RAW_PART        2
71
72 /* flags */
73 #define SCDOPEN         0x0001  /* device opened */
74 #define SCDVALID        0x0002  /* parameters loaded */
75 #define SCDINIT         0x0004  /* device is init'd */
76 #define SCDPROBING      0x0020  /* probing */
77 #define SCDTOC          0x0100  /* already read toc */
78 #define SCDMBXBSY       0x0200  /* local mbx is busy */
79 #define SCDSPINNING     0x0400  /* drive is spun up */
80
81 #define SCD_S_BEGIN     0
82 #define SCD_S_BEGIN1    1
83 #define SCD_S_WAITSTAT  2
84 #define SCD_S_WAITFIFO  3
85 #define SCD_S_WAITSPIN  4
86 #define SCD_S_WAITREAD  5
87 #define SCD_S_WAITPARAM 6
88
89 #define RDELAY_WAIT     300
90 #define RDELAY_WAITREAD 300
91
92 #define SCDBLKSIZE      2048
93
94 #ifdef SCD_DEBUG
95    static int scd_debuglevel = SCD_DEBUG;
96 #  define XDEBUG(level, data) {if (scd_debuglevel >= level) printf data;}
97 #else
98 #  define XDEBUG(level, data)
99 #endif
100
101 struct scd_mbx {
102         short           unit;
103         short           port;
104         short           retry;
105         short           nblk;
106         int             sz;
107         u_long          skip;
108         struct buf      *bp;
109         int             p_offset;
110         short           count;
111 };
112
113 static struct scd_data {
114         int     iobase;
115         char    double_speed;
116         char    *name;
117         short   flags;
118         int     blksize;
119         u_long  disksize;
120         struct disklabel dlabel;
121         int     openflag;
122         struct {
123                 unsigned int  adr :4;
124                 unsigned int  ctl :4; /* xcdplayer needs this */
125                 unsigned char start_msf[3];
126         } toc[MAX_TRACKS];
127         short   first_track;
128         short   last_track;
129         struct  ioc_play_msf last_play;
130
131         short   audio_status;
132         struct buf_queue_head head;             /* head of buf queue */
133         struct scd_mbx mbx;
134 } scd_data[NSCD];
135
136 /* prototypes */
137 static  void    hsg2msf(int hsg, bcd_t *msf);
138 static  int     msf2hsg(bcd_t *msf);
139
140 static void process_attention(unsigned unit);
141 static __inline void write_control(unsigned port, unsigned data);
142 static int waitfor_status_bits(int unit, int bits_set, int bits_clear);
143 static int send_cmd(u_int unit, u_char cmd, u_int nargs, ...);
144 static void init_drive(unsigned unit);
145 static int spin_up(unsigned unit);
146 static int read_toc(unsigned unit);
147 static int get_result(u_int unit, int result_len, u_char *result);
148 static void print_error(int unit, int errcode);
149
150 static void scd_start(int unit);
151 static timeout_t scd_timeout;
152 static void scd_doread(int state, struct scd_mbx *mbxin);
153
154 static int scd_eject(int unit);
155 static int scd_stop(int unit);
156 static int scd_pause(int unit);
157 static int scd_resume(int unit);
158 static int scd_playtracks(int unit, struct ioc_play_track *pt);
159 static int scd_playmsf(int unit, struct ioc_play_msf *msf);
160 static int scd_play(int unit, struct ioc_play_msf *msf);
161 static int scd_subchan(int unit, struct ioc_read_subchannel *sc);
162 static int read_subcode(int unit, struct sony_subchannel_position_data *sc);
163
164 /* for xcdplayer */
165 static int scd_toc_header(int unit, struct ioc_toc_header *th);
166 static int scd_toc_entrys(int unit, struct ioc_read_toc_entry *te);
167 static int scd_toc_entry(int unit, struct ioc_read_toc_single_entry *te);
168 #define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */
169
170 static int      scd_probe(struct isa_device *dev);
171 static int      scd_attach(struct isa_device *dev);
172 struct  isa_driver      scddriver = { scd_probe, scd_attach, "scd" };
173
174 /* For canceling our timeout */
175 static struct callout_handle tohandle = CALLOUT_HANDLE_INITIALIZER(&tohanle);
176
177 static  d_open_t        scdopen;
178 static  d_close_t       scdclose;
179 static  d_ioctl_t       scdioctl;
180 static  d_strategy_t    scdstrategy;
181
182 #define CDEV_MAJOR 45
183 #define BDEV_MAJOR 16
184 static struct cdevsw scd_cdevsw = {
185         /* open */      scdopen,
186         /* close */     scdclose,
187         /* read */      physread,
188         /* write */     nowrite,
189         /* ioctl */     scdioctl,
190         /* poll */      nopoll,
191         /* mmap */      nommap,
192         /* strategy */  scdstrategy,
193         /* name */      "scd",
194         /* maj */       CDEV_MAJOR,
195         /* dump */      nodump,
196         /* psize */     nopsize,
197         /* flags */     D_DISK,
198         /* bmaj */      BDEV_MAJOR
199 };
200
201
202 static int
203 scd_attach(struct isa_device *dev)
204 {
205         int     unit = dev->id_unit;
206         struct scd_data *cd = scd_data + unit;
207
208         cd->iobase = dev->id_iobase;    /* Already set by probe, but ... */
209
210         /* name filled in probe */
211         printf("scd%d: <%s>\n", dev->id_unit, scd_data[dev->id_unit].name);
212
213         init_drive(dev->id_unit);
214
215         cd->flags = SCDINIT;
216         cd->audio_status = CD_AS_AUDIO_INVALID;
217         bufq_init(&cd->head);
218
219         make_dev(&scd_cdevsw, dkmakeminor(unit, 0, 0),
220             UID_ROOT, GID_OPERATOR, 0640, "rscd%da", unit);
221         make_dev(&scd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
222             UID_ROOT, GID_OPERATOR, 0640, "rscd%dc", unit);
223         make_dev(&scd_cdevsw, dkmakeminor(unit, 0, 0),
224             UID_ROOT, GID_OPERATOR, 0640, "scd%da", unit);
225         make_dev(&scd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
226             UID_ROOT, GID_OPERATOR, 0640, "scd%dc", unit);
227         return 1;
228 }
229
230 static  int
231 scdopen(dev_t dev, int flags, int fmt, struct proc *p)
232 {
233         int unit,part,phys;
234         int rc;
235         struct scd_data *cd;
236
237         unit = scd_unit(dev);
238         if (unit >= NSCD)
239                 return ENXIO;
240
241         cd = scd_data + unit;
242         part = scd_part(dev);
243         phys = scd_phys(dev);
244
245         /* not initialized*/
246         if (!(cd->flags & SCDINIT))
247                 return ENXIO;
248
249         /* invalidated in the meantime? mark all open part's invalid */
250         if (cd->openflag)
251                 return ENXIO;
252
253         XDEBUG(1,("scd%d: DEBUG: status = 0x%x\n", unit, inb(cd->iobase+IREG_STATUS)));
254
255         if ((rc = spin_up(unit)) != 0) {
256                 print_error(unit, rc);
257                 return EIO;
258         }
259         if (!(cd->flags & SCDTOC)) {
260                 int loop_count = 3;
261
262                 while (loop_count-- > 0 && (rc = read_toc(unit)) != 0) {
263                         if (rc == ERR_NOT_SPINNING) {
264                                 rc = spin_up(unit);
265                                 if (rc) {
266                                         print_error(unit, rc);\
267                                         return EIO;
268                                 }
269                                 continue;
270                         }
271                         printf("scd%d: TOC read error 0x%x\n", unit, rc);
272                         return EIO;
273                 }
274         }
275
276         dev->si_bsize_phys = cd->blksize;
277
278         cd->openflag = 1;
279         cd->flags |= SCDVALID;
280
281         return 0;
282 }
283
284 static  int
285 scdclose(dev_t dev, int flags, int fmt, struct proc *p)
286 {
287         int unit,part,phys;
288         struct scd_data *cd;
289
290         unit = scd_unit(dev);
291         if (unit >= NSCD)
292                 return ENXIO;
293
294         cd = scd_data + unit;
295         part = scd_part(dev);
296         phys = scd_phys(dev);
297
298         if (!(cd->flags & SCDINIT) || !cd->openflag)
299                 return ENXIO;
300
301         if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) {
302                 (void)send_cmd(unit, CMD_SPIN_DOWN, 0);
303                 cd->flags &= ~SCDSPINNING;
304         }
305
306
307         /* close channel */
308         cd->openflag = 0;
309
310         return 0;
311 }
312
313 static  void
314 scdstrategy(struct buf *bp)
315 {
316         struct scd_data *cd;
317         int s;
318         int unit = scd_unit(bp->b_dev);
319
320         cd = scd_data + unit;
321
322         XDEBUG(2, ("scd%d: DEBUG: strategy: block=%ld, bcount=%ld\n",
323                 unit, (long)bp->b_blkno, bp->b_bcount));
324
325         if (unit >= NSCD || bp->b_blkno < 0 || (bp->b_bcount % SCDBLKSIZE)) {
326                 printf("scd%d: strategy failure: blkno = %ld, bcount = %ld\n",
327                         unit, (long)bp->b_blkno, bp->b_bcount);
328                 bp->b_error = EINVAL;
329                 bp->b_flags |= B_ERROR;
330                 goto bad;
331         }
332
333         /* if device invalidated (e.g. media change, door open), error */
334         if (!(cd->flags & SCDVALID)) {
335                 printf("scd%d: media changed\n", unit);
336                 bp->b_error = EIO;
337                 goto bad;
338         }
339
340         /* read only */
341         if (!(bp->b_flags & B_READ)) {
342                 bp->b_error = EROFS;
343                 goto bad;
344         }
345
346         /* no data to read */
347         if (bp->b_bcount == 0)
348                 goto done;
349
350         if (!(cd->flags & SCDTOC)) {
351                 bp->b_error = EIO;
352                 goto bad;
353         }
354         /* adjust transfer if necessary */
355         if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0)
356                 goto done;
357
358         bp->b_pblkno = bp->b_blkno;
359         bp->b_resid = 0;
360
361         /* queue it */
362         s = splbio();
363         bufqdisksort(&cd->head, bp);
364         splx(s);
365
366         /* now check whether we can perform processing */
367         scd_start(unit);
368         return;
369
370 bad:
371         bp->b_flags |= B_ERROR;
372 done:
373         bp->b_resid = bp->b_bcount;
374         biodone(bp);
375         return;
376 }
377
378 static void
379 scd_start(int unit)
380 {
381         struct scd_data *cd = scd_data + unit;
382         struct buf *bp;
383         struct partition *p;
384         int s = splbio();
385
386         if (cd->flags & SCDMBXBSY) {
387                 splx(s);
388                 return;
389         }
390
391         bp = bufq_first(&cd->head);
392         if (bp != 0) {
393                 /* block found to process, dequeue */
394                 bufq_remove(&cd->head, bp);
395                 cd->flags |= SCDMBXBSY;
396                 splx(s);
397         } else {
398                 /* nothing to do */
399                 splx(s);
400                 return;
401         }
402
403         p = cd->dlabel.d_partitions + scd_part(bp->b_dev);
404
405         cd->mbx.unit = unit;
406         cd->mbx.port = cd->iobase;
407         cd->mbx.retry = 3;
408         cd->mbx.bp = bp;
409         cd->mbx.p_offset = p->p_offset;
410         splx(s);
411
412         scd_doread(SCD_S_BEGIN,&(cd->mbx));
413         return;
414 }
415
416 static  int
417 scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
418 {
419         struct scd_data *cd;
420         int unit,part;
421
422         unit = scd_unit(dev);
423         part = scd_part(dev);
424         cd = scd_data + unit;
425
426         XDEBUG(1, ("scd%d: ioctl: cmd=0x%lx\n", unit, cmd));
427
428         if (!(cd->flags & SCDVALID))
429                 return EIO;
430
431         switch (cmd) {
432         case DIOCGDINFO:
433                 *(struct disklabel *)addr = cd->dlabel;
434                 return 0;
435         case DIOCGPART:
436                 ((struct partinfo *)addr)->disklab = &cd->dlabel;
437                 ((struct partinfo *)addr)->part =
438                         &cd->dlabel.d_partitions[0];
439                 return 0;
440         case CDIOCPLAYTRACKS:
441                 return scd_playtracks(unit, (struct ioc_play_track *) addr);
442         case CDIOCPLAYBLOCKS:
443                 return EINVAL;
444         case CDIOCPLAYMSF:
445                 return scd_playmsf(unit, (struct ioc_play_msf *) addr);
446         case CDIOCREADSUBCHANNEL:
447                 return scd_subchan(unit, (struct ioc_read_subchannel *) addr);
448         case CDIOREADTOCHEADER:
449                 return scd_toc_header (unit, (struct ioc_toc_header *) addr);
450         case CDIOREADTOCENTRYS:
451                 return scd_toc_entrys (unit, (struct ioc_read_toc_entry*) addr);
452         case CDIOREADTOCENTRY:
453                 return scd_toc_entry (unit, (struct ioc_read_toc_single_entry*) addr);
454         case CDIOCSETPATCH:
455         case CDIOCGETVOL:
456         case CDIOCSETVOL:
457         case CDIOCSETMONO:
458         case CDIOCSETSTERIO:
459         case CDIOCSETMUTE:
460         case CDIOCSETLEFT:
461         case CDIOCSETRIGHT:
462                 return EINVAL;
463         case CDIOCRESUME:
464                 return scd_resume(unit);
465         case CDIOCPAUSE:
466                 return scd_pause(unit);
467         case CDIOCSTART:
468                 return EINVAL;
469         case CDIOCSTOP:
470                 return scd_stop(unit);
471         case CDIOCEJECT:
472                 return scd_eject(unit);
473         case CDIOCALLOW:
474                 return 0;
475         case CDIOCSETDEBUG:
476 #ifdef SCD_DEBUG
477                 scd_debuglevel++;
478 #endif
479                 return 0;
480         case CDIOCCLRDEBUG:
481 #ifdef SCD_DEBUG
482                 scd_debuglevel = 0;
483
484 #endif
485                 return 0;
486         default:
487                 printf("scd%d: unsupported ioctl (cmd=0x%lx)\n", unit, cmd);
488                 return ENOTTY;
489         }
490 }
491
492 /***************************************************************
493  * lower level of driver starts here
494  **************************************************************/
495
496 static int
497 scd_playtracks(int unit, struct ioc_play_track *pt)
498 {
499         struct scd_data *cd = scd_data + unit;
500         struct ioc_play_msf msf;
501         int a = pt->start_track;
502         int z = pt->end_track;
503         int rc;
504
505         if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
506                 if (rc == -ERR_NOT_SPINNING) {
507                         if (spin_up(unit) != 0)
508                                 return EIO;
509                         rc = read_toc(unit);
510                 }
511                 if (rc != 0) {
512                         print_error(unit, rc);
513                         return EIO;
514                 }
515         }
516
517         XDEBUG(1, ("scd%d: playtracks from %d:%d to %d:%d\n", unit,
518                 a, pt->start_index, z, pt->end_index));
519
520         if (   a < cd->first_track
521             || a > cd->last_track
522             || a > z
523             || z > cd->last_track)
524                 return EINVAL;
525
526         bcopy(cd->toc[a].start_msf, &msf.start_m, 3);
527         hsg2msf(msf2hsg(cd->toc[z+1].start_msf)-1, &msf.end_m);
528
529         return scd_play(unit, &msf);
530 }
531
532 /* The start/end msf is expected to be in bin format */
533 static int
534 scd_playmsf(int unit, struct ioc_play_msf *msfin)
535 {
536         struct ioc_play_msf msf;
537
538         msf.start_m = bin2bcd(msfin->start_m);
539         msf.start_s = bin2bcd(msfin->start_s);
540         msf.start_f = bin2bcd(msfin->start_f);
541         msf.end_m = bin2bcd(msfin->end_m);
542         msf.end_s = bin2bcd(msfin->end_s);
543         msf.end_f = bin2bcd(msfin->end_f);
544
545         return scd_play(unit, &msf);
546 }
547
548 /* The start/end msf is expected to be in bcd format */
549 static int
550 scd_play(int unit, struct ioc_play_msf *msf)
551 {
552         struct scd_data *cd = scd_data + unit;
553         int i, rc;
554
555         XDEBUG(1, ("scd%d: playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", unit,
556                 msf->start_m, msf->start_s, msf->start_f,
557                 msf->end_m, msf->end_s, msf->end_f));
558
559         for (i = 0; i < 2; i++) {
560                 rc = send_cmd(unit, CMD_PLAY_AUDIO, 7,
561                         0x03,
562                         msf->start_m, msf->start_s, msf->start_f,
563                         msf->end_m, msf->end_s, msf->end_f);
564                 if (rc == -ERR_NOT_SPINNING) {
565                         cd->flags &= ~SCDSPINNING;
566                         if (spin_up(unit) != 0)
567                                 return EIO;
568                 } else if (rc < 0) {
569                         print_error(unit, rc);
570                         return EIO;
571                 } else {
572                         break;
573                 }
574         }
575         cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
576         bcopy((char *)msf, (char *)&cd->last_play, sizeof(struct ioc_play_msf));
577         return 0;
578 }
579
580 static int
581 scd_stop(int unit)
582 {
583         struct scd_data *cd = scd_data + unit;
584
585         (void)send_cmd(unit, CMD_STOP_AUDIO, 0);
586         cd->audio_status = CD_AS_PLAY_COMPLETED;
587         return 0;
588 }
589
590 static int
591 scd_pause(int unit)
592 {
593         struct scd_data *cd = scd_data + unit;
594         struct sony_subchannel_position_data subpos;
595
596         if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS)
597                 return EINVAL;
598
599         if (read_subcode(unit, &subpos) != 0)
600                 return EIO;
601
602         if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0)
603                 return EIO;
604
605         cd->last_play.start_m = subpos.abs_msf[0];
606         cd->last_play.start_s = subpos.abs_msf[1];
607         cd->last_play.start_f = subpos.abs_msf[2];
608         cd->audio_status = CD_AS_PLAY_PAUSED;
609
610         XDEBUG(1, ("scd%d: pause @ %02x:%02x:%02x\n", unit,
611                 cd->last_play.start_m,
612                 cd->last_play.start_s,
613                 cd->last_play.start_f));
614
615         return 0;
616 }
617
618 static int
619 scd_resume(int unit)
620 {
621         if (scd_data[unit].audio_status != CD_AS_PLAY_PAUSED)
622                 return EINVAL;
623         return scd_play(unit, &scd_data[unit].last_play);
624 }
625
626 static int
627 scd_eject(int unit)
628 {
629         struct scd_data *cd = scd_data + unit;
630
631         cd->audio_status = CD_AS_AUDIO_INVALID;
632         cd->flags &= ~(SCDSPINNING|SCDTOC);
633
634         if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0 ||
635             send_cmd(unit, CMD_SPIN_DOWN, 0) != 0 ||
636             send_cmd(unit, CMD_EJECT, 0) != 0)
637         {
638                 return EIO;
639         }
640         return 0;
641 }
642
643 static int
644 scd_subchan(int unit, struct ioc_read_subchannel *sc)
645 {
646         struct scd_data *cd = scd_data + unit;
647         struct sony_subchannel_position_data q;
648         struct cd_sub_channel_info data;
649
650         XDEBUG(1, ("scd%d: subchan af=%d, df=%d\n", unit,
651                 sc->address_format,
652                 sc->data_format));
653
654         if (sc->address_format != CD_MSF_FORMAT)
655                 return EINVAL;
656
657         if (sc->data_format != CD_CURRENT_POSITION)
658                 return EINVAL;
659
660         if (read_subcode(unit, &q) != 0)
661                 return EIO;
662
663         data.header.audio_status = cd->audio_status;
664         data.what.position.data_format = CD_MSF_FORMAT;
665         data.what.position.track_number = bcd2bin(q.track_number);
666         data.what.position.reladdr.msf.unused = 0;
667         data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]);
668         data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]);
669         data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]);
670         data.what.position.absaddr.msf.unused = 0;
671         data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]);
672         data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]);
673         data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]);
674
675         if (copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len))!=0)
676                 return EFAULT;
677         return 0;
678 }
679
680 static __inline void
681 write_control(unsigned port, unsigned data)
682 {
683         outb(port + OREG_CONTROL, data);
684 }
685
686 static int
687 scd_probe(struct isa_device *dev)
688 {
689         struct sony_drive_configuration drive_config;
690         int unit = dev->id_unit;
691         int rc;
692         static char namebuf[8+16+8+3];
693         char *s = namebuf;
694         int loop_count = 0;
695         static int once;
696
697         if (!once++)
698                 cdevsw_add(&scd_cdevsw);
699
700         scd_data[unit].flags = SCDPROBING;
701         scd_data[unit].iobase = dev->id_iobase;
702
703         bzero(&drive_config, sizeof(drive_config));
704
705 again:
706         /* Reset drive */
707         write_control(dev->id_iobase, CBIT_RESET_DRIVE);
708
709         /* Calm down */
710         DELAY(300000);
711
712         /* Only the ATTENTION bit may be set */
713         if ((inb(dev->id_iobase+IREG_STATUS) & ~1) != 0) {
714                 XDEBUG(1, ("scd: too many bits set. probe failed.\n"));
715                 return 0;
716         }
717         rc = send_cmd(unit, CMD_GET_DRIVE_CONFIG, 0);
718         if (rc != sizeof(drive_config)) {
719                 /* Sometimes if the drive is playing audio I get */
720                 /* the bad result 82. Fix by repeating the reset */
721                 if (rc > 0 && loop_count++ == 0)
722                         goto again;
723                 return 0;
724         }
725         if (get_result(unit, rc, (u_char *)&drive_config) != 0)
726                 return 0;
727
728         bcopy(drive_config.vendor, namebuf, 8);
729         s = namebuf+8;
730         while (*(s-1) == ' ')   /* Strip trailing spaces */
731                 s--;
732         *s++ = ' ';
733         bcopy(drive_config.product, s, 16);
734         s += 16;
735         while (*(s-1) == ' ')
736                 s--;
737         *s++ = ' ';
738         bcopy(drive_config.revision, s, 8);
739         s += 8;
740         while (*(s-1) == ' ')
741                 s--;
742         *s = 0;
743
744         scd_data[unit].name = namebuf;
745
746         if (drive_config.config & 0x10)
747                 scd_data[unit].double_speed = 1;
748         else
749                 scd_data[unit].double_speed = 0;
750
751         return 4;
752 }
753
754 static int
755 read_subcode(int unit, struct sony_subchannel_position_data *sc)
756 {
757         int rc;
758
759         rc = send_cmd(unit, CMD_GET_SUBCHANNEL_DATA, 0);
760         if (rc < 0 || rc < sizeof(*sc))
761                 return EIO;
762         if (get_result(unit, rc, (u_char *)sc) != 0)
763                 return EIO;
764         return 0;
765 }
766
767 /* State machine copied from mcd.c */
768
769 /* This (and the code in mcd.c) will not work with more than one drive */
770 /* because there is only one mbxsave below. Should fix that some day. */
771 /* (mbxsave & state should probably be included in the scd_data struct and */
772 /*  the unit number used as first argument to scd_doread().) /Micke */
773
774 /* state machine to process read requests
775  * initialize with SCD_S_BEGIN: reset state machine
776  * SCD_S_WAITSTAT:  wait for ready (!busy)
777  * SCD_S_WAITSPIN:  wait for drive to spin up (if not spinning)
778  * SCD_S_WAITFIFO:  wait for param fifo to get ready, them exec. command.
779  * SCD_S_WAITREAD:  wait for data ready, read data
780  * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read.
781  */
782
783 static struct scd_mbx *mbxsave;
784
785 static void
786 scd_timeout(void *arg)
787 {
788         scd_doread((int)arg, mbxsave);
789 }
790
791 static void
792 scd_doread(int state, struct scd_mbx *mbxin)
793 {
794         struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? mbxsave : mbxin;
795         int     unit = mbx->unit;
796         int     port = mbx->port;
797         struct  buf *bp = mbx->bp;
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                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITSTAT, tohandle);
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                         tohandle = timeout(scd_timeout,
826                                            (caddr_t)SCD_S_WAITSTAT,hz/100); /* XXX */
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  = (bp->b_blkno / (mbx->sz/DEV_BSIZE))
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                 tohandle = timeout(scd_timeout,
865                                    (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */
866                 return;
867
868         case SCD_S_WAITSPIN:
869                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITSPIN, tohandle);
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                         tohandle = timeout(scd_timeout,
876                                            (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */
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                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITFIFO, tohandle);
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                         tohandle = timeout(scd_timeout,
903                                            (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */
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                         tohandle = timeout(scd_timeout,
916                                            (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */
917                         return;
918                 }
919
920                 reg = port + OREG_WPARAMS;
921                 /* send the read command */
922                 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                 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                 tohandle = timeout(scd_timeout,
940                                    (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */
941                 return;
942
943         case SCD_S_WAITREAD:
944                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITREAD, tohandle);
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                         tohandle = timeout(scd_timeout,
956                                            (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */
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                 untimeout(scd_timeout,(caddr_t)SCD_S_WAITPARAM, tohandle);
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                         tohandle = timeout(scd_timeout,
985                                            (caddr_t)SCD_S_WAITPARAM,hz/100); /* XXX */
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(bp);
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(bp);
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         struct sony_tracklist *tl = &toc->tracks[0];
1164
1165         if (tl->track != 0xb0)
1166                 return tl;
1167         (char *)tl += 9;
1168         if (tl->track != 0xb1)
1169                 return tl;
1170         (char *)tl += 9;
1171         if (tl->track != 0xb2)
1172                 return tl;
1173         (char *)tl += 9;
1174         if (tl->track != 0xb3)
1175                 return tl;
1176         (char *)tl += 9;
1177         if (tl->track != 0xb4)
1178                 return tl;
1179         (char *)tl += 9;
1180         if (tl->track != 0xc0)
1181                 return tl;
1182         (char *)tl += 9;
1183         return tl;
1184 }
1185
1186 static int
1187 read_toc(unsigned unit)
1188 {
1189         struct scd_data *cd;
1190         unsigned part = 0;      /* For now ... */
1191         struct sony_toc toc;
1192         struct sony_tracklist *tl;
1193         int rc, i, j;
1194         u_long first, last;
1195
1196         cd = scd_data + unit;
1197
1198         rc = send_cmd(unit, CMD_GET_TOC, 1, part+1);
1199         if (rc < 0)
1200                 return rc;
1201         if (rc > sizeof(toc)) {
1202                 printf("scd%d: program error: toc too large (%d)\n", unit, rc);
1203                 return EIO;
1204         }
1205         if (get_result(unit, rc, (u_char *)&toc) != 0)
1206                 return EIO;
1207
1208         XDEBUG(1, ("scd%d: toc read. len = %d, sizeof(toc) = %d\n", unit, rc, sizeof(toc)));
1209
1210         tl = get_tl(&toc, rc);
1211         first = msf2hsg(tl->start_msf);
1212         last = msf2hsg(toc.lead_out_start_msf);
1213         cd->blksize = SCDBLKSIZE;
1214         cd->disksize = last*cd->blksize/DEV_BSIZE;
1215
1216         XDEBUG(1, ("scd%d: firstsector = %ld, lastsector = %ld", unit,
1217                         first, last));
1218
1219         cd->first_track = bcd2bin(toc.first_track);
1220         cd->last_track = bcd2bin(toc.last_track);
1221         if (cd->last_track > (MAX_TRACKS-2))
1222                 cd->last_track = MAX_TRACKS-2;
1223         for (j = 0, i = cd->first_track; i <= cd->last_track; i++, j++) {
1224                 cd->toc[i].adr = tl[j].adr;
1225                 cd->toc[i].ctl = tl[j].ctl; /* for xcdplayer */
1226                 bcopy(tl[j].start_msf, cd->toc[i].start_msf, 3);
1227 #ifdef SCD_DEBUG
1228                 if (scd_debuglevel > 0) {
1229                         if ((j % 3) == 0)
1230                                 printf("\nscd%d: tracks ", unit);
1231                         printf("[%03d: %2d %2d %2d]  ", i,
1232                                 bcd2bin(cd->toc[i].start_msf[0]),
1233                                 bcd2bin(cd->toc[i].start_msf[1]),
1234                                 bcd2bin(cd->toc[i].start_msf[2]));
1235                 }
1236 #endif
1237         }
1238         bcopy(toc.lead_out_start_msf, cd->toc[cd->last_track+1].start_msf, 3);
1239 #ifdef SCD_DEBUG
1240         if (scd_debuglevel > 0) {
1241                 i = cd->last_track+1;
1242                 printf("[END: %2d %2d %2d]\n",
1243                         bcd2bin(cd->toc[i].start_msf[0]),
1244                         bcd2bin(cd->toc[i].start_msf[1]),
1245                         bcd2bin(cd->toc[i].start_msf[2]));
1246         }
1247 #endif
1248
1249         bzero(&cd->dlabel,sizeof(struct disklabel));
1250         /* filled with spaces first */
1251         strncpy(cd->dlabel.d_typename,"               ",
1252                 sizeof(cd->dlabel.d_typename));
1253         strncpy(cd->dlabel.d_typename, cd->name,
1254                 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
1255         strncpy(cd->dlabel.d_packname,"unknown        ",
1256                 sizeof(cd->dlabel.d_packname));
1257         cd->dlabel.d_secsize    = cd->blksize;
1258         cd->dlabel.d_nsectors   = 100;
1259         cd->dlabel.d_ntracks    = 1;
1260         cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
1261         cd->dlabel.d_secpercyl  = 100;
1262         cd->dlabel.d_secperunit = cd->disksize;
1263         cd->dlabel.d_rpm        = 300;
1264         cd->dlabel.d_interleave = 1;
1265         cd->dlabel.d_flags      = D_REMOVABLE;
1266         cd->dlabel.d_npartitions= 1;
1267         cd->dlabel.d_partitions[0].p_offset = 0;
1268         cd->dlabel.d_partitions[0].p_size = cd->disksize;
1269         cd->dlabel.d_partitions[0].p_fstype = 9;
1270         cd->dlabel.d_magic      = DISKMAGIC;
1271         cd->dlabel.d_magic2     = DISKMAGIC;
1272         cd->dlabel.d_checksum   = dkcksum(&cd->dlabel);
1273
1274         cd->flags |= SCDTOC;
1275
1276         return 0;
1277 }
1278
1279 static void
1280 init_drive(unsigned unit)
1281 {
1282         int rc;
1283
1284         rc = send_cmd(unit, CMD_SET_DRIVE_PARAM, 2,
1285                 0x05, 0x03 | ((scd_data[unit].double_speed) ? 0x04: 0));
1286         if (rc != 0)
1287                 printf("scd%d: Unable to set parameters. Errcode = 0x%x\n", unit, rc);
1288 }
1289
1290 /* Returns 0 or errno */
1291 static int
1292 get_result(u_int unit, int result_len, u_char *result)
1293 {
1294         unsigned int port = scd_data[unit].iobase;
1295         unsigned int res_reg = port + IREG_RESULT;
1296         int loop_index = 2; /* send_cmd() reads two bytes ... */
1297
1298         XDEBUG(1, ("scd%d: DEBUG: get_result: bytes=%d\n", unit, result_len));
1299
1300         while (result_len-- > 0) {
1301                 if (loop_index++ >= 10) {
1302                         loop_index = 1;
1303                         if (waitfor_status_bits(unit, SBIT_RESULT_READY, 0))
1304                                 return EIO;
1305                         write_control(port, CBIT_RESULT_READY_CLEAR);
1306                 }
1307                 if (result)
1308                         *result++ = inb(res_reg);
1309                 else
1310                         (void)inb(res_reg);
1311         }
1312         return 0;
1313 }
1314
1315 /* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */
1316 static int
1317 send_cmd(u_int unit, u_char cmd, u_int nargs, ...)
1318 {
1319         va_list ap;
1320         u_int port = scd_data[unit].iobase;
1321         u_int reg;
1322         u_char c;
1323         int rc;
1324         int i;
1325
1326         if (waitfor_status_bits(unit, 0, SBIT_BUSY)) {
1327                 printf("scd%d: drive busy\n", unit);
1328                 return -0x100;
1329         }
1330
1331         XDEBUG(1,("scd%d: DEBUG: send_cmd: cmd=0x%x nargs=%d", unit, cmd, nargs));
1332
1333         write_control(port, CBIT_RESULT_READY_CLEAR);
1334         write_control(port, CBIT_RPARAM_CLEAR);
1335
1336         for (i = 0; i < 100; i++)
1337                 if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
1338                         break;
1339         if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
1340                 XDEBUG(1, ("\nscd%d: wparam timeout\n", unit));
1341                 return -EIO;
1342         }
1343
1344         va_start(ap, nargs);
1345         reg = port + OREG_WPARAMS;
1346         for (i = 0; i < nargs; i++) {
1347                 c = (u_char)va_arg(ap, int);
1348                 outb(reg, c);
1349                 XDEBUG(1, (",{0x%x}", c));
1350         }
1351         va_end(ap);
1352         XDEBUG(1, ("\n"));
1353
1354         outb(port+OREG_COMMAND, cmd);
1355
1356         rc = waitfor_status_bits(unit, SBIT_RESULT_READY, SBIT_BUSY);
1357         if (rc)
1358                 return -0x100;
1359
1360         reg = port + IREG_RESULT;
1361         write_control(port, CBIT_RESULT_READY_CLEAR);
1362         switch ((rc = inb(reg)) & 0xf0) {
1363         case 0x20:
1364                 rc = inb(reg);
1365                 /* FALL TROUGH */
1366         case 0x50:
1367                 XDEBUG(1, ("scd%d: DEBUG: send_cmd: drive_error=0x%x\n", unit, rc));
1368                 return -rc;
1369         case 0x00:
1370         default:
1371                 rc = inb(reg);
1372                 XDEBUG(1, ("scd%d: DEBUG: send_cmd: result_len=%d\n", unit, rc));
1373                 return rc;
1374         }
1375 }
1376
1377 static void
1378 print_error(int unit, int errcode)
1379 {
1380         switch (errcode) {
1381         case -ERR_CD_NOT_LOADED:
1382                 printf("scd%d: door is open\n", unit);
1383                 break;
1384         case -ERR_NO_CD_INSIDE:
1385                 printf("scd%d: no cd inside\n", unit);
1386                 break;
1387         default:
1388                 if (errcode == -0x100 || errcode > 0)
1389                         printf("scd%d: device timeout\n", unit);
1390                 else
1391                         printf("scd%d: unexpected error 0x%x\n", unit, -errcode);
1392                 break;
1393         }
1394 }
1395
1396 /* Returns 0 or errno value */
1397 static int
1398 waitfor_status_bits(int unit, int bits_set, int bits_clear)
1399 {
1400         u_int port = scd_data[unit].iobase;
1401         u_int flags = scd_data[unit].flags;
1402         u_int reg = port + IREG_STATUS;
1403         u_int max_loop;
1404         u_char c = 0;
1405
1406         if (flags & SCDPROBING) {
1407                 max_loop = 0;
1408                 while (max_loop++ < 1000) {
1409                         c = inb(reg);
1410                         if (c == 0xff)
1411                                 return EIO;
1412                         if (c & SBIT_ATTENTION) {
1413                                 process_attention(unit);
1414                                 continue;
1415                         }
1416                         if ((c & bits_set) == bits_set &&
1417                             (c & bits_clear) == 0)
1418                         {
1419                                 break;
1420                         }
1421                         DELAY(10000);
1422                 }
1423         } else {
1424                 max_loop = 100;
1425                 while (max_loop-- > 0) {
1426                         c = inb(reg);
1427                         if (c & SBIT_ATTENTION) {
1428                                 process_attention(unit);
1429                                 continue;
1430                         }
1431                         if ((c & bits_set) == bits_set &&
1432                             (c & bits_clear) == 0)
1433                         {
1434                                 break;
1435                         }
1436                         tsleep(waitfor_status_bits, 0, "waitfor", hz/10);
1437                 }
1438         }
1439         if ((c & bits_set) == bits_set &&
1440             (c & bits_clear) == 0)
1441         {
1442                 return 0;
1443         }
1444 #ifdef SCD_DEBUG
1445         if (scd_debuglevel > 0)
1446                 printf("scd%d: DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", unit, c, bits_set, bits_clear);
1447         else
1448 #endif
1449                 printf("scd%d: timeout.\n", unit);
1450         return EIO;
1451 }
1452
1453 /* these two routines for xcdplayer - "borrowed" from mcd.c */
1454 static int
1455 scd_toc_header (int unit, struct ioc_toc_header* th)
1456 {
1457         struct scd_data *cd = scd_data + unit;
1458         int rc;
1459
1460         if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1461                 print_error(unit, rc);
1462                 return EIO;
1463         }
1464
1465         th->starting_track = cd->first_track;
1466         th->ending_track = cd->last_track;
1467         th->len = 0; /* not used */
1468
1469         return 0;
1470 }
1471
1472 static int
1473 scd_toc_entrys (int unit, struct ioc_read_toc_entry *te)
1474 {
1475         struct scd_data *cd = scd_data + unit;
1476         struct cd_toc_entry toc_entry;
1477         int rc, i, len = te->data_len;
1478
1479         if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1480                 print_error(unit, rc);
1481                 return EIO;
1482         }
1483
1484         /* find the toc to copy*/
1485         i = te->starting_track;
1486         if (i == SCD_LASTPLUS1)
1487                 i = cd->last_track + 1;
1488
1489         /* verify starting track */
1490         if (i < cd->first_track || i > cd->last_track+1)
1491                 return EINVAL;
1492
1493         /* valid length ? */
1494         if (len < sizeof(struct cd_toc_entry)
1495             || (len % sizeof(struct cd_toc_entry)) != 0)
1496                 return EINVAL;
1497
1498         /* copy the toc data */
1499         toc_entry.control = cd->toc[i].ctl;
1500         toc_entry.addr_type = te->address_format;
1501         toc_entry.track = i;
1502         if (te->address_format == CD_MSF_FORMAT) {
1503                 toc_entry.addr.msf.unused = 0;
1504                 toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
1505                 toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
1506                 toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
1507         }
1508
1509         /* copy the data back */
1510         if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0)
1511                 return EFAULT;
1512
1513         return 0;
1514 }
1515
1516
1517 static int
1518 scd_toc_entry (int unit, struct ioc_read_toc_single_entry *te)
1519 {
1520         struct scd_data *cd = scd_data + unit;
1521         struct cd_toc_entry toc_entry;
1522         int rc, i;
1523
1524         if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1525                 print_error(unit, rc);
1526                 return EIO;
1527         }
1528
1529         /* find the toc to copy*/
1530         i = te->track;
1531         if (i == SCD_LASTPLUS1)
1532                 i = cd->last_track + 1;
1533
1534         /* verify starting track */
1535         if (i < cd->first_track || i > cd->last_track+1)
1536                 return EINVAL;
1537
1538         /* copy the toc data */
1539         toc_entry.control = cd->toc[i].ctl;
1540         toc_entry.addr_type = te->address_format;
1541         toc_entry.track = i;
1542         if (te->address_format == CD_MSF_FORMAT) {
1543                 toc_entry.addr.msf.unused = 0;
1544                 toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
1545                 toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
1546                 toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
1547         }
1548
1549         /* copy the data back */
1550         bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry));
1551
1552         return 0;
1553 }