xref: /freebsd/sys/dev/sound/midi/midi.h (revision ae83180158c4c937f170e31eff311b18c0286a93)
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 #include <sys/sysctl.h>
49 
50 #include <sys/kernel.h> /* for DATA_SET */
51 
52 #include <sys/module.h>
53 #include <sys/conf.h>
54 #include <sys/file.h>
55 #include <sys/uio.h>
56 #include <sys/syslog.h>
57 #include <sys/errno.h>
58 #include <sys/malloc.h>
59 #include <sys/bus.h>
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 <machine/limits.h>
66 #include <sys/soundcard.h>
67 #include <sys/rman.h>
68 #include <sys/mman.h>
69 #include <sys/poll.h>
70 #include <sys/mutex.h>
71 #include <sys/condvar.h>
72 
73 #include <dev/sound/midi/miditypes.h>
74 #include <dev/sound/midi/midibuf.h>
75 #include <dev/sound/midi/midisynth.h>
76 
77 #define MIDI_CDEV_MAJOR 30
78 
79 /*#define MIDI_OUTOFGIANT*/
80 
81 /*
82  * The order of mutex lock (from the first to the last)
83  *
84  * 1. sequencer flags, queues, timer and device list
85  * 2. midi synth voice and channel
86  * 3. midi synth status
87  * 4. generic midi flags and queues
88  * 5. midi device
89  */
90 
91 /*
92  * descriptor of midi operations ...
93  *
94  */
95 
96 struct _mididev_info {
97 
98 	/*
99 	 * the first part of the descriptor is filled up from a
100 	 * template.
101 	 */
102 	char		name[64];
103 
104 	int		type;
105 
106 	d_open_t	*open;
107 	d_close_t	*close;
108 	d_ioctl_t	*ioctl;
109 	midi_callback_t	*callback;
110 
111 	/*
112 	 * combinations of the following flags are used as second argument in
113 	 * the callback from the dma module to the device-specific routines.
114 	 */
115 
116 #define MIDI_CB_RD       0x100   /* read callback */
117 #define MIDI_CB_WR       0x200   /* write callback */
118 #define MIDI_CB_REASON_MASK      0xff
119 #define MIDI_CB_START    0x01   /* start dma op */
120 #define MIDI_CB_STOP     0x03   /* stop dma op */
121 #define MIDI_CB_ABORT    0x04   /* abort dma op */
122 #define MIDI_CB_INIT     0x05   /* init board parameters */
123 
124 	/*
125 	 * callback extensions
126 	 */
127 #define MIDI_CB_DMADONE         0x10
128 #define MIDI_CB_DMAUPDATE       0x11
129 #define MIDI_CB_DMASTOP         0x12
130 
131 	/* init can only be called with int enabled and
132 	 * no pending DMA activity.
133 	 */
134 
135 	/*
136 	 * whereas from here, parameters are set at runtime.
137 	 * resources are stored in the softc of the device,
138 	 * not in the common structure.
139 	 */
140 
141 	int		unit;	/* unit number of the device */
142 	int		midiunit;	/* unit number for midi devices */
143 	int		synthunit;	/* unit number for synth devices */
144 	int		mdtype;	/* MDT_MIDI or MDT_SYNTH */
145 	void		*softc;	/* softc for the device */
146 	device_t	dev;	/* device_t for the device */
147 
148 	int		bd_id;	/* used to hold board-id info, eg. sb version,
149 				 * mss codec type, etc. etc.
150 				 */
151 
152 	struct mtx	flagqueue_mtx;	/* Mutex to protect flags and queues */
153 
154 	/* Queues */
155 	midi_dbuf	midi_dbuf_in;		/* midi input event/message queue */
156 	midi_dbuf	midi_dbuf_out;		/* midi output event/message queue */
157 	midi_dbuf	midi_dbuf_passthru;	/* midi passthru event/message queue */
158 
159         /*
160          * these parameters describe the operation of the board.
161          * Generic things like busy flag, speed, etc are here.
162          */
163 
164 	/* Flags */
165 	volatile u_long	flags;	/* 32 bits, used for various purposes. */
166 	int		fflags;	/* file flag */
167 
168 	/*
169 	 * we have separate flags for read and write, although in some
170 	 * cases this is probably not necessary (e.g. because we cannot
171 	 * know how many processes are using the device, we cannot
172 	 * distinguish if open, close, abort are for a write or for a
173 	 * read).
174 	 */
175 
176 	/*
177 	 * the following flag is used by open-close routines
178 	 * to mark the status of the device.
179 	 */
180 #define MIDI_F_BUSY              0x0001  /* has been opened 	*/
181 	/*
182 	 * the next two are used to allow only one pending operation of
183 	 * each type.
184 	 */
185 #define MIDI_F_READING           0x0004  /* have a pending read */
186 #define MIDI_F_WRITING           0x0008  /* have a pending write */
187 
188 	/*
189 	 * flag used to mark a pending close.
190 	 */
191 #define MIDI_F_CLOSING           0x0040  /* a pending close */
192 
193 	/*
194 	 * if user has not set block size, then make it adaptive
195 	 * (0.25s, or the perhaps last read/write ?)
196 	 */
197 #define	MIDI_F_HAS_SIZE		0x0080	/* user set block size */
198 	/*
199 	 * assorted flags related to operating mode.
200 	 */
201 #define MIDI_F_STEREO            0x0100	/* doing stereo */
202 #define MIDI_F_NBIO              0x0200	/* do non-blocking i/o */
203 #define MIDI_F_PASSTHRU          0x0400 /* pass received data to output port */
204 
205 	/*
206 	 * these flags mark a pending abort on a r/w operation.
207 	 */
208 #define MIDI_F_ABORTING          0x1000  /* a pending abort */
209 
210 	/*
211 	 * this is used to mark that board initialization is needed, e.g.
212 	 * because of a change in sampling rate, format, etc. -- It will
213 	 * be done at the next convenient time.
214 	 */
215 #define MIDI_F_INIT              0x4000  /* changed parameters. need init */
216 
217 	int		play_blocksize, rec_blocksize;	/* blocksize for io and dma ops */
218 
219 #define mwsel midi_dbuf_out.sel
220 #define mrsel midi_dbuf_in.sel
221 	u_long		nterrupts;	/* counter of interrupts */
222 	u_long		magic;
223 #define	MAGIC(unit) ( 0xa4d10de0 + unit )
224 	void		*device_data ;	/* just in case it is needed...*/
225 
226 	midi_intr_t	*intr;		/* interrupt handler of the upper layer (ie sequencer) */
227 	void		*intrarg;	/* argument to interrupt handler */
228 
229 	/* The following is the interface from a midi sequencer to a midi device. */
230 	synthdev_info	synth;
231 
232 	/* This is the status message to display via /dev/midistat */
233 	char		midistat[128];
234 
235 	/* The tailq entry of the next midi device. */
236 	TAILQ_ENTRY(_mididev_info)	md_link;
237 
238 	/* The tailq entry of the next midi device opened by a sequencer. */
239 	TAILQ_ENTRY(_mididev_info)	md_linkseq;
240 } ;
241 
242 /*
243  * then ioctls and other stuff
244  */
245 
246 #define NMIDI_MAX	16	/* Number of supported devices */
247 
248 /*
249  * many variables should be reduced to a range. Here define a macro
250  */
251 
252 #define RANGE(var, low, high) (var) = \
253 ((var)<(low)?(low) : (var)>(high)?(high) : (var))
254 
255 /*
256  * convert dev_t to unit and dev
257  */
258 #define MIDIMINOR(x)       (minor(x))
259 #define MIDIUNIT(x)        ((MIDIMINOR(x) & 0x000000f0) >> 4)
260 #define MIDIDEV(x)         (MIDIMINOR(x) & 0x0000000f)
261 #define MIDIMKMINOR(u, d)  (((u) & 0x0f) << 4 | ((d) & 0x0f))
262 #define MIDIMKDEV(m, u, d) (makedev((m), MIDIMKMINOR((u), (d))))
263 
264 /*
265  * see if the device is configured
266  */
267 #define MIDICONFED(x) ((x)->ioctl != NULL)
268 
269 /*
270  * finally, all default parameters
271  */
272 #define MIDI_BUFFSIZE (1024) /* XXX */
273 
274 #ifdef _KERNEL
275 
276 /* This is the generic midi drvier initializer. */
277 	int midiinit(mididev_info *d, device_t dev);
278 
279 /* This provides an access to the mididev_info. */
280 	mididev_info *get_mididev_info(dev_t i_dev, int *unit);
281 	mididev_info *get_mididev_info_unit(int unit);
282 	mididev_info *get_mididev_midi_unit(int unit);
283 	mididev_info *get_mididev_synth_unit(int unit);
284 	mididev_info *create_mididev_info_unit(int type, mididev_info *mdinf, synthdev_info *syninf);
285 	int mididev_info_number(void);
286 	int mididev_midi_number(void);
287 	int mididev_synth_number(void);
288 #define MDT_MIDI	(0)
289 #define MDT_SYNTH	(1)
290 
291 /* These are the generic methods for a midi driver. */
292 	d_open_t midi_open;
293 	d_close_t midi_close;
294 	d_ioctl_t midi_ioctl;
295 	d_read_t midi_read;
296 	d_write_t midi_write;
297 	d_poll_t midi_poll;
298 
299 /* Common interrupt handler */
300 void	midi_intr(mididev_info *);
301 
302 /* Sync output */
303 int	midi_sync(mididev_info *);
304 
305 struct _midi_cmdtab {
306 	int	cmd;
307 	char *	name;
308 };
309 typedef struct _midi_cmdtab	midi_cmdtab;
310 
311 char	*midi_cmdname(int cmd, midi_cmdtab *tab);
312 
313 SYSCTL_DECL(_hw_midi);
314 
315 extern int	midi_debug;
316 #define MIDI_DEBUG(x)			\
317 	do {				\
318 		if (midi_debug) {	\
319 			(x);		\
320 		}			\
321 	} while(0)
322 
323 extern midi_cmdtab	cmdtab_midiioctl[];
324 
325 #endif /* _KERNEL */
326 
327 /*
328  * Minor numbers for the midi driver.
329  */
330 
331 #define MIDI_DEV_MIDIN	2	/* Raw midi access */
332 #define MIDI_DEV_STATUS	15	/* /dev/midistat */
333 
334 #endif	/* _MIDI_H_ */
335