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