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