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