1 /* $NetBSD: mcd.c,v 1.116 2016/07/14 10:19:06 msaitoh Exp $ */ 2 3 /* 4 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Charles M. Hannum. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * Copyright 1993 by Holger Veit (data part) 21 * Copyright 1993 by Brian Moore (audio part) 22 * All rights reserved. 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 27 * 1. Redistributions of source code must retain the above copyright 28 * notice, this list of conditions and the following disclaimer. 29 * 2. Redistributions in binary form must reproduce the above copyright 30 * notice, this list of conditions and the following disclaimer in the 31 * documentation and/or other materials provided with the distribution. 32 * 3. All advertising materials mentioning features or use of this software 33 * must display the following acknowledgement: 34 * This software was developed by Holger Veit and Brian Moore 35 * for use with "386BSD" and similar operating systems. 36 * "Similar operating systems" includes mainly non-profit oriented 37 * systems for research and education, including but not restricted to 38 * "NetBSD", "FreeBSD", "Mach" (by CMU). 39 * 4. Neither the name of the developer(s) nor the name "386BSD" 40 * may be used to endorse or promote products derived from this 41 * software without specific prior written permission. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY 44 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE 47 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 48 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 49 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 51 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 54 */ 55 56 /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/ 57 58 #include <sys/cdefs.h> 59 __KERNEL_RCSID(0, "$NetBSD: mcd.c,v 1.116 2016/07/14 10:19:06 msaitoh Exp $"); 60 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/callout.h> 64 #include <sys/kernel.h> 65 #include <sys/proc.h> 66 #include <sys/conf.h> 67 #include <sys/file.h> 68 #include <sys/buf.h> 69 #include <sys/bufq.h> 70 #include <sys/stat.h> 71 #include <sys/uio.h> 72 #include <sys/ioctl.h> 73 #include <sys/cdio.h> 74 #include <sys/errno.h> 75 #include <sys/disklabel.h> 76 #include <sys/device.h> 77 #include <sys/disk.h> 78 #include <sys/mutex.h> 79 #include <sys/cpu.h> 80 #include <sys/intr.h> 81 #include <sys/bus.h> 82 83 #include <dev/isa/isavar.h> 84 #include <dev/isa/mcdreg.h> 85 86 #ifndef MCDDEBUG 87 #define MCD_TRACE(fmt,...) 88 #else 89 #define MCD_TRACE(fmt,...) {if (sc->debug) {printf("%s: st=%02x: ", device_xname(sc->sc_dev), sc->status); printf(fmt,__VA_ARGS__);}} 90 #endif 91 92 #define MCDPART(dev) DISKPART(dev) 93 #define MCDUNIT(dev) DISKUNIT(dev) 94 95 /* toc */ 96 #define MCD_MAXTOCS 104 /* from the Linux driver */ 97 98 /* control promiscuous match */ 99 #include "opt_mcd_promisc.h" 100 101 #ifdef MCD_PROMISC 102 int mcd_promisc = 1; 103 #else 104 int mcd_promisc = 0; 105 #endif 106 107 struct mcd_mbx { 108 int retry, count; 109 struct buf *bp; 110 daddr_t blkno; 111 int nblk; 112 int sz; 113 u_long skip; 114 int state; 115 #define MCD_S_IDLE 0 116 #define MCD_S_BEGIN 1 117 #define MCD_S_WAITMODE 2 118 #define MCD_S_WAITREAD 3 119 int mode; 120 }; 121 122 struct mcd_softc { 123 device_t sc_dev; 124 struct disk sc_dk; 125 kmutex_t sc_lock; 126 void *sc_ih; 127 128 callout_t sc_pintr_ch; 129 130 bus_space_tag_t sc_iot; 131 bus_space_handle_t sc_ioh; 132 133 int irq, drq; 134 135 const char *type; 136 int flags; 137 #define MCDF_WLABEL 0x04 /* label is writable */ 138 #define MCDF_LABELLING 0x08 /* writing label */ 139 #define MCDF_LOADED 0x10 /* parameters loaded */ 140 short status; 141 short audio_status; 142 int blksize; 143 u_long disksize; 144 struct mcd_volinfo volinfo; 145 union mcd_qchninfo toc[MCD_MAXTOCS]; 146 struct mcd_command lastpb; 147 struct mcd_mbx mbx; 148 int lastmode; 149 #define MCD_MD_UNKNOWN -1 150 int lastupc; 151 #define MCD_UPC_UNKNOWN -1 152 struct bufq_state *buf_queue; 153 int active; 154 u_char readcmd; 155 u_char debug; 156 u_char probe; 157 }; 158 159 static int bcd2bin(bcd_t); 160 static bcd_t bin2bcd(int); 161 static void hsg2msf(int, bcd_t *); 162 static daddr_t msf2hsg(bcd_t *, int); 163 164 int mcd_playtracks(struct mcd_softc *, struct ioc_play_track *); 165 int mcd_playmsf(struct mcd_softc *, struct ioc_play_msf *); 166 int mcd_playblocks(struct mcd_softc *, struct ioc_play_blocks *); 167 int mcd_stop(struct mcd_softc *); 168 int mcd_eject(struct mcd_softc *); 169 int mcd_read_subchannel(struct mcd_softc *, struct ioc_read_subchannel *, 170 struct cd_sub_channel_info *); 171 int mcd_pause(struct mcd_softc *); 172 int mcd_resume(struct mcd_softc *); 173 int mcd_toc_header(struct mcd_softc *, struct ioc_toc_header *); 174 int mcd_toc_entries(struct mcd_softc *, struct ioc_read_toc_entry *, 175 struct cd_toc_entry *, int *); 176 177 int mcd_getreply(struct mcd_softc *); 178 int mcd_getstat(struct mcd_softc *); 179 int mcd_getresult(struct mcd_softc *, struct mcd_result *); 180 void mcd_setflags(struct mcd_softc *); 181 int mcd_get(struct mcd_softc *, char *, int); 182 int mcd_send(struct mcd_softc *, struct mcd_mbox *, int); 183 int mcdintr(void *); 184 void mcd_soft_reset(struct mcd_softc *); 185 int mcd_hard_reset(struct mcd_softc *); 186 int mcd_setmode(struct mcd_softc *, int); 187 int mcd_setupc(struct mcd_softc *, int); 188 int mcd_read_toc(struct mcd_softc *); 189 int mcd_getqchan(struct mcd_softc *, union mcd_qchninfo *, int); 190 int mcd_setlock(struct mcd_softc *, int); 191 192 int mcd_find(bus_space_tag_t, bus_space_handle_t, struct mcd_softc *); 193 int mcdprobe(device_t, cfdata_t, void *); 194 void mcdattach(device_t, device_t, void *); 195 196 CFATTACH_DECL_NEW(mcd, sizeof(struct mcd_softc), 197 mcdprobe, mcdattach, NULL, NULL); 198 199 extern struct cfdriver mcd_cd; 200 201 dev_type_open(mcdopen); 202 dev_type_close(mcdclose); 203 dev_type_read(mcdread); 204 dev_type_write(mcdwrite); 205 dev_type_ioctl(mcdioctl); 206 dev_type_strategy(mcdstrategy); 207 dev_type_dump(mcddump); 208 dev_type_size(mcdsize); 209 210 const struct bdevsw mcd_bdevsw = { 211 .d_open = mcdopen, 212 .d_close = mcdclose, 213 .d_strategy = mcdstrategy, 214 .d_ioctl = mcdioctl, 215 .d_dump = mcddump, 216 .d_psize = mcdsize, 217 .d_discard = nodiscard, 218 .d_flag = D_DISK 219 }; 220 221 const struct cdevsw mcd_cdevsw = { 222 .d_open = mcdopen, 223 .d_close = mcdclose, 224 .d_read = mcdread, 225 .d_write = mcdwrite, 226 .d_ioctl = mcdioctl, 227 .d_stop = nostop, 228 .d_tty = notty, 229 .d_poll = nopoll, 230 .d_mmap = nommap, 231 .d_kqfilter = nokqfilter, 232 .d_discard = nodiscard, 233 .d_flag = D_DISK 234 }; 235 236 void mcdgetdefaultlabel(struct mcd_softc *, struct disklabel *); 237 void mcdgetdisklabel(struct mcd_softc *); 238 int mcd_get_parms(struct mcd_softc *); 239 void mcdstart(struct mcd_softc *); 240 void mcd_pseudointr(void *); 241 242 struct dkdriver mcddkdriver = { 243 .d_strategy = mcdstrategy 244 }; 245 246 #define MCD_RETRIES 3 247 #define MCD_RDRETRIES 3 248 249 /* several delays */ 250 #define RDELAY_WAITMODE 300 251 #define RDELAY_WAITREAD 800 252 253 #define DELAY_GRANULARITY 25 /* 25us */ 254 #define DELAY_GETREPLY 100000 /* 100000 * 25us */ 255 256 void 257 mcdattach(device_t parent, device_t self, void *aux) 258 { 259 struct mcd_softc *sc = device_private(self); 260 struct isa_attach_args *ia = aux; 261 bus_space_tag_t iot = ia->ia_iot; 262 bus_space_handle_t ioh; 263 struct mcd_mbox mbx; 264 265 aprint_naive("\n"); 266 267 /* Map i/o space */ 268 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh)) { 269 aprint_error(": can't map i/o space\n"); 270 return; 271 } 272 273 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 274 275 sc->sc_iot = iot; 276 sc->sc_ioh = ioh; 277 278 sc->probe = 0; 279 sc->debug = 0; 280 281 if (!mcd_find(iot, ioh, sc)) { 282 aprint_error(": mcd_find failed\n"); 283 return; 284 } 285 286 bufq_alloc(&sc->buf_queue, "disksort", BUFQ_SORT_RAWBLOCK); 287 callout_init(&sc->sc_pintr_ch, 0); 288 289 /* 290 * Initialize and attach the disk structure. 291 */ 292 disk_init(&sc->sc_dk, device_xname(sc->sc_dev), &mcddkdriver); 293 disk_attach(&sc->sc_dk); 294 295 aprint_normal(": model %s\n", sc->type != 0 ? sc->type : "unknown"); 296 297 (void) mcd_setlock(sc, MCD_LK_UNLOCK); 298 299 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE; 300 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1; 301 mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE; 302 mbx.cmd.data.config.data1 = 0x01; 303 mbx.res.length = 0; 304 (void) mcd_send(sc, &mbx, 0); 305 306 mcd_soft_reset(sc); 307 308 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq, 309 IST_EDGE, IPL_BIO, mcdintr, sc); 310 } 311 312 int 313 mcdopen(dev_t dev, int flag, int fmt, struct lwp *l) 314 { 315 int error, part; 316 struct mcd_softc *sc; 317 318 sc = device_lookup_private(&mcd_cd, MCDUNIT(dev)); 319 if (sc == NULL) 320 return ENXIO; 321 322 mutex_enter(&sc->sc_lock); 323 324 if (sc->sc_dk.dk_openmask != 0) { 325 /* 326 * If any partition is open, but the disk has been invalidated, 327 * disallow further opens. 328 */ 329 if ((sc->flags & MCDF_LOADED) == 0) { 330 error = EIO; 331 goto bad3; 332 } 333 } else { 334 /* 335 * Lock the drawer. This will also notice any pending disk 336 * change or door open indicator and clear the MCDF_LOADED bit 337 * if necessary. 338 */ 339 (void) mcd_setlock(sc, MCD_LK_LOCK); 340 341 if ((sc->flags & MCDF_LOADED) == 0) { 342 /* Partially reset the state. */ 343 sc->lastmode = MCD_MD_UNKNOWN; 344 sc->lastupc = MCD_UPC_UNKNOWN; 345 346 sc->flags |= MCDF_LOADED; 347 348 /* Set the mode, causing the disk to spin up. */ 349 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 350 goto bad2; 351 352 /* Load the physical device parameters. */ 353 if (mcd_get_parms(sc) != 0) { 354 error = ENXIO; 355 goto bad2; 356 } 357 358 /* Read the table of contents. */ 359 if ((error = mcd_read_toc(sc)) != 0) 360 goto bad2; 361 362 /* Fabricate a disk label. */ 363 mcdgetdisklabel(sc); 364 } 365 } 366 367 part = MCDPART(dev); 368 369 MCD_TRACE("open: partition=%d disksize=%ld blksize=%d\n", part, 370 sc->disksize, sc->blksize); 371 372 /* Check that the partition exists. */ 373 if (part != RAW_PART && 374 (part >= sc->sc_dk.dk_label->d_npartitions || 375 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 376 error = ENXIO; 377 goto bad; 378 } 379 380 /* Insure only one open at a time. */ 381 switch (fmt) { 382 case S_IFCHR: 383 sc->sc_dk.dk_copenmask |= (1 << part); 384 break; 385 case S_IFBLK: 386 sc->sc_dk.dk_bopenmask |= (1 << part); 387 break; 388 } 389 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 390 391 mutex_exit(&sc->sc_lock); 392 return 0; 393 394 bad2: 395 sc->flags &= ~MCDF_LOADED; 396 397 bad: 398 if (sc->sc_dk.dk_openmask == 0) { 399 #if 0 400 (void) mcd_setmode(sc, MCD_MD_SLEEP); 401 #endif 402 (void) mcd_setlock(sc, MCD_LK_UNLOCK); 403 } 404 405 bad3: 406 mutex_exit(&sc->sc_lock); 407 return error; 408 } 409 410 int 411 mcdclose(dev_t dev, int flag, int fmt, struct lwp *l) 412 { 413 struct mcd_softc *sc = device_lookup_private(&mcd_cd, MCDUNIT(dev)); 414 int part = MCDPART(dev); 415 416 MCD_TRACE("close: partition=%d\n", part); 417 418 mutex_enter(&sc->sc_lock); 419 420 switch (fmt) { 421 case S_IFCHR: 422 sc->sc_dk.dk_copenmask &= ~(1 << part); 423 break; 424 case S_IFBLK: 425 sc->sc_dk.dk_bopenmask &= ~(1 << part); 426 break; 427 } 428 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 429 430 if (sc->sc_dk.dk_openmask == 0) { 431 /* XXXX Must wait for I/O to complete! */ 432 433 #if 0 434 (void) mcd_setmode(sc, MCD_MD_SLEEP); 435 #endif 436 (void) mcd_setlock(sc, MCD_LK_UNLOCK); 437 } 438 439 mutex_exit(&sc->sc_lock); 440 return 0; 441 } 442 443 void 444 mcdstrategy(struct buf *bp) 445 { 446 struct mcd_softc *sc; 447 struct disklabel *lp; 448 daddr_t blkno; 449 int s; 450 451 sc = device_lookup_private(&mcd_cd, MCDUNIT(bp->b_dev)); 452 lp = sc->sc_dk.dk_label; 453 454 /* Test validity. */ 455 MCD_TRACE("strategy: buf=0x%p blkno=%d bcount=%d\n", bp, 456 (int) bp->b_blkno, bp->b_bcount); 457 if (bp->b_blkno < 0 || 458 (bp->b_bcount % sc->blksize) != 0) { 459 printf("%s: strategy: blkno = %" PRId64 " bcount = %d\n", 460 device_xname(sc->sc_dev), bp->b_blkno, bp->b_bcount); 461 bp->b_error = EINVAL; 462 goto done; 463 } 464 465 /* If device invalidated (e.g. media change, door open), error. */ 466 if ((sc->flags & MCDF_LOADED) == 0) { 467 MCD_TRACE("strategy: drive not valid%s", "\n"); 468 bp->b_error = EIO; 469 goto done; 470 } 471 472 /* No data to read. */ 473 if (bp->b_bcount == 0) 474 goto done; 475 476 /* 477 * Do bounds checking, adjust transfer. if error, process. 478 * If end of partition, just return. 479 */ 480 if (MCDPART(bp->b_dev) != RAW_PART && 481 bounds_check_with_label(&sc->sc_dk, bp, 482 (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0) 483 goto done; 484 485 /* 486 * Now convert the block number to absolute and put it in 487 * terms of the device's logical block size. 488 */ 489 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 490 if (MCDPART(bp->b_dev) != RAW_PART) 491 blkno += lp->d_partitions[MCDPART(bp->b_dev)].p_offset; 492 493 bp->b_rawblkno = blkno; 494 495 /* Queue it. */ 496 s = splbio(); 497 bufq_put(sc->buf_queue, bp); 498 splx(s); 499 if (!sc->active) 500 mcdstart(sc); 501 return; 502 503 done: 504 bp->b_resid = bp->b_bcount; 505 biodone(bp); 506 } 507 508 void 509 mcdstart(struct mcd_softc *sc) 510 { 511 struct buf *bp; 512 int s; 513 514 loop: 515 s = splbio(); 516 517 if ((bp = bufq_get(sc->buf_queue)) == NULL) { 518 /* Nothing to do. */ 519 sc->active = 0; 520 splx(s); 521 return; 522 } 523 524 /* Block found to process. */ 525 MCD_TRACE("start: found block bp=0x%p\n", bp); 526 splx(s); 527 528 /* Changed media? */ 529 if ((sc->flags & MCDF_LOADED) == 0) { 530 MCD_TRACE("start: drive not valid%s", "\n"); 531 bp->b_error = EIO; 532 biodone(bp); 533 goto loop; 534 } 535 536 sc->active = 1; 537 538 /* Instrumentation. */ 539 s = splbio(); 540 disk_busy(&sc->sc_dk); 541 splx(s); 542 543 sc->mbx.retry = MCD_RDRETRIES; 544 sc->mbx.bp = bp; 545 sc->mbx.blkno = bp->b_rawblkno; 546 sc->mbx.nblk = bp->b_bcount / sc->blksize; 547 sc->mbx.sz = sc->blksize; 548 sc->mbx.skip = 0; 549 sc->mbx.state = MCD_S_BEGIN; 550 sc->mbx.mode = MCD_MD_COOKED; 551 552 s = splbio(); 553 (void) mcdintr(sc); 554 splx(s); 555 } 556 557 int 558 mcdread(dev_t dev, struct uio *uio, int flags) 559 { 560 561 return (physio(mcdstrategy, NULL, dev, B_READ, minphys, uio)); 562 } 563 564 int 565 mcdwrite(dev_t dev, struct uio *uio, int flags) 566 { 567 568 return (physio(mcdstrategy, NULL, dev, B_WRITE, minphys, uio)); 569 } 570 571 int 572 mcdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 573 { 574 struct mcd_softc *sc = device_lookup_private(&mcd_cd, MCDUNIT(dev)); 575 int error; 576 int part; 577 #ifdef __HAVE_OLD_DISKLABEL 578 struct disklabel newlabel; 579 #endif 580 581 MCD_TRACE("ioctl: cmd=0x%lx\n", cmd); 582 583 if ((sc->flags & MCDF_LOADED) == 0) 584 return EIO; 585 586 error = disk_ioctl(&sc->sc_dk, dev, cmd, addr, flag, l); 587 if (error != EPASSTHROUGH) 588 return error; 589 590 part = MCDPART(dev); 591 switch (cmd) { 592 case DIOCWDINFO: 593 case DIOCSDINFO: 594 #ifdef __HAVE_OLD_DISKLABEL 595 case ODIOCWDINFO: 596 case ODIOCSDINFO: 597 #endif 598 { 599 struct disklabel *lp; 600 601 if ((flag & FWRITE) == 0) 602 return EBADF; 603 604 #ifdef __HAVE_OLD_DISKLABEL 605 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 606 memset(&newlabel, 0, sizeof newlabel); 607 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 608 lp = &newlabel; 609 } else 610 #endif 611 lp = addr; 612 613 mutex_enter(&sc->sc_lock); 614 sc->flags |= MCDF_LABELLING; 615 616 error = setdisklabel(sc->sc_dk.dk_label, 617 lp, /*sc->sc_dk.dk_openmask : */0, 618 sc->sc_dk.dk_cpulabel); 619 if (error == 0) { 620 } 621 622 sc->flags &= ~MCDF_LABELLING; 623 mutex_exit(&sc->sc_lock); 624 return error; 625 } 626 627 case DIOCWLABEL: 628 return EBADF; 629 630 case DIOCGDEFLABEL: 631 mcdgetdefaultlabel(sc, addr); 632 return 0; 633 634 #ifdef __HAVE_OLD_DISKLABEL 635 case ODIOCGDEFLABEL: 636 mcdgetdefaultlabel(sc, &newlabel); 637 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 638 return ENOTTY; 639 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 640 return 0; 641 #endif 642 643 case CDIOCPLAYTRACKS: 644 return mcd_playtracks(sc, addr); 645 case CDIOCPLAYMSF: 646 return mcd_playmsf(sc, addr); 647 case CDIOCPLAYBLOCKS: 648 return mcd_playblocks(sc, addr); 649 case CDIOCREADSUBCHANNEL: { 650 struct cd_sub_channel_info info; 651 error = mcd_read_subchannel(sc, addr, &info); 652 if (error != 0) { 653 struct ioc_read_subchannel *ch = addr; 654 error = copyout(&info, ch->data, ch->data_len); 655 } 656 return error; 657 } 658 case CDIOCREADSUBCHANNEL_BUF: 659 return mcd_read_subchannel(sc, addr, 660 &((struct ioc_read_subchannel_buf *)addr)->info); 661 case CDIOREADTOCHEADER: 662 return mcd_toc_header(sc, addr); 663 case CDIOREADTOCENTRYS: { 664 struct cd_toc_entry entries[MCD_MAXTOCS]; 665 struct ioc_read_toc_entry *te = addr; 666 int count; 667 if (te->data_len > sizeof entries) 668 return EINVAL; 669 error = mcd_toc_entries(sc, te, entries, &count); 670 if (error == 0) 671 /* Copy the data back. */ 672 error = copyout(entries, te->data, min(te->data_len, 673 count * sizeof(struct cd_toc_entry))); 674 return error; 675 } 676 case CDIOREADTOCENTRIES_BUF: { 677 struct ioc_read_toc_entry_buf *te = addr; 678 int count; 679 if (te->req.data_len > sizeof te->entry) 680 return EINVAL; 681 return mcd_toc_entries(sc, &te->req, te->entry, &count); 682 } 683 case CDIOCSETPATCH: 684 case CDIOCGETVOL: 685 case CDIOCSETVOL: 686 case CDIOCSETMONO: 687 case CDIOCSETSTEREO: 688 case CDIOCSETMUTE: 689 case CDIOCSETLEFT: 690 case CDIOCSETRIGHT: 691 return EINVAL; 692 case CDIOCRESUME: 693 return mcd_resume(sc); 694 case CDIOCPAUSE: 695 return mcd_pause(sc); 696 case CDIOCSTART: 697 return EINVAL; 698 case CDIOCSTOP: 699 return mcd_stop(sc); 700 case DIOCEJECT: 701 if (*(int *)addr == 0) { 702 /* 703 * Don't force eject: check that we are the only 704 * partition open. If so, unlock it. 705 */ 706 if ((sc->sc_dk.dk_openmask & ~(1 << part)) == 0 && 707 sc->sc_dk.dk_bopenmask + sc->sc_dk.dk_copenmask == 708 sc->sc_dk.dk_openmask) { 709 error = mcd_setlock(sc, MCD_LK_UNLOCK); 710 if (error) 711 return (error); 712 } else { 713 return (EBUSY); 714 } 715 } 716 /* FALLTHROUGH */ 717 case CDIOCEJECT: /* FALLTHROUGH */ 718 case ODIOCEJECT: 719 return mcd_eject(sc); 720 case CDIOCALLOW: 721 return mcd_setlock(sc, MCD_LK_UNLOCK); 722 case CDIOCPREVENT: 723 return mcd_setlock(sc, MCD_LK_LOCK); 724 case DIOCLOCK: 725 return mcd_setlock(sc, 726 (*(int *)addr) ? MCD_LK_LOCK : MCD_LK_UNLOCK); 727 case CDIOCSETDEBUG: 728 sc->debug = 1; 729 return 0; 730 case CDIOCCLRDEBUG: 731 sc->debug = 0; 732 return 0; 733 case CDIOCRESET: 734 return mcd_hard_reset(sc); 735 736 default: 737 return ENOTTY; 738 } 739 740 #ifdef DIAGNOSTIC 741 panic("mcdioctl: impossible"); 742 #endif 743 } 744 745 void 746 mcdgetdefaultlabel(struct mcd_softc *sc, struct disklabel *lp) 747 { 748 749 memset(lp, 0, sizeof(struct disklabel)); 750 751 lp->d_secsize = sc->blksize; 752 lp->d_ntracks = 1; 753 lp->d_nsectors = 100; 754 lp->d_ncylinders = (sc->disksize / 100) + 1; 755 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 756 757 strncpy(lp->d_typename, "Mitsumi CD-ROM", 16); 758 lp->d_type = 0; /* XXX */ 759 strncpy(lp->d_packname, "fictitious", 16); 760 lp->d_secperunit = sc->disksize; 761 lp->d_rpm = 300; 762 lp->d_interleave = 1; 763 lp->d_flags = D_REMOVABLE; 764 765 lp->d_partitions[0].p_offset = 0; 766 lp->d_partitions[0].p_size = 767 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 768 lp->d_partitions[0].p_fstype = FS_ISO9660; 769 lp->d_partitions[RAW_PART].p_offset = 0; 770 lp->d_partitions[RAW_PART].p_size = 771 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 772 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 773 lp->d_npartitions = RAW_PART + 1; 774 775 lp->d_magic = DISKMAGIC; 776 lp->d_magic2 = DISKMAGIC; 777 lp->d_checksum = dkcksum(lp); 778 } 779 780 /* 781 * This could have been taken from scsi/cd.c, but it is not clear 782 * whether the scsi cd driver is linked in. 783 */ 784 void 785 mcdgetdisklabel(struct mcd_softc *sc) 786 { 787 struct disklabel *lp = sc->sc_dk.dk_label; 788 789 memset(sc->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 790 791 mcdgetdefaultlabel(sc, lp); 792 } 793 794 int 795 mcd_get_parms(struct mcd_softc *sc) 796 { 797 struct mcd_mbox mbx; 798 daddr_t size; 799 int error; 800 801 /* Send volume info command. */ 802 mbx.cmd.opcode = MCD_CMDGETVOLINFO; 803 mbx.cmd.length = 0; 804 mbx.res.length = sizeof(mbx.res.data.volinfo); 805 if ((error = mcd_send(sc, &mbx, 1)) != 0) 806 return error; 807 808 if (mbx.res.data.volinfo.trk_low == 0x00 && 809 mbx.res.data.volinfo.trk_high == 0x00) 810 return EINVAL; 811 812 /* Volinfo is OK. */ 813 sc->volinfo = mbx.res.data.volinfo; 814 sc->blksize = MCD_BLKSIZE_COOKED; 815 size = msf2hsg(sc->volinfo.vol_msf, 0); 816 sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE); 817 return 0; 818 } 819 820 int 821 mcdsize(dev_t dev) 822 { 823 824 /* CD-ROMs are read-only. */ 825 return -1; 826 } 827 828 int 829 mcddump(dev_t dev, daddr_t blkno, void *va, 830 size_t size) 831 { 832 833 /* Not implemented. */ 834 return ENXIO; 835 } 836 837 /* 838 * Find the board and fill in the softc. 839 */ 840 int 841 mcd_find(bus_space_tag_t iot, bus_space_handle_t ioh, struct mcd_softc *sc) 842 { 843 int i; 844 struct mcd_mbox mbx; 845 846 sc->sc_iot = iot; 847 sc->sc_ioh = ioh; 848 849 /* Send a reset. */ 850 bus_space_write_1(iot, ioh, MCD_RESET, 0); 851 delay(1000000); 852 /* Get any pending status and throw away. */ 853 for (i = 10; i; i--) 854 bus_space_read_1(iot, ioh, MCD_STATUS); 855 delay(1000); 856 857 /* Send get status command. */ 858 mbx.cmd.opcode = MCD_CMDGETSTAT; 859 mbx.cmd.length = 0; 860 mbx.res.length = 0; 861 if (mcd_send(sc, &mbx, 0) != 0) 862 return 0; 863 864 /* Get info about the drive. */ 865 mbx.cmd.opcode = MCD_CMDCONTINFO; 866 mbx.cmd.length = 0; 867 mbx.res.length = sizeof(mbx.res.data.continfo); 868 if (mcd_send(sc, &mbx, 0) != 0) 869 return 0; 870 871 /* 872 * The following is code which is not guaranteed to work for all 873 * drives, because the meaning of the expected 'M' is not clear 874 * (M_itsumi is an obvious assumption, but I don't trust that). 875 * Also, the original hack had a bogus condition that always 876 * returned true. 877 * 878 * Note: Which models support interrupts? >=LU005S? 879 */ 880 sc->readcmd = MCD_CMDREADSINGLESPEED; 881 switch (mbx.res.data.continfo.code) { 882 case 'M': 883 if (mbx.res.data.continfo.version <= 2) 884 sc->type = "LU002S"; 885 else if (mbx.res.data.continfo.version <= 5) 886 sc->type = "LU005S"; 887 else 888 sc->type = "LU006S"; 889 break; 890 case 'F': 891 sc->type = "FX001"; 892 break; 893 case 'D': 894 sc->type = "FX001D"; 895 sc->readcmd = MCD_CMDREADDOUBLESPEED; 896 break; 897 default: 898 /* 899 * mcd_send() says the response looked OK but the 900 * drive type is unknown. If mcd_promisc, match anyway. 901 */ 902 if (mcd_promisc != 0) 903 return 0; 904 905 #ifdef MCDDEBUG 906 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n", 907 device_xname(sc->sc_dev), 908 mbx.res.data.continfo.code, mbx.res.data.continfo.version); 909 #endif 910 sc->type = 0; 911 break; 912 } 913 914 return 1; 915 916 } 917 918 int 919 mcdprobe(device_t parent, cfdata_t match, void *aux) 920 { 921 struct isa_attach_args *ia = aux; 922 struct mcd_softc sc; 923 bus_space_tag_t iot = ia->ia_iot; 924 bus_space_handle_t ioh; 925 int rv; 926 927 if (ia->ia_nio < 1) 928 return (0); 929 if (ia->ia_nirq < 1) 930 return (0); 931 932 if (ISA_DIRECT_CONFIG(ia)) 933 return (0); 934 935 /* Disallow wildcarded i/o address. */ 936 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT) 937 return (0); 938 if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) 939 return (0); 940 941 /* Map i/o space */ 942 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh)) 943 return 0; 944 945 sc.debug = 0; 946 sc.probe = 1; 947 948 rv = mcd_find(iot, ioh, &sc); 949 950 bus_space_unmap(iot, ioh, MCD_NPORT); 951 952 if (rv) { 953 ia->ia_nio = 1; 954 ia->ia_io[0].ir_size = MCD_NPORT; 955 956 ia->ia_nirq = 1; 957 958 ia->ia_niomem = 0; 959 ia->ia_ndrq = 0; 960 } 961 962 return (rv); 963 } 964 965 int 966 mcd_getreply(struct mcd_softc *sc) 967 { 968 bus_space_tag_t iot = sc->sc_iot; 969 bus_space_handle_t ioh = sc->sc_ioh; 970 int i; 971 972 /* Wait until xfer port senses data ready. */ 973 for (i = DELAY_GETREPLY; i; i--) { 974 if ((bus_space_read_1(iot, ioh, MCD_XFER) & 975 MCD_XF_STATUSUNAVAIL) == 0) 976 break; 977 delay(DELAY_GRANULARITY); 978 } 979 if (!i) 980 return -1; 981 982 /* Get the data. */ 983 return bus_space_read_1(iot, ioh, MCD_STATUS); 984 } 985 986 int 987 mcd_getstat(struct mcd_softc *sc) 988 { 989 struct mcd_mbox mbx; 990 991 mbx.cmd.opcode = MCD_CMDGETSTAT; 992 mbx.cmd.length = 0; 993 mbx.res.length = 0; 994 return mcd_send(sc, &mbx, 1); 995 } 996 997 int 998 mcd_getresult(struct mcd_softc *sc, struct mcd_result *res) 999 { 1000 int i, x; 1001 1002 if (sc->debug) 1003 printf("%s: mcd_getresult: %d", device_xname(sc->sc_dev), 1004 res->length); 1005 1006 if ((x = mcd_getreply(sc)) < 0) { 1007 if (sc->debug) 1008 printf(" timeout\n"); 1009 else if (!sc->probe) 1010 printf("%s: timeout in getresult\n", 1011 device_xname(sc->sc_dev)); 1012 return EIO; 1013 } 1014 if (sc->debug) 1015 printf(" %02x", (u_int)x); 1016 sc->status = x; 1017 mcd_setflags(sc); 1018 1019 if ((sc->status & MCD_ST_CMDCHECK) != 0) 1020 return EINVAL; 1021 1022 for (i = 0; i < res->length; i++) { 1023 if ((x = mcd_getreply(sc)) < 0) { 1024 if (sc->debug) 1025 printf(" timeout\n"); 1026 else 1027 printf("%s: timeout in getresult\n", 1028 device_xname(sc->sc_dev)); 1029 return EIO; 1030 } 1031 if (sc->debug) 1032 printf(" %02x", (u_int)x); 1033 res->data.raw.data[i] = x; 1034 } 1035 1036 if (sc->debug) 1037 printf(" succeeded\n"); 1038 1039 #ifdef MCDDEBUG 1040 delay(10); 1041 while ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_XFER) & 1042 MCD_XF_STATUSUNAVAIL) == 0) { 1043 x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_STATUS); 1044 printf("%s: got extra byte %02x during getstatus\n", 1045 device_xname(sc->sc_dev), (u_int)x); 1046 delay(10); 1047 } 1048 #endif 1049 1050 return 0; 1051 } 1052 1053 void 1054 mcd_setflags(struct mcd_softc *sc) 1055 { 1056 1057 /* Check flags. */ 1058 if ((sc->flags & MCDF_LOADED) != 0 && 1059 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) != 1060 MCD_ST_DSKIN) { 1061 if ((sc->status & MCD_ST_DOOROPEN) != 0) 1062 printf("%s: door open\n", device_xname(sc->sc_dev)); 1063 else if ((sc->status & MCD_ST_DSKIN) == 0) 1064 printf("%s: no disk present\n", device_xname(sc->sc_dev)); 1065 else if ((sc->status & MCD_ST_DSKCHNG) != 0) 1066 printf("%s: media change\n", device_xname(sc->sc_dev)); 1067 sc->flags &= ~MCDF_LOADED; 1068 } 1069 1070 if ((sc->status & MCD_ST_AUDIOBSY) != 0) 1071 sc->audio_status = CD_AS_PLAY_IN_PROGRESS; 1072 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS || 1073 sc->audio_status == CD_AS_AUDIO_INVALID) 1074 sc->audio_status = CD_AS_PLAY_COMPLETED; 1075 } 1076 1077 int 1078 mcd_send(struct mcd_softc *sc, struct mcd_mbox *mbx, int diskin) 1079 { 1080 int retry, i, error; 1081 bus_space_tag_t iot = sc->sc_iot; 1082 bus_space_handle_t ioh = sc->sc_ioh; 1083 1084 if (sc->debug) { 1085 printf("%s: mcd_send: %d %02x", device_xname(sc->sc_dev), 1086 mbx->cmd.length, (u_int)mbx->cmd.opcode); 1087 for (i = 0; i < mbx->cmd.length; i++) 1088 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]); 1089 printf("\n"); 1090 } 1091 1092 for (retry = MCD_RETRIES; retry; retry--) { 1093 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.opcode); 1094 for (i = 0; i < mbx->cmd.length; i++) 1095 bus_space_write_1(iot, ioh, MCD_COMMAND, 1096 mbx->cmd.data.raw.data[i]); 1097 if ((error = mcd_getresult(sc, &mbx->res)) == 0) 1098 break; 1099 if (error == EINVAL) 1100 return error; 1101 } 1102 if (!retry) 1103 return error; 1104 if (diskin && (sc->flags & MCDF_LOADED) == 0) 1105 return EIO; 1106 1107 return 0; 1108 } 1109 1110 static int 1111 bcd2bin(bcd_t b) 1112 { 1113 1114 return (b >> 4) * 10 + (b & 15); 1115 } 1116 1117 static bcd_t 1118 bin2bcd(int b) 1119 { 1120 1121 return ((b / 10) << 4) | (b % 10); 1122 } 1123 1124 static void 1125 hsg2msf(int hsg, bcd_t *msf) 1126 { 1127 1128 hsg += 150; 1129 F_msf(msf) = bin2bcd(hsg % 75); 1130 hsg /= 75; 1131 S_msf(msf) = bin2bcd(hsg % 60); 1132 hsg /= 60; 1133 M_msf(msf) = bin2bcd(hsg); 1134 } 1135 1136 static daddr_t 1137 msf2hsg(bcd_t *msf, int relative) 1138 { 1139 daddr_t blkno; 1140 1141 blkno = bcd2bin(M_msf(msf)) * 75 * 60 + 1142 bcd2bin(S_msf(msf)) * 75 + 1143 bcd2bin(F_msf(msf)); 1144 if (!relative) 1145 blkno -= 150; 1146 return blkno; 1147 } 1148 1149 void 1150 mcd_pseudointr(void *v) 1151 { 1152 struct mcd_softc *sc = v; 1153 int s; 1154 1155 s = splbio(); 1156 (void) mcdintr(sc); 1157 splx(s); 1158 } 1159 1160 /* 1161 * State machine to process read requests. 1162 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode 1163 * MCD_S_WAITMODE: waits for status reply from set mode, set read command 1164 * MCD_S_WAITREAD: wait for read ready, read data. 1165 */ 1166 int 1167 mcdintr(void *arg) 1168 { 1169 struct mcd_softc *sc = arg; 1170 struct mcd_mbx *mbx = &sc->mbx; 1171 struct buf *bp = mbx->bp; 1172 bus_space_tag_t iot = sc->sc_iot; 1173 bus_space_handle_t ioh = sc->sc_ioh; 1174 1175 int i; 1176 u_char x; 1177 bcd_t msf[3]; 1178 1179 switch (mbx->state) { 1180 case MCD_S_IDLE: 1181 return 0; 1182 1183 case MCD_S_BEGIN: 1184 tryagain: 1185 if (mbx->mode == sc->lastmode) 1186 goto firstblock; 1187 1188 sc->lastmode = MCD_MD_UNKNOWN; 1189 bus_space_write_1(iot, ioh, MCD_COMMAND, MCD_CMDSETMODE); 1190 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->mode); 1191 1192 mbx->count = RDELAY_WAITMODE; 1193 mbx->state = MCD_S_WAITMODE; 1194 1195 case MCD_S_WAITMODE: 1196 callout_stop(&sc->sc_pintr_ch); 1197 for (i = 20; i; i--) { 1198 x = bus_space_read_1(iot, ioh, MCD_XFER); 1199 if ((x & MCD_XF_STATUSUNAVAIL) == 0) 1200 break; 1201 delay(50); 1202 } 1203 if (i == 0) 1204 goto hold; 1205 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS); 1206 mcd_setflags(sc); 1207 if ((sc->flags & MCDF_LOADED) == 0) 1208 goto changed; 1209 MCD_TRACE("doread: got WAITMODE delay=%d\n", 1210 RDELAY_WAITMODE - mbx->count); 1211 1212 sc->lastmode = mbx->mode; 1213 1214 firstblock: 1215 MCD_TRACE("doread: read blkno=%d for bp=0x%p\n", 1216 (int) mbx->blkno, bp); 1217 1218 /* Build parameter block. */ 1219 hsg2msf(mbx->blkno, msf); 1220 1221 /* Send the read command. */ 1222 bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd); 1223 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]); 1224 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]); 1225 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]); 1226 bus_space_write_1(iot, ioh, MCD_COMMAND, 0); 1227 bus_space_write_1(iot, ioh, MCD_COMMAND, 0); 1228 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk); 1229 1230 mbx->count = RDELAY_WAITREAD; 1231 mbx->state = MCD_S_WAITREAD; 1232 1233 case MCD_S_WAITREAD: 1234 callout_stop(&sc->sc_pintr_ch); 1235 nextblock: 1236 loop: 1237 for (i = 20; i; i--) { 1238 x = bus_space_read_1(iot, ioh, MCD_XFER); 1239 if ((x & MCD_XF_DATAUNAVAIL) == 0) 1240 goto gotblock; 1241 if ((x & MCD_XF_STATUSUNAVAIL) == 0) 1242 break; 1243 delay(50); 1244 } 1245 if (i == 0) 1246 goto hold; 1247 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS); 1248 mcd_setflags(sc); 1249 if ((sc->flags & MCDF_LOADED) == 0) 1250 goto changed; 1251 #if 0 1252 printf("%s: got status byte %02x during read\n", 1253 device_xname(sc->sc_dev), (u_int)sc->status); 1254 #endif 1255 goto loop; 1256 1257 gotblock: 1258 MCD_TRACE("doread: got data delay=%d\n", 1259 RDELAY_WAITREAD - mbx->count); 1260 1261 /* Data is ready. */ 1262 bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */ 1263 bus_space_read_multi_1(iot, ioh, MCD_RDATA, 1264 (char *)bp->b_data + mbx->skip, mbx->sz); 1265 bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */ 1266 mbx->blkno += 1; 1267 mbx->skip += mbx->sz; 1268 if (--mbx->nblk > 0) 1269 goto nextblock; 1270 1271 mbx->state = MCD_S_IDLE; 1272 1273 /* Return buffer. */ 1274 bp->b_resid = 0; 1275 disk_unbusy(&sc->sc_dk, bp->b_bcount, (bp->b_flags & B_READ)); 1276 biodone(bp); 1277 1278 mcdstart(sc); 1279 return 1; 1280 1281 hold: 1282 if (mbx->count-- < 0) { 1283 printf("%s: timeout in state %d", 1284 device_xname(sc->sc_dev), mbx->state); 1285 goto readerr; 1286 } 1287 1288 #if 0 1289 printf("%s: sleep in state %d\n", device_xname(sc->sc_dev), 1290 mbx->state); 1291 #endif 1292 callout_reset(&sc->sc_pintr_ch, hz / 100, 1293 mcd_pseudointr, sc); 1294 return -1; 1295 } 1296 1297 readerr: 1298 if (mbx->retry-- > 0) { 1299 printf("; retrying\n"); 1300 goto tryagain; 1301 } else 1302 printf("; giving up\n"); 1303 1304 changed: 1305 /* Invalidate the buffer. */ 1306 bp->b_error = EIO; 1307 bp->b_resid = bp->b_bcount - mbx->skip; 1308 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid), 1309 (bp->b_flags & B_READ)); 1310 biodone(bp); 1311 1312 mcdstart(sc); 1313 return -1; 1314 1315 #ifdef notyet 1316 printf("%s: unit timeout; resetting\n", device_xname(sc->sc_dev)); 1317 bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET); 1318 delay(300000); 1319 (void) mcd_getstat(sc, 1); 1320 (void) mcd_getstat(sc, 1); 1321 /*sc->status &= ~MCD_ST_DSKCHNG; */ 1322 sc->debug = 1; /* preventive set debug mode */ 1323 #endif 1324 } 1325 1326 void 1327 mcd_soft_reset(struct mcd_softc *sc) 1328 { 1329 1330 sc->debug = 0; 1331 sc->flags = 0; 1332 sc->lastmode = MCD_MD_UNKNOWN; 1333 sc->lastupc = MCD_UPC_UNKNOWN; 1334 sc->audio_status = CD_AS_AUDIO_INVALID; 1335 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */ 1336 } 1337 1338 int 1339 mcd_hard_reset(struct mcd_softc *sc) 1340 { 1341 struct mcd_mbox mbx; 1342 1343 mcd_soft_reset(sc); 1344 1345 mbx.cmd.opcode = MCD_CMDRESET; 1346 mbx.cmd.length = 0; 1347 mbx.res.length = 0; 1348 return mcd_send(sc, &mbx, 0); 1349 } 1350 1351 int 1352 mcd_setmode(struct mcd_softc *sc, int mode) 1353 { 1354 struct mcd_mbox mbx; 1355 int error; 1356 1357 if (sc->lastmode == mode) 1358 return 0; 1359 if (sc->debug) 1360 printf("%s: setting mode to %d\n", device_xname(sc->sc_dev), mode); 1361 sc->lastmode = MCD_MD_UNKNOWN; 1362 1363 mbx.cmd.opcode = MCD_CMDSETMODE; 1364 mbx.cmd.length = sizeof(mbx.cmd.data.datamode); 1365 mbx.cmd.data.datamode.mode = mode; 1366 mbx.res.length = 0; 1367 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1368 return error; 1369 1370 sc->lastmode = mode; 1371 return 0; 1372 } 1373 1374 int 1375 mcd_setupc(struct mcd_softc *sc, int upc) 1376 { 1377 struct mcd_mbox mbx; 1378 int error; 1379 1380 if (sc->lastupc == upc) 1381 return 0; 1382 if (sc->debug) 1383 printf("%s: setting upc to %d\n", device_xname(sc->sc_dev), upc); 1384 sc->lastupc = MCD_UPC_UNKNOWN; 1385 1386 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE; 1387 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1; 1388 mbx.cmd.data.config.subcommand = MCD_CF_READUPC; 1389 mbx.cmd.data.config.data1 = upc; 1390 mbx.res.length = 0; 1391 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1392 return error; 1393 1394 sc->lastupc = upc; 1395 return 0; 1396 } 1397 1398 int 1399 mcd_toc_header(struct mcd_softc *sc, struct ioc_toc_header *th) 1400 { 1401 1402 if (sc->debug) 1403 printf("%s: mcd_toc_header: reading toc header\n", 1404 device_xname(sc->sc_dev)); 1405 1406 th->len = msf2hsg(sc->volinfo.vol_msf, 0); 1407 th->starting_track = bcd2bin(sc->volinfo.trk_low); 1408 th->ending_track = bcd2bin(sc->volinfo.trk_high); 1409 1410 return 0; 1411 } 1412 1413 int 1414 mcd_read_toc(struct mcd_softc *sc) 1415 { 1416 struct ioc_toc_header th; 1417 union mcd_qchninfo q; 1418 int error, trk, idx, retry; 1419 1420 if ((error = mcd_toc_header(sc, &th)) != 0) 1421 return error; 1422 1423 if ((error = mcd_stop(sc)) != 0) 1424 return error; 1425 1426 if (sc->debug) 1427 printf("%s: read_toc: reading qchannel info\n", 1428 device_xname(sc->sc_dev)); 1429 1430 for (trk = th.starting_track; trk <= th.ending_track; trk++) 1431 sc->toc[trk].toc.idx_no = 0x00; 1432 trk = th.ending_track - th.starting_track + 1; 1433 for (retry = 300; retry && trk > 0; retry--) { 1434 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0) 1435 break; 1436 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00) 1437 continue; 1438 idx = bcd2bin(q.toc.idx_no); 1439 if (idx < MCD_MAXTOCS && 1440 sc->toc[idx].toc.idx_no == 0x00) { 1441 sc->toc[idx] = q; 1442 trk--; 1443 } 1444 } 1445 1446 /* Inform the drive that we're finished so it turns off the light. */ 1447 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1448 return error; 1449 1450 if (trk != 0) 1451 return EINVAL; 1452 1453 /* Add a fake last+1 for mcd_playtracks(). */ 1454 idx = th.ending_track + 1; 1455 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control; 1456 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type; 1457 sc->toc[idx].toc.trk_no = 0x00; 1458 sc->toc[idx].toc.idx_no = 0xaa; 1459 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0]; 1460 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1]; 1461 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2]; 1462 1463 return 0; 1464 } 1465 1466 int 1467 mcd_toc_entries(struct mcd_softc *sc, struct ioc_read_toc_entry *te, 1468 struct cd_toc_entry *entries, int *count) 1469 { 1470 int len = te->data_len; 1471 struct ioc_toc_header header; 1472 u_char trk; 1473 daddr_t lba; 1474 int error, n; 1475 1476 if (len < sizeof(struct cd_toc_entry)) 1477 return EINVAL; 1478 if (te->address_format != CD_MSF_FORMAT && 1479 te->address_format != CD_LBA_FORMAT) 1480 return EINVAL; 1481 1482 /* Copy the TOC header. */ 1483 if ((error = mcd_toc_header(sc, &header)) != 0) 1484 return error; 1485 1486 /* Verify starting track. */ 1487 trk = te->starting_track; 1488 if (trk == 0x00) 1489 trk = header.starting_track; 1490 else if (trk == 0xaa) 1491 trk = header.ending_track + 1; 1492 else if (trk < header.starting_track || 1493 trk > header.ending_track + 1) 1494 return EINVAL; 1495 1496 /* Copy the TOC data. */ 1497 for (n = 0; trk <= header.ending_track + 1; n++, trk++) { 1498 if (n * sizeof entries[0] > len) 1499 break; 1500 if (sc->toc[trk].toc.idx_no == 0x00) 1501 continue; 1502 entries[n].control = sc->toc[trk].toc.control; 1503 entries[n].addr_type = sc->toc[trk].toc.addr_type; 1504 entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no); 1505 switch (te->address_format) { 1506 case CD_MSF_FORMAT: 1507 entries[n].addr.addr[0] = 0; 1508 entries[n].addr.addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]); 1509 entries[n].addr.addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]); 1510 entries[n].addr.addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]); 1511 break; 1512 case CD_LBA_FORMAT: 1513 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0); 1514 entries[n].addr.addr[0] = lba >> 24; 1515 entries[n].addr.addr[1] = lba >> 16; 1516 entries[n].addr.addr[2] = lba >> 8; 1517 entries[n].addr.addr[3] = lba; 1518 break; 1519 } 1520 } 1521 1522 *count = n; 1523 return 0; 1524 } 1525 1526 int 1527 mcd_stop(struct mcd_softc *sc) 1528 { 1529 struct mcd_mbox mbx; 1530 int error; 1531 1532 if (sc->debug) 1533 printf("%s: mcd_stop: stopping play\n", device_xname(sc->sc_dev)); 1534 1535 mbx.cmd.opcode = MCD_CMDSTOPAUDIO; 1536 mbx.cmd.length = 0; 1537 mbx.res.length = 0; 1538 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1539 return error; 1540 1541 sc->audio_status = CD_AS_PLAY_COMPLETED; 1542 return 0; 1543 } 1544 1545 int 1546 mcd_getqchan(struct mcd_softc *sc, union mcd_qchninfo *q, int qchn) 1547 { 1548 struct mcd_mbox mbx; 1549 int error; 1550 1551 if (qchn == CD_TRACK_INFO) { 1552 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0) 1553 return error; 1554 } else { 1555 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1556 return error; 1557 } 1558 if (qchn == CD_MEDIA_CATALOG) { 1559 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0) 1560 return error; 1561 } else { 1562 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0) 1563 return error; 1564 } 1565 1566 mbx.cmd.opcode = MCD_CMDGETQCHN; 1567 mbx.cmd.length = 0; 1568 mbx.res.length = sizeof(mbx.res.data.qchninfo); 1569 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1570 return error; 1571 1572 *q = mbx.res.data.qchninfo; 1573 return 0; 1574 } 1575 1576 int 1577 mcd_read_subchannel(struct mcd_softc *sc, struct ioc_read_subchannel *ch, 1578 struct cd_sub_channel_info *info) 1579 { 1580 int len = ch->data_len; 1581 union mcd_qchninfo q; 1582 daddr_t lba; 1583 int error; 1584 1585 if (sc->debug) 1586 printf("%s: subchan: af=%d df=%d\n", device_xname(sc->sc_dev), 1587 ch->address_format, ch->data_format); 1588 1589 if (len > sizeof(*info) || len < sizeof(info->header)) 1590 return EINVAL; 1591 if (ch->address_format != CD_MSF_FORMAT && 1592 ch->address_format != CD_LBA_FORMAT) 1593 return EINVAL; 1594 if (ch->data_format != CD_CURRENT_POSITION && 1595 ch->data_format != CD_MEDIA_CATALOG) 1596 return EINVAL; 1597 1598 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0) 1599 return error; 1600 1601 info->header.audio_status = sc->audio_status; 1602 info->what.media_catalog.data_format = ch->data_format; 1603 1604 switch (ch->data_format) { 1605 case CD_MEDIA_CATALOG: 1606 info->what.media_catalog.mc_valid = 1; 1607 #if 0 1608 info->what.media_catalog.mc_number = 1609 #endif 1610 break; 1611 1612 case CD_CURRENT_POSITION: 1613 info->what.position.track_number = bcd2bin(q.current.trk_no); 1614 info->what.position.index_number = bcd2bin(q.current.idx_no); 1615 switch (ch->address_format) { 1616 case CD_MSF_FORMAT: 1617 info->what.position.reladdr.addr[0] = 0; 1618 info->what.position.reladdr.addr[1] = bcd2bin(q.current.relative_pos[0]); 1619 info->what.position.reladdr.addr[2] = bcd2bin(q.current.relative_pos[1]); 1620 info->what.position.reladdr.addr[3] = bcd2bin(q.current.relative_pos[2]); 1621 info->what.position.absaddr.addr[0] = 0; 1622 info->what.position.absaddr.addr[1] = bcd2bin(q.current.absolute_pos[0]); 1623 info->what.position.absaddr.addr[2] = bcd2bin(q.current.absolute_pos[1]); 1624 info->what.position.absaddr.addr[3] = bcd2bin(q.current.absolute_pos[2]); 1625 break; 1626 case CD_LBA_FORMAT: 1627 lba = msf2hsg(q.current.relative_pos, 1); 1628 /* 1629 * Pre-gap has index number of 0, and decreasing MSF 1630 * address. Must be converted to negative LBA, per 1631 * SCSI spec. 1632 */ 1633 if (info->what.position.index_number == 0x00) 1634 lba = -lba; 1635 info->what.position.reladdr.addr[0] = lba >> 24; 1636 info->what.position.reladdr.addr[1] = lba >> 16; 1637 info->what.position.reladdr.addr[2] = lba >> 8; 1638 info->what.position.reladdr.addr[3] = lba; 1639 lba = msf2hsg(q.current.absolute_pos, 0); 1640 info->what.position.absaddr.addr[0] = lba >> 24; 1641 info->what.position.absaddr.addr[1] = lba >> 16; 1642 info->what.position.absaddr.addr[2] = lba >> 8; 1643 info->what.position.absaddr.addr[3] = lba; 1644 break; 1645 } 1646 break; 1647 } 1648 1649 return 0; 1650 } 1651 1652 int 1653 mcd_playtracks(struct mcd_softc *sc, struct ioc_play_track *p) 1654 { 1655 struct mcd_mbox mbx; 1656 int a = p->start_track; 1657 int z = p->end_track; 1658 int error; 1659 1660 if (sc->debug) 1661 printf("%s: playtracks: from %d:%d to %d:%d\n", 1662 device_xname(sc->sc_dev), 1663 a, p->start_index, z, p->end_index); 1664 1665 if (a < bcd2bin(sc->volinfo.trk_low) || 1666 a > bcd2bin(sc->volinfo.trk_high) || 1667 a > z || 1668 z < bcd2bin(sc->volinfo.trk_low) || 1669 z > bcd2bin(sc->volinfo.trk_high)) 1670 return EINVAL; 1671 1672 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1673 return error; 1674 1675 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1676 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1677 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0]; 1678 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1]; 1679 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2]; 1680 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0]; 1681 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1]; 1682 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2]; 1683 sc->lastpb = mbx.cmd; 1684 mbx.res.length = 0; 1685 return mcd_send(sc, &mbx, 1); 1686 } 1687 1688 int 1689 mcd_playmsf(struct mcd_softc *sc, struct ioc_play_msf *p) 1690 { 1691 struct mcd_mbox mbx; 1692 int error; 1693 1694 if (sc->debug) 1695 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n", 1696 device_xname(sc->sc_dev), 1697 p->start_m, p->start_s, p->start_f, 1698 p->end_m, p->end_s, p->end_f); 1699 1700 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >= 1701 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f)) 1702 return EINVAL; 1703 1704 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1705 return error; 1706 1707 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1708 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1709 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m); 1710 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s); 1711 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f); 1712 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m); 1713 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s); 1714 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f); 1715 sc->lastpb = mbx.cmd; 1716 mbx.res.length = 0; 1717 return mcd_send(sc, &mbx, 1); 1718 } 1719 1720 int 1721 mcd_playblocks(struct mcd_softc *sc, struct ioc_play_blocks *p) 1722 { 1723 struct mcd_mbox mbx; 1724 int error; 1725 1726 if (sc->debug) 1727 printf("%s: playblocks: blkno %d length %d\n", 1728 device_xname(sc->sc_dev), p->blk, p->len); 1729 1730 if (p->blk > sc->disksize || p->len > sc->disksize || 1731 (p->blk + p->len) > sc->disksize) 1732 return 0; 1733 1734 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1735 return error; 1736 1737 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1738 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1739 hsg2msf(p->blk, mbx.cmd.data.play.start_msf); 1740 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf); 1741 sc->lastpb = mbx.cmd; 1742 mbx.res.length = 0; 1743 return mcd_send(sc, &mbx, 1); 1744 } 1745 1746 int 1747 mcd_pause(struct mcd_softc *sc) 1748 { 1749 union mcd_qchninfo q; 1750 int error; 1751 1752 /* Verify current status. */ 1753 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) { 1754 printf("%s: pause: attempted when not playing\n", 1755 device_xname(sc->sc_dev)); 1756 return EINVAL; 1757 } 1758 1759 /* Get the current position. */ 1760 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0) 1761 return error; 1762 1763 /* Copy it into lastpb. */ 1764 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0]; 1765 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1]; 1766 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2]; 1767 1768 /* Stop playing. */ 1769 if ((error = mcd_stop(sc)) != 0) 1770 return error; 1771 1772 /* Set the proper status and exit. */ 1773 sc->audio_status = CD_AS_PLAY_PAUSED; 1774 return 0; 1775 } 1776 1777 int 1778 mcd_resume(struct mcd_softc *sc) 1779 { 1780 struct mcd_mbox mbx; 1781 int error; 1782 1783 if (sc->audio_status != CD_AS_PLAY_PAUSED) 1784 return EINVAL; 1785 1786 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1787 return error; 1788 1789 mbx.cmd = sc->lastpb; 1790 mbx.res.length = 0; 1791 return mcd_send(sc, &mbx, 1); 1792 } 1793 1794 int 1795 mcd_eject(struct mcd_softc *sc) 1796 { 1797 struct mcd_mbox mbx; 1798 1799 mbx.cmd.opcode = MCD_CMDEJECTDISK; 1800 mbx.cmd.length = 0; 1801 mbx.res.length = 0; 1802 return mcd_send(sc, &mbx, 0); 1803 } 1804 1805 int 1806 mcd_setlock(struct mcd_softc *sc, int mode) 1807 { 1808 struct mcd_mbox mbx; 1809 1810 mbx.cmd.opcode = MCD_CMDSETLOCK; 1811 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode); 1812 mbx.cmd.data.lockmode.mode = mode; 1813 mbx.res.length = 0; 1814 return mcd_send(sc, &mbx, 1); 1815 } 1816