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