v4l2-event.c 8.5 KB

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