v4l2-common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. v4l2 common internal API header
  3. This header contains internal shared ioctl definitions for use by the
  4. internal low-level v4l2 drivers.
  5. Each ioctl begins with VIDIOC_INT_ to clearly mark that it is an internal
  6. define,
  7. Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef V4L2_COMMON_H_
  21. #define V4L2_COMMON_H_
  22. #include <media/v4l2-dev.h>
  23. /* Common printk constructs for v4l-i2c drivers. These macros create a unique
  24. prefix consisting of the driver name, the adapter number and the i2c
  25. address. */
  26. #define v4l_printk(level, name, adapter, addr, fmt, arg...) \
  27. printk(level "%s %d-%04x: " fmt, name, i2c_adapter_id(adapter), addr , ## arg)
  28. #define v4l_client_printk(level, client, fmt, arg...) \
  29. v4l_printk(level, (client)->dev.driver->name, (client)->adapter, \
  30. (client)->addr, fmt , ## arg)
  31. #define v4l_err(client, fmt, arg...) \
  32. v4l_client_printk(KERN_ERR, client, fmt , ## arg)
  33. #define v4l_warn(client, fmt, arg...) \
  34. v4l_client_printk(KERN_WARNING, client, fmt , ## arg)
  35. #define v4l_info(client, fmt, arg...) \
  36. v4l_client_printk(KERN_INFO, client, fmt , ## arg)
  37. /* These three macros assume that the debug level is set with a module
  38. parameter called 'debug'. */
  39. #define v4l_dbg(level, debug, client, fmt, arg...) \
  40. do { \
  41. if (debug >= (level)) \
  42. v4l_client_printk(KERN_DEBUG, client, fmt , ## arg); \
  43. } while (0)
  44. /* Add a version of v4l_dbg to be used on drivers using dev_foo() macros */
  45. #define dev_dbg_lvl(__dev, __level, __debug, __fmt, __arg...) \
  46. do { \
  47. if (__debug >= (__level)) \
  48. dev_printk(KERN_DEBUG, __dev, __fmt, ##__arg); \
  49. } while (0)
  50. /* ------------------------------------------------------------------------- */
  51. /* These printk constructs can be used with v4l2_device and v4l2_subdev */
  52. #define v4l2_printk(level, dev, fmt, arg...) \
  53. printk(level "%s: " fmt, (dev)->name , ## arg)
  54. #define v4l2_err(dev, fmt, arg...) \
  55. v4l2_printk(KERN_ERR, dev, fmt , ## arg)
  56. #define v4l2_warn(dev, fmt, arg...) \
  57. v4l2_printk(KERN_WARNING, dev, fmt , ## arg)
  58. #define v4l2_info(dev, fmt, arg...) \
  59. v4l2_printk(KERN_INFO, dev, fmt , ## arg)
  60. /* These three macros assume that the debug level is set with a module
  61. parameter called 'debug'. */
  62. #define v4l2_dbg(level, debug, dev, fmt, arg...) \
  63. do { \
  64. if (debug >= (level)) \
  65. v4l2_printk(KERN_DEBUG, dev, fmt , ## arg); \
  66. } while (0)
  67. /**
  68. * v4l2_ctrl_query_fill- Fill in a struct v4l2_queryctrl
  69. *
  70. * @qctrl: pointer to the &struct v4l2_queryctrl to be filled
  71. * @min: minimum value for the control
  72. * @max: maximum value for the control
  73. * @step: control step
  74. * @def: default value for the control
  75. *
  76. * Fills the &struct v4l2_queryctrl fields for the query control.
  77. *
  78. * .. note::
  79. *
  80. * This function assumes that the @qctrl->id field is filled.
  81. *
  82. * Returns -EINVAL if the control is not known by the V4L2 core, 0 on success.
  83. */
  84. int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
  85. s32 min, s32 max, s32 step, s32 def);
  86. /* ------------------------------------------------------------------------- */
  87. /* I2C Helper functions */
  88. struct i2c_driver;
  89. struct i2c_adapter;
  90. struct i2c_client;
  91. struct i2c_device_id;
  92. struct v4l2_device;
  93. struct v4l2_subdev;
  94. struct v4l2_subdev_ops;
  95. /**
  96. * v4l2_i2c_new_subdev - Load an i2c module and return an initialized
  97. * &struct v4l2_subdev.
  98. *
  99. * @v4l2_dev: pointer to &struct v4l2_device
  100. * @adapter: pointer to struct i2c_adapter
  101. * @client_type: name of the chip that's on the adapter.
  102. * @addr: I2C address. If zero, it will use @probe_addrs
  103. * @probe_addrs: array with a list of address. The last entry at such
  104. * array should be %I2C_CLIENT_END.
  105. *
  106. * returns a &struct v4l2_subdev pointer.
  107. */
  108. struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
  109. struct i2c_adapter *adapter, const char *client_type,
  110. u8 addr, const unsigned short *probe_addrs);
  111. struct i2c_board_info;
  112. /**
  113. * v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized
  114. * &struct v4l2_subdev.
  115. *
  116. * @v4l2_dev: pointer to &struct v4l2_device
  117. * @adapter: pointer to struct i2c_adapter
  118. * @info: pointer to struct i2c_board_info used to replace the irq,
  119. * platform_data and addr arguments.
  120. * @probe_addrs: array with a list of address. The last entry at such
  121. * array should be %I2C_CLIENT_END.
  122. *
  123. * returns a &struct v4l2_subdev pointer.
  124. */
  125. struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
  126. struct i2c_adapter *adapter, struct i2c_board_info *info,
  127. const unsigned short *probe_addrs);
  128. /**
  129. * v4l2_i2c_subdev_set_name - Set name for an I²C sub-device
  130. *
  131. * @sd: pointer to &struct v4l2_subdev
  132. * @client: pointer to struct i2c_client
  133. * @devname: the name of the device; if NULL, the I²C device's name will be used
  134. * @postfix: sub-device specific string to put right after the I²C device name;
  135. * may be NULL
  136. */
  137. void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
  138. const char *devname, const char *postfix);
  139. /**
  140. * v4l2_i2c_subdev_init - Initializes a &struct v4l2_subdev with data from
  141. * an i2c_client struct.
  142. *
  143. * @sd: pointer to &struct v4l2_subdev
  144. * @client: pointer to struct i2c_client
  145. * @ops: pointer to &struct v4l2_subdev_ops
  146. */
  147. void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
  148. const struct v4l2_subdev_ops *ops);
  149. /**
  150. * v4l2_i2c_subdev_addr - returns i2c client address of &struct v4l2_subdev.
  151. *
  152. * @sd: pointer to &struct v4l2_subdev
  153. *
  154. * Returns the address of an I2C sub-device
  155. */
  156. unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd);
  157. /**
  158. * enum v4l2_i2c_tuner_type - specifies the range of tuner address that
  159. * should be used when seeking for I2C devices.
  160. *
  161. * @ADDRS_RADIO: Radio tuner addresses.
  162. * Represent the following I2C addresses:
  163. * 0x10 (if compiled with tea5761 support)
  164. * and 0x60.
  165. * @ADDRS_DEMOD: Demod tuner addresses.
  166. * Represent the following I2C addresses:
  167. * 0x42, 0x43, 0x4a and 0x4b.
  168. * @ADDRS_TV: TV tuner addresses.
  169. * Represent the following I2C addresses:
  170. * 0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62,
  171. * 0x63 and 0x64.
  172. * @ADDRS_TV_WITH_DEMOD: TV tuner addresses if demod is present, this
  173. * excludes addresses used by the demodulator
  174. * from the list of candidates.
  175. * Represent the following I2C addresses:
  176. * 0x60, 0x61, 0x62, 0x63 and 0x64.
  177. *
  178. * NOTE: All I2C addresses above use the 7-bit notation.
  179. */
  180. enum v4l2_i2c_tuner_type {
  181. ADDRS_RADIO,
  182. ADDRS_DEMOD,
  183. ADDRS_TV,
  184. ADDRS_TV_WITH_DEMOD,
  185. };
  186. /**
  187. * v4l2_i2c_tuner_addrs - Return a list of I2C tuner addresses to probe.
  188. *
  189. * @type: type of the tuner to seek, as defined by
  190. * &enum v4l2_i2c_tuner_type.
  191. *
  192. * NOTE: Use only if the tuner addresses are unknown.
  193. */
  194. const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type);
  195. /* ------------------------------------------------------------------------- */
  196. /* SPI Helper functions */
  197. #if defined(CONFIG_SPI)
  198. #include <linux/spi/spi.h>
  199. struct spi_device;
  200. /**
  201. * v4l2_spi_new_subdev - Load an spi module and return an initialized
  202. * &struct v4l2_subdev.
  203. *
  204. *
  205. * @v4l2_dev: pointer to &struct v4l2_device.
  206. * @master: pointer to struct spi_master.
  207. * @info: pointer to struct spi_board_info.
  208. *
  209. * returns a &struct v4l2_subdev pointer.
  210. */
  211. struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
  212. struct spi_master *master, struct spi_board_info *info);
  213. /**
  214. * v4l2_spi_subdev_init - Initialize a v4l2_subdev with data from an
  215. * spi_device struct.
  216. *
  217. * @sd: pointer to &struct v4l2_subdev
  218. * @spi: pointer to struct spi_device.
  219. * @ops: pointer to &struct v4l2_subdev_ops
  220. */
  221. void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
  222. const struct v4l2_subdev_ops *ops);
  223. #endif
  224. /* ------------------------------------------------------------------------- */
  225. /*
  226. * FIXME: these remaining ioctls/structs should be removed as well, but they
  227. * are still used in tuner-simple.c (TUNER_SET_CONFIG) and cx18/ivtv (RESET).
  228. * To remove these ioctls some more cleanup is needed in those modules.
  229. *
  230. * It doesn't make much sense on documenting them, as what we really want is
  231. * to get rid of them.
  232. */
  233. /* s_config */
  234. struct v4l2_priv_tun_config {
  235. int tuner;
  236. void *priv;
  237. };
  238. #define TUNER_SET_CONFIG _IOW('d', 92, struct v4l2_priv_tun_config)
  239. #define VIDIOC_INT_RESET _IOW ('d', 102, u32)
  240. /* ------------------------------------------------------------------------- */
  241. /* Miscellaneous helper functions */
  242. /**
  243. * v4l_bound_align_image - adjust video dimensions according to
  244. * a given constraints.
  245. *
  246. * @width: pointer to width that will be adjusted if needed.
  247. * @wmin: minimum width.
  248. * @wmax: maximum width.
  249. * @walign: least significant bit on width.
  250. * @height: pointer to height that will be adjusted if needed.
  251. * @hmin: minimum height.
  252. * @hmax: maximum height.
  253. * @halign: least significant bit on height.
  254. * @salign: least significant bit for the image size (e. g.
  255. * :math:`width * height`).
  256. *
  257. * Clip an image to have @width between @wmin and @wmax, and @height between
  258. * @hmin and @hmax, inclusive.
  259. *
  260. * Additionally, the @width will be a multiple of :math:`2^{walign}`,
  261. * the @height will be a multiple of :math:`2^{halign}`, and the overall
  262. * size :math:`width * height` will be a multiple of :math:`2^{salign}`.
  263. *
  264. * .. note::
  265. *
  266. * #. The clipping rectangle may be shrunk or enlarged to fit the alignment
  267. * constraints.
  268. * #. @wmax must not be smaller than @wmin.
  269. * #. @hmax must not be smaller than @hmin.
  270. * #. The alignments must not be so high there are no possible image
  271. * sizes within the allowed bounds.
  272. * #. @wmin and @hmin must be at least 1 (don't use 0).
  273. * #. For @walign, @halign and @salign, if you don't care about a certain
  274. * alignment, specify ``0``, as :math:`2^0 = 1` and one byte alignment
  275. * is equivalent to no alignment.
  276. * #. If you only want to adjust downward, specify a maximum that's the
  277. * same as the initial value.
  278. */
  279. void v4l_bound_align_image(unsigned int *width, unsigned int wmin,
  280. unsigned int wmax, unsigned int walign,
  281. unsigned int *height, unsigned int hmin,
  282. unsigned int hmax, unsigned int halign,
  283. unsigned int salign);
  284. /**
  285. * v4l2_find_nearest_size - Find the nearest size among a discrete
  286. * set of resolutions contained in an array of a driver specific struct.
  287. *
  288. * @array: a driver specific array of image sizes
  289. * @array_size: the length of the driver specific array of image sizes
  290. * @width_field: the name of the width field in the driver specific struct
  291. * @height_field: the name of the height field in the driver specific struct
  292. * @width: desired width.
  293. * @height: desired height.
  294. *
  295. * Finds the closest resolution to minimize the width and height differences
  296. * between what requested and the supported resolutions. The size of the width
  297. * and height fields in the driver specific must equal to that of u32, i.e. four
  298. * bytes.
  299. *
  300. * Returns the best match or NULL if the length of the array is zero.
  301. */
  302. #define v4l2_find_nearest_size(array, array_size, width_field, height_field, \
  303. width, height) \
  304. ({ \
  305. BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \
  306. sizeof((array)->height_field) != sizeof(u32)); \
  307. (typeof(&(array)[0]))__v4l2_find_nearest_size( \
  308. (array), array_size, sizeof(*(array)), \
  309. offsetof(typeof(*(array)), width_field), \
  310. offsetof(typeof(*(array)), height_field), \
  311. width, height); \
  312. })
  313. const void *
  314. __v4l2_find_nearest_size(const void *array, size_t array_size,
  315. size_t entry_size, size_t width_offset,
  316. size_t height_offset, s32 width, s32 height);
  317. /**
  318. * v4l2_get_timestamp - helper routine to get a timestamp to be used when
  319. * filling streaming metadata. Internally, it uses ktime_get_ts(),
  320. * which is the recommended way to get it.
  321. *
  322. * @tv: pointer to &struct timeval to be filled.
  323. */
  324. void v4l2_get_timestamp(struct timeval *tv);
  325. /**
  326. * v4l2_g_parm_cap - helper routine for vidioc_g_parm to fill this in by
  327. * calling the g_frame_interval op of the given subdev. It only works
  328. * for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
  329. * function name.
  330. *
  331. * @vdev: the struct video_device pointer. Used to determine the device caps.
  332. * @sd: the sub-device pointer.
  333. * @a: the VIDIOC_G_PARM argument.
  334. */
  335. int v4l2_g_parm_cap(struct video_device *vdev,
  336. struct v4l2_subdev *sd, struct v4l2_streamparm *a);
  337. /**
  338. * v4l2_s_parm_cap - helper routine for vidioc_s_parm to fill this in by
  339. * calling the s_frame_interval op of the given subdev. It only works
  340. * for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
  341. * function name.
  342. *
  343. * @vdev: the struct video_device pointer. Used to determine the device caps.
  344. * @sd: the sub-device pointer.
  345. * @a: the VIDIOC_S_PARM argument.
  346. */
  347. int v4l2_s_parm_cap(struct video_device *vdev,
  348. struct v4l2_subdev *sd, struct v4l2_streamparm *a);
  349. #endif /* V4L2_COMMON_H_ */