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