mc-request.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. poll_wait(filp, &req->poll_wait, wait);
  84. spin_lock_irqsave(&req->lock, flags);
  85. if (req->state == MEDIA_REQUEST_STATE_COMPLETE) {
  86. ret = EPOLLPRI;
  87. goto unlock;
  88. }
  89. if (req->state != MEDIA_REQUEST_STATE_QUEUED) {
  90. ret = EPOLLERR;
  91. goto unlock;
  92. }
  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 fd f;
  209. struct media_request *req;
  210. if (!mdev || !mdev->ops ||
  211. !mdev->ops->req_validate || !mdev->ops->req_queue)
  212. return ERR_PTR(-EBADR);
  213. f = fdget(request_fd);
  214. if (!f.file)
  215. goto err_no_req_fd;
  216. if (f.file->f_op != &request_fops)
  217. goto err_fput;
  218. req = f.file->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 fdget() 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. fdput(f);
  231. return req;
  232. err_fput:
  233. fdput(f);
  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. if (mdev->ops->req_alloc)
  249. req = mdev->ops->req_alloc(mdev);
  250. else
  251. req = kzalloc(sizeof(*req), GFP_KERNEL);
  252. if (!req)
  253. return -ENOMEM;
  254. fd = get_unused_fd_flags(O_CLOEXEC);
  255. if (fd < 0) {
  256. ret = fd;
  257. goto err_free_req;
  258. }
  259. filp = anon_inode_getfile("request", &request_fops, NULL, O_CLOEXEC);
  260. if (IS_ERR(filp)) {
  261. ret = PTR_ERR(filp);
  262. goto err_put_fd;
  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_put_fd:
  281. put_unused_fd(fd);
  282. err_free_req:
  283. if (mdev->ops->req_free)
  284. mdev->ops->req_free(req);
  285. else
  286. kfree(req);
  287. return ret;
  288. }
  289. static void media_request_object_release(struct kref *kref)
  290. {
  291. struct media_request_object *obj =
  292. container_of(kref, struct media_request_object, kref);
  293. struct media_request *req = obj->req;
  294. if (WARN_ON(req))
  295. media_request_object_unbind(obj);
  296. obj->ops->release(obj);
  297. }
  298. struct media_request_object *
  299. media_request_object_find(struct media_request *req,
  300. const struct media_request_object_ops *ops,
  301. void *priv)
  302. {
  303. struct media_request_object *obj;
  304. struct media_request_object *found = NULL;
  305. unsigned long flags;
  306. if (WARN_ON(!ops || !priv))
  307. return NULL;
  308. spin_lock_irqsave(&req->lock, flags);
  309. list_for_each_entry(obj, &req->objects, list) {
  310. if (obj->ops == ops && obj->priv == priv) {
  311. media_request_object_get(obj);
  312. found = obj;
  313. break;
  314. }
  315. }
  316. spin_unlock_irqrestore(&req->lock, flags);
  317. return found;
  318. }
  319. EXPORT_SYMBOL_GPL(media_request_object_find);
  320. void media_request_object_put(struct media_request_object *obj)
  321. {
  322. kref_put(&obj->kref, media_request_object_release);
  323. }
  324. EXPORT_SYMBOL_GPL(media_request_object_put);
  325. void media_request_object_init(struct media_request_object *obj)
  326. {
  327. obj->ops = NULL;
  328. obj->req = NULL;
  329. obj->priv = NULL;
  330. obj->completed = false;
  331. INIT_LIST_HEAD(&obj->list);
  332. kref_init(&obj->kref);
  333. }
  334. EXPORT_SYMBOL_GPL(media_request_object_init);
  335. int media_request_object_bind(struct media_request *req,
  336. const struct media_request_object_ops *ops,
  337. void *priv, bool is_buffer,
  338. struct media_request_object *obj)
  339. {
  340. unsigned long flags;
  341. int ret = -EBUSY;
  342. if (WARN_ON(!ops->release))
  343. return -EBADR;
  344. spin_lock_irqsave(&req->lock, flags);
  345. if (WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING))
  346. goto unlock;
  347. obj->req = req;
  348. obj->ops = ops;
  349. obj->priv = priv;
  350. if (is_buffer)
  351. list_add_tail(&obj->list, &req->objects);
  352. else
  353. list_add(&obj->list, &req->objects);
  354. req->num_incomplete_objects++;
  355. ret = 0;
  356. unlock:
  357. spin_unlock_irqrestore(&req->lock, flags);
  358. return ret;
  359. }
  360. EXPORT_SYMBOL_GPL(media_request_object_bind);
  361. void media_request_object_unbind(struct media_request_object *obj)
  362. {
  363. struct media_request *req = obj->req;
  364. unsigned long flags;
  365. bool completed = false;
  366. if (WARN_ON(!req))
  367. return;
  368. spin_lock_irqsave(&req->lock, flags);
  369. list_del(&obj->list);
  370. obj->req = NULL;
  371. if (req->state == MEDIA_REQUEST_STATE_COMPLETE)
  372. goto unlock;
  373. if (WARN_ON(req->state == MEDIA_REQUEST_STATE_VALIDATING))
  374. goto unlock;
  375. if (req->state == MEDIA_REQUEST_STATE_CLEANING) {
  376. if (!obj->completed)
  377. req->num_incomplete_objects--;
  378. goto unlock;
  379. }
  380. if (WARN_ON(!req->num_incomplete_objects))
  381. goto unlock;
  382. req->num_incomplete_objects--;
  383. if (req->state == MEDIA_REQUEST_STATE_QUEUED &&
  384. !req->num_incomplete_objects) {
  385. req->state = MEDIA_REQUEST_STATE_COMPLETE;
  386. completed = true;
  387. wake_up_interruptible_all(&req->poll_wait);
  388. }
  389. unlock:
  390. spin_unlock_irqrestore(&req->lock, flags);
  391. if (obj->ops->unbind)
  392. obj->ops->unbind(obj);
  393. if (completed)
  394. media_request_put(req);
  395. }
  396. EXPORT_SYMBOL_GPL(media_request_object_unbind);
  397. void media_request_object_complete(struct media_request_object *obj)
  398. {
  399. struct media_request *req = obj->req;
  400. unsigned long flags;
  401. bool completed = false;
  402. spin_lock_irqsave(&req->lock, flags);
  403. if (obj->completed)
  404. goto unlock;
  405. obj->completed = true;
  406. if (WARN_ON(!req->num_incomplete_objects) ||
  407. WARN_ON(req->state != MEDIA_REQUEST_STATE_QUEUED))
  408. goto unlock;
  409. if (!--req->num_incomplete_objects) {
  410. req->state = MEDIA_REQUEST_STATE_COMPLETE;
  411. wake_up_interruptible_all(&req->poll_wait);
  412. completed = true;
  413. }
  414. unlock:
  415. spin_unlock_irqrestore(&req->lock, flags);
  416. if (completed)
  417. media_request_put(req);
  418. }
  419. EXPORT_SYMBOL_GPL(media_request_object_complete);