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