blob: 85573638aa1ef45acc1c47c9a7048cb5fdd52b2a [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);
Laurent Pinchart61f5db52009-12-09 08:40:08 -030038#if defined(CONFIG_MEDIA_CONTROLLER)
39 struct media_entity *entity;
40#endif
41 struct v4l2_fh *vfh = NULL;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030042 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 }
Laurent Pinchart61f5db52009-12-09 08:40:08 -030064#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
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030073 return 0;
Sakari Ailus02adb1c2010-03-03 12:49:38 -030074
75err:
76 if (vfh != NULL) {
Laurent Pinchart61f5db52009-12-09 08:40:08 -030077 v4l2_fh_del(vfh);
Sakari Ailus02adb1c2010-03-03 12:49:38 -030078 v4l2_fh_exit(vfh);
79 kfree(vfh);
80 }
81
82 return ret;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -030083}
84
85static int subdev_close(struct file *file)
86{
Laurent Pinchart61f5db52009-12-09 08:40:08 -030087#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
Sakari Ailus02adb1c2010-03-03 12:49:38 -030091 struct v4l2_fh *vfh = file->private_data;
92
Laurent Pinchart61f5db52009-12-09 08:40:08 -030093#if defined(CONFIG_MEDIA_CONTROLLER)
94 if (sd->v4l2_dev->mdev)
95 media_entity_put(&sd->entity);
96#endif
Sakari Ailus02adb1c2010-03-03 12:49:38 -030097 if (vfh != NULL) {
98 v4l2_fh_del(vfh);
99 v4l2_fh_exit(vfh);
100 kfree(vfh);
101 }
102
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300103 return 0;
104}
105
106static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
107{
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300108 struct video_device *vdev = video_devdata(file);
109 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300110 struct v4l2_fh *fh = file->private_data;
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300111
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300112 switch (cmd) {
Laurent Pinchartea8aa432009-12-09 08:39:54 -0300113 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
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300134 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
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300146 default:
147 return -ENOIOCTLCMD;
148 }
149
150 return 0;
151}
152
153static 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
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300159static 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
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300176const struct v4l2_file_operations v4l2_subdev_fops = {
177 .owner = THIS_MODULE,
178 .open = subdev_open,
179 .unlocked_ioctl = subdev_ioctl,
180 .release = subdev_close,
Sakari Ailus02adb1c2010-03-03 12:49:38 -0300181 .poll = subdev_poll,
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300182};
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300183
184void 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;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300195#if defined(CONFIG_MEDIA_CONTROLLER)
196 sd->entity.name = sd->name;
197 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
198#endif
Laurent Pinchart3dd5ee02009-12-09 08:38:52 -0300199}
200EXPORT_SYMBOL(v4l2_subdev_init);