blob: 4a431e3ea3a2c7f8bb69bbd2d347cd419883d283 [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
Karsten Wiesea9edfc62006-10-06 16:08:27 +020036static DEFINE_SPINLOCK(shutdown_lock);
37static LIST_HEAD(shutdown_files);
38
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -080039static const struct file_operations snd_shutdown_f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
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 -070084/**
85 * snd_card_new - create and initialize a soundcard structure
86 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
87 * @xid: card identification (ASCII string)
88 * @module: top level module for locking
89 * @extra_size: allocate this extra size after the main soundcard structure
90 *
91 * Creates and initializes a soundcard structure.
92 *
Takashi Iwai512bbd62005-11-17 13:51:18 +010093 * Returns kmallocated snd_card structure. Creates the ALSA control interface
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * (which is blocked until snd_card_register function is called).
95 */
Takashi Iwai512bbd62005-11-17 13:51:18 +010096struct snd_card *snd_card_new(int idx, const char *xid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct module *module, int extra_size)
98{
Takashi Iwai512bbd62005-11-17 13:51:18 +010099 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 int err;
101
102 if (extra_size < 0)
103 extra_size = 0;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200104 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (card == NULL)
106 return NULL;
107 if (xid) {
108 if (!snd_info_check_reserved_words(xid))
109 goto __error;
110 strlcpy(card->id, xid, sizeof(card->id));
111 }
112 err = 0;
Takashi Iwai746df942006-05-15 19:49:05 +0200113 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (idx < 0) {
115 int idx2;
116 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
Oliver Neukum5c33dd72007-01-16 17:49:21 +0100117 /* idx == -1 == 0xffff means: take any free slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (~snd_cards_lock & idx & 1<<idx2) {
119 idx = idx2;
120 if (idx >= snd_ecards_limit)
121 snd_ecards_limit = idx + 1;
122 break;
123 }
Oliver Neukum5c33dd72007-01-16 17:49:21 +0100124 } else {
125 if (idx < snd_ecards_limit) {
126 if (snd_cards_lock & (1 << idx))
127 err = -EBUSY; /* invalid */
128 } else {
129 if (idx < SNDRV_CARDS)
130 snd_ecards_limit = idx + 1; /* increase the limit */
131 else
132 err = -ENODEV;
133 }
134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (idx < 0 || err < 0) {
Takashi Iwai746df942006-05-15 19:49:05 +0200136 mutex_unlock(&snd_card_mutex);
Oliver Neukum5c33dd72007-01-16 17:49:21 +0100137 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i), error: %d\n",
138 idx, snd_ecards_limit - 1, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 goto __error;
140 }
141 snd_cards_lock |= 1 << idx; /* lock it */
Takashi Iwai746df942006-05-15 19:49:05 +0200142 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 card->number = idx;
144 card->module = module;
145 INIT_LIST_HEAD(&card->devices);
146 init_rwsem(&card->controls_rwsem);
147 rwlock_init(&card->ctl_files_rwlock);
148 INIT_LIST_HEAD(&card->controls);
149 INIT_LIST_HEAD(&card->ctl_files);
150 spin_lock_init(&card->files_lock);
151 init_waitqueue_head(&card->shutdown_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#ifdef CONFIG_PM
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100153 mutex_init(&card->power_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 init_waitqueue_head(&card->power_sleep);
155#endif
156 /* the control interface cannot be accessed from the user space until */
157 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
158 if ((err = snd_ctl_create(card)) < 0) {
159 snd_printd("unable to register control minors\n");
160 goto __error;
161 }
162 if ((err = snd_info_card_create(card)) < 0) {
163 snd_printd("unable to create card info\n");
164 goto __error_ctl;
165 }
166 if (extra_size > 0)
Takashi Iwai512bbd62005-11-17 13:51:18 +0100167 card->private_data = (char *)card + sizeof(struct snd_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return card;
169
170 __error_ctl:
171 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
172 __error:
173 kfree(card);
174 return NULL;
175}
176
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200177EXPORT_SYMBOL(snd_card_new);
178
Takashi Iwai746df942006-05-15 19:49:05 +0200179/* return non-zero if a card is already locked */
180int snd_card_locked(int card)
181{
182 int locked;
183
184 mutex_lock(&snd_card_mutex);
185 locked = snd_cards_lock & (1 << card);
186 mutex_unlock(&snd_card_mutex);
187 return locked;
188}
189
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100190static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
191{
192 return -ENODEV;
193}
194
195static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
196 size_t count, loff_t *offset)
197{
198 return -ENODEV;
199}
200
201static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
202 size_t count, loff_t *offset)
203{
204 return -ENODEV;
205}
206
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200207static int snd_disconnect_release(struct inode *inode, struct file *file)
208{
209 struct snd_monitor_file *df = NULL, *_df;
210
211 spin_lock(&shutdown_lock);
212 list_for_each_entry(_df, &shutdown_files, shutdown_list) {
213 if (_df->file == file) {
214 df = _df;
215 break;
216 }
217 }
218 spin_unlock(&shutdown_lock);
219
220 if (likely(df))
221 return df->disconnected_f_op->release(inode, file);
222
223 panic("%s(%p, %p) failed!", __FUNCTION__, inode, file);
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
227{
228 return POLLERR | POLLNVAL;
229}
230
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100231static long snd_disconnect_ioctl(struct file *file,
232 unsigned int cmd, unsigned long arg)
233{
234 return -ENODEV;
235}
236
237static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
238{
239 return -ENODEV;
240}
241
242static int snd_disconnect_fasync(int fd, struct file *file, int on)
243{
244 return -ENODEV;
245}
246
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800247static const struct file_operations snd_shutdown_f_ops =
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200248{
249 .owner = THIS_MODULE,
250 .llseek = snd_disconnect_llseek,
251 .read = snd_disconnect_read,
252 .write = snd_disconnect_write,
253 .release = snd_disconnect_release,
254 .poll = snd_disconnect_poll,
255 .unlocked_ioctl = snd_disconnect_ioctl,
256#ifdef CONFIG_COMPAT
257 .compat_ioctl = snd_disconnect_ioctl,
258#endif
259 .mmap = snd_disconnect_mmap,
260 .fasync = snd_disconnect_fasync
261};
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/**
264 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
265 * @card: soundcard structure
266 *
267 * Disconnects all APIs from the file-operations (user space).
268 *
269 * Returns zero, otherwise a negative error code.
270 *
271 * Note: The current implementation replaces all active file->f_op with special
272 * dummy file operations (they do nothing except release).
273 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100274int snd_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct snd_monitor_file *mfile;
277 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int err;
279
280 spin_lock(&card->files_lock);
281 if (card->shutdown) {
282 spin_unlock(&card->files_lock);
283 return 0;
284 }
285 card->shutdown = 1;
286 spin_unlock(&card->files_lock);
287
288 /* phase 1: disable fops (user space) operations for ALSA API */
Takashi Iwai746df942006-05-15 19:49:05 +0200289 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 snd_cards[card->number] = NULL;
Takashi Iwai746df942006-05-15 19:49:05 +0200291 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /* phase 2: replace file->f_op with special dummy operations */
294
295 spin_lock(&card->files_lock);
296 mfile = card->files;
297 while (mfile) {
298 file = mfile->file;
299
300 /* it's critical part, use endless loop */
301 /* we have no room to fail */
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200302 mfile->disconnected_f_op = mfile->file->f_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200304 spin_lock(&shutdown_lock);
305 list_add(&mfile->shutdown_list, &shutdown_files);
306 spin_unlock(&shutdown_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200308 fops_get(&snd_shutdown_f_ops);
309 mfile->file->f_op = &snd_shutdown_f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 mfile = mfile->next;
312 }
313 spin_unlock(&card->files_lock);
314
315 /* phase 3: notify all connected devices about disconnection */
316 /* at this point, they cannot respond to any calls except release() */
317
318#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
319 if (snd_mixer_oss_notify_callback)
320 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
321#endif
322
323 /* notify all devices that we are disconnected */
324 err = snd_device_disconnect_all(card);
325 if (err < 0)
326 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
327
Takashi Iwai746d4a02006-06-23 14:37:59 +0200328 snd_info_card_disconnect(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330}
331
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200332EXPORT_SYMBOL(snd_card_disconnect);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/**
335 * snd_card_free - frees given soundcard structure
336 * @card: soundcard structure
337 *
338 * This function releases the soundcard structure and the all assigned
339 * devices automatically. That is, you don't have to release the devices
340 * by yourself.
341 *
342 * Returns zero. Frees all associated devices and frees the control
343 * interface associated to given soundcard.
344 */
Takashi Iwaic4614822006-06-23 14:38:23 +0200345static int snd_card_do_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
348 if (snd_mixer_oss_notify_callback)
349 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
350#endif
351 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
352 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
353 /* Fatal, but this situation should never occur */
354 }
355 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
356 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
357 /* Fatal, but this situation should never occur */
358 }
359 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
360 snd_printk(KERN_ERR "unable to free all devices (post)\n");
361 /* Fatal, but this situation should never occur */
362 }
363 if (card->private_free)
364 card->private_free(card);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200365 snd_info_free_entry(card->proc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (snd_info_card_free(card) < 0) {
367 snd_printk(KERN_WARNING "unable to free card info\n");
368 /* Not fatal error */
369 }
Takashi Iwai7d2aae12007-01-26 12:40:31 +0100370#ifndef CONFIG_SYSFS_DEPRECATED
371 if (card->card_dev)
372 device_unregister(card->card_dev);
373#endif
Takashi Iwaic4614822006-06-23 14:38:23 +0200374 kfree(card);
375 return 0;
376}
377
378static int snd_card_free_prepare(struct snd_card *card)
379{
380 if (card == NULL)
381 return -EINVAL;
382 (void) snd_card_disconnect(card);
Takashi Iwai746df942006-05-15 19:49:05 +0200383 mutex_lock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200384 snd_cards[card->number] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 snd_cards_lock &= ~(1 << card->number);
Takashi Iwai746df942006-05-15 19:49:05 +0200386 mutex_unlock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200387#ifdef CONFIG_PM
388 wake_up(&card->power_sleep);
389#endif
390 return 0;
391}
392
393int snd_card_free_when_closed(struct snd_card *card)
394{
395 int free_now = 0;
396 int ret = snd_card_free_prepare(card);
397 if (ret)
398 return ret;
399
400 spin_lock(&card->files_lock);
401 if (card->files == NULL)
402 free_now = 1;
403 else
404 card->free_on_last_close = 1;
405 spin_unlock(&card->files_lock);
406
407 if (free_now)
408 snd_card_do_free(card);
409 return 0;
410}
411
412EXPORT_SYMBOL(snd_card_free_when_closed);
413
414int snd_card_free(struct snd_card *card)
415{
416 int ret = snd_card_free_prepare(card);
417 if (ret)
418 return ret;
419
420 /* wait, until all devices are ready for the free operation */
421 wait_event(card->shutdown_sleep, card->files == NULL);
422 snd_card_do_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424}
425
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200426EXPORT_SYMBOL(snd_card_free);
427
Takashi Iwai512bbd62005-11-17 13:51:18 +0100428static void choose_default_id(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Clemens Ladischd0015442005-11-20 14:09:05 +0100430 int i, len, idx_flag = 0, loops = SNDRV_CARDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 char *id, *spos;
432
433 id = spos = card->shortname;
434 while (*id != '\0') {
435 if (*id == ' ')
436 spos = id + 1;
437 id++;
438 }
439 id = card->id;
440 while (*spos != '\0' && !isalnum(*spos))
441 spos++;
442 if (isdigit(*spos))
443 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
444 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
445 if (isalnum(*spos))
446 *id++ = *spos;
447 spos++;
448 }
449 *id = '\0';
450
451 id = card->id;
452
453 if (*id == '\0')
454 strcpy(id, "default");
455
456 while (1) {
457 if (loops-- == 0) {
458 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
459 strcpy(card->id, card->proc_root->name);
460 return;
461 }
462 if (!snd_info_check_reserved_words(id))
463 goto __change;
464 for (i = 0; i < snd_ecards_limit; i++) {
465 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
466 goto __change;
467 }
468 break;
469
470 __change:
471 len = strlen(id);
Clemens Ladischd0015442005-11-20 14:09:05 +0100472 if (idx_flag) {
473 if (id[len-1] != '9')
474 id[len-1]++;
475 else
476 id[len-1] = 'A';
477 } else if ((size_t)len <= sizeof(card->id) - 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 strcat(id, "_1");
479 idx_flag++;
480 } else {
481 spos = id + len - 2;
482 if ((size_t)len <= sizeof(card->id) - 2)
483 spos++;
484 *spos++ = '_';
485 *spos++ = '1';
486 *spos++ = '\0';
487 idx_flag++;
488 }
489 }
490}
491
492/**
493 * snd_card_register - register the soundcard
494 * @card: soundcard structure
495 *
496 * This function registers all the devices assigned to the soundcard.
497 * Until calling this, the ALSA control interface is blocked from the
498 * external accesses. Thus, you should call this function at the end
499 * of the initialization of the card.
500 *
501 * Returns zero otherwise a negative error code if the registrain failed.
502 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100503int snd_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200507 snd_assert(card != NULL, return -EINVAL);
Takashi Iwai7d2aae12007-01-26 12:40:31 +0100508#ifndef CONFIG_SYSFS_DEPRECATED
509 if (!card->card_dev) {
510 card->card_dev = device_create(sound_class, card->dev, 0,
511 "card%i", card->number);
512 if (IS_ERR(card->card_dev))
513 card->card_dev = NULL;
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700514 }
Takashi Iwai7d2aae12007-01-26 12:40:31 +0100515#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if ((err = snd_device_register_all(card)) < 0)
517 return err;
Takashi Iwai746df942006-05-15 19:49:05 +0200518 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (snd_cards[card->number]) {
520 /* already registered */
Takashi Iwai746df942006-05-15 19:49:05 +0200521 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 0;
523 }
524 if (card->id[0] == '\0')
525 choose_default_id(card);
526 snd_cards[card->number] = card;
Takashi Iwai746df942006-05-15 19:49:05 +0200527 mutex_unlock(&snd_card_mutex);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100528 init_info_for_card(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
530 if (snd_mixer_oss_notify_callback)
531 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
532#endif
533 return 0;
534}
535
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200536EXPORT_SYMBOL(snd_card_register);
537
Takashi Iwaie28563c2005-12-01 10:42:42 +0100538#ifdef CONFIG_PROC_FS
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200539static struct snd_info_entry *snd_card_info_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Takashi Iwaia381a7a2005-11-17 15:55:49 +0100541static void snd_card_info_read(struct snd_info_entry *entry,
542 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100545 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200548 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if ((card = snd_cards[idx]) != NULL) {
550 count++;
Clemens Ladischd0015442005-11-20 14:09:05 +0100551 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 idx,
553 card->id,
554 card->driver,
555 card->shortname);
Clemens Ladischd0015442005-11-20 14:09:05 +0100556 snd_iprintf(buffer, " %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 card->longname);
558 }
Takashi Iwai746df942006-05-15 19:49:05 +0200559 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 if (!count)
562 snd_iprintf(buffer, "--- no soundcards ---\n");
563}
564
Takashi Iwaie28563c2005-12-01 10:42:42 +0100565#ifdef CONFIG_SND_OSSEMUL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Takashi Iwai512bbd62005-11-17 13:51:18 +0100567void snd_card_info_read_oss(struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100570 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200573 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if ((card = snd_cards[idx]) != NULL) {
575 count++;
576 snd_iprintf(buffer, "%s\n", card->longname);
577 }
Takashi Iwai746df942006-05-15 19:49:05 +0200578 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 if (!count) {
581 snd_iprintf(buffer, "--- no soundcards ---\n");
582 }
583}
584
585#endif
586
587#ifdef MODULE
Takashi Iwai512bbd62005-11-17 13:51:18 +0100588static struct snd_info_entry *snd_card_module_info_entry;
589static void snd_card_module_info_read(struct snd_info_entry *entry,
590 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 int idx;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100593 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 for (idx = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200596 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if ((card = snd_cards[idx]) != NULL)
Clemens Ladischd0015442005-11-20 14:09:05 +0100598 snd_iprintf(buffer, "%2i %s\n",
599 idx, card->module->name);
Takashi Iwai746df942006-05-15 19:49:05 +0200600 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602}
603#endif
604
605int __init snd_card_info_init(void)
606{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100607 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200610 if (! entry)
611 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 entry->c.text.read = snd_card_info_read;
613 if (snd_info_register(entry) < 0) {
614 snd_info_free_entry(entry);
615 return -ENOMEM;
616 }
617 snd_card_info_entry = entry;
618
619#ifdef MODULE
620 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
621 if (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 entry->c.text.read = snd_card_module_info_read;
623 if (snd_info_register(entry) < 0)
624 snd_info_free_entry(entry);
625 else
626 snd_card_module_info_entry = entry;
627 }
628#endif
629
630 return 0;
631}
632
633int __exit snd_card_info_done(void)
634{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200635 snd_info_free_entry(snd_card_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636#ifdef MODULE
Takashi Iwai746d4a02006-06-23 14:37:59 +0200637 snd_info_free_entry(snd_card_module_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638#endif
639 return 0;
640}
641
Takashi Iwaie28563c2005-12-01 10:42:42 +0100642#endif /* CONFIG_PROC_FS */
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/**
645 * snd_component_add - add a component string
646 * @card: soundcard structure
647 * @component: the component id string
648 *
649 * This function adds the component id string to the supported list.
650 * The component can be referred from the alsa-lib.
651 *
652 * Returns zero otherwise a negative error code.
653 */
654
Takashi Iwai512bbd62005-11-17 13:51:18 +0100655int snd_component_add(struct snd_card *card, const char *component)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 char *ptr;
658 int len = strlen(component);
659
660 ptr = strstr(card->components, component);
661 if (ptr != NULL) {
662 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
663 return 1;
664 }
665 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
666 snd_BUG();
667 return -ENOMEM;
668 }
669 if (card->components[0] != '\0')
670 strcat(card->components, " ");
671 strcat(card->components, component);
672 return 0;
673}
674
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200675EXPORT_SYMBOL(snd_component_add);
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/**
678 * snd_card_file_add - add the file to the file list of the card
679 * @card: soundcard structure
680 * @file: file pointer
681 *
682 * This function adds the file to the file linked-list of the card.
683 * This linked-list is used to keep tracking the connection state,
684 * and to avoid the release of busy resources by hotplug.
685 *
686 * Returns zero or a negative error code.
687 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100688int snd_card_file_add(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct snd_monitor_file *mfile;
691
692 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
693 if (mfile == NULL)
694 return -ENOMEM;
695 mfile->file = file;
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200696 mfile->disconnected_f_op = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 mfile->next = NULL;
698 spin_lock(&card->files_lock);
699 if (card->shutdown) {
700 spin_unlock(&card->files_lock);
701 kfree(mfile);
702 return -ENODEV;
703 }
704 mfile->next = card->files;
705 card->files = mfile;
706 spin_unlock(&card->files_lock);
707 return 0;
708}
709
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200710EXPORT_SYMBOL(snd_card_file_add);
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/**
713 * snd_card_file_remove - remove the file from the file list
714 * @card: soundcard structure
715 * @file: file pointer
716 *
717 * This function removes the file formerly added to the card via
718 * snd_card_file_add() function.
Takashi Iwai2b29b132006-06-23 14:38:26 +0200719 * If all files are removed and snd_card_free_when_closed() was
720 * called beforehand, it processes the pending release of
721 * resources.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 *
723 * Returns zero or a negative error code.
724 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100725int snd_card_file_remove(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 struct snd_monitor_file *mfile, *pfile = NULL;
Takashi Iwaic4614822006-06-23 14:38:23 +0200728 int last_close = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 spin_lock(&card->files_lock);
731 mfile = card->files;
732 while (mfile) {
733 if (mfile->file == file) {
734 if (pfile)
735 pfile->next = mfile->next;
736 else
737 card->files = mfile->next;
738 break;
739 }
740 pfile = mfile;
741 mfile = mfile->next;
742 }
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200743 if (mfile && mfile->disconnected_f_op) {
744 fops_put(mfile->disconnected_f_op);
745 spin_lock(&shutdown_lock);
746 list_del(&mfile->shutdown_list);
747 spin_unlock(&shutdown_lock);
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (card->files == NULL)
Takashi Iwaic4614822006-06-23 14:38:23 +0200750 last_close = 1;
751 spin_unlock(&card->files_lock);
752 if (last_close) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 wake_up(&card->shutdown_sleep);
Takashi Iwaic4614822006-06-23 14:38:23 +0200754 if (card->free_on_last_close)
755 snd_card_do_free(card);
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!mfile) {
758 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
759 return -ENOENT;
760 }
761 kfree(mfile);
762 return 0;
763}
764
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200765EXPORT_SYMBOL(snd_card_file_remove);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#ifdef CONFIG_PM
768/**
769 * snd_power_wait - wait until the power-state is changed.
770 * @card: soundcard structure
771 * @power_state: expected power state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 *
773 * Waits until the power-state is changed.
774 *
775 * Note: the power lock must be active before call.
776 */
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200777int snd_power_wait(struct snd_card *card, unsigned int power_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 wait_queue_t wait;
780 int result = 0;
781
782 /* fastpath */
783 if (snd_power_get_state(card) == power_state)
784 return 0;
785 init_waitqueue_entry(&wait, current);
786 add_wait_queue(&card->power_sleep, &wait);
787 while (1) {
788 if (card->shutdown) {
789 result = -ENODEV;
790 break;
791 }
792 if (snd_power_get_state(card) == power_state)
793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 set_current_state(TASK_UNINTERRUPTIBLE);
795 snd_power_unlock(card);
796 schedule_timeout(30 * HZ);
797 snd_power_lock(card);
798 }
799 remove_wait_queue(&card->power_sleep, &wait);
800 return result;
801}
802
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200803EXPORT_SYMBOL(snd_power_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804#endif /* CONFIG_PM */