1 /* $NetBSD: mcd.c,v 1.118 2019/02/24 20:50:03 kamil 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.118 2019/02/24 20:50:03 kamil 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, uimin(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 /* FALLTHROUGH */ 1196 case MCD_S_WAITMODE: 1197 callout_stop(&sc->sc_pintr_ch); 1198 for (i = 20; i; i--) { 1199 x = bus_space_read_1(iot, ioh, MCD_XFER); 1200 if ((x & MCD_XF_STATUSUNAVAIL) == 0) 1201 break; 1202 delay(50); 1203 } 1204 if (i == 0) 1205 goto hold; 1206 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS); 1207 mcd_setflags(sc); 1208 if ((sc->flags & MCDF_LOADED) == 0) 1209 goto changed; 1210 MCD_TRACE("doread: got WAITMODE delay=%d\n", 1211 RDELAY_WAITMODE - mbx->count); 1212 1213 sc->lastmode = mbx->mode; 1214 1215 firstblock: 1216 MCD_TRACE("doread: read blkno=%d for bp=0x%p\n", 1217 (int) mbx->blkno, bp); 1218 1219 /* Build parameter block. */ 1220 hsg2msf(mbx->blkno, msf); 1221 1222 /* Send the read command. */ 1223 bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd); 1224 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]); 1225 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]); 1226 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]); 1227 bus_space_write_1(iot, ioh, MCD_COMMAND, 0); 1228 bus_space_write_1(iot, ioh, MCD_COMMAND, 0); 1229 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk); 1230 1231 mbx->count = RDELAY_WAITREAD; 1232 mbx->state = MCD_S_WAITREAD; 1233 1234 /* FALLTHROUGH */ 1235 case MCD_S_WAITREAD: 1236 callout_stop(&sc->sc_pintr_ch); 1237 nextblock: 1238 loop: 1239 for (i = 20; i; i--) { 1240 x = bus_space_read_1(iot, ioh, MCD_XFER); 1241 if ((x & MCD_XF_DATAUNAVAIL) == 0) 1242 goto gotblock; 1243 if ((x & MCD_XF_STATUSUNAVAIL) == 0) 1244 break; 1245 delay(50); 1246 } 1247 if (i == 0) 1248 goto hold; 1249 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS); 1250 mcd_setflags(sc); 1251 if ((sc->flags & MCDF_LOADED) == 0) 1252 goto changed; 1253 #if 0 1254 printf("%s: got status byte %02x during read\n", 1255 device_xname(sc->sc_dev), (u_int)sc->status); 1256 #endif 1257 goto loop; 1258 1259 gotblock: 1260 MCD_TRACE("doread: got data delay=%d\n", 1261 RDELAY_WAITREAD - mbx->count); 1262 1263 /* Data is ready. */ 1264 bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */ 1265 bus_space_read_multi_1(iot, ioh, MCD_RDATA, 1266 (char *)bp->b_data + mbx->skip, mbx->sz); 1267 bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */ 1268 mbx->blkno += 1; 1269 mbx->skip += mbx->sz; 1270 if (--mbx->nblk > 0) 1271 goto nextblock; 1272 1273 mbx->state = MCD_S_IDLE; 1274 1275 /* Return buffer. */ 1276 bp->b_resid = 0; 1277 disk_unbusy(&sc->sc_dk, bp->b_bcount, (bp->b_flags & B_READ)); 1278 biodone(bp); 1279 1280 mcdstart(sc); 1281 return 1; 1282 1283 hold: 1284 if (mbx->count-- < 0) { 1285 printf("%s: timeout in state %d", 1286 device_xname(sc->sc_dev), mbx->state); 1287 goto readerr; 1288 } 1289 1290 #if 0 1291 printf("%s: sleep in state %d\n", device_xname(sc->sc_dev), 1292 mbx->state); 1293 #endif 1294 callout_reset(&sc->sc_pintr_ch, hz / 100, 1295 mcd_pseudointr, sc); 1296 return -1; 1297 } 1298 1299 readerr: 1300 if (mbx->retry-- > 0) { 1301 printf("; retrying\n"); 1302 goto tryagain; 1303 } else 1304 printf("; giving up\n"); 1305 1306 changed: 1307 /* Invalidate the buffer. */ 1308 bp->b_error = EIO; 1309 bp->b_resid = bp->b_bcount - mbx->skip; 1310 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid), 1311 (bp->b_flags & B_READ)); 1312 biodone(bp); 1313 1314 mcdstart(sc); 1315 return -1; 1316 1317 #ifdef notyet 1318 printf("%s: unit timeout; resetting\n", device_xname(sc->sc_dev)); 1319 bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET); 1320 delay(300000); 1321 (void) mcd_getstat(sc, 1); 1322 (void) mcd_getstat(sc, 1); 1323 /*sc->status &= ~MCD_ST_DSKCHNG; */ 1324 sc->debug = 1; /* preventive set debug mode */ 1325 #endif 1326 } 1327 1328 void 1329 mcd_soft_reset(struct mcd_softc *sc) 1330 { 1331 1332 sc->debug = 0; 1333 sc->flags = 0; 1334 sc->lastmode = MCD_MD_UNKNOWN; 1335 sc->lastupc = MCD_UPC_UNKNOWN; 1336 sc->audio_status = CD_AS_AUDIO_INVALID; 1337 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */ 1338 } 1339 1340 int 1341 mcd_hard_reset(struct mcd_softc *sc) 1342 { 1343 struct mcd_mbox mbx; 1344 1345 mcd_soft_reset(sc); 1346 1347 mbx.cmd.opcode = MCD_CMDRESET; 1348 mbx.cmd.length = 0; 1349 mbx.res.length = 0; 1350 return mcd_send(sc, &mbx, 0); 1351 } 1352 1353 int 1354 mcd_setmode(struct mcd_softc *sc, int mode) 1355 { 1356 struct mcd_mbox mbx; 1357 int error; 1358 1359 if (sc->lastmode == mode) 1360 return 0; 1361 if (sc->debug) 1362 printf("%s: setting mode to %d\n", device_xname(sc->sc_dev), mode); 1363 sc->lastmode = MCD_MD_UNKNOWN; 1364 1365 mbx.cmd.opcode = MCD_CMDSETMODE; 1366 mbx.cmd.length = sizeof(mbx.cmd.data.datamode); 1367 mbx.cmd.data.datamode.mode = mode; 1368 mbx.res.length = 0; 1369 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1370 return error; 1371 1372 sc->lastmode = mode; 1373 return 0; 1374 } 1375 1376 int 1377 mcd_setupc(struct mcd_softc *sc, int upc) 1378 { 1379 struct mcd_mbox mbx; 1380 int error; 1381 1382 if (sc->lastupc == upc) 1383 return 0; 1384 if (sc->debug) 1385 printf("%s: setting upc to %d\n", device_xname(sc->sc_dev), upc); 1386 sc->lastupc = MCD_UPC_UNKNOWN; 1387 1388 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE; 1389 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1; 1390 mbx.cmd.data.config.subcommand = MCD_CF_READUPC; 1391 mbx.cmd.data.config.data1 = upc; 1392 mbx.res.length = 0; 1393 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1394 return error; 1395 1396 sc->lastupc = upc; 1397 return 0; 1398 } 1399 1400 int 1401 mcd_toc_header(struct mcd_softc *sc, struct ioc_toc_header *th) 1402 { 1403 1404 if (sc->debug) 1405 printf("%s: mcd_toc_header: reading toc header\n", 1406 device_xname(sc->sc_dev)); 1407 1408 th->len = msf2hsg(sc->volinfo.vol_msf, 0); 1409 th->starting_track = bcd2bin(sc->volinfo.trk_low); 1410 th->ending_track = bcd2bin(sc->volinfo.trk_high); 1411 1412 return 0; 1413 } 1414 1415 int 1416 mcd_read_toc(struct mcd_softc *sc) 1417 { 1418 struct ioc_toc_header th; 1419 union mcd_qchninfo q; 1420 int error, trk, idx, retry; 1421 1422 if ((error = mcd_toc_header(sc, &th)) != 0) 1423 return error; 1424 1425 if ((error = mcd_stop(sc)) != 0) 1426 return error; 1427 1428 if (sc->debug) 1429 printf("%s: read_toc: reading qchannel info\n", 1430 device_xname(sc->sc_dev)); 1431 1432 for (trk = th.starting_track; trk <= th.ending_track; trk++) 1433 sc->toc[trk].toc.idx_no = 0x00; 1434 trk = th.ending_track - th.starting_track + 1; 1435 for (retry = 300; retry && trk > 0; retry--) { 1436 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0) 1437 break; 1438 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00) 1439 continue; 1440 idx = bcd2bin(q.toc.idx_no); 1441 if (idx < MCD_MAXTOCS && 1442 sc->toc[idx].toc.idx_no == 0x00) { 1443 sc->toc[idx] = q; 1444 trk--; 1445 } 1446 } 1447 1448 /* Inform the drive that we're finished so it turns off the light. */ 1449 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1450 return error; 1451 1452 if (trk != 0) 1453 return EINVAL; 1454 1455 /* Add a fake last+1 for mcd_playtracks(). */ 1456 idx = th.ending_track + 1; 1457 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control; 1458 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type; 1459 sc->toc[idx].toc.trk_no = 0x00; 1460 sc->toc[idx].toc.idx_no = 0xaa; 1461 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0]; 1462 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1]; 1463 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2]; 1464 1465 return 0; 1466 } 1467 1468 int 1469 mcd_toc_entries(struct mcd_softc *sc, struct ioc_read_toc_entry *te, 1470 struct cd_toc_entry *entries, int *count) 1471 { 1472 int len = te->data_len; 1473 struct ioc_toc_header header; 1474 u_char trk; 1475 daddr_t lba; 1476 int error, n; 1477 1478 if (len < sizeof(struct cd_toc_entry)) 1479 return EINVAL; 1480 if (te->address_format != CD_MSF_FORMAT && 1481 te->address_format != CD_LBA_FORMAT) 1482 return EINVAL; 1483 1484 /* Copy the TOC header. */ 1485 if ((error = mcd_toc_header(sc, &header)) != 0) 1486 return error; 1487 1488 /* Verify starting track. */ 1489 trk = te->starting_track; 1490 if (trk == 0x00) 1491 trk = header.starting_track; 1492 else if (trk == 0xaa) 1493 trk = header.ending_track + 1; 1494 else if (trk < header.starting_track || 1495 trk > header.ending_track + 1) 1496 return EINVAL; 1497 1498 /* Copy the TOC data. */ 1499 for (n = 0; trk <= header.ending_track + 1; n++, trk++) { 1500 if (n * sizeof entries[0] > len) 1501 break; 1502 if (sc->toc[trk].toc.idx_no == 0x00) 1503 continue; 1504 entries[n].control = sc->toc[trk].toc.control; 1505 entries[n].addr_type = sc->toc[trk].toc.addr_type; 1506 entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no); 1507 switch (te->address_format) { 1508 case CD_MSF_FORMAT: 1509 entries[n].addr.addr[0] = 0; 1510 entries[n].addr.addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]); 1511 entries[n].addr.addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]); 1512 entries[n].addr.addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]); 1513 break; 1514 case CD_LBA_FORMAT: 1515 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0); 1516 entries[n].addr.addr[0] = lba >> 24; 1517 entries[n].addr.addr[1] = lba >> 16; 1518 entries[n].addr.addr[2] = lba >> 8; 1519 entries[n].addr.addr[3] = lba; 1520 break; 1521 } 1522 } 1523 1524 *count = n; 1525 return 0; 1526 } 1527 1528 int 1529 mcd_stop(struct mcd_softc *sc) 1530 { 1531 struct mcd_mbox mbx; 1532 int error; 1533 1534 if (sc->debug) 1535 printf("%s: mcd_stop: stopping play\n", device_xname(sc->sc_dev)); 1536 1537 mbx.cmd.opcode = MCD_CMDSTOPAUDIO; 1538 mbx.cmd.length = 0; 1539 mbx.res.length = 0; 1540 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1541 return error; 1542 1543 sc->audio_status = CD_AS_PLAY_COMPLETED; 1544 return 0; 1545 } 1546 1547 int 1548 mcd_getqchan(struct mcd_softc *sc, union mcd_qchninfo *q, int qchn) 1549 { 1550 struct mcd_mbox mbx; 1551 int error; 1552 1553 if (qchn == CD_TRACK_INFO) { 1554 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0) 1555 return error; 1556 } else { 1557 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1558 return error; 1559 } 1560 if (qchn == CD_MEDIA_CATALOG) { 1561 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0) 1562 return error; 1563 } else { 1564 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0) 1565 return error; 1566 } 1567 1568 mbx.cmd.opcode = MCD_CMDGETQCHN; 1569 mbx.cmd.length = 0; 1570 mbx.res.length = sizeof(mbx.res.data.qchninfo); 1571 if ((error = mcd_send(sc, &mbx, 1)) != 0) 1572 return error; 1573 1574 *q = mbx.res.data.qchninfo; 1575 return 0; 1576 } 1577 1578 int 1579 mcd_read_subchannel(struct mcd_softc *sc, struct ioc_read_subchannel *ch, 1580 struct cd_sub_channel_info *info) 1581 { 1582 int len = ch->data_len; 1583 union mcd_qchninfo q; 1584 daddr_t lba; 1585 int error; 1586 1587 if (sc->debug) 1588 printf("%s: subchan: af=%d df=%d\n", device_xname(sc->sc_dev), 1589 ch->address_format, ch->data_format); 1590 1591 if (len > sizeof(*info) || len < sizeof(info->header)) 1592 return EINVAL; 1593 if (ch->address_format != CD_MSF_FORMAT && 1594 ch->address_format != CD_LBA_FORMAT) 1595 return EINVAL; 1596 if (ch->data_format != CD_CURRENT_POSITION && 1597 ch->data_format != CD_MEDIA_CATALOG) 1598 return EINVAL; 1599 1600 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0) 1601 return error; 1602 1603 info->header.audio_status = sc->audio_status; 1604 info->what.media_catalog.data_format = ch->data_format; 1605 1606 switch (ch->data_format) { 1607 case CD_MEDIA_CATALOG: 1608 info->what.media_catalog.mc_valid = 1; 1609 #if 0 1610 info->what.media_catalog.mc_number = 1611 #endif 1612 break; 1613 1614 case CD_CURRENT_POSITION: 1615 info->what.position.track_number = bcd2bin(q.current.trk_no); 1616 info->what.position.index_number = bcd2bin(q.current.idx_no); 1617 switch (ch->address_format) { 1618 case CD_MSF_FORMAT: 1619 info->what.position.reladdr.addr[0] = 0; 1620 info->what.position.reladdr.addr[1] = bcd2bin(q.current.relative_pos[0]); 1621 info->what.position.reladdr.addr[2] = bcd2bin(q.current.relative_pos[1]); 1622 info->what.position.reladdr.addr[3] = bcd2bin(q.current.relative_pos[2]); 1623 info->what.position.absaddr.addr[0] = 0; 1624 info->what.position.absaddr.addr[1] = bcd2bin(q.current.absolute_pos[0]); 1625 info->what.position.absaddr.addr[2] = bcd2bin(q.current.absolute_pos[1]); 1626 info->what.position.absaddr.addr[3] = bcd2bin(q.current.absolute_pos[2]); 1627 break; 1628 case CD_LBA_FORMAT: 1629 lba = msf2hsg(q.current.relative_pos, 1); 1630 /* 1631 * Pre-gap has index number of 0, and decreasing MSF 1632 * address. Must be converted to negative LBA, per 1633 * SCSI spec. 1634 */ 1635 if (info->what.position.index_number == 0x00) 1636 lba = -lba; 1637 info->what.position.reladdr.addr[0] = lba >> 24; 1638 info->what.position.reladdr.addr[1] = lba >> 16; 1639 info->what.position.reladdr.addr[2] = lba >> 8; 1640 info->what.position.reladdr.addr[3] = lba; 1641 lba = msf2hsg(q.current.absolute_pos, 0); 1642 info->what.position.absaddr.addr[0] = lba >> 24; 1643 info->what.position.absaddr.addr[1] = lba >> 16; 1644 info->what.position.absaddr.addr[2] = lba >> 8; 1645 info->what.position.absaddr.addr[3] = lba; 1646 break; 1647 } 1648 break; 1649 } 1650 1651 return 0; 1652 } 1653 1654 int 1655 mcd_playtracks(struct mcd_softc *sc, struct ioc_play_track *p) 1656 { 1657 struct mcd_mbox mbx; 1658 int a = p->start_track; 1659 int z = p->end_track; 1660 int error; 1661 1662 if (sc->debug) 1663 printf("%s: playtracks: from %d:%d to %d:%d\n", 1664 device_xname(sc->sc_dev), 1665 a, p->start_index, z, p->end_index); 1666 1667 if (a < bcd2bin(sc->volinfo.trk_low) || 1668 a > bcd2bin(sc->volinfo.trk_high) || 1669 a > z || 1670 z < bcd2bin(sc->volinfo.trk_low) || 1671 z > bcd2bin(sc->volinfo.trk_high)) 1672 return EINVAL; 1673 1674 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1675 return error; 1676 1677 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1678 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1679 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0]; 1680 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1]; 1681 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2]; 1682 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0]; 1683 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1]; 1684 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2]; 1685 sc->lastpb = mbx.cmd; 1686 mbx.res.length = 0; 1687 return mcd_send(sc, &mbx, 1); 1688 } 1689 1690 int 1691 mcd_playmsf(struct mcd_softc *sc, struct ioc_play_msf *p) 1692 { 1693 struct mcd_mbox mbx; 1694 int error; 1695 1696 if (sc->debug) 1697 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n", 1698 device_xname(sc->sc_dev), 1699 p->start_m, p->start_s, p->start_f, 1700 p->end_m, p->end_s, p->end_f); 1701 1702 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >= 1703 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f)) 1704 return EINVAL; 1705 1706 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1707 return error; 1708 1709 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1710 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1711 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m); 1712 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s); 1713 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f); 1714 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m); 1715 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s); 1716 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f); 1717 sc->lastpb = mbx.cmd; 1718 mbx.res.length = 0; 1719 return mcd_send(sc, &mbx, 1); 1720 } 1721 1722 int 1723 mcd_playblocks(struct mcd_softc *sc, struct ioc_play_blocks *p) 1724 { 1725 struct mcd_mbox mbx; 1726 int error; 1727 1728 if (sc->debug) 1729 printf("%s: playblocks: blkno %d length %d\n", 1730 device_xname(sc->sc_dev), p->blk, p->len); 1731 1732 if (p->blk > sc->disksize || p->len > sc->disksize || 1733 (p->blk + p->len) > sc->disksize) 1734 return 0; 1735 1736 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1737 return error; 1738 1739 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED; 1740 mbx.cmd.length = sizeof(mbx.cmd.data.play); 1741 hsg2msf(p->blk, mbx.cmd.data.play.start_msf); 1742 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf); 1743 sc->lastpb = mbx.cmd; 1744 mbx.res.length = 0; 1745 return mcd_send(sc, &mbx, 1); 1746 } 1747 1748 int 1749 mcd_pause(struct mcd_softc *sc) 1750 { 1751 union mcd_qchninfo q; 1752 int error; 1753 1754 /* Verify current status. */ 1755 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) { 1756 printf("%s: pause: attempted when not playing\n", 1757 device_xname(sc->sc_dev)); 1758 return EINVAL; 1759 } 1760 1761 /* Get the current position. */ 1762 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0) 1763 return error; 1764 1765 /* Copy it into lastpb. */ 1766 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0]; 1767 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1]; 1768 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2]; 1769 1770 /* Stop playing. */ 1771 if ((error = mcd_stop(sc)) != 0) 1772 return error; 1773 1774 /* Set the proper status and exit. */ 1775 sc->audio_status = CD_AS_PLAY_PAUSED; 1776 return 0; 1777 } 1778 1779 int 1780 mcd_resume(struct mcd_softc *sc) 1781 { 1782 struct mcd_mbox mbx; 1783 int error; 1784 1785 if (sc->audio_status != CD_AS_PLAY_PAUSED) 1786 return EINVAL; 1787 1788 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0) 1789 return error; 1790 1791 mbx.cmd = sc->lastpb; 1792 mbx.res.length = 0; 1793 return mcd_send(sc, &mbx, 1); 1794 } 1795 1796 int 1797 mcd_eject(struct mcd_softc *sc) 1798 { 1799 struct mcd_mbox mbx; 1800 1801 mbx.cmd.opcode = MCD_CMDEJECTDISK; 1802 mbx.cmd.length = 0; 1803 mbx.res.length = 0; 1804 return mcd_send(sc, &mbx, 0); 1805 } 1806 1807 int 1808 mcd_setlock(struct mcd_softc *sc, int mode) 1809 { 1810 struct mcd_mbox mbx; 1811 1812 mbx.cmd.opcode = MCD_CMDSETLOCK; 1813 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode); 1814 mbx.cmd.data.lockmode.mode = mode; 1815 mbx.res.length = 0; 1816 return mcd_send(sc, &mbx, 1); 1817 } 1818