Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Initialization routines |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/sched.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 25 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/file.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/time.h> |
| 29 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/pm.h> |
Takashi Iwai | f246406 | 2014-01-29 12:13:43 +0100 | [diff] [blame] | 31 | #include <linux/completion.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <sound/core.h> |
| 34 | #include <sound/control.h> |
| 35 | #include <sound/info.h> |
| 36 | |
Takashi Iwai | 82a783f | 2009-09-07 15:50:18 +0200 | [diff] [blame] | 37 | /* monitor files for graceful shutdown (hotplug) */ |
| 38 | struct 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 Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 45 | static DEFINE_SPINLOCK(shutdown_lock); |
| 46 | static LIST_HEAD(shutdown_files); |
| 47 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 48 | static const struct file_operations snd_shutdown_f_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Takashi Iwai | 7bb2491 | 2013-05-15 08:46:39 +0200 | [diff] [blame] | 50 | /* locked for registering/using */ |
| 51 | static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS); |
Takashi Iwai | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 52 | struct snd_card *snd_cards[SNDRV_CARDS]; |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 53 | EXPORT_SYMBOL(snd_cards); |
| 54 | |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 55 | static DEFINE_MUTEX(snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame] | 57 | static char *slots[SNDRV_CARDS]; |
| 58 | module_param_array(slots, charp, NULL, 0444); |
| 59 | MODULE_PARM_DESC(slots, "Module names assigned to the slots."); |
| 60 | |
Sudheer Papothi | 4cd40db | 2016-01-29 00:36:30 +0530 | [diff] [blame] | 61 | #define SND_CARD_STATE_MAX_LEN 16 |
| 62 | |
Takashi Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 63 | /* return non-zero if the given index is reserved for the given |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame] | 64 | * module via slots option |
| 65 | */ |
Takashi Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 66 | static int module_slot_match(struct module *module, int idx) |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame] | 67 | { |
Takashi Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 68 | int match = 1; |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame] | 69 | #ifdef MODULE |
Takashi Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 70 | const char *s1, *s2; |
| 71 | |
Takashi Iwai | 60f6fef | 2013-10-28 12:54:52 +0100 | [diff] [blame] | 72 | if (!module || !*module->name || !slots[idx]) |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame] | 73 | return 0; |
Takashi Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 74 | |
| 75 | s1 = module->name; |
| 76 | s2 = slots[idx]; |
| 77 | if (*s2 == '!') { |
| 78 | match = 0; /* negative match */ |
| 79 | s2++; |
| 80 | } |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame] | 81 | /* 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 Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 92 | return !match; |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame] | 93 | if (!c1) |
| 94 | break; |
| 95 | } |
Takashi Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 96 | #endif /* MODULE */ |
| 97 | return match; |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Takashi Iwai | 8eeaa2f | 2014-02-10 09:48:47 +0100 | [diff] [blame] | 100 | #if IS_ENABLED(CONFIG_SND_MIXER_OSS) |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 101 | int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag); |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 102 | EXPORT_SYMBOL(snd_mixer_oss_notify_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | #endif |
| 104 | |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 105 | #ifdef CONFIG_SND_PROC_FS |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 106 | static void snd_card_id_read(struct snd_info_entry *entry, |
| 107 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | snd_iprintf(buffer, "%s\n", entry->card->id); |
| 110 | } |
| 111 | |
Sudheer Papothi | 4cd40db | 2016-01-29 00:36:30 +0530 | [diff] [blame] | 112 | static 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 | |
| 126 | static 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 | |
| 137 | static 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 Iwai | 2471b6c | 2015-05-18 09:20:24 +0200 | [diff] [blame] | 142 | static int init_info_for_card(struct snd_card *card) |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 143 | { |
Sudheer Papothi | 4cd40db | 2016-01-29 00:36:30 +0530 | [diff] [blame] | 144 | struct snd_info_entry *entry, *entry_state; |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 145 | |
Takashi Iwai | 2471b6c | 2015-05-18 09:20:24 +0200 | [diff] [blame] | 146 | entry = snd_info_create_card_entry(card, "id", card->proc_root); |
| 147 | if (!entry) { |
Takashi Iwai | f2f9307 | 2014-02-04 18:21:03 +0100 | [diff] [blame] | 148 | dev_dbg(card->dev, "unable to create card entry\n"); |
Colin Ian King | 0755e74 | 2015-06-29 17:10:22 +0100 | [diff] [blame] | 149 | return -ENOMEM; |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 150 | } |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 151 | entry->c.text.read = snd_card_id_read; |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 152 | card->proc_id = entry; |
Takashi Iwai | 2471b6c | 2015-05-18 09:20:24 +0200 | [diff] [blame] | 153 | |
Sudheer Papothi | 4cd40db | 2016-01-29 00:36:30 +0530 | [diff] [blame] | 154 | 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 Iwai | 2471b6c | 2015-05-18 09:20:24 +0200 | [diff] [blame] | 165 | return snd_info_card_register(card); |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 166 | } |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 167 | #else /* !CONFIG_SND_PROC_FS */ |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 168 | #define init_info_for_card(card) |
| 169 | #endif |
| 170 | |
Takashi Iwai | deb6596 | 2014-01-23 11:02:15 +0100 | [diff] [blame] | 171 | static 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 | */ |
| 180 | static 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 Iwai | 4b440be | 2015-01-29 21:32:47 +0100 | [diff] [blame] | 196 | /* 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 | */ |
| 200 | static 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 | */ |
| 209 | void 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 | } |
| 217 | EXPORT_SYMBOL_GPL(snd_device_initialize); |
| 218 | |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 219 | static int snd_card_do_free(struct snd_card *card); |
Takashi Iwai | 6bbc7fe | 2015-01-30 12:27:43 +0100 | [diff] [blame] | 220 | static const struct attribute_group card_dev_attr_group; |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 221 | |
| 222 | static void release_card_device(struct device *dev) |
| 223 | { |
| 224 | snd_card_do_free(dev_to_snd_card(dev)); |
| 225 | } |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | /** |
Takashi Iwai | 393aa9c | 2014-01-29 12:51:12 +0100 | [diff] [blame] | 228 | * snd_card_new - create and initialize a soundcard structure |
| 229 | * @parent: the parent device object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * @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 Iwai | 53fb1e6 | 2008-12-28 16:32:08 +0100 | [diff] [blame] | 234 | * @card_ret: the pointer to store the created card instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | * |
| 236 | * Creates and initializes a soundcard structure. |
| 237 | * |
Takashi Iwai | 53fb1e6 | 2008-12-28 16:32:08 +0100 | [diff] [blame] | 238 | * 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 Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 242 | * Return: Zero if successful or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | */ |
Takashi Iwai | 393aa9c | 2014-01-29 12:51:12 +0100 | [diff] [blame] | 244 | int snd_card_new(struct device *parent, int idx, const char *xid, |
Takashi Iwai | 53fb1e6 | 2008-12-28 16:32:08 +0100 | [diff] [blame] | 245 | struct module *module, int extra_size, |
| 246 | struct snd_card **card_ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 248 | struct snd_card *card; |
Takashi Iwai | deb6596 | 2014-01-23 11:02:15 +0100 | [diff] [blame] | 249 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Takashi Iwai | 53fb1e6 | 2008-12-28 16:32:08 +0100 | [diff] [blame] | 251 | if (snd_BUG_ON(!card_ret)) |
| 252 | return -EINVAL; |
| 253 | *card_ret = NULL; |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (extra_size < 0) |
| 256 | extra_size = 0; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 257 | card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL); |
Takashi Iwai | 53fb1e6 | 2008-12-28 16:32:08 +0100 | [diff] [blame] | 258 | if (!card) |
| 259 | return -ENOMEM; |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 260 | if (extra_size > 0) |
| 261 | card->private_data = (char *)card + sizeof(struct snd_card); |
Jaroslav Kysela | 10a8ebb | 2009-06-02 12:02:38 +0200 | [diff] [blame] | 262 | if (xid) |
Jaroslav Kysela | 5fdc18d | 2009-06-04 00:12:18 +0200 | [diff] [blame] | 263 | strlcpy(card->id, xid, sizeof(card->id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | err = 0; |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 265 | mutex_lock(&snd_card_mutex); |
Takashi Iwai | deb6596 | 2014-01-23 11:02:15 +0100 | [diff] [blame] | 266 | 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 Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 270 | if (idx < 0) |
| 271 | err = -ENODEV; |
| 272 | else if (idx < snd_ecards_limit) { |
Takashi Iwai | 7bb2491 | 2013-05-15 08:46:39 +0200 | [diff] [blame] | 273 | if (test_bit(idx, snd_cards_lock)) |
Takashi Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 274 | err = -EBUSY; /* invalid */ |
| 275 | } else if (idx >= SNDRV_CARDS) |
| 276 | err = -ENODEV; |
| 277 | if (err < 0) { |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 278 | mutex_unlock(&snd_card_mutex); |
Takashi Iwai | f2f9307 | 2014-02-04 18:21:03 +0100 | [diff] [blame] | 279 | dev_err(parent, "cannot find the slot for index %d (range 0-%i), error: %d\n", |
Oliver Neukum | 5c33dd7 | 2007-01-16 17:49:21 +0100 | [diff] [blame] | 280 | idx, snd_ecards_limit - 1, err); |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 281 | kfree(card); |
| 282 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
Takashi Iwai | 7bb2491 | 2013-05-15 08:46:39 +0200 | [diff] [blame] | 284 | set_bit(idx, snd_cards_lock); /* lock it */ |
Takashi Iwai | a93bbaa | 2008-05-27 17:59:24 +0200 | [diff] [blame] | 285 | if (idx >= snd_ecards_limit) |
| 286 | snd_ecards_limit = idx + 1; /* increase the limit */ |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 287 | mutex_unlock(&snd_card_mutex); |
Takashi Iwai | 393aa9c | 2014-01-29 12:51:12 +0100 | [diff] [blame] | 288 | card->dev = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | 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 Clausen | 07f4d9d | 2014-06-18 13:32:31 +0200 | [diff] [blame] | 294 | mutex_init(&card->user_ctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | INIT_LIST_HEAD(&card->controls); |
| 296 | INIT_LIST_HEAD(&card->ctl_files); |
| 297 | spin_lock_init(&card->files_lock); |
Takashi Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 298 | INIT_LIST_HEAD(&card->files_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | #ifdef CONFIG_PM |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 300 | mutex_init(&card->power_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | init_waitqueue_head(&card->power_sleep); |
| 302 | #endif |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 303 | |
Sudheer Papothi | 4cd40db | 2016-01-29 00:36:30 +0530 | [diff] [blame] | 304 | init_waitqueue_head(&card->offline_poll_wait); |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 305 | 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 Iwai | 6bbc7fe | 2015-01-30 12:27:43 +0100 | [diff] [blame] | 309 | card->card_dev.groups = card->dev_groups; |
| 310 | card->dev_groups[0] = &card_dev_attr_group; |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 311 | err = kobject_set_name(&card->card_dev.kobj, "card%d", idx); |
| 312 | if (err < 0) |
| 313 | goto __error; |
| 314 | |
Heiner Kallweit | de65360 | 2015-12-22 19:09:05 +0100 | [diff] [blame] | 315 | snprintf(card->irq_descr, sizeof(card->irq_descr), "%s:%s", |
| 316 | dev_driver_string(card->dev), dev_name(&card->card_dev)); |
| 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | /* 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 Iwai | 53fb1e6 | 2008-12-28 16:32:08 +0100 | [diff] [blame] | 320 | err = snd_ctl_create(card); |
| 321 | if (err < 0) { |
Takashi Iwai | f2f9307 | 2014-02-04 18:21:03 +0100 | [diff] [blame] | 322 | dev_err(parent, "unable to register control minors\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | goto __error; |
| 324 | } |
Takashi Iwai | 53fb1e6 | 2008-12-28 16:32:08 +0100 | [diff] [blame] | 325 | err = snd_info_card_create(card); |
| 326 | if (err < 0) { |
Takashi Iwai | f2f9307 | 2014-02-04 18:21:03 +0100 | [diff] [blame] | 327 | dev_err(parent, "unable to create card info\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | goto __error_ctl; |
| 329 | } |
Takashi Iwai | 53fb1e6 | 2008-12-28 16:32:08 +0100 | [diff] [blame] | 330 | *card_ret = card; |
| 331 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | __error_ctl: |
Takashi Iwai | 289ca02 | 2014-01-29 15:53:35 +0100 | [diff] [blame] | 334 | snd_device_free_all(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | __error: |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 336 | put_device(&card->card_dev); |
Takashi Iwai | 53fb1e6 | 2008-12-28 16:32:08 +0100 | [diff] [blame] | 337 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
Takashi Iwai | 393aa9c | 2014-01-29 12:51:12 +0100 | [diff] [blame] | 339 | EXPORT_SYMBOL(snd_card_new); |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 340 | |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 341 | /* return non-zero if a card is already locked */ |
| 342 | int snd_card_locked(int card) |
| 343 | { |
| 344 | int locked; |
| 345 | |
| 346 | mutex_lock(&snd_card_mutex); |
Takashi Iwai | 7bb2491 | 2013-05-15 08:46:39 +0200 | [diff] [blame] | 347 | locked = test_bit(card, snd_cards_lock); |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 348 | mutex_unlock(&snd_card_mutex); |
| 349 | return locked; |
| 350 | } |
| 351 | |
Clemens Ladisch | b3b0abe | 2006-03-03 14:08:43 +0100 | [diff] [blame] | 352 | static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig) |
| 353 | { |
| 354 | return -ENODEV; |
| 355 | } |
| 356 | |
| 357 | static ssize_t snd_disconnect_read(struct file *file, char __user *buf, |
| 358 | size_t count, loff_t *offset) |
| 359 | { |
| 360 | return -ENODEV; |
| 361 | } |
| 362 | |
| 363 | static 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 Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 369 | static 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 Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 377 | list_del_init(&df->shutdown_list); |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | } |
| 381 | spin_unlock(&shutdown_lock); |
| 382 | |
Al Viro | 233e70f | 2008-10-31 23:28:30 +0000 | [diff] [blame] | 383 | if (likely(df)) { |
| 384 | if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync) |
| 385 | df->disconnected_f_op->fasync(-1, file, 0); |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 386 | return df->disconnected_f_op->release(inode, file); |
Al Viro | 233e70f | 2008-10-31 23:28:30 +0000 | [diff] [blame] | 387 | } |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 388 | |
Harvey Harrison | 9bf8e7d | 2008-03-03 15:32:18 -0800 | [diff] [blame] | 389 | panic("%s(%p, %p) failed!", __func__, inode, file); |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait) |
| 393 | { |
| 394 | return POLLERR | POLLNVAL; |
| 395 | } |
| 396 | |
Clemens Ladisch | b3b0abe | 2006-03-03 14:08:43 +0100 | [diff] [blame] | 397 | static long snd_disconnect_ioctl(struct file *file, |
| 398 | unsigned int cmd, unsigned long arg) |
| 399 | { |
| 400 | return -ENODEV; |
| 401 | } |
| 402 | |
| 403 | static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma) |
| 404 | { |
| 405 | return -ENODEV; |
| 406 | } |
| 407 | |
| 408 | static int snd_disconnect_fasync(int fd, struct file *file, int on) |
| 409 | { |
| 410 | return -ENODEV; |
| 411 | } |
| 412 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 413 | static const struct file_operations snd_shutdown_f_ops = |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 414 | { |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | /** |
| 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 Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 435 | * Return: Zero, otherwise a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | * |
| 437 | * Note: The current implementation replaces all active file->f_op with special |
| 438 | * dummy file operations (they do nothing except release). |
| 439 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 440 | int snd_card_disconnect(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
| 442 | struct snd_monitor_file *mfile; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
Takashi Iwai | f18638d | 2008-04-17 12:52:02 +0200 | [diff] [blame] | 444 | if (!card) |
| 445 | return -EINVAL; |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | 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 Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 456 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | snd_cards[card->number] = NULL; |
Takashi Iwai | 7bb2491 | 2013-05-15 08:46:39 +0200 | [diff] [blame] | 458 | clear_bit(card->number, snd_cards_lock); |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 459 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | /* phase 2: replace file->f_op with special dummy operations */ |
| 462 | |
| 463 | spin_lock(&card->files_lock); |
Takashi Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 464 | list_for_each_entry(mfile, &card->files_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | /* it's critical part, use endless loop */ |
| 466 | /* we have no room to fail */ |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 467 | mfile->disconnected_f_op = mfile->file->f_op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 469 | spin_lock(&shutdown_lock); |
| 470 | list_add(&mfile->shutdown_list, &shutdown_files); |
| 471 | spin_unlock(&shutdown_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 473 | mfile->file->f_op = &snd_shutdown_f_ops; |
Miguel Boton | bc9abce | 2008-01-13 12:03:53 +0100 | [diff] [blame] | 474 | fops_get(mfile->file->f_op); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 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 Iwai | 8eeaa2f | 2014-02-10 09:48:47 +0100 | [diff] [blame] | 481 | #if IS_ENABLED(CONFIG_SND_MIXER_OSS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | 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 Iwai | e086e30 | 2015-02-27 18:01:22 +0100 | [diff] [blame] | 487 | snd_device_disconnect_all(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 489 | snd_info_card_disconnect(card); |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 490 | if (card->registered) { |
| 491 | device_del(&card->card_dev); |
| 492 | card->registered = false; |
Takashi Iwai | 73d38b1 | 2008-04-17 12:50:47 +0200 | [diff] [blame] | 493 | } |
Takashi Iwai | f18638d | 2008-04-17 12:52:02 +0200 | [diff] [blame] | 494 | #ifdef CONFIG_PM |
| 495 | wake_up(&card->power_sleep); |
| 496 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 500 | EXPORT_SYMBOL(snd_card_disconnect); |
| 501 | |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 502 | static int snd_card_do_free(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | { |
Takashi Iwai | 8eeaa2f | 2014-02-10 09:48:47 +0100 | [diff] [blame] | 504 | #if IS_ENABLED(CONFIG_SND_MIXER_OSS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if (snd_mixer_oss_notify_callback) |
| 506 | snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE); |
| 507 | #endif |
Takashi Iwai | 72620d6 | 2014-02-04 11:36:11 +0100 | [diff] [blame] | 508 | snd_device_free_all(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | if (card->private_free) |
| 510 | card->private_free(card); |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 511 | snd_info_free_entry(card->proc_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (snd_info_card_free(card) < 0) { |
Takashi Iwai | f2f9307 | 2014-02-04 18:21:03 +0100 | [diff] [blame] | 513 | dev_warn(card->dev, "unable to free card info\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | /* Not fatal error */ |
| 515 | } |
Takashi Iwai | f246406 | 2014-01-29 12:13:43 +0100 | [diff] [blame] | 516 | if (card->release_completion) |
| 517 | complete(card->release_completion); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 518 | kfree(card); |
| 519 | return 0; |
| 520 | } |
| 521 | |
Takashi Iwai | eb9c38d | 2014-10-30 15:19:43 +0100 | [diff] [blame] | 522 | /** |
| 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 Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 531 | int snd_card_free_when_closed(struct snd_card *card) |
| 532 | { |
Takashi Iwai | f18638d | 2008-04-17 12:52:02 +0200 | [diff] [blame] | 533 | int ret = snd_card_disconnect(card); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 534 | if (ret) |
| 535 | return ret; |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 536 | put_device(&card->card_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return 0; |
| 538 | } |
Takashi Iwai | f246406 | 2014-01-29 12:13:43 +0100 | [diff] [blame] | 539 | EXPORT_SYMBOL(snd_card_free_when_closed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Takashi Iwai | eb9c38d | 2014-10-30 15:19:43 +0100 | [diff] [blame] | 541 | /** |
| 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 Iwai | f246406 | 2014-01-29 12:13:43 +0100 | [diff] [blame] | 554 | int 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 Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 568 | EXPORT_SYMBOL(snd_card_free); |
| 569 | |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 570 | /* retrieve the last word of shortname or longname */ |
| 571 | static const char *retrieve_id_from_card_name(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 573 | 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 */ |
| 584 | static 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 */ |
| 598 | static 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 | */ |
| 618 | static void snd_card_set_id_no_lock(struct snd_card *card, const char *src, |
| 619 | const char *nid) |
| 620 | { |
| 621 | int len, loops; |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 622 | bool is_default = false; |
Jaroslav Kysela | 10a8ebb | 2009-06-02 12:02:38 +0200 | [diff] [blame] | 623 | char *id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 625 | copy_valid_id_string(card, src, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | id = card->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 628 | again: |
| 629 | /* use "Default" for obviously invalid strings |
| 630 | * ("card" conflicts with proc directories) |
| 631 | */ |
| 632 | if (!*id || !strncmp(id, "card", 4)) { |
Takashi Iwai | d8e4f9a | 2011-04-04 12:43:23 +0200 | [diff] [blame] | 633 | strcpy(id, "Default"); |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 634 | is_default = true; |
| 635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Takashi Iwai | 8edbb19 | 2013-05-24 16:30:39 +0200 | [diff] [blame] | 637 | len = strlen(id); |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 638 | for (loops = 0; loops < SNDRV_CARDS; loops++) { |
Takashi Iwai | 8edbb19 | 2013-05-24 16:30:39 +0200 | [diff] [blame] | 639 | char *spos; |
| 640 | char sfxstr[5]; /* "_012" */ |
| 641 | int sfxlen; |
| 642 | |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 643 | if (card_id_ok(card, id)) |
| 644 | return; /* OK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Takashi Iwai | 8edbb19 | 2013-05-24 16:30:39 +0200 | [diff] [blame] | 646 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 655 | /* fallback to the default id */ |
| 656 | if (!is_default) { |
| 657 | *id = 0; |
| 658 | goto again; |
| 659 | } |
| 660 | /* last resort... */ |
Takashi Iwai | f2f9307 | 2014-02-04 18:21:03 +0100 | [diff] [blame] | 661 | dev_err(card->dev, "unable to set card id (%s)\n", id); |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 662 | if (card->proc_root->name) |
Takashi Iwai | 97f44f5 | 2013-10-29 15:20:06 +0100 | [diff] [blame] | 663 | strlcpy(card->id, card->proc_root->name, sizeof(card->id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Mark Brown | 872c782 | 2009-06-03 20:43:29 +0100 | [diff] [blame] | 666 | /** |
| 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 | */ |
| 674 | void snd_card_set_id(struct snd_card *card, const char *nid) |
| 675 | { |
Jaroslav Kysela | 5fdc18d | 2009-06-04 00:12:18 +0200 | [diff] [blame] | 676 | /* check if user specified own card->id */ |
| 677 | if (card->id[0] != '\0') |
| 678 | return; |
| 679 | mutex_lock(&snd_card_mutex); |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 680 | snd_card_set_id_no_lock(card, nid, nid); |
Jaroslav Kysela | 5fdc18d | 2009-06-04 00:12:18 +0200 | [diff] [blame] | 681 | mutex_unlock(&snd_card_mutex); |
Mark Brown | 872c782 | 2009-06-03 20:43:29 +0100 | [diff] [blame] | 682 | } |
Jaroslav Kysela | 10a8ebb | 2009-06-02 12:02:38 +0200 | [diff] [blame] | 683 | EXPORT_SYMBOL(snd_card_set_id); |
| 684 | |
Jaroslav Kysela | 9fb6198 | 2008-11-11 16:51:02 +0100 | [diff] [blame] | 685 | static ssize_t |
| 686 | card_id_show_attr(struct device *dev, |
| 687 | struct device_attribute *attr, char *buf) |
| 688 | { |
Takashi Iwai | b203dba | 2014-02-19 11:18:10 +0100 | [diff] [blame] | 689 | struct snd_card *card = container_of(dev, struct snd_card, card_dev); |
| 690 | return snprintf(buf, PAGE_SIZE, "%s\n", card->id); |
Jaroslav Kysela | 9fb6198 | 2008-11-11 16:51:02 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static ssize_t |
| 694 | card_id_store_attr(struct device *dev, struct device_attribute *attr, |
| 695 | const char *buf, size_t count) |
| 696 | { |
Takashi Iwai | b203dba | 2014-02-19 11:18:10 +0100 | [diff] [blame] | 697 | struct snd_card *card = container_of(dev, struct snd_card, card_dev); |
Jaroslav Kysela | 9fb6198 | 2008-11-11 16:51:02 +0100 | [diff] [blame] | 698 | 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 Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 712 | if (!card_id_ok(NULL, buf1)) { |
Jaroslav Kysela | 9fb6198 | 2008-11-11 16:51:02 +0100 | [diff] [blame] | 713 | mutex_unlock(&snd_card_mutex); |
| 714 | return -EEXIST; |
| 715 | } |
Jaroslav Kysela | 9fb6198 | 2008-11-11 16:51:02 +0100 | [diff] [blame] | 716 | strcpy(card->id, buf1); |
Jaroslav Kysela | c2eb9c4 | 2008-11-12 16:31:37 +0100 | [diff] [blame] | 717 | snd_info_card_id_change(card); |
Jaroslav Kysela | 9fb6198 | 2008-11-11 16:51:02 +0100 | [diff] [blame] | 718 | mutex_unlock(&snd_card_mutex); |
| 719 | |
| 720 | return count; |
| 721 | } |
| 722 | |
Takashi Iwai | 34356db | 2014-01-29 11:54:10 +0100 | [diff] [blame] | 723 | static DEVICE_ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr); |
Jaroslav Kysela | 9fb6198 | 2008-11-11 16:51:02 +0100 | [diff] [blame] | 724 | |
| 725 | static ssize_t |
| 726 | card_number_show_attr(struct device *dev, |
| 727 | struct device_attribute *attr, char *buf) |
| 728 | { |
Takashi Iwai | b203dba | 2014-02-19 11:18:10 +0100 | [diff] [blame] | 729 | struct snd_card *card = container_of(dev, struct snd_card, card_dev); |
| 730 | return snprintf(buf, PAGE_SIZE, "%i\n", card->number); |
Jaroslav Kysela | 9fb6198 | 2008-11-11 16:51:02 +0100 | [diff] [blame] | 731 | } |
| 732 | |
Takashi Iwai | 34356db | 2014-01-29 11:54:10 +0100 | [diff] [blame] | 733 | static DEVICE_ATTR(number, S_IRUGO, card_number_show_attr, NULL); |
| 734 | |
| 735 | static struct attribute *card_dev_attrs[] = { |
| 736 | &dev_attr_id.attr, |
| 737 | &dev_attr_number.attr, |
| 738 | NULL |
| 739 | }; |
| 740 | |
Takashi Iwai | 6bbc7fe | 2015-01-30 12:27:43 +0100 | [diff] [blame] | 741 | static const struct attribute_group card_dev_attr_group = { |
Takashi Iwai | 34356db | 2014-01-29 11:54:10 +0100 | [diff] [blame] | 742 | .attrs = card_dev_attrs, |
| 743 | }; |
| 744 | |
Takashi Iwai | 6bbc7fe | 2015-01-30 12:27:43 +0100 | [diff] [blame] | 745 | /** |
| 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 | */ |
| 750 | int 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 Iwai | 34356db | 2014-01-29 11:54:10 +0100 | [diff] [blame] | 765 | }; |
Takashi Iwai | 6bbc7fe | 2015-01-30 12:27:43 +0100 | [diff] [blame] | 766 | EXPORT_SYMBOL_GPL(snd_card_add_dev_attr); |
Jaroslav Kysela | 9fb6198 | 2008-11-11 16:51:02 +0100 | [diff] [blame] | 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | /** |
| 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 Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 777 | * Return: Zero otherwise a negative error code if the registration failed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 779 | int snd_card_register(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | { |
| 781 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 783 | if (snd_BUG_ON(!card)) |
| 784 | return -EINVAL; |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 785 | |
Takashi Iwai | 8bfb181 | 2014-01-29 11:46:11 +0100 | [diff] [blame] | 786 | if (!card->registered) { |
| 787 | err = device_add(&card->card_dev); |
| 788 | if (err < 0) |
| 789 | return err; |
| 790 | card->registered = true; |
Greg Kroah-Hartman | d80f19f | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 791 | } |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | if ((err = snd_device_register_all(card)) < 0) |
| 794 | return err; |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 795 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | if (snd_cards[card->number]) { |
| 797 | /* already registered */ |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 798 | mutex_unlock(&snd_card_mutex); |
Takashi Iwai | 2471b6c | 2015-05-18 09:20:24 +0200 | [diff] [blame] | 799 | return snd_info_card_register(card); /* register pending info */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
Takashi Iwai | e7df2a3 | 2012-03-09 17:41:53 +0100 | [diff] [blame] | 801 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | snd_cards[card->number] = card; |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 814 | mutex_unlock(&snd_card_mutex); |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 815 | init_info_for_card(card); |
Takashi Iwai | 8eeaa2f | 2014-02-10 09:48:47 +0100 | [diff] [blame] | 816 | #if IS_ENABLED(CONFIG_SND_MIXER_OSS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | 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 Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 823 | EXPORT_SYMBOL(snd_card_register); |
| 824 | |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 825 | #ifdef CONFIG_SND_PROC_FS |
Takashi Iwai | a381a7a | 2005-11-17 15:55:49 +0100 | [diff] [blame] | 826 | static void snd_card_info_read(struct snd_info_entry *entry, |
| 827 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
| 829 | int idx, count; |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 830 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
| 832 | for (idx = count = 0; idx < SNDRV_CARDS; idx++) { |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 833 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | if ((card = snd_cards[idx]) != NULL) { |
| 835 | count++; |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 836 | snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | idx, |
| 838 | card->id, |
| 839 | card->driver, |
| 840 | card->shortname); |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 841 | snd_iprintf(buffer, " %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | card->longname); |
| 843 | } |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 844 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | if (!count) |
| 847 | snd_iprintf(buffer, "--- no soundcards ---\n"); |
| 848 | } |
| 849 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 850 | #ifdef CONFIG_SND_OSSEMUL |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 851 | void snd_card_info_read_oss(struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | { |
| 853 | int idx, count; |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 854 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
| 856 | for (idx = count = 0; idx < SNDRV_CARDS; idx++) { |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 857 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | if ((card = snd_cards[idx]) != NULL) { |
| 859 | count++; |
| 860 | snd_iprintf(buffer, "%s\n", card->longname); |
| 861 | } |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 862 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | if (!count) { |
| 865 | snd_iprintf(buffer, "--- no soundcards ---\n"); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | #endif |
| 870 | |
| 871 | #ifdef MODULE |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 872 | static void snd_card_module_info_read(struct snd_info_entry *entry, |
| 873 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
| 875 | int idx; |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 876 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
| 878 | for (idx = 0; idx < SNDRV_CARDS; idx++) { |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 879 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | if ((card = snd_cards[idx]) != NULL) |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 881 | snd_iprintf(buffer, "%2i %s\n", |
| 882 | idx, card->module->name); |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 883 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | #endif |
| 887 | |
| 888 | int __init snd_card_info_init(void) |
| 889 | { |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 890 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
| 892 | entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL); |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 893 | if (! entry) |
| 894 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | entry->c.text.read = snd_card_info_read; |
Takashi Iwai | b591b6e | 2015-04-22 22:29:10 +0200 | [diff] [blame] | 896 | if (snd_info_register(entry) < 0) |
| 897 | return -ENOMEM; /* freed in error path */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | #ifdef MODULE |
| 900 | entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL); |
Takashi Iwai | b591b6e | 2015-04-22 22:29:10 +0200 | [diff] [blame] | 901 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | #endif |
| 907 | |
| 908 | return 0; |
| 909 | } |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 910 | #endif /* CONFIG_SND_PROC_FS */ |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /** |
| 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 Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 920 | * Return: Zero otherwise a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | */ |
| 922 | |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 923 | int snd_component_add(struct snd_card *card, const char *component) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | { |
| 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 Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 943 | EXPORT_SYMBOL(snd_component_add); |
| 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | /** |
| 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 Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 954 | * Return: zero or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 956 | int snd_card_file_add(struct snd_card *card, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | { |
| 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 Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 964 | mfile->disconnected_f_op = NULL; |
Takashi Iwai | a45e3d6 | 2011-03-24 09:50:15 +0100 | [diff] [blame] | 965 | INIT_LIST_HEAD(&mfile->shutdown_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | spin_lock(&card->files_lock); |
| 967 | if (card->shutdown) { |
| 968 | spin_unlock(&card->files_lock); |
| 969 | kfree(mfile); |
| 970 | return -ENODEV; |
| 971 | } |
Takashi Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 972 | list_add(&mfile->list, &card->files_list); |
Takashi Iwai | f246406 | 2014-01-29 12:13:43 +0100 | [diff] [blame] | 973 | get_device(&card->card_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | spin_unlock(&card->files_lock); |
| 975 | return 0; |
| 976 | } |
| 977 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 978 | EXPORT_SYMBOL(snd_card_file_add); |
| 979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | /** |
| 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 Iwai | 2b29b13 | 2006-06-23 14:38:26 +0200 | [diff] [blame] | 987 | * If all files are removed and snd_card_free_when_closed() was |
| 988 | * called beforehand, it processes the pending release of |
| 989 | * resources. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 991 | * Return: Zero or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 993 | int snd_card_file_remove(struct snd_card *card, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | { |
Takashi Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 995 | struct snd_monitor_file *mfile, *found = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
| 997 | spin_lock(&card->files_lock); |
Takashi Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 998 | list_for_each_entry(mfile, &card->files_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | if (mfile->file == file) { |
Takashi Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 1000 | list_del(&mfile->list); |
Takashi Iwai | a45e3d6 | 2011-03-24 09:50:15 +0100 | [diff] [blame] | 1001 | spin_lock(&shutdown_lock); |
| 1002 | list_del(&mfile->shutdown_list); |
| 1003 | spin_unlock(&shutdown_lock); |
Takashi Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 1004 | if (mfile->disconnected_f_op) |
| 1005 | fops_put(mfile->disconnected_f_op); |
| 1006 | found = mfile; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | break; |
| 1008 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 1010 | spin_unlock(&card->files_lock); |
Takashi Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 1011 | if (!found) { |
Takashi Iwai | f2f9307 | 2014-02-04 18:21:03 +0100 | [diff] [blame] | 1012 | dev_err(card->dev, "card file remove problem (%p)\n", file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | return -ENOENT; |
| 1014 | } |
Takashi Iwai | 118dd6b | 2009-02-23 16:35:21 +0100 | [diff] [blame] | 1015 | kfree(found); |
Takashi Iwai | f246406 | 2014-01-29 12:13:43 +0100 | [diff] [blame] | 1016 | put_device(&card->card_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | return 0; |
| 1018 | } |
| 1019 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 1020 | EXPORT_SYMBOL(snd_card_file_remove); |
| 1021 | |
Sudheer Papothi | 4cd40db | 2016-01-29 00:36:30 +0530 | [diff] [blame] | 1022 | /** |
| 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 | */ |
| 1029 | void 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 | } |
| 1039 | EXPORT_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 | */ |
| 1045 | bool snd_card_is_online_state(struct snd_card *card) |
| 1046 | { |
| 1047 | return !card->offline; |
| 1048 | } |
| 1049 | EXPORT_SYMBOL(snd_card_is_online_state); |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | * |
| 1057 | * Waits until the power-state is changed. |
| 1058 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1059 | * Return: Zero if successful, or a negative error code. |
| 1060 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | * Note: the power lock must be active before call. |
| 1062 | */ |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame] | 1063 | int snd_power_wait(struct snd_card *card, unsigned int power_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | { |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | 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 Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 1089 | EXPORT_SYMBOL(snd_power_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | #endif /* CONFIG_PM */ |