Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 1 | /* |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 2 | * V4L2 sub-device |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 3 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 4 | * Copyright (C) 2010 Nokia Corporation |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 5 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 6 | * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> |
| 7 | * Sakari Ailus <sakari.ailus@iki.fi> |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 8 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 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. |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 12 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 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. |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 17 | * |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 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 |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 21 | */ |
| 22 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 23 | #include <linux/ioctl.h> |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/types.h> |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 26 | #include <linux/videodev2.h> |
| 27 | |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 28 | #include <media/v4l2-ctrls.h> |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 29 | #include <media/v4l2-device.h> |
| 30 | #include <media/v4l2-ioctl.h> |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 31 | #include <media/v4l2-fh.h> |
| 32 | #include <media/v4l2-event.h> |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 33 | |
| 34 | static int subdev_open(struct file *file) |
| 35 | { |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 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 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 62 | return 0; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 63 | |
| 64 | err: |
| 65 | if (vfh != NULL) { |
| 66 | v4l2_fh_exit(vfh); |
| 67 | kfree(vfh); |
| 68 | } |
| 69 | |
| 70 | return ret; |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static int subdev_close(struct file *file) |
| 74 | { |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 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 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg) |
| 87 | { |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 88 | struct video_device *vdev = video_devdata(file); |
| 89 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 90 | struct v4l2_fh *fh = file->private_data; |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 91 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 92 | switch (cmd) { |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 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 | |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 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 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 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 | |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 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 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 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, |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 161 | .poll = subdev_poll, |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 162 | }; |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 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); |