blob: f2772ba6f61163f59d25c31b9b1b4cf16d824856 [file] [log] [blame]
Laurent Pinchartcf4b9212009-12-09 08:39:56 -03001/*
2 * Media device node
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 *
6 * Based on drivers/media/video/v4l2_dev.c code authored by
7 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
8 * Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
9 *
10 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 * Sakari Ailus <sakari.ailus@iki.fi>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * --
27 *
28 * Generic media device node infrastructure to register and unregister
29 * character devices using a dynamic major number and proper reference
30 * counting.
31 */
32
Sachin Kamat3c16b2b2012-09-24 08:26:24 -030033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030035#include <linux/errno.h>
36#include <linux/init.h>
37#include <linux/module.h>
38#include <linux/kernel.h>
39#include <linux/kmod.h>
40#include <linux/slab.h>
41#include <linux/mm.h>
42#include <linux/string.h>
43#include <linux/types.h>
44#include <linux/uaccess.h>
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030045
46#include <media/media-devnode.h>
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -030047#include <media/media-device.h>
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030048
49#define MEDIA_NUM_DEVICES 256
50#define MEDIA_NAME "media"
51
52static dev_t media_dev_t;
53
54/*
55 * Active devices
56 */
57static DEFINE_MUTEX(media_devnode_lock);
58static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
59
60/* Called when the last user of the media device exits. */
61static void media_devnode_release(struct device *cd)
62{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030063 struct media_devnode *devnode = to_media_devnode(cd);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030064
65 mutex_lock(&media_devnode_lock);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030066 /* Mark device node number as free */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030067 clear_bit(devnode->minor, media_devnode_nums);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030068 mutex_unlock(&media_devnode_lock);
69
70 /* Release media_devnode and perform other cleanups as needed. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030071 if (devnode->release)
72 devnode->release(devnode);
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -030073
74 kfree(devnode);
Shuah Khan5b28dde2016-05-04 16:48:28 -030075 pr_debug("%s: Media Devnode Deallocated\n", __func__);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030076}
77
78static struct bus_type media_bus_type = {
79 .name = MEDIA_NAME,
80};
81
82static ssize_t media_read(struct file *filp, char __user *buf,
83 size_t sz, loff_t *off)
84{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030085 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030086
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030087 if (!devnode->fops->read)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030088 return -EINVAL;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030089 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030090 return -EIO;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030091 return devnode->fops->read(filp, buf, sz, off);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030092}
93
94static ssize_t media_write(struct file *filp, const char __user *buf,
95 size_t sz, loff_t *off)
96{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030097 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030098
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030099 if (!devnode->fops->write)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300100 return -EINVAL;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300101 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300102 return -EIO;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300103 return devnode->fops->write(filp, buf, sz, off);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300104}
105
106static unsigned int media_poll(struct file *filp,
107 struct poll_table_struct *poll)
108{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300109 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300110
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300111 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300112 return POLLERR | POLLHUP;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300113 if (!devnode->fops->poll)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300114 return DEFAULT_POLLMASK;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300115 return devnode->fops->poll(filp, poll);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300116}
117
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300118static long
119__media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
120 long (*ioctl_func)(struct file *filp, unsigned int cmd,
121 unsigned long arg))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300122{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300123 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300124
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300125 if (!ioctl_func)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300126 return -ENOTTY;
127
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300128 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300129 return -EIO;
130
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300131 return ioctl_func(filp, cmd, arg);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300132}
133
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300134static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
135{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300136 struct media_devnode *devnode = media_devnode_data(filp);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300137
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300138 return __media_ioctl(filp, cmd, arg, devnode->fops->ioctl);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300139}
140
141#ifdef CONFIG_COMPAT
142
143static long media_compat_ioctl(struct file *filp, unsigned int cmd,
144 unsigned long arg)
145{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300146 struct media_devnode *devnode = media_devnode_data(filp);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300147
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300148 return __media_ioctl(filp, cmd, arg, devnode->fops->compat_ioctl);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300149}
150
151#endif /* CONFIG_COMPAT */
152
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300153/* Override for the open function */
154static int media_open(struct inode *inode, struct file *filp)
155{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300156 struct media_devnode *devnode;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300157 int ret;
158
159 /* Check if the media device is available. This needs to be done with
160 * the media_devnode_lock held to prevent an open/unregister race:
161 * without the lock, the device could be unregistered and freed between
162 * the media_devnode_is_registered() and get_device() calls, leading to
163 * a crash.
164 */
165 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300166 devnode = container_of(inode->i_cdev, struct media_devnode, cdev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300167 /* return ENXIO if the media device has been removed
168 already or if it is not registered anymore. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300169 if (!media_devnode_is_registered(devnode)) {
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300170 mutex_unlock(&media_devnode_lock);
171 return -ENXIO;
172 }
173 /* and increase the device refcount */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300174 get_device(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300175 mutex_unlock(&media_devnode_lock);
176
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300177 filp->private_data = devnode;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300178
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300179 if (devnode->fops->open) {
180 ret = devnode->fops->open(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300181 if (ret) {
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300182 put_device(&devnode->dev);
Shuah Khand40ec6f2016-01-27 21:49:33 -0200183 filp->private_data = NULL;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300184 return ret;
185 }
186 }
187
188 return 0;
189}
190
191/* Override for the release function */
192static int media_release(struct inode *inode, struct file *filp)
193{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300194 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300195
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300196 if (devnode->fops->release)
197 devnode->fops->release(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300198
Max Kellermannbf244f62016-03-21 10:30:28 -0300199 filp->private_data = NULL;
200
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300201 /* decrease the refcount unconditionally since the release()
202 return value is ignored. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300203 put_device(&devnode->dev);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300204
205 pr_debug("%s: Media Release\n", __func__);
Mauro Carvalho Chehab8b37c642014-09-03 15:18:27 -0300206 return 0;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300207}
208
209static const struct file_operations media_devnode_fops = {
210 .owner = THIS_MODULE,
211 .read = media_read,
212 .write = media_write,
213 .open = media_open,
214 .unlocked_ioctl = media_ioctl,
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300215#ifdef CONFIG_COMPAT
216 .compat_ioctl = media_compat_ioctl,
217#endif /* CONFIG_COMPAT */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300218 .release = media_release,
219 .poll = media_poll,
220 .llseek = no_llseek,
221};
222
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300223int __must_check media_devnode_register(struct media_device *mdev,
224 struct media_devnode *devnode,
Sakari Ailus85de7212013-12-12 12:38:17 -0300225 struct module *owner)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300226{
227 int minor;
228 int ret;
229
230 /* Part 1: Find a free minor number */
231 mutex_lock(&media_devnode_lock);
Laurent Pinchart8c89ddd2011-05-30 15:45:47 -0300232 minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300233 if (minor == MEDIA_NUM_DEVICES) {
234 mutex_unlock(&media_devnode_lock);
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300235 pr_err("could not get a free minor\n");
Shuah Khan5b28dde2016-05-04 16:48:28 -0300236 kfree(devnode);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300237 return -ENFILE;
238 }
239
Laurent Pinchart8c89ddd2011-05-30 15:45:47 -0300240 set_bit(minor, media_devnode_nums);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300241 mutex_unlock(&media_devnode_lock);
242
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300243 devnode->minor = minor;
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300244 devnode->media_dev = mdev;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300245
Shuah Khan5b28dde2016-05-04 16:48:28 -0300246 /* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300247 devnode->dev.bus = &media_bus_type;
248 devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor);
249 devnode->dev.release = media_devnode_release;
250 if (devnode->parent)
251 devnode->dev.parent = devnode->parent;
252 dev_set_name(&devnode->dev, "media%d", devnode->minor);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300253 device_initialize(&devnode->dev);
254
255 /* Part 2: Initialize and register the character device */
256 cdev_init(&devnode->cdev, &media_devnode_fops);
257 devnode->cdev.owner = owner;
258 devnode->cdev.kobj.parent = &devnode->dev.kobj;
259
260 ret = cdev_add(&devnode->cdev, MKDEV(MAJOR(media_dev_t), devnode->minor), 1);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300261 if (ret < 0) {
Shuah Khan5b28dde2016-05-04 16:48:28 -0300262 pr_err("%s: cdev_add failed\n", __func__);
263 goto cdev_add_error;
264 }
265
266 /* Part 3: Add the media device */
267 ret = device_add(&devnode->dev);
268 if (ret < 0) {
269 pr_err("%s: device_add failed\n", __func__);
270 goto device_add_error;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300271 }
272
273 /* Part 4: Activate this minor. The char device can now be used. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300274 set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300275
276 return 0;
277
Shuah Khan5b28dde2016-05-04 16:48:28 -0300278device_add_error:
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300279 cdev_del(&devnode->cdev);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300280cdev_add_error:
281 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300282 clear_bit(devnode->minor, media_devnode_nums);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300283 devnode->media_dev = NULL;
Max Kellermann88336e12016-03-21 04:33:12 -0700284 mutex_unlock(&media_devnode_lock);
285
Shuah Khan5b28dde2016-05-04 16:48:28 -0300286 put_device(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300287 return ret;
288}
289
Shuah Khan6f0dd242016-06-10 14:37:23 -0300290void media_devnode_unregister_prepare(struct media_devnode *devnode)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300291{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300292 /* Check if devnode was ever registered at all */
293 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300294 return;
295
296 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300297 clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Shuah Khan6f0dd242016-06-10 14:37:23 -0300298 mutex_unlock(&media_devnode_lock);
299}
300
301void media_devnode_unregister(struct media_devnode *devnode)
302{
303 mutex_lock(&media_devnode_lock);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300304 /* Delete the cdev on this minor as well */
305 cdev_del(&devnode->cdev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300306 mutex_unlock(&media_devnode_lock);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300307 device_del(&devnode->dev);
308 devnode->media_dev = NULL;
309 put_device(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300310}
311
312/*
313 * Initialise media for linux
314 */
315static int __init media_devnode_init(void)
316{
317 int ret;
318
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300319 pr_info("Linux media interface: v0.10\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300320 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
321 MEDIA_NAME);
322 if (ret < 0) {
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300323 pr_warn("unable to allocate major\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300324 return ret;
325 }
326
327 ret = bus_register(&media_bus_type);
328 if (ret < 0) {
329 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300330 pr_warn("bus_register failed\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300331 return -EIO;
332 }
333
334 return 0;
335}
336
337static void __exit media_devnode_exit(void)
338{
339 bus_unregister(&media_bus_type);
340 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
341}
342
Laurent Pinchartff732d22012-03-12 10:02:47 -0300343subsys_initcall(media_devnode_init);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300344module_exit(media_devnode_exit)
345
346MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
347MODULE_DESCRIPTION("Device node registration for media drivers");
348MODULE_LICENSE("GPL");