~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/control.c

Version: ~ [ linux-5.2-rc5 ] ~ [ linux-5.1.11 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.52 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.127 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.182 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.182 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.68 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp