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