xref: /freebsd/sys/dev/sound/midi/midi.h (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
1 /*
2  * Include file for midi driver.
3  *
4  * Copyright by Seigo Tanimura 1999.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  *
29  */
30 
31 /*
32  * first, include kernel header files.
33  */
34 
35 #ifndef _MIDI_H_
36 #define _MIDI_H_
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/ioccom.h>
41 
42 #include <sys/filio.h>
43 #include <sys/lock.h>
44 #include <sys/sockio.h>
45 #include <sys/fcntl.h>
46 #include <sys/tty.h>
47 #include <sys/proc.h>
48 
49 #include <sys/kernel.h> /* for DATA_SET */
50 
51 #include <sys/module.h>
52 #include <sys/conf.h>
53 #include <sys/file.h>
54 #include <sys/uio.h>
55 #include <sys/syslog.h>
56 #include <sys/errno.h>
57 #include <sys/malloc.h>
58 #include <sys/bus.h>
59 #include <machine/clock.h>	/* for DELAY */
60 #include <machine/resource.h>
61 #include <machine/bus_memio.h>
62 #include <machine/bus_pio.h>
63 #include <machine/bus.h>
64 #include <machine/clock.h>	/* for DELAY */
65 #include <sys/soundcard.h>
66 #include <sys/rman.h>
67 #include <sys/mman.h>
68 #include <sys/poll.h>
69 #include <sys/mutex.h>
70 
71 #include <dev/sound/midi/miditypes.h>
72 #include <dev/sound/midi/midibuf.h>
73 #include <dev/sound/midi/midisynth.h>
74 
75 #define MIDI_CDEV_MAJOR 30
76 
77 /*#define MIDI_OUTOFGIANT*/
78 
79 /*
80  * The order of mutex lock (from the first to the last)
81  *
82  * 1. sequencer flags, queues, timer and device list
83  * 2. midi synth voice and channel
84  * 3. midi synth status
85  * 4. generic midi flags and queues
86  * 5. midi device
87  */
88 
89 /*
90  * descriptor of midi operations ...
91  *
92  */
93 
94 struct _mididev_info {
95 
96 	/*
97 	 * the first part of the descriptor is filled up from a
98 	 * template.
99 	 */
100 	char name[64];
101 
102 	int type;
103 
104 	d_open_t *open;
105 	d_close_t *close;
106 	d_ioctl_t *ioctl;
107 	midi_callback_t *callback;
108 
109 	/*
110 	 * combinations of the following flags are used as second argument in
111 	 * the callback from the dma module to the device-specific routines.
112 	 */
113 
114 #define MIDI_CB_RD       0x100   /* read callback */
115 #define MIDI_CB_WR       0x200   /* write callback */
116 #define MIDI_CB_REASON_MASK      0xff
117 #define MIDI_CB_START    0x01   /* start dma op */
118 #define MIDI_CB_STOP     0x03   /* stop dma op */
119 #define MIDI_CB_ABORT    0x04   /* abort dma op */
120 #define MIDI_CB_INIT     0x05   /* init board parameters */
121 
122 	/*
123 	 * callback extensions
124 	 */
125 #define MIDI_CB_DMADONE         0x10
126 #define MIDI_CB_DMAUPDATE       0x11
127 #define MIDI_CB_DMASTOP         0x12
128 
129 	/* init can only be called with int enabled and
130 	 * no pending DMA activity.
131 	 */
132 
133 	/*
134 	 * whereas from here, parameters are set at runtime.
135 	 * resources are stored in the softc of the device,
136 	 * not in the common structure.
137 	 */
138 
139 	int unit; /* unit number of the device */
140 	void *softc; /* softc for the device */
141 	device_t dev; /* device_t for the device */
142 
143 	int bd_id ;     /* used to hold board-id info, eg. sb version,
144 			 * mss codec type, etc. etc.
145 			 */
146 
147 	struct mtx flagqueue_mtx; /* Mutex to protect flags and queues */
148 
149 	/* Queues */
150 	midi_dbuf midi_dbuf_in; /* midi input event/message queue */
151 	midi_dbuf midi_dbuf_out; /* midi output event/message queue */
152 	midi_dbuf midi_dbuf_passthru; /* midi passthru event/message queue */
153 
154         /*
155          * these parameters describe the operation of the board.
156          * Generic things like busy flag, speed, etc are here.
157          */
158 
159 	/* Flags */
160 	volatile u_long  flags ;     /* 32 bits, used for various purposes. */
161 	int fflags; /* file flag */
162 
163 	/*
164 	 * we have separate flags for read and write, although in some
165 	 * cases this is probably not necessary (e.g. because we cannot
166 	 * know how many processes are using the device, we cannot
167 	 * distinguish if open, close, abort are for a write or for a
168 	 * read).
169 	 */
170 
171 	/*
172 	 * the following flag is used by open-close routines
173 	 * to mark the status of the device.
174 	 */
175 #define MIDI_F_BUSY              0x0001  /* has been opened 	*/
176 	/*
177 	 * the next two are used to allow only one pending operation of
178 	 * each type.
179 	 */
180 #define MIDI_F_READING           0x0004  /* have a pending read */
181 #define MIDI_F_WRITING           0x0008  /* have a pending write */
182 
183 	/*
184 	 * flag used to mark a pending close.
185 	 */
186 #define MIDI_F_CLOSING           0x0040  /* a pending close */
187 
188 	/*
189 	 * if user has not set block size, then make it adaptive
190 	 * (0.25s, or the perhaps last read/write ?)
191 	 */
192 #define	MIDI_F_HAS_SIZE		0x0080	/* user set block size */
193 	/*
194 	 * assorted flags related to operating mode.
195 	 */
196 #define MIDI_F_STEREO            0x0100	/* doing stereo */
197 #define MIDI_F_NBIO              0x0200	/* do non-blocking i/o */
198 #define MIDI_F_PASSTHRU          0x0400 /* pass received data to output port */
199 
200 	/*
201 	 * these flags mark a pending abort on a r/w operation.
202 	 */
203 #define MIDI_F_ABORTING          0x1000  /* a pending abort */
204 
205 	/*
206 	 * this is used to mark that board initialization is needed, e.g.
207 	 * because of a change in sampling rate, format, etc. -- It will
208 	 * be done at the next convenient time.
209 	 */
210 #define MIDI_F_INIT              0x4000  /* changed parameters. need init */
211 
212 	int     play_blocksize, rec_blocksize;  /* blocksize for io and dma ops */
213 
214 #define mwsel midi_dbuf_out.sel
215 #define mrsel midi_dbuf_in.sel
216 	u_long	interrupts;	/* counter of interrupts */
217 	u_long	magic;
218 #define	MAGIC(unit) ( 0xa4d10de0 + unit )
219 	void    *device_data ;	/* just in case it is needed...*/
220 
221 	midi_intr_t	*intr;	/* interrupt handler of the upper layer (ie sequencer) */
222 	void		*intrarg;	/* argument to interrupt handler */
223 
224 	/* The following is the interface from a midi sequencer to a midi device. */
225 	synthdev_info synth;
226 
227 	/* This is the status message to display via /dev/midistat */
228 	char midistat[128];
229 
230 	/* The tailq entry of the next midi device. */
231 	TAILQ_ENTRY(_mididev_info) md_link;
232 
233 	/* The tailq entry of the next midi device opened by a sequencer. */
234 	TAILQ_ENTRY(_mididev_info) md_linkseq;
235 } ;
236 
237 /*
238  * then ioctls and other stuff
239  */
240 
241 #define NMIDI_MAX	16	/* Number of supported devices */
242 
243 /*
244  * many variables should be reduced to a range. Here define a macro
245  */
246 
247 #define RANGE(var, low, high) (var) = \
248 ((var)<(low)?(low) : (var)>(high)?(high) : (var))
249 
250 /*
251  * convert dev_t to unit and dev
252  */
253 #define MIDIMINOR(x)       (minor(x))
254 #define MIDIUNIT(x)        ((MIDIMINOR(x) & 0x000000f0) >> 4)
255 #define MIDIDEV(x)         (MIDIMINOR(x) & 0x0000000f)
256 #define MIDIMKMINOR(u, d)  (((u) & 0x0f) << 4 | ((d) & 0x0f))
257 #define MIDIMKDEV(m, u, d) (makedev((m), MIDIMKMINOR((u), (d))))
258 
259 /*
260  * see if the device is configured
261  */
262 #define MIDICONFED(x) ((x)->ioctl != NULL)
263 
264 /*
265  * finally, all default parameters
266  */
267 #define MIDI_BUFFSIZE (1024) /* XXX */
268 
269 /*
270  * some macros for debugging purposes
271  * DDB/DEB to enable/disable debugging stuff
272  * BVDDB   to enable debugging when bootverbose
273  */
274 #define DDB(x)	x	/* XXX */
275 #define BVDDB(x) if (bootverbose) x
276 
277 #ifndef DEB
278 #define DEB(x)
279 #endif
280 
281 /* This is the generic midi drvier initializer. */
282 	int midiinit(mididev_info *d, device_t dev);
283 
284 /* This provides an access to the mididev_info. */
285 	mididev_info *get_mididev_info(dev_t i_dev, int *unit);
286 	mididev_info *get_mididev_info_unit(int unit);
287 	mididev_info *create_mididev_info_unit(int type, mididev_info *mdinf, synthdev_info *syninf);
288 	int mididev_info_number(void);
289 #define MDT_MIDI	(0)
290 #define MDT_SYNTH	(1)
291 
292 /* These are the generic methods for a midi driver. */
293 	d_open_t midi_open;
294 	d_close_t midi_close;
295 	d_ioctl_t midi_ioctl;
296 	d_read_t midi_read;
297 	d_write_t midi_write;
298 	d_poll_t midi_poll;
299 
300 /* Common interrupt handler */
301 void midi_intr(mididev_info *);
302 
303 /* Sync output */
304 int midi_sync(mididev_info *);
305 
306 /*
307  * Minor numbers for the midi driver.
308  */
309 
310 #define MIDI_DEV_MIDIN	2	/* Raw midi access */
311 #define MIDI_DEV_STATUS	11	/* /dev/midistat */
312 
313 #endif	/* _MIDI_H_ */
314