Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003     V4L2 device support header.
0004 
0005     Copyright (C) 2008  Hans Verkuil <hverkuil@xs4all.nl>
0006 
0007  */
0008 
0009 #ifndef _V4L2_DEVICE_H
0010 #define _V4L2_DEVICE_H
0011 
0012 #include <media/media-device.h>
0013 #include <media/v4l2-subdev.h>
0014 #include <media/v4l2-dev.h>
0015 
0016 #define V4L2_DEVICE_NAME_SIZE (20 + 16)
0017 
0018 struct v4l2_ctrl_handler;
0019 
0020 /**
0021  * struct v4l2_device - main struct to for V4L2 device drivers
0022  *
0023  * @dev: pointer to struct device.
0024  * @mdev: pointer to struct media_device, may be NULL.
0025  * @subdevs: used to keep track of the registered subdevs
0026  * @lock: lock this struct; can be used by the driver as well
0027  *  if this struct is embedded into a larger struct.
0028  * @name: unique device name, by default the driver name + bus ID
0029  * @notify: notify operation called by some sub-devices.
0030  * @ctrl_handler: The control handler. May be %NULL.
0031  * @prio: Device's priority state
0032  * @ref: Keep track of the references to this struct.
0033  * @release: Release function that is called when the ref count
0034  *  goes to 0.
0035  *
0036  * Each instance of a V4L2 device should create the v4l2_device struct,
0037  * either stand-alone or embedded in a larger struct.
0038  *
0039  * It allows easy access to sub-devices (see v4l2-subdev.h) and provides
0040  * basic V4L2 device-level support.
0041  *
0042  * .. note::
0043  *
0044  *    #) @dev->driver_data points to this struct.
0045  *    #) @dev might be %NULL if there is no parent device
0046  */
0047 struct v4l2_device {
0048     struct device *dev;
0049     struct media_device *mdev;
0050     struct list_head subdevs;
0051     spinlock_t lock;
0052     char name[V4L2_DEVICE_NAME_SIZE];
0053     void (*notify)(struct v4l2_subdev *sd,
0054             unsigned int notification, void *arg);
0055     struct v4l2_ctrl_handler *ctrl_handler;
0056     struct v4l2_prio_state prio;
0057     struct kref ref;
0058     void (*release)(struct v4l2_device *v4l2_dev);
0059 };
0060 
0061 /**
0062  * v4l2_device_get - gets a V4L2 device reference
0063  *
0064  * @v4l2_dev: pointer to struct &v4l2_device
0065  *
0066  * This is an ancillary routine meant to increment the usage for the
0067  * struct &v4l2_device pointed by @v4l2_dev.
0068  */
0069 static inline void v4l2_device_get(struct v4l2_device *v4l2_dev)
0070 {
0071     kref_get(&v4l2_dev->ref);
0072 }
0073 
0074 /**
0075  * v4l2_device_put - puts a V4L2 device reference
0076  *
0077  * @v4l2_dev: pointer to struct &v4l2_device
0078  *
0079  * This is an ancillary routine meant to decrement the usage for the
0080  * struct &v4l2_device pointed by @v4l2_dev.
0081  */
0082 int v4l2_device_put(struct v4l2_device *v4l2_dev);
0083 
0084 /**
0085  * v4l2_device_register - Initialize v4l2_dev and make @dev->driver_data
0086  *  point to @v4l2_dev.
0087  *
0088  * @dev: pointer to struct &device
0089  * @v4l2_dev: pointer to struct &v4l2_device
0090  *
0091  * .. note::
0092  *  @dev may be %NULL in rare cases (ISA devices).
0093  *  In such case the caller must fill in the @v4l2_dev->name field
0094  *  before calling this function.
0095  */
0096 int __must_check v4l2_device_register(struct device *dev,
0097                       struct v4l2_device *v4l2_dev);
0098 
0099 /**
0100  * v4l2_device_set_name - Optional function to initialize the
0101  *  name field of struct &v4l2_device
0102  *
0103  * @v4l2_dev: pointer to struct &v4l2_device
0104  * @basename: base name for the device name
0105  * @instance: pointer to a static atomic_t var with the instance usage for
0106  *  the device driver.
0107  *
0108  * v4l2_device_set_name() initializes the name field of struct &v4l2_device
0109  * using the driver name and a driver-global atomic_t instance.
0110  *
0111  * This function will increment the instance counter and returns the
0112  * instance value used in the name.
0113  *
0114  * Example:
0115  *
0116  *   static atomic_t drv_instance = ATOMIC_INIT(0);
0117  *
0118  *   ...
0119  *
0120  *   instance = v4l2_device_set_name(&\ v4l2_dev, "foo", &\ drv_instance);
0121  *
0122  * The first time this is called the name field will be set to foo0 and
0123  * this function returns 0. If the name ends with a digit (e.g. cx18),
0124  * then the name will be set to cx18-0 since cx180 would look really odd.
0125  */
0126 int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
0127              atomic_t *instance);
0128 
0129 /**
0130  * v4l2_device_disconnect - Change V4L2 device state to disconnected.
0131  *
0132  * @v4l2_dev: pointer to struct v4l2_device
0133  *
0134  * Should be called when the USB parent disconnects.
0135  * Since the parent disappears, this ensures that @v4l2_dev doesn't have
0136  * an invalid parent pointer.
0137  *
0138  * .. note:: This function sets @v4l2_dev->dev to NULL.
0139  */
0140 void v4l2_device_disconnect(struct v4l2_device *v4l2_dev);
0141 
0142 /**
0143  *  v4l2_device_unregister - Unregister all sub-devices and any other
0144  *   resources related to @v4l2_dev.
0145  *
0146  * @v4l2_dev: pointer to struct v4l2_device
0147  */
0148 void v4l2_device_unregister(struct v4l2_device *v4l2_dev);
0149 
0150 /**
0151  * v4l2_device_register_subdev - Registers a subdev with a v4l2 device.
0152  *
0153  * @v4l2_dev: pointer to struct &v4l2_device
0154  * @sd: pointer to &struct v4l2_subdev
0155  *
0156  * While registered, the subdev module is marked as in-use.
0157  *
0158  * An error is returned if the module is no longer loaded on any attempts
0159  * to register it.
0160  */
0161 int __must_check v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
0162                          struct v4l2_subdev *sd);
0163 
0164 /**
0165  * v4l2_device_unregister_subdev - Unregisters a subdev with a v4l2 device.
0166  *
0167  * @sd: pointer to &struct v4l2_subdev
0168  *
0169  * .. note ::
0170  *
0171  *  Can also be called if the subdev wasn't registered. In such
0172  *  case, it will do nothing.
0173  */
0174 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
0175 
0176 /**
0177  * __v4l2_device_register_subdev_nodes - Registers device nodes for
0178  *      all subdevs of the v4l2 device that are marked with the
0179  *      %V4L2_SUBDEV_FL_HAS_DEVNODE flag.
0180  *
0181  * @v4l2_dev: pointer to struct v4l2_device
0182  * @read_only: subdevices read-only flag. True to register the subdevices
0183  *  device nodes in read-only mode, false to allow full access to the
0184  *  subdevice userspace API.
0185  */
0186 int __must_check
0187 __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
0188                     bool read_only);
0189 
0190 /**
0191  * v4l2_device_register_subdev_nodes - Registers subdevices device nodes with
0192  *  unrestricted access to the subdevice userspace operations
0193  *
0194  * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
0195  * for more details.
0196  *
0197  * @v4l2_dev: pointer to struct v4l2_device
0198  */
0199 static inline int __must_check
0200 v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
0201 {
0202 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
0203     return __v4l2_device_register_subdev_nodes(v4l2_dev, false);
0204 #else
0205     return 0;
0206 #endif
0207 }
0208 
0209 /**
0210  * v4l2_device_register_ro_subdev_nodes - Registers subdevices device nodes
0211  *  in read-only mode
0212  *
0213  * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
0214  * for more details.
0215  *
0216  * @v4l2_dev: pointer to struct v4l2_device
0217  */
0218 static inline int __must_check
0219 v4l2_device_register_ro_subdev_nodes(struct v4l2_device *v4l2_dev)
0220 {
0221 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
0222     return __v4l2_device_register_subdev_nodes(v4l2_dev, true);
0223 #else
0224     return 0;
0225 #endif
0226 }
0227 
0228 /**
0229  * v4l2_subdev_notify - Sends a notification to v4l2_device.
0230  *
0231  * @sd: pointer to &struct v4l2_subdev
0232  * @notification: type of notification. Please notice that the notification
0233  *  type is driver-specific.
0234  * @arg: arguments for the notification. Those are specific to each
0235  *  notification type.
0236  */
0237 static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,
0238                       unsigned int notification, void *arg)
0239 {
0240     if (sd && sd->v4l2_dev && sd->v4l2_dev->notify)
0241         sd->v4l2_dev->notify(sd, notification, arg);
0242 }
0243 
0244 /**
0245  * v4l2_device_supports_requests - Test if requests are supported.
0246  *
0247  * @v4l2_dev: pointer to struct v4l2_device
0248  */
0249 static inline bool v4l2_device_supports_requests(struct v4l2_device *v4l2_dev)
0250 {
0251     return v4l2_dev->mdev && v4l2_dev->mdev->ops &&
0252            v4l2_dev->mdev->ops->req_queue;
0253 }
0254 
0255 /* Helper macros to iterate over all subdevs. */
0256 
0257 /**
0258  * v4l2_device_for_each_subdev - Helper macro that interates over all
0259  *  sub-devices of a given &v4l2_device.
0260  *
0261  * @sd: pointer that will be filled by the macro with all
0262  *  &struct v4l2_subdev pointer used as an iterator by the loop.
0263  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0264  *
0265  * This macro iterates over all sub-devices owned by the @v4l2_dev device.
0266  * It acts as a for loop iterator and executes the next statement with
0267  * the @sd variable pointing to each sub-device in turn.
0268  */
0269 #define v4l2_device_for_each_subdev(sd, v4l2_dev)           \
0270     list_for_each_entry(sd, &(v4l2_dev)->subdevs, list)
0271 
0272 /**
0273  * __v4l2_device_call_subdevs_p - Calls the specified operation for
0274  *  all subdevs matching the condition.
0275  *
0276  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0277  * @sd: pointer that will be filled by the macro with all
0278  *  &struct v4l2_subdev pointer used as an iterator by the loop.
0279  * @cond: condition to be match
0280  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0281  *     Each element there groups a set of operations functions.
0282  * @f: operation function that will be called if @cond matches.
0283  *  The operation functions are defined in groups, according to
0284  *  each element at &struct v4l2_subdev_ops.
0285  * @args: arguments for @f.
0286  *
0287  * Ignore any errors.
0288  *
0289  * Note: subdevs cannot be added or deleted while walking
0290  * the subdevs list.
0291  */
0292 #define __v4l2_device_call_subdevs_p(v4l2_dev, sd, cond, o, f, args...) \
0293     do {                                \
0294         list_for_each_entry((sd), &(v4l2_dev)->subdevs, list)   \
0295             if ((cond) && (sd)->ops->o && (sd)->ops->o->f)  \
0296                 (sd)->ops->o->f((sd) , ##args);     \
0297     } while (0)
0298 
0299 /**
0300  * __v4l2_device_call_subdevs - Calls the specified operation for
0301  *  all subdevs matching the condition.
0302  *
0303  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0304  * @cond: condition to be match
0305  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0306  *     Each element there groups a set of operations functions.
0307  * @f: operation function that will be called if @cond matches.
0308  *  The operation functions are defined in groups, according to
0309  *  each element at &struct v4l2_subdev_ops.
0310  * @args: arguments for @f.
0311  *
0312  * Ignore any errors.
0313  *
0314  * Note: subdevs cannot be added or deleted while walking
0315  * the subdevs list.
0316  */
0317 #define __v4l2_device_call_subdevs(v4l2_dev, cond, o, f, args...)   \
0318     do {                                \
0319         struct v4l2_subdev *__sd;               \
0320                                     \
0321         __v4l2_device_call_subdevs_p(v4l2_dev, __sd, cond, o,   \
0322                         f , ##args);        \
0323     } while (0)
0324 
0325 /**
0326  * __v4l2_device_call_subdevs_until_err_p - Calls the specified operation for
0327  *  all subdevs matching the condition.
0328  *
0329  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0330  * @sd: pointer that will be filled by the macro with all
0331  *  &struct v4l2_subdev sub-devices associated with @v4l2_dev.
0332  * @cond: condition to be match
0333  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0334  *     Each element there groups a set of operations functions.
0335  * @f: operation function that will be called if @cond matches.
0336  *  The operation functions are defined in groups, according to
0337  *  each element at &struct v4l2_subdev_ops.
0338  * @args: arguments for @f.
0339  *
0340  * Return:
0341  *
0342  * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
0343  * for any subdevice, then abort and return with that error code, zero
0344  * otherwise.
0345  *
0346  * Note: subdevs cannot be added or deleted while walking
0347  * the subdevs list.
0348  */
0349 #define __v4l2_device_call_subdevs_until_err_p(v4l2_dev, sd, cond, o, f, args...) \
0350 ({                                  \
0351     long __err = 0;                         \
0352                                     \
0353     list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) {     \
0354         if ((cond) && (sd)->ops->o && (sd)->ops->o->f)      \
0355             __err = (sd)->ops->o->f((sd) , ##args);     \
0356         if (__err && __err != -ENOIOCTLCMD)         \
0357             break;                      \
0358     }                               \
0359     (__err == -ENOIOCTLCMD) ? 0 : __err;                \
0360 })
0361 
0362 /**
0363  * __v4l2_device_call_subdevs_until_err - Calls the specified operation for
0364  *  all subdevs matching the condition.
0365  *
0366  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0367  * @cond: condition to be match
0368  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0369  *     Each element there groups a set of operations functions.
0370  * @f: operation function that will be called if @cond matches.
0371  *  The operation functions are defined in groups, according to
0372  *  each element at &struct v4l2_subdev_ops.
0373  * @args: arguments for @f.
0374  *
0375  * Return:
0376  *
0377  * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
0378  * for any subdevice, then abort and return with that error code,
0379  * zero otherwise.
0380  *
0381  * Note: subdevs cannot be added or deleted while walking
0382  * the subdevs list.
0383  */
0384 #define __v4l2_device_call_subdevs_until_err(v4l2_dev, cond, o, f, args...) \
0385 ({                                  \
0386     struct v4l2_subdev *__sd;                   \
0387     __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, cond, o, \
0388                         f , ##args);        \
0389 })
0390 
0391 /**
0392  * v4l2_device_call_all - Calls the specified operation for
0393  *  all subdevs matching the &v4l2_subdev.grp_id, as assigned
0394  *  by the bridge driver.
0395  *
0396  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0397  * @grpid: &struct v4l2_subdev->grp_id group ID to match.
0398  *      Use 0 to match them all.
0399  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0400  *     Each element there groups a set of operations functions.
0401  * @f: operation function that will be called if @cond matches.
0402  *  The operation functions are defined in groups, according to
0403  *  each element at &struct v4l2_subdev_ops.
0404  * @args: arguments for @f.
0405  *
0406  * Ignore any errors.
0407  *
0408  * Note: subdevs cannot be added or deleted while walking
0409  * the subdevs list.
0410  */
0411 #define v4l2_device_call_all(v4l2_dev, grpid, o, f, args...)        \
0412     do {                                \
0413         struct v4l2_subdev *__sd;               \
0414                                     \
0415         __v4l2_device_call_subdevs_p(v4l2_dev, __sd,        \
0416             (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
0417             ##args);                    \
0418     } while (0)
0419 
0420 /**
0421  * v4l2_device_call_until_err - Calls the specified operation for
0422  *  all subdevs matching the &v4l2_subdev.grp_id, as assigned
0423  *  by the bridge driver, until an error occurs.
0424  *
0425  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0426  * @grpid: &struct v4l2_subdev->grp_id group ID to match.
0427  *     Use 0 to match them all.
0428  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0429  *     Each element there groups a set of operations functions.
0430  * @f: operation function that will be called if @cond matches.
0431  *  The operation functions are defined in groups, according to
0432  *  each element at &struct v4l2_subdev_ops.
0433  * @args: arguments for @f.
0434  *
0435  * Return:
0436  *
0437  * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
0438  * for any subdevice, then abort and return with that error code,
0439  * zero otherwise.
0440  *
0441  * Note: subdevs cannot be added or deleted while walking
0442  * the subdevs list.
0443  */
0444 #define v4l2_device_call_until_err(v4l2_dev, grpid, o, f, args...)  \
0445 ({                                  \
0446     struct v4l2_subdev *__sd;                   \
0447     __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd,      \
0448             (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
0449             ##args);                    \
0450 })
0451 
0452 /**
0453  * v4l2_device_mask_call_all - Calls the specified operation for
0454  *  all subdevices where a group ID matches a specified bitmask.
0455  *
0456  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0457  * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
0458  *      group ID to be matched. Use 0 to match them all.
0459  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0460  *     Each element there groups a set of operations functions.
0461  * @f: operation function that will be called if @cond matches.
0462  *  The operation functions are defined in groups, according to
0463  *  each element at &struct v4l2_subdev_ops.
0464  * @args: arguments for @f.
0465  *
0466  * Ignore any errors.
0467  *
0468  * Note: subdevs cannot be added or deleted while walking
0469  * the subdevs list.
0470  */
0471 #define v4l2_device_mask_call_all(v4l2_dev, grpmsk, o, f, args...)  \
0472     do {                                \
0473         struct v4l2_subdev *__sd;               \
0474                                     \
0475         __v4l2_device_call_subdevs_p(v4l2_dev, __sd,        \
0476             (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o,  \
0477             f , ##args);                    \
0478     } while (0)
0479 
0480 /**
0481  * v4l2_device_mask_call_until_err - Calls the specified operation for
0482  *  all subdevices where a group ID matches a specified bitmask.
0483  *
0484  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0485  * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
0486  *      group ID to be matched. Use 0 to match them all.
0487  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0488  *     Each element there groups a set of operations functions.
0489  * @f: operation function that will be called if @cond matches.
0490  *  The operation functions are defined in groups, according to
0491  *  each element at &struct v4l2_subdev_ops.
0492  * @args: arguments for @f.
0493  *
0494  * Return:
0495  *
0496  * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
0497  * for any subdevice, then abort and return with that error code,
0498  * zero otherwise.
0499  *
0500  * Note: subdevs cannot be added or deleted while walking
0501  * the subdevs list.
0502  */
0503 #define v4l2_device_mask_call_until_err(v4l2_dev, grpmsk, o, f, args...) \
0504 ({                                  \
0505     struct v4l2_subdev *__sd;                   \
0506     __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd,      \
0507             (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o,  \
0508             f , ##args);                    \
0509 })
0510 
0511 
0512 /**
0513  * v4l2_device_has_op - checks if any subdev with matching grpid has a
0514  *  given ops.
0515  *
0516  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0517  * @grpid: &struct v4l2_subdev->grp_id group ID to match.
0518  *     Use 0 to match them all.
0519  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0520  *     Each element there groups a set of operations functions.
0521  * @f: operation function that will be called if @cond matches.
0522  *  The operation functions are defined in groups, according to
0523  *  each element at &struct v4l2_subdev_ops.
0524  */
0525 #define v4l2_device_has_op(v4l2_dev, grpid, o, f)           \
0526 ({                                  \
0527     struct v4l2_subdev *__sd;                   \
0528     bool __result = false;                      \
0529     list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) {     \
0530         if ((grpid) && __sd->grp_id != (grpid))         \
0531             continue;                   \
0532         if (v4l2_subdev_has_op(__sd, o, f)) {           \
0533             __result = true;                \
0534             break;                      \
0535         }                           \
0536     }                               \
0537     __result;                           \
0538 })
0539 
0540 /**
0541  * v4l2_device_mask_has_op - checks if any subdev with matching group
0542  *  mask has a given ops.
0543  *
0544  * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
0545  * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
0546  *      group ID to be matched. Use 0 to match them all.
0547  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
0548  *     Each element there groups a set of operations functions.
0549  * @f: operation function that will be called if @cond matches.
0550  *  The operation functions are defined in groups, according to
0551  *  each element at &struct v4l2_subdev_ops.
0552  */
0553 #define v4l2_device_mask_has_op(v4l2_dev, grpmsk, o, f)         \
0554 ({                                  \
0555     struct v4l2_subdev *__sd;                   \
0556     bool __result = false;                      \
0557     list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) {     \
0558         if ((grpmsk) && !(__sd->grp_id & (grpmsk)))     \
0559             continue;                   \
0560         if (v4l2_subdev_has_op(__sd, o, f)) {           \
0561             __result = true;                \
0562             break;                      \
0563         }                           \
0564     }                               \
0565     __result;                           \
0566 })
0567 
0568 #endif