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