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