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