v4l2-event.c 8.7 KB

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