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