KEYS: add missing permission check for request_key() destination
[linux/fpc-iii.git] / sound / core / control.c
blobfdffe37d40b8f3691669317373881a781c3649b1
1 /*
2 * Routines for driver control interface
3 * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/threads.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/time.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <sound/info.h>
31 #include <sound/control.h>
33 /* max number of user-defined controls */
34 #define MAX_USER_CONTROLS 32
35 #define MAX_CONTROL_COUNT 1028
37 struct snd_kctl_ioctl {
38 struct list_head list; /* list of all ioctls */
39 snd_kctl_ioctl_func_t fioctl;
42 static DECLARE_RWSEM(snd_ioctl_rwsem);
43 static LIST_HEAD(snd_control_ioctls);
44 #ifdef CONFIG_COMPAT
45 static LIST_HEAD(snd_control_compat_ioctls);
46 #endif
48 static int snd_ctl_open(struct inode *inode, struct file *file)
50 unsigned long flags;
51 struct snd_card *card;
52 struct snd_ctl_file *ctl;
53 int err;
55 err = nonseekable_open(inode, file);
56 if (err < 0)
57 return err;
59 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
60 if (!card) {
61 err = -ENODEV;
62 goto __error1;
64 err = snd_card_file_add(card, file);
65 if (err < 0) {
66 err = -ENODEV;
67 goto __error1;
69 if (!try_module_get(card->module)) {
70 err = -EFAULT;
71 goto __error2;
73 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
74 if (ctl == NULL) {
75 err = -ENOMEM;
76 goto __error;
78 INIT_LIST_HEAD(&ctl->events);
79 init_waitqueue_head(&ctl->change_sleep);
80 spin_lock_init(&ctl->read_lock);
81 ctl->card = card;
82 ctl->prefer_pcm_subdevice = -1;
83 ctl->prefer_rawmidi_subdevice = -1;
84 ctl->pid = get_pid(task_pid(current));
85 file->private_data = ctl;
86 write_lock_irqsave(&card->ctl_files_rwlock, flags);
87 list_add_tail(&ctl->list, &card->ctl_files);
88 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
89 snd_card_unref(card);
90 return 0;
92 __error:
93 module_put(card->module);
94 __error2:
95 snd_card_file_remove(card, file);
96 __error1:
97 if (card)
98 snd_card_unref(card);
99 return err;
102 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
104 unsigned long flags;
105 struct snd_kctl_event *cread;
107 spin_lock_irqsave(&ctl->read_lock, flags);
108 while (!list_empty(&ctl->events)) {
109 cread = snd_kctl_event(ctl->events.next);
110 list_del(&cread->list);
111 kfree(cread);
113 spin_unlock_irqrestore(&ctl->read_lock, flags);
116 static int snd_ctl_release(struct inode *inode, struct file *file)
118 unsigned long flags;
119 struct snd_card *card;
120 struct snd_ctl_file *ctl;
121 struct snd_kcontrol *control;
122 unsigned int idx;
124 ctl = file->private_data;
125 file->private_data = NULL;
126 card = ctl->card;
127 write_lock_irqsave(&card->ctl_files_rwlock, flags);
128 list_del(&ctl->list);
129 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
130 down_write(&card->controls_rwsem);
131 list_for_each_entry(control, &card->controls, list)
132 for (idx = 0; idx < control->count; idx++)
133 if (control->vd[idx].owner == ctl)
134 control->vd[idx].owner = NULL;
135 up_write(&card->controls_rwsem);
136 snd_ctl_empty_read_queue(ctl);
137 put_pid(ctl->pid);
138 kfree(ctl);
139 module_put(card->module);
140 snd_card_file_remove(card, file);
141 return 0;
144 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
145 struct snd_ctl_elem_id *id)
147 unsigned long flags;
148 struct snd_ctl_file *ctl;
149 struct snd_kctl_event *ev;
151 if (snd_BUG_ON(!card || !id))
152 return;
153 if (card->shutdown)
154 return;
155 read_lock(&card->ctl_files_rwlock);
156 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
157 card->mixer_oss_change_count++;
158 #endif
159 list_for_each_entry(ctl, &card->ctl_files, list) {
160 if (!ctl->subscribed)
161 continue;
162 spin_lock_irqsave(&ctl->read_lock, flags);
163 list_for_each_entry(ev, &ctl->events, list) {
164 if (ev->id.numid == id->numid) {
165 ev->mask |= mask;
166 goto _found;
169 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
170 if (ev) {
171 ev->id = *id;
172 ev->mask = mask;
173 list_add_tail(&ev->list, &ctl->events);
174 } else {
175 dev_err(card->dev, "No memory available to allocate event\n");
177 _found:
178 wake_up(&ctl->change_sleep);
179 spin_unlock_irqrestore(&ctl->read_lock, flags);
180 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
182 read_unlock(&card->ctl_files_rwlock);
185 EXPORT_SYMBOL(snd_ctl_notify);
188 * snd_ctl_new - create a control instance from the template
189 * @control: the control template
190 * @access: the default control access
192 * Allocates a new struct snd_kcontrol instance and copies the given template
193 * to the new instance. It does not copy volatile data (access).
195 * Return: The pointer of the new instance, or %NULL on failure.
197 static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
198 unsigned int access)
200 struct snd_kcontrol *kctl;
201 unsigned int idx;
203 if (snd_BUG_ON(!control || !control->count))
204 return NULL;
206 if (control->count > MAX_CONTROL_COUNT)
207 return NULL;
209 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
210 if (kctl == NULL) {
211 pr_err("ALSA: Cannot allocate control instance\n");
212 return NULL;
214 *kctl = *control;
215 for (idx = 0; idx < kctl->count; idx++)
216 kctl->vd[idx].access = access;
217 return kctl;
221 * snd_ctl_new1 - create a control instance from the template
222 * @ncontrol: the initialization record
223 * @private_data: the private data to set
225 * Allocates a new struct snd_kcontrol instance and initialize from the given
226 * template. When the access field of ncontrol is 0, it's assumed as
227 * READWRITE access. When the count field is 0, it's assumes as one.
229 * Return: The pointer of the newly generated instance, or %NULL on failure.
231 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
232 void *private_data)
234 struct snd_kcontrol kctl;
235 unsigned int access;
237 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
238 return NULL;
239 memset(&kctl, 0, sizeof(kctl));
240 kctl.id.iface = ncontrol->iface;
241 kctl.id.device = ncontrol->device;
242 kctl.id.subdevice = ncontrol->subdevice;
243 if (ncontrol->name) {
244 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
245 if (strcmp(ncontrol->name, kctl.id.name) != 0)
246 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
247 ncontrol->name, kctl.id.name);
249 kctl.id.index = ncontrol->index;
250 kctl.count = ncontrol->count ? ncontrol->count : 1;
251 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
252 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
253 SNDRV_CTL_ELEM_ACCESS_VOLATILE|
254 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
255 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
256 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
257 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
258 kctl.info = ncontrol->info;
259 kctl.get = ncontrol->get;
260 kctl.put = ncontrol->put;
261 kctl.tlv.p = ncontrol->tlv.p;
262 kctl.private_value = ncontrol->private_value;
263 kctl.private_data = private_data;
264 return snd_ctl_new(&kctl, access);
267 EXPORT_SYMBOL(snd_ctl_new1);
270 * snd_ctl_free_one - release the control instance
271 * @kcontrol: the control instance
273 * Releases the control instance created via snd_ctl_new()
274 * or snd_ctl_new1().
275 * Don't call this after the control was added to the card.
277 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
279 if (kcontrol) {
280 if (kcontrol->private_free)
281 kcontrol->private_free(kcontrol);
282 kfree(kcontrol);
286 EXPORT_SYMBOL(snd_ctl_free_one);
288 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
289 unsigned int count)
291 struct snd_kcontrol *kctl;
293 /* Make sure that the ids assigned to the control do not wrap around */
294 if (card->last_numid >= UINT_MAX - count)
295 card->last_numid = 0;
297 list_for_each_entry(kctl, &card->controls, list) {
298 if (kctl->id.numid < card->last_numid + 1 + count &&
299 kctl->id.numid + kctl->count > card->last_numid + 1) {
300 card->last_numid = kctl->id.numid + kctl->count - 1;
301 return true;
304 return false;
307 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
309 unsigned int iter = 100000;
311 while (snd_ctl_remove_numid_conflict(card, count)) {
312 if (--iter == 0) {
313 /* this situation is very unlikely */
314 dev_err(card->dev, "unable to allocate new control numid\n");
315 return -ENOMEM;
318 return 0;
322 * snd_ctl_add - add the control instance to the card
323 * @card: the card instance
324 * @kcontrol: the control instance to add
326 * Adds the control instance created via snd_ctl_new() or
327 * snd_ctl_new1() to the given card. Assigns also an unique
328 * numid used for fast search.
330 * It frees automatically the control which cannot be added.
332 * Return: Zero if successful, or a negative error code on failure.
335 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
337 struct snd_ctl_elem_id id;
338 unsigned int idx;
339 unsigned int count;
340 int err = -EINVAL;
342 if (! kcontrol)
343 return err;
344 if (snd_BUG_ON(!card || !kcontrol->info))
345 goto error;
346 id = kcontrol->id;
347 if (id.index > UINT_MAX - kcontrol->count)
348 goto error;
350 down_write(&card->controls_rwsem);
351 if (snd_ctl_find_id(card, &id)) {
352 up_write(&card->controls_rwsem);
353 dev_err(card->dev, "control %i:%i:%i:%s:%i is already present\n",
354 id.iface,
355 id.device,
356 id.subdevice,
357 id.name,
358 id.index);
359 err = -EBUSY;
360 goto error;
362 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
363 up_write(&card->controls_rwsem);
364 err = -ENOMEM;
365 goto error;
367 list_add_tail(&kcontrol->list, &card->controls);
368 card->controls_count += kcontrol->count;
369 kcontrol->id.numid = card->last_numid + 1;
370 card->last_numid += kcontrol->count;
371 count = kcontrol->count;
372 up_write(&card->controls_rwsem);
373 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
374 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
375 return 0;
377 error:
378 snd_ctl_free_one(kcontrol);
379 return err;
382 EXPORT_SYMBOL(snd_ctl_add);
385 * snd_ctl_replace - replace the control instance of the card
386 * @card: the card instance
387 * @kcontrol: the control instance to replace
388 * @add_on_replace: add the control if not already added
390 * Replaces the given control. If the given control does not exist
391 * and the add_on_replace flag is set, the control is added. If the
392 * control exists, it is destroyed first.
394 * It frees automatically the control which cannot be added or replaced.
396 * Return: Zero if successful, or a negative error code on failure.
398 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
399 bool add_on_replace)
401 struct snd_ctl_elem_id id;
402 unsigned int count;
403 unsigned int idx;
404 struct snd_kcontrol *old;
405 int ret;
407 if (!kcontrol)
408 return -EINVAL;
409 if (snd_BUG_ON(!card || !kcontrol->info)) {
410 ret = -EINVAL;
411 goto error;
413 id = kcontrol->id;
414 down_write(&card->controls_rwsem);
415 old = snd_ctl_find_id(card, &id);
416 if (!old) {
417 if (add_on_replace)
418 goto add;
419 up_write(&card->controls_rwsem);
420 ret = -EINVAL;
421 goto error;
423 ret = snd_ctl_remove(card, old);
424 if (ret < 0) {
425 up_write(&card->controls_rwsem);
426 goto error;
428 add:
429 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
430 up_write(&card->controls_rwsem);
431 ret = -ENOMEM;
432 goto error;
434 list_add_tail(&kcontrol->list, &card->controls);
435 card->controls_count += kcontrol->count;
436 kcontrol->id.numid = card->last_numid + 1;
437 card->last_numid += kcontrol->count;
438 count = kcontrol->count;
439 up_write(&card->controls_rwsem);
440 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
441 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
442 return 0;
444 error:
445 snd_ctl_free_one(kcontrol);
446 return ret;
448 EXPORT_SYMBOL(snd_ctl_replace);
451 * snd_ctl_remove - remove the control from the card and release it
452 * @card: the card instance
453 * @kcontrol: the control instance to remove
455 * Removes the control from the card and then releases the instance.
456 * You don't need to call snd_ctl_free_one(). You must be in
457 * the write lock - down_write(&card->controls_rwsem).
459 * Return: 0 if successful, or a negative error code on failure.
461 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
463 struct snd_ctl_elem_id id;
464 unsigned int idx;
466 if (snd_BUG_ON(!card || !kcontrol))
467 return -EINVAL;
468 list_del(&kcontrol->list);
469 card->controls_count -= kcontrol->count;
470 id = kcontrol->id;
471 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
472 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
473 snd_ctl_free_one(kcontrol);
474 return 0;
477 EXPORT_SYMBOL(snd_ctl_remove);
480 * snd_ctl_remove_id - remove the control of the given id and release it
481 * @card: the card instance
482 * @id: the control id to remove
484 * Finds the control instance with the given id, removes it from the
485 * card list and releases it.
487 * Return: 0 if successful, or a negative error code on failure.
489 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
491 struct snd_kcontrol *kctl;
492 int ret;
494 down_write(&card->controls_rwsem);
495 kctl = snd_ctl_find_id(card, id);
496 if (kctl == NULL) {
497 up_write(&card->controls_rwsem);
498 return -ENOENT;
500 ret = snd_ctl_remove(card, kctl);
501 up_write(&card->controls_rwsem);
502 return ret;
505 EXPORT_SYMBOL(snd_ctl_remove_id);
508 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
509 * @file: active control handle
510 * @id: the control id to remove
512 * Finds the control instance with the given id, removes it from the
513 * card list and releases it.
515 * Return: 0 if successful, or a negative error code on failure.
517 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
518 struct snd_ctl_elem_id *id)
520 struct snd_card *card = file->card;
521 struct snd_kcontrol *kctl;
522 int idx, ret;
524 down_write(&card->controls_rwsem);
525 kctl = snd_ctl_find_id(card, id);
526 if (kctl == NULL) {
527 ret = -ENOENT;
528 goto error;
530 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
531 ret = -EINVAL;
532 goto error;
534 for (idx = 0; idx < kctl->count; idx++)
535 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
536 ret = -EBUSY;
537 goto error;
539 ret = snd_ctl_remove(card, kctl);
540 if (ret < 0)
541 goto error;
542 card->user_ctl_count--;
543 error:
544 up_write(&card->controls_rwsem);
545 return ret;
549 * snd_ctl_activate_id - activate/inactivate the control of the given id
550 * @card: the card instance
551 * @id: the control id to activate/inactivate
552 * @active: non-zero to activate
554 * Finds the control instance with the given id, and activate or
555 * inactivate the control together with notification, if changed.
557 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
559 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
560 int active)
562 struct snd_kcontrol *kctl;
563 struct snd_kcontrol_volatile *vd;
564 unsigned int index_offset;
565 int ret;
567 down_write(&card->controls_rwsem);
568 kctl = snd_ctl_find_id(card, id);
569 if (kctl == NULL) {
570 ret = -ENOENT;
571 goto unlock;
573 index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
574 vd = &kctl->vd[index_offset];
575 ret = 0;
576 if (active) {
577 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
578 goto unlock;
579 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
580 } else {
581 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
582 goto unlock;
583 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
585 ret = 1;
586 unlock:
587 up_write(&card->controls_rwsem);
588 if (ret > 0)
589 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
590 return ret;
592 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
595 * snd_ctl_rename_id - replace the id of a control on the card
596 * @card: the card instance
597 * @src_id: the old id
598 * @dst_id: the new id
600 * Finds the control with the old id from the card, and replaces the
601 * id with the new one.
603 * Return: Zero if successful, or a negative error code on failure.
605 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
606 struct snd_ctl_elem_id *dst_id)
608 struct snd_kcontrol *kctl;
610 down_write(&card->controls_rwsem);
611 kctl = snd_ctl_find_id(card, src_id);
612 if (kctl == NULL) {
613 up_write(&card->controls_rwsem);
614 return -ENOENT;
616 kctl->id = *dst_id;
617 kctl->id.numid = card->last_numid + 1;
618 card->last_numid += kctl->count;
619 up_write(&card->controls_rwsem);
620 return 0;
623 EXPORT_SYMBOL(snd_ctl_rename_id);
626 * snd_ctl_find_numid - find the control instance with the given number-id
627 * @card: the card instance
628 * @numid: the number-id to search
630 * Finds the control instance with the given number-id from the card.
632 * The caller must down card->controls_rwsem before calling this function
633 * (if the race condition can happen).
635 * Return: The pointer of the instance if found, or %NULL if not.
638 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
640 struct snd_kcontrol *kctl;
642 if (snd_BUG_ON(!card || !numid))
643 return NULL;
644 list_for_each_entry(kctl, &card->controls, list) {
645 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
646 return kctl;
648 return NULL;
651 EXPORT_SYMBOL(snd_ctl_find_numid);
654 * snd_ctl_find_id - find the control instance with the given id
655 * @card: the card instance
656 * @id: the id to search
658 * Finds the control instance with the given id from the card.
660 * The caller must down card->controls_rwsem before calling this function
661 * (if the race condition can happen).
663 * Return: The pointer of the instance if found, or %NULL if not.
666 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
667 struct snd_ctl_elem_id *id)
669 struct snd_kcontrol *kctl;
671 if (snd_BUG_ON(!card || !id))
672 return NULL;
673 if (id->numid != 0)
674 return snd_ctl_find_numid(card, id->numid);
675 list_for_each_entry(kctl, &card->controls, list) {
676 if (kctl->id.iface != id->iface)
677 continue;
678 if (kctl->id.device != id->device)
679 continue;
680 if (kctl->id.subdevice != id->subdevice)
681 continue;
682 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
683 continue;
684 if (kctl->id.index > id->index)
685 continue;
686 if (kctl->id.index + kctl->count <= id->index)
687 continue;
688 return kctl;
690 return NULL;
693 EXPORT_SYMBOL(snd_ctl_find_id);
695 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
696 unsigned int cmd, void __user *arg)
698 struct snd_ctl_card_info *info;
700 info = kzalloc(sizeof(*info), GFP_KERNEL);
701 if (! info)
702 return -ENOMEM;
703 down_read(&snd_ioctl_rwsem);
704 info->card = card->number;
705 strlcpy(info->id, card->id, sizeof(info->id));
706 strlcpy(info->driver, card->driver, sizeof(info->driver));
707 strlcpy(info->name, card->shortname, sizeof(info->name));
708 strlcpy(info->longname, card->longname, sizeof(info->longname));
709 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
710 strlcpy(info->components, card->components, sizeof(info->components));
711 up_read(&snd_ioctl_rwsem);
712 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
713 kfree(info);
714 return -EFAULT;
716 kfree(info);
717 return 0;
720 static int snd_ctl_elem_list(struct snd_card *card,
721 struct snd_ctl_elem_list __user *_list)
723 struct list_head *plist;
724 struct snd_ctl_elem_list list;
725 struct snd_kcontrol *kctl;
726 struct snd_ctl_elem_id *dst, *id;
727 unsigned int offset, space, jidx;
729 if (copy_from_user(&list, _list, sizeof(list)))
730 return -EFAULT;
731 offset = list.offset;
732 space = list.space;
733 /* try limit maximum space */
734 if (space > 16384)
735 return -ENOMEM;
736 if (space > 0) {
737 /* allocate temporary buffer for atomic operation */
738 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
739 if (dst == NULL)
740 return -ENOMEM;
741 down_read(&card->controls_rwsem);
742 list.count = card->controls_count;
743 plist = card->controls.next;
744 while (plist != &card->controls) {
745 if (offset == 0)
746 break;
747 kctl = snd_kcontrol(plist);
748 if (offset < kctl->count)
749 break;
750 offset -= kctl->count;
751 plist = plist->next;
753 list.used = 0;
754 id = dst;
755 while (space > 0 && plist != &card->controls) {
756 kctl = snd_kcontrol(plist);
757 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
758 snd_ctl_build_ioff(id, kctl, jidx);
759 id++;
760 space--;
761 list.used++;
763 plist = plist->next;
764 offset = 0;
766 up_read(&card->controls_rwsem);
767 if (list.used > 0 &&
768 copy_to_user(list.pids, dst,
769 list.used * sizeof(struct snd_ctl_elem_id))) {
770 vfree(dst);
771 return -EFAULT;
773 vfree(dst);
774 } else {
775 down_read(&card->controls_rwsem);
776 list.count = card->controls_count;
777 up_read(&card->controls_rwsem);
779 if (copy_to_user(_list, &list, sizeof(list)))
780 return -EFAULT;
781 return 0;
784 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
785 struct snd_ctl_elem_info *info)
787 struct snd_card *card = ctl->card;
788 struct snd_kcontrol *kctl;
789 struct snd_kcontrol_volatile *vd;
790 unsigned int index_offset;
791 int result;
793 down_read(&card->controls_rwsem);
794 kctl = snd_ctl_find_id(card, &info->id);
795 if (kctl == NULL) {
796 up_read(&card->controls_rwsem);
797 return -ENOENT;
799 #ifdef CONFIG_SND_DEBUG
800 info->access = 0;
801 #endif
802 result = kctl->info(kctl, info);
803 if (result >= 0) {
804 snd_BUG_ON(info->access);
805 index_offset = snd_ctl_get_ioff(kctl, &info->id);
806 vd = &kctl->vd[index_offset];
807 snd_ctl_build_ioff(&info->id, kctl, index_offset);
808 info->access = vd->access;
809 if (vd->owner) {
810 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
811 if (vd->owner == ctl)
812 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
813 info->owner = pid_vnr(vd->owner->pid);
814 } else {
815 info->owner = -1;
818 up_read(&card->controls_rwsem);
819 return result;
822 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
823 struct snd_ctl_elem_info __user *_info)
825 struct snd_ctl_elem_info info;
826 int result;
828 if (copy_from_user(&info, _info, sizeof(info)))
829 return -EFAULT;
830 snd_power_lock(ctl->card);
831 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
832 if (result >= 0)
833 result = snd_ctl_elem_info(ctl, &info);
834 snd_power_unlock(ctl->card);
835 if (result >= 0)
836 if (copy_to_user(_info, &info, sizeof(info)))
837 return -EFAULT;
838 return result;
841 static int snd_ctl_elem_read(struct snd_card *card,
842 struct snd_ctl_elem_value *control)
844 struct snd_kcontrol *kctl;
845 struct snd_kcontrol_volatile *vd;
846 unsigned int index_offset;
847 int result;
849 down_read(&card->controls_rwsem);
850 kctl = snd_ctl_find_id(card, &control->id);
851 if (kctl == NULL) {
852 result = -ENOENT;
853 } else {
854 index_offset = snd_ctl_get_ioff(kctl, &control->id);
855 vd = &kctl->vd[index_offset];
856 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
857 kctl->get != NULL) {
858 snd_ctl_build_ioff(&control->id, kctl, index_offset);
859 result = kctl->get(kctl, control);
860 } else
861 result = -EPERM;
863 up_read(&card->controls_rwsem);
864 return result;
867 static int snd_ctl_elem_read_user(struct snd_card *card,
868 struct snd_ctl_elem_value __user *_control)
870 struct snd_ctl_elem_value *control;
871 int result;
873 control = memdup_user(_control, sizeof(*control));
874 if (IS_ERR(control))
875 return PTR_ERR(control);
877 snd_power_lock(card);
878 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
879 if (result >= 0)
880 result = snd_ctl_elem_read(card, control);
881 snd_power_unlock(card);
882 if (result >= 0)
883 if (copy_to_user(_control, control, sizeof(*control)))
884 result = -EFAULT;
885 kfree(control);
886 return result;
889 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
890 struct snd_ctl_elem_value *control)
892 struct snd_kcontrol *kctl;
893 struct snd_kcontrol_volatile *vd;
894 unsigned int index_offset;
895 int result;
897 down_read(&card->controls_rwsem);
898 kctl = snd_ctl_find_id(card, &control->id);
899 if (kctl == NULL) {
900 result = -ENOENT;
901 } else {
902 index_offset = snd_ctl_get_ioff(kctl, &control->id);
903 vd = &kctl->vd[index_offset];
904 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
905 kctl->put == NULL ||
906 (file && vd->owner && vd->owner != file)) {
907 result = -EPERM;
908 } else {
909 snd_ctl_build_ioff(&control->id, kctl, index_offset);
910 result = kctl->put(kctl, control);
912 if (result > 0) {
913 struct snd_ctl_elem_id id = control->id;
914 up_read(&card->controls_rwsem);
915 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
916 return 0;
919 up_read(&card->controls_rwsem);
920 return result;
923 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
924 struct snd_ctl_elem_value __user *_control)
926 struct snd_ctl_elem_value *control;
927 struct snd_card *card;
928 int result;
930 control = memdup_user(_control, sizeof(*control));
931 if (IS_ERR(control))
932 return PTR_ERR(control);
934 card = file->card;
935 snd_power_lock(card);
936 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
937 if (result >= 0)
938 result = snd_ctl_elem_write(card, file, control);
939 snd_power_unlock(card);
940 if (result >= 0)
941 if (copy_to_user(_control, control, sizeof(*control)))
942 result = -EFAULT;
943 kfree(control);
944 return result;
947 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
948 struct snd_ctl_elem_id __user *_id)
950 struct snd_card *card = file->card;
951 struct snd_ctl_elem_id id;
952 struct snd_kcontrol *kctl;
953 struct snd_kcontrol_volatile *vd;
954 int result;
956 if (copy_from_user(&id, _id, sizeof(id)))
957 return -EFAULT;
958 down_write(&card->controls_rwsem);
959 kctl = snd_ctl_find_id(card, &id);
960 if (kctl == NULL) {
961 result = -ENOENT;
962 } else {
963 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
964 if (vd->owner != NULL)
965 result = -EBUSY;
966 else {
967 vd->owner = file;
968 result = 0;
971 up_write(&card->controls_rwsem);
972 return result;
975 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
976 struct snd_ctl_elem_id __user *_id)
978 struct snd_card *card = file->card;
979 struct snd_ctl_elem_id id;
980 struct snd_kcontrol *kctl;
981 struct snd_kcontrol_volatile *vd;
982 int result;
984 if (copy_from_user(&id, _id, sizeof(id)))
985 return -EFAULT;
986 down_write(&card->controls_rwsem);
987 kctl = snd_ctl_find_id(card, &id);
988 if (kctl == NULL) {
989 result = -ENOENT;
990 } else {
991 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
992 if (vd->owner == NULL)
993 result = -EINVAL;
994 else if (vd->owner != file)
995 result = -EPERM;
996 else {
997 vd->owner = NULL;
998 result = 0;
1001 up_write(&card->controls_rwsem);
1002 return result;
1005 struct user_element {
1006 struct snd_ctl_elem_info info;
1007 struct snd_card *card;
1008 void *elem_data; /* element data */
1009 unsigned long elem_data_size; /* size of element data in bytes */
1010 void *tlv_data; /* TLV data */
1011 unsigned long tlv_data_size; /* TLV data size */
1012 void *priv_data; /* private data (like strings for enumerated type) */
1015 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1016 struct snd_ctl_elem_info *uinfo)
1018 struct user_element *ue = kcontrol->private_data;
1020 *uinfo = ue->info;
1021 return 0;
1024 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1025 struct snd_ctl_elem_info *uinfo)
1027 struct user_element *ue = kcontrol->private_data;
1028 const char *names;
1029 unsigned int item;
1031 item = uinfo->value.enumerated.item;
1033 *uinfo = ue->info;
1035 item = min(item, uinfo->value.enumerated.items - 1);
1036 uinfo->value.enumerated.item = item;
1038 names = ue->priv_data;
1039 for (; item > 0; --item)
1040 names += strlen(names) + 1;
1041 strcpy(uinfo->value.enumerated.name, names);
1043 return 0;
1046 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1047 struct snd_ctl_elem_value *ucontrol)
1049 struct user_element *ue = kcontrol->private_data;
1051 mutex_lock(&ue->card->user_ctl_lock);
1052 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
1053 mutex_unlock(&ue->card->user_ctl_lock);
1054 return 0;
1057 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1058 struct snd_ctl_elem_value *ucontrol)
1060 int change;
1061 struct user_element *ue = kcontrol->private_data;
1063 mutex_lock(&ue->card->user_ctl_lock);
1064 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
1065 if (change)
1066 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
1067 mutex_unlock(&ue->card->user_ctl_lock);
1068 return change;
1071 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
1072 int op_flag,
1073 unsigned int size,
1074 unsigned int __user *tlv)
1076 struct user_element *ue = kcontrol->private_data;
1077 int change = 0;
1078 void *new_data;
1080 if (op_flag > 0) {
1081 if (size > 1024 * 128) /* sane value */
1082 return -EINVAL;
1084 new_data = memdup_user(tlv, size);
1085 if (IS_ERR(new_data))
1086 return PTR_ERR(new_data);
1087 mutex_lock(&ue->card->user_ctl_lock);
1088 change = ue->tlv_data_size != size;
1089 if (!change)
1090 change = memcmp(ue->tlv_data, new_data, size) != 0;
1091 kfree(ue->tlv_data);
1092 ue->tlv_data = new_data;
1093 ue->tlv_data_size = size;
1094 mutex_unlock(&ue->card->user_ctl_lock);
1095 } else {
1096 int ret = 0;
1098 mutex_lock(&ue->card->user_ctl_lock);
1099 if (!ue->tlv_data_size || !ue->tlv_data) {
1100 ret = -ENXIO;
1101 goto err_unlock;
1103 if (size < ue->tlv_data_size) {
1104 ret = -ENOSPC;
1105 goto err_unlock;
1107 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
1108 ret = -EFAULT;
1109 err_unlock:
1110 mutex_unlock(&ue->card->user_ctl_lock);
1111 if (ret)
1112 return ret;
1114 return change;
1117 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1119 char *names, *p;
1120 size_t buf_len, name_len;
1121 unsigned int i;
1122 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1124 if (ue->info.value.enumerated.names_length > 64 * 1024)
1125 return -EINVAL;
1127 names = memdup_user((const void __user *)user_ptrval,
1128 ue->info.value.enumerated.names_length);
1129 if (IS_ERR(names))
1130 return PTR_ERR(names);
1132 /* check that there are enough valid names */
1133 buf_len = ue->info.value.enumerated.names_length;
1134 p = names;
1135 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1136 name_len = strnlen(p, buf_len);
1137 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1138 kfree(names);
1139 return -EINVAL;
1141 p += name_len + 1;
1142 buf_len -= name_len + 1;
1145 ue->priv_data = names;
1146 ue->info.value.enumerated.names_ptr = 0;
1148 return 0;
1151 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1153 struct user_element *ue = kcontrol->private_data;
1155 kfree(ue->tlv_data);
1156 kfree(ue->priv_data);
1157 kfree(ue);
1160 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1161 struct snd_ctl_elem_info *info, int replace)
1163 struct snd_card *card = file->card;
1164 struct snd_kcontrol kctl, *_kctl;
1165 unsigned int access;
1166 long private_size;
1167 struct user_element *ue;
1168 int idx, err;
1170 if (info->count < 1)
1171 return -EINVAL;
1172 if (!*info->id.name)
1173 return -EINVAL;
1174 if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1175 return -EINVAL;
1176 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
1177 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
1178 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1179 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
1180 info->id.numid = 0;
1181 memset(&kctl, 0, sizeof(kctl));
1183 if (replace) {
1184 err = snd_ctl_remove_user_ctl(file, &info->id);
1185 if (err)
1186 return err;
1189 if (card->user_ctl_count >= MAX_USER_CONTROLS)
1190 return -ENOMEM;
1192 memcpy(&kctl.id, &info->id, sizeof(info->id));
1193 kctl.count = info->owner ? info->owner : 1;
1194 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1195 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1196 kctl.info = snd_ctl_elem_user_enum_info;
1197 else
1198 kctl.info = snd_ctl_elem_user_info;
1199 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1200 kctl.get = snd_ctl_elem_user_get;
1201 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1202 kctl.put = snd_ctl_elem_user_put;
1203 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1204 kctl.tlv.c = snd_ctl_elem_user_tlv;
1205 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1207 switch (info->type) {
1208 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1209 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1210 private_size = sizeof(long);
1211 if (info->count > 128)
1212 return -EINVAL;
1213 break;
1214 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1215 private_size = sizeof(long long);
1216 if (info->count > 64)
1217 return -EINVAL;
1218 break;
1219 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1220 private_size = sizeof(unsigned int);
1221 if (info->count > 128 || info->value.enumerated.items == 0)
1222 return -EINVAL;
1223 break;
1224 case SNDRV_CTL_ELEM_TYPE_BYTES:
1225 private_size = sizeof(unsigned char);
1226 if (info->count > 512)
1227 return -EINVAL;
1228 break;
1229 case SNDRV_CTL_ELEM_TYPE_IEC958:
1230 private_size = sizeof(struct snd_aes_iec958);
1231 if (info->count != 1)
1232 return -EINVAL;
1233 break;
1234 default:
1235 return -EINVAL;
1237 private_size *= info->count;
1238 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
1239 if (ue == NULL)
1240 return -ENOMEM;
1241 ue->card = card;
1242 ue->info = *info;
1243 ue->info.access = 0;
1244 ue->elem_data = (char *)ue + sizeof(*ue);
1245 ue->elem_data_size = private_size;
1246 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1247 err = snd_ctl_elem_init_enum_names(ue);
1248 if (err < 0) {
1249 kfree(ue);
1250 return err;
1253 kctl.private_free = snd_ctl_elem_user_free;
1254 _kctl = snd_ctl_new(&kctl, access);
1255 if (_kctl == NULL) {
1256 kfree(ue->priv_data);
1257 kfree(ue);
1258 return -ENOMEM;
1260 _kctl->private_data = ue;
1261 for (idx = 0; idx < _kctl->count; idx++)
1262 _kctl->vd[idx].owner = file;
1263 err = snd_ctl_add(card, _kctl);
1264 if (err < 0)
1265 return err;
1267 down_write(&card->controls_rwsem);
1268 card->user_ctl_count++;
1269 up_write(&card->controls_rwsem);
1271 return 0;
1274 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1275 struct snd_ctl_elem_info __user *_info, int replace)
1277 struct snd_ctl_elem_info info;
1278 if (copy_from_user(&info, _info, sizeof(info)))
1279 return -EFAULT;
1280 return snd_ctl_elem_add(file, &info, replace);
1283 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1284 struct snd_ctl_elem_id __user *_id)
1286 struct snd_ctl_elem_id id;
1288 if (copy_from_user(&id, _id, sizeof(id)))
1289 return -EFAULT;
1290 return snd_ctl_remove_user_ctl(file, &id);
1293 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1295 int subscribe;
1296 if (get_user(subscribe, ptr))
1297 return -EFAULT;
1298 if (subscribe < 0) {
1299 subscribe = file->subscribed;
1300 if (put_user(subscribe, ptr))
1301 return -EFAULT;
1302 return 0;
1304 if (subscribe) {
1305 file->subscribed = 1;
1306 return 0;
1307 } else if (file->subscribed) {
1308 snd_ctl_empty_read_queue(file);
1309 file->subscribed = 0;
1311 return 0;
1314 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1315 struct snd_ctl_tlv __user *_tlv,
1316 int op_flag)
1318 struct snd_card *card = file->card;
1319 struct snd_ctl_tlv tlv;
1320 struct snd_kcontrol *kctl;
1321 struct snd_kcontrol_volatile *vd;
1322 unsigned int len;
1323 int err = 0;
1325 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1326 return -EFAULT;
1327 if (tlv.length < sizeof(unsigned int) * 2)
1328 return -EINVAL;
1329 if (!tlv.numid)
1330 return -EINVAL;
1331 down_read(&card->controls_rwsem);
1332 kctl = snd_ctl_find_numid(card, tlv.numid);
1333 if (kctl == NULL) {
1334 err = -ENOENT;
1335 goto __kctl_end;
1337 if (kctl->tlv.p == NULL) {
1338 err = -ENXIO;
1339 goto __kctl_end;
1341 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1342 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1343 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1344 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1345 err = -ENXIO;
1346 goto __kctl_end;
1348 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1349 if (vd->owner != NULL && vd->owner != file) {
1350 err = -EPERM;
1351 goto __kctl_end;
1353 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1354 if (err > 0) {
1355 struct snd_ctl_elem_id id = kctl->id;
1356 up_read(&card->controls_rwsem);
1357 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &id);
1358 return 0;
1360 } else {
1361 if (op_flag) {
1362 err = -ENXIO;
1363 goto __kctl_end;
1365 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1366 if (tlv.length < len) {
1367 err = -ENOMEM;
1368 goto __kctl_end;
1370 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1371 err = -EFAULT;
1373 __kctl_end:
1374 up_read(&card->controls_rwsem);
1375 return err;
1378 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1380 struct snd_ctl_file *ctl;
1381 struct snd_card *card;
1382 struct snd_kctl_ioctl *p;
1383 void __user *argp = (void __user *)arg;
1384 int __user *ip = argp;
1385 int err;
1387 ctl = file->private_data;
1388 card = ctl->card;
1389 if (snd_BUG_ON(!card))
1390 return -ENXIO;
1391 switch (cmd) {
1392 case SNDRV_CTL_IOCTL_PVERSION:
1393 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1394 case SNDRV_CTL_IOCTL_CARD_INFO:
1395 return snd_ctl_card_info(card, ctl, cmd, argp);
1396 case SNDRV_CTL_IOCTL_ELEM_LIST:
1397 return snd_ctl_elem_list(card, argp);
1398 case SNDRV_CTL_IOCTL_ELEM_INFO:
1399 return snd_ctl_elem_info_user(ctl, argp);
1400 case SNDRV_CTL_IOCTL_ELEM_READ:
1401 return snd_ctl_elem_read_user(card, argp);
1402 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1403 return snd_ctl_elem_write_user(ctl, argp);
1404 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1405 return snd_ctl_elem_lock(ctl, argp);
1406 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1407 return snd_ctl_elem_unlock(ctl, argp);
1408 case SNDRV_CTL_IOCTL_ELEM_ADD:
1409 return snd_ctl_elem_add_user(ctl, argp, 0);
1410 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1411 return snd_ctl_elem_add_user(ctl, argp, 1);
1412 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1413 return snd_ctl_elem_remove(ctl, argp);
1414 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1415 return snd_ctl_subscribe_events(ctl, ip);
1416 case SNDRV_CTL_IOCTL_TLV_READ:
1417 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1418 case SNDRV_CTL_IOCTL_TLV_WRITE:
1419 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1420 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1421 return snd_ctl_tlv_ioctl(ctl, argp, -1);
1422 case SNDRV_CTL_IOCTL_POWER:
1423 return -ENOPROTOOPT;
1424 case SNDRV_CTL_IOCTL_POWER_STATE:
1425 #ifdef CONFIG_PM
1426 return put_user(card->power_state, ip) ? -EFAULT : 0;
1427 #else
1428 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1429 #endif
1431 down_read(&snd_ioctl_rwsem);
1432 list_for_each_entry(p, &snd_control_ioctls, list) {
1433 err = p->fioctl(card, ctl, cmd, arg);
1434 if (err != -ENOIOCTLCMD) {
1435 up_read(&snd_ioctl_rwsem);
1436 return err;
1439 up_read(&snd_ioctl_rwsem);
1440 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
1441 return -ENOTTY;
1444 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1445 size_t count, loff_t * offset)
1447 struct snd_ctl_file *ctl;
1448 int err = 0;
1449 ssize_t result = 0;
1451 ctl = file->private_data;
1452 if (snd_BUG_ON(!ctl || !ctl->card))
1453 return -ENXIO;
1454 if (!ctl->subscribed)
1455 return -EBADFD;
1456 if (count < sizeof(struct snd_ctl_event))
1457 return -EINVAL;
1458 spin_lock_irq(&ctl->read_lock);
1459 while (count >= sizeof(struct snd_ctl_event)) {
1460 struct snd_ctl_event ev;
1461 struct snd_kctl_event *kev;
1462 while (list_empty(&ctl->events)) {
1463 wait_queue_t wait;
1464 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1465 err = -EAGAIN;
1466 goto __end_lock;
1468 init_waitqueue_entry(&wait, current);
1469 add_wait_queue(&ctl->change_sleep, &wait);
1470 set_current_state(TASK_INTERRUPTIBLE);
1471 spin_unlock_irq(&ctl->read_lock);
1472 schedule();
1473 remove_wait_queue(&ctl->change_sleep, &wait);
1474 if (ctl->card->shutdown)
1475 return -ENODEV;
1476 if (signal_pending(current))
1477 return -ERESTARTSYS;
1478 spin_lock_irq(&ctl->read_lock);
1480 kev = snd_kctl_event(ctl->events.next);
1481 ev.type = SNDRV_CTL_EVENT_ELEM;
1482 ev.data.elem.mask = kev->mask;
1483 ev.data.elem.id = kev->id;
1484 list_del(&kev->list);
1485 spin_unlock_irq(&ctl->read_lock);
1486 kfree(kev);
1487 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1488 err = -EFAULT;
1489 goto __end;
1491 spin_lock_irq(&ctl->read_lock);
1492 buffer += sizeof(struct snd_ctl_event);
1493 count -= sizeof(struct snd_ctl_event);
1494 result += sizeof(struct snd_ctl_event);
1496 __end_lock:
1497 spin_unlock_irq(&ctl->read_lock);
1498 __end:
1499 return result > 0 ? result : err;
1502 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1504 unsigned int mask;
1505 struct snd_ctl_file *ctl;
1507 ctl = file->private_data;
1508 if (!ctl->subscribed)
1509 return 0;
1510 poll_wait(file, &ctl->change_sleep, wait);
1512 mask = 0;
1513 if (!list_empty(&ctl->events))
1514 mask |= POLLIN | POLLRDNORM;
1516 return mask;
1520 * register the device-specific control-ioctls.
1521 * called from each device manager like pcm.c, hwdep.c, etc.
1523 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1525 struct snd_kctl_ioctl *pn;
1527 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1528 if (pn == NULL)
1529 return -ENOMEM;
1530 pn->fioctl = fcn;
1531 down_write(&snd_ioctl_rwsem);
1532 list_add_tail(&pn->list, lists);
1533 up_write(&snd_ioctl_rwsem);
1534 return 0;
1537 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1539 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1542 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1544 #ifdef CONFIG_COMPAT
1545 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1547 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1550 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1551 #endif
1554 * de-register the device-specific control-ioctls.
1556 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1557 struct list_head *lists)
1559 struct snd_kctl_ioctl *p;
1561 if (snd_BUG_ON(!fcn))
1562 return -EINVAL;
1563 down_write(&snd_ioctl_rwsem);
1564 list_for_each_entry(p, lists, list) {
1565 if (p->fioctl == fcn) {
1566 list_del(&p->list);
1567 up_write(&snd_ioctl_rwsem);
1568 kfree(p);
1569 return 0;
1572 up_write(&snd_ioctl_rwsem);
1573 snd_BUG();
1574 return -EINVAL;
1577 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1579 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1582 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1584 #ifdef CONFIG_COMPAT
1585 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1587 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1590 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1591 #endif
1593 static int snd_ctl_fasync(int fd, struct file * file, int on)
1595 struct snd_ctl_file *ctl;
1597 ctl = file->private_data;
1598 return fasync_helper(fd, file, on, &ctl->fasync);
1602 * ioctl32 compat
1604 #ifdef CONFIG_COMPAT
1605 #include "control_compat.c"
1606 #else
1607 #define snd_ctl_ioctl_compat NULL
1608 #endif
1611 * INIT PART
1614 static const struct file_operations snd_ctl_f_ops =
1616 .owner = THIS_MODULE,
1617 .read = snd_ctl_read,
1618 .open = snd_ctl_open,
1619 .release = snd_ctl_release,
1620 .llseek = no_llseek,
1621 .poll = snd_ctl_poll,
1622 .unlocked_ioctl = snd_ctl_ioctl,
1623 .compat_ioctl = snd_ctl_ioctl_compat,
1624 .fasync = snd_ctl_fasync,
1628 * registration of the control device
1630 static int snd_ctl_dev_register(struct snd_device *device)
1632 struct snd_card *card = device->device_data;
1633 int err, cardnum;
1634 char name[16];
1636 if (snd_BUG_ON(!card))
1637 return -ENXIO;
1638 cardnum = card->number;
1639 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1640 return -ENXIO;
1641 sprintf(name, "controlC%i", cardnum);
1642 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1643 &snd_ctl_f_ops, card, name)) < 0)
1644 return err;
1645 return 0;
1649 * disconnection of the control device
1651 static int snd_ctl_dev_disconnect(struct snd_device *device)
1653 struct snd_card *card = device->device_data;
1654 struct snd_ctl_file *ctl;
1655 int err, cardnum;
1657 if (snd_BUG_ON(!card))
1658 return -ENXIO;
1659 cardnum = card->number;
1660 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1661 return -ENXIO;
1663 read_lock(&card->ctl_files_rwlock);
1664 list_for_each_entry(ctl, &card->ctl_files, list) {
1665 wake_up(&ctl->change_sleep);
1666 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1668 read_unlock(&card->ctl_files_rwlock);
1670 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1671 card, -1)) < 0)
1672 return err;
1673 return 0;
1677 * free all controls
1679 static int snd_ctl_dev_free(struct snd_device *device)
1681 struct snd_card *card = device->device_data;
1682 struct snd_kcontrol *control;
1684 down_write(&card->controls_rwsem);
1685 while (!list_empty(&card->controls)) {
1686 control = snd_kcontrol(card->controls.next);
1687 snd_ctl_remove(card, control);
1689 up_write(&card->controls_rwsem);
1690 return 0;
1694 * create control core:
1695 * called from init.c
1697 int snd_ctl_create(struct snd_card *card)
1699 static struct snd_device_ops ops = {
1700 .dev_free = snd_ctl_dev_free,
1701 .dev_register = snd_ctl_dev_register,
1702 .dev_disconnect = snd_ctl_dev_disconnect,
1705 if (snd_BUG_ON(!card))
1706 return -ENXIO;
1707 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1711 * Frequently used control callbacks/helpers
1713 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1714 struct snd_ctl_elem_info *uinfo)
1716 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1717 uinfo->count = 1;
1718 uinfo->value.integer.min = 0;
1719 uinfo->value.integer.max = 1;
1720 return 0;
1723 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1725 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1726 struct snd_ctl_elem_info *uinfo)
1728 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1729 uinfo->count = 2;
1730 uinfo->value.integer.min = 0;
1731 uinfo->value.integer.max = 1;
1732 return 0;
1735 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
1738 * snd_ctl_enum_info - fills the info structure for an enumerated control
1739 * @info: the structure to be filled
1740 * @channels: the number of the control's channels; often one
1741 * @items: the number of control values; also the size of @names
1742 * @names: an array containing the names of all control values
1744 * Sets all required fields in @info to their appropriate values.
1745 * If the control's accessibility is not the default (readable and writable),
1746 * the caller has to fill @info->access.
1748 * Return: Zero.
1750 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1751 unsigned int items, const char *const names[])
1753 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1754 info->count = channels;
1755 info->value.enumerated.items = items;
1756 if (info->value.enumerated.item >= items)
1757 info->value.enumerated.item = items - 1;
1758 strlcpy(info->value.enumerated.name,
1759 names[info->value.enumerated.item],
1760 sizeof(info->value.enumerated.name));
1761 return 0;
1763 EXPORT_SYMBOL(snd_ctl_enum_info);