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>
13 * Licensed under the GPLv2 only.
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/poll.h>
19 #include <linux/device.h>
21 #include <linux/idr.h>
22 #include <linux/string.h>
23 #include <linux/kobject.h>
24 #include <linux/uio_driver.h>
26 #define UIO_MAX_DEVICES 255
33 struct fasync_struct
*async_queue
;
34 wait_queue_head_t wait
;
36 struct uio_info
*info
;
37 struct kobject
*map_dir
;
41 static DEFINE_IDR(uio_idr
);
42 static const struct file_operations uio_fops
;
44 /* UIO class infrastructure */
45 static struct uio_class
{
58 #define to_map(map) container_of(map, struct uio_map, kobj)
60 static ssize_t
map_addr_show(struct uio_mem
*mem
, char *buf
)
62 return sprintf(buf
, "0x%lx\n", mem
->addr
);
65 static ssize_t
map_size_show(struct uio_mem
*mem
, char *buf
)
67 return sprintf(buf
, "0x%lx\n", mem
->size
);
70 static ssize_t
map_offset_show(struct uio_mem
*mem
, char *buf
)
72 return sprintf(buf
, "0x%lx\n", mem
->addr
& ~PAGE_MASK
);
75 struct uio_sysfs_entry
{
76 struct attribute attr
;
77 ssize_t (*show
)(struct uio_mem
*, char *);
78 ssize_t (*store
)(struct uio_mem
*, const char *, size_t);
81 static struct uio_sysfs_entry addr_attribute
=
82 __ATTR(addr
, S_IRUGO
, map_addr_show
, NULL
);
83 static struct uio_sysfs_entry size_attribute
=
84 __ATTR(size
, S_IRUGO
, map_size_show
, NULL
);
85 static struct uio_sysfs_entry offset_attribute
=
86 __ATTR(offset
, S_IRUGO
, map_offset_show
, NULL
);
88 static struct attribute
*attrs
[] = {
91 &offset_attribute
.attr
,
92 NULL
, /* need to NULL terminate the list of attributes */
95 static void map_release(struct kobject
*kobj
)
97 struct uio_map
*map
= to_map(kobj
);
101 static ssize_t
map_type_show(struct kobject
*kobj
, struct attribute
*attr
,
104 struct uio_map
*map
= to_map(kobj
);
105 struct uio_mem
*mem
= map
->mem
;
106 struct uio_sysfs_entry
*entry
;
108 entry
= container_of(attr
, struct uio_sysfs_entry
, attr
);
113 return entry
->show(mem
, buf
);
116 static struct sysfs_ops uio_sysfs_ops
= {
117 .show
= map_type_show
,
120 static struct kobj_type map_attr_type
= {
121 .release
= map_release
,
122 .sysfs_ops
= &uio_sysfs_ops
,
123 .default_attrs
= attrs
,
126 static ssize_t
show_name(struct device
*dev
,
127 struct device_attribute
*attr
, char *buf
)
129 struct uio_device
*idev
= dev_get_drvdata(dev
);
131 return sprintf(buf
, "%s\n", idev
->info
->name
);
135 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
137 static ssize_t
show_version(struct device
*dev
,
138 struct device_attribute
*attr
, char *buf
)
140 struct uio_device
*idev
= dev_get_drvdata(dev
);
142 return sprintf(buf
, "%s\n", idev
->info
->version
);
146 static DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
148 static ssize_t
show_event(struct device
*dev
,
149 struct device_attribute
*attr
, char *buf
)
151 struct uio_device
*idev
= dev_get_drvdata(dev
);
153 return sprintf(buf
, "%u\n",
154 (unsigned int)atomic_read(&idev
->event
));
158 static DEVICE_ATTR(event
, S_IRUGO
, show_event
, NULL
);
160 static struct attribute
*uio_attrs
[] = {
162 &dev_attr_version
.attr
,
163 &dev_attr_event
.attr
,
167 static struct attribute_group uio_attr_grp
= {
174 static int uio_dev_add_attributes(struct uio_device
*idev
)
182 ret
= sysfs_create_group(&idev
->dev
->kobj
, &uio_attr_grp
);
186 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
187 mem
= &idev
->info
->mem
[mi
];
192 idev
->map_dir
= kobject_create_and_add("maps",
197 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
200 kobject_init(&map
->kobj
, &map_attr_type
);
203 ret
= kobject_add(&map
->kobj
, idev
->map_dir
, "map%d", mi
);
206 ret
= kobject_uevent(&map
->kobj
, KOBJ_ADD
);
214 for (mi
--; mi
>=0; mi
--) {
215 mem
= &idev
->info
->mem
[mi
];
217 kobject_put(&map
->kobj
);
219 kobject_put(idev
->map_dir
);
220 sysfs_remove_group(&idev
->dev
->kobj
, &uio_attr_grp
);
222 dev_err(idev
->dev
, "error creating sysfs files (%d)\n", ret
);
226 static void uio_dev_del_attributes(struct uio_device
*idev
)
230 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
231 mem
= &idev
->info
->mem
[mi
];
234 kobject_put(&mem
->map
->kobj
);
236 kobject_put(idev
->map_dir
);
237 sysfs_remove_group(&idev
->dev
->kobj
, &uio_attr_grp
);
240 static int uio_get_minor(struct uio_device
*idev
)
242 static DEFINE_MUTEX(minor_lock
);
243 int retval
= -ENOMEM
;
246 mutex_lock(&minor_lock
);
247 if (idr_pre_get(&uio_idr
, GFP_KERNEL
) == 0)
250 retval
= idr_get_new(&uio_idr
, idev
, &id
);
252 if (retval
== -EAGAIN
)
256 idev
->minor
= id
& MAX_ID_MASK
;
258 mutex_unlock(&minor_lock
);
262 static void uio_free_minor(struct uio_device
*idev
)
264 idr_remove(&uio_idr
, idev
->minor
);
268 * uio_event_notify - trigger an interrupt event
269 * @info: UIO device capabilities
271 void uio_event_notify(struct uio_info
*info
)
273 struct uio_device
*idev
= info
->uio_dev
;
275 atomic_inc(&idev
->event
);
276 wake_up_interruptible(&idev
->wait
);
277 kill_fasync(&idev
->async_queue
, SIGIO
, POLL_IN
);
279 EXPORT_SYMBOL_GPL(uio_event_notify
);
282 * uio_interrupt - hardware interrupt handler
283 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
284 * @dev_id: Pointer to the devices uio_device structure
286 static irqreturn_t
uio_interrupt(int irq
, void *dev_id
)
288 struct uio_device
*idev
= (struct uio_device
*)dev_id
;
289 irqreturn_t ret
= idev
->info
->handler(irq
, idev
->info
);
291 if (ret
== IRQ_HANDLED
)
292 uio_event_notify(idev
->info
);
297 struct uio_listener
{
298 struct uio_device
*dev
;
302 static int uio_open(struct inode
*inode
, struct file
*filep
)
304 struct uio_device
*idev
;
305 struct uio_listener
*listener
;
309 idev
= idr_find(&uio_idr
, iminor(inode
));
315 if (!try_module_get(idev
->owner
)) {
320 listener
= kmalloc(sizeof(*listener
), GFP_KERNEL
);
323 goto err_alloc_listener
;
326 listener
->dev
= idev
;
327 listener
->event_count
= atomic_read(&idev
->event
);
328 filep
->private_data
= listener
;
330 if (idev
->info
->open
) {
331 ret
= idev
->info
->open(idev
->info
, inode
);
343 module_put(idev
->owner
);
350 static int uio_fasync(int fd
, struct file
*filep
, int on
)
352 struct uio_listener
*listener
= filep
->private_data
;
353 struct uio_device
*idev
= listener
->dev
;
355 return fasync_helper(fd
, filep
, on
, &idev
->async_queue
);
358 static int uio_release(struct inode
*inode
, struct file
*filep
)
361 struct uio_listener
*listener
= filep
->private_data
;
362 struct uio_device
*idev
= listener
->dev
;
364 if (idev
->info
->release
)
365 ret
= idev
->info
->release(idev
->info
, inode
);
367 module_put(idev
->owner
);
369 if (filep
->f_flags
& FASYNC
)
370 ret
= uio_fasync(-1, filep
, 0);
375 static unsigned int uio_poll(struct file
*filep
, poll_table
*wait
)
377 struct uio_listener
*listener
= filep
->private_data
;
378 struct uio_device
*idev
= listener
->dev
;
380 if (idev
->info
->irq
== UIO_IRQ_NONE
)
383 poll_wait(filep
, &idev
->wait
, wait
);
384 if (listener
->event_count
!= atomic_read(&idev
->event
))
385 return POLLIN
| POLLRDNORM
;
389 static ssize_t
uio_read(struct file
*filep
, char __user
*buf
,
390 size_t count
, loff_t
*ppos
)
392 struct uio_listener
*listener
= filep
->private_data
;
393 struct uio_device
*idev
= listener
->dev
;
394 DECLARE_WAITQUEUE(wait
, current
);
398 if (idev
->info
->irq
== UIO_IRQ_NONE
)
401 if (count
!= sizeof(s32
))
404 add_wait_queue(&idev
->wait
, &wait
);
407 set_current_state(TASK_INTERRUPTIBLE
);
409 event_count
= atomic_read(&idev
->event
);
410 if (event_count
!= listener
->event_count
) {
411 if (copy_to_user(buf
, &event_count
, count
))
414 listener
->event_count
= event_count
;
420 if (filep
->f_flags
& O_NONBLOCK
) {
425 if (signal_pending(current
)) {
426 retval
= -ERESTARTSYS
;
432 __set_current_state(TASK_RUNNING
);
433 remove_wait_queue(&idev
->wait
, &wait
);
438 static ssize_t
uio_write(struct file
*filep
, const char __user
*buf
,
439 size_t count
, loff_t
*ppos
)
441 struct uio_listener
*listener
= filep
->private_data
;
442 struct uio_device
*idev
= listener
->dev
;
446 if (idev
->info
->irq
== UIO_IRQ_NONE
)
449 if (count
!= sizeof(s32
))
452 if (!idev
->info
->irqcontrol
)
455 if (copy_from_user(&irq_on
, buf
, count
))
458 retval
= idev
->info
->irqcontrol(idev
->info
, irq_on
);
460 return retval
? retval
: sizeof(s32
);
463 static int uio_find_mem_index(struct vm_area_struct
*vma
)
466 struct uio_device
*idev
= vma
->vm_private_data
;
468 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
469 if (idev
->info
->mem
[mi
].size
== 0)
471 if (vma
->vm_pgoff
== mi
)
477 static void uio_vma_open(struct vm_area_struct
*vma
)
479 struct uio_device
*idev
= vma
->vm_private_data
;
483 static void uio_vma_close(struct vm_area_struct
*vma
)
485 struct uio_device
*idev
= vma
->vm_private_data
;
489 static int uio_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
491 struct uio_device
*idev
= vma
->vm_private_data
;
493 unsigned long offset
;
495 int mi
= uio_find_mem_index(vma
);
497 return VM_FAULT_SIGBUS
;
500 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
503 offset
= (vmf
->pgoff
- mi
) << PAGE_SHIFT
;
505 if (idev
->info
->mem
[mi
].memtype
== UIO_MEM_LOGICAL
)
506 page
= virt_to_page(idev
->info
->mem
[mi
].addr
+ offset
);
508 page
= vmalloc_to_page((void *)idev
->info
->mem
[mi
].addr
515 static struct vm_operations_struct uio_vm_ops
= {
516 .open
= uio_vma_open
,
517 .close
= uio_vma_close
,
518 .fault
= uio_vma_fault
,
521 static int uio_mmap_physical(struct vm_area_struct
*vma
)
523 struct uio_device
*idev
= vma
->vm_private_data
;
524 int mi
= uio_find_mem_index(vma
);
528 vma
->vm_flags
|= VM_IO
| VM_RESERVED
;
530 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
532 return remap_pfn_range(vma
,
534 idev
->info
->mem
[mi
].addr
>> PAGE_SHIFT
,
535 vma
->vm_end
- vma
->vm_start
,
539 static int uio_mmap_logical(struct vm_area_struct
*vma
)
541 vma
->vm_flags
|= VM_RESERVED
;
542 vma
->vm_ops
= &uio_vm_ops
;
547 static int uio_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
549 struct uio_listener
*listener
= filep
->private_data
;
550 struct uio_device
*idev
= listener
->dev
;
552 unsigned long requested_pages
, actual_pages
;
555 if (vma
->vm_end
< vma
->vm_start
)
558 vma
->vm_private_data
= idev
;
560 mi
= uio_find_mem_index(vma
);
564 requested_pages
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
565 actual_pages
= (idev
->info
->mem
[mi
].size
+ PAGE_SIZE
-1) >> PAGE_SHIFT
;
566 if (requested_pages
> actual_pages
)
569 if (idev
->info
->mmap
) {
570 ret
= idev
->info
->mmap(idev
->info
, vma
);
574 switch (idev
->info
->mem
[mi
].memtype
) {
576 return uio_mmap_physical(vma
);
577 case UIO_MEM_LOGICAL
:
578 case UIO_MEM_VIRTUAL
:
579 return uio_mmap_logical(vma
);
585 static const struct file_operations uio_fops
= {
586 .owner
= THIS_MODULE
,
588 .release
= uio_release
,
593 .fasync
= uio_fasync
,
596 static int uio_major_init(void)
598 uio_major
= register_chrdev(0, "uio", &uio_fops
);
604 static void uio_major_cleanup(void)
606 unregister_chrdev(uio_major
, "uio");
609 static int init_uio_class(void)
613 if (uio_class
!= NULL
) {
614 kref_get(&uio_class
->kref
);
618 /* This is the first time in here, set everything up properly */
619 ret
= uio_major_init();
623 uio_class
= kzalloc(sizeof(*uio_class
), GFP_KERNEL
);
629 kref_init(&uio_class
->kref
);
630 uio_class
->class = class_create(THIS_MODULE
, "uio");
631 if (IS_ERR(uio_class
->class)) {
632 ret
= IS_ERR(uio_class
->class);
633 printk(KERN_ERR
"class_create failed for uio\n");
634 goto err_class_create
;
647 static void release_uio_class(struct kref
*kref
)
649 /* Ok, we cheat as we know we only have one uio_class */
650 class_destroy(uio_class
->class);
656 static void uio_class_destroy(void)
659 kref_put(&uio_class
->kref
, release_uio_class
);
663 * uio_register_device - register a new userspace IO device
664 * @owner: module that creates the new device
665 * @parent: parent device
666 * @info: UIO device capabilities
668 * returns zero on success or a negative error code.
670 int __uio_register_device(struct module
*owner
,
671 struct device
*parent
,
672 struct uio_info
*info
)
674 struct uio_device
*idev
;
677 if (!parent
|| !info
|| !info
->name
|| !info
->version
)
680 info
->uio_dev
= NULL
;
682 ret
= init_uio_class();
686 idev
= kzalloc(sizeof(*idev
), GFP_KERNEL
);
694 init_waitqueue_head(&idev
->wait
);
695 atomic_set(&idev
->event
, 0);
697 ret
= uio_get_minor(idev
);
701 idev
->dev
= device_create(uio_class
->class, parent
,
702 MKDEV(uio_major
, idev
->minor
), idev
,
703 "uio%d", idev
->minor
);
704 if (IS_ERR(idev
->dev
)) {
705 printk(KERN_ERR
"UIO: device register failed\n");
706 ret
= PTR_ERR(idev
->dev
);
707 goto err_device_create
;
710 ret
= uio_dev_add_attributes(idev
);
712 goto err_uio_dev_add_attributes
;
714 info
->uio_dev
= idev
;
716 if (idev
->info
->irq
>= 0) {
717 ret
= request_irq(idev
->info
->irq
, uio_interrupt
,
718 idev
->info
->irq_flags
, idev
->info
->name
, idev
);
720 goto err_request_irq
;
726 uio_dev_del_attributes(idev
);
727 err_uio_dev_add_attributes
:
728 device_destroy(uio_class
->class, MKDEV(uio_major
, idev
->minor
));
730 uio_free_minor(idev
);
737 EXPORT_SYMBOL_GPL(__uio_register_device
);
740 * uio_unregister_device - unregister a industrial IO device
741 * @info: UIO device capabilities
744 void uio_unregister_device(struct uio_info
*info
)
746 struct uio_device
*idev
;
748 if (!info
|| !info
->uio_dev
)
751 idev
= info
->uio_dev
;
753 uio_free_minor(idev
);
756 free_irq(info
->irq
, idev
);
758 uio_dev_del_attributes(idev
);
760 dev_set_drvdata(idev
->dev
, NULL
);
761 device_destroy(uio_class
->class, MKDEV(uio_major
, idev
->minor
));
767 EXPORT_SYMBOL_GPL(uio_unregister_device
);
769 static int __init
uio_init(void)
774 static void __exit
uio_exit(void)
778 module_init(uio_init
)
779 module_exit(uio_exit
)
780 MODULE_LICENSE("GPL v2");