blob: 150ee16ebd81184e68d66c934001d36973809e61 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Hans Verkuilb2f06482005-11-13 16:07:55 -08002/*
3 v4l2 common internal API header
4
5 This header contains internal shared ioctl definitions for use by the
6 internal low-level v4l2 drivers.
7 Each ioctl begins with VIDIOC_INT_ to clearly mark that it is an internal
8 define,
9
10 Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
11
Hans Verkuilb2f06482005-11-13 16:07:55 -080012 */
13
14#ifndef V4L2_COMMON_H_
15#define V4L2_COMMON_H_
16
Arnd Bergmann77cdffc2019-12-16 15:15:00 +010017#include <linux/time.h>
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030018#include <media/v4l2-dev.h>
19
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -040020/* Common printk constructs for v4l-i2c drivers. These macros create a unique
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020021 prefix consisting of the driver name, the adapter number and the i2c
22 address. */
23#define v4l_printk(level, name, adapter, addr, fmt, arg...) \
24 printk(level "%s %d-%04x: " fmt, name, i2c_adapter_id(adapter), addr , ## arg)
25
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -020026#define v4l_client_printk(level, client, fmt, arg...) \
Lars-Peter Clausenf9d32f22013-09-29 10:51:01 +020027 v4l_printk(level, (client)->dev.driver->name, (client)->adapter, \
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020028 (client)->addr, fmt , ## arg)
29
30#define v4l_err(client, fmt, arg...) \
31 v4l_client_printk(KERN_ERR, client, fmt , ## arg)
32
33#define v4l_warn(client, fmt, arg...) \
34 v4l_client_printk(KERN_WARNING, client, fmt , ## arg)
35
36#define v4l_info(client, fmt, arg...) \
37 v4l_client_printk(KERN_INFO, client, fmt , ## arg)
38
39/* These three macros assume that the debug level is set with a module
40 parameter called 'debug'. */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -020041#define v4l_dbg(level, debug, client, fmt, arg...) \
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -050042 do { \
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020043 if (debug >= (level)) \
44 v4l_client_printk(KERN_DEBUG, client, fmt , ## arg); \
45 } while (0)
46
Mauro Carvalho Chehaba41231d2016-11-16 08:35:08 -020047/* Add a version of v4l_dbg to be used on drivers using dev_foo() macros */
48#define dev_dbg_lvl(__dev, __level, __debug, __fmt, __arg...) \
49 do { \
50 if (__debug >= (__level)) \
51 dev_printk(KERN_DEBUG, __dev, __fmt, ##__arg); \
52 } while (0)
53
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020054/* ------------------------------------------------------------------------- */
55
Hans Verkuildd991202008-11-23 12:19:45 -030056/* These printk constructs can be used with v4l2_device and v4l2_subdev */
57#define v4l2_printk(level, dev, fmt, arg...) \
58 printk(level "%s: " fmt, (dev)->name , ## arg)
59
60#define v4l2_err(dev, fmt, arg...) \
61 v4l2_printk(KERN_ERR, dev, fmt , ## arg)
62
63#define v4l2_warn(dev, fmt, arg...) \
64 v4l2_printk(KERN_WARNING, dev, fmt , ## arg)
65
66#define v4l2_info(dev, fmt, arg...) \
67 v4l2_printk(KERN_INFO, dev, fmt , ## arg)
68
69/* These three macros assume that the debug level is set with a module
70 parameter called 'debug'. */
71#define v4l2_dbg(level, debug, dev, fmt, arg...) \
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -050072 do { \
Hans Verkuildd991202008-11-23 12:19:45 -030073 if (debug >= (level)) \
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -050074 v4l2_printk(KERN_DEBUG, dev, fmt , ## arg); \
Hans Verkuildd991202008-11-23 12:19:45 -030075 } while (0)
76
Mauro Carvalho Chehabb8719152016-07-21 12:26:04 -030077/**
78 * v4l2_ctrl_query_fill- Fill in a struct v4l2_queryctrl
79 *
80 * @qctrl: pointer to the &struct v4l2_queryctrl to be filled
81 * @min: minimum value for the control
82 * @max: maximum value for the control
83 * @step: control step
84 * @def: default value for the control
85 *
86 * Fills the &struct v4l2_queryctrl fields for the query control.
87 *
88 * .. note::
89 *
90 * This function assumes that the @qctrl->id field is filled.
91 *
92 * Returns -EINVAL if the control is not known by the V4L2 core, 0 on success.
93 */
Hans Verkuildd991202008-11-23 12:19:45 -030094
Mauro Carvalho Chehabb8719152016-07-21 12:26:04 -030095int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
96 s32 min, s32 max, s32 step, s32 def);
Hans Verkuil9cb23182006-06-18 14:11:08 -030097
98/* ------------------------------------------------------------------------- */
99
Hans Verkuildd991202008-11-23 12:19:45 -0300100struct v4l2_device;
101struct v4l2_subdev;
102struct v4l2_subdev_ops;
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300103
Ezequiel Garcia02283b92019-08-15 13:48:03 -0300104/* I2C Helper functions */
105#include <linux/i2c.h>
106
107/**
108 * enum v4l2_i2c_tuner_type - specifies the range of tuner address that
109 * should be used when seeking for I2C devices.
110 *
111 * @ADDRS_RADIO: Radio tuner addresses.
112 * Represent the following I2C addresses:
113 * 0x10 (if compiled with tea5761 support)
114 * and 0x60.
115 * @ADDRS_DEMOD: Demod tuner addresses.
116 * Represent the following I2C addresses:
117 * 0x42, 0x43, 0x4a and 0x4b.
118 * @ADDRS_TV: TV tuner addresses.
119 * Represent the following I2C addresses:
120 * 0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62,
121 * 0x63 and 0x64.
122 * @ADDRS_TV_WITH_DEMOD: TV tuner addresses if demod is present, this
123 * excludes addresses used by the demodulator
124 * from the list of candidates.
125 * Represent the following I2C addresses:
126 * 0x60, 0x61, 0x62, 0x63 and 0x64.
127 *
128 * NOTE: All I2C addresses above use the 7-bit notation.
129 */
130enum v4l2_i2c_tuner_type {
131 ADDRS_RADIO,
132 ADDRS_DEMOD,
133 ADDRS_TV,
134 ADDRS_TV_WITH_DEMOD,
135};
136
137#if defined(CONFIG_VIDEO_V4L2_I2C)
138
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300139/**
140 * v4l2_i2c_new_subdev - Load an i2c module and return an initialized
141 * &struct v4l2_subdev.
142 *
143 * @v4l2_dev: pointer to &struct v4l2_device
144 * @adapter: pointer to struct i2c_adapter
145 * @client_type: name of the chip that's on the adapter.
146 * @addr: I2C address. If zero, it will use @probe_addrs
147 * @probe_addrs: array with a list of address. The last entry at such
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500148 * array should be %I2C_CLIENT_END.
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300149 *
150 * returns a &struct v4l2_subdev pointer.
151 */
Hans Verkuil3c7c9372011-01-08 07:08:02 -0300152struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300153 struct i2c_adapter *adapter, const char *client_type,
Hans Verkuilf0222c72009-06-09 17:12:33 -0300154 u8 addr, const unsigned short *probe_addrs);
155
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300156/**
157 * v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized
158 * &struct v4l2_subdev.
159 *
160 * @v4l2_dev: pointer to &struct v4l2_device
161 * @adapter: pointer to struct i2c_adapter
162 * @info: pointer to struct i2c_board_info used to replace the irq,
163 * platform_data and addr arguments.
164 * @probe_addrs: array with a list of address. The last entry at such
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500165 * array should be %I2C_CLIENT_END.
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300166 *
167 * returns a &struct v4l2_subdev pointer.
168 */
Hans Verkuilf0222c72009-06-09 17:12:33 -0300169struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300170 struct i2c_adapter *adapter, struct i2c_board_info *info,
171 const unsigned short *probe_addrs);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300172
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300173/**
Sakari Ailus06582932018-08-29 05:52:46 -0400174 * v4l2_i2c_subdev_set_name - Set name for an I²C sub-device
175 *
176 * @sd: pointer to &struct v4l2_subdev
177 * @client: pointer to struct i2c_client
178 * @devname: the name of the device; if NULL, the I²C device's name will be used
179 * @postfix: sub-device specific string to put right after the I²C device name;
180 * may be NULL
181 */
182void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
183 const char *devname, const char *postfix);
184
185/**
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300186 * v4l2_i2c_subdev_init - Initializes a &struct v4l2_subdev with data from
187 * an i2c_client struct.
188 *
189 * @sd: pointer to &struct v4l2_subdev
190 * @client: pointer to struct i2c_client
191 * @ops: pointer to &struct v4l2_subdev_ops
192 */
Hans Verkuildd991202008-11-23 12:19:45 -0300193void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
194 const struct v4l2_subdev_ops *ops);
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300195
196/**
197 * v4l2_i2c_subdev_addr - returns i2c client address of &struct v4l2_subdev.
198 *
199 * @sd: pointer to &struct v4l2_subdev
200 *
201 * Returns the address of an I2C sub-device
202 */
Hans Verkuilab3731902009-02-21 18:08:41 -0300203unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd);
Hans Verkuildd991202008-11-23 12:19:45 -0300204
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400205/**
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400206 * v4l2_i2c_tuner_addrs - Return a list of I2C tuner addresses to probe.
207 *
208 * @type: type of the tuner to seek, as defined by
209 * &enum v4l2_i2c_tuner_type.
210 *
211 * NOTE: Use only if the tuner addresses are unknown.
212 */
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300213const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type);
214
Ezequiel Garcia51ff3922019-08-15 13:48:05 -0300215/**
216 * v4l2_i2c_subdev_unregister - Unregister a v4l2_subdev
217 *
218 * @sd: pointer to &struct v4l2_subdev
219 */
220void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd);
221
Ezequiel Garcia02283b92019-08-15 13:48:03 -0300222#else
223
224static inline struct v4l2_subdev *
225v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
226 struct i2c_adapter *adapter, const char *client_type,
227 u8 addr, const unsigned short *probe_addrs)
228{
229 return NULL;
230}
231
232static inline struct v4l2_subdev *
233v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
234 struct i2c_adapter *adapter, struct i2c_board_info *info,
235 const unsigned short *probe_addrs)
236{
237 return NULL;
238}
239
240static inline void
241v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
242 const char *devname, const char *postfix)
243{}
244
245static inline void
246v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
247 const struct v4l2_subdev_ops *ops)
248{}
249
250static inline unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
251{
252 return I2C_CLIENT_END;
253}
254
255static inline const unsigned short *
256v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
257{
258 return NULL;
259}
260
Ezequiel Garcia51ff3922019-08-15 13:48:05 -0300261static inline void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd)
262{}
263
Ezequiel Garcia02283b92019-08-15 13:48:03 -0300264#endif
265
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300266/* ------------------------------------------------------------------------- */
267
Dmitri Belimov85e09212010-03-23 11:23:29 -0300268/* SPI Helper functions */
Dmitri Belimov85e09212010-03-23 11:23:29 -0300269
270#include <linux/spi/spi.h>
271
Ezequiel Garcia7c795df2019-08-15 13:48:02 -0300272#if defined(CONFIG_SPI)
Dmitri Belimov85e09212010-03-23 11:23:29 -0300273
Mauro Carvalho Chehabb8719152016-07-21 12:26:04 -0300274/**
275 * v4l2_spi_new_subdev - Load an spi module and return an initialized
276 * &struct v4l2_subdev.
277 *
278 *
279 * @v4l2_dev: pointer to &struct v4l2_device.
280 * @master: pointer to struct spi_master.
281 * @info: pointer to struct spi_board_info.
282 *
283 * returns a &struct v4l2_subdev pointer.
284 */
Dmitri Belimov85e09212010-03-23 11:23:29 -0300285struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
286 struct spi_master *master, struct spi_board_info *info);
287
Mauro Carvalho Chehabb8719152016-07-21 12:26:04 -0300288/**
289 * v4l2_spi_subdev_init - Initialize a v4l2_subdev with data from an
290 * spi_device struct.
291 *
292 * @sd: pointer to &struct v4l2_subdev
293 * @spi: pointer to struct spi_device.
294 * @ops: pointer to &struct v4l2_subdev_ops
295 */
Dmitri Belimov85e09212010-03-23 11:23:29 -0300296void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
297 const struct v4l2_subdev_ops *ops);
Ezequiel Garcia7c795df2019-08-15 13:48:02 -0300298
Ezequiel Garciaa9cff392019-08-15 13:48:04 -0300299/**
300 * v4l2_spi_subdev_unregister - Unregister a v4l2_subdev
301 *
302 * @sd: pointer to &struct v4l2_subdev
303 */
304void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd);
305
Ezequiel Garcia7c795df2019-08-15 13:48:02 -0300306#else
307
308static inline struct v4l2_subdev *
309v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
310 struct spi_master *master, struct spi_board_info *info)
311{
312 return NULL;
313}
314
315static inline void
316v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
317 const struct v4l2_subdev_ops *ops)
318{}
319
Ezequiel Garciaa9cff392019-08-15 13:48:04 -0300320static inline void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd)
321{}
Dmitri Belimov85e09212010-03-23 11:23:29 -0300322#endif
323
324/* ------------------------------------------------------------------------- */
325
Mauro Carvalho Chehab01154ef2017-09-21 10:04:30 -0400326/*
327 * FIXME: these remaining ioctls/structs should be removed as well, but they
328 * are still used in tuner-simple.c (TUNER_SET_CONFIG) and cx18/ivtv (RESET).
329 * To remove these ioctls some more cleanup is needed in those modules.
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400330 *
331 * It doesn't make much sense on documenting them, as what we really want is
332 * to get rid of them.
Mauro Carvalho Chehab01154ef2017-09-21 10:04:30 -0400333 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200334
Hans Verkuil78a3b4d2009-04-01 03:41:09 -0300335/* s_config */
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300336struct v4l2_priv_tun_config {
337 int tuner;
338 void *priv;
339};
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300340#define TUNER_SET_CONFIG _IOW('d', 92, struct v4l2_priv_tun_config)
Hans Verkuil757d2502006-01-23 17:11:10 -0200341
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500342#define VIDIOC_INT_RESET _IOW ('d', 102, u32)
Hans Verkuil6c31e592009-04-02 11:37:41 -0300343
Trent Piephob0d31592009-05-30 21:45:46 -0300344/* ------------------------------------------------------------------------- */
345
346/* Miscellaneous helper functions */
347
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400348/**
349 * v4l_bound_align_image - adjust video dimensions according to
350 * a given constraints.
351 *
352 * @width: pointer to width that will be adjusted if needed.
353 * @wmin: minimum width.
354 * @wmax: maximum width.
355 * @walign: least significant bit on width.
356 * @height: pointer to height that will be adjusted if needed.
357 * @hmin: minimum height.
358 * @hmax: maximum height.
Niklas Söderlundae5a8ca2018-09-12 20:07:38 -0400359 * @halign: least significant bit on height.
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400360 * @salign: least significant bit for the image size (e. g.
361 * :math:`width * height`).
362 *
363 * Clip an image to have @width between @wmin and @wmax, and @height between
364 * @hmin and @hmax, inclusive.
365 *
366 * Additionally, the @width will be a multiple of :math:`2^{walign}`,
367 * the @height will be a multiple of :math:`2^{halign}`, and the overall
368 * size :math:`width * height` will be a multiple of :math:`2^{salign}`.
369 *
370 * .. note::
371 *
372 * #. The clipping rectangle may be shrunk or enlarged to fit the alignment
373 * constraints.
374 * #. @wmax must not be smaller than @wmin.
375 * #. @hmax must not be smaller than @hmin.
376 * #. The alignments must not be so high there are no possible image
377 * sizes within the allowed bounds.
378 * #. @wmin and @hmin must be at least 1 (don't use 0).
379 * #. For @walign, @halign and @salign, if you don't care about a certain
380 * alignment, specify ``0``, as :math:`2^0 = 1` and one byte alignment
381 * is equivalent to no alignment.
382 * #. If you only want to adjust downward, specify a maximum that's the
383 * same as the initial value.
384 */
385void v4l_bound_align_image(unsigned int *width, unsigned int wmin,
Trent Piephob0d31592009-05-30 21:45:46 -0300386 unsigned int wmax, unsigned int walign,
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400387 unsigned int *height, unsigned int hmin,
Trent Piephob0d31592009-05-30 21:45:46 -0300388 unsigned int hmax, unsigned int halign,
389 unsigned int salign);
Hans Verkuil3fd8e642010-09-30 09:29:37 -0300390
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400391/**
Sakari Ailus95ce9c22018-02-23 04:50:14 -0500392 * v4l2_find_nearest_size - Find the nearest size among a discrete
393 * set of resolutions contained in an array of a driver specific struct.
394 *
395 * @array: a driver specific array of image sizes
Sakari Ailusd2dc57b2018-03-21 16:29:27 -0400396 * @array_size: the length of the driver specific array of image sizes
Sakari Ailus95ce9c22018-02-23 04:50:14 -0500397 * @width_field: the name of the width field in the driver specific struct
398 * @height_field: the name of the height field in the driver specific struct
399 * @width: desired width.
400 * @height: desired height.
401 *
402 * Finds the closest resolution to minimize the width and height differences
403 * between what requested and the supported resolutions. The size of the width
404 * and height fields in the driver specific must equal to that of u32, i.e. four
405 * bytes.
406 *
407 * Returns the best match or NULL if the length of the array is zero.
408 */
Sakari Ailusd2dc57b2018-03-21 16:29:27 -0400409#define v4l2_find_nearest_size(array, array_size, width_field, height_field, \
Sakari Ailus95ce9c22018-02-23 04:50:14 -0500410 width, height) \
411 ({ \
412 BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \
413 sizeof((array)->height_field) != sizeof(u32)); \
Sakari Ailus2bbc46e2018-06-21 05:41:21 -0400414 (typeof(&(array)[0]))__v4l2_find_nearest_size( \
Sakari Ailusd2dc57b2018-03-21 16:29:27 -0400415 (array), array_size, sizeof(*(array)), \
Sakari Ailus95ce9c22018-02-23 04:50:14 -0500416 offsetof(typeof(*(array)), width_field), \
417 offsetof(typeof(*(array)), height_field), \
418 width, height); \
419 })
420const void *
421__v4l2_find_nearest_size(const void *array, size_t array_size,
422 size_t entry_size, size_t width_offset,
423 size_t height_offset, s32 width, s32 height);
424
425/**
Hans Verkuil672de9a2018-01-22 03:58:36 -0500426 * v4l2_g_parm_cap - helper routine for vidioc_g_parm to fill this in by
427 * calling the g_frame_interval op of the given subdev. It only works
428 * for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
429 * function name.
430 *
431 * @vdev: the struct video_device pointer. Used to determine the device caps.
432 * @sd: the sub-device pointer.
433 * @a: the VIDIOC_G_PARM argument.
434 */
435int v4l2_g_parm_cap(struct video_device *vdev,
436 struct v4l2_subdev *sd, struct v4l2_streamparm *a);
437
438/**
439 * v4l2_s_parm_cap - helper routine for vidioc_s_parm to fill this in by
440 * calling the s_frame_interval op of the given subdev. It only works
441 * for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
442 * function name.
443 *
444 * @vdev: the struct video_device pointer. Used to determine the device caps.
445 * @sd: the sub-device pointer.
446 * @a: the VIDIOC_S_PARM argument.
447 */
448int v4l2_s_parm_cap(struct video_device *vdev,
449 struct v4l2_subdev *sd, struct v4l2_streamparm *a);
450
Akinobu Mita85de5e02018-10-20 10:26:25 -0400451/* Compare two v4l2_fract structs */
452#define V4L2_FRACT_COMPARE(a, OP, b) \
453 ((u64)(a).numerator * (b).denominator OP \
454 (u64)(b).numerator * (a).denominator)
455
Ezequiel Garciaf44b9692019-03-28 14:07:04 -0400456/* ------------------------------------------------------------------------- */
457
458/* Pixel format and FourCC helpers */
459
460/**
Benoit Parrotd5a897c2019-10-07 12:10:07 -0300461 * enum v4l2_pixel_encoding - specifies the pixel encoding value
462 *
463 * @V4L2_PIXEL_ENC_UNKNOWN: Pixel encoding is unknown/un-initialized
464 * @V4L2_PIXEL_ENC_YUV: Pixel encoding is YUV
465 * @V4L2_PIXEL_ENC_RGB: Pixel encoding is RGB
466 * @V4L2_PIXEL_ENC_BAYER: Pixel encoding is Bayer
467 */
468enum v4l2_pixel_encoding {
469 V4L2_PIXEL_ENC_UNKNOWN = 0,
470 V4L2_PIXEL_ENC_YUV = 1,
471 V4L2_PIXEL_ENC_RGB = 2,
472 V4L2_PIXEL_ENC_BAYER = 3,
473};
474
475/**
Ezequiel Garciaf44b9692019-03-28 14:07:04 -0400476 * struct v4l2_format_info - information about a V4L2 format
477 * @format: 4CC format identifier (V4L2_PIX_FMT_*)
Benoit Parrotd5a897c2019-10-07 12:10:07 -0300478 * @pixel_enc: Pixel encoding (see enum v4l2_pixel_encoding above)
Ezequiel Garciaf44b9692019-03-28 14:07:04 -0400479 * @mem_planes: Number of memory planes, which includes the alpha plane (1 to 4).
480 * @comp_planes: Number of component planes, which includes the alpha plane (1 to 4).
481 * @bpp: Array of per-plane bytes per pixel
482 * @hdiv: Horizontal chroma subsampling factor
483 * @vdiv: Vertical chroma subsampling factor
484 * @block_w: Per-plane macroblock pixel width (optional)
485 * @block_h: Per-plane macroblock pixel height (optional)
486 */
487struct v4l2_format_info {
488 u32 format;
Benoit Parrotd5a897c2019-10-07 12:10:07 -0300489 u8 pixel_enc;
Ezequiel Garciaf44b9692019-03-28 14:07:04 -0400490 u8 mem_planes;
491 u8 comp_planes;
492 u8 bpp[4];
493 u8 hdiv;
494 u8 vdiv;
495 u8 block_w[4];
496 u8 block_h[4];
497};
498
Benoit Parrotd5a897c2019-10-07 12:10:07 -0300499static inline bool v4l2_is_format_rgb(const struct v4l2_format_info *f)
500{
501 return f && f->pixel_enc == V4L2_PIXEL_ENC_RGB;
502}
Ezequiel Garciaf44b9692019-03-28 14:07:04 -0400503
Benoit Parrotd5a897c2019-10-07 12:10:07 -0300504static inline bool v4l2_is_format_yuv(const struct v4l2_format_info *f)
505{
506 return f && f->pixel_enc == V4L2_PIXEL_ENC_YUV;
507}
508
509static inline bool v4l2_is_format_bayer(const struct v4l2_format_info *f)
510{
511 return f && f->pixel_enc == V4L2_PIXEL_ENC_BAYER;
512}
513
514const struct v4l2_format_info *v4l2_format_info(u32 format);
Boris Brezillon32cddf92019-05-28 13:02:18 -0400515void v4l2_apply_frmsize_constraints(u32 *width, u32 *height,
516 const struct v4l2_frmsize_stepwise *frmsize);
Boris Brezillonce57a822019-05-28 13:02:17 -0400517int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,
518 u32 width, u32 height);
519int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
520 u32 width, u32 height);
Ezequiel Garciaf44b9692019-03-28 14:07:04 -0400521
Arnd Bergmann77cdffc2019-12-16 15:15:00 +0100522static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf)
523{
524 /*
525 * When the timestamp comes from 32-bit user space, there may be
526 * uninitialized data in tv_usec, so cast it to u32.
527 * Otherwise allow invalid input for backwards compatibility.
528 */
529 return buf->timestamp.tv_sec * NSEC_PER_SEC +
530 (u32)buf->timestamp.tv_usec * NSEC_PER_USEC;
531}
532
533static inline void v4l2_buffer_set_timestamp(struct v4l2_buffer *buf,
534 u64 timestamp)
535{
536 struct timespec64 ts = ns_to_timespec64(timestamp);
537
538 buf->timestamp.tv_sec = ts.tv_sec;
539 buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
540}
541
Hans Verkuilb2f06482005-11-13 16:07:55 -0800542#endif /* V4L2_COMMON_H_ */