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