v4l2-ctrls-request.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * V4L2 controls framework Request API implementation.
  4. *
  5. * Copyright (C) 2018-2021 Hans Verkuil <hverkuil-cisco@xs4all.nl>
  6. */
  7. #define pr_fmt(fmt) "v4l2-ctrls: " fmt
  8. #include <linux/export.h>
  9. #include <linux/slab.h>
  10. #include <media/v4l2-ctrls.h>
  11. #include <media/v4l2-dev.h>
  12. #include <media/v4l2-ioctl.h>
  13. #include "v4l2-ctrls-priv.h"
  14. /* Initialize the request-related fields in a control handler */
  15. void v4l2_ctrl_handler_init_request(struct v4l2_ctrl_handler *hdl)
  16. {
  17. INIT_LIST_HEAD(&hdl->requests);
  18. INIT_LIST_HEAD(&hdl->requests_queued);
  19. hdl->request_is_queued = false;
  20. media_request_object_init(&hdl->req_obj);
  21. }
  22. /* Free the request-related fields in a control handler */
  23. void v4l2_ctrl_handler_free_request(struct v4l2_ctrl_handler *hdl)
  24. {
  25. struct v4l2_ctrl_handler *req, *next_req;
  26. /*
  27. * Do nothing if this isn't the main handler or the main
  28. * handler is not used in any request.
  29. *
  30. * The main handler can be identified by having a NULL ops pointer in
  31. * the request object.
  32. */
  33. if (hdl->req_obj.ops || list_empty(&hdl->requests))
  34. return;
  35. /*
  36. * If the main handler is freed and it is used by handler objects in
  37. * outstanding requests, then unbind and put those objects before
  38. * freeing the main handler.
  39. */
  40. list_for_each_entry_safe(req, next_req, &hdl->requests, requests) {
  41. media_request_object_unbind(&req->req_obj);
  42. media_request_object_put(&req->req_obj);
  43. }
  44. }
  45. static int v4l2_ctrl_request_clone(struct v4l2_ctrl_handler *hdl,
  46. const struct v4l2_ctrl_handler *from)
  47. {
  48. struct v4l2_ctrl_ref *ref;
  49. int err = 0;
  50. if (WARN_ON(!hdl || hdl == from))
  51. return -EINVAL;
  52. if (hdl->error)
  53. return hdl->error;
  54. WARN_ON(hdl->lock != &hdl->_lock);
  55. mutex_lock(from->lock);
  56. list_for_each_entry(ref, &from->ctrl_refs, node) {
  57. struct v4l2_ctrl *ctrl = ref->ctrl;
  58. struct v4l2_ctrl_ref *new_ref;
  59. /* Skip refs inherited from other devices */
  60. if (ref->from_other_dev)
  61. continue;
  62. err = handler_new_ref(hdl, ctrl, &new_ref, false, true);
  63. if (err)
  64. break;
  65. }
  66. mutex_unlock(from->lock);
  67. return err;
  68. }
  69. static void v4l2_ctrl_request_queue(struct media_request_object *obj)
  70. {
  71. struct v4l2_ctrl_handler *hdl =
  72. container_of(obj, struct v4l2_ctrl_handler, req_obj);
  73. struct v4l2_ctrl_handler *main_hdl = obj->priv;
  74. mutex_lock(main_hdl->lock);
  75. list_add_tail(&hdl->requests_queued, &main_hdl->requests_queued);
  76. hdl->request_is_queued = true;
  77. mutex_unlock(main_hdl->lock);
  78. }
  79. static void v4l2_ctrl_request_unbind(struct media_request_object *obj)
  80. {
  81. struct v4l2_ctrl_handler *hdl =
  82. container_of(obj, struct v4l2_ctrl_handler, req_obj);
  83. struct v4l2_ctrl_handler *main_hdl = obj->priv;
  84. mutex_lock(main_hdl->lock);
  85. list_del_init(&hdl->requests);
  86. if (hdl->request_is_queued) {
  87. list_del_init(&hdl->requests_queued);
  88. hdl->request_is_queued = false;
  89. }
  90. mutex_unlock(main_hdl->lock);
  91. }
  92. static void v4l2_ctrl_request_release(struct media_request_object *obj)
  93. {
  94. struct v4l2_ctrl_handler *hdl =
  95. container_of(obj, struct v4l2_ctrl_handler, req_obj);
  96. v4l2_ctrl_handler_free(hdl);
  97. kfree(hdl);
  98. }
  99. static const struct media_request_object_ops req_ops = {
  100. .queue = v4l2_ctrl_request_queue,
  101. .unbind = v4l2_ctrl_request_unbind,
  102. .release = v4l2_ctrl_request_release,
  103. };
  104. struct v4l2_ctrl_handler *v4l2_ctrl_request_hdl_find(struct media_request *req,
  105. struct v4l2_ctrl_handler *parent)
  106. {
  107. struct media_request_object *obj;
  108. if (WARN_ON(req->state != MEDIA_REQUEST_STATE_VALIDATING &&
  109. req->state != MEDIA_REQUEST_STATE_QUEUED))
  110. return NULL;
  111. obj = media_request_object_find(req, &req_ops, parent);
  112. if (obj)
  113. return container_of(obj, struct v4l2_ctrl_handler, req_obj);
  114. return NULL;
  115. }
  116. EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_find);
  117. struct v4l2_ctrl *
  118. v4l2_ctrl_request_hdl_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
  119. {
  120. struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
  121. return (ref && ref->p_req_valid) ? ref->ctrl : NULL;
  122. }
  123. EXPORT_SYMBOL_GPL(v4l2_ctrl_request_hdl_ctrl_find);
  124. static int v4l2_ctrl_request_bind(struct media_request *req,
  125. struct v4l2_ctrl_handler *hdl,
  126. struct v4l2_ctrl_handler *from)
  127. {
  128. int ret;
  129. ret = v4l2_ctrl_request_clone(hdl, from);
  130. if (!ret) {
  131. ret = media_request_object_bind(req, &req_ops,
  132. from, false, &hdl->req_obj);
  133. if (!ret) {
  134. mutex_lock(from->lock);
  135. list_add_tail(&hdl->requests, &from->requests);
  136. mutex_unlock(from->lock);
  137. }
  138. }
  139. return ret;
  140. }
  141. static struct media_request_object *
  142. v4l2_ctrls_find_req_obj(struct v4l2_ctrl_handler *hdl,
  143. struct media_request *req, bool set)
  144. {
  145. struct media_request_object *obj;
  146. struct v4l2_ctrl_handler *new_hdl;
  147. int ret;
  148. if (IS_ERR(req))
  149. return ERR_CAST(req);
  150. if (set && WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING))
  151. return ERR_PTR(-EBUSY);
  152. obj = media_request_object_find(req, &req_ops, hdl);
  153. if (obj)
  154. return obj;
  155. /*
  156. * If there are no controls in this completed request,
  157. * then that can only happen if:
  158. *
  159. * 1) no controls were present in the queued request, and
  160. * 2) v4l2_ctrl_request_complete() could not allocate a
  161. * control handler object to store the completed state in.
  162. *
  163. * So return ENOMEM to indicate that there was an out-of-memory
  164. * error.
  165. */
  166. if (!set)
  167. return ERR_PTR(-ENOMEM);
  168. new_hdl = kzalloc(sizeof(*new_hdl), GFP_KERNEL);
  169. if (!new_hdl)
  170. return ERR_PTR(-ENOMEM);
  171. obj = &new_hdl->req_obj;
  172. ret = v4l2_ctrl_handler_init(new_hdl, (hdl->nr_of_buckets - 1) * 8);
  173. if (!ret)
  174. ret = v4l2_ctrl_request_bind(req, new_hdl, hdl);
  175. if (ret) {
  176. v4l2_ctrl_handler_free(new_hdl);
  177. kfree(new_hdl);
  178. return ERR_PTR(ret);
  179. }
  180. media_request_object_get(obj);
  181. return obj;
  182. }
  183. int v4l2_g_ext_ctrls_request(struct v4l2_ctrl_handler *hdl, struct video_device *vdev,
  184. struct media_device *mdev, struct v4l2_ext_controls *cs)
  185. {
  186. struct media_request_object *obj = NULL;
  187. struct media_request *req = NULL;
  188. int ret;
  189. if (!mdev || cs->request_fd < 0)
  190. return -EINVAL;
  191. req = media_request_get_by_fd(mdev, cs->request_fd);
  192. if (IS_ERR(req))
  193. return PTR_ERR(req);
  194. if (req->state != MEDIA_REQUEST_STATE_COMPLETE) {
  195. media_request_put(req);
  196. return -EACCES;
  197. }
  198. ret = media_request_lock_for_access(req);
  199. if (ret) {
  200. media_request_put(req);
  201. return ret;
  202. }
  203. obj = v4l2_ctrls_find_req_obj(hdl, req, false);
  204. if (IS_ERR(obj)) {
  205. media_request_unlock_for_access(req);
  206. media_request_put(req);
  207. return PTR_ERR(obj);
  208. }
  209. hdl = container_of(obj, struct v4l2_ctrl_handler,
  210. req_obj);
  211. ret = v4l2_g_ext_ctrls_common(hdl, cs, vdev);
  212. media_request_unlock_for_access(req);
  213. media_request_object_put(obj);
  214. media_request_put(req);
  215. return ret;
  216. }
  217. int try_set_ext_ctrls_request(struct v4l2_fh *fh,
  218. struct v4l2_ctrl_handler *hdl,
  219. struct video_device *vdev,
  220. struct media_device *mdev,
  221. struct v4l2_ext_controls *cs, bool set)
  222. {
  223. struct media_request_object *obj = NULL;
  224. struct media_request *req = NULL;
  225. int ret;
  226. if (!mdev) {
  227. dprintk(vdev, "%s: missing media device\n",
  228. video_device_node_name(vdev));
  229. return -EINVAL;
  230. }
  231. if (cs->request_fd < 0) {
  232. dprintk(vdev, "%s: invalid request fd %d\n",
  233. video_device_node_name(vdev), cs->request_fd);
  234. return -EINVAL;
  235. }
  236. req = media_request_get_by_fd(mdev, cs->request_fd);
  237. if (IS_ERR(req)) {
  238. dprintk(vdev, "%s: cannot find request fd %d\n",
  239. video_device_node_name(vdev), cs->request_fd);
  240. return PTR_ERR(req);
  241. }
  242. ret = media_request_lock_for_update(req);
  243. if (ret) {
  244. dprintk(vdev, "%s: cannot lock request fd %d\n",
  245. video_device_node_name(vdev), cs->request_fd);
  246. media_request_put(req);
  247. return ret;
  248. }
  249. obj = v4l2_ctrls_find_req_obj(hdl, req, set);
  250. if (IS_ERR(obj)) {
  251. dprintk(vdev,
  252. "%s: cannot find request object for request fd %d\n",
  253. video_device_node_name(vdev),
  254. cs->request_fd);
  255. media_request_unlock_for_update(req);
  256. media_request_put(req);
  257. return PTR_ERR(obj);
  258. }
  259. hdl = container_of(obj, struct v4l2_ctrl_handler,
  260. req_obj);
  261. ret = try_set_ext_ctrls_common(fh, hdl, cs, vdev, set);
  262. if (ret)
  263. dprintk(vdev,
  264. "%s: try_set_ext_ctrls_common failed (%d)\n",
  265. video_device_node_name(vdev), ret);
  266. media_request_unlock_for_update(req);
  267. media_request_object_put(obj);
  268. media_request_put(req);
  269. return ret;
  270. }
  271. void v4l2_ctrl_request_complete(struct media_request *req,
  272. struct v4l2_ctrl_handler *main_hdl)
  273. {
  274. struct media_request_object *obj;
  275. struct v4l2_ctrl_handler *hdl;
  276. struct v4l2_ctrl_ref *ref;
  277. if (!req || !main_hdl)
  278. return;
  279. /*
  280. * Note that it is valid if nothing was found. It means
  281. * that this request doesn't have any controls and so just
  282. * wants to leave the controls unchanged.
  283. */
  284. obj = media_request_object_find(req, &req_ops, main_hdl);
  285. if (!obj) {
  286. int ret;
  287. /* Create a new request so the driver can return controls */
  288. hdl = kzalloc(sizeof(*hdl), GFP_KERNEL);
  289. if (!hdl)
  290. return;
  291. ret = v4l2_ctrl_handler_init(hdl, (main_hdl->nr_of_buckets - 1) * 8);
  292. if (!ret)
  293. ret = v4l2_ctrl_request_bind(req, hdl, main_hdl);
  294. if (ret) {
  295. v4l2_ctrl_handler_free(hdl);
  296. kfree(hdl);
  297. return;
  298. }
  299. hdl->request_is_queued = true;
  300. obj = media_request_object_find(req, &req_ops, main_hdl);
  301. }
  302. hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);
  303. list_for_each_entry(ref, &hdl->ctrl_refs, node) {
  304. struct v4l2_ctrl *ctrl = ref->ctrl;
  305. struct v4l2_ctrl *master = ctrl->cluster[0];
  306. unsigned int i;
  307. if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
  308. v4l2_ctrl_lock(master);
  309. /* g_volatile_ctrl will update the current control values */
  310. for (i = 0; i < master->ncontrols; i++)
  311. cur_to_new(master->cluster[i]);
  312. call_op(master, g_volatile_ctrl);
  313. new_to_req(ref);
  314. v4l2_ctrl_unlock(master);
  315. continue;
  316. }
  317. if (ref->p_req_valid)
  318. continue;
  319. /* Copy the current control value into the request */
  320. v4l2_ctrl_lock(ctrl);
  321. cur_to_req(ref);
  322. v4l2_ctrl_unlock(ctrl);
  323. }
  324. mutex_lock(main_hdl->lock);
  325. WARN_ON(!hdl->request_is_queued);
  326. list_del_init(&hdl->requests_queued);
  327. hdl->request_is_queued = false;
  328. mutex_unlock(main_hdl->lock);
  329. media_request_object_complete(obj);
  330. media_request_object_put(obj);
  331. }
  332. EXPORT_SYMBOL(v4l2_ctrl_request_complete);
  333. int v4l2_ctrl_request_setup(struct media_request *req,
  334. struct v4l2_ctrl_handler *main_hdl)
  335. {
  336. struct media_request_object *obj;
  337. struct v4l2_ctrl_handler *hdl;
  338. struct v4l2_ctrl_ref *ref;
  339. int ret = 0;
  340. if (!req || !main_hdl)
  341. return 0;
  342. if (WARN_ON(req->state != MEDIA_REQUEST_STATE_QUEUED))
  343. return -EBUSY;
  344. /*
  345. * Note that it is valid if nothing was found. It means
  346. * that this request doesn't have any controls and so just
  347. * wants to leave the controls unchanged.
  348. */
  349. obj = media_request_object_find(req, &req_ops, main_hdl);
  350. if (!obj)
  351. return 0;
  352. if (obj->completed) {
  353. media_request_object_put(obj);
  354. return -EBUSY;
  355. }
  356. hdl = container_of(obj, struct v4l2_ctrl_handler, req_obj);
  357. list_for_each_entry(ref, &hdl->ctrl_refs, node)
  358. ref->req_done = false;
  359. list_for_each_entry(ref, &hdl->ctrl_refs, node) {
  360. struct v4l2_ctrl *ctrl = ref->ctrl;
  361. struct v4l2_ctrl *master = ctrl->cluster[0];
  362. bool have_new_data = false;
  363. int i;
  364. /*
  365. * Skip if this control was already handled by a cluster.
  366. * Skip button controls and read-only controls.
  367. */
  368. if (ref->req_done || (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
  369. continue;
  370. v4l2_ctrl_lock(master);
  371. for (i = 0; i < master->ncontrols; i++) {
  372. if (master->cluster[i]) {
  373. struct v4l2_ctrl_ref *r =
  374. find_ref(hdl, master->cluster[i]->id);
  375. if (r->p_req_valid) {
  376. have_new_data = true;
  377. break;
  378. }
  379. }
  380. }
  381. if (!have_new_data) {
  382. v4l2_ctrl_unlock(master);
  383. continue;
  384. }
  385. for (i = 0; i < master->ncontrols; i++) {
  386. if (master->cluster[i]) {
  387. struct v4l2_ctrl_ref *r =
  388. find_ref(hdl, master->cluster[i]->id);
  389. ret = req_to_new(r);
  390. if (ret) {
  391. v4l2_ctrl_unlock(master);
  392. goto error;
  393. }
  394. master->cluster[i]->is_new = 1;
  395. r->req_done = true;
  396. }
  397. }
  398. /*
  399. * For volatile autoclusters that are currently in auto mode
  400. * we need to discover if it will be set to manual mode.
  401. * If so, then we have to copy the current volatile values
  402. * first since those will become the new manual values (which
  403. * may be overwritten by explicit new values from this set
  404. * of controls).
  405. */
  406. if (master->is_auto && master->has_volatiles &&
  407. !is_cur_manual(master)) {
  408. s32 new_auto_val = *master->p_new.p_s32;
  409. /*
  410. * If the new value == the manual value, then copy
  411. * the current volatile values.
  412. */
  413. if (new_auto_val == master->manual_mode_value)
  414. update_from_auto_cluster(master);
  415. }
  416. ret = try_or_set_cluster(NULL, master, true, 0);
  417. v4l2_ctrl_unlock(master);
  418. if (ret)
  419. break;
  420. }
  421. error:
  422. media_request_object_put(obj);
  423. return ret;
  424. }
  425. EXPORT_SYMBOL(v4l2_ctrl_request_setup);