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