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