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