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