xref: /openbmc/linux/sound/core/init.c (revision 72620d60)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Initialization routines
3c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
81da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
91da177e4SLinus Torvalds  *   (at your option) any later version.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
121da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
131da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141da177e4SLinus Torvalds  *   GNU General Public License for more details.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
171da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
181da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <linux/init.h>
231da177e4SLinus Torvalds #include <linux/sched.h>
24da155d5bSPaul Gortmaker #include <linux/module.h>
2551990e82SPaul Gortmaker #include <linux/device.h>
261da177e4SLinus Torvalds #include <linux/file.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/time.h>
291da177e4SLinus Torvalds #include <linux/ctype.h>
301da177e4SLinus Torvalds #include <linux/pm.h>
31f2464064STakashi Iwai #include <linux/completion.h>
32d052d1beSRussell King 
331da177e4SLinus Torvalds #include <sound/core.h>
341da177e4SLinus Torvalds #include <sound/control.h>
351da177e4SLinus Torvalds #include <sound/info.h>
361da177e4SLinus Torvalds 
3782a783f4STakashi Iwai /* monitor files for graceful shutdown (hotplug) */
3882a783f4STakashi Iwai struct snd_monitor_file {
3982a783f4STakashi Iwai 	struct file *file;
4082a783f4STakashi Iwai 	const struct file_operations *disconnected_f_op;
4182a783f4STakashi Iwai 	struct list_head shutdown_list;	/* still need to shutdown */
4282a783f4STakashi Iwai 	struct list_head list;	/* link of monitor files */
4382a783f4STakashi Iwai };
4482a783f4STakashi Iwai 
45a9edfc60SKarsten Wiese static DEFINE_SPINLOCK(shutdown_lock);
46a9edfc60SKarsten Wiese static LIST_HEAD(shutdown_files);
47a9edfc60SKarsten Wiese 
489c2e08c5SArjan van de Ven static const struct file_operations snd_shutdown_f_ops;
491da177e4SLinus Torvalds 
507bb2491bSTakashi Iwai /* locked for registering/using */
517bb2491bSTakashi Iwai static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS);
526581f4e7STakashi Iwai struct snd_card *snd_cards[SNDRV_CARDS];
53c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_cards);
54c0d3fb39STakashi Iwai 
55746df948STakashi Iwai static DEFINE_MUTEX(snd_card_mutex);
561da177e4SLinus Torvalds 
57304cd07fSTakashi Iwai static char *slots[SNDRV_CARDS];
58304cd07fSTakashi Iwai module_param_array(slots, charp, NULL, 0444);
59304cd07fSTakashi Iwai MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
60304cd07fSTakashi Iwai 
61a93bbaa7STakashi Iwai /* return non-zero if the given index is reserved for the given
62304cd07fSTakashi Iwai  * module via slots option
63304cd07fSTakashi Iwai  */
64a93bbaa7STakashi Iwai static int module_slot_match(struct module *module, int idx)
65304cd07fSTakashi Iwai {
66a93bbaa7STakashi Iwai 	int match = 1;
67304cd07fSTakashi Iwai #ifdef MODULE
68a93bbaa7STakashi Iwai 	const char *s1, *s2;
69a93bbaa7STakashi Iwai 
7060f6fef8STakashi Iwai 	if (!module || !*module->name || !slots[idx])
71304cd07fSTakashi Iwai 		return 0;
72a93bbaa7STakashi Iwai 
73a93bbaa7STakashi Iwai 	s1 = module->name;
74a93bbaa7STakashi Iwai 	s2 = slots[idx];
75a93bbaa7STakashi Iwai 	if (*s2 == '!') {
76a93bbaa7STakashi Iwai 		match = 0; /* negative match */
77a93bbaa7STakashi Iwai 		s2++;
78a93bbaa7STakashi Iwai 	}
79304cd07fSTakashi Iwai 	/* compare module name strings
80304cd07fSTakashi Iwai 	 * hyphens are handled as equivalent with underscore
81304cd07fSTakashi Iwai 	 */
82304cd07fSTakashi Iwai 	for (;;) {
83304cd07fSTakashi Iwai 		char c1 = *s1++;
84304cd07fSTakashi Iwai 		char c2 = *s2++;
85304cd07fSTakashi Iwai 		if (c1 == '-')
86304cd07fSTakashi Iwai 			c1 = '_';
87304cd07fSTakashi Iwai 		if (c2 == '-')
88304cd07fSTakashi Iwai 			c2 = '_';
89304cd07fSTakashi Iwai 		if (c1 != c2)
90a93bbaa7STakashi Iwai 			return !match;
91304cd07fSTakashi Iwai 		if (!c1)
92304cd07fSTakashi Iwai 			break;
93304cd07fSTakashi Iwai 	}
94a93bbaa7STakashi Iwai #endif /* MODULE */
95a93bbaa7STakashi Iwai 	return match;
96304cd07fSTakashi Iwai }
97304cd07fSTakashi Iwai 
988eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
99512bbd6aSTakashi Iwai int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
100c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds 
103e28563ccSTakashi Iwai #ifdef CONFIG_PROC_FS
104512bbd6aSTakashi Iwai static void snd_card_id_read(struct snd_info_entry *entry,
105512bbd6aSTakashi Iwai 			     struct snd_info_buffer *buffer)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	snd_iprintf(buffer, "%s\n", entry->card->id);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
110e28563ccSTakashi Iwai static inline int init_info_for_card(struct snd_card *card)
111e28563ccSTakashi Iwai {
112e28563ccSTakashi Iwai 	int err;
113e28563ccSTakashi Iwai 	struct snd_info_entry *entry;
114e28563ccSTakashi Iwai 
115e28563ccSTakashi Iwai 	if ((err = snd_info_card_register(card)) < 0) {
116f2f9307aSTakashi Iwai 		dev_dbg(card->dev, "unable to create card info\n");
117e28563ccSTakashi Iwai 		return err;
118e28563ccSTakashi Iwai 	}
119e28563ccSTakashi Iwai 	if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
120f2f9307aSTakashi Iwai 		dev_dbg(card->dev, "unable to create card entry\n");
121e28563ccSTakashi Iwai 		return err;
122e28563ccSTakashi Iwai 	}
123e28563ccSTakashi Iwai 	entry->c.text.read = snd_card_id_read;
124e28563ccSTakashi Iwai 	if (snd_info_register(entry) < 0) {
125e28563ccSTakashi Iwai 		snd_info_free_entry(entry);
126e28563ccSTakashi Iwai 		entry = NULL;
127e28563ccSTakashi Iwai 	}
128e28563ccSTakashi Iwai 	card->proc_id = entry;
129e28563ccSTakashi Iwai 	return 0;
130e28563ccSTakashi Iwai }
131e28563ccSTakashi Iwai #else /* !CONFIG_PROC_FS */
132e28563ccSTakashi Iwai #define init_info_for_card(card)
133e28563ccSTakashi Iwai #endif
134e28563ccSTakashi Iwai 
135deb6596fSTakashi Iwai static int check_empty_slot(struct module *module, int slot)
136deb6596fSTakashi Iwai {
137deb6596fSTakashi Iwai 	return !slots[slot] || !*slots[slot];
138deb6596fSTakashi Iwai }
139deb6596fSTakashi Iwai 
140deb6596fSTakashi Iwai /* return an empty slot number (>= 0) found in the given bitmask @mask.
141deb6596fSTakashi Iwai  * @mask == -1 == 0xffffffff means: take any free slot up to 32
142deb6596fSTakashi Iwai  * when no slot is available, return the original @mask as is.
143deb6596fSTakashi Iwai  */
144deb6596fSTakashi Iwai static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int),
145deb6596fSTakashi Iwai 				 struct module *module)
146deb6596fSTakashi Iwai {
147deb6596fSTakashi Iwai 	int slot;
148deb6596fSTakashi Iwai 
149deb6596fSTakashi Iwai 	for (slot = 0; slot < SNDRV_CARDS; slot++) {
150deb6596fSTakashi Iwai 		if (slot < 32 && !(mask & (1U << slot)))
151deb6596fSTakashi Iwai 			continue;
152deb6596fSTakashi Iwai 		if (!test_bit(slot, snd_cards_lock)) {
153deb6596fSTakashi Iwai 			if (check(module, slot))
154deb6596fSTakashi Iwai 				return slot; /* found */
155deb6596fSTakashi Iwai 		}
156deb6596fSTakashi Iwai 	}
157deb6596fSTakashi Iwai 	return mask; /* unchanged */
158deb6596fSTakashi Iwai }
159deb6596fSTakashi Iwai 
1608bfb181cSTakashi Iwai static int snd_card_do_free(struct snd_card *card);
16134356dbdSTakashi Iwai static const struct attribute_group *card_dev_attr_groups[];
1628bfb181cSTakashi Iwai 
1638bfb181cSTakashi Iwai static void release_card_device(struct device *dev)
1648bfb181cSTakashi Iwai {
1658bfb181cSTakashi Iwai 	snd_card_do_free(dev_to_snd_card(dev));
1668bfb181cSTakashi Iwai }
1678bfb181cSTakashi Iwai 
1681da177e4SLinus Torvalds /**
169393aa9c1STakashi Iwai  *  snd_card_new - create and initialize a soundcard structure
170393aa9c1STakashi Iwai  *  @parent: the parent device object
1711da177e4SLinus Torvalds  *  @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
1721da177e4SLinus Torvalds  *  @xid: card identification (ASCII string)
1731da177e4SLinus Torvalds  *  @module: top level module for locking
1741da177e4SLinus Torvalds  *  @extra_size: allocate this extra size after the main soundcard structure
17553fb1e63STakashi Iwai  *  @card_ret: the pointer to store the created card instance
1761da177e4SLinus Torvalds  *
1771da177e4SLinus Torvalds  *  Creates and initializes a soundcard structure.
1781da177e4SLinus Torvalds  *
17953fb1e63STakashi Iwai  *  The function allocates snd_card instance via kzalloc with the given
18053fb1e63STakashi Iwai  *  space for the driver to use freely.  The allocated struct is stored
18153fb1e63STakashi Iwai  *  in the given card_ret pointer.
18253fb1e63STakashi Iwai  *
183eb7c06e8SYacine Belkadi  *  Return: Zero if successful or a negative error code.
1841da177e4SLinus Torvalds  */
185393aa9c1STakashi Iwai int snd_card_new(struct device *parent, int idx, const char *xid,
18653fb1e63STakashi Iwai 		    struct module *module, int extra_size,
18753fb1e63STakashi Iwai 		    struct snd_card **card_ret)
1881da177e4SLinus Torvalds {
189512bbd6aSTakashi Iwai 	struct snd_card *card;
190deb6596fSTakashi Iwai 	int err;
1911da177e4SLinus Torvalds 
19253fb1e63STakashi Iwai 	if (snd_BUG_ON(!card_ret))
19353fb1e63STakashi Iwai 		return -EINVAL;
19453fb1e63STakashi Iwai 	*card_ret = NULL;
19553fb1e63STakashi Iwai 
1961da177e4SLinus Torvalds 	if (extra_size < 0)
1971da177e4SLinus Torvalds 		extra_size = 0;
198ca2c0966STakashi Iwai 	card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
19953fb1e63STakashi Iwai 	if (!card)
20053fb1e63STakashi Iwai 		return -ENOMEM;
2018bfb181cSTakashi Iwai 	if (extra_size > 0)
2028bfb181cSTakashi Iwai 		card->private_data = (char *)card + sizeof(struct snd_card);
20310a8ebbbSJaroslav Kysela 	if (xid)
2045fdc18d9SJaroslav Kysela 		strlcpy(card->id, xid, sizeof(card->id));
2051da177e4SLinus Torvalds 	err = 0;
206746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
207deb6596fSTakashi Iwai 	if (idx < 0) /* first check the matching module-name slot */
208deb6596fSTakashi Iwai 		idx = get_slot_from_bitmask(idx, module_slot_match, module);
209deb6596fSTakashi Iwai 	if (idx < 0) /* if not matched, assign an empty slot */
210deb6596fSTakashi Iwai 		idx = get_slot_from_bitmask(idx, check_empty_slot, module);
211a93bbaa7STakashi Iwai 	if (idx < 0)
212a93bbaa7STakashi Iwai 		err = -ENODEV;
213a93bbaa7STakashi Iwai 	else if (idx < snd_ecards_limit) {
2147bb2491bSTakashi Iwai 		if (test_bit(idx, snd_cards_lock))
2155c33dd70SOliver Neukum 			err = -EBUSY;	/* invalid */
216a93bbaa7STakashi Iwai 	} else if (idx >= SNDRV_CARDS)
2171da177e4SLinus Torvalds 		err = -ENODEV;
218a93bbaa7STakashi Iwai 	if (err < 0) {
219746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
220f2f9307aSTakashi Iwai 		dev_err(parent, "cannot find the slot for index %d (range 0-%i), error: %d\n",
2215c33dd70SOliver Neukum 			 idx, snd_ecards_limit - 1, err);
2228bfb181cSTakashi Iwai 		kfree(card);
2238bfb181cSTakashi Iwai 		return err;
2241da177e4SLinus Torvalds 	}
2257bb2491bSTakashi Iwai 	set_bit(idx, snd_cards_lock);		/* lock it */
226a93bbaa7STakashi Iwai 	if (idx >= snd_ecards_limit)
227a93bbaa7STakashi Iwai 		snd_ecards_limit = idx + 1; /* increase the limit */
228746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
229393aa9c1STakashi Iwai 	card->dev = parent;
2301da177e4SLinus Torvalds 	card->number = idx;
2311da177e4SLinus Torvalds 	card->module = module;
2321da177e4SLinus Torvalds 	INIT_LIST_HEAD(&card->devices);
2331da177e4SLinus Torvalds 	init_rwsem(&card->controls_rwsem);
2341da177e4SLinus Torvalds 	rwlock_init(&card->ctl_files_rwlock);
2351da177e4SLinus Torvalds 	INIT_LIST_HEAD(&card->controls);
2361da177e4SLinus Torvalds 	INIT_LIST_HEAD(&card->ctl_files);
2371da177e4SLinus Torvalds 	spin_lock_init(&card->files_lock);
238118dd6bfSTakashi Iwai 	INIT_LIST_HEAD(&card->files_list);
2391da177e4SLinus Torvalds #ifdef CONFIG_PM
2401a60d4c5SIngo Molnar 	mutex_init(&card->power_lock);
2411da177e4SLinus Torvalds 	init_waitqueue_head(&card->power_sleep);
2421da177e4SLinus Torvalds #endif
2438bfb181cSTakashi Iwai 
2448bfb181cSTakashi Iwai 	device_initialize(&card->card_dev);
2458bfb181cSTakashi Iwai 	card->card_dev.parent = parent;
2468bfb181cSTakashi Iwai 	card->card_dev.class = sound_class;
2478bfb181cSTakashi Iwai 	card->card_dev.release = release_card_device;
24834356dbdSTakashi Iwai 	card->card_dev.groups = card_dev_attr_groups;
2498bfb181cSTakashi Iwai 	err = kobject_set_name(&card->card_dev.kobj, "card%d", idx);
2508bfb181cSTakashi Iwai 	if (err < 0)
2518bfb181cSTakashi Iwai 		goto __error;
2528bfb181cSTakashi Iwai 
2531da177e4SLinus Torvalds 	/* the control interface cannot be accessed from the user space until */
2541da177e4SLinus Torvalds 	/* snd_cards_bitmask and snd_cards are set with snd_card_register */
25553fb1e63STakashi Iwai 	err = snd_ctl_create(card);
25653fb1e63STakashi Iwai 	if (err < 0) {
257f2f9307aSTakashi Iwai 		dev_err(parent, "unable to register control minors\n");
2581da177e4SLinus Torvalds 		goto __error;
2591da177e4SLinus Torvalds 	}
26053fb1e63STakashi Iwai 	err = snd_info_card_create(card);
26153fb1e63STakashi Iwai 	if (err < 0) {
262f2f9307aSTakashi Iwai 		dev_err(parent, "unable to create card info\n");
2631da177e4SLinus Torvalds 		goto __error_ctl;
2641da177e4SLinus Torvalds 	}
26553fb1e63STakashi Iwai 	*card_ret = card;
26653fb1e63STakashi Iwai 	return 0;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds       __error_ctl:
269289ca025STakashi Iwai 	snd_device_free_all(card);
2701da177e4SLinus Torvalds       __error:
2718bfb181cSTakashi Iwai 	put_device(&card->card_dev);
27253fb1e63STakashi Iwai   	return err;
2731da177e4SLinus Torvalds }
274393aa9c1STakashi Iwai EXPORT_SYMBOL(snd_card_new);
275c0d3fb39STakashi Iwai 
276746df948STakashi Iwai /* return non-zero if a card is already locked */
277746df948STakashi Iwai int snd_card_locked(int card)
278746df948STakashi Iwai {
279746df948STakashi Iwai 	int locked;
280746df948STakashi Iwai 
281746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
2827bb2491bSTakashi Iwai 	locked = test_bit(card, snd_cards_lock);
283746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
284746df948STakashi Iwai 	return locked;
285746df948STakashi Iwai }
286746df948STakashi Iwai 
287b3b0abe1SClemens Ladisch static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
288b3b0abe1SClemens Ladisch {
289b3b0abe1SClemens Ladisch 	return -ENODEV;
290b3b0abe1SClemens Ladisch }
291b3b0abe1SClemens Ladisch 
292b3b0abe1SClemens Ladisch static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
293b3b0abe1SClemens Ladisch 				   size_t count, loff_t *offset)
294b3b0abe1SClemens Ladisch {
295b3b0abe1SClemens Ladisch 	return -ENODEV;
296b3b0abe1SClemens Ladisch }
297b3b0abe1SClemens Ladisch 
298b3b0abe1SClemens Ladisch static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
299b3b0abe1SClemens Ladisch 				    size_t count, loff_t *offset)
300b3b0abe1SClemens Ladisch {
301b3b0abe1SClemens Ladisch 	return -ENODEV;
302b3b0abe1SClemens Ladisch }
303b3b0abe1SClemens Ladisch 
304a9edfc60SKarsten Wiese static int snd_disconnect_release(struct inode *inode, struct file *file)
305a9edfc60SKarsten Wiese {
306a9edfc60SKarsten Wiese 	struct snd_monitor_file *df = NULL, *_df;
307a9edfc60SKarsten Wiese 
308a9edfc60SKarsten Wiese 	spin_lock(&shutdown_lock);
309a9edfc60SKarsten Wiese 	list_for_each_entry(_df, &shutdown_files, shutdown_list) {
310a9edfc60SKarsten Wiese 		if (_df->file == file) {
311a9edfc60SKarsten Wiese 			df = _df;
312118dd6bfSTakashi Iwai 			list_del_init(&df->shutdown_list);
313a9edfc60SKarsten Wiese 			break;
314a9edfc60SKarsten Wiese 		}
315a9edfc60SKarsten Wiese 	}
316a9edfc60SKarsten Wiese 	spin_unlock(&shutdown_lock);
317a9edfc60SKarsten Wiese 
318233e70f4SAl Viro 	if (likely(df)) {
319233e70f4SAl Viro 		if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync)
320233e70f4SAl Viro 			df->disconnected_f_op->fasync(-1, file, 0);
321a9edfc60SKarsten Wiese 		return df->disconnected_f_op->release(inode, file);
322233e70f4SAl Viro 	}
323a9edfc60SKarsten Wiese 
3249bf8e7ddSHarvey Harrison 	panic("%s(%p, %p) failed!", __func__, inode, file);
325a9edfc60SKarsten Wiese }
326a9edfc60SKarsten Wiese 
3271da177e4SLinus Torvalds static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	return POLLERR | POLLNVAL;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
332b3b0abe1SClemens Ladisch static long snd_disconnect_ioctl(struct file *file,
333b3b0abe1SClemens Ladisch 				 unsigned int cmd, unsigned long arg)
334b3b0abe1SClemens Ladisch {
335b3b0abe1SClemens Ladisch 	return -ENODEV;
336b3b0abe1SClemens Ladisch }
337b3b0abe1SClemens Ladisch 
338b3b0abe1SClemens Ladisch static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
339b3b0abe1SClemens Ladisch {
340b3b0abe1SClemens Ladisch 	return -ENODEV;
341b3b0abe1SClemens Ladisch }
342b3b0abe1SClemens Ladisch 
343b3b0abe1SClemens Ladisch static int snd_disconnect_fasync(int fd, struct file *file, int on)
344b3b0abe1SClemens Ladisch {
345b3b0abe1SClemens Ladisch 	return -ENODEV;
346b3b0abe1SClemens Ladisch }
347b3b0abe1SClemens Ladisch 
3489c2e08c5SArjan van de Ven static const struct file_operations snd_shutdown_f_ops =
349a9edfc60SKarsten Wiese {
350a9edfc60SKarsten Wiese 	.owner = 	THIS_MODULE,
351a9edfc60SKarsten Wiese 	.llseek =	snd_disconnect_llseek,
352a9edfc60SKarsten Wiese 	.read = 	snd_disconnect_read,
353a9edfc60SKarsten Wiese 	.write =	snd_disconnect_write,
354a9edfc60SKarsten Wiese 	.release =	snd_disconnect_release,
355a9edfc60SKarsten Wiese 	.poll =		snd_disconnect_poll,
356a9edfc60SKarsten Wiese 	.unlocked_ioctl = snd_disconnect_ioctl,
357a9edfc60SKarsten Wiese #ifdef CONFIG_COMPAT
358a9edfc60SKarsten Wiese 	.compat_ioctl = snd_disconnect_ioctl,
359a9edfc60SKarsten Wiese #endif
360a9edfc60SKarsten Wiese 	.mmap =		snd_disconnect_mmap,
361a9edfc60SKarsten Wiese 	.fasync =	snd_disconnect_fasync
362a9edfc60SKarsten Wiese };
363a9edfc60SKarsten Wiese 
3641da177e4SLinus Torvalds /**
3651da177e4SLinus Torvalds  *  snd_card_disconnect - disconnect all APIs from the file-operations (user space)
3661da177e4SLinus Torvalds  *  @card: soundcard structure
3671da177e4SLinus Torvalds  *
3681da177e4SLinus Torvalds  *  Disconnects all APIs from the file-operations (user space).
3691da177e4SLinus Torvalds  *
370eb7c06e8SYacine Belkadi  *  Return: Zero, otherwise a negative error code.
3711da177e4SLinus Torvalds  *
3721da177e4SLinus Torvalds  *  Note: The current implementation replaces all active file->f_op with special
3731da177e4SLinus Torvalds  *        dummy file operations (they do nothing except release).
3741da177e4SLinus Torvalds  */
375512bbd6aSTakashi Iwai int snd_card_disconnect(struct snd_card *card)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	struct snd_monitor_file *mfile;
3781da177e4SLinus Torvalds 	int err;
3791da177e4SLinus Torvalds 
380f18638dcSTakashi Iwai 	if (!card)
381f18638dcSTakashi Iwai 		return -EINVAL;
382f18638dcSTakashi Iwai 
3831da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
3841da177e4SLinus Torvalds 	if (card->shutdown) {
3851da177e4SLinus Torvalds 		spin_unlock(&card->files_lock);
3861da177e4SLinus Torvalds 		return 0;
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 	card->shutdown = 1;
3891da177e4SLinus Torvalds 	spin_unlock(&card->files_lock);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	/* phase 1: disable fops (user space) operations for ALSA API */
392746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
3931da177e4SLinus Torvalds 	snd_cards[card->number] = NULL;
3947bb2491bSTakashi Iwai 	clear_bit(card->number, snd_cards_lock);
395746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	/* phase 2: replace file->f_op with special dummy operations */
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
400118dd6bfSTakashi Iwai 	list_for_each_entry(mfile, &card->files_list, list) {
4011da177e4SLinus Torvalds 		/* it's critical part, use endless loop */
4021da177e4SLinus Torvalds 		/* we have no room to fail */
403a9edfc60SKarsten Wiese 		mfile->disconnected_f_op = mfile->file->f_op;
4041da177e4SLinus Torvalds 
405a9edfc60SKarsten Wiese 		spin_lock(&shutdown_lock);
406a9edfc60SKarsten Wiese 		list_add(&mfile->shutdown_list, &shutdown_files);
407a9edfc60SKarsten Wiese 		spin_unlock(&shutdown_lock);
4081da177e4SLinus Torvalds 
409a9edfc60SKarsten Wiese 		mfile->file->f_op = &snd_shutdown_f_ops;
410bc9abce0SMiguel Boton 		fops_get(mfile->file->f_op);
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 	spin_unlock(&card->files_lock);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	/* phase 3: notify all connected devices about disconnection */
4151da177e4SLinus Torvalds 	/* at this point, they cannot respond to any calls except release() */
4161da177e4SLinus Torvalds 
4178eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
4181da177e4SLinus Torvalds 	if (snd_mixer_oss_notify_callback)
4191da177e4SLinus Torvalds 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
4201da177e4SLinus Torvalds #endif
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	/* notify all devices that we are disconnected */
4231da177e4SLinus Torvalds 	err = snd_device_disconnect_all(card);
4241da177e4SLinus Torvalds 	if (err < 0)
425f2f9307aSTakashi Iwai 		dev_err(card->dev, "not all devices for card %i can be disconnected\n", card->number);
4261da177e4SLinus Torvalds 
427746d4a02STakashi Iwai 	snd_info_card_disconnect(card);
4288bfb181cSTakashi Iwai 	if (card->registered) {
4298bfb181cSTakashi Iwai 		device_del(&card->card_dev);
4308bfb181cSTakashi Iwai 		card->registered = false;
43173d38b13STakashi Iwai 	}
432f18638dcSTakashi Iwai #ifdef CONFIG_PM
433f18638dcSTakashi Iwai 	wake_up(&card->power_sleep);
434f18638dcSTakashi Iwai #endif
4351da177e4SLinus Torvalds 	return 0;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
438c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_disconnect);
439c0d3fb39STakashi Iwai 
4401da177e4SLinus Torvalds /**
4411da177e4SLinus Torvalds  *  snd_card_free - frees given soundcard structure
4421da177e4SLinus Torvalds  *  @card: soundcard structure
4431da177e4SLinus Torvalds  *
4441da177e4SLinus Torvalds  *  This function releases the soundcard structure and the all assigned
4451da177e4SLinus Torvalds  *  devices automatically.  That is, you don't have to release the devices
4461da177e4SLinus Torvalds  *  by yourself.
4471da177e4SLinus Torvalds  *
448eb7c06e8SYacine Belkadi  *  Return: Zero. Frees all associated devices and frees the control
4491da177e4SLinus Torvalds  *  interface associated to given soundcard.
4501da177e4SLinus Torvalds  */
451c461482cSTakashi Iwai static int snd_card_do_free(struct snd_card *card)
4521da177e4SLinus Torvalds {
4538eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
4541da177e4SLinus Torvalds 	if (snd_mixer_oss_notify_callback)
4551da177e4SLinus Torvalds 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
4561da177e4SLinus Torvalds #endif
45772620d60STakashi Iwai 	snd_device_free_all(card);
4581da177e4SLinus Torvalds 	if (card->private_free)
4591da177e4SLinus Torvalds 		card->private_free(card);
460746d4a02STakashi Iwai 	snd_info_free_entry(card->proc_id);
4611da177e4SLinus Torvalds 	if (snd_info_card_free(card) < 0) {
462f2f9307aSTakashi Iwai 		dev_warn(card->dev, "unable to free card info\n");
4631da177e4SLinus Torvalds 		/* Not fatal error */
4641da177e4SLinus Torvalds 	}
465f2464064STakashi Iwai 	if (card->release_completion)
466f2464064STakashi Iwai 		complete(card->release_completion);
467c461482cSTakashi Iwai 	kfree(card);
468c461482cSTakashi Iwai 	return 0;
469c461482cSTakashi Iwai }
470c461482cSTakashi Iwai 
471c461482cSTakashi Iwai int snd_card_free_when_closed(struct snd_card *card)
472c461482cSTakashi Iwai {
473f18638dcSTakashi Iwai 	int ret = snd_card_disconnect(card);
474c461482cSTakashi Iwai 	if (ret)
475c461482cSTakashi Iwai 		return ret;
4768bfb181cSTakashi Iwai 	put_device(&card->card_dev);
4771da177e4SLinus Torvalds 	return 0;
4781da177e4SLinus Torvalds }
479f2464064STakashi Iwai EXPORT_SYMBOL(snd_card_free_when_closed);
4801da177e4SLinus Torvalds 
481f2464064STakashi Iwai int snd_card_free(struct snd_card *card)
482f2464064STakashi Iwai {
483f2464064STakashi Iwai 	struct completion released;
484f2464064STakashi Iwai 	int ret;
485f2464064STakashi Iwai 
486f2464064STakashi Iwai 	init_completion(&released);
487f2464064STakashi Iwai 	card->release_completion = &released;
488f2464064STakashi Iwai 	ret = snd_card_free_when_closed(card);
489f2464064STakashi Iwai 	if (ret)
490f2464064STakashi Iwai 		return ret;
491f2464064STakashi Iwai 	/* wait, until all devices are ready for the free operation */
492f2464064STakashi Iwai 	wait_for_completion(&released);
493f2464064STakashi Iwai 	return 0;
494f2464064STakashi Iwai }
495c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_free);
496c0d3fb39STakashi Iwai 
497e7df2a3aSTakashi Iwai /* retrieve the last word of shortname or longname */
498e7df2a3aSTakashi Iwai static const char *retrieve_id_from_card_name(const char *name)
4991da177e4SLinus Torvalds {
500e7df2a3aSTakashi Iwai 	const char *spos = name;
501e7df2a3aSTakashi Iwai 
502e7df2a3aSTakashi Iwai 	while (*name) {
503e7df2a3aSTakashi Iwai 		if (isspace(*name) && isalnum(name[1]))
504e7df2a3aSTakashi Iwai 			spos = name + 1;
505e7df2a3aSTakashi Iwai 		name++;
506e7df2a3aSTakashi Iwai 	}
507e7df2a3aSTakashi Iwai 	return spos;
508e7df2a3aSTakashi Iwai }
509e7df2a3aSTakashi Iwai 
510e7df2a3aSTakashi Iwai /* return true if the given id string doesn't conflict any other card ids */
511e7df2a3aSTakashi Iwai static bool card_id_ok(struct snd_card *card, const char *id)
512e7df2a3aSTakashi Iwai {
513e7df2a3aSTakashi Iwai 	int i;
514e7df2a3aSTakashi Iwai 	if (!snd_info_check_reserved_words(id))
515e7df2a3aSTakashi Iwai 		return false;
516e7df2a3aSTakashi Iwai 	for (i = 0; i < snd_ecards_limit; i++) {
517e7df2a3aSTakashi Iwai 		if (snd_cards[i] && snd_cards[i] != card &&
518e7df2a3aSTakashi Iwai 		    !strcmp(snd_cards[i]->id, id))
519e7df2a3aSTakashi Iwai 			return false;
520e7df2a3aSTakashi Iwai 	}
521e7df2a3aSTakashi Iwai 	return true;
522e7df2a3aSTakashi Iwai }
523e7df2a3aSTakashi Iwai 
524e7df2a3aSTakashi Iwai /* copy to card->id only with valid letters from nid */
525e7df2a3aSTakashi Iwai static void copy_valid_id_string(struct snd_card *card, const char *src,
526e7df2a3aSTakashi Iwai 				 const char *nid)
527e7df2a3aSTakashi Iwai {
528e7df2a3aSTakashi Iwai 	char *id = card->id;
529e7df2a3aSTakashi Iwai 
530e7df2a3aSTakashi Iwai 	while (*nid && !isalnum(*nid))
531e7df2a3aSTakashi Iwai 		nid++;
532e7df2a3aSTakashi Iwai 	if (isdigit(*nid))
533e7df2a3aSTakashi Iwai 		*id++ = isalpha(*src) ? *src : 'D';
534e7df2a3aSTakashi Iwai 	while (*nid && (size_t)(id - card->id) < sizeof(card->id) - 1) {
535e7df2a3aSTakashi Iwai 		if (isalnum(*nid))
536e7df2a3aSTakashi Iwai 			*id++ = *nid;
537e7df2a3aSTakashi Iwai 		nid++;
538e7df2a3aSTakashi Iwai 	}
539e7df2a3aSTakashi Iwai 	*id = 0;
540e7df2a3aSTakashi Iwai }
541e7df2a3aSTakashi Iwai 
542e7df2a3aSTakashi Iwai /* Set card->id from the given string
543e7df2a3aSTakashi Iwai  * If the string conflicts with other ids, add a suffix to make it unique.
544e7df2a3aSTakashi Iwai  */
545e7df2a3aSTakashi Iwai static void snd_card_set_id_no_lock(struct snd_card *card, const char *src,
546e7df2a3aSTakashi Iwai 				    const char *nid)
547e7df2a3aSTakashi Iwai {
548e7df2a3aSTakashi Iwai 	int len, loops;
549e7df2a3aSTakashi Iwai 	bool is_default = false;
55010a8ebbbSJaroslav Kysela 	char *id;
5511da177e4SLinus Torvalds 
552e7df2a3aSTakashi Iwai 	copy_valid_id_string(card, src, nid);
5531da177e4SLinus Torvalds 	id = card->id;
5541da177e4SLinus Torvalds 
555e7df2a3aSTakashi Iwai  again:
556e7df2a3aSTakashi Iwai 	/* use "Default" for obviously invalid strings
557e7df2a3aSTakashi Iwai 	 * ("card" conflicts with proc directories)
558e7df2a3aSTakashi Iwai 	 */
559e7df2a3aSTakashi Iwai 	if (!*id || !strncmp(id, "card", 4)) {
560d8e4f9aeSTakashi Iwai 		strcpy(id, "Default");
561e7df2a3aSTakashi Iwai 		is_default = true;
5621da177e4SLinus Torvalds 	}
5631da177e4SLinus Torvalds 
5648edbb198STakashi Iwai 	len = strlen(id);
565e7df2a3aSTakashi Iwai 	for (loops = 0; loops < SNDRV_CARDS; loops++) {
5668edbb198STakashi Iwai 		char *spos;
5678edbb198STakashi Iwai 		char sfxstr[5]; /* "_012" */
5688edbb198STakashi Iwai 		int sfxlen;
5698edbb198STakashi Iwai 
570e7df2a3aSTakashi Iwai 		if (card_id_ok(card, id))
571e7df2a3aSTakashi Iwai 			return; /* OK */
572e7df2a3aSTakashi Iwai 
5738edbb198STakashi Iwai 		/* Add _XYZ suffix */
5748edbb198STakashi Iwai 		sprintf(sfxstr, "_%X", loops + 1);
5758edbb198STakashi Iwai 		sfxlen = strlen(sfxstr);
5768edbb198STakashi Iwai 		if (len + sfxlen >= sizeof(card->id))
5778edbb198STakashi Iwai 			spos = id + sizeof(card->id) - sfxlen - 1;
578d001544dSClemens Ladisch 		else
5798edbb198STakashi Iwai 			spos = id + len;
5808edbb198STakashi Iwai 		strcpy(spos, sfxstr);
5811da177e4SLinus Torvalds 	}
582e7df2a3aSTakashi Iwai 	/* fallback to the default id */
583e7df2a3aSTakashi Iwai 	if (!is_default) {
584e7df2a3aSTakashi Iwai 		*id = 0;
585e7df2a3aSTakashi Iwai 		goto again;
586e7df2a3aSTakashi Iwai 	}
587e7df2a3aSTakashi Iwai 	/* last resort... */
588f2f9307aSTakashi Iwai 	dev_err(card->dev, "unable to set card id (%s)\n", id);
589e7df2a3aSTakashi Iwai 	if (card->proc_root->name)
59097f44f56STakashi Iwai 		strlcpy(card->id, card->proc_root->name, sizeof(card->id));
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
593872c7820SMark Brown /**
594872c7820SMark Brown  *  snd_card_set_id - set card identification name
595872c7820SMark Brown  *  @card: soundcard structure
596872c7820SMark Brown  *  @nid: new identification string
597872c7820SMark Brown  *
598872c7820SMark Brown  *  This function sets the card identification and checks for name
599872c7820SMark Brown  *  collisions.
600872c7820SMark Brown  */
601872c7820SMark Brown void snd_card_set_id(struct snd_card *card, const char *nid)
602872c7820SMark Brown {
6035fdc18d9SJaroslav Kysela 	/* check if user specified own card->id */
6045fdc18d9SJaroslav Kysela 	if (card->id[0] != '\0')
6055fdc18d9SJaroslav Kysela 		return;
6065fdc18d9SJaroslav Kysela 	mutex_lock(&snd_card_mutex);
607e7df2a3aSTakashi Iwai 	snd_card_set_id_no_lock(card, nid, nid);
6085fdc18d9SJaroslav Kysela 	mutex_unlock(&snd_card_mutex);
609872c7820SMark Brown }
61010a8ebbbSJaroslav Kysela EXPORT_SYMBOL(snd_card_set_id);
61110a8ebbbSJaroslav Kysela 
6129fb6198eSJaroslav Kysela static ssize_t
6139fb6198eSJaroslav Kysela card_id_show_attr(struct device *dev,
6149fb6198eSJaroslav Kysela 		  struct device_attribute *attr, char *buf)
6159fb6198eSJaroslav Kysela {
616b203dbabSTakashi Iwai 	struct snd_card *card = container_of(dev, struct snd_card, card_dev);
617b203dbabSTakashi Iwai 	return snprintf(buf, PAGE_SIZE, "%s\n", card->id);
6189fb6198eSJaroslav Kysela }
6199fb6198eSJaroslav Kysela 
6209fb6198eSJaroslav Kysela static ssize_t
6219fb6198eSJaroslav Kysela card_id_store_attr(struct device *dev, struct device_attribute *attr,
6229fb6198eSJaroslav Kysela 		   const char *buf, size_t count)
6239fb6198eSJaroslav Kysela {
624b203dbabSTakashi Iwai 	struct snd_card *card = container_of(dev, struct snd_card, card_dev);
6259fb6198eSJaroslav Kysela 	char buf1[sizeof(card->id)];
6269fb6198eSJaroslav Kysela 	size_t copy = count > sizeof(card->id) - 1 ?
6279fb6198eSJaroslav Kysela 					sizeof(card->id) - 1 : count;
6289fb6198eSJaroslav Kysela 	size_t idx;
6299fb6198eSJaroslav Kysela 	int c;
6309fb6198eSJaroslav Kysela 
6319fb6198eSJaroslav Kysela 	for (idx = 0; idx < copy; idx++) {
6329fb6198eSJaroslav Kysela 		c = buf[idx];
6339fb6198eSJaroslav Kysela 		if (!isalnum(c) && c != '_' && c != '-')
6349fb6198eSJaroslav Kysela 			return -EINVAL;
6359fb6198eSJaroslav Kysela 	}
6369fb6198eSJaroslav Kysela 	memcpy(buf1, buf, copy);
6379fb6198eSJaroslav Kysela 	buf1[copy] = '\0';
6389fb6198eSJaroslav Kysela 	mutex_lock(&snd_card_mutex);
639e7df2a3aSTakashi Iwai 	if (!card_id_ok(NULL, buf1)) {
6409fb6198eSJaroslav Kysela 		mutex_unlock(&snd_card_mutex);
6419fb6198eSJaroslav Kysela 		return -EEXIST;
6429fb6198eSJaroslav Kysela 	}
6439fb6198eSJaroslav Kysela 	strcpy(card->id, buf1);
644c2eb9c4eSJaroslav Kysela 	snd_info_card_id_change(card);
6459fb6198eSJaroslav Kysela 	mutex_unlock(&snd_card_mutex);
6469fb6198eSJaroslav Kysela 
6479fb6198eSJaroslav Kysela 	return count;
6489fb6198eSJaroslav Kysela }
6499fb6198eSJaroslav Kysela 
65034356dbdSTakashi Iwai static DEVICE_ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr);
6519fb6198eSJaroslav Kysela 
6529fb6198eSJaroslav Kysela static ssize_t
6539fb6198eSJaroslav Kysela card_number_show_attr(struct device *dev,
6549fb6198eSJaroslav Kysela 		     struct device_attribute *attr, char *buf)
6559fb6198eSJaroslav Kysela {
656b203dbabSTakashi Iwai 	struct snd_card *card = container_of(dev, struct snd_card, card_dev);
657b203dbabSTakashi Iwai 	return snprintf(buf, PAGE_SIZE, "%i\n", card->number);
6589fb6198eSJaroslav Kysela }
6599fb6198eSJaroslav Kysela 
66034356dbdSTakashi Iwai static DEVICE_ATTR(number, S_IRUGO, card_number_show_attr, NULL);
66134356dbdSTakashi Iwai 
66234356dbdSTakashi Iwai static struct attribute *card_dev_attrs[] = {
66334356dbdSTakashi Iwai 	&dev_attr_id.attr,
66434356dbdSTakashi Iwai 	&dev_attr_number.attr,
66534356dbdSTakashi Iwai 	NULL
66634356dbdSTakashi Iwai };
66734356dbdSTakashi Iwai 
66834356dbdSTakashi Iwai static struct attribute_group card_dev_attr_group = {
66934356dbdSTakashi Iwai 	.attrs	= card_dev_attrs,
67034356dbdSTakashi Iwai };
67134356dbdSTakashi Iwai 
67234356dbdSTakashi Iwai static const struct attribute_group *card_dev_attr_groups[] = {
67334356dbdSTakashi Iwai 	&card_dev_attr_group,
67434356dbdSTakashi Iwai 	NULL
67534356dbdSTakashi Iwai };
6769fb6198eSJaroslav Kysela 
6771da177e4SLinus Torvalds /**
6781da177e4SLinus Torvalds  *  snd_card_register - register the soundcard
6791da177e4SLinus Torvalds  *  @card: soundcard structure
6801da177e4SLinus Torvalds  *
6811da177e4SLinus Torvalds  *  This function registers all the devices assigned to the soundcard.
6821da177e4SLinus Torvalds  *  Until calling this, the ALSA control interface is blocked from the
6831da177e4SLinus Torvalds  *  external accesses.  Thus, you should call this function at the end
6841da177e4SLinus Torvalds  *  of the initialization of the card.
6851da177e4SLinus Torvalds  *
686eb7c06e8SYacine Belkadi  *  Return: Zero otherwise a negative error code if the registration failed.
6871da177e4SLinus Torvalds  */
688512bbd6aSTakashi Iwai int snd_card_register(struct snd_card *card)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 	int err;
6911da177e4SLinus Torvalds 
6927eaa943cSTakashi Iwai 	if (snd_BUG_ON(!card))
6937eaa943cSTakashi Iwai 		return -EINVAL;
69439aba963SKay Sievers 
6958bfb181cSTakashi Iwai 	if (!card->registered) {
6968bfb181cSTakashi Iwai 		err = device_add(&card->card_dev);
6978bfb181cSTakashi Iwai 		if (err < 0)
6988bfb181cSTakashi Iwai 			return err;
6998bfb181cSTakashi Iwai 		card->registered = true;
700d80f19faSGreg Kroah-Hartman 	}
70139aba963SKay Sievers 
7021da177e4SLinus Torvalds 	if ((err = snd_device_register_all(card)) < 0)
7031da177e4SLinus Torvalds 		return err;
704746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
7051da177e4SLinus Torvalds 	if (snd_cards[card->number]) {
7061da177e4SLinus Torvalds 		/* already registered */
707746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
7081da177e4SLinus Torvalds 		return 0;
7091da177e4SLinus Torvalds 	}
710e7df2a3aSTakashi Iwai 	if (*card->id) {
711e7df2a3aSTakashi Iwai 		/* make a unique id name from the given string */
712e7df2a3aSTakashi Iwai 		char tmpid[sizeof(card->id)];
713e7df2a3aSTakashi Iwai 		memcpy(tmpid, card->id, sizeof(card->id));
714e7df2a3aSTakashi Iwai 		snd_card_set_id_no_lock(card, tmpid, tmpid);
715e7df2a3aSTakashi Iwai 	} else {
716e7df2a3aSTakashi Iwai 		/* create an id from either shortname or longname */
717e7df2a3aSTakashi Iwai 		const char *src;
718e7df2a3aSTakashi Iwai 		src = *card->shortname ? card->shortname : card->longname;
719e7df2a3aSTakashi Iwai 		snd_card_set_id_no_lock(card, src,
720e7df2a3aSTakashi Iwai 					retrieve_id_from_card_name(src));
721e7df2a3aSTakashi Iwai 	}
7221da177e4SLinus Torvalds 	snd_cards[card->number] = card;
723746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
724e28563ccSTakashi Iwai 	init_info_for_card(card);
7258eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
7261da177e4SLinus Torvalds 	if (snd_mixer_oss_notify_callback)
7271da177e4SLinus Torvalds 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
7281da177e4SLinus Torvalds #endif
7291da177e4SLinus Torvalds 	return 0;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
732c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_register);
733c0d3fb39STakashi Iwai 
734e28563ccSTakashi Iwai #ifdef CONFIG_PROC_FS
7356581f4e7STakashi Iwai static struct snd_info_entry *snd_card_info_entry;
7361da177e4SLinus Torvalds 
737a381a7a6STakashi Iwai static void snd_card_info_read(struct snd_info_entry *entry,
738a381a7a6STakashi Iwai 			       struct snd_info_buffer *buffer)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	int idx, count;
741512bbd6aSTakashi Iwai 	struct snd_card *card;
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
744746df948STakashi Iwai 		mutex_lock(&snd_card_mutex);
7451da177e4SLinus Torvalds 		if ((card = snd_cards[idx]) != NULL) {
7461da177e4SLinus Torvalds 			count++;
747d001544dSClemens Ladisch 			snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
7481da177e4SLinus Torvalds 					idx,
7491da177e4SLinus Torvalds 					card->id,
7501da177e4SLinus Torvalds 					card->driver,
7511da177e4SLinus Torvalds 					card->shortname);
7521da177e4SLinus Torvalds 			snd_iprintf(buffer, "                      %s\n",
7531da177e4SLinus Torvalds 					card->longname);
7541da177e4SLinus Torvalds 		}
755746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 	if (!count)
7581da177e4SLinus Torvalds 		snd_iprintf(buffer, "--- no soundcards ---\n");
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
761e28563ccSTakashi Iwai #ifdef CONFIG_SND_OSSEMUL
7621da177e4SLinus Torvalds 
763512bbd6aSTakashi Iwai void snd_card_info_read_oss(struct snd_info_buffer *buffer)
7641da177e4SLinus Torvalds {
7651da177e4SLinus Torvalds 	int idx, count;
766512bbd6aSTakashi Iwai 	struct snd_card *card;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
769746df948STakashi Iwai 		mutex_lock(&snd_card_mutex);
7701da177e4SLinus Torvalds 		if ((card = snd_cards[idx]) != NULL) {
7711da177e4SLinus Torvalds 			count++;
7721da177e4SLinus Torvalds 			snd_iprintf(buffer, "%s\n", card->longname);
7731da177e4SLinus Torvalds 		}
774746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds 	if (!count) {
7771da177e4SLinus Torvalds 		snd_iprintf(buffer, "--- no soundcards ---\n");
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds #endif
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds #ifdef MODULE
784512bbd6aSTakashi Iwai static struct snd_info_entry *snd_card_module_info_entry;
785512bbd6aSTakashi Iwai static void snd_card_module_info_read(struct snd_info_entry *entry,
786512bbd6aSTakashi Iwai 				      struct snd_info_buffer *buffer)
7871da177e4SLinus Torvalds {
7881da177e4SLinus Torvalds 	int idx;
789512bbd6aSTakashi Iwai 	struct snd_card *card;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	for (idx = 0; idx < SNDRV_CARDS; idx++) {
792746df948STakashi Iwai 		mutex_lock(&snd_card_mutex);
7931da177e4SLinus Torvalds 		if ((card = snd_cards[idx]) != NULL)
794d001544dSClemens Ladisch 			snd_iprintf(buffer, "%2i %s\n",
795d001544dSClemens Ladisch 				    idx, card->module->name);
796746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds #endif
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds int __init snd_card_info_init(void)
8021da177e4SLinus Torvalds {
803512bbd6aSTakashi Iwai 	struct snd_info_entry *entry;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
8067c22f1aaSTakashi Iwai 	if (! entry)
8077c22f1aaSTakashi Iwai 		return -ENOMEM;
8081da177e4SLinus Torvalds 	entry->c.text.read = snd_card_info_read;
8091da177e4SLinus Torvalds 	if (snd_info_register(entry) < 0) {
8101da177e4SLinus Torvalds 		snd_info_free_entry(entry);
8111da177e4SLinus Torvalds 		return -ENOMEM;
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 	snd_card_info_entry = entry;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds #ifdef MODULE
8161da177e4SLinus Torvalds 	entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
8171da177e4SLinus Torvalds 	if (entry) {
8181da177e4SLinus Torvalds 		entry->c.text.read = snd_card_module_info_read;
8191da177e4SLinus Torvalds 		if (snd_info_register(entry) < 0)
8201da177e4SLinus Torvalds 			snd_info_free_entry(entry);
8211da177e4SLinus Torvalds 		else
8221da177e4SLinus Torvalds 			snd_card_module_info_entry = entry;
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds #endif
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	return 0;
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds int __exit snd_card_info_done(void)
8301da177e4SLinus Torvalds {
831746d4a02STakashi Iwai 	snd_info_free_entry(snd_card_info_entry);
8321da177e4SLinus Torvalds #ifdef MODULE
833746d4a02STakashi Iwai 	snd_info_free_entry(snd_card_module_info_entry);
8341da177e4SLinus Torvalds #endif
8351da177e4SLinus Torvalds 	return 0;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
838e28563ccSTakashi Iwai #endif /* CONFIG_PROC_FS */
839e28563ccSTakashi Iwai 
8401da177e4SLinus Torvalds /**
8411da177e4SLinus Torvalds  *  snd_component_add - add a component string
8421da177e4SLinus Torvalds  *  @card: soundcard structure
8431da177e4SLinus Torvalds  *  @component: the component id string
8441da177e4SLinus Torvalds  *
8451da177e4SLinus Torvalds  *  This function adds the component id string to the supported list.
8461da177e4SLinus Torvalds  *  The component can be referred from the alsa-lib.
8471da177e4SLinus Torvalds  *
848eb7c06e8SYacine Belkadi  *  Return: Zero otherwise a negative error code.
8491da177e4SLinus Torvalds  */
8501da177e4SLinus Torvalds 
851512bbd6aSTakashi Iwai int snd_component_add(struct snd_card *card, const char *component)
8521da177e4SLinus Torvalds {
8531da177e4SLinus Torvalds 	char *ptr;
8541da177e4SLinus Torvalds 	int len = strlen(component);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	ptr = strstr(card->components, component);
8571da177e4SLinus Torvalds 	if (ptr != NULL) {
8581da177e4SLinus Torvalds 		if (ptr[len] == '\0' || ptr[len] == ' ')	/* already there */
8591da177e4SLinus Torvalds 			return 1;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 	if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
8621da177e4SLinus Torvalds 		snd_BUG();
8631da177e4SLinus Torvalds 		return -ENOMEM;
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 	if (card->components[0] != '\0')
8661da177e4SLinus Torvalds 		strcat(card->components, " ");
8671da177e4SLinus Torvalds 	strcat(card->components, component);
8681da177e4SLinus Torvalds 	return 0;
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
871c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_component_add);
872c0d3fb39STakashi Iwai 
8731da177e4SLinus Torvalds /**
8741da177e4SLinus Torvalds  *  snd_card_file_add - add the file to the file list of the card
8751da177e4SLinus Torvalds  *  @card: soundcard structure
8761da177e4SLinus Torvalds  *  @file: file pointer
8771da177e4SLinus Torvalds  *
8781da177e4SLinus Torvalds  *  This function adds the file to the file linked-list of the card.
8791da177e4SLinus Torvalds  *  This linked-list is used to keep tracking the connection state,
8801da177e4SLinus Torvalds  *  and to avoid the release of busy resources by hotplug.
8811da177e4SLinus Torvalds  *
882eb7c06e8SYacine Belkadi  *  Return: zero or a negative error code.
8831da177e4SLinus Torvalds  */
884512bbd6aSTakashi Iwai int snd_card_file_add(struct snd_card *card, struct file *file)
8851da177e4SLinus Torvalds {
8861da177e4SLinus Torvalds 	struct snd_monitor_file *mfile;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
8891da177e4SLinus Torvalds 	if (mfile == NULL)
8901da177e4SLinus Torvalds 		return -ENOMEM;
8911da177e4SLinus Torvalds 	mfile->file = file;
892a9edfc60SKarsten Wiese 	mfile->disconnected_f_op = NULL;
893a45e3d6bSTakashi Iwai 	INIT_LIST_HEAD(&mfile->shutdown_list);
8941da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
8951da177e4SLinus Torvalds 	if (card->shutdown) {
8961da177e4SLinus Torvalds 		spin_unlock(&card->files_lock);
8971da177e4SLinus Torvalds 		kfree(mfile);
8981da177e4SLinus Torvalds 		return -ENODEV;
8991da177e4SLinus Torvalds 	}
900118dd6bfSTakashi Iwai 	list_add(&mfile->list, &card->files_list);
901f2464064STakashi Iwai 	get_device(&card->card_dev);
9021da177e4SLinus Torvalds 	spin_unlock(&card->files_lock);
9031da177e4SLinus Torvalds 	return 0;
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
906c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_file_add);
907c0d3fb39STakashi Iwai 
9081da177e4SLinus Torvalds /**
9091da177e4SLinus Torvalds  *  snd_card_file_remove - remove the file from the file list
9101da177e4SLinus Torvalds  *  @card: soundcard structure
9111da177e4SLinus Torvalds  *  @file: file pointer
9121da177e4SLinus Torvalds  *
9131da177e4SLinus Torvalds  *  This function removes the file formerly added to the card via
9141da177e4SLinus Torvalds  *  snd_card_file_add() function.
9152b29b13cSTakashi Iwai  *  If all files are removed and snd_card_free_when_closed() was
9162b29b13cSTakashi Iwai  *  called beforehand, it processes the pending release of
9172b29b13cSTakashi Iwai  *  resources.
9181da177e4SLinus Torvalds  *
919eb7c06e8SYacine Belkadi  *  Return: Zero or a negative error code.
9201da177e4SLinus Torvalds  */
921512bbd6aSTakashi Iwai int snd_card_file_remove(struct snd_card *card, struct file *file)
9221da177e4SLinus Torvalds {
923118dd6bfSTakashi Iwai 	struct snd_monitor_file *mfile, *found = NULL;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
926118dd6bfSTakashi Iwai 	list_for_each_entry(mfile, &card->files_list, list) {
9271da177e4SLinus Torvalds 		if (mfile->file == file) {
928118dd6bfSTakashi Iwai 			list_del(&mfile->list);
929a45e3d6bSTakashi Iwai 			spin_lock(&shutdown_lock);
930a45e3d6bSTakashi Iwai 			list_del(&mfile->shutdown_list);
931a45e3d6bSTakashi Iwai 			spin_unlock(&shutdown_lock);
932118dd6bfSTakashi Iwai 			if (mfile->disconnected_f_op)
933118dd6bfSTakashi Iwai 				fops_put(mfile->disconnected_f_op);
934118dd6bfSTakashi Iwai 			found = mfile;
9351da177e4SLinus Torvalds 			break;
9361da177e4SLinus Torvalds 		}
9371da177e4SLinus Torvalds 	}
938c461482cSTakashi Iwai 	spin_unlock(&card->files_lock);
939118dd6bfSTakashi Iwai 	if (!found) {
940f2f9307aSTakashi Iwai 		dev_err(card->dev, "card file remove problem (%p)\n", file);
9411da177e4SLinus Torvalds 		return -ENOENT;
9421da177e4SLinus Torvalds 	}
943118dd6bfSTakashi Iwai 	kfree(found);
944f2464064STakashi Iwai 	put_device(&card->card_dev);
9451da177e4SLinus Torvalds 	return 0;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
948c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_file_remove);
949c0d3fb39STakashi Iwai 
9501da177e4SLinus Torvalds #ifdef CONFIG_PM
9511da177e4SLinus Torvalds /**
9521da177e4SLinus Torvalds  *  snd_power_wait - wait until the power-state is changed.
9531da177e4SLinus Torvalds  *  @card: soundcard structure
9541da177e4SLinus Torvalds  *  @power_state: expected power state
9551da177e4SLinus Torvalds  *
9561da177e4SLinus Torvalds  *  Waits until the power-state is changed.
9571da177e4SLinus Torvalds  *
958eb7c06e8SYacine Belkadi  *  Return: Zero if successful, or a negative error code.
959eb7c06e8SYacine Belkadi  *
9601da177e4SLinus Torvalds  *  Note: the power lock must be active before call.
9611da177e4SLinus Torvalds  */
962cbac4b0cSTakashi Iwai int snd_power_wait(struct snd_card *card, unsigned int power_state)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	wait_queue_t wait;
9651da177e4SLinus Torvalds 	int result = 0;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	/* fastpath */
9681da177e4SLinus Torvalds 	if (snd_power_get_state(card) == power_state)
9691da177e4SLinus Torvalds 		return 0;
9701da177e4SLinus Torvalds 	init_waitqueue_entry(&wait, current);
9711da177e4SLinus Torvalds 	add_wait_queue(&card->power_sleep, &wait);
9721da177e4SLinus Torvalds 	while (1) {
9731da177e4SLinus Torvalds 		if (card->shutdown) {
9741da177e4SLinus Torvalds 			result = -ENODEV;
9751da177e4SLinus Torvalds 			break;
9761da177e4SLinus Torvalds 		}
9771da177e4SLinus Torvalds 		if (snd_power_get_state(card) == power_state)
9781da177e4SLinus Torvalds 			break;
9791da177e4SLinus Torvalds 		set_current_state(TASK_UNINTERRUPTIBLE);
9801da177e4SLinus Torvalds 		snd_power_unlock(card);
9811da177e4SLinus Torvalds 		schedule_timeout(30 * HZ);
9821da177e4SLinus Torvalds 		snd_power_lock(card);
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds 	remove_wait_queue(&card->power_sleep, &wait);
9851da177e4SLinus Torvalds 	return result;
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds 
988c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_power_wait);
9891da177e4SLinus Torvalds #endif /* CONFIG_PM */
990