include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / media / video / v4l2-device.c
blobca9f0da18f4d258b475d1974dca855dfcb38033c
1 /*
2 V4L2 device support.
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/module.h>
24 #include <linux/i2c.h>
25 #if defined(CONFIG_SPI)
26 #include <linux/spi/spi.h>
27 #endif
28 #include <linux/videodev2.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ctrls.h>
32 int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
34 if (v4l2_dev == NULL)
35 return -EINVAL;
37 INIT_LIST_HEAD(&v4l2_dev->subdevs);
38 spin_lock_init(&v4l2_dev->lock);
39 mutex_init(&v4l2_dev->ioctl_lock);
40 v4l2_prio_init(&v4l2_dev->prio);
41 kref_init(&v4l2_dev->ref);
42 v4l2_dev->dev = dev;
43 if (dev == NULL) {
44 /* If dev == NULL, then name must be filled in by the caller */
45 WARN_ON(!v4l2_dev->name[0]);
46 return 0;
49 /* Set name to driver name + device name if it is empty. */
50 if (!v4l2_dev->name[0])
51 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
52 dev->driver->name, dev_name(dev));
53 if (!dev_get_drvdata(dev))
54 dev_set_drvdata(dev, v4l2_dev);
55 return 0;
57 EXPORT_SYMBOL_GPL(v4l2_device_register);
59 static void v4l2_device_release(struct kref *ref)
61 struct v4l2_device *v4l2_dev =
62 container_of(ref, struct v4l2_device, ref);
64 if (v4l2_dev->release)
65 v4l2_dev->release(v4l2_dev);
68 int v4l2_device_put(struct v4l2_device *v4l2_dev)
70 return kref_put(&v4l2_dev->ref, v4l2_device_release);
72 EXPORT_SYMBOL_GPL(v4l2_device_put);
74 int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
75 atomic_t *instance)
77 int num = atomic_inc_return(instance) - 1;
78 int len = strlen(basename);
80 if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
81 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
82 "%s-%d", basename, num);
83 else
84 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
85 "%s%d", basename, num);
86 return num;
88 EXPORT_SYMBOL_GPL(v4l2_device_set_name);
90 void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
92 if (v4l2_dev->dev == NULL)
93 return;
95 if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
96 dev_set_drvdata(v4l2_dev->dev, NULL);
97 v4l2_dev->dev = NULL;
99 EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
101 void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
103 struct v4l2_subdev *sd, *next;
105 if (v4l2_dev == NULL)
106 return;
107 v4l2_device_disconnect(v4l2_dev);
109 /* Unregister subdevs */
110 list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
111 v4l2_device_unregister_subdev(sd);
112 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
113 if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
114 struct i2c_client *client = v4l2_get_subdevdata(sd);
116 /* We need to unregister the i2c client explicitly.
117 We cannot rely on i2c_del_adapter to always
118 unregister clients for us, since if the i2c bus
119 is a platform bus, then it is never deleted. */
120 if (client)
121 i2c_unregister_device(client);
122 continue;
124 #endif
125 #if defined(CONFIG_SPI)
126 if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
127 struct spi_device *spi = v4l2_get_subdevdata(sd);
129 if (spi)
130 spi_unregister_device(spi);
131 continue;
133 #endif
136 EXPORT_SYMBOL_GPL(v4l2_device_unregister);
138 int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
139 struct v4l2_subdev *sd)
141 #if defined(CONFIG_MEDIA_CONTROLLER)
142 struct media_entity *entity = &sd->entity;
143 #endif
144 int err;
146 /* Check for valid input */
147 if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
148 return -EINVAL;
150 /* Warn if we apparently re-register a subdev */
151 WARN_ON(sd->v4l2_dev != NULL);
153 if (!try_module_get(sd->owner))
154 return -ENODEV;
156 sd->v4l2_dev = v4l2_dev;
157 if (sd->internal_ops && sd->internal_ops->registered) {
158 err = sd->internal_ops->registered(sd);
159 if (err) {
160 module_put(sd->owner);
161 return err;
165 /* This just returns 0 if either of the two args is NULL */
166 err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
167 if (err) {
168 if (sd->internal_ops && sd->internal_ops->unregistered)
169 sd->internal_ops->unregistered(sd);
170 module_put(sd->owner);
171 return err;
174 #if defined(CONFIG_MEDIA_CONTROLLER)
175 /* Register the entity. */
176 if (v4l2_dev->mdev) {
177 err = media_device_register_entity(v4l2_dev->mdev, entity);
178 if (err < 0) {
179 if (sd->internal_ops && sd->internal_ops->unregistered)
180 sd->internal_ops->unregistered(sd);
181 module_put(sd->owner);
182 return err;
185 #endif
187 spin_lock(&v4l2_dev->lock);
188 list_add_tail(&sd->list, &v4l2_dev->subdevs);
189 spin_unlock(&v4l2_dev->lock);
191 return 0;
193 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
195 int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
197 struct video_device *vdev;
198 struct v4l2_subdev *sd;
199 int err;
201 /* Register a device node for every subdev marked with the
202 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
204 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
205 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
206 continue;
208 vdev = &sd->devnode;
209 strlcpy(vdev->name, sd->name, sizeof(vdev->name));
210 vdev->v4l2_dev = v4l2_dev;
211 vdev->fops = &v4l2_subdev_fops;
212 vdev->release = video_device_release_empty;
213 vdev->ctrl_handler = sd->ctrl_handler;
214 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
215 sd->owner);
216 if (err < 0)
217 return err;
218 #if defined(CONFIG_MEDIA_CONTROLLER)
219 sd->entity.v4l.major = VIDEO_MAJOR;
220 sd->entity.v4l.minor = vdev->minor;
221 #endif
223 return 0;
225 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
227 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
229 struct v4l2_device *v4l2_dev;
231 /* return if it isn't registered */
232 if (sd == NULL || sd->v4l2_dev == NULL)
233 return;
235 v4l2_dev = sd->v4l2_dev;
237 spin_lock(&v4l2_dev->lock);
238 list_del(&sd->list);
239 spin_unlock(&v4l2_dev->lock);
241 if (sd->internal_ops && sd->internal_ops->unregistered)
242 sd->internal_ops->unregistered(sd);
243 sd->v4l2_dev = NULL;
245 #if defined(CONFIG_MEDIA_CONTROLLER)
246 if (v4l2_dev->mdev)
247 media_device_unregister_entity(&sd->entity);
248 #endif
249 video_unregister_device(&sd->devnode);
250 module_put(sd->owner);
252 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);