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