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