1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * V4L2 sub-device 4 * 5 * Copyright (C) 2010 Nokia Corporation 6 * 7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> 8 * Sakari Ailus <sakari.ailus@iki.fi> 9 */ 10 11 #include <linux/ioctl.h> 12 #include <linux/leds.h> 13 #include <linux/mm.h> 14 #include <linux/module.h> 15 #include <linux/slab.h> 16 #include <linux/types.h> 17 #include <linux/videodev2.h> 18 #include <linux/export.h> 19 #include <linux/version.h> 20 21 #include <media/v4l2-ctrls.h> 22 #include <media/v4l2-device.h> 23 #include <media/v4l2-ioctl.h> 24 #include <media/v4l2-fh.h> 25 #include <media/v4l2-event.h> 26 27 #include "v4l2-subdev-priv.h" 28 29 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 30 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) 31 { 32 struct v4l2_subdev_state *state; 33 static struct lock_class_key key; 34 35 state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key); 36 if (IS_ERR(state)) 37 return PTR_ERR(state); 38 39 fh->state = state; 40 41 return 0; 42 } 43 44 static void subdev_fh_free(struct v4l2_subdev_fh *fh) 45 { 46 __v4l2_subdev_state_free(fh->state); 47 fh->state = NULL; 48 } 49 50 static int subdev_open(struct file *file) 51 { 52 struct video_device *vdev = video_devdata(file); 53 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 54 struct v4l2_subdev_fh *subdev_fh; 55 int ret; 56 57 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); 58 if (subdev_fh == NULL) 59 return -ENOMEM; 60 61 ret = subdev_fh_init(subdev_fh, sd); 62 if (ret) { 63 kfree(subdev_fh); 64 return ret; 65 } 66 67 v4l2_fh_init(&subdev_fh->vfh, vdev); 68 v4l2_fh_add(&subdev_fh->vfh); 69 file->private_data = &subdev_fh->vfh; 70 71 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) { 72 struct module *owner; 73 74 owner = sd->entity.graph_obj.mdev->dev->driver->owner; 75 if (!try_module_get(owner)) { 76 ret = -EBUSY; 77 goto err; 78 } 79 subdev_fh->owner = owner; 80 } 81 82 if (sd->internal_ops && sd->internal_ops->open) { 83 ret = sd->internal_ops->open(sd, subdev_fh); 84 if (ret < 0) 85 goto err; 86 } 87 88 return 0; 89 90 err: 91 module_put(subdev_fh->owner); 92 v4l2_fh_del(&subdev_fh->vfh); 93 v4l2_fh_exit(&subdev_fh->vfh); 94 subdev_fh_free(subdev_fh); 95 kfree(subdev_fh); 96 97 return ret; 98 } 99 100 static int subdev_close(struct file *file) 101 { 102 struct video_device *vdev = video_devdata(file); 103 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 104 struct v4l2_fh *vfh = file->private_data; 105 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 106 107 if (sd->internal_ops && sd->internal_ops->close) 108 sd->internal_ops->close(sd, subdev_fh); 109 module_put(subdev_fh->owner); 110 v4l2_fh_del(vfh); 111 v4l2_fh_exit(vfh); 112 subdev_fh_free(subdev_fh); 113 kfree(subdev_fh); 114 file->private_data = NULL; 115 116 return 0; 117 } 118 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 119 static int subdev_open(struct file *file) 120 { 121 return -ENODEV; 122 } 123 124 static int subdev_close(struct file *file) 125 { 126 return -ENODEV; 127 } 128 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 129 130 static inline int check_which(u32 which) 131 { 132 if (which != V4L2_SUBDEV_FORMAT_TRY && 133 which != V4L2_SUBDEV_FORMAT_ACTIVE) 134 return -EINVAL; 135 136 return 0; 137 } 138 139 static inline int check_pad(struct v4l2_subdev *sd, u32 pad) 140 { 141 #if defined(CONFIG_MEDIA_CONTROLLER) 142 if (sd->entity.num_pads) { 143 if (pad >= sd->entity.num_pads) 144 return -EINVAL; 145 return 0; 146 } 147 #endif 148 /* allow pad 0 on subdevices not registered as media entities */ 149 if (pad > 0) 150 return -EINVAL; 151 return 0; 152 } 153 154 static int check_state_pads(u32 which, struct v4l2_subdev_state *state) 155 { 156 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads)) 157 return -EINVAL; 158 159 return 0; 160 } 161 162 static inline int check_format(struct v4l2_subdev *sd, 163 struct v4l2_subdev_state *state, 164 struct v4l2_subdev_format *format) 165 { 166 if (!format) 167 return -EINVAL; 168 169 return check_which(format->which) ? : check_pad(sd, format->pad) ? : 170 check_state_pads(format->which, state); 171 } 172 173 static int call_get_fmt(struct v4l2_subdev *sd, 174 struct v4l2_subdev_state *state, 175 struct v4l2_subdev_format *format) 176 { 177 return check_format(sd, state, format) ? : 178 sd->ops->pad->get_fmt(sd, state, format); 179 } 180 181 static int call_set_fmt(struct v4l2_subdev *sd, 182 struct v4l2_subdev_state *state, 183 struct v4l2_subdev_format *format) 184 { 185 return check_format(sd, state, format) ? : 186 sd->ops->pad->set_fmt(sd, state, format); 187 } 188 189 static int call_enum_mbus_code(struct v4l2_subdev *sd, 190 struct v4l2_subdev_state *state, 191 struct v4l2_subdev_mbus_code_enum *code) 192 { 193 if (!code) 194 return -EINVAL; 195 196 return check_which(code->which) ? : check_pad(sd, code->pad) ? : 197 check_state_pads(code->which, state) ? : 198 sd->ops->pad->enum_mbus_code(sd, state, code); 199 } 200 201 static int call_enum_frame_size(struct v4l2_subdev *sd, 202 struct v4l2_subdev_state *state, 203 struct v4l2_subdev_frame_size_enum *fse) 204 { 205 if (!fse) 206 return -EINVAL; 207 208 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? : 209 check_state_pads(fse->which, state) ? : 210 sd->ops->pad->enum_frame_size(sd, state, fse); 211 } 212 213 static inline int check_frame_interval(struct v4l2_subdev *sd, 214 struct v4l2_subdev_frame_interval *fi) 215 { 216 if (!fi) 217 return -EINVAL; 218 219 return check_pad(sd, fi->pad); 220 } 221 222 static int call_g_frame_interval(struct v4l2_subdev *sd, 223 struct v4l2_subdev_frame_interval *fi) 224 { 225 return check_frame_interval(sd, fi) ? : 226 sd->ops->video->g_frame_interval(sd, fi); 227 } 228 229 static int call_s_frame_interval(struct v4l2_subdev *sd, 230 struct v4l2_subdev_frame_interval *fi) 231 { 232 return check_frame_interval(sd, fi) ? : 233 sd->ops->video->s_frame_interval(sd, fi); 234 } 235 236 static int call_enum_frame_interval(struct v4l2_subdev *sd, 237 struct v4l2_subdev_state *state, 238 struct v4l2_subdev_frame_interval_enum *fie) 239 { 240 if (!fie) 241 return -EINVAL; 242 243 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? : 244 check_state_pads(fie->which, state) ? : 245 sd->ops->pad->enum_frame_interval(sd, state, fie); 246 } 247 248 static inline int check_selection(struct v4l2_subdev *sd, 249 struct v4l2_subdev_state *state, 250 struct v4l2_subdev_selection *sel) 251 { 252 if (!sel) 253 return -EINVAL; 254 255 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? : 256 check_state_pads(sel->which, state); 257 } 258 259 static int call_get_selection(struct v4l2_subdev *sd, 260 struct v4l2_subdev_state *state, 261 struct v4l2_subdev_selection *sel) 262 { 263 return check_selection(sd, state, sel) ? : 264 sd->ops->pad->get_selection(sd, state, sel); 265 } 266 267 static int call_set_selection(struct v4l2_subdev *sd, 268 struct v4l2_subdev_state *state, 269 struct v4l2_subdev_selection *sel) 270 { 271 return check_selection(sd, state, sel) ? : 272 sd->ops->pad->set_selection(sd, state, sel); 273 } 274 275 static inline int check_edid(struct v4l2_subdev *sd, 276 struct v4l2_subdev_edid *edid) 277 { 278 if (!edid) 279 return -EINVAL; 280 281 if (edid->blocks && edid->edid == NULL) 282 return -EINVAL; 283 284 return check_pad(sd, edid->pad); 285 } 286 287 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 288 { 289 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid); 290 } 291 292 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 293 { 294 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid); 295 } 296 297 static int call_dv_timings_cap(struct v4l2_subdev *sd, 298 struct v4l2_dv_timings_cap *cap) 299 { 300 if (!cap) 301 return -EINVAL; 302 303 return check_pad(sd, cap->pad) ? : 304 sd->ops->pad->dv_timings_cap(sd, cap); 305 } 306 307 static int call_enum_dv_timings(struct v4l2_subdev *sd, 308 struct v4l2_enum_dv_timings *dvt) 309 { 310 if (!dvt) 311 return -EINVAL; 312 313 return check_pad(sd, dvt->pad) ? : 314 sd->ops->pad->enum_dv_timings(sd, dvt); 315 } 316 317 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad, 318 struct v4l2_mbus_config *config) 319 { 320 return check_pad(sd, pad) ? : 321 sd->ops->pad->get_mbus_config(sd, pad, config); 322 } 323 324 static int call_s_stream(struct v4l2_subdev *sd, int enable) 325 { 326 int ret; 327 328 #if IS_REACHABLE(CONFIG_LEDS_CLASS) 329 if (!IS_ERR_OR_NULL(sd->privacy_led)) { 330 if (enable) 331 led_set_brightness(sd->privacy_led, 332 sd->privacy_led->max_brightness); 333 else 334 led_set_brightness(sd->privacy_led, 0); 335 } 336 #endif 337 ret = sd->ops->video->s_stream(sd, enable); 338 339 if (!enable && ret < 0) { 340 dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret); 341 return 0; 342 } 343 344 return ret; 345 } 346 347 #ifdef CONFIG_MEDIA_CONTROLLER 348 /* 349 * Create state-management wrapper for pad ops dealing with subdev state. The 350 * wrapper handles the case where the caller does not provide the called 351 * subdev's state. This should be removed when all the callers are fixed. 352 */ 353 #define DEFINE_STATE_WRAPPER(f, arg_type) \ 354 static int call_##f##_state(struct v4l2_subdev *sd, \ 355 struct v4l2_subdev_state *_state, \ 356 arg_type *arg) \ 357 { \ 358 struct v4l2_subdev_state *state = _state; \ 359 int ret; \ 360 if (!_state) \ 361 state = v4l2_subdev_lock_and_get_active_state(sd); \ 362 ret = call_##f(sd, state, arg); \ 363 if (!_state && state) \ 364 v4l2_subdev_unlock_state(state); \ 365 return ret; \ 366 } 367 368 #else /* CONFIG_MEDIA_CONTROLLER */ 369 370 #define DEFINE_STATE_WRAPPER(f, arg_type) \ 371 static int call_##f##_state(struct v4l2_subdev *sd, \ 372 struct v4l2_subdev_state *state, \ 373 arg_type *arg) \ 374 { \ 375 return call_##f(sd, state, arg); \ 376 } 377 378 #endif /* CONFIG_MEDIA_CONTROLLER */ 379 380 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format); 381 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format); 382 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum); 383 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum); 384 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum); 385 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection); 386 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection); 387 388 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = { 389 .get_fmt = call_get_fmt_state, 390 .set_fmt = call_set_fmt_state, 391 .enum_mbus_code = call_enum_mbus_code_state, 392 .enum_frame_size = call_enum_frame_size_state, 393 .enum_frame_interval = call_enum_frame_interval_state, 394 .get_selection = call_get_selection_state, 395 .set_selection = call_set_selection_state, 396 .get_edid = call_get_edid, 397 .set_edid = call_set_edid, 398 .dv_timings_cap = call_dv_timings_cap, 399 .enum_dv_timings = call_enum_dv_timings, 400 .get_mbus_config = call_get_mbus_config, 401 }; 402 403 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = { 404 .g_frame_interval = call_g_frame_interval, 405 .s_frame_interval = call_s_frame_interval, 406 .s_stream = call_s_stream, 407 }; 408 409 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = { 410 .pad = &v4l2_subdev_call_pad_wrappers, 411 .video = &v4l2_subdev_call_video_wrappers, 412 }; 413 EXPORT_SYMBOL(v4l2_subdev_call_wrappers); 414 415 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 416 417 static struct v4l2_subdev_state * 418 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh, 419 unsigned int cmd, void *arg) 420 { 421 u32 which; 422 423 switch (cmd) { 424 default: 425 return NULL; 426 case VIDIOC_SUBDEV_G_FMT: 427 case VIDIOC_SUBDEV_S_FMT: 428 which = ((struct v4l2_subdev_format *)arg)->which; 429 break; 430 case VIDIOC_SUBDEV_G_CROP: 431 case VIDIOC_SUBDEV_S_CROP: 432 which = ((struct v4l2_subdev_crop *)arg)->which; 433 break; 434 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: 435 which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which; 436 break; 437 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: 438 which = ((struct v4l2_subdev_frame_size_enum *)arg)->which; 439 break; 440 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: 441 which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which; 442 break; 443 case VIDIOC_SUBDEV_G_SELECTION: 444 case VIDIOC_SUBDEV_S_SELECTION: 445 which = ((struct v4l2_subdev_selection *)arg)->which; 446 break; 447 } 448 449 return which == V4L2_SUBDEV_FORMAT_TRY ? 450 subdev_fh->state : 451 v4l2_subdev_get_unlocked_active_state(sd); 452 } 453 454 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg, 455 struct v4l2_subdev_state *state) 456 { 457 struct video_device *vdev = video_devdata(file); 458 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 459 struct v4l2_fh *vfh = file->private_data; 460 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags); 461 int rval; 462 463 switch (cmd) { 464 case VIDIOC_SUBDEV_QUERYCAP: { 465 struct v4l2_subdev_capability *cap = arg; 466 467 memset(cap->reserved, 0, sizeof(cap->reserved)); 468 cap->version = LINUX_VERSION_CODE; 469 cap->capabilities = ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0; 470 471 return 0; 472 } 473 474 case VIDIOC_QUERYCTRL: 475 /* 476 * TODO: this really should be folded into v4l2_queryctrl (this 477 * currently returns -EINVAL for NULL control handlers). 478 * However, v4l2_queryctrl() is still called directly by 479 * drivers as well and until that has been addressed I believe 480 * it is safer to do the check here. The same is true for the 481 * other control ioctls below. 482 */ 483 if (!vfh->ctrl_handler) 484 return -ENOTTY; 485 return v4l2_queryctrl(vfh->ctrl_handler, arg); 486 487 case VIDIOC_QUERY_EXT_CTRL: 488 if (!vfh->ctrl_handler) 489 return -ENOTTY; 490 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg); 491 492 case VIDIOC_QUERYMENU: 493 if (!vfh->ctrl_handler) 494 return -ENOTTY; 495 return v4l2_querymenu(vfh->ctrl_handler, arg); 496 497 case VIDIOC_G_CTRL: 498 if (!vfh->ctrl_handler) 499 return -ENOTTY; 500 return v4l2_g_ctrl(vfh->ctrl_handler, arg); 501 502 case VIDIOC_S_CTRL: 503 if (!vfh->ctrl_handler) 504 return -ENOTTY; 505 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); 506 507 case VIDIOC_G_EXT_CTRLS: 508 if (!vfh->ctrl_handler) 509 return -ENOTTY; 510 return v4l2_g_ext_ctrls(vfh->ctrl_handler, 511 vdev, sd->v4l2_dev->mdev, arg); 512 513 case VIDIOC_S_EXT_CTRLS: 514 if (!vfh->ctrl_handler) 515 return -ENOTTY; 516 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, 517 vdev, sd->v4l2_dev->mdev, arg); 518 519 case VIDIOC_TRY_EXT_CTRLS: 520 if (!vfh->ctrl_handler) 521 return -ENOTTY; 522 return v4l2_try_ext_ctrls(vfh->ctrl_handler, 523 vdev, sd->v4l2_dev->mdev, arg); 524 525 case VIDIOC_DQEVENT: 526 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 527 return -ENOIOCTLCMD; 528 529 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); 530 531 case VIDIOC_SUBSCRIBE_EVENT: 532 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); 533 534 case VIDIOC_UNSUBSCRIBE_EVENT: 535 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); 536 537 #ifdef CONFIG_VIDEO_ADV_DEBUG 538 case VIDIOC_DBG_G_REGISTER: 539 { 540 struct v4l2_dbg_register *p = arg; 541 542 if (!capable(CAP_SYS_ADMIN)) 543 return -EPERM; 544 return v4l2_subdev_call(sd, core, g_register, p); 545 } 546 case VIDIOC_DBG_S_REGISTER: 547 { 548 struct v4l2_dbg_register *p = arg; 549 550 if (!capable(CAP_SYS_ADMIN)) 551 return -EPERM; 552 return v4l2_subdev_call(sd, core, s_register, p); 553 } 554 case VIDIOC_DBG_G_CHIP_INFO: 555 { 556 struct v4l2_dbg_chip_info *p = arg; 557 558 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr) 559 return -EINVAL; 560 if (sd->ops->core && sd->ops->core->s_register) 561 p->flags |= V4L2_CHIP_FL_WRITABLE; 562 if (sd->ops->core && sd->ops->core->g_register) 563 p->flags |= V4L2_CHIP_FL_READABLE; 564 strscpy(p->name, sd->name, sizeof(p->name)); 565 return 0; 566 } 567 #endif 568 569 case VIDIOC_LOG_STATUS: { 570 int ret; 571 572 pr_info("%s: ================= START STATUS =================\n", 573 sd->name); 574 ret = v4l2_subdev_call(sd, core, log_status); 575 pr_info("%s: ================== END STATUS ==================\n", 576 sd->name); 577 return ret; 578 } 579 580 case VIDIOC_SUBDEV_G_FMT: { 581 struct v4l2_subdev_format *format = arg; 582 583 memset(format->reserved, 0, sizeof(format->reserved)); 584 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 585 return v4l2_subdev_call(sd, pad, get_fmt, state, format); 586 } 587 588 case VIDIOC_SUBDEV_S_FMT: { 589 struct v4l2_subdev_format *format = arg; 590 591 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 592 return -EPERM; 593 594 memset(format->reserved, 0, sizeof(format->reserved)); 595 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 596 return v4l2_subdev_call(sd, pad, set_fmt, state, format); 597 } 598 599 case VIDIOC_SUBDEV_G_CROP: { 600 struct v4l2_subdev_crop *crop = arg; 601 struct v4l2_subdev_selection sel; 602 603 memset(crop->reserved, 0, sizeof(crop->reserved)); 604 memset(&sel, 0, sizeof(sel)); 605 sel.which = crop->which; 606 sel.pad = crop->pad; 607 sel.target = V4L2_SEL_TGT_CROP; 608 609 rval = v4l2_subdev_call( 610 sd, pad, get_selection, state, &sel); 611 612 crop->rect = sel.r; 613 614 return rval; 615 } 616 617 case VIDIOC_SUBDEV_S_CROP: { 618 struct v4l2_subdev_crop *crop = arg; 619 struct v4l2_subdev_selection sel; 620 621 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 622 return -EPERM; 623 624 memset(crop->reserved, 0, sizeof(crop->reserved)); 625 memset(&sel, 0, sizeof(sel)); 626 sel.which = crop->which; 627 sel.pad = crop->pad; 628 sel.target = V4L2_SEL_TGT_CROP; 629 sel.r = crop->rect; 630 631 rval = v4l2_subdev_call( 632 sd, pad, set_selection, state, &sel); 633 634 crop->rect = sel.r; 635 636 return rval; 637 } 638 639 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { 640 struct v4l2_subdev_mbus_code_enum *code = arg; 641 642 memset(code->reserved, 0, sizeof(code->reserved)); 643 return v4l2_subdev_call(sd, pad, enum_mbus_code, state, 644 code); 645 } 646 647 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { 648 struct v4l2_subdev_frame_size_enum *fse = arg; 649 650 memset(fse->reserved, 0, sizeof(fse->reserved)); 651 return v4l2_subdev_call(sd, pad, enum_frame_size, state, 652 fse); 653 } 654 655 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: { 656 struct v4l2_subdev_frame_interval *fi = arg; 657 658 memset(fi->reserved, 0, sizeof(fi->reserved)); 659 return v4l2_subdev_call(sd, video, g_frame_interval, arg); 660 } 661 662 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: { 663 struct v4l2_subdev_frame_interval *fi = arg; 664 665 if (ro_subdev) 666 return -EPERM; 667 668 memset(fi->reserved, 0, sizeof(fi->reserved)); 669 return v4l2_subdev_call(sd, video, s_frame_interval, arg); 670 } 671 672 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { 673 struct v4l2_subdev_frame_interval_enum *fie = arg; 674 675 memset(fie->reserved, 0, sizeof(fie->reserved)); 676 return v4l2_subdev_call(sd, pad, enum_frame_interval, state, 677 fie); 678 } 679 680 case VIDIOC_SUBDEV_G_SELECTION: { 681 struct v4l2_subdev_selection *sel = arg; 682 683 memset(sel->reserved, 0, sizeof(sel->reserved)); 684 return v4l2_subdev_call( 685 sd, pad, get_selection, state, sel); 686 } 687 688 case VIDIOC_SUBDEV_S_SELECTION: { 689 struct v4l2_subdev_selection *sel = arg; 690 691 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 692 return -EPERM; 693 694 memset(sel->reserved, 0, sizeof(sel->reserved)); 695 return v4l2_subdev_call( 696 sd, pad, set_selection, state, sel); 697 } 698 699 case VIDIOC_G_EDID: { 700 struct v4l2_subdev_edid *edid = arg; 701 702 return v4l2_subdev_call(sd, pad, get_edid, edid); 703 } 704 705 case VIDIOC_S_EDID: { 706 struct v4l2_subdev_edid *edid = arg; 707 708 return v4l2_subdev_call(sd, pad, set_edid, edid); 709 } 710 711 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: { 712 struct v4l2_dv_timings_cap *cap = arg; 713 714 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap); 715 } 716 717 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: { 718 struct v4l2_enum_dv_timings *dvt = arg; 719 720 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt); 721 } 722 723 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS: 724 return v4l2_subdev_call(sd, video, query_dv_timings, arg); 725 726 case VIDIOC_SUBDEV_G_DV_TIMINGS: 727 return v4l2_subdev_call(sd, video, g_dv_timings, arg); 728 729 case VIDIOC_SUBDEV_S_DV_TIMINGS: 730 if (ro_subdev) 731 return -EPERM; 732 733 return v4l2_subdev_call(sd, video, s_dv_timings, arg); 734 735 case VIDIOC_SUBDEV_G_STD: 736 return v4l2_subdev_call(sd, video, g_std, arg); 737 738 case VIDIOC_SUBDEV_S_STD: { 739 v4l2_std_id *std = arg; 740 741 if (ro_subdev) 742 return -EPERM; 743 744 return v4l2_subdev_call(sd, video, s_std, *std); 745 } 746 747 case VIDIOC_SUBDEV_ENUMSTD: { 748 struct v4l2_standard *p = arg; 749 v4l2_std_id id; 750 751 if (v4l2_subdev_call(sd, video, g_tvnorms, &id)) 752 return -EINVAL; 753 754 return v4l_video_std_enumstd(p, id); 755 } 756 757 case VIDIOC_SUBDEV_QUERYSTD: 758 return v4l2_subdev_call(sd, video, querystd, arg); 759 760 default: 761 return v4l2_subdev_call(sd, core, ioctl, cmd, arg); 762 } 763 764 return 0; 765 } 766 767 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg) 768 { 769 struct video_device *vdev = video_devdata(file); 770 struct mutex *lock = vdev->lock; 771 long ret = -ENODEV; 772 773 if (lock && mutex_lock_interruptible(lock)) 774 return -ERESTARTSYS; 775 776 if (video_is_registered(vdev)) { 777 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 778 struct v4l2_fh *vfh = file->private_data; 779 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 780 struct v4l2_subdev_state *state; 781 782 state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg); 783 784 if (state) 785 v4l2_subdev_lock_state(state); 786 787 ret = subdev_do_ioctl(file, cmd, arg, state); 788 789 if (state) 790 v4l2_subdev_unlock_state(state); 791 } 792 793 if (lock) 794 mutex_unlock(lock); 795 return ret; 796 } 797 798 static long subdev_ioctl(struct file *file, unsigned int cmd, 799 unsigned long arg) 800 { 801 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock); 802 } 803 804 #ifdef CONFIG_COMPAT 805 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 806 unsigned long arg) 807 { 808 struct video_device *vdev = video_devdata(file); 809 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 810 811 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg); 812 } 813 #endif 814 815 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 816 static long subdev_ioctl(struct file *file, unsigned int cmd, 817 unsigned long arg) 818 { 819 return -ENODEV; 820 } 821 822 #ifdef CONFIG_COMPAT 823 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 824 unsigned long arg) 825 { 826 return -ENODEV; 827 } 828 #endif 829 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 830 831 static __poll_t subdev_poll(struct file *file, poll_table *wait) 832 { 833 struct video_device *vdev = video_devdata(file); 834 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 835 struct v4l2_fh *fh = file->private_data; 836 837 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 838 return EPOLLERR; 839 840 poll_wait(file, &fh->wait, wait); 841 842 if (v4l2_event_pending(fh)) 843 return EPOLLPRI; 844 845 return 0; 846 } 847 848 const struct v4l2_file_operations v4l2_subdev_fops = { 849 .owner = THIS_MODULE, 850 .open = subdev_open, 851 .unlocked_ioctl = subdev_ioctl, 852 #ifdef CONFIG_COMPAT 853 .compat_ioctl32 = subdev_compat_ioctl32, 854 #endif 855 .release = subdev_close, 856 .poll = subdev_poll, 857 }; 858 859 #ifdef CONFIG_MEDIA_CONTROLLER 860 861 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity, 862 struct fwnode_endpoint *endpoint) 863 { 864 struct fwnode_handle *fwnode; 865 struct v4l2_subdev *sd; 866 867 if (!is_media_entity_v4l2_subdev(entity)) 868 return -EINVAL; 869 870 sd = media_entity_to_v4l2_subdev(entity); 871 872 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode); 873 fwnode_handle_put(fwnode); 874 875 if (device_match_fwnode(sd->dev, fwnode)) 876 return endpoint->port; 877 878 return -ENXIO; 879 } 880 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1); 881 882 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd, 883 struct media_link *link, 884 struct v4l2_subdev_format *source_fmt, 885 struct v4l2_subdev_format *sink_fmt) 886 { 887 bool pass = true; 888 889 /* The width, height and code must match. */ 890 if (source_fmt->format.width != sink_fmt->format.width) { 891 dev_dbg(sd->entity.graph_obj.mdev->dev, 892 "%s: width does not match (source %u, sink %u)\n", 893 __func__, 894 source_fmt->format.width, sink_fmt->format.width); 895 pass = false; 896 } 897 898 if (source_fmt->format.height != sink_fmt->format.height) { 899 dev_dbg(sd->entity.graph_obj.mdev->dev, 900 "%s: height does not match (source %u, sink %u)\n", 901 __func__, 902 source_fmt->format.height, sink_fmt->format.height); 903 pass = false; 904 } 905 906 if (source_fmt->format.code != sink_fmt->format.code) { 907 dev_dbg(sd->entity.graph_obj.mdev->dev, 908 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n", 909 __func__, 910 source_fmt->format.code, sink_fmt->format.code); 911 pass = false; 912 } 913 914 /* The field order must match, or the sink field order must be NONE 915 * to support interlaced hardware connected to bridges that support 916 * progressive formats only. 917 */ 918 if (source_fmt->format.field != sink_fmt->format.field && 919 sink_fmt->format.field != V4L2_FIELD_NONE) { 920 dev_dbg(sd->entity.graph_obj.mdev->dev, 921 "%s: field does not match (source %u, sink %u)\n", 922 __func__, 923 source_fmt->format.field, sink_fmt->format.field); 924 pass = false; 925 } 926 927 if (pass) 928 return 0; 929 930 dev_dbg(sd->entity.graph_obj.mdev->dev, 931 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__, 932 link->source->entity->name, link->source->index, 933 link->sink->entity->name, link->sink->index); 934 935 return -EPIPE; 936 } 937 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default); 938 939 static int 940 v4l2_subdev_link_validate_get_format(struct media_pad *pad, 941 struct v4l2_subdev_format *fmt) 942 { 943 if (is_media_entity_v4l2_subdev(pad->entity)) { 944 struct v4l2_subdev *sd = 945 media_entity_to_v4l2_subdev(pad->entity); 946 947 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE; 948 fmt->pad = pad->index; 949 return v4l2_subdev_call_state_active(sd, pad, get_fmt, fmt); 950 } 951 952 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L, 953 "Driver bug! Wrong media entity type 0x%08x, entity %s\n", 954 pad->entity->function, pad->entity->name); 955 956 return -EINVAL; 957 } 958 959 int v4l2_subdev_link_validate(struct media_link *link) 960 { 961 struct v4l2_subdev *sink; 962 struct v4l2_subdev_format sink_fmt, source_fmt; 963 int rval; 964 965 rval = v4l2_subdev_link_validate_get_format( 966 link->source, &source_fmt); 967 if (rval < 0) 968 return 0; 969 970 rval = v4l2_subdev_link_validate_get_format( 971 link->sink, &sink_fmt); 972 if (rval < 0) 973 return 0; 974 975 sink = media_entity_to_v4l2_subdev(link->sink->entity); 976 977 rval = v4l2_subdev_call(sink, pad, link_validate, link, 978 &source_fmt, &sink_fmt); 979 if (rval != -ENOIOCTLCMD) 980 return rval; 981 982 return v4l2_subdev_link_validate_default( 983 sink, link, &source_fmt, &sink_fmt); 984 } 985 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate); 986 987 struct v4l2_subdev_state * 988 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name, 989 struct lock_class_key *lock_key) 990 { 991 struct v4l2_subdev_state *state; 992 int ret; 993 994 state = kzalloc(sizeof(*state), GFP_KERNEL); 995 if (!state) 996 return ERR_PTR(-ENOMEM); 997 998 __mutex_init(&state->_lock, lock_name, lock_key); 999 if (sd->state_lock) 1000 state->lock = sd->state_lock; 1001 else 1002 state->lock = &state->_lock; 1003 1004 if (sd->entity.num_pads) { 1005 state->pads = kvcalloc(sd->entity.num_pads, 1006 sizeof(*state->pads), GFP_KERNEL); 1007 if (!state->pads) { 1008 ret = -ENOMEM; 1009 goto err; 1010 } 1011 } 1012 1013 /* 1014 * There can be no race at this point, but we lock the state anyway to 1015 * satisfy lockdep checks. 1016 */ 1017 v4l2_subdev_lock_state(state); 1018 ret = v4l2_subdev_call(sd, pad, init_cfg, state); 1019 v4l2_subdev_unlock_state(state); 1020 1021 if (ret < 0 && ret != -ENOIOCTLCMD) 1022 goto err; 1023 1024 return state; 1025 1026 err: 1027 if (state && state->pads) 1028 kvfree(state->pads); 1029 1030 kfree(state); 1031 1032 return ERR_PTR(ret); 1033 } 1034 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc); 1035 1036 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state) 1037 { 1038 if (!state) 1039 return; 1040 1041 mutex_destroy(&state->_lock); 1042 1043 kvfree(state->pads); 1044 kfree(state); 1045 } 1046 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free); 1047 1048 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name, 1049 struct lock_class_key *key) 1050 { 1051 struct v4l2_subdev_state *state; 1052 1053 state = __v4l2_subdev_state_alloc(sd, name, key); 1054 if (IS_ERR(state)) 1055 return PTR_ERR(state); 1056 1057 sd->active_state = state; 1058 1059 return 0; 1060 } 1061 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize); 1062 1063 void v4l2_subdev_cleanup(struct v4l2_subdev *sd) 1064 { 1065 __v4l2_subdev_state_free(sd->active_state); 1066 sd->active_state = NULL; 1067 } 1068 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup); 1069 1070 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 1071 1072 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state, 1073 struct v4l2_subdev_format *format) 1074 { 1075 struct v4l2_mbus_framefmt *fmt; 1076 1077 if (format->pad >= sd->entity.num_pads) 1078 return -EINVAL; 1079 1080 fmt = v4l2_subdev_get_pad_format(sd, state, format->pad); 1081 if (!fmt) 1082 return -EINVAL; 1083 1084 format->format = *fmt; 1085 1086 return 0; 1087 } 1088 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt); 1089 1090 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 1091 1092 #endif /* CONFIG_MEDIA_CONTROLLER */ 1093 1094 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) 1095 { 1096 INIT_LIST_HEAD(&sd->list); 1097 BUG_ON(!ops); 1098 sd->ops = ops; 1099 sd->v4l2_dev = NULL; 1100 sd->flags = 0; 1101 sd->name[0] = '\0'; 1102 sd->grp_id = 0; 1103 sd->dev_priv = NULL; 1104 sd->host_priv = NULL; 1105 sd->privacy_led = NULL; 1106 #if defined(CONFIG_MEDIA_CONTROLLER) 1107 sd->entity.name = sd->name; 1108 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV; 1109 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN; 1110 #endif 1111 } 1112 EXPORT_SYMBOL(v4l2_subdev_init); 1113 1114 void v4l2_subdev_notify_event(struct v4l2_subdev *sd, 1115 const struct v4l2_event *ev) 1116 { 1117 v4l2_event_queue(sd->devnode, ev); 1118 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev); 1119 } 1120 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event); 1121 1122 int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd) 1123 { 1124 #if IS_REACHABLE(CONFIG_LEDS_CLASS) 1125 sd->privacy_led = led_get(sd->dev, "privacy-led"); 1126 if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT) 1127 return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led), 1128 "getting privacy LED\n"); 1129 1130 if (!IS_ERR_OR_NULL(sd->privacy_led)) { 1131 mutex_lock(&sd->privacy_led->led_access); 1132 led_sysfs_disable(sd->privacy_led); 1133 led_trigger_remove(sd->privacy_led); 1134 led_set_brightness(sd->privacy_led, 0); 1135 mutex_unlock(&sd->privacy_led->led_access); 1136 } 1137 #endif 1138 return 0; 1139 } 1140 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led); 1141 1142 void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd) 1143 { 1144 #if IS_REACHABLE(CONFIG_LEDS_CLASS) 1145 if (!IS_ERR_OR_NULL(sd->privacy_led)) { 1146 mutex_lock(&sd->privacy_led->led_access); 1147 led_sysfs_enable(sd->privacy_led); 1148 mutex_unlock(&sd->privacy_led->led_access); 1149 led_put(sd->privacy_led); 1150 } 1151 #endif 1152 } 1153 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led); 1154