midi.c revision 1.12 1 /* $NetBSD: midi.c,v 1.12 1999/09/09 10:24:45 augustss 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 "midi.h"
40 #include "sequencer.h"
41
42 #include <sys/param.h>
43 #include <sys/ioctl.h>
44 #include <sys/fcntl.h>
45 #include <sys/vnode.h>
46 #include <sys/select.h>
47 #include <sys/poll.h>
48 #include <sys/malloc.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/syslog.h>
52 #include <sys/kernel.h>
53 #include <sys/signalvar.h>
54 #include <sys/conf.h>
55 #include <sys/audioio.h>
56 #include <sys/midiio.h>
57 #include <sys/device.h>
58
59 #include <dev/audio_if.h>
60 #include <dev/midi_if.h>
61 #include <dev/midivar.h>
62
63 #if NMIDI > 0
64
65 #ifdef AUDIO_DEBUG
66 #define DPRINTF(x) if (mididebug) printf x
67 #define DPRINTFN(n,x) if (mididebug >= (n)) printf x
68 int mididebug = 0;
69 #else
70 #define DPRINTF(x)
71 #define DPRINTFN(n,x)
72 #endif
73
74 int midi_wait;
75
76 void midi_in __P((void *, int));
77 void midi_out __P((void *));
78 int midi_start_output __P((struct midi_softc *, int));
79 int midi_sleep_timo __P((int *, char *, int));
80 int midi_sleep __P((int *, char *));
81 void midi_wakeup __P((int *));
82 void midi_initbuf __P((struct midi_buffer *));
83 void midi_timeout __P((void *));
84
85 int midiprobe __P((struct device *, struct cfdata *, void *));
86 void midiattach __P((struct device *, struct device *, void *));
87
88 struct cfattach midi_ca = {
89 sizeof(struct midi_softc), midiprobe, midiattach
90 };
91
92 #ifdef MIDI_SAVE
93 #define MIDI_SAVE_SIZE 100000
94 int midicnt;
95 struct {
96 int cnt;
97 u_char buf[MIDI_SAVE_SIZE];
98 } midisave;
99 #define MIDI_GETSAVE _IOWR('m', 100, int)
100
101 #endif
102
103 extern struct cfdriver midi_cd;
104
105 int
106 midiprobe(parent, match, aux)
107 struct device *parent;
108 struct cfdata *match;
109 void *aux;
110 {
111 struct audio_attach_args *sa = aux;
112
113 DPRINTFN(6,("midiprobe: type=%d sa=%p hw=%p\n",
114 sa->type, sa, sa->hwif));
115 return (sa->type == AUDIODEV_TYPE_MIDI) ? 1 : 0;
116 }
117
118 void
119 midiattach(parent, self, aux)
120 struct device *parent, *self;
121 void *aux;
122 {
123 struct midi_softc *sc = (void *)self;
124 struct audio_attach_args *sa = aux;
125 struct midi_hw_if *hwp = sa->hwif;
126 void *hdlp = sa->hdl;
127
128 DPRINTFN(6, ("MIDI attach\n"));
129
130 #ifdef DIAGNOSTIC
131 if (hwp == 0 ||
132 hwp->open == 0 ||
133 hwp->close == 0 ||
134 hwp->output == 0 ||
135 hwp->getinfo == 0) {
136 printf("midi: missing method\n");
137 return;
138 }
139 #endif
140 sc->hw_if = hwp;
141 sc->hw_hdl = hdlp;
142 midi_attach(sc, parent);
143 }
144
145 void
146 midi_attach(sc, parent)
147 struct midi_softc *sc;
148 struct device *parent;
149 {
150 struct midi_info mi;
151
152 sc->isopen = 0;
153
154 midi_wait = MIDI_WAIT * hz / 1000000;
155 if (midi_wait == 0)
156 midi_wait = 1;
157
158 sc->sc_dev = parent;
159 sc->hw_if->getinfo(sc->hw_hdl, &mi);
160 sc->props = mi.props;
161 printf(": <%s>\n", mi.name);
162 }
163
164 int
165 midi_unit_count()
166 {
167 return midi_cd.cd_ndevs;
168 }
169
170 void
171 midi_initbuf(mb)
172 struct midi_buffer *mb;
173 {
174 mb->used = 0;
175 mb->usedhigh = MIDI_BUFSIZE;
176 mb->end = mb->start + mb->usedhigh;
177 mb->inp = mb->outp = mb->start;
178 }
179
180 int
181 midi_sleep_timo(chan, label, timo)
182 int *chan;
183 char *label;
184 int timo;
185 {
186 int st;
187
188 if (!label)
189 label = "midi";
190
191 DPRINTFN(5, ("midi_sleep_timo: %p %s %d\n", chan, label, timo));
192 *chan = 1;
193 st = tsleep(chan, PWAIT | PCATCH, label, timo);
194 *chan = 0;
195 #ifdef MIDI_DEBUG
196 if (st != 0)
197 printf("midi_sleep: %d\n", st);
198 #endif
199 return st;
200 }
201
202 int
203 midi_sleep(chan, label)
204 int *chan;
205 char *label;
206 {
207 return midi_sleep_timo(chan, label, 0);
208 }
209
210 void
211 midi_wakeup(chan)
212 int *chan;
213 {
214 if (*chan) {
215 DPRINTFN(5, ("midi_wakeup: %p\n", chan));
216 wakeup(chan);
217 *chan = 0;
218 }
219 }
220
221 static int midi_lengths[] = { 2,2,2,2,1,1,2,0 };
222 /* Number of bytes in a MIDI command */
223 #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
224
225 void
226 midi_in(addr, data)
227 void *addr;
228 int data;
229 {
230 struct midi_softc *sc = addr;
231 struct midi_buffer *mb = &sc->inbuf;
232 int i;
233
234 if (!sc->isopen)
235 return;
236 if (data == MIDI_ACK)
237 return;
238 DPRINTFN(3, ("midi_in: %p 0x%02x\n", sc, data));
239 if (!(sc->flags & FREAD))
240 return; /* discard data if not reading */
241
242 switch(sc->in_state) {
243 case MIDI_IN_START:
244 if (MIDI_IS_STATUS(data)) {
245 switch(data) {
246 case 0xf0: /* Sysex */
247 sc->in_state = MIDI_IN_SYSEX;
248 break;
249 case 0xf1: /* MTC quarter frame */
250 case 0xf3: /* Song select */
251 sc->in_state = MIDI_IN_DATA;
252 sc->in_msg[0] = data;
253 sc->in_pos = 1;
254 sc->in_left = 1;
255 break;
256 case 0xf2: /* Song position pointer */
257 sc->in_state = MIDI_IN_DATA;
258 sc->in_msg[0] = data;
259 sc->in_pos = 1;
260 sc->in_left = 2;
261 break;
262 default:
263 if (MIDI_IS_COMMON(data)) {
264 sc->in_msg[0] = data;
265 sc->in_pos = 1;
266 goto deliver;
267 } else {
268 sc->in_state = MIDI_IN_DATA;
269 sc->in_msg[0] = sc->in_status = data;
270 sc->in_pos = 1;
271 sc->in_left =
272 MIDI_LENGTH(sc->in_status);
273 }
274 break;
275 }
276 } else {
277 if (MIDI_IS_STATUS(sc->in_status)) {
278 sc->in_state = MIDI_IN_DATA;
279 sc->in_msg[0] = sc->in_status;
280 sc->in_msg[1] = data;
281 sc->in_pos = 2;
282 sc->in_left = MIDI_LENGTH(sc->in_status) - 1;
283 }
284 }
285 return;
286 case MIDI_IN_DATA:
287 sc->in_msg[sc->in_pos++] = data;
288 if (--sc->in_left <= 0)
289 break; /* deliver data */
290 return;
291 case MIDI_IN_SYSEX:
292 if (data == MIDI_SYSEX_END)
293 sc->in_state = MIDI_IN_START;
294 return;
295 }
296 deliver:
297 sc->in_state = MIDI_IN_START;
298 #if NSEQUENCER > 0
299 if (sc->seqopen) {
300 extern void midiseq_in __P((struct midi_dev *,u_char *,int));
301 midiseq_in(sc->seq_md, sc->in_msg, sc->in_pos);
302 return;
303 }
304 #endif
305
306 if (mb->used + sc->in_pos > mb->usedhigh) {
307 DPRINTF(("midi_in: buffer full, discard data=0x%02x\n",
308 sc->in_msg[0]));
309 return;
310 }
311 for (i = 0; i < sc->in_pos; i++) {
312 *mb->inp++ = sc->in_msg[i];
313 if (mb->inp >= mb->end)
314 mb->inp = mb->start;
315 mb->used++;
316 }
317 midi_wakeup(&sc->rchan);
318 selwakeup(&sc->rsel);
319 if (sc->async)
320 psignal(sc->async, SIGIO);
321 }
322
323 void
324 midi_out(addr)
325 void *addr;
326 {
327 struct midi_softc *sc = addr;
328
329 if (!sc->isopen)
330 return;
331 DPRINTFN(3, ("midi_out: %p\n", sc));
332 midi_start_output(sc, 1);
333 }
334
335 int
336 midiopen(dev, flags, ifmt, p)
337 dev_t dev;
338 int flags, ifmt;
339 struct proc *p;
340 {
341 int unit = MIDIUNIT(dev);
342 struct midi_softc *sc;
343 struct midi_hw_if *hw;
344 int error;
345
346 if (unit >= midi_cd.cd_ndevs ||
347 (sc = midi_cd.cd_devs[unit]) == NULL)
348 return ENXIO;
349 DPRINTF(("midiopen %p\n", sc));
350
351 hw = sc->hw_if;
352 if (!hw)
353 return ENXIO;
354 if (sc->isopen)
355 return EBUSY;
356 sc->in_state = MIDI_IN_START;
357 sc->in_status = 0;
358 error = hw->open(sc->hw_hdl, flags, midi_in, midi_out, sc);
359 if (error)
360 return error;
361 sc->isopen++;
362 midi_initbuf(&sc->outbuf);
363 midi_initbuf(&sc->inbuf);
364 sc->flags = flags;
365 sc->rchan = 0;
366 sc->wchan = 0;
367 sc->pbus = 0;
368 sc->async = 0;
369
370 #ifdef MIDI_SAVE
371 if (midicnt != 0) {
372 midisave.cnt = midicnt;
373 midicnt = 0;
374 }
375 #endif
376
377 return 0;
378 }
379
380 int
381 midiclose(dev, flags, ifmt, p)
382 dev_t dev;
383 int flags, ifmt;
384 struct proc *p;
385 {
386 int unit = MIDIUNIT(dev);
387 struct midi_softc *sc = midi_cd.cd_devs[unit];
388 struct midi_hw_if *hw = sc->hw_if;
389 int s, error;
390
391 DPRINTF(("midiclose %p\n", sc));
392
393 midi_start_output(sc, 0);
394 error = 0;
395 s = splaudio();
396 while (sc->outbuf.used > 0 && !error) {
397 DPRINTFN(2,("midiclose sleep used=%d\n", sc->outbuf.used));
398 error = midi_sleep_timo(&sc->wchan, "mid_dr", 30*hz);
399 }
400 splx(s);
401 sc->isopen = 0;
402 hw->close(sc->hw_hdl);
403 #if NSEQUENCER > 0
404 sc->seqopen = 0;
405 sc->seq_md = 0;
406 #endif
407 return 0;
408 }
409
410 int
411 midiread(dev, uio, ioflag)
412 dev_t dev;
413 struct uio *uio;
414 int ioflag;
415 {
416 int unit = MIDIUNIT(dev);
417 struct midi_softc *sc = midi_cd.cd_devs[unit];
418 struct midi_buffer *mb = &sc->inbuf;
419 int error;
420 u_char *outp;
421 int used, cc, n, resid;
422 int s;
423
424 DPRINTF(("midiread: %p, count=%lu\n", sc,
425 (unsigned long)uio->uio_resid));
426
427 error = 0;
428 resid = uio->uio_resid;
429 while (uio->uio_resid == resid && !error) {
430 s = splaudio();
431 while (mb->used <= 0) {
432 if (ioflag & IO_NDELAY) {
433 splx(s);
434 return EWOULDBLOCK;
435 }
436 error = midi_sleep(&sc->rchan, "mid rd");
437 if (error) {
438 splx(s);
439 return error;
440 }
441 }
442 used = mb->used;
443 outp = mb->outp;
444 splx(s);
445 cc = used; /* maximum to read */
446 n = mb->end - outp;
447 if (n < cc)
448 cc = n; /* don't read beyond end of buffer */
449 if (uio->uio_resid < cc)
450 cc = uio->uio_resid; /* and no more than we want */
451 DPRINTFN(3, ("midiread: uiomove cc=%d\n", cc));
452 error = uiomove(outp, cc, uio);
453 if (error)
454 break;
455 used -= cc;
456 outp += cc;
457 if (outp >= mb->end)
458 outp = mb->start;
459 s = splaudio();
460 mb->outp = outp;
461 mb->used = used;
462 splx(s);
463 }
464 return error;
465 }
466
467 void
468 midi_timeout(arg)
469 void *arg;
470 {
471 struct midi_softc *sc = arg;
472
473 DPRINTFN(3,("midi_timeout: %p\n", sc));
474 midi_start_output(sc, 1);
475 }
476
477 int
478 midi_start_output(sc, intr)
479 struct midi_softc *sc;
480 int intr;
481 {
482 struct midi_buffer *mb = &sc->outbuf;
483 u_char *outp;
484 int error;
485 int s;
486 int i, mmax;
487
488 error = 0;
489 mmax = sc->props & MIDI_PROP_OUT_INTR ? 1 : MIDI_MAX_WRITE;
490 s = splaudio();
491 if (sc->pbus && !intr) {
492 DPRINTFN(4, ("midi_start_output: busy\n"));
493 splx(s);
494 return 0;
495 }
496 sc->pbus = 1;
497 for (i = 0; i < mmax && mb->used > 0 && !error; i++) {
498 outp = mb->outp;
499 splx(s);
500 DPRINTFN(4, ("midi_start_output: %p i=%d, data=0x%02x\n",
501 sc, i, *outp));
502 #ifdef MIDI_SAVE
503 midisave.buf[midicnt] = *outp;
504 midicnt = (midicnt + 1) % MIDI_SAVE_SIZE;
505 #endif
506 error = sc->hw_if->output(sc->hw_hdl, *outp++);
507 if (outp >= mb->end)
508 outp = mb->start;
509 s = splaudio();
510 mb->outp = outp;
511 mb->used--;
512 }
513 midi_wakeup(&sc->wchan);
514 selwakeup(&sc->wsel);
515 if (sc->async)
516 psignal(sc->async, SIGIO);
517 if (mb->used > 0) {
518 if (!(sc->props & MIDI_PROP_OUT_INTR))
519 timeout(midi_timeout, sc, midi_wait);
520 } else
521 sc->pbus = 0;
522 splx(s);
523 return error;
524 }
525
526 int
527 midiwrite(dev, uio, ioflag)
528 dev_t dev;
529 struct uio *uio;
530 int ioflag;
531 {
532 int unit = MIDIUNIT(dev);
533 struct midi_softc *sc = midi_cd.cd_devs[unit];
534 struct midi_buffer *mb = &sc->outbuf;
535 int error;
536 u_char *inp;
537 int used, cc, n;
538 int s;
539
540 DPRINTFN(2, ("midiwrite: %p, unit=%d, count=%lu\n", sc, unit,
541 (unsigned long)uio->uio_resid));
542
543 error = 0;
544 while (uio->uio_resid > 0 && !error) {
545 s = splaudio();
546 if (mb->used >= mb->usedhigh) {
547 DPRINTFN(3,("midi_write: sleep used=%d hiwat=%d\n",
548 mb->used, mb->usedhigh));
549 if (ioflag & IO_NDELAY) {
550 splx(s);
551 return EWOULDBLOCK;
552 }
553 error = midi_sleep(&sc->wchan, "mid wr");
554 if (error) {
555 splx(s);
556 return error;
557 }
558 }
559 used = mb->used;
560 inp = mb->inp;
561 splx(s);
562 cc = mb->usedhigh - used; /* maximum to write */
563 n = mb->end - inp;
564 if (n < cc)
565 cc = n; /* don't write beyond end of buffer */
566 if (uio->uio_resid < cc)
567 cc = uio->uio_resid; /* and no more than we have */
568 error = uiomove(inp, cc, uio);
569 #ifdef MIDI_DEBUG
570 if (error)
571 printf("midi_write:(1) uiomove failed %d; "
572 "cc=%d inp=%p\n",
573 error, cc, inp);
574 #endif
575 if (error)
576 break;
577 inp = mb->inp + cc;
578 if (inp >= mb->end)
579 inp = mb->start;
580 s = splaudio();
581 mb->inp = inp;
582 mb->used += cc;
583 splx(s);
584 error = midi_start_output(sc, 0);
585 }
586 return error;
587 }
588
589 /*
590 * This write routine is only called from sequencer code and expects
591 * a write that is smaller than the MIDI buffer.
592 */
593 int
594 midi_writebytes(unit, buf, cc)
595 int unit;
596 u_char *buf;
597 int cc;
598 {
599 struct midi_softc *sc = midi_cd.cd_devs[unit];
600 struct midi_buffer *mb = &sc->outbuf;
601 int n, s;
602
603 DPRINTFN(2, ("midi_writebytes: %p, unit=%d, cc=%d\n", sc, unit, cc));
604 DPRINTFN(3, ("midi_writebytes: %x %x %x\n",buf[0],buf[1],buf[2]));
605
606 s = splaudio();
607 if (mb->used + cc >= mb->usedhigh) {
608 splx(s);
609 return (EWOULDBLOCK);
610 }
611 n = mb->end - mb->inp;
612 if (cc < n)
613 n = cc;
614 mb->used += cc;
615 memcpy(mb->inp, buf, n);
616 mb->inp += n;
617 if (mb->inp >= mb->end) {
618 mb->inp = mb->start;
619 cc -= n;
620 if (cc > 0) {
621 memcpy(mb->inp, buf + n, cc);
622 mb->inp += cc;
623 }
624 }
625 splx(s);
626 return (midi_start_output(sc, 0));
627 }
628
629 int
630 midiioctl(dev, cmd, addr, flag, p)
631 dev_t dev;
632 u_long cmd;
633 caddr_t addr;
634 int flag;
635 struct proc *p;
636 {
637 int unit = MIDIUNIT(dev);
638 struct midi_softc *sc = midi_cd.cd_devs[unit];
639 struct midi_hw_if *hw = sc->hw_if;
640 int error;
641
642 DPRINTF(("midiioctl: %p cmd=0x%08lx\n", sc, cmd));
643 error = 0;
644 switch (cmd) {
645 case FIONBIO:
646 /* All handled in the upper FS layer. */
647 break;
648
649 case FIOASYNC:
650 if (*(int *)addr) {
651 if (sc->async)
652 return EBUSY;
653 sc->async = p;
654 DPRINTF(("midi_ioctl: FIOASYNC %p\n", p));
655 } else
656 sc->async = 0;
657 break;
658
659 #if 0
660 case MIDI_PRETIME:
661 /* XXX OSS
662 * This should set up a read timeout, but that's
663 * why we have poll(), so there's nothing yet. */
664 error = EINVAL;
665 break;
666 #endif
667
668 #ifdef MIDI_SAVE
669 case MIDI_GETSAVE:
670 error = copyout(&midisave, *(void **)addr, sizeof midisave);
671 break;
672 #endif
673
674 default:
675 if (hw->ioctl)
676 error = hw->ioctl(sc->hw_hdl, cmd, addr, flag, p);
677 else
678 error = EINVAL;
679 break;
680 }
681 return error;
682 }
683
684 int
685 midipoll(dev, events, p)
686 dev_t dev;
687 int events;
688 struct proc *p;
689 {
690 int unit = MIDIUNIT(dev);
691 struct midi_softc *sc = midi_cd.cd_devs[unit];
692 int revents = 0;
693 int s = splaudio();
694
695 DPRINTF(("midipoll: %p events=0x%x\n", sc, events));
696
697 if (events & (POLLIN | POLLRDNORM))
698 if (sc->inbuf.used > 0)
699 revents |= events & (POLLIN | POLLRDNORM);
700
701 if (events & (POLLOUT | POLLWRNORM))
702 if (sc->outbuf.used < sc->outbuf.usedhigh)
703 revents |= events & (POLLOUT | POLLWRNORM);
704
705 if (revents == 0) {
706 if (events & (POLLIN | POLLRDNORM))
707 selrecord(p, &sc->rsel);
708
709 if (events & (POLLOUT | POLLWRNORM))
710 selrecord(p, &sc->wsel);
711 }
712
713 splx(s);
714 return revents;
715 }
716
717 void
718 midi_getinfo(dev, mi)
719 dev_t dev;
720 struct midi_info *mi;
721 {
722 int unit = MIDIUNIT(dev);
723 struct midi_softc *sc;
724
725 if (unit >= midi_cd.cd_ndevs ||
726 (sc = midi_cd.cd_devs[unit]) == NULL)
727 return;
728 sc->hw_if->getinfo(sc->hw_hdl, mi);
729 }
730
731 #endif /* NMIDI > 0 */
732
733 #if NMIDI > 0 || NMIDIBUS > 0
734
735 int audioprint __P((void *, const char *));
736
737 struct device *
738 midi_attach_mi(mhwp, hdlp, dev)
739 struct midi_hw_if *mhwp;
740 void *hdlp;
741 struct device *dev;
742 {
743 struct audio_attach_args arg;
744
745 #ifdef DIAGNOSTIC
746 if (mhwp == NULL) {
747 printf("midi_attach_mi: NULL\n");
748 return (0);
749 }
750 #endif
751 arg.type = AUDIODEV_TYPE_MIDI;
752 arg.hwif = mhwp;
753 arg.hdl = hdlp;
754 return (config_found(dev, &arg, audioprint));
755 }
756
757 #endif /* NMIDI > 0 || NMIDIBUS > 0 */
758