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/export.h> 12 #include <linux/ioctl.h> 13 #include <linux/leds.h> 14 #include <linux/mm.h> 15 #include <linux/module.h> 16 #include <linux/overflow.h> 17 #include <linux/slab.h> 18 #include <linux/types.h> 19 #include <linux/version.h> 20 #include <linux/videodev2.h> 21 22 #include <media/v4l2-ctrls.h> 23 #include <media/v4l2-device.h> 24 #include <media/v4l2-event.h> 25 #include <media/v4l2-fh.h> 26 #include <media/v4l2-ioctl.h> 27 28 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 29 /* 30 * The Streams API is an experimental feature. To use the Streams API, set 31 * 'v4l2_subdev_enable_streams_api' to 1 below. 32 */ 33 34 static bool v4l2_subdev_enable_streams_api; 35 #endif 36 37 /* 38 * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set 39 * of streams. 40 * 41 * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX 42 * restricts the total number of streams in a pad, although the stream ID is 43 * not restricted. 44 */ 45 #define V4L2_SUBDEV_MAX_STREAM_ID 63 46 47 #include "v4l2-subdev-priv.h" 48 49 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 50 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) 51 { 52 struct v4l2_subdev_state *state; 53 static struct lock_class_key key; 54 55 state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key); 56 if (IS_ERR(state)) 57 return PTR_ERR(state); 58 59 fh->state = state; 60 61 return 0; 62 } 63 64 static void subdev_fh_free(struct v4l2_subdev_fh *fh) 65 { 66 __v4l2_subdev_state_free(fh->state); 67 fh->state = NULL; 68 } 69 70 static int subdev_open(struct file *file) 71 { 72 struct video_device *vdev = video_devdata(file); 73 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 74 struct v4l2_subdev_fh *subdev_fh; 75 int ret; 76 77 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); 78 if (subdev_fh == NULL) 79 return -ENOMEM; 80 81 ret = subdev_fh_init(subdev_fh, sd); 82 if (ret) { 83 kfree(subdev_fh); 84 return ret; 85 } 86 87 v4l2_fh_init(&subdev_fh->vfh, vdev); 88 v4l2_fh_add(&subdev_fh->vfh); 89 file->private_data = &subdev_fh->vfh; 90 91 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) { 92 struct module *owner; 93 94 owner = sd->entity.graph_obj.mdev->dev->driver->owner; 95 if (!try_module_get(owner)) { 96 ret = -EBUSY; 97 goto err; 98 } 99 subdev_fh->owner = owner; 100 } 101 102 if (sd->internal_ops && sd->internal_ops->open) { 103 ret = sd->internal_ops->open(sd, subdev_fh); 104 if (ret < 0) 105 goto err; 106 } 107 108 return 0; 109 110 err: 111 module_put(subdev_fh->owner); 112 v4l2_fh_del(&subdev_fh->vfh); 113 v4l2_fh_exit(&subdev_fh->vfh); 114 subdev_fh_free(subdev_fh); 115 kfree(subdev_fh); 116 117 return ret; 118 } 119 120 static int subdev_close(struct file *file) 121 { 122 struct video_device *vdev = video_devdata(file); 123 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 124 struct v4l2_fh *vfh = file->private_data; 125 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 126 127 if (sd->internal_ops && sd->internal_ops->close) 128 sd->internal_ops->close(sd, subdev_fh); 129 module_put(subdev_fh->owner); 130 v4l2_fh_del(vfh); 131 v4l2_fh_exit(vfh); 132 subdev_fh_free(subdev_fh); 133 kfree(subdev_fh); 134 file->private_data = NULL; 135 136 return 0; 137 } 138 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 139 static int subdev_open(struct file *file) 140 { 141 return -ENODEV; 142 } 143 144 static int subdev_close(struct file *file) 145 { 146 return -ENODEV; 147 } 148 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 149 150 static inline int check_which(u32 which) 151 { 152 if (which != V4L2_SUBDEV_FORMAT_TRY && 153 which != V4L2_SUBDEV_FORMAT_ACTIVE) 154 return -EINVAL; 155 156 return 0; 157 } 158 159 static inline int check_pad(struct v4l2_subdev *sd, u32 pad) 160 { 161 #if defined(CONFIG_MEDIA_CONTROLLER) 162 if (sd->entity.num_pads) { 163 if (pad >= sd->entity.num_pads) 164 return -EINVAL; 165 return 0; 166 } 167 #endif 168 /* allow pad 0 on subdevices not registered as media entities */ 169 if (pad > 0) 170 return -EINVAL; 171 return 0; 172 } 173 174 static int check_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state, 175 u32 which, u32 pad, u32 stream) 176 { 177 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) { 178 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 179 if (!v4l2_subdev_state_get_stream_format(state, pad, stream)) 180 return -EINVAL; 181 return 0; 182 #else 183 return -EINVAL; 184 #endif 185 } 186 187 if (stream != 0) 188 return -EINVAL; 189 190 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads)) 191 return -EINVAL; 192 193 return 0; 194 } 195 196 static inline int check_format(struct v4l2_subdev *sd, 197 struct v4l2_subdev_state *state, 198 struct v4l2_subdev_format *format) 199 { 200 if (!format) 201 return -EINVAL; 202 203 return check_which(format->which) ? : check_pad(sd, format->pad) ? : 204 check_state(sd, state, format->which, format->pad, format->stream); 205 } 206 207 static int call_get_fmt(struct v4l2_subdev *sd, 208 struct v4l2_subdev_state *state, 209 struct v4l2_subdev_format *format) 210 { 211 return check_format(sd, state, format) ? : 212 sd->ops->pad->get_fmt(sd, state, format); 213 } 214 215 static int call_set_fmt(struct v4l2_subdev *sd, 216 struct v4l2_subdev_state *state, 217 struct v4l2_subdev_format *format) 218 { 219 return check_format(sd, state, format) ? : 220 sd->ops->pad->set_fmt(sd, state, format); 221 } 222 223 static int call_enum_mbus_code(struct v4l2_subdev *sd, 224 struct v4l2_subdev_state *state, 225 struct v4l2_subdev_mbus_code_enum *code) 226 { 227 if (!code) 228 return -EINVAL; 229 230 return check_which(code->which) ? : check_pad(sd, code->pad) ? : 231 check_state(sd, state, code->which, code->pad, code->stream) ? : 232 sd->ops->pad->enum_mbus_code(sd, state, code); 233 } 234 235 static int call_enum_frame_size(struct v4l2_subdev *sd, 236 struct v4l2_subdev_state *state, 237 struct v4l2_subdev_frame_size_enum *fse) 238 { 239 if (!fse) 240 return -EINVAL; 241 242 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? : 243 check_state(sd, state, fse->which, fse->pad, fse->stream) ? : 244 sd->ops->pad->enum_frame_size(sd, state, fse); 245 } 246 247 static inline int check_frame_interval(struct v4l2_subdev *sd, 248 struct v4l2_subdev_frame_interval *fi) 249 { 250 if (!fi) 251 return -EINVAL; 252 253 return check_pad(sd, fi->pad); 254 } 255 256 static int call_g_frame_interval(struct v4l2_subdev *sd, 257 struct v4l2_subdev_frame_interval *fi) 258 { 259 return check_frame_interval(sd, fi) ? : 260 sd->ops->video->g_frame_interval(sd, fi); 261 } 262 263 static int call_s_frame_interval(struct v4l2_subdev *sd, 264 struct v4l2_subdev_frame_interval *fi) 265 { 266 return check_frame_interval(sd, fi) ? : 267 sd->ops->video->s_frame_interval(sd, fi); 268 } 269 270 static int call_enum_frame_interval(struct v4l2_subdev *sd, 271 struct v4l2_subdev_state *state, 272 struct v4l2_subdev_frame_interval_enum *fie) 273 { 274 if (!fie) 275 return -EINVAL; 276 277 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? : 278 check_state(sd, state, fie->which, fie->pad, fie->stream) ? : 279 sd->ops->pad->enum_frame_interval(sd, state, fie); 280 } 281 282 static inline int check_selection(struct v4l2_subdev *sd, 283 struct v4l2_subdev_state *state, 284 struct v4l2_subdev_selection *sel) 285 { 286 if (!sel) 287 return -EINVAL; 288 289 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? : 290 check_state(sd, state, sel->which, sel->pad, sel->stream); 291 } 292 293 static int call_get_selection(struct v4l2_subdev *sd, 294 struct v4l2_subdev_state *state, 295 struct v4l2_subdev_selection *sel) 296 { 297 return check_selection(sd, state, sel) ? : 298 sd->ops->pad->get_selection(sd, state, sel); 299 } 300 301 static int call_set_selection(struct v4l2_subdev *sd, 302 struct v4l2_subdev_state *state, 303 struct v4l2_subdev_selection *sel) 304 { 305 return check_selection(sd, state, sel) ? : 306 sd->ops->pad->set_selection(sd, state, sel); 307 } 308 309 static inline int check_edid(struct v4l2_subdev *sd, 310 struct v4l2_subdev_edid *edid) 311 { 312 if (!edid) 313 return -EINVAL; 314 315 if (edid->blocks && edid->edid == NULL) 316 return -EINVAL; 317 318 return check_pad(sd, edid->pad); 319 } 320 321 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 322 { 323 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid); 324 } 325 326 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 327 { 328 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid); 329 } 330 331 static int call_dv_timings_cap(struct v4l2_subdev *sd, 332 struct v4l2_dv_timings_cap *cap) 333 { 334 if (!cap) 335 return -EINVAL; 336 337 return check_pad(sd, cap->pad) ? : 338 sd->ops->pad->dv_timings_cap(sd, cap); 339 } 340 341 static int call_enum_dv_timings(struct v4l2_subdev *sd, 342 struct v4l2_enum_dv_timings *dvt) 343 { 344 if (!dvt) 345 return -EINVAL; 346 347 return check_pad(sd, dvt->pad) ? : 348 sd->ops->pad->enum_dv_timings(sd, dvt); 349 } 350 351 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad, 352 struct v4l2_mbus_config *config) 353 { 354 return check_pad(sd, pad) ? : 355 sd->ops->pad->get_mbus_config(sd, pad, config); 356 } 357 358 static int call_s_stream(struct v4l2_subdev *sd, int enable) 359 { 360 int ret; 361 362 /* 363 * The .s_stream() operation must never be called to start or stop an 364 * already started or stopped subdev. Catch offenders but don't return 365 * an error yet to avoid regressions. 366 * 367 * As .s_stream() is mutually exclusive with the .enable_streams() and 368 * .disable_streams() operation, we can use the enabled_streams field 369 * to store the subdev streaming state. 370 */ 371 if (WARN_ON(!!sd->enabled_streams == !!enable)) 372 return 0; 373 374 #if IS_REACHABLE(CONFIG_LEDS_CLASS) 375 if (!IS_ERR_OR_NULL(sd->privacy_led)) { 376 if (enable) 377 led_set_brightness(sd->privacy_led, 378 sd->privacy_led->max_brightness); 379 else 380 led_set_brightness(sd->privacy_led, 0); 381 } 382 #endif 383 ret = sd->ops->video->s_stream(sd, enable); 384 385 if (!enable && ret < 0) { 386 dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret); 387 ret = 0; 388 } 389 390 if (!ret) 391 sd->enabled_streams = enable ? BIT(0) : 0; 392 393 return ret; 394 } 395 396 #ifdef CONFIG_MEDIA_CONTROLLER 397 /* 398 * Create state-management wrapper for pad ops dealing with subdev state. The 399 * wrapper handles the case where the caller does not provide the called 400 * subdev's state. This should be removed when all the callers are fixed. 401 */ 402 #define DEFINE_STATE_WRAPPER(f, arg_type) \ 403 static int call_##f##_state(struct v4l2_subdev *sd, \ 404 struct v4l2_subdev_state *_state, \ 405 arg_type *arg) \ 406 { \ 407 struct v4l2_subdev_state *state = _state; \ 408 int ret; \ 409 if (!_state) \ 410 state = v4l2_subdev_lock_and_get_active_state(sd); \ 411 ret = call_##f(sd, state, arg); \ 412 if (!_state && state) \ 413 v4l2_subdev_unlock_state(state); \ 414 return ret; \ 415 } 416 417 #else /* CONFIG_MEDIA_CONTROLLER */ 418 419 #define DEFINE_STATE_WRAPPER(f, arg_type) \ 420 static int call_##f##_state(struct v4l2_subdev *sd, \ 421 struct v4l2_subdev_state *state, \ 422 arg_type *arg) \ 423 { \ 424 return call_##f(sd, state, arg); \ 425 } 426 427 #endif /* CONFIG_MEDIA_CONTROLLER */ 428 429 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format); 430 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format); 431 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum); 432 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum); 433 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum); 434 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection); 435 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection); 436 437 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = { 438 .get_fmt = call_get_fmt_state, 439 .set_fmt = call_set_fmt_state, 440 .enum_mbus_code = call_enum_mbus_code_state, 441 .enum_frame_size = call_enum_frame_size_state, 442 .enum_frame_interval = call_enum_frame_interval_state, 443 .get_selection = call_get_selection_state, 444 .set_selection = call_set_selection_state, 445 .get_edid = call_get_edid, 446 .set_edid = call_set_edid, 447 .dv_timings_cap = call_dv_timings_cap, 448 .enum_dv_timings = call_enum_dv_timings, 449 .get_mbus_config = call_get_mbus_config, 450 }; 451 452 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = { 453 .g_frame_interval = call_g_frame_interval, 454 .s_frame_interval = call_s_frame_interval, 455 .s_stream = call_s_stream, 456 }; 457 458 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = { 459 .pad = &v4l2_subdev_call_pad_wrappers, 460 .video = &v4l2_subdev_call_video_wrappers, 461 }; 462 EXPORT_SYMBOL(v4l2_subdev_call_wrappers); 463 464 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 465 466 static struct v4l2_subdev_state * 467 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh, 468 unsigned int cmd, void *arg) 469 { 470 u32 which; 471 472 switch (cmd) { 473 default: 474 return NULL; 475 case VIDIOC_SUBDEV_G_FMT: 476 case VIDIOC_SUBDEV_S_FMT: 477 which = ((struct v4l2_subdev_format *)arg)->which; 478 break; 479 case VIDIOC_SUBDEV_G_CROP: 480 case VIDIOC_SUBDEV_S_CROP: 481 which = ((struct v4l2_subdev_crop *)arg)->which; 482 break; 483 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: 484 which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which; 485 break; 486 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: 487 which = ((struct v4l2_subdev_frame_size_enum *)arg)->which; 488 break; 489 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: 490 which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which; 491 break; 492 case VIDIOC_SUBDEV_G_SELECTION: 493 case VIDIOC_SUBDEV_S_SELECTION: 494 which = ((struct v4l2_subdev_selection *)arg)->which; 495 break; 496 case VIDIOC_SUBDEV_G_ROUTING: 497 case VIDIOC_SUBDEV_S_ROUTING: 498 which = ((struct v4l2_subdev_routing *)arg)->which; 499 break; 500 } 501 502 return which == V4L2_SUBDEV_FORMAT_TRY ? 503 subdev_fh->state : 504 v4l2_subdev_get_unlocked_active_state(sd); 505 } 506 507 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg, 508 struct v4l2_subdev_state *state) 509 { 510 struct video_device *vdev = video_devdata(file); 511 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 512 struct v4l2_fh *vfh = file->private_data; 513 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 514 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags); 515 bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS; 516 bool client_supports_streams = subdev_fh->client_caps & 517 V4L2_SUBDEV_CLIENT_CAP_STREAMS; 518 int rval; 519 520 switch (cmd) { 521 case VIDIOC_SUBDEV_QUERYCAP: { 522 struct v4l2_subdev_capability *cap = arg; 523 524 memset(cap->reserved, 0, sizeof(cap->reserved)); 525 cap->version = LINUX_VERSION_CODE; 526 cap->capabilities = 527 (ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) | 528 (streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0); 529 530 return 0; 531 } 532 533 case VIDIOC_QUERYCTRL: 534 /* 535 * TODO: this really should be folded into v4l2_queryctrl (this 536 * currently returns -EINVAL for NULL control handlers). 537 * However, v4l2_queryctrl() is still called directly by 538 * drivers as well and until that has been addressed I believe 539 * it is safer to do the check here. The same is true for the 540 * other control ioctls below. 541 */ 542 if (!vfh->ctrl_handler) 543 return -ENOTTY; 544 return v4l2_queryctrl(vfh->ctrl_handler, arg); 545 546 case VIDIOC_QUERY_EXT_CTRL: 547 if (!vfh->ctrl_handler) 548 return -ENOTTY; 549 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg); 550 551 case VIDIOC_QUERYMENU: 552 if (!vfh->ctrl_handler) 553 return -ENOTTY; 554 return v4l2_querymenu(vfh->ctrl_handler, arg); 555 556 case VIDIOC_G_CTRL: 557 if (!vfh->ctrl_handler) 558 return -ENOTTY; 559 return v4l2_g_ctrl(vfh->ctrl_handler, arg); 560 561 case VIDIOC_S_CTRL: 562 if (!vfh->ctrl_handler) 563 return -ENOTTY; 564 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); 565 566 case VIDIOC_G_EXT_CTRLS: 567 if (!vfh->ctrl_handler) 568 return -ENOTTY; 569 return v4l2_g_ext_ctrls(vfh->ctrl_handler, 570 vdev, sd->v4l2_dev->mdev, arg); 571 572 case VIDIOC_S_EXT_CTRLS: 573 if (!vfh->ctrl_handler) 574 return -ENOTTY; 575 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, 576 vdev, sd->v4l2_dev->mdev, arg); 577 578 case VIDIOC_TRY_EXT_CTRLS: 579 if (!vfh->ctrl_handler) 580 return -ENOTTY; 581 return v4l2_try_ext_ctrls(vfh->ctrl_handler, 582 vdev, sd->v4l2_dev->mdev, arg); 583 584 case VIDIOC_DQEVENT: 585 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 586 return -ENOIOCTLCMD; 587 588 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); 589 590 case VIDIOC_SUBSCRIBE_EVENT: 591 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); 592 593 case VIDIOC_UNSUBSCRIBE_EVENT: 594 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); 595 596 #ifdef CONFIG_VIDEO_ADV_DEBUG 597 case VIDIOC_DBG_G_REGISTER: 598 { 599 struct v4l2_dbg_register *p = arg; 600 601 if (!capable(CAP_SYS_ADMIN)) 602 return -EPERM; 603 return v4l2_subdev_call(sd, core, g_register, p); 604 } 605 case VIDIOC_DBG_S_REGISTER: 606 { 607 struct v4l2_dbg_register *p = arg; 608 609 if (!capable(CAP_SYS_ADMIN)) 610 return -EPERM; 611 return v4l2_subdev_call(sd, core, s_register, p); 612 } 613 case VIDIOC_DBG_G_CHIP_INFO: 614 { 615 struct v4l2_dbg_chip_info *p = arg; 616 617 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr) 618 return -EINVAL; 619 if (sd->ops->core && sd->ops->core->s_register) 620 p->flags |= V4L2_CHIP_FL_WRITABLE; 621 if (sd->ops->core && sd->ops->core->g_register) 622 p->flags |= V4L2_CHIP_FL_READABLE; 623 strscpy(p->name, sd->name, sizeof(p->name)); 624 return 0; 625 } 626 #endif 627 628 case VIDIOC_LOG_STATUS: { 629 int ret; 630 631 pr_info("%s: ================= START STATUS =================\n", 632 sd->name); 633 ret = v4l2_subdev_call(sd, core, log_status); 634 pr_info("%s: ================== END STATUS ==================\n", 635 sd->name); 636 return ret; 637 } 638 639 case VIDIOC_SUBDEV_G_FMT: { 640 struct v4l2_subdev_format *format = arg; 641 642 if (!client_supports_streams) 643 format->stream = 0; 644 645 memset(format->reserved, 0, sizeof(format->reserved)); 646 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 647 return v4l2_subdev_call(sd, pad, get_fmt, state, format); 648 } 649 650 case VIDIOC_SUBDEV_S_FMT: { 651 struct v4l2_subdev_format *format = arg; 652 653 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 654 return -EPERM; 655 656 if (!client_supports_streams) 657 format->stream = 0; 658 659 memset(format->reserved, 0, sizeof(format->reserved)); 660 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 661 return v4l2_subdev_call(sd, pad, set_fmt, state, format); 662 } 663 664 case VIDIOC_SUBDEV_G_CROP: { 665 struct v4l2_subdev_crop *crop = arg; 666 struct v4l2_subdev_selection sel; 667 668 if (!client_supports_streams) 669 crop->stream = 0; 670 671 memset(crop->reserved, 0, sizeof(crop->reserved)); 672 memset(&sel, 0, sizeof(sel)); 673 sel.which = crop->which; 674 sel.pad = crop->pad; 675 sel.target = V4L2_SEL_TGT_CROP; 676 677 rval = v4l2_subdev_call( 678 sd, pad, get_selection, state, &sel); 679 680 crop->rect = sel.r; 681 682 return rval; 683 } 684 685 case VIDIOC_SUBDEV_S_CROP: { 686 struct v4l2_subdev_crop *crop = arg; 687 struct v4l2_subdev_selection sel; 688 689 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 690 return -EPERM; 691 692 if (!client_supports_streams) 693 crop->stream = 0; 694 695 memset(crop->reserved, 0, sizeof(crop->reserved)); 696 memset(&sel, 0, sizeof(sel)); 697 sel.which = crop->which; 698 sel.pad = crop->pad; 699 sel.target = V4L2_SEL_TGT_CROP; 700 sel.r = crop->rect; 701 702 rval = v4l2_subdev_call( 703 sd, pad, set_selection, state, &sel); 704 705 crop->rect = sel.r; 706 707 return rval; 708 } 709 710 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { 711 struct v4l2_subdev_mbus_code_enum *code = arg; 712 713 if (!client_supports_streams) 714 code->stream = 0; 715 716 memset(code->reserved, 0, sizeof(code->reserved)); 717 return v4l2_subdev_call(sd, pad, enum_mbus_code, state, 718 code); 719 } 720 721 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { 722 struct v4l2_subdev_frame_size_enum *fse = arg; 723 724 if (!client_supports_streams) 725 fse->stream = 0; 726 727 memset(fse->reserved, 0, sizeof(fse->reserved)); 728 return v4l2_subdev_call(sd, pad, enum_frame_size, state, 729 fse); 730 } 731 732 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: { 733 struct v4l2_subdev_frame_interval *fi = arg; 734 735 if (!client_supports_streams) 736 fi->stream = 0; 737 738 memset(fi->reserved, 0, sizeof(fi->reserved)); 739 return v4l2_subdev_call(sd, video, g_frame_interval, arg); 740 } 741 742 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: { 743 struct v4l2_subdev_frame_interval *fi = arg; 744 745 if (ro_subdev) 746 return -EPERM; 747 748 if (!client_supports_streams) 749 fi->stream = 0; 750 751 memset(fi->reserved, 0, sizeof(fi->reserved)); 752 return v4l2_subdev_call(sd, video, s_frame_interval, arg); 753 } 754 755 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { 756 struct v4l2_subdev_frame_interval_enum *fie = arg; 757 758 if (!client_supports_streams) 759 fie->stream = 0; 760 761 memset(fie->reserved, 0, sizeof(fie->reserved)); 762 return v4l2_subdev_call(sd, pad, enum_frame_interval, state, 763 fie); 764 } 765 766 case VIDIOC_SUBDEV_G_SELECTION: { 767 struct v4l2_subdev_selection *sel = arg; 768 769 if (!client_supports_streams) 770 sel->stream = 0; 771 772 memset(sel->reserved, 0, sizeof(sel->reserved)); 773 return v4l2_subdev_call( 774 sd, pad, get_selection, state, sel); 775 } 776 777 case VIDIOC_SUBDEV_S_SELECTION: { 778 struct v4l2_subdev_selection *sel = arg; 779 780 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 781 return -EPERM; 782 783 if (!client_supports_streams) 784 sel->stream = 0; 785 786 memset(sel->reserved, 0, sizeof(sel->reserved)); 787 return v4l2_subdev_call( 788 sd, pad, set_selection, state, sel); 789 } 790 791 case VIDIOC_G_EDID: { 792 struct v4l2_subdev_edid *edid = arg; 793 794 return v4l2_subdev_call(sd, pad, get_edid, edid); 795 } 796 797 case VIDIOC_S_EDID: { 798 struct v4l2_subdev_edid *edid = arg; 799 800 return v4l2_subdev_call(sd, pad, set_edid, edid); 801 } 802 803 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: { 804 struct v4l2_dv_timings_cap *cap = arg; 805 806 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap); 807 } 808 809 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: { 810 struct v4l2_enum_dv_timings *dvt = arg; 811 812 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt); 813 } 814 815 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS: 816 return v4l2_subdev_call(sd, video, query_dv_timings, arg); 817 818 case VIDIOC_SUBDEV_G_DV_TIMINGS: 819 return v4l2_subdev_call(sd, video, g_dv_timings, arg); 820 821 case VIDIOC_SUBDEV_S_DV_TIMINGS: 822 if (ro_subdev) 823 return -EPERM; 824 825 return v4l2_subdev_call(sd, video, s_dv_timings, arg); 826 827 case VIDIOC_SUBDEV_G_STD: 828 return v4l2_subdev_call(sd, video, g_std, arg); 829 830 case VIDIOC_SUBDEV_S_STD: { 831 v4l2_std_id *std = arg; 832 833 if (ro_subdev) 834 return -EPERM; 835 836 return v4l2_subdev_call(sd, video, s_std, *std); 837 } 838 839 case VIDIOC_SUBDEV_ENUMSTD: { 840 struct v4l2_standard *p = arg; 841 v4l2_std_id id; 842 843 if (v4l2_subdev_call(sd, video, g_tvnorms, &id)) 844 return -EINVAL; 845 846 return v4l_video_std_enumstd(p, id); 847 } 848 849 case VIDIOC_SUBDEV_QUERYSTD: 850 return v4l2_subdev_call(sd, video, querystd, arg); 851 852 case VIDIOC_SUBDEV_G_ROUTING: { 853 struct v4l2_subdev_routing *routing = arg; 854 struct v4l2_subdev_krouting *krouting; 855 856 if (!v4l2_subdev_enable_streams_api) 857 return -ENOIOCTLCMD; 858 859 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) 860 return -ENOIOCTLCMD; 861 862 memset(routing->reserved, 0, sizeof(routing->reserved)); 863 864 krouting = &state->routing; 865 866 if (routing->num_routes < krouting->num_routes) { 867 routing->num_routes = krouting->num_routes; 868 return -ENOSPC; 869 } 870 871 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes, 872 krouting->routes, 873 krouting->num_routes * sizeof(*krouting->routes)); 874 routing->num_routes = krouting->num_routes; 875 876 return 0; 877 } 878 879 case VIDIOC_SUBDEV_S_ROUTING: { 880 struct v4l2_subdev_routing *routing = arg; 881 struct v4l2_subdev_route *routes = 882 (struct v4l2_subdev_route *)(uintptr_t)routing->routes; 883 struct v4l2_subdev_krouting krouting = {}; 884 unsigned int i; 885 886 if (!v4l2_subdev_enable_streams_api) 887 return -ENOIOCTLCMD; 888 889 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) 890 return -ENOIOCTLCMD; 891 892 if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 893 return -EPERM; 894 895 memset(routing->reserved, 0, sizeof(routing->reserved)); 896 897 for (i = 0; i < routing->num_routes; ++i) { 898 const struct v4l2_subdev_route *route = &routes[i]; 899 const struct media_pad *pads = sd->entity.pads; 900 901 if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID || 902 route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID) 903 return -EINVAL; 904 905 if (route->sink_pad >= sd->entity.num_pads) 906 return -EINVAL; 907 908 if (!(pads[route->sink_pad].flags & 909 MEDIA_PAD_FL_SINK)) 910 return -EINVAL; 911 912 if (route->source_pad >= sd->entity.num_pads) 913 return -EINVAL; 914 915 if (!(pads[route->source_pad].flags & 916 MEDIA_PAD_FL_SOURCE)) 917 return -EINVAL; 918 } 919 920 krouting.num_routes = routing->num_routes; 921 krouting.routes = routes; 922 923 return v4l2_subdev_call(sd, pad, set_routing, state, 924 routing->which, &krouting); 925 } 926 927 case VIDIOC_SUBDEV_G_CLIENT_CAP: { 928 struct v4l2_subdev_client_capability *client_cap = arg; 929 930 client_cap->capabilities = subdev_fh->client_caps; 931 932 return 0; 933 } 934 935 case VIDIOC_SUBDEV_S_CLIENT_CAP: { 936 struct v4l2_subdev_client_capability *client_cap = arg; 937 938 /* 939 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not 940 * enabled. Remove this when streams API is no longer 941 * experimental. 942 */ 943 if (!v4l2_subdev_enable_streams_api) 944 client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS; 945 946 /* Filter out unsupported capabilities */ 947 client_cap->capabilities &= V4L2_SUBDEV_CLIENT_CAP_STREAMS; 948 949 subdev_fh->client_caps = client_cap->capabilities; 950 951 return 0; 952 } 953 954 default: 955 return v4l2_subdev_call(sd, core, ioctl, cmd, arg); 956 } 957 958 return 0; 959 } 960 961 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg) 962 { 963 struct video_device *vdev = video_devdata(file); 964 struct mutex *lock = vdev->lock; 965 long ret = -ENODEV; 966 967 if (lock && mutex_lock_interruptible(lock)) 968 return -ERESTARTSYS; 969 970 if (video_is_registered(vdev)) { 971 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 972 struct v4l2_fh *vfh = file->private_data; 973 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 974 struct v4l2_subdev_state *state; 975 976 state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg); 977 978 if (state) 979 v4l2_subdev_lock_state(state); 980 981 ret = subdev_do_ioctl(file, cmd, arg, state); 982 983 if (state) 984 v4l2_subdev_unlock_state(state); 985 } 986 987 if (lock) 988 mutex_unlock(lock); 989 return ret; 990 } 991 992 static long subdev_ioctl(struct file *file, unsigned int cmd, 993 unsigned long arg) 994 { 995 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock); 996 } 997 998 #ifdef CONFIG_COMPAT 999 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 1000 unsigned long arg) 1001 { 1002 struct video_device *vdev = video_devdata(file); 1003 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 1004 1005 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg); 1006 } 1007 #endif 1008 1009 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 1010 static long subdev_ioctl(struct file *file, unsigned int cmd, 1011 unsigned long arg) 1012 { 1013 return -ENODEV; 1014 } 1015 1016 #ifdef CONFIG_COMPAT 1017 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 1018 unsigned long arg) 1019 { 1020 return -ENODEV; 1021 } 1022 #endif 1023 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 1024 1025 static __poll_t subdev_poll(struct file *file, poll_table *wait) 1026 { 1027 struct video_device *vdev = video_devdata(file); 1028 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 1029 struct v4l2_fh *fh = file->private_data; 1030 1031 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 1032 return EPOLLERR; 1033 1034 poll_wait(file, &fh->wait, wait); 1035 1036 if (v4l2_event_pending(fh)) 1037 return EPOLLPRI; 1038 1039 return 0; 1040 } 1041 1042 const struct v4l2_file_operations v4l2_subdev_fops = { 1043 .owner = THIS_MODULE, 1044 .open = subdev_open, 1045 .unlocked_ioctl = subdev_ioctl, 1046 #ifdef CONFIG_COMPAT 1047 .compat_ioctl32 = subdev_compat_ioctl32, 1048 #endif 1049 .release = subdev_close, 1050 .poll = subdev_poll, 1051 }; 1052 1053 #ifdef CONFIG_MEDIA_CONTROLLER 1054 1055 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity, 1056 struct fwnode_endpoint *endpoint) 1057 { 1058 struct fwnode_handle *fwnode; 1059 struct v4l2_subdev *sd; 1060 1061 if (!is_media_entity_v4l2_subdev(entity)) 1062 return -EINVAL; 1063 1064 sd = media_entity_to_v4l2_subdev(entity); 1065 1066 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode); 1067 fwnode_handle_put(fwnode); 1068 1069 if (device_match_fwnode(sd->dev, fwnode)) 1070 return endpoint->port; 1071 1072 return -ENXIO; 1073 } 1074 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1); 1075 1076 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd, 1077 struct media_link *link, 1078 struct v4l2_subdev_format *source_fmt, 1079 struct v4l2_subdev_format *sink_fmt) 1080 { 1081 bool pass = true; 1082 1083 /* The width, height and code must match. */ 1084 if (source_fmt->format.width != sink_fmt->format.width) { 1085 dev_dbg(sd->entity.graph_obj.mdev->dev, 1086 "%s: width does not match (source %u, sink %u)\n", 1087 __func__, 1088 source_fmt->format.width, sink_fmt->format.width); 1089 pass = false; 1090 } 1091 1092 if (source_fmt->format.height != sink_fmt->format.height) { 1093 dev_dbg(sd->entity.graph_obj.mdev->dev, 1094 "%s: height does not match (source %u, sink %u)\n", 1095 __func__, 1096 source_fmt->format.height, sink_fmt->format.height); 1097 pass = false; 1098 } 1099 1100 if (source_fmt->format.code != sink_fmt->format.code) { 1101 dev_dbg(sd->entity.graph_obj.mdev->dev, 1102 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n", 1103 __func__, 1104 source_fmt->format.code, sink_fmt->format.code); 1105 pass = false; 1106 } 1107 1108 /* The field order must match, or the sink field order must be NONE 1109 * to support interlaced hardware connected to bridges that support 1110 * progressive formats only. 1111 */ 1112 if (source_fmt->format.field != sink_fmt->format.field && 1113 sink_fmt->format.field != V4L2_FIELD_NONE) { 1114 dev_dbg(sd->entity.graph_obj.mdev->dev, 1115 "%s: field does not match (source %u, sink %u)\n", 1116 __func__, 1117 source_fmt->format.field, sink_fmt->format.field); 1118 pass = false; 1119 } 1120 1121 if (pass) 1122 return 0; 1123 1124 dev_dbg(sd->entity.graph_obj.mdev->dev, 1125 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__, 1126 link->source->entity->name, link->source->index, 1127 link->sink->entity->name, link->sink->index); 1128 1129 return -EPIPE; 1130 } 1131 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default); 1132 1133 static int 1134 v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream, 1135 struct v4l2_subdev_format *fmt, 1136 bool states_locked) 1137 { 1138 struct v4l2_subdev_state *state; 1139 struct v4l2_subdev *sd; 1140 int ret; 1141 1142 if (!is_media_entity_v4l2_subdev(pad->entity)) { 1143 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L, 1144 "Driver bug! Wrong media entity type 0x%08x, entity %s\n", 1145 pad->entity->function, pad->entity->name); 1146 1147 return -EINVAL; 1148 } 1149 1150 sd = media_entity_to_v4l2_subdev(pad->entity); 1151 1152 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE; 1153 fmt->pad = pad->index; 1154 fmt->stream = stream; 1155 1156 if (states_locked) 1157 state = v4l2_subdev_get_locked_active_state(sd); 1158 else 1159 state = v4l2_subdev_lock_and_get_active_state(sd); 1160 1161 ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt); 1162 1163 if (!states_locked && state) 1164 v4l2_subdev_unlock_state(state); 1165 1166 return ret; 1167 } 1168 1169 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 1170 1171 static void __v4l2_link_validate_get_streams(struct media_pad *pad, 1172 u64 *streams_mask, 1173 bool states_locked) 1174 { 1175 struct v4l2_subdev_route *route; 1176 struct v4l2_subdev_state *state; 1177 struct v4l2_subdev *subdev; 1178 1179 subdev = media_entity_to_v4l2_subdev(pad->entity); 1180 1181 *streams_mask = 0; 1182 1183 if (states_locked) 1184 state = v4l2_subdev_get_locked_active_state(subdev); 1185 else 1186 state = v4l2_subdev_lock_and_get_active_state(subdev); 1187 1188 if (WARN_ON(!state)) 1189 return; 1190 1191 for_each_active_route(&state->routing, route) { 1192 u32 route_pad; 1193 u32 route_stream; 1194 1195 if (pad->flags & MEDIA_PAD_FL_SOURCE) { 1196 route_pad = route->source_pad; 1197 route_stream = route->source_stream; 1198 } else { 1199 route_pad = route->sink_pad; 1200 route_stream = route->sink_stream; 1201 } 1202 1203 if (route_pad != pad->index) 1204 continue; 1205 1206 *streams_mask |= BIT_ULL(route_stream); 1207 } 1208 1209 if (!states_locked) 1210 v4l2_subdev_unlock_state(state); 1211 } 1212 1213 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 1214 1215 static void v4l2_link_validate_get_streams(struct media_pad *pad, 1216 u64 *streams_mask, 1217 bool states_locked) 1218 { 1219 struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity); 1220 1221 if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) { 1222 /* Non-streams subdevs have an implicit stream 0 */ 1223 *streams_mask = BIT_ULL(0); 1224 return; 1225 } 1226 1227 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 1228 __v4l2_link_validate_get_streams(pad, streams_mask, states_locked); 1229 #else 1230 /* This shouldn't happen */ 1231 *streams_mask = 0; 1232 #endif 1233 } 1234 1235 static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked) 1236 { 1237 struct v4l2_subdev *sink_subdev = 1238 media_entity_to_v4l2_subdev(link->sink->entity); 1239 struct device *dev = sink_subdev->entity.graph_obj.mdev->dev; 1240 u64 source_streams_mask; 1241 u64 sink_streams_mask; 1242 u64 dangling_sink_streams; 1243 u32 stream; 1244 int ret; 1245 1246 dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n", 1247 link->source->entity->name, link->source->index, 1248 link->sink->entity->name, link->sink->index); 1249 1250 v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked); 1251 v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked); 1252 1253 /* 1254 * It is ok to have more source streams than sink streams as extra 1255 * source streams can just be ignored by the receiver, but having extra 1256 * sink streams is an error as streams must have a source. 1257 */ 1258 dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) & 1259 sink_streams_mask; 1260 if (dangling_sink_streams) { 1261 dev_err(dev, "Dangling sink streams: mask %#llx\n", 1262 dangling_sink_streams); 1263 return -EINVAL; 1264 } 1265 1266 /* Validate source and sink stream formats */ 1267 1268 for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) { 1269 struct v4l2_subdev_format sink_fmt, source_fmt; 1270 1271 if (!(sink_streams_mask & BIT_ULL(stream))) 1272 continue; 1273 1274 dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n", 1275 link->source->entity->name, link->source->index, stream, 1276 link->sink->entity->name, link->sink->index, stream); 1277 1278 ret = v4l2_subdev_link_validate_get_format(link->source, stream, 1279 &source_fmt, states_locked); 1280 if (ret < 0) { 1281 dev_dbg(dev, 1282 "Failed to get format for \"%s\":%u:%u (but that's ok)\n", 1283 link->source->entity->name, link->source->index, 1284 stream); 1285 continue; 1286 } 1287 1288 ret = v4l2_subdev_link_validate_get_format(link->sink, stream, 1289 &sink_fmt, states_locked); 1290 if (ret < 0) { 1291 dev_dbg(dev, 1292 "Failed to get format for \"%s\":%u:%u (but that's ok)\n", 1293 link->sink->entity->name, link->sink->index, 1294 stream); 1295 continue; 1296 } 1297 1298 /* TODO: add stream number to link_validate() */ 1299 ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link, 1300 &source_fmt, &sink_fmt); 1301 if (!ret) 1302 continue; 1303 1304 if (ret != -ENOIOCTLCMD) 1305 return ret; 1306 1307 ret = v4l2_subdev_link_validate_default(sink_subdev, link, 1308 &source_fmt, &sink_fmt); 1309 1310 if (ret) 1311 return ret; 1312 } 1313 1314 return 0; 1315 } 1316 1317 int v4l2_subdev_link_validate(struct media_link *link) 1318 { 1319 struct v4l2_subdev *source_sd, *sink_sd; 1320 struct v4l2_subdev_state *source_state, *sink_state; 1321 bool states_locked; 1322 int ret; 1323 1324 if (!is_media_entity_v4l2_subdev(link->sink->entity) || 1325 !is_media_entity_v4l2_subdev(link->source->entity)) { 1326 pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n", 1327 !is_media_entity_v4l2_subdev(link->sink->entity) ? 1328 "sink" : "source", 1329 link->source->entity->name, link->source->index, 1330 link->sink->entity->name, link->sink->index); 1331 return 0; 1332 } 1333 1334 sink_sd = media_entity_to_v4l2_subdev(link->sink->entity); 1335 source_sd = media_entity_to_v4l2_subdev(link->source->entity); 1336 1337 sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd); 1338 source_state = v4l2_subdev_get_unlocked_active_state(source_sd); 1339 1340 states_locked = sink_state && source_state; 1341 1342 if (states_locked) { 1343 v4l2_subdev_lock_state(sink_state); 1344 v4l2_subdev_lock_state(source_state); 1345 } 1346 1347 ret = v4l2_subdev_link_validate_locked(link, states_locked); 1348 1349 if (states_locked) { 1350 v4l2_subdev_unlock_state(sink_state); 1351 v4l2_subdev_unlock_state(source_state); 1352 } 1353 1354 return ret; 1355 } 1356 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate); 1357 1358 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity, 1359 unsigned int pad0, unsigned int pad1) 1360 { 1361 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); 1362 struct v4l2_subdev_krouting *routing; 1363 struct v4l2_subdev_state *state; 1364 unsigned int i; 1365 1366 state = v4l2_subdev_lock_and_get_active_state(sd); 1367 1368 routing = &state->routing; 1369 1370 for (i = 0; i < routing->num_routes; ++i) { 1371 struct v4l2_subdev_route *route = &routing->routes[i]; 1372 1373 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE)) 1374 continue; 1375 1376 if ((route->sink_pad == pad0 && route->source_pad == pad1) || 1377 (route->source_pad == pad0 && route->sink_pad == pad1)) { 1378 v4l2_subdev_unlock_state(state); 1379 return true; 1380 } 1381 } 1382 1383 v4l2_subdev_unlock_state(state); 1384 1385 return false; 1386 } 1387 EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep); 1388 1389 struct v4l2_subdev_state * 1390 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name, 1391 struct lock_class_key *lock_key) 1392 { 1393 struct v4l2_subdev_state *state; 1394 int ret; 1395 1396 state = kzalloc(sizeof(*state), GFP_KERNEL); 1397 if (!state) 1398 return ERR_PTR(-ENOMEM); 1399 1400 __mutex_init(&state->_lock, lock_name, lock_key); 1401 if (sd->state_lock) 1402 state->lock = sd->state_lock; 1403 else 1404 state->lock = &state->_lock; 1405 1406 /* Drivers that support streams do not need the legacy pad config */ 1407 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) { 1408 state->pads = kvcalloc(sd->entity.num_pads, 1409 sizeof(*state->pads), GFP_KERNEL); 1410 if (!state->pads) { 1411 ret = -ENOMEM; 1412 goto err; 1413 } 1414 } 1415 1416 /* 1417 * There can be no race at this point, but we lock the state anyway to 1418 * satisfy lockdep checks. 1419 */ 1420 v4l2_subdev_lock_state(state); 1421 ret = v4l2_subdev_call(sd, pad, init_cfg, state); 1422 v4l2_subdev_unlock_state(state); 1423 1424 if (ret < 0 && ret != -ENOIOCTLCMD) 1425 goto err; 1426 1427 return state; 1428 1429 err: 1430 if (state && state->pads) 1431 kvfree(state->pads); 1432 1433 kfree(state); 1434 1435 return ERR_PTR(ret); 1436 } 1437 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc); 1438 1439 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state) 1440 { 1441 if (!state) 1442 return; 1443 1444 mutex_destroy(&state->_lock); 1445 1446 kfree(state->routing.routes); 1447 kvfree(state->stream_configs.configs); 1448 kvfree(state->pads); 1449 kfree(state); 1450 } 1451 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free); 1452 1453 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name, 1454 struct lock_class_key *key) 1455 { 1456 struct v4l2_subdev_state *state; 1457 1458 state = __v4l2_subdev_state_alloc(sd, name, key); 1459 if (IS_ERR(state)) 1460 return PTR_ERR(state); 1461 1462 sd->active_state = state; 1463 1464 return 0; 1465 } 1466 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize); 1467 1468 void v4l2_subdev_cleanup(struct v4l2_subdev *sd) 1469 { 1470 struct v4l2_async_subdev_endpoint *ase, *ase_tmp; 1471 1472 __v4l2_subdev_state_free(sd->active_state); 1473 sd->active_state = NULL; 1474 1475 if (list_empty(&sd->async_subdev_endpoint_list)) 1476 return; 1477 1478 list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list, 1479 async_subdev_endpoint_entry) { 1480 list_del(&ase->async_subdev_endpoint_entry); 1481 1482 kfree(ase); 1483 } 1484 } 1485 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup); 1486 1487 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 1488 1489 static int 1490 v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs, 1491 const struct v4l2_subdev_krouting *routing) 1492 { 1493 struct v4l2_subdev_stream_configs new_configs = { 0 }; 1494 struct v4l2_subdev_route *route; 1495 u32 idx; 1496 1497 /* Count number of formats needed */ 1498 for_each_active_route(routing, route) { 1499 /* 1500 * Each route needs a format on both ends of the route. 1501 */ 1502 new_configs.num_configs += 2; 1503 } 1504 1505 if (new_configs.num_configs) { 1506 new_configs.configs = kvcalloc(new_configs.num_configs, 1507 sizeof(*new_configs.configs), 1508 GFP_KERNEL); 1509 1510 if (!new_configs.configs) 1511 return -ENOMEM; 1512 } 1513 1514 /* 1515 * Fill in the 'pad' and stream' value for each item in the array from 1516 * the routing table 1517 */ 1518 idx = 0; 1519 1520 for_each_active_route(routing, route) { 1521 new_configs.configs[idx].pad = route->sink_pad; 1522 new_configs.configs[idx].stream = route->sink_stream; 1523 1524 idx++; 1525 1526 new_configs.configs[idx].pad = route->source_pad; 1527 new_configs.configs[idx].stream = route->source_stream; 1528 1529 idx++; 1530 } 1531 1532 kvfree(stream_configs->configs); 1533 *stream_configs = new_configs; 1534 1535 return 0; 1536 } 1537 1538 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state, 1539 struct v4l2_subdev_format *format) 1540 { 1541 struct v4l2_mbus_framefmt *fmt; 1542 1543 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) 1544 fmt = v4l2_subdev_state_get_stream_format(state, format->pad, 1545 format->stream); 1546 else if (format->pad < sd->entity.num_pads && format->stream == 0) 1547 fmt = v4l2_subdev_get_pad_format(sd, state, format->pad); 1548 else 1549 fmt = NULL; 1550 1551 if (!fmt) 1552 return -EINVAL; 1553 1554 format->format = *fmt; 1555 1556 return 0; 1557 } 1558 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt); 1559 1560 int v4l2_subdev_set_routing(struct v4l2_subdev *sd, 1561 struct v4l2_subdev_state *state, 1562 const struct v4l2_subdev_krouting *routing) 1563 { 1564 struct v4l2_subdev_krouting *dst = &state->routing; 1565 const struct v4l2_subdev_krouting *src = routing; 1566 struct v4l2_subdev_krouting new_routing = { 0 }; 1567 size_t bytes; 1568 int r; 1569 1570 if (unlikely(check_mul_overflow((size_t)src->num_routes, 1571 sizeof(*src->routes), &bytes))) 1572 return -EOVERFLOW; 1573 1574 lockdep_assert_held(state->lock); 1575 1576 if (src->num_routes > 0) { 1577 new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL); 1578 if (!new_routing.routes) 1579 return -ENOMEM; 1580 } 1581 1582 new_routing.num_routes = src->num_routes; 1583 1584 r = v4l2_subdev_init_stream_configs(&state->stream_configs, 1585 &new_routing); 1586 if (r) { 1587 kfree(new_routing.routes); 1588 return r; 1589 } 1590 1591 kfree(dst->routes); 1592 *dst = new_routing; 1593 1594 return 0; 1595 } 1596 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing); 1597 1598 struct v4l2_subdev_route * 1599 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing, 1600 struct v4l2_subdev_route *route) 1601 { 1602 if (route) 1603 ++route; 1604 else 1605 route = &routing->routes[0]; 1606 1607 for (; route < routing->routes + routing->num_routes; ++route) { 1608 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE)) 1609 continue; 1610 1611 return route; 1612 } 1613 1614 return NULL; 1615 } 1616 EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route); 1617 1618 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd, 1619 struct v4l2_subdev_state *state, 1620 const struct v4l2_subdev_krouting *routing, 1621 const struct v4l2_mbus_framefmt *fmt) 1622 { 1623 struct v4l2_subdev_stream_configs *stream_configs; 1624 unsigned int i; 1625 int ret; 1626 1627 ret = v4l2_subdev_set_routing(sd, state, routing); 1628 if (ret) 1629 return ret; 1630 1631 stream_configs = &state->stream_configs; 1632 1633 for (i = 0; i < stream_configs->num_configs; ++i) 1634 stream_configs->configs[i].fmt = *fmt; 1635 1636 return 0; 1637 } 1638 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt); 1639 1640 struct v4l2_mbus_framefmt * 1641 v4l2_subdev_state_get_stream_format(struct v4l2_subdev_state *state, 1642 unsigned int pad, u32 stream) 1643 { 1644 struct v4l2_subdev_stream_configs *stream_configs; 1645 unsigned int i; 1646 1647 lockdep_assert_held(state->lock); 1648 1649 stream_configs = &state->stream_configs; 1650 1651 for (i = 0; i < stream_configs->num_configs; ++i) { 1652 if (stream_configs->configs[i].pad == pad && 1653 stream_configs->configs[i].stream == stream) 1654 return &stream_configs->configs[i].fmt; 1655 } 1656 1657 return NULL; 1658 } 1659 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_format); 1660 1661 struct v4l2_rect * 1662 v4l2_subdev_state_get_stream_crop(struct v4l2_subdev_state *state, 1663 unsigned int pad, u32 stream) 1664 { 1665 struct v4l2_subdev_stream_configs *stream_configs; 1666 unsigned int i; 1667 1668 lockdep_assert_held(state->lock); 1669 1670 stream_configs = &state->stream_configs; 1671 1672 for (i = 0; i < stream_configs->num_configs; ++i) { 1673 if (stream_configs->configs[i].pad == pad && 1674 stream_configs->configs[i].stream == stream) 1675 return &stream_configs->configs[i].crop; 1676 } 1677 1678 return NULL; 1679 } 1680 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_crop); 1681 1682 struct v4l2_rect * 1683 v4l2_subdev_state_get_stream_compose(struct v4l2_subdev_state *state, 1684 unsigned int pad, u32 stream) 1685 { 1686 struct v4l2_subdev_stream_configs *stream_configs; 1687 unsigned int i; 1688 1689 lockdep_assert_held(state->lock); 1690 1691 stream_configs = &state->stream_configs; 1692 1693 for (i = 0; i < stream_configs->num_configs; ++i) { 1694 if (stream_configs->configs[i].pad == pad && 1695 stream_configs->configs[i].stream == stream) 1696 return &stream_configs->configs[i].compose; 1697 } 1698 1699 return NULL; 1700 } 1701 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_compose); 1702 1703 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing, 1704 u32 pad, u32 stream, u32 *other_pad, 1705 u32 *other_stream) 1706 { 1707 unsigned int i; 1708 1709 for (i = 0; i < routing->num_routes; ++i) { 1710 struct v4l2_subdev_route *route = &routing->routes[i]; 1711 1712 if (route->source_pad == pad && 1713 route->source_stream == stream) { 1714 if (other_pad) 1715 *other_pad = route->sink_pad; 1716 if (other_stream) 1717 *other_stream = route->sink_stream; 1718 return 0; 1719 } 1720 1721 if (route->sink_pad == pad && route->sink_stream == stream) { 1722 if (other_pad) 1723 *other_pad = route->source_pad; 1724 if (other_stream) 1725 *other_stream = route->source_stream; 1726 return 0; 1727 } 1728 } 1729 1730 return -EINVAL; 1731 } 1732 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end); 1733 1734 struct v4l2_mbus_framefmt * 1735 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state, 1736 u32 pad, u32 stream) 1737 { 1738 u32 other_pad, other_stream; 1739 int ret; 1740 1741 ret = v4l2_subdev_routing_find_opposite_end(&state->routing, 1742 pad, stream, 1743 &other_pad, &other_stream); 1744 if (ret) 1745 return NULL; 1746 1747 return v4l2_subdev_state_get_stream_format(state, other_pad, 1748 other_stream); 1749 } 1750 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format); 1751 1752 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state, 1753 u32 pad0, u32 pad1, u64 *streams) 1754 { 1755 const struct v4l2_subdev_krouting *routing = &state->routing; 1756 struct v4l2_subdev_route *route; 1757 u64 streams0 = 0; 1758 u64 streams1 = 0; 1759 1760 for_each_active_route(routing, route) { 1761 if (route->sink_pad == pad0 && route->source_pad == pad1 && 1762 (*streams & BIT_ULL(route->sink_stream))) { 1763 streams0 |= BIT_ULL(route->sink_stream); 1764 streams1 |= BIT_ULL(route->source_stream); 1765 } 1766 if (route->source_pad == pad0 && route->sink_pad == pad1 && 1767 (*streams & BIT_ULL(route->source_stream))) { 1768 streams0 |= BIT_ULL(route->source_stream); 1769 streams1 |= BIT_ULL(route->sink_stream); 1770 } 1771 } 1772 1773 *streams = streams0; 1774 return streams1; 1775 } 1776 EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams); 1777 1778 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd, 1779 const struct v4l2_subdev_krouting *routing, 1780 enum v4l2_subdev_routing_restriction disallow) 1781 { 1782 u32 *remote_pads = NULL; 1783 unsigned int i, j; 1784 int ret = -EINVAL; 1785 1786 if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX | 1787 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) { 1788 remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads), 1789 GFP_KERNEL); 1790 if (!remote_pads) 1791 return -ENOMEM; 1792 1793 for (i = 0; i < sd->entity.num_pads; ++i) 1794 remote_pads[i] = U32_MAX; 1795 } 1796 1797 for (i = 0; i < routing->num_routes; ++i) { 1798 const struct v4l2_subdev_route *route = &routing->routes[i]; 1799 1800 /* Validate the sink and source pad numbers. */ 1801 if (route->sink_pad >= sd->entity.num_pads || 1802 !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) { 1803 dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n", 1804 i, route->sink_pad); 1805 goto out; 1806 } 1807 1808 if (route->source_pad >= sd->entity.num_pads || 1809 !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) { 1810 dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n", 1811 i, route->source_pad); 1812 goto out; 1813 } 1814 1815 /* 1816 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a 1817 * sink pad must be routed to a single source pad. 1818 */ 1819 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) { 1820 if (remote_pads[route->sink_pad] != U32_MAX && 1821 remote_pads[route->sink_pad] != route->source_pad) { 1822 dev_dbg(sd->dev, 1823 "route %u attempts to mix %s streams\n", 1824 i, "sink"); 1825 goto out; 1826 } 1827 } 1828 1829 /* 1830 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a 1831 * source pad must originate from a single sink pad. 1832 */ 1833 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) { 1834 if (remote_pads[route->source_pad] != U32_MAX && 1835 remote_pads[route->source_pad] != route->sink_pad) { 1836 dev_dbg(sd->dev, 1837 "route %u attempts to mix %s streams\n", 1838 i, "source"); 1839 goto out; 1840 } 1841 } 1842 1843 /* 1844 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink 1845 * side can not do stream multiplexing, i.e. there can be only 1846 * a single stream in a sink pad. 1847 */ 1848 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) { 1849 if (remote_pads[route->sink_pad] != U32_MAX) { 1850 dev_dbg(sd->dev, 1851 "route %u attempts to multiplex on %s pad %u\n", 1852 i, "sink", route->sink_pad); 1853 goto out; 1854 } 1855 } 1856 1857 /* 1858 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the 1859 * source side can not do stream multiplexing, i.e. there can 1860 * be only a single stream in a source pad. 1861 */ 1862 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) { 1863 if (remote_pads[route->source_pad] != U32_MAX) { 1864 dev_dbg(sd->dev, 1865 "route %u attempts to multiplex on %s pad %u\n", 1866 i, "source", route->source_pad); 1867 goto out; 1868 } 1869 } 1870 1871 if (remote_pads) { 1872 remote_pads[route->sink_pad] = route->source_pad; 1873 remote_pads[route->source_pad] = route->sink_pad; 1874 } 1875 1876 for (j = i + 1; j < routing->num_routes; ++j) { 1877 const struct v4l2_subdev_route *r = &routing->routes[j]; 1878 1879 /* 1880 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can 1881 * originate from the same (sink) stream. 1882 */ 1883 if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) && 1884 route->sink_pad == r->sink_pad && 1885 route->sink_stream == r->sink_stream) { 1886 dev_dbg(sd->dev, 1887 "routes %u and %u originate from same sink (%u/%u)\n", 1888 i, j, route->sink_pad, 1889 route->sink_stream); 1890 goto out; 1891 } 1892 1893 /* 1894 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end 1895 * at the same (source) stream. 1896 */ 1897 if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) && 1898 route->source_pad == r->source_pad && 1899 route->source_stream == r->source_stream) { 1900 dev_dbg(sd->dev, 1901 "routes %u and %u end at same source (%u/%u)\n", 1902 i, j, route->source_pad, 1903 route->source_stream); 1904 goto out; 1905 } 1906 } 1907 } 1908 1909 ret = 0; 1910 1911 out: 1912 kfree(remote_pads); 1913 return ret; 1914 } 1915 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate); 1916 1917 static int v4l2_subdev_enable_streams_fallback(struct v4l2_subdev *sd, u32 pad, 1918 u64 streams_mask) 1919 { 1920 struct device *dev = sd->entity.graph_obj.mdev->dev; 1921 unsigned int i; 1922 int ret; 1923 1924 /* 1925 * The subdev doesn't implement pad-based stream enable, fall back 1926 * on the .s_stream() operation. This can only be done for subdevs that 1927 * have a single source pad, as sd->enabled_streams is global to the 1928 * subdev. 1929 */ 1930 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE)) 1931 return -EOPNOTSUPP; 1932 1933 for (i = 0; i < sd->entity.num_pads; ++i) { 1934 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE) 1935 return -EOPNOTSUPP; 1936 } 1937 1938 if (sd->enabled_streams & streams_mask) { 1939 dev_dbg(dev, "set of streams %#llx already enabled on %s:%u\n", 1940 streams_mask, sd->entity.name, pad); 1941 return -EALREADY; 1942 } 1943 1944 /* Start streaming when the first streams are enabled. */ 1945 if (!sd->enabled_streams) { 1946 ret = v4l2_subdev_call(sd, video, s_stream, 1); 1947 if (ret) 1948 return ret; 1949 } 1950 1951 sd->enabled_streams |= streams_mask; 1952 1953 return 0; 1954 } 1955 1956 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad, 1957 u64 streams_mask) 1958 { 1959 struct device *dev = sd->entity.graph_obj.mdev->dev; 1960 struct v4l2_subdev_state *state; 1961 u64 found_streams = 0; 1962 unsigned int i; 1963 int ret; 1964 1965 /* A few basic sanity checks first. */ 1966 if (pad >= sd->entity.num_pads) 1967 return -EINVAL; 1968 1969 if (!streams_mask) 1970 return 0; 1971 1972 /* Fallback on .s_stream() if .enable_streams() isn't available. */ 1973 if (!sd->ops->pad || !sd->ops->pad->enable_streams) 1974 return v4l2_subdev_enable_streams_fallback(sd, pad, 1975 streams_mask); 1976 1977 state = v4l2_subdev_lock_and_get_active_state(sd); 1978 1979 /* 1980 * Verify that the requested streams exist and that they are not 1981 * already enabled. 1982 */ 1983 for (i = 0; i < state->stream_configs.num_configs; ++i) { 1984 struct v4l2_subdev_stream_config *cfg = 1985 &state->stream_configs.configs[i]; 1986 1987 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream))) 1988 continue; 1989 1990 found_streams |= BIT_ULL(cfg->stream); 1991 1992 if (cfg->enabled) { 1993 dev_dbg(dev, "stream %u already enabled on %s:%u\n", 1994 cfg->stream, sd->entity.name, pad); 1995 ret = -EALREADY; 1996 goto done; 1997 } 1998 } 1999 2000 if (found_streams != streams_mask) { 2001 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n", 2002 streams_mask & ~found_streams, sd->entity.name, pad); 2003 ret = -EINVAL; 2004 goto done; 2005 } 2006 2007 dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask); 2008 2009 /* Call the .enable_streams() operation. */ 2010 ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad, 2011 streams_mask); 2012 if (ret) { 2013 dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad, 2014 streams_mask, ret); 2015 goto done; 2016 } 2017 2018 /* Mark the streams as enabled. */ 2019 for (i = 0; i < state->stream_configs.num_configs; ++i) { 2020 struct v4l2_subdev_stream_config *cfg = 2021 &state->stream_configs.configs[i]; 2022 2023 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream))) 2024 cfg->enabled = true; 2025 } 2026 2027 done: 2028 v4l2_subdev_unlock_state(state); 2029 2030 return ret; 2031 } 2032 EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams); 2033 2034 static int v4l2_subdev_disable_streams_fallback(struct v4l2_subdev *sd, u32 pad, 2035 u64 streams_mask) 2036 { 2037 struct device *dev = sd->entity.graph_obj.mdev->dev; 2038 unsigned int i; 2039 int ret; 2040 2041 /* 2042 * If the subdev doesn't implement pad-based stream enable, fall back 2043 * on the .s_stream() operation. This can only be done for subdevs that 2044 * have a single source pad, as sd->enabled_streams is global to the 2045 * subdev. 2046 */ 2047 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE)) 2048 return -EOPNOTSUPP; 2049 2050 for (i = 0; i < sd->entity.num_pads; ++i) { 2051 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE) 2052 return -EOPNOTSUPP; 2053 } 2054 2055 if ((sd->enabled_streams & streams_mask) != streams_mask) { 2056 dev_dbg(dev, "set of streams %#llx already disabled on %s:%u\n", 2057 streams_mask, sd->entity.name, pad); 2058 return -EALREADY; 2059 } 2060 2061 /* Stop streaming when the last streams are disabled. */ 2062 if (!(sd->enabled_streams & ~streams_mask)) { 2063 ret = v4l2_subdev_call(sd, video, s_stream, 0); 2064 if (ret) 2065 return ret; 2066 } 2067 2068 sd->enabled_streams &= ~streams_mask; 2069 2070 return 0; 2071 } 2072 2073 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad, 2074 u64 streams_mask) 2075 { 2076 struct device *dev = sd->entity.graph_obj.mdev->dev; 2077 struct v4l2_subdev_state *state; 2078 u64 found_streams = 0; 2079 unsigned int i; 2080 int ret; 2081 2082 /* A few basic sanity checks first. */ 2083 if (pad >= sd->entity.num_pads) 2084 return -EINVAL; 2085 2086 if (!streams_mask) 2087 return 0; 2088 2089 /* Fallback on .s_stream() if .disable_streams() isn't available. */ 2090 if (!sd->ops->pad || !sd->ops->pad->disable_streams) 2091 return v4l2_subdev_disable_streams_fallback(sd, pad, 2092 streams_mask); 2093 2094 state = v4l2_subdev_lock_and_get_active_state(sd); 2095 2096 /* 2097 * Verify that the requested streams exist and that they are not 2098 * already disabled. 2099 */ 2100 for (i = 0; i < state->stream_configs.num_configs; ++i) { 2101 struct v4l2_subdev_stream_config *cfg = 2102 &state->stream_configs.configs[i]; 2103 2104 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream))) 2105 continue; 2106 2107 found_streams |= BIT_ULL(cfg->stream); 2108 2109 if (!cfg->enabled) { 2110 dev_dbg(dev, "stream %u already disabled on %s:%u\n", 2111 cfg->stream, sd->entity.name, pad); 2112 ret = -EALREADY; 2113 goto done; 2114 } 2115 } 2116 2117 if (found_streams != streams_mask) { 2118 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n", 2119 streams_mask & ~found_streams, sd->entity.name, pad); 2120 ret = -EINVAL; 2121 goto done; 2122 } 2123 2124 dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask); 2125 2126 /* Call the .disable_streams() operation. */ 2127 ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad, 2128 streams_mask); 2129 if (ret) { 2130 dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad, 2131 streams_mask, ret); 2132 goto done; 2133 } 2134 2135 /* Mark the streams as disabled. */ 2136 for (i = 0; i < state->stream_configs.num_configs; ++i) { 2137 struct v4l2_subdev_stream_config *cfg = 2138 &state->stream_configs.configs[i]; 2139 2140 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream))) 2141 cfg->enabled = false; 2142 } 2143 2144 done: 2145 v4l2_subdev_unlock_state(state); 2146 2147 return ret; 2148 } 2149 EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams); 2150 2151 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable) 2152 { 2153 struct v4l2_subdev_state *state; 2154 struct v4l2_subdev_route *route; 2155 struct media_pad *pad; 2156 u64 source_mask = 0; 2157 int pad_index = -1; 2158 2159 /* 2160 * Find the source pad. This helper is meant for subdevs that have a 2161 * single source pad, so failures shouldn't happen, but catch them 2162 * loudly nonetheless as they indicate a driver bug. 2163 */ 2164 media_entity_for_each_pad(&sd->entity, pad) { 2165 if (pad->flags & MEDIA_PAD_FL_SOURCE) { 2166 pad_index = pad->index; 2167 break; 2168 } 2169 } 2170 2171 if (WARN_ON(pad_index == -1)) 2172 return -EINVAL; 2173 2174 /* 2175 * As there's a single source pad, just collect all the source streams. 2176 */ 2177 state = v4l2_subdev_lock_and_get_active_state(sd); 2178 2179 for_each_active_route(&state->routing, route) 2180 source_mask |= BIT_ULL(route->source_stream); 2181 2182 v4l2_subdev_unlock_state(state); 2183 2184 if (enable) 2185 return v4l2_subdev_enable_streams(sd, pad_index, source_mask); 2186 else 2187 return v4l2_subdev_disable_streams(sd, pad_index, source_mask); 2188 } 2189 EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper); 2190 2191 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 2192 2193 #endif /* CONFIG_MEDIA_CONTROLLER */ 2194 2195 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) 2196 { 2197 INIT_LIST_HEAD(&sd->list); 2198 BUG_ON(!ops); 2199 sd->ops = ops; 2200 sd->v4l2_dev = NULL; 2201 sd->flags = 0; 2202 sd->name[0] = '\0'; 2203 sd->grp_id = 0; 2204 sd->dev_priv = NULL; 2205 sd->host_priv = NULL; 2206 sd->privacy_led = NULL; 2207 INIT_LIST_HEAD(&sd->async_subdev_endpoint_list); 2208 #if defined(CONFIG_MEDIA_CONTROLLER) 2209 sd->entity.name = sd->name; 2210 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV; 2211 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN; 2212 #endif 2213 } 2214 EXPORT_SYMBOL(v4l2_subdev_init); 2215 2216 void v4l2_subdev_notify_event(struct v4l2_subdev *sd, 2217 const struct v4l2_event *ev) 2218 { 2219 v4l2_event_queue(sd->devnode, ev); 2220 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev); 2221 } 2222 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event); 2223 2224 int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd) 2225 { 2226 #if IS_REACHABLE(CONFIG_LEDS_CLASS) 2227 sd->privacy_led = led_get(sd->dev, "privacy-led"); 2228 if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT) 2229 return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led), 2230 "getting privacy LED\n"); 2231 2232 if (!IS_ERR_OR_NULL(sd->privacy_led)) { 2233 mutex_lock(&sd->privacy_led->led_access); 2234 led_sysfs_disable(sd->privacy_led); 2235 led_trigger_remove(sd->privacy_led); 2236 led_set_brightness(sd->privacy_led, 0); 2237 mutex_unlock(&sd->privacy_led->led_access); 2238 } 2239 #endif 2240 return 0; 2241 } 2242 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led); 2243 2244 void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd) 2245 { 2246 #if IS_REACHABLE(CONFIG_LEDS_CLASS) 2247 if (!IS_ERR_OR_NULL(sd->privacy_led)) { 2248 mutex_lock(&sd->privacy_led->led_access); 2249 led_sysfs_enable(sd->privacy_led); 2250 mutex_unlock(&sd->privacy_led->led_access); 2251 led_put(sd->privacy_led); 2252 } 2253 #endif 2254 } 2255 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led); 2256