Branch data Line data Source code
1 : : /*
2 : : * Initialization routines
3 : : * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/init.h>
23 : : #include <linux/sched.h>
24 : : #include <linux/module.h>
25 : : #include <linux/device.h>
26 : : #include <linux/file.h>
27 : : #include <linux/slab.h>
28 : : #include <linux/time.h>
29 : : #include <linux/ctype.h>
30 : : #include <linux/pm.h>
31 : :
32 : : #include <sound/core.h>
33 : : #include <sound/control.h>
34 : : #include <sound/info.h>
35 : :
36 : : /* monitor files for graceful shutdown (hotplug) */
37 : : struct snd_monitor_file {
38 : : struct file *file;
39 : : const struct file_operations *disconnected_f_op;
40 : : struct list_head shutdown_list; /* still need to shutdown */
41 : : struct list_head list; /* link of monitor files */
42 : : };
43 : :
44 : : static DEFINE_SPINLOCK(shutdown_lock);
45 : : static LIST_HEAD(shutdown_files);
46 : :
47 : : static const struct file_operations snd_shutdown_f_ops;
48 : :
49 : : /* locked for registering/using */
50 : : static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS);
51 : : struct snd_card *snd_cards[SNDRV_CARDS];
52 : : EXPORT_SYMBOL(snd_cards);
53 : :
54 : : static DEFINE_MUTEX(snd_card_mutex);
55 : :
56 : : static char *slots[SNDRV_CARDS];
57 : : module_param_array(slots, charp, NULL, 0444);
58 : : MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
59 : :
60 : : /* return non-zero if the given index is reserved for the given
61 : : * module via slots option
62 : : */
63 : 0 : static int module_slot_match(struct module *module, int idx)
64 : : {
65 : : int match = 1;
66 : : #ifdef MODULE
67 : : const char *s1, *s2;
68 : :
69 : : if (!module || !*module->name || !slots[idx])
70 : : return 0;
71 : :
72 : : s1 = module->name;
73 : : s2 = slots[idx];
74 : : if (*s2 == '!') {
75 : : match = 0; /* negative match */
76 : : s2++;
77 : : }
78 : : /* compare module name strings
79 : : * hyphens are handled as equivalent with underscore
80 : : */
81 : : for (;;) {
82 : : char c1 = *s1++;
83 : : char c2 = *s2++;
84 : : if (c1 == '-')
85 : : c1 = '_';
86 : : if (c2 == '-')
87 : : c2 = '_';
88 : : if (c1 != c2)
89 : : return !match;
90 : : if (!c1)
91 : : break;
92 : : }
93 : : #endif /* MODULE */
94 : 0 : return match;
95 : : }
96 : :
97 : : #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
98 : : int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
99 : : EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
100 : : #endif
101 : :
102 : : #ifdef CONFIG_PROC_FS
103 : 0 : static void snd_card_id_read(struct snd_info_entry *entry,
104 : : struct snd_info_buffer *buffer)
105 : : {
106 : 1 : snd_iprintf(buffer, "%s\n", entry->card->id);
107 : 1 : }
108 : :
109 : : static inline int init_info_for_card(struct snd_card *card)
110 : : {
111 : : int err;
112 : : struct snd_info_entry *entry;
113 : :
114 [ # # ]: 0 : if ((err = snd_info_card_register(card)) < 0) {
115 : : snd_printd("unable to create card info\n");
116 : : return err;
117 : : }
118 [ # # ]: 0 : if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
119 : : snd_printd("unable to create card entry\n");
120 : : return err;
121 : : }
122 : 0 : entry->c.text.read = snd_card_id_read;
123 [ # # ]: 0 : if (snd_info_register(entry) < 0) {
124 : 0 : snd_info_free_entry(entry);
125 : : entry = NULL;
126 : : }
127 : 0 : card->proc_id = entry;
128 : : return 0;
129 : : }
130 : : #else /* !CONFIG_PROC_FS */
131 : : #define init_info_for_card(card)
132 : : #endif
133 : :
134 : 0 : static int check_empty_slot(struct module *module, int slot)
135 : : {
136 [ # # ][ # # ]: 0 : return !slots[slot] || !*slots[slot];
137 : : }
138 : :
139 : : /* return an empty slot number (>= 0) found in the given bitmask @mask.
140 : : * @mask == -1 == 0xffffffff means: take any free slot up to 32
141 : : * when no slot is available, return the original @mask as is.
142 : : */
143 : 0 : static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int),
144 : : struct module *module)
145 : : {
146 : : int slot;
147 : :
148 [ # # ]: 0 : for (slot = 0; slot < SNDRV_CARDS; slot++) {
149 [ # # ][ # # ]: 0 : if (slot < 32 && !(mask & (1U << slot)))
150 : 0 : continue;
151 [ # # ]: 0 : if (!test_bit(slot, snd_cards_lock)) {
152 [ # # ]: 0 : if (check(module, slot))
153 : : return slot; /* found */
154 : : }
155 : : }
156 : : return mask; /* unchanged */
157 : : }
158 : :
159 : : /**
160 : : * snd_card_create - create and initialize a soundcard structure
161 : : * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
162 : : * @xid: card identification (ASCII string)
163 : : * @module: top level module for locking
164 : : * @extra_size: allocate this extra size after the main soundcard structure
165 : : * @card_ret: the pointer to store the created card instance
166 : : *
167 : : * Creates and initializes a soundcard structure.
168 : : *
169 : : * The function allocates snd_card instance via kzalloc with the given
170 : : * space for the driver to use freely. The allocated struct is stored
171 : : * in the given card_ret pointer.
172 : : *
173 : : * Return: Zero if successful or a negative error code.
174 : : */
175 : 0 : int snd_card_create(int idx, const char *xid,
176 : : struct module *module, int extra_size,
177 : : struct snd_card **card_ret)
178 : : {
179 : : struct snd_card *card;
180 : : int err;
181 : :
182 [ # # ]: 0 : if (snd_BUG_ON(!card_ret))
183 : : return -EINVAL;
184 : 0 : *card_ret = NULL;
185 : :
186 [ # # ]: 0 : if (extra_size < 0)
187 : : extra_size = 0;
188 : 0 : card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
189 [ # # ]: 0 : if (!card)
190 : : return -ENOMEM;
191 [ # # ]: 0 : if (xid)
192 : 0 : strlcpy(card->id, xid, sizeof(card->id));
193 : : err = 0;
194 : 0 : mutex_lock(&snd_card_mutex);
195 [ # # ]: 0 : if (idx < 0) /* first check the matching module-name slot */
196 : 0 : idx = get_slot_from_bitmask(idx, module_slot_match, module);
197 [ # # ]: 0 : if (idx < 0) /* if not matched, assign an empty slot */
198 : 0 : idx = get_slot_from_bitmask(idx, check_empty_slot, module);
199 [ # # ]: 0 : if (idx < 0)
200 : : err = -ENODEV;
201 [ # # ]: 0 : else if (idx < snd_ecards_limit) {
202 [ # # ]: 0 : if (test_bit(idx, snd_cards_lock))
203 : : err = -EBUSY; /* invalid */
204 [ # # ]: 0 : } else if (idx >= SNDRV_CARDS)
205 : : err = -ENODEV;
206 [ # # ]: 0 : if (err < 0) {
207 : 0 : mutex_unlock(&snd_card_mutex);
208 : 0 : snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i), error: %d\n",
209 : : idx, snd_ecards_limit - 1, err);
210 : 0 : goto __error;
211 : : }
212 : 0 : set_bit(idx, snd_cards_lock); /* lock it */
213 [ # # ]: 0 : if (idx >= snd_ecards_limit)
214 : 0 : snd_ecards_limit = idx + 1; /* increase the limit */
215 : 0 : mutex_unlock(&snd_card_mutex);
216 : 0 : card->number = idx;
217 : 0 : card->module = module;
218 : 0 : INIT_LIST_HEAD(&card->devices);
219 : 0 : init_rwsem(&card->controls_rwsem);
220 : 0 : rwlock_init(&card->ctl_files_rwlock);
221 : 0 : INIT_LIST_HEAD(&card->controls);
222 : 0 : INIT_LIST_HEAD(&card->ctl_files);
223 : 0 : spin_lock_init(&card->files_lock);
224 : 0 : INIT_LIST_HEAD(&card->files_list);
225 : 0 : init_waitqueue_head(&card->shutdown_sleep);
226 : 0 : atomic_set(&card->refcount, 0);
227 : : #ifdef CONFIG_PM
228 : 0 : mutex_init(&card->power_lock);
229 : 0 : init_waitqueue_head(&card->power_sleep);
230 : : #endif
231 : : /* the control interface cannot be accessed from the user space until */
232 : : /* snd_cards_bitmask and snd_cards are set with snd_card_register */
233 : 0 : err = snd_ctl_create(card);
234 [ # # ]: 0 : if (err < 0) {
235 : 0 : snd_printk(KERN_ERR "unable to register control minors\n");
236 : 0 : goto __error;
237 : : }
238 : 0 : err = snd_info_card_create(card);
239 [ # # ]: 0 : if (err < 0) {
240 : 0 : snd_printk(KERN_ERR "unable to create card info\n");
241 : : goto __error_ctl;
242 : : }
243 [ # # ]: 0 : if (extra_size > 0)
244 : 0 : card->private_data = (char *)card + sizeof(struct snd_card);
245 : 0 : *card_ret = card;
246 : 0 : return 0;
247 : :
248 : : __error_ctl:
249 : 0 : snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
250 : : __error:
251 : 0 : kfree(card);
252 : 0 : return err;
253 : : }
254 : : EXPORT_SYMBOL(snd_card_create);
255 : :
256 : : /* return non-zero if a card is already locked */
257 : 0 : int snd_card_locked(int card)
258 : : {
259 : : int locked;
260 : :
261 : 0 : mutex_lock(&snd_card_mutex);
262 : : locked = test_bit(card, snd_cards_lock);
263 : 0 : mutex_unlock(&snd_card_mutex);
264 : 0 : return locked;
265 : : }
266 : :
267 : 0 : static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
268 : : {
269 : 0 : return -ENODEV;
270 : : }
271 : :
272 : 0 : static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
273 : : size_t count, loff_t *offset)
274 : : {
275 : 0 : return -ENODEV;
276 : : }
277 : :
278 : 0 : static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
279 : : size_t count, loff_t *offset)
280 : : {
281 : 0 : return -ENODEV;
282 : : }
283 : :
284 : 0 : static int snd_disconnect_release(struct inode *inode, struct file *file)
285 : : {
286 : : struct snd_monitor_file *df = NULL, *_df;
287 : :
288 : : spin_lock(&shutdown_lock);
289 [ # # ]: 0 : list_for_each_entry(_df, &shutdown_files, shutdown_list) {
290 [ # # ]: 0 : if (_df->file == file) {
291 : : df = _df;
292 : : list_del_init(&df->shutdown_list);
293 : : break;
294 : : }
295 : : }
296 : : spin_unlock(&shutdown_lock);
297 : :
298 [ # # ]: 0 : if (likely(df)) {
299 [ # # ][ # # ]: 0 : if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync)
300 : 0 : df->disconnected_f_op->fasync(-1, file, 0);
301 : 0 : return df->disconnected_f_op->release(inode, file);
302 : : }
303 : :
304 : 0 : panic("%s(%p, %p) failed!", __func__, inode, file);
305 : : }
306 : :
307 : 0 : static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
308 : : {
309 : 0 : return POLLERR | POLLNVAL;
310 : : }
311 : :
312 : 0 : static long snd_disconnect_ioctl(struct file *file,
313 : : unsigned int cmd, unsigned long arg)
314 : : {
315 : 0 : return -ENODEV;
316 : : }
317 : :
318 : 0 : static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
319 : : {
320 : 0 : return -ENODEV;
321 : : }
322 : :
323 : 0 : static int snd_disconnect_fasync(int fd, struct file *file, int on)
324 : : {
325 : 0 : return -ENODEV;
326 : : }
327 : :
328 : : static const struct file_operations snd_shutdown_f_ops =
329 : : {
330 : : .owner = THIS_MODULE,
331 : : .llseek = snd_disconnect_llseek,
332 : : .read = snd_disconnect_read,
333 : : .write = snd_disconnect_write,
334 : : .release = snd_disconnect_release,
335 : : .poll = snd_disconnect_poll,
336 : : .unlocked_ioctl = snd_disconnect_ioctl,
337 : : #ifdef CONFIG_COMPAT
338 : : .compat_ioctl = snd_disconnect_ioctl,
339 : : #endif
340 : : .mmap = snd_disconnect_mmap,
341 : : .fasync = snd_disconnect_fasync
342 : : };
343 : :
344 : : /**
345 : : * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
346 : : * @card: soundcard structure
347 : : *
348 : : * Disconnects all APIs from the file-operations (user space).
349 : : *
350 : : * Return: Zero, otherwise a negative error code.
351 : : *
352 : : * Note: The current implementation replaces all active file->f_op with special
353 : : * dummy file operations (they do nothing except release).
354 : : */
355 : 0 : int snd_card_disconnect(struct snd_card *card)
356 : : {
357 : : struct snd_monitor_file *mfile;
358 : : int err;
359 : :
360 [ # # ]: 0 : if (!card)
361 : : return -EINVAL;
362 : :
363 : : spin_lock(&card->files_lock);
364 [ # # ]: 0 : if (card->shutdown) {
365 : : spin_unlock(&card->files_lock);
366 : 0 : return 0;
367 : : }
368 : 0 : card->shutdown = 1;
369 : : spin_unlock(&card->files_lock);
370 : :
371 : : /* phase 1: disable fops (user space) operations for ALSA API */
372 : 0 : mutex_lock(&snd_card_mutex);
373 : 0 : snd_cards[card->number] = NULL;
374 : 0 : clear_bit(card->number, snd_cards_lock);
375 : 0 : mutex_unlock(&snd_card_mutex);
376 : :
377 : : /* phase 2: replace file->f_op with special dummy operations */
378 : :
379 : : spin_lock(&card->files_lock);
380 [ # # ]: 0 : list_for_each_entry(mfile, &card->files_list, list) {
381 : : /* it's critical part, use endless loop */
382 : : /* we have no room to fail */
383 : 0 : mfile->disconnected_f_op = mfile->file->f_op;
384 : :
385 : : spin_lock(&shutdown_lock);
386 : 0 : list_add(&mfile->shutdown_list, &shutdown_files);
387 : : spin_unlock(&shutdown_lock);
388 : :
389 : 0 : mfile->file->f_op = &snd_shutdown_f_ops;
390 [ # # ]: 0 : fops_get(mfile->file->f_op);
391 : : }
392 : : spin_unlock(&card->files_lock);
393 : :
394 : : /* phase 3: notify all connected devices about disconnection */
395 : : /* at this point, they cannot respond to any calls except release() */
396 : :
397 : : #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
398 : : if (snd_mixer_oss_notify_callback)
399 : : snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
400 : : #endif
401 : :
402 : : /* notify all devices that we are disconnected */
403 : 0 : err = snd_device_disconnect_all(card);
404 [ # # ]: 0 : if (err < 0)
405 : 0 : snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
406 : :
407 : 0 : snd_info_card_disconnect(card);
408 [ # # ]: 0 : if (card->card_dev) {
409 : 0 : device_unregister(card->card_dev);
410 : 0 : card->card_dev = NULL;
411 : : }
412 : : #ifdef CONFIG_PM
413 : 0 : wake_up(&card->power_sleep);
414 : : #endif
415 : 0 : return 0;
416 : : }
417 : :
418 : : EXPORT_SYMBOL(snd_card_disconnect);
419 : :
420 : : /**
421 : : * snd_card_free - frees given soundcard structure
422 : : * @card: soundcard structure
423 : : *
424 : : * This function releases the soundcard structure and the all assigned
425 : : * devices automatically. That is, you don't have to release the devices
426 : : * by yourself.
427 : : *
428 : : * Return: Zero. Frees all associated devices and frees the control
429 : : * interface associated to given soundcard.
430 : : */
431 : 0 : static int snd_card_do_free(struct snd_card *card)
432 : : {
433 : : #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
434 : : if (snd_mixer_oss_notify_callback)
435 : : snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
436 : : #endif
437 [ # # ]: 0 : if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
438 : 0 : snd_printk(KERN_ERR "unable to free all devices (pre)\n");
439 : : /* Fatal, but this situation should never occur */
440 : : }
441 [ # # ]: 0 : if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
442 : 0 : snd_printk(KERN_ERR "unable to free all devices (normal)\n");
443 : : /* Fatal, but this situation should never occur */
444 : : }
445 [ # # ]: 0 : if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
446 : 0 : snd_printk(KERN_ERR "unable to free all devices (post)\n");
447 : : /* Fatal, but this situation should never occur */
448 : : }
449 [ # # ]: 0 : if (card->private_free)
450 : 0 : card->private_free(card);
451 : 0 : snd_info_free_entry(card->proc_id);
452 [ # # ]: 0 : if (snd_info_card_free(card) < 0) {
453 : 0 : snd_printk(KERN_WARNING "unable to free card info\n");
454 : : /* Not fatal error */
455 : : }
456 : 0 : kfree(card);
457 : 0 : return 0;
458 : : }
459 : :
460 : : /**
461 : : * snd_card_unref - release the reference counter
462 : : * @card: the card instance
463 : : *
464 : : * Decrements the reference counter. When it reaches to zero, wake up
465 : : * the sleeper and call the destructor if needed.
466 : : */
467 : 0 : void snd_card_unref(struct snd_card *card)
468 : : {
469 [ # # ]: 0 : if (atomic_dec_and_test(&card->refcount)) {
470 : 0 : wake_up(&card->shutdown_sleep);
471 [ # # ]: 0 : if (card->free_on_last_close)
472 : 0 : snd_card_do_free(card);
473 : : }
474 : 0 : }
475 : : EXPORT_SYMBOL(snd_card_unref);
476 : :
477 : 0 : int snd_card_free_when_closed(struct snd_card *card)
478 : : {
479 : : int ret;
480 : :
481 : 0 : atomic_inc(&card->refcount);
482 : 0 : ret = snd_card_disconnect(card);
483 [ # # ]: 0 : if (ret) {
484 : : atomic_dec(&card->refcount);
485 : 0 : return ret;
486 : : }
487 : :
488 : 0 : card->free_on_last_close = 1;
489 [ # # ]: 0 : if (atomic_dec_and_test(&card->refcount))
490 : 0 : snd_card_do_free(card);
491 : : return 0;
492 : : }
493 : :
494 : : EXPORT_SYMBOL(snd_card_free_when_closed);
495 : :
496 : 0 : int snd_card_free(struct snd_card *card)
497 : : {
498 : 0 : int ret = snd_card_disconnect(card);
499 [ # # ]: 0 : if (ret)
500 : : return ret;
501 : :
502 : : /* wait, until all devices are ready for the free operation */
503 [ # # ][ # # ]: 0 : wait_event(card->shutdown_sleep, !atomic_read(&card->refcount));
504 : 0 : snd_card_do_free(card);
505 : 0 : return 0;
506 : : }
507 : :
508 : : EXPORT_SYMBOL(snd_card_free);
509 : :
510 : : /* retrieve the last word of shortname or longname */
511 : : static const char *retrieve_id_from_card_name(const char *name)
512 : : {
513 : : const char *spos = name;
514 : :
515 [ # # ]: 0 : while (*name) {
516 [ # # ][ # # ]: 0 : if (isspace(*name) && isalnum(name[1]))
517 : 0 : spos = name + 1;
518 : 0 : name++;
519 : : }
520 : : return spos;
521 : : }
522 : :
523 : : /* return true if the given id string doesn't conflict any other card ids */
524 : 0 : static bool card_id_ok(struct snd_card *card, const char *id)
525 : : {
526 : : int i;
527 [ # # ]: 0 : if (!snd_info_check_reserved_words(id))
528 : : return false;
529 [ # # ]: 0 : for (i = 0; i < snd_ecards_limit; i++) {
530 [ # # ][ # # ]: 0 : if (snd_cards[i] && snd_cards[i] != card &&
[ # # ]
531 : 0 : !strcmp(snd_cards[i]->id, id))
532 : : return false;
533 : : }
534 : : return true;
535 : : }
536 : :
537 : : /* copy to card->id only with valid letters from nid */
538 : 0 : static void copy_valid_id_string(struct snd_card *card, const char *src,
539 : : const char *nid)
540 : : {
541 : 0 : char *id = card->id;
542 : :
543 [ # # ][ # # ]: 0 : while (*nid && !isalnum(*nid))
544 : 0 : nid++;
545 [ # # ]: 0 : if (isdigit(*nid))
546 [ # # ]: 0 : *id++ = isalpha(*src) ? *src : 'D';
547 [ # # ][ # # ]: 0 : while (*nid && (size_t)(id - card->id) < sizeof(card->id) - 1) {
548 [ # # ]: 0 : if (isalnum(*nid))
549 : 0 : *id++ = *nid;
550 : 0 : nid++;
551 : : }
552 : 0 : *id = 0;
553 : 0 : }
554 : :
555 : : /* Set card->id from the given string
556 : : * If the string conflicts with other ids, add a suffix to make it unique.
557 : : */
558 : 0 : static void snd_card_set_id_no_lock(struct snd_card *card, const char *src,
559 : : const char *nid)
560 : : {
561 : : int len, loops;
562 : : bool is_default = false;
563 : : char *id;
564 : :
565 : 0 : copy_valid_id_string(card, src, nid);
566 : 0 : id = card->id;
567 : :
568 : : again:
569 : : /* use "Default" for obviously invalid strings
570 : : * ("card" conflicts with proc directories)
571 : : */
572 [ # # ][ # # ]: 0 : if (!*id || !strncmp(id, "card", 4)) {
573 : 0 : strcpy(id, "Default");
574 : : is_default = true;
575 : : }
576 : :
577 : 0 : len = strlen(id);
578 [ # # ]: 0 : for (loops = 0; loops < SNDRV_CARDS; loops++) {
579 : : char *spos;
580 : : char sfxstr[5]; /* "_012" */
581 : : int sfxlen;
582 : :
583 [ # # ]: 0 : if (card_id_ok(card, id))
584 : 0 : return; /* OK */
585 : :
586 : : /* Add _XYZ suffix */
587 : 0 : sprintf(sfxstr, "_%X", loops + 1);
588 : 0 : sfxlen = strlen(sfxstr);
589 [ # # ]: 0 : if (len + sfxlen >= sizeof(card->id))
590 : 0 : spos = id + sizeof(card->id) - sfxlen - 1;
591 : : else
592 : 0 : spos = id + len;
593 : 0 : strcpy(spos, sfxstr);
594 : : }
595 : : /* fallback to the default id */
596 [ # # ]: 0 : if (!is_default) {
597 : 0 : *id = 0;
598 : 0 : goto again;
599 : : }
600 : : /* last resort... */
601 : 0 : snd_printk(KERN_ERR "unable to set card id (%s)\n", id);
602 [ # # ]: 0 : if (card->proc_root->name)
603 : 0 : strlcpy(card->id, card->proc_root->name, sizeof(card->id));
604 : : }
605 : :
606 : : /**
607 : : * snd_card_set_id - set card identification name
608 : : * @card: soundcard structure
609 : : * @nid: new identification string
610 : : *
611 : : * This function sets the card identification and checks for name
612 : : * collisions.
613 : : */
614 : 0 : void snd_card_set_id(struct snd_card *card, const char *nid)
615 : : {
616 : : /* check if user specified own card->id */
617 [ # # ]: 0 : if (card->id[0] != '\0')
618 : 0 : return;
619 : 0 : mutex_lock(&snd_card_mutex);
620 : 0 : snd_card_set_id_no_lock(card, nid, nid);
621 : 0 : mutex_unlock(&snd_card_mutex);
622 : : }
623 : : EXPORT_SYMBOL(snd_card_set_id);
624 : :
625 : : static ssize_t
626 : 0 : card_id_show_attr(struct device *dev,
627 : : struct device_attribute *attr, char *buf)
628 : : {
629 : 0 : struct snd_card *card = dev_get_drvdata(dev);
630 [ # # ]: 0 : return snprintf(buf, PAGE_SIZE, "%s\n", card ? card->id : "(null)");
631 : : }
632 : :
633 : : static ssize_t
634 : 0 : card_id_store_attr(struct device *dev, struct device_attribute *attr,
635 : : const char *buf, size_t count)
636 : : {
637 : 0 : struct snd_card *card = dev_get_drvdata(dev);
638 : : char buf1[sizeof(card->id)];
639 : 0 : size_t copy = count > sizeof(card->id) - 1 ?
640 : : sizeof(card->id) - 1 : count;
641 : : size_t idx;
642 : : int c;
643 : :
644 [ # # ]: 0 : for (idx = 0; idx < copy; idx++) {
645 : 0 : c = buf[idx];
646 [ # # ][ # # ]: 0 : if (!isalnum(c) && c != '_' && c != '-')
647 : : return -EINVAL;
648 : : }
649 : 0 : memcpy(buf1, buf, copy);
650 : 0 : buf1[copy] = '\0';
651 : 0 : mutex_lock(&snd_card_mutex);
652 [ # # ]: 0 : if (!card_id_ok(NULL, buf1)) {
653 : 0 : mutex_unlock(&snd_card_mutex);
654 : 0 : return -EEXIST;
655 : : }
656 : 0 : strcpy(card->id, buf1);
657 : 0 : snd_info_card_id_change(card);
658 : 0 : mutex_unlock(&snd_card_mutex);
659 : :
660 : 0 : return count;
661 : : }
662 : :
663 : : static struct device_attribute card_id_attrs =
664 : : __ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr);
665 : :
666 : : static ssize_t
667 : 0 : card_number_show_attr(struct device *dev,
668 : : struct device_attribute *attr, char *buf)
669 : : {
670 : 0 : struct snd_card *card = dev_get_drvdata(dev);
671 [ # # ]: 0 : return snprintf(buf, PAGE_SIZE, "%i\n", card ? card->number : -1);
672 : : }
673 : :
674 : : static struct device_attribute card_number_attrs =
675 : : __ATTR(number, S_IRUGO, card_number_show_attr, NULL);
676 : :
677 : : /**
678 : : * snd_card_register - register the soundcard
679 : : * @card: soundcard structure
680 : : *
681 : : * This function registers all the devices assigned to the soundcard.
682 : : * Until calling this, the ALSA control interface is blocked from the
683 : : * external accesses. Thus, you should call this function at the end
684 : : * of the initialization of the card.
685 : : *
686 : : * Return: Zero otherwise a negative error code if the registration failed.
687 : : */
688 : 0 : int snd_card_register(struct snd_card *card)
689 : : {
690 : : int err;
691 : :
692 [ # # ]: 0 : if (snd_BUG_ON(!card))
693 : : return -EINVAL;
694 : :
695 [ # # ]: 0 : if (!card->card_dev) {
696 : 0 : card->card_dev = device_create(sound_class, card->dev,
697 : : MKDEV(0, 0), card,
698 : : "card%i", card->number);
699 [ # # ]: 0 : if (IS_ERR(card->card_dev))
700 : 0 : card->card_dev = NULL;
701 : : }
702 : :
703 [ # # ]: 0 : if ((err = snd_device_register_all(card)) < 0)
704 : : return err;
705 : 0 : mutex_lock(&snd_card_mutex);
706 [ # # ]: 0 : if (snd_cards[card->number]) {
707 : : /* already registered */
708 : 0 : mutex_unlock(&snd_card_mutex);
709 : 0 : return 0;
710 : : }
711 [ # # ]: 0 : if (*card->id) {
712 : : /* make a unique id name from the given string */
713 : : char tmpid[sizeof(card->id)];
714 : 0 : memcpy(tmpid, card->id, sizeof(card->id));
715 : 0 : snd_card_set_id_no_lock(card, tmpid, tmpid);
716 : : } else {
717 : : /* create an id from either shortname or longname */
718 : : const char *src;
719 [ # # ]: 0 : src = *card->shortname ? card->shortname : card->longname;
720 : 0 : snd_card_set_id_no_lock(card, src,
721 : : retrieve_id_from_card_name(src));
722 : : }
723 : 0 : snd_cards[card->number] = card;
724 : 0 : mutex_unlock(&snd_card_mutex);
725 : : init_info_for_card(card);
726 : : #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
727 : : if (snd_mixer_oss_notify_callback)
728 : : snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
729 : : #endif
730 [ # # ]: 0 : if (card->card_dev) {
731 : 0 : err = device_create_file(card->card_dev, &card_id_attrs);
732 [ # # ]: 0 : if (err < 0)
733 : : return err;
734 : 0 : err = device_create_file(card->card_dev, &card_number_attrs);
735 [ # # ]: 0 : if (err < 0)
736 : 0 : return err;
737 : : }
738 : :
739 : : return 0;
740 : : }
741 : :
742 : : EXPORT_SYMBOL(snd_card_register);
743 : :
744 : : #ifdef CONFIG_PROC_FS
745 : : static struct snd_info_entry *snd_card_info_entry;
746 : :
747 : 0 : static void snd_card_info_read(struct snd_info_entry *entry,
748 : : struct snd_info_buffer *buffer)
749 : : {
750 : : int idx, count;
751 : : struct snd_card *card;
752 : :
753 [ + + ]: 9 : for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
754 : 8 : mutex_lock(&snd_card_mutex);
755 [ + + ]: 8 : if ((card = snd_cards[idx]) != NULL) {
756 : 1 : count++;
757 : 1 : snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
758 : : idx,
759 : 1 : card->id,
760 : 1 : card->driver,
761 : 1 : card->shortname);
762 : 1 : snd_iprintf(buffer, " %s\n",
763 : 1 : card->longname);
764 : : }
765 : 8 : mutex_unlock(&snd_card_mutex);
766 : : }
767 [ - + ]: 1 : if (!count)
768 : 0 : snd_iprintf(buffer, "--- no soundcards ---\n");
769 : 1 : }
770 : :
771 : : #ifdef CONFIG_SND_OSSEMUL
772 : :
773 : : void snd_card_info_read_oss(struct snd_info_buffer *buffer)
774 : : {
775 : : int idx, count;
776 : : struct snd_card *card;
777 : :
778 : : for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
779 : : mutex_lock(&snd_card_mutex);
780 : : if ((card = snd_cards[idx]) != NULL) {
781 : : count++;
782 : : snd_iprintf(buffer, "%s\n", card->longname);
783 : : }
784 : : mutex_unlock(&snd_card_mutex);
785 : : }
786 : : if (!count) {
787 : : snd_iprintf(buffer, "--- no soundcards ---\n");
788 : : }
789 : : }
790 : :
791 : : #endif
792 : :
793 : : #ifdef MODULE
794 : : static struct snd_info_entry *snd_card_module_info_entry;
795 : : static void snd_card_module_info_read(struct snd_info_entry *entry,
796 : : struct snd_info_buffer *buffer)
797 : : {
798 : : int idx;
799 : : struct snd_card *card;
800 : :
801 : : for (idx = 0; idx < SNDRV_CARDS; idx++) {
802 : : mutex_lock(&snd_card_mutex);
803 : : if ((card = snd_cards[idx]) != NULL)
804 : : snd_iprintf(buffer, "%2i %s\n",
805 : : idx, card->module->name);
806 : : mutex_unlock(&snd_card_mutex);
807 : : }
808 : : }
809 : : #endif
810 : :
811 : 0 : int __init snd_card_info_init(void)
812 : : {
813 : : struct snd_info_entry *entry;
814 : :
815 : 0 : entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
816 [ # # ]: 0 : if (! entry)
817 : : return -ENOMEM;
818 : 0 : entry->c.text.read = snd_card_info_read;
819 [ # # ]: 0 : if (snd_info_register(entry) < 0) {
820 : 0 : snd_info_free_entry(entry);
821 : 0 : return -ENOMEM;
822 : : }
823 : 0 : snd_card_info_entry = entry;
824 : :
825 : : #ifdef MODULE
826 : : entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
827 : : if (entry) {
828 : : entry->c.text.read = snd_card_module_info_read;
829 : : if (snd_info_register(entry) < 0)
830 : : snd_info_free_entry(entry);
831 : : else
832 : : snd_card_module_info_entry = entry;
833 : : }
834 : : #endif
835 : :
836 : 0 : return 0;
837 : : }
838 : :
839 : 0 : int __exit snd_card_info_done(void)
840 : : {
841 : 0 : snd_info_free_entry(snd_card_info_entry);
842 : : #ifdef MODULE
843 : : snd_info_free_entry(snd_card_module_info_entry);
844 : : #endif
845 : 0 : return 0;
846 : : }
847 : :
848 : : #endif /* CONFIG_PROC_FS */
849 : :
850 : : /**
851 : : * snd_component_add - add a component string
852 : : * @card: soundcard structure
853 : : * @component: the component id string
854 : : *
855 : : * This function adds the component id string to the supported list.
856 : : * The component can be referred from the alsa-lib.
857 : : *
858 : : * Return: Zero otherwise a negative error code.
859 : : */
860 : :
861 : 0 : int snd_component_add(struct snd_card *card, const char *component)
862 : : {
863 : : char *ptr;
864 : 0 : int len = strlen(component);
865 : :
866 : 0 : ptr = strstr(card->components, component);
867 [ # # ]: 0 : if (ptr != NULL) {
868 [ # # ]: 0 : if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
869 : : return 1;
870 : : }
871 [ # # ]: 0 : if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
872 : : snd_BUG();
873 : : return -ENOMEM;
874 : : }
875 [ # # ]: 0 : if (card->components[0] != '\0')
876 : 0 : strcat(card->components, " ");
877 : 0 : strcat(card->components, component);
878 : 0 : return 0;
879 : : }
880 : :
881 : : EXPORT_SYMBOL(snd_component_add);
882 : :
883 : : /**
884 : : * snd_card_file_add - add the file to the file list of the card
885 : : * @card: soundcard structure
886 : : * @file: file pointer
887 : : *
888 : : * This function adds the file to the file linked-list of the card.
889 : : * This linked-list is used to keep tracking the connection state,
890 : : * and to avoid the release of busy resources by hotplug.
891 : : *
892 : : * Return: zero or a negative error code.
893 : : */
894 : 0 : int snd_card_file_add(struct snd_card *card, struct file *file)
895 : : {
896 : : struct snd_monitor_file *mfile;
897 : :
898 : : mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
899 [ # # ]: 0 : if (mfile == NULL)
900 : : return -ENOMEM;
901 : 0 : mfile->file = file;
902 : 0 : mfile->disconnected_f_op = NULL;
903 : 0 : INIT_LIST_HEAD(&mfile->shutdown_list);
904 : : spin_lock(&card->files_lock);
905 [ # # ]: 0 : if (card->shutdown) {
906 : : spin_unlock(&card->files_lock);
907 : 0 : kfree(mfile);
908 : 0 : return -ENODEV;
909 : : }
910 : 0 : list_add(&mfile->list, &card->files_list);
911 : 0 : atomic_inc(&card->refcount);
912 : : spin_unlock(&card->files_lock);
913 : 0 : return 0;
914 : : }
915 : :
916 : : EXPORT_SYMBOL(snd_card_file_add);
917 : :
918 : : /**
919 : : * snd_card_file_remove - remove the file from the file list
920 : : * @card: soundcard structure
921 : : * @file: file pointer
922 : : *
923 : : * This function removes the file formerly added to the card via
924 : : * snd_card_file_add() function.
925 : : * If all files are removed and snd_card_free_when_closed() was
926 : : * called beforehand, it processes the pending release of
927 : : * resources.
928 : : *
929 : : * Return: Zero or a negative error code.
930 : : */
931 : 0 : int snd_card_file_remove(struct snd_card *card, struct file *file)
932 : : {
933 : : struct snd_monitor_file *mfile, *found = NULL;
934 : :
935 : : spin_lock(&card->files_lock);
936 [ # # ]: 0 : list_for_each_entry(mfile, &card->files_list, list) {
937 [ # # ]: 0 : if (mfile->file == file) {
938 : : list_del(&mfile->list);
939 : : spin_lock(&shutdown_lock);
940 : : list_del(&mfile->shutdown_list);
941 : : spin_unlock(&shutdown_lock);
942 [ # # ]: 0 : if (mfile->disconnected_f_op)
943 [ # # ]: 0 : fops_put(mfile->disconnected_f_op);
944 : : found = mfile;
945 : 0 : break;
946 : : }
947 : : }
948 : : spin_unlock(&card->files_lock);
949 [ # # ]: 0 : if (!found) {
950 : 0 : snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
951 : 0 : return -ENOENT;
952 : : }
953 : 0 : kfree(found);
954 : 0 : snd_card_unref(card);
955 : 0 : return 0;
956 : : }
957 : :
958 : : EXPORT_SYMBOL(snd_card_file_remove);
959 : :
960 : : #ifdef CONFIG_PM
961 : : /**
962 : : * snd_power_wait - wait until the power-state is changed.
963 : : * @card: soundcard structure
964 : : * @power_state: expected power state
965 : : *
966 : : * Waits until the power-state is changed.
967 : : *
968 : : * Return: Zero if successful, or a negative error code.
969 : : *
970 : : * Note: the power lock must be active before call.
971 : : */
972 : 0 : int snd_power_wait(struct snd_card *card, unsigned int power_state)
973 : : {
974 : : wait_queue_t wait;
975 : : int result = 0;
976 : :
977 : : /* fastpath */
978 [ # # ]: 0 : if (snd_power_get_state(card) == power_state)
979 : : return 0;
980 : 0 : init_waitqueue_entry(&wait, current);
981 : 0 : add_wait_queue(&card->power_sleep, &wait);
982 : : while (1) {
983 [ # # ]: 0 : if (card->shutdown) {
984 : : result = -ENODEV;
985 : : break;
986 : : }
987 [ # # ]: 0 : if (snd_power_get_state(card) == power_state)
988 : : break;
989 : 0 : set_current_state(TASK_UNINTERRUPTIBLE);
990 : : snd_power_unlock(card);
991 : 0 : schedule_timeout(30 * HZ);
992 : : snd_power_lock(card);
993 : : }
994 : 0 : remove_wait_queue(&card->power_sleep, &wait);
995 : 0 : return result;
996 : : }
997 : :
998 : : EXPORT_SYMBOL(snd_power_wait);
999 : : #endif /* CONFIG_PM */
|