Commit | Line | Data |
---|---|---|
2096a5dc | 1 | /* |
3dd5ee08 | 2 | * V4L2 sub-device |
2096a5dc | 3 | * |
3dd5ee08 | 4 | * Copyright (C) 2010 Nokia Corporation |
2096a5dc | 5 | * |
3dd5ee08 LP |
6 | * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> |
7 | * Sakari Ailus <sakari.ailus@iki.fi> | |
2096a5dc | 8 | * |
3dd5ee08 LP |
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. | |
2096a5dc | 12 | * |
3dd5ee08 LP |
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. | |
2096a5dc | 17 | * |
3dd5ee08 LP |
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 | |
2096a5dc LP |
21 | */ |
22 | ||
2096a5dc | 23 | #include <linux/ioctl.h> |
02adb1cc SA |
24 | #include <linux/slab.h> |
25 | #include <linux/types.h> | |
2096a5dc LP |
26 | #include <linux/videodev2.h> |
27 | ||
ea8aa434 | 28 | #include <media/v4l2-ctrls.h> |
2096a5dc LP |
29 | #include <media/v4l2-device.h> |
30 | #include <media/v4l2-ioctl.h> | |
02adb1cc SA |
31 | #include <media/v4l2-fh.h> |
32 | #include <media/v4l2-event.h> | |
2096a5dc | 33 | |
7cd5a16b SV |
34 | static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) |
35 | { | |
36 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) | |
37 | /* Allocate try format and crop in the same memory block */ | |
38 | fh->try_fmt = kzalloc((sizeof(*fh->try_fmt) + sizeof(*fh->try_crop)) | |
39 | * sd->entity.num_pads, GFP_KERNEL); | |
40 | if (fh->try_fmt == NULL) | |
41 | return -ENOMEM; | |
42 | ||
43 | fh->try_crop = (struct v4l2_rect *) | |
44 | (fh->try_fmt + sd->entity.num_pads); | |
45 | #endif | |
46 | return 0; | |
47 | } | |
48 | ||
49 | static void subdev_fh_free(struct v4l2_subdev_fh *fh) | |
50 | { | |
51 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) | |
52 | kfree(fh->try_fmt); | |
53 | fh->try_fmt = NULL; | |
54 | fh->try_crop = NULL; | |
55 | #endif | |
56 | } | |
57 | ||
2096a5dc LP |
58 | static int subdev_open(struct file *file) |
59 | { | |
02adb1cc SA |
60 | struct video_device *vdev = video_devdata(file); |
61 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
7cd5a16b | 62 | struct v4l2_subdev_fh *subdev_fh; |
61f5db54 | 63 | #if defined(CONFIG_MEDIA_CONTROLLER) |
f0beea8f | 64 | struct media_entity *entity = NULL; |
61f5db54 | 65 | #endif |
02adb1cc SA |
66 | int ret; |
67 | ||
7cd5a16b SV |
68 | subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); |
69 | if (subdev_fh == NULL) | |
70 | return -ENOMEM; | |
02adb1cc | 71 | |
7cd5a16b SV |
72 | ret = subdev_fh_init(subdev_fh, sd); |
73 | if (ret) { | |
74 | kfree(subdev_fh); | |
75 | return ret; | |
76 | } | |
77 | ||
523f46d6 | 78 | v4l2_fh_init(&subdev_fh->vfh, vdev); |
7cd5a16b SV |
79 | v4l2_fh_add(&subdev_fh->vfh); |
80 | file->private_data = &subdev_fh->vfh; | |
61f5db54 LP |
81 | #if defined(CONFIG_MEDIA_CONTROLLER) |
82 | if (sd->v4l2_dev->mdev) { | |
83 | entity = media_entity_get(&sd->entity); | |
84 | if (!entity) { | |
85 | ret = -EBUSY; | |
86 | goto err; | |
87 | } | |
88 | } | |
89 | #endif | |
7cd5a16b | 90 | |
f0beea8f LP |
91 | if (sd->internal_ops && sd->internal_ops->open) { |
92 | ret = sd->internal_ops->open(sd, subdev_fh); | |
93 | if (ret < 0) | |
94 | goto err; | |
95 | } | |
96 | ||
2096a5dc | 97 | return 0; |
02adb1cc SA |
98 | |
99 | err: | |
f0beea8f LP |
100 | #if defined(CONFIG_MEDIA_CONTROLLER) |
101 | if (entity) | |
102 | media_entity_put(entity); | |
103 | #endif | |
7cd5a16b SV |
104 | v4l2_fh_del(&subdev_fh->vfh); |
105 | v4l2_fh_exit(&subdev_fh->vfh); | |
106 | subdev_fh_free(subdev_fh); | |
107 | kfree(subdev_fh); | |
02adb1cc SA |
108 | |
109 | return ret; | |
2096a5dc LP |
110 | } |
111 | ||
112 | static int subdev_close(struct file *file) | |
113 | { | |
61f5db54 LP |
114 | struct video_device *vdev = video_devdata(file); |
115 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
02adb1cc | 116 | struct v4l2_fh *vfh = file->private_data; |
7cd5a16b | 117 | struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); |
02adb1cc | 118 | |
f0beea8f LP |
119 | if (sd->internal_ops && sd->internal_ops->close) |
120 | sd->internal_ops->close(sd, subdev_fh); | |
61f5db54 LP |
121 | #if defined(CONFIG_MEDIA_CONTROLLER) |
122 | if (sd->v4l2_dev->mdev) | |
123 | media_entity_put(&sd->entity); | |
124 | #endif | |
7cd5a16b SV |
125 | v4l2_fh_del(vfh); |
126 | v4l2_fh_exit(vfh); | |
127 | subdev_fh_free(subdev_fh); | |
128 | kfree(subdev_fh); | |
129 | file->private_data = NULL; | |
02adb1cc | 130 | |
2096a5dc LP |
131 | return 0; |
132 | } | |
133 | ||
134 | static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg) | |
135 | { | |
ea8aa434 LP |
136 | struct video_device *vdev = video_devdata(file); |
137 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
7cd5a16b | 138 | struct v4l2_fh *vfh = file->private_data; |
333c8b97 LP |
139 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
140 | struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); | |
141 | #endif | |
ea8aa434 | 142 | |
2096a5dc | 143 | switch (cmd) { |
ea8aa434 | 144 | case VIDIOC_QUERYCTRL: |
18d171ba | 145 | return v4l2_queryctrl(vfh->ctrl_handler, arg); |
ea8aa434 LP |
146 | |
147 | case VIDIOC_QUERYMENU: | |
18d171ba | 148 | return v4l2_querymenu(vfh->ctrl_handler, arg); |
ea8aa434 LP |
149 | |
150 | case VIDIOC_G_CTRL: | |
18d171ba | 151 | return v4l2_g_ctrl(vfh->ctrl_handler, arg); |
ea8aa434 LP |
152 | |
153 | case VIDIOC_S_CTRL: | |
ab892bac | 154 | return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); |
ea8aa434 LP |
155 | |
156 | case VIDIOC_G_EXT_CTRLS: | |
18d171ba | 157 | return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg); |
ea8aa434 LP |
158 | |
159 | case VIDIOC_S_EXT_CTRLS: | |
ab892bac | 160 | return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg); |
ea8aa434 LP |
161 | |
162 | case VIDIOC_TRY_EXT_CTRLS: | |
18d171ba | 163 | return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg); |
ea8aa434 | 164 | |
02adb1cc SA |
165 | case VIDIOC_DQEVENT: |
166 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) | |
167 | return -ENOIOCTLCMD; | |
168 | ||
7cd5a16b | 169 | return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); |
02adb1cc SA |
170 | |
171 | case VIDIOC_SUBSCRIBE_EVENT: | |
7cd5a16b | 172 | return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); |
02adb1cc SA |
173 | |
174 | case VIDIOC_UNSUBSCRIBE_EVENT: | |
7cd5a16b | 175 | return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); |
69967a71 MH |
176 | |
177 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
178 | case VIDIOC_DBG_G_REGISTER: | |
179 | { | |
180 | struct v4l2_dbg_register *p = arg; | |
181 | ||
182 | if (!capable(CAP_SYS_ADMIN)) | |
183 | return -EPERM; | |
184 | return v4l2_subdev_call(sd, core, g_register, p); | |
185 | } | |
186 | case VIDIOC_DBG_S_REGISTER: | |
187 | { | |
188 | struct v4l2_dbg_register *p = arg; | |
189 | ||
190 | if (!capable(CAP_SYS_ADMIN)) | |
191 | return -EPERM; | |
192 | return v4l2_subdev_call(sd, core, s_register, p); | |
193 | } | |
194 | #endif | |
333c8b97 LP |
195 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
196 | case VIDIOC_SUBDEV_G_FMT: { | |
197 | struct v4l2_subdev_format *format = arg; | |
198 | ||
199 | if (format->which != V4L2_SUBDEV_FORMAT_TRY && | |
200 | format->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
201 | return -EINVAL; | |
202 | ||
203 | if (format->pad >= sd->entity.num_pads) | |
204 | return -EINVAL; | |
205 | ||
206 | return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format); | |
207 | } | |
208 | ||
209 | case VIDIOC_SUBDEV_S_FMT: { | |
210 | struct v4l2_subdev_format *format = arg; | |
211 | ||
212 | if (format->which != V4L2_SUBDEV_FORMAT_TRY && | |
213 | format->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
214 | return -EINVAL; | |
215 | ||
216 | if (format->pad >= sd->entity.num_pads) | |
217 | return -EINVAL; | |
02adb1cc | 218 | |
333c8b97 LP |
219 | return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format); |
220 | } | |
221 | ||
f6a5cb1b AK |
222 | case VIDIOC_SUBDEV_G_CROP: { |
223 | struct v4l2_subdev_crop *crop = arg; | |
224 | ||
225 | if (crop->which != V4L2_SUBDEV_FORMAT_TRY && | |
226 | crop->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
227 | return -EINVAL; | |
228 | ||
229 | if (crop->pad >= sd->entity.num_pads) | |
230 | return -EINVAL; | |
231 | ||
232 | return v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop); | |
233 | } | |
234 | ||
235 | case VIDIOC_SUBDEV_S_CROP: { | |
236 | struct v4l2_subdev_crop *crop = arg; | |
237 | ||
238 | if (crop->which != V4L2_SUBDEV_FORMAT_TRY && | |
239 | crop->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
240 | return -EINVAL; | |
241 | ||
242 | if (crop->pad >= sd->entity.num_pads) | |
243 | return -EINVAL; | |
244 | ||
245 | return v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop); | |
246 | } | |
247 | ||
333c8b97 LP |
248 | case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { |
249 | struct v4l2_subdev_mbus_code_enum *code = arg; | |
250 | ||
251 | if (code->pad >= sd->entity.num_pads) | |
252 | return -EINVAL; | |
253 | ||
254 | return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh, | |
255 | code); | |
256 | } | |
257 | ||
258 | case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { | |
259 | struct v4l2_subdev_frame_size_enum *fse = arg; | |
260 | ||
261 | if (fse->pad >= sd->entity.num_pads) | |
262 | return -EINVAL; | |
263 | ||
264 | return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh, | |
265 | fse); | |
266 | } | |
35c3017a LP |
267 | |
268 | case VIDIOC_SUBDEV_G_FRAME_INTERVAL: | |
269 | return v4l2_subdev_call(sd, video, g_frame_interval, arg); | |
270 | ||
271 | case VIDIOC_SUBDEV_S_FRAME_INTERVAL: | |
272 | return v4l2_subdev_call(sd, video, s_frame_interval, arg); | |
273 | ||
274 | case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { | |
275 | struct v4l2_subdev_frame_interval_enum *fie = arg; | |
276 | ||
277 | if (fie->pad >= sd->entity.num_pads) | |
278 | return -EINVAL; | |
279 | ||
280 | return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh, | |
281 | fie); | |
282 | } | |
333c8b97 | 283 | #endif |
2096a5dc | 284 | default: |
c30b46e5 | 285 | return v4l2_subdev_call(sd, core, ioctl, cmd, arg); |
2096a5dc LP |
286 | } |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | static long subdev_ioctl(struct file *file, unsigned int cmd, | |
292 | unsigned long arg) | |
293 | { | |
294 | return video_usercopy(file, cmd, arg, subdev_do_ioctl); | |
295 | } | |
296 | ||
02adb1cc SA |
297 | static unsigned int subdev_poll(struct file *file, poll_table *wait) |
298 | { | |
299 | struct video_device *vdev = video_devdata(file); | |
300 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
301 | struct v4l2_fh *fh = file->private_data; | |
302 | ||
303 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) | |
304 | return POLLERR; | |
305 | ||
523f46d6 | 306 | poll_wait(file, &fh->wait, wait); |
02adb1cc SA |
307 | |
308 | if (v4l2_event_pending(fh)) | |
309 | return POLLPRI; | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
2096a5dc LP |
314 | const struct v4l2_file_operations v4l2_subdev_fops = { |
315 | .owner = THIS_MODULE, | |
316 | .open = subdev_open, | |
317 | .unlocked_ioctl = subdev_ioctl, | |
318 | .release = subdev_close, | |
02adb1cc | 319 | .poll = subdev_poll, |
2096a5dc | 320 | }; |
3dd5ee08 LP |
321 | |
322 | void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) | |
323 | { | |
324 | INIT_LIST_HEAD(&sd->list); | |
325 | BUG_ON(!ops); | |
326 | sd->ops = ops; | |
327 | sd->v4l2_dev = NULL; | |
328 | sd->flags = 0; | |
329 | sd->name[0] = '\0'; | |
330 | sd->grp_id = 0; | |
331 | sd->dev_priv = NULL; | |
332 | sd->host_priv = NULL; | |
61f5db54 LP |
333 | #if defined(CONFIG_MEDIA_CONTROLLER) |
334 | sd->entity.name = sd->name; | |
335 | sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV; | |
336 | #endif | |
3dd5ee08 LP |
337 | } |
338 | EXPORT_SYMBOL(v4l2_subdev_init); |