Home | History | Annotate | Line # | Download | only in dev
midivar.h revision 1.11.14.7
      1 /*	$NetBSD: midivar.h,v 1.11.14.7 2006/05/20 03:17:43 chap 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 (at) NetBSD.org) and (midi FST refactoring and
      9  * Active Sense) Chapman Flack <nblists (at) anastigmatix.net>.
     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 	int     state;
    179 	u_char *pos;
    180 	u_char *end;
    181 	u_char  msg[3];
    182 };
    183 
    184 struct midi_softc {
    185 	struct	device dev;
    186 	void	*hw_hdl;	/* Hardware driver handle */
    187 	struct	midi_hw_if *hw_if; /* Hardware interface */
    188 	struct	device *sc_dev;	/* Hardware device struct */
    189 	int	isopen;		/* Open indicator */
    190 	int	flags;		/* Open flags */
    191 	int	dying;
    192 	struct	midi_buffer outbuf;
    193 	struct	midi_buffer inbuf;
    194 	int	props;
    195 	int	rchan, wchan;
    196 	struct  simplelock out_lock; /* overkill or no? */
    197 	struct  simplelock in_lock;
    198 
    199 #define MIDI_OUT_LOCK(sc,s) \
    200 	do { \
    201 		(s) = splaudio(); \
    202 		simple_lock(&(sc)->out_lock); \
    203 	} while (/*CONSTCOND*/0)
    204 #define MIDI_OUT_UNLOCK(sc,s) \
    205 	do { \
    206 		simple_unlock(&(sc)->out_lock); \
    207 		splx((s)); \
    208 	} while (/*CONSTCOND*/0)
    209 #define MIDI_IN_LOCK(sc,s) \
    210 	do { \
    211 		(s) = splaudio(); \
    212 		simple_lock(&(sc)->in_lock); \
    213 	} while (/*CONSTCOND*/0)
    214 #define MIDI_IN_UNLOCK(sc,s) \
    215 	do { \
    216 		simple_unlock(&(sc)->in_lock); \
    217 		splx((s)); \
    218 	} while (/*CONSTCOND*/0)
    219 
    220 	int	pbus;
    221 	struct	selinfo wsel;	/* write selector */
    222 	struct	selinfo rsel;	/* read selector */
    223 	struct	proc *async;	/* process who wants audio SIGIO */
    224 
    225 	struct callout sc_callout;
    226 
    227 	/* MIDI input state machine; states are *s of 4 to allow | CAT bits */
    228 	struct midi_state rcv;
    229 	struct midi_state xmt;
    230 #define MIDI_IN_START	0
    231 #define MIDI_IN_RUN0_1	4
    232 #define MIDI_IN_RUN1_1	8
    233 #define MIDI_IN_RUN0_2 12
    234 #define MIDI_IN_RUN1_2 16
    235 #define MIDI_IN_RUN2_2 20
    236 #define MIDI_IN_COM0_1 24
    237 #define MIDI_IN_COM0_2 28
    238 #define MIDI_IN_COM1_2 32
    239 #define MIDI_IN_SYX1_3 36
    240 #define MIDI_IN_SYX2_3 40
    241 #define MIDI_IN_SYX0_3 44
    242 #define MIDI_IN_RNX0_1 48
    243 #define MIDI_IN_RNX0_2 52
    244 #define MIDI_IN_RNX1_2 56
    245 #define MIDI_IN_RNY1_2 60 /* not needed except for accurate error counts */
    246 
    247 #define MIDI_CAT_DATA 0
    248 #define MIDI_CAT_STATUS1 1
    249 #define MIDI_CAT_STATUS2 2
    250 #define MIDI_CAT_COMMON 3
    251 
    252 #if NSEQUENCER > 0
    253 	/* Synthesizer emulation stuff */
    254 	int	seqopen;
    255 	struct	midi_dev *seq_md; /* structure that links us with the seq. */
    256 #endif
    257 };
    258 
    259 #define MIDIUNIT(d) ((d) & 0xff)
    260 
    261 #endif /* _SYS_DEV_MIDIVAR_H_ */
    262