blob: 3e0cebacefe1e74c42148f7e3e227f9de47852b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Initialization routines
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/sched.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050025#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/file.h>
27#include <linux/slab.h>
28#include <linux/time.h>
29#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/pm.h>
Takashi Iwaif2464062014-01-29 12:13:43 +010031#include <linux/completion.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/core.h>
34#include <sound/control.h>
35#include <sound/info.h>
36
Takashi Iwai82a783f2009-09-07 15:50:18 +020037/* monitor files for graceful shutdown (hotplug) */
38struct snd_monitor_file {
39 struct file *file;
40 const struct file_operations *disconnected_f_op;
41 struct list_head shutdown_list; /* still need to shutdown */
42 struct list_head list; /* link of monitor files */
43};
44
Karsten Wiesea9edfc62006-10-06 16:08:27 +020045static DEFINE_SPINLOCK(shutdown_lock);
46static LIST_HEAD(shutdown_files);
47
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -080048static const struct file_operations snd_shutdown_f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Takashi Iwai7bb24912013-05-15 08:46:39 +020050/* locked for registering/using */
51static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS);
Takashi Iwai6581f4e2006-05-17 17:14:51 +020052struct snd_card *snd_cards[SNDRV_CARDS];
Takashi Iwaic0d3fb32006-04-28 15:13:39 +020053EXPORT_SYMBOL(snd_cards);
54
Takashi Iwai746df942006-05-15 19:49:05 +020055static DEFINE_MUTEX(snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Takashi Iwai304cd072007-10-26 15:10:15 +020057static char *slots[SNDRV_CARDS];
58module_param_array(slots, charp, NULL, 0444);
59MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
60
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020061/* return non-zero if the given index is reserved for the given
Takashi Iwai304cd072007-10-26 15:10:15 +020062 * module via slots option
63 */
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020064static int module_slot_match(struct module *module, int idx)
Takashi Iwai304cd072007-10-26 15:10:15 +020065{
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020066 int match = 1;
Takashi Iwai304cd072007-10-26 15:10:15 +020067#ifdef MODULE
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020068 const char *s1, *s2;
69
Takashi Iwai60f6fef2013-10-28 12:54:52 +010070 if (!module || !*module->name || !slots[idx])
Takashi Iwai304cd072007-10-26 15:10:15 +020071 return 0;
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020072
73 s1 = module->name;
74 s2 = slots[idx];
75 if (*s2 == '!') {
76 match = 0; /* negative match */
77 s2++;
78 }
Takashi Iwai304cd072007-10-26 15:10:15 +020079 /* compare module name strings
80 * hyphens are handled as equivalent with underscore
81 */
82 for (;;) {
83 char c1 = *s1++;
84 char c2 = *s2++;
85 if (c1 == '-')
86 c1 = '_';
87 if (c2 == '-')
88 c2 = '_';
89 if (c1 != c2)
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020090 return !match;
Takashi Iwai304cd072007-10-26 15:10:15 +020091 if (!c1)
92 break;
93 }
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020094#endif /* MODULE */
95 return match;
Takashi Iwai304cd072007-10-26 15:10:15 +020096}
97
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +010098#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Takashi Iwai512bbd62005-11-17 13:51:18 +010099int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200100EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif
102
Jie Yangcd6a6502015-05-27 19:45:45 +0800103#ifdef CONFIG_SND_PROC_FS
Takashi Iwai512bbd62005-11-17 13:51:18 +0100104static void snd_card_id_read(struct snd_info_entry *entry,
105 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 snd_iprintf(buffer, "%s\n", entry->card->id);
108}
109
Takashi Iwai2471b6c2015-05-18 09:20:24 +0200110static int init_info_for_card(struct snd_card *card)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100111{
112 int err;
113 struct snd_info_entry *entry;
114
Takashi Iwai2471b6c2015-05-18 09:20:24 +0200115 entry = snd_info_create_card_entry(card, "id", card->proc_root);
116 if (!entry) {
Takashi Iwaif2f93072014-02-04 18:21:03 +0100117 dev_dbg(card->dev, "unable to create card entry\n");
Takashi Iwaie28563c2005-12-01 10:42:42 +0100118 return err;
119 }
Takashi Iwaie28563c2005-12-01 10:42:42 +0100120 entry->c.text.read = snd_card_id_read;
Takashi Iwaie28563c2005-12-01 10:42:42 +0100121 card->proc_id = entry;
Takashi Iwai2471b6c2015-05-18 09:20:24 +0200122
123 return snd_info_card_register(card);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100124}
Jie Yangcd6a6502015-05-27 19:45:45 +0800125#else /* !CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100126#define init_info_for_card(card)
127#endif
128
Takashi Iwaideb65962014-01-23 11:02:15 +0100129static int check_empty_slot(struct module *module, int slot)
130{
131 return !slots[slot] || !*slots[slot];
132}
133
134/* return an empty slot number (>= 0) found in the given bitmask @mask.
135 * @mask == -1 == 0xffffffff means: take any free slot up to 32
136 * when no slot is available, return the original @mask as is.
137 */
138static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int),
139 struct module *module)
140{
141 int slot;
142
143 for (slot = 0; slot < SNDRV_CARDS; slot++) {
144 if (slot < 32 && !(mask & (1U << slot)))
145 continue;
146 if (!test_bit(slot, snd_cards_lock)) {
147 if (check(module, slot))
148 return slot; /* found */
149 }
150 }
151 return mask; /* unchanged */
152}
153
Takashi Iwai4b440be2015-01-29 21:32:47 +0100154/* the default release callback set in snd_device_initialize() below;
155 * this is just NOP for now, as almost all jobs are already done in
156 * dev_free callback of snd_device chain instead.
157 */
158static void default_release(struct device *dev)
159{
160}
161
162/**
163 * snd_device_initialize - Initialize struct device for sound devices
164 * @dev: device to initialize
165 * @card: card to assign, optional
166 */
167void snd_device_initialize(struct device *dev, struct snd_card *card)
168{
169 device_initialize(dev);
170 if (card)
171 dev->parent = &card->card_dev;
172 dev->class = sound_class;
173 dev->release = default_release;
174}
175EXPORT_SYMBOL_GPL(snd_device_initialize);
176
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100177static int snd_card_do_free(struct snd_card *card);
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100178static const struct attribute_group card_dev_attr_group;
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100179
180static void release_card_device(struct device *dev)
181{
182 snd_card_do_free(dev_to_snd_card(dev));
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/**
Takashi Iwai393aa9c2014-01-29 12:51:12 +0100186 * snd_card_new - create and initialize a soundcard structure
187 * @parent: the parent device object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
189 * @xid: card identification (ASCII string)
190 * @module: top level module for locking
191 * @extra_size: allocate this extra size after the main soundcard structure
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100192 * @card_ret: the pointer to store the created card instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
194 * Creates and initializes a soundcard structure.
195 *
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100196 * The function allocates snd_card instance via kzalloc with the given
197 * space for the driver to use freely. The allocated struct is stored
198 * in the given card_ret pointer.
199 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100200 * Return: Zero if successful or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Takashi Iwai393aa9c2014-01-29 12:51:12 +0100202int snd_card_new(struct device *parent, int idx, const char *xid,
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100203 struct module *module, int extra_size,
204 struct snd_card **card_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100206 struct snd_card *card;
Takashi Iwaideb65962014-01-23 11:02:15 +0100207 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100209 if (snd_BUG_ON(!card_ret))
210 return -EINVAL;
211 *card_ret = NULL;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (extra_size < 0)
214 extra_size = 0;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200215 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100216 if (!card)
217 return -ENOMEM;
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100218 if (extra_size > 0)
219 card->private_data = (char *)card + sizeof(struct snd_card);
Jaroslav Kysela10a8ebb2009-06-02 12:02:38 +0200220 if (xid)
Jaroslav Kysela5fdc18d2009-06-04 00:12:18 +0200221 strlcpy(card->id, xid, sizeof(card->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 err = 0;
Takashi Iwai746df942006-05-15 19:49:05 +0200223 mutex_lock(&snd_card_mutex);
Takashi Iwaideb65962014-01-23 11:02:15 +0100224 if (idx < 0) /* first check the matching module-name slot */
225 idx = get_slot_from_bitmask(idx, module_slot_match, module);
226 if (idx < 0) /* if not matched, assign an empty slot */
227 idx = get_slot_from_bitmask(idx, check_empty_slot, module);
Takashi Iwaia93bbaa2008-05-27 17:59:24 +0200228 if (idx < 0)
229 err = -ENODEV;
230 else if (idx < snd_ecards_limit) {
Takashi Iwai7bb24912013-05-15 08:46:39 +0200231 if (test_bit(idx, snd_cards_lock))
Takashi Iwaia93bbaa2008-05-27 17:59:24 +0200232 err = -EBUSY; /* invalid */
233 } else if (idx >= SNDRV_CARDS)
234 err = -ENODEV;
235 if (err < 0) {
Takashi Iwai746df942006-05-15 19:49:05 +0200236 mutex_unlock(&snd_card_mutex);
Takashi Iwaif2f93072014-02-04 18:21:03 +0100237 dev_err(parent, "cannot find the slot for index %d (range 0-%i), error: %d\n",
Oliver Neukum5c33dd72007-01-16 17:49:21 +0100238 idx, snd_ecards_limit - 1, err);
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100239 kfree(card);
240 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Takashi Iwai7bb24912013-05-15 08:46:39 +0200242 set_bit(idx, snd_cards_lock); /* lock it */
Takashi Iwaia93bbaa2008-05-27 17:59:24 +0200243 if (idx >= snd_ecards_limit)
244 snd_ecards_limit = idx + 1; /* increase the limit */
Takashi Iwai746df942006-05-15 19:49:05 +0200245 mutex_unlock(&snd_card_mutex);
Takashi Iwai393aa9c2014-01-29 12:51:12 +0100246 card->dev = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 card->number = idx;
248 card->module = module;
249 INIT_LIST_HEAD(&card->devices);
250 init_rwsem(&card->controls_rwsem);
251 rwlock_init(&card->ctl_files_rwlock);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +0200252 mutex_init(&card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 INIT_LIST_HEAD(&card->controls);
254 INIT_LIST_HEAD(&card->ctl_files);
255 spin_lock_init(&card->files_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100256 INIT_LIST_HEAD(&card->files_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#ifdef CONFIG_PM
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100258 mutex_init(&card->power_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 init_waitqueue_head(&card->power_sleep);
260#endif
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100261
262 device_initialize(&card->card_dev);
263 card->card_dev.parent = parent;
264 card->card_dev.class = sound_class;
265 card->card_dev.release = release_card_device;
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100266 card->card_dev.groups = card->dev_groups;
267 card->dev_groups[0] = &card_dev_attr_group;
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100268 err = kobject_set_name(&card->card_dev.kobj, "card%d", idx);
269 if (err < 0)
270 goto __error;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* the control interface cannot be accessed from the user space until */
273 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100274 err = snd_ctl_create(card);
275 if (err < 0) {
Takashi Iwaif2f93072014-02-04 18:21:03 +0100276 dev_err(parent, "unable to register control minors\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto __error;
278 }
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100279 err = snd_info_card_create(card);
280 if (err < 0) {
Takashi Iwaif2f93072014-02-04 18:21:03 +0100281 dev_err(parent, "unable to create card info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 goto __error_ctl;
283 }
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100284 *card_ret = card;
285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 __error_ctl:
Takashi Iwai289ca022014-01-29 15:53:35 +0100288 snd_device_free_all(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 __error:
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100290 put_device(&card->card_dev);
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
Takashi Iwai393aa9c2014-01-29 12:51:12 +0100293EXPORT_SYMBOL(snd_card_new);
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200294
Takashi Iwai746df942006-05-15 19:49:05 +0200295/* return non-zero if a card is already locked */
296int snd_card_locked(int card)
297{
298 int locked;
299
300 mutex_lock(&snd_card_mutex);
Takashi Iwai7bb24912013-05-15 08:46:39 +0200301 locked = test_bit(card, snd_cards_lock);
Takashi Iwai746df942006-05-15 19:49:05 +0200302 mutex_unlock(&snd_card_mutex);
303 return locked;
304}
305
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100306static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
307{
308 return -ENODEV;
309}
310
311static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
312 size_t count, loff_t *offset)
313{
314 return -ENODEV;
315}
316
317static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
318 size_t count, loff_t *offset)
319{
320 return -ENODEV;
321}
322
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200323static int snd_disconnect_release(struct inode *inode, struct file *file)
324{
325 struct snd_monitor_file *df = NULL, *_df;
326
327 spin_lock(&shutdown_lock);
328 list_for_each_entry(_df, &shutdown_files, shutdown_list) {
329 if (_df->file == file) {
330 df = _df;
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100331 list_del_init(&df->shutdown_list);
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200332 break;
333 }
334 }
335 spin_unlock(&shutdown_lock);
336
Al Viro233e70f2008-10-31 23:28:30 +0000337 if (likely(df)) {
338 if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync)
339 df->disconnected_f_op->fasync(-1, file, 0);
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200340 return df->disconnected_f_op->release(inode, file);
Al Viro233e70f2008-10-31 23:28:30 +0000341 }
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200342
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800343 panic("%s(%p, %p) failed!", __func__, inode, file);
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
347{
348 return POLLERR | POLLNVAL;
349}
350
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100351static long snd_disconnect_ioctl(struct file *file,
352 unsigned int cmd, unsigned long arg)
353{
354 return -ENODEV;
355}
356
357static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
358{
359 return -ENODEV;
360}
361
362static int snd_disconnect_fasync(int fd, struct file *file, int on)
363{
364 return -ENODEV;
365}
366
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800367static const struct file_operations snd_shutdown_f_ops =
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200368{
369 .owner = THIS_MODULE,
370 .llseek = snd_disconnect_llseek,
371 .read = snd_disconnect_read,
372 .write = snd_disconnect_write,
373 .release = snd_disconnect_release,
374 .poll = snd_disconnect_poll,
375 .unlocked_ioctl = snd_disconnect_ioctl,
376#ifdef CONFIG_COMPAT
377 .compat_ioctl = snd_disconnect_ioctl,
378#endif
379 .mmap = snd_disconnect_mmap,
380 .fasync = snd_disconnect_fasync
381};
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/**
384 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
385 * @card: soundcard structure
386 *
387 * Disconnects all APIs from the file-operations (user space).
388 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100389 * Return: Zero, otherwise a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 *
391 * Note: The current implementation replaces all active file->f_op with special
392 * dummy file operations (they do nothing except release).
393 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100394int snd_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct snd_monitor_file *mfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Takashi Iwaif18638d2008-04-17 12:52:02 +0200398 if (!card)
399 return -EINVAL;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 spin_lock(&card->files_lock);
402 if (card->shutdown) {
403 spin_unlock(&card->files_lock);
404 return 0;
405 }
406 card->shutdown = 1;
407 spin_unlock(&card->files_lock);
408
409 /* phase 1: disable fops (user space) operations for ALSA API */
Takashi Iwai746df942006-05-15 19:49:05 +0200410 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 snd_cards[card->number] = NULL;
Takashi Iwai7bb24912013-05-15 08:46:39 +0200412 clear_bit(card->number, snd_cards_lock);
Takashi Iwai746df942006-05-15 19:49:05 +0200413 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* phase 2: replace file->f_op with special dummy operations */
416
417 spin_lock(&card->files_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100418 list_for_each_entry(mfile, &card->files_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* it's critical part, use endless loop */
420 /* we have no room to fail */
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200421 mfile->disconnected_f_op = mfile->file->f_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200423 spin_lock(&shutdown_lock);
424 list_add(&mfile->shutdown_list, &shutdown_files);
425 spin_unlock(&shutdown_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200427 mfile->file->f_op = &snd_shutdown_f_ops;
Miguel Botonbc9abce2008-01-13 12:03:53 +0100428 fops_get(mfile->file->f_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 spin_unlock(&card->files_lock);
431
432 /* phase 3: notify all connected devices about disconnection */
433 /* at this point, they cannot respond to any calls except release() */
434
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100435#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (snd_mixer_oss_notify_callback)
437 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
438#endif
439
440 /* notify all devices that we are disconnected */
Takashi Iwaie086e302015-02-27 18:01:22 +0100441 snd_device_disconnect_all(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Takashi Iwai746d4a02006-06-23 14:37:59 +0200443 snd_info_card_disconnect(card);
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100444 if (card->registered) {
445 device_del(&card->card_dev);
446 card->registered = false;
Takashi Iwai73d38b12008-04-17 12:50:47 +0200447 }
Takashi Iwaif18638d2008-04-17 12:52:02 +0200448#ifdef CONFIG_PM
449 wake_up(&card->power_sleep);
450#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0;
452}
453
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200454EXPORT_SYMBOL(snd_card_disconnect);
455
Takashi Iwaic4614822006-06-23 14:38:23 +0200456static int snd_card_do_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100458#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (snd_mixer_oss_notify_callback)
460 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
461#endif
Takashi Iwai72620d62014-02-04 11:36:11 +0100462 snd_device_free_all(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (card->private_free)
464 card->private_free(card);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200465 snd_info_free_entry(card->proc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (snd_info_card_free(card) < 0) {
Takashi Iwaif2f93072014-02-04 18:21:03 +0100467 dev_warn(card->dev, "unable to free card info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* Not fatal error */
469 }
Takashi Iwaif2464062014-01-29 12:13:43 +0100470 if (card->release_completion)
471 complete(card->release_completion);
Takashi Iwaic4614822006-06-23 14:38:23 +0200472 kfree(card);
473 return 0;
474}
475
Takashi Iwaieb9c38d2014-10-30 15:19:43 +0100476/**
477 * snd_card_free_when_closed - Disconnect the card, free it later eventually
478 * @card: soundcard structure
479 *
480 * Unlike snd_card_free(), this function doesn't try to release the card
481 * resource immediately, but tries to disconnect at first. When the card
482 * is still in use, the function returns before freeing the resources.
483 * The card resources will be freed when the refcount gets to zero.
484 */
Takashi Iwaic4614822006-06-23 14:38:23 +0200485int snd_card_free_when_closed(struct snd_card *card)
486{
Takashi Iwaif18638d2008-04-17 12:52:02 +0200487 int ret = snd_card_disconnect(card);
Takashi Iwaic4614822006-06-23 14:38:23 +0200488 if (ret)
489 return ret;
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100490 put_device(&card->card_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return 0;
492}
Takashi Iwaif2464062014-01-29 12:13:43 +0100493EXPORT_SYMBOL(snd_card_free_when_closed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Takashi Iwaieb9c38d2014-10-30 15:19:43 +0100495/**
496 * snd_card_free - frees given soundcard structure
497 * @card: soundcard structure
498 *
499 * This function releases the soundcard structure and the all assigned
500 * devices automatically. That is, you don't have to release the devices
501 * by yourself.
502 *
503 * This function waits until the all resources are properly released.
504 *
505 * Return: Zero. Frees all associated devices and frees the control
506 * interface associated to given soundcard.
507 */
Takashi Iwaif2464062014-01-29 12:13:43 +0100508int snd_card_free(struct snd_card *card)
509{
510 struct completion released;
511 int ret;
512
513 init_completion(&released);
514 card->release_completion = &released;
515 ret = snd_card_free_when_closed(card);
516 if (ret)
517 return ret;
518 /* wait, until all devices are ready for the free operation */
519 wait_for_completion(&released);
520 return 0;
521}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200522EXPORT_SYMBOL(snd_card_free);
523
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100524/* retrieve the last word of shortname or longname */
525static const char *retrieve_id_from_card_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100527 const char *spos = name;
528
529 while (*name) {
530 if (isspace(*name) && isalnum(name[1]))
531 spos = name + 1;
532 name++;
533 }
534 return spos;
535}
536
537/* return true if the given id string doesn't conflict any other card ids */
538static bool card_id_ok(struct snd_card *card, const char *id)
539{
540 int i;
541 if (!snd_info_check_reserved_words(id))
542 return false;
543 for (i = 0; i < snd_ecards_limit; i++) {
544 if (snd_cards[i] && snd_cards[i] != card &&
545 !strcmp(snd_cards[i]->id, id))
546 return false;
547 }
548 return true;
549}
550
551/* copy to card->id only with valid letters from nid */
552static void copy_valid_id_string(struct snd_card *card, const char *src,
553 const char *nid)
554{
555 char *id = card->id;
556
557 while (*nid && !isalnum(*nid))
558 nid++;
559 if (isdigit(*nid))
560 *id++ = isalpha(*src) ? *src : 'D';
561 while (*nid && (size_t)(id - card->id) < sizeof(card->id) - 1) {
562 if (isalnum(*nid))
563 *id++ = *nid;
564 nid++;
565 }
566 *id = 0;
567}
568
569/* Set card->id from the given string
570 * If the string conflicts with other ids, add a suffix to make it unique.
571 */
572static void snd_card_set_id_no_lock(struct snd_card *card, const char *src,
573 const char *nid)
574{
575 int len, loops;
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100576 bool is_default = false;
Jaroslav Kysela10a8ebb2009-06-02 12:02:38 +0200577 char *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100579 copy_valid_id_string(card, src, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 id = card->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100582 again:
583 /* use "Default" for obviously invalid strings
584 * ("card" conflicts with proc directories)
585 */
586 if (!*id || !strncmp(id, "card", 4)) {
Takashi Iwaid8e4f9a2011-04-04 12:43:23 +0200587 strcpy(id, "Default");
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100588 is_default = true;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Takashi Iwai8edbb192013-05-24 16:30:39 +0200591 len = strlen(id);
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100592 for (loops = 0; loops < SNDRV_CARDS; loops++) {
Takashi Iwai8edbb192013-05-24 16:30:39 +0200593 char *spos;
594 char sfxstr[5]; /* "_012" */
595 int sfxlen;
596
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100597 if (card_id_ok(card, id))
598 return; /* OK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Takashi Iwai8edbb192013-05-24 16:30:39 +0200600 /* Add _XYZ suffix */
601 sprintf(sfxstr, "_%X", loops + 1);
602 sfxlen = strlen(sfxstr);
603 if (len + sfxlen >= sizeof(card->id))
604 spos = id + sizeof(card->id) - sfxlen - 1;
605 else
606 spos = id + len;
607 strcpy(spos, sfxstr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100609 /* fallback to the default id */
610 if (!is_default) {
611 *id = 0;
612 goto again;
613 }
614 /* last resort... */
Takashi Iwaif2f93072014-02-04 18:21:03 +0100615 dev_err(card->dev, "unable to set card id (%s)\n", id);
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100616 if (card->proc_root->name)
Takashi Iwai97f44f52013-10-29 15:20:06 +0100617 strlcpy(card->id, card->proc_root->name, sizeof(card->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Mark Brown872c7822009-06-03 20:43:29 +0100620/**
621 * snd_card_set_id - set card identification name
622 * @card: soundcard structure
623 * @nid: new identification string
624 *
625 * This function sets the card identification and checks for name
626 * collisions.
627 */
628void snd_card_set_id(struct snd_card *card, const char *nid)
629{
Jaroslav Kysela5fdc18d2009-06-04 00:12:18 +0200630 /* check if user specified own card->id */
631 if (card->id[0] != '\0')
632 return;
633 mutex_lock(&snd_card_mutex);
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100634 snd_card_set_id_no_lock(card, nid, nid);
Jaroslav Kysela5fdc18d2009-06-04 00:12:18 +0200635 mutex_unlock(&snd_card_mutex);
Mark Brown872c7822009-06-03 20:43:29 +0100636}
Jaroslav Kysela10a8ebb2009-06-02 12:02:38 +0200637EXPORT_SYMBOL(snd_card_set_id);
638
Jaroslav Kysela9fb6198e2008-11-11 16:51:02 +0100639static ssize_t
640card_id_show_attr(struct device *dev,
641 struct device_attribute *attr, char *buf)
642{
Takashi Iwaib203dba2014-02-19 11:18:10 +0100643 struct snd_card *card = container_of(dev, struct snd_card, card_dev);
644 return snprintf(buf, PAGE_SIZE, "%s\n", card->id);
Jaroslav Kysela9fb6198e2008-11-11 16:51:02 +0100645}
646
647static ssize_t
648card_id_store_attr(struct device *dev, struct device_attribute *attr,
649 const char *buf, size_t count)
650{
Takashi Iwaib203dba2014-02-19 11:18:10 +0100651 struct snd_card *card = container_of(dev, struct snd_card, card_dev);
Jaroslav Kysela9fb6198e2008-11-11 16:51:02 +0100652 char buf1[sizeof(card->id)];
653 size_t copy = count > sizeof(card->id) - 1 ?
654 sizeof(card->id) - 1 : count;
655 size_t idx;
656 int c;
657
658 for (idx = 0; idx < copy; idx++) {
659 c = buf[idx];
660 if (!isalnum(c) && c != '_' && c != '-')
661 return -EINVAL;
662 }
663 memcpy(buf1, buf, copy);
664 buf1[copy] = '\0';
665 mutex_lock(&snd_card_mutex);
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100666 if (!card_id_ok(NULL, buf1)) {
Jaroslav Kysela9fb6198e2008-11-11 16:51:02 +0100667 mutex_unlock(&snd_card_mutex);
668 return -EEXIST;
669 }
Jaroslav Kysela9fb6198e2008-11-11 16:51:02 +0100670 strcpy(card->id, buf1);
Jaroslav Kyselac2eb9c42008-11-12 16:31:37 +0100671 snd_info_card_id_change(card);
Jaroslav Kysela9fb6198e2008-11-11 16:51:02 +0100672 mutex_unlock(&snd_card_mutex);
673
674 return count;
675}
676
Takashi Iwai34356db2014-01-29 11:54:10 +0100677static DEVICE_ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr);
Jaroslav Kysela9fb6198e2008-11-11 16:51:02 +0100678
679static ssize_t
680card_number_show_attr(struct device *dev,
681 struct device_attribute *attr, char *buf)
682{
Takashi Iwaib203dba2014-02-19 11:18:10 +0100683 struct snd_card *card = container_of(dev, struct snd_card, card_dev);
684 return snprintf(buf, PAGE_SIZE, "%i\n", card->number);
Jaroslav Kysela9fb6198e2008-11-11 16:51:02 +0100685}
686
Takashi Iwai34356db2014-01-29 11:54:10 +0100687static DEVICE_ATTR(number, S_IRUGO, card_number_show_attr, NULL);
688
689static struct attribute *card_dev_attrs[] = {
690 &dev_attr_id.attr,
691 &dev_attr_number.attr,
692 NULL
693};
694
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100695static const struct attribute_group card_dev_attr_group = {
Takashi Iwai34356db2014-01-29 11:54:10 +0100696 .attrs = card_dev_attrs,
697};
698
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100699/**
700 * snd_card_add_dev_attr - Append a new sysfs attribute group to card
701 * @card: card instance
702 * @group: attribute group to append
703 */
704int snd_card_add_dev_attr(struct snd_card *card,
705 const struct attribute_group *group)
706{
707 int i;
708
709 /* loop for (arraysize-1) here to keep NULL at the last entry */
710 for (i = 0; i < ARRAY_SIZE(card->dev_groups) - 1; i++) {
711 if (!card->dev_groups[i]) {
712 card->dev_groups[i] = group;
713 return 0;
714 }
715 }
716
717 dev_err(card->dev, "Too many groups assigned\n");
718 return -ENOSPC;
Takashi Iwai34356db2014-01-29 11:54:10 +0100719};
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100720EXPORT_SYMBOL_GPL(snd_card_add_dev_attr);
Jaroslav Kysela9fb6198e2008-11-11 16:51:02 +0100721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/**
723 * snd_card_register - register the soundcard
724 * @card: soundcard structure
725 *
726 * This function registers all the devices assigned to the soundcard.
727 * Until calling this, the ALSA control interface is blocked from the
728 * external accesses. Thus, you should call this function at the end
729 * of the initialization of the card.
730 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100731 * Return: Zero otherwise a negative error code if the registration failed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100733int snd_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200737 if (snd_BUG_ON(!card))
738 return -EINVAL;
Kay Sievers39aba962010-09-04 22:33:14 -0700739
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100740 if (!card->registered) {
741 err = device_add(&card->card_dev);
742 if (err < 0)
743 return err;
744 card->registered = true;
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700745 }
Kay Sievers39aba962010-09-04 22:33:14 -0700746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if ((err = snd_device_register_all(card)) < 0)
748 return err;
Takashi Iwai746df942006-05-15 19:49:05 +0200749 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (snd_cards[card->number]) {
751 /* already registered */
Takashi Iwai746df942006-05-15 19:49:05 +0200752 mutex_unlock(&snd_card_mutex);
Takashi Iwai2471b6c2015-05-18 09:20:24 +0200753 return snd_info_card_register(card); /* register pending info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100755 if (*card->id) {
756 /* make a unique id name from the given string */
757 char tmpid[sizeof(card->id)];
758 memcpy(tmpid, card->id, sizeof(card->id));
759 snd_card_set_id_no_lock(card, tmpid, tmpid);
760 } else {
761 /* create an id from either shortname or longname */
762 const char *src;
763 src = *card->shortname ? card->shortname : card->longname;
764 snd_card_set_id_no_lock(card, src,
765 retrieve_id_from_card_name(src));
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 snd_cards[card->number] = card;
Takashi Iwai746df942006-05-15 19:49:05 +0200768 mutex_unlock(&snd_card_mutex);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100769 init_info_for_card(card);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100770#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (snd_mixer_oss_notify_callback)
772 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
773#endif
774 return 0;
775}
776
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200777EXPORT_SYMBOL(snd_card_register);
778
Jie Yangcd6a6502015-05-27 19:45:45 +0800779#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia381a7a2005-11-17 15:55:49 +0100780static void snd_card_info_read(struct snd_info_entry *entry,
781 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100784 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200787 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if ((card = snd_cards[idx]) != NULL) {
789 count++;
Clemens Ladischd0015442005-11-20 14:09:05 +0100790 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 idx,
792 card->id,
793 card->driver,
794 card->shortname);
Clemens Ladischd0015442005-11-20 14:09:05 +0100795 snd_iprintf(buffer, " %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 card->longname);
797 }
Takashi Iwai746df942006-05-15 19:49:05 +0200798 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 if (!count)
801 snd_iprintf(buffer, "--- no soundcards ---\n");
802}
803
Takashi Iwaie28563c2005-12-01 10:42:42 +0100804#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai512bbd62005-11-17 13:51:18 +0100805void snd_card_info_read_oss(struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100808 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200811 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if ((card = snd_cards[idx]) != NULL) {
813 count++;
814 snd_iprintf(buffer, "%s\n", card->longname);
815 }
Takashi Iwai746df942006-05-15 19:49:05 +0200816 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 if (!count) {
819 snd_iprintf(buffer, "--- no soundcards ---\n");
820 }
821}
822
823#endif
824
825#ifdef MODULE
Takashi Iwai512bbd62005-11-17 13:51:18 +0100826static void snd_card_module_info_read(struct snd_info_entry *entry,
827 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 int idx;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100830 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 for (idx = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200833 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if ((card = snd_cards[idx]) != NULL)
Clemens Ladischd0015442005-11-20 14:09:05 +0100835 snd_iprintf(buffer, "%2i %s\n",
836 idx, card->module->name);
Takashi Iwai746df942006-05-15 19:49:05 +0200837 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839}
840#endif
841
842int __init snd_card_info_init(void)
843{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100844 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200847 if (! entry)
848 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 entry->c.text.read = snd_card_info_read;
Takashi Iwaib591b6e2015-04-22 22:29:10 +0200850 if (snd_info_register(entry) < 0)
851 return -ENOMEM; /* freed in error path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853#ifdef MODULE
854 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
Takashi Iwaib591b6e2015-04-22 22:29:10 +0200855 if (!entry)
856 return -ENOMEM;
857 entry->c.text.read = snd_card_module_info_read;
858 if (snd_info_register(entry) < 0)
859 return -ENOMEM; /* freed in error path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860#endif
861
862 return 0;
863}
Jie Yangcd6a6502015-05-27 19:45:45 +0800864#endif /* CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/**
867 * snd_component_add - add a component string
868 * @card: soundcard structure
869 * @component: the component id string
870 *
871 * This function adds the component id string to the supported list.
872 * The component can be referred from the alsa-lib.
873 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100874 * Return: Zero otherwise a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
876
Takashi Iwai512bbd62005-11-17 13:51:18 +0100877int snd_component_add(struct snd_card *card, const char *component)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 char *ptr;
880 int len = strlen(component);
881
882 ptr = strstr(card->components, component);
883 if (ptr != NULL) {
884 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
885 return 1;
886 }
887 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
888 snd_BUG();
889 return -ENOMEM;
890 }
891 if (card->components[0] != '\0')
892 strcat(card->components, " ");
893 strcat(card->components, component);
894 return 0;
895}
896
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200897EXPORT_SYMBOL(snd_component_add);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899/**
900 * snd_card_file_add - add the file to the file list of the card
901 * @card: soundcard structure
902 * @file: file pointer
903 *
904 * This function adds the file to the file linked-list of the card.
905 * This linked-list is used to keep tracking the connection state,
906 * and to avoid the release of busy resources by hotplug.
907 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100908 * Return: zero or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100910int snd_card_file_add(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct snd_monitor_file *mfile;
913
914 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
915 if (mfile == NULL)
916 return -ENOMEM;
917 mfile->file = file;
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200918 mfile->disconnected_f_op = NULL;
Takashi Iwaia45e3d62011-03-24 09:50:15 +0100919 INIT_LIST_HEAD(&mfile->shutdown_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 spin_lock(&card->files_lock);
921 if (card->shutdown) {
922 spin_unlock(&card->files_lock);
923 kfree(mfile);
924 return -ENODEV;
925 }
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100926 list_add(&mfile->list, &card->files_list);
Takashi Iwaif2464062014-01-29 12:13:43 +0100927 get_device(&card->card_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 spin_unlock(&card->files_lock);
929 return 0;
930}
931
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200932EXPORT_SYMBOL(snd_card_file_add);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/**
935 * snd_card_file_remove - remove the file from the file list
936 * @card: soundcard structure
937 * @file: file pointer
938 *
939 * This function removes the file formerly added to the card via
940 * snd_card_file_add() function.
Takashi Iwai2b29b132006-06-23 14:38:26 +0200941 * If all files are removed and snd_card_free_when_closed() was
942 * called beforehand, it processes the pending release of
943 * resources.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100945 * Return: Zero or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100947int snd_card_file_remove(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100949 struct snd_monitor_file *mfile, *found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 spin_lock(&card->files_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100952 list_for_each_entry(mfile, &card->files_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (mfile->file == file) {
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100954 list_del(&mfile->list);
Takashi Iwaia45e3d62011-03-24 09:50:15 +0100955 spin_lock(&shutdown_lock);
956 list_del(&mfile->shutdown_list);
957 spin_unlock(&shutdown_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100958 if (mfile->disconnected_f_op)
959 fops_put(mfile->disconnected_f_op);
960 found = mfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Takashi Iwaic4614822006-06-23 14:38:23 +0200964 spin_unlock(&card->files_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100965 if (!found) {
Takashi Iwaif2f93072014-02-04 18:21:03 +0100966 dev_err(card->dev, "card file remove problem (%p)\n", file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return -ENOENT;
968 }
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100969 kfree(found);
Takashi Iwaif2464062014-01-29 12:13:43 +0100970 put_device(&card->card_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return 0;
972}
973
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200974EXPORT_SYMBOL(snd_card_file_remove);
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976#ifdef CONFIG_PM
977/**
978 * snd_power_wait - wait until the power-state is changed.
979 * @card: soundcard structure
980 * @power_state: expected power state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 *
982 * Waits until the power-state is changed.
983 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100984 * Return: Zero if successful, or a negative error code.
985 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * Note: the power lock must be active before call.
987 */
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200988int snd_power_wait(struct snd_card *card, unsigned int power_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 wait_queue_t wait;
991 int result = 0;
992
993 /* fastpath */
994 if (snd_power_get_state(card) == power_state)
995 return 0;
996 init_waitqueue_entry(&wait, current);
997 add_wait_queue(&card->power_sleep, &wait);
998 while (1) {
999 if (card->shutdown) {
1000 result = -ENODEV;
1001 break;
1002 }
1003 if (snd_power_get_state(card) == power_state)
1004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 set_current_state(TASK_UNINTERRUPTIBLE);
1006 snd_power_unlock(card);
1007 schedule_timeout(30 * HZ);
1008 snd_power_lock(card);
1009 }
1010 remove_wait_queue(&card->power_sleep, &wait);
1011 return result;
1012}
1013
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001014EXPORT_SYMBOL(snd_power_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015#endif /* CONFIG_PM */