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