blob: 6152a7554dfd2dd32492897befd1bb8ed502cd95 [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
39static 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++)
117 if (~snd_cards_lock & idx & 1<<idx2) {
118 idx = idx2;
119 if (idx >= snd_ecards_limit)
120 snd_ecards_limit = idx + 1;
121 break;
122 }
123 } else if (idx < snd_ecards_limit) {
124 if (snd_cards_lock & (1 << idx))
125 err = -ENODEV; /* invalid */
126 } else if (idx < SNDRV_CARDS)
127 snd_ecards_limit = idx + 1; /* increase the limit */
128 else
129 err = -ENODEV;
130 if (idx < 0 || err < 0) {
Takashi Iwai746df942006-05-15 19:49:05 +0200131 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
133 goto __error;
134 }
135 snd_cards_lock |= 1 << idx; /* lock it */
Takashi Iwai746df942006-05-15 19:49:05 +0200136 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 card->number = idx;
138 card->module = module;
139 INIT_LIST_HEAD(&card->devices);
140 init_rwsem(&card->controls_rwsem);
141 rwlock_init(&card->ctl_files_rwlock);
142 INIT_LIST_HEAD(&card->controls);
143 INIT_LIST_HEAD(&card->ctl_files);
144 spin_lock_init(&card->files_lock);
145 init_waitqueue_head(&card->shutdown_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#ifdef CONFIG_PM
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100147 mutex_init(&card->power_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 init_waitqueue_head(&card->power_sleep);
149#endif
150 /* the control interface cannot be accessed from the user space until */
151 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
152 if ((err = snd_ctl_create(card)) < 0) {
153 snd_printd("unable to register control minors\n");
154 goto __error;
155 }
156 if ((err = snd_info_card_create(card)) < 0) {
157 snd_printd("unable to create card info\n");
158 goto __error_ctl;
159 }
160 if (extra_size > 0)
Takashi Iwai512bbd62005-11-17 13:51:18 +0100161 card->private_data = (char *)card + sizeof(struct snd_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return card;
163
164 __error_ctl:
165 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
166 __error:
167 kfree(card);
168 return NULL;
169}
170
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200171EXPORT_SYMBOL(snd_card_new);
172
Takashi Iwai746df942006-05-15 19:49:05 +0200173/* return non-zero if a card is already locked */
174int snd_card_locked(int card)
175{
176 int locked;
177
178 mutex_lock(&snd_card_mutex);
179 locked = snd_cards_lock & (1 << card);
180 mutex_unlock(&snd_card_mutex);
181 return locked;
182}
183
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100184static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
185{
186 return -ENODEV;
187}
188
189static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
190 size_t count, loff_t *offset)
191{
192 return -ENODEV;
193}
194
195static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
196 size_t count, loff_t *offset)
197{
198 return -ENODEV;
199}
200
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200201static int snd_disconnect_release(struct inode *inode, struct file *file)
202{
203 struct snd_monitor_file *df = NULL, *_df;
204
205 spin_lock(&shutdown_lock);
206 list_for_each_entry(_df, &shutdown_files, shutdown_list) {
207 if (_df->file == file) {
208 df = _df;
209 break;
210 }
211 }
212 spin_unlock(&shutdown_lock);
213
214 if (likely(df))
215 return df->disconnected_f_op->release(inode, file);
216
217 panic("%s(%p, %p) failed!", __FUNCTION__, inode, file);
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
221{
222 return POLLERR | POLLNVAL;
223}
224
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100225static long snd_disconnect_ioctl(struct file *file,
226 unsigned int cmd, unsigned long arg)
227{
228 return -ENODEV;
229}
230
231static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
232{
233 return -ENODEV;
234}
235
236static int snd_disconnect_fasync(int fd, struct file *file, int on)
237{
238 return -ENODEV;
239}
240
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200241static struct file_operations snd_shutdown_f_ops =
242{
243 .owner = THIS_MODULE,
244 .llseek = snd_disconnect_llseek,
245 .read = snd_disconnect_read,
246 .write = snd_disconnect_write,
247 .release = snd_disconnect_release,
248 .poll = snd_disconnect_poll,
249 .unlocked_ioctl = snd_disconnect_ioctl,
250#ifdef CONFIG_COMPAT
251 .compat_ioctl = snd_disconnect_ioctl,
252#endif
253 .mmap = snd_disconnect_mmap,
254 .fasync = snd_disconnect_fasync
255};
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/**
258 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
259 * @card: soundcard structure
260 *
261 * Disconnects all APIs from the file-operations (user space).
262 *
263 * Returns zero, otherwise a negative error code.
264 *
265 * Note: The current implementation replaces all active file->f_op with special
266 * dummy file operations (they do nothing except release).
267 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100268int snd_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct snd_monitor_file *mfile;
271 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int err;
273
274 spin_lock(&card->files_lock);
275 if (card->shutdown) {
276 spin_unlock(&card->files_lock);
277 return 0;
278 }
279 card->shutdown = 1;
280 spin_unlock(&card->files_lock);
281
282 /* phase 1: disable fops (user space) operations for ALSA API */
Takashi Iwai746df942006-05-15 19:49:05 +0200283 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 snd_cards[card->number] = NULL;
Takashi Iwai746df942006-05-15 19:49:05 +0200285 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* phase 2: replace file->f_op with special dummy operations */
288
289 spin_lock(&card->files_lock);
290 mfile = card->files;
291 while (mfile) {
292 file = mfile->file;
293
294 /* it's critical part, use endless loop */
295 /* we have no room to fail */
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200296 mfile->disconnected_f_op = mfile->file->f_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200298 spin_lock(&shutdown_lock);
299 list_add(&mfile->shutdown_list, &shutdown_files);
300 spin_unlock(&shutdown_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200302 fops_get(&snd_shutdown_f_ops);
303 mfile->file->f_op = &snd_shutdown_f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 mfile = mfile->next;
306 }
307 spin_unlock(&card->files_lock);
308
309 /* phase 3: notify all connected devices about disconnection */
310 /* at this point, they cannot respond to any calls except release() */
311
312#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
313 if (snd_mixer_oss_notify_callback)
314 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
315#endif
316
317 /* notify all devices that we are disconnected */
318 err = snd_device_disconnect_all(card);
319 if (err < 0)
320 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
321
Takashi Iwai746d4a02006-06-23 14:37:59 +0200322 snd_info_card_disconnect(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
324}
325
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200326EXPORT_SYMBOL(snd_card_disconnect);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/**
329 * snd_card_free - frees given soundcard structure
330 * @card: soundcard structure
331 *
332 * This function releases the soundcard structure and the all assigned
333 * devices automatically. That is, you don't have to release the devices
334 * by yourself.
335 *
336 * Returns zero. Frees all associated devices and frees the control
337 * interface associated to given soundcard.
338 */
Takashi Iwaic4614822006-06-23 14:38:23 +0200339static int snd_card_do_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
342 if (snd_mixer_oss_notify_callback)
343 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
344#endif
345 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
346 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
347 /* Fatal, but this situation should never occur */
348 }
349 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
350 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
351 /* Fatal, but this situation should never occur */
352 }
353 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
354 snd_printk(KERN_ERR "unable to free all devices (post)\n");
355 /* Fatal, but this situation should never occur */
356 }
357 if (card->private_free)
358 card->private_free(card);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200359 snd_info_free_entry(card->proc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (snd_info_card_free(card) < 0) {
361 snd_printk(KERN_WARNING "unable to free card info\n");
362 /* Not fatal error */
363 }
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700364 if (card->dev)
365 device_unregister(card->dev);
Takashi Iwaic4614822006-06-23 14:38:23 +0200366 kfree(card);
367 return 0;
368}
369
370static int snd_card_free_prepare(struct snd_card *card)
371{
372 if (card == NULL)
373 return -EINVAL;
374 (void) snd_card_disconnect(card);
Takashi Iwai746df942006-05-15 19:49:05 +0200375 mutex_lock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200376 snd_cards[card->number] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 snd_cards_lock &= ~(1 << card->number);
Takashi Iwai746df942006-05-15 19:49:05 +0200378 mutex_unlock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200379#ifdef CONFIG_PM
380 wake_up(&card->power_sleep);
381#endif
382 return 0;
383}
384
385int snd_card_free_when_closed(struct snd_card *card)
386{
387 int free_now = 0;
388 int ret = snd_card_free_prepare(card);
389 if (ret)
390 return ret;
391
392 spin_lock(&card->files_lock);
393 if (card->files == NULL)
394 free_now = 1;
395 else
396 card->free_on_last_close = 1;
397 spin_unlock(&card->files_lock);
398
399 if (free_now)
400 snd_card_do_free(card);
401 return 0;
402}
403
404EXPORT_SYMBOL(snd_card_free_when_closed);
405
406int snd_card_free(struct snd_card *card)
407{
408 int ret = snd_card_free_prepare(card);
409 if (ret)
410 return ret;
411
412 /* wait, until all devices are ready for the free operation */
413 wait_event(card->shutdown_sleep, card->files == NULL);
414 snd_card_do_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
416}
417
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200418EXPORT_SYMBOL(snd_card_free);
419
Takashi Iwai512bbd62005-11-17 13:51:18 +0100420static void choose_default_id(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Clemens Ladischd0015442005-11-20 14:09:05 +0100422 int i, len, idx_flag = 0, loops = SNDRV_CARDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 char *id, *spos;
424
425 id = spos = card->shortname;
426 while (*id != '\0') {
427 if (*id == ' ')
428 spos = id + 1;
429 id++;
430 }
431 id = card->id;
432 while (*spos != '\0' && !isalnum(*spos))
433 spos++;
434 if (isdigit(*spos))
435 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
436 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
437 if (isalnum(*spos))
438 *id++ = *spos;
439 spos++;
440 }
441 *id = '\0';
442
443 id = card->id;
444
445 if (*id == '\0')
446 strcpy(id, "default");
447
448 while (1) {
449 if (loops-- == 0) {
450 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
451 strcpy(card->id, card->proc_root->name);
452 return;
453 }
454 if (!snd_info_check_reserved_words(id))
455 goto __change;
456 for (i = 0; i < snd_ecards_limit; i++) {
457 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
458 goto __change;
459 }
460 break;
461
462 __change:
463 len = strlen(id);
Clemens Ladischd0015442005-11-20 14:09:05 +0100464 if (idx_flag) {
465 if (id[len-1] != '9')
466 id[len-1]++;
467 else
468 id[len-1] = 'A';
469 } else if ((size_t)len <= sizeof(card->id) - 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 strcat(id, "_1");
471 idx_flag++;
472 } else {
473 spos = id + len - 2;
474 if ((size_t)len <= sizeof(card->id) - 2)
475 spos++;
476 *spos++ = '_';
477 *spos++ = '1';
478 *spos++ = '\0';
479 idx_flag++;
480 }
481 }
482}
483
484/**
485 * snd_card_register - register the soundcard
486 * @card: soundcard structure
487 *
488 * This function registers all the devices assigned to the soundcard.
489 * Until calling this, the ALSA control interface is blocked from the
490 * external accesses. Thus, you should call this function at the end
491 * of the initialization of the card.
492 *
493 * Returns zero otherwise a negative error code if the registrain failed.
494 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100495int snd_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200499 snd_assert(card != NULL, return -EINVAL);
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700500 if (!card->dev) {
501 card->dev = device_create(sound_class, card->parent, 0,
502 "card%i", card->number);
503 if (IS_ERR(card->dev))
504 card->dev = NULL;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if ((err = snd_device_register_all(card)) < 0)
507 return err;
Takashi Iwai746df942006-05-15 19:49:05 +0200508 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (snd_cards[card->number]) {
510 /* already registered */
Takashi Iwai746df942006-05-15 19:49:05 +0200511 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 0;
513 }
514 if (card->id[0] == '\0')
515 choose_default_id(card);
516 snd_cards[card->number] = card;
Takashi Iwai746df942006-05-15 19:49:05 +0200517 mutex_unlock(&snd_card_mutex);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100518 init_info_for_card(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
520 if (snd_mixer_oss_notify_callback)
521 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
522#endif
523 return 0;
524}
525
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200526EXPORT_SYMBOL(snd_card_register);
527
Takashi Iwaie28563c2005-12-01 10:42:42 +0100528#ifdef CONFIG_PROC_FS
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200529static struct snd_info_entry *snd_card_info_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Takashi Iwaia381a7a2005-11-17 15:55:49 +0100531static void snd_card_info_read(struct snd_info_entry *entry,
532 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100535 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200538 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if ((card = snd_cards[idx]) != NULL) {
540 count++;
Clemens Ladischd0015442005-11-20 14:09:05 +0100541 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 idx,
543 card->id,
544 card->driver,
545 card->shortname);
Clemens Ladischd0015442005-11-20 14:09:05 +0100546 snd_iprintf(buffer, " %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 card->longname);
548 }
Takashi Iwai746df942006-05-15 19:49:05 +0200549 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 if (!count)
552 snd_iprintf(buffer, "--- no soundcards ---\n");
553}
554
Takashi Iwaie28563c2005-12-01 10:42:42 +0100555#ifdef CONFIG_SND_OSSEMUL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Takashi Iwai512bbd62005-11-17 13:51:18 +0100557void snd_card_info_read_oss(struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100560 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200563 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if ((card = snd_cards[idx]) != NULL) {
565 count++;
566 snd_iprintf(buffer, "%s\n", card->longname);
567 }
Takashi Iwai746df942006-05-15 19:49:05 +0200568 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 if (!count) {
571 snd_iprintf(buffer, "--- no soundcards ---\n");
572 }
573}
574
575#endif
576
577#ifdef MODULE
Takashi Iwai512bbd62005-11-17 13:51:18 +0100578static struct snd_info_entry *snd_card_module_info_entry;
579static void snd_card_module_info_read(struct snd_info_entry *entry,
580 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int idx;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100583 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 for (idx = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200586 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if ((card = snd_cards[idx]) != NULL)
Clemens Ladischd0015442005-11-20 14:09:05 +0100588 snd_iprintf(buffer, "%2i %s\n",
589 idx, card->module->name);
Takashi Iwai746df942006-05-15 19:49:05 +0200590 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592}
593#endif
594
595int __init snd_card_info_init(void)
596{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100597 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200600 if (! entry)
601 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 entry->c.text.read = snd_card_info_read;
603 if (snd_info_register(entry) < 0) {
604 snd_info_free_entry(entry);
605 return -ENOMEM;
606 }
607 snd_card_info_entry = entry;
608
609#ifdef MODULE
610 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
611 if (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 entry->c.text.read = snd_card_module_info_read;
613 if (snd_info_register(entry) < 0)
614 snd_info_free_entry(entry);
615 else
616 snd_card_module_info_entry = entry;
617 }
618#endif
619
620 return 0;
621}
622
623int __exit snd_card_info_done(void)
624{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200625 snd_info_free_entry(snd_card_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626#ifdef MODULE
Takashi Iwai746d4a02006-06-23 14:37:59 +0200627 snd_info_free_entry(snd_card_module_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628#endif
629 return 0;
630}
631
Takashi Iwaie28563c2005-12-01 10:42:42 +0100632#endif /* CONFIG_PROC_FS */
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/**
635 * snd_component_add - add a component string
636 * @card: soundcard structure
637 * @component: the component id string
638 *
639 * This function adds the component id string to the supported list.
640 * The component can be referred from the alsa-lib.
641 *
642 * Returns zero otherwise a negative error code.
643 */
644
Takashi Iwai512bbd62005-11-17 13:51:18 +0100645int snd_component_add(struct snd_card *card, const char *component)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 char *ptr;
648 int len = strlen(component);
649
650 ptr = strstr(card->components, component);
651 if (ptr != NULL) {
652 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
653 return 1;
654 }
655 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
656 snd_BUG();
657 return -ENOMEM;
658 }
659 if (card->components[0] != '\0')
660 strcat(card->components, " ");
661 strcat(card->components, component);
662 return 0;
663}
664
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200665EXPORT_SYMBOL(snd_component_add);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
668 * snd_card_file_add - add the file to the file list of the card
669 * @card: soundcard structure
670 * @file: file pointer
671 *
672 * This function adds the file to the file linked-list of the card.
673 * This linked-list is used to keep tracking the connection state,
674 * and to avoid the release of busy resources by hotplug.
675 *
676 * Returns zero or a negative error code.
677 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100678int snd_card_file_add(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct snd_monitor_file *mfile;
681
682 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
683 if (mfile == NULL)
684 return -ENOMEM;
685 mfile->file = file;
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200686 mfile->disconnected_f_op = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 mfile->next = NULL;
688 spin_lock(&card->files_lock);
689 if (card->shutdown) {
690 spin_unlock(&card->files_lock);
691 kfree(mfile);
692 return -ENODEV;
693 }
694 mfile->next = card->files;
695 card->files = mfile;
696 spin_unlock(&card->files_lock);
697 return 0;
698}
699
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200700EXPORT_SYMBOL(snd_card_file_add);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/**
703 * snd_card_file_remove - remove the file from the file list
704 * @card: soundcard structure
705 * @file: file pointer
706 *
707 * This function removes the file formerly added to the card via
708 * snd_card_file_add() function.
Takashi Iwai2b29b132006-06-23 14:38:26 +0200709 * If all files are removed and snd_card_free_when_closed() was
710 * called beforehand, it processes the pending release of
711 * resources.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 *
713 * Returns zero or a negative error code.
714 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100715int snd_card_file_remove(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct snd_monitor_file *mfile, *pfile = NULL;
Takashi Iwaic4614822006-06-23 14:38:23 +0200718 int last_close = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 spin_lock(&card->files_lock);
721 mfile = card->files;
722 while (mfile) {
723 if (mfile->file == file) {
724 if (pfile)
725 pfile->next = mfile->next;
726 else
727 card->files = mfile->next;
728 break;
729 }
730 pfile = mfile;
731 mfile = mfile->next;
732 }
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200733 if (mfile && mfile->disconnected_f_op) {
734 fops_put(mfile->disconnected_f_op);
735 spin_lock(&shutdown_lock);
736 list_del(&mfile->shutdown_list);
737 spin_unlock(&shutdown_lock);
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (card->files == NULL)
Takashi Iwaic4614822006-06-23 14:38:23 +0200740 last_close = 1;
741 spin_unlock(&card->files_lock);
742 if (last_close) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 wake_up(&card->shutdown_sleep);
Takashi Iwaic4614822006-06-23 14:38:23 +0200744 if (card->free_on_last_close)
745 snd_card_do_free(card);
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!mfile) {
748 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
749 return -ENOENT;
750 }
751 kfree(mfile);
752 return 0;
753}
754
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200755EXPORT_SYMBOL(snd_card_file_remove);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757#ifdef CONFIG_PM
758/**
759 * snd_power_wait - wait until the power-state is changed.
760 * @card: soundcard structure
761 * @power_state: expected power state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
763 * Waits until the power-state is changed.
764 *
765 * Note: the power lock must be active before call.
766 */
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200767int snd_power_wait(struct snd_card *card, unsigned int power_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 wait_queue_t wait;
770 int result = 0;
771
772 /* fastpath */
773 if (snd_power_get_state(card) == power_state)
774 return 0;
775 init_waitqueue_entry(&wait, current);
776 add_wait_queue(&card->power_sleep, &wait);
777 while (1) {
778 if (card->shutdown) {
779 result = -ENODEV;
780 break;
781 }
782 if (snd_power_get_state(card) == power_state)
783 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 set_current_state(TASK_UNINTERRUPTIBLE);
785 snd_power_unlock(card);
786 schedule_timeout(30 * HZ);
787 snd_power_lock(card);
788 }
789 remove_wait_queue(&card->power_sleep, &wait);
790 return result;
791}
792
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200793EXPORT_SYMBOL(snd_power_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794#endif /* CONFIG_PM */