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