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