blob: 02d1b6327117b4ba599bed35f56e9c89a6669943 [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
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300161 if (!try_module_get(sd->owner))
162 return -ENODEV;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300163
Hans Verkuil45f6f842011-01-08 07:15:53 -0300164 sd->v4l2_dev = v4l2_dev;
165 if (sd->internal_ops && sd->internal_ops->registered) {
166 err = sd->internal_ops->registered(sd);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300167 if (err)
168 goto error_module;
Hans Verkuil45f6f842011-01-08 07:15:53 -0300169 }
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300170
Hans Verkuil11bbc1c2010-05-16 09:24:06 -0300171 /* This just returns 0 if either of the two args is NULL */
Hans Verkuil34a6b7d2012-09-14 07:15:03 -0300172 err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler, NULL);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300173 if (err)
174 goto error_unregister;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300175
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300176#if defined(CONFIG_MEDIA_CONTROLLER)
177 /* Register the entity. */
178 if (v4l2_dev->mdev) {
179 err = media_device_register_entity(v4l2_dev->mdev, entity);
Laurent Pinchart317efce2012-11-24 21:35:48 -0300180 if (err < 0)
181 goto error_unregister;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300182 }
183#endif
184
Hans Verkuil3a63e4492009-02-14 11:54:23 -0300185 spin_lock(&v4l2_dev->lock);
186 list_add_tail(&sd->list, &v4l2_dev->subdevs);
187 spin_unlock(&v4l2_dev->lock);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300188
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300189 return 0;
Laurent Pinchart317efce2012-11-24 21:35:48 -0300190
191error_unregister:
192 if (sd->internal_ops && sd->internal_ops->unregistered)
193 sd->internal_ops->unregistered(sd);
194error_module:
195 module_put(sd->owner);
196 sd->v4l2_dev = NULL;
197 return err;
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300198}
199EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
200
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300201static void v4l2_device_release_subdev_node(struct video_device *vdev)
202{
203 struct v4l2_subdev *sd = video_get_drvdata(vdev);
204 sd->devnode = NULL;
205 kfree(vdev);
206}
207
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300208int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
209{
210 struct video_device *vdev;
211 struct v4l2_subdev *sd;
212 int err;
213
214 /* Register a device node for every subdev marked with the
215 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
216 */
217 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
218 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
219 continue;
220
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300221 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
222 if (!vdev) {
223 err = -ENOMEM;
224 goto clean_up;
225 }
226
227 video_set_drvdata(vdev, sd);
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300228 strlcpy(vdev->name, sd->name, sizeof(vdev->name));
229 vdev->v4l2_dev = v4l2_dev;
230 vdev->fops = &v4l2_subdev_fops;
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300231 vdev->release = v4l2_device_release_subdev_node;
Hans Verkuil18d171b2011-05-25 08:52:13 -0300232 vdev->ctrl_handler = sd->ctrl_handler;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300233 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
234 sd->owner);
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300235 if (err < 0) {
236 kfree(vdev);
237 goto clean_up;
238 }
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300239#if defined(CONFIG_MEDIA_CONTROLLER)
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300240 sd->entity.info.v4l.major = VIDEO_MAJOR;
241 sd->entity.info.v4l.minor = vdev->minor;
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300242#endif
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300243 sd->devnode = vdev;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300244 }
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300245 return 0;
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300246
247clean_up:
248 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
249 if (!sd->devnode)
250 break;
251 video_unregister_device(sd->devnode);
252 }
253
254 return err;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300255}
256EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
257
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300258void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
259{
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300260 struct v4l2_device *v4l2_dev;
261
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300262 /* return if it isn't registered */
Hans Verkuilb0167602009-02-14 12:00:53 -0300263 if (sd == NULL || sd->v4l2_dev == NULL)
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300264 return;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300265
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300266 v4l2_dev = sd->v4l2_dev;
267
268 spin_lock(&v4l2_dev->lock);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300269 list_del(&sd->list);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300270 spin_unlock(&v4l2_dev->lock);
271
Hans Verkuil45f6f842011-01-08 07:15:53 -0300272 if (sd->internal_ops && sd->internal_ops->unregistered)
273 sd->internal_ops->unregistered(sd);
Hans Verkuilb0167602009-02-14 12:00:53 -0300274 sd->v4l2_dev = NULL;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300275
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300276#if defined(CONFIG_MEDIA_CONTROLLER)
Sylwester Nawrockic2efd3e2013-05-09 08:29:33 -0300277 if (v4l2_dev->mdev) {
278 media_entity_remove_links(&sd->entity);
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300279 media_device_unregister_entity(&sd->entity);
Sylwester Nawrockic2efd3e2013-05-09 08:29:33 -0300280 }
Laurent Pinchart61f5db52009-12-09 08:40:08 -0300281#endif
Guennadi Liakhovetski3e0ec412011-09-13 08:07:55 -0300282 video_unregister_device(sd->devnode);
Hans Verkuil2a1fcdf2008-11-29 21:36:58 -0300283 module_put(sd->owner);
284}
285EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);