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