0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/ioctl.h>
0012 #include <linux/mm.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/types.h>
0016 #include <linux/videodev2.h>
0017 #include <linux/export.h>
0018 #include <linux/version.h>
0019
0020 #include <media/v4l2-ctrls.h>
0021 #include <media/v4l2-device.h>
0022 #include <media/v4l2-ioctl.h>
0023 #include <media/v4l2-fh.h>
0024 #include <media/v4l2-event.h>
0025
0026 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
0027 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
0028 {
0029 struct v4l2_subdev_state *state;
0030 static struct lock_class_key key;
0031
0032 state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key);
0033 if (IS_ERR(state))
0034 return PTR_ERR(state);
0035
0036 fh->state = state;
0037
0038 return 0;
0039 }
0040
0041 static void subdev_fh_free(struct v4l2_subdev_fh *fh)
0042 {
0043 __v4l2_subdev_state_free(fh->state);
0044 fh->state = NULL;
0045 }
0046
0047 static int subdev_open(struct file *file)
0048 {
0049 struct video_device *vdev = video_devdata(file);
0050 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
0051 struct v4l2_subdev_fh *subdev_fh;
0052 int ret;
0053
0054 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
0055 if (subdev_fh == NULL)
0056 return -ENOMEM;
0057
0058 ret = subdev_fh_init(subdev_fh, sd);
0059 if (ret) {
0060 kfree(subdev_fh);
0061 return ret;
0062 }
0063
0064 v4l2_fh_init(&subdev_fh->vfh, vdev);
0065 v4l2_fh_add(&subdev_fh->vfh);
0066 file->private_data = &subdev_fh->vfh;
0067
0068 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
0069 struct module *owner;
0070
0071 owner = sd->entity.graph_obj.mdev->dev->driver->owner;
0072 if (!try_module_get(owner)) {
0073 ret = -EBUSY;
0074 goto err;
0075 }
0076 subdev_fh->owner = owner;
0077 }
0078
0079 if (sd->internal_ops && sd->internal_ops->open) {
0080 ret = sd->internal_ops->open(sd, subdev_fh);
0081 if (ret < 0)
0082 goto err;
0083 }
0084
0085 return 0;
0086
0087 err:
0088 module_put(subdev_fh->owner);
0089 v4l2_fh_del(&subdev_fh->vfh);
0090 v4l2_fh_exit(&subdev_fh->vfh);
0091 subdev_fh_free(subdev_fh);
0092 kfree(subdev_fh);
0093
0094 return ret;
0095 }
0096
0097 static int subdev_close(struct file *file)
0098 {
0099 struct video_device *vdev = video_devdata(file);
0100 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
0101 struct v4l2_fh *vfh = file->private_data;
0102 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
0103
0104 if (sd->internal_ops && sd->internal_ops->close)
0105 sd->internal_ops->close(sd, subdev_fh);
0106 module_put(subdev_fh->owner);
0107 v4l2_fh_del(vfh);
0108 v4l2_fh_exit(vfh);
0109 subdev_fh_free(subdev_fh);
0110 kfree(subdev_fh);
0111 file->private_data = NULL;
0112
0113 return 0;
0114 }
0115 #else
0116 static int subdev_open(struct file *file)
0117 {
0118 return -ENODEV;
0119 }
0120
0121 static int subdev_close(struct file *file)
0122 {
0123 return -ENODEV;
0124 }
0125 #endif
0126
0127 static inline int check_which(u32 which)
0128 {
0129 if (which != V4L2_SUBDEV_FORMAT_TRY &&
0130 which != V4L2_SUBDEV_FORMAT_ACTIVE)
0131 return -EINVAL;
0132
0133 return 0;
0134 }
0135
0136 static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
0137 {
0138 #if defined(CONFIG_MEDIA_CONTROLLER)
0139 if (sd->entity.num_pads) {
0140 if (pad >= sd->entity.num_pads)
0141 return -EINVAL;
0142 return 0;
0143 }
0144 #endif
0145
0146 if (pad > 0)
0147 return -EINVAL;
0148 return 0;
0149 }
0150
0151 static int check_state_pads(u32 which, struct v4l2_subdev_state *state)
0152 {
0153 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
0154 return -EINVAL;
0155
0156 return 0;
0157 }
0158
0159 static inline int check_format(struct v4l2_subdev *sd,
0160 struct v4l2_subdev_state *state,
0161 struct v4l2_subdev_format *format)
0162 {
0163 if (!format)
0164 return -EINVAL;
0165
0166 return check_which(format->which) ? : check_pad(sd, format->pad) ? :
0167 check_state_pads(format->which, state);
0168 }
0169
0170 static int call_get_fmt(struct v4l2_subdev *sd,
0171 struct v4l2_subdev_state *state,
0172 struct v4l2_subdev_format *format)
0173 {
0174 return check_format(sd, state, format) ? :
0175 sd->ops->pad->get_fmt(sd, state, format);
0176 }
0177
0178 static int call_set_fmt(struct v4l2_subdev *sd,
0179 struct v4l2_subdev_state *state,
0180 struct v4l2_subdev_format *format)
0181 {
0182 return check_format(sd, state, format) ? :
0183 sd->ops->pad->set_fmt(sd, state, format);
0184 }
0185
0186 static int call_enum_mbus_code(struct v4l2_subdev *sd,
0187 struct v4l2_subdev_state *state,
0188 struct v4l2_subdev_mbus_code_enum *code)
0189 {
0190 if (!code)
0191 return -EINVAL;
0192
0193 return check_which(code->which) ? : check_pad(sd, code->pad) ? :
0194 check_state_pads(code->which, state) ? :
0195 sd->ops->pad->enum_mbus_code(sd, state, code);
0196 }
0197
0198 static int call_enum_frame_size(struct v4l2_subdev *sd,
0199 struct v4l2_subdev_state *state,
0200 struct v4l2_subdev_frame_size_enum *fse)
0201 {
0202 if (!fse)
0203 return -EINVAL;
0204
0205 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
0206 check_state_pads(fse->which, state) ? :
0207 sd->ops->pad->enum_frame_size(sd, state, fse);
0208 }
0209
0210 static inline int check_frame_interval(struct v4l2_subdev *sd,
0211 struct v4l2_subdev_frame_interval *fi)
0212 {
0213 if (!fi)
0214 return -EINVAL;
0215
0216 return check_pad(sd, fi->pad);
0217 }
0218
0219 static int call_g_frame_interval(struct v4l2_subdev *sd,
0220 struct v4l2_subdev_frame_interval *fi)
0221 {
0222 return check_frame_interval(sd, fi) ? :
0223 sd->ops->video->g_frame_interval(sd, fi);
0224 }
0225
0226 static int call_s_frame_interval(struct v4l2_subdev *sd,
0227 struct v4l2_subdev_frame_interval *fi)
0228 {
0229 return check_frame_interval(sd, fi) ? :
0230 sd->ops->video->s_frame_interval(sd, fi);
0231 }
0232
0233 static int call_enum_frame_interval(struct v4l2_subdev *sd,
0234 struct v4l2_subdev_state *state,
0235 struct v4l2_subdev_frame_interval_enum *fie)
0236 {
0237 if (!fie)
0238 return -EINVAL;
0239
0240 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
0241 check_state_pads(fie->which, state) ? :
0242 sd->ops->pad->enum_frame_interval(sd, state, fie);
0243 }
0244
0245 static inline int check_selection(struct v4l2_subdev *sd,
0246 struct v4l2_subdev_state *state,
0247 struct v4l2_subdev_selection *sel)
0248 {
0249 if (!sel)
0250 return -EINVAL;
0251
0252 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
0253 check_state_pads(sel->which, state);
0254 }
0255
0256 static int call_get_selection(struct v4l2_subdev *sd,
0257 struct v4l2_subdev_state *state,
0258 struct v4l2_subdev_selection *sel)
0259 {
0260 return check_selection(sd, state, sel) ? :
0261 sd->ops->pad->get_selection(sd, state, sel);
0262 }
0263
0264 static int call_set_selection(struct v4l2_subdev *sd,
0265 struct v4l2_subdev_state *state,
0266 struct v4l2_subdev_selection *sel)
0267 {
0268 return check_selection(sd, state, sel) ? :
0269 sd->ops->pad->set_selection(sd, state, sel);
0270 }
0271
0272 static inline int check_edid(struct v4l2_subdev *sd,
0273 struct v4l2_subdev_edid *edid)
0274 {
0275 if (!edid)
0276 return -EINVAL;
0277
0278 if (edid->blocks && edid->edid == NULL)
0279 return -EINVAL;
0280
0281 return check_pad(sd, edid->pad);
0282 }
0283
0284 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
0285 {
0286 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
0287 }
0288
0289 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
0290 {
0291 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
0292 }
0293
0294 static int call_dv_timings_cap(struct v4l2_subdev *sd,
0295 struct v4l2_dv_timings_cap *cap)
0296 {
0297 if (!cap)
0298 return -EINVAL;
0299
0300 return check_pad(sd, cap->pad) ? :
0301 sd->ops->pad->dv_timings_cap(sd, cap);
0302 }
0303
0304 static int call_enum_dv_timings(struct v4l2_subdev *sd,
0305 struct v4l2_enum_dv_timings *dvt)
0306 {
0307 if (!dvt)
0308 return -EINVAL;
0309
0310 return check_pad(sd, dvt->pad) ? :
0311 sd->ops->pad->enum_dv_timings(sd, dvt);
0312 }
0313
0314 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
0315 struct v4l2_mbus_config *config)
0316 {
0317 return check_pad(sd, pad) ? :
0318 sd->ops->pad->get_mbus_config(sd, pad, config);
0319 }
0320
0321 #ifdef CONFIG_MEDIA_CONTROLLER
0322
0323
0324
0325
0326
0327 #define DEFINE_STATE_WRAPPER(f, arg_type) \
0328 static int call_##f##_state(struct v4l2_subdev *sd, \
0329 struct v4l2_subdev_state *_state, \
0330 arg_type *arg) \
0331 { \
0332 struct v4l2_subdev_state *state = _state; \
0333 int ret; \
0334 if (!_state) \
0335 state = v4l2_subdev_lock_and_get_active_state(sd); \
0336 ret = call_##f(sd, state, arg); \
0337 if (!_state && state) \
0338 v4l2_subdev_unlock_state(state); \
0339 return ret; \
0340 }
0341
0342 #else
0343
0344 #define DEFINE_STATE_WRAPPER(f, arg_type) \
0345 static int call_##f##_state(struct v4l2_subdev *sd, \
0346 struct v4l2_subdev_state *state, \
0347 arg_type *arg) \
0348 { \
0349 return call_##f(sd, state, arg); \
0350 }
0351
0352 #endif
0353
0354 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
0355 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
0356 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
0357 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
0358 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
0359 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
0360 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);
0361
0362 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
0363 .get_fmt = call_get_fmt_state,
0364 .set_fmt = call_set_fmt_state,
0365 .enum_mbus_code = call_enum_mbus_code_state,
0366 .enum_frame_size = call_enum_frame_size_state,
0367 .enum_frame_interval = call_enum_frame_interval_state,
0368 .get_selection = call_get_selection_state,
0369 .set_selection = call_set_selection_state,
0370 .get_edid = call_get_edid,
0371 .set_edid = call_set_edid,
0372 .dv_timings_cap = call_dv_timings_cap,
0373 .enum_dv_timings = call_enum_dv_timings,
0374 .get_mbus_config = call_get_mbus_config,
0375 };
0376
0377 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
0378 .g_frame_interval = call_g_frame_interval,
0379 .s_frame_interval = call_s_frame_interval,
0380 };
0381
0382 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
0383 .pad = &v4l2_subdev_call_pad_wrappers,
0384 .video = &v4l2_subdev_call_video_wrappers,
0385 };
0386 EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
0387
0388 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
0389
0390 static struct v4l2_subdev_state *
0391 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
0392 unsigned int cmd, void *arg)
0393 {
0394 u32 which;
0395
0396 switch (cmd) {
0397 default:
0398 return NULL;
0399 case VIDIOC_SUBDEV_G_FMT:
0400 case VIDIOC_SUBDEV_S_FMT:
0401 which = ((struct v4l2_subdev_format *)arg)->which;
0402 break;
0403 case VIDIOC_SUBDEV_G_CROP:
0404 case VIDIOC_SUBDEV_S_CROP:
0405 which = ((struct v4l2_subdev_crop *)arg)->which;
0406 break;
0407 case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
0408 which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
0409 break;
0410 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
0411 which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
0412 break;
0413 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
0414 which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
0415 break;
0416 case VIDIOC_SUBDEV_G_SELECTION:
0417 case VIDIOC_SUBDEV_S_SELECTION:
0418 which = ((struct v4l2_subdev_selection *)arg)->which;
0419 break;
0420 }
0421
0422 return which == V4L2_SUBDEV_FORMAT_TRY ?
0423 subdev_fh->state :
0424 v4l2_subdev_get_unlocked_active_state(sd);
0425 }
0426
0427 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
0428 struct v4l2_subdev_state *state)
0429 {
0430 struct video_device *vdev = video_devdata(file);
0431 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
0432 struct v4l2_fh *vfh = file->private_data;
0433 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
0434 int rval;
0435
0436 switch (cmd) {
0437 case VIDIOC_SUBDEV_QUERYCAP: {
0438 struct v4l2_subdev_capability *cap = arg;
0439
0440 memset(cap->reserved, 0, sizeof(cap->reserved));
0441 cap->version = LINUX_VERSION_CODE;
0442 cap->capabilities = ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0;
0443
0444 return 0;
0445 }
0446
0447 case VIDIOC_QUERYCTRL:
0448
0449
0450
0451
0452
0453
0454
0455
0456 if (!vfh->ctrl_handler)
0457 return -ENOTTY;
0458 return v4l2_queryctrl(vfh->ctrl_handler, arg);
0459
0460 case VIDIOC_QUERY_EXT_CTRL:
0461 if (!vfh->ctrl_handler)
0462 return -ENOTTY;
0463 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
0464
0465 case VIDIOC_QUERYMENU:
0466 if (!vfh->ctrl_handler)
0467 return -ENOTTY;
0468 return v4l2_querymenu(vfh->ctrl_handler, arg);
0469
0470 case VIDIOC_G_CTRL:
0471 if (!vfh->ctrl_handler)
0472 return -ENOTTY;
0473 return v4l2_g_ctrl(vfh->ctrl_handler, arg);
0474
0475 case VIDIOC_S_CTRL:
0476 if (!vfh->ctrl_handler)
0477 return -ENOTTY;
0478 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
0479
0480 case VIDIOC_G_EXT_CTRLS:
0481 if (!vfh->ctrl_handler)
0482 return -ENOTTY;
0483 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
0484 vdev, sd->v4l2_dev->mdev, arg);
0485
0486 case VIDIOC_S_EXT_CTRLS:
0487 if (!vfh->ctrl_handler)
0488 return -ENOTTY;
0489 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
0490 vdev, sd->v4l2_dev->mdev, arg);
0491
0492 case VIDIOC_TRY_EXT_CTRLS:
0493 if (!vfh->ctrl_handler)
0494 return -ENOTTY;
0495 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
0496 vdev, sd->v4l2_dev->mdev, arg);
0497
0498 case VIDIOC_DQEVENT:
0499 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
0500 return -ENOIOCTLCMD;
0501
0502 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
0503
0504 case VIDIOC_SUBSCRIBE_EVENT:
0505 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
0506
0507 case VIDIOC_UNSUBSCRIBE_EVENT:
0508 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
0509
0510 #ifdef CONFIG_VIDEO_ADV_DEBUG
0511 case VIDIOC_DBG_G_REGISTER:
0512 {
0513 struct v4l2_dbg_register *p = arg;
0514
0515 if (!capable(CAP_SYS_ADMIN))
0516 return -EPERM;
0517 return v4l2_subdev_call(sd, core, g_register, p);
0518 }
0519 case VIDIOC_DBG_S_REGISTER:
0520 {
0521 struct v4l2_dbg_register *p = arg;
0522
0523 if (!capable(CAP_SYS_ADMIN))
0524 return -EPERM;
0525 return v4l2_subdev_call(sd, core, s_register, p);
0526 }
0527 case VIDIOC_DBG_G_CHIP_INFO:
0528 {
0529 struct v4l2_dbg_chip_info *p = arg;
0530
0531 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
0532 return -EINVAL;
0533 if (sd->ops->core && sd->ops->core->s_register)
0534 p->flags |= V4L2_CHIP_FL_WRITABLE;
0535 if (sd->ops->core && sd->ops->core->g_register)
0536 p->flags |= V4L2_CHIP_FL_READABLE;
0537 strscpy(p->name, sd->name, sizeof(p->name));
0538 return 0;
0539 }
0540 #endif
0541
0542 case VIDIOC_LOG_STATUS: {
0543 int ret;
0544
0545 pr_info("%s: ================= START STATUS =================\n",
0546 sd->name);
0547 ret = v4l2_subdev_call(sd, core, log_status);
0548 pr_info("%s: ================== END STATUS ==================\n",
0549 sd->name);
0550 return ret;
0551 }
0552
0553 case VIDIOC_SUBDEV_G_FMT: {
0554 struct v4l2_subdev_format *format = arg;
0555
0556 memset(format->reserved, 0, sizeof(format->reserved));
0557 memset(format->format.reserved, 0, sizeof(format->format.reserved));
0558 return v4l2_subdev_call(sd, pad, get_fmt, state, format);
0559 }
0560
0561 case VIDIOC_SUBDEV_S_FMT: {
0562 struct v4l2_subdev_format *format = arg;
0563
0564 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
0565 return -EPERM;
0566
0567 memset(format->reserved, 0, sizeof(format->reserved));
0568 memset(format->format.reserved, 0, sizeof(format->format.reserved));
0569 return v4l2_subdev_call(sd, pad, set_fmt, state, format);
0570 }
0571
0572 case VIDIOC_SUBDEV_G_CROP: {
0573 struct v4l2_subdev_crop *crop = arg;
0574 struct v4l2_subdev_selection sel;
0575
0576 memset(crop->reserved, 0, sizeof(crop->reserved));
0577 memset(&sel, 0, sizeof(sel));
0578 sel.which = crop->which;
0579 sel.pad = crop->pad;
0580 sel.target = V4L2_SEL_TGT_CROP;
0581
0582 rval = v4l2_subdev_call(
0583 sd, pad, get_selection, state, &sel);
0584
0585 crop->rect = sel.r;
0586
0587 return rval;
0588 }
0589
0590 case VIDIOC_SUBDEV_S_CROP: {
0591 struct v4l2_subdev_crop *crop = arg;
0592 struct v4l2_subdev_selection sel;
0593
0594 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
0595 return -EPERM;
0596
0597 memset(crop->reserved, 0, sizeof(crop->reserved));
0598 memset(&sel, 0, sizeof(sel));
0599 sel.which = crop->which;
0600 sel.pad = crop->pad;
0601 sel.target = V4L2_SEL_TGT_CROP;
0602 sel.r = crop->rect;
0603
0604 rval = v4l2_subdev_call(
0605 sd, pad, set_selection, state, &sel);
0606
0607 crop->rect = sel.r;
0608
0609 return rval;
0610 }
0611
0612 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
0613 struct v4l2_subdev_mbus_code_enum *code = arg;
0614
0615 memset(code->reserved, 0, sizeof(code->reserved));
0616 return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
0617 code);
0618 }
0619
0620 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
0621 struct v4l2_subdev_frame_size_enum *fse = arg;
0622
0623 memset(fse->reserved, 0, sizeof(fse->reserved));
0624 return v4l2_subdev_call(sd, pad, enum_frame_size, state,
0625 fse);
0626 }
0627
0628 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
0629 struct v4l2_subdev_frame_interval *fi = arg;
0630
0631 memset(fi->reserved, 0, sizeof(fi->reserved));
0632 return v4l2_subdev_call(sd, video, g_frame_interval, arg);
0633 }
0634
0635 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
0636 struct v4l2_subdev_frame_interval *fi = arg;
0637
0638 if (ro_subdev)
0639 return -EPERM;
0640
0641 memset(fi->reserved, 0, sizeof(fi->reserved));
0642 return v4l2_subdev_call(sd, video, s_frame_interval, arg);
0643 }
0644
0645 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
0646 struct v4l2_subdev_frame_interval_enum *fie = arg;
0647
0648 memset(fie->reserved, 0, sizeof(fie->reserved));
0649 return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
0650 fie);
0651 }
0652
0653 case VIDIOC_SUBDEV_G_SELECTION: {
0654 struct v4l2_subdev_selection *sel = arg;
0655
0656 memset(sel->reserved, 0, sizeof(sel->reserved));
0657 return v4l2_subdev_call(
0658 sd, pad, get_selection, state, sel);
0659 }
0660
0661 case VIDIOC_SUBDEV_S_SELECTION: {
0662 struct v4l2_subdev_selection *sel = arg;
0663
0664 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
0665 return -EPERM;
0666
0667 memset(sel->reserved, 0, sizeof(sel->reserved));
0668 return v4l2_subdev_call(
0669 sd, pad, set_selection, state, sel);
0670 }
0671
0672 case VIDIOC_G_EDID: {
0673 struct v4l2_subdev_edid *edid = arg;
0674
0675 return v4l2_subdev_call(sd, pad, get_edid, edid);
0676 }
0677
0678 case VIDIOC_S_EDID: {
0679 struct v4l2_subdev_edid *edid = arg;
0680
0681 return v4l2_subdev_call(sd, pad, set_edid, edid);
0682 }
0683
0684 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
0685 struct v4l2_dv_timings_cap *cap = arg;
0686
0687 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
0688 }
0689
0690 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
0691 struct v4l2_enum_dv_timings *dvt = arg;
0692
0693 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
0694 }
0695
0696 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
0697 return v4l2_subdev_call(sd, video, query_dv_timings, arg);
0698
0699 case VIDIOC_SUBDEV_G_DV_TIMINGS:
0700 return v4l2_subdev_call(sd, video, g_dv_timings, arg);
0701
0702 case VIDIOC_SUBDEV_S_DV_TIMINGS:
0703 if (ro_subdev)
0704 return -EPERM;
0705
0706 return v4l2_subdev_call(sd, video, s_dv_timings, arg);
0707
0708 case VIDIOC_SUBDEV_G_STD:
0709 return v4l2_subdev_call(sd, video, g_std, arg);
0710
0711 case VIDIOC_SUBDEV_S_STD: {
0712 v4l2_std_id *std = arg;
0713
0714 if (ro_subdev)
0715 return -EPERM;
0716
0717 return v4l2_subdev_call(sd, video, s_std, *std);
0718 }
0719
0720 case VIDIOC_SUBDEV_ENUMSTD: {
0721 struct v4l2_standard *p = arg;
0722 v4l2_std_id id;
0723
0724 if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
0725 return -EINVAL;
0726
0727 return v4l_video_std_enumstd(p, id);
0728 }
0729
0730 case VIDIOC_SUBDEV_QUERYSTD:
0731 return v4l2_subdev_call(sd, video, querystd, arg);
0732
0733 default:
0734 return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
0735 }
0736
0737 return 0;
0738 }
0739
0740 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
0741 {
0742 struct video_device *vdev = video_devdata(file);
0743 struct mutex *lock = vdev->lock;
0744 long ret = -ENODEV;
0745
0746 if (lock && mutex_lock_interruptible(lock))
0747 return -ERESTARTSYS;
0748
0749 if (video_is_registered(vdev)) {
0750 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
0751 struct v4l2_fh *vfh = file->private_data;
0752 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
0753 struct v4l2_subdev_state *state;
0754
0755 state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
0756
0757 if (state)
0758 v4l2_subdev_lock_state(state);
0759
0760 ret = subdev_do_ioctl(file, cmd, arg, state);
0761
0762 if (state)
0763 v4l2_subdev_unlock_state(state);
0764 }
0765
0766 if (lock)
0767 mutex_unlock(lock);
0768 return ret;
0769 }
0770
0771 static long subdev_ioctl(struct file *file, unsigned int cmd,
0772 unsigned long arg)
0773 {
0774 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
0775 }
0776
0777 #ifdef CONFIG_COMPAT
0778 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
0779 unsigned long arg)
0780 {
0781 struct video_device *vdev = video_devdata(file);
0782 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
0783
0784 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
0785 }
0786 #endif
0787
0788 #else
0789 static long subdev_ioctl(struct file *file, unsigned int cmd,
0790 unsigned long arg)
0791 {
0792 return -ENODEV;
0793 }
0794
0795 #ifdef CONFIG_COMPAT
0796 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
0797 unsigned long arg)
0798 {
0799 return -ENODEV;
0800 }
0801 #endif
0802 #endif
0803
0804 static __poll_t subdev_poll(struct file *file, poll_table *wait)
0805 {
0806 struct video_device *vdev = video_devdata(file);
0807 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
0808 struct v4l2_fh *fh = file->private_data;
0809
0810 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
0811 return EPOLLERR;
0812
0813 poll_wait(file, &fh->wait, wait);
0814
0815 if (v4l2_event_pending(fh))
0816 return EPOLLPRI;
0817
0818 return 0;
0819 }
0820
0821 const struct v4l2_file_operations v4l2_subdev_fops = {
0822 .owner = THIS_MODULE,
0823 .open = subdev_open,
0824 .unlocked_ioctl = subdev_ioctl,
0825 #ifdef CONFIG_COMPAT
0826 .compat_ioctl32 = subdev_compat_ioctl32,
0827 #endif
0828 .release = subdev_close,
0829 .poll = subdev_poll,
0830 };
0831
0832 #ifdef CONFIG_MEDIA_CONTROLLER
0833
0834 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
0835 struct fwnode_endpoint *endpoint)
0836 {
0837 struct fwnode_handle *fwnode;
0838 struct v4l2_subdev *sd;
0839
0840 if (!is_media_entity_v4l2_subdev(entity))
0841 return -EINVAL;
0842
0843 sd = media_entity_to_v4l2_subdev(entity);
0844
0845 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
0846 fwnode_handle_put(fwnode);
0847
0848 if (dev_fwnode(sd->dev) == fwnode)
0849 return endpoint->port;
0850
0851 return -ENXIO;
0852 }
0853 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
0854
0855 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
0856 struct media_link *link,
0857 struct v4l2_subdev_format *source_fmt,
0858 struct v4l2_subdev_format *sink_fmt)
0859 {
0860 bool pass = true;
0861
0862
0863 if (source_fmt->format.width != sink_fmt->format.width) {
0864 dev_dbg(sd->entity.graph_obj.mdev->dev,
0865 "%s: width does not match (source %u, sink %u)\n",
0866 __func__,
0867 source_fmt->format.width, sink_fmt->format.width);
0868 pass = false;
0869 }
0870
0871 if (source_fmt->format.height != sink_fmt->format.height) {
0872 dev_dbg(sd->entity.graph_obj.mdev->dev,
0873 "%s: height does not match (source %u, sink %u)\n",
0874 __func__,
0875 source_fmt->format.height, sink_fmt->format.height);
0876 pass = false;
0877 }
0878
0879 if (source_fmt->format.code != sink_fmt->format.code) {
0880 dev_dbg(sd->entity.graph_obj.mdev->dev,
0881 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
0882 __func__,
0883 source_fmt->format.code, sink_fmt->format.code);
0884 pass = false;
0885 }
0886
0887
0888
0889
0890
0891 if (source_fmt->format.field != sink_fmt->format.field &&
0892 sink_fmt->format.field != V4L2_FIELD_NONE) {
0893 dev_dbg(sd->entity.graph_obj.mdev->dev,
0894 "%s: field does not match (source %u, sink %u)\n",
0895 __func__,
0896 source_fmt->format.field, sink_fmt->format.field);
0897 pass = false;
0898 }
0899
0900 if (pass)
0901 return 0;
0902
0903 dev_dbg(sd->entity.graph_obj.mdev->dev,
0904 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
0905 link->source->entity->name, link->source->index,
0906 link->sink->entity->name, link->sink->index);
0907
0908 return -EPIPE;
0909 }
0910 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
0911
0912 static int
0913 v4l2_subdev_link_validate_get_format(struct media_pad *pad,
0914 struct v4l2_subdev_format *fmt)
0915 {
0916 if (is_media_entity_v4l2_subdev(pad->entity)) {
0917 struct v4l2_subdev *sd =
0918 media_entity_to_v4l2_subdev(pad->entity);
0919
0920 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
0921 fmt->pad = pad->index;
0922 return v4l2_subdev_call_state_active(sd, pad, get_fmt, fmt);
0923 }
0924
0925 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
0926 "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
0927 pad->entity->function, pad->entity->name);
0928
0929 return -EINVAL;
0930 }
0931
0932 int v4l2_subdev_link_validate(struct media_link *link)
0933 {
0934 struct v4l2_subdev *sink;
0935 struct v4l2_subdev_format sink_fmt, source_fmt;
0936 int rval;
0937
0938 rval = v4l2_subdev_link_validate_get_format(
0939 link->source, &source_fmt);
0940 if (rval < 0)
0941 return 0;
0942
0943 rval = v4l2_subdev_link_validate_get_format(
0944 link->sink, &sink_fmt);
0945 if (rval < 0)
0946 return 0;
0947
0948 sink = media_entity_to_v4l2_subdev(link->sink->entity);
0949
0950 rval = v4l2_subdev_call(sink, pad, link_validate, link,
0951 &source_fmt, &sink_fmt);
0952 if (rval != -ENOIOCTLCMD)
0953 return rval;
0954
0955 return v4l2_subdev_link_validate_default(
0956 sink, link, &source_fmt, &sink_fmt);
0957 }
0958 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
0959
0960 struct v4l2_subdev_state *
0961 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
0962 struct lock_class_key *lock_key)
0963 {
0964 struct v4l2_subdev_state *state;
0965 int ret;
0966
0967 state = kzalloc(sizeof(*state), GFP_KERNEL);
0968 if (!state)
0969 return ERR_PTR(-ENOMEM);
0970
0971 __mutex_init(&state->_lock, lock_name, lock_key);
0972 if (sd->state_lock)
0973 state->lock = sd->state_lock;
0974 else
0975 state->lock = &state->_lock;
0976
0977 if (sd->entity.num_pads) {
0978 state->pads = kvcalloc(sd->entity.num_pads,
0979 sizeof(*state->pads), GFP_KERNEL);
0980 if (!state->pads) {
0981 ret = -ENOMEM;
0982 goto err;
0983 }
0984 }
0985
0986
0987
0988
0989
0990 v4l2_subdev_lock_state(state);
0991 ret = v4l2_subdev_call(sd, pad, init_cfg, state);
0992 v4l2_subdev_unlock_state(state);
0993
0994 if (ret < 0 && ret != -ENOIOCTLCMD)
0995 goto err;
0996
0997 return state;
0998
0999 err:
1000 if (state && state->pads)
1001 kvfree(state->pads);
1002
1003 kfree(state);
1004
1005 return ERR_PTR(ret);
1006 }
1007 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
1008
1009 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
1010 {
1011 if (!state)
1012 return;
1013
1014 mutex_destroy(&state->_lock);
1015
1016 kvfree(state->pads);
1017 kfree(state);
1018 }
1019 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
1020
1021 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1022 struct lock_class_key *key)
1023 {
1024 struct v4l2_subdev_state *state;
1025
1026 state = __v4l2_subdev_state_alloc(sd, name, key);
1027 if (IS_ERR(state))
1028 return PTR_ERR(state);
1029
1030 sd->active_state = state;
1031
1032 return 0;
1033 }
1034 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
1035
1036 void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
1037 {
1038 __v4l2_subdev_state_free(sd->active_state);
1039 sd->active_state = NULL;
1040 }
1041 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
1042
1043 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1044
1045 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1046 struct v4l2_subdev_format *format)
1047 {
1048 struct v4l2_mbus_framefmt *fmt;
1049
1050 if (format->pad >= sd->entity.num_pads)
1051 return -EINVAL;
1052
1053 fmt = v4l2_subdev_get_pad_format(sd, state, format->pad);
1054 if (!fmt)
1055 return -EINVAL;
1056
1057 format->format = *fmt;
1058
1059 return 0;
1060 }
1061 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
1062
1063 #endif
1064
1065 #endif
1066
1067 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
1068 {
1069 INIT_LIST_HEAD(&sd->list);
1070 BUG_ON(!ops);
1071 sd->ops = ops;
1072 sd->v4l2_dev = NULL;
1073 sd->flags = 0;
1074 sd->name[0] = '\0';
1075 sd->grp_id = 0;
1076 sd->dev_priv = NULL;
1077 sd->host_priv = NULL;
1078 #if defined(CONFIG_MEDIA_CONTROLLER)
1079 sd->entity.name = sd->name;
1080 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
1081 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
1082 #endif
1083 }
1084 EXPORT_SYMBOL(v4l2_subdev_init);
1085
1086 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
1087 const struct v4l2_event *ev)
1088 {
1089 v4l2_event_queue(sd->devnode, ev);
1090 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
1091 }
1092 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);