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