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