Deleted Added
full compact
scd.c (112946) scd.c (119419)
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
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 $ */
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/sys/dev/scd/scd.c 119419 2003-08-24 18:03:45Z obrien $");
44
45
46
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}
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}