Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 1 | /* |
| 2 | * v4l2-event.c |
| 3 | * |
| 4 | * V4L2 events. |
| 5 | * |
| 6 | * Copyright (C) 2009--2010 Nokia Corporation. |
| 7 | * |
| 8 | * Contact: Sakari Ailus <sakari.ailus@maxwell.research.nokia.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * version 2 as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 22 | * 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | #include <media/v4l2-dev.h> |
| 26 | #include <media/v4l2-fh.h> |
| 27 | #include <media/v4l2-event.h> |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 28 | #include <media/v4l2-ctrls.h> |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 29 | |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/slab.h> |
| 32 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 33 | static void v4l2_event_unsubscribe_all(struct v4l2_fh *fh); |
| 34 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 35 | int v4l2_event_alloc(struct v4l2_fh *fh, unsigned int n) |
| 36 | { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 37 | unsigned long flags; |
| 38 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 39 | while (fh->nallocated < n) { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 40 | struct v4l2_kevent *kev; |
| 41 | |
| 42 | kev = kzalloc(sizeof(*kev), GFP_KERNEL); |
| 43 | if (kev == NULL) |
| 44 | return -ENOMEM; |
| 45 | |
| 46 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 47 | list_add_tail(&kev->list, &fh->free); |
| 48 | fh->nallocated++; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 49 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | EXPORT_SYMBOL_GPL(v4l2_event_alloc); |
| 55 | |
| 56 | #define list_kfree(list, type, member) \ |
| 57 | while (!list_empty(list)) { \ |
| 58 | type *hi; \ |
| 59 | hi = list_first_entry(list, type, member); \ |
| 60 | list_del(&hi->member); \ |
| 61 | kfree(hi); \ |
| 62 | } |
| 63 | |
| 64 | void v4l2_event_free(struct v4l2_fh *fh) |
| 65 | { |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 66 | list_kfree(&fh->free, struct v4l2_kevent, list); |
| 67 | list_kfree(&fh->available, struct v4l2_kevent, list); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 68 | v4l2_event_unsubscribe_all(fh); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 69 | } |
| 70 | EXPORT_SYMBOL_GPL(v4l2_event_free); |
| 71 | |
| 72 | static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event) |
| 73 | { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 74 | struct v4l2_kevent *kev; |
| 75 | unsigned long flags; |
| 76 | |
| 77 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
| 78 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 79 | if (list_empty(&fh->available)) { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 80 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
| 81 | return -ENOENT; |
| 82 | } |
| 83 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 84 | WARN_ON(fh->navailable == 0); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 85 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 86 | kev = list_first_entry(&fh->available, struct v4l2_kevent, list); |
| 87 | list_move(&kev->list, &fh->free); |
| 88 | fh->navailable--; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 89 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 90 | kev->event.pending = fh->navailable; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 91 | *event = kev->event; |
| 92 | |
| 93 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event, |
| 99 | int nonblocking) |
| 100 | { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 101 | int ret; |
| 102 | |
| 103 | if (nonblocking) |
| 104 | return __v4l2_event_dequeue(fh, event); |
| 105 | |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 106 | /* Release the vdev lock while waiting */ |
| 107 | if (fh->vdev->lock) |
| 108 | mutex_unlock(fh->vdev->lock); |
| 109 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 110 | do { |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 111 | ret = wait_event_interruptible(fh->wait, |
| 112 | fh->navailable != 0); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 113 | if (ret < 0) |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 114 | break; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 115 | |
| 116 | ret = __v4l2_event_dequeue(fh, event); |
| 117 | } while (ret == -ENOENT); |
| 118 | |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 119 | if (fh->vdev->lock) |
| 120 | mutex_lock(fh->vdev->lock); |
| 121 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 122 | return ret; |
| 123 | } |
Laurent Pinchart | 0a4f8d0 | 2010-05-02 14:32:43 -0300 | [diff] [blame] | 124 | EXPORT_SYMBOL_GPL(v4l2_event_dequeue); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 125 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 126 | /* Caller must hold fh->vdev->fh_lock! */ |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 127 | static struct v4l2_subscribed_event *v4l2_event_subscribed( |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 128 | struct v4l2_fh *fh, u32 type, u32 id) |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 129 | { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 130 | struct v4l2_subscribed_event *sev; |
| 131 | |
Sakari Ailus | f3cd385 | 2010-05-03 12:42:46 -0300 | [diff] [blame] | 132 | assert_spin_locked(&fh->vdev->fh_lock); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 133 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 134 | list_for_each_entry(sev, &fh->subscribed, list) { |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 135 | if (sev->type == type && sev->id == id) |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 136 | return sev; |
| 137 | } |
| 138 | |
| 139 | return NULL; |
| 140 | } |
| 141 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 142 | static void __v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev, |
| 143 | const struct timespec *ts) |
| 144 | { |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 145 | struct v4l2_subscribed_event *sev; |
| 146 | struct v4l2_kevent *kev; |
| 147 | |
| 148 | /* Are we subscribed? */ |
| 149 | sev = v4l2_event_subscribed(fh, ev->type, ev->id); |
| 150 | if (sev == NULL) |
| 151 | return; |
| 152 | |
| 153 | /* Increase event sequence number on fh. */ |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 154 | fh->sequence++; |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 155 | |
| 156 | /* Do we have any free events? */ |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 157 | if (list_empty(&fh->free)) |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 158 | return; |
| 159 | |
| 160 | /* Take one and fill it. */ |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 161 | kev = list_first_entry(&fh->free, struct v4l2_kevent, list); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 162 | kev->event.type = ev->type; |
| 163 | kev->event.u = ev->u; |
| 164 | kev->event.id = ev->id; |
| 165 | kev->event.timestamp = *ts; |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 166 | kev->event.sequence = fh->sequence; |
| 167 | list_move_tail(&kev->list, &fh->available); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 168 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 169 | fh->navailable++; |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 170 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 171 | wake_up_all(&fh->wait); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 172 | } |
| 173 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 174 | void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev) |
| 175 | { |
| 176 | struct v4l2_fh *fh; |
| 177 | unsigned long flags; |
| 178 | struct timespec timestamp; |
| 179 | |
| 180 | ktime_get_ts(×tamp); |
| 181 | |
| 182 | spin_lock_irqsave(&vdev->fh_lock, flags); |
| 183 | |
| 184 | list_for_each_entry(fh, &vdev->fh_list, list) { |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 185 | __v4l2_event_queue_fh(fh, ev, ×tamp); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | spin_unlock_irqrestore(&vdev->fh_lock, flags); |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(v4l2_event_queue); |
| 191 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 192 | void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev) |
| 193 | { |
| 194 | unsigned long flags; |
| 195 | struct timespec timestamp; |
| 196 | |
| 197 | ktime_get_ts(×tamp); |
| 198 | |
| 199 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
| 200 | __v4l2_event_queue_fh(fh, ev, ×tamp); |
| 201 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
| 202 | } |
| 203 | EXPORT_SYMBOL_GPL(v4l2_event_queue_fh); |
| 204 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 205 | int v4l2_event_pending(struct v4l2_fh *fh) |
| 206 | { |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 207 | return fh->navailable; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 208 | } |
| 209 | EXPORT_SYMBOL_GPL(v4l2_event_pending); |
| 210 | |
| 211 | int v4l2_event_subscribe(struct v4l2_fh *fh, |
| 212 | struct v4l2_event_subscription *sub) |
| 213 | { |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 214 | struct v4l2_subscribed_event *sev, *found_ev; |
| 215 | struct v4l2_ctrl *ctrl = NULL; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 216 | unsigned long flags; |
| 217 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 218 | if (sub->type == V4L2_EVENT_CTRL) { |
| 219 | ctrl = v4l2_ctrl_find(fh->ctrl_handler, sub->id); |
| 220 | if (ctrl == NULL) |
| 221 | return -EINVAL; |
| 222 | } |
| 223 | |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame^] | 224 | sev = kzalloc(sizeof(*sev), GFP_KERNEL); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 225 | if (!sev) |
| 226 | return -ENOMEM; |
| 227 | |
| 228 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
| 229 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 230 | found_ev = v4l2_event_subscribed(fh, sub->type, sub->id); |
| 231 | if (!found_ev) { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 232 | INIT_LIST_HEAD(&sev->list); |
| 233 | sev->type = sub->type; |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 234 | sev->id = sub->id; |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame^] | 235 | sev->fh = fh; |
| 236 | sev->flags = sub->flags; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 237 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 238 | list_add(&sev->list, &fh->subscribed); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
| 242 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 243 | /* v4l2_ctrl_add_fh uses a mutex, so do this outside the spin lock */ |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame^] | 244 | if (found_ev) |
| 245 | kfree(sev); |
| 246 | else if (ctrl) |
| 247 | v4l2_ctrl_add_event(ctrl, sev); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(v4l2_event_subscribe); |
| 252 | |
| 253 | static void v4l2_event_unsubscribe_all(struct v4l2_fh *fh) |
| 254 | { |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 255 | struct v4l2_event_subscription sub; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 256 | struct v4l2_subscribed_event *sev; |
| 257 | unsigned long flags; |
| 258 | |
| 259 | do { |
| 260 | sev = NULL; |
| 261 | |
| 262 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 263 | if (!list_empty(&fh->subscribed)) { |
| 264 | sev = list_first_entry(&fh->subscribed, |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 265 | struct v4l2_subscribed_event, list); |
| 266 | sub.type = sev->type; |
| 267 | sub.id = sev->id; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 268 | } |
| 269 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 270 | if (sev) |
| 271 | v4l2_event_unsubscribe(fh, &sub); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 272 | } while (sev); |
| 273 | } |
| 274 | |
| 275 | int v4l2_event_unsubscribe(struct v4l2_fh *fh, |
| 276 | struct v4l2_event_subscription *sub) |
| 277 | { |
| 278 | struct v4l2_subscribed_event *sev; |
| 279 | unsigned long flags; |
| 280 | |
| 281 | if (sub->type == V4L2_EVENT_ALL) { |
| 282 | v4l2_event_unsubscribe_all(fh); |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
| 287 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 288 | sev = v4l2_event_subscribed(fh, sub->type, sub->id); |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame^] | 289 | if (sev != NULL) { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 290 | list_del(&sev->list); |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame^] | 291 | sev->fh = NULL; |
| 292 | } |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 293 | |
| 294 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame^] | 295 | if (sev && sev->type == V4L2_EVENT_CTRL) { |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 296 | struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler, sev->id); |
| 297 | |
| 298 | if (ctrl) |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame^] | 299 | v4l2_ctrl_del_event(ctrl, sev); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 300 | } |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 301 | |
| 302 | kfree(sev); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe); |