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 | 26 | #include <linux/videodev2.h> |
35a24636 | 27 | #include <linux/export.h> |
2096a5dc | 28 | |
ea8aa434 | 29 | #include <media/v4l2-ctrls.h> |
2096a5dc LP |
30 | #include <media/v4l2-device.h> |
31 | #include <media/v4l2-ioctl.h> | |
02adb1cc SA |
32 | #include <media/v4l2-fh.h> |
33 | #include <media/v4l2-event.h> | |
2096a5dc | 34 | |
7cd5a16b SV |
35 | static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) |
36 | { | |
37 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) | |
ae184cda SA |
38 | fh->pad = kzalloc(sizeof(*fh->pad) * sd->entity.num_pads, GFP_KERNEL); |
39 | if (fh->pad == NULL) | |
7cd5a16b | 40 | return -ENOMEM; |
7cd5a16b SV |
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) | |
ae184cda SA |
48 | kfree(fh->pad); |
49 | fh->pad = NULL; | |
7cd5a16b SV |
50 | #endif |
51 | } | |
52 | ||
2096a5dc LP |
53 | static int subdev_open(struct file *file) |
54 | { | |
02adb1cc SA |
55 | struct video_device *vdev = video_devdata(file); |
56 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
7cd5a16b | 57 | struct v4l2_subdev_fh *subdev_fh; |
61f5db54 | 58 | #if defined(CONFIG_MEDIA_CONTROLLER) |
f0beea8f | 59 | struct media_entity *entity = NULL; |
61f5db54 | 60 | #endif |
02adb1cc SA |
61 | int ret; |
62 | ||
7cd5a16b SV |
63 | subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); |
64 | if (subdev_fh == NULL) | |
65 | return -ENOMEM; | |
02adb1cc | 66 | |
7cd5a16b SV |
67 | ret = subdev_fh_init(subdev_fh, sd); |
68 | if (ret) { | |
69 | kfree(subdev_fh); | |
70 | return ret; | |
71 | } | |
72 | ||
523f46d6 | 73 | v4l2_fh_init(&subdev_fh->vfh, vdev); |
7cd5a16b SV |
74 | v4l2_fh_add(&subdev_fh->vfh); |
75 | file->private_data = &subdev_fh->vfh; | |
61f5db54 LP |
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 | |
7cd5a16b | 85 | |
f0beea8f LP |
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 | ||
2096a5dc | 92 | return 0; |
02adb1cc SA |
93 | |
94 | err: | |
f0beea8f LP |
95 | #if defined(CONFIG_MEDIA_CONTROLLER) |
96 | if (entity) | |
97 | media_entity_put(entity); | |
98 | #endif | |
7cd5a16b SV |
99 | v4l2_fh_del(&subdev_fh->vfh); |
100 | v4l2_fh_exit(&subdev_fh->vfh); | |
101 | subdev_fh_free(subdev_fh); | |
102 | kfree(subdev_fh); | |
02adb1cc SA |
103 | |
104 | return ret; | |
2096a5dc LP |
105 | } |
106 | ||
107 | static int subdev_close(struct file *file) | |
108 | { | |
61f5db54 LP |
109 | struct video_device *vdev = video_devdata(file); |
110 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
02adb1cc | 111 | struct v4l2_fh *vfh = file->private_data; |
7cd5a16b | 112 | struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); |
02adb1cc | 113 | |
f0beea8f LP |
114 | if (sd->internal_ops && sd->internal_ops->close) |
115 | sd->internal_ops->close(sd, subdev_fh); | |
61f5db54 LP |
116 | #if defined(CONFIG_MEDIA_CONTROLLER) |
117 | if (sd->v4l2_dev->mdev) | |
118 | media_entity_put(&sd->entity); | |
119 | #endif | |
7cd5a16b SV |
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; | |
02adb1cc | 125 | |
2096a5dc LP |
126 | return 0; |
127 | } | |
128 | ||
129 | static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg) | |
130 | { | |
ea8aa434 LP |
131 | struct video_device *vdev = video_devdata(file); |
132 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
7cd5a16b | 133 | struct v4l2_fh *vfh = file->private_data; |
333c8b97 LP |
134 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
135 | struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); | |
136 | #endif | |
ea8aa434 | 137 | |
2096a5dc | 138 | switch (cmd) { |
ea8aa434 | 139 | case VIDIOC_QUERYCTRL: |
18d171ba | 140 | return v4l2_queryctrl(vfh->ctrl_handler, arg); |
ea8aa434 LP |
141 | |
142 | case VIDIOC_QUERYMENU: | |
18d171ba | 143 | return v4l2_querymenu(vfh->ctrl_handler, arg); |
ea8aa434 LP |
144 | |
145 | case VIDIOC_G_CTRL: | |
18d171ba | 146 | return v4l2_g_ctrl(vfh->ctrl_handler, arg); |
ea8aa434 LP |
147 | |
148 | case VIDIOC_S_CTRL: | |
ab892bac | 149 | return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); |
ea8aa434 LP |
150 | |
151 | case VIDIOC_G_EXT_CTRLS: | |
18d171ba | 152 | return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg); |
ea8aa434 LP |
153 | |
154 | case VIDIOC_S_EXT_CTRLS: | |
ab892bac | 155 | return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg); |
ea8aa434 LP |
156 | |
157 | case VIDIOC_TRY_EXT_CTRLS: | |
18d171ba | 158 | return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg); |
ea8aa434 | 159 | |
02adb1cc SA |
160 | case VIDIOC_DQEVENT: |
161 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) | |
162 | return -ENOIOCTLCMD; | |
163 | ||
7cd5a16b | 164 | return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); |
02adb1cc SA |
165 | |
166 | case VIDIOC_SUBSCRIBE_EVENT: | |
7cd5a16b | 167 | return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); |
02adb1cc SA |
168 | |
169 | case VIDIOC_UNSUBSCRIBE_EVENT: | |
7cd5a16b | 170 | return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); |
69967a71 MH |
171 | |
172 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
173 | case VIDIOC_DBG_G_REGISTER: | |
174 | { | |
175 | struct v4l2_dbg_register *p = arg; | |
176 | ||
177 | if (!capable(CAP_SYS_ADMIN)) | |
178 | return -EPERM; | |
179 | return v4l2_subdev_call(sd, core, g_register, p); | |
180 | } | |
181 | case VIDIOC_DBG_S_REGISTER: | |
182 | { | |
183 | struct v4l2_dbg_register *p = arg; | |
184 | ||
185 | if (!capable(CAP_SYS_ADMIN)) | |
186 | return -EPERM; | |
187 | return v4l2_subdev_call(sd, core, s_register, p); | |
188 | } | |
189 | #endif | |
bcd158de | 190 | |
42194e72 HV |
191 | case VIDIOC_LOG_STATUS: { |
192 | int ret; | |
193 | ||
194 | pr_info("%s: ================= START STATUS =================\n", | |
195 | sd->name); | |
196 | ret = v4l2_subdev_call(sd, core, log_status); | |
197 | pr_info("%s: ================== END STATUS ==================\n", | |
198 | sd->name); | |
199 | return ret; | |
200 | } | |
bcd158de | 201 | |
333c8b97 LP |
202 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
203 | case VIDIOC_SUBDEV_G_FMT: { | |
204 | struct v4l2_subdev_format *format = arg; | |
205 | ||
206 | if (format->which != V4L2_SUBDEV_FORMAT_TRY && | |
207 | format->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
208 | return -EINVAL; | |
209 | ||
210 | if (format->pad >= sd->entity.num_pads) | |
211 | return -EINVAL; | |
212 | ||
213 | return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format); | |
214 | } | |
215 | ||
216 | case VIDIOC_SUBDEV_S_FMT: { | |
217 | struct v4l2_subdev_format *format = arg; | |
218 | ||
219 | if (format->which != V4L2_SUBDEV_FORMAT_TRY && | |
220 | format->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
221 | return -EINVAL; | |
222 | ||
223 | if (format->pad >= sd->entity.num_pads) | |
224 | return -EINVAL; | |
02adb1cc | 225 | |
333c8b97 LP |
226 | return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format); |
227 | } | |
228 | ||
f6a5cb1b AK |
229 | case VIDIOC_SUBDEV_G_CROP: { |
230 | struct v4l2_subdev_crop *crop = arg; | |
231 | ||
232 | if (crop->which != V4L2_SUBDEV_FORMAT_TRY && | |
233 | crop->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
234 | return -EINVAL; | |
235 | ||
236 | if (crop->pad >= sd->entity.num_pads) | |
237 | return -EINVAL; | |
238 | ||
239 | return v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop); | |
240 | } | |
241 | ||
242 | case VIDIOC_SUBDEV_S_CROP: { | |
243 | struct v4l2_subdev_crop *crop = arg; | |
244 | ||
245 | if (crop->which != V4L2_SUBDEV_FORMAT_TRY && | |
246 | crop->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
247 | return -EINVAL; | |
248 | ||
249 | if (crop->pad >= sd->entity.num_pads) | |
250 | return -EINVAL; | |
251 | ||
252 | return v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop); | |
253 | } | |
254 | ||
333c8b97 LP |
255 | case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { |
256 | struct v4l2_subdev_mbus_code_enum *code = arg; | |
257 | ||
258 | if (code->pad >= sd->entity.num_pads) | |
259 | return -EINVAL; | |
260 | ||
261 | return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh, | |
262 | code); | |
263 | } | |
264 | ||
265 | case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { | |
266 | struct v4l2_subdev_frame_size_enum *fse = arg; | |
267 | ||
268 | if (fse->pad >= sd->entity.num_pads) | |
269 | return -EINVAL; | |
270 | ||
271 | return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh, | |
272 | fse); | |
273 | } | |
35c3017a LP |
274 | |
275 | case VIDIOC_SUBDEV_G_FRAME_INTERVAL: | |
276 | return v4l2_subdev_call(sd, video, g_frame_interval, arg); | |
277 | ||
278 | case VIDIOC_SUBDEV_S_FRAME_INTERVAL: | |
279 | return v4l2_subdev_call(sd, video, s_frame_interval, arg); | |
280 | ||
281 | case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { | |
282 | struct v4l2_subdev_frame_interval_enum *fie = arg; | |
283 | ||
284 | if (fie->pad >= sd->entity.num_pads) | |
285 | return -EINVAL; | |
286 | ||
287 | return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh, | |
288 | fie); | |
289 | } | |
ae184cda SA |
290 | |
291 | case VIDIOC_SUBDEV_G_SELECTION: { | |
292 | struct v4l2_subdev_selection *sel = arg; | |
293 | ||
294 | if (sel->which != V4L2_SUBDEV_FORMAT_TRY && | |
295 | sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
296 | return -EINVAL; | |
297 | ||
298 | if (sel->pad >= sd->entity.num_pads) | |
299 | return -EINVAL; | |
300 | ||
301 | return v4l2_subdev_call( | |
302 | sd, pad, get_selection, subdev_fh, sel); | |
303 | } | |
304 | ||
305 | case VIDIOC_SUBDEV_S_SELECTION: { | |
306 | struct v4l2_subdev_selection *sel = arg; | |
307 | ||
308 | if (sel->which != V4L2_SUBDEV_FORMAT_TRY && | |
309 | sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
310 | return -EINVAL; | |
311 | ||
312 | if (sel->pad >= sd->entity.num_pads) | |
313 | return -EINVAL; | |
314 | ||
315 | return v4l2_subdev_call( | |
316 | sd, pad, set_selection, subdev_fh, sel); | |
317 | } | |
333c8b97 | 318 | #endif |
2096a5dc | 319 | default: |
c30b46e5 | 320 | return v4l2_subdev_call(sd, core, ioctl, cmd, arg); |
2096a5dc LP |
321 | } |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
326 | static long subdev_ioctl(struct file *file, unsigned int cmd, | |
327 | unsigned long arg) | |
328 | { | |
329 | return video_usercopy(file, cmd, arg, subdev_do_ioctl); | |
330 | } | |
331 | ||
02adb1cc SA |
332 | static unsigned int subdev_poll(struct file *file, poll_table *wait) |
333 | { | |
334 | struct video_device *vdev = video_devdata(file); | |
335 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
336 | struct v4l2_fh *fh = file->private_data; | |
337 | ||
338 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) | |
339 | return POLLERR; | |
340 | ||
523f46d6 | 341 | poll_wait(file, &fh->wait, wait); |
02adb1cc SA |
342 | |
343 | if (v4l2_event_pending(fh)) | |
344 | return POLLPRI; | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
2096a5dc LP |
349 | const struct v4l2_file_operations v4l2_subdev_fops = { |
350 | .owner = THIS_MODULE, | |
351 | .open = subdev_open, | |
352 | .unlocked_ioctl = subdev_ioctl, | |
353 | .release = subdev_close, | |
02adb1cc | 354 | .poll = subdev_poll, |
2096a5dc | 355 | }; |
3dd5ee08 LP |
356 | |
357 | void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) | |
358 | { | |
359 | INIT_LIST_HEAD(&sd->list); | |
360 | BUG_ON(!ops); | |
361 | sd->ops = ops; | |
362 | sd->v4l2_dev = NULL; | |
363 | sd->flags = 0; | |
364 | sd->name[0] = '\0'; | |
365 | sd->grp_id = 0; | |
366 | sd->dev_priv = NULL; | |
367 | sd->host_priv = NULL; | |
61f5db54 LP |
368 | #if defined(CONFIG_MEDIA_CONTROLLER) |
369 | sd->entity.name = sd->name; | |
370 | sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV; | |
371 | #endif | |
3dd5ee08 LP |
372 | } |
373 | EXPORT_SYMBOL(v4l2_subdev_init); |