blob: 0c67f5a278fb9c7449f2bb890966dd85cf83b71d [file] [log] [blame]
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03001/*
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03002 * V4L2 sub-device
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03003 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03004 * Copyright (C) 2010 Nokia Corporation
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03005 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03006 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03008 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -03009 * 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 Pinchart2096a5d2009-12-09 08:38:49 -030012 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -030013 * 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 Pinchart2096a5d2009-12-09 08:38:49 -030017 *
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -030018 * 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 Pinchart2096a5d2009-12-09 08:38:49 -030021 */
22
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030023#include <linux/ioctl.h>
Sakari Ailus02adb1c2010-03-03 12:49:38 -030024#include <linux/slab.h>
25#include <linux/types.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030026#include <linux/videodev2.h>
27
Laurent Pinchartea8aa432009-12-09 08:39:54 -030028#include <media/v4l2-ctrls.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030029#include <media/v4l2-device.h>
30#include <media/v4l2-ioctl.h>
Sakari Ailus02adb1c2010-03-03 12:49:38 -030031#include <media/v4l2-fh.h>
32#include <media/v4l2-event.h>
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030033
34static int subdev_open(struct file *file)
35{
Sakari Ailus02adb1c2010-03-03 12:49:38 -030036 struct video_device *vdev = video_devdata(file);
37 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
38 struct v4l2_fh *vfh;
39 int ret;
40
41 if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) {
42 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
43 if (vfh == NULL)
44 return -ENOMEM;
45
46 ret = v4l2_fh_init(vfh, vdev);
47 if (ret)
48 goto err;
49
50 ret = v4l2_event_init(vfh);
51 if (ret)
52 goto err;
53
54 ret = v4l2_event_alloc(vfh, sd->nevents);
55 if (ret)
56 goto err;
57
58 v4l2_fh_add(vfh);
59 file->private_data = vfh;
60 }
61
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030062 return 0;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030063
64err:
65 if (vfh != NULL) {
66 v4l2_fh_exit(vfh);
67 kfree(vfh);
68 }
69
70 return ret;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030071}
72
73static int subdev_close(struct file *file)
74{
Sakari Ailus02adb1c2010-03-03 12:49:38 -030075 struct v4l2_fh *vfh = file->private_data;
76
77 if (vfh != NULL) {
78 v4l2_fh_del(vfh);
79 v4l2_fh_exit(vfh);
80 kfree(vfh);
81 }
82
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030083 return 0;
84}
85
86static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
87{
Laurent Pinchartea8aa432009-12-09 08:39:54 -030088 struct video_device *vdev = video_devdata(file);
89 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Sakari Ailus02adb1c2010-03-03 12:49:38 -030090 struct v4l2_fh *fh = file->private_data;
Laurent Pinchartea8aa432009-12-09 08:39:54 -030091
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030092 switch (cmd) {
Laurent Pinchartea8aa432009-12-09 08:39:54 -030093 case VIDIOC_QUERYCTRL:
94 return v4l2_subdev_queryctrl(sd, arg);
95
96 case VIDIOC_QUERYMENU:
97 return v4l2_subdev_querymenu(sd, arg);
98
99 case VIDIOC_G_CTRL:
100 return v4l2_subdev_g_ctrl(sd, arg);
101
102 case VIDIOC_S_CTRL:
103 return v4l2_subdev_s_ctrl(sd, arg);
104
105 case VIDIOC_G_EXT_CTRLS:
106 return v4l2_subdev_g_ext_ctrls(sd, arg);
107
108 case VIDIOC_S_EXT_CTRLS:
109 return v4l2_subdev_s_ext_ctrls(sd, arg);
110
111 case VIDIOC_TRY_EXT_CTRLS:
112 return v4l2_subdev_try_ext_ctrls(sd, arg);
113
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300114 case VIDIOC_DQEVENT:
115 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
116 return -ENOIOCTLCMD;
117
118 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
119
120 case VIDIOC_SUBSCRIBE_EVENT:
121 return v4l2_subdev_call(sd, core, subscribe_event, fh, arg);
122
123 case VIDIOC_UNSUBSCRIBE_EVENT:
124 return v4l2_subdev_call(sd, core, unsubscribe_event, fh, arg);
125
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300126 default:
127 return -ENOIOCTLCMD;
128 }
129
130 return 0;
131}
132
133static long subdev_ioctl(struct file *file, unsigned int cmd,
134 unsigned long arg)
135{
136 return video_usercopy(file, cmd, arg, subdev_do_ioctl);
137}
138
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300139static unsigned int subdev_poll(struct file *file, poll_table *wait)
140{
141 struct video_device *vdev = video_devdata(file);
142 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
143 struct v4l2_fh *fh = file->private_data;
144
145 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
146 return POLLERR;
147
148 poll_wait(file, &fh->events->wait, wait);
149
150 if (v4l2_event_pending(fh))
151 return POLLPRI;
152
153 return 0;
154}
155
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300156const struct v4l2_file_operations v4l2_subdev_fops = {
157 .owner = THIS_MODULE,
158 .open = subdev_open,
159 .unlocked_ioctl = subdev_ioctl,
160 .release = subdev_close,
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300161 .poll = subdev_poll,
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300162};
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300163
164void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
165{
166 INIT_LIST_HEAD(&sd->list);
167 BUG_ON(!ops);
168 sd->ops = ops;
169 sd->v4l2_dev = NULL;
170 sd->flags = 0;
171 sd->name[0] = '\0';
172 sd->grp_id = 0;
173 sd->dev_priv = NULL;
174 sd->host_priv = NULL;
175}
176EXPORT_SYMBOL(v4l2_subdev_init);