Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <sound/core.h> |
| 33 | #include <sound/control.h> |
| 34 | #include <sound/info.h> |
| 35 | |
| 36 | struct snd_shutdown_f_ops { |
| 37 | struct file_operations f_ops; |
| 38 | struct snd_shutdown_f_ops *next; |
| 39 | }; |
| 40 | |
| 41 | unsigned int snd_cards_lock = 0; /* locked for registering/using */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 42 | struct snd_card *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | DEFINE_RWLOCK(snd_card_rwlock); |
| 44 | |
| 45 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 46 | int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #endif |
| 48 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 49 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 50 | static void snd_card_id_read(struct snd_info_entry *entry, |
| 51 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
| 53 | snd_iprintf(buffer, "%s\n", entry->card->id); |
| 54 | } |
| 55 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 56 | static inline int init_info_for_card(struct snd_card *card) |
| 57 | { |
| 58 | int err; |
| 59 | struct snd_info_entry *entry; |
| 60 | |
| 61 | if ((err = snd_info_card_register(card)) < 0) { |
| 62 | snd_printd("unable to create card info\n"); |
| 63 | return err; |
| 64 | } |
| 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; |
| 68 | } |
| 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; |
| 74 | } |
| 75 | card->proc_id = entry; |
| 76 | return 0; |
| 77 | } |
| 78 | #else /* !CONFIG_PROC_FS */ |
| 79 | #define init_info_for_card(card) |
| 80 | #endif |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | static void snd_card_free_thread(void * __card); |
| 83 | |
| 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 |
| 90 | * |
| 91 | * Creates and initializes a soundcard structure. |
| 92 | * |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 93 | * Returns kmallocated snd_card structure. Creates the ALSA control interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * (which is blocked until snd_card_register function is called). |
| 95 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 96 | struct snd_card *snd_card_new(int idx, const char *xid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | struct module *module, int extra_size) |
| 98 | { |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 99 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | int err; |
| 101 | |
| 102 | if (extra_size < 0) |
| 103 | extra_size = 0; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 104 | card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)); |
| 111 | } |
| 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; |
| 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) { |
| 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; |
| 134 | } |
| 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 |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 148 | mutex_init(&card->power_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 156 | } |
| 157 | if ((err = snd_info_card_create(card)) < 0) { |
| 158 | snd_printd("unable to create card info\n"); |
| 159 | goto __error_ctl; |
| 160 | } |
| 161 | if (extra_size > 0) |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 162 | card->private_data = (char *)card + sizeof(struct snd_card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return card; |
| 164 | |
| 165 | __error_ctl: |
| 166 | snd_device_free_all(card, SNDRV_DEV_CMD_PRE); |
| 167 | __error: |
| 168 | kfree(card); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
Clemens Ladisch | b3b0abe | 2006-03-03 14:08:43 +0100 | [diff] [blame] | 172 | static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig) |
| 173 | { |
| 174 | return -ENODEV; |
| 175 | } |
| 176 | |
| 177 | static ssize_t snd_disconnect_read(struct file *file, char __user *buf, |
| 178 | size_t count, loff_t *offset) |
| 179 | { |
| 180 | return -ENODEV; |
| 181 | } |
| 182 | |
| 183 | static ssize_t snd_disconnect_write(struct file *file, const char __user *buf, |
| 184 | size_t count, loff_t *offset) |
| 185 | { |
| 186 | return -ENODEV; |
| 187 | } |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait) |
| 190 | { |
| 191 | return POLLERR | POLLNVAL; |
| 192 | } |
| 193 | |
Clemens Ladisch | b3b0abe | 2006-03-03 14:08:43 +0100 | [diff] [blame] | 194 | static long snd_disconnect_ioctl(struct file *file, |
| 195 | unsigned int cmd, unsigned long arg) |
| 196 | { |
| 197 | return -ENODEV; |
| 198 | } |
| 199 | |
| 200 | static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma) |
| 201 | { |
| 202 | return -ENODEV; |
| 203 | } |
| 204 | |
| 205 | static int snd_disconnect_fasync(int fd, struct file *file, int on) |
| 206 | { |
| 207 | return -ENODEV; |
| 208 | } |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /** |
| 211 | * snd_card_disconnect - disconnect all APIs from the file-operations (user space) |
| 212 | * @card: soundcard structure |
| 213 | * |
| 214 | * Disconnects all APIs from the file-operations (user space). |
| 215 | * |
| 216 | * Returns zero, otherwise a negative error code. |
| 217 | * |
| 218 | * Note: The current implementation replaces all active file->f_op with special |
| 219 | * dummy file operations (they do nothing except release). |
| 220 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 221 | int snd_card_disconnect(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
| 223 | struct snd_monitor_file *mfile; |
| 224 | struct file *file; |
| 225 | struct snd_shutdown_f_ops *s_f_ops; |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 226 | struct file_operations *f_ops; |
| 227 | const struct file_operations *old_f_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | int err; |
| 229 | |
| 230 | spin_lock(&card->files_lock); |
| 231 | if (card->shutdown) { |
| 232 | spin_unlock(&card->files_lock); |
| 233 | return 0; |
| 234 | } |
| 235 | card->shutdown = 1; |
| 236 | spin_unlock(&card->files_lock); |
| 237 | |
| 238 | /* phase 1: disable fops (user space) operations for ALSA API */ |
| 239 | write_lock(&snd_card_rwlock); |
| 240 | snd_cards[card->number] = NULL; |
| 241 | write_unlock(&snd_card_rwlock); |
| 242 | |
| 243 | /* phase 2: replace file->f_op with special dummy operations */ |
| 244 | |
| 245 | spin_lock(&card->files_lock); |
| 246 | mfile = card->files; |
| 247 | while (mfile) { |
| 248 | file = mfile->file; |
| 249 | |
| 250 | /* it's critical part, use endless loop */ |
| 251 | /* we have no room to fail */ |
| 252 | s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC); |
| 253 | if (s_f_ops == NULL) |
| 254 | panic("Atomic allocation failed for snd_shutdown_f_ops!"); |
| 255 | |
| 256 | f_ops = &s_f_ops->f_ops; |
| 257 | |
| 258 | memset(f_ops, 0, sizeof(*f_ops)); |
| 259 | f_ops->owner = file->f_op->owner; |
| 260 | f_ops->release = file->f_op->release; |
Clemens Ladisch | b3b0abe | 2006-03-03 14:08:43 +0100 | [diff] [blame] | 261 | f_ops->llseek = snd_disconnect_llseek; |
| 262 | f_ops->read = snd_disconnect_read; |
| 263 | f_ops->write = snd_disconnect_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | f_ops->poll = snd_disconnect_poll; |
Clemens Ladisch | b3b0abe | 2006-03-03 14:08:43 +0100 | [diff] [blame] | 265 | f_ops->unlocked_ioctl = snd_disconnect_ioctl; |
| 266 | #ifdef CONFIG_COMPAT |
| 267 | f_ops->compat_ioctl = snd_disconnect_ioctl; |
| 268 | #endif |
| 269 | f_ops->mmap = snd_disconnect_mmap; |
| 270 | f_ops->fasync = snd_disconnect_fasync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | s_f_ops->next = card->s_f_ops; |
| 273 | card->s_f_ops = s_f_ops; |
| 274 | |
| 275 | f_ops = fops_get(f_ops); |
| 276 | |
| 277 | old_f_ops = file->f_op; |
| 278 | file->f_op = f_ops; /* must be atomic */ |
| 279 | fops_put(old_f_ops); |
| 280 | |
| 281 | mfile = mfile->next; |
| 282 | } |
| 283 | spin_unlock(&card->files_lock); |
| 284 | |
| 285 | /* phase 3: notify all connected devices about disconnection */ |
| 286 | /* at this point, they cannot respond to any calls except release() */ |
| 287 | |
| 288 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) |
| 289 | if (snd_mixer_oss_notify_callback) |
| 290 | snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT); |
| 291 | #endif |
| 292 | |
| 293 | /* notify all devices that we are disconnected */ |
| 294 | err = snd_device_disconnect_all(card); |
| 295 | if (err < 0) |
| 296 | snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | /** |
| 302 | * snd_card_free - frees given soundcard structure |
| 303 | * @card: soundcard structure |
| 304 | * |
| 305 | * This function releases the soundcard structure and the all assigned |
| 306 | * devices automatically. That is, you don't have to release the devices |
| 307 | * by yourself. |
| 308 | * |
| 309 | * Returns zero. Frees all associated devices and frees the control |
| 310 | * interface associated to given soundcard. |
| 311 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 312 | int snd_card_free(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | struct snd_shutdown_f_ops *s_f_ops; |
| 315 | |
| 316 | if (card == NULL) |
| 317 | return -EINVAL; |
| 318 | write_lock(&snd_card_rwlock); |
| 319 | snd_cards[card->number] = NULL; |
| 320 | write_unlock(&snd_card_rwlock); |
| 321 | |
| 322 | #ifdef CONFIG_PM |
| 323 | wake_up(&card->power_sleep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /* wait, until all devices are ready for the free operation */ |
| 326 | wait_event(card->shutdown_sleep, card->files == NULL); |
| 327 | |
| 328 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) |
| 329 | if (snd_mixer_oss_notify_callback) |
| 330 | snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE); |
| 331 | #endif |
| 332 | if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) { |
| 333 | snd_printk(KERN_ERR "unable to free all devices (pre)\n"); |
| 334 | /* Fatal, but this situation should never occur */ |
| 335 | } |
| 336 | if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) { |
| 337 | snd_printk(KERN_ERR "unable to free all devices (normal)\n"); |
| 338 | /* Fatal, but this situation should never occur */ |
| 339 | } |
| 340 | if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) { |
| 341 | snd_printk(KERN_ERR "unable to free all devices (post)\n"); |
| 342 | /* Fatal, but this situation should never occur */ |
| 343 | } |
| 344 | if (card->private_free) |
| 345 | card->private_free(card); |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 346 | snd_info_unregister(card->proc_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | if (snd_info_card_free(card) < 0) { |
| 348 | snd_printk(KERN_WARNING "unable to free card info\n"); |
| 349 | /* Not fatal error */ |
| 350 | } |
| 351 | while (card->s_f_ops) { |
| 352 | s_f_ops = card->s_f_ops; |
| 353 | card->s_f_ops = s_f_ops->next; |
| 354 | kfree(s_f_ops); |
| 355 | } |
| 356 | write_lock(&snd_card_rwlock); |
| 357 | snd_cards_lock &= ~(1 << card->number); |
| 358 | write_unlock(&snd_card_rwlock); |
| 359 | kfree(card); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static void snd_card_free_thread(void * __card) |
| 364 | { |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 365 | struct snd_card *card = __card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | struct module * module = card->module; |
| 367 | |
| 368 | if (!try_module_get(module)) { |
| 369 | snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number); |
| 370 | module = NULL; |
| 371 | } |
| 372 | |
| 373 | snd_card_free(card); |
| 374 | |
| 375 | module_put(module); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * snd_card_free_in_thread - call snd_card_free() in thread |
| 380 | * @card: soundcard structure |
| 381 | * |
| 382 | * This function schedules the call of snd_card_free() function in a |
| 383 | * work queue. When all devices are released (non-busy), the work |
| 384 | * is woken up and calls snd_card_free(). |
| 385 | * |
| 386 | * When a card can be disconnected at any time by hotplug service, |
| 387 | * this function should be used in disconnect (or detach) callback |
| 388 | * instead of calling snd_card_free() directly. |
| 389 | * |
| 390 | * Returns - zero otherwise a negative error code if the start of thread failed. |
| 391 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 392 | int snd_card_free_in_thread(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
| 394 | if (card->files == NULL) { |
| 395 | snd_card_free(card); |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | if (schedule_work(&card->free_workq)) |
| 400 | return 0; |
| 401 | |
| 402 | snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number); |
| 403 | /* try to free the structure immediately */ |
| 404 | snd_card_free(card); |
| 405 | return -EFAULT; |
| 406 | } |
| 407 | |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 408 | static void choose_default_id(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 410 | int i, len, idx_flag = 0, loops = SNDRV_CARDS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | char *id, *spos; |
| 412 | |
| 413 | id = spos = card->shortname; |
| 414 | while (*id != '\0') { |
| 415 | if (*id == ' ') |
| 416 | spos = id + 1; |
| 417 | id++; |
| 418 | } |
| 419 | id = card->id; |
| 420 | while (*spos != '\0' && !isalnum(*spos)) |
| 421 | spos++; |
| 422 | if (isdigit(*spos)) |
| 423 | *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D'; |
| 424 | while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) { |
| 425 | if (isalnum(*spos)) |
| 426 | *id++ = *spos; |
| 427 | spos++; |
| 428 | } |
| 429 | *id = '\0'; |
| 430 | |
| 431 | id = card->id; |
| 432 | |
| 433 | if (*id == '\0') |
| 434 | strcpy(id, "default"); |
| 435 | |
| 436 | while (1) { |
| 437 | if (loops-- == 0) { |
| 438 | snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id); |
| 439 | strcpy(card->id, card->proc_root->name); |
| 440 | return; |
| 441 | } |
| 442 | if (!snd_info_check_reserved_words(id)) |
| 443 | goto __change; |
| 444 | for (i = 0; i < snd_ecards_limit; i++) { |
| 445 | if (snd_cards[i] && !strcmp(snd_cards[i]->id, id)) |
| 446 | goto __change; |
| 447 | } |
| 448 | break; |
| 449 | |
| 450 | __change: |
| 451 | len = strlen(id); |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 452 | if (idx_flag) { |
| 453 | if (id[len-1] != '9') |
| 454 | id[len-1]++; |
| 455 | else |
| 456 | id[len-1] = 'A'; |
| 457 | } else if ((size_t)len <= sizeof(card->id) - 3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | strcat(id, "_1"); |
| 459 | idx_flag++; |
| 460 | } else { |
| 461 | spos = id + len - 2; |
| 462 | if ((size_t)len <= sizeof(card->id) - 2) |
| 463 | spos++; |
| 464 | *spos++ = '_'; |
| 465 | *spos++ = '1'; |
| 466 | *spos++ = '\0'; |
| 467 | idx_flag++; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * snd_card_register - register the soundcard |
| 474 | * @card: soundcard structure |
| 475 | * |
| 476 | * This function registers all the devices assigned to the soundcard. |
| 477 | * Until calling this, the ALSA control interface is blocked from the |
| 478 | * external accesses. Thus, you should call this function at the end |
| 479 | * of the initialization of the card. |
| 480 | * |
| 481 | * Returns zero otherwise a negative error code if the registrain failed. |
| 482 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 483 | int snd_card_register(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
| 485 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 487 | snd_assert(card != NULL, return -EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | if ((err = snd_device_register_all(card)) < 0) |
| 489 | return err; |
| 490 | write_lock(&snd_card_rwlock); |
| 491 | if (snd_cards[card->number]) { |
| 492 | /* already registered */ |
| 493 | write_unlock(&snd_card_rwlock); |
| 494 | return 0; |
| 495 | } |
| 496 | if (card->id[0] == '\0') |
| 497 | choose_default_id(card); |
| 498 | snd_cards[card->number] = card; |
| 499 | write_unlock(&snd_card_rwlock); |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 500 | init_info_for_card(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) |
| 502 | if (snd_mixer_oss_notify_callback) |
| 503 | snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER); |
| 504 | #endif |
| 505 | return 0; |
| 506 | } |
| 507 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 508 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 509 | static struct snd_info_entry *snd_card_info_entry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Takashi Iwai | a381a7a | 2005-11-17 15:55:49 +0100 | [diff] [blame] | 511 | static void snd_card_info_read(struct snd_info_entry *entry, |
| 512 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
| 514 | int idx, count; |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 515 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
| 517 | for (idx = count = 0; idx < SNDRV_CARDS; idx++) { |
| 518 | read_lock(&snd_card_rwlock); |
| 519 | if ((card = snd_cards[idx]) != NULL) { |
| 520 | count++; |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 521 | snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | idx, |
| 523 | card->id, |
| 524 | card->driver, |
| 525 | card->shortname); |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 526 | snd_iprintf(buffer, " %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | card->longname); |
| 528 | } |
| 529 | read_unlock(&snd_card_rwlock); |
| 530 | } |
| 531 | if (!count) |
| 532 | snd_iprintf(buffer, "--- no soundcards ---\n"); |
| 533 | } |
| 534 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 535 | #ifdef CONFIG_SND_OSSEMUL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 537 | void snd_card_info_read_oss(struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
| 539 | int idx, count; |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 540 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | for (idx = count = 0; idx < SNDRV_CARDS; idx++) { |
| 543 | read_lock(&snd_card_rwlock); |
| 544 | if ((card = snd_cards[idx]) != NULL) { |
| 545 | count++; |
| 546 | snd_iprintf(buffer, "%s\n", card->longname); |
| 547 | } |
| 548 | read_unlock(&snd_card_rwlock); |
| 549 | } |
| 550 | if (!count) { |
| 551 | snd_iprintf(buffer, "--- no soundcards ---\n"); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | #endif |
| 556 | |
| 557 | #ifdef MODULE |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 558 | static struct snd_info_entry *snd_card_module_info_entry; |
| 559 | static void snd_card_module_info_read(struct snd_info_entry *entry, |
| 560 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
| 562 | int idx; |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 563 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
| 565 | for (idx = 0; idx < SNDRV_CARDS; idx++) { |
| 566 | read_lock(&snd_card_rwlock); |
| 567 | if ((card = snd_cards[idx]) != NULL) |
Clemens Ladisch | d001544 | 2005-11-20 14:09:05 +0100 | [diff] [blame] | 568 | snd_iprintf(buffer, "%2i %s\n", |
| 569 | idx, card->module->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | read_unlock(&snd_card_rwlock); |
| 571 | } |
| 572 | } |
| 573 | #endif |
| 574 | |
| 575 | int __init snd_card_info_init(void) |
| 576 | { |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 577 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL); |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 580 | if (! entry) |
| 581 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | entry->c.text.read_size = PAGE_SIZE; |
| 583 | entry->c.text.read = snd_card_info_read; |
| 584 | if (snd_info_register(entry) < 0) { |
| 585 | snd_info_free_entry(entry); |
| 586 | return -ENOMEM; |
| 587 | } |
| 588 | snd_card_info_entry = entry; |
| 589 | |
| 590 | #ifdef MODULE |
| 591 | entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL); |
| 592 | if (entry) { |
| 593 | entry->c.text.read_size = PAGE_SIZE; |
| 594 | entry->c.text.read = snd_card_module_info_read; |
| 595 | if (snd_info_register(entry) < 0) |
| 596 | snd_info_free_entry(entry); |
| 597 | else |
| 598 | snd_card_module_info_entry = entry; |
| 599 | } |
| 600 | #endif |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | int __exit snd_card_info_done(void) |
| 606 | { |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 607 | snd_info_unregister(snd_card_info_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | #ifdef MODULE |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 609 | snd_info_unregister(snd_card_module_info_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | #endif |
| 611 | return 0; |
| 612 | } |
| 613 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 614 | #endif /* CONFIG_PROC_FS */ |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | /** |
| 617 | * snd_component_add - add a component string |
| 618 | * @card: soundcard structure |
| 619 | * @component: the component id string |
| 620 | * |
| 621 | * This function adds the component id string to the supported list. |
| 622 | * The component can be referred from the alsa-lib. |
| 623 | * |
| 624 | * Returns zero otherwise a negative error code. |
| 625 | */ |
| 626 | |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 627 | int snd_component_add(struct snd_card *card, const char *component) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
| 629 | char *ptr; |
| 630 | int len = strlen(component); |
| 631 | |
| 632 | ptr = strstr(card->components, component); |
| 633 | if (ptr != NULL) { |
| 634 | if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */ |
| 635 | return 1; |
| 636 | } |
| 637 | if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) { |
| 638 | snd_BUG(); |
| 639 | return -ENOMEM; |
| 640 | } |
| 641 | if (card->components[0] != '\0') |
| 642 | strcat(card->components, " "); |
| 643 | strcat(card->components, component); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * snd_card_file_add - add the file to the file list of the card |
| 649 | * @card: soundcard structure |
| 650 | * @file: file pointer |
| 651 | * |
| 652 | * This function adds the file to the file linked-list of the card. |
| 653 | * This linked-list is used to keep tracking the connection state, |
| 654 | * and to avoid the release of busy resources by hotplug. |
| 655 | * |
| 656 | * Returns zero or a negative error code. |
| 657 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 658 | int snd_card_file_add(struct snd_card *card, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
| 660 | struct snd_monitor_file *mfile; |
| 661 | |
| 662 | mfile = kmalloc(sizeof(*mfile), GFP_KERNEL); |
| 663 | if (mfile == NULL) |
| 664 | return -ENOMEM; |
| 665 | mfile->file = file; |
| 666 | mfile->next = NULL; |
| 667 | spin_lock(&card->files_lock); |
| 668 | if (card->shutdown) { |
| 669 | spin_unlock(&card->files_lock); |
| 670 | kfree(mfile); |
| 671 | return -ENODEV; |
| 672 | } |
| 673 | mfile->next = card->files; |
| 674 | card->files = mfile; |
| 675 | spin_unlock(&card->files_lock); |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * snd_card_file_remove - remove the file from the file list |
| 681 | * @card: soundcard structure |
| 682 | * @file: file pointer |
| 683 | * |
| 684 | * This function removes the file formerly added to the card via |
| 685 | * snd_card_file_add() function. |
| 686 | * If all files are removed and the release of the card is |
| 687 | * scheduled, it will wake up the the thread to call snd_card_free() |
| 688 | * (see snd_card_free_in_thread() function). |
| 689 | * |
| 690 | * Returns zero or a negative error code. |
| 691 | */ |
Takashi Iwai | 512bbd6 | 2005-11-17 13:51:18 +0100 | [diff] [blame] | 692 | int snd_card_file_remove(struct snd_card *card, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
| 694 | struct snd_monitor_file *mfile, *pfile = NULL; |
| 695 | |
| 696 | spin_lock(&card->files_lock); |
| 697 | mfile = card->files; |
| 698 | while (mfile) { |
| 699 | if (mfile->file == file) { |
| 700 | if (pfile) |
| 701 | pfile->next = mfile->next; |
| 702 | else |
| 703 | card->files = mfile->next; |
| 704 | break; |
| 705 | } |
| 706 | pfile = mfile; |
| 707 | mfile = mfile->next; |
| 708 | } |
| 709 | spin_unlock(&card->files_lock); |
| 710 | if (card->files == NULL) |
| 711 | wake_up(&card->shutdown_sleep); |
| 712 | if (!mfile) { |
| 713 | snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file); |
| 714 | return -ENOENT; |
| 715 | } |
| 716 | kfree(mfile); |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | #ifdef CONFIG_PM |
| 721 | /** |
| 722 | * snd_power_wait - wait until the power-state is changed. |
| 723 | * @card: soundcard structure |
| 724 | * @power_state: expected power state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | * |
| 726 | * Waits until the power-state is changed. |
| 727 | * |
| 728 | * Note: the power lock must be active before call. |
| 729 | */ |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame^] | 730 | int snd_power_wait(struct snd_card *card, unsigned int power_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | { |
| 732 | wait_queue_t wait; |
| 733 | int result = 0; |
| 734 | |
| 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; |
| 744 | } |
| 745 | if (snd_power_get_state(card) == power_state) |
| 746 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 748 | snd_power_unlock(card); |
| 749 | schedule_timeout(30 * HZ); |
| 750 | snd_power_lock(card); |
| 751 | } |
| 752 | remove_wait_queue(&card->power_sleep, &wait); |
| 753 | return result; |
| 754 | } |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | #endif /* CONFIG_PM */ |