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