midi.c revision 1.41.2.4 1 1.41.2.4 yamt /* $NetBSD: midi.c,v 1.41.2.4 2007/09/03 14:33:14 yamt 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.41.2.2 yamt * by Lennart Augustsson (augustss (at) NetBSD.org) and (MIDI FST and Active
9 1.41.2.2 yamt * Sense handling) Chapman Flack (chap (at) NetBSD.org).
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.41.2.4 yamt __KERNEL_RCSID(0, "$NetBSD: midi.c,v 1.41.2.4 2007/09/03 14:33:14 yamt 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.1 augustss #ifdef AUDIO_DEBUG
70 1.1 augustss #define DPRINTF(x) if (mididebug) printf x
71 1.1 augustss #define DPRINTFN(n,x) if (mididebug >= (n)) printf x
72 1.1 augustss int mididebug = 0;
73 1.41.2.2 yamt /*
74 1.41.2.2 yamt * 1: detected protocol errors and buffer overflows
75 1.41.2.2 yamt * 2: probe, attach, detach
76 1.41.2.2 yamt * 3: open, close
77 1.41.2.2 yamt * 4: data received except realtime
78 1.41.2.2 yamt * 5: ioctl
79 1.41.2.2 yamt * 6: read, write, poll
80 1.41.2.2 yamt * 7: data transmitted
81 1.41.2.2 yamt * 8: uiomoves, synchronization
82 1.41.2.2 yamt * 9: realtime data received
83 1.41.2.2 yamt */
84 1.1 augustss #else
85 1.1 augustss #define DPRINTF(x)
86 1.1 augustss #define DPRINTFN(n,x)
87 1.1 augustss #endif
88 1.1 augustss
89 1.41.2.2 yamt static struct simplelock hwif_register_lock = SIMPLELOCK_INITIALIZER;
90 1.41.2.2 yamt static struct midi_softc *hwif_softc = NULL;
91 1.1 augustss
92 1.22 augustss void midi_in(void *, int);
93 1.22 augustss void midi_out(void *);
94 1.41.2.2 yamt int midi_poll_out(struct midi_softc *);
95 1.41.2.2 yamt int midi_intr_out(struct midi_softc *);
96 1.41.2.2 yamt int midi_msg_out(struct midi_softc *,
97 1.41.2.2 yamt u_char **, u_char **, u_char **, u_char **);
98 1.41.2.2 yamt int midi_start_output(struct midi_softc *);
99 1.41.2.2 yamt int midi_sleep_timo(int *, const char *, int, struct simplelock *);
100 1.41.2.2 yamt int midi_sleep(int *, const char *, struct simplelock *);
101 1.22 augustss void midi_wakeup(int *);
102 1.22 augustss void midi_initbuf(struct midi_buffer *);
103 1.41.2.2 yamt void midi_xmt_asense(void *);
104 1.41.2.2 yamt void midi_rcv_asense(void *);
105 1.41.2.3 yamt void midi_softintr_rd(void *);
106 1.41.2.3 yamt void midi_softintr_wr(void *);
107 1.22 augustss
108 1.22 augustss int midiprobe(struct device *, struct cfdata *, void *);
109 1.22 augustss void midiattach(struct device *, struct device *, void *);
110 1.22 augustss int mididetach(struct device *, int);
111 1.22 augustss int midiactivate(struct device *, enum devact);
112 1.1 augustss
113 1.25 gehenna dev_type_open(midiopen);
114 1.25 gehenna dev_type_close(midiclose);
115 1.25 gehenna dev_type_read(midiread);
116 1.25 gehenna dev_type_write(midiwrite);
117 1.25 gehenna dev_type_ioctl(midiioctl);
118 1.25 gehenna dev_type_poll(midipoll);
119 1.30 jdolecek dev_type_kqfilter(midikqfilter);
120 1.25 gehenna
121 1.25 gehenna const struct cdevsw midi_cdevsw = {
122 1.25 gehenna midiopen, midiclose, midiread, midiwrite, midiioctl,
123 1.41.2.2 yamt nostop, notty, midipoll, nommap, midikqfilter, D_OTHER,
124 1.25 gehenna };
125 1.25 gehenna
126 1.28 thorpej CFATTACH_DECL(midi, sizeof(struct midi_softc),
127 1.29 thorpej midiprobe, midiattach, mididetach, midiactivate);
128 1.1 augustss
129 1.41.2.2 yamt #define MIDI_XMT_ASENSE_PERIOD mstohz(275)
130 1.41.2.2 yamt #define MIDI_RCV_ASENSE_PERIOD mstohz(300)
131 1.4 augustss
132 1.1 augustss extern struct cfdriver midi_cd;
133 1.1 augustss
134 1.1 augustss int
135 1.41.2.2 yamt midiprobe(struct device *parent, struct cfdata *match,
136 1.41.2.2 yamt void *aux)
137 1.1 augustss {
138 1.1 augustss struct audio_attach_args *sa = aux;
139 1.1 augustss
140 1.41.2.2 yamt 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.38 yamt const struct midi_hw_if *hwp = sa->hwif;
151 1.1 augustss void *hdlp = sa->hdl;
152 1.1 augustss
153 1.41.2.2 yamt 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.41.2.2 yamt 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.41.2.1 yamt mn = device_unit(self);
205 1.18 tshiozak vdevgone(maj, mn, mn, VCHR);
206 1.41.2.2 yamt
207 1.41.2.2 yamt if ( !(sc->props & MIDI_PROP_NO_OUTPUT) ) {
208 1.41.2.2 yamt evcnt_detach(&sc->xmt.bytesDiscarded);
209 1.41.2.2 yamt evcnt_detach(&sc->xmt.incompleteMessages);
210 1.41.2.2 yamt }
211 1.41.2.2 yamt if ( sc->props & MIDI_PROP_CAN_INPUT ) {
212 1.41.2.2 yamt evcnt_detach(&sc->rcv.bytesDiscarded);
213 1.41.2.2 yamt evcnt_detach(&sc->rcv.incompleteMessages);
214 1.41.2.2 yamt }
215 1.18 tshiozak
216 1.41.2.3 yamt if (sc->sih_rd != NULL) {
217 1.41.2.3 yamt softintr_disestablish(sc->sih_rd);
218 1.41.2.3 yamt sc->sih_rd = NULL;
219 1.41.2.3 yamt }
220 1.41.2.3 yamt if (sc->sih_wr != NULL) {
221 1.41.2.3 yamt softintr_disestablish(sc->sih_wr);
222 1.41.2.3 yamt sc->sih_wr = NULL;
223 1.41.2.3 yamt }
224 1.41.2.3 yamt
225 1.18 tshiozak return (0);
226 1.18 tshiozak }
227 1.18 tshiozak
228 1.2 augustss void
229 1.22 augustss midi_attach(struct midi_softc *sc, struct device *parent)
230 1.2 augustss {
231 1.2 augustss struct midi_info mi;
232 1.41.2.2 yamt int s;
233 1.2 augustss
234 1.41.2.4 yamt callout_init(&sc->xmt_asense_co, 0);
235 1.41.2.4 yamt callout_init(&sc->rcv_asense_co, 0);
236 1.41.2.2 yamt callout_setfunc(&sc->xmt_asense_co, midi_xmt_asense, sc);
237 1.41.2.2 yamt callout_setfunc(&sc->rcv_asense_co, midi_rcv_asense, sc);
238 1.41.2.2 yamt simple_lock_init(&sc->out_lock);
239 1.41.2.2 yamt simple_lock_init(&sc->in_lock);
240 1.41.2.2 yamt sc->dying = 0;
241 1.1 augustss sc->isopen = 0;
242 1.1 augustss
243 1.2 augustss sc->sc_dev = parent;
244 1.41.2.3 yamt
245 1.41.2.3 yamt sc->sih_rd = softintr_establish(IPL_SOFTSERIAL, midi_softintr_rd, sc);
246 1.41.2.3 yamt sc->sih_wr = softintr_establish(IPL_SOFTSERIAL, midi_softintr_wr, sc);
247 1.41.2.3 yamt
248 1.41.2.2 yamt s = splaudio();
249 1.41.2.2 yamt simple_lock(&hwif_register_lock);
250 1.41.2.2 yamt hwif_softc = sc;
251 1.2 augustss sc->hw_if->getinfo(sc->hw_hdl, &mi);
252 1.41.2.2 yamt hwif_softc = NULL;
253 1.41.2.2 yamt simple_unlock(&hwif_register_lock);
254 1.41.2.2 yamt splx(s);
255 1.41.2.2 yamt
256 1.1 augustss sc->props = mi.props;
257 1.41.2.2 yamt
258 1.41.2.2 yamt if ( !(sc->props & MIDI_PROP_NO_OUTPUT) ) {
259 1.41.2.2 yamt evcnt_attach_dynamic(&sc->xmt.bytesDiscarded,
260 1.41.2.2 yamt EVCNT_TYPE_MISC, NULL,
261 1.41.2.2 yamt sc->dev.dv_xname, "xmt bytes discarded");
262 1.41.2.2 yamt evcnt_attach_dynamic(&sc->xmt.incompleteMessages,
263 1.41.2.2 yamt EVCNT_TYPE_MISC, NULL,
264 1.41.2.2 yamt sc->dev.dv_xname, "xmt incomplete msgs");
265 1.41.2.2 yamt }
266 1.41.2.2 yamt if ( sc->props & MIDI_PROP_CAN_INPUT ) {
267 1.41.2.2 yamt evcnt_attach_dynamic(&sc->rcv.bytesDiscarded,
268 1.41.2.2 yamt EVCNT_TYPE_MISC, NULL,
269 1.41.2.2 yamt sc->dev.dv_xname, "rcv bytes discarded");
270 1.41.2.2 yamt evcnt_attach_dynamic(&sc->rcv.incompleteMessages,
271 1.41.2.2 yamt EVCNT_TYPE_MISC, NULL,
272 1.41.2.2 yamt sc->dev.dv_xname, "rcv incomplete msgs");
273 1.41.2.2 yamt }
274 1.41.2.2 yamt
275 1.41.2.2 yamt printf(": %s%s\n", mi.name,
276 1.41.2.2 yamt (sc->props & (MIDI_PROP_OUT_INTR|MIDI_PROP_NO_OUTPUT)) ?
277 1.41.2.2 yamt "" : " (CPU-intensive output)");
278 1.41.2.2 yamt }
279 1.41.2.2 yamt
280 1.41.2.2 yamt void midi_register_hw_if_ext(struct midi_hw_if_ext *exthw) {
281 1.41.2.2 yamt if ( hwif_softc != NULL ) /* ignore calls resulting from non-init */
282 1.41.2.2 yamt hwif_softc->hw_if_ext = exthw; /* uses of getinfo */
283 1.1 augustss }
284 1.1 augustss
285 1.1 augustss int
286 1.22 augustss midi_unit_count(void)
287 1.1 augustss {
288 1.41.2.2 yamt int i;
289 1.41.2.2 yamt for ( i = 0; i < midi_cd.cd_ndevs; ++i )
290 1.41.2.2 yamt if ( NULL == midi_cd.cd_devs[i] )
291 1.41.2.2 yamt break;
292 1.41.2.2 yamt return i;
293 1.1 augustss }
294 1.1 augustss
295 1.1 augustss void
296 1.22 augustss midi_initbuf(struct midi_buffer *mb)
297 1.1 augustss {
298 1.41.2.2 yamt mb->idx_producerp = mb->idx_consumerp = mb->idx;
299 1.41.2.2 yamt mb->buf_producerp = mb->buf_consumerp = mb->buf;
300 1.1 augustss }
301 1.41.2.2 yamt #define PACK_MB_IDX(cat,len) (((cat)<<4)|(len))
302 1.41.2.2 yamt #define MB_IDX_CAT(idx) ((idx)>>4)
303 1.41.2.2 yamt #define MB_IDX_LEN(idx) ((idx)&0xf)
304 1.1 augustss
305 1.2 augustss int
306 1.41.2.2 yamt midi_sleep_timo(int *chan, const char *label, int timo, struct simplelock *lk)
307 1.1 augustss {
308 1.1 augustss int st;
309 1.1 augustss
310 1.1 augustss if (!label)
311 1.1 augustss label = "midi";
312 1.1 augustss
313 1.41.2.2 yamt DPRINTFN(8, ("midi_sleep_timo: %p %s %d\n", chan, label, timo));
314 1.1 augustss *chan = 1;
315 1.41.2.2 yamt st = ltsleep(chan, PWAIT | PCATCH, label, timo, lk);
316 1.1 augustss *chan = 0;
317 1.1 augustss #ifdef MIDI_DEBUG
318 1.1 augustss if (st != 0)
319 1.1 augustss printf("midi_sleep: %d\n", st);
320 1.1 augustss #endif
321 1.1 augustss return st;
322 1.1 augustss }
323 1.1 augustss
324 1.2 augustss int
325 1.41.2.2 yamt midi_sleep(int *chan, const char *label, struct simplelock *lk)
326 1.1 augustss {
327 1.41.2.2 yamt return midi_sleep_timo(chan, label, 0, lk);
328 1.1 augustss }
329 1.1 augustss
330 1.2 augustss void
331 1.22 augustss midi_wakeup(int *chan)
332 1.1 augustss {
333 1.1 augustss if (*chan) {
334 1.41.2.2 yamt DPRINTFN(8, ("midi_wakeup: %p\n", chan));
335 1.1 augustss wakeup(chan);
336 1.1 augustss *chan = 0;
337 1.1 augustss }
338 1.1 augustss }
339 1.1 augustss
340 1.41.2.2 yamt /* in midivar.h:
341 1.41.2.2 yamt #define MIDI_CAT_DATA 0
342 1.41.2.2 yamt #define MIDI_CAT_STATUS1 1
343 1.41.2.2 yamt #define MIDI_CAT_STATUS2 2
344 1.41.2.2 yamt #define MIDI_CAT_COMMON 3
345 1.41.2.2 yamt */
346 1.41.2.2 yamt static char const midi_cats[] = "\0\0\0\0\0\0\0\0\2\2\2\2\1\1\2\3";
347 1.41.2.2 yamt #define MIDI_CAT(d) (midi_cats[((d)>>4)&15])
348 1.41.2.2 yamt #define FST_RETURN(offp,endp,ret) \
349 1.41.2.2 yamt return (s->pos=s->msg+(offp)), (s->end=s->msg+(endp)), (ret)
350 1.41.2.2 yamt
351 1.41.2.2 yamt enum fst_ret { FST_CHN, FST_CHV, FST_COM, FST_SYX, FST_RT, FST_MORE, FST_ERR,
352 1.41.2.2 yamt FST_HUH, FST_SXP };
353 1.41.2.2 yamt enum fst_form { FST_CANON, FST_COMPR, FST_VCOMP };
354 1.41.2.2 yamt static struct {
355 1.41.2.2 yamt int off;
356 1.41.2.2 yamt enum fst_ret tag;
357 1.41.2.2 yamt } const midi_forms[] = {
358 1.41.2.2 yamt [FST_CANON] = { .off=0, .tag=FST_CHN },
359 1.41.2.2 yamt [FST_COMPR] = { .off=1, .tag=FST_CHN },
360 1.41.2.2 yamt [FST_VCOMP] = { .off=0, .tag=FST_CHV }
361 1.41.2.2 yamt };
362 1.41.2.2 yamt #define FST_CRETURN(endp) \
363 1.41.2.2 yamt FST_RETURN(midi_forms[form].off,endp,midi_forms[form].tag)
364 1.41.2.2 yamt
365 1.41.2.2 yamt /*
366 1.41.2.2 yamt * A MIDI finite state transducer suitable for receiving or transmitting. It
367 1.41.2.2 yamt * will accept correct MIDI input that uses, doesn't use, or sometimes uses the
368 1.41.2.2 yamt * 'running status' compression technique, and transduce it to fully expanded
369 1.41.2.2 yamt * (form=FST_CANON) or fully compressed (form=FST_COMPR or FST_VCOMP) form.
370 1.41.2.2 yamt *
371 1.41.2.2 yamt * Returns FST_MORE if a complete message has not been parsed yet (SysEx
372 1.41.2.2 yamt * messages are the exception), FST_ERR or FST_HUH if the input does not
373 1.41.2.2 yamt * conform to the protocol, or FST_CHN (channel messages), FST_COM (System
374 1.41.2.2 yamt * Common messages), FST_RT (System Real-Time messages), or FST_SYX (System
375 1.41.2.2 yamt * Exclusive) to broadly categorize the message parsed. s->pos and s->end
376 1.41.2.2 yamt * locate the parsed message; while (s->pos<s->end) putchar(*(s->pos++));
377 1.41.2.2 yamt * would output it.
378 1.41.2.2 yamt *
379 1.41.2.2 yamt * FST_HUH means the character c wasn't valid in the original state, but the
380 1.41.2.2 yamt * state has now been reset to START and the caller should try again passing
381 1.41.2.2 yamt * the same c. FST_ERR means c isn't valid in the start state; the caller
382 1.41.2.2 yamt * should kiss it goodbye and continue to try successive characters from the
383 1.41.2.2 yamt * input until something other than FST_ERR or FST_HUH is returned, at which
384 1.41.2.2 yamt * point things are resynchronized.
385 1.41.2.2 yamt *
386 1.41.2.2 yamt * A FST_SYX return means that between pos and end are from 1 to 3
387 1.41.2.2 yamt * bytes of a system exclusive message. A SysEx message will be delivered in
388 1.41.2.2 yamt * one or more chunks of that form, where the first begins with 0xf0 and the
389 1.41.2.2 yamt * last (which is the only one that might have length < 3) ends with 0xf7.
390 1.41.2.2 yamt *
391 1.41.2.2 yamt * Messages corrupted by a protocol error are discarded and won't be seen at
392 1.41.2.2 yamt * all; again SysEx is the exception, as one or more chunks of it may already
393 1.41.2.2 yamt * have been parsed.
394 1.41.2.2 yamt *
395 1.41.2.2 yamt * For FST_CHN messages, s->msg[0] always contains the status byte even if
396 1.41.2.2 yamt * FST_COMPR form was requested (pos then points to msg[1]). That way, the
397 1.41.2.2 yamt * caller can always identify the exact message if there is a need to do so.
398 1.41.2.2 yamt * For all other message types except FST_SYX, the status byte is at *pos
399 1.41.2.2 yamt * (which may not necessarily be msg[0]!). There is only one SysEx status
400 1.41.2.2 yamt * byte, so the return value FST_SYX is sufficient to identify it.
401 1.41.2.2 yamt *
402 1.41.2.2 yamt * To simplify some use cases, compression can also be requested with
403 1.41.2.2 yamt * form=FST_VCOMP. In this form a compressible channel message is indicated
404 1.41.2.2 yamt * by returning a classification of FST_CHV instead of FST_CHN, and pos points
405 1.41.2.2 yamt * to the status byte rather than being advanced past it. If the caller in this
406 1.41.2.2 yamt * case saves the bytes from pos to end, it will have saved the entire message,
407 1.41.2.2 yamt * and can act on the FST_CHV tag to drop the first byte later. In this form,
408 1.41.2.2 yamt * unlike FST_CANON, hidden note-off (i.e. note-on with velocity 0) may occur.
409 1.41.2.2 yamt *
410 1.41.2.2 yamt * Two obscure points in the MIDI protocol complicate things further, both to
411 1.41.2.2 yamt * do with the EndSysEx code, 0xf7. First, this code is permitted (and
412 1.41.2.2 yamt * meaningless) outside of a System Exclusive message, anywhere a status byte
413 1.41.2.2 yamt * could appear. Second, it is allowed to be absent at the end of a System
414 1.41.2.2 yamt * Exclusive message (!) - any status byte at all (non-realtime) is allowed to
415 1.41.2.2 yamt * terminate the message. Both require accomodation in the interface to
416 1.41.2.2 yamt * midi_fst's caller. A stray 0xf7 should be ignored BUT should count as a
417 1.41.2.2 yamt * message received for purposes of Active Sense timeout; the case is
418 1.41.2.2 yamt * represented by a return of FST_COM with a length of zero (pos == end). A
419 1.41.2.2 yamt * status byte other than 0xf7 during a system exclusive message will cause an
420 1.41.2.2 yamt * FST_SXP (sysex plus) return; the bytes from pos to end are the end of the
421 1.41.2.2 yamt * system exclusive message, and after handling those the caller should call
422 1.41.2.2 yamt * midi_fst again with the same input byte.
423 1.41.2.2 yamt *
424 1.41.2.2 yamt * midi(4) will never produce either such form of rubbish.
425 1.41.2.2 yamt */
426 1.41.2.2 yamt static enum fst_ret
427 1.41.2.2 yamt midi_fst(struct midi_state *s, u_char c, enum fst_form form)
428 1.41.2.2 yamt {
429 1.41.2.2 yamt int syxpos = 0;
430 1.41.2.2 yamt
431 1.41.2.2 yamt if ( c >= 0xf8 ) { /* All realtime messages bypass state machine */
432 1.41.2.2 yamt if ( c == 0xf9 || c == 0xfd ) {
433 1.41.2.2 yamt DPRINTF( ("midi_fst: s=%p c=0x%02x undefined\n",
434 1.41.2.2 yamt s, c));
435 1.41.2.2 yamt s->bytesDiscarded.ev_count++;
436 1.41.2.2 yamt return FST_ERR;
437 1.41.2.2 yamt }
438 1.41.2.2 yamt DPRINTFN(9, ("midi_fst: s=%p System Real-Time data=0x%02x\n",
439 1.41.2.2 yamt s, c));
440 1.41.2.2 yamt s->msg[2] = c;
441 1.41.2.2 yamt FST_RETURN(2,3,FST_RT);
442 1.41.2.2 yamt }
443 1.41.2.2 yamt
444 1.41.2.2 yamt DPRINTFN(4, ("midi_fst: s=%p data=0x%02x state=%d\n",
445 1.41.2.2 yamt s, c, s->state));
446 1.41.2.2 yamt
447 1.41.2.2 yamt switch ( s->state | MIDI_CAT(c) ) { /* break ==> return FST_MORE */
448 1.41.2.2 yamt
449 1.41.2.2 yamt case MIDI_IN_START | MIDI_CAT_COMMON:
450 1.41.2.2 yamt case MIDI_IN_RUN1_1 | MIDI_CAT_COMMON:
451 1.41.2.2 yamt case MIDI_IN_RUN2_2 | MIDI_CAT_COMMON:
452 1.41.2.2 yamt case MIDI_IN_RXX2_2 | MIDI_CAT_COMMON:
453 1.41.2.2 yamt s->msg[0] = c;
454 1.41.2.2 yamt switch ( c ) {
455 1.41.2.2 yamt case 0xf0: s->state = MIDI_IN_SYX1_3; break;
456 1.41.2.2 yamt case 0xf1: s->state = MIDI_IN_COM0_1; break;
457 1.41.2.2 yamt case 0xf2: s->state = MIDI_IN_COM0_2; break;
458 1.41.2.2 yamt case 0xf3: s->state = MIDI_IN_COM0_1; break;
459 1.41.2.2 yamt case 0xf6: s->state = MIDI_IN_START; FST_RETURN(0,1,FST_COM);
460 1.41.2.2 yamt case 0xf7: s->state = MIDI_IN_START; FST_RETURN(0,0,FST_COM);
461 1.41.2.2 yamt default: goto protocol_violation;
462 1.41.2.2 yamt }
463 1.41.2.2 yamt break;
464 1.41.2.2 yamt
465 1.41.2.2 yamt case MIDI_IN_RUN1_1 | MIDI_CAT_STATUS1:
466 1.41.2.2 yamt if ( c == s->msg[0] ) {
467 1.41.2.2 yamt s->state = MIDI_IN_RNX0_1;
468 1.41.2.2 yamt break;
469 1.41.2.2 yamt }
470 1.41.2.2 yamt /* FALLTHROUGH */
471 1.41.2.2 yamt case MIDI_IN_RUN2_2 | MIDI_CAT_STATUS1:
472 1.41.2.2 yamt case MIDI_IN_RXX2_2 | MIDI_CAT_STATUS1:
473 1.41.2.2 yamt case MIDI_IN_START | MIDI_CAT_STATUS1:
474 1.41.2.2 yamt s->state = MIDI_IN_RUN0_1;
475 1.41.2.2 yamt s->msg[0] = c;
476 1.41.2.2 yamt break;
477 1.41.2.2 yamt
478 1.41.2.2 yamt case MIDI_IN_RUN2_2 | MIDI_CAT_STATUS2:
479 1.41.2.2 yamt case MIDI_IN_RXX2_2 | MIDI_CAT_STATUS2:
480 1.41.2.2 yamt if ( c == s->msg[0] ) {
481 1.41.2.2 yamt s->state = MIDI_IN_RNX0_2;
482 1.41.2.2 yamt break;
483 1.41.2.2 yamt }
484 1.41.2.2 yamt if ( (c ^ s->msg[0]) == 0x10 && (c & 0xe0) == 0x80 ) {
485 1.41.2.2 yamt s->state = MIDI_IN_RXX0_2;
486 1.41.2.2 yamt s->msg[0] = c;
487 1.41.2.2 yamt break;
488 1.41.2.2 yamt }
489 1.41.2.2 yamt /* FALLTHROUGH */
490 1.41.2.2 yamt case MIDI_IN_RUN1_1 | MIDI_CAT_STATUS2:
491 1.41.2.2 yamt case MIDI_IN_START | MIDI_CAT_STATUS2:
492 1.41.2.2 yamt s->state = MIDI_IN_RUN0_2;
493 1.41.2.2 yamt s->msg[0] = c;
494 1.41.2.2 yamt break;
495 1.41.2.2 yamt
496 1.41.2.2 yamt case MIDI_IN_COM0_1 | MIDI_CAT_DATA:
497 1.41.2.2 yamt s->state = MIDI_IN_START;
498 1.41.2.2 yamt s->msg[1] = c;
499 1.41.2.2 yamt FST_RETURN(0,2,FST_COM);
500 1.41.2.2 yamt
501 1.41.2.2 yamt case MIDI_IN_COM0_2 | MIDI_CAT_DATA:
502 1.41.2.2 yamt s->state = MIDI_IN_COM1_2;
503 1.41.2.2 yamt s->msg[1] = c;
504 1.41.2.2 yamt break;
505 1.41.2.2 yamt
506 1.41.2.2 yamt case MIDI_IN_COM1_2 | MIDI_CAT_DATA:
507 1.41.2.2 yamt s->state = MIDI_IN_START;
508 1.41.2.2 yamt s->msg[2] = c;
509 1.41.2.2 yamt FST_RETURN(0,3,FST_COM);
510 1.41.2.2 yamt
511 1.41.2.2 yamt case MIDI_IN_RUN0_1 | MIDI_CAT_DATA:
512 1.41.2.2 yamt s->state = MIDI_IN_RUN1_1;
513 1.41.2.2 yamt s->msg[1] = c;
514 1.41.2.2 yamt FST_RETURN(0,2,FST_CHN);
515 1.41.2.2 yamt
516 1.41.2.2 yamt case MIDI_IN_RUN1_1 | MIDI_CAT_DATA:
517 1.41.2.2 yamt case MIDI_IN_RNX0_1 | MIDI_CAT_DATA:
518 1.41.2.2 yamt s->state = MIDI_IN_RUN1_1;
519 1.41.2.2 yamt s->msg[1] = c;
520 1.41.2.2 yamt FST_CRETURN(2);
521 1.41.2.2 yamt
522 1.41.2.2 yamt case MIDI_IN_RUN0_2 | MIDI_CAT_DATA:
523 1.41.2.2 yamt s->state = MIDI_IN_RUN1_2;
524 1.41.2.2 yamt s->msg[1] = c;
525 1.41.2.2 yamt break;
526 1.41.2.2 yamt
527 1.41.2.2 yamt case MIDI_IN_RUN1_2 | MIDI_CAT_DATA:
528 1.41.2.2 yamt if ( FST_CANON == form && 0 == c && (s->msg[0]&0xf0) == 0x90 ) {
529 1.41.2.2 yamt s->state = MIDI_IN_RXX2_2;
530 1.41.2.2 yamt s->msg[0] ^= 0x10;
531 1.41.2.2 yamt s->msg[2] = 64;
532 1.41.2.2 yamt } else {
533 1.41.2.2 yamt s->state = MIDI_IN_RUN2_2;
534 1.41.2.2 yamt s->msg[2] = c;
535 1.41.2.2 yamt }
536 1.41.2.2 yamt FST_RETURN(0,3,FST_CHN);
537 1.41.2.2 yamt
538 1.41.2.2 yamt case MIDI_IN_RUN2_2 | MIDI_CAT_DATA:
539 1.41.2.2 yamt s->state = MIDI_IN_RNX1_2;
540 1.41.2.2 yamt s->msg[1] = c;
541 1.41.2.2 yamt break;
542 1.41.2.2 yamt
543 1.41.2.2 yamt case MIDI_IN_RXX2_2 | MIDI_CAT_DATA:
544 1.41.2.2 yamt s->state = MIDI_IN_RXX1_2;
545 1.41.2.2 yamt s->msg[0] ^= 0x10;
546 1.41.2.2 yamt s->msg[1] = c;
547 1.41.2.2 yamt break;
548 1.41.2.2 yamt
549 1.41.2.2 yamt case MIDI_IN_RNX0_2 | MIDI_CAT_DATA:
550 1.41.2.2 yamt s->state = MIDI_IN_RNY1_2;
551 1.41.2.2 yamt s->msg[1] = c;
552 1.41.2.2 yamt break;
553 1.41.2.2 yamt
554 1.41.2.2 yamt case MIDI_IN_RXX0_2 | MIDI_CAT_DATA:
555 1.41.2.2 yamt s->state = MIDI_IN_RXY1_2;
556 1.41.2.2 yamt s->msg[1] = c;
557 1.41.2.2 yamt break;
558 1.41.2.2 yamt
559 1.41.2.2 yamt case MIDI_IN_RNX1_2 | MIDI_CAT_DATA:
560 1.41.2.2 yamt case MIDI_IN_RNY1_2 | MIDI_CAT_DATA:
561 1.41.2.2 yamt if ( FST_CANON == form && 0 == c && (s->msg[0]&0xf0) == 0x90 ) {
562 1.41.2.2 yamt s->state = MIDI_IN_RXX2_2;
563 1.41.2.2 yamt s->msg[0] ^= 0x10;
564 1.41.2.2 yamt s->msg[2] = 64;
565 1.41.2.2 yamt FST_RETURN(0,3,FST_CHN);
566 1.41.2.2 yamt }
567 1.41.2.2 yamt s->state = MIDI_IN_RUN2_2;
568 1.41.2.2 yamt s->msg[2] = c;
569 1.41.2.2 yamt FST_CRETURN(3);
570 1.41.2.2 yamt
571 1.41.2.2 yamt case MIDI_IN_RXX1_2 | MIDI_CAT_DATA:
572 1.41.2.2 yamt case MIDI_IN_RXY1_2 | MIDI_CAT_DATA:
573 1.41.2.2 yamt if ( ( 0 == c && (s->msg[0]&0xf0) == 0x90)
574 1.41.2.2 yamt || (64 == c && (s->msg[0]&0xf0) == 0x80
575 1.41.2.2 yamt && FST_CANON != form) ) {
576 1.41.2.2 yamt s->state = MIDI_IN_RXX2_2;
577 1.41.2.2 yamt s->msg[0] ^= 0x10;
578 1.41.2.2 yamt s->msg[2] = 64 - c;
579 1.41.2.2 yamt FST_CRETURN(3);
580 1.41.2.2 yamt }
581 1.41.2.2 yamt s->state = MIDI_IN_RUN2_2;
582 1.41.2.2 yamt s->msg[2] = c;
583 1.41.2.2 yamt FST_RETURN(0,3,FST_CHN);
584 1.41.2.2 yamt
585 1.41.2.2 yamt case MIDI_IN_SYX1_3 | MIDI_CAT_DATA:
586 1.41.2.2 yamt s->state = MIDI_IN_SYX2_3;
587 1.41.2.2 yamt s->msg[1] = c;
588 1.41.2.2 yamt break;
589 1.41.2.2 yamt
590 1.41.2.2 yamt case MIDI_IN_SYX2_3 | MIDI_CAT_DATA:
591 1.41.2.2 yamt s->state = MIDI_IN_SYX0_3;
592 1.41.2.2 yamt s->msg[2] = c;
593 1.41.2.2 yamt FST_RETURN(0,3,FST_SYX);
594 1.41.2.2 yamt
595 1.41.2.2 yamt case MIDI_IN_SYX0_3 | MIDI_CAT_DATA:
596 1.41.2.2 yamt s->state = MIDI_IN_SYX1_3;
597 1.41.2.2 yamt s->msg[0] = c;
598 1.41.2.2 yamt break;
599 1.41.2.2 yamt
600 1.41.2.2 yamt case MIDI_IN_SYX2_3 | MIDI_CAT_COMMON:
601 1.41.2.2 yamt case MIDI_IN_SYX2_3 | MIDI_CAT_STATUS1:
602 1.41.2.2 yamt case MIDI_IN_SYX2_3 | MIDI_CAT_STATUS2:
603 1.41.2.2 yamt ++ syxpos;
604 1.41.2.2 yamt /* FALLTHROUGH */
605 1.41.2.2 yamt case MIDI_IN_SYX1_3 | MIDI_CAT_COMMON:
606 1.41.2.2 yamt case MIDI_IN_SYX1_3 | MIDI_CAT_STATUS1:
607 1.41.2.2 yamt case MIDI_IN_SYX1_3 | MIDI_CAT_STATUS2:
608 1.41.2.2 yamt ++ syxpos;
609 1.41.2.2 yamt /* FALLTHROUGH */
610 1.41.2.2 yamt case MIDI_IN_SYX0_3 | MIDI_CAT_COMMON:
611 1.41.2.2 yamt case MIDI_IN_SYX0_3 | MIDI_CAT_STATUS1:
612 1.41.2.2 yamt case MIDI_IN_SYX0_3 | MIDI_CAT_STATUS2:
613 1.41.2.2 yamt s->state = MIDI_IN_START;
614 1.41.2.2 yamt if ( c == 0xf7 ) {
615 1.41.2.2 yamt s->msg[syxpos] = c;
616 1.41.2.2 yamt FST_RETURN(0,1+syxpos,FST_SYX);
617 1.41.2.2 yamt }
618 1.41.2.2 yamt s->msg[syxpos] = 0xf7;
619 1.41.2.2 yamt FST_RETURN(0,1+syxpos,FST_SXP);
620 1.41.2.2 yamt
621 1.41.2.2 yamt default:
622 1.41.2.2 yamt protocol_violation:
623 1.41.2.2 yamt DPRINTF(("midi_fst: unexpected %#02x in state %u\n",
624 1.41.2.2 yamt c, s->state));
625 1.41.2.2 yamt switch ( s->state ) {
626 1.41.2.2 yamt case MIDI_IN_RUN1_1: /* can only get here by seeing an */
627 1.41.2.2 yamt case MIDI_IN_RUN2_2: /* INVALID System Common message */
628 1.41.2.2 yamt case MIDI_IN_RXX2_2:
629 1.41.2.2 yamt s->state = MIDI_IN_START;
630 1.41.2.2 yamt /* FALLTHROUGH */
631 1.41.2.2 yamt case MIDI_IN_START:
632 1.41.2.2 yamt s->bytesDiscarded.ev_count++;
633 1.41.2.2 yamt return FST_ERR;
634 1.41.2.2 yamt case MIDI_IN_COM1_2:
635 1.41.2.2 yamt case MIDI_IN_RUN1_2:
636 1.41.2.2 yamt case MIDI_IN_RNY1_2:
637 1.41.2.2 yamt case MIDI_IN_RXY1_2:
638 1.41.2.2 yamt s->bytesDiscarded.ev_count++;
639 1.41.2.2 yamt /* FALLTHROUGH */
640 1.41.2.2 yamt case MIDI_IN_COM0_1:
641 1.41.2.2 yamt case MIDI_IN_RUN0_1:
642 1.41.2.2 yamt case MIDI_IN_RNX0_1:
643 1.41.2.2 yamt case MIDI_IN_COM0_2:
644 1.41.2.2 yamt case MIDI_IN_RUN0_2:
645 1.41.2.2 yamt case MIDI_IN_RNX0_2:
646 1.41.2.2 yamt case MIDI_IN_RXX0_2:
647 1.41.2.2 yamt case MIDI_IN_RNX1_2:
648 1.41.2.2 yamt case MIDI_IN_RXX1_2:
649 1.41.2.2 yamt s->bytesDiscarded.ev_count++;
650 1.41.2.2 yamt s->incompleteMessages.ev_count++;
651 1.41.2.2 yamt break;
652 1.41.2.2 yamt #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
653 1.41.2.2 yamt default:
654 1.41.2.2 yamt printf("midi_fst: mishandled %#02x(%u) in state %u?!\n",
655 1.41.2.2 yamt c, MIDI_CAT(c), s->state);
656 1.41.2.2 yamt #endif
657 1.41.2.2 yamt }
658 1.41.2.2 yamt s->state = MIDI_IN_START;
659 1.41.2.2 yamt return FST_HUH;
660 1.41.2.2 yamt }
661 1.41.2.2 yamt return FST_MORE;
662 1.41.2.2 yamt }
663 1.1 augustss
664 1.1 augustss void
665 1.41.2.3 yamt midi_softintr_rd(void *cookie)
666 1.41.2.3 yamt {
667 1.41.2.3 yamt struct midi_softc *sc = cookie;
668 1.41.2.3 yamt struct proc *p;
669 1.41.2.3 yamt
670 1.41.2.3 yamt if (sc->async != NULL) {
671 1.41.2.3 yamt mutex_enter(&proclist_mutex);
672 1.41.2.3 yamt if ((p = sc->async) != NULL)
673 1.41.2.3 yamt psignal(p, SIGIO);
674 1.41.2.3 yamt mutex_exit(&proclist_mutex);
675 1.41.2.3 yamt }
676 1.41.2.3 yamt midi_wakeup(&sc->rchan);
677 1.41.2.3 yamt selnotify(&sc->rsel, 0); /* filter will spin if locked */
678 1.41.2.3 yamt }
679 1.41.2.3 yamt
680 1.41.2.3 yamt void
681 1.41.2.3 yamt midi_softintr_wr(void *cookie)
682 1.41.2.3 yamt {
683 1.41.2.3 yamt struct midi_softc *sc = cookie;
684 1.41.2.3 yamt struct proc *p;
685 1.41.2.3 yamt
686 1.41.2.3 yamt if (sc->async != NULL) {
687 1.41.2.3 yamt mutex_enter(&proclist_mutex);
688 1.41.2.3 yamt if ((p = sc->async) != NULL)
689 1.41.2.3 yamt psignal(p, SIGIO);
690 1.41.2.3 yamt mutex_exit(&proclist_mutex);
691 1.41.2.3 yamt }
692 1.41.2.3 yamt midi_wakeup(&sc->wchan);
693 1.41.2.3 yamt selnotify(&sc->wsel, 0); /* filter will spin if locked */
694 1.41.2.3 yamt }
695 1.41.2.3 yamt
696 1.41.2.3 yamt void
697 1.22 augustss midi_in(void *addr, int data)
698 1.1 augustss {
699 1.1 augustss struct midi_softc *sc = addr;
700 1.1 augustss struct midi_buffer *mb = &sc->inbuf;
701 1.1 augustss int i;
702 1.41.2.2 yamt int count;
703 1.41.2.2 yamt enum fst_ret got;
704 1.41.2.2 yamt int s; /* hw may have various spls so impose our own */
705 1.41.2.2 yamt MIDI_BUF_DECLARE(idx);
706 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
707 1.1 augustss
708 1.3 augustss if (!sc->isopen)
709 1.3 augustss return;
710 1.14 augustss
711 1.1 augustss if (!(sc->flags & FREAD))
712 1.1 augustss return; /* discard data if not reading */
713 1.41.2.2 yamt
714 1.41.2.2 yamt sxp_again:
715 1.41.2.2 yamt do
716 1.41.2.2 yamt got = midi_fst(&sc->rcv, data, FST_CANON);
717 1.41.2.2 yamt while ( got == FST_HUH );
718 1.41.2.2 yamt
719 1.41.2.2 yamt switch ( got ) {
720 1.41.2.2 yamt case FST_MORE:
721 1.41.2.2 yamt case FST_ERR:
722 1.1 augustss return;
723 1.41.2.2 yamt case FST_CHN:
724 1.41.2.2 yamt case FST_COM:
725 1.41.2.2 yamt case FST_RT:
726 1.1 augustss #if NSEQUENCER > 0
727 1.41.2.2 yamt if (sc->seqopen) {
728 1.41.2.2 yamt extern void midiseq_in(struct midi_dev *,u_char *,int);
729 1.41.2.2 yamt count = sc->rcv.end - sc->rcv.pos;
730 1.41.2.2 yamt midiseq_in(sc->seq_md, sc->rcv.pos, count);
731 1.41.2.2 yamt return;
732 1.41.2.2 yamt }
733 1.1 augustss #endif
734 1.41.2.2 yamt /*
735 1.41.2.2 yamt * Pass Active Sense to the sequencer if it's open, but not to
736 1.41.2.2 yamt * a raw reader. (Really should do something intelligent with
737 1.41.2.2 yamt * it then, though....)
738 1.41.2.2 yamt */
739 1.41.2.2 yamt if ( got == FST_RT && MIDI_ACK == sc->rcv.pos[0] ) {
740 1.41.2.2 yamt if ( !sc->rcv_expect_asense ) {
741 1.41.2.2 yamt sc->rcv_expect_asense = 1;
742 1.41.2.2 yamt callout_schedule(&sc->rcv_asense_co,
743 1.41.2.2 yamt MIDI_RCV_ASENSE_PERIOD);
744 1.41.2.2 yamt }
745 1.41.2.2 yamt sc->rcv_quiescent = 0;
746 1.41.2.2 yamt sc->rcv_eof = 0;
747 1.41.2.2 yamt return;
748 1.41.2.2 yamt }
749 1.41.2.2 yamt /* FALLTHROUGH */
750 1.41.2.2 yamt /*
751 1.41.2.2 yamt * Ultimately SysEx msgs should be offered to the sequencer also; the
752 1.41.2.2 yamt * sequencer API addresses them - but maybe our sequencer can't handle
753 1.41.2.2 yamt * them yet, so offer only to raw reader. (Which means, ultimately,
754 1.41.2.2 yamt * discard them if the sequencer's open, as it's not doing reads!)
755 1.41.2.2 yamt * -> When SysEx support is added to the sequencer, be sure to handle
756 1.41.2.2 yamt * FST_SXP there too.
757 1.41.2.2 yamt */
758 1.41.2.2 yamt case FST_SYX:
759 1.41.2.2 yamt case FST_SXP:
760 1.41.2.2 yamt count = sc->rcv.end - sc->rcv.pos;
761 1.41.2.2 yamt MIDI_IN_LOCK(sc,s);
762 1.41.2.2 yamt sc->rcv_quiescent = 0;
763 1.41.2.2 yamt sc->rcv_eof = 0;
764 1.41.2.2 yamt if ( 0 == count ) {
765 1.41.2.2 yamt MIDI_IN_UNLOCK(sc,s);
766 1.41.2.2 yamt break;
767 1.41.2.2 yamt }
768 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(mb,idx);
769 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(mb,buf);
770 1.41.2.2 yamt if (count > buf_lim - buf_cur
771 1.41.2.2 yamt || 1 > idx_lim - idx_cur) {
772 1.41.2.2 yamt sc->rcv.bytesDiscarded.ev_count += count;
773 1.41.2.2 yamt MIDI_IN_UNLOCK(sc,s);
774 1.41.2.2 yamt DPRINTF(("midi_in: buffer full, discard data=0x%02x\n",
775 1.41.2.2 yamt sc->rcv.pos[0]));
776 1.41.2.2 yamt return;
777 1.41.2.2 yamt }
778 1.41.2.2 yamt for (i = 0; i < count; i++) {
779 1.41.2.2 yamt *buf_cur++ = sc->rcv.pos[i];
780 1.41.2.2 yamt MIDI_BUF_WRAP(buf);
781 1.41.2.2 yamt }
782 1.41.2.2 yamt *idx_cur++ = PACK_MB_IDX(got,count);
783 1.41.2.2 yamt MIDI_BUF_WRAP(idx);
784 1.41.2.2 yamt MIDI_BUF_PRODUCER_WBACK(mb,buf);
785 1.41.2.2 yamt MIDI_BUF_PRODUCER_WBACK(mb,idx);
786 1.41.2.2 yamt MIDI_IN_UNLOCK(sc,s);
787 1.41.2.3 yamt softintr_schedule(sc->sih_rd);
788 1.41.2.2 yamt break;
789 1.41.2.2 yamt default: /* don't #ifdef this away, gcc will say FST_HUH not handled */
790 1.41.2.2 yamt printf("midi_in: midi_fst returned %d?!\n", got);
791 1.1 augustss }
792 1.41.2.2 yamt if ( FST_SXP == got )
793 1.41.2.2 yamt goto sxp_again;
794 1.1 augustss }
795 1.1 augustss
796 1.1 augustss void
797 1.22 augustss midi_out(void *addr)
798 1.1 augustss {
799 1.1 augustss struct midi_softc *sc = addr;
800 1.3 augustss
801 1.3 augustss if (!sc->isopen)
802 1.3 augustss return;
803 1.41.2.2 yamt DPRINTFN(8, ("midi_out: %p\n", sc));
804 1.41.2.2 yamt midi_intr_out(sc);
805 1.1 augustss }
806 1.1 augustss
807 1.1 augustss int
808 1.41.2.1 yamt midiopen(dev_t dev, int flags, int ifmt, struct lwp *l)
809 1.1 augustss {
810 1.1 augustss struct midi_softc *sc;
811 1.38 yamt const struct midi_hw_if *hw;
812 1.1 augustss int error;
813 1.1 augustss
814 1.17 thorpej sc = device_lookup(&midi_cd, MIDIUNIT(dev));
815 1.17 thorpej if (sc == NULL)
816 1.17 thorpej return (ENXIO);
817 1.18 tshiozak if (sc->dying)
818 1.18 tshiozak return (EIO);
819 1.17 thorpej
820 1.41.2.2 yamt DPRINTFN(3,("midiopen %p\n", sc));
821 1.1 augustss
822 1.1 augustss hw = sc->hw_if;
823 1.1 augustss if (!hw)
824 1.1 augustss return ENXIO;
825 1.1 augustss if (sc->isopen)
826 1.1 augustss return EBUSY;
827 1.41.2.2 yamt
828 1.41.2.2 yamt /* put both state machines into known states */
829 1.41.2.2 yamt sc->rcv.state = MIDI_IN_START;
830 1.41.2.2 yamt sc->rcv.pos = sc->rcv.msg;
831 1.41.2.2 yamt sc->rcv.end = sc->rcv.msg;
832 1.41.2.2 yamt sc->xmt.state = MIDI_IN_START;
833 1.41.2.2 yamt sc->xmt.pos = sc->xmt.msg;
834 1.41.2.2 yamt sc->xmt.end = sc->xmt.msg;
835 1.41.2.2 yamt
836 1.41.2.2 yamt /* copy error counters so an ioctl (TBA) can give since-open stats */
837 1.41.2.2 yamt sc->rcv.atOpen.bytesDiscarded = sc->rcv.bytesDiscarded.ev_count;
838 1.41.2.2 yamt sc->rcv.atQuery.bytesDiscarded = sc->rcv.bytesDiscarded.ev_count;
839 1.41.2.2 yamt
840 1.41.2.2 yamt sc->xmt.atOpen.bytesDiscarded = sc->xmt.bytesDiscarded.ev_count;
841 1.41.2.2 yamt sc->xmt.atQuery.bytesDiscarded = sc->xmt.bytesDiscarded.ev_count;
842 1.41.2.2 yamt
843 1.41.2.2 yamt /* and the buffers */
844 1.41.2.2 yamt midi_initbuf(&sc->outbuf);
845 1.41.2.2 yamt midi_initbuf(&sc->inbuf);
846 1.41.2.2 yamt
847 1.41.2.2 yamt /* and the receive flags */
848 1.41.2.2 yamt sc->rcv_expect_asense = 0;
849 1.41.2.2 yamt sc->rcv_quiescent = 0;
850 1.41.2.2 yamt sc->rcv_eof = 0;
851 1.41.2.2 yamt
852 1.1 augustss error = hw->open(sc->hw_hdl, flags, midi_in, midi_out, sc);
853 1.1 augustss if (error)
854 1.1 augustss return error;
855 1.1 augustss sc->isopen++;
856 1.1 augustss sc->flags = flags;
857 1.1 augustss sc->rchan = 0;
858 1.1 augustss sc->wchan = 0;
859 1.1 augustss sc->pbus = 0;
860 1.1 augustss sc->async = 0;
861 1.1 augustss
862 1.4 augustss #ifdef MIDI_SAVE
863 1.4 augustss if (midicnt != 0) {
864 1.4 augustss midisave.cnt = midicnt;
865 1.4 augustss midicnt = 0;
866 1.4 augustss }
867 1.4 augustss #endif
868 1.4 augustss
869 1.1 augustss return 0;
870 1.1 augustss }
871 1.1 augustss
872 1.1 augustss int
873 1.41.2.2 yamt midiclose(dev_t dev, int flags, int ifmt,
874 1.41.2.2 yamt struct lwp *l)
875 1.1 augustss {
876 1.1 augustss int unit = MIDIUNIT(dev);
877 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
878 1.38 yamt const struct midi_hw_if *hw = sc->hw_if;
879 1.1 augustss int s, error;
880 1.1 augustss
881 1.41.2.2 yamt DPRINTFN(3,("midiclose %p\n", sc));
882 1.1 augustss
883 1.41.2.2 yamt /* midi_start_output(sc); anything buffered => pbus already set! */
884 1.1 augustss error = 0;
885 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
886 1.41.2.2 yamt while (sc->pbus) {
887 1.41.2.2 yamt DPRINTFN(8,("midiclose sleep ...\n"));
888 1.41.2.2 yamt error =
889 1.41.2.2 yamt midi_sleep_timo(&sc->wchan, "mid_dr", 30*hz, &sc->out_lock);
890 1.1 augustss }
891 1.3 augustss sc->isopen = 0;
892 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
893 1.41.2.2 yamt callout_stop(&sc->xmt_asense_co); /* xxx fix this - sleep? */
894 1.41.2.2 yamt callout_stop(&sc->rcv_asense_co);
895 1.1 augustss hw->close(sc->hw_hdl);
896 1.1 augustss #if NSEQUENCER > 0
897 1.1 augustss sc->seqopen = 0;
898 1.7 augustss sc->seq_md = 0;
899 1.1 augustss #endif
900 1.1 augustss return 0;
901 1.1 augustss }
902 1.1 augustss
903 1.1 augustss int
904 1.22 augustss midiread(dev_t dev, struct uio *uio, int ioflag)
905 1.1 augustss {
906 1.1 augustss int unit = MIDIUNIT(dev);
907 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
908 1.1 augustss struct midi_buffer *mb = &sc->inbuf;
909 1.1 augustss int error;
910 1.1 augustss int s;
911 1.41.2.2 yamt MIDI_BUF_DECLARE(idx);
912 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
913 1.41.2.2 yamt int appetite;
914 1.41.2.2 yamt int first = 1;
915 1.1 augustss
916 1.41.2.2 yamt DPRINTFN(6,("midiread: %p, count=%lu\n", sc,
917 1.11 nathanw (unsigned long)uio->uio_resid));
918 1.1 augustss
919 1.18 tshiozak if (sc->dying)
920 1.18 tshiozak return EIO;
921 1.41.2.2 yamt if ( !(sc->props & MIDI_PROP_CAN_INPUT) )
922 1.41.2.2 yamt return ENXIO;
923 1.18 tshiozak
924 1.41.2.2 yamt MIDI_IN_LOCK(sc,s);
925 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(mb,idx);
926 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(mb,buf);
927 1.41.2.2 yamt MIDI_IN_UNLOCK(sc,s);
928 1.41.2.2 yamt
929 1.1 augustss error = 0;
930 1.41.2.2 yamt for ( ;; ) {
931 1.41.2.2 yamt /*
932 1.41.2.2 yamt * If the used portion of idx wraps around the end, just take
933 1.41.2.2 yamt * the first part on this iteration, and we'll get the rest on
934 1.41.2.2 yamt * the next.
935 1.41.2.2 yamt */
936 1.41.2.2 yamt if ( idx_lim > idx_end )
937 1.41.2.2 yamt idx_lim = idx_end;
938 1.41.2.2 yamt /*
939 1.41.2.2 yamt * Count bytes through the last complete message that will
940 1.41.2.2 yamt * fit in the requested read.
941 1.41.2.2 yamt */
942 1.41.2.2 yamt for (appetite = uio->uio_resid; idx_cur < idx_lim; ++idx_cur) {
943 1.41.2.2 yamt if ( appetite < MB_IDX_LEN(*idx_cur) )
944 1.41.2.2 yamt break;
945 1.41.2.2 yamt appetite -= MB_IDX_LEN(*idx_cur);
946 1.41.2.2 yamt }
947 1.41.2.2 yamt appetite = uio->uio_resid - appetite;
948 1.41.2.2 yamt /*
949 1.41.2.2 yamt * Only if the read is too small to hold even the first
950 1.41.2.2 yamt * complete message will we return a partial one (updating idx
951 1.41.2.2 yamt * to reflect the remaining length of the message).
952 1.41.2.2 yamt */
953 1.41.2.2 yamt if ( appetite == 0 && idx_cur < idx_lim ) {
954 1.41.2.2 yamt if ( !first )
955 1.41.2.2 yamt goto unlocked_exit; /* idx_cur not advanced */
956 1.41.2.2 yamt appetite = uio->uio_resid;
957 1.41.2.2 yamt *idx_cur = PACK_MB_IDX(MB_IDX_CAT(*idx_cur),
958 1.41.2.2 yamt MB_IDX_LEN(*idx_cur) - appetite);
959 1.41.2.2 yamt }
960 1.41.2.2 yamt KASSERT(buf_cur + appetite <= buf_lim);
961 1.41.2.2 yamt
962 1.41.2.2 yamt /* move the bytes */
963 1.41.2.2 yamt if ( appetite > 0 ) {
964 1.41.2.2 yamt first = 0; /* we know we won't return empty-handed */
965 1.41.2.2 yamt /* do two uiomoves if data wrap around end of buf */
966 1.41.2.2 yamt if ( buf_cur + appetite > buf_end ) {
967 1.41.2.2 yamt DPRINTFN(8,
968 1.41.2.4 yamt ("midiread: uiomove cc=%td (prewrap)\n",
969 1.41.2.2 yamt buf_end - buf_cur));
970 1.41.2.2 yamt error = uiomove(buf_cur, buf_end-buf_cur, uio);
971 1.41.2.2 yamt if ( error )
972 1.41.2.2 yamt goto unlocked_exit;
973 1.41.2.2 yamt appetite -= buf_end - buf_cur;
974 1.41.2.2 yamt buf_cur = mb->buf;
975 1.1 augustss }
976 1.41.2.2 yamt DPRINTFN(8, ("midiread: uiomove cc=%d\n", appetite));
977 1.41.2.2 yamt error = uiomove(buf_cur, appetite, uio);
978 1.41.2.2 yamt if ( error )
979 1.41.2.2 yamt goto unlocked_exit;
980 1.41.2.2 yamt buf_cur += appetite;
981 1.41.2.2 yamt }
982 1.41.2.2 yamt
983 1.41.2.2 yamt MIDI_BUF_WRAP(idx);
984 1.41.2.2 yamt MIDI_BUF_WRAP(buf);
985 1.41.2.2 yamt
986 1.41.2.2 yamt MIDI_IN_LOCK(sc,s);
987 1.41.2.2 yamt MIDI_BUF_CONSUMER_WBACK(mb,idx);
988 1.41.2.2 yamt MIDI_BUF_CONSUMER_WBACK(mb,buf);
989 1.41.2.2 yamt if ( 0 == uio->uio_resid ) /* if read satisfied, we're done */
990 1.41.2.2 yamt break;
991 1.41.2.2 yamt MIDI_BUF_CONSUMER_REFRESH(mb,idx);
992 1.41.2.2 yamt if ( idx_cur == idx_lim ) { /* need to wait for data? */
993 1.41.2.2 yamt if ( !first || sc->rcv_eof ) /* never block reader if */
994 1.41.2.2 yamt break; /* any data already in hand */
995 1.41.2.2 yamt if (ioflag & IO_NDELAY) {
996 1.41.2.2 yamt error = EWOULDBLOCK;
997 1.41.2.2 yamt break;
998 1.1 augustss }
999 1.41.2.2 yamt error = midi_sleep(&sc->rchan, "mid rd", &sc->in_lock);
1000 1.41.2.2 yamt if ( error )
1001 1.41.2.2 yamt break;
1002 1.41.2.2 yamt MIDI_BUF_CONSUMER_REFRESH(mb,idx); /* what'd we get? */
1003 1.1 augustss }
1004 1.41.2.2 yamt MIDI_BUF_CONSUMER_REFRESH(mb,buf);
1005 1.41.2.2 yamt MIDI_IN_UNLOCK(sc,s);
1006 1.41.2.2 yamt if ( sc->dying )
1007 1.18 tshiozak return EIO;
1008 1.1 augustss }
1009 1.41.2.2 yamt MIDI_IN_UNLOCK(sc,s);
1010 1.41.2.2 yamt
1011 1.41.2.2 yamt unlocked_exit:
1012 1.1 augustss return error;
1013 1.1 augustss }
1014 1.1 augustss
1015 1.1 augustss void
1016 1.41.2.2 yamt midi_rcv_asense(void *arg)
1017 1.41.2.2 yamt {
1018 1.41.2.2 yamt struct midi_softc *sc = arg;
1019 1.41.2.2 yamt int s;
1020 1.41.2.2 yamt
1021 1.41.2.2 yamt if ( sc->dying || !sc->isopen )
1022 1.41.2.2 yamt return;
1023 1.41.2.2 yamt
1024 1.41.2.2 yamt if ( sc->rcv_quiescent ) {
1025 1.41.2.2 yamt MIDI_IN_LOCK(sc,s);
1026 1.41.2.2 yamt sc->rcv_eof = 1;
1027 1.41.2.2 yamt sc->rcv_quiescent = 0;
1028 1.41.2.2 yamt sc->rcv_expect_asense = 0;
1029 1.41.2.2 yamt MIDI_IN_UNLOCK(sc,s);
1030 1.41.2.3 yamt softintr_schedule(sc->sih_rd);
1031 1.41.2.2 yamt return;
1032 1.41.2.2 yamt }
1033 1.41.2.2 yamt
1034 1.41.2.2 yamt sc->rcv_quiescent = 1;
1035 1.41.2.2 yamt callout_schedule(&sc->rcv_asense_co, MIDI_RCV_ASENSE_PERIOD);
1036 1.41.2.2 yamt }
1037 1.41.2.2 yamt
1038 1.41.2.2 yamt void
1039 1.41.2.2 yamt midi_xmt_asense(void *arg)
1040 1.1 augustss {
1041 1.1 augustss struct midi_softc *sc = arg;
1042 1.41.2.2 yamt int s;
1043 1.41.2.2 yamt int error;
1044 1.41.2.2 yamt int armed;
1045 1.41.2.2 yamt
1046 1.41.2.2 yamt if ( sc->dying || !sc->isopen )
1047 1.41.2.2 yamt return;
1048 1.41.2.2 yamt
1049 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1050 1.41.2.2 yamt if ( sc->pbus || sc->dying || !sc->isopen ) {
1051 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1052 1.41.2.2 yamt return;
1053 1.41.2.2 yamt }
1054 1.41.2.2 yamt sc->pbus = 1;
1055 1.41.2.2 yamt DPRINTFN(8,("midi_xmt_asense: %p\n", sc));
1056 1.41.2.2 yamt
1057 1.41.2.2 yamt if ( sc->props & MIDI_PROP_OUT_INTR ) {
1058 1.41.2.2 yamt error = sc->hw_if->output(sc->hw_hdl, MIDI_ACK);
1059 1.41.2.2 yamt armed = (error == 0);
1060 1.41.2.2 yamt } else { /* polled output, do with interrupts unmasked */
1061 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1062 1.41.2.2 yamt /* running from softclock, so top half won't sneak in here */
1063 1.41.2.2 yamt error = sc->hw_if->output(sc->hw_hdl, MIDI_ACK);
1064 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1065 1.41.2.2 yamt armed = 0;
1066 1.41.2.2 yamt }
1067 1.1 augustss
1068 1.41.2.2 yamt if ( !armed ) {
1069 1.41.2.2 yamt sc->pbus = 0;
1070 1.41.2.2 yamt callout_schedule(&sc->xmt_asense_co, MIDI_XMT_ASENSE_PERIOD);
1071 1.41.2.2 yamt }
1072 1.41.2.2 yamt
1073 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1074 1.41.2.2 yamt }
1075 1.41.2.2 yamt
1076 1.41.2.2 yamt /*
1077 1.41.2.2 yamt * The way this function was hacked up to plug into poll_out and intr_out
1078 1.41.2.2 yamt * after they were written won't win it any beauty contests, but it'll work
1079 1.41.2.2 yamt * (code in haste, refactor at leisure). This may be called with the lock
1080 1.41.2.2 yamt * (by intr_out) or without the lock (by poll_out) so it only does what could
1081 1.41.2.2 yamt * be safe either way.
1082 1.41.2.2 yamt */
1083 1.41.2.2 yamt int midi_msg_out(struct midi_softc *sc,
1084 1.41.2.2 yamt u_char **idx, u_char **idxl, u_char **buf, u_char **bufl) {
1085 1.41.2.2 yamt MIDI_BUF_DECLARE(idx);
1086 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
1087 1.41.2.2 yamt MIDI_BUF_EXTENT_INIT(&sc->outbuf,idx);
1088 1.41.2.2 yamt MIDI_BUF_EXTENT_INIT(&sc->outbuf,buf);
1089 1.41.2.2 yamt int length;
1090 1.41.2.2 yamt int error;
1091 1.41.2.2 yamt u_char contig[3];
1092 1.41.2.2 yamt u_char *cp;
1093 1.41.2.2 yamt u_char *ep;
1094 1.41.2.2 yamt
1095 1.41.2.2 yamt idx_cur = *idx;
1096 1.41.2.2 yamt idx_lim = *idxl;
1097 1.41.2.2 yamt buf_cur = *buf;
1098 1.41.2.2 yamt buf_lim = *bufl;
1099 1.41.2.2 yamt
1100 1.41.2.2 yamt length = MB_IDX_LEN(*idx_cur);
1101 1.41.2.2 yamt
1102 1.41.2.2 yamt for ( cp = contig, ep = cp + length; cp < ep; ) {
1103 1.41.2.2 yamt *cp++ = *buf_cur++;
1104 1.41.2.2 yamt MIDI_BUF_WRAP(buf);
1105 1.41.2.2 yamt }
1106 1.41.2.2 yamt cp = contig;
1107 1.41.2.2 yamt
1108 1.41.2.2 yamt switch ( MB_IDX_CAT(*idx_cur) ) {
1109 1.41.2.2 yamt case FST_CHV: /* chnmsg to be compressed (for device that wants it) */
1110 1.41.2.2 yamt ++ cp;
1111 1.41.2.2 yamt -- length;
1112 1.41.2.2 yamt /* FALLTHROUGH */
1113 1.41.2.2 yamt case FST_CHN:
1114 1.41.2.2 yamt error = sc->hw_if_ext->channel(sc->hw_hdl,
1115 1.41.2.2 yamt MIDI_GET_STATUS(contig[0]),
1116 1.41.2.2 yamt MIDI_GET_CHAN(contig[0]),
1117 1.41.2.2 yamt cp, length);
1118 1.41.2.2 yamt break;
1119 1.41.2.2 yamt case FST_COM:
1120 1.41.2.2 yamt error = sc->hw_if_ext->common(sc->hw_hdl,
1121 1.41.2.2 yamt MIDI_GET_STATUS(contig[0]),
1122 1.41.2.2 yamt cp, length);
1123 1.41.2.2 yamt break;
1124 1.41.2.2 yamt case FST_SYX:
1125 1.41.2.2 yamt case FST_SXP:
1126 1.41.2.2 yamt error = sc->hw_if_ext->sysex(sc->hw_hdl,
1127 1.41.2.2 yamt cp, length);
1128 1.41.2.2 yamt break;
1129 1.41.2.2 yamt case FST_RT:
1130 1.41.2.2 yamt error = sc->hw_if->output(sc->hw_hdl, *cp);
1131 1.41.2.2 yamt break;
1132 1.41.2.2 yamt default:
1133 1.41.2.2 yamt error = EIO;
1134 1.41.2.2 yamt }
1135 1.41.2.2 yamt
1136 1.41.2.2 yamt if ( !error ) {
1137 1.41.2.2 yamt ++ idx_cur;
1138 1.41.2.2 yamt MIDI_BUF_WRAP(idx);
1139 1.41.2.2 yamt *idx = idx_cur;
1140 1.41.2.2 yamt *idxl = idx_lim;
1141 1.41.2.2 yamt *buf = buf_cur;
1142 1.41.2.2 yamt *bufl = buf_lim;
1143 1.41.2.2 yamt }
1144 1.41.2.2 yamt
1145 1.41.2.2 yamt return error;
1146 1.1 augustss }
1147 1.1 augustss
1148 1.41.2.2 yamt /*
1149 1.41.2.2 yamt * midi_poll_out is intended for the midi hw (the vast majority of MIDI UARTs
1150 1.41.2.2 yamt * on sound cards, apparently) that _do not have transmit-ready interrupts_.
1151 1.41.2.2 yamt * Every call to hw_if->output for one of these may busy-wait to output the
1152 1.41.2.2 yamt * byte; at the standard midi data rate that'll be 320us per byte. The
1153 1.41.2.2 yamt * technique of writing only MIDI_MAX_WRITE bytes in a row and then waiting
1154 1.41.2.2 yamt * for MIDI_WAIT does not reduce the total time spent busy-waiting, and it
1155 1.41.2.2 yamt * adds arbitrary delays in transmission (and, since MIDI_WAIT is roughly the
1156 1.41.2.2 yamt * same as the time to send MIDI_MAX_WRITE bytes, it effectively halves the
1157 1.41.2.2 yamt * data rate). Here, a somewhat bolder approach is taken. Since midi traffic
1158 1.41.2.2 yamt * is bursty but time-sensitive--most of the time there will be none at all,
1159 1.41.2.2 yamt * but when there is it should go out ASAP--the strategy is to just get it
1160 1.41.2.2 yamt * over with, and empty the buffer in one go. The effect this can have on
1161 1.41.2.2 yamt * the rest of the system will be limited by the size of the buffer and the
1162 1.41.2.2 yamt * sparseness of the traffic. But some precautions are in order. Interrupts
1163 1.41.2.2 yamt * should all be unmasked when this is called, and midiwrite should not fill
1164 1.41.2.2 yamt * the buffer more than once (when MIDI_PROP_CAN_INTR is false) without a
1165 1.41.2.2 yamt * yield() so some other process can get scheduled. If the write is nonblocking,
1166 1.41.2.2 yamt * midiwrite should return a short count rather than yield.
1167 1.41.2.2 yamt *
1168 1.41.2.2 yamt * Someday when there is fine-grained MP support, this should be reworked to
1169 1.41.2.2 yamt * run in a callout so the writing process really could proceed concurrently.
1170 1.41.2.2 yamt * But obviously where performance is a concern, interrupt-driven hardware
1171 1.41.2.2 yamt * such as USB midi or (apparently) clcs will always be preferable. And it
1172 1.41.2.2 yamt * seems (kern/32651) that many of the devices currently working in poll mode
1173 1.41.2.2 yamt * may really have tx interrupt capability and want only implementation; that
1174 1.41.2.2 yamt * ought to happen.
1175 1.41.2.2 yamt */
1176 1.1 augustss int
1177 1.41.2.2 yamt midi_poll_out(struct midi_softc *sc)
1178 1.1 augustss {
1179 1.1 augustss struct midi_buffer *mb = &sc->outbuf;
1180 1.1 augustss int error;
1181 1.41.2.2 yamt int msglen;
1182 1.1 augustss int s;
1183 1.41.2.2 yamt MIDI_BUF_DECLARE(idx);
1184 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
1185 1.1 augustss
1186 1.1 augustss error = 0;
1187 1.18 tshiozak
1188 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1189 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(mb,idx);
1190 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(mb,buf);
1191 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1192 1.41.2.2 yamt
1193 1.41.2.2 yamt for ( ;; ) {
1194 1.41.2.2 yamt while ( idx_cur != idx_lim ) {
1195 1.41.2.2 yamt if ( sc->hw_if_ext ) {
1196 1.41.2.2 yamt error = midi_msg_out(sc, &idx_cur, &idx_lim,
1197 1.41.2.2 yamt &buf_cur, &buf_lim);
1198 1.41.2.2 yamt if ( error )
1199 1.41.2.2 yamt goto ioerror;
1200 1.41.2.2 yamt continue;
1201 1.41.2.2 yamt }
1202 1.41.2.2 yamt /* or, lacking hw_if_ext ... */
1203 1.41.2.2 yamt msglen = MB_IDX_LEN(*idx_cur);
1204 1.41.2.2 yamt DPRINTFN(7,("midi_poll_out: %p <- %#02x\n",
1205 1.41.2.2 yamt sc->hw_hdl, *buf_cur));
1206 1.41.2.2 yamt error = sc->hw_if->output(sc->hw_hdl, *buf_cur);
1207 1.41.2.2 yamt if ( error )
1208 1.41.2.2 yamt goto ioerror;
1209 1.41.2.2 yamt ++ buf_cur;
1210 1.41.2.2 yamt MIDI_BUF_WRAP(buf);
1211 1.41.2.2 yamt -- msglen;
1212 1.41.2.2 yamt if ( msglen )
1213 1.41.2.2 yamt *idx_cur = PACK_MB_IDX(MB_IDX_CAT(*idx_cur),
1214 1.41.2.2 yamt msglen);
1215 1.41.2.2 yamt else {
1216 1.41.2.2 yamt ++ idx_cur;
1217 1.41.2.2 yamt MIDI_BUF_WRAP(idx);
1218 1.41.2.2 yamt }
1219 1.41.2.2 yamt }
1220 1.41.2.2 yamt KASSERT(buf_cur == buf_lim);
1221 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1222 1.41.2.2 yamt MIDI_BUF_CONSUMER_WBACK(mb,idx);
1223 1.41.2.2 yamt MIDI_BUF_CONSUMER_WBACK(mb,buf);
1224 1.41.2.2 yamt MIDI_BUF_CONSUMER_REFRESH(mb,idx); /* any more to transmit? */
1225 1.41.2.2 yamt MIDI_BUF_CONSUMER_REFRESH(mb,buf);
1226 1.41.2.2 yamt if ( idx_lim == idx_cur )
1227 1.41.2.2 yamt break; /* still holding lock */
1228 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1229 1.41.2.2 yamt }
1230 1.41.2.2 yamt goto disarm; /* lock held */
1231 1.18 tshiozak
1232 1.41.2.2 yamt ioerror:
1233 1.41.2.2 yamt #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
1234 1.41.2.2 yamt printf("%s: midi_poll_output error %d\n",
1235 1.41.2.2 yamt sc->dev.dv_xname, error);
1236 1.4 augustss #endif
1237 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1238 1.41.2.2 yamt MIDI_BUF_CONSUMER_WBACK(mb,idx);
1239 1.41.2.2 yamt MIDI_BUF_CONSUMER_WBACK(mb,buf);
1240 1.41.2.2 yamt
1241 1.41.2.2 yamt disarm:
1242 1.41.2.2 yamt sc->pbus = 0;
1243 1.41.2.2 yamt callout_schedule(&sc->xmt_asense_co, MIDI_XMT_ASENSE_PERIOD);
1244 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1245 1.41.2.2 yamt return error;
1246 1.41.2.2 yamt }
1247 1.41.2.2 yamt
1248 1.41.2.2 yamt /*
1249 1.41.2.2 yamt * The interrupt flavor acquires spl and lock once and releases at the end,
1250 1.41.2.2 yamt * as it expects to write only one byte or message. The interface convention
1251 1.41.2.2 yamt * is that if hw_if->output returns 0, it has initiated transmission and the
1252 1.41.2.2 yamt * completion interrupt WILL be forthcoming; if it has not returned 0, NO
1253 1.41.2.2 yamt * interrupt will be forthcoming, and if it returns EINPROGRESS it wants
1254 1.41.2.2 yamt * another byte right away.
1255 1.41.2.2 yamt */
1256 1.41.2.2 yamt int
1257 1.41.2.2 yamt midi_intr_out(struct midi_softc *sc)
1258 1.41.2.2 yamt {
1259 1.41.2.2 yamt struct midi_buffer *mb = &sc->outbuf;
1260 1.41.2.2 yamt int error;
1261 1.41.2.2 yamt int msglen;
1262 1.41.2.2 yamt int s;
1263 1.41.2.2 yamt MIDI_BUF_DECLARE(idx);
1264 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
1265 1.41.2.2 yamt int armed = 0;
1266 1.41.2.2 yamt
1267 1.41.2.2 yamt error = 0;
1268 1.41.2.2 yamt
1269 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1270 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(mb,idx);
1271 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(mb,buf);
1272 1.41.2.2 yamt
1273 1.41.2.2 yamt while ( idx_cur != idx_lim ) {
1274 1.41.2.2 yamt if ( sc->hw_if_ext ) {
1275 1.41.2.2 yamt error = midi_msg_out(sc, &idx_cur, &idx_lim,
1276 1.41.2.2 yamt &buf_cur, &buf_lim);
1277 1.41.2.2 yamt if ( !error ) /* no EINPROGRESS from extended hw_if */
1278 1.41.2.2 yamt armed = 1;
1279 1.20 tshiozak break;
1280 1.41.2.2 yamt }
1281 1.41.2.2 yamt /* or, lacking hw_if_ext ... */
1282 1.41.2.2 yamt msglen = MB_IDX_LEN(*idx_cur);
1283 1.41.2.2 yamt error = sc->hw_if->output(sc->hw_hdl, *buf_cur);
1284 1.41.2.2 yamt if ( error && error != EINPROGRESS )
1285 1.41.2.2 yamt break;
1286 1.41.2.2 yamt ++ buf_cur;
1287 1.41.2.2 yamt MIDI_BUF_WRAP(buf);
1288 1.41.2.2 yamt -- msglen;
1289 1.41.2.2 yamt if ( msglen )
1290 1.41.2.2 yamt *idx_cur = PACK_MB_IDX(MB_IDX_CAT(*idx_cur),msglen);
1291 1.41.2.2 yamt else {
1292 1.41.2.2 yamt ++ idx_cur;
1293 1.41.2.2 yamt MIDI_BUF_WRAP(idx);
1294 1.41.2.2 yamt }
1295 1.41.2.2 yamt if ( !error ) {
1296 1.41.2.2 yamt armed = 1;
1297 1.41.2.2 yamt break;
1298 1.41.2.2 yamt }
1299 1.41.2.2 yamt }
1300 1.41.2.2 yamt MIDI_BUF_CONSUMER_WBACK(mb,idx);
1301 1.41.2.2 yamt MIDI_BUF_CONSUMER_WBACK(mb,buf);
1302 1.41.2.2 yamt if ( !armed ) {
1303 1.41.2.2 yamt sc->pbus = 0;
1304 1.41.2.2 yamt callout_schedule(&sc->xmt_asense_co, MIDI_XMT_ASENSE_PERIOD);
1305 1.1 augustss }
1306 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1307 1.41.2.3 yamt softintr_schedule(sc->sih_wr);
1308 1.20 tshiozak
1309 1.41.2.2 yamt #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
1310 1.41.2.2 yamt if ( error )
1311 1.41.2.2 yamt printf("%s: midi_intr_output error %d\n",
1312 1.41.2.2 yamt sc->dev.dv_xname, error);
1313 1.41.2.2 yamt #endif
1314 1.1 augustss return error;
1315 1.1 augustss }
1316 1.1 augustss
1317 1.1 augustss int
1318 1.41.2.2 yamt midi_start_output(struct midi_softc *sc)
1319 1.41.2.2 yamt {
1320 1.41.2.2 yamt if (sc->dying)
1321 1.41.2.2 yamt return EIO;
1322 1.41.2.2 yamt
1323 1.41.2.2 yamt if ( sc->props & MIDI_PROP_OUT_INTR )
1324 1.41.2.2 yamt return midi_intr_out(sc);
1325 1.41.2.2 yamt return midi_poll_out(sc);
1326 1.41.2.2 yamt }
1327 1.41.2.2 yamt
1328 1.41.2.2 yamt static int
1329 1.41.2.2 yamt real_writebytes(struct midi_softc *sc, u_char *ibuf, int cc)
1330 1.41.2.2 yamt {
1331 1.41.2.2 yamt u_char *iend = ibuf + cc;
1332 1.41.2.2 yamt struct midi_buffer *mb = &sc->outbuf;
1333 1.41.2.2 yamt int arming = 0;
1334 1.41.2.2 yamt int count;
1335 1.41.2.2 yamt int s;
1336 1.41.2.2 yamt int got;
1337 1.41.2.2 yamt enum fst_form form;
1338 1.41.2.2 yamt MIDI_BUF_DECLARE(idx);
1339 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
1340 1.41.2.2 yamt
1341 1.41.2.2 yamt /*
1342 1.41.2.2 yamt * If the hardware uses the extended hw_if, pass it canonicalized
1343 1.41.2.2 yamt * messages (or compressed ones if it specifically requests, using
1344 1.41.2.2 yamt * VCOMP form so the bottom half can still pass the op and chan along);
1345 1.41.2.2 yamt * if it does not, send it compressed messages (using COMPR form as
1346 1.41.2.2 yamt * there is no need to preserve the status for the bottom half).
1347 1.41.2.2 yamt */
1348 1.41.2.2 yamt if ( NULL == sc->hw_if_ext )
1349 1.41.2.2 yamt form = FST_COMPR;
1350 1.41.2.2 yamt else if ( sc->hw_if_ext->compress )
1351 1.41.2.2 yamt form = FST_VCOMP;
1352 1.41.2.2 yamt else
1353 1.41.2.2 yamt form = FST_CANON;
1354 1.41.2.2 yamt
1355 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1356 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(mb,idx);
1357 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(mb,buf);
1358 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1359 1.41.2.2 yamt
1360 1.41.2.2 yamt if (sc->dying)
1361 1.41.2.2 yamt return EIO;
1362 1.41.2.2 yamt
1363 1.41.2.2 yamt while ( ibuf < iend ) {
1364 1.41.2.2 yamt got = midi_fst(&sc->xmt, *ibuf, form);
1365 1.41.2.2 yamt ++ ibuf;
1366 1.41.2.2 yamt switch ( got ) {
1367 1.41.2.2 yamt case FST_MORE:
1368 1.41.2.2 yamt continue;
1369 1.41.2.2 yamt case FST_ERR:
1370 1.41.2.2 yamt case FST_HUH:
1371 1.41.2.2 yamt return EPROTO;
1372 1.41.2.2 yamt case FST_CHN:
1373 1.41.2.2 yamt case FST_CHV: /* only occurs in VCOMP form */
1374 1.41.2.2 yamt case FST_COM:
1375 1.41.2.2 yamt case FST_RT:
1376 1.41.2.2 yamt case FST_SYX:
1377 1.41.2.2 yamt case FST_SXP:
1378 1.41.2.2 yamt break; /* go add to buffer */
1379 1.41.2.2 yamt #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
1380 1.41.2.2 yamt default:
1381 1.41.2.2 yamt printf("midi_wr: midi_fst returned %d?!\n", got);
1382 1.41.2.2 yamt #endif
1383 1.41.2.2 yamt }
1384 1.41.2.2 yamt count = sc->xmt.end - sc->xmt.pos;
1385 1.41.2.2 yamt if ( 0 == count ) /* can happen with stray 0xf7; see midi_fst */
1386 1.41.2.2 yamt continue;
1387 1.41.2.2 yamt /*
1388 1.41.2.2 yamt * return EWOULDBLOCK if the data passed will not fit in
1389 1.41.2.2 yamt * the buffer; the caller should have taken steps to avoid that.
1390 1.41.2.2 yamt * If got==FST_SXP we lose the new status byte, but we're losing
1391 1.41.2.2 yamt * anyway, so c'est la vie.
1392 1.41.2.2 yamt */
1393 1.41.2.2 yamt if ( idx_cur == idx_lim || count > buf_lim - buf_cur ) {
1394 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1395 1.41.2.2 yamt MIDI_BUF_PRODUCER_REFRESH(mb,idx); /* get the most */
1396 1.41.2.2 yamt MIDI_BUF_PRODUCER_REFRESH(mb,buf); /* current facts */
1397 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1398 1.41.2.2 yamt if ( idx_cur == idx_lim || count > buf_lim - buf_cur )
1399 1.41.2.2 yamt return EWOULDBLOCK; /* caller's problem */
1400 1.41.2.2 yamt }
1401 1.41.2.2 yamt *idx_cur++ = PACK_MB_IDX(got,count);
1402 1.41.2.2 yamt MIDI_BUF_WRAP(idx);
1403 1.41.2.2 yamt while ( count ) {
1404 1.41.2.2 yamt *buf_cur++ = *(sc->xmt.pos)++;
1405 1.41.2.2 yamt MIDI_BUF_WRAP(buf);
1406 1.41.2.2 yamt -- count;
1407 1.41.2.2 yamt }
1408 1.41.2.2 yamt if ( FST_SXP == got )
1409 1.41.2.2 yamt -- ibuf; /* again with same status byte */
1410 1.41.2.2 yamt }
1411 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1412 1.41.2.2 yamt MIDI_BUF_PRODUCER_WBACK(mb,buf);
1413 1.41.2.2 yamt MIDI_BUF_PRODUCER_WBACK(mb,idx);
1414 1.41.2.2 yamt /*
1415 1.41.2.2 yamt * If the output transfer is not already busy, and there is a message
1416 1.41.2.2 yamt * buffered, mark it busy, stop the Active Sense callout (what if we're
1417 1.41.2.2 yamt * too late and it's expired already? No big deal, an extra Active Sense
1418 1.41.2.2 yamt * never hurt anybody) and start the output transfer once we're out of
1419 1.41.2.2 yamt * the critical section (pbus==1 will stop anyone else doing the same).
1420 1.41.2.2 yamt */
1421 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(mb,idx); /* check what consumer's got to read */
1422 1.41.2.2 yamt if ( !sc->pbus && idx_cur < idx_lim ) {
1423 1.41.2.2 yamt sc->pbus = 1;
1424 1.41.2.2 yamt callout_stop(&sc->xmt_asense_co);
1425 1.41.2.2 yamt arming = 1;
1426 1.41.2.2 yamt }
1427 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1428 1.41.2.2 yamt return arming ? midi_start_output(sc) : 0;
1429 1.41.2.2 yamt }
1430 1.41.2.2 yamt
1431 1.41.2.2 yamt int
1432 1.22 augustss midiwrite(dev_t dev, struct uio *uio, int ioflag)
1433 1.1 augustss {
1434 1.1 augustss int unit = MIDIUNIT(dev);
1435 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
1436 1.1 augustss struct midi_buffer *mb = &sc->outbuf;
1437 1.1 augustss int error;
1438 1.41.2.2 yamt u_char inp[256];
1439 1.1 augustss int s;
1440 1.41.2.2 yamt MIDI_BUF_DECLARE(idx);
1441 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
1442 1.41.2.2 yamt size_t idxspace;
1443 1.41.2.2 yamt size_t bufspace;
1444 1.41.2.2 yamt size_t xfrcount;
1445 1.41.2.2 yamt int pollout = 0;
1446 1.1 augustss
1447 1.41.2.2 yamt DPRINTFN(6, ("midiwrite: %p, unit=%d, count=%lu\n", sc, unit,
1448 1.11 nathanw (unsigned long)uio->uio_resid));
1449 1.1 augustss
1450 1.18 tshiozak if (sc->dying)
1451 1.18 tshiozak return EIO;
1452 1.18 tshiozak
1453 1.1 augustss error = 0;
1454 1.1 augustss while (uio->uio_resid > 0 && !error) {
1455 1.41.2.2 yamt
1456 1.41.2.2 yamt /*
1457 1.41.2.2 yamt * block if necessary for the minimum buffer space to guarantee
1458 1.41.2.2 yamt * we can write something.
1459 1.41.2.2 yamt */
1460 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1461 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(mb,idx); /* init can't go above loop; */
1462 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(mb,buf); /* real_writebytes moves cur */
1463 1.41.2.2 yamt for ( ;; ) {
1464 1.41.2.2 yamt idxspace = MIDI_BUF_PRODUCER_REFRESH(mb,idx) - idx_cur;
1465 1.41.2.2 yamt bufspace = MIDI_BUF_PRODUCER_REFRESH(mb,buf) - buf_cur;
1466 1.41.2.2 yamt if ( idxspace >= 1 && bufspace >= 3 && !pollout )
1467 1.41.2.2 yamt break;
1468 1.41.2.4 yamt DPRINTFN(8,("midi_write: sleep idx=%zd buf=%zd\n",
1469 1.41.2.2 yamt idxspace, bufspace));
1470 1.1 augustss if (ioflag & IO_NDELAY) {
1471 1.41.2.2 yamt error = EWOULDBLOCK;
1472 1.41.2.2 yamt /*
1473 1.41.2.2 yamt * If some amount has already been transferred,
1474 1.41.2.2 yamt * the common syscall code will automagically
1475 1.41.2.2 yamt * convert this to success with a short count.
1476 1.41.2.2 yamt */
1477 1.41.2.2 yamt goto locked_exit;
1478 1.1 augustss }
1479 1.41.2.2 yamt if ( pollout ) {
1480 1.41.2.3 yamt preempt(); /* see midi_poll_output */
1481 1.41.2.2 yamt pollout = 0;
1482 1.41.2.2 yamt } else
1483 1.41.2.2 yamt error = midi_sleep(&sc->wchan, "mid wr",
1484 1.41.2.2 yamt &sc->out_lock);
1485 1.41.2.2 yamt if (error)
1486 1.41.2.2 yamt /*
1487 1.41.2.2 yamt * Similarly, the common code will handle
1488 1.41.2.2 yamt * EINTR and ERESTART properly here, changing to
1489 1.41.2.2 yamt * a short count if something transferred.
1490 1.41.2.2 yamt */
1491 1.41.2.2 yamt goto locked_exit;
1492 1.39 perry }
1493 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1494 1.41.2.2 yamt
1495 1.41.2.2 yamt /*
1496 1.41.2.2 yamt * The number of bytes we can safely extract from the uio
1497 1.41.2.2 yamt * depends on the available idx and buf space. Worst case,
1498 1.41.2.2 yamt * every byte is a message so 1 idx is required per byte.
1499 1.41.2.2 yamt * Worst case, the first byte completes a 3-byte msg in prior
1500 1.41.2.2 yamt * state, and every subsequent byte is a Program Change or
1501 1.41.2.2 yamt * Channel Pressure msg with running status and expands to 2
1502 1.41.2.2 yamt * bytes, so the buf space reqd is 3+2(n-1) or 2n+1. So limit
1503 1.41.2.2 yamt * the transfer to the min of idxspace and (bufspace-1)>>1.
1504 1.41.2.2 yamt */
1505 1.41.2.2 yamt xfrcount = (bufspace - 1) >> 1;
1506 1.41.2.2 yamt if ( xfrcount > idxspace )
1507 1.41.2.2 yamt xfrcount = idxspace;
1508 1.41.2.2 yamt if ( xfrcount > sizeof inp )
1509 1.41.2.2 yamt xfrcount = sizeof inp;
1510 1.41.2.2 yamt if ( xfrcount > uio->uio_resid )
1511 1.41.2.2 yamt xfrcount = uio->uio_resid;
1512 1.41.2.2 yamt
1513 1.41.2.2 yamt error = uiomove(inp, xfrcount, uio);
1514 1.1 augustss #ifdef MIDI_DEBUG
1515 1.1 augustss if (error)
1516 1.9 augustss printf("midi_write:(1) uiomove failed %d; "
1517 1.41.2.2 yamt "xfrcount=%d inp=%p\n",
1518 1.41.2.2 yamt error, xfrcount, inp);
1519 1.1 augustss #endif
1520 1.41.2.2 yamt if ( error )
1521 1.1 augustss break;
1522 1.41.2.2 yamt
1523 1.41.2.2 yamt /*
1524 1.41.2.2 yamt * The number of bytes we extracted being calculated to
1525 1.41.2.2 yamt * definitely fit in the buffer even with canonicalization,
1526 1.41.2.2 yamt * there is no excuse for real_writebytes to return EWOULDBLOCK.
1527 1.41.2.2 yamt */
1528 1.41.2.2 yamt error = real_writebytes(sc, inp, xfrcount);
1529 1.41.2.2 yamt KASSERT(error != EWOULDBLOCK);
1530 1.41.2.2 yamt
1531 1.41.2.2 yamt if ( error )
1532 1.41.2.2 yamt break;
1533 1.41.2.2 yamt /*
1534 1.41.2.2 yamt * If this is a polling device and we just sent a buffer, let's
1535 1.41.2.2 yamt * not send another without giving some other process a chance.
1536 1.41.2.2 yamt */
1537 1.41.2.2 yamt if ( ! (sc->props & MIDI_PROP_OUT_INTR) )
1538 1.41.2.2 yamt pollout = 1;
1539 1.41.2.4 yamt DPRINTFN(8,("midiwrite: uio_resid now %zu, props=%d\n",
1540 1.41.2.2 yamt uio->uio_resid, sc->props));
1541 1.1 augustss }
1542 1.1 augustss return error;
1543 1.41.2.2 yamt
1544 1.41.2.2 yamt locked_exit:
1545 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1546 1.41.2.2 yamt return error;
1547 1.5 augustss }
1548 1.5 augustss
1549 1.5 augustss /*
1550 1.9 augustss * This write routine is only called from sequencer code and expects
1551 1.5 augustss * a write that is smaller than the MIDI buffer.
1552 1.5 augustss */
1553 1.5 augustss int
1554 1.40 christos midi_writebytes(int unit, u_char *bf, int cc)
1555 1.5 augustss {
1556 1.5 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
1557 1.5 augustss
1558 1.41.2.2 yamt DPRINTFN(7, ("midi_writebytes: %p, unit=%d, cc=%d %#02x %#02x %#02x\n",
1559 1.41.2.2 yamt sc, unit, cc, bf[0], bf[1], bf[2]));
1560 1.41.2.2 yamt return real_writebytes(sc, bf, cc);
1561 1.1 augustss }
1562 1.1 augustss
1563 1.1 augustss int
1564 1.41.2.4 yamt midiioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1565 1.1 augustss {
1566 1.1 augustss int unit = MIDIUNIT(dev);
1567 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
1568 1.38 yamt const struct midi_hw_if *hw = sc->hw_if;
1569 1.1 augustss int error;
1570 1.41.2.2 yamt int s;
1571 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
1572 1.1 augustss
1573 1.41.2.2 yamt DPRINTFN(5,("midiioctl: %p cmd=0x%08lx\n", sc, cmd));
1574 1.18 tshiozak
1575 1.18 tshiozak if (sc->dying)
1576 1.18 tshiozak return EIO;
1577 1.18 tshiozak
1578 1.1 augustss error = 0;
1579 1.1 augustss switch (cmd) {
1580 1.1 augustss case FIONBIO:
1581 1.1 augustss /* All handled in the upper FS layer. */
1582 1.1 augustss break;
1583 1.41.2.2 yamt
1584 1.41.2.2 yamt case FIONREAD:
1585 1.41.2.2 yamt /*
1586 1.41.2.2 yamt * This code relies on the current implementation of midi_in
1587 1.41.2.2 yamt * always updating buf and idx together in a critical section,
1588 1.41.2.2 yamt * so buf always ends at a message boundary. Document this
1589 1.41.2.2 yamt * ioctl as always returning a value such that the last message
1590 1.41.2.2 yamt * included is complete (SysEx the only exception), and then
1591 1.41.2.2 yamt * make sure the implementation doesn't regress. NB that
1592 1.41.2.2 yamt * means if this ioctl returns n and the proc then issues a
1593 1.41.2.2 yamt * read of n, n bytes will be read, but if the proc issues a
1594 1.41.2.2 yamt * read of m < n, fewer than m bytes may be read to ensure the
1595 1.41.2.2 yamt * read ends at a message boundary.
1596 1.41.2.2 yamt */
1597 1.41.2.2 yamt MIDI_IN_LOCK(sc,s);
1598 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(&sc->inbuf,buf);
1599 1.41.2.2 yamt MIDI_IN_UNLOCK(sc,s);
1600 1.41.2.2 yamt *(int *)addr = buf_lim - buf_cur;
1601 1.41.2.2 yamt break;
1602 1.1 augustss
1603 1.1 augustss case FIOASYNC:
1604 1.1 augustss if (*(int *)addr) {
1605 1.1 augustss if (sc->async)
1606 1.1 augustss return EBUSY;
1607 1.41.2.1 yamt sc->async = l->l_proc;
1608 1.41.2.2 yamt DPRINTFN(5,("midi_ioctl: FIOASYNC %p\n", l->l_proc));
1609 1.1 augustss } else
1610 1.1 augustss sc->async = 0;
1611 1.1 augustss break;
1612 1.1 augustss
1613 1.1 augustss #if 0
1614 1.1 augustss case MIDI_PRETIME:
1615 1.1 augustss /* XXX OSS
1616 1.1 augustss * This should set up a read timeout, but that's
1617 1.1 augustss * why we have poll(), so there's nothing yet. */
1618 1.1 augustss error = EINVAL;
1619 1.1 augustss break;
1620 1.1 augustss #endif
1621 1.1 augustss
1622 1.4 augustss #ifdef MIDI_SAVE
1623 1.4 augustss case MIDI_GETSAVE:
1624 1.4 augustss error = copyout(&midisave, *(void **)addr, sizeof midisave);
1625 1.4 augustss break;
1626 1.4 augustss #endif
1627 1.4 augustss
1628 1.1 augustss default:
1629 1.1 augustss if (hw->ioctl)
1630 1.41.2.1 yamt error = hw->ioctl(sc->hw_hdl, cmd, addr, flag, l);
1631 1.1 augustss else
1632 1.1 augustss error = EINVAL;
1633 1.1 augustss break;
1634 1.1 augustss }
1635 1.1 augustss return error;
1636 1.1 augustss }
1637 1.1 augustss
1638 1.1 augustss int
1639 1.41.2.1 yamt midipoll(dev_t dev, int events, struct lwp *l)
1640 1.1 augustss {
1641 1.1 augustss int unit = MIDIUNIT(dev);
1642 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
1643 1.1 augustss int revents = 0;
1644 1.24 gson int s;
1645 1.41.2.2 yamt MIDI_BUF_DECLARE(idx);
1646 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
1647 1.1 augustss
1648 1.41.2.2 yamt DPRINTFN(6,("midipoll: %p events=0x%x\n", sc, events));
1649 1.1 augustss
1650 1.18 tshiozak if (sc->dying)
1651 1.41 ws return POLLHUP;
1652 1.24 gson
1653 1.24 gson s = splaudio();
1654 1.18 tshiozak
1655 1.41.2.2 yamt if ((sc->flags&FREAD) && (events & (POLLIN | POLLRDNORM))) {
1656 1.41.2.2 yamt simple_lock(&sc->in_lock);
1657 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(&sc->inbuf,idx);
1658 1.41.2.2 yamt if (idx_cur < idx_lim)
1659 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
1660 1.41.2.2 yamt else
1661 1.41.2.1 yamt selrecord(l, &sc->rsel);
1662 1.41.2.2 yamt simple_unlock(&sc->in_lock);
1663 1.41.2.2 yamt }
1664 1.1 augustss
1665 1.41.2.2 yamt if ((sc->flags&FWRITE) && (events & (POLLOUT | POLLWRNORM))) {
1666 1.41.2.2 yamt simple_lock(&sc->out_lock);
1667 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(&sc->outbuf,idx);
1668 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(&sc->outbuf,buf);
1669 1.41.2.2 yamt if ( idx_lim - idx_cur >= 1 && buf_lim - buf_cur >= 3 )
1670 1.41.2.2 yamt revents |= events & (POLLOUT | POLLWRNORM);
1671 1.41.2.2 yamt else
1672 1.41.2.1 yamt selrecord(l, &sc->wsel);
1673 1.41.2.2 yamt simple_unlock(&sc->out_lock);
1674 1.1 augustss }
1675 1.1 augustss
1676 1.1 augustss splx(s);
1677 1.1 augustss return revents;
1678 1.30 jdolecek }
1679 1.30 jdolecek
1680 1.30 jdolecek static void
1681 1.30 jdolecek filt_midirdetach(struct knote *kn)
1682 1.30 jdolecek {
1683 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
1684 1.30 jdolecek int s;
1685 1.30 jdolecek
1686 1.30 jdolecek s = splaudio();
1687 1.31 christos SLIST_REMOVE(&sc->rsel.sel_klist, kn, knote, kn_selnext);
1688 1.30 jdolecek splx(s);
1689 1.30 jdolecek }
1690 1.30 jdolecek
1691 1.30 jdolecek static int
1692 1.30 jdolecek filt_midiread(struct knote *kn, long hint)
1693 1.30 jdolecek {
1694 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
1695 1.41.2.2 yamt int s;
1696 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
1697 1.30 jdolecek
1698 1.30 jdolecek /* XXXLUKEM (thorpej): please make sure this is correct. */
1699 1.30 jdolecek
1700 1.41.2.2 yamt MIDI_IN_LOCK(sc,s);
1701 1.41.2.2 yamt MIDI_BUF_CONSUMER_INIT(&sc->inbuf,buf);
1702 1.41.2.2 yamt kn->kn_data = buf_lim - buf_cur;
1703 1.41.2.2 yamt MIDI_IN_UNLOCK(sc,s);
1704 1.30 jdolecek return (kn->kn_data > 0);
1705 1.30 jdolecek }
1706 1.30 jdolecek
1707 1.30 jdolecek static const struct filterops midiread_filtops =
1708 1.30 jdolecek { 1, NULL, filt_midirdetach, filt_midiread };
1709 1.30 jdolecek
1710 1.30 jdolecek static void
1711 1.30 jdolecek filt_midiwdetach(struct knote *kn)
1712 1.30 jdolecek {
1713 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
1714 1.30 jdolecek int s;
1715 1.30 jdolecek
1716 1.30 jdolecek s = splaudio();
1717 1.31 christos SLIST_REMOVE(&sc->wsel.sel_klist, kn, knote, kn_selnext);
1718 1.30 jdolecek splx(s);
1719 1.30 jdolecek }
1720 1.30 jdolecek
1721 1.30 jdolecek static int
1722 1.30 jdolecek filt_midiwrite(struct knote *kn, long hint)
1723 1.30 jdolecek {
1724 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
1725 1.41.2.2 yamt int s;
1726 1.41.2.2 yamt MIDI_BUF_DECLARE(idx);
1727 1.41.2.2 yamt MIDI_BUF_DECLARE(buf);
1728 1.30 jdolecek
1729 1.30 jdolecek /* XXXLUKEM (thorpej): please make sure this is correct. */
1730 1.30 jdolecek
1731 1.41.2.2 yamt MIDI_OUT_LOCK(sc,s);
1732 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(&sc->outbuf,idx);
1733 1.41.2.2 yamt MIDI_BUF_PRODUCER_INIT(&sc->outbuf,buf);
1734 1.41.2.2 yamt kn->kn_data = ((buf_lim - buf_cur)-1)>>1;
1735 1.41.2.2 yamt if ( kn->kn_data > idx_lim - idx_cur )
1736 1.41.2.2 yamt kn->kn_data = idx_lim - idx_cur;
1737 1.41.2.2 yamt MIDI_OUT_UNLOCK(sc,s);
1738 1.30 jdolecek return (kn->kn_data > 0);
1739 1.30 jdolecek }
1740 1.30 jdolecek
1741 1.30 jdolecek static const struct filterops midiwrite_filtops =
1742 1.30 jdolecek { 1, NULL, filt_midiwdetach, filt_midiwrite };
1743 1.30 jdolecek
1744 1.30 jdolecek int
1745 1.30 jdolecek midikqfilter(dev_t dev, struct knote *kn)
1746 1.30 jdolecek {
1747 1.30 jdolecek int unit = MIDIUNIT(dev);
1748 1.30 jdolecek struct midi_softc *sc = midi_cd.cd_devs[unit];
1749 1.30 jdolecek struct klist *klist;
1750 1.30 jdolecek int s;
1751 1.30 jdolecek
1752 1.30 jdolecek switch (kn->kn_filter) {
1753 1.30 jdolecek case EVFILT_READ:
1754 1.31 christos klist = &sc->rsel.sel_klist;
1755 1.30 jdolecek kn->kn_fop = &midiread_filtops;
1756 1.30 jdolecek break;
1757 1.30 jdolecek
1758 1.30 jdolecek case EVFILT_WRITE:
1759 1.31 christos klist = &sc->wsel.sel_klist;
1760 1.30 jdolecek kn->kn_fop = &midiwrite_filtops;
1761 1.30 jdolecek break;
1762 1.30 jdolecek
1763 1.30 jdolecek default:
1764 1.30 jdolecek return (1);
1765 1.30 jdolecek }
1766 1.30 jdolecek
1767 1.30 jdolecek kn->kn_hook = sc;
1768 1.30 jdolecek
1769 1.30 jdolecek s = splaudio();
1770 1.30 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1771 1.30 jdolecek splx(s);
1772 1.30 jdolecek
1773 1.30 jdolecek return (0);
1774 1.1 augustss }
1775 1.1 augustss
1776 1.1 augustss void
1777 1.22 augustss midi_getinfo(dev_t dev, struct midi_info *mi)
1778 1.1 augustss {
1779 1.1 augustss struct midi_softc *sc;
1780 1.1 augustss
1781 1.17 thorpej sc = device_lookup(&midi_cd, MIDIUNIT(dev));
1782 1.17 thorpej if (sc == NULL)
1783 1.18 tshiozak return;
1784 1.18 tshiozak if (sc->dying)
1785 1.1 augustss return;
1786 1.17 thorpej
1787 1.1 augustss sc->hw_if->getinfo(sc->hw_hdl, mi);
1788 1.4 augustss }
1789 1.4 augustss
1790 1.41.2.2 yamt #elif NMIDIBUS > 0 /* but NMIDI == 0 */
1791 1.41.2.2 yamt
1792 1.41.2.2 yamt void midi_register_hw_if_ext(struct midi_hw_if_ext *exthw) { /* stub */
1793 1.41.2.2 yamt }
1794 1.41.2.2 yamt
1795 1.10 drochner #endif /* NMIDI > 0 */
1796 1.10 drochner
1797 1.10 drochner #if NMIDI > 0 || NMIDIBUS > 0
1798 1.10 drochner
1799 1.22 augustss int audioprint(void *, const char *);
1800 1.4 augustss
1801 1.12 augustss struct device *
1802 1.38 yamt midi_attach_mi(const struct midi_hw_if *mhwp, void *hdlp, struct device *dev)
1803 1.4 augustss {
1804 1.4 augustss struct audio_attach_args arg;
1805 1.4 augustss
1806 1.4 augustss #ifdef DIAGNOSTIC
1807 1.4 augustss if (mhwp == NULL) {
1808 1.32 thorpej aprint_error("midi_attach_mi: NULL\n");
1809 1.12 augustss return (0);
1810 1.4 augustss }
1811 1.4 augustss #endif
1812 1.4 augustss arg.type = AUDIODEV_TYPE_MIDI;
1813 1.4 augustss arg.hwif = mhwp;
1814 1.4 augustss arg.hdl = hdlp;
1815 1.12 augustss return (config_found(dev, &arg, audioprint));
1816 1.1 augustss }
1817 1.1 augustss
1818 1.10 drochner #endif /* NMIDI > 0 || NMIDIBUS > 0 */
1819