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>
32 #include <sound/core.h>
33 #include <sound/control.h>
34 #include <sound/info.h>
36 static DEFINE_SPINLOCK(shutdown_lock
);
37 static LIST_HEAD(shutdown_files
);
39 static const struct file_operations snd_shutdown_f_ops
;
41 static unsigned int snd_cards_lock
; /* locked for registering/using */
42 struct snd_card
*snd_cards
[SNDRV_CARDS
];
43 EXPORT_SYMBOL(snd_cards
);
45 static DEFINE_MUTEX(snd_card_mutex
);
47 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
48 int (*snd_mixer_oss_notify_callback
)(struct snd_card
*card
, int free_flag
);
49 EXPORT_SYMBOL(snd_mixer_oss_notify_callback
);
53 static void snd_card_id_read(struct snd_info_entry
*entry
,
54 struct snd_info_buffer
*buffer
)
56 snd_iprintf(buffer
, "%s\n", entry
->card
->id
);
59 static inline int init_info_for_card(struct snd_card
*card
)
62 struct snd_info_entry
*entry
;
64 if ((err
= snd_info_card_register(card
)) < 0) {
65 snd_printd("unable to create card info\n");
68 if ((entry
= snd_info_create_card_entry(card
, "id", card
->proc_root
)) == NULL
) {
69 snd_printd("unable to create card entry\n");
72 entry
->c
.text
.read
= snd_card_id_read
;
73 if (snd_info_register(entry
) < 0) {
74 snd_info_free_entry(entry
);
77 card
->proc_id
= entry
;
80 #else /* !CONFIG_PROC_FS */
81 #define init_info_for_card(card)
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
;
104 card
= kzalloc(sizeof(*card
) + extra_size
, GFP_KERNEL
);
108 if (!snd_info_check_reserved_words(xid
))
110 strlcpy(card
->id
, xid
, sizeof(card
->id
));
113 mutex_lock(&snd_card_mutex
);
116 for (idx2
= 0; idx2
< SNDRV_CARDS
; idx2
++)
117 /* idx == -1 == 0xffff means: take any free slot */
118 if (~snd_cards_lock
& idx
& 1<<idx2
) {
120 if (idx
>= snd_ecards_limit
)
121 snd_ecards_limit
= idx
+ 1;
125 if (idx
< snd_ecards_limit
) {
126 if (snd_cards_lock
& (1 << idx
))
127 err
= -EBUSY
; /* invalid */
129 if (idx
< SNDRV_CARDS
)
130 snd_ecards_limit
= idx
+ 1; /* increase the limit */
135 if (idx
< 0 || err
< 0) {
136 mutex_unlock(&snd_card_mutex
);
137 snd_printk(KERN_ERR
"cannot find the slot for index %d (range 0-%i), error: %d\n",
138 idx
, snd_ecards_limit
- 1, err
);
141 snd_cards_lock
|= 1 << idx
; /* lock it */
142 mutex_unlock(&snd_card_mutex
);
144 card
->module
= module
;
145 INIT_LIST_HEAD(&card
->devices
);
146 init_rwsem(&card
->controls_rwsem
);
147 rwlock_init(&card
->ctl_files_rwlock
);
148 INIT_LIST_HEAD(&card
->controls
);
149 INIT_LIST_HEAD(&card
->ctl_files
);
150 spin_lock_init(&card
->files_lock
);
151 init_waitqueue_head(&card
->shutdown_sleep
);
153 mutex_init(&card
->power_lock
);
154 init_waitqueue_head(&card
->power_sleep
);
156 /* the control interface cannot be accessed from the user space until */
157 /* snd_cards_bitmask and snd_cards are set with snd_card_register */
158 if ((err
= snd_ctl_create(card
)) < 0) {
159 snd_printd("unable to register control minors\n");
162 if ((err
= snd_info_card_create(card
)) < 0) {
163 snd_printd("unable to create card info\n");
167 card
->private_data
= (char *)card
+ sizeof(struct snd_card
);
171 snd_device_free_all(card
, SNDRV_DEV_CMD_PRE
);
177 EXPORT_SYMBOL(snd_card_new
);
179 /* return non-zero if a card is already locked */
180 int snd_card_locked(int card
)
184 mutex_lock(&snd_card_mutex
);
185 locked
= snd_cards_lock
& (1 << card
);
186 mutex_unlock(&snd_card_mutex
);
190 static loff_t
snd_disconnect_llseek(struct file
*file
, loff_t offset
, int orig
)
195 static ssize_t
snd_disconnect_read(struct file
*file
, char __user
*buf
,
196 size_t count
, loff_t
*offset
)
201 static ssize_t
snd_disconnect_write(struct file
*file
, const char __user
*buf
,
202 size_t count
, loff_t
*offset
)
207 static int snd_disconnect_release(struct inode
*inode
, struct file
*file
)
209 struct snd_monitor_file
*df
= NULL
, *_df
;
211 spin_lock(&shutdown_lock
);
212 list_for_each_entry(_df
, &shutdown_files
, shutdown_list
) {
213 if (_df
->file
== file
) {
218 spin_unlock(&shutdown_lock
);
221 return df
->disconnected_f_op
->release(inode
, file
);
223 panic("%s(%p, %p) failed!", __FUNCTION__
, inode
, file
);
226 static unsigned int snd_disconnect_poll(struct file
* file
, poll_table
* wait
)
228 return POLLERR
| POLLNVAL
;
231 static long snd_disconnect_ioctl(struct file
*file
,
232 unsigned int cmd
, unsigned long arg
)
237 static int snd_disconnect_mmap(struct file
*file
, struct vm_area_struct
*vma
)
242 static int snd_disconnect_fasync(int fd
, struct file
*file
, int on
)
247 static const struct file_operations snd_shutdown_f_ops
=
249 .owner
= THIS_MODULE
,
250 .llseek
= snd_disconnect_llseek
,
251 .read
= snd_disconnect_read
,
252 .write
= snd_disconnect_write
,
253 .release
= snd_disconnect_release
,
254 .poll
= snd_disconnect_poll
,
255 .unlocked_ioctl
= snd_disconnect_ioctl
,
257 .compat_ioctl
= snd_disconnect_ioctl
,
259 .mmap
= snd_disconnect_mmap
,
260 .fasync
= snd_disconnect_fasync
264 * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
265 * @card: soundcard structure
267 * Disconnects all APIs from the file-operations (user space).
269 * Returns zero, otherwise a negative error code.
271 * Note: The current implementation replaces all active file->f_op with special
272 * dummy file operations (they do nothing except release).
274 int snd_card_disconnect(struct snd_card
*card
)
276 struct snd_monitor_file
*mfile
;
280 spin_lock(&card
->files_lock
);
281 if (card
->shutdown
) {
282 spin_unlock(&card
->files_lock
);
286 spin_unlock(&card
->files_lock
);
288 /* phase 1: disable fops (user space) operations for ALSA API */
289 mutex_lock(&snd_card_mutex
);
290 snd_cards
[card
->number
] = NULL
;
291 mutex_unlock(&snd_card_mutex
);
293 /* phase 2: replace file->f_op with special dummy operations */
295 spin_lock(&card
->files_lock
);
300 /* it's critical part, use endless loop */
301 /* we have no room to fail */
302 mfile
->disconnected_f_op
= mfile
->file
->f_op
;
304 spin_lock(&shutdown_lock
);
305 list_add(&mfile
->shutdown_list
, &shutdown_files
);
306 spin_unlock(&shutdown_lock
);
308 fops_get(&snd_shutdown_f_ops
);
309 mfile
->file
->f_op
= &snd_shutdown_f_ops
;
313 spin_unlock(&card
->files_lock
);
315 /* phase 3: notify all connected devices about disconnection */
316 /* at this point, they cannot respond to any calls except release() */
318 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
319 if (snd_mixer_oss_notify_callback
)
320 snd_mixer_oss_notify_callback(card
, SND_MIXER_OSS_NOTIFY_DISCONNECT
);
323 /* notify all devices that we are disconnected */
324 err
= snd_device_disconnect_all(card
);
326 snd_printk(KERN_ERR
"not all devices for card %i can be disconnected\n", card
->number
);
328 snd_info_card_disconnect(card
);
332 EXPORT_SYMBOL(snd_card_disconnect
);
335 * snd_card_free - frees given soundcard structure
336 * @card: soundcard structure
338 * This function releases the soundcard structure and the all assigned
339 * devices automatically. That is, you don't have to release the devices
342 * Returns zero. Frees all associated devices and frees the control
343 * interface associated to given soundcard.
345 static int snd_card_do_free(struct snd_card
*card
)
347 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
348 if (snd_mixer_oss_notify_callback
)
349 snd_mixer_oss_notify_callback(card
, SND_MIXER_OSS_NOTIFY_FREE
);
351 if (snd_device_free_all(card
, SNDRV_DEV_CMD_PRE
) < 0) {
352 snd_printk(KERN_ERR
"unable to free all devices (pre)\n");
353 /* Fatal, but this situation should never occur */
355 if (snd_device_free_all(card
, SNDRV_DEV_CMD_NORMAL
) < 0) {
356 snd_printk(KERN_ERR
"unable to free all devices (normal)\n");
357 /* Fatal, but this situation should never occur */
359 if (snd_device_free_all(card
, SNDRV_DEV_CMD_POST
) < 0) {
360 snd_printk(KERN_ERR
"unable to free all devices (post)\n");
361 /* Fatal, but this situation should never occur */
363 if (card
->private_free
)
364 card
->private_free(card
);
365 snd_info_free_entry(card
->proc_id
);
366 if (snd_info_card_free(card
) < 0) {
367 snd_printk(KERN_WARNING
"unable to free card info\n");
368 /* Not fatal error */
370 #ifndef CONFIG_SYSFS_DEPRECATED
372 device_unregister(card
->card_dev
);
378 static int snd_card_free_prepare(struct snd_card
*card
)
382 (void) snd_card_disconnect(card
);
383 mutex_lock(&snd_card_mutex
);
384 snd_cards
[card
->number
] = NULL
;
385 snd_cards_lock
&= ~(1 << card
->number
);
386 mutex_unlock(&snd_card_mutex
);
388 wake_up(&card
->power_sleep
);
393 int snd_card_free_when_closed(struct snd_card
*card
)
396 int ret
= snd_card_free_prepare(card
);
400 spin_lock(&card
->files_lock
);
401 if (card
->files
== NULL
)
404 card
->free_on_last_close
= 1;
405 spin_unlock(&card
->files_lock
);
408 snd_card_do_free(card
);
412 EXPORT_SYMBOL(snd_card_free_when_closed
);
414 int snd_card_free(struct snd_card
*card
)
416 int ret
= snd_card_free_prepare(card
);
420 /* wait, until all devices are ready for the free operation */
421 wait_event(card
->shutdown_sleep
, card
->files
== NULL
);
422 snd_card_do_free(card
);
426 EXPORT_SYMBOL(snd_card_free
);
428 static void choose_default_id(struct snd_card
*card
)
430 int i
, len
, idx_flag
= 0, loops
= SNDRV_CARDS
;
433 id
= spos
= card
->shortname
;
434 while (*id
!= '\0') {
440 while (*spos
!= '\0' && !isalnum(*spos
))
443 *id
++ = isalpha(card
->shortname
[0]) ? card
->shortname
[0] : 'D';
444 while (*spos
!= '\0' && (size_t)(id
- card
->id
) < sizeof(card
->id
) - 1) {
454 strcpy(id
, "default");
458 snd_printk(KERN_ERR
"unable to choose default card id (%s)\n", id
);
459 strcpy(card
->id
, card
->proc_root
->name
);
462 if (!snd_info_check_reserved_words(id
))
464 for (i
= 0; i
< snd_ecards_limit
; i
++) {
465 if (snd_cards
[i
] && !strcmp(snd_cards
[i
]->id
, id
))
473 if (id
[len
-1] != '9')
477 } else if ((size_t)len
<= sizeof(card
->id
) - 3) {
482 if ((size_t)len
<= sizeof(card
->id
) - 2)
493 * snd_card_register - register the soundcard
494 * @card: soundcard structure
496 * This function registers all the devices assigned to the soundcard.
497 * Until calling this, the ALSA control interface is blocked from the
498 * external accesses. Thus, you should call this function at the end
499 * of the initialization of the card.
501 * Returns zero otherwise a negative error code if the registrain failed.
503 int snd_card_register(struct snd_card
*card
)
507 snd_assert(card
!= NULL
, return -EINVAL
);
508 #ifndef CONFIG_SYSFS_DEPRECATED
509 if (!card
->card_dev
) {
510 card
->card_dev
= device_create(sound_class
, card
->dev
, 0,
511 "card%i", card
->number
);
512 if (IS_ERR(card
->card_dev
))
513 card
->card_dev
= NULL
;
516 if ((err
= snd_device_register_all(card
)) < 0)
518 mutex_lock(&snd_card_mutex
);
519 if (snd_cards
[card
->number
]) {
520 /* already registered */
521 mutex_unlock(&snd_card_mutex
);
524 if (card
->id
[0] == '\0')
525 choose_default_id(card
);
526 snd_cards
[card
->number
] = card
;
527 mutex_unlock(&snd_card_mutex
);
528 init_info_for_card(card
);
529 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
530 if (snd_mixer_oss_notify_callback
)
531 snd_mixer_oss_notify_callback(card
, SND_MIXER_OSS_NOTIFY_REGISTER
);
536 EXPORT_SYMBOL(snd_card_register
);
538 #ifdef CONFIG_PROC_FS
539 static struct snd_info_entry
*snd_card_info_entry
;
541 static void snd_card_info_read(struct snd_info_entry
*entry
,
542 struct snd_info_buffer
*buffer
)
545 struct snd_card
*card
;
547 for (idx
= count
= 0; idx
< SNDRV_CARDS
; idx
++) {
548 mutex_lock(&snd_card_mutex
);
549 if ((card
= snd_cards
[idx
]) != NULL
) {
551 snd_iprintf(buffer
, "%2i [%-15s]: %s - %s\n",
556 snd_iprintf(buffer
, " %s\n",
559 mutex_unlock(&snd_card_mutex
);
562 snd_iprintf(buffer
, "--- no soundcards ---\n");
565 #ifdef CONFIG_SND_OSSEMUL
567 void snd_card_info_read_oss(struct snd_info_buffer
*buffer
)
570 struct snd_card
*card
;
572 for (idx
= count
= 0; idx
< SNDRV_CARDS
; idx
++) {
573 mutex_lock(&snd_card_mutex
);
574 if ((card
= snd_cards
[idx
]) != NULL
) {
576 snd_iprintf(buffer
, "%s\n", card
->longname
);
578 mutex_unlock(&snd_card_mutex
);
581 snd_iprintf(buffer
, "--- no soundcards ---\n");
588 static struct snd_info_entry
*snd_card_module_info_entry
;
589 static void snd_card_module_info_read(struct snd_info_entry
*entry
,
590 struct snd_info_buffer
*buffer
)
593 struct snd_card
*card
;
595 for (idx
= 0; idx
< SNDRV_CARDS
; idx
++) {
596 mutex_lock(&snd_card_mutex
);
597 if ((card
= snd_cards
[idx
]) != NULL
)
598 snd_iprintf(buffer
, "%2i %s\n",
599 idx
, card
->module
->name
);
600 mutex_unlock(&snd_card_mutex
);
605 int __init
snd_card_info_init(void)
607 struct snd_info_entry
*entry
;
609 entry
= snd_info_create_module_entry(THIS_MODULE
, "cards", NULL
);
612 entry
->c
.text
.read
= snd_card_info_read
;
613 if (snd_info_register(entry
) < 0) {
614 snd_info_free_entry(entry
);
617 snd_card_info_entry
= entry
;
620 entry
= snd_info_create_module_entry(THIS_MODULE
, "modules", NULL
);
622 entry
->c
.text
.read
= snd_card_module_info_read
;
623 if (snd_info_register(entry
) < 0)
624 snd_info_free_entry(entry
);
626 snd_card_module_info_entry
= entry
;
633 int __exit
snd_card_info_done(void)
635 snd_info_free_entry(snd_card_info_entry
);
637 snd_info_free_entry(snd_card_module_info_entry
);
642 #endif /* CONFIG_PROC_FS */
645 * snd_component_add - add a component string
646 * @card: soundcard structure
647 * @component: the component id string
649 * This function adds the component id string to the supported list.
650 * The component can be referred from the alsa-lib.
652 * Returns zero otherwise a negative error code.
655 int snd_component_add(struct snd_card
*card
, const char *component
)
658 int len
= strlen(component
);
660 ptr
= strstr(card
->components
, component
);
662 if (ptr
[len
] == '\0' || ptr
[len
] == ' ') /* already there */
665 if (strlen(card
->components
) + 1 + len
+ 1 > sizeof(card
->components
)) {
669 if (card
->components
[0] != '\0')
670 strcat(card
->components
, " ");
671 strcat(card
->components
, component
);
675 EXPORT_SYMBOL(snd_component_add
);
678 * snd_card_file_add - add the file to the file list of the card
679 * @card: soundcard structure
680 * @file: file pointer
682 * This function adds the file to the file linked-list of the card.
683 * This linked-list is used to keep tracking the connection state,
684 * and to avoid the release of busy resources by hotplug.
686 * Returns zero or a negative error code.
688 int snd_card_file_add(struct snd_card
*card
, struct file
*file
)
690 struct snd_monitor_file
*mfile
;
692 mfile
= kmalloc(sizeof(*mfile
), GFP_KERNEL
);
696 mfile
->disconnected_f_op
= NULL
;
698 spin_lock(&card
->files_lock
);
699 if (card
->shutdown
) {
700 spin_unlock(&card
->files_lock
);
704 mfile
->next
= card
->files
;
706 spin_unlock(&card
->files_lock
);
710 EXPORT_SYMBOL(snd_card_file_add
);
713 * snd_card_file_remove - remove the file from the file list
714 * @card: soundcard structure
715 * @file: file pointer
717 * This function removes the file formerly added to the card via
718 * snd_card_file_add() function.
719 * If all files are removed and snd_card_free_when_closed() was
720 * called beforehand, it processes the pending release of
723 * Returns zero or a negative error code.
725 int snd_card_file_remove(struct snd_card
*card
, struct file
*file
)
727 struct snd_monitor_file
*mfile
, *pfile
= NULL
;
730 spin_lock(&card
->files_lock
);
733 if (mfile
->file
== file
) {
735 pfile
->next
= mfile
->next
;
737 card
->files
= mfile
->next
;
743 if (mfile
&& mfile
->disconnected_f_op
) {
744 fops_put(mfile
->disconnected_f_op
);
745 spin_lock(&shutdown_lock
);
746 list_del(&mfile
->shutdown_list
);
747 spin_unlock(&shutdown_lock
);
749 if (card
->files
== NULL
)
751 spin_unlock(&card
->files_lock
);
753 wake_up(&card
->shutdown_sleep
);
754 if (card
->free_on_last_close
)
755 snd_card_do_free(card
);
758 snd_printk(KERN_ERR
"ALSA card file remove problem (%p)\n", file
);
765 EXPORT_SYMBOL(snd_card_file_remove
);
769 * snd_power_wait - wait until the power-state is changed.
770 * @card: soundcard structure
771 * @power_state: expected power state
773 * Waits until the power-state is changed.
775 * Note: the power lock must be active before call.
777 int snd_power_wait(struct snd_card
*card
, unsigned int power_state
)
783 if (snd_power_get_state(card
) == power_state
)
785 init_waitqueue_entry(&wait
, current
);
786 add_wait_queue(&card
->power_sleep
, &wait
);
788 if (card
->shutdown
) {
792 if (snd_power_get_state(card
) == power_state
)
794 set_current_state(TASK_UNINTERRUPTIBLE
);
795 snd_power_unlock(card
);
796 schedule_timeout(30 * HZ
);
797 snd_power_lock(card
);
799 remove_wait_queue(&card
->power_sleep
, &wait
);
803 EXPORT_SYMBOL(snd_power_wait
);
804 #endif /* CONFIG_PM */