media-request.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. *
  8. * Author: Hans Verkuil <hverkuil@kernel.org>
  9. * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
  10. */
  11. #ifndef MEDIA_REQUEST_H
  12. #define MEDIA_REQUEST_H
  13. #include <linux/list.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/refcount.h>
  17. #include <media/media-device.h>
  18. /**
  19. * enum media_request_state - media request state
  20. *
  21. * @MEDIA_REQUEST_STATE_IDLE: Idle
  22. * @MEDIA_REQUEST_STATE_VALIDATING: Validating the request, no state changes
  23. * allowed
  24. * @MEDIA_REQUEST_STATE_QUEUED: Queued
  25. * @MEDIA_REQUEST_STATE_COMPLETE: Completed, the request is done
  26. * @MEDIA_REQUEST_STATE_CLEANING: Cleaning, the request is being re-inited
  27. * @MEDIA_REQUEST_STATE_UPDATING: The request is being updated, i.e.
  28. * request objects are being added,
  29. * modified or removed
  30. * @NR_OF_MEDIA_REQUEST_STATE: The number of media request states, used
  31. * internally for sanity check purposes
  32. */
  33. enum media_request_state {
  34. MEDIA_REQUEST_STATE_IDLE,
  35. MEDIA_REQUEST_STATE_VALIDATING,
  36. MEDIA_REQUEST_STATE_QUEUED,
  37. MEDIA_REQUEST_STATE_COMPLETE,
  38. MEDIA_REQUEST_STATE_CLEANING,
  39. MEDIA_REQUEST_STATE_UPDATING,
  40. NR_OF_MEDIA_REQUEST_STATE,
  41. };
  42. struct media_request_object;
  43. /**
  44. * struct media_request - Media device request
  45. * @mdev: Media device this request belongs to
  46. * @kref: Reference count
  47. * @debug_str: Prefix for debug messages (process name:fd)
  48. * @state: The state of the request
  49. * @updating_count: count the number of request updates that are in progress
  50. * @access_count: count the number of request accesses that are in progress
  51. * @objects: List of @struct media_request_object request objects
  52. * @num_incomplete_objects: The number of incomplete objects in the request
  53. * @manual_completion: if true, then the request won't be marked as completed
  54. * when @num_incomplete_objects reaches 0. Call media_request_manual_complete()
  55. * to complete the request after @num_incomplete_objects == 0.
  56. * @poll_wait: Wait queue for poll
  57. * @lock: Serializes access to this struct
  58. */
  59. struct media_request {
  60. struct media_device *mdev;
  61. struct kref kref;
  62. char debug_str[TASK_COMM_LEN + 11];
  63. enum media_request_state state;
  64. unsigned int updating_count;
  65. unsigned int access_count;
  66. struct list_head objects;
  67. unsigned int num_incomplete_objects;
  68. bool manual_completion;
  69. wait_queue_head_t poll_wait;
  70. spinlock_t lock;
  71. };
  72. #ifdef CONFIG_MEDIA_CONTROLLER
  73. /**
  74. * media_request_lock_for_access - Lock the request to access its objects
  75. *
  76. * @req: The media request
  77. *
  78. * Use before accessing a completed request. A reference to the request must
  79. * be held during the access. This usually takes place automatically through
  80. * a file handle. Use @media_request_unlock_for_access when done.
  81. */
  82. static inline int __must_check
  83. media_request_lock_for_access(struct media_request *req)
  84. {
  85. unsigned long flags;
  86. int ret = -EBUSY;
  87. spin_lock_irqsave(&req->lock, flags);
  88. if (req->state == MEDIA_REQUEST_STATE_COMPLETE) {
  89. req->access_count++;
  90. ret = 0;
  91. }
  92. spin_unlock_irqrestore(&req->lock, flags);
  93. return ret;
  94. }
  95. /**
  96. * media_request_unlock_for_access - Unlock a request previously locked for
  97. * access
  98. *
  99. * @req: The media request
  100. *
  101. * Unlock a request that has previously been locked using
  102. * @media_request_lock_for_access.
  103. */
  104. static inline void media_request_unlock_for_access(struct media_request *req)
  105. {
  106. unsigned long flags;
  107. spin_lock_irqsave(&req->lock, flags);
  108. if (!WARN_ON(!req->access_count))
  109. req->access_count--;
  110. spin_unlock_irqrestore(&req->lock, flags);
  111. }
  112. /**
  113. * media_request_lock_for_update - Lock the request for updating its objects
  114. *
  115. * @req: The media request
  116. *
  117. * Use before updating a request, i.e. adding, modifying or removing a request
  118. * object in it. A reference to the request must be held during the update. This
  119. * usually takes place automatically through a file handle. Use
  120. * @media_request_unlock_for_update when done.
  121. */
  122. static inline int __must_check
  123. media_request_lock_for_update(struct media_request *req)
  124. {
  125. unsigned long flags;
  126. int ret = 0;
  127. spin_lock_irqsave(&req->lock, flags);
  128. if (req->state == MEDIA_REQUEST_STATE_IDLE ||
  129. req->state == MEDIA_REQUEST_STATE_UPDATING) {
  130. req->state = MEDIA_REQUEST_STATE_UPDATING;
  131. req->updating_count++;
  132. } else {
  133. ret = -EBUSY;
  134. }
  135. spin_unlock_irqrestore(&req->lock, flags);
  136. return ret;
  137. }
  138. /**
  139. * media_request_unlock_for_update - Unlock a request previously locked for
  140. * update
  141. *
  142. * @req: The media request
  143. *
  144. * Unlock a request that has previously been locked using
  145. * @media_request_lock_for_update.
  146. */
  147. static inline void media_request_unlock_for_update(struct media_request *req)
  148. {
  149. unsigned long flags;
  150. spin_lock_irqsave(&req->lock, flags);
  151. WARN_ON(req->updating_count <= 0);
  152. if (!--req->updating_count)
  153. req->state = MEDIA_REQUEST_STATE_IDLE;
  154. spin_unlock_irqrestore(&req->lock, flags);
  155. }
  156. /**
  157. * media_request_get - Get the media request
  158. *
  159. * @req: The media request
  160. *
  161. * Get the media request.
  162. */
  163. static inline void media_request_get(struct media_request *req)
  164. {
  165. kref_get(&req->kref);
  166. }
  167. /**
  168. * media_request_put - Put the media request
  169. *
  170. * @req: The media request
  171. *
  172. * Put the media request. The media request will be released
  173. * when the refcount reaches 0.
  174. */
  175. void media_request_put(struct media_request *req);
  176. /**
  177. * media_request_get_by_fd - Get a media request by fd
  178. *
  179. * @mdev: Media device this request belongs to
  180. * @request_fd: The file descriptor of the request
  181. *
  182. * Get the request represented by @request_fd that is owned
  183. * by the media device.
  184. *
  185. * Return a -EBADR error pointer if requests are not supported
  186. * by this driver. Return -EINVAL if the request was not found.
  187. * Return the pointer to the request if found: the caller will
  188. * have to call @media_request_put when it finished using the
  189. * request.
  190. */
  191. struct media_request *
  192. media_request_get_by_fd(struct media_device *mdev, int request_fd);
  193. /**
  194. * media_request_alloc - Allocate the media request
  195. *
  196. * @mdev: Media device this request belongs to
  197. * @alloc_fd: Store the request's file descriptor in this int
  198. *
  199. * Allocated the media request and put the fd in @alloc_fd.
  200. */
  201. int media_request_alloc(struct media_device *mdev,
  202. int *alloc_fd);
  203. /**
  204. * media_request_mark_manual_completion - Enable manual completion
  205. *
  206. * @req: The request
  207. *
  208. * Mark that the request has to be manually completed by calling
  209. * media_request_manual_complete().
  210. *
  211. * This function shall be called in the req_queue callback.
  212. */
  213. static inline void
  214. media_request_mark_manual_completion(struct media_request *req)
  215. {
  216. req->manual_completion = true;
  217. }
  218. /**
  219. * media_request_manual_complete - Mark the request as completed
  220. *
  221. * @req: The request
  222. *
  223. * This function completes a request that was marked for manual completion by an
  224. * earlier call to media_request_mark_manual_completion(). The request's
  225. * @manual_completion field is reset to false.
  226. *
  227. * All objects contained in the request must have been completed previously. It
  228. * is an error to call this function otherwise. If such an error occurred, the
  229. * function will WARN and the object completion will be delayed until
  230. * @num_incomplete_objects is 0.
  231. */
  232. void media_request_manual_complete(struct media_request *req);
  233. #else
  234. static inline void media_request_get(struct media_request *req)
  235. {
  236. }
  237. static inline void media_request_put(struct media_request *req)
  238. {
  239. }
  240. static inline struct media_request *
  241. media_request_get_by_fd(struct media_device *mdev, int request_fd)
  242. {
  243. return ERR_PTR(-EBADR);
  244. }
  245. #endif
  246. /**
  247. * struct media_request_object_ops - Media request object operations
  248. * @prepare: Validate and prepare the request object, optional.
  249. * @unprepare: Unprepare the request object, optional.
  250. * @queue: Queue the request object, optional.
  251. * @unbind: Unbind the request object, optional.
  252. * @release: Release the request object, required.
  253. */
  254. struct media_request_object_ops {
  255. int (*prepare)(struct media_request_object *object);
  256. void (*unprepare)(struct media_request_object *object);
  257. void (*queue)(struct media_request_object *object);
  258. void (*unbind)(struct media_request_object *object);
  259. void (*release)(struct media_request_object *object);
  260. };
  261. /**
  262. * struct media_request_object - An opaque object that belongs to a media
  263. * request
  264. *
  265. * @mdev: Media device this object belongs to
  266. * @ops: object's operations
  267. * @priv: object's priv pointer
  268. * @req: the request this object belongs to (can be NULL)
  269. * @list: List entry of the object for @struct media_request
  270. * @kref: Reference count of the object, acquire before releasing req->lock
  271. * @completed: If true, then this object was completed.
  272. *
  273. * An object related to the request. This struct is always embedded in
  274. * another struct that contains the actual data for this request object.
  275. */
  276. struct media_request_object {
  277. struct media_device *mdev;
  278. const struct media_request_object_ops *ops;
  279. void *priv;
  280. struct media_request *req;
  281. struct list_head list;
  282. struct kref kref;
  283. bool completed;
  284. };
  285. #ifdef CONFIG_MEDIA_CONTROLLER
  286. /**
  287. * media_request_object_get - Get a media request object
  288. *
  289. * @obj: The object
  290. *
  291. * Get a media request object.
  292. */
  293. static inline void media_request_object_get(struct media_request_object *obj)
  294. {
  295. kref_get(&obj->kref);
  296. }
  297. /**
  298. * media_request_object_put - Put a media request object
  299. *
  300. * @obj: The object
  301. *
  302. * Put a media request object. Once all references are gone, the
  303. * object's memory is released.
  304. */
  305. void media_request_object_put(struct media_request_object *obj);
  306. /**
  307. * media_request_object_find - Find an object in a request
  308. *
  309. * @req: The media request
  310. * @ops: Find an object with this ops value
  311. * @priv: Find an object with this priv value
  312. *
  313. * Both @ops and @priv must be non-NULL.
  314. *
  315. * Returns the object pointer or NULL if not found. The caller must
  316. * call media_request_object_put() once it finished using the object.
  317. *
  318. * Since this function needs to walk the list of objects it takes
  319. * the @req->lock spin lock to make this safe.
  320. */
  321. struct media_request_object *
  322. media_request_object_find(struct media_request *req,
  323. const struct media_request_object_ops *ops,
  324. void *priv);
  325. /**
  326. * media_request_object_init - Initialise a media request object
  327. *
  328. * @obj: The object
  329. *
  330. * Initialise a media request object. The object will be released using the
  331. * release callback of the ops once it has no references (this function
  332. * initialises references to one).
  333. */
  334. void media_request_object_init(struct media_request_object *obj);
  335. /**
  336. * media_request_object_bind - Bind a media request object to a request
  337. *
  338. * @req: The media request
  339. * @ops: The object ops for this object
  340. * @priv: A driver-specific priv pointer associated with this object
  341. * @is_buffer: Set to true if the object is a buffer object.
  342. * @obj: The object
  343. *
  344. * Bind this object to the request and set the ops and priv values of
  345. * the object so it can be found later with media_request_object_find().
  346. *
  347. * Every bound object must be unbound or completed by the kernel at some
  348. * point in time, otherwise the request will never complete. When the
  349. * request is released all completed objects will be unbound by the
  350. * request core code.
  351. *
  352. * Buffer objects will be added to the end of the request's object
  353. * list, non-buffer objects will be added to the front of the list.
  354. * This ensures that all buffer objects are at the end of the list
  355. * and that all non-buffer objects that they depend on are processed
  356. * first.
  357. */
  358. int media_request_object_bind(struct media_request *req,
  359. const struct media_request_object_ops *ops,
  360. void *priv, bool is_buffer,
  361. struct media_request_object *obj);
  362. /**
  363. * media_request_object_unbind - Unbind a media request object
  364. *
  365. * @obj: The object
  366. *
  367. * Unbind the media request object from the request.
  368. */
  369. void media_request_object_unbind(struct media_request_object *obj);
  370. /**
  371. * media_request_object_complete - Mark the media request object as complete
  372. *
  373. * @obj: The object
  374. *
  375. * Mark the media request object as complete. Only bound objects can
  376. * be completed.
  377. */
  378. void media_request_object_complete(struct media_request_object *obj);
  379. #else
  380. static inline int __must_check
  381. media_request_lock_for_access(struct media_request *req)
  382. {
  383. return -EINVAL;
  384. }
  385. static inline void media_request_unlock_for_access(struct media_request *req)
  386. {
  387. }
  388. static inline int __must_check
  389. media_request_lock_for_update(struct media_request *req)
  390. {
  391. return -EINVAL;
  392. }
  393. static inline void media_request_unlock_for_update(struct media_request *req)
  394. {
  395. }
  396. static inline void media_request_object_get(struct media_request_object *obj)
  397. {
  398. }
  399. static inline void media_request_object_put(struct media_request_object *obj)
  400. {
  401. }
  402. static inline struct media_request_object *
  403. media_request_object_find(struct media_request *req,
  404. const struct media_request_object_ops *ops,
  405. void *priv)
  406. {
  407. return NULL;
  408. }
  409. static inline void media_request_object_init(struct media_request_object *obj)
  410. {
  411. obj->ops = NULL;
  412. obj->req = NULL;
  413. }
  414. static inline int media_request_object_bind(struct media_request *req,
  415. const struct media_request_object_ops *ops,
  416. void *priv, bool is_buffer,
  417. struct media_request_object *obj)
  418. {
  419. return 0;
  420. }
  421. static inline void media_request_object_unbind(struct media_request_object *obj)
  422. {
  423. }
  424. static inline void media_request_object_complete(struct media_request_object *obj)
  425. {
  426. }
  427. #endif
  428. #endif