v4l2-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <linux/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 (!sd)
  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. void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
  273. unsigned int walign,
  274. u32 *h, unsigned int hmin, unsigned int hmax,
  275. unsigned int halign, unsigned int salign)
  276. {
  277. *w = clamp_align(*w, wmin, wmax, walign);
  278. *h = clamp_align(*h, hmin, hmax, halign);
  279. /* Usually we don't need to align the size and are done now. */
  280. if (!salign)
  281. return;
  282. /* How much alignment do we have? */
  283. walign = __ffs(*w);
  284. halign = __ffs(*h);
  285. /* Enough to satisfy the image alignment? */
  286. if (walign + halign < salign) {
  287. /* Max walign where there is still a valid width */
  288. unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
  289. /* Max halign where there is still a valid height */
  290. unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
  291. /* up the smaller alignment until we have enough */
  292. do {
  293. if (halign >= hmaxa ||
  294. (walign <= halign && walign < wmaxa)) {
  295. *w = clamp_align(*w, wmin, wmax, walign + 1);
  296. walign = __ffs(*w);
  297. } else {
  298. *h = clamp_align(*h, hmin, hmax, halign + 1);
  299. halign = __ffs(*h);
  300. }
  301. } while (halign + walign < salign);
  302. }
  303. }
  304. EXPORT_SYMBOL_GPL(v4l_bound_align_image);
  305. const void *
  306. __v4l2_find_nearest_size(const void *array, size_t array_size,
  307. size_t entry_size, size_t width_offset,
  308. size_t height_offset, s32 width, s32 height)
  309. {
  310. u32 error, min_error = U32_MAX;
  311. const void *best = NULL;
  312. unsigned int i;
  313. if (!array)
  314. return NULL;
  315. for (i = 0; i < array_size; i++, array += entry_size) {
  316. const u32 *entry_width = array + width_offset;
  317. const u32 *entry_height = array + height_offset;
  318. error = abs(*entry_width - width) + abs(*entry_height - height);
  319. if (error > min_error)
  320. continue;
  321. min_error = error;
  322. best = array;
  323. if (!error)
  324. break;
  325. }
  326. return best;
  327. }
  328. EXPORT_SYMBOL_GPL(__v4l2_find_nearest_size);
  329. void v4l2_get_timestamp(struct timeval *tv)
  330. {
  331. struct timespec ts;
  332. ktime_get_ts(&ts);
  333. tv->tv_sec = ts.tv_sec;
  334. tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  335. }
  336. EXPORT_SYMBOL_GPL(v4l2_get_timestamp);
  337. int v4l2_g_parm_cap(struct video_device *vdev,
  338. struct v4l2_subdev *sd, struct v4l2_streamparm *a)
  339. {
  340. struct v4l2_subdev_frame_interval ival = { 0 };
  341. int ret;
  342. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  343. a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  344. return -EINVAL;
  345. if (vdev->device_caps & V4L2_CAP_READWRITE)
  346. a->parm.capture.readbuffers = 2;
  347. if (v4l2_subdev_has_op(sd, video, g_frame_interval))
  348. a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  349. ret = v4l2_subdev_call(sd, video, g_frame_interval, &ival);
  350. if (!ret)
  351. a->parm.capture.timeperframe = ival.interval;
  352. return ret;
  353. }
  354. EXPORT_SYMBOL_GPL(v4l2_g_parm_cap);
  355. int v4l2_s_parm_cap(struct video_device *vdev,
  356. struct v4l2_subdev *sd, struct v4l2_streamparm *a)
  357. {
  358. struct v4l2_subdev_frame_interval ival = {
  359. .interval = a->parm.capture.timeperframe
  360. };
  361. int ret;
  362. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  363. a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  364. return -EINVAL;
  365. memset(&a->parm, 0, sizeof(a->parm));
  366. if (vdev->device_caps & V4L2_CAP_READWRITE)
  367. a->parm.capture.readbuffers = 2;
  368. else
  369. a->parm.capture.readbuffers = 0;
  370. if (v4l2_subdev_has_op(sd, video, g_frame_interval))
  371. a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  372. ret = v4l2_subdev_call(sd, video, s_frame_interval, &ival);
  373. if (!ret)
  374. a->parm.capture.timeperframe = ival.interval;
  375. return ret;
  376. }
  377. EXPORT_SYMBOL_GPL(v4l2_s_parm_cap);