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