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