/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2V4L2 device support header.34Copyright (C) 2008 Hans Verkuil <[email protected]>56*/78#ifndef _V4L2_DEVICE_H9#define _V4L2_DEVICE_H1011#include <media/media-device.h>12#include <media/v4l2-subdev.h>13#include <media/v4l2-dev.h>1415struct v4l2_ctrl_handler;1617/**18* struct v4l2_device - main struct to for V4L2 device drivers19*20* @dev: pointer to struct device.21* @mdev: pointer to struct media_device, may be NULL.22* @subdevs: used to keep track of the registered subdevs23* @lock: lock this struct; can be used by the driver as well24* if this struct is embedded into a larger struct.25* @name: unique device name, by default the driver name + bus ID26* @notify: notify operation called by some sub-devices.27* @ctrl_handler: The control handler. May be %NULL.28* @prio: Device's priority state29* @ref: Keep track of the references to this struct.30* @release: Release function that is called when the ref count31* goes to 0.32*33* Each instance of a V4L2 device should create the v4l2_device struct,34* either stand-alone or embedded in a larger struct.35*36* It allows easy access to sub-devices (see v4l2-subdev.h) and provides37* basic V4L2 device-level support.38*39* .. note::40*41* #) @dev->driver_data points to this struct.42* #) @dev might be %NULL if there is no parent device43*/44struct v4l2_device {45struct device *dev;46struct media_device *mdev;47struct list_head subdevs;48spinlock_t lock;49char name[36];50void (*notify)(struct v4l2_subdev *sd,51unsigned int notification, void *arg);52struct v4l2_ctrl_handler *ctrl_handler;53struct v4l2_prio_state prio;54struct kref ref;55void (*release)(struct v4l2_device *v4l2_dev);56};5758/**59* v4l2_device_get - gets a V4L2 device reference60*61* @v4l2_dev: pointer to struct &v4l2_device62*63* This is an ancillary routine meant to increment the usage for the64* struct &v4l2_device pointed by @v4l2_dev.65*/66static inline void v4l2_device_get(struct v4l2_device *v4l2_dev)67{68kref_get(&v4l2_dev->ref);69}7071/**72* v4l2_device_put - puts a V4L2 device reference73*74* @v4l2_dev: pointer to struct &v4l2_device75*76* This is an ancillary routine meant to decrement the usage for the77* struct &v4l2_device pointed by @v4l2_dev.78*/79int v4l2_device_put(struct v4l2_device *v4l2_dev);8081/**82* v4l2_device_register - Initialize v4l2_dev and make @dev->driver_data83* point to @v4l2_dev.84*85* @dev: pointer to struct &device86* @v4l2_dev: pointer to struct &v4l2_device87*88* .. note::89* @dev may be %NULL in rare cases (ISA devices).90* In such case the caller must fill in the @v4l2_dev->name field91* before calling this function.92*/93int __must_check v4l2_device_register(struct device *dev,94struct v4l2_device *v4l2_dev);9596/**97* v4l2_device_set_name - Optional function to initialize the98* name field of struct &v4l2_device99*100* @v4l2_dev: pointer to struct &v4l2_device101* @basename: base name for the device name102* @instance: pointer to a static atomic_t var with the instance usage for103* the device driver.104*105* v4l2_device_set_name() initializes the name field of struct &v4l2_device106* using the driver name and a driver-global atomic_t instance.107*108* This function will increment the instance counter and returns the109* instance value used in the name.110*111* Example:112*113* static atomic_t drv_instance = ATOMIC_INIT(0);114*115* ...116*117* instance = v4l2_device_set_name(&\ v4l2_dev, "foo", &\ drv_instance);118*119* The first time this is called the name field will be set to foo0 and120* this function returns 0. If the name ends with a digit (e.g. cx18),121* then the name will be set to cx18-0 since cx180 would look really odd.122*/123int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,124atomic_t *instance);125126/**127* v4l2_device_disconnect - Change V4L2 device state to disconnected.128*129* @v4l2_dev: pointer to struct v4l2_device130*131* Should be called when the USB parent disconnects.132* Since the parent disappears, this ensures that @v4l2_dev doesn't have133* an invalid parent pointer.134*135* .. note:: This function sets @v4l2_dev->dev to NULL.136*/137void v4l2_device_disconnect(struct v4l2_device *v4l2_dev);138139/**140* v4l2_device_unregister - Unregister all sub-devices and any other141* resources related to @v4l2_dev.142*143* @v4l2_dev: pointer to struct v4l2_device144*/145void v4l2_device_unregister(struct v4l2_device *v4l2_dev);146147/**148* v4l2_device_register_subdev - Registers a subdev with a v4l2 device.149*150* @v4l2_dev: pointer to struct &v4l2_device151* @sd: pointer to &struct v4l2_subdev152*153* While registered, the subdev module is marked as in-use.154*155* An error is returned if the module is no longer loaded on any attempts156* to register it.157*/158#define v4l2_device_register_subdev(v4l2_dev, sd) \159__v4l2_device_register_subdev(v4l2_dev, sd, THIS_MODULE)160int __must_check __v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,161struct v4l2_subdev *sd,162struct module *module);163164/**165* v4l2_device_unregister_subdev - Unregisters a subdev with a v4l2 device.166*167* @sd: pointer to &struct v4l2_subdev168*169* .. note ::170*171* Can also be called if the subdev wasn't registered. In such172* case, it will do nothing.173*/174void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);175176/**177* __v4l2_device_register_subdev_nodes - Registers device nodes for178* all subdevs of the v4l2 device that are marked with the179* %V4L2_SUBDEV_FL_HAS_DEVNODE flag.180*181* @v4l2_dev: pointer to struct v4l2_device182* @read_only: subdevices read-only flag. True to register the subdevices183* device nodes in read-only mode, false to allow full access to the184* subdevice userspace API.185*/186int __must_check187__v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,188bool read_only);189190/**191* v4l2_device_register_subdev_nodes - Registers subdevices device nodes with192* unrestricted access to the subdevice userspace operations193*194* Internally calls __v4l2_device_register_subdev_nodes(). See its documentation195* for more details.196*197* @v4l2_dev: pointer to struct v4l2_device198*/199static inline int __must_check200v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)201{202#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)203return __v4l2_device_register_subdev_nodes(v4l2_dev, false);204#else205return 0;206#endif207}208209/**210* v4l2_device_register_ro_subdev_nodes - Registers subdevices device nodes211* in read-only mode212*213* Internally calls __v4l2_device_register_subdev_nodes(). See its documentation214* for more details.215*216* @v4l2_dev: pointer to struct v4l2_device217*/218static inline int __must_check219v4l2_device_register_ro_subdev_nodes(struct v4l2_device *v4l2_dev)220{221#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)222return __v4l2_device_register_subdev_nodes(v4l2_dev, true);223#else224return 0;225#endif226}227228/**229* v4l2_subdev_notify - Sends a notification to v4l2_device.230*231* @sd: pointer to &struct v4l2_subdev232* @notification: type of notification. Please notice that the notification233* type is driver-specific.234* @arg: arguments for the notification. Those are specific to each235* notification type.236*/237static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,238unsigned int notification, void *arg)239{240if (sd && sd->v4l2_dev && sd->v4l2_dev->notify)241sd->v4l2_dev->notify(sd, notification, arg);242}243244/**245* v4l2_device_supports_requests - Test if requests are supported.246*247* @v4l2_dev: pointer to struct v4l2_device248*/249static inline bool v4l2_device_supports_requests(struct v4l2_device *v4l2_dev)250{251return v4l2_dev->mdev && v4l2_dev->mdev->ops &&252v4l2_dev->mdev->ops->req_queue;253}254255/* Helper macros to iterate over all subdevs. */256257/**258* v4l2_device_for_each_subdev - Helper macro that interates over all259* sub-devices of a given &v4l2_device.260*261* @sd: pointer that will be filled by the macro with all262* &struct v4l2_subdev pointer used as an iterator by the loop.263* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.264*265* This macro iterates over all sub-devices owned by the @v4l2_dev device.266* It acts as a for loop iterator and executes the next statement with267* the @sd variable pointing to each sub-device in turn.268*/269#define v4l2_device_for_each_subdev(sd, v4l2_dev) \270list_for_each_entry(sd, &(v4l2_dev)->subdevs, list)271272/**273* __v4l2_device_call_subdevs_p - Calls the specified operation for274* all subdevs matching the condition.275*276* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.277* @sd: pointer that will be filled by the macro with all278* &struct v4l2_subdev pointer used as an iterator by the loop.279* @cond: condition to be match280* @o: name of the element at &struct v4l2_subdev_ops that contains @f.281* Each element there groups a set of operations functions.282* @f: operation function that will be called if @cond matches.283* The operation functions are defined in groups, according to284* each element at &struct v4l2_subdev_ops.285* @args: arguments for @f.286*287* Ignore any errors.288*289* Note: subdevs cannot be added or deleted while walking290* the subdevs list.291*/292#define __v4l2_device_call_subdevs_p(v4l2_dev, sd, cond, o, f, args...) \293do { \294list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) \295if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \296(sd)->ops->o->f((sd) , ##args); \297} while (0)298299/**300* __v4l2_device_call_subdevs - Calls the specified operation for301* all subdevs matching the condition.302*303* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.304* @cond: condition to be match305* @o: name of the element at &struct v4l2_subdev_ops that contains @f.306* Each element there groups a set of operations functions.307* @f: operation function that will be called if @cond matches.308* The operation functions are defined in groups, according to309* each element at &struct v4l2_subdev_ops.310* @args: arguments for @f.311*312* Ignore any errors.313*314* Note: subdevs cannot be added or deleted while walking315* the subdevs list.316*/317#define __v4l2_device_call_subdevs(v4l2_dev, cond, o, f, args...) \318do { \319struct v4l2_subdev *__sd; \320\321__v4l2_device_call_subdevs_p(v4l2_dev, __sd, cond, o, \322f , ##args); \323} while (0)324325/**326* __v4l2_device_call_subdevs_until_err_p - Calls the specified operation for327* all subdevs matching the condition.328*329* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.330* @sd: pointer that will be filled by the macro with all331* &struct v4l2_subdev sub-devices associated with @v4l2_dev.332* @cond: condition to be match333* @o: name of the element at &struct v4l2_subdev_ops that contains @f.334* Each element there groups a set of operations functions.335* @f: operation function that will be called if @cond matches.336* The operation functions are defined in groups, according to337* each element at &struct v4l2_subdev_ops.338* @args: arguments for @f.339*340* Return:341*342* If the operation returns an error other than 0 or ``-ENOIOCTLCMD``343* for any subdevice, then abort and return with that error code, zero344* otherwise.345*346* Note: subdevs cannot be added or deleted while walking347* the subdevs list.348*/349#define __v4l2_device_call_subdevs_until_err_p(v4l2_dev, sd, cond, o, f, args...) \350({ \351long __err = 0; \352\353list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) { \354if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \355__err = (sd)->ops->o->f((sd) , ##args); \356if (__err && __err != -ENOIOCTLCMD) \357break; \358} \359(__err == -ENOIOCTLCMD) ? 0 : __err; \360})361362/**363* __v4l2_device_call_subdevs_until_err - Calls the specified operation for364* all subdevs matching the condition.365*366* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.367* @cond: condition to be match368* @o: name of the element at &struct v4l2_subdev_ops that contains @f.369* Each element there groups a set of operations functions.370* @f: operation function that will be called if @cond matches.371* The operation functions are defined in groups, according to372* each element at &struct v4l2_subdev_ops.373* @args: arguments for @f.374*375* Return:376*377* If the operation returns an error other than 0 or ``-ENOIOCTLCMD``378* for any subdevice, then abort and return with that error code,379* zero otherwise.380*381* Note: subdevs cannot be added or deleted while walking382* the subdevs list.383*/384#define __v4l2_device_call_subdevs_until_err(v4l2_dev, cond, o, f, args...) \385({ \386struct v4l2_subdev *__sd; \387__v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, cond, o, \388f , ##args); \389})390391/**392* v4l2_device_call_all - Calls the specified operation for393* all subdevs matching the &v4l2_subdev.grp_id, as assigned394* by the bridge driver.395*396* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.397* @grpid: &struct v4l2_subdev->grp_id group ID to match.398* Use 0 to match them all.399* @o: name of the element at &struct v4l2_subdev_ops that contains @f.400* Each element there groups a set of operations functions.401* @f: operation function that will be called if @cond matches.402* The operation functions are defined in groups, according to403* each element at &struct v4l2_subdev_ops.404* @args: arguments for @f.405*406* Ignore any errors.407*408* Note: subdevs cannot be added or deleted while walking409* the subdevs list.410*/411#define v4l2_device_call_all(v4l2_dev, grpid, o, f, args...) \412do { \413struct v4l2_subdev *__sd; \414\415__v4l2_device_call_subdevs_p(v4l2_dev, __sd, \416(grpid) == 0 || __sd->grp_id == (grpid), o, f , \417##args); \418} while (0)419420/**421* v4l2_device_call_until_err - Calls the specified operation for422* all subdevs matching the &v4l2_subdev.grp_id, as assigned423* by the bridge driver, until an error occurs.424*425* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.426* @grpid: &struct v4l2_subdev->grp_id group ID to match.427* Use 0 to match them all.428* @o: name of the element at &struct v4l2_subdev_ops that contains @f.429* Each element there groups a set of operations functions.430* @f: operation function that will be called if @cond matches.431* The operation functions are defined in groups, according to432* each element at &struct v4l2_subdev_ops.433* @args: arguments for @f.434*435* Return:436*437* If the operation returns an error other than 0 or ``-ENOIOCTLCMD``438* for any subdevice, then abort and return with that error code,439* zero otherwise.440*441* Note: subdevs cannot be added or deleted while walking442* the subdevs list.443*/444#define v4l2_device_call_until_err(v4l2_dev, grpid, o, f, args...) \445({ \446struct v4l2_subdev *__sd; \447__v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \448(grpid) == 0 || __sd->grp_id == (grpid), o, f , \449##args); \450})451452/**453* v4l2_device_mask_call_all - Calls the specified operation for454* all subdevices where a group ID matches a specified bitmask.455*456* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.457* @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id458* group ID to be matched. Use 0 to match them all.459* @o: name of the element at &struct v4l2_subdev_ops that contains @f.460* Each element there groups a set of operations functions.461* @f: operation function that will be called if @cond matches.462* The operation functions are defined in groups, according to463* each element at &struct v4l2_subdev_ops.464* @args: arguments for @f.465*466* Ignore any errors.467*468* Note: subdevs cannot be added or deleted while walking469* the subdevs list.470*/471#define v4l2_device_mask_call_all(v4l2_dev, grpmsk, o, f, args...) \472do { \473struct v4l2_subdev *__sd; \474\475__v4l2_device_call_subdevs_p(v4l2_dev, __sd, \476(grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \477f , ##args); \478} while (0)479480/**481* v4l2_device_mask_call_until_err - Calls the specified operation for482* all subdevices where a group ID matches a specified bitmask.483*484* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.485* @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id486* group ID to be matched. Use 0 to match them all.487* @o: name of the element at &struct v4l2_subdev_ops that contains @f.488* Each element there groups a set of operations functions.489* @f: operation function that will be called if @cond matches.490* The operation functions are defined in groups, according to491* each element at &struct v4l2_subdev_ops.492* @args: arguments for @f.493*494* Return:495*496* If the operation returns an error other than 0 or ``-ENOIOCTLCMD``497* for any subdevice, then abort and return with that error code,498* zero otherwise.499*500* Note: subdevs cannot be added or deleted while walking501* the subdevs list.502*/503#define v4l2_device_mask_call_until_err(v4l2_dev, grpmsk, o, f, args...) \504({ \505struct v4l2_subdev *__sd; \506__v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \507(grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \508f , ##args); \509})510511512/**513* v4l2_device_has_op - checks if any subdev with matching grpid has a514* given ops.515*516* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.517* @grpid: &struct v4l2_subdev->grp_id group ID to match.518* Use 0 to match them all.519* @o: name of the element at &struct v4l2_subdev_ops that contains @f.520* Each element there groups a set of operations functions.521* @f: operation function that will be called if @cond matches.522* The operation functions are defined in groups, according to523* each element at &struct v4l2_subdev_ops.524*/525#define v4l2_device_has_op(v4l2_dev, grpid, o, f) \526({ \527struct v4l2_subdev *__sd; \528bool __result = false; \529list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \530if ((grpid) && __sd->grp_id != (grpid)) \531continue; \532if (v4l2_subdev_has_op(__sd, o, f)) { \533__result = true; \534break; \535} \536} \537__result; \538})539540/**541* v4l2_device_mask_has_op - checks if any subdev with matching group542* mask has a given ops.543*544* @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.545* @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id546* group ID to be matched. Use 0 to match them all.547* @o: name of the element at &struct v4l2_subdev_ops that contains @f.548* Each element there groups a set of operations functions.549* @f: operation function that will be called if @cond matches.550* The operation functions are defined in groups, according to551* each element at &struct v4l2_subdev_ops.552*/553#define v4l2_device_mask_has_op(v4l2_dev, grpmsk, o, f) \554({ \555struct v4l2_subdev *__sd; \556bool __result = false; \557list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \558if ((grpmsk) && !(__sd->grp_id & (grpmsk))) \559continue; \560if (v4l2_subdev_has_op(__sd, o, f)) { \561__result = true; \562break; \563} \564} \565__result; \566})567568#endif569570571