/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2v4l2 common internal API header34This header contains internal shared ioctl definitions for use by the5internal low-level v4l2 drivers.6Each ioctl begins with VIDIOC_INT_ to clearly mark that it is an internal7define,89Copyright (C) 2005 Hans Verkuil <[email protected]>1011*/1213#ifndef V4L2_COMMON_H_14#define V4L2_COMMON_H_1516#include <linux/time.h>17#include <media/v4l2-dev.h>1819/* Common printk constructs for v4l-i2c drivers. These macros create a unique20prefix consisting of the driver name, the adapter number and the i2c21address. */22#define v4l_printk(level, name, adapter, addr, fmt, arg...) \23printk(level "%s %d-%04x: " fmt, name, i2c_adapter_id(adapter), addr , ## arg)2425#define v4l_client_printk(level, client, fmt, arg...) \26v4l_printk(level, (client)->dev.driver->name, (client)->adapter, \27(client)->addr, fmt , ## arg)2829#define v4l_err(client, fmt, arg...) \30v4l_client_printk(KERN_ERR, client, fmt , ## arg)3132#define v4l_warn(client, fmt, arg...) \33v4l_client_printk(KERN_WARNING, client, fmt , ## arg)3435#define v4l_info(client, fmt, arg...) \36v4l_client_printk(KERN_INFO, client, fmt , ## arg)3738/* These three macros assume that the debug level is set with a module39parameter called 'debug'. */40#define v4l_dbg(level, debug, client, fmt, arg...) \41do { \42if (debug >= (level)) \43v4l_client_printk(KERN_DEBUG, client, fmt , ## arg); \44} while (0)4546/* Add a version of v4l_dbg to be used on drivers using dev_foo() macros */47#define dev_dbg_lvl(__dev, __level, __debug, __fmt, __arg...) \48do { \49if (__debug >= (__level)) \50dev_printk(KERN_DEBUG, __dev, __fmt, ##__arg); \51} while (0)5253/* ------------------------------------------------------------------------- */5455/* These printk constructs can be used with v4l2_device and v4l2_subdev */56#define v4l2_printk(level, dev, fmt, arg...) \57printk(level "%s: " fmt, (dev)->name , ## arg)5859#define v4l2_err(dev, fmt, arg...) \60v4l2_printk(KERN_ERR, dev, fmt , ## arg)6162#define v4l2_warn(dev, fmt, arg...) \63v4l2_printk(KERN_WARNING, dev, fmt , ## arg)6465#define v4l2_info(dev, fmt, arg...) \66v4l2_printk(KERN_INFO, dev, fmt , ## arg)6768/* These three macros assume that the debug level is set with a module69parameter called 'debug'. */70#define v4l2_dbg(level, debug, dev, fmt, arg...) \71do { \72if (debug >= (level)) \73v4l2_printk(KERN_DEBUG, dev, fmt , ## arg); \74} while (0)7576/**77* v4l2_ctrl_query_fill- Fill in a struct v4l2_queryctrl78*79* @qctrl: pointer to the &struct v4l2_queryctrl to be filled80* @min: minimum value for the control81* @max: maximum value for the control82* @step: control step83* @def: default value for the control84*85* Fills the &struct v4l2_queryctrl fields for the query control.86*87* .. note::88*89* This function assumes that the @qctrl->id field is filled.90*91* Returns -EINVAL if the control is not known by the V4L2 core, 0 on success.92*/9394int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl,95s32 min, s32 max, s32 step, s32 def);9697/* ------------------------------------------------------------------------- */9899struct v4l2_device;100struct v4l2_subdev;101struct v4l2_subdev_ops;102103/* I2C Helper functions */104#include <linux/i2c.h>105106/**107* enum v4l2_i2c_tuner_type - specifies the range of tuner address that108* should be used when seeking for I2C devices.109*110* @ADDRS_RADIO: Radio tuner addresses.111* Represent the following I2C addresses:112* 0x10 (if compiled with tea5761 support)113* and 0x60.114* @ADDRS_DEMOD: Demod tuner addresses.115* Represent the following I2C addresses:116* 0x42, 0x43, 0x4a and 0x4b.117* @ADDRS_TV: TV tuner addresses.118* Represent the following I2C addresses:119* 0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62,120* 0x63 and 0x64.121* @ADDRS_TV_WITH_DEMOD: TV tuner addresses if demod is present, this122* excludes addresses used by the demodulator123* from the list of candidates.124* Represent the following I2C addresses:125* 0x60, 0x61, 0x62, 0x63 and 0x64.126*127* NOTE: All I2C addresses above use the 7-bit notation.128*/129enum v4l2_i2c_tuner_type {130ADDRS_RADIO,131ADDRS_DEMOD,132ADDRS_TV,133ADDRS_TV_WITH_DEMOD,134};135136#if defined(CONFIG_VIDEO_V4L2_I2C)137138/**139* v4l2_i2c_new_subdev - Load an i2c module and return an initialized140* &struct v4l2_subdev.141*142* @v4l2_dev: pointer to &struct v4l2_device143* @adapter: pointer to struct i2c_adapter144* @client_type: name of the chip that's on the adapter.145* @addr: I2C address. If zero, it will use @probe_addrs146* @probe_addrs: array with a list of address. The last entry at such147* array should be %I2C_CLIENT_END.148*149* returns a &struct v4l2_subdev pointer.150*/151struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,152struct i2c_adapter *adapter, const char *client_type,153u8 addr, const unsigned short *probe_addrs);154155/**156* v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized157* &struct v4l2_subdev.158*159* @v4l2_dev: pointer to &struct v4l2_device160* @adapter: pointer to struct i2c_adapter161* @info: pointer to struct i2c_board_info used to replace the irq,162* platform_data and addr arguments.163* @probe_addrs: array with a list of address. The last entry at such164* array should be %I2C_CLIENT_END.165*166* returns a &struct v4l2_subdev pointer.167*/168struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,169struct i2c_adapter *adapter, struct i2c_board_info *info,170const unsigned short *probe_addrs);171172/**173* v4l2_i2c_subdev_set_name - Set name for an I²C sub-device174*175* @sd: pointer to &struct v4l2_subdev176* @client: pointer to struct i2c_client177* @devname: the name of the device; if NULL, the I²C device drivers's name178* will be used179* @postfix: sub-device specific string to put right after the I²C device name;180* may be NULL181*/182void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,183const char *devname, const char *postfix);184185/**186* v4l2_i2c_subdev_init - Initializes a &struct v4l2_subdev with data from187* an i2c_client struct.188*189* @sd: pointer to &struct v4l2_subdev190* @client: pointer to struct i2c_client191* @ops: pointer to &struct v4l2_subdev_ops192*/193void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,194const struct v4l2_subdev_ops *ops);195196/**197* v4l2_i2c_subdev_addr - returns i2c client address of &struct v4l2_subdev.198*199* @sd: pointer to &struct v4l2_subdev200*201* Returns the address of an I2C sub-device202*/203unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd);204205/**206* v4l2_i2c_tuner_addrs - Return a list of I2C tuner addresses to probe.207*208* @type: type of the tuner to seek, as defined by209* &enum v4l2_i2c_tuner_type.210*211* NOTE: Use only if the tuner addresses are unknown.212*/213const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type);214215/**216* v4l2_i2c_subdev_unregister - Unregister a v4l2_subdev217*218* @sd: pointer to &struct v4l2_subdev219*/220void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd);221222#else223224static inline struct v4l2_subdev *225v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,226struct i2c_adapter *adapter, const char *client_type,227u8 addr, const unsigned short *probe_addrs)228{229return NULL;230}231232static inline struct v4l2_subdev *233v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,234struct i2c_adapter *adapter, struct i2c_board_info *info,235const unsigned short *probe_addrs)236{237return NULL;238}239240static inline void241v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,242const char *devname, const char *postfix)243{}244245static inline void246v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,247const struct v4l2_subdev_ops *ops)248{}249250static inline unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)251{252return I2C_CLIENT_END;253}254255static inline const unsigned short *256v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)257{258return NULL;259}260261static inline void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd)262{}263264#endif265266/* ------------------------------------------------------------------------- */267268/* SPI Helper functions */269270#include <linux/spi/spi.h>271272#if defined(CONFIG_SPI)273274/**275* v4l2_spi_new_subdev - Load an spi module and return an initialized276* &struct v4l2_subdev.277*278*279* @v4l2_dev: pointer to &struct v4l2_device.280* @ctlr: pointer to struct spi_controller.281* @info: pointer to struct spi_board_info.282*283* returns a &struct v4l2_subdev pointer.284*/285struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,286struct spi_controller *ctlr, struct spi_board_info *info);287288/**289* v4l2_spi_subdev_init - Initialize a v4l2_subdev with data from an290* spi_device struct.291*292* @sd: pointer to &struct v4l2_subdev293* @spi: pointer to struct spi_device.294* @ops: pointer to &struct v4l2_subdev_ops295*/296void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,297const struct v4l2_subdev_ops *ops);298299/**300* v4l2_spi_subdev_unregister - Unregister a v4l2_subdev301*302* @sd: pointer to &struct v4l2_subdev303*/304void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd);305306#else307308static inline struct v4l2_subdev *309v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,310struct spi_controller *ctlr, struct spi_board_info *info)311{312return NULL;313}314315static inline void316v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,317const struct v4l2_subdev_ops *ops)318{}319320static inline void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd)321{}322#endif323324/* ------------------------------------------------------------------------- */325326/*327* FIXME: these remaining ioctls/structs should be removed as well, but they328* are still used in tuner-simple.c (TUNER_SET_CONFIG) and cx18/ivtv (RESET).329* To remove these ioctls some more cleanup is needed in those modules.330*331* It doesn't make much sense on documenting them, as what we really want is332* to get rid of them.333*/334335/* s_config */336struct v4l2_priv_tun_config {337int tuner;338void *priv;339};340#define TUNER_SET_CONFIG _IOW('d', 92, struct v4l2_priv_tun_config)341342#define VIDIOC_INT_RESET _IOW ('d', 102, u32)343344/* ------------------------------------------------------------------------- */345346/* Miscellaneous helper functions */347348/**349* v4l_bound_align_image - adjust video dimensions according to350* a given constraints.351*352* @width: pointer to width that will be adjusted if needed.353* @wmin: minimum width.354* @wmax: maximum width.355* @walign: least significant bit on width.356* @height: pointer to height that will be adjusted if needed.357* @hmin: minimum height.358* @hmax: maximum height.359* @halign: least significant bit on height.360* @salign: least significant bit for the image size (e. g.361* :math:`width * height`).362*363* Clip an image to have @width between @wmin and @wmax, and @height between364* @hmin and @hmax, inclusive.365*366* Additionally, the @width will be a multiple of :math:`2^{walign}`,367* the @height will be a multiple of :math:`2^{halign}`, and the overall368* size :math:`width * height` will be a multiple of :math:`2^{salign}`.369*370* .. note::371*372* #. The clipping rectangle may be shrunk or enlarged to fit the alignment373* constraints.374* #. @wmax must not be smaller than @wmin.375* #. @hmax must not be smaller than @hmin.376* #. The alignments must not be so high there are no possible image377* sizes within the allowed bounds.378* #. @wmin and @hmin must be at least 1 (don't use 0).379* #. For @walign, @halign and @salign, if you don't care about a certain380* alignment, specify ``0``, as :math:`2^0 = 1` and one byte alignment381* is equivalent to no alignment.382* #. If you only want to adjust downward, specify a maximum that's the383* same as the initial value.384*/385void v4l_bound_align_image(unsigned int *width, unsigned int wmin,386unsigned int wmax, unsigned int walign,387unsigned int *height, unsigned int hmin,388unsigned int hmax, unsigned int halign,389unsigned int salign);390391/**392* v4l2_find_nearest_size_conditional - Find the nearest size among a discrete393* set of resolutions contained in an array of a driver specific struct,394* with conditionally exlusion of certain modes395*396* @array: a driver specific array of image sizes397* @array_size: the length of the driver specific array of image sizes398* @width_field: the name of the width field in the driver specific struct399* @height_field: the name of the height field in the driver specific struct400* @width: desired width401* @height: desired height402* @func: ignores mode if returns false403* @context: context for the function404*405* Finds the closest resolution to minimize the width and height differences406* between what requested and the supported resolutions. The size of the width407* and height fields in the driver specific must equal to that of u32, i.e. four408* bytes. @func is called for each mode considered, a mode is ignored if @func409* returns false for it.410*411* Returns the best match or NULL if the length of the array is zero.412*/413#define v4l2_find_nearest_size_conditional(array, array_size, width_field, \414height_field, width, height, \415func, context) \416({ \417BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \418sizeof((array)->height_field) != sizeof(u32)); \419(typeof(&(array)[0]))__v4l2_find_nearest_size_conditional( \420(array), array_size, sizeof(*(array)), \421offsetof(typeof(*(array)), width_field), \422offsetof(typeof(*(array)), height_field), \423width, height, func, context); \424})425const void *426__v4l2_find_nearest_size_conditional(const void *array, size_t array_size,427size_t entry_size, size_t width_offset,428size_t height_offset, s32 width,429s32 height,430bool (*func)(const void *array,431size_t index,432const void *context),433const void *context);434435/**436* v4l2_find_nearest_size - Find the nearest size among a discrete set of437* resolutions contained in an array of a driver specific struct438*439* @array: a driver specific array of image sizes440* @array_size: the length of the driver specific array of image sizes441* @width_field: the name of the width field in the driver specific struct442* @height_field: the name of the height field in the driver specific struct443* @width: desired width444* @height: desired height445*446* Finds the closest resolution to minimize the width and height differences447* between what requested and the supported resolutions. The size of the width448* and height fields in the driver specific must equal to that of u32, i.e. four449* bytes.450*451* Returns the best match or NULL if the length of the array is zero.452*/453#define v4l2_find_nearest_size(array, array_size, width_field, \454height_field, width, height) \455v4l2_find_nearest_size_conditional(array, array_size, width_field, \456height_field, width, height, NULL, \457NULL)458459/**460* v4l2_g_parm_cap - helper routine for vidioc_g_parm to fill this in by461* calling the get_frame_interval op of the given subdev. It only works462* for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the463* function name.464*465* @vdev: the struct video_device pointer. Used to determine the device caps.466* @sd: the sub-device pointer.467* @a: the VIDIOC_G_PARM argument.468*/469int v4l2_g_parm_cap(struct video_device *vdev,470struct v4l2_subdev *sd, struct v4l2_streamparm *a);471472/**473* v4l2_s_parm_cap - helper routine for vidioc_s_parm to fill this in by474* calling the set_frame_interval op of the given subdev. It only works475* for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the476* function name.477*478* @vdev: the struct video_device pointer. Used to determine the device caps.479* @sd: the sub-device pointer.480* @a: the VIDIOC_S_PARM argument.481*/482int v4l2_s_parm_cap(struct video_device *vdev,483struct v4l2_subdev *sd, struct v4l2_streamparm *a);484485/* Compare two v4l2_fract structs */486#define V4L2_FRACT_COMPARE(a, OP, b) \487((u64)(a).numerator * (b).denominator OP \488(u64)(b).numerator * (a).denominator)489490/* ------------------------------------------------------------------------- */491492/* Pixel format and FourCC helpers */493494/**495* enum v4l2_pixel_encoding - specifies the pixel encoding value496*497* @V4L2_PIXEL_ENC_UNKNOWN: Pixel encoding is unknown/un-initialized498* @V4L2_PIXEL_ENC_YUV: Pixel encoding is YUV499* @V4L2_PIXEL_ENC_RGB: Pixel encoding is RGB500* @V4L2_PIXEL_ENC_BAYER: Pixel encoding is Bayer501*/502enum v4l2_pixel_encoding {503V4L2_PIXEL_ENC_UNKNOWN = 0,504V4L2_PIXEL_ENC_YUV = 1,505V4L2_PIXEL_ENC_RGB = 2,506V4L2_PIXEL_ENC_BAYER = 3,507};508509/**510* struct v4l2_format_info - information about a V4L2 format511* @format: 4CC format identifier (V4L2_PIX_FMT_*)512* @pixel_enc: Pixel encoding (see enum v4l2_pixel_encoding above)513* @mem_planes: Number of memory planes, which includes the alpha plane (1 to 4).514* @comp_planes: Number of component planes, which includes the alpha plane (1 to 4).515* @bpp: Array of per-plane bytes per pixel516* @bpp_div: Array of per-plane bytes per pixel divisors to support fractional pixel sizes.517* @hdiv: Horizontal chroma subsampling factor518* @vdiv: Vertical chroma subsampling factor519* @block_w: Per-plane macroblock pixel width (optional)520* @block_h: Per-plane macroblock pixel height (optional)521*/522struct v4l2_format_info {523u32 format;524u8 pixel_enc;525u8 mem_planes;526u8 comp_planes;527u8 bpp[4];528u8 bpp_div[4];529u8 hdiv;530u8 vdiv;531u8 block_w[4];532u8 block_h[4];533};534535static inline bool v4l2_is_format_rgb(const struct v4l2_format_info *f)536{537return f && f->pixel_enc == V4L2_PIXEL_ENC_RGB;538}539540static inline bool v4l2_is_format_yuv(const struct v4l2_format_info *f)541{542return f && f->pixel_enc == V4L2_PIXEL_ENC_YUV;543}544545static inline bool v4l2_is_format_bayer(const struct v4l2_format_info *f)546{547return f && f->pixel_enc == V4L2_PIXEL_ENC_BAYER;548}549550const struct v4l2_format_info *v4l2_format_info(u32 format);551void v4l2_apply_frmsize_constraints(u32 *width, u32 *height,552const struct v4l2_frmsize_stepwise *frmsize);553int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,554u32 width, u32 height);555int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,556u32 width, u32 height);557558/**559* v4l2_get_link_freq - Get link rate from transmitter560*561* @pad: The transmitter's media pad (or control handler for non-MC users or562* compatibility reasons, don't use in new code)563* @mul: The multiplier between pixel rate and link frequency. Bits per pixel on564* D-PHY, samples per clock on parallel. 0 otherwise.565* @div: The divisor between pixel rate and link frequency. Number of data lanes566* times two on D-PHY, 1 on parallel. 0 otherwise.567*568* This function is intended for obtaining the link frequency from the569* transmitter sub-devices. It returns the link rate, either from the570* V4L2_CID_LINK_FREQ control implemented by the transmitter, or value571* calculated based on the V4L2_CID_PIXEL_RATE implemented by the transmitter.572*573* Return:574* * >0: Link frequency575* * %-ENOENT: Link frequency or pixel rate control not found576* * %-EINVAL: Invalid link frequency value577*/578#ifdef CONFIG_MEDIA_CONTROLLER579#define v4l2_get_link_freq(pad, mul, div) \580_Generic(pad, \581struct media_pad *: __v4l2_get_link_freq_pad, \582struct v4l2_ctrl_handler *: __v4l2_get_link_freq_ctrl) \583(pad, mul, div)584s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,585unsigned int div);586#else587#define v4l2_get_link_freq(handler, mul, div) \588__v4l2_get_link_freq_ctrl(handler, mul, div)589#endif590s64 __v4l2_get_link_freq_ctrl(struct v4l2_ctrl_handler *handler,591unsigned int mul, unsigned int div);592593void v4l2_simplify_fraction(u32 *numerator, u32 *denominator,594unsigned int n_terms, unsigned int threshold);595u32 v4l2_fraction_to_interval(u32 numerator, u32 denominator);596597/**598* v4l2_link_freq_to_bitmap - Figure out platform-supported link frequencies599* @dev: The struct device600* @fw_link_freqs: Array of link frequencies from firmware601* @num_of_fw_link_freqs: Number of entries in @fw_link_freqs602* @driver_link_freqs: Array of link frequencies supported by the driver603* @num_of_driver_link_freqs: Number of entries in @driver_link_freqs604* @bitmap: Bitmap of driver-supported link frequencies found in @fw_link_freqs605*606* This function checks which driver-supported link frequencies are enabled in607* system firmware and sets the corresponding bits in @bitmap (after first608* zeroing it).609*610* Return:611* * %0: Success612* * %-ENOENT: No match found between driver-supported link frequencies and613* those available in firmware.614* * %-ENODATA: No link frequencies were specified in firmware.615*/616int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,617unsigned int num_of_fw_link_freqs,618const s64 *driver_link_freqs,619unsigned int num_of_driver_link_freqs,620unsigned long *bitmap);621622static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf)623{624/*625* When the timestamp comes from 32-bit user space, there may be626* uninitialized data in tv_usec, so cast it to u32.627* Otherwise allow invalid input for backwards compatibility.628*/629return buf->timestamp.tv_sec * NSEC_PER_SEC +630(u32)buf->timestamp.tv_usec * NSEC_PER_USEC;631}632633static inline void v4l2_buffer_set_timestamp(struct v4l2_buffer *buf,634u64 timestamp)635{636struct timespec64 ts = ns_to_timespec64(timestamp);637638buf->timestamp.tv_sec = ts.tv_sec;639buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;640}641642static inline bool v4l2_is_colorspace_valid(__u32 colorspace)643{644return colorspace > V4L2_COLORSPACE_DEFAULT &&645colorspace < V4L2_COLORSPACE_LAST;646}647648static inline bool v4l2_is_xfer_func_valid(__u32 xfer_func)649{650return xfer_func > V4L2_XFER_FUNC_DEFAULT &&651xfer_func < V4L2_XFER_FUNC_LAST;652}653654static inline bool v4l2_is_ycbcr_enc_valid(__u8 ycbcr_enc)655{656return ycbcr_enc > V4L2_YCBCR_ENC_DEFAULT &&657ycbcr_enc < V4L2_YCBCR_ENC_LAST;658}659660static inline bool v4l2_is_hsv_enc_valid(__u8 hsv_enc)661{662return hsv_enc == V4L2_HSV_ENC_180 || hsv_enc == V4L2_HSV_ENC_256;663}664665static inline bool v4l2_is_quant_valid(__u8 quantization)666{667return quantization == V4L2_QUANTIZATION_FULL_RANGE ||668quantization == V4L2_QUANTIZATION_LIM_RANGE;669}670671#endif /* V4L2_COMMON_H_ */672673674