v4l2-event.c 8.3 KB

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