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