v4l2-event.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. /* Increase event sequence number on fh. */
  94. fh->sequence++;
  95. /* Do we have any free events? */
  96. if (sev->in_use == sev->elems) {
  97. /* no, remove the oldest one */
  98. kev = sev->events + sev_pos(sev, 0);
  99. list_del(&kev->list);
  100. sev->in_use--;
  101. sev->first = sev_pos(sev, 1);
  102. fh->navailable--;
  103. if (sev->elems == 1) {
  104. if (sev->ops && sev->ops->replace) {
  105. sev->ops->replace(&kev->event, ev);
  106. copy_payload = false;
  107. }
  108. } else if (sev->ops && sev->ops->merge) {
  109. struct v4l2_kevent *second_oldest =
  110. sev->events + sev_pos(sev, 0);
  111. sev->ops->merge(&kev->event, &second_oldest->event);
  112. }
  113. }
  114. /* Take one and fill it. */
  115. kev = sev->events + sev_pos(sev, sev->in_use);
  116. kev->event.type = ev->type;
  117. if (copy_payload)
  118. kev->event.u = ev->u;
  119. kev->event.id = ev->id;
  120. kev->event.timestamp = *ts;
  121. kev->event.sequence = fh->sequence;
  122. sev->in_use++;
  123. list_add_tail(&kev->list, &fh->available);
  124. fh->navailable++;
  125. wake_up_all(&fh->wait);
  126. }
  127. void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
  128. {
  129. struct v4l2_fh *fh;
  130. unsigned long flags;
  131. struct timespec timestamp;
  132. if (vdev == NULL)
  133. return;
  134. ktime_get_ts(&timestamp);
  135. spin_lock_irqsave(&vdev->fh_lock, flags);
  136. list_for_each_entry(fh, &vdev->fh_list, list)
  137. __v4l2_event_queue_fh(fh, ev, &timestamp);
  138. spin_unlock_irqrestore(&vdev->fh_lock, flags);
  139. }
  140. EXPORT_SYMBOL_GPL(v4l2_event_queue);
  141. void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
  142. {
  143. unsigned long flags;
  144. struct timespec timestamp;
  145. ktime_get_ts(&timestamp);
  146. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  147. __v4l2_event_queue_fh(fh, ev, &timestamp);
  148. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  149. }
  150. EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
  151. int v4l2_event_pending(struct v4l2_fh *fh)
  152. {
  153. return fh->navailable;
  154. }
  155. EXPORT_SYMBOL_GPL(v4l2_event_pending);
  156. int v4l2_event_subscribe(struct v4l2_fh *fh,
  157. const struct v4l2_event_subscription *sub, unsigned elems,
  158. const struct v4l2_subscribed_event_ops *ops)
  159. {
  160. struct v4l2_subscribed_event *sev, *found_ev;
  161. unsigned long flags;
  162. unsigned i;
  163. int ret = 0;
  164. if (sub->type == V4L2_EVENT_ALL)
  165. return -EINVAL;
  166. if (elems < 1)
  167. elems = 1;
  168. sev = kvzalloc(struct_size(sev, events, elems), GFP_KERNEL);
  169. if (!sev)
  170. return -ENOMEM;
  171. for (i = 0; i < elems; i++)
  172. sev->events[i].sev = sev;
  173. sev->type = sub->type;
  174. sev->id = sub->id;
  175. sev->flags = sub->flags;
  176. sev->fh = fh;
  177. sev->ops = ops;
  178. sev->elems = elems;
  179. mutex_lock(&fh->subscribe_lock);
  180. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  181. found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
  182. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  183. if (found_ev) {
  184. /* Already listening */
  185. kvfree(sev);
  186. goto out_unlock;
  187. }
  188. if (sev->ops && sev->ops->add) {
  189. ret = sev->ops->add(sev, elems);
  190. if (ret) {
  191. kvfree(sev);
  192. goto out_unlock;
  193. }
  194. }
  195. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  196. list_add(&sev->list, &fh->subscribed);
  197. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  198. out_unlock:
  199. mutex_unlock(&fh->subscribe_lock);
  200. return ret;
  201. }
  202. EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
  203. void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
  204. {
  205. struct v4l2_event_subscription sub;
  206. struct v4l2_subscribed_event *sev;
  207. unsigned long flags;
  208. do {
  209. sev = NULL;
  210. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  211. if (!list_empty(&fh->subscribed)) {
  212. sev = list_first_entry(&fh->subscribed,
  213. struct v4l2_subscribed_event, list);
  214. sub.type = sev->type;
  215. sub.id = sev->id;
  216. }
  217. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  218. if (sev)
  219. v4l2_event_unsubscribe(fh, &sub);
  220. } while (sev);
  221. }
  222. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
  223. int v4l2_event_unsubscribe(struct v4l2_fh *fh,
  224. const struct v4l2_event_subscription *sub)
  225. {
  226. struct v4l2_subscribed_event *sev;
  227. unsigned long flags;
  228. int i;
  229. if (sub->type == V4L2_EVENT_ALL) {
  230. v4l2_event_unsubscribe_all(fh);
  231. return 0;
  232. }
  233. mutex_lock(&fh->subscribe_lock);
  234. spin_lock_irqsave(&fh->vdev->fh_lock, flags);
  235. sev = v4l2_event_subscribed(fh, sub->type, sub->id);
  236. if (sev != NULL) {
  237. /* Remove any pending events for this subscription */
  238. for (i = 0; i < sev->in_use; i++) {
  239. list_del(&sev->events[sev_pos(sev, i)].list);
  240. fh->navailable--;
  241. }
  242. list_del(&sev->list);
  243. }
  244. spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
  245. if (sev && sev->ops && sev->ops->del)
  246. sev->ops->del(sev);
  247. mutex_unlock(&fh->subscribe_lock);
  248. kvfree(sev);
  249. return 0;
  250. }
  251. EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);
  252. int v4l2_event_subdev_unsubscribe(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  253. struct v4l2_event_subscription *sub)
  254. {
  255. return v4l2_event_unsubscribe(fh, sub);
  256. }
  257. EXPORT_SYMBOL_GPL(v4l2_event_subdev_unsubscribe);
  258. static void v4l2_event_src_replace(struct v4l2_event *old,
  259. const struct v4l2_event *new)
  260. {
  261. u32 old_changes = old->u.src_change.changes;
  262. old->u.src_change = new->u.src_change;
  263. old->u.src_change.changes |= old_changes;
  264. }
  265. static void v4l2_event_src_merge(const struct v4l2_event *old,
  266. struct v4l2_event *new)
  267. {
  268. new->u.src_change.changes |= old->u.src_change.changes;
  269. }
  270. static const struct v4l2_subscribed_event_ops v4l2_event_src_ch_ops = {
  271. .replace = v4l2_event_src_replace,
  272. .merge = v4l2_event_src_merge,
  273. };
  274. int v4l2_src_change_event_subscribe(struct v4l2_fh *fh,
  275. const struct v4l2_event_subscription *sub)
  276. {
  277. if (sub->type == V4L2_EVENT_SOURCE_CHANGE)
  278. return v4l2_event_subscribe(fh, sub, 0, &v4l2_event_src_ch_ops);
  279. return -EINVAL;
  280. }
  281. EXPORT_SYMBOL_GPL(v4l2_src_change_event_subscribe);
  282. int v4l2_src_change_event_subdev_subscribe(struct v4l2_subdev *sd,
  283. struct v4l2_fh *fh, struct v4l2_event_subscription *sub)
  284. {
  285. return v4l2_src_change_event_subscribe(fh, sub);
  286. }
  287. EXPORT_SYMBOL_GPL(v4l2_src_change_event_subdev_subscribe);