1 /*
2  *	Video for Linux Two
3  *
4  *	A generic video device interface for the LINUX operating system
5  *	using a set of device structures/vectors for low level operations.
6  *
7  *	This file replaces the videodev.c file that comes with the
8  *	regular kernel distribution.
9  *
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.
14  *
15  * Author:	Bill Dirks <bill@thedirks.org>
16  *		based on code by Alan Cox, <alan@cymru.net>
17  *
18  */
19 
20 /*
21  * Video capture interface for Linux
22  *
23  *	A generic video device interface for the LINUX operating system
24  *	using a set of device structures/vectors for low level operations.
25  *
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.
30  *
31  * Author:	Alan Cox, <alan@lxorguk.ukuu.org.uk>
32  *
33  * Fixes:
34  */
35 
36 /*
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)
45  */
46 
47 #include <linux/module.h>
48 #include <linux/types.h>
49 #include <linux/kernel.h>
50 #include <linux/mm.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>
56 #endif
57 #include <linux/uaccess.h>
58 #include <asm/pgtable.h>
59 #include <asm/io.h>
60 #include <asm/div64.h>
61 #include <media/v4l2-common.h>
62 #include <media/v4l2-device.h>
63 #include <media/v4l2-ctrls.h>
64 
65 #include <linux/videodev2.h>
66 
67 MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
68 MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
69 MODULE_LICENSE("GPL");
70 
71 /*
72  *
73  *	V 4 L 2   D R I V E R   H E L P E R   A P I
74  *
75  */
76 
77 /*
78  *  Video Standard Operations (contributed by Michael Schimek)
79  */
80 
81 /* Helper functions for control handling			     */
82 
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)
85 {
86 	const char *name;
87 	s64 min = _min;
88 	s64 max = _max;
89 	u64 step = _step;
90 	s64 def = _def;
91 
92 	v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
93 		       &min, &max, &step, &def, &qctrl->flags);
94 
95 	if (name == NULL)
96 		return -EINVAL;
97 
98 	qctrl->minimum = min;
99 	qctrl->maximum = max;
100 	qctrl->step = step;
101 	qctrl->default_value = def;
102 	qctrl->reserved[0] = qctrl->reserved[1] = 0;
103 	strlcpy(qctrl->name, name, sizeof(qctrl->name));
104 	return 0;
105 }
106 EXPORT_SYMBOL(v4l2_ctrl_query_fill);
107 
108 /* I2C Helper functions */
109 
110 #if IS_ENABLED(CONFIG_I2C)
111 
112 void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
113 		const struct v4l2_subdev_ops *ops)
114 {
115 	v4l2_subdev_init(sd, ops);
116 	sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
117 	/* the owner is the same as the i2c_client's driver owner */
118 	sd->owner = client->dev.driver->owner;
119 	sd->dev = &client->dev;
120 	/* i2c_client and v4l2_subdev point to one another */
121 	v4l2_set_subdevdata(sd, client);
122 	i2c_set_clientdata(client, sd);
123 	/* initialize name */
124 	snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
125 		client->dev.driver->name, i2c_adapter_id(client->adapter),
126 		client->addr);
127 }
128 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
129 
130 /* Load an i2c sub-device. */
131 struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
132 		struct i2c_adapter *adapter, struct i2c_board_info *info,
133 		const unsigned short *probe_addrs)
134 {
135 	struct v4l2_subdev *sd = NULL;
136 	struct i2c_client *client;
137 
138 	BUG_ON(!v4l2_dev);
139 
140 	request_module(I2C_MODULE_PREFIX "%s", info->type);
141 
142 	/* Create the i2c client */
143 	if (info->addr == 0 && probe_addrs)
144 		client = i2c_new_probed_device(adapter, info, probe_addrs,
145 					       NULL);
146 	else
147 		client = i2c_new_device(adapter, info);
148 
149 	/* Note: by loading the module first we are certain that c->driver
150 	   will be set if the driver was found. If the module was not loaded
151 	   first, then the i2c core tries to delay-load the module for us,
152 	   and then c->driver is still NULL until the module is finally
153 	   loaded. This delay-load mechanism doesn't work if other drivers
154 	   want to use the i2c device, so explicitly loading the module
155 	   is the best alternative. */
156 	if (client == NULL || client->dev.driver == NULL)
157 		goto error;
158 
159 	/* Lock the module so we can safely get the v4l2_subdev pointer */
160 	if (!try_module_get(client->dev.driver->owner))
161 		goto error;
162 	sd = i2c_get_clientdata(client);
163 
164 	/* Register with the v4l2_device which increases the module's
165 	   use count as well. */
166 	if (v4l2_device_register_subdev(v4l2_dev, sd))
167 		sd = NULL;
168 	/* Decrease the module use count to match the first try_module_get. */
169 	module_put(client->dev.driver->owner);
170 
171 error:
172 	/* If we have a client but no subdev, then something went wrong and
173 	   we must unregister the client. */
174 	if (client && sd == NULL)
175 		i2c_unregister_device(client);
176 	return sd;
177 }
178 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
179 
180 struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
181 		struct i2c_adapter *adapter, const char *client_type,
182 		u8 addr, const unsigned short *probe_addrs)
183 {
184 	struct i2c_board_info info;
185 
186 	/* Setup the i2c board info with the device type and
187 	   the device address. */
188 	memset(&info, 0, sizeof(info));
189 	strlcpy(info.type, client_type, sizeof(info.type));
190 	info.addr = addr;
191 
192 	return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
193 }
194 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
195 
196 /* Return i2c client address of v4l2_subdev. */
197 unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
198 {
199 	struct i2c_client *client = v4l2_get_subdevdata(sd);
200 
201 	return client ? client->addr : I2C_CLIENT_END;
202 }
203 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
204 
205 /* Return a list of I2C tuner addresses to probe. Use only if the tuner
206    addresses are unknown. */
207 const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
208 {
209 	static const unsigned short radio_addrs[] = {
210 #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761)
211 		0x10,
212 #endif
213 		0x60,
214 		I2C_CLIENT_END
215 	};
216 	static const unsigned short demod_addrs[] = {
217 		0x42, 0x43, 0x4a, 0x4b,
218 		I2C_CLIENT_END
219 	};
220 	static const unsigned short tv_addrs[] = {
221 		0x42, 0x43, 0x4a, 0x4b,		/* tda8290 */
222 		0x60, 0x61, 0x62, 0x63, 0x64,
223 		I2C_CLIENT_END
224 	};
225 
226 	switch (type) {
227 	case ADDRS_RADIO:
228 		return radio_addrs;
229 	case ADDRS_DEMOD:
230 		return demod_addrs;
231 	case ADDRS_TV:
232 		return tv_addrs;
233 	case ADDRS_TV_WITH_DEMOD:
234 		return tv_addrs + 4;
235 	}
236 	return NULL;
237 }
238 EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
239 
240 #endif /* defined(CONFIG_I2C) */
241 
242 #if defined(CONFIG_SPI)
243 
244 /* Load an spi sub-device. */
245 
246 void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
247 		const struct v4l2_subdev_ops *ops)
248 {
249 	v4l2_subdev_init(sd, ops);
250 	sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
251 	/* the owner is the same as the spi_device's driver owner */
252 	sd->owner = spi->dev.driver->owner;
253 	sd->dev = &spi->dev;
254 	/* spi_device and v4l2_subdev point to one another */
255 	v4l2_set_subdevdata(sd, spi);
256 	spi_set_drvdata(spi, sd);
257 	/* initialize name */
258 	strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
259 }
260 EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
261 
262 struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
263 		struct spi_master *master, struct spi_board_info *info)
264 {
265 	struct v4l2_subdev *sd = NULL;
266 	struct spi_device *spi = NULL;
267 
268 	BUG_ON(!v4l2_dev);
269 
270 	if (info->modalias[0])
271 		request_module(info->modalias);
272 
273 	spi = spi_new_device(master, info);
274 
275 	if (spi == NULL || spi->dev.driver == NULL)
276 		goto error;
277 
278 	if (!try_module_get(spi->dev.driver->owner))
279 		goto error;
280 
281 	sd = spi_get_drvdata(spi);
282 
283 	/* Register with the v4l2_device which increases the module's
284 	   use count as well. */
285 	if (v4l2_device_register_subdev(v4l2_dev, sd))
286 		sd = NULL;
287 
288 	/* Decrease the module use count to match the first try_module_get. */
289 	module_put(spi->dev.driver->owner);
290 
291 error:
292 	/* If we have a client but no subdev, then something went wrong and
293 	   we must unregister the client. */
294 	if (!sd)
295 		spi_unregister_device(spi);
296 
297 	return sd;
298 }
299 EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
300 
301 #endif /* defined(CONFIG_SPI) */
302 
303 /* Clamp x to be between min and max, aligned to a multiple of 2^align.  min
304  * and max don't have to be aligned, but there must be at least one valid
305  * value.  E.g., min=17,max=31,align=4 is not allowed as there are no multiples
306  * of 16 between 17 and 31.  */
307 static unsigned int clamp_align(unsigned int x, unsigned int min,
308 				unsigned int max, unsigned int align)
309 {
310 	/* Bits that must be zero to be aligned */
311 	unsigned int mask = ~((1 << align) - 1);
312 
313 	/* Clamp to aligned min and max */
314 	x = clamp(x, (min + ~mask) & mask, max & mask);
315 
316 	/* Round to nearest aligned value */
317 	if (align)
318 		x = (x + (1 << (align - 1))) & mask;
319 
320 	return x;
321 }
322 
323 void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
324 			   unsigned int walign,
325 			   u32 *h, unsigned int hmin, unsigned int hmax,
326 			   unsigned int halign, unsigned int salign)
327 {
328 	*w = clamp_align(*w, wmin, wmax, walign);
329 	*h = clamp_align(*h, hmin, hmax, halign);
330 
331 	/* Usually we don't need to align the size and are done now. */
332 	if (!salign)
333 		return;
334 
335 	/* How much alignment do we have? */
336 	walign = __ffs(*w);
337 	halign = __ffs(*h);
338 	/* Enough to satisfy the image alignment? */
339 	if (walign + halign < salign) {
340 		/* Max walign where there is still a valid width */
341 		unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
342 		/* Max halign where there is still a valid height */
343 		unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
344 
345 		/* up the smaller alignment until we have enough */
346 		do {
347 			if (halign >= hmaxa ||
348 			    (walign <= halign && walign < wmaxa)) {
349 				*w = clamp_align(*w, wmin, wmax, walign + 1);
350 				walign = __ffs(*w);
351 			} else {
352 				*h = clamp_align(*h, hmin, hmax, halign + 1);
353 				halign = __ffs(*h);
354 			}
355 		} while (halign + walign < salign);
356 	}
357 }
358 EXPORT_SYMBOL_GPL(v4l_bound_align_image);
359 
360 const struct v4l2_frmsize_discrete *
361 v4l2_find_nearest_format(const struct v4l2_frmsize_discrete *sizes,
362 			  size_t num_sizes,
363 			  s32 width, s32 height)
364 {
365 	int i;
366 	u32 error, min_error = UINT_MAX;
367 	const struct v4l2_frmsize_discrete *size, *best = NULL;
368 
369 	if (!sizes)
370 		return NULL;
371 
372 	for (i = 0, size = sizes; i < num_sizes; i++, size++) {
373 		error = abs(size->width - width) + abs(size->height - height);
374 		if (error < min_error) {
375 			min_error = error;
376 			best = size;
377 		}
378 		if (!error)
379 			break;
380 	}
381 
382 	return best;
383 }
384 EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
385 
386 void v4l2_get_timestamp(struct timeval *tv)
387 {
388 	struct timespec ts;
389 
390 	ktime_get_ts(&ts);
391 	tv->tv_sec = ts.tv_sec;
392 	tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
393 }
394 EXPORT_SYMBOL_GPL(v4l2_get_timestamp);
395