blob: 5be5b9b931bfefee5a2798775b7c33d02f42222e [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Routines for driver control interface
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/threads.h>
8#include <linux/interrupt.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -04009#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/slab.h>
11#include <linux/vmalloc.h>
12#include <linux/time.h>
Al Viro88a89032018-01-07 13:09:15 -050013#include <linux/mm.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <sound/core.h>
16#include <sound/minors.h>
17#include <sound/info.h>
18#include <sound/control.h>
19
20/* max number of user-defined controls */
21#define MAX_USER_CONTROLS 32
Dan Rosenberg5591bf02010-09-28 14:18:20 -040022#define MAX_CONTROL_COUNT 1028
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Takashi Iwai82e9bae2005-11-17 13:53:23 +010024struct snd_kctl_ioctl {
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 struct list_head list; /* list of all ioctls */
26 snd_kctl_ioctl_func_t fioctl;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010027};
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static DECLARE_RWSEM(snd_ioctl_rwsem);
30static LIST_HEAD(snd_control_ioctls);
31#ifdef CONFIG_COMPAT
32static LIST_HEAD(snd_control_compat_ioctls);
33#endif
34
35static int snd_ctl_open(struct inode *inode, struct file *file)
36{
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010038 struct snd_card *card;
39 struct snd_ctl_file *ctl;
Takashi Iwai23c18d42014-02-19 14:30:29 +010040 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +030042 err = stream_open(inode, file);
Takashi Iwai02f48652010-04-13 11:49:04 +020043 if (err < 0)
44 return err;
45
Clemens Ladischf87135f2005-11-20 14:06:59 +010046 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (!card) {
48 err = -ENODEV;
49 goto __error1;
50 }
51 err = snd_card_file_add(card, file);
52 if (err < 0) {
53 err = -ENODEV;
54 goto __error1;
55 }
56 if (!try_module_get(card->module)) {
57 err = -EFAULT;
58 goto __error2;
59 }
Takashi Iwaica2c0962005-09-09 14:20:23 +020060 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (ctl == NULL) {
62 err = -ENOMEM;
63 goto __error;
64 }
65 INIT_LIST_HEAD(&ctl->events);
66 init_waitqueue_head(&ctl->change_sleep);
67 spin_lock_init(&ctl->read_lock);
68 ctl->card = card;
Takashi Iwai23c18d42014-02-19 14:30:29 +010069 for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
70 ctl->preferred_subdevice[i] = -1;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +010071 ctl->pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 file->private_data = ctl;
73 write_lock_irqsave(&card->ctl_files_rwlock, flags);
74 list_add_tail(&ctl->list, &card->ctl_files);
75 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
Takashi Iwaia0830db2012-10-16 13:05:59 +020076 snd_card_unref(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return 0;
78
79 __error:
80 module_put(card->module);
81 __error2:
82 snd_card_file_remove(card, file);
83 __error1:
Takashi Iwaia0830db2012-10-16 13:05:59 +020084 if (card)
85 snd_card_unref(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return err;
87}
88
Takashi Iwai82e9bae2005-11-17 13:53:23 +010089static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Borislav Petkov7507e8d2007-10-22 17:11:09 +020091 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010092 struct snd_kctl_event *cread;
Richard Fitzgeralddd5f3132018-02-27 17:29:54 +000093
Borislav Petkov7507e8d2007-10-22 17:11:09 +020094 spin_lock_irqsave(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 while (!list_empty(&ctl->events)) {
96 cread = snd_kctl_event(ctl->events.next);
97 list_del(&cread->list);
98 kfree(cread);
99 }
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200100 spin_unlock_irqrestore(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103static int snd_ctl_release(struct inode *inode, struct file *file)
104{
105 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100106 struct snd_card *card;
107 struct snd_ctl_file *ctl;
108 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 unsigned int idx;
110
111 ctl = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 file->private_data = NULL;
113 card = ctl->card;
114 write_lock_irqsave(&card->ctl_files_rwlock, flags);
115 list_del(&ctl->list);
116 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
117 down_write(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200118 list_for_each_entry(control, &card->controls, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 for (idx = 0; idx < control->count; idx++)
120 if (control->vd[idx].owner == ctl)
121 control->vd[idx].owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 up_write(&card->controls_rwsem);
123 snd_ctl_empty_read_queue(ctl);
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100124 put_pid(ctl->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 kfree(ctl);
126 module_put(card->module);
127 snd_card_file_remove(card, file);
128 return 0;
129}
130
Takashi Iwai12cddbd2014-10-30 13:44:34 +0100131/**
132 * snd_ctl_notify - Send notification to user-space for a control change
133 * @card: the card to send notification
134 * @mask: the event mask, SNDRV_CTL_EVENT_*
135 * @id: the ctl element id to send notification
136 *
137 * This function adds an event record with the given id and mask, appends
138 * to the list and wakes up the user-space for notification. This can be
139 * called in the atomic context.
140 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100141void snd_ctl_notify(struct snd_card *card, unsigned int mask,
142 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100145 struct snd_ctl_file *ctl;
146 struct snd_kctl_event *ev;
Richard Fitzgeralddd5f3132018-02-27 17:29:54 +0000147
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200148 if (snd_BUG_ON(!card || !id))
149 return;
Takashi Iwaif388cdc2016-07-08 08:05:19 +0200150 if (card->shutdown)
151 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 read_lock(&card->ctl_files_rwlock);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100153#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 card->mixer_oss_change_count++;
155#endif
Johannes Berg9244b2c2006-10-05 16:02:22 +0200156 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!ctl->subscribed)
158 continue;
159 spin_lock_irqsave(&ctl->read_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200160 list_for_each_entry(ev, &ctl->events, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (ev->id.numid == id->numid) {
162 ev->mask |= mask;
163 goto _found;
164 }
165 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200166 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (ev) {
168 ev->id = *id;
169 ev->mask = mask;
170 list_add_tail(&ev->list, &ctl->events);
171 } else {
Takashi Iwaibb009452014-02-04 18:18:16 +0100172 dev_err(card->dev, "No memory available to allocate event\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 _found:
175 wake_up(&ctl->change_sleep);
176 spin_unlock_irqrestore(&ctl->read_lock, flags);
177 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
178 }
179 read_unlock(&card->ctl_files_rwlock);
180}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200181EXPORT_SYMBOL(snd_ctl_notify);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/**
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900184 * snd_ctl_new - create a new control instance with some elements
185 * @kctl: the pointer to store new control instance
186 * @count: the number of elements in this control
187 * @access: the default access flags for elements in this control
188 * @file: given when locking these elements
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900190 * Allocates a memory object for a new control instance. The instance has
191 * elements as many as the given number (@count). Each element has given
192 * access permissions (@access). Each element is locked when @file is given.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900194 * Return: 0 on success, error code on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900196static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
197 unsigned int access, struct snd_ctl_file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900199 unsigned int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 unsigned int idx;
Richard Fitzgeralddd5f3132018-02-27 17:29:54 +0000201
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900202 if (count == 0 || count > MAX_CONTROL_COUNT)
203 return -EINVAL;
Dan Rosenberg5591bf02010-09-28 14:18:20 -0400204
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900205 size = sizeof(struct snd_kcontrol);
206 size += sizeof(struct snd_kcontrol_volatile) * count;
Dan Rosenberg5591bf02010-09-28 14:18:20 -0400207
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900208 *kctl = kzalloc(size, GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +0100209 if (!*kctl)
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900210 return -ENOMEM;
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900211
212 for (idx = 0; idx < count; idx++) {
213 (*kctl)->vd[idx].access = access;
214 (*kctl)->vd[idx].owner = file;
215 }
216 (*kctl)->count = count;
217
218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/**
222 * snd_ctl_new1 - create a control instance from the template
223 * @ncontrol: the initialization record
224 * @private_data: the private data to set
225 *
Richard Fitzgeralddd5f3132018-02-27 17:29:54 +0000226 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * template. When the access field of ncontrol is 0, it's assumed as
228 * READWRITE access. When the count field is 0, it's assumes as one.
229 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100230 * Return: The pointer of the newly generated instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100232struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
233 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900235 struct snd_kcontrol *kctl;
236 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 unsigned int access;
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900238 int err;
Richard Fitzgeralddd5f3132018-02-27 17:29:54 +0000239
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200240 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
241 return NULL;
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900242
243 count = ncontrol->count;
244 if (count == 0)
245 count = 1;
246
247 access = ncontrol->access;
248 if (access == 0)
249 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
250 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
251 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
252 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
253 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
254 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
255 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
256
257 err = snd_ctl_new(&kctl, count, access, NULL);
258 if (err < 0)
259 return NULL;
260
261 /* The 'numid' member is decided when calling snd_ctl_add(). */
262 kctl->id.iface = ncontrol->iface;
263 kctl->id.device = ncontrol->device;
264 kctl->id.subdevice = ncontrol->subdevice;
Mark Brown366840d2008-10-29 14:40:30 +0000265 if (ncontrol->name) {
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900266 strlcpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
267 if (strcmp(ncontrol->name, kctl->id.name) != 0)
Takashi Iwaibb009452014-02-04 18:18:16 +0100268 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900269 ncontrol->name, kctl->id.name);
Mark Brown366840d2008-10-29 14:40:30 +0000270 }
Takashi Sakamoto2225e792015-03-10 22:13:31 +0900271 kctl->id.index = ncontrol->index;
272
273 kctl->info = ncontrol->info;
274 kctl->get = ncontrol->get;
275 kctl->put = ncontrol->put;
276 kctl->tlv.p = ncontrol->tlv.p;
277
278 kctl->private_value = ncontrol->private_value;
279 kctl->private_data = private_data;
280
281 return kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200283EXPORT_SYMBOL(snd_ctl_new1);
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/**
286 * snd_ctl_free_one - release the control instance
287 * @kcontrol: the control instance
288 *
289 * Releases the control instance created via snd_ctl_new()
290 * or snd_ctl_new1().
291 * Don't call this after the control was added to the card.
292 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100293void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 if (kcontrol) {
296 if (kcontrol->private_free)
297 kcontrol->private_free(kcontrol);
298 kfree(kcontrol);
299 }
300}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200301EXPORT_SYMBOL(snd_ctl_free_one);
302
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100303static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
304 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100306 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Lars-Peter Clausenac902c12014-06-18 13:32:34 +0200308 /* Make sure that the ids assigned to the control do not wrap around */
309 if (card->last_numid >= UINT_MAX - count)
310 card->last_numid = 0;
311
Johannes Berg9244b2c2006-10-05 16:02:22 +0200312 list_for_each_entry(kctl, &card->controls, list) {
Clemens Ladisch7c733582011-03-07 13:22:50 +0100313 if (kctl->id.numid < card->last_numid + 1 + count &&
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100314 kctl->id.numid + kctl->count > card->last_numid + 1) {
315 card->last_numid = kctl->id.numid + kctl->count - 1;
316 return true;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100319 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100322static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100324 unsigned int iter = 100000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100326 while (snd_ctl_remove_numid_conflict(card, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (--iter == 0) {
328 /* this situation is very unlikely */
Takashi Iwaibb009452014-02-04 18:18:16 +0100329 dev_err(card->dev, "unable to allocate new control numid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -ENOMEM;
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 return 0;
334}
335
Takashi Iwai3103c082018-11-22 15:22:40 +0100336enum snd_ctl_add_mode {
337 CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
338};
339
340/* add/replace a new kcontrol object; call with card->controls_rwsem locked */
341static int __snd_ctl_add_replace(struct snd_card *card,
342 struct snd_kcontrol *kcontrol,
343 enum snd_ctl_add_mode mode)
Takashi Iwaie1a7bfe2018-11-22 14:36:17 +0100344{
345 struct snd_ctl_elem_id id;
346 unsigned int idx;
347 unsigned int count;
Takashi Iwai3103c082018-11-22 15:22:40 +0100348 struct snd_kcontrol *old;
349 int err;
Takashi Iwaie1a7bfe2018-11-22 14:36:17 +0100350
351 id = kcontrol->id;
352 if (id.index > UINT_MAX - kcontrol->count)
353 return -EINVAL;
354
Takashi Iwai3103c082018-11-22 15:22:40 +0100355 old = snd_ctl_find_id(card, &id);
356 if (!old) {
357 if (mode == CTL_REPLACE)
358 return -EINVAL;
359 } else {
360 if (mode == CTL_ADD_EXCLUSIVE) {
361 dev_err(card->dev,
362 "control %i:%i:%i:%s:%i is already present\n",
363 id.iface, id.device, id.subdevice, id.name,
364 id.index);
365 return -EBUSY;
366 }
367
368 err = snd_ctl_remove(card, old);
369 if (err < 0)
370 return err;
Takashi Iwaie1a7bfe2018-11-22 14:36:17 +0100371 }
372
373 if (snd_ctl_find_hole(card, kcontrol->count) < 0)
374 return -ENOMEM;
375
376 list_add_tail(&kcontrol->list, &card->controls);
377 card->controls_count += kcontrol->count;
378 kcontrol->id.numid = card->last_numid + 1;
379 card->last_numid += kcontrol->count;
380
381 id = kcontrol->id;
382 count = kcontrol->count;
383 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
384 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
385
386 return 0;
387}
388
Takashi Iwai3103c082018-11-22 15:22:40 +0100389static int snd_ctl_add_replace(struct snd_card *card,
390 struct snd_kcontrol *kcontrol,
391 enum snd_ctl_add_mode mode)
392{
393 int err = -EINVAL;
394
395 if (! kcontrol)
396 return err;
397 if (snd_BUG_ON(!card || !kcontrol->info))
398 goto error;
399
400 down_write(&card->controls_rwsem);
401 err = __snd_ctl_add_replace(card, kcontrol, mode);
402 up_write(&card->controls_rwsem);
403 if (err < 0)
404 goto error;
405 return 0;
406
407 error:
408 snd_ctl_free_one(kcontrol);
409 return err;
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/**
413 * snd_ctl_add - add the control instance to the card
414 * @card: the card instance
415 * @kcontrol: the control instance to add
416 *
417 * Adds the control instance created via snd_ctl_new() or
418 * snd_ctl_new1() to the given card. Assigns also an unique
419 * numid used for fast search.
420 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * It frees automatically the control which cannot be added.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100422 *
423 * Return: Zero if successful, or a negative error code on failure.
424 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100426int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Takashi Iwai3103c082018-11-22 15:22:40 +0100428 return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200430EXPORT_SYMBOL(snd_ctl_add);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/**
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000433 * snd_ctl_replace - replace the control instance of the card
434 * @card: the card instance
435 * @kcontrol: the control instance to replace
436 * @add_on_replace: add the control if not already added
437 *
438 * Replaces the given control. If the given control does not exist
439 * and the add_on_replace flag is set, the control is added. If the
440 * control exists, it is destroyed first.
441 *
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000442 * It frees automatically the control which cannot be added or replaced.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100443 *
444 * Return: Zero if successful, or a negative error code on failure.
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000445 */
446int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
447 bool add_on_replace)
448{
Takashi Iwai3103c082018-11-22 15:22:40 +0100449 return snd_ctl_add_replace(card, kcontrol,
450 add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000451}
452EXPORT_SYMBOL(snd_ctl_replace);
453
454/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * snd_ctl_remove - remove the control from the card and release it
456 * @card: the card instance
457 * @kcontrol: the control instance to remove
458 *
459 * Removes the control from the card and then releases the instance.
460 * You don't need to call snd_ctl_free_one(). You must be in
461 * the write lock - down_write(&card->controls_rwsem).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100462 *
463 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100465int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100467 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 unsigned int idx;
469
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200470 if (snd_BUG_ON(!card || !kcontrol))
471 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 list_del(&kcontrol->list);
473 card->controls_count -= kcontrol->count;
474 id = kcontrol->id;
475 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
476 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
477 snd_ctl_free_one(kcontrol);
478 return 0;
479}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200480EXPORT_SYMBOL(snd_ctl_remove);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/**
483 * snd_ctl_remove_id - remove the control of the given id and release it
484 * @card: the card instance
485 * @id: the control id to remove
486 *
487 * Finds the control instance with the given id, removes it from the
488 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100489 *
490 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100492int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100494 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int ret;
496
497 down_write(&card->controls_rwsem);
498 kctl = snd_ctl_find_id(card, id);
499 if (kctl == NULL) {
500 up_write(&card->controls_rwsem);
501 return -ENOENT;
502 }
503 ret = snd_ctl_remove(card, kctl);
504 up_write(&card->controls_rwsem);
505 return ret;
506}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200507EXPORT_SYMBOL(snd_ctl_remove_id);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/**
Clemens Ladischf217ac52009-08-17 12:27:22 +0200510 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * @file: active control handle
512 * @id: the control id to remove
513 *
514 * Finds the control instance with the given id, removes it from the
515 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100516 *
517 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
Clemens Ladischf217ac52009-08-17 12:27:22 +0200519static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
520 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100522 struct snd_card *card = file->card;
523 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int idx, ret;
525
526 down_write(&card->controls_rwsem);
527 kctl = snd_ctl_find_id(card, id);
528 if (kctl == NULL) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200529 ret = -ENOENT;
530 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Clemens Ladisch18dd0aa2009-08-17 12:28:09 +0200532 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
533 ret = -EINVAL;
534 goto error;
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 for (idx = 0; idx < kctl->count; idx++)
537 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200538 ret = -EBUSY;
539 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 ret = snd_ctl_remove(card, kctl);
Clemens Ladischf217ac52009-08-17 12:27:22 +0200542 if (ret < 0)
543 goto error;
544 card->user_ctl_count--;
Clemens Ladisch317b8082009-08-17 12:26:34 +0200545error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 up_write(&card->controls_rwsem);
547 return ret;
548}
549
550/**
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200551 * snd_ctl_activate_id - activate/inactivate the control of the given id
552 * @card: the card instance
553 * @id: the control id to activate/inactivate
554 * @active: non-zero to activate
555 *
556 * Finds the control instance with the given id, and activate or
557 * inactivate the control together with notification, if changed.
Takashi Sakamotoc78497e2015-04-11 17:41:02 +0900558 * The given ID data is filled with full information.
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200559 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100560 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200561 */
562int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
563 int active)
564{
565 struct snd_kcontrol *kctl;
566 struct snd_kcontrol_volatile *vd;
567 unsigned int index_offset;
568 int ret;
569
570 down_write(&card->controls_rwsem);
571 kctl = snd_ctl_find_id(card, id);
572 if (kctl == NULL) {
573 ret = -ENOENT;
574 goto unlock;
575 }
Lars-Peter Clausen31584ed2014-11-07 14:12:34 +0100576 index_offset = snd_ctl_get_ioff(kctl, id);
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200577 vd = &kctl->vd[index_offset];
578 ret = 0;
579 if (active) {
580 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
581 goto unlock;
582 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
583 } else {
584 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
585 goto unlock;
586 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
587 }
Takashi Sakamotoc78497e2015-04-11 17:41:02 +0900588 snd_ctl_build_ioff(id, kctl, index_offset);
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200589 ret = 1;
590 unlock:
591 up_write(&card->controls_rwsem);
592 if (ret > 0)
593 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
594 return ret;
595}
596EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
597
598/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * snd_ctl_rename_id - replace the id of a control on the card
600 * @card: the card instance
601 * @src_id: the old id
602 * @dst_id: the new id
603 *
604 * Finds the control with the old id from the card, and replaces the
605 * id with the new one.
606 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100607 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100609int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
610 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100612 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 down_write(&card->controls_rwsem);
615 kctl = snd_ctl_find_id(card, src_id);
616 if (kctl == NULL) {
617 up_write(&card->controls_rwsem);
618 return -ENOENT;
619 }
620 kctl->id = *dst_id;
621 kctl->id.numid = card->last_numid + 1;
622 card->last_numid += kctl->count;
623 up_write(&card->controls_rwsem);
624 return 0;
625}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200626EXPORT_SYMBOL(snd_ctl_rename_id);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/**
629 * snd_ctl_find_numid - find the control instance with the given number-id
630 * @card: the card instance
631 * @numid: the number-id to search
632 *
633 * Finds the control instance with the given number-id from the card.
634 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * The caller must down card->controls_rwsem before calling this function
636 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100637 *
638 * Return: The pointer of the instance if found, or %NULL if not.
639 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100641struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100643 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200645 if (snd_BUG_ON(!card || !numid))
646 return NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200647 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
649 return kctl;
650 }
651 return NULL;
652}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200653EXPORT_SYMBOL(snd_ctl_find_numid);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/**
656 * snd_ctl_find_id - find the control instance with the given id
657 * @card: the card instance
658 * @id: the id to search
659 *
660 * Finds the control instance with the given id from the card.
661 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * The caller must down card->controls_rwsem before calling this function
663 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100664 *
665 * Return: The pointer of the instance if found, or %NULL if not.
666 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100668struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
669 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100671 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200673 if (snd_BUG_ON(!card || !id))
674 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (id->numid != 0)
676 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200677 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (kctl->id.iface != id->iface)
679 continue;
680 if (kctl->id.device != id->device)
681 continue;
682 if (kctl->id.subdevice != id->subdevice)
683 continue;
684 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
685 continue;
686 if (kctl->id.index > id->index)
687 continue;
688 if (kctl->id.index + kctl->count <= id->index)
689 continue;
690 return kctl;
691 }
692 return NULL;
693}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200694EXPORT_SYMBOL(snd_ctl_find_id);
695
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100696static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 unsigned int cmd, void __user *arg)
698{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100699 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Takashi Iwaica2c0962005-09-09 14:20:23 +0200701 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (! info)
703 return -ENOMEM;
704 down_read(&snd_ioctl_rwsem);
705 info->card = card->number;
706 strlcpy(info->id, card->id, sizeof(info->id));
707 strlcpy(info->driver, card->driver, sizeof(info->driver));
708 strlcpy(info->name, card->shortname, sizeof(info->name));
709 strlcpy(info->longname, card->longname, sizeof(info->longname));
710 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
711 strlcpy(info->components, card->components, sizeof(info->components));
712 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100713 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 kfree(info);
715 return -EFAULT;
716 }
717 kfree(info);
718 return 0;
719}
720
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100721static int snd_ctl_elem_list(struct snd_card *card,
722 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100724 struct snd_ctl_elem_list list;
725 struct snd_kcontrol *kctl;
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200726 struct snd_ctl_elem_id id;
Luca Tettamanti78fa2c42011-05-25 22:43:27 +0200727 unsigned int offset, space, jidx;
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200728 int err = 0;
Richard Fitzgeralddd5f3132018-02-27 17:29:54 +0000729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (copy_from_user(&list, _list, sizeof(list)))
731 return -EFAULT;
732 offset = list.offset;
733 space = list.space;
Takashi Sakamoto4e361d32017-05-24 10:04:30 +0900734
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200735 down_read(&card->controls_rwsem);
736 list.count = card->controls_count;
737 list.used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (space > 0) {
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200739 list_for_each_entry(kctl, &card->controls, list) {
740 if (offset >= kctl->count) {
741 offset -= kctl->count;
742 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200744 for (jidx = offset; jidx < kctl->count; jidx++) {
745 snd_ctl_build_ioff(&id, kctl, jidx);
746 if (copy_to_user(list.pids + list.used, &id,
747 sizeof(id))) {
748 err = -EFAULT;
749 goto out;
750 }
751 list.used++;
752 if (!--space)
753 goto out;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 offset = 0;
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Takashi Iwai53e7bf42017-05-22 17:43:04 +0200758 out:
759 up_read(&card->controls_rwsem);
760 if (!err && copy_to_user(_list, &list, sizeof(list)))
761 err = -EFAULT;
762 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Takashi Sakamoto860c1992016-07-07 21:57:10 +0900765static bool validate_element_member_dimension(struct snd_ctl_elem_info *info)
766{
767 unsigned int members;
768 unsigned int i;
769
770 if (info->dimen.d[0] == 0)
771 return true;
772
773 members = 1;
774 for (i = 0; i < ARRAY_SIZE(info->dimen.d); ++i) {
775 if (info->dimen.d[i] == 0)
776 break;
777 members *= info->dimen.d[i];
778
779 /*
780 * info->count should be validated in advance, to guarantee
781 * calculation soundness.
782 */
783 if (members > info->count)
784 return false;
785 }
786
787 for (++i; i < ARRAY_SIZE(info->dimen.d); ++i) {
788 if (info->dimen.d[i] > 0)
789 return false;
790 }
791
792 return members == info->count;
793}
794
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100795static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
796 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100798 struct snd_card *card = ctl->card;
799 struct snd_kcontrol *kctl;
800 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned int index_offset;
802 int result;
Richard Fitzgeralddd5f3132018-02-27 17:29:54 +0000803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 down_read(&card->controls_rwsem);
805 kctl = snd_ctl_find_id(card, &info->id);
806 if (kctl == NULL) {
807 up_read(&card->controls_rwsem);
808 return -ENOENT;
809 }
810#ifdef CONFIG_SND_DEBUG
811 info->access = 0;
812#endif
813 result = kctl->info(kctl, info);
814 if (result >= 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200815 snd_BUG_ON(info->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 index_offset = snd_ctl_get_ioff(kctl, &info->id);
817 vd = &kctl->vd[index_offset];
818 snd_ctl_build_ioff(&info->id, kctl, index_offset);
819 info->access = vd->access;
820 if (vd->owner) {
821 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
822 if (vd->owner == ctl)
823 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100824 info->owner = pid_vnr(vd->owner->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 } else {
826 info->owner = -1;
827 }
828 }
829 up_read(&card->controls_rwsem);
830 return result;
831}
832
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100833static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
834 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100836 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int result;
838
839 if (copy_from_user(&info, _info, sizeof(info)))
840 return -EFAULT;
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200841 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Takashi Iwai7d8e8292017-08-30 16:13:25 +0200842 if (result < 0)
843 return result;
844 result = snd_ctl_elem_info(ctl, &info);
845 if (result < 0)
846 return result;
847 if (copy_to_user(_info, &info, sizeof(info)))
848 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return result;
850}
851
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200852static int snd_ctl_elem_read(struct snd_card *card,
853 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100855 struct snd_kcontrol *kctl;
856 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 unsigned int index_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 kctl = snd_ctl_find_id(card, &control->id);
Takashi Sakamotobecf9e52017-08-20 13:49:07 +0900860 if (kctl == NULL)
861 return -ENOENT;
862
863 index_offset = snd_ctl_get_ioff(kctl, &control->id);
864 vd = &kctl->vd[index_offset];
Richard Fitzgerald5a236992018-02-27 17:01:18 +0000865 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
Takashi Sakamotobecf9e52017-08-20 13:49:07 +0900866 return -EPERM;
867
868 snd_ctl_build_ioff(&control->id, kctl, index_offset);
869 return kctl->get(kctl, control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100872static int snd_ctl_elem_read_user(struct snd_card *card,
873 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100875 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int result;
Li Zefanef44a1e2009-04-10 09:43:08 +0800877
878 control = memdup_user(_control, sizeof(*control));
879 if (IS_ERR(control))
880 return PTR_ERR(control);
881
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200882 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai7d8e8292017-08-30 16:13:25 +0200883 if (result < 0)
884 goto error;
885
886 down_read(&card->controls_rwsem);
887 result = snd_ctl_elem_read(card, control);
888 up_read(&card->controls_rwsem);
889 if (result < 0)
890 goto error;
891
892 if (copy_to_user(_control, control, sizeof(*control)))
893 result = -EFAULT;
894 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 kfree(control);
896 return result;
897}
898
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200899static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
900 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100902 struct snd_kcontrol *kctl;
903 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100905 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 kctl = snd_ctl_find_id(card, &control->id);
Takashi Sakamotobecf9e52017-08-20 13:49:07 +0900908 if (kctl == NULL)
909 return -ENOENT;
910
911 index_offset = snd_ctl_get_ioff(kctl, &control->id);
912 vd = &kctl->vd[index_offset];
913 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
914 (file && vd->owner && vd->owner != file)) {
915 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Takashi Sakamotobecf9e52017-08-20 13:49:07 +0900917
918 snd_ctl_build_ioff(&control->id, kctl, index_offset);
919 result = kctl->put(kctl, control);
920 if (result < 0)
921 return result;
922
923 if (result > 0) {
924 struct snd_ctl_elem_id id = control->id;
925 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
926 }
927
928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100931static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
932 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100934 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100935 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int result;
937
Li Zefanef44a1e2009-04-10 09:43:08 +0800938 control = memdup_user(_control, sizeof(*control));
939 if (IS_ERR(control))
940 return PTR_ERR(control);
941
Giuliano Pochini64649402006-03-13 14:11:11 +0100942 card = file->card;
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200943 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai7d8e8292017-08-30 16:13:25 +0200944 if (result < 0)
945 goto error;
946
947 down_write(&card->controls_rwsem);
948 result = snd_ctl_elem_write(card, file, control);
949 up_write(&card->controls_rwsem);
950 if (result < 0)
951 goto error;
952
953 if (copy_to_user(_control, control, sizeof(*control)))
954 result = -EFAULT;
955 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 kfree(control);
957 return result;
958}
959
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100960static int snd_ctl_elem_lock(struct snd_ctl_file *file,
961 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100963 struct snd_card *card = file->card;
964 struct snd_ctl_elem_id id;
965 struct snd_kcontrol *kctl;
966 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int result;
Richard Fitzgeralddd5f3132018-02-27 17:29:54 +0000968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (copy_from_user(&id, _id, sizeof(id)))
970 return -EFAULT;
971 down_write(&card->controls_rwsem);
972 kctl = snd_ctl_find_id(card, &id);
973 if (kctl == NULL) {
974 result = -ENOENT;
975 } else {
976 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
977 if (vd->owner != NULL)
978 result = -EBUSY;
979 else {
980 vd->owner = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 result = 0;
982 }
983 }
984 up_write(&card->controls_rwsem);
985 return result;
986}
987
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100988static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
989 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100991 struct snd_card *card = file->card;
992 struct snd_ctl_elem_id id;
993 struct snd_kcontrol *kctl;
994 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int result;
Richard Fitzgeralddd5f3132018-02-27 17:29:54 +0000996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (copy_from_user(&id, _id, sizeof(id)))
998 return -EFAULT;
999 down_write(&card->controls_rwsem);
1000 kctl = snd_ctl_find_id(card, &id);
1001 if (kctl == NULL) {
1002 result = -ENOENT;
1003 } else {
1004 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1005 if (vd->owner == NULL)
1006 result = -EINVAL;
1007 else if (vd->owner != file)
1008 result = -EPERM;
1009 else {
1010 vd->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 result = 0;
1012 }
1013 }
1014 up_write(&card->controls_rwsem);
1015 return result;
1016}
1017
1018struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001019 struct snd_ctl_elem_info info;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001020 struct snd_card *card;
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001021 char *elem_data; /* element data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001023 void *tlv_data; /* TLV data */
1024 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 void *priv_data; /* private data (like strings for enumerated type) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026};
1027
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001028static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1029 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 struct user_element *ue = kcontrol->private_data;
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001032 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001034 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 *uinfo = ue->info;
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001036 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
1039}
1040
Clemens Ladisch8d448162011-10-07 22:38:59 +02001041static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1042 struct snd_ctl_elem_info *uinfo)
1043{
1044 struct user_element *ue = kcontrol->private_data;
1045 const char *names;
1046 unsigned int item;
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001047 unsigned int offset;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001048
1049 item = uinfo->value.enumerated.item;
1050
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001051 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
Clemens Ladisch8d448162011-10-07 22:38:59 +02001052 *uinfo = ue->info;
Takashi Sakamotoc378c3b2015-04-11 17:41:03 +09001053 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
Clemens Ladisch8d448162011-10-07 22:38:59 +02001054
1055 item = min(item, uinfo->value.enumerated.items - 1);
1056 uinfo->value.enumerated.item = item;
1057
1058 names = ue->priv_data;
1059 for (; item > 0; --item)
1060 names += strlen(names) + 1;
1061 strcpy(uinfo->value.enumerated.name, names);
1062
1063 return 0;
1064}
1065
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001066static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1067 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct user_element *ue = kcontrol->private_data;
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001070 unsigned int size = ue->elem_data_size;
1071 char *src = ue->elem_data +
1072 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001074 memcpy(&ucontrol->value, src, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 0;
1076}
1077
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001078static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1079 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 int change;
1082 struct user_element *ue = kcontrol->private_data;
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001083 unsigned int size = ue->elem_data_size;
1084 char *dst = ue->elem_data +
1085 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001086
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001087 change = memcmp(&ucontrol->value, dst, size) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (change)
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001089 memcpy(dst, &ucontrol->value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return change;
1091}
1092
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001093static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1094 unsigned int size)
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001095{
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001096 struct user_element *ue = kctl->private_data;
1097 unsigned int *container;
Takashi Sakamotoda428822017-08-24 10:46:15 +09001098 struct snd_ctl_elem_id id;
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001099 unsigned int mask = 0;
Takashi Sakamotoda428822017-08-24 10:46:15 +09001100 int i;
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001101 int change;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001102
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001103 if (size > 1024 * 128) /* sane value */
1104 return -EINVAL;
Takashi Sakamoto30d83402017-08-03 20:20:42 +09001105
Al Viro88a89032018-01-07 13:09:15 -05001106 container = vmemdup_user(buf, size);
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001107 if (IS_ERR(container))
1108 return PTR_ERR(container);
Li Zefanef44a1e2009-04-10 09:43:08 +08001109
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001110 change = ue->tlv_data_size != size;
1111 if (!change)
Takashi Iwai241bc822017-08-22 15:44:45 +02001112 change = memcmp(ue->tlv_data, container, size) != 0;
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001113 if (!change) {
Al Viro88a89032018-01-07 13:09:15 -05001114 kvfree(container);
Takashi Sakamoto30d83402017-08-03 20:20:42 +09001115 return 0;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001116 }
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001117
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001118 if (ue->tlv_data == NULL) {
1119 /* Now TLV data is available. */
1120 for (i = 0; i < kctl->count; ++i)
1121 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1122 mask = SNDRV_CTL_EVENT_MASK_INFO;
1123 }
1124
Al Viro88a89032018-01-07 13:09:15 -05001125 kvfree(ue->tlv_data);
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001126 ue->tlv_data = container;
1127 ue->tlv_data_size = size;
1128
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001129 mask |= SNDRV_CTL_EVENT_MASK_TLV;
Takashi Sakamotoda428822017-08-24 10:46:15 +09001130 for (i = 0; i < kctl->count; ++i) {
1131 snd_ctl_build_ioff(&id, kctl, i);
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001132 snd_ctl_notify(ue->card, mask, &id);
Takashi Sakamotoda428822017-08-24 10:46:15 +09001133 }
Takashi Sakamotofb8027e2017-08-24 10:46:14 +09001134
Takashi Sakamoto6d4d41f2017-08-03 20:20:44 +09001135 return change;
1136}
1137
1138static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1139 unsigned int size)
1140{
1141 struct user_element *ue = kctl->private_data;
1142
1143 if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1144 return -ENXIO;
1145
1146 if (size < ue->tlv_data_size)
1147 return -ENOSPC;
1148
1149 if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1150 return -EFAULT;
1151
1152 return 0;
1153}
1154
1155static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1156 unsigned int size, unsigned int __user *buf)
1157{
1158 if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1159 return replace_user_tlv(kctl, buf, size);
1160 else
1161 return read_user_tlv(kctl, buf, size);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001162}
1163
Clemens Ladisch8d448162011-10-07 22:38:59 +02001164static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1165{
1166 char *names, *p;
1167 size_t buf_len, name_len;
1168 unsigned int i;
Olof Johansson447c6f92011-11-05 22:51:54 +01001169 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001170
1171 if (ue->info.value.enumerated.names_length > 64 * 1024)
1172 return -EINVAL;
1173
Al Viro59aeaf32018-01-07 13:11:03 -05001174 names = vmemdup_user((const void __user *)user_ptrval,
Clemens Ladisch8d448162011-10-07 22:38:59 +02001175 ue->info.value.enumerated.names_length);
1176 if (IS_ERR(names))
1177 return PTR_ERR(names);
1178
1179 /* check that there are enough valid names */
1180 buf_len = ue->info.value.enumerated.names_length;
1181 p = names;
1182 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1183 name_len = strnlen(p, buf_len);
1184 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
Al Viro59aeaf32018-01-07 13:11:03 -05001185 kvfree(names);
Clemens Ladisch8d448162011-10-07 22:38:59 +02001186 return -EINVAL;
1187 }
1188 p += name_len + 1;
1189 buf_len -= name_len + 1;
1190 }
1191
1192 ue->priv_data = names;
1193 ue->info.value.enumerated.names_ptr = 0;
1194
1195 return 0;
1196}
1197
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001198static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001200 struct user_element *ue = kcontrol->private_data;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001201
Al Viro88a89032018-01-07 13:09:15 -05001202 kvfree(ue->tlv_data);
Al Viro59aeaf32018-01-07 13:11:03 -05001203 kvfree(ue->priv_data);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001204 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001207static int snd_ctl_elem_add(struct snd_ctl_file *file,
1208 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001210 /* The capacity of struct snd_ctl_elem_value.value.*/
1211 static const unsigned int value_sizes[] = {
1212 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
1213 [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
1214 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
1215 [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
1216 [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
1217 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
1218 };
1219 static const unsigned int max_value_counts[] = {
1220 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
1221 [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
1222 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
1223 [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
1224 [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
1225 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
1226 };
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001227 struct snd_card *card = file->card;
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001228 struct snd_kcontrol *kctl;
1229 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 unsigned int access;
1231 long private_size;
1232 struct user_element *ue;
Takashi Sakamotocab2ed72015-04-11 17:41:04 +09001233 unsigned int offset;
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001234 int err;
Lu Guanqun983929c2011-08-24 11:12:34 +08001235
Takashi Iwaibe3bb822015-03-11 18:12:49 +01001236 if (!*info->id.name)
1237 return -EINVAL;
1238 if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1239 return -EINVAL;
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001240
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001241 /* Delete a control to replace them if needed. */
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001242 if (replace) {
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001243 info->id.numid = 0;
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001244 err = snd_ctl_remove_user_ctl(file, &info->id);
1245 if (err)
1246 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001248
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001249 /*
1250 * The number of userspace controls are counted control by control,
1251 * not element by element.
1252 */
1253 if (card->user_ctl_count + 1 > MAX_USER_CONTROLS)
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001254 return -ENOMEM;
1255
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001256 /* Check the number of elements for this userspace control. */
1257 count = info->owner;
1258 if (count == 0)
1259 count = 1;
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001260
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001261 /* Arrange access permissions if needed. */
1262 access = info->access;
1263 if (access == 0)
1264 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1265 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1266 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001267 SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1268
1269 /* In initial state, nothing is available as TLV container. */
1270 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001271 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1272 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1273
1274 /*
1275 * Check information and calculate the size of data specific to
1276 * this userspace control.
1277 */
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001278 if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
1279 info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return -EINVAL;
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001281 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
1282 info->value.enumerated.items == 0)
1283 return -EINVAL;
1284 if (info->count < 1 ||
1285 info->count > max_value_counts[info->type])
1286 return -EINVAL;
Takashi Sakamoto860c1992016-07-07 21:57:10 +09001287 if (!validate_element_member_dimension(info))
1288 return -EINVAL;
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001289 private_size = value_sizes[info->type] * info->count;
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001290
1291 /*
1292 * Keep memory object for this userspace control. After passing this
1293 * code block, the instance should be freed by snd_ctl_free_one().
1294 *
1295 * Note that these elements in this control are locked.
1296 */
1297 err = snd_ctl_new(&kctl, count, access, file);
1298 if (err < 0)
1299 return err;
Takashi Iwaie79d74a2015-03-12 16:57:51 +01001300 memcpy(&kctl->id, &info->id, sizeof(kctl->id));
Takashi Sakamotoe1c78df2015-04-12 10:12:25 +09001301 kctl->private_data = kzalloc(sizeof(struct user_element) + private_size * count,
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001302 GFP_KERNEL);
1303 if (kctl->private_data == NULL) {
1304 kfree(kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return -ENOMEM;
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001306 }
1307 kctl->private_free = snd_ctl_elem_user_free;
1308
1309 /* Set private data for this userspace control. */
1310 ue = (struct user_element *)kctl->private_data;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001311 ue->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001313 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 ue->elem_data = (char *)ue + sizeof(*ue);
1315 ue->elem_data_size = private_size;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001316 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1317 err = snd_ctl_elem_init_enum_names(ue);
1318 if (err < 0) {
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001319 snd_ctl_free_one(kctl);
Clemens Ladisch8d448162011-10-07 22:38:59 +02001320 return err;
1321 }
1322 }
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001323
1324 /* Set callback functions. */
1325 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1326 kctl->info = snd_ctl_elem_user_enum_info;
1327 else
1328 kctl->info = snd_ctl_elem_user_info;
1329 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1330 kctl->get = snd_ctl_elem_user_get;
1331 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1332 kctl->put = snd_ctl_elem_user_put;
Takashi Sakamotob8e22042017-08-24 10:46:16 +09001333 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
Takashi Sakamoto2225e792015-03-10 22:13:31 +09001334 kctl->tlv.c = snd_ctl_elem_user_tlv;
1335
1336 /* This function manage to free the instance on failure. */
Takashi Iwaie1a7bfe2018-11-22 14:36:17 +01001337 down_write(&card->controls_rwsem);
Takashi Iwai3103c082018-11-22 15:22:40 +01001338 err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
Takashi Iwaie1a7bfe2018-11-22 14:36:17 +01001339 if (err < 0) {
1340 snd_ctl_free_one(kctl);
1341 goto unlock;
1342 }
Takashi Sakamotocab2ed72015-04-11 17:41:04 +09001343 offset = snd_ctl_get_ioff(kctl, &info->id);
1344 snd_ctl_build_ioff(&info->id, kctl, offset);
1345 /*
1346 * Here we cannot fill any field for the number of elements added by
1347 * this operation because there're no specific fields. The usage of
1348 * 'owner' field for this purpose may cause any bugs to userspace
1349 * applications because the field originally means PID of a process
1350 * which locks the element.
1351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 card->user_ctl_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Takashi Iwaie1a7bfe2018-11-22 14:36:17 +01001355 unlock:
1356 up_write(&card->controls_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return 0;
1358}
1359
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001360static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1361 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001363 struct snd_ctl_elem_info info;
Takashi Sakamotocab2ed72015-04-11 17:41:04 +09001364 int err;
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (copy_from_user(&info, _info, sizeof(info)))
1367 return -EFAULT;
Takashi Sakamotocab2ed72015-04-11 17:41:04 +09001368 err = snd_ctl_elem_add(file, &info, replace);
1369 if (err < 0)
1370 return err;
1371 if (copy_to_user(_info, &info, sizeof(info))) {
1372 snd_ctl_remove_user_ctl(file, &info.id);
1373 return -EFAULT;
1374 }
1375
1376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001379static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1380 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001382 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (copy_from_user(&id, _id, sizeof(id)))
1385 return -EFAULT;
Clemens Ladischf217ac52009-08-17 12:27:22 +02001386 return snd_ctl_remove_user_ctl(file, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001389static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 int subscribe;
1392 if (get_user(subscribe, ptr))
1393 return -EFAULT;
1394 if (subscribe < 0) {
1395 subscribe = file->subscribed;
1396 if (put_user(subscribe, ptr))
1397 return -EFAULT;
1398 return 0;
1399 }
1400 if (subscribe) {
1401 file->subscribed = 1;
1402 return 0;
1403 } else if (file->subscribed) {
1404 snd_ctl_empty_read_queue(file);
1405 file->subscribed = 0;
1406 }
1407 return 0;
1408}
1409
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001410static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1411 struct snd_kcontrol *kctl,
1412 struct snd_ctl_elem_id *id,
1413 unsigned int __user *buf, unsigned int size)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001414{
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001415 static const struct {
1416 int op;
1417 int perm;
1418 } pairs[] = {
1419 {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1420 {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1421 {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1422 };
1423 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1424 int i;
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001425
1426 /* Check support of the request for this element. */
1427 for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1428 if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1429 break;
1430 }
1431 if (i == ARRAY_SIZE(pairs))
1432 return -ENXIO;
1433
1434 if (kctl->tlv.c == NULL)
1435 return -ENXIO;
1436
1437 /* When locked, this is unavailable. */
1438 if (vd->owner != NULL && vd->owner != file)
1439 return -EPERM;
1440
Takashi Sakamotofb8027e2017-08-24 10:46:14 +09001441 return kctl->tlv.c(kctl, op_flag, size, buf);
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001442}
1443
1444static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1445 unsigned int __user *buf, unsigned int size)
1446{
1447 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001448 unsigned int len;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001449
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001450 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1451 return -ENXIO;
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001452
1453 if (kctl->tlv.p == NULL)
1454 return -ENXIO;
1455
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001456 len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1457 if (size < len)
1458 return -ENOMEM;
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001459
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001460 if (copy_to_user(buf, kctl->tlv.p, len))
1461 return -EFAULT;
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001462
1463 return 0;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001464}
1465
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001466static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1467 struct snd_ctl_tlv __user *buf,
1468 int op_flag)
1469{
1470 struct snd_ctl_tlv header;
Takashi Iwai1ba78622018-04-23 15:01:44 +02001471 unsigned int __user *container;
Takashi Sakamoto450296f2017-08-03 20:20:43 +09001472 unsigned int container_size;
1473 struct snd_kcontrol *kctl;
1474 struct snd_ctl_elem_id id;
1475 struct snd_kcontrol_volatile *vd;
1476
1477 if (copy_from_user(&header, buf, sizeof(header)))
1478 return -EFAULT;
1479
1480 /* In design of control core, numerical ID starts at 1. */
1481 if (header.numid == 0)
1482 return -EINVAL;
1483
1484 /* At least, container should include type and length fields. */
1485 if (header.length < sizeof(unsigned int) * 2)
1486 return -EINVAL;
1487 container_size = header.length;
1488 container = buf->tlv;
1489
1490 kctl = snd_ctl_find_numid(file->card, header.numid);
1491 if (kctl == NULL)
1492 return -ENOENT;
1493
1494 /* Calculate index of the element in this set. */
1495 id = kctl->id;
1496 snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1497 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1498
1499 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1500 return call_tlv_handler(file, op_flag, kctl, &id, container,
1501 container_size);
1502 } else {
1503 if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1504 return read_tlv_buf(kctl, &id, container,
1505 container_size);
1506 }
1507 }
1508
1509 /* Not supported. */
1510 return -ENXIO;
1511}
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1514{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001515 struct snd_ctl_file *ctl;
1516 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001517 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 void __user *argp = (void __user *)arg;
1519 int __user *ip = argp;
1520 int err;
1521
1522 ctl = file->private_data;
1523 card = ctl->card;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001524 if (snd_BUG_ON(!card))
1525 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 switch (cmd) {
1527 case SNDRV_CTL_IOCTL_PVERSION:
1528 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1529 case SNDRV_CTL_IOCTL_CARD_INFO:
1530 return snd_ctl_card_info(card, ctl, cmd, argp);
1531 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001532 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 case SNDRV_CTL_IOCTL_ELEM_INFO:
1534 return snd_ctl_elem_info_user(ctl, argp);
1535 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001536 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1538 return snd_ctl_elem_write_user(ctl, argp);
1539 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1540 return snd_ctl_elem_lock(ctl, argp);
1541 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1542 return snd_ctl_elem_unlock(ctl, argp);
1543 case SNDRV_CTL_IOCTL_ELEM_ADD:
1544 return snd_ctl_elem_add_user(ctl, argp, 0);
1545 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1546 return snd_ctl_elem_add_user(ctl, argp, 1);
1547 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1548 return snd_ctl_elem_remove(ctl, argp);
1549 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1550 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001551 case SNDRV_CTL_IOCTL_TLV_READ:
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001552 down_read(&ctl->card->controls_rwsem);
1553 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1554 up_read(&ctl->card->controls_rwsem);
1555 return err;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001556 case SNDRV_CTL_IOCTL_TLV_WRITE:
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001557 down_write(&ctl->card->controls_rwsem);
1558 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1559 up_write(&ctl->card->controls_rwsem);
1560 return err;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001561 case SNDRV_CTL_IOCTL_TLV_COMMAND:
Takashi Sakamoto4c8099e2017-08-03 20:20:41 +09001562 down_write(&ctl->card->controls_rwsem);
1563 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1564 up_write(&ctl->card->controls_rwsem);
1565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001567 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 case SNDRV_CTL_IOCTL_POWER_STATE:
1569#ifdef CONFIG_PM
1570 return put_user(card->power_state, ip) ? -EFAULT : 0;
1571#else
1572 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1573#endif
1574 }
1575 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001576 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 err = p->fioctl(card, ctl, cmd, arg);
1578 if (err != -ENOIOCTLCMD) {
1579 up_read(&snd_ioctl_rwsem);
1580 return err;
1581 }
1582 }
1583 up_read(&snd_ioctl_rwsem);
Takashi Iwaibb009452014-02-04 18:18:16 +01001584 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return -ENOTTY;
1586}
1587
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001588static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1589 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001591 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int err = 0;
1593 ssize_t result = 0;
1594
1595 ctl = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001596 if (snd_BUG_ON(!ctl || !ctl->card))
1597 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (!ctl->subscribed)
1599 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001600 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return -EINVAL;
1602 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001603 while (count >= sizeof(struct snd_ctl_event)) {
1604 struct snd_ctl_event ev;
1605 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 while (list_empty(&ctl->events)) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001607 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1609 err = -EAGAIN;
1610 goto __end_lock;
1611 }
1612 init_waitqueue_entry(&wait, current);
1613 add_wait_queue(&ctl->change_sleep, &wait);
1614 set_current_state(TASK_INTERRUPTIBLE);
1615 spin_unlock_irq(&ctl->read_lock);
1616 schedule();
1617 remove_wait_queue(&ctl->change_sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001618 if (ctl->card->shutdown)
1619 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001621 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 spin_lock_irq(&ctl->read_lock);
1623 }
1624 kev = snd_kctl_event(ctl->events.next);
1625 ev.type = SNDRV_CTL_EVENT_ELEM;
1626 ev.data.elem.mask = kev->mask;
1627 ev.data.elem.id = kev->id;
1628 list_del(&kev->list);
1629 spin_unlock_irq(&ctl->read_lock);
1630 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001631 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 err = -EFAULT;
1633 goto __end;
1634 }
1635 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001636 buffer += sizeof(struct snd_ctl_event);
1637 count -= sizeof(struct snd_ctl_event);
1638 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640 __end_lock:
1641 spin_unlock_irq(&ctl->read_lock);
1642 __end:
1643 return result > 0 ? result : err;
1644}
1645
Al Viro680ef722017-07-02 23:27:36 -04001646static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Al Viro680ef722017-07-02 23:27:36 -04001648 __poll_t mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001649 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 ctl = file->private_data;
1652 if (!ctl->subscribed)
1653 return 0;
1654 poll_wait(file, &ctl->change_sleep, wait);
1655
1656 mask = 0;
1657 if (!list_empty(&ctl->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001658 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 return mask;
1661}
1662
1663/*
1664 * register the device-specific control-ioctls.
1665 * called from each device manager like pcm.c, hwdep.c, etc.
1666 */
1667static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1668{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001669 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001671 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (pn == NULL)
1673 return -ENOMEM;
1674 pn->fioctl = fcn;
1675 down_write(&snd_ioctl_rwsem);
1676 list_add_tail(&pn->list, lists);
1677 up_write(&snd_ioctl_rwsem);
1678 return 0;
1679}
1680
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001681/**
1682 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1683 * @fcn: ioctl callback function
1684 *
1685 * called from each device manager like pcm.c, hwdep.c, etc.
1686 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1688{
1689 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1690}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001691EXPORT_SYMBOL(snd_ctl_register_ioctl);
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693#ifdef CONFIG_COMPAT
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001694/**
1695 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1696 * control-ioctls
1697 * @fcn: ioctl callback function
1698 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1700{
1701 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1702}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001703EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704#endif
1705
1706/*
1707 * de-register the device-specific control-ioctls.
1708 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001709static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1710 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001712 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001714 if (snd_BUG_ON(!fcn))
1715 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001717 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (p->fioctl == fcn) {
1719 list_del(&p->list);
1720 up_write(&snd_ioctl_rwsem);
1721 kfree(p);
1722 return 0;
1723 }
1724 }
1725 up_write(&snd_ioctl_rwsem);
1726 snd_BUG();
1727 return -EINVAL;
1728}
1729
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001730/**
1731 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1732 * @fcn: ioctl callback function to unregister
1733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1735{
1736 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1737}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001738EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740#ifdef CONFIG_COMPAT
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001741/**
1742 * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
1743 * control-ioctls
1744 * @fcn: ioctl callback function to unregister
1745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1747{
1748 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1749}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001750EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751#endif
1752
1753static int snd_ctl_fasync(int fd, struct file * file, int on)
1754{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001755 struct snd_ctl_file *ctl;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 ctl = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001758 return fasync_helper(fd, file, on, &ctl->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Takashi Iwai23c18d42014-02-19 14:30:29 +01001761/* return the preferred subdevice number if already assigned;
1762 * otherwise return -1
1763 */
1764int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
1765{
1766 struct snd_ctl_file *kctl;
1767 int subdevice = -1;
1768
1769 read_lock(&card->ctl_files_rwlock);
1770 list_for_each_entry(kctl, &card->ctl_files, list) {
1771 if (kctl->pid == task_pid(current)) {
1772 subdevice = kctl->preferred_subdevice[type];
1773 if (subdevice != -1)
1774 break;
1775 }
1776 }
1777 read_unlock(&card->ctl_files_rwlock);
1778 return subdevice;
1779}
1780EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/*
1783 * ioctl32 compat
1784 */
1785#ifdef CONFIG_COMPAT
1786#include "control_compat.c"
1787#else
1788#define snd_ctl_ioctl_compat NULL
1789#endif
1790
1791/*
1792 * INIT PART
1793 */
1794
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001795static const struct file_operations snd_ctl_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 .owner = THIS_MODULE,
1798 .read = snd_ctl_read,
1799 .open = snd_ctl_open,
1800 .release = snd_ctl_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001801 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 .poll = snd_ctl_poll,
1803 .unlocked_ioctl = snd_ctl_ioctl,
1804 .compat_ioctl = snd_ctl_ioctl_compat,
1805 .fasync = snd_ctl_fasync,
1806};
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/*
1809 * registration of the control device
1810 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001811static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001813 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Takashi Iwai40a4b262015-01-30 08:34:58 +01001815 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1816 &snd_ctl_f_ops, card, &card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
1819/*
1820 * disconnection of the control device
1821 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001822static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001824 struct snd_card *card = device->device_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001825 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Takashi Iwaid8009882008-09-07 12:51:13 +02001827 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001828 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 wake_up(&ctl->change_sleep);
1830 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1831 }
Takashi Iwaid8009882008-09-07 12:51:13 +02001832 read_unlock(&card->ctl_files_rwlock);
Takashi Iwaic4614822006-06-23 14:38:23 +02001833
Takashi Iwai40a4b262015-01-30 08:34:58 +01001834 return snd_unregister_device(&card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837/*
1838 * free all controls
1839 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001840static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001842 struct snd_card *card = device->device_data;
1843 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 down_write(&card->controls_rwsem);
1846 while (!list_empty(&card->controls)) {
1847 control = snd_kcontrol(card->controls.next);
1848 snd_ctl_remove(card, control);
1849 }
1850 up_write(&card->controls_rwsem);
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001851 put_device(&card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 0;
1853}
1854
1855/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 * create control core:
1857 * called from init.c
1858 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001859int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001861 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 .dev_free = snd_ctl_dev_free,
1863 .dev_register = snd_ctl_dev_register,
1864 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 };
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001866 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001868 if (snd_BUG_ON(!card))
1869 return -ENXIO;
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001870 if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
1871 return -ENXIO;
1872
1873 snd_device_initialize(&card->ctl_dev, card);
1874 dev_set_name(&card->ctl_dev, "controlC%d", card->number);
1875
1876 err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1877 if (err < 0)
1878 put_device(&card->ctl_dev);
1879 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001881
1882/*
Clemens Ladisch96007322011-01-10 16:25:44 +01001883 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001884 */
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001885
1886/**
1887 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
1888 * callback with a mono channel
1889 * @kcontrol: the kcontrol instance
1890 * @uinfo: info to store
1891 *
1892 * This is a function that can be used as info callback for a standard
1893 * boolean control with a single mono channel.
1894 */
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001895int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_info *uinfo)
1897{
1898 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1899 uinfo->count = 1;
1900 uinfo->value.integer.min = 0;
1901 uinfo->value.integer.max = 1;
1902 return 0;
1903}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001904EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1905
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001906/**
1907 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
1908 * callback with stereo two channels
1909 * @kcontrol: the kcontrol instance
1910 * @uinfo: info to store
1911 *
1912 * This is a function that can be used as info callback for a standard
1913 * boolean control with stereo two channels.
1914 */
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001915int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1916 struct snd_ctl_elem_info *uinfo)
1917{
1918 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1919 uinfo->count = 2;
1920 uinfo->value.integer.min = 0;
1921 uinfo->value.integer.max = 1;
1922 return 0;
1923}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001924EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
Clemens Ladisch96007322011-01-10 16:25:44 +01001925
1926/**
1927 * snd_ctl_enum_info - fills the info structure for an enumerated control
1928 * @info: the structure to be filled
1929 * @channels: the number of the control's channels; often one
1930 * @items: the number of control values; also the size of @names
1931 * @names: an array containing the names of all control values
1932 *
1933 * Sets all required fields in @info to their appropriate values.
1934 * If the control's accessibility is not the default (readable and writable),
1935 * the caller has to fill @info->access.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001936 *
1937 * Return: Zero.
Clemens Ladisch96007322011-01-10 16:25:44 +01001938 */
1939int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1940 unsigned int items, const char *const names[])
1941{
1942 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1943 info->count = channels;
1944 info->value.enumerated.items = items;
Takashi Iwaia7e6fb92014-10-20 18:08:50 +02001945 if (!items)
1946 return 0;
Clemens Ladisch96007322011-01-10 16:25:44 +01001947 if (info->value.enumerated.item >= items)
1948 info->value.enumerated.item = items - 1;
Takashi Iwaidf803e12014-10-20 18:07:21 +02001949 WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
1950 "ALSA: too long item name '%s'\n",
1951 names[info->value.enumerated.item]);
Clemens Ladisch96007322011-01-10 16:25:44 +01001952 strlcpy(info->value.enumerated.name,
1953 names[info->value.enumerated.item],
1954 sizeof(info->value.enumerated.name));
1955 return 0;
1956}
1957EXPORT_SYMBOL(snd_ctl_enum_info);