xref: /openbmc/linux/sound/core/init.c (revision cb9c2bd4)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Initialization routines
4c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/init.h>
81da177e4SLinus Torvalds #include <linux/sched.h>
9da155d5bSPaul Gortmaker #include <linux/module.h>
1051990e82SPaul Gortmaker #include <linux/device.h>
111da177e4SLinus Torvalds #include <linux/file.h>
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/time.h>
141da177e4SLinus Torvalds #include <linux/ctype.h>
151da177e4SLinus Torvalds #include <linux/pm.h>
162d670ea2SHui Wang #include <linux/debugfs.h>
17f2464064STakashi Iwai #include <linux/completion.h>
1829bb274eSTakashi Iwai #include <linux/interrupt.h>
19d052d1beSRussell King 
201da177e4SLinus Torvalds #include <sound/core.h>
211da177e4SLinus Torvalds #include <sound/control.h>
221da177e4SLinus Torvalds #include <sound/info.h>
231da177e4SLinus Torvalds 
2482a783f4STakashi Iwai /* monitor files for graceful shutdown (hotplug) */
2582a783f4STakashi Iwai struct snd_monitor_file {
2682a783f4STakashi Iwai 	struct file *file;
2782a783f4STakashi Iwai 	const struct file_operations *disconnected_f_op;
2882a783f4STakashi Iwai 	struct list_head shutdown_list;	/* still need to shutdown */
2982a783f4STakashi Iwai 	struct list_head list;	/* link of monitor files */
3082a783f4STakashi Iwai };
3182a783f4STakashi Iwai 
32a9edfc60SKarsten Wiese static DEFINE_SPINLOCK(shutdown_lock);
33a9edfc60SKarsten Wiese static LIST_HEAD(shutdown_files);
34a9edfc60SKarsten Wiese 
359c2e08c5SArjan van de Ven static const struct file_operations snd_shutdown_f_ops;
361da177e4SLinus Torvalds 
377bb2491bSTakashi Iwai /* locked for registering/using */
387bb2491bSTakashi Iwai static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS);
39f4fa9689STakashi Iwai static struct snd_card *snd_cards[SNDRV_CARDS];
40c0d3fb39STakashi Iwai 
41746df948STakashi Iwai static DEFINE_MUTEX(snd_card_mutex);
421da177e4SLinus Torvalds 
43304cd07fSTakashi Iwai static char *slots[SNDRV_CARDS];
44304cd07fSTakashi Iwai module_param_array(slots, charp, NULL, 0444);
45304cd07fSTakashi Iwai MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
46304cd07fSTakashi Iwai 
47a93bbaa7STakashi Iwai /* return non-zero if the given index is reserved for the given
48304cd07fSTakashi Iwai  * module via slots option
49304cd07fSTakashi Iwai  */
module_slot_match(struct module * module,int idx)50a93bbaa7STakashi Iwai static int module_slot_match(struct module *module, int idx)
51304cd07fSTakashi Iwai {
52a93bbaa7STakashi Iwai 	int match = 1;
53304cd07fSTakashi Iwai #ifdef MODULE
54a93bbaa7STakashi Iwai 	const char *s1, *s2;
55a93bbaa7STakashi Iwai 
5660f6fef8STakashi Iwai 	if (!module || !*module->name || !slots[idx])
57304cd07fSTakashi Iwai 		return 0;
58a93bbaa7STakashi Iwai 
59a93bbaa7STakashi Iwai 	s1 = module->name;
60a93bbaa7STakashi Iwai 	s2 = slots[idx];
61a93bbaa7STakashi Iwai 	if (*s2 == '!') {
62a93bbaa7STakashi Iwai 		match = 0; /* negative match */
63a93bbaa7STakashi Iwai 		s2++;
64a93bbaa7STakashi Iwai 	}
65304cd07fSTakashi Iwai 	/* compare module name strings
66304cd07fSTakashi Iwai 	 * hyphens are handled as equivalent with underscore
67304cd07fSTakashi Iwai 	 */
68304cd07fSTakashi Iwai 	for (;;) {
69304cd07fSTakashi Iwai 		char c1 = *s1++;
70304cd07fSTakashi Iwai 		char c2 = *s2++;
71304cd07fSTakashi Iwai 		if (c1 == '-')
72304cd07fSTakashi Iwai 			c1 = '_';
73304cd07fSTakashi Iwai 		if (c2 == '-')
74304cd07fSTakashi Iwai 			c2 = '_';
75304cd07fSTakashi Iwai 		if (c1 != c2)
76a93bbaa7STakashi Iwai 			return !match;
77304cd07fSTakashi Iwai 		if (!c1)
78304cd07fSTakashi Iwai 			break;
79304cd07fSTakashi Iwai 	}
80a93bbaa7STakashi Iwai #endif /* MODULE */
81a93bbaa7STakashi Iwai 	return match;
82304cd07fSTakashi Iwai }
83304cd07fSTakashi Iwai 
848eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
85512bbd6aSTakashi Iwai int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
86c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
871da177e4SLinus Torvalds #endif
881da177e4SLinus Torvalds 
check_empty_slot(struct module * module,int slot)89deb6596fSTakashi Iwai static int check_empty_slot(struct module *module, int slot)
90deb6596fSTakashi Iwai {
91deb6596fSTakashi Iwai 	return !slots[slot] || !*slots[slot];
92deb6596fSTakashi Iwai }
93deb6596fSTakashi Iwai 
94deb6596fSTakashi Iwai /* return an empty slot number (>= 0) found in the given bitmask @mask.
95deb6596fSTakashi Iwai  * @mask == -1 == 0xffffffff means: take any free slot up to 32
96deb6596fSTakashi Iwai  * when no slot is available, return the original @mask as is.
97deb6596fSTakashi Iwai  */
get_slot_from_bitmask(int mask,int (* check)(struct module *,int),struct module * module)98deb6596fSTakashi Iwai static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int),
99deb6596fSTakashi Iwai 				 struct module *module)
100deb6596fSTakashi Iwai {
101deb6596fSTakashi Iwai 	int slot;
102deb6596fSTakashi Iwai 
103deb6596fSTakashi Iwai 	for (slot = 0; slot < SNDRV_CARDS; slot++) {
104deb6596fSTakashi Iwai 		if (slot < 32 && !(mask & (1U << slot)))
105deb6596fSTakashi Iwai 			continue;
106deb6596fSTakashi Iwai 		if (!test_bit(slot, snd_cards_lock)) {
107deb6596fSTakashi Iwai 			if (check(module, slot))
108deb6596fSTakashi Iwai 				return slot; /* found */
109deb6596fSTakashi Iwai 		}
110deb6596fSTakashi Iwai 	}
111deb6596fSTakashi Iwai 	return mask; /* unchanged */
112deb6596fSTakashi Iwai }
113deb6596fSTakashi Iwai 
1147f018db1STakashi Iwai /* the default release callback set in snd_device_alloc() */
default_release_alloc(struct device * dev)1157f018db1STakashi Iwai static void default_release_alloc(struct device *dev)
1167f018db1STakashi Iwai {
1177f018db1STakashi Iwai 	kfree(dev);
1187f018db1STakashi Iwai }
1197f018db1STakashi Iwai 
1207f018db1STakashi Iwai /**
1217f018db1STakashi Iwai  * snd_device_alloc - Allocate and initialize struct device for sound devices
1227f018db1STakashi Iwai  * @dev_p: pointer to store the allocated device
1237f018db1STakashi Iwai  * @card: card to assign, optional
1247f018db1STakashi Iwai  *
1257f018db1STakashi Iwai  * For releasing the allocated device, call put_device().
1267f018db1STakashi Iwai  */
snd_device_alloc(struct device ** dev_p,struct snd_card * card)1277f018db1STakashi Iwai int snd_device_alloc(struct device **dev_p, struct snd_card *card)
1287f018db1STakashi Iwai {
1297f018db1STakashi Iwai 	struct device *dev;
1307f018db1STakashi Iwai 
1317f018db1STakashi Iwai 	*dev_p = NULL;
1327f018db1STakashi Iwai 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1337f018db1STakashi Iwai 	if (!dev)
1347f018db1STakashi Iwai 		return -ENOMEM;
1357f018db1STakashi Iwai 	device_initialize(dev);
1367f018db1STakashi Iwai 	if (card)
1377f018db1STakashi Iwai 		dev->parent = &card->card_dev;
1387f018db1STakashi Iwai 	dev->class = &sound_class;
1397f018db1STakashi Iwai 	dev->release = default_release_alloc;
1407f018db1STakashi Iwai 	*dev_p = dev;
1417f018db1STakashi Iwai 	return 0;
1427f018db1STakashi Iwai }
1437f018db1STakashi Iwai EXPORT_SYMBOL_GPL(snd_device_alloc);
1447f018db1STakashi Iwai 
145e8ad415bSTakashi Iwai static int snd_card_init(struct snd_card *card, struct device *parent,
146e8ad415bSTakashi Iwai 			 int idx, const char *xid, struct module *module,
147e8ad415bSTakashi Iwai 			 size_t extra_size);
1488bfb181cSTakashi Iwai static int snd_card_do_free(struct snd_card *card);
1496bbc7fedSTakashi Iwai static const struct attribute_group card_dev_attr_group;
1508bfb181cSTakashi Iwai 
release_card_device(struct device * dev)1518bfb181cSTakashi Iwai static void release_card_device(struct device *dev)
1528bfb181cSTakashi Iwai {
1538bfb181cSTakashi Iwai 	snd_card_do_free(dev_to_snd_card(dev));
1548bfb181cSTakashi Iwai }
1558bfb181cSTakashi Iwai 
1561da177e4SLinus Torvalds /**
157393aa9c1STakashi Iwai  *  snd_card_new - create and initialize a soundcard structure
158393aa9c1STakashi Iwai  *  @parent: the parent device object
1591da177e4SLinus Torvalds  *  @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
1601da177e4SLinus Torvalds  *  @xid: card identification (ASCII string)
1611da177e4SLinus Torvalds  *  @module: top level module for locking
1621da177e4SLinus Torvalds  *  @extra_size: allocate this extra size after the main soundcard structure
16353fb1e63STakashi Iwai  *  @card_ret: the pointer to store the created card instance
1641da177e4SLinus Torvalds  *
16553fb1e63STakashi Iwai  *  The function allocates snd_card instance via kzalloc with the given
16653fb1e63STakashi Iwai  *  space for the driver to use freely.  The allocated struct is stored
16753fb1e63STakashi Iwai  *  in the given card_ret pointer.
16853fb1e63STakashi Iwai  *
169eb7c06e8SYacine Belkadi  *  Return: Zero if successful or a negative error code.
1701da177e4SLinus Torvalds  */
snd_card_new(struct device * parent,int idx,const char * xid,struct module * module,int extra_size,struct snd_card ** card_ret)171393aa9c1STakashi Iwai int snd_card_new(struct device *parent, int idx, const char *xid,
17253fb1e63STakashi Iwai 		    struct module *module, int extra_size,
17353fb1e63STakashi Iwai 		    struct snd_card **card_ret)
1741da177e4SLinus Torvalds {
175512bbd6aSTakashi Iwai 	struct snd_card *card;
176deb6596fSTakashi Iwai 	int err;
1771da177e4SLinus Torvalds 
17853fb1e63STakashi Iwai 	if (snd_BUG_ON(!card_ret))
17953fb1e63STakashi Iwai 		return -EINVAL;
18053fb1e63STakashi Iwai 	*card_ret = NULL;
18153fb1e63STakashi Iwai 
1821da177e4SLinus Torvalds 	if (extra_size < 0)
1831da177e4SLinus Torvalds 		extra_size = 0;
184ca2c0966STakashi Iwai 	card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
18553fb1e63STakashi Iwai 	if (!card)
18653fb1e63STakashi Iwai 		return -ENOMEM;
187e8ad415bSTakashi Iwai 
188e8ad415bSTakashi Iwai 	err = snd_card_init(card, parent, idx, xid, module, extra_size);
189c3afa2a4STakashi Iwai 	if (err < 0)
190c3afa2a4STakashi Iwai 		return err; /* card is freed by error handler */
191e8ad415bSTakashi Iwai 
192e8ad415bSTakashi Iwai 	*card_ret = card;
193e8ad415bSTakashi Iwai 	return 0;
194e8ad415bSTakashi Iwai }
195e8ad415bSTakashi Iwai EXPORT_SYMBOL(snd_card_new);
196e8ad415bSTakashi Iwai 
__snd_card_release(struct device * dev,void * data)197e8ad415bSTakashi Iwai static void __snd_card_release(struct device *dev, void *data)
198e8ad415bSTakashi Iwai {
199e8ad415bSTakashi Iwai 	snd_card_free(data);
200e8ad415bSTakashi Iwai }
201e8ad415bSTakashi Iwai 
202e8ad415bSTakashi Iwai /**
203e8ad415bSTakashi Iwai  * snd_devm_card_new - managed snd_card object creation
204e8ad415bSTakashi Iwai  * @parent: the parent device object
205e8ad415bSTakashi Iwai  * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
206e8ad415bSTakashi Iwai  * @xid: card identification (ASCII string)
207e8ad415bSTakashi Iwai  * @module: top level module for locking
208e8ad415bSTakashi Iwai  * @extra_size: allocate this extra size after the main soundcard structure
209e8ad415bSTakashi Iwai  * @card_ret: the pointer to store the created card instance
210e8ad415bSTakashi Iwai  *
211e8ad415bSTakashi Iwai  * This function works like snd_card_new() but manages the allocated resource
212e8ad415bSTakashi Iwai  * via devres, i.e. you don't need to free explicitly.
213e8ad415bSTakashi Iwai  *
214e8ad415bSTakashi Iwai  * When a snd_card object is created with this function and registered via
215e8ad415bSTakashi Iwai  * snd_card_register(), the very first devres action to call snd_card_free()
216e8ad415bSTakashi Iwai  * is added automatically.  In that way, the resource disconnection is assured
217e8ad415bSTakashi Iwai  * at first, then released in the expected order.
218fee2b871STakashi Iwai  *
219fee2b871STakashi Iwai  * If an error happens at the probe before snd_card_register() is called and
220fee2b871STakashi Iwai  * there have been other devres resources, you'd need to free the card manually
221fee2b871STakashi Iwai  * via snd_card_free() call in the error; otherwise it may lead to UAF due to
222fee2b871STakashi Iwai  * devres call orders.  You can use snd_card_free_on_error() helper for
223fee2b871STakashi Iwai  * handling it more easily.
224281dee67STakashi Iwai  *
225281dee67STakashi Iwai  * Return: zero if successful, or a negative error code
226e8ad415bSTakashi Iwai  */
snd_devm_card_new(struct device * parent,int idx,const char * xid,struct module * module,size_t extra_size,struct snd_card ** card_ret)227e8ad415bSTakashi Iwai int snd_devm_card_new(struct device *parent, int idx, const char *xid,
228e8ad415bSTakashi Iwai 		      struct module *module, size_t extra_size,
229e8ad415bSTakashi Iwai 		      struct snd_card **card_ret)
230e8ad415bSTakashi Iwai {
231e8ad415bSTakashi Iwai 	struct snd_card *card;
232e8ad415bSTakashi Iwai 	int err;
233e8ad415bSTakashi Iwai 
234e8ad415bSTakashi Iwai 	*card_ret = NULL;
235e8ad415bSTakashi Iwai 	card = devres_alloc(__snd_card_release, sizeof(*card) + extra_size,
236e8ad415bSTakashi Iwai 			    GFP_KERNEL);
237e8ad415bSTakashi Iwai 	if (!card)
238e8ad415bSTakashi Iwai 		return -ENOMEM;
239e8ad415bSTakashi Iwai 	card->managed = true;
240e8ad415bSTakashi Iwai 	err = snd_card_init(card, parent, idx, xid, module, extra_size);
241e8ad415bSTakashi Iwai 	if (err < 0) {
242c3afa2a4STakashi Iwai 		devres_free(card); /* in managed mode, we need to free manually */
243e8ad415bSTakashi Iwai 		return err;
244e8ad415bSTakashi Iwai 	}
245e8ad415bSTakashi Iwai 
246e8ad415bSTakashi Iwai 	devres_add(parent, card);
247e8ad415bSTakashi Iwai 	*card_ret = card;
248e8ad415bSTakashi Iwai 	return 0;
249e8ad415bSTakashi Iwai }
250e8ad415bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_devm_card_new);
251e8ad415bSTakashi Iwai 
252fee2b871STakashi Iwai /**
253fee2b871STakashi Iwai  * snd_card_free_on_error - a small helper for handling devm probe errors
254fee2b871STakashi Iwai  * @dev: the managed device object
255fee2b871STakashi Iwai  * @ret: the return code from the probe callback
256fee2b871STakashi Iwai  *
257fee2b871STakashi Iwai  * This function handles the explicit snd_card_free() call at the error from
258fee2b871STakashi Iwai  * the probe callback.  It's just a small helper for simplifying the error
259fee2b871STakashi Iwai  * handling with the managed devices.
260281dee67STakashi Iwai  *
261281dee67STakashi Iwai  * Return: zero if successful, or a negative error code
262fee2b871STakashi Iwai  */
snd_card_free_on_error(struct device * dev,int ret)263fee2b871STakashi Iwai int snd_card_free_on_error(struct device *dev, int ret)
264fee2b871STakashi Iwai {
265fee2b871STakashi Iwai 	struct snd_card *card;
266fee2b871STakashi Iwai 
267fee2b871STakashi Iwai 	if (!ret)
268fee2b871STakashi Iwai 		return 0;
269fee2b871STakashi Iwai 	card = devres_find(dev, __snd_card_release, NULL, NULL);
270fee2b871STakashi Iwai 	if (card)
271fee2b871STakashi Iwai 		snd_card_free(card);
272fee2b871STakashi Iwai 	return ret;
273fee2b871STakashi Iwai }
274fee2b871STakashi Iwai EXPORT_SYMBOL_GPL(snd_card_free_on_error);
275fee2b871STakashi Iwai 
snd_card_init(struct snd_card * card,struct device * parent,int idx,const char * xid,struct module * module,size_t extra_size)276e8ad415bSTakashi Iwai static int snd_card_init(struct snd_card *card, struct device *parent,
277e8ad415bSTakashi Iwai 			 int idx, const char *xid, struct module *module,
278e8ad415bSTakashi Iwai 			 size_t extra_size)
279e8ad415bSTakashi Iwai {
280e8ad415bSTakashi Iwai 	int err;
281e8ad415bSTakashi Iwai 
2828bfb181cSTakashi Iwai 	if (extra_size > 0)
2838bfb181cSTakashi Iwai 		card->private_data = (char *)card + sizeof(struct snd_card);
28410a8ebbbSJaroslav Kysela 	if (xid)
28575b1a8f9SJoe Perches 		strscpy(card->id, xid, sizeof(card->id));
2861da177e4SLinus Torvalds 	err = 0;
287746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
288deb6596fSTakashi Iwai 	if (idx < 0) /* first check the matching module-name slot */
289deb6596fSTakashi Iwai 		idx = get_slot_from_bitmask(idx, module_slot_match, module);
290deb6596fSTakashi Iwai 	if (idx < 0) /* if not matched, assign an empty slot */
291deb6596fSTakashi Iwai 		idx = get_slot_from_bitmask(idx, check_empty_slot, module);
292a93bbaa7STakashi Iwai 	if (idx < 0)
293a93bbaa7STakashi Iwai 		err = -ENODEV;
294a93bbaa7STakashi Iwai 	else if (idx < snd_ecards_limit) {
2957bb2491bSTakashi Iwai 		if (test_bit(idx, snd_cards_lock))
2965c33dd70SOliver Neukum 			err = -EBUSY;	/* invalid */
297a93bbaa7STakashi Iwai 	} else if (idx >= SNDRV_CARDS)
2981da177e4SLinus Torvalds 		err = -ENODEV;
299a93bbaa7STakashi Iwai 	if (err < 0) {
300746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
301f2f9307aSTakashi Iwai 		dev_err(parent, "cannot find the slot for index %d (range 0-%i), error: %d\n",
3025c33dd70SOliver Neukum 			 idx, snd_ecards_limit - 1, err);
303c3afa2a4STakashi Iwai 		if (!card->managed)
304c3afa2a4STakashi Iwai 			kfree(card); /* manually free here, as no destructor called */
3058bfb181cSTakashi Iwai 		return err;
3061da177e4SLinus Torvalds 	}
3077bb2491bSTakashi Iwai 	set_bit(idx, snd_cards_lock);		/* lock it */
308a93bbaa7STakashi Iwai 	if (idx >= snd_ecards_limit)
309a93bbaa7STakashi Iwai 		snd_ecards_limit = idx + 1; /* increase the limit */
310746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
311393aa9c1STakashi Iwai 	card->dev = parent;
3121da177e4SLinus Torvalds 	card->number = idx;
31381033c6bSTakashi Iwai #ifdef MODULE
31481033c6bSTakashi Iwai 	WARN_ON(!module);
31581033c6bSTakashi Iwai #endif
316e644036aSTakashi Iwai 	card->module = module;
3171da177e4SLinus Torvalds 	INIT_LIST_HEAD(&card->devices);
3181da177e4SLinus Torvalds 	init_rwsem(&card->controls_rwsem);
3191da177e4SLinus Torvalds 	rwlock_init(&card->ctl_files_rwlock);
3201da177e4SLinus Torvalds 	INIT_LIST_HEAD(&card->controls);
3211da177e4SLinus Torvalds 	INIT_LIST_HEAD(&card->ctl_files);
322c27e1efbSTakashi Iwai #ifdef CONFIG_SND_CTL_FAST_LOOKUP
323c27e1efbSTakashi Iwai 	xa_init(&card->ctl_numids);
324c27e1efbSTakashi Iwai 	xa_init(&card->ctl_hash);
325c27e1efbSTakashi Iwai #endif
3261da177e4SLinus Torvalds 	spin_lock_init(&card->files_lock);
327118dd6bfSTakashi Iwai 	INIT_LIST_HEAD(&card->files_list);
328d4cfb30fSTakashi Iwai 	mutex_init(&card->memory_mutex);
3291da177e4SLinus Torvalds #ifdef CONFIG_PM
3301da177e4SLinus Torvalds 	init_waitqueue_head(&card->power_sleep);
331e94fdbd7STakashi Iwai 	init_waitqueue_head(&card->power_ref_sleep);
332e94fdbd7STakashi Iwai 	atomic_set(&card->power_ref, 0);
3331da177e4SLinus Torvalds #endif
334c44027c8STakashi Iwai 	init_waitqueue_head(&card->remove_sleep);
335fabb26dcSTakashi Iwai 	card->sync_irq = -1;
3368bfb181cSTakashi Iwai 
3378bfb181cSTakashi Iwai 	device_initialize(&card->card_dev);
3388bfb181cSTakashi Iwai 	card->card_dev.parent = parent;
3398d0cf150SIvan Orlov 	card->card_dev.class = &sound_class;
3408bfb181cSTakashi Iwai 	card->card_dev.release = release_card_device;
3416bbc7fedSTakashi Iwai 	card->card_dev.groups = card->dev_groups;
3426bbc7fedSTakashi Iwai 	card->dev_groups[0] = &card_dev_attr_group;
3438bfb181cSTakashi Iwai 	err = kobject_set_name(&card->card_dev.kobj, "card%d", idx);
3448bfb181cSTakashi Iwai 	if (err < 0)
3458bfb181cSTakashi Iwai 		goto __error;
3468bfb181cSTakashi Iwai 
347de65360bSHeiner Kallweit 	snprintf(card->irq_descr, sizeof(card->irq_descr), "%s:%s",
348de65360bSHeiner Kallweit 		 dev_driver_string(card->dev), dev_name(&card->card_dev));
349de65360bSHeiner Kallweit 
3501da177e4SLinus Torvalds 	/* the control interface cannot be accessed from the user space until */
3511da177e4SLinus Torvalds 	/* snd_cards_bitmask and snd_cards are set with snd_card_register */
35253fb1e63STakashi Iwai 	err = snd_ctl_create(card);
35353fb1e63STakashi Iwai 	if (err < 0) {
354f2f9307aSTakashi Iwai 		dev_err(parent, "unable to register control minors\n");
3551da177e4SLinus Torvalds 		goto __error;
3561da177e4SLinus Torvalds 	}
35753fb1e63STakashi Iwai 	err = snd_info_card_create(card);
35853fb1e63STakashi Iwai 	if (err < 0) {
359f2f9307aSTakashi Iwai 		dev_err(parent, "unable to create card info\n");
3601da177e4SLinus Torvalds 		goto __error_ctl;
3611da177e4SLinus Torvalds 	}
3622d670ea2SHui Wang 
3632d670ea2SHui Wang #ifdef CONFIG_SND_DEBUG
364091c2848SPeter Ujfalusi 	card->debugfs_root = debugfs_create_dir(dev_name(&card->card_dev),
365091c2848SPeter Ujfalusi 						sound_debugfs_root);
3662d670ea2SHui Wang #endif
36753fb1e63STakashi Iwai 	return 0;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds       __error_ctl:
370289ca025STakashi Iwai 	snd_device_free_all(card);
3711da177e4SLinus Torvalds       __error:
3728bfb181cSTakashi Iwai 	put_device(&card->card_dev);
37353fb1e63STakashi Iwai   	return err;
3741da177e4SLinus Torvalds }
375c0d3fb39STakashi Iwai 
376f4fa9689STakashi Iwai /**
377f4fa9689STakashi Iwai  * snd_card_ref - Get the card object from the index
378f4fa9689STakashi Iwai  * @idx: the card index
379f4fa9689STakashi Iwai  *
380f4fa9689STakashi Iwai  * Returns a card object corresponding to the given index or NULL if not found.
381f4fa9689STakashi Iwai  * Release the object via snd_card_unref().
382281dee67STakashi Iwai  *
383281dee67STakashi Iwai  * Return: a card object or NULL
384f4fa9689STakashi Iwai  */
snd_card_ref(int idx)385f4fa9689STakashi Iwai struct snd_card *snd_card_ref(int idx)
386f4fa9689STakashi Iwai {
387f4fa9689STakashi Iwai 	struct snd_card *card;
388f4fa9689STakashi Iwai 
389f4fa9689STakashi Iwai 	mutex_lock(&snd_card_mutex);
390f4fa9689STakashi Iwai 	card = snd_cards[idx];
391f4fa9689STakashi Iwai 	if (card)
392f4fa9689STakashi Iwai 		get_device(&card->card_dev);
393f4fa9689STakashi Iwai 	mutex_unlock(&snd_card_mutex);
394f4fa9689STakashi Iwai 	return card;
395f4fa9689STakashi Iwai }
396f4fa9689STakashi Iwai EXPORT_SYMBOL_GPL(snd_card_ref);
397f4fa9689STakashi Iwai 
398746df948STakashi Iwai /* return non-zero if a card is already locked */
snd_card_locked(int card)399746df948STakashi Iwai int snd_card_locked(int card)
400746df948STakashi Iwai {
401746df948STakashi Iwai 	int locked;
402746df948STakashi Iwai 
403746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
4047bb2491bSTakashi Iwai 	locked = test_bit(card, snd_cards_lock);
405746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
406746df948STakashi Iwai 	return locked;
407746df948STakashi Iwai }
408746df948STakashi Iwai 
snd_disconnect_llseek(struct file * file,loff_t offset,int orig)409b3b0abe1SClemens Ladisch static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
410b3b0abe1SClemens Ladisch {
411b3b0abe1SClemens Ladisch 	return -ENODEV;
412b3b0abe1SClemens Ladisch }
413b3b0abe1SClemens Ladisch 
snd_disconnect_read(struct file * file,char __user * buf,size_t count,loff_t * offset)414b3b0abe1SClemens Ladisch static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
415b3b0abe1SClemens Ladisch 				   size_t count, loff_t *offset)
416b3b0abe1SClemens Ladisch {
417b3b0abe1SClemens Ladisch 	return -ENODEV;
418b3b0abe1SClemens Ladisch }
419b3b0abe1SClemens Ladisch 
snd_disconnect_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)420b3b0abe1SClemens Ladisch static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
421b3b0abe1SClemens Ladisch 				    size_t count, loff_t *offset)
422b3b0abe1SClemens Ladisch {
423b3b0abe1SClemens Ladisch 	return -ENODEV;
424b3b0abe1SClemens Ladisch }
425b3b0abe1SClemens Ladisch 
snd_disconnect_release(struct inode * inode,struct file * file)426a9edfc60SKarsten Wiese static int snd_disconnect_release(struct inode *inode, struct file *file)
427a9edfc60SKarsten Wiese {
428a9edfc60SKarsten Wiese 	struct snd_monitor_file *df = NULL, *_df;
429a9edfc60SKarsten Wiese 
430a9edfc60SKarsten Wiese 	spin_lock(&shutdown_lock);
431a9edfc60SKarsten Wiese 	list_for_each_entry(_df, &shutdown_files, shutdown_list) {
432a9edfc60SKarsten Wiese 		if (_df->file == file) {
433a9edfc60SKarsten Wiese 			df = _df;
434118dd6bfSTakashi Iwai 			list_del_init(&df->shutdown_list);
435a9edfc60SKarsten Wiese 			break;
436a9edfc60SKarsten Wiese 		}
437a9edfc60SKarsten Wiese 	}
438a9edfc60SKarsten Wiese 	spin_unlock(&shutdown_lock);
439a9edfc60SKarsten Wiese 
440233e70f4SAl Viro 	if (likely(df)) {
441233e70f4SAl Viro 		if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync)
442233e70f4SAl Viro 			df->disconnected_f_op->fasync(-1, file, 0);
443a9edfc60SKarsten Wiese 		return df->disconnected_f_op->release(inode, file);
444233e70f4SAl Viro 	}
445a9edfc60SKarsten Wiese 
4469bf8e7ddSHarvey Harrison 	panic("%s(%p, %p) failed!", __func__, inode, file);
447a9edfc60SKarsten Wiese }
448a9edfc60SKarsten Wiese 
snd_disconnect_poll(struct file * file,poll_table * wait)449680ef72aSAl Viro static __poll_t snd_disconnect_poll(struct file * file, poll_table * wait)
4501da177e4SLinus Torvalds {
451a9a08845SLinus Torvalds 	return EPOLLERR | EPOLLNVAL;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
snd_disconnect_ioctl(struct file * file,unsigned int cmd,unsigned long arg)454b3b0abe1SClemens Ladisch static long snd_disconnect_ioctl(struct file *file,
455b3b0abe1SClemens Ladisch 				 unsigned int cmd, unsigned long arg)
456b3b0abe1SClemens Ladisch {
457b3b0abe1SClemens Ladisch 	return -ENODEV;
458b3b0abe1SClemens Ladisch }
459b3b0abe1SClemens Ladisch 
snd_disconnect_mmap(struct file * file,struct vm_area_struct * vma)460b3b0abe1SClemens Ladisch static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
461b3b0abe1SClemens Ladisch {
462b3b0abe1SClemens Ladisch 	return -ENODEV;
463b3b0abe1SClemens Ladisch }
464b3b0abe1SClemens Ladisch 
snd_disconnect_fasync(int fd,struct file * file,int on)465b3b0abe1SClemens Ladisch static int snd_disconnect_fasync(int fd, struct file *file, int on)
466b3b0abe1SClemens Ladisch {
467b3b0abe1SClemens Ladisch 	return -ENODEV;
468b3b0abe1SClemens Ladisch }
469b3b0abe1SClemens Ladisch 
4709c2e08c5SArjan van de Ven static const struct file_operations snd_shutdown_f_ops =
471a9edfc60SKarsten Wiese {
472a9edfc60SKarsten Wiese 	.owner = 	THIS_MODULE,
473a9edfc60SKarsten Wiese 	.llseek =	snd_disconnect_llseek,
474a9edfc60SKarsten Wiese 	.read = 	snd_disconnect_read,
475a9edfc60SKarsten Wiese 	.write =	snd_disconnect_write,
476a9edfc60SKarsten Wiese 	.release =	snd_disconnect_release,
477a9edfc60SKarsten Wiese 	.poll =		snd_disconnect_poll,
478a9edfc60SKarsten Wiese 	.unlocked_ioctl = snd_disconnect_ioctl,
479a9edfc60SKarsten Wiese #ifdef CONFIG_COMPAT
480a9edfc60SKarsten Wiese 	.compat_ioctl = snd_disconnect_ioctl,
481a9edfc60SKarsten Wiese #endif
482a9edfc60SKarsten Wiese 	.mmap =		snd_disconnect_mmap,
483a9edfc60SKarsten Wiese 	.fasync =	snd_disconnect_fasync
484a9edfc60SKarsten Wiese };
485a9edfc60SKarsten Wiese 
4861da177e4SLinus Torvalds /**
4871da177e4SLinus Torvalds  *  snd_card_disconnect - disconnect all APIs from the file-operations (user space)
4881da177e4SLinus Torvalds  *  @card: soundcard structure
4891da177e4SLinus Torvalds  *
4901da177e4SLinus Torvalds  *  Disconnects all APIs from the file-operations (user space).
4911da177e4SLinus Torvalds  *
492eb7c06e8SYacine Belkadi  *  Return: Zero, otherwise a negative error code.
4931da177e4SLinus Torvalds  *
4941da177e4SLinus Torvalds  *  Note: The current implementation replaces all active file->f_op with special
4951da177e4SLinus Torvalds  *        dummy file operations (they do nothing except release).
4961da177e4SLinus Torvalds  */
snd_card_disconnect(struct snd_card * card)497663f922fSUwe Kleine-König void snd_card_disconnect(struct snd_card *card)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	struct snd_monitor_file *mfile;
5001da177e4SLinus Torvalds 
501f18638dcSTakashi Iwai 	if (!card)
502663f922fSUwe Kleine-König 		return;
503f18638dcSTakashi Iwai 
5041da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
5051da177e4SLinus Torvalds 	if (card->shutdown) {
5061da177e4SLinus Torvalds 		spin_unlock(&card->files_lock);
507663f922fSUwe Kleine-König 		return;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 	card->shutdown = 1;
5101da177e4SLinus Torvalds 
5112a3f7221STakashi Iwai 	/* replace file->f_op with special dummy operations */
512118dd6bfSTakashi Iwai 	list_for_each_entry(mfile, &card->files_list, list) {
5131da177e4SLinus Torvalds 		/* it's critical part, use endless loop */
5141da177e4SLinus Torvalds 		/* we have no room to fail */
515a9edfc60SKarsten Wiese 		mfile->disconnected_f_op = mfile->file->f_op;
5161da177e4SLinus Torvalds 
517a9edfc60SKarsten Wiese 		spin_lock(&shutdown_lock);
518a9edfc60SKarsten Wiese 		list_add(&mfile->shutdown_list, &shutdown_files);
519a9edfc60SKarsten Wiese 		spin_unlock(&shutdown_lock);
5201da177e4SLinus Torvalds 
521a9edfc60SKarsten Wiese 		mfile->file->f_op = &snd_shutdown_f_ops;
522bc9abce0SMiguel Boton 		fops_get(mfile->file->f_op);
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds 	spin_unlock(&card->files_lock);
5251da177e4SLinus Torvalds 
5262f103287STakashi Iwai #ifdef CONFIG_PM
5272f103287STakashi Iwai 	/* wake up sleepers here before other callbacks for avoiding potential
5282f103287STakashi Iwai 	 * deadlocks with other locks (e.g. in kctls);
5292f103287STakashi Iwai 	 * then this notifies the shutdown and sleepers would abort immediately
5302f103287STakashi Iwai 	 */
5312f103287STakashi Iwai 	wake_up_all(&card->power_sleep);
5322f103287STakashi Iwai #endif
5332f103287STakashi Iwai 
5342a3f7221STakashi Iwai 	/* notify all connected devices about disconnection */
5351da177e4SLinus Torvalds 	/* at this point, they cannot respond to any calls except release() */
5361da177e4SLinus Torvalds 
5378eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
5381da177e4SLinus Torvalds 	if (snd_mixer_oss_notify_callback)
5391da177e4SLinus Torvalds 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
5401da177e4SLinus Torvalds #endif
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	/* notify all devices that we are disconnected */
543e086e303STakashi Iwai 	snd_device_disconnect_all(card);
5441da177e4SLinus Torvalds 
54529bb274eSTakashi Iwai 	if (card->sync_irq > 0)
54629bb274eSTakashi Iwai 		synchronize_irq(card->sync_irq);
54729bb274eSTakashi Iwai 
548746d4a02STakashi Iwai 	snd_info_card_disconnect(card);
549cb9c2bd4STakashi Iwai #ifdef CONFIG_SND_DEBUG
550cb9c2bd4STakashi Iwai 	debugfs_remove(card->debugfs_root);
551cb9c2bd4STakashi Iwai 	card->debugfs_root = NULL;
552cb9c2bd4STakashi Iwai #endif
553cb9c2bd4STakashi Iwai 
5548bfb181cSTakashi Iwai 	if (card->registered) {
5558bfb181cSTakashi Iwai 		device_del(&card->card_dev);
5568bfb181cSTakashi Iwai 		card->registered = false;
55773d38b13STakashi Iwai 	}
5582a3f7221STakashi Iwai 
5592a3f7221STakashi Iwai 	/* disable fops (user space) operations for ALSA API */
5602a3f7221STakashi Iwai 	mutex_lock(&snd_card_mutex);
5612a3f7221STakashi Iwai 	snd_cards[card->number] = NULL;
5622a3f7221STakashi Iwai 	clear_bit(card->number, snd_cards_lock);
5632a3f7221STakashi Iwai 	mutex_unlock(&snd_card_mutex);
5642a3f7221STakashi Iwai 
565f18638dcSTakashi Iwai #ifdef CONFIG_PM
566e94fdbd7STakashi Iwai 	snd_power_sync_ref(card);
567f18638dcSTakashi Iwai #endif
5681da177e4SLinus Torvalds }
569c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_disconnect);
570c0d3fb39STakashi Iwai 
571c44027c8STakashi Iwai /**
572c44027c8STakashi Iwai  * snd_card_disconnect_sync - disconnect card and wait until files get closed
573c44027c8STakashi Iwai  * @card: card object to disconnect
574c44027c8STakashi Iwai  *
575c44027c8STakashi Iwai  * This calls snd_card_disconnect() for disconnecting all belonging components
576c44027c8STakashi Iwai  * and waits until all pending files get closed.
577c44027c8STakashi Iwai  * It assures that all accesses from user-space finished so that the driver
578c44027c8STakashi Iwai  * can release its resources gracefully.
579c44027c8STakashi Iwai  */
snd_card_disconnect_sync(struct snd_card * card)580c44027c8STakashi Iwai void snd_card_disconnect_sync(struct snd_card *card)
581c44027c8STakashi Iwai {
582663f922fSUwe Kleine-König 	snd_card_disconnect(card);
583c44027c8STakashi Iwai 
584c44027c8STakashi Iwai 	spin_lock_irq(&card->files_lock);
585c44027c8STakashi Iwai 	wait_event_lock_irq(card->remove_sleep,
586c44027c8STakashi Iwai 			    list_empty(&card->files_list),
587c44027c8STakashi Iwai 			    card->files_lock);
588c44027c8STakashi Iwai 	spin_unlock_irq(&card->files_lock);
589c44027c8STakashi Iwai }
590c44027c8STakashi Iwai EXPORT_SYMBOL_GPL(snd_card_disconnect_sync);
591c44027c8STakashi Iwai 
snd_card_do_free(struct snd_card * card)592c461482cSTakashi Iwai static int snd_card_do_free(struct snd_card *card)
5931da177e4SLinus Torvalds {
594e8ad415bSTakashi Iwai 	card->releasing = true;
5958eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
5961da177e4SLinus Torvalds 	if (snd_mixer_oss_notify_callback)
5971da177e4SLinus Torvalds 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
5981da177e4SLinus Torvalds #endif
59972620d60STakashi Iwai 	snd_device_free_all(card);
6001da177e4SLinus Torvalds 	if (card->private_free)
6011da177e4SLinus Torvalds 		card->private_free(card);
6021da177e4SLinus Torvalds 	if (snd_info_card_free(card) < 0) {
603f2f9307aSTakashi Iwai 		dev_warn(card->dev, "unable to free card info\n");
6041da177e4SLinus Torvalds 		/* Not fatal error */
6051da177e4SLinus Torvalds 	}
606f2464064STakashi Iwai 	if (card->release_completion)
607f2464064STakashi Iwai 		complete(card->release_completion);
608e8ad415bSTakashi Iwai 	if (!card->managed)
609c461482cSTakashi Iwai 		kfree(card);
610c461482cSTakashi Iwai 	return 0;
611c461482cSTakashi Iwai }
612c461482cSTakashi Iwai 
613eb9c38d5STakashi Iwai /**
614eb9c38d5STakashi Iwai  * snd_card_free_when_closed - Disconnect the card, free it later eventually
615eb9c38d5STakashi Iwai  * @card: soundcard structure
616eb9c38d5STakashi Iwai  *
617eb9c38d5STakashi Iwai  * Unlike snd_card_free(), this function doesn't try to release the card
618eb9c38d5STakashi Iwai  * resource immediately, but tries to disconnect at first.  When the card
619eb9c38d5STakashi Iwai  * is still in use, the function returns before freeing the resources.
620eb9c38d5STakashi Iwai  * The card resources will be freed when the refcount gets to zero.
621281dee67STakashi Iwai  *
622281dee67STakashi Iwai  * Return: zero if successful, or a negative error code
623eb9c38d5STakashi Iwai  */
snd_card_free_when_closed(struct snd_card * card)62473c5685cSUwe Kleine-König void snd_card_free_when_closed(struct snd_card *card)
625c461482cSTakashi Iwai {
626663f922fSUwe Kleine-König 	if (!card)
62773c5685cSUwe Kleine-König 		return;
628663f922fSUwe Kleine-König 
629663f922fSUwe Kleine-König 	snd_card_disconnect(card);
6308bfb181cSTakashi Iwai 	put_device(&card->card_dev);
63173c5685cSUwe Kleine-König 	return;
6321da177e4SLinus Torvalds }
633f2464064STakashi Iwai EXPORT_SYMBOL(snd_card_free_when_closed);
6341da177e4SLinus Torvalds 
635eb9c38d5STakashi Iwai /**
636eb9c38d5STakashi Iwai  * snd_card_free - frees given soundcard structure
637eb9c38d5STakashi Iwai  * @card: soundcard structure
638eb9c38d5STakashi Iwai  *
639eb9c38d5STakashi Iwai  * This function releases the soundcard structure and the all assigned
640eb9c38d5STakashi Iwai  * devices automatically.  That is, you don't have to release the devices
641eb9c38d5STakashi Iwai  * by yourself.
642eb9c38d5STakashi Iwai  *
643eb9c38d5STakashi Iwai  * This function waits until the all resources are properly released.
644eb9c38d5STakashi Iwai  *
645eb9c38d5STakashi Iwai  * Return: Zero. Frees all associated devices and frees the control
646eb9c38d5STakashi Iwai  * interface associated to given soundcard.
647eb9c38d5STakashi Iwai  */
snd_card_free(struct snd_card * card)64825a5a77aSUwe Kleine-König void snd_card_free(struct snd_card *card)
649f2464064STakashi Iwai {
650bec206dbSPierre-Louis Bossart 	DECLARE_COMPLETION_ONSTACK(released);
651f2464064STakashi Iwai 
652825a5248STakashi Iwai 	/* The call of snd_card_free() is allowed from various code paths;
653825a5248STakashi Iwai 	 * a manual call from the driver and the call via devres_free, and
654825a5248STakashi Iwai 	 * we need to avoid double-free. Moreover, the release via devres
655825a5248STakashi Iwai 	 * may call snd_card_free() twice due to its nature, we need to have
656825a5248STakashi Iwai 	 * the check here at the beginning.
657825a5248STakashi Iwai 	 */
658825a5248STakashi Iwai 	if (card->releasing)
65925a5a77aSUwe Kleine-König 		return;
660825a5248STakashi Iwai 
661f2464064STakashi Iwai 	card->release_completion = &released;
66273c5685cSUwe Kleine-König 	snd_card_free_when_closed(card);
66373c5685cSUwe Kleine-König 
664f2464064STakashi Iwai 	/* wait, until all devices are ready for the free operation */
665f2464064STakashi Iwai 	wait_for_completion(&released);
666f2464064STakashi Iwai }
667c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_free);
668c0d3fb39STakashi Iwai 
669e7df2a3aSTakashi Iwai /* retrieve the last word of shortname or longname */
retrieve_id_from_card_name(const char * name)670e7df2a3aSTakashi Iwai static const char *retrieve_id_from_card_name(const char *name)
6711da177e4SLinus Torvalds {
672e7df2a3aSTakashi Iwai 	const char *spos = name;
673e7df2a3aSTakashi Iwai 
674e7df2a3aSTakashi Iwai 	while (*name) {
675e7df2a3aSTakashi Iwai 		if (isspace(*name) && isalnum(name[1]))
676e7df2a3aSTakashi Iwai 			spos = name + 1;
677e7df2a3aSTakashi Iwai 		name++;
678e7df2a3aSTakashi Iwai 	}
679e7df2a3aSTakashi Iwai 	return spos;
680e7df2a3aSTakashi Iwai }
681e7df2a3aSTakashi Iwai 
682e7df2a3aSTakashi Iwai /* return true if the given id string doesn't conflict any other card ids */
card_id_ok(struct snd_card * card,const char * id)683e7df2a3aSTakashi Iwai static bool card_id_ok(struct snd_card *card, const char *id)
684e7df2a3aSTakashi Iwai {
685e7df2a3aSTakashi Iwai 	int i;
686e7df2a3aSTakashi Iwai 	if (!snd_info_check_reserved_words(id))
687e7df2a3aSTakashi Iwai 		return false;
688e7df2a3aSTakashi Iwai 	for (i = 0; i < snd_ecards_limit; i++) {
689e7df2a3aSTakashi Iwai 		if (snd_cards[i] && snd_cards[i] != card &&
690e7df2a3aSTakashi Iwai 		    !strcmp(snd_cards[i]->id, id))
691e7df2a3aSTakashi Iwai 			return false;
692e7df2a3aSTakashi Iwai 	}
693e7df2a3aSTakashi Iwai 	return true;
694e7df2a3aSTakashi Iwai }
695e7df2a3aSTakashi Iwai 
696e7df2a3aSTakashi Iwai /* copy to card->id only with valid letters from nid */
copy_valid_id_string(struct snd_card * card,const char * src,const char * nid)697e7df2a3aSTakashi Iwai static void copy_valid_id_string(struct snd_card *card, const char *src,
698e7df2a3aSTakashi Iwai 				 const char *nid)
699e7df2a3aSTakashi Iwai {
700e7df2a3aSTakashi Iwai 	char *id = card->id;
701e7df2a3aSTakashi Iwai 
702e7df2a3aSTakashi Iwai 	while (*nid && !isalnum(*nid))
703e7df2a3aSTakashi Iwai 		nid++;
704e7df2a3aSTakashi Iwai 	if (isdigit(*nid))
705e7df2a3aSTakashi Iwai 		*id++ = isalpha(*src) ? *src : 'D';
706e7df2a3aSTakashi Iwai 	while (*nid && (size_t)(id - card->id) < sizeof(card->id) - 1) {
707e7df2a3aSTakashi Iwai 		if (isalnum(*nid))
708e7df2a3aSTakashi Iwai 			*id++ = *nid;
709e7df2a3aSTakashi Iwai 		nid++;
710e7df2a3aSTakashi Iwai 	}
711e7df2a3aSTakashi Iwai 	*id = 0;
712e7df2a3aSTakashi Iwai }
713e7df2a3aSTakashi Iwai 
714e7df2a3aSTakashi Iwai /* Set card->id from the given string
715e7df2a3aSTakashi Iwai  * If the string conflicts with other ids, add a suffix to make it unique.
716e7df2a3aSTakashi Iwai  */
snd_card_set_id_no_lock(struct snd_card * card,const char * src,const char * nid)717e7df2a3aSTakashi Iwai static void snd_card_set_id_no_lock(struct snd_card *card, const char *src,
718e7df2a3aSTakashi Iwai 				    const char *nid)
719e7df2a3aSTakashi Iwai {
720e7df2a3aSTakashi Iwai 	int len, loops;
721e7df2a3aSTakashi Iwai 	bool is_default = false;
72210a8ebbbSJaroslav Kysela 	char *id;
7231da177e4SLinus Torvalds 
724e7df2a3aSTakashi Iwai 	copy_valid_id_string(card, src, nid);
7251da177e4SLinus Torvalds 	id = card->id;
7261da177e4SLinus Torvalds 
727e7df2a3aSTakashi Iwai  again:
728e7df2a3aSTakashi Iwai 	/* use "Default" for obviously invalid strings
729e7df2a3aSTakashi Iwai 	 * ("card" conflicts with proc directories)
730e7df2a3aSTakashi Iwai 	 */
731e7df2a3aSTakashi Iwai 	if (!*id || !strncmp(id, "card", 4)) {
732d8e4f9aeSTakashi Iwai 		strcpy(id, "Default");
733e7df2a3aSTakashi Iwai 		is_default = true;
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds 
7368edbb198STakashi Iwai 	len = strlen(id);
737e7df2a3aSTakashi Iwai 	for (loops = 0; loops < SNDRV_CARDS; loops++) {
7388edbb198STakashi Iwai 		char *spos;
7398edbb198STakashi Iwai 		char sfxstr[5]; /* "_012" */
7408edbb198STakashi Iwai 		int sfxlen;
7418edbb198STakashi Iwai 
742e7df2a3aSTakashi Iwai 		if (card_id_ok(card, id))
743e7df2a3aSTakashi Iwai 			return; /* OK */
744e7df2a3aSTakashi Iwai 
7458edbb198STakashi Iwai 		/* Add _XYZ suffix */
7468edbb198STakashi Iwai 		sprintf(sfxstr, "_%X", loops + 1);
7478edbb198STakashi Iwai 		sfxlen = strlen(sfxstr);
7488edbb198STakashi Iwai 		if (len + sfxlen >= sizeof(card->id))
7498edbb198STakashi Iwai 			spos = id + sizeof(card->id) - sfxlen - 1;
750d001544dSClemens Ladisch 		else
7518edbb198STakashi Iwai 			spos = id + len;
7528edbb198STakashi Iwai 		strcpy(spos, sfxstr);
7531da177e4SLinus Torvalds 	}
754e7df2a3aSTakashi Iwai 	/* fallback to the default id */
755e7df2a3aSTakashi Iwai 	if (!is_default) {
756e7df2a3aSTakashi Iwai 		*id = 0;
757e7df2a3aSTakashi Iwai 		goto again;
758e7df2a3aSTakashi Iwai 	}
759e7df2a3aSTakashi Iwai 	/* last resort... */
760f2f9307aSTakashi Iwai 	dev_err(card->dev, "unable to set card id (%s)\n", id);
761e7df2a3aSTakashi Iwai 	if (card->proc_root->name)
76275b1a8f9SJoe Perches 		strscpy(card->id, card->proc_root->name, sizeof(card->id));
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
765872c7820SMark Brown /**
766872c7820SMark Brown  *  snd_card_set_id - set card identification name
767872c7820SMark Brown  *  @card: soundcard structure
768872c7820SMark Brown  *  @nid: new identification string
769872c7820SMark Brown  *
770872c7820SMark Brown  *  This function sets the card identification and checks for name
771872c7820SMark Brown  *  collisions.
772872c7820SMark Brown  */
snd_card_set_id(struct snd_card * card,const char * nid)773872c7820SMark Brown void snd_card_set_id(struct snd_card *card, const char *nid)
774872c7820SMark Brown {
7755fdc18d9SJaroslav Kysela 	/* check if user specified own card->id */
7765fdc18d9SJaroslav Kysela 	if (card->id[0] != '\0')
7775fdc18d9SJaroslav Kysela 		return;
7785fdc18d9SJaroslav Kysela 	mutex_lock(&snd_card_mutex);
779e7df2a3aSTakashi Iwai 	snd_card_set_id_no_lock(card, nid, nid);
7805fdc18d9SJaroslav Kysela 	mutex_unlock(&snd_card_mutex);
781872c7820SMark Brown }
78210a8ebbbSJaroslav Kysela EXPORT_SYMBOL(snd_card_set_id);
78310a8ebbbSJaroslav Kysela 
id_show(struct device * dev,struct device_attribute * attr,char * buf)784873fd813SYueHaibing static ssize_t id_show(struct device *dev,
7859fb6198eSJaroslav Kysela 		       struct device_attribute *attr, char *buf)
7869fb6198eSJaroslav Kysela {
787b203dbabSTakashi Iwai 	struct snd_card *card = container_of(dev, struct snd_card, card_dev);
7880031812bSTakashi Iwai 	return sysfs_emit(buf, "%s\n", card->id);
7899fb6198eSJaroslav Kysela }
7909fb6198eSJaroslav Kysela 
id_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)791873fd813SYueHaibing static ssize_t id_store(struct device *dev, struct device_attribute *attr,
7929fb6198eSJaroslav Kysela 			const char *buf, size_t count)
7939fb6198eSJaroslav Kysela {
794b203dbabSTakashi Iwai 	struct snd_card *card = container_of(dev, struct snd_card, card_dev);
7959fb6198eSJaroslav Kysela 	char buf1[sizeof(card->id)];
7969fb6198eSJaroslav Kysela 	size_t copy = count > sizeof(card->id) - 1 ?
7979fb6198eSJaroslav Kysela 					sizeof(card->id) - 1 : count;
7989fb6198eSJaroslav Kysela 	size_t idx;
7999fb6198eSJaroslav Kysela 	int c;
8009fb6198eSJaroslav Kysela 
8019fb6198eSJaroslav Kysela 	for (idx = 0; idx < copy; idx++) {
8029fb6198eSJaroslav Kysela 		c = buf[idx];
8039fb6198eSJaroslav Kysela 		if (!isalnum(c) && c != '_' && c != '-')
8049fb6198eSJaroslav Kysela 			return -EINVAL;
8059fb6198eSJaroslav Kysela 	}
8069fb6198eSJaroslav Kysela 	memcpy(buf1, buf, copy);
8079fb6198eSJaroslav Kysela 	buf1[copy] = '\0';
8089fb6198eSJaroslav Kysela 	mutex_lock(&snd_card_mutex);
809e7df2a3aSTakashi Iwai 	if (!card_id_ok(NULL, buf1)) {
8109fb6198eSJaroslav Kysela 		mutex_unlock(&snd_card_mutex);
8119fb6198eSJaroslav Kysela 		return -EEXIST;
8129fb6198eSJaroslav Kysela 	}
8139fb6198eSJaroslav Kysela 	strcpy(card->id, buf1);
814c2eb9c4eSJaroslav Kysela 	snd_info_card_id_change(card);
8159fb6198eSJaroslav Kysela 	mutex_unlock(&snd_card_mutex);
8169fb6198eSJaroslav Kysela 
8179fb6198eSJaroslav Kysela 	return count;
8189fb6198eSJaroslav Kysela }
8199fb6198eSJaroslav Kysela 
820873fd813SYueHaibing static DEVICE_ATTR_RW(id);
8219fb6198eSJaroslav Kysela 
number_show(struct device * dev,struct device_attribute * attr,char * buf)822873fd813SYueHaibing static ssize_t number_show(struct device *dev,
8239fb6198eSJaroslav Kysela 			   struct device_attribute *attr, char *buf)
8249fb6198eSJaroslav Kysela {
825b203dbabSTakashi Iwai 	struct snd_card *card = container_of(dev, struct snd_card, card_dev);
8260031812bSTakashi Iwai 	return sysfs_emit(buf, "%i\n", card->number);
8279fb6198eSJaroslav Kysela }
8289fb6198eSJaroslav Kysela 
829873fd813SYueHaibing static DEVICE_ATTR_RO(number);
83034356dbdSTakashi Iwai 
83134356dbdSTakashi Iwai static struct attribute *card_dev_attrs[] = {
83234356dbdSTakashi Iwai 	&dev_attr_id.attr,
83334356dbdSTakashi Iwai 	&dev_attr_number.attr,
83434356dbdSTakashi Iwai 	NULL
83534356dbdSTakashi Iwai };
83634356dbdSTakashi Iwai 
8376bbc7fedSTakashi Iwai static const struct attribute_group card_dev_attr_group = {
83834356dbdSTakashi Iwai 	.attrs	= card_dev_attrs,
83934356dbdSTakashi Iwai };
84034356dbdSTakashi Iwai 
8416bbc7fedSTakashi Iwai /**
8426bbc7fedSTakashi Iwai  * snd_card_add_dev_attr - Append a new sysfs attribute group to card
8436bbc7fedSTakashi Iwai  * @card: card instance
8446bbc7fedSTakashi Iwai  * @group: attribute group to append
845281dee67STakashi Iwai  *
846281dee67STakashi Iwai  * Return: zero if successful, or a negative error code
8476bbc7fedSTakashi Iwai  */
snd_card_add_dev_attr(struct snd_card * card,const struct attribute_group * group)8486bbc7fedSTakashi Iwai int snd_card_add_dev_attr(struct snd_card *card,
8496bbc7fedSTakashi Iwai 			  const struct attribute_group *group)
8506bbc7fedSTakashi Iwai {
8516bbc7fedSTakashi Iwai 	int i;
8526bbc7fedSTakashi Iwai 
8536bbc7fedSTakashi Iwai 	/* loop for (arraysize-1) here to keep NULL at the last entry */
8546bbc7fedSTakashi Iwai 	for (i = 0; i < ARRAY_SIZE(card->dev_groups) - 1; i++) {
8556bbc7fedSTakashi Iwai 		if (!card->dev_groups[i]) {
8566bbc7fedSTakashi Iwai 			card->dev_groups[i] = group;
8576bbc7fedSTakashi Iwai 			return 0;
8586bbc7fedSTakashi Iwai 		}
8596bbc7fedSTakashi Iwai 	}
8606bbc7fedSTakashi Iwai 
8616bbc7fedSTakashi Iwai 	dev_err(card->dev, "Too many groups assigned\n");
8626bbc7fedSTakashi Iwai 	return -ENOSPC;
86335f80014STakashi Iwai }
8646bbc7fedSTakashi Iwai EXPORT_SYMBOL_GPL(snd_card_add_dev_attr);
8659fb6198eSJaroslav Kysela 
trigger_card_free(void * data)866e8ad415bSTakashi Iwai static void trigger_card_free(void *data)
867e8ad415bSTakashi Iwai {
868e8ad415bSTakashi Iwai 	snd_card_free(data);
869e8ad415bSTakashi Iwai }
870e8ad415bSTakashi Iwai 
8711da177e4SLinus Torvalds /**
8721da177e4SLinus Torvalds  *  snd_card_register - register the soundcard
8731da177e4SLinus Torvalds  *  @card: soundcard structure
8741da177e4SLinus Torvalds  *
8751da177e4SLinus Torvalds  *  This function registers all the devices assigned to the soundcard.
8761da177e4SLinus Torvalds  *  Until calling this, the ALSA control interface is blocked from the
8771da177e4SLinus Torvalds  *  external accesses.  Thus, you should call this function at the end
8781da177e4SLinus Torvalds  *  of the initialization of the card.
8791da177e4SLinus Torvalds  *
880eb7c06e8SYacine Belkadi  *  Return: Zero otherwise a negative error code if the registration failed.
8811da177e4SLinus Torvalds  */
snd_card_register(struct snd_card * card)882512bbd6aSTakashi Iwai int snd_card_register(struct snd_card *card)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	int err;
8851da177e4SLinus Torvalds 
8867eaa943cSTakashi Iwai 	if (snd_BUG_ON(!card))
8877eaa943cSTakashi Iwai 		return -EINVAL;
88839aba963SKay Sievers 
8898bfb181cSTakashi Iwai 	if (!card->registered) {
8908bfb181cSTakashi Iwai 		err = device_add(&card->card_dev);
8918bfb181cSTakashi Iwai 		if (err < 0)
8928bfb181cSTakashi Iwai 			return err;
8938bfb181cSTakashi Iwai 		card->registered = true;
894e8ad415bSTakashi Iwai 	} else {
895e8ad415bSTakashi Iwai 		if (card->managed)
896e8ad415bSTakashi Iwai 			devm_remove_action(card->dev, trigger_card_free, card);
897e8ad415bSTakashi Iwai 	}
898e8ad415bSTakashi Iwai 
899e8ad415bSTakashi Iwai 	if (card->managed) {
900e8ad415bSTakashi Iwai 		err = devm_add_action(card->dev, trigger_card_free, card);
901e8ad415bSTakashi Iwai 		if (err < 0)
902e8ad415bSTakashi Iwai 			return err;
903d80f19faSGreg Kroah-Hartman 	}
90439aba963SKay Sievers 
905e3ded899STakashi Iwai 	err = snd_device_register_all(card);
906e3ded899STakashi Iwai 	if (err < 0)
9071da177e4SLinus Torvalds 		return err;
908746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
9091da177e4SLinus Torvalds 	if (snd_cards[card->number]) {
9101da177e4SLinus Torvalds 		/* already registered */
911746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
9122471b6c8STakashi Iwai 		return snd_info_card_register(card); /* register pending info */
9131da177e4SLinus Torvalds 	}
914e7df2a3aSTakashi Iwai 	if (*card->id) {
915e7df2a3aSTakashi Iwai 		/* make a unique id name from the given string */
916e7df2a3aSTakashi Iwai 		char tmpid[sizeof(card->id)];
917e7df2a3aSTakashi Iwai 		memcpy(tmpid, card->id, sizeof(card->id));
918e7df2a3aSTakashi Iwai 		snd_card_set_id_no_lock(card, tmpid, tmpid);
919e7df2a3aSTakashi Iwai 	} else {
920e7df2a3aSTakashi Iwai 		/* create an id from either shortname or longname */
921e7df2a3aSTakashi Iwai 		const char *src;
922e7df2a3aSTakashi Iwai 		src = *card->shortname ? card->shortname : card->longname;
923e7df2a3aSTakashi Iwai 		snd_card_set_id_no_lock(card, src,
924e7df2a3aSTakashi Iwai 					retrieve_id_from_card_name(src));
925e7df2a3aSTakashi Iwai 	}
9261da177e4SLinus Torvalds 	snd_cards[card->number] = card;
927746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
92829b2625fSTakashi Iwai 	err = snd_info_card_register(card);
92929b2625fSTakashi Iwai 	if (err < 0)
93029b2625fSTakashi Iwai 		return err;
93129b2625fSTakashi Iwai 
9328eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
9331da177e4SLinus Torvalds 	if (snd_mixer_oss_notify_callback)
9341da177e4SLinus Torvalds 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
9351da177e4SLinus Torvalds #endif
9361da177e4SLinus Torvalds 	return 0;
9371da177e4SLinus Torvalds }
938c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_register);
939c0d3fb39STakashi Iwai 
940cd6a6503SJie Yang #ifdef CONFIG_SND_PROC_FS
snd_card_info_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)941a381a7a6STakashi Iwai static void snd_card_info_read(struct snd_info_entry *entry,
942a381a7a6STakashi Iwai 			       struct snd_info_buffer *buffer)
9431da177e4SLinus Torvalds {
9441da177e4SLinus Torvalds 	int idx, count;
945512bbd6aSTakashi Iwai 	struct snd_card *card;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
948746df948STakashi Iwai 		mutex_lock(&snd_card_mutex);
949e3ded899STakashi Iwai 		card = snd_cards[idx];
950e3ded899STakashi Iwai 		if (card) {
9511da177e4SLinus Torvalds 			count++;
952d001544dSClemens Ladisch 			snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
9531da177e4SLinus Torvalds 					idx,
9541da177e4SLinus Torvalds 					card->id,
9551da177e4SLinus Torvalds 					card->driver,
9561da177e4SLinus Torvalds 					card->shortname);
9571da177e4SLinus Torvalds 			snd_iprintf(buffer, "                      %s\n",
9581da177e4SLinus Torvalds 					card->longname);
9591da177e4SLinus Torvalds 		}
960746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 	if (!count)
9631da177e4SLinus Torvalds 		snd_iprintf(buffer, "--- no soundcards ---\n");
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
966e28563ccSTakashi Iwai #ifdef CONFIG_SND_OSSEMUL
snd_card_info_read_oss(struct snd_info_buffer * buffer)967512bbd6aSTakashi Iwai void snd_card_info_read_oss(struct snd_info_buffer *buffer)
9681da177e4SLinus Torvalds {
9691da177e4SLinus Torvalds 	int idx, count;
970512bbd6aSTakashi Iwai 	struct snd_card *card;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
973746df948STakashi Iwai 		mutex_lock(&snd_card_mutex);
974e3ded899STakashi Iwai 		card = snd_cards[idx];
975e3ded899STakashi Iwai 		if (card) {
9761da177e4SLinus Torvalds 			count++;
9771da177e4SLinus Torvalds 			snd_iprintf(buffer, "%s\n", card->longname);
9781da177e4SLinus Torvalds 		}
979746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 	if (!count) {
9821da177e4SLinus Torvalds 		snd_iprintf(buffer, "--- no soundcards ---\n");
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds #endif
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds #ifdef MODULE
snd_card_module_info_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)989512bbd6aSTakashi Iwai static void snd_card_module_info_read(struct snd_info_entry *entry,
990512bbd6aSTakashi Iwai 				      struct snd_info_buffer *buffer)
9911da177e4SLinus Torvalds {
9921da177e4SLinus Torvalds 	int idx;
993512bbd6aSTakashi Iwai 	struct snd_card *card;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	for (idx = 0; idx < SNDRV_CARDS; idx++) {
996746df948STakashi Iwai 		mutex_lock(&snd_card_mutex);
997e3ded899STakashi Iwai 		card = snd_cards[idx];
998e3ded899STakashi Iwai 		if (card)
999d001544dSClemens Ladisch 			snd_iprintf(buffer, "%2i %s\n",
1000d001544dSClemens Ladisch 				    idx, card->module->name);
1001746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds #endif
10051da177e4SLinus Torvalds 
snd_card_info_init(void)10061da177e4SLinus Torvalds int __init snd_card_info_init(void)
10071da177e4SLinus Torvalds {
1008512bbd6aSTakashi Iwai 	struct snd_info_entry *entry;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
10117c22f1aaSTakashi Iwai 	if (! entry)
10127c22f1aaSTakashi Iwai 		return -ENOMEM;
10131da177e4SLinus Torvalds 	entry->c.text.read = snd_card_info_read;
1014b591b6e9STakashi Iwai 	if (snd_info_register(entry) < 0)
1015b591b6e9STakashi Iwai 		return -ENOMEM; /* freed in error path */
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds #ifdef MODULE
10181da177e4SLinus Torvalds 	entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
1019b591b6e9STakashi Iwai 	if (!entry)
1020b591b6e9STakashi Iwai 		return -ENOMEM;
10211da177e4SLinus Torvalds 	entry->c.text.read = snd_card_module_info_read;
10221da177e4SLinus Torvalds 	if (snd_info_register(entry) < 0)
1023b591b6e9STakashi Iwai 		return -ENOMEM; /* freed in error path */
10241da177e4SLinus Torvalds #endif
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	return 0;
10271da177e4SLinus Torvalds }
1028cd6a6503SJie Yang #endif /* CONFIG_SND_PROC_FS */
1029e28563ccSTakashi Iwai 
10301da177e4SLinus Torvalds /**
10311da177e4SLinus Torvalds  *  snd_component_add - add a component string
10321da177e4SLinus Torvalds  *  @card: soundcard structure
10331da177e4SLinus Torvalds  *  @component: the component id string
10341da177e4SLinus Torvalds  *
10351da177e4SLinus Torvalds  *  This function adds the component id string to the supported list.
10361da177e4SLinus Torvalds  *  The component can be referred from the alsa-lib.
10371da177e4SLinus Torvalds  *
1038eb7c06e8SYacine Belkadi  *  Return: Zero otherwise a negative error code.
10391da177e4SLinus Torvalds  */
10401da177e4SLinus Torvalds 
snd_component_add(struct snd_card * card,const char * component)1041512bbd6aSTakashi Iwai int snd_component_add(struct snd_card *card, const char *component)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	char *ptr;
10441da177e4SLinus Torvalds 	int len = strlen(component);
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	ptr = strstr(card->components, component);
10471da177e4SLinus Torvalds 	if (ptr != NULL) {
10481da177e4SLinus Torvalds 		if (ptr[len] == '\0' || ptr[len] == ' ')	/* already there */
10491da177e4SLinus Torvalds 			return 1;
10501da177e4SLinus Torvalds 	}
10511da177e4SLinus Torvalds 	if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
10521da177e4SLinus Torvalds 		snd_BUG();
10531da177e4SLinus Torvalds 		return -ENOMEM;
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds 	if (card->components[0] != '\0')
10561da177e4SLinus Torvalds 		strcat(card->components, " ");
10571da177e4SLinus Torvalds 	strcat(card->components, component);
10581da177e4SLinus Torvalds 	return 0;
10591da177e4SLinus Torvalds }
1060c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_component_add);
1061c0d3fb39STakashi Iwai 
10621da177e4SLinus Torvalds /**
10631da177e4SLinus Torvalds  *  snd_card_file_add - add the file to the file list of the card
10641da177e4SLinus Torvalds  *  @card: soundcard structure
10651da177e4SLinus Torvalds  *  @file: file pointer
10661da177e4SLinus Torvalds  *
10671da177e4SLinus Torvalds  *  This function adds the file to the file linked-list of the card.
10681da177e4SLinus Torvalds  *  This linked-list is used to keep tracking the connection state,
10691da177e4SLinus Torvalds  *  and to avoid the release of busy resources by hotplug.
10701da177e4SLinus Torvalds  *
1071eb7c06e8SYacine Belkadi  *  Return: zero or a negative error code.
10721da177e4SLinus Torvalds  */
snd_card_file_add(struct snd_card * card,struct file * file)1073512bbd6aSTakashi Iwai int snd_card_file_add(struct snd_card *card, struct file *file)
10741da177e4SLinus Torvalds {
10751da177e4SLinus Torvalds 	struct snd_monitor_file *mfile;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
10781da177e4SLinus Torvalds 	if (mfile == NULL)
10791da177e4SLinus Torvalds 		return -ENOMEM;
10801da177e4SLinus Torvalds 	mfile->file = file;
1081a9edfc60SKarsten Wiese 	mfile->disconnected_f_op = NULL;
1082a45e3d6bSTakashi Iwai 	INIT_LIST_HEAD(&mfile->shutdown_list);
10831da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
10841da177e4SLinus Torvalds 	if (card->shutdown) {
10851da177e4SLinus Torvalds 		spin_unlock(&card->files_lock);
10861da177e4SLinus Torvalds 		kfree(mfile);
10871da177e4SLinus Torvalds 		return -ENODEV;
10881da177e4SLinus Torvalds 	}
1089118dd6bfSTakashi Iwai 	list_add(&mfile->list, &card->files_list);
1090f2464064STakashi Iwai 	get_device(&card->card_dev);
10911da177e4SLinus Torvalds 	spin_unlock(&card->files_lock);
10921da177e4SLinus Torvalds 	return 0;
10931da177e4SLinus Torvalds }
1094c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_file_add);
1095c0d3fb39STakashi Iwai 
10961da177e4SLinus Torvalds /**
10971da177e4SLinus Torvalds  *  snd_card_file_remove - remove the file from the file list
10981da177e4SLinus Torvalds  *  @card: soundcard structure
10991da177e4SLinus Torvalds  *  @file: file pointer
11001da177e4SLinus Torvalds  *
11011da177e4SLinus Torvalds  *  This function removes the file formerly added to the card via
11021da177e4SLinus Torvalds  *  snd_card_file_add() function.
11032b29b13cSTakashi Iwai  *  If all files are removed and snd_card_free_when_closed() was
11042b29b13cSTakashi Iwai  *  called beforehand, it processes the pending release of
11052b29b13cSTakashi Iwai  *  resources.
11061da177e4SLinus Torvalds  *
1107eb7c06e8SYacine Belkadi  *  Return: Zero or a negative error code.
11081da177e4SLinus Torvalds  */
snd_card_file_remove(struct snd_card * card,struct file * file)1109512bbd6aSTakashi Iwai int snd_card_file_remove(struct snd_card *card, struct file *file)
11101da177e4SLinus Torvalds {
1111118dd6bfSTakashi Iwai 	struct snd_monitor_file *mfile, *found = NULL;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
1114118dd6bfSTakashi Iwai 	list_for_each_entry(mfile, &card->files_list, list) {
11151da177e4SLinus Torvalds 		if (mfile->file == file) {
1116118dd6bfSTakashi Iwai 			list_del(&mfile->list);
1117a45e3d6bSTakashi Iwai 			spin_lock(&shutdown_lock);
1118a45e3d6bSTakashi Iwai 			list_del(&mfile->shutdown_list);
1119a45e3d6bSTakashi Iwai 			spin_unlock(&shutdown_lock);
1120118dd6bfSTakashi Iwai 			if (mfile->disconnected_f_op)
1121118dd6bfSTakashi Iwai 				fops_put(mfile->disconnected_f_op);
1122118dd6bfSTakashi Iwai 			found = mfile;
11231da177e4SLinus Torvalds 			break;
11241da177e4SLinus Torvalds 		}
11251da177e4SLinus Torvalds 	}
1126c44027c8STakashi Iwai 	if (list_empty(&card->files_list))
1127c44027c8STakashi Iwai 		wake_up_all(&card->remove_sleep);
1128c461482cSTakashi Iwai 	spin_unlock(&card->files_lock);
1129118dd6bfSTakashi Iwai 	if (!found) {
1130f2f9307aSTakashi Iwai 		dev_err(card->dev, "card file remove problem (%p)\n", file);
11311da177e4SLinus Torvalds 		return -ENOENT;
11321da177e4SLinus Torvalds 	}
1133118dd6bfSTakashi Iwai 	kfree(found);
1134f2464064STakashi Iwai 	put_device(&card->card_dev);
11351da177e4SLinus Torvalds 	return 0;
11361da177e4SLinus Torvalds }
1137c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_file_remove);
1138c0d3fb39STakashi Iwai 
11391da177e4SLinus Torvalds #ifdef CONFIG_PM
11401da177e4SLinus Torvalds /**
1141e94fdbd7STakashi Iwai  * snd_power_ref_and_wait - wait until the card gets powered up
11421da177e4SLinus Torvalds  * @card: soundcard structure
11431da177e4SLinus Torvalds  *
1144e94fdbd7STakashi Iwai  * Take the power_ref reference count of the given card, and
1145e94fdbd7STakashi Iwai  * wait until the card gets powered up to SNDRV_CTL_POWER_D0 state.
1146e94fdbd7STakashi Iwai  * The refcount is down again while sleeping until power-up, hence this
1147e94fdbd7STakashi Iwai  * function can be used for syncing the floating control ops accesses,
1148e94fdbd7STakashi Iwai  * typically around calling control ops.
1149e94fdbd7STakashi Iwai  *
1150e94fdbd7STakashi Iwai  * The caller needs to pull down the refcount via snd_power_unref() later
1151e94fdbd7STakashi Iwai  * no matter whether the error is returned from this function or not.
11521da177e4SLinus Torvalds  *
1153eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code.
11541da177e4SLinus Torvalds  */
snd_power_ref_and_wait(struct snd_card * card)1155e94fdbd7STakashi Iwai int snd_power_ref_and_wait(struct snd_card *card)
11561da177e4SLinus Torvalds {
1157e94fdbd7STakashi Iwai 	snd_power_ref(card);
1158e94fdbd7STakashi Iwai 	if (snd_power_get_state(card) == SNDRV_CTL_POWER_D0)
11591da177e4SLinus Torvalds 		return 0;
11608c0ae778STakashi Iwai 	wait_event_cmd(card->power_sleep,
11618c0ae778STakashi Iwai 		       card->shutdown ||
11628c0ae778STakashi Iwai 		       snd_power_get_state(card) == SNDRV_CTL_POWER_D0,
11638c0ae778STakashi Iwai 		       snd_power_unref(card), snd_power_ref(card));
11648c0ae778STakashi Iwai 	return card->shutdown ? -ENODEV : 0;
11651da177e4SLinus Torvalds }
1166e94fdbd7STakashi Iwai EXPORT_SYMBOL_GPL(snd_power_ref_and_wait);
1167e94fdbd7STakashi Iwai 
1168e94fdbd7STakashi Iwai /**
1169e94fdbd7STakashi Iwai  * snd_power_wait - wait until the card gets powered up (old form)
1170e94fdbd7STakashi Iwai  * @card: soundcard structure
1171e94fdbd7STakashi Iwai  *
1172e94fdbd7STakashi Iwai  * Wait until the card gets powered up to SNDRV_CTL_POWER_D0 state.
1173e94fdbd7STakashi Iwai  *
1174e94fdbd7STakashi Iwai  * Return: Zero if successful, or a negative error code.
1175e94fdbd7STakashi Iwai  */
snd_power_wait(struct snd_card * card)1176b6cc78daSTakashi Iwai int snd_power_wait(struct snd_card *card)
1177e94fdbd7STakashi Iwai {
1178e94fdbd7STakashi Iwai 	int ret;
1179e94fdbd7STakashi Iwai 
1180e94fdbd7STakashi Iwai 	ret = snd_power_ref_and_wait(card);
1181e94fdbd7STakashi Iwai 	snd_power_unref(card);
1182e94fdbd7STakashi Iwai 	return ret;
1183e94fdbd7STakashi Iwai }
1184c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_power_wait);
11851da177e4SLinus Torvalds #endif /* CONFIG_PM */
1186