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