v4l2-event.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #include <linux/export.h>
  31. static unsigned sev_pos(const struct v4l2_subscribed_event *sev, unsigned idx)
  32. {
  33. idx += sev->first;
  34. return idx >= sev->elems ? idx - sev->elems : idx;
  35. }
  36. static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
  37. {
  38. struct v4l2_kevent *kev;
  39. unsigned long flags;
  40. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  41. if (list_empty(&fh->available)) {
  42. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  43. return -ENOENT;
  44. }
  45. WARN_ON(fh->navailable == 0);
  46. kev = list_first_entry(&fh->available, struct v4l2_kevent, list);
  47. list_del(&kev->list);
  48. fh->navailable--;
  49. kev->event.pending = fh->navailable;
  50. *event = kev->event;
  51. kev->sev->first = sev_pos(kev->sev, 1);
  52. kev->sev->in_use--;
  53. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  54. return 0;
  55. }
  56. int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
  57. int nonblocking)
  58. {
  59. int ret;
  60. if (nonblocking)
  61. return __v4l2_event_dequeue(fh, event);
  62. /* Release the vdev lock while waiting */
  63. if (fh->vdev->lock)
  64. mutex_unlock(fh->vdev->lock);
  65. do {
  66. ret = wait_event_interruptible(fh->wait,
  67. fh->navailable != 0);
  68. if (ret < 0)
  69. break;
  70. ret = __v4l2_event_dequeue(fh, event);
  71. } while (ret == -ENOENT);
  72. if (fh->vdev->lock)
  73. mutex_lock(fh->vdev->lock);
  74. return ret;
  75. }
  76. EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
  77. /* Caller must hold fh->vdev->fh_lock! */
  78. static struct v4l2_subscribed_event *v4l2_event_subscribed(
  79. struct v4l2_fh *fh, u32 type, u32 id)
  80. {
  81. struct v4l2_subscribed_event *sev;
  82. assert_spin_locked(&fh->vdev->fh_lock);
  83. list_for_each_entry(sev, &fh->subscribed, list)
  84. if (sev->type == type && sev->id == id)
  85. return sev;
  86. return NULL;
  87. }
  88. static void __v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev,
  89. const struct timespec *ts)
  90. {
  91. struct v4l2_subscribed_event *sev;
  92. struct v4l2_kevent *kev;
  93. bool copy_payload = true;
  94. /* Are we subscribed? */
  95. sev = v4l2_event_subscribed(fh, ev->type, ev->id);
  96. if (sev == NULL)
  97. return;
  98. /* Increase event sequence number on fh. */
  99. fh->sequence++;
  100. /* Do we have any free events? */
  101. if (sev->in_use == sev->elems) {
  102. /* no, remove the oldest one */
  103. kev = sev->events + sev_pos(sev, 0);
  104. list_del(&kev->list);
  105. sev->in_use--;
  106. sev->first = sev_pos(sev, 1);
  107. fh->navailable--;
  108. if (sev->elems == 1) {
  109. if (sev->replace) {
  110. sev->replace(&kev->event, ev);
  111. copy_payload = false;
  112. }
  113. } else if (sev->merge) {
  114. struct v4l2_kevent *second_oldest =
  115. sev->events + sev_pos(sev, 0);
  116. sev->merge(&kev->event, &second_oldest->event);
  117. }
  118. }
  119. /* Take one and fill it. */
  120. kev = sev->events + sev_pos(sev, sev->in_use);
  121. kev->event.type = ev->type;
  122. if (copy_payload)
  123. kev->event.u = ev->u;
  124. kev->event.id = ev->id;
  125. kev->event.timestamp = *ts;
  126. kev->event.sequence = fh->sequence;
  127. sev->in_use++;
  128. list_add_tail(&kev->list, &fh->available);
  129. fh->navailable++;
  130. wake_up_all(&fh->wait);
  131. }
  132. void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
  133. {
  134. struct v4l2_fh *fh;
  135. unsigned long flags;
  136. struct timespec timestamp;
  137. ktime_get_ts(&timestamp);
  138. spin_lock_irqsave(&vdev->fh_lock, flags);
  139. list_for_each_entry(fh, &vdev->fh_list, list)
  140. __v4l2_event_queue_fh(fh, ev, &timestamp);
  141. spin_unlock_irqrestore(&vdev->fh_lock, flags);
  142. }
  143. EXPORT_SYMBOL_GPL(v4l2_event_queue);
  144. void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
  145. {
  146. unsigned long flags;
  147. struct timespec timestamp;
  148. ktime_get_ts(&timestamp);
  149. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  150. __v4l2_event_queue_fh(fh, ev, &timestamp);
  151. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  152. }
  153. EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
  154. int v4l2_event_pending(struct v4l2_fh *fh)
  155. {
  156. return fh->navailable;
  157. }
  158. EXPORT_SYMBOL_GPL(v4l2_event_pending);
  159. static void ctrls_replace(struct v4l2_event *old, const struct v4l2_event *new)
  160. {
  161. u32 old_changes = old->u.ctrl.changes;
  162. old->u.ctrl = new->u.ctrl;
  163. old->u.ctrl.changes |= old_changes;
  164. }
  165. static void ctrls_merge(const struct v4l2_event *old, struct v4l2_event *new)
  166. {
  167. new->u.ctrl.changes |= old->u.ctrl.changes;
  168. }
  169. int v4l2_event_subscribe(struct v4l2_fh *fh,
  170. struct v4l2_event_subscription *sub, unsigned elems)
  171. {
  172. struct v4l2_subscribed_event *sev, *found_ev;
  173. struct v4l2_ctrl *ctrl = NULL;
  174. unsigned long flags;
  175. unsigned i;
  176. if (sub->type == V4L2_EVENT_ALL)
  177. return -EINVAL;
  178. if (elems < 1)
  179. elems = 1;
  180. if (sub->type == V4L2_EVENT_CTRL) {
  181. ctrl = v4l2_ctrl_find(fh->ctrl_handler, sub->id);
  182. if (ctrl == NULL)
  183. return -EINVAL;
  184. }
  185. sev = kzalloc(sizeof(*sev) + sizeof(struct v4l2_kevent) * elems, GFP_KERNEL);
  186. if (!sev)
  187. return -ENOMEM;
  188. for (i = 0; i < elems; i++)
  189. sev->events[i].sev = sev;
  190. sev->type = sub->type;
  191. sev->id = sub->id;
  192. sev->flags = sub->flags;
  193. sev->fh = fh;
  194. sev->elems = elems;
  195. if (ctrl) {
  196. sev->replace = ctrls_replace;
  197. sev->merge = ctrls_merge;
  198. }
  199. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  200. found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
  201. if (!found_ev)
  202. list_add(&sev->list, &fh->subscribed);
  203. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  204. /* v4l2_ctrl_add_event uses a mutex, so do this outside the spin lock */
  205. if (found_ev)
  206. kfree(sev);
  207. else if (ctrl)
  208. v4l2_ctrl_add_event(ctrl, sev);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
  212. void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
  213. {
  214. struct v4l2_event_subscription sub;
  215. struct v4l2_subscribed_event *sev;
  216. unsigned long flags;
  217. do {
  218. sev = NULL;
  219. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  220. if (!list_empty(&fh->subscribed)) {
  221. sev = list_first_entry(&fh->subscribed,
  222. struct v4l2_subscribed_event, list);
  223. sub.type = sev->type;
  224. sub.id = sev->id;
  225. }
  226. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  227. if (sev)
  228. v4l2_event_unsubscribe(fh, &sub);
  229. } while (sev);
  230. }
  231. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
  232. int v4l2_event_unsubscribe(struct v4l2_fh *fh,
  233. struct v4l2_event_subscription *sub)
  234. {
  235. struct v4l2_subscribed_event *sev;
  236. unsigned long flags;
  237. int i;
  238. if (sub->type == V4L2_EVENT_ALL) {
  239. v4l2_event_unsubscribe_all(fh);
  240. return 0;
  241. }
  242. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  243. sev = v4l2_event_subscribed(fh, sub->type, sub->id);
  244. if (sev != NULL) {
  245. /* Remove any pending events for this subscription */
  246. for (i = 0; i < sev->in_use; i++) {
  247. list_del(&sev->events[sev_pos(sev, i)].list);
  248. fh->navailable--;
  249. }
  250. list_del(&sev->list);
  251. }
  252. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  253. if (sev && sev->type == V4L2_EVENT_CTRL) {
  254. struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler, sev->id);
  255. if (ctrl)
  256. v4l2_ctrl_del_event(ctrl, sev);
  257. }
  258. kfree(sev);
  259. return 0;
  260. }
  261. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);