uio: Cleanup irq handling.
[deliverable/linux.git] / drivers / uio / uio.c
CommitLineData
beafc54c
HK
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>
5a0e3ad6 20#include <linux/slab.h>
beafc54c
HK
21#include <linux/mm.h>
22#include <linux/idr.h>
d43c36dc 23#include <linux/sched.h>
beafc54c
HK
24#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;
81e7c6a6 39 struct kobject *map_dir;
e70c412e 40 struct kobject *portio_dir;
beafc54c
HK
41};
42
43static int uio_major;
44static DEFINE_IDR(uio_idr);
4f014691 45static const struct file_operations uio_fops;
beafc54c
HK
46
47/* UIO class infrastructure */
3d4f9d76 48static struct class *uio_class;
beafc54c 49
0d4a7bc1
JC
50/* Protect idr accesses */
51static DEFINE_MUTEX(minor_lock);
52
beafc54c
HK
53/*
54 * attributes
55 */
56
81e7c6a6
GKH
57struct uio_map {
58 struct kobject kobj;
59 struct uio_mem *mem;
beafc54c 60};
81e7c6a6 61#define to_map(map) container_of(map, struct uio_map, kobj)
beafc54c 62
82057791
HK
63static 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
4f808bcd 71static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
beafc54c 72{
4f808bcd
BP
73 return sprintf(buf, "0x%lx\n", mem->addr);
74}
beafc54c 75
4f808bcd
BP
76static ssize_t map_size_show(struct uio_mem *mem, char *buf)
77{
78 return sprintf(buf, "0x%lx\n", mem->size);
beafc54c
HK
79}
80
e2b39df1
HK
81static 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
e70c412e 86struct map_sysfs_entry {
4f808bcd
BP
87 struct attribute attr;
88 ssize_t (*show)(struct uio_mem *, char *);
89 ssize_t (*store)(struct uio_mem *, const char *, size_t);
90};
91
82057791
HK
92static struct map_sysfs_entry name_attribute =
93 __ATTR(name, S_IRUGO, map_name_show, NULL);
e70c412e 94static struct map_sysfs_entry addr_attribute =
4f808bcd 95 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
e70c412e 96static struct map_sysfs_entry size_attribute =
4f808bcd 97 __ATTR(size, S_IRUGO, map_size_show, NULL);
e70c412e 98static struct map_sysfs_entry offset_attribute =
e2b39df1 99 __ATTR(offset, S_IRUGO, map_offset_show, NULL);
beafc54c 100
81e7c6a6 101static struct attribute *attrs[] = {
82057791 102 &name_attribute.attr,
4f808bcd 103 &addr_attribute.attr,
81e7c6a6 104 &size_attribute.attr,
e2b39df1 105 &offset_attribute.attr,
81e7c6a6 106 NULL, /* need to NULL terminate the list of attributes */
beafc54c
HK
107};
108
81e7c6a6
GKH
109static void map_release(struct kobject *kobj)
110{
111 struct uio_map *map = to_map(kobj);
112 kfree(map);
113}
114
4f808bcd
BP
115static 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;
e70c412e 120 struct map_sysfs_entry *entry;
4f808bcd 121
e70c412e 122 entry = container_of(attr, struct map_sysfs_entry, attr);
4f808bcd
BP
123
124 if (!entry->show)
125 return -EIO;
126
127 return entry->show(mem, buf);
128}
129
52cf25d0 130static const struct sysfs_ops map_sysfs_ops = {
4f808bcd
BP
131 .show = map_type_show,
132};
133
beafc54c 134static struct kobj_type map_attr_type = {
81e7c6a6 135 .release = map_release,
e70c412e 136 .sysfs_ops = &map_sysfs_ops,
81e7c6a6 137 .default_attrs = attrs,
beafc54c
HK
138};
139
e70c412e
HK
140struct 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
82057791
HK
146static 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
e70c412e
HK
154static 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
82057791
HK
180static struct portio_sysfs_entry portio_name_attribute =
181 __ATTR(name, S_IRUGO, portio_name_show, NULL);
e70c412e
HK
182static 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[] = {
82057791 190 &portio_name_attribute.attr,
e70c412e
HK
191 &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
52cf25d0 218static const struct sysfs_ops portio_sysfs_ops = {
e70c412e
HK
219 .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
beafc54c
HK
228static ssize_t show_name(struct device *dev,
229 struct device_attribute *attr, char *buf)
230{
231 struct uio_device *idev = dev_get_drvdata(dev);
70a9156b 232 return sprintf(buf, "%s\n", idev->info->name);
beafc54c
HK
233}
234static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
235
236static ssize_t show_version(struct device *dev,
237 struct device_attribute *attr, char *buf)
238{
239 struct uio_device *idev = dev_get_drvdata(dev);
70a9156b 240 return sprintf(buf, "%s\n", idev->info->version);
beafc54c
HK
241}
242static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
243
244static ssize_t show_event(struct device *dev,
245 struct device_attribute *attr, char *buf)
246{
247 struct uio_device *idev = dev_get_drvdata(dev);
70a9156b 248 return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
beafc54c
HK
249}
250static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
251
252static struct attribute *uio_attrs[] = {
253 &dev_attr_name.attr,
254 &dev_attr_version.attr,
255 &dev_attr_event.attr,
256 NULL,
257};
258
259static struct attribute_group uio_attr_grp = {
260 .attrs = uio_attrs,
261};
262
263/*
264 * device functions
265 */
266static int uio_dev_add_attributes(struct uio_device *idev)
267{
268 int ret;
e70c412e 269 int mi, pi;
beafc54c 270 int map_found = 0;
e70c412e 271 int portio_found = 0;
beafc54c 272 struct uio_mem *mem;
81e7c6a6 273 struct uio_map *map;
e70c412e
HK
274 struct uio_port *port;
275 struct uio_portio *portio;
beafc54c
HK
276
277 ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
278 if (ret)
279 goto err_group;
280
281 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
282 mem = &idev->info->mem[mi];
283 if (mem->size == 0)
284 break;
285 if (!map_found) {
286 map_found = 1;
81e7c6a6
GKH
287 idev->map_dir = kobject_create_and_add("maps",
288 &idev->dev->kobj);
289 if (!idev->map_dir)
e70c412e 290 goto err_map;
beafc54c 291 }
81e7c6a6
GKH
292 map = kzalloc(sizeof(*map), GFP_KERNEL);
293 if (!map)
e70c412e 294 goto err_map;
f9cb074b 295 kobject_init(&map->kobj, &map_attr_type);
81e7c6a6
GKH
296 map->mem = mem;
297 mem->map = map;
b2d6db58 298 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
81e7c6a6 299 if (ret)
e70c412e 300 goto err_map;
81e7c6a6 301 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
beafc54c 302 if (ret)
e70c412e
HK
303 goto err_map;
304 }
305
306 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
307 port = &idev->info->port[pi];
308 if (port->size == 0)
309 break;
310 if (!portio_found) {
311 portio_found = 1;
312 idev->portio_dir = kobject_create_and_add("portio",
313 &idev->dev->kobj);
314 if (!idev->portio_dir)
315 goto err_portio;
316 }
317 portio = kzalloc(sizeof(*portio), GFP_KERNEL);
318 if (!portio)
319 goto err_portio;
320 kobject_init(&portio->kobj, &portio_attr_type);
321 portio->port = port;
322 port->portio = portio;
323 ret = kobject_add(&portio->kobj, idev->portio_dir,
324 "port%d", pi);
325 if (ret)
326 goto err_portio;
327 ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
328 if (ret)
329 goto err_portio;
beafc54c
HK
330 }
331
332 return 0;
333
e70c412e
HK
334err_portio:
335 for (pi--; pi >= 0; pi--) {
336 port = &idev->info->port[pi];
337 portio = port->portio;
338 kobject_put(&portio->kobj);
339 }
340 kobject_put(idev->portio_dir);
341err_map:
beafc54c
HK
342 for (mi--; mi>=0; mi--) {
343 mem = &idev->info->mem[mi];
81e7c6a6 344 map = mem->map;
c10997f6 345 kobject_put(&map->kobj);
beafc54c 346 }
c10997f6 347 kobject_put(idev->map_dir);
beafc54c
HK
348 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
349err_group:
350 dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
351 return ret;
352}
353
354static void uio_dev_del_attributes(struct uio_device *idev)
355{
e70c412e 356 int i;
beafc54c 357 struct uio_mem *mem;
e70c412e
HK
358 struct uio_port *port;
359
360 for (i = 0; i < MAX_UIO_MAPS; i++) {
361 mem = &idev->info->mem[i];
beafc54c
HK
362 if (mem->size == 0)
363 break;
c10997f6 364 kobject_put(&mem->map->kobj);
beafc54c 365 }
c10997f6 366 kobject_put(idev->map_dir);
e70c412e
HK
367
368 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
369 port = &idev->info->port[i];
370 if (port->size == 0)
371 break;
372 kobject_put(&port->portio->kobj);
373 }
374 kobject_put(idev->portio_dir);
375
beafc54c
HK
376 sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
377}
378
379static int uio_get_minor(struct uio_device *idev)
380{
beafc54c
HK
381 int retval = -ENOMEM;
382 int id;
383
384 mutex_lock(&minor_lock);
385 if (idr_pre_get(&uio_idr, GFP_KERNEL) == 0)
386 goto exit;
387
388 retval = idr_get_new(&uio_idr, idev, &id);
389 if (retval < 0) {
390 if (retval == -EAGAIN)
391 retval = -ENOMEM;
392 goto exit;
393 }
394 idev->minor = id & MAX_ID_MASK;
395exit:
396 mutex_unlock(&minor_lock);
397 return retval;
398}
399
400static void uio_free_minor(struct uio_device *idev)
401{
0d4a7bc1 402 mutex_lock(&minor_lock);
beafc54c 403 idr_remove(&uio_idr, idev->minor);
0d4a7bc1 404 mutex_unlock(&minor_lock);
beafc54c
HK
405}
406
407/**
408 * uio_event_notify - trigger an interrupt event
409 * @info: UIO device capabilities
410 */
411void uio_event_notify(struct uio_info *info)
412{
413 struct uio_device *idev = info->uio_dev;
414
415 atomic_inc(&idev->event);
416 wake_up_interruptible(&idev->wait);
417 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
418}
419EXPORT_SYMBOL_GPL(uio_event_notify);
420
421/**
422 * uio_interrupt - hardware interrupt handler
423 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
424 * @dev_id: Pointer to the devices uio_device structure
425 */
426static irqreturn_t uio_interrupt(int irq, void *dev_id)
427{
428 struct uio_device *idev = (struct uio_device *)dev_id;
429 irqreturn_t ret = idev->info->handler(irq, idev->info);
430
431 if (ret == IRQ_HANDLED)
432 uio_event_notify(idev->info);
433
434 return ret;
435}
436
437struct uio_listener {
438 struct uio_device *dev;
439 s32 event_count;
440};
441
442static int uio_open(struct inode *inode, struct file *filep)
443{
444 struct uio_device *idev;
445 struct uio_listener *listener;
446 int ret = 0;
447
0d4a7bc1 448 mutex_lock(&minor_lock);
beafc54c 449 idev = idr_find(&uio_idr, iminor(inode));
0d4a7bc1 450 mutex_unlock(&minor_lock);
fbc8a81d
JC
451 if (!idev) {
452 ret = -ENODEV;
453 goto out;
454 }
beafc54c 455
fbc8a81d
JC
456 if (!try_module_get(idev->owner)) {
457 ret = -ENODEV;
458 goto out;
459 }
610ad506 460
beafc54c 461 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
610ad506
UKK
462 if (!listener) {
463 ret = -ENOMEM;
464 goto err_alloc_listener;
465 }
beafc54c
HK
466
467 listener->dev = idev;
468 listener->event_count = atomic_read(&idev->event);
469 filep->private_data = listener;
470
471 if (idev->info->open) {
beafc54c 472 ret = idev->info->open(idev->info, inode);
610ad506
UKK
473 if (ret)
474 goto err_infoopen;
beafc54c 475 }
610ad506
UKK
476 return 0;
477
478err_infoopen:
610ad506 479 kfree(listener);
610ad506 480
0d4a7bc1 481err_alloc_listener:
610ad506 482 module_put(idev->owner);
beafc54c 483
fbc8a81d 484out:
beafc54c
HK
485 return ret;
486}
487
488static int uio_fasync(int fd, struct file *filep, int on)
489{
490 struct uio_listener *listener = filep->private_data;
491 struct uio_device *idev = listener->dev;
492
493 return fasync_helper(fd, filep, on, &idev->async_queue);
494}
495
496static int uio_release(struct inode *inode, struct file *filep)
497{
498 int ret = 0;
499 struct uio_listener *listener = filep->private_data;
500 struct uio_device *idev = listener->dev;
501
610ad506 502 if (idev->info->release)
beafc54c 503 ret = idev->info->release(idev->info, inode);
610ad506
UKK
504
505 module_put(idev->owner);
beafc54c
HK
506 kfree(listener);
507 return ret;
508}
509
510static unsigned int uio_poll(struct file *filep, poll_table *wait)
511{
512 struct uio_listener *listener = filep->private_data;
513 struct uio_device *idev = listener->dev;
514
6427a765 515 if (!idev->info->irq)
beafc54c
HK
516 return -EIO;
517
518 poll_wait(filep, &idev->wait, wait);
519 if (listener->event_count != atomic_read(&idev->event))
520 return POLLIN | POLLRDNORM;
521 return 0;
522}
523
524static ssize_t uio_read(struct file *filep, char __user *buf,
525 size_t count, loff_t *ppos)
526{
527 struct uio_listener *listener = filep->private_data;
528 struct uio_device *idev = listener->dev;
529 DECLARE_WAITQUEUE(wait, current);
530 ssize_t retval;
531 s32 event_count;
532
6427a765 533 if (!idev->info->irq)
beafc54c
HK
534 return -EIO;
535
536 if (count != sizeof(s32))
537 return -EINVAL;
538
539 add_wait_queue(&idev->wait, &wait);
540
541 do {
542 set_current_state(TASK_INTERRUPTIBLE);
543
544 event_count = atomic_read(&idev->event);
545 if (event_count != listener->event_count) {
546 if (copy_to_user(buf, &event_count, count))
547 retval = -EFAULT;
548 else {
549 listener->event_count = event_count;
550 retval = count;
551 }
552 break;
553 }
554
555 if (filep->f_flags & O_NONBLOCK) {
556 retval = -EAGAIN;
557 break;
558 }
559
560 if (signal_pending(current)) {
561 retval = -ERESTARTSYS;
562 break;
563 }
564 schedule();
565 } while (1);
566
567 __set_current_state(TASK_RUNNING);
568 remove_wait_queue(&idev->wait, &wait);
569
570 return retval;
571}
572
328a14e7
HK
573static ssize_t uio_write(struct file *filep, const char __user *buf,
574 size_t count, loff_t *ppos)
575{
576 struct uio_listener *listener = filep->private_data;
577 struct uio_device *idev = listener->dev;
578 ssize_t retval;
579 s32 irq_on;
580
6427a765 581 if (!idev->info->irq)
328a14e7
HK
582 return -EIO;
583
584 if (count != sizeof(s32))
585 return -EINVAL;
586
587 if (!idev->info->irqcontrol)
588 return -ENOSYS;
589
590 if (copy_from_user(&irq_on, buf, count))
591 return -EFAULT;
592
593 retval = idev->info->irqcontrol(idev->info, irq_on);
594
595 return retval ? retval : sizeof(s32);
596}
597
beafc54c
HK
598static int uio_find_mem_index(struct vm_area_struct *vma)
599{
600 int mi;
601 struct uio_device *idev = vma->vm_private_data;
602
603 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
604 if (idev->info->mem[mi].size == 0)
605 return -1;
606 if (vma->vm_pgoff == mi)
607 return mi;
608 }
609 return -1;
610}
611
612static void uio_vma_open(struct vm_area_struct *vma)
613{
614 struct uio_device *idev = vma->vm_private_data;
615 idev->vma_count++;
616}
617
618static void uio_vma_close(struct vm_area_struct *vma)
619{
620 struct uio_device *idev = vma->vm_private_data;
621 idev->vma_count--;
622}
623
a18b630d 624static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
beafc54c
HK
625{
626 struct uio_device *idev = vma->vm_private_data;
a18b630d 627 struct page *page;
02683ffd 628 unsigned long offset;
beafc54c
HK
629
630 int mi = uio_find_mem_index(vma);
631 if (mi < 0)
a18b630d 632 return VM_FAULT_SIGBUS;
beafc54c 633
02683ffd
AH
634 /*
635 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
636 * to use mem[N].
637 */
638 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
639
beafc54c 640 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
02683ffd 641 page = virt_to_page(idev->info->mem[mi].addr + offset);
beafc54c 642 else
02683ffd
AH
643 page = vmalloc_to_page((void *)idev->info->mem[mi].addr
644 + offset);
beafc54c 645 get_page(page);
a18b630d
NP
646 vmf->page = page;
647 return 0;
beafc54c
HK
648}
649
f0f37e2f 650static const struct vm_operations_struct uio_vm_ops = {
beafc54c
HK
651 .open = uio_vma_open,
652 .close = uio_vma_close,
a18b630d 653 .fault = uio_vma_fault,
beafc54c
HK
654};
655
656static int uio_mmap_physical(struct vm_area_struct *vma)
657{
658 struct uio_device *idev = vma->vm_private_data;
659 int mi = uio_find_mem_index(vma);
660 if (mi < 0)
661 return -EINVAL;
662
663 vma->vm_flags |= VM_IO | VM_RESERVED;
c9698d6b
JSC
664
665 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
beafc54c
HK
666
667 return remap_pfn_range(vma,
668 vma->vm_start,
669 idev->info->mem[mi].addr >> PAGE_SHIFT,
670 vma->vm_end - vma->vm_start,
671 vma->vm_page_prot);
672}
673
674static int uio_mmap_logical(struct vm_area_struct *vma)
675{
676 vma->vm_flags |= VM_RESERVED;
677 vma->vm_ops = &uio_vm_ops;
678 uio_vma_open(vma);
679 return 0;
680}
681
682static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
683{
684 struct uio_listener *listener = filep->private_data;
685 struct uio_device *idev = listener->dev;
686 int mi;
687 unsigned long requested_pages, actual_pages;
688 int ret = 0;
689
690 if (vma->vm_end < vma->vm_start)
691 return -EINVAL;
692
693 vma->vm_private_data = idev;
694
695 mi = uio_find_mem_index(vma);
696 if (mi < 0)
697 return -EINVAL;
698
699 requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
6da2d377
IA
700 actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
701 + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
beafc54c
HK
702 if (requested_pages > actual_pages)
703 return -EINVAL;
704
705 if (idev->info->mmap) {
beafc54c 706 ret = idev->info->mmap(idev->info, vma);
beafc54c
HK
707 return ret;
708 }
709
710 switch (idev->info->mem[mi].memtype) {
711 case UIO_MEM_PHYS:
712 return uio_mmap_physical(vma);
713 case UIO_MEM_LOGICAL:
714 case UIO_MEM_VIRTUAL:
715 return uio_mmap_logical(vma);
716 default:
717 return -EINVAL;
718 }
719}
720
4f014691 721static const struct file_operations uio_fops = {
beafc54c
HK
722 .owner = THIS_MODULE,
723 .open = uio_open,
724 .release = uio_release,
725 .read = uio_read,
328a14e7 726 .write = uio_write,
beafc54c
HK
727 .mmap = uio_mmap,
728 .poll = uio_poll,
729 .fasync = uio_fasync,
730};
731
732static int uio_major_init(void)
733{
734 uio_major = register_chrdev(0, "uio", &uio_fops);
735 if (uio_major < 0)
736 return uio_major;
737 return 0;
738}
739
740static void uio_major_cleanup(void)
741{
742 unregister_chrdev(uio_major, "uio");
743}
744
745static int init_uio_class(void)
746{
3d4f9d76
EB
747 struct class *class;
748 int ret;
beafc54c
HK
749
750 /* This is the first time in here, set everything up properly */
751 ret = uio_major_init();
752 if (ret)
753 goto exit;
754
3d4f9d76
EB
755 class = class_create(THIS_MODULE, "uio");
756 if (IS_ERR(class)) {
757 ret = IS_ERR(class);
beafc54c
HK
758 printk(KERN_ERR "class_create failed for uio\n");
759 goto err_class_create;
760 }
3d4f9d76 761 uio_class = class;
beafc54c
HK
762 return 0;
763
764err_class_create:
beafc54c
HK
765 uio_major_cleanup();
766exit:
767 return ret;
768}
769
3d4f9d76 770static void release_uio_class(void)
beafc54c
HK
771{
772 /* Ok, we cheat as we know we only have one uio_class */
3d4f9d76 773 class_destroy(uio_class);
beafc54c 774 uio_class = NULL;
3d4f9d76 775 uio_major_cleanup();
beafc54c
HK
776}
777
778/**
779 * uio_register_device - register a new userspace IO device
780 * @owner: module that creates the new device
781 * @parent: parent device
782 * @info: UIO device capabilities
783 *
784 * returns zero on success or a negative error code.
785 */
786int __uio_register_device(struct module *owner,
787 struct device *parent,
788 struct uio_info *info)
789{
790 struct uio_device *idev;
791 int ret = 0;
792
793 if (!parent || !info || !info->name || !info->version)
794 return -EINVAL;
795
796 info->uio_dev = NULL;
797
beafc54c
HK
798 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
799 if (!idev) {
800 ret = -ENOMEM;
801 goto err_kzalloc;
802 }
803
804 idev->owner = owner;
805 idev->info = info;
806 init_waitqueue_head(&idev->wait);
807 atomic_set(&idev->event, 0);
808
809 ret = uio_get_minor(idev);
810 if (ret)
811 goto err_get_minor;
812
3d4f9d76 813 idev->dev = device_create(uio_class, parent,
a9b12619
GKH
814 MKDEV(uio_major, idev->minor), idev,
815 "uio%d", idev->minor);
beafc54c
HK
816 if (IS_ERR(idev->dev)) {
817 printk(KERN_ERR "UIO: device register failed\n");
818 ret = PTR_ERR(idev->dev);
819 goto err_device_create;
820 }
beafc54c
HK
821
822 ret = uio_dev_add_attributes(idev);
823 if (ret)
824 goto err_uio_dev_add_attributes;
825
826 info->uio_dev = idev;
827
6427a765
EB
828 if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
829 ret = request_irq(info->irq, uio_interrupt,
830 info->irq_flags, info->name, idev);
beafc54c
HK
831 if (ret)
832 goto err_request_irq;
833 }
834
835 return 0;
836
837err_request_irq:
838 uio_dev_del_attributes(idev);
839err_uio_dev_add_attributes:
3d4f9d76 840 device_destroy(uio_class, MKDEV(uio_major, idev->minor));
beafc54c
HK
841err_device_create:
842 uio_free_minor(idev);
843err_get_minor:
844 kfree(idev);
845err_kzalloc:
beafc54c
HK
846 return ret;
847}
848EXPORT_SYMBOL_GPL(__uio_register_device);
849
850/**
851 * uio_unregister_device - unregister a industrial IO device
852 * @info: UIO device capabilities
853 *
854 */
855void uio_unregister_device(struct uio_info *info)
856{
857 struct uio_device *idev;
858
859 if (!info || !info->uio_dev)
860 return;
861
862 idev = info->uio_dev;
863
864 uio_free_minor(idev);
865
6427a765 866 if (info->irq && (info->irq != UIO_IRQ_CUSTOM))
beafc54c
HK
867 free_irq(info->irq, idev);
868
869 uio_dev_del_attributes(idev);
870
3d4f9d76 871 device_destroy(uio_class, MKDEV(uio_major, idev->minor));
beafc54c 872 kfree(idev);
beafc54c
HK
873
874 return;
875}
876EXPORT_SYMBOL_GPL(uio_unregister_device);
877
878static int __init uio_init(void)
879{
3d4f9d76 880 return init_uio_class();
beafc54c
HK
881}
882
883static void __exit uio_exit(void)
884{
3d4f9d76 885 release_uio_class();
beafc54c
HK
886}
887
888module_init(uio_init)
889module_exit(uio_exit)
890MODULE_LICENSE("GPL v2");
This page took 0.36343 seconds and 5 git commands to generate.