[ALSA] seq-midi - silently ignore non-MIDI events
[linux-2.6] / 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 DEFINE_RWLOCK(snd_card_rwlock);
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 < 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;
93                         }
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;
105         }
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;
127         }
128         if ((err = snd_info_card_create(card)) < 0) {
129                 snd_printd("unable to create card info\n");
130                 goto __error_ctl;
131         }
132         if (extra_size > 0)
133                 card->private_data = (char *)card + sizeof(snd_card_t);
134         return card;
135
136       __error_ctl:
137         snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
138       __error:
139         kfree(card);
140         return NULL;
141 }
142
143 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
144 {
145         return POLLERR | POLLNVAL;
146 }
147
148 /**
149  *  snd_card_disconnect - disconnect all APIs from the file-operations (user space)
150  *  @card: soundcard structure
151  *
152  *  Disconnects all APIs from the file-operations (user space).
153  *
154  *  Returns zero, otherwise a negative error code.
155  *
156  *  Note: The current implementation replaces all active file->f_op with special
157  *        dummy file operations (they do nothing except release).
158  */
159 int snd_card_disconnect(snd_card_t * card)
160 {
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;
166
167         spin_lock(&card->files_lock);
168         if (card->shutdown) {
169                 spin_unlock(&card->files_lock);
170                 return 0;
171         }
172         card->shutdown = 1;
173         spin_unlock(&card->files_lock);
174
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);
179         
180         /* phase 2: replace file->f_op with special dummy operations */
181         
182         spin_lock(&card->files_lock);
183         mfile = card->files;
184         while (mfile) {
185                 file = mfile->file;
186
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!");
192
193                 f_ops = &s_f_ops->f_ops;
194
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;
199
200                 s_f_ops->next = card->s_f_ops;
201                 card->s_f_ops = s_f_ops;
202                 
203                 f_ops = fops_get(f_ops);
204
205                 old_f_ops = file->f_op;
206                 file->f_op = f_ops;     /* must be atomic */
207                 fops_put(old_f_ops);
208                 
209                 mfile = mfile->next;
210         }
211         spin_unlock(&card->files_lock); 
212
213         /* phase 3: notify all connected devices about disconnection */
214         /* at this point, they cannot respond to any calls except release() */
215
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
220
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);
225
226         return 0;       
227 }
228
229 #if defined(CONFIG_PM) && defined(CONFIG_SND_GENERIC_PM)
230 static void snd_generic_device_unregister(struct snd_generic_device *dev);
231 #endif
232
233 /**
234  *  snd_card_free - frees given soundcard structure
235  *  @card: soundcard structure
236  *
237  *  This function releases the soundcard structure and the all assigned
238  *  devices automatically.  That is, you don't have to release the devices
239  *  by yourself.
240  *
241  *  Returns zero. Frees all associated devices and frees the control
242  *  interface associated to given soundcard.
243  */
244 int snd_card_free(snd_card_t * card)
245 {
246         struct snd_shutdown_f_ops *s_f_ops;
247
248         if (card == NULL)
249                 return -EINVAL;
250         write_lock(&snd_card_rwlock);
251         snd_cards[card->number] = NULL;
252         write_unlock(&snd_card_rwlock);
253
254 #ifdef CONFIG_PM
255         wake_up(&card->power_sleep);
256 #ifdef CONFIG_SND_GENERIC_PM
257         if (card->pm_dev) {
258                 snd_generic_device_unregister(card->pm_dev);
259                 card->pm_dev = NULL;
260         }
261 #endif
262 #endif
263
264         /* wait, until all devices are ready for the free operation */
265         wait_event(card->shutdown_sleep, card->files == NULL);
266
267 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
268         if (snd_mixer_oss_notify_callback)
269                 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
270 #endif
271         if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
272                 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
273                 /* Fatal, but this situation should never occur */
274         }
275         if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
276                 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
277                 /* Fatal, but this situation should never occur */
278         }
279         if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
280                 snd_printk(KERN_ERR "unable to free all devices (post)\n");
281                 /* Fatal, but this situation should never occur */
282         }
283         if (card->private_free)
284                 card->private_free(card);
285         if (card->proc_id)
286                 snd_info_unregister(card->proc_id);
287         if (snd_info_card_free(card) < 0) {
288                 snd_printk(KERN_WARNING "unable to free card info\n");
289                 /* Not fatal error */
290         }
291         while (card->s_f_ops) {
292                 s_f_ops = card->s_f_ops;
293                 card->s_f_ops = s_f_ops->next;
294                 kfree(s_f_ops);
295         }
296         write_lock(&snd_card_rwlock);
297         snd_cards_lock &= ~(1 << card->number);
298         write_unlock(&snd_card_rwlock);
299         kfree(card);
300         return 0;
301 }
302
303 static void snd_card_free_thread(void * __card)
304 {
305         snd_card_t *card = __card;
306         struct module * module = card->module;
307
308         if (!try_module_get(module)) {
309                 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
310                 module = NULL;
311         }
312
313         snd_card_free(card);
314
315         module_put(module);
316 }
317
318 /**
319  *  snd_card_free_in_thread - call snd_card_free() in thread
320  *  @card: soundcard structure
321  *
322  *  This function schedules the call of snd_card_free() function in a
323  *  work queue.  When all devices are released (non-busy), the work
324  *  is woken up and calls snd_card_free().
325  *
326  *  When a card can be disconnected at any time by hotplug service,
327  *  this function should be used in disconnect (or detach) callback
328  *  instead of calling snd_card_free() directly.
329  *  
330  *  Returns - zero otherwise a negative error code if the start of thread failed.
331  */
332 int snd_card_free_in_thread(snd_card_t * card)
333 {
334         if (card->files == NULL) {
335                 snd_card_free(card);
336                 return 0;
337         }
338
339         if (schedule_work(&card->free_workq))
340                 return 0;
341
342         snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
343         /* try to free the structure immediately */
344         snd_card_free(card);
345         return -EFAULT;
346 }
347
348 static void choose_default_id(snd_card_t * card)
349 {
350         int i, len, idx_flag = 0, loops = 8;
351         char *id, *spos;
352         
353         id = spos = card->shortname;    
354         while (*id != '\0') {
355                 if (*id == ' ')
356                         spos = id + 1;
357                 id++;
358         }
359         id = card->id;
360         while (*spos != '\0' && !isalnum(*spos))
361                 spos++;
362         if (isdigit(*spos))
363                 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
364         while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
365                 if (isalnum(*spos))
366                         *id++ = *spos;
367                 spos++;
368         }
369         *id = '\0';
370
371         id = card->id;
372         
373         if (*id == '\0')
374                 strcpy(id, "default");
375
376         while (1) {
377                 if (loops-- == 0) {
378                         snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
379                         strcpy(card->id, card->proc_root->name);
380                         return;
381                 }
382                 if (!snd_info_check_reserved_words(id))
383                         goto __change;
384                 for (i = 0; i < snd_ecards_limit; i++) {
385                         if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
386                                 goto __change;
387                 }
388                 break;
389
390               __change:
391                 len = strlen(id);
392                 if (idx_flag)
393                         id[len-1]++;
394                 else if ((size_t)len <= sizeof(card->id) - 3) {
395                         strcat(id, "_1");
396                         idx_flag++;
397                 } else {
398                         spos = id + len - 2;
399                         if ((size_t)len <= sizeof(card->id) - 2)
400                                 spos++;
401                         *spos++ = '_';
402                         *spos++ = '1';
403                         *spos++ = '\0';
404                         idx_flag++;
405                 }
406         }
407 }
408
409 /**
410  *  snd_card_register - register the soundcard
411  *  @card: soundcard structure
412  *
413  *  This function registers all the devices assigned to the soundcard.
414  *  Until calling this, the ALSA control interface is blocked from the
415  *  external accesses.  Thus, you should call this function at the end
416  *  of the initialization of the card.
417  *
418  *  Returns zero otherwise a negative error code if the registrain failed.
419  */
420 int snd_card_register(snd_card_t * card)
421 {
422         int err;
423         snd_info_entry_t *entry;
424
425         snd_runtime_check(card != NULL, return -EINVAL);
426         if ((err = snd_device_register_all(card)) < 0)
427                 return err;
428         write_lock(&snd_card_rwlock);
429         if (snd_cards[card->number]) {
430                 /* already registered */
431                 write_unlock(&snd_card_rwlock);
432                 return 0;
433         }
434         if (card->id[0] == '\0')
435                 choose_default_id(card);
436         snd_cards[card->number] = card;
437         write_unlock(&snd_card_rwlock);
438         if ((err = snd_info_card_register(card)) < 0) {
439                 snd_printd("unable to create card info\n");
440                 goto __skip_info;
441         }
442         if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
443                 snd_printd("unable to create card entry\n");
444                 goto __skip_info;
445         }
446         entry->c.text.read_size = PAGE_SIZE;
447         entry->c.text.read = snd_card_id_read;
448         if (snd_info_register(entry) < 0) {
449                 snd_info_free_entry(entry);
450                 entry = NULL;
451         }
452         card->proc_id = entry;
453       __skip_info:
454 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
455         if (snd_mixer_oss_notify_callback)
456                 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
457 #endif
458         return 0;
459 }
460
461 static snd_info_entry_t *snd_card_info_entry = NULL;
462
463 static void snd_card_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
464 {
465         int idx, count;
466         snd_card_t *card;
467
468         for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
469                 read_lock(&snd_card_rwlock);
470                 if ((card = snd_cards[idx]) != NULL) {
471                         count++;
472                         snd_iprintf(buffer, "%i [%-15s]: %s - %s\n",
473                                         idx,
474                                         card->id,
475                                         card->driver,
476                                         card->shortname);
477                         snd_iprintf(buffer, "                     %s\n",
478                                         card->longname);
479                 }
480                 read_unlock(&snd_card_rwlock);
481         }
482         if (!count)
483                 snd_iprintf(buffer, "--- no soundcards ---\n");
484 }
485
486 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
487
488 void snd_card_info_read_oss(snd_info_buffer_t * buffer)
489 {
490         int idx, count;
491         snd_card_t *card;
492
493         for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
494                 read_lock(&snd_card_rwlock);
495                 if ((card = snd_cards[idx]) != NULL) {
496                         count++;
497                         snd_iprintf(buffer, "%s\n", card->longname);
498                 }
499                 read_unlock(&snd_card_rwlock);
500         }
501         if (!count) {
502                 snd_iprintf(buffer, "--- no soundcards ---\n");
503         }
504 }
505
506 #endif
507
508 #ifdef MODULE
509 static snd_info_entry_t *snd_card_module_info_entry;
510 static void snd_card_module_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
511 {
512         int idx;
513         snd_card_t *card;
514
515         for (idx = 0; idx < SNDRV_CARDS; idx++) {
516                 read_lock(&snd_card_rwlock);
517                 if ((card = snd_cards[idx]) != NULL)
518                         snd_iprintf(buffer, "%i %s\n", idx, card->module->name);
519                 read_unlock(&snd_card_rwlock);
520         }
521 }
522 #endif
523
524 int __init snd_card_info_init(void)
525 {
526         snd_info_entry_t *entry;
527
528         entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
529         snd_runtime_check(entry != NULL, return -ENOMEM);
530         entry->c.text.read_size = PAGE_SIZE;
531         entry->c.text.read = snd_card_info_read;
532         if (snd_info_register(entry) < 0) {
533                 snd_info_free_entry(entry);
534                 return -ENOMEM;
535         }
536         snd_card_info_entry = entry;
537
538 #ifdef MODULE
539         entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
540         if (entry) {
541                 entry->c.text.read_size = PAGE_SIZE;
542                 entry->c.text.read = snd_card_module_info_read;
543                 if (snd_info_register(entry) < 0)
544                         snd_info_free_entry(entry);
545                 else
546                         snd_card_module_info_entry = entry;
547         }
548 #endif
549
550         return 0;
551 }
552
553 int __exit snd_card_info_done(void)
554 {
555         if (snd_card_info_entry)
556                 snd_info_unregister(snd_card_info_entry);
557 #ifdef MODULE
558         if (snd_card_module_info_entry)
559                 snd_info_unregister(snd_card_module_info_entry);
560 #endif
561         return 0;
562 }
563
564 /**
565  *  snd_component_add - add a component string
566  *  @card: soundcard structure
567  *  @component: the component id string
568  *
569  *  This function adds the component id string to the supported list.
570  *  The component can be referred from the alsa-lib.
571  *
572  *  Returns zero otherwise a negative error code.
573  */
574   
575 int snd_component_add(snd_card_t *card, const char *component)
576 {
577         char *ptr;
578         int len = strlen(component);
579
580         ptr = strstr(card->components, component);
581         if (ptr != NULL) {
582                 if (ptr[len] == '\0' || ptr[len] == ' ')        /* already there */
583                         return 1;
584         }
585         if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
586                 snd_BUG();
587                 return -ENOMEM;
588         }
589         if (card->components[0] != '\0')
590                 strcat(card->components, " ");
591         strcat(card->components, component);
592         return 0;
593 }
594
595 /**
596  *  snd_card_file_add - add the file to the file list of the card
597  *  @card: soundcard structure
598  *  @file: file pointer
599  *
600  *  This function adds the file to the file linked-list of the card.
601  *  This linked-list is used to keep tracking the connection state,
602  *  and to avoid the release of busy resources by hotplug.
603  *
604  *  Returns zero or a negative error code.
605  */
606 int snd_card_file_add(snd_card_t *card, struct file *file)
607 {
608         struct snd_monitor_file *mfile;
609
610         mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
611         if (mfile == NULL)
612                 return -ENOMEM;
613         mfile->file = file;
614         mfile->next = NULL;
615         spin_lock(&card->files_lock);
616         if (card->shutdown) {
617                 spin_unlock(&card->files_lock);
618                 kfree(mfile);
619                 return -ENODEV;
620         }
621         mfile->next = card->files;
622         card->files = mfile;
623         spin_unlock(&card->files_lock);
624         return 0;
625 }
626
627 /**
628  *  snd_card_file_remove - remove the file from the file list
629  *  @card: soundcard structure
630  *  @file: file pointer
631  *
632  *  This function removes the file formerly added to the card via
633  *  snd_card_file_add() function.
634  *  If all files are removed and the release of the card is
635  *  scheduled, it will wake up the the thread to call snd_card_free()
636  *  (see snd_card_free_in_thread() function).
637  *
638  *  Returns zero or a negative error code.
639  */
640 int snd_card_file_remove(snd_card_t *card, struct file *file)
641 {
642         struct snd_monitor_file *mfile, *pfile = NULL;
643
644         spin_lock(&card->files_lock);
645         mfile = card->files;
646         while (mfile) {
647                 if (mfile->file == file) {
648                         if (pfile)
649                                 pfile->next = mfile->next;
650                         else
651                                 card->files = mfile->next;
652                         break;
653                 }
654                 pfile = mfile;
655                 mfile = mfile->next;
656         }
657         spin_unlock(&card->files_lock);
658         if (card->files == NULL)
659                 wake_up(&card->shutdown_sleep);
660         if (!mfile) {
661                 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
662                 return -ENOENT;
663         }
664         kfree(mfile);
665         return 0;
666 }
667
668 #ifdef CONFIG_PM
669 /**
670  *  snd_power_wait - wait until the power-state is changed.
671  *  @card: soundcard structure
672  *  @power_state: expected power state
673  *  @file: file structure for the O_NONBLOCK check (optional)
674  *
675  *  Waits until the power-state is changed.
676  *
677  *  Note: the power lock must be active before call.
678  */
679 int snd_power_wait(snd_card_t *card, unsigned int power_state, struct file *file)
680 {
681         wait_queue_t wait;
682         int result = 0;
683
684         /* fastpath */
685         if (snd_power_get_state(card) == power_state)
686                 return 0;
687         init_waitqueue_entry(&wait, current);
688         add_wait_queue(&card->power_sleep, &wait);
689         while (1) {
690                 if (card->shutdown) {
691                         result = -ENODEV;
692                         break;
693                 }
694                 if (snd_power_get_state(card) == power_state)
695                         break;
696 #if 0 /* block all devices */
697                 if (file && (file->f_flags & O_NONBLOCK)) {
698                         result = -EAGAIN;
699                         break;
700                 }
701 #endif
702                 set_current_state(TASK_UNINTERRUPTIBLE);
703                 snd_power_unlock(card);
704                 schedule_timeout(30 * HZ);
705                 snd_power_lock(card);
706         }
707         remove_wait_queue(&card->power_sleep, &wait);
708         return result;
709 }
710
711 /**
712  * snd_card_set_pm_callback - set the PCI power-management callbacks
713  * @card: soundcard structure
714  * @suspend: suspend callback function
715  * @resume: resume callback function
716  * @private_data: private data to pass to the callback functions
717  *
718  * Sets the power-management callback functions of the card.
719  * These callbacks are called from ALSA's common PCI suspend/resume
720  * handler and from the control API.
721  */
722 int snd_card_set_pm_callback(snd_card_t *card,
723                              int (*suspend)(snd_card_t *, pm_message_t),
724                              int (*resume)(snd_card_t *),
725                              void *private_data)
726 {
727         card->pm_suspend = suspend;
728         card->pm_resume = resume;
729         card->pm_private_data = private_data;
730         return 0;
731 }
732
733 #ifdef CONFIG_SND_GENERIC_PM
734 /*
735  * use platform_device for generic power-management without a proper bus
736  * (e.g. ISA)
737  */
738 struct snd_generic_device {
739         struct platform_device pdev;
740         snd_card_t *card;
741 };
742
743 #define get_snd_generic_card(dev)       container_of(to_platform_device(dev), struct snd_generic_device, pdev)->card
744
745 #define SND_GENERIC_NAME        "snd_generic_pm"
746
747 static int snd_generic_suspend(struct device *dev, pm_message_t state, u32 level);
748 static int snd_generic_resume(struct device *dev, u32 level);
749
750 static struct device_driver snd_generic_driver = {
751         .name           = SND_GENERIC_NAME,
752         .bus            = &platform_bus_type,
753         .suspend        = snd_generic_suspend,
754         .resume         = snd_generic_resume,
755 };
756
757 static int generic_driver_registered;
758
759 static void generic_driver_unregister(void)
760 {
761         if (generic_driver_registered) {
762                 generic_driver_registered--;
763                 if (! generic_driver_registered)
764                         driver_unregister(&snd_generic_driver);
765         }
766 }
767
768 static struct snd_generic_device *snd_generic_device_register(snd_card_t *card)
769 {
770         struct snd_generic_device *dev;
771
772         if (! generic_driver_registered) {
773                 if (driver_register(&snd_generic_driver) < 0)
774                         return NULL;
775         }
776         generic_driver_registered++;
777
778         dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
779         if (! dev) {
780                 generic_driver_unregister();
781                 return NULL;
782         }
783
784         dev->pdev.name = SND_GENERIC_NAME;
785         dev->pdev.id = card->number;
786         dev->card = card;
787         if (platform_device_register(&dev->pdev) < 0) {
788                 kfree(dev);
789                 generic_driver_unregister();
790                 return NULL;
791         }
792         return dev;
793 }
794
795 static void snd_generic_device_unregister(struct snd_generic_device *dev)
796 {
797         platform_device_unregister(&dev->pdev);
798         kfree(dev);
799         generic_driver_unregister();
800 }
801
802 /* suspend/resume callbacks for snd_generic platform device */
803 static int snd_generic_suspend(struct device *dev, pm_message_t state, u32 level)
804 {
805         snd_card_t *card;
806
807         if (level != SUSPEND_DISABLE)
808                 return 0;
809
810         card = get_snd_generic_card(dev);
811         if (card->power_state == SNDRV_CTL_POWER_D3hot)
812                 return 0;
813         card->pm_suspend(card, PMSG_SUSPEND);
814         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
815         return 0;
816 }
817
818 static int snd_generic_resume(struct device *dev, u32 level)
819 {
820         snd_card_t *card;
821
822         if (level != RESUME_ENABLE)
823                 return 0;
824
825         card = get_snd_generic_card(dev);
826         if (card->power_state == SNDRV_CTL_POWER_D0)
827                 return 0;
828         card->pm_resume(card);
829         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
830         return 0;
831 }
832
833 /**
834  * snd_card_set_generic_pm_callback - set the generic power-management callbacks
835  * @card: soundcard structure
836  * @suspend: suspend callback function
837  * @resume: resume callback function
838  * @private_data: private data to pass to the callback functions
839  *
840  * Registers the power-management and sets the lowlevel callbacks for
841  * the given card.  These callbacks are called from the ALSA's common
842  * PM handler and from the control API.
843  */
844 int snd_card_set_generic_pm_callback(snd_card_t *card,
845                                  int (*suspend)(snd_card_t *, pm_message_t),
846                                  int (*resume)(snd_card_t *),
847                                  void *private_data)
848 {
849         card->pm_dev = snd_generic_device_register(card);
850         if (! card->pm_dev)
851                 return -ENOMEM;
852         snd_card_set_pm_callback(card, suspend, resume, private_data);
853         return 0;
854 }
855 #endif /* CONFIG_SND_GENERIC_PM */
856
857 #ifdef CONFIG_PCI
858 int snd_card_pci_suspend(struct pci_dev *dev, pm_message_t state)
859 {
860         snd_card_t *card = pci_get_drvdata(dev);
861         int err;
862         if (! card || ! card->pm_suspend)
863                 return 0;
864         if (card->power_state == SNDRV_CTL_POWER_D3hot)
865                 return 0;
866         err = card->pm_suspend(card, PMSG_SUSPEND);
867         pci_save_state(dev);
868         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
869         return err;
870 }
871
872 int snd_card_pci_resume(struct pci_dev *dev)
873 {
874         snd_card_t *card = pci_get_drvdata(dev);
875         if (! card || ! card->pm_resume)
876                 return 0;
877         if (card->power_state == SNDRV_CTL_POWER_D0)
878                 return 0;
879         /* restore the PCI config space */
880         pci_restore_state(dev);
881         card->pm_resume(card);
882         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
883         return 0;
884 }
885 #endif
886
887 #endif /* CONFIG_PM */