xref: /openbsd-src/sys/dev/midi.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /*	$OpenBSD: midi.c,v 1.21 2009/11/09 17:53:39 nicm Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, 2004 Alexandre Ratchov
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * TODO
21  *	- put the sequencer stuff in sequencer.c and sequencervar.h
22  *	  there is no reason to have it here. The sequencer
23  *	  driver need only to open the midi hw_if thus it does not
24  *	  need this driver
25  */
26 
27 #include "midi.h"
28 #include "sequencer.h"
29 #if NMIDI > 0
30 
31 #include <sys/param.h>
32 #include <sys/fcntl.h>
33 #include <sys/systm.h>
34 #include <sys/ioctl.h>
35 #include <sys/exec.h>
36 #include <sys/conf.h>
37 #include <sys/lkm.h>
38 #include <sys/proc.h>
39 #include <sys/poll.h>
40 #include <sys/kernel.h>
41 #include <sys/timeout.h>
42 #include <sys/vnode.h>
43 #include <sys/signalvar.h>
44 #include <sys/malloc.h>
45 #include <sys/device.h>
46 
47 #include <dev/midi_if.h>
48 #include <dev/audio_if.h>
49 #include <dev/midivar.h>
50 
51 
52 int     midiopen(dev_t, int, int, struct proc *);
53 int     midiclose(dev_t, int, int, struct proc *);
54 int     midiread(dev_t, struct uio *, int);
55 int     midiwrite(dev_t, struct uio *, int);
56 int     midipoll(dev_t, int, struct proc *);
57 int	midikqfilter(dev_t, struct knote *);
58 int	midiioctl(dev_t, u_long, caddr_t, int, struct proc *);
59 int	midiprobe(struct device *, void *, void *);
60 void	midiattach(struct device *, struct device *, void *);
61 int	mididetach(struct device *, int);
62 int	midiprint(void *, const char *);
63 
64 void	midi_iintr(void *, int);
65 void 	midi_ointr(void *);
66 void	midi_out_start(struct midi_softc *);
67 void	midi_out_stop(struct midi_softc *);
68 void	midi_out_do(struct midi_softc *);
69 void	midi_attach(struct midi_softc *, struct device *);
70 
71 
72 #if NSEQUENCER > 0
73 int		   midi_unit_count(void);
74 void		   midi_toevent(struct midi_softc *, int);
75 int		   midi_writebytes(int, u_char *, int);
76 void		   midiseq_in(struct midi_dev *, u_char *, int);
77 #endif
78 
79 struct cfattach midi_ca = {
80 	sizeof(struct midi_softc), midiprobe, midiattach, mididetach
81 };
82 
83 struct cfdriver midi_cd = {
84 	NULL, "midi", DV_DULL
85 };
86 
87 
88 void filt_midiwdetach(struct knote *);
89 int filt_midiwrite(struct knote *, long);
90 
91 struct filterops midiwrite_filtops = {
92 	1, NULL, filt_midiwdetach, filt_midiwrite
93 };
94 
95 void filt_midirdetach(struct knote *);
96 int filt_midiread(struct knote *, long);
97 
98 struct filterops midiread_filtops = {
99 	1, NULL, filt_midirdetach, filt_midiread
100 };
101 
102 
103 void
104 midi_iintr(void *addr, int data)
105 {
106 	struct midi_softc  *sc = (struct midi_softc *)addr;
107 	struct midi_buffer *mb = &sc->inbuf;
108 
109 	if (sc->isdying || !sc->isopen || !(sc->flags & FREAD)) return;
110 
111 #if NSEQUENCER > 0
112 	if (sc->seqopen) {
113 		midi_toevent(sc, data);
114 		return;
115 	}
116 #endif
117 	if (MIDIBUF_ISFULL(mb))
118 		return; /* discard data */
119 
120 	MIDIBUF_WRITE(mb, data);
121 	if (mb->used == 1) {
122 		if (sc->rchan) {
123 			sc->rchan = 0;
124 			wakeup(&sc->rchan);
125 		}
126 		selwakeup(&sc->rsel);
127 		if (sc->async)
128 			psignal(sc->async, SIGIO);
129 	}
130 }
131 
132 
133 int
134 midiread(dev_t dev, struct uio *uio, int ioflag)
135 {
136 	struct midi_softc  *sc = MIDI_DEV2SC(dev);
137 	struct midi_buffer *mb = &sc->inbuf;
138 	unsigned 	    count;
139 	int		    s, error;
140 
141 	if (!(sc->flags & FREAD))
142 		return ENXIO;
143 
144 	/* if there is no data then sleep (unless IO_NDELAY flag is set) */
145 
146 	s = splaudio();
147 	while(MIDIBUF_ISEMPTY(mb)) {
148 		if (sc->isdying) {
149 			splx(s);
150 			return EIO;
151 		}
152 		if (ioflag & IO_NDELAY) {
153 			splx(s);
154 			return EWOULDBLOCK;
155 		}
156 		sc->rchan = 1;
157 		error = tsleep(&sc->rchan, PWAIT|PCATCH, "mid_rd", 0);
158 		if (error) {
159 			splx(s);
160 			return error;
161 		}
162 	}
163 
164 	/* at this stage, there is at least 1 byte */
165 
166 	while (uio->uio_resid > 0  &&  mb->used > 0) {
167 		count = MIDIBUF_SIZE - mb->start;
168 		if (count > mb->used)
169 			count = mb->used;
170 		if (count > uio->uio_resid)
171 			count = uio->uio_resid;
172 		error = uiomove(mb->data + mb->start, count, uio);
173 		if (error) {
174 			splx(s);
175 			return error;
176 		}
177 		MIDIBUF_REMOVE(mb, count);
178 	}
179 	splx(s);
180 	return 0;
181 }
182 
183 
184 void
185 midi_ointr(void *addr)
186 {
187 	struct midi_softc  *sc = (struct midi_softc *)addr;
188 	struct midi_buffer *mb;
189 	int 		   s;
190 
191 	if (sc->isopen && !sc->isdying) {
192 #ifdef MIDI_DEBUG
193 		if (!sc->isbusy) {
194 			printf("midi_ointr: output should be busy\n");
195 		}
196 #endif
197 		mb = &sc->outbuf;
198 		s = splaudio();
199 		if (mb->used == 0)
200 			midi_out_stop(sc);
201 		else
202 			midi_out_do(sc); /* restart output */
203 		splx(s);
204 	}
205 }
206 
207 
208 void
209 midi_out_start(struct midi_softc *sc)
210 {
211 	if (!sc->isbusy) {
212 		sc->isbusy = 1;
213 		midi_out_do(sc);
214 	}
215 }
216 
217 void
218 midi_out_stop(struct midi_softc *sc)
219 {
220 	sc->isbusy = 0;
221 	if (sc->wchan) {
222 		sc->wchan = 0;
223 		wakeup(&sc->wchan);
224 	}
225 	selwakeup(&sc->wsel);
226 	if (sc->async)
227 		psignal(sc->async, SIGIO);
228 }
229 
230 
231 	/*
232 	 * drain output buffer, must be called with
233 	 * interrupts disabled
234 	 */
235 void
236 midi_out_do(struct midi_softc *sc)
237 {
238 	struct midi_buffer *mb = &sc->outbuf;
239 	unsigned 	    i, max;
240 	int		    error;
241 
242 	/*
243 	 * If output interrupts are not supported then we write MIDI_MAXWRITE
244 	 * bytes instead of 1, and then we wait sc->wait
245 	 */
246 
247 	max = sc->props & MIDI_PROP_OUT_INTR ? 1 : MIDI_MAXWRITE;
248 	for (i = max; i != 0;) {
249 		if (mb->used == 0)
250 			break;
251 		error = sc->hw_if->output(sc->hw_hdl, mb->data[mb->start]);
252 		/*
253 		 * 0 means that data is being sent, an interrupt will
254 		 * be generated when the interface becomes ready again
255 		 *
256 		 * EINPROGRESS means that data has been queued, but
257 		 * will not be sent immediately and thus will not
258 		 * generate interrupt, in this case we can send
259 		 * another byte. The flush() method can be called
260 		 * to force the transfer.
261 		 *
262 		 * EAGAIN means that data cannot be queued or sent;
263 		 * because the interface isn't ready. An interrupt
264 		 * will be generated once the interface is ready again
265 		 *
266 		 * any other (fatal) error code means that data couldn't
267 		 * be sent and was lost, interrupt will not be generated
268 		 */
269 		if (error == EINPROGRESS) {
270 			MIDIBUF_REMOVE(mb, 1);
271 			if (MIDIBUF_ISEMPTY(mb)) {
272 				if (sc->hw_if->flush != NULL)
273 					sc->hw_if->flush(sc->hw_hdl);
274 				midi_out_stop(sc);
275 				return;
276 			}
277 		} else if (error == 0) {
278 			MIDIBUF_REMOVE(mb, 1);
279 			i--;
280 		} else if (error == EAGAIN) {
281 			break;
282 		} else {
283 			MIDIBUF_INIT(mb);
284 			midi_out_stop(sc);
285 			return;
286 		}
287 	}
288 
289 	if (!(sc->props & MIDI_PROP_OUT_INTR)) {
290 		if (MIDIBUF_ISEMPTY(mb))
291 			midi_out_stop(sc);
292 		else
293 			timeout_add(&sc->timeo, sc->wait);
294 	}
295 }
296 
297 
298 int
299 midiwrite(dev_t dev, struct uio *uio, int ioflag)
300 {
301 	struct midi_softc  *sc = MIDI_DEV2SC(dev);
302 	struct midi_buffer *mb = &sc->outbuf;
303 	unsigned 	    count;
304 	int		    s, error;
305 
306 	if (!(sc->flags & FWRITE))
307 		return ENXIO;
308 	if (sc->isdying)
309 		return EIO;
310 
311 	/*
312 	 * If IO_NDELAY flag is set then check if there is enough room
313 	 * in the buffer to store at least one byte. If not then dont
314 	 * start the write process.
315 	 */
316 
317 	if ((ioflag & IO_NDELAY) &&  MIDIBUF_ISFULL(mb)  &&
318 	    (uio->uio_resid > 0))
319 	    	return EWOULDBLOCK;
320 
321 	while (uio->uio_resid > 0) {
322 		s = splaudio();
323 		while (MIDIBUF_ISFULL(mb)) {
324 			if (ioflag & IO_NDELAY) {
325 				/*
326 				 * At this stage at least one byte is already
327 				 * moved so we do not return EWOULDBLOCK
328 				 */
329 				splx(s);
330 				return 0;
331 			}
332 			sc->wchan = 1;
333 			error = tsleep(&sc->wchan, PWAIT|PCATCH, "mid_wr", 0);
334 			if (error) {
335 				splx(s);
336 				return error;
337 			}
338 			if (sc->isdying) {
339 				splx(s);
340 				return EIO;
341 			}
342 		}
343 
344 		count = MIDIBUF_SIZE - MIDIBUF_END(mb);
345 		if (count > MIDIBUF_AVAIL(mb))
346 			count = MIDIBUF_AVAIL(mb);
347 		if (count > uio->uio_resid)
348 			count = uio->uio_resid;
349 		error = uiomove(mb->data + MIDIBUF_END(mb), count, uio);
350 		if (error) {
351 			splx(s);
352 			return error;
353 		}
354 		mb->used += count;
355 		midi_out_start(sc);
356 		splx(s);
357 	}
358 	return 0;
359 }
360 
361 
362 int
363 midipoll(dev_t dev, int events, struct proc *p)
364 {
365 	struct midi_softc *sc = MIDI_DEV2SC(dev);
366 	int		   s, revents;
367 
368 	if (sc->isdying)
369 		return POLLERR;
370 
371 	revents = 0;
372 	s = splaudio();
373 	if (events & (POLLIN | POLLRDNORM)) {
374 		if (!MIDIBUF_ISEMPTY(&sc->inbuf))
375 			revents |= events & (POLLIN | POLLRDNORM);
376 	}
377 	if (events & (POLLOUT | POLLWRNORM)) {
378 		if (!MIDIBUF_ISFULL(&sc->outbuf))
379 			revents |= events & (POLLOUT | POLLWRNORM);
380 	}
381 	if (revents == 0) {
382 		if (events & (POLLIN | POLLRDNORM))
383 			selrecord(p, &sc->rsel);
384 		if (events & (POLLOUT | POLLWRNORM))
385 			selrecord(p, &sc->wsel);
386 	}
387 	splx(s);
388 	return (revents);
389 }
390 
391 
392 int
393 midikqfilter(dev_t dev, struct knote *kn)
394 {
395 	struct midi_softc *sc = MIDI_DEV2SC(dev);
396 	struct klist 	  *klist;
397 	int		   s;
398 
399 	switch (kn->kn_filter) {
400 	case EVFILT_READ:
401 		klist = &sc->rsel.si_note;
402 		kn->kn_fop = &midiread_filtops;
403 		break;
404 	case EVFILT_WRITE:
405 		klist = &sc->wsel.si_note;
406 		kn->kn_fop = &midiwrite_filtops;
407 		break;
408 	default:
409 		return (EPERM);
410 	}
411 	kn->kn_hook = (void *)sc;
412 
413 	s = splaudio();
414 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
415 	splx(s);
416 
417 	return (0);
418 }
419 
420 
421 void
422 filt_midirdetach(struct knote *kn)
423 {
424 	struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
425 	int		   s;
426 
427 	s = splaudio();
428 	SLIST_REMOVE(&sc->rsel.si_note, kn, knote, kn_selnext);
429 	splx(s);
430 }
431 
432 
433 int
434 filt_midiread(struct knote *kn, long hint)
435 {
436 	struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
437 	int		   s, retval;
438 
439 	s = splaudio();
440 	retval = !MIDIBUF_ISEMPTY(&sc->inbuf);
441 	splx(s);
442 
443 	return (retval);
444 }
445 
446 
447 void
448 filt_midiwdetach(struct knote *kn)
449 {
450 	struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
451 	int 		   s;
452 
453 	s = splaudio();
454 	SLIST_REMOVE(&sc->wsel.si_note, kn, knote, kn_selnext);
455 	splx(s);
456 }
457 
458 
459 int
460 filt_midiwrite(struct knote *kn, long hint)
461 {
462 	struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
463 	int		   s, retval;
464 
465 	s = splaudio();
466 	retval = !MIDIBUF_ISFULL(&sc->outbuf);
467 	splx(s);
468 
469 	return (retval);
470 }
471 
472 
473 int
474 midiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
475 {
476 	struct midi_softc *sc = MIDI_DEV2SC(dev);
477 
478 	if (sc->isdying) return EIO;
479 
480 	switch(cmd) {
481 	case FIONBIO:
482 		/* All handled in the upper FS layer */
483 		break;
484 	case FIOASYNC:
485 		if (*(int *)addr) {
486 			if (sc->async) return EBUSY;
487 			sc->async = p;
488 		} else
489 			sc->async = 0;
490 		break;
491 	default:
492 		return ENOTTY;
493 		break;
494 	}
495 	return 0;
496 }
497 
498 
499 int
500 midiopen(dev_t dev, int flags, int mode, struct proc *p)
501 {
502 	struct midi_softc *sc;
503 	int		   err;
504 
505 	if (MIDI_UNIT(dev) >= midi_cd.cd_ndevs)
506 		return ENXIO;
507 	sc = MIDI_DEV2SC(dev);
508 	if (sc == NULL)		/* there may be more units than devices */
509 		return ENXIO;
510 	if (sc->isdying)
511 		return EIO;
512 	if (sc->isopen)
513 		return EBUSY;
514 
515 	MIDIBUF_INIT(&sc->inbuf);
516 	MIDIBUF_INIT(&sc->outbuf);
517 	sc->isbusy = 0;
518 	sc->rchan = sc->wchan = 0;
519 	sc->async = 0;
520 	sc->flags = flags;
521 
522 	err = sc->hw_if->open(sc->hw_hdl, flags, midi_iintr, midi_ointr, sc);
523 	if (err)
524 		return err;
525 	sc->isopen = 1;
526 #if NSEQUENCER > 0
527 	sc->seq_md = 0;
528 	sc->seqopen = 0;
529 	sc->evstatus = 0xff;
530 #endif
531 	return 0;
532 }
533 
534 
535 int
536 midiclose(dev_t dev, int fflag, int devtype, struct proc *p)
537 {
538 	struct midi_softc  *sc = MIDI_DEV2SC(dev);
539 	struct midi_buffer *mb;
540 	int 		    error;
541 	int		    s;
542 
543 	mb = &sc->outbuf;
544 	if (!sc->isdying) {
545 		/* start draining output buffer */
546 		s = splaudio();
547 		if (!MIDIBUF_ISEMPTY(mb))
548 			midi_out_start(sc);
549 		while (sc->isbusy) {
550 			sc->wchan = 1;
551 			error = tsleep(&sc->wchan, PWAIT|PCATCH, "mid_dr", 0);
552 			if (error || sc->isdying)
553 				break;
554 		}
555 		splx(s);
556 	}
557 
558 	/*
559 	 * some hw_if->close() reset immediately the midi uart
560 	 * which flushes the internal buffer of the uart device,
561 	 * so we may lose some (important) data. To avoid this, we sleep 2*wait,
562 	 * which gives the time to the uart to drain its internal buffers.
563 	 *
564 	 * Note: we'd better sleep in the corresponding hw_if->close()
565 	 */
566 
567 	tsleep(&sc->wchan, PWAIT|PCATCH, "mid_cl", 2 * sc->wait);
568 	sc->hw_if->close(sc->hw_hdl);
569 	sc->isopen = 0;
570 	return 0;
571 }
572 
573 
574 int
575 midiprobe(struct device *parent, void *match, void *aux)
576 {
577 	struct audio_attach_args *sa = aux;
578 	return (sa != NULL && (sa->type == AUDIODEV_TYPE_MIDI) ? 1 : 0);
579 }
580 
581 
582 void
583 midi_attach(struct midi_softc *sc, struct device *parent)
584 {
585 	struct midi_info 	  mi;
586 
587 	sc->isdying = 0;
588 	sc->wait = (hz * MIDI_MAXWRITE) /  MIDI_RATE;
589 	if (sc->wait == 0)
590 		sc->wait = 1;
591 	sc->hw_if->getinfo(sc->hw_hdl, &mi);
592 	sc->props = mi.props;
593 	sc->isopen = 0;
594 	timeout_set(&sc->timeo, midi_ointr, sc);
595 	printf(": <%s>\n", mi.name);
596 }
597 
598 
599 void
600 midiattach(struct device *parent, struct device *self, void *aux)
601 {
602 	struct midi_softc        *sc = (struct midi_softc *)self;
603 	struct audio_attach_args *sa = (struct audio_attach_args *)aux;
604 	struct midi_hw_if        *hwif = sa->hwif;
605 	void  			 *hdl = sa->hdl;
606 
607 #ifdef DIAGNOSTIC
608 	if (hwif == 0 ||
609 	    hwif->open == 0 ||
610 	    hwif->close == 0 ||
611 	    hwif->output == 0 ||
612 	    hwif->getinfo == 0) {
613 		printf("midi: missing method\n");
614 		return;
615 	}
616 #endif
617 	sc->hw_if = hwif;
618 	sc->hw_hdl = hdl;
619 	midi_attach(sc, parent);
620 }
621 
622 
623 int
624 mididetach(struct device *self, int flags)
625 {
626 	struct midi_softc *sc = (struct midi_softc *)self;
627 	int    maj, mn;
628 
629 	sc->isdying = 1;
630 	if (sc->wchan) {
631 		sc->wchan = 0;
632 		wakeup(&sc->wchan);
633 	}
634 	if (sc->rchan) {
635 		sc->rchan = 0;
636 		wakeup(&sc->rchan);
637 	}
638 
639 	/* locate the major number */
640         for (maj = 0; maj < nchrdev; maj++) {
641                 if (cdevsw[maj].d_open == midiopen) {
642         		/* Nuke the vnodes for any open instances (calls close). */
643         		mn = self->dv_unit;
644         		vdevgone(maj, mn, mn, VCHR);
645 		}
646 	}
647 	return 0;
648 }
649 
650 
651 int
652 midiprint(void *aux, const char *pnp)
653 {
654 	if (pnp)
655 		printf("midi at %s", pnp);
656 	return (UNCONF);
657 }
658 
659 
660 void
661 midi_getinfo(dev_t dev, struct midi_info *mi)
662 {
663 	struct midi_softc *sc = MIDI_DEV2SC(dev);
664 	if (MIDI_UNIT(dev) >= midi_cd.cd_ndevs || sc == NULL || sc->isdying) {
665 		mi->name = "unconfigured";
666 		mi->props = 0;
667 		return;
668 	}
669 	sc->hw_if->getinfo(sc->hw_hdl, mi);
670 }
671 
672 
673 struct device *
674 midi_attach_mi(struct midi_hw_if *hwif, void *hdl, struct device *dev)
675 {
676 	struct audio_attach_args arg;
677 
678 	arg.type = AUDIODEV_TYPE_MIDI;
679 	arg.hwif = hwif;
680 	arg.hdl = hdl;
681 	return config_found(dev, &arg, midiprint);
682 }
683 
684 
685 int
686 midi_unit_count(void)
687 {
688 	return midi_cd.cd_ndevs;
689 }
690 
691 
692 #if NSEQUENCER > 0
693 #define MIDI_EVLEN(status) 	(midi_evlen[((status) >> 4) & 7])
694 unsigned midi_evlen[] = { 2, 2, 2, 2, 1, 1, 2 };
695 
696 void
697 midi_toevent(struct midi_softc *sc, int data)
698 {
699 	unsigned char mesg[3];
700 
701 	if (data >= 0xf8) {		/* is it a realtime message ? */
702 		switch(data) {
703 		case 0xf8:		/* midi timer tic */
704 		case 0xfa:		/* midi timer start */
705 		case 0xfb:		/* midi timer continue (after stop) */
706 		case 0xfc:		/* midi timer stop */
707 			mesg[0] = data;
708 			midiseq_in(sc->seq_md, mesg, 1);
709 			break;
710 		default:
711 			break;
712 		}
713 	} else if (data >= 0x80) {	/* is it a common or voice message ? */
714 		sc->evstatus = data;
715 		sc->evindex = 0;
716 	} else {			/* else it is a data byte */
717 		/* strip common messages and bogus data */
718 		if (sc->evstatus >= 0xf0 || sc->evstatus < 0x80)
719 			return;
720 
721 		sc->evdata[sc->evindex++] = data;
722 		if (sc->evindex == MIDI_EVLEN(sc->evstatus)) {
723 			sc->evindex = 0;
724 			mesg[0] = sc->evstatus;
725 			mesg[1] = sc->evdata[0];
726 			mesg[2] = sc->evdata[1];
727 			midiseq_in(sc->seq_md, mesg, 1 + MIDI_EVLEN(sc->evstatus));
728 		}
729 	}
730 }
731 
732 
733 int
734 midi_writebytes(int unit, unsigned char *mesg, int mesglen)
735 {
736 	struct midi_softc  *sc = midi_cd.cd_devs[unit];
737 	struct midi_buffer *mb = &sc->outbuf;
738 	unsigned 	    count;
739 	int		    s;
740 
741 	s = splaudio();
742 	if (mesglen > MIDIBUF_AVAIL(mb)) {
743 		splx(s);
744 		return EWOULDBLOCK;
745 	}
746 
747 	while (mesglen > 0) {
748 		count = MIDIBUF_SIZE - MIDIBUF_END(mb);
749 		if (count > MIDIBUF_AVAIL(mb)) count = MIDIBUF_AVAIL(mb);
750 		if (count > mesglen) count = mesglen;
751 		bcopy(mesg, mb->data + MIDIBUF_END(mb), count);
752 		mb->used += count;
753 		mesg += count;
754 		mesglen -= count;
755 		midi_out_start(sc);
756 	}
757 	splx(s);
758 	return 0;
759 }
760 
761 #endif /* NSEQUENCER > 0 */
762 #endif /* NMIDI > 0 */
763