v4l2-event.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * v4l2-event.c
  3. *
  4. * V4L2 events.
  5. *
  6. * Copyright (C) 2009--2010 Nokia Corporation.
  7. *
  8. * Contact: Sakari Ailus <sakari.ailus@iki.fi>
  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. #include <media/v4l2-dev.h>
  25. #include <media/v4l2-fh.h>
  26. #include <media/v4l2-event.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/export.h>
  30. static unsigned sev_pos(const struct v4l2_subscribed_event *sev, unsigned idx)
  31. {
  32. idx += sev->first;
  33. return idx >= sev->elems ? idx - sev->elems : idx;
  34. }
  35. static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
  36. {
  37. struct v4l2_kevent *kev;
  38. unsigned long flags;
  39. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  40. if (list_empty(&fh->available)) {
  41. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  42. return -ENOENT;
  43. }
  44. WARN_ON(fh->navailable == 0);
  45. kev = list_first_entry(&fh->available, struct v4l2_kevent, list);
  46. list_del(&kev->list);
  47. fh->navailable--;
  48. kev->event.pending = fh->navailable;
  49. *event = kev->event;
  50. kev->sev->first = sev_pos(kev->sev, 1);
  51. kev->sev->in_use--;
  52. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  53. return 0;
  54. }
  55. int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
  56. int nonblocking)
  57. {
  58. int ret;
  59. if (nonblocking)
  60. return __v4l2_event_dequeue(fh, event);
  61. /* Release the vdev lock while waiting */
  62. if (fh->vdev->lock)
  63. mutex_unlock(fh->vdev->lock);
  64. do {
  65. ret = wait_event_interruptible(fh->wait,
  66. fh->navailable != 0);
  67. if (ret < 0)
  68. break;
  69. ret = __v4l2_event_dequeue(fh, event);
  70. } while (ret == -ENOENT);
  71. if (fh->vdev->lock)
  72. mutex_lock(fh->vdev->lock);
  73. return ret;
  74. }
  75. EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
  76. /* Caller must hold fh->vdev->fh_lock! */
  77. static struct v4l2_subscribed_event *v4l2_event_subscribed(
  78. struct v4l2_fh *fh, u32 type, u32 id)
  79. {
  80. struct v4l2_subscribed_event *sev;
  81. assert_spin_locked(&fh->vdev->fh_lock);
  82. list_for_each_entry(sev, &fh->subscribed, list)
  83. if (sev->type == type && sev->id == id)
  84. return sev;
  85. return NULL;
  86. }
  87. static void __v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev,
  88. const struct timespec *ts)
  89. {
  90. struct v4l2_subscribed_event *sev;
  91. struct v4l2_kevent *kev;
  92. bool copy_payload = true;
  93. /* Are we subscribed? */
  94. sev = v4l2_event_subscribed(fh, ev->type, ev->id);
  95. if (sev == NULL)
  96. return;
  97. /*
  98. * If the event has been added to the fh->subscribed list, but its
  99. * add op has not completed yet elems will be 0, treat this as
  100. * not being subscribed.
  101. */
  102. if (!sev->elems)
  103. return;
  104. /* Increase event sequence number on fh. */
  105. fh->sequence++;
  106. /* Do we have any free events? */
  107. if (sev->in_use == sev->elems) {
  108. /* no, remove the oldest one */
  109. kev = sev->events + sev_pos(sev, 0);
  110. list_del(&kev->list);
  111. sev->in_use--;
  112. sev->first = sev_pos(sev, 1);
  113. fh->navailable--;
  114. if (sev->elems == 1) {
  115. if (sev->ops && sev->ops->replace) {
  116. sev->ops->replace(&kev->event, ev);
  117. copy_payload = false;
  118. }
  119. } else if (sev->ops && sev->ops->merge) {
  120. struct v4l2_kevent *second_oldest =
  121. sev->events + sev_pos(sev, 0);
  122. sev->ops->merge(&kev->event, &second_oldest->event);
  123. }
  124. }
  125. /* Take one and fill it. */
  126. kev = sev->events + sev_pos(sev, sev->in_use);
  127. kev->event.type = ev->type;
  128. if (copy_payload)
  129. kev->event.u = ev->u;
  130. kev->event.id = ev->id;
  131. kev->event.timestamp = *ts;
  132. kev->event.sequence = fh->sequence;
  133. sev->in_use++;
  134. list_add_tail(&kev->list, &fh->available);
  135. fh->navailable++;
  136. wake_up_all(&fh->wait);
  137. }
  138. void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
  139. {
  140. struct v4l2_fh *fh;
  141. unsigned long flags;
  142. struct timespec timestamp;
  143. if (vdev == NULL)
  144. return;
  145. ktime_get_ts(&timestamp);
  146. spin_lock_irqsave(&vdev->fh_lock, flags);
  147. list_for_each_entry(fh, &vdev->fh_list, list)
  148. __v4l2_event_queue_fh(fh, ev, &timestamp);
  149. spin_unlock_irqrestore(&vdev->fh_lock, flags);
  150. }
  151. EXPORT_SYMBOL_GPL(v4l2_event_queue);
  152. void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
  153. {
  154. unsigned long flags;
  155. struct timespec timestamp;
  156. ktime_get_ts(&timestamp);
  157. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  158. __v4l2_event_queue_fh(fh, ev, &timestamp);
  159. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  160. }
  161. EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
  162. int v4l2_event_pending(struct v4l2_fh *fh)
  163. {
  164. return fh->navailable;
  165. }
  166. EXPORT_SYMBOL_GPL(v4l2_event_pending);
  167. int v4l2_event_subscribe(struct v4l2_fh *fh,
  168. const struct v4l2_event_subscription *sub, unsigned elems,
  169. const struct v4l2_subscribed_event_ops *ops)
  170. {
  171. struct v4l2_subscribed_event *sev, *found_ev;
  172. unsigned long flags;
  173. unsigned i;
  174. if (sub->type == V4L2_EVENT_ALL)
  175. return -EINVAL;
  176. if (elems < 1)
  177. elems = 1;
  178. sev = kzalloc(sizeof(*sev) + sizeof(struct v4l2_kevent) * elems, GFP_KERNEL);
  179. if (!sev)
  180. return -ENOMEM;
  181. for (i = 0; i < elems; i++)
  182. sev->events[i].sev = sev;
  183. sev->type = sub->type;
  184. sev->id = sub->id;
  185. sev->flags = sub->flags;
  186. sev->fh = fh;
  187. sev->ops = ops;
  188. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  189. found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
  190. if (!found_ev)
  191. list_add(&sev->list, &fh->subscribed);
  192. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  193. if (found_ev) {
  194. kfree(sev);
  195. return 0; /* Already listening */
  196. }
  197. if (sev->ops && sev->ops->add) {
  198. int ret = sev->ops->add(sev, elems);
  199. if (ret) {
  200. sev->ops = NULL;
  201. v4l2_event_unsubscribe(fh, sub);
  202. return ret;
  203. }
  204. }
  205. /* Mark as ready for use */
  206. sev->elems = elems;
  207. return 0;
  208. }
  209. EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
  210. void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
  211. {
  212. struct v4l2_event_subscription sub;
  213. struct v4l2_subscribed_event *sev;
  214. unsigned long flags;
  215. do {
  216. sev = NULL;
  217. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  218. if (!list_empty(&fh->subscribed)) {
  219. sev = list_first_entry(&fh->subscribed,
  220. struct v4l2_subscribed_event, list);
  221. sub.type = sev->type;
  222. sub.id = sev->id;
  223. }
  224. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  225. if (sev)
  226. v4l2_event_unsubscribe(fh, &sub);
  227. } while (sev);
  228. }
  229. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
  230. int v4l2_event_unsubscribe(struct v4l2_fh *fh,
  231. const struct v4l2_event_subscription *sub)
  232. {
  233. struct v4l2_subscribed_event *sev;
  234. unsigned long flags;
  235. int i;
  236. if (sub->type == V4L2_EVENT_ALL) {
  237. v4l2_event_unsubscribe_all(fh);
  238. return 0;
  239. }
  240. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  241. sev = v4l2_event_subscribed(fh, sub->type, sub->id);
  242. if (sev != NULL) {
  243. /* Remove any pending events for this subscription */
  244. for (i = 0; i < sev->in_use; i++) {
  245. list_del(&sev->events[sev_pos(sev, i)].list);
  246. fh->navailable--;
  247. }
  248. list_del(&sev->list);
  249. }
  250. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  251. if (sev && sev->ops && sev->ops->del)
  252. sev->ops->del(sev);
  253. kfree(sev);
  254. return 0;
  255. }
  256. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);
  257. int v4l2_event_subdev_unsubscribe(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  258. struct v4l2_event_subscription *sub)
  259. {
  260. return v4l2_event_unsubscribe(fh, sub);
  261. }
  262. EXPORT_SYMBOL_GPL(v4l2_event_subdev_unsubscribe);
  263. static void v4l2_event_src_replace(struct v4l2_event *old,
  264. const struct v4l2_event *new)
  265. {
  266. u32 old_changes = old->u.src_change.changes;
  267. old->u.src_change = new->u.src_change;
  268. old->u.src_change.changes |= old_changes;
  269. }
  270. static void v4l2_event_src_merge(const struct v4l2_event *old,
  271. struct v4l2_event *new)
  272. {
  273. new->u.src_change.changes |= old->u.src_change.changes;
  274. }
  275. static const struct v4l2_subscribed_event_ops v4l2_event_src_ch_ops = {
  276. .replace = v4l2_event_src_replace,
  277. .merge = v4l2_event_src_merge,
  278. };
  279. int v4l2_src_change_event_subscribe(struct v4l2_fh *fh,
  280. const struct v4l2_event_subscription *sub)
  281. {
  282. if (sub->type == V4L2_EVENT_SOURCE_CHANGE)
  283. return v4l2_event_subscribe(fh, sub, 0, &v4l2_event_src_ch_ops);
  284. return -EINVAL;
  285. }
  286. EXPORT_SYMBOL_GPL(v4l2_src_change_event_subscribe);
  287. int v4l2_src_change_event_subdev_subscribe(struct v4l2_subdev *sd,
  288. struct v4l2_fh *fh, struct v4l2_event_subscription *sub)
  289. {
  290. return v4l2_src_change_event_subscribe(fh, sub);
  291. }
  292. EXPORT_SYMBOL_GPL(v4l2_src_change_event_subdev_subscribe);