[PATCH] PCI: really fix parent's subordinate busnr
[linux-2.6/btrfs-unstable.git] / sound / core / init.c
blobad68761abba1a86eb9dd9fa16d0c495ecb428d84
1 /*
2 * Initialization routines
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
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>
32 #include <sound/core.h>
33 #include <sound/control.h>
34 #include <sound/info.h>
36 struct snd_shutdown_f_ops {
37 struct file_operations f_ops;
38 struct snd_shutdown_f_ops *next;
41 unsigned int snd_cards_lock = 0; /* locked for registering/using */
42 struct snd_card *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL};
43 DEFINE_RWLOCK(snd_card_rwlock);
45 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
46 int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
47 #endif
49 #ifdef CONFIG_PROC_FS
50 static void snd_card_id_read(struct snd_info_entry *entry,
51 struct snd_info_buffer *buffer)
53 snd_iprintf(buffer, "%s\n", entry->card->id);
56 static inline int init_info_for_card(struct snd_card *card)
58 int err;
59 struct snd_info_entry *entry;
61 if ((err = snd_info_card_register(card)) < 0) {
62 snd_printd("unable to create card info\n");
63 return err;
65 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
66 snd_printd("unable to create card entry\n");
67 return err;
69 entry->c.text.read_size = PAGE_SIZE;
70 entry->c.text.read = snd_card_id_read;
71 if (snd_info_register(entry) < 0) {
72 snd_info_free_entry(entry);
73 entry = NULL;
75 card->proc_id = entry;
76 return 0;
78 #else /* !CONFIG_PROC_FS */
79 #define init_info_for_card(card)
80 #endif
82 static void snd_card_free_thread(void * __card);
84 /**
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
91 * Creates and initializes a soundcard structure.
93 * Returns kmallocated snd_card structure. Creates the ALSA control interface
94 * (which is blocked until snd_card_register function is called).
96 struct snd_card *snd_card_new(int idx, const char *xid,
97 struct module *module, int extra_size)
99 struct snd_card *card;
100 int err;
102 if (extra_size < 0)
103 extra_size = 0;
104 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
105 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));
112 err = 0;
113 write_lock(&snd_card_rwlock);
114 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;
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) {
131 write_unlock(&snd_card_rwlock);
132 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
133 goto __error;
135 snd_cards_lock |= 1 << idx; /* lock it */
136 write_unlock(&snd_card_rwlock);
137 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);
146 INIT_WORK(&card->free_workq, snd_card_free_thread, card);
147 #ifdef CONFIG_PM
148 mutex_init(&card->power_lock);
149 init_waitqueue_head(&card->power_sleep);
150 #endif
151 /* the control interface cannot be accessed from the user space until */
152 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
153 if ((err = snd_ctl_create(card)) < 0) {
154 snd_printd("unable to register control minors\n");
155 goto __error;
157 if ((err = snd_info_card_create(card)) < 0) {
158 snd_printd("unable to create card info\n");
159 goto __error_ctl;
161 if (extra_size > 0)
162 card->private_data = (char *)card + sizeof(struct snd_card);
163 return card;
165 __error_ctl:
166 snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
167 __error:
168 kfree(card);
169 return NULL;
172 static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
174 return -ENODEV;
177 static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
178 size_t count, loff_t *offset)
180 return -ENODEV;
183 static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
184 size_t count, loff_t *offset)
186 return -ENODEV;
189 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
191 return POLLERR | POLLNVAL;
194 static long snd_disconnect_ioctl(struct file *file,
195 unsigned int cmd, unsigned long arg)
197 return -ENODEV;
200 static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
202 return -ENODEV;
205 static int snd_disconnect_fasync(int fd, struct file *file, int on)
207 return -ENODEV;
211 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
212 * @card: soundcard structure
214 * Disconnects all APIs from the file-operations (user space).
216 * Returns zero, otherwise a negative error code.
218 * Note: The current implementation replaces all active file->f_op with special
219 * dummy file operations (they do nothing except release).
221 int snd_card_disconnect(struct snd_card *card)
223 struct snd_monitor_file *mfile;
224 struct file *file;
225 struct snd_shutdown_f_ops *s_f_ops;
226 struct file_operations *f_ops, *old_f_ops;
227 int err;
229 spin_lock(&card->files_lock);
230 if (card->shutdown) {
231 spin_unlock(&card->files_lock);
232 return 0;
234 card->shutdown = 1;
235 spin_unlock(&card->files_lock);
237 /* phase 1: disable fops (user space) operations for ALSA API */
238 write_lock(&snd_card_rwlock);
239 snd_cards[card->number] = NULL;
240 write_unlock(&snd_card_rwlock);
242 /* phase 2: replace file->f_op with special dummy operations */
244 spin_lock(&card->files_lock);
245 mfile = card->files;
246 while (mfile) {
247 file = mfile->file;
249 /* it's critical part, use endless loop */
250 /* we have no room to fail */
251 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
252 if (s_f_ops == NULL)
253 panic("Atomic allocation failed for snd_shutdown_f_ops!");
255 f_ops = &s_f_ops->f_ops;
257 memset(f_ops, 0, sizeof(*f_ops));
258 f_ops->owner = file->f_op->owner;
259 f_ops->release = file->f_op->release;
260 f_ops->llseek = snd_disconnect_llseek;
261 f_ops->read = snd_disconnect_read;
262 f_ops->write = snd_disconnect_write;
263 f_ops->poll = snd_disconnect_poll;
264 f_ops->unlocked_ioctl = snd_disconnect_ioctl;
265 #ifdef CONFIG_COMPAT
266 f_ops->compat_ioctl = snd_disconnect_ioctl;
267 #endif
268 f_ops->mmap = snd_disconnect_mmap;
269 f_ops->fasync = snd_disconnect_fasync;
271 s_f_ops->next = card->s_f_ops;
272 card->s_f_ops = s_f_ops;
274 f_ops = fops_get(f_ops);
276 old_f_ops = file->f_op;
277 file->f_op = f_ops; /* must be atomic */
278 fops_put(old_f_ops);
280 mfile = mfile->next;
282 spin_unlock(&card->files_lock);
284 /* phase 3: notify all connected devices about disconnection */
285 /* at this point, they cannot respond to any calls except release() */
287 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
288 if (snd_mixer_oss_notify_callback)
289 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
290 #endif
292 /* notify all devices that we are disconnected */
293 err = snd_device_disconnect_all(card);
294 if (err < 0)
295 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
297 return 0;
301 * snd_card_free - frees given soundcard structure
302 * @card: soundcard structure
304 * This function releases the soundcard structure and the all assigned
305 * devices automatically. That is, you don't have to release the devices
306 * by yourself.
308 * Returns zero. Frees all associated devices and frees the control
309 * interface associated to given soundcard.
311 int snd_card_free(struct snd_card *card)
313 struct snd_shutdown_f_ops *s_f_ops;
315 if (card == NULL)
316 return -EINVAL;
317 write_lock(&snd_card_rwlock);
318 snd_cards[card->number] = NULL;
319 write_unlock(&snd_card_rwlock);
321 #ifdef CONFIG_PM
322 wake_up(&card->power_sleep);
323 #endif
324 /* wait, until all devices are ready for the free operation */
325 wait_event(card->shutdown_sleep, card->files == NULL);
327 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
328 if (snd_mixer_oss_notify_callback)
329 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
330 #endif
331 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
332 snd_printk(KERN_ERR "unable to free all devices (pre)\n");
333 /* Fatal, but this situation should never occur */
335 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
336 snd_printk(KERN_ERR "unable to free all devices (normal)\n");
337 /* Fatal, but this situation should never occur */
339 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
340 snd_printk(KERN_ERR "unable to free all devices (post)\n");
341 /* Fatal, but this situation should never occur */
343 if (card->private_free)
344 card->private_free(card);
345 snd_info_unregister(card->proc_id);
346 if (snd_info_card_free(card) < 0) {
347 snd_printk(KERN_WARNING "unable to free card info\n");
348 /* Not fatal error */
350 while (card->s_f_ops) {
351 s_f_ops = card->s_f_ops;
352 card->s_f_ops = s_f_ops->next;
353 kfree(s_f_ops);
355 write_lock(&snd_card_rwlock);
356 snd_cards_lock &= ~(1 << card->number);
357 write_unlock(&snd_card_rwlock);
358 kfree(card);
359 return 0;
362 static void snd_card_free_thread(void * __card)
364 struct snd_card *card = __card;
365 struct module * module = card->module;
367 if (!try_module_get(module)) {
368 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
369 module = NULL;
372 snd_card_free(card);
374 module_put(module);
378 * snd_card_free_in_thread - call snd_card_free() in thread
379 * @card: soundcard structure
381 * This function schedules the call of snd_card_free() function in a
382 * work queue. When all devices are released (non-busy), the work
383 * is woken up and calls snd_card_free().
385 * When a card can be disconnected at any time by hotplug service,
386 * this function should be used in disconnect (or detach) callback
387 * instead of calling snd_card_free() directly.
389 * Returns - zero otherwise a negative error code if the start of thread failed.
391 int snd_card_free_in_thread(struct snd_card *card)
393 if (card->files == NULL) {
394 snd_card_free(card);
395 return 0;
398 if (schedule_work(&card->free_workq))
399 return 0;
401 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
402 /* try to free the structure immediately */
403 snd_card_free(card);
404 return -EFAULT;
407 static void choose_default_id(struct snd_card *card)
409 int i, len, idx_flag = 0, loops = SNDRV_CARDS;
410 char *id, *spos;
412 id = spos = card->shortname;
413 while (*id != '\0') {
414 if (*id == ' ')
415 spos = id + 1;
416 id++;
418 id = card->id;
419 while (*spos != '\0' && !isalnum(*spos))
420 spos++;
421 if (isdigit(*spos))
422 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
423 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
424 if (isalnum(*spos))
425 *id++ = *spos;
426 spos++;
428 *id = '\0';
430 id = card->id;
432 if (*id == '\0')
433 strcpy(id, "default");
435 while (1) {
436 if (loops-- == 0) {
437 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
438 strcpy(card->id, card->proc_root->name);
439 return;
441 if (!snd_info_check_reserved_words(id))
442 goto __change;
443 for (i = 0; i < snd_ecards_limit; i++) {
444 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
445 goto __change;
447 break;
449 __change:
450 len = strlen(id);
451 if (idx_flag) {
452 if (id[len-1] != '9')
453 id[len-1]++;
454 else
455 id[len-1] = 'A';
456 } else if ((size_t)len <= sizeof(card->id) - 3) {
457 strcat(id, "_1");
458 idx_flag++;
459 } else {
460 spos = id + len - 2;
461 if ((size_t)len <= sizeof(card->id) - 2)
462 spos++;
463 *spos++ = '_';
464 *spos++ = '1';
465 *spos++ = '\0';
466 idx_flag++;
472 * snd_card_register - register the soundcard
473 * @card: soundcard structure
475 * This function registers all the devices assigned to the soundcard.
476 * Until calling this, the ALSA control interface is blocked from the
477 * external accesses. Thus, you should call this function at the end
478 * of the initialization of the card.
480 * Returns zero otherwise a negative error code if the registrain failed.
482 int snd_card_register(struct snd_card *card)
484 int err;
486 snd_assert(card != NULL, return -EINVAL);
487 if ((err = snd_device_register_all(card)) < 0)
488 return err;
489 write_lock(&snd_card_rwlock);
490 if (snd_cards[card->number]) {
491 /* already registered */
492 write_unlock(&snd_card_rwlock);
493 return 0;
495 if (card->id[0] == '\0')
496 choose_default_id(card);
497 snd_cards[card->number] = card;
498 write_unlock(&snd_card_rwlock);
499 init_info_for_card(card);
500 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
501 if (snd_mixer_oss_notify_callback)
502 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
503 #endif
504 return 0;
507 #ifdef CONFIG_PROC_FS
508 static struct snd_info_entry *snd_card_info_entry = NULL;
510 static void snd_card_info_read(struct snd_info_entry *entry,
511 struct snd_info_buffer *buffer)
513 int idx, count;
514 struct snd_card *card;
516 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
517 read_lock(&snd_card_rwlock);
518 if ((card = snd_cards[idx]) != NULL) {
519 count++;
520 snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
521 idx,
522 card->id,
523 card->driver,
524 card->shortname);
525 snd_iprintf(buffer, " %s\n",
526 card->longname);
528 read_unlock(&snd_card_rwlock);
530 if (!count)
531 snd_iprintf(buffer, "--- no soundcards ---\n");
534 #ifdef CONFIG_SND_OSSEMUL
536 void snd_card_info_read_oss(struct snd_info_buffer *buffer)
538 int idx, count;
539 struct snd_card *card;
541 for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
542 read_lock(&snd_card_rwlock);
543 if ((card = snd_cards[idx]) != NULL) {
544 count++;
545 snd_iprintf(buffer, "%s\n", card->longname);
547 read_unlock(&snd_card_rwlock);
549 if (!count) {
550 snd_iprintf(buffer, "--- no soundcards ---\n");
554 #endif
556 #ifdef MODULE
557 static struct snd_info_entry *snd_card_module_info_entry;
558 static void snd_card_module_info_read(struct snd_info_entry *entry,
559 struct snd_info_buffer *buffer)
561 int idx;
562 struct snd_card *card;
564 for (idx = 0; idx < SNDRV_CARDS; idx++) {
565 read_lock(&snd_card_rwlock);
566 if ((card = snd_cards[idx]) != NULL)
567 snd_iprintf(buffer, "%2i %s\n",
568 idx, card->module->name);
569 read_unlock(&snd_card_rwlock);
572 #endif
574 int __init snd_card_info_init(void)
576 struct snd_info_entry *entry;
578 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
579 if (! entry)
580 return -ENOMEM;
581 entry->c.text.read_size = PAGE_SIZE;
582 entry->c.text.read = snd_card_info_read;
583 if (snd_info_register(entry) < 0) {
584 snd_info_free_entry(entry);
585 return -ENOMEM;
587 snd_card_info_entry = entry;
589 #ifdef MODULE
590 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
591 if (entry) {
592 entry->c.text.read_size = PAGE_SIZE;
593 entry->c.text.read = snd_card_module_info_read;
594 if (snd_info_register(entry) < 0)
595 snd_info_free_entry(entry);
596 else
597 snd_card_module_info_entry = entry;
599 #endif
601 return 0;
604 int __exit snd_card_info_done(void)
606 snd_info_unregister(snd_card_info_entry);
607 #ifdef MODULE
608 snd_info_unregister(snd_card_module_info_entry);
609 #endif
610 return 0;
613 #endif /* CONFIG_PROC_FS */
616 * snd_component_add - add a component string
617 * @card: soundcard structure
618 * @component: the component id string
620 * This function adds the component id string to the supported list.
621 * The component can be referred from the alsa-lib.
623 * Returns zero otherwise a negative error code.
626 int snd_component_add(struct snd_card *card, const char *component)
628 char *ptr;
629 int len = strlen(component);
631 ptr = strstr(card->components, component);
632 if (ptr != NULL) {
633 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
634 return 1;
636 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
637 snd_BUG();
638 return -ENOMEM;
640 if (card->components[0] != '\0')
641 strcat(card->components, " ");
642 strcat(card->components, component);
643 return 0;
647 * snd_card_file_add - add the file to the file list of the card
648 * @card: soundcard structure
649 * @file: file pointer
651 * This function adds the file to the file linked-list of the card.
652 * This linked-list is used to keep tracking the connection state,
653 * and to avoid the release of busy resources by hotplug.
655 * Returns zero or a negative error code.
657 int snd_card_file_add(struct snd_card *card, struct file *file)
659 struct snd_monitor_file *mfile;
661 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
662 if (mfile == NULL)
663 return -ENOMEM;
664 mfile->file = file;
665 mfile->next = NULL;
666 spin_lock(&card->files_lock);
667 if (card->shutdown) {
668 spin_unlock(&card->files_lock);
669 kfree(mfile);
670 return -ENODEV;
672 mfile->next = card->files;
673 card->files = mfile;
674 spin_unlock(&card->files_lock);
675 return 0;
679 * snd_card_file_remove - remove the file from the file list
680 * @card: soundcard structure
681 * @file: file pointer
683 * This function removes the file formerly added to the card via
684 * snd_card_file_add() function.
685 * If all files are removed and the release of the card is
686 * scheduled, it will wake up the the thread to call snd_card_free()
687 * (see snd_card_free_in_thread() function).
689 * Returns zero or a negative error code.
691 int snd_card_file_remove(struct snd_card *card, struct file *file)
693 struct snd_monitor_file *mfile, *pfile = NULL;
695 spin_lock(&card->files_lock);
696 mfile = card->files;
697 while (mfile) {
698 if (mfile->file == file) {
699 if (pfile)
700 pfile->next = mfile->next;
701 else
702 card->files = mfile->next;
703 break;
705 pfile = mfile;
706 mfile = mfile->next;
708 spin_unlock(&card->files_lock);
709 if (card->files == NULL)
710 wake_up(&card->shutdown_sleep);
711 if (!mfile) {
712 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
713 return -ENOENT;
715 kfree(mfile);
716 return 0;
719 #ifdef CONFIG_PM
721 * snd_power_wait - wait until the power-state is changed.
722 * @card: soundcard structure
723 * @power_state: expected power state
724 * @file: file structure for the O_NONBLOCK check (optional)
726 * Waits until the power-state is changed.
728 * Note: the power lock must be active before call.
730 int snd_power_wait(struct snd_card *card, unsigned int power_state, struct file *file)
732 wait_queue_t wait;
733 int result = 0;
735 /* fastpath */
736 if (snd_power_get_state(card) == power_state)
737 return 0;
738 init_waitqueue_entry(&wait, current);
739 add_wait_queue(&card->power_sleep, &wait);
740 while (1) {
741 if (card->shutdown) {
742 result = -ENODEV;
743 break;
745 if (snd_power_get_state(card) == power_state)
746 break;
747 #if 0 /* block all devices */
748 if (file && (file->f_flags & O_NONBLOCK)) {
749 result = -EAGAIN;
750 break;
752 #endif
753 set_current_state(TASK_UNINTERRUPTIBLE);
754 snd_power_unlock(card);
755 schedule_timeout(30 * HZ);
756 snd_power_lock(card);
758 remove_wait_queue(&card->power_sleep, &wait);
759 return result;
762 #endif /* CONFIG_PM */