8a5cedb0a4bea3ff4d9ba3d43484be19d6ce23d9
[linux-2.6-microblaze.git] / sound / core / control.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Routines for driver control interface
4  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5  */
6
7 #include <linux/threads.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12 #include <linux/time.h>
13 #include <linux/mm.h>
14 #include <linux/math64.h>
15 #include <linux/sched/signal.h>
16 #include <sound/core.h>
17 #include <sound/minors.h>
18 #include <sound/info.h>
19 #include <sound/control.h>
20
21 /* max number of user-defined controls */
22 #define MAX_USER_CONTROLS       32
23 #define MAX_CONTROL_COUNT       1028
24
25 struct snd_kctl_ioctl {
26         struct list_head list;          /* list of all ioctls */
27         snd_kctl_ioctl_func_t fioctl;
28 };
29
30 static DECLARE_RWSEM(snd_ioctl_rwsem);
31 static LIST_HEAD(snd_control_ioctls);
32 #ifdef CONFIG_COMPAT
33 static LIST_HEAD(snd_control_compat_ioctls);
34 #endif
35
36 static int snd_ctl_open(struct inode *inode, struct file *file)
37 {
38         unsigned long flags;
39         struct snd_card *card;
40         struct snd_ctl_file *ctl;
41         int i, err;
42
43         err = stream_open(inode, file);
44         if (err < 0)
45                 return err;
46
47         card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
48         if (!card) {
49                 err = -ENODEV;
50                 goto __error1;
51         }
52         err = snd_card_file_add(card, file);
53         if (err < 0) {
54                 err = -ENODEV;
55                 goto __error1;
56         }
57         if (!try_module_get(card->module)) {
58                 err = -EFAULT;
59                 goto __error2;
60         }
61         ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
62         if (ctl == NULL) {
63                 err = -ENOMEM;
64                 goto __error;
65         }
66         INIT_LIST_HEAD(&ctl->events);
67         init_waitqueue_head(&ctl->change_sleep);
68         spin_lock_init(&ctl->read_lock);
69         ctl->card = card;
70         for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
71                 ctl->preferred_subdevice[i] = -1;
72         ctl->pid = get_pid(task_pid(current));
73         file->private_data = ctl;
74         write_lock_irqsave(&card->ctl_files_rwlock, flags);
75         list_add_tail(&ctl->list, &card->ctl_files);
76         write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
77         snd_card_unref(card);
78         return 0;
79
80       __error:
81         module_put(card->module);
82       __error2:
83         snd_card_file_remove(card, file);
84       __error1:
85         if (card)
86                 snd_card_unref(card);
87         return err;
88 }
89
90 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
91 {
92         unsigned long flags;
93         struct snd_kctl_event *cread;
94
95         spin_lock_irqsave(&ctl->read_lock, flags);
96         while (!list_empty(&ctl->events)) {
97                 cread = snd_kctl_event(ctl->events.next);
98                 list_del(&cread->list);
99                 kfree(cread);
100         }
101         spin_unlock_irqrestore(&ctl->read_lock, flags);
102 }
103
104 static int snd_ctl_release(struct inode *inode, struct file *file)
105 {
106         unsigned long flags;
107         struct snd_card *card;
108         struct snd_ctl_file *ctl;
109         struct snd_kcontrol *control;
110         unsigned int idx;
111
112         ctl = file->private_data;
113         file->private_data = NULL;
114         card = ctl->card;
115         write_lock_irqsave(&card->ctl_files_rwlock, flags);
116         list_del(&ctl->list);
117         write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
118         down_write(&card->controls_rwsem);
119         list_for_each_entry(control, &card->controls, list)
120                 for (idx = 0; idx < control->count; idx++)
121                         if (control->vd[idx].owner == ctl)
122                                 control->vd[idx].owner = NULL;
123         up_write(&card->controls_rwsem);
124         snd_ctl_empty_read_queue(ctl);
125         put_pid(ctl->pid);
126         kfree(ctl);
127         module_put(card->module);
128         snd_card_file_remove(card, file);
129         return 0;
130 }
131
132 /**
133  * snd_ctl_notify - Send notification to user-space for a control change
134  * @card: the card to send notification
135  * @mask: the event mask, SNDRV_CTL_EVENT_*
136  * @id: the ctl element id to send notification
137  *
138  * This function adds an event record with the given id and mask, appends
139  * to the list and wakes up the user-space for notification.  This can be
140  * called in the atomic context.
141  */
142 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
143                     struct snd_ctl_elem_id *id)
144 {
145         unsigned long flags;
146         struct snd_ctl_file *ctl;
147         struct snd_kctl_event *ev;
148
149         if (snd_BUG_ON(!card || !id))
150                 return;
151         if (card->shutdown)
152                 return;
153         read_lock_irqsave(&card->ctl_files_rwlock, flags);
154 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
155         card->mixer_oss_change_count++;
156 #endif
157         list_for_each_entry(ctl, &card->ctl_files, list) {
158                 if (!ctl->subscribed)
159                         continue;
160                 spin_lock(&ctl->read_lock);
161                 list_for_each_entry(ev, &ctl->events, list) {
162                         if (ev->id.numid == id->numid) {
163                                 ev->mask |= mask;
164                                 goto _found;
165                         }
166                 }
167                 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
168                 if (ev) {
169                         ev->id = *id;
170                         ev->mask = mask;
171                         list_add_tail(&ev->list, &ctl->events);
172                 } else {
173                         dev_err(card->dev, "No memory available to allocate event\n");
174                 }
175         _found:
176                 wake_up(&ctl->change_sleep);
177                 spin_unlock(&ctl->read_lock);
178                 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
179         }
180         read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
181 }
182 EXPORT_SYMBOL(snd_ctl_notify);
183
184 /**
185  * snd_ctl_notify_one - Send notification to user-space for a control change
186  * @card: the card to send notification
187  * @mask: the event mask, SNDRV_CTL_EVENT_*
188  * @kctl: the pointer with the control instance
189  * @ioff: the additional offset to the control index
190  *
191  * This function calls snd_ctl_notify() and does additional jobs
192  * like LED state changes.
193  */
194 void snd_ctl_notify_one(struct snd_card *card, unsigned int mask,
195                         struct snd_kcontrol *kctl, unsigned int ioff)
196 {
197         struct snd_ctl_elem_id id = kctl->id;
198
199         id.index += ioff;
200         id.numid += ioff;
201         snd_ctl_notify(card, mask, &id);
202 }
203 EXPORT_SYMBOL(snd_ctl_notify_one);
204
205 /**
206  * snd_ctl_new - create a new control instance with some elements
207  * @kctl: the pointer to store new control instance
208  * @count: the number of elements in this control
209  * @access: the default access flags for elements in this control
210  * @file: given when locking these elements
211  *
212  * Allocates a memory object for a new control instance. The instance has
213  * elements as many as the given number (@count). Each element has given
214  * access permissions (@access). Each element is locked when @file is given.
215  *
216  * Return: 0 on success, error code on failure
217  */
218 static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
219                        unsigned int access, struct snd_ctl_file *file)
220 {
221         unsigned int idx;
222
223         if (count == 0 || count > MAX_CONTROL_COUNT)
224                 return -EINVAL;
225
226         *kctl = kzalloc(struct_size(*kctl, vd, count), GFP_KERNEL);
227         if (!*kctl)
228                 return -ENOMEM;
229
230         for (idx = 0; idx < count; idx++) {
231                 (*kctl)->vd[idx].access = access;
232                 (*kctl)->vd[idx].owner = file;
233         }
234         (*kctl)->count = count;
235
236         return 0;
237 }
238
239 /**
240  * snd_ctl_new1 - create a control instance from the template
241  * @ncontrol: the initialization record
242  * @private_data: the private data to set
243  *
244  * Allocates a new struct snd_kcontrol instance and initialize from the given
245  * template.  When the access field of ncontrol is 0, it's assumed as
246  * READWRITE access. When the count field is 0, it's assumes as one.
247  *
248  * Return: The pointer of the newly generated instance, or %NULL on failure.
249  */
250 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
251                                   void *private_data)
252 {
253         struct snd_kcontrol *kctl;
254         unsigned int count;
255         unsigned int access;
256         int err;
257
258         if (snd_BUG_ON(!ncontrol || !ncontrol->info))
259                 return NULL;
260
261         count = ncontrol->count;
262         if (count == 0)
263                 count = 1;
264
265         access = ncontrol->access;
266         if (access == 0)
267                 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
268         access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
269                    SNDRV_CTL_ELEM_ACCESS_VOLATILE |
270                    SNDRV_CTL_ELEM_ACCESS_INACTIVE |
271                    SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
272                    SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
273                    SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
274                    SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
275
276         err = snd_ctl_new(&kctl, count, access, NULL);
277         if (err < 0)
278                 return NULL;
279
280         /* The 'numid' member is decided when calling snd_ctl_add(). */
281         kctl->id.iface = ncontrol->iface;
282         kctl->id.device = ncontrol->device;
283         kctl->id.subdevice = ncontrol->subdevice;
284         if (ncontrol->name) {
285                 strscpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
286                 if (strcmp(ncontrol->name, kctl->id.name) != 0)
287                         pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
288                                 ncontrol->name, kctl->id.name);
289         }
290         kctl->id.index = ncontrol->index;
291
292         kctl->info = ncontrol->info;
293         kctl->get = ncontrol->get;
294         kctl->put = ncontrol->put;
295         kctl->tlv.p = ncontrol->tlv.p;
296
297         kctl->private_value = ncontrol->private_value;
298         kctl->private_data = private_data;
299
300         return kctl;
301 }
302 EXPORT_SYMBOL(snd_ctl_new1);
303
304 /**
305  * snd_ctl_free_one - release the control instance
306  * @kcontrol: the control instance
307  *
308  * Releases the control instance created via snd_ctl_new()
309  * or snd_ctl_new1().
310  * Don't call this after the control was added to the card.
311  */
312 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
313 {
314         if (kcontrol) {
315                 if (kcontrol->private_free)
316                         kcontrol->private_free(kcontrol);
317                 kfree(kcontrol);
318         }
319 }
320 EXPORT_SYMBOL(snd_ctl_free_one);
321
322 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
323                                           unsigned int count)
324 {
325         struct snd_kcontrol *kctl;
326
327         /* Make sure that the ids assigned to the control do not wrap around */
328         if (card->last_numid >= UINT_MAX - count)
329                 card->last_numid = 0;
330
331         list_for_each_entry(kctl, &card->controls, list) {
332                 if (kctl->id.numid < card->last_numid + 1 + count &&
333                     kctl->id.numid + kctl->count > card->last_numid + 1) {
334                         card->last_numid = kctl->id.numid + kctl->count - 1;
335                         return true;
336                 }
337         }
338         return false;
339 }
340
341 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
342 {
343         unsigned int iter = 100000;
344
345         while (snd_ctl_remove_numid_conflict(card, count)) {
346                 if (--iter == 0) {
347                         /* this situation is very unlikely */
348                         dev_err(card->dev, "unable to allocate new control numid\n");
349                         return -ENOMEM;
350                 }
351         }
352         return 0;
353 }
354
355 enum snd_ctl_add_mode {
356         CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
357 };
358
359 /* add/replace a new kcontrol object; call with card->controls_rwsem locked */
360 static int __snd_ctl_add_replace(struct snd_card *card,
361                                  struct snd_kcontrol *kcontrol,
362                                  enum snd_ctl_add_mode mode)
363 {
364         struct snd_ctl_elem_id id;
365         unsigned int idx;
366         struct snd_kcontrol *old;
367         int err;
368
369         id = kcontrol->id;
370         if (id.index > UINT_MAX - kcontrol->count)
371                 return -EINVAL;
372
373         old = snd_ctl_find_id(card, &id);
374         if (!old) {
375                 if (mode == CTL_REPLACE)
376                         return -EINVAL;
377         } else {
378                 if (mode == CTL_ADD_EXCLUSIVE) {
379                         dev_err(card->dev,
380                                 "control %i:%i:%i:%s:%i is already present\n",
381                                 id.iface, id.device, id.subdevice, id.name,
382                                 id.index);
383                         return -EBUSY;
384                 }
385
386                 err = snd_ctl_remove(card, old);
387                 if (err < 0)
388                         return err;
389         }
390
391         if (snd_ctl_find_hole(card, kcontrol->count) < 0)
392                 return -ENOMEM;
393
394         list_add_tail(&kcontrol->list, &card->controls);
395         card->controls_count += kcontrol->count;
396         kcontrol->id.numid = card->last_numid + 1;
397         card->last_numid += kcontrol->count;
398
399         for (idx = 0; idx < kcontrol->count; idx++)
400                 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_ADD, kcontrol, idx);
401
402         return 0;
403 }
404
405 static int snd_ctl_add_replace(struct snd_card *card,
406                                struct snd_kcontrol *kcontrol,
407                                enum snd_ctl_add_mode mode)
408 {
409         int err = -EINVAL;
410
411         if (! kcontrol)
412                 return err;
413         if (snd_BUG_ON(!card || !kcontrol->info))
414                 goto error;
415
416         down_write(&card->controls_rwsem);
417         err = __snd_ctl_add_replace(card, kcontrol, mode);
418         up_write(&card->controls_rwsem);
419         if (err < 0)
420                 goto error;
421         return 0;
422
423  error:
424         snd_ctl_free_one(kcontrol);
425         return err;
426 }
427
428 /**
429  * snd_ctl_add - add the control instance to the card
430  * @card: the card instance
431  * @kcontrol: the control instance to add
432  *
433  * Adds the control instance created via snd_ctl_new() or
434  * snd_ctl_new1() to the given card. Assigns also an unique
435  * numid used for fast search.
436  *
437  * It frees automatically the control which cannot be added.
438  *
439  * Return: Zero if successful, or a negative error code on failure.
440  *
441  */
442 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
443 {
444         return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
445 }
446 EXPORT_SYMBOL(snd_ctl_add);
447
448 /**
449  * snd_ctl_replace - replace the control instance of the card
450  * @card: the card instance
451  * @kcontrol: the control instance to replace
452  * @add_on_replace: add the control if not already added
453  *
454  * Replaces the given control.  If the given control does not exist
455  * and the add_on_replace flag is set, the control is added.  If the
456  * control exists, it is destroyed first.
457  *
458  * It frees automatically the control which cannot be added or replaced.
459  *
460  * Return: Zero if successful, or a negative error code on failure.
461  */
462 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
463                     bool add_on_replace)
464 {
465         return snd_ctl_add_replace(card, kcontrol,
466                                    add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
467 }
468 EXPORT_SYMBOL(snd_ctl_replace);
469
470 /**
471  * snd_ctl_remove - remove the control from the card and release it
472  * @card: the card instance
473  * @kcontrol: the control instance to remove
474  *
475  * Removes the control from the card and then releases the instance.
476  * You don't need to call snd_ctl_free_one(). You must be in
477  * the write lock - down_write(&card->controls_rwsem).
478  *
479  * Return: 0 if successful, or a negative error code on failure.
480  */
481 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
482 {
483         unsigned int idx;
484
485         if (snd_BUG_ON(!card || !kcontrol))
486                 return -EINVAL;
487         list_del(&kcontrol->list);
488         card->controls_count -= kcontrol->count;
489         for (idx = 0; idx < kcontrol->count; idx++)
490                 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_REMOVE, kcontrol, idx);
491         snd_ctl_free_one(kcontrol);
492         return 0;
493 }
494 EXPORT_SYMBOL(snd_ctl_remove);
495
496 /**
497  * snd_ctl_remove_id - remove the control of the given id and release it
498  * @card: the card instance
499  * @id: the control id to remove
500  *
501  * Finds the control instance with the given id, removes it from the
502  * card list and releases it.
503  *
504  * Return: 0 if successful, or a negative error code on failure.
505  */
506 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
507 {
508         struct snd_kcontrol *kctl;
509         int ret;
510
511         down_write(&card->controls_rwsem);
512         kctl = snd_ctl_find_id(card, id);
513         if (kctl == NULL) {
514                 up_write(&card->controls_rwsem);
515                 return -ENOENT;
516         }
517         ret = snd_ctl_remove(card, kctl);
518         up_write(&card->controls_rwsem);
519         return ret;
520 }
521 EXPORT_SYMBOL(snd_ctl_remove_id);
522
523 /**
524  * snd_ctl_remove_user_ctl - remove and release the unlocked user control
525  * @file: active control handle
526  * @id: the control id to remove
527  *
528  * Finds the control instance with the given id, removes it from the
529  * card list and releases it.
530  *
531  * Return: 0 if successful, or a negative error code on failure.
532  */
533 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
534                                    struct snd_ctl_elem_id *id)
535 {
536         struct snd_card *card = file->card;
537         struct snd_kcontrol *kctl;
538         int idx, ret;
539
540         down_write(&card->controls_rwsem);
541         kctl = snd_ctl_find_id(card, id);
542         if (kctl == NULL) {
543                 ret = -ENOENT;
544                 goto error;
545         }
546         if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
547                 ret = -EINVAL;
548                 goto error;
549         }
550         for (idx = 0; idx < kctl->count; idx++)
551                 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
552                         ret = -EBUSY;
553                         goto error;
554                 }
555         ret = snd_ctl_remove(card, kctl);
556         if (ret < 0)
557                 goto error;
558         card->user_ctl_count--;
559 error:
560         up_write(&card->controls_rwsem);
561         return ret;
562 }
563
564 /**
565  * snd_ctl_activate_id - activate/inactivate the control of the given id
566  * @card: the card instance
567  * @id: the control id to activate/inactivate
568  * @active: non-zero to activate
569  *
570  * Finds the control instance with the given id, and activate or
571  * inactivate the control together with notification, if changed.
572  * The given ID data is filled with full information.
573  *
574  * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
575  */
576 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
577                         int active)
578 {
579         struct snd_kcontrol *kctl;
580         struct snd_kcontrol_volatile *vd;
581         unsigned int index_offset;
582         int ret;
583
584         down_write(&card->controls_rwsem);
585         kctl = snd_ctl_find_id(card, id);
586         if (kctl == NULL) {
587                 ret = -ENOENT;
588                 goto unlock;
589         }
590         index_offset = snd_ctl_get_ioff(kctl, id);
591         vd = &kctl->vd[index_offset];
592         ret = 0;
593         if (active) {
594                 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
595                         goto unlock;
596                 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
597         } else {
598                 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
599                         goto unlock;
600                 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
601         }
602         snd_ctl_build_ioff(id, kctl, index_offset);
603         downgrade_write(&card->controls_rwsem);
604         snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, index_offset);
605         up_read(&card->controls_rwsem);
606         return 1;
607
608  unlock:
609         up_write(&card->controls_rwsem);
610         return ret;
611 }
612 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
613
614 /**
615  * snd_ctl_rename_id - replace the id of a control on the card
616  * @card: the card instance
617  * @src_id: the old id
618  * @dst_id: the new id
619  *
620  * Finds the control with the old id from the card, and replaces the
621  * id with the new one.
622  *
623  * Return: Zero if successful, or a negative error code on failure.
624  */
625 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
626                       struct snd_ctl_elem_id *dst_id)
627 {
628         struct snd_kcontrol *kctl;
629
630         down_write(&card->controls_rwsem);
631         kctl = snd_ctl_find_id(card, src_id);
632         if (kctl == NULL) {
633                 up_write(&card->controls_rwsem);
634                 return -ENOENT;
635         }
636         kctl->id = *dst_id;
637         kctl->id.numid = card->last_numid + 1;
638         card->last_numid += kctl->count;
639         up_write(&card->controls_rwsem);
640         return 0;
641 }
642 EXPORT_SYMBOL(snd_ctl_rename_id);
643
644 /**
645  * snd_ctl_find_numid - find the control instance with the given number-id
646  * @card: the card instance
647  * @numid: the number-id to search
648  *
649  * Finds the control instance with the given number-id from the card.
650  *
651  * The caller must down card->controls_rwsem before calling this function
652  * (if the race condition can happen).
653  *
654  * Return: The pointer of the instance if found, or %NULL if not.
655  *
656  */
657 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
658 {
659         struct snd_kcontrol *kctl;
660
661         if (snd_BUG_ON(!card || !numid))
662                 return NULL;
663         list_for_each_entry(kctl, &card->controls, list) {
664                 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
665                         return kctl;
666         }
667         return NULL;
668 }
669 EXPORT_SYMBOL(snd_ctl_find_numid);
670
671 /**
672  * snd_ctl_find_id - find the control instance with the given id
673  * @card: the card instance
674  * @id: the id to search
675  *
676  * Finds the control instance with the given id from the card.
677  *
678  * The caller must down card->controls_rwsem before calling this function
679  * (if the race condition can happen).
680  *
681  * Return: The pointer of the instance if found, or %NULL if not.
682  *
683  */
684 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
685                                      struct snd_ctl_elem_id *id)
686 {
687         struct snd_kcontrol *kctl;
688
689         if (snd_BUG_ON(!card || !id))
690                 return NULL;
691         if (id->numid != 0)
692                 return snd_ctl_find_numid(card, id->numid);
693         list_for_each_entry(kctl, &card->controls, list) {
694                 if (kctl->id.iface != id->iface)
695                         continue;
696                 if (kctl->id.device != id->device)
697                         continue;
698                 if (kctl->id.subdevice != id->subdevice)
699                         continue;
700                 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
701                         continue;
702                 if (kctl->id.index > id->index)
703                         continue;
704                 if (kctl->id.index + kctl->count <= id->index)
705                         continue;
706                 return kctl;
707         }
708         return NULL;
709 }
710 EXPORT_SYMBOL(snd_ctl_find_id);
711
712 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
713                              unsigned int cmd, void __user *arg)
714 {
715         struct snd_ctl_card_info *info;
716
717         info = kzalloc(sizeof(*info), GFP_KERNEL);
718         if (! info)
719                 return -ENOMEM;
720         down_read(&snd_ioctl_rwsem);
721         info->card = card->number;
722         strscpy(info->id, card->id, sizeof(info->id));
723         strscpy(info->driver, card->driver, sizeof(info->driver));
724         strscpy(info->name, card->shortname, sizeof(info->name));
725         strscpy(info->longname, card->longname, sizeof(info->longname));
726         strscpy(info->mixername, card->mixername, sizeof(info->mixername));
727         strscpy(info->components, card->components, sizeof(info->components));
728         up_read(&snd_ioctl_rwsem);
729         if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
730                 kfree(info);
731                 return -EFAULT;
732         }
733         kfree(info);
734         return 0;
735 }
736
737 static int snd_ctl_elem_list(struct snd_card *card,
738                              struct snd_ctl_elem_list *list)
739 {
740         struct snd_kcontrol *kctl;
741         struct snd_ctl_elem_id id;
742         unsigned int offset, space, jidx;
743         int err = 0;
744
745         offset = list->offset;
746         space = list->space;
747
748         down_read(&card->controls_rwsem);
749         list->count = card->controls_count;
750         list->used = 0;
751         if (space > 0) {
752                 list_for_each_entry(kctl, &card->controls, list) {
753                         if (offset >= kctl->count) {
754                                 offset -= kctl->count;
755                                 continue;
756                         }
757                         for (jidx = offset; jidx < kctl->count; jidx++) {
758                                 snd_ctl_build_ioff(&id, kctl, jidx);
759                                 if (copy_to_user(list->pids + list->used, &id,
760                                                  sizeof(id))) {
761                                         err = -EFAULT;
762                                         goto out;
763                                 }
764                                 list->used++;
765                                 if (!--space)
766                                         goto out;
767                         }
768                         offset = 0;
769                 }
770         }
771  out:
772         up_read(&card->controls_rwsem);
773         return err;
774 }
775
776 static int snd_ctl_elem_list_user(struct snd_card *card,
777                                   struct snd_ctl_elem_list __user *_list)
778 {
779         struct snd_ctl_elem_list list;
780         int err;
781
782         if (copy_from_user(&list, _list, sizeof(list)))
783                 return -EFAULT;
784         err = snd_ctl_elem_list(card, &list);
785         if (err)
786                 return err;
787         if (copy_to_user(_list, &list, sizeof(list)))
788                 return -EFAULT;
789
790         return 0;
791 }
792
793 /* Check whether the given kctl info is valid */
794 static int snd_ctl_check_elem_info(struct snd_card *card,
795                                    const struct snd_ctl_elem_info *info)
796 {
797         static const unsigned int max_value_counts[] = {
798                 [SNDRV_CTL_ELEM_TYPE_BOOLEAN]   = 128,
799                 [SNDRV_CTL_ELEM_TYPE_INTEGER]   = 128,
800                 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
801                 [SNDRV_CTL_ELEM_TYPE_BYTES]     = 512,
802                 [SNDRV_CTL_ELEM_TYPE_IEC958]    = 1,
803                 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
804         };
805
806         if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
807             info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64) {
808                 if (card)
809                         dev_err(card->dev,
810                                 "control %i:%i:%i:%s:%i: invalid type %d\n",
811                                 info->id.iface, info->id.device,
812                                 info->id.subdevice, info->id.name,
813                                 info->id.index, info->type);
814                 return -EINVAL;
815         }
816         if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
817             info->value.enumerated.items == 0) {
818                 if (card)
819                         dev_err(card->dev,
820                                 "control %i:%i:%i:%s:%i: zero enum items\n",
821                                 info->id.iface, info->id.device,
822                                 info->id.subdevice, info->id.name,
823                                 info->id.index);
824                 return -EINVAL;
825         }
826         if (info->count > max_value_counts[info->type]) {
827                 if (card)
828                         dev_err(card->dev,
829                                 "control %i:%i:%i:%s:%i: invalid count %d\n",
830                                 info->id.iface, info->id.device,
831                                 info->id.subdevice, info->id.name,
832                                 info->id.index, info->count);
833                 return -EINVAL;
834         }
835
836         return 0;
837 }
838
839 /* The capacity of struct snd_ctl_elem_value.value.*/
840 static const unsigned int value_sizes[] = {
841         [SNDRV_CTL_ELEM_TYPE_BOOLEAN]   = sizeof(long),
842         [SNDRV_CTL_ELEM_TYPE_INTEGER]   = sizeof(long),
843         [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
844         [SNDRV_CTL_ELEM_TYPE_BYTES]     = sizeof(unsigned char),
845         [SNDRV_CTL_ELEM_TYPE_IEC958]    = sizeof(struct snd_aes_iec958),
846         [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
847 };
848
849 #ifdef CONFIG_SND_CTL_VALIDATION
850 /* fill the remaining snd_ctl_elem_value data with the given pattern */
851 static void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
852                                       struct snd_ctl_elem_info *info,
853                                       u32 pattern)
854 {
855         size_t offset = value_sizes[info->type] * info->count;
856
857         offset = DIV_ROUND_UP(offset, sizeof(u32));
858         memset32((u32 *)control->value.bytes.data + offset, pattern,
859                  sizeof(control->value) / sizeof(u32) - offset);
860 }
861
862 /* check whether the given integer ctl value is valid */
863 static int sanity_check_int_value(struct snd_card *card,
864                                   const struct snd_ctl_elem_value *control,
865                                   const struct snd_ctl_elem_info *info,
866                                   int i)
867 {
868         long long lval, lmin, lmax, lstep;
869         u64 rem;
870
871         switch (info->type) {
872         default:
873         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
874                 lval = control->value.integer.value[i];
875                 lmin = 0;
876                 lmax = 1;
877                 lstep = 0;
878                 break;
879         case SNDRV_CTL_ELEM_TYPE_INTEGER:
880                 lval = control->value.integer.value[i];
881                 lmin = info->value.integer.min;
882                 lmax = info->value.integer.max;
883                 lstep = info->value.integer.step;
884                 break;
885         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
886                 lval = control->value.integer64.value[i];
887                 lmin = info->value.integer64.min;
888                 lmax = info->value.integer64.max;
889                 lstep = info->value.integer64.step;
890                 break;
891         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
892                 lval = control->value.enumerated.item[i];
893                 lmin = 0;
894                 lmax = info->value.enumerated.items - 1;
895                 lstep = 0;
896                 break;
897         }
898
899         if (lval < lmin || lval > lmax) {
900                 dev_err(card->dev,
901                         "control %i:%i:%i:%s:%i: value out of range %lld (%lld/%lld) at count %i\n",
902                         control->id.iface, control->id.device,
903                         control->id.subdevice, control->id.name,
904                         control->id.index, lval, lmin, lmax, i);
905                 return -EINVAL;
906         }
907         if (lstep) {
908                 div64_u64_rem(lval, lstep, &rem);
909                 if (rem) {
910                         dev_err(card->dev,
911                                 "control %i:%i:%i:%s:%i: unaligned value %lld (step %lld) at count %i\n",
912                                 control->id.iface, control->id.device,
913                                 control->id.subdevice, control->id.name,
914                                 control->id.index, lval, lstep, i);
915                         return -EINVAL;
916                 }
917         }
918
919         return 0;
920 }
921
922 /* perform sanity checks to the given snd_ctl_elem_value object */
923 static int sanity_check_elem_value(struct snd_card *card,
924                                    const struct snd_ctl_elem_value *control,
925                                    const struct snd_ctl_elem_info *info,
926                                    u32 pattern)
927 {
928         size_t offset;
929         int i, ret = 0;
930         u32 *p;
931
932         switch (info->type) {
933         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
934         case SNDRV_CTL_ELEM_TYPE_INTEGER:
935         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
936         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
937                 for (i = 0; i < info->count; i++) {
938                         ret = sanity_check_int_value(card, control, info, i);
939                         if (ret < 0)
940                                 return ret;
941                 }
942                 break;
943         default:
944                 break;
945         }
946
947         /* check whether the remaining area kept untouched */
948         offset = value_sizes[info->type] * info->count;
949         offset = DIV_ROUND_UP(offset, sizeof(u32));
950         p = (u32 *)control->value.bytes.data + offset;
951         for (; offset < sizeof(control->value) / sizeof(u32); offset++, p++) {
952                 if (*p != pattern) {
953                         ret = -EINVAL;
954                         break;
955                 }
956                 *p = 0; /* clear the checked area */
957         }
958
959         return ret;
960 }
961 #else
962 static inline void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
963                                              struct snd_ctl_elem_info *info,
964                                              u32 pattern)
965 {
966 }
967
968 static inline int sanity_check_elem_value(struct snd_card *card,
969                                           struct snd_ctl_elem_value *control,
970                                           struct snd_ctl_elem_info *info,
971                                           u32 pattern)
972 {
973         return 0;
974 }
975 #endif
976
977 static int __snd_ctl_elem_info(struct snd_card *card,
978                                struct snd_kcontrol *kctl,
979                                struct snd_ctl_elem_info *info,
980                                struct snd_ctl_file *ctl)
981 {
982         struct snd_kcontrol_volatile *vd;
983         unsigned int index_offset;
984         int result;
985
986 #ifdef CONFIG_SND_DEBUG
987         info->access = 0;
988 #endif
989         result = kctl->info(kctl, info);
990         if (result >= 0) {
991                 snd_BUG_ON(info->access);
992                 index_offset = snd_ctl_get_ioff(kctl, &info->id);
993                 vd = &kctl->vd[index_offset];
994                 snd_ctl_build_ioff(&info->id, kctl, index_offset);
995                 info->access = vd->access;
996                 if (vd->owner) {
997                         info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
998                         if (vd->owner == ctl)
999                                 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
1000                         info->owner = pid_vnr(vd->owner->pid);
1001                 } else {
1002                         info->owner = -1;
1003                 }
1004                 if (!snd_ctl_skip_validation(info) &&
1005                     snd_ctl_check_elem_info(card, info) < 0)
1006                         result = -EINVAL;
1007         }
1008         return result;
1009 }
1010
1011 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
1012                              struct snd_ctl_elem_info *info)
1013 {
1014         struct snd_card *card = ctl->card;
1015         struct snd_kcontrol *kctl;
1016         int result;
1017
1018         down_read(&card->controls_rwsem);
1019         kctl = snd_ctl_find_id(card, &info->id);
1020         if (kctl == NULL)
1021                 result = -ENOENT;
1022         else
1023                 result = __snd_ctl_elem_info(card, kctl, info, ctl);
1024         up_read(&card->controls_rwsem);
1025         return result;
1026 }
1027
1028 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
1029                                   struct snd_ctl_elem_info __user *_info)
1030 {
1031         struct snd_ctl_elem_info info;
1032         int result;
1033
1034         if (copy_from_user(&info, _info, sizeof(info)))
1035                 return -EFAULT;
1036         result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
1037         if (result < 0)
1038                 return result;
1039         result = snd_ctl_elem_info(ctl, &info);
1040         if (result < 0)
1041                 return result;
1042         /* drop internal access flags */
1043         info.access &= ~SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
1044         if (copy_to_user(_info, &info, sizeof(info)))
1045                 return -EFAULT;
1046         return result;
1047 }
1048
1049 static int snd_ctl_elem_read(struct snd_card *card,
1050                              struct snd_ctl_elem_value *control)
1051 {
1052         struct snd_kcontrol *kctl;
1053         struct snd_kcontrol_volatile *vd;
1054         unsigned int index_offset;
1055         struct snd_ctl_elem_info info;
1056         const u32 pattern = 0xdeadbeef;
1057         int ret;
1058
1059         kctl = snd_ctl_find_id(card, &control->id);
1060         if (kctl == NULL)
1061                 return -ENOENT;
1062
1063         index_offset = snd_ctl_get_ioff(kctl, &control->id);
1064         vd = &kctl->vd[index_offset];
1065         if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
1066                 return -EPERM;
1067
1068         snd_ctl_build_ioff(&control->id, kctl, index_offset);
1069
1070 #ifdef CONFIG_SND_CTL_VALIDATION
1071         /* info is needed only for validation */
1072         memset(&info, 0, sizeof(info));
1073         info.id = control->id;
1074         ret = __snd_ctl_elem_info(card, kctl, &info, NULL);
1075         if (ret < 0)
1076                 return ret;
1077 #endif
1078
1079         if (!snd_ctl_skip_validation(&info))
1080                 fill_remaining_elem_value(control, &info, pattern);
1081         ret = kctl->get(kctl, control);
1082         if (ret < 0)
1083                 return ret;
1084         if (!snd_ctl_skip_validation(&info) &&
1085             sanity_check_elem_value(card, control, &info, pattern) < 0) {
1086                 dev_err(card->dev,
1087                         "control %i:%i:%i:%s:%i: access overflow\n",
1088                         control->id.iface, control->id.device,
1089                         control->id.subdevice, control->id.name,
1090                         control->id.index);
1091                 return -EINVAL;
1092         }
1093         return ret;
1094 }
1095
1096 static int snd_ctl_elem_read_user(struct snd_card *card,
1097                                   struct snd_ctl_elem_value __user *_control)
1098 {
1099         struct snd_ctl_elem_value *control;
1100         int result;
1101
1102         control = memdup_user(_control, sizeof(*control));
1103         if (IS_ERR(control))
1104                 return PTR_ERR(control);
1105
1106         result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1107         if (result < 0)
1108                 goto error;
1109
1110         down_read(&card->controls_rwsem);
1111         result = snd_ctl_elem_read(card, control);
1112         up_read(&card->controls_rwsem);
1113         if (result < 0)
1114                 goto error;
1115
1116         if (copy_to_user(_control, control, sizeof(*control)))
1117                 result = -EFAULT;
1118  error:
1119         kfree(control);
1120         return result;
1121 }
1122
1123 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
1124                               struct snd_ctl_elem_value *control)
1125 {
1126         struct snd_kcontrol *kctl;
1127         struct snd_kcontrol_volatile *vd;
1128         unsigned int index_offset;
1129         int result;
1130
1131         down_write(&card->controls_rwsem);
1132         kctl = snd_ctl_find_id(card, &control->id);
1133         if (kctl == NULL) {
1134                 up_write(&card->controls_rwsem);
1135                 return -ENOENT;
1136         }
1137
1138         index_offset = snd_ctl_get_ioff(kctl, &control->id);
1139         vd = &kctl->vd[index_offset];
1140         if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
1141             (file && vd->owner && vd->owner != file)) {
1142                 up_write(&card->controls_rwsem);
1143                 return -EPERM;
1144         }
1145
1146         snd_ctl_build_ioff(&control->id, kctl, index_offset);
1147         result = kctl->put(kctl, control);
1148         if (result < 0) {
1149                 up_write(&card->controls_rwsem);
1150                 return result;
1151         }
1152
1153         if (result > 0) {
1154                 downgrade_write(&card->controls_rwsem);
1155                 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, index_offset);
1156                 up_read(&card->controls_rwsem);
1157         } else {
1158                 up_write(&card->controls_rwsem);
1159         }
1160
1161         return 0;
1162 }
1163
1164 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
1165                                    struct snd_ctl_elem_value __user *_control)
1166 {
1167         struct snd_ctl_elem_value *control;
1168         struct snd_card *card;
1169         int result;
1170
1171         control = memdup_user(_control, sizeof(*control));
1172         if (IS_ERR(control))
1173                 return PTR_ERR(control);
1174
1175         card = file->card;
1176         result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1177         if (result < 0)
1178                 goto error;
1179
1180         result = snd_ctl_elem_write(card, file, control);
1181         if (result < 0)
1182                 goto error;
1183
1184         if (copy_to_user(_control, control, sizeof(*control)))
1185                 result = -EFAULT;
1186  error:
1187         kfree(control);
1188         return result;
1189 }
1190
1191 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
1192                              struct snd_ctl_elem_id __user *_id)
1193 {
1194         struct snd_card *card = file->card;
1195         struct snd_ctl_elem_id id;
1196         struct snd_kcontrol *kctl;
1197         struct snd_kcontrol_volatile *vd;
1198         int result;
1199
1200         if (copy_from_user(&id, _id, sizeof(id)))
1201                 return -EFAULT;
1202         down_write(&card->controls_rwsem);
1203         kctl = snd_ctl_find_id(card, &id);
1204         if (kctl == NULL) {
1205                 result = -ENOENT;
1206         } else {
1207                 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1208                 if (vd->owner != NULL)
1209                         result = -EBUSY;
1210                 else {
1211                         vd->owner = file;
1212                         result = 0;
1213                 }
1214         }
1215         up_write(&card->controls_rwsem);
1216         return result;
1217 }
1218
1219 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
1220                                struct snd_ctl_elem_id __user *_id)
1221 {
1222         struct snd_card *card = file->card;
1223         struct snd_ctl_elem_id id;
1224         struct snd_kcontrol *kctl;
1225         struct snd_kcontrol_volatile *vd;
1226         int result;
1227
1228         if (copy_from_user(&id, _id, sizeof(id)))
1229                 return -EFAULT;
1230         down_write(&card->controls_rwsem);
1231         kctl = snd_ctl_find_id(card, &id);
1232         if (kctl == NULL) {
1233                 result = -ENOENT;
1234         } else {
1235                 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1236                 if (vd->owner == NULL)
1237                         result = -EINVAL;
1238                 else if (vd->owner != file)
1239                         result = -EPERM;
1240                 else {
1241                         vd->owner = NULL;
1242                         result = 0;
1243                 }
1244         }
1245         up_write(&card->controls_rwsem);
1246         return result;
1247 }
1248
1249 struct user_element {
1250         struct snd_ctl_elem_info info;
1251         struct snd_card *card;
1252         char *elem_data;                /* element data */
1253         unsigned long elem_data_size;   /* size of element data in bytes */
1254         void *tlv_data;                 /* TLV data */
1255         unsigned long tlv_data_size;    /* TLV data size */
1256         void *priv_data;                /* private data (like strings for enumerated type) */
1257 };
1258
1259 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1260                                   struct snd_ctl_elem_info *uinfo)
1261 {
1262         struct user_element *ue = kcontrol->private_data;
1263         unsigned int offset;
1264
1265         offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1266         *uinfo = ue->info;
1267         snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1268
1269         return 0;
1270 }
1271
1272 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1273                                        struct snd_ctl_elem_info *uinfo)
1274 {
1275         struct user_element *ue = kcontrol->private_data;
1276         const char *names;
1277         unsigned int item;
1278         unsigned int offset;
1279
1280         item = uinfo->value.enumerated.item;
1281
1282         offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1283         *uinfo = ue->info;
1284         snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1285
1286         item = min(item, uinfo->value.enumerated.items - 1);
1287         uinfo->value.enumerated.item = item;
1288
1289         names = ue->priv_data;
1290         for (; item > 0; --item)
1291                 names += strlen(names) + 1;
1292         strcpy(uinfo->value.enumerated.name, names);
1293
1294         return 0;
1295 }
1296
1297 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1298                                  struct snd_ctl_elem_value *ucontrol)
1299 {
1300         struct user_element *ue = kcontrol->private_data;
1301         unsigned int size = ue->elem_data_size;
1302         char *src = ue->elem_data +
1303                         snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1304
1305         memcpy(&ucontrol->value, src, size);
1306         return 0;
1307 }
1308
1309 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1310                                  struct snd_ctl_elem_value *ucontrol)
1311 {
1312         int change;
1313         struct user_element *ue = kcontrol->private_data;
1314         unsigned int size = ue->elem_data_size;
1315         char *dst = ue->elem_data +
1316                         snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1317
1318         change = memcmp(&ucontrol->value, dst, size) != 0;
1319         if (change)
1320                 memcpy(dst, &ucontrol->value, size);
1321         return change;
1322 }
1323
1324 static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1325                             unsigned int size)
1326 {
1327         struct user_element *ue = kctl->private_data;
1328         unsigned int *container;
1329         unsigned int mask = 0;
1330         int i;
1331         int change;
1332
1333         if (size > 1024 * 128)  /* sane value */
1334                 return -EINVAL;
1335
1336         container = vmemdup_user(buf, size);
1337         if (IS_ERR(container))
1338                 return PTR_ERR(container);
1339
1340         change = ue->tlv_data_size != size;
1341         if (!change)
1342                 change = memcmp(ue->tlv_data, container, size) != 0;
1343         if (!change) {
1344                 kvfree(container);
1345                 return 0;
1346         }
1347
1348         if (ue->tlv_data == NULL) {
1349                 /* Now TLV data is available. */
1350                 for (i = 0; i < kctl->count; ++i)
1351                         kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1352                 mask = SNDRV_CTL_EVENT_MASK_INFO;
1353         }
1354
1355         kvfree(ue->tlv_data);
1356         ue->tlv_data = container;
1357         ue->tlv_data_size = size;
1358
1359         mask |= SNDRV_CTL_EVENT_MASK_TLV;
1360         for (i = 0; i < kctl->count; ++i)
1361                 snd_ctl_notify_one(ue->card, mask, kctl, i);
1362
1363         return change;
1364 }
1365
1366 static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1367                          unsigned int size)
1368 {
1369         struct user_element *ue = kctl->private_data;
1370
1371         if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1372                 return -ENXIO;
1373
1374         if (size < ue->tlv_data_size)
1375                 return -ENOSPC;
1376
1377         if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1378                 return -EFAULT;
1379
1380         return 0;
1381 }
1382
1383 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1384                                  unsigned int size, unsigned int __user *buf)
1385 {
1386         if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1387                 return replace_user_tlv(kctl, buf, size);
1388         else
1389                 return read_user_tlv(kctl, buf, size);
1390 }
1391
1392 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1393 {
1394         char *names, *p;
1395         size_t buf_len, name_len;
1396         unsigned int i;
1397         const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1398
1399         if (ue->info.value.enumerated.names_length > 64 * 1024)
1400                 return -EINVAL;
1401
1402         names = vmemdup_user((const void __user *)user_ptrval,
1403                 ue->info.value.enumerated.names_length);
1404         if (IS_ERR(names))
1405                 return PTR_ERR(names);
1406
1407         /* check that there are enough valid names */
1408         buf_len = ue->info.value.enumerated.names_length;
1409         p = names;
1410         for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1411                 name_len = strnlen(p, buf_len);
1412                 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1413                         kvfree(names);
1414                         return -EINVAL;
1415                 }
1416                 p += name_len + 1;
1417                 buf_len -= name_len + 1;
1418         }
1419
1420         ue->priv_data = names;
1421         ue->info.value.enumerated.names_ptr = 0;
1422
1423         return 0;
1424 }
1425
1426 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1427 {
1428         struct user_element *ue = kcontrol->private_data;
1429
1430         kvfree(ue->tlv_data);
1431         kvfree(ue->priv_data);
1432         kfree(ue);
1433 }
1434
1435 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1436                             struct snd_ctl_elem_info *info, int replace)
1437 {
1438         struct snd_card *card = file->card;
1439         struct snd_kcontrol *kctl;
1440         unsigned int count;
1441         unsigned int access;
1442         long private_size;
1443         struct user_element *ue;
1444         unsigned int offset;
1445         int err;
1446
1447         if (!*info->id.name)
1448                 return -EINVAL;
1449         if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1450                 return -EINVAL;
1451
1452         /* Delete a control to replace them if needed. */
1453         if (replace) {
1454                 info->id.numid = 0;
1455                 err = snd_ctl_remove_user_ctl(file, &info->id);
1456                 if (err)
1457                         return err;
1458         }
1459
1460         /*
1461          * The number of userspace controls are counted control by control,
1462          * not element by element.
1463          */
1464         if (card->user_ctl_count + 1 > MAX_USER_CONTROLS)
1465                 return -ENOMEM;
1466
1467         /* Check the number of elements for this userspace control. */
1468         count = info->owner;
1469         if (count == 0)
1470                 count = 1;
1471
1472         /* Arrange access permissions if needed. */
1473         access = info->access;
1474         if (access == 0)
1475                 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1476         access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1477                    SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1478                    SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1479
1480         /* In initial state, nothing is available as TLV container. */
1481         if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1482                 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1483         access |= SNDRV_CTL_ELEM_ACCESS_USER;
1484
1485         /*
1486          * Check information and calculate the size of data specific to
1487          * this userspace control.
1488          */
1489         /* pass NULL to card for suppressing error messages */
1490         err = snd_ctl_check_elem_info(NULL, info);
1491         if (err < 0)
1492                 return err;
1493         /* user-space control doesn't allow zero-size data */
1494         if (info->count < 1)
1495                 return -EINVAL;
1496         private_size = value_sizes[info->type] * info->count;
1497
1498         /*
1499          * Keep memory object for this userspace control. After passing this
1500          * code block, the instance should be freed by snd_ctl_free_one().
1501          *
1502          * Note that these elements in this control are locked.
1503          */
1504         err = snd_ctl_new(&kctl, count, access, file);
1505         if (err < 0)
1506                 return err;
1507         memcpy(&kctl->id, &info->id, sizeof(kctl->id));
1508         kctl->private_data = kzalloc(sizeof(struct user_element) + private_size * count,
1509                                      GFP_KERNEL);
1510         if (kctl->private_data == NULL) {
1511                 kfree(kctl);
1512                 return -ENOMEM;
1513         }
1514         kctl->private_free = snd_ctl_elem_user_free;
1515
1516         /* Set private data for this userspace control. */
1517         ue = (struct user_element *)kctl->private_data;
1518         ue->card = card;
1519         ue->info = *info;
1520         ue->info.access = 0;
1521         ue->elem_data = (char *)ue + sizeof(*ue);
1522         ue->elem_data_size = private_size;
1523         if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1524                 err = snd_ctl_elem_init_enum_names(ue);
1525                 if (err < 0) {
1526                         snd_ctl_free_one(kctl);
1527                         return err;
1528                 }
1529         }
1530
1531         /* Set callback functions. */
1532         if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1533                 kctl->info = snd_ctl_elem_user_enum_info;
1534         else
1535                 kctl->info = snd_ctl_elem_user_info;
1536         if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1537                 kctl->get = snd_ctl_elem_user_get;
1538         if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1539                 kctl->put = snd_ctl_elem_user_put;
1540         if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1541                 kctl->tlv.c = snd_ctl_elem_user_tlv;
1542
1543         /* This function manage to free the instance on failure. */
1544         down_write(&card->controls_rwsem);
1545         err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
1546         if (err < 0) {
1547                 snd_ctl_free_one(kctl);
1548                 goto unlock;
1549         }
1550         offset = snd_ctl_get_ioff(kctl, &info->id);
1551         snd_ctl_build_ioff(&info->id, kctl, offset);
1552         /*
1553          * Here we cannot fill any field for the number of elements added by
1554          * this operation because there're no specific fields. The usage of
1555          * 'owner' field for this purpose may cause any bugs to userspace
1556          * applications because the field originally means PID of a process
1557          * which locks the element.
1558          */
1559
1560         card->user_ctl_count++;
1561
1562  unlock:
1563         up_write(&card->controls_rwsem);
1564         return err;
1565 }
1566
1567 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1568                                  struct snd_ctl_elem_info __user *_info, int replace)
1569 {
1570         struct snd_ctl_elem_info info;
1571         int err;
1572
1573         if (copy_from_user(&info, _info, sizeof(info)))
1574                 return -EFAULT;
1575         err = snd_ctl_elem_add(file, &info, replace);
1576         if (err < 0)
1577                 return err;
1578         if (copy_to_user(_info, &info, sizeof(info))) {
1579                 snd_ctl_remove_user_ctl(file, &info.id);
1580                 return -EFAULT;
1581         }
1582
1583         return 0;
1584 }
1585
1586 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1587                                struct snd_ctl_elem_id __user *_id)
1588 {
1589         struct snd_ctl_elem_id id;
1590
1591         if (copy_from_user(&id, _id, sizeof(id)))
1592                 return -EFAULT;
1593         return snd_ctl_remove_user_ctl(file, &id);
1594 }
1595
1596 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1597 {
1598         int subscribe;
1599         if (get_user(subscribe, ptr))
1600                 return -EFAULT;
1601         if (subscribe < 0) {
1602                 subscribe = file->subscribed;
1603                 if (put_user(subscribe, ptr))
1604                         return -EFAULT;
1605                 return 0;
1606         }
1607         if (subscribe) {
1608                 file->subscribed = 1;
1609                 return 0;
1610         } else if (file->subscribed) {
1611                 snd_ctl_empty_read_queue(file);
1612                 file->subscribed = 0;
1613         }
1614         return 0;
1615 }
1616
1617 static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1618                             struct snd_kcontrol *kctl,
1619                             struct snd_ctl_elem_id *id,
1620                             unsigned int __user *buf, unsigned int size)
1621 {
1622         static const struct {
1623                 int op;
1624                 int perm;
1625         } pairs[] = {
1626                 {SNDRV_CTL_TLV_OP_READ,  SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1627                 {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1628                 {SNDRV_CTL_TLV_OP_CMD,   SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1629         };
1630         struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1631         int i;
1632
1633         /* Check support of the request for this element. */
1634         for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1635                 if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1636                         break;
1637         }
1638         if (i == ARRAY_SIZE(pairs))
1639                 return -ENXIO;
1640
1641         if (kctl->tlv.c == NULL)
1642                 return -ENXIO;
1643
1644         /* Write and command operations are not allowed for locked element. */
1645         if (op_flag != SNDRV_CTL_TLV_OP_READ &&
1646             vd->owner != NULL && vd->owner != file)
1647                 return -EPERM;
1648
1649         return kctl->tlv.c(kctl, op_flag, size, buf);
1650 }
1651
1652 static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1653                         unsigned int __user *buf, unsigned int size)
1654 {
1655         struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1656         unsigned int len;
1657
1658         if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1659                 return -ENXIO;
1660
1661         if (kctl->tlv.p == NULL)
1662                 return -ENXIO;
1663
1664         len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1665         if (size < len)
1666                 return -ENOMEM;
1667
1668         if (copy_to_user(buf, kctl->tlv.p, len))
1669                 return -EFAULT;
1670
1671         return 0;
1672 }
1673
1674 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1675                              struct snd_ctl_tlv __user *buf,
1676                              int op_flag)
1677 {
1678         struct snd_ctl_tlv header;
1679         unsigned int __user *container;
1680         unsigned int container_size;
1681         struct snd_kcontrol *kctl;
1682         struct snd_ctl_elem_id id;
1683         struct snd_kcontrol_volatile *vd;
1684
1685         if (copy_from_user(&header, buf, sizeof(header)))
1686                 return -EFAULT;
1687
1688         /* In design of control core, numerical ID starts at 1. */
1689         if (header.numid == 0)
1690                 return -EINVAL;
1691
1692         /* At least, container should include type and length fields.  */
1693         if (header.length < sizeof(unsigned int) * 2)
1694                 return -EINVAL;
1695         container_size = header.length;
1696         container = buf->tlv;
1697
1698         kctl = snd_ctl_find_numid(file->card, header.numid);
1699         if (kctl == NULL)
1700                 return -ENOENT;
1701
1702         /* Calculate index of the element in this set. */
1703         id = kctl->id;
1704         snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1705         vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1706
1707         if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1708                 return call_tlv_handler(file, op_flag, kctl, &id, container,
1709                                         container_size);
1710         } else {
1711                 if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1712                         return read_tlv_buf(kctl, &id, container,
1713                                             container_size);
1714                 }
1715         }
1716
1717         /* Not supported. */
1718         return -ENXIO;
1719 }
1720
1721 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1722 {
1723         struct snd_ctl_file *ctl;
1724         struct snd_card *card;
1725         struct snd_kctl_ioctl *p;
1726         void __user *argp = (void __user *)arg;
1727         int __user *ip = argp;
1728         int err;
1729
1730         ctl = file->private_data;
1731         card = ctl->card;
1732         if (snd_BUG_ON(!card))
1733                 return -ENXIO;
1734         switch (cmd) {
1735         case SNDRV_CTL_IOCTL_PVERSION:
1736                 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1737         case SNDRV_CTL_IOCTL_CARD_INFO:
1738                 return snd_ctl_card_info(card, ctl, cmd, argp);
1739         case SNDRV_CTL_IOCTL_ELEM_LIST:
1740                 return snd_ctl_elem_list_user(card, argp);
1741         case SNDRV_CTL_IOCTL_ELEM_INFO:
1742                 return snd_ctl_elem_info_user(ctl, argp);
1743         case SNDRV_CTL_IOCTL_ELEM_READ:
1744                 return snd_ctl_elem_read_user(card, argp);
1745         case SNDRV_CTL_IOCTL_ELEM_WRITE:
1746                 return snd_ctl_elem_write_user(ctl, argp);
1747         case SNDRV_CTL_IOCTL_ELEM_LOCK:
1748                 return snd_ctl_elem_lock(ctl, argp);
1749         case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1750                 return snd_ctl_elem_unlock(ctl, argp);
1751         case SNDRV_CTL_IOCTL_ELEM_ADD:
1752                 return snd_ctl_elem_add_user(ctl, argp, 0);
1753         case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1754                 return snd_ctl_elem_add_user(ctl, argp, 1);
1755         case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1756                 return snd_ctl_elem_remove(ctl, argp);
1757         case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1758                 return snd_ctl_subscribe_events(ctl, ip);
1759         case SNDRV_CTL_IOCTL_TLV_READ:
1760                 down_read(&ctl->card->controls_rwsem);
1761                 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1762                 up_read(&ctl->card->controls_rwsem);
1763                 return err;
1764         case SNDRV_CTL_IOCTL_TLV_WRITE:
1765                 down_write(&ctl->card->controls_rwsem);
1766                 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1767                 up_write(&ctl->card->controls_rwsem);
1768                 return err;
1769         case SNDRV_CTL_IOCTL_TLV_COMMAND:
1770                 down_write(&ctl->card->controls_rwsem);
1771                 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1772                 up_write(&ctl->card->controls_rwsem);
1773                 return err;
1774         case SNDRV_CTL_IOCTL_POWER:
1775                 return -ENOPROTOOPT;
1776         case SNDRV_CTL_IOCTL_POWER_STATE:
1777 #ifdef CONFIG_PM
1778                 return put_user(card->power_state, ip) ? -EFAULT : 0;
1779 #else
1780                 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1781 #endif
1782         }
1783         down_read(&snd_ioctl_rwsem);
1784         list_for_each_entry(p, &snd_control_ioctls, list) {
1785                 err = p->fioctl(card, ctl, cmd, arg);
1786                 if (err != -ENOIOCTLCMD) {
1787                         up_read(&snd_ioctl_rwsem);
1788                         return err;
1789                 }
1790         }
1791         up_read(&snd_ioctl_rwsem);
1792         dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
1793         return -ENOTTY;
1794 }
1795
1796 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1797                             size_t count, loff_t * offset)
1798 {
1799         struct snd_ctl_file *ctl;
1800         int err = 0;
1801         ssize_t result = 0;
1802
1803         ctl = file->private_data;
1804         if (snd_BUG_ON(!ctl || !ctl->card))
1805                 return -ENXIO;
1806         if (!ctl->subscribed)
1807                 return -EBADFD;
1808         if (count < sizeof(struct snd_ctl_event))
1809                 return -EINVAL;
1810         spin_lock_irq(&ctl->read_lock);
1811         while (count >= sizeof(struct snd_ctl_event)) {
1812                 struct snd_ctl_event ev;
1813                 struct snd_kctl_event *kev;
1814                 while (list_empty(&ctl->events)) {
1815                         wait_queue_entry_t wait;
1816                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1817                                 err = -EAGAIN;
1818                                 goto __end_lock;
1819                         }
1820                         init_waitqueue_entry(&wait, current);
1821                         add_wait_queue(&ctl->change_sleep, &wait);
1822                         set_current_state(TASK_INTERRUPTIBLE);
1823                         spin_unlock_irq(&ctl->read_lock);
1824                         schedule();
1825                         remove_wait_queue(&ctl->change_sleep, &wait);
1826                         if (ctl->card->shutdown)
1827                                 return -ENODEV;
1828                         if (signal_pending(current))
1829                                 return -ERESTARTSYS;
1830                         spin_lock_irq(&ctl->read_lock);
1831                 }
1832                 kev = snd_kctl_event(ctl->events.next);
1833                 ev.type = SNDRV_CTL_EVENT_ELEM;
1834                 ev.data.elem.mask = kev->mask;
1835                 ev.data.elem.id = kev->id;
1836                 list_del(&kev->list);
1837                 spin_unlock_irq(&ctl->read_lock);
1838                 kfree(kev);
1839                 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1840                         err = -EFAULT;
1841                         goto __end;
1842                 }
1843                 spin_lock_irq(&ctl->read_lock);
1844                 buffer += sizeof(struct snd_ctl_event);
1845                 count -= sizeof(struct snd_ctl_event);
1846                 result += sizeof(struct snd_ctl_event);
1847         }
1848       __end_lock:
1849         spin_unlock_irq(&ctl->read_lock);
1850       __end:
1851         return result > 0 ? result : err;
1852 }
1853
1854 static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
1855 {
1856         __poll_t mask;
1857         struct snd_ctl_file *ctl;
1858
1859         ctl = file->private_data;
1860         if (!ctl->subscribed)
1861                 return 0;
1862         poll_wait(file, &ctl->change_sleep, wait);
1863
1864         mask = 0;
1865         if (!list_empty(&ctl->events))
1866                 mask |= EPOLLIN | EPOLLRDNORM;
1867
1868         return mask;
1869 }
1870
1871 /*
1872  * register the device-specific control-ioctls.
1873  * called from each device manager like pcm.c, hwdep.c, etc.
1874  */
1875 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1876 {
1877         struct snd_kctl_ioctl *pn;
1878
1879         pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1880         if (pn == NULL)
1881                 return -ENOMEM;
1882         pn->fioctl = fcn;
1883         down_write(&snd_ioctl_rwsem);
1884         list_add_tail(&pn->list, lists);
1885         up_write(&snd_ioctl_rwsem);
1886         return 0;
1887 }
1888
1889 /**
1890  * snd_ctl_register_ioctl - register the device-specific control-ioctls
1891  * @fcn: ioctl callback function
1892  *
1893  * called from each device manager like pcm.c, hwdep.c, etc.
1894  */
1895 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1896 {
1897         return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1898 }
1899 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1900
1901 #ifdef CONFIG_COMPAT
1902 /**
1903  * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1904  * control-ioctls
1905  * @fcn: ioctl callback function
1906  */
1907 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1908 {
1909         return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1910 }
1911 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1912 #endif
1913
1914 /*
1915  * de-register the device-specific control-ioctls.
1916  */
1917 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1918                                      struct list_head *lists)
1919 {
1920         struct snd_kctl_ioctl *p;
1921
1922         if (snd_BUG_ON(!fcn))
1923                 return -EINVAL;
1924         down_write(&snd_ioctl_rwsem);
1925         list_for_each_entry(p, lists, list) {
1926                 if (p->fioctl == fcn) {
1927                         list_del(&p->list);
1928                         up_write(&snd_ioctl_rwsem);
1929                         kfree(p);
1930                         return 0;
1931                 }
1932         }
1933         up_write(&snd_ioctl_rwsem);
1934         snd_BUG();
1935         return -EINVAL;
1936 }
1937
1938 /**
1939  * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1940  * @fcn: ioctl callback function to unregister
1941  */
1942 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1943 {
1944         return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1945 }
1946 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1947
1948 #ifdef CONFIG_COMPAT
1949 /**
1950  * snd_ctl_unregister_ioctl_compat - de-register the device-specific compat
1951  * 32bit control-ioctls
1952  * @fcn: ioctl callback function to unregister
1953  */
1954 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1955 {
1956         return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1957 }
1958 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1959 #endif
1960
1961 static int snd_ctl_fasync(int fd, struct file * file, int on)
1962 {
1963         struct snd_ctl_file *ctl;
1964
1965         ctl = file->private_data;
1966         return fasync_helper(fd, file, on, &ctl->fasync);
1967 }
1968
1969 /* return the preferred subdevice number if already assigned;
1970  * otherwise return -1
1971  */
1972 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
1973 {
1974         struct snd_ctl_file *kctl;
1975         int subdevice = -1;
1976         unsigned long flags;
1977
1978         read_lock_irqsave(&card->ctl_files_rwlock, flags);
1979         list_for_each_entry(kctl, &card->ctl_files, list) {
1980                 if (kctl->pid == task_pid(current)) {
1981                         subdevice = kctl->preferred_subdevice[type];
1982                         if (subdevice != -1)
1983                                 break;
1984                 }
1985         }
1986         read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
1987         return subdevice;
1988 }
1989 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
1990
1991 /*
1992  * ioctl32 compat
1993  */
1994 #ifdef CONFIG_COMPAT
1995 #include "control_compat.c"
1996 #else
1997 #define snd_ctl_ioctl_compat    NULL
1998 #endif
1999
2000 /*
2001  *  INIT PART
2002  */
2003
2004 static const struct file_operations snd_ctl_f_ops =
2005 {
2006         .owner =        THIS_MODULE,
2007         .read =         snd_ctl_read,
2008         .open =         snd_ctl_open,
2009         .release =      snd_ctl_release,
2010         .llseek =       no_llseek,
2011         .poll =         snd_ctl_poll,
2012         .unlocked_ioctl =       snd_ctl_ioctl,
2013         .compat_ioctl = snd_ctl_ioctl_compat,
2014         .fasync =       snd_ctl_fasync,
2015 };
2016
2017 /*
2018  * registration of the control device
2019  */
2020 static int snd_ctl_dev_register(struct snd_device *device)
2021 {
2022         struct snd_card *card = device->device_data;
2023
2024         return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
2025                                    &snd_ctl_f_ops, card, &card->ctl_dev);
2026 }
2027
2028 /*
2029  * disconnection of the control device
2030  */
2031 static int snd_ctl_dev_disconnect(struct snd_device *device)
2032 {
2033         struct snd_card *card = device->device_data;
2034         struct snd_ctl_file *ctl;
2035         unsigned long flags;
2036
2037         read_lock_irqsave(&card->ctl_files_rwlock, flags);
2038         list_for_each_entry(ctl, &card->ctl_files, list) {
2039                 wake_up(&ctl->change_sleep);
2040                 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
2041         }
2042         read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2043
2044         return snd_unregister_device(&card->ctl_dev);
2045 }
2046
2047 /*
2048  * free all controls
2049  */
2050 static int snd_ctl_dev_free(struct snd_device *device)
2051 {
2052         struct snd_card *card = device->device_data;
2053         struct snd_kcontrol *control;
2054
2055         down_write(&card->controls_rwsem);
2056         while (!list_empty(&card->controls)) {
2057                 control = snd_kcontrol(card->controls.next);
2058                 snd_ctl_remove(card, control);
2059         }
2060         up_write(&card->controls_rwsem);
2061         put_device(&card->ctl_dev);
2062         return 0;
2063 }
2064
2065 /*
2066  * create control core:
2067  * called from init.c
2068  */
2069 int snd_ctl_create(struct snd_card *card)
2070 {
2071         static const struct snd_device_ops ops = {
2072                 .dev_free = snd_ctl_dev_free,
2073                 .dev_register = snd_ctl_dev_register,
2074                 .dev_disconnect = snd_ctl_dev_disconnect,
2075         };
2076         int err;
2077
2078         if (snd_BUG_ON(!card))
2079                 return -ENXIO;
2080         if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
2081                 return -ENXIO;
2082
2083         snd_device_initialize(&card->ctl_dev, card);
2084         dev_set_name(&card->ctl_dev, "controlC%d", card->number);
2085
2086         err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
2087         if (err < 0)
2088                 put_device(&card->ctl_dev);
2089         return err;
2090 }
2091
2092 /*
2093  * Frequently used control callbacks/helpers
2094  */
2095
2096 /**
2097  * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
2098  * callback with a mono channel
2099  * @kcontrol: the kcontrol instance
2100  * @uinfo: info to store
2101  *
2102  * This is a function that can be used as info callback for a standard
2103  * boolean control with a single mono channel.
2104  */
2105 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
2106                               struct snd_ctl_elem_info *uinfo)
2107 {
2108         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2109         uinfo->count = 1;
2110         uinfo->value.integer.min = 0;
2111         uinfo->value.integer.max = 1;
2112         return 0;
2113 }
2114 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
2115
2116 /**
2117  * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
2118  * callback with stereo two channels
2119  * @kcontrol: the kcontrol instance
2120  * @uinfo: info to store
2121  *
2122  * This is a function that can be used as info callback for a standard
2123  * boolean control with stereo two channels.
2124  */
2125 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
2126                                 struct snd_ctl_elem_info *uinfo)
2127 {
2128         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2129         uinfo->count = 2;
2130         uinfo->value.integer.min = 0;
2131         uinfo->value.integer.max = 1;
2132         return 0;
2133 }
2134 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
2135
2136 /**
2137  * snd_ctl_enum_info - fills the info structure for an enumerated control
2138  * @info: the structure to be filled
2139  * @channels: the number of the control's channels; often one
2140  * @items: the number of control values; also the size of @names
2141  * @names: an array containing the names of all control values
2142  *
2143  * Sets all required fields in @info to their appropriate values.
2144  * If the control's accessibility is not the default (readable and writable),
2145  * the caller has to fill @info->access.
2146  *
2147  * Return: Zero.
2148  */
2149 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
2150                       unsigned int items, const char *const names[])
2151 {
2152         info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2153         info->count = channels;
2154         info->value.enumerated.items = items;
2155         if (!items)
2156                 return 0;
2157         if (info->value.enumerated.item >= items)
2158                 info->value.enumerated.item = items - 1;
2159         WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
2160              "ALSA: too long item name '%s'\n",
2161              names[info->value.enumerated.item]);
2162         strscpy(info->value.enumerated.name,
2163                 names[info->value.enumerated.item],
2164                 sizeof(info->value.enumerated.name));
2165         return 0;
2166 }
2167 EXPORT_SYMBOL(snd_ctl_enum_info);