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