blob: c81664835cfa916328eccf88f0605608a82a56fe [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
Sudheer Papothi4cd40db2016-01-29 00:36:30 +053061#define SND_CARD_STATE_MAX_LEN 16
62
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020063/* return non-zero if the given index is reserved for the given
Takashi Iwai304cd072007-10-26 15:10:15 +020064 * module via slots option
65 */
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020066static int module_slot_match(struct module *module, int idx)
Takashi Iwai304cd072007-10-26 15:10:15 +020067{
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020068 int match = 1;
Takashi Iwai304cd072007-10-26 15:10:15 +020069#ifdef MODULE
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020070 const char *s1, *s2;
71
Takashi Iwai60f6fef2013-10-28 12:54:52 +010072 if (!module || !*module->name || !slots[idx])
Takashi Iwai304cd072007-10-26 15:10:15 +020073 return 0;
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020074
75 s1 = module->name;
76 s2 = slots[idx];
77 if (*s2 == '!') {
78 match = 0; /* negative match */
79 s2++;
80 }
Takashi Iwai304cd072007-10-26 15:10:15 +020081 /* compare module name strings
82 * hyphens are handled as equivalent with underscore
83 */
84 for (;;) {
85 char c1 = *s1++;
86 char c2 = *s2++;
87 if (c1 == '-')
88 c1 = '_';
89 if (c2 == '-')
90 c2 = '_';
91 if (c1 != c2)
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020092 return !match;
Takashi Iwai304cd072007-10-26 15:10:15 +020093 if (!c1)
94 break;
95 }
Takashi Iwaia93bbaa2008-05-27 17:59:24 +020096#endif /* MODULE */
97 return match;
Takashi Iwai304cd072007-10-26 15:10:15 +020098}
99
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100100#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Takashi Iwai512bbd62005-11-17 13:51:18 +0100101int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200102EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif
104
Jie Yangcd6a6502015-05-27 19:45:45 +0800105#ifdef CONFIG_SND_PROC_FS
Takashi Iwai512bbd62005-11-17 13:51:18 +0100106static void snd_card_id_read(struct snd_info_entry *entry,
107 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 snd_iprintf(buffer, "%s\n", entry->card->id);
110}
111
Sudheer Papothi4cd40db2016-01-29 00:36:30 +0530112static ssize_t snd_card_state_read(struct snd_info_entry *entry,
113 void *file_private_data, struct file *file,
114 char __user *buf, size_t count, loff_t pos)
115{
116 int len;
117 char buffer[SND_CARD_STATE_MAX_LEN];
118
119 /* make sure offline is updated prior to wake up */
120 rmb();
121 len = snprintf(buffer, sizeof(buffer), "%s\n",
122 entry->card->offline ? "OFFLINE" : "ONLINE");
123 return simple_read_from_buffer(buf, count, &pos, buffer, len);
124}
125
126static unsigned int snd_card_state_poll(struct snd_info_entry *entry,
127 void *private_data, struct file *file,
128 poll_table *wait)
129{
130 poll_wait(file, &entry->card->offline_poll_wait, wait);
131 if (xchg(&entry->card->offline_change, 0))
132 return POLLIN | POLLPRI | POLLRDNORM;
133 else
134 return 0;
135}
136
137static struct snd_info_entry_ops snd_card_state_proc_ops = {
138 .read = snd_card_state_read,
139 .poll = snd_card_state_poll,
140};
141
Takashi Iwai2471b6c2015-05-18 09:20:24 +0200142static int init_info_for_card(struct snd_card *card)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100143{
Sudheer Papothi4cd40db2016-01-29 00:36:30 +0530144 struct snd_info_entry *entry, *entry_state;
Takashi Iwaie28563c2005-12-01 10:42:42 +0100145
Takashi Iwai2471b6c2015-05-18 09:20:24 +0200146 entry = snd_info_create_card_entry(card, "id", card->proc_root);
147 if (!entry) {
Takashi Iwaif2f93072014-02-04 18:21:03 +0100148 dev_dbg(card->dev, "unable to create card entry\n");
Colin Ian King0755e742015-06-29 17:10:22 +0100149 return -ENOMEM;
Takashi Iwaie28563c2005-12-01 10:42:42 +0100150 }
Takashi Iwaie28563c2005-12-01 10:42:42 +0100151 entry->c.text.read = snd_card_id_read;
Takashi Iwaie28563c2005-12-01 10:42:42 +0100152 card->proc_id = entry;
Takashi Iwai2471b6c2015-05-18 09:20:24 +0200153
Sudheer Papothi4cd40db2016-01-29 00:36:30 +0530154 entry_state = snd_info_create_card_entry(card, "state",
155 card->proc_root);
156 if (!entry_state) {
157 dev_dbg(card->dev, "unable to create card entry state\n");
158 card->proc_id = NULL;
159 return -ENOMEM;
160 }
161 entry_state->size = SND_CARD_STATE_MAX_LEN;
162 entry_state->content = SNDRV_INFO_CONTENT_DATA;
163 entry_state->c.ops = &snd_card_state_proc_ops;
164
Takashi Iwai2471b6c2015-05-18 09:20:24 +0200165 return snd_info_card_register(card);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100166}
Jie Yangcd6a6502015-05-27 19:45:45 +0800167#else /* !CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100168#define init_info_for_card(card)
169#endif
170
Takashi Iwaideb65962014-01-23 11:02:15 +0100171static int check_empty_slot(struct module *module, int slot)
172{
173 return !slots[slot] || !*slots[slot];
174}
175
176/* return an empty slot number (>= 0) found in the given bitmask @mask.
177 * @mask == -1 == 0xffffffff means: take any free slot up to 32
178 * when no slot is available, return the original @mask as is.
179 */
180static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int),
181 struct module *module)
182{
183 int slot;
184
185 for (slot = 0; slot < SNDRV_CARDS; slot++) {
186 if (slot < 32 && !(mask & (1U << slot)))
187 continue;
188 if (!test_bit(slot, snd_cards_lock)) {
189 if (check(module, slot))
190 return slot; /* found */
191 }
192 }
193 return mask; /* unchanged */
194}
195
Takashi Iwai4b440be2015-01-29 21:32:47 +0100196/* the default release callback set in snd_device_initialize() below;
197 * this is just NOP for now, as almost all jobs are already done in
198 * dev_free callback of snd_device chain instead.
199 */
200static void default_release(struct device *dev)
201{
202}
203
204/**
205 * snd_device_initialize - Initialize struct device for sound devices
206 * @dev: device to initialize
207 * @card: card to assign, optional
208 */
209void snd_device_initialize(struct device *dev, struct snd_card *card)
210{
211 device_initialize(dev);
212 if (card)
213 dev->parent = &card->card_dev;
214 dev->class = sound_class;
215 dev->release = default_release;
216}
217EXPORT_SYMBOL_GPL(snd_device_initialize);
218
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100219static int snd_card_do_free(struct snd_card *card);
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100220static const struct attribute_group card_dev_attr_group;
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100221
222static void release_card_device(struct device *dev)
223{
224 snd_card_do_free(dev_to_snd_card(dev));
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/**
Takashi Iwai393aa9c2014-01-29 12:51:12 +0100228 * snd_card_new - create and initialize a soundcard structure
229 * @parent: the parent device object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
231 * @xid: card identification (ASCII string)
232 * @module: top level module for locking
233 * @extra_size: allocate this extra size after the main soundcard structure
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100234 * @card_ret: the pointer to store the created card instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 *
236 * Creates and initializes a soundcard structure.
237 *
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100238 * The function allocates snd_card instance via kzalloc with the given
239 * space for the driver to use freely. The allocated struct is stored
240 * in the given card_ret pointer.
241 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100242 * Return: Zero if successful or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
Takashi Iwai393aa9c2014-01-29 12:51:12 +0100244int snd_card_new(struct device *parent, int idx, const char *xid,
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100245 struct module *module, int extra_size,
246 struct snd_card **card_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100248 struct snd_card *card;
Takashi Iwaideb65962014-01-23 11:02:15 +0100249 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100251 if (snd_BUG_ON(!card_ret))
252 return -EINVAL;
253 *card_ret = NULL;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (extra_size < 0)
256 extra_size = 0;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200257 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100258 if (!card)
259 return -ENOMEM;
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100260 if (extra_size > 0)
261 card->private_data = (char *)card + sizeof(struct snd_card);
Jaroslav Kysela10a8ebb2009-06-02 12:02:38 +0200262 if (xid)
Jaroslav Kysela5fdc18d2009-06-04 00:12:18 +0200263 strlcpy(card->id, xid, sizeof(card->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 err = 0;
Takashi Iwai746df942006-05-15 19:49:05 +0200265 mutex_lock(&snd_card_mutex);
Takashi Iwaideb65962014-01-23 11:02:15 +0100266 if (idx < 0) /* first check the matching module-name slot */
267 idx = get_slot_from_bitmask(idx, module_slot_match, module);
268 if (idx < 0) /* if not matched, assign an empty slot */
269 idx = get_slot_from_bitmask(idx, check_empty_slot, module);
Takashi Iwaia93bbaa2008-05-27 17:59:24 +0200270 if (idx < 0)
271 err = -ENODEV;
272 else if (idx < snd_ecards_limit) {
Takashi Iwai7bb24912013-05-15 08:46:39 +0200273 if (test_bit(idx, snd_cards_lock))
Takashi Iwaia93bbaa2008-05-27 17:59:24 +0200274 err = -EBUSY; /* invalid */
275 } else if (idx >= SNDRV_CARDS)
276 err = -ENODEV;
277 if (err < 0) {
Takashi Iwai746df942006-05-15 19:49:05 +0200278 mutex_unlock(&snd_card_mutex);
Takashi Iwaif2f93072014-02-04 18:21:03 +0100279 dev_err(parent, "cannot find the slot for index %d (range 0-%i), error: %d\n",
Oliver Neukum5c33dd72007-01-16 17:49:21 +0100280 idx, snd_ecards_limit - 1, err);
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100281 kfree(card);
282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Takashi Iwai7bb24912013-05-15 08:46:39 +0200284 set_bit(idx, snd_cards_lock); /* lock it */
Takashi Iwaia93bbaa2008-05-27 17:59:24 +0200285 if (idx >= snd_ecards_limit)
286 snd_ecards_limit = idx + 1; /* increase the limit */
Takashi Iwai746df942006-05-15 19:49:05 +0200287 mutex_unlock(&snd_card_mutex);
Takashi Iwai393aa9c2014-01-29 12:51:12 +0100288 card->dev = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 card->number = idx;
290 card->module = module;
291 INIT_LIST_HEAD(&card->devices);
292 init_rwsem(&card->controls_rwsem);
293 rwlock_init(&card->ctl_files_rwlock);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +0200294 mutex_init(&card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 INIT_LIST_HEAD(&card->controls);
296 INIT_LIST_HEAD(&card->ctl_files);
297 spin_lock_init(&card->files_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100298 INIT_LIST_HEAD(&card->files_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#ifdef CONFIG_PM
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100300 mutex_init(&card->power_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 init_waitqueue_head(&card->power_sleep);
302#endif
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100303
Sudheer Papothi4cd40db2016-01-29 00:36:30 +0530304 init_waitqueue_head(&card->offline_poll_wait);
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100305 device_initialize(&card->card_dev);
306 card->card_dev.parent = parent;
307 card->card_dev.class = sound_class;
308 card->card_dev.release = release_card_device;
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100309 card->card_dev.groups = card->dev_groups;
310 card->dev_groups[0] = &card_dev_attr_group;
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100311 err = kobject_set_name(&card->card_dev.kobj, "card%d", idx);
312 if (err < 0)
313 goto __error;
314
Heiner Kallweitde653602015-12-22 19:09:05 +0100315 snprintf(card->irq_descr, sizeof(card->irq_descr), "%s:%s",
316 dev_driver_string(card->dev), dev_name(&card->card_dev));
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* the control interface cannot be accessed from the user space until */
319 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100320 err = snd_ctl_create(card);
321 if (err < 0) {
Takashi Iwaif2f93072014-02-04 18:21:03 +0100322 dev_err(parent, "unable to register control minors\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto __error;
324 }
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100325 err = snd_info_card_create(card);
326 if (err < 0) {
Takashi Iwaif2f93072014-02-04 18:21:03 +0100327 dev_err(parent, "unable to create card info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 goto __error_ctl;
329 }
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100330 *card_ret = card;
331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 __error_ctl:
Takashi Iwai289ca022014-01-29 15:53:35 +0100334 snd_device_free_all(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 __error:
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100336 put_device(&card->card_dev);
Takashi Iwai53fb1e62008-12-28 16:32:08 +0100337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
Takashi Iwai393aa9c2014-01-29 12:51:12 +0100339EXPORT_SYMBOL(snd_card_new);
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200340
Takashi Iwai746df942006-05-15 19:49:05 +0200341/* return non-zero if a card is already locked */
342int snd_card_locked(int card)
343{
344 int locked;
345
346 mutex_lock(&snd_card_mutex);
Takashi Iwai7bb24912013-05-15 08:46:39 +0200347 locked = test_bit(card, snd_cards_lock);
Takashi Iwai746df942006-05-15 19:49:05 +0200348 mutex_unlock(&snd_card_mutex);
349 return locked;
350}
351
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100352static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
353{
354 return -ENODEV;
355}
356
357static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
358 size_t count, loff_t *offset)
359{
360 return -ENODEV;
361}
362
363static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
364 size_t count, loff_t *offset)
365{
366 return -ENODEV;
367}
368
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200369static int snd_disconnect_release(struct inode *inode, struct file *file)
370{
371 struct snd_monitor_file *df = NULL, *_df;
372
373 spin_lock(&shutdown_lock);
374 list_for_each_entry(_df, &shutdown_files, shutdown_list) {
375 if (_df->file == file) {
376 df = _df;
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100377 list_del_init(&df->shutdown_list);
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200378 break;
379 }
380 }
381 spin_unlock(&shutdown_lock);
382
Al Viro233e70f2008-10-31 23:28:30 +0000383 if (likely(df)) {
384 if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync)
385 df->disconnected_f_op->fasync(-1, file, 0);
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200386 return df->disconnected_f_op->release(inode, file);
Al Viro233e70f2008-10-31 23:28:30 +0000387 }
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200388
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800389 panic("%s(%p, %p) failed!", __func__, inode, file);
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
393{
394 return POLLERR | POLLNVAL;
395}
396
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100397static long snd_disconnect_ioctl(struct file *file,
398 unsigned int cmd, unsigned long arg)
399{
400 return -ENODEV;
401}
402
403static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
404{
405 return -ENODEV;
406}
407
408static int snd_disconnect_fasync(int fd, struct file *file, int on)
409{
410 return -ENODEV;
411}
412
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800413static const struct file_operations snd_shutdown_f_ops =
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200414{
415 .owner = THIS_MODULE,
416 .llseek = snd_disconnect_llseek,
417 .read = snd_disconnect_read,
418 .write = snd_disconnect_write,
419 .release = snd_disconnect_release,
420 .poll = snd_disconnect_poll,
421 .unlocked_ioctl = snd_disconnect_ioctl,
422#ifdef CONFIG_COMPAT
423 .compat_ioctl = snd_disconnect_ioctl,
424#endif
425 .mmap = snd_disconnect_mmap,
426 .fasync = snd_disconnect_fasync
427};
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/**
430 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
431 * @card: soundcard structure
432 *
433 * Disconnects all APIs from the file-operations (user space).
434 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100435 * Return: Zero, otherwise a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *
437 * Note: The current implementation replaces all active file->f_op with special
438 * dummy file operations (they do nothing except release).
439 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100440int snd_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct snd_monitor_file *mfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Takashi Iwaif18638d2008-04-17 12:52:02 +0200444 if (!card)
445 return -EINVAL;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 spin_lock(&card->files_lock);
448 if (card->shutdown) {
449 spin_unlock(&card->files_lock);
450 return 0;
451 }
452 card->shutdown = 1;
453 spin_unlock(&card->files_lock);
454
455 /* phase 1: disable fops (user space) operations for ALSA API */
Takashi Iwai746df942006-05-15 19:49:05 +0200456 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 snd_cards[card->number] = NULL;
Takashi Iwai7bb24912013-05-15 08:46:39 +0200458 clear_bit(card->number, snd_cards_lock);
Takashi Iwai746df942006-05-15 19:49:05 +0200459 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* phase 2: replace file->f_op with special dummy operations */
462
463 spin_lock(&card->files_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100464 list_for_each_entry(mfile, &card->files_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* it's critical part, use endless loop */
466 /* we have no room to fail */
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200467 mfile->disconnected_f_op = mfile->file->f_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200469 spin_lock(&shutdown_lock);
470 list_add(&mfile->shutdown_list, &shutdown_files);
471 spin_unlock(&shutdown_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200473 mfile->file->f_op = &snd_shutdown_f_ops;
Miguel Botonbc9abce2008-01-13 12:03:53 +0100474 fops_get(mfile->file->f_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 spin_unlock(&card->files_lock);
477
478 /* phase 3: notify all connected devices about disconnection */
479 /* at this point, they cannot respond to any calls except release() */
480
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100481#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (snd_mixer_oss_notify_callback)
483 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
484#endif
485
486 /* notify all devices that we are disconnected */
Takashi Iwaie086e302015-02-27 18:01:22 +0100487 snd_device_disconnect_all(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Takashi Iwai746d4a02006-06-23 14:37:59 +0200489 snd_info_card_disconnect(card);
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100490 if (card->registered) {
491 device_del(&card->card_dev);
492 card->registered = false;
Takashi Iwai73d38b12008-04-17 12:50:47 +0200493 }
Takashi Iwaif18638d2008-04-17 12:52:02 +0200494#ifdef CONFIG_PM
495 wake_up(&card->power_sleep);
496#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return 0;
498}
499
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200500EXPORT_SYMBOL(snd_card_disconnect);
501
Takashi Iwaic4614822006-06-23 14:38:23 +0200502static int snd_card_do_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100504#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (snd_mixer_oss_notify_callback)
506 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
507#endif
Takashi Iwai72620d62014-02-04 11:36:11 +0100508 snd_device_free_all(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (card->private_free)
510 card->private_free(card);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200511 snd_info_free_entry(card->proc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (snd_info_card_free(card) < 0) {
Takashi Iwaif2f93072014-02-04 18:21:03 +0100513 dev_warn(card->dev, "unable to free card info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Not fatal error */
515 }
Takashi Iwaif2464062014-01-29 12:13:43 +0100516 if (card->release_completion)
517 complete(card->release_completion);
Takashi Iwaic4614822006-06-23 14:38:23 +0200518 kfree(card);
519 return 0;
520}
521
Takashi Iwaieb9c38d2014-10-30 15:19:43 +0100522/**
523 * snd_card_free_when_closed - Disconnect the card, free it later eventually
524 * @card: soundcard structure
525 *
526 * Unlike snd_card_free(), this function doesn't try to release the card
527 * resource immediately, but tries to disconnect at first. When the card
528 * is still in use, the function returns before freeing the resources.
529 * The card resources will be freed when the refcount gets to zero.
530 */
Takashi Iwaic4614822006-06-23 14:38:23 +0200531int snd_card_free_when_closed(struct snd_card *card)
532{
Takashi Iwaif18638d2008-04-17 12:52:02 +0200533 int ret = snd_card_disconnect(card);
Takashi Iwaic4614822006-06-23 14:38:23 +0200534 if (ret)
535 return ret;
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100536 put_device(&card->card_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
538}
Takashi Iwaif2464062014-01-29 12:13:43 +0100539EXPORT_SYMBOL(snd_card_free_when_closed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Takashi Iwaieb9c38d2014-10-30 15:19:43 +0100541/**
542 * snd_card_free - frees given soundcard structure
543 * @card: soundcard structure
544 *
545 * This function releases the soundcard structure and the all assigned
546 * devices automatically. That is, you don't have to release the devices
547 * by yourself.
548 *
549 * This function waits until the all resources are properly released.
550 *
551 * Return: Zero. Frees all associated devices and frees the control
552 * interface associated to given soundcard.
553 */
Takashi Iwaif2464062014-01-29 12:13:43 +0100554int snd_card_free(struct snd_card *card)
555{
556 struct completion released;
557 int ret;
558
559 init_completion(&released);
560 card->release_completion = &released;
561 ret = snd_card_free_when_closed(card);
562 if (ret)
563 return ret;
564 /* wait, until all devices are ready for the free operation */
565 wait_for_completion(&released);
566 return 0;
567}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200568EXPORT_SYMBOL(snd_card_free);
569
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100570/* retrieve the last word of shortname or longname */
571static const char *retrieve_id_from_card_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100573 const char *spos = name;
574
575 while (*name) {
576 if (isspace(*name) && isalnum(name[1]))
577 spos = name + 1;
578 name++;
579 }
580 return spos;
581}
582
583/* return true if the given id string doesn't conflict any other card ids */
584static bool card_id_ok(struct snd_card *card, const char *id)
585{
586 int i;
587 if (!snd_info_check_reserved_words(id))
588 return false;
589 for (i = 0; i < snd_ecards_limit; i++) {
590 if (snd_cards[i] && snd_cards[i] != card &&
591 !strcmp(snd_cards[i]->id, id))
592 return false;
593 }
594 return true;
595}
596
597/* copy to card->id only with valid letters from nid */
598static void copy_valid_id_string(struct snd_card *card, const char *src,
599 const char *nid)
600{
601 char *id = card->id;
602
603 while (*nid && !isalnum(*nid))
604 nid++;
605 if (isdigit(*nid))
606 *id++ = isalpha(*src) ? *src : 'D';
607 while (*nid && (size_t)(id - card->id) < sizeof(card->id) - 1) {
608 if (isalnum(*nid))
609 *id++ = *nid;
610 nid++;
611 }
612 *id = 0;
613}
614
615/* Set card->id from the given string
616 * If the string conflicts with other ids, add a suffix to make it unique.
617 */
618static void snd_card_set_id_no_lock(struct snd_card *card, const char *src,
619 const char *nid)
620{
621 int len, loops;
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100622 bool is_default = false;
Jaroslav Kysela10a8ebb2009-06-02 12:02:38 +0200623 char *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100625 copy_valid_id_string(card, src, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 id = card->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100628 again:
629 /* use "Default" for obviously invalid strings
630 * ("card" conflicts with proc directories)
631 */
632 if (!*id || !strncmp(id, "card", 4)) {
Takashi Iwaid8e4f9a2011-04-04 12:43:23 +0200633 strcpy(id, "Default");
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100634 is_default = true;
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Takashi Iwai8edbb192013-05-24 16:30:39 +0200637 len = strlen(id);
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100638 for (loops = 0; loops < SNDRV_CARDS; loops++) {
Takashi Iwai8edbb192013-05-24 16:30:39 +0200639 char *spos;
640 char sfxstr[5]; /* "_012" */
641 int sfxlen;
642
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100643 if (card_id_ok(card, id))
644 return; /* OK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Takashi Iwai8edbb192013-05-24 16:30:39 +0200646 /* Add _XYZ suffix */
647 sprintf(sfxstr, "_%X", loops + 1);
648 sfxlen = strlen(sfxstr);
649 if (len + sfxlen >= sizeof(card->id))
650 spos = id + sizeof(card->id) - sfxlen - 1;
651 else
652 spos = id + len;
653 strcpy(spos, sfxstr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100655 /* fallback to the default id */
656 if (!is_default) {
657 *id = 0;
658 goto again;
659 }
660 /* last resort... */
Takashi Iwaif2f93072014-02-04 18:21:03 +0100661 dev_err(card->dev, "unable to set card id (%s)\n", id);
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100662 if (card->proc_root->name)
Takashi Iwai97f44f52013-10-29 15:20:06 +0100663 strlcpy(card->id, card->proc_root->name, sizeof(card->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Mark Brown872c7822009-06-03 20:43:29 +0100666/**
667 * snd_card_set_id - set card identification name
668 * @card: soundcard structure
669 * @nid: new identification string
670 *
671 * This function sets the card identification and checks for name
672 * collisions.
673 */
674void snd_card_set_id(struct snd_card *card, const char *nid)
675{
Jaroslav Kysela5fdc18d2009-06-04 00:12:18 +0200676 /* check if user specified own card->id */
677 if (card->id[0] != '\0')
678 return;
679 mutex_lock(&snd_card_mutex);
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100680 snd_card_set_id_no_lock(card, nid, nid);
Jaroslav Kysela5fdc18d2009-06-04 00:12:18 +0200681 mutex_unlock(&snd_card_mutex);
Mark Brown872c7822009-06-03 20:43:29 +0100682}
Jaroslav Kysela10a8ebb2009-06-02 12:02:38 +0200683EXPORT_SYMBOL(snd_card_set_id);
684
Jaroslav Kysela9fb61982008-11-11 16:51:02 +0100685static ssize_t
686card_id_show_attr(struct device *dev,
687 struct device_attribute *attr, char *buf)
688{
Takashi Iwaib203dba2014-02-19 11:18:10 +0100689 struct snd_card *card = container_of(dev, struct snd_card, card_dev);
690 return snprintf(buf, PAGE_SIZE, "%s\n", card->id);
Jaroslav Kysela9fb61982008-11-11 16:51:02 +0100691}
692
693static ssize_t
694card_id_store_attr(struct device *dev, struct device_attribute *attr,
695 const char *buf, size_t count)
696{
Takashi Iwaib203dba2014-02-19 11:18:10 +0100697 struct snd_card *card = container_of(dev, struct snd_card, card_dev);
Jaroslav Kysela9fb61982008-11-11 16:51:02 +0100698 char buf1[sizeof(card->id)];
699 size_t copy = count > sizeof(card->id) - 1 ?
700 sizeof(card->id) - 1 : count;
701 size_t idx;
702 int c;
703
704 for (idx = 0; idx < copy; idx++) {
705 c = buf[idx];
706 if (!isalnum(c) && c != '_' && c != '-')
707 return -EINVAL;
708 }
709 memcpy(buf1, buf, copy);
710 buf1[copy] = '\0';
711 mutex_lock(&snd_card_mutex);
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100712 if (!card_id_ok(NULL, buf1)) {
Jaroslav Kysela9fb61982008-11-11 16:51:02 +0100713 mutex_unlock(&snd_card_mutex);
714 return -EEXIST;
715 }
Jaroslav Kysela9fb61982008-11-11 16:51:02 +0100716 strcpy(card->id, buf1);
Jaroslav Kyselac2eb9c42008-11-12 16:31:37 +0100717 snd_info_card_id_change(card);
Jaroslav Kysela9fb61982008-11-11 16:51:02 +0100718 mutex_unlock(&snd_card_mutex);
719
720 return count;
721}
722
Takashi Iwai34356db2014-01-29 11:54:10 +0100723static DEVICE_ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr);
Jaroslav Kysela9fb61982008-11-11 16:51:02 +0100724
725static ssize_t
726card_number_show_attr(struct device *dev,
727 struct device_attribute *attr, char *buf)
728{
Takashi Iwaib203dba2014-02-19 11:18:10 +0100729 struct snd_card *card = container_of(dev, struct snd_card, card_dev);
730 return snprintf(buf, PAGE_SIZE, "%i\n", card->number);
Jaroslav Kysela9fb61982008-11-11 16:51:02 +0100731}
732
Takashi Iwai34356db2014-01-29 11:54:10 +0100733static DEVICE_ATTR(number, S_IRUGO, card_number_show_attr, NULL);
734
735static struct attribute *card_dev_attrs[] = {
736 &dev_attr_id.attr,
737 &dev_attr_number.attr,
738 NULL
739};
740
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100741static const struct attribute_group card_dev_attr_group = {
Takashi Iwai34356db2014-01-29 11:54:10 +0100742 .attrs = card_dev_attrs,
743};
744
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100745/**
746 * snd_card_add_dev_attr - Append a new sysfs attribute group to card
747 * @card: card instance
748 * @group: attribute group to append
749 */
750int snd_card_add_dev_attr(struct snd_card *card,
751 const struct attribute_group *group)
752{
753 int i;
754
755 /* loop for (arraysize-1) here to keep NULL at the last entry */
756 for (i = 0; i < ARRAY_SIZE(card->dev_groups) - 1; i++) {
757 if (!card->dev_groups[i]) {
758 card->dev_groups[i] = group;
759 return 0;
760 }
761 }
762
763 dev_err(card->dev, "Too many groups assigned\n");
764 return -ENOSPC;
Takashi Iwai34356db2014-01-29 11:54:10 +0100765};
Takashi Iwai6bbc7fe2015-01-30 12:27:43 +0100766EXPORT_SYMBOL_GPL(snd_card_add_dev_attr);
Jaroslav Kysela9fb61982008-11-11 16:51:02 +0100767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/**
769 * snd_card_register - register the soundcard
770 * @card: soundcard structure
771 *
772 * This function registers all the devices assigned to the soundcard.
773 * Until calling this, the ALSA control interface is blocked from the
774 * external accesses. Thus, you should call this function at the end
775 * of the initialization of the card.
776 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100777 * Return: Zero otherwise a negative error code if the registration failed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100779int snd_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200783 if (snd_BUG_ON(!card))
784 return -EINVAL;
Kay Sievers39aba962010-09-04 22:33:14 -0700785
Takashi Iwai8bfb1812014-01-29 11:46:11 +0100786 if (!card->registered) {
787 err = device_add(&card->card_dev);
788 if (err < 0)
789 return err;
790 card->registered = true;
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700791 }
Kay Sievers39aba962010-09-04 22:33:14 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if ((err = snd_device_register_all(card)) < 0)
794 return err;
Takashi Iwai746df942006-05-15 19:49:05 +0200795 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (snd_cards[card->number]) {
797 /* already registered */
Takashi Iwai746df942006-05-15 19:49:05 +0200798 mutex_unlock(&snd_card_mutex);
Takashi Iwai2471b6c2015-05-18 09:20:24 +0200799 return snd_info_card_register(card); /* register pending info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Takashi Iwaie7df2a32012-03-09 17:41:53 +0100801 if (*card->id) {
802 /* make a unique id name from the given string */
803 char tmpid[sizeof(card->id)];
804 memcpy(tmpid, card->id, sizeof(card->id));
805 snd_card_set_id_no_lock(card, tmpid, tmpid);
806 } else {
807 /* create an id from either shortname or longname */
808 const char *src;
809 src = *card->shortname ? card->shortname : card->longname;
810 snd_card_set_id_no_lock(card, src,
811 retrieve_id_from_card_name(src));
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 snd_cards[card->number] = card;
Takashi Iwai746df942006-05-15 19:49:05 +0200814 mutex_unlock(&snd_card_mutex);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100815 init_info_for_card(card);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100816#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (snd_mixer_oss_notify_callback)
818 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
819#endif
820 return 0;
821}
822
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200823EXPORT_SYMBOL(snd_card_register);
824
Jie Yangcd6a6502015-05-27 19:45:45 +0800825#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia381a7a2005-11-17 15:55:49 +0100826static void snd_card_info_read(struct snd_info_entry *entry,
827 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100830 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 for (idx = count = 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) {
835 count++;
Clemens Ladischd0015442005-11-20 14:09:05 +0100836 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 idx,
838 card->id,
839 card->driver,
840 card->shortname);
Clemens Ladischd0015442005-11-20 14:09:05 +0100841 snd_iprintf(buffer, " %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 card->longname);
843 }
Takashi Iwai746df942006-05-15 19:49:05 +0200844 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 if (!count)
847 snd_iprintf(buffer, "--- no soundcards ---\n");
848}
849
Takashi Iwaie28563c2005-12-01 10:42:42 +0100850#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai512bbd62005-11-17 13:51:18 +0100851void snd_card_info_read_oss(struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100854 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200857 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if ((card = snd_cards[idx]) != NULL) {
859 count++;
860 snd_iprintf(buffer, "%s\n", card->longname);
861 }
Takashi Iwai746df942006-05-15 19:49:05 +0200862 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 if (!count) {
865 snd_iprintf(buffer, "--- no soundcards ---\n");
866 }
867}
868
869#endif
870
871#ifdef MODULE
Takashi Iwai512bbd62005-11-17 13:51:18 +0100872static void snd_card_module_info_read(struct snd_info_entry *entry,
873 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 int idx;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100876 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 for (idx = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200879 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if ((card = snd_cards[idx]) != NULL)
Clemens Ladischd0015442005-11-20 14:09:05 +0100881 snd_iprintf(buffer, "%2i %s\n",
882 idx, card->module->name);
Takashi Iwai746df942006-05-15 19:49:05 +0200883 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885}
886#endif
887
888int __init snd_card_info_init(void)
889{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100890 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200893 if (! entry)
894 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 entry->c.text.read = snd_card_info_read;
Takashi Iwaib591b6e2015-04-22 22:29:10 +0200896 if (snd_info_register(entry) < 0)
897 return -ENOMEM; /* freed in error path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899#ifdef MODULE
900 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
Takashi Iwaib591b6e2015-04-22 22:29:10 +0200901 if (!entry)
902 return -ENOMEM;
903 entry->c.text.read = snd_card_module_info_read;
904 if (snd_info_register(entry) < 0)
905 return -ENOMEM; /* freed in error path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#endif
907
908 return 0;
909}
Jie Yangcd6a6502015-05-27 19:45:45 +0800910#endif /* CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912/**
913 * snd_component_add - add a component string
914 * @card: soundcard structure
915 * @component: the component id string
916 *
917 * This function adds the component id string to the supported list.
918 * The component can be referred from the alsa-lib.
919 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100920 * Return: Zero otherwise a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
922
Takashi Iwai512bbd62005-11-17 13:51:18 +0100923int snd_component_add(struct snd_card *card, const char *component)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 char *ptr;
926 int len = strlen(component);
927
928 ptr = strstr(card->components, component);
929 if (ptr != NULL) {
930 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
931 return 1;
932 }
933 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
934 snd_BUG();
935 return -ENOMEM;
936 }
937 if (card->components[0] != '\0')
938 strcat(card->components, " ");
939 strcat(card->components, component);
940 return 0;
941}
942
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200943EXPORT_SYMBOL(snd_component_add);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/**
946 * snd_card_file_add - add the file to the file list of the card
947 * @card: soundcard structure
948 * @file: file pointer
949 *
950 * This function adds the file to the file linked-list of the card.
951 * This linked-list is used to keep tracking the connection state,
952 * and to avoid the release of busy resources by hotplug.
953 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100954 * Return: zero or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100956int snd_card_file_add(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct snd_monitor_file *mfile;
959
960 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
961 if (mfile == NULL)
962 return -ENOMEM;
963 mfile->file = file;
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200964 mfile->disconnected_f_op = NULL;
Takashi Iwaia45e3d62011-03-24 09:50:15 +0100965 INIT_LIST_HEAD(&mfile->shutdown_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 spin_lock(&card->files_lock);
967 if (card->shutdown) {
968 spin_unlock(&card->files_lock);
969 kfree(mfile);
970 return -ENODEV;
971 }
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100972 list_add(&mfile->list, &card->files_list);
Takashi Iwaif2464062014-01-29 12:13:43 +0100973 get_device(&card->card_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 spin_unlock(&card->files_lock);
975 return 0;
976}
977
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200978EXPORT_SYMBOL(snd_card_file_add);
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/**
981 * snd_card_file_remove - remove the file from the file list
982 * @card: soundcard structure
983 * @file: file pointer
984 *
985 * This function removes the file formerly added to the card via
986 * snd_card_file_add() function.
Takashi Iwai2b29b132006-06-23 14:38:26 +0200987 * If all files are removed and snd_card_free_when_closed() was
988 * called beforehand, it processes the pending release of
989 * resources.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100991 * Return: Zero or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100993int snd_card_file_remove(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100995 struct snd_monitor_file *mfile, *found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 spin_lock(&card->files_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +0100998 list_for_each_entry(mfile, &card->files_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (mfile->file == file) {
Takashi Iwai118dd6b2009-02-23 16:35:21 +01001000 list_del(&mfile->list);
Takashi Iwaia45e3d62011-03-24 09:50:15 +01001001 spin_lock(&shutdown_lock);
1002 list_del(&mfile->shutdown_list);
1003 spin_unlock(&shutdown_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +01001004 if (mfile->disconnected_f_op)
1005 fops_put(mfile->disconnected_f_op);
1006 found = mfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
Takashi Iwaic4614822006-06-23 14:38:23 +02001010 spin_unlock(&card->files_lock);
Takashi Iwai118dd6b2009-02-23 16:35:21 +01001011 if (!found) {
Takashi Iwaif2f93072014-02-04 18:21:03 +01001012 dev_err(card->dev, "card file remove problem (%p)\n", file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -ENOENT;
1014 }
Takashi Iwai118dd6b2009-02-23 16:35:21 +01001015 kfree(found);
Takashi Iwaif2464062014-01-29 12:13:43 +01001016 put_device(&card->card_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
1018}
1019
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001020EXPORT_SYMBOL(snd_card_file_remove);
1021
Sudheer Papothi4cd40db2016-01-29 00:36:30 +05301022/**
1023 * snd_card_change_online_state - mark card's online/offline state
1024 * @card: Card to mark
1025 * @online: whether online of offline
1026 *
1027 * Mutes the DAI DAC.
1028 */
1029void snd_card_change_online_state(struct snd_card *card, int online)
1030{
1031 snd_printd("snd card %s state change %d -> %d\n",
1032 card->shortname, !card->offline, online);
1033 card->offline = !online;
1034 /* make sure offline is updated prior to wake up */
1035 wmb();
1036 xchg(&card->offline_change, 1);
1037 wake_up_interruptible(&card->offline_poll_wait);
1038}
1039EXPORT_SYMBOL(snd_card_change_online_state);
1040
1041/**
1042 * snd_card_is_online_state - return true if card is online state
1043 * @card: Card to query
1044 */
1045bool snd_card_is_online_state(struct snd_card *card)
1046{
1047 return !card->offline;
1048}
1049EXPORT_SYMBOL(snd_card_is_online_state);
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051#ifdef CONFIG_PM
1052/**
1053 * snd_power_wait - wait until the power-state is changed.
1054 * @card: soundcard structure
1055 * @power_state: expected power state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 *
1057 * Waits until the power-state is changed.
1058 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001059 * Return: Zero if successful, or a negative error code.
1060 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 * Note: the power lock must be active before call.
1062 */
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001063int snd_power_wait(struct snd_card *card, unsigned int power_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 wait_queue_t wait;
1066 int result = 0;
1067
1068 /* fastpath */
1069 if (snd_power_get_state(card) == power_state)
1070 return 0;
1071 init_waitqueue_entry(&wait, current);
1072 add_wait_queue(&card->power_sleep, &wait);
1073 while (1) {
1074 if (card->shutdown) {
1075 result = -ENODEV;
1076 break;
1077 }
1078 if (snd_power_get_state(card) == power_state)
1079 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 set_current_state(TASK_UNINTERRUPTIBLE);
1081 snd_power_unlock(card);
1082 schedule_timeout(30 * HZ);
1083 snd_power_lock(card);
1084 }
1085 remove_wait_queue(&card->power_sleep, &wait);
1086 return result;
1087}
1088
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001089EXPORT_SYMBOL(snd_power_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090#endif /* CONFIG_PM */