v4l2-ctrls.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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/mutex.h>
  20. #include <linux/videodev2.h>
  21. /* forward references */
  22. struct file;
  23. struct v4l2_ctrl_handler;
  24. struct v4l2_ctrl_helper;
  25. struct v4l2_ctrl;
  26. struct video_device;
  27. struct v4l2_subdev;
  28. struct v4l2_subscribed_event;
  29. struct v4l2_fh;
  30. struct poll_table_struct;
  31. /** union v4l2_ctrl_ptr - A pointer to a control value.
  32. * @p_s32: Pointer to a 32-bit signed value.
  33. * @p_s64: Pointer to a 64-bit signed value.
  34. * @p_u8: Pointer to a 8-bit unsigned value.
  35. * @p_u16: Pointer to a 16-bit unsigned value.
  36. * @p_u32: Pointer to a 32-bit unsigned value.
  37. * @p_char: Pointer to a string.
  38. * @p: Pointer to a compound value.
  39. */
  40. union v4l2_ctrl_ptr {
  41. s32 *p_s32;
  42. s64 *p_s64;
  43. u8 *p_u8;
  44. u16 *p_u16;
  45. u32 *p_u32;
  46. char *p_char;
  47. void *p;
  48. };
  49. /** struct v4l2_ctrl_ops - The control operations that the driver has to provide.
  50. * @g_volatile_ctrl: Get a new value for this control. Generally only relevant
  51. * for volatile (and usually read-only) controls such as a control
  52. * that returns the current signal strength which changes
  53. * continuously.
  54. * If not set, then the currently cached value will be returned.
  55. * @try_ctrl: Test whether the control's value is valid. Only relevant when
  56. * the usual min/max/step checks are not sufficient.
  57. * @s_ctrl: Actually set the new control value. s_ctrl is compulsory. The
  58. * ctrl->handler->lock is held when these ops are called, so no
  59. * one else can access controls owned by that handler.
  60. */
  61. struct v4l2_ctrl_ops {
  62. int (*g_volatile_ctrl)(struct v4l2_ctrl *ctrl);
  63. int (*try_ctrl)(struct v4l2_ctrl *ctrl);
  64. int (*s_ctrl)(struct v4l2_ctrl *ctrl);
  65. };
  66. /** struct v4l2_ctrl_type_ops - The control type operations that the driver has to provide.
  67. * @equal: return true if both values are equal.
  68. * @init: initialize the value.
  69. * @log: log the value.
  70. * @validate: validate the value. Return 0 on success and a negative value otherwise.
  71. */
  72. struct v4l2_ctrl_type_ops {
  73. bool (*equal)(const struct v4l2_ctrl *ctrl, u32 idx,
  74. union v4l2_ctrl_ptr ptr1,
  75. union v4l2_ctrl_ptr ptr2);
  76. void (*init)(const struct v4l2_ctrl *ctrl, u32 idx,
  77. union v4l2_ctrl_ptr ptr);
  78. void (*log)(const struct v4l2_ctrl *ctrl);
  79. int (*validate)(const struct v4l2_ctrl *ctrl, u32 idx,
  80. union v4l2_ctrl_ptr ptr);
  81. };
  82. typedef void (*v4l2_ctrl_notify_fnc)(struct v4l2_ctrl *ctrl, void *priv);
  83. /** struct v4l2_ctrl - The control structure.
  84. * @node: The list node.
  85. * @ev_subs: The list of control event subscriptions.
  86. * @handler: The handler that owns the control.
  87. * @cluster: Point to start of cluster array.
  88. * @ncontrols: Number of controls in cluster array.
  89. * @done: Internal flag: set for each processed control.
  90. * @is_new: Set when the user specified a new value for this control. It
  91. * is also set when called from v4l2_ctrl_handler_setup. Drivers
  92. * should never set this flag.
  93. * @has_changed: Set when the current value differs from the new value. Drivers
  94. * should never use this flag.
  95. * @is_private: If set, then this control is private to its handler and it
  96. * will not be added to any other handlers. Drivers can set
  97. * this flag.
  98. * @is_auto: If set, then this control selects whether the other cluster
  99. * members are in 'automatic' mode or 'manual' mode. This is
  100. * used for autogain/gain type clusters. Drivers should never
  101. * set this flag directly.
  102. * @is_int: If set, then this control has a simple integer value (i.e. it
  103. * uses ctrl->val).
  104. * @is_string: If set, then this control has type V4L2_CTRL_TYPE_STRING.
  105. * @is_ptr: If set, then this control is an array and/or has type >= V4L2_CTRL_COMPOUND_TYPES
  106. * and/or has type V4L2_CTRL_TYPE_STRING. In other words, struct
  107. * v4l2_ext_control uses field p to point to the data.
  108. * @is_array: If set, then this control contains an N-dimensional array.
  109. * @has_volatiles: If set, then one or more members of the cluster are volatile.
  110. * Drivers should never touch this flag.
  111. * @call_notify: If set, then call the handler's notify function whenever the
  112. * control's value changes.
  113. * @manual_mode_value: If the is_auto flag is set, then this is the value
  114. * of the auto control that determines if that control is in
  115. * manual mode. So if the value of the auto control equals this
  116. * value, then the whole cluster is in manual mode. Drivers should
  117. * never set this flag directly.
  118. * @ops: The control ops.
  119. * @type_ops: The control type ops.
  120. * @id: The control ID.
  121. * @name: The control name.
  122. * @type: The control type.
  123. * @minimum: The control's minimum value.
  124. * @maximum: The control's maximum value.
  125. * @default_value: The control's default value.
  126. * @step: The control's step value for non-menu controls.
  127. * @elems: The number of elements in the N-dimensional array.
  128. * @elem_size: The size in bytes of the control.
  129. * @dims: The size of each dimension.
  130. * @nr_of_dims:The number of dimensions in @dims.
  131. * @menu_skip_mask: The control's skip mask for menu controls. This makes it
  132. * easy to skip menu items that are not valid. If bit X is set,
  133. * then menu item X is skipped. Of course, this only works for
  134. * menus with <= 32 menu items. There are no menus that come
  135. * close to that number, so this is OK. Should we ever need more,
  136. * then this will have to be extended to a u64 or a bit array.
  137. * @qmenu: A const char * array for all menu items. Array entries that are
  138. * empty strings ("") correspond to non-existing menu items (this
  139. * is in addition to the menu_skip_mask above). The last entry
  140. * must be NULL.
  141. * @flags: The control's flags.
  142. * @cur: The control's current value.
  143. * @val: The control's new s32 value.
  144. * @val64: The control's new s64 value.
  145. * @priv: The control's private pointer. For use by the driver. It is
  146. * untouched by the control framework. Note that this pointer is
  147. * not freed when the control is deleted. Should this be needed
  148. * then a new internal bitfield can be added to tell the framework
  149. * to free this pointer.
  150. */
  151. struct v4l2_ctrl {
  152. /* Administrative fields */
  153. struct list_head node;
  154. struct list_head ev_subs;
  155. struct v4l2_ctrl_handler *handler;
  156. struct v4l2_ctrl **cluster;
  157. unsigned ncontrols;
  158. unsigned int done:1;
  159. unsigned int is_new:1;
  160. unsigned int has_changed:1;
  161. unsigned int is_private:1;
  162. unsigned int is_auto:1;
  163. unsigned int is_int:1;
  164. unsigned int is_string:1;
  165. unsigned int is_ptr:1;
  166. unsigned int is_array:1;
  167. unsigned int has_volatiles:1;
  168. unsigned int call_notify:1;
  169. unsigned int manual_mode_value:8;
  170. const struct v4l2_ctrl_ops *ops;
  171. const struct v4l2_ctrl_type_ops *type_ops;
  172. u32 id;
  173. const char *name;
  174. enum v4l2_ctrl_type type;
  175. s64 minimum, maximum, default_value;
  176. u32 elems;
  177. u32 elem_size;
  178. u32 dims[V4L2_CTRL_MAX_DIMS];
  179. u32 nr_of_dims;
  180. union {
  181. u64 step;
  182. u64 menu_skip_mask;
  183. };
  184. union {
  185. const char * const *qmenu;
  186. const s64 *qmenu_int;
  187. };
  188. unsigned long flags;
  189. void *priv;
  190. s32 val;
  191. struct {
  192. s32 val;
  193. } cur;
  194. union v4l2_ctrl_ptr p_new;
  195. union v4l2_ctrl_ptr p_cur;
  196. };
  197. /** struct v4l2_ctrl_ref - The control reference.
  198. * @node: List node for the sorted list.
  199. * @next: Single-link list node for the hash.
  200. * @ctrl: The actual control information.
  201. * @helper: Pointer to helper struct. Used internally in prepare_ext_ctrls().
  202. *
  203. * Each control handler has a list of these refs. The list_head is used to
  204. * keep a sorted-by-control-ID list of all controls, while the next pointer
  205. * is used to link the control in the hash's bucket.
  206. */
  207. struct v4l2_ctrl_ref {
  208. struct list_head node;
  209. struct v4l2_ctrl_ref *next;
  210. struct v4l2_ctrl *ctrl;
  211. struct v4l2_ctrl_helper *helper;
  212. };
  213. /** struct v4l2_ctrl_handler - The control handler keeps track of all the
  214. * controls: both the controls owned by the handler and those inherited
  215. * from other handlers.
  216. * @_lock: Default for "lock".
  217. * @lock: Lock to control access to this handler and its controls.
  218. * May be replaced by the user right after init.
  219. * @ctrls: The list of controls owned by this handler.
  220. * @ctrl_refs: The list of control references.
  221. * @cached: The last found control reference. It is common that the same
  222. * control is needed multiple times, so this is a simple
  223. * optimization.
  224. * @buckets: Buckets for the hashing. Allows for quick control lookup.
  225. * @notify: A notify callback that is called whenever the control changes value.
  226. * Note that the handler's lock is held when the notify function
  227. * is called!
  228. * @notify_priv: Passed as argument to the v4l2_ctrl notify callback.
  229. * @nr_of_buckets: Total number of buckets in the array.
  230. * @error: The error code of the first failed control addition.
  231. */
  232. struct v4l2_ctrl_handler {
  233. struct mutex _lock;
  234. struct mutex *lock;
  235. struct list_head ctrls;
  236. struct list_head ctrl_refs;
  237. struct v4l2_ctrl_ref *cached;
  238. struct v4l2_ctrl_ref **buckets;
  239. v4l2_ctrl_notify_fnc notify;
  240. void *notify_priv;
  241. u16 nr_of_buckets;
  242. int error;
  243. };
  244. /** struct v4l2_ctrl_config - Control configuration structure.
  245. * @ops: The control ops.
  246. * @type_ops: The control type ops. Only needed for compound controls.
  247. * @id: The control ID.
  248. * @name: The control name.
  249. * @type: The control type.
  250. * @min: The control's minimum value.
  251. * @max: The control's maximum value.
  252. * @step: The control's step value for non-menu controls.
  253. * @def: The control's default value.
  254. * @dims: The size of each dimension.
  255. * @elem_size: The size in bytes of the control.
  256. * @flags: The control's flags.
  257. * @menu_skip_mask: The control's skip mask for menu controls. This makes it
  258. * easy to skip menu items that are not valid. If bit X is set,
  259. * then menu item X is skipped. Of course, this only works for
  260. * menus with <= 64 menu items. There are no menus that come
  261. * close to that number, so this is OK. Should we ever need more,
  262. * then this will have to be extended to a bit array.
  263. * @qmenu: A const char * array for all menu items. Array entries that are
  264. * empty strings ("") correspond to non-existing menu items (this
  265. * is in addition to the menu_skip_mask above). The last entry
  266. * must be NULL.
  267. * @is_private: If set, then this control is private to its handler and it
  268. * will not be added to any other handlers.
  269. */
  270. struct v4l2_ctrl_config {
  271. const struct v4l2_ctrl_ops *ops;
  272. const struct v4l2_ctrl_type_ops *type_ops;
  273. u32 id;
  274. const char *name;
  275. enum v4l2_ctrl_type type;
  276. s64 min;
  277. s64 max;
  278. u64 step;
  279. s64 def;
  280. u32 dims[V4L2_CTRL_MAX_DIMS];
  281. u32 elem_size;
  282. u32 flags;
  283. u64 menu_skip_mask;
  284. const char * const *qmenu;
  285. const s64 *qmenu_int;
  286. unsigned int is_private:1;
  287. };
  288. /** v4l2_ctrl_fill() - Fill in the control fields based on the control ID.
  289. *
  290. * This works for all standard V4L2 controls.
  291. * For non-standard controls it will only fill in the given arguments
  292. * and @name will be NULL.
  293. *
  294. * This function will overwrite the contents of @name, @type and @flags.
  295. * The contents of @min, @max, @step and @def may be modified depending on
  296. * the type.
  297. *
  298. * Do not use in drivers! It is used internally for backwards compatibility
  299. * control handling only. Once all drivers are converted to use the new
  300. * control framework this function will no longer be exported.
  301. */
  302. void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
  303. s64 *min, s64 *max, u64 *step, s64 *def, u32 *flags);
  304. /** v4l2_ctrl_handler_init_class() - Initialize the control handler.
  305. * @hdl: The control handler.
  306. * @nr_of_controls_hint: A hint of how many controls this handler is
  307. * expected to refer to. This is the total number, so including
  308. * any inherited controls. It doesn't have to be precise, but if
  309. * it is way off, then you either waste memory (too many buckets
  310. * are allocated) or the control lookup becomes slower (not enough
  311. * buckets are allocated, so there are more slow list lookups).
  312. * It will always work, though.
  313. * @key: Used by the lock validator if CONFIG_LOCKDEP is set.
  314. * @name: Used by the lock validator if CONFIG_LOCKDEP is set.
  315. *
  316. * Returns an error if the buckets could not be allocated. This error will
  317. * also be stored in @hdl->error.
  318. *
  319. * Never use this call directly, always use the v4l2_ctrl_handler_init
  320. * macro that hides the @key and @name arguments.
  321. */
  322. int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
  323. unsigned nr_of_controls_hint,
  324. struct lock_class_key *key, const char *name);
  325. #ifdef CONFIG_LOCKDEP
  326. #define v4l2_ctrl_handler_init(hdl, nr_of_controls_hint) \
  327. ( \
  328. ({ \
  329. static struct lock_class_key _key; \
  330. v4l2_ctrl_handler_init_class(hdl, nr_of_controls_hint, \
  331. &_key, \
  332. KBUILD_BASENAME ":" \
  333. __stringify(__LINE__) ":" \
  334. "(" #hdl ")->_lock"); \
  335. }) \
  336. )
  337. #else
  338. #define v4l2_ctrl_handler_init(hdl, nr_of_controls_hint) \
  339. v4l2_ctrl_handler_init_class(hdl, nr_of_controls_hint, NULL, NULL)
  340. #endif
  341. /** v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
  342. * the control list.
  343. * @hdl: The control handler.
  344. *
  345. * Does nothing if @hdl == NULL.
  346. */
  347. void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
  348. /** v4l2_ctrl_lock() - Helper function to lock the handler
  349. * associated with the control.
  350. * @ctrl: The control to lock.
  351. */
  352. static inline void v4l2_ctrl_lock(struct v4l2_ctrl *ctrl)
  353. {
  354. mutex_lock(ctrl->handler->lock);
  355. }
  356. /** v4l2_ctrl_unlock() - Helper function to unlock the handler
  357. * associated with the control.
  358. * @ctrl: The control to unlock.
  359. */
  360. static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
  361. {
  362. mutex_unlock(ctrl->handler->lock);
  363. }
  364. /** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
  365. * to the handler to initialize the hardware to the current control values.
  366. * @hdl: The control handler.
  367. *
  368. * Button controls will be skipped, as are read-only controls.
  369. *
  370. * If @hdl == NULL, then this just returns 0.
  371. */
  372. int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
  373. /** v4l2_ctrl_handler_log_status() - Log all controls owned by the handler.
  374. * @hdl: The control handler.
  375. * @prefix: The prefix to use when logging the control values. If the
  376. * prefix does not end with a space, then ": " will be added
  377. * after the prefix. If @prefix == NULL, then no prefix will be
  378. * used.
  379. *
  380. * For use with VIDIOC_LOG_STATUS.
  381. *
  382. * Does nothing if @hdl == NULL.
  383. */
  384. void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
  385. const char *prefix);
  386. /** v4l2_ctrl_new_custom() - Allocate and initialize a new custom V4L2
  387. * control.
  388. * @hdl: The control handler.
  389. * @cfg: The control's configuration data.
  390. * @priv: The control's driver-specific private data.
  391. *
  392. * If the &v4l2_ctrl struct could not be allocated then NULL is returned
  393. * and @hdl->error is set to the error code (if it wasn't set already).
  394. */
  395. struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
  396. const struct v4l2_ctrl_config *cfg, void *priv);
  397. /** v4l2_ctrl_new_std() - Allocate and initialize a new standard V4L2 non-menu control.
  398. * @hdl: The control handler.
  399. * @ops: The control ops.
  400. * @id: The control ID.
  401. * @min: The control's minimum value.
  402. * @max: The control's maximum value.
  403. * @step: The control's step value
  404. * @def: The control's default value.
  405. *
  406. * If the &v4l2_ctrl struct could not be allocated, or the control
  407. * ID is not known, then NULL is returned and @hdl->error is set to the
  408. * appropriate error code (if it wasn't set already).
  409. *
  410. * If @id refers to a menu control, then this function will return NULL.
  411. *
  412. * Use v4l2_ctrl_new_std_menu() when adding menu controls.
  413. */
  414. struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
  415. const struct v4l2_ctrl_ops *ops,
  416. u32 id, s64 min, s64 max, u64 step, s64 def);
  417. /** v4l2_ctrl_new_std_menu() - Allocate and initialize a new standard V4L2 menu control.
  418. * @hdl: The control handler.
  419. * @ops: The control ops.
  420. * @id: The control ID.
  421. * @max: The control's maximum value.
  422. * @mask: The control's skip mask for menu controls. This makes it
  423. * easy to skip menu items that are not valid. If bit X is set,
  424. * then menu item X is skipped. Of course, this only works for
  425. * menus with <= 64 menu items. There are no menus that come
  426. * close to that number, so this is OK. Should we ever need more,
  427. * then this will have to be extended to a bit array.
  428. * @def: The control's default value.
  429. *
  430. * Same as v4l2_ctrl_new_std(), but @min is set to 0 and the @mask value
  431. * determines which menu items are to be skipped.
  432. *
  433. * If @id refers to a non-menu control, then this function will return NULL.
  434. */
  435. struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
  436. const struct v4l2_ctrl_ops *ops,
  437. u32 id, u8 max, u64 mask, u8 def);
  438. /** v4l2_ctrl_new_std_menu_items() - Create a new standard V4L2 menu control
  439. * with driver specific menu.
  440. * @hdl: The control handler.
  441. * @ops: The control ops.
  442. * @id: The control ID.
  443. * @max: The control's maximum value.
  444. * @mask: The control's skip mask for menu controls. This makes it
  445. * easy to skip menu items that are not valid. If bit X is set,
  446. * then menu item X is skipped. Of course, this only works for
  447. * menus with <= 64 menu items. There are no menus that come
  448. * close to that number, so this is OK. Should we ever need more,
  449. * then this will have to be extended to a bit array.
  450. * @def: The control's default value.
  451. * @qmenu: The new menu.
  452. *
  453. * Same as v4l2_ctrl_new_std_menu(), but @qmenu will be the driver specific
  454. * menu of this control.
  455. *
  456. */
  457. struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
  458. const struct v4l2_ctrl_ops *ops, u32 id, u8 max,
  459. u64 mask, u8 def, const char * const *qmenu);
  460. /** v4l2_ctrl_new_int_menu() - Create a new standard V4L2 integer menu control.
  461. * @hdl: The control handler.
  462. * @ops: The control ops.
  463. * @id: The control ID.
  464. * @max: The control's maximum value.
  465. * @def: The control's default value.
  466. * @qmenu_int: The control's menu entries.
  467. *
  468. * Same as v4l2_ctrl_new_std_menu(), but @mask is set to 0 and it additionaly
  469. * takes as an argument an array of integers determining the menu items.
  470. *
  471. * If @id refers to a non-integer-menu control, then this function will return NULL.
  472. */
  473. struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
  474. const struct v4l2_ctrl_ops *ops,
  475. u32 id, u8 max, u8 def, const s64 *qmenu_int);
  476. /** v4l2_ctrl_add_ctrl() - Add a control from another handler to this handler.
  477. * @hdl: The control handler.
  478. * @ctrl: The control to add.
  479. *
  480. * It will return NULL if it was unable to add the control reference.
  481. * If the control already belonged to the handler, then it will do
  482. * nothing and just return @ctrl.
  483. */
  484. struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
  485. struct v4l2_ctrl *ctrl);
  486. /** v4l2_ctrl_add_handler() - Add all controls from handler @add to
  487. * handler @hdl.
  488. * @hdl: The control handler.
  489. * @add: The control handler whose controls you want to add to
  490. * the @hdl control handler.
  491. * @filter: This function will filter which controls should be added.
  492. *
  493. * Does nothing if either of the two handlers is a NULL pointer.
  494. * If @filter is NULL, then all controls are added. Otherwise only those
  495. * controls for which @filter returns true will be added.
  496. * In case of an error @hdl->error will be set to the error code (if it
  497. * wasn't set already).
  498. */
  499. int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
  500. struct v4l2_ctrl_handler *add,
  501. bool (*filter)(const struct v4l2_ctrl *ctrl));
  502. /** v4l2_ctrl_radio_filter() - Standard filter for radio controls.
  503. * @ctrl: The control that is filtered.
  504. *
  505. * This will return true for any controls that are valid for radio device
  506. * nodes. Those are all of the V4L2_CID_AUDIO_* user controls and all FM
  507. * transmitter class controls.
  508. *
  509. * This function is to be used with v4l2_ctrl_add_handler().
  510. */
  511. bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl);
  512. /** v4l2_ctrl_cluster() - Mark all controls in the cluster as belonging to that cluster.
  513. * @ncontrols: The number of controls in this cluster.
  514. * @controls: The cluster control array of size @ncontrols.
  515. */
  516. void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls);
  517. /** v4l2_ctrl_auto_cluster() - Mark all controls in the cluster as belonging to
  518. * that cluster and set it up for autofoo/foo-type handling.
  519. * @ncontrols: The number of controls in this cluster.
  520. * @controls: The cluster control array of size @ncontrols. The first control
  521. * must be the 'auto' control (e.g. autogain, autoexposure, etc.)
  522. * @manual_val: The value for the first control in the cluster that equals the
  523. * manual setting.
  524. * @set_volatile: If true, then all controls except the first auto control will
  525. * be volatile.
  526. *
  527. * Use for control groups where one control selects some automatic feature and
  528. * the other controls are only active whenever the automatic feature is turned
  529. * off (manual mode). Typical examples: autogain vs gain, auto-whitebalance vs
  530. * red and blue balance, etc.
  531. *
  532. * The behavior of such controls is as follows:
  533. *
  534. * When the autofoo control is set to automatic, then any manual controls
  535. * are set to inactive and any reads will call g_volatile_ctrl (if the control
  536. * was marked volatile).
  537. *
  538. * When the autofoo control is set to manual, then any manual controls will
  539. * be marked active, and any reads will just return the current value without
  540. * going through g_volatile_ctrl.
  541. *
  542. * In addition, this function will set the V4L2_CTRL_FLAG_UPDATE flag
  543. * on the autofoo control and V4L2_CTRL_FLAG_INACTIVE on the foo control(s)
  544. * if autofoo is in auto mode.
  545. */
  546. void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
  547. u8 manual_val, bool set_volatile);
  548. /** v4l2_ctrl_find() - Find a control with the given ID.
  549. * @hdl: The control handler.
  550. * @id: The control ID to find.
  551. *
  552. * If @hdl == NULL this will return NULL as well. Will lock the handler so
  553. * do not use from inside &v4l2_ctrl_ops.
  554. */
  555. struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id);
  556. /** v4l2_ctrl_activate() - Make the control active or inactive.
  557. * @ctrl: The control to (de)activate.
  558. * @active: True if the control should become active.
  559. *
  560. * This sets or clears the V4L2_CTRL_FLAG_INACTIVE flag atomically.
  561. * Does nothing if @ctrl == NULL.
  562. * This will usually be called from within the s_ctrl op.
  563. * The V4L2_EVENT_CTRL event will be generated afterwards.
  564. *
  565. * This function assumes that the control handler is locked.
  566. */
  567. void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active);
  568. /** v4l2_ctrl_grab() - Mark the control as grabbed or not grabbed.
  569. * @ctrl: The control to (de)activate.
  570. * @grabbed: True if the control should become grabbed.
  571. *
  572. * This sets or clears the V4L2_CTRL_FLAG_GRABBED flag atomically.
  573. * Does nothing if @ctrl == NULL.
  574. * The V4L2_EVENT_CTRL event will be generated afterwards.
  575. * This will usually be called when starting or stopping streaming in the
  576. * driver.
  577. *
  578. * This function assumes that the control handler is not locked and will
  579. * take the lock itself.
  580. */
  581. void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed);
  582. /** __v4l2_ctrl_modify_range() - Unlocked variant of v4l2_ctrl_modify_range() */
  583. int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
  584. s64 min, s64 max, u64 step, s64 def);
  585. /** v4l2_ctrl_modify_range() - Update the range of a control.
  586. * @ctrl: The control to update.
  587. * @min: The control's minimum value.
  588. * @max: The control's maximum value.
  589. * @step: The control's step value
  590. * @def: The control's default value.
  591. *
  592. * Update the range of a control on the fly. This works for control types
  593. * INTEGER, BOOLEAN, MENU, INTEGER MENU and BITMASK. For menu controls the
  594. * @step value is interpreted as a menu_skip_mask.
  595. *
  596. * An error is returned if one of the range arguments is invalid for this
  597. * control type.
  598. *
  599. * This function assumes that the control handler is not locked and will
  600. * take the lock itself.
  601. */
  602. static inline int v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
  603. s64 min, s64 max, u64 step, s64 def)
  604. {
  605. int rval;
  606. v4l2_ctrl_lock(ctrl);
  607. rval = __v4l2_ctrl_modify_range(ctrl, min, max, step, def);
  608. v4l2_ctrl_unlock(ctrl);
  609. return rval;
  610. }
  611. /** v4l2_ctrl_notify() - Function to set a notify callback for a control.
  612. * @ctrl: The control.
  613. * @notify: The callback function.
  614. * @priv: The callback private handle, passed as argument to the callback.
  615. *
  616. * This function sets a callback function for the control. If @ctrl is NULL,
  617. * then it will do nothing. If @notify is NULL, then the notify callback will
  618. * be removed.
  619. *
  620. * There can be only one notify. If another already exists, then a WARN_ON
  621. * will be issued and the function will do nothing.
  622. */
  623. void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv);
  624. /** v4l2_ctrl_get_name() - Get the name of the control
  625. * @id: The control ID.
  626. *
  627. * This function returns the name of the given control ID or NULL if it isn't
  628. * a known control.
  629. */
  630. const char *v4l2_ctrl_get_name(u32 id);
  631. /** v4l2_ctrl_get_menu() - Get the menu string array of the control
  632. * @id: The control ID.
  633. *
  634. * This function returns the NULL-terminated menu string array name of the
  635. * given control ID or NULL if it isn't a known menu control.
  636. */
  637. const char * const *v4l2_ctrl_get_menu(u32 id);
  638. /** v4l2_ctrl_get_int_menu() - Get the integer menu array of the control
  639. * @id: The control ID.
  640. * @len: The size of the integer array.
  641. *
  642. * This function returns the integer array of the given control ID or NULL if it
  643. * if it isn't a known integer menu control.
  644. */
  645. const s64 *v4l2_ctrl_get_int_menu(u32 id, u32 *len);
  646. /** v4l2_ctrl_g_ctrl() - Helper function to get the control's value from within a driver.
  647. * @ctrl: The control.
  648. *
  649. * This returns the control's value safely by going through the control
  650. * framework. This function will lock the control's handler, so it cannot be
  651. * used from within the &v4l2_ctrl_ops functions.
  652. *
  653. * This function is for integer type controls only.
  654. */
  655. s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
  656. /** __v4l2_ctrl_s_ctrl() - Unlocked variant of v4l2_ctrl_s_ctrl(). */
  657. int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
  658. /** v4l2_ctrl_s_ctrl() - Helper function to set the control's value from within a driver.
  659. * @ctrl: The control.
  660. * @val: The new value.
  661. *
  662. * This set the control's new value safely by going through the control
  663. * framework. This function will lock the control's handler, so it cannot be
  664. * used from within the &v4l2_ctrl_ops functions.
  665. *
  666. * This function is for integer type controls only.
  667. */
  668. static inline int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
  669. {
  670. int rval;
  671. v4l2_ctrl_lock(ctrl);
  672. rval = __v4l2_ctrl_s_ctrl(ctrl, val);
  673. v4l2_ctrl_unlock(ctrl);
  674. return rval;
  675. }
  676. /** v4l2_ctrl_g_ctrl_int64() - Helper function to get a 64-bit control's value from within a driver.
  677. * @ctrl: The control.
  678. *
  679. * This returns the control's value safely by going through the control
  680. * framework. This function will lock the control's handler, so it cannot be
  681. * used from within the &v4l2_ctrl_ops functions.
  682. *
  683. * This function is for 64-bit integer type controls only.
  684. */
  685. s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl);
  686. /** __v4l2_ctrl_s_ctrl_int64() - Unlocked variant of v4l2_ctrl_s_ctrl_int64(). */
  687. int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val);
  688. /** v4l2_ctrl_s_ctrl_int64() - Helper function to set a 64-bit control's value from within a driver.
  689. * @ctrl: The control.
  690. * @val: The new value.
  691. *
  692. * This set the control's new value safely by going through the control
  693. * framework. This function will lock the control's handler, so it cannot be
  694. * used from within the &v4l2_ctrl_ops functions.
  695. *
  696. * This function is for 64-bit integer type controls only.
  697. */
  698. static inline int v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
  699. {
  700. int rval;
  701. v4l2_ctrl_lock(ctrl);
  702. rval = __v4l2_ctrl_s_ctrl_int64(ctrl, val);
  703. v4l2_ctrl_unlock(ctrl);
  704. return rval;
  705. }
  706. /** __v4l2_ctrl_s_ctrl_string() - Unlocked variant of v4l2_ctrl_s_ctrl_string(). */
  707. int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s);
  708. /** v4l2_ctrl_s_ctrl_string() - Helper function to set a control's string value from within a driver.
  709. * @ctrl: The control.
  710. * @s: The new string.
  711. *
  712. * This set the control's new string safely by going through the control
  713. * framework. This function will lock the control's handler, so it cannot be
  714. * used from within the &v4l2_ctrl_ops functions.
  715. *
  716. * This function is for string type controls only.
  717. */
  718. static inline int v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
  719. {
  720. int rval;
  721. v4l2_ctrl_lock(ctrl);
  722. rval = __v4l2_ctrl_s_ctrl_string(ctrl, s);
  723. v4l2_ctrl_unlock(ctrl);
  724. return rval;
  725. }
  726. /* Internal helper functions that deal with control events. */
  727. extern const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops;
  728. void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new);
  729. void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new);
  730. /* Can be used as a vidioc_log_status function that just dumps all controls
  731. associated with the filehandle. */
  732. int v4l2_ctrl_log_status(struct file *file, void *fh);
  733. /* Can be used as a vidioc_subscribe_event function that just subscribes
  734. control events. */
  735. int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
  736. const struct v4l2_event_subscription *sub);
  737. /* Can be used as a poll function that just polls for control events. */
  738. unsigned int v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait);
  739. /* Helpers for ioctl_ops. If hdl == NULL then they will all return -EINVAL. */
  740. int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc);
  741. int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc);
  742. int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);
  743. int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
  744. int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
  745. struct v4l2_control *ctrl);
  746. int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
  747. int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
  748. int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
  749. struct v4l2_ext_controls *c);
  750. /* Helpers for subdevices. If the associated ctrl_handler == NULL then they
  751. will all return -EINVAL. */
  752. int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
  753. int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
  754. int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
  755. int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
  756. int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
  757. int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
  758. int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
  759. /* Can be used as a subscribe_event function that just subscribes control
  760. events. */
  761. int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  762. struct v4l2_event_subscription *sub);
  763. /* Log all controls owned by subdev's control handler. */
  764. int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd);
  765. #endif