v4l2-ctrls.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. V4L2 controls support header.
  3. Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #ifndef _V4L2_CTRLS_H
  17. #define _V4L2_CTRLS_H
  18. #include <linux/list.h>
  19. #include <linux/device.h>
  20. #include <linux/videodev2.h>
  21. /* forward references */
  22. struct v4l2_ctrl_handler;
  23. struct v4l2_ctrl;
  24. struct video_device;
  25. struct v4l2_subdev;
  26. /** struct v4l2_ctrl_ops - The control operations that the driver has to provide.
  27. * @g_volatile_ctrl: Get a new value for this control. Generally only relevant
  28. * for volatile (and usually read-only) controls such as a control
  29. * that returns the current signal strength which changes
  30. * continuously.
  31. * If not set, then the currently cached value will be returned.
  32. * @try_ctrl: Test whether the control's value is valid. Only relevant when
  33. * the usual min/max/step checks are not sufficient.
  34. * @s_ctrl: Actually set the new control value. s_ctrl is compulsory. The
  35. * ctrl->handler->lock is held when these ops are called, so no
  36. * one else can access controls owned by that handler.
  37. */
  38. struct v4l2_ctrl_ops {
  39. int (*g_volatile_ctrl)(struct v4l2_ctrl *ctrl);
  40. int (*try_ctrl)(struct v4l2_ctrl *ctrl);
  41. int (*s_ctrl)(struct v4l2_ctrl *ctrl);
  42. };
  43. /** struct v4l2_ctrl - The control structure.
  44. * @node: The list node.
  45. * @handler: The handler that owns the control.
  46. * @cluster: Point to start of cluster array.
  47. * @ncontrols: Number of controls in cluster array.
  48. * @done: Internal flag: set for each processed control.
  49. * @is_new: Set when the user specified a new value for this control. It
  50. * is also set when called from v4l2_ctrl_handler_setup. Drivers
  51. * should never set this flag.
  52. * @is_private: If set, then this control is private to its handler and it
  53. * will not be added to any other handlers. Drivers can set
  54. * this flag.
  55. * @is_volatile: If set, then this control is volatile. This means that the
  56. * control's current value cannot be cached and needs to be
  57. * retrieved through the g_volatile_ctrl op. Drivers can set
  58. * this flag.
  59. * @ops: The control ops.
  60. * @id: The control ID.
  61. * @name: The control name.
  62. * @type: The control type.
  63. * @minimum: The control's minimum value.
  64. * @maximum: The control's maximum value.
  65. * @default_value: The control's default value.
  66. * @step: The control's step value for non-menu controls.
  67. * @menu_skip_mask: The control's skip mask for menu controls. This makes it
  68. * easy to skip menu items that are not valid. If bit X is set,
  69. * then menu item X is skipped. Of course, this only works for
  70. * menus with <= 32 menu items. There are no menus that come
  71. * close to that number, so this is OK. Should we ever need more,
  72. * then this will have to be extended to a u64 or a bit array.
  73. * @qmenu: A const char * array for all menu items. Array entries that are
  74. * empty strings ("") correspond to non-existing menu items (this
  75. * is in addition to the menu_skip_mask above). The last entry
  76. * must be NULL.
  77. * @flags: The control's flags.
  78. * @cur: The control's current value.
  79. * @val: The control's new s32 value.
  80. * @val64: The control's new s64 value.
  81. * @string: The control's new string value.
  82. * @priv: The control's private pointer. For use by the driver. It is
  83. * untouched by the control framework. Note that this pointer is
  84. * not freed when the control is deleted. Should this be needed
  85. * then a new internal bitfield can be added to tell the framework
  86. * to free this pointer.
  87. */
  88. struct v4l2_ctrl {
  89. /* Administrative fields */
  90. struct list_head node;
  91. struct v4l2_ctrl_handler *handler;
  92. struct v4l2_ctrl **cluster;
  93. unsigned ncontrols;
  94. unsigned int done:1;
  95. unsigned int is_new:1;
  96. unsigned int is_private:1;
  97. unsigned int is_volatile:1;
  98. const struct v4l2_ctrl_ops *ops;
  99. u32 id;
  100. const char *name;
  101. enum v4l2_ctrl_type type;
  102. s32 minimum, maximum, default_value;
  103. union {
  104. u32 step;
  105. u32 menu_skip_mask;
  106. };
  107. const char * const *qmenu;
  108. unsigned long flags;
  109. union {
  110. s32 val;
  111. s64 val64;
  112. char *string;
  113. } cur;
  114. union {
  115. s32 val;
  116. s64 val64;
  117. char *string;
  118. };
  119. void *priv;
  120. };
  121. /** struct v4l2_ctrl_ref - The control reference.
  122. * @node: List node for the sorted list.
  123. * @next: Single-link list node for the hash.
  124. * @ctrl: The actual control information.
  125. *
  126. * Each control handler has a list of these refs. The list_head is used to
  127. * keep a sorted-by-control-ID list of all controls, while the next pointer
  128. * is used to link the control in the hash's bucket.
  129. */
  130. struct v4l2_ctrl_ref {
  131. struct list_head node;
  132. struct v4l2_ctrl_ref *next;
  133. struct v4l2_ctrl *ctrl;
  134. };
  135. /** struct v4l2_ctrl_handler - The control handler keeps track of all the
  136. * controls: both the controls owned by the handler and those inherited
  137. * from other handlers.
  138. * @lock: Lock to control access to this handler and its controls.
  139. * @ctrls: The list of controls owned by this handler.
  140. * @ctrl_refs: The list of control references.
  141. * @cached: The last found control reference. It is common that the same
  142. * control is needed multiple times, so this is a simple
  143. * optimization.
  144. * @buckets: Buckets for the hashing. Allows for quick control lookup.
  145. * @nr_of_buckets: Total number of buckets in the array.
  146. * @error: The error code of the first failed control addition.
  147. */
  148. struct v4l2_ctrl_handler {
  149. struct mutex lock;
  150. struct list_head ctrls;
  151. struct list_head ctrl_refs;
  152. struct v4l2_ctrl_ref *cached;
  153. struct v4l2_ctrl_ref **buckets;
  154. u16 nr_of_buckets;
  155. int error;
  156. };
  157. /** struct v4l2_ctrl_config - Control configuration structure.
  158. * @ops: The control ops.
  159. * @id: The control ID.
  160. * @name: The control name.
  161. * @type: The control type.
  162. * @min: The control's minimum value.
  163. * @max: The control's maximum value.
  164. * @step: The control's step value for non-menu controls.
  165. * @def: The control's default value.
  166. * @flags: The control's flags.
  167. * @menu_skip_mask: The control's skip mask for menu controls. This makes it
  168. * easy to skip menu items that are not valid. If bit X is set,
  169. * then menu item X is skipped. Of course, this only works for
  170. * menus with <= 32 menu items. There are no menus that come
  171. * close to that number, so this is OK. Should we ever need more,
  172. * then this will have to be extended to a u64 or a bit array.
  173. * @qmenu: A const char * array for all menu items. Array entries that are
  174. * empty strings ("") correspond to non-existing menu items (this
  175. * is in addition to the menu_skip_mask above). The last entry
  176. * must be NULL.
  177. * @is_private: If set, then this control is private to its handler and it
  178. * will not be added to any other handlers.
  179. * @is_volatile: If set, then this control is volatile. This means that the
  180. * control's current value cannot be cached and needs to be
  181. * retrieved through the g_volatile_ctrl op.
  182. */
  183. struct v4l2_ctrl_config {
  184. const struct v4l2_ctrl_ops *ops;
  185. u32 id;
  186. const char *name;
  187. enum v4l2_ctrl_type type;
  188. s32 min;
  189. s32 max;
  190. u32 step;
  191. s32 def;
  192. u32 flags;
  193. u32 menu_skip_mask;
  194. const char * const *qmenu;
  195. unsigned int is_private:1;
  196. unsigned int is_volatile:1;
  197. };
  198. /** v4l2_ctrl_fill() - Fill in the control fields based on the control ID.
  199. *
  200. * This works for all standard V4L2 controls.
  201. * For non-standard controls it will only fill in the given arguments
  202. * and @name will be NULL.
  203. *
  204. * This function will overwrite the contents of @name, @type and @flags.
  205. * The contents of @min, @max, @step and @def may be modified depending on
  206. * the type.
  207. *
  208. * Do not use in drivers! It is used internally for backwards compatibility
  209. * control handling only. Once all drivers are converted to use the new
  210. * control framework this function will no longer be exported.
  211. */
  212. void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
  213. s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags);
  214. /** v4l2_ctrl_handler_init() - Initialize the control handler.
  215. * @hdl: The control handler.
  216. * @nr_of_controls_hint: A hint of how many controls this handler is
  217. * expected to refer to. This is the total number, so including
  218. * any inherited controls. It doesn't have to be precise, but if
  219. * it is way off, then you either waste memory (too many buckets
  220. * are allocated) or the control lookup becomes slower (not enough
  221. * buckets are allocated, so there are more slow list lookups).
  222. * It will always work, though.
  223. *
  224. * Returns an error if the buckets could not be allocated. This error will
  225. * also be stored in @hdl->error.
  226. */
  227. int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
  228. unsigned nr_of_controls_hint);
  229. /** v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
  230. * the control list.
  231. * @hdl: The control handler.
  232. *
  233. * Does nothing if @hdl == NULL.
  234. */
  235. void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
  236. /** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
  237. * to the handler to initialize the hardware to the current control values.
  238. * @hdl: The control handler.
  239. *
  240. * Button controls will be skipped, as are read-only controls.
  241. *
  242. * If @hdl == NULL, then this just returns 0.
  243. */
  244. int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
  245. /** v4l2_ctrl_handler_log_status() - Log all controls owned by the handler.
  246. * @hdl: The control handler.
  247. * @prefix: The prefix to use when logging the control values. If the
  248. * prefix does not end with a space, then ": " will be added
  249. * after the prefix. If @prefix == NULL, then no prefix will be
  250. * used.
  251. *
  252. * For use with VIDIOC_LOG_STATUS.
  253. *
  254. * Does nothing if @hdl == NULL.
  255. */
  256. void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
  257. const char *prefix);
  258. /** v4l2_ctrl_new_custom() - Allocate and initialize a new custom V4L2
  259. * control.
  260. * @hdl: The control handler.
  261. * @cfg: The control's configuration data.
  262. * @priv: The control's driver-specific private data.
  263. *
  264. * If the &v4l2_ctrl struct could not be allocated then NULL is returned
  265. * and @hdl->error is set to the error code (if it wasn't set already).
  266. */
  267. struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
  268. const struct v4l2_ctrl_config *cfg, void *priv);
  269. /** v4l2_ctrl_new_std() - Allocate and initialize a new standard V4L2 non-menu control.
  270. * @hdl: The control handler.
  271. * @ops: The control ops.
  272. * @id: The control ID.
  273. * @min: The control's minimum value.
  274. * @max: The control's maximum value.
  275. * @step: The control's step value
  276. * @def: The control's default value.
  277. *
  278. * If the &v4l2_ctrl struct could not be allocated, or the control
  279. * ID is not known, then NULL is returned and @hdl->error is set to the
  280. * appropriate error code (if it wasn't set already).
  281. *
  282. * If @id refers to a menu control, then this function will return NULL.
  283. *
  284. * Use v4l2_ctrl_new_std_menu() when adding menu controls.
  285. */
  286. struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
  287. const struct v4l2_ctrl_ops *ops,
  288. u32 id, s32 min, s32 max, u32 step, s32 def);
  289. /** v4l2_ctrl_new_std_menu() - Allocate and initialize a new standard V4L2 menu control.
  290. * @hdl: The control handler.
  291. * @ops: The control ops.
  292. * @id: The control ID.
  293. * @max: The control's maximum value.
  294. * @mask: The control's skip mask for menu controls. This makes it
  295. * easy to skip menu items that are not valid. If bit X is set,
  296. * then menu item X is skipped. Of course, this only works for
  297. * menus with <= 32 menu items. There are no menus that come
  298. * close to that number, so this is OK. Should we ever need more,
  299. * then this will have to be extended to a u64 or a bit array.
  300. * @def: The control's default value.
  301. *
  302. * Same as v4l2_ctrl_new_std(), but @min is set to 0 and the @mask value
  303. * determines which menu items are to be skipped.
  304. *
  305. * If @id refers to a non-menu control, then this function will return NULL.
  306. */
  307. struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
  308. const struct v4l2_ctrl_ops *ops,
  309. u32 id, s32 max, s32 mask, s32 def);
  310. /** v4l2_ctrl_add_ctrl() - Add a control from another handler to this handler.
  311. * @hdl: The control handler.
  312. * @ctrl: The control to add.
  313. *
  314. * It will return NULL if it was unable to add the control reference.
  315. * If the control already belonged to the handler, then it will do
  316. * nothing and just return @ctrl.
  317. */
  318. struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
  319. struct v4l2_ctrl *ctrl);
  320. /** v4l2_ctrl_add_handler() - Add all controls from handler @add to
  321. * handler @hdl.
  322. * @hdl: The control handler.
  323. * @add: The control handler whose controls you want to add to
  324. * the @hdl control handler.
  325. *
  326. * Does nothing if either of the two is a NULL pointer.
  327. * In case of an error @hdl->error will be set to the error code (if it
  328. * wasn't set already).
  329. */
  330. int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
  331. struct v4l2_ctrl_handler *add);
  332. /** v4l2_ctrl_cluster() - Mark all controls in the cluster as belonging to that cluster.
  333. * @ncontrols: The number of controls in this cluster.
  334. * @controls: The cluster control array of size @ncontrols.
  335. */
  336. void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls);
  337. /** v4l2_ctrl_find() - Find a control with the given ID.
  338. * @hdl: The control handler.
  339. * @id: The control ID to find.
  340. *
  341. * If @hdl == NULL this will return NULL as well. Will lock the handler so
  342. * do not use from inside &v4l2_ctrl_ops.
  343. */
  344. struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id);
  345. /** v4l2_ctrl_activate() - Make the control active or inactive.
  346. * @ctrl: The control to (de)activate.
  347. * @active: True if the control should become active.
  348. *
  349. * This sets or clears the V4L2_CTRL_FLAG_INACTIVE flag atomically.
  350. * Does nothing if @ctrl == NULL.
  351. * This will usually be called from within the s_ctrl op.
  352. *
  353. * This function can be called regardless of whether the control handler
  354. * is locked or not.
  355. */
  356. void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active);
  357. /** v4l2_ctrl_grab() - Mark the control as grabbed or not grabbed.
  358. * @ctrl: The control to (de)activate.
  359. * @grabbed: True if the control should become grabbed.
  360. *
  361. * This sets or clears the V4L2_CTRL_FLAG_GRABBED flag atomically.
  362. * Does nothing if @ctrl == NULL.
  363. * This will usually be called when starting or stopping streaming in the
  364. * driver.
  365. *
  366. * This function can be called regardless of whether the control handler
  367. * is locked or not.
  368. */
  369. void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed);
  370. /** v4l2_ctrl_lock() - Helper function to lock the handler
  371. * associated with the control.
  372. * @ctrl: The control to lock.
  373. */
  374. static inline void v4l2_ctrl_lock(struct v4l2_ctrl *ctrl)
  375. {
  376. mutex_lock(&ctrl->handler->lock);
  377. }
  378. /** v4l2_ctrl_lock() - Helper function to unlock the handler
  379. * associated with the control.
  380. * @ctrl: The control to unlock.
  381. */
  382. static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
  383. {
  384. mutex_unlock(&ctrl->handler->lock);
  385. }
  386. /** v4l2_ctrl_g_ctrl() - Helper function to get the control's value from within a driver.
  387. * @ctrl: The control.
  388. *
  389. * This returns the control's value safely by going through the control
  390. * framework. This function will lock the control's handler, so it cannot be
  391. * used from within the &v4l2_ctrl_ops functions.
  392. *
  393. * This function is for integer type controls only.
  394. */
  395. s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
  396. /** v4l2_ctrl_s_ctrl() - Helper function to set the control's value from within a driver.
  397. * @ctrl: The control.
  398. * @val: The new value.
  399. *
  400. * This set the control's new value safely by going through the control
  401. * framework. This function will lock the control's handler, so it cannot be
  402. * used from within the &v4l2_ctrl_ops functions.
  403. *
  404. * This function is for integer type controls only.
  405. */
  406. int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
  407. /* Helpers for ioctl_ops. If hdl == NULL then they will all return -EINVAL. */
  408. int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc);
  409. int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);
  410. int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
  411. int v4l2_s_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
  412. int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
  413. int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
  414. int v4l2_s_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
  415. /* Helpers for subdevices. If the associated ctrl_handler == NULL then they
  416. will all return -EINVAL. */
  417. int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
  418. int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
  419. int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
  420. int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
  421. int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
  422. int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
  423. int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
  424. #endif