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