blob: e941367dd28f8ab907350e83cedc7e1b1ac40a94 [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>
20#include <linux/mm.h>
21#include <linux/idr.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040022#include <linux/sched.h>
Hans J. Kochbeafc542006-12-07 10:58:29 +010023#include <linux/string.h>
24#include <linux/kobject.h>
25#include <linux/uio_driver.h>
26
27#define UIO_MAX_DEVICES 255
28
29struct uio_device {
30 struct module *owner;
31 struct device *dev;
32 int minor;
33 atomic_t event;
34 struct fasync_struct *async_queue;
35 wait_queue_head_t wait;
36 int vma_count;
37 struct uio_info *info;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000038 struct kobject *map_dir;
Hans J. Koche70c4122008-12-06 02:23:13 +010039 struct kobject *portio_dir;
Hans J. Kochbeafc542006-12-07 10:58:29 +010040};
41
42static int uio_major;
43static DEFINE_IDR(uio_idr);
Jan Engelhardt4f014692008-01-22 20:50:54 +010044static const struct file_operations uio_fops;
Hans J. Kochbeafc542006-12-07 10:58:29 +010045
46/* UIO class infrastructure */
47static struct uio_class {
48 struct kref kref;
49 struct class *class;
50} *uio_class;
51
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -060052/* Protect idr accesses */
53static DEFINE_MUTEX(minor_lock);
54
Hans J. Kochbeafc542006-12-07 10:58:29 +010055/*
56 * attributes
57 */
58
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000059struct uio_map {
60 struct kobject kobj;
61 struct uio_mem *mem;
Hans J. Kochbeafc542006-12-07 10:58:29 +010062};
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000063#define to_map(map) container_of(map, struct uio_map, kobj)
Hans J. Kochbeafc542006-12-07 10:58:29 +010064
Hans J. Koch82057792009-01-07 00:15:39 +010065static ssize_t map_name_show(struct uio_mem *mem, char *buf)
66{
67 if (unlikely(!mem->name))
68 mem->name = "";
69
70 return sprintf(buf, "%s\n", mem->name);
71}
72
Brandon Philips4f808bc2008-02-19 01:55:05 -080073static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
Hans J. Kochbeafc542006-12-07 10:58:29 +010074{
Brandon Philips4f808bc2008-02-19 01:55:05 -080075 return sprintf(buf, "0x%lx\n", mem->addr);
Hans J. Kochbeafc542006-12-07 10:58:29 +010076}
77
Brandon Philips4f808bc2008-02-19 01:55:05 -080078static ssize_t map_size_show(struct uio_mem *mem, char *buf)
79{
80 return sprintf(buf, "0x%lx\n", mem->size);
81}
82
Hans J. Koche2b39df2008-09-18 23:53:18 +020083static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
84{
85 return sprintf(buf, "0x%lx\n", mem->addr & ~PAGE_MASK);
86}
87
Hans J. Koche70c4122008-12-06 02:23:13 +010088struct map_sysfs_entry {
Brandon Philips4f808bc2008-02-19 01:55:05 -080089 struct attribute attr;
90 ssize_t (*show)(struct uio_mem *, char *);
91 ssize_t (*store)(struct uio_mem *, const char *, size_t);
92};
93
Hans J. Koch82057792009-01-07 00:15:39 +010094static struct map_sysfs_entry name_attribute =
95 __ATTR(name, S_IRUGO, map_name_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +010096static struct map_sysfs_entry addr_attribute =
Brandon Philips4f808bc2008-02-19 01:55:05 -080097 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +010098static struct map_sysfs_entry size_attribute =
Brandon Philips4f808bc2008-02-19 01:55:05 -080099 __ATTR(size, S_IRUGO, map_size_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +0100100static struct map_sysfs_entry offset_attribute =
Hans J. Koche2b39df2008-09-18 23:53:18 +0200101 __ATTR(offset, S_IRUGO, map_offset_show, NULL);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100102
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000103static struct attribute *attrs[] = {
Hans J. Koch82057792009-01-07 00:15:39 +0100104 &name_attribute.attr,
Brandon Philips4f808bc2008-02-19 01:55:05 -0800105 &addr_attribute.attr,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000106 &size_attribute.attr,
Hans J. Koche2b39df2008-09-18 23:53:18 +0200107 &offset_attribute.attr,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000108 NULL, /* need to NULL terminate the list of attributes */
Hans J. Kochbeafc542006-12-07 10:58:29 +0100109};
110
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000111static void map_release(struct kobject *kobj)
112{
113 struct uio_map *map = to_map(kobj);
114 kfree(map);
115}
116
Brandon Philips4f808bc2008-02-19 01:55:05 -0800117static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
118 char *buf)
119{
120 struct uio_map *map = to_map(kobj);
121 struct uio_mem *mem = map->mem;
Hans J. Koche70c4122008-12-06 02:23:13 +0100122 struct map_sysfs_entry *entry;
Brandon Philips4f808bc2008-02-19 01:55:05 -0800123
Hans J. Koche70c4122008-12-06 02:23:13 +0100124 entry = container_of(attr, struct map_sysfs_entry, attr);
Brandon Philips4f808bc2008-02-19 01:55:05 -0800125
126 if (!entry->show)
127 return -EIO;
128
129 return entry->show(mem, buf);
130}
131
Hans J. Koche70c4122008-12-06 02:23:13 +0100132static struct sysfs_ops map_sysfs_ops = {
Brandon Philips4f808bc2008-02-19 01:55:05 -0800133 .show = map_type_show,
134};
135
Hans J. Kochbeafc542006-12-07 10:58:29 +0100136static struct kobj_type map_attr_type = {
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000137 .release = map_release,
Hans J. Koche70c4122008-12-06 02:23:13 +0100138 .sysfs_ops = &map_sysfs_ops,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000139 .default_attrs = attrs,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100140};
141
Hans J. Koche70c4122008-12-06 02:23:13 +0100142struct uio_portio {
143 struct kobject kobj;
144 struct uio_port *port;
145};
146#define to_portio(portio) container_of(portio, struct uio_portio, kobj)
147
Hans J. Koch82057792009-01-07 00:15:39 +0100148static ssize_t portio_name_show(struct uio_port *port, char *buf)
149{
150 if (unlikely(!port->name))
151 port->name = "";
152
153 return sprintf(buf, "%s\n", port->name);
154}
155
Hans J. Koche70c4122008-12-06 02:23:13 +0100156static ssize_t portio_start_show(struct uio_port *port, char *buf)
157{
158 return sprintf(buf, "0x%lx\n", port->start);
159}
160
161static ssize_t portio_size_show(struct uio_port *port, char *buf)
162{
163 return sprintf(buf, "0x%lx\n", port->size);
164}
165
166static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
167{
168 const char *porttypes[] = {"none", "x86", "gpio", "other"};
169
170 if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
171 return -EINVAL;
172
173 return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
174}
175
176struct portio_sysfs_entry {
177 struct attribute attr;
178 ssize_t (*show)(struct uio_port *, char *);
179 ssize_t (*store)(struct uio_port *, const char *, size_t);
180};
181
Hans J. Koch82057792009-01-07 00:15:39 +0100182static struct portio_sysfs_entry portio_name_attribute =
183 __ATTR(name, S_IRUGO, portio_name_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +0100184static struct portio_sysfs_entry portio_start_attribute =
185 __ATTR(start, S_IRUGO, portio_start_show, NULL);
186static struct portio_sysfs_entry portio_size_attribute =
187 __ATTR(size, S_IRUGO, portio_size_show, NULL);
188static struct portio_sysfs_entry portio_porttype_attribute =
189 __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
190
191static struct attribute *portio_attrs[] = {
Hans J. Koch82057792009-01-07 00:15:39 +0100192 &portio_name_attribute.attr,
Hans J. Koche70c4122008-12-06 02:23:13 +0100193 &portio_start_attribute.attr,
194 &portio_size_attribute.attr,
195 &portio_porttype_attribute.attr,
196 NULL,
197};
198
199static void portio_release(struct kobject *kobj)
200{
201 struct uio_portio *portio = to_portio(kobj);
202 kfree(portio);
203}
204
205static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
206 char *buf)
207{
208 struct uio_portio *portio = to_portio(kobj);
209 struct uio_port *port = portio->port;
210 struct portio_sysfs_entry *entry;
211
212 entry = container_of(attr, struct portio_sysfs_entry, attr);
213
214 if (!entry->show)
215 return -EIO;
216
217 return entry->show(port, buf);
218}
219
220static struct sysfs_ops portio_sysfs_ops = {
221 .show = portio_type_show,
222};
223
224static struct kobj_type portio_attr_type = {
225 .release = portio_release,
226 .sysfs_ops = &portio_sysfs_ops,
227 .default_attrs = portio_attrs,
228};
229
Hans J. Kochbeafc542006-12-07 10:58:29 +0100230static ssize_t show_name(struct device *dev,
231 struct device_attribute *attr, char *buf)
232{
233 struct uio_device *idev = dev_get_drvdata(dev);
234 if (idev)
235 return sprintf(buf, "%s\n", idev->info->name);
236 else
237 return -ENODEV;
238}
239static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
240
241static ssize_t show_version(struct device *dev,
242 struct device_attribute *attr, char *buf)
243{
244 struct uio_device *idev = dev_get_drvdata(dev);
245 if (idev)
246 return sprintf(buf, "%s\n", idev->info->version);
247 else
248 return -ENODEV;
249}
250static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
251
252static ssize_t show_event(struct device *dev,
253 struct device_attribute *attr, char *buf)
254{
255 struct uio_device *idev = dev_get_drvdata(dev);
256 if (idev)
257 return sprintf(buf, "%u\n",
258 (unsigned int)atomic_read(&idev->event));
259 else
260 return -ENODEV;
261}
262static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
263
264static struct attribute *uio_attrs[] = {
265 &dev_attr_name.attr,
266 &dev_attr_version.attr,
267 &dev_attr_event.attr,
268 NULL,
269};
270
271static struct attribute_group uio_attr_grp = {
272 .attrs = uio_attrs,
273};
274
275/*
276 * device functions
277 */
278static int uio_dev_add_attributes(struct uio_device *idev)
279{
280 int ret;
Hans J. Koche70c4122008-12-06 02:23:13 +0100281 int mi, pi;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100282 int map_found = 0;
Hans J. Koche70c4122008-12-06 02:23:13 +0100283 int portio_found = 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100284 struct uio_mem *mem;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000285 struct uio_map *map;
Hans J. Koche70c4122008-12-06 02:23:13 +0100286 struct uio_port *port;
287 struct uio_portio *portio;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100288
289 ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
290 if (ret)
291 goto err_group;
292
293 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
294 mem = &idev->info->mem[mi];
295 if (mem->size == 0)
296 break;
297 if (!map_found) {
298 map_found = 1;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000299 idev->map_dir = kobject_create_and_add("maps",
300 &idev->dev->kobj);
301 if (!idev->map_dir)
Hans J. Koche70c4122008-12-06 02:23:13 +0100302 goto err_map;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100303 }
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000304 map = kzalloc(sizeof(*map), GFP_KERNEL);
305 if (!map)
Hans J. Koche70c4122008-12-06 02:23:13 +0100306 goto err_map;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700307 kobject_init(&map->kobj, &map_attr_type);
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000308 map->mem = mem;
309 mem->map = map;
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700310 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100311 if (ret)
Hans J. Koche70c4122008-12-06 02:23:13 +0100312 goto err_map;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000313 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
314 if (ret)
Hans J. Koche70c4122008-12-06 02:23:13 +0100315 goto err_map;
316 }
317
318 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
319 port = &idev->info->port[pi];
320 if (port->size == 0)
321 break;
322 if (!portio_found) {
323 portio_found = 1;
324 idev->portio_dir = kobject_create_and_add("portio",
325 &idev->dev->kobj);
326 if (!idev->portio_dir)
327 goto err_portio;
328 }
329 portio = kzalloc(sizeof(*portio), GFP_KERNEL);
330 if (!portio)
331 goto err_portio;
332 kobject_init(&portio->kobj, &portio_attr_type);
333 portio->port = port;
334 port->portio = portio;
335 ret = kobject_add(&portio->kobj, idev->portio_dir,
336 "port%d", pi);
337 if (ret)
338 goto err_portio;
339 ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
340 if (ret)
341 goto err_portio;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100342 }
343
344 return 0;
345
Hans J. Koche70c4122008-12-06 02:23:13 +0100346err_portio:
347 for (pi--; pi >= 0; pi--) {
348 port = &idev->info->port[pi];
349 portio = port->portio;
350 kobject_put(&portio->kobj);
351 }
352 kobject_put(idev->portio_dir);
353err_map:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100354 for (mi--; mi>=0; mi--) {
355 mem = &idev->info->mem[mi];
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000356 map = mem->map;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800357 kobject_put(&map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100358 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800359 kobject_put(idev->map_dir);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100360 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
361err_group:
362 dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
363 return ret;
364}
365
366static void uio_dev_del_attributes(struct uio_device *idev)
367{
Hans J. Koche70c4122008-12-06 02:23:13 +0100368 int i;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100369 struct uio_mem *mem;
Hans J. Koche70c4122008-12-06 02:23:13 +0100370 struct uio_port *port;
371
372 for (i = 0; i < MAX_UIO_MAPS; i++) {
373 mem = &idev->info->mem[i];
Hans J. Kochbeafc542006-12-07 10:58:29 +0100374 if (mem->size == 0)
375 break;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800376 kobject_put(&mem->map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100377 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800378 kobject_put(idev->map_dir);
Hans J. Koche70c4122008-12-06 02:23:13 +0100379
380 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
381 port = &idev->info->port[i];
382 if (port->size == 0)
383 break;
384 kobject_put(&port->portio->kobj);
385 }
386 kobject_put(idev->portio_dir);
387
Hans J. Kochbeafc542006-12-07 10:58:29 +0100388 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
389}
390
391static int uio_get_minor(struct uio_device *idev)
392{
Hans J. Kochbeafc542006-12-07 10:58:29 +0100393 int retval = -ENOMEM;
394 int id;
395
396 mutex_lock(&minor_lock);
397 if (idr_pre_get(&uio_idr, GFP_KERNEL) == 0)
398 goto exit;
399
400 retval = idr_get_new(&uio_idr, idev, &id);
401 if (retval < 0) {
402 if (retval == -EAGAIN)
403 retval = -ENOMEM;
404 goto exit;
405 }
406 idev->minor = id & MAX_ID_MASK;
407exit:
408 mutex_unlock(&minor_lock);
409 return retval;
410}
411
412static void uio_free_minor(struct uio_device *idev)
413{
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600414 mutex_lock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100415 idr_remove(&uio_idr, idev->minor);
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600416 mutex_unlock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100417}
418
419/**
420 * uio_event_notify - trigger an interrupt event
421 * @info: UIO device capabilities
422 */
423void uio_event_notify(struct uio_info *info)
424{
425 struct uio_device *idev = info->uio_dev;
426
427 atomic_inc(&idev->event);
428 wake_up_interruptible(&idev->wait);
429 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
430}
431EXPORT_SYMBOL_GPL(uio_event_notify);
432
433/**
434 * uio_interrupt - hardware interrupt handler
435 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
436 * @dev_id: Pointer to the devices uio_device structure
437 */
438static irqreturn_t uio_interrupt(int irq, void *dev_id)
439{
440 struct uio_device *idev = (struct uio_device *)dev_id;
441 irqreturn_t ret = idev->info->handler(irq, idev->info);
442
443 if (ret == IRQ_HANDLED)
444 uio_event_notify(idev->info);
445
446 return ret;
447}
448
449struct uio_listener {
450 struct uio_device *dev;
451 s32 event_count;
452};
453
454static int uio_open(struct inode *inode, struct file *filep)
455{
456 struct uio_device *idev;
457 struct uio_listener *listener;
458 int ret = 0;
459
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600460 mutex_lock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100461 idev = idr_find(&uio_idr, iminor(inode));
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600462 mutex_unlock(&minor_lock);
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600463 if (!idev) {
464 ret = -ENODEV;
465 goto out;
466 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100467
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600468 if (!try_module_get(idev->owner)) {
469 ret = -ENODEV;
470 goto out;
471 }
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200472
Hans J. Kochbeafc542006-12-07 10:58:29 +0100473 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200474 if (!listener) {
475 ret = -ENOMEM;
476 goto err_alloc_listener;
477 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100478
479 listener->dev = idev;
480 listener->event_count = atomic_read(&idev->event);
481 filep->private_data = listener;
482
483 if (idev->info->open) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100484 ret = idev->info->open(idev->info, inode);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200485 if (ret)
486 goto err_infoopen;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100487 }
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200488 return 0;
489
490err_infoopen:
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200491 kfree(listener);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200492
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600493err_alloc_listener:
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200494 module_put(idev->owner);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100495
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600496out:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100497 return ret;
498}
499
500static int uio_fasync(int fd, struct file *filep, int on)
501{
502 struct uio_listener *listener = filep->private_data;
503 struct uio_device *idev = listener->dev;
504
505 return fasync_helper(fd, filep, on, &idev->async_queue);
506}
507
508static int uio_release(struct inode *inode, struct file *filep)
509{
510 int ret = 0;
511 struct uio_listener *listener = filep->private_data;
512 struct uio_device *idev = listener->dev;
513
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200514 if (idev->info->release)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100515 ret = idev->info->release(idev->info, inode);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200516
517 module_put(idev->owner);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100518 kfree(listener);
519 return ret;
520}
521
522static unsigned int uio_poll(struct file *filep, poll_table *wait)
523{
524 struct uio_listener *listener = filep->private_data;
525 struct uio_device *idev = listener->dev;
526
527 if (idev->info->irq == UIO_IRQ_NONE)
528 return -EIO;
529
530 poll_wait(filep, &idev->wait, wait);
531 if (listener->event_count != atomic_read(&idev->event))
532 return POLLIN | POLLRDNORM;
533 return 0;
534}
535
536static ssize_t uio_read(struct file *filep, char __user *buf,
537 size_t count, loff_t *ppos)
538{
539 struct uio_listener *listener = filep->private_data;
540 struct uio_device *idev = listener->dev;
541 DECLARE_WAITQUEUE(wait, current);
542 ssize_t retval;
543 s32 event_count;
544
545 if (idev->info->irq == UIO_IRQ_NONE)
546 return -EIO;
547
548 if (count != sizeof(s32))
549 return -EINVAL;
550
551 add_wait_queue(&idev->wait, &wait);
552
553 do {
554 set_current_state(TASK_INTERRUPTIBLE);
555
556 event_count = atomic_read(&idev->event);
557 if (event_count != listener->event_count) {
558 if (copy_to_user(buf, &event_count, count))
559 retval = -EFAULT;
560 else {
561 listener->event_count = event_count;
562 retval = count;
563 }
564 break;
565 }
566
567 if (filep->f_flags & O_NONBLOCK) {
568 retval = -EAGAIN;
569 break;
570 }
571
572 if (signal_pending(current)) {
573 retval = -ERESTARTSYS;
574 break;
575 }
576 schedule();
577 } while (1);
578
579 __set_current_state(TASK_RUNNING);
580 remove_wait_queue(&idev->wait, &wait);
581
582 return retval;
583}
584
Hans J. Koch328a14e2008-05-23 13:50:14 +0200585static ssize_t uio_write(struct file *filep, const char __user *buf,
586 size_t count, loff_t *ppos)
587{
588 struct uio_listener *listener = filep->private_data;
589 struct uio_device *idev = listener->dev;
590 ssize_t retval;
591 s32 irq_on;
592
593 if (idev->info->irq == UIO_IRQ_NONE)
594 return -EIO;
595
596 if (count != sizeof(s32))
597 return -EINVAL;
598
599 if (!idev->info->irqcontrol)
600 return -ENOSYS;
601
602 if (copy_from_user(&irq_on, buf, count))
603 return -EFAULT;
604
605 retval = idev->info->irqcontrol(idev->info, irq_on);
606
607 return retval ? retval : sizeof(s32);
608}
609
Hans J. Kochbeafc542006-12-07 10:58:29 +0100610static int uio_find_mem_index(struct vm_area_struct *vma)
611{
612 int mi;
613 struct uio_device *idev = vma->vm_private_data;
614
615 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
616 if (idev->info->mem[mi].size == 0)
617 return -1;
618 if (vma->vm_pgoff == mi)
619 return mi;
620 }
621 return -1;
622}
623
624static void uio_vma_open(struct vm_area_struct *vma)
625{
626 struct uio_device *idev = vma->vm_private_data;
627 idev->vma_count++;
628}
629
630static void uio_vma_close(struct vm_area_struct *vma)
631{
632 struct uio_device *idev = vma->vm_private_data;
633 idev->vma_count--;
634}
635
Nick Piggina18b6302008-02-06 01:37:35 -0800636static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100637{
638 struct uio_device *idev = vma->vm_private_data;
Nick Piggina18b6302008-02-06 01:37:35 -0800639 struct page *page;
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200640 unsigned long offset;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100641
642 int mi = uio_find_mem_index(vma);
643 if (mi < 0)
Nick Piggina18b6302008-02-06 01:37:35 -0800644 return VM_FAULT_SIGBUS;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100645
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200646 /*
647 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
648 * to use mem[N].
649 */
650 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
651
Hans J. Kochbeafc542006-12-07 10:58:29 +0100652 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200653 page = virt_to_page(idev->info->mem[mi].addr + offset);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100654 else
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200655 page = vmalloc_to_page((void *)idev->info->mem[mi].addr
656 + offset);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100657 get_page(page);
Nick Piggina18b6302008-02-06 01:37:35 -0800658 vmf->page = page;
659 return 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100660}
661
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400662static const struct vm_operations_struct uio_vm_ops = {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100663 .open = uio_vma_open,
664 .close = uio_vma_close,
Nick Piggina18b6302008-02-06 01:37:35 -0800665 .fault = uio_vma_fault,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100666};
667
668static int uio_mmap_physical(struct vm_area_struct *vma)
669{
670 struct uio_device *idev = vma->vm_private_data;
671 int mi = uio_find_mem_index(vma);
672 if (mi < 0)
673 return -EINVAL;
674
675 vma->vm_flags |= VM_IO | VM_RESERVED;
676
Jean-Samuel Chenardc9698d6b2008-03-14 11:28:36 +0100677 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
678
Hans J. Kochbeafc542006-12-07 10:58:29 +0100679 return remap_pfn_range(vma,
680 vma->vm_start,
681 idev->info->mem[mi].addr >> PAGE_SHIFT,
682 vma->vm_end - vma->vm_start,
683 vma->vm_page_prot);
684}
685
686static int uio_mmap_logical(struct vm_area_struct *vma)
687{
688 vma->vm_flags |= VM_RESERVED;
689 vma->vm_ops = &uio_vm_ops;
690 uio_vma_open(vma);
691 return 0;
692}
693
694static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
695{
696 struct uio_listener *listener = filep->private_data;
697 struct uio_device *idev = listener->dev;
698 int mi;
699 unsigned long requested_pages, actual_pages;
700 int ret = 0;
701
702 if (vma->vm_end < vma->vm_start)
703 return -EINVAL;
704
705 vma->vm_private_data = idev;
706
707 mi = uio_find_mem_index(vma);
708 if (mi < 0)
709 return -EINVAL;
710
711 requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
Ian Abbott6da2d372009-02-24 17:22:59 +0000712 actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
713 + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100714 if (requested_pages > actual_pages)
715 return -EINVAL;
716
717 if (idev->info->mmap) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100718 ret = idev->info->mmap(idev->info, vma);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100719 return ret;
720 }
721
722 switch (idev->info->mem[mi].memtype) {
723 case UIO_MEM_PHYS:
724 return uio_mmap_physical(vma);
725 case UIO_MEM_LOGICAL:
726 case UIO_MEM_VIRTUAL:
727 return uio_mmap_logical(vma);
728 default:
729 return -EINVAL;
730 }
731}
732
Jan Engelhardt4f014692008-01-22 20:50:54 +0100733static const struct file_operations uio_fops = {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100734 .owner = THIS_MODULE,
735 .open = uio_open,
736 .release = uio_release,
737 .read = uio_read,
Hans J. Koch328a14e2008-05-23 13:50:14 +0200738 .write = uio_write,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100739 .mmap = uio_mmap,
740 .poll = uio_poll,
741 .fasync = uio_fasync,
742};
743
744static int uio_major_init(void)
745{
746 uio_major = register_chrdev(0, "uio", &uio_fops);
747 if (uio_major < 0)
748 return uio_major;
749 return 0;
750}
751
752static void uio_major_cleanup(void)
753{
754 unregister_chrdev(uio_major, "uio");
755}
756
757static int init_uio_class(void)
758{
759 int ret = 0;
760
761 if (uio_class != NULL) {
762 kref_get(&uio_class->kref);
763 goto exit;
764 }
765
766 /* This is the first time in here, set everything up properly */
767 ret = uio_major_init();
768 if (ret)
769 goto exit;
770
771 uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL);
772 if (!uio_class) {
773 ret = -ENOMEM;
774 goto err_kzalloc;
775 }
776
777 kref_init(&uio_class->kref);
778 uio_class->class = class_create(THIS_MODULE, "uio");
779 if (IS_ERR(uio_class->class)) {
780 ret = IS_ERR(uio_class->class);
781 printk(KERN_ERR "class_create failed for uio\n");
782 goto err_class_create;
783 }
784 return 0;
785
786err_class_create:
787 kfree(uio_class);
788 uio_class = NULL;
789err_kzalloc:
790 uio_major_cleanup();
791exit:
792 return ret;
793}
794
795static void release_uio_class(struct kref *kref)
796{
797 /* Ok, we cheat as we know we only have one uio_class */
798 class_destroy(uio_class->class);
799 kfree(uio_class);
800 uio_major_cleanup();
801 uio_class = NULL;
802}
803
804static void uio_class_destroy(void)
805{
806 if (uio_class)
807 kref_put(&uio_class->kref, release_uio_class);
808}
809
810/**
811 * uio_register_device - register a new userspace IO device
812 * @owner: module that creates the new device
813 * @parent: parent device
814 * @info: UIO device capabilities
815 *
816 * returns zero on success or a negative error code.
817 */
818int __uio_register_device(struct module *owner,
819 struct device *parent,
820 struct uio_info *info)
821{
822 struct uio_device *idev;
823 int ret = 0;
824
825 if (!parent || !info || !info->name || !info->version)
826 return -EINVAL;
827
828 info->uio_dev = NULL;
829
830 ret = init_uio_class();
831 if (ret)
832 return ret;
833
834 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
835 if (!idev) {
836 ret = -ENOMEM;
837 goto err_kzalloc;
838 }
839
840 idev->owner = owner;
841 idev->info = info;
842 init_waitqueue_head(&idev->wait);
843 atomic_set(&idev->event, 0);
844
845 ret = uio_get_minor(idev);
846 if (ret)
847 goto err_get_minor;
848
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700849 idev->dev = device_create(uio_class->class, parent,
850 MKDEV(uio_major, idev->minor), idev,
851 "uio%d", idev->minor);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100852 if (IS_ERR(idev->dev)) {
853 printk(KERN_ERR "UIO: device register failed\n");
854 ret = PTR_ERR(idev->dev);
855 goto err_device_create;
856 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100857
858 ret = uio_dev_add_attributes(idev);
859 if (ret)
860 goto err_uio_dev_add_attributes;
861
862 info->uio_dev = idev;
863
864 if (idev->info->irq >= 0) {
865 ret = request_irq(idev->info->irq, uio_interrupt,
866 idev->info->irq_flags, idev->info->name, idev);
867 if (ret)
868 goto err_request_irq;
869 }
870
871 return 0;
872
873err_request_irq:
874 uio_dev_del_attributes(idev);
875err_uio_dev_add_attributes:
876 device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
877err_device_create:
878 uio_free_minor(idev);
879err_get_minor:
880 kfree(idev);
881err_kzalloc:
882 uio_class_destroy();
883 return ret;
884}
885EXPORT_SYMBOL_GPL(__uio_register_device);
886
887/**
888 * uio_unregister_device - unregister a industrial IO device
889 * @info: UIO device capabilities
890 *
891 */
892void uio_unregister_device(struct uio_info *info)
893{
894 struct uio_device *idev;
895
896 if (!info || !info->uio_dev)
897 return;
898
899 idev = info->uio_dev;
900
901 uio_free_minor(idev);
902
903 if (info->irq >= 0)
904 free_irq(info->irq, idev);
905
906 uio_dev_del_attributes(idev);
907
908 dev_set_drvdata(idev->dev, NULL);
909 device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
910 kfree(idev);
911 uio_class_destroy();
912
913 return;
914}
915EXPORT_SYMBOL_GPL(uio_unregister_device);
916
917static int __init uio_init(void)
918{
919 return 0;
920}
921
922static void __exit uio_exit(void)
923{
924}
925
926module_init(uio_init)
927module_exit(uio_exit)
928MODULE_LICENSE("GPL v2");