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