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 | 95 | #if defined(CONFIG_MEDIA_CONTROLLER) |
5164d6aa | 96 | media_entity_put(entity); |
f0beea8f | 97 | #endif |
7cd5a16b SV |
98 | v4l2_fh_del(&subdev_fh->vfh); |
99 | v4l2_fh_exit(&subdev_fh->vfh); | |
100 | subdev_fh_free(subdev_fh); | |
101 | kfree(subdev_fh); | |
02adb1cc SA |
102 | |
103 | return ret; | |
2096a5dc LP |
104 | } |
105 | ||
106 | static int subdev_close(struct file *file) | |
107 | { | |
61f5db54 LP |
108 | struct video_device *vdev = video_devdata(file); |
109 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
02adb1cc | 110 | struct v4l2_fh *vfh = file->private_data; |
7cd5a16b | 111 | struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); |
02adb1cc | 112 | |
f0beea8f LP |
113 | if (sd->internal_ops && sd->internal_ops->close) |
114 | sd->internal_ops->close(sd, subdev_fh); | |
61f5db54 LP |
115 | #if defined(CONFIG_MEDIA_CONTROLLER) |
116 | if (sd->v4l2_dev->mdev) | |
117 | media_entity_put(&sd->entity); | |
118 | #endif | |
7cd5a16b SV |
119 | v4l2_fh_del(vfh); |
120 | v4l2_fh_exit(vfh); | |
121 | subdev_fh_free(subdev_fh); | |
122 | kfree(subdev_fh); | |
123 | file->private_data = NULL; | |
02adb1cc | 124 | |
2096a5dc LP |
125 | return 0; |
126 | } | |
127 | ||
d352bcc9 | 128 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
b225e398 SA |
129 | static int check_format(struct v4l2_subdev *sd, |
130 | struct v4l2_subdev_format *format) | |
131 | { | |
132 | if (format->which != V4L2_SUBDEV_FORMAT_TRY && | |
133 | format->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
134 | return -EINVAL; | |
135 | ||
136 | if (format->pad >= sd->entity.num_pads) | |
137 | return -EINVAL; | |
138 | ||
139 | return 0; | |
140 | } | |
141 | ||
142 | static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop) | |
143 | { | |
144 | if (crop->which != V4L2_SUBDEV_FORMAT_TRY && | |
145 | crop->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
146 | return -EINVAL; | |
147 | ||
148 | if (crop->pad >= sd->entity.num_pads) | |
149 | return -EINVAL; | |
150 | ||
151 | return 0; | |
152 | } | |
153 | ||
154 | static int check_selection(struct v4l2_subdev *sd, | |
155 | struct v4l2_subdev_selection *sel) | |
156 | { | |
157 | if (sel->which != V4L2_SUBDEV_FORMAT_TRY && | |
158 | sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
159 | return -EINVAL; | |
160 | ||
161 | if (sel->pad >= sd->entity.num_pads) | |
162 | return -EINVAL; | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
167 | static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) | |
168 | { | |
169 | if (edid->pad >= sd->entity.num_pads) | |
170 | return -EINVAL; | |
171 | ||
172 | if (edid->blocks && edid->edid == NULL) | |
173 | return -EINVAL; | |
174 | ||
175 | return 0; | |
176 | } | |
d352bcc9 | 177 | #endif |
b225e398 | 178 | |
2096a5dc LP |
179 | static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg) |
180 | { | |
ea8aa434 LP |
181 | struct video_device *vdev = video_devdata(file); |
182 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
7cd5a16b | 183 | struct v4l2_fh *vfh = file->private_data; |
333c8b97 LP |
184 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
185 | struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); | |
b225e398 | 186 | int rval; |
d352bcc9 | 187 | #endif |
ea8aa434 | 188 | |
2096a5dc | 189 | switch (cmd) { |
ea8aa434 | 190 | case VIDIOC_QUERYCTRL: |
18d171ba | 191 | return v4l2_queryctrl(vfh->ctrl_handler, arg); |
ea8aa434 | 192 | |
e6bee368 HV |
193 | case VIDIOC_QUERY_EXT_CTRL: |
194 | return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg); | |
195 | ||
ea8aa434 | 196 | case VIDIOC_QUERYMENU: |
18d171ba | 197 | return v4l2_querymenu(vfh->ctrl_handler, arg); |
ea8aa434 LP |
198 | |
199 | case VIDIOC_G_CTRL: | |
18d171ba | 200 | return v4l2_g_ctrl(vfh->ctrl_handler, arg); |
ea8aa434 LP |
201 | |
202 | case VIDIOC_S_CTRL: | |
ab892bac | 203 | return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); |
ea8aa434 LP |
204 | |
205 | case VIDIOC_G_EXT_CTRLS: | |
18d171ba | 206 | return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg); |
ea8aa434 LP |
207 | |
208 | case VIDIOC_S_EXT_CTRLS: | |
ab892bac | 209 | return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg); |
ea8aa434 LP |
210 | |
211 | case VIDIOC_TRY_EXT_CTRLS: | |
18d171ba | 212 | return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg); |
ea8aa434 | 213 | |
02adb1cc SA |
214 | case VIDIOC_DQEVENT: |
215 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) | |
216 | return -ENOIOCTLCMD; | |
217 | ||
7cd5a16b | 218 | return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); |
02adb1cc SA |
219 | |
220 | case VIDIOC_SUBSCRIBE_EVENT: | |
7cd5a16b | 221 | return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); |
02adb1cc SA |
222 | |
223 | case VIDIOC_UNSUBSCRIBE_EVENT: | |
7cd5a16b | 224 | return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); |
69967a71 MH |
225 | |
226 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
227 | case VIDIOC_DBG_G_REGISTER: | |
228 | { | |
229 | struct v4l2_dbg_register *p = arg; | |
230 | ||
231 | if (!capable(CAP_SYS_ADMIN)) | |
232 | return -EPERM; | |
233 | return v4l2_subdev_call(sd, core, g_register, p); | |
234 | } | |
235 | case VIDIOC_DBG_S_REGISTER: | |
236 | { | |
237 | struct v4l2_dbg_register *p = arg; | |
238 | ||
239 | if (!capable(CAP_SYS_ADMIN)) | |
240 | return -EPERM; | |
241 | return v4l2_subdev_call(sd, core, s_register, p); | |
242 | } | |
243 | #endif | |
bcd158de | 244 | |
42194e72 HV |
245 | case VIDIOC_LOG_STATUS: { |
246 | int ret; | |
247 | ||
248 | pr_info("%s: ================= START STATUS =================\n", | |
249 | sd->name); | |
250 | ret = v4l2_subdev_call(sd, core, log_status); | |
251 | pr_info("%s: ================== END STATUS ==================\n", | |
252 | sd->name); | |
253 | return ret; | |
254 | } | |
bcd158de | 255 | |
333c8b97 LP |
256 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
257 | case VIDIOC_SUBDEV_G_FMT: { | |
258 | struct v4l2_subdev_format *format = arg; | |
259 | ||
b225e398 SA |
260 | rval = check_format(sd, format); |
261 | if (rval) | |
262 | return rval; | |
333c8b97 | 263 | |
f7234138 | 264 | return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format); |
333c8b97 LP |
265 | } |
266 | ||
267 | case VIDIOC_SUBDEV_S_FMT: { | |
268 | struct v4l2_subdev_format *format = arg; | |
269 | ||
b225e398 SA |
270 | rval = check_format(sd, format); |
271 | if (rval) | |
272 | return rval; | |
02adb1cc | 273 | |
f7234138 | 274 | return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format); |
333c8b97 LP |
275 | } |
276 | ||
f6a5cb1b AK |
277 | case VIDIOC_SUBDEV_G_CROP: { |
278 | struct v4l2_subdev_crop *crop = arg; | |
5b9d770f | 279 | struct v4l2_subdev_selection sel; |
f6a5cb1b | 280 | |
b225e398 SA |
281 | rval = check_crop(sd, crop); |
282 | if (rval) | |
283 | return rval; | |
f6a5cb1b | 284 | |
5b9d770f SA |
285 | memset(&sel, 0, sizeof(sel)); |
286 | sel.which = crop->which; | |
287 | sel.pad = crop->pad; | |
5689b288 | 288 | sel.target = V4L2_SEL_TGT_CROP; |
5b9d770f SA |
289 | |
290 | rval = v4l2_subdev_call( | |
f7234138 | 291 | sd, pad, get_selection, subdev_fh->pad, &sel); |
5b9d770f SA |
292 | |
293 | crop->rect = sel.r; | |
294 | ||
295 | return rval; | |
f6a5cb1b AK |
296 | } |
297 | ||
298 | case VIDIOC_SUBDEV_S_CROP: { | |
299 | struct v4l2_subdev_crop *crop = arg; | |
5b9d770f | 300 | struct v4l2_subdev_selection sel; |
f6a5cb1b | 301 | |
b225e398 SA |
302 | rval = check_crop(sd, crop); |
303 | if (rval) | |
304 | return rval; | |
f6a5cb1b | 305 | |
5b9d770f SA |
306 | memset(&sel, 0, sizeof(sel)); |
307 | sel.which = crop->which; | |
308 | sel.pad = crop->pad; | |
5689b288 | 309 | sel.target = V4L2_SEL_TGT_CROP; |
5b9d770f SA |
310 | sel.r = crop->rect; |
311 | ||
312 | rval = v4l2_subdev_call( | |
f7234138 | 313 | sd, pad, set_selection, subdev_fh->pad, &sel); |
5b9d770f SA |
314 | |
315 | crop->rect = sel.r; | |
316 | ||
317 | return rval; | |
f6a5cb1b AK |
318 | } |
319 | ||
333c8b97 LP |
320 | case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { |
321 | struct v4l2_subdev_mbus_code_enum *code = arg; | |
322 | ||
20058f90 HV |
323 | if (code->which != V4L2_SUBDEV_FORMAT_TRY && |
324 | code->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
325 | return -EINVAL; | |
326 | ||
333c8b97 LP |
327 | if (code->pad >= sd->entity.num_pads) |
328 | return -EINVAL; | |
329 | ||
f7234138 | 330 | return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad, |
333c8b97 LP |
331 | code); |
332 | } | |
333 | ||
334 | case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { | |
335 | struct v4l2_subdev_frame_size_enum *fse = arg; | |
336 | ||
20058f90 HV |
337 | if (fse->which != V4L2_SUBDEV_FORMAT_TRY && |
338 | fse->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
339 | return -EINVAL; | |
340 | ||
333c8b97 LP |
341 | if (fse->pad >= sd->entity.num_pads) |
342 | return -EINVAL; | |
343 | ||
f7234138 | 344 | return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad, |
333c8b97 LP |
345 | fse); |
346 | } | |
35c3017a | 347 | |
743e1837 SA |
348 | case VIDIOC_SUBDEV_G_FRAME_INTERVAL: { |
349 | struct v4l2_subdev_frame_interval *fi = arg; | |
350 | ||
351 | if (fi->pad >= sd->entity.num_pads) | |
352 | return -EINVAL; | |
353 | ||
35c3017a | 354 | return v4l2_subdev_call(sd, video, g_frame_interval, arg); |
743e1837 SA |
355 | } |
356 | ||
357 | case VIDIOC_SUBDEV_S_FRAME_INTERVAL: { | |
358 | struct v4l2_subdev_frame_interval *fi = arg; | |
359 | ||
360 | if (fi->pad >= sd->entity.num_pads) | |
361 | return -EINVAL; | |
35c3017a | 362 | |
35c3017a | 363 | return v4l2_subdev_call(sd, video, s_frame_interval, arg); |
743e1837 | 364 | } |
35c3017a LP |
365 | |
366 | case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { | |
367 | struct v4l2_subdev_frame_interval_enum *fie = arg; | |
368 | ||
20058f90 HV |
369 | if (fie->which != V4L2_SUBDEV_FORMAT_TRY && |
370 | fie->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
371 | return -EINVAL; | |
372 | ||
35c3017a LP |
373 | if (fie->pad >= sd->entity.num_pads) |
374 | return -EINVAL; | |
375 | ||
f7234138 | 376 | return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad, |
35c3017a LP |
377 | fie); |
378 | } | |
ae184cda SA |
379 | |
380 | case VIDIOC_SUBDEV_G_SELECTION: { | |
381 | struct v4l2_subdev_selection *sel = arg; | |
382 | ||
b225e398 SA |
383 | rval = check_selection(sd, sel); |
384 | if (rval) | |
385 | return rval; | |
ae184cda SA |
386 | |
387 | return v4l2_subdev_call( | |
f7234138 | 388 | sd, pad, get_selection, subdev_fh->pad, sel); |
ae184cda SA |
389 | } |
390 | ||
391 | case VIDIOC_SUBDEV_S_SELECTION: { | |
392 | struct v4l2_subdev_selection *sel = arg; | |
393 | ||
b225e398 SA |
394 | rval = check_selection(sd, sel); |
395 | if (rval) | |
396 | return rval; | |
ae184cda SA |
397 | |
398 | return v4l2_subdev_call( | |
f7234138 | 399 | sd, pad, set_selection, subdev_fh->pad, sel); |
ae184cda | 400 | } |
ed45ce2c | 401 | |
f2e90847 LP |
402 | case VIDIOC_G_EDID: { |
403 | struct v4l2_subdev_edid *edid = arg; | |
ed45ce2c | 404 | |
b225e398 SA |
405 | rval = check_edid(sd, edid); |
406 | if (rval) | |
407 | return rval; | |
f2e90847 LP |
408 | |
409 | return v4l2_subdev_call(sd, pad, get_edid, edid); | |
410 | } | |
411 | ||
412 | case VIDIOC_S_EDID: { | |
413 | struct v4l2_subdev_edid *edid = arg; | |
414 | ||
b225e398 SA |
415 | rval = check_edid(sd, edid); |
416 | if (rval) | |
417 | return rval; | |
f2e90847 LP |
418 | |
419 | return v4l2_subdev_call(sd, pad, set_edid, edid); | |
420 | } | |
9cfd65e8 LP |
421 | |
422 | case VIDIOC_SUBDEV_DV_TIMINGS_CAP: { | |
423 | struct v4l2_dv_timings_cap *cap = arg; | |
424 | ||
425 | if (cap->pad >= sd->entity.num_pads) | |
426 | return -EINVAL; | |
427 | ||
428 | return v4l2_subdev_call(sd, pad, dv_timings_cap, cap); | |
429 | } | |
430 | ||
431 | case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: { | |
432 | struct v4l2_enum_dv_timings *dvt = arg; | |
433 | ||
434 | if (dvt->pad >= sd->entity.num_pads) | |
435 | return -EINVAL; | |
436 | ||
437 | return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt); | |
438 | } | |
439 | ||
440 | case VIDIOC_SUBDEV_QUERY_DV_TIMINGS: | |
441 | return v4l2_subdev_call(sd, video, query_dv_timings, arg); | |
442 | ||
443 | case VIDIOC_SUBDEV_G_DV_TIMINGS: | |
444 | return v4l2_subdev_call(sd, video, g_dv_timings, arg); | |
445 | ||
446 | case VIDIOC_SUBDEV_S_DV_TIMINGS: | |
447 | return v4l2_subdev_call(sd, video, s_dv_timings, arg); | |
333c8b97 | 448 | #endif |
2096a5dc | 449 | default: |
c30b46e5 | 450 | return v4l2_subdev_call(sd, core, ioctl, cmd, arg); |
2096a5dc LP |
451 | } |
452 | ||
453 | return 0; | |
454 | } | |
455 | ||
456 | static long subdev_ioctl(struct file *file, unsigned int cmd, | |
457 | unsigned long arg) | |
458 | { | |
459 | return video_usercopy(file, cmd, arg, subdev_do_ioctl); | |
460 | } | |
461 | ||
ab58a301 HV |
462 | #ifdef CONFIG_COMPAT |
463 | static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, | |
464 | unsigned long arg) | |
465 | { | |
466 | struct video_device *vdev = video_devdata(file); | |
467 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
468 | ||
469 | return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg); | |
470 | } | |
471 | #endif | |
472 | ||
02adb1cc SA |
473 | static unsigned int subdev_poll(struct file *file, poll_table *wait) |
474 | { | |
475 | struct video_device *vdev = video_devdata(file); | |
476 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); | |
477 | struct v4l2_fh *fh = file->private_data; | |
478 | ||
479 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) | |
480 | return POLLERR; | |
481 | ||
523f46d6 | 482 | poll_wait(file, &fh->wait, wait); |
02adb1cc SA |
483 | |
484 | if (v4l2_event_pending(fh)) | |
485 | return POLLPRI; | |
486 | ||
487 | return 0; | |
488 | } | |
489 | ||
2096a5dc LP |
490 | const struct v4l2_file_operations v4l2_subdev_fops = { |
491 | .owner = THIS_MODULE, | |
492 | .open = subdev_open, | |
493 | .unlocked_ioctl = subdev_ioctl, | |
ab58a301 HV |
494 | #ifdef CONFIG_COMPAT |
495 | .compat_ioctl32 = subdev_compat_ioctl32, | |
496 | #endif | |
2096a5dc | 497 | .release = subdev_close, |
02adb1cc | 498 | .poll = subdev_poll, |
2096a5dc | 499 | }; |
3dd5ee08 | 500 | |
8227c92b SA |
501 | #ifdef CONFIG_MEDIA_CONTROLLER |
502 | int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd, | |
503 | struct media_link *link, | |
504 | struct v4l2_subdev_format *source_fmt, | |
505 | struct v4l2_subdev_format *sink_fmt) | |
506 | { | |
24acf8b2 | 507 | /* The width, height and code must match. */ |
8227c92b SA |
508 | if (source_fmt->format.width != sink_fmt->format.width |
509 | || source_fmt->format.height != sink_fmt->format.height | |
510 | || source_fmt->format.code != sink_fmt->format.code) | |
511 | return -EINVAL; | |
512 | ||
24acf8b2 LP |
513 | /* The field order must match, or the sink field order must be NONE |
514 | * to support interlaced hardware connected to bridges that support | |
515 | * progressive formats only. | |
516 | */ | |
517 | if (source_fmt->format.field != sink_fmt->format.field && | |
518 | sink_fmt->format.field != V4L2_FIELD_NONE) | |
519 | return -EINVAL; | |
520 | ||
8227c92b SA |
521 | return 0; |
522 | } | |
523 | EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default); | |
524 | ||
525 | static int | |
526 | v4l2_subdev_link_validate_get_format(struct media_pad *pad, | |
527 | struct v4l2_subdev_format *fmt) | |
528 | { | |
864a1212 LP |
529 | if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) { |
530 | struct v4l2_subdev *sd = | |
531 | media_entity_to_v4l2_subdev(pad->entity); | |
532 | ||
8227c92b SA |
533 | fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE; |
534 | fmt->pad = pad->index; | |
864a1212 | 535 | return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt); |
8227c92b | 536 | } |
864a1212 LP |
537 | |
538 | WARN(pad->entity->type != MEDIA_ENT_T_DEVNODE_V4L, | |
539 | "Driver bug! Wrong media entity type 0x%08x, entity %s\n", | |
540 | pad->entity->type, pad->entity->name); | |
541 | ||
542 | return -EINVAL; | |
8227c92b SA |
543 | } |
544 | ||
545 | int v4l2_subdev_link_validate(struct media_link *link) | |
546 | { | |
547 | struct v4l2_subdev *sink; | |
548 | struct v4l2_subdev_format sink_fmt, source_fmt; | |
549 | int rval; | |
550 | ||
551 | rval = v4l2_subdev_link_validate_get_format( | |
552 | link->source, &source_fmt); | |
553 | if (rval < 0) | |
554 | return 0; | |
555 | ||
556 | rval = v4l2_subdev_link_validate_get_format( | |
557 | link->sink, &sink_fmt); | |
558 | if (rval < 0) | |
559 | return 0; | |
560 | ||
561 | sink = media_entity_to_v4l2_subdev(link->sink->entity); | |
562 | ||
563 | rval = v4l2_subdev_call(sink, pad, link_validate, link, | |
564 | &source_fmt, &sink_fmt); | |
565 | if (rval != -ENOIOCTLCMD) | |
566 | return rval; | |
567 | ||
568 | return v4l2_subdev_link_validate_default( | |
569 | sink, link, &source_fmt, &sink_fmt); | |
570 | } | |
571 | EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate); | |
572 | #endif /* CONFIG_MEDIA_CONTROLLER */ | |
573 | ||
3dd5ee08 LP |
574 | void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) |
575 | { | |
576 | INIT_LIST_HEAD(&sd->list); | |
577 | BUG_ON(!ops); | |
578 | sd->ops = ops; | |
579 | sd->v4l2_dev = NULL; | |
580 | sd->flags = 0; | |
581 | sd->name[0] = '\0'; | |
582 | sd->grp_id = 0; | |
583 | sd->dev_priv = NULL; | |
584 | sd->host_priv = NULL; | |
61f5db54 LP |
585 | #if defined(CONFIG_MEDIA_CONTROLLER) |
586 | sd->entity.name = sd->name; | |
587 | sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV; | |
588 | #endif | |
3dd5ee08 LP |
589 | } |
590 | EXPORT_SYMBOL(v4l2_subdev_init); |