1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * V4L2 controls framework uAPI implementation: 4 * 5 * Copyright (C) 2010-2021 Hans Verkuil <hverkuil@kernel.org> 6 */ 7 8 #define pr_fmt(fmt) "v4l2-ctrls: " fmt 9 10 #include <linux/export.h> 11 #include <linux/mm.h> 12 #include <linux/slab.h> 13 #include <media/v4l2-ctrls.h> 14 #include <media/v4l2-dev.h> 15 #include <media/v4l2-device.h> 16 #include <media/v4l2-event.h> 17 #include <media/v4l2-ioctl.h> 18 19 #include "v4l2-ctrls-priv.h" 20 21 /* Internal temporary helper struct, one for each v4l2_ext_control */ 22 struct v4l2_ctrl_helper { 23 /* Pointer to the control reference of the master control */ 24 struct v4l2_ctrl_ref *mref; 25 /* The control ref corresponding to the v4l2_ext_control ID field. */ 26 struct v4l2_ctrl_ref *ref; 27 /* 28 * v4l2_ext_control index of the next control belonging to the 29 * same cluster, or 0 if there isn't any. 30 */ 31 u32 next; 32 }; 33 34 /* 35 * Helper functions to copy control payload data from kernel space to 36 * user space and vice versa. 37 */ 38 39 /* Helper function: copy the given control value back to the caller */ 40 static int ptr_to_user(struct v4l2_ext_control *c, 41 struct v4l2_ctrl *ctrl, 42 union v4l2_ctrl_ptr ptr) 43 { 44 u32 len; 45 46 if (ctrl->is_ptr && !ctrl->is_string) 47 return copy_to_user(c->ptr, ptr.p_const, c->size) ? 48 -EFAULT : 0; 49 50 switch (ctrl->type) { 51 case V4L2_CTRL_TYPE_STRING: 52 len = strlen(ptr.p_char); 53 if (c->size < len + 1) { 54 c->size = ctrl->elem_size; 55 return -ENOSPC; 56 } 57 return copy_to_user(c->string, ptr.p_char, len + 1) ? 58 -EFAULT : 0; 59 case V4L2_CTRL_TYPE_INTEGER64: 60 c->value64 = *ptr.p_s64; 61 break; 62 default: 63 c->value = *ptr.p_s32; 64 break; 65 } 66 return 0; 67 } 68 69 /* Helper function: copy the current control value back to the caller */ 70 static int cur_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl) 71 { 72 return ptr_to_user(c, ctrl, ctrl->p_cur); 73 } 74 75 /* Helper function: copy the new control value back to the caller */ 76 static int new_to_user(struct v4l2_ext_control *c, 77 struct v4l2_ctrl *ctrl) 78 { 79 return ptr_to_user(c, ctrl, ctrl->p_new); 80 } 81 82 /* Helper function: copy the request value back to the caller */ 83 static int req_to_user(struct v4l2_ext_control *c, 84 struct v4l2_ctrl_ref *ref) 85 { 86 return ptr_to_user(c, ref->ctrl, ref->p_req); 87 } 88 89 /* Helper function: copy the initial control value back to the caller */ 90 static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl) 91 { 92 ctrl->type_ops->init(ctrl, 0, ctrl->p_new); 93 94 return ptr_to_user(c, ctrl, ctrl->p_new); 95 } 96 97 /* Helper function: copy the minimum control value back to the caller */ 98 static int min_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl) 99 { 100 ctrl->type_ops->minimum(ctrl, 0, ctrl->p_new); 101 102 return ptr_to_user(c, ctrl, ctrl->p_new); 103 } 104 105 /* Helper function: copy the maximum control value back to the caller */ 106 static int max_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl) 107 { 108 ctrl->type_ops->maximum(ctrl, 0, ctrl->p_new); 109 110 return ptr_to_user(c, ctrl, ctrl->p_new); 111 } 112 113 /* Helper function: copy the caller-provider value as the new control value */ 114 static int user_to_new(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl) 115 { 116 int ret; 117 u32 size; 118 119 ctrl->is_new = 0; 120 if (ctrl->is_dyn_array && 121 c->size > ctrl->p_array_alloc_elems * ctrl->elem_size) { 122 void *old = ctrl->p_array; 123 void *tmp = kvzalloc(2 * c->size, GFP_KERNEL); 124 125 if (!tmp) 126 return -ENOMEM; 127 memcpy(tmp, ctrl->p_new.p, ctrl->elems * ctrl->elem_size); 128 memcpy(tmp + c->size, ctrl->p_cur.p, ctrl->elems * ctrl->elem_size); 129 ctrl->p_new.p = tmp; 130 ctrl->p_cur.p = tmp + c->size; 131 ctrl->p_array = tmp; 132 ctrl->p_array_alloc_elems = c->size / ctrl->elem_size; 133 kvfree(old); 134 } 135 136 if (ctrl->is_ptr && !ctrl->is_string) { 137 unsigned int elems = c->size / ctrl->elem_size; 138 139 if (copy_from_user(ctrl->p_new.p, c->ptr, c->size)) 140 return -EFAULT; 141 ctrl->is_new = 1; 142 if (ctrl->is_dyn_array) 143 ctrl->new_elems = elems; 144 else if (ctrl->is_array) 145 ctrl->type_ops->init(ctrl, elems, ctrl->p_new); 146 return 0; 147 } 148 149 switch (ctrl->type) { 150 case V4L2_CTRL_TYPE_INTEGER64: 151 *ctrl->p_new.p_s64 = c->value64; 152 break; 153 case V4L2_CTRL_TYPE_STRING: 154 size = c->size; 155 if (size == 0) 156 return -ERANGE; 157 if (size > ctrl->maximum + 1) 158 size = ctrl->maximum + 1; 159 ret = copy_from_user(ctrl->p_new.p_char, c->string, size) ? -EFAULT : 0; 160 if (!ret) { 161 char last = ctrl->p_new.p_char[size - 1]; 162 163 ctrl->p_new.p_char[size - 1] = 0; 164 /* 165 * If the string was longer than ctrl->maximum, 166 * then return an error. 167 */ 168 if (strlen(ctrl->p_new.p_char) == ctrl->maximum && last) 169 return -ERANGE; 170 ctrl->is_new = 1; 171 } 172 return ret; 173 default: 174 *ctrl->p_new.p_s32 = c->value; 175 break; 176 } 177 ctrl->is_new = 1; 178 return 0; 179 } 180 181 /* 182 * VIDIOC_G/TRY/S_EXT_CTRLS implementation 183 */ 184 185 /* 186 * Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS: 187 * 188 * It is not a fully atomic operation, just best-effort only. After all, if 189 * multiple controls have to be set through multiple i2c writes (for example) 190 * then some initial writes may succeed while others fail. Thus leaving the 191 * system in an inconsistent state. The question is how much effort you are 192 * willing to spend on trying to make something atomic that really isn't. 193 * 194 * From the point of view of an application the main requirement is that 195 * when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an 196 * error should be returned without actually affecting any controls. 197 * 198 * If all the values are correct, then it is acceptable to just give up 199 * in case of low-level errors. 200 * 201 * It is important though that the application can tell when only a partial 202 * configuration was done. The way we do that is through the error_idx field 203 * of struct v4l2_ext_controls: if that is equal to the count field then no 204 * controls were affected. Otherwise all controls before that index were 205 * successful in performing their 'get' or 'set' operation, the control at 206 * the given index failed, and you don't know what happened with the controls 207 * after the failed one. Since if they were part of a control cluster they 208 * could have been successfully processed (if a cluster member was encountered 209 * at index < error_idx), they could have failed (if a cluster member was at 210 * error_idx), or they may not have been processed yet (if the first cluster 211 * member appeared after error_idx). 212 * 213 * It is all fairly theoretical, though. In practice all you can do is to 214 * bail out. If error_idx == count, then it is an application bug. If 215 * error_idx < count then it is only an application bug if the error code was 216 * EBUSY. That usually means that something started streaming just when you 217 * tried to set the controls. In all other cases it is a driver/hardware 218 * problem and all you can do is to retry or bail out. 219 * 220 * Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that 221 * never modifies controls the error_idx is just set to whatever control 222 * has an invalid value. 223 */ 224 225 /* 226 * Prepare for the extended g/s/try functions. 227 * Find the controls in the control array and do some basic checks. 228 */ 229 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl, 230 struct v4l2_ext_controls *cs, 231 struct v4l2_ctrl_helper *helpers, 232 struct video_device *vdev, 233 bool get) 234 { 235 struct v4l2_ctrl_helper *h; 236 bool have_clusters = false; 237 u32 i; 238 239 for (i = 0, h = helpers; i < cs->count; i++, h++) { 240 struct v4l2_ext_control *c = &cs->controls[i]; 241 struct v4l2_ctrl_ref *ref; 242 struct v4l2_ctrl *ctrl; 243 u32 id = c->id & V4L2_CTRL_ID_MASK; 244 245 cs->error_idx = i; 246 247 if (cs->which && 248 (cs->which < V4L2_CTRL_WHICH_DEF_VAL || 249 cs->which > V4L2_CTRL_WHICH_MAX_VAL) && 250 V4L2_CTRL_ID2WHICH(id) != cs->which) { 251 dprintk(vdev, 252 "invalid which 0x%x or control id 0x%x\n", 253 cs->which, id); 254 return -EINVAL; 255 } 256 257 /* 258 * Old-style private controls are not allowed for 259 * extended controls. 260 */ 261 if (id >= V4L2_CID_PRIVATE_BASE) { 262 dprintk(vdev, 263 "old-style private controls not allowed\n"); 264 return -EINVAL; 265 } 266 ref = find_ref_lock(hdl, id); 267 if (!ref) { 268 dprintk(vdev, "cannot find control id 0x%x\n", id); 269 return -EINVAL; 270 } 271 h->ref = ref; 272 ctrl = ref->ctrl; 273 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED) { 274 dprintk(vdev, "control id 0x%x is disabled\n", id); 275 return -EINVAL; 276 } 277 278 if (!(ctrl->flags & V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX) && 279 (cs->which == V4L2_CTRL_WHICH_MIN_VAL || 280 cs->which == V4L2_CTRL_WHICH_MAX_VAL)) { 281 dprintk(vdev, 282 "invalid which 0x%x or control id 0x%x\n", 283 cs->which, id); 284 return -EINVAL; 285 } 286 287 if (ctrl->cluster[0]->ncontrols > 1) 288 have_clusters = true; 289 if (ctrl->cluster[0] != ctrl) 290 ref = find_ref_lock(hdl, ctrl->cluster[0]->id); 291 if (ctrl->is_dyn_array) { 292 unsigned int max_size = ctrl->dims[0] * ctrl->elem_size; 293 unsigned int tot_size = ctrl->elem_size; 294 295 if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) 296 tot_size *= ref->p_req_elems; 297 else 298 tot_size *= ctrl->elems; 299 300 c->size = ctrl->elem_size * (c->size / ctrl->elem_size); 301 if (get) { 302 if (c->size < tot_size) { 303 c->size = tot_size; 304 return -ENOSPC; 305 } 306 c->size = tot_size; 307 } else { 308 if (c->size > max_size) { 309 c->size = max_size; 310 return -ENOSPC; 311 } 312 if (!c->size) 313 return -EFAULT; 314 } 315 } else if (ctrl->is_ptr && !ctrl->is_string) { 316 unsigned int tot_size = ctrl->elems * ctrl->elem_size; 317 318 if (c->size < tot_size) { 319 /* 320 * In the get case the application first 321 * queries to obtain the size of the control. 322 */ 323 if (get) { 324 c->size = tot_size; 325 return -ENOSPC; 326 } 327 dprintk(vdev, 328 "pointer control id 0x%x size too small, %d bytes but %d bytes needed\n", 329 id, c->size, tot_size); 330 return -EFAULT; 331 } 332 c->size = tot_size; 333 } 334 /* Store the ref to the master control of the cluster */ 335 h->mref = ref; 336 /* 337 * Initially set next to 0, meaning that there is no other 338 * control in this helper array belonging to the same 339 * cluster. 340 */ 341 h->next = 0; 342 } 343 344 /* 345 * We are done if there were no controls that belong to a multi- 346 * control cluster. 347 */ 348 if (!have_clusters) 349 return 0; 350 351 /* 352 * The code below figures out in O(n) time which controls in the list 353 * belong to the same cluster. 354 */ 355 356 /* This has to be done with the handler lock taken. */ 357 mutex_lock(hdl->lock); 358 359 /* First zero the helper field in the master control references */ 360 for (i = 0; i < cs->count; i++) 361 helpers[i].mref->helper = NULL; 362 for (i = 0, h = helpers; i < cs->count; i++, h++) { 363 struct v4l2_ctrl_ref *mref = h->mref; 364 365 /* 366 * If the mref->helper is set, then it points to an earlier 367 * helper that belongs to the same cluster. 368 */ 369 if (mref->helper) { 370 /* 371 * Set the next field of mref->helper to the current 372 * index: this means that the earlier helper now 373 * points to the next helper in the same cluster. 374 */ 375 mref->helper->next = i; 376 /* 377 * mref should be set only for the first helper in the 378 * cluster, clear the others. 379 */ 380 h->mref = NULL; 381 } 382 /* Point the mref helper to the current helper struct. */ 383 mref->helper = h; 384 } 385 mutex_unlock(hdl->lock); 386 return 0; 387 } 388 389 /* 390 * Handles the corner case where cs->count == 0. It checks whether the 391 * specified control class exists. If that class ID is 0, then it checks 392 * whether there are any controls at all. 393 */ 394 static int class_check(struct v4l2_ctrl_handler *hdl, u32 which) 395 { 396 if (which == 0 || (which >= V4L2_CTRL_WHICH_DEF_VAL && 397 which <= V4L2_CTRL_WHICH_MAX_VAL)) 398 return 0; 399 return find_ref_lock(hdl, which | 1) ? 0 : -EINVAL; 400 } 401 402 /* 403 * Get extended controls. Allocates the helpers array if needed. 404 * 405 * Note that v4l2_g_ext_ctrls_common() with 'which' set to 406 * V4L2_CTRL_WHICH_REQUEST_VAL is only called if the request was 407 * completed, and in that case p_req_valid is true for all controls. 408 */ 409 int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl, 410 struct v4l2_ext_controls *cs, 411 struct video_device *vdev) 412 { 413 struct v4l2_ctrl_helper helper[4]; 414 struct v4l2_ctrl_helper *helpers = helper; 415 int ret; 416 int i, j; 417 bool is_default, is_request, is_min, is_max; 418 419 is_default = (cs->which == V4L2_CTRL_WHICH_DEF_VAL); 420 is_request = (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL); 421 is_min = (cs->which == V4L2_CTRL_WHICH_MIN_VAL); 422 is_max = (cs->which == V4L2_CTRL_WHICH_MAX_VAL); 423 424 cs->error_idx = cs->count; 425 cs->which = V4L2_CTRL_ID2WHICH(cs->which); 426 427 if (!hdl) 428 return -EINVAL; 429 430 if (cs->count == 0) 431 return class_check(hdl, cs->which); 432 433 if (cs->count > ARRAY_SIZE(helper)) { 434 helpers = kvmalloc_objs(helper[0], cs->count); 435 if (!helpers) 436 return -ENOMEM; 437 } 438 439 ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, true); 440 cs->error_idx = cs->count; 441 442 for (i = 0; !ret && i < cs->count; i++) 443 if (helpers[i].ref->ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY) 444 ret = -EACCES; 445 446 for (i = 0; !ret && i < cs->count; i++) { 447 struct v4l2_ctrl *master; 448 bool is_volatile = false; 449 u32 idx = i; 450 451 if (!helpers[i].mref) 452 continue; 453 454 master = helpers[i].mref->ctrl; 455 cs->error_idx = i; 456 457 v4l2_ctrl_lock(master); 458 459 /* 460 * g_volatile_ctrl will update the new control values. 461 * This makes no sense for V4L2_CTRL_WHICH_DEF_VAL, 462 * V4L2_CTRL_WHICH_MIN_VAL, V4L2_CTRL_WHICH_MAX_VAL and 463 * V4L2_CTRL_WHICH_REQUEST_VAL. In the case of requests 464 * it is v4l2_ctrl_request_complete() that copies the 465 * volatile controls at the time of request completion 466 * to the request, so you don't want to do that again. 467 */ 468 if (!is_default && !is_request && !is_min && !is_max && 469 ((master->flags & V4L2_CTRL_FLAG_VOLATILE) || 470 (master->has_volatiles && !is_cur_manual(master)))) { 471 for (j = 0; j < master->ncontrols; j++) 472 cur_to_new(master->cluster[j]); 473 ret = call_op(master, g_volatile_ctrl); 474 is_volatile = true; 475 } 476 477 if (ret) { 478 v4l2_ctrl_unlock(master); 479 break; 480 } 481 482 /* 483 * Copy the default value (if is_default is true), the 484 * request value (if is_request is true and p_req is valid), 485 * the new volatile value (if is_volatile is true) or the 486 * current value. 487 */ 488 do { 489 struct v4l2_ctrl_ref *ref = helpers[idx].ref; 490 491 if (is_default) 492 ret = def_to_user(cs->controls + idx, ref->ctrl); 493 else if (is_request && ref->p_req_array_enomem) 494 ret = -ENOMEM; 495 else if (is_request && ref->p_req_valid) 496 ret = req_to_user(cs->controls + idx, ref); 497 else if (is_min) 498 ret = min_to_user(cs->controls + idx, ref->ctrl); 499 else if (is_max) 500 ret = max_to_user(cs->controls + idx, ref->ctrl); 501 else if (is_volatile) 502 ret = new_to_user(cs->controls + idx, ref->ctrl); 503 else 504 ret = cur_to_user(cs->controls + idx, ref->ctrl); 505 idx = helpers[idx].next; 506 } while (!ret && idx); 507 508 v4l2_ctrl_unlock(master); 509 } 510 511 if (cs->count > ARRAY_SIZE(helper)) 512 kvfree(helpers); 513 return ret; 514 } 515 516 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct video_device *vdev, 517 struct media_device *mdev, struct v4l2_ext_controls *cs) 518 { 519 if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) 520 return v4l2_g_ext_ctrls_request(hdl, vdev, mdev, cs); 521 522 return v4l2_g_ext_ctrls_common(hdl, cs, vdev); 523 } 524 EXPORT_SYMBOL(v4l2_g_ext_ctrls); 525 526 /* Validate a new control */ 527 static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new) 528 { 529 return ctrl->type_ops->validate(ctrl, p_new); 530 } 531 532 /* Validate controls. */ 533 static int validate_ctrls(struct v4l2_ext_controls *cs, 534 struct v4l2_ctrl_helper *helpers, 535 struct video_device *vdev, 536 bool set) 537 { 538 unsigned int i; 539 int ret = 0; 540 541 cs->error_idx = cs->count; 542 for (i = 0; i < cs->count; i++) { 543 struct v4l2_ctrl *ctrl = helpers[i].ref->ctrl; 544 union v4l2_ctrl_ptr p_new; 545 546 cs->error_idx = i; 547 548 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY) { 549 dprintk(vdev, 550 "control id 0x%x is read-only\n", 551 ctrl->id); 552 return -EACCES; 553 } 554 /* 555 * This test is also done in try_set_control_cluster() which 556 * is called in atomic context, so that has the final say, 557 * but it makes sense to do an up-front check as well. Once 558 * an error occurs in try_set_control_cluster() some other 559 * controls may have been set already and we want to do a 560 * best-effort to avoid that. 561 */ 562 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)) { 563 dprintk(vdev, 564 "control id 0x%x is grabbed, cannot set\n", 565 ctrl->id); 566 return -EBUSY; 567 } 568 /* 569 * Skip validation for now if the payload needs to be copied 570 * from userspace into kernelspace. We'll validate those later. 571 */ 572 if (ctrl->is_ptr) 573 continue; 574 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64) 575 p_new.p_s64 = &cs->controls[i].value64; 576 else 577 p_new.p_s32 = &cs->controls[i].value; 578 ret = validate_new(ctrl, p_new); 579 if (ret) 580 return ret; 581 } 582 return 0; 583 } 584 585 /* Try or try-and-set controls */ 586 int try_set_ext_ctrls_common(struct v4l2_fh *fh, 587 struct v4l2_ctrl_handler *hdl, 588 struct v4l2_ext_controls *cs, 589 struct video_device *vdev, bool set) 590 { 591 struct v4l2_ctrl_helper helper[4]; 592 struct v4l2_ctrl_helper *helpers = helper; 593 unsigned int i, j; 594 int ret; 595 596 cs->error_idx = cs->count; 597 598 /* Default/minimum/maximum values cannot be changed */ 599 if (cs->which == V4L2_CTRL_WHICH_DEF_VAL || 600 cs->which == V4L2_CTRL_WHICH_MIN_VAL || 601 cs->which == V4L2_CTRL_WHICH_MAX_VAL) { 602 dprintk(vdev, "%s: cannot change default/min/max value\n", 603 video_device_node_name(vdev)); 604 return -EINVAL; 605 } 606 607 cs->which = V4L2_CTRL_ID2WHICH(cs->which); 608 609 if (!hdl) { 610 dprintk(vdev, "%s: invalid null control handler\n", 611 video_device_node_name(vdev)); 612 return -EINVAL; 613 } 614 615 if (cs->count == 0) 616 return class_check(hdl, cs->which); 617 618 if (cs->count > ARRAY_SIZE(helper)) { 619 helpers = kvmalloc_objs(helper[0], cs->count); 620 if (!helpers) 621 return -ENOMEM; 622 } 623 ret = prepare_ext_ctrls(hdl, cs, helpers, vdev, false); 624 if (!ret) 625 ret = validate_ctrls(cs, helpers, vdev, set); 626 if (ret && set) 627 cs->error_idx = cs->count; 628 for (i = 0; !ret && i < cs->count; i++) { 629 struct v4l2_ctrl *master; 630 u32 idx = i; 631 632 if (!helpers[i].mref) 633 continue; 634 635 cs->error_idx = i; 636 master = helpers[i].mref->ctrl; 637 v4l2_ctrl_lock(master); 638 639 /* Reset the 'is_new' flags of the cluster */ 640 for (j = 0; j < master->ncontrols; j++) 641 if (master->cluster[j]) 642 master->cluster[j]->is_new = 0; 643 644 /* 645 * For volatile autoclusters that are currently in auto mode 646 * we need to discover if it will be set to manual mode. 647 * If so, then we have to copy the current volatile values 648 * first since those will become the new manual values (which 649 * may be overwritten by explicit new values from this set 650 * of controls). 651 */ 652 if (master->is_auto && master->has_volatiles && 653 !is_cur_manual(master)) { 654 /* Pick an initial non-manual value */ 655 s32 new_auto_val = master->manual_mode_value + 1; 656 u32 tmp_idx = idx; 657 658 do { 659 /* 660 * Check if the auto control is part of the 661 * list, and remember the new value. 662 */ 663 if (helpers[tmp_idx].ref->ctrl == master) 664 new_auto_val = cs->controls[tmp_idx].value; 665 tmp_idx = helpers[tmp_idx].next; 666 } while (tmp_idx); 667 /* 668 * If the new value == the manual value, then copy 669 * the current volatile values. 670 */ 671 if (new_auto_val == master->manual_mode_value) 672 update_from_auto_cluster(master); 673 } 674 675 /* 676 * Copy the new caller-supplied control values. 677 * user_to_new() sets 'is_new' to 1. 678 */ 679 do { 680 struct v4l2_ctrl *ctrl = helpers[idx].ref->ctrl; 681 682 ret = user_to_new(cs->controls + idx, ctrl); 683 if (!ret && ctrl->is_ptr) { 684 ret = validate_new(ctrl, ctrl->p_new); 685 if (ret) 686 dprintk(vdev, 687 "failed to validate control %s (%d)\n", 688 v4l2_ctrl_get_name(ctrl->id), ret); 689 } 690 idx = helpers[idx].next; 691 } while (!ret && idx); 692 693 if (!ret) 694 ret = try_or_set_cluster(fh, master, 695 !hdl->req_obj.req && set, 0); 696 if (!ret && hdl->req_obj.req && set) { 697 for (j = 0; j < master->ncontrols; j++) { 698 struct v4l2_ctrl_ref *ref = 699 find_ref(hdl, master->cluster[j]->id); 700 701 new_to_req(ref); 702 } 703 } 704 705 /* Copy the new values back to userspace. */ 706 if (!ret) { 707 idx = i; 708 do { 709 ret = new_to_user(cs->controls + idx, 710 helpers[idx].ref->ctrl); 711 idx = helpers[idx].next; 712 } while (!ret && idx); 713 } 714 v4l2_ctrl_unlock(master); 715 } 716 717 if (cs->count > ARRAY_SIZE(helper)) 718 kvfree(helpers); 719 return ret; 720 } 721 722 static int try_set_ext_ctrls(struct v4l2_fh *fh, 723 struct v4l2_ctrl_handler *hdl, 724 struct video_device *vdev, 725 struct media_device *mdev, 726 struct v4l2_ext_controls *cs, bool set) 727 { 728 int ret; 729 730 if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) 731 return try_set_ext_ctrls_request(fh, hdl, vdev, mdev, cs, set); 732 733 ret = try_set_ext_ctrls_common(fh, hdl, cs, vdev, set); 734 if (ret) 735 dprintk(vdev, 736 "%s: try_set_ext_ctrls_common failed (%d)\n", 737 video_device_node_name(vdev), ret); 738 739 return ret; 740 } 741 742 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, 743 struct video_device *vdev, 744 struct media_device *mdev, 745 struct v4l2_ext_controls *cs) 746 { 747 return try_set_ext_ctrls(NULL, hdl, vdev, mdev, cs, false); 748 } 749 EXPORT_SYMBOL(v4l2_try_ext_ctrls); 750 751 int v4l2_s_ext_ctrls(struct v4l2_fh *fh, 752 struct v4l2_ctrl_handler *hdl, 753 struct video_device *vdev, 754 struct media_device *mdev, 755 struct v4l2_ext_controls *cs) 756 { 757 return try_set_ext_ctrls(fh, hdl, vdev, mdev, cs, true); 758 } 759 EXPORT_SYMBOL(v4l2_s_ext_ctrls); 760 761 /* 762 * VIDIOC_G/S_CTRL implementation 763 */ 764 765 /* Helper function to get a single control */ 766 static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c) 767 { 768 struct v4l2_ctrl *master = ctrl->cluster[0]; 769 int ret = 0; 770 int i; 771 772 /* Compound controls are not supported. The new_to_user() and 773 * cur_to_user() calls below would need to be modified not to access 774 * userspace memory when called from get_ctrl(). 775 */ 776 if (!ctrl->is_int && ctrl->type != V4L2_CTRL_TYPE_INTEGER64) 777 return -EINVAL; 778 779 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY) 780 return -EACCES; 781 782 v4l2_ctrl_lock(master); 783 /* g_volatile_ctrl will update the current control values */ 784 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) { 785 for (i = 0; i < master->ncontrols; i++) 786 cur_to_new(master->cluster[i]); 787 ret = call_op(master, g_volatile_ctrl); 788 if (!ret) 789 ret = new_to_user(c, ctrl); 790 } else { 791 ret = cur_to_user(c, ctrl); 792 } 793 v4l2_ctrl_unlock(master); 794 return ret; 795 } 796 797 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control) 798 { 799 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id); 800 struct v4l2_ext_control c; 801 int ret; 802 803 if (!ctrl || !ctrl->is_int) 804 return -EINVAL; 805 ret = get_ctrl(ctrl, &c); 806 807 if (!ret) 808 control->value = c.value; 809 810 return ret; 811 } 812 EXPORT_SYMBOL(v4l2_g_ctrl); 813 814 /* Helper function for VIDIOC_S_CTRL compatibility */ 815 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags) 816 { 817 struct v4l2_ctrl *master = ctrl->cluster[0]; 818 int ret; 819 int i; 820 821 /* Reset the 'is_new' flags of the cluster */ 822 for (i = 0; i < master->ncontrols; i++) 823 if (master->cluster[i]) 824 master->cluster[i]->is_new = 0; 825 826 ret = validate_new(ctrl, ctrl->p_new); 827 if (ret) 828 return ret; 829 830 /* 831 * For autoclusters with volatiles that are switched from auto to 832 * manual mode we have to update the current volatile values since 833 * those will become the initial manual values after such a switch. 834 */ 835 if (master->is_auto && master->has_volatiles && ctrl == master && 836 !is_cur_manual(master) && ctrl->val == master->manual_mode_value) 837 update_from_auto_cluster(master); 838 839 ctrl->is_new = 1; 840 return try_or_set_cluster(fh, master, true, ch_flags); 841 } 842 843 /* Helper function for VIDIOC_S_CTRL compatibility */ 844 static int set_ctrl_lock(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, 845 struct v4l2_ext_control *c) 846 { 847 int ret; 848 849 v4l2_ctrl_lock(ctrl); 850 ret = user_to_new(c, ctrl); 851 if (!ret) 852 ret = set_ctrl(fh, ctrl, 0); 853 if (!ret) 854 ret = cur_to_user(c, ctrl); 855 v4l2_ctrl_unlock(ctrl); 856 return ret; 857 } 858 859 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl, 860 struct v4l2_control *control) 861 { 862 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id); 863 struct v4l2_ext_control c = { control->id }; 864 int ret; 865 866 if (!ctrl || !ctrl->is_int) 867 return -EINVAL; 868 869 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY) 870 return -EACCES; 871 872 c.value = control->value; 873 ret = set_ctrl_lock(fh, ctrl, &c); 874 control->value = c.value; 875 return ret; 876 } 877 EXPORT_SYMBOL(v4l2_s_ctrl); 878 879 /* 880 * Helper functions for drivers to get/set controls. 881 */ 882 883 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl) 884 { 885 struct v4l2_ext_control c; 886 887 /* It's a driver bug if this happens. */ 888 if (WARN_ON(!ctrl->is_int)) 889 return 0; 890 c.value = 0; 891 get_ctrl(ctrl, &c); 892 return c.value; 893 } 894 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl); 895 896 s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl) 897 { 898 struct v4l2_ext_control c; 899 900 /* It's a driver bug if this happens. */ 901 if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64)) 902 return 0; 903 c.value64 = 0; 904 get_ctrl(ctrl, &c); 905 return c.value64; 906 } 907 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl_int64); 908 909 int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val) 910 { 911 lockdep_assert_held(ctrl->handler->lock); 912 913 /* It's a driver bug if this happens. */ 914 if (WARN_ON(!ctrl->is_int)) 915 return -EINVAL; 916 ctrl->val = val; 917 return set_ctrl(NULL, ctrl, 0); 918 } 919 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl); 920 921 int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val) 922 { 923 lockdep_assert_held(ctrl->handler->lock); 924 925 /* It's a driver bug if this happens. */ 926 if (WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64)) 927 return -EINVAL; 928 *ctrl->p_new.p_s64 = val; 929 return set_ctrl(NULL, ctrl, 0); 930 } 931 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_int64); 932 933 int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s) 934 { 935 lockdep_assert_held(ctrl->handler->lock); 936 937 /* It's a driver bug if this happens. */ 938 if (WARN_ON(ctrl->type != V4L2_CTRL_TYPE_STRING)) 939 return -EINVAL; 940 strscpy(ctrl->p_new.p_char, s, ctrl->maximum + 1); 941 return set_ctrl(NULL, ctrl, 0); 942 } 943 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string); 944 945 int __v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl, 946 enum v4l2_ctrl_type type, const void *p) 947 { 948 lockdep_assert_held(ctrl->handler->lock); 949 950 /* It's a driver bug if this happens. */ 951 if (WARN_ON(ctrl->type != type)) 952 return -EINVAL; 953 /* Setting dynamic arrays is not (yet?) supported. */ 954 if (WARN_ON(ctrl->is_dyn_array)) 955 return -EINVAL; 956 memcpy(ctrl->p_new.p, p, ctrl->elems * ctrl->elem_size); 957 return set_ctrl(NULL, ctrl, 0); 958 } 959 EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_compound); 960 961 /* 962 * Modify the range of a control. 963 */ 964 int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl, 965 s64 min, s64 max, u64 step, s64 def) 966 { 967 bool value_changed; 968 bool range_changed = false; 969 int ret; 970 971 lockdep_assert_held(ctrl->handler->lock); 972 973 switch (ctrl->type) { 974 case V4L2_CTRL_TYPE_INTEGER: 975 case V4L2_CTRL_TYPE_INTEGER64: 976 case V4L2_CTRL_TYPE_BOOLEAN: 977 case V4L2_CTRL_TYPE_MENU: 978 case V4L2_CTRL_TYPE_INTEGER_MENU: 979 case V4L2_CTRL_TYPE_BITMASK: 980 case V4L2_CTRL_TYPE_U8: 981 case V4L2_CTRL_TYPE_U16: 982 case V4L2_CTRL_TYPE_U32: 983 if (ctrl->is_array) 984 return -EINVAL; 985 ret = check_range(ctrl->type, min, max, step, def); 986 if (ret) 987 return ret; 988 break; 989 default: 990 return -EINVAL; 991 } 992 if (ctrl->minimum != min || ctrl->maximum != max || 993 ctrl->step != step || ctrl->default_value != def) { 994 range_changed = true; 995 ctrl->minimum = min; 996 ctrl->maximum = max; 997 ctrl->step = step; 998 ctrl->default_value = def; 999 } 1000 cur_to_new(ctrl); 1001 if (validate_new(ctrl, ctrl->p_new)) { 1002 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64) 1003 *ctrl->p_new.p_s64 = def; 1004 else 1005 *ctrl->p_new.p_s32 = def; 1006 } 1007 1008 if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64) 1009 value_changed = *ctrl->p_new.p_s64 != *ctrl->p_cur.p_s64; 1010 else 1011 value_changed = *ctrl->p_new.p_s32 != *ctrl->p_cur.p_s32; 1012 if (value_changed) 1013 ret = set_ctrl(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE); 1014 else if (range_changed) 1015 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE); 1016 return ret; 1017 } 1018 EXPORT_SYMBOL(__v4l2_ctrl_modify_range); 1019 1020 int __v4l2_ctrl_modify_dimensions(struct v4l2_ctrl *ctrl, 1021 u32 dims[V4L2_CTRL_MAX_DIMS]) 1022 { 1023 unsigned int elems = 1; 1024 unsigned int i; 1025 void *p_array; 1026 1027 lockdep_assert_held(ctrl->handler->lock); 1028 1029 if (!ctrl->is_array || ctrl->is_dyn_array) 1030 return -EINVAL; 1031 1032 for (i = 0; i < ctrl->nr_of_dims; i++) 1033 elems *= dims[i]; 1034 if (elems == 0) 1035 return -EINVAL; 1036 p_array = kvzalloc(2 * elems * ctrl->elem_size, GFP_KERNEL); 1037 if (!p_array) 1038 return -ENOMEM; 1039 kvfree(ctrl->p_array); 1040 ctrl->p_array_alloc_elems = elems; 1041 ctrl->elems = elems; 1042 ctrl->new_elems = elems; 1043 ctrl->p_array = p_array; 1044 ctrl->p_new.p = p_array; 1045 ctrl->p_cur.p = p_array + elems * ctrl->elem_size; 1046 for (i = 0; i < ctrl->nr_of_dims; i++) 1047 ctrl->dims[i] = dims[i]; 1048 ctrl->type_ops->init(ctrl, 0, ctrl->p_cur); 1049 cur_to_new(ctrl); 1050 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_VALUE | 1051 V4L2_EVENT_CTRL_CH_DIMENSIONS); 1052 return 0; 1053 } 1054 EXPORT_SYMBOL(__v4l2_ctrl_modify_dimensions); 1055 1056 /* Implement VIDIOC_QUERY_EXT_CTRL */ 1057 int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc) 1058 { 1059 const unsigned int next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND; 1060 u32 id = qc->id & V4L2_CTRL_ID_MASK; 1061 struct v4l2_ctrl_ref *ref; 1062 struct v4l2_ctrl *ctrl; 1063 1064 if (!hdl) 1065 return -EINVAL; 1066 1067 mutex_lock(hdl->lock); 1068 1069 /* Try to find it */ 1070 ref = find_ref(hdl, id); 1071 1072 if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) { 1073 bool is_compound; 1074 /* Match any control that is not hidden */ 1075 unsigned int mask = 1; 1076 bool match = false; 1077 1078 if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) { 1079 /* Match any hidden control */ 1080 match = true; 1081 } else if ((qc->id & next_flags) == next_flags) { 1082 /* Match any control, compound or not */ 1083 mask = 0; 1084 } 1085 1086 /* Find the next control with ID > qc->id */ 1087 1088 /* Did we reach the end of the control list? */ 1089 if (id >= node2id(hdl->ctrl_refs.prev)) { 1090 ref = NULL; /* Yes, so there is no next control */ 1091 } else if (ref) { 1092 struct v4l2_ctrl_ref *pos = ref; 1093 1094 /* 1095 * We found a control with the given ID, so just get 1096 * the next valid one in the list. 1097 */ 1098 ref = NULL; 1099 list_for_each_entry_continue(pos, &hdl->ctrl_refs, node) { 1100 is_compound = pos->ctrl->is_array || 1101 pos->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES; 1102 if (id < pos->ctrl->id && 1103 (is_compound & mask) == match) { 1104 ref = pos; 1105 break; 1106 } 1107 } 1108 } else { 1109 struct v4l2_ctrl_ref *pos; 1110 1111 /* 1112 * No control with the given ID exists, so start 1113 * searching for the next largest ID. We know there 1114 * is one, otherwise the first 'if' above would have 1115 * been true. 1116 */ 1117 list_for_each_entry(pos, &hdl->ctrl_refs, node) { 1118 is_compound = pos->ctrl->is_array || 1119 pos->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES; 1120 if (id < pos->ctrl->id && 1121 (is_compound & mask) == match) { 1122 ref = pos; 1123 break; 1124 } 1125 } 1126 } 1127 } 1128 mutex_unlock(hdl->lock); 1129 1130 if (!ref) 1131 return -EINVAL; 1132 1133 ctrl = ref->ctrl; 1134 memset(qc, 0, sizeof(*qc)); 1135 if (id >= V4L2_CID_PRIVATE_BASE) 1136 qc->id = id; 1137 else 1138 qc->id = ctrl->id; 1139 strscpy(qc->name, ctrl->name, sizeof(qc->name)); 1140 qc->flags = user_flags(ctrl); 1141 qc->type = ctrl->type; 1142 qc->elem_size = ctrl->elem_size; 1143 qc->elems = ctrl->elems; 1144 qc->nr_of_dims = ctrl->nr_of_dims; 1145 memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0])); 1146 qc->minimum = ctrl->minimum; 1147 qc->maximum = ctrl->maximum; 1148 qc->default_value = ctrl->default_value; 1149 if (ctrl->type == V4L2_CTRL_TYPE_MENU || 1150 ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU) 1151 qc->step = 1; 1152 else 1153 qc->step = ctrl->step; 1154 return 0; 1155 } 1156 EXPORT_SYMBOL(v4l2_query_ext_ctrl); 1157 1158 void v4l2_query_ext_ctrl_to_v4l2_queryctrl(struct v4l2_queryctrl *to, 1159 const struct v4l2_query_ext_ctrl *from) 1160 { 1161 to->id = from->id; 1162 to->type = from->type; 1163 to->flags = from->flags; 1164 strscpy(to->name, from->name, sizeof(to->name)); 1165 1166 switch (from->type) { 1167 case V4L2_CTRL_TYPE_INTEGER: 1168 case V4L2_CTRL_TYPE_BOOLEAN: 1169 case V4L2_CTRL_TYPE_MENU: 1170 case V4L2_CTRL_TYPE_INTEGER_MENU: 1171 case V4L2_CTRL_TYPE_STRING: 1172 case V4L2_CTRL_TYPE_BITMASK: 1173 to->minimum = from->minimum; 1174 to->maximum = from->maximum; 1175 to->step = from->step; 1176 to->default_value = from->default_value; 1177 break; 1178 default: 1179 to->minimum = 0; 1180 to->maximum = 0; 1181 to->step = 0; 1182 to->default_value = 0; 1183 break; 1184 } 1185 } 1186 EXPORT_SYMBOL(v4l2_query_ext_ctrl_to_v4l2_queryctrl); 1187 1188 /* Implement VIDIOC_QUERYCTRL */ 1189 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc) 1190 { 1191 struct v4l2_query_ext_ctrl qec = { qc->id }; 1192 int rc; 1193 1194 rc = v4l2_query_ext_ctrl(hdl, &qec); 1195 if (rc) 1196 return rc; 1197 1198 v4l2_query_ext_ctrl_to_v4l2_queryctrl(qc, &qec); 1199 1200 return 0; 1201 } 1202 EXPORT_SYMBOL(v4l2_queryctrl); 1203 1204 /* Implement VIDIOC_QUERYMENU */ 1205 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm) 1206 { 1207 struct v4l2_ctrl *ctrl; 1208 u32 i = qm->index; 1209 1210 ctrl = v4l2_ctrl_find(hdl, qm->id); 1211 if (!ctrl) 1212 return -EINVAL; 1213 1214 qm->reserved = 0; 1215 /* Sanity checks */ 1216 switch (ctrl->type) { 1217 case V4L2_CTRL_TYPE_MENU: 1218 if (!ctrl->qmenu) 1219 return -EINVAL; 1220 break; 1221 case V4L2_CTRL_TYPE_INTEGER_MENU: 1222 if (!ctrl->qmenu_int) 1223 return -EINVAL; 1224 break; 1225 default: 1226 return -EINVAL; 1227 } 1228 1229 if (i < ctrl->minimum || i > ctrl->maximum) 1230 return -EINVAL; 1231 1232 /* Use mask to see if this menu item should be skipped */ 1233 if (i < BITS_PER_LONG_LONG && (ctrl->menu_skip_mask & BIT_ULL(i))) 1234 return -EINVAL; 1235 /* Empty menu items should also be skipped */ 1236 if (ctrl->type == V4L2_CTRL_TYPE_MENU) { 1237 if (!ctrl->qmenu[i] || ctrl->qmenu[i][0] == '\0') 1238 return -EINVAL; 1239 strscpy(qm->name, ctrl->qmenu[i], sizeof(qm->name)); 1240 } else { 1241 qm->value = ctrl->qmenu_int[i]; 1242 } 1243 return 0; 1244 } 1245 EXPORT_SYMBOL(v4l2_querymenu); 1246 1247 /* 1248 * VIDIOC_LOG_STATUS helpers 1249 */ 1250 1251 int v4l2_ctrl_log_status(struct file *file, void *priv) 1252 { 1253 struct video_device *vfd = video_devdata(file); 1254 1255 if (vfd->v4l2_dev) { 1256 struct v4l2_fh *vfh = file_to_v4l2_fh(file); 1257 1258 v4l2_ctrl_handler_log_status(vfh->ctrl_handler, 1259 vfd->v4l2_dev->name); 1260 } 1261 1262 return 0; 1263 } 1264 EXPORT_SYMBOL(v4l2_ctrl_log_status); 1265 1266 int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd) 1267 { 1268 v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name); 1269 return 0; 1270 } 1271 EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status); 1272 1273 /* 1274 * VIDIOC_(UN)SUBSCRIBE_EVENT implementation 1275 */ 1276 1277 static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev, 1278 unsigned int elems) 1279 { 1280 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id); 1281 1282 if (!ctrl) 1283 return -EINVAL; 1284 1285 v4l2_ctrl_lock(ctrl); 1286 list_add_tail(&sev->node, &ctrl->ev_subs); 1287 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS && 1288 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) 1289 send_initial_event(sev->fh, ctrl); 1290 v4l2_ctrl_unlock(ctrl); 1291 return 0; 1292 } 1293 1294 static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev) 1295 { 1296 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id); 1297 1298 if (!ctrl) 1299 return; 1300 1301 v4l2_ctrl_lock(ctrl); 1302 list_del(&sev->node); 1303 v4l2_ctrl_unlock(ctrl); 1304 } 1305 1306 void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new) 1307 { 1308 u32 old_changes = old->u.ctrl.changes; 1309 1310 old->u.ctrl = new->u.ctrl; 1311 old->u.ctrl.changes |= old_changes; 1312 } 1313 EXPORT_SYMBOL(v4l2_ctrl_replace); 1314 1315 void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new) 1316 { 1317 new->u.ctrl.changes |= old->u.ctrl.changes; 1318 } 1319 EXPORT_SYMBOL(v4l2_ctrl_merge); 1320 1321 const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = { 1322 .add = v4l2_ctrl_add_event, 1323 .del = v4l2_ctrl_del_event, 1324 .replace = v4l2_ctrl_replace, 1325 .merge = v4l2_ctrl_merge, 1326 }; 1327 EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops); 1328 1329 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh, 1330 const struct v4l2_event_subscription *sub) 1331 { 1332 if (sub->type == V4L2_EVENT_CTRL) 1333 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops); 1334 return -EINVAL; 1335 } 1336 EXPORT_SYMBOL(v4l2_ctrl_subscribe_event); 1337 1338 int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, 1339 struct v4l2_event_subscription *sub) 1340 { 1341 if (!sd->ctrl_handler) 1342 return -EINVAL; 1343 return v4l2_ctrl_subscribe_event(fh, sub); 1344 } 1345 EXPORT_SYMBOL(v4l2_ctrl_subdev_subscribe_event); 1346 1347 /* 1348 * poll helper 1349 */ 1350 __poll_t v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait) 1351 { 1352 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1353 1354 poll_wait(file, &fh->wait, wait); 1355 if (v4l2_event_pending(fh)) 1356 return EPOLLPRI; 1357 return 0; 1358 } 1359 EXPORT_SYMBOL(v4l2_ctrl_poll); 1360