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/ioctl.h> 12 #include <linux/mm.h> 13 #include <linux/module.h> 14 #include <linux/slab.h> 15 #include <linux/types.h> 16 #include <linux/videodev2.h> 17 #include <linux/export.h> 18 #include <linux/version.h> 19 20 #include <media/v4l2-ctrls.h> 21 #include <media/v4l2-device.h> 22 #include <media/v4l2-ioctl.h> 23 #include <media/v4l2-fh.h> 24 #include <media/v4l2-event.h> 25 26 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 27 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) 28 { 29 struct v4l2_subdev_state *state; 30 31 state = v4l2_subdev_alloc_state(sd); 32 if (IS_ERR(state)) 33 return PTR_ERR(state); 34 35 fh->state = state; 36 37 return 0; 38 } 39 40 static void subdev_fh_free(struct v4l2_subdev_fh *fh) 41 { 42 v4l2_subdev_free_state(fh->state); 43 fh->state = NULL; 44 } 45 46 static int subdev_open(struct file *file) 47 { 48 struct video_device *vdev = video_devdata(file); 49 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 50 struct v4l2_subdev_fh *subdev_fh; 51 int ret; 52 53 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); 54 if (subdev_fh == NULL) 55 return -ENOMEM; 56 57 ret = subdev_fh_init(subdev_fh, sd); 58 if (ret) { 59 kfree(subdev_fh); 60 return ret; 61 } 62 63 v4l2_fh_init(&subdev_fh->vfh, vdev); 64 v4l2_fh_add(&subdev_fh->vfh); 65 file->private_data = &subdev_fh->vfh; 66 #if defined(CONFIG_MEDIA_CONTROLLER) 67 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) { 68 struct module *owner; 69 70 owner = sd->entity.graph_obj.mdev->dev->driver->owner; 71 if (!try_module_get(owner)) { 72 ret = -EBUSY; 73 goto err; 74 } 75 subdev_fh->owner = owner; 76 } 77 #endif 78 79 if (sd->internal_ops && sd->internal_ops->open) { 80 ret = sd->internal_ops->open(sd, subdev_fh); 81 if (ret < 0) 82 goto err; 83 } 84 85 return 0; 86 87 err: 88 module_put(subdev_fh->owner); 89 v4l2_fh_del(&subdev_fh->vfh); 90 v4l2_fh_exit(&subdev_fh->vfh); 91 subdev_fh_free(subdev_fh); 92 kfree(subdev_fh); 93 94 return ret; 95 } 96 97 static int subdev_close(struct file *file) 98 { 99 struct video_device *vdev = video_devdata(file); 100 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 101 struct v4l2_fh *vfh = file->private_data; 102 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 103 104 if (sd->internal_ops && sd->internal_ops->close) 105 sd->internal_ops->close(sd, subdev_fh); 106 module_put(subdev_fh->owner); 107 v4l2_fh_del(vfh); 108 v4l2_fh_exit(vfh); 109 subdev_fh_free(subdev_fh); 110 kfree(subdev_fh); 111 file->private_data = NULL; 112 113 return 0; 114 } 115 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 116 static int subdev_open(struct file *file) 117 { 118 return -ENODEV; 119 } 120 121 static int subdev_close(struct file *file) 122 { 123 return -ENODEV; 124 } 125 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 126 127 static inline int check_which(u32 which) 128 { 129 if (which != V4L2_SUBDEV_FORMAT_TRY && 130 which != V4L2_SUBDEV_FORMAT_ACTIVE) 131 return -EINVAL; 132 133 return 0; 134 } 135 136 static inline int check_pad(struct v4l2_subdev *sd, u32 pad) 137 { 138 #if defined(CONFIG_MEDIA_CONTROLLER) 139 if (sd->entity.num_pads) { 140 if (pad >= sd->entity.num_pads) 141 return -EINVAL; 142 return 0; 143 } 144 #endif 145 /* allow pad 0 on subdevices not registered as media entities */ 146 if (pad > 0) 147 return -EINVAL; 148 return 0; 149 } 150 151 static int check_state_pads(u32 which, struct v4l2_subdev_state *state) 152 { 153 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads)) 154 return -EINVAL; 155 156 return 0; 157 } 158 159 static inline int check_format(struct v4l2_subdev *sd, 160 struct v4l2_subdev_state *state, 161 struct v4l2_subdev_format *format) 162 { 163 if (!format) 164 return -EINVAL; 165 166 return check_which(format->which) ? : check_pad(sd, format->pad) ? : 167 check_state_pads(format->which, state); 168 } 169 170 static int call_get_fmt(struct v4l2_subdev *sd, 171 struct v4l2_subdev_state *state, 172 struct v4l2_subdev_format *format) 173 { 174 return check_format(sd, state, format) ? : 175 sd->ops->pad->get_fmt(sd, state, format); 176 } 177 178 static int call_set_fmt(struct v4l2_subdev *sd, 179 struct v4l2_subdev_state *state, 180 struct v4l2_subdev_format *format) 181 { 182 return check_format(sd, state, format) ? : 183 sd->ops->pad->set_fmt(sd, state, format); 184 } 185 186 static int call_enum_mbus_code(struct v4l2_subdev *sd, 187 struct v4l2_subdev_state *state, 188 struct v4l2_subdev_mbus_code_enum *code) 189 { 190 if (!code) 191 return -EINVAL; 192 193 return check_which(code->which) ? : check_pad(sd, code->pad) ? : 194 check_state_pads(code->which, state) ? : 195 sd->ops->pad->enum_mbus_code(sd, state, code); 196 } 197 198 static int call_enum_frame_size(struct v4l2_subdev *sd, 199 struct v4l2_subdev_state *state, 200 struct v4l2_subdev_frame_size_enum *fse) 201 { 202 if (!fse) 203 return -EINVAL; 204 205 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? : 206 check_state_pads(fse->which, state) ? : 207 sd->ops->pad->enum_frame_size(sd, state, fse); 208 } 209 210 static inline int check_frame_interval(struct v4l2_subdev *sd, 211 struct v4l2_subdev_frame_interval *fi) 212 { 213 if (!fi) 214 return -EINVAL; 215 216 return check_pad(sd, fi->pad); 217 } 218 219 static int call_g_frame_interval(struct v4l2_subdev *sd, 220 struct v4l2_subdev_frame_interval *fi) 221 { 222 return check_frame_interval(sd, fi) ? : 223 sd->ops->video->g_frame_interval(sd, fi); 224 } 225 226 static int call_s_frame_interval(struct v4l2_subdev *sd, 227 struct v4l2_subdev_frame_interval *fi) 228 { 229 return check_frame_interval(sd, fi) ? : 230 sd->ops->video->s_frame_interval(sd, fi); 231 } 232 233 static int call_enum_frame_interval(struct v4l2_subdev *sd, 234 struct v4l2_subdev_state *state, 235 struct v4l2_subdev_frame_interval_enum *fie) 236 { 237 if (!fie) 238 return -EINVAL; 239 240 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? : 241 check_state_pads(fie->which, state) ? : 242 sd->ops->pad->enum_frame_interval(sd, state, fie); 243 } 244 245 static inline int check_selection(struct v4l2_subdev *sd, 246 struct v4l2_subdev_state *state, 247 struct v4l2_subdev_selection *sel) 248 { 249 if (!sel) 250 return -EINVAL; 251 252 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? : 253 check_state_pads(sel->which, state); 254 } 255 256 static int call_get_selection(struct v4l2_subdev *sd, 257 struct v4l2_subdev_state *state, 258 struct v4l2_subdev_selection *sel) 259 { 260 return check_selection(sd, state, sel) ? : 261 sd->ops->pad->get_selection(sd, state, sel); 262 } 263 264 static int call_set_selection(struct v4l2_subdev *sd, 265 struct v4l2_subdev_state *state, 266 struct v4l2_subdev_selection *sel) 267 { 268 return check_selection(sd, state, sel) ? : 269 sd->ops->pad->set_selection(sd, state, sel); 270 } 271 272 static inline int check_edid(struct v4l2_subdev *sd, 273 struct v4l2_subdev_edid *edid) 274 { 275 if (!edid) 276 return -EINVAL; 277 278 if (edid->blocks && edid->edid == NULL) 279 return -EINVAL; 280 281 return check_pad(sd, edid->pad); 282 } 283 284 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 285 { 286 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid); 287 } 288 289 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 290 { 291 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid); 292 } 293 294 static int call_dv_timings_cap(struct v4l2_subdev *sd, 295 struct v4l2_dv_timings_cap *cap) 296 { 297 if (!cap) 298 return -EINVAL; 299 300 return check_pad(sd, cap->pad) ? : 301 sd->ops->pad->dv_timings_cap(sd, cap); 302 } 303 304 static int call_enum_dv_timings(struct v4l2_subdev *sd, 305 struct v4l2_enum_dv_timings *dvt) 306 { 307 if (!dvt) 308 return -EINVAL; 309 310 return check_pad(sd, dvt->pad) ? : 311 sd->ops->pad->enum_dv_timings(sd, dvt); 312 } 313 314 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad, 315 struct v4l2_mbus_config *config) 316 { 317 return check_pad(sd, pad) ? : 318 sd->ops->pad->get_mbus_config(sd, pad, config); 319 } 320 321 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = { 322 .get_fmt = call_get_fmt, 323 .set_fmt = call_set_fmt, 324 .enum_mbus_code = call_enum_mbus_code, 325 .enum_frame_size = call_enum_frame_size, 326 .enum_frame_interval = call_enum_frame_interval, 327 .get_selection = call_get_selection, 328 .set_selection = call_set_selection, 329 .get_edid = call_get_edid, 330 .set_edid = call_set_edid, 331 .dv_timings_cap = call_dv_timings_cap, 332 .enum_dv_timings = call_enum_dv_timings, 333 .get_mbus_config = call_get_mbus_config, 334 }; 335 336 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = { 337 .g_frame_interval = call_g_frame_interval, 338 .s_frame_interval = call_s_frame_interval, 339 }; 340 341 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = { 342 .pad = &v4l2_subdev_call_pad_wrappers, 343 .video = &v4l2_subdev_call_video_wrappers, 344 }; 345 EXPORT_SYMBOL(v4l2_subdev_call_wrappers); 346 347 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 348 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg) 349 { 350 struct video_device *vdev = video_devdata(file); 351 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 352 struct v4l2_fh *vfh = file->private_data; 353 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 354 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags); 355 int rval; 356 357 switch (cmd) { 358 case VIDIOC_SUBDEV_QUERYCAP: { 359 struct v4l2_subdev_capability *cap = arg; 360 361 memset(cap->reserved, 0, sizeof(cap->reserved)); 362 cap->version = LINUX_VERSION_CODE; 363 cap->capabilities = ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0; 364 365 return 0; 366 } 367 368 case VIDIOC_QUERYCTRL: 369 /* 370 * TODO: this really should be folded into v4l2_queryctrl (this 371 * currently returns -EINVAL for NULL control handlers). 372 * However, v4l2_queryctrl() is still called directly by 373 * drivers as well and until that has been addressed I believe 374 * it is safer to do the check here. The same is true for the 375 * other control ioctls below. 376 */ 377 if (!vfh->ctrl_handler) 378 return -ENOTTY; 379 return v4l2_queryctrl(vfh->ctrl_handler, arg); 380 381 case VIDIOC_QUERY_EXT_CTRL: 382 if (!vfh->ctrl_handler) 383 return -ENOTTY; 384 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg); 385 386 case VIDIOC_QUERYMENU: 387 if (!vfh->ctrl_handler) 388 return -ENOTTY; 389 return v4l2_querymenu(vfh->ctrl_handler, arg); 390 391 case VIDIOC_G_CTRL: 392 if (!vfh->ctrl_handler) 393 return -ENOTTY; 394 return v4l2_g_ctrl(vfh->ctrl_handler, arg); 395 396 case VIDIOC_S_CTRL: 397 if (!vfh->ctrl_handler) 398 return -ENOTTY; 399 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); 400 401 case VIDIOC_G_EXT_CTRLS: 402 if (!vfh->ctrl_handler) 403 return -ENOTTY; 404 return v4l2_g_ext_ctrls(vfh->ctrl_handler, 405 vdev, sd->v4l2_dev->mdev, arg); 406 407 case VIDIOC_S_EXT_CTRLS: 408 if (!vfh->ctrl_handler) 409 return -ENOTTY; 410 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, 411 vdev, sd->v4l2_dev->mdev, arg); 412 413 case VIDIOC_TRY_EXT_CTRLS: 414 if (!vfh->ctrl_handler) 415 return -ENOTTY; 416 return v4l2_try_ext_ctrls(vfh->ctrl_handler, 417 vdev, sd->v4l2_dev->mdev, arg); 418 419 case VIDIOC_DQEVENT: 420 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 421 return -ENOIOCTLCMD; 422 423 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); 424 425 case VIDIOC_SUBSCRIBE_EVENT: 426 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); 427 428 case VIDIOC_UNSUBSCRIBE_EVENT: 429 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); 430 431 #ifdef CONFIG_VIDEO_ADV_DEBUG 432 case VIDIOC_DBG_G_REGISTER: 433 { 434 struct v4l2_dbg_register *p = arg; 435 436 if (!capable(CAP_SYS_ADMIN)) 437 return -EPERM; 438 return v4l2_subdev_call(sd, core, g_register, p); 439 } 440 case VIDIOC_DBG_S_REGISTER: 441 { 442 struct v4l2_dbg_register *p = arg; 443 444 if (!capable(CAP_SYS_ADMIN)) 445 return -EPERM; 446 return v4l2_subdev_call(sd, core, s_register, p); 447 } 448 case VIDIOC_DBG_G_CHIP_INFO: 449 { 450 struct v4l2_dbg_chip_info *p = arg; 451 452 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr) 453 return -EINVAL; 454 if (sd->ops->core && sd->ops->core->s_register) 455 p->flags |= V4L2_CHIP_FL_WRITABLE; 456 if (sd->ops->core && sd->ops->core->g_register) 457 p->flags |= V4L2_CHIP_FL_READABLE; 458 strscpy(p->name, sd->name, sizeof(p->name)); 459 return 0; 460 } 461 #endif 462 463 case VIDIOC_LOG_STATUS: { 464 int ret; 465 466 pr_info("%s: ================= START STATUS =================\n", 467 sd->name); 468 ret = v4l2_subdev_call(sd, core, log_status); 469 pr_info("%s: ================== END STATUS ==================\n", 470 sd->name); 471 return ret; 472 } 473 474 case VIDIOC_SUBDEV_G_FMT: { 475 struct v4l2_subdev_format *format = arg; 476 477 memset(format->reserved, 0, sizeof(format->reserved)); 478 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 479 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->state, format); 480 } 481 482 case VIDIOC_SUBDEV_S_FMT: { 483 struct v4l2_subdev_format *format = arg; 484 485 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 486 return -EPERM; 487 488 memset(format->reserved, 0, sizeof(format->reserved)); 489 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 490 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->state, format); 491 } 492 493 case VIDIOC_SUBDEV_G_CROP: { 494 struct v4l2_subdev_crop *crop = arg; 495 struct v4l2_subdev_selection sel; 496 497 memset(crop->reserved, 0, sizeof(crop->reserved)); 498 memset(&sel, 0, sizeof(sel)); 499 sel.which = crop->which; 500 sel.pad = crop->pad; 501 sel.target = V4L2_SEL_TGT_CROP; 502 503 rval = v4l2_subdev_call( 504 sd, pad, get_selection, subdev_fh->state, &sel); 505 506 crop->rect = sel.r; 507 508 return rval; 509 } 510 511 case VIDIOC_SUBDEV_S_CROP: { 512 struct v4l2_subdev_crop *crop = arg; 513 struct v4l2_subdev_selection sel; 514 515 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 516 return -EPERM; 517 518 memset(crop->reserved, 0, sizeof(crop->reserved)); 519 memset(&sel, 0, sizeof(sel)); 520 sel.which = crop->which; 521 sel.pad = crop->pad; 522 sel.target = V4L2_SEL_TGT_CROP; 523 sel.r = crop->rect; 524 525 rval = v4l2_subdev_call( 526 sd, pad, set_selection, subdev_fh->state, &sel); 527 528 crop->rect = sel.r; 529 530 return rval; 531 } 532 533 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { 534 struct v4l2_subdev_mbus_code_enum *code = arg; 535 536 memset(code->reserved, 0, sizeof(code->reserved)); 537 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->state, 538 code); 539 } 540 541 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { 542 struct v4l2_subdev_frame_size_enum *fse = arg; 543 544 memset(fse->reserved, 0, sizeof(fse->reserved)); 545 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->state, 546 fse); 547 } 548 549 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: { 550 struct v4l2_subdev_frame_interval *fi = arg; 551 552 memset(fi->reserved, 0, sizeof(fi->reserved)); 553 return v4l2_subdev_call(sd, video, g_frame_interval, arg); 554 } 555 556 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: { 557 struct v4l2_subdev_frame_interval *fi = arg; 558 559 if (ro_subdev) 560 return -EPERM; 561 562 memset(fi->reserved, 0, sizeof(fi->reserved)); 563 return v4l2_subdev_call(sd, video, s_frame_interval, arg); 564 } 565 566 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { 567 struct v4l2_subdev_frame_interval_enum *fie = arg; 568 569 memset(fie->reserved, 0, sizeof(fie->reserved)); 570 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->state, 571 fie); 572 } 573 574 case VIDIOC_SUBDEV_G_SELECTION: { 575 struct v4l2_subdev_selection *sel = arg; 576 577 memset(sel->reserved, 0, sizeof(sel->reserved)); 578 return v4l2_subdev_call( 579 sd, pad, get_selection, subdev_fh->state, sel); 580 } 581 582 case VIDIOC_SUBDEV_S_SELECTION: { 583 struct v4l2_subdev_selection *sel = arg; 584 585 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 586 return -EPERM; 587 588 memset(sel->reserved, 0, sizeof(sel->reserved)); 589 return v4l2_subdev_call( 590 sd, pad, set_selection, subdev_fh->state, sel); 591 } 592 593 case VIDIOC_G_EDID: { 594 struct v4l2_subdev_edid *edid = arg; 595 596 return v4l2_subdev_call(sd, pad, get_edid, edid); 597 } 598 599 case VIDIOC_S_EDID: { 600 struct v4l2_subdev_edid *edid = arg; 601 602 return v4l2_subdev_call(sd, pad, set_edid, edid); 603 } 604 605 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: { 606 struct v4l2_dv_timings_cap *cap = arg; 607 608 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap); 609 } 610 611 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: { 612 struct v4l2_enum_dv_timings *dvt = arg; 613 614 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt); 615 } 616 617 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS: 618 return v4l2_subdev_call(sd, video, query_dv_timings, arg); 619 620 case VIDIOC_SUBDEV_G_DV_TIMINGS: 621 return v4l2_subdev_call(sd, video, g_dv_timings, arg); 622 623 case VIDIOC_SUBDEV_S_DV_TIMINGS: 624 if (ro_subdev) 625 return -EPERM; 626 627 return v4l2_subdev_call(sd, video, s_dv_timings, arg); 628 629 case VIDIOC_SUBDEV_G_STD: 630 return v4l2_subdev_call(sd, video, g_std, arg); 631 632 case VIDIOC_SUBDEV_S_STD: { 633 v4l2_std_id *std = arg; 634 635 if (ro_subdev) 636 return -EPERM; 637 638 return v4l2_subdev_call(sd, video, s_std, *std); 639 } 640 641 case VIDIOC_SUBDEV_ENUMSTD: { 642 struct v4l2_standard *p = arg; 643 v4l2_std_id id; 644 645 if (v4l2_subdev_call(sd, video, g_tvnorms, &id)) 646 return -EINVAL; 647 648 return v4l_video_std_enumstd(p, id); 649 } 650 651 case VIDIOC_SUBDEV_QUERYSTD: 652 return v4l2_subdev_call(sd, video, querystd, arg); 653 654 default: 655 return v4l2_subdev_call(sd, core, ioctl, cmd, arg); 656 } 657 658 return 0; 659 } 660 661 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg) 662 { 663 struct video_device *vdev = video_devdata(file); 664 struct mutex *lock = vdev->lock; 665 long ret = -ENODEV; 666 667 if (lock && mutex_lock_interruptible(lock)) 668 return -ERESTARTSYS; 669 if (video_is_registered(vdev)) 670 ret = subdev_do_ioctl(file, cmd, arg); 671 if (lock) 672 mutex_unlock(lock); 673 return ret; 674 } 675 676 static long subdev_ioctl(struct file *file, unsigned int cmd, 677 unsigned long arg) 678 { 679 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock); 680 } 681 682 #ifdef CONFIG_COMPAT 683 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 684 unsigned long arg) 685 { 686 struct video_device *vdev = video_devdata(file); 687 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 688 689 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg); 690 } 691 #endif 692 693 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 694 static long subdev_ioctl(struct file *file, unsigned int cmd, 695 unsigned long arg) 696 { 697 return -ENODEV; 698 } 699 700 #ifdef CONFIG_COMPAT 701 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 702 unsigned long arg) 703 { 704 return -ENODEV; 705 } 706 #endif 707 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 708 709 static __poll_t subdev_poll(struct file *file, poll_table *wait) 710 { 711 struct video_device *vdev = video_devdata(file); 712 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 713 struct v4l2_fh *fh = file->private_data; 714 715 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 716 return EPOLLERR; 717 718 poll_wait(file, &fh->wait, wait); 719 720 if (v4l2_event_pending(fh)) 721 return EPOLLPRI; 722 723 return 0; 724 } 725 726 const struct v4l2_file_operations v4l2_subdev_fops = { 727 .owner = THIS_MODULE, 728 .open = subdev_open, 729 .unlocked_ioctl = subdev_ioctl, 730 #ifdef CONFIG_COMPAT 731 .compat_ioctl32 = subdev_compat_ioctl32, 732 #endif 733 .release = subdev_close, 734 .poll = subdev_poll, 735 }; 736 737 #ifdef CONFIG_MEDIA_CONTROLLER 738 739 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity, 740 struct fwnode_endpoint *endpoint) 741 { 742 struct fwnode_handle *fwnode; 743 struct v4l2_subdev *sd; 744 745 if (!is_media_entity_v4l2_subdev(entity)) 746 return -EINVAL; 747 748 sd = media_entity_to_v4l2_subdev(entity); 749 750 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode); 751 fwnode_handle_put(fwnode); 752 753 if (dev_fwnode(sd->dev) == fwnode) 754 return endpoint->port; 755 756 return -ENXIO; 757 } 758 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1); 759 760 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd, 761 struct media_link *link, 762 struct v4l2_subdev_format *source_fmt, 763 struct v4l2_subdev_format *sink_fmt) 764 { 765 bool pass = true; 766 767 /* The width, height and code must match. */ 768 if (source_fmt->format.width != sink_fmt->format.width) { 769 dev_dbg(sd->entity.graph_obj.mdev->dev, 770 "%s: width does not match (source %u, sink %u)\n", 771 __func__, 772 source_fmt->format.width, sink_fmt->format.width); 773 pass = false; 774 } 775 776 if (source_fmt->format.height != sink_fmt->format.height) { 777 dev_dbg(sd->entity.graph_obj.mdev->dev, 778 "%s: height does not match (source %u, sink %u)\n", 779 __func__, 780 source_fmt->format.height, sink_fmt->format.height); 781 pass = false; 782 } 783 784 if (source_fmt->format.code != sink_fmt->format.code) { 785 dev_dbg(sd->entity.graph_obj.mdev->dev, 786 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n", 787 __func__, 788 source_fmt->format.code, sink_fmt->format.code); 789 pass = false; 790 } 791 792 /* The field order must match, or the sink field order must be NONE 793 * to support interlaced hardware connected to bridges that support 794 * progressive formats only. 795 */ 796 if (source_fmt->format.field != sink_fmt->format.field && 797 sink_fmt->format.field != V4L2_FIELD_NONE) { 798 dev_dbg(sd->entity.graph_obj.mdev->dev, 799 "%s: field does not match (source %u, sink %u)\n", 800 __func__, 801 source_fmt->format.field, sink_fmt->format.field); 802 pass = false; 803 } 804 805 if (pass) 806 return 0; 807 808 dev_dbg(sd->entity.graph_obj.mdev->dev, 809 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__, 810 link->source->entity->name, link->source->index, 811 link->sink->entity->name, link->sink->index); 812 813 return -EPIPE; 814 } 815 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default); 816 817 static int 818 v4l2_subdev_link_validate_get_format(struct media_pad *pad, 819 struct v4l2_subdev_format *fmt) 820 { 821 if (is_media_entity_v4l2_subdev(pad->entity)) { 822 struct v4l2_subdev *sd = 823 media_entity_to_v4l2_subdev(pad->entity); 824 825 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE; 826 fmt->pad = pad->index; 827 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt); 828 } 829 830 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L, 831 "Driver bug! Wrong media entity type 0x%08x, entity %s\n", 832 pad->entity->function, pad->entity->name); 833 834 return -EINVAL; 835 } 836 837 int v4l2_subdev_link_validate(struct media_link *link) 838 { 839 struct v4l2_subdev *sink; 840 struct v4l2_subdev_format sink_fmt, source_fmt; 841 int rval; 842 843 rval = v4l2_subdev_link_validate_get_format( 844 link->source, &source_fmt); 845 if (rval < 0) 846 return 0; 847 848 rval = v4l2_subdev_link_validate_get_format( 849 link->sink, &sink_fmt); 850 if (rval < 0) 851 return 0; 852 853 sink = media_entity_to_v4l2_subdev(link->sink->entity); 854 855 rval = v4l2_subdev_call(sink, pad, link_validate, link, 856 &source_fmt, &sink_fmt); 857 if (rval != -ENOIOCTLCMD) 858 return rval; 859 860 return v4l2_subdev_link_validate_default( 861 sink, link, &source_fmt, &sink_fmt); 862 } 863 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate); 864 865 struct v4l2_subdev_state *v4l2_subdev_alloc_state(struct v4l2_subdev *sd) 866 { 867 struct v4l2_subdev_state *state; 868 int ret; 869 870 state = kzalloc(sizeof(*state), GFP_KERNEL); 871 if (!state) 872 return ERR_PTR(-ENOMEM); 873 874 if (sd->entity.num_pads) { 875 state->pads = kvmalloc_array(sd->entity.num_pads, 876 sizeof(*state->pads), 877 GFP_KERNEL | __GFP_ZERO); 878 if (!state->pads) { 879 ret = -ENOMEM; 880 goto err; 881 } 882 } 883 884 ret = v4l2_subdev_call(sd, pad, init_cfg, state); 885 if (ret < 0 && ret != -ENOIOCTLCMD) 886 goto err; 887 888 return state; 889 890 err: 891 if (state && state->pads) 892 kvfree(state->pads); 893 894 kfree(state); 895 896 return ERR_PTR(ret); 897 } 898 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_state); 899 900 void v4l2_subdev_free_state(struct v4l2_subdev_state *state) 901 { 902 if (!state) 903 return; 904 905 kvfree(state->pads); 906 kfree(state); 907 } 908 EXPORT_SYMBOL_GPL(v4l2_subdev_free_state); 909 910 #endif /* CONFIG_MEDIA_CONTROLLER */ 911 912 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) 913 { 914 INIT_LIST_HEAD(&sd->list); 915 BUG_ON(!ops); 916 sd->ops = ops; 917 sd->v4l2_dev = NULL; 918 sd->flags = 0; 919 sd->name[0] = '\0'; 920 sd->grp_id = 0; 921 sd->dev_priv = NULL; 922 sd->host_priv = NULL; 923 #if defined(CONFIG_MEDIA_CONTROLLER) 924 sd->entity.name = sd->name; 925 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV; 926 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN; 927 #endif 928 } 929 EXPORT_SYMBOL(v4l2_subdev_init); 930 931 void v4l2_subdev_notify_event(struct v4l2_subdev *sd, 932 const struct v4l2_event *ev) 933 { 934 v4l2_event_queue(sd->devnode, ev); 935 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev); 936 } 937 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event); 938