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 | |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 34 | static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) |
| 35 | { |
| 36 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
| 37 | /* Allocate try format and crop in the same memory block */ |
| 38 | fh->try_fmt = kzalloc((sizeof(*fh->try_fmt) + sizeof(*fh->try_crop)) |
| 39 | * sd->entity.num_pads, GFP_KERNEL); |
| 40 | if (fh->try_fmt == NULL) |
| 41 | return -ENOMEM; |
| 42 | |
| 43 | fh->try_crop = (struct v4l2_rect *) |
| 44 | (fh->try_fmt + sd->entity.num_pads); |
| 45 | #endif |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static void subdev_fh_free(struct v4l2_subdev_fh *fh) |
| 50 | { |
| 51 | #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) |
| 52 | kfree(fh->try_fmt); |
| 53 | fh->try_fmt = NULL; |
| 54 | fh->try_crop = NULL; |
| 55 | #endif |
| 56 | } |
| 57 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 58 | static int subdev_open(struct file *file) |
| 59 | { |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 60 | struct video_device *vdev = video_devdata(file); |
| 61 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 62 | struct v4l2_subdev_fh *subdev_fh; |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 63 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 64 | struct media_entity *entity; |
| 65 | #endif |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 66 | int ret; |
| 67 | |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 68 | subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); |
| 69 | if (subdev_fh == NULL) |
| 70 | return -ENOMEM; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 71 | |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 72 | ret = subdev_fh_init(subdev_fh, sd); |
| 73 | if (ret) { |
| 74 | kfree(subdev_fh); |
| 75 | return ret; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 76 | } |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 77 | |
| 78 | ret = v4l2_fh_init(&subdev_fh->vfh, vdev); |
| 79 | if (ret) |
| 80 | goto err; |
| 81 | |
| 82 | if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) { |
| 83 | ret = v4l2_event_init(&subdev_fh->vfh); |
| 84 | if (ret) |
| 85 | goto err; |
| 86 | |
| 87 | ret = v4l2_event_alloc(&subdev_fh->vfh, sd->nevents); |
| 88 | if (ret) |
| 89 | goto err; |
| 90 | } |
| 91 | |
| 92 | v4l2_fh_add(&subdev_fh->vfh); |
| 93 | file->private_data = &subdev_fh->vfh; |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 94 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 95 | if (sd->v4l2_dev->mdev) { |
| 96 | entity = media_entity_get(&sd->entity); |
| 97 | if (!entity) { |
| 98 | ret = -EBUSY; |
| 99 | goto err; |
| 100 | } |
| 101 | } |
| 102 | #endif |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 103 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 104 | return 0; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 105 | |
| 106 | err: |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 107 | v4l2_fh_del(&subdev_fh->vfh); |
| 108 | v4l2_fh_exit(&subdev_fh->vfh); |
| 109 | subdev_fh_free(subdev_fh); |
| 110 | kfree(subdev_fh); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 111 | |
| 112 | return ret; |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static int subdev_close(struct file *file) |
| 116 | { |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 117 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 118 | struct video_device *vdev = video_devdata(file); |
| 119 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
| 120 | #endif |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 121 | struct v4l2_fh *vfh = file->private_data; |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 122 | struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 123 | |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 124 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 125 | if (sd->v4l2_dev->mdev) |
| 126 | media_entity_put(&sd->entity); |
| 127 | #endif |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 128 | v4l2_fh_del(vfh); |
| 129 | v4l2_fh_exit(vfh); |
| 130 | subdev_fh_free(subdev_fh); |
| 131 | kfree(subdev_fh); |
| 132 | file->private_data = NULL; |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 133 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg) |
| 138 | { |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 139 | struct video_device *vdev = video_devdata(file); |
| 140 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 141 | struct v4l2_fh *vfh = file->private_data; |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 142 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 143 | switch (cmd) { |
Laurent Pinchart | ea8aa43 | 2009-12-09 08:39:54 -0300 | [diff] [blame] | 144 | case VIDIOC_QUERYCTRL: |
| 145 | return v4l2_subdev_queryctrl(sd, arg); |
| 146 | |
| 147 | case VIDIOC_QUERYMENU: |
| 148 | return v4l2_subdev_querymenu(sd, arg); |
| 149 | |
| 150 | case VIDIOC_G_CTRL: |
| 151 | return v4l2_subdev_g_ctrl(sd, arg); |
| 152 | |
| 153 | case VIDIOC_S_CTRL: |
| 154 | return v4l2_subdev_s_ctrl(sd, arg); |
| 155 | |
| 156 | case VIDIOC_G_EXT_CTRLS: |
| 157 | return v4l2_subdev_g_ext_ctrls(sd, arg); |
| 158 | |
| 159 | case VIDIOC_S_EXT_CTRLS: |
| 160 | return v4l2_subdev_s_ext_ctrls(sd, arg); |
| 161 | |
| 162 | case VIDIOC_TRY_EXT_CTRLS: |
| 163 | return v4l2_subdev_try_ext_ctrls(sd, arg); |
| 164 | |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 165 | case VIDIOC_DQEVENT: |
| 166 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) |
| 167 | return -ENOIOCTLCMD; |
| 168 | |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 169 | return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 170 | |
| 171 | case VIDIOC_SUBSCRIBE_EVENT: |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 172 | return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 173 | |
| 174 | case VIDIOC_UNSUBSCRIBE_EVENT: |
Stanimir Varbanov | 7cd5a16 | 2010-05-21 06:04:24 -0300 | [diff] [blame^] | 175 | return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 176 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 177 | default: |
| 178 | return -ENOIOCTLCMD; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static long subdev_ioctl(struct file *file, unsigned int cmd, |
| 185 | unsigned long arg) |
| 186 | { |
| 187 | return video_usercopy(file, cmd, arg, subdev_do_ioctl); |
| 188 | } |
| 189 | |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 190 | static unsigned int subdev_poll(struct file *file, poll_table *wait) |
| 191 | { |
| 192 | struct video_device *vdev = video_devdata(file); |
| 193 | struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); |
| 194 | struct v4l2_fh *fh = file->private_data; |
| 195 | |
| 196 | if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) |
| 197 | return POLLERR; |
| 198 | |
| 199 | poll_wait(file, &fh->events->wait, wait); |
| 200 | |
| 201 | if (v4l2_event_pending(fh)) |
| 202 | return POLLPRI; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 207 | const struct v4l2_file_operations v4l2_subdev_fops = { |
| 208 | .owner = THIS_MODULE, |
| 209 | .open = subdev_open, |
| 210 | .unlocked_ioctl = subdev_ioctl, |
| 211 | .release = subdev_close, |
Sakari Ailus | 02adb1c | 2010-03-03 12:49:38 -0300 | [diff] [blame] | 212 | .poll = subdev_poll, |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 213 | }; |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 214 | |
| 215 | void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) |
| 216 | { |
| 217 | INIT_LIST_HEAD(&sd->list); |
| 218 | BUG_ON(!ops); |
| 219 | sd->ops = ops; |
| 220 | sd->v4l2_dev = NULL; |
| 221 | sd->flags = 0; |
| 222 | sd->name[0] = '\0'; |
| 223 | sd->grp_id = 0; |
| 224 | sd->dev_priv = NULL; |
| 225 | sd->host_priv = NULL; |
Laurent Pinchart | 61f5db5 | 2009-12-09 08:40:08 -0300 | [diff] [blame] | 226 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 227 | sd->entity.name = sd->name; |
| 228 | sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV; |
| 229 | #endif |
Laurent Pinchart | 3dd5ee0 | 2009-12-09 08:38:52 -0300 | [diff] [blame] | 230 | } |
| 231 | EXPORT_SYMBOL(v4l2_subdev_init); |