blob: 5850d99d21e32af63184db89794c54c2ee8a3bf9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Initialization routines
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/init.h>
24#include <linux/sched.h>
25#include <linux/file.h>
26#include <linux/slab.h>
27#include <linux/time.h>
28#include <linux/ctype.h>
29#include <linux/pci.h>
30#include <linux/pm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/core.h>
33#include <sound/control.h>
34#include <sound/info.h>
35
36struct snd_shutdown_f_ops {
37 struct file_operations f_ops;
38 struct snd_shutdown_f_ops *next;
39};
40
Takashi Iwai6581f4e2006-05-17 17:14:51 +020041static unsigned int snd_cards_lock; /* locked for registering/using */
42struct snd_card *snd_cards[SNDRV_CARDS];
Takashi Iwaic0d3fb32006-04-28 15:13:39 +020043EXPORT_SYMBOL(snd_cards);
44
Takashi Iwai746df942006-05-15 19:49:05 +020045static DEFINE_MUTEX(snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
Takashi Iwai512bbd62005-11-17 13:51:18 +010048int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
Takashi Iwaic0d3fb32006-04-28 15:13:39 +020049EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#endif
51
Takashi Iwaie28563c2005-12-01 10:42:42 +010052#ifdef CONFIG_PROC_FS
Takashi Iwai512bbd62005-11-17 13:51:18 +010053static void snd_card_id_read(struct snd_info_entry *entry,
54 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 snd_iprintf(buffer, "%s\n", entry->card->id);
57}
58
Takashi Iwaie28563c2005-12-01 10:42:42 +010059static inline int init_info_for_card(struct snd_card *card)
60{
61 int err;
62 struct snd_info_entry *entry;
63
64 if ((err = snd_info_card_register(card)) < 0) {
65 snd_printd("unable to create card info\n");
66 return err;
67 }
68 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
69 snd_printd("unable to create card entry\n");
70 return err;
71 }
Takashi Iwaie28563c2005-12-01 10:42:42 +010072 entry->c.text.read = snd_card_id_read;
73 if (snd_info_register(entry) < 0) {
74 snd_info_free_entry(entry);
75 entry = NULL;
76 }
77 card->proc_id = entry;
78 return 0;
79}
80#else /* !CONFIG_PROC_FS */
81#define init_info_for_card(card)
82#endif
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static void snd_card_free_thread(void * __card);
85
86/**
87 * snd_card_new - create and initialize a soundcard structure
88 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
89 * @xid: card identification (ASCII string)
90 * @module: top level module for locking
91 * @extra_size: allocate this extra size after the main soundcard structure
92 *
93 * Creates and initializes a soundcard structure.
94 *
Takashi Iwai512bbd62005-11-17 13:51:18 +010095 * Returns kmallocated snd_card structure. Creates the ALSA control interface
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * (which is blocked until snd_card_register function is called).
97 */
Takashi Iwai512bbd62005-11-17 13:51:18 +010098struct snd_card *snd_card_new(int idx, const char *xid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct module *module, int extra_size)
100{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100101 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int err;
103
104 if (extra_size < 0)
105 extra_size = 0;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200106 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (card == NULL)
108 return NULL;
109 if (xid) {
110 if (!snd_info_check_reserved_words(xid))
111 goto __error;
112 strlcpy(card->id, xid, sizeof(card->id));
113 }
114 err = 0;
Takashi Iwai746df942006-05-15 19:49:05 +0200115 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (idx < 0) {
117 int idx2;
118 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
119 if (~snd_cards_lock & idx & 1<<idx2) {
120 idx = idx2;
121 if (idx >= snd_ecards_limit)
122 snd_ecards_limit = idx + 1;
123 break;
124 }
125 } else if (idx < snd_ecards_limit) {
126 if (snd_cards_lock & (1 << idx))
127 err = -ENODEV; /* invalid */
128 } else if (idx < SNDRV_CARDS)
129 snd_ecards_limit = idx + 1; /* increase the limit */
130 else
131 err = -ENODEV;
132 if (idx < 0 || err < 0) {
Takashi Iwai746df942006-05-15 19:49:05 +0200133 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
135 goto __error;
136 }
137 snd_cards_lock |= 1 << idx; /* lock it */
Takashi Iwai746df942006-05-15 19:49:05 +0200138 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 card->number = idx;
140 card->module = module;
141 INIT_LIST_HEAD(&card->devices);
142 init_rwsem(&card->controls_rwsem);
143 rwlock_init(&card->ctl_files_rwlock);
144 INIT_LIST_HEAD(&card->controls);
145 INIT_LIST_HEAD(&card->ctl_files);
146 spin_lock_init(&card->files_lock);
147 init_waitqueue_head(&card->shutdown_sleep);
148 INIT_WORK(&card->free_workq, snd_card_free_thread, card);
149#ifdef CONFIG_PM
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100150 mutex_init(&card->power_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 init_waitqueue_head(&card->power_sleep);
152#endif
153 /* the control interface cannot be accessed from the user space until */
154 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
155 if ((err = snd_ctl_create(card)) < 0) {
156 snd_printd("unable to register control minors\n");
157 goto __error;
158 }
159 if ((err = snd_info_card_create(card)) < 0) {
160 snd_printd("unable to create card info\n");
161 goto __error_ctl;
162 }
163 if (extra_size > 0)
Takashi Iwai512bbd62005-11-17 13:51:18 +0100164 card->private_data = (char *)card + sizeof(struct snd_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return card;
166
167 __error_ctl:
168 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
169 __error:
170 kfree(card);
171 return NULL;
172}
173
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200174EXPORT_SYMBOL(snd_card_new);
175
Takashi Iwai746df942006-05-15 19:49:05 +0200176/* return non-zero if a card is already locked */
177int snd_card_locked(int card)
178{
179 int locked;
180
181 mutex_lock(&snd_card_mutex);
182 locked = snd_cards_lock & (1 << card);
183 mutex_unlock(&snd_card_mutex);
184 return locked;
185}
186
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100187static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
188{
189 return -ENODEV;
190}
191
192static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
193 size_t count, loff_t *offset)
194{
195 return -ENODEV;
196}
197
198static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
199 size_t count, loff_t *offset)
200{
201 return -ENODEV;
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
205{
206 return POLLERR | POLLNVAL;
207}
208
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100209static long snd_disconnect_ioctl(struct file *file,
210 unsigned int cmd, unsigned long arg)
211{
212 return -ENODEV;
213}
214
215static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
216{
217 return -ENODEV;
218}
219
220static int snd_disconnect_fasync(int fd, struct file *file, int on)
221{
222 return -ENODEV;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/**
226 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
227 * @card: soundcard structure
228 *
229 * Disconnects all APIs from the file-operations (user space).
230 *
231 * Returns zero, otherwise a negative error code.
232 *
233 * Note: The current implementation replaces all active file->f_op with special
234 * dummy file operations (they do nothing except release).
235 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100236int snd_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct snd_monitor_file *mfile;
239 struct file *file;
240 struct snd_shutdown_f_ops *s_f_ops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800241 struct file_operations *f_ops;
242 const struct file_operations *old_f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int err;
244
245 spin_lock(&card->files_lock);
246 if (card->shutdown) {
247 spin_unlock(&card->files_lock);
248 return 0;
249 }
250 card->shutdown = 1;
251 spin_unlock(&card->files_lock);
252
253 /* phase 1: disable fops (user space) operations for ALSA API */
Takashi Iwai746df942006-05-15 19:49:05 +0200254 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 snd_cards[card->number] = NULL;
Takashi Iwai746df942006-05-15 19:49:05 +0200256 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* phase 2: replace file->f_op with special dummy operations */
259
260 spin_lock(&card->files_lock);
261 mfile = card->files;
262 while (mfile) {
263 file = mfile->file;
264
265 /* it's critical part, use endless loop */
266 /* we have no room to fail */
267 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
268 if (s_f_ops == NULL)
269 panic("Atomic allocation failed for snd_shutdown_f_ops!");
270
271 f_ops = &s_f_ops->f_ops;
272
273 memset(f_ops, 0, sizeof(*f_ops));
274 f_ops->owner = file->f_op->owner;
275 f_ops->release = file->f_op->release;
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100276 f_ops->llseek = snd_disconnect_llseek;
277 f_ops->read = snd_disconnect_read;
278 f_ops->write = snd_disconnect_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 f_ops->poll = snd_disconnect_poll;
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100280 f_ops->unlocked_ioctl = snd_disconnect_ioctl;
281#ifdef CONFIG_COMPAT
282 f_ops->compat_ioctl = snd_disconnect_ioctl;
283#endif
284 f_ops->mmap = snd_disconnect_mmap;
285 f_ops->fasync = snd_disconnect_fasync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 s_f_ops->next = card->s_f_ops;
288 card->s_f_ops = s_f_ops;
289
290 f_ops = fops_get(f_ops);
291
292 old_f_ops = file->f_op;
293 file->f_op = f_ops; /* must be atomic */
294 fops_put(old_f_ops);
295
296 mfile = mfile->next;
297 }
298 spin_unlock(&card->files_lock);
299
300 /* phase 3: notify all connected devices about disconnection */
301 /* at this point, they cannot respond to any calls except release() */
302
303#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
304 if (snd_mixer_oss_notify_callback)
305 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
306#endif
307
308 /* notify all devices that we are disconnected */
309 err = snd_device_disconnect_all(card);
310 if (err < 0)
311 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
312
Takashi Iwai746d4a02006-06-23 14:37:59 +0200313 snd_info_card_disconnect(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315}
316
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200317EXPORT_SYMBOL(snd_card_disconnect);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/**
320 * snd_card_free - frees given soundcard structure
321 * @card: soundcard structure
322 *
323 * This function releases the soundcard structure and the all assigned
324 * devices automatically. That is, you don't have to release the devices
325 * by yourself.
326 *
327 * Returns zero. Frees all associated devices and frees the control
328 * interface associated to given soundcard.
329 */
Takashi Iwaic4614822006-06-23 14:38:23 +0200330static int snd_card_do_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct snd_shutdown_f_ops *s_f_ops;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
335 if (snd_mixer_oss_notify_callback)
336 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
337#endif
338 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
339 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
340 /* Fatal, but this situation should never occur */
341 }
342 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
343 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
344 /* Fatal, but this situation should never occur */
345 }
346 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
347 snd_printk(KERN_ERR "unable to free all devices (post)\n");
348 /* Fatal, but this situation should never occur */
349 }
350 if (card->private_free)
351 card->private_free(card);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200352 snd_info_free_entry(card->proc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (snd_info_card_free(card) < 0) {
354 snd_printk(KERN_WARNING "unable to free card info\n");
355 /* Not fatal error */
356 }
357 while (card->s_f_ops) {
358 s_f_ops = card->s_f_ops;
359 card->s_f_ops = s_f_ops->next;
360 kfree(s_f_ops);
361 }
Takashi Iwaic4614822006-06-23 14:38:23 +0200362 kfree(card);
363 return 0;
364}
365
366static int snd_card_free_prepare(struct snd_card *card)
367{
368 if (card == NULL)
369 return -EINVAL;
370 (void) snd_card_disconnect(card);
Takashi Iwai746df942006-05-15 19:49:05 +0200371 mutex_lock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200372 snd_cards[card->number] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 snd_cards_lock &= ~(1 << card->number);
Takashi Iwai746df942006-05-15 19:49:05 +0200374 mutex_unlock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200375#ifdef CONFIG_PM
376 wake_up(&card->power_sleep);
377#endif
378 return 0;
379}
380
381int snd_card_free_when_closed(struct snd_card *card)
382{
383 int free_now = 0;
384 int ret = snd_card_free_prepare(card);
385 if (ret)
386 return ret;
387
388 spin_lock(&card->files_lock);
389 if (card->files == NULL)
390 free_now = 1;
391 else
392 card->free_on_last_close = 1;
393 spin_unlock(&card->files_lock);
394
395 if (free_now)
396 snd_card_do_free(card);
397 return 0;
398}
399
400EXPORT_SYMBOL(snd_card_free_when_closed);
401
402int snd_card_free(struct snd_card *card)
403{
404 int ret = snd_card_free_prepare(card);
405 if (ret)
406 return ret;
407
408 /* wait, until all devices are ready for the free operation */
409 wait_event(card->shutdown_sleep, card->files == NULL);
410 snd_card_do_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
412}
413
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200414EXPORT_SYMBOL(snd_card_free);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static void snd_card_free_thread(void * __card)
417{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100418 struct snd_card *card = __card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct module * module = card->module;
420
421 if (!try_module_get(module)) {
422 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
423 module = NULL;
424 }
425
426 snd_card_free(card);
427
428 module_put(module);
429}
430
431/**
432 * snd_card_free_in_thread - call snd_card_free() in thread
433 * @card: soundcard structure
434 *
435 * This function schedules the call of snd_card_free() function in a
436 * work queue. When all devices are released (non-busy), the work
437 * is woken up and calls snd_card_free().
438 *
439 * When a card can be disconnected at any time by hotplug service,
440 * this function should be used in disconnect (or detach) callback
441 * instead of calling snd_card_free() directly.
442 *
443 * Returns - zero otherwise a negative error code if the start of thread failed.
444 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100445int snd_card_free_in_thread(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 if (card->files == NULL) {
448 snd_card_free(card);
449 return 0;
450 }
451
452 if (schedule_work(&card->free_workq))
453 return 0;
454
455 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
456 /* try to free the structure immediately */
457 snd_card_free(card);
458 return -EFAULT;
459}
460
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200461EXPORT_SYMBOL(snd_card_free_in_thread);
462
Takashi Iwai512bbd62005-11-17 13:51:18 +0100463static void choose_default_id(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Clemens Ladischd0015442005-11-20 14:09:05 +0100465 int i, len, idx_flag = 0, loops = SNDRV_CARDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 char *id, *spos;
467
468 id = spos = card->shortname;
469 while (*id != '\0') {
470 if (*id == ' ')
471 spos = id + 1;
472 id++;
473 }
474 id = card->id;
475 while (*spos != '\0' && !isalnum(*spos))
476 spos++;
477 if (isdigit(*spos))
478 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
479 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
480 if (isalnum(*spos))
481 *id++ = *spos;
482 spos++;
483 }
484 *id = '\0';
485
486 id = card->id;
487
488 if (*id == '\0')
489 strcpy(id, "default");
490
491 while (1) {
492 if (loops-- == 0) {
493 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
494 strcpy(card->id, card->proc_root->name);
495 return;
496 }
497 if (!snd_info_check_reserved_words(id))
498 goto __change;
499 for (i = 0; i < snd_ecards_limit; i++) {
500 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
501 goto __change;
502 }
503 break;
504
505 __change:
506 len = strlen(id);
Clemens Ladischd0015442005-11-20 14:09:05 +0100507 if (idx_flag) {
508 if (id[len-1] != '9')
509 id[len-1]++;
510 else
511 id[len-1] = 'A';
512 } else if ((size_t)len <= sizeof(card->id) - 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 strcat(id, "_1");
514 idx_flag++;
515 } else {
516 spos = id + len - 2;
517 if ((size_t)len <= sizeof(card->id) - 2)
518 spos++;
519 *spos++ = '_';
520 *spos++ = '1';
521 *spos++ = '\0';
522 idx_flag++;
523 }
524 }
525}
526
527/**
528 * snd_card_register - register the soundcard
529 * @card: soundcard structure
530 *
531 * This function registers all the devices assigned to the soundcard.
532 * Until calling this, the ALSA control interface is blocked from the
533 * external accesses. Thus, you should call this function at the end
534 * of the initialization of the card.
535 *
536 * Returns zero otherwise a negative error code if the registrain failed.
537 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100538int snd_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200542 snd_assert(card != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if ((err = snd_device_register_all(card)) < 0)
544 return err;
Takashi Iwai746df942006-05-15 19:49:05 +0200545 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (snd_cards[card->number]) {
547 /* already registered */
Takashi Iwai746df942006-05-15 19:49:05 +0200548 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 0;
550 }
551 if (card->id[0] == '\0')
552 choose_default_id(card);
553 snd_cards[card->number] = card;
Takashi Iwai746df942006-05-15 19:49:05 +0200554 mutex_unlock(&snd_card_mutex);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100555 init_info_for_card(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
557 if (snd_mixer_oss_notify_callback)
558 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
559#endif
560 return 0;
561}
562
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200563EXPORT_SYMBOL(snd_card_register);
564
Takashi Iwaie28563c2005-12-01 10:42:42 +0100565#ifdef CONFIG_PROC_FS
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200566static struct snd_info_entry *snd_card_info_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Takashi Iwaia381a7a2005-11-17 15:55:49 +0100568static void snd_card_info_read(struct snd_info_entry *entry,
569 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100572 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200575 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if ((card = snd_cards[idx]) != NULL) {
577 count++;
Clemens Ladischd0015442005-11-20 14:09:05 +0100578 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 idx,
580 card->id,
581 card->driver,
582 card->shortname);
Clemens Ladischd0015442005-11-20 14:09:05 +0100583 snd_iprintf(buffer, " %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 card->longname);
585 }
Takashi Iwai746df942006-05-15 19:49:05 +0200586 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588 if (!count)
589 snd_iprintf(buffer, "--- no soundcards ---\n");
590}
591
Takashi Iwaie28563c2005-12-01 10:42:42 +0100592#ifdef CONFIG_SND_OSSEMUL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Takashi Iwai512bbd62005-11-17 13:51:18 +0100594void snd_card_info_read_oss(struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100597 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200600 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if ((card = snd_cards[idx]) != NULL) {
602 count++;
603 snd_iprintf(buffer, "%s\n", card->longname);
604 }
Takashi Iwai746df942006-05-15 19:49:05 +0200605 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 if (!count) {
608 snd_iprintf(buffer, "--- no soundcards ---\n");
609 }
610}
611
612#endif
613
614#ifdef MODULE
Takashi Iwai512bbd62005-11-17 13:51:18 +0100615static struct snd_info_entry *snd_card_module_info_entry;
616static void snd_card_module_info_read(struct snd_info_entry *entry,
617 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 int idx;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100620 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 for (idx = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200623 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if ((card = snd_cards[idx]) != NULL)
Clemens Ladischd0015442005-11-20 14:09:05 +0100625 snd_iprintf(buffer, "%2i %s\n",
626 idx, card->module->name);
Takashi Iwai746df942006-05-15 19:49:05 +0200627 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629}
630#endif
631
632int __init snd_card_info_init(void)
633{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100634 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200637 if (! entry)
638 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 entry->c.text.read = snd_card_info_read;
640 if (snd_info_register(entry) < 0) {
641 snd_info_free_entry(entry);
642 return -ENOMEM;
643 }
644 snd_card_info_entry = entry;
645
646#ifdef MODULE
647 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
648 if (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 entry->c.text.read = snd_card_module_info_read;
650 if (snd_info_register(entry) < 0)
651 snd_info_free_entry(entry);
652 else
653 snd_card_module_info_entry = entry;
654 }
655#endif
656
657 return 0;
658}
659
660int __exit snd_card_info_done(void)
661{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200662 snd_info_free_entry(snd_card_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#ifdef MODULE
Takashi Iwai746d4a02006-06-23 14:37:59 +0200664 snd_info_free_entry(snd_card_module_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#endif
666 return 0;
667}
668
Takashi Iwaie28563c2005-12-01 10:42:42 +0100669#endif /* CONFIG_PROC_FS */
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/**
672 * snd_component_add - add a component string
673 * @card: soundcard structure
674 * @component: the component id string
675 *
676 * This function adds the component id string to the supported list.
677 * The component can be referred from the alsa-lib.
678 *
679 * Returns zero otherwise a negative error code.
680 */
681
Takashi Iwai512bbd62005-11-17 13:51:18 +0100682int snd_component_add(struct snd_card *card, const char *component)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 char *ptr;
685 int len = strlen(component);
686
687 ptr = strstr(card->components, component);
688 if (ptr != NULL) {
689 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
690 return 1;
691 }
692 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
693 snd_BUG();
694 return -ENOMEM;
695 }
696 if (card->components[0] != '\0')
697 strcat(card->components, " ");
698 strcat(card->components, component);
699 return 0;
700}
701
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200702EXPORT_SYMBOL(snd_component_add);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/**
705 * snd_card_file_add - add the file to the file list of the card
706 * @card: soundcard structure
707 * @file: file pointer
708 *
709 * This function adds the file to the file linked-list of the card.
710 * This linked-list is used to keep tracking the connection state,
711 * and to avoid the release of busy resources by hotplug.
712 *
713 * Returns zero or a negative error code.
714 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100715int snd_card_file_add(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct snd_monitor_file *mfile;
718
719 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
720 if (mfile == NULL)
721 return -ENOMEM;
722 mfile->file = file;
723 mfile->next = NULL;
724 spin_lock(&card->files_lock);
725 if (card->shutdown) {
726 spin_unlock(&card->files_lock);
727 kfree(mfile);
728 return -ENODEV;
729 }
730 mfile->next = card->files;
731 card->files = mfile;
732 spin_unlock(&card->files_lock);
733 return 0;
734}
735
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200736EXPORT_SYMBOL(snd_card_file_add);
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/**
739 * snd_card_file_remove - remove the file from the file list
740 * @card: soundcard structure
741 * @file: file pointer
742 *
743 * This function removes the file formerly added to the card via
744 * snd_card_file_add() function.
745 * If all files are removed and the release of the card is
746 * scheduled, it will wake up the the thread to call snd_card_free()
747 * (see snd_card_free_in_thread() function).
748 *
749 * Returns zero or a negative error code.
750 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100751int snd_card_file_remove(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct snd_monitor_file *mfile, *pfile = NULL;
Takashi Iwaic4614822006-06-23 14:38:23 +0200754 int last_close = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 spin_lock(&card->files_lock);
757 mfile = card->files;
758 while (mfile) {
759 if (mfile->file == file) {
760 if (pfile)
761 pfile->next = mfile->next;
762 else
763 card->files = mfile->next;
764 break;
765 }
766 pfile = mfile;
767 mfile = mfile->next;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (card->files == NULL)
Takashi Iwaic4614822006-06-23 14:38:23 +0200770 last_close = 1;
771 spin_unlock(&card->files_lock);
772 if (last_close) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 wake_up(&card->shutdown_sleep);
Takashi Iwaic4614822006-06-23 14:38:23 +0200774 if (card->free_on_last_close)
775 snd_card_do_free(card);
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!mfile) {
778 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
779 return -ENOENT;
780 }
781 kfree(mfile);
782 return 0;
783}
784
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200785EXPORT_SYMBOL(snd_card_file_remove);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#ifdef CONFIG_PM
788/**
789 * snd_power_wait - wait until the power-state is changed.
790 * @card: soundcard structure
791 * @power_state: expected power state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 *
793 * Waits until the power-state is changed.
794 *
795 * Note: the power lock must be active before call.
796 */
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200797int snd_power_wait(struct snd_card *card, unsigned int power_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 wait_queue_t wait;
800 int result = 0;
801
802 /* fastpath */
803 if (snd_power_get_state(card) == power_state)
804 return 0;
805 init_waitqueue_entry(&wait, current);
806 add_wait_queue(&card->power_sleep, &wait);
807 while (1) {
808 if (card->shutdown) {
809 result = -ENODEV;
810 break;
811 }
812 if (snd_power_get_state(card) == power_state)
813 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 set_current_state(TASK_UNINTERRUPTIBLE);
815 snd_power_unlock(card);
816 schedule_timeout(30 * HZ);
817 snd_power_lock(card);
818 }
819 remove_wait_queue(&card->power_sleep, &wait);
820 return result;
821}
822
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200823EXPORT_SYMBOL(snd_power_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824#endif /* CONFIG_PM */