xref: /linux/sound/core/init.c (revision 54a8a2220c936a47840c9a3d74910c5a56fae2ed)
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 = 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));
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 #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
234 
235 /**
236  *  snd_card_free - frees given soundcard structure
237  *  @card: soundcard structure
238  *
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.
242  *
243  *  Returns zero. Frees all associated devices and frees the control
244  *  interface associated to given soundcard.
245  */
246 int snd_card_free(snd_card_t * card)
247 {
248 	struct snd_shutdown_f_ops *s_f_ops;
249 
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);
255 
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);
261 
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 */
269 	}
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 */
273 	}
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 */
277 	}
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 */
285 	}
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);
291 	}
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;
297 }
298 
299 static void snd_card_free_thread(void * __card)
300 {
301 	snd_card_t *card = __card;
302 	struct module * module = card->module;
303 
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;
307 	}
308 
309 	snd_card_free(card);
310 
311 	module_put(module);
312 }
313 
314 /**
315  *  snd_card_free_in_thread - call snd_card_free() in thread
316  *  @card: soundcard structure
317  *
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().
321  *
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.
325  *
326  *  Returns - zero otherwise a negative error code if the start of thread failed.
327  */
328 int snd_card_free_in_thread(snd_card_t * card)
329 {
330 	if (card->files == NULL) {
331 		snd_card_free(card);
332 		return 0;
333 	}
334 
335 	if (schedule_work(&card->free_workq))
336 		return 0;
337 
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;
342 }
343 
344 static void choose_default_id(snd_card_t * card)
345 {
346 	int i, len, idx_flag = 0, loops = 8;
347 	char *id, *spos;
348 
349 	id = spos = card->shortname;
350 	while (*id != '\0') {
351 		if (*id == ' ')
352 			spos = id + 1;
353 		id++;
354 	}
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++;
364 	}
365 	*id = '\0';
366 
367 	id = card->id;
368 
369 	if (*id == '\0')
370 		strcpy(id, "default");
371 
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;
377       		}
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;
383 		}
384 		break;
385 
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++;
401 		}
402 	}
403 }
404 
405 /**
406  *  snd_card_register - register the soundcard
407  *  @card: soundcard structure
408  *
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.
413  *
414  *  Returns zero otherwise a negative error code if the registrain failed.
415  */
416 int snd_card_register(snd_card_t * card)
417 {
418 	int err;
419 	snd_info_entry_t *entry;
420 
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;
429 	}
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;
437 	}
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;
441 	}
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;
447 	}
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;
455 }
456 
457 static snd_info_entry_t *snd_card_info_entry = NULL;
458 
459 static void snd_card_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
460 {
461 	int idx, count;
462 	snd_card_t *card;
463 
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);
475 		}
476 		read_unlock(&snd_card_rwlock);
477 	}
478 	if (!count)
479 		snd_iprintf(buffer, "--- no soundcards ---\n");
480 }
481 
482 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
483 
484 void snd_card_info_read_oss(snd_info_buffer_t * buffer)
485 {
486 	int idx, count;
487 	snd_card_t *card;
488 
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);
494 		}
495 		read_unlock(&snd_card_rwlock);
496 	}
497 	if (!count) {
498 		snd_iprintf(buffer, "--- no soundcards ---\n");
499 	}
500 }
501 
502 #endif
503 
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)
507 {
508 	int idx;
509 	snd_card_t *card;
510 
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);
516 	}
517 }
518 #endif
519 
520 int __init snd_card_info_init(void)
521 {
522 	snd_info_entry_t *entry;
523 
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;
531 	}
532 	snd_card_info_entry = entry;
533 
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;
543 	}
544 #endif
545 
546 	return 0;
547 }
548 
549 int __exit snd_card_info_done(void)
550 {
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;
558 }
559 
560 /**
561  *  snd_component_add - add a component string
562  *  @card: soundcard structure
563  *  @component: the component id string
564  *
565  *  This function adds the component id string to the supported list.
566  *  The component can be referred from the alsa-lib.
567  *
568  *  Returns zero otherwise a negative error code.
569  */
570 
571 int snd_component_add(snd_card_t *card, const char *component)
572 {
573 	char *ptr;
574 	int len = strlen(component);
575 
576 	ptr = strstr(card->components, component);
577 	if (ptr != NULL) {
578 		if (ptr[len] == '\0' || ptr[len] == ' ')	/* already there */
579 			return 1;
580 	}
581 	if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
582 		snd_BUG();
583 		return -ENOMEM;
584 	}
585 	if (card->components[0] != '\0')
586 		strcat(card->components, " ");
587 	strcat(card->components, component);
588 	return 0;
589 }
590 
591 /**
592  *  snd_card_file_add - add the file to the file list of the card
593  *  @card: soundcard structure
594  *  @file: file pointer
595  *
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.
599  *
600  *  Returns zero or a negative error code.
601  */
602 int snd_card_file_add(snd_card_t *card, struct file *file)
603 {
604 	struct snd_monitor_file *mfile;
605 
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;
616 	}
617 	mfile->next = card->files;
618 	card->files = mfile;
619 	spin_unlock(&card->files_lock);
620 	return 0;
621 }
622 
623 /**
624  *  snd_card_file_remove - remove the file from the file list
625  *  @card: soundcard structure
626  *  @file: file pointer
627  *
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).
633  *
634  *  Returns zero or a negative error code.
635  */
636 int snd_card_file_remove(snd_card_t *card, struct file *file)
637 {
638 	struct snd_monitor_file *mfile, *pfile = NULL;
639 
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;
649 		}
650 		pfile = mfile;
651 		mfile = mfile->next;
652 	}
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;
659 	}
660 	kfree(mfile);
661 	return 0;
662 }
663 
664 #ifdef CONFIG_SND_GENERIC_DRIVER
665 /*
666  * generic device without a proper bus using platform_device
667  * (e.g. ISA)
668  */
669 struct snd_generic_device {
670 	struct platform_device pdev;
671 	snd_card_t *card;
672 };
673 
674 #define get_snd_generic_card(dev)	container_of(to_platform_device(dev), struct snd_generic_device, pdev)->card
675 
676 #define SND_GENERIC_NAME	"snd_generic"
677 
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
682 
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
691 };
692 
693 void snd_generic_device_release(struct device *dev)
694 {
695 }
696 
697 static int snd_generic_device_register(snd_card_t *card)
698 {
699 	struct snd_generic_device *dev;
700 	int err;
701 
702 	if (card->generic_dev)
703 		return 0; /* already registered */
704 
705 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
706 	if (! dev) {
707 		snd_printk(KERN_ERR "can't allocate generic_device\n");
708 		return -ENOMEM;
709 	}
710 
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;
718 	}
719 	card->generic_dev = dev;
720 	return 0;
721 }
722 
723 static void snd_generic_device_unregister(snd_card_t *card)
724 {
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;
730 	}
731 }
732 
733 /**
734  * snd_card_set_generic_dev - assign the generic device to the card
735  * @card: soundcard structure
736  *
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.
740  *
741  * Returns zero if successful, or a negative error code.
742  */
743 int snd_card_set_generic_dev(snd_card_t *card)
744 {
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;
751 }
752 #endif /* CONFIG_SND_GENERIC_DRIVER */
753 
754 #ifdef CONFIG_PM
755 /**
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)
760  *
761  *  Waits until the power-state is changed.
762  *
763  *  Note: the power lock must be active before call.
764  */
765 int snd_power_wait(snd_card_t *card, unsigned int power_state, struct file *file)
766 {
767 	wait_queue_t wait;
768 	int result = 0;
769 
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;
779 		}
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;
786 		}
787 #endif
788 		set_current_state(TASK_UNINTERRUPTIBLE);
789 		snd_power_unlock(card);
790 		schedule_timeout(30 * HZ);
791 		snd_power_lock(card);
792 	}
793 	remove_wait_queue(&card->power_sleep, &wait);
794 	return result;
795 }
796 
797 /**
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
803  *
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.
807  */
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)
812 {
813 	card->pm_suspend = suspend;
814 	card->pm_resume = resume;
815 	card->pm_private_data = private_data;
816 	return 0;
817 }
818 
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)
822 {
823 	snd_card_t *card;
824 
825 	if (level != SUSPEND_DISABLE)
826 		return 0;
827 
828 	card = get_snd_generic_card(dev);
829 	if (card->power_state == SNDRV_CTL_POWER_D3hot)
830 		return 0;
831 	card->pm_suspend(card, PMSG_SUSPEND);
832 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
833 	return 0;
834 }
835 
836 static int snd_generic_resume(struct device *dev, u32 level)
837 {
838 	snd_card_t *card;
839 
840 	if (level != RESUME_ENABLE)
841 		return 0;
842 
843 	card = get_snd_generic_card(dev);
844 	if (card->power_state == SNDRV_CTL_POWER_D0)
845 		return 0;
846 	card->pm_resume(card);
847 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
848 	return 0;
849 }
850 
851 /**
852  * snd_card_set_generic_pm_callback - set the generic power-management callbacks
853  * @card: soundcard structure
854  * @suspend: suspend callback function
855  * @resume: resume callback function
856  * @private_data: private data to pass to the callback functions
857  *
858  * Registers the power-management and sets the lowlevel callbacks for
859  * the given card.  These callbacks are called from the ALSA's common
860  * PM handler and from the control API.
861  */
862 int snd_card_set_generic_pm_callback(snd_card_t *card,
863 				 int (*suspend)(snd_card_t *, pm_message_t),
864 				 int (*resume)(snd_card_t *),
865 				 void *private_data)
866 {
867 	int err;
868 	if ((err = snd_generic_device_register(card)) < 0)
869 		return err;
870 	return snd_card_set_pm_callback(card, suspend, resume, private_data);
871 }
872 #endif /* CONFIG_SND_GENERIC_DRIVER */
873 
874 #ifdef CONFIG_PCI
875 int snd_card_pci_suspend(struct pci_dev *dev, pm_message_t state)
876 {
877 	snd_card_t *card = pci_get_drvdata(dev);
878 	int err;
879 	if (! card || ! card->pm_suspend)
880 		return 0;
881 	if (card->power_state == SNDRV_CTL_POWER_D3hot)
882 		return 0;
883 	err = card->pm_suspend(card, PMSG_SUSPEND);
884 	pci_save_state(dev);
885 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
886 	return err;
887 }
888 
889 int snd_card_pci_resume(struct pci_dev *dev)
890 {
891 	snd_card_t *card = pci_get_drvdata(dev);
892 	if (! card || ! card->pm_resume)
893 		return 0;
894 	if (card->power_state == SNDRV_CTL_POWER_D0)
895 		return 0;
896 	/* restore the PCI config space */
897 	pci_restore_state(dev);
898 	card->pm_resume(card);
899 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
900 	return 0;
901 }
902 #endif
903 
904 #endif /* CONFIG_PM */
905