midivar.h revision 1.15 1 1.15 cube /* $NetBSD: midivar.h,v 1.15 2008/03/04 21:56:11 cube 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.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 * 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.1 augustss
40 1.4 augustss #ifndef _SYS_DEV_MIDIVAR_H_
41 1.4 augustss #define _SYS_DEV_MIDIVAR_H_
42 1.4 augustss
43 1.3 augustss #define MIDI_BUFSIZE 1024
44 1.1 augustss
45 1.2 augustss #include "sequencer.h"
46 1.2 augustss
47 1.7 thorpej #include <sys/callout.h>
48 1.12 chap #include <sys/cdefs.h>
49 1.12 chap #include <sys/device.h>
50 1.14 ad #include <sys/simplelock.h>
51 1.7 thorpej
52 1.12 chap /*
53 1.12 chap * In both xmt and rcv direction, the midi_fst runs at the time data are
54 1.12 chap * buffered (midi_writebytes for xmt, midi_in for rcv) so what's in the
55 1.12 chap * buffer is always in canonical form (or compressed, on xmt, if the hw
56 1.12 chap * wants it that way). To preserve message boundaries for the buffer
57 1.12 chap * consumer, but allow transfers larger than one message, the buffer is
58 1.12 chap * split into a buf fork and an idx fork, where each byte of idx encodes
59 1.12 chap * the type and length of a message. Because messages are variable length,
60 1.12 chap * it is a guess how to set the relative sizes of idx and buf, or how many
61 1.12 chap * messages can be buffered before one or the other fills.
62 1.12 chap *
63 1.12 chap * The producer adds only complete messages to a buffer (except for SysEx
64 1.12 chap * messages, which have unpredictable length). A consumer serving byte-at-a-
65 1.12 chap * time hardware may partially consume a message, in which case it updates
66 1.12 chap * the length count at *idx_consumerp to reflect the remaining length of the
67 1.12 chap * message, only incrementing idx_consumerp when the message has been entirely
68 1.12 chap * consumed.
69 1.12 chap *
70 1.12 chap * The buffers are structured in the simple 1 reader 1 writer bounded buffer
71 1.12 chap * form, considered full when 1 unused byte remains. This should allow their
72 1.12 chap * use with minimal locking provided single pointer reads and writes can be
73 1.12 chap * assured atomic ... but then I chickened out on assuming that assurance, and
74 1.12 chap * added the extra locks to the code.
75 1.12 chap *
76 1.12 chap * Macros for manipulating the buffers:
77 1.12 chap *
78 1.12 chap * MIDI_BUF_DECLARE(frk) where frk is either buf or idx:
79 1.12 chap * declares the local variables frk_cur, frk_lim, frk_org, and frk_end.
80 1.12 chap *
81 1.12 chap * MIDI_BUF_CONSUMER_INIT(mb,frk)
82 1.12 chap * MIDI_BUF_PRODUCER_INIT(mb,frk)
83 1.12 chap * initializes frk_org and frk_end to the base and end (that is, address just
84 1.12 chap * past the last valid byte) of the buffer fork frk, frk_cur to the
85 1.12 chap * consumer's or producer's current position, respectively, and frk_lim to
86 1.12 chap * the current limit (for either consumer or producer, immediately following
87 1.12 chap * this macro, frk_lim-frk_cur gives the number of bytes to play with). That
88 1.12 chap * means frk_lim may actually point past the buffer; loops on the condition
89 1.12 chap * (frk_cur < frk_lim) must contain WRAP(frk) if proceeding byte-by-byte, or
90 1.12 chap * must explicitly handle wrapping around frk_end if doing anything clever.
91 1.12 chap * These are expression-shaped macros that have the value frk_lim. When used
92 1.12 chap * without locking--provided pointer reads and writes can be assumed atomic--
93 1.12 chap * these macros give a conservative estimate of what is available to consume
94 1.12 chap * or produce.
95 1.12 chap *
96 1.12 chap * MIDI_BUF_WRAP(frk)
97 1.12 chap * tests whether frk_cur == frk_end and, if so, wraps both frk_cur and
98 1.12 chap * frk_lim around the beginning of the buffer. Because the test is ==, it
99 1.12 chap * must be applied at each byte in a loop; if the loop is proceeding in
100 1.12 chap * bigger steps, the possibility of wrap must be coded for. This expression-
101 1.12 chap * shaped macro has the value of frk_cur after wrapping.
102 1.12 chap *
103 1.12 chap * MIDI_BUF_CONSUMER_REFRESH(mb,frk)
104 1.12 chap * MIDI_BUF_PRODUCER_REFRESH(mb,frk)
105 1.12 chap * refresh the local value frk_lim for a new snapshot of bytes available; an
106 1.12 chap * expression-shaped macro with the new value of frk_lim. Usually used after
107 1.12 chap * using up the first conservative estimate and obtaining a lock to get a
108 1.12 chap * final value. Used unlocked, just gives a more recent conservative estimate.
109 1.12 chap *
110 1.12 chap * MIDI_BUF_CONSUMER_WBACK(mb,frk)
111 1.12 chap * MIDI_BUF_PRODUCER_WBACK(mb,frk)
112 1.12 chap * write back the local copy of frk_cur to the buffer, after a barrier to
113 1.12 chap * ensure prior writes go first. Under the right atomicity conditions a
114 1.12 chap * producer could get away with using these unlocked, as long as the order
115 1.12 chap * is buf followed by idx. A consumer should update both in a critical
116 1.12 chap * section.
117 1.12 chap */
118 1.1 augustss struct midi_buffer {
119 1.12 chap u_char * __volatile idx_producerp;
120 1.12 chap u_char * __volatile idx_consumerp;
121 1.12 chap u_char * __volatile buf_producerp;
122 1.12 chap u_char * __volatile buf_consumerp;
123 1.12 chap u_char idx[MIDI_BUFSIZE/3];
124 1.12 chap u_char buf[MIDI_BUFSIZE-MIDI_BUFSIZE/3];
125 1.1 augustss };
126 1.12 chap #define MIDI_BUF_DECLARE(frk) \
127 1.12 chap u_char *__CONCAT(frk,_cur); \
128 1.12 chap u_char *__CONCAT(frk,_lim); \
129 1.12 chap u_char *__CONCAT(frk,_org); \
130 1.12 chap u_char *__CONCAT(frk,_end)
131 1.12 chap
132 1.12 chap #define MIDI_BUF_CONSUMER_REFRESH(mb,frk) \
133 1.12 chap ((__CONCAT(frk,_lim)=(mb)->__CONCAT(frk,_producerp)), \
134 1.12 chap __CONCAT(frk,_lim) < __CONCAT(frk,_cur) ? \
135 1.12 chap (__CONCAT(frk,_lim) += sizeof (mb)->frk) : __CONCAT(frk,_lim))
136 1.12 chap
137 1.12 chap #define MIDI_BUF_PRODUCER_REFRESH(mb,frk) \
138 1.12 chap ((__CONCAT(frk,_lim)=(mb)->__CONCAT(frk,_consumerp)-1), \
139 1.12 chap __CONCAT(frk,_lim) < __CONCAT(frk,_cur) ? \
140 1.12 chap (__CONCAT(frk,_lim) += sizeof (mb)->frk) : __CONCAT(frk,_lim))
141 1.12 chap
142 1.12 chap #define MIDI_BUF_EXTENT_INIT(mb,frk) \
143 1.12 chap ((__CONCAT(frk,_org)=(mb)->frk), \
144 1.12 chap (__CONCAT(frk,_end)=__CONCAT(frk,_org)+sizeof (mb)->frk))
145 1.12 chap
146 1.12 chap #define MIDI_BUF_CONSUMER_INIT(mb,frk) \
147 1.12 chap (MIDI_BUF_EXTENT_INIT((mb),frk), \
148 1.12 chap (__CONCAT(frk,_cur)=(mb)->__CONCAT(frk,_consumerp)), \
149 1.12 chap MIDI_BUF_CONSUMER_REFRESH((mb),frk))
150 1.12 chap
151 1.12 chap #define MIDI_BUF_PRODUCER_INIT(mb,frk) \
152 1.12 chap (MIDI_BUF_EXTENT_INIT((mb),frk), \
153 1.12 chap (__CONCAT(frk,_cur)=(mb)->__CONCAT(frk,_producerp)), \
154 1.12 chap MIDI_BUF_PRODUCER_REFRESH((mb),frk))
155 1.12 chap
156 1.12 chap #define MIDI_BUF_WRAP(frk) \
157 1.12 chap (__predict_false(__CONCAT(frk,_cur)==__CONCAT(frk,_end)) ? (\
158 1.12 chap (__CONCAT(frk,_lim)-=__CONCAT(frk,_end)-__CONCAT(frk,_org)), \
159 1.12 chap (__CONCAT(frk,_cur)=__CONCAT(frk,_org))) : __CONCAT(frk,_cur))
160 1.12 chap
161 1.12 chap #define MIDI_BUF_CONSUMER_WBACK(mb,frk) do { \
162 1.12 chap __insn_barrier(); \
163 1.12 chap (mb)->__CONCAT(frk,_consumerp)=__CONCAT(frk,_cur); \
164 1.12 chap } while (/*CONSTCOND*/0)
165 1.12 chap
166 1.12 chap #define MIDI_BUF_PRODUCER_WBACK(mb,frk) do { \
167 1.12 chap __insn_barrier(); \
168 1.12 chap (mb)->__CONCAT(frk,_producerp)=__CONCAT(frk,_cur); \
169 1.12 chap } while (/*CONSTCOND*/0)
170 1.12 chap
171 1.1 augustss
172 1.1 augustss #define MIDI_MAX_WRITE 32 /* max bytes written with busy wait */
173 1.1 augustss #define MIDI_WAIT 10000 /* microseconds to wait after busy wait */
174 1.1 augustss
175 1.12 chap struct midi_state {
176 1.12 chap struct evcnt bytesDiscarded;
177 1.12 chap struct evcnt incompleteMessages;
178 1.12 chap struct {
179 1.12 chap uint32_t bytesDiscarded;
180 1.12 chap uint32_t incompleteMessages;
181 1.12 chap } atOpen,
182 1.12 chap atQuery;
183 1.12 chap int state;
184 1.12 chap u_char *pos;
185 1.12 chap u_char *end;
186 1.12 chap u_char msg[3];
187 1.12 chap };
188 1.12 chap
189 1.1 augustss struct midi_softc {
190 1.15 cube device_t dev;
191 1.1 augustss void *hw_hdl; /* Hardware driver handle */
192 1.12 chap const struct midi_hw_if *hw_if; /* Hardware interface */
193 1.12 chap const struct midi_hw_if_ext *hw_if_ext; /* see midi_if.h */
194 1.1 augustss struct device *sc_dev; /* Hardware device struct */
195 1.1 augustss int isopen; /* Open indicator */
196 1.1 augustss int flags; /* Open flags */
197 1.8 tshiozak int dying;
198 1.1 augustss struct midi_buffer outbuf;
199 1.1 augustss struct midi_buffer inbuf;
200 1.1 augustss int props;
201 1.1 augustss int rchan, wchan;
202 1.12 chap struct simplelock out_lock; /* overkill or no? */
203 1.12 chap struct simplelock in_lock;
204 1.12 chap
205 1.12 chap #define MIDI_OUT_LOCK(sc,s) \
206 1.12 chap do { \
207 1.12 chap (s) = splaudio(); \
208 1.12 chap simple_lock(&(sc)->out_lock); \
209 1.12 chap } while (/*CONSTCOND*/0)
210 1.12 chap #define MIDI_OUT_UNLOCK(sc,s) \
211 1.12 chap do { \
212 1.12 chap simple_unlock(&(sc)->out_lock); \
213 1.12 chap splx((s)); \
214 1.12 chap } while (/*CONSTCOND*/0)
215 1.12 chap #define MIDI_IN_LOCK(sc,s) \
216 1.12 chap do { \
217 1.12 chap (s) = splaudio(); \
218 1.12 chap simple_lock(&(sc)->in_lock); \
219 1.12 chap } while (/*CONSTCOND*/0)
220 1.12 chap #define MIDI_IN_UNLOCK(sc,s) \
221 1.12 chap do { \
222 1.12 chap simple_unlock(&(sc)->in_lock); \
223 1.12 chap splx((s)); \
224 1.12 chap } while (/*CONSTCOND*/0)
225 1.12 chap
226 1.1 augustss int pbus;
227 1.12 chap int rcv_expect_asense;
228 1.12 chap int rcv_quiescent;
229 1.12 chap int rcv_eof;
230 1.1 augustss struct selinfo wsel; /* write selector */
231 1.1 augustss struct selinfo rsel; /* read selector */
232 1.1 augustss struct proc *async; /* process who wants audio SIGIO */
233 1.13 ad void *sih_rd;
234 1.13 ad void *sih_wr;
235 1.7 thorpej
236 1.12 chap struct callout xmt_asense_co;
237 1.12 chap struct callout rcv_asense_co;
238 1.1 augustss
239 1.12 chap /* MIDI input state machine; states are *s of 4 to allow | CAT bits */
240 1.12 chap struct midi_state rcv;
241 1.12 chap struct midi_state xmt;
242 1.12 chap #define MIDI_IN_START 0
243 1.12 chap #define MIDI_IN_RUN0_1 4
244 1.12 chap #define MIDI_IN_RUN1_1 8
245 1.12 chap #define MIDI_IN_RUN0_2 12
246 1.12 chap #define MIDI_IN_RUN1_2 16
247 1.12 chap #define MIDI_IN_RUN2_2 20
248 1.12 chap #define MIDI_IN_COM0_1 24
249 1.12 chap #define MIDI_IN_COM0_2 28
250 1.12 chap #define MIDI_IN_COM1_2 32
251 1.12 chap #define MIDI_IN_SYX1_3 36
252 1.12 chap #define MIDI_IN_SYX2_3 40
253 1.12 chap #define MIDI_IN_SYX0_3 44
254 1.12 chap #define MIDI_IN_RNX0_1 48
255 1.12 chap #define MIDI_IN_RNX0_2 52
256 1.12 chap #define MIDI_IN_RNX1_2 56
257 1.12 chap #define MIDI_IN_RNY1_2 60 /* not needed except for accurate error counts */
258 1.12 chap /*
259 1.12 chap * Four more states are needed to model the equivalence of NoteOff vel. 64
260 1.12 chap * and NoteOn vel. 0 for canonicalization or compression. In each of these 4
261 1.12 chap * states, we know the last message input and output was a NoteOn or a NoteOff.
262 1.12 chap */
263 1.12 chap #define MIDI_IN_RXX2_2 64 /* last output == msg[0] != last input */
264 1.12 chap #define MIDI_IN_RXX0_2 68 /* last output != msg[0] == this input */
265 1.12 chap #define MIDI_IN_RXX1_2 72 /* " */
266 1.12 chap #define MIDI_IN_RXY1_2 76 /* variant of RXX1_2 needed for error count only */
267 1.12 chap
268 1.12 chap #define MIDI_CAT_DATA 0
269 1.12 chap #define MIDI_CAT_STATUS1 1
270 1.12 chap #define MIDI_CAT_STATUS2 2
271 1.12 chap #define MIDI_CAT_COMMON 3
272 1.1 augustss
273 1.1 augustss #if NSEQUENCER > 0
274 1.1 augustss /* Synthesizer emulation stuff */
275 1.1 augustss int seqopen;
276 1.5 augustss struct midi_dev *seq_md; /* structure that links us with the seq. */
277 1.1 augustss #endif
278 1.1 augustss };
279 1.1 augustss
280 1.1 augustss #define MIDIUNIT(d) ((d) & 0xff)
281 1.1 augustss
282 1.4 augustss #endif /* _SYS_DEV_MIDIVAR_H_ */
283