blob: d7607a25acdf7086e05cbe16d2d64c7e19ed5897 [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 -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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
202{
203 return POLLERR | POLLNVAL;
204}
205
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100206static long snd_disconnect_ioctl(struct file *file,
207 unsigned int cmd, unsigned long arg)
208{
209 return -ENODEV;
210}
211
212static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
213{
214 return -ENODEV;
215}
216
217static int snd_disconnect_fasync(int fd, struct file *file, int on)
218{
219 return -ENODEV;
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/**
223 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
224 * @card: soundcard structure
225 *
226 * Disconnects all APIs from the file-operations (user space).
227 *
228 * Returns zero, otherwise a negative error code.
229 *
230 * Note: The current implementation replaces all active file->f_op with special
231 * dummy file operations (they do nothing except release).
232 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100233int snd_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 struct snd_monitor_file *mfile;
236 struct file *file;
237 struct snd_shutdown_f_ops *s_f_ops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800238 struct file_operations *f_ops;
239 const struct file_operations *old_f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 int err;
241
242 spin_lock(&card->files_lock);
243 if (card->shutdown) {
244 spin_unlock(&card->files_lock);
245 return 0;
246 }
247 card->shutdown = 1;
248 spin_unlock(&card->files_lock);
249
250 /* phase 1: disable fops (user space) operations for ALSA API */
Takashi Iwai746df942006-05-15 19:49:05 +0200251 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 snd_cards[card->number] = NULL;
Takashi Iwai746df942006-05-15 19:49:05 +0200253 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /* phase 2: replace file->f_op with special dummy operations */
256
257 spin_lock(&card->files_lock);
258 mfile = card->files;
259 while (mfile) {
260 file = mfile->file;
261
262 /* it's critical part, use endless loop */
263 /* we have no room to fail */
264 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
265 if (s_f_ops == NULL)
266 panic("Atomic allocation failed for snd_shutdown_f_ops!");
267
268 f_ops = &s_f_ops->f_ops;
269
270 memset(f_ops, 0, sizeof(*f_ops));
271 f_ops->owner = file->f_op->owner;
272 f_ops->release = file->f_op->release;
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100273 f_ops->llseek = snd_disconnect_llseek;
274 f_ops->read = snd_disconnect_read;
275 f_ops->write = snd_disconnect_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 f_ops->poll = snd_disconnect_poll;
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100277 f_ops->unlocked_ioctl = snd_disconnect_ioctl;
278#ifdef CONFIG_COMPAT
279 f_ops->compat_ioctl = snd_disconnect_ioctl;
280#endif
281 f_ops->mmap = snd_disconnect_mmap;
282 f_ops->fasync = snd_disconnect_fasync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 s_f_ops->next = card->s_f_ops;
285 card->s_f_ops = s_f_ops;
286
287 f_ops = fops_get(f_ops);
288
289 old_f_ops = file->f_op;
290 file->f_op = f_ops; /* must be atomic */
291 fops_put(old_f_ops);
292
293 mfile = mfile->next;
294 }
295 spin_unlock(&card->files_lock);
296
297 /* phase 3: notify all connected devices about disconnection */
298 /* at this point, they cannot respond to any calls except release() */
299
300#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
301 if (snd_mixer_oss_notify_callback)
302 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
303#endif
304
305 /* notify all devices that we are disconnected */
306 err = snd_device_disconnect_all(card);
307 if (err < 0)
308 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
309
Takashi Iwai746d4a02006-06-23 14:37:59 +0200310 snd_info_card_disconnect(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
312}
313
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200314EXPORT_SYMBOL(snd_card_disconnect);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/**
317 * snd_card_free - frees given soundcard structure
318 * @card: soundcard structure
319 *
320 * This function releases the soundcard structure and the all assigned
321 * devices automatically. That is, you don't have to release the devices
322 * by yourself.
323 *
324 * Returns zero. Frees all associated devices and frees the control
325 * interface associated to given soundcard.
326 */
Takashi Iwaic4614822006-06-23 14:38:23 +0200327static int snd_card_do_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct snd_shutdown_f_ops *s_f_ops;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
332 if (snd_mixer_oss_notify_callback)
333 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
334#endif
335 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
336 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
337 /* Fatal, but this situation should never occur */
338 }
339 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
340 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
341 /* Fatal, but this situation should never occur */
342 }
343 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
344 snd_printk(KERN_ERR "unable to free all devices (post)\n");
345 /* Fatal, but this situation should never occur */
346 }
347 if (card->private_free)
348 card->private_free(card);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200349 snd_info_free_entry(card->proc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (snd_info_card_free(card) < 0) {
351 snd_printk(KERN_WARNING "unable to free card info\n");
352 /* Not fatal error */
353 }
354 while (card->s_f_ops) {
355 s_f_ops = card->s_f_ops;
356 card->s_f_ops = s_f_ops->next;
357 kfree(s_f_ops);
358 }
Takashi Iwaic4614822006-06-23 14:38:23 +0200359 kfree(card);
360 return 0;
361}
362
363static int snd_card_free_prepare(struct snd_card *card)
364{
365 if (card == NULL)
366 return -EINVAL;
367 (void) snd_card_disconnect(card);
Takashi Iwai746df942006-05-15 19:49:05 +0200368 mutex_lock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200369 snd_cards[card->number] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 snd_cards_lock &= ~(1 << card->number);
Takashi Iwai746df942006-05-15 19:49:05 +0200371 mutex_unlock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200372#ifdef CONFIG_PM
373 wake_up(&card->power_sleep);
374#endif
375 return 0;
376}
377
378int snd_card_free_when_closed(struct snd_card *card)
379{
380 int free_now = 0;
381 int ret = snd_card_free_prepare(card);
382 if (ret)
383 return ret;
384
385 spin_lock(&card->files_lock);
386 if (card->files == NULL)
387 free_now = 1;
388 else
389 card->free_on_last_close = 1;
390 spin_unlock(&card->files_lock);
391
392 if (free_now)
393 snd_card_do_free(card);
394 return 0;
395}
396
397EXPORT_SYMBOL(snd_card_free_when_closed);
398
399int snd_card_free(struct snd_card *card)
400{
401 int ret = snd_card_free_prepare(card);
402 if (ret)
403 return ret;
404
405 /* wait, until all devices are ready for the free operation */
406 wait_event(card->shutdown_sleep, card->files == NULL);
407 snd_card_do_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
409}
410
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200411EXPORT_SYMBOL(snd_card_free);
412
Takashi Iwai512bbd62005-11-17 13:51:18 +0100413static void choose_default_id(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Clemens Ladischd0015442005-11-20 14:09:05 +0100415 int i, len, idx_flag = 0, loops = SNDRV_CARDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 char *id, *spos;
417
418 id = spos = card->shortname;
419 while (*id != '\0') {
420 if (*id == ' ')
421 spos = id + 1;
422 id++;
423 }
424 id = card->id;
425 while (*spos != '\0' && !isalnum(*spos))
426 spos++;
427 if (isdigit(*spos))
428 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
429 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
430 if (isalnum(*spos))
431 *id++ = *spos;
432 spos++;
433 }
434 *id = '\0';
435
436 id = card->id;
437
438 if (*id == '\0')
439 strcpy(id, "default");
440
441 while (1) {
442 if (loops-- == 0) {
443 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
444 strcpy(card->id, card->proc_root->name);
445 return;
446 }
447 if (!snd_info_check_reserved_words(id))
448 goto __change;
449 for (i = 0; i < snd_ecards_limit; i++) {
450 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
451 goto __change;
452 }
453 break;
454
455 __change:
456 len = strlen(id);
Clemens Ladischd0015442005-11-20 14:09:05 +0100457 if (idx_flag) {
458 if (id[len-1] != '9')
459 id[len-1]++;
460 else
461 id[len-1] = 'A';
462 } else if ((size_t)len <= sizeof(card->id) - 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 strcat(id, "_1");
464 idx_flag++;
465 } else {
466 spos = id + len - 2;
467 if ((size_t)len <= sizeof(card->id) - 2)
468 spos++;
469 *spos++ = '_';
470 *spos++ = '1';
471 *spos++ = '\0';
472 idx_flag++;
473 }
474 }
475}
476
477/**
478 * snd_card_register - register the soundcard
479 * @card: soundcard structure
480 *
481 * This function registers all the devices assigned to the soundcard.
482 * Until calling this, the ALSA control interface is blocked from the
483 * external accesses. Thus, you should call this function at the end
484 * of the initialization of the card.
485 *
486 * Returns zero otherwise a negative error code if the registrain failed.
487 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100488int snd_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200492 snd_assert(card != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if ((err = snd_device_register_all(card)) < 0)
494 return err;
Takashi Iwai746df942006-05-15 19:49:05 +0200495 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (snd_cards[card->number]) {
497 /* already registered */
Takashi Iwai746df942006-05-15 19:49:05 +0200498 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 0;
500 }
501 if (card->id[0] == '\0')
502 choose_default_id(card);
503 snd_cards[card->number] = card;
Takashi Iwai746df942006-05-15 19:49:05 +0200504 mutex_unlock(&snd_card_mutex);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100505 init_info_for_card(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
507 if (snd_mixer_oss_notify_callback)
508 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
509#endif
510 return 0;
511}
512
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200513EXPORT_SYMBOL(snd_card_register);
514
Takashi Iwaie28563c2005-12-01 10:42:42 +0100515#ifdef CONFIG_PROC_FS
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200516static struct snd_info_entry *snd_card_info_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Takashi Iwaia381a7a2005-11-17 15:55:49 +0100518static void snd_card_info_read(struct snd_info_entry *entry,
519 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100522 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200525 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if ((card = snd_cards[idx]) != NULL) {
527 count++;
Clemens Ladischd0015442005-11-20 14:09:05 +0100528 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 idx,
530 card->id,
531 card->driver,
532 card->shortname);
Clemens Ladischd0015442005-11-20 14:09:05 +0100533 snd_iprintf(buffer, " %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 card->longname);
535 }
Takashi Iwai746df942006-05-15 19:49:05 +0200536 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 if (!count)
539 snd_iprintf(buffer, "--- no soundcards ---\n");
540}
541
Takashi Iwaie28563c2005-12-01 10:42:42 +0100542#ifdef CONFIG_SND_OSSEMUL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Takashi Iwai512bbd62005-11-17 13:51:18 +0100544void snd_card_info_read_oss(struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100547 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200550 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if ((card = snd_cards[idx]) != NULL) {
552 count++;
553 snd_iprintf(buffer, "%s\n", card->longname);
554 }
Takashi Iwai746df942006-05-15 19:49:05 +0200555 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 if (!count) {
558 snd_iprintf(buffer, "--- no soundcards ---\n");
559 }
560}
561
562#endif
563
564#ifdef MODULE
Takashi Iwai512bbd62005-11-17 13:51:18 +0100565static struct snd_info_entry *snd_card_module_info_entry;
566static void snd_card_module_info_read(struct snd_info_entry *entry,
567 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int idx;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100570 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 for (idx = 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)
Clemens Ladischd0015442005-11-20 14:09:05 +0100575 snd_iprintf(buffer, "%2i %s\n",
576 idx, card->module->name);
Takashi Iwai746df942006-05-15 19:49:05 +0200577 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579}
580#endif
581
582int __init snd_card_info_init(void)
583{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100584 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200587 if (! entry)
588 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 entry->c.text.read = snd_card_info_read;
590 if (snd_info_register(entry) < 0) {
591 snd_info_free_entry(entry);
592 return -ENOMEM;
593 }
594 snd_card_info_entry = entry;
595
596#ifdef MODULE
597 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
598 if (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 entry->c.text.read = snd_card_module_info_read;
600 if (snd_info_register(entry) < 0)
601 snd_info_free_entry(entry);
602 else
603 snd_card_module_info_entry = entry;
604 }
605#endif
606
607 return 0;
608}
609
610int __exit snd_card_info_done(void)
611{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200612 snd_info_free_entry(snd_card_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#ifdef MODULE
Takashi Iwai746d4a02006-06-23 14:37:59 +0200614 snd_info_free_entry(snd_card_module_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615#endif
616 return 0;
617}
618
Takashi Iwaie28563c2005-12-01 10:42:42 +0100619#endif /* CONFIG_PROC_FS */
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/**
622 * snd_component_add - add a component string
623 * @card: soundcard structure
624 * @component: the component id string
625 *
626 * This function adds the component id string to the supported list.
627 * The component can be referred from the alsa-lib.
628 *
629 * Returns zero otherwise a negative error code.
630 */
631
Takashi Iwai512bbd62005-11-17 13:51:18 +0100632int snd_component_add(struct snd_card *card, const char *component)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 char *ptr;
635 int len = strlen(component);
636
637 ptr = strstr(card->components, component);
638 if (ptr != NULL) {
639 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
640 return 1;
641 }
642 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
643 snd_BUG();
644 return -ENOMEM;
645 }
646 if (card->components[0] != '\0')
647 strcat(card->components, " ");
648 strcat(card->components, component);
649 return 0;
650}
651
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200652EXPORT_SYMBOL(snd_component_add);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/**
655 * snd_card_file_add - add the file to the file list of the card
656 * @card: soundcard structure
657 * @file: file pointer
658 *
659 * This function adds the file to the file linked-list of the card.
660 * This linked-list is used to keep tracking the connection state,
661 * and to avoid the release of busy resources by hotplug.
662 *
663 * Returns zero or a negative error code.
664 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100665int snd_card_file_add(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct snd_monitor_file *mfile;
668
669 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
670 if (mfile == NULL)
671 return -ENOMEM;
672 mfile->file = file;
673 mfile->next = NULL;
674 spin_lock(&card->files_lock);
675 if (card->shutdown) {
676 spin_unlock(&card->files_lock);
677 kfree(mfile);
678 return -ENODEV;
679 }
680 mfile->next = card->files;
681 card->files = mfile;
682 spin_unlock(&card->files_lock);
683 return 0;
684}
685
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200686EXPORT_SYMBOL(snd_card_file_add);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/**
689 * snd_card_file_remove - remove the file from the file list
690 * @card: soundcard structure
691 * @file: file pointer
692 *
693 * This function removes the file formerly added to the card via
694 * snd_card_file_add() function.
Takashi Iwai2b29b132006-06-23 14:38:26 +0200695 * If all files are removed and snd_card_free_when_closed() was
696 * called beforehand, it processes the pending release of
697 * resources.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 *
699 * Returns zero or a negative error code.
700 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100701int snd_card_file_remove(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct snd_monitor_file *mfile, *pfile = NULL;
Takashi Iwaic4614822006-06-23 14:38:23 +0200704 int last_close = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 spin_lock(&card->files_lock);
707 mfile = card->files;
708 while (mfile) {
709 if (mfile->file == file) {
710 if (pfile)
711 pfile->next = mfile->next;
712 else
713 card->files = mfile->next;
714 break;
715 }
716 pfile = mfile;
717 mfile = mfile->next;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (card->files == NULL)
Takashi Iwaic4614822006-06-23 14:38:23 +0200720 last_close = 1;
721 spin_unlock(&card->files_lock);
722 if (last_close) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 wake_up(&card->shutdown_sleep);
Takashi Iwaic4614822006-06-23 14:38:23 +0200724 if (card->free_on_last_close)
725 snd_card_do_free(card);
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!mfile) {
728 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
729 return -ENOENT;
730 }
731 kfree(mfile);
732 return 0;
733}
734
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200735EXPORT_SYMBOL(snd_card_file_remove);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737#ifdef CONFIG_PM
738/**
739 * snd_power_wait - wait until the power-state is changed.
740 * @card: soundcard structure
741 * @power_state: expected power state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 *
743 * Waits until the power-state is changed.
744 *
745 * Note: the power lock must be active before call.
746 */
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200747int snd_power_wait(struct snd_card *card, unsigned int power_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 wait_queue_t wait;
750 int result = 0;
751
752 /* fastpath */
753 if (snd_power_get_state(card) == power_state)
754 return 0;
755 init_waitqueue_entry(&wait, current);
756 add_wait_queue(&card->power_sleep, &wait);
757 while (1) {
758 if (card->shutdown) {
759 result = -ENODEV;
760 break;
761 }
762 if (snd_power_get_state(card) == power_state)
763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 set_current_state(TASK_UNINTERRUPTIBLE);
765 snd_power_unlock(card);
766 schedule_timeout(30 * HZ);
767 snd_power_lock(card);
768 }
769 remove_wait_queue(&card->power_sleep, &wait);
770 return result;
771}
772
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200773EXPORT_SYMBOL(snd_power_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774#endif /* CONFIG_PM */