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