xref: /openbmc/linux/sound/sound_core.c (revision 8f6e60419645f7fea84c361ca2c16dd1feee08e4)
11da177e4SLinus Torvalds /*
2d886e87cSTejun Heo  *	Sound core.  This file is composed of two parts.  sound_class
3d886e87cSTejun Heo  *	which is common to both OSS and ALSA and OSS sound core which
4d886e87cSTejun Heo  *	is used OSS or emulation of it.
5d886e87cSTejun Heo  */
6d886e87cSTejun Heo 
7d886e87cSTejun Heo /*
8d886e87cSTejun Heo  * First, the common part.
9d886e87cSTejun Heo  */
10d886e87cSTejun Heo #include <linux/module.h>
11d886e87cSTejun Heo #include <linux/device.h>
12d886e87cSTejun Heo #include <linux/err.h>
13954a973cSKay Sievers #include <linux/kdev_t.h>
14954a973cSKay Sievers #include <linux/major.h>
153d1ee379SMark Brown #include <sound/core.h>
16d886e87cSTejun Heo 
17d886e87cSTejun Heo #ifdef CONFIG_SOUND_OSS_CORE
18d886e87cSTejun Heo static int __init init_oss_soundcore(void);
19de30d36bSKrzysztof Helt static void cleanup_oss_soundcore(void);
20d886e87cSTejun Heo #else
21d886e87cSTejun Heo static inline int init_oss_soundcore(void)	{ return 0; }
22d886e87cSTejun Heo static inline void cleanup_oss_soundcore(void)	{ }
23d886e87cSTejun Heo #endif
24d886e87cSTejun Heo 
25d886e87cSTejun Heo struct class *sound_class;
26d886e87cSTejun Heo EXPORT_SYMBOL(sound_class);
27d886e87cSTejun Heo 
28d886e87cSTejun Heo MODULE_DESCRIPTION("Core sound module");
29d886e87cSTejun Heo MODULE_AUTHOR("Alan Cox");
30d886e87cSTejun Heo MODULE_LICENSE("GPL");
31d886e87cSTejun Heo 
322c9ede55SAl Viro static char *sound_devnode(struct device *dev, umode_t *mode)
337a9d56f6SKay Sievers {
34954a973cSKay Sievers 	if (MAJOR(dev->devt) == SOUND_MAJOR)
35954a973cSKay Sievers 		return NULL;
367a9d56f6SKay Sievers 	return kasprintf(GFP_KERNEL, "snd/%s", dev_name(dev));
377a9d56f6SKay Sievers }
387a9d56f6SKay Sievers 
39d886e87cSTejun Heo static int __init init_soundcore(void)
40d886e87cSTejun Heo {
41d886e87cSTejun Heo 	int rc;
42d886e87cSTejun Heo 
43d886e87cSTejun Heo 	rc = init_oss_soundcore();
44d886e87cSTejun Heo 	if (rc)
45d886e87cSTejun Heo 		return rc;
46d886e87cSTejun Heo 
47d886e87cSTejun Heo 	sound_class = class_create(THIS_MODULE, "sound");
48d886e87cSTejun Heo 	if (IS_ERR(sound_class)) {
49d886e87cSTejun Heo 		cleanup_oss_soundcore();
50d886e87cSTejun Heo 		return PTR_ERR(sound_class);
51d886e87cSTejun Heo 	}
52d886e87cSTejun Heo 
53e454cea2SKay Sievers 	sound_class->devnode = sound_devnode;
547a9d56f6SKay Sievers 
55d886e87cSTejun Heo 	return 0;
56d886e87cSTejun Heo }
57d886e87cSTejun Heo 
58d886e87cSTejun Heo static void __exit cleanup_soundcore(void)
59d886e87cSTejun Heo {
60d886e87cSTejun Heo 	cleanup_oss_soundcore();
61d886e87cSTejun Heo 	class_destroy(sound_class);
62d886e87cSTejun Heo }
63d886e87cSTejun Heo 
64c181a13aSThadeu Lima de Souza Cascardo subsys_initcall(init_soundcore);
65d886e87cSTejun Heo module_exit(cleanup_soundcore);
66d886e87cSTejun Heo 
67d886e87cSTejun Heo 
68d886e87cSTejun Heo #ifdef CONFIG_SOUND_OSS_CORE
69d886e87cSTejun Heo /*
70d886e87cSTejun Heo  *	OSS sound core handling. Breaks out sound functions to submodules
711da177e4SLinus Torvalds  *
722f1e593dSAlan Cox  *	Author:		Alan Cox <alan@lxorguk.ukuu.org.uk>
731da177e4SLinus Torvalds  *
741da177e4SLinus Torvalds  *	Fixes:
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  *
771da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
781da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
791da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
801da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
811da177e4SLinus Torvalds  *
821da177e4SLinus Torvalds  *                         --------------------
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  *	Top level handler for the sound subsystem. Various devices can
851da177e4SLinus Torvalds  *	plug into this. The fact they don't all go via OSS doesn't mean
861da177e4SLinus Torvalds  *	they don't have to implement the OSS API. There is a lot of logic
871da177e4SLinus Torvalds  *	to keeping much of the OSS weight out of the code in a compatibility
881da177e4SLinus Torvalds  *	module, but it's up to the driver to rember to load it...
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  *	The code provides a set of functions for registration of devices
911da177e4SLinus Torvalds  *	by type. This is done rather than providing a single call so that
921da177e4SLinus Torvalds  *	we can hide any future changes in the internals (eg when we go to
931da177e4SLinus Torvalds  *	32bit dev_t) from the modules and their interface.
941da177e4SLinus Torvalds  *
951da177e4SLinus Torvalds  *	Secondly we need to allocate the dsp, dsp16 and audio devices as
961da177e4SLinus Torvalds  *	one. Thus we misuse the chains a bit to simplify this.
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  *	Thirdly to make it more fun and for 2.3.x and above we do all
991da177e4SLinus Torvalds  *	of this using fine grained locking.
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  *	FIXME: we have to resolve modules and fine grained load/unload
1021da177e4SLinus Torvalds  *	locking at some point in 2.3.x.
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #include <linux/init.h>
1061da177e4SLinus Torvalds #include <linux/slab.h>
1071da177e4SLinus Torvalds #include <linux/types.h>
1081da177e4SLinus Torvalds #include <linux/kernel.h>
1091da177e4SLinus Torvalds #include <linux/sound.h>
1101da177e4SLinus Torvalds #include <linux/kmod.h>
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #define SOUND_STEP 16
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds struct sound_unit
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	int unit_minor;
11799ac48f5SArjan van de Ven 	const struct file_operations *unit_fops;
1181da177e4SLinus Torvalds 	struct sound_unit *next;
1191da177e4SLinus Torvalds 	char name[32];
1201da177e4SLinus Torvalds };
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds #ifdef CONFIG_SOUND_MSNDCLAS
1231da177e4SLinus Torvalds extern int msnd_classic_init(void);
1241da177e4SLinus Torvalds #endif
1251da177e4SLinus Torvalds #ifdef CONFIG_SOUND_MSNDPIN
1261da177e4SLinus Torvalds extern int msnd_pinnacle_init(void);
1271da177e4SLinus Torvalds #endif
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
13093fe4483STejun Heo  * By default, OSS sound_core claims full legacy minor range (0-255)
13193fe4483STejun Heo  * of SOUND_MAJOR to trap open attempts to any sound minor and
13293fe4483STejun Heo  * requests modules using custom sound-slot/service-* module aliases.
13393fe4483STejun Heo  * The only benefit of doing this is allowing use of custom module
13493fe4483STejun Heo  * aliases instead of the standard char-major-* ones.  This behavior
13593fe4483STejun Heo  * prevents alternative OSS implementation and is scheduled to be
13693fe4483STejun Heo  * removed.
13793fe4483STejun Heo  *
13893fe4483STejun Heo  * CONFIG_SOUND_OSS_CORE_PRECLAIM and soundcore.preclaim_oss kernel
13993fe4483STejun Heo  * parameter are added to allow distros and developers to try and
14093fe4483STejun Heo  * switch to alternative implementations without needing to rebuild
14193fe4483STejun Heo  * the kernel in the meantime.  If preclaim_oss is non-zero, the
14293fe4483STejun Heo  * kernel will behave the same as before.  All SOUND_MAJOR minors are
14393fe4483STejun Heo  * preclaimed and the custom module aliases along with standard chrdev
14493fe4483STejun Heo  * ones are emitted if a missing device is opened.  If preclaim_oss is
14593fe4483STejun Heo  * zero, sound_core only grabs what's actually in use and for missing
14693fe4483STejun Heo  * devices only the standard chrdev aliases are requested.
14793fe4483STejun Heo  *
14893fe4483STejun Heo  * All these clutters are scheduled to be removed along with
149*8f6e6041STao Ma  * sound-slot/service-* module aliases.
15093fe4483STejun Heo  */
15193fe4483STejun Heo #ifdef CONFIG_SOUND_OSS_CORE_PRECLAIM
15293fe4483STejun Heo static int preclaim_oss = 1;
15393fe4483STejun Heo #else
15493fe4483STejun Heo static int preclaim_oss = 0;
15593fe4483STejun Heo #endif
15693fe4483STejun Heo 
15793fe4483STejun Heo module_param(preclaim_oss, int, 0444);
15893fe4483STejun Heo 
15993fe4483STejun Heo static int soundcore_open(struct inode *, struct file *);
16093fe4483STejun Heo 
16193fe4483STejun Heo static const struct file_operations soundcore_fops =
16293fe4483STejun Heo {
16393fe4483STejun Heo 	/* We must have an owner or the module locking fails */
16493fe4483STejun Heo 	.owner	= THIS_MODULE,
16593fe4483STejun Heo 	.open	= soundcore_open,
1666038f373SArnd Bergmann 	.llseek = noop_llseek,
16793fe4483STejun Heo };
16893fe4483STejun Heo 
16993fe4483STejun Heo /*
1701da177e4SLinus Torvalds  *	Low level list operator. Scan the ordered list, find a hole and
1711da177e4SLinus Torvalds  *	join into it. Called with the lock asserted
1721da177e4SLinus Torvalds  */
1731da177e4SLinus Torvalds 
17499ac48f5SArjan van de Ven static int __sound_insert_unit(struct sound_unit * s, struct sound_unit **list, const struct file_operations *fops, int index, int low, int top)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	int n=low;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (index < 0) {	/* first free */
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 		while (*list && (*list)->unit_minor<n)
1811da177e4SLinus Torvalds 			list=&((*list)->next);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 		while(n<top)
1841da177e4SLinus Torvalds 		{
1851da177e4SLinus Torvalds 			/* Found a hole ? */
1861da177e4SLinus Torvalds 			if(*list==NULL || (*list)->unit_minor>n)
1871da177e4SLinus Torvalds 				break;
1881da177e4SLinus Torvalds 			list=&((*list)->next);
1891da177e4SLinus Torvalds 			n+=SOUND_STEP;
1901da177e4SLinus Torvalds 		}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 		if(n>=top)
1931da177e4SLinus Torvalds 			return -ENOENT;
1941da177e4SLinus Torvalds 	} else {
1951da177e4SLinus Torvalds 		n = low+(index*16);
1961da177e4SLinus Torvalds 		while (*list) {
1971da177e4SLinus Torvalds 			if ((*list)->unit_minor==n)
1981da177e4SLinus Torvalds 				return -EBUSY;
1991da177e4SLinus Torvalds 			if ((*list)->unit_minor>n)
2001da177e4SLinus Torvalds 				break;
2011da177e4SLinus Torvalds 			list=&((*list)->next);
2021da177e4SLinus Torvalds 		}
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	/*
2061da177e4SLinus Torvalds 	 *	Fill it in
2071da177e4SLinus Torvalds 	 */
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	s->unit_minor=n;
2101da177e4SLinus Torvalds 	s->unit_fops=fops;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/*
2131da177e4SLinus Torvalds 	 *	Link it
2141da177e4SLinus Torvalds 	 */
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	s->next=*list;
2171da177e4SLinus Torvalds 	*list=s;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	return n;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds /*
2241da177e4SLinus Torvalds  *	Remove a node from the chain. Called with the lock asserted
2251da177e4SLinus Torvalds  */
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	while(*list)
2301da177e4SLinus Torvalds 	{
2311da177e4SLinus Torvalds 		struct sound_unit *p=*list;
2321da177e4SLinus Torvalds 		if(p->unit_minor==unit)
2331da177e4SLinus Torvalds 		{
2341da177e4SLinus Torvalds 			*list=p->next;
2351da177e4SLinus Torvalds 			return p;
2361da177e4SLinus Torvalds 		}
2371da177e4SLinus Torvalds 		list=&(p->next);
2381da177e4SLinus Torvalds 	}
2391da177e4SLinus Torvalds 	printk(KERN_ERR "Sound device %d went missing!\n", unit);
2401da177e4SLinus Torvalds 	return NULL;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /*
2441da177e4SLinus Torvalds  *	This lock guards the sound loader list.
2451da177e4SLinus Torvalds  */
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static DEFINE_SPINLOCK(sound_loader_lock);
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds /*
2501da177e4SLinus Torvalds  *	Allocate the controlling structure and add it to the sound driver
2511da177e4SLinus Torvalds  *	list. Acquires locks as needed
2521da177e4SLinus Torvalds  */
2531da177e4SLinus Torvalds 
25499ac48f5SArjan van de Ven static int sound_insert_unit(struct sound_unit **list, const struct file_operations *fops, int index, int low, int top, const char *name, umode_t mode, struct device *dev)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
2571da177e4SLinus Torvalds 	int r;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	if (!s)
2601da177e4SLinus Torvalds 		return -ENOMEM;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	spin_lock(&sound_loader_lock);
26393fe4483STejun Heo retry:
2641da177e4SLinus Torvalds 	r = __sound_insert_unit(s, list, fops, index, low, top);
2651da177e4SLinus Torvalds 	spin_unlock(&sound_loader_lock);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	if (r < 0)
2681da177e4SLinus Torvalds 		goto fail;
2691da177e4SLinus Torvalds 	else if (r < SOUND_STEP)
2701da177e4SLinus Torvalds 		sprintf(s->name, "sound/%s", name);
2711da177e4SLinus Torvalds 	else
2721da177e4SLinus Torvalds 		sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
2731da177e4SLinus Torvalds 
27493fe4483STejun Heo 	if (!preclaim_oss) {
27593fe4483STejun Heo 		/*
27693fe4483STejun Heo 		 * Something else might have grabbed the minor.  If
27793fe4483STejun Heo 		 * first free slot is requested, rescan with @low set
27893fe4483STejun Heo 		 * to the next unit; otherwise, -EBUSY.
27993fe4483STejun Heo 		 */
28093fe4483STejun Heo 		r = __register_chrdev(SOUND_MAJOR, s->unit_minor, 1, s->name,
28193fe4483STejun Heo 				      &soundcore_fops);
28293fe4483STejun Heo 		if (r < 0) {
28393fe4483STejun Heo 			spin_lock(&sound_loader_lock);
28493fe4483STejun Heo 			__sound_remove_unit(list, s->unit_minor);
28593fe4483STejun Heo 			if (index < 0) {
28693fe4483STejun Heo 				low = s->unit_minor + SOUND_STEP;
28793fe4483STejun Heo 				goto retry;
28893fe4483STejun Heo 			}
28993fe4483STejun Heo 			spin_unlock(&sound_loader_lock);
29093fe4483STejun Heo 			return -EBUSY;
29193fe4483STejun Heo 		}
29293fe4483STejun Heo 	}
29393fe4483STejun Heo 
294abe9ab8fSGreg Kroah-Hartman 	device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
29589409211SGreg Kroah-Hartman 		      NULL, s->name+6);
29693fe4483STejun Heo 	return s->unit_minor;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds fail:
2991da177e4SLinus Torvalds 	kfree(s);
3001da177e4SLinus Torvalds 	return r;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds /*
3041da177e4SLinus Torvalds  *	Remove a unit. Acquires locks as needed. The drivers MUST have
3051da177e4SLinus Torvalds  *	completed the removal before their file operations become
3061da177e4SLinus Torvalds  *	invalid.
3071da177e4SLinus Torvalds  */
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds static void sound_remove_unit(struct sound_unit **list, int unit)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	struct sound_unit *p;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	spin_lock(&sound_loader_lock);
3141da177e4SLinus Torvalds 	p = __sound_remove_unit(list, unit);
3151da177e4SLinus Torvalds 	spin_unlock(&sound_loader_lock);
3161da177e4SLinus Torvalds 	if (p) {
31793fe4483STejun Heo 		if (!preclaim_oss)
31893fe4483STejun Heo 			__unregister_chrdev(SOUND_MAJOR, p->unit_minor, 1,
31993fe4483STejun Heo 					    p->name);
320d80f19faSGreg Kroah-Hartman 		device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
3211da177e4SLinus Torvalds 		kfree(p);
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds /*
3261da177e4SLinus Torvalds  *	Allocations
3271da177e4SLinus Torvalds  *
3281da177e4SLinus Torvalds  *	0	*16		Mixers
3291da177e4SLinus Torvalds  *	1	*8		Sequencers
3301da177e4SLinus Torvalds  *	2	*16		Midi
3311da177e4SLinus Torvalds  *	3	*16		DSP
3321da177e4SLinus Torvalds  *	4	*16		SunDSP
3331da177e4SLinus Torvalds  *	5	*16		DSP16
3341da177e4SLinus Torvalds  *	6	--		sndstat (obsolete)
3351da177e4SLinus Torvalds  *	7	*16		unused
3361da177e4SLinus Torvalds  *	8	--		alternate sequencer (see above)
3371da177e4SLinus Torvalds  *	9	*16		raw synthesizer access
3381da177e4SLinus Torvalds  *	10	*16		unused
3391da177e4SLinus Torvalds  *	11	*16		unused
3401da177e4SLinus Torvalds  *	12	*16		unused
3411da177e4SLinus Torvalds  *	13	*16		unused
3421da177e4SLinus Torvalds  *	14	*16		unused
3431da177e4SLinus Torvalds  *	15	*16		unused
3441da177e4SLinus Torvalds  */
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds static struct sound_unit *chains[SOUND_STEP];
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /**
349d568121cSTakashi Iwai  *	register_sound_special_device - register a special sound node
3501da177e4SLinus Torvalds  *	@fops: File operations for the driver
3511da177e4SLinus Torvalds  *	@unit: Unit number to allocate
352d568121cSTakashi Iwai  *      @dev: device pointer
3531da177e4SLinus Torvalds  *
3541da177e4SLinus Torvalds  *	Allocate a special sound device by minor number from the sound
355af901ca1SAndré Goddard Rosa  *	subsystem. The allocated number is returned on success. On failure
3561da177e4SLinus Torvalds  *	a negative error code is returned.
3571da177e4SLinus Torvalds  */
3581da177e4SLinus Torvalds 
35999ac48f5SArjan van de Ven int register_sound_special_device(const struct file_operations *fops, int unit,
360d568121cSTakashi Iwai 				  struct device *dev)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	const int chain = unit % SOUND_STEP;
3632abb8017STakashi Iwai 	int max_unit = 256;
3641da177e4SLinus Torvalds 	const char *name;
3651da177e4SLinus Torvalds 	char _name[16];
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	switch (chain) {
3681da177e4SLinus Torvalds 	    case 0:
3691da177e4SLinus Torvalds 		name = "mixer";
3701da177e4SLinus Torvalds 		break;
3711da177e4SLinus Torvalds 	    case 1:
3721da177e4SLinus Torvalds 		name = "sequencer";
3731da177e4SLinus Torvalds 		if (unit >= SOUND_STEP)
3741da177e4SLinus Torvalds 			goto __unknown;
3751da177e4SLinus Torvalds 		max_unit = unit + 1;
3761da177e4SLinus Torvalds 		break;
3771da177e4SLinus Torvalds 	    case 2:
3781da177e4SLinus Torvalds 		name = "midi";
3791da177e4SLinus Torvalds 		break;
3801da177e4SLinus Torvalds 	    case 3:
3811da177e4SLinus Torvalds 		name = "dsp";
3821da177e4SLinus Torvalds 		break;
3831da177e4SLinus Torvalds 	    case 4:
3841da177e4SLinus Torvalds 		name = "audio";
3851da177e4SLinus Torvalds 		break;
386848669daSTakashi Iwai 	    case 5:
387848669daSTakashi Iwai 		name = "dspW";
388848669daSTakashi Iwai 		break;
3891da177e4SLinus Torvalds 	    case 8:
3901da177e4SLinus Torvalds 		name = "sequencer2";
3911da177e4SLinus Torvalds 		if (unit >= SOUND_STEP)
3921da177e4SLinus Torvalds 			goto __unknown;
3931da177e4SLinus Torvalds 		max_unit = unit + 1;
3941da177e4SLinus Torvalds 		break;
3951da177e4SLinus Torvalds 	    case 9:
3961da177e4SLinus Torvalds 		name = "dmmidi";
3971da177e4SLinus Torvalds 		break;
3981da177e4SLinus Torvalds 	    case 10:
3991da177e4SLinus Torvalds 		name = "dmfm";
4001da177e4SLinus Torvalds 		break;
4011da177e4SLinus Torvalds 	    case 12:
4021da177e4SLinus Torvalds 		name = "adsp";
4031da177e4SLinus Torvalds 		break;
4041da177e4SLinus Torvalds 	    case 13:
4051da177e4SLinus Torvalds 		name = "amidi";
4061da177e4SLinus Torvalds 		break;
4071da177e4SLinus Torvalds 	    case 14:
4081da177e4SLinus Torvalds 		name = "admmidi";
4091da177e4SLinus Torvalds 		break;
4101da177e4SLinus Torvalds 	    default:
4111da177e4SLinus Torvalds 	    	{
4121da177e4SLinus Torvalds 		    __unknown:
4131da177e4SLinus Torvalds 			sprintf(_name, "unknown%d", chain);
4141da177e4SLinus Torvalds 		    	if (unit >= SOUND_STEP)
4151da177e4SLinus Torvalds 		    		strcat(_name, "-");
4161da177e4SLinus Torvalds 		    	name = _name;
4171da177e4SLinus Torvalds 		}
4181da177e4SLinus Torvalds 		break;
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 	return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
421d568121cSTakashi Iwai 				 name, S_IRUSR | S_IWUSR, dev);
422d568121cSTakashi Iwai }
423d568121cSTakashi Iwai 
424d568121cSTakashi Iwai EXPORT_SYMBOL(register_sound_special_device);
425d568121cSTakashi Iwai 
42699ac48f5SArjan van de Ven int register_sound_special(const struct file_operations *fops, int unit)
427d568121cSTakashi Iwai {
428d568121cSTakashi Iwai 	return register_sound_special_device(fops, unit, NULL);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_special);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds /**
4341da177e4SLinus Torvalds  *	register_sound_mixer - register a mixer device
4351da177e4SLinus Torvalds  *	@fops: File operations for the driver
4361da177e4SLinus Torvalds  *	@dev: Unit number to allocate
4371da177e4SLinus Torvalds  *
4381da177e4SLinus Torvalds  *	Allocate a mixer device. Unit is the number of the mixer requested.
4391da177e4SLinus Torvalds  *	Pass -1 to request the next free mixer unit. On success the allocated
4401da177e4SLinus Torvalds  *	number is returned, on failure a negative error code is returned.
4411da177e4SLinus Torvalds  */
4421da177e4SLinus Torvalds 
44399ac48f5SArjan van de Ven int register_sound_mixer(const struct file_operations *fops, int dev)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	return sound_insert_unit(&chains[0], fops, dev, 0, 128,
446d568121cSTakashi Iwai 				 "mixer", S_IRUSR | S_IWUSR, NULL);
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_mixer);
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds /**
4521da177e4SLinus Torvalds  *	register_sound_midi - register a midi device
4531da177e4SLinus Torvalds  *	@fops: File operations for the driver
4541da177e4SLinus Torvalds  *	@dev: Unit number to allocate
4551da177e4SLinus Torvalds  *
4561da177e4SLinus Torvalds  *	Allocate a midi device. Unit is the number of the midi device requested.
4571da177e4SLinus Torvalds  *	Pass -1 to request the next free midi unit. On success the allocated
4581da177e4SLinus Torvalds  *	number is returned, on failure a negative error code is returned.
4591da177e4SLinus Torvalds  */
4601da177e4SLinus Torvalds 
46199ac48f5SArjan van de Ven int register_sound_midi(const struct file_operations *fops, int dev)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	return sound_insert_unit(&chains[2], fops, dev, 2, 130,
464d568121cSTakashi Iwai 				 "midi", S_IRUSR | S_IWUSR, NULL);
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_midi);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  *	DSP's are registered as a triple. Register only one and cheat
4711da177e4SLinus Torvalds  *	in open - see below.
4721da177e4SLinus Torvalds  */
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds /**
4751da177e4SLinus Torvalds  *	register_sound_dsp - register a DSP device
4761da177e4SLinus Torvalds  *	@fops: File operations for the driver
4771da177e4SLinus Torvalds  *	@dev: Unit number to allocate
4781da177e4SLinus Torvalds  *
4791da177e4SLinus Torvalds  *	Allocate a DSP device. Unit is the number of the DSP requested.
4801da177e4SLinus Torvalds  *	Pass -1 to request the next free DSP unit. On success the allocated
4811da177e4SLinus Torvalds  *	number is returned, on failure a negative error code is returned.
4821da177e4SLinus Torvalds  *
4831da177e4SLinus Torvalds  *	This function allocates both the audio and dsp device entries together
4841da177e4SLinus Torvalds  *	and will always allocate them as a matching pair - eg dsp3/audio3
4851da177e4SLinus Torvalds  */
4861da177e4SLinus Torvalds 
48799ac48f5SArjan van de Ven int register_sound_dsp(const struct file_operations *fops, int dev)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	return sound_insert_unit(&chains[3], fops, dev, 3, 131,
490d568121cSTakashi Iwai 				 "dsp", S_IWUSR | S_IRUSR, NULL);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_dsp);
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds /**
4961da177e4SLinus Torvalds  *	unregister_sound_special - unregister a special sound device
4971da177e4SLinus Torvalds  *	@unit: unit number to allocate
4981da177e4SLinus Torvalds  *
4991da177e4SLinus Torvalds  *	Release a sound device that was allocated with
5001da177e4SLinus Torvalds  *	register_sound_special(). The unit passed is the return value from
5011da177e4SLinus Torvalds  *	the register function.
5021da177e4SLinus Torvalds  */
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds void unregister_sound_special(int unit)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	sound_remove_unit(&chains[unit % SOUND_STEP], unit);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_special);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds /**
5131da177e4SLinus Torvalds  *	unregister_sound_mixer - unregister a mixer
5141da177e4SLinus Torvalds  *	@unit: unit number to allocate
5151da177e4SLinus Torvalds  *
5161da177e4SLinus Torvalds  *	Release a sound device that was allocated with register_sound_mixer().
5171da177e4SLinus Torvalds  *	The unit passed is the return value from the register function.
5181da177e4SLinus Torvalds  */
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds void unregister_sound_mixer(int unit)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds 	sound_remove_unit(&chains[0], unit);
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_mixer);
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds /**
5281da177e4SLinus Torvalds  *	unregister_sound_midi - unregister a midi device
5291da177e4SLinus Torvalds  *	@unit: unit number to allocate
5301da177e4SLinus Torvalds  *
5311da177e4SLinus Torvalds  *	Release a sound device that was allocated with register_sound_midi().
5321da177e4SLinus Torvalds  *	The unit passed is the return value from the register function.
5331da177e4SLinus Torvalds  */
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds void unregister_sound_midi(int unit)
5361da177e4SLinus Torvalds {
537a39c4ad1SHannes Eder 	sound_remove_unit(&chains[2], unit);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_midi);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds /**
5431da177e4SLinus Torvalds  *	unregister_sound_dsp - unregister a DSP device
5441da177e4SLinus Torvalds  *	@unit: unit number to allocate
5451da177e4SLinus Torvalds  *
5461da177e4SLinus Torvalds  *	Release a sound device that was allocated with register_sound_dsp().
5471da177e4SLinus Torvalds  *	The unit passed is the return value from the register function.
5481da177e4SLinus Torvalds  *
5491da177e4SLinus Torvalds  *	Both of the allocated units are released together automatically.
5501da177e4SLinus Torvalds  */
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds void unregister_sound_dsp(int unit)
5531da177e4SLinus Torvalds {
554a39c4ad1SHannes Eder 	sound_remove_unit(&chains[3], unit);
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_dsp);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds static struct sound_unit *__look_for_unit(int chain, int unit)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	struct sound_unit *s;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	s=chains[chain];
5651da177e4SLinus Torvalds 	while(s && s->unit_minor <= unit)
5661da177e4SLinus Torvalds 	{
5671da177e4SLinus Torvalds 		if(s->unit_minor==unit)
5681da177e4SLinus Torvalds 			return s;
5691da177e4SLinus Torvalds 		s=s->next;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 	return NULL;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
574a39c4ad1SHannes Eder static int soundcore_open(struct inode *inode, struct file *file)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	int chain;
5771da177e4SLinus Torvalds 	int unit = iminor(inode);
5781da177e4SLinus Torvalds 	struct sound_unit *s;
57999ac48f5SArjan van de Ven 	const struct file_operations *new_fops = NULL;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	chain=unit&0x0F;
5821da177e4SLinus Torvalds 	if(chain==4 || chain==5)	/* dsp/audio/dsp16 */
5831da177e4SLinus Torvalds 	{
5841da177e4SLinus Torvalds 		unit&=0xF0;
5851da177e4SLinus Torvalds 		unit|=3;
5861da177e4SLinus Torvalds 		chain=3;
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	spin_lock(&sound_loader_lock);
5901da177e4SLinus Torvalds 	s = __look_for_unit(chain, unit);
5911da177e4SLinus Torvalds 	if (s)
5921da177e4SLinus Torvalds 		new_fops = fops_get(s->unit_fops);
59393fe4483STejun Heo 	if (preclaim_oss && !new_fops) {
5941da177e4SLinus Torvalds 		spin_unlock(&sound_loader_lock);
5950a848680STejun Heo 
5961da177e4SLinus Torvalds 		/*
5971da177e4SLinus Torvalds 		 *  Please, don't change this order or code.
5981da177e4SLinus Torvalds 		 *  For ALSA slot means soundcard and OSS emulation code
5991da177e4SLinus Torvalds 		 *  comes as add-on modules which aren't depend on
6001da177e4SLinus Torvalds 		 *  ALSA toplevel modules for soundcards, thus we need
6011da177e4SLinus Torvalds 		 *  load them at first.	  [Jaroslav Kysela <perex@jcu.cz>]
6021da177e4SLinus Torvalds 		 */
6031da177e4SLinus Torvalds 		request_module("sound-slot-%i", unit>>4);
6041da177e4SLinus Torvalds 		request_module("sound-service-%i-%i", unit>>4, chain);
6050a848680STejun Heo 
6060a848680STejun Heo 		/*
6070a848680STejun Heo 		 * sound-slot/service-* module aliases are scheduled
6080a848680STejun Heo 		 * for removal in favor of the standard char-major-*
6090a848680STejun Heo 		 * module aliases.  For the time being, generate both
6100a848680STejun Heo 		 * the legacy and standard module aliases to ease
6110a848680STejun Heo 		 * transition.
6120a848680STejun Heo 		 */
6130a848680STejun Heo 		if (request_module("char-major-%d-%d", SOUND_MAJOR, unit) > 0)
6140a848680STejun Heo 			request_module("char-major-%d", SOUND_MAJOR);
6150a848680STejun Heo 
6161da177e4SLinus Torvalds 		spin_lock(&sound_loader_lock);
6171da177e4SLinus Torvalds 		s = __look_for_unit(chain, unit);
6181da177e4SLinus Torvalds 		if (s)
6191da177e4SLinus Torvalds 			new_fops = fops_get(s->unit_fops);
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 	if (new_fops) {
6221da177e4SLinus Torvalds 		/*
6231da177e4SLinus Torvalds 		 * We rely upon the fact that we can't be unloaded while the
6241da177e4SLinus Torvalds 		 * subdriver is there, so if ->open() is successful we can
6251da177e4SLinus Torvalds 		 * safely drop the reference counter and if it is not we can
6261da177e4SLinus Torvalds 		 * revert to old ->f_op. Ugly, indeed, but that's the cost of
6271da177e4SLinus Torvalds 		 * switching ->f_op in the first place.
6281da177e4SLinus Torvalds 		 */
6291da177e4SLinus Torvalds 		int err = 0;
63099ac48f5SArjan van de Ven 		const struct file_operations *old_fops = file->f_op;
6311da177e4SLinus Torvalds 		file->f_op = new_fops;
6321da177e4SLinus Torvalds 		spin_unlock(&sound_loader_lock);
633171d9f7dSJohn Kacur 
63490dc763fSArnd Bergmann 		if (file->f_op->open)
6351da177e4SLinus Torvalds 			err = file->f_op->open(inode,file);
636171d9f7dSJohn Kacur 
6371da177e4SLinus Torvalds 		if (err) {
6381da177e4SLinus Torvalds 			fops_put(file->f_op);
6391da177e4SLinus Torvalds 			file->f_op = fops_get(old_fops);
6401da177e4SLinus Torvalds 		}
641171d9f7dSJohn Kacur 
6421da177e4SLinus Torvalds 		fops_put(old_fops);
6431da177e4SLinus Torvalds 		return err;
6441da177e4SLinus Torvalds 	}
6451da177e4SLinus Torvalds 	spin_unlock(&sound_loader_lock);
6461da177e4SLinus Torvalds 	return -ENODEV;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
6501da177e4SLinus Torvalds 
651de30d36bSKrzysztof Helt static void cleanup_oss_soundcore(void)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	/* We have nothing to really do here - we know the lists must be
6541da177e4SLinus Torvalds 	   empty */
6551da177e4SLinus Torvalds 	unregister_chrdev(SOUND_MAJOR, "sound");
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
658d886e87cSTejun Heo static int __init init_oss_soundcore(void)
6591da177e4SLinus Torvalds {
66093fe4483STejun Heo 	if (preclaim_oss &&
66193fe4483STejun Heo 	    register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops) == -1) {
6621da177e4SLinus Torvalds 		printk(KERN_ERR "soundcore: sound device already in use.\n");
6631da177e4SLinus Torvalds 		return -EBUSY;
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	return 0;
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
669d886e87cSTejun Heo #endif /* CONFIG_SOUND_OSS_CORE */
670