1 /*
2 * Routines for driver control interface
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22 #include <linux/threads.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/time.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <sound/info.h>
31 #include <sound/control.h>
32
33 /* max number of user-defined controls */
34 #define MAX_USER_CONTROLS 32
35 #define MAX_CONTROL_COUNT 1028
36
37 struct snd_kctl_ioctl {
38 struct list_head list; /* list of all ioctls */
39 snd_kctl_ioctl_func_t fioctl;
40 };
41
42 static DECLARE_RWSEM(snd_ioctl_rwsem);
43 static LIST_HEAD(snd_control_ioctls);
44 #ifdef CONFIG_COMPAT
45 static LIST_HEAD(snd_control_compat_ioctls);
46 #endif
47
snd_ctl_open(struct inode * inode,struct file * file)48 static int snd_ctl_open(struct inode *inode, struct file *file)
49 {
50 unsigned long flags;
51 struct snd_card *card;
52 struct snd_ctl_file *ctl;
53 int err;
54
55 err = nonseekable_open(inode, file);
56 if (err < 0)
57 return err;
58
59 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
60 if (!card) {
61 err = -ENODEV;
62 goto __error1;
63 }
64 err = snd_card_file_add(card, file);
65 if (err < 0) {
66 err = -ENODEV;
67 goto __error1;
68 }
69 if (!try_module_get(card->module)) {
70 err = -EFAULT;
71 goto __error2;
72 }
73 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
74 if (ctl == NULL) {
75 err = -ENOMEM;
76 goto __error;
77 }
78 INIT_LIST_HEAD(&ctl->events);
79 init_waitqueue_head(&ctl->change_sleep);
80 spin_lock_init(&ctl->read_lock);
81 ctl->card = card;
82 ctl->prefer_pcm_subdevice = -1;
83 ctl->prefer_rawmidi_subdevice = -1;
84 ctl->pid = get_pid(task_pid(current));
85 file->private_data = ctl;
86 write_lock_irqsave(&card->ctl_files_rwlock, flags);
87 list_add_tail(&ctl->list, &card->ctl_files);
88 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
89 snd_card_unref(card);
90 return 0;
91
92 __error:
93 module_put(card->module);
94 __error2:
95 snd_card_file_remove(card, file);
96 __error1:
97 if (card)
98 snd_card_unref(card);
99 return err;
100 }
101
snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)102 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
103 {
104 unsigned long flags;
105 struct snd_kctl_event *cread;
106
107 spin_lock_irqsave(&ctl->read_lock, flags);
108 while (!list_empty(&ctl->events)) {
109 cread = snd_kctl_event(ctl->events.next);
110 list_del(&cread->list);
111 kfree(cread);
112 }
113 spin_unlock_irqrestore(&ctl->read_lock, flags);
114 }
115
snd_ctl_release(struct inode * inode,struct file * file)116 static int snd_ctl_release(struct inode *inode, struct file *file)
117 {
118 unsigned long flags;
119 struct snd_card *card;
120 struct snd_ctl_file *ctl;
121 struct snd_kcontrol *control;
122 unsigned int idx;
123
124 ctl = file->private_data;
125 file->private_data = NULL;
126 card = ctl->card;
127 write_lock_irqsave(&card->ctl_files_rwlock, flags);
128 list_del(&ctl->list);
129 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
130 down_write(&card->controls_rwsem);
131 list_for_each_entry(control, &card->controls, list)
132 for (idx = 0; idx < control->count; idx++)
133 if (control->vd[idx].owner == ctl)
134 control->vd[idx].owner = NULL;
135 up_write(&card->controls_rwsem);
136 snd_ctl_empty_read_queue(ctl);
137 put_pid(ctl->pid);
138 kfree(ctl);
139 module_put(card->module);
140 snd_card_file_remove(card, file);
141 return 0;
142 }
143
snd_ctl_notify(struct snd_card * card,unsigned int mask,struct snd_ctl_elem_id * id)144 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
145 struct snd_ctl_elem_id *id)
146 {
147 unsigned long flags;
148 struct snd_ctl_file *ctl;
149 struct snd_kctl_event *ev;
150
151 if (snd_BUG_ON(!card || !id))
152 return;
153 if (card->shutdown)
154 return;
155 read_lock(&card->ctl_files_rwlock);
156 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
157 card->mixer_oss_change_count++;
158 #endif
159 list_for_each_entry(ctl, &card->ctl_files, list) {
160 if (!ctl->subscribed)
161 continue;
162 spin_lock_irqsave(&ctl->read_lock, flags);
163 list_for_each_entry(ev, &ctl->events, list) {
164 if (ev->id.numid == id->numid) {
165 ev->mask |= mask;
166 goto _found;
167 }
168 }
169 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
170 if (ev) {
171 ev->id = *id;
172 ev->mask = mask;
173 list_add_tail(&ev->list, &ctl->events);
174 } else {
175 dev_err(card->dev, "No memory available to allocate event\n");
176 }
177 _found:
178 wake_up(&ctl->change_sleep);
179 spin_unlock_irqrestore(&ctl->read_lock, flags);
180 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
181 }
182 read_unlock(&card->ctl_files_rwlock);
183 }
184
185 EXPORT_SYMBOL(snd_ctl_notify);
186
187 /**
188 * snd_ctl_new - create a control instance from the template
189 * @control: the control template
190 * @access: the default control access
191 *
192 * Allocates a new struct snd_kcontrol instance and copies the given template
193 * to the new instance. It does not copy volatile data (access).
194 *
195 * Return: The pointer of the new instance, or %NULL on failure.
196 */
snd_ctl_new(struct snd_kcontrol * control,unsigned int access)197 static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
198 unsigned int access)
199 {
200 struct snd_kcontrol *kctl;
201 unsigned int idx;
202
203 if (snd_BUG_ON(!control || !control->count))
204 return NULL;
205
206 if (control->count > MAX_CONTROL_COUNT)
207 return NULL;
208
209 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
210 if (kctl == NULL) {
211 pr_err("ALSA: Cannot allocate control instance\n");
212 return NULL;
213 }
214 *kctl = *control;
215 for (idx = 0; idx < kctl->count; idx++)
216 kctl->vd[idx].access = access;
217 return kctl;
218 }
219
220 /**
221 * snd_ctl_new1 - create a control instance from the template
222 * @ncontrol: the initialization record
223 * @private_data: the private data to set
224 *
225 * Allocates a new struct snd_kcontrol instance and initialize from the given
226 * template. When the access field of ncontrol is 0, it's assumed as
227 * READWRITE access. When the count field is 0, it's assumes as one.
228 *
229 * Return: The pointer of the newly generated instance, or %NULL on failure.
230 */
snd_ctl_new1(const struct snd_kcontrol_new * ncontrol,void * private_data)231 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
232 void *private_data)
233 {
234 struct snd_kcontrol kctl;
235 unsigned int access;
236
237 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
238 return NULL;
239 memset(&kctl, 0, sizeof(kctl));
240 kctl.id.iface = ncontrol->iface;
241 kctl.id.device = ncontrol->device;
242 kctl.id.subdevice = ncontrol->subdevice;
243 if (ncontrol->name) {
244 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
245 if (strcmp(ncontrol->name, kctl.id.name) != 0)
246 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
247 ncontrol->name, kctl.id.name);
248 }
249 kctl.id.index = ncontrol->index;
250 kctl.count = ncontrol->count ? ncontrol->count : 1;
251 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
252 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
253 SNDRV_CTL_ELEM_ACCESS_VOLATILE|
254 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
255 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
256 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
257 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
258 kctl.info = ncontrol->info;
259 kctl.get = ncontrol->get;
260 kctl.put = ncontrol->put;
261 kctl.tlv.p = ncontrol->tlv.p;
262 kctl.private_value = ncontrol->private_value;
263 kctl.private_data = private_data;
264 return snd_ctl_new(&kctl, access);
265 }
266
267 EXPORT_SYMBOL(snd_ctl_new1);
268
269 /**
270 * snd_ctl_free_one - release the control instance
271 * @kcontrol: the control instance
272 *
273 * Releases the control instance created via snd_ctl_new()
274 * or snd_ctl_new1().
275 * Don't call this after the control was added to the card.
276 */
snd_ctl_free_one(struct snd_kcontrol * kcontrol)277 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
278 {
279 if (kcontrol) {
280 if (kcontrol->private_free)
281 kcontrol->private_free(kcontrol);
282 kfree(kcontrol);
283 }
284 }
285
286 EXPORT_SYMBOL(snd_ctl_free_one);
287
snd_ctl_remove_numid_conflict(struct snd_card * card,unsigned int count)288 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
289 unsigned int count)
290 {
291 struct snd_kcontrol *kctl;
292
293 /* Make sure that the ids assigned to the control do not wrap around */
294 if (card->last_numid >= UINT_MAX - count)
295 card->last_numid = 0;
296
297 list_for_each_entry(kctl, &card->controls, list) {
298 if (kctl->id.numid < card->last_numid + 1 + count &&
299 kctl->id.numid + kctl->count > card->last_numid + 1) {
300 card->last_numid = kctl->id.numid + kctl->count - 1;
301 return true;
302 }
303 }
304 return false;
305 }
306
snd_ctl_find_hole(struct snd_card * card,unsigned int count)307 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
308 {
309 unsigned int iter = 100000;
310
311 while (snd_ctl_remove_numid_conflict(card, count)) {
312 if (--iter == 0) {
313 /* this situation is very unlikely */
314 dev_err(card->dev, "unable to allocate new control numid\n");
315 return -ENOMEM;
316 }
317 }
318 return 0;
319 }
320
321 /**
322 * snd_ctl_add - add the control instance to the card
323 * @card: the card instance
324 * @kcontrol: the control instance to add
325 *
326 * Adds the control instance created via snd_ctl_new() or
327 * snd_ctl_new1() to the given card. Assigns also an unique
328 * numid used for fast search.
329 *
330 * It frees automatically the control which cannot be added.
331 *
332 * Return: Zero if successful, or a negative error code on failure.
333 *
334 */
snd_ctl_add(struct snd_card * card,struct snd_kcontrol * kcontrol)335 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
336 {
337 struct snd_ctl_elem_id id;
338 unsigned int idx;
339 unsigned int count;
340 int err = -EINVAL;
341
342 if (! kcontrol)
343 return err;
344 if (snd_BUG_ON(!card || !kcontrol->info))
345 goto error;
346 id = kcontrol->id;
347 if (id.index > UINT_MAX - kcontrol->count)
348 goto error;
349
350 down_write(&card->controls_rwsem);
351 if (snd_ctl_find_id(card, &id)) {
352 up_write(&card->controls_rwsem);
353 dev_err(card->dev, "control %i:%i:%i:%s:%i is already present\n",
354 id.iface,
355 id.device,
356 id.subdevice,
357 id.name,
358 id.index);
359 err = -EBUSY;
360 goto error;
361 }
362 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
363 up_write(&card->controls_rwsem);
364 err = -ENOMEM;
365 goto error;
366 }
367 list_add_tail(&kcontrol->list, &card->controls);
368 card->controls_count += kcontrol->count;
369 kcontrol->id.numid = card->last_numid + 1;
370 card->last_numid += kcontrol->count;
371 count = kcontrol->count;
372 up_write(&card->controls_rwsem);
373 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
374 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
375 return 0;
376
377 error:
378 snd_ctl_free_one(kcontrol);
379 return err;
380 }
381
382 EXPORT_SYMBOL(snd_ctl_add);
383
384 /**
385 * snd_ctl_replace - replace the control instance of the card
386 * @card: the card instance
387 * @kcontrol: the control instance to replace
388 * @add_on_replace: add the control if not already added
389 *
390 * Replaces the given control. If the given control does not exist
391 * and the add_on_replace flag is set, the control is added. If the
392 * control exists, it is destroyed first.
393 *
394 * It frees automatically the control which cannot be added or replaced.
395 *
396 * Return: Zero if successful, or a negative error code on failure.
397 */
snd_ctl_replace(struct snd_card * card,struct snd_kcontrol * kcontrol,bool add_on_replace)398 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
399 bool add_on_replace)
400 {
401 struct snd_ctl_elem_id id;
402 unsigned int count;
403 unsigned int idx;
404 struct snd_kcontrol *old;
405 int ret;
406
407 if (!kcontrol)
408 return -EINVAL;
409 if (snd_BUG_ON(!card || !kcontrol->info)) {
410 ret = -EINVAL;
411 goto error;
412 }
413 id = kcontrol->id;
414 down_write(&card->controls_rwsem);
415 old = snd_ctl_find_id(card, &id);
416 if (!old) {
417 if (add_on_replace)
418 goto add;
419 up_write(&card->controls_rwsem);
420 ret = -EINVAL;
421 goto error;
422 }
423 ret = snd_ctl_remove(card, old);
424 if (ret < 0) {
425 up_write(&card->controls_rwsem);
426 goto error;
427 }
428 add:
429 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
430 up_write(&card->controls_rwsem);
431 ret = -ENOMEM;
432 goto error;
433 }
434 list_add_tail(&kcontrol->list, &card->controls);
435 card->controls_count += kcontrol->count;
436 kcontrol->id.numid = card->last_numid + 1;
437 card->last_numid += kcontrol->count;
438 count = kcontrol->count;
439 up_write(&card->controls_rwsem);
440 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
441 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
442 return 0;
443
444 error:
445 snd_ctl_free_one(kcontrol);
446 return ret;
447 }
448 EXPORT_SYMBOL(snd_ctl_replace);
449
450 /**
451 * snd_ctl_remove - remove the control from the card and release it
452 * @card: the card instance
453 * @kcontrol: the control instance to remove
454 *
455 * Removes the control from the card and then releases the instance.
456 * You don't need to call snd_ctl_free_one(). You must be in
457 * the write lock - down_write(&card->controls_rwsem).
458 *
459 * Return: 0 if successful, or a negative error code on failure.
460 */
snd_ctl_remove(struct snd_card * card,struct snd_kcontrol * kcontrol)461 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
462 {
463 struct snd_ctl_elem_id id;
464 unsigned int idx;
465
466 if (snd_BUG_ON(!card || !kcontrol))
467 return -EINVAL;
468 list_del(&kcontrol->list);
469 card->controls_count -= kcontrol->count;
470 id = kcontrol->id;
471 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
472 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
473 snd_ctl_free_one(kcontrol);
474 return 0;
475 }
476
477 EXPORT_SYMBOL(snd_ctl_remove);
478
479 /**
480 * snd_ctl_remove_id - remove the control of the given id and release it
481 * @card: the card instance
482 * @id: the control id to remove
483 *
484 * Finds the control instance with the given id, removes it from the
485 * card list and releases it.
486 *
487 * Return: 0 if successful, or a negative error code on failure.
488 */
snd_ctl_remove_id(struct snd_card * card,struct snd_ctl_elem_id * id)489 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
490 {
491 struct snd_kcontrol *kctl;
492 int ret;
493
494 down_write(&card->controls_rwsem);
495 kctl = snd_ctl_find_id(card, id);
496 if (kctl == NULL) {
497 up_write(&card->controls_rwsem);
498 return -ENOENT;
499 }
500 ret = snd_ctl_remove(card, kctl);
501 up_write(&card->controls_rwsem);
502 return ret;
503 }
504
505 EXPORT_SYMBOL(snd_ctl_remove_id);
506
507 /**
508 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
509 * @file: active control handle
510 * @id: the control id to remove
511 *
512 * Finds the control instance with the given id, removes it from the
513 * card list and releases it.
514 *
515 * Return: 0 if successful, or a negative error code on failure.
516 */
snd_ctl_remove_user_ctl(struct snd_ctl_file * file,struct snd_ctl_elem_id * id)517 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
518 struct snd_ctl_elem_id *id)
519 {
520 struct snd_card *card = file->card;
521 struct snd_kcontrol *kctl;
522 int idx, ret;
523
524 down_write(&card->controls_rwsem);
525 kctl = snd_ctl_find_id(card, id);
526 if (kctl == NULL) {
527 ret = -ENOENT;
528 goto error;
529 }
530 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
531 ret = -EINVAL;
532 goto error;
533 }
534 for (idx = 0; idx < kctl->count; idx++)
535 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
536 ret = -EBUSY;
537 goto error;
538 }
539 ret = snd_ctl_remove(card, kctl);
540 if (ret < 0)
541 goto error;
542 card->user_ctl_count--;
543 error:
544 up_write(&card->controls_rwsem);
545 return ret;
546 }
547
548 /**
549 * snd_ctl_activate_id - activate/inactivate the control of the given id
550 * @card: the card instance
551 * @id: the control id to activate/inactivate
552 * @active: non-zero to activate
553 *
554 * Finds the control instance with the given id, and activate or
555 * inactivate the control together with notification, if changed.
556 *
557 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
558 */
snd_ctl_activate_id(struct snd_card * card,struct snd_ctl_elem_id * id,int active)559 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
560 int active)
561 {
562 struct snd_kcontrol *kctl;
563 struct snd_kcontrol_volatile *vd;
564 unsigned int index_offset;
565 int ret;
566
567 down_write(&card->controls_rwsem);
568 kctl = snd_ctl_find_id(card, id);
569 if (kctl == NULL) {
570 ret = -ENOENT;
571 goto unlock;
572 }
573 index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
574 vd = &kctl->vd[index_offset];
575 ret = 0;
576 if (active) {
577 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
578 goto unlock;
579 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
580 } else {
581 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
582 goto unlock;
583 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
584 }
585 ret = 1;
586 unlock:
587 up_write(&card->controls_rwsem);
588 if (ret > 0)
589 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
590 return ret;
591 }
592 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
593
594 /**
595 * snd_ctl_rename_id - replace the id of a control on the card
596 * @card: the card instance
597 * @src_id: the old id
598 * @dst_id: the new id
599 *
600 * Finds the control with the old id from the card, and replaces the
601 * id with the new one.
602 *
603 * Return: Zero if successful, or a negative error code on failure.
604 */
snd_ctl_rename_id(struct snd_card * card,struct snd_ctl_elem_id * src_id,struct snd_ctl_elem_id * dst_id)605 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
606 struct snd_ctl_elem_id *dst_id)
607 {
608 struct snd_kcontrol *kctl;
609
610 down_write(&card->controls_rwsem);
611 kctl = snd_ctl_find_id(card, src_id);
612 if (kctl == NULL) {
613 up_write(&card->controls_rwsem);
614 return -ENOENT;
615 }
616 kctl->id = *dst_id;
617 kctl->id.numid = card->last_numid + 1;
618 card->last_numid += kctl->count;
619 up_write(&card->controls_rwsem);
620 return 0;
621 }
622
623 EXPORT_SYMBOL(snd_ctl_rename_id);
624
625 /**
626 * snd_ctl_find_numid - find the control instance with the given number-id
627 * @card: the card instance
628 * @numid: the number-id to search
629 *
630 * Finds the control instance with the given number-id from the card.
631 *
632 * The caller must down card->controls_rwsem before calling this function
633 * (if the race condition can happen).
634 *
635 * Return: The pointer of the instance if found, or %NULL if not.
636 *
637 */
snd_ctl_find_numid(struct snd_card * card,unsigned int numid)638 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
639 {
640 struct snd_kcontrol *kctl;
641
642 if (snd_BUG_ON(!card || !numid))
643 return NULL;
644 list_for_each_entry(kctl, &card->controls, list) {
645 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
646 return kctl;
647 }
648 return NULL;
649 }
650
651 EXPORT_SYMBOL(snd_ctl_find_numid);
652
653 /**
654 * snd_ctl_find_id - find the control instance with the given id
655 * @card: the card instance
656 * @id: the id to search
657 *
658 * Finds the control instance with the given id from the card.
659 *
660 * The caller must down card->controls_rwsem before calling this function
661 * (if the race condition can happen).
662 *
663 * Return: The pointer of the instance if found, or %NULL if not.
664 *
665 */
snd_ctl_find_id(struct snd_card * card,struct snd_ctl_elem_id * id)666 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
667 struct snd_ctl_elem_id *id)
668 {
669 struct snd_kcontrol *kctl;
670
671 if (snd_BUG_ON(!card || !id))
672 return NULL;
673 if (id->numid != 0)
674 return snd_ctl_find_numid(card, id->numid);
675 list_for_each_entry(kctl, &card->controls, list) {
676 if (kctl->id.iface != id->iface)
677 continue;
678 if (kctl->id.device != id->device)
679 continue;
680 if (kctl->id.subdevice != id->subdevice)
681 continue;
682 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
683 continue;
684 if (kctl->id.index > id->index)
685 continue;
686 if (kctl->id.index + kctl->count <= id->index)
687 continue;
688 return kctl;
689 }
690 return NULL;
691 }
692
693 EXPORT_SYMBOL(snd_ctl_find_id);
694
snd_ctl_card_info(struct snd_card * card,struct snd_ctl_file * ctl,unsigned int cmd,void __user * arg)695 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
696 unsigned int cmd, void __user *arg)
697 {
698 struct snd_ctl_card_info *info;
699
700 info = kzalloc(sizeof(*info), GFP_KERNEL);
701 if (! info)
702 return -ENOMEM;
703 down_read(&snd_ioctl_rwsem);
704 info->card = card->number;
705 strlcpy(info->id, card->id, sizeof(info->id));
706 strlcpy(info->driver, card->driver, sizeof(info->driver));
707 strlcpy(info->name, card->shortname, sizeof(info->name));
708 strlcpy(info->longname, card->longname, sizeof(info->longname));
709 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
710 strlcpy(info->components, card->components, sizeof(info->components));
711 up_read(&snd_ioctl_rwsem);
712 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
713 kfree(info);
714 return -EFAULT;
715 }
716 kfree(info);
717 return 0;
718 }
719
snd_ctl_elem_list(struct snd_card * card,struct snd_ctl_elem_list __user * _list)720 static int snd_ctl_elem_list(struct snd_card *card,
721 struct snd_ctl_elem_list __user *_list)
722 {
723 struct list_head *plist;
724 struct snd_ctl_elem_list list;
725 struct snd_kcontrol *kctl;
726 struct snd_ctl_elem_id *dst, *id;
727 unsigned int offset, space, jidx;
728
729 if (copy_from_user(&list, _list, sizeof(list)))
730 return -EFAULT;
731 offset = list.offset;
732 space = list.space;
733 /* try limit maximum space */
734 if (space > 16384)
735 return -ENOMEM;
736 if (space > 0) {
737 /* allocate temporary buffer for atomic operation */
738 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
739 if (dst == NULL)
740 return -ENOMEM;
741 down_read(&card->controls_rwsem);
742 list.count = card->controls_count;
743 plist = card->controls.next;
744 while (plist != &card->controls) {
745 if (offset == 0)
746 break;
747 kctl = snd_kcontrol(plist);
748 if (offset < kctl->count)
749 break;
750 offset -= kctl->count;
751 plist = plist->next;
752 }
753 list.used = 0;
754 id = dst;
755 while (space > 0 && plist != &card->controls) {
756 kctl = snd_kcontrol(plist);
757 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
758 snd_ctl_build_ioff(id, kctl, jidx);
759 id++;
760 space--;
761 list.used++;
762 }
763 plist = plist->next;
764 offset = 0;
765 }
766 up_read(&card->controls_rwsem);
767 if (list.used > 0 &&
768 copy_to_user(list.pids, dst,
769 list.used * sizeof(struct snd_ctl_elem_id))) {
770 vfree(dst);
771 return -EFAULT;
772 }
773 vfree(dst);
774 } else {
775 down_read(&card->controls_rwsem);
776 list.count = card->controls_count;
777 up_read(&card->controls_rwsem);
778 }
779 if (copy_to_user(_list, &list, sizeof(list)))
780 return -EFAULT;
781 return 0;
782 }
783
snd_ctl_elem_info(struct snd_ctl_file * ctl,struct snd_ctl_elem_info * info)784 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
785 struct snd_ctl_elem_info *info)
786 {
787 struct snd_card *card = ctl->card;
788 struct snd_kcontrol *kctl;
789 struct snd_kcontrol_volatile *vd;
790 unsigned int index_offset;
791 int result;
792
793 down_read(&card->controls_rwsem);
794 kctl = snd_ctl_find_id(card, &info->id);
795 if (kctl == NULL) {
796 up_read(&card->controls_rwsem);
797 return -ENOENT;
798 }
799 #ifdef CONFIG_SND_DEBUG
800 info->access = 0;
801 #endif
802 result = kctl->info(kctl, info);
803 if (result >= 0) {
804 snd_BUG_ON(info->access);
805 index_offset = snd_ctl_get_ioff(kctl, &info->id);
806 vd = &kctl->vd[index_offset];
807 snd_ctl_build_ioff(&info->id, kctl, index_offset);
808 info->access = vd->access;
809 if (vd->owner) {
810 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
811 if (vd->owner == ctl)
812 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
813 info->owner = pid_vnr(vd->owner->pid);
814 } else {
815 info->owner = -1;
816 }
817 }
818 up_read(&card->controls_rwsem);
819 return result;
820 }
821
snd_ctl_elem_info_user(struct snd_ctl_file * ctl,struct snd_ctl_elem_info __user * _info)822 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
823 struct snd_ctl_elem_info __user *_info)
824 {
825 struct snd_ctl_elem_info info;
826 int result;
827
828 if (copy_from_user(&info, _info, sizeof(info)))
829 return -EFAULT;
830 snd_power_lock(ctl->card);
831 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
832 if (result >= 0)
833 result = snd_ctl_elem_info(ctl, &info);
834 snd_power_unlock(ctl->card);
835 if (result >= 0)
836 if (copy_to_user(_info, &info, sizeof(info)))
837 return -EFAULT;
838 return result;
839 }
840
snd_ctl_elem_read(struct snd_card * card,struct snd_ctl_elem_value * control)841 static int snd_ctl_elem_read(struct snd_card *card,
842 struct snd_ctl_elem_value *control)
843 {
844 struct snd_kcontrol *kctl;
845 struct snd_kcontrol_volatile *vd;
846 unsigned int index_offset;
847 int result;
848
849 down_read(&card->controls_rwsem);
850 kctl = snd_ctl_find_id(card, &control->id);
851 if (kctl == NULL) {
852 result = -ENOENT;
853 } else {
854 index_offset = snd_ctl_get_ioff(kctl, &control->id);
855 vd = &kctl->vd[index_offset];
856 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
857 kctl->get != NULL) {
858 snd_ctl_build_ioff(&control->id, kctl, index_offset);
859 result = kctl->get(kctl, control);
860 } else
861 result = -EPERM;
862 }
863 up_read(&card->controls_rwsem);
864 return result;
865 }
866
snd_ctl_elem_read_user(struct snd_card * card,struct snd_ctl_elem_value __user * _control)867 static int snd_ctl_elem_read_user(struct snd_card *card,
868 struct snd_ctl_elem_value __user *_control)
869 {
870 struct snd_ctl_elem_value *control;
871 int result;
872
873 control = memdup_user(_control, sizeof(*control));
874 if (IS_ERR(control))
875 return PTR_ERR(control);
876
877 snd_power_lock(card);
878 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
879 if (result >= 0)
880 result = snd_ctl_elem_read(card, control);
881 snd_power_unlock(card);
882 if (result >= 0)
883 if (copy_to_user(_control, control, sizeof(*control)))
884 result = -EFAULT;
885 kfree(control);
886 return result;
887 }
888
snd_ctl_elem_write(struct snd_card * card,struct snd_ctl_file * file,struct snd_ctl_elem_value * control)889 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
890 struct snd_ctl_elem_value *control)
891 {
892 struct snd_kcontrol *kctl;
893 struct snd_kcontrol_volatile *vd;
894 unsigned int index_offset;
895 int result;
896
897 down_read(&card->controls_rwsem);
898 kctl = snd_ctl_find_id(card, &control->id);
899 if (kctl == NULL) {
900 result = -ENOENT;
901 } else {
902 index_offset = snd_ctl_get_ioff(kctl, &control->id);
903 vd = &kctl->vd[index_offset];
904 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
905 kctl->put == NULL ||
906 (file && vd->owner && vd->owner != file)) {
907 result = -EPERM;
908 } else {
909 snd_ctl_build_ioff(&control->id, kctl, index_offset);
910 result = kctl->put(kctl, control);
911 }
912 if (result > 0) {
913 struct snd_ctl_elem_id id = control->id;
914 up_read(&card->controls_rwsem);
915 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
916 return 0;
917 }
918 }
919 up_read(&card->controls_rwsem);
920 return result;
921 }
922
snd_ctl_elem_write_user(struct snd_ctl_file * file,struct snd_ctl_elem_value __user * _control)923 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
924 struct snd_ctl_elem_value __user *_control)
925 {
926 struct snd_ctl_elem_value *control;
927 struct snd_card *card;
928 int result;
929
930 control = memdup_user(_control, sizeof(*control));
931 if (IS_ERR(control))
932 return PTR_ERR(control);
933
934 card = file->card;
935 snd_power_lock(card);
936 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
937 if (result >= 0)
938 result = snd_ctl_elem_write(card, file, control);
939 snd_power_unlock(card);
940 if (result >= 0)
941 if (copy_to_user(_control, control, sizeof(*control)))
942 result = -EFAULT;
943 kfree(control);
944 return result;
945 }
946
snd_ctl_elem_lock(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)947 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
948 struct snd_ctl_elem_id __user *_id)
949 {
950 struct snd_card *card = file->card;
951 struct snd_ctl_elem_id id;
952 struct snd_kcontrol *kctl;
953 struct snd_kcontrol_volatile *vd;
954 int result;
955
956 if (copy_from_user(&id, _id, sizeof(id)))
957 return -EFAULT;
958 down_write(&card->controls_rwsem);
959 kctl = snd_ctl_find_id(card, &id);
960 if (kctl == NULL) {
961 result = -ENOENT;
962 } else {
963 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
964 if (vd->owner != NULL)
965 result = -EBUSY;
966 else {
967 vd->owner = file;
968 result = 0;
969 }
970 }
971 up_write(&card->controls_rwsem);
972 return result;
973 }
974
snd_ctl_elem_unlock(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)975 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
976 struct snd_ctl_elem_id __user *_id)
977 {
978 struct snd_card *card = file->card;
979 struct snd_ctl_elem_id id;
980 struct snd_kcontrol *kctl;
981 struct snd_kcontrol_volatile *vd;
982 int result;
983
984 if (copy_from_user(&id, _id, sizeof(id)))
985 return -EFAULT;
986 down_write(&card->controls_rwsem);
987 kctl = snd_ctl_find_id(card, &id);
988 if (kctl == NULL) {
989 result = -ENOENT;
990 } else {
991 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
992 if (vd->owner == NULL)
993 result = -EINVAL;
994 else if (vd->owner != file)
995 result = -EPERM;
996 else {
997 vd->owner = NULL;
998 result = 0;
999 }
1000 }
1001 up_write(&card->controls_rwsem);
1002 return result;
1003 }
1004
1005 struct user_element {
1006 struct snd_ctl_elem_info info;
1007 struct snd_card *card;
1008 void *elem_data; /* element data */
1009 unsigned long elem_data_size; /* size of element data in bytes */
1010 void *tlv_data; /* TLV data */
1011 unsigned long tlv_data_size; /* TLV data size */
1012 void *priv_data; /* private data (like strings for enumerated type) */
1013 };
1014
snd_ctl_elem_user_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1015 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1016 struct snd_ctl_elem_info *uinfo)
1017 {
1018 struct user_element *ue = kcontrol->private_data;
1019
1020 *uinfo = ue->info;
1021 return 0;
1022 }
1023
snd_ctl_elem_user_enum_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1024 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1025 struct snd_ctl_elem_info *uinfo)
1026 {
1027 struct user_element *ue = kcontrol->private_data;
1028 const char *names;
1029 unsigned int item;
1030
1031 item = uinfo->value.enumerated.item;
1032
1033 *uinfo = ue->info;
1034
1035 item = min(item, uinfo->value.enumerated.items - 1);
1036 uinfo->value.enumerated.item = item;
1037
1038 names = ue->priv_data;
1039 for (; item > 0; --item)
1040 names += strlen(names) + 1;
1041 strcpy(uinfo->value.enumerated.name, names);
1042
1043 return 0;
1044 }
1045
snd_ctl_elem_user_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1046 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1047 struct snd_ctl_elem_value *ucontrol)
1048 {
1049 struct user_element *ue = kcontrol->private_data;
1050
1051 mutex_lock(&ue->card->user_ctl_lock);
1052 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
1053 mutex_unlock(&ue->card->user_ctl_lock);
1054 return 0;
1055 }
1056
snd_ctl_elem_user_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1057 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1058 struct snd_ctl_elem_value *ucontrol)
1059 {
1060 int change;
1061 struct user_element *ue = kcontrol->private_data;
1062
1063 mutex_lock(&ue->card->user_ctl_lock);
1064 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
1065 if (change)
1066 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
1067 mutex_unlock(&ue->card->user_ctl_lock);
1068 return change;
1069 }
1070
snd_ctl_elem_user_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * tlv)1071 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
1072 int op_flag,
1073 unsigned int size,
1074 unsigned int __user *tlv)
1075 {
1076 struct user_element *ue = kcontrol->private_data;
1077 int change = 0;
1078 void *new_data;
1079
1080 if (op_flag > 0) {
1081 if (size > 1024 * 128) /* sane value */
1082 return -EINVAL;
1083
1084 new_data = memdup_user(tlv, size);
1085 if (IS_ERR(new_data))
1086 return PTR_ERR(new_data);
1087 mutex_lock(&ue->card->user_ctl_lock);
1088 change = ue->tlv_data_size != size;
1089 if (!change)
1090 change = memcmp(ue->tlv_data, new_data, size) != 0;
1091 kfree(ue->tlv_data);
1092 ue->tlv_data = new_data;
1093 ue->tlv_data_size = size;
1094 mutex_unlock(&ue->card->user_ctl_lock);
1095 } else {
1096 int ret = 0;
1097
1098 mutex_lock(&ue->card->user_ctl_lock);
1099 if (!ue->tlv_data_size || !ue->tlv_data) {
1100 ret = -ENXIO;
1101 goto err_unlock;
1102 }
1103 if (size < ue->tlv_data_size) {
1104 ret = -ENOSPC;
1105 goto err_unlock;
1106 }
1107 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
1108 ret = -EFAULT;
1109 err_unlock:
1110 mutex_unlock(&ue->card->user_ctl_lock);
1111 if (ret)
1112 return ret;
1113 }
1114 return change;
1115 }
1116
snd_ctl_elem_init_enum_names(struct user_element * ue)1117 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1118 {
1119 char *names, *p;
1120 size_t buf_len, name_len;
1121 unsigned int i;
1122 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1123
1124 if (ue->info.value.enumerated.names_length > 64 * 1024)
1125 return -EINVAL;
1126
1127 names = memdup_user((const void __user *)user_ptrval,
1128 ue->info.value.enumerated.names_length);
1129 if (IS_ERR(names))
1130 return PTR_ERR(names);
1131
1132 /* check that there are enough valid names */
1133 buf_len = ue->info.value.enumerated.names_length;
1134 p = names;
1135 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1136 name_len = strnlen(p, buf_len);
1137 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1138 kfree(names);
1139 return -EINVAL;
1140 }
1141 p += name_len + 1;
1142 buf_len -= name_len + 1;
1143 }
1144
1145 ue->priv_data = names;
1146 ue->info.value.enumerated.names_ptr = 0;
1147
1148 return 0;
1149 }
1150
snd_ctl_elem_user_free(struct snd_kcontrol * kcontrol)1151 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1152 {
1153 struct user_element *ue = kcontrol->private_data;
1154
1155 kfree(ue->tlv_data);
1156 kfree(ue->priv_data);
1157 kfree(ue);
1158 }
1159
snd_ctl_elem_add(struct snd_ctl_file * file,struct snd_ctl_elem_info * info,int replace)1160 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1161 struct snd_ctl_elem_info *info, int replace)
1162 {
1163 struct snd_card *card = file->card;
1164 struct snd_kcontrol kctl, *_kctl;
1165 unsigned int access;
1166 long private_size;
1167 struct user_element *ue;
1168 int idx, err;
1169
1170 if (info->count < 1)
1171 return -EINVAL;
1172 if (!*info->id.name)
1173 return -EINVAL;
1174 if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1175 return -EINVAL;
1176 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
1177 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
1178 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1179 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
1180 info->id.numid = 0;
1181 memset(&kctl, 0, sizeof(kctl));
1182
1183 if (replace) {
1184 err = snd_ctl_remove_user_ctl(file, &info->id);
1185 if (err)
1186 return err;
1187 }
1188
1189 if (card->user_ctl_count >= MAX_USER_CONTROLS)
1190 return -ENOMEM;
1191
1192 memcpy(&kctl.id, &info->id, sizeof(info->id));
1193 kctl.count = info->owner ? info->owner : 1;
1194 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1195 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1196 kctl.info = snd_ctl_elem_user_enum_info;
1197 else
1198 kctl.info = snd_ctl_elem_user_info;
1199 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1200 kctl.get = snd_ctl_elem_user_get;
1201 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1202 kctl.put = snd_ctl_elem_user_put;
1203 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1204 kctl.tlv.c = snd_ctl_elem_user_tlv;
1205 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1206 }
1207 switch (info->type) {
1208 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1209 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1210 private_size = sizeof(long);
1211 if (info->count > 128)
1212 return -EINVAL;
1213 break;
1214 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1215 private_size = sizeof(long long);
1216 if (info->count > 64)
1217 return -EINVAL;
1218 break;
1219 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1220 private_size = sizeof(unsigned int);
1221 if (info->count > 128 || info->value.enumerated.items == 0)
1222 return -EINVAL;
1223 break;
1224 case SNDRV_CTL_ELEM_TYPE_BYTES:
1225 private_size = sizeof(unsigned char);
1226 if (info->count > 512)
1227 return -EINVAL;
1228 break;
1229 case SNDRV_CTL_ELEM_TYPE_IEC958:
1230 private_size = sizeof(struct snd_aes_iec958);
1231 if (info->count != 1)
1232 return -EINVAL;
1233 break;
1234 default:
1235 return -EINVAL;
1236 }
1237 private_size *= info->count;
1238 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
1239 if (ue == NULL)
1240 return -ENOMEM;
1241 ue->card = card;
1242 ue->info = *info;
1243 ue->info.access = 0;
1244 ue->elem_data = (char *)ue + sizeof(*ue);
1245 ue->elem_data_size = private_size;
1246 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1247 err = snd_ctl_elem_init_enum_names(ue);
1248 if (err < 0) {
1249 kfree(ue);
1250 return err;
1251 }
1252 }
1253 kctl.private_free = snd_ctl_elem_user_free;
1254 _kctl = snd_ctl_new(&kctl, access);
1255 if (_kctl == NULL) {
1256 kfree(ue->priv_data);
1257 kfree(ue);
1258 return -ENOMEM;
1259 }
1260 _kctl->private_data = ue;
1261 for (idx = 0; idx < _kctl->count; idx++)
1262 _kctl->vd[idx].owner = file;
1263 err = snd_ctl_add(card, _kctl);
1264 if (err < 0)
1265 return err;
1266
1267 down_write(&card->controls_rwsem);
1268 card->user_ctl_count++;
1269 up_write(&card->controls_rwsem);
1270
1271 return 0;
1272 }
1273
snd_ctl_elem_add_user(struct snd_ctl_file * file,struct snd_ctl_elem_info __user * _info,int replace)1274 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1275 struct snd_ctl_elem_info __user *_info, int replace)
1276 {
1277 struct snd_ctl_elem_info info;
1278 if (copy_from_user(&info, _info, sizeof(info)))
1279 return -EFAULT;
1280 return snd_ctl_elem_add(file, &info, replace);
1281 }
1282
snd_ctl_elem_remove(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)1283 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1284 struct snd_ctl_elem_id __user *_id)
1285 {
1286 struct snd_ctl_elem_id id;
1287
1288 if (copy_from_user(&id, _id, sizeof(id)))
1289 return -EFAULT;
1290 return snd_ctl_remove_user_ctl(file, &id);
1291 }
1292
snd_ctl_subscribe_events(struct snd_ctl_file * file,int __user * ptr)1293 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1294 {
1295 int subscribe;
1296 if (get_user(subscribe, ptr))
1297 return -EFAULT;
1298 if (subscribe < 0) {
1299 subscribe = file->subscribed;
1300 if (put_user(subscribe, ptr))
1301 return -EFAULT;
1302 return 0;
1303 }
1304 if (subscribe) {
1305 file->subscribed = 1;
1306 return 0;
1307 } else if (file->subscribed) {
1308 snd_ctl_empty_read_queue(file);
1309 file->subscribed = 0;
1310 }
1311 return 0;
1312 }
1313
snd_ctl_tlv_ioctl(struct snd_ctl_file * file,struct snd_ctl_tlv __user * _tlv,int op_flag)1314 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1315 struct snd_ctl_tlv __user *_tlv,
1316 int op_flag)
1317 {
1318 struct snd_card *card = file->card;
1319 struct snd_ctl_tlv tlv;
1320 struct snd_kcontrol *kctl;
1321 struct snd_kcontrol_volatile *vd;
1322 unsigned int len;
1323 int err = 0;
1324
1325 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1326 return -EFAULT;
1327 if (tlv.length < sizeof(unsigned int) * 2)
1328 return -EINVAL;
1329 down_read(&card->controls_rwsem);
1330 kctl = snd_ctl_find_numid(card, tlv.numid);
1331 if (kctl == NULL) {
1332 err = -ENOENT;
1333 goto __kctl_end;
1334 }
1335 if (kctl->tlv.p == NULL) {
1336 err = -ENXIO;
1337 goto __kctl_end;
1338 }
1339 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1340 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1341 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1342 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1343 err = -ENXIO;
1344 goto __kctl_end;
1345 }
1346 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1347 if (vd->owner != NULL && vd->owner != file) {
1348 err = -EPERM;
1349 goto __kctl_end;
1350 }
1351 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1352 if (err > 0) {
1353 struct snd_ctl_elem_id id = kctl->id;
1354 up_read(&card->controls_rwsem);
1355 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &id);
1356 return 0;
1357 }
1358 } else {
1359 if (op_flag) {
1360 err = -ENXIO;
1361 goto __kctl_end;
1362 }
1363 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1364 if (tlv.length < len) {
1365 err = -ENOMEM;
1366 goto __kctl_end;
1367 }
1368 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1369 err = -EFAULT;
1370 }
1371 __kctl_end:
1372 up_read(&card->controls_rwsem);
1373 return err;
1374 }
1375
snd_ctl_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1376 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1377 {
1378 struct snd_ctl_file *ctl;
1379 struct snd_card *card;
1380 struct snd_kctl_ioctl *p;
1381 void __user *argp = (void __user *)arg;
1382 int __user *ip = argp;
1383 int err;
1384
1385 ctl = file->private_data;
1386 card = ctl->card;
1387 if (snd_BUG_ON(!card))
1388 return -ENXIO;
1389 switch (cmd) {
1390 case SNDRV_CTL_IOCTL_PVERSION:
1391 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1392 case SNDRV_CTL_IOCTL_CARD_INFO:
1393 return snd_ctl_card_info(card, ctl, cmd, argp);
1394 case SNDRV_CTL_IOCTL_ELEM_LIST:
1395 return snd_ctl_elem_list(card, argp);
1396 case SNDRV_CTL_IOCTL_ELEM_INFO:
1397 return snd_ctl_elem_info_user(ctl, argp);
1398 case SNDRV_CTL_IOCTL_ELEM_READ:
1399 return snd_ctl_elem_read_user(card, argp);
1400 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1401 return snd_ctl_elem_write_user(ctl, argp);
1402 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1403 return snd_ctl_elem_lock(ctl, argp);
1404 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1405 return snd_ctl_elem_unlock(ctl, argp);
1406 case SNDRV_CTL_IOCTL_ELEM_ADD:
1407 return snd_ctl_elem_add_user(ctl, argp, 0);
1408 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1409 return snd_ctl_elem_add_user(ctl, argp, 1);
1410 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1411 return snd_ctl_elem_remove(ctl, argp);
1412 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1413 return snd_ctl_subscribe_events(ctl, ip);
1414 case SNDRV_CTL_IOCTL_TLV_READ:
1415 return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1416 case SNDRV_CTL_IOCTL_TLV_WRITE:
1417 return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1418 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1419 return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1420 case SNDRV_CTL_IOCTL_POWER:
1421 return -ENOPROTOOPT;
1422 case SNDRV_CTL_IOCTL_POWER_STATE:
1423 #ifdef CONFIG_PM
1424 return put_user(card->power_state, ip) ? -EFAULT : 0;
1425 #else
1426 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1427 #endif
1428 }
1429 down_read(&snd_ioctl_rwsem);
1430 list_for_each_entry(p, &snd_control_ioctls, list) {
1431 err = p->fioctl(card, ctl, cmd, arg);
1432 if (err != -ENOIOCTLCMD) {
1433 up_read(&snd_ioctl_rwsem);
1434 return err;
1435 }
1436 }
1437 up_read(&snd_ioctl_rwsem);
1438 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
1439 return -ENOTTY;
1440 }
1441
snd_ctl_read(struct file * file,char __user * buffer,size_t count,loff_t * offset)1442 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1443 size_t count, loff_t * offset)
1444 {
1445 struct snd_ctl_file *ctl;
1446 int err = 0;
1447 ssize_t result = 0;
1448
1449 ctl = file->private_data;
1450 if (snd_BUG_ON(!ctl || !ctl->card))
1451 return -ENXIO;
1452 if (!ctl->subscribed)
1453 return -EBADFD;
1454 if (count < sizeof(struct snd_ctl_event))
1455 return -EINVAL;
1456 spin_lock_irq(&ctl->read_lock);
1457 while (count >= sizeof(struct snd_ctl_event)) {
1458 struct snd_ctl_event ev;
1459 struct snd_kctl_event *kev;
1460 while (list_empty(&ctl->events)) {
1461 wait_queue_t wait;
1462 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1463 err = -EAGAIN;
1464 goto __end_lock;
1465 }
1466 init_waitqueue_entry(&wait, current);
1467 add_wait_queue(&ctl->change_sleep, &wait);
1468 set_current_state(TASK_INTERRUPTIBLE);
1469 spin_unlock_irq(&ctl->read_lock);
1470 schedule();
1471 remove_wait_queue(&ctl->change_sleep, &wait);
1472 if (ctl->card->shutdown)
1473 return -ENODEV;
1474 if (signal_pending(current))
1475 return -ERESTARTSYS;
1476 spin_lock_irq(&ctl->read_lock);
1477 }
1478 kev = snd_kctl_event(ctl->events.next);
1479 ev.type = SNDRV_CTL_EVENT_ELEM;
1480 ev.data.elem.mask = kev->mask;
1481 ev.data.elem.id = kev->id;
1482 list_del(&kev->list);
1483 spin_unlock_irq(&ctl->read_lock);
1484 kfree(kev);
1485 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1486 err = -EFAULT;
1487 goto __end;
1488 }
1489 spin_lock_irq(&ctl->read_lock);
1490 buffer += sizeof(struct snd_ctl_event);
1491 count -= sizeof(struct snd_ctl_event);
1492 result += sizeof(struct snd_ctl_event);
1493 }
1494 __end_lock:
1495 spin_unlock_irq(&ctl->read_lock);
1496 __end:
1497 return result > 0 ? result : err;
1498 }
1499
snd_ctl_poll(struct file * file,poll_table * wait)1500 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1501 {
1502 unsigned int mask;
1503 struct snd_ctl_file *ctl;
1504
1505 ctl = file->private_data;
1506 if (!ctl->subscribed)
1507 return 0;
1508 poll_wait(file, &ctl->change_sleep, wait);
1509
1510 mask = 0;
1511 if (!list_empty(&ctl->events))
1512 mask |= POLLIN | POLLRDNORM;
1513
1514 return mask;
1515 }
1516
1517 /*
1518 * register the device-specific control-ioctls.
1519 * called from each device manager like pcm.c, hwdep.c, etc.
1520 */
_snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn,struct list_head * lists)1521 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1522 {
1523 struct snd_kctl_ioctl *pn;
1524
1525 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1526 if (pn == NULL)
1527 return -ENOMEM;
1528 pn->fioctl = fcn;
1529 down_write(&snd_ioctl_rwsem);
1530 list_add_tail(&pn->list, lists);
1531 up_write(&snd_ioctl_rwsem);
1532 return 0;
1533 }
1534
snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)1535 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1536 {
1537 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1538 }
1539
1540 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1541
1542 #ifdef CONFIG_COMPAT
snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)1543 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1544 {
1545 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1546 }
1547
1548 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1549 #endif
1550
1551 /*
1552 * de-register the device-specific control-ioctls.
1553 */
_snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,struct list_head * lists)1554 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1555 struct list_head *lists)
1556 {
1557 struct snd_kctl_ioctl *p;
1558
1559 if (snd_BUG_ON(!fcn))
1560 return -EINVAL;
1561 down_write(&snd_ioctl_rwsem);
1562 list_for_each_entry(p, lists, list) {
1563 if (p->fioctl == fcn) {
1564 list_del(&p->list);
1565 up_write(&snd_ioctl_rwsem);
1566 kfree(p);
1567 return 0;
1568 }
1569 }
1570 up_write(&snd_ioctl_rwsem);
1571 snd_BUG();
1572 return -EINVAL;
1573 }
1574
snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)1575 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1576 {
1577 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1578 }
1579
1580 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1581
1582 #ifdef CONFIG_COMPAT
snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)1583 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1584 {
1585 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1586 }
1587
1588 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1589 #endif
1590
snd_ctl_fasync(int fd,struct file * file,int on)1591 static int snd_ctl_fasync(int fd, struct file * file, int on)
1592 {
1593 struct snd_ctl_file *ctl;
1594
1595 ctl = file->private_data;
1596 return fasync_helper(fd, file, on, &ctl->fasync);
1597 }
1598
1599 /*
1600 * ioctl32 compat
1601 */
1602 #ifdef CONFIG_COMPAT
1603 #include "control_compat.c"
1604 #else
1605 #define snd_ctl_ioctl_compat NULL
1606 #endif
1607
1608 /*
1609 * INIT PART
1610 */
1611
1612 static const struct file_operations snd_ctl_f_ops =
1613 {
1614 .owner = THIS_MODULE,
1615 .read = snd_ctl_read,
1616 .open = snd_ctl_open,
1617 .release = snd_ctl_release,
1618 .llseek = no_llseek,
1619 .poll = snd_ctl_poll,
1620 .unlocked_ioctl = snd_ctl_ioctl,
1621 .compat_ioctl = snd_ctl_ioctl_compat,
1622 .fasync = snd_ctl_fasync,
1623 };
1624
1625 /*
1626 * registration of the control device
1627 */
snd_ctl_dev_register(struct snd_device * device)1628 static int snd_ctl_dev_register(struct snd_device *device)
1629 {
1630 struct snd_card *card = device->device_data;
1631 int err, cardnum;
1632 char name[16];
1633
1634 if (snd_BUG_ON(!card))
1635 return -ENXIO;
1636 cardnum = card->number;
1637 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1638 return -ENXIO;
1639 sprintf(name, "controlC%i", cardnum);
1640 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1641 &snd_ctl_f_ops, card, name)) < 0)
1642 return err;
1643 return 0;
1644 }
1645
1646 /*
1647 * disconnection of the control device
1648 */
snd_ctl_dev_disconnect(struct snd_device * device)1649 static int snd_ctl_dev_disconnect(struct snd_device *device)
1650 {
1651 struct snd_card *card = device->device_data;
1652 struct snd_ctl_file *ctl;
1653 int err, cardnum;
1654
1655 if (snd_BUG_ON(!card))
1656 return -ENXIO;
1657 cardnum = card->number;
1658 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1659 return -ENXIO;
1660
1661 read_lock(&card->ctl_files_rwlock);
1662 list_for_each_entry(ctl, &card->ctl_files, list) {
1663 wake_up(&ctl->change_sleep);
1664 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1665 }
1666 read_unlock(&card->ctl_files_rwlock);
1667
1668 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1669 card, -1)) < 0)
1670 return err;
1671 return 0;
1672 }
1673
1674 /*
1675 * free all controls
1676 */
snd_ctl_dev_free(struct snd_device * device)1677 static int snd_ctl_dev_free(struct snd_device *device)
1678 {
1679 struct snd_card *card = device->device_data;
1680 struct snd_kcontrol *control;
1681
1682 down_write(&card->controls_rwsem);
1683 while (!list_empty(&card->controls)) {
1684 control = snd_kcontrol(card->controls.next);
1685 snd_ctl_remove(card, control);
1686 }
1687 up_write(&card->controls_rwsem);
1688 return 0;
1689 }
1690
1691 /*
1692 * create control core:
1693 * called from init.c
1694 */
snd_ctl_create(struct snd_card * card)1695 int snd_ctl_create(struct snd_card *card)
1696 {
1697 static struct snd_device_ops ops = {
1698 .dev_free = snd_ctl_dev_free,
1699 .dev_register = snd_ctl_dev_register,
1700 .dev_disconnect = snd_ctl_dev_disconnect,
1701 };
1702
1703 if (snd_BUG_ON(!card))
1704 return -ENXIO;
1705 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1706 }
1707
1708 /*
1709 * Frequently used control callbacks/helpers
1710 */
snd_ctl_boolean_mono_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1711 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_info *uinfo)
1713 {
1714 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1715 uinfo->count = 1;
1716 uinfo->value.integer.min = 0;
1717 uinfo->value.integer.max = 1;
1718 return 0;
1719 }
1720
1721 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1722
snd_ctl_boolean_stereo_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1723 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1724 struct snd_ctl_elem_info *uinfo)
1725 {
1726 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1727 uinfo->count = 2;
1728 uinfo->value.integer.min = 0;
1729 uinfo->value.integer.max = 1;
1730 return 0;
1731 }
1732
1733 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
1734
1735 /**
1736 * snd_ctl_enum_info - fills the info structure for an enumerated control
1737 * @info: the structure to be filled
1738 * @channels: the number of the control's channels; often one
1739 * @items: the number of control values; also the size of @names
1740 * @names: an array containing the names of all control values
1741 *
1742 * Sets all required fields in @info to their appropriate values.
1743 * If the control's accessibility is not the default (readable and writable),
1744 * the caller has to fill @info->access.
1745 *
1746 * Return: Zero.
1747 */
snd_ctl_enum_info(struct snd_ctl_elem_info * info,unsigned int channels,unsigned int items,const char * const names[])1748 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1749 unsigned int items, const char *const names[])
1750 {
1751 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1752 info->count = channels;
1753 info->value.enumerated.items = items;
1754 if (info->value.enumerated.item >= items)
1755 info->value.enumerated.item = items - 1;
1756 strlcpy(info->value.enumerated.name,
1757 names[info->value.enumerated.item],
1758 sizeof(info->value.enumerated.name));
1759 return 0;
1760 }
1761 EXPORT_SYMBOL(snd_ctl_enum_info);
1762