1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003 Mathew Kanner 5 * Copyright (c) 1993 Hannu Savolainen 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * The sequencer personality manager. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/ioccom.h> 40 41 #include <sys/filio.h> 42 #include <sys/lock.h> 43 #include <sys/sockio.h> 44 #include <sys/fcntl.h> 45 #include <sys/proc.h> 46 #include <sys/sysctl.h> 47 48 #include <sys/kernel.h> /* for DATA_SET */ 49 50 #include <sys/module.h> 51 #include <sys/conf.h> 52 #include <sys/file.h> 53 #include <sys/uio.h> 54 #include <sys/syslog.h> 55 #include <sys/errno.h> 56 #include <sys/malloc.h> 57 #include <sys/bus.h> 58 #include <machine/resource.h> 59 #include <machine/bus.h> 60 #include <machine/clock.h> /* for DELAY */ 61 #include <sys/soundcard.h> 62 #include <sys/rman.h> 63 #include <sys/mman.h> 64 #include <sys/poll.h> 65 #include <sys/mutex.h> 66 #include <sys/condvar.h> 67 #include <sys/kthread.h> 68 #include <sys/unistd.h> 69 #include <sys/selinfo.h> 70 71 #ifdef HAVE_KERNEL_OPTION_HEADERS 72 #include "opt_snd.h" 73 #endif 74 75 #include <dev/sound/midi/midi.h> 76 #include <dev/sound/midi/midiq.h> 77 #include "synth_if.h" 78 79 #include <dev/sound/midi/sequencer.h> 80 81 #define TMR_TIMERBASE 13 82 83 #define SND_DEV_SEQ 1 /* Sequencer output /dev/sequencer (FM 84 * synthesizer and MIDI output) */ 85 #define SND_DEV_MUSIC 8 /* /dev/music, level 2 interface */ 86 87 /* Length of a sequencer event. */ 88 #define EV_SZ 8 89 #define IEV_SZ 8 90 91 /* Lookup modes */ 92 #define LOOKUP_EXIST (0) 93 #define LOOKUP_OPEN (1) 94 #define LOOKUP_CLOSE (2) 95 96 #define PCMMKMINOR(u, d, c) \ 97 ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f)) 98 #define MIDIMKMINOR(u, d, c) PCMMKMINOR(u, d, c) 99 #define MIDIUNIT(y) ((dev2unit(y) >> 4) & 0x0f) 100 #define MIDIDEV(y) (dev2unit(y) & 0x0f) 101 102 /* These are the entries to the sequencer driver. */ 103 static d_open_t mseq_open; 104 static d_close_t mseq_close; 105 static d_ioctl_t mseq_ioctl; 106 static d_read_t mseq_read; 107 static d_write_t mseq_write; 108 static d_poll_t mseq_poll; 109 110 static struct cdevsw seq_cdevsw = { 111 .d_version = D_VERSION, 112 .d_open = mseq_open, 113 .d_close = mseq_close, 114 .d_read = mseq_read, 115 .d_write = mseq_write, 116 .d_ioctl = mseq_ioctl, 117 .d_poll = mseq_poll, 118 .d_name = "sequencer", 119 }; 120 121 struct seq_softc { 122 KOBJ_FIELDS; 123 124 struct mtx seq_lock, q_lock; 125 struct cv empty_cv, reset_cv, in_cv, out_cv, state_cv, th_cv; 126 127 MIDIQ_HEAD(, u_char) in_q, out_q; 128 129 u_long flags; 130 /* Flags (protected by flag_mtx of mididev_info) */ 131 int fflags; /* Access mode */ 132 int music; 133 134 int out_water; /* Sequence output threshould */ 135 snd_sync_parm sync_parm; /* AIOSYNC parameter set */ 136 struct thread *sync_thread; /* AIOSYNCing thread */ 137 struct selinfo in_sel, out_sel; 138 int midi_number; 139 struct cdev *seqdev, *musicdev; 140 int unit; 141 int maxunits; 142 kobj_t *midis; 143 int *midi_flags; 144 kobj_t mapper; 145 void *mapper_cookie; 146 struct timeval timerstop, timersub; 147 int timerbase, tempo; 148 int timerrun; 149 int done; 150 int playing; 151 int recording; 152 int busy; 153 int pre_event_timeout; 154 int waiting; 155 }; 156 157 /* 158 * Module specific stuff, including how many sequecers 159 * we currently own. 160 */ 161 162 SYSCTL_NODE(_hw_midi, OID_AUTO, seq, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 163 "Midi sequencer"); 164 165 int seq_debug; 166 /* XXX: should this be moved into debug.midi? */ 167 SYSCTL_INT(_hw_midi_seq, OID_AUTO, debug, CTLFLAG_RW, &seq_debug, 0, ""); 168 169 midi_cmdtab cmdtab_seqevent[] = { 170 {SEQ_NOTEOFF, "SEQ_NOTEOFF"}, 171 {SEQ_NOTEON, "SEQ_NOTEON"}, 172 {SEQ_WAIT, "SEQ_WAIT"}, 173 {SEQ_PGMCHANGE, "SEQ_PGMCHANGE"}, 174 {SEQ_SYNCTIMER, "SEQ_SYNCTIMER"}, 175 {SEQ_MIDIPUTC, "SEQ_MIDIPUTC"}, 176 {SEQ_DRUMON, "SEQ_DRUMON"}, 177 {SEQ_DRUMOFF, "SEQ_DRUMOFF"}, 178 {SEQ_ECHO, "SEQ_ECHO"}, 179 {SEQ_AFTERTOUCH, "SEQ_AFTERTOUCH"}, 180 {SEQ_CONTROLLER, "SEQ_CONTROLLER"}, 181 {SEQ_BALANCE, "SEQ_BALANCE"}, 182 {SEQ_VOLMODE, "SEQ_VOLMODE"}, 183 {SEQ_FULLSIZE, "SEQ_FULLSIZE"}, 184 {SEQ_PRIVATE, "SEQ_PRIVATE"}, 185 {SEQ_EXTENDED, "SEQ_EXTENDED"}, 186 {EV_SEQ_LOCAL, "EV_SEQ_LOCAL"}, 187 {EV_TIMING, "EV_TIMING"}, 188 {EV_CHN_COMMON, "EV_CHN_COMMON"}, 189 {EV_CHN_VOICE, "EV_CHN_VOICE"}, 190 {EV_SYSEX, "EV_SYSEX"}, 191 {-1, NULL}, 192 }; 193 194 midi_cmdtab cmdtab_seqioctl[] = { 195 {SNDCTL_SEQ_RESET, "SNDCTL_SEQ_RESET"}, 196 {SNDCTL_SEQ_SYNC, "SNDCTL_SEQ_SYNC"}, 197 {SNDCTL_SYNTH_INFO, "SNDCTL_SYNTH_INFO"}, 198 {SNDCTL_SEQ_CTRLRATE, "SNDCTL_SEQ_CTRLRATE"}, 199 {SNDCTL_SEQ_GETOUTCOUNT, "SNDCTL_SEQ_GETOUTCOUNT"}, 200 {SNDCTL_SEQ_GETINCOUNT, "SNDCTL_SEQ_GETINCOUNT"}, 201 {SNDCTL_SEQ_PERCMODE, "SNDCTL_SEQ_PERCMODE"}, 202 {SNDCTL_FM_LOAD_INSTR, "SNDCTL_FM_LOAD_INSTR"}, 203 {SNDCTL_SEQ_TESTMIDI, "SNDCTL_SEQ_TESTMIDI"}, 204 {SNDCTL_SEQ_RESETSAMPLES, "SNDCTL_SEQ_RESETSAMPLES"}, 205 {SNDCTL_SEQ_NRSYNTHS, "SNDCTL_SEQ_NRSYNTHS"}, 206 {SNDCTL_SEQ_NRMIDIS, "SNDCTL_SEQ_NRMIDIS"}, 207 {SNDCTL_SEQ_GETTIME, "SNDCTL_SEQ_GETTIME"}, 208 {SNDCTL_MIDI_INFO, "SNDCTL_MIDI_INFO"}, 209 {SNDCTL_SEQ_THRESHOLD, "SNDCTL_SEQ_THRESHOLD"}, 210 {SNDCTL_SYNTH_MEMAVL, "SNDCTL_SYNTH_MEMAVL"}, 211 {SNDCTL_FM_4OP_ENABLE, "SNDCTL_FM_4OP_ENABLE"}, 212 {SNDCTL_PMGR_ACCESS, "SNDCTL_PMGR_ACCESS"}, 213 {SNDCTL_SEQ_PANIC, "SNDCTL_SEQ_PANIC"}, 214 {SNDCTL_SEQ_OUTOFBAND, "SNDCTL_SEQ_OUTOFBAND"}, 215 {SNDCTL_TMR_TIMEBASE, "SNDCTL_TMR_TIMEBASE"}, 216 {SNDCTL_TMR_START, "SNDCTL_TMR_START"}, 217 {SNDCTL_TMR_STOP, "SNDCTL_TMR_STOP"}, 218 {SNDCTL_TMR_CONTINUE, "SNDCTL_TMR_CONTINUE"}, 219 {SNDCTL_TMR_TEMPO, "SNDCTL_TMR_TEMPO"}, 220 {SNDCTL_TMR_SOURCE, "SNDCTL_TMR_SOURCE"}, 221 {SNDCTL_TMR_METRONOME, "SNDCTL_TMR_METRONOME"}, 222 {SNDCTL_TMR_SELECT, "SNDCTL_TMR_SELECT"}, 223 {SNDCTL_MIDI_PRETIME, "SNDCTL_MIDI_PRETIME"}, 224 {AIONWRITE, "AIONWRITE"}, 225 {AIOGSIZE, "AIOGSIZE"}, 226 {AIOSSIZE, "AIOSSIZE"}, 227 {AIOGFMT, "AIOGFMT"}, 228 {AIOSFMT, "AIOSFMT"}, 229 {AIOGMIX, "AIOGMIX"}, 230 {AIOSMIX, "AIOSMIX"}, 231 {AIOSTOP, "AIOSTOP"}, 232 {AIOSYNC, "AIOSYNC"}, 233 {AIOGCAP, "AIOGCAP"}, 234 {-1, NULL}, 235 }; 236 237 midi_cmdtab cmdtab_timer[] = { 238 {TMR_WAIT_REL, "TMR_WAIT_REL"}, 239 {TMR_WAIT_ABS, "TMR_WAIT_ABS"}, 240 {TMR_STOP, "TMR_STOP"}, 241 {TMR_START, "TMR_START"}, 242 {TMR_CONTINUE, "TMR_CONTINUE"}, 243 {TMR_TEMPO, "TMR_TEMPO"}, 244 {TMR_ECHO, "TMR_ECHO"}, 245 {TMR_CLOCK, "TMR_CLOCK"}, 246 {TMR_SPP, "TMR_SPP"}, 247 {TMR_TIMESIG, "TMR_TIMESIG"}, 248 {-1, NULL}, 249 }; 250 251 midi_cmdtab cmdtab_seqcv[] = { 252 {MIDI_NOTEOFF, "MIDI_NOTEOFF"}, 253 {MIDI_NOTEON, "MIDI_NOTEON"}, 254 {MIDI_KEY_PRESSURE, "MIDI_KEY_PRESSURE"}, 255 {-1, NULL}, 256 }; 257 258 midi_cmdtab cmdtab_seqccmn[] = { 259 {MIDI_CTL_CHANGE, "MIDI_CTL_CHANGE"}, 260 {MIDI_PGM_CHANGE, "MIDI_PGM_CHANGE"}, 261 {MIDI_CHN_PRESSURE, "MIDI_CHN_PRESSURE"}, 262 {MIDI_PITCH_BEND, "MIDI_PITCH_BEND"}, 263 {MIDI_SYSTEM_PREFIX, "MIDI_SYSTEM_PREFIX"}, 264 {-1, NULL}, 265 }; 266 267 #ifndef KOBJMETHOD_END 268 #define KOBJMETHOD_END { NULL, NULL } 269 #endif 270 271 /* 272 * static const char *mpu401_mprovider(kobj_t obj, struct mpu401 *m); 273 */ 274 275 static kobj_method_t seq_methods[] = { 276 /* KOBJMETHOD(mpu_provider,mpu401_mprovider), */ 277 KOBJMETHOD_END 278 }; 279 280 DEFINE_CLASS(sequencer, seq_methods, 0); 281 282 /* The followings are the local function. */ 283 static int seq_convertold(u_char *event, u_char *out); 284 285 /* 286 * static void seq_midiinput(struct seq_softc * scp, void *md); 287 */ 288 static void seq_reset(struct seq_softc *scp); 289 static int seq_sync(struct seq_softc *scp); 290 291 static int seq_processevent(struct seq_softc *scp, u_char *event); 292 293 static int seq_timing(struct seq_softc *scp, u_char *event); 294 static int seq_local(struct seq_softc *scp, u_char *event); 295 296 static int seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event); 297 static int seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event); 298 static int seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event); 299 300 static int seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md); 301 void seq_copytoinput(struct seq_softc *scp, u_char *event, int len); 302 int seq_modevent(module_t mod, int type, void *data); 303 struct seq_softc *seqs[10]; 304 static struct mtx seqinfo_mtx; 305 static u_long nseq = 0; 306 307 static void timer_start(struct seq_softc *t); 308 static void timer_stop(struct seq_softc *t); 309 static void timer_setvals(struct seq_softc *t, int tempo, int timerbase); 310 static void timer_wait(struct seq_softc *t, int ticks, int wait_abs); 311 static int timer_now(struct seq_softc *t); 312 313 static void 314 timer_start(struct seq_softc *t) 315 { 316 t->timerrun = 1; 317 getmicrotime(&t->timersub); 318 } 319 320 static void 321 timer_continue(struct seq_softc *t) 322 { 323 struct timeval now; 324 325 if (t->timerrun == 1) 326 return; 327 t->timerrun = 1; 328 getmicrotime(&now); 329 timevalsub(&now, &t->timerstop); 330 timevaladd(&t->timersub, &now); 331 } 332 333 static void 334 timer_stop(struct seq_softc *t) 335 { 336 t->timerrun = 0; 337 getmicrotime(&t->timerstop); 338 } 339 340 static void 341 timer_setvals(struct seq_softc *t, int tempo, int timerbase) 342 { 343 t->tempo = tempo; 344 t->timerbase = timerbase; 345 } 346 347 static void 348 timer_wait(struct seq_softc *t, int ticks, int wait_abs) 349 { 350 struct timeval now, when; 351 int ret; 352 unsigned long long i; 353 354 while (t->timerrun == 0) { 355 SEQ_DEBUG(2, printf("Timer wait when timer isn't running\n")); 356 /* 357 * The old sequencer used timeouts that only increased 358 * the timer when the timer was running. 359 * Hence the sequencer would stick (?) if the 360 * timer was disabled. 361 */ 362 cv_wait(&t->reset_cv, &t->seq_lock); 363 if (t->playing == 0) 364 return; 365 } 366 367 i = ticks * 60ull * 1000000ull / (t->tempo * t->timerbase); 368 369 when.tv_sec = i / 1000000; 370 when.tv_usec = i % 1000000; 371 372 #if 0 373 printf("timer_wait tempo %d timerbase %d ticks %d abs %d u_sec %llu\n", 374 t->tempo, t->timerbase, ticks, wait_abs, i); 375 #endif 376 377 if (wait_abs != 0) { 378 getmicrotime(&now); 379 timevalsub(&now, &t->timersub); 380 timevalsub(&when, &now); 381 } 382 if (when.tv_sec < 0 || when.tv_usec < 0) { 383 SEQ_DEBUG(3, 384 printf("seq_timer error negative time %lds.%06lds\n", 385 (long)when.tv_sec, (long)when.tv_usec)); 386 return; 387 } 388 i = when.tv_sec * 1000000ull; 389 i += when.tv_usec; 390 i *= hz; 391 i /= 1000000ull; 392 #if 0 393 printf("seq_timer usec %llu ticks %llu\n", 394 when.tv_sec * 1000000ull + when.tv_usec, i); 395 #endif 396 t->waiting = 1; 397 ret = cv_timedwait(&t->reset_cv, &t->seq_lock, i + 1); 398 t->waiting = 0; 399 400 if (ret != EWOULDBLOCK) 401 SEQ_DEBUG(3, printf("seq_timer didn't timeout\n")); 402 403 } 404 405 static int 406 timer_now(struct seq_softc *t) 407 { 408 struct timeval now; 409 unsigned long long i; 410 int ret; 411 412 if (t->timerrun == 0) 413 now = t->timerstop; 414 else 415 getmicrotime(&now); 416 417 timevalsub(&now, &t->timersub); 418 419 i = now.tv_sec * 1000000ull; 420 i += now.tv_usec; 421 i *= t->timerbase; 422 /* i /= t->tempo; */ 423 i /= 1000000ull; 424 425 ret = i; 426 /* 427 * printf("timer_now: %llu %d\n", i, ret); 428 */ 429 430 return ret; 431 } 432 433 static void 434 seq_eventthread(void *arg) 435 { 436 struct seq_softc *scp = arg; 437 u_char event[EV_SZ]; 438 439 mtx_lock(&scp->seq_lock); 440 SEQ_DEBUG(2, printf("seq_eventthread started\n")); 441 while (scp->done == 0) { 442 restart: 443 while (scp->playing == 0) { 444 cv_wait(&scp->state_cv, &scp->seq_lock); 445 if (scp->done) 446 goto done; 447 } 448 449 while (MIDIQ_EMPTY(scp->out_q)) { 450 cv_broadcast(&scp->empty_cv); 451 cv_wait(&scp->out_cv, &scp->seq_lock); 452 if (scp->playing == 0) 453 goto restart; 454 if (scp->done) 455 goto done; 456 } 457 458 MIDIQ_DEQ(scp->out_q, event, EV_SZ); 459 460 if (MIDIQ_AVAIL(scp->out_q) < scp->out_water) { 461 cv_broadcast(&scp->out_cv); 462 selwakeup(&scp->out_sel); 463 } 464 seq_processevent(scp, event); 465 } 466 467 done: 468 cv_broadcast(&scp->th_cv); 469 mtx_unlock(&scp->seq_lock); 470 SEQ_DEBUG(2, printf("seq_eventthread finished\n")); 471 kproc_exit(0); 472 } 473 474 /* 475 * seq_processevent: This maybe called by the event thread or the IOCTL 476 * handler for queued and out of band events respectively. 477 */ 478 static int 479 seq_processevent(struct seq_softc *scp, u_char *event) 480 { 481 int ret; 482 kobj_t m; 483 484 ret = 0; 485 486 if (event[0] == EV_SEQ_LOCAL) 487 ret = seq_local(scp, event); 488 else if (event[0] == EV_TIMING) 489 ret = seq_timing(scp, event); 490 else if (event[0] != EV_CHN_VOICE && 491 event[0] != EV_CHN_COMMON && 492 event[0] != EV_SYSEX && 493 event[0] != SEQ_MIDIPUTC) { 494 ret = 1; 495 SEQ_DEBUG(2, printf("seq_processevent not known %d\n", 496 event[0])); 497 } else if (seq_fetch_mid(scp, event[1], &m) != 0) { 498 ret = 1; 499 SEQ_DEBUG(2, printf("seq_processevent midi unit not found %d\n", 500 event[1])); 501 } else 502 switch (event[0]) { 503 case EV_CHN_VOICE: 504 ret = seq_chnvoice(scp, m, event); 505 break; 506 case EV_CHN_COMMON: 507 ret = seq_chncommon(scp, m, event); 508 break; 509 case EV_SYSEX: 510 ret = seq_sysex(scp, m, event); 511 break; 512 case SEQ_MIDIPUTC: 513 mtx_unlock(&scp->seq_lock); 514 ret = SYNTH_WRITERAW(m, &event[2], 1); 515 mtx_lock(&scp->seq_lock); 516 break; 517 } 518 return ret; 519 } 520 521 static int 522 seq_addunit(void) 523 { 524 struct seq_softc *scp; 525 int ret; 526 u_char *buf; 527 528 /* Allocate the softc. */ 529 ret = ENOMEM; 530 scp = malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT | M_ZERO); 531 if (scp == NULL) { 532 SEQ_DEBUG(1, printf("seq_addunit: softc allocation failed.\n")); 533 goto err; 534 } 535 kobj_init((kobj_t)scp, &sequencer_class); 536 537 buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO); 538 if (buf == NULL) 539 goto err; 540 MIDIQ_INIT(scp->in_q, buf, EV_SZ * 1024); 541 buf = malloc(sizeof(*buf) * EV_SZ * 1024, M_TEMP, M_NOWAIT | M_ZERO); 542 if (buf == NULL) 543 goto err; 544 MIDIQ_INIT(scp->out_q, buf, EV_SZ * 1024); 545 ret = EINVAL; 546 547 scp->midis = malloc(sizeof(kobj_t) * 32, M_TEMP, M_NOWAIT | M_ZERO); 548 scp->midi_flags = malloc(sizeof(*scp->midi_flags) * 32, M_TEMP, 549 M_NOWAIT | M_ZERO); 550 551 if (scp->midis == NULL || scp->midi_flags == NULL) 552 goto err; 553 554 scp->flags = 0; 555 556 mtx_init(&scp->seq_lock, "seqflq", NULL, 0); 557 cv_init(&scp->state_cv, "seqstate"); 558 cv_init(&scp->empty_cv, "seqempty"); 559 cv_init(&scp->reset_cv, "seqtimer"); 560 cv_init(&scp->out_cv, "seqqout"); 561 cv_init(&scp->in_cv, "seqqin"); 562 cv_init(&scp->th_cv, "seqstart"); 563 564 /* 565 * Init the damn timer 566 */ 567 568 scp->mapper = midimapper_addseq(scp, &scp->unit, &scp->mapper_cookie); 569 if (scp->mapper == NULL) 570 goto err; 571 572 scp->seqdev = make_dev(&seq_cdevsw, 573 MIDIMKMINOR(scp->unit, SND_DEV_SEQ, 0), UID_ROOT, 574 GID_WHEEL, 0666, "sequencer%d", scp->unit); 575 576 scp->musicdev = make_dev(&seq_cdevsw, 577 MIDIMKMINOR(scp->unit, SND_DEV_MUSIC, 0), UID_ROOT, 578 GID_WHEEL, 0666, "music%d", scp->unit); 579 580 if (scp->seqdev == NULL || scp->musicdev == NULL) 581 goto err; 582 /* 583 * TODO: Add to list of sequencers this module provides 584 */ 585 586 ret = 587 kproc_create 588 (seq_eventthread, scp, NULL, RFHIGHPID, 0, 589 "sequencer %02d", scp->unit); 590 591 if (ret) 592 goto err; 593 594 scp->seqdev->si_drv1 = scp->musicdev->si_drv1 = scp; 595 596 SEQ_DEBUG(2, printf("sequencer %d created scp %p\n", scp->unit, scp)); 597 598 ret = 0; 599 600 mtx_lock(&seqinfo_mtx); 601 seqs[nseq++] = scp; 602 mtx_unlock(&seqinfo_mtx); 603 604 goto ok; 605 606 err: 607 if (scp != NULL) { 608 if (scp->seqdev != NULL) 609 destroy_dev(scp->seqdev); 610 if (scp->musicdev != NULL) 611 destroy_dev(scp->musicdev); 612 /* 613 * TODO: Destroy mutex and cv 614 */ 615 if (scp->midis != NULL) 616 free(scp->midis, M_TEMP); 617 if (scp->midi_flags != NULL) 618 free(scp->midi_flags, M_TEMP); 619 if (scp->out_q.b) 620 free(scp->out_q.b, M_TEMP); 621 if (scp->in_q.b) 622 free(scp->in_q.b, M_TEMP); 623 free(scp, M_DEVBUF); 624 } 625 ok: 626 return ret; 627 } 628 629 static int 630 seq_delunit(int unit) 631 { 632 struct seq_softc *scp = seqs[unit]; 633 int i; 634 635 //SEQ_DEBUG(4, printf("seq_delunit: %d\n", unit)); 636 SEQ_DEBUG(1, printf("seq_delunit: 1 \n")); 637 mtx_lock(&scp->seq_lock); 638 639 scp->playing = 0; 640 scp->done = 1; 641 cv_broadcast(&scp->out_cv); 642 cv_broadcast(&scp->state_cv); 643 cv_broadcast(&scp->reset_cv); 644 SEQ_DEBUG(1, printf("seq_delunit: 2 \n")); 645 cv_wait(&scp->th_cv, &scp->seq_lock); 646 SEQ_DEBUG(1, printf("seq_delunit: 3.0 \n")); 647 mtx_unlock(&scp->seq_lock); 648 SEQ_DEBUG(1, printf("seq_delunit: 3.1 \n")); 649 650 cv_destroy(&scp->state_cv); 651 SEQ_DEBUG(1, printf("seq_delunit: 4 \n")); 652 cv_destroy(&scp->empty_cv); 653 SEQ_DEBUG(1, printf("seq_delunit: 5 \n")); 654 cv_destroy(&scp->reset_cv); 655 SEQ_DEBUG(1, printf("seq_delunit: 6 \n")); 656 cv_destroy(&scp->out_cv); 657 SEQ_DEBUG(1, printf("seq_delunit: 7 \n")); 658 cv_destroy(&scp->in_cv); 659 SEQ_DEBUG(1, printf("seq_delunit: 8 \n")); 660 cv_destroy(&scp->th_cv); 661 662 SEQ_DEBUG(1, printf("seq_delunit: 10 \n")); 663 if (scp->seqdev) 664 destroy_dev(scp->seqdev); 665 SEQ_DEBUG(1, printf("seq_delunit: 11 \n")); 666 if (scp->musicdev) 667 destroy_dev(scp->musicdev); 668 SEQ_DEBUG(1, printf("seq_delunit: 12 \n")); 669 scp->seqdev = scp->musicdev = NULL; 670 if (scp->midis != NULL) 671 free(scp->midis, M_TEMP); 672 SEQ_DEBUG(1, printf("seq_delunit: 13 \n")); 673 if (scp->midi_flags != NULL) 674 free(scp->midi_flags, M_TEMP); 675 SEQ_DEBUG(1, printf("seq_delunit: 14 \n")); 676 free(scp->out_q.b, M_TEMP); 677 SEQ_DEBUG(1, printf("seq_delunit: 15 \n")); 678 free(scp->in_q.b, M_TEMP); 679 680 SEQ_DEBUG(1, printf("seq_delunit: 16 \n")); 681 682 mtx_destroy(&scp->seq_lock); 683 SEQ_DEBUG(1, printf("seq_delunit: 17 \n")); 684 free(scp, M_DEVBUF); 685 686 mtx_lock(&seqinfo_mtx); 687 for (i = unit; i < (nseq - 1); i++) 688 seqs[i] = seqs[i + 1]; 689 nseq--; 690 mtx_unlock(&seqinfo_mtx); 691 692 return 0; 693 } 694 695 int 696 seq_modevent(module_t mod, int type, void *data) 697 { 698 int retval, r; 699 700 retval = 0; 701 702 switch (type) { 703 case MOD_LOAD: 704 mtx_init(&seqinfo_mtx, "seqmod", NULL, 0); 705 retval = seq_addunit(); 706 break; 707 708 case MOD_UNLOAD: 709 while (nseq) { 710 r = seq_delunit(nseq - 1); 711 if (r) { 712 retval = r; 713 break; 714 } 715 } 716 if (nseq == 0) { 717 retval = 0; 718 mtx_destroy(&seqinfo_mtx); 719 } 720 break; 721 722 default: 723 break; 724 } 725 726 return retval; 727 } 728 729 static int 730 seq_fetch_mid(struct seq_softc *scp, int unit, kobj_t *md) 731 { 732 733 if (unit >= scp->midi_number || unit < 0) 734 return EINVAL; 735 736 *md = scp->midis[unit]; 737 738 return 0; 739 } 740 741 int 742 mseq_open(struct cdev *i_dev, int flags, int mode, struct thread *td) 743 { 744 struct seq_softc *scp = i_dev->si_drv1; 745 int i; 746 747 if (scp == NULL) 748 return ENXIO; 749 750 SEQ_DEBUG(3, printf("seq_open: scp %p unit %d, flags 0x%x.\n", 751 scp, scp->unit, flags)); 752 753 /* 754 * Mark this device busy. 755 */ 756 757 mtx_lock(&scp->seq_lock); 758 if (scp->busy) { 759 mtx_unlock(&scp->seq_lock); 760 SEQ_DEBUG(2, printf("seq_open: unit %d is busy.\n", scp->unit)); 761 return EBUSY; 762 } 763 scp->fflags = flags; 764 /* 765 if ((scp->fflags & O_NONBLOCK) != 0) 766 scp->flags |= SEQ_F_NBIO; 767 */ 768 scp->music = MIDIDEV(i_dev) == SND_DEV_MUSIC; 769 770 /* 771 * Enumerate the available midi devices 772 */ 773 scp->midi_number = 0; 774 scp->maxunits = midimapper_open(scp->mapper, &scp->mapper_cookie); 775 776 if (scp->maxunits == 0) 777 SEQ_DEBUG(2, printf("seq_open: no midi devices\n")); 778 779 for (i = 0; i < scp->maxunits; i++) { 780 scp->midis[scp->midi_number] = 781 midimapper_fetch_synth(scp->mapper, scp->mapper_cookie, i); 782 if (scp->midis[scp->midi_number]) { 783 if (SYNTH_OPEN(scp->midis[scp->midi_number], scp, 784 scp->fflags) != 0) 785 scp->midis[scp->midi_number] = NULL; 786 else { 787 scp->midi_flags[scp->midi_number] = 788 SYNTH_QUERY(scp->midis[scp->midi_number]); 789 scp->midi_number++; 790 } 791 } 792 } 793 794 timer_setvals(scp, 60, 100); 795 796 timer_start(scp); 797 timer_stop(scp); 798 /* 799 * actually, if we're in rdonly mode, we should start the timer 800 */ 801 /* 802 * TODO: Handle recording now 803 */ 804 805 scp->out_water = MIDIQ_SIZE(scp->out_q) / 2; 806 807 scp->busy = 1; 808 mtx_unlock(&scp->seq_lock); 809 810 SEQ_DEBUG(2, printf("seq_open: opened, mode %s.\n", 811 scp->music ? "music" : "sequencer")); 812 SEQ_DEBUG(2, 813 printf("Sequencer %d %p opened maxunits %d midi_number %d:\n", 814 scp->unit, scp, scp->maxunits, scp->midi_number)); 815 for (i = 0; i < scp->midi_number; i++) 816 SEQ_DEBUG(3, printf(" midi %d %p\n", i, scp->midis[i])); 817 818 return 0; 819 } 820 821 /* 822 * mseq_close 823 */ 824 int 825 mseq_close(struct cdev *i_dev, int flags, int mode, struct thread *td) 826 { 827 int i; 828 struct seq_softc *scp = i_dev->si_drv1; 829 int ret; 830 831 if (scp == NULL) 832 return ENXIO; 833 834 SEQ_DEBUG(2, printf("seq_close: unit %d.\n", scp->unit)); 835 836 mtx_lock(&scp->seq_lock); 837 838 ret = ENXIO; 839 if (scp->busy == 0) 840 goto err; 841 842 seq_reset(scp); 843 seq_sync(scp); 844 845 for (i = 0; i < scp->midi_number; i++) 846 if (scp->midis[i]) 847 SYNTH_CLOSE(scp->midis[i]); 848 849 midimapper_close(scp->mapper, scp->mapper_cookie); 850 851 timer_stop(scp); 852 853 scp->busy = 0; 854 ret = 0; 855 856 err: 857 SEQ_DEBUG(3, printf("seq_close: closed ret = %d.\n", ret)); 858 mtx_unlock(&scp->seq_lock); 859 return ret; 860 } 861 862 int 863 mseq_read(struct cdev *i_dev, struct uio *uio, int ioflag) 864 { 865 int retval, used; 866 struct seq_softc *scp = i_dev->si_drv1; 867 868 #define SEQ_RSIZE 32 869 u_char buf[SEQ_RSIZE]; 870 871 if (scp == NULL) 872 return ENXIO; 873 874 SEQ_DEBUG(7, printf("mseq_read: unit %d, resid %zd.\n", 875 scp->unit, uio->uio_resid)); 876 877 mtx_lock(&scp->seq_lock); 878 if ((scp->fflags & FREAD) == 0) { 879 SEQ_DEBUG(2, printf("mseq_read: unit %d is not for reading.\n", 880 scp->unit)); 881 retval = EIO; 882 goto err1; 883 } 884 /* 885 * Begin recording. 886 */ 887 /* 888 * if ((scp->flags & SEQ_F_READING) == 0) 889 */ 890 /* 891 * TODO, start recording if not alread 892 */ 893 894 /* 895 * I think the semantics are to return as soon 896 * as possible. 897 * Second thought, it doesn't seem like midimoutain 898 * expects that at all. 899 * TODO: Look up in some sort of spec 900 */ 901 902 while (uio->uio_resid > 0) { 903 while (MIDIQ_EMPTY(scp->in_q)) { 904 retval = EWOULDBLOCK; 905 /* 906 * I wish I knew which one to care about 907 */ 908 909 if (scp->fflags & O_NONBLOCK) 910 goto err1; 911 if (ioflag & O_NONBLOCK) 912 goto err1; 913 914 retval = cv_wait_sig(&scp->in_cv, &scp->seq_lock); 915 if (retval != 0) 916 goto err1; 917 } 918 919 used = MIN(MIDIQ_LEN(scp->in_q), uio->uio_resid); 920 used = MIN(used, SEQ_RSIZE); 921 922 SEQ_DEBUG(8, printf("midiread: uiomove cc=%d\n", used)); 923 MIDIQ_DEQ(scp->in_q, buf, used); 924 mtx_unlock(&scp->seq_lock); 925 retval = uiomove(buf, used, uio); 926 mtx_lock(&scp->seq_lock); 927 if (retval) 928 goto err1; 929 } 930 931 retval = 0; 932 err1: 933 mtx_unlock(&scp->seq_lock); 934 SEQ_DEBUG(6, printf("mseq_read: ret %d, resid %zd.\n", 935 retval, uio->uio_resid)); 936 937 return retval; 938 } 939 940 int 941 mseq_write(struct cdev *i_dev, struct uio *uio, int ioflag) 942 { 943 u_char event[EV_SZ], newevent[EV_SZ], ev_code; 944 struct seq_softc *scp = i_dev->si_drv1; 945 int retval; 946 int used; 947 948 SEQ_DEBUG(7, printf("seq_write: unit %d, resid %zd.\n", 949 scp->unit, uio->uio_resid)); 950 951 if (scp == NULL) 952 return ENXIO; 953 954 mtx_lock(&scp->seq_lock); 955 956 if ((scp->fflags & FWRITE) == 0) { 957 SEQ_DEBUG(2, printf("seq_write: unit %d is not for writing.\n", 958 scp->unit)); 959 retval = EIO; 960 goto err0; 961 } 962 while (uio->uio_resid > 0) { 963 while (MIDIQ_AVAIL(scp->out_q) == 0) { 964 retval = EWOULDBLOCK; 965 if (scp->fflags & O_NONBLOCK) 966 goto err0; 967 if (ioflag & O_NONBLOCK) 968 goto err0; 969 SEQ_DEBUG(8, printf("seq_write cvwait\n")); 970 971 scp->playing = 1; 972 cv_broadcast(&scp->out_cv); 973 cv_broadcast(&scp->state_cv); 974 975 retval = cv_wait_sig(&scp->out_cv, &scp->seq_lock); 976 /* 977 * We slept, maybe things have changed since last 978 * dying check 979 */ 980 if (retval != 0) 981 goto err0; 982 #if 0 983 /* 984 * Useless test 985 */ 986 if (scp != i_dev->si_drv1) 987 retval = ENXIO; 988 #endif 989 } 990 991 used = MIN(uio->uio_resid, 4); 992 993 SEQ_DEBUG(8, printf("seqout: resid %zd len %jd avail %jd\n", 994 uio->uio_resid, (intmax_t)MIDIQ_LEN(scp->out_q), 995 (intmax_t)MIDIQ_AVAIL(scp->out_q))); 996 997 if (used != 4) { 998 retval = ENXIO; 999 goto err0; 1000 } 1001 mtx_unlock(&scp->seq_lock); 1002 retval = uiomove(event, used, uio); 1003 mtx_lock(&scp->seq_lock); 1004 if (retval) 1005 goto err0; 1006 1007 ev_code = event[0]; 1008 SEQ_DEBUG(8, printf("seq_write: unit %d, event %s.\n", 1009 scp->unit, midi_cmdname(ev_code, cmdtab_seqevent))); 1010 1011 /* Have a look at the event code. */ 1012 if (ev_code == SEQ_FULLSIZE) { 1013 /* 1014 * TODO: restore code for SEQ_FULLSIZE 1015 */ 1016 #if 0 1017 /* 1018 * A long event, these are the patches/samples for a 1019 * synthesizer. 1020 */ 1021 midiunit = *(u_short *)&event[2]; 1022 mtx_lock(&sd->seq_lock); 1023 ret = lookup_mididev(scp, midiunit, LOOKUP_OPEN, &md); 1024 mtx_unlock(&sd->seq_lock); 1025 if (ret != 0) 1026 return (ret); 1027 1028 SEQ_DEBUG(printf("seq_write: loading a patch to the unit %d.\n", midiunit)); 1029 1030 ret = md->synth.loadpatch(md, *(short *)&event[0], buf, 1031 p + 4, count, 0); 1032 return (ret); 1033 #else 1034 /* 1035 * For now, just flush the darn buffer 1036 */ 1037 SEQ_DEBUG(2, 1038 printf("seq_write: SEQ_FULLSIZE flusing buffer.\n")); 1039 while (uio->uio_resid > 0) { 1040 mtx_unlock(&scp->seq_lock); 1041 retval = uiomove(event, MIN(EV_SZ, uio->uio_resid), uio); 1042 mtx_lock(&scp->seq_lock); 1043 if (retval) 1044 goto err0; 1045 } 1046 retval = 0; 1047 goto err0; 1048 #endif 1049 } 1050 retval = EINVAL; 1051 if (ev_code >= 128) { 1052 int error; 1053 1054 /* 1055 * Some sort of an extended event. The size is eight 1056 * bytes. scoop extra info. 1057 */ 1058 if (scp->music && ev_code == SEQ_EXTENDED) { 1059 SEQ_DEBUG(2, printf("seq_write: invalid level two event %x.\n", ev_code)); 1060 goto err0; 1061 } 1062 mtx_unlock(&scp->seq_lock); 1063 if (uio->uio_resid < 4) 1064 error = EINVAL; 1065 else 1066 error = uiomove((caddr_t)&event[4], 4, uio); 1067 mtx_lock(&scp->seq_lock); 1068 if (error) { 1069 SEQ_DEBUG(2, 1070 printf("seq_write: user memory mangled?\n")); 1071 goto err0; 1072 } 1073 } else { 1074 /* 1075 * Size four event. 1076 */ 1077 if (scp->music) { 1078 SEQ_DEBUG(2, printf("seq_write: four byte event in music mode.\n")); 1079 goto err0; 1080 } 1081 } 1082 if (ev_code == SEQ_MIDIPUTC) { 1083 /* 1084 * TODO: event[2] is unit number to receive char. 1085 * Range check it. 1086 */ 1087 } 1088 if (scp->music) { 1089 #ifdef not_ever_ever 1090 if (event[0] == EV_TIMING && 1091 (event[1] == TMR_START || event[1] == TMR_STOP)) { 1092 /* 1093 * For now, try to make midimoutain work by 1094 * forcing these events to be processed 1095 * immediately. 1096 */ 1097 seq_processevent(scp, event); 1098 } else 1099 MIDIQ_ENQ(scp->out_q, event, EV_SZ); 1100 #else 1101 MIDIQ_ENQ(scp->out_q, event, EV_SZ); 1102 #endif 1103 } else { 1104 if (seq_convertold(event, newevent) > 0) 1105 MIDIQ_ENQ(scp->out_q, newevent, EV_SZ); 1106 #if 0 1107 else 1108 goto err0; 1109 #endif 1110 } 1111 } 1112 1113 scp->playing = 1; 1114 cv_broadcast(&scp->state_cv); 1115 cv_broadcast(&scp->out_cv); 1116 1117 retval = 0; 1118 1119 err0: 1120 SEQ_DEBUG(6, 1121 printf("seq_write done: leftover buffer length %zd retval %d\n", 1122 uio->uio_resid, retval)); 1123 mtx_unlock(&scp->seq_lock); 1124 return retval; 1125 } 1126 1127 int 1128 mseq_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, 1129 struct thread *td) 1130 { 1131 int midiunit, ret, tmp; 1132 struct seq_softc *scp = i_dev->si_drv1; 1133 struct synth_info *synthinfo; 1134 struct midi_info *midiinfo; 1135 u_char event[EV_SZ]; 1136 u_char newevent[EV_SZ]; 1137 1138 kobj_t md; 1139 1140 /* 1141 * struct snd_size *sndsize; 1142 */ 1143 1144 if (scp == NULL) 1145 return ENXIO; 1146 1147 SEQ_DEBUG(6, printf("seq_ioctl: unit %d, cmd %s.\n", 1148 scp->unit, midi_cmdname(cmd, cmdtab_seqioctl))); 1149 1150 ret = 0; 1151 1152 switch (cmd) { 1153 case SNDCTL_SEQ_GETTIME: 1154 /* 1155 * ioctl needed by libtse 1156 */ 1157 mtx_lock(&scp->seq_lock); 1158 *(int *)arg = timer_now(scp); 1159 mtx_unlock(&scp->seq_lock); 1160 SEQ_DEBUG(6, printf("seq_ioctl: gettime %d.\n", *(int *)arg)); 1161 ret = 0; 1162 break; 1163 case SNDCTL_TMR_METRONOME: 1164 /* fallthrough */ 1165 case SNDCTL_TMR_SOURCE: 1166 /* 1167 * Not implemented 1168 */ 1169 ret = 0; 1170 break; 1171 case SNDCTL_TMR_TEMPO: 1172 event[1] = TMR_TEMPO; 1173 event[4] = *(int *)arg & 0xFF; 1174 event[5] = (*(int *)arg >> 8) & 0xFF; 1175 event[6] = (*(int *)arg >> 16) & 0xFF; 1176 event[7] = (*(int *)arg >> 24) & 0xFF; 1177 goto timerevent; 1178 case SNDCTL_TMR_TIMEBASE: 1179 event[1] = TMR_TIMERBASE; 1180 event[4] = *(int *)arg & 0xFF; 1181 event[5] = (*(int *)arg >> 8) & 0xFF; 1182 event[6] = (*(int *)arg >> 16) & 0xFF; 1183 event[7] = (*(int *)arg >> 24) & 0xFF; 1184 goto timerevent; 1185 case SNDCTL_TMR_START: 1186 event[1] = TMR_START; 1187 goto timerevent; 1188 case SNDCTL_TMR_STOP: 1189 event[1] = TMR_STOP; 1190 goto timerevent; 1191 case SNDCTL_TMR_CONTINUE: 1192 event[1] = TMR_CONTINUE; 1193 timerevent: 1194 event[0] = EV_TIMING; 1195 mtx_lock(&scp->seq_lock); 1196 if (!scp->music) { 1197 ret = EINVAL; 1198 mtx_unlock(&scp->seq_lock); 1199 break; 1200 } 1201 seq_processevent(scp, event); 1202 mtx_unlock(&scp->seq_lock); 1203 break; 1204 case SNDCTL_TMR_SELECT: 1205 SEQ_DEBUG(2, 1206 printf("seq_ioctl: SNDCTL_TMR_SELECT not supported\n")); 1207 ret = EINVAL; 1208 break; 1209 case SNDCTL_SEQ_SYNC: 1210 if (mode == O_RDONLY) { 1211 ret = 0; 1212 break; 1213 } 1214 mtx_lock(&scp->seq_lock); 1215 ret = seq_sync(scp); 1216 mtx_unlock(&scp->seq_lock); 1217 break; 1218 case SNDCTL_SEQ_PANIC: 1219 /* fallthrough */ 1220 case SNDCTL_SEQ_RESET: 1221 /* 1222 * SNDCTL_SEQ_PANIC == SNDCTL_SEQ_RESET 1223 */ 1224 mtx_lock(&scp->seq_lock); 1225 seq_reset(scp); 1226 mtx_unlock(&scp->seq_lock); 1227 ret = 0; 1228 break; 1229 case SNDCTL_SEQ_TESTMIDI: 1230 mtx_lock(&scp->seq_lock); 1231 /* 1232 * TODO: SNDCTL_SEQ_TESTMIDI now means "can I write to the 1233 * device?". 1234 */ 1235 mtx_unlock(&scp->seq_lock); 1236 break; 1237 #if 0 1238 case SNDCTL_SEQ_GETINCOUNT: 1239 if (mode == O_WRONLY) 1240 *(int *)arg = 0; 1241 else { 1242 mtx_lock(&scp->seq_lock); 1243 *(int *)arg = scp->in_q.rl; 1244 mtx_unlock(&scp->seq_lock); 1245 SEQ_DEBUG(printf("seq_ioctl: incount %d.\n", 1246 *(int *)arg)); 1247 } 1248 ret = 0; 1249 break; 1250 case SNDCTL_SEQ_GETOUTCOUNT: 1251 if (mode == O_RDONLY) 1252 *(int *)arg = 0; 1253 else { 1254 mtx_lock(&scp->seq_lock); 1255 *(int *)arg = scp->out_q.fl; 1256 mtx_unlock(&scp->seq_lock); 1257 SEQ_DEBUG(printf("seq_ioctl: outcount %d.\n", 1258 *(int *)arg)); 1259 } 1260 ret = 0; 1261 break; 1262 #endif 1263 case SNDCTL_SEQ_CTRLRATE: 1264 if (*(int *)arg != 0) { 1265 ret = EINVAL; 1266 break; 1267 } 1268 mtx_lock(&scp->seq_lock); 1269 *(int *)arg = scp->timerbase; 1270 mtx_unlock(&scp->seq_lock); 1271 SEQ_DEBUG(3, printf("seq_ioctl: ctrlrate %d.\n", *(int *)arg)); 1272 ret = 0; 1273 break; 1274 /* 1275 * TODO: ioctl SNDCTL_SEQ_RESETSAMPLES 1276 */ 1277 #if 0 1278 case SNDCTL_SEQ_RESETSAMPLES: 1279 mtx_lock(&scp->seq_lock); 1280 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md); 1281 mtx_unlock(&scp->seq_lock); 1282 if (ret != 0) 1283 break; 1284 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg, 1285 SND_DEV_MIDIN), cmd, arg, mode, td); 1286 break; 1287 #endif 1288 case SNDCTL_SEQ_NRSYNTHS: 1289 mtx_lock(&scp->seq_lock); 1290 *(int *)arg = scp->midi_number; 1291 mtx_unlock(&scp->seq_lock); 1292 SEQ_DEBUG(3, printf("seq_ioctl: synths %d.\n", *(int *)arg)); 1293 ret = 0; 1294 break; 1295 case SNDCTL_SEQ_NRMIDIS: 1296 mtx_lock(&scp->seq_lock); 1297 if (scp->music) 1298 *(int *)arg = 0; 1299 else { 1300 /* 1301 * TODO: count the numbder of devices that can WRITERAW 1302 */ 1303 *(int *)arg = scp->midi_number; 1304 } 1305 mtx_unlock(&scp->seq_lock); 1306 SEQ_DEBUG(3, printf("seq_ioctl: midis %d.\n", *(int *)arg)); 1307 ret = 0; 1308 break; 1309 /* 1310 * TODO: ioctl SNDCTL_SYNTH_MEMAVL 1311 */ 1312 #if 0 1313 case SNDCTL_SYNTH_MEMAVL: 1314 mtx_lock(&scp->seq_lock); 1315 ret = lookup_mididev(scp, *(int *)arg, LOOKUP_OPEN, &md); 1316 mtx_unlock(&scp->seq_lock); 1317 if (ret != 0) 1318 break; 1319 ret = midi_ioctl(MIDIMKDEV(major(i_dev), *(int *)arg, 1320 SND_DEV_MIDIN), cmd, arg, mode, td); 1321 break; 1322 #endif 1323 case SNDCTL_SEQ_OUTOFBAND: 1324 for (ret = 0; ret < EV_SZ; ret++) 1325 event[ret] = (u_char)arg[0]; 1326 1327 mtx_lock(&scp->seq_lock); 1328 if (scp->music) 1329 ret = seq_processevent(scp, event); 1330 else { 1331 if (seq_convertold(event, newevent) > 0) 1332 ret = seq_processevent(scp, newevent); 1333 else 1334 ret = EINVAL; 1335 } 1336 mtx_unlock(&scp->seq_lock); 1337 break; 1338 case SNDCTL_SYNTH_INFO: 1339 synthinfo = (struct synth_info *)arg; 1340 midiunit = synthinfo->device; 1341 mtx_lock(&scp->seq_lock); 1342 if (seq_fetch_mid(scp, midiunit, &md) == 0) { 1343 bzero(synthinfo, sizeof(*synthinfo)); 1344 synthinfo->name[0] = 'f'; 1345 synthinfo->name[1] = 'a'; 1346 synthinfo->name[2] = 'k'; 1347 synthinfo->name[3] = 'e'; 1348 synthinfo->name[4] = 's'; 1349 synthinfo->name[5] = 'y'; 1350 synthinfo->name[6] = 'n'; 1351 synthinfo->name[7] = 't'; 1352 synthinfo->name[8] = 'h'; 1353 synthinfo->device = midiunit; 1354 synthinfo->synth_type = SYNTH_TYPE_MIDI; 1355 synthinfo->capabilities = scp->midi_flags[midiunit]; 1356 ret = 0; 1357 } else 1358 ret = EINVAL; 1359 mtx_unlock(&scp->seq_lock); 1360 break; 1361 case SNDCTL_MIDI_INFO: 1362 midiinfo = (struct midi_info *)arg; 1363 midiunit = midiinfo->device; 1364 mtx_lock(&scp->seq_lock); 1365 if (seq_fetch_mid(scp, midiunit, &md) == 0) { 1366 bzero(midiinfo, sizeof(*midiinfo)); 1367 midiinfo->name[0] = 'f'; 1368 midiinfo->name[1] = 'a'; 1369 midiinfo->name[2] = 'k'; 1370 midiinfo->name[3] = 'e'; 1371 midiinfo->name[4] = 'm'; 1372 midiinfo->name[5] = 'i'; 1373 midiinfo->name[6] = 'd'; 1374 midiinfo->name[7] = 'i'; 1375 midiinfo->device = midiunit; 1376 midiinfo->capabilities = scp->midi_flags[midiunit]; 1377 /* 1378 * TODO: What devtype? 1379 */ 1380 midiinfo->dev_type = 0x01; 1381 ret = 0; 1382 } else 1383 ret = EINVAL; 1384 mtx_unlock(&scp->seq_lock); 1385 break; 1386 case SNDCTL_SEQ_THRESHOLD: 1387 mtx_lock(&scp->seq_lock); 1388 RANGE(*(int *)arg, 1, MIDIQ_SIZE(scp->out_q) - 1); 1389 scp->out_water = *(int *)arg; 1390 mtx_unlock(&scp->seq_lock); 1391 SEQ_DEBUG(3, printf("seq_ioctl: water %d.\n", *(int *)arg)); 1392 ret = 0; 1393 break; 1394 case SNDCTL_MIDI_PRETIME: 1395 tmp = *(int *)arg; 1396 if (tmp < 0) 1397 tmp = 0; 1398 mtx_lock(&scp->seq_lock); 1399 scp->pre_event_timeout = (hz * tmp) / 10; 1400 *(int *)arg = scp->pre_event_timeout; 1401 mtx_unlock(&scp->seq_lock); 1402 SEQ_DEBUG(3, printf("seq_ioctl: pretime %d.\n", *(int *)arg)); 1403 ret = 0; 1404 break; 1405 case SNDCTL_FM_4OP_ENABLE: 1406 case SNDCTL_PMGR_IFACE: 1407 case SNDCTL_PMGR_ACCESS: 1408 /* 1409 * Patch manager and fm are ded, ded, ded. 1410 */ 1411 /* fallthrough */ 1412 default: 1413 /* 1414 * TODO: Consider ioctl default case. 1415 * Old code used to 1416 * if ((scp->fflags & O_ACCMODE) == FREAD) { 1417 * ret = EIO; 1418 * break; 1419 * } 1420 * Then pass on the ioctl to device 0 1421 */ 1422 SEQ_DEBUG(2, 1423 printf("seq_ioctl: unsupported IOCTL %ld.\n", cmd)); 1424 ret = EINVAL; 1425 break; 1426 } 1427 1428 return ret; 1429 } 1430 1431 int 1432 mseq_poll(struct cdev *i_dev, int events, struct thread *td) 1433 { 1434 int ret, lim; 1435 struct seq_softc *scp = i_dev->si_drv1; 1436 1437 SEQ_DEBUG(3, printf("seq_poll: unit %d.\n", scp->unit)); 1438 SEQ_DEBUG(1, printf("seq_poll: unit %d.\n", scp->unit)); 1439 1440 mtx_lock(&scp->seq_lock); 1441 1442 ret = 0; 1443 1444 /* Look up the appropriate queue and select it. */ 1445 if ((events & (POLLOUT | POLLWRNORM)) != 0) { 1446 /* Start playing. */ 1447 scp->playing = 1; 1448 cv_broadcast(&scp->state_cv); 1449 cv_broadcast(&scp->out_cv); 1450 1451 lim = scp->out_water; 1452 1453 if (MIDIQ_AVAIL(scp->out_q) < lim) 1454 /* No enough space, record select. */ 1455 selrecord(td, &scp->out_sel); 1456 else 1457 /* We can write now. */ 1458 ret |= events & (POLLOUT | POLLWRNORM); 1459 } 1460 if ((events & (POLLIN | POLLRDNORM)) != 0) { 1461 /* TODO: Start recording. */ 1462 1463 /* Find out the boundary. */ 1464 lim = 1; 1465 if (MIDIQ_LEN(scp->in_q) < lim) 1466 /* No data ready, record select. */ 1467 selrecord(td, &scp->in_sel); 1468 else 1469 /* We can read now. */ 1470 ret |= events & (POLLIN | POLLRDNORM); 1471 } 1472 mtx_unlock(&scp->seq_lock); 1473 1474 return (ret); 1475 } 1476 1477 #if 0 1478 static void 1479 sein_qtr(void *p, void /* mididev_info */ *md) 1480 { 1481 struct seq_softc *scp; 1482 1483 scp = (struct seq_softc *)p; 1484 1485 mtx_lock(&scp->seq_lock); 1486 1487 /* Restart playing if we have the data to output. */ 1488 if (scp->queueout_pending) 1489 seq_callback(scp, SEQ_CB_START | SEQ_CB_WR); 1490 /* Check the midi device if we are reading. */ 1491 if ((scp->flags & SEQ_F_READING) != 0) 1492 seq_midiinput(scp, md); 1493 1494 mtx_unlock(&scp->seq_lock); 1495 } 1496 1497 #endif 1498 /* 1499 * seq_convertold 1500 * Was the old playevent. Use this to convert and old 1501 * style /dev/sequencer event to a /dev/music event 1502 */ 1503 static int 1504 seq_convertold(u_char *event, u_char *out) 1505 { 1506 int used; 1507 u_char dev, chn, note, vel; 1508 1509 out[0] = out[1] = out[2] = out[3] = out[4] = out[5] = out[6] = 1510 out[7] = 0; 1511 1512 dev = 0; 1513 chn = event[1]; 1514 note = event[2]; 1515 vel = event[3]; 1516 1517 used = 0; 1518 1519 restart: 1520 /* 1521 * TODO: Debug statement 1522 */ 1523 switch (event[0]) { 1524 case EV_TIMING: 1525 case EV_CHN_VOICE: 1526 case EV_CHN_COMMON: 1527 case EV_SYSEX: 1528 case EV_SEQ_LOCAL: 1529 out[0] = event[0]; 1530 out[1] = event[1]; 1531 out[2] = event[2]; 1532 out[3] = event[3]; 1533 out[4] = event[4]; 1534 out[5] = event[5]; 1535 out[6] = event[6]; 1536 out[7] = event[7]; 1537 used += 8; 1538 break; 1539 case SEQ_NOTEOFF: 1540 out[0] = EV_CHN_VOICE; 1541 out[1] = dev; 1542 out[2] = MIDI_NOTEOFF; 1543 out[3] = chn; 1544 out[4] = note; 1545 out[5] = 255; 1546 used += 4; 1547 break; 1548 1549 case SEQ_NOTEON: 1550 out[0] = EV_CHN_VOICE; 1551 out[1] = dev; 1552 out[2] = MIDI_NOTEON; 1553 out[3] = chn; 1554 out[4] = note; 1555 out[5] = vel; 1556 used += 4; 1557 break; 1558 1559 /* 1560 * wait delay = (event[2] << 16) + (event[3] << 8) + event[4] 1561 */ 1562 1563 case SEQ_PGMCHANGE: 1564 out[0] = EV_CHN_COMMON; 1565 out[1] = dev; 1566 out[2] = MIDI_PGM_CHANGE; 1567 out[3] = chn; 1568 out[4] = note; 1569 out[5] = vel; 1570 used += 4; 1571 break; 1572 /* 1573 out[0] = EV_TIMING; 1574 out[1] = dev; 1575 out[2] = MIDI_PGM_CHANGE; 1576 out[3] = chn; 1577 out[4] = note; 1578 out[5] = vel; 1579 SEQ_DEBUG(4,printf("seq_playevent: synctimer\n")); 1580 break; 1581 */ 1582 1583 case SEQ_MIDIPUTC: 1584 SEQ_DEBUG(4, 1585 printf("seq_playevent: put data 0x%02x, unit %d.\n", 1586 event[1], event[2])); 1587 /* 1588 * Pass through to the midi device. 1589 * device = event[2] 1590 * data = event[1] 1591 */ 1592 out[0] = SEQ_MIDIPUTC; 1593 out[1] = dev; 1594 out[2] = chn; 1595 used += 4; 1596 break; 1597 #ifdef notyet 1598 case SEQ_ECHO: 1599 /* 1600 * This isn't handled here yet because I don't know if I can 1601 * just use four bytes events. There might be consequences 1602 * in the _read routing 1603 */ 1604 if (seq_copytoinput(scp, event, 4) == EAGAIN) { 1605 ret = QUEUEFULL; 1606 break; 1607 } 1608 ret = MORE; 1609 break; 1610 #endif 1611 case SEQ_EXTENDED: 1612 switch (event[1]) { 1613 case SEQ_NOTEOFF: 1614 case SEQ_NOTEON: 1615 case SEQ_PGMCHANGE: 1616 event++; 1617 used = 4; 1618 goto restart; 1619 break; 1620 case SEQ_AFTERTOUCH: 1621 /* 1622 * SYNTH_AFTERTOUCH(md, event[3], event[4]) 1623 */ 1624 case SEQ_BALANCE: 1625 /* 1626 * SYNTH_PANNING(md, event[3], (char)event[4]) 1627 */ 1628 case SEQ_CONTROLLER: 1629 /* 1630 * SYNTH_CONTROLLER(md, event[3], event[4], *(short *)&event[5]) 1631 */ 1632 case SEQ_VOLMODE: 1633 /* 1634 * SYNTH_VOLUMEMETHOD(md, event[3]) 1635 */ 1636 default: 1637 SEQ_DEBUG(2, 1638 printf("seq_convertold: SEQ_EXTENDED type %d" 1639 "not handled\n", event[1])); 1640 break; 1641 } 1642 break; 1643 case SEQ_WAIT: 1644 out[0] = EV_TIMING; 1645 out[1] = TMR_WAIT_REL; 1646 out[4] = event[2]; 1647 out[5] = event[3]; 1648 out[6] = event[4]; 1649 1650 SEQ_DEBUG(5, printf("SEQ_WAIT %d", 1651 event[2] + (event[3] << 8) + (event[4] << 24))); 1652 1653 used += 4; 1654 break; 1655 1656 case SEQ_ECHO: 1657 case SEQ_SYNCTIMER: 1658 case SEQ_PRIVATE: 1659 default: 1660 SEQ_DEBUG(2, 1661 printf("seq_convertold: event type %d not handled %d %d %d\n", 1662 event[0], event[1], event[2], event[3])); 1663 break; 1664 } 1665 return used; 1666 } 1667 1668 /* 1669 * Writting to the sequencer buffer never blocks and drops 1670 * input which cannot be queued 1671 */ 1672 void 1673 seq_copytoinput(struct seq_softc *scp, u_char *event, int len) 1674 { 1675 1676 mtx_assert(&scp->seq_lock, MA_OWNED); 1677 1678 if (MIDIQ_AVAIL(scp->in_q) < len) { 1679 /* 1680 * ENOROOM? EINPUTDROPPED? ETOUGHLUCK? 1681 */ 1682 SEQ_DEBUG(2, printf("seq_copytoinput: queue full\n")); 1683 } else { 1684 MIDIQ_ENQ(scp->in_q, event, len); 1685 selwakeup(&scp->in_sel); 1686 cv_broadcast(&scp->in_cv); 1687 } 1688 1689 } 1690 1691 static int 1692 seq_chnvoice(struct seq_softc *scp, kobj_t md, u_char *event) 1693 { 1694 int ret, voice; 1695 u_char cmd, chn, note, parm; 1696 1697 ret = 0; 1698 cmd = event[2]; 1699 chn = event[3]; 1700 note = event[4]; 1701 parm = event[5]; 1702 1703 mtx_assert(&scp->seq_lock, MA_OWNED); 1704 1705 SEQ_DEBUG(5, printf("seq_chnvoice: unit %d, dev %d, cmd %s," 1706 " chn %d, note %d, parm %d.\n", scp->unit, event[1], 1707 midi_cmdname(cmd, cmdtab_seqcv), chn, note, parm)); 1708 1709 voice = SYNTH_ALLOC(md, chn, note); 1710 1711 mtx_unlock(&scp->seq_lock); 1712 1713 switch (cmd) { 1714 case MIDI_NOTEON: 1715 if (note < 128 || note == 255) { 1716 #if 0 1717 if (scp->music && chn == 9) { 1718 /* 1719 * This channel is a percussion. The note 1720 * number is the patch number. 1721 */ 1722 /* 1723 mtx_unlock(&scp->seq_lock); 1724 if (SYNTH_SETINSTR(md, voice, 128 + note) 1725 == EAGAIN) { 1726 mtx_lock(&scp->seq_lock); 1727 return (QUEUEFULL); 1728 } 1729 mtx_lock(&scp->seq_lock); 1730 */ 1731 note = 60; /* Middle C. */ 1732 } 1733 #endif 1734 if (scp->music) { 1735 /* 1736 mtx_unlock(&scp->seq_lock); 1737 if (SYNTH_SETUPVOICE(md, voice, chn) 1738 == EAGAIN) { 1739 mtx_lock(&scp->seq_lock); 1740 return (QUEUEFULL); 1741 } 1742 mtx_lock(&scp->seq_lock); 1743 */ 1744 } 1745 SYNTH_STARTNOTE(md, voice, note, parm); 1746 } 1747 break; 1748 case MIDI_NOTEOFF: 1749 SYNTH_KILLNOTE(md, voice, note, parm); 1750 break; 1751 case MIDI_KEY_PRESSURE: 1752 SYNTH_AFTERTOUCH(md, voice, parm); 1753 break; 1754 default: 1755 ret = 1; 1756 SEQ_DEBUG(2, printf("seq_chnvoice event type %d not handled\n", 1757 event[1])); 1758 break; 1759 } 1760 1761 mtx_lock(&scp->seq_lock); 1762 return ret; 1763 } 1764 1765 static int 1766 seq_chncommon(struct seq_softc *scp, kobj_t md, u_char *event) 1767 { 1768 int ret; 1769 u_short w14; 1770 u_char cmd, chn, p1; 1771 1772 ret = 0; 1773 cmd = event[2]; 1774 chn = event[3]; 1775 p1 = event[4]; 1776 w14 = *(u_short *)&event[6]; 1777 1778 SEQ_DEBUG(5, printf("seq_chncommon: unit %d, dev %d, cmd %s, chn %d," 1779 " p1 %d, w14 %d.\n", scp->unit, event[1], 1780 midi_cmdname(cmd, cmdtab_seqccmn), chn, p1, w14)); 1781 mtx_unlock(&scp->seq_lock); 1782 switch (cmd) { 1783 case MIDI_PGM_CHANGE: 1784 SEQ_DEBUG(4, printf("seq_chncommon pgmchn chn %d pg %d\n", 1785 chn, p1)); 1786 SYNTH_SETINSTR(md, chn, p1); 1787 break; 1788 case MIDI_CTL_CHANGE: 1789 SEQ_DEBUG(4, printf("seq_chncommon ctlch chn %d pg %d %d\n", 1790 chn, p1, w14)); 1791 SYNTH_CONTROLLER(md, chn, p1, w14); 1792 break; 1793 case MIDI_PITCH_BEND: 1794 if (scp->music) { 1795 /* 1796 * TODO: MIDI_PITCH_BEND 1797 */ 1798 #if 0 1799 mtx_lock(&md->synth.vc_mtx); 1800 md->synth.chn_info[chn].bender_value = w14; 1801 if (md->midiunit >= 0) { 1802 /* 1803 * Handle all of the notes playing on this 1804 * channel. 1805 */ 1806 key = ((int)chn << 8); 1807 for (i = 0; i < md->synth.alloc.max_voice; i++) 1808 if ((md->synth.alloc.map[i] & 0xff00) == key) { 1809 mtx_unlock(&md->synth.vc_mtx); 1810 mtx_unlock(&scp->seq_lock); 1811 if (md->synth.bender(md, i, w14) == EAGAIN) { 1812 mtx_lock(&scp->seq_lock); 1813 return (QUEUEFULL); 1814 } 1815 mtx_lock(&scp->seq_lock); 1816 } 1817 } else { 1818 mtx_unlock(&md->synth.vc_mtx); 1819 mtx_unlock(&scp->seq_lock); 1820 if (md->synth.bender(md, chn, w14) == EAGAIN) { 1821 mtx_lock(&scp->seq_lock); 1822 return (QUEUEFULL); 1823 } 1824 mtx_lock(&scp->seq_lock); 1825 } 1826 #endif 1827 } else 1828 SYNTH_BENDER(md, chn, w14); 1829 break; 1830 default: 1831 ret = 1; 1832 SEQ_DEBUG(2, 1833 printf("seq_chncommon event type %d not handled.\n", 1834 event[1])); 1835 break; 1836 } 1837 mtx_lock(&scp->seq_lock); 1838 return ret; 1839 } 1840 1841 static int 1842 seq_timing(struct seq_softc *scp, u_char *event) 1843 { 1844 int param; 1845 int ret; 1846 1847 ret = 0; 1848 param = event[4] + (event[5] << 8) + 1849 (event[6] << 16) + (event[7] << 24); 1850 1851 SEQ_DEBUG(5, printf("seq_timing: unit %d, cmd %d, param %d.\n", 1852 scp->unit, event[1], param)); 1853 switch (event[1]) { 1854 case TMR_WAIT_REL: 1855 timer_wait(scp, param, 0); 1856 break; 1857 case TMR_WAIT_ABS: 1858 timer_wait(scp, param, 1); 1859 break; 1860 case TMR_START: 1861 timer_start(scp); 1862 cv_broadcast(&scp->reset_cv); 1863 break; 1864 case TMR_STOP: 1865 timer_stop(scp); 1866 /* 1867 * The following cv_broadcast isn't needed since we only 1868 * wait for 0->1 transitions. It probably won't hurt 1869 */ 1870 cv_broadcast(&scp->reset_cv); 1871 break; 1872 case TMR_CONTINUE: 1873 timer_continue(scp); 1874 cv_broadcast(&scp->reset_cv); 1875 break; 1876 case TMR_TEMPO: 1877 if (param < 8) 1878 param = 8; 1879 if (param > 360) 1880 param = 360; 1881 SEQ_DEBUG(4, printf("Timer set tempo %d\n", param)); 1882 timer_setvals(scp, param, scp->timerbase); 1883 break; 1884 case TMR_TIMERBASE: 1885 if (param < 1) 1886 param = 1; 1887 if (param > 1000) 1888 param = 1000; 1889 SEQ_DEBUG(4, printf("Timer set timerbase %d\n", param)); 1890 timer_setvals(scp, scp->tempo, param); 1891 break; 1892 case TMR_ECHO: 1893 /* 1894 * TODO: Consider making 4-byte events for /dev/sequencer 1895 * PRO: Maybe needed by legacy apps 1896 * CON: soundcard.h has been warning for a while many years 1897 * to expect 8 byte events. 1898 */ 1899 #if 0 1900 if (scp->music) 1901 seq_copytoinput(scp, event, 8); 1902 else { 1903 param = (param << 8 | SEQ_ECHO); 1904 seq_copytoinput(scp, (u_char *)¶m, 4); 1905 } 1906 #else 1907 seq_copytoinput(scp, event, 8); 1908 #endif 1909 break; 1910 default: 1911 SEQ_DEBUG(2, printf("seq_timing event type %d not handled.\n", 1912 event[1])); 1913 ret = 1; 1914 break; 1915 } 1916 return ret; 1917 } 1918 1919 static int 1920 seq_local(struct seq_softc *scp, u_char *event) 1921 { 1922 int ret; 1923 1924 ret = 0; 1925 mtx_assert(&scp->seq_lock, MA_OWNED); 1926 1927 SEQ_DEBUG(5, printf("seq_local: unit %d, cmd %d\n", scp->unit, 1928 event[1])); 1929 switch (event[1]) { 1930 default: 1931 SEQ_DEBUG(1, printf("seq_local event type %d not handled\n", 1932 event[1])); 1933 ret = 1; 1934 break; 1935 } 1936 return ret; 1937 } 1938 1939 static int 1940 seq_sysex(struct seq_softc *scp, kobj_t md, u_char *event) 1941 { 1942 int i, l; 1943 1944 mtx_assert(&scp->seq_lock, MA_OWNED); 1945 SEQ_DEBUG(5, printf("seq_sysex: unit %d device %d\n", scp->unit, 1946 event[1])); 1947 l = 0; 1948 for (i = 0; i < 6 && event[i + 2] != 0xff; i++) 1949 l = i + 1; 1950 if (l > 0) { 1951 mtx_unlock(&scp->seq_lock); 1952 if (SYNTH_SENDSYSEX(md, &event[2], l) == EAGAIN) { 1953 mtx_lock(&scp->seq_lock); 1954 return 1; 1955 } 1956 mtx_lock(&scp->seq_lock); 1957 } 1958 return 0; 1959 } 1960 1961 /* 1962 * Reset no longer closes the raw devices nor seq_sync's 1963 * Callers are IOCTL and seq_close 1964 */ 1965 static void 1966 seq_reset(struct seq_softc *scp) 1967 { 1968 int chn, i; 1969 kobj_t m; 1970 1971 mtx_assert(&scp->seq_lock, MA_OWNED); 1972 1973 SEQ_DEBUG(5, printf("seq_reset: unit %d.\n", scp->unit)); 1974 1975 /* 1976 * Stop reading and writing. 1977 */ 1978 1979 /* scp->recording = 0; */ 1980 scp->playing = 0; 1981 cv_broadcast(&scp->state_cv); 1982 cv_broadcast(&scp->out_cv); 1983 cv_broadcast(&scp->reset_cv); 1984 1985 /* 1986 * For now, don't reset the timers. 1987 */ 1988 MIDIQ_CLEAR(scp->in_q); 1989 MIDIQ_CLEAR(scp->out_q); 1990 1991 for (i = 0; i < scp->midi_number; i++) { 1992 m = scp->midis[i]; 1993 mtx_unlock(&scp->seq_lock); 1994 SYNTH_RESET(m); 1995 for (chn = 0; chn < 16; chn++) { 1996 SYNTH_CONTROLLER(m, chn, 123, 0); 1997 SYNTH_CONTROLLER(m, chn, 121, 0); 1998 SYNTH_BENDER(m, chn, 1 << 13); 1999 } 2000 mtx_lock(&scp->seq_lock); 2001 } 2002 } 2003 2004 /* 2005 * seq_sync 2006 * *really* flush the output queue 2007 * flush the event queue, then flush the synthsisers. 2008 * Callers are IOCTL and close 2009 */ 2010 2011 #define SEQ_SYNC_TIMEOUT 8 2012 static int 2013 seq_sync(struct seq_softc *scp) 2014 { 2015 int i, rl, sync[16], done; 2016 2017 mtx_assert(&scp->seq_lock, MA_OWNED); 2018 2019 SEQ_DEBUG(4, printf("seq_sync: unit %d.\n", scp->unit)); 2020 2021 /* 2022 * Wait until output queue is empty. Check every so often to see if 2023 * the queue is moving along. If it isn't just abort. 2024 */ 2025 while (!MIDIQ_EMPTY(scp->out_q)) { 2026 if (!scp->playing) { 2027 scp->playing = 1; 2028 cv_broadcast(&scp->state_cv); 2029 cv_broadcast(&scp->out_cv); 2030 } 2031 rl = MIDIQ_LEN(scp->out_q); 2032 2033 i = cv_timedwait_sig(&scp->out_cv, 2034 &scp->seq_lock, SEQ_SYNC_TIMEOUT * hz); 2035 2036 if (i == EINTR || i == ERESTART) { 2037 if (i == EINTR) { 2038 /* 2039 * XXX: I don't know why we stop playing 2040 */ 2041 scp->playing = 0; 2042 cv_broadcast(&scp->out_cv); 2043 } 2044 return i; 2045 } 2046 if (i == EWOULDBLOCK && rl == MIDIQ_LEN(scp->out_q) && 2047 scp->waiting == 0) { 2048 /* 2049 * A queue seems to be stuck up. Give up and clear 2050 * queues. 2051 */ 2052 MIDIQ_CLEAR(scp->out_q); 2053 scp->playing = 0; 2054 cv_broadcast(&scp->state_cv); 2055 cv_broadcast(&scp->out_cv); 2056 cv_broadcast(&scp->reset_cv); 2057 2058 /* 2059 * TODO: Consider if the raw devices need to be flushed 2060 */ 2061 2062 SEQ_DEBUG(1, printf("seq_sync queue stuck, aborting\n")); 2063 2064 return i; 2065 } 2066 } 2067 2068 scp->playing = 0; 2069 /* 2070 * Since syncing a midi device might block, unlock scp->seq_lock. 2071 */ 2072 2073 mtx_unlock(&scp->seq_lock); 2074 for (i = 0; i < scp->midi_number; i++) 2075 sync[i] = 1; 2076 2077 do { 2078 done = 1; 2079 for (i = 0; i < scp->midi_number; i++) 2080 if (sync[i]) { 2081 if (SYNTH_INSYNC(scp->midis[i]) == 0) 2082 sync[i] = 0; 2083 else 2084 done = 0; 2085 } 2086 if (!done) 2087 DELAY(5000); 2088 2089 } while (!done); 2090 2091 mtx_lock(&scp->seq_lock); 2092 return 0; 2093 } 2094 2095 char * 2096 midi_cmdname(int cmd, midi_cmdtab *tab) 2097 { 2098 while (tab->name != NULL) { 2099 if (cmd == tab->cmd) 2100 return (tab->name); 2101 tab++; 2102 } 2103 2104 return ("unknown"); 2105 } 2106