blob: d5aa30eeff4ac56252136f967b1cd7f955c9a65e [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
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04007 * Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -03008 * 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
Al Viroc23e0cb2017-07-03 03:02:56 -0400102static __poll_t media_poll(struct file *filp,
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300103 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))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800108 return EPOLLERR | EPOLLHUP;
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
Logan Gunthorpe857313e2017-03-17 12:48:18 -0600251 /* Part 2: Initialize the character device */
Shuah Khan5b28dde2016-05-04 16:48:28 -0300252 cdev_init(&devnode->cdev, &media_devnode_fops);
253 devnode->cdev.owner = owner;
Hans Verkuil13deaec2019-02-21 08:22:41 -0500254 kobject_set_name(&devnode->cdev.kobj, "media%d", devnode->minor);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300255
Logan Gunthorpe857313e2017-03-17 12:48:18 -0600256 /* Part 3: Add the media and char device */
257 ret = cdev_device_add(&devnode->cdev, &devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300258 if (ret < 0) {
Logan Gunthorpe857313e2017-03-17 12:48:18 -0600259 pr_err("%s: cdev_device_add failed\n", __func__);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300260 goto cdev_add_error;
261 }
262
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300263 /* Part 4: Activate this minor. The char device can now be used. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300264 set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300265
266 return 0;
267
Shuah Khan5b28dde2016-05-04 16:48:28 -0300268cdev_add_error:
269 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300270 clear_bit(devnode->minor, media_devnode_nums);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300271 devnode->media_dev = NULL;
Max Kellermann88336e12016-03-21 04:33:12 -0700272 mutex_unlock(&media_devnode_lock);
273
Shuah Khan5b28dde2016-05-04 16:48:28 -0300274 put_device(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300275 return ret;
276}
277
Shuah Khan6f0dd242016-06-10 14:37:23 -0300278void media_devnode_unregister_prepare(struct media_devnode *devnode)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300279{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300280 /* Check if devnode was ever registered at all */
281 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300282 return;
283
284 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300285 clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Shuah Khan6f0dd242016-06-10 14:37:23 -0300286 mutex_unlock(&media_devnode_lock);
287}
288
289void media_devnode_unregister(struct media_devnode *devnode)
290{
291 mutex_lock(&media_devnode_lock);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300292 /* Delete the cdev on this minor as well */
Logan Gunthorpe857313e2017-03-17 12:48:18 -0600293 cdev_device_del(&devnode->cdev, &devnode->dev);
Shuah Khan5b28dde2016-05-04 16:48:28 -0300294 devnode->media_dev = NULL;
Shuah Khan10cb0502019-04-01 20:43:18 -0400295 mutex_unlock(&media_devnode_lock);
296
Shuah Khan5b28dde2016-05-04 16:48:28 -0300297 put_device(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300298}
299
300/*
301 * Initialise media for linux
302 */
303static int __init media_devnode_init(void)
304{
305 int ret;
306
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300307 pr_info("Linux media interface: v0.10\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300308 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
309 MEDIA_NAME);
310 if (ret < 0) {
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300311 pr_warn("unable to allocate major\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300312 return ret;
313 }
314
315 ret = bus_register(&media_bus_type);
316 if (ret < 0) {
317 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300318 pr_warn("bus_register failed\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300319 return -EIO;
320 }
321
322 return 0;
323}
324
325static void __exit media_devnode_exit(void)
326{
327 bus_unregister(&media_bus_type);
328 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
329}
330
Laurent Pinchartff732d22012-03-12 10:02:47 -0300331subsys_initcall(media_devnode_init);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300332module_exit(media_devnode_exit)
333
334MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
335MODULE_DESCRIPTION("Device node registration for media drivers");
336MODULE_LICENSE("GPL");