blob: e6a2c3b302d426319c3f5e143959909f52c52e5f [file] [log] [blame]
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -03001/*
2 V4L2 device support.
3
4 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
5
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.
10
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.
15
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
19 */
20
21#include <linux/types.h>
22#include <linux/ioctl.h>
23#include <linux/i2c.h>
Dmitri Belimov85e09212010-03-23 11:23:29 -030024#if defined(CONFIG_SPI)
25#include <linux/spi/spi.h>
26#endif
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030027#include <linux/videodev2.h>
28#include <media/v4l2-device.h>
Hans Verkuil11bbc1c2010-05-16 09:24:06 -030029#include <media/v4l2-ctrls.h>
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030030
31int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
32{
Hans Verkuil3a63e4492009-02-14 11:54:23 -030033 if (v4l2_dev == NULL)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030034 return -EINVAL;
Hans Verkuil3a63e4492009-02-14 11:54:23 -030035
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030036 INIT_LIST_HEAD(&v4l2_dev->subdevs);
37 spin_lock_init(&v4l2_dev->lock);
Hans Verkuil879aa242010-11-26 06:47:28 -030038 mutex_init(&v4l2_dev->ioctl_lock);
Hans Verkuil0f62fd62011-02-24 10:42:24 -030039 v4l2_prio_init(&v4l2_dev->prio);
Hans Verkuilbedf8bc2011-03-12 06:37:19 -030040 kref_init(&v4l2_dev->ref);
Dave Young236c5442011-09-06 09:08:08 -030041 get_device(dev);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030042 v4l2_dev->dev = dev;
Hans Verkuil3a63e4492009-02-14 11:54:23 -030043 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;
47 }
48
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",
Kay Sieversc3ef01c2009-03-24 16:38:22 -070052 dev->driver->name, dev_name(dev));
Laurent Pinchart95db3a62009-12-09 08:40:05 -030053 if (!dev_get_drvdata(dev))
54 dev_set_drvdata(dev, v4l2_dev);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030055 return 0;
56}
57EXPORT_SYMBOL_GPL(v4l2_device_register);
58
Hans Verkuilbedf8bc2011-03-12 06:37:19 -030059static void v4l2_device_release(struct kref *ref)
60{
61 struct v4l2_device *v4l2_dev =
62 container_of(ref, struct v4l2_device, ref);
63
64 if (v4l2_dev->release)
65 v4l2_dev->release(v4l2_dev);
66}
67
68int v4l2_device_put(struct v4l2_device *v4l2_dev)
69{
70 return kref_put(&v4l2_dev->ref, v4l2_device_release);
71}
72EXPORT_SYMBOL_GPL(v4l2_device_put);
73
Hans Verkuil102e7812009-05-02 10:12:50 -030074int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
75 atomic_t *instance)
76{
77 int num = atomic_inc_return(instance) - 1;
78 int len = strlen(basename);
79
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;
87}
88EXPORT_SYMBOL_GPL(v4l2_device_set_name);
89
Hans Verkuilae6cfaa2009-03-14 08:28:45 -030090void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
91{
Laurent Pinchart95db3a62009-12-09 08:40:05 -030092 if (v4l2_dev->dev == NULL)
93 return;
94
95 if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
Hans Verkuilae6cfaa2009-03-14 08:28:45 -030096 dev_set_drvdata(v4l2_dev->dev, NULL);
Dave Young236c5442011-09-06 09:08:08 -030097 put_device(v4l2_dev->dev);
Laurent Pinchart95db3a62009-12-09 08:40:05 -030098 v4l2_dev->dev = NULL;
Hans Verkuilae6cfaa2009-03-14 08:28:45 -030099}
100EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
101
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300102void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
103{
104 struct v4l2_subdev *sd, *next;
105
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300106 if (v4l2_dev == NULL)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300107 return;
Hans Verkuilae6cfaa2009-03-14 08:28:45 -0300108 v4l2_device_disconnect(v4l2_dev);
109
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300110 /* Unregister subdevs */
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300111 list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300112 v4l2_device_unregister_subdev(sd);
Randy Dunlapef5b5162009-06-10 11:58:22 -0300113#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300114 if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
115 struct i2c_client *client = v4l2_get_subdevdata(sd);
116
117 /* We need to unregister the i2c client explicitly.
118 We cannot rely on i2c_del_adapter to always
119 unregister clients for us, since if the i2c bus
120 is a platform bus, then it is never deleted. */
121 if (client)
122 i2c_unregister_device(client);
Hans Verkuil672dcd52011-01-11 17:48:21 -0300123 continue;
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300124 }
Randy Dunlapb7fd6062009-05-11 13:37:41 -0300125#endif
Dmitri Belimov85e09212010-03-23 11:23:29 -0300126#if defined(CONFIG_SPI)
127 if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
128 struct spi_device *spi = v4l2_get_subdevdata(sd);
129
130 if (spi)
131 spi_unregister_device(spi);
Hans Verkuil672dcd52011-01-11 17:48:21 -0300132 continue;
Dmitri Belimov85e09212010-03-23 11:23:29 -0300133 }
134#endif
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300135 }
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300136}
137EXPORT_SYMBOL_GPL(v4l2_device_unregister);
138
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300139int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300140 struct v4l2_subdev *sd)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300141{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300142#if defined(CONFIG_MEDIA_CONTROLLER)
143 struct media_entity *entity = &sd->entity;
144#endif
Hans Verkuil11bbc1c2010-05-16 09:24:06 -0300145 int err;
146
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300147 /* Check for valid input */
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300148 if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300149 return -EINVAL;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300150
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300151 /* Warn if we apparently re-register a subdev */
Hans Verkuilb0167602009-02-14 12:00:53 -0300152 WARN_ON(sd->v4l2_dev != NULL);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300153
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300154 if (!try_module_get(sd->owner))
155 return -ENODEV;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300156
Hans Verkuil45f6f842011-01-08 07:15:53 -0300157 sd->v4l2_dev = v4l2_dev;
158 if (sd->internal_ops && sd->internal_ops->registered) {
159 err = sd->internal_ops->registered(sd);
Laurent Pinchartb7534f002011-04-30 10:34:05 -0300160 if (err) {
161 module_put(sd->owner);
Hans Verkuil45f6f842011-01-08 07:15:53 -0300162 return err;
Laurent Pinchartb7534f002011-04-30 10:34:05 -0300163 }
Hans Verkuil45f6f842011-01-08 07:15:53 -0300164 }
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300165
Hans Verkuil11bbc1c2010-05-16 09:24:06 -0300166 /* This just returns 0 if either of the two args is NULL */
167 err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
Hans Verkuil45f6f842011-01-08 07:15:53 -0300168 if (err) {
169 if (sd->internal_ops && sd->internal_ops->unregistered)
170 sd->internal_ops->unregistered(sd);
Laurent Pinchartb7534f002011-04-30 10:34:05 -0300171 module_put(sd->owner);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -0300172 return err;
Hans Verkuil45f6f842011-01-08 07:15:53 -0300173 }
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300174
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300175#if defined(CONFIG_MEDIA_CONTROLLER)
176 /* Register the entity. */
177 if (v4l2_dev->mdev) {
178 err = media_device_register_entity(v4l2_dev->mdev, entity);
179 if (err < 0) {
180 if (sd->internal_ops && sd->internal_ops->unregistered)
181 sd->internal_ops->unregistered(sd);
182 module_put(sd->owner);
183 return err;
184 }
185 }
186#endif
187
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300188 spin_lock(&v4l2_dev->lock);
189 list_add_tail(&sd->list, &v4l2_dev->subdevs);
190 spin_unlock(&v4l2_dev->lock);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300191
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300192 return 0;
193}
194EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
195
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300196int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
197{
198 struct video_device *vdev;
199 struct v4l2_subdev *sd;
200 int err;
201
202 /* Register a device node for every subdev marked with the
203 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
204 */
205 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
206 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
207 continue;
208
209 vdev = &sd->devnode;
210 strlcpy(vdev->name, sd->name, sizeof(vdev->name));
211 vdev->v4l2_dev = v4l2_dev;
212 vdev->fops = &v4l2_subdev_fops;
213 vdev->release = video_device_release_empty;
Hans Verkuil18d171b2011-05-25 08:52:13 -0300214 vdev->ctrl_handler = sd->ctrl_handler;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300215 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
216 sd->owner);
217 if (err < 0)
218 return err;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300219#if defined(CONFIG_MEDIA_CONTROLLER)
220 sd->entity.v4l.major = VIDEO_MAJOR;
221 sd->entity.v4l.minor = vdev->minor;
222#endif
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300223 }
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300224 return 0;
225}
226EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
227
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300228void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
229{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300230 struct v4l2_device *v4l2_dev;
231
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300232 /* return if it isn't registered */
Hans Verkuilb0167602009-02-14 12:00:53 -0300233 if (sd == NULL || sd->v4l2_dev == NULL)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300234 return;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300235
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300236 v4l2_dev = sd->v4l2_dev;
237
238 spin_lock(&v4l2_dev->lock);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300239 list_del(&sd->list);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300240 spin_unlock(&v4l2_dev->lock);
241
Hans Verkuil45f6f842011-01-08 07:15:53 -0300242 if (sd->internal_ops && sd->internal_ops->unregistered)
243 sd->internal_ops->unregistered(sd);
Hans Verkuilb0167602009-02-14 12:00:53 -0300244 sd->v4l2_dev = NULL;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300245
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300246#if defined(CONFIG_MEDIA_CONTROLLER)
247 if (v4l2_dev->mdev)
248 media_device_unregister_entity(&sd->entity);
249#endif
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300250 video_unregister_device(&sd->devnode);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300251 module_put(sd->owner);
252}
253EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);