blob: cdc87ec61e54c85614576d99be71641e6c55f969 [file] [log] [blame]
Hans Verkuilb2f06482005-11-13 16:07:55 -08001/*
2 v4l2 common internal API header
3
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
9 Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26#ifndef V4L2_COMMON_H_
27#define V4L2_COMMON_H_
28
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030029#include <media/v4l2-dev.h>
30
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -040031/* Common printk constructs for v4l-i2c drivers. These macros create a unique
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020032 prefix consisting of the driver name, the adapter number and the i2c
33 address. */
34#define v4l_printk(level, name, adapter, addr, fmt, arg...) \
35 printk(level "%s %d-%04x: " fmt, name, i2c_adapter_id(adapter), addr , ## arg)
36
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -020037#define v4l_client_printk(level, client, fmt, arg...) \
Lars-Peter Clausenf9d32f22013-09-29 10:51:01 +020038 v4l_printk(level, (client)->dev.driver->name, (client)->adapter, \
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020039 (client)->addr, fmt , ## arg)
40
41#define v4l_err(client, fmt, arg...) \
42 v4l_client_printk(KERN_ERR, client, fmt , ## arg)
43
44#define v4l_warn(client, fmt, arg...) \
45 v4l_client_printk(KERN_WARNING, client, fmt , ## arg)
46
47#define v4l_info(client, fmt, arg...) \
48 v4l_client_printk(KERN_INFO, client, fmt , ## arg)
49
50/* These three macros assume that the debug level is set with a module
51 parameter called 'debug'. */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -020052#define v4l_dbg(level, debug, client, fmt, arg...) \
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -050053 do { \
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020054 if (debug >= (level)) \
55 v4l_client_printk(KERN_DEBUG, client, fmt , ## arg); \
56 } while (0)
57
Mauro Carvalho Chehaba41231d2016-11-16 08:35:08 -020058/* Add a version of v4l_dbg to be used on drivers using dev_foo() macros */
59#define dev_dbg_lvl(__dev, __level, __debug, __fmt, __arg...) \
60 do { \
61 if (__debug >= (__level)) \
62 dev_printk(KERN_DEBUG, __dev, __fmt, ##__arg); \
63 } while (0)
64
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020065/* ------------------------------------------------------------------------- */
66
Hans Verkuildd991202008-11-23 12:19:45 -030067/* These printk constructs can be used with v4l2_device and v4l2_subdev */
68#define v4l2_printk(level, dev, fmt, arg...) \
69 printk(level "%s: " fmt, (dev)->name , ## arg)
70
71#define v4l2_err(dev, fmt, arg...) \
72 v4l2_printk(KERN_ERR, dev, fmt , ## arg)
73
74#define v4l2_warn(dev, fmt, arg...) \
75 v4l2_printk(KERN_WARNING, dev, fmt , ## arg)
76
77#define v4l2_info(dev, fmt, arg...) \
78 v4l2_printk(KERN_INFO, dev, fmt , ## arg)
79
80/* These three macros assume that the debug level is set with a module
81 parameter called 'debug'. */
82#define v4l2_dbg(level, debug, dev, fmt, arg...) \
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -050083 do { \
Hans Verkuildd991202008-11-23 12:19:45 -030084 if (debug >= (level)) \
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -050085 v4l2_printk(KERN_DEBUG, dev, fmt , ## arg); \
Hans Verkuildd991202008-11-23 12:19:45 -030086 } while (0)
87
Mauro Carvalho Chehabb8719152016-07-21 12:26:04 -030088/**
89 * v4l2_ctrl_query_fill- Fill in a struct v4l2_queryctrl
90 *
91 * @qctrl: pointer to the &struct v4l2_queryctrl to be filled
92 * @min: minimum value for the control
93 * @max: maximum value for the control
94 * @step: control step
95 * @def: default value for the control
96 *
97 * Fills the &struct v4l2_queryctrl fields for the query control.
98 *
99 * .. note::
100 *
101 * This function assumes that the @qctrl->id field is filled.
102 *
103 * Returns -EINVAL if the control is not known by the V4L2 core, 0 on success.
104 */
Hans Verkuildd991202008-11-23 12:19:45 -0300105
Mauro Carvalho Chehabb8719152016-07-21 12:26:04 -0300106int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
107 s32 min, s32 max, s32 step, s32 def);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300108
109/* ------------------------------------------------------------------------- */
110
Hans Verkuil78a3b4d2009-04-01 03:41:09 -0300111/* I2C Helper functions */
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300112
113struct i2c_driver;
114struct i2c_adapter;
115struct i2c_client;
Jean Delvared2653e92008-04-29 23:11:39 +0200116struct i2c_device_id;
Hans Verkuildd991202008-11-23 12:19:45 -0300117struct v4l2_device;
118struct v4l2_subdev;
119struct v4l2_subdev_ops;
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300120
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300121/**
122 * v4l2_i2c_new_subdev - Load an i2c module and return an initialized
123 * &struct v4l2_subdev.
124 *
125 * @v4l2_dev: pointer to &struct v4l2_device
126 * @adapter: pointer to struct i2c_adapter
127 * @client_type: name of the chip that's on the adapter.
128 * @addr: I2C address. If zero, it will use @probe_addrs
129 * @probe_addrs: array with a list of address. The last entry at such
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500130 * array should be %I2C_CLIENT_END.
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300131 *
132 * returns a &struct v4l2_subdev pointer.
133 */
Hans Verkuil3c7c9372011-01-08 07:08:02 -0300134struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300135 struct i2c_adapter *adapter, const char *client_type,
Hans Verkuilf0222c72009-06-09 17:12:33 -0300136 u8 addr, const unsigned short *probe_addrs);
137
138struct i2c_board_info;
139
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300140/**
141 * v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized
142 * &struct v4l2_subdev.
143 *
144 * @v4l2_dev: pointer to &struct v4l2_device
145 * @adapter: pointer to struct i2c_adapter
146 * @info: pointer to struct i2c_board_info used to replace the irq,
147 * platform_data and addr arguments.
148 * @probe_addrs: array with a list of address. The last entry at such
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500149 * array should be %I2C_CLIENT_END.
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300150 *
151 * returns a &struct v4l2_subdev pointer.
152 */
Hans Verkuilf0222c72009-06-09 17:12:33 -0300153struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300154 struct i2c_adapter *adapter, struct i2c_board_info *info,
155 const unsigned short *probe_addrs);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300156
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300157/**
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 */
Hans Verkuildd991202008-11-23 12:19:45 -0300165void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
166 const struct v4l2_subdev_ops *ops);
Mauro Carvalho Chehaba39c57f2016-07-21 10:08:31 -0300167
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 */
Hans Verkuilab3731902009-02-21 18:08:41 -0300175unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd);
Hans Verkuildd991202008-11-23 12:19:45 -0300176
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400177/**
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 */
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300200enum v4l2_i2c_tuner_type {
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400201 ADDRS_RADIO,
202 ADDRS_DEMOD,
203 ADDRS_TV,
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300204 ADDRS_TV_WITH_DEMOD,
205};
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400206/**
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 */
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300214const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type);
215
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300216/* ------------------------------------------------------------------------- */
217
Dmitri Belimov85e09212010-03-23 11:23:29 -0300218/* SPI Helper functions */
219#if defined(CONFIG_SPI)
220
221#include <linux/spi/spi.h>
222
223struct spi_device;
224
Mauro Carvalho Chehabb8719152016-07-21 12:26:04 -0300225/**
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 */
Dmitri Belimov85e09212010-03-23 11:23:29 -0300236struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
237 struct spi_master *master, struct spi_board_info *info);
238
Mauro Carvalho Chehabb8719152016-07-21 12:26:04 -0300239/**
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 */
Dmitri Belimov85e09212010-03-23 11:23:29 -0300247void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
248 const struct v4l2_subdev_ops *ops);
249#endif
250
251/* ------------------------------------------------------------------------- */
252
Mauro Carvalho Chehab01154ef2017-09-21 10:04:30 -0400253/*
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.
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400257 *
258 * It doesn't make much sense on documenting them, as what we really want is
259 * to get rid of them.
Mauro Carvalho Chehab01154ef2017-09-21 10:04:30 -0400260 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200261
Hans Verkuil78a3b4d2009-04-01 03:41:09 -0300262/* s_config */
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300263struct v4l2_priv_tun_config {
264 int tuner;
265 void *priv;
266};
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300267#define TUNER_SET_CONFIG _IOW('d', 92, struct v4l2_priv_tun_config)
Hans Verkuil757d2502006-01-23 17:11:10 -0200268
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500269#define VIDIOC_INT_RESET _IOW ('d', 102, u32)
Hans Verkuil6c31e592009-04-02 11:37:41 -0300270
Trent Piephob0d31592009-05-30 21:45:46 -0300271/* ------------------------------------------------------------------------- */
272
273/* Miscellaneous helper functions */
274
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400275/**
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.
286 * @halign: least significant bit on width.
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,
Trent Piephob0d31592009-05-30 21:45:46 -0300313 unsigned int wmax, unsigned int walign,
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400314 unsigned int *height, unsigned int hmin,
Trent Piephob0d31592009-05-30 21:45:46 -0300315 unsigned int hmax, unsigned int halign,
316 unsigned int salign);
Hans Verkuil3fd8e642010-09-30 09:29:37 -0300317
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400318/**
Sakari Ailus95ce9c22018-02-23 04:50:14 -0500319 * 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
Sakari Ailusd2dc57b2018-03-21 16:29:27 -0400323 * @array_size: the length of the driver specific array of image sizes
Sakari Ailus95ce9c22018-02-23 04:50:14 -0500324 * @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 */
Sakari Ailusd2dc57b2018-03-21 16:29:27 -0400336#define v4l2_find_nearest_size(array, array_size, width_field, height_field, \
Sakari Ailus95ce9c22018-02-23 04:50:14 -0500337 width, height) \
338 ({ \
339 BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \
340 sizeof((array)->height_field) != sizeof(u32)); \
Sakari Ailus2bbc46e2018-06-21 05:41:21 -0400341 (typeof(&(array)[0]))__v4l2_find_nearest_size( \
Sakari Ailusd2dc57b2018-03-21 16:29:27 -0400342 (array), array_size, sizeof(*(array)), \
Sakari Ailus95ce9c22018-02-23 04:50:14 -0500343 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
352/**
Mauro Carvalho Chehab76a59fe2017-09-22 09:03:54 -0400353 * v4l2_get_timestamp - helper routine to get a timestamp to be used when
354 * filling streaming metadata. Internally, it uses ktime_get_ts(),
355 * which is the recommended way to get it.
356 *
357 * @tv: pointer to &struct timeval to be filled.
358 */
Sakari Ailusabd23292012-09-15 07:51:47 -0300359void v4l2_get_timestamp(struct timeval *tv);
360
Hans Verkuil672de9a2018-01-22 03:58:36 -0500361/**
362 * v4l2_g_parm_cap - helper routine for vidioc_g_parm to fill this in by
363 * calling the g_frame_interval op of the given subdev. It only works
364 * for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
365 * function name.
366 *
367 * @vdev: the struct video_device pointer. Used to determine the device caps.
368 * @sd: the sub-device pointer.
369 * @a: the VIDIOC_G_PARM argument.
370 */
371int v4l2_g_parm_cap(struct video_device *vdev,
372 struct v4l2_subdev *sd, struct v4l2_streamparm *a);
373
374/**
375 * v4l2_s_parm_cap - helper routine for vidioc_s_parm to fill this in by
376 * calling the s_frame_interval op of the given subdev. It only works
377 * for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
378 * function name.
379 *
380 * @vdev: the struct video_device pointer. Used to determine the device caps.
381 * @sd: the sub-device pointer.
382 * @a: the VIDIOC_S_PARM argument.
383 */
384int v4l2_s_parm_cap(struct video_device *vdev,
385 struct v4l2_subdev *sd, struct v4l2_streamparm *a);
386
Hans Verkuilb2f06482005-11-13 16:07:55 -0800387#endif /* V4L2_COMMON_H_ */