v4l2-event.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <media/v4l2-ctrls.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.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. /* Increase event sequence number on fh. */
  98. fh->sequence++;
  99. /* Do we have any free events? */
  100. if (sev->in_use == sev->elems) {
  101. /* no, remove the oldest one */
  102. kev = sev->events + sev_pos(sev, 0);
  103. list_del(&kev->list);
  104. sev->in_use--;
  105. sev->first = sev_pos(sev, 1);
  106. fh->navailable--;
  107. if (sev->elems == 1) {
  108. if (sev->replace) {
  109. sev->replace(&kev->event, ev);
  110. copy_payload = false;
  111. }
  112. } else if (sev->merge) {
  113. struct v4l2_kevent *second_oldest =
  114. sev->events + sev_pos(sev, 0);
  115. sev->merge(&kev->event, &second_oldest->event);
  116. }
  117. }
  118. /* Take one and fill it. */
  119. kev = sev->events + sev_pos(sev, sev->in_use);
  120. kev->event.type = ev->type;
  121. if (copy_payload)
  122. kev->event.u = ev->u;
  123. kev->event.id = ev->id;
  124. kev->event.timestamp = *ts;
  125. kev->event.sequence = fh->sequence;
  126. sev->in_use++;
  127. list_add_tail(&kev->list, &fh->available);
  128. fh->navailable++;
  129. wake_up_all(&fh->wait);
  130. }
  131. void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
  132. {
  133. struct v4l2_fh *fh;
  134. unsigned long flags;
  135. struct timespec timestamp;
  136. ktime_get_ts(&timestamp);
  137. spin_lock_irqsave(&vdev->fh_lock, flags);
  138. list_for_each_entry(fh, &vdev->fh_list, list)
  139. __v4l2_event_queue_fh(fh, ev, &timestamp);
  140. spin_unlock_irqrestore(&vdev->fh_lock, flags);
  141. }
  142. EXPORT_SYMBOL_GPL(v4l2_event_queue);
  143. void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
  144. {
  145. unsigned long flags;
  146. struct timespec timestamp;
  147. ktime_get_ts(&timestamp);
  148. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  149. __v4l2_event_queue_fh(fh, ev, &timestamp);
  150. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  151. }
  152. EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
  153. int v4l2_event_pending(struct v4l2_fh *fh)
  154. {
  155. return fh->navailable;
  156. }
  157. EXPORT_SYMBOL_GPL(v4l2_event_pending);
  158. static void ctrls_replace(struct v4l2_event *old, const struct v4l2_event *new)
  159. {
  160. u32 old_changes = old->u.ctrl.changes;
  161. old->u.ctrl = new->u.ctrl;
  162. old->u.ctrl.changes |= old_changes;
  163. }
  164. static void ctrls_merge(const struct v4l2_event *old, struct v4l2_event *new)
  165. {
  166. new->u.ctrl.changes |= old->u.ctrl.changes;
  167. }
  168. int v4l2_event_subscribe(struct v4l2_fh *fh,
  169. struct v4l2_event_subscription *sub, unsigned elems)
  170. {
  171. struct v4l2_subscribed_event *sev, *found_ev;
  172. struct v4l2_ctrl *ctrl = NULL;
  173. unsigned long flags;
  174. unsigned i;
  175. if (elems < 1)
  176. elems = 1;
  177. if (sub->type == V4L2_EVENT_CTRL) {
  178. ctrl = v4l2_ctrl_find(fh->ctrl_handler, sub->id);
  179. if (ctrl == NULL)
  180. return -EINVAL;
  181. }
  182. sev = kzalloc(sizeof(*sev) + sizeof(struct v4l2_kevent) * elems, GFP_KERNEL);
  183. if (!sev)
  184. return -ENOMEM;
  185. for (i = 0; i < elems; i++)
  186. sev->events[i].sev = sev;
  187. sev->type = sub->type;
  188. sev->id = sub->id;
  189. sev->flags = sub->flags;
  190. sev->fh = fh;
  191. sev->elems = elems;
  192. if (ctrl) {
  193. sev->replace = ctrls_replace;
  194. sev->merge = ctrls_merge;
  195. }
  196. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  197. found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
  198. if (!found_ev)
  199. list_add(&sev->list, &fh->subscribed);
  200. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  201. /* v4l2_ctrl_add_event uses a mutex, so do this outside the spin lock */
  202. if (found_ev)
  203. kfree(sev);
  204. else if (ctrl)
  205. v4l2_ctrl_add_event(ctrl, sev);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
  209. void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
  210. {
  211. struct v4l2_event_subscription sub;
  212. struct v4l2_subscribed_event *sev;
  213. unsigned long flags;
  214. do {
  215. sev = NULL;
  216. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  217. if (!list_empty(&fh->subscribed)) {
  218. sev = list_first_entry(&fh->subscribed,
  219. struct v4l2_subscribed_event, list);
  220. sub.type = sev->type;
  221. sub.id = sev->id;
  222. }
  223. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  224. if (sev)
  225. v4l2_event_unsubscribe(fh, &sub);
  226. } while (sev);
  227. }
  228. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
  229. int v4l2_event_unsubscribe(struct v4l2_fh *fh,
  230. struct v4l2_event_subscription *sub)
  231. {
  232. struct v4l2_subscribed_event *sev;
  233. unsigned long flags;
  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. list_del(&sev->list);
  242. sev->fh = NULL;
  243. }
  244. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  245. if (sev && sev->type == V4L2_EVENT_CTRL) {
  246. struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler, sev->id);
  247. if (ctrl)
  248. v4l2_ctrl_del_event(ctrl, sev);
  249. }
  250. kfree(sev);
  251. return 0;
  252. }
  253. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);