1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun V4L2 device support header.
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #ifndef _V4L2_DEVICE_H
10*4882a593Smuzhiyun #define _V4L2_DEVICE_H
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <media/media-device.h>
13*4882a593Smuzhiyun #include <media/v4l2-subdev.h>
14*4882a593Smuzhiyun #include <media/v4l2-dev.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define V4L2_DEVICE_NAME_SIZE (20 + 16)
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun struct v4l2_ctrl_handler;
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun * struct v4l2_device - main struct to for V4L2 device drivers
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * @dev: pointer to struct device.
24*4882a593Smuzhiyun * @mdev: pointer to struct media_device, may be NULL.
25*4882a593Smuzhiyun * @subdevs: used to keep track of the registered subdevs
26*4882a593Smuzhiyun * @lock: lock this struct; can be used by the driver as well
27*4882a593Smuzhiyun * if this struct is embedded into a larger struct.
28*4882a593Smuzhiyun * @name: unique device name, by default the driver name + bus ID
29*4882a593Smuzhiyun * @notify: notify operation called by some sub-devices.
30*4882a593Smuzhiyun * @ctrl_handler: The control handler. May be %NULL.
31*4882a593Smuzhiyun * @prio: Device's priority state
32*4882a593Smuzhiyun * @ref: Keep track of the references to this struct.
33*4882a593Smuzhiyun * @release: Release function that is called when the ref count
34*4882a593Smuzhiyun * goes to 0.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * Each instance of a V4L2 device should create the v4l2_device struct,
37*4882a593Smuzhiyun * either stand-alone or embedded in a larger struct.
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * It allows easy access to sub-devices (see v4l2-subdev.h) and provides
40*4882a593Smuzhiyun * basic V4L2 device-level support.
41*4882a593Smuzhiyun *
42*4882a593Smuzhiyun * .. note::
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * #) @dev->driver_data points to this struct.
45*4882a593Smuzhiyun * #) @dev might be %NULL if there is no parent device
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun struct v4l2_device {
48*4882a593Smuzhiyun struct device *dev;
49*4882a593Smuzhiyun struct media_device *mdev;
50*4882a593Smuzhiyun struct list_head subdevs;
51*4882a593Smuzhiyun spinlock_t lock;
52*4882a593Smuzhiyun char name[V4L2_DEVICE_NAME_SIZE];
53*4882a593Smuzhiyun void (*notify)(struct v4l2_subdev *sd,
54*4882a593Smuzhiyun unsigned int notification, void *arg);
55*4882a593Smuzhiyun struct v4l2_ctrl_handler *ctrl_handler;
56*4882a593Smuzhiyun struct v4l2_prio_state prio;
57*4882a593Smuzhiyun struct kref ref;
58*4882a593Smuzhiyun void (*release)(struct v4l2_device *v4l2_dev);
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /**
62*4882a593Smuzhiyun * v4l2_device_get - gets a V4L2 device reference
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * @v4l2_dev: pointer to struct &v4l2_device
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * This is an ancillary routine meant to increment the usage for the
67*4882a593Smuzhiyun * struct &v4l2_device pointed by @v4l2_dev.
68*4882a593Smuzhiyun */
v4l2_device_get(struct v4l2_device * v4l2_dev)69*4882a593Smuzhiyun static inline void v4l2_device_get(struct v4l2_device *v4l2_dev)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun kref_get(&v4l2_dev->ref);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun * v4l2_device_put - puts a V4L2 device reference
76*4882a593Smuzhiyun *
77*4882a593Smuzhiyun * @v4l2_dev: pointer to struct &v4l2_device
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * This is an ancillary routine meant to decrement the usage for the
80*4882a593Smuzhiyun * struct &v4l2_device pointed by @v4l2_dev.
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun int v4l2_device_put(struct v4l2_device *v4l2_dev);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /**
85*4882a593Smuzhiyun * v4l2_device_register - Initialize v4l2_dev and make @dev->driver_data
86*4882a593Smuzhiyun * point to @v4l2_dev.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * @dev: pointer to struct &device
89*4882a593Smuzhiyun * @v4l2_dev: pointer to struct &v4l2_device
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * .. note::
92*4882a593Smuzhiyun * @dev may be %NULL in rare cases (ISA devices).
93*4882a593Smuzhiyun * In such case the caller must fill in the @v4l2_dev->name field
94*4882a593Smuzhiyun * before calling this function.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun int __must_check v4l2_device_register(struct device *dev,
97*4882a593Smuzhiyun struct v4l2_device *v4l2_dev);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun * v4l2_device_set_name - Optional function to initialize the
101*4882a593Smuzhiyun * name field of struct &v4l2_device
102*4882a593Smuzhiyun *
103*4882a593Smuzhiyun * @v4l2_dev: pointer to struct &v4l2_device
104*4882a593Smuzhiyun * @basename: base name for the device name
105*4882a593Smuzhiyun * @instance: pointer to a static atomic_t var with the instance usage for
106*4882a593Smuzhiyun * the device driver.
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * v4l2_device_set_name() initializes the name field of struct &v4l2_device
109*4882a593Smuzhiyun * using the driver name and a driver-global atomic_t instance.
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * This function will increment the instance counter and returns the
112*4882a593Smuzhiyun * instance value used in the name.
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun * Example:
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun * static atomic_t drv_instance = ATOMIC_INIT(0);
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * ...
119*4882a593Smuzhiyun *
120*4882a593Smuzhiyun * instance = v4l2_device_set_name(&\ v4l2_dev, "foo", &\ drv_instance);
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * The first time this is called the name field will be set to foo0 and
123*4882a593Smuzhiyun * this function returns 0. If the name ends with a digit (e.g. cx18),
124*4882a593Smuzhiyun * then the name will be set to cx18-0 since cx180 would look really odd.
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
127*4882a593Smuzhiyun atomic_t *instance);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /**
130*4882a593Smuzhiyun * v4l2_device_disconnect - Change V4L2 device state to disconnected.
131*4882a593Smuzhiyun *
132*4882a593Smuzhiyun * @v4l2_dev: pointer to struct v4l2_device
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * Should be called when the USB parent disconnects.
135*4882a593Smuzhiyun * Since the parent disappears, this ensures that @v4l2_dev doesn't have
136*4882a593Smuzhiyun * an invalid parent pointer.
137*4882a593Smuzhiyun *
138*4882a593Smuzhiyun * .. note:: This function sets @v4l2_dev->dev to NULL.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun void v4l2_device_disconnect(struct v4l2_device *v4l2_dev);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /**
143*4882a593Smuzhiyun * v4l2_device_unregister - Unregister all sub-devices and any other
144*4882a593Smuzhiyun * resources related to @v4l2_dev.
145*4882a593Smuzhiyun *
146*4882a593Smuzhiyun * @v4l2_dev: pointer to struct v4l2_device
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun void v4l2_device_unregister(struct v4l2_device *v4l2_dev);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun * v4l2_device_register_subdev - Registers a subdev with a v4l2 device.
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * @v4l2_dev: pointer to struct &v4l2_device
154*4882a593Smuzhiyun * @sd: pointer to &struct v4l2_subdev
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * While registered, the subdev module is marked as in-use.
157*4882a593Smuzhiyun *
158*4882a593Smuzhiyun * An error is returned if the module is no longer loaded on any attempts
159*4882a593Smuzhiyun * to register it.
160*4882a593Smuzhiyun */
161*4882a593Smuzhiyun int __must_check v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
162*4882a593Smuzhiyun struct v4l2_subdev *sd);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /**
165*4882a593Smuzhiyun * v4l2_device_unregister_subdev - Unregisters a subdev with a v4l2 device.
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * @sd: pointer to &struct v4l2_subdev
168*4882a593Smuzhiyun *
169*4882a593Smuzhiyun * .. note ::
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * Can also be called if the subdev wasn't registered. In such
172*4882a593Smuzhiyun * case, it will do nothing.
173*4882a593Smuzhiyun */
174*4882a593Smuzhiyun void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /**
177*4882a593Smuzhiyun * __v4l2_device_register_ro_subdev_nodes - Registers device nodes for
178*4882a593Smuzhiyun * all subdevs of the v4l2 device that are marked with the
179*4882a593Smuzhiyun * %V4L2_SUBDEV_FL_HAS_DEVNODE flag.
180*4882a593Smuzhiyun *
181*4882a593Smuzhiyun * @v4l2_dev: pointer to struct v4l2_device
182*4882a593Smuzhiyun * @read_only: subdevices read-only flag. True to register the subdevices
183*4882a593Smuzhiyun * device nodes in read-only mode, false to allow full access to the
184*4882a593Smuzhiyun * subdevice userspace API.
185*4882a593Smuzhiyun */
186*4882a593Smuzhiyun int __must_check
187*4882a593Smuzhiyun __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
188*4882a593Smuzhiyun bool read_only);
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun * v4l2_device_register_subdev_nodes - Registers subdevices device nodes with
192*4882a593Smuzhiyun * unrestricted access to the subdevice userspace operations
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
195*4882a593Smuzhiyun * for more details.
196*4882a593Smuzhiyun *
197*4882a593Smuzhiyun * @v4l2_dev: pointer to struct v4l2_device
198*4882a593Smuzhiyun */
199*4882a593Smuzhiyun static inline int __must_check
v4l2_device_register_subdev_nodes(struct v4l2_device * v4l2_dev)200*4882a593Smuzhiyun v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
203*4882a593Smuzhiyun return __v4l2_device_register_subdev_nodes(v4l2_dev, false);
204*4882a593Smuzhiyun #else
205*4882a593Smuzhiyun return 0;
206*4882a593Smuzhiyun #endif
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /**
210*4882a593Smuzhiyun * v4l2_device_register_ro_subdev_nodes - Registers subdevices device nodes
211*4882a593Smuzhiyun * in read-only mode
212*4882a593Smuzhiyun *
213*4882a593Smuzhiyun * Internally calls __v4l2_device_register_subdev_nodes(). See its documentation
214*4882a593Smuzhiyun * for more details.
215*4882a593Smuzhiyun *
216*4882a593Smuzhiyun * @v4l2_dev: pointer to struct v4l2_device
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun static inline int __must_check
v4l2_device_register_ro_subdev_nodes(struct v4l2_device * v4l2_dev)219*4882a593Smuzhiyun v4l2_device_register_ro_subdev_nodes(struct v4l2_device *v4l2_dev)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
222*4882a593Smuzhiyun return __v4l2_device_register_subdev_nodes(v4l2_dev, true);
223*4882a593Smuzhiyun #else
224*4882a593Smuzhiyun return 0;
225*4882a593Smuzhiyun #endif
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /**
229*4882a593Smuzhiyun * v4l2_subdev_notify - Sends a notification to v4l2_device.
230*4882a593Smuzhiyun *
231*4882a593Smuzhiyun * @sd: pointer to &struct v4l2_subdev
232*4882a593Smuzhiyun * @notification: type of notification. Please notice that the notification
233*4882a593Smuzhiyun * type is driver-specific.
234*4882a593Smuzhiyun * @arg: arguments for the notification. Those are specific to each
235*4882a593Smuzhiyun * notification type.
236*4882a593Smuzhiyun */
v4l2_subdev_notify(struct v4l2_subdev * sd,unsigned int notification,void * arg)237*4882a593Smuzhiyun static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,
238*4882a593Smuzhiyun unsigned int notification, void *arg)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun if (sd && sd->v4l2_dev && sd->v4l2_dev->notify)
241*4882a593Smuzhiyun sd->v4l2_dev->notify(sd, notification, arg);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun /**
245*4882a593Smuzhiyun * v4l2_device_supports_requests - Test if requests are supported.
246*4882a593Smuzhiyun *
247*4882a593Smuzhiyun * @v4l2_dev: pointer to struct v4l2_device
248*4882a593Smuzhiyun */
v4l2_device_supports_requests(struct v4l2_device * v4l2_dev)249*4882a593Smuzhiyun static inline bool v4l2_device_supports_requests(struct v4l2_device *v4l2_dev)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun return v4l2_dev->mdev && v4l2_dev->mdev->ops &&
252*4882a593Smuzhiyun v4l2_dev->mdev->ops->req_queue;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /* Helper macros to iterate over all subdevs. */
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /**
258*4882a593Smuzhiyun * v4l2_device_for_each_subdev - Helper macro that interates over all
259*4882a593Smuzhiyun * sub-devices of a given &v4l2_device.
260*4882a593Smuzhiyun *
261*4882a593Smuzhiyun * @sd: pointer that will be filled by the macro with all
262*4882a593Smuzhiyun * &struct v4l2_subdev pointer used as an iterator by the loop.
263*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
264*4882a593Smuzhiyun *
265*4882a593Smuzhiyun * This macro iterates over all sub-devices owned by the @v4l2_dev device.
266*4882a593Smuzhiyun * It acts as a for loop iterator and executes the next statement with
267*4882a593Smuzhiyun * the @sd variable pointing to each sub-device in turn.
268*4882a593Smuzhiyun */
269*4882a593Smuzhiyun #define v4l2_device_for_each_subdev(sd, v4l2_dev) \
270*4882a593Smuzhiyun list_for_each_entry(sd, &(v4l2_dev)->subdevs, list)
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /**
273*4882a593Smuzhiyun * __v4l2_device_call_subdevs_p - Calls the specified operation for
274*4882a593Smuzhiyun * all subdevs matching the condition.
275*4882a593Smuzhiyun *
276*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
277*4882a593Smuzhiyun * @sd: pointer that will be filled by the macro with all
278*4882a593Smuzhiyun * &struct v4l2_subdev pointer used as an iterator by the loop.
279*4882a593Smuzhiyun * @cond: condition to be match
280*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
281*4882a593Smuzhiyun * Each element there groups a set of operations functions.
282*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
283*4882a593Smuzhiyun * The operation functions are defined in groups, according to
284*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
285*4882a593Smuzhiyun * @args: arguments for @f.
286*4882a593Smuzhiyun *
287*4882a593Smuzhiyun * Ignore any errors.
288*4882a593Smuzhiyun *
289*4882a593Smuzhiyun * Note: subdevs cannot be added or deleted while walking
290*4882a593Smuzhiyun * the subdevs list.
291*4882a593Smuzhiyun */
292*4882a593Smuzhiyun #define __v4l2_device_call_subdevs_p(v4l2_dev, sd, cond, o, f, args...) \
293*4882a593Smuzhiyun do { \
294*4882a593Smuzhiyun list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) \
295*4882a593Smuzhiyun if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
296*4882a593Smuzhiyun (sd)->ops->o->f((sd) , ##args); \
297*4882a593Smuzhiyun } while (0)
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /**
300*4882a593Smuzhiyun * __v4l2_device_call_subdevs - Calls the specified operation for
301*4882a593Smuzhiyun * all subdevs matching the condition.
302*4882a593Smuzhiyun *
303*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
304*4882a593Smuzhiyun * @cond: condition to be match
305*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
306*4882a593Smuzhiyun * Each element there groups a set of operations functions.
307*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
308*4882a593Smuzhiyun * The operation functions are defined in groups, according to
309*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
310*4882a593Smuzhiyun * @args: arguments for @f.
311*4882a593Smuzhiyun *
312*4882a593Smuzhiyun * Ignore any errors.
313*4882a593Smuzhiyun *
314*4882a593Smuzhiyun * Note: subdevs cannot be added or deleted while walking
315*4882a593Smuzhiyun * the subdevs list.
316*4882a593Smuzhiyun */
317*4882a593Smuzhiyun #define __v4l2_device_call_subdevs(v4l2_dev, cond, o, f, args...) \
318*4882a593Smuzhiyun do { \
319*4882a593Smuzhiyun struct v4l2_subdev *__sd; \
320*4882a593Smuzhiyun \
321*4882a593Smuzhiyun __v4l2_device_call_subdevs_p(v4l2_dev, __sd, cond, o, \
322*4882a593Smuzhiyun f , ##args); \
323*4882a593Smuzhiyun } while (0)
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /**
326*4882a593Smuzhiyun * __v4l2_device_call_subdevs_until_err_p - Calls the specified operation for
327*4882a593Smuzhiyun * all subdevs matching the condition.
328*4882a593Smuzhiyun *
329*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
330*4882a593Smuzhiyun * @sd: pointer that will be filled by the macro with all
331*4882a593Smuzhiyun * &struct v4l2_subdev sub-devices associated with @v4l2_dev.
332*4882a593Smuzhiyun * @cond: condition to be match
333*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
334*4882a593Smuzhiyun * Each element there groups a set of operations functions.
335*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
336*4882a593Smuzhiyun * The operation functions are defined in groups, according to
337*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
338*4882a593Smuzhiyun * @args: arguments for @f.
339*4882a593Smuzhiyun *
340*4882a593Smuzhiyun * Return:
341*4882a593Smuzhiyun *
342*4882a593Smuzhiyun * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
343*4882a593Smuzhiyun * for any subdevice, then abort and return with that error code, zero
344*4882a593Smuzhiyun * otherwise.
345*4882a593Smuzhiyun *
346*4882a593Smuzhiyun * Note: subdevs cannot be added or deleted while walking
347*4882a593Smuzhiyun * the subdevs list.
348*4882a593Smuzhiyun */
349*4882a593Smuzhiyun #define __v4l2_device_call_subdevs_until_err_p(v4l2_dev, sd, cond, o, f, args...) \
350*4882a593Smuzhiyun ({ \
351*4882a593Smuzhiyun long __err = 0; \
352*4882a593Smuzhiyun \
353*4882a593Smuzhiyun list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) { \
354*4882a593Smuzhiyun if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
355*4882a593Smuzhiyun __err = (sd)->ops->o->f((sd) , ##args); \
356*4882a593Smuzhiyun if (__err && __err != -ENOIOCTLCMD) \
357*4882a593Smuzhiyun break; \
358*4882a593Smuzhiyun } \
359*4882a593Smuzhiyun (__err == -ENOIOCTLCMD) ? 0 : __err; \
360*4882a593Smuzhiyun })
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun /**
363*4882a593Smuzhiyun * __v4l2_device_call_subdevs_until_err - Calls the specified operation for
364*4882a593Smuzhiyun * all subdevs matching the condition.
365*4882a593Smuzhiyun *
366*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
367*4882a593Smuzhiyun * @cond: condition to be match
368*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
369*4882a593Smuzhiyun * Each element there groups a set of operations functions.
370*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
371*4882a593Smuzhiyun * The operation functions are defined in groups, according to
372*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
373*4882a593Smuzhiyun * @args: arguments for @f.
374*4882a593Smuzhiyun *
375*4882a593Smuzhiyun * Return:
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
378*4882a593Smuzhiyun * for any subdevice, then abort and return with that error code,
379*4882a593Smuzhiyun * zero otherwise.
380*4882a593Smuzhiyun *
381*4882a593Smuzhiyun * Note: subdevs cannot be added or deleted while walking
382*4882a593Smuzhiyun * the subdevs list.
383*4882a593Smuzhiyun */
384*4882a593Smuzhiyun #define __v4l2_device_call_subdevs_until_err(v4l2_dev, cond, o, f, args...) \
385*4882a593Smuzhiyun ({ \
386*4882a593Smuzhiyun struct v4l2_subdev *__sd; \
387*4882a593Smuzhiyun __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, cond, o, \
388*4882a593Smuzhiyun f , ##args); \
389*4882a593Smuzhiyun })
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun /**
392*4882a593Smuzhiyun * v4l2_device_call_all - Calls the specified operation for
393*4882a593Smuzhiyun * all subdevs matching the &v4l2_subdev.grp_id, as assigned
394*4882a593Smuzhiyun * by the bridge driver.
395*4882a593Smuzhiyun *
396*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
397*4882a593Smuzhiyun * @grpid: &struct v4l2_subdev->grp_id group ID to match.
398*4882a593Smuzhiyun * Use 0 to match them all.
399*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
400*4882a593Smuzhiyun * Each element there groups a set of operations functions.
401*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
402*4882a593Smuzhiyun * The operation functions are defined in groups, according to
403*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
404*4882a593Smuzhiyun * @args: arguments for @f.
405*4882a593Smuzhiyun *
406*4882a593Smuzhiyun * Ignore any errors.
407*4882a593Smuzhiyun *
408*4882a593Smuzhiyun * Note: subdevs cannot be added or deleted while walking
409*4882a593Smuzhiyun * the subdevs list.
410*4882a593Smuzhiyun */
411*4882a593Smuzhiyun #define v4l2_device_call_all(v4l2_dev, grpid, o, f, args...) \
412*4882a593Smuzhiyun do { \
413*4882a593Smuzhiyun struct v4l2_subdev *__sd; \
414*4882a593Smuzhiyun \
415*4882a593Smuzhiyun __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
416*4882a593Smuzhiyun (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
417*4882a593Smuzhiyun ##args); \
418*4882a593Smuzhiyun } while (0)
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun /**
421*4882a593Smuzhiyun * v4l2_device_call_until_err - Calls the specified operation for
422*4882a593Smuzhiyun * all subdevs matching the &v4l2_subdev.grp_id, as assigned
423*4882a593Smuzhiyun * by the bridge driver, until an error occurs.
424*4882a593Smuzhiyun *
425*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
426*4882a593Smuzhiyun * @grpid: &struct v4l2_subdev->grp_id group ID to match.
427*4882a593Smuzhiyun * Use 0 to match them all.
428*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
429*4882a593Smuzhiyun * Each element there groups a set of operations functions.
430*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
431*4882a593Smuzhiyun * The operation functions are defined in groups, according to
432*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
433*4882a593Smuzhiyun * @args: arguments for @f.
434*4882a593Smuzhiyun *
435*4882a593Smuzhiyun * Return:
436*4882a593Smuzhiyun *
437*4882a593Smuzhiyun * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
438*4882a593Smuzhiyun * for any subdevice, then abort and return with that error code,
439*4882a593Smuzhiyun * zero otherwise.
440*4882a593Smuzhiyun *
441*4882a593Smuzhiyun * Note: subdevs cannot be added or deleted while walking
442*4882a593Smuzhiyun * the subdevs list.
443*4882a593Smuzhiyun */
444*4882a593Smuzhiyun #define v4l2_device_call_until_err(v4l2_dev, grpid, o, f, args...) \
445*4882a593Smuzhiyun ({ \
446*4882a593Smuzhiyun struct v4l2_subdev *__sd; \
447*4882a593Smuzhiyun __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
448*4882a593Smuzhiyun (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
449*4882a593Smuzhiyun ##args); \
450*4882a593Smuzhiyun })
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun /**
453*4882a593Smuzhiyun * v4l2_device_mask_call_all - Calls the specified operation for
454*4882a593Smuzhiyun * all subdevices where a group ID matches a specified bitmask.
455*4882a593Smuzhiyun *
456*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
457*4882a593Smuzhiyun * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
458*4882a593Smuzhiyun * group ID to be matched. Use 0 to match them all.
459*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
460*4882a593Smuzhiyun * Each element there groups a set of operations functions.
461*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
462*4882a593Smuzhiyun * The operation functions are defined in groups, according to
463*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
464*4882a593Smuzhiyun * @args: arguments for @f.
465*4882a593Smuzhiyun *
466*4882a593Smuzhiyun * Ignore any errors.
467*4882a593Smuzhiyun *
468*4882a593Smuzhiyun * Note: subdevs cannot be added or deleted while walking
469*4882a593Smuzhiyun * the subdevs list.
470*4882a593Smuzhiyun */
471*4882a593Smuzhiyun #define v4l2_device_mask_call_all(v4l2_dev, grpmsk, o, f, args...) \
472*4882a593Smuzhiyun do { \
473*4882a593Smuzhiyun struct v4l2_subdev *__sd; \
474*4882a593Smuzhiyun \
475*4882a593Smuzhiyun __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
476*4882a593Smuzhiyun (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
477*4882a593Smuzhiyun f , ##args); \
478*4882a593Smuzhiyun } while (0)
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /**
481*4882a593Smuzhiyun * v4l2_device_mask_call_until_err - Calls the specified operation for
482*4882a593Smuzhiyun * all subdevices where a group ID matches a specified bitmask.
483*4882a593Smuzhiyun *
484*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
485*4882a593Smuzhiyun * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
486*4882a593Smuzhiyun * group ID to be matched. Use 0 to match them all.
487*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
488*4882a593Smuzhiyun * Each element there groups a set of operations functions.
489*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
490*4882a593Smuzhiyun * The operation functions are defined in groups, according to
491*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
492*4882a593Smuzhiyun * @args: arguments for @f.
493*4882a593Smuzhiyun *
494*4882a593Smuzhiyun * Return:
495*4882a593Smuzhiyun *
496*4882a593Smuzhiyun * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
497*4882a593Smuzhiyun * for any subdevice, then abort and return with that error code,
498*4882a593Smuzhiyun * zero otherwise.
499*4882a593Smuzhiyun *
500*4882a593Smuzhiyun * Note: subdevs cannot be added or deleted while walking
501*4882a593Smuzhiyun * the subdevs list.
502*4882a593Smuzhiyun */
503*4882a593Smuzhiyun #define v4l2_device_mask_call_until_err(v4l2_dev, grpmsk, o, f, args...) \
504*4882a593Smuzhiyun ({ \
505*4882a593Smuzhiyun struct v4l2_subdev *__sd; \
506*4882a593Smuzhiyun __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
507*4882a593Smuzhiyun (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
508*4882a593Smuzhiyun f , ##args); \
509*4882a593Smuzhiyun })
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun /**
513*4882a593Smuzhiyun * v4l2_device_has_op - checks if any subdev with matching grpid has a
514*4882a593Smuzhiyun * given ops.
515*4882a593Smuzhiyun *
516*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
517*4882a593Smuzhiyun * @grpid: &struct v4l2_subdev->grp_id group ID to match.
518*4882a593Smuzhiyun * Use 0 to match them all.
519*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
520*4882a593Smuzhiyun * Each element there groups a set of operations functions.
521*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
522*4882a593Smuzhiyun * The operation functions are defined in groups, according to
523*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
524*4882a593Smuzhiyun */
525*4882a593Smuzhiyun #define v4l2_device_has_op(v4l2_dev, grpid, o, f) \
526*4882a593Smuzhiyun ({ \
527*4882a593Smuzhiyun struct v4l2_subdev *__sd; \
528*4882a593Smuzhiyun bool __result = false; \
529*4882a593Smuzhiyun list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
530*4882a593Smuzhiyun if ((grpid) && __sd->grp_id != (grpid)) \
531*4882a593Smuzhiyun continue; \
532*4882a593Smuzhiyun if (v4l2_subdev_has_op(__sd, o, f)) { \
533*4882a593Smuzhiyun __result = true; \
534*4882a593Smuzhiyun break; \
535*4882a593Smuzhiyun } \
536*4882a593Smuzhiyun } \
537*4882a593Smuzhiyun __result; \
538*4882a593Smuzhiyun })
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun * v4l2_device_mask_has_op - checks if any subdev with matching group
542*4882a593Smuzhiyun * mask has a given ops.
543*4882a593Smuzhiyun *
544*4882a593Smuzhiyun * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
545*4882a593Smuzhiyun * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
546*4882a593Smuzhiyun * group ID to be matched. Use 0 to match them all.
547*4882a593Smuzhiyun * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
548*4882a593Smuzhiyun * Each element there groups a set of operations functions.
549*4882a593Smuzhiyun * @f: operation function that will be called if @cond matches.
550*4882a593Smuzhiyun * The operation functions are defined in groups, according to
551*4882a593Smuzhiyun * each element at &struct v4l2_subdev_ops.
552*4882a593Smuzhiyun */
553*4882a593Smuzhiyun #define v4l2_device_mask_has_op(v4l2_dev, grpmsk, o, f) \
554*4882a593Smuzhiyun ({ \
555*4882a593Smuzhiyun struct v4l2_subdev *__sd; \
556*4882a593Smuzhiyun bool __result = false; \
557*4882a593Smuzhiyun list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
558*4882a593Smuzhiyun if ((grpmsk) && !(__sd->grp_id & (grpmsk))) \
559*4882a593Smuzhiyun continue; \
560*4882a593Smuzhiyun if (v4l2_subdev_has_op(__sd, o, f)) { \
561*4882a593Smuzhiyun __result = true; \
562*4882a593Smuzhiyun break; \
563*4882a593Smuzhiyun } \
564*4882a593Smuzhiyun } \
565*4882a593Smuzhiyun __result; \
566*4882a593Smuzhiyun })
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun #endif
569