[media] v4l: Make v4l2_subdev inherit from media_entity
[linux-2.6.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 #if defined(CONFIG_MEDIA_CONTROLLER)
39         struct media_entity *entity;
40 #endif
41         struct v4l2_fh *vfh = NULL;
42         int ret;
43
44         if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) {
45                 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
46                 if (vfh == NULL)
47                         return -ENOMEM;
48
49                 ret = v4l2_fh_init(vfh, vdev);
50                 if (ret)
51                         goto err;
52
53                 ret = v4l2_event_init(vfh);
54                 if (ret)
55                         goto err;
56
57                 ret = v4l2_event_alloc(vfh, sd->nevents);
58                 if (ret)
59                         goto err;
60
61                 v4l2_fh_add(vfh);
62                 file->private_data = vfh;
63         }
64 #if defined(CONFIG_MEDIA_CONTROLLER)
65         if (sd->v4l2_dev->mdev) {
66                 entity = media_entity_get(&sd->entity);
67                 if (!entity) {
68                         ret = -EBUSY;
69                         goto err;
70                 }
71         }
72 #endif
73         return 0;
74
75 err:
76         if (vfh != NULL) {
77                 v4l2_fh_del(vfh);
78                 v4l2_fh_exit(vfh);
79                 kfree(vfh);
80         }
81
82         return ret;
83 }
84
85 static int subdev_close(struct file *file)
86 {
87 #if defined(CONFIG_MEDIA_CONTROLLER)
88         struct video_device *vdev = video_devdata(file);
89         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
90 #endif
91         struct v4l2_fh *vfh = file->private_data;
92
93 #if defined(CONFIG_MEDIA_CONTROLLER)
94         if (sd->v4l2_dev->mdev)
95                 media_entity_put(&sd->entity);
96 #endif
97         if (vfh != NULL) {
98                 v4l2_fh_del(vfh);
99                 v4l2_fh_exit(vfh);
100                 kfree(vfh);
101         }
102
103         return 0;
104 }
105
106 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
107 {
108         struct video_device *vdev = video_devdata(file);
109         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
110         struct v4l2_fh *fh = file->private_data;
111
112         switch (cmd) {
113         case VIDIOC_QUERYCTRL:
114                 return v4l2_subdev_queryctrl(sd, arg);
115
116         case VIDIOC_QUERYMENU:
117                 return v4l2_subdev_querymenu(sd, arg);
118
119         case VIDIOC_G_CTRL:
120                 return v4l2_subdev_g_ctrl(sd, arg);
121
122         case VIDIOC_S_CTRL:
123                 return v4l2_subdev_s_ctrl(sd, arg);
124
125         case VIDIOC_G_EXT_CTRLS:
126                 return v4l2_subdev_g_ext_ctrls(sd, arg);
127
128         case VIDIOC_S_EXT_CTRLS:
129                 return v4l2_subdev_s_ext_ctrls(sd, arg);
130
131         case VIDIOC_TRY_EXT_CTRLS:
132                 return v4l2_subdev_try_ext_ctrls(sd, arg);
133
134         case VIDIOC_DQEVENT:
135                 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
136                         return -ENOIOCTLCMD;
137
138                 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
139
140         case VIDIOC_SUBSCRIBE_EVENT:
141                 return v4l2_subdev_call(sd, core, subscribe_event, fh, arg);
142
143         case VIDIOC_UNSUBSCRIBE_EVENT:
144                 return v4l2_subdev_call(sd, core, unsubscribe_event, fh, arg);
145
146         default:
147                 return -ENOIOCTLCMD;
148         }
149
150         return 0;
151 }
152
153 static long subdev_ioctl(struct file *file, unsigned int cmd,
154         unsigned long arg)
155 {
156         return video_usercopy(file, cmd, arg, subdev_do_ioctl);
157 }
158
159 static unsigned int subdev_poll(struct file *file, poll_table *wait)
160 {
161         struct video_device *vdev = video_devdata(file);
162         struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
163         struct v4l2_fh *fh = file->private_data;
164
165         if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
166                 return POLLERR;
167
168         poll_wait(file, &fh->events->wait, wait);
169
170         if (v4l2_event_pending(fh))
171                 return POLLPRI;
172
173         return 0;
174 }
175
176 const struct v4l2_file_operations v4l2_subdev_fops = {
177         .owner = THIS_MODULE,
178         .open = subdev_open,
179         .unlocked_ioctl = subdev_ioctl,
180         .release = subdev_close,
181         .poll = subdev_poll,
182 };
183
184 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
185 {
186         INIT_LIST_HEAD(&sd->list);
187         BUG_ON(!ops);
188         sd->ops = ops;
189         sd->v4l2_dev = NULL;
190         sd->flags = 0;
191         sd->name[0] = '\0';
192         sd->grp_id = 0;
193         sd->dev_priv = NULL;
194         sd->host_priv = NULL;
195 #if defined(CONFIG_MEDIA_CONTROLLER)
196         sd->entity.name = sd->name;
197         sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
198 #endif
199 }
200 EXPORT_SYMBOL(v4l2_subdev_init);