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