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