v4l2-common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. /* Check for correctness of the ctrl's value based on the data from
  75. struct v4l2_queryctrl and the available menu items. Note that
  76. menu_items may be NULL, in that case it is ignored. */
  77. int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
  78. const char * const *menu_items)
  79. {
  80. if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
  81. return -EINVAL;
  82. if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
  83. return -EBUSY;
  84. if (qctrl->type == V4L2_CTRL_TYPE_STRING)
  85. return 0;
  86. if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
  87. qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
  88. qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
  89. return 0;
  90. if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
  91. return -ERANGE;
  92. if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
  93. if (menu_items[ctrl->value] == NULL ||
  94. menu_items[ctrl->value][0] == '\0')
  95. return -EINVAL;
  96. }
  97. if (qctrl->type == V4L2_CTRL_TYPE_BITMASK &&
  98. (ctrl->value & ~qctrl->maximum))
  99. return -ERANGE;
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(v4l2_ctrl_check);
  103. /* Fill in a struct v4l2_queryctrl */
  104. int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 _min, s32 _max, s32 _step, s32 _def)
  105. {
  106. const char *name;
  107. s64 min = _min;
  108. s64 max = _max;
  109. u64 step = _step;
  110. s64 def = _def;
  111. v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
  112. &min, &max, &step, &def, &qctrl->flags);
  113. if (name == NULL)
  114. return -EINVAL;
  115. qctrl->minimum = min;
  116. qctrl->maximum = max;
  117. qctrl->step = step;
  118. qctrl->default_value = def;
  119. qctrl->reserved[0] = qctrl->reserved[1] = 0;
  120. strlcpy(qctrl->name, name, sizeof(qctrl->name));
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(v4l2_ctrl_query_fill);
  124. /* I2C Helper functions */
  125. #if IS_ENABLED(CONFIG_I2C)
  126. void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
  127. const struct v4l2_subdev_ops *ops)
  128. {
  129. v4l2_subdev_init(sd, ops);
  130. sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
  131. /* the owner is the same as the i2c_client's driver owner */
  132. sd->owner = client->dev.driver->owner;
  133. sd->dev = &client->dev;
  134. /* i2c_client and v4l2_subdev point to one another */
  135. v4l2_set_subdevdata(sd, client);
  136. i2c_set_clientdata(client, sd);
  137. /* initialize name */
  138. snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
  139. client->dev.driver->name, i2c_adapter_id(client->adapter),
  140. client->addr);
  141. }
  142. EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
  143. /* Load an i2c sub-device. */
  144. struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
  145. struct i2c_adapter *adapter, struct i2c_board_info *info,
  146. const unsigned short *probe_addrs)
  147. {
  148. struct v4l2_subdev *sd = NULL;
  149. struct i2c_client *client;
  150. BUG_ON(!v4l2_dev);
  151. 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,
  155. NULL);
  156. else
  157. client = i2c_new_device(adapter, info);
  158. /* Note: by loading the module first we are certain that c->driver
  159. will be set if the driver was found. If the module was not loaded
  160. first, then the i2c core tries to delay-load the module for us,
  161. and then c->driver is still NULL until the module is finally
  162. loaded. This delay-load mechanism doesn't work if other drivers
  163. want to use the i2c device, so explicitly loading the module
  164. is the best alternative. */
  165. if (client == NULL || client->dev.driver == NULL)
  166. goto error;
  167. /* Lock the module so we can safely get the v4l2_subdev pointer */
  168. if (!try_module_get(client->dev.driver->owner))
  169. goto error;
  170. sd = i2c_get_clientdata(client);
  171. /* Register with the v4l2_device which increases the module's
  172. use count as well. */
  173. if (v4l2_device_register_subdev(v4l2_dev, sd))
  174. sd = NULL;
  175. /* Decrease the module use count to match the first try_module_get. */
  176. module_put(client->dev.driver->owner);
  177. error:
  178. /* If we have a client but no subdev, then something went wrong and
  179. we must unregister the client. */
  180. if (client && sd == NULL)
  181. i2c_unregister_device(client);
  182. return sd;
  183. }
  184. EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
  185. struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
  186. struct i2c_adapter *adapter, const char *client_type,
  187. u8 addr, const unsigned short *probe_addrs)
  188. {
  189. struct i2c_board_info info;
  190. /* Setup the i2c board info with the device type and
  191. the device address. */
  192. memset(&info, 0, sizeof(info));
  193. strlcpy(info.type, client_type, sizeof(info.type));
  194. info.addr = addr;
  195. return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
  196. }
  197. EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
  198. /* Return i2c client address of v4l2_subdev. */
  199. unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
  200. {
  201. struct i2c_client *client = v4l2_get_subdevdata(sd);
  202. return client ? client->addr : I2C_CLIENT_END;
  203. }
  204. EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
  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. switch (type) {
  226. case ADDRS_RADIO:
  227. return radio_addrs;
  228. case ADDRS_DEMOD:
  229. return demod_addrs;
  230. case ADDRS_TV:
  231. return tv_addrs;
  232. case ADDRS_TV_WITH_DEMOD:
  233. return tv_addrs + 4;
  234. }
  235. return NULL;
  236. }
  237. EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
  238. #endif /* defined(CONFIG_I2C) */
  239. #if defined(CONFIG_SPI)
  240. /* Load an spi sub-device. */
  241. void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
  242. const struct v4l2_subdev_ops *ops)
  243. {
  244. v4l2_subdev_init(sd, ops);
  245. sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
  246. /* the owner is the same as the spi_device's driver owner */
  247. sd->owner = spi->dev.driver->owner;
  248. sd->dev = &spi->dev;
  249. /* spi_device and v4l2_subdev point to one another */
  250. v4l2_set_subdevdata(sd, spi);
  251. spi_set_drvdata(spi, sd);
  252. /* initialize name */
  253. strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
  254. }
  255. EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
  256. struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
  257. struct spi_master *master, struct spi_board_info *info)
  258. {
  259. struct v4l2_subdev *sd = NULL;
  260. struct spi_device *spi = NULL;
  261. BUG_ON(!v4l2_dev);
  262. if (info->modalias[0])
  263. request_module(info->modalias);
  264. spi = spi_new_device(master, info);
  265. if (spi == NULL || spi->dev.driver == NULL)
  266. goto error;
  267. if (!try_module_get(spi->dev.driver->owner))
  268. goto error;
  269. sd = spi_get_drvdata(spi);
  270. /* Register with the v4l2_device which increases the module's
  271. use count as well. */
  272. if (v4l2_device_register_subdev(v4l2_dev, sd))
  273. sd = NULL;
  274. /* Decrease the module use count to match the first try_module_get. */
  275. module_put(spi->dev.driver->owner);
  276. error:
  277. /* If we have a client but no subdev, then something went wrong and
  278. we must unregister the client. */
  279. if (spi && sd == NULL)
  280. spi_unregister_device(spi);
  281. return sd;
  282. }
  283. EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
  284. #endif /* defined(CONFIG_SPI) */
  285. /* Clamp x to be between min and max, aligned to a multiple of 2^align. min
  286. * and max don't have to be aligned, but there must be at least one valid
  287. * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
  288. * of 16 between 17 and 31. */
  289. static unsigned int clamp_align(unsigned int x, unsigned int min,
  290. unsigned int max, unsigned int align)
  291. {
  292. /* Bits that must be zero to be aligned */
  293. unsigned int mask = ~((1 << align) - 1);
  294. /* Clamp to aligned min and max */
  295. x = clamp(x, (min + ~mask) & mask, max & mask);
  296. /* Round to nearest aligned value */
  297. if (align)
  298. x = (x + (1 << (align - 1))) & mask;
  299. return x;
  300. }
  301. /* Bound an image to have a width between wmin and wmax, and height between
  302. * hmin and hmax, inclusive. Additionally, the width will be a multiple of
  303. * 2^walign, the height will be a multiple of 2^halign, and the overall size
  304. * (width*height) will be a multiple of 2^salign. The image may be shrunk
  305. * or enlarged to fit the alignment constraints.
  306. *
  307. * The width or height maximum must not be smaller than the corresponding
  308. * minimum. The alignments must not be so high there are no possible image
  309. * sizes within the allowed bounds. wmin and hmin must be at least 1
  310. * (don't use 0). If you don't care about a certain alignment, specify 0,
  311. * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
  312. * you only want to adjust downward, specify a maximum that's the same as
  313. * the initial value.
  314. */
  315. void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
  316. unsigned int walign,
  317. u32 *h, unsigned int hmin, unsigned int hmax,
  318. unsigned int halign, unsigned int salign)
  319. {
  320. *w = clamp_align(*w, wmin, wmax, walign);
  321. *h = clamp_align(*h, hmin, hmax, halign);
  322. /* Usually we don't need to align the size and are done now. */
  323. if (!salign)
  324. return;
  325. /* How much alignment do we have? */
  326. walign = __ffs(*w);
  327. halign = __ffs(*h);
  328. /* Enough to satisfy the image alignment? */
  329. if (walign + halign < salign) {
  330. /* Max walign where there is still a valid width */
  331. unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
  332. /* Max halign where there is still a valid height */
  333. unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
  334. /* up the smaller alignment until we have enough */
  335. do {
  336. if (halign >= hmaxa ||
  337. (walign <= halign && walign < wmaxa)) {
  338. *w = clamp_align(*w, wmin, wmax, walign + 1);
  339. walign = __ffs(*w);
  340. } else {
  341. *h = clamp_align(*h, hmin, hmax, halign + 1);
  342. halign = __ffs(*h);
  343. }
  344. } while (halign + walign < salign);
  345. }
  346. }
  347. EXPORT_SYMBOL_GPL(v4l_bound_align_image);
  348. const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
  349. const struct v4l2_discrete_probe *probe,
  350. s32 width, s32 height)
  351. {
  352. int i;
  353. u32 error, min_error = UINT_MAX;
  354. const struct v4l2_frmsize_discrete *size, *best = NULL;
  355. if (!probe)
  356. return best;
  357. for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
  358. error = abs(size->width - width) + abs(size->height - height);
  359. if (error < min_error) {
  360. min_error = error;
  361. best = size;
  362. }
  363. if (!error)
  364. break;
  365. }
  366. return best;
  367. }
  368. EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
  369. void v4l2_get_timestamp(struct timeval *tv)
  370. {
  371. struct timespec ts;
  372. ktime_get_ts(&ts);
  373. tv->tv_sec = ts.tv_sec;
  374. tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  375. }
  376. EXPORT_SYMBOL_GPL(v4l2_get_timestamp);