1 /* $NetBSD: midi.c,v 1.10 1998/12/20 14:26:44 drochner Exp $ */ 2 3 /* 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (augustss@netbsd.org). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include "midi.h" 40 #include "sequencer.h" 41 42 #include <sys/param.h> 43 #include <sys/ioctl.h> 44 #include <sys/fcntl.h> 45 #include <sys/vnode.h> 46 #include <sys/select.h> 47 #include <sys/poll.h> 48 #include <sys/malloc.h> 49 #include <sys/proc.h> 50 #include <sys/systm.h> 51 #include <sys/syslog.h> 52 #include <sys/kernel.h> 53 #include <sys/signalvar.h> 54 #include <sys/conf.h> 55 #include <sys/audioio.h> 56 #include <sys/midiio.h> 57 #include <sys/device.h> 58 59 #include <dev/audio_if.h> 60 #include <dev/midi_if.h> 61 #include <dev/midivar.h> 62 63 #if NMIDI > 0 64 65 #ifdef AUDIO_DEBUG 66 #define DPRINTF(x) if (mididebug) printf x 67 #define DPRINTFN(n,x) if (mididebug >= (n)) printf x 68 int mididebug = 0; 69 #else 70 #define DPRINTF(x) 71 #define DPRINTFN(n,x) 72 #endif 73 74 int midi_wait; 75 76 void midi_in __P((void *, int)); 77 void midi_out __P((void *)); 78 int midi_start_output __P((struct midi_softc *, int)); 79 int midi_sleep_timo __P((int *, char *, int)); 80 int midi_sleep __P((int *, char *)); 81 void midi_wakeup __P((int *)); 82 void midi_initbuf __P((struct midi_buffer *)); 83 void midi_timeout __P((void *)); 84 85 int midiprobe __P((struct device *, struct cfdata *, void *)); 86 void midiattach __P((struct device *, struct device *, void *)); 87 88 struct cfattach midi_ca = { 89 sizeof(struct midi_softc), midiprobe, midiattach 90 }; 91 92 #ifdef MIDI_SAVE 93 #define MIDI_SAVE_SIZE 100000 94 int midicnt; 95 struct { 96 int cnt; 97 u_char buf[MIDI_SAVE_SIZE]; 98 } midisave; 99 #define MIDI_GETSAVE _IOWR('m', 100, int) 100 101 #endif 102 103 extern struct cfdriver midi_cd; 104 105 int 106 midiprobe(parent, match, aux) 107 struct device *parent; 108 struct cfdata *match; 109 void *aux; 110 { 111 struct audio_attach_args *sa = aux; 112 113 DPRINTFN(6,("midiprobe: type=%d sa=%p hw=%p\n", 114 sa->type, sa, sa->hwif)); 115 return (sa->type == AUDIODEV_TYPE_MIDI) ? 1 : 0; 116 } 117 118 void 119 midiattach(parent, self, aux) 120 struct device *parent, *self; 121 void *aux; 122 { 123 struct midi_softc *sc = (void *)self; 124 struct audio_attach_args *sa = aux; 125 struct midi_hw_if *hwp = sa->hwif; 126 void *hdlp = sa->hdl; 127 128 DPRINTFN(6, ("MIDI attach\n")); 129 130 #ifdef DIAGNOSTIC 131 if (hwp == 0 || 132 hwp->open == 0 || 133 hwp->close == 0 || 134 hwp->output == 0 || 135 hwp->getinfo == 0) { 136 printf("midi: missing method\n"); 137 return; 138 } 139 #endif 140 sc->hw_if = hwp; 141 sc->hw_hdl = hdlp; 142 midi_attach(sc, parent); 143 } 144 145 void 146 midi_attach(sc, parent) 147 struct midi_softc *sc; 148 struct device *parent; 149 { 150 struct midi_info mi; 151 152 sc->isopen = 0; 153 154 midi_wait = MIDI_WAIT * hz / 1000000; 155 if (midi_wait == 0) 156 midi_wait = 1; 157 158 sc->sc_dev = parent; 159 sc->hw_if->getinfo(sc->hw_hdl, &mi); 160 sc->props = mi.props; 161 printf(": <%s>\n", mi.name); 162 } 163 164 int 165 midi_unit_count() 166 { 167 return midi_cd.cd_ndevs; 168 } 169 170 void 171 midi_initbuf(mb) 172 struct midi_buffer *mb; 173 { 174 mb->used = 0; 175 mb->usedhigh = MIDI_BUFSIZE; 176 mb->end = mb->start + mb->usedhigh; 177 mb->inp = mb->outp = mb->start; 178 } 179 180 int 181 midi_sleep_timo(chan, label, timo) 182 int *chan; 183 char *label; 184 int timo; 185 { 186 int st; 187 188 if (!label) 189 label = "midi"; 190 191 DPRINTFN(5, ("midi_sleep_timo: %p %s %d\n", chan, label, timo)); 192 *chan = 1; 193 st = tsleep(chan, PWAIT | PCATCH, label, timo); 194 *chan = 0; 195 #ifdef MIDI_DEBUG 196 if (st != 0) 197 printf("midi_sleep: %d\n", st); 198 #endif 199 return st; 200 } 201 202 int 203 midi_sleep(chan, label) 204 int *chan; 205 char *label; 206 { 207 return midi_sleep_timo(chan, label, 0); 208 } 209 210 void 211 midi_wakeup(chan) 212 int *chan; 213 { 214 if (*chan) { 215 DPRINTFN(5, ("midi_wakeup: %p\n", chan)); 216 wakeup(chan); 217 *chan = 0; 218 } 219 } 220 221 static int midi_lengths[] = { 2,2,2,2,1,1,2,0 }; 222 /* Number of bytes in a MIDI command */ 223 #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7]) 224 225 void 226 midi_in(addr, data) 227 void *addr; 228 int data; 229 { 230 struct midi_softc *sc = addr; 231 struct midi_buffer *mb = &sc->inbuf; 232 int i; 233 234 if (!sc->isopen) 235 return; 236 if (data == MIDI_ACK) 237 return; 238 DPRINTFN(3, ("midi_in: %p 0x%02x\n", sc, data)); 239 if (!(sc->flags & FREAD)) 240 return; /* discard data if not reading */ 241 242 switch(sc->in_state) { 243 case MIDI_IN_START: 244 if (MIDI_IS_STATUS(data)) { 245 switch(data) { 246 case 0xf0: /* Sysex */ 247 sc->in_state = MIDI_IN_SYSEX; 248 break; 249 case 0xf1: /* MTC quarter frame */ 250 case 0xf3: /* Song select */ 251 sc->in_state = MIDI_IN_DATA; 252 sc->in_msg[0] = data; 253 sc->in_pos = 1; 254 sc->in_left = 1; 255 break; 256 case 0xf2: /* Song position pointer */ 257 sc->in_state = MIDI_IN_DATA; 258 sc->in_msg[0] = data; 259 sc->in_pos = 1; 260 sc->in_left = 2; 261 break; 262 default: 263 if (MIDI_IS_COMMON(data)) { 264 sc->in_msg[0] = data; 265 sc->in_pos = 1; 266 goto deliver; 267 } else { 268 sc->in_state = MIDI_IN_DATA; 269 sc->in_msg[0] = sc->in_status = data; 270 sc->in_pos = 1; 271 sc->in_left = 272 MIDI_LENGTH(sc->in_status); 273 } 274 break; 275 } 276 } else { 277 if (MIDI_IS_STATUS(sc->in_status)) { 278 sc->in_state = MIDI_IN_DATA; 279 sc->in_msg[0] = sc->in_status; 280 sc->in_msg[1] = data; 281 sc->in_pos = 2; 282 sc->in_left = MIDI_LENGTH(sc->in_status) - 1; 283 } 284 } 285 return; 286 case MIDI_IN_DATA: 287 sc->in_msg[sc->in_pos++] = data; 288 if (--sc->in_left <= 0) 289 break; /* deliver data */ 290 return; 291 case MIDI_IN_SYSEX: 292 if (data == MIDI_SYSEX_END) 293 sc->in_state = MIDI_IN_START; 294 return; 295 } 296 deliver: 297 sc->in_state = MIDI_IN_START; 298 #if NSEQUENCER > 0 299 if (sc->seqopen) { 300 extern void midiseq_in __P((struct midi_dev *,u_char *,int)); 301 midiseq_in(sc->seq_md, sc->in_msg, sc->in_pos); 302 return; 303 } 304 #endif 305 306 if (mb->used + sc->in_pos > mb->usedhigh) { 307 DPRINTF(("midi_in: buffer full, discard data=0x%02x\n", 308 sc->in_msg[0])); 309 return; 310 } 311 for (i = 0; i < sc->in_pos; i++) { 312 *mb->inp++ = sc->in_msg[i]; 313 if (mb->inp >= mb->end) 314 mb->inp = mb->start; 315 mb->used++; 316 } 317 midi_wakeup(&sc->rchan); 318 selwakeup(&sc->rsel); 319 if (sc->async) 320 psignal(sc->async, SIGIO); 321 } 322 323 void 324 midi_out(addr) 325 void *addr; 326 { 327 struct midi_softc *sc = addr; 328 329 if (!sc->isopen) 330 return; 331 DPRINTFN(3, ("midi_out: %p\n", sc)); 332 midi_start_output(sc, 1); 333 } 334 335 int 336 midiopen(dev, flags, ifmt, p) 337 dev_t dev; 338 int flags, ifmt; 339 struct proc *p; 340 { 341 int unit = MIDIUNIT(dev); 342 struct midi_softc *sc; 343 struct midi_hw_if *hw; 344 int error; 345 346 if (unit >= midi_cd.cd_ndevs || 347 (sc = midi_cd.cd_devs[unit]) == NULL) 348 return ENXIO; 349 DPRINTF(("midiopen %p\n", sc)); 350 351 hw = sc->hw_if; 352 if (!hw) 353 return ENXIO; 354 if (sc->isopen) 355 return EBUSY; 356 sc->in_state = MIDI_IN_START; 357 sc->in_status = 0; 358 error = hw->open(sc->hw_hdl, flags, midi_in, midi_out, sc); 359 if (error) 360 return error; 361 sc->isopen++; 362 midi_initbuf(&sc->outbuf); 363 midi_initbuf(&sc->inbuf); 364 sc->flags = flags; 365 sc->rchan = 0; 366 sc->wchan = 0; 367 sc->pbus = 0; 368 sc->async = 0; 369 370 #ifdef MIDI_SAVE 371 if (midicnt != 0) { 372 midisave.cnt = midicnt; 373 midicnt = 0; 374 } 375 #endif 376 377 return 0; 378 } 379 380 int 381 midiclose(dev, flags, ifmt, p) 382 dev_t dev; 383 int flags, ifmt; 384 struct proc *p; 385 { 386 int unit = MIDIUNIT(dev); 387 struct midi_softc *sc = midi_cd.cd_devs[unit]; 388 struct midi_hw_if *hw = sc->hw_if; 389 int s, error; 390 391 DPRINTF(("midiclose %p\n", sc)); 392 393 midi_start_output(sc, 0); 394 error = 0; 395 s = splaudio(); 396 while (sc->outbuf.used > 0 && !error) { 397 DPRINTFN(2,("midiclose sleep used=%d\n", sc->outbuf.used)); 398 error = midi_sleep_timo(&sc->wchan, "mid_dr", 30*hz); 399 } 400 splx(s); 401 sc->isopen = 0; 402 hw->close(sc->hw_hdl); 403 #if NSEQUENCER > 0 404 sc->seqopen = 0; 405 sc->seq_md = 0; 406 #endif 407 return 0; 408 } 409 410 int 411 midiread(dev, uio, ioflag) 412 dev_t dev; 413 struct uio *uio; 414 int ioflag; 415 { 416 int unit = MIDIUNIT(dev); 417 struct midi_softc *sc = midi_cd.cd_devs[unit]; 418 struct midi_buffer *mb = &sc->inbuf; 419 int error; 420 u_char *outp; 421 int used, cc, n, resid; 422 int s; 423 424 DPRINTF(("midiread: %p, count=%d\n", sc, uio->uio_resid)); 425 426 error = 0; 427 resid = uio->uio_resid; 428 while (uio->uio_resid == resid && !error) { 429 s = splaudio(); 430 while (mb->used <= 0) { 431 if (ioflag & IO_NDELAY) { 432 splx(s); 433 return EWOULDBLOCK; 434 } 435 error = midi_sleep(&sc->rchan, "mid rd"); 436 if (error) { 437 splx(s); 438 return error; 439 } 440 } 441 used = mb->used; 442 outp = mb->outp; 443 splx(s); 444 cc = used; /* maximum to read */ 445 n = mb->end - outp; 446 if (n < cc) 447 cc = n; /* don't read beyond end of buffer */ 448 if (uio->uio_resid < cc) 449 cc = uio->uio_resid; /* and no more than we want */ 450 DPRINTFN(3, ("midiread: uiomove cc=%d\n", cc)); 451 error = uiomove(outp, cc, uio); 452 if (error) 453 break; 454 used -= cc; 455 outp += cc; 456 if (outp >= mb->end) 457 outp = mb->start; 458 s = splaudio(); 459 mb->outp = outp; 460 mb->used = used; 461 splx(s); 462 } 463 return error; 464 } 465 466 void 467 midi_timeout(arg) 468 void *arg; 469 { 470 struct midi_softc *sc = arg; 471 472 DPRINTFN(3,("midi_timeout: %p\n", sc)); 473 midi_start_output(sc, 1); 474 } 475 476 int 477 midi_start_output(sc, intr) 478 struct midi_softc *sc; 479 int intr; 480 { 481 struct midi_buffer *mb = &sc->outbuf; 482 u_char *outp; 483 int error; 484 int s; 485 int i, mmax; 486 487 error = 0; 488 mmax = sc->props & MIDI_PROP_OUT_INTR ? 1 : MIDI_MAX_WRITE; 489 s = splaudio(); 490 if (sc->pbus && !intr) { 491 DPRINTFN(4, ("midi_start_output: busy\n")); 492 splx(s); 493 return 0; 494 } 495 sc->pbus = 1; 496 for (i = 0; i < mmax && mb->used > 0 && !error; i++) { 497 outp = mb->outp; 498 splx(s); 499 DPRINTFN(4, ("midi_start_output: %p i=%d, data=0x%02x\n", 500 sc, i, *outp)); 501 #ifdef MIDI_SAVE 502 midisave.buf[midicnt] = *outp; 503 midicnt = (midicnt + 1) % MIDI_SAVE_SIZE; 504 #endif 505 error = sc->hw_if->output(sc->hw_hdl, *outp++); 506 if (outp >= mb->end) 507 outp = mb->start; 508 s = splaudio(); 509 mb->outp = outp; 510 mb->used--; 511 } 512 midi_wakeup(&sc->wchan); 513 selwakeup(&sc->wsel); 514 if (sc->async) 515 psignal(sc->async, SIGIO); 516 if (mb->used > 0) { 517 if (!(sc->props & MIDI_PROP_OUT_INTR)) 518 timeout(midi_timeout, sc, midi_wait); 519 } else 520 sc->pbus = 0; 521 splx(s); 522 return error; 523 } 524 525 int 526 midiwrite(dev, uio, ioflag) 527 dev_t dev; 528 struct uio *uio; 529 int ioflag; 530 { 531 int unit = MIDIUNIT(dev); 532 struct midi_softc *sc = midi_cd.cd_devs[unit]; 533 struct midi_buffer *mb = &sc->outbuf; 534 int error; 535 u_char *inp; 536 int used, cc, n; 537 int s; 538 539 DPRINTFN(2, ("midiwrite: %p, unit=%d, count=%d\n", sc, unit, 540 uio->uio_resid)); 541 542 error = 0; 543 while (uio->uio_resid > 0 && !error) { 544 s = splaudio(); 545 if (mb->used >= mb->usedhigh) { 546 DPRINTFN(3,("midi_write: sleep used=%d hiwat=%d\n", 547 mb->used, mb->usedhigh)); 548 if (ioflag & IO_NDELAY) { 549 splx(s); 550 return EWOULDBLOCK; 551 } 552 error = midi_sleep(&sc->wchan, "mid wr"); 553 if (error) { 554 splx(s); 555 return error; 556 } 557 } 558 used = mb->used; 559 inp = mb->inp; 560 splx(s); 561 cc = mb->usedhigh - used; /* maximum to write */ 562 n = mb->end - inp; 563 if (n < cc) 564 cc = n; /* don't write beyond end of buffer */ 565 if (uio->uio_resid < cc) 566 cc = uio->uio_resid; /* and no more than we have */ 567 error = uiomove(inp, cc, uio); 568 #ifdef MIDI_DEBUG 569 if (error) 570 printf("midi_write:(1) uiomove failed %d; " 571 "cc=%d inp=%p\n", 572 error, cc, inp); 573 #endif 574 if (error) 575 break; 576 inp = mb->inp + cc; 577 if (inp >= mb->end) 578 inp = mb->start; 579 s = splaudio(); 580 mb->inp = inp; 581 mb->used += cc; 582 splx(s); 583 error = midi_start_output(sc, 0); 584 } 585 return error; 586 } 587 588 /* 589 * This write routine is only called from sequencer code and expects 590 * a write that is smaller than the MIDI buffer. 591 */ 592 int 593 midi_writebytes(unit, buf, cc) 594 int unit; 595 u_char *buf; 596 int cc; 597 { 598 struct midi_softc *sc = midi_cd.cd_devs[unit]; 599 struct midi_buffer *mb = &sc->outbuf; 600 int n, s; 601 602 DPRINTFN(2, ("midi_writebytes: %p, unit=%d, cc=%d\n", sc, unit, cc)); 603 DPRINTFN(3, ("midi_writebytes: %x %x %x\n",buf[0],buf[1],buf[2])); 604 605 s = splaudio(); 606 if (mb->used + cc >= mb->usedhigh) { 607 splx(s); 608 return (EWOULDBLOCK); 609 } 610 n = mb->end - mb->inp; 611 if (cc < n) 612 n = cc; 613 mb->used += cc; 614 memcpy(mb->inp, buf, n); 615 mb->inp += n; 616 if (mb->inp >= mb->end) { 617 mb->inp = mb->start; 618 cc -= n; 619 if (cc > 0) { 620 memcpy(mb->inp, buf + n, cc); 621 mb->inp += cc; 622 } 623 } 624 splx(s); 625 return (midi_start_output(sc, 0)); 626 } 627 628 int 629 midiioctl(dev, cmd, addr, flag, p) 630 dev_t dev; 631 u_long cmd; 632 caddr_t addr; 633 int flag; 634 struct proc *p; 635 { 636 int unit = MIDIUNIT(dev); 637 struct midi_softc *sc = midi_cd.cd_devs[unit]; 638 struct midi_hw_if *hw = sc->hw_if; 639 int error; 640 641 DPRINTF(("midiioctl: %p cmd=0x%08lx\n", sc, cmd)); 642 error = 0; 643 switch (cmd) { 644 case FIONBIO: 645 /* All handled in the upper FS layer. */ 646 break; 647 648 case FIOASYNC: 649 if (*(int *)addr) { 650 if (sc->async) 651 return EBUSY; 652 sc->async = p; 653 DPRINTF(("midi_ioctl: FIOASYNC %p\n", p)); 654 } else 655 sc->async = 0; 656 break; 657 658 #if 0 659 case MIDI_PRETIME: 660 /* XXX OSS 661 * This should set up a read timeout, but that's 662 * why we have poll(), so there's nothing yet. */ 663 error = EINVAL; 664 break; 665 #endif 666 667 #ifdef MIDI_SAVE 668 case MIDI_GETSAVE: 669 error = copyout(&midisave, *(void **)addr, sizeof midisave); 670 break; 671 #endif 672 673 default: 674 if (hw->ioctl) 675 error = hw->ioctl(sc->hw_hdl, cmd, addr, flag, p); 676 else 677 error = EINVAL; 678 break; 679 } 680 return error; 681 } 682 683 int 684 midipoll(dev, events, p) 685 dev_t dev; 686 int events; 687 struct proc *p; 688 { 689 int unit = MIDIUNIT(dev); 690 struct midi_softc *sc = midi_cd.cd_devs[unit]; 691 int revents = 0; 692 int s = splaudio(); 693 694 DPRINTF(("midipoll: %p events=0x%x\n", sc, events)); 695 696 if (events & (POLLIN | POLLRDNORM)) 697 if (sc->inbuf.used > 0) 698 revents |= events & (POLLIN | POLLRDNORM); 699 700 if (events & (POLLOUT | POLLWRNORM)) 701 if (sc->outbuf.used < sc->outbuf.usedhigh) 702 revents |= events & (POLLOUT | POLLWRNORM); 703 704 if (revents == 0) { 705 if (events & (POLLIN | POLLRDNORM)) 706 selrecord(p, &sc->rsel); 707 708 if (events & (POLLOUT | POLLWRNORM)) 709 selrecord(p, &sc->wsel); 710 } 711 712 splx(s); 713 return revents; 714 } 715 716 void 717 midi_getinfo(dev, mi) 718 dev_t dev; 719 struct midi_info *mi; 720 { 721 int unit = MIDIUNIT(dev); 722 struct midi_softc *sc; 723 724 if (unit >= midi_cd.cd_ndevs || 725 (sc = midi_cd.cd_devs[unit]) == NULL) 726 return; 727 sc->hw_if->getinfo(sc->hw_hdl, mi); 728 } 729 730 #endif /* NMIDI > 0 */ 731 732 #if NMIDI > 0 || NMIDIBUS > 0 733 734 int audioprint __P((void *, const char *)); 735 736 void 737 midi_attach_mi(mhwp, hdlp, dev) 738 struct midi_hw_if *mhwp; 739 void *hdlp; 740 struct device *dev; 741 { 742 struct audio_attach_args arg; 743 744 #ifdef DIAGNOSTIC 745 if (mhwp == NULL) { 746 printf("midi_attach_mi: NULL\n"); 747 return; 748 } 749 #endif 750 arg.type = AUDIODEV_TYPE_MIDI; 751 arg.hwif = mhwp; 752 arg.hdl = hdlp; 753 (void)config_found(dev, &arg, audioprint); 754 } 755 756 #endif /* NMIDI > 0 || NMIDIBUS > 0 */ 757