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