v4l2-common.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. v4l2 common internal API header
  4. This header contains internal shared ioctl definitions for use by the
  5. internal low-level v4l2 drivers.
  6. Each ioctl begins with VIDIOC_INT_ to clearly mark that it is an internal
  7. define,
  8. Copyright (C) 2005 Hans Verkuil <[email protected]>
  9. */
  10. #ifndef V4L2_COMMON_H_
  11. #define V4L2_COMMON_H_
  12. #include <linux/time.h>
  13. #include <media/v4l2-dev.h>
  14. /* Common printk constructs for v4l-i2c drivers. These macros create a unique
  15. prefix consisting of the driver name, the adapter number and the i2c
  16. address. */
  17. #define v4l_printk(level, name, adapter, addr, fmt, arg...) \
  18. printk(level "%s %d-%04x: " fmt, name, i2c_adapter_id(adapter), addr , ## arg)
  19. #define v4l_client_printk(level, client, fmt, arg...) \
  20. v4l_printk(level, (client)->dev.driver->name, (client)->adapter, \
  21. (client)->addr, fmt , ## arg)
  22. #define v4l_err(client, fmt, arg...) \
  23. v4l_client_printk(KERN_ERR, client, fmt , ## arg)
  24. #define v4l_warn(client, fmt, arg...) \
  25. v4l_client_printk(KERN_WARNING, client, fmt , ## arg)
  26. #define v4l_info(client, fmt, arg...) \
  27. v4l_client_printk(KERN_INFO, client, fmt , ## arg)
  28. /* These three macros assume that the debug level is set with a module
  29. parameter called 'debug'. */
  30. #define v4l_dbg(level, debug, client, fmt, arg...) \
  31. do { \
  32. if (debug >= (level)) \
  33. v4l_client_printk(KERN_DEBUG, client, fmt , ## arg); \
  34. } while (0)
  35. /* Add a version of v4l_dbg to be used on drivers using dev_foo() macros */
  36. #define dev_dbg_lvl(__dev, __level, __debug, __fmt, __arg...) \
  37. do { \
  38. if (__debug >= (__level)) \
  39. dev_printk(KERN_DEBUG, __dev, __fmt, ##__arg); \
  40. } while (0)
  41. /* ------------------------------------------------------------------------- */
  42. /* These printk constructs can be used with v4l2_device and v4l2_subdev */
  43. #define v4l2_printk(level, dev, fmt, arg...) \
  44. printk(level "%s: " fmt, (dev)->name , ## arg)
  45. #define v4l2_err(dev, fmt, arg...) \
  46. v4l2_printk(KERN_ERR, dev, fmt , ## arg)
  47. #define v4l2_warn(dev, fmt, arg...) \
  48. v4l2_printk(KERN_WARNING, dev, fmt , ## arg)
  49. #define v4l2_info(dev, fmt, arg...) \
  50. v4l2_printk(KERN_INFO, dev, fmt , ## arg)
  51. /* These three macros assume that the debug level is set with a module
  52. parameter called 'debug'. */
  53. #define v4l2_dbg(level, debug, dev, fmt, arg...) \
  54. do { \
  55. if (debug >= (level)) \
  56. v4l2_printk(KERN_DEBUG, dev, fmt , ## arg); \
  57. } while (0)
  58. /**
  59. * v4l2_ctrl_query_fill- Fill in a struct v4l2_queryctrl
  60. *
  61. * @qctrl: pointer to the &struct v4l2_queryctrl to be filled
  62. * @min: minimum value for the control
  63. * @max: maximum value for the control
  64. * @step: control step
  65. * @def: default value for the control
  66. *
  67. * Fills the &struct v4l2_queryctrl fields for the query control.
  68. *
  69. * .. note::
  70. *
  71. * This function assumes that the @qctrl->id field is filled.
  72. *
  73. * Returns -EINVAL if the control is not known by the V4L2 core, 0 on success.
  74. */
  75. int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
  76. s32 min, s32 max, s32 step, s32 def);
  77. /* ------------------------------------------------------------------------- */
  78. struct v4l2_device;
  79. struct v4l2_subdev;
  80. struct v4l2_subdev_ops;
  81. /* I2C Helper functions */
  82. #include <linux/i2c.h>
  83. /**
  84. * enum v4l2_i2c_tuner_type - specifies the range of tuner address that
  85. * should be used when seeking for I2C devices.
  86. *
  87. * @ADDRS_RADIO: Radio tuner addresses.
  88. * Represent the following I2C addresses:
  89. * 0x10 (if compiled with tea5761 support)
  90. * and 0x60.
  91. * @ADDRS_DEMOD: Demod tuner addresses.
  92. * Represent the following I2C addresses:
  93. * 0x42, 0x43, 0x4a and 0x4b.
  94. * @ADDRS_TV: TV tuner addresses.
  95. * Represent the following I2C addresses:
  96. * 0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62,
  97. * 0x63 and 0x64.
  98. * @ADDRS_TV_WITH_DEMOD: TV tuner addresses if demod is present, this
  99. * excludes addresses used by the demodulator
  100. * from the list of candidates.
  101. * Represent the following I2C addresses:
  102. * 0x60, 0x61, 0x62, 0x63 and 0x64.
  103. *
  104. * NOTE: All I2C addresses above use the 7-bit notation.
  105. */
  106. enum v4l2_i2c_tuner_type {
  107. ADDRS_RADIO,
  108. ADDRS_DEMOD,
  109. ADDRS_TV,
  110. ADDRS_TV_WITH_DEMOD,
  111. };
  112. #if defined(CONFIG_VIDEO_V4L2_I2C)
  113. /**
  114. * v4l2_i2c_new_subdev - Load an i2c module and return an initialized
  115. * &struct v4l2_subdev.
  116. *
  117. * @v4l2_dev: pointer to &struct v4l2_device
  118. * @adapter: pointer to struct i2c_adapter
  119. * @client_type: name of the chip that's on the adapter.
  120. * @addr: I2C address. If zero, it will use @probe_addrs
  121. * @probe_addrs: array with a list of address. The last entry at such
  122. * array should be %I2C_CLIENT_END.
  123. *
  124. * returns a &struct v4l2_subdev pointer.
  125. */
  126. struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
  127. struct i2c_adapter *adapter, const char *client_type,
  128. u8 addr, const unsigned short *probe_addrs);
  129. /**
  130. * v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized
  131. * &struct v4l2_subdev.
  132. *
  133. * @v4l2_dev: pointer to &struct v4l2_device
  134. * @adapter: pointer to struct i2c_adapter
  135. * @info: pointer to struct i2c_board_info used to replace the irq,
  136. * platform_data and addr arguments.
  137. * @probe_addrs: array with a list of address. The last entry at such
  138. * array should be %I2C_CLIENT_END.
  139. *
  140. * returns a &struct v4l2_subdev pointer.
  141. */
  142. struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
  143. struct i2c_adapter *adapter, struct i2c_board_info *info,
  144. const unsigned short *probe_addrs);
  145. /**
  146. * v4l2_i2c_subdev_set_name - Set name for an I²C sub-device
  147. *
  148. * @sd: pointer to &struct v4l2_subdev
  149. * @client: pointer to struct i2c_client
  150. * @devname: the name of the device; if NULL, the I²C device drivers's name
  151. * will be used
  152. * @postfix: sub-device specific string to put right after the I²C device name;
  153. * may be NULL
  154. */
  155. void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
  156. const char *devname, const char *postfix);
  157. /**
  158. * v4l2_i2c_subdev_init - Initializes a &struct v4l2_subdev with data from
  159. * an i2c_client struct.
  160. *
  161. * @sd: pointer to &struct v4l2_subdev
  162. * @client: pointer to struct i2c_client
  163. * @ops: pointer to &struct v4l2_subdev_ops
  164. */
  165. void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
  166. const struct v4l2_subdev_ops *ops);
  167. /**
  168. * v4l2_i2c_subdev_addr - returns i2c client address of &struct v4l2_subdev.
  169. *
  170. * @sd: pointer to &struct v4l2_subdev
  171. *
  172. * Returns the address of an I2C sub-device
  173. */
  174. unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd);
  175. /**
  176. * v4l2_i2c_tuner_addrs - Return a list of I2C tuner addresses to probe.
  177. *
  178. * @type: type of the tuner to seek, as defined by
  179. * &enum v4l2_i2c_tuner_type.
  180. *
  181. * NOTE: Use only if the tuner addresses are unknown.
  182. */
  183. const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type);
  184. /**
  185. * v4l2_i2c_subdev_unregister - Unregister a v4l2_subdev
  186. *
  187. * @sd: pointer to &struct v4l2_subdev
  188. */
  189. void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd);
  190. #else
  191. static inline struct v4l2_subdev *
  192. v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
  193. struct i2c_adapter *adapter, const char *client_type,
  194. u8 addr, const unsigned short *probe_addrs)
  195. {
  196. return NULL;
  197. }
  198. static inline struct v4l2_subdev *
  199. v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
  200. struct i2c_adapter *adapter, struct i2c_board_info *info,
  201. const unsigned short *probe_addrs)
  202. {
  203. return NULL;
  204. }
  205. static inline void
  206. v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
  207. const char *devname, const char *postfix)
  208. {}
  209. static inline void
  210. v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
  211. const struct v4l2_subdev_ops *ops)
  212. {}
  213. static inline unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
  214. {
  215. return I2C_CLIENT_END;
  216. }
  217. static inline const unsigned short *
  218. v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
  219. {
  220. return NULL;
  221. }
  222. static inline void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd)
  223. {}
  224. #endif
  225. /* ------------------------------------------------------------------------- */
  226. /* SPI Helper functions */
  227. #include <linux/spi/spi.h>
  228. #if defined(CONFIG_SPI)
  229. /**
  230. * v4l2_spi_new_subdev - Load an spi module and return an initialized
  231. * &struct v4l2_subdev.
  232. *
  233. *
  234. * @v4l2_dev: pointer to &struct v4l2_device.
  235. * @master: pointer to struct spi_master.
  236. * @info: pointer to struct spi_board_info.
  237. *
  238. * returns a &struct v4l2_subdev pointer.
  239. */
  240. struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
  241. struct spi_master *master, struct spi_board_info *info);
  242. /**
  243. * v4l2_spi_subdev_init - Initialize a v4l2_subdev with data from an
  244. * spi_device struct.
  245. *
  246. * @sd: pointer to &struct v4l2_subdev
  247. * @spi: pointer to struct spi_device.
  248. * @ops: pointer to &struct v4l2_subdev_ops
  249. */
  250. void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
  251. const struct v4l2_subdev_ops *ops);
  252. /**
  253. * v4l2_spi_subdev_unregister - Unregister a v4l2_subdev
  254. *
  255. * @sd: pointer to &struct v4l2_subdev
  256. */
  257. void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd);
  258. #else
  259. static inline struct v4l2_subdev *
  260. v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
  261. struct spi_master *master, struct spi_board_info *info)
  262. {
  263. return NULL;
  264. }
  265. static inline void
  266. v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
  267. const struct v4l2_subdev_ops *ops)
  268. {}
  269. static inline void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd)
  270. {}
  271. #endif
  272. /* ------------------------------------------------------------------------- */
  273. /*
  274. * FIXME: these remaining ioctls/structs should be removed as well, but they
  275. * are still used in tuner-simple.c (TUNER_SET_CONFIG) and cx18/ivtv (RESET).
  276. * To remove these ioctls some more cleanup is needed in those modules.
  277. *
  278. * It doesn't make much sense on documenting them, as what we really want is
  279. * to get rid of them.
  280. */
  281. /* s_config */
  282. struct v4l2_priv_tun_config {
  283. int tuner;
  284. void *priv;
  285. };
  286. #define TUNER_SET_CONFIG _IOW('d', 92, struct v4l2_priv_tun_config)
  287. #define VIDIOC_INT_RESET _IOW ('d', 102, u32)
  288. /* ------------------------------------------------------------------------- */
  289. /* Miscellaneous helper functions */
  290. /**
  291. * v4l_bound_align_image - adjust video dimensions according to
  292. * a given constraints.
  293. *
  294. * @width: pointer to width that will be adjusted if needed.
  295. * @wmin: minimum width.
  296. * @wmax: maximum width.
  297. * @walign: least significant bit on width.
  298. * @height: pointer to height that will be adjusted if needed.
  299. * @hmin: minimum height.
  300. * @hmax: maximum height.
  301. * @halign: least significant bit on height.
  302. * @salign: least significant bit for the image size (e. g.
  303. * :math:`width * height`).
  304. *
  305. * Clip an image to have @width between @wmin and @wmax, and @height between
  306. * @hmin and @hmax, inclusive.
  307. *
  308. * Additionally, the @width will be a multiple of :math:`2^{walign}`,
  309. * the @height will be a multiple of :math:`2^{halign}`, and the overall
  310. * size :math:`width * height` will be a multiple of :math:`2^{salign}`.
  311. *
  312. * .. note::
  313. *
  314. * #. The clipping rectangle may be shrunk or enlarged to fit the alignment
  315. * constraints.
  316. * #. @wmax must not be smaller than @wmin.
  317. * #. @hmax must not be smaller than @hmin.
  318. * #. The alignments must not be so high there are no possible image
  319. * sizes within the allowed bounds.
  320. * #. @wmin and @hmin must be at least 1 (don't use 0).
  321. * #. For @walign, @halign and @salign, if you don't care about a certain
  322. * alignment, specify ``0``, as :math:`2^0 = 1` and one byte alignment
  323. * is equivalent to no alignment.
  324. * #. If you only want to adjust downward, specify a maximum that's the
  325. * same as the initial value.
  326. */
  327. void v4l_bound_align_image(unsigned int *width, unsigned int wmin,
  328. unsigned int wmax, unsigned int walign,
  329. unsigned int *height, unsigned int hmin,
  330. unsigned int hmax, unsigned int halign,
  331. unsigned int salign);
  332. /**
  333. * v4l2_find_nearest_size - Find the nearest size among a discrete
  334. * set of resolutions contained in an array of a driver specific struct.
  335. *
  336. * @array: a driver specific array of image sizes
  337. * @array_size: the length of the driver specific array of image sizes
  338. * @width_field: the name of the width field in the driver specific struct
  339. * @height_field: the name of the height field in the driver specific struct
  340. * @width: desired width.
  341. * @height: desired height.
  342. *
  343. * Finds the closest resolution to minimize the width and height differences
  344. * between what requested and the supported resolutions. The size of the width
  345. * and height fields in the driver specific must equal to that of u32, i.e. four
  346. * bytes.
  347. *
  348. * Returns the best match or NULL if the length of the array is zero.
  349. */
  350. #define v4l2_find_nearest_size(array, array_size, width_field, height_field, \
  351. width, height) \
  352. ({ \
  353. BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \
  354. sizeof((array)->height_field) != sizeof(u32)); \
  355. (typeof(&(array)[0]))__v4l2_find_nearest_size( \
  356. (array), array_size, sizeof(*(array)), \
  357. offsetof(typeof(*(array)), width_field), \
  358. offsetof(typeof(*(array)), height_field), \
  359. width, height); \
  360. })
  361. const void *
  362. __v4l2_find_nearest_size(const void *array, size_t array_size,
  363. size_t entry_size, size_t width_offset,
  364. size_t height_offset, s32 width, s32 height);
  365. /**
  366. * v4l2_g_parm_cap - helper routine for vidioc_g_parm to fill this in by
  367. * calling the g_frame_interval op of the given subdev. It only works
  368. * for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
  369. * function name.
  370. *
  371. * @vdev: the struct video_device pointer. Used to determine the device caps.
  372. * @sd: the sub-device pointer.
  373. * @a: the VIDIOC_G_PARM argument.
  374. */
  375. int v4l2_g_parm_cap(struct video_device *vdev,
  376. struct v4l2_subdev *sd, struct v4l2_streamparm *a);
  377. /**
  378. * v4l2_s_parm_cap - helper routine for vidioc_s_parm to fill this in by
  379. * calling the s_frame_interval op of the given subdev. It only works
  380. * for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
  381. * function name.
  382. *
  383. * @vdev: the struct video_device pointer. Used to determine the device caps.
  384. * @sd: the sub-device pointer.
  385. * @a: the VIDIOC_S_PARM argument.
  386. */
  387. int v4l2_s_parm_cap(struct video_device *vdev,
  388. struct v4l2_subdev *sd, struct v4l2_streamparm *a);
  389. /* Compare two v4l2_fract structs */
  390. #define V4L2_FRACT_COMPARE(a, OP, b) \
  391. ((u64)(a).numerator * (b).denominator OP \
  392. (u64)(b).numerator * (a).denominator)
  393. /* ------------------------------------------------------------------------- */
  394. /* Pixel format and FourCC helpers */
  395. /**
  396. * enum v4l2_pixel_encoding - specifies the pixel encoding value
  397. *
  398. * @V4L2_PIXEL_ENC_UNKNOWN: Pixel encoding is unknown/un-initialized
  399. * @V4L2_PIXEL_ENC_YUV: Pixel encoding is YUV
  400. * @V4L2_PIXEL_ENC_RGB: Pixel encoding is RGB
  401. * @V4L2_PIXEL_ENC_BAYER: Pixel encoding is Bayer
  402. */
  403. enum v4l2_pixel_encoding {
  404. V4L2_PIXEL_ENC_UNKNOWN = 0,
  405. V4L2_PIXEL_ENC_YUV = 1,
  406. V4L2_PIXEL_ENC_RGB = 2,
  407. V4L2_PIXEL_ENC_BAYER = 3,
  408. };
  409. /**
  410. * struct v4l2_format_info - information about a V4L2 format
  411. * @format: 4CC format identifier (V4L2_PIX_FMT_*)
  412. * @pixel_enc: Pixel encoding (see enum v4l2_pixel_encoding above)
  413. * @mem_planes: Number of memory planes, which includes the alpha plane (1 to 4).
  414. * @comp_planes: Number of component planes, which includes the alpha plane (1 to 4).
  415. * @bpp: Array of per-plane bytes per pixel
  416. * @hdiv: Horizontal chroma subsampling factor
  417. * @vdiv: Vertical chroma subsampling factor
  418. * @block_w: Per-plane macroblock pixel width (optional)
  419. * @block_h: Per-plane macroblock pixel height (optional)
  420. */
  421. struct v4l2_format_info {
  422. u32 format;
  423. u8 pixel_enc;
  424. u8 mem_planes;
  425. u8 comp_planes;
  426. u8 bpp[4];
  427. u8 hdiv;
  428. u8 vdiv;
  429. u8 block_w[4];
  430. u8 block_h[4];
  431. };
  432. static inline bool v4l2_is_format_rgb(const struct v4l2_format_info *f)
  433. {
  434. return f && f->pixel_enc == V4L2_PIXEL_ENC_RGB;
  435. }
  436. static inline bool v4l2_is_format_yuv(const struct v4l2_format_info *f)
  437. {
  438. return f && f->pixel_enc == V4L2_PIXEL_ENC_YUV;
  439. }
  440. static inline bool v4l2_is_format_bayer(const struct v4l2_format_info *f)
  441. {
  442. return f && f->pixel_enc == V4L2_PIXEL_ENC_BAYER;
  443. }
  444. const struct v4l2_format_info *v4l2_format_info(u32 format);
  445. void v4l2_apply_frmsize_constraints(u32 *width, u32 *height,
  446. const struct v4l2_frmsize_stepwise *frmsize);
  447. int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,
  448. u32 width, u32 height);
  449. int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
  450. u32 width, u32 height);
  451. /**
  452. * v4l2_get_link_freq - Get link rate from transmitter
  453. *
  454. * @handler: The transmitter's control handler
  455. * @mul: The multiplier between pixel rate and link frequency. Bits per pixel on
  456. * D-PHY, samples per clock on parallel. 0 otherwise.
  457. * @div: The divisor between pixel rate and link frequency. Number of data lanes
  458. * times two on D-PHY, 1 on parallel. 0 otherwise.
  459. *
  460. * This function is intended for obtaining the link frequency from the
  461. * transmitter sub-devices. It returns the link rate, either from the
  462. * V4L2_CID_LINK_FREQ control implemented by the transmitter, or value
  463. * calculated based on the V4L2_CID_PIXEL_RATE implemented by the transmitter.
  464. *
  465. * Returns link frequency on success, otherwise a negative error code:
  466. * -ENOENT: Link frequency or pixel rate control not found
  467. * -EINVAL: Invalid link frequency value
  468. */
  469. s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
  470. unsigned int div);
  471. void v4l2_simplify_fraction(u32 *numerator, u32 *denominator,
  472. unsigned int n_terms, unsigned int threshold);
  473. u32 v4l2_fraction_to_interval(u32 numerator, u32 denominator);
  474. static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf)
  475. {
  476. /*
  477. * When the timestamp comes from 32-bit user space, there may be
  478. * uninitialized data in tv_usec, so cast it to u32.
  479. * Otherwise allow invalid input for backwards compatibility.
  480. */
  481. return buf->timestamp.tv_sec * NSEC_PER_SEC +
  482. (u32)buf->timestamp.tv_usec * NSEC_PER_USEC;
  483. }
  484. static inline void v4l2_buffer_set_timestamp(struct v4l2_buffer *buf,
  485. u64 timestamp)
  486. {
  487. struct timespec64 ts = ns_to_timespec64(timestamp);
  488. buf->timestamp.tv_sec = ts.tv_sec;
  489. buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  490. }
  491. static inline bool v4l2_is_colorspace_valid(__u32 colorspace)
  492. {
  493. return colorspace > V4L2_COLORSPACE_DEFAULT &&
  494. colorspace < V4L2_COLORSPACE_LAST;
  495. }
  496. static inline bool v4l2_is_xfer_func_valid(__u32 xfer_func)
  497. {
  498. return xfer_func > V4L2_XFER_FUNC_DEFAULT &&
  499. xfer_func < V4L2_XFER_FUNC_LAST;
  500. }
  501. static inline bool v4l2_is_ycbcr_enc_valid(__u8 ycbcr_enc)
  502. {
  503. return ycbcr_enc > V4L2_YCBCR_ENC_DEFAULT &&
  504. ycbcr_enc < V4L2_YCBCR_ENC_LAST;
  505. }
  506. static inline bool v4l2_is_hsv_enc_valid(__u8 hsv_enc)
  507. {
  508. return hsv_enc == V4L2_HSV_ENC_180 || hsv_enc == V4L2_HSV_ENC_256;
  509. }
  510. static inline bool v4l2_is_quant_valid(__u8 quantization)
  511. {
  512. return quantization == V4L2_QUANTIZATION_FULL_RANGE ||
  513. quantization == V4L2_QUANTIZATION_LIM_RANGE;
  514. }
  515. #endif /* V4L2_COMMON_H_ */