1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 V4L2 device support header.
5 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
10 #define _V4L2_DEVICE_H
12 #include <media/media-device.h>
13 #include <media/v4l2-subdev.h>
14 #include <media/v4l2-dev.h>
16 struct v4l2_ctrl_handler;
19 * struct v4l2_device - main struct to for V4L2 device drivers
21 * @dev: pointer to struct device.
22 * @mdev: pointer to struct media_device, may be NULL.
23 * @subdevs: used to keep track of the registered subdevs
24 * @lock: lock this struct; can be used by the driver as well
25 * if this struct is embedded into a larger struct.
26 * @name: unique device name, by default the driver name + bus ID
27 * @notify: notify operation called by some sub-devices.
28 * @ctrl_handler: The control handler. May be %NULL.
29 * @prio: Device's priority state
30 * @ref: Keep track of the references to this struct.
31 * @release: Release function that is called when the ref count
34 * Each instance of a V4L2 device should create the v4l2_device struct,
35 * either stand-alone or embedded in a larger struct.
37 * It allows easy access to sub-devices (see v4l2-subdev.h) and provides
38 * basic V4L2 device-level support.
42 * #) @dev->driver_data points to this struct.
43 * #) @dev might be %NULL if there is no parent device
47 struct media_device *mdev;
48 struct list_head subdevs;
51 void (*notify)(struct v4l2_subdev *sd,
52 unsigned int notification, void *arg);
53 struct v4l2_ctrl_handler *ctrl_handler;
54 struct v4l2_prio_state prio;
56 void (*release)(struct v4l2_device *v4l2_dev);
60 * v4l2_device_get - gets a V4L2 device reference
62 * @v4l2_dev: pointer to struct &v4l2_device
64 * This is an ancillary routine meant to increment the usage for the
65 * struct &v4l2_device pointed by @v4l2_dev.
67 static inline void v4l2_device_get(struct v4l2_device *v4l2_dev)
69 kref_get(&v4l2_dev->ref);
73 * v4l2_device_put - puts a V4L2 device reference
75 * @v4l2_dev: pointer to struct &v4l2_device
77 * This is an ancillary routine meant to decrement the usage for the
78 * struct &v4l2_device pointed by @v4l2_dev.
80 int v4l2_device_put(struct v4l2_device *v4l2_dev);
83 * v4l2_device_register - Initialize v4l2_dev and make @dev->driver_data
86 * @dev: pointer to struct &device
87 * @v4l2_dev: pointer to struct &v4l2_device
90 * @dev may be %NULL in rare cases (ISA devices).
91 * In such case the caller must fill in the @v4l2_dev->name field
92 * before calling this function.
94 int __must_check v4l2_device_register(struct device *dev,
95 struct v4l2_device *v4l2_dev);
98 * v4l2_device_set_name - Optional function to initialize the
99 * name field of struct &v4l2_device
101 * @v4l2_dev: pointer to struct &v4l2_device
102 * @basename: base name for the device name
103 * @instance: pointer to a static atomic_t var with the instance usage for
106 * v4l2_device_set_name() initializes the name field of struct &v4l2_device
107 * using the driver name and a driver-global atomic_t instance.
109 * This function will increment the instance counter and returns the
110 * instance value used in the name.
114 * static atomic_t drv_instance = ATOMIC_INIT(0);
118 * instance = v4l2_device_set_name(&\ v4l2_dev, "foo", &\ drv_instance);
120 * The first time this is called the name field will be set to foo0 and
121 * this function returns 0. If the name ends with a digit (e.g. cx18),
122 * then the name will be set to cx18-0 since cx180 would look really odd.
124 int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
128 * v4l2_device_disconnect - Change V4L2 device state to disconnected.
130 * @v4l2_dev: pointer to struct v4l2_device
132 * Should be called when the USB parent disconnects.
133 * Since the parent disappears, this ensures that @v4l2_dev doesn't have
134 * an invalid parent pointer.
136 * .. note:: This function sets @v4l2_dev->dev to NULL.
138 void v4l2_device_disconnect(struct v4l2_device *v4l2_dev);
141 * v4l2_device_unregister - Unregister all sub-devices and any other
142 * resources related to @v4l2_dev.
144 * @v4l2_dev: pointer to struct v4l2_device
146 void v4l2_device_unregister(struct v4l2_device *v4l2_dev);
149 * v4l2_device_register_subdev - Registers a subdev with a v4l2 device.
151 * @v4l2_dev: pointer to struct &v4l2_device
152 * @sd: pointer to &struct v4l2_subdev
154 * While registered, the subdev module is marked as in-use.
156 * An error is returned if the module is no longer loaded on any attempts
159 int __must_check v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
160 struct v4l2_subdev *sd);
163 * v4l2_device_unregister_subdev - Unregisters a subdev with a v4l2 device.
165 * @sd: pointer to &struct v4l2_subdev
169 * Can also be called if the subdev wasn't registered. In such
170 * case, it will do nothing.
172 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
175 * __v4l2_device_register_subdev_nodes - Registers device nodes for
176 * all subdevs of the v4l2 device that are marked with the
177 * %V4L2_SUBDEV_FL_HAS_DEVNODE flag.
179 * @v4l2_dev: pointer to struct v4l2_device
180 * @read_only: subdevices read-only flag. True to register the subdevices
181 * device nodes in read-only mode, false to allow full access to the
182 * subdevice userspace API.
185 __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
189 * v4l2_device_register_subdev_nodes - Registers subdevices device nodes with
190 * unrestricted access to the subdevice userspace operations
192 * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
195 * @v4l2_dev: pointer to struct v4l2_device
197 static inline int __must_check
198 v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
200 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
201 return __v4l2_device_register_subdev_nodes(v4l2_dev, false);
208 * v4l2_device_register_ro_subdev_nodes - Registers subdevices device nodes
211 * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
214 * @v4l2_dev: pointer to struct v4l2_device
216 static inline int __must_check
217 v4l2_device_register_ro_subdev_nodes(struct v4l2_device *v4l2_dev)
219 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
220 return __v4l2_device_register_subdev_nodes(v4l2_dev, true);
227 * v4l2_subdev_notify - Sends a notification to v4l2_device.
229 * @sd: pointer to &struct v4l2_subdev
230 * @notification: type of notification. Please notice that the notification
231 * type is driver-specific.
232 * @arg: arguments for the notification. Those are specific to each
235 static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,
236 unsigned int notification, void *arg)
238 if (sd && sd->v4l2_dev && sd->v4l2_dev->notify)
239 sd->v4l2_dev->notify(sd, notification, arg);
243 * v4l2_device_supports_requests - Test if requests are supported.
245 * @v4l2_dev: pointer to struct v4l2_device
247 static inline bool v4l2_device_supports_requests(struct v4l2_device *v4l2_dev)
249 return v4l2_dev->mdev && v4l2_dev->mdev->ops &&
250 v4l2_dev->mdev->ops->req_queue;
253 /* Helper macros to iterate over all subdevs. */
256 * v4l2_device_for_each_subdev - Helper macro that interates over all
257 * sub-devices of a given &v4l2_device.
259 * @sd: pointer that will be filled by the macro with all
260 * &struct v4l2_subdev pointer used as an iterator by the loop.
261 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
263 * This macro iterates over all sub-devices owned by the @v4l2_dev device.
264 * It acts as a for loop iterator and executes the next statement with
265 * the @sd variable pointing to each sub-device in turn.
267 #define v4l2_device_for_each_subdev(sd, v4l2_dev) \
268 list_for_each_entry(sd, &(v4l2_dev)->subdevs, list)
271 * __v4l2_device_call_subdevs_p - Calls the specified operation for
272 * all subdevs matching the condition.
274 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
275 * @sd: pointer that will be filled by the macro with all
276 * &struct v4l2_subdev pointer used as an iterator by the loop.
277 * @cond: condition to be match
278 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
279 * Each element there groups a set of operations functions.
280 * @f: operation function that will be called if @cond matches.
281 * The operation functions are defined in groups, according to
282 * each element at &struct v4l2_subdev_ops.
283 * @args: arguments for @f.
287 * Note: subdevs cannot be added or deleted while walking
290 #define __v4l2_device_call_subdevs_p(v4l2_dev, sd, cond, o, f, args...) \
292 list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) \
293 if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
294 (sd)->ops->o->f((sd) , ##args); \
298 * __v4l2_device_call_subdevs - Calls the specified operation for
299 * all subdevs matching the condition.
301 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
302 * @cond: condition to be match
303 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
304 * Each element there groups a set of operations functions.
305 * @f: operation function that will be called if @cond matches.
306 * The operation functions are defined in groups, according to
307 * each element at &struct v4l2_subdev_ops.
308 * @args: arguments for @f.
312 * Note: subdevs cannot be added or deleted while walking
315 #define __v4l2_device_call_subdevs(v4l2_dev, cond, o, f, args...) \
317 struct v4l2_subdev *__sd; \
319 __v4l2_device_call_subdevs_p(v4l2_dev, __sd, cond, o, \
324 * __v4l2_device_call_subdevs_until_err_p - Calls the specified operation for
325 * all subdevs matching the condition.
327 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
328 * @sd: pointer that will be filled by the macro with all
329 * &struct v4l2_subdev sub-devices associated with @v4l2_dev.
330 * @cond: condition to be match
331 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
332 * Each element there groups a set of operations functions.
333 * @f: operation function that will be called if @cond matches.
334 * The operation functions are defined in groups, according to
335 * each element at &struct v4l2_subdev_ops.
336 * @args: arguments for @f.
340 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
341 * for any subdevice, then abort and return with that error code, zero
344 * Note: subdevs cannot be added or deleted while walking
347 #define __v4l2_device_call_subdevs_until_err_p(v4l2_dev, sd, cond, o, f, args...) \
351 list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) { \
352 if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
353 __err = (sd)->ops->o->f((sd) , ##args); \
354 if (__err && __err != -ENOIOCTLCMD) \
357 (__err == -ENOIOCTLCMD) ? 0 : __err; \
361 * __v4l2_device_call_subdevs_until_err - Calls the specified operation for
362 * all subdevs matching the condition.
364 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
365 * @cond: condition to be match
366 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
367 * Each element there groups a set of operations functions.
368 * @f: operation function that will be called if @cond matches.
369 * The operation functions are defined in groups, according to
370 * each element at &struct v4l2_subdev_ops.
371 * @args: arguments for @f.
375 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
376 * for any subdevice, then abort and return with that error code,
379 * Note: subdevs cannot be added or deleted while walking
382 #define __v4l2_device_call_subdevs_until_err(v4l2_dev, cond, o, f, args...) \
384 struct v4l2_subdev *__sd; \
385 __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, cond, o, \
390 * v4l2_device_call_all - Calls the specified operation for
391 * all subdevs matching the &v4l2_subdev.grp_id, as assigned
392 * by the bridge driver.
394 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
395 * @grpid: &struct v4l2_subdev->grp_id group ID to match.
396 * Use 0 to match them all.
397 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
398 * Each element there groups a set of operations functions.
399 * @f: operation function that will be called if @cond matches.
400 * The operation functions are defined in groups, according to
401 * each element at &struct v4l2_subdev_ops.
402 * @args: arguments for @f.
406 * Note: subdevs cannot be added or deleted while walking
409 #define v4l2_device_call_all(v4l2_dev, grpid, o, f, args...) \
411 struct v4l2_subdev *__sd; \
413 __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
414 (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
419 * v4l2_device_call_until_err - Calls the specified operation for
420 * all subdevs matching the &v4l2_subdev.grp_id, as assigned
421 * by the bridge driver, until an error occurs.
423 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
424 * @grpid: &struct v4l2_subdev->grp_id group ID to match.
425 * Use 0 to match them all.
426 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
427 * Each element there groups a set of operations functions.
428 * @f: operation function that will be called if @cond matches.
429 * The operation functions are defined in groups, according to
430 * each element at &struct v4l2_subdev_ops.
431 * @args: arguments for @f.
435 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
436 * for any subdevice, then abort and return with that error code,
439 * Note: subdevs cannot be added or deleted while walking
442 #define v4l2_device_call_until_err(v4l2_dev, grpid, o, f, args...) \
444 struct v4l2_subdev *__sd; \
445 __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
446 (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
451 * v4l2_device_mask_call_all - Calls the specified operation for
452 * all subdevices where a group ID matches a specified bitmask.
454 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
455 * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
456 * group ID to be matched. Use 0 to match them all.
457 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
458 * Each element there groups a set of operations functions.
459 * @f: operation function that will be called if @cond matches.
460 * The operation functions are defined in groups, according to
461 * each element at &struct v4l2_subdev_ops.
462 * @args: arguments for @f.
466 * Note: subdevs cannot be added or deleted while walking
469 #define v4l2_device_mask_call_all(v4l2_dev, grpmsk, o, f, args...) \
471 struct v4l2_subdev *__sd; \
473 __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
474 (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
479 * v4l2_device_mask_call_until_err - Calls the specified operation for
480 * all subdevices where a group ID matches a specified bitmask.
482 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
483 * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
484 * group ID to be matched. Use 0 to match them all.
485 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
486 * Each element there groups a set of operations functions.
487 * @f: operation function that will be called if @cond matches.
488 * The operation functions are defined in groups, according to
489 * each element at &struct v4l2_subdev_ops.
490 * @args: arguments for @f.
494 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
495 * for any subdevice, then abort and return with that error code,
498 * Note: subdevs cannot be added or deleted while walking
501 #define v4l2_device_mask_call_until_err(v4l2_dev, grpmsk, o, f, args...) \
503 struct v4l2_subdev *__sd; \
504 __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
505 (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
511 * v4l2_device_has_op - checks if any subdev with matching grpid has a
514 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
515 * @grpid: &struct v4l2_subdev->grp_id group ID to match.
516 * Use 0 to match them all.
517 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
518 * Each element there groups a set of operations functions.
519 * @f: operation function that will be called if @cond matches.
520 * The operation functions are defined in groups, according to
521 * each element at &struct v4l2_subdev_ops.
523 #define v4l2_device_has_op(v4l2_dev, grpid, o, f) \
525 struct v4l2_subdev *__sd; \
526 bool __result = false; \
527 list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
528 if ((grpid) && __sd->grp_id != (grpid)) \
530 if (v4l2_subdev_has_op(__sd, o, f)) { \
539 * v4l2_device_mask_has_op - checks if any subdev with matching group
540 * mask has a given ops.
542 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
543 * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
544 * group ID to be matched. Use 0 to match them all.
545 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
546 * Each element there groups a set of operations functions.
547 * @f: operation function that will be called if @cond matches.
548 * The operation functions are defined in groups, according to
549 * each element at &struct v4l2_subdev_ops.
551 #define v4l2_device_mask_has_op(v4l2_dev, grpmsk, o, f) \
553 struct v4l2_subdev *__sd; \
554 bool __result = false; \
555 list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
556 if ((grpmsk) && !(__sd->grp_id & (grpmsk))) \
558 if (v4l2_subdev_has_op(__sd, o, f)) { \