blob: 48d38a79cbbba41d51b22b1c6487714348833749 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Initialization routines
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
34
Karsten Wiesea9edfc62006-10-06 16:08:27 +020035static DEFINE_SPINLOCK(shutdown_lock);
36static LIST_HEAD(shutdown_files);
37
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -080038static const struct file_operations snd_shutdown_f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwai6581f4e2006-05-17 17:14:51 +020040static unsigned int snd_cards_lock; /* locked for registering/using */
41struct snd_card *snd_cards[SNDRV_CARDS];
Takashi Iwaic0d3fb32006-04-28 15:13:39 +020042EXPORT_SYMBOL(snd_cards);
43
Takashi Iwai746df942006-05-15 19:49:05 +020044static DEFINE_MUTEX(snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Takashi Iwai304cd072007-10-26 15:10:15 +020046static char *slots[SNDRV_CARDS];
47module_param_array(slots, charp, NULL, 0444);
48MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
49
50/* return non-zero if the given index is already reserved for another
51 * module via slots option
52 */
53static int module_slot_mismatch(struct module *module, int idx)
54{
55#ifdef MODULE
56 char *s1, *s2;
57 if (!module || !module->name || !slots[idx])
58 return 0;
59 s1 = slots[idx];
60 s2 = module->name;
61 /* compare module name strings
62 * hyphens are handled as equivalent with underscore
63 */
64 for (;;) {
65 char c1 = *s1++;
66 char c2 = *s2++;
67 if (c1 == '-')
68 c1 = '_';
69 if (c2 == '-')
70 c2 = '_';
71 if (c1 != c2)
72 return 1;
73 if (!c1)
74 break;
75 }
76#endif
77 return 0;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
Takashi Iwai512bbd62005-11-17 13:51:18 +010081int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
Takashi Iwaic0d3fb32006-04-28 15:13:39 +020082EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84
Takashi Iwaie28563c2005-12-01 10:42:42 +010085#ifdef CONFIG_PROC_FS
Takashi Iwai512bbd62005-11-17 13:51:18 +010086static void snd_card_id_read(struct snd_info_entry *entry,
87 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 snd_iprintf(buffer, "%s\n", entry->card->id);
90}
91
Takashi Iwaie28563c2005-12-01 10:42:42 +010092static inline int init_info_for_card(struct snd_card *card)
93{
94 int err;
95 struct snd_info_entry *entry;
96
97 if ((err = snd_info_card_register(card)) < 0) {
98 snd_printd("unable to create card info\n");
99 return err;
100 }
101 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
102 snd_printd("unable to create card entry\n");
103 return err;
104 }
Takashi Iwaie28563c2005-12-01 10:42:42 +0100105 entry->c.text.read = snd_card_id_read;
106 if (snd_info_register(entry) < 0) {
107 snd_info_free_entry(entry);
108 entry = NULL;
109 }
110 card->proc_id = entry;
111 return 0;
112}
113#else /* !CONFIG_PROC_FS */
114#define init_info_for_card(card)
115#endif
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/**
118 * snd_card_new - create and initialize a soundcard structure
119 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
120 * @xid: card identification (ASCII string)
121 * @module: top level module for locking
122 * @extra_size: allocate this extra size after the main soundcard structure
123 *
124 * Creates and initializes a soundcard structure.
125 *
Takashi Iwai512bbd62005-11-17 13:51:18 +0100126 * Returns kmallocated snd_card structure. Creates the ALSA control interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * (which is blocked until snd_card_register function is called).
128 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100129struct snd_card *snd_card_new(int idx, const char *xid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct module *module, int extra_size)
131{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100132 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int err;
134
135 if (extra_size < 0)
136 extra_size = 0;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200137 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (card == NULL)
139 return NULL;
140 if (xid) {
141 if (!snd_info_check_reserved_words(xid))
142 goto __error;
143 strlcpy(card->id, xid, sizeof(card->id));
144 }
145 err = 0;
Takashi Iwai746df942006-05-15 19:49:05 +0200146 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (idx < 0) {
148 int idx2;
149 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
Oliver Neukum5c33dd72007-01-16 17:49:21 +0100150 /* idx == -1 == 0xffff means: take any free slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (~snd_cards_lock & idx & 1<<idx2) {
Takashi Iwai304cd072007-10-26 15:10:15 +0200152 if (module_slot_mismatch(module, idx2))
153 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 idx = idx2;
155 if (idx >= snd_ecards_limit)
156 snd_ecards_limit = idx + 1;
157 break;
158 }
Oliver Neukum5c33dd72007-01-16 17:49:21 +0100159 } else {
160 if (idx < snd_ecards_limit) {
161 if (snd_cards_lock & (1 << idx))
162 err = -EBUSY; /* invalid */
163 } else {
164 if (idx < SNDRV_CARDS)
165 snd_ecards_limit = idx + 1; /* increase the limit */
166 else
167 err = -ENODEV;
168 }
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (idx < 0 || err < 0) {
Takashi Iwai746df942006-05-15 19:49:05 +0200171 mutex_unlock(&snd_card_mutex);
Oliver Neukum5c33dd72007-01-16 17:49:21 +0100172 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i), error: %d\n",
173 idx, snd_ecards_limit - 1, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 goto __error;
175 }
176 snd_cards_lock |= 1 << idx; /* lock it */
Takashi Iwai746df942006-05-15 19:49:05 +0200177 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 card->number = idx;
179 card->module = module;
180 INIT_LIST_HEAD(&card->devices);
181 init_rwsem(&card->controls_rwsem);
182 rwlock_init(&card->ctl_files_rwlock);
183 INIT_LIST_HEAD(&card->controls);
184 INIT_LIST_HEAD(&card->ctl_files);
185 spin_lock_init(&card->files_lock);
186 init_waitqueue_head(&card->shutdown_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#ifdef CONFIG_PM
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100188 mutex_init(&card->power_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 init_waitqueue_head(&card->power_sleep);
190#endif
191 /* the control interface cannot be accessed from the user space until */
192 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
193 if ((err = snd_ctl_create(card)) < 0) {
194 snd_printd("unable to register control minors\n");
195 goto __error;
196 }
197 if ((err = snd_info_card_create(card)) < 0) {
198 snd_printd("unable to create card info\n");
199 goto __error_ctl;
200 }
201 if (extra_size > 0)
Takashi Iwai512bbd62005-11-17 13:51:18 +0100202 card->private_data = (char *)card + sizeof(struct snd_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return card;
204
205 __error_ctl:
206 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
207 __error:
208 kfree(card);
209 return NULL;
210}
211
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200212EXPORT_SYMBOL(snd_card_new);
213
Takashi Iwai746df942006-05-15 19:49:05 +0200214/* return non-zero if a card is already locked */
215int snd_card_locked(int card)
216{
217 int locked;
218
219 mutex_lock(&snd_card_mutex);
220 locked = snd_cards_lock & (1 << card);
221 mutex_unlock(&snd_card_mutex);
222 return locked;
223}
224
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100225static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
226{
227 return -ENODEV;
228}
229
230static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
231 size_t count, loff_t *offset)
232{
233 return -ENODEV;
234}
235
236static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
237 size_t count, loff_t *offset)
238{
239 return -ENODEV;
240}
241
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200242static int snd_disconnect_release(struct inode *inode, struct file *file)
243{
244 struct snd_monitor_file *df = NULL, *_df;
245
246 spin_lock(&shutdown_lock);
247 list_for_each_entry(_df, &shutdown_files, shutdown_list) {
248 if (_df->file == file) {
249 df = _df;
250 break;
251 }
252 }
253 spin_unlock(&shutdown_lock);
254
255 if (likely(df))
256 return df->disconnected_f_op->release(inode, file);
257
258 panic("%s(%p, %p) failed!", __FUNCTION__, inode, file);
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
262{
263 return POLLERR | POLLNVAL;
264}
265
Clemens Ladischb3b0abe2006-03-03 14:08:43 +0100266static long snd_disconnect_ioctl(struct file *file,
267 unsigned int cmd, unsigned long arg)
268{
269 return -ENODEV;
270}
271
272static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
273{
274 return -ENODEV;
275}
276
277static int snd_disconnect_fasync(int fd, struct file *file, int on)
278{
279 return -ENODEV;
280}
281
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800282static const struct file_operations snd_shutdown_f_ops =
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200283{
284 .owner = THIS_MODULE,
285 .llseek = snd_disconnect_llseek,
286 .read = snd_disconnect_read,
287 .write = snd_disconnect_write,
288 .release = snd_disconnect_release,
289 .poll = snd_disconnect_poll,
290 .unlocked_ioctl = snd_disconnect_ioctl,
291#ifdef CONFIG_COMPAT
292 .compat_ioctl = snd_disconnect_ioctl,
293#endif
294 .mmap = snd_disconnect_mmap,
295 .fasync = snd_disconnect_fasync
296};
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/**
299 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
300 * @card: soundcard structure
301 *
302 * Disconnects all APIs from the file-operations (user space).
303 *
304 * Returns zero, otherwise a negative error code.
305 *
306 * Note: The current implementation replaces all active file->f_op with special
307 * dummy file operations (they do nothing except release).
308 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100309int snd_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct snd_monitor_file *mfile;
312 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int err;
314
315 spin_lock(&card->files_lock);
316 if (card->shutdown) {
317 spin_unlock(&card->files_lock);
318 return 0;
319 }
320 card->shutdown = 1;
321 spin_unlock(&card->files_lock);
322
323 /* phase 1: disable fops (user space) operations for ALSA API */
Takashi Iwai746df942006-05-15 19:49:05 +0200324 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 snd_cards[card->number] = NULL;
Takashi Iwai746df942006-05-15 19:49:05 +0200326 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* phase 2: replace file->f_op with special dummy operations */
329
330 spin_lock(&card->files_lock);
331 mfile = card->files;
332 while (mfile) {
333 file = mfile->file;
334
335 /* it's critical part, use endless loop */
336 /* we have no room to fail */
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200337 mfile->disconnected_f_op = mfile->file->f_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200339 spin_lock(&shutdown_lock);
340 list_add(&mfile->shutdown_list, &shutdown_files);
341 spin_unlock(&shutdown_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200343 fops_get(&snd_shutdown_f_ops);
344 mfile->file->f_op = &snd_shutdown_f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 mfile = mfile->next;
347 }
348 spin_unlock(&card->files_lock);
349
350 /* phase 3: notify all connected devices about disconnection */
351 /* at this point, they cannot respond to any calls except release() */
352
353#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
354 if (snd_mixer_oss_notify_callback)
355 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
356#endif
357
358 /* notify all devices that we are disconnected */
359 err = snd_device_disconnect_all(card);
360 if (err < 0)
361 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
362
Takashi Iwai746d4a02006-06-23 14:37:59 +0200363 snd_info_card_disconnect(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365}
366
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200367EXPORT_SYMBOL(snd_card_disconnect);
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/**
370 * snd_card_free - frees given soundcard structure
371 * @card: soundcard structure
372 *
373 * This function releases the soundcard structure and the all assigned
374 * devices automatically. That is, you don't have to release the devices
375 * by yourself.
376 *
377 * Returns zero. Frees all associated devices and frees the control
378 * interface associated to given soundcard.
379 */
Takashi Iwaic4614822006-06-23 14:38:23 +0200380static int snd_card_do_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
383 if (snd_mixer_oss_notify_callback)
384 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
385#endif
386 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
387 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
388 /* Fatal, but this situation should never occur */
389 }
390 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
391 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
392 /* Fatal, but this situation should never occur */
393 }
394 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
395 snd_printk(KERN_ERR "unable to free all devices (post)\n");
396 /* Fatal, but this situation should never occur */
397 }
398 if (card->private_free)
399 card->private_free(card);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200400 snd_info_free_entry(card->proc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (snd_info_card_free(card) < 0) {
402 snd_printk(KERN_WARNING "unable to free card info\n");
403 /* Not fatal error */
404 }
Takashi Iwai7d2aae12007-01-26 12:40:31 +0100405#ifndef CONFIG_SYSFS_DEPRECATED
406 if (card->card_dev)
407 device_unregister(card->card_dev);
408#endif
Takashi Iwaic4614822006-06-23 14:38:23 +0200409 kfree(card);
410 return 0;
411}
412
413static int snd_card_free_prepare(struct snd_card *card)
414{
415 if (card == NULL)
416 return -EINVAL;
417 (void) snd_card_disconnect(card);
Takashi Iwai746df942006-05-15 19:49:05 +0200418 mutex_lock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200419 snd_cards[card->number] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 snd_cards_lock &= ~(1 << card->number);
Takashi Iwai746df942006-05-15 19:49:05 +0200421 mutex_unlock(&snd_card_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200422#ifdef CONFIG_PM
423 wake_up(&card->power_sleep);
424#endif
425 return 0;
426}
427
428int snd_card_free_when_closed(struct snd_card *card)
429{
430 int free_now = 0;
431 int ret = snd_card_free_prepare(card);
432 if (ret)
433 return ret;
434
435 spin_lock(&card->files_lock);
436 if (card->files == NULL)
437 free_now = 1;
438 else
439 card->free_on_last_close = 1;
440 spin_unlock(&card->files_lock);
441
442 if (free_now)
443 snd_card_do_free(card);
444 return 0;
445}
446
447EXPORT_SYMBOL(snd_card_free_when_closed);
448
449int snd_card_free(struct snd_card *card)
450{
451 int ret = snd_card_free_prepare(card);
452 if (ret)
453 return ret;
454
455 /* wait, until all devices are ready for the free operation */
456 wait_event(card->shutdown_sleep, card->files == NULL);
457 snd_card_do_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459}
460
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200461EXPORT_SYMBOL(snd_card_free);
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);
Takashi Iwai7d2aae12007-01-26 12:40:31 +0100543#ifndef CONFIG_SYSFS_DEPRECATED
544 if (!card->card_dev) {
545 card->card_dev = device_create(sound_class, card->dev, 0,
546 "card%i", card->number);
547 if (IS_ERR(card->card_dev))
548 card->card_dev = NULL;
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700549 }
Takashi Iwai7d2aae12007-01-26 12:40:31 +0100550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if ((err = snd_device_register_all(card)) < 0)
552 return err;
Takashi Iwai746df942006-05-15 19:49:05 +0200553 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (snd_cards[card->number]) {
555 /* already registered */
Takashi Iwai746df942006-05-15 19:49:05 +0200556 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 0;
558 }
559 if (card->id[0] == '\0')
560 choose_default_id(card);
561 snd_cards[card->number] = card;
Takashi Iwai746df942006-05-15 19:49:05 +0200562 mutex_unlock(&snd_card_mutex);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100563 init_info_for_card(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
565 if (snd_mixer_oss_notify_callback)
566 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
567#endif
568 return 0;
569}
570
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200571EXPORT_SYMBOL(snd_card_register);
572
Takashi Iwaie28563c2005-12-01 10:42:42 +0100573#ifdef CONFIG_PROC_FS
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200574static struct snd_info_entry *snd_card_info_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Takashi Iwaia381a7a2005-11-17 15:55:49 +0100576static void snd_card_info_read(struct snd_info_entry *entry,
577 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100580 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200583 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if ((card = snd_cards[idx]) != NULL) {
585 count++;
Clemens Ladischd0015442005-11-20 14:09:05 +0100586 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 idx,
588 card->id,
589 card->driver,
590 card->shortname);
Clemens Ladischd0015442005-11-20 14:09:05 +0100591 snd_iprintf(buffer, " %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 card->longname);
593 }
Takashi Iwai746df942006-05-15 19:49:05 +0200594 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 if (!count)
597 snd_iprintf(buffer, "--- no soundcards ---\n");
598}
599
Takashi Iwaie28563c2005-12-01 10:42:42 +0100600#ifdef CONFIG_SND_OSSEMUL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Takashi Iwai512bbd62005-11-17 13:51:18 +0100602void snd_card_info_read_oss(struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 int idx, count;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100605 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200608 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if ((card = snd_cards[idx]) != NULL) {
610 count++;
611 snd_iprintf(buffer, "%s\n", card->longname);
612 }
Takashi Iwai746df942006-05-15 19:49:05 +0200613 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 if (!count) {
616 snd_iprintf(buffer, "--- no soundcards ---\n");
617 }
618}
619
620#endif
621
622#ifdef MODULE
Takashi Iwai512bbd62005-11-17 13:51:18 +0100623static struct snd_info_entry *snd_card_module_info_entry;
624static void snd_card_module_info_read(struct snd_info_entry *entry,
625 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 int idx;
Takashi Iwai512bbd62005-11-17 13:51:18 +0100628 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 for (idx = 0; idx < SNDRV_CARDS; idx++) {
Takashi Iwai746df942006-05-15 19:49:05 +0200631 mutex_lock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if ((card = snd_cards[idx]) != NULL)
Clemens Ladischd0015442005-11-20 14:09:05 +0100633 snd_iprintf(buffer, "%2i %s\n",
634 idx, card->module->name);
Takashi Iwai746df942006-05-15 19:49:05 +0200635 mutex_unlock(&snd_card_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637}
638#endif
639
640int __init snd_card_info_init(void)
641{
Takashi Iwai512bbd62005-11-17 13:51:18 +0100642 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200645 if (! entry)
646 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 entry->c.text.read = snd_card_info_read;
648 if (snd_info_register(entry) < 0) {
649 snd_info_free_entry(entry);
650 return -ENOMEM;
651 }
652 snd_card_info_entry = entry;
653
654#ifdef MODULE
655 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
656 if (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 entry->c.text.read = snd_card_module_info_read;
658 if (snd_info_register(entry) < 0)
659 snd_info_free_entry(entry);
660 else
661 snd_card_module_info_entry = entry;
662 }
663#endif
664
665 return 0;
666}
667
668int __exit snd_card_info_done(void)
669{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200670 snd_info_free_entry(snd_card_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671#ifdef MODULE
Takashi Iwai746d4a02006-06-23 14:37:59 +0200672 snd_info_free_entry(snd_card_module_info_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673#endif
674 return 0;
675}
676
Takashi Iwaie28563c2005-12-01 10:42:42 +0100677#endif /* CONFIG_PROC_FS */
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/**
680 * snd_component_add - add a component string
681 * @card: soundcard structure
682 * @component: the component id string
683 *
684 * This function adds the component id string to the supported list.
685 * The component can be referred from the alsa-lib.
686 *
687 * Returns zero otherwise a negative error code.
688 */
689
Takashi Iwai512bbd62005-11-17 13:51:18 +0100690int snd_component_add(struct snd_card *card, const char *component)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 char *ptr;
693 int len = strlen(component);
694
695 ptr = strstr(card->components, component);
696 if (ptr != NULL) {
697 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
698 return 1;
699 }
700 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
701 snd_BUG();
702 return -ENOMEM;
703 }
704 if (card->components[0] != '\0')
705 strcat(card->components, " ");
706 strcat(card->components, component);
707 return 0;
708}
709
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200710EXPORT_SYMBOL(snd_component_add);
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/**
713 * snd_card_file_add - add the file to the file list of the card
714 * @card: soundcard structure
715 * @file: file pointer
716 *
717 * This function adds the file to the file linked-list of the card.
718 * This linked-list is used to keep tracking the connection state,
719 * and to avoid the release of busy resources by hotplug.
720 *
721 * Returns zero or a negative error code.
722 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100723int snd_card_file_add(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 struct snd_monitor_file *mfile;
726
727 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
728 if (mfile == NULL)
729 return -ENOMEM;
730 mfile->file = file;
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200731 mfile->disconnected_f_op = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 mfile->next = NULL;
733 spin_lock(&card->files_lock);
734 if (card->shutdown) {
735 spin_unlock(&card->files_lock);
736 kfree(mfile);
737 return -ENODEV;
738 }
739 mfile->next = card->files;
740 card->files = mfile;
741 spin_unlock(&card->files_lock);
742 return 0;
743}
744
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200745EXPORT_SYMBOL(snd_card_file_add);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/**
748 * snd_card_file_remove - remove the file from the file list
749 * @card: soundcard structure
750 * @file: file pointer
751 *
752 * This function removes the file formerly added to the card via
753 * snd_card_file_add() function.
Takashi Iwai2b29b132006-06-23 14:38:26 +0200754 * If all files are removed and snd_card_free_when_closed() was
755 * called beforehand, it processes the pending release of
756 * resources.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *
758 * Returns zero or a negative error code.
759 */
Takashi Iwai512bbd62005-11-17 13:51:18 +0100760int snd_card_file_remove(struct snd_card *card, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct snd_monitor_file *mfile, *pfile = NULL;
Takashi Iwaic4614822006-06-23 14:38:23 +0200763 int last_close = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 spin_lock(&card->files_lock);
766 mfile = card->files;
767 while (mfile) {
768 if (mfile->file == file) {
769 if (pfile)
770 pfile->next = mfile->next;
771 else
772 card->files = mfile->next;
773 break;
774 }
775 pfile = mfile;
776 mfile = mfile->next;
777 }
Karsten Wiesea9edfc62006-10-06 16:08:27 +0200778 if (mfile && mfile->disconnected_f_op) {
779 fops_put(mfile->disconnected_f_op);
780 spin_lock(&shutdown_lock);
781 list_del(&mfile->shutdown_list);
782 spin_unlock(&shutdown_lock);
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (card->files == NULL)
Takashi Iwaic4614822006-06-23 14:38:23 +0200785 last_close = 1;
786 spin_unlock(&card->files_lock);
787 if (last_close) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 wake_up(&card->shutdown_sleep);
Takashi Iwaic4614822006-06-23 14:38:23 +0200789 if (card->free_on_last_close)
790 snd_card_do_free(card);
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!mfile) {
793 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
794 return -ENOENT;
795 }
796 kfree(mfile);
797 return 0;
798}
799
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200800EXPORT_SYMBOL(snd_card_file_remove);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#ifdef CONFIG_PM
803/**
804 * snd_power_wait - wait until the power-state is changed.
805 * @card: soundcard structure
806 * @power_state: expected power state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
808 * Waits until the power-state is changed.
809 *
810 * Note: the power lock must be active before call.
811 */
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200812int snd_power_wait(struct snd_card *card, unsigned int power_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 wait_queue_t wait;
815 int result = 0;
816
817 /* fastpath */
818 if (snd_power_get_state(card) == power_state)
819 return 0;
820 init_waitqueue_entry(&wait, current);
821 add_wait_queue(&card->power_sleep, &wait);
822 while (1) {
823 if (card->shutdown) {
824 result = -ENODEV;
825 break;
826 }
827 if (snd_power_get_state(card) == power_state)
828 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 set_current_state(TASK_UNINTERRUPTIBLE);
830 snd_power_unlock(card);
831 schedule_timeout(30 * HZ);
832 snd_power_lock(card);
833 }
834 remove_wait_queue(&card->power_sleep, &wait);
835 return result;
836}
837
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200838EXPORT_SYMBOL(snd_power_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839#endif /* CONFIG_PM */