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