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