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