blob: 5b0a30b9252be2a205fcef2feecaf6fd1f3e57f6 [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 Verkuil0f62fd62011-02-24 10:42:24 -030040 v4l2_prio_init(&v4l2_dev->prio);
Hans Verkuilbedf8bc2011-03-12 06:37:19 -030041 kref_init(&v4l2_dev->ref);
Dave Young236c5442011-09-06 09:08:08 -030042 get_device(dev);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030043 v4l2_dev->dev = dev;
Hans Verkuil3a63e4492009-02-14 11:54:23 -030044 if (dev == NULL) {
45 /* If dev == NULL, then name must be filled in by the caller */
Hans Verkuil9e882e32013-06-12 06:04:04 -030046 if (WARN_ON(!v4l2_dev->name[0]))
47 return -EINVAL;
Hans Verkuil3a63e4492009-02-14 11:54:23 -030048 return 0;
49 }
50
51 /* Set name to driver name + device name if it is empty. */
52 if (!v4l2_dev->name[0])
53 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
Kay Sieversc3ef01c2009-03-24 16:38:22 -070054 dev->driver->name, dev_name(dev));
Laurent Pinchart95db3a62009-12-09 08:40:05 -030055 if (!dev_get_drvdata(dev))
56 dev_set_drvdata(dev, v4l2_dev);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -030057 return 0;
58}
59EXPORT_SYMBOL_GPL(v4l2_device_register);
60
Hans Verkuilbedf8bc2011-03-12 06:37:19 -030061static void v4l2_device_release(struct kref *ref)
62{
63 struct v4l2_device *v4l2_dev =
64 container_of(ref, struct v4l2_device, ref);
65
66 if (v4l2_dev->release)
67 v4l2_dev->release(v4l2_dev);
68}
69
70int v4l2_device_put(struct v4l2_device *v4l2_dev)
71{
72 return kref_put(&v4l2_dev->ref, v4l2_device_release);
73}
74EXPORT_SYMBOL_GPL(v4l2_device_put);
75
Hans Verkuil102e7812009-05-02 10:12:50 -030076int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
77 atomic_t *instance)
78{
79 int num = atomic_inc_return(instance) - 1;
80 int len = strlen(basename);
81
82 if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
83 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
84 "%s-%d", basename, num);
85 else
86 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
87 "%s%d", basename, num);
88 return num;
89}
90EXPORT_SYMBOL_GPL(v4l2_device_set_name);
91
Hans Verkuilae6cfaa2009-03-14 08:28:45 -030092void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
93{
Laurent Pinchart95db3a62009-12-09 08:40:05 -030094 if (v4l2_dev->dev == NULL)
95 return;
96
97 if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
Hans Verkuilae6cfaa2009-03-14 08:28:45 -030098 dev_set_drvdata(v4l2_dev->dev, NULL);
Dave Young236c5442011-09-06 09:08:08 -030099 put_device(v4l2_dev->dev);
Laurent Pinchart95db3a62009-12-09 08:40:05 -0300100 v4l2_dev->dev = NULL;
Hans Verkuilae6cfaa2009-03-14 08:28:45 -0300101}
102EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
103
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300104void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
105{
106 struct v4l2_subdev *sd, *next;
107
Hans Verkuil9e882e32013-06-12 06:04:04 -0300108 /* Just return if v4l2_dev is NULL or if it was already
109 * unregistered before. */
110 if (v4l2_dev == NULL || !v4l2_dev->name[0])
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300111 return;
Hans Verkuilae6cfaa2009-03-14 08:28:45 -0300112 v4l2_device_disconnect(v4l2_dev);
113
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300114 /* Unregister subdevs */
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300115 list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300116 v4l2_device_unregister_subdev(sd);
Peter Senna Tschudin7b34be72013-01-20 01:32:56 -0300117#if IS_ENABLED(CONFIG_I2C)
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300118 if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
119 struct i2c_client *client = v4l2_get_subdevdata(sd);
120
121 /* We need to unregister the i2c client explicitly.
122 We cannot rely on i2c_del_adapter to always
123 unregister clients for us, since if the i2c bus
124 is a platform bus, then it is never deleted. */
125 if (client)
126 i2c_unregister_device(client);
Hans Verkuil672dcd52011-01-11 17:48:21 -0300127 continue;
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300128 }
Randy Dunlapb7fd6062009-05-11 13:37:41 -0300129#endif
Dmitri Belimov85e09212010-03-23 11:23:29 -0300130#if defined(CONFIG_SPI)
131 if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
132 struct spi_device *spi = v4l2_get_subdevdata(sd);
133
134 if (spi)
135 spi_unregister_device(spi);
Hans Verkuil672dcd52011-01-11 17:48:21 -0300136 continue;
Dmitri Belimov85e09212010-03-23 11:23:29 -0300137 }
138#endif
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300139 }
Hans Verkuil9e882e32013-06-12 06:04:04 -0300140 /* Mark as unregistered, thus preventing duplicate unregistrations */
141 v4l2_dev->name[0] = '\0';
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300142}
143EXPORT_SYMBOL_GPL(v4l2_device_unregister);
144
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300145int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300146 struct v4l2_subdev *sd)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300147{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300148#if defined(CONFIG_MEDIA_CONTROLLER)
149 struct media_entity *entity = &sd->entity;
150#endif
Hans Verkuil11bbc1c2010-05-16 09:24:06 -0300151 int err;
152
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300153 /* Check for valid input */
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300154 if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300155 return -EINVAL;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300156
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300157 /* Warn if we apparently re-register a subdev */
Hans Verkuilb0167602009-02-14 12:00:53 -0300158 WARN_ON(sd->v4l2_dev != NULL);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300159
Sakari Ailusb2a06ae2013-12-12 09:36:46 -0300160 /*
161 * The reason to acquire the module here is to avoid unloading
162 * a module of sub-device which is registered to a media
163 * device. To make it possible to unload modules for media
164 * devices that also register sub-devices, do not
165 * try_module_get() such sub-device owners.
166 */
167 sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
168 sd->owner == v4l2_dev->dev->driver->owner;
169
170 if (!sd->owner_v4l2_dev && !try_module_get(sd->owner))
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300171 return -ENODEV;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300172
Hans Verkuil45f6f842011-01-08 07:15:53 -0300173 sd->v4l2_dev = v4l2_dev;
174 if (sd->internal_ops && sd->internal_ops->registered) {
175 err = sd->internal_ops->registered(sd);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300176 if (err)
177 goto error_module;
Hans Verkuil45f6f842011-01-08 07:15:53 -0300178 }
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300179
Hans Verkuil11bbc1c2010-05-16 09:24:06 -0300180 /* This just returns 0 if either of the two args is NULL */
Hans Verkuil34a6b7d2012-09-14 07:15:03 -0300181 err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler, NULL);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300182 if (err)
183 goto error_unregister;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300184
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300185#if defined(CONFIG_MEDIA_CONTROLLER)
186 /* Register the entity. */
187 if (v4l2_dev->mdev) {
188 err = media_device_register_entity(v4l2_dev->mdev, entity);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300189 if (err < 0)
190 goto error_unregister;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300191 }
192#endif
193
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300194 spin_lock(&v4l2_dev->lock);
195 list_add_tail(&sd->list, &v4l2_dev->subdevs);
196 spin_unlock(&v4l2_dev->lock);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300197
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300198 return 0;
Laurent Pinchart317efce2012-11-24 21:35:48 -0300199
200error_unregister:
201 if (sd->internal_ops && sd->internal_ops->unregistered)
202 sd->internal_ops->unregistered(sd);
203error_module:
Sakari Ailusb2a06ae2013-12-12 09:36:46 -0300204 if (!sd->owner_v4l2_dev)
205 module_put(sd->owner);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300206 sd->v4l2_dev = NULL;
207 return err;
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300208}
209EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
210
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300211static void v4l2_device_release_subdev_node(struct video_device *vdev)
212{
213 struct v4l2_subdev *sd = video_get_drvdata(vdev);
214 sd->devnode = NULL;
215 kfree(vdev);
216}
217
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300218int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
219{
220 struct video_device *vdev;
221 struct v4l2_subdev *sd;
222 int err;
223
224 /* Register a device node for every subdev marked with the
225 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
226 */
227 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
228 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
229 continue;
230
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300231 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
232 if (!vdev) {
233 err = -ENOMEM;
234 goto clean_up;
235 }
236
237 video_set_drvdata(vdev, sd);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300238 strlcpy(vdev->name, sd->name, sizeof(vdev->name));
239 vdev->v4l2_dev = v4l2_dev;
240 vdev->fops = &v4l2_subdev_fops;
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300241 vdev->release = v4l2_device_release_subdev_node;
Hans Verkuil18d171b2011-05-25 08:52:13 -0300242 vdev->ctrl_handler = sd->ctrl_handler;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300243 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
244 sd->owner);
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300245 if (err < 0) {
246 kfree(vdev);
247 goto clean_up;
248 }
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300249#if defined(CONFIG_MEDIA_CONTROLLER)
Mauro Carvalho Chehabe31a0ba2015-01-02 12:18:23 -0300250 sd->entity.info.dev.major = VIDEO_MAJOR;
251 sd->entity.info.dev.minor = vdev->minor;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300252#endif
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300253 sd->devnode = vdev;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300254 }
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300255 return 0;
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300256
257clean_up:
258 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
259 if (!sd->devnode)
260 break;
261 video_unregister_device(sd->devnode);
262 }
263
264 return err;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300265}
266EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
267
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300268void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
269{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300270 struct v4l2_device *v4l2_dev;
271
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300272 /* return if it isn't registered */
Hans Verkuilb0167602009-02-14 12:00:53 -0300273 if (sd == NULL || sd->v4l2_dev == NULL)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300274 return;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300275
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300276 v4l2_dev = sd->v4l2_dev;
277
278 spin_lock(&v4l2_dev->lock);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300279 list_del(&sd->list);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300280 spin_unlock(&v4l2_dev->lock);
281
Hans Verkuil45f6f842011-01-08 07:15:53 -0300282 if (sd->internal_ops && sd->internal_ops->unregistered)
283 sd->internal_ops->unregistered(sd);
Hans Verkuilb0167602009-02-14 12:00:53 -0300284 sd->v4l2_dev = NULL;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300285
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300286#if defined(CONFIG_MEDIA_CONTROLLER)
Sylwester Nawrockic2efd3e2013-05-09 08:29:33 -0300287 if (v4l2_dev->mdev) {
288 media_entity_remove_links(&sd->entity);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300289 media_device_unregister_entity(&sd->entity);
Sylwester Nawrockic2efd3e2013-05-09 08:29:33 -0300290 }
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300291#endif
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300292 video_unregister_device(sd->devnode);
Sakari Ailusb2a06ae2013-12-12 09:36:46 -0300293 if (!sd->owner_v4l2_dev)
294 module_put(sd->owner);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300295}
296EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);