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