blob: 56b3e2d49c82321509e699217ee1085382c40ce4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines for driver control interface
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/threads.h>
23#include <linux/interrupt.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/time.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010028#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include <sound/minors.h>
31#include <sound/info.h>
32#include <sound/control.h>
33
34/* max number of user-defined controls */
35#define MAX_USER_CONTROLS 32
Dan Rosenberg5591bf02010-09-28 14:18:20 -040036#define MAX_CONTROL_COUNT 1028
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai82e9bae2005-11-17 13:53:23 +010038struct snd_kctl_ioctl {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct list_head list; /* list of all ioctls */
40 snd_kctl_ioctl_func_t fioctl;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010041};
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static DECLARE_RWSEM(snd_ioctl_rwsem);
44static LIST_HEAD(snd_control_ioctls);
45#ifdef CONFIG_COMPAT
46static LIST_HEAD(snd_control_compat_ioctls);
47#endif
48
49static int snd_ctl_open(struct inode *inode, struct file *file)
50{
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010052 struct snd_card *card;
53 struct snd_ctl_file *ctl;
Takashi Iwai23c18d42014-02-19 14:30:29 +010054 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Takashi Iwai02f48652010-04-13 11:49:04 +020056 err = nonseekable_open(inode, file);
57 if (err < 0)
58 return err;
59
Clemens Ladischf87135f2005-11-20 14:06:59 +010060 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (!card) {
62 err = -ENODEV;
63 goto __error1;
64 }
65 err = snd_card_file_add(card, file);
66 if (err < 0) {
67 err = -ENODEV;
68 goto __error1;
69 }
70 if (!try_module_get(card->module)) {
71 err = -EFAULT;
72 goto __error2;
73 }
Takashi Iwaica2c0962005-09-09 14:20:23 +020074 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (ctl == NULL) {
76 err = -ENOMEM;
77 goto __error;
78 }
79 INIT_LIST_HEAD(&ctl->events);
80 init_waitqueue_head(&ctl->change_sleep);
81 spin_lock_init(&ctl->read_lock);
82 ctl->card = card;
Takashi Iwai23c18d42014-02-19 14:30:29 +010083 for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
84 ctl->preferred_subdevice[i] = -1;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +010085 ctl->pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 file->private_data = ctl;
87 write_lock_irqsave(&card->ctl_files_rwlock, flags);
88 list_add_tail(&ctl->list, &card->ctl_files);
89 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
Takashi Iwaia0830db2012-10-16 13:05:59 +020090 snd_card_unref(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 0;
92
93 __error:
94 module_put(card->module);
95 __error2:
96 snd_card_file_remove(card, file);
97 __error1:
Takashi Iwaia0830db2012-10-16 13:05:59 +020098 if (card)
99 snd_card_unref(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return err;
101}
102
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100103static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200105 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100106 struct snd_kctl_event *cread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200108 spin_lock_irqsave(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 while (!list_empty(&ctl->events)) {
110 cread = snd_kctl_event(ctl->events.next);
111 list_del(&cread->list);
112 kfree(cread);
113 }
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200114 spin_unlock_irqrestore(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static int snd_ctl_release(struct inode *inode, struct file *file)
118{
119 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100120 struct snd_card *card;
121 struct snd_ctl_file *ctl;
122 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned int idx;
124
125 ctl = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 file->private_data = NULL;
127 card = ctl->card;
128 write_lock_irqsave(&card->ctl_files_rwlock, flags);
129 list_del(&ctl->list);
130 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
131 down_write(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200132 list_for_each_entry(control, &card->controls, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 for (idx = 0; idx < control->count; idx++)
134 if (control->vd[idx].owner == ctl)
135 control->vd[idx].owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 up_write(&card->controls_rwsem);
137 snd_ctl_empty_read_queue(ctl);
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100138 put_pid(ctl->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 kfree(ctl);
140 module_put(card->module);
141 snd_card_file_remove(card, file);
142 return 0;
143}
144
Takashi Iwai12cddbd2014-10-30 13:44:34 +0100145/**
146 * snd_ctl_notify - Send notification to user-space for a control change
147 * @card: the card to send notification
148 * @mask: the event mask, SNDRV_CTL_EVENT_*
149 * @id: the ctl element id to send notification
150 *
151 * This function adds an event record with the given id and mask, appends
152 * to the list and wakes up the user-space for notification. This can be
153 * called in the atomic context.
154 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100155void snd_ctl_notify(struct snd_card *card, unsigned int mask,
156 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100159 struct snd_ctl_file *ctl;
160 struct snd_kctl_event *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200162 if (snd_BUG_ON(!card || !id))
163 return;
Takashi Iwaif388cdc2016-07-08 08:05:19 +0200164 if (card->shutdown)
165 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 read_lock(&card->ctl_files_rwlock);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100167#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 card->mixer_oss_change_count++;
169#endif
Johannes Berg9244b2c2006-10-05 16:02:22 +0200170 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (!ctl->subscribed)
172 continue;
173 spin_lock_irqsave(&ctl->read_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200174 list_for_each_entry(ev, &ctl->events, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (ev->id.numid == id->numid) {
176 ev->mask |= mask;
177 goto _found;
178 }
179 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200180 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (ev) {
182 ev->id = *id;
183 ev->mask = mask;
184 list_add_tail(&ev->list, &ctl->events);
185 } else {
Takashi Iwaibb009452014-02-04 18:18:16 +0100186 dev_err(card->dev, "No memory available to allocate event\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 _found:
189 wake_up(&ctl->change_sleep);
190 spin_unlock_irqrestore(&ctl->read_lock, flags);
191 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
192 }
193 read_unlock(&card->ctl_files_rwlock);
194}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200195EXPORT_SYMBOL(snd_ctl_notify);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/**
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900198 * snd_ctl_new - create a new control instance with some elements
199 * @kctl: the pointer to store new control instance
200 * @count: the number of elements in this control
201 * @access: the default access flags for elements in this control
202 * @file: given when locking these elements
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900204 * Allocates a memory object for a new control instance. The instance has
205 * elements as many as the given number (@count). Each element has given
206 * access permissions (@access). Each element is locked when @file is given.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900208 * Return: 0 on success, error code on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900210static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
211 unsigned int access, struct snd_ctl_file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900213 unsigned int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned int idx;
215
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900216 if (count == 0 || count > MAX_CONTROL_COUNT)
217 return -EINVAL;
Dan Rosenberg5591bf02010-09-28 14:18:20 -0400218
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900219 size = sizeof(struct snd_kcontrol);
220 size += sizeof(struct snd_kcontrol_volatile) * count;
Dan Rosenberg5591bf02010-09-28 14:18:20 -0400221
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900222 *kctl = kzalloc(size, GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +0100223 if (!*kctl)
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900224 return -ENOMEM;
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900225
226 for (idx = 0; idx < count; idx++) {
227 (*kctl)->vd[idx].access = access;
228 (*kctl)->vd[idx].owner = file;
229 }
230 (*kctl)->count = count;
231
232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/**
236 * snd_ctl_new1 - create a control instance from the template
237 * @ncontrol: the initialization record
238 * @private_data: the private data to set
239 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100240 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * template. When the access field of ncontrol is 0, it's assumed as
242 * READWRITE access. When the count field is 0, it's assumes as one.
243 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100244 * Return: The pointer of the newly generated instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100246struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
247 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900249 struct snd_kcontrol *kctl;
250 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned int access;
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900252 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200254 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
255 return NULL;
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900256
257 count = ncontrol->count;
258 if (count == 0)
259 count = 1;
260
261 access = ncontrol->access;
262 if (access == 0)
263 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
264 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
265 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
266 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
267 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
268 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
269 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
270
271 err = snd_ctl_new(&kctl, count, access, NULL);
272 if (err < 0)
273 return NULL;
274
275 /* The 'numid' member is decided when calling snd_ctl_add(). */
276 kctl->id.iface = ncontrol->iface;
277 kctl->id.device = ncontrol->device;
278 kctl->id.subdevice = ncontrol->subdevice;
Mark Brown366840d2008-10-29 14:40:30 +0000279 if (ncontrol->name) {
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900280 strlcpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
281 if (strcmp(ncontrol->name, kctl->id.name) != 0)
Takashi Iwaibb009452014-02-04 18:18:16 +0100282 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900283 ncontrol->name, kctl->id.name);
Mark Brown366840d2008-10-29 14:40:30 +0000284 }
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900285 kctl->id.index = ncontrol->index;
286
287 kctl->info = ncontrol->info;
288 kctl->get = ncontrol->get;
289 kctl->put = ncontrol->put;
290 kctl->tlv.p = ncontrol->tlv.p;
291
292 kctl->private_value = ncontrol->private_value;
293 kctl->private_data = private_data;
294
295 return kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200297EXPORT_SYMBOL(snd_ctl_new1);
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/**
300 * snd_ctl_free_one - release the control instance
301 * @kcontrol: the control instance
302 *
303 * Releases the control instance created via snd_ctl_new()
304 * or snd_ctl_new1().
305 * Don't call this after the control was added to the card.
306 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100307void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 if (kcontrol) {
310 if (kcontrol->private_free)
311 kcontrol->private_free(kcontrol);
312 kfree(kcontrol);
313 }
314}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200315EXPORT_SYMBOL(snd_ctl_free_one);
316
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100317static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
318 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100320 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Lars-Peter Clausenac902c12014-06-18 13:32:34 +0200322 /* Make sure that the ids assigned to the control do not wrap around */
323 if (card->last_numid >= UINT_MAX - count)
324 card->last_numid = 0;
325
Johannes Berg9244b2c2006-10-05 16:02:22 +0200326 list_for_each_entry(kctl, &card->controls, list) {
Clemens Ladisch7c733582011-03-07 13:22:50 +0100327 if (kctl->id.numid < card->last_numid + 1 + count &&
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100328 kctl->id.numid + kctl->count > card->last_numid + 1) {
329 card->last_numid = kctl->id.numid + kctl->count - 1;
330 return true;
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100333 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100336static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100338 unsigned int iter = 100000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100340 while (snd_ctl_remove_numid_conflict(card, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (--iter == 0) {
342 /* this situation is very unlikely */
Takashi Iwaibb009452014-02-04 18:18:16 +0100343 dev_err(card->dev, "unable to allocate new control numid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -ENOMEM;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 return 0;
348}
349
350/**
351 * snd_ctl_add - add the control instance to the card
352 * @card: the card instance
353 * @kcontrol: the control instance to add
354 *
355 * Adds the control instance created via snd_ctl_new() or
356 * snd_ctl_new1() to the given card. Assigns also an unique
357 * numid used for fast search.
358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * It frees automatically the control which cannot be added.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100360 *
361 * Return: Zero if successful, or a negative error code on failure.
362 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100364int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100366 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 unsigned int idx;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200368 unsigned int count;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100369 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100371 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100372 return err;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200373 if (snd_BUG_ON(!card || !kcontrol->info))
374 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 id = kcontrol->id;
Lars-Peter Clausen883a1d42014-06-18 13:32:35 +0200376 if (id.index > UINT_MAX - kcontrol->count)
377 goto error;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 down_write(&card->controls_rwsem);
380 if (snd_ctl_find_id(card, &id)) {
381 up_write(&card->controls_rwsem);
Takashi Iwaibb009452014-02-04 18:18:16 +0100382 dev_err(card->dev, "control %i:%i:%i:%s:%i is already present\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 id.iface,
384 id.device,
385 id.subdevice,
386 id.name,
387 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100388 err = -EBUSY;
389 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
392 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100393 err = -ENOMEM;
394 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 list_add_tail(&kcontrol->list, &card->controls);
397 card->controls_count += kcontrol->count;
398 kcontrol->id.numid = card->last_numid + 1;
399 card->last_numid += kcontrol->count;
Takashi Sakamotod34890c2015-02-08 22:39:44 +0900400 id = kcontrol->id;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200401 count = kcontrol->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 up_write(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200403 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
405 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100406
407 error:
408 snd_ctl_free_one(kcontrol);
409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200411EXPORT_SYMBOL(snd_ctl_add);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/**
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000414 * snd_ctl_replace - replace the control instance of the card
415 * @card: the card instance
416 * @kcontrol: the control instance to replace
417 * @add_on_replace: add the control if not already added
418 *
419 * Replaces the given control. If the given control does not exist
420 * and the add_on_replace flag is set, the control is added. If the
421 * control exists, it is destroyed first.
422 *
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000423 * It frees automatically the control which cannot be added or replaced.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100424 *
425 * Return: Zero if successful, or a negative error code on failure.
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000426 */
427int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
428 bool add_on_replace)
429{
430 struct snd_ctl_elem_id id;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200431 unsigned int count;
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000432 unsigned int idx;
433 struct snd_kcontrol *old;
434 int ret;
435
436 if (!kcontrol)
437 return -EINVAL;
438 if (snd_BUG_ON(!card || !kcontrol->info)) {
439 ret = -EINVAL;
440 goto error;
441 }
442 id = kcontrol->id;
443 down_write(&card->controls_rwsem);
444 old = snd_ctl_find_id(card, &id);
445 if (!old) {
446 if (add_on_replace)
447 goto add;
448 up_write(&card->controls_rwsem);
449 ret = -EINVAL;
450 goto error;
451 }
452 ret = snd_ctl_remove(card, old);
453 if (ret < 0) {
454 up_write(&card->controls_rwsem);
455 goto error;
456 }
457add:
458 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
459 up_write(&card->controls_rwsem);
460 ret = -ENOMEM;
461 goto error;
462 }
463 list_add_tail(&kcontrol->list, &card->controls);
464 card->controls_count += kcontrol->count;
465 kcontrol->id.numid = card->last_numid + 1;
466 card->last_numid += kcontrol->count;
Takashi Sakamotoe6ff3842015-02-10 00:01:45 +0900467 id = kcontrol->id;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200468 count = kcontrol->count;
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000469 up_write(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200470 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000471 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
472 return 0;
473
474error:
475 snd_ctl_free_one(kcontrol);
476 return ret;
477}
478EXPORT_SYMBOL(snd_ctl_replace);
479
480/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * snd_ctl_remove - remove the control from the card and release it
482 * @card: the card instance
483 * @kcontrol: the control instance to remove
484 *
485 * Removes the control from the card and then releases the instance.
486 * You don't need to call snd_ctl_free_one(). You must be in
487 * the write lock - down_write(&card->controls_rwsem).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100488 *
489 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100491int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100493 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 unsigned int idx;
495
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200496 if (snd_BUG_ON(!card || !kcontrol))
497 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 list_del(&kcontrol->list);
499 card->controls_count -= kcontrol->count;
500 id = kcontrol->id;
501 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
502 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
503 snd_ctl_free_one(kcontrol);
504 return 0;
505}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200506EXPORT_SYMBOL(snd_ctl_remove);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/**
509 * snd_ctl_remove_id - remove the control of the given id and release it
510 * @card: the card instance
511 * @id: the control id to remove
512 *
513 * Finds the control instance with the given id, removes it from the
514 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100515 *
516 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100518int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100520 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int ret;
522
523 down_write(&card->controls_rwsem);
524 kctl = snd_ctl_find_id(card, id);
525 if (kctl == NULL) {
526 up_write(&card->controls_rwsem);
527 return -ENOENT;
528 }
529 ret = snd_ctl_remove(card, kctl);
530 up_write(&card->controls_rwsem);
531 return ret;
532}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200533EXPORT_SYMBOL(snd_ctl_remove_id);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/**
Clemens Ladischf217ac52009-08-17 12:27:22 +0200536 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * @file: active control handle
538 * @id: the control id to remove
539 *
540 * Finds the control instance with the given id, removes it from the
541 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100542 *
543 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Clemens Ladischf217ac52009-08-17 12:27:22 +0200545static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
546 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100548 struct snd_card *card = file->card;
549 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int idx, ret;
551
552 down_write(&card->controls_rwsem);
553 kctl = snd_ctl_find_id(card, id);
554 if (kctl == NULL) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200555 ret = -ENOENT;
556 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Clemens Ladisch18dd0aa2009-08-17 12:28:09 +0200558 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
559 ret = -EINVAL;
560 goto error;
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 for (idx = 0; idx < kctl->count; idx++)
563 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200564 ret = -EBUSY;
565 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 ret = snd_ctl_remove(card, kctl);
Clemens Ladischf217ac52009-08-17 12:27:22 +0200568 if (ret < 0)
569 goto error;
570 card->user_ctl_count--;
Clemens Ladisch317b8082009-08-17 12:26:34 +0200571error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 up_write(&card->controls_rwsem);
573 return ret;
574}
575
576/**
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200577 * snd_ctl_activate_id - activate/inactivate the control of the given id
578 * @card: the card instance
579 * @id: the control id to activate/inactivate
580 * @active: non-zero to activate
581 *
582 * Finds the control instance with the given id, and activate or
583 * inactivate the control together with notification, if changed.
Takashi Sakamotoc78497e2015-04-11 17:41:02 +0900584 * The given ID data is filled with full information.
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200585 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100586 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200587 */
588int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
589 int active)
590{
591 struct snd_kcontrol *kctl;
592 struct snd_kcontrol_volatile *vd;
593 unsigned int index_offset;
594 int ret;
595
596 down_write(&card->controls_rwsem);
597 kctl = snd_ctl_find_id(card, id);
598 if (kctl == NULL) {
599 ret = -ENOENT;
600 goto unlock;
601 }
Lars-Peter Clausen31584ed2014-11-07 14:12:34 +0100602 index_offset = snd_ctl_get_ioff(kctl, id);
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200603 vd = &kctl->vd[index_offset];
604 ret = 0;
605 if (active) {
606 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
607 goto unlock;
608 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
609 } else {
610 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
611 goto unlock;
612 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
613 }
Takashi Sakamotoc78497e2015-04-11 17:41:02 +0900614 snd_ctl_build_ioff(id, kctl, index_offset);
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200615 ret = 1;
616 unlock:
617 up_write(&card->controls_rwsem);
618 if (ret > 0)
619 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
620 return ret;
621}
622EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
623
624/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 * snd_ctl_rename_id - replace the id of a control on the card
626 * @card: the card instance
627 * @src_id: the old id
628 * @dst_id: the new id
629 *
630 * Finds the control with the old id from the card, and replaces the
631 * id with the new one.
632 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100633 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100635int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
636 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100638 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 down_write(&card->controls_rwsem);
641 kctl = snd_ctl_find_id(card, src_id);
642 if (kctl == NULL) {
643 up_write(&card->controls_rwsem);
644 return -ENOENT;
645 }
646 kctl->id = *dst_id;
647 kctl->id.numid = card->last_numid + 1;
648 card->last_numid += kctl->count;
649 up_write(&card->controls_rwsem);
650 return 0;
651}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200652EXPORT_SYMBOL(snd_ctl_rename_id);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/**
655 * snd_ctl_find_numid - find the control instance with the given number-id
656 * @card: the card instance
657 * @numid: the number-id to search
658 *
659 * Finds the control instance with the given number-id from the card.
660 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * The caller must down card->controls_rwsem before calling this function
662 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100663 *
664 * Return: The pointer of the instance if found, or %NULL if not.
665 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100667struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100669 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200671 if (snd_BUG_ON(!card || !numid))
672 return NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200673 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
675 return kctl;
676 }
677 return NULL;
678}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200679EXPORT_SYMBOL(snd_ctl_find_numid);
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/**
682 * snd_ctl_find_id - find the control instance with the given id
683 * @card: the card instance
684 * @id: the id to search
685 *
686 * Finds the control instance with the given id from the card.
687 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * The caller must down card->controls_rwsem before calling this function
689 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100690 *
691 * Return: The pointer of the instance if found, or %NULL if not.
692 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100694struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
695 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100697 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200699 if (snd_BUG_ON(!card || !id))
700 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (id->numid != 0)
702 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200703 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (kctl->id.iface != id->iface)
705 continue;
706 if (kctl->id.device != id->device)
707 continue;
708 if (kctl->id.subdevice != id->subdevice)
709 continue;
710 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
711 continue;
712 if (kctl->id.index > id->index)
713 continue;
714 if (kctl->id.index + kctl->count <= id->index)
715 continue;
716 return kctl;
717 }
718 return NULL;
719}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200720EXPORT_SYMBOL(snd_ctl_find_id);
721
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100722static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned int cmd, void __user *arg)
724{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100725 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Takashi Iwaica2c0962005-09-09 14:20:23 +0200727 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (! info)
729 return -ENOMEM;
730 down_read(&snd_ioctl_rwsem);
731 info->card = card->number;
732 strlcpy(info->id, card->id, sizeof(info->id));
733 strlcpy(info->driver, card->driver, sizeof(info->driver));
734 strlcpy(info->name, card->shortname, sizeof(info->name));
735 strlcpy(info->longname, card->longname, sizeof(info->longname));
736 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
737 strlcpy(info->components, card->components, sizeof(info->components));
738 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100739 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 kfree(info);
741 return -EFAULT;
742 }
743 kfree(info);
744 return 0;
745}
746
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100747static int snd_ctl_elem_list(struct snd_card *card,
748 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100750 struct snd_ctl_elem_list list;
751 struct snd_kcontrol *kctl;
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200752 struct snd_ctl_elem_id id;
Luca Tettamanti78fa2c42011-05-25 22:43:27 +0200753 unsigned int offset, space, jidx;
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200754 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (copy_from_user(&list, _list, sizeof(list)))
757 return -EFAULT;
758 offset = list.offset;
759 space = list.space;
Takashi Sakamoto4e361d32017-05-24 10:04:30 +0900760
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200761 down_read(&card->controls_rwsem);
762 list.count = card->controls_count;
763 list.used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (space > 0) {
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200765 list_for_each_entry(kctl, &card->controls, list) {
766 if (offset >= kctl->count) {
767 offset -= kctl->count;
768 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200770 for (jidx = offset; jidx < kctl->count; jidx++) {
771 snd_ctl_build_ioff(&id, kctl, jidx);
772 if (copy_to_user(list.pids + list.used, &id,
773 sizeof(id))) {
774 err = -EFAULT;
775 goto out;
776 }
777 list.used++;
778 if (!--space)
779 goto out;
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 offset = 0;
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200784 out:
785 up_read(&card->controls_rwsem);
786 if (!err && copy_to_user(_list, &list, sizeof(list)))
787 err = -EFAULT;
788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Takashi Sakamoto860c1992016-07-07 21:57:10 +0900791static bool validate_element_member_dimension(struct snd_ctl_elem_info *info)
792{
793 unsigned int members;
794 unsigned int i;
795
796 if (info->dimen.d[0] == 0)
797 return true;
798
799 members = 1;
800 for (i = 0; i < ARRAY_SIZE(info->dimen.d); ++i) {
801 if (info->dimen.d[i] == 0)
802 break;
803 members *= info->dimen.d[i];
804
805 /*
806 * info->count should be validated in advance, to guarantee
807 * calculation soundness.
808 */
809 if (members > info->count)
810 return false;
811 }
812
813 for (++i; i < ARRAY_SIZE(info->dimen.d); ++i) {
814 if (info->dimen.d[i] > 0)
815 return false;
816 }
817
818 return members == info->count;
819}
820
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100821static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
822 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100824 struct snd_card *card = ctl->card;
825 struct snd_kcontrol *kctl;
826 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 unsigned int index_offset;
828 int result;
829
830 down_read(&card->controls_rwsem);
831 kctl = snd_ctl_find_id(card, &info->id);
832 if (kctl == NULL) {
833 up_read(&card->controls_rwsem);
834 return -ENOENT;
835 }
836#ifdef CONFIG_SND_DEBUG
837 info->access = 0;
838#endif
839 result = kctl->info(kctl, info);
840 if (result >= 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200841 snd_BUG_ON(info->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 index_offset = snd_ctl_get_ioff(kctl, &info->id);
843 vd = &kctl->vd[index_offset];
844 snd_ctl_build_ioff(&info->id, kctl, index_offset);
845 info->access = vd->access;
846 if (vd->owner) {
847 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
848 if (vd->owner == ctl)
849 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100850 info->owner = pid_vnr(vd->owner->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 } else {
852 info->owner = -1;
853 }
854 }
855 up_read(&card->controls_rwsem);
856 return result;
857}
858
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100859static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
860 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100862 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int result;
864
865 if (copy_from_user(&info, _info, sizeof(info)))
866 return -EFAULT;
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200867 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Takashi Iwai7d8e8292017-08-30 16:13:25 +0200868 if (result < 0)
869 return result;
870 result = snd_ctl_elem_info(ctl, &info);
871 if (result < 0)
872 return result;
873 if (copy_to_user(_info, &info, sizeof(info)))
874 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return result;
876}
877
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200878static int snd_ctl_elem_read(struct snd_card *card,
879 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100881 struct snd_kcontrol *kctl;
882 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 unsigned int index_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 kctl = snd_ctl_find_id(card, &control->id);
Takashi Sakamotobecf9e52017-08-20 13:49:07 +0900886 if (kctl == NULL)
887 return -ENOENT;
888
889 index_offset = snd_ctl_get_ioff(kctl, &control->id);
890 vd = &kctl->vd[index_offset];
891 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get == NULL)
892 return -EPERM;
893
894 snd_ctl_build_ioff(&control->id, kctl, index_offset);
895 return kctl->get(kctl, control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100898static int snd_ctl_elem_read_user(struct snd_card *card,
899 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100901 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int result;
Li Zefanef44a1e2009-04-10 09:43:08 +0800903
904 control = memdup_user(_control, sizeof(*control));
905 if (IS_ERR(control))
906 return PTR_ERR(control);
907
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200908 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai7d8e8292017-08-30 16:13:25 +0200909 if (result < 0)
910 goto error;
911
912 down_read(&card->controls_rwsem);
913 result = snd_ctl_elem_read(card, control);
914 up_read(&card->controls_rwsem);
915 if (result < 0)
916 goto error;
917
918 if (copy_to_user(_control, control, sizeof(*control)))
919 result = -EFAULT;
920 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 kfree(control);
922 return result;
923}
924
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200925static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
926 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100928 struct snd_kcontrol *kctl;
929 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100931 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 kctl = snd_ctl_find_id(card, &control->id);
Takashi Sakamotobecf9e52017-08-20 13:49:07 +0900934 if (kctl == NULL)
935 return -ENOENT;
936
937 index_offset = snd_ctl_get_ioff(kctl, &control->id);
938 vd = &kctl->vd[index_offset];
939 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
940 (file && vd->owner && vd->owner != file)) {
941 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Takashi Sakamotobecf9e52017-08-20 13:49:07 +0900943
944 snd_ctl_build_ioff(&control->id, kctl, index_offset);
945 result = kctl->put(kctl, control);
946 if (result < 0)
947 return result;
948
949 if (result > 0) {
950 struct snd_ctl_elem_id id = control->id;
951 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
952 }
953
954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100957static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
958 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100960 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100961 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int result;
963
Li Zefanef44a1e2009-04-10 09:43:08 +0800964 control = memdup_user(_control, sizeof(*control));
965 if (IS_ERR(control))
966 return PTR_ERR(control);
967
Giuliano Pochini64649402006-03-13 14:11:11 +0100968 card = file->card;
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200969 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai7d8e8292017-08-30 16:13:25 +0200970 if (result < 0)
971 goto error;
972
973 down_write(&card->controls_rwsem);
974 result = snd_ctl_elem_write(card, file, control);
975 up_write(&card->controls_rwsem);
976 if (result < 0)
977 goto error;
978
979 if (copy_to_user(_control, control, sizeof(*control)))
980 result = -EFAULT;
981 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 kfree(control);
983 return result;
984}
985
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100986static int snd_ctl_elem_lock(struct snd_ctl_file *file,
987 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100989 struct snd_card *card = file->card;
990 struct snd_ctl_elem_id id;
991 struct snd_kcontrol *kctl;
992 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 int result;
994
995 if (copy_from_user(&id, _id, sizeof(id)))
996 return -EFAULT;
997 down_write(&card->controls_rwsem);
998 kctl = snd_ctl_find_id(card, &id);
999 if (kctl == NULL) {
1000 result = -ENOENT;
1001 } else {
1002 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1003 if (vd->owner != NULL)
1004 result = -EBUSY;
1005 else {
1006 vd->owner = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 result = 0;
1008 }
1009 }
1010 up_write(&card->controls_rwsem);
1011 return result;
1012}
1013
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001014static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
1015 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001017 struct snd_card *card = file->card;
1018 struct snd_ctl_elem_id id;
1019 struct snd_kcontrol *kctl;
1020 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int result;
1022
1023 if (copy_from_user(&id, _id, sizeof(id)))
1024 return -EFAULT;
1025 down_write(&card->controls_rwsem);
1026 kctl = snd_ctl_find_id(card, &id);
1027 if (kctl == NULL) {
1028 result = -ENOENT;
1029 } else {
1030 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1031 if (vd->owner == NULL)
1032 result = -EINVAL;
1033 else if (vd->owner != file)
1034 result = -EPERM;
1035 else {
1036 vd->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 result = 0;
1038 }
1039 }
1040 up_write(&card->controls_rwsem);
1041 return result;
1042}
1043
1044struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001045 struct snd_ctl_elem_info info;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001046 struct snd_card *card;
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001047 char *elem_data; /* element data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001049 void *tlv_data; /* TLV data */
1050 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 void *priv_data; /* private data (like strings for enumerated type) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052};
1053
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001054static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1055 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct user_element *ue = kcontrol->private_data;
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001058 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001060 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 *uinfo = ue->info;
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001062 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return 0;
1065}
1066
Clemens Ladisch8d448162011-10-07 22:38:59 +02001067static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1068 struct snd_ctl_elem_info *uinfo)
1069{
1070 struct user_element *ue = kcontrol->private_data;
1071 const char *names;
1072 unsigned int item;
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001073 unsigned int offset;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001074
1075 item = uinfo->value.enumerated.item;
1076
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001077 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
Clemens Ladisch8d448162011-10-07 22:38:59 +02001078 *uinfo = ue->info;
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001079 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
Clemens Ladisch8d448162011-10-07 22:38:59 +02001080
1081 item = min(item, uinfo->value.enumerated.items - 1);
1082 uinfo->value.enumerated.item = item;
1083
1084 names = ue->priv_data;
1085 for (; item > 0; --item)
1086 names += strlen(names) + 1;
1087 strcpy(uinfo->value.enumerated.name, names);
1088
1089 return 0;
1090}
1091
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001092static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1093 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 struct user_element *ue = kcontrol->private_data;
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001096 unsigned int size = ue->elem_data_size;
1097 char *src = ue->elem_data +
1098 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001100 memcpy(&ucontrol->value, src, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return 0;
1102}
1103
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001104static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1105 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 int change;
1108 struct user_element *ue = kcontrol->private_data;
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001109 unsigned int size = ue->elem_data_size;
1110 char *dst = ue->elem_data +
1111 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001112
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001113 change = memcmp(&ucontrol->value, dst, size) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (change)
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001115 memcpy(dst, &ucontrol->value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return change;
1117}
1118
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001119static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1120 unsigned int size)
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001121{
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001122 struct user_element *ue = kctl->private_data;
1123 unsigned int *container;
Takashi Sakamotoda428822017-08-24 10:46:15 +09001124 struct snd_ctl_elem_id id;
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001125 unsigned int mask = 0;
Takashi Sakamotoda428822017-08-24 10:46:15 +09001126 int i;
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001127 int change;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001128
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001129 if (size > 1024 * 128) /* sane value */
1130 return -EINVAL;
Takashi Sakamoto30d83402017-08-03 20:20:42 +09001131
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001132 container = memdup_user(buf, size);
1133 if (IS_ERR(container))
1134 return PTR_ERR(container);
Li Zefanef44a1e2009-04-10 09:43:08 +08001135
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001136 change = ue->tlv_data_size != size;
1137 if (!change)
Takashi Iwai241bc822017-08-22 15:44:45 +02001138 change = memcmp(ue->tlv_data, container, size) != 0;
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001139 if (!change) {
1140 kfree(container);
Takashi Sakamoto30d83402017-08-03 20:20:42 +09001141 return 0;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001142 }
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001143
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001144 if (ue->tlv_data == NULL) {
1145 /* Now TLV data is available. */
1146 for (i = 0; i < kctl->count; ++i)
1147 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1148 mask = SNDRV_CTL_EVENT_MASK_INFO;
1149 }
1150
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001151 kfree(ue->tlv_data);
1152 ue->tlv_data = container;
1153 ue->tlv_data_size = size;
1154
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001155 mask |= SNDRV_CTL_EVENT_MASK_TLV;
Takashi Sakamotoda428822017-08-24 10:46:15 +09001156 for (i = 0; i < kctl->count; ++i) {
1157 snd_ctl_build_ioff(&id, kctl, i);
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001158 snd_ctl_notify(ue->card, mask, &id);
Takashi Sakamotoda428822017-08-24 10:46:15 +09001159 }
Takashi Sakamotofb8027e2017-08-24 10:46:14 +09001160
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001161 return change;
1162}
1163
1164static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1165 unsigned int size)
1166{
1167 struct user_element *ue = kctl->private_data;
1168
1169 if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1170 return -ENXIO;
1171
1172 if (size < ue->tlv_data_size)
1173 return -ENOSPC;
1174
1175 if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1176 return -EFAULT;
1177
1178 return 0;
1179}
1180
1181static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1182 unsigned int size, unsigned int __user *buf)
1183{
1184 if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1185 return replace_user_tlv(kctl, buf, size);
1186 else
1187 return read_user_tlv(kctl, buf, size);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001188}
1189
Clemens Ladisch8d448162011-10-07 22:38:59 +02001190static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1191{
1192 char *names, *p;
1193 size_t buf_len, name_len;
1194 unsigned int i;
Olof Johansson447c6f92011-11-05 22:51:54 +01001195 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001196
1197 if (ue->info.value.enumerated.names_length > 64 * 1024)
1198 return -EINVAL;
1199
Olof Johansson447c6f92011-11-05 22:51:54 +01001200 names = memdup_user((const void __user *)user_ptrval,
Clemens Ladisch8d448162011-10-07 22:38:59 +02001201 ue->info.value.enumerated.names_length);
1202 if (IS_ERR(names))
1203 return PTR_ERR(names);
1204
1205 /* check that there are enough valid names */
1206 buf_len = ue->info.value.enumerated.names_length;
1207 p = names;
1208 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1209 name_len = strnlen(p, buf_len);
1210 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1211 kfree(names);
1212 return -EINVAL;
1213 }
1214 p += name_len + 1;
1215 buf_len -= name_len + 1;
1216 }
1217
1218 ue->priv_data = names;
1219 ue->info.value.enumerated.names_ptr = 0;
1220
1221 return 0;
1222}
1223
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001224static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001226 struct user_element *ue = kcontrol->private_data;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001227
1228 kfree(ue->tlv_data);
1229 kfree(ue->priv_data);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001230 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001233static int snd_ctl_elem_add(struct snd_ctl_file *file,
1234 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001236 /* The capacity of struct snd_ctl_elem_value.value.*/
1237 static const unsigned int value_sizes[] = {
1238 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
1239 [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
1240 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
1241 [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
1242 [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
1243 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
1244 };
1245 static const unsigned int max_value_counts[] = {
1246 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
1247 [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
1248 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
1249 [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
1250 [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
1251 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
1252 };
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001253 struct snd_card *card = file->card;
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001254 struct snd_kcontrol *kctl;
1255 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 unsigned int access;
1257 long private_size;
1258 struct user_element *ue;
Takashi Sakamotocab2ed72015-04-11 17:41:04 +09001259 unsigned int offset;
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001260 int err;
Lu Guanqun983929c2011-08-24 11:12:34 +08001261
Takashi Iwaibe3bb822015-03-11 18:12:49 +01001262 if (!*info->id.name)
1263 return -EINVAL;
1264 if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1265 return -EINVAL;
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001266
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001267 /* Delete a control to replace them if needed. */
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001268 if (replace) {
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001269 info->id.numid = 0;
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001270 err = snd_ctl_remove_user_ctl(file, &info->id);
1271 if (err)
1272 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001274
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001275 /*
1276 * The number of userspace controls are counted control by control,
1277 * not element by element.
1278 */
1279 if (card->user_ctl_count + 1 > MAX_USER_CONTROLS)
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001280 return -ENOMEM;
1281
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001282 /* Check the number of elements for this userspace control. */
1283 count = info->owner;
1284 if (count == 0)
1285 count = 1;
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001286
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001287 /* Arrange access permissions if needed. */
1288 access = info->access;
1289 if (access == 0)
1290 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1291 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1292 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001293 SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1294
1295 /* In initial state, nothing is available as TLV container. */
1296 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001297 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1298 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1299
1300 /*
1301 * Check information and calculate the size of data specific to
1302 * this userspace control.
1303 */
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001304 if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
1305 info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EINVAL;
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001307 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
1308 info->value.enumerated.items == 0)
1309 return -EINVAL;
1310 if (info->count < 1 ||
1311 info->count > max_value_counts[info->type])
1312 return -EINVAL;
Takashi Sakamoto860c1992016-07-07 21:57:10 +09001313 if (!validate_element_member_dimension(info))
1314 return -EINVAL;
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001315 private_size = value_sizes[info->type] * info->count;
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001316
1317 /*
1318 * Keep memory object for this userspace control. After passing this
1319 * code block, the instance should be freed by snd_ctl_free_one().
1320 *
1321 * Note that these elements in this control are locked.
1322 */
1323 err = snd_ctl_new(&kctl, count, access, file);
1324 if (err < 0)
1325 return err;
Takashi Iwaie79d74a2015-03-12 16:57:51 +01001326 memcpy(&kctl->id, &info->id, sizeof(kctl->id));
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001327 kctl->private_data = kzalloc(sizeof(struct user_element) + private_size * count,
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001328 GFP_KERNEL);
1329 if (kctl->private_data == NULL) {
1330 kfree(kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -ENOMEM;
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001332 }
1333 kctl->private_free = snd_ctl_elem_user_free;
1334
1335 /* Set private data for this userspace control. */
1336 ue = (struct user_element *)kctl->private_data;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001337 ue->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001339 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 ue->elem_data = (char *)ue + sizeof(*ue);
1341 ue->elem_data_size = private_size;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001342 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1343 err = snd_ctl_elem_init_enum_names(ue);
1344 if (err < 0) {
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001345 snd_ctl_free_one(kctl);
Clemens Ladisch8d448162011-10-07 22:38:59 +02001346 return err;
1347 }
1348 }
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001349
1350 /* Set callback functions. */
1351 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1352 kctl->info = snd_ctl_elem_user_enum_info;
1353 else
1354 kctl->info = snd_ctl_elem_user_info;
1355 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1356 kctl->get = snd_ctl_elem_user_get;
1357 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1358 kctl->put = snd_ctl_elem_user_put;
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001359 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001360 kctl->tlv.c = snd_ctl_elem_user_tlv;
1361
1362 /* This function manage to free the instance on failure. */
1363 err = snd_ctl_add(card, kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001364 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return err;
Takashi Sakamotocab2ed72015-04-11 17:41:04 +09001366 offset = snd_ctl_get_ioff(kctl, &info->id);
1367 snd_ctl_build_ioff(&info->id, kctl, offset);
1368 /*
1369 * Here we cannot fill any field for the number of elements added by
1370 * this operation because there're no specific fields. The usage of
1371 * 'owner' field for this purpose may cause any bugs to userspace
1372 * applications because the field originally means PID of a process
1373 * which locks the element.
1374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 down_write(&card->controls_rwsem);
1377 card->user_ctl_count++;
1378 up_write(&card->controls_rwsem);
1379
1380 return 0;
1381}
1382
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001383static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1384 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001386 struct snd_ctl_elem_info info;
Takashi Sakamotocab2ed72015-04-11 17:41:04 +09001387 int err;
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (copy_from_user(&info, _info, sizeof(info)))
1390 return -EFAULT;
Takashi Sakamotocab2ed72015-04-11 17:41:04 +09001391 err = snd_ctl_elem_add(file, &info, replace);
1392 if (err < 0)
1393 return err;
1394 if (copy_to_user(_info, &info, sizeof(info))) {
1395 snd_ctl_remove_user_ctl(file, &info.id);
1396 return -EFAULT;
1397 }
1398
1399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001402static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1403 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001405 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 if (copy_from_user(&id, _id, sizeof(id)))
1408 return -EFAULT;
Clemens Ladischf217ac52009-08-17 12:27:22 +02001409 return snd_ctl_remove_user_ctl(file, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001412static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 int subscribe;
1415 if (get_user(subscribe, ptr))
1416 return -EFAULT;
1417 if (subscribe < 0) {
1418 subscribe = file->subscribed;
1419 if (put_user(subscribe, ptr))
1420 return -EFAULT;
1421 return 0;
1422 }
1423 if (subscribe) {
1424 file->subscribed = 1;
1425 return 0;
1426 } else if (file->subscribed) {
1427 snd_ctl_empty_read_queue(file);
1428 file->subscribed = 0;
1429 }
1430 return 0;
1431}
1432
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001433static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1434 struct snd_kcontrol *kctl,
1435 struct snd_ctl_elem_id *id,
1436 unsigned int __user *buf, unsigned int size)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001437{
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001438 static const struct {
1439 int op;
1440 int perm;
1441 } pairs[] = {
1442 {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1443 {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1444 {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1445 };
1446 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1447 int i;
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001448
1449 /* Check support of the request for this element. */
1450 for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1451 if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1452 break;
1453 }
1454 if (i == ARRAY_SIZE(pairs))
1455 return -ENXIO;
1456
1457 if (kctl->tlv.c == NULL)
1458 return -ENXIO;
1459
1460 /* When locked, this is unavailable. */
1461 if (vd->owner != NULL && vd->owner != file)
1462 return -EPERM;
1463
Takashi Sakamotofb8027e2017-08-24 10:46:14 +09001464 return kctl->tlv.c(kctl, op_flag, size, buf);
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001465}
1466
1467static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1468 unsigned int __user *buf, unsigned int size)
1469{
1470 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001471 unsigned int len;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001472
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001473 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1474 return -ENXIO;
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001475
1476 if (kctl->tlv.p == NULL)
1477 return -ENXIO;
1478
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001479 len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1480 if (size < len)
1481 return -ENOMEM;
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001482
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001483 if (copy_to_user(buf, kctl->tlv.p, len))
1484 return -EFAULT;
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001485
1486 return 0;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001487}
1488
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001489static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1490 struct snd_ctl_tlv __user *buf,
1491 int op_flag)
1492{
1493 struct snd_ctl_tlv header;
1494 unsigned int *container;
1495 unsigned int container_size;
1496 struct snd_kcontrol *kctl;
1497 struct snd_ctl_elem_id id;
1498 struct snd_kcontrol_volatile *vd;
1499
1500 if (copy_from_user(&header, buf, sizeof(header)))
1501 return -EFAULT;
1502
1503 /* In design of control core, numerical ID starts at 1. */
1504 if (header.numid == 0)
1505 return -EINVAL;
1506
1507 /* At least, container should include type and length fields. */
1508 if (header.length < sizeof(unsigned int) * 2)
1509 return -EINVAL;
1510 container_size = header.length;
1511 container = buf->tlv;
1512
1513 kctl = snd_ctl_find_numid(file->card, header.numid);
1514 if (kctl == NULL)
1515 return -ENOENT;
1516
1517 /* Calculate index of the element in this set. */
1518 id = kctl->id;
1519 snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1520 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1521
1522 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1523 return call_tlv_handler(file, op_flag, kctl, &id, container,
1524 container_size);
1525 } else {
1526 if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1527 return read_tlv_buf(kctl, &id, container,
1528 container_size);
1529 }
1530 }
1531
1532 /* Not supported. */
1533 return -ENXIO;
1534}
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1537{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001538 struct snd_ctl_file *ctl;
1539 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001540 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 void __user *argp = (void __user *)arg;
1542 int __user *ip = argp;
1543 int err;
1544
1545 ctl = file->private_data;
1546 card = ctl->card;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001547 if (snd_BUG_ON(!card))
1548 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 switch (cmd) {
1550 case SNDRV_CTL_IOCTL_PVERSION:
1551 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1552 case SNDRV_CTL_IOCTL_CARD_INFO:
1553 return snd_ctl_card_info(card, ctl, cmd, argp);
1554 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001555 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 case SNDRV_CTL_IOCTL_ELEM_INFO:
1557 return snd_ctl_elem_info_user(ctl, argp);
1558 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001559 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1561 return snd_ctl_elem_write_user(ctl, argp);
1562 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1563 return snd_ctl_elem_lock(ctl, argp);
1564 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1565 return snd_ctl_elem_unlock(ctl, argp);
1566 case SNDRV_CTL_IOCTL_ELEM_ADD:
1567 return snd_ctl_elem_add_user(ctl, argp, 0);
1568 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1569 return snd_ctl_elem_add_user(ctl, argp, 1);
1570 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1571 return snd_ctl_elem_remove(ctl, argp);
1572 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1573 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001574 case SNDRV_CTL_IOCTL_TLV_READ:
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001575 down_read(&ctl->card->controls_rwsem);
1576 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1577 up_read(&ctl->card->controls_rwsem);
1578 return err;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001579 case SNDRV_CTL_IOCTL_TLV_WRITE:
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001580 down_write(&ctl->card->controls_rwsem);
1581 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1582 up_write(&ctl->card->controls_rwsem);
1583 return err;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001584 case SNDRV_CTL_IOCTL_TLV_COMMAND:
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001585 down_write(&ctl->card->controls_rwsem);
1586 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1587 up_write(&ctl->card->controls_rwsem);
1588 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001590 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 case SNDRV_CTL_IOCTL_POWER_STATE:
1592#ifdef CONFIG_PM
1593 return put_user(card->power_state, ip) ? -EFAULT : 0;
1594#else
1595 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1596#endif
1597 }
1598 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001599 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 err = p->fioctl(card, ctl, cmd, arg);
1601 if (err != -ENOIOCTLCMD) {
1602 up_read(&snd_ioctl_rwsem);
1603 return err;
1604 }
1605 }
1606 up_read(&snd_ioctl_rwsem);
Takashi Iwaibb009452014-02-04 18:18:16 +01001607 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return -ENOTTY;
1609}
1610
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001611static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1612 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001614 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 int err = 0;
1616 ssize_t result = 0;
1617
1618 ctl = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001619 if (snd_BUG_ON(!ctl || !ctl->card))
1620 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (!ctl->subscribed)
1622 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001623 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return -EINVAL;
1625 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001626 while (count >= sizeof(struct snd_ctl_event)) {
1627 struct snd_ctl_event ev;
1628 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 while (list_empty(&ctl->events)) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001630 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1632 err = -EAGAIN;
1633 goto __end_lock;
1634 }
1635 init_waitqueue_entry(&wait, current);
1636 add_wait_queue(&ctl->change_sleep, &wait);
1637 set_current_state(TASK_INTERRUPTIBLE);
1638 spin_unlock_irq(&ctl->read_lock);
1639 schedule();
1640 remove_wait_queue(&ctl->change_sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001641 if (ctl->card->shutdown)
1642 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001644 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 spin_lock_irq(&ctl->read_lock);
1646 }
1647 kev = snd_kctl_event(ctl->events.next);
1648 ev.type = SNDRV_CTL_EVENT_ELEM;
1649 ev.data.elem.mask = kev->mask;
1650 ev.data.elem.id = kev->id;
1651 list_del(&kev->list);
1652 spin_unlock_irq(&ctl->read_lock);
1653 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001654 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 err = -EFAULT;
1656 goto __end;
1657 }
1658 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001659 buffer += sizeof(struct snd_ctl_event);
1660 count -= sizeof(struct snd_ctl_event);
1661 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663 __end_lock:
1664 spin_unlock_irq(&ctl->read_lock);
1665 __end:
1666 return result > 0 ? result : err;
1667}
1668
1669static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1670{
1671 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001672 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 ctl = file->private_data;
1675 if (!ctl->subscribed)
1676 return 0;
1677 poll_wait(file, &ctl->change_sleep, wait);
1678
1679 mask = 0;
1680 if (!list_empty(&ctl->events))
1681 mask |= POLLIN | POLLRDNORM;
1682
1683 return mask;
1684}
1685
1686/*
1687 * register the device-specific control-ioctls.
1688 * called from each device manager like pcm.c, hwdep.c, etc.
1689 */
1690static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1691{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001692 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001694 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (pn == NULL)
1696 return -ENOMEM;
1697 pn->fioctl = fcn;
1698 down_write(&snd_ioctl_rwsem);
1699 list_add_tail(&pn->list, lists);
1700 up_write(&snd_ioctl_rwsem);
1701 return 0;
1702}
1703
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001704/**
1705 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1706 * @fcn: ioctl callback function
1707 *
1708 * called from each device manager like pcm.c, hwdep.c, etc.
1709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1711{
1712 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1713}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001714EXPORT_SYMBOL(snd_ctl_register_ioctl);
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716#ifdef CONFIG_COMPAT
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001717/**
1718 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1719 * control-ioctls
1720 * @fcn: ioctl callback function
1721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1723{
1724 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1725}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001726EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727#endif
1728
1729/*
1730 * de-register the device-specific control-ioctls.
1731 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001732static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1733 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001735 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001737 if (snd_BUG_ON(!fcn))
1738 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001740 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (p->fioctl == fcn) {
1742 list_del(&p->list);
1743 up_write(&snd_ioctl_rwsem);
1744 kfree(p);
1745 return 0;
1746 }
1747 }
1748 up_write(&snd_ioctl_rwsem);
1749 snd_BUG();
1750 return -EINVAL;
1751}
1752
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001753/**
1754 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1755 * @fcn: ioctl callback function to unregister
1756 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1758{
1759 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1760}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001761EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763#ifdef CONFIG_COMPAT
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001764/**
1765 * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
1766 * control-ioctls
1767 * @fcn: ioctl callback function to unregister
1768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1770{
1771 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1772}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001773EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774#endif
1775
1776static int snd_ctl_fasync(int fd, struct file * file, int on)
1777{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001778 struct snd_ctl_file *ctl;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 ctl = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001781 return fasync_helper(fd, file, on, &ctl->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Takashi Iwai23c18d42014-02-19 14:30:29 +01001784/* return the preferred subdevice number if already assigned;
1785 * otherwise return -1
1786 */
1787int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
1788{
1789 struct snd_ctl_file *kctl;
1790 int subdevice = -1;
1791
1792 read_lock(&card->ctl_files_rwlock);
1793 list_for_each_entry(kctl, &card->ctl_files, list) {
1794 if (kctl->pid == task_pid(current)) {
1795 subdevice = kctl->preferred_subdevice[type];
1796 if (subdevice != -1)
1797 break;
1798 }
1799 }
1800 read_unlock(&card->ctl_files_rwlock);
1801 return subdevice;
1802}
1803EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805/*
1806 * ioctl32 compat
1807 */
1808#ifdef CONFIG_COMPAT
1809#include "control_compat.c"
1810#else
1811#define snd_ctl_ioctl_compat NULL
1812#endif
1813
1814/*
1815 * INIT PART
1816 */
1817
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001818static const struct file_operations snd_ctl_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 .owner = THIS_MODULE,
1821 .read = snd_ctl_read,
1822 .open = snd_ctl_open,
1823 .release = snd_ctl_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001824 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 .poll = snd_ctl_poll,
1826 .unlocked_ioctl = snd_ctl_ioctl,
1827 .compat_ioctl = snd_ctl_ioctl_compat,
1828 .fasync = snd_ctl_fasync,
1829};
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831/*
1832 * registration of the control device
1833 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001834static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001836 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Takashi Iwai40a4b262015-01-30 08:34:58 +01001838 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1839 &snd_ctl_f_ops, card, &card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
1842/*
1843 * disconnection of the control device
1844 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001845static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001847 struct snd_card *card = device->device_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001848 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Takashi Iwaid8009882008-09-07 12:51:13 +02001850 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001851 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 wake_up(&ctl->change_sleep);
1853 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1854 }
Takashi Iwaid8009882008-09-07 12:51:13 +02001855 read_unlock(&card->ctl_files_rwlock);
Takashi Iwaic4614822006-06-23 14:38:23 +02001856
Takashi Iwai40a4b262015-01-30 08:34:58 +01001857 return snd_unregister_device(&card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
1860/*
1861 * free all controls
1862 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001863static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001865 struct snd_card *card = device->device_data;
1866 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 down_write(&card->controls_rwsem);
1869 while (!list_empty(&card->controls)) {
1870 control = snd_kcontrol(card->controls.next);
1871 snd_ctl_remove(card, control);
1872 }
1873 up_write(&card->controls_rwsem);
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001874 put_device(&card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 0;
1876}
1877
1878/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 * create control core:
1880 * called from init.c
1881 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001882int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001884 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 .dev_free = snd_ctl_dev_free,
1886 .dev_register = snd_ctl_dev_register,
1887 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 };
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001889 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001891 if (snd_BUG_ON(!card))
1892 return -ENXIO;
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001893 if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
1894 return -ENXIO;
1895
1896 snd_device_initialize(&card->ctl_dev, card);
1897 dev_set_name(&card->ctl_dev, "controlC%d", card->number);
1898
1899 err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1900 if (err < 0)
1901 put_device(&card->ctl_dev);
1902 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001904
1905/*
Clemens Ladisch96007322011-01-10 16:25:44 +01001906 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001907 */
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001908
1909/**
1910 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
1911 * callback with a mono channel
1912 * @kcontrol: the kcontrol instance
1913 * @uinfo: info to store
1914 *
1915 * This is a function that can be used as info callback for a standard
1916 * boolean control with a single mono channel.
1917 */
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001918int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1919 struct snd_ctl_elem_info *uinfo)
1920{
1921 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1922 uinfo->count = 1;
1923 uinfo->value.integer.min = 0;
1924 uinfo->value.integer.max = 1;
1925 return 0;
1926}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001927EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1928
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001929/**
1930 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
1931 * callback with stereo two channels
1932 * @kcontrol: the kcontrol instance
1933 * @uinfo: info to store
1934 *
1935 * This is a function that can be used as info callback for a standard
1936 * boolean control with stereo two channels.
1937 */
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001938int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1939 struct snd_ctl_elem_info *uinfo)
1940{
1941 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1942 uinfo->count = 2;
1943 uinfo->value.integer.min = 0;
1944 uinfo->value.integer.max = 1;
1945 return 0;
1946}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001947EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
Clemens Ladisch96007322011-01-10 16:25:44 +01001948
1949/**
1950 * snd_ctl_enum_info - fills the info structure for an enumerated control
1951 * @info: the structure to be filled
1952 * @channels: the number of the control's channels; often one
1953 * @items: the number of control values; also the size of @names
1954 * @names: an array containing the names of all control values
1955 *
1956 * Sets all required fields in @info to their appropriate values.
1957 * If the control's accessibility is not the default (readable and writable),
1958 * the caller has to fill @info->access.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001959 *
1960 * Return: Zero.
Clemens Ladisch96007322011-01-10 16:25:44 +01001961 */
1962int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1963 unsigned int items, const char *const names[])
1964{
1965 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1966 info->count = channels;
1967 info->value.enumerated.items = items;
Takashi Iwaia7e6fb92014-10-20 18:08:50 +02001968 if (!items)
1969 return 0;
Clemens Ladisch96007322011-01-10 16:25:44 +01001970 if (info->value.enumerated.item >= items)
1971 info->value.enumerated.item = items - 1;
Takashi Iwaidf803e12014-10-20 18:07:21 +02001972 WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
1973 "ALSA: too long item name '%s'\n",
1974 names[info->value.enumerated.item]);
Clemens Ladisch96007322011-01-10 16:25:44 +01001975 strlcpy(info->value.enumerated.name,
1976 names[info->value.enumerated.item],
1977 sizeof(info->value.enumerated.name));
1978 return 0;
1979}
1980EXPORT_SYMBOL(snd_ctl_enum_info);