48 */ 49static const char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore"; 50 51#include <sys/param.h> 52#include <sys/systm.h> 53#include <sys/kernel.h> 54#include <sys/conf.h> 55#include <sys/fcntl.h> 56#include <sys/bio.h> 57#include <sys/cdio.h> 58#include <sys/disklabel.h> 59#include <sys/bus.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/mcd/mcdreg.h> 69#include <dev/mcd/mcdvar.h> 70 71#define MCD_TRACE(format, args...) \ 72{ \ 73 if (sc->debug) { \ 74 device_printf(sc->dev, "status=0x%02x: ", \ 75 sc->data.status); \ 76 printf(format, ## args); \ 77 } \ 78} 79 80#define mcd_part(dev) ((minor(dev)) & 7) 81#define mcd_unit(dev) (((minor(dev)) & 0x38) >> 3) 82#define mcd_phys(dev) (((minor(dev)) & 0x40) >> 6) 83#define RAW_PART 2 84 85/* flags */ 86#define MCDVALID 0x0001 /* parameters loaded */ 87#define MCDINIT 0x0002 /* device is init'd */ 88#define MCDNEWMODEL 0x0004 /* device is new model */ 89#define MCDLABEL 0x0008 /* label is read */ 90#define MCDPROBING 0x0010 /* probing */ 91#define MCDREADRAW 0x0020 /* read raw mode (2352 bytes) */ 92#define MCDVOLINFO 0x0040 /* already read volinfo */ 93#define MCDTOC 0x0080 /* already read toc */ 94#define MCDMBXBSY 0x0100 /* local mbx is busy */ 95 96/* status */ 97#define MCDAUDIOBSY MCD_ST_AUDIOBSY /* playing audio */ 98#define MCDDSKCHNG MCD_ST_DSKCHNG /* sensed change of disk */ 99#define MCDDSKIN MCD_ST_DSKIN /* sensed disk in drive */ 100#define MCDDOOROPEN MCD_ST_DOOROPEN /* sensed door open */ 101 102/* These are apparently the different states a mitsumi can get up to */ 103#define MCDCDABSENT 0x0030 104#define MCDCDPRESENT 0x0020 105#define MCDSCLOSED 0x0080 106#define MCDSOPEN 0x00a0 107 108#define MCD_MD_UNKNOWN (-1) 109 110#define MCD_TYPE_UNKNOWN 0 111#define MCD_TYPE_LU002S 1 112#define MCD_TYPE_LU005S 2 113#define MCD_TYPE_LU006S 3 114#define MCD_TYPE_FX001 4 115#define MCD_TYPE_FX001D 5 116 117/* reader state machine */ 118#define MCD_S_BEGIN 0 119#define MCD_S_BEGIN1 1 120#define MCD_S_WAITSTAT 2 121#define MCD_S_WAITMODE 3 122#define MCD_S_WAITREAD 4 123 124/* prototypes */ 125static void mcd_start(struct mcd_softc *); 126static int mcd_getdisklabel(struct mcd_softc *); 127#ifdef NOTYET 128static void mcd_configure(struct mcd_softc *sc); 129#endif 130static int mcd_get(struct mcd_softc *, char *buf, int nmax); 131static int mcd_setflags(struct mcd_softc *); 132static int mcd_getstat(struct mcd_softc *,int sflg); 133static int mcd_send(struct mcd_softc *, int cmd,int nretrys); 134static void hsg2msf(int hsg, bcd_t *msf); 135static int msf2hsg(bcd_t *msf, int relative); 136static int mcd_volinfo(struct mcd_softc *); 137static int mcd_waitrdy(struct mcd_softc *,int dly); 138static timeout_t mcd_timeout; 139static void mcd_doread(struct mcd_softc *, int state, struct mcd_mbx *mbxin); 140static void mcd_soft_reset(struct mcd_softc *); 141static int mcd_hard_reset(struct mcd_softc *); 142static int mcd_setmode(struct mcd_softc *, int mode); 143static int mcd_getqchan(struct mcd_softc *, struct mcd_qchninfo *q); 144static int mcd_subchan(struct mcd_softc *, struct ioc_read_subchannel *sc); 145static int mcd_toc_header(struct mcd_softc *, struct ioc_toc_header *th); 146static int mcd_read_toc(struct mcd_softc *); 147static int mcd_toc_entrys(struct mcd_softc *, struct ioc_read_toc_entry *te); 148#if 0 149static int mcd_toc_entry(struct mcd_softc *, struct ioc_read_toc_single_entry *te); 150#endif 151static int mcd_stop(struct mcd_softc *); 152static int mcd_eject(struct mcd_softc *); 153static int mcd_inject(struct mcd_softc *); 154static int mcd_playtracks(struct mcd_softc *, struct ioc_play_track *pt); 155static int mcd_play(struct mcd_softc *, struct mcd_read2 *pb); 156static int mcd_playmsf(struct mcd_softc *, struct ioc_play_msf *pt); 157static int mcd_playblocks(struct mcd_softc *, struct ioc_play_blocks *); 158static int mcd_pause(struct mcd_softc *); 159static int mcd_resume(struct mcd_softc *); 160static int mcd_lock_door(struct mcd_softc *, int lock); 161static int mcd_close_tray(struct mcd_softc *); 162 163static d_open_t mcdopen; 164static d_close_t mcdclose; 165static d_ioctl_t mcdioctl; 166static d_psize_t mcdsize; 167static d_strategy_t mcdstrategy; 168 169#define CDEV_MAJOR 29 170 171static struct cdevsw mcd_cdevsw = { 172 /* open */ mcdopen, 173 /* close */ mcdclose, 174 /* read */ physread, 175 /* write */ nowrite, 176 /* ioctl */ mcdioctl, 177 /* poll */ nopoll, 178 /* mmap */ nommap, 179 /* strategy */ mcdstrategy, 180 /* name */ "mcd", 181 /* maj */ CDEV_MAJOR, 182 /* dump */ nodump, 183 /* psize */ nopsize, 184 /* flags */ D_DISK, 185}; 186 187#define MCD_RETRYS 5 188#define MCD_RDRETRYS 8 189 190#define CLOSE_TRAY_SECS 8 191#define DISK_SENSE_SECS 3 192#define WAIT_FRAC 4 193 194/* several delays */ 195#define RDELAY_WAITSTAT 300 196#define RDELAY_WAITMODE 300 197#define RDELAY_WAITREAD 800 198 199#define MIN_DELAY 15 200#define DELAY_GETREPLY 5000000 201 202int 203mcd_attach(struct mcd_softc *sc) 204{ 205 struct mcd_data *cd; 206 int unit; 207 208 cd = &sc->data; 209 unit = device_get_unit(sc->dev); 210 211 cd->flags |= MCDINIT; 212 mcd_soft_reset(sc); 213 bioq_init(&cd->head); 214 215#ifdef NOTYET 216 /* wire controller for interrupts and dma */ 217 mcd_configure(sc); 218#endif 219 /* name filled in probe */ 220 sc->mcd_dev_t[0] = make_dev(&mcd_cdevsw, 0, 221 UID_ROOT, GID_OPERATOR, 0640, "mcd%d", unit); 222 sc->mcd_dev_t[1] = make_dev_alias(sc->mcd_dev_t[0], "mcd%da", unit); 223 sc->mcd_dev_t[2] = make_dev_alias(sc->mcd_dev_t[0], "mcd%dc", unit); 224 225 sc->mcd_dev_t[0]->si_drv1 = sc->mcd_dev_t[1]->si_drv1 = 226 sc->mcd_dev_t[2]->si_drv1 = (void *)sc; 227 228 return 0; 229} 230 231static int 232mcdopen(dev_t dev, int flags, int fmt, struct thread *td) 233{ 234 struct mcd_softc *sc; 235 struct mcd_data *cd; 236 int part,phys,r,retry; 237 238 sc = (struct mcd_softc *)dev->si_drv1; 239 cd = &sc->data; 240 part = mcd_part(dev); 241 phys = mcd_phys(dev); 242 243 /* not initialized*/ 244 if (!(cd->flags & MCDINIT)) 245 return ENXIO; 246 247 /* invalidated in the meantime? mark all open part's invalid */ 248 if (!(cd->flags & MCDVALID) && cd->openflags) 249 return ENXIO; 250 251 if (mcd_getstat(sc, 1) == -1) 252 return EIO; 253 254 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN)) 255 || !(cd->status & MCDDSKIN)) 256 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) { 257 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdsn1", hz/WAIT_FRAC); 258 if ((r = mcd_getstat(sc, 1)) == -1) 259 return EIO; 260 if (r != -2) 261 break; 262 } 263 264 if (( (cd->status & (MCDDOOROPEN|MCDDSKCHNG)) 265 || !(cd->status & MCDDSKIN) 266 ) 267 && major(dev) == CDEV_MAJOR && part == RAW_PART 268 ) { 269 cd->openflags |= (1<<part); 270 if (phys) 271 cd->partflags[part] |= MCDREADRAW; 272 return 0; 273 } 274 if (cd->status & MCDDOOROPEN) { 275 device_printf(sc->dev, "door is open\n"); 276 return ENXIO; 277 } 278 if (!(cd->status & MCDDSKIN)) { 279 device_printf(sc->dev, "no CD inside\n"); 280 return ENXIO; 281 } 282 if (cd->status & MCDDSKCHNG) { 283 device_printf(sc->dev, "CD not sensed\n"); 284 return ENXIO; 285 } 286 287 if (mcdsize(dev) < 0) { 288 if (major(dev) == CDEV_MAJOR && part == RAW_PART) { 289 cd->openflags |= (1<<part); 290 if (phys) 291 cd->partflags[part] |= MCDREADRAW; 292 return 0; 293 } 294 device_printf(sc->dev, "failed to get disk size\n"); 295 return ENXIO; 296 } else 297 cd->flags |= MCDVALID; 298 299 /* XXX get a default disklabel */ 300 mcd_getdisklabel(sc); 301 302MCD_TRACE("open: partition=%d, disksize = %ld, blksize=%d\n", 303 part, cd->disksize, cd->blksize); 304 305 dev->si_bsize_phys = cd->blksize; 306 307 if (part == RAW_PART || 308 (part < cd->dlabel.d_npartitions && 309 cd->dlabel.d_partitions[part].p_fstype != FS_UNUSED)) { 310 cd->openflags |= (1<<part); 311 if (part == RAW_PART && phys) 312 cd->partflags[part] |= MCDREADRAW; 313 (void) mcd_lock_door(sc, MCD_LK_LOCK); 314 if (!(cd->flags & MCDVALID)) 315 return ENXIO; 316 return 0; 317 } 318 319 return ENXIO; 320} 321 322static int 323mcdclose(dev_t dev, int flags, int fmt, struct thread *td) 324{ 325 struct mcd_softc *sc; 326 struct mcd_data *cd; 327 int part; 328 329 sc = (struct mcd_softc *)dev->si_drv1; 330 cd = &sc->data; 331 part = mcd_part(dev); 332 333 if (!(cd->flags & MCDINIT) || !(cd->openflags & (1<<part))) 334 return ENXIO; 335 336 MCD_TRACE("close: partition=%d\n", part); 337 338 (void) mcd_lock_door(sc, MCD_LK_UNLOCK); 339 cd->openflags &= ~(1<<part); 340 cd->partflags[part] &= ~MCDREADRAW; 341 342 return 0; 343} 344 345static void 346mcdstrategy(struct bio *bp) 347{ 348 struct mcd_softc *sc; 349 struct mcd_data *cd; 350 int s; 351 352 int unit = mcd_unit(bp->bio_dev); 353 354 sc = (struct mcd_softc *)bp->bio_dev->si_drv1; 355 cd = &sc->data; 356 357 /* test validity */ 358/*MCD_TRACE("strategy: buf=0x%lx, unit=%ld, block#=%ld bcount=%ld\n", 359 bp,unit,bp->bio_blkno,bp->bio_bcount);*/ 360 if (bp->bio_blkno < 0) { 361 printf("mcdstrategy: unit = %d, blkno = %ld, bcount = %ld\n", 362 unit, (long)bp->bio_blkno, bp->bio_bcount); 363 printf("mcd: mcdstratregy failure"); 364 bp->bio_error = EINVAL; 365 bp->bio_flags |= BIO_ERROR; 366 goto bad; 367 } 368 369 /* if device invalidated (e.g. media change, door open), error */ 370 if (!(cd->flags & MCDVALID)) { 371MCD_TRACE("strategy: drive not valid\n"); 372 bp->bio_error = EIO; 373 goto bad; 374 } 375 376 /* read only */ 377 if (!(bp->bio_cmd == BIO_READ)) { 378 bp->bio_error = EROFS; 379 goto bad; 380 } 381 382 /* no data to read */ 383 if (bp->bio_bcount == 0) 384 goto done; 385 386 /* for non raw access, check partition limits */ 387 if (mcd_part(bp->bio_dev) != RAW_PART) { 388 if (!(cd->flags & MCDLABEL)) { 389 bp->bio_error = EIO; 390 goto bad; 391 } 392 /* adjust transfer if necessary */ 393 if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0) { 394 goto done; 395 } 396 } else { 397 bp->bio_pblkno = bp->bio_blkno; 398 bp->bio_resid = 0; 399 } 400 401 /* queue it */ 402 s = splbio(); 403 bioqdisksort(&cd->head, bp); 404 splx(s); 405 406 /* now check whether we can perform processing */ 407 mcd_start(sc); 408 return; 409 410bad: 411 bp->bio_flags |= BIO_ERROR; 412done: 413 bp->bio_resid = bp->bio_bcount; 414 biodone(bp); 415 return; 416} 417 418static void 419mcd_start(struct mcd_softc *sc) 420{ 421 struct mcd_data *cd = &sc->data; 422 struct partition *p; 423 struct bio *bp; 424 int s = splbio(); 425 426 if (cd->flags & MCDMBXBSY) { 427 splx(s); 428 return; 429 } 430 431 bp = bioq_first(&cd->head); 432 if (bp != 0) { 433 /* block found to process, dequeue */ 434 /*MCD_TRACE("mcd_start: found block bp=0x%x\n",bp,0,0,0);*/ 435 bioq_remove(&cd->head, bp); 436 splx(s); 437 } else { 438 /* nothing to do */ 439 splx(s); 440 return; 441 } 442 443 /* changed media? */ 444 if (!(cd->flags & MCDVALID)) { 445 MCD_TRACE("mcd_start: drive not valid\n"); 446 return; 447 } 448 449 p = cd->dlabel.d_partitions + mcd_part(bp->bio_dev); 450 451 cd->flags |= MCDMBXBSY; 452 if (cd->partflags[mcd_part(bp->bio_dev)] & MCDREADRAW) 453 cd->flags |= MCDREADRAW; 454 cd->mbx.unit = device_get_unit(sc->dev); 455 cd->mbx.retry = MCD_RETRYS; 456 cd->mbx.bp = bp; 457 cd->mbx.p_offset = p->p_offset; 458 459 /* calling the read routine */ 460 mcd_doread(sc, MCD_S_BEGIN,&(cd->mbx)); 461 /* triggers mcd_start, when successful finished */ 462 return; 463} 464 465static int 466mcdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 467{ 468 struct mcd_softc *sc; 469 struct mcd_data *cd; 470 int unit,part,retry,r; 471 472 unit = mcd_unit(dev); 473 part = mcd_part(dev); 474 sc = (struct mcd_softc *)dev->si_drv1; 475 cd = &sc->data; 476 477 if (mcd_getstat(sc, 1) == -1) /* detect disk change too */ 478 return EIO; 479MCD_TRACE("ioctl called 0x%lx\n", cmd); 480 481 switch (cmd) { 482 case CDIOCSETPATCH: 483 case CDIOCGETVOL: 484 case CDIOCSETVOL: 485 case CDIOCSETMONO: 486 case CDIOCSETSTERIO: 487 case CDIOCSETMUTE: 488 case CDIOCSETLEFT: 489 case CDIOCSETRIGHT: 490 return EINVAL; 491 case CDIOCEJECT: 492 return mcd_eject(sc); 493 case CDIOCSETDEBUG: 494 cd->debug = 1; 495 return 0; 496 case CDIOCCLRDEBUG: 497 cd->debug = 0; 498 return 0; 499 case CDIOCRESET: 500 return mcd_hard_reset(sc); 501 case CDIOCALLOW: 502 return mcd_lock_door(sc, MCD_LK_UNLOCK); 503 case CDIOCPREVENT: 504 return mcd_lock_door(sc, MCD_LK_LOCK); 505 case CDIOCCLOSE: 506 return mcd_inject(sc); 507 } 508 509 if (!(cd->flags & MCDVALID)) { 510 if ( major(dev) != CDEV_MAJOR 511 || part != RAW_PART 512 || !(cd->openflags & (1<<RAW_PART)) 513 ) 514 return ENXIO; 515 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN)) 516 || !(cd->status & MCDDSKIN)) 517 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) { 518 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdsn2", hz/WAIT_FRAC); 519 if ((r = mcd_getstat(sc, 1)) == -1) 520 return EIO; 521 if (r != -2) 522 break; 523 } 524 if ( (cd->status & (MCDDOOROPEN|MCDDSKCHNG)) 525 || !(cd->status & MCDDSKIN) 526 || mcdsize(dev) < 0 527 ) 528 return ENXIO; 529 cd->flags |= MCDVALID; 530 mcd_getdisklabel(sc); 531 if (mcd_phys(dev)) 532 cd->partflags[part] |= MCDREADRAW; 533 (void) mcd_lock_door(sc, MCD_LK_LOCK); 534 if (!(cd->flags & MCDVALID)) 535 return ENXIO; 536 } 537 538 switch (cmd) { 539 case DIOCGDINFO: 540 *(struct disklabel *) addr = cd->dlabel; 541 return 0; 542 /* 543 * a bit silly, but someone might want to test something on a 544 * section of cdrom. 545 */ 546 case DIOCWDINFO: 547 case DIOCSDINFO: 548 if ((flags & FWRITE) == 0) 549 return EBADF; 550 else { 551 return setdisklabel(&cd->dlabel, 552 (struct disklabel *) addr, 553 0); 554 } 555 case DIOCWLABEL: 556 return EBADF; 557 case CDIOCPLAYTRACKS: 558 return mcd_playtracks(sc, (struct ioc_play_track *) addr); 559 case CDIOCPLAYBLOCKS: 560 return mcd_playblocks(sc, (struct ioc_play_blocks *) addr); 561 case CDIOCPLAYMSF: 562 return mcd_playmsf(sc, (struct ioc_play_msf *) addr); 563 case CDIOCREADSUBCHANNEL: 564 return mcd_subchan(sc, (struct ioc_read_subchannel *) addr); 565 case CDIOREADTOCHEADER: 566 return mcd_toc_header(sc, (struct ioc_toc_header *) addr); 567 case CDIOREADTOCENTRYS: 568 return mcd_toc_entrys(sc, (struct ioc_read_toc_entry *) addr); 569 case CDIOCRESUME: 570 return mcd_resume(sc); 571 case CDIOCPAUSE: 572 return mcd_pause(sc); 573 case CDIOCSTART: 574 if (mcd_setmode(sc, MCD_MD_COOKED) != 0) 575 return EIO; 576 return 0; 577 case CDIOCSTOP: 578 return mcd_stop(sc); 579 default: 580 return ENOTTY; 581 } 582 /*NOTREACHED*/ 583} 584 585/* this could have been taken from scsi/cd.c, but it is not clear 586 * whether the scsi cd driver is linked in 587 */ 588static int 589mcd_getdisklabel(struct mcd_softc *sc) 590{ 591 struct mcd_data *cd = &sc->data; 592 593 if (cd->flags & MCDLABEL) 594 return -1; 595 596 bzero(&cd->dlabel,sizeof(struct disklabel)); 597 /* filled with spaces first */ 598 strncpy(cd->dlabel.d_typename," ", 599 sizeof(cd->dlabel.d_typename)); 600 strncpy(cd->dlabel.d_typename, cd->name, 601 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1)); 602 strncpy(cd->dlabel.d_packname,"unknown ", 603 sizeof(cd->dlabel.d_packname)); 604 cd->dlabel.d_secsize = cd->blksize; 605 cd->dlabel.d_nsectors = 100; 606 cd->dlabel.d_ntracks = 1; 607 cd->dlabel.d_ncylinders = (cd->disksize/100)+1; 608 cd->dlabel.d_secpercyl = 100; 609 cd->dlabel.d_secperunit = cd->disksize; 610 cd->dlabel.d_rpm = 300; 611 cd->dlabel.d_interleave = 1; 612 cd->dlabel.d_flags = D_REMOVABLE; 613 cd->dlabel.d_npartitions= 1; 614 cd->dlabel.d_partitions[0].p_offset = 0; 615 cd->dlabel.d_partitions[0].p_size = cd->disksize; 616 cd->dlabel.d_partitions[0].p_fstype = 9; 617 cd->dlabel.d_magic = DISKMAGIC; 618 cd->dlabel.d_magic2 = DISKMAGIC; 619 cd->dlabel.d_checksum = dkcksum(&cd->dlabel); 620 621 cd->flags |= MCDLABEL; 622 return 0; 623} 624 625static int 626mcdsize(dev_t dev) 627{ 628 struct mcd_softc *sc; 629 struct mcd_data *cd; 630 int size; 631 int unit; 632 633 unit = mcd_unit(dev); 634 sc = (struct mcd_softc *)dev->si_drv1; 635 cd = &sc->data; 636 637 if (mcd_volinfo(sc) == 0) { 638 cd->blksize = MCDBLK; 639 size = msf2hsg(cd->volinfo.vol_msf, 0); 640 cd->disksize = size * (MCDBLK/DEV_BSIZE); 641 return 0; 642 } 643 return -1; 644} 645 646/*************************************************************** 647 * lower level of driver starts here 648 **************************************************************/ 649 650#ifdef NOTDEF 651static char 652irqs[] = { 653 0x00,0x00,0x10,0x20,0x00,0x30,0x00,0x00, 654 0x00,0x10,0x40,0x50,0x00,0x00,0x00,0x00 655}; 656 657static char 658drqs[] = { 659 0x00,0x01,0x00,0x03,0x00,0x05,0x06,0x07, 660}; 661#endif 662 663#ifdef NOT_YET 664static void 665mcd_configure(struct mcd_softc *sc) 666{ 667 MCD_WRITE(sc, MCD_REG_CONFIG, sc->data.config); 668} 669#endif 670 671/* Wait for non-busy - return 0 on timeout */ 672static int 673twiddle_thumbs(struct mcd_softc *sc, int count, char *whine) 674{ 675 int i; 676 677 for (i = 0; i < count; i++) { 678 if (!(MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL)) 679 return 1; 680 } 681 if (bootverbose) 682 device_printf(sc->dev, "timeout %s\n", whine); 683 return 0; 684} 685 686/* check to see if a Mitsumi CD-ROM is attached to the ISA bus */ 687 688int 689mcd_probe(struct mcd_softc *sc) 690{ 691 int unit; 692 int i, j; 693 unsigned char stbytes[3]; 694 695 unit = device_get_unit(sc->dev); 696 sc->data.flags = MCDPROBING; 697 698#ifdef NOTDEF 699 /* get irq/drq configuration word */ 700 sc->data.config = irqs[dev->id_irq]; /* | drqs[dev->id_drq];*/ 701#else 702 sc->data.config = 0; 703#endif 704 705 /* send a reset */ 706 MCD_WRITE(sc, MCD_FLAGS, M_RESET); 707 708 /* 709 * delay awhile by getting any pending garbage (old data) and 710 * throwing it away. 711 */ 712 for (i = 1000000; i != 0; i--) 713 (void)MCD_READ(sc, MCD_FLAGS); 714 715 /* Get status */ 716 MCD_WRITE(sc, MCD_DATA, MCD_CMDGETSTAT); 717 if (!twiddle_thumbs(sc, 1000000, "getting status")) 718 return (ENXIO); /* Timeout */ 719 /* Get version information */ 720 MCD_WRITE(sc, MCD_DATA, MCD_CMDCONTINFO); 721 for (j = 0; j < 3; j++) { 722 if (!twiddle_thumbs(sc, 3000, "getting version info")) 723 return (ENXIO); 724 stbytes[j] = (MCD_READ(sc, MCD_DATA) & 0xFF); 725 } 726 if (stbytes[1] == stbytes[2]) 727 return (ENXIO); 728 if (stbytes[2] >= 4 || stbytes[1] != 'M') { 729 MCD_WRITE(sc, MCD_CTRL, M_PICKLE); 730 sc->data.flags |= MCDNEWMODEL; 731 } 732 sc->data.read_command = MCD_CMDSINGLESPEEDREAD; 733 switch (stbytes[1]) { 734 case 'M': 735 if (stbytes[2] <= 2) { 736 sc->data.type = MCD_TYPE_LU002S; 737 sc->data.name = "Mitsumi LU002S"; 738 } else if (stbytes[2] <= 5) { 739 sc->data.type = MCD_TYPE_LU005S; 740 sc->data.name = "Mitsumi LU005S"; 741 } else { 742 sc->data.type = MCD_TYPE_LU006S; 743 sc->data.name = "Mitsumi LU006S"; 744 } 745 break; 746 case 'F': 747 sc->data.type = MCD_TYPE_FX001; 748 sc->data.name = "Mitsumi FX001"; 749 break; 750 case 'D': 751 sc->data.type = MCD_TYPE_FX001D; 752 sc->data.name = "Mitsumi FX001D"; 753 sc->data.read_command = MCD_CMDDOUBLESPEEDREAD; 754 break; 755 default: 756 sc->data.type = MCD_TYPE_UNKNOWN; 757 sc->data.name = "Mitsumi ???"; 758 break; 759 } 760 761 if (bootverbose) 762 device_printf(sc->dev, "type %s, version info: %c %x\n", 763 sc->data.name, stbytes[1], stbytes[2]); 764 765 return 0; 766} 767 768 769static int 770mcd_waitrdy(struct mcd_softc *sc, int dly) 771{ 772 int i; 773 774 /* wait until flag port senses status ready */ 775 for (i=0; i<dly; i+=MIN_DELAY) { 776 if (!(MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL)) 777 return 0; 778 DELAY(MIN_DELAY); 779 } 780 return -1; 781} 782 783static int 784mcd_getreply(struct mcd_softc *sc, int dly) 785{ 786 787 /* wait data to become ready */ 788 if (mcd_waitrdy(sc, dly)<0) { 789 device_printf(sc->dev, "timeout getreply\n"); 790 return -1; 791 } 792 793 /* get the data */ 794 return MCD_READ(sc, MCD_REG_STATUS) & 0xFF; 795} 796 797static int 798mcd_getstat(struct mcd_softc *sc, int sflg) 799{ 800 int i; 801 struct mcd_data *cd = &sc->data; 802 803 /* get the status */ 804 if (sflg) 805 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDGETSTAT); 806 i = mcd_getreply(sc, DELAY_GETREPLY); 807 if (i<0 || (i & MCD_ST_CMDCHECK)) { 808 cd->curr_mode = MCD_MD_UNKNOWN; 809 return -1; 810 } 811 812 cd->status = i; 813 814 if (mcd_setflags(sc) < 0) 815 return -2; 816 return cd->status; 817} 818 819static int 820mcd_setflags(struct mcd_softc *sc) 821{ 822 struct mcd_data *cd = &sc->data; 823 824 /* check flags */ 825 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN)) 826 || !(cd->status & MCDDSKIN)) { 827 MCD_TRACE("setflags: sensed DSKCHNG or DOOROPEN or !DSKIN\n"); 828 mcd_soft_reset(sc); 829 return -1; 830 } 831 832 if (cd->status & MCDAUDIOBSY) 833 cd->audio_status = CD_AS_PLAY_IN_PROGRESS; 834 else if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS) 835 cd->audio_status = CD_AS_PLAY_COMPLETED; 836 return 0; 837} 838 839static int 840mcd_get(struct mcd_softc *sc, char *buf, int nmax) 841{ 842 int i,k; 843 844 for (i=0; i<nmax; i++) { 845 /* wait for data */ 846 if ((k = mcd_getreply(sc, DELAY_GETREPLY)) < 0) { 847 device_printf(sc->dev, "timeout mcd_get\n"); 848 return -1; 849 } 850 buf[i] = k; 851 } 852 return i; 853} 854 855static int 856mcd_send(struct mcd_softc *sc, int cmd,int nretrys) 857{ 858 int i,k=0; 859 860/*MCD_TRACE("mcd_send: command = 0x%02x\n",cmd,0,0,0);*/ 861 for (i=0; i<nretrys; i++) { 862 MCD_WRITE(sc, MCD_REG_COMMAND, cmd); 863 if ((k=mcd_getstat(sc, 0)) != -1) 864 break; 865 } 866 if (k == -2) { 867 device_printf(sc->dev, "media changed\n"); 868 return -1; 869 } 870 if (i == nretrys) { 871 device_printf(sc->dev, "mcd_send retry cnt exceeded\n"); 872 return -1; 873 } 874/*MCD_TRACE("mcd_send: done\n",0,0,0,0);*/ 875 return 0; 876} 877 878static void 879hsg2msf(int hsg, bcd_t *msf) 880{ 881 hsg += 150; 882 F_msf(msf) = bin2bcd(hsg % 75); 883 hsg /= 75; 884 S_msf(msf) = bin2bcd(hsg % 60); 885 hsg /= 60; 886 M_msf(msf) = bin2bcd(hsg); 887} 888 889static int 890msf2hsg(bcd_t *msf, int relative) 891{ 892 return (bcd2bin(M_msf(msf)) * 60 + bcd2bin(S_msf(msf))) * 75 + 893 bcd2bin(F_msf(msf)) - (!relative) * 150; 894} 895 896static int 897mcd_volinfo(struct mcd_softc *sc) 898{ 899 struct mcd_data *cd = &sc->data; 900 901 /* Just return if we already have it */ 902 if (cd->flags & MCDVOLINFO) return 0; 903 904/*MCD_TRACE("mcd_volinfo: enter\n",0,0,0,0);*/ 905 906 /* send volume info command */ 907 if (mcd_send(sc, MCD_CMDGETVOLINFO,MCD_RETRYS) < 0) 908 return EIO; 909 910 /* get data */ 911 if (mcd_get(sc, (char*) &cd->volinfo,sizeof(struct mcd_volinfo)) < 0) { 912 device_printf(sc->dev, "mcd_volinfo: error read data\n"); 913 return EIO; 914 } 915 916 if (cd->volinfo.trk_low > 0 && 917 cd->volinfo.trk_high >= cd->volinfo.trk_low 918 ) { 919 cd->flags |= MCDVOLINFO; /* volinfo is OK */ 920 return 0; 921 } 922 923 return EINVAL; 924} 925 926/* state machine to process read requests 927 * initialize with MCD_S_BEGIN: calculate sizes, and read status 928 * MCD_S_WAITSTAT: wait for status reply, set mode 929 * MCD_S_WAITMODE: waits for status reply from set mode, set read command 930 * MCD_S_WAITREAD: wait for read ready, read data 931 */ 932static void 933mcd_timeout(void *arg) 934{ 935 struct mcd_softc *sc; 936 937 sc = (struct mcd_softc *)arg; 938 939 mcd_doread(sc, sc->ch_state, sc->ch_mbxsave); 940} 941 942static void 943mcd_doread(struct mcd_softc *sc, int state, struct mcd_mbx *mbxin) 944{ 945 struct mcd_mbx *mbx; 946 struct bio *bp; 947 struct mcd_data *cd; 948 int rm, i, k; 949 struct mcd_read2 rbuf; 950 int blknum; 951 caddr_t addr; 952 953 mbx = (state!=MCD_S_BEGIN) ? sc->ch_mbxsave : mbxin; 954 bp = mbx->bp; 955 cd = &sc->data; 956 957loop: 958 switch (state) { 959 case MCD_S_BEGIN: 960 mbx = sc->ch_mbxsave = mbxin; 961 962 case MCD_S_BEGIN1: 963retry_status: 964 /* get status */ 965 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDGETSTAT); 966 mbx->count = RDELAY_WAITSTAT; 967 sc->ch_state = MCD_S_WAITSTAT; 968 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */ 969 return; 970 case MCD_S_WAITSTAT: 971 sc->ch_state = MCD_S_WAITSTAT; 972 untimeout(mcd_timeout,(caddr_t)sc, sc->ch); 973 if (mbx->count-- >= 0) { 974 if (MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) { 975 sc->ch_state = MCD_S_WAITSTAT; 976 timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */ 977 return; 978 } 979 cd->status = MCD_READ(sc, MCD_REG_STATUS) & 0xFF; 980 if (cd->status & MCD_ST_CMDCHECK) 981 goto retry_status; 982 if (mcd_setflags(sc) < 0) 983 goto changed; 984 MCD_TRACE("got WAITSTAT delay=%d\n", 985 RDELAY_WAITSTAT-mbx->count); 986 /* reject, if audio active */ 987 if (cd->status & MCDAUDIOBSY) { 988 device_printf(sc->dev, "audio is active\n"); 989 goto readerr; 990 } 991 992retry_mode: 993 /* to check for raw/cooked mode */ 994 if (cd->flags & MCDREADRAW) { 995 rm = MCD_MD_RAW; 996 mbx->sz = MCDRBLK; 997 } else { 998 rm = MCD_MD_COOKED; 999 mbx->sz = cd->blksize; 1000 } 1001 1002 if (rm == cd->curr_mode) 1003 goto modedone; 1004 1005 mbx->count = RDELAY_WAITMODE; 1006 1007 cd->curr_mode = MCD_MD_UNKNOWN; 1008 mbx->mode = rm; 1009 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDSETMODE); 1010 MCD_WRITE(sc, MCD_REG_COMMAND, rm); 1011 1012 sc->ch_state = MCD_S_WAITMODE; 1013 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */ 1014 return; 1015 } else { 1016 device_printf(sc->dev, "timeout getstatus\n"); 1017 goto readerr; 1018 } 1019 1020 case MCD_S_WAITMODE: 1021 sc->ch_state = MCD_S_WAITMODE; 1022 untimeout(mcd_timeout, (caddr_t)sc, sc->ch); 1023 if (mbx->count-- < 0) { 1024 device_printf(sc->dev, "timeout set mode\n"); 1025 goto readerr; 1026 } 1027 if (MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) { 1028 sc->ch_state = MCD_S_WAITMODE; 1029 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100); 1030 return; 1031 } 1032 cd->status = MCD_READ(sc, MCD_REG_STATUS) & 0xFF; 1033 if (cd->status & MCD_ST_CMDCHECK) { 1034 cd->curr_mode = MCD_MD_UNKNOWN; 1035 goto retry_mode; 1036 } 1037 if (mcd_setflags(sc) < 0) 1038 goto changed; 1039 cd->curr_mode = mbx->mode; 1040 MCD_TRACE("got WAITMODE delay=%d\n", 1041 RDELAY_WAITMODE-mbx->count); 1042modedone: 1043 /* for first block */ 1044 mbx->nblk = (bp->bio_bcount + (mbx->sz-1)) / mbx->sz; 1045 mbx->skip = 0; 1046 1047nextblock: 1048 blknum = (bp->bio_blkno / (mbx->sz/DEV_BSIZE)) 1049 + mbx->p_offset + mbx->skip/mbx->sz; 1050 1051 MCD_TRACE("mcd_doread: read blknum=%d for bp=%p\n", 1052 blknum, bp); 1053 1054 /* build parameter block */ 1055 hsg2msf(blknum,rbuf.start_msf); 1056retry_read: 1057 /* send the read command */ 1058 disable_intr(); 1059 MCD_WRITE(sc, MCD_REG_COMMAND, cd->read_command); 1060 MCD_WRITE(sc, MCD_REG_COMMAND, rbuf.start_msf[0]); 1061 MCD_WRITE(sc, MCD_REG_COMMAND, rbuf.start_msf[1]); 1062 MCD_WRITE(sc, MCD_REG_COMMAND, rbuf.start_msf[2]); 1063 MCD_WRITE(sc, MCD_REG_COMMAND, 0); 1064 MCD_WRITE(sc, MCD_REG_COMMAND, 0); 1065 MCD_WRITE(sc, MCD_REG_COMMAND, 1); 1066 enable_intr(); 1067 1068 /* Spin briefly (<= 2ms) to avoid missing next block */ 1069 for (i = 0; i < 20; i++) { 1070 k = MCD_READ(sc, MCD_FLAGS); 1071 if (!(k & MFL_DATA_NOT_AVAIL)) 1072 goto got_it; 1073 DELAY(100); 1074 } 1075 1076 mbx->count = RDELAY_WAITREAD; 1077 sc->ch_state = MCD_S_WAITREAD; 1078 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */ 1079 return; 1080 case MCD_S_WAITREAD: 1081 sc->ch_state = MCD_S_WAITREAD; 1082 untimeout(mcd_timeout, (caddr_t)sc, sc->ch); 1083 if (mbx->count-- > 0) { 1084 k = MCD_READ(sc, MCD_FLAGS); 1085 if (!(k & MFL_DATA_NOT_AVAIL)) { /* XXX */ 1086 MCD_TRACE("got data delay=%d\n", 1087 RDELAY_WAITREAD-mbx->count); 1088 got_it: 1089 /* data is ready */ 1090 addr = bp->bio_data + mbx->skip; 1091 1092 MCD_WRITE(sc, MCD_REG_CTL2,0x04); /* XXX */ 1093 for (i=0; i<mbx->sz; i++) 1094 *addr++ = MCD_READ(sc, MCD_REG_RDATA); 1095 MCD_WRITE(sc, MCD_REG_CTL2,0x0c); /* XXX */ 1096 1097 k = MCD_READ(sc, MCD_FLAGS); 1098 /* If we still have some junk, read it too */ 1099 if (!(k & MFL_DATA_NOT_AVAIL)) { 1100 MCD_WRITE(sc, MCD_REG_CTL2, 0x04); /* XXX */ 1101 (void)MCD_READ(sc, MCD_REG_RDATA); 1102 (void)MCD_READ(sc, MCD_REG_RDATA); 1103 MCD_WRITE(sc, MCD_REG_CTL2, 0x0c); /* XXX */ 1104 } 1105 1106 if (--mbx->nblk > 0) { 1107 mbx->skip += mbx->sz; 1108 goto nextblock; 1109 } 1110 1111 /* return buffer */ 1112 bp->bio_resid = 0; 1113 biodone(bp); 1114 1115 cd->flags &= ~(MCDMBXBSY|MCDREADRAW); 1116 mcd_start(sc); 1117 return; 1118 } 1119 if (!(k & MFL_STATUS_NOT_AVAIL)) { 1120 cd->status = MCD_READ(sc, MCD_REG_STATUS) & 0xFF; 1121 if (cd->status & MCD_ST_CMDCHECK) 1122 goto retry_read; 1123 if (mcd_setflags(sc) < 0) 1124 goto changed; 1125 } 1126 sc->ch_state = MCD_S_WAITREAD; 1127 sc->ch = timeout(mcd_timeout, (caddr_t)sc, hz/100); /* XXX */ 1128 return; 1129 } else { 1130 device_printf(sc->dev, "timeout read data\n"); 1131 goto readerr; 1132 } 1133 } 1134 1135readerr: 1136 if (mbx->retry-- > 0) { 1137 device_printf(sc->dev, "retrying\n"); 1138 state = MCD_S_BEGIN1; 1139 goto loop; 1140 } 1141harderr: 1142 /* invalidate the buffer */ 1143 bp->bio_flags |= BIO_ERROR; 1144 bp->bio_resid = bp->bio_bcount; 1145 biodone(bp); 1146 1147 cd->flags &= ~(MCDMBXBSY|MCDREADRAW); 1148 mcd_start(sc); 1149 return; 1150 1151changed: 1152 device_printf(sc->dev, "media changed\n"); 1153 goto harderr; 1154 1155#ifdef NOTDEF 1156 device_printf(sc->dev, "unit timeout, resetting\n"); 1157 MCD_WRITE(sc, MCD_REG_RESET, MCD_CMDRESET); 1158 DELAY(300000); 1159 (void)mcd_getstat(sc, 1); 1160 (void)mcd_getstat(sc, 1); 1161 /*cd->status &= ~MCDDSKCHNG; */ 1162 cd->debug = 1; /* preventive set debug mode */ 1163 1164#endif 1165 1166} 1167 1168static int 1169mcd_lock_door(struct mcd_softc *sc, int lock) 1170{ 1171 1172 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDLOCKDRV); 1173 MCD_WRITE(sc, MCD_REG_COMMAND, lock); 1174 if (mcd_getstat(sc, 0) == -1) 1175 return EIO; 1176 return 0; 1177} 1178 1179static int 1180mcd_close_tray(struct mcd_softc *sc) 1181{ 1182 struct mcd_data *cd = &sc->data; 1183 int retry, r; 1184 1185 if (mcd_getstat(sc, 1) == -1) 1186 return EIO; 1187 if (cd->status & MCDDOOROPEN) { 1188 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDCLOSETRAY); 1189 for (retry = 0; retry < CLOSE_TRAY_SECS * WAIT_FRAC; retry++) { 1190 if (MCD_READ(sc, MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) 1191 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdcls", hz/WAIT_FRAC); 1192 else { 1193 if ((r = mcd_getstat(sc, 0)) == -1) 1194 return EIO; 1195 return 0; 1196 } 1197 } 1198 return ENXIO; 1199 } 1200 return 0; 1201} 1202 1203static int 1204mcd_eject(struct mcd_softc *sc) 1205{ 1206 struct mcd_data *cd = &sc->data; 1207 int r; 1208 1209 if (mcd_getstat(sc, 1) == -1) /* detect disk change too */ 1210 return EIO; 1211 if (cd->status & MCDDOOROPEN) 1212 return 0; 1213 if ((r = mcd_stop(sc)) == EIO) 1214 return r; 1215 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDEJECTDISK); 1216 if (mcd_getstat(sc, 0) == -1) 1217 return EIO; 1218 return 0; 1219} 1220 1221static int 1222mcd_inject(struct mcd_softc *sc) 1223{ 1224 struct mcd_data *cd = &sc->data; 1225 1226 if (mcd_getstat(sc, 1) == -1) /* detect disk change too */ 1227 return EIO; 1228 if (cd->status & MCDDOOROPEN) 1229 return mcd_close_tray(sc); 1230 return 0; 1231} 1232 1233static int 1234mcd_hard_reset(struct mcd_softc *sc) 1235{ 1236 struct mcd_data *cd = &sc->data; 1237 1238 MCD_WRITE(sc, MCD_REG_RESET, MCD_CMDRESET); 1239 cd->curr_mode = MCD_MD_UNKNOWN; 1240 cd->audio_status = CD_AS_AUDIO_INVALID; 1241 return 0; 1242} 1243 1244static void 1245mcd_soft_reset(struct mcd_softc *sc) 1246{ 1247 struct mcd_data *cd = &sc->data; 1248 int i; 1249 1250 cd->flags &= (MCDINIT|MCDPROBING|MCDNEWMODEL); 1251 cd->curr_mode = MCD_MD_UNKNOWN; 1252 for (i=0; i<MAXPARTITIONS; i++) cd->partflags[i] = 0; 1253 cd->audio_status = CD_AS_AUDIO_INVALID; 1254} 1255 1256static int 1257mcd_setmode(struct mcd_softc *sc, int mode) 1258{ 1259 struct mcd_data *cd = &sc->data; 1260 int retry, st; 1261 1262 if (cd->curr_mode == mode) 1263 return 0; 1264 if (cd->debug) 1265 device_printf(sc->dev, "setting mode to %d\n", mode); 1266 for(retry=0; retry<MCD_RETRYS; retry++) 1267 { 1268 cd->curr_mode = MCD_MD_UNKNOWN; 1269 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDSETMODE); 1270 MCD_WRITE(sc, MCD_REG_COMMAND, mode); 1271 if ((st = mcd_getstat(sc, 0)) >= 0) { 1272 cd->curr_mode = mode; 1273 return 0; 1274 } 1275 if (st == -2) { 1276 device_printf(sc->dev, "media changed\n"); 1277 break; 1278 } 1279 } 1280 1281 return -1; 1282} 1283 1284static int 1285mcd_toc_header(struct mcd_softc *sc, struct ioc_toc_header *th) 1286{ 1287 struct mcd_data *cd = &sc->data; 1288 int r; 1289 1290 if ((r = mcd_volinfo(sc)) != 0) 1291 return r; 1292 1293 th->starting_track = bcd2bin(cd->volinfo.trk_low); 1294 th->ending_track = bcd2bin(cd->volinfo.trk_high); 1295 th->len = 2 * sizeof(u_char) /* start & end tracks */ + 1296 (th->ending_track + 1 - th->starting_track + 1) * 1297 sizeof(struct cd_toc_entry); 1298 1299 return 0; 1300} 1301 1302static int 1303mcd_read_toc(struct mcd_softc *sc) 1304{ 1305 struct mcd_data *cd = &sc->data; 1306 struct ioc_toc_header th; 1307 struct mcd_qchninfo q; 1308 int rc, trk, idx, retry; 1309 1310 /* Only read TOC if needed */ 1311 if (cd->flags & MCDTOC) 1312 return 0; 1313 1314 if (cd->debug) 1315 device_printf(sc->dev, "reading toc header\n"); 1316 1317 if ((rc = mcd_toc_header(sc, &th)) != 0) 1318 return rc; 1319 1320 if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0) 1321 return EIO; 1322 1323 if (mcd_setmode(sc, MCD_MD_TOC) != 0) 1324 return EIO; 1325 1326 if (cd->debug) 1327 device_printf(sc->dev, "get_toc reading qchannel info\n"); 1328 1329 for(trk=th.starting_track; trk<=th.ending_track; trk++) 1330 cd->toc[trk].idx_no = 0; 1331 trk = th.ending_track - th.starting_track + 1; 1332 for(retry=0; retry<600 && trk>0; retry++) 1333 { 1334 if (mcd_getqchan(sc, &q) < 0) break; 1335 idx = bcd2bin(q.idx_no); 1336 if (idx>=th.starting_track && idx<=th.ending_track && q.trk_no==0) { 1337 if (cd->toc[idx].idx_no == 0) { 1338 cd->toc[idx] = q; 1339 trk--; 1340 } 1341 } 1342 } 1343 1344 if (mcd_setmode(sc, MCD_MD_COOKED) != 0) 1345 return EIO; 1346 1347 if (trk != 0) 1348 return ENXIO; 1349 1350 /* add a fake last+1 */ 1351 idx = th.ending_track + 1; 1352 cd->toc[idx].control = cd->toc[idx-1].control; 1353 cd->toc[idx].addr_type = cd->toc[idx-1].addr_type; 1354 cd->toc[idx].trk_no = 0; 1355 cd->toc[idx].idx_no = MCD_LASTPLUS1; 1356 cd->toc[idx].hd_pos_msf[0] = cd->volinfo.vol_msf[0]; 1357 cd->toc[idx].hd_pos_msf[1] = cd->volinfo.vol_msf[1]; 1358 cd->toc[idx].hd_pos_msf[2] = cd->volinfo.vol_msf[2]; 1359 1360 if (cd->debug) 1361 { int i; 1362 for (i = th.starting_track; i <= idx; i++) 1363 device_printf(sc->dev, "trk %d idx %d pos %d %d %d\n", 1364 i, 1365 cd->toc[i].idx_no > 0x99 ? cd->toc[i].idx_no : 1366 bcd2bin(cd->toc[i].idx_no), 1367 bcd2bin(cd->toc[i].hd_pos_msf[0]), 1368 bcd2bin(cd->toc[i].hd_pos_msf[1]), 1369 bcd2bin(cd->toc[i].hd_pos_msf[2])); 1370 } 1371 1372 cd->flags |= MCDTOC; 1373 1374 return 0; 1375} 1376 1377#if 0 1378static int 1379mcd_toc_entry(struct mcd_softc *sc, struct ioc_read_toc_single_entry *te) 1380{ 1381 struct mcd_data *cd = &sc->data; 1382 struct ioc_toc_header th; 1383 int rc, trk; 1384 1385 if (te->address_format != CD_MSF_FORMAT 1386 && te->address_format != CD_LBA_FORMAT) 1387 return EINVAL; 1388 1389 /* Copy the toc header */ 1390 if ((rc = mcd_toc_header(sc, &th)) != 0) 1391 return rc; 1392 1393 /* verify starting track */ 1394 trk = te->track; 1395 if (trk == 0) 1396 trk = th.starting_track; 1397 else if (trk == MCD_LASTPLUS1) 1398 trk = th.ending_track + 1; 1399 else if (trk < th.starting_track || trk > th.ending_track + 1) 1400 return EINVAL; 1401 1402 /* Make sure we have a valid toc */ 1403 if ((rc=mcd_read_toc(sc)) != 0) 1404 return rc; 1405 1406 /* Copy the TOC data. */ 1407 if (cd->toc[trk].idx_no == 0) 1408 return EIO; 1409 1410 te->entry.control = cd->toc[trk].control; 1411 te->entry.addr_type = cd->toc[trk].addr_type; 1412 te->entry.track = 1413 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no : 1414 bcd2bin(cd->toc[trk].idx_no); 1415 switch (te->address_format) { 1416 case CD_MSF_FORMAT: 1417 te->entry.addr.msf.unused = 0; 1418 te->entry.addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]); 1419 te->entry.addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]); 1420 te->entry.addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]); 1421 break; 1422 case CD_LBA_FORMAT: 1423 te->entry.addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0)); 1424 break; 1425 } 1426 return 0; 1427} 1428#endif 1429 1430static int 1431mcd_toc_entrys(struct mcd_softc *sc, struct ioc_read_toc_entry *te) 1432{ 1433 struct mcd_data *cd = &sc->data; 1434 struct cd_toc_entry entries[MCD_MAXTOCS]; 1435 struct ioc_toc_header th; 1436 int rc, n, trk, len; 1437 1438 if ( te->data_len < sizeof(entries[0]) 1439 || (te->data_len % sizeof(entries[0])) != 0 1440 || (te->address_format != CD_MSF_FORMAT 1441 && te->address_format != CD_LBA_FORMAT) 1442 ) 1443 return EINVAL; 1444 1445 /* Copy the toc header */ 1446 if ((rc = mcd_toc_header(sc, &th)) != 0) 1447 return rc; 1448 1449 /* verify starting track */ 1450 trk = te->starting_track; 1451 if (trk == 0) 1452 trk = th.starting_track; 1453 else if (trk == MCD_LASTPLUS1) 1454 trk = th.ending_track + 1; 1455 else if (trk < th.starting_track || trk > th.ending_track + 1) 1456 return EINVAL; 1457 1458 len = ((th.ending_track + 1 - trk) + 1) * 1459 sizeof(entries[0]); 1460 if (te->data_len < len) 1461 len = te->data_len; 1462 if (len > sizeof(entries)) 1463 return EINVAL; 1464 1465 /* Make sure we have a valid toc */ 1466 if ((rc=mcd_read_toc(sc)) != 0) 1467 return rc; 1468 1469 /* Copy the TOC data. */ 1470 for (n = 0; len > 0 && trk <= th.ending_track + 1; trk++) { 1471 if (cd->toc[trk].idx_no == 0) 1472 continue; 1473 entries[n].control = cd->toc[trk].control; 1474 entries[n].addr_type = cd->toc[trk].addr_type; 1475 entries[n].track = 1476 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no : 1477 bcd2bin(cd->toc[trk].idx_no); 1478 switch (te->address_format) { 1479 case CD_MSF_FORMAT: 1480 entries[n].addr.msf.unused = 0; 1481 entries[n].addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]); 1482 entries[n].addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]); 1483 entries[n].addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]); 1484 break; 1485 case CD_LBA_FORMAT: 1486 entries[n].addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0)); 1487 break; 1488 } 1489 len -= sizeof(struct cd_toc_entry); 1490 n++; 1491 } 1492 1493 /* copy the data back */ 1494 return copyout(entries, te->data, n * sizeof(struct cd_toc_entry)); 1495} 1496 1497static int 1498mcd_stop(struct mcd_softc *sc) 1499{ 1500 struct mcd_data *cd = &sc->data; 1501 1502 /* Verify current status */ 1503 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS && 1504 cd->audio_status != CD_AS_PLAY_PAUSED && 1505 cd->audio_status != CD_AS_PLAY_COMPLETED) { 1506 if (cd->debug) 1507 device_printf(sc->dev, 1508 "stop attempted when not playing, audio status %d\n", 1509 cd->audio_status); 1510 return EINVAL; 1511 } 1512 if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS) 1513 if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0) 1514 return EIO; 1515 cd->audio_status = CD_AS_PLAY_COMPLETED; 1516 return 0; 1517} 1518 1519static int 1520mcd_getqchan(struct mcd_softc *sc, struct mcd_qchninfo *q) 1521{ 1522 struct mcd_data *cd = &sc->data; 1523 1524 if (mcd_send(sc, MCD_CMDGETQCHN, MCD_RETRYS) < 0) 1525 return -1; 1526 if (mcd_get(sc, (char *) q, sizeof(struct mcd_qchninfo)) < 0) 1527 return -1; 1528 if (cd->debug) { 1529 device_printf(sc->dev, 1530 "getqchan control=0x%x addr_type=0x%x trk=%d ind=%d ttm=%d:%d.%d dtm=%d:%d.%d\n", 1531 q->control, q->addr_type, 1532 bcd2bin(q->trk_no), 1533 bcd2bin(q->idx_no), 1534 bcd2bin(q->trk_size_msf[0]), 1535 bcd2bin(q->trk_size_msf[1]), 1536 bcd2bin(q->trk_size_msf[2]), 1537 bcd2bin(q->hd_pos_msf[0]), 1538 bcd2bin(q->hd_pos_msf[1]), 1539 bcd2bin(q->hd_pos_msf[2])); 1540 } 1541 return 0; 1542} 1543 1544static int 1545mcd_subchan(struct mcd_softc *sc, struct ioc_read_subchannel *sch) 1546{ 1547 struct mcd_data *cd = &sc->data; 1548 struct mcd_qchninfo q; 1549 struct cd_sub_channel_info data; 1550 int lba; 1551 1552 if (cd->debug) 1553 device_printf(sc->dev, "subchan af=%d, df=%d\n", 1554 sch->address_format, 1555 sch->data_format); 1556 1557 if (sch->address_format != CD_MSF_FORMAT && 1558 sch->address_format != CD_LBA_FORMAT) 1559 return EINVAL; 1560 1561 if (sch->data_format != CD_CURRENT_POSITION && 1562 sch->data_format != CD_MEDIA_CATALOG) 1563 return EINVAL; 1564 1565 if (mcd_setmode(sc, MCD_MD_COOKED) != 0) 1566 return EIO; 1567 1568 if (mcd_getqchan(sc, &q) < 0) 1569 return EIO; 1570 1571 data.header.audio_status = cd->audio_status; 1572 data.what.position.data_format = sch->data_format; 1573 1574 switch (sch->data_format) { 1575 case CD_MEDIA_CATALOG: 1576 data.what.media_catalog.mc_valid = 1; 1577 data.what.media_catalog.mc_number[0] = '\0'; 1578 break; 1579 1580 case CD_CURRENT_POSITION: 1581 data.what.position.control = q.control; 1582 data.what.position.addr_type = q.addr_type; 1583 data.what.position.track_number = bcd2bin(q.trk_no); 1584 data.what.position.index_number = bcd2bin(q.idx_no); 1585 switch (sch->address_format) { 1586 case CD_MSF_FORMAT: 1587 data.what.position.reladdr.msf.unused = 0; 1588 data.what.position.reladdr.msf.minute = bcd2bin(q.trk_size_msf[0]); 1589 data.what.position.reladdr.msf.second = bcd2bin(q.trk_size_msf[1]); 1590 data.what.position.reladdr.msf.frame = bcd2bin(q.trk_size_msf[2]); 1591 data.what.position.absaddr.msf.unused = 0; 1592 data.what.position.absaddr.msf.minute = bcd2bin(q.hd_pos_msf[0]); 1593 data.what.position.absaddr.msf.second = bcd2bin(q.hd_pos_msf[1]); 1594 data.what.position.absaddr.msf.frame = bcd2bin(q.hd_pos_msf[2]); 1595 break; 1596 case CD_LBA_FORMAT: 1597 lba = msf2hsg(q.trk_size_msf, 1); 1598 /* 1599 * Pre-gap has index number of 0, and decreasing MSF 1600 * address. Must be converted to negative LBA, per 1601 * SCSI spec. 1602 */ 1603 if (data.what.position.index_number == 0) 1604 lba = -lba; 1605 data.what.position.reladdr.lba = htonl(lba); 1606 data.what.position.absaddr.lba = htonl(msf2hsg(q.hd_pos_msf, 0)); 1607 break; 1608 } 1609 break; 1610 } 1611 1612 return copyout(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len)); 1613} 1614 1615static int 1616mcd_playmsf(struct mcd_softc *sc, struct ioc_play_msf *p) 1617{ 1618 struct mcd_data *cd = &sc->data; 1619 struct mcd_read2 pb; 1620 1621 if (cd->debug) 1622 device_printf(sc->dev, "playmsf: from %d:%d.%d to %d:%d.%d\n", 1623 p->start_m, p->start_s, p->start_f, 1624 p->end_m, p->end_s, p->end_f); 1625 1626 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >= 1627 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) || 1628 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) > 1629 M_msf(cd->volinfo.vol_msf) * 60 * 75 + 1630 S_msf(cd->volinfo.vol_msf) * 75 + 1631 F_msf(cd->volinfo.vol_msf)) 1632 return EINVAL; 1633 1634 pb.start_msf[0] = bin2bcd(p->start_m); 1635 pb.start_msf[1] = bin2bcd(p->start_s); 1636 pb.start_msf[2] = bin2bcd(p->start_f); 1637 pb.end_msf[0] = bin2bcd(p->end_m); 1638 pb.end_msf[1] = bin2bcd(p->end_s); 1639 pb.end_msf[2] = bin2bcd(p->end_f); 1640 1641 if (mcd_setmode(sc, MCD_MD_COOKED) != 0) 1642 return EIO; 1643 1644 return mcd_play(sc, &pb); 1645} 1646 1647static int 1648mcd_playtracks(struct mcd_softc *sc, struct ioc_play_track *pt) 1649{ 1650 struct mcd_data *cd = &sc->data; 1651 struct mcd_read2 pb; 1652 int a = pt->start_track; 1653 int z = pt->end_track; 1654 int rc, i; 1655 1656 if ((rc = mcd_read_toc(sc)) != 0) 1657 return rc; 1658 1659 if (cd->debug) 1660 device_printf(sc->dev, "playtracks from %d:%d to %d:%d\n", 1661 a, pt->start_index, z, pt->end_index); 1662 1663 if ( a < bcd2bin(cd->volinfo.trk_low) 1664 || a > bcd2bin(cd->volinfo.trk_high) 1665 || a > z 1666 || z < bcd2bin(cd->volinfo.trk_low) 1667 || z > bcd2bin(cd->volinfo.trk_high)) 1668 return EINVAL; 1669 1670 for (i = 0; i < 3; i++) { 1671 pb.start_msf[i] = cd->toc[a].hd_pos_msf[i]; 1672 pb.end_msf[i] = cd->toc[z+1].hd_pos_msf[i]; 1673 } 1674 1675 if (mcd_setmode(sc, MCD_MD_COOKED) != 0) 1676 return EIO; 1677 1678 return mcd_play(sc, &pb); 1679} 1680 1681static int 1682mcd_playblocks(struct mcd_softc *sc, struct ioc_play_blocks *p) 1683{ 1684 struct mcd_data *cd = &sc->data; 1685 struct mcd_read2 pb; 1686 1687 if (cd->debug) 1688 device_printf(sc->dev, "playblocks: blkno %d length %d\n", 1689 p->blk, p->len); 1690 1691 if (p->blk > cd->disksize || p->len > cd->disksize || 1692 p->blk < 0 || p->len < 0 || 1693 (p->blk + p->len) > cd->disksize) 1694 return EINVAL; 1695 1696 hsg2msf(p->blk, pb.start_msf); 1697 hsg2msf(p->blk + p->len, pb.end_msf); 1698 1699 if (mcd_setmode(sc, MCD_MD_COOKED) != 0) 1700 return EIO; 1701 1702 return mcd_play(sc, &pb); 1703} 1704 1705static int 1706mcd_play(struct mcd_softc *sc, struct mcd_read2 *pb) 1707{ 1708 struct mcd_data *cd = &sc->data; 1709 int retry, st = -1, status; 1710 1711 cd->lastpb = *pb; 1712 for(retry=0; retry<MCD_RETRYS; retry++) { 1713 1714 disable_intr(); 1715 MCD_WRITE(sc, MCD_REG_COMMAND, MCD_CMDSINGLESPEEDREAD); 1716 MCD_WRITE(sc, MCD_REG_COMMAND, pb->start_msf[0]); 1717 MCD_WRITE(sc, MCD_REG_COMMAND, pb->start_msf[1]); 1718 MCD_WRITE(sc, MCD_REG_COMMAND, pb->start_msf[2]); 1719 MCD_WRITE(sc, MCD_REG_COMMAND, pb->end_msf[0]); 1720 MCD_WRITE(sc, MCD_REG_COMMAND, pb->end_msf[1]); 1721 MCD_WRITE(sc, MCD_REG_COMMAND, pb->end_msf[2]); 1722 enable_intr(); 1723 1724 status=mcd_getstat(sc, 0); 1725 if (status == -1) 1726 continue; 1727 else if (status != -2) 1728 st = 0; 1729 break; 1730 } 1731 1732 if (status == -2) { 1733 device_printf(sc->dev, "media changed\n"); 1734 return ENXIO; 1735 } 1736 if (cd->debug) 1737 device_printf(sc->dev, 1738 "mcd_play retry=%d, status=0x%02x\n", retry, status); 1739 if (st < 0) 1740 return ENXIO; 1741 cd->audio_status = CD_AS_PLAY_IN_PROGRESS; 1742 return 0; 1743} 1744 1745static int 1746mcd_pause(struct mcd_softc *sc) 1747{ 1748 struct mcd_data *cd = &sc->data; 1749 struct mcd_qchninfo q; 1750 int rc; 1751 1752 /* Verify current status */ 1753 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS && 1754 cd->audio_status != CD_AS_PLAY_PAUSED) { 1755 if (cd->debug) 1756 device_printf(sc->dev, 1757 "pause attempted when not playing, audio status %d\n", 1758 cd->audio_status); 1759 return EINVAL; 1760 } 1761 1762 /* Get the current position */ 1763 if (mcd_getqchan(sc, &q) < 0) 1764 return EIO; 1765 1766 /* Copy it into lastpb */ 1767 cd->lastpb.start_msf[0] = q.hd_pos_msf[0]; 1768 cd->lastpb.start_msf[1] = q.hd_pos_msf[1]; 1769 cd->lastpb.start_msf[2] = q.hd_pos_msf[2]; 1770 1771 /* Stop playing */ 1772 if ((rc=mcd_stop(sc)) != 0) 1773 return rc; 1774 1775 /* Set the proper status and exit */ 1776 cd->audio_status = CD_AS_PLAY_PAUSED; 1777 return 0; 1778} 1779 1780static int 1781mcd_resume(struct mcd_softc *sc) 1782{ 1783 struct mcd_data *cd = &sc->data; 1784 1785 if (cd->audio_status != CD_AS_PLAY_PAUSED) 1786 return EINVAL; 1787 return mcd_play(sc, &cd->lastpb); 1788} 1789#endif /* GEOM */
|