2 V4L2 device support header.
4 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef _V4L2_DEVICE_H
22 #define _V4L2_DEVICE_H
24 #include <media/media-device.h>
25 #include <media/v4l2-subdev.h>
26 #include <media/v4l2-dev.h>
28 #define V4L2_DEVICE_NAME_SIZE (20 + 16)
30 struct v4l2_ctrl_handler
;
33 * struct v4l2_device - main struct to for V4L2 device drivers
35 * @dev: pointer to struct device.
36 * @mdev: pointer to struct media_device
37 * @subdevs: used to keep track of the registered subdevs
38 * @lock: lock this struct; can be used by the driver as well
39 * if this struct is embedded into a larger struct.
40 * @name: unique device name, by default the driver name + bus ID
41 * @notify: notify operation called by some sub-devices.
42 * @ctrl_handler: The control handler. May be %NULL.
43 * @prio: Device's priority state
44 * @ref: Keep track of the references to this struct.
45 * @release: Release function that is called when the ref count
48 * Each instance of a V4L2 device should create the v4l2_device struct,
49 * either stand-alone or embedded in a larger struct.
51 * It allows easy access to sub-devices (see v4l2-subdev.h) and provides
52 * basic V4L2 device-level support.
56 * #) @dev->driver_data points to this struct.
57 * #) @dev might be %NULL if there is no parent device
61 #if defined(CONFIG_MEDIA_CONTROLLER)
62 struct media_device
*mdev
;
64 struct list_head subdevs
;
66 char name
[V4L2_DEVICE_NAME_SIZE
];
67 void (*notify
)(struct v4l2_subdev
*sd
,
68 unsigned int notification
, void *arg
);
69 struct v4l2_ctrl_handler
*ctrl_handler
;
70 struct v4l2_prio_state prio
;
72 void (*release
)(struct v4l2_device
*v4l2_dev
);
76 * v4l2_device_get - gets a V4L2 device reference
78 * @v4l2_dev: pointer to struct &v4l2_device
80 * This is an ancillary routine meant to increment the usage for the
81 * struct &v4l2_device pointed by @v4l2_dev.
83 static inline void v4l2_device_get(struct v4l2_device
*v4l2_dev
)
85 kref_get(&v4l2_dev
->ref
);
89 * v4l2_device_put - putss a V4L2 device reference
91 * @v4l2_dev: pointer to struct &v4l2_device
93 * This is an ancillary routine meant to decrement the usage for the
94 * struct &v4l2_device pointed by @v4l2_dev.
96 int v4l2_device_put(struct v4l2_device
*v4l2_dev
);
99 * v4l2_device_register - Initialize v4l2_dev and make @dev->driver_data
100 * point to @v4l2_dev.
102 * @dev: pointer to struct &device
103 * @v4l2_dev: pointer to struct &v4l2_device
106 * @dev may be %NULL in rare cases (ISA devices).
107 * In such case the caller must fill in the @v4l2_dev->name field
108 * before calling this function.
110 int __must_check
v4l2_device_register(struct device
*dev
,
111 struct v4l2_device
*v4l2_dev
);
114 * v4l2_device_set_name - Optional function to initialize the
115 * name field of struct &v4l2_device
117 * @v4l2_dev: pointer to struct &v4l2_device
118 * @basename: base name for the device name
119 * @instance: pointer to a static atomic_t var with the instance usage for
122 * v4l2_device_set_name() initializes the name field of struct &v4l2_device
123 * using the driver name and a driver-global atomic_t instance.
125 * This function will increment the instance counter and returns the
126 * instance value used in the name.
130 * static atomic_t drv_instance = ATOMIC_INIT(0);
134 * instance = v4l2_device_set_name(&\ v4l2_dev, "foo", &\ drv_instance);
136 * The first time this is called the name field will be set to foo0 and
137 * this function returns 0. If the name ends with a digit (e.g. cx18),
138 * then the name will be set to cx18-0 since cx180 would look really odd.
140 int v4l2_device_set_name(struct v4l2_device
*v4l2_dev
, const char *basename
,
144 * v4l2_device_disconnect - Change V4L2 device state to disconnected.
146 * @v4l2_dev: pointer to struct v4l2_device
148 * Should be called when the USB parent disconnects.
149 * Since the parent disappears, this ensures that @v4l2_dev doesn't have
150 * an invalid parent pointer.
152 * .. note:: This function sets @v4l2_dev->dev to NULL.
154 void v4l2_device_disconnect(struct v4l2_device
*v4l2_dev
);
157 * v4l2_device_unregister - Unregister all sub-devices and any other
158 * resources related to @v4l2_dev.
160 * @v4l2_dev: pointer to struct v4l2_device
162 void v4l2_device_unregister(struct v4l2_device
*v4l2_dev
);
165 * v4l2_device_register_subdev - Registers a subdev with a v4l2 device.
167 * @v4l2_dev: pointer to struct &v4l2_device
168 * @sd: pointer to &struct v4l2_subdev
170 * While registered, the subdev module is marked as in-use.
172 * An error is returned if the module is no longer loaded on any attempts
175 int __must_check
v4l2_device_register_subdev(struct v4l2_device
*v4l2_dev
,
176 struct v4l2_subdev
*sd
);
179 * v4l2_device_unregister_subdev - Unregisters a subdev with a v4l2 device.
181 * @sd: pointer to &struct v4l2_subdev
185 * Can also be called if the subdev wasn't registered. In such
186 * case, it will do nothing.
188 void v4l2_device_unregister_subdev(struct v4l2_subdev
*sd
);
191 * v4l2_device_register_subdev_nodes - Registers device nodes for all subdevs
192 * of the v4l2 device that are marked with
193 * the %V4L2_SUBDEV_FL_HAS_DEVNODE flag.
195 * @v4l2_dev: pointer to struct v4l2_device
198 v4l2_device_register_subdev_nodes(struct v4l2_device
*v4l2_dev
);
201 * v4l2_subdev_notify - Sends a notification to v4l2_device.
203 * @sd: pointer to &struct v4l2_subdev
204 * @notification: type of notification. Please notice that the notification
205 * type is driver-specific.
206 * @arg: arguments for the notification. Those are specific to each
209 static inline void v4l2_subdev_notify(struct v4l2_subdev
*sd
,
210 unsigned int notification
, void *arg
)
212 if (sd
&& sd
->v4l2_dev
&& sd
->v4l2_dev
->notify
)
213 sd
->v4l2_dev
->notify(sd
, notification
, arg
);
216 /* Helper macros to iterate over all subdevs. */
219 * v4l2_device_for_each_subdev - Helper macro that interates over all
220 * sub-devices of a given &v4l2_device.
222 * @sd: pointer that will be filled by the macro with all
223 * &struct v4l2_subdev pointer used as an iterator by the loop.
224 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
226 * This macro iterates over all sub-devices owned by the @v4l2_dev device.
227 * It acts as a for loop iterator and executes the next statement with
228 * the @sd variable pointing to each sub-device in turn.
230 #define v4l2_device_for_each_subdev(sd, v4l2_dev) \
231 list_for_each_entry(sd, &(v4l2_dev)->subdevs, list)
234 * __v4l2_device_call_subdevs_p - Calls the specified operation for
235 * all subdevs matching the condition.
237 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
238 * @sd: pointer that will be filled by the macro with all
239 * &struct v4l2_subdev pointer used as an iterator by the loop.
240 * @cond: condition to be match
241 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
242 * Each element there groups a set of operations functions.
243 * @f: operation function that will be called if @cond matches.
244 * The operation functions are defined in groups, according to
245 * each element at &struct v4l2_subdev_ops.
246 * @args...: arguments for @f.
250 * Note: subdevs cannot be added or deleted while walking
253 #define __v4l2_device_call_subdevs_p(v4l2_dev, sd, cond, o, f, args...) \
255 list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) \
256 if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
257 (sd)->ops->o->f((sd) , ##args); \
261 * __v4l2_device_call_subdevs - Calls the specified operation for
262 * all subdevs matching the condition.
264 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
265 * @cond: condition to be match
266 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
267 * Each element there groups a set of operations functions.
268 * @f: operation function that will be called if @cond matches.
269 * The operation functions are defined in groups, according to
270 * each element at &struct v4l2_subdev_ops.
271 * @args...: arguments for @f.
275 * Note: subdevs cannot be added or deleted while walking
278 #define __v4l2_device_call_subdevs(v4l2_dev, cond, o, f, args...) \
280 struct v4l2_subdev *__sd; \
282 __v4l2_device_call_subdevs_p(v4l2_dev, __sd, cond, o, \
287 * __v4l2_device_call_subdevs_until_err_p - Calls the specified operation for
288 * all subdevs matching the condition.
290 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
291 * @sd: pointer that will be filled by the macro with all
292 * &struct v4l2_subdev sub-devices associated with @v4l2_dev.
293 * @cond: condition to be match
294 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
295 * Each element there groups a set of operations functions.
296 * @f: operation function that will be called if @cond matches.
297 * The operation functions are defined in groups, according to
298 * each element at &struct v4l2_subdev_ops.
299 * @args...: arguments for @f.
303 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
304 * for any subdevice, then abort and return with that error code, zero
307 * Note: subdevs cannot be added or deleted while walking
310 #define __v4l2_device_call_subdevs_until_err_p(v4l2_dev, sd, cond, o, f, args...) \
314 list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) { \
315 if ((cond) && (sd)->ops->o && (sd)->ops->o->f) \
316 __err = (sd)->ops->o->f((sd) , ##args); \
317 if (__err && __err != -ENOIOCTLCMD) \
320 (__err == -ENOIOCTLCMD) ? 0 : __err; \
324 * __v4l2_device_call_subdevs_until_err - 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 * @cond: condition to be match
329 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
330 * Each element there groups a set of operations functions.
331 * @f: operation function that will be called if @cond matches.
332 * The operation functions are defined in groups, according to
333 * each element at &struct v4l2_subdev_ops.
334 * @args...: arguments for @f.
338 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
339 * for any subdevice, then abort and return with that error code,
342 * Note: subdevs cannot be added or deleted while walking
345 #define __v4l2_device_call_subdevs_until_err(v4l2_dev, cond, o, f, args...) \
347 struct v4l2_subdev *__sd; \
348 __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, cond, o, \
353 * v4l2_device_call_all - Calls the specified operation for
354 * all subdevs matching the &v4l2_subdev.grp_id, as assigned
355 * by the bridge driver.
357 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
358 * @grpid: &struct v4l2_subdev->grp_id group ID to match.
359 * Use 0 to match them all.
360 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
361 * Each element there groups a set of operations functions.
362 * @f: operation function that will be called if @cond matches.
363 * The operation functions are defined in groups, according to
364 * each element at &struct v4l2_subdev_ops.
365 * @args...: arguments for @f.
369 * Note: subdevs cannot be added or deleted while walking
372 #define v4l2_device_call_all(v4l2_dev, grpid, o, f, args...) \
374 struct v4l2_subdev *__sd; \
376 __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
377 !(grpid) || __sd->grp_id == (grpid), o, f , \
382 * v4l2_device_call_until_err - Calls the specified operation for
383 * all subdevs matching the &v4l2_subdev.grp_id, as assigned
384 * by the bridge driver, until an error occurs.
386 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
387 * @grpid: &struct v4l2_subdev->grp_id group ID to match.
388 * Use 0 to match them all.
389 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
390 * Each element there groups a set of operations functions.
391 * @f: operation function that will be called if @cond matches.
392 * The operation functions are defined in groups, according to
393 * each element at &struct v4l2_subdev_ops.
394 * @args...: arguments for @f.
398 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
399 * for any subdevice, then abort and return with that error code,
402 * Note: subdevs cannot be added or deleted while walking
405 #define v4l2_device_call_until_err(v4l2_dev, grpid, o, f, args...) \
407 struct v4l2_subdev *__sd; \
408 __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
409 !(grpid) || __sd->grp_id == (grpid), o, f , \
414 * v4l2_device_mask_call_all - Calls the specified operation for
415 * all subdevices where a group ID matches a specified bitmask.
417 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
418 * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
419 * group ID to be matched. Use 0 to match them all.
420 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
421 * Each element there groups a set of operations functions.
422 * @f: operation function that will be called if @cond matches.
423 * The operation functions are defined in groups, according to
424 * each element at &struct v4l2_subdev_ops.
425 * @args...: arguments for @f.
429 * Note: subdevs cannot be added or deleted while walking
432 #define v4l2_device_mask_call_all(v4l2_dev, grpmsk, o, f, args...) \
434 struct v4l2_subdev *__sd; \
436 __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
437 !(grpmsk) || (__sd->grp_id & (grpmsk)), o, f , \
442 * v4l2_device_mask_call_until_err - Calls the specified operation for
443 * all subdevices where a group ID matches a specified bitmask.
445 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
446 * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
447 * group ID to be matched. Use 0 to match them all.
448 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
449 * Each element there groups a set of operations functions.
450 * @f: operation function that will be called if @cond matches.
451 * The operation functions are defined in groups, according to
452 * each element at &struct v4l2_subdev_ops.
453 * @args...: arguments for @f.
457 * If the operation returns an error other than 0 or ``-ENOIOCTLCMD``
458 * for any subdevice, then abort and return with that error code,
461 * Note: subdevs cannot be added or deleted while walking
464 #define v4l2_device_mask_call_until_err(v4l2_dev, grpmsk, o, f, args...) \
466 struct v4l2_subdev *__sd; \
467 __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
468 !(grpmsk) || (__sd->grp_id & (grpmsk)), o, f , \
474 * v4l2_device_has_op - checks if any subdev with matching grpid has a
477 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
478 * @grpid: &struct v4l2_subdev->grp_id group ID to match.
479 * Use 0 to match them all.
480 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
481 * Each element there groups a set of operations functions.
482 * @f: operation function that will be called if @cond matches.
483 * The operation functions are defined in groups, according to
484 * each element at &struct v4l2_subdev_ops.
486 #define v4l2_device_has_op(v4l2_dev, grpid, o, f) \
488 struct v4l2_subdev *__sd; \
489 bool __result = false; \
490 list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
491 if ((grpid) && __sd->grp_id != (grpid)) \
493 if (v4l2_subdev_has_op(__sd, o, f)) { \
502 * v4l2_device_mask_has_op - checks if any subdev with matching group
503 * mask has a given ops.
505 * @v4l2_dev: &struct v4l2_device owning the sub-devices to iterate over.
506 * @grpmsk: bitmask to be checked against &struct v4l2_subdev->grp_id
507 * group ID to be matched. Use 0 to match them all.
508 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
509 * Each element there groups a set of operations functions.
510 * @f: operation function that will be called if @cond matches.
511 * The operation functions are defined in groups, according to
512 * each element at &struct v4l2_subdev_ops.
514 #define v4l2_device_mask_has_op(v4l2_dev, grpmsk, o, f) \
516 struct v4l2_subdev *__sd; \
517 bool __result = false; \
518 list_for_each_entry(__sd, &(v4l2_dev)->subdevs, list) { \
519 if ((grpmsk) && !(__sd->grp_id & (grpmsk))) \
521 if (v4l2_subdev_has_op(__sd, o, f)) { \