blob: b66dc9d0766bd87afaeb584f495937305f591637 [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>
47
48#define MEDIA_NUM_DEVICES 256
49#define MEDIA_NAME "media"
50
51static dev_t media_dev_t;
52
53/*
54 * Active devices
55 */
56static DEFINE_MUTEX(media_devnode_lock);
57static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
58
59/* Called when the last user of the media device exits. */
60static void media_devnode_release(struct device *cd)
61{
62 struct media_devnode *mdev = to_media_devnode(cd);
63
64 mutex_lock(&media_devnode_lock);
65
66 /* Delete the cdev on this minor as well */
67 cdev_del(&mdev->cdev);
68
69 /* Mark device node number as free */
70 clear_bit(mdev->minor, media_devnode_nums);
71
72 mutex_unlock(&media_devnode_lock);
73
74 /* Release media_devnode and perform other cleanups as needed. */
75 if (mdev->release)
76 mdev->release(mdev);
77}
78
79static struct bus_type media_bus_type = {
80 .name = MEDIA_NAME,
81};
82
83static ssize_t media_read(struct file *filp, char __user *buf,
84 size_t sz, loff_t *off)
85{
86 struct media_devnode *mdev = media_devnode_data(filp);
87
88 if (!mdev->fops->read)
89 return -EINVAL;
90 if (!media_devnode_is_registered(mdev))
91 return -EIO;
92 return mdev->fops->read(filp, buf, sz, off);
93}
94
95static ssize_t media_write(struct file *filp, const char __user *buf,
96 size_t sz, loff_t *off)
97{
98 struct media_devnode *mdev = media_devnode_data(filp);
99
100 if (!mdev->fops->write)
101 return -EINVAL;
102 if (!media_devnode_is_registered(mdev))
103 return -EIO;
104 return mdev->fops->write(filp, buf, sz, off);
105}
106
107static unsigned int media_poll(struct file *filp,
108 struct poll_table_struct *poll)
109{
110 struct media_devnode *mdev = media_devnode_data(filp);
111
112 if (!media_devnode_is_registered(mdev))
113 return POLLERR | POLLHUP;
114 if (!mdev->fops->poll)
115 return DEFAULT_POLLMASK;
116 return mdev->fops->poll(filp, poll);
117}
118
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300119static long
120__media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
121 long (*ioctl_func)(struct file *filp, unsigned int cmd,
122 unsigned long arg))
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300123{
124 struct media_devnode *mdev = media_devnode_data(filp);
125
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300126 if (!ioctl_func)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300127 return -ENOTTY;
128
129 if (!media_devnode_is_registered(mdev))
130 return -EIO;
131
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300132 return ioctl_func(filp, cmd, arg);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300133}
134
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300135static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
136{
137 struct media_devnode *mdev = media_devnode_data(filp);
138
139 return __media_ioctl(filp, cmd, arg, mdev->fops->ioctl);
140}
141
142#ifdef CONFIG_COMPAT
143
144static long media_compat_ioctl(struct file *filp, unsigned int cmd,
145 unsigned long arg)
146{
147 struct media_devnode *mdev = media_devnode_data(filp);
148
149 return __media_ioctl(filp, cmd, arg, mdev->fops->compat_ioctl);
150}
151
152#endif /* CONFIG_COMPAT */
153
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300154/* Override for the open function */
155static int media_open(struct inode *inode, struct file *filp)
156{
157 struct media_devnode *mdev;
158 int ret;
159
160 /* Check if the media device is available. This needs to be done with
161 * the media_devnode_lock held to prevent an open/unregister race:
162 * without the lock, the device could be unregistered and freed between
163 * the media_devnode_is_registered() and get_device() calls, leading to
164 * a crash.
165 */
166 mutex_lock(&media_devnode_lock);
167 mdev = container_of(inode->i_cdev, struct media_devnode, cdev);
168 /* return ENXIO if the media device has been removed
169 already or if it is not registered anymore. */
170 if (!media_devnode_is_registered(mdev)) {
171 mutex_unlock(&media_devnode_lock);
172 return -ENXIO;
173 }
174 /* and increase the device refcount */
175 get_device(&mdev->dev);
176 mutex_unlock(&media_devnode_lock);
177
178 filp->private_data = mdev;
179
180 if (mdev->fops->open) {
181 ret = mdev->fops->open(filp);
182 if (ret) {
183 put_device(&mdev->dev);
Shuah Khand40ec6f2016-01-27 21:49:33 -0200184 filp->private_data = NULL;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300185 return ret;
186 }
187 }
188
189 return 0;
190}
191
192/* Override for the release function */
193static int media_release(struct inode *inode, struct file *filp)
194{
195 struct media_devnode *mdev = media_devnode_data(filp);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300196
197 if (mdev->fops->release)
198 mdev->fops->release(filp);
199
Max Kellermannbf244f62016-03-21 10:30:28 -0300200 filp->private_data = NULL;
201
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300202 /* decrease the refcount unconditionally since the release()
203 return value is ignored. */
204 put_device(&mdev->dev);
Mauro Carvalho Chehab8b37c642014-09-03 15:18:27 -0300205 return 0;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300206}
207
208static const struct file_operations media_devnode_fops = {
209 .owner = THIS_MODULE,
210 .read = media_read,
211 .write = media_write,
212 .open = media_open,
213 .unlocked_ioctl = media_ioctl,
Sakari Ailusc6c1d502013-01-22 12:27:55 -0300214#ifdef CONFIG_COMPAT
215 .compat_ioctl = media_compat_ioctl,
216#endif /* CONFIG_COMPAT */
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300217 .release = media_release,
218 .poll = media_poll,
219 .llseek = no_llseek,
220};
221
Sakari Ailus85de7212013-12-12 12:38:17 -0300222int __must_check media_devnode_register(struct media_devnode *mdev,
223 struct module *owner)
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300224{
225 int minor;
226 int ret;
227
228 /* Part 1: Find a free minor number */
229 mutex_lock(&media_devnode_lock);
Laurent Pinchart8c89ddd2011-05-30 15:45:47 -0300230 minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300231 if (minor == MEDIA_NUM_DEVICES) {
232 mutex_unlock(&media_devnode_lock);
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300233 pr_err("could not get a free minor\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300234 return -ENFILE;
235 }
236
Laurent Pinchart8c89ddd2011-05-30 15:45:47 -0300237 set_bit(minor, media_devnode_nums);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300238 mutex_unlock(&media_devnode_lock);
239
240 mdev->minor = minor;
241
242 /* Part 2: Initialize and register the character device */
243 cdev_init(&mdev->cdev, &media_devnode_fops);
Sakari Ailus85de7212013-12-12 12:38:17 -0300244 mdev->cdev.owner = owner;
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300245
246 ret = cdev_add(&mdev->cdev, MKDEV(MAJOR(media_dev_t), mdev->minor), 1);
247 if (ret < 0) {
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300248 pr_err("%s: cdev_add failed\n", __func__);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300249 goto error;
250 }
251
252 /* Part 3: Register the media device */
253 mdev->dev.bus = &media_bus_type;
254 mdev->dev.devt = MKDEV(MAJOR(media_dev_t), mdev->minor);
255 mdev->dev.release = media_devnode_release;
256 if (mdev->parent)
257 mdev->dev.parent = mdev->parent;
258 dev_set_name(&mdev->dev, "media%d", mdev->minor);
259 ret = device_register(&mdev->dev);
260 if (ret < 0) {
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300261 pr_err("%s: device_register failed\n", __func__);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300262 goto error;
263 }
264
265 /* Part 4: Activate this minor. The char device can now be used. */
266 set_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
267
268 return 0;
269
270error:
Max Kellermann88336e12016-03-21 04:33:12 -0700271 mutex_lock(&media_devnode_lock);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300272 cdev_del(&mdev->cdev);
273 clear_bit(mdev->minor, media_devnode_nums);
Max Kellermann88336e12016-03-21 04:33:12 -0700274 mutex_unlock(&media_devnode_lock);
275
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300276 return ret;
277}
278
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300279void media_devnode_unregister(struct media_devnode *mdev)
280{
281 /* Check if mdev was ever registered at all */
282 if (!media_devnode_is_registered(mdev))
283 return;
284
285 mutex_lock(&media_devnode_lock);
286 clear_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
287 mutex_unlock(&media_devnode_lock);
288 device_unregister(&mdev->dev);
289}
290
291/*
292 * Initialise media for linux
293 */
294static int __init media_devnode_init(void)
295{
296 int ret;
297
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300298 pr_info("Linux media interface: v0.10\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300299 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
300 MEDIA_NAME);
301 if (ret < 0) {
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300302 pr_warn("unable to allocate major\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300303 return ret;
304 }
305
306 ret = bus_register(&media_bus_type);
307 if (ret < 0) {
308 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
Sachin Kamat3c16b2b2012-09-24 08:26:24 -0300309 pr_warn("bus_register failed\n");
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300310 return -EIO;
311 }
312
313 return 0;
314}
315
316static void __exit media_devnode_exit(void)
317{
318 bus_unregister(&media_bus_type);
319 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
320}
321
Laurent Pinchartff732d22012-03-12 10:02:47 -0300322subsys_initcall(media_devnode_init);
Laurent Pinchartcf4b9212009-12-09 08:39:56 -0300323module_exit(media_devnode_exit)
324
325MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
326MODULE_DESCRIPTION("Device node registration for media drivers");
327MODULE_LICENSE("GPL");