v4l2-ctrls.h 33 KB

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