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