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