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>
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
)
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
);
44 /* If dev == NULL, then name must be filled in by the caller */
45 WARN_ON(!v4l2_dev
->name
[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
);
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
,
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
);
84 snprintf(v4l2_dev
->name
, sizeof(v4l2_dev
->name
),
85 "%s%d", basename
, 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
)
95 if (dev_get_drvdata(v4l2_dev
->dev
) == v4l2_dev
)
96 dev_set_drvdata(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
)
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. */
121 i2c_unregister_device(client
);
125 #if defined(CONFIG_SPI)
126 if (sd
->flags
& V4L2_SUBDEV_FL_IS_SPI
) {
127 struct spi_device
*spi
= v4l2_get_subdevdata(sd
);
130 spi_unregister_device(spi
);
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
;
146 /* Check for valid input */
147 if (v4l2_dev
== NULL
|| sd
== NULL
|| !sd
->name
[0])
150 /* Warn if we apparently re-register a subdev */
151 WARN_ON(sd
->v4l2_dev
!= NULL
);
153 if (!try_module_get(sd
->owner
))
156 sd
->v4l2_dev
= v4l2_dev
;
157 if (sd
->internal_ops
&& sd
->internal_ops
->registered
) {
158 err
= sd
->internal_ops
->registered(sd
);
160 module_put(sd
->owner
);
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
);
168 if (sd
->internal_ops
&& sd
->internal_ops
->unregistered
)
169 sd
->internal_ops
->unregistered(sd
);
170 module_put(sd
->owner
);
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
);
179 if (sd
->internal_ops
&& sd
->internal_ops
->unregistered
)
180 sd
->internal_ops
->unregistered(sd
);
181 module_put(sd
->owner
);
187 spin_lock(&v4l2_dev
->lock
);
188 list_add_tail(&sd
->list
, &v4l2_dev
->subdevs
);
189 spin_unlock(&v4l2_dev
->lock
);
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
;
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
))
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,
218 #if defined(CONFIG_MEDIA_CONTROLLER)
219 sd
->entity
.v4l
.major
= VIDEO_MAJOR
;
220 sd
->entity
.v4l
.minor
= vdev
->minor
;
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
)
235 v4l2_dev
= sd
->v4l2_dev
;
237 spin_lock(&v4l2_dev
->lock
);
239 spin_unlock(&v4l2_dev
->lock
);
241 if (sd
->internal_ops
&& sd
->internal_ops
->unregistered
)
242 sd
->internal_ops
->unregistered(sd
);
245 #if defined(CONFIG_MEDIA_CONTROLLER)
247 media_device_unregister_entity(&sd
->entity
);
249 video_unregister_device(&sd
->devnode
);
250 module_put(sd
->owner
);
252 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev
);