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