• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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 
12  */
13 
14 #ifndef V4L2_COMMON_H_
15 #define V4L2_COMMON_H_
16 
17 #include <media/v4l2-dev.h>
18 
19 /* Common printk constructs for v4l-i2c drivers. These macros create a unique
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 
25 #define v4l_client_printk(level, client, fmt, arg...)			    \
26 	v4l_printk(level, (client)->dev.driver->name, (client)->adapter, \
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'. */
40 #define v4l_dbg(level, debug, client, fmt, arg...)			     \
41 	do {								     \
42 		if (debug >= (level))					     \
43 			v4l_client_printk(KERN_DEBUG, client, fmt , ## arg); \
44 	} while (0)
45 
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 
53 /* ------------------------------------------------------------------------- */
54 
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...)			\
71 	do {								\
72 		if (debug >= (level))					\
73 			v4l2_printk(KERN_DEBUG, dev, fmt , ## arg);	\
74 	} while (0)
75 
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  */
93 
94 int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
95 			 s32 min, s32 max, s32 step, s32 def);
96 
97 /* ------------------------------------------------------------------------- */
98 
99 struct v4l2_device;
100 struct v4l2_subdev;
101 struct v4l2_subdev_ops;
102 
103 /* I2C Helper functions */
104 #include <linux/i2c.h>
105 
106 /**
107  * enum v4l2_i2c_tuner_type - specifies the range of tuner address that
108  *	should be used when seeking for I2C devices.
109  *
110  * @ADDRS_RADIO:		Radio tuner addresses.
111  *				Represent the following I2C addresses:
112  *				0x10 (if compiled with tea5761 support)
113  *				and 0x60.
114  * @ADDRS_DEMOD:		Demod tuner addresses.
115  *				Represent the following I2C addresses:
116  *				0x42, 0x43, 0x4a and 0x4b.
117  * @ADDRS_TV:			TV tuner addresses.
118  *				Represent the following I2C addresses:
119  *				0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62,
120  *				0x63 and 0x64.
121  * @ADDRS_TV_WITH_DEMOD:	TV tuner addresses if demod is present, this
122  *				excludes addresses used by the demodulator
123  *				from the list of candidates.
124  *				Represent the following I2C addresses:
125  *				0x60, 0x61, 0x62, 0x63 and 0x64.
126  *
127  * NOTE: All I2C addresses above use the 7-bit notation.
128  */
129 enum v4l2_i2c_tuner_type {
130 	ADDRS_RADIO,
131 	ADDRS_DEMOD,
132 	ADDRS_TV,
133 	ADDRS_TV_WITH_DEMOD,
134 };
135 
136 #if defined(CONFIG_VIDEO_V4L2_I2C)
137 
138 /**
139  * v4l2_i2c_new_subdev - Load an i2c module and return an initialized
140  *	&struct v4l2_subdev.
141  *
142  * @v4l2_dev: pointer to &struct v4l2_device
143  * @adapter: pointer to struct i2c_adapter
144  * @client_type:  name of the chip that's on the adapter.
145  * @addr: I2C address. If zero, it will use @probe_addrs
146  * @probe_addrs: array with a list of address. The last entry at such
147  *	array should be %I2C_CLIENT_END.
148  *
149  * returns a &struct v4l2_subdev pointer.
150  */
151 struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
152 		struct i2c_adapter *adapter, const char *client_type,
153 		u8 addr, const unsigned short *probe_addrs);
154 
155 /**
156  * v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized
157  *	&struct v4l2_subdev.
158  *
159  * @v4l2_dev: pointer to &struct v4l2_device
160  * @adapter: pointer to struct i2c_adapter
161  * @info: pointer to struct i2c_board_info used to replace the irq,
162  *	 platform_data and addr arguments.
163  * @probe_addrs: array with a list of address. The last entry at such
164  *	array should be %I2C_CLIENT_END.
165  *
166  * returns a &struct v4l2_subdev pointer.
167  */
168 struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
169 		struct i2c_adapter *adapter, struct i2c_board_info *info,
170 		const unsigned short *probe_addrs);
171 
172 /**
173  * v4l2_i2c_subdev_set_name - Set name for an I²C sub-device
174  *
175  * @sd: pointer to &struct v4l2_subdev
176  * @client: pointer to struct i2c_client
177  * @devname: the name of the device; if NULL, the I²C device drivers's name
178  *           will be used
179  * @postfix: sub-device specific string to put right after the I²C device name;
180  *	     may be NULL
181  */
182 void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
183 			      const char *devname, const char *postfix);
184 
185 /**
186  * 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  */
193 void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
194 		const struct v4l2_subdev_ops *ops);
195 
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  */
203 unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd);
204 
205 /**
206  * 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  */
213 const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type);
214 
215 /**
216  * v4l2_i2c_subdev_unregister - Unregister a v4l2_subdev
217  *
218  * @sd: pointer to &struct v4l2_subdev
219  */
220 void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd);
221 
222 #else
223 
224 static inline struct v4l2_subdev *
v4l2_i2c_new_subdev(struct v4l2_device * v4l2_dev,struct i2c_adapter * adapter,const char * client_type,u8 addr,const unsigned short * probe_addrs)225 v4l2_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 
232 static inline struct v4l2_subdev *
v4l2_i2c_new_subdev_board(struct v4l2_device * v4l2_dev,struct i2c_adapter * adapter,struct i2c_board_info * info,const unsigned short * probe_addrs)233 v4l2_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 
240 static inline void
v4l2_i2c_subdev_set_name(struct v4l2_subdev * sd,struct i2c_client * client,const char * devname,const char * postfix)241 v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
242 			 const char *devname, const char *postfix)
243 {}
244 
245 static inline void
v4l2_i2c_subdev_init(struct v4l2_subdev * sd,struct i2c_client * client,const struct v4l2_subdev_ops * ops)246 v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
247 		     const struct v4l2_subdev_ops *ops)
248 {}
249 
v4l2_i2c_subdev_addr(struct v4l2_subdev * sd)250 static inline unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
251 {
252 	return I2C_CLIENT_END;
253 }
254 
255 static inline const unsigned short *
v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)256 v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
257 {
258 	return NULL;
259 }
260 
v4l2_i2c_subdev_unregister(struct v4l2_subdev * sd)261 static inline void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd)
262 {}
263 
264 #endif
265 
266 /* ------------------------------------------------------------------------- */
267 
268 /* SPI Helper functions */
269 
270 #include <linux/spi/spi.h>
271 
272 #if defined(CONFIG_SPI)
273 
274 /**
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  */
285 struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
286 		struct spi_master *master, struct spi_board_info *info);
287 
288 /**
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  */
296 void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
297 		const struct v4l2_subdev_ops *ops);
298 
299 /**
300  * v4l2_spi_subdev_unregister - Unregister a v4l2_subdev
301  *
302  * @sd: pointer to &struct v4l2_subdev
303  */
304 void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd);
305 
306 #else
307 
308 static inline struct v4l2_subdev *
v4l2_spi_new_subdev(struct v4l2_device * v4l2_dev,struct spi_master * master,struct spi_board_info * info)309 v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
310 		    struct spi_master *master, struct spi_board_info *info)
311 {
312 	return NULL;
313 }
314 
315 static inline void
v4l2_spi_subdev_init(struct v4l2_subdev * sd,struct spi_device * spi,const struct v4l2_subdev_ops * ops)316 v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
317 		     const struct v4l2_subdev_ops *ops)
318 {}
319 
v4l2_spi_subdev_unregister(struct v4l2_subdev * sd)320 static inline void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd)
321 {}
322 #endif
323 
324 /* ------------------------------------------------------------------------- */
325 
326 /*
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.
330  *
331  * It doesn't make much sense on documenting them, as what we really want is
332  * to get rid of them.
333  */
334 
335 /* s_config */
336 struct v4l2_priv_tun_config {
337 	int tuner;
338 	void *priv;
339 };
340 #define TUNER_SET_CONFIG           _IOW('d', 92, struct v4l2_priv_tun_config)
341 
342 #define VIDIOC_INT_RESET		_IOW ('d', 102, u32)
343 
344 /* ------------------------------------------------------------------------- */
345 
346 /* Miscellaneous helper functions */
347 
348 /**
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.
359  * @halign:	least significant bit on height.
360  * @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  */
385 void v4l_bound_align_image(unsigned int *width, unsigned int wmin,
386 			   unsigned int wmax, unsigned int walign,
387 			   unsigned int *height, unsigned int hmin,
388 			   unsigned int hmax, unsigned int halign,
389 			   unsigned int salign);
390 
391 /**
392  * 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
396  * @array_size: the length of the driver specific array of image sizes
397  * @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  */
409 #define v4l2_find_nearest_size(array, array_size, width_field, height_field, \
410 			       width, height)				\
411 	({								\
412 		BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \
413 			     sizeof((array)->height_field) != sizeof(u32)); \
414 		(typeof(&(array)[0]))__v4l2_find_nearest_size(		\
415 			(array), array_size, sizeof(*(array)),		\
416 			offsetof(typeof(*(array)), width_field),	\
417 			offsetof(typeof(*(array)), height_field),	\
418 			width, height);					\
419 	})
420 const 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 /**
426  * 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  */
435 int 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  */
448 int v4l2_s_parm_cap(struct video_device *vdev,
449 		    struct v4l2_subdev *sd, struct v4l2_streamparm *a);
450 
451 /* 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 
456 /* ------------------------------------------------------------------------- */
457 
458 /* Pixel format and FourCC helpers */
459 
460 /**
461  * struct v4l2_format_info - information about a V4L2 format
462  * @format: 4CC format identifier (V4L2_PIX_FMT_*)
463  * @mem_planes: Number of memory planes, which includes the alpha plane (1 to 4).
464  * @comp_planes: Number of component planes, which includes the alpha plane (1 to 4).
465  * @bpp: Array of per-plane bytes per pixel
466  * @hdiv: Horizontal chroma subsampling factor
467  * @vdiv: Vertical chroma subsampling factor
468  * @block_w: Per-plane macroblock pixel width (optional)
469  * @block_h: Per-plane macroblock pixel height (optional)
470  */
471 struct v4l2_format_info {
472 	u32 format;
473 	u8 mem_planes;
474 	u8 comp_planes;
475 	u8 bpp[4];
476 	u8 hdiv;
477 	u8 vdiv;
478 	u8 block_w[4];
479 	u8 block_h[4];
480 };
481 
482 const struct v4l2_format_info *v4l2_format_info(u32 format);
483 
484 void v4l2_apply_frmsize_constraints(u32 *width, u32 *height,
485 				    const struct v4l2_frmsize_stepwise *frmsize);
486 int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,
487 		     u32 width, u32 height);
488 int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
489 			u32 width, u32 height);
490 
491 #endif /* V4L2_COMMON_H_ */
492