[media] v4l: subdev: Events support
[pandora-kernel.git] / drivers / media / video / v4l2-subdev.c
1 /*
2  * V4L2 sub-device
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  *
6  * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7  *          Sakari Ailus <sakari.ailus@iki.fi>
8  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <linux/ioctl.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
26 #include <linux/videodev2.h>
27
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-fh.h>
32 #include <media/v4l2-event.h>
33
34 static int subdev_open(struct file *file)
35 {
36         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
62         return 0;
63
64 err:
65         if (vfh != NULL) {
66                 v4l2_fh_exit(vfh);
67                 kfree(vfh);
68         }
69
70         return ret;
71 }
72
73 static int subdev_close(struct file *file)
74 {
75         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
83         return 0;
84 }
85
86 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
87 {
88         struct video_device *vdev = video_devdata(file);
89         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
90         struct v4l2_fh *fh = file->private_data;
91
92         switch (cmd) {
93         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
114         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
126         default:
127                 return -ENOIOCTLCMD;
128         }
129
130         return 0;
131 }
132
133 static 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
139 static 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
156 const struct v4l2_file_operations v4l2_subdev_fops = {
157         .owner = THIS_MODULE,
158         .open = subdev_open,
159         .unlocked_ioctl = subdev_ioctl,
160         .release = subdev_close,
161         .poll = subdev_poll,
162 };
163
164 void 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 }
176 EXPORT_SYMBOL(v4l2_subdev_init);