[PATCH] ALSA: Fix Oops of suspend/resume with generic drivers
[linux-2.6/linux-loongson.git] / sound / core / init.c
blobc72a79115cca2e64ecc36fc9de1fae40bc24f2f3
1 /*
2 * Initialization routines
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/file.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/ctype.h>
29 #include <linux/pci.h>
30 #include <linux/pm.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
35 struct snd_shutdown_f_ops {
36 struct file_operations f_ops;
37 struct snd_shutdown_f_ops *next;
40 unsigned int snd_cards_lock = 0; /* locked for registering/using */
41 snd_card_t *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL};
42 DEFINE_RWLOCK(snd_card_rwlock);
44 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
45 int (*snd_mixer_oss_notify_callback)(snd_card_t *card, int free_flag);
46 #endif
48 static void snd_card_id_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
50 snd_iprintf(buffer, "%s\n", entry->card->id);
53 static void snd_card_free_thread(void * __card);
55 /**
56 * snd_card_new - create and initialize a soundcard structure
57 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
58 * @xid: card identification (ASCII string)
59 * @module: top level module for locking
60 * @extra_size: allocate this extra size after the main soundcard structure
62 * Creates and initializes a soundcard structure.
64 * Returns kmallocated snd_card_t structure. Creates the ALSA control interface
65 * (which is blocked until snd_card_register function is called).
67 snd_card_t *snd_card_new(int idx, const char *xid,
68 struct module *module, int extra_size)
70 snd_card_t *card;
71 int err;
73 if (extra_size < 0)
74 extra_size = 0;
75 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
76 if (card == NULL)
77 return NULL;
78 if (xid) {
79 if (!snd_info_check_reserved_words(xid))
80 goto __error;
81 strlcpy(card->id, xid, sizeof(card->id));
83 err = 0;
84 write_lock(&snd_card_rwlock);
85 if (idx < 0) {
86 int idx2;
87 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
88 if (~snd_cards_lock & idx & 1<<idx2) {
89 idx = idx2;
90 if (idx >= snd_ecards_limit)
91 snd_ecards_limit = idx + 1;
92 break;
94 } else if (idx < snd_ecards_limit) {
95 if (snd_cards_lock & (1 << idx))
96 err = -ENODEV; /* invalid */
97 } else if (idx < SNDRV_CARDS)
98 snd_ecards_limit = idx + 1; /* increase the limit */
99 else
100 err = -ENODEV;
101 if (idx < 0 || err < 0) {
102 write_unlock(&snd_card_rwlock);
103 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
104 goto __error;
106 snd_cards_lock |= 1 << idx; /* lock it */
107 write_unlock(&snd_card_rwlock);
108 card->number = idx;
109 card->module = module;
110 INIT_LIST_HEAD(&card->devices);
111 init_rwsem(&card->controls_rwsem);
112 rwlock_init(&card->ctl_files_rwlock);
113 INIT_LIST_HEAD(&card->controls);
114 INIT_LIST_HEAD(&card->ctl_files);
115 spin_lock_init(&card->files_lock);
116 init_waitqueue_head(&card->shutdown_sleep);
117 INIT_WORK(&card->free_workq, snd_card_free_thread, card);
118 #ifdef CONFIG_PM
119 init_MUTEX(&card->power_lock);
120 init_waitqueue_head(&card->power_sleep);
121 #endif
122 /* the control interface cannot be accessed from the user space until */
123 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
124 if ((err = snd_ctl_create(card)) < 0) {
125 snd_printd("unable to register control minors\n");
126 goto __error;
128 if ((err = snd_info_card_create(card)) < 0) {
129 snd_printd("unable to create card info\n");
130 goto __error_ctl;
132 if (extra_size > 0)
133 card->private_data = (char *)card + sizeof(snd_card_t);
134 return card;
136 __error_ctl:
137 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
138 __error:
139 kfree(card);
140 return NULL;
143 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
145 return POLLERR | POLLNVAL;
149 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
150 * @card: soundcard structure
152 * Disconnects all APIs from the file-operations (user space).
154 * Returns zero, otherwise a negative error code.
156 * Note: The current implementation replaces all active file->f_op with special
157 * dummy file operations (they do nothing except release).
159 int snd_card_disconnect(snd_card_t * card)
161 struct snd_monitor_file *mfile;
162 struct file *file;
163 struct snd_shutdown_f_ops *s_f_ops;
164 struct file_operations *f_ops, *old_f_ops;
165 int err;
167 spin_lock(&card->files_lock);
168 if (card->shutdown) {
169 spin_unlock(&card->files_lock);
170 return 0;
172 card->shutdown = 1;
173 spin_unlock(&card->files_lock);
175 /* phase 1: disable fops (user space) operations for ALSA API */
176 write_lock(&snd_card_rwlock);
177 snd_cards[card->number] = NULL;
178 write_unlock(&snd_card_rwlock);
180 /* phase 2: replace file->f_op with special dummy operations */
182 spin_lock(&card->files_lock);
183 mfile = card->files;
184 while (mfile) {
185 file = mfile->file;
187 /* it's critical part, use endless loop */
188 /* we have no room to fail */
189 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
190 if (s_f_ops == NULL)
191 panic("Atomic allocation failed for snd_shutdown_f_ops!");
193 f_ops = &s_f_ops->f_ops;
195 memset(f_ops, 0, sizeof(*f_ops));
196 f_ops->owner = file->f_op->owner;
197 f_ops->release = file->f_op->release;
198 f_ops->poll = snd_disconnect_poll;
200 s_f_ops->next = card->s_f_ops;
201 card->s_f_ops = s_f_ops;
203 f_ops = fops_get(f_ops);
205 old_f_ops = file->f_op;
206 file->f_op = f_ops; /* must be atomic */
207 fops_put(old_f_ops);
209 mfile = mfile->next;
211 spin_unlock(&card->files_lock);
213 /* phase 3: notify all connected devices about disconnection */
214 /* at this point, they cannot respond to any calls except release() */
216 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
217 if (snd_mixer_oss_notify_callback)
218 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
219 #endif
221 /* notify all devices that we are disconnected */
222 err = snd_device_disconnect_all(card);
223 if (err < 0)
224 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
226 return 0;
229 #ifdef CONFIG_SND_GENERIC_DRIVER
230 static void snd_generic_device_unregister(snd_card_t *card);
231 #else
232 #define snd_generic_device_unregister(x) /*NOP*/
233 #endif
236 * snd_card_free - frees given soundcard structure
237 * @card: soundcard structure
239 * This function releases the soundcard structure and the all assigned
240 * devices automatically. That is, you don't have to release the devices
241 * by yourself.
243 * Returns zero. Frees all associated devices and frees the control
244 * interface associated to given soundcard.
246 int snd_card_free(snd_card_t * card)
248 struct snd_shutdown_f_ops *s_f_ops;
250 if (card == NULL)
251 return -EINVAL;
252 write_lock(&snd_card_rwlock);
253 snd_cards[card->number] = NULL;
254 write_unlock(&snd_card_rwlock);
256 #ifdef CONFIG_PM
257 wake_up(&card->power_sleep);
258 #endif
259 /* wait, until all devices are ready for the free operation */
260 wait_event(card->shutdown_sleep, card->files == NULL);
262 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
263 if (snd_mixer_oss_notify_callback)
264 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
265 #endif
266 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
267 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
268 /* Fatal, but this situation should never occur */
270 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
271 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
272 /* Fatal, but this situation should never occur */
274 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
275 snd_printk(KERN_ERR "unable to free all devices (post)\n");
276 /* Fatal, but this situation should never occur */
278 if (card->private_free)
279 card->private_free(card);
280 if (card->proc_id)
281 snd_info_unregister(card->proc_id);
282 if (snd_info_card_free(card) < 0) {
283 snd_printk(KERN_WARNING "unable to free card info\n");
284 /* Not fatal error */
286 snd_generic_device_unregister(card);
287 while (card->s_f_ops) {
288 s_f_ops = card->s_f_ops;
289 card->s_f_ops = s_f_ops->next;
290 kfree(s_f_ops);
292 write_lock(&snd_card_rwlock);
293 snd_cards_lock &= ~(1 << card->number);
294 write_unlock(&snd_card_rwlock);
295 kfree(card);
296 return 0;
299 static void snd_card_free_thread(void * __card)
301 snd_card_t *card = __card;
302 struct module * module = card->module;
304 if (!try_module_get(module)) {
305 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
306 module = NULL;
309 snd_card_free(card);
311 module_put(module);
315 * snd_card_free_in_thread - call snd_card_free() in thread
316 * @card: soundcard structure
318 * This function schedules the call of snd_card_free() function in a
319 * work queue. When all devices are released (non-busy), the work
320 * is woken up and calls snd_card_free().
322 * When a card can be disconnected at any time by hotplug service,
323 * this function should be used in disconnect (or detach) callback
324 * instead of calling snd_card_free() directly.
326 * Returns - zero otherwise a negative error code if the start of thread failed.
328 int snd_card_free_in_thread(snd_card_t * card)
330 if (card->files == NULL) {
331 snd_card_free(card);
332 return 0;
335 if (schedule_work(&card->free_workq))
336 return 0;
338 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
339 /* try to free the structure immediately */
340 snd_card_free(card);
341 return -EFAULT;
344 static void choose_default_id(snd_card_t * card)
346 int i, len, idx_flag = 0, loops = 8;
347 char *id, *spos;
349 id = spos = card->shortname;
350 while (*id != '\0') {
351 if (*id == ' ')
352 spos = id + 1;
353 id++;
355 id = card->id;
356 while (*spos != '\0' && !isalnum(*spos))
357 spos++;
358 if (isdigit(*spos))
359 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
360 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
361 if (isalnum(*spos))
362 *id++ = *spos;
363 spos++;
365 *id = '\0';
367 id = card->id;
369 if (*id == '\0')
370 strcpy(id, "default");
372 while (1) {
373 if (loops-- == 0) {
374 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
375 strcpy(card->id, card->proc_root->name);
376 return;
378 if (!snd_info_check_reserved_words(id))
379 goto __change;
380 for (i = 0; i < snd_ecards_limit; i++) {
381 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
382 goto __change;
384 break;
386 __change:
387 len = strlen(id);
388 if (idx_flag)
389 id[len-1]++;
390 else if ((size_t)len <= sizeof(card->id) - 3) {
391 strcat(id, "_1");
392 idx_flag++;
393 } else {
394 spos = id + len - 2;
395 if ((size_t)len <= sizeof(card->id) - 2)
396 spos++;
397 *spos++ = '_';
398 *spos++ = '1';
399 *spos++ = '\0';
400 idx_flag++;
406 * snd_card_register - register the soundcard
407 * @card: soundcard structure
409 * This function registers all the devices assigned to the soundcard.
410 * Until calling this, the ALSA control interface is blocked from the
411 * external accesses. Thus, you should call this function at the end
412 * of the initialization of the card.
414 * Returns zero otherwise a negative error code if the registrain failed.
416 int snd_card_register(snd_card_t * card)
418 int err;
419 snd_info_entry_t *entry;
421 snd_runtime_check(card != NULL, return -EINVAL);
422 if ((err = snd_device_register_all(card)) < 0)
423 return err;
424 write_lock(&snd_card_rwlock);
425 if (snd_cards[card->number]) {
426 /* already registered */
427 write_unlock(&snd_card_rwlock);
428 return 0;
430 if (card->id[0] == '\0')
431 choose_default_id(card);
432 snd_cards[card->number] = card;
433 write_unlock(&snd_card_rwlock);
434 if ((err = snd_info_card_register(card)) < 0) {
435 snd_printd("unable to create card info\n");
436 goto __skip_info;
438 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
439 snd_printd("unable to create card entry\n");
440 goto __skip_info;
442 entry->c.text.read_size = PAGE_SIZE;
443 entry->c.text.read = snd_card_id_read;
444 if (snd_info_register(entry) < 0) {
445 snd_info_free_entry(entry);
446 entry = NULL;
448 card->proc_id = entry;
449 __skip_info:
450 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
451 if (snd_mixer_oss_notify_callback)
452 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
453 #endif
454 return 0;
457 static snd_info_entry_t *snd_card_info_entry = NULL;
459 static void snd_card_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
461 int idx, count;
462 snd_card_t *card;
464 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
465 read_lock(&snd_card_rwlock);
466 if ((card = snd_cards[idx]) != NULL) {
467 count++;
468 snd_iprintf(buffer, "%i [%-15s]: %s - %s\n",
469 idx,
470 card->id,
471 card->driver,
472 card->shortname);
473 snd_iprintf(buffer, " %s\n",
474 card->longname);
476 read_unlock(&snd_card_rwlock);
478 if (!count)
479 snd_iprintf(buffer, "--- no soundcards ---\n");
482 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
484 void snd_card_info_read_oss(snd_info_buffer_t * buffer)
486 int idx, count;
487 snd_card_t *card;
489 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
490 read_lock(&snd_card_rwlock);
491 if ((card = snd_cards[idx]) != NULL) {
492 count++;
493 snd_iprintf(buffer, "%s\n", card->longname);
495 read_unlock(&snd_card_rwlock);
497 if (!count) {
498 snd_iprintf(buffer, "--- no soundcards ---\n");
502 #endif
504 #ifdef MODULE
505 static snd_info_entry_t *snd_card_module_info_entry;
506 static void snd_card_module_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
508 int idx;
509 snd_card_t *card;
511 for (idx = 0; idx < SNDRV_CARDS; idx++) {
512 read_lock(&snd_card_rwlock);
513 if ((card = snd_cards[idx]) != NULL)
514 snd_iprintf(buffer, "%i %s\n", idx, card->module->name);
515 read_unlock(&snd_card_rwlock);
518 #endif
520 int __init snd_card_info_init(void)
522 snd_info_entry_t *entry;
524 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
525 snd_runtime_check(entry != NULL, return -ENOMEM);
526 entry->c.text.read_size = PAGE_SIZE;
527 entry->c.text.read = snd_card_info_read;
528 if (snd_info_register(entry) < 0) {
529 snd_info_free_entry(entry);
530 return -ENOMEM;
532 snd_card_info_entry = entry;
534 #ifdef MODULE
535 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
536 if (entry) {
537 entry->c.text.read_size = PAGE_SIZE;
538 entry->c.text.read = snd_card_module_info_read;
539 if (snd_info_register(entry) < 0)
540 snd_info_free_entry(entry);
541 else
542 snd_card_module_info_entry = entry;
544 #endif
546 return 0;
549 int __exit snd_card_info_done(void)
551 if (snd_card_info_entry)
552 snd_info_unregister(snd_card_info_entry);
553 #ifdef MODULE
554 if (snd_card_module_info_entry)
555 snd_info_unregister(snd_card_module_info_entry);
556 #endif
557 return 0;
561 * snd_component_add - add a component string
562 * @card: soundcard structure
563 * @component: the component id string
565 * This function adds the component id string to the supported list.
566 * The component can be referred from the alsa-lib.
568 * Returns zero otherwise a negative error code.
571 int snd_component_add(snd_card_t *card, const char *component)
573 char *ptr;
574 int len = strlen(component);
576 ptr = strstr(card->components, component);
577 if (ptr != NULL) {
578 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
579 return 1;
581 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
582 snd_BUG();
583 return -ENOMEM;
585 if (card->components[0] != '\0')
586 strcat(card->components, " ");
587 strcat(card->components, component);
588 return 0;
592 * snd_card_file_add - add the file to the file list of the card
593 * @card: soundcard structure
594 * @file: file pointer
596 * This function adds the file to the file linked-list of the card.
597 * This linked-list is used to keep tracking the connection state,
598 * and to avoid the release of busy resources by hotplug.
600 * Returns zero or a negative error code.
602 int snd_card_file_add(snd_card_t *card, struct file *file)
604 struct snd_monitor_file *mfile;
606 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
607 if (mfile == NULL)
608 return -ENOMEM;
609 mfile->file = file;
610 mfile->next = NULL;
611 spin_lock(&card->files_lock);
612 if (card->shutdown) {
613 spin_unlock(&card->files_lock);
614 kfree(mfile);
615 return -ENODEV;
617 mfile->next = card->files;
618 card->files = mfile;
619 spin_unlock(&card->files_lock);
620 return 0;
624 * snd_card_file_remove - remove the file from the file list
625 * @card: soundcard structure
626 * @file: file pointer
628 * This function removes the file formerly added to the card via
629 * snd_card_file_add() function.
630 * If all files are removed and the release of the card is
631 * scheduled, it will wake up the the thread to call snd_card_free()
632 * (see snd_card_free_in_thread() function).
634 * Returns zero or a negative error code.
636 int snd_card_file_remove(snd_card_t *card, struct file *file)
638 struct snd_monitor_file *mfile, *pfile = NULL;
640 spin_lock(&card->files_lock);
641 mfile = card->files;
642 while (mfile) {
643 if (mfile->file == file) {
644 if (pfile)
645 pfile->next = mfile->next;
646 else
647 card->files = mfile->next;
648 break;
650 pfile = mfile;
651 mfile = mfile->next;
653 spin_unlock(&card->files_lock);
654 if (card->files == NULL)
655 wake_up(&card->shutdown_sleep);
656 if (!mfile) {
657 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
658 return -ENOENT;
660 kfree(mfile);
661 return 0;
664 #ifdef CONFIG_SND_GENERIC_DRIVER
666 * generic device without a proper bus using platform_device
667 * (e.g. ISA)
669 struct snd_generic_device {
670 struct platform_device pdev;
671 snd_card_t *card;
674 #define get_snd_generic_card(dev) container_of(to_platform_device(dev), struct snd_generic_device, pdev)->card
676 #define SND_GENERIC_NAME "snd_generic"
678 #ifdef CONFIG_PM
679 static int snd_generic_suspend(struct device *dev, pm_message_t state, u32 level);
680 static int snd_generic_resume(struct device *dev, u32 level);
681 #endif
683 /* initialized in sound.c */
684 struct device_driver snd_generic_driver = {
685 .name = SND_GENERIC_NAME,
686 .bus = &platform_bus_type,
687 #ifdef CONFIG_PM
688 .suspend = snd_generic_suspend,
689 .resume = snd_generic_resume,
690 #endif
693 void snd_generic_device_release(struct device *dev)
697 static int snd_generic_device_register(snd_card_t *card)
699 struct snd_generic_device *dev;
700 int err;
702 if (card->generic_dev)
703 return 0; /* already registered */
705 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
706 if (! dev) {
707 snd_printk(KERN_ERR "can't allocate generic_device\n");
708 return -ENOMEM;
711 dev->pdev.name = SND_GENERIC_NAME;
712 dev->pdev.id = card->number;
713 dev->pdev.dev.release = snd_generic_device_release;
714 dev->card = card;
715 if ((err = platform_device_register(&dev->pdev)) < 0) {
716 kfree(dev);
717 return err;
719 card->generic_dev = dev;
720 return 0;
723 static void snd_generic_device_unregister(snd_card_t *card)
725 struct snd_generic_device *dev = card->generic_dev;
726 if (dev) {
727 platform_device_unregister(&dev->pdev);
728 kfree(dev);
729 card->generic_dev = NULL;
734 * snd_card_set_generic_dev - assign the generic device to the card
735 * @card: soundcard structure
737 * Assigns a generic device to the card. This function is provided as the
738 * last resort, for devices without any proper bus. Thus this won't override
739 * the device already assigned to the card.
741 * Returns zero if successful, or a negative error code.
743 int snd_card_set_generic_dev(snd_card_t *card)
745 int err;
746 if ((err = snd_generic_device_register(card)) < 0)
747 return err;
748 if (! card->dev)
749 snd_card_set_dev(card, &card->generic_dev->pdev.dev);
750 return 0;
752 #endif /* CONFIG_SND_GENERIC_DRIVER */
754 #ifdef CONFIG_PM
756 * snd_power_wait - wait until the power-state is changed.
757 * @card: soundcard structure
758 * @power_state: expected power state
759 * @file: file structure for the O_NONBLOCK check (optional)
761 * Waits until the power-state is changed.
763 * Note: the power lock must be active before call.
765 int snd_power_wait(snd_card_t *card, unsigned int power_state, struct file *file)
767 wait_queue_t wait;
768 int result = 0;
770 /* fastpath */
771 if (snd_power_get_state(card) == power_state)
772 return 0;
773 init_waitqueue_entry(&wait, current);
774 add_wait_queue(&card->power_sleep, &wait);
775 while (1) {
776 if (card->shutdown) {
777 result = -ENODEV;
778 break;
780 if (snd_power_get_state(card) == power_state)
781 break;
782 #if 0 /* block all devices */
783 if (file && (file->f_flags & O_NONBLOCK)) {
784 result = -EAGAIN;
785 break;
787 #endif
788 set_current_state(TASK_UNINTERRUPTIBLE);
789 snd_power_unlock(card);
790 schedule_timeout(30 * HZ);
791 snd_power_lock(card);
793 remove_wait_queue(&card->power_sleep, &wait);
794 return result;
798 * snd_card_set_pm_callback - set the PCI power-management callbacks
799 * @card: soundcard structure
800 * @suspend: suspend callback function
801 * @resume: resume callback function
802 * @private_data: private data to pass to the callback functions
804 * Sets the power-management callback functions of the card.
805 * These callbacks are called from ALSA's common PCI suspend/resume
806 * handler and from the control API.
808 int snd_card_set_pm_callback(snd_card_t *card,
809 int (*suspend)(snd_card_t *, pm_message_t),
810 int (*resume)(snd_card_t *),
811 void *private_data)
813 card->pm_suspend = suspend;
814 card->pm_resume = resume;
815 card->pm_private_data = private_data;
816 return 0;
819 #ifdef CONFIG_SND_GENERIC_DRIVER
820 /* suspend/resume callbacks for snd_generic platform device */
821 static int snd_generic_suspend(struct device *dev, pm_message_t state, u32 level)
823 snd_card_t *card;
825 if (level != SUSPEND_DISABLE)
826 return 0;
828 card = get_snd_generic_card(dev);
829 if (card->power_state == SNDRV_CTL_POWER_D3hot)
830 return 0;
831 if (card->pm_suspend)
832 card->pm_suspend(card, PMSG_SUSPEND);
833 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
834 return 0;
837 static int snd_generic_resume(struct device *dev, u32 level)
839 snd_card_t *card;
841 if (level != RESUME_ENABLE)
842 return 0;
844 card = get_snd_generic_card(dev);
845 if (card->power_state == SNDRV_CTL_POWER_D0)
846 return 0;
847 if (card->pm_suspend)
848 card->pm_resume(card);
849 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
850 return 0;
854 * snd_card_set_generic_pm_callback - set the generic power-management callbacks
855 * @card: soundcard structure
856 * @suspend: suspend callback function
857 * @resume: resume callback function
858 * @private_data: private data to pass to the callback functions
860 * Registers the power-management and sets the lowlevel callbacks for
861 * the given card. These callbacks are called from the ALSA's common
862 * PM handler and from the control API.
864 int snd_card_set_generic_pm_callback(snd_card_t *card,
865 int (*suspend)(snd_card_t *, pm_message_t),
866 int (*resume)(snd_card_t *),
867 void *private_data)
869 int err;
870 if ((err = snd_generic_device_register(card)) < 0)
871 return err;
872 return snd_card_set_pm_callback(card, suspend, resume, private_data);
874 #endif /* CONFIG_SND_GENERIC_DRIVER */
876 #ifdef CONFIG_PCI
877 int snd_card_pci_suspend(struct pci_dev *dev, pm_message_t state)
879 snd_card_t *card = pci_get_drvdata(dev);
880 int err;
881 if (! card || ! card->pm_suspend)
882 return 0;
883 if (card->power_state == SNDRV_CTL_POWER_D3hot)
884 return 0;
885 err = card->pm_suspend(card, PMSG_SUSPEND);
886 pci_save_state(dev);
887 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
888 return err;
891 int snd_card_pci_resume(struct pci_dev *dev)
893 snd_card_t *card = pci_get_drvdata(dev);
894 if (! card || ! card->pm_resume)
895 return 0;
896 if (card->power_state == SNDRV_CTL_POWER_D0)
897 return 0;
898 /* restore the PCI config space */
899 pci_restore_state(dev);
900 card->pm_resume(card);
901 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
902 return 0;
904 #endif
906 #endif /* CONFIG_PM */