vserver 1.9.5.x5
[linux-2.6.git] / sound / core / init.c
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>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34
35 struct snd_shutdown_f_ops {
36         struct file_operations f_ops;
37         struct snd_shutdown_f_ops *next;
38 };
39
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 rwlock_t snd_card_rwlock = RW_LOCK_UNLOCKED;
43
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
47
48 static void snd_card_id_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
49 {
50         snd_iprintf(buffer, "%s\n", entry->card->id);
51 }
52
53 static void snd_card_free_thread(void * __card);
54
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
61  *
62  *  Creates and initializes a soundcard structure.
63  *
64  *  Returns kmallocated snd_card_t structure. Creates the ALSA control interface
65  *  (which is blocked until snd_card_register function is called).
66  */
67 snd_card_t *snd_card_new(int idx, const char *xid,
68                          struct module *module, int extra_size)
69 {
70         snd_card_t *card;
71         int err;
72
73         if (extra_size < 0)
74                 extra_size = 0;
75         card = kcalloc(1, 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));
82         }
83         err = 0;
84         write_lock(&snd_card_rwlock);
85         if (idx < 0) {
86                 int idx2;
87                 for (idx2 = 0; idx2 < snd_ecards_limit; idx2++)
88                         if (!(snd_cards_lock & (1 << idx2))) {
89                                 idx = idx2;
90                                 break;
91                         }
92                 if (idx < 0 && snd_ecards_limit < SNDRV_CARDS)
93                         /* for dynamically additional devices like hotplug:
94                          * increment the limit if still free slot exists.
95                          */
96                         idx = snd_ecards_limit++;
97         } else if (idx < snd_ecards_limit) {
98                 if (snd_cards_lock & (1 << idx))
99                         err = -ENODEV;  /* invalid */
100         } else if (idx < SNDRV_CARDS)
101                 snd_ecards_limit = idx + 1; /* increase the limit */
102         else
103                 err = -ENODEV;
104         if (idx < 0 || err < 0) {
105                 write_unlock(&snd_card_rwlock);
106                 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
107                 goto __error;
108         }
109         snd_cards_lock |= 1 << idx;             /* lock it */
110         write_unlock(&snd_card_rwlock);
111         card->number = idx;
112         card->module = module;
113         INIT_LIST_HEAD(&card->devices);
114         init_rwsem(&card->controls_rwsem);
115         rwlock_init(&card->ctl_files_rwlock);
116         INIT_LIST_HEAD(&card->controls);
117         INIT_LIST_HEAD(&card->ctl_files);
118         spin_lock_init(&card->files_lock);
119         init_waitqueue_head(&card->shutdown_sleep);
120         INIT_WORK(&card->free_workq, snd_card_free_thread, card);
121 #ifdef CONFIG_PM
122         init_MUTEX(&card->power_lock);
123         init_waitqueue_head(&card->power_sleep);
124 #endif
125         /* the control interface cannot be accessed from the user space until */
126         /* snd_cards_bitmask and snd_cards are set with snd_card_register */
127         if ((err = snd_ctl_create(card)) < 0) {
128                 snd_printd("unable to register control minors\n");
129                 goto __error;
130         }
131         if ((err = snd_info_card_create(card)) < 0) {
132                 snd_printd("unable to create card info\n");
133                 goto __error_ctl;
134         }
135         if (extra_size > 0)
136                 card->private_data = (char *)card + sizeof(snd_card_t);
137         return card;
138
139       __error_ctl:
140         snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
141       __error:
142         kfree(card);
143         return NULL;
144 }
145
146 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
147 {
148         return POLLERR | POLLNVAL;
149 }
150
151 /**
152  *  snd_card_disconnect - disconnect all APIs from the file-operations (user space)
153  *  @card: soundcard structure
154  *
155  *  Disconnects all APIs from the file-operations (user space).
156  *
157  *  Returns zero, otherwise a negative error code.
158  *
159  *  Note: The current implementation replaces all active file->f_op with special
160  *        dummy file operations (they do nothing except release).
161  */
162 int snd_card_disconnect(snd_card_t * card)
163 {
164         struct snd_monitor_file *mfile;
165         struct file *file;
166         struct snd_shutdown_f_ops *s_f_ops;
167         struct file_operations *f_ops, *old_f_ops;
168         int err;
169
170         spin_lock(&card->files_lock);
171         if (card->shutdown) {
172                 spin_unlock(&card->files_lock);
173                 return 0;
174         }
175         card->shutdown = 1;
176         spin_unlock(&card->files_lock);
177
178         /* phase 1: disable fops (user space) operations for ALSA API */
179         write_lock(&snd_card_rwlock);
180         snd_cards[card->number] = NULL;
181         write_unlock(&snd_card_rwlock);
182         
183         /* phase 2: replace file->f_op with special dummy operations */
184         
185         spin_lock(&card->files_lock);
186         mfile = card->files;
187         while (mfile) {
188                 file = mfile->file;
189
190                 /* it's critical part, use endless loop */
191                 /* we have no room to fail */
192                 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
193                 if (s_f_ops == NULL)
194                         panic("Atomic allocation failed for snd_shutdown_f_ops!");
195
196                 f_ops = &s_f_ops->f_ops;
197
198                 memset(f_ops, 0, sizeof(*f_ops));
199                 f_ops->owner = file->f_op->owner;
200                 f_ops->release = file->f_op->release;
201                 f_ops->poll = snd_disconnect_poll;
202
203                 s_f_ops->next = card->s_f_ops;
204                 card->s_f_ops = s_f_ops;
205                 
206                 f_ops = fops_get(f_ops);
207
208                 old_f_ops = file->f_op;
209                 file->f_op = f_ops;     /* must be atomic */
210                 fops_put(old_f_ops);
211                 
212                 mfile = mfile->next;
213         }
214         spin_unlock(&card->files_lock); 
215
216         /* phase 3: notify all connected devices about disconnection */
217         /* at this point, they cannot respond to any calls except release() */
218
219 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
220         if (snd_mixer_oss_notify_callback)
221                 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
222 #endif
223
224         /* notify all devices that we are disconnected */
225         err = snd_device_disconnect_all(card);
226         if (err < 0)
227                 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
228
229         return 0;       
230 }
231
232 /**
233  *  snd_card_free - frees given soundcard structure
234  *  @card: soundcard structure
235  *
236  *  This function releases the soundcard structure and the all assigned
237  *  devices automatically.  That is, you don't have to release the devices
238  *  by yourself.
239  *
240  *  Returns zero. Frees all associated devices and frees the control
241  *  interface associated to given soundcard.
242  */
243 int snd_card_free(snd_card_t * card)
244 {
245         struct snd_shutdown_f_ops *s_f_ops;
246
247         if (card == NULL)
248                 return -EINVAL;
249         write_lock(&snd_card_rwlock);
250         snd_cards[card->number] = NULL;
251         write_unlock(&snd_card_rwlock);
252
253 #ifdef CONFIG_PM
254         wake_up(&card->power_sleep);
255 #ifdef CONFIG_ISA
256         if (card->pm_dev) {
257                 pm_unregister(card->pm_dev);
258                 card->pm_dev = NULL;
259         }
260 #endif
261 #endif
262
263         /* wait, until all devices are ready for the free operation */
264         wait_event(card->shutdown_sleep, card->files == NULL);
265
266 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
267         if (snd_mixer_oss_notify_callback)
268                 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
269 #endif
270         if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
271                 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
272                 /* Fatal, but this situation should never occur */
273         }
274         if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
275                 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
276                 /* Fatal, but this situation should never occur */
277         }
278         if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
279                 snd_printk(KERN_ERR "unable to free all devices (post)\n");
280                 /* Fatal, but this situation should never occur */
281         }
282         if (card->private_free)
283                 card->private_free(card);
284         if (card->proc_id)
285                 snd_info_unregister(card->proc_id);
286         if (snd_info_card_free(card) < 0) {
287                 snd_printk(KERN_WARNING "unable to free card info\n");
288                 /* Not fatal error */
289         }
290         while (card->s_f_ops) {
291                 s_f_ops = card->s_f_ops;
292                 card->s_f_ops = s_f_ops->next;
293                 kfree(s_f_ops);
294         }
295         write_lock(&snd_card_rwlock);
296         snd_cards_lock &= ~(1 << card->number);
297         write_unlock(&snd_card_rwlock);
298         kfree(card);
299         return 0;
300 }
301
302 static void snd_card_free_thread(void * __card)
303 {
304         snd_card_t *card = __card;
305         struct module * module = card->module;
306
307         if (!try_module_get(module)) {
308                 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
309                 module = NULL;
310         }
311
312         snd_card_free(card);
313
314         module_put(module);
315 }
316
317 /**
318  *  snd_card_free_in_thread - call snd_card_free() in thread
319  *  @card: soundcard structure
320  *
321  *  This function schedules the call of snd_card_free() function in a
322  *  work queue.  When all devices are released (non-busy), the work
323  *  is woken up and calls snd_card_free().
324  *
325  *  When a card can be disconnected at any time by hotplug service,
326  *  this function should be used in disconnect (or detach) callback
327  *  instead of calling snd_card_free() directly.
328  *  
329  *  Returns - zero otherwise a negative error code if the start of thread failed.
330  */
331 int snd_card_free_in_thread(snd_card_t * card)
332 {
333         if (card->files == NULL) {
334                 snd_card_free(card);
335                 return 0;
336         }
337
338         if (schedule_work(&card->free_workq))
339                 return 0;
340
341         snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
342         /* try to free the structure immediately */
343         snd_card_free(card);
344         return -EFAULT;
345 }
346
347 static void choose_default_id(snd_card_t * card)
348 {
349         int i, len, idx_flag = 0, loops = 8;
350         char *id, *spos;
351         
352         id = spos = card->shortname;    
353         while (*id != '\0') {
354                 if (*id == ' ')
355                         spos = id + 1;
356                 id++;
357         }
358         id = card->id;
359         while (*spos != '\0' && !isalnum(*spos))
360                 spos++;
361         if (isdigit(*spos))
362                 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
363         while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
364                 if (isalnum(*spos))
365                         *id++ = *spos;
366                 spos++;
367         }
368         *id = '\0';
369
370         id = card->id;
371         
372         if (*id == '\0')
373                 strcpy(id, "default");
374
375         while (1) {
376                 if (loops-- == 0) {
377                         snd_printk(KERN_ERR "unable to choose default card id (%s)", id);
378                         strcpy(card->id, card->proc_root->name);
379                         return;
380                 }
381                 if (!snd_info_check_reserved_words(id))
382                         goto __change;
383                 for (i = 0; i < snd_ecards_limit; i++) {
384                         if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
385                                 goto __change;
386                 }
387                 break;
388
389               __change:
390                 len = strlen(id);
391                 if (idx_flag)
392                         id[len-1]++;
393                 else if ((size_t)len <= sizeof(card->id) - 3) {
394                         strcat(id, "_1");
395                         idx_flag++;
396                 } else {
397                         spos = id + len - 2;
398                         if ((size_t)len <= sizeof(card->id) - 2)
399                                 spos++;
400                         *spos++ = '_';
401                         *spos++ = '1';
402                         *spos++ = '\0';
403                         idx_flag++;
404                 }
405         }
406 }
407
408 /**
409  *  snd_card_register - register the soundcard
410  *  @card: soundcard structure
411  *
412  *  This function registers all the devices assigned to the soundcard.
413  *  Until calling this, the ALSA control interface is blocked from the
414  *  external accesses.  Thus, you should call this function at the end
415  *  of the initialization of the card.
416  *
417  *  Returns zero otherwise a negative error code if the registrain failed.
418  */
419 int snd_card_register(snd_card_t * card)
420 {
421         int err;
422         snd_info_entry_t *entry;
423
424         snd_runtime_check(card != NULL, return -EINVAL);
425         if ((err = snd_device_register_all(card)) < 0)
426                 return err;
427         write_lock(&snd_card_rwlock);
428         if (snd_cards[card->number]) {
429                 /* already registered */
430                 write_unlock(&snd_card_rwlock);
431                 return 0;
432         }
433         if (card->id[0] == '\0')
434                 choose_default_id(card);
435         snd_cards[card->number] = card;
436         write_unlock(&snd_card_rwlock);
437         if ((err = snd_info_card_register(card)) < 0) {
438                 snd_printd("unable to create card info\n");
439                 goto __skip_info;
440         }
441         if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
442                 snd_printd("unable to create card entry\n");
443                 goto __skip_info;
444         }
445         entry->c.text.read_size = PAGE_SIZE;
446         entry->c.text.read = snd_card_id_read;
447         if (snd_info_register(entry) < 0) {
448                 snd_info_free_entry(entry);
449                 entry = NULL;
450         }
451         card->proc_id = entry;
452       __skip_info:
453 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
454         if (snd_mixer_oss_notify_callback)
455                 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
456 #endif
457         return 0;
458 }
459
460 static snd_info_entry_t *snd_card_info_entry = NULL;
461
462 static void snd_card_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
463 {
464         int idx, count;
465         snd_card_t *card;
466
467         for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
468                 read_lock(&snd_card_rwlock);
469                 if ((card = snd_cards[idx]) != NULL) {
470                         count++;
471                         snd_iprintf(buffer, "%i [%-15s]: %s - %s\n",
472                                         idx,
473                                         card->id,
474                                         card->driver,
475                                         card->shortname);
476                         snd_iprintf(buffer, "                     %s\n",
477                                         card->longname);
478                 }
479                 read_unlock(&snd_card_rwlock);
480         }
481         if (!count)
482                 snd_iprintf(buffer, "--- no soundcards ---\n");
483 }
484
485 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
486
487 void snd_card_info_read_oss(snd_info_buffer_t * buffer)
488 {
489         int idx, count;
490         snd_card_t *card;
491
492         for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
493                 read_lock(&snd_card_rwlock);
494                 if ((card = snd_cards[idx]) != NULL) {
495                         count++;
496                         snd_iprintf(buffer, "%s\n", card->longname);
497                 }
498                 read_unlock(&snd_card_rwlock);
499         }
500         if (!count) {
501                 snd_iprintf(buffer, "--- no soundcards ---\n");
502         }
503 }
504
505 #endif
506
507 #ifdef MODULE
508 static snd_info_entry_t *snd_card_module_info_entry;
509 static void snd_card_module_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
510 {
511         int idx;
512         snd_card_t *card;
513
514         for (idx = 0; idx < SNDRV_CARDS; idx++) {
515                 read_lock(&snd_card_rwlock);
516                 if ((card = snd_cards[idx]) != NULL)
517                         snd_iprintf(buffer, "%i %s\n", idx, card->module->name);
518                 read_unlock(&snd_card_rwlock);
519         }
520 }
521 #endif
522
523 int __init snd_card_info_init(void)
524 {
525         snd_info_entry_t *entry;
526
527         entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
528         snd_runtime_check(entry != NULL, return -ENOMEM);
529         entry->c.text.read_size = PAGE_SIZE;
530         entry->c.text.read = snd_card_info_read;
531         if (snd_info_register(entry) < 0) {
532                 snd_info_free_entry(entry);
533                 return -ENOMEM;
534         }
535         snd_card_info_entry = entry;
536
537 #ifdef MODULE
538         entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
539         if (entry) {
540                 entry->c.text.read_size = PAGE_SIZE;
541                 entry->c.text.read = snd_card_module_info_read;
542                 if (snd_info_register(entry) < 0)
543                         snd_info_free_entry(entry);
544                 else
545                         snd_card_module_info_entry = entry;
546         }
547 #endif
548
549         return 0;
550 }
551
552 int __exit snd_card_info_done(void)
553 {
554         if (snd_card_info_entry)
555                 snd_info_unregister(snd_card_info_entry);
556 #ifdef MODULE
557         if (snd_card_module_info_entry)
558                 snd_info_unregister(snd_card_module_info_entry);
559 #endif
560         return 0;
561 }
562
563 /**
564  *  snd_component_add - add a component string
565  *  @card: soundcard structure
566  *  @component: the component id string
567  *
568  *  This function adds the component id string to the supported list.
569  *  The component can be referred from the alsa-lib.
570  *
571  *  Returns zero otherwise a negative error code.
572  */
573   
574 int snd_component_add(snd_card_t *card, const char *component)
575 {
576         char *ptr;
577         int len = strlen(component);
578
579         ptr = strstr(card->components, component);
580         if (ptr != NULL) {
581                 if (ptr[len] == '\0' || ptr[len] == ' ')        /* already there */
582                         return 1;
583         }
584         if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
585                 snd_BUG();
586                 return -ENOMEM;
587         }
588         if (card->components[0] != '\0')
589                 strcat(card->components, " ");
590         strcat(card->components, component);
591         return 0;
592 }
593
594 /**
595  *  snd_card_file_add - add the file to the file list of the card
596  *  @card: soundcard structure
597  *  @file: file pointer
598  *
599  *  This function adds the file to the file linked-list of the card.
600  *  This linked-list is used to keep tracking the connection state,
601  *  and to avoid the release of busy resources by hotplug.
602  *
603  *  Returns zero or a negative error code.
604  */
605 int snd_card_file_add(snd_card_t *card, struct file *file)
606 {
607         struct snd_monitor_file *mfile;
608
609         mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
610         if (mfile == NULL)
611                 return -ENOMEM;
612         mfile->file = file;
613         mfile->next = NULL;
614         spin_lock(&card->files_lock);
615         if (card->shutdown) {
616                 spin_unlock(&card->files_lock);
617                 kfree(mfile);
618                 return -ENODEV;
619         }
620         mfile->next = card->files;
621         card->files = mfile;
622         spin_unlock(&card->files_lock);
623         return 0;
624 }
625
626 /**
627  *  snd_card_file_remove - remove the file from the file list
628  *  @card: soundcard structure
629  *  @file: file pointer
630  *
631  *  This function removes the file formerly added to the card via
632  *  snd_card_file_add() function.
633  *  If all files are removed and the release of the card is
634  *  scheduled, it will wake up the the thread to call snd_card_free()
635  *  (see snd_card_free_in_thread() function).
636  *
637  *  Returns zero or a negative error code.
638  */
639 int snd_card_file_remove(snd_card_t *card, struct file *file)
640 {
641         struct snd_monitor_file *mfile, *pfile = NULL;
642
643         spin_lock(&card->files_lock);
644         mfile = card->files;
645         while (mfile) {
646                 if (mfile->file == file) {
647                         if (pfile)
648                                 pfile->next = mfile->next;
649                         else
650                                 card->files = mfile->next;
651                         break;
652                 }
653                 pfile = mfile;
654                 mfile = mfile->next;
655         }
656         spin_unlock(&card->files_lock);
657         if (card->files == NULL)
658                 wake_up(&card->shutdown_sleep);
659         if (!mfile) {
660                 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
661                 return -ENOENT;
662         }
663         kfree(mfile);
664         return 0;
665 }
666
667 #ifdef CONFIG_PM
668 /**
669  *  snd_power_wait - wait until the power-state is changed.
670  *  @card: soundcard structure
671  *  @power_state: expected power state
672  *  @file: file structure for the O_NONBLOCK check (optional)
673  *
674  *  Waits until the power-state is changed.
675  *
676  *  Note: the power lock must be active before call.
677  */
678 int snd_power_wait(snd_card_t *card, unsigned int power_state, struct file *file)
679 {
680         wait_queue_t wait;
681         int result = 0;
682
683         /* fastpath */
684         if (snd_power_get_state(card) == power_state)
685                 return 0;
686         init_waitqueue_entry(&wait, current);
687         add_wait_queue(&card->power_sleep, &wait);
688         while (1) {
689                 if (card->shutdown) {
690                         result = -ENODEV;
691                         break;
692                 }
693                 if (snd_power_get_state(card) == power_state)
694                         break;
695 #if 0 /* block all devices */
696                 if (file && (file->f_flags & O_NONBLOCK)) {
697                         result = -EAGAIN;
698                         break;
699                 }
700 #endif
701                 set_current_state(TASK_UNINTERRUPTIBLE);
702                 snd_power_unlock(card);
703                 schedule_timeout(30 * HZ);
704                 snd_power_lock(card);
705         }
706         remove_wait_queue(&card->power_sleep, &wait);
707         return result;
708 }
709
710 /**
711  * snd_card_set_pm_callback - set the PCI power-management callbacks
712  * @card: soundcard structure
713  * @suspend: suspend callback function
714  * @resume: resume callback function
715  * @private_data: private data to pass to the callback functions
716  *
717  * Sets the power-management callback functions of the card.
718  * These callbacks are called from ALSA's common PCI suspend/resume
719  * handler and from the control API.
720  */
721 int snd_card_set_pm_callback(snd_card_t *card,
722                              int (*suspend)(snd_card_t *, unsigned int),
723                              int (*resume)(snd_card_t *, unsigned int),
724                              void *private_data)
725 {
726         card->pm_suspend = suspend;
727         card->pm_resume = resume;
728         card->pm_private_data = private_data;
729         return 0;
730 }
731
732 static int snd_generic_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
733 {
734         snd_card_t *card = dev->data;
735
736         switch (rqst) {
737         case PM_SUSPEND:
738                 if (card->power_state == SNDRV_CTL_POWER_D3hot)
739                         break;
740                 /* FIXME: the correct state value? */
741                 card->pm_suspend(card, 0);
742                 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
743                 break;
744         case PM_RESUME:
745                 if (card->power_state == SNDRV_CTL_POWER_D0)
746                         break;
747                 /* FIXME: the correct state value? */
748                 card->pm_resume(card, 0);
749                 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
750                 break;
751         }
752         return 0;
753 }
754
755 /**
756  * snd_card_set_dev_pm_callback - set the generic power-management callbacks
757  * @card: soundcard structure
758  * @type: PM device type (PM_XXX)
759  * @suspend: suspend callback function
760  * @resume: resume callback function
761  * @private_data: private data to pass to the callback functions
762  *
763  * Registers the power-management and sets the lowlevel callbacks for
764  * the given card with the given PM type.  These callbacks are called
765  * from the ALSA's common PM handler and from the control API.
766  */
767 int snd_card_set_dev_pm_callback(snd_card_t *card, int type,
768                                  int (*suspend)(snd_card_t *, unsigned int),
769                                  int (*resume)(snd_card_t *, unsigned int),
770                                  void *private_data)
771 {
772         card->pm_dev = pm_register(type, 0, snd_generic_pm_callback);
773         if (! card->pm_dev)
774                 return -ENOMEM;
775         card->pm_dev->data = card;
776         snd_card_set_pm_callback(card, suspend, resume, private_data);
777         return 0;
778 }
779
780 #ifdef CONFIG_PCI
781 int snd_card_pci_suspend(struct pci_dev *dev, u32 state)
782 {
783         snd_card_t *card = pci_get_drvdata(dev);
784         int err;
785         if (! card || ! card->pm_suspend)
786                 return 0;
787         if (card->power_state == SNDRV_CTL_POWER_D3hot)
788                 return 0;
789         /* FIXME: correct state value? */
790         err = card->pm_suspend(card, 0);
791         pci_save_state(dev);
792         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
793         return err;
794 }
795
796 int snd_card_pci_resume(struct pci_dev *dev)
797 {
798         snd_card_t *card = pci_get_drvdata(dev);
799         if (! card || ! card->pm_resume)
800                 return 0;
801         if (card->power_state == SNDRV_CTL_POWER_D0)
802                 return 0;
803         /* restore the PCI config space */
804         pci_restore_state(dev);
805         /* FIXME: correct state value? */
806         card->pm_resume(card, 0);
807         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
808         return 0;
809 }
810 #endif
811
812 #endif /* CONFIG_PM */