xref: /linux/sound/core/init.c (revision b0148a98ec5151fec82064d95f11eb9efbc628ea)
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 			if (~snd_cards_lock & idx & 1<<idx2) {
118 				idx = idx2;
119 				if (idx >= snd_ecards_limit)
120 					snd_ecards_limit = idx + 1;
121 				break;
122 			}
123 	} else if (idx < snd_ecards_limit) {
124 		if (snd_cards_lock & (1 << idx))
125 			err = -ENODEV;	/* invalid */
126 	} else if (idx < SNDRV_CARDS)
127 		snd_ecards_limit = idx + 1; /* increase the limit */
128 	else
129 		err = -ENODEV;
130 	if (idx < 0 || err < 0) {
131 		mutex_unlock(&snd_card_mutex);
132 		snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
133 		goto __error;
134 	}
135 	snd_cards_lock |= 1 << idx;		/* lock it */
136 	mutex_unlock(&snd_card_mutex);
137 	card->number = idx;
138 	card->module = module;
139 	INIT_LIST_HEAD(&card->devices);
140 	init_rwsem(&card->controls_rwsem);
141 	rwlock_init(&card->ctl_files_rwlock);
142 	INIT_LIST_HEAD(&card->controls);
143 	INIT_LIST_HEAD(&card->ctl_files);
144 	spin_lock_init(&card->files_lock);
145 	init_waitqueue_head(&card->shutdown_sleep);
146 #ifdef CONFIG_PM
147 	mutex_init(&card->power_lock);
148 	init_waitqueue_head(&card->power_sleep);
149 #endif
150 	/* the control interface cannot be accessed from the user space until */
151 	/* snd_cards_bitmask and snd_cards are set with snd_card_register */
152 	if ((err = snd_ctl_create(card)) < 0) {
153 		snd_printd("unable to register control minors\n");
154 		goto __error;
155 	}
156 	if ((err = snd_info_card_create(card)) < 0) {
157 		snd_printd("unable to create card info\n");
158 		goto __error_ctl;
159 	}
160 	if (extra_size > 0)
161 		card->private_data = (char *)card + sizeof(struct snd_card);
162 	return card;
163 
164       __error_ctl:
165 	snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
166       __error:
167 	kfree(card);
168       	return NULL;
169 }
170 
171 EXPORT_SYMBOL(snd_card_new);
172 
173 /* return non-zero if a card is already locked */
174 int snd_card_locked(int card)
175 {
176 	int locked;
177 
178 	mutex_lock(&snd_card_mutex);
179 	locked = snd_cards_lock & (1 << card);
180 	mutex_unlock(&snd_card_mutex);
181 	return locked;
182 }
183 
184 static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
185 {
186 	return -ENODEV;
187 }
188 
189 static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
190 				   size_t count, loff_t *offset)
191 {
192 	return -ENODEV;
193 }
194 
195 static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
196 				    size_t count, loff_t *offset)
197 {
198 	return -ENODEV;
199 }
200 
201 static int snd_disconnect_release(struct inode *inode, struct file *file)
202 {
203 	struct snd_monitor_file *df = NULL, *_df;
204 
205 	spin_lock(&shutdown_lock);
206 	list_for_each_entry(_df, &shutdown_files, shutdown_list) {
207 		if (_df->file == file) {
208 			df = _df;
209 			break;
210 		}
211 	}
212 	spin_unlock(&shutdown_lock);
213 
214 	if (likely(df))
215 		return df->disconnected_f_op->release(inode, file);
216 
217 	panic("%s(%p, %p) failed!", __FUNCTION__, inode, file);
218 }
219 
220 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
221 {
222 	return POLLERR | POLLNVAL;
223 }
224 
225 static long snd_disconnect_ioctl(struct file *file,
226 				 unsigned int cmd, unsigned long arg)
227 {
228 	return -ENODEV;
229 }
230 
231 static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
232 {
233 	return -ENODEV;
234 }
235 
236 static int snd_disconnect_fasync(int fd, struct file *file, int on)
237 {
238 	return -ENODEV;
239 }
240 
241 static struct file_operations snd_shutdown_f_ops =
242 {
243 	.owner = 	THIS_MODULE,
244 	.llseek =	snd_disconnect_llseek,
245 	.read = 	snd_disconnect_read,
246 	.write =	snd_disconnect_write,
247 	.release =	snd_disconnect_release,
248 	.poll =		snd_disconnect_poll,
249 	.unlocked_ioctl = snd_disconnect_ioctl,
250 #ifdef CONFIG_COMPAT
251 	.compat_ioctl = snd_disconnect_ioctl,
252 #endif
253 	.mmap =		snd_disconnect_mmap,
254 	.fasync =	snd_disconnect_fasync
255 };
256 
257 /**
258  *  snd_card_disconnect - disconnect all APIs from the file-operations (user space)
259  *  @card: soundcard structure
260  *
261  *  Disconnects all APIs from the file-operations (user space).
262  *
263  *  Returns zero, otherwise a negative error code.
264  *
265  *  Note: The current implementation replaces all active file->f_op with special
266  *        dummy file operations (they do nothing except release).
267  */
268 int snd_card_disconnect(struct snd_card *card)
269 {
270 	struct snd_monitor_file *mfile;
271 	struct file *file;
272 	int err;
273 
274 	spin_lock(&card->files_lock);
275 	if (card->shutdown) {
276 		spin_unlock(&card->files_lock);
277 		return 0;
278 	}
279 	card->shutdown = 1;
280 	spin_unlock(&card->files_lock);
281 
282 	/* phase 1: disable fops (user space) operations for ALSA API */
283 	mutex_lock(&snd_card_mutex);
284 	snd_cards[card->number] = NULL;
285 	mutex_unlock(&snd_card_mutex);
286 
287 	/* phase 2: replace file->f_op with special dummy operations */
288 
289 	spin_lock(&card->files_lock);
290 	mfile = card->files;
291 	while (mfile) {
292 		file = mfile->file;
293 
294 		/* it's critical part, use endless loop */
295 		/* we have no room to fail */
296 		mfile->disconnected_f_op = mfile->file->f_op;
297 
298 		spin_lock(&shutdown_lock);
299 		list_add(&mfile->shutdown_list, &shutdown_files);
300 		spin_unlock(&shutdown_lock);
301 
302 		fops_get(&snd_shutdown_f_ops);
303 		mfile->file->f_op = &snd_shutdown_f_ops;
304 
305 		mfile = mfile->next;
306 	}
307 	spin_unlock(&card->files_lock);
308 
309 	/* phase 3: notify all connected devices about disconnection */
310 	/* at this point, they cannot respond to any calls except release() */
311 
312 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
313 	if (snd_mixer_oss_notify_callback)
314 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
315 #endif
316 
317 	/* notify all devices that we are disconnected */
318 	err = snd_device_disconnect_all(card);
319 	if (err < 0)
320 		snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
321 
322 	snd_info_card_disconnect(card);
323 	return 0;
324 }
325 
326 EXPORT_SYMBOL(snd_card_disconnect);
327 
328 /**
329  *  snd_card_free - frees given soundcard structure
330  *  @card: soundcard structure
331  *
332  *  This function releases the soundcard structure and the all assigned
333  *  devices automatically.  That is, you don't have to release the devices
334  *  by yourself.
335  *
336  *  Returns zero. Frees all associated devices and frees the control
337  *  interface associated to given soundcard.
338  */
339 static int snd_card_do_free(struct snd_card *card)
340 {
341 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
342 	if (snd_mixer_oss_notify_callback)
343 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
344 #endif
345 	if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
346 		snd_printk(KERN_ERR "unable to free all devices (pre)\n");
347 		/* Fatal, but this situation should never occur */
348 	}
349 	if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
350 		snd_printk(KERN_ERR "unable to free all devices (normal)\n");
351 		/* Fatal, but this situation should never occur */
352 	}
353 	if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
354 		snd_printk(KERN_ERR "unable to free all devices (post)\n");
355 		/* Fatal, but this situation should never occur */
356 	}
357 	if (card->private_free)
358 		card->private_free(card);
359 	snd_info_free_entry(card->proc_id);
360 	if (snd_info_card_free(card) < 0) {
361 		snd_printk(KERN_WARNING "unable to free card info\n");
362 		/* Not fatal error */
363 	}
364 #ifndef CONFIG_SYSFS_DEPRECATED
365 	if (card->card_dev)
366 		device_unregister(card->card_dev);
367 #endif
368 	kfree(card);
369 	return 0;
370 }
371 
372 static int snd_card_free_prepare(struct snd_card *card)
373 {
374 	if (card == NULL)
375 		return -EINVAL;
376 	(void) snd_card_disconnect(card);
377 	mutex_lock(&snd_card_mutex);
378 	snd_cards[card->number] = NULL;
379 	snd_cards_lock &= ~(1 << card->number);
380 	mutex_unlock(&snd_card_mutex);
381 #ifdef CONFIG_PM
382 	wake_up(&card->power_sleep);
383 #endif
384 	return 0;
385 }
386 
387 int snd_card_free_when_closed(struct snd_card *card)
388 {
389 	int free_now = 0;
390 	int ret = snd_card_free_prepare(card);
391 	if (ret)
392 		return ret;
393 
394 	spin_lock(&card->files_lock);
395 	if (card->files == NULL)
396 		free_now = 1;
397 	else
398 		card->free_on_last_close = 1;
399 	spin_unlock(&card->files_lock);
400 
401 	if (free_now)
402 		snd_card_do_free(card);
403 	return 0;
404 }
405 
406 EXPORT_SYMBOL(snd_card_free_when_closed);
407 
408 int snd_card_free(struct snd_card *card)
409 {
410 	int ret = snd_card_free_prepare(card);
411 	if (ret)
412 		return ret;
413 
414 	/* wait, until all devices are ready for the free operation */
415 	wait_event(card->shutdown_sleep, card->files == NULL);
416 	snd_card_do_free(card);
417 	return 0;
418 }
419 
420 EXPORT_SYMBOL(snd_card_free);
421 
422 static void choose_default_id(struct snd_card *card)
423 {
424 	int i, len, idx_flag = 0, loops = SNDRV_CARDS;
425 	char *id, *spos;
426 
427 	id = spos = card->shortname;
428 	while (*id != '\0') {
429 		if (*id == ' ')
430 			spos = id + 1;
431 		id++;
432 	}
433 	id = card->id;
434 	while (*spos != '\0' && !isalnum(*spos))
435 		spos++;
436 	if (isdigit(*spos))
437 		*id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
438 	while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
439 		if (isalnum(*spos))
440 			*id++ = *spos;
441 		spos++;
442 	}
443 	*id = '\0';
444 
445 	id = card->id;
446 
447 	if (*id == '\0')
448 		strcpy(id, "default");
449 
450 	while (1) {
451 	      	if (loops-- == 0) {
452       			snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
453       			strcpy(card->id, card->proc_root->name);
454       			return;
455       		}
456 	      	if (!snd_info_check_reserved_words(id))
457       			goto __change;
458 		for (i = 0; i < snd_ecards_limit; i++) {
459 			if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
460 				goto __change;
461 		}
462 		break;
463 
464 	      __change:
465 		len = strlen(id);
466 		if (idx_flag) {
467 			if (id[len-1] != '9')
468 				id[len-1]++;
469 			else
470 				id[len-1] = 'A';
471 		} else if ((size_t)len <= sizeof(card->id) - 3) {
472 			strcat(id, "_1");
473 			idx_flag++;
474 		} else {
475 			spos = id + len - 2;
476 			if ((size_t)len <= sizeof(card->id) - 2)
477 				spos++;
478 			*spos++ = '_';
479 			*spos++ = '1';
480 			*spos++ = '\0';
481 			idx_flag++;
482 		}
483 	}
484 }
485 
486 /**
487  *  snd_card_register - register the soundcard
488  *  @card: soundcard structure
489  *
490  *  This function registers all the devices assigned to the soundcard.
491  *  Until calling this, the ALSA control interface is blocked from the
492  *  external accesses.  Thus, you should call this function at the end
493  *  of the initialization of the card.
494  *
495  *  Returns zero otherwise a negative error code if the registrain failed.
496  */
497 int snd_card_register(struct snd_card *card)
498 {
499 	int err;
500 
501 	snd_assert(card != NULL, return -EINVAL);
502 #ifndef CONFIG_SYSFS_DEPRECATED
503 	if (!card->card_dev) {
504 		card->card_dev = device_create(sound_class, card->dev, 0,
505 					       "card%i", card->number);
506 		if (IS_ERR(card->card_dev))
507 			card->card_dev = NULL;
508 	}
509 #endif
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->disconnected_f_op = NULL;
691 	mfile->next = NULL;
692 	spin_lock(&card->files_lock);
693 	if (card->shutdown) {
694 		spin_unlock(&card->files_lock);
695 		kfree(mfile);
696 		return -ENODEV;
697 	}
698 	mfile->next = card->files;
699 	card->files = mfile;
700 	spin_unlock(&card->files_lock);
701 	return 0;
702 }
703 
704 EXPORT_SYMBOL(snd_card_file_add);
705 
706 /**
707  *  snd_card_file_remove - remove the file from the file list
708  *  @card: soundcard structure
709  *  @file: file pointer
710  *
711  *  This function removes the file formerly added to the card via
712  *  snd_card_file_add() function.
713  *  If all files are removed and snd_card_free_when_closed() was
714  *  called beforehand, it processes the pending release of
715  *  resources.
716  *
717  *  Returns zero or a negative error code.
718  */
719 int snd_card_file_remove(struct snd_card *card, struct file *file)
720 {
721 	struct snd_monitor_file *mfile, *pfile = NULL;
722 	int last_close = 0;
723 
724 	spin_lock(&card->files_lock);
725 	mfile = card->files;
726 	while (mfile) {
727 		if (mfile->file == file) {
728 			if (pfile)
729 				pfile->next = mfile->next;
730 			else
731 				card->files = mfile->next;
732 			break;
733 		}
734 		pfile = mfile;
735 		mfile = mfile->next;
736 	}
737 	if (mfile && mfile->disconnected_f_op) {
738 		fops_put(mfile->disconnected_f_op);
739 		spin_lock(&shutdown_lock);
740 		list_del(&mfile->shutdown_list);
741 		spin_unlock(&shutdown_lock);
742 	}
743 	if (card->files == NULL)
744 		last_close = 1;
745 	spin_unlock(&card->files_lock);
746 	if (last_close) {
747 		wake_up(&card->shutdown_sleep);
748 		if (card->free_on_last_close)
749 			snd_card_do_free(card);
750 	}
751 	if (!mfile) {
752 		snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
753 		return -ENOENT;
754 	}
755 	kfree(mfile);
756 	return 0;
757 }
758 
759 EXPORT_SYMBOL(snd_card_file_remove);
760 
761 #ifdef CONFIG_PM
762 /**
763  *  snd_power_wait - wait until the power-state is changed.
764  *  @card: soundcard structure
765  *  @power_state: expected power state
766  *
767  *  Waits until the power-state is changed.
768  *
769  *  Note: the power lock must be active before call.
770  */
771 int snd_power_wait(struct snd_card *card, unsigned int power_state)
772 {
773 	wait_queue_t wait;
774 	int result = 0;
775 
776 	/* fastpath */
777 	if (snd_power_get_state(card) == power_state)
778 		return 0;
779 	init_waitqueue_entry(&wait, current);
780 	add_wait_queue(&card->power_sleep, &wait);
781 	while (1) {
782 		if (card->shutdown) {
783 			result = -ENODEV;
784 			break;
785 		}
786 		if (snd_power_get_state(card) == power_state)
787 			break;
788 		set_current_state(TASK_UNINTERRUPTIBLE);
789 		snd_power_unlock(card);
790 		schedule_timeout(30 * HZ);
791 		snd_power_lock(card);
792 	}
793 	remove_wait_queue(&card->power_sleep, &wait);
794 	return result;
795 }
796 
797 EXPORT_SYMBOL(snd_power_wait);
798 #endif /* CONFIG_PM */
799