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