xref: /freebsd/sys/dev/sound/pcm/sound.h (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
1 /*
2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3  * Copyright by Hannu Savolainen 1995
4  * All rights reserved.
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  * first, include kernel header files.
32  */
33 
34 #ifndef _OS_H_
35 #define _OS_H_
36 
37 #ifdef _KERNEL
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/ioccom.h>
41 #include <sys/filio.h>
42 #include <sys/sockio.h>
43 #include <sys/fcntl.h>
44 #include <sys/tty.h>
45 #include <sys/proc.h>
46 #include <sys/kernel.h> /* for DATA_SET */
47 #include <sys/module.h>
48 #include <sys/conf.h>
49 #include <sys/file.h>
50 #include <sys/uio.h>
51 #include <sys/syslog.h>
52 #include <sys/errno.h>
53 #include <sys/malloc.h>
54 #include <sys/bus.h>
55 #if __FreeBSD_version < 500000
56 #include <sys/buf.h>
57 #endif
58 #include <machine/clock.h>	/* for DELAY */
59 #include <machine/resource.h>
60 #include <machine/bus.h>
61 #include <sys/rman.h>
62 #include <sys/mman.h>
63 #include <sys/poll.h>
64 #include <sys/sbuf.h>
65 #include <sys/soundcard.h>
66 #include <sys/sysctl.h>
67 #include <sys/kobj.h>
68 #include <vm/vm.h>
69 #include <vm/pmap.h>
70 
71 #undef	USING_MUTEX
72 #undef	USING_DEVFS
73 
74 #if __FreeBSD_version > 500000
75 #include <sys/lock.h>
76 #include <sys/mutex.h>
77 
78 #define USING_MUTEX
79 #define USING_DEVFS
80 #else
81 #define	INTR_TYPE_AV	INTR_TYPE_TTY
82 #define	INTR_MPSAFE	0
83 #endif
84 
85 #define SND_DYNSYSCTL
86 
87 struct pcm_channel;
88 struct pcm_feeder;
89 struct snd_dbuf;
90 struct snd_mixer;
91 
92 #include <dev/sound/pcm/buffer.h>
93 #include <dev/sound/pcm/channel.h>
94 #include <dev/sound/pcm/feeder.h>
95 #include <dev/sound/pcm/mixer.h>
96 
97 #define	PCM_SOFTC_SIZE	512
98 
99 #define SND_STATUSLEN	64
100 
101 #define SOUND_MODVER	1
102 
103 #define SOUND_MINVER	1
104 #define SOUND_PREFVER	SOUND_MODVER
105 #define SOUND_MAXVER	1
106 
107 /*
108 PROPOSAL:
109 each unit needs:
110 status, mixer, dsp, dspW, audio, sequencer, midi-in, seq2, sndproc = 9 devices
111 dspW and audio are deprecated.
112 dsp needs min 64 channels, will give it 256
113 
114 minor = (unit << 20) + (dev << 16) + channel
115 currently minor = (channel << 16) + (unit << 4) + dev
116 
117 nomenclature:
118 	/dev/pcmX/dsp.(0..255)
119 	/dev/pcmX/dspW
120 	/dev/pcmX/audio
121 	/dev/pcmX/status
122 	/dev/pcmX/mixer
123 	[etc.]
124 */
125 
126 #define PCMMINOR(x) (minor(x))
127 #define PCMCHAN(x) ((PCMMINOR(x) & 0x00ff0000) >> 16)
128 #define PCMUNIT(x) ((PCMMINOR(x) & 0x000000f0) >> 4)
129 #define PCMDEV(x)   (PCMMINOR(x) & 0x0000000f)
130 #define PCMMKMINOR(u, d, c) ((((c) & 0xff) << 16) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
131 
132 #define SD_F_SIMPLEX		0x00000001
133 #define	SD_F_AUTOVCHAN		0x00000002
134 #define SD_F_PRIO_RD		0x10000000
135 #define SD_F_PRIO_WR		0x20000000
136 #define SD_F_PRIO_SET		(SD_F_PRIO_RD | SD_F_PRIO_WR)
137 #define SD_F_DIR_SET		0x40000000
138 #define SD_F_TRANSIENT		0xf0000000
139 
140 /* many variables should be reduced to a range. Here define a macro */
141 #define RANGE(var, low, high) (var) = \
142 	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
143 #define DSP_BUFFSIZE (8192)
144 
145 /* make figuring out what a format is easier. got AFMT_STEREO already */
146 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE)
147 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
148 #define AFMT_8BIT (AFMT_U8 | AFMT_S8)
149 #define AFMT_SIGNED (AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
150 #define AFMT_BIGENDIAN (AFMT_S16_BE | AFMT_U16_BE)
151 
152 struct pcm_channel *fkchan_setup(device_t dev);
153 int fkchan_kill(struct pcm_channel *c);
154 
155 /*
156  * Major nuber for the sound driver.
157  */
158 #define SND_CDEV_MAJOR 30
159 
160 #define	SND_MAXVCHANS	255
161 
162 /*
163  * Minor numbers for the sound driver.
164  *
165  * Unfortunately Creative called the codec chip of SB as a DSP. For this
166  * reason the /dev/dsp is reserved for digitized audio use. There is a
167  * device for true DSP processors but it will be called something else.
168  * In v3.0 it's /dev/sndproc but this could be a temporary solution.
169  */
170 
171 #define SND_DEV_CTL	0	/* Control port /dev/mixer */
172 #define SND_DEV_SEQ	1	/* Sequencer /dev/sequencer */
173 #define SND_DEV_MIDIN	2	/* Raw midi access */
174 #define SND_DEV_DSP	3	/* Digitized voice /dev/dsp */
175 #define SND_DEV_AUDIO	4	/* Sparc compatible /dev/audio */
176 #define SND_DEV_DSP16	5	/* Like /dev/dsp but 16 bits/sample */
177 #define SND_DEV_STATUS	6	/* /dev/sndstat */
178 				/* #7 not in use now. */
179 #define SND_DEV_SEQ2	8	/* /dev/sequencer, level 2 interface */
180 #define SND_DEV_SNDPROC 9	/* /dev/sndproc for programmable devices */
181 #define SND_DEV_PSS	SND_DEV_SNDPROC /* ? */
182 #define SND_DEV_NORESET	10
183 #define	SND_DEV_DSPREC	11	/* recording channels */
184 
185 #define DSP_DEFAULT_SPEED	8000
186 
187 #define ON		1
188 #define OFF		0
189 
190 extern int pcm_veto_load;
191 extern int snd_unit;
192 extern devclass_t pcm_devclass;
193 
194 /*
195  * some macros for debugging purposes
196  * DDB/DEB to enable/disable debugging stuff
197  * BVDDB   to enable debugging when bootverbose
198  */
199 #define BVDDB(x) if (bootverbose) x
200 
201 #ifndef DEB
202 #define DEB(x)
203 #endif
204 
205 SYSCTL_DECL(_hw_snd);
206 
207 struct sysctl_ctx_list *snd_sysctl_tree(device_t dev);
208 struct sysctl_oid *snd_sysctl_tree_top(device_t dev);
209 
210 struct pcm_channel *pcm_getfakechan(struct snddev_info *d);
211 struct pcm_channel *pcm_chnalloc(struct snddev_info *d, int direction, pid_t pid, int chnum);
212 int pcm_chnrelease(struct pcm_channel *c);
213 int pcm_chnref(struct pcm_channel *c, int ref);
214 int pcm_inprog(struct snddev_info *d, int delta);
215 
216 struct pcm_channel *pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, void *devinfo);
217 int pcm_chn_destroy(struct pcm_channel *ch);
218 int pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch);
219 int pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch);
220 
221 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
222 unsigned int pcm_getbuffersize(device_t dev, unsigned int min, unsigned int deflt, unsigned int max);
223 int pcm_register(device_t dev, void *devinfo, int numplay, int numrec);
224 int pcm_unregister(device_t dev);
225 int pcm_setstatus(device_t dev, char *str);
226 u_int32_t pcm_getflags(device_t dev);
227 void pcm_setflags(device_t dev, u_int32_t val);
228 void *pcm_getdevinfo(device_t dev);
229 
230 
231 int snd_setup_intr(device_t dev, struct resource *res, int flags,
232 		   driver_intr_t hand, void *param, void **cookiep);
233 
234 void *snd_mtxcreate(const char *desc, const char *type);
235 void snd_mtxfree(void *m);
236 void snd_mtxassert(void *m);
237 #define	snd_mtxlock(m) mtx_lock(m)
238 #define	snd_mtxunlock(m) mtx_unlock(m)
239 
240 int sysctl_hw_snd_vchans(SYSCTL_HANDLER_ARGS);
241 
242 typedef int (*sndstat_handler)(struct sbuf *s, device_t dev, int verbose);
243 int sndstat_register(device_t dev, char *str, sndstat_handler handler);
244 int sndstat_registerfile(char *str);
245 int sndstat_unregister(device_t dev);
246 int sndstat_unregisterfile(char *str);
247 int sndstat_busy(void);
248 
249 #define SND_DECLARE_FILE(version) \
250 	_SND_DECLARE_FILE(__LINE__, version)
251 
252 #define _SND_DECLARE_FILE(uniq, version) \
253 	__SND_DECLARE_FILE(uniq, version)
254 
255 #define __SND_DECLARE_FILE(uniq, version) \
256 	static char sndstat_vinfo[] = version; \
257 	SYSINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_registerfile, sndstat_vinfo); \
258 	SYSUNINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_unregisterfile, sndstat_vinfo);
259 
260 /* usage of flags in device config entry (config file) */
261 #define DV_F_DRQ_MASK	0x00000007	/* mask for secondary drq */
262 #define	DV_F_DUAL_DMA	0x00000010	/* set to use secondary dma channel */
263 
264 /* ought to be made obsolete but still used by mss */
265 #define	DV_F_DEV_MASK	0x0000ff00	/* force device type/class */
266 #define	DV_F_DEV_SHIFT	8		/* force device type/class */
267 
268 #define	PCM_DEBUG_MTX
269 
270 /*
271  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
272  * so that the macro versions of pcm_{,un}lock can dereference them.
273  * we also have to do this now makedev() has gone away.
274  */
275 
276 struct snddev_channel {
277 	SLIST_ENTRY(snddev_channel) link;
278 	struct pcm_channel *channel;
279 	int chan_num;
280 	struct cdev *dsp_devt;
281 	struct cdev *dspW_devt;
282 	struct cdev *audio_devt;
283 	struct cdev *dspr_devt;
284 };
285 
286 struct snddev_info {
287 	SLIST_HEAD(, snddev_channel) channels;
288 	struct pcm_channel *fakechan;
289 	unsigned devcount, playcount, reccount, vchancount;
290 	unsigned flags;
291 	int inprog;
292 	unsigned int bufsz;
293 	void *devinfo;
294 	device_t dev;
295 	char status[SND_STATUSLEN];
296 	struct sysctl_ctx_list sysctl_tree;
297 	struct sysctl_oid *sysctl_tree_top;
298 	struct mtx *lock;
299 	struct cdev *mixer_dev;
300 
301 };
302 
303 #ifdef	PCM_DEBUG_MTX
304 #define	pcm_lock(d) mtx_lock(((struct snddev_info *)(d))->lock)
305 #define	pcm_unlock(d) mtx_unlock(((struct snddev_info *)(d))->lock)
306 #else
307 void pcm_lock(struct snddev_info *d);
308 void pcm_unlock(struct snddev_info *d);
309 #endif
310 
311 #ifdef KLD_MODULE
312 #define PCM_KLDSTRING(a) ("kld " # a)
313 #else
314 #define PCM_KLDSTRING(a) ""
315 #endif
316 
317 #endif /* _KERNEL */
318 
319 #endif	/* _OS_H_ */
320