v4l2-event.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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(struct_size(sev, events, elems), GFP_KERNEL);
  175. if (!sev)
  176. return -ENOMEM;
  177. for (i = 0; i < elems; i++)
  178. sev->events[i].sev = sev;
  179. sev->type = sub->type;
  180. sev->id = sub->id;
  181. sev->flags = sub->flags;
  182. sev->fh = fh;
  183. sev->ops = ops;
  184. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  185. found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
  186. if (!found_ev)
  187. list_add(&sev->list, &fh->subscribed);
  188. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  189. if (found_ev) {
  190. kvfree(sev);
  191. return 0; /* Already listening */
  192. }
  193. if (sev->ops && sev->ops->add) {
  194. int ret = sev->ops->add(sev, elems);
  195. if (ret) {
  196. sev->ops = NULL;
  197. v4l2_event_unsubscribe(fh, sub);
  198. return ret;
  199. }
  200. }
  201. /* Mark as ready for use */
  202. sev->elems = elems;
  203. return 0;
  204. }
  205. EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
  206. void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
  207. {
  208. struct v4l2_event_subscription sub;
  209. struct v4l2_subscribed_event *sev;
  210. unsigned long flags;
  211. do {
  212. sev = NULL;
  213. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  214. if (!list_empty(&fh->subscribed)) {
  215. sev = list_first_entry(&fh->subscribed,
  216. struct v4l2_subscribed_event, list);
  217. sub.type = sev->type;
  218. sub.id = sev->id;
  219. }
  220. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  221. if (sev)
  222. v4l2_event_unsubscribe(fh, &sub);
  223. } while (sev);
  224. }
  225. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
  226. int v4l2_event_unsubscribe(struct v4l2_fh *fh,
  227. const struct v4l2_event_subscription *sub)
  228. {
  229. struct v4l2_subscribed_event *sev;
  230. unsigned long flags;
  231. int i;
  232. if (sub->type == V4L2_EVENT_ALL) {
  233. v4l2_event_unsubscribe_all(fh);
  234. return 0;
  235. }
  236. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  237. sev = v4l2_event_subscribed(fh, sub->type, sub->id);
  238. if (sev != NULL) {
  239. /* Remove any pending events for this subscription */
  240. for (i = 0; i < sev->in_use; i++) {
  241. list_del(&sev->events[sev_pos(sev, i)].list);
  242. fh->navailable--;
  243. }
  244. list_del(&sev->list);
  245. }
  246. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  247. if (sev && sev->ops && sev->ops->del)
  248. sev->ops->del(sev);
  249. kvfree(sev);
  250. return 0;
  251. }
  252. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);
  253. int v4l2_event_subdev_unsubscribe(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  254. struct v4l2_event_subscription *sub)
  255. {
  256. return v4l2_event_unsubscribe(fh, sub);
  257. }
  258. EXPORT_SYMBOL_GPL(v4l2_event_subdev_unsubscribe);
  259. static void v4l2_event_src_replace(struct v4l2_event *old,
  260. const struct v4l2_event *new)
  261. {
  262. u32 old_changes = old->u.src_change.changes;
  263. old->u.src_change = new->u.src_change;
  264. old->u.src_change.changes |= old_changes;
  265. }
  266. static void v4l2_event_src_merge(const struct v4l2_event *old,
  267. struct v4l2_event *new)
  268. {
  269. new->u.src_change.changes |= old->u.src_change.changes;
  270. }
  271. static const struct v4l2_subscribed_event_ops v4l2_event_src_ch_ops = {
  272. .replace = v4l2_event_src_replace,
  273. .merge = v4l2_event_src_merge,
  274. };
  275. int v4l2_src_change_event_subscribe(struct v4l2_fh *fh,
  276. const struct v4l2_event_subscription *sub)
  277. {
  278. if (sub->type == V4L2_EVENT_SOURCE_CHANGE)
  279. return v4l2_event_subscribe(fh, sub, 0, &v4l2_event_src_ch_ops);
  280. return -EINVAL;
  281. }
  282. EXPORT_SYMBOL_GPL(v4l2_src_change_event_subscribe);
  283. int v4l2_src_change_event_subdev_subscribe(struct v4l2_subdev *sd,
  284. struct v4l2_fh *fh, struct v4l2_event_subscription *sub)
  285. {
  286. return v4l2_src_change_event_subscribe(fh, sub);
  287. }
  288. EXPORT_SYMBOL_GPL(v4l2_src_change_event_subdev_subscribe);