blob: bc774cce0a4d5c5986feb425ad8ecd86025a5b3b [file] [log] [blame]
Hans J. Kochbeafc542006-12-07 10:58:29 +01001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Hans J. Kochbeafc542006-12-07 10:58:29 +010021#include <linux/mm.h>
22#include <linux/idr.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040023#include <linux/sched.h>
Hans J. Kochbeafc542006-12-07 10:58:29 +010024#include <linux/string.h>
25#include <linux/kobject.h>
26#include <linux/uio_driver.h>
27
28#define UIO_MAX_DEVICES 255
29
30struct 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-Hartman81e7c6a2007-12-04 22:41:54 +000039 struct kobject *map_dir;
Hans J. Koche70c4122008-12-06 02:23:13 +010040 struct kobject *portio_dir;
Hans J. Kochbeafc542006-12-07 10:58:29 +010041};
42
43static int uio_major;
44static DEFINE_IDR(uio_idr);
Jan Engelhardt4f014692008-01-22 20:50:54 +010045static const struct file_operations uio_fops;
Hans J. Kochbeafc542006-12-07 10:58:29 +010046
47/* UIO class infrastructure */
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -070048static struct class *uio_class;
Hans J. Kochbeafc542006-12-07 10:58:29 +010049
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -060050/* Protect idr accesses */
51static DEFINE_MUTEX(minor_lock);
52
Hans J. Kochbeafc542006-12-07 10:58:29 +010053/*
54 * attributes
55 */
56
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000057struct uio_map {
58 struct kobject kobj;
59 struct uio_mem *mem;
Hans J. Kochbeafc542006-12-07 10:58:29 +010060};
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000061#define to_map(map) container_of(map, struct uio_map, kobj)
Hans J. Kochbeafc542006-12-07 10:58:29 +010062
Hans J. Koch82057792009-01-07 00:15:39 +010063static ssize_t map_name_show(struct uio_mem *mem, char *buf)
64{
65 if (unlikely(!mem->name))
66 mem->name = "";
67
68 return sprintf(buf, "%s\n", mem->name);
69}
70
Brandon Philips4f808bc2008-02-19 01:55:05 -080071static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
Hans J. Kochbeafc542006-12-07 10:58:29 +010072{
Brandon Philips4f808bc2008-02-19 01:55:05 -080073 return sprintf(buf, "0x%lx\n", mem->addr);
Hans J. Kochbeafc542006-12-07 10:58:29 +010074}
75
Brandon Philips4f808bc2008-02-19 01:55:05 -080076static ssize_t map_size_show(struct uio_mem *mem, char *buf)
77{
78 return sprintf(buf, "0x%lx\n", mem->size);
79}
80
Hans J. Koche2b39df2008-09-18 23:53:18 +020081static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
82{
83 return sprintf(buf, "0x%lx\n", mem->addr & ~PAGE_MASK);
84}
85
Hans J. Koche70c4122008-12-06 02:23:13 +010086struct map_sysfs_entry {
Brandon Philips4f808bc2008-02-19 01:55:05 -080087 struct attribute attr;
88 ssize_t (*show)(struct uio_mem *, char *);
89 ssize_t (*store)(struct uio_mem *, const char *, size_t);
90};
91
Hans J. Koch82057792009-01-07 00:15:39 +010092static struct map_sysfs_entry name_attribute =
93 __ATTR(name, S_IRUGO, map_name_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +010094static struct map_sysfs_entry addr_attribute =
Brandon Philips4f808bc2008-02-19 01:55:05 -080095 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +010096static struct map_sysfs_entry size_attribute =
Brandon Philips4f808bc2008-02-19 01:55:05 -080097 __ATTR(size, S_IRUGO, map_size_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +010098static struct map_sysfs_entry offset_attribute =
Hans J. Koche2b39df2008-09-18 23:53:18 +020099 __ATTR(offset, S_IRUGO, map_offset_show, NULL);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100100
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000101static struct attribute *attrs[] = {
Hans J. Koch82057792009-01-07 00:15:39 +0100102 &name_attribute.attr,
Brandon Philips4f808bc2008-02-19 01:55:05 -0800103 &addr_attribute.attr,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000104 &size_attribute.attr,
Hans J. Koche2b39df2008-09-18 23:53:18 +0200105 &offset_attribute.attr,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000106 NULL, /* need to NULL terminate the list of attributes */
Hans J. Kochbeafc542006-12-07 10:58:29 +0100107};
108
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000109static void map_release(struct kobject *kobj)
110{
111 struct uio_map *map = to_map(kobj);
112 kfree(map);
113}
114
Brandon Philips4f808bc2008-02-19 01:55:05 -0800115static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
116 char *buf)
117{
118 struct uio_map *map = to_map(kobj);
119 struct uio_mem *mem = map->mem;
Hans J. Koche70c4122008-12-06 02:23:13 +0100120 struct map_sysfs_entry *entry;
Brandon Philips4f808bc2008-02-19 01:55:05 -0800121
Hans J. Koche70c4122008-12-06 02:23:13 +0100122 entry = container_of(attr, struct map_sysfs_entry, attr);
Brandon Philips4f808bc2008-02-19 01:55:05 -0800123
124 if (!entry->show)
125 return -EIO;
126
127 return entry->show(mem, buf);
128}
129
Emese Revfy52cf25d2010-01-19 02:58:23 +0100130static const struct sysfs_ops map_sysfs_ops = {
Brandon Philips4f808bc2008-02-19 01:55:05 -0800131 .show = map_type_show,
132};
133
Hans J. Kochbeafc542006-12-07 10:58:29 +0100134static struct kobj_type map_attr_type = {
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000135 .release = map_release,
Hans J. Koche70c4122008-12-06 02:23:13 +0100136 .sysfs_ops = &map_sysfs_ops,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000137 .default_attrs = attrs,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100138};
139
Hans J. Koche70c4122008-12-06 02:23:13 +0100140struct uio_portio {
141 struct kobject kobj;
142 struct uio_port *port;
143};
144#define to_portio(portio) container_of(portio, struct uio_portio, kobj)
145
Hans J. Koch82057792009-01-07 00:15:39 +0100146static ssize_t portio_name_show(struct uio_port *port, char *buf)
147{
148 if (unlikely(!port->name))
149 port->name = "";
150
151 return sprintf(buf, "%s\n", port->name);
152}
153
Hans J. Koche70c4122008-12-06 02:23:13 +0100154static ssize_t portio_start_show(struct uio_port *port, char *buf)
155{
156 return sprintf(buf, "0x%lx\n", port->start);
157}
158
159static ssize_t portio_size_show(struct uio_port *port, char *buf)
160{
161 return sprintf(buf, "0x%lx\n", port->size);
162}
163
164static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
165{
166 const char *porttypes[] = {"none", "x86", "gpio", "other"};
167
168 if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
169 return -EINVAL;
170
171 return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
172}
173
174struct portio_sysfs_entry {
175 struct attribute attr;
176 ssize_t (*show)(struct uio_port *, char *);
177 ssize_t (*store)(struct uio_port *, const char *, size_t);
178};
179
Hans J. Koch82057792009-01-07 00:15:39 +0100180static struct portio_sysfs_entry portio_name_attribute =
181 __ATTR(name, S_IRUGO, portio_name_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +0100182static struct portio_sysfs_entry portio_start_attribute =
183 __ATTR(start, S_IRUGO, portio_start_show, NULL);
184static struct portio_sysfs_entry portio_size_attribute =
185 __ATTR(size, S_IRUGO, portio_size_show, NULL);
186static struct portio_sysfs_entry portio_porttype_attribute =
187 __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
188
189static struct attribute *portio_attrs[] = {
Hans J. Koch82057792009-01-07 00:15:39 +0100190 &portio_name_attribute.attr,
Hans J. Koche70c4122008-12-06 02:23:13 +0100191 &portio_start_attribute.attr,
192 &portio_size_attribute.attr,
193 &portio_porttype_attribute.attr,
194 NULL,
195};
196
197static void portio_release(struct kobject *kobj)
198{
199 struct uio_portio *portio = to_portio(kobj);
200 kfree(portio);
201}
202
203static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
204 char *buf)
205{
206 struct uio_portio *portio = to_portio(kobj);
207 struct uio_port *port = portio->port;
208 struct portio_sysfs_entry *entry;
209
210 entry = container_of(attr, struct portio_sysfs_entry, attr);
211
212 if (!entry->show)
213 return -EIO;
214
215 return entry->show(port, buf);
216}
217
Emese Revfy52cf25d2010-01-19 02:58:23 +0100218static const struct sysfs_ops portio_sysfs_ops = {
Hans J. Koche70c4122008-12-06 02:23:13 +0100219 .show = portio_type_show,
220};
221
222static struct kobj_type portio_attr_type = {
223 .release = portio_release,
224 .sysfs_ops = &portio_sysfs_ops,
225 .default_attrs = portio_attrs,
226};
227
Hans J. Kochbeafc542006-12-07 10:58:29 +0100228static ssize_t show_name(struct device *dev,
229 struct device_attribute *attr, char *buf)
230{
231 struct uio_device *idev = dev_get_drvdata(dev);
232 if (idev)
233 return sprintf(buf, "%s\n", idev->info->name);
234 else
235 return -ENODEV;
236}
237static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
238
239static ssize_t show_version(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
242 struct uio_device *idev = dev_get_drvdata(dev);
243 if (idev)
244 return sprintf(buf, "%s\n", idev->info->version);
245 else
246 return -ENODEV;
247}
248static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
249
250static ssize_t show_event(struct device *dev,
251 struct device_attribute *attr, char *buf)
252{
253 struct uio_device *idev = dev_get_drvdata(dev);
254 if (idev)
255 return sprintf(buf, "%u\n",
256 (unsigned int)atomic_read(&idev->event));
257 else
258 return -ENODEV;
259}
260static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
261
262static struct attribute *uio_attrs[] = {
263 &dev_attr_name.attr,
264 &dev_attr_version.attr,
265 &dev_attr_event.attr,
266 NULL,
267};
268
269static struct attribute_group uio_attr_grp = {
270 .attrs = uio_attrs,
271};
272
273/*
274 * device functions
275 */
276static int uio_dev_add_attributes(struct uio_device *idev)
277{
278 int ret;
Hans J. Koche70c4122008-12-06 02:23:13 +0100279 int mi, pi;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100280 int map_found = 0;
Hans J. Koche70c4122008-12-06 02:23:13 +0100281 int portio_found = 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100282 struct uio_mem *mem;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000283 struct uio_map *map;
Hans J. Koche70c4122008-12-06 02:23:13 +0100284 struct uio_port *port;
285 struct uio_portio *portio;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100286
287 ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
288 if (ret)
289 goto err_group;
290
291 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
292 mem = &idev->info->mem[mi];
293 if (mem->size == 0)
294 break;
295 if (!map_found) {
296 map_found = 1;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000297 idev->map_dir = kobject_create_and_add("maps",
298 &idev->dev->kobj);
299 if (!idev->map_dir)
Hans J. Koche70c4122008-12-06 02:23:13 +0100300 goto err_map;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100301 }
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000302 map = kzalloc(sizeof(*map), GFP_KERNEL);
303 if (!map)
Hans J. Koche70c4122008-12-06 02:23:13 +0100304 goto err_map;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700305 kobject_init(&map->kobj, &map_attr_type);
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000306 map->mem = mem;
307 mem->map = map;
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700308 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100309 if (ret)
Hans J. Koche70c4122008-12-06 02:23:13 +0100310 goto err_map;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000311 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
312 if (ret)
Hans J. Koche70c4122008-12-06 02:23:13 +0100313 goto err_map;
314 }
315
316 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
317 port = &idev->info->port[pi];
318 if (port->size == 0)
319 break;
320 if (!portio_found) {
321 portio_found = 1;
322 idev->portio_dir = kobject_create_and_add("portio",
323 &idev->dev->kobj);
324 if (!idev->portio_dir)
325 goto err_portio;
326 }
327 portio = kzalloc(sizeof(*portio), GFP_KERNEL);
328 if (!portio)
329 goto err_portio;
330 kobject_init(&portio->kobj, &portio_attr_type);
331 portio->port = port;
332 port->portio = portio;
333 ret = kobject_add(&portio->kobj, idev->portio_dir,
334 "port%d", pi);
335 if (ret)
336 goto err_portio;
337 ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
338 if (ret)
339 goto err_portio;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100340 }
341
342 return 0;
343
Hans J. Koche70c4122008-12-06 02:23:13 +0100344err_portio:
345 for (pi--; pi >= 0; pi--) {
346 port = &idev->info->port[pi];
347 portio = port->portio;
348 kobject_put(&portio->kobj);
349 }
350 kobject_put(idev->portio_dir);
351err_map:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100352 for (mi--; mi>=0; mi--) {
353 mem = &idev->info->mem[mi];
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000354 map = mem->map;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800355 kobject_put(&map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100356 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800357 kobject_put(idev->map_dir);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100358 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
359err_group:
360 dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
361 return ret;
362}
363
364static void uio_dev_del_attributes(struct uio_device *idev)
365{
Hans J. Koche70c4122008-12-06 02:23:13 +0100366 int i;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100367 struct uio_mem *mem;
Hans J. Koche70c4122008-12-06 02:23:13 +0100368 struct uio_port *port;
369
370 for (i = 0; i < MAX_UIO_MAPS; i++) {
371 mem = &idev->info->mem[i];
Hans J. Kochbeafc542006-12-07 10:58:29 +0100372 if (mem->size == 0)
373 break;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800374 kobject_put(&mem->map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100375 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800376 kobject_put(idev->map_dir);
Hans J. Koche70c4122008-12-06 02:23:13 +0100377
378 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
379 port = &idev->info->port[i];
380 if (port->size == 0)
381 break;
382 kobject_put(&port->portio->kobj);
383 }
384 kobject_put(idev->portio_dir);
385
Hans J. Kochbeafc542006-12-07 10:58:29 +0100386 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
387}
388
389static int uio_get_minor(struct uio_device *idev)
390{
Hans J. Kochbeafc542006-12-07 10:58:29 +0100391 int retval = -ENOMEM;
392 int id;
393
394 mutex_lock(&minor_lock);
395 if (idr_pre_get(&uio_idr, GFP_KERNEL) == 0)
396 goto exit;
397
398 retval = idr_get_new(&uio_idr, idev, &id);
399 if (retval < 0) {
400 if (retval == -EAGAIN)
401 retval = -ENOMEM;
402 goto exit;
403 }
404 idev->minor = id & MAX_ID_MASK;
405exit:
406 mutex_unlock(&minor_lock);
407 return retval;
408}
409
410static void uio_free_minor(struct uio_device *idev)
411{
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600412 mutex_lock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100413 idr_remove(&uio_idr, idev->minor);
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600414 mutex_unlock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100415}
416
417/**
418 * uio_event_notify - trigger an interrupt event
419 * @info: UIO device capabilities
420 */
421void uio_event_notify(struct uio_info *info)
422{
423 struct uio_device *idev = info->uio_dev;
424
425 atomic_inc(&idev->event);
426 wake_up_interruptible(&idev->wait);
427 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
428}
429EXPORT_SYMBOL_GPL(uio_event_notify);
430
431/**
432 * uio_interrupt - hardware interrupt handler
433 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
434 * @dev_id: Pointer to the devices uio_device structure
435 */
436static irqreturn_t uio_interrupt(int irq, void *dev_id)
437{
438 struct uio_device *idev = (struct uio_device *)dev_id;
439 irqreturn_t ret = idev->info->handler(irq, idev->info);
440
441 if (ret == IRQ_HANDLED)
442 uio_event_notify(idev->info);
443
444 return ret;
445}
446
447struct uio_listener {
448 struct uio_device *dev;
449 s32 event_count;
450};
451
452static int uio_open(struct inode *inode, struct file *filep)
453{
454 struct uio_device *idev;
455 struct uio_listener *listener;
456 int ret = 0;
457
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600458 mutex_lock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100459 idev = idr_find(&uio_idr, iminor(inode));
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600460 mutex_unlock(&minor_lock);
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600461 if (!idev) {
462 ret = -ENODEV;
463 goto out;
464 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100465
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600466 if (!try_module_get(idev->owner)) {
467 ret = -ENODEV;
468 goto out;
469 }
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200470
Hans J. Kochbeafc542006-12-07 10:58:29 +0100471 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200472 if (!listener) {
473 ret = -ENOMEM;
474 goto err_alloc_listener;
475 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100476
477 listener->dev = idev;
478 listener->event_count = atomic_read(&idev->event);
479 filep->private_data = listener;
480
481 if (idev->info->open) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100482 ret = idev->info->open(idev->info, inode);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200483 if (ret)
484 goto err_infoopen;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100485 }
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200486 return 0;
487
488err_infoopen:
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200489 kfree(listener);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200490
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600491err_alloc_listener:
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200492 module_put(idev->owner);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100493
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600494out:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100495 return ret;
496}
497
498static int uio_fasync(int fd, struct file *filep, int on)
499{
500 struct uio_listener *listener = filep->private_data;
501 struct uio_device *idev = listener->dev;
502
503 return fasync_helper(fd, filep, on, &idev->async_queue);
504}
505
506static int uio_release(struct inode *inode, struct file *filep)
507{
508 int ret = 0;
509 struct uio_listener *listener = filep->private_data;
510 struct uio_device *idev = listener->dev;
511
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200512 if (idev->info->release)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100513 ret = idev->info->release(idev->info, inode);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200514
515 module_put(idev->owner);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100516 kfree(listener);
517 return ret;
518}
519
520static unsigned int uio_poll(struct file *filep, poll_table *wait)
521{
522 struct uio_listener *listener = filep->private_data;
523 struct uio_device *idev = listener->dev;
524
525 if (idev->info->irq == UIO_IRQ_NONE)
526 return -EIO;
527
528 poll_wait(filep, &idev->wait, wait);
529 if (listener->event_count != atomic_read(&idev->event))
530 return POLLIN | POLLRDNORM;
531 return 0;
532}
533
534static ssize_t uio_read(struct file *filep, char __user *buf,
535 size_t count, loff_t *ppos)
536{
537 struct uio_listener *listener = filep->private_data;
538 struct uio_device *idev = listener->dev;
539 DECLARE_WAITQUEUE(wait, current);
540 ssize_t retval;
541 s32 event_count;
542
543 if (idev->info->irq == UIO_IRQ_NONE)
544 return -EIO;
545
546 if (count != sizeof(s32))
547 return -EINVAL;
548
549 add_wait_queue(&idev->wait, &wait);
550
551 do {
552 set_current_state(TASK_INTERRUPTIBLE);
553
554 event_count = atomic_read(&idev->event);
555 if (event_count != listener->event_count) {
556 if (copy_to_user(buf, &event_count, count))
557 retval = -EFAULT;
558 else {
559 listener->event_count = event_count;
560 retval = count;
561 }
562 break;
563 }
564
565 if (filep->f_flags & O_NONBLOCK) {
566 retval = -EAGAIN;
567 break;
568 }
569
570 if (signal_pending(current)) {
571 retval = -ERESTARTSYS;
572 break;
573 }
574 schedule();
575 } while (1);
576
577 __set_current_state(TASK_RUNNING);
578 remove_wait_queue(&idev->wait, &wait);
579
580 return retval;
581}
582
Hans J. Koch328a14e2008-05-23 13:50:14 +0200583static ssize_t uio_write(struct file *filep, const char __user *buf,
584 size_t count, loff_t *ppos)
585{
586 struct uio_listener *listener = filep->private_data;
587 struct uio_device *idev = listener->dev;
588 ssize_t retval;
589 s32 irq_on;
590
591 if (idev->info->irq == UIO_IRQ_NONE)
592 return -EIO;
593
594 if (count != sizeof(s32))
595 return -EINVAL;
596
597 if (!idev->info->irqcontrol)
598 return -ENOSYS;
599
600 if (copy_from_user(&irq_on, buf, count))
601 return -EFAULT;
602
603 retval = idev->info->irqcontrol(idev->info, irq_on);
604
605 return retval ? retval : sizeof(s32);
606}
607
Hans J. Kochbeafc542006-12-07 10:58:29 +0100608static int uio_find_mem_index(struct vm_area_struct *vma)
609{
610 int mi;
611 struct uio_device *idev = vma->vm_private_data;
612
613 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
614 if (idev->info->mem[mi].size == 0)
615 return -1;
616 if (vma->vm_pgoff == mi)
617 return mi;
618 }
619 return -1;
620}
621
622static void uio_vma_open(struct vm_area_struct *vma)
623{
624 struct uio_device *idev = vma->vm_private_data;
625 idev->vma_count++;
626}
627
628static void uio_vma_close(struct vm_area_struct *vma)
629{
630 struct uio_device *idev = vma->vm_private_data;
631 idev->vma_count--;
632}
633
Nick Piggina18b6302008-02-06 01:37:35 -0800634static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100635{
636 struct uio_device *idev = vma->vm_private_data;
Nick Piggina18b6302008-02-06 01:37:35 -0800637 struct page *page;
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200638 unsigned long offset;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100639
640 int mi = uio_find_mem_index(vma);
641 if (mi < 0)
Nick Piggina18b6302008-02-06 01:37:35 -0800642 return VM_FAULT_SIGBUS;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100643
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200644 /*
645 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
646 * to use mem[N].
647 */
648 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
649
Hans J. Kochbeafc542006-12-07 10:58:29 +0100650 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200651 page = virt_to_page(idev->info->mem[mi].addr + offset);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100652 else
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200653 page = vmalloc_to_page((void *)idev->info->mem[mi].addr
654 + offset);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100655 get_page(page);
Nick Piggina18b6302008-02-06 01:37:35 -0800656 vmf->page = page;
657 return 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100658}
659
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400660static const struct vm_operations_struct uio_vm_ops = {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100661 .open = uio_vma_open,
662 .close = uio_vma_close,
Nick Piggina18b6302008-02-06 01:37:35 -0800663 .fault = uio_vma_fault,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100664};
665
666static int uio_mmap_physical(struct vm_area_struct *vma)
667{
668 struct uio_device *idev = vma->vm_private_data;
669 int mi = uio_find_mem_index(vma);
670 if (mi < 0)
671 return -EINVAL;
672
673 vma->vm_flags |= VM_IO | VM_RESERVED;
674
Jean-Samuel Chenardc9698d6b2008-03-14 11:28:36 +0100675 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
676
Hans J. Kochbeafc542006-12-07 10:58:29 +0100677 return remap_pfn_range(vma,
678 vma->vm_start,
679 idev->info->mem[mi].addr >> PAGE_SHIFT,
680 vma->vm_end - vma->vm_start,
681 vma->vm_page_prot);
682}
683
684static int uio_mmap_logical(struct vm_area_struct *vma)
685{
686 vma->vm_flags |= VM_RESERVED;
687 vma->vm_ops = &uio_vm_ops;
688 uio_vma_open(vma);
689 return 0;
690}
691
692static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
693{
694 struct uio_listener *listener = filep->private_data;
695 struct uio_device *idev = listener->dev;
696 int mi;
697 unsigned long requested_pages, actual_pages;
698 int ret = 0;
699
700 if (vma->vm_end < vma->vm_start)
701 return -EINVAL;
702
703 vma->vm_private_data = idev;
704
705 mi = uio_find_mem_index(vma);
706 if (mi < 0)
707 return -EINVAL;
708
709 requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
Ian Abbott6da2d372009-02-24 17:22:59 +0000710 actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
711 + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100712 if (requested_pages > actual_pages)
713 return -EINVAL;
714
715 if (idev->info->mmap) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100716 ret = idev->info->mmap(idev->info, vma);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100717 return ret;
718 }
719
720 switch (idev->info->mem[mi].memtype) {
721 case UIO_MEM_PHYS:
722 return uio_mmap_physical(vma);
723 case UIO_MEM_LOGICAL:
724 case UIO_MEM_VIRTUAL:
725 return uio_mmap_logical(vma);
726 default:
727 return -EINVAL;
728 }
729}
730
Jan Engelhardt4f014692008-01-22 20:50:54 +0100731static const struct file_operations uio_fops = {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100732 .owner = THIS_MODULE,
733 .open = uio_open,
734 .release = uio_release,
735 .read = uio_read,
Hans J. Koch328a14e2008-05-23 13:50:14 +0200736 .write = uio_write,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100737 .mmap = uio_mmap,
738 .poll = uio_poll,
739 .fasync = uio_fasync,
740};
741
742static int uio_major_init(void)
743{
744 uio_major = register_chrdev(0, "uio", &uio_fops);
745 if (uio_major < 0)
746 return uio_major;
747 return 0;
748}
749
750static void uio_major_cleanup(void)
751{
752 unregister_chrdev(uio_major, "uio");
753}
754
755static int init_uio_class(void)
756{
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700757 struct class *class;
758 int ret;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100759
760 /* This is the first time in here, set everything up properly */
761 ret = uio_major_init();
762 if (ret)
763 goto exit;
764
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700765 class = class_create(THIS_MODULE, "uio");
766 if (IS_ERR(class)) {
767 ret = IS_ERR(class);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100768 printk(KERN_ERR "class_create failed for uio\n");
769 goto err_class_create;
770 }
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700771 uio_class = class;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100772 return 0;
773
774err_class_create:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100775 uio_major_cleanup();
776exit:
777 return ret;
778}
779
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700780static void release_uio_class(void)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100781{
782 /* Ok, we cheat as we know we only have one uio_class */
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700783 class_destroy(uio_class);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100784 uio_class = NULL;
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700785 uio_major_cleanup();
Hans J. Kochbeafc542006-12-07 10:58:29 +0100786}
787
788/**
789 * uio_register_device - register a new userspace IO device
790 * @owner: module that creates the new device
791 * @parent: parent device
792 * @info: UIO device capabilities
793 *
794 * returns zero on success or a negative error code.
795 */
796int __uio_register_device(struct module *owner,
797 struct device *parent,
798 struct uio_info *info)
799{
800 struct uio_device *idev;
801 int ret = 0;
802
803 if (!parent || !info || !info->name || !info->version)
804 return -EINVAL;
805
806 info->uio_dev = NULL;
807
Hans J. Kochbeafc542006-12-07 10:58:29 +0100808 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
809 if (!idev) {
810 ret = -ENOMEM;
811 goto err_kzalloc;
812 }
813
814 idev->owner = owner;
815 idev->info = info;
816 init_waitqueue_head(&idev->wait);
817 atomic_set(&idev->event, 0);
818
819 ret = uio_get_minor(idev);
820 if (ret)
821 goto err_get_minor;
822
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700823 idev->dev = device_create(uio_class, parent,
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700824 MKDEV(uio_major, idev->minor), idev,
825 "uio%d", idev->minor);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100826 if (IS_ERR(idev->dev)) {
827 printk(KERN_ERR "UIO: device register failed\n");
828 ret = PTR_ERR(idev->dev);
829 goto err_device_create;
830 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100831
832 ret = uio_dev_add_attributes(idev);
833 if (ret)
834 goto err_uio_dev_add_attributes;
835
836 info->uio_dev = idev;
837
838 if (idev->info->irq >= 0) {
839 ret = request_irq(idev->info->irq, uio_interrupt,
840 idev->info->irq_flags, idev->info->name, idev);
841 if (ret)
842 goto err_request_irq;
843 }
844
845 return 0;
846
847err_request_irq:
848 uio_dev_del_attributes(idev);
849err_uio_dev_add_attributes:
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700850 device_destroy(uio_class, MKDEV(uio_major, idev->minor));
Hans J. Kochbeafc542006-12-07 10:58:29 +0100851err_device_create:
852 uio_free_minor(idev);
853err_get_minor:
854 kfree(idev);
855err_kzalloc:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100856 return ret;
857}
858EXPORT_SYMBOL_GPL(__uio_register_device);
859
860/**
861 * uio_unregister_device - unregister a industrial IO device
862 * @info: UIO device capabilities
863 *
864 */
865void uio_unregister_device(struct uio_info *info)
866{
867 struct uio_device *idev;
868
869 if (!info || !info->uio_dev)
870 return;
871
872 idev = info->uio_dev;
873
874 uio_free_minor(idev);
875
876 if (info->irq >= 0)
877 free_irq(info->irq, idev);
878
879 uio_dev_del_attributes(idev);
880
881 dev_set_drvdata(idev->dev, NULL);
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700882 device_destroy(uio_class, MKDEV(uio_major, idev->minor));
Hans J. Kochbeafc542006-12-07 10:58:29 +0100883 kfree(idev);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100884
885 return;
886}
887EXPORT_SYMBOL_GPL(uio_unregister_device);
888
889static int __init uio_init(void)
890{
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700891 return init_uio_class();
Hans J. Kochbeafc542006-12-07 10:58:29 +0100892}
893
894static void __exit uio_exit(void)
895{
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700896 release_uio_class();
Hans J. Kochbeafc542006-12-07 10:58:29 +0100897}
898
899module_init(uio_init)
900module_exit(uio_exit)
901MODULE_LICENSE("GPL v2");