db61037337427b9adc881868fa104c9ec015bc06
[linux-2.6.git] / sound / core / init.c
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>
31
32 #include <sound/core.h>
33 #include <sound/control.h>
34 #include <sound/info.h>
35
36 static DEFINE_SPINLOCK(shutdown_lock);
37 static LIST_HEAD(shutdown_files);
38
39 static struct file_operations snd_shutdown_f_ops;
40
41 static unsigned int snd_cards_lock;     /* locked for registering/using */
42 struct snd_card *snd_cards[SNDRV_CARDS];
43 EXPORT_SYMBOL(snd_cards);
44
45 static DEFINE_MUTEX(snd_card_mutex);
46
47 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
48 int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
49 EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
50 #endif
51
52 #ifdef CONFIG_PROC_FS
53 static void snd_card_id_read(struct snd_info_entry *entry,
54                              struct snd_info_buffer *buffer)
55 {
56         snd_iprintf(buffer, "%s\n", entry->card->id);
57 }
58
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         }
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
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  *
93  *  Returns kmallocated snd_card structure. Creates the ALSA control interface
94  *  (which is blocked until snd_card_register function is called).
95  */
96 struct snd_card *snd_card_new(int idx, const char *xid,
97                          struct module *module, int extra_size)
98 {
99         struct snd_card *card;
100         int err;
101
102         if (extra_size < 0)
103                 extra_size = 0;
104         card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
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;
113         mutex_lock(&snd_card_mutex);
114         if (idx < 0) {
115                 int idx2;
116                 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
117                         /* idx == -1 == 0xffff means: take any free slot */
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                         }
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         }
135         if (idx < 0 || err < 0) {
136                 mutex_unlock(&snd_card_mutex);
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);
139                 goto __error;
140         }
141         snd_cards_lock |= 1 << idx;             /* lock it */
142         mutex_unlock(&snd_card_mutex);
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);
152 #ifdef CONFIG_PM
153         mutex_init(&card->power_lock);
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)
167                 card->private_data = (char *)card + sizeof(struct snd_card);
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
177 EXPORT_SYMBOL(snd_card_new);
178
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
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
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
226 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
227 {
228         return POLLERR | POLLNVAL;
229 }
230
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
247 static struct file_operations snd_shutdown_f_ops =
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
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  */
274 int snd_card_disconnect(struct snd_card *card)
275 {
276         struct snd_monitor_file *mfile;
277         struct file *file;
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 */
289         mutex_lock(&snd_card_mutex);
290         snd_cards[card->number] = NULL;
291         mutex_unlock(&snd_card_mutex);
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 */
302                 mfile->disconnected_f_op = mfile->file->f_op;
303
304                 spin_lock(&shutdown_lock);
305                 list_add(&mfile->shutdown_list, &shutdown_files);
306                 spin_unlock(&shutdown_lock);
307
308                 fops_get(&snd_shutdown_f_ops);
309                 mfile->file->f_op = &snd_shutdown_f_ops;
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
328         snd_info_card_disconnect(card);
329         return 0;       
330 }
331
332 EXPORT_SYMBOL(snd_card_disconnect);
333
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  */
345 static int snd_card_do_free(struct snd_card *card)
346 {
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);
365         snd_info_free_entry(card->proc_id);
366         if (snd_info_card_free(card) < 0) {
367                 snd_printk(KERN_WARNING "unable to free card info\n");
368                 /* Not fatal error */
369         }
370 #ifndef CONFIG_SYSFS_DEPRECATED
371         if (card->card_dev)
372                 device_unregister(card->card_dev);
373 #endif
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);
383         mutex_lock(&snd_card_mutex);
384         snd_cards[card->number] = NULL;
385         snd_cards_lock &= ~(1 << card->number);
386         mutex_unlock(&snd_card_mutex);
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);
423         return 0;
424 }
425
426 EXPORT_SYMBOL(snd_card_free);
427
428 static void choose_default_id(struct snd_card *card)
429 {
430         int i, len, idx_flag = 0, loops = SNDRV_CARDS;
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);
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) {
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  */
503 int snd_card_register(struct snd_card *card)
504 {
505         int err;
506
507         snd_assert(card != NULL, return -EINVAL);
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;
514         }
515 #endif
516         if ((err = snd_device_register_all(card)) < 0)
517                 return err;
518         mutex_lock(&snd_card_mutex);
519         if (snd_cards[card->number]) {
520                 /* already registered */
521                 mutex_unlock(&snd_card_mutex);
522                 return 0;
523         }
524         if (card->id[0] == '\0')
525                 choose_default_id(card);
526         snd_cards[card->number] = card;
527         mutex_unlock(&snd_card_mutex);
528         init_info_for_card(card);
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
536 EXPORT_SYMBOL(snd_card_register);
537
538 #ifdef CONFIG_PROC_FS
539 static struct snd_info_entry *snd_card_info_entry;
540
541 static void snd_card_info_read(struct snd_info_entry *entry,
542                                struct snd_info_buffer *buffer)
543 {
544         int idx, count;
545         struct snd_card *card;
546
547         for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
548                 mutex_lock(&snd_card_mutex);
549                 if ((card = snd_cards[idx]) != NULL) {
550                         count++;
551                         snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
552                                         idx,
553                                         card->id,
554                                         card->driver,
555                                         card->shortname);
556                         snd_iprintf(buffer, "                      %s\n",
557                                         card->longname);
558                 }
559                 mutex_unlock(&snd_card_mutex);
560         }
561         if (!count)
562                 snd_iprintf(buffer, "--- no soundcards ---\n");
563 }
564
565 #ifdef CONFIG_SND_OSSEMUL
566
567 void snd_card_info_read_oss(struct snd_info_buffer *buffer)
568 {
569         int idx, count;
570         struct snd_card *card;
571
572         for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
573                 mutex_lock(&snd_card_mutex);
574                 if ((card = snd_cards[idx]) != NULL) {
575                         count++;
576                         snd_iprintf(buffer, "%s\n", card->longname);
577                 }
578                 mutex_unlock(&snd_card_mutex);
579         }
580         if (!count) {
581                 snd_iprintf(buffer, "--- no soundcards ---\n");
582         }
583 }
584
585 #endif
586
587 #ifdef MODULE
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)
591 {
592         int idx;
593         struct snd_card *card;
594
595         for (idx = 0; idx < SNDRV_CARDS; idx++) {
596                 mutex_lock(&snd_card_mutex);
597                 if ((card = snd_cards[idx]) != NULL)
598                         snd_iprintf(buffer, "%2i %s\n",
599                                     idx, card->module->name);
600                 mutex_unlock(&snd_card_mutex);
601         }
602 }
603 #endif
604
605 int __init snd_card_info_init(void)
606 {
607         struct snd_info_entry *entry;
608
609         entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
610         if (! entry)
611                 return -ENOMEM;
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) {
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 {
635         snd_info_free_entry(snd_card_info_entry);
636 #ifdef MODULE
637         snd_info_free_entry(snd_card_module_info_entry);
638 #endif
639         return 0;
640 }
641
642 #endif /* CONFIG_PROC_FS */
643
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   
655 int snd_component_add(struct snd_card *card, const char *component)
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
675 EXPORT_SYMBOL(snd_component_add);
676
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  */
688 int snd_card_file_add(struct snd_card *card, struct file *file)
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;
696         mfile->disconnected_f_op = NULL;
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
710 EXPORT_SYMBOL(snd_card_file_add);
711
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.
719  *  If all files are removed and snd_card_free_when_closed() was
720  *  called beforehand, it processes the pending release of
721  *  resources.
722  *
723  *  Returns zero or a negative error code.
724  */
725 int snd_card_file_remove(struct snd_card *card, struct file *file)
726 {
727         struct snd_monitor_file *mfile, *pfile = NULL;
728         int last_close = 0;
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         }
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         }
749         if (card->files == NULL)
750                 last_close = 1;
751         spin_unlock(&card->files_lock);
752         if (last_close) {
753                 wake_up(&card->shutdown_sleep);
754                 if (card->free_on_last_close)
755                         snd_card_do_free(card);
756         }
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
765 EXPORT_SYMBOL(snd_card_file_remove);
766
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
772  *
773  *  Waits until the power-state is changed.
774  *
775  *  Note: the power lock must be active before call.
776  */
777 int snd_power_wait(struct snd_card *card, unsigned int power_state)
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;
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
803 EXPORT_SYMBOL(snd_power_wait);
804 #endif /* CONFIG_PM */