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