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