blob: 015f92aab44a85b602c0ae58b3632fe7661015cf [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>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040023#include <linux/module.h>
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030024#include <linux/i2c.h>
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -030025#include <linux/slab.h>
Dmitri Belimov85e09212010-03-23 11:23:29 -030026#if defined(CONFIG_SPI)
27#include <linux/spi/spi.h>
28#endif
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030029#include <linux/videodev2.h>
30#include <media/v4l2-device.h>
Hans Verkuil11bbc1c2010-05-16 09:24:06 -030031#include <media/v4l2-ctrls.h>
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030032
33int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
34{
Hans Verkuil3a63e4492009-02-14 11:54:23 -030035 if (v4l2_dev == NULL)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030036 return -EINVAL;
Hans Verkuil3a63e4492009-02-14 11:54:23 -030037
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030038 INIT_LIST_HEAD(&v4l2_dev->subdevs);
39 spin_lock_init(&v4l2_dev->lock);
Hans Verkuil879aa242010-11-26 06:47:28 -030040 mutex_init(&v4l2_dev->ioctl_lock);
Hans Verkuil0f62fd62011-02-24 10:42:24 -030041 v4l2_prio_init(&v4l2_dev->prio);
Hans Verkuilbedf8bc2011-03-12 06:37:19 -030042 kref_init(&v4l2_dev->ref);
Dave Young236c5442011-09-06 09:08:08 -030043 get_device(dev);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030044 v4l2_dev->dev = dev;
Hans Verkuil3a63e4492009-02-14 11:54:23 -030045 if (dev == NULL) {
46 /* If dev == NULL, then name must be filled in by the caller */
Hans Verkuil9e882e32013-06-12 06:04:04 -030047 if (WARN_ON(!v4l2_dev->name[0]))
48 return -EINVAL;
Hans Verkuil3a63e4492009-02-14 11:54:23 -030049 return 0;
50 }
51
52 /* Set name to driver name + device name if it is empty. */
53 if (!v4l2_dev->name[0])
54 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
Kay Sieversc3ef01c2009-03-24 16:38:22 -070055 dev->driver->name, dev_name(dev));
Laurent Pinchart95db3a62009-12-09 08:40:05 -030056 if (!dev_get_drvdata(dev))
57 dev_set_drvdata(dev, v4l2_dev);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030058 return 0;
59}
60EXPORT_SYMBOL_GPL(v4l2_device_register);
61
Hans Verkuilbedf8bc2011-03-12 06:37:19 -030062static void v4l2_device_release(struct kref *ref)
63{
64 struct v4l2_device *v4l2_dev =
65 container_of(ref, struct v4l2_device, ref);
66
67 if (v4l2_dev->release)
68 v4l2_dev->release(v4l2_dev);
69}
70
71int v4l2_device_put(struct v4l2_device *v4l2_dev)
72{
73 return kref_put(&v4l2_dev->ref, v4l2_device_release);
74}
75EXPORT_SYMBOL_GPL(v4l2_device_put);
76
Hans Verkuil102e7812009-05-02 10:12:50 -030077int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
78 atomic_t *instance)
79{
80 int num = atomic_inc_return(instance) - 1;
81 int len = strlen(basename);
82
83 if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
84 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
85 "%s-%d", basename, num);
86 else
87 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
88 "%s%d", basename, num);
89 return num;
90}
91EXPORT_SYMBOL_GPL(v4l2_device_set_name);
92
Hans Verkuilae6cfaa2009-03-14 08:28:45 -030093void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
94{
Laurent Pinchart95db3a62009-12-09 08:40:05 -030095 if (v4l2_dev->dev == NULL)
96 return;
97
98 if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
Hans Verkuilae6cfaa2009-03-14 08:28:45 -030099 dev_set_drvdata(v4l2_dev->dev, NULL);
Dave Young236c5442011-09-06 09:08:08 -0300100 put_device(v4l2_dev->dev);
Laurent Pinchart95db3a62009-12-09 08:40:05 -0300101 v4l2_dev->dev = NULL;
Hans Verkuilae6cfaa2009-03-14 08:28:45 -0300102}
103EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
104
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300105void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
106{
107 struct v4l2_subdev *sd, *next;
108
Hans Verkuil9e882e32013-06-12 06:04:04 -0300109 /* Just return if v4l2_dev is NULL or if it was already
110 * unregistered before. */
111 if (v4l2_dev == NULL || !v4l2_dev->name[0])
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300112 return;
Hans Verkuilae6cfaa2009-03-14 08:28:45 -0300113 v4l2_device_disconnect(v4l2_dev);
114
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300115 /* Unregister subdevs */
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300116 list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300117 v4l2_device_unregister_subdev(sd);
Peter Senna Tschudin7b34be72013-01-20 01:32:56 -0300118#if IS_ENABLED(CONFIG_I2C)
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300119 if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
120 struct i2c_client *client = v4l2_get_subdevdata(sd);
121
122 /* We need to unregister the i2c client explicitly.
123 We cannot rely on i2c_del_adapter to always
124 unregister clients for us, since if the i2c bus
125 is a platform bus, then it is never deleted. */
126 if (client)
127 i2c_unregister_device(client);
Hans Verkuil672dcd52011-01-11 17:48:21 -0300128 continue;
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300129 }
Randy Dunlapb7fd6062009-05-11 13:37:41 -0300130#endif
Dmitri Belimov85e09212010-03-23 11:23:29 -0300131#if defined(CONFIG_SPI)
132 if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
133 struct spi_device *spi = v4l2_get_subdevdata(sd);
134
135 if (spi)
136 spi_unregister_device(spi);
Hans Verkuil672dcd52011-01-11 17:48:21 -0300137 continue;
Dmitri Belimov85e09212010-03-23 11:23:29 -0300138 }
139#endif
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300140 }
Hans Verkuil9e882e32013-06-12 06:04:04 -0300141 /* Mark as unregistered, thus preventing duplicate unregistrations */
142 v4l2_dev->name[0] = '\0';
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300143}
144EXPORT_SYMBOL_GPL(v4l2_device_unregister);
145
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300146int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300147 struct v4l2_subdev *sd)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300148{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300149#if defined(CONFIG_MEDIA_CONTROLLER)
150 struct media_entity *entity = &sd->entity;
151#endif
Hans Verkuil11bbc1c2010-05-16 09:24:06 -0300152 int err;
153
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300154 /* Check for valid input */
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300155 if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300156 return -EINVAL;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300157
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300158 /* Warn if we apparently re-register a subdev */
Hans Verkuilb0167602009-02-14 12:00:53 -0300159 WARN_ON(sd->v4l2_dev != NULL);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300160
Sakari Ailusb2a06ae2013-12-12 09:36:46 -0300161 /*
162 * The reason to acquire the module here is to avoid unloading
163 * a module of sub-device which is registered to a media
164 * device. To make it possible to unload modules for media
165 * devices that also register sub-devices, do not
166 * try_module_get() such sub-device owners.
167 */
168 sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
169 sd->owner == v4l2_dev->dev->driver->owner;
170
171 if (!sd->owner_v4l2_dev && !try_module_get(sd->owner))
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300172 return -ENODEV;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300173
Hans Verkuil45f6f842011-01-08 07:15:53 -0300174 sd->v4l2_dev = v4l2_dev;
175 if (sd->internal_ops && sd->internal_ops->registered) {
176 err = sd->internal_ops->registered(sd);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300177 if (err)
178 goto error_module;
Hans Verkuil45f6f842011-01-08 07:15:53 -0300179 }
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300180
Hans Verkuil11bbc1c2010-05-16 09:24:06 -0300181 /* This just returns 0 if either of the two args is NULL */
Hans Verkuil34a6b7d2012-09-14 07:15:03 -0300182 err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler, NULL);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300183 if (err)
184 goto error_unregister;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300185
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300186#if defined(CONFIG_MEDIA_CONTROLLER)
187 /* Register the entity. */
188 if (v4l2_dev->mdev) {
189 err = media_device_register_entity(v4l2_dev->mdev, entity);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300190 if (err < 0)
191 goto error_unregister;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300192 }
193#endif
194
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300195 spin_lock(&v4l2_dev->lock);
196 list_add_tail(&sd->list, &v4l2_dev->subdevs);
197 spin_unlock(&v4l2_dev->lock);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300198
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300199 return 0;
Laurent Pinchart317efce2012-11-24 21:35:48 -0300200
201error_unregister:
202 if (sd->internal_ops && sd->internal_ops->unregistered)
203 sd->internal_ops->unregistered(sd);
204error_module:
Sakari Ailusb2a06ae2013-12-12 09:36:46 -0300205 if (!sd->owner_v4l2_dev)
206 module_put(sd->owner);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300207 sd->v4l2_dev = NULL;
208 return err;
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300209}
210EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
211
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300212static void v4l2_device_release_subdev_node(struct video_device *vdev)
213{
214 struct v4l2_subdev *sd = video_get_drvdata(vdev);
215 sd->devnode = NULL;
216 kfree(vdev);
217}
218
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300219int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
220{
221 struct video_device *vdev;
222 struct v4l2_subdev *sd;
223 int err;
224
225 /* Register a device node for every subdev marked with the
226 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
227 */
228 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
229 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
230 continue;
231
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300232 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
233 if (!vdev) {
234 err = -ENOMEM;
235 goto clean_up;
236 }
237
238 video_set_drvdata(vdev, sd);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300239 strlcpy(vdev->name, sd->name, sizeof(vdev->name));
240 vdev->v4l2_dev = v4l2_dev;
241 vdev->fops = &v4l2_subdev_fops;
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300242 vdev->release = v4l2_device_release_subdev_node;
Hans Verkuil18d171b2011-05-25 08:52:13 -0300243 vdev->ctrl_handler = sd->ctrl_handler;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300244 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
245 sd->owner);
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300246 if (err < 0) {
247 kfree(vdev);
248 goto clean_up;
249 }
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300250#if defined(CONFIG_MEDIA_CONTROLLER)
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300251 sd->entity.info.v4l.major = VIDEO_MAJOR;
252 sd->entity.info.v4l.minor = vdev->minor;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300253#endif
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300254 sd->devnode = vdev;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300255 }
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300256 return 0;
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300257
258clean_up:
259 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
260 if (!sd->devnode)
261 break;
262 video_unregister_device(sd->devnode);
263 }
264
265 return err;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300266}
267EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
268
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300269void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
270{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300271 struct v4l2_device *v4l2_dev;
272
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300273 /* return if it isn't registered */
Hans Verkuilb0167602009-02-14 12:00:53 -0300274 if (sd == NULL || sd->v4l2_dev == NULL)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300275 return;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300276
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300277 v4l2_dev = sd->v4l2_dev;
278
279 spin_lock(&v4l2_dev->lock);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300280 list_del(&sd->list);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300281 spin_unlock(&v4l2_dev->lock);
282
Hans Verkuil45f6f842011-01-08 07:15:53 -0300283 if (sd->internal_ops && sd->internal_ops->unregistered)
284 sd->internal_ops->unregistered(sd);
Hans Verkuilb0167602009-02-14 12:00:53 -0300285 sd->v4l2_dev = NULL;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300286
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300287#if defined(CONFIG_MEDIA_CONTROLLER)
Sylwester Nawrockic2efd3e2013-05-09 08:29:33 -0300288 if (v4l2_dev->mdev) {
289 media_entity_remove_links(&sd->entity);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300290 media_device_unregister_entity(&sd->entity);
Sylwester Nawrockic2efd3e2013-05-09 08:29:33 -0300291 }
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300292#endif
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300293 video_unregister_device(sd->devnode);
Sakari Ailusb2a06ae2013-12-12 09:36:46 -0300294 if (!sd->owner_v4l2_dev)
295 module_put(sd->owner);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300296}
297EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);