blob: ae46753c90cbe96a2bb359eca45fa27d9f576284 [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 *
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030022 * --
23 *
24 * Generic media device node infrastructure to register and unregister
25 * character devices using a dynamic major number and proper reference
26 * counting.
27 */
28
Sachin Kamat3c16b2b2012-09-24 08:26:24 -030029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030031#include <linux/errno.h>
32#include <linux/init.h>
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/kmod.h>
36#include <linux/slab.h>
37#include <linux/mm.h>
38#include <linux/string.h>
39#include <linux/types.h>
40#include <linux/uaccess.h>
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030041
42#include <media/media-devnode.h>
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -030043#include <media/media-device.h>
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030044
45#define MEDIA_NUM_DEVICES 256
46#define MEDIA_NAME "media"
47
48static dev_t media_dev_t;
49
50/*
51 * Active devices
52 */
53static DEFINE_MUTEX(media_devnode_lock);
54static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
55
56/* Called when the last user of the media device exits. */
57static void media_devnode_release(struct device *cd)
58{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030059 struct media_devnode *devnode = to_media_devnode(cd);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030060
61 mutex_lock(&media_devnode_lock);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030062 /* Mark device node number as free */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030063 clear_bit(devnode->minor, media_devnode_nums);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030064 mutex_unlock(&media_devnode_lock);
65
66 /* Release media_devnode and perform other cleanups as needed. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030067 if (devnode->release)
68 devnode->release(devnode);
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -030069
70 kfree(devnode);
Shuah Khan5b28dde2016-05-04 16:48:28 -030071 pr_debug("%s: Media Devnode Deallocated\n", __func__);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030072}
73
74static struct bus_type media_bus_type = {
75 .name = MEDIA_NAME,
76};
77
78static ssize_t media_read(struct file *filp, char __user *buf,
79 size_t sz, loff_t *off)
80{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030081 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030082
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030083 if (!devnode->fops->read)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030084 return -EINVAL;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030085 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030086 return -EIO;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030087 return devnode->fops->read(filp, buf, sz, off);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030088}
89
90static ssize_t media_write(struct file *filp, const char __user *buf,
91 size_t sz, loff_t *off)
92{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030093 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030094
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030095 if (!devnode->fops->write)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030096 return -EINVAL;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030097 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030098 return -EIO;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030099 return devnode->fops->write(filp, buf, sz, off);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300100}
101
102static unsigned int media_poll(struct file *filp,
103 struct poll_table_struct *poll)
104{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300105 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300106
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300107 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300108 return POLLERR | POLLHUP;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300109 if (!devnode->fops->poll)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300110 return DEFAULT_POLLMASK;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300111 return devnode->fops->poll(filp, poll);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300112}
113
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300114static long
115__media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
116 long (*ioctl_func)(struct file *filp, unsigned int cmd,
117 unsigned long arg))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300118{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300119 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300120
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300121 if (!ioctl_func)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300122 return -ENOTTY;
123
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300124 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300125 return -EIO;
126
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300127 return ioctl_func(filp, cmd, arg);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300128}
129
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300130static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
131{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300132 struct media_devnode *devnode = media_devnode_data(filp);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300133
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300134 return __media_ioctl(filp, cmd, arg, devnode->fops->ioctl);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300135}
136
137#ifdef CONFIG_COMPAT
138
139static long media_compat_ioctl(struct file *filp, unsigned int cmd,
140 unsigned long arg)
141{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300142 struct media_devnode *devnode = media_devnode_data(filp);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300143
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300144 return __media_ioctl(filp, cmd, arg, devnode->fops->compat_ioctl);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300145}
146
147#endif /* CONFIG_COMPAT */
148
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300149/* Override for the open function */
150static int media_open(struct inode *inode, struct file *filp)
151{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300152 struct media_devnode *devnode;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300153 int ret;
154
155 /* Check if the media device is available. This needs to be done with
156 * the media_devnode_lock held to prevent an open/unregister race:
157 * without the lock, the device could be unregistered and freed between
158 * the media_devnode_is_registered() and get_device() calls, leading to
159 * a crash.
160 */
161 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300162 devnode = container_of(inode->i_cdev, struct media_devnode, cdev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300163 /* return ENXIO if the media device has been removed
164 already or if it is not registered anymore. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300165 if (!media_devnode_is_registered(devnode)) {
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300166 mutex_unlock(&media_devnode_lock);
167 return -ENXIO;
168 }
169 /* and increase the device refcount */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300170 get_device(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300171 mutex_unlock(&media_devnode_lock);
172
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300173 filp->private_data = devnode;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300174
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300175 if (devnode->fops->open) {
176 ret = devnode->fops->open(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300177 if (ret) {
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300178 put_device(&devnode->dev);
Shuah Khand40ec6f2016-01-27 21:49:33 -0200179 filp->private_data = NULL;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300180 return ret;
181 }
182 }
183
184 return 0;
185}
186
187/* Override for the release function */
188static int media_release(struct inode *inode, struct file *filp)
189{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300190 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300191
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300192 if (devnode->fops->release)
193 devnode->fops->release(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300194
Max Kellermannbf244f62016-03-21 10:30:28 -0300195 filp->private_data = NULL;
196
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300197 /* decrease the refcount unconditionally since the release()
198 return value is ignored. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300199 put_device(&devnode->dev);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300200
201 pr_debug("%s: Media Release\n", __func__);
Mauro Carvalho Chehab8b37c642014-09-03 15:18:27 -0300202 return 0;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300203}
204
205static const struct file_operations media_devnode_fops = {
206 .owner = THIS_MODULE,
207 .read = media_read,
208 .write = media_write,
209 .open = media_open,
210 .unlocked_ioctl = media_ioctl,
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300211#ifdef CONFIG_COMPAT
212 .compat_ioctl = media_compat_ioctl,
213#endif /* CONFIG_COMPAT */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300214 .release = media_release,
215 .poll = media_poll,
216 .llseek = no_llseek,
217};
218
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300219int __must_check media_devnode_register(struct media_device *mdev,
220 struct media_devnode *devnode,
Sakari Ailus85de7212013-12-12 12:38:17 -0300221 struct module *owner)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300222{
223 int minor;
224 int ret;
225
226 /* Part 1: Find a free minor number */
227 mutex_lock(&media_devnode_lock);
Laurent Pinchart8c89ddd2011-05-30 15:45:47 -0300228 minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300229 if (minor == MEDIA_NUM_DEVICES) {
230 mutex_unlock(&media_devnode_lock);
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300231 pr_err("could not get a free minor\n");
Shuah Khan5b28dde2016-05-04 16:48:28 -0300232 kfree(devnode);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300233 return -ENFILE;
234 }
235
Laurent Pinchart8c89ddd2011-05-30 15:45:47 -0300236 set_bit(minor, media_devnode_nums);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300237 mutex_unlock(&media_devnode_lock);
238
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300239 devnode->minor = minor;
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300240 devnode->media_dev = mdev;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300241
Shuah Khan5b28dde2016-05-04 16:48:28 -0300242 /* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300243 devnode->dev.bus = &media_bus_type;
244 devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor);
245 devnode->dev.release = media_devnode_release;
246 if (devnode->parent)
247 devnode->dev.parent = devnode->parent;
248 dev_set_name(&devnode->dev, "media%d", devnode->minor);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300249 device_initialize(&devnode->dev);
250
251 /* Part 2: Initialize and register the character device */
252 cdev_init(&devnode->cdev, &media_devnode_fops);
253 devnode->cdev.owner = owner;
254 devnode->cdev.kobj.parent = &devnode->dev.kobj;
255
256 ret = cdev_add(&devnode->cdev, MKDEV(MAJOR(media_dev_t), devnode->minor), 1);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300257 if (ret < 0) {
Shuah Khan5b28dde2016-05-04 16:48:28 -0300258 pr_err("%s: cdev_add failed\n", __func__);
259 goto cdev_add_error;
260 }
261
262 /* Part 3: Add the media device */
263 ret = device_add(&devnode->dev);
264 if (ret < 0) {
265 pr_err("%s: device_add failed\n", __func__);
266 goto device_add_error;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300267 }
268
269 /* Part 4: Activate this minor. The char device can now be used. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300270 set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300271
272 return 0;
273
Shuah Khan5b28dde2016-05-04 16:48:28 -0300274device_add_error:
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300275 cdev_del(&devnode->cdev);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300276cdev_add_error:
277 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300278 clear_bit(devnode->minor, media_devnode_nums);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300279 devnode->media_dev = NULL;
Max Kellermann88336e12016-03-21 04:33:12 -0700280 mutex_unlock(&media_devnode_lock);
281
Shuah Khan5b28dde2016-05-04 16:48:28 -0300282 put_device(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300283 return ret;
284}
285
Shuah Khan6f0dd242016-06-10 14:37:23 -0300286void media_devnode_unregister_prepare(struct media_devnode *devnode)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300287{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300288 /* Check if devnode was ever registered at all */
289 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300290 return;
291
292 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300293 clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Shuah Khan6f0dd242016-06-10 14:37:23 -0300294 mutex_unlock(&media_devnode_lock);
295}
296
297void media_devnode_unregister(struct media_devnode *devnode)
298{
299 mutex_lock(&media_devnode_lock);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300300 /* Delete the cdev on this minor as well */
301 cdev_del(&devnode->cdev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300302 mutex_unlock(&media_devnode_lock);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300303 device_del(&devnode->dev);
304 devnode->media_dev = NULL;
305 put_device(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300306}
307
308/*
309 * Initialise media for linux
310 */
311static int __init media_devnode_init(void)
312{
313 int ret;
314
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300315 pr_info("Linux media interface: v0.10\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300316 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
317 MEDIA_NAME);
318 if (ret < 0) {
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300319 pr_warn("unable to allocate major\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300320 return ret;
321 }
322
323 ret = bus_register(&media_bus_type);
324 if (ret < 0) {
325 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300326 pr_warn("bus_register failed\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300327 return -EIO;
328 }
329
330 return 0;
331}
332
333static void __exit media_devnode_exit(void)
334{
335 bus_unregister(&media_bus_type);
336 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
337}
338
Laurent Pinchartff732d22012-03-12 10:02:47 -0300339subsys_initcall(media_devnode_init);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300340module_exit(media_devnode_exit)
341
342MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
343MODULE_DESCRIPTION("Device node registration for media drivers");
344MODULE_LICENSE("GPL");