media-request.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Media device request objects
  4. *
  5. * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. * Copyright (C) 2018 Intel Corporation
  7. * Copyright (C) 2018 Google, Inc.
  8. *
  9. * Author: Hans Verkuil <hans.verkuil@cisco.com>
  10. * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
  11. */
  12. #include <linux/anon_inodes.h>
  13. #include <linux/file.h>
  14. #include <linux/refcount.h>
  15. #include <media/media-device.h>
  16. #include <media/media-request.h>
  17. static const char * const request_state[] = {
  18. [MEDIA_REQUEST_STATE_IDLE] = "idle",
  19. [MEDIA_REQUEST_STATE_VALIDATING] = "validating",
  20. [MEDIA_REQUEST_STATE_QUEUED] = "queued",
  21. [MEDIA_REQUEST_STATE_COMPLETE] = "complete",
  22. [MEDIA_REQUEST_STATE_CLEANING] = "cleaning",
  23. [MEDIA_REQUEST_STATE_UPDATING] = "updating",
  24. };
  25. static const char *
  26. media_request_state_str(enum media_request_state state)
  27. {
  28. BUILD_BUG_ON(ARRAY_SIZE(request_state) != NR_OF_MEDIA_REQUEST_STATE);
  29. if (WARN_ON(state >= ARRAY_SIZE(request_state)))
  30. return "invalid";
  31. return request_state[state];
  32. }
  33. static void media_request_clean(struct media_request *req)
  34. {
  35. struct media_request_object *obj, *obj_safe;
  36. /* Just a sanity check. No other code path is allowed to change this. */
  37. WARN_ON(req->state != MEDIA_REQUEST_STATE_CLEANING);
  38. WARN_ON(req->updating_count);
  39. WARN_ON(req->access_count);
  40. list_for_each_entry_safe(obj, obj_safe, &req->objects, list) {
  41. media_request_object_unbind(obj);
  42. media_request_object_put(obj);
  43. }
  44. req->updating_count = 0;
  45. req->access_count = 0;
  46. WARN_ON(req->num_incomplete_objects);
  47. req->num_incomplete_objects = 0;
  48. wake_up_interruptible_all(&req->poll_wait);
  49. }
  50. static void media_request_release(struct kref *kref)
  51. {
  52. struct media_request *req =
  53. container_of(kref, struct media_request, kref);
  54. struct media_device *mdev = req->mdev;
  55. dev_dbg(mdev->dev, "request: release %s\n", req->debug_str);
  56. /* No other users, no need for a spinlock */
  57. req->state = MEDIA_REQUEST_STATE_CLEANING;
  58. media_request_clean(req);
  59. if (mdev->ops->req_free)
  60. mdev->ops->req_free(req);
  61. else
  62. kfree(req);
  63. }
  64. void media_request_put(struct media_request *req)
  65. {
  66. kref_put(&req->kref, media_request_release);
  67. }
  68. EXPORT_SYMBOL_GPL(media_request_put);
  69. static int media_request_close(struct inode *inode, struct file *filp)
  70. {
  71. struct media_request *req = filp->private_data;
  72. media_request_put(req);
  73. return 0;
  74. }
  75. static __poll_t media_request_poll(struct file *filp,
  76. struct poll_table_struct *wait)
  77. {
  78. struct media_request *req = filp->private_data;
  79. unsigned long flags;
  80. __poll_t ret = 0;
  81. if (!(poll_requested_events(wait) & EPOLLPRI))
  82. return 0;
  83. spin_lock_irqsave(&req->lock, flags);
  84. if (req->state == MEDIA_REQUEST_STATE_COMPLETE) {
  85. ret = EPOLLPRI;
  86. goto unlock;
  87. }
  88. if (req->state != MEDIA_REQUEST_STATE_QUEUED) {
  89. ret = EPOLLERR;
  90. goto unlock;
  91. }
  92. poll_wait(filp, &req->poll_wait, wait);
  93. unlock:
  94. spin_unlock_irqrestore(&req->lock, flags);
  95. return ret;
  96. }
  97. static long media_request_ioctl_queue(struct media_request *req)
  98. {
  99. struct media_device *mdev = req->mdev;
  100. enum media_request_state state;
  101. unsigned long flags;
  102. int ret;
  103. dev_dbg(mdev->dev, "request: queue %s\n", req->debug_str);
  104. /*
  105. * Ensure the request that is validated will be the one that gets queued
  106. * next by serialising the queueing process. This mutex is also used
  107. * to serialize with canceling a vb2 queue and with setting values such
  108. * as controls in a request.
  109. */
  110. mutex_lock(&mdev->req_queue_mutex);
  111. media_request_get(req);
  112. spin_lock_irqsave(&req->lock, flags);
  113. if (req->state == MEDIA_REQUEST_STATE_IDLE)
  114. req->state = MEDIA_REQUEST_STATE_VALIDATING;
  115. state = req->state;
  116. spin_unlock_irqrestore(&req->lock, flags);
  117. if (state != MEDIA_REQUEST_STATE_VALIDATING) {
  118. dev_dbg(mdev->dev,
  119. "request: unable to queue %s, request in state %s\n",
  120. req->debug_str, media_request_state_str(state));
  121. media_request_put(req);
  122. mutex_unlock(&mdev->req_queue_mutex);
  123. return -EBUSY;
  124. }
  125. ret = mdev->ops->req_validate(req);
  126. /*
  127. * If the req_validate was successful, then we mark the state as QUEUED
  128. * and call req_queue. The reason we set the state first is that this
  129. * allows req_queue to unbind or complete the queued objects in case
  130. * they are immediately 'consumed'. State changes from QUEUED to another
  131. * state can only happen if either the driver changes the state or if
  132. * the user cancels the vb2 queue. The driver can only change the state
  133. * after each object is queued through the req_queue op (and note that
  134. * that op cannot fail), so setting the state to QUEUED up front is
  135. * safe.
  136. *
  137. * The other reason for changing the state is if the vb2 queue is
  138. * canceled, and that uses the req_queue_mutex which is still locked
  139. * while req_queue is called, so that's safe as well.
  140. */
  141. spin_lock_irqsave(&req->lock, flags);
  142. req->state = ret ? MEDIA_REQUEST_STATE_IDLE
  143. : MEDIA_REQUEST_STATE_QUEUED;
  144. spin_unlock_irqrestore(&req->lock, flags);
  145. if (!ret)
  146. mdev->ops->req_queue(req);
  147. mutex_unlock(&mdev->req_queue_mutex);
  148. if (ret) {
  149. dev_dbg(mdev->dev, "request: can't queue %s (%d)\n",
  150. req->debug_str, ret);
  151. media_request_put(req);
  152. }
  153. return ret;
  154. }
  155. static long media_request_ioctl_reinit(struct media_request *req)
  156. {
  157. struct media_device *mdev = req->mdev;
  158. unsigned long flags;
  159. spin_lock_irqsave(&req->lock, flags);
  160. if (req->state != MEDIA_REQUEST_STATE_IDLE &&
  161. req->state != MEDIA_REQUEST_STATE_COMPLETE) {
  162. dev_dbg(mdev->dev,
  163. "request: %s not in idle or complete state, cannot reinit\n",
  164. req->debug_str);
  165. spin_unlock_irqrestore(&req->lock, flags);
  166. return -EBUSY;
  167. }
  168. if (req->access_count) {
  169. dev_dbg(mdev->dev,
  170. "request: %s is being accessed, cannot reinit\n",
  171. req->debug_str);
  172. spin_unlock_irqrestore(&req->lock, flags);
  173. return -EBUSY;
  174. }
  175. req->state = MEDIA_REQUEST_STATE_CLEANING;
  176. spin_unlock_irqrestore(&req->lock, flags);
  177. media_request_clean(req);
  178. spin_lock_irqsave(&req->lock, flags);
  179. req->state = MEDIA_REQUEST_STATE_IDLE;
  180. spin_unlock_irqrestore(&req->lock, flags);
  181. return 0;
  182. }
  183. static long media_request_ioctl(struct file *filp, unsigned int cmd,
  184. unsigned long arg)
  185. {
  186. struct media_request *req = filp->private_data;
  187. switch (cmd) {
  188. case MEDIA_REQUEST_IOC_QUEUE:
  189. return media_request_ioctl_queue(req);
  190. case MEDIA_REQUEST_IOC_REINIT:
  191. return media_request_ioctl_reinit(req);
  192. default:
  193. return -ENOIOCTLCMD;
  194. }
  195. }
  196. static const struct file_operations request_fops = {
  197. .owner = THIS_MODULE,
  198. .poll = media_request_poll,
  199. .unlocked_ioctl = media_request_ioctl,
  200. #ifdef CONFIG_COMPAT
  201. .compat_ioctl = media_request_ioctl,
  202. #endif /* CONFIG_COMPAT */
  203. .release = media_request_close,
  204. };
  205. struct media_request *
  206. media_request_get_by_fd(struct media_device *mdev, int request_fd)
  207. {
  208. struct file *filp;
  209. struct media_request *req;
  210. if (!mdev || !mdev->ops ||
  211. !mdev->ops->req_validate || !mdev->ops->req_queue)
  212. return ERR_PTR(-EACCES);
  213. filp = fget(request_fd);
  214. if (!filp)
  215. goto err_no_req_fd;
  216. if (filp->f_op != &request_fops)
  217. goto err_fput;
  218. req = filp->private_data;
  219. if (req->mdev != mdev)
  220. goto err_fput;
  221. /*
  222. * Note: as long as someone has an open filehandle of the request,
  223. * the request can never be released. The fget() above ensures that
  224. * even if userspace closes the request filehandle, the release()
  225. * fop won't be called, so the media_request_get() always succeeds
  226. * and there is no race condition where the request was released
  227. * before media_request_get() is called.
  228. */
  229. media_request_get(req);
  230. fput(filp);
  231. return req;
  232. err_fput:
  233. fput(filp);
  234. err_no_req_fd:
  235. dev_dbg(mdev->dev, "cannot find request_fd %d\n", request_fd);
  236. return ERR_PTR(-EINVAL);
  237. }
  238. EXPORT_SYMBOL_GPL(media_request_get_by_fd);
  239. int media_request_alloc(struct media_device *mdev, int *alloc_fd)
  240. {
  241. struct media_request *req;
  242. struct file *filp;
  243. int fd;
  244. int ret;
  245. /* Either both are NULL or both are non-NULL */
  246. if (WARN_ON(!mdev->ops->req_alloc ^ !mdev->ops->req_free))
  247. return -ENOMEM;
  248. fd = get_unused_fd_flags(O_CLOEXEC);
  249. if (fd < 0)
  250. return fd;
  251. filp = anon_inode_getfile("request", &request_fops, NULL, O_CLOEXEC);
  252. if (IS_ERR(filp)) {
  253. ret = PTR_ERR(filp);
  254. goto err_put_fd;
  255. }
  256. if (mdev->ops->req_alloc)
  257. req = mdev->ops->req_alloc(mdev);
  258. else
  259. req = kzalloc(sizeof(*req), GFP_KERNEL);
  260. if (!req) {
  261. ret = -ENOMEM;
  262. goto err_fput;
  263. }
  264. filp->private_data = req;
  265. req->mdev = mdev;
  266. req->state = MEDIA_REQUEST_STATE_IDLE;
  267. req->num_incomplete_objects = 0;
  268. kref_init(&req->kref);
  269. INIT_LIST_HEAD(&req->objects);
  270. spin_lock_init(&req->lock);
  271. init_waitqueue_head(&req->poll_wait);
  272. req->updating_count = 0;
  273. req->access_count = 0;
  274. *alloc_fd = fd;
  275. snprintf(req->debug_str, sizeof(req->debug_str), "%u:%d",
  276. atomic_inc_return(&mdev->request_id), fd);
  277. dev_dbg(mdev->dev, "request: allocated %s\n", req->debug_str);
  278. fd_install(fd, filp);
  279. return 0;
  280. err_fput:
  281. fput(filp);
  282. err_put_fd:
  283. put_unused_fd(fd);
  284. return ret;
  285. }
  286. static void media_request_object_release(struct kref *kref)
  287. {
  288. struct media_request_object *obj =
  289. container_of(kref, struct media_request_object, kref);
  290. struct media_request *req = obj->req;
  291. if (WARN_ON(req))
  292. media_request_object_unbind(obj);
  293. obj->ops->release(obj);
  294. }
  295. struct media_request_object *
  296. media_request_object_find(struct media_request *req,
  297. const struct media_request_object_ops *ops,
  298. void *priv)
  299. {
  300. struct media_request_object *obj;
  301. struct media_request_object *found = NULL;
  302. unsigned long flags;
  303. if (WARN_ON(!ops || !priv))
  304. return NULL;
  305. spin_lock_irqsave(&req->lock, flags);
  306. list_for_each_entry(obj, &req->objects, list) {
  307. if (obj->ops == ops && obj->priv == priv) {
  308. media_request_object_get(obj);
  309. found = obj;
  310. break;
  311. }
  312. }
  313. spin_unlock_irqrestore(&req->lock, flags);
  314. return found;
  315. }
  316. EXPORT_SYMBOL_GPL(media_request_object_find);
  317. void media_request_object_put(struct media_request_object *obj)
  318. {
  319. kref_put(&obj->kref, media_request_object_release);
  320. }
  321. EXPORT_SYMBOL_GPL(media_request_object_put);
  322. void media_request_object_init(struct media_request_object *obj)
  323. {
  324. obj->ops = NULL;
  325. obj->req = NULL;
  326. obj->priv = NULL;
  327. obj->completed = false;
  328. INIT_LIST_HEAD(&obj->list);
  329. kref_init(&obj->kref);
  330. }
  331. EXPORT_SYMBOL_GPL(media_request_object_init);
  332. int media_request_object_bind(struct media_request *req,
  333. const struct media_request_object_ops *ops,
  334. void *priv, bool is_buffer,
  335. struct media_request_object *obj)
  336. {
  337. unsigned long flags;
  338. int ret = -EBUSY;
  339. if (WARN_ON(!ops->release))
  340. return -EACCES;
  341. spin_lock_irqsave(&req->lock, flags);
  342. if (WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING))
  343. goto unlock;
  344. obj->req = req;
  345. obj->ops = ops;
  346. obj->priv = priv;
  347. if (is_buffer)
  348. list_add_tail(&obj->list, &req->objects);
  349. else
  350. list_add(&obj->list, &req->objects);
  351. req->num_incomplete_objects++;
  352. ret = 0;
  353. unlock:
  354. spin_unlock_irqrestore(&req->lock, flags);
  355. return ret;
  356. }
  357. EXPORT_SYMBOL_GPL(media_request_object_bind);
  358. void media_request_object_unbind(struct media_request_object *obj)
  359. {
  360. struct media_request *req = obj->req;
  361. unsigned long flags;
  362. bool completed = false;
  363. if (WARN_ON(!req))
  364. return;
  365. spin_lock_irqsave(&req->lock, flags);
  366. list_del(&obj->list);
  367. obj->req = NULL;
  368. if (req->state == MEDIA_REQUEST_STATE_COMPLETE)
  369. goto unlock;
  370. if (WARN_ON(req->state == MEDIA_REQUEST_STATE_VALIDATING))
  371. goto unlock;
  372. if (req->state == MEDIA_REQUEST_STATE_CLEANING) {
  373. if (!obj->completed)
  374. req->num_incomplete_objects--;
  375. goto unlock;
  376. }
  377. if (WARN_ON(!req->num_incomplete_objects))
  378. goto unlock;
  379. req->num_incomplete_objects--;
  380. if (req->state == MEDIA_REQUEST_STATE_QUEUED &&
  381. !req->num_incomplete_objects) {
  382. req->state = MEDIA_REQUEST_STATE_COMPLETE;
  383. completed = true;
  384. wake_up_interruptible_all(&req->poll_wait);
  385. }
  386. unlock:
  387. spin_unlock_irqrestore(&req->lock, flags);
  388. if (obj->ops->unbind)
  389. obj->ops->unbind(obj);
  390. if (completed)
  391. media_request_put(req);
  392. }
  393. EXPORT_SYMBOL_GPL(media_request_object_unbind);
  394. void media_request_object_complete(struct media_request_object *obj)
  395. {
  396. struct media_request *req = obj->req;
  397. unsigned long flags;
  398. bool completed = false;
  399. spin_lock_irqsave(&req->lock, flags);
  400. if (obj->completed)
  401. goto unlock;
  402. obj->completed = true;
  403. if (WARN_ON(!req->num_incomplete_objects) ||
  404. WARN_ON(req->state != MEDIA_REQUEST_STATE_QUEUED))
  405. goto unlock;
  406. if (!--req->num_incomplete_objects) {
  407. req->state = MEDIA_REQUEST_STATE_COMPLETE;
  408. wake_up_interruptible_all(&req->poll_wait);
  409. completed = true;
  410. }
  411. unlock:
  412. spin_unlock_irqrestore(&req->lock, flags);
  413. if (completed)
  414. media_request_put(req);
  415. }
  416. EXPORT_SYMBOL_GPL(media_request_object_complete);