Stephen Hemminger | fbb5858 | 2018-07-20 15:11:26 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 2 | /* |
| 3 | * drivers/uio/uio.c |
| 4 | * |
| 5 | * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de> |
| 6 | * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de> |
Hans J. Koch | 318af55 | 2010-10-30 00:36:47 +0200 | [diff] [blame] | 7 | * Copyright(C) 2006, Hans J. Koch <hjk@hansjkoch.de> |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 8 | * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com> |
| 9 | * |
| 10 | * Userspace IO |
| 11 | * |
| 12 | * Base Functions |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/poll.h> |
| 18 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 20 | #include <linux/mm.h> |
| 21 | #include <linux/idr.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 22 | #include <linux/sched/signal.h> |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 23 | #include <linux/string.h> |
| 24 | #include <linux/kobject.h> |
Eric W. Biederman | 91960a4 | 2010-09-14 11:38:06 -0700 | [diff] [blame] | 25 | #include <linux/cdev.h> |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 26 | #include <linux/uio_driver.h> |
| 27 | |
Eric W. Biederman | 91960a4 | 2010-09-14 11:38:06 -0700 | [diff] [blame] | 28 | #define UIO_MAX_DEVICES (1U << MINORBITS) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 29 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 30 | static int uio_major; |
Eric W. Biederman | 91960a4 | 2010-09-14 11:38:06 -0700 | [diff] [blame] | 31 | static struct cdev *uio_cdev; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 32 | static DEFINE_IDR(uio_idr); |
Jan Engelhardt | 4f01469 | 2008-01-22 20:50:54 +0100 | [diff] [blame] | 33 | static const struct file_operations uio_fops; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 34 | |
Jonathan Corbet | 0d4a7bc | 2008-08-26 17:15:45 -0600 | [diff] [blame] | 35 | /* Protect idr accesses */ |
| 36 | static DEFINE_MUTEX(minor_lock); |
| 37 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 38 | /* |
| 39 | * attributes |
| 40 | */ |
| 41 | |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 42 | struct uio_map { |
| 43 | struct kobject kobj; |
| 44 | struct uio_mem *mem; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 45 | }; |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 46 | #define to_map(map) container_of(map, struct uio_map, kobj) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 47 | |
Hans J. Koch | 8205779 | 2009-01-07 00:15:39 +0100 | [diff] [blame] | 48 | static ssize_t map_name_show(struct uio_mem *mem, char *buf) |
| 49 | { |
| 50 | if (unlikely(!mem->name)) |
| 51 | mem->name = ""; |
| 52 | |
| 53 | return sprintf(buf, "%s\n", mem->name); |
| 54 | } |
| 55 | |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 56 | static ssize_t map_addr_show(struct uio_mem *mem, char *buf) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 57 | { |
Cristian Stoica | e0f1147 | 2014-10-09 15:00:27 +0300 | [diff] [blame] | 58 | return sprintf(buf, "%pa\n", &mem->addr); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 61 | static ssize_t map_size_show(struct uio_mem *mem, char *buf) |
| 62 | { |
Cristian Stoica | e0f1147 | 2014-10-09 15:00:27 +0300 | [diff] [blame] | 63 | return sprintf(buf, "%pa\n", &mem->size); |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Hans J. Koch | e2b39df | 2008-09-18 23:53:18 +0200 | [diff] [blame] | 66 | static ssize_t map_offset_show(struct uio_mem *mem, char *buf) |
| 67 | { |
Michal Sojka | 171058fb | 2017-03-16 14:50:08 +0100 | [diff] [blame] | 68 | return sprintf(buf, "0x%llx\n", (unsigned long long)mem->offs); |
Hans J. Koch | e2b39df | 2008-09-18 23:53:18 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 71 | struct map_sysfs_entry { |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 72 | struct attribute attr; |
| 73 | ssize_t (*show)(struct uio_mem *, char *); |
| 74 | ssize_t (*store)(struct uio_mem *, const char *, size_t); |
| 75 | }; |
| 76 | |
Hans J. Koch | 8205779 | 2009-01-07 00:15:39 +0100 | [diff] [blame] | 77 | static struct map_sysfs_entry name_attribute = |
| 78 | __ATTR(name, S_IRUGO, map_name_show, NULL); |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 79 | static struct map_sysfs_entry addr_attribute = |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 80 | __ATTR(addr, S_IRUGO, map_addr_show, NULL); |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 81 | static struct map_sysfs_entry size_attribute = |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 82 | __ATTR(size, S_IRUGO, map_size_show, NULL); |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 83 | static struct map_sysfs_entry offset_attribute = |
Hans J. Koch | e2b39df | 2008-09-18 23:53:18 +0200 | [diff] [blame] | 84 | __ATTR(offset, S_IRUGO, map_offset_show, NULL); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 85 | |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 86 | static struct attribute *attrs[] = { |
Hans J. Koch | 8205779 | 2009-01-07 00:15:39 +0100 | [diff] [blame] | 87 | &name_attribute.attr, |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 88 | &addr_attribute.attr, |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 89 | &size_attribute.attr, |
Hans J. Koch | e2b39df | 2008-09-18 23:53:18 +0200 | [diff] [blame] | 90 | &offset_attribute.attr, |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 91 | NULL, /* need to NULL terminate the list of attributes */ |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 94 | static void map_release(struct kobject *kobj) |
| 95 | { |
| 96 | struct uio_map *map = to_map(kobj); |
| 97 | kfree(map); |
| 98 | } |
| 99 | |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 100 | static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr, |
| 101 | char *buf) |
| 102 | { |
| 103 | struct uio_map *map = to_map(kobj); |
| 104 | struct uio_mem *mem = map->mem; |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 105 | struct map_sysfs_entry *entry; |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 106 | |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 107 | entry = container_of(attr, struct map_sysfs_entry, attr); |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 108 | |
| 109 | if (!entry->show) |
| 110 | return -EIO; |
| 111 | |
| 112 | return entry->show(mem, buf); |
| 113 | } |
| 114 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 115 | static const struct sysfs_ops map_sysfs_ops = { |
Brandon Philips | 4f808bc | 2008-02-19 01:55:05 -0800 | [diff] [blame] | 116 | .show = map_type_show, |
| 117 | }; |
| 118 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 119 | static struct kobj_type map_attr_type = { |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 120 | .release = map_release, |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 121 | .sysfs_ops = &map_sysfs_ops, |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 122 | .default_attrs = attrs, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 123 | }; |
| 124 | |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 125 | struct uio_portio { |
| 126 | struct kobject kobj; |
| 127 | struct uio_port *port; |
| 128 | }; |
| 129 | #define to_portio(portio) container_of(portio, struct uio_portio, kobj) |
| 130 | |
Hans J. Koch | 8205779 | 2009-01-07 00:15:39 +0100 | [diff] [blame] | 131 | static ssize_t portio_name_show(struct uio_port *port, char *buf) |
| 132 | { |
| 133 | if (unlikely(!port->name)) |
| 134 | port->name = ""; |
| 135 | |
| 136 | return sprintf(buf, "%s\n", port->name); |
| 137 | } |
| 138 | |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 139 | static ssize_t portio_start_show(struct uio_port *port, char *buf) |
| 140 | { |
| 141 | return sprintf(buf, "0x%lx\n", port->start); |
| 142 | } |
| 143 | |
| 144 | static ssize_t portio_size_show(struct uio_port *port, char *buf) |
| 145 | { |
| 146 | return sprintf(buf, "0x%lx\n", port->size); |
| 147 | } |
| 148 | |
| 149 | static ssize_t portio_porttype_show(struct uio_port *port, char *buf) |
| 150 | { |
| 151 | const char *porttypes[] = {"none", "x86", "gpio", "other"}; |
| 152 | |
| 153 | if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER)) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | return sprintf(buf, "port_%s\n", porttypes[port->porttype]); |
| 157 | } |
| 158 | |
| 159 | struct portio_sysfs_entry { |
| 160 | struct attribute attr; |
| 161 | ssize_t (*show)(struct uio_port *, char *); |
| 162 | ssize_t (*store)(struct uio_port *, const char *, size_t); |
| 163 | }; |
| 164 | |
Hans J. Koch | 8205779 | 2009-01-07 00:15:39 +0100 | [diff] [blame] | 165 | static struct portio_sysfs_entry portio_name_attribute = |
| 166 | __ATTR(name, S_IRUGO, portio_name_show, NULL); |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 167 | static struct portio_sysfs_entry portio_start_attribute = |
| 168 | __ATTR(start, S_IRUGO, portio_start_show, NULL); |
| 169 | static struct portio_sysfs_entry portio_size_attribute = |
| 170 | __ATTR(size, S_IRUGO, portio_size_show, NULL); |
| 171 | static struct portio_sysfs_entry portio_porttype_attribute = |
| 172 | __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL); |
| 173 | |
| 174 | static struct attribute *portio_attrs[] = { |
Hans J. Koch | 8205779 | 2009-01-07 00:15:39 +0100 | [diff] [blame] | 175 | &portio_name_attribute.attr, |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 176 | &portio_start_attribute.attr, |
| 177 | &portio_size_attribute.attr, |
| 178 | &portio_porttype_attribute.attr, |
| 179 | NULL, |
| 180 | }; |
| 181 | |
| 182 | static void portio_release(struct kobject *kobj) |
| 183 | { |
| 184 | struct uio_portio *portio = to_portio(kobj); |
| 185 | kfree(portio); |
| 186 | } |
| 187 | |
| 188 | static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr, |
| 189 | char *buf) |
| 190 | { |
| 191 | struct uio_portio *portio = to_portio(kobj); |
| 192 | struct uio_port *port = portio->port; |
| 193 | struct portio_sysfs_entry *entry; |
| 194 | |
| 195 | entry = container_of(attr, struct portio_sysfs_entry, attr); |
| 196 | |
| 197 | if (!entry->show) |
| 198 | return -EIO; |
| 199 | |
| 200 | return entry->show(port, buf); |
| 201 | } |
| 202 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 203 | static const struct sysfs_ops portio_sysfs_ops = { |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 204 | .show = portio_type_show, |
| 205 | }; |
| 206 | |
| 207 | static struct kobj_type portio_attr_type = { |
| 208 | .release = portio_release, |
| 209 | .sysfs_ops = &portio_sysfs_ops, |
| 210 | .default_attrs = portio_attrs, |
| 211 | }; |
| 212 | |
Greg Kroah-Hartman | c9dce92 | 2013-07-24 15:05:23 -0700 | [diff] [blame] | 213 | static ssize_t name_show(struct device *dev, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 214 | struct device_attribute *attr, char *buf) |
| 215 | { |
| 216 | struct uio_device *idev = dev_get_drvdata(dev); |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 217 | int ret; |
| 218 | |
| 219 | mutex_lock(&idev->info_lock); |
| 220 | if (!idev->info) { |
| 221 | ret = -EINVAL; |
| 222 | dev_err(dev, "the device has been unregistered\n"); |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | ret = sprintf(buf, "%s\n", idev->info->name); |
| 227 | |
| 228 | out: |
| 229 | mutex_unlock(&idev->info_lock); |
| 230 | return ret; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 231 | } |
Greg Kroah-Hartman | c9dce92 | 2013-07-24 15:05:23 -0700 | [diff] [blame] | 232 | static DEVICE_ATTR_RO(name); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 233 | |
Greg Kroah-Hartman | c9dce92 | 2013-07-24 15:05:23 -0700 | [diff] [blame] | 234 | static ssize_t version_show(struct device *dev, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 235 | struct device_attribute *attr, char *buf) |
| 236 | { |
| 237 | struct uio_device *idev = dev_get_drvdata(dev); |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 238 | int ret; |
| 239 | |
| 240 | mutex_lock(&idev->info_lock); |
| 241 | if (!idev->info) { |
| 242 | ret = -EINVAL; |
| 243 | dev_err(dev, "the device has been unregistered\n"); |
| 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | ret = sprintf(buf, "%s\n", idev->info->version); |
| 248 | |
| 249 | out: |
| 250 | mutex_unlock(&idev->info_lock); |
| 251 | return ret; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 252 | } |
Greg Kroah-Hartman | c9dce92 | 2013-07-24 15:05:23 -0700 | [diff] [blame] | 253 | static DEVICE_ATTR_RO(version); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 254 | |
Greg Kroah-Hartman | c9dce92 | 2013-07-24 15:05:23 -0700 | [diff] [blame] | 255 | static ssize_t event_show(struct device *dev, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 256 | struct device_attribute *attr, char *buf) |
| 257 | { |
| 258 | struct uio_device *idev = dev_get_drvdata(dev); |
Eric W. Biederman | 70a9156 | 2010-09-14 11:36:54 -0700 | [diff] [blame] | 259 | return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event)); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 260 | } |
Greg Kroah-Hartman | c9dce92 | 2013-07-24 15:05:23 -0700 | [diff] [blame] | 261 | static DEVICE_ATTR_RO(event); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 262 | |
Greg Kroah-Hartman | c9dce92 | 2013-07-24 15:05:23 -0700 | [diff] [blame] | 263 | static struct attribute *uio_attrs[] = { |
| 264 | &dev_attr_name.attr, |
| 265 | &dev_attr_version.attr, |
| 266 | &dev_attr_event.attr, |
| 267 | NULL, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 268 | }; |
Greg Kroah-Hartman | c9dce92 | 2013-07-24 15:05:23 -0700 | [diff] [blame] | 269 | ATTRIBUTE_GROUPS(uio); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 270 | |
Eric W. Biederman | c66fdab | 2010-09-14 11:38:36 -0700 | [diff] [blame] | 271 | /* UIO class infrastructure */ |
| 272 | static struct class uio_class = { |
| 273 | .name = "uio", |
Greg Kroah-Hartman | c9dce92 | 2013-07-24 15:05:23 -0700 | [diff] [blame] | 274 | .dev_groups = uio_groups, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | /* |
| 278 | * device functions |
| 279 | */ |
| 280 | static int uio_dev_add_attributes(struct uio_device *idev) |
| 281 | { |
| 282 | int ret; |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 283 | int mi, pi; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 284 | int map_found = 0; |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 285 | int portio_found = 0; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 286 | struct uio_mem *mem; |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 287 | struct uio_map *map; |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 288 | struct uio_port *port; |
| 289 | struct uio_portio *portio; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 290 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 291 | for (mi = 0; mi < MAX_UIO_MAPS; mi++) { |
| 292 | mem = &idev->info->mem[mi]; |
| 293 | if (mem->size == 0) |
| 294 | break; |
| 295 | if (!map_found) { |
| 296 | map_found = 1; |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 297 | idev->map_dir = kobject_create_and_add("maps", |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 298 | &idev->dev.kobj); |
Dan Carpenter | 0320a27 | 2016-04-01 14:04:23 +0300 | [diff] [blame] | 299 | if (!idev->map_dir) { |
| 300 | ret = -ENOMEM; |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 301 | goto err_map; |
Dan Carpenter | 0320a27 | 2016-04-01 14:04:23 +0300 | [diff] [blame] | 302 | } |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 303 | } |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 304 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
Dan Carpenter | 0320a27 | 2016-04-01 14:04:23 +0300 | [diff] [blame] | 305 | if (!map) { |
| 306 | ret = -ENOMEM; |
Suman Anna | 0d83539 | 2017-05-09 18:58:24 -0500 | [diff] [blame] | 307 | goto err_map; |
Dan Carpenter | 0320a27 | 2016-04-01 14:04:23 +0300 | [diff] [blame] | 308 | } |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 309 | kobject_init(&map->kobj, &map_attr_type); |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 310 | map->mem = mem; |
| 311 | mem->map = map; |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 312 | ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 313 | if (ret) |
Cong Ding | 75f0aef | 2013-10-25 00:25:53 -0700 | [diff] [blame] | 314 | goto err_map_kobj; |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 315 | ret = kobject_uevent(&map->kobj, KOBJ_ADD); |
| 316 | if (ret) |
Suman Anna | 0d83539 | 2017-05-09 18:58:24 -0500 | [diff] [blame] | 317 | goto err_map_kobj; |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) { |
| 321 | port = &idev->info->port[pi]; |
| 322 | if (port->size == 0) |
| 323 | break; |
| 324 | if (!portio_found) { |
| 325 | portio_found = 1; |
| 326 | idev->portio_dir = kobject_create_and_add("portio", |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 327 | &idev->dev.kobj); |
Dan Carpenter | 0320a27 | 2016-04-01 14:04:23 +0300 | [diff] [blame] | 328 | if (!idev->portio_dir) { |
| 329 | ret = -ENOMEM; |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 330 | goto err_portio; |
Dan Carpenter | 0320a27 | 2016-04-01 14:04:23 +0300 | [diff] [blame] | 331 | } |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 332 | } |
| 333 | portio = kzalloc(sizeof(*portio), GFP_KERNEL); |
Dan Carpenter | 0320a27 | 2016-04-01 14:04:23 +0300 | [diff] [blame] | 334 | if (!portio) { |
| 335 | ret = -ENOMEM; |
Suman Anna | 0d83539 | 2017-05-09 18:58:24 -0500 | [diff] [blame] | 336 | goto err_portio; |
Dan Carpenter | 0320a27 | 2016-04-01 14:04:23 +0300 | [diff] [blame] | 337 | } |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 338 | kobject_init(&portio->kobj, &portio_attr_type); |
| 339 | portio->port = port; |
| 340 | port->portio = portio; |
| 341 | ret = kobject_add(&portio->kobj, idev->portio_dir, |
| 342 | "port%d", pi); |
| 343 | if (ret) |
Cong Ding | 75f0aef | 2013-10-25 00:25:53 -0700 | [diff] [blame] | 344 | goto err_portio_kobj; |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 345 | ret = kobject_uevent(&portio->kobj, KOBJ_ADD); |
| 346 | if (ret) |
Suman Anna | 0d83539 | 2017-05-09 18:58:24 -0500 | [diff] [blame] | 347 | goto err_portio_kobj; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 352 | err_portio: |
Cong Ding | 75f0aef | 2013-10-25 00:25:53 -0700 | [diff] [blame] | 353 | pi--; |
| 354 | err_portio_kobj: |
| 355 | for (; pi >= 0; pi--) { |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 356 | port = &idev->info->port[pi]; |
| 357 | portio = port->portio; |
| 358 | kobject_put(&portio->kobj); |
| 359 | } |
| 360 | kobject_put(idev->portio_dir); |
| 361 | err_map: |
Cong Ding | 75f0aef | 2013-10-25 00:25:53 -0700 | [diff] [blame] | 362 | mi--; |
| 363 | err_map_kobj: |
| 364 | for (; mi >= 0; mi--) { |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 365 | mem = &idev->info->mem[mi]; |
Greg Kroah-Hartman | 81e7c6a | 2007-12-04 22:41:54 +0000 | [diff] [blame] | 366 | map = mem->map; |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 367 | kobject_put(&map->kobj); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 368 | } |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 369 | kobject_put(idev->map_dir); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 370 | dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | static void uio_dev_del_attributes(struct uio_device *idev) |
| 375 | { |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 376 | int i; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 377 | struct uio_mem *mem; |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 378 | struct uio_port *port; |
| 379 | |
| 380 | for (i = 0; i < MAX_UIO_MAPS; i++) { |
| 381 | mem = &idev->info->mem[i]; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 382 | if (mem->size == 0) |
| 383 | break; |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 384 | kobject_put(&mem->map->kobj); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 385 | } |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 386 | kobject_put(idev->map_dir); |
Hans J. Koch | e70c412 | 2008-12-06 02:23:13 +0100 | [diff] [blame] | 387 | |
| 388 | for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) { |
| 389 | port = &idev->info->port[i]; |
| 390 | if (port->size == 0) |
| 391 | break; |
| 392 | kobject_put(&port->portio->kobj); |
| 393 | } |
| 394 | kobject_put(idev->portio_dir); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static int uio_get_minor(struct uio_device *idev) |
| 398 | { |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 399 | int retval = -ENOMEM; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 400 | |
| 401 | mutex_lock(&minor_lock); |
Tejun Heo | 6d77093 | 2013-02-27 17:04:47 -0800 | [diff] [blame] | 402 | retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL); |
| 403 | if (retval >= 0) { |
| 404 | idev->minor = retval; |
Damian Hobson-Garcia | 5ed0505 | 2013-03-26 10:31:22 +0900 | [diff] [blame] | 405 | retval = 0; |
Tejun Heo | 6d77093 | 2013-02-27 17:04:47 -0800 | [diff] [blame] | 406 | } else if (retval == -ENOSPC) { |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 407 | dev_err(&idev->dev, "too many uio devices\n"); |
Hillf Danton | c6edc42 | 2011-03-31 20:38:47 +0800 | [diff] [blame] | 408 | retval = -EINVAL; |
Hillf Danton | c6edc42 | 2011-03-31 20:38:47 +0800 | [diff] [blame] | 409 | } |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 410 | mutex_unlock(&minor_lock); |
| 411 | return retval; |
| 412 | } |
| 413 | |
| 414 | static void uio_free_minor(struct uio_device *idev) |
| 415 | { |
Jonathan Corbet | 0d4a7bc | 2008-08-26 17:15:45 -0600 | [diff] [blame] | 416 | mutex_lock(&minor_lock); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 417 | idr_remove(&uio_idr, idev->minor); |
Jonathan Corbet | 0d4a7bc | 2008-08-26 17:15:45 -0600 | [diff] [blame] | 418 | mutex_unlock(&minor_lock); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
| 422 | * uio_event_notify - trigger an interrupt event |
| 423 | * @info: UIO device capabilities |
| 424 | */ |
| 425 | void uio_event_notify(struct uio_info *info) |
| 426 | { |
| 427 | struct uio_device *idev = info->uio_dev; |
| 428 | |
| 429 | atomic_inc(&idev->event); |
| 430 | wake_up_interruptible(&idev->wait); |
| 431 | kill_fasync(&idev->async_queue, SIGIO, POLL_IN); |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(uio_event_notify); |
| 434 | |
| 435 | /** |
| 436 | * uio_interrupt - hardware interrupt handler |
| 437 | * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer |
| 438 | * @dev_id: Pointer to the devices uio_device structure |
| 439 | */ |
| 440 | static irqreturn_t uio_interrupt(int irq, void *dev_id) |
| 441 | { |
| 442 | struct uio_device *idev = (struct uio_device *)dev_id; |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 443 | irqreturn_t ret; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 444 | |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 445 | mutex_lock(&idev->info_lock); |
| 446 | |
| 447 | ret = idev->info->handler(irq, idev->info); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 448 | if (ret == IRQ_HANDLED) |
| 449 | uio_event_notify(idev->info); |
| 450 | |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 451 | mutex_unlock(&idev->info_lock); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | struct uio_listener { |
| 456 | struct uio_device *dev; |
| 457 | s32 event_count; |
| 458 | }; |
| 459 | |
| 460 | static int uio_open(struct inode *inode, struct file *filep) |
| 461 | { |
| 462 | struct uio_device *idev; |
| 463 | struct uio_listener *listener; |
| 464 | int ret = 0; |
| 465 | |
Jonathan Corbet | 0d4a7bc | 2008-08-26 17:15:45 -0600 | [diff] [blame] | 466 | mutex_lock(&minor_lock); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 467 | idev = idr_find(&uio_idr, iminor(inode)); |
Jonathan Corbet | 0d4a7bc | 2008-08-26 17:15:45 -0600 | [diff] [blame] | 468 | mutex_unlock(&minor_lock); |
Jonathan Corbet | fbc8a81 | 2008-05-15 10:39:37 -0600 | [diff] [blame] | 469 | if (!idev) { |
| 470 | ret = -ENODEV; |
| 471 | goto out; |
| 472 | } |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 473 | |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 474 | get_device(&idev->dev); |
| 475 | |
Jonathan Corbet | fbc8a81 | 2008-05-15 10:39:37 -0600 | [diff] [blame] | 476 | if (!try_module_get(idev->owner)) { |
| 477 | ret = -ENODEV; |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 478 | goto err_module_get; |
Jonathan Corbet | fbc8a81 | 2008-05-15 10:39:37 -0600 | [diff] [blame] | 479 | } |
Uwe Kleine-König | 610ad50 | 2008-04-11 11:07:39 +0200 | [diff] [blame] | 480 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 481 | listener = kmalloc(sizeof(*listener), GFP_KERNEL); |
Uwe Kleine-König | 610ad50 | 2008-04-11 11:07:39 +0200 | [diff] [blame] | 482 | if (!listener) { |
| 483 | ret = -ENOMEM; |
| 484 | goto err_alloc_listener; |
| 485 | } |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 486 | |
| 487 | listener->dev = idev; |
| 488 | listener->event_count = atomic_read(&idev->event); |
| 489 | filep->private_data = listener; |
| 490 | |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 491 | mutex_lock(&idev->info_lock); |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 492 | if (!idev->info) { |
| 493 | mutex_unlock(&idev->info_lock); |
| 494 | ret = -EINVAL; |
| 495 | goto err_alloc_listener; |
| 496 | } |
| 497 | |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 498 | if (idev->info && idev->info->open) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 499 | ret = idev->info->open(idev->info, inode); |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 500 | mutex_unlock(&idev->info_lock); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 501 | if (ret) |
| 502 | goto err_infoopen; |
| 503 | |
Uwe Kleine-König | 610ad50 | 2008-04-11 11:07:39 +0200 | [diff] [blame] | 504 | return 0; |
| 505 | |
| 506 | err_infoopen: |
Uwe Kleine-König | 610ad50 | 2008-04-11 11:07:39 +0200 | [diff] [blame] | 507 | kfree(listener); |
Uwe Kleine-König | 610ad50 | 2008-04-11 11:07:39 +0200 | [diff] [blame] | 508 | |
Jonathan Corbet | 0d4a7bc | 2008-08-26 17:15:45 -0600 | [diff] [blame] | 509 | err_alloc_listener: |
Uwe Kleine-König | 610ad50 | 2008-04-11 11:07:39 +0200 | [diff] [blame] | 510 | module_put(idev->owner); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 511 | |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 512 | err_module_get: |
| 513 | put_device(&idev->dev); |
| 514 | |
Jonathan Corbet | fbc8a81 | 2008-05-15 10:39:37 -0600 | [diff] [blame] | 515 | out: |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | static int uio_fasync(int fd, struct file *filep, int on) |
| 520 | { |
| 521 | struct uio_listener *listener = filep->private_data; |
| 522 | struct uio_device *idev = listener->dev; |
| 523 | |
| 524 | return fasync_helper(fd, filep, on, &idev->async_queue); |
| 525 | } |
| 526 | |
| 527 | static int uio_release(struct inode *inode, struct file *filep) |
| 528 | { |
| 529 | int ret = 0; |
| 530 | struct uio_listener *listener = filep->private_data; |
| 531 | struct uio_device *idev = listener->dev; |
| 532 | |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 533 | mutex_lock(&idev->info_lock); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 534 | if (idev->info && idev->info->release) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 535 | ret = idev->info->release(idev->info, inode); |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 536 | mutex_unlock(&idev->info_lock); |
Uwe Kleine-König | 610ad50 | 2008-04-11 11:07:39 +0200 | [diff] [blame] | 537 | |
| 538 | module_put(idev->owner); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 539 | kfree(listener); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 540 | put_device(&idev->dev); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 541 | return ret; |
| 542 | } |
| 543 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 544 | static __poll_t uio_poll(struct file *filep, poll_table *wait) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 545 | { |
| 546 | struct uio_listener *listener = filep->private_data; |
| 547 | struct uio_device *idev = listener->dev; |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 548 | __poll_t ret = 0; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 549 | |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 550 | mutex_lock(&idev->info_lock); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 551 | if (!idev->info || !idev->info->irq) |
| 552 | ret = -EIO; |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 553 | mutex_unlock(&idev->info_lock); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 554 | |
| 555 | if (ret) |
| 556 | return ret; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 557 | |
| 558 | poll_wait(filep, &idev->wait, wait); |
| 559 | if (listener->event_count != atomic_read(&idev->event)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 560 | return EPOLLIN | EPOLLRDNORM; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static ssize_t uio_read(struct file *filep, char __user *buf, |
| 565 | size_t count, loff_t *ppos) |
| 566 | { |
| 567 | struct uio_listener *listener = filep->private_data; |
| 568 | struct uio_device *idev = listener->dev; |
| 569 | DECLARE_WAITQUEUE(wait, current); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 570 | ssize_t retval = 0; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 571 | s32 event_count; |
| 572 | |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 573 | mutex_lock(&idev->info_lock); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 574 | if (!idev->info || !idev->info->irq) |
| 575 | retval = -EIO; |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 576 | mutex_unlock(&idev->info_lock); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 577 | |
| 578 | if (retval) |
| 579 | return retval; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 580 | |
| 581 | if (count != sizeof(s32)) |
| 582 | return -EINVAL; |
| 583 | |
| 584 | add_wait_queue(&idev->wait, &wait); |
| 585 | |
| 586 | do { |
| 587 | set_current_state(TASK_INTERRUPTIBLE); |
| 588 | |
| 589 | event_count = atomic_read(&idev->event); |
| 590 | if (event_count != listener->event_count) { |
Michal Hocko | edce5e6 | 2015-09-07 09:21:24 +0200 | [diff] [blame] | 591 | __set_current_state(TASK_RUNNING); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 592 | if (copy_to_user(buf, &event_count, count)) |
| 593 | retval = -EFAULT; |
| 594 | else { |
| 595 | listener->event_count = event_count; |
| 596 | retval = count; |
| 597 | } |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | if (filep->f_flags & O_NONBLOCK) { |
| 602 | retval = -EAGAIN; |
| 603 | break; |
| 604 | } |
| 605 | |
| 606 | if (signal_pending(current)) { |
| 607 | retval = -ERESTARTSYS; |
| 608 | break; |
| 609 | } |
| 610 | schedule(); |
| 611 | } while (1); |
| 612 | |
| 613 | __set_current_state(TASK_RUNNING); |
| 614 | remove_wait_queue(&idev->wait, &wait); |
| 615 | |
| 616 | return retval; |
| 617 | } |
| 618 | |
Hans J. Koch | 328a14e | 2008-05-23 13:50:14 +0200 | [diff] [blame] | 619 | static ssize_t uio_write(struct file *filep, const char __user *buf, |
| 620 | size_t count, loff_t *ppos) |
| 621 | { |
| 622 | struct uio_listener *listener = filep->private_data; |
| 623 | struct uio_device *idev = listener->dev; |
| 624 | ssize_t retval; |
| 625 | s32 irq_on; |
| 626 | |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 627 | mutex_lock(&idev->info_lock); |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 628 | if (!idev->info) { |
| 629 | retval = -EINVAL; |
| 630 | goto out; |
| 631 | } |
| 632 | |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 633 | if (!idev->info || !idev->info->irq) { |
Hamish Martin | 81daa40 | 2018-05-14 13:32:22 +1200 | [diff] [blame] | 634 | retval = -EIO; |
| 635 | goto out; |
| 636 | } |
Hans J. Koch | 328a14e | 2008-05-23 13:50:14 +0200 | [diff] [blame] | 637 | |
Hamish Martin | 81daa40 | 2018-05-14 13:32:22 +1200 | [diff] [blame] | 638 | if (count != sizeof(s32)) { |
| 639 | retval = -EINVAL; |
| 640 | goto out; |
| 641 | } |
Hans J. Koch | 328a14e | 2008-05-23 13:50:14 +0200 | [diff] [blame] | 642 | |
Hamish Martin | 81daa40 | 2018-05-14 13:32:22 +1200 | [diff] [blame] | 643 | if (!idev->info->irqcontrol) { |
| 644 | retval = -ENOSYS; |
| 645 | goto out; |
| 646 | } |
Hans J. Koch | 328a14e | 2008-05-23 13:50:14 +0200 | [diff] [blame] | 647 | |
Hamish Martin | 81daa40 | 2018-05-14 13:32:22 +1200 | [diff] [blame] | 648 | if (copy_from_user(&irq_on, buf, count)) { |
| 649 | retval = -EFAULT; |
| 650 | goto out; |
| 651 | } |
Hans J. Koch | 328a14e | 2008-05-23 13:50:14 +0200 | [diff] [blame] | 652 | |
| 653 | retval = idev->info->irqcontrol(idev->info, irq_on); |
| 654 | |
Hamish Martin | 81daa40 | 2018-05-14 13:32:22 +1200 | [diff] [blame] | 655 | out: |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 656 | mutex_unlock(&idev->info_lock); |
Hans J. Koch | 328a14e | 2008-05-23 13:50:14 +0200 | [diff] [blame] | 657 | return retval ? retval : sizeof(s32); |
| 658 | } |
| 659 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 660 | static int uio_find_mem_index(struct vm_area_struct *vma) |
| 661 | { |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 662 | struct uio_device *idev = vma->vm_private_data; |
| 663 | |
Hillf Danton | f0c554f | 2011-03-28 23:33:26 +0200 | [diff] [blame] | 664 | if (vma->vm_pgoff < MAX_UIO_MAPS) { |
| 665 | if (idev->info->mem[vma->vm_pgoff].size == 0) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 666 | return -1; |
Hillf Danton | f0c554f | 2011-03-28 23:33:26 +0200 | [diff] [blame] | 667 | return (int)vma->vm_pgoff; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 668 | } |
| 669 | return -1; |
| 670 | } |
| 671 | |
Souptick Joarder | 9b85e95 | 2018-04-17 00:03:55 +0530 | [diff] [blame] | 672 | static vm_fault_t uio_vma_fault(struct vm_fault *vmf) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 673 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 674 | struct uio_device *idev = vmf->vma->vm_private_data; |
Nick Piggin | a18b630 | 2008-02-06 01:37:35 -0800 | [diff] [blame] | 675 | struct page *page; |
Andrew G. Harvey | 02683ff | 2008-09-24 01:10:02 +0200 | [diff] [blame] | 676 | unsigned long offset; |
Ben Hutchings | e6418fc | 2013-10-27 21:53:40 +0000 | [diff] [blame] | 677 | void *addr; |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 678 | int ret = 0; |
| 679 | int mi; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 680 | |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 681 | mutex_lock(&idev->info_lock); |
| 682 | if (!idev->info) { |
| 683 | ret = VM_FAULT_SIGBUS; |
| 684 | goto out; |
| 685 | } |
| 686 | |
| 687 | mi = uio_find_mem_index(vmf->vma); |
| 688 | if (mi < 0) { |
| 689 | ret = VM_FAULT_SIGBUS; |
| 690 | goto out; |
| 691 | } |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 692 | |
Andrew G. Harvey | 02683ff | 2008-09-24 01:10:02 +0200 | [diff] [blame] | 693 | /* |
| 694 | * We need to subtract mi because userspace uses offset = N*PAGE_SIZE |
| 695 | * to use mem[N]. |
| 696 | */ |
| 697 | offset = (vmf->pgoff - mi) << PAGE_SHIFT; |
| 698 | |
Ben Hutchings | e6418fc | 2013-10-27 21:53:40 +0000 | [diff] [blame] | 699 | addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 700 | if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL) |
Ben Hutchings | e6418fc | 2013-10-27 21:53:40 +0000 | [diff] [blame] | 701 | page = virt_to_page(addr); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 702 | else |
Ben Hutchings | e6418fc | 2013-10-27 21:53:40 +0000 | [diff] [blame] | 703 | page = vmalloc_to_page(addr); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 704 | get_page(page); |
Nick Piggin | a18b630 | 2008-02-06 01:37:35 -0800 | [diff] [blame] | 705 | vmf->page = page; |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 706 | |
| 707 | out: |
| 708 | mutex_unlock(&idev->info_lock); |
| 709 | |
| 710 | return ret; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 711 | } |
| 712 | |
Uwe Kleine-König | 7294151 | 2013-08-07 13:02:53 +0200 | [diff] [blame] | 713 | static const struct vm_operations_struct uio_logical_vm_ops = { |
Nick Piggin | a18b630 | 2008-02-06 01:37:35 -0800 | [diff] [blame] | 714 | .fault = uio_vma_fault, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 715 | }; |
| 716 | |
Uwe Kleine-König | 7294151 | 2013-08-07 13:02:53 +0200 | [diff] [blame] | 717 | static int uio_mmap_logical(struct vm_area_struct *vma) |
| 718 | { |
| 719 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
| 720 | vma->vm_ops = &uio_logical_vm_ops; |
Uwe Kleine-König | 7294151 | 2013-08-07 13:02:53 +0200 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static const struct vm_operations_struct uio_physical_vm_ops = { |
| 725 | #ifdef CONFIG_HAVE_IOREMAP_PROT |
| 726 | .access = generic_access_phys, |
| 727 | #endif |
| 728 | }; |
| 729 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 730 | static int uio_mmap_physical(struct vm_area_struct *vma) |
| 731 | { |
| 732 | struct uio_device *idev = vma->vm_private_data; |
| 733 | int mi = uio_find_mem_index(vma); |
Linus Torvalds | 7314e61 | 2013-10-29 10:21:34 -0700 | [diff] [blame] | 734 | struct uio_mem *mem; |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 735 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 736 | if (mi < 0) |
| 737 | return -EINVAL; |
Linus Torvalds | 7314e61 | 2013-10-29 10:21:34 -0700 | [diff] [blame] | 738 | mem = idev->info->mem + mi; |
| 739 | |
Linus Torvalds | b655028 | 2013-12-02 11:50:37 -0800 | [diff] [blame] | 740 | if (mem->addr & ~PAGE_MASK) |
| 741 | return -ENODEV; |
Greg Kroah-Hartman | b29f680 | 2014-06-17 16:07:08 -0700 | [diff] [blame] | 742 | if (vma->vm_end - vma->vm_start > mem->size) |
Linus Torvalds | 7314e61 | 2013-10-29 10:21:34 -0700 | [diff] [blame] | 743 | return -EINVAL; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 744 | |
Uwe Kleine-König | 7294151 | 2013-08-07 13:02:53 +0200 | [diff] [blame] | 745 | vma->vm_ops = &uio_physical_vm_ops; |
Jean-Samuel Chenard | c9698d6b | 2008-03-14 11:28:36 +0100 | [diff] [blame] | 746 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 747 | |
Linus Torvalds | 7314e61 | 2013-10-29 10:21:34 -0700 | [diff] [blame] | 748 | /* |
| 749 | * We cannot use the vm_iomap_memory() helper here, |
| 750 | * because vma->vm_pgoff is the map index we looked |
| 751 | * up above in uio_find_mem_index(), rather than an |
| 752 | * actual page offset into the mmap. |
| 753 | * |
| 754 | * So we just do the physical mmap without a page |
| 755 | * offset. |
| 756 | */ |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 757 | return remap_pfn_range(vma, |
| 758 | vma->vm_start, |
Linus Torvalds | 7314e61 | 2013-10-29 10:21:34 -0700 | [diff] [blame] | 759 | mem->addr >> PAGE_SHIFT, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 760 | vma->vm_end - vma->vm_start, |
| 761 | vma->vm_page_prot); |
| 762 | } |
| 763 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 764 | static int uio_mmap(struct file *filep, struct vm_area_struct *vma) |
| 765 | { |
| 766 | struct uio_listener *listener = filep->private_data; |
| 767 | struct uio_device *idev = listener->dev; |
| 768 | int mi; |
| 769 | unsigned long requested_pages, actual_pages; |
| 770 | int ret = 0; |
| 771 | |
| 772 | if (vma->vm_end < vma->vm_start) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | vma->vm_private_data = idev; |
| 776 | |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 777 | mutex_lock(&idev->info_lock); |
| 778 | if (!idev->info) { |
| 779 | ret = -EINVAL; |
| 780 | goto out; |
| 781 | } |
| 782 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 783 | mi = uio_find_mem_index(vma); |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 784 | if (mi < 0) { |
| 785 | ret = -EINVAL; |
| 786 | goto out; |
| 787 | } |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 788 | |
Libin | 52c2dad | 2013-07-03 15:01:27 -0700 | [diff] [blame] | 789 | requested_pages = vma_pages(vma); |
Ian Abbott | 6da2d37 | 2009-02-24 17:22:59 +0000 | [diff] [blame] | 790 | actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK) |
| 791 | + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT; |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 792 | if (requested_pages > actual_pages) { |
| 793 | ret = -EINVAL; |
| 794 | goto out; |
| 795 | } |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 796 | |
| 797 | if (idev->info->mmap) { |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 798 | ret = idev->info->mmap(idev->info, vma); |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 799 | goto out; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | switch (idev->info->mem[mi].memtype) { |
| 803 | case UIO_MEM_PHYS: |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 804 | ret = uio_mmap_physical(vma); |
| 805 | break; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 806 | case UIO_MEM_LOGICAL: |
| 807 | case UIO_MEM_VIRTUAL: |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 808 | ret = uio_mmap_logical(vma); |
| 809 | break; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 810 | default: |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 811 | ret = -EINVAL; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 812 | } |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 813 | |
| 814 | out: |
| 815 | mutex_unlock(&idev->info_lock); |
| 816 | return 0; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Jan Engelhardt | 4f01469 | 2008-01-22 20:50:54 +0100 | [diff] [blame] | 819 | static const struct file_operations uio_fops = { |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 820 | .owner = THIS_MODULE, |
| 821 | .open = uio_open, |
| 822 | .release = uio_release, |
| 823 | .read = uio_read, |
Hans J. Koch | 328a14e | 2008-05-23 13:50:14 +0200 | [diff] [blame] | 824 | .write = uio_write, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 825 | .mmap = uio_mmap, |
| 826 | .poll = uio_poll, |
| 827 | .fasync = uio_fasync, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 828 | .llseek = noop_llseek, |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 829 | }; |
| 830 | |
| 831 | static int uio_major_init(void) |
| 832 | { |
Eric W. Biederman | 91960a4 | 2010-09-14 11:38:06 -0700 | [diff] [blame] | 833 | static const char name[] = "uio"; |
| 834 | struct cdev *cdev = NULL; |
| 835 | dev_t uio_dev = 0; |
| 836 | int result; |
| 837 | |
| 838 | result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name); |
| 839 | if (result) |
| 840 | goto out; |
| 841 | |
| 842 | result = -ENOMEM; |
| 843 | cdev = cdev_alloc(); |
| 844 | if (!cdev) |
| 845 | goto out_unregister; |
| 846 | |
| 847 | cdev->owner = THIS_MODULE; |
| 848 | cdev->ops = &uio_fops; |
| 849 | kobject_set_name(&cdev->kobj, "%s", name); |
| 850 | |
| 851 | result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES); |
| 852 | if (result) |
| 853 | goto out_put; |
| 854 | |
| 855 | uio_major = MAJOR(uio_dev); |
| 856 | uio_cdev = cdev; |
Wanlong Gao | d80df1c | 2011-08-20 12:12:08 +0800 | [diff] [blame] | 857 | return 0; |
Eric W. Biederman | 91960a4 | 2010-09-14 11:38:06 -0700 | [diff] [blame] | 858 | out_put: |
| 859 | kobject_put(&cdev->kobj); |
| 860 | out_unregister: |
| 861 | unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES); |
Wanlong Gao | d80df1c | 2011-08-20 12:12:08 +0800 | [diff] [blame] | 862 | out: |
| 863 | return result; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static void uio_major_cleanup(void) |
| 867 | { |
Eric W. Biederman | 91960a4 | 2010-09-14 11:38:06 -0700 | [diff] [blame] | 868 | unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES); |
| 869 | cdev_del(uio_cdev); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static int init_uio_class(void) |
| 873 | { |
Eric W. Biederman | 3d4f9d7 | 2010-09-14 11:36:27 -0700 | [diff] [blame] | 874 | int ret; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 875 | |
| 876 | /* This is the first time in here, set everything up properly */ |
| 877 | ret = uio_major_init(); |
| 878 | if (ret) |
| 879 | goto exit; |
| 880 | |
Eric W. Biederman | c66fdab | 2010-09-14 11:38:36 -0700 | [diff] [blame] | 881 | ret = class_register(&uio_class); |
| 882 | if (ret) { |
| 883 | printk(KERN_ERR "class_register failed for uio\n"); |
| 884 | goto err_class_register; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 885 | } |
| 886 | return 0; |
| 887 | |
Eric W. Biederman | c66fdab | 2010-09-14 11:38:36 -0700 | [diff] [blame] | 888 | err_class_register: |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 889 | uio_major_cleanup(); |
| 890 | exit: |
| 891 | return ret; |
| 892 | } |
| 893 | |
Eric W. Biederman | 3d4f9d7 | 2010-09-14 11:36:27 -0700 | [diff] [blame] | 894 | static void release_uio_class(void) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 895 | { |
Eric W. Biederman | c66fdab | 2010-09-14 11:38:36 -0700 | [diff] [blame] | 896 | class_unregister(&uio_class); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 897 | uio_major_cleanup(); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 898 | } |
| 899 | |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 900 | static void uio_device_release(struct device *dev) |
| 901 | { |
| 902 | struct uio_device *idev = dev_get_drvdata(dev); |
| 903 | |
| 904 | kfree(idev); |
| 905 | } |
| 906 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 907 | /** |
| 908 | * uio_register_device - register a new userspace IO device |
| 909 | * @owner: module that creates the new device |
| 910 | * @parent: parent device |
| 911 | * @info: UIO device capabilities |
| 912 | * |
| 913 | * returns zero on success or a negative error code. |
| 914 | */ |
| 915 | int __uio_register_device(struct module *owner, |
| 916 | struct device *parent, |
| 917 | struct uio_info *info) |
| 918 | { |
| 919 | struct uio_device *idev; |
| 920 | int ret = 0; |
| 921 | |
| 922 | if (!parent || !info || !info->name || !info->version) |
| 923 | return -EINVAL; |
| 924 | |
| 925 | info->uio_dev = NULL; |
| 926 | |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 927 | idev = kzalloc(sizeof(*idev), GFP_KERNEL); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 928 | if (!idev) { |
Michal Simek | e6789cd | 2013-09-12 07:39:59 +0200 | [diff] [blame] | 929 | return -ENOMEM; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | idev->owner = owner; |
| 933 | idev->info = info; |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 934 | mutex_init(&idev->info_lock); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 935 | init_waitqueue_head(&idev->wait); |
| 936 | atomic_set(&idev->event, 0); |
| 937 | |
| 938 | ret = uio_get_minor(idev); |
| 939 | if (ret) |
Michal Simek | e6789cd | 2013-09-12 07:39:59 +0200 | [diff] [blame] | 940 | return ret; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 941 | |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 942 | idev->dev.devt = MKDEV(uio_major, idev->minor); |
| 943 | idev->dev.class = &uio_class; |
| 944 | idev->dev.parent = parent; |
| 945 | idev->dev.release = uio_device_release; |
| 946 | dev_set_drvdata(&idev->dev, idev); |
| 947 | |
| 948 | ret = dev_set_name(&idev->dev, "uio%d", idev->minor); |
| 949 | if (ret) |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 950 | goto err_device_create; |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 951 | |
| 952 | ret = device_register(&idev->dev); |
| 953 | if (ret) |
| 954 | goto err_device_create; |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 955 | |
| 956 | ret = uio_dev_add_attributes(idev); |
| 957 | if (ret) |
| 958 | goto err_uio_dev_add_attributes; |
| 959 | |
| 960 | info->uio_dev = idev; |
| 961 | |
Eric W. Biederman | 6427a76 | 2010-09-14 11:37:36 -0700 | [diff] [blame] | 962 | if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) { |
Brian Russell | a087146 | 2015-03-19 17:55:26 +0000 | [diff] [blame] | 963 | /* |
| 964 | * Note that we deliberately don't use devm_request_irq |
| 965 | * here. The parent module can unregister the UIO device |
| 966 | * and call pci_disable_msi, which requires that this |
| 967 | * irq has been freed. However, the device may have open |
| 968 | * FDs at the time of unregister and therefore may not be |
| 969 | * freed until they are released. |
| 970 | */ |
Xiubo Li | 9421e45 | 2018-07-06 22:05:37 -0400 | [diff] [blame] | 971 | ret = request_threaded_irq(info->irq, NULL, uio_interrupt, |
| 972 | info->irq_flags, info->name, idev); |
| 973 | |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 974 | if (ret) |
| 975 | goto err_request_irq; |
| 976 | } |
| 977 | |
| 978 | return 0; |
| 979 | |
| 980 | err_request_irq: |
| 981 | uio_dev_del_attributes(idev); |
| 982 | err_uio_dev_add_attributes: |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 983 | device_unregister(&idev->dev); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 984 | err_device_create: |
| 985 | uio_free_minor(idev); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 986 | return ret; |
| 987 | } |
| 988 | EXPORT_SYMBOL_GPL(__uio_register_device); |
| 989 | |
| 990 | /** |
| 991 | * uio_unregister_device - unregister a industrial IO device |
| 992 | * @info: UIO device capabilities |
| 993 | * |
| 994 | */ |
| 995 | void uio_unregister_device(struct uio_info *info) |
| 996 | { |
| 997 | struct uio_device *idev; |
| 998 | |
| 999 | if (!info || !info->uio_dev) |
| 1000 | return; |
| 1001 | |
| 1002 | idev = info->uio_dev; |
| 1003 | |
| 1004 | uio_free_minor(idev); |
| 1005 | |
Xiubo Li | 57c5f4d | 2018-07-06 22:05:39 -0400 | [diff] [blame] | 1006 | mutex_lock(&idev->info_lock); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 1007 | uio_dev_del_attributes(idev); |
| 1008 | |
Stephen Hemminger | 9ff2c13 | 2015-05-14 09:19:13 -0700 | [diff] [blame] | 1009 | if (info->irq && info->irq != UIO_IRQ_CUSTOM) |
| 1010 | free_irq(info->irq, idev); |
Brian Russell | a087146 | 2015-03-19 17:55:26 +0000 | [diff] [blame] | 1011 | |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 1012 | idev->info = NULL; |
Xiubo Li | 543af58 | 2018-07-06 22:05:38 -0400 | [diff] [blame] | 1013 | mutex_unlock(&idev->info_lock); |
Hamish Martin | a93e7b3 | 2018-05-14 13:32:23 +1200 | [diff] [blame] | 1014 | |
| 1015 | device_unregister(&idev->dev); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 1016 | |
| 1017 | return; |
| 1018 | } |
| 1019 | EXPORT_SYMBOL_GPL(uio_unregister_device); |
| 1020 | |
| 1021 | static int __init uio_init(void) |
| 1022 | { |
Eric W. Biederman | 3d4f9d7 | 2010-09-14 11:36:27 -0700 | [diff] [blame] | 1023 | return init_uio_class(); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | static void __exit uio_exit(void) |
| 1027 | { |
Eric W. Biederman | 3d4f9d7 | 2010-09-14 11:36:27 -0700 | [diff] [blame] | 1028 | release_uio_class(); |
Johannes Thumshirn | e2ef939 | 2015-07-08 17:24:46 +0200 | [diff] [blame] | 1029 | idr_destroy(&uio_idr); |
Hans J. Koch | beafc54 | 2006-12-07 10:58:29 +0100 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | module_init(uio_init) |
| 1033 | module_exit(uio_exit) |
| 1034 | MODULE_LICENSE("GPL v2"); |