1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 V4L2 device support header.
4
5 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
6
7 */
8
9#ifndef _V4L2_DEVICE_H
10#define _V4L2_DEVICE_H
11
12#include <media/media-device.h>
13#include <media/v4l2-subdev.h>
14#include <media/v4l2-dev.h>
15
16struct v4l2_ctrl_handler;
17
18/**
19 * struct v4l2_device - main struct to for V4L2 device drivers
20 *
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
32 * goes to 0.
33 *
34 * Each instance of a V4L2 device should create the v4l2_device struct,
35 * either stand-alone or embedded in a larger struct.
36 *
37 * It allows easy access to sub-devices (see v4l2-subdev.h) and provides
38 * basic V4L2 device-level support.
39 *
40 * .. note::
41 *
42 * #) @dev->driver_data points to this struct.
43 * #) @dev might be %NULL if there is no parent device
44 */
45struct v4l2_device {
46 struct device *dev;
47 struct media_device *mdev;
48 struct list_head subdevs;
49 spinlock_t lock;
50 char name[36];
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;
55 struct kref ref;
56 void (*release)(struct v4l2_device *v4l2_dev);
57};
58
59/**
60 * v4l2_device_get - gets a V4L2 device reference
61 *
62 * @v4l2_dev: pointer to struct &v4l2_device
63 *
64 * This is an ancillary routine meant to increment the usage for the
65 * struct &v4l2_device pointed by @v4l2_dev.
66 */
67static inline void v4l2_device_get(struct v4l2_device *v4l2_dev)
68{
69 kref_get(kref: &v4l2_dev->ref);
70}
71
72/**
73 * v4l2_device_put - puts a V4L2 device reference
74 *
75 * @v4l2_dev: pointer to struct &v4l2_device
76 *
77 * This is an ancillary routine meant to decrement the usage for the
78 * struct &v4l2_device pointed by @v4l2_dev.
79 */
80int v4l2_device_put(struct v4l2_device *v4l2_dev);
81
82/**
83 * v4l2_device_register - Initialize v4l2_dev and make @dev->driver_data
84 * point to @v4l2_dev.
85 *
86 * @dev: pointer to struct &device
87 * @v4l2_dev: pointer to struct &v4l2_device
88 *
89 * .. note::
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.
93 */
94int __must_check v4l2_device_register(struct device *dev,
95 struct v4l2_device *v4l2_dev);
96
97/**
98 * v4l2_device_set_name - Optional function to initialize the
99 * name field of struct &v4l2_device
100 *
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
104 * the device driver.
105 *
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.
108 *
109 * This function will increment the instance counter and returns the
110 * instance value used in the name.
111 *
112 * Example:
113 *
114 * static atomic_t drv_instance = ATOMIC_INIT(0);
115 *
116 * ...
117 *
118 * instance = v4l2_device_set_name(&\ v4l2_dev, "foo", &\ drv_instance);
119 *
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.
123 */
124int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
125 atomic_t *instance);
126
127/**
128 * v4l2_device_disconnect - Change V4L2 device state to disconnected.
129 *
130 * @v4l2_dev: pointer to struct v4l2_device
131 *
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.
135 *
136 * .. note:: This function sets @v4l2_dev->dev to NULL.
137 */
138void v4l2_device_disconnect(struct v4l2_device *v4l2_dev);
139
140/**
141 * v4l2_device_unregister - Unregister all sub-devices and any other
142 * resources related to @v4l2_dev.
143 *
144 * @v4l2_dev: pointer to struct v4l2_device
145 */
146void v4l2_device_unregister(struct v4l2_device *v4l2_dev);
147
148/**
149 * v4l2_device_register_subdev - Registers a subdev with a v4l2 device.
150 *
151 * @v4l2_dev: pointer to struct &v4l2_device
152 * @sd: pointer to &struct v4l2_subdev
153 *
154 * While registered, the subdev module is marked as in-use.
155 *
156 * An error is returned if the module is no longer loaded on any attempts
157 * to register it.
158 */
159int __must_check v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
160 struct v4l2_subdev *sd);
161
162/**
163 * v4l2_device_unregister_subdev - Unregisters a subdev with a v4l2 device.
164 *
165 * @sd: pointer to &struct v4l2_subdev
166 *
167 * .. note ::
168 *
169 * Can also be called if the subdev wasn't registered. In such
170 * case, it will do nothing.
171 */
172void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
173
174/**
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.
178 *
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.
183 */
184int __must_check
185__v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
186 bool read_only);
187
188/**
189 * v4l2_device_register_subdev_nodes - Registers subdevices device nodes with
190 * unrestricted access to the subdevice userspace operations
191 *
192 * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
193 * for more details.
194 *
195 * @v4l2_dev: pointer to struct v4l2_device
196 */
197static inline int __must_check
198v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
199{
200#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
201 return __v4l2_device_register_subdev_nodes(v4l2_dev, read_only: false);
202#else
203 return 0;
204#endif
205}
206
207/**
208 * v4l2_device_register_ro_subdev_nodes - Registers subdevices device nodes
209 * in read-only mode
210 *
211 * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
212 * for more details.
213 *
214 * @v4l2_dev: pointer to struct v4l2_device
215 */
216static inline int __must_check
217v4l2_device_register_ro_subdev_nodes(struct v4l2_device *v4l2_dev)
218{
219#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
220 return __v4l2_device_register_subdev_nodes(v4l2_dev, read_only: true);
221#else
222 return 0;
223#endif
224}
225
226/**
227 * v4l2_subdev_notify - Sends a notification to v4l2_device.
228 *
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
233 * notification type.
234 */
235static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,
236 unsigned int notification, void *arg)
237{
238 if (sd && sd->v4l2_dev && sd->v4l2_dev->notify)
239 sd->v4l2_dev->notify(sd, notification, arg);
240}
241
242/**
243 * v4l2_device_supports_requests - Test if requests are supported.
244 *
245 * @v4l2_dev: pointer to struct v4l2_device
246 */
247static inline bool v4l2_device_supports_requests(struct v4l2_device *v4l2_dev)
248{
249 return v4l2_dev->mdev && v4l2_dev->mdev->ops &&
250 v4l2_dev->mdev->ops->req_queue;
251}
252
253/* Helper macros to iterate over all subdevs. */
254
255/**
256 * v4l2_device_for_each_subdev - Helper macro that interates over all
257 * sub-devices of a given &v4l2_device.
258 *
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.
262 *
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.
266 */
267#define v4l2_device_for_each_subdev(sd, v4l2_dev) \
268 list_for_each_entry(sd, &(v4l2_dev)->subdevs, list)
269
270/**
271 * __v4l2_device_call_subdevs_p - Calls the specified operation for
272 * all subdevs matching the condition.
273 *
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.
284 *
285 * Ignore any errors.
286 *
287 * Note: subdevs cannot be added or deleted while walking
288 * the subdevs list.
289 */
290#define __v4l2_device_call_subdevs_p(v4l2_dev, sd, cond, o, f, args...) \
291 do { \
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); \
295 } while (0)
296
297/**
298 * __v4l2_device_call_subdevs - Calls the specified operation for
299 * all subdevs matching the condition.
300 *
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.
309 *
310 * Ignore any errors.
311 *
312 * Note: subdevs cannot be added or deleted while walking
313 * the subdevs list.
314 */
315#define __v4l2_device_call_subdevs(v4l2_dev, cond, o, f, args...) \
316 do { \
317 struct v4l2_subdev *__sd; \
318 \
319 __v4l2_device_call_subdevs_p(v4l2_dev, __sd, cond, o, \
320 f , ##args); \
321 } while (0)
322
323/**
324 * __v4l2_device_call_subdevs_until_err_p - Calls the specified operation for
325 * all subdevs matching the condition.
326 *
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.
337 *
338 * Return:
339 *
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
342 * otherwise.
343 *
344 * Note: subdevs cannot be added or deleted while walking
345 * the subdevs list.
346 */
347#define __v4l2_device_call_subdevs_until_err_p(v4l2_dev, sd, cond, o, f, args...) \
348({ \
349 long __err = 0; \
350 \
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) \
355 break; \
356 } \
357 (__err == -ENOIOCTLCMD) ? 0 : __err; \
358})
359
360/**
361 * __v4l2_device_call_subdevs_until_err - Calls the specified operation for
362 * all subdevs matching the condition.
363 *
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.
372 *
373 * Return:
374 *
375 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
376 * for any subdevice, then abort and return with that error code,
377 * zero otherwise.
378 *
379 * Note: subdevs cannot be added or deleted while walking
380 * the subdevs list.
381 */
382#define __v4l2_device_call_subdevs_until_err(v4l2_dev, cond, o, f, args...) \
383({ \
384 struct v4l2_subdev *__sd; \
385 __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, cond, o, \
386 f , ##args); \
387})
388
389/**
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.
393 *
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.
403 *
404 * Ignore any errors.
405 *
406 * Note: subdevs cannot be added or deleted while walking
407 * the subdevs list.
408 */
409#define v4l2_device_call_all(v4l2_dev, grpid, o, f, args...) \
410 do { \
411 struct v4l2_subdev *__sd; \
412 \
413 __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
414 (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
415 ##args); \
416 } while (0)
417
418/**
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.
422 *
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.
432 *
433 * Return:
434 *
435 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
436 * for any subdevice, then abort and return with that error code,
437 * zero otherwise.
438 *
439 * Note: subdevs cannot be added or deleted while walking
440 * the subdevs list.
441 */
442#define v4l2_device_call_until_err(v4l2_dev, grpid, o, f, args...) \
443({ \
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 , \
447 ##args); \
448})
449
450/**
451 * v4l2_device_mask_call_all - Calls the specified operation for
452 * all subdevices where a group ID matches a specified bitmask.
453 *
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.
463 *
464 * Ignore any errors.
465 *
466 * Note: subdevs cannot be added or deleted while walking
467 * the subdevs list.
468 */
469#define v4l2_device_mask_call_all(v4l2_dev, grpmsk, o, f, args...) \
470 do { \
471 struct v4l2_subdev *__sd; \
472 \
473 __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
474 (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
475 f , ##args); \
476 } while (0)
477
478/**
479 * v4l2_device_mask_call_until_err - Calls the specified operation for
480 * all subdevices where a group ID matches a specified bitmask.
481 *
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.
491 *
492 * Return:
493 *
494 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
495 * for any subdevice, then abort and return with that error code,
496 * zero otherwise.
497 *
498 * Note: subdevs cannot be added or deleted while walking
499 * the subdevs list.
500 */
501#define v4l2_device_mask_call_until_err(v4l2_dev, grpmsk, o, f, args...) \
502({ \
503 struct v4l2_subdev *__sd; \
504 __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
505 (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
506 f , ##args); \
507})
508
509
510/**
511 * v4l2_device_has_op - checks if any subdev with matching grpid has a
512 * given ops.
513 *
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.
522 */
523#define v4l2_device_has_op(v4l2_dev, grpid, o, f) \
524({ \
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)) \
529 continue; \
530 if (v4l2_subdev_has_op(__sd, o, f)) { \
531 __result = true; \
532 break; \
533 } \
534 } \
535 __result; \
536})
537
538/**
539 * v4l2_device_mask_has_op - checks if any subdev with matching group
540 * mask has a given ops.
541 *
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.
550 */
551#define v4l2_device_mask_has_op(v4l2_dev, grpmsk, o, f) \
552({ \
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))) \
557 continue; \
558 if (v4l2_subdev_has_op(__sd, o, f)) { \
559 __result = true; \
560 break; \
561 } \
562 } \
563 __result; \
564})
565
566#endif
567

source code of linux/include/media/v4l2-device.h