4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
7 * This file replaces the videodev.c file that comes with the
8 * regular kernel distribution.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * Author: Bill Dirks <bill@thedirks.org>
16 * based on code by Alan Cox, <alan@cymru.net>
21 * Video capture interface for Linux
23 * A generic video device interface for the LINUX operating system
24 * using a set of device structures/vectors for low level operations.
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version
29 * 2 of the License, or (at your option) any later version.
31 * Author: Alan Cox, <alan@lxorguk.ukuu.org.uk>
37 * Video4linux 1/2 integration by Justin Schoeman
38 * <justin@suntiger.ee.up.ac.za>
39 * 2.4 PROCFS support ported from 2.4 kernels by
40 * Iñaki García Etxebarria <garetxe@euskalnet.net>
41 * Makefile fix by "W. Michael Petullo" <mike@flyn.org>
42 * 2.4 devfs support ported from 2.4 kernels by
43 * Dan Merillat <dan@merillat.org>
44 * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
47 #include <linux/module.h>
48 #include <linux/types.h>
49 #include <linux/kernel.h>
51 #include <linux/string.h>
52 #include <linux/errno.h>
53 #include <linux/i2c.h>
54 #if defined(CONFIG_SPI)
55 #include <linux/spi/spi.h>
57 #include <linux/uaccess.h>
58 #include <asm/pgtable.h>
60 #include <asm/div64.h>
61 #include <media/v4l2-common.h>
62 #include <media/v4l2-device.h>
63 #include <media/v4l2-ctrls.h>
65 #include <linux/videodev2.h>
67 MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
68 MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
69 MODULE_LICENSE("GPL");
73 * V 4 L 2 D R I V E R H E L P E R A P I
78 * Video Standard Operations (contributed by Michael Schimek)
81 /* Helper functions for control handling */
83 /* Fill in a struct v4l2_queryctrl */
84 int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 _min, s32 _max, s32 _step, s32 _def)
92 v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
93 &min, &max, &step, &def, &qctrl->flags);
101 qctrl->default_value = def;
102 qctrl->reserved[0] = qctrl->reserved[1] = 0;
103 strscpy(qctrl->name, name, sizeof(qctrl->name));
106 EXPORT_SYMBOL(v4l2_ctrl_query_fill);
108 /* I2C Helper functions */
110 #if IS_ENABLED(CONFIG_I2C)
112 void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
113 const char *devname, const char *postfix)
116 devname = client->dev.driver->name;
120 snprintf(sd->name, sizeof(sd->name), "%s%s %d-%04x", devname, postfix,
121 i2c_adapter_id(client->adapter), client->addr);
123 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_set_name);
125 void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
126 const struct v4l2_subdev_ops *ops)
128 v4l2_subdev_init(sd, ops);
129 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
130 /* the owner is the same as the i2c_client's driver owner */
131 sd->owner = client->dev.driver->owner;
132 sd->dev = &client->dev;
133 /* i2c_client and v4l2_subdev point to one another */
134 v4l2_set_subdevdata(sd, client);
135 i2c_set_clientdata(client, sd);
136 v4l2_i2c_subdev_set_name(sd, client, NULL, NULL);
138 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
140 /* Load an i2c sub-device. */
141 struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
142 struct i2c_adapter *adapter, struct i2c_board_info *info,
143 const unsigned short *probe_addrs)
145 struct v4l2_subdev *sd = NULL;
146 struct i2c_client *client;
150 request_module(I2C_MODULE_PREFIX "%s", info->type);
152 /* Create the i2c client */
153 if (info->addr == 0 && probe_addrs)
154 client = i2c_new_probed_device(adapter, info, probe_addrs,
157 client = i2c_new_device(adapter, info);
159 /* Note: by loading the module first we are certain that c->driver
160 will be set if the driver was found. If the module was not loaded
161 first, then the i2c core tries to delay-load the module for us,
162 and then c->driver is still NULL until the module is finally
163 loaded. This delay-load mechanism doesn't work if other drivers
164 want to use the i2c device, so explicitly loading the module
165 is the best alternative. */
166 if (client == NULL || client->dev.driver == NULL)
169 /* Lock the module so we can safely get the v4l2_subdev pointer */
170 if (!try_module_get(client->dev.driver->owner))
172 sd = i2c_get_clientdata(client);
174 /* Register with the v4l2_device which increases the module's
175 use count as well. */
176 if (v4l2_device_register_subdev(v4l2_dev, sd))
178 /* Decrease the module use count to match the first try_module_get. */
179 module_put(client->dev.driver->owner);
182 /* If we have a client but no subdev, then something went wrong and
183 we must unregister the client. */
184 if (client && sd == NULL)
185 i2c_unregister_device(client);
188 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
190 struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
191 struct i2c_adapter *adapter, const char *client_type,
192 u8 addr, const unsigned short *probe_addrs)
194 struct i2c_board_info info;
196 /* Setup the i2c board info with the device type and
197 the device address. */
198 memset(&info, 0, sizeof(info));
199 strscpy(info.type, client_type, sizeof(info.type));
202 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
204 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
206 /* Return i2c client address of v4l2_subdev. */
207 unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
209 struct i2c_client *client = v4l2_get_subdevdata(sd);
211 return client ? client->addr : I2C_CLIENT_END;
213 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
215 /* Return a list of I2C tuner addresses to probe. Use only if the tuner
216 addresses are unknown. */
217 const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
219 static const unsigned short radio_addrs[] = {
220 #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761)
226 static const unsigned short demod_addrs[] = {
227 0x42, 0x43, 0x4a, 0x4b,
230 static const unsigned short tv_addrs[] = {
231 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
232 0x60, 0x61, 0x62, 0x63, 0x64,
243 case ADDRS_TV_WITH_DEMOD:
248 EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
250 #endif /* defined(CONFIG_I2C) */
252 #if defined(CONFIG_SPI)
254 /* Load an spi sub-device. */
256 void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
257 const struct v4l2_subdev_ops *ops)
259 v4l2_subdev_init(sd, ops);
260 sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
261 /* the owner is the same as the spi_device's driver owner */
262 sd->owner = spi->dev.driver->owner;
264 /* spi_device and v4l2_subdev point to one another */
265 v4l2_set_subdevdata(sd, spi);
266 spi_set_drvdata(spi, sd);
267 /* initialize name */
268 snprintf(sd->name, sizeof(sd->name), "%s %s",
269 spi->dev.driver->name, dev_name(&spi->dev));
271 EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
273 struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
274 struct spi_master *master, struct spi_board_info *info)
276 struct v4l2_subdev *sd = NULL;
277 struct spi_device *spi = NULL;
281 if (info->modalias[0])
282 request_module(info->modalias);
284 spi = spi_new_device(master, info);
286 if (spi == NULL || spi->dev.driver == NULL)
289 if (!try_module_get(spi->dev.driver->owner))
292 sd = spi_get_drvdata(spi);
294 /* Register with the v4l2_device which increases the module's
295 use count as well. */
296 if (v4l2_device_register_subdev(v4l2_dev, sd))
299 /* Decrease the module use count to match the first try_module_get. */
300 module_put(spi->dev.driver->owner);
303 /* If we have a client but no subdev, then something went wrong and
304 we must unregister the client. */
306 spi_unregister_device(spi);
310 EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
312 #endif /* defined(CONFIG_SPI) */
314 /* Clamp x to be between min and max, aligned to a multiple of 2^align. min
315 * and max don't have to be aligned, but there must be at least one valid
316 * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
317 * of 16 between 17 and 31. */
318 static unsigned int clamp_align(unsigned int x, unsigned int min,
319 unsigned int max, unsigned int align)
321 /* Bits that must be zero to be aligned */
322 unsigned int mask = ~((1 << align) - 1);
324 /* Clamp to aligned min and max */
325 x = clamp(x, (min + ~mask) & mask, max & mask);
327 /* Round to nearest aligned value */
329 x = (x + (1 << (align - 1))) & mask;
334 void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
336 u32 *h, unsigned int hmin, unsigned int hmax,
337 unsigned int halign, unsigned int salign)
339 *w = clamp_align(*w, wmin, wmax, walign);
340 *h = clamp_align(*h, hmin, hmax, halign);
342 /* Usually we don't need to align the size and are done now. */
346 /* How much alignment do we have? */
349 /* Enough to satisfy the image alignment? */
350 if (walign + halign < salign) {
351 /* Max walign where there is still a valid width */
352 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
353 /* Max halign where there is still a valid height */
354 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
356 /* up the smaller alignment until we have enough */
358 if (halign >= hmaxa ||
359 (walign <= halign && walign < wmaxa)) {
360 *w = clamp_align(*w, wmin, wmax, walign + 1);
363 *h = clamp_align(*h, hmin, hmax, halign + 1);
366 } while (halign + walign < salign);
369 EXPORT_SYMBOL_GPL(v4l_bound_align_image);
372 __v4l2_find_nearest_size(const void *array, size_t array_size,
373 size_t entry_size, size_t width_offset,
374 size_t height_offset, s32 width, s32 height)
376 u32 error, min_error = U32_MAX;
377 const void *best = NULL;
383 for (i = 0; i < array_size; i++, array += entry_size) {
384 const u32 *entry_width = array + width_offset;
385 const u32 *entry_height = array + height_offset;
387 error = abs(*entry_width - width) + abs(*entry_height - height);
388 if (error > min_error)
399 EXPORT_SYMBOL_GPL(__v4l2_find_nearest_size);
401 void v4l2_get_timestamp(struct timeval *tv)
406 tv->tv_sec = ts.tv_sec;
407 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
409 EXPORT_SYMBOL_GPL(v4l2_get_timestamp);
411 int v4l2_g_parm_cap(struct video_device *vdev,
412 struct v4l2_subdev *sd, struct v4l2_streamparm *a)
414 struct v4l2_subdev_frame_interval ival = { 0 };
417 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
418 a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
421 if (vdev->device_caps & V4L2_CAP_READWRITE)
422 a->parm.capture.readbuffers = 2;
423 if (v4l2_subdev_has_op(sd, video, g_frame_interval))
424 a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
425 ret = v4l2_subdev_call(sd, video, g_frame_interval, &ival);
427 a->parm.capture.timeperframe = ival.interval;
430 EXPORT_SYMBOL_GPL(v4l2_g_parm_cap);
432 int v4l2_s_parm_cap(struct video_device *vdev,
433 struct v4l2_subdev *sd, struct v4l2_streamparm *a)
435 struct v4l2_subdev_frame_interval ival = {
436 .interval = a->parm.capture.timeperframe
440 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
441 a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
444 memset(&a->parm, 0, sizeof(a->parm));
445 if (vdev->device_caps & V4L2_CAP_READWRITE)
446 a->parm.capture.readbuffers = 2;
448 a->parm.capture.readbuffers = 0;
450 if (v4l2_subdev_has_op(sd, video, g_frame_interval))
451 a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
452 ret = v4l2_subdev_call(sd, video, s_frame_interval, &ival);
454 a->parm.capture.timeperframe = ival.interval;
457 EXPORT_SYMBOL_GPL(v4l2_s_parm_cap);