media-request.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 <[email protected]>
  9. * Author: Sakari Ailus <[email protected]>
  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. * @poll_wait: Wait queue for poll
  54. * @lock: Serializes access to this struct
  55. */
  56. struct media_request {
  57. struct media_device *mdev;
  58. struct kref kref;
  59. char debug_str[TASK_COMM_LEN + 11];
  60. enum media_request_state state;
  61. unsigned int updating_count;
  62. unsigned int access_count;
  63. struct list_head objects;
  64. unsigned int num_incomplete_objects;
  65. wait_queue_head_t poll_wait;
  66. spinlock_t lock;
  67. };
  68. #ifdef CONFIG_MEDIA_CONTROLLER
  69. /**
  70. * media_request_lock_for_access - Lock the request to access its objects
  71. *
  72. * @req: The media request
  73. *
  74. * Use before accessing a completed request. A reference to the request must
  75. * be held during the access. This usually takes place automatically through
  76. * a file handle. Use @media_request_unlock_for_access when done.
  77. */
  78. static inline int __must_check
  79. media_request_lock_for_access(struct media_request *req)
  80. {
  81. unsigned long flags;
  82. int ret = -EBUSY;
  83. spin_lock_irqsave(&req->lock, flags);
  84. if (req->state == MEDIA_REQUEST_STATE_COMPLETE) {
  85. req->access_count++;
  86. ret = 0;
  87. }
  88. spin_unlock_irqrestore(&req->lock, flags);
  89. return ret;
  90. }
  91. /**
  92. * media_request_unlock_for_access - Unlock a request previously locked for
  93. * access
  94. *
  95. * @req: The media request
  96. *
  97. * Unlock a request that has previously been locked using
  98. * @media_request_lock_for_access.
  99. */
  100. static inline void media_request_unlock_for_access(struct media_request *req)
  101. {
  102. unsigned long flags;
  103. spin_lock_irqsave(&req->lock, flags);
  104. if (!WARN_ON(!req->access_count))
  105. req->access_count--;
  106. spin_unlock_irqrestore(&req->lock, flags);
  107. }
  108. /**
  109. * media_request_lock_for_update - Lock the request for updating its objects
  110. *
  111. * @req: The media request
  112. *
  113. * Use before updating a request, i.e. adding, modifying or removing a request
  114. * object in it. A reference to the request must be held during the update. This
  115. * usually takes place automatically through a file handle. Use
  116. * @media_request_unlock_for_update when done.
  117. */
  118. static inline int __must_check
  119. media_request_lock_for_update(struct media_request *req)
  120. {
  121. unsigned long flags;
  122. int ret = 0;
  123. spin_lock_irqsave(&req->lock, flags);
  124. if (req->state == MEDIA_REQUEST_STATE_IDLE ||
  125. req->state == MEDIA_REQUEST_STATE_UPDATING) {
  126. req->state = MEDIA_REQUEST_STATE_UPDATING;
  127. req->updating_count++;
  128. } else {
  129. ret = -EBUSY;
  130. }
  131. spin_unlock_irqrestore(&req->lock, flags);
  132. return ret;
  133. }
  134. /**
  135. * media_request_unlock_for_update - Unlock a request previously locked for
  136. * update
  137. *
  138. * @req: The media request
  139. *
  140. * Unlock a request that has previously been locked using
  141. * @media_request_lock_for_update.
  142. */
  143. static inline void media_request_unlock_for_update(struct media_request *req)
  144. {
  145. unsigned long flags;
  146. spin_lock_irqsave(&req->lock, flags);
  147. WARN_ON(req->updating_count <= 0);
  148. if (!--req->updating_count)
  149. req->state = MEDIA_REQUEST_STATE_IDLE;
  150. spin_unlock_irqrestore(&req->lock, flags);
  151. }
  152. /**
  153. * media_request_get - Get the media request
  154. *
  155. * @req: The media request
  156. *
  157. * Get the media request.
  158. */
  159. static inline void media_request_get(struct media_request *req)
  160. {
  161. kref_get(&req->kref);
  162. }
  163. /**
  164. * media_request_put - Put the media request
  165. *
  166. * @req: The media request
  167. *
  168. * Put the media request. The media request will be released
  169. * when the refcount reaches 0.
  170. */
  171. void media_request_put(struct media_request *req);
  172. /**
  173. * media_request_get_by_fd - Get a media request by fd
  174. *
  175. * @mdev: Media device this request belongs to
  176. * @request_fd: The file descriptor of the request
  177. *
  178. * Get the request represented by @request_fd that is owned
  179. * by the media device.
  180. *
  181. * Return a -EBADR error pointer if requests are not supported
  182. * by this driver. Return -EINVAL if the request was not found.
  183. * Return the pointer to the request if found: the caller will
  184. * have to call @media_request_put when it finished using the
  185. * request.
  186. */
  187. struct media_request *
  188. media_request_get_by_fd(struct media_device *mdev, int request_fd);
  189. /**
  190. * media_request_alloc - Allocate the media request
  191. *
  192. * @mdev: Media device this request belongs to
  193. * @alloc_fd: Store the request's file descriptor in this int
  194. *
  195. * Allocated the media request and put the fd in @alloc_fd.
  196. */
  197. int media_request_alloc(struct media_device *mdev,
  198. int *alloc_fd);
  199. #else
  200. static inline void media_request_get(struct media_request *req)
  201. {
  202. }
  203. static inline void media_request_put(struct media_request *req)
  204. {
  205. }
  206. static inline struct media_request *
  207. media_request_get_by_fd(struct media_device *mdev, int request_fd)
  208. {
  209. return ERR_PTR(-EBADR);
  210. }
  211. #endif
  212. /**
  213. * struct media_request_object_ops - Media request object operations
  214. * @prepare: Validate and prepare the request object, optional.
  215. * @unprepare: Unprepare the request object, optional.
  216. * @queue: Queue the request object, optional.
  217. * @unbind: Unbind the request object, optional.
  218. * @release: Release the request object, required.
  219. */
  220. struct media_request_object_ops {
  221. int (*prepare)(struct media_request_object *object);
  222. void (*unprepare)(struct media_request_object *object);
  223. void (*queue)(struct media_request_object *object);
  224. void (*unbind)(struct media_request_object *object);
  225. void (*release)(struct media_request_object *object);
  226. };
  227. /**
  228. * struct media_request_object - An opaque object that belongs to a media
  229. * request
  230. *
  231. * @ops: object's operations
  232. * @priv: object's priv pointer
  233. * @req: the request this object belongs to (can be NULL)
  234. * @list: List entry of the object for @struct media_request
  235. * @kref: Reference count of the object, acquire before releasing req->lock
  236. * @completed: If true, then this object was completed.
  237. *
  238. * An object related to the request. This struct is always embedded in
  239. * another struct that contains the actual data for this request object.
  240. */
  241. struct media_request_object {
  242. const struct media_request_object_ops *ops;
  243. void *priv;
  244. struct media_request *req;
  245. struct list_head list;
  246. struct kref kref;
  247. bool completed;
  248. };
  249. #ifdef CONFIG_MEDIA_CONTROLLER
  250. /**
  251. * media_request_object_get - Get a media request object
  252. *
  253. * @obj: The object
  254. *
  255. * Get a media request object.
  256. */
  257. static inline void media_request_object_get(struct media_request_object *obj)
  258. {
  259. kref_get(&obj->kref);
  260. }
  261. /**
  262. * media_request_object_put - Put a media request object
  263. *
  264. * @obj: The object
  265. *
  266. * Put a media request object. Once all references are gone, the
  267. * object's memory is released.
  268. */
  269. void media_request_object_put(struct media_request_object *obj);
  270. /**
  271. * media_request_object_find - Find an object in a request
  272. *
  273. * @req: The media request
  274. * @ops: Find an object with this ops value
  275. * @priv: Find an object with this priv value
  276. *
  277. * Both @ops and @priv must be non-NULL.
  278. *
  279. * Returns the object pointer or NULL if not found. The caller must
  280. * call media_request_object_put() once it finished using the object.
  281. *
  282. * Since this function needs to walk the list of objects it takes
  283. * the @req->lock spin lock to make this safe.
  284. */
  285. struct media_request_object *
  286. media_request_object_find(struct media_request *req,
  287. const struct media_request_object_ops *ops,
  288. void *priv);
  289. /**
  290. * media_request_object_init - Initialise a media request object
  291. *
  292. * @obj: The object
  293. *
  294. * Initialise a media request object. The object will be released using the
  295. * release callback of the ops once it has no references (this function
  296. * initialises references to one).
  297. */
  298. void media_request_object_init(struct media_request_object *obj);
  299. /**
  300. * media_request_object_bind - Bind a media request object to a request
  301. *
  302. * @req: The media request
  303. * @ops: The object ops for this object
  304. * @priv: A driver-specific priv pointer associated with this object
  305. * @is_buffer: Set to true if the object a buffer object.
  306. * @obj: The object
  307. *
  308. * Bind this object to the request and set the ops and priv values of
  309. * the object so it can be found later with media_request_object_find().
  310. *
  311. * Every bound object must be unbound or completed by the kernel at some
  312. * point in time, otherwise the request will never complete. When the
  313. * request is released all completed objects will be unbound by the
  314. * request core code.
  315. *
  316. * Buffer objects will be added to the end of the request's object
  317. * list, non-buffer objects will be added to the front of the list.
  318. * This ensures that all buffer objects are at the end of the list
  319. * and that all non-buffer objects that they depend on are processed
  320. * first.
  321. */
  322. int media_request_object_bind(struct media_request *req,
  323. const struct media_request_object_ops *ops,
  324. void *priv, bool is_buffer,
  325. struct media_request_object *obj);
  326. /**
  327. * media_request_object_unbind - Unbind a media request object
  328. *
  329. * @obj: The object
  330. *
  331. * Unbind the media request object from the request.
  332. */
  333. void media_request_object_unbind(struct media_request_object *obj);
  334. /**
  335. * media_request_object_complete - Mark the media request object as complete
  336. *
  337. * @obj: The object
  338. *
  339. * Mark the media request object as complete. Only bound objects can
  340. * be completed.
  341. */
  342. void media_request_object_complete(struct media_request_object *obj);
  343. #else
  344. static inline int __must_check
  345. media_request_lock_for_access(struct media_request *req)
  346. {
  347. return -EINVAL;
  348. }
  349. static inline void media_request_unlock_for_access(struct media_request *req)
  350. {
  351. }
  352. static inline int __must_check
  353. media_request_lock_for_update(struct media_request *req)
  354. {
  355. return -EINVAL;
  356. }
  357. static inline void media_request_unlock_for_update(struct media_request *req)
  358. {
  359. }
  360. static inline void media_request_object_get(struct media_request_object *obj)
  361. {
  362. }
  363. static inline void media_request_object_put(struct media_request_object *obj)
  364. {
  365. }
  366. static inline struct media_request_object *
  367. media_request_object_find(struct media_request *req,
  368. const struct media_request_object_ops *ops,
  369. void *priv)
  370. {
  371. return NULL;
  372. }
  373. static inline void media_request_object_init(struct media_request_object *obj)
  374. {
  375. obj->ops = NULL;
  376. obj->req = NULL;
  377. }
  378. static inline int media_request_object_bind(struct media_request *req,
  379. const struct media_request_object_ops *ops,
  380. void *priv, bool is_buffer,
  381. struct media_request_object *obj)
  382. {
  383. return 0;
  384. }
  385. static inline void media_request_object_unbind(struct media_request_object *obj)
  386. {
  387. }
  388. static inline void media_request_object_complete(struct media_request_object *obj)
  389. {
  390. }
  391. #endif
  392. #endif