xref: /openbmc/linux/sound/core/init.c (revision 97f44f56)
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>
31d052d1beSRussell King 
321da177e4SLinus Torvalds #include <sound/core.h>
331da177e4SLinus Torvalds #include <sound/control.h>
341da177e4SLinus Torvalds #include <sound/info.h>
351da177e4SLinus Torvalds 
3682a783f4STakashi Iwai /* monitor files for graceful shutdown (hotplug) */
3782a783f4STakashi Iwai struct snd_monitor_file {
3882a783f4STakashi Iwai 	struct file *file;
3982a783f4STakashi Iwai 	const struct file_operations *disconnected_f_op;
4082a783f4STakashi Iwai 	struct list_head shutdown_list;	/* still need to shutdown */
4182a783f4STakashi Iwai 	struct list_head list;	/* link of monitor files */
4282a783f4STakashi Iwai };
4382a783f4STakashi Iwai 
44a9edfc60SKarsten Wiese static DEFINE_SPINLOCK(shutdown_lock);
45a9edfc60SKarsten Wiese static LIST_HEAD(shutdown_files);
46a9edfc60SKarsten Wiese 
479c2e08c5SArjan van de Ven static const struct file_operations snd_shutdown_f_ops;
481da177e4SLinus Torvalds 
497bb2491bSTakashi Iwai /* locked for registering/using */
507bb2491bSTakashi Iwai static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS);
516581f4e7STakashi Iwai struct snd_card *snd_cards[SNDRV_CARDS];
52c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_cards);
53c0d3fb39STakashi Iwai 
54746df948STakashi Iwai static DEFINE_MUTEX(snd_card_mutex);
551da177e4SLinus Torvalds 
56304cd07fSTakashi Iwai static char *slots[SNDRV_CARDS];
57304cd07fSTakashi Iwai module_param_array(slots, charp, NULL, 0444);
58304cd07fSTakashi Iwai MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
59304cd07fSTakashi Iwai 
60a93bbaa7STakashi Iwai /* return non-zero if the given index is reserved for the given
61304cd07fSTakashi Iwai  * module via slots option
62304cd07fSTakashi Iwai  */
63a93bbaa7STakashi Iwai static int module_slot_match(struct module *module, int idx)
64304cd07fSTakashi Iwai {
65a93bbaa7STakashi Iwai 	int match = 1;
66304cd07fSTakashi Iwai #ifdef MODULE
67a93bbaa7STakashi Iwai 	const char *s1, *s2;
68a93bbaa7STakashi Iwai 
6960f6fef8STakashi Iwai 	if (!module || !*module->name || !slots[idx])
70304cd07fSTakashi Iwai 		return 0;
71a93bbaa7STakashi Iwai 
72a93bbaa7STakashi Iwai 	s1 = module->name;
73a93bbaa7STakashi Iwai 	s2 = slots[idx];
74a93bbaa7STakashi Iwai 	if (*s2 == '!') {
75a93bbaa7STakashi Iwai 		match = 0; /* negative match */
76a93bbaa7STakashi Iwai 		s2++;
77a93bbaa7STakashi Iwai 	}
78304cd07fSTakashi Iwai 	/* compare module name strings
79304cd07fSTakashi Iwai 	 * hyphens are handled as equivalent with underscore
80304cd07fSTakashi Iwai 	 */
81304cd07fSTakashi Iwai 	for (;;) {
82304cd07fSTakashi Iwai 		char c1 = *s1++;
83304cd07fSTakashi Iwai 		char c2 = *s2++;
84304cd07fSTakashi Iwai 		if (c1 == '-')
85304cd07fSTakashi Iwai 			c1 = '_';
86304cd07fSTakashi Iwai 		if (c2 == '-')
87304cd07fSTakashi Iwai 			c2 = '_';
88304cd07fSTakashi Iwai 		if (c1 != c2)
89a93bbaa7STakashi Iwai 			return !match;
90304cd07fSTakashi Iwai 		if (!c1)
91304cd07fSTakashi Iwai 			break;
92304cd07fSTakashi Iwai 	}
93a93bbaa7STakashi Iwai #endif /* MODULE */
94a93bbaa7STakashi Iwai 	return match;
95304cd07fSTakashi Iwai }
96304cd07fSTakashi Iwai 
971da177e4SLinus Torvalds #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
98512bbd6aSTakashi Iwai int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
99c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
1001da177e4SLinus Torvalds #endif
1011da177e4SLinus Torvalds 
102e28563ccSTakashi Iwai #ifdef CONFIG_PROC_FS
103512bbd6aSTakashi Iwai static void snd_card_id_read(struct snd_info_entry *entry,
104512bbd6aSTakashi Iwai 			     struct snd_info_buffer *buffer)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	snd_iprintf(buffer, "%s\n", entry->card->id);
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
109e28563ccSTakashi Iwai static inline int init_info_for_card(struct snd_card *card)
110e28563ccSTakashi Iwai {
111e28563ccSTakashi Iwai 	int err;
112e28563ccSTakashi Iwai 	struct snd_info_entry *entry;
113e28563ccSTakashi Iwai 
114e28563ccSTakashi Iwai 	if ((err = snd_info_card_register(card)) < 0) {
115e28563ccSTakashi Iwai 		snd_printd("unable to create card info\n");
116e28563ccSTakashi Iwai 		return err;
117e28563ccSTakashi Iwai 	}
118e28563ccSTakashi Iwai 	if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
119e28563ccSTakashi Iwai 		snd_printd("unable to create card entry\n");
120e28563ccSTakashi Iwai 		return err;
121e28563ccSTakashi Iwai 	}
122e28563ccSTakashi Iwai 	entry->c.text.read = snd_card_id_read;
123e28563ccSTakashi Iwai 	if (snd_info_register(entry) < 0) {
124e28563ccSTakashi Iwai 		snd_info_free_entry(entry);
125e28563ccSTakashi Iwai 		entry = NULL;
126e28563ccSTakashi Iwai 	}
127e28563ccSTakashi Iwai 	card->proc_id = entry;
128e28563ccSTakashi Iwai 	return 0;
129e28563ccSTakashi Iwai }
130e28563ccSTakashi Iwai #else /* !CONFIG_PROC_FS */
131e28563ccSTakashi Iwai #define init_info_for_card(card)
132e28563ccSTakashi Iwai #endif
133e28563ccSTakashi Iwai 
1341da177e4SLinus Torvalds /**
13553fb1e63STakashi Iwai  *  snd_card_create - create and initialize a soundcard structure
1361da177e4SLinus Torvalds  *  @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
1371da177e4SLinus Torvalds  *  @xid: card identification (ASCII string)
1381da177e4SLinus Torvalds  *  @module: top level module for locking
1391da177e4SLinus Torvalds  *  @extra_size: allocate this extra size after the main soundcard structure
14053fb1e63STakashi Iwai  *  @card_ret: the pointer to store the created card instance
1411da177e4SLinus Torvalds  *
1421da177e4SLinus Torvalds  *  Creates and initializes a soundcard structure.
1431da177e4SLinus Torvalds  *
14453fb1e63STakashi Iwai  *  The function allocates snd_card instance via kzalloc with the given
14553fb1e63STakashi Iwai  *  space for the driver to use freely.  The allocated struct is stored
14653fb1e63STakashi Iwai  *  in the given card_ret pointer.
14753fb1e63STakashi Iwai  *
148eb7c06e8SYacine Belkadi  *  Return: Zero if successful or a negative error code.
1491da177e4SLinus Torvalds  */
15053fb1e63STakashi Iwai int snd_card_create(int idx, const char *xid,
15153fb1e63STakashi Iwai 		    struct module *module, int extra_size,
15253fb1e63STakashi Iwai 		    struct snd_card **card_ret)
1531da177e4SLinus Torvalds {
154512bbd6aSTakashi Iwai 	struct snd_card *card;
155a93bbaa7STakashi Iwai 	int err, idx2;
1561da177e4SLinus Torvalds 
15753fb1e63STakashi Iwai 	if (snd_BUG_ON(!card_ret))
15853fb1e63STakashi Iwai 		return -EINVAL;
15953fb1e63STakashi Iwai 	*card_ret = NULL;
16053fb1e63STakashi Iwai 
1611da177e4SLinus Torvalds 	if (extra_size < 0)
1621da177e4SLinus Torvalds 		extra_size = 0;
163ca2c0966STakashi Iwai 	card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
16453fb1e63STakashi Iwai 	if (!card)
16553fb1e63STakashi Iwai 		return -ENOMEM;
16610a8ebbbSJaroslav Kysela 	if (xid)
1675fdc18d9SJaroslav Kysela 		strlcpy(card->id, xid, sizeof(card->id));
1681da177e4SLinus Torvalds 	err = 0;
169746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
1701da177e4SLinus Torvalds 	if (idx < 0) {
1717bb2491bSTakashi Iwai 		for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) {
1725c33dd70SOliver Neukum 			/* idx == -1 == 0xffff means: take any free slot */
1737bb2491bSTakashi Iwai 			if (idx2 < sizeof(int) && !(idx & (1U << idx2)))
1747bb2491bSTakashi Iwai 				continue;
1757bb2491bSTakashi Iwai 			if (!test_bit(idx2, snd_cards_lock)) {
176a93bbaa7STakashi Iwai 				if (module_slot_match(module, idx2)) {
1771da177e4SLinus Torvalds 					idx = idx2;
1781da177e4SLinus Torvalds 					break;
1791da177e4SLinus Torvalds 				}
180a93bbaa7STakashi Iwai 			}
181a93bbaa7STakashi Iwai 		}
1827bb2491bSTakashi Iwai 	}
183a93bbaa7STakashi Iwai 	if (idx < 0) {
1847bb2491bSTakashi Iwai 		for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) {
185a93bbaa7STakashi Iwai 			/* idx == -1 == 0xffff means: take any free slot */
1867bb2491bSTakashi Iwai 			if (idx2 < sizeof(int) && !(idx & (1U << idx2)))
1877bb2491bSTakashi Iwai 				continue;
1887bb2491bSTakashi Iwai 			if (!test_bit(idx2, snd_cards_lock)) {
189a93bbaa7STakashi Iwai 				if (!slots[idx2] || !*slots[idx2]) {
190a93bbaa7STakashi Iwai 					idx = idx2;
191a93bbaa7STakashi Iwai 					break;
192a93bbaa7STakashi Iwai 				}
193a93bbaa7STakashi Iwai 			}
194a93bbaa7STakashi Iwai 		}
1957bb2491bSTakashi Iwai 	}
196a93bbaa7STakashi Iwai 	if (idx < 0)
197a93bbaa7STakashi Iwai 		err = -ENODEV;
198a93bbaa7STakashi Iwai 	else if (idx < snd_ecards_limit) {
1997bb2491bSTakashi Iwai 		if (test_bit(idx, snd_cards_lock))
2005c33dd70SOliver Neukum 			err = -EBUSY;	/* invalid */
201a93bbaa7STakashi Iwai 	} else if (idx >= SNDRV_CARDS)
2021da177e4SLinus Torvalds 		err = -ENODEV;
203a93bbaa7STakashi Iwai 	if (err < 0) {
204746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
2055c33dd70SOliver Neukum 		snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i), error: %d\n",
2065c33dd70SOliver Neukum 			 idx, snd_ecards_limit - 1, err);
2071da177e4SLinus Torvalds 		goto __error;
2081da177e4SLinus Torvalds 	}
2097bb2491bSTakashi Iwai 	set_bit(idx, snd_cards_lock);		/* lock it */
210a93bbaa7STakashi Iwai 	if (idx >= snd_ecards_limit)
211a93bbaa7STakashi Iwai 		snd_ecards_limit = idx + 1; /* increase the limit */
212746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
2131da177e4SLinus Torvalds 	card->number = idx;
2141da177e4SLinus Torvalds 	card->module = module;
2151da177e4SLinus Torvalds 	INIT_LIST_HEAD(&card->devices);
2161da177e4SLinus Torvalds 	init_rwsem(&card->controls_rwsem);
2171da177e4SLinus Torvalds 	rwlock_init(&card->ctl_files_rwlock);
2181da177e4SLinus Torvalds 	INIT_LIST_HEAD(&card->controls);
2191da177e4SLinus Torvalds 	INIT_LIST_HEAD(&card->ctl_files);
2201da177e4SLinus Torvalds 	spin_lock_init(&card->files_lock);
221118dd6bfSTakashi Iwai 	INIT_LIST_HEAD(&card->files_list);
2221da177e4SLinus Torvalds 	init_waitqueue_head(&card->shutdown_sleep);
223a0830dbdSTakashi Iwai 	atomic_set(&card->refcount, 0);
2241da177e4SLinus Torvalds #ifdef CONFIG_PM
2251a60d4c5SIngo Molnar 	mutex_init(&card->power_lock);
2261da177e4SLinus Torvalds 	init_waitqueue_head(&card->power_sleep);
2271da177e4SLinus Torvalds #endif
2281da177e4SLinus Torvalds 	/* the control interface cannot be accessed from the user space until */
2291da177e4SLinus Torvalds 	/* snd_cards_bitmask and snd_cards are set with snd_card_register */
23053fb1e63STakashi Iwai 	err = snd_ctl_create(card);
23153fb1e63STakashi Iwai 	if (err < 0) {
23253fb1e63STakashi Iwai 		snd_printk(KERN_ERR "unable to register control minors\n");
2331da177e4SLinus Torvalds 		goto __error;
2341da177e4SLinus Torvalds 	}
23553fb1e63STakashi Iwai 	err = snd_info_card_create(card);
23653fb1e63STakashi Iwai 	if (err < 0) {
23753fb1e63STakashi Iwai 		snd_printk(KERN_ERR "unable to create card info\n");
2381da177e4SLinus Torvalds 		goto __error_ctl;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	if (extra_size > 0)
241512bbd6aSTakashi Iwai 		card->private_data = (char *)card + sizeof(struct snd_card);
24253fb1e63STakashi Iwai 	*card_ret = card;
24353fb1e63STakashi Iwai 	return 0;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds       __error_ctl:
2461da177e4SLinus Torvalds 	snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
2471da177e4SLinus Torvalds       __error:
2481da177e4SLinus Torvalds 	kfree(card);
24953fb1e63STakashi Iwai   	return err;
2501da177e4SLinus Torvalds }
25153fb1e63STakashi Iwai EXPORT_SYMBOL(snd_card_create);
252c0d3fb39STakashi Iwai 
253746df948STakashi Iwai /* return non-zero if a card is already locked */
254746df948STakashi Iwai int snd_card_locked(int card)
255746df948STakashi Iwai {
256746df948STakashi Iwai 	int locked;
257746df948STakashi Iwai 
258746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
2597bb2491bSTakashi Iwai 	locked = test_bit(card, snd_cards_lock);
260746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
261746df948STakashi Iwai 	return locked;
262746df948STakashi Iwai }
263746df948STakashi Iwai 
264b3b0abe1SClemens Ladisch static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
265b3b0abe1SClemens Ladisch {
266b3b0abe1SClemens Ladisch 	return -ENODEV;
267b3b0abe1SClemens Ladisch }
268b3b0abe1SClemens Ladisch 
269b3b0abe1SClemens Ladisch static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
270b3b0abe1SClemens Ladisch 				   size_t count, loff_t *offset)
271b3b0abe1SClemens Ladisch {
272b3b0abe1SClemens Ladisch 	return -ENODEV;
273b3b0abe1SClemens Ladisch }
274b3b0abe1SClemens Ladisch 
275b3b0abe1SClemens Ladisch static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
276b3b0abe1SClemens Ladisch 				    size_t count, loff_t *offset)
277b3b0abe1SClemens Ladisch {
278b3b0abe1SClemens Ladisch 	return -ENODEV;
279b3b0abe1SClemens Ladisch }
280b3b0abe1SClemens Ladisch 
281a9edfc60SKarsten Wiese static int snd_disconnect_release(struct inode *inode, struct file *file)
282a9edfc60SKarsten Wiese {
283a9edfc60SKarsten Wiese 	struct snd_monitor_file *df = NULL, *_df;
284a9edfc60SKarsten Wiese 
285a9edfc60SKarsten Wiese 	spin_lock(&shutdown_lock);
286a9edfc60SKarsten Wiese 	list_for_each_entry(_df, &shutdown_files, shutdown_list) {
287a9edfc60SKarsten Wiese 		if (_df->file == file) {
288a9edfc60SKarsten Wiese 			df = _df;
289118dd6bfSTakashi Iwai 			list_del_init(&df->shutdown_list);
290a9edfc60SKarsten Wiese 			break;
291a9edfc60SKarsten Wiese 		}
292a9edfc60SKarsten Wiese 	}
293a9edfc60SKarsten Wiese 	spin_unlock(&shutdown_lock);
294a9edfc60SKarsten Wiese 
295233e70f4SAl Viro 	if (likely(df)) {
296233e70f4SAl Viro 		if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync)
297233e70f4SAl Viro 			df->disconnected_f_op->fasync(-1, file, 0);
298a9edfc60SKarsten Wiese 		return df->disconnected_f_op->release(inode, file);
299233e70f4SAl Viro 	}
300a9edfc60SKarsten Wiese 
3019bf8e7ddSHarvey Harrison 	panic("%s(%p, %p) failed!", __func__, inode, file);
302a9edfc60SKarsten Wiese }
303a9edfc60SKarsten Wiese 
3041da177e4SLinus Torvalds static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	return POLLERR | POLLNVAL;
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
309b3b0abe1SClemens Ladisch static long snd_disconnect_ioctl(struct file *file,
310b3b0abe1SClemens Ladisch 				 unsigned int cmd, unsigned long arg)
311b3b0abe1SClemens Ladisch {
312b3b0abe1SClemens Ladisch 	return -ENODEV;
313b3b0abe1SClemens Ladisch }
314b3b0abe1SClemens Ladisch 
315b3b0abe1SClemens Ladisch static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
316b3b0abe1SClemens Ladisch {
317b3b0abe1SClemens Ladisch 	return -ENODEV;
318b3b0abe1SClemens Ladisch }
319b3b0abe1SClemens Ladisch 
320b3b0abe1SClemens Ladisch static int snd_disconnect_fasync(int fd, struct file *file, int on)
321b3b0abe1SClemens Ladisch {
322b3b0abe1SClemens Ladisch 	return -ENODEV;
323b3b0abe1SClemens Ladisch }
324b3b0abe1SClemens Ladisch 
3259c2e08c5SArjan van de Ven static const struct file_operations snd_shutdown_f_ops =
326a9edfc60SKarsten Wiese {
327a9edfc60SKarsten Wiese 	.owner = 	THIS_MODULE,
328a9edfc60SKarsten Wiese 	.llseek =	snd_disconnect_llseek,
329a9edfc60SKarsten Wiese 	.read = 	snd_disconnect_read,
330a9edfc60SKarsten Wiese 	.write =	snd_disconnect_write,
331a9edfc60SKarsten Wiese 	.release =	snd_disconnect_release,
332a9edfc60SKarsten Wiese 	.poll =		snd_disconnect_poll,
333a9edfc60SKarsten Wiese 	.unlocked_ioctl = snd_disconnect_ioctl,
334a9edfc60SKarsten Wiese #ifdef CONFIG_COMPAT
335a9edfc60SKarsten Wiese 	.compat_ioctl = snd_disconnect_ioctl,
336a9edfc60SKarsten Wiese #endif
337a9edfc60SKarsten Wiese 	.mmap =		snd_disconnect_mmap,
338a9edfc60SKarsten Wiese 	.fasync =	snd_disconnect_fasync
339a9edfc60SKarsten Wiese };
340a9edfc60SKarsten Wiese 
3411da177e4SLinus Torvalds /**
3421da177e4SLinus Torvalds  *  snd_card_disconnect - disconnect all APIs from the file-operations (user space)
3431da177e4SLinus Torvalds  *  @card: soundcard structure
3441da177e4SLinus Torvalds  *
3451da177e4SLinus Torvalds  *  Disconnects all APIs from the file-operations (user space).
3461da177e4SLinus Torvalds  *
347eb7c06e8SYacine Belkadi  *  Return: Zero, otherwise a negative error code.
3481da177e4SLinus Torvalds  *
3491da177e4SLinus Torvalds  *  Note: The current implementation replaces all active file->f_op with special
3501da177e4SLinus Torvalds  *        dummy file operations (they do nothing except release).
3511da177e4SLinus Torvalds  */
352512bbd6aSTakashi Iwai int snd_card_disconnect(struct snd_card *card)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	struct snd_monitor_file *mfile;
3551da177e4SLinus Torvalds 	int err;
3561da177e4SLinus Torvalds 
357f18638dcSTakashi Iwai 	if (!card)
358f18638dcSTakashi Iwai 		return -EINVAL;
359f18638dcSTakashi Iwai 
3601da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
3611da177e4SLinus Torvalds 	if (card->shutdown) {
3621da177e4SLinus Torvalds 		spin_unlock(&card->files_lock);
3631da177e4SLinus Torvalds 		return 0;
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 	card->shutdown = 1;
3661da177e4SLinus Torvalds 	spin_unlock(&card->files_lock);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/* phase 1: disable fops (user space) operations for ALSA API */
369746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
3701da177e4SLinus Torvalds 	snd_cards[card->number] = NULL;
3717bb2491bSTakashi Iwai 	clear_bit(card->number, snd_cards_lock);
372746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	/* phase 2: replace file->f_op with special dummy operations */
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
377118dd6bfSTakashi Iwai 	list_for_each_entry(mfile, &card->files_list, list) {
3781da177e4SLinus Torvalds 		/* it's critical part, use endless loop */
3791da177e4SLinus Torvalds 		/* we have no room to fail */
380a9edfc60SKarsten Wiese 		mfile->disconnected_f_op = mfile->file->f_op;
3811da177e4SLinus Torvalds 
382a9edfc60SKarsten Wiese 		spin_lock(&shutdown_lock);
383a9edfc60SKarsten Wiese 		list_add(&mfile->shutdown_list, &shutdown_files);
384a9edfc60SKarsten Wiese 		spin_unlock(&shutdown_lock);
3851da177e4SLinus Torvalds 
386a9edfc60SKarsten Wiese 		mfile->file->f_op = &snd_shutdown_f_ops;
387bc9abce0SMiguel Boton 		fops_get(mfile->file->f_op);
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 	spin_unlock(&card->files_lock);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	/* phase 3: notify all connected devices about disconnection */
3921da177e4SLinus Torvalds 	/* at this point, they cannot respond to any calls except release() */
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
3951da177e4SLinus Torvalds 	if (snd_mixer_oss_notify_callback)
3961da177e4SLinus Torvalds 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
3971da177e4SLinus Torvalds #endif
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	/* notify all devices that we are disconnected */
4001da177e4SLinus Torvalds 	err = snd_device_disconnect_all(card);
4011da177e4SLinus Torvalds 	if (err < 0)
4021da177e4SLinus Torvalds 		snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
4031da177e4SLinus Torvalds 
404746d4a02STakashi Iwai 	snd_info_card_disconnect(card);
40573d38b13STakashi Iwai 	if (card->card_dev) {
40673d38b13STakashi Iwai 		device_unregister(card->card_dev);
40773d38b13STakashi Iwai 		card->card_dev = NULL;
40873d38b13STakashi Iwai 	}
409f18638dcSTakashi Iwai #ifdef CONFIG_PM
410f18638dcSTakashi Iwai 	wake_up(&card->power_sleep);
411f18638dcSTakashi Iwai #endif
4121da177e4SLinus Torvalds 	return 0;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
415c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_disconnect);
416c0d3fb39STakashi Iwai 
4171da177e4SLinus Torvalds /**
4181da177e4SLinus Torvalds  *  snd_card_free - frees given soundcard structure
4191da177e4SLinus Torvalds  *  @card: soundcard structure
4201da177e4SLinus Torvalds  *
4211da177e4SLinus Torvalds  *  This function releases the soundcard structure and the all assigned
4221da177e4SLinus Torvalds  *  devices automatically.  That is, you don't have to release the devices
4231da177e4SLinus Torvalds  *  by yourself.
4241da177e4SLinus Torvalds  *
425eb7c06e8SYacine Belkadi  *  Return: Zero. Frees all associated devices and frees the control
4261da177e4SLinus Torvalds  *  interface associated to given soundcard.
4271da177e4SLinus Torvalds  */
428c461482cSTakashi Iwai static int snd_card_do_free(struct snd_card *card)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
4311da177e4SLinus Torvalds 	if (snd_mixer_oss_notify_callback)
4321da177e4SLinus Torvalds 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
4331da177e4SLinus Torvalds #endif
4341da177e4SLinus Torvalds 	if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
4351da177e4SLinus Torvalds 		snd_printk(KERN_ERR "unable to free all devices (pre)\n");
4361da177e4SLinus Torvalds 		/* Fatal, but this situation should never occur */
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 	if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
4391da177e4SLinus Torvalds 		snd_printk(KERN_ERR "unable to free all devices (normal)\n");
4401da177e4SLinus Torvalds 		/* Fatal, but this situation should never occur */
4411da177e4SLinus Torvalds 	}
4421da177e4SLinus Torvalds 	if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
4431da177e4SLinus Torvalds 		snd_printk(KERN_ERR "unable to free all devices (post)\n");
4441da177e4SLinus Torvalds 		/* Fatal, but this situation should never occur */
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 	if (card->private_free)
4471da177e4SLinus Torvalds 		card->private_free(card);
448746d4a02STakashi Iwai 	snd_info_free_entry(card->proc_id);
4491da177e4SLinus Torvalds 	if (snd_info_card_free(card) < 0) {
4501da177e4SLinus Torvalds 		snd_printk(KERN_WARNING "unable to free card info\n");
4511da177e4SLinus Torvalds 		/* Not fatal error */
4521da177e4SLinus Torvalds 	}
453c461482cSTakashi Iwai 	kfree(card);
454c461482cSTakashi Iwai 	return 0;
455c461482cSTakashi Iwai }
456c461482cSTakashi Iwai 
457a0830dbdSTakashi Iwai /**
458a0830dbdSTakashi Iwai  * snd_card_unref - release the reference counter
459a0830dbdSTakashi Iwai  * @card: the card instance
460a0830dbdSTakashi Iwai  *
461a0830dbdSTakashi Iwai  * Decrements the reference counter.  When it reaches to zero, wake up
462a0830dbdSTakashi Iwai  * the sleeper and call the destructor if needed.
463a0830dbdSTakashi Iwai  */
464a0830dbdSTakashi Iwai void snd_card_unref(struct snd_card *card)
465a0830dbdSTakashi Iwai {
466a0830dbdSTakashi Iwai 	if (atomic_dec_and_test(&card->refcount)) {
467a0830dbdSTakashi Iwai 		wake_up(&card->shutdown_sleep);
468a0830dbdSTakashi Iwai 		if (card->free_on_last_close)
469a0830dbdSTakashi Iwai 			snd_card_do_free(card);
470a0830dbdSTakashi Iwai 	}
471a0830dbdSTakashi Iwai }
472a0830dbdSTakashi Iwai EXPORT_SYMBOL(snd_card_unref);
473a0830dbdSTakashi Iwai 
474c461482cSTakashi Iwai int snd_card_free_when_closed(struct snd_card *card)
475c461482cSTakashi Iwai {
476a0830dbdSTakashi Iwai 	int ret;
477a0830dbdSTakashi Iwai 
478a0830dbdSTakashi Iwai 	atomic_inc(&card->refcount);
479a0830dbdSTakashi Iwai 	ret = snd_card_disconnect(card);
480a0830dbdSTakashi Iwai 	if (ret) {
481a0830dbdSTakashi Iwai 		atomic_dec(&card->refcount);
482c461482cSTakashi Iwai 		return ret;
483a0830dbdSTakashi Iwai 	}
484c461482cSTakashi Iwai 
485c461482cSTakashi Iwai 	card->free_on_last_close = 1;
486a0830dbdSTakashi Iwai 	if (atomic_dec_and_test(&card->refcount))
487c461482cSTakashi Iwai 		snd_card_do_free(card);
488c461482cSTakashi Iwai 	return 0;
489c461482cSTakashi Iwai }
490c461482cSTakashi Iwai 
491c461482cSTakashi Iwai EXPORT_SYMBOL(snd_card_free_when_closed);
492c461482cSTakashi Iwai 
493c461482cSTakashi Iwai int snd_card_free(struct snd_card *card)
494c461482cSTakashi Iwai {
495f18638dcSTakashi Iwai 	int ret = snd_card_disconnect(card);
496c461482cSTakashi Iwai 	if (ret)
497c461482cSTakashi Iwai 		return ret;
498c461482cSTakashi Iwai 
499c461482cSTakashi Iwai 	/* wait, until all devices are ready for the free operation */
500a0830dbdSTakashi Iwai 	wait_event(card->shutdown_sleep, !atomic_read(&card->refcount));
501c461482cSTakashi Iwai 	snd_card_do_free(card);
5021da177e4SLinus Torvalds 	return 0;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
505c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_free);
506c0d3fb39STakashi Iwai 
507e7df2a3aSTakashi Iwai /* retrieve the last word of shortname or longname */
508e7df2a3aSTakashi Iwai static const char *retrieve_id_from_card_name(const char *name)
5091da177e4SLinus Torvalds {
510e7df2a3aSTakashi Iwai 	const char *spos = name;
511e7df2a3aSTakashi Iwai 
512e7df2a3aSTakashi Iwai 	while (*name) {
513e7df2a3aSTakashi Iwai 		if (isspace(*name) && isalnum(name[1]))
514e7df2a3aSTakashi Iwai 			spos = name + 1;
515e7df2a3aSTakashi Iwai 		name++;
516e7df2a3aSTakashi Iwai 	}
517e7df2a3aSTakashi Iwai 	return spos;
518e7df2a3aSTakashi Iwai }
519e7df2a3aSTakashi Iwai 
520e7df2a3aSTakashi Iwai /* return true if the given id string doesn't conflict any other card ids */
521e7df2a3aSTakashi Iwai static bool card_id_ok(struct snd_card *card, const char *id)
522e7df2a3aSTakashi Iwai {
523e7df2a3aSTakashi Iwai 	int i;
524e7df2a3aSTakashi Iwai 	if (!snd_info_check_reserved_words(id))
525e7df2a3aSTakashi Iwai 		return false;
526e7df2a3aSTakashi Iwai 	for (i = 0; i < snd_ecards_limit; i++) {
527e7df2a3aSTakashi Iwai 		if (snd_cards[i] && snd_cards[i] != card &&
528e7df2a3aSTakashi Iwai 		    !strcmp(snd_cards[i]->id, id))
529e7df2a3aSTakashi Iwai 			return false;
530e7df2a3aSTakashi Iwai 	}
531e7df2a3aSTakashi Iwai 	return true;
532e7df2a3aSTakashi Iwai }
533e7df2a3aSTakashi Iwai 
534e7df2a3aSTakashi Iwai /* copy to card->id only with valid letters from nid */
535e7df2a3aSTakashi Iwai static void copy_valid_id_string(struct snd_card *card, const char *src,
536e7df2a3aSTakashi Iwai 				 const char *nid)
537e7df2a3aSTakashi Iwai {
538e7df2a3aSTakashi Iwai 	char *id = card->id;
539e7df2a3aSTakashi Iwai 
540e7df2a3aSTakashi Iwai 	while (*nid && !isalnum(*nid))
541e7df2a3aSTakashi Iwai 		nid++;
542e7df2a3aSTakashi Iwai 	if (isdigit(*nid))
543e7df2a3aSTakashi Iwai 		*id++ = isalpha(*src) ? *src : 'D';
544e7df2a3aSTakashi Iwai 	while (*nid && (size_t)(id - card->id) < sizeof(card->id) - 1) {
545e7df2a3aSTakashi Iwai 		if (isalnum(*nid))
546e7df2a3aSTakashi Iwai 			*id++ = *nid;
547e7df2a3aSTakashi Iwai 		nid++;
548e7df2a3aSTakashi Iwai 	}
549e7df2a3aSTakashi Iwai 	*id = 0;
550e7df2a3aSTakashi Iwai }
551e7df2a3aSTakashi Iwai 
552e7df2a3aSTakashi Iwai /* Set card->id from the given string
553e7df2a3aSTakashi Iwai  * If the string conflicts with other ids, add a suffix to make it unique.
554e7df2a3aSTakashi Iwai  */
555e7df2a3aSTakashi Iwai static void snd_card_set_id_no_lock(struct snd_card *card, const char *src,
556e7df2a3aSTakashi Iwai 				    const char *nid)
557e7df2a3aSTakashi Iwai {
558e7df2a3aSTakashi Iwai 	int len, loops;
559e7df2a3aSTakashi Iwai 	bool is_default = false;
56010a8ebbbSJaroslav Kysela 	char *id;
5611da177e4SLinus Torvalds 
562e7df2a3aSTakashi Iwai 	copy_valid_id_string(card, src, nid);
5631da177e4SLinus Torvalds 	id = card->id;
5641da177e4SLinus Torvalds 
565e7df2a3aSTakashi Iwai  again:
566e7df2a3aSTakashi Iwai 	/* use "Default" for obviously invalid strings
567e7df2a3aSTakashi Iwai 	 * ("card" conflicts with proc directories)
568e7df2a3aSTakashi Iwai 	 */
569e7df2a3aSTakashi Iwai 	if (!*id || !strncmp(id, "card", 4)) {
570d8e4f9aeSTakashi Iwai 		strcpy(id, "Default");
571e7df2a3aSTakashi Iwai 		is_default = true;
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 
5748edbb198STakashi Iwai 	len = strlen(id);
575e7df2a3aSTakashi Iwai 	for (loops = 0; loops < SNDRV_CARDS; loops++) {
5768edbb198STakashi Iwai 		char *spos;
5778edbb198STakashi Iwai 		char sfxstr[5]; /* "_012" */
5788edbb198STakashi Iwai 		int sfxlen;
5798edbb198STakashi Iwai 
580e7df2a3aSTakashi Iwai 		if (card_id_ok(card, id))
581e7df2a3aSTakashi Iwai 			return; /* OK */
582e7df2a3aSTakashi Iwai 
5838edbb198STakashi Iwai 		/* Add _XYZ suffix */
5848edbb198STakashi Iwai 		sprintf(sfxstr, "_%X", loops + 1);
5858edbb198STakashi Iwai 		sfxlen = strlen(sfxstr);
5868edbb198STakashi Iwai 		if (len + sfxlen >= sizeof(card->id))
5878edbb198STakashi Iwai 			spos = id + sizeof(card->id) - sfxlen - 1;
588d001544dSClemens Ladisch 		else
5898edbb198STakashi Iwai 			spos = id + len;
5908edbb198STakashi Iwai 		strcpy(spos, sfxstr);
5911da177e4SLinus Torvalds 	}
592e7df2a3aSTakashi Iwai 	/* fallback to the default id */
593e7df2a3aSTakashi Iwai 	if (!is_default) {
594e7df2a3aSTakashi Iwai 		*id = 0;
595e7df2a3aSTakashi Iwai 		goto again;
596e7df2a3aSTakashi Iwai 	}
597e7df2a3aSTakashi Iwai 	/* last resort... */
598e7df2a3aSTakashi Iwai 	snd_printk(KERN_ERR "unable to set card id (%s)\n", id);
599e7df2a3aSTakashi Iwai 	if (card->proc_root->name)
60097f44f56STakashi Iwai 		strlcpy(card->id, card->proc_root->name, sizeof(card->id));
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
603872c7820SMark Brown /**
604872c7820SMark Brown  *  snd_card_set_id - set card identification name
605872c7820SMark Brown  *  @card: soundcard structure
606872c7820SMark Brown  *  @nid: new identification string
607872c7820SMark Brown  *
608872c7820SMark Brown  *  This function sets the card identification and checks for name
609872c7820SMark Brown  *  collisions.
610872c7820SMark Brown  */
611872c7820SMark Brown void snd_card_set_id(struct snd_card *card, const char *nid)
612872c7820SMark Brown {
6135fdc18d9SJaroslav Kysela 	/* check if user specified own card->id */
6145fdc18d9SJaroslav Kysela 	if (card->id[0] != '\0')
6155fdc18d9SJaroslav Kysela 		return;
6165fdc18d9SJaroslav Kysela 	mutex_lock(&snd_card_mutex);
617e7df2a3aSTakashi Iwai 	snd_card_set_id_no_lock(card, nid, nid);
6185fdc18d9SJaroslav Kysela 	mutex_unlock(&snd_card_mutex);
619872c7820SMark Brown }
62010a8ebbbSJaroslav Kysela EXPORT_SYMBOL(snd_card_set_id);
62110a8ebbbSJaroslav Kysela 
6229fb6198eSJaroslav Kysela static ssize_t
6239fb6198eSJaroslav Kysela card_id_show_attr(struct device *dev,
6249fb6198eSJaroslav Kysela 		  struct device_attribute *attr, char *buf)
6259fb6198eSJaroslav Kysela {
6269fb6198eSJaroslav Kysela 	struct snd_card *card = dev_get_drvdata(dev);
6279fb6198eSJaroslav Kysela 	return snprintf(buf, PAGE_SIZE, "%s\n", card ? card->id : "(null)");
6289fb6198eSJaroslav Kysela }
6299fb6198eSJaroslav Kysela 
6309fb6198eSJaroslav Kysela static ssize_t
6319fb6198eSJaroslav Kysela card_id_store_attr(struct device *dev, struct device_attribute *attr,
6329fb6198eSJaroslav Kysela 		   const char *buf, size_t count)
6339fb6198eSJaroslav Kysela {
6349fb6198eSJaroslav Kysela 	struct snd_card *card = dev_get_drvdata(dev);
6359fb6198eSJaroslav Kysela 	char buf1[sizeof(card->id)];
6369fb6198eSJaroslav Kysela 	size_t copy = count > sizeof(card->id) - 1 ?
6379fb6198eSJaroslav Kysela 					sizeof(card->id) - 1 : count;
6389fb6198eSJaroslav Kysela 	size_t idx;
6399fb6198eSJaroslav Kysela 	int c;
6409fb6198eSJaroslav Kysela 
6419fb6198eSJaroslav Kysela 	for (idx = 0; idx < copy; idx++) {
6429fb6198eSJaroslav Kysela 		c = buf[idx];
6439fb6198eSJaroslav Kysela 		if (!isalnum(c) && c != '_' && c != '-')
6449fb6198eSJaroslav Kysela 			return -EINVAL;
6459fb6198eSJaroslav Kysela 	}
6469fb6198eSJaroslav Kysela 	memcpy(buf1, buf, copy);
6479fb6198eSJaroslav Kysela 	buf1[copy] = '\0';
6489fb6198eSJaroslav Kysela 	mutex_lock(&snd_card_mutex);
649e7df2a3aSTakashi Iwai 	if (!card_id_ok(NULL, buf1)) {
6509fb6198eSJaroslav Kysela 		mutex_unlock(&snd_card_mutex);
6519fb6198eSJaroslav Kysela 		return -EEXIST;
6529fb6198eSJaroslav Kysela 	}
6539fb6198eSJaroslav Kysela 	strcpy(card->id, buf1);
654c2eb9c4eSJaroslav Kysela 	snd_info_card_id_change(card);
6559fb6198eSJaroslav Kysela 	mutex_unlock(&snd_card_mutex);
6569fb6198eSJaroslav Kysela 
6579fb6198eSJaroslav Kysela 	return count;
6589fb6198eSJaroslav Kysela }
6599fb6198eSJaroslav Kysela 
6609fb6198eSJaroslav Kysela static struct device_attribute card_id_attrs =
6619fb6198eSJaroslav Kysela 	__ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr);
6629fb6198eSJaroslav Kysela 
6639fb6198eSJaroslav Kysela static ssize_t
6649fb6198eSJaroslav Kysela card_number_show_attr(struct device *dev,
6659fb6198eSJaroslav Kysela 		     struct device_attribute *attr, char *buf)
6669fb6198eSJaroslav Kysela {
6679fb6198eSJaroslav Kysela 	struct snd_card *card = dev_get_drvdata(dev);
6689fb6198eSJaroslav Kysela 	return snprintf(buf, PAGE_SIZE, "%i\n", card ? card->number : -1);
6699fb6198eSJaroslav Kysela }
6709fb6198eSJaroslav Kysela 
6719fb6198eSJaroslav Kysela static struct device_attribute card_number_attrs =
6729fb6198eSJaroslav Kysela 	__ATTR(number, S_IRUGO, card_number_show_attr, NULL);
6739fb6198eSJaroslav Kysela 
6741da177e4SLinus Torvalds /**
6751da177e4SLinus Torvalds  *  snd_card_register - register the soundcard
6761da177e4SLinus Torvalds  *  @card: soundcard structure
6771da177e4SLinus Torvalds  *
6781da177e4SLinus Torvalds  *  This function registers all the devices assigned to the soundcard.
6791da177e4SLinus Torvalds  *  Until calling this, the ALSA control interface is blocked from the
6801da177e4SLinus Torvalds  *  external accesses.  Thus, you should call this function at the end
6811da177e4SLinus Torvalds  *  of the initialization of the card.
6821da177e4SLinus Torvalds  *
683eb7c06e8SYacine Belkadi  *  Return: Zero otherwise a negative error code if the registration failed.
6841da177e4SLinus Torvalds  */
685512bbd6aSTakashi Iwai int snd_card_register(struct snd_card *card)
6861da177e4SLinus Torvalds {
6871da177e4SLinus Torvalds 	int err;
6881da177e4SLinus Torvalds 
6897eaa943cSTakashi Iwai 	if (snd_BUG_ON(!card))
6907eaa943cSTakashi Iwai 		return -EINVAL;
69139aba963SKay Sievers 
6927d2aae1eSTakashi Iwai 	if (!card->card_dev) {
693abe9ab8fSGreg Kroah-Hartman 		card->card_dev = device_create(sound_class, card->dev,
6949fb6198eSJaroslav Kysela 					       MKDEV(0, 0), card,
695d80f19faSGreg Kroah-Hartman 					       "card%i", card->number);
6967d2aae1eSTakashi Iwai 		if (IS_ERR(card->card_dev))
6977d2aae1eSTakashi Iwai 			card->card_dev = NULL;
698d80f19faSGreg Kroah-Hartman 	}
69939aba963SKay Sievers 
7001da177e4SLinus Torvalds 	if ((err = snd_device_register_all(card)) < 0)
7011da177e4SLinus Torvalds 		return err;
702746df948STakashi Iwai 	mutex_lock(&snd_card_mutex);
7031da177e4SLinus Torvalds 	if (snd_cards[card->number]) {
7041da177e4SLinus Torvalds 		/* already registered */
705746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
7061da177e4SLinus Torvalds 		return 0;
7071da177e4SLinus Torvalds 	}
708e7df2a3aSTakashi Iwai 	if (*card->id) {
709e7df2a3aSTakashi Iwai 		/* make a unique id name from the given string */
710e7df2a3aSTakashi Iwai 		char tmpid[sizeof(card->id)];
711e7df2a3aSTakashi Iwai 		memcpy(tmpid, card->id, sizeof(card->id));
712e7df2a3aSTakashi Iwai 		snd_card_set_id_no_lock(card, tmpid, tmpid);
713e7df2a3aSTakashi Iwai 	} else {
714e7df2a3aSTakashi Iwai 		/* create an id from either shortname or longname */
715e7df2a3aSTakashi Iwai 		const char *src;
716e7df2a3aSTakashi Iwai 		src = *card->shortname ? card->shortname : card->longname;
717e7df2a3aSTakashi Iwai 		snd_card_set_id_no_lock(card, src,
718e7df2a3aSTakashi Iwai 					retrieve_id_from_card_name(src));
719e7df2a3aSTakashi Iwai 	}
7201da177e4SLinus Torvalds 	snd_cards[card->number] = card;
721746df948STakashi Iwai 	mutex_unlock(&snd_card_mutex);
722e28563ccSTakashi Iwai 	init_info_for_card(card);
7231da177e4SLinus Torvalds #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
7241da177e4SLinus Torvalds 	if (snd_mixer_oss_notify_callback)
7251da177e4SLinus Torvalds 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
7261da177e4SLinus Torvalds #endif
7279fb6198eSJaroslav Kysela 	if (card->card_dev) {
7282af75293SHannes Eder 		err = device_create_file(card->card_dev, &card_id_attrs);
7292af75293SHannes Eder 		if (err < 0)
7302af75293SHannes Eder 			return err;
7312af75293SHannes Eder 		err = device_create_file(card->card_dev, &card_number_attrs);
7322af75293SHannes Eder 		if (err < 0)
7332af75293SHannes Eder 			return err;
7349fb6198eSJaroslav Kysela 	}
73539aba963SKay Sievers 
7361da177e4SLinus Torvalds 	return 0;
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
739c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_register);
740c0d3fb39STakashi Iwai 
741e28563ccSTakashi Iwai #ifdef CONFIG_PROC_FS
7426581f4e7STakashi Iwai static struct snd_info_entry *snd_card_info_entry;
7431da177e4SLinus Torvalds 
744a381a7a6STakashi Iwai static void snd_card_info_read(struct snd_info_entry *entry,
745a381a7a6STakashi Iwai 			       struct snd_info_buffer *buffer)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	int idx, count;
748512bbd6aSTakashi Iwai 	struct snd_card *card;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
751746df948STakashi Iwai 		mutex_lock(&snd_card_mutex);
7521da177e4SLinus Torvalds 		if ((card = snd_cards[idx]) != NULL) {
7531da177e4SLinus Torvalds 			count++;
754d001544dSClemens Ladisch 			snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
7551da177e4SLinus Torvalds 					idx,
7561da177e4SLinus Torvalds 					card->id,
7571da177e4SLinus Torvalds 					card->driver,
7581da177e4SLinus Torvalds 					card->shortname);
7591da177e4SLinus Torvalds 			snd_iprintf(buffer, "                      %s\n",
7601da177e4SLinus Torvalds 					card->longname);
7611da177e4SLinus Torvalds 		}
762746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds 	if (!count)
7651da177e4SLinus Torvalds 		snd_iprintf(buffer, "--- no soundcards ---\n");
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
768e28563ccSTakashi Iwai #ifdef CONFIG_SND_OSSEMUL
7691da177e4SLinus Torvalds 
770512bbd6aSTakashi Iwai void snd_card_info_read_oss(struct snd_info_buffer *buffer)
7711da177e4SLinus Torvalds {
7721da177e4SLinus Torvalds 	int idx, count;
773512bbd6aSTakashi Iwai 	struct snd_card *card;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
776746df948STakashi Iwai 		mutex_lock(&snd_card_mutex);
7771da177e4SLinus Torvalds 		if ((card = snd_cards[idx]) != NULL) {
7781da177e4SLinus Torvalds 			count++;
7791da177e4SLinus Torvalds 			snd_iprintf(buffer, "%s\n", card->longname);
7801da177e4SLinus Torvalds 		}
781746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 	if (!count) {
7841da177e4SLinus Torvalds 		snd_iprintf(buffer, "--- no soundcards ---\n");
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds #endif
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds #ifdef MODULE
791512bbd6aSTakashi Iwai static struct snd_info_entry *snd_card_module_info_entry;
792512bbd6aSTakashi Iwai static void snd_card_module_info_read(struct snd_info_entry *entry,
793512bbd6aSTakashi Iwai 				      struct snd_info_buffer *buffer)
7941da177e4SLinus Torvalds {
7951da177e4SLinus Torvalds 	int idx;
796512bbd6aSTakashi Iwai 	struct snd_card *card;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	for (idx = 0; idx < SNDRV_CARDS; idx++) {
799746df948STakashi Iwai 		mutex_lock(&snd_card_mutex);
8001da177e4SLinus Torvalds 		if ((card = snd_cards[idx]) != NULL)
801d001544dSClemens Ladisch 			snd_iprintf(buffer, "%2i %s\n",
802d001544dSClemens Ladisch 				    idx, card->module->name);
803746df948STakashi Iwai 		mutex_unlock(&snd_card_mutex);
8041da177e4SLinus Torvalds 	}
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds #endif
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds int __init snd_card_info_init(void)
8091da177e4SLinus Torvalds {
810512bbd6aSTakashi Iwai 	struct snd_info_entry *entry;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
8137c22f1aaSTakashi Iwai 	if (! entry)
8147c22f1aaSTakashi Iwai 		return -ENOMEM;
8151da177e4SLinus Torvalds 	entry->c.text.read = snd_card_info_read;
8161da177e4SLinus Torvalds 	if (snd_info_register(entry) < 0) {
8171da177e4SLinus Torvalds 		snd_info_free_entry(entry);
8181da177e4SLinus Torvalds 		return -ENOMEM;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 	snd_card_info_entry = entry;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds #ifdef MODULE
8231da177e4SLinus Torvalds 	entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
8241da177e4SLinus Torvalds 	if (entry) {
8251da177e4SLinus Torvalds 		entry->c.text.read = snd_card_module_info_read;
8261da177e4SLinus Torvalds 		if (snd_info_register(entry) < 0)
8271da177e4SLinus Torvalds 			snd_info_free_entry(entry);
8281da177e4SLinus Torvalds 		else
8291da177e4SLinus Torvalds 			snd_card_module_info_entry = entry;
8301da177e4SLinus Torvalds 	}
8311da177e4SLinus Torvalds #endif
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	return 0;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds int __exit snd_card_info_done(void)
8371da177e4SLinus Torvalds {
838746d4a02STakashi Iwai 	snd_info_free_entry(snd_card_info_entry);
8391da177e4SLinus Torvalds #ifdef MODULE
840746d4a02STakashi Iwai 	snd_info_free_entry(snd_card_module_info_entry);
8411da177e4SLinus Torvalds #endif
8421da177e4SLinus Torvalds 	return 0;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
845e28563ccSTakashi Iwai #endif /* CONFIG_PROC_FS */
846e28563ccSTakashi Iwai 
8471da177e4SLinus Torvalds /**
8481da177e4SLinus Torvalds  *  snd_component_add - add a component string
8491da177e4SLinus Torvalds  *  @card: soundcard structure
8501da177e4SLinus Torvalds  *  @component: the component id string
8511da177e4SLinus Torvalds  *
8521da177e4SLinus Torvalds  *  This function adds the component id string to the supported list.
8531da177e4SLinus Torvalds  *  The component can be referred from the alsa-lib.
8541da177e4SLinus Torvalds  *
855eb7c06e8SYacine Belkadi  *  Return: Zero otherwise a negative error code.
8561da177e4SLinus Torvalds  */
8571da177e4SLinus Torvalds 
858512bbd6aSTakashi Iwai int snd_component_add(struct snd_card *card, const char *component)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	char *ptr;
8611da177e4SLinus Torvalds 	int len = strlen(component);
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	ptr = strstr(card->components, component);
8641da177e4SLinus Torvalds 	if (ptr != NULL) {
8651da177e4SLinus Torvalds 		if (ptr[len] == '\0' || ptr[len] == ' ')	/* already there */
8661da177e4SLinus Torvalds 			return 1;
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 	if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
8691da177e4SLinus Torvalds 		snd_BUG();
8701da177e4SLinus Torvalds 		return -ENOMEM;
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds 	if (card->components[0] != '\0')
8731da177e4SLinus Torvalds 		strcat(card->components, " ");
8741da177e4SLinus Torvalds 	strcat(card->components, component);
8751da177e4SLinus Torvalds 	return 0;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
878c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_component_add);
879c0d3fb39STakashi Iwai 
8801da177e4SLinus Torvalds /**
8811da177e4SLinus Torvalds  *  snd_card_file_add - add the file to the file list of the card
8821da177e4SLinus Torvalds  *  @card: soundcard structure
8831da177e4SLinus Torvalds  *  @file: file pointer
8841da177e4SLinus Torvalds  *
8851da177e4SLinus Torvalds  *  This function adds the file to the file linked-list of the card.
8861da177e4SLinus Torvalds  *  This linked-list is used to keep tracking the connection state,
8871da177e4SLinus Torvalds  *  and to avoid the release of busy resources by hotplug.
8881da177e4SLinus Torvalds  *
889eb7c06e8SYacine Belkadi  *  Return: zero or a negative error code.
8901da177e4SLinus Torvalds  */
891512bbd6aSTakashi Iwai int snd_card_file_add(struct snd_card *card, struct file *file)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	struct snd_monitor_file *mfile;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
8961da177e4SLinus Torvalds 	if (mfile == NULL)
8971da177e4SLinus Torvalds 		return -ENOMEM;
8981da177e4SLinus Torvalds 	mfile->file = file;
899a9edfc60SKarsten Wiese 	mfile->disconnected_f_op = NULL;
900a45e3d6bSTakashi Iwai 	INIT_LIST_HEAD(&mfile->shutdown_list);
9011da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
9021da177e4SLinus Torvalds 	if (card->shutdown) {
9031da177e4SLinus Torvalds 		spin_unlock(&card->files_lock);
9041da177e4SLinus Torvalds 		kfree(mfile);
9051da177e4SLinus Torvalds 		return -ENODEV;
9061da177e4SLinus Torvalds 	}
907118dd6bfSTakashi Iwai 	list_add(&mfile->list, &card->files_list);
908a0830dbdSTakashi Iwai 	atomic_inc(&card->refcount);
9091da177e4SLinus Torvalds 	spin_unlock(&card->files_lock);
9101da177e4SLinus Torvalds 	return 0;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
913c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_file_add);
914c0d3fb39STakashi Iwai 
9151da177e4SLinus Torvalds /**
9161da177e4SLinus Torvalds  *  snd_card_file_remove - remove the file from the file list
9171da177e4SLinus Torvalds  *  @card: soundcard structure
9181da177e4SLinus Torvalds  *  @file: file pointer
9191da177e4SLinus Torvalds  *
9201da177e4SLinus Torvalds  *  This function removes the file formerly added to the card via
9211da177e4SLinus Torvalds  *  snd_card_file_add() function.
9222b29b13cSTakashi Iwai  *  If all files are removed and snd_card_free_when_closed() was
9232b29b13cSTakashi Iwai  *  called beforehand, it processes the pending release of
9242b29b13cSTakashi Iwai  *  resources.
9251da177e4SLinus Torvalds  *
926eb7c06e8SYacine Belkadi  *  Return: Zero or a negative error code.
9271da177e4SLinus Torvalds  */
928512bbd6aSTakashi Iwai int snd_card_file_remove(struct snd_card *card, struct file *file)
9291da177e4SLinus Torvalds {
930118dd6bfSTakashi Iwai 	struct snd_monitor_file *mfile, *found = NULL;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	spin_lock(&card->files_lock);
933118dd6bfSTakashi Iwai 	list_for_each_entry(mfile, &card->files_list, list) {
9341da177e4SLinus Torvalds 		if (mfile->file == file) {
935118dd6bfSTakashi Iwai 			list_del(&mfile->list);
936a45e3d6bSTakashi Iwai 			spin_lock(&shutdown_lock);
937a45e3d6bSTakashi Iwai 			list_del(&mfile->shutdown_list);
938a45e3d6bSTakashi Iwai 			spin_unlock(&shutdown_lock);
939118dd6bfSTakashi Iwai 			if (mfile->disconnected_f_op)
940118dd6bfSTakashi Iwai 				fops_put(mfile->disconnected_f_op);
941118dd6bfSTakashi Iwai 			found = mfile;
9421da177e4SLinus Torvalds 			break;
9431da177e4SLinus Torvalds 		}
9441da177e4SLinus Torvalds 	}
945c461482cSTakashi Iwai 	spin_unlock(&card->files_lock);
946118dd6bfSTakashi Iwai 	if (!found) {
9471da177e4SLinus Torvalds 		snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
9481da177e4SLinus Torvalds 		return -ENOENT;
9491da177e4SLinus Torvalds 	}
950118dd6bfSTakashi Iwai 	kfree(found);
951a0830dbdSTakashi Iwai 	snd_card_unref(card);
9521da177e4SLinus Torvalds 	return 0;
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
955c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_card_file_remove);
956c0d3fb39STakashi Iwai 
9571da177e4SLinus Torvalds #ifdef CONFIG_PM
9581da177e4SLinus Torvalds /**
9591da177e4SLinus Torvalds  *  snd_power_wait - wait until the power-state is changed.
9601da177e4SLinus Torvalds  *  @card: soundcard structure
9611da177e4SLinus Torvalds  *  @power_state: expected power state
9621da177e4SLinus Torvalds  *
9631da177e4SLinus Torvalds  *  Waits until the power-state is changed.
9641da177e4SLinus Torvalds  *
965eb7c06e8SYacine Belkadi  *  Return: Zero if successful, or a negative error code.
966eb7c06e8SYacine Belkadi  *
9671da177e4SLinus Torvalds  *  Note: the power lock must be active before call.
9681da177e4SLinus Torvalds  */
969cbac4b0cSTakashi Iwai int snd_power_wait(struct snd_card *card, unsigned int power_state)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	wait_queue_t wait;
9721da177e4SLinus Torvalds 	int result = 0;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	/* fastpath */
9751da177e4SLinus Torvalds 	if (snd_power_get_state(card) == power_state)
9761da177e4SLinus Torvalds 		return 0;
9771da177e4SLinus Torvalds 	init_waitqueue_entry(&wait, current);
9781da177e4SLinus Torvalds 	add_wait_queue(&card->power_sleep, &wait);
9791da177e4SLinus Torvalds 	while (1) {
9801da177e4SLinus Torvalds 		if (card->shutdown) {
9811da177e4SLinus Torvalds 			result = -ENODEV;
9821da177e4SLinus Torvalds 			break;
9831da177e4SLinus Torvalds 		}
9841da177e4SLinus Torvalds 		if (snd_power_get_state(card) == power_state)
9851da177e4SLinus Torvalds 			break;
9861da177e4SLinus Torvalds 		set_current_state(TASK_UNINTERRUPTIBLE);
9871da177e4SLinus Torvalds 		snd_power_unlock(card);
9881da177e4SLinus Torvalds 		schedule_timeout(30 * HZ);
9891da177e4SLinus Torvalds 		snd_power_lock(card);
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 	remove_wait_queue(&card->power_sleep, &wait);
9921da177e4SLinus Torvalds 	return result;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
995c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_power_wait);
9961da177e4SLinus Torvalds #endif /* CONFIG_PM */
997