sound.h revision 160439
1139749Simp/*-
2119853Scg * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
350724Scg * Copyright by Hannu Savolainen 1995
450724Scg * All rights reserved.
550724Scg *
650724Scg * Redistribution and use in source and binary forms, with or without
750724Scg * modification, are permitted provided that the following conditions
850724Scg * are met:
950724Scg * 1. Redistributions of source code must retain the above copyright
1050724Scg *    notice, this list of conditions and the following disclaimer.
1150724Scg * 2. Redistributions in binary form must reproduce the above copyright
1250724Scg *    notice, this list of conditions and the following disclaimer in the
1350724Scg *    documentation and/or other materials provided with the distribution.
1450724Scg *
1550724Scg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1650724Scg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1750724Scg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1850724Scg * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1950724Scg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2050724Scg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2150724Scg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2250724Scg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2350724Scg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2450724Scg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2550724Scg * SUCH DAMAGE.
2650724Scg *
2750733Speter * $FreeBSD: head/sys/dev/sound/pcm/sound.h 160439 2006-07-17 17:43:06Z netchild $
2850724Scg */
2950724Scg
3050724Scg/*
3150724Scg * first, include kernel header files.
3250724Scg */
3350724Scg
3450724Scg#ifndef _OS_H_
3550724Scg#define _OS_H_
3650724Scg
3755205Speter#ifdef _KERNEL
3850724Scg#include <sys/param.h>
3950724Scg#include <sys/systm.h>
4050724Scg#include <sys/ioccom.h>
4150724Scg#include <sys/filio.h>
4250724Scg#include <sys/sockio.h>
4350724Scg#include <sys/fcntl.h>
4450724Scg#include <sys/tty.h>
4550724Scg#include <sys/proc.h>
4650724Scg#include <sys/kernel.h> /* for DATA_SET */
4750724Scg#include <sys/module.h>
4850724Scg#include <sys/conf.h>
4950724Scg#include <sys/file.h>
5050724Scg#include <sys/uio.h>
5150724Scg#include <sys/syslog.h>
5250724Scg#include <sys/errno.h>
5350724Scg#include <sys/malloc.h>
5450724Scg#include <sys/bus.h>
55103327Sphk#if __FreeBSD_version < 500000
56103327Sphk#include <sys/buf.h>
5763469Scg#endif
5850724Scg#include <machine/resource.h>
5950724Scg#include <machine/bus.h>
6050724Scg#include <sys/rman.h>
6150724Scg#include <sys/mman.h>
6250724Scg#include <sys/poll.h>
6382180Scg#include <sys/sbuf.h>
6450724Scg#include <sys/soundcard.h>
6570617Sjhb#include <sys/sysctl.h>
6673769Scg#include <sys/kobj.h>
6750724Scg#include <vm/vm.h>
6850724Scg#include <vm/pmap.h>
6950724Scg
7078670Scg#undef	USING_MUTEX
7178670Scg#undef	USING_DEVFS
7278670Scg
7373126Scg#if __FreeBSD_version > 500000
74103338Sbde#include <sys/lock.h>
75103338Sbde#include <sys/mutex.h>
76103338Sbde
7773126Scg#define USING_MUTEX
7873126Scg#define USING_DEVFS
79128232Sgreen#else
80128232Sgreen#define	INTR_TYPE_AV	INTR_TYPE_TTY
81128232Sgreen#define	INTR_MPSAFE	0
8273760Scg#endif
8382180Scg
8473126Scg#define SND_DYNSYSCTL
8573126Scg
8674763Scgstruct pcm_channel;
8774763Scgstruct pcm_feeder;
8874763Scgstruct snd_dbuf;
8974763Scgstruct snd_mixer;
9074763Scg
9174763Scg#include <dev/sound/pcm/buffer.h>
9253465Scg#include <dev/sound/pcm/channel.h>
9364881Scg#include <dev/sound/pcm/feeder.h>
9453465Scg#include <dev/sound/pcm/mixer.h>
9550724Scg
9682180Scg#define	PCM_SOFTC_SIZE	512
9777269Scg
9874763Scg#define SND_STATUSLEN	64
9964442Scg
100132236Stanimura#define SOUND_MODVER	1
10162483Scg
102132236Stanimura#define SOUND_MINVER	1
103132236Stanimura#define SOUND_PREFVER	SOUND_MODVER
104132236Stanimura#define SOUND_MAXVER	1
10562483Scg
10678362Scg/*
10778362ScgPROPOSAL:
10878362Scgeach unit needs:
10978362Scgstatus, mixer, dsp, dspW, audio, sequencer, midi-in, seq2, sndproc = 9 devices
11078362ScgdspW and audio are deprecated.
11178362Scgdsp needs min 64 channels, will give it 256
11250724Scg
11378362Scgminor = (unit << 20) + (dev << 16) + channel
11478362Scgcurrently minor = (channel << 16) + (unit << 4) + dev
11578362Scg
11678362Scgnomenclature:
11778362Scg	/dev/pcmX/dsp.(0..255)
11878362Scg	/dev/pcmX/dspW
11978362Scg	/dev/pcmX/audio
12078362Scg	/dev/pcmX/status
12178362Scg	/dev/pcmX/mixer
12278362Scg	[etc.]
12378362Scg*/
12478362Scg
125156762Sariff#define PCMMAXCHAN		0xff
126156762Sariff#define PCMMAXDEV		0x0f
127156762Sariff#define PCMMAXUNIT		0x0f
128156762Sariff#define PCMMINOR(x)		(minor(x))
129156762Sariff#define PCMCHAN(x)		((PCMMINOR(x) >> 16) & PCMMAXCHAN)
130156762Sariff#define PCMUNIT(x)		((PCMMINOR(x) >> 4) & PCMMAXUNIT)
131156762Sariff#define PCMDEV(x)		(PCMMINOR(x) & PCMMAXDEV)
132156762Sariff#define PCMMKMINOR(u, d, c)	((((c) & PCMMAXCHAN) << 16) | \
133156762Sariff				(((u) & PCMMAXUNIT) << 4) | ((d) & PCMMAXDEV))
13478362Scg
13550724Scg#define SD_F_SIMPLEX		0x00000001
136150827Snetchild#define SD_F_AUTOVCHAN		0x00000002
137150827Snetchild#define SD_F_SOFTVOL		0x00000004
13850724Scg#define SD_F_PRIO_RD		0x10000000
13950724Scg#define SD_F_PRIO_WR		0x20000000
14050724Scg#define SD_F_PRIO_SET		(SD_F_PRIO_RD | SD_F_PRIO_WR)
14150724Scg#define SD_F_DIR_SET		0x40000000
14250724Scg#define SD_F_TRANSIENT		0xf0000000
14350724Scg
14450724Scg/* many variables should be reduced to a range. Here define a macro */
14550724Scg#define RANGE(var, low, high) (var) = \
14650724Scg	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
14755204Scg#define DSP_BUFFSIZE (8192)
14850724Scg
14950724Scg/* make figuring out what a format is easier. got AFMT_STEREO already */
15065486Scg#define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE)
151148606Snetchild#define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
15250724Scg#define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
153149950Snetchild#define AFMT_8BIT (AFMT_MU_LAW | AFMT_A_LAW | AFMT_U8 | AFMT_S8)
154148606Snetchild#define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | \
155148606Snetchild			AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
156148606Snetchild#define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_S24_BE | AFMT_U24_BE | \
157148606Snetchild			AFMT_S16_BE | AFMT_U16_BE)
15850724Scg
15974763Scgstruct pcm_channel *fkchan_setup(device_t dev);
16074763Scgint fkchan_kill(struct pcm_channel *c);
16150724Scg
162156762Sariff/* XXX Flawed definition. I'll fix it someday. */
163156762Sariff#define	SND_MAXVCHANS	PCMMAXCHAN
16478853Scg
16578362Scg/*
16650724Scg * Minor numbers for the sound driver.
16750724Scg *
16850724Scg * Unfortunately Creative called the codec chip of SB as a DSP. For this
16950724Scg * reason the /dev/dsp is reserved for digitized audio use. There is a
17050724Scg * device for true DSP processors but it will be called something else.
17150724Scg * In v3.0 it's /dev/sndproc but this could be a temporary solution.
17250724Scg */
17350724Scg
17450724Scg#define SND_DEV_CTL	0	/* Control port /dev/mixer */
17550724Scg#define SND_DEV_SEQ	1	/* Sequencer /dev/sequencer */
17650724Scg#define SND_DEV_MIDIN	2	/* Raw midi access */
17750724Scg#define SND_DEV_DSP	3	/* Digitized voice /dev/dsp */
17850724Scg#define SND_DEV_AUDIO	4	/* Sparc compatible /dev/audio */
17950724Scg#define SND_DEV_DSP16	5	/* Like /dev/dsp but 16 bits/sample */
18050724Scg#define SND_DEV_STATUS	6	/* /dev/sndstat */
18150724Scg				/* #7 not in use now. */
18250724Scg#define SND_DEV_SEQ2	8	/* /dev/sequencer, level 2 interface */
18350724Scg#define SND_DEV_SNDPROC 9	/* /dev/sndproc for programmable devices */
18450724Scg#define SND_DEV_PSS	SND_DEV_SNDPROC /* ? */
18551769Scg#define SND_DEV_NORESET	10
18683089Scg#define	SND_DEV_DSPREC	11	/* recording channels */
18750724Scg
18850724Scg#define DSP_DEFAULT_SPEED	8000
18950724Scg
19050724Scg#define ON		1
19150724Scg#define OFF		0
19250724Scg
19389834Scgextern int pcm_veto_load;
19478362Scgextern int snd_unit;
19578362Scgextern devclass_t pcm_devclass;
19678362Scg
19750724Scg/*
19850724Scg * some macros for debugging purposes
19950724Scg * DDB/DEB to enable/disable debugging stuff
20050724Scg * BVDDB   to enable debugging when bootverbose
20150724Scg */
20250724Scg#define BVDDB(x) if (bootverbose) x
20350724Scg
20450724Scg#ifndef DEB
20550724Scg#define DEB(x)
20650724Scg#endif
20750724Scg
20870617SjhbSYSCTL_DECL(_hw_snd);
20970617Sjhb
21082180Scgstruct sysctl_ctx_list *snd_sysctl_tree(device_t dev);
21182180Scgstruct sysctl_oid *snd_sysctl_tree_top(device_t dev);
21282180Scg
21382180Scgstruct pcm_channel *pcm_getfakechan(struct snddev_info *d);
214156929Sariffint pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction, pid_t pid, int chnum);
21578214Scgint pcm_chnrelease(struct pcm_channel *c);
21677269Scgint pcm_chnref(struct pcm_channel *c, int ref);
21782180Scgint pcm_inprog(struct snddev_info *d, int delta);
21877269Scg
21977269Scgstruct pcm_channel *pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, void *devinfo);
22077269Scgint pcm_chn_destroy(struct pcm_channel *ch);
221124740Smatkint pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch);
222124740Smatkint pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch);
22377269Scg
22470134Scgint pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
225160439Snetchildunsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
22650724Scgint pcm_register(device_t dev, void *devinfo, int numplay, int numrec);
22765340Scgint pcm_unregister(device_t dev);
22850724Scgint pcm_setstatus(device_t dev, char *str);
22950724Scgu_int32_t pcm_getflags(device_t dev);
23050724Scgvoid pcm_setflags(device_t dev, u_int32_t val);
23158384Scgvoid *pcm_getdevinfo(device_t dev);
23250724Scg
233124617Sphk
23473131Scgint snd_setup_intr(device_t dev, struct resource *res, int flags,
23573131Scg		   driver_intr_t hand, void *param, void **cookiep);
23673131Scg
23793814Sjhbvoid *snd_mtxcreate(const char *desc, const char *type);
23873131Scgvoid snd_mtxfree(void *m);
23973131Scgvoid snd_mtxassert(void *m);
240119096Scg#define	snd_mtxlock(m) mtx_lock(m)
241119096Scg#define	snd_mtxunlock(m) mtx_unlock(m)
242119096Scg
24382180Scgint sysctl_hw_snd_vchans(SYSCTL_HANDLER_ARGS);
24482180Scg
24582180Scgtypedef int (*sndstat_handler)(struct sbuf *s, device_t dev, int verbose);
246150827Snetchildint sndstat_acquire(void);
247150827Snetchildint sndstat_release(void);
24882180Scgint sndstat_register(device_t dev, char *str, sndstat_handler handler);
24982180Scgint sndstat_registerfile(char *str);
25082180Scgint sndstat_unregister(device_t dev);
25182180Scgint sndstat_unregisterfile(char *str);
25282180Scg
25382180Scg#define SND_DECLARE_FILE(version) \
25482180Scg	_SND_DECLARE_FILE(__LINE__, version)
25582180Scg
25682180Scg#define _SND_DECLARE_FILE(uniq, version) \
25782180Scg	__SND_DECLARE_FILE(uniq, version)
25882180Scg
25982180Scg#define __SND_DECLARE_FILE(uniq, version) \
26082180Scg	static char sndstat_vinfo[] = version; \
26182180Scg	SYSINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_registerfile, sndstat_vinfo); \
26282180Scg	SYSUNINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_unregisterfile, sndstat_vinfo);
26382180Scg
26450724Scg/* usage of flags in device config entry (config file) */
26550724Scg#define DV_F_DRQ_MASK	0x00000007	/* mask for secondary drq */
26650724Scg#define	DV_F_DUAL_DMA	0x00000010	/* set to use secondary dma channel */
26750724Scg
268119096Scg/* ought to be made obsolete but still used by mss */
26950724Scg#define	DV_F_DEV_MASK	0x0000ff00	/* force device type/class */
27050724Scg#define	DV_F_DEV_SHIFT	8		/* force device type/class */
27182180Scg
272119096Scg#define	PCM_DEBUG_MTX
273107237Scg
274119096Scg/*
275119096Scg * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
276119096Scg * so that the macro versions of pcm_{,un}lock can dereference them.
277124617Sphk * we also have to do this now makedev() has gone away.
278119096Scg */
279119096Scg
280119096Scgstruct snddev_channel {
281119096Scg	SLIST_ENTRY(snddev_channel) link;
282119096Scg	struct pcm_channel *channel;
283124740Smatk	int chan_num;
284130585Sphk	struct cdev *dsp_devt;
285130585Sphk	struct cdev *dspW_devt;
286130585Sphk	struct cdev *audio_devt;
287130585Sphk	struct cdev *dspr_devt;
288119096Scg};
289119096Scg
290119096Scgstruct snddev_info {
291119096Scg	SLIST_HEAD(, snddev_channel) channels;
292119096Scg	struct pcm_channel *fakechan;
293119096Scg	unsigned devcount, playcount, reccount, vchancount;
294119096Scg	unsigned flags;
295119096Scg	int inprog;
296119096Scg	unsigned int bufsz;
297119096Scg	void *devinfo;
298119096Scg	device_t dev;
299119096Scg	char status[SND_STATUSLEN];
300119096Scg	struct sysctl_ctx_list sysctl_tree;
301119096Scg	struct sysctl_oid *sysctl_tree_top;
302119096Scg	struct mtx *lock;
303130585Sphk	struct cdev *mixer_dev;
304124617Sphk
305119096Scg};
306119096Scg
307124617Sphk#ifdef	PCM_DEBUG_MTX
308119096Scg#define	pcm_lock(d) mtx_lock(((struct snddev_info *)(d))->lock)
309119096Scg#define	pcm_unlock(d) mtx_unlock(((struct snddev_info *)(d))->lock)
310119096Scg#else
311119096Scgvoid pcm_lock(struct snddev_info *d);
312119096Scgvoid pcm_unlock(struct snddev_info *d);
313119096Scg#endif
314119096Scg
315126695Smatk#ifdef KLD_MODULE
316126695Smatk#define PCM_KLDSTRING(a) ("kld " # a)
317126695Smatk#else
318126695Smatk#define PCM_KLDSTRING(a) ""
319126695Smatk#endif
320126695Smatk
32182180Scg#endif /* _KERNEL */
32282180Scg
32382180Scg#endif	/* _OS_H_ */
324