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