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