blob: 728bb2ce0bc773c2e168e9f0fe2ff6087e687250 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Russell Kingd052d1b2005-10-29 19:07:23 +010031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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
41unsigned int snd_cards_lock = 0; /* locked for registering/using */
Takashi Iwai512bbd62005-11-17 13:51:18 +010042struct snd_card *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL};
Linus Torvalds1da177e2005-04-16 15:20:36 -070043DEFINE_RWLOCK(snd_card_rwlock);
44
45#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
Takashi Iwai512bbd62005-11-17 13:51:18 +010046int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#endif
48
Takashi Iwai512bbd62005-11-17 13:51:18 +010049static void snd_card_id_read(struct snd_info_entry *entry,
50 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 snd_iprintf(buffer, "%s\n", entry->card->id);
53}
54
55static void snd_card_free_thread(void * __card);
56
57/**
58 * snd_card_new - create and initialize a soundcard structure
59 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
60 * @xid: card identification (ASCII string)
61 * @module: top level module for locking
62 * @extra_size: allocate this extra size after the main soundcard structure
63 *
64 * Creates and initializes a soundcard structure.
65 *
Takashi Iwai512bbd62005-11-17 13:51:18 +010066 * Returns kmallocated snd_card structure. Creates the ALSA control interface
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * (which is blocked until snd_card_register function is called).
68 */
Takashi Iwai512bbd62005-11-17 13:51:18 +010069struct snd_card *snd_card_new(int idx, const char *xid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct module *module, int extra_size)
71{
Takashi Iwai512bbd62005-11-17 13:51:18 +010072 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int err;
74
75 if (extra_size < 0)
76 extra_size = 0;
Takashi Iwaica2c0962005-09-09 14:20:23 +020077 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (card == NULL)
79 return NULL;
80 if (xid) {
81 if (!snd_info_check_reserved_words(xid))
82 goto __error;
83 strlcpy(card->id, xid, sizeof(card->id));
84 }
85 err = 0;
86 write_lock(&snd_card_rwlock);
87 if (idx < 0) {
88 int idx2;
89 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
90 if (~snd_cards_lock & idx & 1<<idx2) {
91 idx = idx2;
92 if (idx >= snd_ecards_limit)
93 snd_ecards_limit = idx + 1;
94 break;
95 }
96 } else if (idx < snd_ecards_limit) {
97 if (snd_cards_lock & (1 << idx))
98 err = -ENODEV; /* invalid */
99 } else if (idx < SNDRV_CARDS)
100 snd_ecards_limit = idx + 1; /* increase the limit */
101 else
102 err = -ENODEV;
103 if (idx < 0 || err < 0) {
104 write_unlock(&snd_card_rwlock);
105 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
106 goto __error;
107 }
108 snd_cards_lock |= 1 << idx; /* lock it */
109 write_unlock(&snd_card_rwlock);
110 card->number = idx;
111 card->module = module;
112 INIT_LIST_HEAD(&card->devices);
113 init_rwsem(&card->controls_rwsem);
114 rwlock_init(&card->ctl_files_rwlock);
115 INIT_LIST_HEAD(&card->controls);
116 INIT_LIST_HEAD(&card->ctl_files);
117 spin_lock_init(&card->files_lock);
118 init_waitqueue_head(&card->shutdown_sleep);
119 INIT_WORK(&card->free_workq, snd_card_free_thread, card);
120#ifdef CONFIG_PM
121 init_MUTEX(&card->power_lock);
122 init_waitqueue_head(&card->power_sleep);
123#endif
124 /* the control interface cannot be accessed from the user space until */
125 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
126 if ((err = snd_ctl_create(card)) < 0) {
127 snd_printd("unable to register control minors\n");
128 goto __error;
129 }
130 if ((err = snd_info_card_create(card)) < 0) {
131 snd_printd("unable to create card info\n");
132 goto __error_ctl;
133 }
134 if (extra_size > 0)
Takashi Iwai512bbd62005-11-17 13:51:18 +0100135 card->private_data = (char *)card + sizeof(struct snd_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return card;
137
138 __error_ctl:
139 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
140 __error:
141 kfree(card);
142 return NULL;
143}
144
145static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
146{
147 return POLLERR | POLLNVAL;
148}
149
150/**
151 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
152 * @card: soundcard structure
153 *
154 * Disconnects all APIs from the file-operations (user space).
155 *
156 * Returns zero, otherwise a negative error code.
157 *
158 * Note: The current implementation replaces all active file->f_op with special
159 * dummy file operations (they do nothing except release).
160 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100161int snd_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct snd_monitor_file *mfile;
164 struct file *file;
165 struct snd_shutdown_f_ops *s_f_ops;
166 struct file_operations *f_ops, *old_f_ops;
167 int err;
168
169 spin_lock(&card->files_lock);
170 if (card->shutdown) {
171 spin_unlock(&card->files_lock);
172 return 0;
173 }
174 card->shutdown = 1;
175 spin_unlock(&card->files_lock);
176
177 /* phase 1: disable fops (user space) operations for ALSA API */
178 write_lock(&snd_card_rwlock);
179 snd_cards[card->number] = NULL;
180 write_unlock(&snd_card_rwlock);
181
182 /* phase 2: replace file->f_op with special dummy operations */
183
184 spin_lock(&card->files_lock);
185 mfile = card->files;
186 while (mfile) {
187 file = mfile->file;
188
189 /* it's critical part, use endless loop */
190 /* we have no room to fail */
191 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
192 if (s_f_ops == NULL)
193 panic("Atomic allocation failed for snd_shutdown_f_ops!");
194
195 f_ops = &s_f_ops->f_ops;
196
197 memset(f_ops, 0, sizeof(*f_ops));
198 f_ops->owner = file->f_op->owner;
199 f_ops->release = file->f_op->release;
200 f_ops->poll = snd_disconnect_poll;
201
202 s_f_ops->next = card->s_f_ops;
203 card->s_f_ops = s_f_ops;
204
205 f_ops = fops_get(f_ops);
206
207 old_f_ops = file->f_op;
208 file->f_op = f_ops; /* must be atomic */
209 fops_put(old_f_ops);
210
211 mfile = mfile->next;
212 }
213 spin_unlock(&card->files_lock);
214
215 /* phase 3: notify all connected devices about disconnection */
216 /* at this point, they cannot respond to any calls except release() */
217
218#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
219 if (snd_mixer_oss_notify_callback)
220 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
221#endif
222
223 /* notify all devices that we are disconnected */
224 err = snd_device_disconnect_all(card);
225 if (err < 0)
226 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
227
228 return 0;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
232 * snd_card_free - frees given soundcard structure
233 * @card: soundcard structure
234 *
235 * This function releases the soundcard structure and the all assigned
236 * devices automatically. That is, you don't have to release the devices
237 * by yourself.
238 *
239 * Returns zero. Frees all associated devices and frees the control
240 * interface associated to given soundcard.
241 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100242int snd_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct snd_shutdown_f_ops *s_f_ops;
245
246 if (card == NULL)
247 return -EINVAL;
248 write_lock(&snd_card_rwlock);
249 snd_cards[card->number] = NULL;
250 write_unlock(&snd_card_rwlock);
251
252#ifdef CONFIG_PM
253 wake_up(&card->power_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* wait, until all devices are ready for the free operation */
256 wait_event(card->shutdown_sleep, card->files == NULL);
257
258#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
259 if (snd_mixer_oss_notify_callback)
260 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
261#endif
262 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
263 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
264 /* Fatal, but this situation should never occur */
265 }
266 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
267 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
268 /* Fatal, but this situation should never occur */
269 }
270 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
271 snd_printk(KERN_ERR "unable to free all devices (post)\n");
272 /* Fatal, but this situation should never occur */
273 }
274 if (card->private_free)
275 card->private_free(card);
276 if (card->proc_id)
277 snd_info_unregister(card->proc_id);
278 if (snd_info_card_free(card) < 0) {
279 snd_printk(KERN_WARNING "unable to free card info\n");
280 /* Not fatal error */
281 }
282 while (card->s_f_ops) {
283 s_f_ops = card->s_f_ops;
284 card->s_f_ops = s_f_ops->next;
285 kfree(s_f_ops);
286 }
287 write_lock(&snd_card_rwlock);
288 snd_cards_lock &= ~(1 << card->number);
289 write_unlock(&snd_card_rwlock);
290 kfree(card);
291 return 0;
292}
293
294static void snd_card_free_thread(void * __card)
295{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100296 struct snd_card *card = __card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 struct module * module = card->module;
298
299 if (!try_module_get(module)) {
300 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
301 module = NULL;
302 }
303
304 snd_card_free(card);
305
306 module_put(module);
307}
308
309/**
310 * snd_card_free_in_thread - call snd_card_free() in thread
311 * @card: soundcard structure
312 *
313 * This function schedules the call of snd_card_free() function in a
314 * work queue. When all devices are released (non-busy), the work
315 * is woken up and calls snd_card_free().
316 *
317 * When a card can be disconnected at any time by hotplug service,
318 * this function should be used in disconnect (or detach) callback
319 * instead of calling snd_card_free() directly.
320 *
321 * Returns - zero otherwise a negative error code if the start of thread failed.
322 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100323int snd_card_free_in_thread(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 if (card->files == NULL) {
326 snd_card_free(card);
327 return 0;
328 }
329
330 if (schedule_work(&card->free_workq))
331 return 0;
332
333 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
334 /* try to free the structure immediately */
335 snd_card_free(card);
336 return -EFAULT;
337}
338
Takashi Iwai512bbd62005-11-17 13:51:18 +0100339static void choose_default_id(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 int i, len, idx_flag = 0, loops = 8;
342 char *id, *spos;
343
344 id = spos = card->shortname;
345 while (*id != '\0') {
346 if (*id == ' ')
347 spos = id + 1;
348 id++;
349 }
350 id = card->id;
351 while (*spos != '\0' && !isalnum(*spos))
352 spos++;
353 if (isdigit(*spos))
354 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
355 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
356 if (isalnum(*spos))
357 *id++ = *spos;
358 spos++;
359 }
360 *id = '\0';
361
362 id = card->id;
363
364 if (*id == '\0')
365 strcpy(id, "default");
366
367 while (1) {
368 if (loops-- == 0) {
369 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
370 strcpy(card->id, card->proc_root->name);
371 return;
372 }
373 if (!snd_info_check_reserved_words(id))
374 goto __change;
375 for (i = 0; i < snd_ecards_limit; i++) {
376 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
377 goto __change;
378 }
379 break;
380
381 __change:
382 len = strlen(id);
383 if (idx_flag)
384 id[len-1]++;
385 else if ((size_t)len <= sizeof(card->id) - 3) {
386 strcat(id, "_1");
387 idx_flag++;
388 } else {
389 spos = id + len - 2;
390 if ((size_t)len <= sizeof(card->id) - 2)
391 spos++;
392 *spos++ = '_';
393 *spos++ = '1';
394 *spos++ = '\0';
395 idx_flag++;
396 }
397 }
398}
399
400/**
401 * snd_card_register - register the soundcard
402 * @card: soundcard structure
403 *
404 * This function registers all the devices assigned to the soundcard.
405 * Until calling this, the ALSA control interface is blocked from the
406 * external accesses. Thus, you should call this function at the end
407 * of the initialization of the card.
408 *
409 * Returns zero otherwise a negative error code if the registrain failed.
410 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100411int snd_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 int err;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100414 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200416 snd_assert(card != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if ((err = snd_device_register_all(card)) < 0)
418 return err;
419 write_lock(&snd_card_rwlock);
420 if (snd_cards[card->number]) {
421 /* already registered */
422 write_unlock(&snd_card_rwlock);
423 return 0;
424 }
425 if (card->id[0] == '\0')
426 choose_default_id(card);
427 snd_cards[card->number] = card;
428 write_unlock(&snd_card_rwlock);
429 if ((err = snd_info_card_register(card)) < 0) {
430 snd_printd("unable to create card info\n");
431 goto __skip_info;
432 }
433 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
434 snd_printd("unable to create card entry\n");
435 goto __skip_info;
436 }
437 entry->c.text.read_size = PAGE_SIZE;
438 entry->c.text.read = snd_card_id_read;
439 if (snd_info_register(entry) < 0) {
440 snd_info_free_entry(entry);
441 entry = NULL;
442 }
443 card->proc_id = entry;
444 __skip_info:
445#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
446 if (snd_mixer_oss_notify_callback)
447 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
448#endif
449 return 0;
450}
451
Takashi Iwai512bbd62005-11-17 13:51:18 +0100452static struct snd_info_entry *snd_card_info_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Takashi Iwaia381a7a2005-11-17 15:55:49 +0100454static void snd_card_info_read(struct snd_info_entry *entry,
455 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100458 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
461 read_lock(&snd_card_rwlock);
462 if ((card = snd_cards[idx]) != NULL) {
463 count++;
464 snd_iprintf(buffer, "%i [%-15s]: %s - %s\n",
465 idx,
466 card->id,
467 card->driver,
468 card->shortname);
469 snd_iprintf(buffer, " %s\n",
470 card->longname);
471 }
472 read_unlock(&snd_card_rwlock);
473 }
474 if (!count)
475 snd_iprintf(buffer, "--- no soundcards ---\n");
476}
477
478#if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
479
Takashi Iwai512bbd62005-11-17 13:51:18 +0100480void snd_card_info_read_oss(struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100483 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
486 read_lock(&snd_card_rwlock);
487 if ((card = snd_cards[idx]) != NULL) {
488 count++;
489 snd_iprintf(buffer, "%s\n", card->longname);
490 }
491 read_unlock(&snd_card_rwlock);
492 }
493 if (!count) {
494 snd_iprintf(buffer, "--- no soundcards ---\n");
495 }
496}
497
498#endif
499
500#ifdef MODULE
Takashi Iwai512bbd62005-11-17 13:51:18 +0100501static struct snd_info_entry *snd_card_module_info_entry;
502static void snd_card_module_info_read(struct snd_info_entry *entry,
503 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 int idx;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100506 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 for (idx = 0; idx < SNDRV_CARDS; idx++) {
509 read_lock(&snd_card_rwlock);
510 if ((card = snd_cards[idx]) != NULL)
511 snd_iprintf(buffer, "%i %s\n", idx, card->module->name);
512 read_unlock(&snd_card_rwlock);
513 }
514}
515#endif
516
517int __init snd_card_info_init(void)
518{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100519 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200522 if (! entry)
523 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 entry->c.text.read_size = PAGE_SIZE;
525 entry->c.text.read = snd_card_info_read;
526 if (snd_info_register(entry) < 0) {
527 snd_info_free_entry(entry);
528 return -ENOMEM;
529 }
530 snd_card_info_entry = entry;
531
532#ifdef MODULE
533 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
534 if (entry) {
535 entry->c.text.read_size = PAGE_SIZE;
536 entry->c.text.read = snd_card_module_info_read;
537 if (snd_info_register(entry) < 0)
538 snd_info_free_entry(entry);
539 else
540 snd_card_module_info_entry = entry;
541 }
542#endif
543
544 return 0;
545}
546
547int __exit snd_card_info_done(void)
548{
549 if (snd_card_info_entry)
550 snd_info_unregister(snd_card_info_entry);
551#ifdef MODULE
552 if (snd_card_module_info_entry)
553 snd_info_unregister(snd_card_module_info_entry);
554#endif
555 return 0;
556}
557
558/**
559 * snd_component_add - add a component string
560 * @card: soundcard structure
561 * @component: the component id string
562 *
563 * This function adds the component id string to the supported list.
564 * The component can be referred from the alsa-lib.
565 *
566 * Returns zero otherwise a negative error code.
567 */
568
Takashi Iwai512bbd62005-11-17 13:51:18 +0100569int snd_component_add(struct snd_card *card, const char *component)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 char *ptr;
572 int len = strlen(component);
573
574 ptr = strstr(card->components, component);
575 if (ptr != NULL) {
576 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
577 return 1;
578 }
579 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
580 snd_BUG();
581 return -ENOMEM;
582 }
583 if (card->components[0] != '\0')
584 strcat(card->components, " ");
585 strcat(card->components, component);
586 return 0;
587}
588
589/**
590 * snd_card_file_add - add the file to the file list of the card
591 * @card: soundcard structure
592 * @file: file pointer
593 *
594 * This function adds the file to the file linked-list of the card.
595 * This linked-list is used to keep tracking the connection state,
596 * and to avoid the release of busy resources by hotplug.
597 *
598 * Returns zero or a negative error code.
599 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100600int snd_card_file_add(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct snd_monitor_file *mfile;
603
604 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
605 if (mfile == NULL)
606 return -ENOMEM;
607 mfile->file = file;
608 mfile->next = NULL;
609 spin_lock(&card->files_lock);
610 if (card->shutdown) {
611 spin_unlock(&card->files_lock);
612 kfree(mfile);
613 return -ENODEV;
614 }
615 mfile->next = card->files;
616 card->files = mfile;
617 spin_unlock(&card->files_lock);
618 return 0;
619}
620
621/**
622 * snd_card_file_remove - remove the file from the file list
623 * @card: soundcard structure
624 * @file: file pointer
625 *
626 * This function removes the file formerly added to the card via
627 * snd_card_file_add() function.
628 * If all files are removed and the release of the card is
629 * scheduled, it will wake up the the thread to call snd_card_free()
630 * (see snd_card_free_in_thread() function).
631 *
632 * Returns zero or a negative error code.
633 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100634int snd_card_file_remove(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct snd_monitor_file *mfile, *pfile = NULL;
637
638 spin_lock(&card->files_lock);
639 mfile = card->files;
640 while (mfile) {
641 if (mfile->file == file) {
642 if (pfile)
643 pfile->next = mfile->next;
644 else
645 card->files = mfile->next;
646 break;
647 }
648 pfile = mfile;
649 mfile = mfile->next;
650 }
651 spin_unlock(&card->files_lock);
652 if (card->files == NULL)
653 wake_up(&card->shutdown_sleep);
654 if (!mfile) {
655 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
656 return -ENOENT;
657 }
658 kfree(mfile);
659 return 0;
660}
661
662#ifdef CONFIG_PM
663/**
664 * snd_power_wait - wait until the power-state is changed.
665 * @card: soundcard structure
666 * @power_state: expected power state
667 * @file: file structure for the O_NONBLOCK check (optional)
668 *
669 * Waits until the power-state is changed.
670 *
671 * Note: the power lock must be active before call.
672 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100673int snd_power_wait(struct snd_card *card, unsigned int power_state, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 wait_queue_t wait;
676 int result = 0;
677
678 /* fastpath */
679 if (snd_power_get_state(card) == power_state)
680 return 0;
681 init_waitqueue_entry(&wait, current);
682 add_wait_queue(&card->power_sleep, &wait);
683 while (1) {
684 if (card->shutdown) {
685 result = -ENODEV;
686 break;
687 }
688 if (snd_power_get_state(card) == power_state)
689 break;
690#if 0 /* block all devices */
691 if (file && (file->f_flags & O_NONBLOCK)) {
692 result = -EAGAIN;
693 break;
694 }
695#endif
696 set_current_state(TASK_UNINTERRUPTIBLE);
697 snd_power_unlock(card);
698 schedule_timeout(30 * HZ);
699 snd_power_lock(card);
700 }
701 remove_wait_queue(&card->power_sleep, &wait);
702 return result;
703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705#endif /* CONFIG_PM */