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