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 #include <linux/types.h>
22 #include <linux/ioctl.h>
23 #include <linux/i2c.h>
24 #if defined(CONFIG_SPI)
25 #include <linux/spi/spi.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ctrls.h>
31 int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
36 INIT_LIST_HEAD(&v4l2_dev->subdevs);
37 spin_lock_init(&v4l2_dev->lock);
38 mutex_init(&v4l2_dev->ioctl_lock);
41 /* If dev == NULL, then name must be filled in by the caller */
42 WARN_ON(!v4l2_dev->name[0]);
46 /* Set name to driver name + device name if it is empty. */
47 if (!v4l2_dev->name[0])
48 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
49 dev->driver->name, dev_name(dev));
50 if (!dev_get_drvdata(dev))
51 dev_set_drvdata(dev, v4l2_dev);
54 EXPORT_SYMBOL_GPL(v4l2_device_register);
56 int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
59 int num = atomic_inc_return(instance) - 1;
60 int len = strlen(basename);
62 if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
63 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
64 "%s-%d", basename, num);
66 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
67 "%s%d", basename, num);
70 EXPORT_SYMBOL_GPL(v4l2_device_set_name);
72 void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
74 if (v4l2_dev->dev == NULL)
77 if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
78 dev_set_drvdata(v4l2_dev->dev, NULL);
81 EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
83 void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
85 struct v4l2_subdev *sd, *next;
89 v4l2_device_disconnect(v4l2_dev);
91 /* Unregister subdevs */
92 list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
93 v4l2_device_unregister_subdev(sd);
94 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
95 if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
96 struct i2c_client *client = v4l2_get_subdevdata(sd);
98 /* We need to unregister the i2c client explicitly.
99 We cannot rely on i2c_del_adapter to always
100 unregister clients for us, since if the i2c bus
101 is a platform bus, then it is never deleted. */
103 i2c_unregister_device(client);
107 #if defined(CONFIG_SPI)
108 if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
109 struct spi_device *spi = v4l2_get_subdevdata(sd);
112 spi_unregister_device(spi);
118 EXPORT_SYMBOL_GPL(v4l2_device_unregister);
120 int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
121 struct v4l2_subdev *sd)
125 /* Check for valid input */
126 if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
129 /* Warn if we apparently re-register a subdev */
130 WARN_ON(sd->v4l2_dev != NULL);
132 if (!try_module_get(sd->owner))
135 sd->v4l2_dev = v4l2_dev;
136 if (sd->internal_ops && sd->internal_ops->registered) {
137 err = sd->internal_ops->registered(sd);
142 /* This just returns 0 if either of the two args is NULL */
143 err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
145 if (sd->internal_ops && sd->internal_ops->unregistered)
146 sd->internal_ops->unregistered(sd);
150 spin_lock(&v4l2_dev->lock);
151 list_add_tail(&sd->list, &v4l2_dev->subdevs);
152 spin_unlock(&v4l2_dev->lock);
156 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
158 int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
160 struct video_device *vdev;
161 struct v4l2_subdev *sd;
164 /* Register a device node for every subdev marked with the
165 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
167 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
168 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
172 strlcpy(vdev->name, sd->name, sizeof(vdev->name));
173 vdev->v4l2_dev = v4l2_dev;
174 vdev->fops = &v4l2_subdev_fops;
175 vdev->release = video_device_release_empty;
176 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
184 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
186 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
188 /* return if it isn't registered */
189 if (sd == NULL || sd->v4l2_dev == NULL)
192 spin_lock(&sd->v4l2_dev->lock);
194 spin_unlock(&sd->v4l2_dev->lock);
195 if (sd->internal_ops && sd->internal_ops->unregistered)
196 sd->internal_ops->unregistered(sd);
199 video_unregister_device(&sd->devnode);
200 module_put(sd->owner);
202 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);