Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 1 | /* |
| 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 Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 22 | * -- |
| 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 Kamat | 3c16b2b | 2012-09-24 08:26:24 -0300 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 31 | #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 Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 41 | |
| 42 | #include <media/media-devnode.h> |
Mauro Carvalho Chehab | a087ce7 | 2016-04-27 19:28:26 -0300 | [diff] [blame] | 43 | #include <media/media-device.h> |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 44 | |
| 45 | #define MEDIA_NUM_DEVICES 256 |
| 46 | #define MEDIA_NAME "media" |
| 47 | |
| 48 | static dev_t media_dev_t; |
| 49 | |
| 50 | /* |
| 51 | * Active devices |
| 52 | */ |
| 53 | static DEFINE_MUTEX(media_devnode_lock); |
| 54 | static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES); |
| 55 | |
| 56 | /* Called when the last user of the media device exits. */ |
| 57 | static void media_devnode_release(struct device *cd) |
| 58 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 59 | struct media_devnode *devnode = to_media_devnode(cd); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 60 | |
| 61 | mutex_lock(&media_devnode_lock); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 62 | /* Mark device node number as free */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 63 | clear_bit(devnode->minor, media_devnode_nums); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 64 | mutex_unlock(&media_devnode_lock); |
| 65 | |
| 66 | /* Release media_devnode and perform other cleanups as needed. */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 67 | if (devnode->release) |
| 68 | devnode->release(devnode); |
Mauro Carvalho Chehab | a087ce7 | 2016-04-27 19:28:26 -0300 | [diff] [blame] | 69 | |
| 70 | kfree(devnode); |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 71 | pr_debug("%s: Media Devnode Deallocated\n", __func__); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static struct bus_type media_bus_type = { |
| 75 | .name = MEDIA_NAME, |
| 76 | }; |
| 77 | |
| 78 | static ssize_t media_read(struct file *filp, char __user *buf, |
| 79 | size_t sz, loff_t *off) |
| 80 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 81 | struct media_devnode *devnode = media_devnode_data(filp); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 82 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 83 | if (!devnode->fops->read) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 84 | return -EINVAL; |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 85 | if (!media_devnode_is_registered(devnode)) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 86 | return -EIO; |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 87 | return devnode->fops->read(filp, buf, sz, off); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static ssize_t media_write(struct file *filp, const char __user *buf, |
| 91 | size_t sz, loff_t *off) |
| 92 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 93 | struct media_devnode *devnode = media_devnode_data(filp); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 94 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 95 | if (!devnode->fops->write) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 96 | return -EINVAL; |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 97 | if (!media_devnode_is_registered(devnode)) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 98 | return -EIO; |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 99 | return devnode->fops->write(filp, buf, sz, off); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static unsigned int media_poll(struct file *filp, |
| 103 | struct poll_table_struct *poll) |
| 104 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 105 | struct media_devnode *devnode = media_devnode_data(filp); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 106 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 107 | if (!media_devnode_is_registered(devnode)) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 108 | return POLLERR | POLLHUP; |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 109 | if (!devnode->fops->poll) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 110 | return DEFAULT_POLLMASK; |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 111 | return devnode->fops->poll(filp, poll); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 112 | } |
| 113 | |
Sakari Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 114 | static 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 Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 118 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 119 | struct media_devnode *devnode = media_devnode_data(filp); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 120 | |
Sakari Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 121 | if (!ioctl_func) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 122 | return -ENOTTY; |
| 123 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 124 | if (!media_devnode_is_registered(devnode)) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 125 | return -EIO; |
| 126 | |
Sakari Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 127 | return ioctl_func(filp, cmd, arg); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 128 | } |
| 129 | |
Sakari Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 130 | static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 131 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 132 | struct media_devnode *devnode = media_devnode_data(filp); |
Sakari Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 133 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 134 | return __media_ioctl(filp, cmd, arg, devnode->fops->ioctl); |
Sakari Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | #ifdef CONFIG_COMPAT |
| 138 | |
| 139 | static long media_compat_ioctl(struct file *filp, unsigned int cmd, |
| 140 | unsigned long arg) |
| 141 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 142 | struct media_devnode *devnode = media_devnode_data(filp); |
Sakari Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 143 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 144 | return __media_ioctl(filp, cmd, arg, devnode->fops->compat_ioctl); |
Sakari Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | #endif /* CONFIG_COMPAT */ |
| 148 | |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 149 | /* Override for the open function */ |
| 150 | static int media_open(struct inode *inode, struct file *filp) |
| 151 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 152 | struct media_devnode *devnode; |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 153 | 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 Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 162 | devnode = container_of(inode->i_cdev, struct media_devnode, cdev); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 163 | /* return ENXIO if the media device has been removed |
| 164 | already or if it is not registered anymore. */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 165 | if (!media_devnode_is_registered(devnode)) { |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 166 | mutex_unlock(&media_devnode_lock); |
| 167 | return -ENXIO; |
| 168 | } |
| 169 | /* and increase the device refcount */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 170 | get_device(&devnode->dev); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 171 | mutex_unlock(&media_devnode_lock); |
| 172 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 173 | filp->private_data = devnode; |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 174 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 175 | if (devnode->fops->open) { |
| 176 | ret = devnode->fops->open(filp); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 177 | if (ret) { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 178 | put_device(&devnode->dev); |
Shuah Khan | d40ec6f | 2016-01-27 21:49:33 -0200 | [diff] [blame] | 179 | filp->private_data = NULL; |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 180 | return ret; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* Override for the release function */ |
| 188 | static int media_release(struct inode *inode, struct file *filp) |
| 189 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 190 | struct media_devnode *devnode = media_devnode_data(filp); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 191 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 192 | if (devnode->fops->release) |
| 193 | devnode->fops->release(filp); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 194 | |
Max Kellermann | bf244f6 | 2016-03-21 10:30:28 -0300 | [diff] [blame] | 195 | filp->private_data = NULL; |
| 196 | |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 197 | /* decrease the refcount unconditionally since the release() |
| 198 | return value is ignored. */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 199 | put_device(&devnode->dev); |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 200 | |
| 201 | pr_debug("%s: Media Release\n", __func__); |
Mauro Carvalho Chehab | 8b37c64 | 2014-09-03 15:18:27 -0300 | [diff] [blame] | 202 | return 0; |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static 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 Ailus | c6c1d50 | 2013-01-22 12:27:55 -0300 | [diff] [blame] | 211 | #ifdef CONFIG_COMPAT |
| 212 | .compat_ioctl = media_compat_ioctl, |
| 213 | #endif /* CONFIG_COMPAT */ |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 214 | .release = media_release, |
| 215 | .poll = media_poll, |
| 216 | .llseek = no_llseek, |
| 217 | }; |
| 218 | |
Mauro Carvalho Chehab | a087ce7 | 2016-04-27 19:28:26 -0300 | [diff] [blame] | 219 | int __must_check media_devnode_register(struct media_device *mdev, |
| 220 | struct media_devnode *devnode, |
Sakari Ailus | 85de721 | 2013-12-12 12:38:17 -0300 | [diff] [blame] | 221 | struct module *owner) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 222 | { |
| 223 | int minor; |
| 224 | int ret; |
| 225 | |
| 226 | /* Part 1: Find a free minor number */ |
| 227 | mutex_lock(&media_devnode_lock); |
Laurent Pinchart | 8c89ddd | 2011-05-30 15:45:47 -0300 | [diff] [blame] | 228 | minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 229 | if (minor == MEDIA_NUM_DEVICES) { |
| 230 | mutex_unlock(&media_devnode_lock); |
Sachin Kamat | 3c16b2b | 2012-09-24 08:26:24 -0300 | [diff] [blame] | 231 | pr_err("could not get a free minor\n"); |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 232 | kfree(devnode); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 233 | return -ENFILE; |
| 234 | } |
| 235 | |
Laurent Pinchart | 8c89ddd | 2011-05-30 15:45:47 -0300 | [diff] [blame] | 236 | set_bit(minor, media_devnode_nums); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 237 | mutex_unlock(&media_devnode_lock); |
| 238 | |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 239 | devnode->minor = minor; |
Mauro Carvalho Chehab | a087ce7 | 2016-04-27 19:28:26 -0300 | [diff] [blame] | 240 | devnode->media_dev = mdev; |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 241 | |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 242 | /* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 243 | 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 Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 249 | 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 Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 257 | if (ret < 0) { |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 258 | 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 Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* Part 4: Activate this minor. The char device can now be used. */ |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 270 | set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 271 | |
| 272 | return 0; |
| 273 | |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 274 | device_add_error: |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 275 | cdev_del(&devnode->cdev); |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 276 | cdev_add_error: |
| 277 | mutex_lock(&media_devnode_lock); |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 278 | clear_bit(devnode->minor, media_devnode_nums); |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 279 | devnode->media_dev = NULL; |
Max Kellermann | 88336e1 | 2016-03-21 04:33:12 -0700 | [diff] [blame] | 280 | mutex_unlock(&media_devnode_lock); |
| 281 | |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 282 | put_device(&devnode->dev); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 283 | return ret; |
| 284 | } |
| 285 | |
Shuah Khan | 6f0dd24 | 2016-06-10 14:37:23 -0300 | [diff] [blame] | 286 | void media_devnode_unregister_prepare(struct media_devnode *devnode) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 287 | { |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 288 | /* Check if devnode was ever registered at all */ |
| 289 | if (!media_devnode_is_registered(devnode)) |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 290 | return; |
| 291 | |
| 292 | mutex_lock(&media_devnode_lock); |
Mauro Carvalho Chehab | 163f1e9 | 2016-03-23 11:22:57 -0300 | [diff] [blame] | 293 | clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); |
Shuah Khan | 6f0dd24 | 2016-06-10 14:37:23 -0300 | [diff] [blame] | 294 | mutex_unlock(&media_devnode_lock); |
| 295 | } |
| 296 | |
| 297 | void media_devnode_unregister(struct media_devnode *devnode) |
| 298 | { |
| 299 | mutex_lock(&media_devnode_lock); |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 300 | /* Delete the cdev on this minor as well */ |
| 301 | cdev_del(&devnode->cdev); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 302 | mutex_unlock(&media_devnode_lock); |
Shuah Khan | 5b28dde | 2016-05-04 16:48:28 -0300 | [diff] [blame] | 303 | device_del(&devnode->dev); |
| 304 | devnode->media_dev = NULL; |
| 305 | put_device(&devnode->dev); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Initialise media for linux |
| 310 | */ |
| 311 | static int __init media_devnode_init(void) |
| 312 | { |
| 313 | int ret; |
| 314 | |
Sachin Kamat | 3c16b2b | 2012-09-24 08:26:24 -0300 | [diff] [blame] | 315 | pr_info("Linux media interface: v0.10\n"); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 316 | ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES, |
| 317 | MEDIA_NAME); |
| 318 | if (ret < 0) { |
Sachin Kamat | 3c16b2b | 2012-09-24 08:26:24 -0300 | [diff] [blame] | 319 | pr_warn("unable to allocate major\n"); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 320 | 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 Kamat | 3c16b2b | 2012-09-24 08:26:24 -0300 | [diff] [blame] | 326 | pr_warn("bus_register failed\n"); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 327 | return -EIO; |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static 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 Pinchart | ff732d2 | 2012-03-12 10:02:47 -0300 | [diff] [blame] | 339 | subsys_initcall(media_devnode_init); |
Laurent Pinchart | cf4b921 | 2009-12-09 08:39:56 -0300 | [diff] [blame] | 340 | module_exit(media_devnode_exit) |
| 341 | |
| 342 | MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); |
| 343 | MODULE_DESCRIPTION("Device node registration for media drivers"); |
| 344 | MODULE_LICENSE("GPL"); |