]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - include/media/v4l2-common.h
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156
[mirror_ubuntu-focal-kernel.git] / include / media / v4l2-common.h
CommitLineData
1a59d1b8 1/* SPDX-License-Identifier: GPL-2.0-or-later */
b2f0648f
HV
2/*
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
b2f0648f
HV
12 */
13
14#ifndef V4L2_COMMON_H_
15#define V4L2_COMMON_H_
16
401998fa
MCC
17#include <media/v4l2-dev.h>
18
76a59fe7 19/* Common printk constructs for v4l-i2c drivers. These macros create a unique
7e8b09ea
HV
20 prefix consisting of the driver name, the adapter number and the i2c
21 address. */
22#define v4l_printk(level, name, adapter, addr, fmt, arg...) \
23 printk(level "%s %d-%04x: " fmt, name, i2c_adapter_id(adapter), addr , ## arg)
24
cab462f7 25#define v4l_client_printk(level, client, fmt, arg...) \
f9d32f25 26 v4l_printk(level, (client)->dev.driver->name, (client)->adapter, \
7e8b09ea
HV
27 (client)->addr, fmt , ## arg)
28
29#define v4l_err(client, fmt, arg...) \
30 v4l_client_printk(KERN_ERR, client, fmt , ## arg)
31
32#define v4l_warn(client, fmt, arg...) \
33 v4l_client_printk(KERN_WARNING, client, fmt , ## arg)
34
35#define v4l_info(client, fmt, arg...) \
36 v4l_client_printk(KERN_INFO, client, fmt , ## arg)
37
38/* These three macros assume that the debug level is set with a module
39 parameter called 'debug'. */
f167cb4e 40#define v4l_dbg(level, debug, client, fmt, arg...) \
6e6a8b5a 41 do { \
7e8b09ea
HV
42 if (debug >= (level)) \
43 v4l_client_printk(KERN_DEBUG, client, fmt , ## arg); \
44 } while (0)
45
a41231d5
MCC
46/* Add a version of v4l_dbg to be used on drivers using dev_foo() macros */
47#define dev_dbg_lvl(__dev, __level, __debug, __fmt, __arg...) \
48 do { \
49 if (__debug >= (__level)) \
50 dev_printk(KERN_DEBUG, __dev, __fmt, ##__arg); \
51 } while (0)
52
7e8b09ea
HV
53/* ------------------------------------------------------------------------- */
54
dd99120c
HV
55/* These printk constructs can be used with v4l2_device and v4l2_subdev */
56#define v4l2_printk(level, dev, fmt, arg...) \
57 printk(level "%s: " fmt, (dev)->name , ## arg)
58
59#define v4l2_err(dev, fmt, arg...) \
60 v4l2_printk(KERN_ERR, dev, fmt , ## arg)
61
62#define v4l2_warn(dev, fmt, arg...) \
63 v4l2_printk(KERN_WARNING, dev, fmt , ## arg)
64
65#define v4l2_info(dev, fmt, arg...) \
66 v4l2_printk(KERN_INFO, dev, fmt , ## arg)
67
68/* These three macros assume that the debug level is set with a module
69 parameter called 'debug'. */
70#define v4l2_dbg(level, debug, dev, fmt, arg...) \
6e6a8b5a 71 do { \
dd99120c 72 if (debug >= (level)) \
6e6a8b5a 73 v4l2_printk(KERN_DEBUG, dev, fmt , ## arg); \
dd99120c
HV
74 } while (0)
75
b8719158
MCC
76/**
77 * v4l2_ctrl_query_fill- Fill in a struct v4l2_queryctrl
78 *
79 * @qctrl: pointer to the &struct v4l2_queryctrl to be filled
80 * @min: minimum value for the control
81 * @max: maximum value for the control
82 * @step: control step
83 * @def: default value for the control
84 *
85 * Fills the &struct v4l2_queryctrl fields for the query control.
86 *
87 * .. note::
88 *
89 * This function assumes that the @qctrl->id field is filled.
90 *
91 * Returns -EINVAL if the control is not known by the V4L2 core, 0 on success.
92 */
dd99120c 93
b8719158
MCC
94int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
95 s32 min, s32 max, s32 step, s32 def);
9cb2318b
HV
96
97/* ------------------------------------------------------------------------- */
98
78a3b4db 99/* I2C Helper functions */
8ffbc655
HV
100
101struct i2c_driver;
102struct i2c_adapter;
103struct i2c_client;
d2653e92 104struct i2c_device_id;
dd99120c
HV
105struct v4l2_device;
106struct v4l2_subdev;
107struct v4l2_subdev_ops;
8ffbc655 108
a39c57f8
MCC
109/**
110 * v4l2_i2c_new_subdev - Load an i2c module and return an initialized
111 * &struct v4l2_subdev.
112 *
113 * @v4l2_dev: pointer to &struct v4l2_device
114 * @adapter: pointer to struct i2c_adapter
115 * @client_type: name of the chip that's on the adapter.
116 * @addr: I2C address. If zero, it will use @probe_addrs
117 * @probe_addrs: array with a list of address. The last entry at such
4a3fad70 118 * array should be %I2C_CLIENT_END.
a39c57f8
MCC
119 *
120 * returns a &struct v4l2_subdev pointer.
121 */
3c7c9370 122struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
9a1f8b34 123 struct i2c_adapter *adapter, const char *client_type,
53dacb15 124 u8 addr, const unsigned short *probe_addrs);
f0222c7d 125
f0222c7d
HV
126struct i2c_board_info;
127
a39c57f8
MCC
128/**
129 * v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized
130 * &struct v4l2_subdev.
131 *
132 * @v4l2_dev: pointer to &struct v4l2_device
133 * @adapter: pointer to struct i2c_adapter
134 * @info: pointer to struct i2c_board_info used to replace the irq,
135 * platform_data and addr arguments.
136 * @probe_addrs: array with a list of address. The last entry at such
4a3fad70 137 * array should be %I2C_CLIENT_END.
a39c57f8
MCC
138 *
139 * returns a &struct v4l2_subdev pointer.
140 */
f0222c7d 141struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
9a1f8b34
LP
142 struct i2c_adapter *adapter, struct i2c_board_info *info,
143 const unsigned short *probe_addrs);
f0222c7d 144
06582930
SA
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's name will be used
151 * @postfix: sub-device specific string to put right after the I²C device name;
152 * may be NULL
153 */
154void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
155 const char *devname, const char *postfix);
156
a39c57f8
MCC
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 */
dd99120c
HV
165void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
166 const struct v4l2_subdev_ops *ops);
a39c57f8
MCC
167
168/**
169 * v4l2_i2c_subdev_addr - returns i2c client address of &struct v4l2_subdev.
170 *
171 * @sd: pointer to &struct v4l2_subdev
172 *
173 * Returns the address of an I2C sub-device
174 */
ab373190 175unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd);
dd99120c 176
76a59fe7
MCC
177/**
178 * enum v4l2_i2c_tuner_type - specifies the range of tuner address that
179 * should be used when seeking for I2C devices.
180 *
181 * @ADDRS_RADIO: Radio tuner addresses.
182 * Represent the following I2C addresses:
183 * 0x10 (if compiled with tea5761 support)
184 * and 0x60.
185 * @ADDRS_DEMOD: Demod tuner addresses.
186 * Represent the following I2C addresses:
187 * 0x42, 0x43, 0x4a and 0x4b.
188 * @ADDRS_TV: TV tuner addresses.
189 * Represent the following I2C addresses:
190 * 0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62,
191 * 0x63 and 0x64.
192 * @ADDRS_TV_WITH_DEMOD: TV tuner addresses if demod is present, this
193 * excludes addresses used by the demodulator
194 * from the list of candidates.
195 * Represent the following I2C addresses:
196 * 0x60, 0x61, 0x62, 0x63 and 0x64.
197 *
198 * NOTE: All I2C addresses above use the 7-bit notation.
199 */
c7d29e2f 200enum v4l2_i2c_tuner_type {
76a59fe7
MCC
201 ADDRS_RADIO,
202 ADDRS_DEMOD,
203 ADDRS_TV,
c7d29e2f
HV
204 ADDRS_TV_WITH_DEMOD,
205};
76a59fe7
MCC
206/**
207 * v4l2_i2c_tuner_addrs - Return a list of I2C tuner addresses to probe.
208 *
209 * @type: type of the tuner to seek, as defined by
210 * &enum v4l2_i2c_tuner_type.
211 *
212 * NOTE: Use only if the tuner addresses are unknown.
213 */
c7d29e2f
HV
214const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type);
215
8ffbc655 216/* ------------------------------------------------------------------------- */
85e09219
DB
217
218/* SPI Helper functions */
219#if defined(CONFIG_SPI)
220
221#include <linux/spi/spi.h>
222
223struct spi_device;
224
b8719158
MCC
225/**
226 * v4l2_spi_new_subdev - Load an spi module and return an initialized
227 * &struct v4l2_subdev.
228 *
229 *
230 * @v4l2_dev: pointer to &struct v4l2_device.
231 * @master: pointer to struct spi_master.
232 * @info: pointer to struct spi_board_info.
233 *
234 * returns a &struct v4l2_subdev pointer.
235 */
85e09219
DB
236struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
237 struct spi_master *master, struct spi_board_info *info);
238
b8719158
MCC
239/**
240 * v4l2_spi_subdev_init - Initialize a v4l2_subdev with data from an
241 * spi_device struct.
242 *
243 * @sd: pointer to &struct v4l2_subdev
244 * @spi: pointer to struct spi_device.
245 * @ops: pointer to &struct v4l2_subdev_ops
246 */
85e09219
DB
247void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
248 const struct v4l2_subdev_ops *ops);
249#endif
250
251/* ------------------------------------------------------------------------- */
8ffbc655 252
01154ef5
MCC
253/*
254 * FIXME: these remaining ioctls/structs should be removed as well, but they
255 * are still used in tuner-simple.c (TUNER_SET_CONFIG) and cx18/ivtv (RESET).
256 * To remove these ioctls some more cleanup is needed in those modules.
76a59fe7
MCC
257 *
258 * It doesn't make much sense on documenting them, as what we really want is
259 * to get rid of them.
01154ef5 260 */
b2f0648f 261
78a3b4db 262/* s_config */
7f171123
MCC
263struct v4l2_priv_tun_config {
264 int tuner;
265 void *priv;
266};
7f171123 267#define TUNER_SET_CONFIG _IOW('d', 92, struct v4l2_priv_tun_config)
757d2505 268
6e6a8b5a 269#define VIDIOC_INT_RESET _IOW ('d', 102, u32)
6c31e598 270
b0d3159b
TP
271/* ------------------------------------------------------------------------- */
272
273/* Miscellaneous helper functions */
274
76a59fe7
MCC
275/**
276 * v4l_bound_align_image - adjust video dimensions according to
277 * a given constraints.
278 *
279 * @width: pointer to width that will be adjusted if needed.
280 * @wmin: minimum width.
281 * @wmax: maximum width.
282 * @walign: least significant bit on width.
283 * @height: pointer to height that will be adjusted if needed.
284 * @hmin: minimum height.
285 * @hmax: maximum height.
ae5a8ca8 286 * @halign: least significant bit on height.
76a59fe7
MCC
287 * @salign: least significant bit for the image size (e. g.
288 * :math:`width * height`).
289 *
290 * Clip an image to have @width between @wmin and @wmax, and @height between
291 * @hmin and @hmax, inclusive.
292 *
293 * Additionally, the @width will be a multiple of :math:`2^{walign}`,
294 * the @height will be a multiple of :math:`2^{halign}`, and the overall
295 * size :math:`width * height` will be a multiple of :math:`2^{salign}`.
296 *
297 * .. note::
298 *
299 * #. The clipping rectangle may be shrunk or enlarged to fit the alignment
300 * constraints.
301 * #. @wmax must not be smaller than @wmin.
302 * #. @hmax must not be smaller than @hmin.
303 * #. The alignments must not be so high there are no possible image
304 * sizes within the allowed bounds.
305 * #. @wmin and @hmin must be at least 1 (don't use 0).
306 * #. For @walign, @halign and @salign, if you don't care about a certain
307 * alignment, specify ``0``, as :math:`2^0 = 1` and one byte alignment
308 * is equivalent to no alignment.
309 * #. If you only want to adjust downward, specify a maximum that's the
310 * same as the initial value.
311 */
312void v4l_bound_align_image(unsigned int *width, unsigned int wmin,
b0d3159b 313 unsigned int wmax, unsigned int walign,
76a59fe7 314 unsigned int *height, unsigned int hmin,
b0d3159b
TP
315 unsigned int hmax, unsigned int halign,
316 unsigned int salign);
3fd8e647 317
95ce9c28
SA
318/**
319 * v4l2_find_nearest_size - Find the nearest size among a discrete
320 * set of resolutions contained in an array of a driver specific struct.
321 *
322 * @array: a driver specific array of image sizes
d2dc57b1 323 * @array_size: the length of the driver specific array of image sizes
95ce9c28
SA
324 * @width_field: the name of the width field in the driver specific struct
325 * @height_field: the name of the height field in the driver specific struct
326 * @width: desired width.
327 * @height: desired height.
328 *
329 * Finds the closest resolution to minimize the width and height differences
330 * between what requested and the supported resolutions. The size of the width
331 * and height fields in the driver specific must equal to that of u32, i.e. four
332 * bytes.
333 *
334 * Returns the best match or NULL if the length of the array is zero.
335 */
d2dc57b1 336#define v4l2_find_nearest_size(array, array_size, width_field, height_field, \
95ce9c28
SA
337 width, height) \
338 ({ \
339 BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \
340 sizeof((array)->height_field) != sizeof(u32)); \
2bbc46e8 341 (typeof(&(array)[0]))__v4l2_find_nearest_size( \
d2dc57b1 342 (array), array_size, sizeof(*(array)), \
95ce9c28
SA
343 offsetof(typeof(*(array)), width_field), \
344 offsetof(typeof(*(array)), height_field), \
345 width, height); \
346 })
347const void *
348__v4l2_find_nearest_size(const void *array, size_t array_size,
349 size_t entry_size, size_t width_offset,
350 size_t height_offset, s32 width, s32 height);
351
672de9a7
HV
352/**
353 * v4l2_g_parm_cap - helper routine for vidioc_g_parm to fill this in by
354 * calling the g_frame_interval op of the given subdev. It only works
355 * for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
356 * function name.
357 *
358 * @vdev: the struct video_device pointer. Used to determine the device caps.
359 * @sd: the sub-device pointer.
360 * @a: the VIDIOC_G_PARM argument.
361 */
362int v4l2_g_parm_cap(struct video_device *vdev,
363 struct v4l2_subdev *sd, struct v4l2_streamparm *a);
364
365/**
366 * v4l2_s_parm_cap - helper routine for vidioc_s_parm to fill this in by
367 * calling the s_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_S_PARM argument.
374 */
375int v4l2_s_parm_cap(struct video_device *vdev,
376 struct v4l2_subdev *sd, struct v4l2_streamparm *a);
377
85de5e06
AM
378/* Compare two v4l2_fract structs */
379#define V4L2_FRACT_COMPARE(a, OP, b) \
380 ((u64)(a).numerator * (b).denominator OP \
381 (u64)(b).numerator * (a).denominator)
382
f44b969a
EG
383/* ------------------------------------------------------------------------- */
384
385/* Pixel format and FourCC helpers */
386
387/**
388 * struct v4l2_format_info - information about a V4L2 format
389 * @format: 4CC format identifier (V4L2_PIX_FMT_*)
390 * @mem_planes: Number of memory planes, which includes the alpha plane (1 to 4).
391 * @comp_planes: Number of component planes, which includes the alpha plane (1 to 4).
392 * @bpp: Array of per-plane bytes per pixel
393 * @hdiv: Horizontal chroma subsampling factor
394 * @vdiv: Vertical chroma subsampling factor
395 * @block_w: Per-plane macroblock pixel width (optional)
396 * @block_h: Per-plane macroblock pixel height (optional)
397 */
398struct v4l2_format_info {
399 u32 format;
400 u8 mem_planes;
401 u8 comp_planes;
402 u8 bpp[4];
403 u8 hdiv;
404 u8 vdiv;
405 u8 block_w[4];
406 u8 block_h[4];
407};
408
409const struct v4l2_format_info *v4l2_format_info(u32 format);
410
411int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, int pixelformat,
412 int width, int height);
413int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, int pixelformat,
414 int width, int height);
415
b2f0648f 416#endif /* V4L2_COMMON_H_ */