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