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