xref: /linux/include/media/v4l2-device.h (revision 6c2d4dd139de417d18151b98c157aa35387038a3)
12a1fcdf0SHans Verkuil /*
22a1fcdf0SHans Verkuil     V4L2 device support header.
32a1fcdf0SHans Verkuil 
42a1fcdf0SHans Verkuil     Copyright (C) 2008  Hans Verkuil <hverkuil@xs4all.nl>
52a1fcdf0SHans Verkuil 
62a1fcdf0SHans Verkuil     This program is free software; you can redistribute it and/or modify
72a1fcdf0SHans Verkuil     it under the terms of the GNU General Public License as published by
82a1fcdf0SHans Verkuil     the Free Software Foundation; either version 2 of the License, or
92a1fcdf0SHans Verkuil     (at your option) any later version.
102a1fcdf0SHans Verkuil 
112a1fcdf0SHans Verkuil     This program is distributed in the hope that it will be useful,
122a1fcdf0SHans Verkuil     but WITHOUT ANY WARRANTY; without even the implied warranty of
132a1fcdf0SHans Verkuil     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
142a1fcdf0SHans Verkuil     GNU General Public License for more details.
152a1fcdf0SHans Verkuil 
162a1fcdf0SHans Verkuil     You should have received a copy of the GNU General Public License
172a1fcdf0SHans Verkuil     along with this program; if not, write to the Free Software
182a1fcdf0SHans Verkuil     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
192a1fcdf0SHans Verkuil  */
202a1fcdf0SHans Verkuil 
212a1fcdf0SHans Verkuil #ifndef _V4L2_DEVICE_H
222a1fcdf0SHans Verkuil #define _V4L2_DEVICE_H
232a1fcdf0SHans Verkuil 
242a1fcdf0SHans Verkuil #include <media/v4l2-subdev.h>
252a1fcdf0SHans Verkuil 
262a1fcdf0SHans Verkuil /* Each instance of a V4L2 device should create the v4l2_device struct,
272a1fcdf0SHans Verkuil    either stand-alone or embedded in a larger struct.
282a1fcdf0SHans Verkuil 
292a1fcdf0SHans Verkuil    It allows easy access to sub-devices (see v4l2-subdev.h) and provides
302a1fcdf0SHans Verkuil    basic V4L2 device-level support.
312a1fcdf0SHans Verkuil  */
322a1fcdf0SHans Verkuil 
331351a58cSKay Sievers #define V4L2_DEVICE_NAME_SIZE (20 + 16)
342a1fcdf0SHans Verkuil 
350996517cSHans Verkuil struct v4l2_ctrl_handler;
360996517cSHans Verkuil 
372a1fcdf0SHans Verkuil struct v4l2_device {
383a63e449SHans Verkuil 	/* dev->driver_data points to this struct.
393a63e449SHans Verkuil 	   Note: dev might be NULL if there is no parent device
403a63e449SHans Verkuil 	   as is the case with e.g. ISA devices. */
412a1fcdf0SHans Verkuil 	struct device *dev;
422a1fcdf0SHans Verkuil 	/* used to keep track of the registered subdevs */
432a1fcdf0SHans Verkuil 	struct list_head subdevs;
442a1fcdf0SHans Verkuil 	/* lock this struct; can be used by the driver as well if this
452a1fcdf0SHans Verkuil 	   struct is embedded into a larger struct. */
462a1fcdf0SHans Verkuil 	spinlock_t lock;
472a1fcdf0SHans Verkuil 	/* unique device name, by default the driver name + bus ID */
482a1fcdf0SHans Verkuil 	char name[V4L2_DEVICE_NAME_SIZE];
4998ec6339SHans Verkuil 	/* notify callback called by some sub-devices. */
5098ec6339SHans Verkuil 	void (*notify)(struct v4l2_subdev *sd,
5198ec6339SHans Verkuil 			unsigned int notification, void *arg);
520996517cSHans Verkuil 	/* The control handler. May be NULL. */
530996517cSHans Verkuil 	struct v4l2_ctrl_handler *ctrl_handler;
542a1fcdf0SHans Verkuil };
552a1fcdf0SHans Verkuil 
563a63e449SHans Verkuil /* Initialize v4l2_dev and make dev->driver_data point to v4l2_dev.
573a63e449SHans Verkuil    dev may be NULL in rare cases (ISA devices). In that case you
583a63e449SHans Verkuil    must fill in the v4l2_dev->name field before calling this function. */
592a1fcdf0SHans Verkuil int __must_check v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev);
60102e7813SHans Verkuil 
61102e7813SHans Verkuil /* Optional function to initialize the name field of struct v4l2_device using
62102e7813SHans Verkuil    the driver name and a driver-global atomic_t instance.
63102e7813SHans Verkuil    This function will increment the instance counter and returns the instance
64102e7813SHans Verkuil    value used in the name.
65102e7813SHans Verkuil 
66102e7813SHans Verkuil    Example:
67102e7813SHans Verkuil 
68102e7813SHans Verkuil    static atomic_t drv_instance = ATOMIC_INIT(0);
69102e7813SHans Verkuil 
70102e7813SHans Verkuil    ...
71102e7813SHans Verkuil 
72102e7813SHans Verkuil    instance = v4l2_device_set_name(&v4l2_dev, "foo", &drv_instance);
73102e7813SHans Verkuil 
74102e7813SHans Verkuil    The first time this is called the name field will be set to foo0 and
75102e7813SHans Verkuil    this function returns 0. If the name ends with a digit (e.g. cx18),
76102e7813SHans Verkuil    then the name will be set to cx18-0 since cx180 looks really odd. */
77102e7813SHans Verkuil int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
78102e7813SHans Verkuil 						atomic_t *instance);
79102e7813SHans Verkuil 
80ae6cfaacSHans Verkuil /* Set v4l2_dev->dev to NULL. Call when the USB parent disconnects.
81ae6cfaacSHans Verkuil    Since the parent disappears this ensures that v4l2_dev doesn't have an
82ae6cfaacSHans Verkuil    invalid parent pointer. */
83ae6cfaacSHans Verkuil void v4l2_device_disconnect(struct v4l2_device *v4l2_dev);
84102e7813SHans Verkuil 
85ae6cfaacSHans Verkuil /* Unregister all sub-devices and any other resources related to v4l2_dev. */
862a1fcdf0SHans Verkuil void v4l2_device_unregister(struct v4l2_device *v4l2_dev);
872a1fcdf0SHans Verkuil 
882a1fcdf0SHans Verkuil /* Register a subdev with a v4l2 device. While registered the subdev module
892a1fcdf0SHans Verkuil    is marked as in-use. An error is returned if the module is no longer
902a1fcdf0SHans Verkuil    loaded when you attempt to register it. */
913a63e449SHans Verkuil int __must_check v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
923a63e449SHans Verkuil 						struct v4l2_subdev *sd);
932a1fcdf0SHans Verkuil /* Unregister a subdev with a v4l2 device. Can also be called if the subdev
942a1fcdf0SHans Verkuil    wasn't registered. In that case it will do nothing. */
952a1fcdf0SHans Verkuil void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
962a1fcdf0SHans Verkuil 
972a1fcdf0SHans Verkuil /* Iterate over all subdevs. */
983a63e449SHans Verkuil #define v4l2_device_for_each_subdev(sd, v4l2_dev)			\
993a63e449SHans Verkuil 	list_for_each_entry(sd, &(v4l2_dev)->subdevs, list)
1002a1fcdf0SHans Verkuil 
1012a1fcdf0SHans Verkuil /* Call the specified callback for all subdevs matching the condition.
1022a1fcdf0SHans Verkuil    Ignore any errors. Note that you cannot add or delete a subdev
1032a1fcdf0SHans Verkuil    while walking the subdevs list. */
104*6c2d4dd1SGuennadi Liakhovetski #define __v4l2_device_call_subdevs_p(v4l2_dev, sd, cond, o, f, args...)	\
105*6c2d4dd1SGuennadi Liakhovetski 	do { 								\
106*6c2d4dd1SGuennadi Liakhovetski 		list_for_each_entry((sd), &(v4l2_dev)->subdevs, list)	\
107*6c2d4dd1SGuennadi Liakhovetski 			if ((cond) && (sd)->ops->o && (sd)->ops->o->f)	\
108*6c2d4dd1SGuennadi Liakhovetski 				(sd)->ops->o->f((sd) , ##args);		\
109*6c2d4dd1SGuennadi Liakhovetski 	} while (0)
110*6c2d4dd1SGuennadi Liakhovetski 
1113a63e449SHans Verkuil #define __v4l2_device_call_subdevs(v4l2_dev, cond, o, f, args...)	\
1122a1fcdf0SHans Verkuil 	do {								\
113*6c2d4dd1SGuennadi Liakhovetski 		struct v4l2_subdev *__sd;				\
1142a1fcdf0SHans Verkuil 									\
115*6c2d4dd1SGuennadi Liakhovetski 		__v4l2_device_call_subdevs_p(v4l2_dev, __sd, cond, o,	\
116*6c2d4dd1SGuennadi Liakhovetski 						f , ##args);		\
1172a1fcdf0SHans Verkuil 	} while (0)
1182a1fcdf0SHans Verkuil 
1192a1fcdf0SHans Verkuil /* Call the specified callback for all subdevs matching the condition.
1202a1fcdf0SHans Verkuil    If the callback returns an error other than 0 or -ENOIOCTLCMD, then
1212a1fcdf0SHans Verkuil    return with that error code. Note that you cannot add or delete a
1222a1fcdf0SHans Verkuil    subdev while walking the subdevs list. */
123*6c2d4dd1SGuennadi Liakhovetski #define __v4l2_device_call_subdevs_until_err_p(v4l2_dev, sd, cond, o, f, args...) \
1242a1fcdf0SHans Verkuil ({ 									\
125*6c2d4dd1SGuennadi Liakhovetski 	long __err = 0;							\
1262a1fcdf0SHans Verkuil 									\
127*6c2d4dd1SGuennadi Liakhovetski 	list_for_each_entry((sd), &(v4l2_dev)->subdevs, list) {		\
128*6c2d4dd1SGuennadi Liakhovetski 		if ((cond) && (sd)->ops->o && (sd)->ops->o->f)		\
129*6c2d4dd1SGuennadi Liakhovetski 			__err = (sd)->ops->o->f((sd) , ##args);		\
130*6c2d4dd1SGuennadi Liakhovetski 		if (__err && __err != -ENOIOCTLCMD)			\
1312a1fcdf0SHans Verkuil 			break; 						\
1322a1fcdf0SHans Verkuil 	} 								\
133*6c2d4dd1SGuennadi Liakhovetski 	(__err == -ENOIOCTLCMD) ? 0 : __err;				\
134*6c2d4dd1SGuennadi Liakhovetski })
135*6c2d4dd1SGuennadi Liakhovetski 
136*6c2d4dd1SGuennadi Liakhovetski #define __v4l2_device_call_subdevs_until_err(v4l2_dev, cond, o, f, args...) \
137*6c2d4dd1SGuennadi Liakhovetski ({									\
138*6c2d4dd1SGuennadi Liakhovetski 	struct v4l2_subdev *__sd;					\
139*6c2d4dd1SGuennadi Liakhovetski 	__v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, cond, o,	\
140*6c2d4dd1SGuennadi Liakhovetski 						f, args...);		\
1412a1fcdf0SHans Verkuil })
1422a1fcdf0SHans Verkuil 
1432a1fcdf0SHans Verkuil /* Call the specified callback for all subdevs matching grp_id (if 0, then
1442a1fcdf0SHans Verkuil    match them all). Ignore any errors. Note that you cannot add or delete
1452a1fcdf0SHans Verkuil    a subdev while walking the subdevs list. */
1463a63e449SHans Verkuil #define v4l2_device_call_all(v4l2_dev, grpid, o, f, args...)		\
147*6c2d4dd1SGuennadi Liakhovetski 	do {								\
148*6c2d4dd1SGuennadi Liakhovetski 		struct v4l2_subdev *__sd;				\
149*6c2d4dd1SGuennadi Liakhovetski 									\
150*6c2d4dd1SGuennadi Liakhovetski 		__v4l2_device_call_subdevs_p(v4l2_dev, __sd,		\
151*6c2d4dd1SGuennadi Liakhovetski 			!(grpid) || __sd->grp_id == (grpid), o, f ,	\
152*6c2d4dd1SGuennadi Liakhovetski 			##args);					\
153*6c2d4dd1SGuennadi Liakhovetski 	} while (0)
1542a1fcdf0SHans Verkuil 
1552a1fcdf0SHans Verkuil /* Call the specified callback for all subdevs matching grp_id (if 0, then
1562a1fcdf0SHans Verkuil    match them all). If the callback returns an error other than 0 or
1572a1fcdf0SHans Verkuil    -ENOIOCTLCMD, then return with that error code. Note that you cannot
1582a1fcdf0SHans Verkuil    add or delete a subdev while walking the subdevs list. */
1593a63e449SHans Verkuil #define v4l2_device_call_until_err(v4l2_dev, grpid, o, f, args...) 	\
160*6c2d4dd1SGuennadi Liakhovetski ({									\
161*6c2d4dd1SGuennadi Liakhovetski 	struct v4l2_subdev *__sd;					\
162*6c2d4dd1SGuennadi Liakhovetski 	__v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd,		\
163*6c2d4dd1SGuennadi Liakhovetski 			!(grpid) || __sd->grp_id == (grpid), o, f ,	\
164*6c2d4dd1SGuennadi Liakhovetski 			##args);					\
165*6c2d4dd1SGuennadi Liakhovetski })
1662a1fcdf0SHans Verkuil 
1672a1fcdf0SHans Verkuil #endif
168