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 | |
| 22 | #include <sound/driver.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/file.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/time.h> |
| 28 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/pm.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <sound/core.h> |
| 32 | #include <sound/control.h> |
| 33 | #include <sound/info.h> |
| 34 | |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 35 | static DEFINE_SPINLOCK(shutdown_lock); |
| 36 | static LIST_HEAD(shutdown_files); |
| 37 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 38 | static const struct file_operations snd_shutdown_f_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Takashi Iwai | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 40 | static unsigned int snd_cards_lock; /* locked for registering/using */ |
| 41 | struct snd_card *snd_cards[SNDRV_CARDS]; |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 42 | EXPORT_SYMBOL(snd_cards); |
| 43 | |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 44 | static DEFINE_MUTEX(snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame^] | 46 | static char *slots[SNDRV_CARDS]; |
| 47 | module_param_array(slots, charp, NULL, 0444); |
| 48 | MODULE_PARM_DESC(slots, "Module names assigned to the slots."); |
| 49 | |
| 50 | /* return non-zero if the given index is already reserved for another |
| 51 | * module via slots option |
| 52 | */ |
| 53 | static int module_slot_mismatch(struct module *module, int idx) |
| 54 | { |
| 55 | #ifdef MODULE |
| 56 | char *s1, *s2; |
| 57 | if (!module || !module->name || !slots[idx]) |
| 58 | return 0; |
| 59 | s1 = slots[idx]; |
| 60 | s2 = module->name; |
| 61 | /* compare module name strings |
| 62 | * hyphens are handled as equivalent with underscore |
| 63 | */ |
| 64 | for (;;) { |
| 65 | char c1 = *s1++; |
| 66 | char c2 = *s2++; |
| 67 | if (c1 == '-') |
| 68 | c1 = '_'; |
| 69 | if (c2 == '-') |
| 70 | c2 = '_'; |
| 71 | if (c1 != c2) |
| 72 | return 1; |
| 73 | if (!c1) |
| 74 | break; |
| 75 | } |
| 76 | #endif |
| 77 | return 0; |
| 78 | } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 81 | 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] | 82 | EXPORT_SYMBOL(snd_mixer_oss_notify_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #endif |
| 84 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 85 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 86 | static void snd_card_id_read(struct snd_info_entry *entry, |
| 87 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | snd_iprintf(buffer, "%s\n", entry->card->id); |
| 90 | } |
| 91 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 92 | static inline int init_info_for_card(struct snd_card *card) |
| 93 | { |
| 94 | int err; |
| 95 | struct snd_info_entry *entry; |
| 96 | |
| 97 | if ((err = snd_info_card_register(card)) < 0) { |
| 98 | snd_printd("unable to create card info\n"); |
| 99 | return err; |
| 100 | } |
| 101 | if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) { |
| 102 | snd_printd("unable to create card entry\n"); |
| 103 | return err; |
| 104 | } |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 105 | entry->c.text.read = snd_card_id_read; |
| 106 | if (snd_info_register(entry) < 0) { |
| 107 | snd_info_free_entry(entry); |
| 108 | entry = NULL; |
| 109 | } |
| 110 | card->proc_id = entry; |
| 111 | return 0; |
| 112 | } |
| 113 | #else /* !CONFIG_PROC_FS */ |
| 114 | #define init_info_for_card(card) |
| 115 | #endif |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /** |
| 118 | * snd_card_new - create and initialize a soundcard structure |
| 119 | * @idx: card index (address) [0 ... (SNDRV_CARDS-1)] |
| 120 | * @xid: card identification (ASCII string) |
| 121 | * @module: top level module for locking |
| 122 | * @extra_size: allocate this extra size after the main soundcard structure |
| 123 | * |
| 124 | * Creates and initializes a soundcard structure. |
| 125 | * |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 126 | * Returns kmallocated snd_card structure. Creates the ALSA control interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | * (which is blocked until snd_card_register function is called). |
| 128 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 129 | struct snd_card *snd_card_new(int idx, const char *xid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | struct module *module, int extra_size) |
| 131 | { |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 132 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | int err; |
| 134 | |
| 135 | if (extra_size < 0) |
| 136 | extra_size = 0; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 137 | card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | if (card == NULL) |
| 139 | return NULL; |
| 140 | if (xid) { |
| 141 | if (!snd_info_check_reserved_words(xid)) |
| 142 | goto __error; |
| 143 | strlcpy(card->id, xid, sizeof(card->id)); |
| 144 | } |
| 145 | err = 0; |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 146 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if (idx < 0) { |
| 148 | int idx2; |
| 149 | for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) |
Oliver Neukum | 5c33dd7 | 2007-01-16 17:49:21 +0100 | [diff] [blame] | 150 | /* idx == -1 == 0xffff means: take any free slot */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | if (~snd_cards_lock & idx & 1<<idx2) { |
Takashi Iwai | 304cd07 | 2007-10-26 15:10:15 +0200 | [diff] [blame^] | 152 | if (module_slot_mismatch(module, idx2)) |
| 153 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | idx = idx2; |
| 155 | if (idx >= snd_ecards_limit) |
| 156 | snd_ecards_limit = idx + 1; |
| 157 | break; |
| 158 | } |
Oliver Neukum | 5c33dd7 | 2007-01-16 17:49:21 +0100 | [diff] [blame] | 159 | } else { |
| 160 | if (idx < snd_ecards_limit) { |
| 161 | if (snd_cards_lock & (1 << idx)) |
| 162 | err = -EBUSY; /* invalid */ |
| 163 | } else { |
| 164 | if (idx < SNDRV_CARDS) |
| 165 | snd_ecards_limit = idx + 1; /* increase the limit */ |
| 166 | else |
| 167 | err = -ENODEV; |
| 168 | } |
| 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | if (idx < 0 || err < 0) { |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 171 | mutex_unlock(&snd_card_mutex); |
Oliver Neukum | 5c33dd7 | 2007-01-16 17:49:21 +0100 | [diff] [blame] | 172 | snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i), error: %d\n", |
| 173 | idx, snd_ecards_limit - 1, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | goto __error; |
| 175 | } |
| 176 | snd_cards_lock |= 1 << idx; /* lock it */ |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 177 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | card->number = idx; |
| 179 | card->module = module; |
| 180 | INIT_LIST_HEAD(&card->devices); |
| 181 | init_rwsem(&card->controls_rwsem); |
| 182 | rwlock_init(&card->ctl_files_rwlock); |
| 183 | INIT_LIST_HEAD(&card->controls); |
| 184 | INIT_LIST_HEAD(&card->ctl_files); |
| 185 | spin_lock_init(&card->files_lock); |
| 186 | init_waitqueue_head(&card->shutdown_sleep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | #ifdef CONFIG_PM |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 188 | mutex_init(&card->power_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | init_waitqueue_head(&card->power_sleep); |
| 190 | #endif |
| 191 | /* the control interface cannot be accessed from the user space until */ |
| 192 | /* snd_cards_bitmask and snd_cards are set with snd_card_register */ |
| 193 | if ((err = snd_ctl_create(card)) < 0) { |
| 194 | snd_printd("unable to register control minors\n"); |
| 195 | goto __error; |
| 196 | } |
| 197 | if ((err = snd_info_card_create(card)) < 0) { |
| 198 | snd_printd("unable to create card info\n"); |
| 199 | goto __error_ctl; |
| 200 | } |
| 201 | if (extra_size > 0) |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 202 | card->private_data = (char *)card + sizeof(struct snd_card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return card; |
| 204 | |
| 205 | __error_ctl: |
| 206 | snd_device_free_all(card, SNDRV_DEV_CMD_PRE); |
| 207 | __error: |
| 208 | kfree(card); |
| 209 | return NULL; |
| 210 | } |
| 211 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 212 | EXPORT_SYMBOL(snd_card_new); |
| 213 | |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 214 | /* return non-zero if a card is already locked */ |
| 215 | int snd_card_locked(int card) |
| 216 | { |
| 217 | int locked; |
| 218 | |
| 219 | mutex_lock(&snd_card_mutex); |
| 220 | locked = snd_cards_lock & (1 << card); |
| 221 | mutex_unlock(&snd_card_mutex); |
| 222 | return locked; |
| 223 | } |
| 224 | |
Clemens Ladisch | b3b0abe | 2006-03-03 14:08:43 +0100 | [diff] [blame] | 225 | static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig) |
| 226 | { |
| 227 | return -ENODEV; |
| 228 | } |
| 229 | |
| 230 | static ssize_t snd_disconnect_read(struct file *file, char __user *buf, |
| 231 | size_t count, loff_t *offset) |
| 232 | { |
| 233 | return -ENODEV; |
| 234 | } |
| 235 | |
| 236 | static ssize_t snd_disconnect_write(struct file *file, const char __user *buf, |
| 237 | size_t count, loff_t *offset) |
| 238 | { |
| 239 | return -ENODEV; |
| 240 | } |
| 241 | |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 242 | static int snd_disconnect_release(struct inode *inode, struct file *file) |
| 243 | { |
| 244 | struct snd_monitor_file *df = NULL, *_df; |
| 245 | |
| 246 | spin_lock(&shutdown_lock); |
| 247 | list_for_each_entry(_df, &shutdown_files, shutdown_list) { |
| 248 | if (_df->file == file) { |
| 249 | df = _df; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | spin_unlock(&shutdown_lock); |
| 254 | |
| 255 | if (likely(df)) |
| 256 | return df->disconnected_f_op->release(inode, file); |
| 257 | |
| 258 | panic("%s(%p, %p) failed!", __FUNCTION__, inode, file); |
| 259 | } |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait) |
| 262 | { |
| 263 | return POLLERR | POLLNVAL; |
| 264 | } |
| 265 | |
Clemens Ladisch | b3b0abe | 2006-03-03 14:08:43 +0100 | [diff] [blame] | 266 | static long snd_disconnect_ioctl(struct file *file, |
| 267 | unsigned int cmd, unsigned long arg) |
| 268 | { |
| 269 | return -ENODEV; |
| 270 | } |
| 271 | |
| 272 | static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma) |
| 273 | { |
| 274 | return -ENODEV; |
| 275 | } |
| 276 | |
| 277 | static int snd_disconnect_fasync(int fd, struct file *file, int on) |
| 278 | { |
| 279 | return -ENODEV; |
| 280 | } |
| 281 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 282 | static const struct file_operations snd_shutdown_f_ops = |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 283 | { |
| 284 | .owner = THIS_MODULE, |
| 285 | .llseek = snd_disconnect_llseek, |
| 286 | .read = snd_disconnect_read, |
| 287 | .write = snd_disconnect_write, |
| 288 | .release = snd_disconnect_release, |
| 289 | .poll = snd_disconnect_poll, |
| 290 | .unlocked_ioctl = snd_disconnect_ioctl, |
| 291 | #ifdef CONFIG_COMPAT |
| 292 | .compat_ioctl = snd_disconnect_ioctl, |
| 293 | #endif |
| 294 | .mmap = snd_disconnect_mmap, |
| 295 | .fasync = snd_disconnect_fasync |
| 296 | }; |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | /** |
| 299 | * snd_card_disconnect - disconnect all APIs from the file-operations (user space) |
| 300 | * @card: soundcard structure |
| 301 | * |
| 302 | * Disconnects all APIs from the file-operations (user space). |
| 303 | * |
| 304 | * Returns zero, otherwise a negative error code. |
| 305 | * |
| 306 | * Note: The current implementation replaces all active file->f_op with special |
| 307 | * dummy file operations (they do nothing except release). |
| 308 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 309 | int snd_card_disconnect(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
| 311 | struct snd_monitor_file *mfile; |
| 312 | struct file *file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | int err; |
| 314 | |
| 315 | spin_lock(&card->files_lock); |
| 316 | if (card->shutdown) { |
| 317 | spin_unlock(&card->files_lock); |
| 318 | return 0; |
| 319 | } |
| 320 | card->shutdown = 1; |
| 321 | spin_unlock(&card->files_lock); |
| 322 | |
| 323 | /* phase 1: disable fops (user space) operations for ALSA API */ |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 324 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | snd_cards[card->number] = NULL; |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 326 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
| 328 | /* phase 2: replace file->f_op with special dummy operations */ |
| 329 | |
| 330 | spin_lock(&card->files_lock); |
| 331 | mfile = card->files; |
| 332 | while (mfile) { |
| 333 | file = mfile->file; |
| 334 | |
| 335 | /* it's critical part, use endless loop */ |
| 336 | /* we have no room to fail */ |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 337 | mfile->disconnected_f_op = mfile->file->f_op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 339 | spin_lock(&shutdown_lock); |
| 340 | list_add(&mfile->shutdown_list, &shutdown_files); |
| 341 | spin_unlock(&shutdown_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 343 | fops_get(&snd_shutdown_f_ops); |
| 344 | mfile->file->f_op = &snd_shutdown_f_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | mfile = mfile->next; |
| 347 | } |
| 348 | spin_unlock(&card->files_lock); |
| 349 | |
| 350 | /* phase 3: notify all connected devices about disconnection */ |
| 351 | /* at this point, they cannot respond to any calls except release() */ |
| 352 | |
| 353 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) |
| 354 | if (snd_mixer_oss_notify_callback) |
| 355 | snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT); |
| 356 | #endif |
| 357 | |
| 358 | /* notify all devices that we are disconnected */ |
| 359 | err = snd_device_disconnect_all(card); |
| 360 | if (err < 0) |
| 361 | snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number); |
| 362 | |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 363 | snd_info_card_disconnect(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 367 | EXPORT_SYMBOL(snd_card_disconnect); |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /** |
| 370 | * snd_card_free - frees given soundcard structure |
| 371 | * @card: soundcard structure |
| 372 | * |
| 373 | * This function releases the soundcard structure and the all assigned |
| 374 | * devices automatically. That is, you don't have to release the devices |
| 375 | * by yourself. |
| 376 | * |
| 377 | * Returns zero. Frees all associated devices and frees the control |
| 378 | * interface associated to given soundcard. |
| 379 | */ |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 380 | static int snd_card_do_free(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) |
| 383 | if (snd_mixer_oss_notify_callback) |
| 384 | snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE); |
| 385 | #endif |
| 386 | if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) { |
| 387 | snd_printk(KERN_ERR "unable to free all devices (pre)\n"); |
| 388 | /* Fatal, but this situation should never occur */ |
| 389 | } |
| 390 | if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) { |
| 391 | snd_printk(KERN_ERR "unable to free all devices (normal)\n"); |
| 392 | /* Fatal, but this situation should never occur */ |
| 393 | } |
| 394 | if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) { |
| 395 | snd_printk(KERN_ERR "unable to free all devices (post)\n"); |
| 396 | /* Fatal, but this situation should never occur */ |
| 397 | } |
| 398 | if (card->private_free) |
| 399 | card->private_free(card); |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 400 | snd_info_free_entry(card->proc_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | if (snd_info_card_free(card) < 0) { |
| 402 | snd_printk(KERN_WARNING "unable to free card info\n"); |
| 403 | /* Not fatal error */ |
| 404 | } |
Takashi Iwai | 7d2aae1 | 2007-01-26 12:40:31 +0100 | [diff] [blame] | 405 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 406 | if (card->card_dev) |
| 407 | device_unregister(card->card_dev); |
| 408 | #endif |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 409 | kfree(card); |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static int snd_card_free_prepare(struct snd_card *card) |
| 414 | { |
| 415 | if (card == NULL) |
| 416 | return -EINVAL; |
| 417 | (void) snd_card_disconnect(card); |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 418 | mutex_lock(&snd_card_mutex); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 419 | snd_cards[card->number] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | snd_cards_lock &= ~(1 << card->number); |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 421 | mutex_unlock(&snd_card_mutex); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 422 | #ifdef CONFIG_PM |
| 423 | wake_up(&card->power_sleep); |
| 424 | #endif |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | int snd_card_free_when_closed(struct snd_card *card) |
| 429 | { |
| 430 | int free_now = 0; |
| 431 | int ret = snd_card_free_prepare(card); |
| 432 | if (ret) |
| 433 | return ret; |
| 434 | |
| 435 | spin_lock(&card->files_lock); |
| 436 | if (card->files == NULL) |
| 437 | free_now = 1; |
| 438 | else |
| 439 | card->free_on_last_close = 1; |
| 440 | spin_unlock(&card->files_lock); |
| 441 | |
| 442 | if (free_now) |
| 443 | snd_card_do_free(card); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | EXPORT_SYMBOL(snd_card_free_when_closed); |
| 448 | |
| 449 | int snd_card_free(struct snd_card *card) |
| 450 | { |
| 451 | int ret = snd_card_free_prepare(card); |
| 452 | if (ret) |
| 453 | return ret; |
| 454 | |
| 455 | /* wait, until all devices are ready for the free operation */ |
| 456 | wait_event(card->shutdown_sleep, card->files == NULL); |
| 457 | snd_card_do_free(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 461 | EXPORT_SYMBOL(snd_card_free); |
| 462 | |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 463 | static void choose_default_id(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 465 | int i, len, idx_flag = 0, loops = SNDRV_CARDS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | char *id, *spos; |
| 467 | |
| 468 | id = spos = card->shortname; |
| 469 | while (*id != '\0') { |
| 470 | if (*id == ' ') |
| 471 | spos = id + 1; |
| 472 | id++; |
| 473 | } |
| 474 | id = card->id; |
| 475 | while (*spos != '\0' && !isalnum(*spos)) |
| 476 | spos++; |
| 477 | if (isdigit(*spos)) |
| 478 | *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D'; |
| 479 | while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) { |
| 480 | if (isalnum(*spos)) |
| 481 | *id++ = *spos; |
| 482 | spos++; |
| 483 | } |
| 484 | *id = '\0'; |
| 485 | |
| 486 | id = card->id; |
| 487 | |
| 488 | if (*id == '\0') |
| 489 | strcpy(id, "default"); |
| 490 | |
| 491 | while (1) { |
| 492 | if (loops-- == 0) { |
| 493 | snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id); |
| 494 | strcpy(card->id, card->proc_root->name); |
| 495 | return; |
| 496 | } |
| 497 | if (!snd_info_check_reserved_words(id)) |
| 498 | goto __change; |
| 499 | for (i = 0; i < snd_ecards_limit; i++) { |
| 500 | if (snd_cards[i] && !strcmp(snd_cards[i]->id, id)) |
| 501 | goto __change; |
| 502 | } |
| 503 | break; |
| 504 | |
| 505 | __change: |
| 506 | len = strlen(id); |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 507 | if (idx_flag) { |
| 508 | if (id[len-1] != '9') |
| 509 | id[len-1]++; |
| 510 | else |
| 511 | id[len-1] = 'A'; |
| 512 | } else if ((size_t)len <= sizeof(card->id) - 3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | strcat(id, "_1"); |
| 514 | idx_flag++; |
| 515 | } else { |
| 516 | spos = id + len - 2; |
| 517 | if ((size_t)len <= sizeof(card->id) - 2) |
| 518 | spos++; |
| 519 | *spos++ = '_'; |
| 520 | *spos++ = '1'; |
| 521 | *spos++ = '\0'; |
| 522 | idx_flag++; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * snd_card_register - register the soundcard |
| 529 | * @card: soundcard structure |
| 530 | * |
| 531 | * This function registers all the devices assigned to the soundcard. |
| 532 | * Until calling this, the ALSA control interface is blocked from the |
| 533 | * external accesses. Thus, you should call this function at the end |
| 534 | * of the initialization of the card. |
| 535 | * |
| 536 | * Returns zero otherwise a negative error code if the registrain failed. |
| 537 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 538 | int snd_card_register(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
| 540 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 542 | snd_assert(card != NULL, return -EINVAL); |
Takashi Iwai | 7d2aae1 | 2007-01-26 12:40:31 +0100 | [diff] [blame] | 543 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 544 | if (!card->card_dev) { |
| 545 | card->card_dev = device_create(sound_class, card->dev, 0, |
| 546 | "card%i", card->number); |
| 547 | if (IS_ERR(card->card_dev)) |
| 548 | card->card_dev = NULL; |
Greg Kroah-Hartman | d80f19f | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 549 | } |
Takashi Iwai | 7d2aae1 | 2007-01-26 12:40:31 +0100 | [diff] [blame] | 550 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if ((err = snd_device_register_all(card)) < 0) |
| 552 | return err; |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 553 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | if (snd_cards[card->number]) { |
| 555 | /* already registered */ |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 556 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | if (card->id[0] == '\0') |
| 560 | choose_default_id(card); |
| 561 | snd_cards[card->number] = card; |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 562 | mutex_unlock(&snd_card_mutex); |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 563 | init_info_for_card(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) |
| 565 | if (snd_mixer_oss_notify_callback) |
| 566 | snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER); |
| 567 | #endif |
| 568 | return 0; |
| 569 | } |
| 570 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 571 | EXPORT_SYMBOL(snd_card_register); |
| 572 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 573 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 574 | static struct snd_info_entry *snd_card_info_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Takashi Iwai | a381a7a | 2005-11-17 15:55:49 +0100 | [diff] [blame] | 576 | static void snd_card_info_read(struct snd_info_entry *entry, |
| 577 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
| 579 | int idx, count; |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 580 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | for (idx = count = 0; idx < SNDRV_CARDS; idx++) { |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 583 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | if ((card = snd_cards[idx]) != NULL) { |
| 585 | count++; |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 586 | snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | idx, |
| 588 | card->id, |
| 589 | card->driver, |
| 590 | card->shortname); |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 591 | snd_iprintf(buffer, " %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | card->longname); |
| 593 | } |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 594 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | if (!count) |
| 597 | snd_iprintf(buffer, "--- no soundcards ---\n"); |
| 598 | } |
| 599 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 600 | #ifdef CONFIG_SND_OSSEMUL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 602 | void snd_card_info_read_oss(struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
| 604 | int idx, count; |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 605 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | for (idx = count = 0; idx < SNDRV_CARDS; idx++) { |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 608 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | if ((card = snd_cards[idx]) != NULL) { |
| 610 | count++; |
| 611 | snd_iprintf(buffer, "%s\n", card->longname); |
| 612 | } |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 613 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | if (!count) { |
| 616 | snd_iprintf(buffer, "--- no soundcards ---\n"); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | #endif |
| 621 | |
| 622 | #ifdef MODULE |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 623 | static struct snd_info_entry *snd_card_module_info_entry; |
| 624 | static void snd_card_module_info_read(struct snd_info_entry *entry, |
| 625 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
| 627 | int idx; |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 628 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | for (idx = 0; idx < SNDRV_CARDS; idx++) { |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 631 | mutex_lock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | if ((card = snd_cards[idx]) != NULL) |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 633 | snd_iprintf(buffer, "%2i %s\n", |
| 634 | idx, card->module->name); |
Takashi Iwai | 746df94 | 2006-05-15 19:49:05 +0200 | [diff] [blame] | 635 | mutex_unlock(&snd_card_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | #endif |
| 639 | |
| 640 | int __init snd_card_info_init(void) |
| 641 | { |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 642 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
| 644 | entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL); |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 645 | if (! entry) |
| 646 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | entry->c.text.read = snd_card_info_read; |
| 648 | if (snd_info_register(entry) < 0) { |
| 649 | snd_info_free_entry(entry); |
| 650 | return -ENOMEM; |
| 651 | } |
| 652 | snd_card_info_entry = entry; |
| 653 | |
| 654 | #ifdef MODULE |
| 655 | entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL); |
| 656 | if (entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | entry->c.text.read = snd_card_module_info_read; |
| 658 | if (snd_info_register(entry) < 0) |
| 659 | snd_info_free_entry(entry); |
| 660 | else |
| 661 | snd_card_module_info_entry = entry; |
| 662 | } |
| 663 | #endif |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | int __exit snd_card_info_done(void) |
| 669 | { |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 670 | snd_info_free_entry(snd_card_info_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | #ifdef MODULE |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 672 | snd_info_free_entry(snd_card_module_info_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | #endif |
| 674 | return 0; |
| 675 | } |
| 676 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 677 | #endif /* CONFIG_PROC_FS */ |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | /** |
| 680 | * snd_component_add - add a component string |
| 681 | * @card: soundcard structure |
| 682 | * @component: the component id string |
| 683 | * |
| 684 | * This function adds the component id string to the supported list. |
| 685 | * The component can be referred from the alsa-lib. |
| 686 | * |
| 687 | * Returns zero otherwise a negative error code. |
| 688 | */ |
| 689 | |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 690 | int snd_component_add(struct snd_card *card, const char *component) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
| 692 | char *ptr; |
| 693 | int len = strlen(component); |
| 694 | |
| 695 | ptr = strstr(card->components, component); |
| 696 | if (ptr != NULL) { |
| 697 | if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */ |
| 698 | return 1; |
| 699 | } |
| 700 | if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) { |
| 701 | snd_BUG(); |
| 702 | return -ENOMEM; |
| 703 | } |
| 704 | if (card->components[0] != '\0') |
| 705 | strcat(card->components, " "); |
| 706 | strcat(card->components, component); |
| 707 | return 0; |
| 708 | } |
| 709 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 710 | EXPORT_SYMBOL(snd_component_add); |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | /** |
| 713 | * snd_card_file_add - add the file to the file list of the card |
| 714 | * @card: soundcard structure |
| 715 | * @file: file pointer |
| 716 | * |
| 717 | * This function adds the file to the file linked-list of the card. |
| 718 | * This linked-list is used to keep tracking the connection state, |
| 719 | * and to avoid the release of busy resources by hotplug. |
| 720 | * |
| 721 | * Returns zero or a negative error code. |
| 722 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 723 | int snd_card_file_add(struct snd_card *card, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | { |
| 725 | struct snd_monitor_file *mfile; |
| 726 | |
| 727 | mfile = kmalloc(sizeof(*mfile), GFP_KERNEL); |
| 728 | if (mfile == NULL) |
| 729 | return -ENOMEM; |
| 730 | mfile->file = file; |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 731 | mfile->disconnected_f_op = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | mfile->next = NULL; |
| 733 | spin_lock(&card->files_lock); |
| 734 | if (card->shutdown) { |
| 735 | spin_unlock(&card->files_lock); |
| 736 | kfree(mfile); |
| 737 | return -ENODEV; |
| 738 | } |
| 739 | mfile->next = card->files; |
| 740 | card->files = mfile; |
| 741 | spin_unlock(&card->files_lock); |
| 742 | return 0; |
| 743 | } |
| 744 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 745 | EXPORT_SYMBOL(snd_card_file_add); |
| 746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | /** |
| 748 | * snd_card_file_remove - remove the file from the file list |
| 749 | * @card: soundcard structure |
| 750 | * @file: file pointer |
| 751 | * |
| 752 | * This function removes the file formerly added to the card via |
| 753 | * snd_card_file_add() function. |
Takashi Iwai | 2b29b13 | 2006-06-23 14:38:26 +0200 | [diff] [blame] | 754 | * If all files are removed and snd_card_free_when_closed() was |
| 755 | * called beforehand, it processes the pending release of |
| 756 | * resources. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | * |
| 758 | * Returns zero or a negative error code. |
| 759 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 760 | int snd_card_file_remove(struct snd_card *card, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
| 762 | struct snd_monitor_file *mfile, *pfile = NULL; |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 763 | int last_close = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | spin_lock(&card->files_lock); |
| 766 | mfile = card->files; |
| 767 | while (mfile) { |
| 768 | if (mfile->file == file) { |
| 769 | if (pfile) |
| 770 | pfile->next = mfile->next; |
| 771 | else |
| 772 | card->files = mfile->next; |
| 773 | break; |
| 774 | } |
| 775 | pfile = mfile; |
| 776 | mfile = mfile->next; |
| 777 | } |
Karsten Wiese | a9edfc6 | 2006-10-06 16:08:27 +0200 | [diff] [blame] | 778 | if (mfile && mfile->disconnected_f_op) { |
| 779 | fops_put(mfile->disconnected_f_op); |
| 780 | spin_lock(&shutdown_lock); |
| 781 | list_del(&mfile->shutdown_list); |
| 782 | spin_unlock(&shutdown_lock); |
| 783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | if (card->files == NULL) |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 785 | last_close = 1; |
| 786 | spin_unlock(&card->files_lock); |
| 787 | if (last_close) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | wake_up(&card->shutdown_sleep); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 789 | if (card->free_on_last_close) |
| 790 | snd_card_do_free(card); |
| 791 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (!mfile) { |
| 793 | snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file); |
| 794 | return -ENOENT; |
| 795 | } |
| 796 | kfree(mfile); |
| 797 | return 0; |
| 798 | } |
| 799 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 800 | EXPORT_SYMBOL(snd_card_file_remove); |
| 801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | #ifdef CONFIG_PM |
| 803 | /** |
| 804 | * snd_power_wait - wait until the power-state is changed. |
| 805 | * @card: soundcard structure |
| 806 | * @power_state: expected power state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | * |
| 808 | * Waits until the power-state is changed. |
| 809 | * |
| 810 | * Note: the power lock must be active before call. |
| 811 | */ |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame] | 812 | int snd_power_wait(struct snd_card *card, unsigned int power_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | { |
| 814 | wait_queue_t wait; |
| 815 | int result = 0; |
| 816 | |
| 817 | /* fastpath */ |
| 818 | if (snd_power_get_state(card) == power_state) |
| 819 | return 0; |
| 820 | init_waitqueue_entry(&wait, current); |
| 821 | add_wait_queue(&card->power_sleep, &wait); |
| 822 | while (1) { |
| 823 | if (card->shutdown) { |
| 824 | result = -ENODEV; |
| 825 | break; |
| 826 | } |
| 827 | if (snd_power_get_state(card) == power_state) |
| 828 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 830 | snd_power_unlock(card); |
| 831 | schedule_timeout(30 * HZ); |
| 832 | snd_power_lock(card); |
| 833 | } |
| 834 | remove_wait_queue(&card->power_sleep, &wait); |
| 835 | return result; |
| 836 | } |
| 837 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 838 | EXPORT_SYMBOL(snd_power_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | #endif /* CONFIG_PM */ |