v4l2-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * Video capture interface for Linux
  21. *
  22. * A generic video device interface for the LINUX operating system
  23. * using a set of device structures/vectors for low level operations.
  24. *
  25. * This program is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU General Public License
  27. * as published by the Free Software Foundation; either version
  28. * 2 of the License, or (at your option) any later version.
  29. *
  30. * Author: Alan Cox, <alan@lxorguk.ukuu.org.uk>
  31. *
  32. * Fixes:
  33. */
  34. /*
  35. * Video4linux 1/2 integration by Justin Schoeman
  36. * <justin@suntiger.ee.up.ac.za>
  37. * 2.4 PROCFS support ported from 2.4 kernels by
  38. * Iñaki García Etxebarria <garetxe@euskalnet.net>
  39. * Makefile fix by "W. Michael Petullo" <mike@flyn.org>
  40. * 2.4 devfs support ported from 2.4 kernels by
  41. * Dan Merillat <dan@merillat.org>
  42. * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
  43. */
  44. #include <linux/module.h>
  45. #include <linux/types.h>
  46. #include <linux/kernel.h>
  47. #include <linux/mm.h>
  48. #include <linux/string.h>
  49. #include <linux/errno.h>
  50. #include <linux/i2c.h>
  51. #if defined(CONFIG_SPI)
  52. #include <linux/spi/spi.h>
  53. #endif
  54. #include <asm/uaccess.h>
  55. #include <asm/pgtable.h>
  56. #include <asm/io.h>
  57. #include <asm/div64.h>
  58. #include <media/v4l2-common.h>
  59. #include <media/v4l2-device.h>
  60. #include <media/v4l2-ctrls.h>
  61. #include <linux/videodev2.h>
  62. MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
  63. MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
  64. MODULE_LICENSE("GPL");
  65. /*
  66. *
  67. * V 4 L 2 D R I V E R H E L P E R A P I
  68. *
  69. */
  70. /*
  71. * Video Standard Operations (contributed by Michael Schimek)
  72. */
  73. /* Helper functions for control handling */
  74. /* Fill in a struct v4l2_queryctrl */
  75. int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 _min, s32 _max, s32 _step, s32 _def)
  76. {
  77. const char *name;
  78. s64 min = _min;
  79. s64 max = _max;
  80. u64 step = _step;
  81. s64 def = _def;
  82. v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
  83. &min, &max, &step, &def, &qctrl->flags);
  84. if (name == NULL)
  85. return -EINVAL;
  86. qctrl->minimum = min;
  87. qctrl->maximum = max;
  88. qctrl->step = step;
  89. qctrl->default_value = def;
  90. qctrl->reserved[0] = qctrl->reserved[1] = 0;
  91. strlcpy(qctrl->name, name, sizeof(qctrl->name));
  92. return 0;
  93. }
  94. EXPORT_SYMBOL(v4l2_ctrl_query_fill);
  95. /* I2C Helper functions */
  96. #if IS_ENABLED(CONFIG_I2C)
  97. void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
  98. const struct v4l2_subdev_ops *ops)
  99. {
  100. v4l2_subdev_init(sd, ops);
  101. sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
  102. /* the owner is the same as the i2c_client's driver owner */
  103. sd->owner = client->dev.driver->owner;
  104. sd->dev = &client->dev;
  105. /* i2c_client and v4l2_subdev point to one another */
  106. v4l2_set_subdevdata(sd, client);
  107. i2c_set_clientdata(client, sd);
  108. /* initialize name */
  109. snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
  110. client->dev.driver->name, i2c_adapter_id(client->adapter),
  111. client->addr);
  112. }
  113. EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
  114. /* Load an i2c sub-device. */
  115. struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
  116. struct i2c_adapter *adapter, struct i2c_board_info *info,
  117. const unsigned short *probe_addrs)
  118. {
  119. struct v4l2_subdev *sd = NULL;
  120. struct i2c_client *client;
  121. BUG_ON(!v4l2_dev);
  122. request_module(I2C_MODULE_PREFIX "%s", info->type);
  123. /* Create the i2c client */
  124. if (info->addr == 0 && probe_addrs)
  125. client = i2c_new_probed_device(adapter, info, probe_addrs,
  126. NULL);
  127. else
  128. client = i2c_new_device(adapter, info);
  129. /* Note: by loading the module first we are certain that c->driver
  130. will be set if the driver was found. If the module was not loaded
  131. first, then the i2c core tries to delay-load the module for us,
  132. and then c->driver is still NULL until the module is finally
  133. loaded. This delay-load mechanism doesn't work if other drivers
  134. want to use the i2c device, so explicitly loading the module
  135. is the best alternative. */
  136. if (client == NULL || client->dev.driver == NULL)
  137. goto error;
  138. /* Lock the module so we can safely get the v4l2_subdev pointer */
  139. if (!try_module_get(client->dev.driver->owner))
  140. goto error;
  141. sd = i2c_get_clientdata(client);
  142. /* Register with the v4l2_device which increases the module's
  143. use count as well. */
  144. if (v4l2_device_register_subdev(v4l2_dev, sd))
  145. sd = NULL;
  146. /* Decrease the module use count to match the first try_module_get. */
  147. module_put(client->dev.driver->owner);
  148. error:
  149. /* If we have a client but no subdev, then something went wrong and
  150. we must unregister the client. */
  151. if (client && sd == NULL)
  152. i2c_unregister_device(client);
  153. return sd;
  154. }
  155. EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
  156. struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
  157. struct i2c_adapter *adapter, const char *client_type,
  158. u8 addr, const unsigned short *probe_addrs)
  159. {
  160. struct i2c_board_info info;
  161. /* Setup the i2c board info with the device type and
  162. the device address. */
  163. memset(&info, 0, sizeof(info));
  164. strlcpy(info.type, client_type, sizeof(info.type));
  165. info.addr = addr;
  166. return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
  167. }
  168. EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
  169. /* Return i2c client address of v4l2_subdev. */
  170. unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
  171. {
  172. struct i2c_client *client = v4l2_get_subdevdata(sd);
  173. return client ? client->addr : I2C_CLIENT_END;
  174. }
  175. EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
  176. /* Return a list of I2C tuner addresses to probe. Use only if the tuner
  177. addresses are unknown. */
  178. const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
  179. {
  180. static const unsigned short radio_addrs[] = {
  181. #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761)
  182. 0x10,
  183. #endif
  184. 0x60,
  185. I2C_CLIENT_END
  186. };
  187. static const unsigned short demod_addrs[] = {
  188. 0x42, 0x43, 0x4a, 0x4b,
  189. I2C_CLIENT_END
  190. };
  191. static const unsigned short tv_addrs[] = {
  192. 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
  193. 0x60, 0x61, 0x62, 0x63, 0x64,
  194. I2C_CLIENT_END
  195. };
  196. switch (type) {
  197. case ADDRS_RADIO:
  198. return radio_addrs;
  199. case ADDRS_DEMOD:
  200. return demod_addrs;
  201. case ADDRS_TV:
  202. return tv_addrs;
  203. case ADDRS_TV_WITH_DEMOD:
  204. return tv_addrs + 4;
  205. }
  206. return NULL;
  207. }
  208. EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
  209. #endif /* defined(CONFIG_I2C) */
  210. #if defined(CONFIG_SPI)
  211. /* Load an spi sub-device. */
  212. void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
  213. const struct v4l2_subdev_ops *ops)
  214. {
  215. v4l2_subdev_init(sd, ops);
  216. sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
  217. /* the owner is the same as the spi_device's driver owner */
  218. sd->owner = spi->dev.driver->owner;
  219. sd->dev = &spi->dev;
  220. /* spi_device and v4l2_subdev point to one another */
  221. v4l2_set_subdevdata(sd, spi);
  222. spi_set_drvdata(spi, sd);
  223. /* initialize name */
  224. strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
  225. }
  226. EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
  227. struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
  228. struct spi_master *master, struct spi_board_info *info)
  229. {
  230. struct v4l2_subdev *sd = NULL;
  231. struct spi_device *spi = NULL;
  232. BUG_ON(!v4l2_dev);
  233. if (info->modalias[0])
  234. request_module(info->modalias);
  235. spi = spi_new_device(master, info);
  236. if (spi == NULL || spi->dev.driver == NULL)
  237. goto error;
  238. if (!try_module_get(spi->dev.driver->owner))
  239. goto error;
  240. sd = spi_get_drvdata(spi);
  241. /* Register with the v4l2_device which increases the module's
  242. use count as well. */
  243. if (v4l2_device_register_subdev(v4l2_dev, sd))
  244. sd = NULL;
  245. /* Decrease the module use count to match the first try_module_get. */
  246. module_put(spi->dev.driver->owner);
  247. error:
  248. /* If we have a client but no subdev, then something went wrong and
  249. we must unregister the client. */
  250. if (spi && sd == NULL)
  251. spi_unregister_device(spi);
  252. return sd;
  253. }
  254. EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
  255. #endif /* defined(CONFIG_SPI) */
  256. /* Clamp x to be between min and max, aligned to a multiple of 2^align. min
  257. * and max don't have to be aligned, but there must be at least one valid
  258. * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
  259. * of 16 between 17 and 31. */
  260. static unsigned int clamp_align(unsigned int x, unsigned int min,
  261. unsigned int max, unsigned int align)
  262. {
  263. /* Bits that must be zero to be aligned */
  264. unsigned int mask = ~((1 << align) - 1);
  265. /* Clamp to aligned min and max */
  266. x = clamp(x, (min + ~mask) & mask, max & mask);
  267. /* Round to nearest aligned value */
  268. if (align)
  269. x = (x + (1 << (align - 1))) & mask;
  270. return x;
  271. }
  272. /* Bound an image to have a width between wmin and wmax, and height between
  273. * hmin and hmax, inclusive. Additionally, the width will be a multiple of
  274. * 2^walign, the height will be a multiple of 2^halign, and the overall size
  275. * (width*height) will be a multiple of 2^salign. The image may be shrunk
  276. * or enlarged to fit the alignment constraints.
  277. *
  278. * The width or height maximum must not be smaller than the corresponding
  279. * minimum. The alignments must not be so high there are no possible image
  280. * sizes within the allowed bounds. wmin and hmin must be at least 1
  281. * (don't use 0). If you don't care about a certain alignment, specify 0,
  282. * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
  283. * you only want to adjust downward, specify a maximum that's the same as
  284. * the initial value.
  285. */
  286. void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
  287. unsigned int walign,
  288. u32 *h, unsigned int hmin, unsigned int hmax,
  289. unsigned int halign, unsigned int salign)
  290. {
  291. *w = clamp_align(*w, wmin, wmax, walign);
  292. *h = clamp_align(*h, hmin, hmax, halign);
  293. /* Usually we don't need to align the size and are done now. */
  294. if (!salign)
  295. return;
  296. /* How much alignment do we have? */
  297. walign = __ffs(*w);
  298. halign = __ffs(*h);
  299. /* Enough to satisfy the image alignment? */
  300. if (walign + halign < salign) {
  301. /* Max walign where there is still a valid width */
  302. unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
  303. /* Max halign where there is still a valid height */
  304. unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
  305. /* up the smaller alignment until we have enough */
  306. do {
  307. if (halign >= hmaxa ||
  308. (walign <= halign && walign < wmaxa)) {
  309. *w = clamp_align(*w, wmin, wmax, walign + 1);
  310. walign = __ffs(*w);
  311. } else {
  312. *h = clamp_align(*h, hmin, hmax, halign + 1);
  313. halign = __ffs(*h);
  314. }
  315. } while (halign + walign < salign);
  316. }
  317. }
  318. EXPORT_SYMBOL_GPL(v4l_bound_align_image);
  319. const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
  320. const struct v4l2_discrete_probe *probe,
  321. s32 width, s32 height)
  322. {
  323. int i;
  324. u32 error, min_error = UINT_MAX;
  325. const struct v4l2_frmsize_discrete *size, *best = NULL;
  326. if (!probe)
  327. return best;
  328. for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
  329. error = abs(size->width - width) + abs(size->height - height);
  330. if (error < min_error) {
  331. min_error = error;
  332. best = size;
  333. }
  334. if (!error)
  335. break;
  336. }
  337. return best;
  338. }
  339. EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
  340. void v4l2_get_timestamp(struct timeval *tv)
  341. {
  342. struct timespec ts;
  343. ktime_get_ts(&ts);
  344. tv->tv_sec = ts.tv_sec;
  345. tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  346. }
  347. EXPORT_SYMBOL_GPL(v4l2_get_timestamp);