midivar.h revision 1.20 1 1.20 mrg /* $NetBSD: midivar.h,v 1.20 2014/12/22 07:02:22 mrg Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.18 jmcneill * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.6 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.12 chap * by Lennart Augustsson (augustss (at) NetBSD.org) and (midi FST refactoring and
9 1.12 chap * Active Sense) 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 *
20 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
31 1.1 augustss */
32 1.1 augustss
33 1.4 augustss #ifndef _SYS_DEV_MIDIVAR_H_
34 1.4 augustss #define _SYS_DEV_MIDIVAR_H_
35 1.4 augustss
36 1.3 augustss #define MIDI_BUFSIZE 1024
37 1.1 augustss
38 1.7 thorpej #include <sys/callout.h>
39 1.12 chap #include <sys/cdefs.h>
40 1.12 chap #include <sys/device.h>
41 1.18 jmcneill #include <sys/condvar.h>
42 1.18 jmcneill #include <sys/mutex.h>
43 1.7 thorpej
44 1.12 chap /*
45 1.12 chap * In both xmt and rcv direction, the midi_fst runs at the time data are
46 1.12 chap * buffered (midi_writebytes for xmt, midi_in for rcv) so what's in the
47 1.12 chap * buffer is always in canonical form (or compressed, on xmt, if the hw
48 1.12 chap * wants it that way). To preserve message boundaries for the buffer
49 1.12 chap * consumer, but allow transfers larger than one message, the buffer is
50 1.12 chap * split into a buf fork and an idx fork, where each byte of idx encodes
51 1.12 chap * the type and length of a message. Because messages are variable length,
52 1.12 chap * it is a guess how to set the relative sizes of idx and buf, or how many
53 1.12 chap * messages can be buffered before one or the other fills.
54 1.12 chap *
55 1.12 chap * The producer adds only complete messages to a buffer (except for SysEx
56 1.12 chap * messages, which have unpredictable length). A consumer serving byte-at-a-
57 1.12 chap * time hardware may partially consume a message, in which case it updates
58 1.12 chap * the length count at *idx_consumerp to reflect the remaining length of the
59 1.12 chap * message, only incrementing idx_consumerp when the message has been entirely
60 1.12 chap * consumed.
61 1.12 chap *
62 1.12 chap * The buffers are structured in the simple 1 reader 1 writer bounded buffer
63 1.12 chap * form, considered full when 1 unused byte remains. This should allow their
64 1.12 chap * use with minimal locking provided single pointer reads and writes can be
65 1.12 chap * assured atomic ... but then I chickened out on assuming that assurance, and
66 1.12 chap * added the extra locks to the code.
67 1.12 chap *
68 1.12 chap * Macros for manipulating the buffers:
69 1.12 chap *
70 1.12 chap * MIDI_BUF_DECLARE(frk) where frk is either buf or idx:
71 1.12 chap * declares the local variables frk_cur, frk_lim, frk_org, and frk_end.
72 1.12 chap *
73 1.12 chap * MIDI_BUF_CONSUMER_INIT(mb,frk)
74 1.12 chap * MIDI_BUF_PRODUCER_INIT(mb,frk)
75 1.12 chap * initializes frk_org and frk_end to the base and end (that is, address just
76 1.12 chap * past the last valid byte) of the buffer fork frk, frk_cur to the
77 1.12 chap * consumer's or producer's current position, respectively, and frk_lim to
78 1.12 chap * the current limit (for either consumer or producer, immediately following
79 1.12 chap * this macro, frk_lim-frk_cur gives the number of bytes to play with). That
80 1.12 chap * means frk_lim may actually point past the buffer; loops on the condition
81 1.12 chap * (frk_cur < frk_lim) must contain WRAP(frk) if proceeding byte-by-byte, or
82 1.12 chap * must explicitly handle wrapping around frk_end if doing anything clever.
83 1.12 chap * These are expression-shaped macros that have the value frk_lim. When used
84 1.12 chap * without locking--provided pointer reads and writes can be assumed atomic--
85 1.12 chap * these macros give a conservative estimate of what is available to consume
86 1.12 chap * or produce.
87 1.12 chap *
88 1.12 chap * MIDI_BUF_WRAP(frk)
89 1.12 chap * tests whether frk_cur == frk_end and, if so, wraps both frk_cur and
90 1.12 chap * frk_lim around the beginning of the buffer. Because the test is ==, it
91 1.12 chap * must be applied at each byte in a loop; if the loop is proceeding in
92 1.12 chap * bigger steps, the possibility of wrap must be coded for. This expression-
93 1.12 chap * shaped macro has the value of frk_cur after wrapping.
94 1.12 chap *
95 1.12 chap * MIDI_BUF_CONSUMER_REFRESH(mb,frk)
96 1.12 chap * MIDI_BUF_PRODUCER_REFRESH(mb,frk)
97 1.12 chap * refresh the local value frk_lim for a new snapshot of bytes available; an
98 1.12 chap * expression-shaped macro with the new value of frk_lim. Usually used after
99 1.12 chap * using up the first conservative estimate and obtaining a lock to get a
100 1.12 chap * final value. Used unlocked, just gives a more recent conservative estimate.
101 1.12 chap *
102 1.12 chap * MIDI_BUF_CONSUMER_WBACK(mb,frk)
103 1.12 chap * MIDI_BUF_PRODUCER_WBACK(mb,frk)
104 1.12 chap * write back the local copy of frk_cur to the buffer, after a barrier to
105 1.12 chap * ensure prior writes go first. Under the right atomicity conditions a
106 1.12 chap * producer could get away with using these unlocked, as long as the order
107 1.12 chap * is buf followed by idx. A consumer should update both in a critical
108 1.12 chap * section.
109 1.12 chap */
110 1.1 augustss struct midi_buffer {
111 1.12 chap u_char * __volatile idx_producerp;
112 1.12 chap u_char * __volatile idx_consumerp;
113 1.12 chap u_char * __volatile buf_producerp;
114 1.12 chap u_char * __volatile buf_consumerp;
115 1.12 chap u_char idx[MIDI_BUFSIZE/3];
116 1.12 chap u_char buf[MIDI_BUFSIZE-MIDI_BUFSIZE/3];
117 1.1 augustss };
118 1.12 chap #define MIDI_BUF_DECLARE(frk) \
119 1.12 chap u_char *__CONCAT(frk,_cur); \
120 1.12 chap u_char *__CONCAT(frk,_lim); \
121 1.12 chap u_char *__CONCAT(frk,_org); \
122 1.12 chap u_char *__CONCAT(frk,_end)
123 1.12 chap
124 1.12 chap #define MIDI_BUF_CONSUMER_REFRESH(mb,frk) \
125 1.12 chap ((__CONCAT(frk,_lim)=(mb)->__CONCAT(frk,_producerp)), \
126 1.12 chap __CONCAT(frk,_lim) < __CONCAT(frk,_cur) ? \
127 1.12 chap (__CONCAT(frk,_lim) += sizeof (mb)->frk) : __CONCAT(frk,_lim))
128 1.12 chap
129 1.12 chap #define MIDI_BUF_PRODUCER_REFRESH(mb,frk) \
130 1.12 chap ((__CONCAT(frk,_lim)=(mb)->__CONCAT(frk,_consumerp)-1), \
131 1.12 chap __CONCAT(frk,_lim) < __CONCAT(frk,_cur) ? \
132 1.12 chap (__CONCAT(frk,_lim) += sizeof (mb)->frk) : __CONCAT(frk,_lim))
133 1.12 chap
134 1.12 chap #define MIDI_BUF_EXTENT_INIT(mb,frk) \
135 1.12 chap ((__CONCAT(frk,_org)=(mb)->frk), \
136 1.12 chap (__CONCAT(frk,_end)=__CONCAT(frk,_org)+sizeof (mb)->frk))
137 1.12 chap
138 1.12 chap #define MIDI_BUF_CONSUMER_INIT(mb,frk) \
139 1.12 chap (MIDI_BUF_EXTENT_INIT((mb),frk), \
140 1.12 chap (__CONCAT(frk,_cur)=(mb)->__CONCAT(frk,_consumerp)), \
141 1.12 chap MIDI_BUF_CONSUMER_REFRESH((mb),frk))
142 1.12 chap
143 1.12 chap #define MIDI_BUF_PRODUCER_INIT(mb,frk) \
144 1.12 chap (MIDI_BUF_EXTENT_INIT((mb),frk), \
145 1.12 chap (__CONCAT(frk,_cur)=(mb)->__CONCAT(frk,_producerp)), \
146 1.12 chap MIDI_BUF_PRODUCER_REFRESH((mb),frk))
147 1.12 chap
148 1.12 chap #define MIDI_BUF_WRAP(frk) \
149 1.12 chap (__predict_false(__CONCAT(frk,_cur)==__CONCAT(frk,_end)) ? (\
150 1.12 chap (__CONCAT(frk,_lim)-=__CONCAT(frk,_end)-__CONCAT(frk,_org)), \
151 1.12 chap (__CONCAT(frk,_cur)=__CONCAT(frk,_org))) : __CONCAT(frk,_cur))
152 1.12 chap
153 1.12 chap #define MIDI_BUF_CONSUMER_WBACK(mb,frk) do { \
154 1.12 chap __insn_barrier(); \
155 1.12 chap (mb)->__CONCAT(frk,_consumerp)=__CONCAT(frk,_cur); \
156 1.12 chap } while (/*CONSTCOND*/0)
157 1.12 chap
158 1.12 chap #define MIDI_BUF_PRODUCER_WBACK(mb,frk) do { \
159 1.12 chap __insn_barrier(); \
160 1.12 chap (mb)->__CONCAT(frk,_producerp)=__CONCAT(frk,_cur); \
161 1.12 chap } while (/*CONSTCOND*/0)
162 1.12 chap
163 1.1 augustss
164 1.1 augustss #define MIDI_MAX_WRITE 32 /* max bytes written with busy wait */
165 1.1 augustss #define MIDI_WAIT 10000 /* microseconds to wait after busy wait */
166 1.1 augustss
167 1.12 chap struct midi_state {
168 1.12 chap struct evcnt bytesDiscarded;
169 1.12 chap struct evcnt incompleteMessages;
170 1.12 chap struct {
171 1.12 chap uint32_t bytesDiscarded;
172 1.12 chap uint32_t incompleteMessages;
173 1.12 chap } atOpen,
174 1.12 chap atQuery;
175 1.12 chap int state;
176 1.12 chap u_char *pos;
177 1.12 chap u_char *end;
178 1.12 chap u_char msg[3];
179 1.12 chap };
180 1.12 chap
181 1.1 augustss struct midi_softc {
182 1.19 plunky device_t dev; /* Hardware device struct */
183 1.1 augustss void *hw_hdl; /* Hardware driver handle */
184 1.12 chap const struct midi_hw_if *hw_if; /* Hardware interface */
185 1.12 chap const struct midi_hw_if_ext *hw_if_ext; /* see midi_if.h */
186 1.1 augustss int isopen; /* Open indicator */
187 1.1 augustss int flags; /* Open flags */
188 1.8 tshiozak int dying;
189 1.1 augustss struct midi_buffer outbuf;
190 1.1 augustss struct midi_buffer inbuf;
191 1.1 augustss int props;
192 1.20 mrg int refcnt;
193 1.20 mrg kcondvar_t detach_cv;
194 1.18 jmcneill kcondvar_t rchan;
195 1.18 jmcneill kcondvar_t wchan;
196 1.18 jmcneill kmutex_t *lock;
197 1.1 augustss int pbus;
198 1.12 chap int rcv_expect_asense;
199 1.12 chap int rcv_quiescent;
200 1.12 chap int rcv_eof;
201 1.1 augustss struct selinfo wsel; /* write selector */
202 1.1 augustss struct selinfo rsel; /* read selector */
203 1.18 jmcneill pid_t async; /* process who wants audio SIGIO */
204 1.18 jmcneill void *sih;
205 1.7 thorpej
206 1.12 chap struct callout xmt_asense_co;
207 1.12 chap struct callout rcv_asense_co;
208 1.1 augustss
209 1.12 chap /* MIDI input state machine; states are *s of 4 to allow | CAT bits */
210 1.12 chap struct midi_state rcv;
211 1.12 chap struct midi_state xmt;
212 1.12 chap #define MIDI_IN_START 0
213 1.12 chap #define MIDI_IN_RUN0_1 4
214 1.12 chap #define MIDI_IN_RUN1_1 8
215 1.12 chap #define MIDI_IN_RUN0_2 12
216 1.12 chap #define MIDI_IN_RUN1_2 16
217 1.12 chap #define MIDI_IN_RUN2_2 20
218 1.12 chap #define MIDI_IN_COM0_1 24
219 1.12 chap #define MIDI_IN_COM0_2 28
220 1.12 chap #define MIDI_IN_COM1_2 32
221 1.12 chap #define MIDI_IN_SYX1_3 36
222 1.12 chap #define MIDI_IN_SYX2_3 40
223 1.12 chap #define MIDI_IN_SYX0_3 44
224 1.12 chap #define MIDI_IN_RNX0_1 48
225 1.12 chap #define MIDI_IN_RNX0_2 52
226 1.12 chap #define MIDI_IN_RNX1_2 56
227 1.12 chap #define MIDI_IN_RNY1_2 60 /* not needed except for accurate error counts */
228 1.12 chap /*
229 1.12 chap * Four more states are needed to model the equivalence of NoteOff vel. 64
230 1.12 chap * and NoteOn vel. 0 for canonicalization or compression. In each of these 4
231 1.12 chap * states, we know the last message input and output was a NoteOn or a NoteOff.
232 1.12 chap */
233 1.12 chap #define MIDI_IN_RXX2_2 64 /* last output == msg[0] != last input */
234 1.12 chap #define MIDI_IN_RXX0_2 68 /* last output != msg[0] == this input */
235 1.12 chap #define MIDI_IN_RXX1_2 72 /* " */
236 1.12 chap #define MIDI_IN_RXY1_2 76 /* variant of RXX1_2 needed for error count only */
237 1.12 chap
238 1.12 chap #define MIDI_CAT_DATA 0
239 1.12 chap #define MIDI_CAT_STATUS1 1
240 1.12 chap #define MIDI_CAT_STATUS2 2
241 1.12 chap #define MIDI_CAT_COMMON 3
242 1.1 augustss
243 1.1 augustss /* Synthesizer emulation stuff */
244 1.1 augustss int seqopen;
245 1.5 augustss struct midi_dev *seq_md; /* structure that links us with the seq. */
246 1.1 augustss };
247 1.1 augustss
248 1.1 augustss #define MIDIUNIT(d) ((d) & 0xff)
249 1.1 augustss
250 1.4 augustss #endif /* _SYS_DEV_MIDIVAR_H_ */
251