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