Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 1 | /* |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 2 | * V4L2 sub-device |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 3 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 4 | * Copyright (C) 2010 Nokia Corporation |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 5 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 6 | * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> |
| 7 | * Sakari Ailus <sakari.ailus@iki.fi> |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 8 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 12 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 17 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 21 | */ |
| 22 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 23 | #include <linux/ioctl.h> |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/types.h> |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 26 | #include <linux/videodev2.h> |
Paul Gortmaker | 35a2463 | 2011-08-01 15:26:38 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 28 | |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 29 | #include <media/v4l2-ctrls.h> |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 30 | #include <media/v4l2-device.h> |
| 31 | #include <media/v4l2-ioctl.h> |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 32 | #include <media/v4l2-fh.h> |
| 33 | #include <media/v4l2-event.h> |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 34 | |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 35 | static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) |
| 36 | { |
| 37 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
Sakari Ailus | ae184cd | 2011-10-14 14:14:26 -0300 | [diff] [blame] | 38 | fh->pad = kzalloc(sizeof(*fh->pad) * sd->entity.num_pads, GFP_KERNEL); |
| 39 | if (fh->pad == NULL) |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 40 | return -ENOMEM; |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 41 | #endif |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static void subdev_fh_free(struct v4l2_subdev_fh *fh) |
| 46 | { |
| 47 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
Sakari Ailus | ae184cd | 2011-10-14 14:14:26 -0300 | [diff] [blame] | 48 | kfree(fh->pad); |
| 49 | fh->pad = NULL; |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 50 | #endif |
| 51 | } |
| 52 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 53 | static int subdev_open(struct file *file) |
| 54 | { |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 55 | struct video_device *vdev = video_devdata(file); |
| 56 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 57 | struct v4l2_subdev_fh *subdev_fh; |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 58 | #if defined(CONFIG_MEDIA_CONTROLLER) |
Laurent Pinchart | f0beea8 | 2010-08-01 19:05:09 -0300 | [diff] [blame] | 59 | struct media_entity *entity = NULL; |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 60 | #endif |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 61 | int ret; |
| 62 | |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 63 | subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); |
| 64 | if (subdev_fh == NULL) |
| 65 | return -ENOMEM; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 66 | |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 67 | ret = subdev_fh_init(subdev_fh, sd); |
| 68 | if (ret) { |
| 69 | kfree(subdev_fh); |
| 70 | return ret; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 71 | } |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 72 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 73 | v4l2_fh_init(&subdev_fh->vfh, vdev); |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 74 | v4l2_fh_add(&subdev_fh->vfh); |
| 75 | file->private_data = &subdev_fh->vfh; |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 76 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 77 | if (sd->v4l2_dev->mdev) { |
| 78 | entity = media_entity_get(&sd->entity); |
| 79 | if (!entity) { |
| 80 | ret = -EBUSY; |
| 81 | goto err; |
| 82 | } |
| 83 | } |
| 84 | #endif |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 85 | |
Laurent Pinchart | f0beea8 | 2010-08-01 19:05:09 -0300 | [diff] [blame] | 86 | if (sd->internal_ops && sd->internal_ops->open) { |
| 87 | ret = sd->internal_ops->open(sd, subdev_fh); |
| 88 | if (ret < 0) |
| 89 | goto err; |
| 90 | } |
| 91 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 92 | return 0; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 93 | |
| 94 | err: |
Laurent Pinchart | f0beea8 | 2010-08-01 19:05:09 -0300 | [diff] [blame] | 95 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 96 | if (entity) |
| 97 | media_entity_put(entity); |
| 98 | #endif |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 99 | v4l2_fh_del(&subdev_fh->vfh); |
| 100 | v4l2_fh_exit(&subdev_fh->vfh); |
| 101 | subdev_fh_free(subdev_fh); |
| 102 | kfree(subdev_fh); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 103 | |
| 104 | return ret; |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int subdev_close(struct file *file) |
| 108 | { |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 109 | struct video_device *vdev = video_devdata(file); |
| 110 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 111 | struct v4l2_fh *vfh = file->private_data; |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 112 | struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 113 | |
Laurent Pinchart | f0beea8 | 2010-08-01 19:05:09 -0300 | [diff] [blame] | 114 | if (sd->internal_ops && sd->internal_ops->close) |
| 115 | sd->internal_ops->close(sd, subdev_fh); |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 116 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 117 | if (sd->v4l2_dev->mdev) |
| 118 | media_entity_put(&sd->entity); |
| 119 | #endif |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 120 | v4l2_fh_del(vfh); |
| 121 | v4l2_fh_exit(vfh); |
| 122 | subdev_fh_free(subdev_fh); |
| 123 | kfree(subdev_fh); |
| 124 | file->private_data = NULL; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 125 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg) |
| 130 | { |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 131 | struct video_device *vdev = video_devdata(file); |
| 132 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 133 | struct v4l2_fh *vfh = file->private_data; |
Laurent Pinchart | 333c8b9 | 2010-03-15 20:26:04 -0300 | [diff] [blame] | 134 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
| 135 | struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); |
| 136 | #endif |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 137 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 138 | switch (cmd) { |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 139 | case VIDIOC_QUERYCTRL: |
Hans Verkuil | 18d171b | 2011-05-25 08:52:13 -0300 | [diff] [blame] | 140 | return v4l2_queryctrl(vfh->ctrl_handler, arg); |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 141 | |
Hans Verkuil | e6bee36 | 2014-06-10 04:22:06 -0300 | [diff] [blame^] | 142 | case VIDIOC_QUERY_EXT_CTRL: |
| 143 | return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg); |
| 144 | |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 145 | case VIDIOC_QUERYMENU: |
Hans Verkuil | 18d171b | 2011-05-25 08:52:13 -0300 | [diff] [blame] | 146 | return v4l2_querymenu(vfh->ctrl_handler, arg); |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 147 | |
| 148 | case VIDIOC_G_CTRL: |
Hans Verkuil | 18d171b | 2011-05-25 08:52:13 -0300 | [diff] [blame] | 149 | return v4l2_g_ctrl(vfh->ctrl_handler, arg); |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 150 | |
| 151 | case VIDIOC_S_CTRL: |
Hans Verkuil | ab892ba | 2011-06-07 06:47:18 -0300 | [diff] [blame] | 152 | return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 153 | |
| 154 | case VIDIOC_G_EXT_CTRLS: |
Hans Verkuil | 18d171b | 2011-05-25 08:52:13 -0300 | [diff] [blame] | 155 | return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg); |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 156 | |
| 157 | case VIDIOC_S_EXT_CTRLS: |
Hans Verkuil | ab892ba | 2011-06-07 06:47:18 -0300 | [diff] [blame] | 158 | return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg); |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 159 | |
| 160 | case VIDIOC_TRY_EXT_CTRLS: |
Hans Verkuil | 18d171b | 2011-05-25 08:52:13 -0300 | [diff] [blame] | 161 | return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg); |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 162 | |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 163 | case VIDIOC_DQEVENT: |
| 164 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) |
| 165 | return -ENOIOCTLCMD; |
| 166 | |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 167 | return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 168 | |
| 169 | case VIDIOC_SUBSCRIBE_EVENT: |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 170 | return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 171 | |
| 172 | case VIDIOC_UNSUBSCRIBE_EVENT: |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame] | 173 | return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); |
Martin Hostettler | 69967a7 | 2011-09-19 02:04:56 -0300 | [diff] [blame] | 174 | |
| 175 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 176 | case VIDIOC_DBG_G_REGISTER: |
| 177 | { |
| 178 | struct v4l2_dbg_register *p = arg; |
| 179 | |
| 180 | if (!capable(CAP_SYS_ADMIN)) |
| 181 | return -EPERM; |
| 182 | return v4l2_subdev_call(sd, core, g_register, p); |
| 183 | } |
| 184 | case VIDIOC_DBG_S_REGISTER: |
| 185 | { |
| 186 | struct v4l2_dbg_register *p = arg; |
| 187 | |
| 188 | if (!capable(CAP_SYS_ADMIN)) |
| 189 | return -EPERM; |
| 190 | return v4l2_subdev_call(sd, core, s_register, p); |
| 191 | } |
| 192 | #endif |
Sylwester Nawrocki | bcd158d | 2011-10-01 11:13:59 -0300 | [diff] [blame] | 193 | |
Hans Verkuil | 42194e7 | 2012-02-02 08:26:20 -0300 | [diff] [blame] | 194 | case VIDIOC_LOG_STATUS: { |
| 195 | int ret; |
| 196 | |
| 197 | pr_info("%s: ================= START STATUS =================\n", |
| 198 | sd->name); |
| 199 | ret = v4l2_subdev_call(sd, core, log_status); |
| 200 | pr_info("%s: ================== END STATUS ==================\n", |
| 201 | sd->name); |
| 202 | return ret; |
| 203 | } |
Sylwester Nawrocki | bcd158d | 2011-10-01 11:13:59 -0300 | [diff] [blame] | 204 | |
Laurent Pinchart | 333c8b9 | 2010-03-15 20:26:04 -0300 | [diff] [blame] | 205 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
| 206 | case VIDIOC_SUBDEV_G_FMT: { |
| 207 | struct v4l2_subdev_format *format = arg; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 208 | |
Laurent Pinchart | 333c8b9 | 2010-03-15 20:26:04 -0300 | [diff] [blame] | 209 | if (format->which != V4L2_SUBDEV_FORMAT_TRY && |
| 210 | format->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 211 | return -EINVAL; |
| 212 | |
| 213 | if (format->pad >= sd->entity.num_pads) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format); |
| 217 | } |
| 218 | |
| 219 | case VIDIOC_SUBDEV_S_FMT: { |
| 220 | struct v4l2_subdev_format *format = arg; |
| 221 | |
| 222 | if (format->which != V4L2_SUBDEV_FORMAT_TRY && |
| 223 | format->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 224 | return -EINVAL; |
| 225 | |
| 226 | if (format->pad >= sd->entity.num_pads) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format); |
| 230 | } |
| 231 | |
Antti Koskipaa | f6a5cb1 | 2010-06-23 05:03:42 -0300 | [diff] [blame] | 232 | case VIDIOC_SUBDEV_G_CROP: { |
| 233 | struct v4l2_subdev_crop *crop = arg; |
Sakari Ailus | 5b9d770 | 2011-11-14 15:30:24 -0300 | [diff] [blame] | 234 | struct v4l2_subdev_selection sel; |
| 235 | int rval; |
Antti Koskipaa | f6a5cb1 | 2010-06-23 05:03:42 -0300 | [diff] [blame] | 236 | |
| 237 | if (crop->which != V4L2_SUBDEV_FORMAT_TRY && |
| 238 | crop->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 239 | return -EINVAL; |
| 240 | |
| 241 | if (crop->pad >= sd->entity.num_pads) |
| 242 | return -EINVAL; |
| 243 | |
Sakari Ailus | 5b9d770 | 2011-11-14 15:30:24 -0300 | [diff] [blame] | 244 | rval = v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop); |
| 245 | if (rval != -ENOIOCTLCMD) |
| 246 | return rval; |
| 247 | |
| 248 | memset(&sel, 0, sizeof(sel)); |
| 249 | sel.which = crop->which; |
| 250 | sel.pad = crop->pad; |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 251 | sel.target = V4L2_SEL_TGT_CROP; |
Sakari Ailus | 5b9d770 | 2011-11-14 15:30:24 -0300 | [diff] [blame] | 252 | |
| 253 | rval = v4l2_subdev_call( |
| 254 | sd, pad, get_selection, subdev_fh, &sel); |
| 255 | |
| 256 | crop->rect = sel.r; |
| 257 | |
| 258 | return rval; |
Antti Koskipaa | f6a5cb1 | 2010-06-23 05:03:42 -0300 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | case VIDIOC_SUBDEV_S_CROP: { |
| 262 | struct v4l2_subdev_crop *crop = arg; |
Sakari Ailus | 5b9d770 | 2011-11-14 15:30:24 -0300 | [diff] [blame] | 263 | struct v4l2_subdev_selection sel; |
| 264 | int rval; |
Antti Koskipaa | f6a5cb1 | 2010-06-23 05:03:42 -0300 | [diff] [blame] | 265 | |
| 266 | if (crop->which != V4L2_SUBDEV_FORMAT_TRY && |
| 267 | crop->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 268 | return -EINVAL; |
| 269 | |
| 270 | if (crop->pad >= sd->entity.num_pads) |
| 271 | return -EINVAL; |
| 272 | |
Sakari Ailus | 5b9d770 | 2011-11-14 15:30:24 -0300 | [diff] [blame] | 273 | rval = v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop); |
| 274 | if (rval != -ENOIOCTLCMD) |
| 275 | return rval; |
| 276 | |
| 277 | memset(&sel, 0, sizeof(sel)); |
| 278 | sel.which = crop->which; |
| 279 | sel.pad = crop->pad; |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 280 | sel.target = V4L2_SEL_TGT_CROP; |
Sakari Ailus | 5b9d770 | 2011-11-14 15:30:24 -0300 | [diff] [blame] | 281 | sel.r = crop->rect; |
| 282 | |
| 283 | rval = v4l2_subdev_call( |
| 284 | sd, pad, set_selection, subdev_fh, &sel); |
| 285 | |
| 286 | crop->rect = sel.r; |
| 287 | |
| 288 | return rval; |
Antti Koskipaa | f6a5cb1 | 2010-06-23 05:03:42 -0300 | [diff] [blame] | 289 | } |
| 290 | |
Laurent Pinchart | 333c8b9 | 2010-03-15 20:26:04 -0300 | [diff] [blame] | 291 | case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { |
| 292 | struct v4l2_subdev_mbus_code_enum *code = arg; |
| 293 | |
| 294 | if (code->pad >= sd->entity.num_pads) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh, |
| 298 | code); |
| 299 | } |
| 300 | |
| 301 | case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { |
| 302 | struct v4l2_subdev_frame_size_enum *fse = arg; |
| 303 | |
| 304 | if (fse->pad >= sd->entity.num_pads) |
| 305 | return -EINVAL; |
| 306 | |
| 307 | return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh, |
| 308 | fse); |
| 309 | } |
Laurent Pinchart | 35c3017 | 2010-05-05 11:38:35 -0300 | [diff] [blame] | 310 | |
Sakari Ailus | 743e183 | 2013-04-22 10:24:51 -0300 | [diff] [blame] | 311 | case VIDIOC_SUBDEV_G_FRAME_INTERVAL: { |
| 312 | struct v4l2_subdev_frame_interval *fi = arg; |
Laurent Pinchart | 35c3017 | 2010-05-05 11:38:35 -0300 | [diff] [blame] | 313 | |
Sakari Ailus | 743e183 | 2013-04-22 10:24:51 -0300 | [diff] [blame] | 314 | if (fi->pad >= sd->entity.num_pads) |
| 315 | return -EINVAL; |
| 316 | |
| 317 | return v4l2_subdev_call(sd, video, g_frame_interval, arg); |
| 318 | } |
| 319 | |
| 320 | case VIDIOC_SUBDEV_S_FRAME_INTERVAL: { |
| 321 | struct v4l2_subdev_frame_interval *fi = arg; |
| 322 | |
| 323 | if (fi->pad >= sd->entity.num_pads) |
| 324 | return -EINVAL; |
| 325 | |
Laurent Pinchart | 35c3017 | 2010-05-05 11:38:35 -0300 | [diff] [blame] | 326 | return v4l2_subdev_call(sd, video, s_frame_interval, arg); |
Sakari Ailus | 743e183 | 2013-04-22 10:24:51 -0300 | [diff] [blame] | 327 | } |
Laurent Pinchart | 35c3017 | 2010-05-05 11:38:35 -0300 | [diff] [blame] | 328 | |
| 329 | case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { |
| 330 | struct v4l2_subdev_frame_interval_enum *fie = arg; |
| 331 | |
| 332 | if (fie->pad >= sd->entity.num_pads) |
| 333 | return -EINVAL; |
| 334 | |
| 335 | return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh, |
| 336 | fie); |
| 337 | } |
Sakari Ailus | ae184cd | 2011-10-14 14:14:26 -0300 | [diff] [blame] | 338 | |
| 339 | case VIDIOC_SUBDEV_G_SELECTION: { |
| 340 | struct v4l2_subdev_selection *sel = arg; |
| 341 | |
| 342 | if (sel->which != V4L2_SUBDEV_FORMAT_TRY && |
| 343 | sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 344 | return -EINVAL; |
| 345 | |
| 346 | if (sel->pad >= sd->entity.num_pads) |
| 347 | return -EINVAL; |
| 348 | |
| 349 | return v4l2_subdev_call( |
| 350 | sd, pad, get_selection, subdev_fh, sel); |
| 351 | } |
| 352 | |
| 353 | case VIDIOC_SUBDEV_S_SELECTION: { |
| 354 | struct v4l2_subdev_selection *sel = arg; |
| 355 | |
| 356 | if (sel->which != V4L2_SUBDEV_FORMAT_TRY && |
| 357 | sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 358 | return -EINVAL; |
| 359 | |
| 360 | if (sel->pad >= sd->entity.num_pads) |
| 361 | return -EINVAL; |
| 362 | |
| 363 | return v4l2_subdev_call( |
| 364 | sd, pad, set_selection, subdev_fh, sel); |
| 365 | } |
Hans Verkuil | ed45ce2 | 2012-08-10 06:07:12 -0300 | [diff] [blame] | 366 | |
Laurent Pinchart | f2e9084 | 2014-01-29 10:07:13 -0300 | [diff] [blame] | 367 | case VIDIOC_G_EDID: { |
| 368 | struct v4l2_subdev_edid *edid = arg; |
Hans Verkuil | ed45ce2 | 2012-08-10 06:07:12 -0300 | [diff] [blame] | 369 | |
Laurent Pinchart | f2e9084 | 2014-01-29 10:07:13 -0300 | [diff] [blame] | 370 | if (edid->pad >= sd->entity.num_pads) |
| 371 | return -EINVAL; |
| 372 | if (edid->blocks && edid->edid == NULL) |
| 373 | return -EINVAL; |
| 374 | |
| 375 | return v4l2_subdev_call(sd, pad, get_edid, edid); |
| 376 | } |
| 377 | |
| 378 | case VIDIOC_S_EDID: { |
| 379 | struct v4l2_subdev_edid *edid = arg; |
| 380 | |
| 381 | if (edid->pad >= sd->entity.num_pads) |
| 382 | return -EINVAL; |
| 383 | if (edid->blocks && edid->edid == NULL) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | return v4l2_subdev_call(sd, pad, set_edid, edid); |
| 387 | } |
Laurent Pinchart | 9cfd65e | 2014-01-29 10:07:13 -0300 | [diff] [blame] | 388 | |
| 389 | case VIDIOC_SUBDEV_DV_TIMINGS_CAP: { |
| 390 | struct v4l2_dv_timings_cap *cap = arg; |
| 391 | |
| 392 | if (cap->pad >= sd->entity.num_pads) |
| 393 | return -EINVAL; |
| 394 | |
| 395 | return v4l2_subdev_call(sd, pad, dv_timings_cap, cap); |
| 396 | } |
| 397 | |
| 398 | case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: { |
| 399 | struct v4l2_enum_dv_timings *dvt = arg; |
| 400 | |
| 401 | if (dvt->pad >= sd->entity.num_pads) |
| 402 | return -EINVAL; |
| 403 | |
| 404 | return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt); |
| 405 | } |
| 406 | |
| 407 | case VIDIOC_SUBDEV_QUERY_DV_TIMINGS: |
| 408 | return v4l2_subdev_call(sd, video, query_dv_timings, arg); |
| 409 | |
| 410 | case VIDIOC_SUBDEV_G_DV_TIMINGS: |
| 411 | return v4l2_subdev_call(sd, video, g_dv_timings, arg); |
| 412 | |
| 413 | case VIDIOC_SUBDEV_S_DV_TIMINGS: |
| 414 | return v4l2_subdev_call(sd, video, s_dv_timings, arg); |
Laurent Pinchart | 333c8b9 | 2010-03-15 20:26:04 -0300 | [diff] [blame] | 415 | #endif |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 416 | default: |
Laurent Pinchart | c30b46e | 2010-02-26 12:23:10 -0300 | [diff] [blame] | 417 | return v4l2_subdev_call(sd, core, ioctl, cmd, arg); |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static long subdev_ioctl(struct file *file, unsigned int cmd, |
| 424 | unsigned long arg) |
| 425 | { |
| 426 | return video_usercopy(file, cmd, arg, subdev_do_ioctl); |
| 427 | } |
| 428 | |
Hans Verkuil | ab58a30 | 2014-02-10 08:08:44 -0300 | [diff] [blame] | 429 | #ifdef CONFIG_COMPAT |
| 430 | static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, |
| 431 | unsigned long arg) |
| 432 | { |
| 433 | struct video_device *vdev = video_devdata(file); |
| 434 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
| 435 | |
| 436 | return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg); |
| 437 | } |
| 438 | #endif |
| 439 | |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 440 | static unsigned int subdev_poll(struct file *file, poll_table *wait) |
| 441 | { |
| 442 | struct video_device *vdev = video_devdata(file); |
| 443 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
| 444 | struct v4l2_fh *fh = file->private_data; |
| 445 | |
| 446 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) |
| 447 | return POLLERR; |
| 448 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 449 | poll_wait(file, &fh->wait, wait); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 450 | |
| 451 | if (v4l2_event_pending(fh)) |
| 452 | return POLLPRI; |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 457 | const struct v4l2_file_operations v4l2_subdev_fops = { |
| 458 | .owner = THIS_MODULE, |
| 459 | .open = subdev_open, |
| 460 | .unlocked_ioctl = subdev_ioctl, |
Hans Verkuil | ab58a30 | 2014-02-10 08:08:44 -0300 | [diff] [blame] | 461 | #ifdef CONFIG_COMPAT |
| 462 | .compat_ioctl32 = subdev_compat_ioctl32, |
| 463 | #endif |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 464 | .release = subdev_close, |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 465 | .poll = subdev_poll, |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 466 | }; |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 467 | |
Sakari Ailus | 8227c92 | 2011-10-10 17:01:25 -0300 | [diff] [blame] | 468 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 469 | int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd, |
| 470 | struct media_link *link, |
| 471 | struct v4l2_subdev_format *source_fmt, |
| 472 | struct v4l2_subdev_format *sink_fmt) |
| 473 | { |
| 474 | if (source_fmt->format.width != sink_fmt->format.width |
| 475 | || source_fmt->format.height != sink_fmt->format.height |
| 476 | || source_fmt->format.code != sink_fmt->format.code) |
| 477 | return -EINVAL; |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default); |
| 482 | |
| 483 | static int |
| 484 | v4l2_subdev_link_validate_get_format(struct media_pad *pad, |
| 485 | struct v4l2_subdev_format *fmt) |
| 486 | { |
Laurent Pinchart | 864a121 | 2012-10-20 17:47:22 -0300 | [diff] [blame] | 487 | if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) { |
| 488 | struct v4l2_subdev *sd = |
| 489 | media_entity_to_v4l2_subdev(pad->entity); |
| 490 | |
Sakari Ailus | 8227c92 | 2011-10-10 17:01:25 -0300 | [diff] [blame] | 491 | fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 492 | fmt->pad = pad->index; |
Laurent Pinchart | 864a121 | 2012-10-20 17:47:22 -0300 | [diff] [blame] | 493 | return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt); |
Sakari Ailus | 8227c92 | 2011-10-10 17:01:25 -0300 | [diff] [blame] | 494 | } |
Laurent Pinchart | 864a121 | 2012-10-20 17:47:22 -0300 | [diff] [blame] | 495 | |
| 496 | WARN(pad->entity->type != MEDIA_ENT_T_DEVNODE_V4L, |
| 497 | "Driver bug! Wrong media entity type 0x%08x, entity %s\n", |
| 498 | pad->entity->type, pad->entity->name); |
| 499 | |
| 500 | return -EINVAL; |
Sakari Ailus | 8227c92 | 2011-10-10 17:01:25 -0300 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | int v4l2_subdev_link_validate(struct media_link *link) |
| 504 | { |
| 505 | struct v4l2_subdev *sink; |
| 506 | struct v4l2_subdev_format sink_fmt, source_fmt; |
| 507 | int rval; |
| 508 | |
| 509 | rval = v4l2_subdev_link_validate_get_format( |
| 510 | link->source, &source_fmt); |
| 511 | if (rval < 0) |
| 512 | return 0; |
| 513 | |
| 514 | rval = v4l2_subdev_link_validate_get_format( |
| 515 | link->sink, &sink_fmt); |
| 516 | if (rval < 0) |
| 517 | return 0; |
| 518 | |
| 519 | sink = media_entity_to_v4l2_subdev(link->sink->entity); |
| 520 | |
| 521 | rval = v4l2_subdev_call(sink, pad, link_validate, link, |
| 522 | &source_fmt, &sink_fmt); |
| 523 | if (rval != -ENOIOCTLCMD) |
| 524 | return rval; |
| 525 | |
| 526 | return v4l2_subdev_link_validate_default( |
| 527 | sink, link, &source_fmt, &sink_fmt); |
| 528 | } |
| 529 | EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate); |
| 530 | #endif /* CONFIG_MEDIA_CONTROLLER */ |
| 531 | |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 532 | void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) |
| 533 | { |
| 534 | INIT_LIST_HEAD(&sd->list); |
| 535 | BUG_ON(!ops); |
| 536 | sd->ops = ops; |
| 537 | sd->v4l2_dev = NULL; |
| 538 | sd->flags = 0; |
| 539 | sd->name[0] = '\0'; |
| 540 | sd->grp_id = 0; |
| 541 | sd->dev_priv = NULL; |
| 542 | sd->host_priv = NULL; |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 543 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 544 | sd->entity.name = sd->name; |
| 545 | sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV; |
| 546 | #endif |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 547 | } |
| 548 | EXPORT_SYMBOL(v4l2_subdev_init); |