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 | * |
Sakari Ailus | 8c5dff9 | 2012-10-28 06:44:17 -0300 | [diff] [blame] | 8 | * Contact: Sakari Ailus <sakari.ailus@iki.fi> |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 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> |
| 28 | |
| 29 | #include <linux/sched.h> |
| 30 | #include <linux/slab.h> |
Paul Gortmaker | 35a2463 | 2011-08-01 15:26:38 -0400 | [diff] [blame] | 31 | #include <linux/export.h> |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 32 | |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 33 | static unsigned sev_pos(const struct v4l2_subscribed_event *sev, unsigned idx) |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 34 | { |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 35 | idx += sev->first; |
| 36 | return idx >= sev->elems ? idx - sev->elems : idx; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 37 | } |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 38 | |
| 39 | static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event) |
| 40 | { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 41 | struct v4l2_kevent *kev; |
| 42 | unsigned long flags; |
| 43 | |
| 44 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
| 45 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 46 | if (list_empty(&fh->available)) { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 47 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
| 48 | return -ENOENT; |
| 49 | } |
| 50 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 51 | WARN_ON(fh->navailable == 0); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 52 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 53 | kev = list_first_entry(&fh->available, struct v4l2_kevent, list); |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 54 | list_del(&kev->list); |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 55 | fh->navailable--; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 56 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 57 | kev->event.pending = fh->navailable; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 58 | *event = kev->event; |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 59 | kev->sev->first = sev_pos(kev->sev, 1); |
| 60 | kev->sev->in_use--; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 61 | |
| 62 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event, |
| 68 | int nonblocking) |
| 69 | { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 70 | int ret; |
| 71 | |
| 72 | if (nonblocking) |
| 73 | return __v4l2_event_dequeue(fh, event); |
| 74 | |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 75 | /* Release the vdev lock while waiting */ |
| 76 | if (fh->vdev->lock) |
| 77 | mutex_unlock(fh->vdev->lock); |
| 78 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 79 | do { |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 80 | ret = wait_event_interruptible(fh->wait, |
| 81 | fh->navailable != 0); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 82 | if (ret < 0) |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 83 | break; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 84 | |
| 85 | ret = __v4l2_event_dequeue(fh, event); |
| 86 | } while (ret == -ENOENT); |
| 87 | |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 88 | if (fh->vdev->lock) |
| 89 | mutex_lock(fh->vdev->lock); |
| 90 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 91 | return ret; |
| 92 | } |
Laurent Pinchart | 0a4f8d0 | 2010-05-02 14:32:43 -0300 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(v4l2_event_dequeue); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 94 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 95 | /* Caller must hold fh->vdev->fh_lock! */ |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 96 | static struct v4l2_subscribed_event *v4l2_event_subscribed( |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 97 | struct v4l2_fh *fh, u32 type, u32 id) |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 98 | { |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 99 | struct v4l2_subscribed_event *sev; |
| 100 | |
Sakari Ailus | f3cd385 | 2010-05-03 12:42:46 -0300 | [diff] [blame] | 101 | assert_spin_locked(&fh->vdev->fh_lock); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 102 | |
Hans Verkuil | 3f66f0e | 2011-06-20 11:56:24 -0300 | [diff] [blame] | 103 | list_for_each_entry(sev, &fh->subscribed, list) |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 104 | if (sev->type == type && sev->id == id) |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 105 | return sev; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 106 | |
| 107 | return NULL; |
| 108 | } |
| 109 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 110 | static void __v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev, |
| 111 | const struct timespec *ts) |
| 112 | { |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 113 | struct v4l2_subscribed_event *sev; |
| 114 | struct v4l2_kevent *kev; |
Hans Verkuil | 2151bdc | 2011-06-18 07:02:20 -0300 | [diff] [blame] | 115 | bool copy_payload = true; |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 116 | |
| 117 | /* Are we subscribed? */ |
| 118 | sev = v4l2_event_subscribed(fh, ev->type, ev->id); |
| 119 | if (sev == NULL) |
| 120 | return; |
| 121 | |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 122 | /* |
| 123 | * If the event has been added to the fh->subscribed list, but its |
| 124 | * add op has not completed yet elems will be 0, treat this as |
| 125 | * not being subscribed. |
| 126 | */ |
| 127 | if (!sev->elems) |
| 128 | return; |
| 129 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 130 | /* Increase event sequence number on fh. */ |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 131 | fh->sequence++; |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 132 | |
| 133 | /* Do we have any free events? */ |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 134 | if (sev->in_use == sev->elems) { |
| 135 | /* no, remove the oldest one */ |
| 136 | kev = sev->events + sev_pos(sev, 0); |
| 137 | list_del(&kev->list); |
| 138 | sev->in_use--; |
| 139 | sev->first = sev_pos(sev, 1); |
| 140 | fh->navailable--; |
Hans Verkuil | 2151bdc | 2011-06-18 07:02:20 -0300 | [diff] [blame] | 141 | if (sev->elems == 1) { |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 142 | if (sev->ops && sev->ops->replace) { |
| 143 | sev->ops->replace(&kev->event, ev); |
Hans Verkuil | 2151bdc | 2011-06-18 07:02:20 -0300 | [diff] [blame] | 144 | copy_payload = false; |
| 145 | } |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 146 | } else if (sev->ops && sev->ops->merge) { |
Hans Verkuil | 2151bdc | 2011-06-18 07:02:20 -0300 | [diff] [blame] | 147 | struct v4l2_kevent *second_oldest = |
| 148 | sev->events + sev_pos(sev, 0); |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 149 | sev->ops->merge(&kev->event, &second_oldest->event); |
Hans Verkuil | 2151bdc | 2011-06-18 07:02:20 -0300 | [diff] [blame] | 150 | } |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 151 | } |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 152 | |
| 153 | /* Take one and fill it. */ |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 154 | kev = sev->events + sev_pos(sev, sev->in_use); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 155 | kev->event.type = ev->type; |
Hans Verkuil | 2151bdc | 2011-06-18 07:02:20 -0300 | [diff] [blame] | 156 | if (copy_payload) |
| 157 | kev->event.u = ev->u; |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 158 | kev->event.id = ev->id; |
| 159 | kev->event.timestamp = *ts; |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 160 | kev->event.sequence = fh->sequence; |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 161 | sev->in_use++; |
| 162 | list_add_tail(&kev->list, &fh->available); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 163 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 164 | fh->navailable++; |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 165 | |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 166 | wake_up_all(&fh->wait); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 167 | } |
| 168 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 169 | void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev) |
| 170 | { |
| 171 | struct v4l2_fh *fh; |
| 172 | unsigned long flags; |
| 173 | struct timespec timestamp; |
| 174 | |
| 175 | ktime_get_ts(×tamp); |
| 176 | |
| 177 | spin_lock_irqsave(&vdev->fh_lock, flags); |
| 178 | |
Hans Verkuil | 3f66f0e | 2011-06-20 11:56:24 -0300 | [diff] [blame] | 179 | list_for_each_entry(fh, &vdev->fh_list, list) |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 180 | __v4l2_event_queue_fh(fh, ev, ×tamp); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 181 | |
| 182 | spin_unlock_irqrestore(&vdev->fh_lock, flags); |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(v4l2_event_queue); |
| 185 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 186 | void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev) |
| 187 | { |
| 188 | unsigned long flags; |
| 189 | struct timespec timestamp; |
| 190 | |
| 191 | ktime_get_ts(×tamp); |
| 192 | |
| 193 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
| 194 | __v4l2_event_queue_fh(fh, ev, ×tamp); |
| 195 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
| 196 | } |
| 197 | EXPORT_SYMBOL_GPL(v4l2_event_queue_fh); |
| 198 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 199 | int v4l2_event_pending(struct v4l2_fh *fh) |
| 200 | { |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 201 | return fh->navailable; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 202 | } |
| 203 | EXPORT_SYMBOL_GPL(v4l2_event_pending); |
| 204 | |
| 205 | int v4l2_event_subscribe(struct v4l2_fh *fh, |
Hans Verkuil | 85f5fe3 | 2012-09-04 11:46:09 -0300 | [diff] [blame] | 206 | const struct v4l2_event_subscription *sub, unsigned elems, |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 207 | const struct v4l2_subscribed_event_ops *ops) |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 208 | { |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 209 | struct v4l2_subscribed_event *sev, *found_ev; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 210 | unsigned long flags; |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 211 | unsigned i; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 212 | |
Hans de Goede | b36b505 | 2011-10-24 05:03:27 -0300 | [diff] [blame] | 213 | if (sub->type == V4L2_EVENT_ALL) |
| 214 | return -EINVAL; |
| 215 | |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 216 | if (elems < 1) |
| 217 | elems = 1; |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 218 | |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 219 | sev = kzalloc(sizeof(*sev) + sizeof(struct v4l2_kevent) * elems, GFP_KERNEL); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 220 | if (!sev) |
| 221 | return -ENOMEM; |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 222 | for (i = 0; i < elems; i++) |
| 223 | sev->events[i].sev = sev; |
| 224 | sev->type = sub->type; |
| 225 | sev->id = sub->id; |
| 226 | sev->flags = sub->flags; |
| 227 | sev->fh = fh; |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 228 | sev->ops = ops; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 229 | |
| 230 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 231 | found_ev = v4l2_event_subscribed(fh, sub->type, sub->id); |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 232 | if (!found_ev) |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 233 | list_add(&sev->list, &fh->subscribed); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 234 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
| 235 | |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 236 | if (found_ev) { |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame] | 237 | kfree(sev); |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 238 | return 0; /* Already listening */ |
| 239 | } |
| 240 | |
| 241 | if (sev->ops && sev->ops->add) { |
Hans Verkuil | 6e6d76c | 2012-05-07 16:53:20 -0300 | [diff] [blame] | 242 | int ret = sev->ops->add(sev, elems); |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 243 | if (ret) { |
| 244 | sev->ops = NULL; |
| 245 | v4l2_event_unsubscribe(fh, sub); |
| 246 | return ret; |
| 247 | } |
| 248 | } |
| 249 | |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 250 | /* Mark as ready for use */ |
| 251 | sev->elems = elems; |
| 252 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | EXPORT_SYMBOL_GPL(v4l2_event_subscribe); |
| 256 | |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 257 | void v4l2_event_unsubscribe_all(struct v4l2_fh *fh) |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 258 | { |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 259 | struct v4l2_event_subscription sub; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 260 | struct v4l2_subscribed_event *sev; |
| 261 | unsigned long flags; |
| 262 | |
| 263 | do { |
| 264 | sev = NULL; |
| 265 | |
| 266 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
Hans Verkuil | 523f46d | 2011-06-13 17:44:42 -0300 | [diff] [blame] | 267 | if (!list_empty(&fh->subscribed)) { |
| 268 | sev = list_first_entry(&fh->subscribed, |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 269 | struct v4l2_subscribed_event, list); |
| 270 | sub.type = sev->type; |
| 271 | sub.id = sev->id; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 272 | } |
| 273 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 274 | if (sev) |
| 275 | v4l2_event_unsubscribe(fh, &sub); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 276 | } while (sev); |
| 277 | } |
Hans Verkuil | f1e393d | 2011-06-13 19:24:17 -0300 | [diff] [blame] | 278 | EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all); |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 279 | |
| 280 | int v4l2_event_unsubscribe(struct v4l2_fh *fh, |
Hans Verkuil | 85f5fe3 | 2012-09-04 11:46:09 -0300 | [diff] [blame] | 281 | const struct v4l2_event_subscription *sub) |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 282 | { |
| 283 | struct v4l2_subscribed_event *sev; |
| 284 | unsigned long flags; |
Hans de Goede | 78c87e8 | 2011-10-26 05:40:27 -0300 | [diff] [blame] | 285 | int i; |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 286 | |
| 287 | if (sub->type == V4L2_EVENT_ALL) { |
| 288 | v4l2_event_unsubscribe_all(fh); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | spin_lock_irqsave(&fh->vdev->fh_lock, flags); |
| 293 | |
Hans Verkuil | 6e23939 | 2011-06-07 11:13:44 -0300 | [diff] [blame] | 294 | sev = v4l2_event_subscribed(fh, sub->type, sub->id); |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame] | 295 | if (sev != NULL) { |
Hans de Goede | 78c87e8 | 2011-10-26 05:40:27 -0300 | [diff] [blame] | 296 | /* Remove any pending events for this subscription */ |
| 297 | for (i = 0; i < sev->in_use; i++) { |
| 298 | list_del(&sev->events[sev_pos(sev, i)].list); |
| 299 | fh->navailable--; |
| 300 | } |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 301 | list_del(&sev->list); |
Hans Verkuil | 77068d3 | 2011-06-13 18:55:58 -0300 | [diff] [blame] | 302 | } |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 303 | |
| 304 | spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); |
Hans de Goede | c53c254 | 2012-04-08 12:59:46 -0300 | [diff] [blame] | 305 | |
| 306 | if (sev && sev->ops && sev->ops->del) |
| 307 | sev->ops->del(sev); |
| 308 | |
Sakari Ailus | c3b5b02 | 2010-03-01 05:14:18 -0300 | [diff] [blame] | 309 | kfree(sev); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe); |
Sylwester Nawrocki | 4f4d14b | 2013-01-22 18:58:57 -0300 | [diff] [blame] | 314 | |
| 315 | int v4l2_event_subdev_unsubscribe(struct v4l2_subdev *sd, struct v4l2_fh *fh, |
| 316 | struct v4l2_event_subscription *sub) |
| 317 | { |
| 318 | return v4l2_event_unsubscribe(fh, sub); |
| 319 | } |
| 320 | EXPORT_SYMBOL_GPL(v4l2_event_subdev_unsubscribe); |
Arun Kumar K | 3cbe6e5 | 2014-05-14 03:59:42 -0300 | [diff] [blame] | 321 | |
| 322 | static void v4l2_event_src_replace(struct v4l2_event *old, |
| 323 | const struct v4l2_event *new) |
| 324 | { |
| 325 | u32 old_changes = old->u.src_change.changes; |
| 326 | |
| 327 | old->u.src_change = new->u.src_change; |
| 328 | old->u.src_change.changes |= old_changes; |
| 329 | } |
| 330 | |
| 331 | static void v4l2_event_src_merge(const struct v4l2_event *old, |
| 332 | struct v4l2_event *new) |
| 333 | { |
| 334 | new->u.src_change.changes |= old->u.src_change.changes; |
| 335 | } |
| 336 | |
| 337 | static const struct v4l2_subscribed_event_ops v4l2_event_src_ch_ops = { |
| 338 | .replace = v4l2_event_src_replace, |
| 339 | .merge = v4l2_event_src_merge, |
| 340 | }; |
| 341 | |
| 342 | int v4l2_src_change_event_subscribe(struct v4l2_fh *fh, |
| 343 | const struct v4l2_event_subscription *sub) |
| 344 | { |
| 345 | if (sub->type == V4L2_EVENT_SOURCE_CHANGE) |
| 346 | return v4l2_event_subscribe(fh, sub, 0, &v4l2_event_src_ch_ops); |
| 347 | return -EINVAL; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(v4l2_src_change_event_subscribe); |
| 350 | |
| 351 | int v4l2_src_change_event_subdev_subscribe(struct v4l2_subdev *sd, |
| 352 | struct v4l2_fh *fh, struct v4l2_event_subscription *sub) |
| 353 | { |
| 354 | return v4l2_src_change_event_subscribe(fh, sub); |
| 355 | } |
| 356 | EXPORT_SYMBOL_GPL(v4l2_src_change_event_subdev_subscribe); |