1 /* $NetBSD: sequencer.c,v 1.64 2015/08/20 14:40:17 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2008 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) and by Andrew Doran. 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Locking: 34 * 35 * o sc_lock: provides atomic access to all data structures. Taken from 36 * both process and soft interrupt context. 37 * 38 * o sc_dvlock: serializes operations on /dev/sequencer. Taken from 39 * process context. Dropped while waiting for data in sequencerread() 40 * to allow concurrent reads/writes while no data available. 41 * 42 * o sc_isopen: we allow only one concurrent open, only to prevent user 43 * and/or application error. 44 * 45 * o MIDI softc locks. These can be spinlocks and there can be many of 46 * them, because we can open many MIDI devices. We take these only in two 47 * places: when enabling redirection from the MIDI device and when 48 * disabling it (open/close). midiseq_in() is called by the MIDI driver 49 * with its own lock held when passing data into this module. To avoid 50 * lock order and context problems, we package the received message as a 51 * sequencer_pcqitem_t and put onto a producer-consumer queue. A soft 52 * interrupt is scheduled to dequeue and decode the message later where we 53 * can safely acquire the sequencer device's sc_lock. PCQ is lockless for 54 * multiple producer, single consumer settings like this one. 55 */ 56 57 #include <sys/cdefs.h> 58 __KERNEL_RCSID(0, "$NetBSD: sequencer.c,v 1.64 2015/08/20 14:40:17 christos Exp $"); 59 60 #include "sequencer.h" 61 62 #include <sys/param.h> 63 #include <sys/ioctl.h> 64 #include <sys/fcntl.h> 65 #include <sys/vnode.h> 66 #include <sys/select.h> 67 #include <sys/poll.h> 68 #include <sys/kmem.h> 69 #include <sys/proc.h> 70 #include <sys/systm.h> 71 #include <sys/syslog.h> 72 #include <sys/kernel.h> 73 #include <sys/signalvar.h> 74 #include <sys/conf.h> 75 #include <sys/audioio.h> 76 #include <sys/midiio.h> 77 #include <sys/device.h> 78 #include <sys/intr.h> 79 #include <sys/atomic.h> 80 #include <sys/pcq.h> 81 #include <sys/vnode.h> 82 #include <sys/kauth.h> 83 84 #include <dev/midi_if.h> 85 #include <dev/midivar.h> 86 #include <dev/sequencervar.h> 87 88 #include "ioconf.h" 89 90 #define ADDTIMEVAL(a, b) ( \ 91 (a)->tv_sec += (b)->tv_sec, \ 92 (a)->tv_usec += (b)->tv_usec, \ 93 (a)->tv_usec > 1000000 ? ((a)->tv_sec++, (a)->tv_usec -= 1000000) : 0\ 94 ) 95 96 #define SUBTIMEVAL(a, b) ( \ 97 (a)->tv_sec -= (b)->tv_sec, \ 98 (a)->tv_usec -= (b)->tv_usec, \ 99 (a)->tv_usec < 0 ? ((a)->tv_sec--, (a)->tv_usec += 1000000) : 0\ 100 ) 101 102 #ifdef AUDIO_DEBUG 103 #define DPRINTF(x) if (sequencerdebug) printf x 104 #define DPRINTFN(n,x) if (sequencerdebug >= (n)) printf x 105 int sequencerdebug = 0; 106 #else 107 #define DPRINTF(x) 108 #define DPRINTFN(n,x) 109 #endif 110 111 #define SEQ_NOTE_MAX 128 112 #define SEQ_NOTE_XXX 255 113 114 #define RECALC_USPERDIV(t) \ 115 ((t)->usperdiv = 60*1000000L/((t)->tempo_beatpermin*(t)->timebase_divperbeat)) 116 117 typedef union sequencer_pcqitem { 118 void *qi_ptr; 119 char qi_msg[4]; 120 } sequencer_pcqitem_t; 121 122 static void seq_reset(struct sequencer_softc *); 123 static int seq_do_command(struct sequencer_softc *, seq_event_t *); 124 static int seq_do_chnvoice(struct sequencer_softc *, seq_event_t *); 125 static int seq_do_chncommon(struct sequencer_softc *, seq_event_t *); 126 static void seq_timer_waitabs(struct sequencer_softc *, uint32_t); 127 static int seq_do_timing(struct sequencer_softc *, seq_event_t *); 128 static int seq_do_local(struct sequencer_softc *, seq_event_t *); 129 static int seq_do_sysex(struct sequencer_softc *, seq_event_t *); 130 static int seq_do_fullsize(struct sequencer_softc *, seq_event_t *, struct uio *); 131 static int seq_input_event(struct sequencer_softc *, seq_event_t *); 132 static int seq_drain(struct sequencer_softc *); 133 static void seq_startoutput(struct sequencer_softc *); 134 static void seq_timeout(void *); 135 static int seq_to_new(seq_event_t *, struct uio *); 136 static void seq_softintr(void *); 137 138 static int midiseq_out(struct midi_dev *, u_char *, u_int, int); 139 static struct midi_dev *midiseq_open(int, int); 140 static void midiseq_close(struct midi_dev *); 141 static void midiseq_reset(struct midi_dev *); 142 static int midiseq_noteon(struct midi_dev *, int, int, seq_event_t *); 143 static int midiseq_noteoff(struct midi_dev *, int, int, seq_event_t *); 144 static int midiseq_keypressure(struct midi_dev *, int, int, seq_event_t *); 145 static int midiseq_pgmchange(struct midi_dev *, int, seq_event_t *); 146 static int midiseq_chnpressure(struct midi_dev *, int, seq_event_t *); 147 static int midiseq_ctlchange(struct midi_dev *, int, seq_event_t *); 148 static int midiseq_pitchbend(struct midi_dev *, int, seq_event_t *); 149 static int midiseq_loadpatch(struct midi_dev *, struct sysex_info *, struct uio *); 150 void midiseq_in(struct midi_dev *, u_char *, int); 151 152 static dev_type_open(sequenceropen); 153 static dev_type_close(sequencerclose); 154 static dev_type_read(sequencerread); 155 static dev_type_write(sequencerwrite); 156 static dev_type_ioctl(sequencerioctl); 157 static dev_type_poll(sequencerpoll); 158 static dev_type_kqfilter(sequencerkqfilter); 159 160 const struct cdevsw sequencer_cdevsw = { 161 .d_open = sequenceropen, 162 .d_close = sequencerclose, 163 .d_read = sequencerread, 164 .d_write = sequencerwrite, 165 .d_ioctl = sequencerioctl, 166 .d_stop = nostop, 167 .d_tty = notty, 168 .d_poll = sequencerpoll, 169 .d_mmap = nommap, 170 .d_kqfilter = sequencerkqfilter, 171 .d_discard = nodiscard, 172 .d_flag = D_OTHER | D_MPSAFE 173 }; 174 static LIST_HEAD(, sequencer_softc) sequencers = LIST_HEAD_INITIALIZER(sequencers); 175 static kmutex_t sequencer_lock; 176 177 static void 178 sequencerdestroy(struct sequencer_softc *sc) 179 { 180 callout_halt(&sc->sc_callout, &sc->lock); 181 callout_destroy(&sc->sc_callout); 182 softint_disestablish(sc->sih); 183 cv_destroy(&sc->rchan); 184 cv_destroy(&sc->wchan); 185 cv_destroy(&sc->lchan); 186 if (sc->pcq) 187 pcq_destroy(sc->pcq); 188 kmem_free(sc, sizeof(*sc)); 189 } 190 191 static struct sequencer_softc * 192 sequencercreate(int unit) 193 { 194 struct sequencer_softc *sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); 195 if (sc == NULL) { 196 #ifdef DIAGNOSTIC 197 printf("%s: out of memory\n", __func__); 198 #endif 199 return NULL; 200 } 201 sc->sc_unit = unit; 202 callout_init(&sc->sc_callout, CALLOUT_MPSAFE); 203 sc->sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE, 204 seq_softintr, sc); 205 mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE); 206 cv_init(&sc->rchan, "midiseqr"); 207 cv_init(&sc->wchan, "midiseqw"); 208 cv_init(&sc->lchan, "midiseql"); 209 sc->pcq = pcq_create(SEQ_MAXQ, KM_SLEEP); 210 if (sc->pcq == NULL) { 211 sequencerdestroy(sc); 212 return NULL; 213 } 214 return sc; 215 } 216 217 218 static struct sequencer_softc * 219 sequencerget(int unit) 220 { 221 struct sequencer_softc *sc; 222 if (unit < 0) { 223 #ifdef DIAGNOSTIC 224 panic("%s: unit %d!", __func__, unit); 225 #endif 226 return NULL; 227 } 228 mutex_enter(&sequencer_lock); 229 LIST_FOREACH(sc, &sequencers, sc_link) { 230 if (sc->sc_unit == unit) { 231 mutex_exit(&sequencer_lock); 232 return sc; 233 } 234 } 235 mutex_exit(&sequencer_lock); 236 if ((sc = sequencercreate(unit)) == NULL) 237 return NULL; 238 mutex_enter(&sequencer_lock); 239 LIST_INSERT_HEAD(&sequencers, sc, sc_link); 240 mutex_exit(&sequencer_lock); 241 return sc; 242 } 243 244 #ifdef notyet 245 static void 246 sequencerput(struct sequencer_softc *sc) 247 { 248 mutex_enter(&sequencer_lock); 249 LIST_REMOVE(sc, sc_link); 250 mutex_exit(&sequencer_lock); 251 sequencerdestroy(sc); 252 } 253 #endif 254 255 void 256 sequencerattach(int n) 257 { 258 mutex_init(&sequencer_lock, MUTEX_DEFAULT, IPL_NONE); 259 } 260 261 /* 262 * Release reference to device acquired with sequencer_enter(). 263 */ 264 static void 265 sequencer_exit(struct sequencer_softc *sc) 266 { 267 268 sc->dvlock--; 269 cv_broadcast(&sc->lchan); 270 mutex_exit(&sc->lock); 271 } 272 273 /* 274 * Look up sequencer device and acquire locks for device access. 275 */ 276 static int 277 sequencer_enter(dev_t dev, struct sequencer_softc **scp) 278 { 279 struct sequencer_softc *sc; 280 281 /* First, find the device and take sc_lock. */ 282 if ((sc = sequencerget(SEQUENCERUNIT(dev))) == NULL) 283 return ENXIO; 284 mutex_enter(&sc->lock); 285 while (sc->dvlock) { 286 cv_wait(&sc->lchan, &sc->lock); 287 } 288 sc->dvlock++; 289 if (sc->dying) { 290 sequencer_exit(sc); 291 return EIO; 292 } 293 *scp = sc; 294 return 0; 295 } 296 297 static int 298 sequenceropen(dev_t dev, int flags, int ifmt, struct lwp *l) 299 { 300 struct sequencer_softc *sc; 301 struct midi_dev *md; 302 struct midi_softc *msc; 303 int error, unit, mdno; 304 305 DPRINTF(("sequenceropen\n")); 306 307 if ((error = sequencer_enter(dev, &sc)) != 0) 308 return error; 309 if (sc->isopen != 0) { 310 sequencer_exit(sc); 311 return EBUSY; 312 } 313 314 if (SEQ_IS_OLD(SEQUENCERUNIT(dev))) 315 sc->mode = SEQ_OLD; 316 else 317 sc->mode = SEQ_NEW; 318 sc->isopen++; 319 sc->flags = flags & (FREAD|FWRITE); 320 sc->pbus = 0; 321 sc->async = 0; 322 sc->input_stamp = ~0; 323 324 sc->nmidi = 0; 325 sc->ndevs = midi_unit_count(); 326 sc->timer.timebase_divperbeat = 100; 327 sc->timer.tempo_beatpermin = 60; 328 RECALC_USPERDIV(&sc->timer); 329 sc->timer.divs_lastevent = sc->timer.divs_lastchange = 0; 330 microtime(&sc->timer.reftime); 331 332 SEQ_QINIT(&sc->inq); 333 SEQ_QINIT(&sc->outq); 334 sc->lowat = SEQ_MAXQ / 2; 335 336 if (sc->ndevs > 0) { 337 mutex_exit(&sc->lock); 338 sc->devs = kmem_alloc(sc->ndevs * sizeof(struct midi_dev *), 339 KM_SLEEP); 340 for (unit = 0; unit < sc->ndevs; unit++) { 341 md = midiseq_open(unit, flags); 342 if (md) { 343 sc->devs[sc->nmidi++] = md; 344 md->seq = sc; 345 md->doingsysex = 0; 346 DPRINTF(("%s: midi unit %d opened as seq %p\n", 347 __func__, unit, md)); 348 } else { 349 DPRINTF(("%s: midi unit %d not opened as seq\n", 350 __func__, unit)); 351 } 352 } 353 mutex_enter(&sc->lock); 354 } else { 355 sc->devs = NULL; 356 } 357 358 /* Only now redirect input from MIDI devices. */ 359 for (mdno = 0; mdno < sc->nmidi; mdno++) { 360 extern struct cfdriver midi_cd; 361 362 msc = device_lookup_private(&midi_cd, sc->devs[mdno]->unit); 363 if (msc) { 364 mutex_enter(msc->lock); 365 msc->seqopen = 1; 366 mutex_exit(msc->lock); 367 } 368 } 369 370 seq_reset(sc); 371 sequencer_exit(sc); 372 373 DPRINTF(("%s: mode=%d, nmidi=%d\n", __func__, sc->mode, sc->nmidi)); 374 return 0; 375 } 376 377 static int 378 seq_drain(struct sequencer_softc *sc) 379 { 380 int error; 381 382 KASSERT(mutex_owned(&sc->lock)); 383 384 DPRINTFN(3, ("seq_drain: %p, len=%d\n", sc, SEQ_QLEN(&sc->outq))); 385 seq_startoutput(sc); 386 error = 0; 387 while (!SEQ_QEMPTY(&sc->outq) && !error) 388 error = cv_timedwait_sig(&sc->wchan, &sc->lock, 60*hz); 389 return (error); 390 } 391 392 static void 393 seq_timeout(void *addr) 394 { 395 struct sequencer_softc *sc = addr; 396 proc_t *p; 397 pid_t pid; 398 399 DPRINTFN(4, ("seq_timeout: %p\n", sc)); 400 401 mutex_enter(&sc->lock); 402 if (sc->timeout == 0) { 403 mutex_spin_exit(&sc->lock); 404 return; 405 } 406 sc->timeout = 0; 407 seq_startoutput(sc); 408 if (SEQ_QLEN(&sc->outq) >= sc->lowat) { 409 mutex_exit(&sc->lock); 410 return; 411 } 412 cv_broadcast(&sc->wchan); 413 selnotify(&sc->wsel, 0, NOTE_SUBMIT); 414 if ((pid = sc->async) != 0) { 415 mutex_enter(proc_lock); 416 if ((p = proc_find(pid)) != NULL) 417 psignal(p, SIGIO); 418 mutex_exit(proc_lock); 419 } 420 mutex_exit(&sc->lock); 421 } 422 423 static void 424 seq_startoutput(struct sequencer_softc *sc) 425 { 426 struct sequencer_queue *q = &sc->outq; 427 seq_event_t cmd; 428 429 KASSERT(mutex_owned(&sc->lock)); 430 431 if (sc->timeout) 432 return; 433 DPRINTFN(4, ("seq_startoutput: %p, len=%d\n", sc, SEQ_QLEN(q))); 434 while (!SEQ_QEMPTY(q) && !sc->timeout) { 435 SEQ_QGET(q, cmd); 436 seq_do_command(sc, &cmd); 437 } 438 } 439 440 static int 441 sequencerclose(dev_t dev, int flags, int ifmt, struct lwp *l) 442 { 443 struct sequencer_softc *sc; 444 struct midi_softc *msc; 445 int unit, error; 446 447 DPRINTF(("%s: %"PRIx64"\n", __func__, dev)); 448 449 if ((error = sequencer_enter(dev, &sc)) != 0) 450 return error; 451 seq_drain(sc); 452 if (sc->timeout) { 453 callout_halt(&sc->sc_callout, &sc->lock); 454 sc->timeout = 0; 455 } 456 /* Bin input from MIDI devices. */ 457 for (unit = 0; unit < sc->nmidi; unit++) { 458 extern struct cfdriver midi_cd; 459 460 msc = device_lookup_private(&midi_cd, unit); 461 if (msc) { 462 mutex_enter(msc->lock); 463 msc->seqopen = 0; 464 mutex_exit(msc->lock); 465 } 466 } 467 mutex_exit(&sc->lock); 468 469 for (unit = 0; unit < sc->nmidi; unit++) 470 if (sc->devs[unit] != NULL) 471 midiseq_close(sc->devs[unit]); 472 if (sc->devs != NULL) { 473 KASSERT(sc->ndevs > 0); 474 kmem_free(sc->devs, sc->ndevs * sizeof(struct midi_dev *)); 475 sc->devs = NULL; 476 } 477 478 mutex_enter(&sc->lock); 479 sc->isopen = 0; 480 sequencer_exit(sc); 481 482 DPRINTF(("%s: %"PRIx64" done\n", __func__, dev)); 483 484 return (0); 485 } 486 487 static int 488 seq_input_event(struct sequencer_softc *sc, seq_event_t *cmd) 489 { 490 struct sequencer_queue *q; 491 492 KASSERT(mutex_owned(&sc->lock)); 493 494 DPRINTFN(2, ("seq_input_event: %02x %02x %02x %02x %02x " 495 "%02x %02x %02x\n", cmd->tag, 496 cmd->unknown.byte[0], cmd->unknown.byte[1], 497 cmd->unknown.byte[2], cmd->unknown.byte[3], 498 cmd->unknown.byte[4], cmd->unknown.byte[5], 499 cmd->unknown.byte[6])); 500 q = &sc->inq; 501 if (SEQ_QFULL(q)) 502 return (ENOMEM); 503 SEQ_QPUT(q, *cmd); 504 cv_broadcast(&sc->rchan); 505 selnotify(&sc->rsel, 0, NOTE_SUBMIT); 506 if (sc->async != 0) { 507 proc_t *p; 508 509 mutex_enter(proc_lock); 510 if ((p = proc_find(sc->async)) != NULL) 511 psignal(p, SIGIO); 512 mutex_exit(proc_lock); 513 } 514 return 0; 515 } 516 517 static void 518 seq_softintr(void *addr) 519 { 520 struct sequencer_softc *sc; 521 struct timeval now; 522 seq_event_t ev; 523 int status, chan, unit; 524 sequencer_pcqitem_t qi; 525 u_long t; 526 527 sc = addr; 528 529 mutex_enter(&sc->lock); 530 531 qi.qi_ptr = pcq_get(sc->pcq); 532 if (qi.qi_ptr == NULL) { 533 mutex_exit(&sc->lock); 534 return; 535 } 536 KASSERT((qi.qi_msg[3] & 0x80) != 0); 537 unit = qi.qi_msg[3] & ~0x80; 538 status = MIDI_GET_STATUS(qi.qi_msg[0]); 539 chan = MIDI_GET_CHAN(qi.qi_msg[0]); 540 switch (status) { 541 case MIDI_NOTEON: /* midi(4) always canonicalizes hidden note-off */ 542 ev = SEQ_MK_CHN(NOTEON, .device=unit, .channel=chan, 543 .key=qi.qi_msg[1], .velocity=qi.qi_msg[2]); 544 break; 545 case MIDI_NOTEOFF: 546 ev = SEQ_MK_CHN(NOTEOFF, .device=unit, .channel=chan, 547 .key=qi.qi_msg[1], .velocity=qi.qi_msg[2]); 548 break; 549 case MIDI_KEY_PRESSURE: 550 ev = SEQ_MK_CHN(KEY_PRESSURE, .device=unit, .channel=chan, 551 .key=qi.qi_msg[1], .pressure=qi.qi_msg[2]); 552 break; 553 case MIDI_CTL_CHANGE: /* XXX not correct for MSB */ 554 ev = SEQ_MK_CHN(CTL_CHANGE, .device=unit, .channel=chan, 555 .controller=qi.qi_msg[1], .value=qi.qi_msg[2]); 556 break; 557 case MIDI_PGM_CHANGE: 558 ev = SEQ_MK_CHN(PGM_CHANGE, .device=unit, .channel=chan, 559 .program=qi.qi_msg[1]); 560 break; 561 case MIDI_CHN_PRESSURE: 562 ev = SEQ_MK_CHN(CHN_PRESSURE, .device=unit, .channel=chan, 563 .pressure=qi.qi_msg[1]); 564 break; 565 case MIDI_PITCH_BEND: 566 ev = SEQ_MK_CHN(PITCH_BEND, .device=unit, .channel=chan, 567 .value=(qi.qi_msg[1] & 0x7f) | ((qi.qi_msg[2] & 0x7f) << 7)); 568 break; 569 default: /* this is now the point where MIDI_ACKs disappear */ 570 mutex_exit(&sc->lock); 571 return; 572 } 573 microtime(&now); 574 if (!sc->timer.running) 575 now = sc->timer.stoptime; 576 SUBTIMEVAL(&now, &sc->timer.reftime); 577 t = now.tv_sec * 1000000 + now.tv_usec; 578 t /= sc->timer.usperdiv; 579 t += sc->timer.divs_lastchange; 580 if (t != sc->input_stamp) { 581 seq_input_event(sc, &SEQ_MK_TIMING(WAIT_ABS, .divisions=t)); 582 sc->input_stamp = t; /* XXX what happens if timer is reset? */ 583 } 584 seq_input_event(sc, &ev); 585 mutex_exit(&sc->lock); 586 } 587 588 static int 589 sequencerread(dev_t dev, struct uio *uio, int ioflag) 590 { 591 struct sequencer_softc *sc; 592 struct sequencer_queue *q; 593 seq_event_t ev; 594 int error; 595 596 DPRINTFN(2, ("sequencerread: %"PRIx64", count=%d, ioflag=%x\n", 597 dev, (int)uio->uio_resid, ioflag)); 598 599 if ((error = sequencer_enter(dev, &sc)) != 0) 600 return error; 601 q = &sc->inq; 602 603 if (sc->mode == SEQ_OLD) { 604 sequencer_exit(sc); 605 DPRINTFN(-1,("sequencerread: old read\n")); 606 return EINVAL; /* XXX unimplemented */ 607 } 608 while (SEQ_QEMPTY(q)) { 609 if (ioflag & IO_NDELAY) { 610 error = EWOULDBLOCK; 611 break; 612 } 613 /* Drop lock to allow concurrent read/write. */ 614 KASSERT(sc->dvlock != 0); 615 sc->dvlock--; 616 error = cv_wait_sig(&sc->rchan, &sc->lock); 617 while (sc->dvlock != 0) { 618 cv_wait(&sc->lchan, &sc->lock); 619 } 620 sc->dvlock++; 621 if (error) { 622 break; 623 } 624 } 625 while (uio->uio_resid >= sizeof(ev) && !error && !SEQ_QEMPTY(q)) { 626 SEQ_QGET(q, ev); 627 mutex_exit(&sc->lock); 628 error = uiomove(&ev, sizeof(ev), uio); 629 mutex_enter(&sc->lock); 630 } 631 sequencer_exit(sc); 632 return error; 633 } 634 635 static int 636 sequencerwrite(dev_t dev, struct uio *uio, int ioflag) 637 { 638 struct sequencer_softc *sc; 639 struct sequencer_queue *q; 640 int error; 641 seq_event_t cmdbuf; 642 int size; 643 644 DPRINTFN(2, ("sequencerwrite: %"PRIx64", count=%d\n", dev, 645 (int)uio->uio_resid)); 646 647 if ((error = sequencer_enter(dev, &sc)) != 0) 648 return error; 649 q = &sc->outq; 650 651 size = sc->mode == SEQ_NEW ? sizeof cmdbuf : SEQOLD_CMDSIZE; 652 while (uio->uio_resid >= size && error == 0) { 653 mutex_exit(&sc->lock); 654 error = uiomove(&cmdbuf, size, uio); 655 if (error == 0) { 656 if (sc->mode == SEQ_OLD && seq_to_new(&cmdbuf, uio)) { 657 mutex_enter(&sc->lock); 658 continue; 659 } 660 if (cmdbuf.tag == SEQ_FULLSIZE) { 661 /* We do it like OSS does, asynchronously */ 662 error = seq_do_fullsize(sc, &cmdbuf, uio); 663 if (error == 0) { 664 mutex_enter(&sc->lock); 665 continue; 666 } 667 } 668 } 669 mutex_enter(&sc->lock); 670 if (error != 0) { 671 break; 672 } 673 while (SEQ_QFULL(q)) { 674 seq_startoutput(sc); 675 if (SEQ_QFULL(q)) { 676 if (ioflag & IO_NDELAY) { 677 error = EWOULDBLOCK; 678 break; 679 } 680 error = cv_wait_sig(&sc->wchan, &sc->lock); 681 if (error) { 682 break; 683 } 684 } 685 } 686 if (error == 0) { 687 SEQ_QPUT(q, cmdbuf); 688 } 689 } 690 if (error == 0) { 691 seq_startoutput(sc); 692 } else { 693 DPRINTFN(2, ("sequencerwrite: error=%d\n", error)); 694 } 695 sequencer_exit(sc); 696 return error; 697 } 698 699 static int 700 sequencerioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 701 { 702 struct sequencer_softc *sc; 703 struct synth_info *si; 704 struct midi_dev *md; 705 int devno, error, t; 706 struct timeval now; 707 u_long tx; 708 709 DPRINTFN(2, ("sequencerioctl: %"PRIx64" cmd=0x%08lx\n", dev, cmd)); 710 711 if ((error = sequencer_enter(dev, &sc)) != 0) 712 return error; 713 switch (cmd) { 714 case FIONBIO: 715 /* All handled in the upper FS layer. */ 716 break; 717 718 case FIOASYNC: 719 if (*(int *)addr) { 720 if (sc->async != 0) 721 return EBUSY; 722 sc->async = curproc->p_pid; 723 DPRINTF(("%s: FIOASYNC %d\n", __func__, 724 sc->async)); 725 } else { 726 sc->async = 0; 727 } 728 break; 729 730 case SEQUENCER_RESET: 731 seq_reset(sc); 732 break; 733 734 case SEQUENCER_PANIC: 735 seq_reset(sc); 736 /* Do more? OSS doesn't */ 737 break; 738 739 case SEQUENCER_SYNC: 740 if (sc->flags != FREAD) 741 seq_drain(sc); 742 break; 743 744 case SEQUENCER_INFO: 745 si = (struct synth_info*)addr; 746 devno = si->device; 747 if (devno < 0 || devno >= sc->nmidi) { 748 error = EINVAL; 749 break; 750 } 751 md = sc->devs[devno]; 752 strncpy(si->name, md->name, sizeof si->name); 753 si->synth_type = SYNTH_TYPE_MIDI; 754 si->synth_subtype = md->subtype; 755 si->nr_voices = md->nr_voices; 756 si->instr_bank_size = md->instr_bank_size; 757 si->capabilities = md->capabilities; 758 break; 759 760 case SEQUENCER_NRSYNTHS: 761 *(int *)addr = sc->nmidi; 762 break; 763 764 case SEQUENCER_NRMIDIS: 765 *(int *)addr = sc->nmidi; 766 break; 767 768 case SEQUENCER_OUTOFBAND: 769 DPRINTFN(3, ("sequencer_ioctl: OOB=%02x %02x %02x %02x %02x %02x %02x %02x\n", 770 *(u_char *)addr, *((u_char *)addr+1), 771 *((u_char *)addr+2), *((u_char *)addr+3), 772 *((u_char *)addr+4), *((u_char *)addr+5), 773 *((u_char *)addr+6), *((u_char *)addr+7))); 774 if ((sc->flags & FWRITE) == 0) { 775 error = EBADF; 776 } else { 777 error = seq_do_command(sc, (seq_event_t *)addr); 778 } 779 break; 780 781 case SEQUENCER_TMR_TIMEBASE: 782 t = *(int *)addr; 783 if (t < 1) 784 t = 1; 785 if (t > 10000) 786 t = 10000; 787 *(int *)addr = t; 788 sc->timer.timebase_divperbeat = t; 789 sc->timer.divs_lastchange = sc->timer.divs_lastevent; 790 microtime(&sc->timer.reftime); 791 RECALC_USPERDIV(&sc->timer); 792 break; 793 794 case SEQUENCER_TMR_START: 795 error = seq_do_timing(sc, &SEQ_MK_TIMING(START)); 796 break; 797 798 case SEQUENCER_TMR_STOP: 799 error = seq_do_timing(sc, &SEQ_MK_TIMING(STOP)); 800 break; 801 802 case SEQUENCER_TMR_CONTINUE: 803 error = seq_do_timing(sc, &SEQ_MK_TIMING(CONTINUE)); 804 break; 805 806 case SEQUENCER_TMR_TEMPO: 807 error = seq_do_timing(sc, 808 &SEQ_MK_TIMING(TEMPO, .bpm=*(int *)addr)); 809 RECALC_USPERDIV(&sc->timer); 810 if (error == 0) 811 *(int *)addr = sc->timer.tempo_beatpermin; 812 break; 813 814 case SEQUENCER_TMR_SOURCE: 815 *(int *)addr = SEQUENCER_TMR_INTERNAL; 816 break; 817 818 case SEQUENCER_TMR_METRONOME: 819 /* noop */ 820 break; 821 822 case SEQUENCER_THRESHOLD: 823 t = SEQ_MAXQ - *(int *)addr / sizeof (seq_event_rec); 824 if (t < 1) 825 t = 1; 826 if (t > SEQ_MAXQ) 827 t = SEQ_MAXQ; 828 sc->lowat = t; 829 break; 830 831 case SEQUENCER_CTRLRATE: 832 *(int *)addr = (sc->timer.tempo_beatpermin 833 *sc->timer.timebase_divperbeat + 30) / 60; 834 break; 835 836 case SEQUENCER_GETTIME: 837 microtime(&now); 838 SUBTIMEVAL(&now, &sc->timer.reftime); 839 tx = now.tv_sec * 1000000 + now.tv_usec; 840 tx /= sc->timer.usperdiv; 841 tx += sc->timer.divs_lastchange; 842 *(int *)addr = tx; 843 break; 844 845 default: 846 DPRINTFN(-1,("sequencer_ioctl: unimpl %08lx\n", cmd)); 847 error = EINVAL; 848 break; 849 } 850 sequencer_exit(sc); 851 852 return error; 853 } 854 855 static int 856 sequencerpoll(dev_t dev, int events, struct lwp *l) 857 { 858 struct sequencer_softc *sc; 859 int revents = 0; 860 if ((sc = sequencerget(SEQUENCERUNIT(dev))) == NULL) 861 return ENXIO; 862 863 DPRINTF(("%s: %p events=0x%x\n", __func__, sc, events)); 864 865 mutex_enter(&sc->lock); 866 if (events & (POLLIN | POLLRDNORM)) 867 if ((sc->flags&FREAD) && !SEQ_QEMPTY(&sc->inq)) 868 revents |= events & (POLLIN | POLLRDNORM); 869 870 if (events & (POLLOUT | POLLWRNORM)) 871 if ((sc->flags&FWRITE) && SEQ_QLEN(&sc->outq) < sc->lowat) 872 revents |= events & (POLLOUT | POLLWRNORM); 873 874 if (revents == 0) { 875 if ((sc->flags&FREAD) && (events & (POLLIN | POLLRDNORM))) 876 selrecord(l, &sc->rsel); 877 878 if ((sc->flags&FWRITE) && (events & (POLLOUT | POLLWRNORM))) 879 selrecord(l, &sc->wsel); 880 } 881 mutex_exit(&sc->lock); 882 883 return revents; 884 } 885 886 static void 887 filt_sequencerrdetach(struct knote *kn) 888 { 889 struct sequencer_softc *sc = kn->kn_hook; 890 891 mutex_enter(&sc->lock); 892 SLIST_REMOVE(&sc->rsel.sel_klist, kn, knote, kn_selnext); 893 mutex_exit(&sc->lock); 894 } 895 896 static int 897 filt_sequencerread(struct knote *kn, long hint) 898 { 899 struct sequencer_softc *sc = kn->kn_hook; 900 int rv; 901 902 if (hint != NOTE_SUBMIT) { 903 mutex_enter(&sc->lock); 904 } 905 if (SEQ_QEMPTY(&sc->inq)) { 906 rv = 0; 907 } else { 908 kn->kn_data = sizeof(seq_event_rec); 909 rv = 1; 910 } 911 if (hint != NOTE_SUBMIT) { 912 mutex_exit(&sc->lock); 913 } 914 return rv; 915 } 916 917 static const struct filterops sequencerread_filtops = 918 { 1, NULL, filt_sequencerrdetach, filt_sequencerread }; 919 920 static void 921 filt_sequencerwdetach(struct knote *kn) 922 { 923 struct sequencer_softc *sc = kn->kn_hook; 924 925 mutex_enter(&sc->lock); 926 SLIST_REMOVE(&sc->wsel.sel_klist, kn, knote, kn_selnext); 927 mutex_exit(&sc->lock); 928 } 929 930 static int 931 filt_sequencerwrite(struct knote *kn, long hint) 932 { 933 struct sequencer_softc *sc = kn->kn_hook; 934 int rv; 935 936 if (hint != NOTE_SUBMIT) { 937 mutex_enter(&sc->lock); 938 } 939 if (SEQ_QLEN(&sc->outq) >= sc->lowat) { 940 rv = 0; 941 } else { 942 kn->kn_data = sizeof(seq_event_rec); 943 rv = 1; 944 } 945 if (hint != NOTE_SUBMIT) { 946 mutex_exit(&sc->lock); 947 } 948 return rv; 949 } 950 951 static const struct filterops sequencerwrite_filtops = 952 { 1, NULL, filt_sequencerwdetach, filt_sequencerwrite }; 953 954 static int 955 sequencerkqfilter(dev_t dev, struct knote *kn) 956 { 957 struct sequencer_softc *sc; 958 struct klist *klist; 959 if ((sc = sequencerget(SEQUENCERUNIT(dev))) == NULL) 960 return ENXIO; 961 962 switch (kn->kn_filter) { 963 case EVFILT_READ: 964 klist = &sc->rsel.sel_klist; 965 kn->kn_fop = &sequencerread_filtops; 966 break; 967 968 case EVFILT_WRITE: 969 klist = &sc->wsel.sel_klist; 970 kn->kn_fop = &sequencerwrite_filtops; 971 break; 972 973 default: 974 return (EINVAL); 975 } 976 977 kn->kn_hook = sc; 978 979 mutex_enter(&sc->lock); 980 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 981 mutex_exit(&sc->lock); 982 983 return (0); 984 } 985 986 static void 987 seq_reset(struct sequencer_softc *sc) 988 { 989 int i, chn; 990 struct midi_dev *md; 991 992 KASSERT(mutex_owned(&sc->lock)); 993 994 if (!(sc->flags & FWRITE)) 995 return; 996 for (i = 0; i < sc->nmidi; i++) { 997 md = sc->devs[i]; 998 midiseq_reset(md); 999 for (chn = 0; chn < MAXCHAN; chn++) { 1000 midiseq_ctlchange(md, chn, &SEQ_MK_CHN(CTL_CHANGE, 1001 .controller=MIDI_CTRL_NOTES_OFF)); 1002 midiseq_ctlchange(md, chn, &SEQ_MK_CHN(CTL_CHANGE, 1003 .controller=MIDI_CTRL_RESET)); 1004 midiseq_pitchbend(md, chn, &SEQ_MK_CHN(PITCH_BEND, 1005 .value=MIDI_BEND_NEUTRAL)); 1006 } 1007 } 1008 } 1009 1010 static int 1011 seq_do_command(struct sequencer_softc *sc, seq_event_t *b) 1012 { 1013 int dev; 1014 1015 KASSERT(mutex_owned(&sc->lock)); 1016 1017 DPRINTFN(4, ("seq_do_command: %p cmd=0x%02x\n", sc, b->timing.op)); 1018 1019 switch(b->tag) { 1020 case SEQ_LOCAL: 1021 return seq_do_local(sc, b); 1022 case SEQ_TIMING: 1023 return seq_do_timing(sc, b); 1024 case SEQ_CHN_VOICE: 1025 return seq_do_chnvoice(sc, b); 1026 case SEQ_CHN_COMMON: 1027 return seq_do_chncommon(sc, b); 1028 case SEQ_SYSEX: 1029 return seq_do_sysex(sc, b); 1030 /* COMPAT */ 1031 case SEQOLD_MIDIPUTC: 1032 dev = b->putc.device; 1033 if (dev < 0 || dev >= sc->nmidi) 1034 return (ENXIO); 1035 return midiseq_out(sc->devs[dev], &b->putc.byte, 1, 0); 1036 default: 1037 DPRINTFN(-1,("seq_do_command: unimpl command %02x\n", b->tag)); 1038 return (EINVAL); 1039 } 1040 } 1041 1042 static int 1043 seq_do_chnvoice(struct sequencer_softc *sc, seq_event_t *b) 1044 { 1045 int dev; 1046 int error; 1047 struct midi_dev *md; 1048 1049 KASSERT(mutex_owned(&sc->lock)); 1050 1051 dev = b->voice.device; 1052 if (dev < 0 || dev >= sc->nmidi || 1053 b->voice.channel > 15 || 1054 b->voice.key >= SEQ_NOTE_MAX) 1055 return ENXIO; 1056 md = sc->devs[dev]; 1057 switch(b->voice.op) { 1058 case MIDI_NOTEON: /* no need to special-case hidden noteoff here */ 1059 error = midiseq_noteon(md, b->voice.channel, b->voice.key, b); 1060 break; 1061 case MIDI_NOTEOFF: 1062 error = midiseq_noteoff(md, b->voice.channel, b->voice.key, b); 1063 break; 1064 case MIDI_KEY_PRESSURE: 1065 error = midiseq_keypressure(md, 1066 b->voice.channel, b->voice.key, b); 1067 break; 1068 default: 1069 DPRINTFN(-1,("seq_do_chnvoice: unimpl command %02x\n", 1070 b->voice.op)); 1071 error = EINVAL; 1072 break; 1073 } 1074 return error; 1075 } 1076 1077 static int 1078 seq_do_chncommon(struct sequencer_softc *sc, seq_event_t *b) 1079 { 1080 int dev; 1081 int error; 1082 struct midi_dev *md; 1083 1084 KASSERT(mutex_owned(&sc->lock)); 1085 1086 dev = b->common.device; 1087 if (dev < 0 || dev >= sc->nmidi || 1088 b->common.channel > 15) 1089 return ENXIO; 1090 md = sc->devs[dev]; 1091 DPRINTFN(2,("seq_do_chncommon: %02x\n", b->common.op)); 1092 1093 error = 0; 1094 switch(b->common.op) { 1095 case MIDI_PGM_CHANGE: 1096 error = midiseq_pgmchange(md, b->common.channel, b); 1097 break; 1098 case MIDI_CTL_CHANGE: 1099 error = midiseq_ctlchange(md, b->common.channel, b); 1100 break; 1101 case MIDI_PITCH_BEND: 1102 error = midiseq_pitchbend(md, b->common.channel, b); 1103 break; 1104 case MIDI_CHN_PRESSURE: 1105 error = midiseq_chnpressure(md, b->common.channel, b); 1106 break; 1107 default: 1108 DPRINTFN(-1,("seq_do_chncommon: unimpl command %02x\n", 1109 b->common.op)); 1110 error = EINVAL; 1111 break; 1112 } 1113 return error; 1114 } 1115 1116 static int 1117 seq_do_local(struct sequencer_softc *sc, seq_event_t *b) 1118 { 1119 1120 KASSERT(mutex_owned(&sc->lock)); 1121 1122 return (EINVAL); 1123 } 1124 1125 static int 1126 seq_do_sysex(struct sequencer_softc *sc, seq_event_t *b) 1127 { 1128 int dev, i; 1129 struct midi_dev *md; 1130 uint8_t *bf = b->sysex.buffer; 1131 1132 KASSERT(mutex_owned(&sc->lock)); 1133 1134 dev = b->sysex.device; 1135 if (dev < 0 || dev >= sc->nmidi) 1136 return (ENXIO); 1137 DPRINTF(("%s: dev=%d\n", __func__, dev)); 1138 md = sc->devs[dev]; 1139 1140 if (!md->doingsysex) { 1141 midiseq_out(md, (uint8_t[]){MIDI_SYSEX_START}, 1, 0); 1142 md->doingsysex = 1; 1143 } 1144 1145 for (i = 0; i < 6 && bf[i] != 0xff; i++) 1146 ; 1147 midiseq_out(md, bf, i, 0); 1148 if (i < 6 || (i > 0 && bf[i-1] == MIDI_SYSEX_END)) 1149 md->doingsysex = 0; 1150 return 0; 1151 } 1152 1153 static void 1154 seq_timer_waitabs(struct sequencer_softc *sc, uint32_t divs) 1155 { 1156 struct timeval when; 1157 long long usec; 1158 struct syn_timer *t; 1159 int ticks; 1160 1161 KASSERT(mutex_owned(&sc->lock)); 1162 1163 t = &sc->timer; 1164 t->divs_lastevent = divs; 1165 divs -= t->divs_lastchange; 1166 usec = (long long)divs * (long long)t->usperdiv; /* convert to usec */ 1167 when.tv_sec = usec / 1000000; 1168 when.tv_usec = usec % 1000000; 1169 DPRINTFN(4, ("seq_timer_waitabs: adjdivs=%d, sleep when=%"PRId64".%06"PRId64, 1170 divs, when.tv_sec, (uint64_t)when.tv_usec)); 1171 ADDTIMEVAL(&when, &t->reftime); /* abstime for end */ 1172 ticks = tvhzto(&when); 1173 DPRINTFN(4, (" when+start=%"PRId64".%06"PRId64", tick=%d\n", 1174 when.tv_sec, (uint64_t)when.tv_usec, ticks)); 1175 if (ticks > 0) { 1176 #ifdef DIAGNOSTIC 1177 if (ticks > 20 * hz) { 1178 /* Waiting more than 20s */ 1179 printf("seq_timer_waitabs: funny ticks=%d, " 1180 "usec=%lld\n", ticks, usec); 1181 } 1182 #endif 1183 sc->timeout = 1; 1184 callout_reset(&sc->sc_callout, ticks, 1185 seq_timeout, sc); 1186 } 1187 #ifdef SEQUENCER_DEBUG 1188 else if (tick < 0) 1189 DPRINTF(("%s: ticks = %d\n", __func__, ticks)); 1190 #endif 1191 } 1192 1193 static int 1194 seq_do_timing(struct sequencer_softc *sc, seq_event_t *b) 1195 { 1196 struct syn_timer *t = &sc->timer; 1197 struct timeval when; 1198 int error; 1199 1200 KASSERT(mutex_owned(&sc->lock)); 1201 1202 error = 0; 1203 switch(b->timing.op) { 1204 case TMR_WAIT_REL: 1205 seq_timer_waitabs(sc, 1206 b->t_WAIT_REL.divisions + t->divs_lastevent); 1207 break; 1208 case TMR_WAIT_ABS: 1209 seq_timer_waitabs(sc, b->t_WAIT_ABS.divisions); 1210 break; 1211 case TMR_START: 1212 microtime(&t->reftime); 1213 t->divs_lastevent = t->divs_lastchange = 0; 1214 t->running = 1; 1215 break; 1216 case TMR_STOP: 1217 microtime(&t->stoptime); 1218 t->running = 0; 1219 break; 1220 case TMR_CONTINUE: 1221 if (t->running) 1222 break; 1223 microtime(&when); 1224 SUBTIMEVAL(&when, &t->stoptime); 1225 ADDTIMEVAL(&t->reftime, &when); 1226 t->running = 1; 1227 break; 1228 case TMR_TEMPO: 1229 /* bpm is unambiguously MIDI clocks per minute / 24 */ 1230 /* (24 MIDI clocks are usually but not always a quarter note) */ 1231 if (b->t_TEMPO.bpm < 8) /* where are these limits specified? */ 1232 t->tempo_beatpermin = 8; 1233 else if (b->t_TEMPO.bpm > 360) /* ? */ 1234 t->tempo_beatpermin = 360; 1235 else 1236 t->tempo_beatpermin = b->t_TEMPO.bpm; 1237 t->divs_lastchange = t->divs_lastevent; 1238 microtime(&t->reftime); 1239 RECALC_USPERDIV(t); 1240 break; 1241 case TMR_ECHO: 1242 error = seq_input_event(sc, b); 1243 break; 1244 case TMR_RESET: 1245 t->divs_lastevent = t->divs_lastchange = 0; 1246 microtime(&t->reftime); 1247 break; 1248 case TMR_SPP: 1249 case TMR_TIMESIG: 1250 DPRINTF(("%s: unimplemented %02x\n", __func__, b->timing.op)); 1251 error = EINVAL; /* not quite accurate... */ 1252 break; 1253 default: 1254 DPRINTF(("%s: unknown %02x\n", __func__, b->timing.op)); 1255 error = EINVAL; 1256 break; 1257 } 1258 return (error); 1259 } 1260 1261 static int 1262 seq_do_fullsize(struct sequencer_softc *sc, seq_event_t *b, struct uio *uio) 1263 { 1264 struct sysex_info sysex; 1265 u_int dev; 1266 1267 #ifdef DIAGNOSTIC 1268 if (sizeof(seq_event_rec) != SEQ_SYSEX_HDRSIZE) { 1269 printf("seq_do_fullsize: sysex size ??\n"); 1270 return EINVAL; 1271 } 1272 #endif 1273 memcpy(&sysex, b, sizeof sysex); 1274 dev = sysex.device_no; 1275 if (/* dev < 0 || */ dev >= sc->nmidi) 1276 return (ENXIO); 1277 DPRINTFN(2, ("seq_do_fullsize: fmt=%04x, dev=%d, len=%d\n", 1278 sysex.key, dev, sysex.len)); 1279 return (midiseq_loadpatch(sc->devs[dev], &sysex, uio)); 1280 } 1281 1282 /* 1283 * Convert an old sequencer event to a new one. 1284 * NOTE: on entry, *ev may contain valid data only in the first 4 bytes. 1285 * That may be true even on exit (!) in the case of SEQOLD_MIDIPUTC; the 1286 * caller will only look at the first bytes in that case anyway. Ugly? Sure. 1287 */ 1288 static int 1289 seq_to_new(seq_event_t *ev, struct uio *uio) 1290 { 1291 int cmd, chan, note, parm; 1292 uint32_t tmp_delay; 1293 int error; 1294 uint8_t *bfp; 1295 1296 cmd = ev->tag; 1297 bfp = ev->unknown.byte; 1298 chan = *bfp++; 1299 note = *bfp++; 1300 parm = *bfp++; 1301 DPRINTFN(3, ("seq_to_new: 0x%02x %d %d %d\n", cmd, chan, note, parm)); 1302 1303 if (cmd >= 0x80) { 1304 /* Fill the event record */ 1305 if (uio->uio_resid >= sizeof *ev - SEQOLD_CMDSIZE) { 1306 error = uiomove(bfp, sizeof *ev - SEQOLD_CMDSIZE, uio); 1307 if (error) 1308 return error; 1309 } else 1310 return EINVAL; 1311 } 1312 1313 switch(cmd) { 1314 case SEQOLD_NOTEOFF: 1315 /* 1316 * What's with the SEQ_NOTE_XXX? In OSS this seems to have 1317 * been undocumented magic for messing with the overall volume 1318 * of a 'voice', equated precariously with 'channel' and 1319 * pretty much unimplementable except by directly frobbing a 1320 * synth chip. For us, who treat everything as interfaced over 1321 * MIDI, this will just be unceremoniously discarded as 1322 * invalid in midiseq_noteoff, making the whole event an 1323 * elaborate no-op, and that doesn't seem to be any different 1324 * from what happens on linux with a MIDI-interfaced device, 1325 * by the way. The moral is ... use the new /dev/music API, ok? 1326 */ 1327 *ev = SEQ_MK_CHN(NOTEOFF, .device=0, .channel=chan, 1328 .key=SEQ_NOTE_XXX, .velocity=parm); 1329 break; 1330 case SEQOLD_NOTEON: 1331 *ev = SEQ_MK_CHN(NOTEON, 1332 .device=0, .channel=chan, .key=note, .velocity=parm); 1333 break; 1334 case SEQOLD_WAIT: 1335 /* 1336 * This event cannot even /exist/ on non-littleendian machines, 1337 * and so help me, that's exactly the way OSS defined it. 1338 * Also, the OSS programmer's guide states (p. 74, v1.11) 1339 * that seqold time units are system clock ticks, unlike 1340 * the new 'divisions' which are determined by timebase. In 1341 * that case we would need to do scaling here - but no such 1342 * behavior is visible in linux either--which also treats this 1343 * value, surprisingly, as an absolute, not relative, time. 1344 * My guess is that this event has gone unused so long that 1345 * nobody could agree we got it wrong no matter what we do. 1346 */ 1347 tmp_delay = *(uint32_t *)ev >> 8; 1348 *ev = SEQ_MK_TIMING(WAIT_ABS, .divisions=tmp_delay); 1349 break; 1350 case SEQOLD_SYNCTIMER: 1351 /* 1352 * The TMR_RESET event is not defined in any OSS materials 1353 * I can find; it may have been invented here just to provide 1354 * an accurate _to_new translation of this event. 1355 */ 1356 *ev = SEQ_MK_TIMING(RESET); 1357 break; 1358 case SEQOLD_PGMCHANGE: 1359 *ev = SEQ_MK_CHN(PGM_CHANGE, 1360 .device=0, .channel=chan, .program=note); 1361 break; 1362 case SEQOLD_MIDIPUTC: 1363 break; /* interpret in normal mode */ 1364 case SEQOLD_ECHO: 1365 case SEQOLD_PRIVATE: 1366 case SEQOLD_EXTENDED: 1367 default: 1368 DPRINTF(("%s: not impl 0x%02x\n", __func__, cmd)); 1369 return EINVAL; 1370 /* In case new-style events show up */ 1371 case SEQ_TIMING: 1372 case SEQ_CHN_VOICE: 1373 case SEQ_CHN_COMMON: 1374 case SEQ_FULLSIZE: 1375 break; 1376 } 1377 return 0; 1378 } 1379 1380 /**********************************************/ 1381 1382 void 1383 midiseq_in(struct midi_dev *md, u_char *msg, int len) 1384 { 1385 struct sequencer_softc *sc; 1386 sequencer_pcqitem_t qi; 1387 1388 DPRINTFN(2, ("midiseq_in: %p %02x %02x %02x\n", 1389 md, msg[0], msg[1], msg[2])); 1390 1391 sc = md->seq; 1392 1393 qi.qi_msg[0] = msg[0]; 1394 qi.qi_msg[1] = msg[1]; 1395 qi.qi_msg[2] = msg[2]; 1396 qi.qi_msg[3] = md->unit | 0x80; /* ensure non-zero value of qi_ptr */ 1397 pcq_put(sc->pcq, qi.qi_ptr); 1398 softint_schedule(sc->sih); 1399 } 1400 1401 static struct midi_dev * 1402 midiseq_open(int unit, int flags) 1403 { 1404 extern struct cfdriver midi_cd; 1405 int error; 1406 struct midi_dev *md; 1407 struct midi_softc *sc; 1408 struct midi_info mi; 1409 int major; 1410 dev_t dev; 1411 vnode_t *vp; 1412 int oflags; 1413 1414 major = devsw_name2chr("midi", NULL, 0); 1415 dev = makedev(major, unit); 1416 1417 DPRINTFN(2, ("midiseq_open: %d %d\n", unit, flags)); 1418 1419 error = cdevvp(dev, &vp); 1420 if (error) 1421 return NULL; 1422 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1423 error = VOP_OPEN(vp, flags, kauth_cred_get()); 1424 VOP_UNLOCK(vp); 1425 if (error) { 1426 vrele(vp); 1427 return NULL; 1428 } 1429 1430 /* Only after we have acquired reference via VOP_OPEN(). */ 1431 midi_getinfo(dev, &mi); 1432 oflags = flags; 1433 if ((mi.props & MIDI_PROP_CAN_INPUT) == 0) 1434 flags &= ~FREAD; 1435 if ((flags & (FREAD|FWRITE)) == 0) { 1436 VOP_CLOSE(vp, oflags, kauth_cred_get()); 1437 vrele(vp); 1438 return NULL; 1439 } 1440 1441 sc = device_lookup_private(&midi_cd, unit); 1442 md = kmem_zalloc(sizeof(*md), KM_SLEEP); 1443 md->unit = unit; 1444 md->name = mi.name; 1445 md->subtype = 0; 1446 md->nr_voices = 128; /* XXX */ 1447 md->instr_bank_size = 128; /* XXX */ 1448 md->vp = vp; 1449 if (mi.props & MIDI_PROP_CAN_INPUT) 1450 md->capabilities |= SYNTH_CAP_INPUT; 1451 sc->seq_md = md; 1452 return (md); 1453 } 1454 1455 static void 1456 midiseq_close(struct midi_dev *md) 1457 { 1458 DPRINTFN(2, ("midiseq_close: %d\n", md->unit)); 1459 (void)vn_close(md->vp, 0, kauth_cred_get()); 1460 kmem_free(md, sizeof(*md)); 1461 } 1462 1463 static void 1464 midiseq_reset(struct midi_dev *md) 1465 { 1466 /* XXX send GM reset? */ 1467 DPRINTFN(3, ("midiseq_reset: %d\n", md->unit)); 1468 } 1469 1470 static int 1471 midiseq_out(struct midi_dev *md, u_char *bf, u_int cc, int chk) 1472 { 1473 DPRINTFN(5, ("midiseq_out: md=%p, unit=%d, bf[0]=0x%02x, cc=%d\n", 1474 md, md->unit, bf[0], cc)); 1475 1476 /* midi(4) does running status compression where appropriate. */ 1477 return midi_writebytes(md->unit, bf, cc); 1478 } 1479 1480 /* 1481 * If the writing process hands us a hidden note-off in a note-on event, 1482 * we will simply write it that way; no need to special case it here, 1483 * as midi(4) will always canonicalize or compress as appropriate anyway. 1484 */ 1485 static int 1486 midiseq_noteon(struct midi_dev *md, int chan, int key, seq_event_t *ev) 1487 { 1488 return midiseq_out(md, (uint8_t[]){ 1489 MIDI_NOTEON | chan, key, ev->c_NOTEON.velocity & 0x7f}, 3, 1); 1490 } 1491 1492 static int 1493 midiseq_noteoff(struct midi_dev *md, int chan, int key, seq_event_t *ev) 1494 { 1495 return midiseq_out(md, (uint8_t[]){ 1496 MIDI_NOTEOFF | chan, key, ev->c_NOTEOFF.velocity & 0x7f}, 3, 1); 1497 } 1498 1499 static int 1500 midiseq_keypressure(struct midi_dev *md, int chan, int key, seq_event_t *ev) 1501 { 1502 return midiseq_out(md, (uint8_t[]){ 1503 MIDI_KEY_PRESSURE | chan, key, 1504 ev->c_KEY_PRESSURE.pressure & 0x7f}, 3, 1); 1505 } 1506 1507 static int 1508 midiseq_pgmchange(struct midi_dev *md, int chan, seq_event_t *ev) 1509 { 1510 if (ev->c_PGM_CHANGE.program > 127) 1511 return EINVAL; 1512 return midiseq_out(md, (uint8_t[]){ 1513 MIDI_PGM_CHANGE | chan, ev->c_PGM_CHANGE.program}, 2, 1); 1514 } 1515 1516 static int 1517 midiseq_chnpressure(struct midi_dev *md, int chan, seq_event_t *ev) 1518 { 1519 if (ev->c_CHN_PRESSURE.pressure > 127) 1520 return EINVAL; 1521 return midiseq_out(md, (uint8_t[]){ 1522 MIDI_CHN_PRESSURE | chan, ev->c_CHN_PRESSURE.pressure}, 2, 1); 1523 } 1524 1525 static int 1526 midiseq_ctlchange(struct midi_dev *md, int chan, seq_event_t *ev) 1527 { 1528 if (ev->c_CTL_CHANGE.controller > 127) 1529 return EINVAL; 1530 return midiseq_out( md, (uint8_t[]){ 1531 MIDI_CTL_CHANGE | chan, ev->c_CTL_CHANGE.controller, 1532 ev->c_CTL_CHANGE.value & 0x7f /* XXX this is SO wrong */ 1533 }, 3, 1); 1534 } 1535 1536 static int 1537 midiseq_pitchbend(struct midi_dev *md, int chan, seq_event_t *ev) 1538 { 1539 return midiseq_out(md, (uint8_t[]){ 1540 MIDI_PITCH_BEND | chan, 1541 ev->c_PITCH_BEND.value & 0x7f, 1542 (ev->c_PITCH_BEND.value >> 7) & 0x7f}, 3, 1); 1543 } 1544 1545 static int 1546 midiseq_loadpatch(struct midi_dev *md, 1547 struct sysex_info *sysex, struct uio *uio) 1548 { 1549 struct sequencer_softc *sc; 1550 u_char c, bf[128]; 1551 int i, cc, error; 1552 1553 if (sysex->key != SEQ_SYSEX_PATCH) { 1554 DPRINTFN(-1,("midiseq_loadpatch: bad patch key 0x%04x\n", 1555 sysex->key)); 1556 return (EINVAL); 1557 } 1558 if (uio->uio_resid < sysex->len) 1559 /* adjust length, should be an error */ 1560 sysex->len = uio->uio_resid; 1561 1562 DPRINTFN(2, ("midiseq_loadpatch: len=%d\n", sysex->len)); 1563 if (sysex->len == 0) 1564 return EINVAL; 1565 error = uiomove(&c, 1, uio); 1566 if (error) 1567 return error; 1568 if (c != MIDI_SYSEX_START) /* must start like this */ 1569 return EINVAL; 1570 sc = md->seq; 1571 mutex_enter(&sc->lock); 1572 error = midiseq_out(md, &c, 1, 0); 1573 mutex_exit(&sc->lock); 1574 if (error) 1575 return error; 1576 --sysex->len; 1577 while (sysex->len > 0) { 1578 cc = sysex->len; 1579 if (cc > sizeof bf) 1580 cc = sizeof bf; 1581 error = uiomove(bf, cc, uio); 1582 if (error) 1583 break; 1584 for(i = 0; i < cc && !MIDI_IS_STATUS(bf[i]); i++) 1585 ; 1586 /* 1587 * XXX midi(4)'s buffer might not accommodate this, and the 1588 * function will not block us (though in this case we have 1589 * a process and could in principle block). 1590 */ 1591 mutex_enter(&sc->lock); 1592 error = midiseq_out(md, bf, i, 0); 1593 mutex_exit(&sc->lock); 1594 if (error) 1595 break; 1596 sysex->len -= i; 1597 if (i != cc) 1598 break; 1599 } 1600 /* 1601 * Any leftover data in uio is rubbish; 1602 * the SYSEX should be one write ending in SYSEX_END. 1603 */ 1604 uio->uio_resid = 0; 1605 c = MIDI_SYSEX_END; 1606 mutex_enter(&sc->lock); 1607 error = midiseq_out(md, &c, 1, 0); 1608 mutex_exit(&sc->lock); 1609 return error; 1610 } 1611 1612 #include "midi.h" 1613 #if NMIDI == 0 1614 static dev_type_open(midiopen); 1615 static dev_type_close(midiclose); 1616 1617 const struct cdevsw midi_cdevsw = { 1618 .d_open = midiopen, 1619 .d_close = midiclose, 1620 .d_read = noread, 1621 .d_write = nowrite, 1622 .d_ioctl = noioctl, 1623 .d_stop = nostop, 1624 .d_tty = notty, 1625 .d_poll = nopoll, 1626 .d_mmap = nommap, 1627 .d_kqfilter = nokqfilter, 1628 .d_discard = nodiscard, 1629 .d_flag = D_OTHER | D_MPSAFE 1630 }; 1631 1632 /* 1633 * If someone has a sequencer, but no midi devices there will 1634 * be unresolved references, so we provide little stubs. 1635 */ 1636 1637 int 1638 midi_unit_count(void) 1639 { 1640 return (0); 1641 } 1642 1643 static int 1644 midiopen(dev_t dev, int flags, int ifmt, struct lwp *l) 1645 { 1646 return (ENXIO); 1647 } 1648 1649 struct cfdriver midi_cd; 1650 1651 void 1652 midi_getinfo(dev_t dev, struct midi_info *mi) 1653 { 1654 mi->name = "Dummy MIDI device"; 1655 mi->props = 0; 1656 } 1657 1658 static int 1659 midiclose(dev_t dev, int flags, int ifmt, struct lwp *l) 1660 { 1661 return (ENXIO); 1662 } 1663 1664 int 1665 midi_writebytes(int unit, u_char *bf, int cc) 1666 { 1667 return (ENXIO); 1668 } 1669 #endif /* NMIDI == 0 */ 1670