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