xref: /openbmc/linux/sound/sound_core.c (revision d248b2771f543417dea7f9af8be05190594e0621)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
3d886e87cSTejun Heo  *	Sound core.  This file is composed of two parts.  sound_class
4d886e87cSTejun Heo  *	which is common to both OSS and ALSA and OSS sound core which
5d886e87cSTejun Heo  *	is used OSS or emulation of it.
6d886e87cSTejun Heo  */
7d886e87cSTejun Heo 
8d886e87cSTejun Heo /*
9d886e87cSTejun Heo  * First, the common part.
10d886e87cSTejun Heo  */
11d886e87cSTejun Heo #include <linux/module.h>
12d886e87cSTejun Heo #include <linux/device.h>
13d886e87cSTejun Heo #include <linux/err.h>
14954a973cSKay Sievers #include <linux/kdev_t.h>
15954a973cSKay Sievers #include <linux/major.h>
163d1ee379SMark Brown #include <sound/core.h>
17d886e87cSTejun Heo 
18d886e87cSTejun Heo #ifdef CONFIG_SOUND_OSS_CORE
19d886e87cSTejun Heo static int __init init_oss_soundcore(void);
20de30d36bSKrzysztof Helt static void cleanup_oss_soundcore(void);
21d886e87cSTejun Heo #else
22d886e87cSTejun Heo static inline int init_oss_soundcore(void)	{ return 0; }
23d886e87cSTejun Heo static inline void cleanup_oss_soundcore(void)	{ }
24d886e87cSTejun Heo #endif
25d886e87cSTejun Heo 
26d886e87cSTejun Heo struct class *sound_class;
27d886e87cSTejun Heo EXPORT_SYMBOL(sound_class);
28d886e87cSTejun Heo 
29d886e87cSTejun Heo MODULE_DESCRIPTION("Core sound module");
30d886e87cSTejun Heo MODULE_AUTHOR("Alan Cox");
31d886e87cSTejun Heo MODULE_LICENSE("GPL");
32d886e87cSTejun Heo 
332c9ede55SAl Viro static char *sound_devnode(struct device *dev, umode_t *mode)
347a9d56f6SKay Sievers {
35954a973cSKay Sievers 	if (MAJOR(dev->devt) == SOUND_MAJOR)
36954a973cSKay Sievers 		return NULL;
377a9d56f6SKay Sievers 	return kasprintf(GFP_KERNEL, "snd/%s", dev_name(dev));
387a9d56f6SKay Sievers }
397a9d56f6SKay Sievers 
40d886e87cSTejun Heo static int __init init_soundcore(void)
41d886e87cSTejun Heo {
42d886e87cSTejun Heo 	int rc;
43d886e87cSTejun Heo 
44d886e87cSTejun Heo 	rc = init_oss_soundcore();
45d886e87cSTejun Heo 	if (rc)
46d886e87cSTejun Heo 		return rc;
47d886e87cSTejun Heo 
48d886e87cSTejun Heo 	sound_class = class_create(THIS_MODULE, "sound");
49d886e87cSTejun Heo 	if (IS_ERR(sound_class)) {
50d886e87cSTejun Heo 		cleanup_oss_soundcore();
51d886e87cSTejun Heo 		return PTR_ERR(sound_class);
52d886e87cSTejun Heo 	}
53d886e87cSTejun Heo 
54e454cea2SKay Sievers 	sound_class->devnode = sound_devnode;
557a9d56f6SKay Sievers 
56d886e87cSTejun Heo 	return 0;
57d886e87cSTejun Heo }
58d886e87cSTejun Heo 
59d886e87cSTejun Heo static void __exit cleanup_soundcore(void)
60d886e87cSTejun Heo {
61d886e87cSTejun Heo 	cleanup_oss_soundcore();
62d886e87cSTejun Heo 	class_destroy(sound_class);
63d886e87cSTejun Heo }
64d886e87cSTejun Heo 
65c181a13aSThadeu Lima de Souza Cascardo subsys_initcall(init_soundcore);
66d886e87cSTejun Heo module_exit(cleanup_soundcore);
67d886e87cSTejun Heo 
68d886e87cSTejun Heo 
69d886e87cSTejun Heo #ifdef CONFIG_SOUND_OSS_CORE
70d886e87cSTejun Heo /*
71d886e87cSTejun Heo  *	OSS sound core handling. Breaks out sound functions to submodules
721da177e4SLinus Torvalds  *
732f1e593dSAlan Cox  *	Author:		Alan Cox <alan@lxorguk.ukuu.org.uk>
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  *	Fixes:
761da177e4SLinus Torvalds  *
771da177e4SLinus Torvalds  *                         --------------------
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  *	Top level handler for the sound subsystem. Various devices can
801da177e4SLinus Torvalds  *	plug into this. The fact they don't all go via OSS doesn't mean
811da177e4SLinus Torvalds  *	they don't have to implement the OSS API. There is a lot of logic
821da177e4SLinus Torvalds  *	to keeping much of the OSS weight out of the code in a compatibility
831da177e4SLinus Torvalds  *	module, but it's up to the driver to rember to load it...
841da177e4SLinus Torvalds  *
851da177e4SLinus Torvalds  *	The code provides a set of functions for registration of devices
861da177e4SLinus Torvalds  *	by type. This is done rather than providing a single call so that
871da177e4SLinus Torvalds  *	we can hide any future changes in the internals (eg when we go to
881da177e4SLinus Torvalds  *	32bit dev_t) from the modules and their interface.
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  *	Secondly we need to allocate the dsp, dsp16 and audio devices as
911da177e4SLinus Torvalds  *	one. Thus we misuse the chains a bit to simplify this.
921da177e4SLinus Torvalds  *
931da177e4SLinus Torvalds  *	Thirdly to make it more fun and for 2.3.x and above we do all
941da177e4SLinus Torvalds  *	of this using fine grained locking.
951da177e4SLinus Torvalds  *
961da177e4SLinus Torvalds  *	FIXME: we have to resolve modules and fine grained load/unload
971da177e4SLinus Torvalds  *	locking at some point in 2.3.x.
981da177e4SLinus Torvalds  */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #include <linux/init.h>
1011da177e4SLinus Torvalds #include <linux/slab.h>
1021da177e4SLinus Torvalds #include <linux/types.h>
1031da177e4SLinus Torvalds #include <linux/kernel.h>
1041da177e4SLinus Torvalds #include <linux/sound.h>
1051da177e4SLinus Torvalds #include <linux/kmod.h>
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #define SOUND_STEP 16
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds struct sound_unit
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	int unit_minor;
11299ac48f5SArjan van de Ven 	const struct file_operations *unit_fops;
1131da177e4SLinus Torvalds 	struct sound_unit *next;
1141da177e4SLinus Torvalds 	char name[32];
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /*
11893fe4483STejun Heo  * By default, OSS sound_core claims full legacy minor range (0-255)
11993fe4483STejun Heo  * of SOUND_MAJOR to trap open attempts to any sound minor and
12093fe4483STejun Heo  * requests modules using custom sound-slot/service-* module aliases.
12193fe4483STejun Heo  * The only benefit of doing this is allowing use of custom module
12293fe4483STejun Heo  * aliases instead of the standard char-major-* ones.  This behavior
12393fe4483STejun Heo  * prevents alternative OSS implementation and is scheduled to be
12493fe4483STejun Heo  * removed.
12593fe4483STejun Heo  *
12693fe4483STejun Heo  * CONFIG_SOUND_OSS_CORE_PRECLAIM and soundcore.preclaim_oss kernel
12793fe4483STejun Heo  * parameter are added to allow distros and developers to try and
12893fe4483STejun Heo  * switch to alternative implementations without needing to rebuild
12993fe4483STejun Heo  * the kernel in the meantime.  If preclaim_oss is non-zero, the
13093fe4483STejun Heo  * kernel will behave the same as before.  All SOUND_MAJOR minors are
13193fe4483STejun Heo  * preclaimed and the custom module aliases along with standard chrdev
13293fe4483STejun Heo  * ones are emitted if a missing device is opened.  If preclaim_oss is
13393fe4483STejun Heo  * zero, sound_core only grabs what's actually in use and for missing
13493fe4483STejun Heo  * devices only the standard chrdev aliases are requested.
13593fe4483STejun Heo  *
13693fe4483STejun Heo  * All these clutters are scheduled to be removed along with
1378f6e6041STao Ma  * sound-slot/service-* module aliases.
13893fe4483STejun Heo  */
139*d248b277SMeng Tang static int preclaim_oss = IS_ENABLED(CONFIG_SOUND_OSS_CORE_PRECLAIM);
14093fe4483STejun Heo 
14193fe4483STejun Heo module_param(preclaim_oss, int, 0444);
14293fe4483STejun Heo 
14393fe4483STejun Heo static int soundcore_open(struct inode *, struct file *);
14493fe4483STejun Heo 
14593fe4483STejun Heo static const struct file_operations soundcore_fops =
14693fe4483STejun Heo {
14793fe4483STejun Heo 	/* We must have an owner or the module locking fails */
14893fe4483STejun Heo 	.owner	= THIS_MODULE,
14993fe4483STejun Heo 	.open	= soundcore_open,
1506038f373SArnd Bergmann 	.llseek = noop_llseek,
15193fe4483STejun Heo };
15293fe4483STejun Heo 
15393fe4483STejun Heo /*
1541da177e4SLinus Torvalds  *	Low level list operator. Scan the ordered list, find a hole and
1551da177e4SLinus Torvalds  *	join into it. Called with the lock asserted
1561da177e4SLinus Torvalds  */
1571da177e4SLinus Torvalds 
15899ac48f5SArjan 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)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	int n=low;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	if (index < 0) {	/* first free */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 		while (*list && (*list)->unit_minor<n)
1651da177e4SLinus Torvalds 			list=&((*list)->next);
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 		while(n<top)
1681da177e4SLinus Torvalds 		{
1691da177e4SLinus Torvalds 			/* Found a hole ? */
1701da177e4SLinus Torvalds 			if(*list==NULL || (*list)->unit_minor>n)
1711da177e4SLinus Torvalds 				break;
1721da177e4SLinus Torvalds 			list=&((*list)->next);
1731da177e4SLinus Torvalds 			n+=SOUND_STEP;
1741da177e4SLinus Torvalds 		}
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 		if(n>=top)
1771da177e4SLinus Torvalds 			return -ENOENT;
1781da177e4SLinus Torvalds 	} else {
1791da177e4SLinus Torvalds 		n = low+(index*16);
1801da177e4SLinus Torvalds 		while (*list) {
1811da177e4SLinus Torvalds 			if ((*list)->unit_minor==n)
1821da177e4SLinus Torvalds 				return -EBUSY;
1831da177e4SLinus Torvalds 			if ((*list)->unit_minor>n)
1841da177e4SLinus Torvalds 				break;
1851da177e4SLinus Torvalds 			list=&((*list)->next);
1861da177e4SLinus Torvalds 		}
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	/*
1901da177e4SLinus Torvalds 	 *	Fill it in
1911da177e4SLinus Torvalds 	 */
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	s->unit_minor=n;
1941da177e4SLinus Torvalds 	s->unit_fops=fops;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	/*
1971da177e4SLinus Torvalds 	 *	Link it
1981da177e4SLinus Torvalds 	 */
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	s->next=*list;
2011da177e4SLinus Torvalds 	*list=s;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	return n;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /*
2081da177e4SLinus Torvalds  *	Remove a node from the chain. Called with the lock asserted
2091da177e4SLinus Torvalds  */
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	while(*list)
2141da177e4SLinus Torvalds 	{
2151da177e4SLinus Torvalds 		struct sound_unit *p=*list;
2161da177e4SLinus Torvalds 		if(p->unit_minor==unit)
2171da177e4SLinus Torvalds 		{
2181da177e4SLinus Torvalds 			*list=p->next;
2191da177e4SLinus Torvalds 			return p;
2201da177e4SLinus Torvalds 		}
2211da177e4SLinus Torvalds 		list=&(p->next);
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 	printk(KERN_ERR "Sound device %d went missing!\n", unit);
2241da177e4SLinus Torvalds 	return NULL;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds /*
2281da177e4SLinus Torvalds  *	This lock guards the sound loader list.
2291da177e4SLinus Torvalds  */
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds static DEFINE_SPINLOCK(sound_loader_lock);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds /*
2341da177e4SLinus Torvalds  *	Allocate the controlling structure and add it to the sound driver
2351da177e4SLinus Torvalds  *	list. Acquires locks as needed
2361da177e4SLinus Torvalds  */
2371da177e4SLinus Torvalds 
23899ac48f5SArjan 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)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
2411da177e4SLinus Torvalds 	int r;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	if (!s)
2441da177e4SLinus Torvalds 		return -ENOMEM;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	spin_lock(&sound_loader_lock);
24793fe4483STejun Heo retry:
2481da177e4SLinus Torvalds 	r = __sound_insert_unit(s, list, fops, index, low, top);
2491da177e4SLinus Torvalds 	spin_unlock(&sound_loader_lock);
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	if (r < 0)
2521da177e4SLinus Torvalds 		goto fail;
2531da177e4SLinus Torvalds 	else if (r < SOUND_STEP)
2541da177e4SLinus Torvalds 		sprintf(s->name, "sound/%s", name);
2551da177e4SLinus Torvalds 	else
2561da177e4SLinus Torvalds 		sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
2571da177e4SLinus Torvalds 
25893fe4483STejun Heo 	if (!preclaim_oss) {
25993fe4483STejun Heo 		/*
26093fe4483STejun Heo 		 * Something else might have grabbed the minor.  If
26193fe4483STejun Heo 		 * first free slot is requested, rescan with @low set
26293fe4483STejun Heo 		 * to the next unit; otherwise, -EBUSY.
26393fe4483STejun Heo 		 */
26493fe4483STejun Heo 		r = __register_chrdev(SOUND_MAJOR, s->unit_minor, 1, s->name,
26593fe4483STejun Heo 				      &soundcore_fops);
26693fe4483STejun Heo 		if (r < 0) {
26793fe4483STejun Heo 			spin_lock(&sound_loader_lock);
26893fe4483STejun Heo 			__sound_remove_unit(list, s->unit_minor);
26993fe4483STejun Heo 			if (index < 0) {
27093fe4483STejun Heo 				low = s->unit_minor + SOUND_STEP;
27193fe4483STejun Heo 				goto retry;
27293fe4483STejun Heo 			}
27393fe4483STejun Heo 			spin_unlock(&sound_loader_lock);
274c7cd7c74SWenwen Wang 			r = -EBUSY;
275c7cd7c74SWenwen Wang 			goto fail;
27693fe4483STejun Heo 		}
27793fe4483STejun Heo 	}
27893fe4483STejun Heo 
279abe9ab8fSGreg Kroah-Hartman 	device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
28002aa2a37SKees Cook 		      NULL, "%s", s->name+6);
28193fe4483STejun Heo 	return s->unit_minor;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds fail:
2841da177e4SLinus Torvalds 	kfree(s);
2851da177e4SLinus Torvalds 	return r;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds /*
2891da177e4SLinus Torvalds  *	Remove a unit. Acquires locks as needed. The drivers MUST have
2901da177e4SLinus Torvalds  *	completed the removal before their file operations become
2911da177e4SLinus Torvalds  *	invalid.
2921da177e4SLinus Torvalds  */
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static void sound_remove_unit(struct sound_unit **list, int unit)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	struct sound_unit *p;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	spin_lock(&sound_loader_lock);
2991da177e4SLinus Torvalds 	p = __sound_remove_unit(list, unit);
3001da177e4SLinus Torvalds 	spin_unlock(&sound_loader_lock);
3011da177e4SLinus Torvalds 	if (p) {
30293fe4483STejun Heo 		if (!preclaim_oss)
30393fe4483STejun Heo 			__unregister_chrdev(SOUND_MAJOR, p->unit_minor, 1,
30493fe4483STejun Heo 					    p->name);
305d80f19faSGreg Kroah-Hartman 		device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
3061da177e4SLinus Torvalds 		kfree(p);
3071da177e4SLinus Torvalds 	}
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds /*
3111da177e4SLinus Torvalds  *	Allocations
3121da177e4SLinus Torvalds  *
3131da177e4SLinus Torvalds  *	0	*16		Mixers
3141da177e4SLinus Torvalds  *	1	*8		Sequencers
3151da177e4SLinus Torvalds  *	2	*16		Midi
3161da177e4SLinus Torvalds  *	3	*16		DSP
3171da177e4SLinus Torvalds  *	4	*16		SunDSP
3181da177e4SLinus Torvalds  *	5	*16		DSP16
3191da177e4SLinus Torvalds  *	6	--		sndstat (obsolete)
3201da177e4SLinus Torvalds  *	7	*16		unused
3211da177e4SLinus Torvalds  *	8	--		alternate sequencer (see above)
3221da177e4SLinus Torvalds  *	9	*16		raw synthesizer access
3231da177e4SLinus Torvalds  *	10	*16		unused
3241da177e4SLinus Torvalds  *	11	*16		unused
3251da177e4SLinus Torvalds  *	12	*16		unused
3261da177e4SLinus Torvalds  *	13	*16		unused
3271da177e4SLinus Torvalds  *	14	*16		unused
3281da177e4SLinus Torvalds  *	15	*16		unused
3291da177e4SLinus Torvalds  */
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static struct sound_unit *chains[SOUND_STEP];
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds /**
334d568121cSTakashi Iwai  *	register_sound_special_device - register a special sound node
3351da177e4SLinus Torvalds  *	@fops: File operations for the driver
3361da177e4SLinus Torvalds  *	@unit: Unit number to allocate
337d568121cSTakashi Iwai  *      @dev: device pointer
3381da177e4SLinus Torvalds  *
3391da177e4SLinus Torvalds  *	Allocate a special sound device by minor number from the sound
340eb7c06e8SYacine Belkadi  *	subsystem.
341eb7c06e8SYacine Belkadi  *
342eb7c06e8SYacine Belkadi  *	Return: The allocated number is returned on success. On failure,
3431da177e4SLinus Torvalds  *	a negative error code is returned.
3441da177e4SLinus Torvalds  */
3451da177e4SLinus Torvalds 
34699ac48f5SArjan van de Ven int register_sound_special_device(const struct file_operations *fops, int unit,
347d568121cSTakashi Iwai 				  struct device *dev)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	const int chain = unit % SOUND_STEP;
3502abb8017STakashi Iwai 	int max_unit = 256;
3511da177e4SLinus Torvalds 	const char *name;
3521da177e4SLinus Torvalds 	char _name[16];
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	switch (chain) {
3551da177e4SLinus Torvalds 	    case 0:
3561da177e4SLinus Torvalds 		name = "mixer";
3571da177e4SLinus Torvalds 		break;
3581da177e4SLinus Torvalds 	    case 1:
3591da177e4SLinus Torvalds 		name = "sequencer";
3601da177e4SLinus Torvalds 		if (unit >= SOUND_STEP)
3611da177e4SLinus Torvalds 			goto __unknown;
3621da177e4SLinus Torvalds 		max_unit = unit + 1;
3631da177e4SLinus Torvalds 		break;
3641da177e4SLinus Torvalds 	    case 2:
3651da177e4SLinus Torvalds 		name = "midi";
3661da177e4SLinus Torvalds 		break;
3671da177e4SLinus Torvalds 	    case 3:
3681da177e4SLinus Torvalds 		name = "dsp";
3691da177e4SLinus Torvalds 		break;
3701da177e4SLinus Torvalds 	    case 4:
3711da177e4SLinus Torvalds 		name = "audio";
3721da177e4SLinus Torvalds 		break;
373848669daSTakashi Iwai 	    case 5:
374848669daSTakashi Iwai 		name = "dspW";
375848669daSTakashi Iwai 		break;
3761da177e4SLinus Torvalds 	    case 8:
3771da177e4SLinus Torvalds 		name = "sequencer2";
3781da177e4SLinus Torvalds 		if (unit >= SOUND_STEP)
3791da177e4SLinus Torvalds 			goto __unknown;
3801da177e4SLinus Torvalds 		max_unit = unit + 1;
3811da177e4SLinus Torvalds 		break;
3821da177e4SLinus Torvalds 	    case 9:
3831da177e4SLinus Torvalds 		name = "dmmidi";
3841da177e4SLinus Torvalds 		break;
3851da177e4SLinus Torvalds 	    case 10:
3861da177e4SLinus Torvalds 		name = "dmfm";
3871da177e4SLinus Torvalds 		break;
3881da177e4SLinus Torvalds 	    case 12:
3891da177e4SLinus Torvalds 		name = "adsp";
3901da177e4SLinus Torvalds 		break;
3911da177e4SLinus Torvalds 	    case 13:
3921da177e4SLinus Torvalds 		name = "amidi";
3931da177e4SLinus Torvalds 		break;
3941da177e4SLinus Torvalds 	    case 14:
3951da177e4SLinus Torvalds 		name = "admmidi";
3961da177e4SLinus Torvalds 		break;
3971da177e4SLinus Torvalds 	    default:
3981da177e4SLinus Torvalds 	    	{
3991da177e4SLinus Torvalds 		    __unknown:
4001da177e4SLinus Torvalds 			sprintf(_name, "unknown%d", chain);
4011da177e4SLinus Torvalds 		    	if (unit >= SOUND_STEP)
4021da177e4SLinus Torvalds 		    		strcat(_name, "-");
4031da177e4SLinus Torvalds 		    	name = _name;
4041da177e4SLinus Torvalds 		}
4051da177e4SLinus Torvalds 		break;
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 	return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
4086a73cf46SJoe Perches 				 name, 0600, dev);
409d568121cSTakashi Iwai }
410d568121cSTakashi Iwai 
411d568121cSTakashi Iwai EXPORT_SYMBOL(register_sound_special_device);
412d568121cSTakashi Iwai 
41399ac48f5SArjan van de Ven int register_sound_special(const struct file_operations *fops, int unit)
414d568121cSTakashi Iwai {
415d568121cSTakashi Iwai 	return register_sound_special_device(fops, unit, NULL);
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_special);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds /**
4211da177e4SLinus Torvalds  *	register_sound_mixer - register a mixer device
4221da177e4SLinus Torvalds  *	@fops: File operations for the driver
4231da177e4SLinus Torvalds  *	@dev: Unit number to allocate
4241da177e4SLinus Torvalds  *
4251da177e4SLinus Torvalds  *	Allocate a mixer device. Unit is the number of the mixer requested.
426eb7c06e8SYacine Belkadi  *	Pass -1 to request the next free mixer unit.
427eb7c06e8SYacine Belkadi  *
428eb7c06e8SYacine Belkadi  *	Return: On success, the allocated number is returned. On failure,
429eb7c06e8SYacine Belkadi  *	a negative error code is returned.
4301da177e4SLinus Torvalds  */
4311da177e4SLinus Torvalds 
43299ac48f5SArjan van de Ven int register_sound_mixer(const struct file_operations *fops, int dev)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	return sound_insert_unit(&chains[0], fops, dev, 0, 128,
4356a73cf46SJoe Perches 				 "mixer", 0600, NULL);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_mixer);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds /*
4411da177e4SLinus Torvalds  *	DSP's are registered as a triple. Register only one and cheat
4421da177e4SLinus Torvalds  *	in open - see below.
4431da177e4SLinus Torvalds  */
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds /**
4461da177e4SLinus Torvalds  *	register_sound_dsp - register a DSP device
4471da177e4SLinus Torvalds  *	@fops: File operations for the driver
4481da177e4SLinus Torvalds  *	@dev: Unit number to allocate
4491da177e4SLinus Torvalds  *
4501da177e4SLinus Torvalds  *	Allocate a DSP device. Unit is the number of the DSP requested.
451eb7c06e8SYacine Belkadi  *	Pass -1 to request the next free DSP unit.
4521da177e4SLinus Torvalds  *
4531da177e4SLinus Torvalds  *	This function allocates both the audio and dsp device entries together
4541da177e4SLinus Torvalds  *	and will always allocate them as a matching pair - eg dsp3/audio3
455eb7c06e8SYacine Belkadi  *
456eb7c06e8SYacine Belkadi  *	Return: On success, the allocated number is returned. On failure,
457eb7c06e8SYacine Belkadi  *	a negative error code is returned.
4581da177e4SLinus Torvalds  */
4591da177e4SLinus Torvalds 
46099ac48f5SArjan van de Ven int register_sound_dsp(const struct file_operations *fops, int dev)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	return sound_insert_unit(&chains[3], fops, dev, 3, 131,
4636a73cf46SJoe Perches 				 "dsp", 0600, NULL);
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_dsp);
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /**
4691da177e4SLinus Torvalds  *	unregister_sound_special - unregister a special sound device
4701da177e4SLinus Torvalds  *	@unit: unit number to allocate
4711da177e4SLinus Torvalds  *
4721da177e4SLinus Torvalds  *	Release a sound device that was allocated with
4731da177e4SLinus Torvalds  *	register_sound_special(). The unit passed is the return value from
4741da177e4SLinus Torvalds  *	the register function.
4751da177e4SLinus Torvalds  */
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds void unregister_sound_special(int unit)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	sound_remove_unit(&chains[unit % SOUND_STEP], unit);
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_special);
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /**
4861da177e4SLinus Torvalds  *	unregister_sound_mixer - unregister a mixer
4871da177e4SLinus Torvalds  *	@unit: unit number to allocate
4881da177e4SLinus Torvalds  *
4891da177e4SLinus Torvalds  *	Release a sound device that was allocated with register_sound_mixer().
4901da177e4SLinus Torvalds  *	The unit passed is the return value from the register function.
4911da177e4SLinus Torvalds  */
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds void unregister_sound_mixer(int unit)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	sound_remove_unit(&chains[0], unit);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_mixer);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds /**
5011da177e4SLinus Torvalds  *	unregister_sound_dsp - unregister a DSP device
5021da177e4SLinus Torvalds  *	@unit: unit number to allocate
5031da177e4SLinus Torvalds  *
5041da177e4SLinus Torvalds  *	Release a sound device that was allocated with register_sound_dsp().
5051da177e4SLinus Torvalds  *	The unit passed is the return value from the register function.
5061da177e4SLinus Torvalds  *
5071da177e4SLinus Torvalds  *	Both of the allocated units are released together automatically.
5081da177e4SLinus Torvalds  */
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds void unregister_sound_dsp(int unit)
5111da177e4SLinus Torvalds {
512a39c4ad1SHannes Eder 	sound_remove_unit(&chains[3], unit);
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_dsp);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds static struct sound_unit *__look_for_unit(int chain, int unit)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	struct sound_unit *s;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	s=chains[chain];
5231da177e4SLinus Torvalds 	while(s && s->unit_minor <= unit)
5241da177e4SLinus Torvalds 	{
5251da177e4SLinus Torvalds 		if(s->unit_minor==unit)
5261da177e4SLinus Torvalds 			return s;
5271da177e4SLinus Torvalds 		s=s->next;
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 	return NULL;
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
532a39c4ad1SHannes Eder static int soundcore_open(struct inode *inode, struct file *file)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	int chain;
5351da177e4SLinus Torvalds 	int unit = iminor(inode);
5361da177e4SLinus Torvalds 	struct sound_unit *s;
53799ac48f5SArjan van de Ven 	const struct file_operations *new_fops = NULL;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	chain=unit&0x0F;
5401da177e4SLinus Torvalds 	if(chain==4 || chain==5)	/* dsp/audio/dsp16 */
5411da177e4SLinus Torvalds 	{
5421da177e4SLinus Torvalds 		unit&=0xF0;
5431da177e4SLinus Torvalds 		unit|=3;
5441da177e4SLinus Torvalds 		chain=3;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	spin_lock(&sound_loader_lock);
5481da177e4SLinus Torvalds 	s = __look_for_unit(chain, unit);
5491da177e4SLinus Torvalds 	if (s)
5501da177e4SLinus Torvalds 		new_fops = fops_get(s->unit_fops);
55193fe4483STejun Heo 	if (preclaim_oss && !new_fops) {
5521da177e4SLinus Torvalds 		spin_unlock(&sound_loader_lock);
5530a848680STejun Heo 
5541da177e4SLinus Torvalds 		/*
5551da177e4SLinus Torvalds 		 *  Please, don't change this order or code.
5561da177e4SLinus Torvalds 		 *  For ALSA slot means soundcard and OSS emulation code
5571da177e4SLinus Torvalds 		 *  comes as add-on modules which aren't depend on
5581da177e4SLinus Torvalds 		 *  ALSA toplevel modules for soundcards, thus we need
5591da177e4SLinus Torvalds 		 *  load them at first.	  [Jaroslav Kysela <perex@jcu.cz>]
5601da177e4SLinus Torvalds 		 */
5611da177e4SLinus Torvalds 		request_module("sound-slot-%i", unit>>4);
5621da177e4SLinus Torvalds 		request_module("sound-service-%i-%i", unit>>4, chain);
5630a848680STejun Heo 
5640a848680STejun Heo 		/*
5650a848680STejun Heo 		 * sound-slot/service-* module aliases are scheduled
5660a848680STejun Heo 		 * for removal in favor of the standard char-major-*
5670a848680STejun Heo 		 * module aliases.  For the time being, generate both
5680a848680STejun Heo 		 * the legacy and standard module aliases to ease
5690a848680STejun Heo 		 * transition.
5700a848680STejun Heo 		 */
5710a848680STejun Heo 		if (request_module("char-major-%d-%d", SOUND_MAJOR, unit) > 0)
5720a848680STejun Heo 			request_module("char-major-%d", SOUND_MAJOR);
5730a848680STejun Heo 
5741da177e4SLinus Torvalds 		spin_lock(&sound_loader_lock);
5751da177e4SLinus Torvalds 		s = __look_for_unit(chain, unit);
5761da177e4SLinus Torvalds 		if (s)
5771da177e4SLinus Torvalds 			new_fops = fops_get(s->unit_fops);
5781da177e4SLinus Torvalds 	}
579e84f9e57SAl Viro 	spin_unlock(&sound_loader_lock);
5801da177e4SLinus Torvalds 	if (new_fops) {
5811da177e4SLinus Torvalds 		/*
5821da177e4SLinus Torvalds 		 * We rely upon the fact that we can't be unloaded while the
583e84f9e57SAl Viro 		 * subdriver is there.
5841da177e4SLinus Torvalds 		 */
5851da177e4SLinus Torvalds 		int err = 0;
586e84f9e57SAl Viro 		replace_fops(file, new_fops);
587171d9f7dSJohn Kacur 
58890dc763fSArnd Bergmann 		if (file->f_op->open)
5891da177e4SLinus Torvalds 			err = file->f_op->open(inode,file);
590171d9f7dSJohn Kacur 
5911da177e4SLinus Torvalds 		return err;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 	return -ENODEV;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
5971da177e4SLinus Torvalds 
598de30d36bSKrzysztof Helt static void cleanup_oss_soundcore(void)
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds 	/* We have nothing to really do here - we know the lists must be
6011da177e4SLinus Torvalds 	   empty */
6021da177e4SLinus Torvalds 	unregister_chrdev(SOUND_MAJOR, "sound");
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
605d886e87cSTejun Heo static int __init init_oss_soundcore(void)
6061da177e4SLinus Torvalds {
60793fe4483STejun Heo 	if (preclaim_oss &&
608cb68429dSAlexey Khoroshilov 	    register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops) < 0) {
6091da177e4SLinus Torvalds 		printk(KERN_ERR "soundcore: sound device already in use.\n");
6101da177e4SLinus Torvalds 		return -EBUSY;
6111da177e4SLinus Torvalds 	}
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	return 0;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
616d886e87cSTejun Heo #endif /* CONFIG_SOUND_OSS_CORE */
617