1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2010 Nokia Corporation
7 * Based on drivers/media/video/v4l2_dev.c code authored by
8 * Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
9 * Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
11 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12 * Sakari Ailus <sakari.ailus@iki.fi>
16 * Generic media device node infrastructure to register and unregister
17 * character devices using a dynamic major number and proper reference
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/kmod.h>
28 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/types.h>
32 #include <linux/uaccess.h>
34 #include <media/media-devnode.h>
35 #include <media/media-device.h>
37 #define MEDIA_NUM_DEVICES 256
38 #define MEDIA_NAME "media"
40 static dev_t media_dev_t
;
45 static DEFINE_MUTEX(media_devnode_lock
);
46 static DECLARE_BITMAP(media_devnode_nums
, MEDIA_NUM_DEVICES
);
48 /* Called when the last user of the media device exits. */
49 static void media_devnode_release(struct device
*cd
)
51 struct media_devnode
*devnode
= to_media_devnode(cd
);
53 mutex_lock(&media_devnode_lock
);
54 /* Mark device node number as free */
55 clear_bit(devnode
->minor
, media_devnode_nums
);
56 mutex_unlock(&media_devnode_lock
);
58 /* Release media_devnode and perform other cleanups as needed. */
60 devnode
->release(devnode
);
63 pr_debug("%s: Media Devnode Deallocated\n", __func__
);
66 static struct bus_type media_bus_type
= {
70 static ssize_t
media_read(struct file
*filp
, char __user
*buf
,
71 size_t sz
, loff_t
*off
)
73 struct media_devnode
*devnode
= media_devnode_data(filp
);
75 if (!devnode
->fops
->read
)
77 if (!media_devnode_is_registered(devnode
))
79 return devnode
->fops
->read(filp
, buf
, sz
, off
);
82 static ssize_t
media_write(struct file
*filp
, const char __user
*buf
,
83 size_t sz
, loff_t
*off
)
85 struct media_devnode
*devnode
= media_devnode_data(filp
);
87 if (!devnode
->fops
->write
)
89 if (!media_devnode_is_registered(devnode
))
91 return devnode
->fops
->write(filp
, buf
, sz
, off
);
94 static __poll_t
media_poll(struct file
*filp
,
95 struct poll_table_struct
*poll
)
97 struct media_devnode
*devnode
= media_devnode_data(filp
);
99 if (!media_devnode_is_registered(devnode
))
100 return EPOLLERR
| EPOLLHUP
;
101 if (!devnode
->fops
->poll
)
102 return DEFAULT_POLLMASK
;
103 return devnode
->fops
->poll(filp
, poll
);
107 __media_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
,
108 long (*ioctl_func
)(struct file
*filp
, unsigned int cmd
,
111 struct media_devnode
*devnode
= media_devnode_data(filp
);
116 if (!media_devnode_is_registered(devnode
))
119 return ioctl_func(filp
, cmd
, arg
);
122 static long media_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
124 struct media_devnode
*devnode
= media_devnode_data(filp
);
126 return __media_ioctl(filp
, cmd
, arg
, devnode
->fops
->ioctl
);
131 static long media_compat_ioctl(struct file
*filp
, unsigned int cmd
,
134 struct media_devnode
*devnode
= media_devnode_data(filp
);
136 return __media_ioctl(filp
, cmd
, arg
, devnode
->fops
->compat_ioctl
);
139 #endif /* CONFIG_COMPAT */
141 /* Override for the open function */
142 static int media_open(struct inode
*inode
, struct file
*filp
)
144 struct media_devnode
*devnode
;
147 /* Check if the media device is available. This needs to be done with
148 * the media_devnode_lock held to prevent an open/unregister race:
149 * without the lock, the device could be unregistered and freed between
150 * the media_devnode_is_registered() and get_device() calls, leading to
153 mutex_lock(&media_devnode_lock
);
154 devnode
= container_of(inode
->i_cdev
, struct media_devnode
, cdev
);
155 /* return ENXIO if the media device has been removed
156 already or if it is not registered anymore. */
157 if (!media_devnode_is_registered(devnode
)) {
158 mutex_unlock(&media_devnode_lock
);
161 /* and increase the device refcount */
162 get_device(&devnode
->dev
);
163 mutex_unlock(&media_devnode_lock
);
165 filp
->private_data
= devnode
;
167 if (devnode
->fops
->open
) {
168 ret
= devnode
->fops
->open(filp
);
170 put_device(&devnode
->dev
);
171 filp
->private_data
= NULL
;
179 /* Override for the release function */
180 static int media_release(struct inode
*inode
, struct file
*filp
)
182 struct media_devnode
*devnode
= media_devnode_data(filp
);
184 if (devnode
->fops
->release
)
185 devnode
->fops
->release(filp
);
187 filp
->private_data
= NULL
;
189 /* decrease the refcount unconditionally since the release()
190 return value is ignored. */
191 put_device(&devnode
->dev
);
193 pr_debug("%s: Media Release\n", __func__
);
197 static const struct file_operations media_devnode_fops
= {
198 .owner
= THIS_MODULE
,
200 .write
= media_write
,
202 .unlocked_ioctl
= media_ioctl
,
204 .compat_ioctl
= media_compat_ioctl
,
205 #endif /* CONFIG_COMPAT */
206 .release
= media_release
,
211 int __must_check
media_devnode_register(struct media_device
*mdev
,
212 struct media_devnode
*devnode
,
213 struct module
*owner
)
218 /* Part 1: Find a free minor number */
219 mutex_lock(&media_devnode_lock
);
220 minor
= find_next_zero_bit(media_devnode_nums
, MEDIA_NUM_DEVICES
, 0);
221 if (minor
== MEDIA_NUM_DEVICES
) {
222 mutex_unlock(&media_devnode_lock
);
223 pr_err("could not get a free minor\n");
228 set_bit(minor
, media_devnode_nums
);
229 mutex_unlock(&media_devnode_lock
);
231 devnode
->minor
= minor
;
232 devnode
->media_dev
= mdev
;
234 /* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */
235 devnode
->dev
.bus
= &media_bus_type
;
236 devnode
->dev
.devt
= MKDEV(MAJOR(media_dev_t
), devnode
->minor
);
237 devnode
->dev
.release
= media_devnode_release
;
239 devnode
->dev
.parent
= devnode
->parent
;
240 dev_set_name(&devnode
->dev
, "media%d", devnode
->minor
);
241 device_initialize(&devnode
->dev
);
243 /* Part 2: Initialize the character device */
244 cdev_init(&devnode
->cdev
, &media_devnode_fops
);
245 devnode
->cdev
.owner
= owner
;
246 kobject_set_name(&devnode
->cdev
.kobj
, "media%d", devnode
->minor
);
248 /* Part 3: Add the media and char device */
249 ret
= cdev_device_add(&devnode
->cdev
, &devnode
->dev
);
251 pr_err("%s: cdev_device_add failed\n", __func__
);
255 /* Part 4: Activate this minor. The char device can now be used. */
256 set_bit(MEDIA_FLAG_REGISTERED
, &devnode
->flags
);
261 mutex_lock(&media_devnode_lock
);
262 clear_bit(devnode
->minor
, media_devnode_nums
);
263 devnode
->media_dev
= NULL
;
264 mutex_unlock(&media_devnode_lock
);
266 put_device(&devnode
->dev
);
270 void media_devnode_unregister_prepare(struct media_devnode
*devnode
)
272 /* Check if devnode was ever registered at all */
273 if (!media_devnode_is_registered(devnode
))
276 mutex_lock(&media_devnode_lock
);
277 clear_bit(MEDIA_FLAG_REGISTERED
, &devnode
->flags
);
278 mutex_unlock(&media_devnode_lock
);
281 void media_devnode_unregister(struct media_devnode
*devnode
)
283 mutex_lock(&media_devnode_lock
);
284 /* Delete the cdev on this minor as well */
285 cdev_device_del(&devnode
->cdev
, &devnode
->dev
);
286 devnode
->media_dev
= NULL
;
287 mutex_unlock(&media_devnode_lock
);
289 put_device(&devnode
->dev
);
293 * Initialise media for linux
295 static int __init
media_devnode_init(void)
299 pr_info("Linux media interface: v0.10\n");
300 ret
= alloc_chrdev_region(&media_dev_t
, 0, MEDIA_NUM_DEVICES
,
303 pr_warn("unable to allocate major\n");
307 ret
= bus_register(&media_bus_type
);
309 unregister_chrdev_region(media_dev_t
, MEDIA_NUM_DEVICES
);
310 pr_warn("bus_register failed\n");
317 static void __exit
media_devnode_exit(void)
319 bus_unregister(&media_bus_type
);
320 unregister_chrdev_region(media_dev_t
, MEDIA_NUM_DEVICES
);
323 subsys_initcall(media_devnode_init
);
324 module_exit(media_devnode_exit
)
326 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
327 MODULE_DESCRIPTION("Device node registration for media drivers");
328 MODULE_LICENSE("GPL");