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