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