sequencer.c revision 1.55 1 /* $NetBSD: sequencer.c,v 1.55 2012/04/09 10:18:16 plunky Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) NetBSD.org) and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Locking:
34 *
35 * o sc_lock: provides atomic access to all data structures. Taken from
36 * both process and soft interrupt context.
37 *
38 * o sc_dvlock: serializes operations on /dev/sequencer. Taken from
39 * process context. Dropped while waiting for data in sequencerread()
40 * to allow concurrent reads/writes while no data available.
41 *
42 * o sc_isopen: we allow only one concurrent open, only to prevent user
43 * and/or application error.
44 *
45 * o MIDI softc locks. These can be spinlocks and there can be many of
46 * them, because we can open many MIDI devices. We take these only in two
47 * places: when enabling redirection from the MIDI device and when
48 * disabling it (open/close). midiseq_in() is called by the MIDI driver
49 * with its own lock held when passing data into this module. To avoid
50 * lock order and context problems, we package the received message as a
51 * sequencer_pcqitem_t and put onto a producer-consumer queue. A soft
52 * interrupt is scheduled to dequeue and decode the message later where we
53 * can safely acquire the sequencer device's sc_lock. PCQ is lockless for
54 * multiple producer, single consumer settings like this one.
55 */
56
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: sequencer.c,v 1.55 2012/04/09 10:18:16 plunky Exp $");
59
60 #include "sequencer.h"
61
62 #include <sys/param.h>
63 #include <sys/ioctl.h>
64 #include <sys/fcntl.h>
65 #include <sys/vnode.h>
66 #include <sys/select.h>
67 #include <sys/poll.h>
68 #include <sys/kmem.h>
69 #include <sys/proc.h>
70 #include <sys/systm.h>
71 #include <sys/syslog.h>
72 #include <sys/kernel.h>
73 #include <sys/signalvar.h>
74 #include <sys/conf.h>
75 #include <sys/audioio.h>
76 #include <sys/midiio.h>
77 #include <sys/device.h>
78 #include <sys/intr.h>
79 #include <sys/atomic.h>
80 #include <sys/pcq.h>
81 #include <sys/vnode.h>
82 #include <sys/kauth.h>
83
84 #include <dev/midi_if.h>
85 #include <dev/midivar.h>
86 #include <dev/sequencervar.h>
87
88 #define ADDTIMEVAL(a, b) ( \
89 (a)->tv_sec += (b)->tv_sec, \
90 (a)->tv_usec += (b)->tv_usec, \
91 (a)->tv_usec > 1000000 ? ((a)->tv_sec++, (a)->tv_usec -= 1000000) : 0\
92 )
93
94 #define SUBTIMEVAL(a, b) ( \
95 (a)->tv_sec -= (b)->tv_sec, \
96 (a)->tv_usec -= (b)->tv_usec, \
97 (a)->tv_usec < 0 ? ((a)->tv_sec--, (a)->tv_usec += 1000000) : 0\
98 )
99
100 #ifdef AUDIO_DEBUG
101 #define DPRINTF(x) if (sequencerdebug) printf x
102 #define DPRINTFN(n,x) if (sequencerdebug >= (n)) printf x
103 int sequencerdebug = 0;
104 #else
105 #define DPRINTF(x)
106 #define DPRINTFN(n,x)
107 #endif
108
109 #define SEQ_NOTE_MAX 128
110 #define SEQ_NOTE_XXX 255
111
112 #define RECALC_USPERDIV(t) \
113 ((t)->usperdiv = 60*1000000L/((t)->tempo_beatpermin*(t)->timebase_divperbeat))
114
115 typedef union sequencer_pcqitem {
116 void *qi_ptr;
117 char qi_msg[4];
118 } sequencer_pcqitem_t;
119
120 struct sequencer_softc seqdevs[NSEQUENCER];
121
122 void sequencerattach(int);
123 static void seq_reset(struct sequencer_softc *);
124 static int seq_do_command(struct sequencer_softc *, seq_event_t *);
125 static int seq_do_chnvoice(struct sequencer_softc *, seq_event_t *);
126 static int seq_do_chncommon(struct sequencer_softc *, seq_event_t *);
127 static void seq_timer_waitabs(struct sequencer_softc *, uint32_t);
128 static int seq_do_timing(struct sequencer_softc *, seq_event_t *);
129 static int seq_do_local(struct sequencer_softc *, seq_event_t *);
130 static int seq_do_sysex(struct sequencer_softc *, seq_event_t *);
131 static int seq_do_fullsize(struct sequencer_softc *, seq_event_t *, struct uio *);
132 static int seq_input_event(struct sequencer_softc *, seq_event_t *);
133 static int seq_drain(struct sequencer_softc *);
134 static void seq_startoutput(struct sequencer_softc *);
135 static void seq_timeout(void *);
136 static int seq_to_new(seq_event_t *, struct uio *);
137 static void seq_softintr(void *);
138
139 static int midiseq_out(struct midi_dev *, u_char *, u_int, int);
140 static struct midi_dev *midiseq_open(int, int);
141 static void midiseq_close(struct midi_dev *);
142 static void midiseq_reset(struct midi_dev *);
143 static int midiseq_noteon(struct midi_dev *, int, int, seq_event_t *);
144 static int midiseq_noteoff(struct midi_dev *, int, int, seq_event_t *);
145 static int midiseq_keypressure(struct midi_dev *, int, int, seq_event_t *);
146 static int midiseq_pgmchange(struct midi_dev *, int, seq_event_t *);
147 static int midiseq_chnpressure(struct midi_dev *, int, seq_event_t *);
148 static int midiseq_ctlchange(struct midi_dev *, int, seq_event_t *);
149 static int midiseq_pitchbend(struct midi_dev *, int, seq_event_t *);
150 static int midiseq_loadpatch(struct midi_dev *, struct sysex_info *, struct uio *);
151 void midiseq_in(struct midi_dev *, u_char *, int);
152
153 static dev_type_open(sequenceropen);
154 static dev_type_close(sequencerclose);
155 static dev_type_read(sequencerread);
156 static dev_type_write(sequencerwrite);
157 static dev_type_ioctl(sequencerioctl);
158 static dev_type_poll(sequencerpoll);
159 static dev_type_kqfilter(sequencerkqfilter);
160
161 const struct cdevsw sequencer_cdevsw = {
162 sequenceropen, sequencerclose, sequencerread, sequencerwrite,
163 sequencerioctl, nostop, notty, sequencerpoll, nommap,
164 sequencerkqfilter, D_OTHER | D_MPSAFE
165 };
166
167 void
168 sequencerattach(int n)
169 {
170 struct sequencer_softc *sc;
171
172 for (n = 0; n < NSEQUENCER; n++) {
173 sc = &seqdevs[n];
174 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
175 sc->sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
176 seq_softintr, sc);
177 mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE);
178 cv_init(&sc->rchan, "midiseqr");
179 cv_init(&sc->wchan, "midiseqw");
180 cv_init(&sc->lchan, "midiseql");
181 sc->pcq = pcq_create(SEQ_MAXQ, KM_SLEEP);
182 if (sc->pcq == NULL) {
183 panic("sequencerattach");
184 }
185 }
186 }
187
188 /*
189 * Release reference to device acquired with sequencer_enter().
190 */
191 static void
192 sequencer_exit(struct sequencer_softc *sc)
193 {
194
195 sc->dvlock--;
196 cv_broadcast(&sc->lchan);
197 mutex_exit(&sc->lock);
198 }
199
200 /*
201 * Look up sequencer device and acquire locks for device access.
202 */
203 static int
204 sequencer_enter(dev_t dev, struct sequencer_softc **scp)
205 {
206 struct sequencer_softc *sc;
207 int unit;
208
209 /* First, find the device and take sc_lock. */
210 unit = SEQUENCERUNIT(dev);
211 if (unit >= NSEQUENCER)
212 return (ENXIO);
213 sc = &seqdevs[unit];
214 if (sc == NULL)
215 return ENXIO;
216 mutex_enter(&sc->lock);
217 while (sc->dvlock) {
218 cv_wait(&sc->lchan, &sc->lock);
219 }
220 sc->dvlock++;
221 if (sc->dying) {
222 sequencer_exit(sc);
223 return EIO;
224 }
225 *scp = sc;
226 return 0;
227 }
228
229 static int
230 sequenceropen(dev_t dev, int flags, int ifmt, struct lwp *l)
231 {
232 int unit = SEQUENCERUNIT(dev);
233 struct sequencer_softc *sc;
234 struct midi_dev *md;
235 struct midi_softc *msc;
236 int error;
237
238 DPRINTF(("sequenceropen\n"));
239
240 if ((error = sequencer_enter(dev, &sc)) != 0)
241 return error;
242 KASSERT(sc == &seqdevs[unit]);
243 if (sc->isopen != 0) {
244 sequencer_exit(sc);
245 return EBUSY;
246 }
247
248 if (SEQ_IS_OLD(unit))
249 sc->mode = SEQ_OLD;
250 else
251 sc->mode = SEQ_NEW;
252 sc->isopen++;
253 sc->flags = flags & (FREAD|FWRITE);
254 sc->pbus = 0;
255 sc->async = 0;
256 sc->input_stamp = ~0;
257
258 sc->nmidi = 0;
259 sc->ndevs = midi_unit_count();
260 sc->timer.timebase_divperbeat = 100;
261 sc->timer.tempo_beatpermin = 60;
262 RECALC_USPERDIV(&sc->timer);
263 sc->timer.divs_lastevent = sc->timer.divs_lastchange = 0;
264 microtime(&sc->timer.reftime);
265
266 SEQ_QINIT(&sc->inq);
267 SEQ_QINIT(&sc->outq);
268 sc->lowat = SEQ_MAXQ / 2;
269
270 if (sc->ndevs > 0) {
271 mutex_exit(&sc->lock);
272 sc->devs = kmem_alloc(sc->ndevs * sizeof(struct midi_dev *),
273 KM_SLEEP);
274 for (unit = 0; unit < sc->ndevs; unit++) {
275 md = midiseq_open(unit, flags);
276 if (md) {
277 sc->devs[sc->nmidi++] = md;
278 md->seq = sc;
279 md->doingsysex = 0;
280 }
281 }
282 mutex_enter(&sc->lock);
283 } else {
284 sc->devs = NULL;
285 }
286
287 /* Only now redirect input from MIDI devices. */
288 for (unit = 0; unit < sc->nmidi; unit++) {
289 msc = sc->devs[unit]->msc;
290 mutex_enter(msc->lock);
291 msc->seqopen = 1;
292 mutex_exit(msc->lock);
293 }
294
295 seq_reset(sc);
296 sequencer_exit(sc);
297
298 DPRINTF(("%s: mode=%d, nmidi=%d\n", __func__, sc->mode, sc->nmidi));
299 return 0;
300 }
301
302 static int
303 seq_drain(struct sequencer_softc *sc)
304 {
305 int error;
306
307 KASSERT(mutex_owned(&sc->lock));
308
309 DPRINTFN(3, ("seq_drain: %p, len=%d\n", sc, SEQ_QLEN(&sc->outq)));
310 seq_startoutput(sc);
311 error = 0;
312 while (!SEQ_QEMPTY(&sc->outq) && !error)
313 error = cv_timedwait_sig(&sc->wchan, &sc->lock, 60*hz);
314 return (error);
315 }
316
317 static void
318 seq_timeout(void *addr)
319 {
320 struct sequencer_softc *sc = addr;
321 proc_t *p;
322 pid_t pid;
323
324 DPRINTFN(4, ("seq_timeout: %p\n", sc));
325
326 mutex_enter(&sc->lock);
327 if (sc->timeout == 0) {
328 mutex_spin_exit(&sc->lock);
329 return;
330 }
331 sc->timeout = 0;
332 seq_startoutput(sc);
333 if (SEQ_QLEN(&sc->outq) >= sc->lowat) {
334 mutex_exit(&sc->lock);
335 return;
336 }
337 cv_broadcast(&sc->wchan);
338 selnotify(&sc->wsel, 0, NOTE_SUBMIT);
339 if ((pid = sc->async) != 0) {
340 mutex_enter(proc_lock);
341 if ((p = proc_find(pid)) != NULL)
342 psignal(p, SIGIO);
343 mutex_exit(proc_lock);
344 }
345 mutex_exit(&sc->lock);
346 }
347
348 static void
349 seq_startoutput(struct sequencer_softc *sc)
350 {
351 struct sequencer_queue *q = &sc->outq;
352 seq_event_t cmd;
353
354 KASSERT(mutex_owned(&sc->lock));
355
356 if (sc->timeout)
357 return;
358 DPRINTFN(4, ("seq_startoutput: %p, len=%d\n", sc, SEQ_QLEN(q)));
359 while (!SEQ_QEMPTY(q) && !sc->timeout) {
360 SEQ_QGET(q, cmd);
361 seq_do_command(sc, &cmd);
362 }
363 }
364
365 static int
366 sequencerclose(dev_t dev, int flags, int ifmt, struct lwp *l)
367 {
368 struct sequencer_softc *sc;
369 struct midi_softc *msc;
370 int unit, error;
371
372 DPRINTF(("sequencerclose: %"PRIx64"\n", dev));
373
374 if ((error = sequencer_enter(dev, &sc)) != 0)
375 return error;
376 seq_drain(sc);
377 if (sc->timeout) {
378 callout_halt(&sc->sc_callout, &sc->lock);
379 sc->timeout = 0;
380 }
381 /* Bin input from MIDI devices. */
382 for (unit = 0; unit < sc->nmidi; unit++) {
383 msc = sc->devs[unit]->msc;
384 mutex_enter(msc->lock);
385 msc->seqopen = 0;
386 mutex_exit(msc->lock);
387 }
388 mutex_exit(&sc->lock);
389
390 for (unit = 0; unit < sc->nmidi; unit++)
391 if (sc->devs[unit] != NULL)
392 midiseq_close(sc->devs[unit]);
393 if (sc->devs != NULL) {
394 KASSERT(sc->ndevs > 0);
395 kmem_free(sc->devs, sc->ndevs * sizeof(struct midi_dev *));
396 sc->devs = NULL;
397 }
398
399 mutex_enter(&sc->lock);
400 sc->isopen = 0;
401 sequencer_exit(sc);
402
403 DPRINTF(("sequencerclose: %"PRIx64" done\n", dev));
404
405 return (0);
406 }
407
408 static int
409 seq_input_event(struct sequencer_softc *sc, seq_event_t *cmd)
410 {
411 struct sequencer_queue *q;
412
413 KASSERT(mutex_owned(&sc->lock));
414
415 DPRINTFN(2, ("seq_input_event: %02x %02x %02x %02x %02x "
416 "%02x %02x %02x\n", cmd->tag,
417 cmd->unknown.byte[0], cmd->unknown.byte[1],
418 cmd->unknown.byte[2], cmd->unknown.byte[3],
419 cmd->unknown.byte[4], cmd->unknown.byte[5],
420 cmd->unknown.byte[6]));
421 q = &sc->inq;
422 if (SEQ_QFULL(q))
423 return (ENOMEM);
424 SEQ_QPUT(q, *cmd);
425 cv_broadcast(&sc->rchan);
426 selnotify(&sc->rsel, 0, NOTE_SUBMIT);
427 if (sc->async != 0) {
428 proc_t *p;
429
430 mutex_enter(proc_lock);
431 if ((p = proc_find(sc->async)) != NULL)
432 psignal(p, SIGIO);
433 mutex_exit(proc_lock);
434 }
435 return 0;
436 }
437
438 static void
439 seq_softintr(void *addr)
440 {
441 struct sequencer_softc *sc;
442 struct timeval now;
443 seq_event_t ev;
444 int status, chan, unit;
445 sequencer_pcqitem_t qi;
446 u_long t;
447
448 sc = addr;
449
450 mutex_enter(&sc->lock);
451
452 qi.qi_ptr = pcq_get(sc->pcq);
453 if (qi.qi_ptr == NULL) {
454 mutex_exit(&sc->lock);
455 return;
456 }
457 KASSERT((qi.qi_msg[3] & 0x80) != 0);
458 unit = qi.qi_msg[3] & ~0x80;
459 status = MIDI_GET_STATUS(qi.qi_msg[0]);
460 chan = MIDI_GET_CHAN(qi.qi_msg[0]);
461 switch (status) {
462 case MIDI_NOTEON: /* midi(4) always canonicalizes hidden note-off */
463 ev = SEQ_MK_CHN(NOTEON, .device=unit, .channel=chan,
464 .key=qi.qi_msg[1], .velocity=qi.qi_msg[2]);
465 break;
466 case MIDI_NOTEOFF:
467 ev = SEQ_MK_CHN(NOTEOFF, .device=unit, .channel=chan,
468 .key=qi.qi_msg[1], .velocity=qi.qi_msg[2]);
469 break;
470 case MIDI_KEY_PRESSURE:
471 ev = SEQ_MK_CHN(KEY_PRESSURE, .device=unit, .channel=chan,
472 .key=qi.qi_msg[1], .pressure=qi.qi_msg[2]);
473 break;
474 case MIDI_CTL_CHANGE: /* XXX not correct for MSB */
475 ev = SEQ_MK_CHN(CTL_CHANGE, .device=unit, .channel=chan,
476 .controller=qi.qi_msg[1], .value=qi.qi_msg[2]);
477 break;
478 case MIDI_PGM_CHANGE:
479 ev = SEQ_MK_CHN(PGM_CHANGE, .device=unit, .channel=chan,
480 .program=qi.qi_msg[1]);
481 break;
482 case MIDI_CHN_PRESSURE:
483 ev = SEQ_MK_CHN(CHN_PRESSURE, .device=unit, .channel=chan,
484 .pressure=qi.qi_msg[1]);
485 break;
486 case MIDI_PITCH_BEND:
487 ev = SEQ_MK_CHN(PITCH_BEND, .device=unit, .channel=chan,
488 .value=(qi.qi_msg[1] & 0x7f) | ((qi.qi_msg[2] & 0x7f) << 7));
489 break;
490 default: /* this is now the point where MIDI_ACKs disappear */
491 mutex_exit(&sc->lock);
492 return;
493 }
494 microtime(&now);
495 if (!sc->timer.running)
496 now = sc->timer.stoptime;
497 SUBTIMEVAL(&now, &sc->timer.reftime);
498 t = now.tv_sec * 1000000 + now.tv_usec;
499 t /= sc->timer.usperdiv;
500 t += sc->timer.divs_lastchange;
501 if (t != sc->input_stamp) {
502 seq_input_event(sc, &SEQ_MK_TIMING(WAIT_ABS, .divisions=t));
503 sc->input_stamp = t; /* XXX wha hoppen if timer is reset? */
504 }
505 seq_input_event(sc, &ev);
506 mutex_exit(&sc->lock);
507 }
508
509 static int
510 sequencerread(dev_t dev, struct uio *uio, int ioflag)
511 {
512 struct sequencer_softc *sc;
513 struct sequencer_queue *q;
514 seq_event_t ev;
515 int error;
516
517 DPRINTFN(20, ("sequencerread: %"PRIx64", count=%d, ioflag=%x\n",
518 dev, (int)uio->uio_resid, ioflag));
519
520 if ((error = sequencer_enter(dev, &sc)) != 0)
521 return error;
522 q = &sc->inq;
523
524 if (sc->mode == SEQ_OLD) {
525 sequencer_exit(sc);
526 DPRINTFN(-1,("sequencerread: old read\n"));
527 return EINVAL; /* XXX unimplemented */
528 }
529 while (SEQ_QEMPTY(q)) {
530 if (ioflag & IO_NDELAY) {
531 error = EWOULDBLOCK;
532 break;
533 }
534 /* Drop lock to allow concurrent read/write. */
535 KASSERT(sc->dvlock != 0);
536 sc->dvlock--;
537 error = cv_wait_sig(&sc->rchan, &sc->lock);
538 while (sc->dvlock != 0) {
539 cv_wait(&sc->lchan, &sc->lock);
540 }
541 sc->dvlock++;
542 if (error) {
543 break;
544 }
545 }
546 while (uio->uio_resid >= sizeof(ev) && !error && !SEQ_QEMPTY(q)) {
547 SEQ_QGET(q, ev);
548 mutex_exit(&sc->lock);
549 error = uiomove(&ev, sizeof(ev), uio);
550 mutex_enter(&sc->lock);
551 }
552 sequencer_exit(sc);
553 return error;
554 }
555
556 static int
557 sequencerwrite(dev_t dev, struct uio *uio, int ioflag)
558 {
559 struct sequencer_softc *sc;
560 struct sequencer_queue *q;
561 int error;
562 seq_event_t cmdbuf;
563 int size;
564
565 DPRINTFN(2, ("sequencerwrite: %"PRIx64", count=%d\n", dev,
566 (int)uio->uio_resid));
567
568 if ((error = sequencer_enter(dev, &sc)) != 0)
569 return error;
570 q = &sc->outq;
571
572 size = sc->mode == SEQ_NEW ? sizeof cmdbuf : SEQOLD_CMDSIZE;
573 while (uio->uio_resid >= size && error == 0) {
574 mutex_exit(&sc->lock);
575 error = uiomove(&cmdbuf, size, uio);
576 if (error == 0) {
577 if (sc->mode == SEQ_OLD && seq_to_new(&cmdbuf, uio)) {
578 mutex_enter(&sc->lock);
579 continue;
580 }
581 if (cmdbuf.tag == SEQ_FULLSIZE) {
582 /* We do it like OSS does, asynchronously */
583 error = seq_do_fullsize(sc, &cmdbuf, uio);
584 if (error == 0) {
585 mutex_enter(&sc->lock);
586 continue;
587 }
588 }
589 }
590 mutex_enter(&sc->lock);
591 if (error != 0) {
592 break;
593 }
594 while (SEQ_QFULL(q)) {
595 seq_startoutput(sc);
596 if (SEQ_QFULL(q)) {
597 if (ioflag & IO_NDELAY) {
598 error = EWOULDBLOCK;
599 break;
600 }
601 error = cv_wait_sig(&sc->wchan, &sc->lock);
602 if (error) {
603 break;
604 }
605 }
606 }
607 if (error == 0) {
608 SEQ_QPUT(q, cmdbuf);
609 }
610 }
611 if (error == 0) {
612 seq_startoutput(sc);
613 } else {
614 DPRINTFN(2, ("sequencerwrite: error=%d\n", error));
615 }
616 sequencer_exit(sc);
617 return error;
618 }
619
620 static int
621 sequencerioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
622 {
623 struct sequencer_softc *sc;
624 struct synth_info *si;
625 struct midi_dev *md;
626 int devno, error, t;
627 struct timeval now;
628 u_long tx;
629
630 DPRINTFN(2, ("sequencerioctl: %"PRIx64" cmd=0x%08lx\n", dev, cmd));
631
632 if ((error = sequencer_enter(dev, &sc)) != 0)
633 return error;
634 switch (cmd) {
635 case FIONBIO:
636 /* All handled in the upper FS layer. */
637 break;
638
639 case FIOASYNC:
640 if (*(int *)addr) {
641 if (sc->async != 0)
642 return EBUSY;
643 sc->async = curproc->p_pid;
644 DPRINTF(("sequencer_ioctl: FIOASYNC %d\n",
645 sc->async));
646 } else {
647 sc->async = 0;
648 }
649 break;
650
651 case SEQUENCER_RESET:
652 seq_reset(sc);
653 break;
654
655 case SEQUENCER_PANIC:
656 seq_reset(sc);
657 /* Do more? OSS doesn't */
658 break;
659
660 case SEQUENCER_SYNC:
661 if (sc->flags != FREAD)
662 seq_drain(sc);
663 break;
664
665 case SEQUENCER_INFO:
666 si = (struct synth_info*)addr;
667 devno = si->device;
668 if (devno < 0 || devno >= sc->nmidi) {
669 error = EINVAL;
670 break;
671 }
672 md = sc->devs[devno];
673 strncpy(si->name, md->name, sizeof si->name);
674 si->synth_type = SYNTH_TYPE_MIDI;
675 si->synth_subtype = md->subtype;
676 si->nr_voices = md->nr_voices;
677 si->instr_bank_size = md->instr_bank_size;
678 si->capabilities = md->capabilities;
679 break;
680
681 case SEQUENCER_NRSYNTHS:
682 *(int *)addr = sc->nmidi;
683 break;
684
685 case SEQUENCER_NRMIDIS:
686 *(int *)addr = sc->nmidi;
687 break;
688
689 case SEQUENCER_OUTOFBAND:
690 DPRINTFN(3, ("sequencer_ioctl: OOB=%02x %02x %02x %02x %02x %02x %02x %02x\n",
691 *(u_char *)addr, *((u_char *)addr+1),
692 *((u_char *)addr+2), *((u_char *)addr+3),
693 *((u_char *)addr+4), *((u_char *)addr+5),
694 *((u_char *)addr+6), *((u_char *)addr+7)));
695 if ((sc->flags & FWRITE) == 0) {
696 error = EBADF;
697 } else {
698 error = seq_do_command(sc, (seq_event_t *)addr);
699 }
700 break;
701
702 case SEQUENCER_TMR_TIMEBASE:
703 t = *(int *)addr;
704 if (t < 1)
705 t = 1;
706 if (t > 10000)
707 t = 10000;
708 *(int *)addr = t;
709 sc->timer.timebase_divperbeat = t;
710 sc->timer.divs_lastchange = sc->timer.divs_lastevent;
711 microtime(&sc->timer.reftime);
712 RECALC_USPERDIV(&sc->timer);
713 break;
714
715 case SEQUENCER_TMR_START:
716 error = seq_do_timing(sc, &SEQ_MK_TIMING(START));
717 break;
718
719 case SEQUENCER_TMR_STOP:
720 error = seq_do_timing(sc, &SEQ_MK_TIMING(STOP));
721 break;
722
723 case SEQUENCER_TMR_CONTINUE:
724 error = seq_do_timing(sc, &SEQ_MK_TIMING(CONTINUE));
725 break;
726
727 case SEQUENCER_TMR_TEMPO:
728 error = seq_do_timing(sc,
729 &SEQ_MK_TIMING(TEMPO, .bpm=*(int *)addr));
730 if (error == 0)
731 *(int *)addr = sc->timer.tempo_beatpermin;
732 break;
733
734 case SEQUENCER_TMR_SOURCE:
735 *(int *)addr = SEQUENCER_TMR_INTERNAL;
736 break;
737
738 case SEQUENCER_TMR_METRONOME:
739 /* noop */
740 break;
741
742 case SEQUENCER_THRESHOLD:
743 t = SEQ_MAXQ - *(int *)addr / sizeof (seq_event_rec);
744 if (t < 1)
745 t = 1;
746 if (t > SEQ_MAXQ)
747 t = SEQ_MAXQ;
748 sc->lowat = t;
749 break;
750
751 case SEQUENCER_CTRLRATE:
752 *(int *)addr = (sc->timer.tempo_beatpermin
753 *sc->timer.timebase_divperbeat + 30) / 60;
754 break;
755
756 case SEQUENCER_GETTIME:
757 microtime(&now);
758 SUBTIMEVAL(&now, &sc->timer.reftime);
759 tx = now.tv_sec * 1000000 + now.tv_usec;
760 tx /= sc->timer.usperdiv;
761 tx += sc->timer.divs_lastchange;
762 *(int *)addr = tx;
763 break;
764
765 default:
766 DPRINTFN(-1,("sequencer_ioctl: unimpl %08lx\n", cmd));
767 error = EINVAL;
768 break;
769 }
770 sequencer_exit(sc);
771
772 return error;
773 }
774
775 static int
776 sequencerpoll(dev_t dev, int events, struct lwp *l)
777 {
778 struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
779 int revents = 0;
780
781 DPRINTF(("sequencerpoll: %p events=0x%x\n", sc, events));
782
783 mutex_enter(&sc->lock);
784 if (events & (POLLIN | POLLRDNORM))
785 if ((sc->flags&FREAD) && !SEQ_QEMPTY(&sc->inq))
786 revents |= events & (POLLIN | POLLRDNORM);
787
788 if (events & (POLLOUT | POLLWRNORM))
789 if ((sc->flags&FWRITE) && SEQ_QLEN(&sc->outq) < sc->lowat)
790 revents |= events & (POLLOUT | POLLWRNORM);
791
792 if (revents == 0) {
793 if ((sc->flags&FREAD) && (events & (POLLIN | POLLRDNORM)))
794 selrecord(l, &sc->rsel);
795
796 if ((sc->flags&FWRITE) && (events & (POLLOUT | POLLWRNORM)))
797 selrecord(l, &sc->wsel);
798 }
799 mutex_exit(&sc->lock);
800
801 return revents;
802 }
803
804 static void
805 filt_sequencerrdetach(struct knote *kn)
806 {
807 struct sequencer_softc *sc = kn->kn_hook;
808
809 mutex_enter(&sc->lock);
810 SLIST_REMOVE(&sc->rsel.sel_klist, kn, knote, kn_selnext);
811 mutex_exit(&sc->lock);
812 }
813
814 static int
815 filt_sequencerread(struct knote *kn, long hint)
816 {
817 struct sequencer_softc *sc = kn->kn_hook;
818 int rv;
819
820 if (hint != NOTE_SUBMIT) {
821 mutex_enter(&sc->lock);
822 }
823 if (SEQ_QEMPTY(&sc->inq)) {
824 rv = 0;
825 } else {
826 kn->kn_data = sizeof(seq_event_rec);
827 rv = 1;
828 }
829 if (hint != NOTE_SUBMIT) {
830 mutex_exit(&sc->lock);
831 }
832 return rv;
833 }
834
835 static const struct filterops sequencerread_filtops =
836 { 1, NULL, filt_sequencerrdetach, filt_sequencerread };
837
838 static void
839 filt_sequencerwdetach(struct knote *kn)
840 {
841 struct sequencer_softc *sc = kn->kn_hook;
842
843 mutex_enter(&sc->lock);
844 SLIST_REMOVE(&sc->wsel.sel_klist, kn, knote, kn_selnext);
845 mutex_exit(&sc->lock);
846 }
847
848 static int
849 filt_sequencerwrite(struct knote *kn, long hint)
850 {
851 struct sequencer_softc *sc = kn->kn_hook;
852 int rv;
853
854 if (hint != NOTE_SUBMIT) {
855 mutex_enter(&sc->lock);
856 }
857 if (SEQ_QLEN(&sc->outq) >= sc->lowat) {
858 rv = 0;
859 } else {
860 kn->kn_data = sizeof(seq_event_rec);
861 rv = 1;
862 }
863 if (hint != NOTE_SUBMIT) {
864 mutex_exit(&sc->lock);
865 }
866 return rv;
867 }
868
869 static const struct filterops sequencerwrite_filtops =
870 { 1, NULL, filt_sequencerwdetach, filt_sequencerwrite };
871
872 static int
873 sequencerkqfilter(dev_t dev, struct knote *kn)
874 {
875 struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
876 struct klist *klist;
877
878 switch (kn->kn_filter) {
879 case EVFILT_READ:
880 klist = &sc->rsel.sel_klist;
881 kn->kn_fop = &sequencerread_filtops;
882 break;
883
884 case EVFILT_WRITE:
885 klist = &sc->wsel.sel_klist;
886 kn->kn_fop = &sequencerwrite_filtops;
887 break;
888
889 default:
890 return (EINVAL);
891 }
892
893 kn->kn_hook = sc;
894
895 mutex_enter(&sc->lock);
896 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
897 mutex_exit(&sc->lock);
898
899 return (0);
900 }
901
902 static void
903 seq_reset(struct sequencer_softc *sc)
904 {
905 int i, chn;
906 struct midi_dev *md;
907
908 KASSERT(mutex_owned(&sc->lock));
909
910 if ( !(sc->flags & FWRITE) )
911 return;
912 for (i = 0; i < sc->nmidi; i++) {
913 md = sc->devs[i];
914 midiseq_reset(md);
915 for (chn = 0; chn < MAXCHAN; chn++) {
916 midiseq_ctlchange(md, chn, &SEQ_MK_CHN(CTL_CHANGE,
917 .controller=MIDI_CTRL_NOTES_OFF));
918 midiseq_ctlchange(md, chn, &SEQ_MK_CHN(CTL_CHANGE,
919 .controller=MIDI_CTRL_RESET));
920 midiseq_pitchbend(md, chn, &SEQ_MK_CHN(PITCH_BEND,
921 .value=MIDI_BEND_NEUTRAL));
922 }
923 }
924 }
925
926 static int
927 seq_do_command(struct sequencer_softc *sc, seq_event_t *b)
928 {
929 int dev;
930
931 KASSERT(mutex_owned(&sc->lock));
932
933 DPRINTFN(4, ("seq_do_command: %p cmd=0x%02x\n", sc, b->timing.op));
934
935 switch(b->tag) {
936 case SEQ_LOCAL:
937 return seq_do_local(sc, b);
938 case SEQ_TIMING:
939 return seq_do_timing(sc, b);
940 case SEQ_CHN_VOICE:
941 return seq_do_chnvoice(sc, b);
942 case SEQ_CHN_COMMON:
943 return seq_do_chncommon(sc, b);
944 case SEQ_SYSEX:
945 return seq_do_sysex(sc, b);
946 /* COMPAT */
947 case SEQOLD_MIDIPUTC:
948 dev = b->putc.device;
949 if (dev < 0 || dev >= sc->nmidi)
950 return (ENXIO);
951 return midiseq_out(sc->devs[dev], &b->putc.byte, 1, 0);
952 default:
953 DPRINTFN(-1,("seq_do_command: unimpl command %02x\n", b->tag));
954 return (EINVAL);
955 }
956 }
957
958 static int
959 seq_do_chnvoice(struct sequencer_softc *sc, seq_event_t *b)
960 {
961 int dev;
962 int error;
963 struct midi_dev *md;
964
965 KASSERT(mutex_owned(&sc->lock));
966
967 dev = b->voice.device;
968 if (dev < 0 || dev >= sc->nmidi ||
969 b->voice.channel > 15 ||
970 b->voice.key >= SEQ_NOTE_MAX)
971 return ENXIO;
972 md = sc->devs[dev];
973 switch(b->voice.op) {
974 case MIDI_NOTEON: /* no need to special-case hidden noteoff here */
975 error = midiseq_noteon(md, b->voice.channel, b->voice.key, b);
976 break;
977 case MIDI_NOTEOFF:
978 error = midiseq_noteoff(md, b->voice.channel, b->voice.key, b);
979 break;
980 case MIDI_KEY_PRESSURE:
981 error = midiseq_keypressure(md,
982 b->voice.channel, b->voice.key, b);
983 break;
984 default:
985 DPRINTFN(-1,("seq_do_chnvoice: unimpl command %02x\n",
986 b->voice.op));
987 error = EINVAL;
988 break;
989 }
990 return error;
991 }
992
993 static int
994 seq_do_chncommon(struct sequencer_softc *sc, seq_event_t *b)
995 {
996 int dev;
997 int error;
998 struct midi_dev *md;
999
1000 KASSERT(mutex_owned(&sc->lock));
1001
1002 dev = b->common.device;
1003 if (dev < 0 || dev >= sc->nmidi ||
1004 b->common.channel > 15)
1005 return ENXIO;
1006 md = sc->devs[dev];
1007 DPRINTFN(2,("seq_do_chncommon: %02x\n", b->common.op));
1008
1009 error = 0;
1010 switch(b->common.op) {
1011 case MIDI_PGM_CHANGE:
1012 error = midiseq_pgmchange(md, b->common.channel, b);
1013 break;
1014 case MIDI_CTL_CHANGE:
1015 error = midiseq_ctlchange(md, b->common.channel, b);
1016 break;
1017 case MIDI_PITCH_BEND:
1018 error = midiseq_pitchbend(md, b->common.channel, b);
1019 break;
1020 case MIDI_CHN_PRESSURE:
1021 error = midiseq_chnpressure(md, b->common.channel, b);
1022 break;
1023 default:
1024 DPRINTFN(-1,("seq_do_chncommon: unimpl command %02x\n",
1025 b->common.op));
1026 error = EINVAL;
1027 break;
1028 }
1029 return error;
1030 }
1031
1032 static int
1033 seq_do_local(struct sequencer_softc *sc, seq_event_t *b)
1034 {
1035
1036 KASSERT(mutex_owned(&sc->lock));
1037
1038 return (EINVAL);
1039 }
1040
1041 static int
1042 seq_do_sysex(struct sequencer_softc *sc, seq_event_t *b)
1043 {
1044 int dev, i;
1045 struct midi_dev *md;
1046 uint8_t *bf = b->sysex.buffer;
1047
1048 KASSERT(mutex_owned(&sc->lock));
1049
1050 dev = b->sysex.device;
1051 if (dev < 0 || dev >= sc->nmidi)
1052 return (ENXIO);
1053 DPRINTF(("seq_do_sysex: dev=%d\n", dev));
1054 md = sc->devs[dev];
1055
1056 if (!md->doingsysex) {
1057 midiseq_out(md, (uint8_t[]){MIDI_SYSEX_START}, 1, 0);
1058 md->doingsysex = 1;
1059 }
1060
1061 for (i = 0; i < 6 && bf[i] != 0xff; i++)
1062 ;
1063 midiseq_out(md, bf, i, 0);
1064 if (i < 6 || (i > 0 && bf[i-1] == MIDI_SYSEX_END))
1065 md->doingsysex = 0;
1066 return 0;
1067 }
1068
1069 static void
1070 seq_timer_waitabs(struct sequencer_softc *sc, uint32_t divs)
1071 {
1072 struct timeval when;
1073 long long usec;
1074 struct syn_timer *t;
1075 int ticks;
1076
1077 KASSERT(mutex_owned(&sc->lock));
1078
1079 t = &sc->timer;
1080 t->divs_lastevent = divs;
1081 divs -= t->divs_lastchange;
1082 usec = (long long)divs * (long long)t->usperdiv; /* convert to usec */
1083 when.tv_sec = usec / 1000000;
1084 when.tv_usec = usec % 1000000;
1085 DPRINTFN(4, ("seq_timer_waitabs: adjdivs=%d, sleep when=%"PRId64".%06"PRId64,
1086 divs, when.tv_sec, (uint64_t)when.tv_usec));
1087 ADDTIMEVAL(&when, &t->reftime); /* abstime for end */
1088 ticks = tvhzto(&when);
1089 DPRINTFN(4, (" when+start=%"PRId64".%06"PRId64", tick=%d\n",
1090 when.tv_sec, (uint64_t)when.tv_usec, ticks));
1091 if (ticks > 0) {
1092 #ifdef DIAGNOSTIC
1093 if (ticks > 20 * hz) {
1094 /* Waiting more than 20s */
1095 printf("seq_timer_waitabs: funny ticks=%d, "
1096 "usec=%lld\n", ticks, usec);
1097 }
1098 #endif
1099 sc->timeout = 1;
1100 callout_reset(&sc->sc_callout, ticks,
1101 seq_timeout, sc);
1102 }
1103 #ifdef SEQUENCER_DEBUG
1104 else if (tick < 0)
1105 DPRINTF(("seq_timer_waitabs: ticks = %d\n", ticks));
1106 #endif
1107 }
1108
1109 static int
1110 seq_do_timing(struct sequencer_softc *sc, seq_event_t *b)
1111 {
1112 struct syn_timer *t = &sc->timer;
1113 struct timeval when;
1114 int error;
1115
1116 KASSERT(mutex_owned(&sc->lock));
1117
1118 error = 0;
1119 switch(b->timing.op) {
1120 case TMR_WAIT_REL:
1121 seq_timer_waitabs(sc,
1122 b->t_WAIT_REL.divisions + t->divs_lastevent);
1123 break;
1124 case TMR_WAIT_ABS:
1125 seq_timer_waitabs(sc, b->t_WAIT_ABS.divisions);
1126 break;
1127 case TMR_START:
1128 microtime(&t->reftime);
1129 t->divs_lastevent = t->divs_lastchange = 0;
1130 t->running = 1;
1131 break;
1132 case TMR_STOP:
1133 microtime(&t->stoptime);
1134 t->running = 0;
1135 break;
1136 case TMR_CONTINUE:
1137 if (t->running)
1138 break;
1139 microtime(&when);
1140 SUBTIMEVAL(&when, &t->stoptime);
1141 ADDTIMEVAL(&t->reftime, &when);
1142 t->running = 1;
1143 break;
1144 case TMR_TEMPO:
1145 /* bpm is unambiguously MIDI clocks per minute / 24 */
1146 /* (24 MIDI clocks are usually but not always a quarter note) */
1147 if (b->t_TEMPO.bpm < 8) /* where are these limits specified? */
1148 t->tempo_beatpermin = 8;
1149 else if (b->t_TEMPO.bpm > 360) /* ? */
1150 t->tempo_beatpermin = 360;
1151 else
1152 t->tempo_beatpermin = b->t_TEMPO.bpm;
1153 t->divs_lastchange = t->divs_lastevent;
1154 microtime(&t->reftime);
1155 RECALC_USPERDIV(t);
1156 break;
1157 case TMR_ECHO:
1158 error = seq_input_event(sc, b);
1159 break;
1160 case TMR_RESET:
1161 t->divs_lastevent = t->divs_lastchange = 0;
1162 microtime(&t->reftime);
1163 break;
1164 case TMR_SPP:
1165 case TMR_TIMESIG:
1166 DPRINTF(("seq_do_timing: unimplemented %02x\n", b->timing.op));
1167 error = EINVAL; /* not quite accurate... */
1168 break;
1169 default:
1170 DPRINTF(("seq_timer: unknown %02x\n", b->timing.op));
1171 error = EINVAL;
1172 break;
1173 }
1174 return (error);
1175 }
1176
1177 static int
1178 seq_do_fullsize(struct sequencer_softc *sc, seq_event_t *b, struct uio *uio)
1179 {
1180 struct sysex_info sysex;
1181 u_int dev;
1182
1183 #ifdef DIAGNOSTIC
1184 if (sizeof(seq_event_rec) != SEQ_SYSEX_HDRSIZE) {
1185 printf("seq_do_fullsize: sysex size ??\n");
1186 return EINVAL;
1187 }
1188 #endif
1189 memcpy(&sysex, b, sizeof sysex);
1190 dev = sysex.device_no;
1191 if (/* dev < 0 || */ dev >= sc->nmidi)
1192 return (ENXIO);
1193 DPRINTFN(2, ("seq_do_fullsize: fmt=%04x, dev=%d, len=%d\n",
1194 sysex.key, dev, sysex.len));
1195 return (midiseq_loadpatch(sc->devs[dev], &sysex, uio));
1196 }
1197
1198 /*
1199 * Convert an old sequencer event to a new one.
1200 * NOTE: on entry, *ev may contain valid data only in the first 4 bytes.
1201 * That may be true even on exit (!) in the case of SEQOLD_MIDIPUTC; the
1202 * caller will only look at the first bytes in that case anyway. Ugly? Sure.
1203 */
1204 static int
1205 seq_to_new(seq_event_t *ev, struct uio *uio)
1206 {
1207 int cmd, chan, note, parm;
1208 uint32_t tmp_delay;
1209 int error;
1210 uint8_t *bfp;
1211
1212 cmd = ev->tag;
1213 bfp = ev->unknown.byte;
1214 chan = *bfp++;
1215 note = *bfp++;
1216 parm = *bfp++;
1217 DPRINTFN(3, ("seq_to_new: 0x%02x %d %d %d\n", cmd, chan, note, parm));
1218
1219 if (cmd >= 0x80) {
1220 /* Fill the event record */
1221 if (uio->uio_resid >= sizeof *ev - SEQOLD_CMDSIZE) {
1222 error = uiomove(bfp, sizeof *ev - SEQOLD_CMDSIZE, uio);
1223 if (error)
1224 return error;
1225 } else
1226 return EINVAL;
1227 }
1228
1229 switch(cmd) {
1230 case SEQOLD_NOTEOFF:
1231 /*
1232 * What's with the SEQ_NOTE_XXX? In OSS this seems to have
1233 * been undocumented magic for messing with the overall volume
1234 * of a 'voice', equated precariously with 'channel' and
1235 * pretty much unimplementable except by directly frobbing a
1236 * synth chip. For us, who treat everything as interfaced over
1237 * MIDI, this will just be unceremoniously discarded as
1238 * invalid in midiseq_noteoff, making the whole event an
1239 * elaborate no-op, and that doesn't seem to be any different
1240 * from what happens on linux with a MIDI-interfaced device,
1241 * by the way. The moral is ... use the new /dev/music API, ok?
1242 */
1243 *ev = SEQ_MK_CHN(NOTEOFF, .device=0, .channel=chan,
1244 .key=SEQ_NOTE_XXX, .velocity=parm);
1245 break;
1246 case SEQOLD_NOTEON:
1247 *ev = SEQ_MK_CHN(NOTEON,
1248 .device=0, .channel=chan, .key=note, .velocity=parm);
1249 break;
1250 case SEQOLD_WAIT:
1251 /*
1252 * This event cannot even /exist/ on non-littleendian machines,
1253 * and so help me, that's exactly the way OSS defined it.
1254 * Also, the OSS programmer's guide states (p. 74, v1.11)
1255 * that seqold time units are system clock ticks, unlike
1256 * the new 'divisions' which are determined by timebase. In
1257 * that case we would need to do scaling here - but no such
1258 * behavior is visible in linux either--which also treats this
1259 * value, surprisingly, as an absolute, not relative, time.
1260 * My guess is that this event has gone unused so long that
1261 * nobody could agree we got it wrong no matter what we do.
1262 */
1263 tmp_delay = *(uint32_t *)ev >> 8;
1264 *ev = SEQ_MK_TIMING(WAIT_ABS, .divisions=tmp_delay);
1265 break;
1266 case SEQOLD_SYNCTIMER:
1267 /*
1268 * The TMR_RESET event is not defined in any OSS materials
1269 * I can find; it may have been invented here just to provide
1270 * an accurate _to_new translation of this event.
1271 */
1272 *ev = SEQ_MK_TIMING(RESET);
1273 break;
1274 case SEQOLD_PGMCHANGE:
1275 *ev = SEQ_MK_CHN(PGM_CHANGE,
1276 .device=0, .channel=chan, .program=note);
1277 break;
1278 case SEQOLD_MIDIPUTC:
1279 break; /* interpret in normal mode */
1280 case SEQOLD_ECHO:
1281 case SEQOLD_PRIVATE:
1282 case SEQOLD_EXTENDED:
1283 default:
1284 DPRINTF(("seq_to_new: not impl 0x%02x\n", cmd));
1285 return EINVAL;
1286 /* In case new-style events show up */
1287 case SEQ_TIMING:
1288 case SEQ_CHN_VOICE:
1289 case SEQ_CHN_COMMON:
1290 case SEQ_FULLSIZE:
1291 break;
1292 }
1293 return 0;
1294 }
1295
1296 /**********************************************/
1297
1298 void
1299 midiseq_in(struct midi_dev *md, u_char *msg, int len)
1300 {
1301 struct sequencer_softc *sc;
1302 sequencer_pcqitem_t qi;
1303
1304 DPRINTFN(2, ("midiseq_in: %p %02x %02x %02x\n",
1305 md, msg[0], msg[1], msg[2]));
1306
1307 sc = md->seq;
1308
1309 qi.qi_msg[0] = msg[0];
1310 qi.qi_msg[1] = msg[1];
1311 qi.qi_msg[2] = msg[2];
1312 qi.qi_msg[3] = md->unit | 0x80; /* ensure non-zero value of qi_ptr */
1313 pcq_put(sc->pcq, qi.qi_ptr);
1314 softint_schedule(sc->sih);
1315 }
1316
1317 static struct midi_dev *
1318 midiseq_open(int unit, int flags)
1319 {
1320 extern struct cfdriver midi_cd;
1321 int error;
1322 struct midi_dev *md;
1323 struct midi_softc *sc;
1324 struct midi_info mi;
1325 int major;
1326 dev_t dev;
1327 vnode_t *vp;
1328 int oflags;
1329
1330 major = devsw_name2chr("midi", NULL, 0);
1331 dev = makedev(major, unit);
1332
1333 DPRINTFN(2, ("midiseq_open: %d %d\n", unit, flags));
1334
1335 error = cdevvp(dev, &vp);
1336 if (error)
1337 return NULL;
1338 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1339 error = VOP_OPEN(vp, flags, kauth_cred_get());
1340 VOP_UNLOCK(vp);
1341 if (error) {
1342 vrele(vp);
1343 return NULL;
1344 }
1345
1346 /* Only after we have acquired reference via VOP_OPEN(). */
1347 midi_getinfo(dev, &mi);
1348 oflags = flags;
1349 if ((mi.props & MIDI_PROP_CAN_INPUT) == 0)
1350 flags &= ~FREAD;
1351 if ((flags & (FREAD|FWRITE)) == 0) {
1352 VOP_CLOSE(vp, oflags, kauth_cred_get());
1353 vrele(vp);
1354 return NULL;
1355 }
1356
1357 sc = device_lookup_private(&midi_cd, unit);
1358 md = kmem_zalloc(sizeof(*md), KM_SLEEP);
1359 md->msc = sc;
1360 md->unit = unit;
1361 md->name = mi.name;
1362 md->subtype = 0;
1363 md->nr_voices = 128; /* XXX */
1364 md->instr_bank_size = 128; /* XXX */
1365 md->vp = vp;
1366 if (mi.props & MIDI_PROP_CAN_INPUT)
1367 md->capabilities |= SYNTH_CAP_INPUT;
1368 sc->seq_md = md;
1369 return (md);
1370 }
1371
1372 static void
1373 midiseq_close(struct midi_dev *md)
1374 {
1375 int major;
1376 dev_t dev;
1377
1378 major = devsw_name2chr("midi", NULL, 0);
1379 dev = makedev(major, md->unit);
1380
1381 DPRINTFN(2, ("midiseq_close: %d\n", md->unit));
1382 (void)vn_close(md->vp, 0, kauth_cred_get());
1383 kmem_free(md, sizeof(*md));
1384 }
1385
1386 static void
1387 midiseq_reset(struct midi_dev *md)
1388 {
1389 /* XXX send GM reset? */
1390 DPRINTFN(3, ("midiseq_reset: %d\n", md->unit));
1391 }
1392
1393 static int
1394 midiseq_out(struct midi_dev *md, u_char *bf, u_int cc, int chk)
1395 {
1396 DPRINTFN(5, ("midiseq_out: m=%p, unit=%d, bf[0]=0x%02x, cc=%d\n",
1397 md->msc, md->unit, bf[0], cc));
1398
1399 /* midi(4) does running status compression where appropriate. */
1400 return midi_writebytes(md->unit, bf, cc);
1401 }
1402
1403 /*
1404 * If the writing process hands us a hidden note-off in a note-on event,
1405 * we will simply write it that way; no need to special case it here,
1406 * as midi(4) will always canonicalize or compress as appropriate anyway.
1407 */
1408 static int
1409 midiseq_noteon(struct midi_dev *md, int chan, int key, seq_event_t *ev)
1410 {
1411 return midiseq_out(md, (uint8_t[]){
1412 MIDI_NOTEON | chan, key, ev->c_NOTEON.velocity & 0x7f}, 3, 1);
1413 }
1414
1415 static int
1416 midiseq_noteoff(struct midi_dev *md, int chan, int key, seq_event_t *ev)
1417 {
1418 return midiseq_out(md, (uint8_t[]){
1419 MIDI_NOTEOFF | chan, key, ev->c_NOTEOFF.velocity & 0x7f}, 3, 1);
1420 }
1421
1422 static int
1423 midiseq_keypressure(struct midi_dev *md, int chan, int key, seq_event_t *ev)
1424 {
1425 return midiseq_out(md, (uint8_t[]){
1426 MIDI_KEY_PRESSURE | chan, key,
1427 ev->c_KEY_PRESSURE.pressure & 0x7f}, 3, 1);
1428 }
1429
1430 static int
1431 midiseq_pgmchange(struct midi_dev *md, int chan, seq_event_t *ev)
1432 {
1433 if (ev->c_PGM_CHANGE.program > 127)
1434 return EINVAL;
1435 return midiseq_out(md, (uint8_t[]){
1436 MIDI_PGM_CHANGE | chan, ev->c_PGM_CHANGE.program}, 2, 1);
1437 }
1438
1439 static int
1440 midiseq_chnpressure(struct midi_dev *md, int chan, seq_event_t *ev)
1441 {
1442 if (ev->c_CHN_PRESSURE.pressure > 127)
1443 return EINVAL;
1444 return midiseq_out(md, (uint8_t[]){
1445 MIDI_CHN_PRESSURE | chan, ev->c_CHN_PRESSURE.pressure}, 2, 1);
1446 }
1447
1448 static int
1449 midiseq_ctlchange(struct midi_dev *md, int chan, seq_event_t *ev)
1450 {
1451 if (ev->c_CTL_CHANGE.controller > 127)
1452 return EINVAL;
1453 return midiseq_out( md, (uint8_t[]){
1454 MIDI_CTL_CHANGE | chan, ev->c_CTL_CHANGE.controller,
1455 ev->c_CTL_CHANGE.value & 0x7f /* XXX this is SO wrong */
1456 }, 3, 1);
1457 }
1458
1459 static int
1460 midiseq_pitchbend(struct midi_dev *md, int chan, seq_event_t *ev)
1461 {
1462 return midiseq_out(md, (uint8_t[]){
1463 MIDI_PITCH_BEND | chan,
1464 ev->c_PITCH_BEND.value & 0x7f,
1465 (ev->c_PITCH_BEND.value >> 7) & 0x7f}, 3, 1);
1466 }
1467
1468 static int
1469 midiseq_loadpatch(struct midi_dev *md,
1470 struct sysex_info *sysex, struct uio *uio)
1471 {
1472 struct sequencer_softc *sc;
1473 u_char c, bf[128];
1474 int i, cc, error;
1475
1476 if (sysex->key != SEQ_SYSEX_PATCH) {
1477 DPRINTFN(-1,("midiseq_loadpatch: bad patch key 0x%04x\n",
1478 sysex->key));
1479 return (EINVAL);
1480 }
1481 if (uio->uio_resid < sysex->len)
1482 /* adjust length, should be an error */
1483 sysex->len = uio->uio_resid;
1484
1485 DPRINTFN(2, ("midiseq_loadpatch: len=%d\n", sysex->len));
1486 if (sysex->len == 0)
1487 return EINVAL;
1488 error = uiomove(&c, 1, uio);
1489 if (error)
1490 return error;
1491 if (c != MIDI_SYSEX_START) /* must start like this */
1492 return EINVAL;
1493 sc = md->seq;
1494 mutex_enter(&sc->lock);
1495 error = midiseq_out(md, &c, 1, 0);
1496 mutex_exit(&sc->lock);
1497 if (error)
1498 return error;
1499 --sysex->len;
1500 while (sysex->len > 0) {
1501 cc = sysex->len;
1502 if (cc > sizeof bf)
1503 cc = sizeof bf;
1504 error = uiomove(bf, cc, uio);
1505 if (error)
1506 break;
1507 for(i = 0; i < cc && !MIDI_IS_STATUS(bf[i]); i++)
1508 ;
1509 /*
1510 * XXX midi(4)'s buffer might not accommodate this, and the
1511 * function will not block us (though in this case we have
1512 * a process and could in principle block).
1513 */
1514 mutex_enter(&sc->lock);
1515 error = midiseq_out(md, bf, i, 0);
1516 mutex_exit(&sc->lock);
1517 if (error)
1518 break;
1519 sysex->len -= i;
1520 if (i != cc)
1521 break;
1522 }
1523 /*
1524 * Any leftover data in uio is rubbish;
1525 * the SYSEX should be one write ending in SYSEX_END.
1526 */
1527 uio->uio_resid = 0;
1528 c = MIDI_SYSEX_END;
1529 mutex_enter(&sc->lock);
1530 error = midiseq_out(md, &c, 1, 0);
1531 mutex_exit(&sc->lock);
1532 return error;
1533 }
1534
1535 #include "midi.h"
1536 #if NMIDI == 0
1537 static dev_type_open(midiopen);
1538 static dev_type_close(midiclose);
1539
1540 const struct cdevsw midi_cdevsw = {
1541 midiopen, midiclose, noread, nowrite, noioctl,
1542 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE
1543 };
1544
1545 /*
1546 * If someone has a sequencer, but no midi devices there will
1547 * be unresolved references, so we provide little stubs.
1548 */
1549
1550 int
1551 midi_unit_count(void)
1552 {
1553 return (0);
1554 }
1555
1556 static int
1557 midiopen(dev_t dev, int flags, int ifmt, struct lwp *l)
1558 {
1559 return (ENXIO);
1560 }
1561
1562 struct cfdriver midi_cd;
1563
1564 void
1565 midi_getinfo(dev_t dev, struct midi_info *mi)
1566 {
1567 mi->name = "Dummy MIDI device";
1568 mi->props = 0;
1569 }
1570
1571 static int
1572 midiclose(dev_t dev, int flags, int ifmt, struct lwp *l)
1573 {
1574 return (ENXIO);
1575 }
1576
1577 int
1578 midi_writebytes(int unit, u_char *bf, int cc)
1579 {
1580 return (ENXIO);
1581 }
1582 #endif /* NMIDI == 0 */
1583