ALSA: control: queue TLV event for a set of user-defined element
[linux-block.git] / sound / core / control.c
CommitLineData
1da177e4
LT
1/*
2 * Routines for driver control interface
c1017a4c 3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
1da177e4
LT
22#include <linux/threads.h>
23#include <linux/interrupt.h>
da155d5b 24#include <linux/module.h>
1da177e4
LT
25#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/time.h>
174cd4b1 28#include <linux/sched/signal.h>
1da177e4
LT
29#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
5591bf07 36#define MAX_CONTROL_COUNT 1028
1da177e4 37
82e9bae6 38struct snd_kctl_ioctl {
1da177e4
LT
39 struct list_head list; /* list of all ioctls */
40 snd_kctl_ioctl_func_t fioctl;
82e9bae6 41};
1da177e4
LT
42
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{
1da177e4 51 unsigned long flags;
82e9bae6
TI
52 struct snd_card *card;
53 struct snd_ctl_file *ctl;
23c18d4b 54 int i, err;
1da177e4 55
02f4865f
TI
56 err = nonseekable_open(inode, file);
57 if (err < 0)
58 return err;
59
f87135f5 60 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
1da177e4
LT
61 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 }
ca2c0966 74 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1da177e4
LT
75 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;
23c18d4b
TI
83 for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
84 ctl->preferred_subdevice[i] = -1;
25d27ede 85 ctl->pid = get_pid(task_pid(current));
1da177e4
LT
86 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);
a0830dbd 90 snd_card_unref(card);
1da177e4
LT
91 return 0;
92
93 __error:
94 module_put(card->module);
95 __error2:
96 snd_card_file_remove(card, file);
97 __error1:
a0830dbd
TI
98 if (card)
99 snd_card_unref(card);
1da177e4
LT
100 return err;
101}
102
82e9bae6 103static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
1da177e4 104{
7507e8da 105 unsigned long flags;
82e9bae6 106 struct snd_kctl_event *cread;
1da177e4 107
7507e8da 108 spin_lock_irqsave(&ctl->read_lock, flags);
1da177e4
LT
109 while (!list_empty(&ctl->events)) {
110 cread = snd_kctl_event(ctl->events.next);
111 list_del(&cread->list);
112 kfree(cread);
113 }
7507e8da 114 spin_unlock_irqrestore(&ctl->read_lock, flags);
1da177e4
LT
115}
116
117static int snd_ctl_release(struct inode *inode, struct file *file)
118{
119 unsigned long flags;
82e9bae6
TI
120 struct snd_card *card;
121 struct snd_ctl_file *ctl;
122 struct snd_kcontrol *control;
1da177e4
LT
123 unsigned int idx;
124
125 ctl = file->private_data;
1da177e4
LT
126 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);
9244b2c3 132 list_for_each_entry(control, &card->controls, list)
1da177e4
LT
133 for (idx = 0; idx < control->count; idx++)
134 if (control->vd[idx].owner == ctl)
135 control->vd[idx].owner = NULL;
1da177e4
LT
136 up_write(&card->controls_rwsem);
137 snd_ctl_empty_read_queue(ctl);
25d27ede 138 put_pid(ctl->pid);
1da177e4
LT
139 kfree(ctl);
140 module_put(card->module);
141 snd_card_file_remove(card, file);
142 return 0;
143}
144
12cddbd8
TI
145/**
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 */
82e9bae6
TI
155void snd_ctl_notify(struct snd_card *card, unsigned int mask,
156 struct snd_ctl_elem_id *id)
1da177e4
LT
157{
158 unsigned long flags;
82e9bae6
TI
159 struct snd_ctl_file *ctl;
160 struct snd_kctl_event *ev;
1da177e4 161
7eaa943c
TI
162 if (snd_BUG_ON(!card || !id))
163 return;
f388cdcd
TI
164 if (card->shutdown)
165 return;
1da177e4 166 read_lock(&card->ctl_files_rwlock);
8eeaa2f9 167#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
1da177e4
LT
168 card->mixer_oss_change_count++;
169#endif
9244b2c3 170 list_for_each_entry(ctl, &card->ctl_files, list) {
1da177e4
LT
171 if (!ctl->subscribed)
172 continue;
173 spin_lock_irqsave(&ctl->read_lock, flags);
9244b2c3 174 list_for_each_entry(ev, &ctl->events, list) {
1da177e4
LT
175 if (ev->id.numid == id->numid) {
176 ev->mask |= mask;
177 goto _found;
178 }
179 }
ca2c0966 180 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
1da177e4
LT
181 if (ev) {
182 ev->id = *id;
183 ev->mask = mask;
184 list_add_tail(&ev->list, &ctl->events);
185 } else {
bb009457 186 dev_err(card->dev, "No memory available to allocate event\n");
1da177e4
LT
187 }
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}
c0d3fb39
TI
195EXPORT_SYMBOL(snd_ctl_notify);
196
1da177e4 197/**
2225e79b
TS
198 * 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
1da177e4 203 *
2225e79b
TS
204 * 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.
1da177e4 207 *
2225e79b 208 * Return: 0 on success, error code on failure
1da177e4 209 */
2225e79b
TS
210static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
211 unsigned int access, struct snd_ctl_file *file)
1da177e4 212{
2225e79b 213 unsigned int size;
1da177e4
LT
214 unsigned int idx;
215
2225e79b
TS
216 if (count == 0 || count > MAX_CONTROL_COUNT)
217 return -EINVAL;
5591bf07 218
2225e79b
TS
219 size = sizeof(struct snd_kcontrol);
220 size += sizeof(struct snd_kcontrol_volatile) * count;
5591bf07 221
2225e79b 222 *kctl = kzalloc(size, GFP_KERNEL);
ec0e9937 223 if (!*kctl)
2225e79b 224 return -ENOMEM;
2225e79b
TS
225
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;
1da177e4
LT
233}
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 *
82e9bae6 240 * Allocates a new struct snd_kcontrol instance and initialize from the given
1da177e4
LT
241 * 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 *
eb7c06e8 244 * Return: The pointer of the newly generated instance, or %NULL on failure.
1da177e4 245 */
82e9bae6
TI
246struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
247 void *private_data)
1da177e4 248{
2225e79b
TS
249 struct snd_kcontrol *kctl;
250 unsigned int count;
1da177e4 251 unsigned int access;
2225e79b 252 int err;
1da177e4 253
7eaa943c
TI
254 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
255 return NULL;
2225e79b
TS
256
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;
366840d7 279 if (ncontrol->name) {
2225e79b
TS
280 strlcpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
281 if (strcmp(ncontrol->name, kctl->id.name) != 0)
bb009457 282 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
2225e79b 283 ncontrol->name, kctl->id.name);
366840d7 284 }
2225e79b
TS
285 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;
1da177e4 296}
c0d3fb39
TI
297EXPORT_SYMBOL(snd_ctl_new1);
298
1da177e4
LT
299/**
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 */
82e9bae6 307void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
1da177e4
LT
308{
309 if (kcontrol) {
310 if (kcontrol->private_free)
311 kcontrol->private_free(kcontrol);
312 kfree(kcontrol);
313 }
314}
c0d3fb39
TI
315EXPORT_SYMBOL(snd_ctl_free_one);
316
0e82e5fa
CL
317static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
318 unsigned int count)
1da177e4 319{
82e9bae6 320 struct snd_kcontrol *kctl;
1da177e4 321
ac902c11
LPC
322 /* 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
9244b2c3 326 list_for_each_entry(kctl, &card->controls, list) {
7c733587 327 if (kctl->id.numid < card->last_numid + 1 + count &&
0e82e5fa
CL
328 kctl->id.numid + kctl->count > card->last_numid + 1) {
329 card->last_numid = kctl->id.numid + kctl->count - 1;
330 return true;
331 }
1da177e4 332 }
0e82e5fa 333 return false;
1da177e4
LT
334}
335
82e9bae6 336static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
1da177e4 337{
0e82e5fa 338 unsigned int iter = 100000;
1da177e4 339
0e82e5fa 340 while (snd_ctl_remove_numid_conflict(card, count)) {
1da177e4
LT
341 if (--iter == 0) {
342 /* this situation is very unlikely */
bb009457 343 dev_err(card->dev, "unable to allocate new control numid\n");
1da177e4
LT
344 return -ENOMEM;
345 }
1da177e4
LT
346 }
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 *
1da177e4 359 * It frees automatically the control which cannot be added.
eb7c06e8
YB
360 *
361 * Return: Zero if successful, or a negative error code on failure.
362 *
1da177e4 363 */
82e9bae6 364int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
1da177e4 365{
82e9bae6 366 struct snd_ctl_elem_id id;
1da177e4 367 unsigned int idx;
fd9f26e4 368 unsigned int count;
c6077b30 369 int err = -EINVAL;
1da177e4 370
73e77ba0 371 if (! kcontrol)
c6077b30 372 return err;
7eaa943c
TI
373 if (snd_BUG_ON(!card || !kcontrol->info))
374 goto error;
1da177e4 375 id = kcontrol->id;
883a1d49
LPC
376 if (id.index > UINT_MAX - kcontrol->count)
377 goto error;
378
1da177e4
LT
379 down_write(&card->controls_rwsem);
380 if (snd_ctl_find_id(card, &id)) {
381 up_write(&card->controls_rwsem);
bb009457 382 dev_err(card->dev, "control %i:%i:%i:%s:%i is already present\n",
1da177e4
LT
383 id.iface,
384 id.device,
385 id.subdevice,
386 id.name,
387 id.index);
c6077b30
TI
388 err = -EBUSY;
389 goto error;
1da177e4
LT
390 }
391 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
392 up_write(&card->controls_rwsem);
c6077b30
TI
393 err = -ENOMEM;
394 goto error;
1da177e4
LT
395 }
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;
d34890cf 400 id = kcontrol->id;
fd9f26e4 401 count = kcontrol->count;
1da177e4 402 up_write(&card->controls_rwsem);
fd9f26e4 403 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
1da177e4
LT
404 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
405 return 0;
c6077b30
TI
406
407 error:
408 snd_ctl_free_one(kcontrol);
409 return err;
1da177e4 410}
c0d3fb39
TI
411EXPORT_SYMBOL(snd_ctl_add);
412
66b5b972
DP
413/**
414 * 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 *
66b5b972 423 * It frees automatically the control which cannot be added or replaced.
eb7c06e8
YB
424 *
425 * Return: Zero if successful, or a negative error code on failure.
66b5b972
DP
426 */
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;
fd9f26e4 431 unsigned int count;
66b5b972
DP
432 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;
e6ff3840 467 id = kcontrol->id;
fd9f26e4 468 count = kcontrol->count;
66b5b972 469 up_write(&card->controls_rwsem);
fd9f26e4 470 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
66b5b972
DP
471 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
1da177e4
LT
480/**
481 * 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).
eb7c06e8
YB
488 *
489 * Return: 0 if successful, or a negative error code on failure.
1da177e4 490 */
82e9bae6 491int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
1da177e4 492{
82e9bae6 493 struct snd_ctl_elem_id id;
1da177e4
LT
494 unsigned int idx;
495
7eaa943c
TI
496 if (snd_BUG_ON(!card || !kcontrol))
497 return -EINVAL;
1da177e4
LT
498 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}
c0d3fb39
TI
506EXPORT_SYMBOL(snd_ctl_remove);
507
1da177e4
LT
508/**
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.
eb7c06e8
YB
515 *
516 * Return: 0 if successful, or a negative error code on failure.
1da177e4 517 */
82e9bae6 518int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
1da177e4 519{
82e9bae6 520 struct snd_kcontrol *kctl;
1da177e4
LT
521 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}
c0d3fb39
TI
533EXPORT_SYMBOL(snd_ctl_remove_id);
534
1da177e4 535/**
f217ac59 536 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
1da177e4
LT
537 * @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.
eb7c06e8
YB
542 *
543 * Return: 0 if successful, or a negative error code on failure.
1da177e4 544 */
f217ac59
CL
545static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
546 struct snd_ctl_elem_id *id)
1da177e4 547{
82e9bae6
TI
548 struct snd_card *card = file->card;
549 struct snd_kcontrol *kctl;
1da177e4
LT
550 int idx, ret;
551
552 down_write(&card->controls_rwsem);
553 kctl = snd_ctl_find_id(card, id);
554 if (kctl == NULL) {
317b8081
CL
555 ret = -ENOENT;
556 goto error;
1da177e4 557 }
18dd0aa5
CL
558 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
559 ret = -EINVAL;
560 goto error;
561 }
1da177e4
LT
562 for (idx = 0; idx < kctl->count; idx++)
563 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
317b8081
CL
564 ret = -EBUSY;
565 goto error;
1da177e4
LT
566 }
567 ret = snd_ctl_remove(card, kctl);
f217ac59
CL
568 if (ret < 0)
569 goto error;
570 card->user_ctl_count--;
317b8081 571error:
1da177e4
LT
572 up_write(&card->controls_rwsem);
573 return ret;
574}
575
3cbdd753
TI
576/**
577 * 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.
c78497e0 584 * The given ID data is filled with full information.
3cbdd753 585 *
eb7c06e8 586 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
3cbdd753
TI
587 */
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 }
31584ed1 602 index_offset = snd_ctl_get_ioff(kctl, id);
3cbdd753
TI
603 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 }
c78497e0 614 snd_ctl_build_ioff(id, kctl, index_offset);
3cbdd753
TI
615 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
1da177e4
LT
624/**
625 * 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 *
eb7c06e8 633 * Return: Zero if successful, or a negative error code on failure.
1da177e4 634 */
82e9bae6
TI
635int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
636 struct snd_ctl_elem_id *dst_id)
1da177e4 637{
82e9bae6 638 struct snd_kcontrol *kctl;
1da177e4
LT
639
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}
c0d3fb39
TI
652EXPORT_SYMBOL(snd_ctl_rename_id);
653
1da177e4
LT
654/**
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 *
1da177e4
LT
661 * The caller must down card->controls_rwsem before calling this function
662 * (if the race condition can happen).
eb7c06e8
YB
663 *
664 * Return: The pointer of the instance if found, or %NULL if not.
665 *
1da177e4 666 */
82e9bae6 667struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
1da177e4 668{
82e9bae6 669 struct snd_kcontrol *kctl;
1da177e4 670
7eaa943c
TI
671 if (snd_BUG_ON(!card || !numid))
672 return NULL;
9244b2c3 673 list_for_each_entry(kctl, &card->controls, list) {
1da177e4
LT
674 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
675 return kctl;
676 }
677 return NULL;
678}
c0d3fb39
TI
679EXPORT_SYMBOL(snd_ctl_find_numid);
680
1da177e4
LT
681/**
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 *
1da177e4
LT
688 * The caller must down card->controls_rwsem before calling this function
689 * (if the race condition can happen).
eb7c06e8
YB
690 *
691 * Return: The pointer of the instance if found, or %NULL if not.
692 *
1da177e4 693 */
82e9bae6
TI
694struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
695 struct snd_ctl_elem_id *id)
1da177e4 696{
82e9bae6 697 struct snd_kcontrol *kctl;
1da177e4 698
7eaa943c
TI
699 if (snd_BUG_ON(!card || !id))
700 return NULL;
1da177e4
LT
701 if (id->numid != 0)
702 return snd_ctl_find_numid(card, id->numid);
9244b2c3 703 list_for_each_entry(kctl, &card->controls, list) {
1da177e4
LT
704 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}
c0d3fb39
TI
720EXPORT_SYMBOL(snd_ctl_find_id);
721
82e9bae6 722static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
1da177e4
LT
723 unsigned int cmd, void __user *arg)
724{
82e9bae6 725 struct snd_ctl_card_info *info;
1da177e4 726
ca2c0966 727 info = kzalloc(sizeof(*info), GFP_KERNEL);
1da177e4
LT
728 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);
82e9bae6 739 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
1da177e4
LT
740 kfree(info);
741 return -EFAULT;
742 }
743 kfree(info);
744 return 0;
745}
746
82e9bae6
TI
747static int snd_ctl_elem_list(struct snd_card *card,
748 struct snd_ctl_elem_list __user *_list)
1da177e4 749{
82e9bae6
TI
750 struct snd_ctl_elem_list list;
751 struct snd_kcontrol *kctl;
53e7bf45 752 struct snd_ctl_elem_id id;
78fa2c4d 753 unsigned int offset, space, jidx;
53e7bf45 754 int err = 0;
1da177e4
LT
755
756 if (copy_from_user(&list, _list, sizeof(list)))
757 return -EFAULT;
758 offset = list.offset;
759 space = list.space;
4e361d3c 760
53e7bf45
TI
761 down_read(&card->controls_rwsem);
762 list.count = card->controls_count;
763 list.used = 0;
1da177e4 764 if (space > 0) {
53e7bf45
TI
765 list_for_each_entry(kctl, &card->controls, list) {
766 if (offset >= kctl->count) {
767 offset -= kctl->count;
768 continue;
769 }
770 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 }
1da177e4 777 list.used++;
53e7bf45
TI
778 if (!--space)
779 goto out;
1da177e4 780 }
1da177e4
LT
781 offset = 0;
782 }
1da177e4 783 }
53e7bf45
TI
784 out:
785 up_read(&card->controls_rwsem);
786 if (!err && copy_to_user(_list, &list, sizeof(list)))
787 err = -EFAULT;
788 return err;
1da177e4
LT
789}
790
860c1994
TS
791static 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
82e9bae6
TI
821static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
822 struct snd_ctl_elem_info *info)
1da177e4 823{
82e9bae6
TI
824 struct snd_card *card = ctl->card;
825 struct snd_kcontrol *kctl;
826 struct snd_kcontrol_volatile *vd;
1da177e4
LT
827 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) {
7eaa943c 841 snd_BUG_ON(info->access);
1da177e4
LT
842 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;
25d27ede 850 info->owner = pid_vnr(vd->owner->pid);
1da177e4
LT
851 } else {
852 info->owner = -1;
853 }
854 }
855 up_read(&card->controls_rwsem);
856 return result;
857}
858
82e9bae6
TI
859static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
860 struct snd_ctl_elem_info __user *_info)
1da177e4 861{
82e9bae6 862 struct snd_ctl_elem_info info;
1da177e4
LT
863 int result;
864
865 if (copy_from_user(&info, _info, sizeof(info)))
866 return -EFAULT;
64649400 867 snd_power_lock(ctl->card);
cbac4b0c 868 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
64649400
GP
869 if (result >= 0)
870 result = snd_ctl_elem_info(ctl, &info);
871 snd_power_unlock(ctl->card);
1da177e4
LT
872 if (result >= 0)
873 if (copy_to_user(_info, &info, sizeof(info)))
874 return -EFAULT;
875 return result;
876}
877
d3bd67cd
TI
878static int snd_ctl_elem_read(struct snd_card *card,
879 struct snd_ctl_elem_value *control)
1da177e4 880{
82e9bae6
TI
881 struct snd_kcontrol *kctl;
882 struct snd_kcontrol_volatile *vd;
1da177e4 883 unsigned int index_offset;
1da177e4 884
1da177e4 885 kctl = snd_ctl_find_id(card, &control->id);
becf9e5d
TS
886 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);
1da177e4
LT
896}
897
82e9bae6
TI
898static int snd_ctl_elem_read_user(struct snd_card *card,
899 struct snd_ctl_elem_value __user *_control)
1da177e4 900{
82e9bae6 901 struct snd_ctl_elem_value *control;
1da177e4 902 int result;
ef44a1ec
LZ
903
904 control = memdup_user(_control, sizeof(*control));
905 if (IS_ERR(control))
906 return PTR_ERR(control);
907
64649400 908 snd_power_lock(card);
cbac4b0c 909 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
becf9e5d
TS
910 if (result >= 0) {
911 down_read(&card->controls_rwsem);
64649400 912 result = snd_ctl_elem_read(card, control);
becf9e5d
TS
913 up_read(&card->controls_rwsem);
914 }
64649400 915 snd_power_unlock(card);
1da177e4
LT
916 if (result >= 0)
917 if (copy_to_user(_control, control, sizeof(*control)))
918 result = -EFAULT;
919 kfree(control);
920 return result;
921}
922
d3bd67cd
TI
923static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
924 struct snd_ctl_elem_value *control)
1da177e4 925{
82e9bae6
TI
926 struct snd_kcontrol *kctl;
927 struct snd_kcontrol_volatile *vd;
1da177e4 928 unsigned int index_offset;
8ace4f3c 929 int result;
1da177e4 930
1da177e4 931 kctl = snd_ctl_find_id(card, &control->id);
becf9e5d
TS
932 if (kctl == NULL)
933 return -ENOENT;
934
935 index_offset = snd_ctl_get_ioff(kctl, &control->id);
936 vd = &kctl->vd[index_offset];
937 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
938 (file && vd->owner && vd->owner != file)) {
939 return -EPERM;
1da177e4 940 }
becf9e5d
TS
941
942 snd_ctl_build_ioff(&control->id, kctl, index_offset);
943 result = kctl->put(kctl, control);
944 if (result < 0)
945 return result;
946
947 if (result > 0) {
948 struct snd_ctl_elem_id id = control->id;
949 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
950 }
951
952 return 0;
1da177e4
LT
953}
954
82e9bae6
TI
955static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
956 struct snd_ctl_elem_value __user *_control)
1da177e4 957{
82e9bae6 958 struct snd_ctl_elem_value *control;
64649400 959 struct snd_card *card;
1da177e4
LT
960 int result;
961
ef44a1ec
LZ
962 control = memdup_user(_control, sizeof(*control));
963 if (IS_ERR(control))
964 return PTR_ERR(control);
965
64649400
GP
966 card = file->card;
967 snd_power_lock(card);
cbac4b0c 968 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
becf9e5d 969 if (result >= 0) {
5bbb1ab5 970 down_write(&card->controls_rwsem);
64649400 971 result = snd_ctl_elem_write(card, file, control);
5bbb1ab5 972 up_write(&card->controls_rwsem);
becf9e5d 973 }
64649400 974 snd_power_unlock(card);
1da177e4
LT
975 if (result >= 0)
976 if (copy_to_user(_control, control, sizeof(*control)))
977 result = -EFAULT;
978 kfree(control);
979 return result;
980}
981
82e9bae6
TI
982static int snd_ctl_elem_lock(struct snd_ctl_file *file,
983 struct snd_ctl_elem_id __user *_id)
1da177e4 984{
82e9bae6
TI
985 struct snd_card *card = file->card;
986 struct snd_ctl_elem_id id;
987 struct snd_kcontrol *kctl;
988 struct snd_kcontrol_volatile *vd;
1da177e4
LT
989 int result;
990
991 if (copy_from_user(&id, _id, sizeof(id)))
992 return -EFAULT;
993 down_write(&card->controls_rwsem);
994 kctl = snd_ctl_find_id(card, &id);
995 if (kctl == NULL) {
996 result = -ENOENT;
997 } else {
998 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
999 if (vd->owner != NULL)
1000 result = -EBUSY;
1001 else {
1002 vd->owner = file;
1da177e4
LT
1003 result = 0;
1004 }
1005 }
1006 up_write(&card->controls_rwsem);
1007 return result;
1008}
1009
82e9bae6
TI
1010static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
1011 struct snd_ctl_elem_id __user *_id)
1da177e4 1012{
82e9bae6
TI
1013 struct snd_card *card = file->card;
1014 struct snd_ctl_elem_id id;
1015 struct snd_kcontrol *kctl;
1016 struct snd_kcontrol_volatile *vd;
1da177e4
LT
1017 int result;
1018
1019 if (copy_from_user(&id, _id, sizeof(id)))
1020 return -EFAULT;
1021 down_write(&card->controls_rwsem);
1022 kctl = snd_ctl_find_id(card, &id);
1023 if (kctl == NULL) {
1024 result = -ENOENT;
1025 } else {
1026 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1027 if (vd->owner == NULL)
1028 result = -EINVAL;
1029 else if (vd->owner != file)
1030 result = -EPERM;
1031 else {
1032 vd->owner = NULL;
1da177e4
LT
1033 result = 0;
1034 }
1035 }
1036 up_write(&card->controls_rwsem);
1037 return result;
1038}
1039
1040struct user_element {
82e9bae6 1041 struct snd_ctl_elem_info info;
07f4d9d7 1042 struct snd_card *card;
e1c78df1 1043 char *elem_data; /* element data */
1da177e4 1044 unsigned long elem_data_size; /* size of element data in bytes */
8aa9b586
JK
1045 void *tlv_data; /* TLV data */
1046 unsigned long tlv_data_size; /* TLV data size */
1da177e4 1047 void *priv_data; /* private data (like strings for enumerated type) */
1da177e4
LT
1048};
1049
82e9bae6
TI
1050static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1051 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1052{
1053 struct user_element *ue = kcontrol->private_data;
c378c3b0 1054 unsigned int offset;
1da177e4 1055
c378c3b0 1056 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1da177e4 1057 *uinfo = ue->info;
c378c3b0
TS
1058 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1059
1da177e4
LT
1060 return 0;
1061}
1062
8d448162
CL
1063static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1064 struct snd_ctl_elem_info *uinfo)
1065{
1066 struct user_element *ue = kcontrol->private_data;
1067 const char *names;
1068 unsigned int item;
c378c3b0 1069 unsigned int offset;
8d448162
CL
1070
1071 item = uinfo->value.enumerated.item;
1072
c378c3b0 1073 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
8d448162 1074 *uinfo = ue->info;
c378c3b0 1075 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
8d448162
CL
1076
1077 item = min(item, uinfo->value.enumerated.items - 1);
1078 uinfo->value.enumerated.item = item;
1079
1080 names = ue->priv_data;
1081 for (; item > 0; --item)
1082 names += strlen(names) + 1;
1083 strcpy(uinfo->value.enumerated.name, names);
1084
1085 return 0;
1086}
1087
82e9bae6
TI
1088static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1089 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1090{
1091 struct user_element *ue = kcontrol->private_data;
e1c78df1
TS
1092 unsigned int size = ue->elem_data_size;
1093 char *src = ue->elem_data +
1094 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1da177e4 1095
e1c78df1 1096 memcpy(&ucontrol->value, src, size);
1da177e4
LT
1097 return 0;
1098}
1099
82e9bae6
TI
1100static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1101 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1102{
1103 int change;
1104 struct user_element *ue = kcontrol->private_data;
e1c78df1
TS
1105 unsigned int size = ue->elem_data_size;
1106 char *dst = ue->elem_data +
1107 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
07f4d9d7 1108
e1c78df1 1109 change = memcmp(&ucontrol->value, dst, size) != 0;
1da177e4 1110 if (change)
e1c78df1 1111 memcpy(dst, &ucontrol->value, size);
1da177e4
LT
1112 return change;
1113}
1114
6d4d41f0
TS
1115static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1116 unsigned int size)
8aa9b586 1117{
6d4d41f0
TS
1118 struct user_element *ue = kctl->private_data;
1119 unsigned int *container;
da428828
TS
1120 struct snd_ctl_elem_id id;
1121 int i;
6d4d41f0 1122 int change;
8aa9b586 1123
6d4d41f0
TS
1124 if (size > 1024 * 128) /* sane value */
1125 return -EINVAL;
30d8340b 1126
6d4d41f0
TS
1127 container = memdup_user(buf, size);
1128 if (IS_ERR(container))
1129 return PTR_ERR(container);
ef44a1ec 1130
6d4d41f0
TS
1131 change = ue->tlv_data_size != size;
1132 if (!change)
241bc82e 1133 change = memcmp(ue->tlv_data, container, size) != 0;
6d4d41f0
TS
1134 if (!change) {
1135 kfree(container);
1136 return 0;
1137 }
30d8340b 1138
6d4d41f0
TS
1139 kfree(ue->tlv_data);
1140 ue->tlv_data = container;
1141 ue->tlv_data_size = size;
07f4d9d7 1142
da428828
TS
1143 for (i = 0; i < kctl->count; ++i) {
1144 snd_ctl_build_ioff(&id, kctl, i);
1145 snd_ctl_notify(ue->card, SNDRV_CTL_EVENT_MASK_TLV, &id);
1146 }
fb8027eb 1147
6d4d41f0
TS
1148 return change;
1149}
30d8340b 1150
6d4d41f0
TS
1151static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1152 unsigned int size)
1153{
1154 struct user_element *ue = kctl->private_data;
1155
1156 if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1157 return -ENXIO;
1158
1159 if (size < ue->tlv_data_size)
1160 return -ENOSPC;
1161
1162 if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1163 return -EFAULT;
1164
1165 return 0;
1166}
1167
1168static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1169 unsigned int size, unsigned int __user *buf)
1170{
1171 if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1172 return replace_user_tlv(kctl, buf, size);
1173 else
1174 return read_user_tlv(kctl, buf, size);
8aa9b586
JK
1175}
1176
8d448162
CL
1177static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1178{
1179 char *names, *p;
1180 size_t buf_len, name_len;
1181 unsigned int i;
447c6f93 1182 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
8d448162
CL
1183
1184 if (ue->info.value.enumerated.names_length > 64 * 1024)
1185 return -EINVAL;
1186
447c6f93 1187 names = memdup_user((const void __user *)user_ptrval,
8d448162
CL
1188 ue->info.value.enumerated.names_length);
1189 if (IS_ERR(names))
1190 return PTR_ERR(names);
1191
1192 /* check that there are enough valid names */
1193 buf_len = ue->info.value.enumerated.names_length;
1194 p = names;
1195 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1196 name_len = strnlen(p, buf_len);
1197 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1198 kfree(names);
1199 return -EINVAL;
1200 }
1201 p += name_len + 1;
1202 buf_len -= name_len + 1;
1203 }
1204
1205 ue->priv_data = names;
1206 ue->info.value.enumerated.names_ptr = 0;
1207
1208 return 0;
1209}
1210
82e9bae6 1211static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1da177e4 1212{
8aa9b586 1213 struct user_element *ue = kcontrol->private_data;
8d448162
CL
1214
1215 kfree(ue->tlv_data);
1216 kfree(ue->priv_data);
8aa9b586 1217 kfree(ue);
1da177e4
LT
1218}
1219
82e9bae6
TI
1220static int snd_ctl_elem_add(struct snd_ctl_file *file,
1221 struct snd_ctl_elem_info *info, int replace)
1da177e4 1222{
4ed56666
TS
1223 /* The capacity of struct snd_ctl_elem_value.value.*/
1224 static const unsigned int value_sizes[] = {
1225 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
1226 [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
1227 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
1228 [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
1229 [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
1230 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
1231 };
1232 static const unsigned int max_value_counts[] = {
1233 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
1234 [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
1235 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
1236 [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
1237 [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
1238 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
1239 };
82e9bae6 1240 struct snd_card *card = file->card;
2225e79b
TS
1241 struct snd_kcontrol *kctl;
1242 unsigned int count;
1da177e4
LT
1243 unsigned int access;
1244 long private_size;
1245 struct user_element *ue;
cab2ed74 1246 unsigned int offset;
2225e79b 1247 int err;
82262a46 1248
be3bb823
TI
1249 if (!*info->id.name)
1250 return -EINVAL;
1251 if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1252 return -EINVAL;
82262a46 1253
2225e79b 1254 /* Delete a control to replace them if needed. */
82262a46 1255 if (replace) {
2225e79b 1256 info->id.numid = 0;
82262a46
LPC
1257 err = snd_ctl_remove_user_ctl(file, &info->id);
1258 if (err)
1259 return err;
1da177e4 1260 }
82262a46 1261
2225e79b
TS
1262 /*
1263 * The number of userspace controls are counted control by control,
1264 * not element by element.
1265 */
1266 if (card->user_ctl_count + 1 > MAX_USER_CONTROLS)
82262a46
LPC
1267 return -ENOMEM;
1268
2225e79b
TS
1269 /* Check the number of elements for this userspace control. */
1270 count = info->owner;
1271 if (count == 0)
1272 count = 1;
1273
1274 /* Arrange access permissions if needed. */
1275 access = info->access;
1276 if (access == 0)
1277 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1278 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1279 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1280 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
1281 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)
8aa9b586 1282 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
2225e79b 1283 access |= SNDRV_CTL_ELEM_ACCESS_USER;
4ed56666 1284
2225e79b
TS
1285 /*
1286 * Check information and calculate the size of data specific to
1287 * this userspace control.
1288 */
4ed56666
TS
1289 if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
1290 info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64)
1da177e4 1291 return -EINVAL;
4ed56666
TS
1292 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
1293 info->value.enumerated.items == 0)
1294 return -EINVAL;
1295 if (info->count < 1 ||
1296 info->count > max_value_counts[info->type])
1297 return -EINVAL;
860c1994
TS
1298 if (!validate_element_member_dimension(info))
1299 return -EINVAL;
4ed56666 1300 private_size = value_sizes[info->type] * info->count;
2225e79b
TS
1301
1302 /*
1303 * Keep memory object for this userspace control. After passing this
1304 * code block, the instance should be freed by snd_ctl_free_one().
1305 *
1306 * Note that these elements in this control are locked.
1307 */
1308 err = snd_ctl_new(&kctl, count, access, file);
1309 if (err < 0)
1310 return err;
e79d74ab 1311 memcpy(&kctl->id, &info->id, sizeof(kctl->id));
e1c78df1 1312 kctl->private_data = kzalloc(sizeof(struct user_element) + private_size * count,
2225e79b
TS
1313 GFP_KERNEL);
1314 if (kctl->private_data == NULL) {
1315 kfree(kctl);
1da177e4 1316 return -ENOMEM;
2225e79b
TS
1317 }
1318 kctl->private_free = snd_ctl_elem_user_free;
1319
1320 /* Set private data for this userspace control. */
1321 ue = (struct user_element *)kctl->private_data;
07f4d9d7 1322 ue->card = card;
1da177e4 1323 ue->info = *info;
86148e84 1324 ue->info.access = 0;
1da177e4
LT
1325 ue->elem_data = (char *)ue + sizeof(*ue);
1326 ue->elem_data_size = private_size;
8d448162
CL
1327 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1328 err = snd_ctl_elem_init_enum_names(ue);
1329 if (err < 0) {
2225e79b 1330 snd_ctl_free_one(kctl);
8d448162
CL
1331 return err;
1332 }
1333 }
2225e79b
TS
1334
1335 /* Set callback functions. */
1336 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1337 kctl->info = snd_ctl_elem_user_enum_info;
1338 else
1339 kctl->info = snd_ctl_elem_user_info;
1340 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1341 kctl->get = snd_ctl_elem_user_get;
1342 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1343 kctl->put = snd_ctl_elem_user_put;
1344 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)
1345 kctl->tlv.c = snd_ctl_elem_user_tlv;
1346
1347 /* This function manage to free the instance on failure. */
1348 err = snd_ctl_add(card, kctl);
2fbf182e 1349 if (err < 0)
1da177e4 1350 return err;
cab2ed74
TS
1351 offset = snd_ctl_get_ioff(kctl, &info->id);
1352 snd_ctl_build_ioff(&info->id, kctl, offset);
1353 /*
1354 * Here we cannot fill any field for the number of elements added by
1355 * this operation because there're no specific fields. The usage of
1356 * 'owner' field for this purpose may cause any bugs to userspace
1357 * applications because the field originally means PID of a process
1358 * which locks the element.
1359 */
1da177e4
LT
1360
1361 down_write(&card->controls_rwsem);
1362 card->user_ctl_count++;
1363 up_write(&card->controls_rwsem);
1364
1365 return 0;
1366}
1367
82e9bae6
TI
1368static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1369 struct snd_ctl_elem_info __user *_info, int replace)
1da177e4 1370{
82e9bae6 1371 struct snd_ctl_elem_info info;
cab2ed74
TS
1372 int err;
1373
1da177e4
LT
1374 if (copy_from_user(&info, _info, sizeof(info)))
1375 return -EFAULT;
cab2ed74
TS
1376 err = snd_ctl_elem_add(file, &info, replace);
1377 if (err < 0)
1378 return err;
1379 if (copy_to_user(_info, &info, sizeof(info))) {
1380 snd_ctl_remove_user_ctl(file, &info.id);
1381 return -EFAULT;
1382 }
1383
1384 return 0;
1da177e4
LT
1385}
1386
82e9bae6
TI
1387static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1388 struct snd_ctl_elem_id __user *_id)
1da177e4 1389{
82e9bae6 1390 struct snd_ctl_elem_id id;
1da177e4
LT
1391
1392 if (copy_from_user(&id, _id, sizeof(id)))
1393 return -EFAULT;
f217ac59 1394 return snd_ctl_remove_user_ctl(file, &id);
1da177e4
LT
1395}
1396
82e9bae6 1397static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1da177e4
LT
1398{
1399 int subscribe;
1400 if (get_user(subscribe, ptr))
1401 return -EFAULT;
1402 if (subscribe < 0) {
1403 subscribe = file->subscribed;
1404 if (put_user(subscribe, ptr))
1405 return -EFAULT;
1406 return 0;
1407 }
1408 if (subscribe) {
1409 file->subscribed = 1;
1410 return 0;
1411 } else if (file->subscribed) {
1412 snd_ctl_empty_read_queue(file);
1413 file->subscribed = 0;
1414 }
1415 return 0;
1416}
1417
450296f3
TS
1418static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1419 struct snd_kcontrol *kctl,
1420 struct snd_ctl_elem_id *id,
1421 unsigned int __user *buf, unsigned int size)
1422{
1423 static const struct {
1424 int op;
1425 int perm;
1426 } pairs[] = {
1427 {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1428 {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1429 {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1430 };
1431 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1432 int i;
450296f3
TS
1433
1434 /* Check support of the request for this element. */
1435 for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1436 if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1437 break;
1438 }
1439 if (i == ARRAY_SIZE(pairs))
1440 return -ENXIO;
1441
1442 if (kctl->tlv.c == NULL)
1443 return -ENXIO;
1444
1445 /* When locked, this is unavailable. */
1446 if (vd->owner != NULL && vd->owner != file)
1447 return -EPERM;
1448
fb8027eb 1449 return kctl->tlv.c(kctl, op_flag, size, buf);
450296f3
TS
1450}
1451
1452static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1453 unsigned int __user *buf, unsigned int size)
1454{
1455 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1456 unsigned int len;
1457
1458 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1459 return -ENXIO;
1460
1461 if (kctl->tlv.p == NULL)
1462 return -ENXIO;
1463
1464 len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1465 if (size < len)
1466 return -ENOMEM;
1467
1468 if (copy_to_user(buf, kctl->tlv.p, len))
1469 return -EFAULT;
1470
1471 return 0;
1472}
1473
8aa9b586 1474static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
450296f3 1475 struct snd_ctl_tlv __user *buf,
8aa9b586 1476 int op_flag)
42750b04 1477{
450296f3
TS
1478 struct snd_ctl_tlv header;
1479 unsigned int *container;
1480 unsigned int container_size;
42750b04 1481 struct snd_kcontrol *kctl;
450296f3 1482 struct snd_ctl_elem_id id;
8aa9b586 1483 struct snd_kcontrol_volatile *vd;
42750b04 1484
450296f3 1485 if (copy_from_user(&header, buf, sizeof(header)))
42750b04 1486 return -EFAULT;
450296f3
TS
1487
1488 /* In design of control core, numerical ID starts at 1. */
1489 if (header.numid == 0)
8aa9b586 1490 return -EINVAL;
450296f3
TS
1491
1492 /* At least, container should include type and length fields. */
1493 if (header.length < sizeof(unsigned int) * 2)
c0bcdbdf 1494 return -EINVAL;
450296f3
TS
1495 container_size = header.length;
1496 container = buf->tlv;
4c8099e9 1497
450296f3 1498 kctl = snd_ctl_find_numid(file->card, header.numid);
4c8099e9
TS
1499 if (kctl == NULL)
1500 return -ENOENT;
1501
450296f3
TS
1502 /* Calculate index of the element in this set. */
1503 id = kctl->id;
1504 snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1505 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
4c8099e9 1506
8aa9b586 1507 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
450296f3
TS
1508 return call_tlv_handler(file, op_flag, kctl, &id, container,
1509 container_size);
8aa9b586 1510 } else {
450296f3
TS
1511 if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1512 return read_tlv_buf(kctl, &id, container,
1513 container_size);
1514 }
8aa9b586 1515 }
4c8099e9 1516
450296f3
TS
1517 /* Not supported. */
1518 return -ENXIO;
42750b04
JK
1519}
1520
1da177e4
LT
1521static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1522{
82e9bae6
TI
1523 struct snd_ctl_file *ctl;
1524 struct snd_card *card;
82e9bae6 1525 struct snd_kctl_ioctl *p;
1da177e4
LT
1526 void __user *argp = (void __user *)arg;
1527 int __user *ip = argp;
1528 int err;
1529
1530 ctl = file->private_data;
1531 card = ctl->card;
7eaa943c
TI
1532 if (snd_BUG_ON(!card))
1533 return -ENXIO;
1da177e4
LT
1534 switch (cmd) {
1535 case SNDRV_CTL_IOCTL_PVERSION:
1536 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1537 case SNDRV_CTL_IOCTL_CARD_INFO:
1538 return snd_ctl_card_info(card, ctl, cmd, argp);
1539 case SNDRV_CTL_IOCTL_ELEM_LIST:
42750b04 1540 return snd_ctl_elem_list(card, argp);
1da177e4
LT
1541 case SNDRV_CTL_IOCTL_ELEM_INFO:
1542 return snd_ctl_elem_info_user(ctl, argp);
1543 case SNDRV_CTL_IOCTL_ELEM_READ:
42750b04 1544 return snd_ctl_elem_read_user(card, argp);
1da177e4
LT
1545 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1546 return snd_ctl_elem_write_user(ctl, argp);
1547 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1548 return snd_ctl_elem_lock(ctl, argp);
1549 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1550 return snd_ctl_elem_unlock(ctl, argp);
1551 case SNDRV_CTL_IOCTL_ELEM_ADD:
1552 return snd_ctl_elem_add_user(ctl, argp, 0);
1553 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1554 return snd_ctl_elem_add_user(ctl, argp, 1);
1555 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1556 return snd_ctl_elem_remove(ctl, argp);
1557 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1558 return snd_ctl_subscribe_events(ctl, ip);
8aa9b586 1559 case SNDRV_CTL_IOCTL_TLV_READ:
4c8099e9
TS
1560 down_read(&ctl->card->controls_rwsem);
1561 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1562 up_read(&ctl->card->controls_rwsem);
1563 return err;
8aa9b586 1564 case SNDRV_CTL_IOCTL_TLV_WRITE:
4c8099e9
TS
1565 down_write(&ctl->card->controls_rwsem);
1566 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1567 up_write(&ctl->card->controls_rwsem);
1568 return err;
8aa9b586 1569 case SNDRV_CTL_IOCTL_TLV_COMMAND:
4c8099e9
TS
1570 down_write(&ctl->card->controls_rwsem);
1571 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1572 up_write(&ctl->card->controls_rwsem);
1573 return err;
1da177e4 1574 case SNDRV_CTL_IOCTL_POWER:
a381a7a6 1575 return -ENOPROTOOPT;
1da177e4
LT
1576 case SNDRV_CTL_IOCTL_POWER_STATE:
1577#ifdef CONFIG_PM
1578 return put_user(card->power_state, ip) ? -EFAULT : 0;
1579#else
1580 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1581#endif
1582 }
1583 down_read(&snd_ioctl_rwsem);
9244b2c3 1584 list_for_each_entry(p, &snd_control_ioctls, list) {
1da177e4
LT
1585 err = p->fioctl(card, ctl, cmd, arg);
1586 if (err != -ENOIOCTLCMD) {
1587 up_read(&snd_ioctl_rwsem);
1588 return err;
1589 }
1590 }
1591 up_read(&snd_ioctl_rwsem);
bb009457 1592 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
1da177e4
LT
1593 return -ENOTTY;
1594}
1595
82e9bae6
TI
1596static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1597 size_t count, loff_t * offset)
1da177e4 1598{
82e9bae6 1599 struct snd_ctl_file *ctl;
1da177e4
LT
1600 int err = 0;
1601 ssize_t result = 0;
1602
1603 ctl = file->private_data;
7eaa943c
TI
1604 if (snd_BUG_ON(!ctl || !ctl->card))
1605 return -ENXIO;
1da177e4
LT
1606 if (!ctl->subscribed)
1607 return -EBADFD;
82e9bae6 1608 if (count < sizeof(struct snd_ctl_event))
1da177e4
LT
1609 return -EINVAL;
1610 spin_lock_irq(&ctl->read_lock);
82e9bae6
TI
1611 while (count >= sizeof(struct snd_ctl_event)) {
1612 struct snd_ctl_event ev;
1613 struct snd_kctl_event *kev;
1da177e4 1614 while (list_empty(&ctl->events)) {
ac6424b9 1615 wait_queue_entry_t wait;
1da177e4
LT
1616 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1617 err = -EAGAIN;
1618 goto __end_lock;
1619 }
1620 init_waitqueue_entry(&wait, current);
1621 add_wait_queue(&ctl->change_sleep, &wait);
1622 set_current_state(TASK_INTERRUPTIBLE);
1623 spin_unlock_irq(&ctl->read_lock);
1624 schedule();
1625 remove_wait_queue(&ctl->change_sleep, &wait);
0914f796
TI
1626 if (ctl->card->shutdown)
1627 return -ENODEV;
1da177e4 1628 if (signal_pending(current))
0e5d720c 1629 return -ERESTARTSYS;
1da177e4
LT
1630 spin_lock_irq(&ctl->read_lock);
1631 }
1632 kev = snd_kctl_event(ctl->events.next);
1633 ev.type = SNDRV_CTL_EVENT_ELEM;
1634 ev.data.elem.mask = kev->mask;
1635 ev.data.elem.id = kev->id;
1636 list_del(&kev->list);
1637 spin_unlock_irq(&ctl->read_lock);
1638 kfree(kev);
82e9bae6 1639 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1da177e4
LT
1640 err = -EFAULT;
1641 goto __end;
1642 }
1643 spin_lock_irq(&ctl->read_lock);
82e9bae6
TI
1644 buffer += sizeof(struct snd_ctl_event);
1645 count -= sizeof(struct snd_ctl_event);
1646 result += sizeof(struct snd_ctl_event);
1da177e4
LT
1647 }
1648 __end_lock:
1649 spin_unlock_irq(&ctl->read_lock);
1650 __end:
1651 return result > 0 ? result : err;
1652}
1653
1654static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1655{
1656 unsigned int mask;
82e9bae6 1657 struct snd_ctl_file *ctl;
1da177e4
LT
1658
1659 ctl = file->private_data;
1660 if (!ctl->subscribed)
1661 return 0;
1662 poll_wait(file, &ctl->change_sleep, wait);
1663
1664 mask = 0;
1665 if (!list_empty(&ctl->events))
1666 mask |= POLLIN | POLLRDNORM;
1667
1668 return mask;
1669}
1670
1671/*
1672 * register the device-specific control-ioctls.
1673 * called from each device manager like pcm.c, hwdep.c, etc.
1674 */
1675static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1676{
82e9bae6 1677 struct snd_kctl_ioctl *pn;
1da177e4 1678
82e9bae6 1679 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1da177e4
LT
1680 if (pn == NULL)
1681 return -ENOMEM;
1682 pn->fioctl = fcn;
1683 down_write(&snd_ioctl_rwsem);
1684 list_add_tail(&pn->list, lists);
1685 up_write(&snd_ioctl_rwsem);
1686 return 0;
1687}
1688
12cddbd8
TI
1689/**
1690 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1691 * @fcn: ioctl callback function
1692 *
1693 * called from each device manager like pcm.c, hwdep.c, etc.
1694 */
1da177e4
LT
1695int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1696{
1697 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1698}
c0d3fb39
TI
1699EXPORT_SYMBOL(snd_ctl_register_ioctl);
1700
1da177e4 1701#ifdef CONFIG_COMPAT
12cddbd8
TI
1702/**
1703 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1704 * control-ioctls
1705 * @fcn: ioctl callback function
1706 */
1da177e4
LT
1707int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1708{
1709 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1710}
c0d3fb39 1711EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1da177e4
LT
1712#endif
1713
1714/*
1715 * de-register the device-specific control-ioctls.
1716 */
82e9bae6
TI
1717static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1718 struct list_head *lists)
1da177e4 1719{
82e9bae6 1720 struct snd_kctl_ioctl *p;
1da177e4 1721
7eaa943c
TI
1722 if (snd_BUG_ON(!fcn))
1723 return -EINVAL;
1da177e4 1724 down_write(&snd_ioctl_rwsem);
9244b2c3 1725 list_for_each_entry(p, lists, list) {
1da177e4
LT
1726 if (p->fioctl == fcn) {
1727 list_del(&p->list);
1728 up_write(&snd_ioctl_rwsem);
1729 kfree(p);
1730 return 0;
1731 }
1732 }
1733 up_write(&snd_ioctl_rwsem);
1734 snd_BUG();
1735 return -EINVAL;
1736}
1737
12cddbd8
TI
1738/**
1739 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1740 * @fcn: ioctl callback function to unregister
1741 */
1da177e4
LT
1742int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1743{
1744 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1745}
c0d3fb39
TI
1746EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1747
1da177e4 1748#ifdef CONFIG_COMPAT
12cddbd8
TI
1749/**
1750 * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
1751 * control-ioctls
1752 * @fcn: ioctl callback function to unregister
1753 */
1da177e4
LT
1754int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1755{
1756 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1757}
c0d3fb39 1758EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1da177e4
LT
1759#endif
1760
1761static int snd_ctl_fasync(int fd, struct file * file, int on)
1762{
82e9bae6 1763 struct snd_ctl_file *ctl;
60aa4924 1764
1da177e4 1765 ctl = file->private_data;
60aa4924 1766 return fasync_helper(fd, file, on, &ctl->fasync);
1da177e4
LT
1767}
1768
23c18d4b
TI
1769/* return the preferred subdevice number if already assigned;
1770 * otherwise return -1
1771 */
1772int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
1773{
1774 struct snd_ctl_file *kctl;
1775 int subdevice = -1;
1776
1777 read_lock(&card->ctl_files_rwlock);
1778 list_for_each_entry(kctl, &card->ctl_files, list) {
1779 if (kctl->pid == task_pid(current)) {
1780 subdevice = kctl->preferred_subdevice[type];
1781 if (subdevice != -1)
1782 break;
1783 }
1784 }
1785 read_unlock(&card->ctl_files_rwlock);
1786 return subdevice;
1787}
1788EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
1789
1da177e4
LT
1790/*
1791 * ioctl32 compat
1792 */
1793#ifdef CONFIG_COMPAT
1794#include "control_compat.c"
1795#else
1796#define snd_ctl_ioctl_compat NULL
1797#endif
1798
1799/*
1800 * INIT PART
1801 */
1802
9c2e08c5 1803static const struct file_operations snd_ctl_f_ops =
1da177e4
LT
1804{
1805 .owner = THIS_MODULE,
1806 .read = snd_ctl_read,
1807 .open = snd_ctl_open,
1808 .release = snd_ctl_release,
02f4865f 1809 .llseek = no_llseek,
1da177e4
LT
1810 .poll = snd_ctl_poll,
1811 .unlocked_ioctl = snd_ctl_ioctl,
1812 .compat_ioctl = snd_ctl_ioctl_compat,
1813 .fasync = snd_ctl_fasync,
1814};
1815
1da177e4
LT
1816/*
1817 * registration of the control device
1818 */
82e9bae6 1819static int snd_ctl_dev_register(struct snd_device *device)
1da177e4 1820{
82e9bae6 1821 struct snd_card *card = device->device_data;
1da177e4 1822
40a4b263
TI
1823 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1824 &snd_ctl_f_ops, card, &card->ctl_dev);
1da177e4
LT
1825}
1826
1827/*
1828 * disconnection of the control device
1829 */
82e9bae6 1830static int snd_ctl_dev_disconnect(struct snd_device *device)
1da177e4 1831{
82e9bae6 1832 struct snd_card *card = device->device_data;
82e9bae6 1833 struct snd_ctl_file *ctl;
1da177e4 1834
d8009882 1835 read_lock(&card->ctl_files_rwlock);
9244b2c3 1836 list_for_each_entry(ctl, &card->ctl_files, list) {
1da177e4
LT
1837 wake_up(&ctl->change_sleep);
1838 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1839 }
d8009882 1840 read_unlock(&card->ctl_files_rwlock);
c461482c 1841
40a4b263 1842 return snd_unregister_device(&card->ctl_dev);
1da177e4
LT
1843}
1844
1845/*
1846 * free all controls
1847 */
82e9bae6 1848static int snd_ctl_dev_free(struct snd_device *device)
1da177e4 1849{
82e9bae6
TI
1850 struct snd_card *card = device->device_data;
1851 struct snd_kcontrol *control;
1da177e4
LT
1852
1853 down_write(&card->controls_rwsem);
1854 while (!list_empty(&card->controls)) {
1855 control = snd_kcontrol(card->controls.next);
1856 snd_ctl_remove(card, control);
1857 }
1858 up_write(&card->controls_rwsem);
0fcd9f4b 1859 put_device(&card->ctl_dev);
1da177e4
LT
1860 return 0;
1861}
1862
1da177e4
LT
1863/*
1864 * create control core:
1865 * called from init.c
1866 */
82e9bae6 1867int snd_ctl_create(struct snd_card *card)
1da177e4 1868{
82e9bae6 1869 static struct snd_device_ops ops = {
1da177e4
LT
1870 .dev_free = snd_ctl_dev_free,
1871 .dev_register = snd_ctl_dev_register,
1872 .dev_disconnect = snd_ctl_dev_disconnect,
1da177e4 1873 };
0fcd9f4b 1874 int err;
1da177e4 1875
7eaa943c
TI
1876 if (snd_BUG_ON(!card))
1877 return -ENXIO;
0fcd9f4b
TI
1878 if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
1879 return -ENXIO;
1880
1881 snd_device_initialize(&card->ctl_dev, card);
1882 dev_set_name(&card->ctl_dev, "controlC%d", card->number);
1883
1884 err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1885 if (err < 0)
1886 put_device(&card->ctl_dev);
1887 return err;
1da177e4 1888}
b9ed4f2b
TI
1889
1890/*
9600732b 1891 * Frequently used control callbacks/helpers
b9ed4f2b 1892 */
12cddbd8
TI
1893
1894/**
1895 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
1896 * callback with a mono channel
1897 * @kcontrol: the kcontrol instance
1898 * @uinfo: info to store
1899 *
1900 * This is a function that can be used as info callback for a standard
1901 * boolean control with a single mono channel.
1902 */
b9ed4f2b
TI
1903int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1904 struct snd_ctl_elem_info *uinfo)
1905{
1906 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1907 uinfo->count = 1;
1908 uinfo->value.integer.min = 0;
1909 uinfo->value.integer.max = 1;
1910 return 0;
1911}
b9ed4f2b
TI
1912EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1913
12cddbd8
TI
1914/**
1915 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
1916 * callback with stereo two channels
1917 * @kcontrol: the kcontrol instance
1918 * @uinfo: info to store
1919 *
1920 * This is a function that can be used as info callback for a standard
1921 * boolean control with stereo two channels.
1922 */
b9ed4f2b
TI
1923int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1924 struct snd_ctl_elem_info *uinfo)
1925{
1926 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1927 uinfo->count = 2;
1928 uinfo->value.integer.min = 0;
1929 uinfo->value.integer.max = 1;
1930 return 0;
1931}
b9ed4f2b 1932EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
9600732b
CL
1933
1934/**
1935 * snd_ctl_enum_info - fills the info structure for an enumerated control
1936 * @info: the structure to be filled
1937 * @channels: the number of the control's channels; often one
1938 * @items: the number of control values; also the size of @names
1939 * @names: an array containing the names of all control values
1940 *
1941 * Sets all required fields in @info to their appropriate values.
1942 * If the control's accessibility is not the default (readable and writable),
1943 * the caller has to fill @info->access.
eb7c06e8
YB
1944 *
1945 * Return: Zero.
9600732b
CL
1946 */
1947int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1948 unsigned int items, const char *const names[])
1949{
1950 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1951 info->count = channels;
1952 info->value.enumerated.items = items;
a7e6fb99
TI
1953 if (!items)
1954 return 0;
9600732b
CL
1955 if (info->value.enumerated.item >= items)
1956 info->value.enumerated.item = items - 1;
df803e13
TI
1957 WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
1958 "ALSA: too long item name '%s'\n",
1959 names[info->value.enumerated.item]);
9600732b
CL
1960 strlcpy(info->value.enumerated.name,
1961 names[info->value.enumerated.item],
1962 sizeof(info->value.enumerated.name));
1963 return 0;
1964}
1965EXPORT_SYMBOL(snd_ctl_enum_info);