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