blob: ecdc02d6ed83c1ca26184b3d66d62ebad22e87b5 [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);
66
67 /* Delete the cdev on this minor as well */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030068 cdev_del(&devnode->cdev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030069
70 /* Mark device node number as free */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030071 clear_bit(devnode->minor, media_devnode_nums);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030072
73 mutex_unlock(&media_devnode_lock);
74
75 /* Release media_devnode and perform other cleanups as needed. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030076 if (devnode->release)
77 devnode->release(devnode);
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -030078
79 kfree(devnode);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030080}
81
82static struct bus_type media_bus_type = {
83 .name = MEDIA_NAME,
84};
85
86static ssize_t media_read(struct file *filp, char __user *buf,
87 size_t sz, loff_t *off)
88{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030089 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030090
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030091 if (!devnode->fops->read)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030092 return -EINVAL;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030093 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030094 return -EIO;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -030095 return devnode->fops->read(filp, buf, sz, off);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -030096}
97
98static ssize_t media_write(struct file *filp, const char __user *buf,
99 size_t sz, loff_t *off)
100{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300101 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300102
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300103 if (!devnode->fops->write)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300104 return -EINVAL;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300105 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300106 return -EIO;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300107 return devnode->fops->write(filp, buf, sz, off);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300108}
109
110static unsigned int media_poll(struct file *filp,
111 struct poll_table_struct *poll)
112{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300113 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300114
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300115 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300116 return POLLERR | POLLHUP;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300117 if (!devnode->fops->poll)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300118 return DEFAULT_POLLMASK;
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300119 return devnode->fops->poll(filp, poll);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300120}
121
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300122static long
123__media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
124 long (*ioctl_func)(struct file *filp, unsigned int cmd,
125 unsigned long arg))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300126{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300127 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300128
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300129 if (!ioctl_func)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300130 return -ENOTTY;
131
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300132 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300133 return -EIO;
134
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300135 return ioctl_func(filp, cmd, arg);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300136}
137
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300138static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
139{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300140 struct media_devnode *devnode = media_devnode_data(filp);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300141
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300142 return __media_ioctl(filp, cmd, arg, devnode->fops->ioctl);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300143}
144
145#ifdef CONFIG_COMPAT
146
147static long media_compat_ioctl(struct file *filp, unsigned int cmd,
148 unsigned long arg)
149{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300150 struct media_devnode *devnode = media_devnode_data(filp);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300151
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300152 return __media_ioctl(filp, cmd, arg, devnode->fops->compat_ioctl);
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300153}
154
155#endif /* CONFIG_COMPAT */
156
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300157/* Override for the open function */
158static int media_open(struct inode *inode, struct file *filp)
159{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300160 struct media_devnode *devnode;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300161 int ret;
162
163 /* Check if the media device is available. This needs to be done with
164 * the media_devnode_lock held to prevent an open/unregister race:
165 * without the lock, the device could be unregistered and freed between
166 * the media_devnode_is_registered() and get_device() calls, leading to
167 * a crash.
168 */
169 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300170 devnode = container_of(inode->i_cdev, struct media_devnode, cdev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300171 /* return ENXIO if the media device has been removed
172 already or if it is not registered anymore. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300173 if (!media_devnode_is_registered(devnode)) {
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300174 mutex_unlock(&media_devnode_lock);
175 return -ENXIO;
176 }
177 /* and increase the device refcount */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300178 get_device(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300179 mutex_unlock(&media_devnode_lock);
180
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300181 filp->private_data = devnode;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300182
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300183 if (devnode->fops->open) {
184 ret = devnode->fops->open(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300185 if (ret) {
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300186 put_device(&devnode->dev);
Shuah Khand40ec6f2016-01-27 21:49:33 -0200187 filp->private_data = NULL;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300188 return ret;
189 }
190 }
191
192 return 0;
193}
194
195/* Override for the release function */
196static int media_release(struct inode *inode, struct file *filp)
197{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300198 struct media_devnode *devnode = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300199
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300200 if (devnode->fops->release)
201 devnode->fops->release(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300202
Max Kellermannbf244f62016-03-21 10:30:28 -0300203 filp->private_data = NULL;
204
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300205 /* decrease the refcount unconditionally since the release()
206 return value is ignored. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300207 put_device(&devnode->dev);
Mauro Carvalho Chehab8b37c642014-09-03 15:18:27 -0300208 return 0;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300209}
210
211static const struct file_operations media_devnode_fops = {
212 .owner = THIS_MODULE,
213 .read = media_read,
214 .write = media_write,
215 .open = media_open,
216 .unlocked_ioctl = media_ioctl,
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300217#ifdef CONFIG_COMPAT
218 .compat_ioctl = media_compat_ioctl,
219#endif /* CONFIG_COMPAT */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300220 .release = media_release,
221 .poll = media_poll,
222 .llseek = no_llseek,
223};
224
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300225int __must_check media_devnode_register(struct media_device *mdev,
226 struct media_devnode *devnode,
Sakari Ailus85de7212013-12-12 12:38:17 -0300227 struct module *owner)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300228{
229 int minor;
230 int ret;
231
232 /* Part 1: Find a free minor number */
233 mutex_lock(&media_devnode_lock);
Laurent Pinchart8c89ddd2011-05-30 15:45:47 -0300234 minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300235 if (minor == MEDIA_NUM_DEVICES) {
236 mutex_unlock(&media_devnode_lock);
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300237 pr_err("could not get a free minor\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300238 return -ENFILE;
239 }
240
Laurent Pinchart8c89ddd2011-05-30 15:45:47 -0300241 set_bit(minor, media_devnode_nums);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300242 mutex_unlock(&media_devnode_lock);
243
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300244 devnode->minor = minor;
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300245 devnode->media_dev = mdev;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300246
247 /* Part 2: Initialize and register the character device */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300248 cdev_init(&devnode->cdev, &media_devnode_fops);
249 devnode->cdev.owner = owner;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300250
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300251 ret = cdev_add(&devnode->cdev, MKDEV(MAJOR(media_dev_t), devnode->minor), 1);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300252 if (ret < 0) {
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300253 pr_err("%s: cdev_add failed\n", __func__);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300254 goto error;
255 }
256
257 /* Part 3: Register the media device */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300258 devnode->dev.bus = &media_bus_type;
259 devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor);
260 devnode->dev.release = media_devnode_release;
261 if (devnode->parent)
262 devnode->dev.parent = devnode->parent;
263 dev_set_name(&devnode->dev, "media%d", devnode->minor);
264 ret = device_register(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300265 if (ret < 0) {
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300266 pr_err("%s: device_register failed\n", __func__);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300267 goto error;
268 }
269
270 /* Part 4: Activate this minor. The char device can now be used. */
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300271 set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300272
273 return 0;
274
275error:
Max Kellermann88336e12016-03-21 04:33:12 -0700276 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300277 cdev_del(&devnode->cdev);
278 clear_bit(devnode->minor, media_devnode_nums);
Max Kellermann88336e12016-03-21 04:33:12 -0700279 mutex_unlock(&media_devnode_lock);
280
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300281 return ret;
282}
283
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300284void media_devnode_unregister(struct media_devnode *devnode)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300285{
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300286 /* Check if devnode was ever registered at all */
287 if (!media_devnode_is_registered(devnode))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300288 return;
289
290 mutex_lock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300291 clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300292 mutex_unlock(&media_devnode_lock);
Mauro Carvalho Chehab163f1e92016-03-23 11:22:57 -0300293 device_unregister(&devnode->dev);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300294}
295
296/*
297 * Initialise media for linux
298 */
299static int __init media_devnode_init(void)
300{
301 int ret;
302
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300303 pr_info("Linux media interface: v0.10\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300304 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
305 MEDIA_NAME);
306 if (ret < 0) {
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300307 pr_warn("unable to allocate major\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300308 return ret;
309 }
310
311 ret = bus_register(&media_bus_type);
312 if (ret < 0) {
313 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300314 pr_warn("bus_register failed\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300315 return -EIO;
316 }
317
318 return 0;
319}
320
321static void __exit media_devnode_exit(void)
322{
323 bus_unregister(&media_bus_type);
324 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
325}
326
Laurent Pinchartff732d22012-03-12 10:02:47 -0300327subsys_initcall(media_devnode_init);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300328module_exit(media_devnode_exit)
329
330MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
331MODULE_DESCRIPTION("Device node registration for media drivers");
332MODULE_LICENSE("GPL");