midi.c revision 1.43.2.6 1 1.43.2.2 chap /* $NetBSD: midi.c,v 1.43.2.6 2006/05/20 03:16:48 chap Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.8 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.43.2.6 chap * by Lennart Augustsson (augustss (at) NetBSD.org) and (MIDI DFA and Active
9 1.43.2.6 chap * Sense handling) Chapman Flack (nblists (at) anastigmatix.net).
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss * 3. All advertising materials mentioning features or use of this software
20 1.1 augustss * must display the following acknowledgement:
21 1.1 augustss * This product includes software developed by the NetBSD
22 1.1 augustss * Foundation, Inc. and its contributors.
23 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 augustss * contributors may be used to endorse or promote products derived
25 1.1 augustss * from this software without specific prior written permission.
26 1.1 augustss *
27 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
38 1.1 augustss */
39 1.23 lukem
40 1.23 lukem #include <sys/cdefs.h>
41 1.43.2.2 chap __KERNEL_RCSID(0, "$NetBSD: midi.c,v 1.43.2.6 2006/05/20 03:16:48 chap Exp $");
42 1.1 augustss
43 1.1 augustss #include "midi.h"
44 1.1 augustss #include "sequencer.h"
45 1.1 augustss
46 1.1 augustss #include <sys/param.h>
47 1.1 augustss #include <sys/ioctl.h>
48 1.1 augustss #include <sys/fcntl.h>
49 1.1 augustss #include <sys/vnode.h>
50 1.1 augustss #include <sys/select.h>
51 1.1 augustss #include <sys/poll.h>
52 1.1 augustss #include <sys/malloc.h>
53 1.1 augustss #include <sys/proc.h>
54 1.1 augustss #include <sys/systm.h>
55 1.15 thorpej #include <sys/callout.h>
56 1.1 augustss #include <sys/syslog.h>
57 1.1 augustss #include <sys/kernel.h>
58 1.1 augustss #include <sys/signalvar.h>
59 1.1 augustss #include <sys/conf.h>
60 1.1 augustss #include <sys/audioio.h>
61 1.1 augustss #include <sys/midiio.h>
62 1.1 augustss
63 1.1 augustss #include <dev/audio_if.h>
64 1.4 augustss #include <dev/midi_if.h>
65 1.1 augustss #include <dev/midivar.h>
66 1.1 augustss
67 1.10 drochner #if NMIDI > 0
68 1.10 drochner
69 1.43.2.5 chap
70 1.1 augustss #ifdef AUDIO_DEBUG
71 1.1 augustss #define DPRINTF(x) if (mididebug) printf x
72 1.1 augustss #define DPRINTFN(n,x) if (mididebug >= (n)) printf x
73 1.1 augustss int mididebug = 0;
74 1.43.2.2 chap /*
75 1.43.2.2 chap * 1: detected protocol errors and buffer overflows
76 1.43.2.2 chap * 2: probe, attach, detach
77 1.43.2.2 chap * 3: open, close
78 1.43.2.2 chap * 4: data received except realtime
79 1.43.2.2 chap * 5: ioctl
80 1.43.2.2 chap * 6: read, write, poll
81 1.43.2.2 chap * 7: data transmitted
82 1.43.2.2 chap * 8: uiomoves, synchronization
83 1.43.2.2 chap * 9: realtime data received
84 1.43.2.2 chap */
85 1.1 augustss #else
86 1.1 augustss #define DPRINTF(x)
87 1.1 augustss #define DPRINTFN(n,x)
88 1.1 augustss #endif
89 1.1 augustss
90 1.1 augustss int midi_wait;
91 1.1 augustss
92 1.22 augustss void midi_in(void *, int);
93 1.22 augustss void midi_out(void *);
94 1.43.2.5 chap int midi_start_output(struct midi_softc *, int, int);
95 1.43.2.1 chap int midi_sleep_timo(int *, char *, int);
96 1.43.2.1 chap int midi_sleep(int *, char *);
97 1.22 augustss void midi_wakeup(int *);
98 1.22 augustss void midi_initbuf(struct midi_buffer *);
99 1.22 augustss void midi_timeout(void *);
100 1.22 augustss
101 1.22 augustss int midiprobe(struct device *, struct cfdata *, void *);
102 1.22 augustss void midiattach(struct device *, struct device *, void *);
103 1.22 augustss int mididetach(struct device *, int);
104 1.22 augustss int midiactivate(struct device *, enum devact);
105 1.1 augustss
106 1.25 gehenna dev_type_open(midiopen);
107 1.25 gehenna dev_type_close(midiclose);
108 1.25 gehenna dev_type_read(midiread);
109 1.25 gehenna dev_type_write(midiwrite);
110 1.25 gehenna dev_type_ioctl(midiioctl);
111 1.25 gehenna dev_type_poll(midipoll);
112 1.30 jdolecek dev_type_kqfilter(midikqfilter);
113 1.25 gehenna
114 1.25 gehenna const struct cdevsw midi_cdevsw = {
115 1.25 gehenna midiopen, midiclose, midiread, midiwrite, midiioctl,
116 1.30 jdolecek nostop, notty, midipoll, nommap, midikqfilter,
117 1.25 gehenna };
118 1.25 gehenna
119 1.28 thorpej CFATTACH_DECL(midi, sizeof(struct midi_softc),
120 1.29 thorpej midiprobe, midiattach, mididetach, midiactivate);
121 1.1 augustss
122 1.4 augustss #ifdef MIDI_SAVE
123 1.4 augustss #define MIDI_SAVE_SIZE 100000
124 1.4 augustss int midicnt;
125 1.4 augustss struct {
126 1.4 augustss int cnt;
127 1.4 augustss u_char buf[MIDI_SAVE_SIZE];
128 1.4 augustss } midisave;
129 1.4 augustss #define MIDI_GETSAVE _IOWR('m', 100, int)
130 1.4 augustss
131 1.4 augustss #endif
132 1.4 augustss
133 1.1 augustss extern struct cfdriver midi_cd;
134 1.1 augustss
135 1.1 augustss int
136 1.22 augustss midiprobe(struct device *parent, struct cfdata *match, void *aux)
137 1.1 augustss {
138 1.1 augustss struct audio_attach_args *sa = aux;
139 1.1 augustss
140 1.43.2.2 chap DPRINTFN(2,("midiprobe: type=%d sa=%p hw=%p\n",
141 1.1 augustss sa->type, sa, sa->hwif));
142 1.16 augustss return (sa->type == AUDIODEV_TYPE_MIDI);
143 1.1 augustss }
144 1.1 augustss
145 1.1 augustss void
146 1.22 augustss midiattach(struct device *parent, struct device *self, void *aux)
147 1.1 augustss {
148 1.1 augustss struct midi_softc *sc = (void *)self;
149 1.1 augustss struct audio_attach_args *sa = aux;
150 1.43.2.1 chap struct midi_hw_if *hwp = sa->hwif;
151 1.1 augustss void *hdlp = sa->hdl;
152 1.1 augustss
153 1.43.2.2 chap DPRINTFN(2, ("MIDI attach\n"));
154 1.1 augustss
155 1.1 augustss #ifdef DIAGNOSTIC
156 1.1 augustss if (hwp == 0 ||
157 1.1 augustss hwp->open == 0 ||
158 1.1 augustss hwp->close == 0 ||
159 1.1 augustss hwp->output == 0 ||
160 1.1 augustss hwp->getinfo == 0) {
161 1.1 augustss printf("midi: missing method\n");
162 1.1 augustss return;
163 1.1 augustss }
164 1.1 augustss #endif
165 1.15 thorpej
166 1.1 augustss sc->hw_if = hwp;
167 1.1 augustss sc->hw_hdl = hdlp;
168 1.2 augustss midi_attach(sc, parent);
169 1.2 augustss }
170 1.2 augustss
171 1.18 tshiozak int
172 1.22 augustss midiactivate(struct device *self, enum devact act)
173 1.18 tshiozak {
174 1.18 tshiozak struct midi_softc *sc = (struct midi_softc *)self;
175 1.18 tshiozak
176 1.18 tshiozak switch (act) {
177 1.18 tshiozak case DVACT_ACTIVATE:
178 1.18 tshiozak return (EOPNOTSUPP);
179 1.18 tshiozak
180 1.18 tshiozak case DVACT_DEACTIVATE:
181 1.18 tshiozak sc->dying = 1;
182 1.18 tshiozak break;
183 1.18 tshiozak }
184 1.18 tshiozak return (0);
185 1.18 tshiozak }
186 1.18 tshiozak
187 1.18 tshiozak int
188 1.22 augustss mididetach(struct device *self, int flags)
189 1.18 tshiozak {
190 1.18 tshiozak struct midi_softc *sc = (struct midi_softc *)self;
191 1.18 tshiozak int maj, mn;
192 1.18 tshiozak
193 1.43.2.2 chap DPRINTFN(2,("midi_detach: sc=%p flags=%d\n", sc, flags));
194 1.18 tshiozak
195 1.18 tshiozak sc->dying = 1;
196 1.18 tshiozak
197 1.18 tshiozak wakeup(&sc->wchan);
198 1.18 tshiozak wakeup(&sc->rchan);
199 1.18 tshiozak
200 1.18 tshiozak /* locate the major number */
201 1.25 gehenna maj = cdevsw_lookup_major(&midi_cdevsw);
202 1.18 tshiozak
203 1.18 tshiozak /* Nuke the vnodes for any open instances (calls close). */
204 1.43.2.1 chap mn = self->dv_unit;
205 1.18 tshiozak vdevgone(maj, mn, mn, VCHR);
206 1.43.2.3 chap
207 1.43.2.6 chap evcnt_detach(&sc->xmt.bytesDiscarded);
208 1.43.2.6 chap evcnt_detach(&sc->xmt.incompleteMessages);
209 1.43.2.3 chap if ( sc->props & MIDI_PROP_CAN_INPUT ) {
210 1.43.2.6 chap evcnt_detach(&sc->rcv.bytesDiscarded);
211 1.43.2.6 chap evcnt_detach(&sc->rcv.incompleteMessages);
212 1.43.2.3 chap }
213 1.18 tshiozak
214 1.18 tshiozak return (0);
215 1.18 tshiozak }
216 1.18 tshiozak
217 1.2 augustss void
218 1.22 augustss midi_attach(struct midi_softc *sc, struct device *parent)
219 1.2 augustss {
220 1.2 augustss struct midi_info mi;
221 1.2 augustss
222 1.43.2.5 chap
223 1.43.2.5 chap callout_init(&sc->sc_callout);
224 1.43.2.5 chap callout_setfunc(&sc->sc_callout, midi_timeout, sc);
225 1.43.2.5 chap simple_lock_init(&sc->out_lock);
226 1.43.2.5 chap sc->dying = 0;
227 1.1 augustss sc->isopen = 0;
228 1.1 augustss
229 1.1 augustss midi_wait = MIDI_WAIT * hz / 1000000;
230 1.1 augustss if (midi_wait == 0)
231 1.1 augustss midi_wait = 1;
232 1.1 augustss
233 1.2 augustss sc->sc_dev = parent;
234 1.2 augustss sc->hw_if->getinfo(sc->hw_hdl, &mi);
235 1.1 augustss sc->props = mi.props;
236 1.43.2.3 chap
237 1.43.2.6 chap evcnt_attach_dynamic(&sc->xmt.bytesDiscarded,
238 1.43.2.6 chap EVCNT_TYPE_MISC, NULL,
239 1.43.2.6 chap sc->dev.dv_xname, "xmt bytes discarded");
240 1.43.2.6 chap evcnt_attach_dynamic(&sc->xmt.incompleteMessages,
241 1.43.2.6 chap EVCNT_TYPE_MISC, NULL,
242 1.43.2.6 chap sc->dev.dv_xname, "xmt incomplete msgs");
243 1.43.2.3 chap if ( sc->props & MIDI_PROP_CAN_INPUT ) {
244 1.43.2.6 chap evcnt_attach_dynamic(&sc->rcv.bytesDiscarded,
245 1.43.2.3 chap EVCNT_TYPE_MISC, NULL,
246 1.43.2.3 chap sc->dev.dv_xname, "rcv bytes discarded");
247 1.43.2.6 chap evcnt_attach_dynamic(&sc->rcv.incompleteMessages,
248 1.43.2.3 chap EVCNT_TYPE_MISC, NULL,
249 1.43.2.3 chap sc->dev.dv_xname, "rcv incomplete msgs");
250 1.43.2.3 chap }
251 1.43.2.3 chap
252 1.13 soren printf(": %s\n", mi.name);
253 1.1 augustss }
254 1.1 augustss
255 1.1 augustss int
256 1.22 augustss midi_unit_count(void)
257 1.1 augustss {
258 1.43.2.3 chap int i;
259 1.43.2.3 chap for ( i = 0; i < midi_cd.cd_ndevs; ++i )
260 1.43.2.3 chap if ( NULL == midi_cd.cd_devs[i] )
261 1.43.2.3 chap break;
262 1.43.2.3 chap return i;
263 1.1 augustss }
264 1.1 augustss
265 1.1 augustss void
266 1.22 augustss midi_initbuf(struct midi_buffer *mb)
267 1.1 augustss {
268 1.1 augustss mb->used = 0;
269 1.1 augustss mb->usedhigh = MIDI_BUFSIZE;
270 1.1 augustss mb->end = mb->start + mb->usedhigh;
271 1.1 augustss mb->inp = mb->outp = mb->start;
272 1.1 augustss }
273 1.1 augustss
274 1.2 augustss int
275 1.43.2.1 chap midi_sleep_timo(int *chan, char *label, int timo)
276 1.1 augustss {
277 1.1 augustss int st;
278 1.1 augustss
279 1.1 augustss if (!label)
280 1.1 augustss label = "midi";
281 1.1 augustss
282 1.43.2.2 chap DPRINTFN(8, ("midi_sleep_timo: %p %s %d\n", chan, label, timo));
283 1.1 augustss *chan = 1;
284 1.1 augustss st = tsleep(chan, PWAIT | PCATCH, label, timo);
285 1.1 augustss *chan = 0;
286 1.1 augustss #ifdef MIDI_DEBUG
287 1.1 augustss if (st != 0)
288 1.1 augustss printf("midi_sleep: %d\n", st);
289 1.1 augustss #endif
290 1.1 augustss return st;
291 1.1 augustss }
292 1.1 augustss
293 1.2 augustss int
294 1.43.2.1 chap midi_sleep(int *chan, char *label)
295 1.1 augustss {
296 1.1 augustss return midi_sleep_timo(chan, label, 0);
297 1.1 augustss }
298 1.1 augustss
299 1.2 augustss void
300 1.22 augustss midi_wakeup(int *chan)
301 1.1 augustss {
302 1.1 augustss if (*chan) {
303 1.43.2.2 chap DPRINTFN(8, ("midi_wakeup: %p\n", chan));
304 1.1 augustss wakeup(chan);
305 1.1 augustss *chan = 0;
306 1.1 augustss }
307 1.1 augustss }
308 1.1 augustss
309 1.43.2.2 chap /* in midivar.h:
310 1.43.2.2 chap #define MIDI_CAT_DATA 0
311 1.43.2.2 chap #define MIDI_CAT_STATUS1 1
312 1.43.2.2 chap #define MIDI_CAT_STATUS2 2
313 1.43.2.2 chap #define MIDI_CAT_COMMON 3
314 1.43.2.2 chap */
315 1.43.2.2 chap static char const midi_cats[] = "\0\0\0\0\0\0\0\0\2\2\2\2\1\1\2\3";
316 1.43.2.2 chap #define MIDI_CAT(d) (midi_cats[((d)>>4)&15])
317 1.43.2.6 chap #define DFA_RETURN(offp,endp,ret) \
318 1.43.2.6 chap return (s->pos=s->msg+(offp)), (s->end=s->msg+(endp)), (ret)
319 1.1 augustss
320 1.43.2.6 chap enum dfa_ret { DFA_MORE, DFA_CHN, DFA_COM, DFA_SYX, DFA_RT, DFA_ERR, DFA_HUH };
321 1.1 augustss
322 1.43.2.6 chap /*
323 1.43.2.6 chap * A MIDI state machine suitable for receiving or transmitting. It will
324 1.43.2.6 chap * accept correct MIDI input that uses, doesn't use, or sometimes uses the
325 1.43.2.6 chap * 'running status' compression technique, and transduce it to fully expanded
326 1.43.2.6 chap * (compress==0) or fully compressed (compress==1) form.
327 1.43.2.6 chap *
328 1.43.2.6 chap * Returns DFA_MORE if a complete message has not been parsed yet (SysEx
329 1.43.2.6 chap * messages are the exception), DFA_ERR or DFA_HUH if the input does not
330 1.43.2.6 chap * conform to the protocol, or DFA_CHN (channel messages), DFA_COM (System
331 1.43.2.6 chap * Common messages), DFA_RT (System Real-Time messages), or DFA_SYX (System
332 1.43.2.6 chap * Exclusive) to broadly categorize the message parsed. s->pos and s->end
333 1.43.2.6 chap * locate the parsed message; while (s->pos<s->end) putchar(*(s->pos++));
334 1.43.2.6 chap * would output it.
335 1.43.2.6 chap *
336 1.43.2.6 chap * DFA_HUH means the character c wasn't valid in the original state, but the
337 1.43.2.6 chap * state has now been reset to START and the caller should try again passing
338 1.43.2.6 chap * the same c. DFA_ERR means c isn't valid in the start state; the caller
339 1.43.2.6 chap * should kiss it goodbye and continue to try successive characters from the
340 1.43.2.6 chap * input until something other than DFA_ERR or DFA_HUH is returned, at which
341 1.43.2.6 chap * point things are resynchronized.
342 1.43.2.6 chap *
343 1.43.2.6 chap * A DFA_SYX return means that between pos and end are from 1 to 3
344 1.43.2.6 chap * bytes of a system exclusive message. A SysEx message will be delivered in
345 1.43.2.6 chap * one or more chunks of that form, where the first begins with 0xf0 and the
346 1.43.2.6 chap * last (which is the only one that might have length < 3) ends with 0xf7.
347 1.43.2.6 chap *
348 1.43.2.6 chap * Messages corrupted by a protocol error are discarded and won't be seen at
349 1.43.2.6 chap * all; again SysEx is the exception, as one or more chunks of it may already
350 1.43.2.6 chap * have been parsed.
351 1.43.2.6 chap *
352 1.43.2.6 chap * For DFA_CHN messages, s->msg[0] always contains the status byte even if
353 1.43.2.6 chap * compression was requested (pos then points to msg[1]). That way, the
354 1.43.2.6 chap * caller can always identify the exact message if there is a need to do so.
355 1.43.2.6 chap * For all other message types except DFA_SYX, the status byte is at *pos
356 1.43.2.6 chap * (which may not necessarily be msg[0]!). There is only one SysEx status
357 1.43.2.6 chap * byte, so the return value DFA_SYX is sufficient to identify it.
358 1.43.2.6 chap */
359 1.43.2.6 chap static enum dfa_ret
360 1.43.2.6 chap midi_dfa(struct midi_state *s, u_char c, int compress)
361 1.43.2.6 chap {
362 1.43.2.6 chap int syxpos = 0;
363 1.43.2.6 chap compress = compress ? 1 : 0;
364 1.14 augustss
365 1.43.2.6 chap if ( c >= 0xf8 ) { /* All realtime messages bypass state machine */
366 1.43.2.6 chap if ( c == 0xf9 || c == 0xfd ) {
367 1.43.2.6 chap DPRINTF( ("midi_dfa: s=%p c=0x%02x undefined\n",
368 1.43.2.6 chap s, c));
369 1.43.2.6 chap s->bytesDiscarded.ev_count++;
370 1.43.2.6 chap return DFA_ERR;
371 1.43.2.2 chap }
372 1.43.2.6 chap DPRINTFN(9, ("midi_dfa: s=%p System Real-Time data=0x%02x\n",
373 1.43.2.6 chap s, c));
374 1.43.2.6 chap s->msg[2] = c;
375 1.43.2.6 chap DFA_RETURN(2,3,DFA_RT);
376 1.43.2.2 chap }
377 1.43.2.2 chap
378 1.43.2.6 chap DPRINTFN(4, ("midi_dfa: s=%p data=0x%02x state=%d\n",
379 1.43.2.6 chap s, c, s->state));
380 1.43.2.2 chap
381 1.43.2.6 chap switch ( s->state | MIDI_CAT(c) ) { /* break ==> return DFA_MORE */
382 1.43.2.2 chap
383 1.43.2.2 chap case MIDI_IN_START | MIDI_CAT_COMMON:
384 1.43.2.2 chap case MIDI_IN_RUN1_1 | MIDI_CAT_COMMON:
385 1.43.2.2 chap case MIDI_IN_RUN2_2 | MIDI_CAT_COMMON:
386 1.43.2.6 chap s->msg[0] = c;
387 1.43.2.6 chap switch ( c ) {
388 1.43.2.6 chap case 0xf0: s->state = MIDI_IN_SYX1_3; break;
389 1.43.2.6 chap case 0xf1: s->state = MIDI_IN_COM0_1; break;
390 1.43.2.6 chap case 0xf2: s->state = MIDI_IN_COM0_2; break;
391 1.43.2.6 chap case 0xf3: s->state = MIDI_IN_COM0_1; break;
392 1.43.2.6 chap case 0xf6: s->state = MIDI_IN_START; DFA_RETURN(0,1,DFA_COM);
393 1.43.2.2 chap default: goto protocol_violation;
394 1.1 augustss }
395 1.43.2.2 chap break;
396 1.43.2.2 chap
397 1.43.2.2 chap case MIDI_IN_RUN1_1 | MIDI_CAT_STATUS1:
398 1.43.2.6 chap if ( c == s->msg[0] ) {
399 1.43.2.6 chap s->state = MIDI_IN_RNX0_1;
400 1.43.2.6 chap break;
401 1.43.2.6 chap }
402 1.43.2.6 chap /* FALLTHROUGH */
403 1.43.2.2 chap case MIDI_IN_RUN2_2 | MIDI_CAT_STATUS1:
404 1.43.2.6 chap case MIDI_IN_START | MIDI_CAT_STATUS1:
405 1.43.2.6 chap s->state = MIDI_IN_RUN0_1;
406 1.43.2.6 chap s->msg[0] = c;
407 1.43.2.6 chap break;
408 1.43.2.2 chap
409 1.43.2.2 chap case MIDI_IN_RUN2_2 | MIDI_CAT_STATUS2:
410 1.43.2.6 chap if ( c == s->msg[0] ) {
411 1.43.2.6 chap s->state = MIDI_IN_RNX0_2;
412 1.43.2.6 chap break;
413 1.43.2.6 chap }
414 1.43.2.6 chap /* FALLTHROUGH */
415 1.43.2.6 chap case MIDI_IN_RUN1_1 | MIDI_CAT_STATUS2:
416 1.43.2.6 chap case MIDI_IN_START | MIDI_CAT_STATUS2:
417 1.43.2.6 chap s->state = MIDI_IN_RUN0_2;
418 1.43.2.6 chap s->msg[0] = c;
419 1.43.2.6 chap break;
420 1.43.2.2 chap
421 1.43.2.2 chap case MIDI_IN_COM0_1 | MIDI_CAT_DATA:
422 1.43.2.6 chap s->state = MIDI_IN_START;
423 1.43.2.6 chap s->msg[1] = c;
424 1.43.2.6 chap DFA_RETURN(0,2,DFA_COM);
425 1.43.2.2 chap
426 1.43.2.2 chap case MIDI_IN_COM0_2 | MIDI_CAT_DATA:
427 1.43.2.6 chap s->state = MIDI_IN_COM1_2;
428 1.43.2.6 chap s->msg[1] = c;
429 1.43.2.6 chap break;
430 1.43.2.2 chap
431 1.43.2.2 chap case MIDI_IN_COM1_2 | MIDI_CAT_DATA:
432 1.43.2.6 chap s->state = MIDI_IN_START;
433 1.43.2.6 chap s->msg[2] = c;
434 1.43.2.6 chap DFA_RETURN(0,3,DFA_COM);
435 1.43.2.2 chap
436 1.43.2.2 chap case MIDI_IN_RUN0_1 | MIDI_CAT_DATA:
437 1.43.2.6 chap s->state = MIDI_IN_RUN1_1;
438 1.43.2.6 chap s->msg[1] = c;
439 1.43.2.6 chap DFA_RETURN(0,2,DFA_CHN);
440 1.43.2.6 chap
441 1.43.2.2 chap case MIDI_IN_RUN1_1 | MIDI_CAT_DATA:
442 1.43.2.6 chap case MIDI_IN_RNX0_1 | MIDI_CAT_DATA:
443 1.43.2.6 chap s->state = MIDI_IN_RUN1_1;
444 1.43.2.6 chap s->msg[1] = c;
445 1.43.2.6 chap DFA_RETURN(compress,2,DFA_CHN);
446 1.43.2.2 chap
447 1.43.2.2 chap case MIDI_IN_RUN0_2 | MIDI_CAT_DATA:
448 1.43.2.6 chap s->state = MIDI_IN_RUN1_2;
449 1.43.2.6 chap s->msg[1] = c;
450 1.43.2.6 chap break;
451 1.43.2.2 chap
452 1.43.2.2 chap case MIDI_IN_RUN1_2 | MIDI_CAT_DATA:
453 1.43.2.6 chap s->state = MIDI_IN_RUN2_2;
454 1.43.2.6 chap s->msg[2] = c;
455 1.43.2.6 chap DFA_RETURN(0,3,DFA_CHN);
456 1.43.2.6 chap
457 1.43.2.6 chap case MIDI_IN_RUN2_2 | MIDI_CAT_DATA:
458 1.43.2.6 chap s->state = MIDI_IN_RNX1_2;
459 1.43.2.6 chap s->msg[1] = c;
460 1.43.2.6 chap break;
461 1.43.2.6 chap
462 1.43.2.6 chap case MIDI_IN_RNX0_2 | MIDI_CAT_DATA:
463 1.43.2.6 chap s->state = MIDI_IN_RNY1_2;
464 1.43.2.6 chap s->msg[1] = c;
465 1.43.2.6 chap break;
466 1.43.2.6 chap
467 1.43.2.6 chap case MIDI_IN_RNX1_2 | MIDI_CAT_DATA:
468 1.43.2.6 chap case MIDI_IN_RNY1_2 | MIDI_CAT_DATA:
469 1.43.2.6 chap s->state = MIDI_IN_RUN2_2;
470 1.43.2.6 chap s->msg[2] = c;
471 1.43.2.6 chap DFA_RETURN(compress,3,DFA_CHN);
472 1.43.2.6 chap
473 1.43.2.6 chap case MIDI_IN_SYX1_3 | MIDI_CAT_DATA:
474 1.43.2.6 chap s->state = MIDI_IN_SYX2_3;
475 1.43.2.6 chap s->msg[1] = c;
476 1.43.2.2 chap break;
477 1.43.2.2 chap
478 1.43.2.6 chap case MIDI_IN_SYX2_3 | MIDI_CAT_DATA:
479 1.43.2.6 chap s->state = MIDI_IN_SYX0_3;
480 1.43.2.6 chap s->msg[2] = c;
481 1.43.2.6 chap DFA_RETURN(0,3,DFA_SYX);
482 1.43.2.6 chap
483 1.43.2.6 chap case MIDI_IN_SYX0_3 | MIDI_CAT_DATA:
484 1.43.2.6 chap s->state = MIDI_IN_SYX1_3;
485 1.43.2.6 chap s->msg[0] = c;
486 1.43.2.6 chap break;
487 1.43.2.6 chap
488 1.43.2.6 chap case MIDI_IN_SYX2_3 | MIDI_CAT_COMMON:
489 1.43.2.6 chap ++ syxpos;
490 1.43.2.6 chap /* FALLTHROUGH */
491 1.43.2.6 chap case MIDI_IN_SYX1_3 | MIDI_CAT_COMMON:
492 1.43.2.6 chap ++ syxpos;
493 1.43.2.6 chap /* FALLTHROUGH */
494 1.43.2.6 chap case MIDI_IN_SYX0_3 | MIDI_CAT_COMMON:
495 1.43.2.6 chap if ( c == 0xf7 ) {
496 1.43.2.6 chap s->state = MIDI_IN_START;
497 1.43.2.6 chap s->msg[syxpos] = c;
498 1.43.2.6 chap DFA_RETURN(0,1+syxpos,DFA_SYX);
499 1.43.2.2 chap }
500 1.43.2.2 chap /* FALLTHROUGH */
501 1.43.2.2 chap
502 1.43.2.2 chap default:
503 1.43.2.2 chap protocol_violation:
504 1.43.2.6 chap DPRINTF(("midi_dfa: unexpected %#02x in state %u\n",
505 1.43.2.6 chap c, s->state));
506 1.43.2.6 chap switch ( s->state ) {
507 1.43.2.3 chap case MIDI_IN_RUN1_1: /* can only get here by seeing an */
508 1.43.2.3 chap case MIDI_IN_RUN2_2: /* INVALID System Common message */
509 1.43.2.6 chap s->state = MIDI_IN_START;
510 1.43.2.3 chap /* FALLTHROUGH */
511 1.43.2.2 chap case MIDI_IN_START:
512 1.43.2.6 chap s->bytesDiscarded.ev_count++;
513 1.43.2.6 chap return DFA_ERR;
514 1.43.2.2 chap case MIDI_IN_COM1_2:
515 1.43.2.2 chap case MIDI_IN_RUN1_2:
516 1.43.2.6 chap case MIDI_IN_SYX2_3:
517 1.43.2.6 chap case MIDI_IN_RNY1_2:
518 1.43.2.6 chap s->bytesDiscarded.ev_count++;
519 1.43.2.2 chap /* FALLTHROUGH */
520 1.43.2.2 chap case MIDI_IN_COM0_1:
521 1.43.2.2 chap case MIDI_IN_RUN0_1:
522 1.43.2.6 chap case MIDI_IN_RNX0_1:
523 1.43.2.2 chap case MIDI_IN_COM0_2:
524 1.43.2.2 chap case MIDI_IN_RUN0_2:
525 1.43.2.6 chap case MIDI_IN_RNX0_2:
526 1.43.2.6 chap case MIDI_IN_RNX1_2:
527 1.43.2.6 chap case MIDI_IN_SYX1_3:
528 1.43.2.6 chap s->bytesDiscarded.ev_count++;
529 1.43.2.2 chap /* FALLTHROUGH */
530 1.43.2.6 chap case MIDI_IN_SYX0_3:
531 1.43.2.6 chap s->incompleteMessages.ev_count++;
532 1.43.2.2 chap break;
533 1.43.2.2 chap #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
534 1.43.2.2 chap default:
535 1.43.2.6 chap printf("midi_dfa: mishandled %#02x(%u) in state %u?!\n",
536 1.43.2.6 chap c, MIDI_CAT(c), s->state);
537 1.43.2.2 chap #endif
538 1.43.2.2 chap }
539 1.43.2.6 chap s->state = MIDI_IN_START;
540 1.43.2.6 chap return DFA_HUH;
541 1.1 augustss }
542 1.43.2.6 chap return DFA_MORE;
543 1.43.2.6 chap }
544 1.43.2.2 chap
545 1.43.2.6 chap void
546 1.43.2.6 chap midi_in(void *addr, int data)
547 1.43.2.6 chap {
548 1.43.2.6 chap struct midi_softc *sc = addr;
549 1.43.2.6 chap struct midi_buffer *mb = &sc->inbuf;
550 1.43.2.6 chap int i;
551 1.43.2.6 chap int count;
552 1.43.2.6 chap enum dfa_ret got;
553 1.43.2.6 chap
554 1.43.2.6 chap if (!sc->isopen)
555 1.1 augustss return;
556 1.43.2.6 chap
557 1.43.2.6 chap if (!(sc->flags & FREAD))
558 1.43.2.6 chap return; /* discard data if not reading */
559 1.43.2.6 chap
560 1.43.2.6 chap do
561 1.43.2.6 chap got = midi_dfa(&sc->rcv, data, 0);
562 1.43.2.6 chap while ( got == DFA_HUH );
563 1.43.2.6 chap
564 1.43.2.6 chap switch ( got ) {
565 1.43.2.6 chap case DFA_MORE:
566 1.43.2.6 chap case DFA_ERR:
567 1.1 augustss return;
568 1.43.2.6 chap case DFA_CHN:
569 1.43.2.6 chap case DFA_COM:
570 1.43.2.6 chap case DFA_RT:
571 1.43.2.6 chap #if NSEQUENCER > 0
572 1.43.2.6 chap if (sc->seqopen) {
573 1.43.2.6 chap extern void midiseq_in(struct midi_dev *,u_char *,int);
574 1.43.2.6 chap count = sc->rcv.end - sc->rcv.pos;
575 1.43.2.6 chap midiseq_in(sc->seq_md, sc->rcv.pos, count);
576 1.43.2.6 chap return;
577 1.43.2.6 chap }
578 1.43.2.6 chap #endif
579 1.43.2.6 chap /*
580 1.43.2.6 chap * Pass Active Sense to the sequencer if it's open, but not to
581 1.43.2.6 chap * a raw reader. (Really should do something intelligent with
582 1.43.2.6 chap * it then, though....)
583 1.43.2.6 chap */
584 1.43.2.6 chap if ( got == DFA_RT && MIDI_ACK == sc->rcv.pos[0] )
585 1.43.2.6 chap return;
586 1.43.2.6 chap /* FALLTHROUGH */
587 1.43.2.6 chap /*
588 1.43.2.6 chap * Ultimately SysEx msgs should be offered to the sequencer also; the
589 1.43.2.6 chap * sequencer API addresses them - but maybe our sequencer can't handle
590 1.43.2.6 chap * them yet, so offer only to raw reader. (Which means, ultimately,
591 1.43.2.6 chap * discard them if the sequencer's open, as it's not doing reads!)
592 1.43.2.6 chap */
593 1.43.2.6 chap case DFA_SYX:
594 1.43.2.6 chap count = sc->rcv.end - sc->rcv.pos;
595 1.43.2.6 chap if (mb->used + count > mb->usedhigh) {
596 1.43.2.6 chap DPRINTF(("midi_in: buffer full, discard data=0x%02x\n",
597 1.43.2.6 chap sc->rcv.pos[0]));
598 1.43.2.6 chap sc->rcv.bytesDiscarded.ev_count += count;
599 1.43.2.6 chap return;
600 1.43.2.6 chap }
601 1.43.2.6 chap for (i = 0; i < count; i++) {
602 1.43.2.6 chap *mb->inp++ = sc->rcv.pos[i];
603 1.43.2.6 chap if (mb->inp >= mb->end)
604 1.43.2.6 chap mb->inp = mb->start;
605 1.43.2.6 chap mb->used++;
606 1.43.2.6 chap }
607 1.43.2.6 chap midi_wakeup(&sc->rchan);
608 1.43.2.6 chap selnotify(&sc->rsel, 0);
609 1.43.2.6 chap if (sc->async)
610 1.43.2.6 chap psignal(sc->async, SIGIO);
611 1.43.2.6 chap break;
612 1.43.2.6 chap #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
613 1.43.2.6 chap default:
614 1.43.2.6 chap printf("midi_in: midi_dfa returned %d?!\n", got);
615 1.43.2.6 chap #endif
616 1.1 augustss }
617 1.1 augustss }
618 1.1 augustss
619 1.1 augustss void
620 1.22 augustss midi_out(void *addr)
621 1.1 augustss {
622 1.1 augustss struct midi_softc *sc = addr;
623 1.3 augustss
624 1.3 augustss if (!sc->isopen)
625 1.3 augustss return;
626 1.43.2.2 chap DPRINTFN(8, ("midi_out: %p\n", sc));
627 1.43.2.5 chap midi_start_output(sc, 0, 1);
628 1.1 augustss }
629 1.1 augustss
630 1.1 augustss int
631 1.43.2.1 chap midiopen(dev_t dev, int flags, int ifmt, struct proc *p)
632 1.1 augustss {
633 1.1 augustss struct midi_softc *sc;
634 1.43.2.1 chap struct midi_hw_if *hw;
635 1.1 augustss int error;
636 1.1 augustss
637 1.17 thorpej sc = device_lookup(&midi_cd, MIDIUNIT(dev));
638 1.17 thorpej if (sc == NULL)
639 1.17 thorpej return (ENXIO);
640 1.18 tshiozak if (sc->dying)
641 1.18 tshiozak return (EIO);
642 1.17 thorpej
643 1.43.2.2 chap DPRINTFN(3,("midiopen %p\n", sc));
644 1.1 augustss
645 1.1 augustss hw = sc->hw_if;
646 1.1 augustss if (!hw)
647 1.1 augustss return ENXIO;
648 1.1 augustss if (sc->isopen)
649 1.1 augustss return EBUSY;
650 1.43.2.6 chap sc->rcv.state = MIDI_IN_START;
651 1.43.2.6 chap sc->xmt.state = MIDI_IN_START;
652 1.43.2.6 chap sc->xmt.pos = sc->xmt.msg;
653 1.43.2.6 chap sc->xmt.end = sc->xmt.msg;
654 1.1 augustss error = hw->open(sc->hw_hdl, flags, midi_in, midi_out, sc);
655 1.1 augustss if (error)
656 1.1 augustss return error;
657 1.1 augustss sc->isopen++;
658 1.6 mycroft midi_initbuf(&sc->outbuf);
659 1.6 mycroft midi_initbuf(&sc->inbuf);
660 1.1 augustss sc->flags = flags;
661 1.1 augustss sc->rchan = 0;
662 1.1 augustss sc->wchan = 0;
663 1.1 augustss sc->pbus = 0;
664 1.1 augustss sc->async = 0;
665 1.1 augustss
666 1.4 augustss #ifdef MIDI_SAVE
667 1.4 augustss if (midicnt != 0) {
668 1.4 augustss midisave.cnt = midicnt;
669 1.4 augustss midicnt = 0;
670 1.4 augustss }
671 1.4 augustss #endif
672 1.4 augustss
673 1.1 augustss return 0;
674 1.1 augustss }
675 1.1 augustss
676 1.1 augustss int
677 1.43.2.1 chap midiclose(dev_t dev, int flags, int ifmt, struct proc *p)
678 1.1 augustss {
679 1.1 augustss int unit = MIDIUNIT(dev);
680 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
681 1.43.2.1 chap struct midi_hw_if *hw = sc->hw_if;
682 1.1 augustss int s, error;
683 1.1 augustss
684 1.43.2.3 chap DPRINTFN(3,("midiclose %p\n", sc));
685 1.1 augustss
686 1.43.2.5 chap midi_start_output(sc, 1, 0);
687 1.1 augustss error = 0;
688 1.1 augustss s = splaudio();
689 1.1 augustss while (sc->outbuf.used > 0 && !error) {
690 1.43.2.2 chap DPRINTFN(8,("midiclose sleep used=%d\n", sc->outbuf.used));
691 1.3 augustss error = midi_sleep_timo(&sc->wchan, "mid_dr", 30*hz);
692 1.1 augustss }
693 1.6 mycroft splx(s);
694 1.43.2.5 chap callout_stop(&sc->sc_callout);
695 1.3 augustss sc->isopen = 0;
696 1.1 augustss hw->close(sc->hw_hdl);
697 1.1 augustss #if NSEQUENCER > 0
698 1.1 augustss sc->seqopen = 0;
699 1.7 augustss sc->seq_md = 0;
700 1.1 augustss #endif
701 1.1 augustss return 0;
702 1.1 augustss }
703 1.1 augustss
704 1.1 augustss int
705 1.22 augustss midiread(dev_t dev, struct uio *uio, int ioflag)
706 1.1 augustss {
707 1.1 augustss int unit = MIDIUNIT(dev);
708 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
709 1.1 augustss struct midi_buffer *mb = &sc->inbuf;
710 1.1 augustss int error;
711 1.1 augustss u_char *outp;
712 1.1 augustss int used, cc, n, resid;
713 1.1 augustss int s;
714 1.1 augustss
715 1.43.2.2 chap DPRINTFN(6,("midiread: %p, count=%lu\n", sc,
716 1.11 nathanw (unsigned long)uio->uio_resid));
717 1.1 augustss
718 1.18 tshiozak if (sc->dying)
719 1.18 tshiozak return EIO;
720 1.43.2.4 chap if ( !(sc->flags & FREAD) )
721 1.43.2.4 chap return EBADF;
722 1.43.2.4 chap if ( !(sc->props & MIDI_PROP_CAN_INPUT) )
723 1.43.2.4 chap return ENXIO;
724 1.18 tshiozak
725 1.1 augustss error = 0;
726 1.1 augustss resid = uio->uio_resid;
727 1.1 augustss while (uio->uio_resid == resid && !error) {
728 1.1 augustss s = splaudio();
729 1.1 augustss while (mb->used <= 0) {
730 1.1 augustss if (ioflag & IO_NDELAY) {
731 1.1 augustss splx(s);
732 1.1 augustss return EWOULDBLOCK;
733 1.1 augustss }
734 1.1 augustss error = midi_sleep(&sc->rchan, "mid rd");
735 1.1 augustss if (error) {
736 1.1 augustss splx(s);
737 1.1 augustss return error;
738 1.1 augustss }
739 1.1 augustss }
740 1.1 augustss used = mb->used;
741 1.1 augustss outp = mb->outp;
742 1.1 augustss splx(s);
743 1.18 tshiozak if (sc->dying)
744 1.18 tshiozak return EIO;
745 1.1 augustss cc = used; /* maximum to read */
746 1.1 augustss n = mb->end - outp;
747 1.1 augustss if (n < cc)
748 1.1 augustss cc = n; /* don't read beyond end of buffer */
749 1.1 augustss if (uio->uio_resid < cc)
750 1.1 augustss cc = uio->uio_resid; /* and no more than we want */
751 1.43.2.2 chap DPRINTFN(8, ("midiread: uiomove cc=%d\n", cc));
752 1.1 augustss error = uiomove(outp, cc, uio);
753 1.1 augustss if (error)
754 1.1 augustss break;
755 1.1 augustss used -= cc;
756 1.1 augustss outp += cc;
757 1.1 augustss if (outp >= mb->end)
758 1.1 augustss outp = mb->start;
759 1.1 augustss s = splaudio();
760 1.1 augustss mb->outp = outp;
761 1.1 augustss mb->used = used;
762 1.1 augustss splx(s);
763 1.1 augustss }
764 1.1 augustss return error;
765 1.1 augustss }
766 1.1 augustss
767 1.1 augustss void
768 1.22 augustss midi_timeout(void *arg)
769 1.1 augustss {
770 1.1 augustss struct midi_softc *sc = arg;
771 1.1 augustss
772 1.43.2.2 chap DPRINTFN(8,("midi_timeout: %p\n", sc));
773 1.43.2.5 chap midi_start_output(sc, 0, 0);
774 1.1 augustss }
775 1.1 augustss
776 1.43.2.5 chap /*
777 1.43.2.5 chap * Control can reach midi_start_output three ways:
778 1.43.2.5 chap * 1. as a result of user writing (user == 1)
779 1.43.2.5 chap * 2. by a ready interrupt from output hw (hw == 1)
780 1.43.2.5 chap * 3. by a callout scheduled at the end of midi_start_output.
781 1.43.2.5 chap *
782 1.43.2.5 chap * The callout is not scheduled unless we are sure the hw will not
783 1.43.2.5 chap * interrupt again (either because it just did and we supplied it
784 1.43.2.5 chap * no data, or because it lacks interrupt capability). So we do not
785 1.43.2.5 chap * expect a hw interrupt to occur with a callout pending.
786 1.43.2.5 chap *
787 1.43.2.5 chap * A user write may occur while the callout is pending, however, or before
788 1.43.2.5 chap * an interrupt-capable device has interrupted. There are two cases:
789 1.43.2.5 chap *
790 1.43.2.5 chap * 1. A device interrupt is expected, or the pending callout is a MIDI_WAIT
791 1.43.2.5 chap * (the brief delay we put in if MIDI_MAX_WRITE characters have been
792 1.43.2.5 chap * written without yielding; interrupt-capable hardware can avoid this
793 1.43.2.5 chap * penalty by making sure not to use EINPROGRESS to consume MIDI_MAX_WRITE
794 1.43.2.5 chap * or more characters at once). In these cases, the user invocation of
795 1.43.2.5 chap * start_output returns without action, leaving the output to be resumed
796 1.43.2.5 chap * by the device interrupt or by the callout after the proper delay. This
797 1.43.2.5 chap * case is identified by sc->pbus == 1.
798 1.43.2.5 chap *
799 1.43.2.5 chap * 2. The pending callout was scheduled to ensure an Active Sense gets
800 1.43.2.5 chap * written if there is no user write in time. In this case the user
801 1.43.2.5 chap * invocation (if there is something to write) cancels the pending
802 1.43.2.5 chap * callout. However, it may be possible the callout timer expired before
803 1.43.2.5 chap * we could cancel it, but the callout hasn't run yet. In this case
804 1.43.2.5 chap * (INVOKING status on the callout) the user invocation returns without
805 1.43.2.6 chap * further action so the callout can do the work.
806 1.43.2.5 chap *
807 1.43.2.5 chap * Is it possible that fast interrupt-driven hardware could interrupt while
808 1.43.2.5 chap * the start_output that triggered it still holds the lock? In this case we'll
809 1.43.2.5 chap * see on unlock that sc->hw_interrupted == 1 and loop around again if there
810 1.43.2.5 chap * is more to write.
811 1.43.2.5 chap */
812 1.1 augustss int
813 1.43.2.5 chap midi_start_output(struct midi_softc *sc, int user, int hw)
814 1.1 augustss {
815 1.1 augustss struct midi_buffer *mb = &sc->outbuf;
816 1.21 tshiozak u_char out;
817 1.1 augustss int error;
818 1.1 augustss int s;
819 1.43.2.5 chap int written;
820 1.43.2.5 chap int to_write;
821 1.43.2.5 chap u_char *from;
822 1.43.2.5 chap u_char *to;
823 1.1 augustss
824 1.43.2.5 chap again:
825 1.43.2.5 chap if ( mididebug > 9 ) return EIO;
826 1.1 augustss error = 0;
827 1.18 tshiozak
828 1.18 tshiozak if (sc->dying)
829 1.18 tshiozak return EIO;
830 1.18 tshiozak
831 1.43.2.5 chap s = splaudio();
832 1.43.2.5 chap
833 1.43.2.5 chap if ( ! simple_lock_try(&sc->out_lock) ) {
834 1.43.2.5 chap if ( hw )
835 1.43.2.5 chap sc->hw_interrupted = 1;
836 1.43.2.5 chap splx(s);
837 1.43.2.5 chap DPRINTFN(8,("midi_start_output: locked out user=%d hw=%d\n",
838 1.43.2.5 chap user, hw));
839 1.1 augustss return 0;
840 1.1 augustss }
841 1.43.2.5 chap
842 1.43.2.5 chap to_write = mb->used;
843 1.43.2.5 chap
844 1.43.2.5 chap if ( user ) {
845 1.43.2.5 chap if ( sc->pbus ) {
846 1.43.2.5 chap simple_unlock(&sc->out_lock);
847 1.43.2.5 chap splx(s);
848 1.43.2.5 chap DPRINTFN(8, ("midi_start_output: delaying\n"));
849 1.43.2.5 chap return 0;
850 1.43.2.5 chap }
851 1.43.2.5 chap if ( to_write > 0 ) {
852 1.43.2.5 chap callout_stop(&sc->sc_callout);
853 1.43.2.5 chap if (callout_invoking(&sc->sc_callout)) {
854 1.43.2.5 chap simple_unlock(&sc->out_lock);
855 1.43.2.5 chap splx(s);
856 1.43.2.5 chap DPRINTFN(8,("midi_start_output: "
857 1.43.2.5 chap "callout got away\n"));
858 1.43.2.5 chap return 0;
859 1.43.2.5 chap }
860 1.43.2.5 chap }
861 1.43.2.5 chap } else if ( hw )
862 1.43.2.5 chap sc->hw_interrupted = 0;
863 1.43.2.5 chap else
864 1.43.2.5 chap callout_ack(&sc->sc_callout);
865 1.43.2.5 chap
866 1.43.2.5 chap splx(s);
867 1.43.2.5 chap /*
868 1.43.2.5 chap * As long as we hold the lock, we can rely on mb->outp not changing
869 1.43.2.5 chap * and mb->used not decreasing. We only need splaudio to update them.
870 1.43.2.5 chap */
871 1.43.2.5 chap
872 1.43.2.5 chap from = mb->outp;
873 1.43.2.5 chap
874 1.43.2.5 chap DPRINTFN(8,("midi_start_output: user=%d hw=%d to_write %u from %p\n",
875 1.43.2.5 chap user, hw, to_write, from));
876 1.43.2.5 chap sc->pbus = 0;
877 1.43.2.5 chap
878 1.43.2.5 chap written = 0;
879 1.43.2.5 chap if ( to_write == 0 && ! user && ! hw ) {
880 1.43.2.5 chap error = sc->hw_if->output(sc->hw_hdl, MIDI_ACK);
881 1.43.2.5 chap written = 1;
882 1.43.2.5 chap }
883 1.43.2.5 chap
884 1.43.2.5 chap do {
885 1.43.2.5 chap if ( to_write > MIDI_MAX_WRITE - written )
886 1.43.2.5 chap to_write = MIDI_MAX_WRITE - written;
887 1.43.2.5 chap if ( to_write == 0 )
888 1.43.2.5 chap break;
889 1.43.2.5 chap to = from + to_write;
890 1.43.2.5 chap if ( to > mb->end )
891 1.43.2.5 chap to = mb->end;
892 1.43.2.5 chap while ( from < to ) {
893 1.43.2.5 chap out = *from++;
894 1.4 augustss #ifdef MIDI_SAVE
895 1.43.2.5 chap midisave.buf[midicnt] = out;
896 1.43.2.5 chap midicnt = (midicnt + 1) % MIDI_SAVE_SIZE;
897 1.4 augustss #endif
898 1.43.2.5 chap DPRINTFN(8, ("midi_start_output: %p data=0x%02x\n",
899 1.43.2.5 chap sc, out));
900 1.43.2.5 chap error = sc->hw_if->output(sc->hw_hdl, out);
901 1.43.2.5 chap if ( error == 0 ) {
902 1.43.2.5 chap if ( sc->props & MIDI_PROP_OUT_INTR )
903 1.43.2.5 chap break;
904 1.43.2.5 chap } else if ( ( sc->props & MIDI_PROP_OUT_INTR )
905 1.43.2.5 chap && error == EINPROGRESS )
906 1.43.2.5 chap continue;
907 1.43.2.5 chap else { /* really an error */
908 1.43.2.5 chap -- from;
909 1.43.2.5 chap break;
910 1.43.2.5 chap }
911 1.43.2.5 chap }
912 1.43.2.5 chap written += from - mb->outp;
913 1.43.2.5 chap s = splaudio();
914 1.43.2.5 chap mb->used -= from - mb->outp;
915 1.43.2.5 chap if ( from == mb->end )
916 1.43.2.5 chap from = mb->start;
917 1.43.2.5 chap mb->outp = from;
918 1.43.2.5 chap splx(s);
919 1.43.2.5 chap to_write = mb->used;
920 1.43.2.5 chap } while ( error == ( sc->props&MIDI_PROP_OUT_INTR ) ? EINPROGRESS : 0 );
921 1.43.2.5 chap
922 1.43.2.5 chap if ( written > 0 ) {
923 1.43.2.5 chap midi_wakeup(&sc->wchan);
924 1.43.2.5 chap selnotify(&sc->wsel, 0);
925 1.43.2.5 chap if (sc->async)
926 1.43.2.5 chap psignal(sc->async, SIGIO);
927 1.43.2.5 chap }
928 1.43.2.5 chap
929 1.43.2.5 chap if ( sc->props & MIDI_PROP_OUT_INTR ) {
930 1.43.2.5 chap if ( written > 0 && error == 0 ) {
931 1.43.2.5 chap sc->pbus = 1;
932 1.43.2.5 chap goto unlock_exit;
933 1.43.2.5 chap }
934 1.43.2.5 chap if ( error == EINPROGRESS )
935 1.43.2.5 chap error = 0;
936 1.1 augustss }
937 1.43.2.5 chap
938 1.43.2.5 chap if ( error != 0 )
939 1.43.2.5 chap goto handle_error;
940 1.43.2.5 chap
941 1.43.2.5 chap if ( to_write == 0 )
942 1.43.2.5 chap callout_schedule(&sc->sc_callout, mstohz(285));
943 1.43.2.5 chap else {
944 1.43.2.5 chap sc->pbus = 1;
945 1.43.2.5 chap callout_schedule(&sc->sc_callout, midi_wait);
946 1.43.2.5 chap }
947 1.43.2.5 chap goto unlock_exit;
948 1.43.2.5 chap
949 1.43.2.5 chap handle_error:
950 1.43.2.5 chap #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
951 1.43.2.5 chap printf("%s: midi_start_output error %d\n",
952 1.43.2.5 chap sc->dev.dv_xname, error);
953 1.43.2.5 chap #endif
954 1.43.2.5 chap
955 1.43.2.5 chap unlock_exit:
956 1.43.2.5 chap simple_unlock(&sc->out_lock);
957 1.43.2.5 chap if ( sc->hw_interrupted ) {
958 1.43.2.5 chap user = 0;
959 1.43.2.5 chap hw = 1;
960 1.43.2.5 chap goto again;
961 1.20 tshiozak }
962 1.20 tshiozak
963 1.1 augustss return error;
964 1.1 augustss }
965 1.1 augustss
966 1.1 augustss int
967 1.22 augustss midiwrite(dev_t dev, struct uio *uio, int ioflag)
968 1.1 augustss {
969 1.1 augustss int unit = MIDIUNIT(dev);
970 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
971 1.1 augustss struct midi_buffer *mb = &sc->outbuf;
972 1.1 augustss int error;
973 1.1 augustss u_char *inp;
974 1.1 augustss int used, cc, n;
975 1.1 augustss int s;
976 1.1 augustss
977 1.43.2.2 chap DPRINTFN(6, ("midiwrite: %p, unit=%d, count=%lu\n", sc, unit,
978 1.11 nathanw (unsigned long)uio->uio_resid));
979 1.1 augustss
980 1.18 tshiozak if (sc->dying)
981 1.18 tshiozak return EIO;
982 1.43.2.4 chap if ( !(sc->flags & FWRITE) )
983 1.43.2.4 chap return EBADF;
984 1.18 tshiozak
985 1.1 augustss error = 0;
986 1.1 augustss while (uio->uio_resid > 0 && !error) {
987 1.1 augustss s = splaudio();
988 1.1 augustss if (mb->used >= mb->usedhigh) {
989 1.43.2.2 chap DPRINTFN(8,("midi_write: sleep used=%d hiwat=%d\n",
990 1.1 augustss mb->used, mb->usedhigh));
991 1.1 augustss if (ioflag & IO_NDELAY) {
992 1.1 augustss splx(s);
993 1.1 augustss return EWOULDBLOCK;
994 1.1 augustss }
995 1.1 augustss error = midi_sleep(&sc->wchan, "mid wr");
996 1.1 augustss if (error) {
997 1.1 augustss splx(s);
998 1.1 augustss return error;
999 1.1 augustss }
1000 1.43.2.1 chap }
1001 1.1 augustss used = mb->used;
1002 1.1 augustss inp = mb->inp;
1003 1.1 augustss splx(s);
1004 1.18 tshiozak if (sc->dying)
1005 1.18 tshiozak return EIO;
1006 1.1 augustss cc = mb->usedhigh - used; /* maximum to write */
1007 1.1 augustss n = mb->end - inp;
1008 1.1 augustss if (n < cc)
1009 1.9 augustss cc = n; /* don't write beyond end of buffer */
1010 1.1 augustss if (uio->uio_resid < cc)
1011 1.1 augustss cc = uio->uio_resid; /* and no more than we have */
1012 1.1 augustss error = uiomove(inp, cc, uio);
1013 1.1 augustss #ifdef MIDI_DEBUG
1014 1.1 augustss if (error)
1015 1.9 augustss printf("midi_write:(1) uiomove failed %d; "
1016 1.9 augustss "cc=%d inp=%p\n",
1017 1.1 augustss error, cc, inp);
1018 1.1 augustss #endif
1019 1.1 augustss if (error)
1020 1.1 augustss break;
1021 1.1 augustss inp = mb->inp + cc;
1022 1.1 augustss if (inp >= mb->end)
1023 1.1 augustss inp = mb->start;
1024 1.1 augustss s = splaudio();
1025 1.1 augustss mb->inp = inp;
1026 1.1 augustss mb->used += cc;
1027 1.1 augustss splx(s);
1028 1.43.2.5 chap error = midi_start_output(sc, 1, 0);
1029 1.1 augustss }
1030 1.1 augustss return error;
1031 1.5 augustss }
1032 1.5 augustss
1033 1.5 augustss /*
1034 1.9 augustss * This write routine is only called from sequencer code and expects
1035 1.5 augustss * a write that is smaller than the MIDI buffer.
1036 1.5 augustss */
1037 1.5 augustss int
1038 1.43.2.1 chap midi_writebytes(int unit, u_char *buf, int cc)
1039 1.5 augustss {
1040 1.5 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
1041 1.5 augustss struct midi_buffer *mb = &sc->outbuf;
1042 1.5 augustss int n, s;
1043 1.5 augustss
1044 1.43.2.2 chap DPRINTFN(7, ("midi_writebytes: %p, unit=%d, cc=%d %#02x %#02x %#02x\n",
1045 1.43.2.2 chap sc, unit, cc, buf[0], buf[1], buf[2]));
1046 1.5 augustss
1047 1.18 tshiozak if (sc->dying)
1048 1.18 tshiozak return EIO;
1049 1.18 tshiozak
1050 1.5 augustss s = splaudio();
1051 1.5 augustss if (mb->used + cc >= mb->usedhigh) {
1052 1.5 augustss splx(s);
1053 1.5 augustss return (EWOULDBLOCK);
1054 1.5 augustss }
1055 1.5 augustss n = mb->end - mb->inp;
1056 1.5 augustss if (cc < n)
1057 1.5 augustss n = cc;
1058 1.9 augustss mb->used += cc;
1059 1.43.2.1 chap memcpy(mb->inp, buf, n);
1060 1.5 augustss mb->inp += n;
1061 1.5 augustss if (mb->inp >= mb->end) {
1062 1.5 augustss mb->inp = mb->start;
1063 1.5 augustss cc -= n;
1064 1.5 augustss if (cc > 0) {
1065 1.43.2.1 chap memcpy(mb->inp, buf + n, cc);
1066 1.5 augustss mb->inp += cc;
1067 1.5 augustss }
1068 1.5 augustss }
1069 1.5 augustss splx(s);
1070 1.43.2.5 chap return (midi_start_output(sc, 1, 0));
1071 1.1 augustss }
1072 1.1 augustss
1073 1.1 augustss int
1074 1.43.2.1 chap midiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1075 1.1 augustss {
1076 1.1 augustss int unit = MIDIUNIT(dev);
1077 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
1078 1.43.2.1 chap struct midi_hw_if *hw = sc->hw_if;
1079 1.1 augustss int error;
1080 1.1 augustss
1081 1.43.2.2 chap DPRINTFN(5,("midiioctl: %p cmd=0x%08lx\n", sc, cmd));
1082 1.18 tshiozak
1083 1.18 tshiozak if (sc->dying)
1084 1.18 tshiozak return EIO;
1085 1.18 tshiozak
1086 1.1 augustss error = 0;
1087 1.1 augustss switch (cmd) {
1088 1.1 augustss case FIONBIO:
1089 1.1 augustss /* All handled in the upper FS layer. */
1090 1.1 augustss break;
1091 1.1 augustss
1092 1.1 augustss case FIOASYNC:
1093 1.1 augustss if (*(int *)addr) {
1094 1.1 augustss if (sc->async)
1095 1.1 augustss return EBUSY;
1096 1.43.2.1 chap sc->async = p;
1097 1.43.2.2 chap DPRINTFN(5,("midi_ioctl: FIOASYNC %p\n", p));
1098 1.1 augustss } else
1099 1.1 augustss sc->async = 0;
1100 1.1 augustss break;
1101 1.1 augustss
1102 1.1 augustss #if 0
1103 1.1 augustss case MIDI_PRETIME:
1104 1.1 augustss /* XXX OSS
1105 1.1 augustss * This should set up a read timeout, but that's
1106 1.1 augustss * why we have poll(), so there's nothing yet. */
1107 1.1 augustss error = EINVAL;
1108 1.1 augustss break;
1109 1.1 augustss #endif
1110 1.1 augustss
1111 1.4 augustss #ifdef MIDI_SAVE
1112 1.4 augustss case MIDI_GETSAVE:
1113 1.4 augustss error = copyout(&midisave, *(void **)addr, sizeof midisave);
1114 1.4 augustss break;
1115 1.4 augustss #endif
1116 1.4 augustss
1117 1.1 augustss default:
1118 1.1 augustss if (hw->ioctl)
1119 1.43.2.1 chap error = hw->ioctl(sc->hw_hdl, cmd, addr, flag, p);
1120 1.1 augustss else
1121 1.1 augustss error = EINVAL;
1122 1.1 augustss break;
1123 1.1 augustss }
1124 1.1 augustss return error;
1125 1.1 augustss }
1126 1.1 augustss
1127 1.1 augustss int
1128 1.43.2.1 chap midipoll(dev_t dev, int events, struct proc *p)
1129 1.1 augustss {
1130 1.1 augustss int unit = MIDIUNIT(dev);
1131 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
1132 1.1 augustss int revents = 0;
1133 1.24 gson int s;
1134 1.1 augustss
1135 1.43.2.2 chap DPRINTFN(6,("midipoll: %p events=0x%x\n", sc, events));
1136 1.1 augustss
1137 1.18 tshiozak if (sc->dying)
1138 1.43.2.1 chap return EIO;
1139 1.24 gson
1140 1.24 gson s = splaudio();
1141 1.18 tshiozak
1142 1.43.2.4 chap if ((sc->flags&FREAD) && (events & (POLLIN | POLLRDNORM)))
1143 1.1 augustss if (sc->inbuf.used > 0)
1144 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
1145 1.1 augustss
1146 1.43.2.4 chap if ((sc->flags&FWRITE) && (events & (POLLOUT | POLLWRNORM)))
1147 1.1 augustss if (sc->outbuf.used < sc->outbuf.usedhigh)
1148 1.1 augustss revents |= events & (POLLOUT | POLLWRNORM);
1149 1.1 augustss
1150 1.1 augustss if (revents == 0) {
1151 1.43.2.4 chap if ((sc->flags&FREAD) && (events & (POLLIN | POLLRDNORM)))
1152 1.43.2.1 chap selrecord(p, &sc->rsel);
1153 1.1 augustss
1154 1.43.2.4 chap if ((sc->flags&FWRITE) && (events & (POLLOUT | POLLWRNORM)))
1155 1.43.2.1 chap selrecord(p, &sc->wsel);
1156 1.1 augustss }
1157 1.1 augustss
1158 1.1 augustss splx(s);
1159 1.1 augustss return revents;
1160 1.30 jdolecek }
1161 1.30 jdolecek
1162 1.30 jdolecek static void
1163 1.30 jdolecek filt_midirdetach(struct knote *kn)
1164 1.30 jdolecek {
1165 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
1166 1.30 jdolecek int s;
1167 1.30 jdolecek
1168 1.30 jdolecek s = splaudio();
1169 1.31 christos SLIST_REMOVE(&sc->rsel.sel_klist, kn, knote, kn_selnext);
1170 1.30 jdolecek splx(s);
1171 1.30 jdolecek }
1172 1.30 jdolecek
1173 1.30 jdolecek static int
1174 1.30 jdolecek filt_midiread(struct knote *kn, long hint)
1175 1.30 jdolecek {
1176 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
1177 1.30 jdolecek
1178 1.30 jdolecek /* XXXLUKEM (thorpej): please make sure this is correct. */
1179 1.30 jdolecek
1180 1.30 jdolecek kn->kn_data = sc->inbuf.used;
1181 1.30 jdolecek return (kn->kn_data > 0);
1182 1.30 jdolecek }
1183 1.30 jdolecek
1184 1.30 jdolecek static const struct filterops midiread_filtops =
1185 1.30 jdolecek { 1, NULL, filt_midirdetach, filt_midiread };
1186 1.30 jdolecek
1187 1.30 jdolecek static void
1188 1.30 jdolecek filt_midiwdetach(struct knote *kn)
1189 1.30 jdolecek {
1190 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
1191 1.30 jdolecek int s;
1192 1.30 jdolecek
1193 1.30 jdolecek s = splaudio();
1194 1.31 christos SLIST_REMOVE(&sc->wsel.sel_klist, kn, knote, kn_selnext);
1195 1.30 jdolecek splx(s);
1196 1.30 jdolecek }
1197 1.30 jdolecek
1198 1.30 jdolecek static int
1199 1.30 jdolecek filt_midiwrite(struct knote *kn, long hint)
1200 1.30 jdolecek {
1201 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
1202 1.30 jdolecek
1203 1.30 jdolecek /* XXXLUKEM (thorpej): please make sure this is correct. */
1204 1.30 jdolecek
1205 1.30 jdolecek kn->kn_data = sc->outbuf.usedhigh - sc->outbuf.used;
1206 1.30 jdolecek return (kn->kn_data > 0);
1207 1.30 jdolecek }
1208 1.30 jdolecek
1209 1.30 jdolecek static const struct filterops midiwrite_filtops =
1210 1.30 jdolecek { 1, NULL, filt_midiwdetach, filt_midiwrite };
1211 1.30 jdolecek
1212 1.30 jdolecek int
1213 1.30 jdolecek midikqfilter(dev_t dev, struct knote *kn)
1214 1.30 jdolecek {
1215 1.30 jdolecek int unit = MIDIUNIT(dev);
1216 1.30 jdolecek struct midi_softc *sc = midi_cd.cd_devs[unit];
1217 1.30 jdolecek struct klist *klist;
1218 1.30 jdolecek int s;
1219 1.30 jdolecek
1220 1.30 jdolecek switch (kn->kn_filter) {
1221 1.30 jdolecek case EVFILT_READ:
1222 1.31 christos klist = &sc->rsel.sel_klist;
1223 1.30 jdolecek kn->kn_fop = &midiread_filtops;
1224 1.30 jdolecek break;
1225 1.30 jdolecek
1226 1.30 jdolecek case EVFILT_WRITE:
1227 1.31 christos klist = &sc->wsel.sel_klist;
1228 1.30 jdolecek kn->kn_fop = &midiwrite_filtops;
1229 1.30 jdolecek break;
1230 1.30 jdolecek
1231 1.30 jdolecek default:
1232 1.30 jdolecek return (1);
1233 1.30 jdolecek }
1234 1.30 jdolecek
1235 1.30 jdolecek kn->kn_hook = sc;
1236 1.30 jdolecek
1237 1.30 jdolecek s = splaudio();
1238 1.30 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1239 1.30 jdolecek splx(s);
1240 1.30 jdolecek
1241 1.30 jdolecek return (0);
1242 1.1 augustss }
1243 1.1 augustss
1244 1.1 augustss void
1245 1.22 augustss midi_getinfo(dev_t dev, struct midi_info *mi)
1246 1.1 augustss {
1247 1.1 augustss struct midi_softc *sc;
1248 1.1 augustss
1249 1.17 thorpej sc = device_lookup(&midi_cd, MIDIUNIT(dev));
1250 1.17 thorpej if (sc == NULL)
1251 1.18 tshiozak return;
1252 1.18 tshiozak if (sc->dying)
1253 1.1 augustss return;
1254 1.17 thorpej
1255 1.1 augustss sc->hw_if->getinfo(sc->hw_hdl, mi);
1256 1.4 augustss }
1257 1.4 augustss
1258 1.10 drochner #endif /* NMIDI > 0 */
1259 1.10 drochner
1260 1.10 drochner #if NMIDI > 0 || NMIDIBUS > 0
1261 1.10 drochner
1262 1.22 augustss int audioprint(void *, const char *);
1263 1.4 augustss
1264 1.12 augustss struct device *
1265 1.43.2.1 chap midi_attach_mi(struct midi_hw_if *mhwp, void *hdlp, struct device *dev)
1266 1.4 augustss {
1267 1.4 augustss struct audio_attach_args arg;
1268 1.4 augustss
1269 1.4 augustss #ifdef DIAGNOSTIC
1270 1.4 augustss if (mhwp == NULL) {
1271 1.32 thorpej aprint_error("midi_attach_mi: NULL\n");
1272 1.12 augustss return (0);
1273 1.4 augustss }
1274 1.4 augustss #endif
1275 1.4 augustss arg.type = AUDIODEV_TYPE_MIDI;
1276 1.4 augustss arg.hwif = mhwp;
1277 1.4 augustss arg.hdl = hdlp;
1278 1.12 augustss return (config_found(dev, &arg, audioprint));
1279 1.1 augustss }
1280 1.1 augustss
1281 1.10 drochner #endif /* NMIDI > 0 || NMIDIBUS > 0 */
1282