0001
0002
0003
0004
0005
0006
0007 #include <linux/threads.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/module.h>
0010 #include <linux/moduleparam.h>
0011 #include <linux/slab.h>
0012 #include <linux/vmalloc.h>
0013 #include <linux/time.h>
0014 #include <linux/mm.h>
0015 #include <linux/math64.h>
0016 #include <linux/sched/signal.h>
0017 #include <sound/core.h>
0018 #include <sound/minors.h>
0019 #include <sound/info.h>
0020 #include <sound/control.h>
0021
0022
0023 static int max_user_ctl_alloc_size = 8 * 1024 * 1024;
0024 module_param_named(max_user_ctl_alloc_size, max_user_ctl_alloc_size, int, 0444);
0025 MODULE_PARM_DESC(max_user_ctl_alloc_size, "Max allocation size for user controls");
0026
0027 #define MAX_CONTROL_COUNT 1028
0028
0029 struct snd_kctl_ioctl {
0030 struct list_head list;
0031 snd_kctl_ioctl_func_t fioctl;
0032 };
0033
0034 static DECLARE_RWSEM(snd_ioctl_rwsem);
0035 static DECLARE_RWSEM(snd_ctl_layer_rwsem);
0036 static LIST_HEAD(snd_control_ioctls);
0037 #ifdef CONFIG_COMPAT
0038 static LIST_HEAD(snd_control_compat_ioctls);
0039 #endif
0040 static struct snd_ctl_layer_ops *snd_ctl_layer;
0041
0042 static int snd_ctl_open(struct inode *inode, struct file *file)
0043 {
0044 unsigned long flags;
0045 struct snd_card *card;
0046 struct snd_ctl_file *ctl;
0047 int i, err;
0048
0049 err = stream_open(inode, file);
0050 if (err < 0)
0051 return err;
0052
0053 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
0054 if (!card) {
0055 err = -ENODEV;
0056 goto __error1;
0057 }
0058 err = snd_card_file_add(card, file);
0059 if (err < 0) {
0060 err = -ENODEV;
0061 goto __error1;
0062 }
0063 if (!try_module_get(card->module)) {
0064 err = -EFAULT;
0065 goto __error2;
0066 }
0067 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
0068 if (ctl == NULL) {
0069 err = -ENOMEM;
0070 goto __error;
0071 }
0072 INIT_LIST_HEAD(&ctl->events);
0073 init_waitqueue_head(&ctl->change_sleep);
0074 spin_lock_init(&ctl->read_lock);
0075 ctl->card = card;
0076 for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
0077 ctl->preferred_subdevice[i] = -1;
0078 ctl->pid = get_pid(task_pid(current));
0079 file->private_data = ctl;
0080 write_lock_irqsave(&card->ctl_files_rwlock, flags);
0081 list_add_tail(&ctl->list, &card->ctl_files);
0082 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
0083 snd_card_unref(card);
0084 return 0;
0085
0086 __error:
0087 module_put(card->module);
0088 __error2:
0089 snd_card_file_remove(card, file);
0090 __error1:
0091 if (card)
0092 snd_card_unref(card);
0093 return err;
0094 }
0095
0096 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
0097 {
0098 unsigned long flags;
0099 struct snd_kctl_event *cread;
0100
0101 spin_lock_irqsave(&ctl->read_lock, flags);
0102 while (!list_empty(&ctl->events)) {
0103 cread = snd_kctl_event(ctl->events.next);
0104 list_del(&cread->list);
0105 kfree(cread);
0106 }
0107 spin_unlock_irqrestore(&ctl->read_lock, flags);
0108 }
0109
0110 static int snd_ctl_release(struct inode *inode, struct file *file)
0111 {
0112 unsigned long flags;
0113 struct snd_card *card;
0114 struct snd_ctl_file *ctl;
0115 struct snd_kcontrol *control;
0116 unsigned int idx;
0117
0118 ctl = file->private_data;
0119 file->private_data = NULL;
0120 card = ctl->card;
0121 write_lock_irqsave(&card->ctl_files_rwlock, flags);
0122 list_del(&ctl->list);
0123 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
0124 down_write(&card->controls_rwsem);
0125 list_for_each_entry(control, &card->controls, list)
0126 for (idx = 0; idx < control->count; idx++)
0127 if (control->vd[idx].owner == ctl)
0128 control->vd[idx].owner = NULL;
0129 up_write(&card->controls_rwsem);
0130 snd_fasync_free(ctl->fasync);
0131 snd_ctl_empty_read_queue(ctl);
0132 put_pid(ctl->pid);
0133 kfree(ctl);
0134 module_put(card->module);
0135 snd_card_file_remove(card, file);
0136 return 0;
0137 }
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
0150 struct snd_ctl_elem_id *id)
0151 {
0152 unsigned long flags;
0153 struct snd_ctl_file *ctl;
0154 struct snd_kctl_event *ev;
0155
0156 if (snd_BUG_ON(!card || !id))
0157 return;
0158 if (card->shutdown)
0159 return;
0160 read_lock_irqsave(&card->ctl_files_rwlock, flags);
0161 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
0162 card->mixer_oss_change_count++;
0163 #endif
0164 list_for_each_entry(ctl, &card->ctl_files, list) {
0165 if (!ctl->subscribed)
0166 continue;
0167 spin_lock(&ctl->read_lock);
0168 list_for_each_entry(ev, &ctl->events, list) {
0169 if (ev->id.numid == id->numid) {
0170 ev->mask |= mask;
0171 goto _found;
0172 }
0173 }
0174 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
0175 if (ev) {
0176 ev->id = *id;
0177 ev->mask = mask;
0178 list_add_tail(&ev->list, &ctl->events);
0179 } else {
0180 dev_err(card->dev, "No memory available to allocate event\n");
0181 }
0182 _found:
0183 wake_up(&ctl->change_sleep);
0184 spin_unlock(&ctl->read_lock);
0185 snd_kill_fasync(ctl->fasync, SIGIO, POLL_IN);
0186 }
0187 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
0188 }
0189 EXPORT_SYMBOL(snd_ctl_notify);
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201 void snd_ctl_notify_one(struct snd_card *card, unsigned int mask,
0202 struct snd_kcontrol *kctl, unsigned int ioff)
0203 {
0204 struct snd_ctl_elem_id id = kctl->id;
0205 struct snd_ctl_layer_ops *lops;
0206
0207 id.index += ioff;
0208 id.numid += ioff;
0209 snd_ctl_notify(card, mask, &id);
0210 down_read(&snd_ctl_layer_rwsem);
0211 for (lops = snd_ctl_layer; lops; lops = lops->next)
0212 lops->lnotify(card, mask, kctl, ioff);
0213 up_read(&snd_ctl_layer_rwsem);
0214 }
0215 EXPORT_SYMBOL(snd_ctl_notify_one);
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230 static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
0231 unsigned int access, struct snd_ctl_file *file)
0232 {
0233 unsigned int idx;
0234
0235 if (count == 0 || count > MAX_CONTROL_COUNT)
0236 return -EINVAL;
0237
0238 *kctl = kzalloc(struct_size(*kctl, vd, count), GFP_KERNEL);
0239 if (!*kctl)
0240 return -ENOMEM;
0241
0242 for (idx = 0; idx < count; idx++) {
0243 (*kctl)->vd[idx].access = access;
0244 (*kctl)->vd[idx].owner = file;
0245 }
0246 (*kctl)->count = count;
0247
0248 return 0;
0249 }
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
0263 void *private_data)
0264 {
0265 struct snd_kcontrol *kctl;
0266 unsigned int count;
0267 unsigned int access;
0268 int err;
0269
0270 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
0271 return NULL;
0272
0273 count = ncontrol->count;
0274 if (count == 0)
0275 count = 1;
0276
0277 access = ncontrol->access;
0278 if (access == 0)
0279 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
0280 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
0281 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
0282 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
0283 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
0284 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
0285 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
0286 SNDRV_CTL_ELEM_ACCESS_LED_MASK |
0287 SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
0288
0289 err = snd_ctl_new(&kctl, count, access, NULL);
0290 if (err < 0)
0291 return NULL;
0292
0293
0294 kctl->id.iface = ncontrol->iface;
0295 kctl->id.device = ncontrol->device;
0296 kctl->id.subdevice = ncontrol->subdevice;
0297 if (ncontrol->name) {
0298 strscpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
0299 if (strcmp(ncontrol->name, kctl->id.name) != 0)
0300 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
0301 ncontrol->name, kctl->id.name);
0302 }
0303 kctl->id.index = ncontrol->index;
0304
0305 kctl->info = ncontrol->info;
0306 kctl->get = ncontrol->get;
0307 kctl->put = ncontrol->put;
0308 kctl->tlv.p = ncontrol->tlv.p;
0309
0310 kctl->private_value = ncontrol->private_value;
0311 kctl->private_data = private_data;
0312
0313 return kctl;
0314 }
0315 EXPORT_SYMBOL(snd_ctl_new1);
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
0326 {
0327 if (kcontrol) {
0328 if (kcontrol->private_free)
0329 kcontrol->private_free(kcontrol);
0330 kfree(kcontrol);
0331 }
0332 }
0333 EXPORT_SYMBOL(snd_ctl_free_one);
0334
0335 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
0336 unsigned int count)
0337 {
0338 struct snd_kcontrol *kctl;
0339
0340
0341 if (card->last_numid >= UINT_MAX - count)
0342 card->last_numid = 0;
0343
0344 list_for_each_entry(kctl, &card->controls, list) {
0345 if (kctl->id.numid < card->last_numid + 1 + count &&
0346 kctl->id.numid + kctl->count > card->last_numid + 1) {
0347 card->last_numid = kctl->id.numid + kctl->count - 1;
0348 return true;
0349 }
0350 }
0351 return false;
0352 }
0353
0354 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
0355 {
0356 unsigned int iter = 100000;
0357
0358 while (snd_ctl_remove_numid_conflict(card, count)) {
0359 if (--iter == 0) {
0360
0361 dev_err(card->dev, "unable to allocate new control numid\n");
0362 return -ENOMEM;
0363 }
0364 }
0365 return 0;
0366 }
0367
0368
0369 static bool elem_id_matches(const struct snd_kcontrol *kctl,
0370 const struct snd_ctl_elem_id *id)
0371 {
0372 return kctl->id.iface == id->iface &&
0373 kctl->id.device == id->device &&
0374 kctl->id.subdevice == id->subdevice &&
0375 !strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)) &&
0376 kctl->id.index <= id->index &&
0377 kctl->id.index + kctl->count > id->index;
0378 }
0379
0380 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
0381
0382
0383
0384
0385 #define MULTIPLIER 37
0386 static unsigned long get_ctl_id_hash(const struct snd_ctl_elem_id *id)
0387 {
0388 int i;
0389 unsigned long h;
0390
0391 h = id->iface;
0392 h = MULTIPLIER * h + id->device;
0393 h = MULTIPLIER * h + id->subdevice;
0394 for (i = 0; i < SNDRV_CTL_ELEM_ID_NAME_MAXLEN && id->name[i]; i++)
0395 h = MULTIPLIER * h + id->name[i];
0396 h = MULTIPLIER * h + id->index;
0397 h &= LONG_MAX;
0398 return h;
0399 }
0400
0401
0402 static void add_hash_entries(struct snd_card *card,
0403 struct snd_kcontrol *kcontrol)
0404 {
0405 struct snd_ctl_elem_id id = kcontrol->id;
0406 int i;
0407
0408 xa_store_range(&card->ctl_numids, kcontrol->id.numid,
0409 kcontrol->id.numid + kcontrol->count - 1,
0410 kcontrol, GFP_KERNEL);
0411
0412 for (i = 0; i < kcontrol->count; i++) {
0413 id.index = kcontrol->id.index + i;
0414 if (xa_insert(&card->ctl_hash, get_ctl_id_hash(&id),
0415 kcontrol, GFP_KERNEL)) {
0416
0417 card->ctl_hash_collision = true;
0418 dev_dbg(card->dev, "ctl_hash collision %d:%s:%d\n",
0419 id.iface, id.name, id.index);
0420 }
0421 }
0422 }
0423
0424
0425 static void remove_hash_entries(struct snd_card *card,
0426 struct snd_kcontrol *kcontrol)
0427 {
0428 struct snd_ctl_elem_id id = kcontrol->id;
0429 struct snd_kcontrol *matched;
0430 unsigned long h;
0431 int i;
0432
0433 for (i = 0; i < kcontrol->count; i++) {
0434 xa_erase(&card->ctl_numids, id.numid);
0435 h = get_ctl_id_hash(&id);
0436 matched = xa_load(&card->ctl_hash, h);
0437 if (matched && (matched == kcontrol ||
0438 elem_id_matches(matched, &id)))
0439 xa_erase(&card->ctl_hash, h);
0440 id.index++;
0441 id.numid++;
0442 }
0443 }
0444 #else
0445 static inline void add_hash_entries(struct snd_card *card,
0446 struct snd_kcontrol *kcontrol)
0447 {
0448 }
0449 static inline void remove_hash_entries(struct snd_card *card,
0450 struct snd_kcontrol *kcontrol)
0451 {
0452 }
0453 #endif
0454
0455 enum snd_ctl_add_mode {
0456 CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
0457 };
0458
0459
0460 static int __snd_ctl_add_replace(struct snd_card *card,
0461 struct snd_kcontrol *kcontrol,
0462 enum snd_ctl_add_mode mode)
0463 {
0464 struct snd_ctl_elem_id id;
0465 unsigned int idx;
0466 struct snd_kcontrol *old;
0467 int err;
0468
0469 id = kcontrol->id;
0470 if (id.index > UINT_MAX - kcontrol->count)
0471 return -EINVAL;
0472
0473 old = snd_ctl_find_id(card, &id);
0474 if (!old) {
0475 if (mode == CTL_REPLACE)
0476 return -EINVAL;
0477 } else {
0478 if (mode == CTL_ADD_EXCLUSIVE) {
0479 dev_err(card->dev,
0480 "control %i:%i:%i:%s:%i is already present\n",
0481 id.iface, id.device, id.subdevice, id.name,
0482 id.index);
0483 return -EBUSY;
0484 }
0485
0486 err = snd_ctl_remove(card, old);
0487 if (err < 0)
0488 return err;
0489 }
0490
0491 if (snd_ctl_find_hole(card, kcontrol->count) < 0)
0492 return -ENOMEM;
0493
0494 list_add_tail(&kcontrol->list, &card->controls);
0495 card->controls_count += kcontrol->count;
0496 kcontrol->id.numid = card->last_numid + 1;
0497 card->last_numid += kcontrol->count;
0498
0499 add_hash_entries(card, kcontrol);
0500
0501 for (idx = 0; idx < kcontrol->count; idx++)
0502 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_ADD, kcontrol, idx);
0503
0504 return 0;
0505 }
0506
0507 static int snd_ctl_add_replace(struct snd_card *card,
0508 struct snd_kcontrol *kcontrol,
0509 enum snd_ctl_add_mode mode)
0510 {
0511 int err = -EINVAL;
0512
0513 if (! kcontrol)
0514 return err;
0515 if (snd_BUG_ON(!card || !kcontrol->info))
0516 goto error;
0517
0518 down_write(&card->controls_rwsem);
0519 err = __snd_ctl_add_replace(card, kcontrol, mode);
0520 up_write(&card->controls_rwsem);
0521 if (err < 0)
0522 goto error;
0523 return 0;
0524
0525 error:
0526 snd_ctl_free_one(kcontrol);
0527 return err;
0528 }
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
0545 {
0546 return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
0547 }
0548 EXPORT_SYMBOL(snd_ctl_add);
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
0565 bool add_on_replace)
0566 {
0567 return snd_ctl_add_replace(card, kcontrol,
0568 add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
0569 }
0570 EXPORT_SYMBOL(snd_ctl_replace);
0571
0572 static int __snd_ctl_remove(struct snd_card *card,
0573 struct snd_kcontrol *kcontrol,
0574 bool remove_hash)
0575 {
0576 unsigned int idx;
0577
0578 if (snd_BUG_ON(!card || !kcontrol))
0579 return -EINVAL;
0580 list_del(&kcontrol->list);
0581
0582 if (remove_hash)
0583 remove_hash_entries(card, kcontrol);
0584
0585 card->controls_count -= kcontrol->count;
0586 for (idx = 0; idx < kcontrol->count; idx++)
0587 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_REMOVE, kcontrol, idx);
0588 snd_ctl_free_one(kcontrol);
0589 return 0;
0590 }
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600
0601
0602
0603 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
0604 {
0605 return __snd_ctl_remove(card, kcontrol, true);
0606 }
0607 EXPORT_SYMBOL(snd_ctl_remove);
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
0620 {
0621 struct snd_kcontrol *kctl;
0622 int ret;
0623
0624 down_write(&card->controls_rwsem);
0625 kctl = snd_ctl_find_id(card, id);
0626 if (kctl == NULL) {
0627 up_write(&card->controls_rwsem);
0628 return -ENOENT;
0629 }
0630 ret = snd_ctl_remove(card, kctl);
0631 up_write(&card->controls_rwsem);
0632 return ret;
0633 }
0634 EXPORT_SYMBOL(snd_ctl_remove_id);
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
0647 struct snd_ctl_elem_id *id)
0648 {
0649 struct snd_card *card = file->card;
0650 struct snd_kcontrol *kctl;
0651 int idx, ret;
0652
0653 down_write(&card->controls_rwsem);
0654 kctl = snd_ctl_find_id(card, id);
0655 if (kctl == NULL) {
0656 ret = -ENOENT;
0657 goto error;
0658 }
0659 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
0660 ret = -EINVAL;
0661 goto error;
0662 }
0663 for (idx = 0; idx < kctl->count; idx++)
0664 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
0665 ret = -EBUSY;
0666 goto error;
0667 }
0668 ret = snd_ctl_remove(card, kctl);
0669 error:
0670 up_write(&card->controls_rwsem);
0671 return ret;
0672 }
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
0687 int active)
0688 {
0689 struct snd_kcontrol *kctl;
0690 struct snd_kcontrol_volatile *vd;
0691 unsigned int index_offset;
0692 int ret;
0693
0694 down_write(&card->controls_rwsem);
0695 kctl = snd_ctl_find_id(card, id);
0696 if (kctl == NULL) {
0697 ret = -ENOENT;
0698 goto unlock;
0699 }
0700 index_offset = snd_ctl_get_ioff(kctl, id);
0701 vd = &kctl->vd[index_offset];
0702 ret = 0;
0703 if (active) {
0704 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
0705 goto unlock;
0706 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
0707 } else {
0708 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
0709 goto unlock;
0710 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
0711 }
0712 snd_ctl_build_ioff(id, kctl, index_offset);
0713 downgrade_write(&card->controls_rwsem);
0714 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, index_offset);
0715 up_read(&card->controls_rwsem);
0716 return 1;
0717
0718 unlock:
0719 up_write(&card->controls_rwsem);
0720 return ret;
0721 }
0722 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
0736 struct snd_ctl_elem_id *dst_id)
0737 {
0738 struct snd_kcontrol *kctl;
0739
0740 down_write(&card->controls_rwsem);
0741 kctl = snd_ctl_find_id(card, src_id);
0742 if (kctl == NULL) {
0743 up_write(&card->controls_rwsem);
0744 return -ENOENT;
0745 }
0746 remove_hash_entries(card, kctl);
0747 kctl->id = *dst_id;
0748 kctl->id.numid = card->last_numid + 1;
0749 card->last_numid += kctl->count;
0750 add_hash_entries(card, kctl);
0751 up_write(&card->controls_rwsem);
0752 return 0;
0753 }
0754 EXPORT_SYMBOL(snd_ctl_rename_id);
0755
0756 #ifndef CONFIG_SND_CTL_FAST_LOOKUP
0757 static struct snd_kcontrol *
0758 snd_ctl_find_numid_slow(struct snd_card *card, unsigned int numid)
0759 {
0760 struct snd_kcontrol *kctl;
0761
0762 list_for_each_entry(kctl, &card->controls, list) {
0763 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
0764 return kctl;
0765 }
0766 return NULL;
0767 }
0768 #endif
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
0784 {
0785 if (snd_BUG_ON(!card || !numid))
0786 return NULL;
0787 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
0788 return xa_load(&card->ctl_numids, numid);
0789 #else
0790 return snd_ctl_find_numid_slow(card, numid);
0791 #endif
0792 }
0793 EXPORT_SYMBOL(snd_ctl_find_numid);
0794
0795
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
0809 struct snd_ctl_elem_id *id)
0810 {
0811 struct snd_kcontrol *kctl;
0812
0813 if (snd_BUG_ON(!card || !id))
0814 return NULL;
0815 if (id->numid != 0)
0816 return snd_ctl_find_numid(card, id->numid);
0817 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
0818 kctl = xa_load(&card->ctl_hash, get_ctl_id_hash(id));
0819 if (kctl && elem_id_matches(kctl, id))
0820 return kctl;
0821 if (!card->ctl_hash_collision)
0822 return NULL;
0823 #endif
0824
0825 list_for_each_entry(kctl, &card->controls, list)
0826 if (elem_id_matches(kctl, id))
0827 return kctl;
0828
0829 return NULL;
0830 }
0831 EXPORT_SYMBOL(snd_ctl_find_id);
0832
0833 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
0834 unsigned int cmd, void __user *arg)
0835 {
0836 struct snd_ctl_card_info *info;
0837
0838 info = kzalloc(sizeof(*info), GFP_KERNEL);
0839 if (! info)
0840 return -ENOMEM;
0841 down_read(&snd_ioctl_rwsem);
0842 info->card = card->number;
0843 strscpy(info->id, card->id, sizeof(info->id));
0844 strscpy(info->driver, card->driver, sizeof(info->driver));
0845 strscpy(info->name, card->shortname, sizeof(info->name));
0846 strscpy(info->longname, card->longname, sizeof(info->longname));
0847 strscpy(info->mixername, card->mixername, sizeof(info->mixername));
0848 strscpy(info->components, card->components, sizeof(info->components));
0849 up_read(&snd_ioctl_rwsem);
0850 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
0851 kfree(info);
0852 return -EFAULT;
0853 }
0854 kfree(info);
0855 return 0;
0856 }
0857
0858 static int snd_ctl_elem_list(struct snd_card *card,
0859 struct snd_ctl_elem_list *list)
0860 {
0861 struct snd_kcontrol *kctl;
0862 struct snd_ctl_elem_id id;
0863 unsigned int offset, space, jidx;
0864 int err = 0;
0865
0866 offset = list->offset;
0867 space = list->space;
0868
0869 down_read(&card->controls_rwsem);
0870 list->count = card->controls_count;
0871 list->used = 0;
0872 if (space > 0) {
0873 list_for_each_entry(kctl, &card->controls, list) {
0874 if (offset >= kctl->count) {
0875 offset -= kctl->count;
0876 continue;
0877 }
0878 for (jidx = offset; jidx < kctl->count; jidx++) {
0879 snd_ctl_build_ioff(&id, kctl, jidx);
0880 if (copy_to_user(list->pids + list->used, &id,
0881 sizeof(id))) {
0882 err = -EFAULT;
0883 goto out;
0884 }
0885 list->used++;
0886 if (!--space)
0887 goto out;
0888 }
0889 offset = 0;
0890 }
0891 }
0892 out:
0893 up_read(&card->controls_rwsem);
0894 return err;
0895 }
0896
0897 static int snd_ctl_elem_list_user(struct snd_card *card,
0898 struct snd_ctl_elem_list __user *_list)
0899 {
0900 struct snd_ctl_elem_list list;
0901 int err;
0902
0903 if (copy_from_user(&list, _list, sizeof(list)))
0904 return -EFAULT;
0905 err = snd_ctl_elem_list(card, &list);
0906 if (err)
0907 return err;
0908 if (copy_to_user(_list, &list, sizeof(list)))
0909 return -EFAULT;
0910
0911 return 0;
0912 }
0913
0914
0915 static int snd_ctl_check_elem_info(struct snd_card *card,
0916 const struct snd_ctl_elem_info *info)
0917 {
0918 static const unsigned int max_value_counts[] = {
0919 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
0920 [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
0921 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
0922 [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
0923 [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
0924 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
0925 };
0926
0927 if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
0928 info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64) {
0929 if (card)
0930 dev_err(card->dev,
0931 "control %i:%i:%i:%s:%i: invalid type %d\n",
0932 info->id.iface, info->id.device,
0933 info->id.subdevice, info->id.name,
0934 info->id.index, info->type);
0935 return -EINVAL;
0936 }
0937 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
0938 info->value.enumerated.items == 0) {
0939 if (card)
0940 dev_err(card->dev,
0941 "control %i:%i:%i:%s:%i: zero enum items\n",
0942 info->id.iface, info->id.device,
0943 info->id.subdevice, info->id.name,
0944 info->id.index);
0945 return -EINVAL;
0946 }
0947 if (info->count > max_value_counts[info->type]) {
0948 if (card)
0949 dev_err(card->dev,
0950 "control %i:%i:%i:%s:%i: invalid count %d\n",
0951 info->id.iface, info->id.device,
0952 info->id.subdevice, info->id.name,
0953 info->id.index, info->count);
0954 return -EINVAL;
0955 }
0956
0957 return 0;
0958 }
0959
0960
0961 static const unsigned int value_sizes[] = {
0962 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
0963 [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
0964 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
0965 [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
0966 [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
0967 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
0968 };
0969
0970
0971 static void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
0972 struct snd_ctl_elem_info *info,
0973 u32 pattern)
0974 {
0975 size_t offset = value_sizes[info->type] * info->count;
0976
0977 offset = DIV_ROUND_UP(offset, sizeof(u32));
0978 memset32((u32 *)control->value.bytes.data + offset, pattern,
0979 sizeof(control->value) / sizeof(u32) - offset);
0980 }
0981
0982
0983 static int sanity_check_int_value(struct snd_card *card,
0984 const struct snd_ctl_elem_value *control,
0985 const struct snd_ctl_elem_info *info,
0986 int i, bool print_error)
0987 {
0988 long long lval, lmin, lmax, lstep;
0989 u64 rem;
0990
0991 switch (info->type) {
0992 default:
0993 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
0994 lval = control->value.integer.value[i];
0995 lmin = 0;
0996 lmax = 1;
0997 lstep = 0;
0998 break;
0999 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1000 lval = control->value.integer.value[i];
1001 lmin = info->value.integer.min;
1002 lmax = info->value.integer.max;
1003 lstep = info->value.integer.step;
1004 break;
1005 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1006 lval = control->value.integer64.value[i];
1007 lmin = info->value.integer64.min;
1008 lmax = info->value.integer64.max;
1009 lstep = info->value.integer64.step;
1010 break;
1011 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1012 lval = control->value.enumerated.item[i];
1013 lmin = 0;
1014 lmax = info->value.enumerated.items - 1;
1015 lstep = 0;
1016 break;
1017 }
1018
1019 if (lval < lmin || lval > lmax) {
1020 if (print_error)
1021 dev_err(card->dev,
1022 "control %i:%i:%i:%s:%i: value out of range %lld (%lld/%lld) at count %i\n",
1023 control->id.iface, control->id.device,
1024 control->id.subdevice, control->id.name,
1025 control->id.index, lval, lmin, lmax, i);
1026 return -EINVAL;
1027 }
1028 if (lstep) {
1029 div64_u64_rem(lval, lstep, &rem);
1030 if (rem) {
1031 if (print_error)
1032 dev_err(card->dev,
1033 "control %i:%i:%i:%s:%i: unaligned value %lld (step %lld) at count %i\n",
1034 control->id.iface, control->id.device,
1035 control->id.subdevice, control->id.name,
1036 control->id.index, lval, lstep, i);
1037 return -EINVAL;
1038 }
1039 }
1040
1041 return 0;
1042 }
1043
1044
1045 static int sanity_check_input_values(struct snd_card *card,
1046 const struct snd_ctl_elem_value *control,
1047 const struct snd_ctl_elem_info *info,
1048 bool print_error)
1049 {
1050 int i, ret;
1051
1052 switch (info->type) {
1053 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1054 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1055 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1056 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1057 for (i = 0; i < info->count; i++) {
1058 ret = sanity_check_int_value(card, control, info, i,
1059 print_error);
1060 if (ret < 0)
1061 return ret;
1062 }
1063 break;
1064 default:
1065 break;
1066 }
1067
1068 return 0;
1069 }
1070
1071
1072 static int sanity_check_elem_value(struct snd_card *card,
1073 const struct snd_ctl_elem_value *control,
1074 const struct snd_ctl_elem_info *info,
1075 u32 pattern)
1076 {
1077 size_t offset;
1078 int ret;
1079 u32 *p;
1080
1081 ret = sanity_check_input_values(card, control, info, true);
1082 if (ret < 0)
1083 return ret;
1084
1085
1086 offset = value_sizes[info->type] * info->count;
1087 offset = DIV_ROUND_UP(offset, sizeof(u32));
1088 p = (u32 *)control->value.bytes.data + offset;
1089 for (; offset < sizeof(control->value) / sizeof(u32); offset++, p++) {
1090 if (*p != pattern) {
1091 ret = -EINVAL;
1092 break;
1093 }
1094 *p = 0;
1095 }
1096
1097 return ret;
1098 }
1099
1100 static int __snd_ctl_elem_info(struct snd_card *card,
1101 struct snd_kcontrol *kctl,
1102 struct snd_ctl_elem_info *info,
1103 struct snd_ctl_file *ctl)
1104 {
1105 struct snd_kcontrol_volatile *vd;
1106 unsigned int index_offset;
1107 int result;
1108
1109 #ifdef CONFIG_SND_DEBUG
1110 info->access = 0;
1111 #endif
1112 result = snd_power_ref_and_wait(card);
1113 if (!result)
1114 result = kctl->info(kctl, info);
1115 snd_power_unref(card);
1116 if (result >= 0) {
1117 snd_BUG_ON(info->access);
1118 index_offset = snd_ctl_get_ioff(kctl, &info->id);
1119 vd = &kctl->vd[index_offset];
1120 snd_ctl_build_ioff(&info->id, kctl, index_offset);
1121 info->access = vd->access;
1122 if (vd->owner) {
1123 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
1124 if (vd->owner == ctl)
1125 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
1126 info->owner = pid_vnr(vd->owner->pid);
1127 } else {
1128 info->owner = -1;
1129 }
1130 if (!snd_ctl_skip_validation(info) &&
1131 snd_ctl_check_elem_info(card, info) < 0)
1132 result = -EINVAL;
1133 }
1134 return result;
1135 }
1136
1137 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
1138 struct snd_ctl_elem_info *info)
1139 {
1140 struct snd_card *card = ctl->card;
1141 struct snd_kcontrol *kctl;
1142 int result;
1143
1144 down_read(&card->controls_rwsem);
1145 kctl = snd_ctl_find_id(card, &info->id);
1146 if (kctl == NULL)
1147 result = -ENOENT;
1148 else
1149 result = __snd_ctl_elem_info(card, kctl, info, ctl);
1150 up_read(&card->controls_rwsem);
1151 return result;
1152 }
1153
1154 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
1155 struct snd_ctl_elem_info __user *_info)
1156 {
1157 struct snd_ctl_elem_info info;
1158 int result;
1159
1160 if (copy_from_user(&info, _info, sizeof(info)))
1161 return -EFAULT;
1162 result = snd_ctl_elem_info(ctl, &info);
1163 if (result < 0)
1164 return result;
1165
1166 info.access &= ~(SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK|
1167 SNDRV_CTL_ELEM_ACCESS_LED_MASK);
1168 if (copy_to_user(_info, &info, sizeof(info)))
1169 return -EFAULT;
1170 return result;
1171 }
1172
1173 static int snd_ctl_elem_read(struct snd_card *card,
1174 struct snd_ctl_elem_value *control)
1175 {
1176 struct snd_kcontrol *kctl;
1177 struct snd_kcontrol_volatile *vd;
1178 unsigned int index_offset;
1179 struct snd_ctl_elem_info info;
1180 const u32 pattern = 0xdeadbeef;
1181 int ret;
1182
1183 kctl = snd_ctl_find_id(card, &control->id);
1184 if (kctl == NULL)
1185 return -ENOENT;
1186
1187 index_offset = snd_ctl_get_ioff(kctl, &control->id);
1188 vd = &kctl->vd[index_offset];
1189 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
1190 return -EPERM;
1191
1192 snd_ctl_build_ioff(&control->id, kctl, index_offset);
1193
1194 #ifdef CONFIG_SND_CTL_DEBUG
1195
1196 memset(&info, 0, sizeof(info));
1197 info.id = control->id;
1198 ret = __snd_ctl_elem_info(card, kctl, &info, NULL);
1199 if (ret < 0)
1200 return ret;
1201 #endif
1202
1203 if (!snd_ctl_skip_validation(&info))
1204 fill_remaining_elem_value(control, &info, pattern);
1205 ret = snd_power_ref_and_wait(card);
1206 if (!ret)
1207 ret = kctl->get(kctl, control);
1208 snd_power_unref(card);
1209 if (ret < 0)
1210 return ret;
1211 if (!snd_ctl_skip_validation(&info) &&
1212 sanity_check_elem_value(card, control, &info, pattern) < 0) {
1213 dev_err(card->dev,
1214 "control %i:%i:%i:%s:%i: access overflow\n",
1215 control->id.iface, control->id.device,
1216 control->id.subdevice, control->id.name,
1217 control->id.index);
1218 return -EINVAL;
1219 }
1220 return ret;
1221 }
1222
1223 static int snd_ctl_elem_read_user(struct snd_card *card,
1224 struct snd_ctl_elem_value __user *_control)
1225 {
1226 struct snd_ctl_elem_value *control;
1227 int result;
1228
1229 control = memdup_user(_control, sizeof(*control));
1230 if (IS_ERR(control))
1231 return PTR_ERR(control);
1232
1233 down_read(&card->controls_rwsem);
1234 result = snd_ctl_elem_read(card, control);
1235 up_read(&card->controls_rwsem);
1236 if (result < 0)
1237 goto error;
1238
1239 if (copy_to_user(_control, control, sizeof(*control)))
1240 result = -EFAULT;
1241 error:
1242 kfree(control);
1243 return result;
1244 }
1245
1246 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
1247 struct snd_ctl_elem_value *control)
1248 {
1249 struct snd_kcontrol *kctl;
1250 struct snd_kcontrol_volatile *vd;
1251 unsigned int index_offset;
1252 int result;
1253
1254 down_write(&card->controls_rwsem);
1255 kctl = snd_ctl_find_id(card, &control->id);
1256 if (kctl == NULL) {
1257 up_write(&card->controls_rwsem);
1258 return -ENOENT;
1259 }
1260
1261 index_offset = snd_ctl_get_ioff(kctl, &control->id);
1262 vd = &kctl->vd[index_offset];
1263 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
1264 (file && vd->owner && vd->owner != file)) {
1265 up_write(&card->controls_rwsem);
1266 return -EPERM;
1267 }
1268
1269 snd_ctl_build_ioff(&control->id, kctl, index_offset);
1270 result = snd_power_ref_and_wait(card);
1271
1272 if (IS_ENABLED(CONFIG_SND_CTL_INPUT_VALIDATION) && !result) {
1273 struct snd_ctl_elem_info info;
1274
1275 memset(&info, 0, sizeof(info));
1276 info.id = control->id;
1277 result = __snd_ctl_elem_info(card, kctl, &info, NULL);
1278 if (!result)
1279 result = sanity_check_input_values(card, control, &info,
1280 false);
1281 }
1282 if (!result)
1283 result = kctl->put(kctl, control);
1284 snd_power_unref(card);
1285 if (result < 0) {
1286 up_write(&card->controls_rwsem);
1287 return result;
1288 }
1289
1290 if (result > 0) {
1291 downgrade_write(&card->controls_rwsem);
1292 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, index_offset);
1293 up_read(&card->controls_rwsem);
1294 } else {
1295 up_write(&card->controls_rwsem);
1296 }
1297
1298 return 0;
1299 }
1300
1301 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
1302 struct snd_ctl_elem_value __user *_control)
1303 {
1304 struct snd_ctl_elem_value *control;
1305 struct snd_card *card;
1306 int result;
1307
1308 control = memdup_user(_control, sizeof(*control));
1309 if (IS_ERR(control))
1310 return PTR_ERR(control);
1311
1312 card = file->card;
1313 result = snd_ctl_elem_write(card, file, control);
1314 if (result < 0)
1315 goto error;
1316
1317 if (copy_to_user(_control, control, sizeof(*control)))
1318 result = -EFAULT;
1319 error:
1320 kfree(control);
1321 return result;
1322 }
1323
1324 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
1325 struct snd_ctl_elem_id __user *_id)
1326 {
1327 struct snd_card *card = file->card;
1328 struct snd_ctl_elem_id id;
1329 struct snd_kcontrol *kctl;
1330 struct snd_kcontrol_volatile *vd;
1331 int result;
1332
1333 if (copy_from_user(&id, _id, sizeof(id)))
1334 return -EFAULT;
1335 down_write(&card->controls_rwsem);
1336 kctl = snd_ctl_find_id(card, &id);
1337 if (kctl == NULL) {
1338 result = -ENOENT;
1339 } else {
1340 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1341 if (vd->owner != NULL)
1342 result = -EBUSY;
1343 else {
1344 vd->owner = file;
1345 result = 0;
1346 }
1347 }
1348 up_write(&card->controls_rwsem);
1349 return result;
1350 }
1351
1352 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
1353 struct snd_ctl_elem_id __user *_id)
1354 {
1355 struct snd_card *card = file->card;
1356 struct snd_ctl_elem_id id;
1357 struct snd_kcontrol *kctl;
1358 struct snd_kcontrol_volatile *vd;
1359 int result;
1360
1361 if (copy_from_user(&id, _id, sizeof(id)))
1362 return -EFAULT;
1363 down_write(&card->controls_rwsem);
1364 kctl = snd_ctl_find_id(card, &id);
1365 if (kctl == NULL) {
1366 result = -ENOENT;
1367 } else {
1368 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1369 if (vd->owner == NULL)
1370 result = -EINVAL;
1371 else if (vd->owner != file)
1372 result = -EPERM;
1373 else {
1374 vd->owner = NULL;
1375 result = 0;
1376 }
1377 }
1378 up_write(&card->controls_rwsem);
1379 return result;
1380 }
1381
1382 struct user_element {
1383 struct snd_ctl_elem_info info;
1384 struct snd_card *card;
1385 char *elem_data;
1386 unsigned long elem_data_size;
1387 void *tlv_data;
1388 unsigned long tlv_data_size;
1389 void *priv_data;
1390 };
1391
1392
1393 static bool check_user_elem_overflow(struct snd_card *card, ssize_t add)
1394 {
1395 return (ssize_t)card->user_ctl_alloc_size + add > max_user_ctl_alloc_size;
1396 }
1397
1398 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1399 struct snd_ctl_elem_info *uinfo)
1400 {
1401 struct user_element *ue = kcontrol->private_data;
1402 unsigned int offset;
1403
1404 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1405 *uinfo = ue->info;
1406 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1407
1408 return 0;
1409 }
1410
1411 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1412 struct snd_ctl_elem_info *uinfo)
1413 {
1414 struct user_element *ue = kcontrol->private_data;
1415 const char *names;
1416 unsigned int item;
1417 unsigned int offset;
1418
1419 item = uinfo->value.enumerated.item;
1420
1421 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1422 *uinfo = ue->info;
1423 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1424
1425 item = min(item, uinfo->value.enumerated.items - 1);
1426 uinfo->value.enumerated.item = item;
1427
1428 names = ue->priv_data;
1429 for (; item > 0; --item)
1430 names += strlen(names) + 1;
1431 strcpy(uinfo->value.enumerated.name, names);
1432
1433 return 0;
1434 }
1435
1436 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1437 struct snd_ctl_elem_value *ucontrol)
1438 {
1439 struct user_element *ue = kcontrol->private_data;
1440 unsigned int size = ue->elem_data_size;
1441 char *src = ue->elem_data +
1442 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1443
1444 memcpy(&ucontrol->value, src, size);
1445 return 0;
1446 }
1447
1448 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1449 struct snd_ctl_elem_value *ucontrol)
1450 {
1451 int change;
1452 struct user_element *ue = kcontrol->private_data;
1453 unsigned int size = ue->elem_data_size;
1454 char *dst = ue->elem_data +
1455 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1456
1457 change = memcmp(&ucontrol->value, dst, size) != 0;
1458 if (change)
1459 memcpy(dst, &ucontrol->value, size);
1460 return change;
1461 }
1462
1463
1464 static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1465 unsigned int size)
1466 {
1467 struct user_element *ue = kctl->private_data;
1468 unsigned int *container;
1469 unsigned int mask = 0;
1470 int i;
1471 int change;
1472
1473 if (size > 1024 * 128)
1474 return -EINVAL;
1475
1476
1477 if (check_user_elem_overflow(ue->card, (ssize_t)(size - ue->tlv_data_size)))
1478 return -ENOMEM;
1479
1480 container = vmemdup_user(buf, size);
1481 if (IS_ERR(container))
1482 return PTR_ERR(container);
1483
1484 change = ue->tlv_data_size != size;
1485 if (!change)
1486 change = memcmp(ue->tlv_data, container, size) != 0;
1487 if (!change) {
1488 kvfree(container);
1489 return 0;
1490 }
1491
1492 if (ue->tlv_data == NULL) {
1493
1494 for (i = 0; i < kctl->count; ++i)
1495 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1496 mask = SNDRV_CTL_EVENT_MASK_INFO;
1497 } else {
1498 ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
1499 ue->tlv_data_size = 0;
1500 kvfree(ue->tlv_data);
1501 }
1502
1503 ue->tlv_data = container;
1504 ue->tlv_data_size = size;
1505
1506 ue->card->user_ctl_alloc_size += size;
1507
1508 mask |= SNDRV_CTL_EVENT_MASK_TLV;
1509 for (i = 0; i < kctl->count; ++i)
1510 snd_ctl_notify_one(ue->card, mask, kctl, i);
1511
1512 return change;
1513 }
1514
1515 static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1516 unsigned int size)
1517 {
1518 struct user_element *ue = kctl->private_data;
1519
1520 if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1521 return -ENXIO;
1522
1523 if (size < ue->tlv_data_size)
1524 return -ENOSPC;
1525
1526 if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1527 return -EFAULT;
1528
1529 return 0;
1530 }
1531
1532 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1533 unsigned int size, unsigned int __user *buf)
1534 {
1535 if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1536 return replace_user_tlv(kctl, buf, size);
1537 else
1538 return read_user_tlv(kctl, buf, size);
1539 }
1540
1541
1542 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1543 {
1544 char *names, *p;
1545 size_t buf_len, name_len;
1546 unsigned int i;
1547 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1548
1549 buf_len = ue->info.value.enumerated.names_length;
1550 if (buf_len > 64 * 1024)
1551 return -EINVAL;
1552
1553 if (check_user_elem_overflow(ue->card, buf_len))
1554 return -ENOMEM;
1555 names = vmemdup_user((const void __user *)user_ptrval, buf_len);
1556 if (IS_ERR(names))
1557 return PTR_ERR(names);
1558
1559
1560 p = names;
1561 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1562 name_len = strnlen(p, buf_len);
1563 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1564 kvfree(names);
1565 return -EINVAL;
1566 }
1567 p += name_len + 1;
1568 buf_len -= name_len + 1;
1569 }
1570
1571 ue->priv_data = names;
1572 ue->info.value.enumerated.names_ptr = 0;
1573
1574 ue->card->user_ctl_alloc_size += ue->info.value.enumerated.names_length;
1575
1576 return 0;
1577 }
1578
1579 static size_t compute_user_elem_size(size_t size, unsigned int count)
1580 {
1581 return sizeof(struct user_element) + size * count;
1582 }
1583
1584 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1585 {
1586 struct user_element *ue = kcontrol->private_data;
1587
1588
1589 ue->card->user_ctl_alloc_size -= compute_user_elem_size(ue->elem_data_size, kcontrol->count);
1590 ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
1591 if (ue->priv_data)
1592 ue->card->user_ctl_alloc_size -= ue->info.value.enumerated.names_length;
1593
1594 kvfree(ue->tlv_data);
1595 kvfree(ue->priv_data);
1596 kfree(ue);
1597 }
1598
1599 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1600 struct snd_ctl_elem_info *info, int replace)
1601 {
1602 struct snd_card *card = file->card;
1603 struct snd_kcontrol *kctl;
1604 unsigned int count;
1605 unsigned int access;
1606 long private_size;
1607 size_t alloc_size;
1608 struct user_element *ue;
1609 unsigned int offset;
1610 int err;
1611
1612 if (!*info->id.name)
1613 return -EINVAL;
1614 if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1615 return -EINVAL;
1616
1617
1618 if (replace) {
1619 info->id.numid = 0;
1620 err = snd_ctl_remove_user_ctl(file, &info->id);
1621 if (err)
1622 return err;
1623 }
1624
1625
1626 count = info->owner;
1627 if (count == 0)
1628 count = 1;
1629
1630
1631 access = info->access;
1632 if (access == 0)
1633 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1634 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1635 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1636 SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1637
1638
1639 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1640 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1641 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1642
1643
1644
1645
1646
1647
1648 err = snd_ctl_check_elem_info(NULL, info);
1649 if (err < 0)
1650 return err;
1651
1652 if (info->count < 1)
1653 return -EINVAL;
1654 private_size = value_sizes[info->type] * info->count;
1655 alloc_size = compute_user_elem_size(private_size, count);
1656
1657 down_write(&card->controls_rwsem);
1658 if (check_user_elem_overflow(card, alloc_size)) {
1659 err = -ENOMEM;
1660 goto unlock;
1661 }
1662
1663
1664
1665
1666
1667
1668
1669 err = snd_ctl_new(&kctl, count, access, file);
1670 if (err < 0)
1671 goto unlock;
1672 memcpy(&kctl->id, &info->id, sizeof(kctl->id));
1673 ue = kzalloc(alloc_size, GFP_KERNEL);
1674 if (!ue) {
1675 kfree(kctl);
1676 err = -ENOMEM;
1677 goto unlock;
1678 }
1679 kctl->private_data = ue;
1680 kctl->private_free = snd_ctl_elem_user_free;
1681
1682
1683 card->user_ctl_alloc_size += alloc_size;
1684
1685
1686 ue->card = card;
1687 ue->info = *info;
1688 ue->info.access = 0;
1689 ue->elem_data = (char *)ue + sizeof(*ue);
1690 ue->elem_data_size = private_size;
1691 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1692 err = snd_ctl_elem_init_enum_names(ue);
1693 if (err < 0) {
1694 snd_ctl_free_one(kctl);
1695 goto unlock;
1696 }
1697 }
1698
1699
1700 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1701 kctl->info = snd_ctl_elem_user_enum_info;
1702 else
1703 kctl->info = snd_ctl_elem_user_info;
1704 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1705 kctl->get = snd_ctl_elem_user_get;
1706 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1707 kctl->put = snd_ctl_elem_user_put;
1708 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1709 kctl->tlv.c = snd_ctl_elem_user_tlv;
1710
1711
1712 err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
1713 if (err < 0) {
1714 snd_ctl_free_one(kctl);
1715 goto unlock;
1716 }
1717 offset = snd_ctl_get_ioff(kctl, &info->id);
1718 snd_ctl_build_ioff(&info->id, kctl, offset);
1719
1720
1721
1722
1723
1724
1725
1726 unlock:
1727 up_write(&card->controls_rwsem);
1728 return err;
1729 }
1730
1731 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1732 struct snd_ctl_elem_info __user *_info, int replace)
1733 {
1734 struct snd_ctl_elem_info info;
1735 int err;
1736
1737 if (copy_from_user(&info, _info, sizeof(info)))
1738 return -EFAULT;
1739 err = snd_ctl_elem_add(file, &info, replace);
1740 if (err < 0)
1741 return err;
1742 if (copy_to_user(_info, &info, sizeof(info))) {
1743 snd_ctl_remove_user_ctl(file, &info.id);
1744 return -EFAULT;
1745 }
1746
1747 return 0;
1748 }
1749
1750 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1751 struct snd_ctl_elem_id __user *_id)
1752 {
1753 struct snd_ctl_elem_id id;
1754
1755 if (copy_from_user(&id, _id, sizeof(id)))
1756 return -EFAULT;
1757 return snd_ctl_remove_user_ctl(file, &id);
1758 }
1759
1760 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1761 {
1762 int subscribe;
1763 if (get_user(subscribe, ptr))
1764 return -EFAULT;
1765 if (subscribe < 0) {
1766 subscribe = file->subscribed;
1767 if (put_user(subscribe, ptr))
1768 return -EFAULT;
1769 return 0;
1770 }
1771 if (subscribe) {
1772 file->subscribed = 1;
1773 return 0;
1774 } else if (file->subscribed) {
1775 snd_ctl_empty_read_queue(file);
1776 file->subscribed = 0;
1777 }
1778 return 0;
1779 }
1780
1781 static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1782 struct snd_kcontrol *kctl,
1783 struct snd_ctl_elem_id *id,
1784 unsigned int __user *buf, unsigned int size)
1785 {
1786 static const struct {
1787 int op;
1788 int perm;
1789 } pairs[] = {
1790 {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1791 {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1792 {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1793 };
1794 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1795 int i, ret;
1796
1797
1798 for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1799 if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1800 break;
1801 }
1802 if (i == ARRAY_SIZE(pairs))
1803 return -ENXIO;
1804
1805 if (kctl->tlv.c == NULL)
1806 return -ENXIO;
1807
1808
1809 if (op_flag != SNDRV_CTL_TLV_OP_READ &&
1810 vd->owner != NULL && vd->owner != file)
1811 return -EPERM;
1812
1813 ret = snd_power_ref_and_wait(file->card);
1814 if (!ret)
1815 ret = kctl->tlv.c(kctl, op_flag, size, buf);
1816 snd_power_unref(file->card);
1817 return ret;
1818 }
1819
1820 static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1821 unsigned int __user *buf, unsigned int size)
1822 {
1823 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1824 unsigned int len;
1825
1826 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1827 return -ENXIO;
1828
1829 if (kctl->tlv.p == NULL)
1830 return -ENXIO;
1831
1832 len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1833 if (size < len)
1834 return -ENOMEM;
1835
1836 if (copy_to_user(buf, kctl->tlv.p, len))
1837 return -EFAULT;
1838
1839 return 0;
1840 }
1841
1842 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1843 struct snd_ctl_tlv __user *buf,
1844 int op_flag)
1845 {
1846 struct snd_ctl_tlv header;
1847 unsigned int __user *container;
1848 unsigned int container_size;
1849 struct snd_kcontrol *kctl;
1850 struct snd_ctl_elem_id id;
1851 struct snd_kcontrol_volatile *vd;
1852
1853 if (copy_from_user(&header, buf, sizeof(header)))
1854 return -EFAULT;
1855
1856
1857 if (header.numid == 0)
1858 return -EINVAL;
1859
1860
1861 if (header.length < sizeof(unsigned int) * 2)
1862 return -EINVAL;
1863 container_size = header.length;
1864 container = buf->tlv;
1865
1866 kctl = snd_ctl_find_numid(file->card, header.numid);
1867 if (kctl == NULL)
1868 return -ENOENT;
1869
1870
1871 id = kctl->id;
1872 snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1873 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1874
1875 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1876 return call_tlv_handler(file, op_flag, kctl, &id, container,
1877 container_size);
1878 } else {
1879 if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1880 return read_tlv_buf(kctl, &id, container,
1881 container_size);
1882 }
1883 }
1884
1885
1886 return -ENXIO;
1887 }
1888
1889 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1890 {
1891 struct snd_ctl_file *ctl;
1892 struct snd_card *card;
1893 struct snd_kctl_ioctl *p;
1894 void __user *argp = (void __user *)arg;
1895 int __user *ip = argp;
1896 int err;
1897
1898 ctl = file->private_data;
1899 card = ctl->card;
1900 if (snd_BUG_ON(!card))
1901 return -ENXIO;
1902 switch (cmd) {
1903 case SNDRV_CTL_IOCTL_PVERSION:
1904 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1905 case SNDRV_CTL_IOCTL_CARD_INFO:
1906 return snd_ctl_card_info(card, ctl, cmd, argp);
1907 case SNDRV_CTL_IOCTL_ELEM_LIST:
1908 return snd_ctl_elem_list_user(card, argp);
1909 case SNDRV_CTL_IOCTL_ELEM_INFO:
1910 return snd_ctl_elem_info_user(ctl, argp);
1911 case SNDRV_CTL_IOCTL_ELEM_READ:
1912 return snd_ctl_elem_read_user(card, argp);
1913 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1914 return snd_ctl_elem_write_user(ctl, argp);
1915 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1916 return snd_ctl_elem_lock(ctl, argp);
1917 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1918 return snd_ctl_elem_unlock(ctl, argp);
1919 case SNDRV_CTL_IOCTL_ELEM_ADD:
1920 return snd_ctl_elem_add_user(ctl, argp, 0);
1921 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1922 return snd_ctl_elem_add_user(ctl, argp, 1);
1923 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1924 return snd_ctl_elem_remove(ctl, argp);
1925 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1926 return snd_ctl_subscribe_events(ctl, ip);
1927 case SNDRV_CTL_IOCTL_TLV_READ:
1928 down_read(&ctl->card->controls_rwsem);
1929 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1930 up_read(&ctl->card->controls_rwsem);
1931 return err;
1932 case SNDRV_CTL_IOCTL_TLV_WRITE:
1933 down_write(&ctl->card->controls_rwsem);
1934 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1935 up_write(&ctl->card->controls_rwsem);
1936 return err;
1937 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1938 down_write(&ctl->card->controls_rwsem);
1939 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1940 up_write(&ctl->card->controls_rwsem);
1941 return err;
1942 case SNDRV_CTL_IOCTL_POWER:
1943 return -ENOPROTOOPT;
1944 case SNDRV_CTL_IOCTL_POWER_STATE:
1945 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1946 }
1947 down_read(&snd_ioctl_rwsem);
1948 list_for_each_entry(p, &snd_control_ioctls, list) {
1949 err = p->fioctl(card, ctl, cmd, arg);
1950 if (err != -ENOIOCTLCMD) {
1951 up_read(&snd_ioctl_rwsem);
1952 return err;
1953 }
1954 }
1955 up_read(&snd_ioctl_rwsem);
1956 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
1957 return -ENOTTY;
1958 }
1959
1960 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1961 size_t count, loff_t * offset)
1962 {
1963 struct snd_ctl_file *ctl;
1964 int err = 0;
1965 ssize_t result = 0;
1966
1967 ctl = file->private_data;
1968 if (snd_BUG_ON(!ctl || !ctl->card))
1969 return -ENXIO;
1970 if (!ctl->subscribed)
1971 return -EBADFD;
1972 if (count < sizeof(struct snd_ctl_event))
1973 return -EINVAL;
1974 spin_lock_irq(&ctl->read_lock);
1975 while (count >= sizeof(struct snd_ctl_event)) {
1976 struct snd_ctl_event ev;
1977 struct snd_kctl_event *kev;
1978 while (list_empty(&ctl->events)) {
1979 wait_queue_entry_t wait;
1980 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1981 err = -EAGAIN;
1982 goto __end_lock;
1983 }
1984 init_waitqueue_entry(&wait, current);
1985 add_wait_queue(&ctl->change_sleep, &wait);
1986 set_current_state(TASK_INTERRUPTIBLE);
1987 spin_unlock_irq(&ctl->read_lock);
1988 schedule();
1989 remove_wait_queue(&ctl->change_sleep, &wait);
1990 if (ctl->card->shutdown)
1991 return -ENODEV;
1992 if (signal_pending(current))
1993 return -ERESTARTSYS;
1994 spin_lock_irq(&ctl->read_lock);
1995 }
1996 kev = snd_kctl_event(ctl->events.next);
1997 ev.type = SNDRV_CTL_EVENT_ELEM;
1998 ev.data.elem.mask = kev->mask;
1999 ev.data.elem.id = kev->id;
2000 list_del(&kev->list);
2001 spin_unlock_irq(&ctl->read_lock);
2002 kfree(kev);
2003 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
2004 err = -EFAULT;
2005 goto __end;
2006 }
2007 spin_lock_irq(&ctl->read_lock);
2008 buffer += sizeof(struct snd_ctl_event);
2009 count -= sizeof(struct snd_ctl_event);
2010 result += sizeof(struct snd_ctl_event);
2011 }
2012 __end_lock:
2013 spin_unlock_irq(&ctl->read_lock);
2014 __end:
2015 return result > 0 ? result : err;
2016 }
2017
2018 static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
2019 {
2020 __poll_t mask;
2021 struct snd_ctl_file *ctl;
2022
2023 ctl = file->private_data;
2024 if (!ctl->subscribed)
2025 return 0;
2026 poll_wait(file, &ctl->change_sleep, wait);
2027
2028 mask = 0;
2029 if (!list_empty(&ctl->events))
2030 mask |= EPOLLIN | EPOLLRDNORM;
2031
2032 return mask;
2033 }
2034
2035
2036
2037
2038
2039 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
2040 {
2041 struct snd_kctl_ioctl *pn;
2042
2043 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
2044 if (pn == NULL)
2045 return -ENOMEM;
2046 pn->fioctl = fcn;
2047 down_write(&snd_ioctl_rwsem);
2048 list_add_tail(&pn->list, lists);
2049 up_write(&snd_ioctl_rwsem);
2050 return 0;
2051 }
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
2062 {
2063 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
2064 }
2065 EXPORT_SYMBOL(snd_ctl_register_ioctl);
2066
2067 #ifdef CONFIG_COMPAT
2068
2069
2070
2071
2072
2073
2074
2075 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
2076 {
2077 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
2078 }
2079 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
2080 #endif
2081
2082
2083
2084
2085 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
2086 struct list_head *lists)
2087 {
2088 struct snd_kctl_ioctl *p;
2089
2090 if (snd_BUG_ON(!fcn))
2091 return -EINVAL;
2092 down_write(&snd_ioctl_rwsem);
2093 list_for_each_entry(p, lists, list) {
2094 if (p->fioctl == fcn) {
2095 list_del(&p->list);
2096 up_write(&snd_ioctl_rwsem);
2097 kfree(p);
2098 return 0;
2099 }
2100 }
2101 up_write(&snd_ioctl_rwsem);
2102 snd_BUG();
2103 return -EINVAL;
2104 }
2105
2106
2107
2108
2109
2110
2111
2112 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
2113 {
2114 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
2115 }
2116 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
2117
2118 #ifdef CONFIG_COMPAT
2119
2120
2121
2122
2123
2124
2125
2126 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
2127 {
2128 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
2129 }
2130 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
2131 #endif
2132
2133 static int snd_ctl_fasync(int fd, struct file * file, int on)
2134 {
2135 struct snd_ctl_file *ctl;
2136
2137 ctl = file->private_data;
2138 return snd_fasync_helper(fd, file, on, &ctl->fasync);
2139 }
2140
2141
2142
2143
2144 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
2145 {
2146 struct snd_ctl_file *kctl;
2147 int subdevice = -1;
2148 unsigned long flags;
2149
2150 read_lock_irqsave(&card->ctl_files_rwlock, flags);
2151 list_for_each_entry(kctl, &card->ctl_files, list) {
2152 if (kctl->pid == task_pid(current)) {
2153 subdevice = kctl->preferred_subdevice[type];
2154 if (subdevice != -1)
2155 break;
2156 }
2157 }
2158 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2159 return subdevice;
2160 }
2161 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
2162
2163
2164
2165
2166 #ifdef CONFIG_COMPAT
2167 #include "control_compat.c"
2168 #else
2169 #define snd_ctl_ioctl_compat NULL
2170 #endif
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182 int snd_ctl_request_layer(const char *module_name)
2183 {
2184 struct snd_ctl_layer_ops *lops;
2185
2186 if (module_name == NULL)
2187 return 0;
2188 down_read(&snd_ctl_layer_rwsem);
2189 for (lops = snd_ctl_layer; lops; lops = lops->next)
2190 if (strcmp(lops->module_name, module_name) == 0)
2191 break;
2192 up_read(&snd_ctl_layer_rwsem);
2193 if (lops)
2194 return 0;
2195 return request_module(module_name);
2196 }
2197 EXPORT_SYMBOL_GPL(snd_ctl_request_layer);
2198
2199
2200
2201
2202
2203
2204
2205
2206 void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops)
2207 {
2208 struct snd_card *card;
2209 int card_number;
2210
2211 down_write(&snd_ctl_layer_rwsem);
2212 lops->next = snd_ctl_layer;
2213 snd_ctl_layer = lops;
2214 up_write(&snd_ctl_layer_rwsem);
2215 for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
2216 card = snd_card_ref(card_number);
2217 if (card) {
2218 down_read(&card->controls_rwsem);
2219 lops->lregister(card);
2220 up_read(&card->controls_rwsem);
2221 snd_card_unref(card);
2222 }
2223 }
2224 }
2225 EXPORT_SYMBOL_GPL(snd_ctl_register_layer);
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235 void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops)
2236 {
2237 struct snd_ctl_layer_ops *lops2, *prev_lops2;
2238
2239 down_write(&snd_ctl_layer_rwsem);
2240 for (lops2 = snd_ctl_layer, prev_lops2 = NULL; lops2; lops2 = lops2->next) {
2241 if (lops2 == lops) {
2242 if (!prev_lops2)
2243 snd_ctl_layer = lops->next;
2244 else
2245 prev_lops2->next = lops->next;
2246 break;
2247 }
2248 prev_lops2 = lops2;
2249 }
2250 up_write(&snd_ctl_layer_rwsem);
2251 }
2252 EXPORT_SYMBOL_GPL(snd_ctl_disconnect_layer);
2253
2254
2255
2256
2257
2258 static const struct file_operations snd_ctl_f_ops =
2259 {
2260 .owner = THIS_MODULE,
2261 .read = snd_ctl_read,
2262 .open = snd_ctl_open,
2263 .release = snd_ctl_release,
2264 .llseek = no_llseek,
2265 .poll = snd_ctl_poll,
2266 .unlocked_ioctl = snd_ctl_ioctl,
2267 .compat_ioctl = snd_ctl_ioctl_compat,
2268 .fasync = snd_ctl_fasync,
2269 };
2270
2271
2272
2273
2274 static int snd_ctl_dev_register(struct snd_device *device)
2275 {
2276 struct snd_card *card = device->device_data;
2277 struct snd_ctl_layer_ops *lops;
2278 int err;
2279
2280 err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
2281 &snd_ctl_f_ops, card, &card->ctl_dev);
2282 if (err < 0)
2283 return err;
2284 down_read(&card->controls_rwsem);
2285 down_read(&snd_ctl_layer_rwsem);
2286 for (lops = snd_ctl_layer; lops; lops = lops->next)
2287 lops->lregister(card);
2288 up_read(&snd_ctl_layer_rwsem);
2289 up_read(&card->controls_rwsem);
2290 return 0;
2291 }
2292
2293
2294
2295
2296 static int snd_ctl_dev_disconnect(struct snd_device *device)
2297 {
2298 struct snd_card *card = device->device_data;
2299 struct snd_ctl_file *ctl;
2300 struct snd_ctl_layer_ops *lops;
2301 unsigned long flags;
2302
2303 read_lock_irqsave(&card->ctl_files_rwlock, flags);
2304 list_for_each_entry(ctl, &card->ctl_files, list) {
2305 wake_up(&ctl->change_sleep);
2306 snd_kill_fasync(ctl->fasync, SIGIO, POLL_ERR);
2307 }
2308 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2309
2310 down_read(&card->controls_rwsem);
2311 down_read(&snd_ctl_layer_rwsem);
2312 for (lops = snd_ctl_layer; lops; lops = lops->next)
2313 lops->ldisconnect(card);
2314 up_read(&snd_ctl_layer_rwsem);
2315 up_read(&card->controls_rwsem);
2316
2317 return snd_unregister_device(&card->ctl_dev);
2318 }
2319
2320
2321
2322
2323 static int snd_ctl_dev_free(struct snd_device *device)
2324 {
2325 struct snd_card *card = device->device_data;
2326 struct snd_kcontrol *control;
2327
2328 down_write(&card->controls_rwsem);
2329 while (!list_empty(&card->controls)) {
2330 control = snd_kcontrol(card->controls.next);
2331 __snd_ctl_remove(card, control, false);
2332 }
2333
2334 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
2335 xa_destroy(&card->ctl_numids);
2336 xa_destroy(&card->ctl_hash);
2337 #endif
2338 up_write(&card->controls_rwsem);
2339 put_device(&card->ctl_dev);
2340 return 0;
2341 }
2342
2343
2344
2345
2346
2347 int snd_ctl_create(struct snd_card *card)
2348 {
2349 static const struct snd_device_ops ops = {
2350 .dev_free = snd_ctl_dev_free,
2351 .dev_register = snd_ctl_dev_register,
2352 .dev_disconnect = snd_ctl_dev_disconnect,
2353 };
2354 int err;
2355
2356 if (snd_BUG_ON(!card))
2357 return -ENXIO;
2358 if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
2359 return -ENXIO;
2360
2361 snd_device_initialize(&card->ctl_dev, card);
2362 dev_set_name(&card->ctl_dev, "controlC%d", card->number);
2363
2364 err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
2365 if (err < 0)
2366 put_device(&card->ctl_dev);
2367 return err;
2368 }
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_info *uinfo)
2387 {
2388 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2389 uinfo->count = 1;
2390 uinfo->value.integer.min = 0;
2391 uinfo->value.integer.max = 1;
2392 return 0;
2393 }
2394 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
2408 struct snd_ctl_elem_info *uinfo)
2409 {
2410 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2411 uinfo->count = 2;
2412 uinfo->value.integer.min = 0;
2413 uinfo->value.integer.max = 1;
2414 return 0;
2415 }
2416 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
2432 unsigned int items, const char *const names[])
2433 {
2434 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2435 info->count = channels;
2436 info->value.enumerated.items = items;
2437 if (!items)
2438 return 0;
2439 if (info->value.enumerated.item >= items)
2440 info->value.enumerated.item = items - 1;
2441 WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
2442 "ALSA: too long item name '%s'\n",
2443 names[info->value.enumerated.item]);
2444 strscpy(info->value.enumerated.name,
2445 names[info->value.enumerated.item],
2446 sizeof(info->value.enumerated.name));
2447 return 0;
2448 }
2449 EXPORT_SYMBOL(snd_ctl_enum_info);