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