v4l2-subdev.c 8.0 KB
Newer Older
1
/*
2
 * V4L2 sub-device
3
 *
4
 * Copyright (C) 2010 Nokia Corporation
5
 *
6 7
 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 *	    Sakari Ailus <sakari.ailus@iki.fi>
8
 *
9 10 11
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
12
 *
13 14 15 16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
17
 *
18 19 20
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 22 23
 */

#include <linux/ioctl.h>
24 25
#include <linux/slab.h>
#include <linux/types.h>
26 27
#include <linux/videodev2.h>

28
#include <media/v4l2-ctrls.h>
29 30
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
31 32
#include <media/v4l2-fh.h>
#include <media/v4l2-event.h>
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
{
#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
	/* Allocate try format and crop in the same memory block */
	fh->try_fmt = kzalloc((sizeof(*fh->try_fmt) + sizeof(*fh->try_crop))
			      * sd->entity.num_pads, GFP_KERNEL);
	if (fh->try_fmt == NULL)
		return -ENOMEM;

	fh->try_crop = (struct v4l2_rect *)
		(fh->try_fmt + sd->entity.num_pads);
#endif
	return 0;
}

static void subdev_fh_free(struct v4l2_subdev_fh *fh)
{
#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
	kfree(fh->try_fmt);
	fh->try_fmt = NULL;
	fh->try_crop = NULL;
#endif
}

58 59
static int subdev_open(struct file *file)
{
60 61
	struct video_device *vdev = video_devdata(file);
	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
62
	struct v4l2_subdev_fh *subdev_fh;
63
#if defined(CONFIG_MEDIA_CONTROLLER)
64
	struct media_entity *entity = NULL;
65
#endif
66 67
	int ret;

68 69 70
	subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
	if (subdev_fh == NULL)
		return -ENOMEM;
71

72 73 74 75 76 77 78 79 80
	ret = subdev_fh_init(subdev_fh, sd);
	if (ret) {
		kfree(subdev_fh);
		return ret;
	}

	ret = v4l2_fh_init(&subdev_fh->vfh, vdev);
	if (ret)
		goto err;
81

82 83
	if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) {
		ret = v4l2_event_init(&subdev_fh->vfh);
84 85 86
		if (ret)
			goto err;

87
		ret = v4l2_event_alloc(&subdev_fh->vfh, sd->nevents);
88 89 90
		if (ret)
			goto err;
	}
91 92 93

	v4l2_fh_add(&subdev_fh->vfh);
	file->private_data = &subdev_fh->vfh;
94 95 96 97 98 99 100 101 102
#if defined(CONFIG_MEDIA_CONTROLLER)
	if (sd->v4l2_dev->mdev) {
		entity = media_entity_get(&sd->entity);
		if (!entity) {
			ret = -EBUSY;
			goto err;
		}
	}
#endif
103

104 105 106 107 108 109
	if (sd->internal_ops && sd->internal_ops->open) {
		ret = sd->internal_ops->open(sd, subdev_fh);
		if (ret < 0)
			goto err;
	}

110
	return 0;
111 112

err:
113 114 115 116
#if defined(CONFIG_MEDIA_CONTROLLER)
	if (entity)
		media_entity_put(entity);
#endif
117 118 119 120
	v4l2_fh_del(&subdev_fh->vfh);
	v4l2_fh_exit(&subdev_fh->vfh);
	subdev_fh_free(subdev_fh);
	kfree(subdev_fh);
121 122

	return ret;
123 124 125 126
}

static int subdev_close(struct file *file)
{
127 128
	struct video_device *vdev = video_devdata(file);
	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
129
	struct v4l2_fh *vfh = file->private_data;
130
	struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
131

132 133
	if (sd->internal_ops && sd->internal_ops->close)
		sd->internal_ops->close(sd, subdev_fh);
134 135 136 137
#if defined(CONFIG_MEDIA_CONTROLLER)
	if (sd->v4l2_dev->mdev)
		media_entity_put(&sd->entity);
#endif
138 139 140 141 142
	v4l2_fh_del(vfh);
	v4l2_fh_exit(vfh);
	subdev_fh_free(subdev_fh);
	kfree(subdev_fh);
	file->private_data = NULL;
143

144 145 146 147 148
	return 0;
}

static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
{
149 150
	struct video_device *vdev = video_devdata(file);
	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
151
	struct v4l2_fh *vfh = file->private_data;
152 153 154
#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
	struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
#endif
155

156
	switch (cmd) {
157
	case VIDIOC_QUERYCTRL:
158
		return v4l2_queryctrl(sd->ctrl_handler, arg);
159 160

	case VIDIOC_QUERYMENU:
161
		return v4l2_querymenu(sd->ctrl_handler, arg);
162 163

	case VIDIOC_G_CTRL:
164
		return v4l2_g_ctrl(sd->ctrl_handler, arg);
165 166

	case VIDIOC_S_CTRL:
167
		return v4l2_s_ctrl(sd->ctrl_handler, arg);
168 169

	case VIDIOC_G_EXT_CTRLS:
170
		return v4l2_g_ext_ctrls(sd->ctrl_handler, arg);
171 172

	case VIDIOC_S_EXT_CTRLS:
173
		return v4l2_s_ext_ctrls(sd->ctrl_handler, arg);
174 175

	case VIDIOC_TRY_EXT_CTRLS:
176
		return v4l2_try_ext_ctrls(sd->ctrl_handler, arg);
177

178 179 180 181
	case VIDIOC_DQEVENT:
		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
			return -ENOIOCTLCMD;

182
		return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
183 184

	case VIDIOC_SUBSCRIBE_EVENT:
185
		return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
186 187

	case VIDIOC_UNSUBSCRIBE_EVENT:
188
		return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
	case VIDIOC_SUBDEV_G_FMT: {
		struct v4l2_subdev_format *format = arg;

		if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
		    format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
			return -EINVAL;

		if (format->pad >= sd->entity.num_pads)
			return -EINVAL;

		return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
	}

	case VIDIOC_SUBDEV_S_FMT: {
		struct v4l2_subdev_format *format = arg;

		if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
		    format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
			return -EINVAL;

		if (format->pad >= sd->entity.num_pads)
			return -EINVAL;
212

213 214 215
		return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
	}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	case VIDIOC_SUBDEV_G_CROP: {
		struct v4l2_subdev_crop *crop = arg;

		if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
		    crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
			return -EINVAL;

		if (crop->pad >= sd->entity.num_pads)
			return -EINVAL;

		return v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
	}

	case VIDIOC_SUBDEV_S_CROP: {
		struct v4l2_subdev_crop *crop = arg;

		if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
		    crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
			return -EINVAL;

		if (crop->pad >= sd->entity.num_pads)
			return -EINVAL;

		return v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
	}

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
		struct v4l2_subdev_mbus_code_enum *code = arg;

		if (code->pad >= sd->entity.num_pads)
			return -EINVAL;

		return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
					code);
	}

	case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
		struct v4l2_subdev_frame_size_enum *fse = arg;

		if (fse->pad >= sd->entity.num_pads)
			return -EINVAL;

		return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
					fse);
	}
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

	case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
		return v4l2_subdev_call(sd, video, g_frame_interval, arg);

	case VIDIOC_SUBDEV_S_FRAME_INTERVAL:
		return v4l2_subdev_call(sd, video, s_frame_interval, arg);

	case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
		struct v4l2_subdev_frame_interval_enum *fie = arg;

		if (fie->pad >= sd->entity.num_pads)
			return -EINVAL;

		return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
					fie);
	}
277
#endif
278
	default:
279
		return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
280 281 282 283 284 285 286 287 288 289 290
	}

	return 0;
}

static long subdev_ioctl(struct file *file, unsigned int cmd,
	unsigned long arg)
{
	return video_usercopy(file, cmd, arg, subdev_do_ioctl);
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static unsigned int subdev_poll(struct file *file, poll_table *wait)
{
	struct video_device *vdev = video_devdata(file);
	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
	struct v4l2_fh *fh = file->private_data;

	if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
		return POLLERR;

	poll_wait(file, &fh->events->wait, wait);

	if (v4l2_event_pending(fh))
		return POLLPRI;

	return 0;
}

308 309 310 311 312
const struct v4l2_file_operations v4l2_subdev_fops = {
	.owner = THIS_MODULE,
	.open = subdev_open,
	.unlocked_ioctl = subdev_ioctl,
	.release = subdev_close,
313
	.poll = subdev_poll,
314
};
315 316 317 318 319 320 321 322 323 324 325 326

void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
{
	INIT_LIST_HEAD(&sd->list);
	BUG_ON(!ops);
	sd->ops = ops;
	sd->v4l2_dev = NULL;
	sd->flags = 0;
	sd->name[0] = '\0';
	sd->grp_id = 0;
	sd->dev_priv = NULL;
	sd->host_priv = NULL;
327 328 329 330
#if defined(CONFIG_MEDIA_CONTROLLER)
	sd->entity.name = sd->name;
	sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
#endif
331 332
}
EXPORT_SYMBOL(v4l2_subdev_init);