v4l2-event.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #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. ktime_get_ts(&timestamp);
  144. spin_lock_irqsave(&vdev->fh_lock, flags);
  145. list_for_each_entry(fh, &vdev->fh_list, list)
  146. __v4l2_event_queue_fh(fh, ev, &timestamp);
  147. spin_unlock_irqrestore(&vdev->fh_lock, flags);
  148. }
  149. EXPORT_SYMBOL_GPL(v4l2_event_queue);
  150. void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
  151. {
  152. unsigned long flags;
  153. struct timespec timestamp;
  154. ktime_get_ts(&timestamp);
  155. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  156. __v4l2_event_queue_fh(fh, ev, &timestamp);
  157. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  158. }
  159. EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
  160. int v4l2_event_pending(struct v4l2_fh *fh)
  161. {
  162. return fh->navailable;
  163. }
  164. EXPORT_SYMBOL_GPL(v4l2_event_pending);
  165. int v4l2_event_subscribe(struct v4l2_fh *fh,
  166. const struct v4l2_event_subscription *sub, unsigned elems,
  167. const struct v4l2_subscribed_event_ops *ops)
  168. {
  169. struct v4l2_subscribed_event *sev, *found_ev;
  170. unsigned long flags;
  171. unsigned i;
  172. if (sub->type == V4L2_EVENT_ALL)
  173. return -EINVAL;
  174. if (elems < 1)
  175. elems = 1;
  176. sev = kzalloc(sizeof(*sev) + sizeof(struct v4l2_kevent) * elems, GFP_KERNEL);
  177. if (!sev)
  178. return -ENOMEM;
  179. for (i = 0; i < elems; i++)
  180. sev->events[i].sev = sev;
  181. sev->type = sub->type;
  182. sev->id = sub->id;
  183. sev->flags = sub->flags;
  184. sev->fh = fh;
  185. sev->ops = ops;
  186. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  187. found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
  188. if (!found_ev)
  189. list_add(&sev->list, &fh->subscribed);
  190. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  191. if (found_ev) {
  192. kfree(sev);
  193. return 0; /* Already listening */
  194. }
  195. if (sev->ops && sev->ops->add) {
  196. int ret = sev->ops->add(sev, elems);
  197. if (ret) {
  198. sev->ops = NULL;
  199. v4l2_event_unsubscribe(fh, sub);
  200. return ret;
  201. }
  202. }
  203. /* Mark as ready for use */
  204. sev->elems = elems;
  205. return 0;
  206. }
  207. EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
  208. void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
  209. {
  210. struct v4l2_event_subscription sub;
  211. struct v4l2_subscribed_event *sev;
  212. unsigned long flags;
  213. do {
  214. sev = NULL;
  215. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  216. if (!list_empty(&fh->subscribed)) {
  217. sev = list_first_entry(&fh->subscribed,
  218. struct v4l2_subscribed_event, list);
  219. sub.type = sev->type;
  220. sub.id = sev->id;
  221. }
  222. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  223. if (sev)
  224. v4l2_event_unsubscribe(fh, &sub);
  225. } while (sev);
  226. }
  227. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
  228. int v4l2_event_unsubscribe(struct v4l2_fh *fh,
  229. const struct v4l2_event_subscription *sub)
  230. {
  231. struct v4l2_subscribed_event *sev;
  232. unsigned long flags;
  233. int i;
  234. if (sub->type == V4L2_EVENT_ALL) {
  235. v4l2_event_unsubscribe_all(fh);
  236. return 0;
  237. }
  238. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  239. sev = v4l2_event_subscribed(fh, sub->type, sub->id);
  240. if (sev != NULL) {
  241. /* Remove any pending events for this subscription */
  242. for (i = 0; i < sev->in_use; i++) {
  243. list_del(&sev->events[sev_pos(sev, i)].list);
  244. fh->navailable--;
  245. }
  246. list_del(&sev->list);
  247. }
  248. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  249. if (sev && sev->ops && sev->ops->del)
  250. sev->ops->del(sev);
  251. kfree(sev);
  252. return 0;
  253. }
  254. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);