xref: /openbmc/linux/sound/sound_core.c (revision cb68429d155507ec46f1922d2beb7bc637de4836)
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
1498f6e6041STao 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),
29502aa2a37SKees Cook 		      NULL, "%s", 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
355eb7c06e8SYacine Belkadi  *	subsystem.
356eb7c06e8SYacine Belkadi  *
357eb7c06e8SYacine Belkadi  *	Return: The allocated number is returned on success. On failure,
3581da177e4SLinus Torvalds  *	a negative error code is returned.
3591da177e4SLinus Torvalds  */
3601da177e4SLinus Torvalds 
36199ac48f5SArjan van de Ven int register_sound_special_device(const struct file_operations *fops, int unit,
362d568121cSTakashi Iwai 				  struct device *dev)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	const int chain = unit % SOUND_STEP;
3652abb8017STakashi Iwai 	int max_unit = 256;
3661da177e4SLinus Torvalds 	const char *name;
3671da177e4SLinus Torvalds 	char _name[16];
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	switch (chain) {
3701da177e4SLinus Torvalds 	    case 0:
3711da177e4SLinus Torvalds 		name = "mixer";
3721da177e4SLinus Torvalds 		break;
3731da177e4SLinus Torvalds 	    case 1:
3741da177e4SLinus Torvalds 		name = "sequencer";
3751da177e4SLinus Torvalds 		if (unit >= SOUND_STEP)
3761da177e4SLinus Torvalds 			goto __unknown;
3771da177e4SLinus Torvalds 		max_unit = unit + 1;
3781da177e4SLinus Torvalds 		break;
3791da177e4SLinus Torvalds 	    case 2:
3801da177e4SLinus Torvalds 		name = "midi";
3811da177e4SLinus Torvalds 		break;
3821da177e4SLinus Torvalds 	    case 3:
3831da177e4SLinus Torvalds 		name = "dsp";
3841da177e4SLinus Torvalds 		break;
3851da177e4SLinus Torvalds 	    case 4:
3861da177e4SLinus Torvalds 		name = "audio";
3871da177e4SLinus Torvalds 		break;
388848669daSTakashi Iwai 	    case 5:
389848669daSTakashi Iwai 		name = "dspW";
390848669daSTakashi Iwai 		break;
3911da177e4SLinus Torvalds 	    case 8:
3921da177e4SLinus Torvalds 		name = "sequencer2";
3931da177e4SLinus Torvalds 		if (unit >= SOUND_STEP)
3941da177e4SLinus Torvalds 			goto __unknown;
3951da177e4SLinus Torvalds 		max_unit = unit + 1;
3961da177e4SLinus Torvalds 		break;
3971da177e4SLinus Torvalds 	    case 9:
3981da177e4SLinus Torvalds 		name = "dmmidi";
3991da177e4SLinus Torvalds 		break;
4001da177e4SLinus Torvalds 	    case 10:
4011da177e4SLinus Torvalds 		name = "dmfm";
4021da177e4SLinus Torvalds 		break;
4031da177e4SLinus Torvalds 	    case 12:
4041da177e4SLinus Torvalds 		name = "adsp";
4051da177e4SLinus Torvalds 		break;
4061da177e4SLinus Torvalds 	    case 13:
4071da177e4SLinus Torvalds 		name = "amidi";
4081da177e4SLinus Torvalds 		break;
4091da177e4SLinus Torvalds 	    case 14:
4101da177e4SLinus Torvalds 		name = "admmidi";
4111da177e4SLinus Torvalds 		break;
4121da177e4SLinus Torvalds 	    default:
4131da177e4SLinus Torvalds 	    	{
4141da177e4SLinus Torvalds 		    __unknown:
4151da177e4SLinus Torvalds 			sprintf(_name, "unknown%d", chain);
4161da177e4SLinus Torvalds 		    	if (unit >= SOUND_STEP)
4171da177e4SLinus Torvalds 		    		strcat(_name, "-");
4181da177e4SLinus Torvalds 		    	name = _name;
4191da177e4SLinus Torvalds 		}
4201da177e4SLinus Torvalds 		break;
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 	return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
423d568121cSTakashi Iwai 				 name, S_IRUSR | S_IWUSR, dev);
424d568121cSTakashi Iwai }
425d568121cSTakashi Iwai 
426d568121cSTakashi Iwai EXPORT_SYMBOL(register_sound_special_device);
427d568121cSTakashi Iwai 
42899ac48f5SArjan van de Ven int register_sound_special(const struct file_operations *fops, int unit)
429d568121cSTakashi Iwai {
430d568121cSTakashi Iwai 	return register_sound_special_device(fops, unit, NULL);
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_special);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds /**
4361da177e4SLinus Torvalds  *	register_sound_mixer - register a mixer device
4371da177e4SLinus Torvalds  *	@fops: File operations for the driver
4381da177e4SLinus Torvalds  *	@dev: Unit number to allocate
4391da177e4SLinus Torvalds  *
4401da177e4SLinus Torvalds  *	Allocate a mixer device. Unit is the number of the mixer requested.
441eb7c06e8SYacine Belkadi  *	Pass -1 to request the next free mixer unit.
442eb7c06e8SYacine Belkadi  *
443eb7c06e8SYacine Belkadi  *	Return: On success, the allocated number is returned. On failure,
444eb7c06e8SYacine Belkadi  *	a negative error code is returned.
4451da177e4SLinus Torvalds  */
4461da177e4SLinus Torvalds 
44799ac48f5SArjan van de Ven int register_sound_mixer(const struct file_operations *fops, int dev)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	return sound_insert_unit(&chains[0], fops, dev, 0, 128,
450d568121cSTakashi Iwai 				 "mixer", S_IRUSR | S_IWUSR, NULL);
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_mixer);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /**
4561da177e4SLinus Torvalds  *	register_sound_midi - register a midi device
4571da177e4SLinus Torvalds  *	@fops: File operations for the driver
4581da177e4SLinus Torvalds  *	@dev: Unit number to allocate
4591da177e4SLinus Torvalds  *
4601da177e4SLinus Torvalds  *	Allocate a midi device. Unit is the number of the midi device requested.
461eb7c06e8SYacine Belkadi  *	Pass -1 to request the next free midi unit.
462eb7c06e8SYacine Belkadi  *
463eb7c06e8SYacine Belkadi  *	Return: On success, the allocated number is returned. On failure,
464eb7c06e8SYacine Belkadi  *	a negative error code is returned.
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds 
46799ac48f5SArjan van de Ven int register_sound_midi(const struct file_operations *fops, int dev)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	return sound_insert_unit(&chains[2], fops, dev, 2, 130,
470d568121cSTakashi Iwai 				 "midi", S_IRUSR | S_IWUSR, NULL);
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_midi);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds /*
4761da177e4SLinus Torvalds  *	DSP's are registered as a triple. Register only one and cheat
4771da177e4SLinus Torvalds  *	in open - see below.
4781da177e4SLinus Torvalds  */
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds /**
4811da177e4SLinus Torvalds  *	register_sound_dsp - register a DSP device
4821da177e4SLinus Torvalds  *	@fops: File operations for the driver
4831da177e4SLinus Torvalds  *	@dev: Unit number to allocate
4841da177e4SLinus Torvalds  *
4851da177e4SLinus Torvalds  *	Allocate a DSP device. Unit is the number of the DSP requested.
486eb7c06e8SYacine Belkadi  *	Pass -1 to request the next free DSP unit.
4871da177e4SLinus Torvalds  *
4881da177e4SLinus Torvalds  *	This function allocates both the audio and dsp device entries together
4891da177e4SLinus Torvalds  *	and will always allocate them as a matching pair - eg dsp3/audio3
490eb7c06e8SYacine Belkadi  *
491eb7c06e8SYacine Belkadi  *	Return: On success, the allocated number is returned. On failure,
492eb7c06e8SYacine Belkadi  *	a negative error code is returned.
4931da177e4SLinus Torvalds  */
4941da177e4SLinus Torvalds 
49599ac48f5SArjan van de Ven int register_sound_dsp(const struct file_operations *fops, int dev)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	return sound_insert_unit(&chains[3], fops, dev, 3, 131,
498d568121cSTakashi Iwai 				 "dsp", S_IWUSR | S_IRUSR, NULL);
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds EXPORT_SYMBOL(register_sound_dsp);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds /**
5041da177e4SLinus Torvalds  *	unregister_sound_special - unregister a special sound device
5051da177e4SLinus Torvalds  *	@unit: unit number to allocate
5061da177e4SLinus Torvalds  *
5071da177e4SLinus Torvalds  *	Release a sound device that was allocated with
5081da177e4SLinus Torvalds  *	register_sound_special(). The unit passed is the return value from
5091da177e4SLinus Torvalds  *	the register function.
5101da177e4SLinus Torvalds  */
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds void unregister_sound_special(int unit)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	sound_remove_unit(&chains[unit % SOUND_STEP], unit);
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_special);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds /**
5211da177e4SLinus Torvalds  *	unregister_sound_mixer - unregister a mixer
5221da177e4SLinus Torvalds  *	@unit: unit number to allocate
5231da177e4SLinus Torvalds  *
5241da177e4SLinus Torvalds  *	Release a sound device that was allocated with register_sound_mixer().
5251da177e4SLinus Torvalds  *	The unit passed is the return value from the register function.
5261da177e4SLinus Torvalds  */
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds void unregister_sound_mixer(int unit)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	sound_remove_unit(&chains[0], unit);
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_mixer);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds /**
5361da177e4SLinus Torvalds  *	unregister_sound_midi - unregister a midi device
5371da177e4SLinus Torvalds  *	@unit: unit number to allocate
5381da177e4SLinus Torvalds  *
5391da177e4SLinus Torvalds  *	Release a sound device that was allocated with register_sound_midi().
5401da177e4SLinus Torvalds  *	The unit passed is the return value from the register function.
5411da177e4SLinus Torvalds  */
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds void unregister_sound_midi(int unit)
5441da177e4SLinus Torvalds {
545a39c4ad1SHannes Eder 	sound_remove_unit(&chains[2], unit);
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_midi);
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /**
5511da177e4SLinus Torvalds  *	unregister_sound_dsp - unregister a DSP device
5521da177e4SLinus Torvalds  *	@unit: unit number to allocate
5531da177e4SLinus Torvalds  *
5541da177e4SLinus Torvalds  *	Release a sound device that was allocated with register_sound_dsp().
5551da177e4SLinus Torvalds  *	The unit passed is the return value from the register function.
5561da177e4SLinus Torvalds  *
5571da177e4SLinus Torvalds  *	Both of the allocated units are released together automatically.
5581da177e4SLinus Torvalds  */
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds void unregister_sound_dsp(int unit)
5611da177e4SLinus Torvalds {
562a39c4ad1SHannes Eder 	sound_remove_unit(&chains[3], unit);
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_sound_dsp);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds static struct sound_unit *__look_for_unit(int chain, int unit)
5691da177e4SLinus Torvalds {
5701da177e4SLinus Torvalds 	struct sound_unit *s;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	s=chains[chain];
5731da177e4SLinus Torvalds 	while(s && s->unit_minor <= unit)
5741da177e4SLinus Torvalds 	{
5751da177e4SLinus Torvalds 		if(s->unit_minor==unit)
5761da177e4SLinus Torvalds 			return s;
5771da177e4SLinus Torvalds 		s=s->next;
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 	return NULL;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
582a39c4ad1SHannes Eder static int soundcore_open(struct inode *inode, struct file *file)
5831da177e4SLinus Torvalds {
5841da177e4SLinus Torvalds 	int chain;
5851da177e4SLinus Torvalds 	int unit = iminor(inode);
5861da177e4SLinus Torvalds 	struct sound_unit *s;
58799ac48f5SArjan van de Ven 	const struct file_operations *new_fops = NULL;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	chain=unit&0x0F;
5901da177e4SLinus Torvalds 	if(chain==4 || chain==5)	/* dsp/audio/dsp16 */
5911da177e4SLinus Torvalds 	{
5921da177e4SLinus Torvalds 		unit&=0xF0;
5931da177e4SLinus Torvalds 		unit|=3;
5941da177e4SLinus Torvalds 		chain=3;
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	spin_lock(&sound_loader_lock);
5981da177e4SLinus Torvalds 	s = __look_for_unit(chain, unit);
5991da177e4SLinus Torvalds 	if (s)
6001da177e4SLinus Torvalds 		new_fops = fops_get(s->unit_fops);
60193fe4483STejun Heo 	if (preclaim_oss && !new_fops) {
6021da177e4SLinus Torvalds 		spin_unlock(&sound_loader_lock);
6030a848680STejun Heo 
6041da177e4SLinus Torvalds 		/*
6051da177e4SLinus Torvalds 		 *  Please, don't change this order or code.
6061da177e4SLinus Torvalds 		 *  For ALSA slot means soundcard and OSS emulation code
6071da177e4SLinus Torvalds 		 *  comes as add-on modules which aren't depend on
6081da177e4SLinus Torvalds 		 *  ALSA toplevel modules for soundcards, thus we need
6091da177e4SLinus Torvalds 		 *  load them at first.	  [Jaroslav Kysela <perex@jcu.cz>]
6101da177e4SLinus Torvalds 		 */
6111da177e4SLinus Torvalds 		request_module("sound-slot-%i", unit>>4);
6121da177e4SLinus Torvalds 		request_module("sound-service-%i-%i", unit>>4, chain);
6130a848680STejun Heo 
6140a848680STejun Heo 		/*
6150a848680STejun Heo 		 * sound-slot/service-* module aliases are scheduled
6160a848680STejun Heo 		 * for removal in favor of the standard char-major-*
6170a848680STejun Heo 		 * module aliases.  For the time being, generate both
6180a848680STejun Heo 		 * the legacy and standard module aliases to ease
6190a848680STejun Heo 		 * transition.
6200a848680STejun Heo 		 */
6210a848680STejun Heo 		if (request_module("char-major-%d-%d", SOUND_MAJOR, unit) > 0)
6220a848680STejun Heo 			request_module("char-major-%d", SOUND_MAJOR);
6230a848680STejun Heo 
6241da177e4SLinus Torvalds 		spin_lock(&sound_loader_lock);
6251da177e4SLinus Torvalds 		s = __look_for_unit(chain, unit);
6261da177e4SLinus Torvalds 		if (s)
6271da177e4SLinus Torvalds 			new_fops = fops_get(s->unit_fops);
6281da177e4SLinus Torvalds 	}
629e84f9e57SAl Viro 	spin_unlock(&sound_loader_lock);
6301da177e4SLinus Torvalds 	if (new_fops) {
6311da177e4SLinus Torvalds 		/*
6321da177e4SLinus Torvalds 		 * We rely upon the fact that we can't be unloaded while the
633e84f9e57SAl Viro 		 * subdriver is there.
6341da177e4SLinus Torvalds 		 */
6351da177e4SLinus Torvalds 		int err = 0;
636e84f9e57SAl Viro 		replace_fops(file, new_fops);
637171d9f7dSJohn Kacur 
63890dc763fSArnd Bergmann 		if (file->f_op->open)
6391da177e4SLinus Torvalds 			err = file->f_op->open(inode,file);
640171d9f7dSJohn Kacur 
6411da177e4SLinus Torvalds 		return err;
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 	return -ENODEV;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
6471da177e4SLinus Torvalds 
648de30d36bSKrzysztof Helt static void cleanup_oss_soundcore(void)
6491da177e4SLinus Torvalds {
6501da177e4SLinus Torvalds 	/* We have nothing to really do here - we know the lists must be
6511da177e4SLinus Torvalds 	   empty */
6521da177e4SLinus Torvalds 	unregister_chrdev(SOUND_MAJOR, "sound");
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
655d886e87cSTejun Heo static int __init init_oss_soundcore(void)
6561da177e4SLinus Torvalds {
65793fe4483STejun Heo 	if (preclaim_oss &&
658*cb68429dSAlexey Khoroshilov 	    register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops) < 0) {
6591da177e4SLinus Torvalds 		printk(KERN_ERR "soundcore: sound device already in use.\n");
6601da177e4SLinus Torvalds 		return -EBUSY;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	return 0;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
666d886e87cSTejun Heo #endif /* CONFIG_SOUND_OSS_CORE */
667