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