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