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