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
{
50 /* Protect idr accesses */
51 static DEFINE_MUTEX(minor_lock
);
61 #define to_map(map) container_of(map, struct uio_map, kobj)
63 static ssize_t
map_addr_show(struct uio_mem
*mem
, char *buf
)
65 return sprintf(buf
, "0x%lx\n", mem
->addr
);
68 static ssize_t
map_size_show(struct uio_mem
*mem
, char *buf
)
70 return sprintf(buf
, "0x%lx\n", mem
->size
);
73 static ssize_t
map_offset_show(struct uio_mem
*mem
, char *buf
)
75 return sprintf(buf
, "0x%lx\n", mem
->addr
& ~PAGE_MASK
);
78 struct uio_sysfs_entry
{
79 struct attribute attr
;
80 ssize_t (*show
)(struct uio_mem
*, char *);
81 ssize_t (*store
)(struct uio_mem
*, const char *, size_t);
84 static struct uio_sysfs_entry addr_attribute
=
85 __ATTR(addr
, S_IRUGO
, map_addr_show
, NULL
);
86 static struct uio_sysfs_entry size_attribute
=
87 __ATTR(size
, S_IRUGO
, map_size_show
, NULL
);
88 static struct uio_sysfs_entry offset_attribute
=
89 __ATTR(offset
, S_IRUGO
, map_offset_show
, NULL
);
91 static struct attribute
*attrs
[] = {
94 &offset_attribute
.attr
,
95 NULL
, /* need to NULL terminate the list of attributes */
98 static void map_release(struct kobject
*kobj
)
100 struct uio_map
*map
= to_map(kobj
);
104 static ssize_t
map_type_show(struct kobject
*kobj
, struct attribute
*attr
,
107 struct uio_map
*map
= to_map(kobj
);
108 struct uio_mem
*mem
= map
->mem
;
109 struct uio_sysfs_entry
*entry
;
111 entry
= container_of(attr
, struct uio_sysfs_entry
, attr
);
116 return entry
->show(mem
, buf
);
119 static struct sysfs_ops uio_sysfs_ops
= {
120 .show
= map_type_show
,
123 static struct kobj_type map_attr_type
= {
124 .release
= map_release
,
125 .sysfs_ops
= &uio_sysfs_ops
,
126 .default_attrs
= attrs
,
129 static ssize_t
show_name(struct device
*dev
,
130 struct device_attribute
*attr
, char *buf
)
132 struct uio_device
*idev
= dev_get_drvdata(dev
);
134 return sprintf(buf
, "%s\n", idev
->info
->name
);
138 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
140 static ssize_t
show_version(struct device
*dev
,
141 struct device_attribute
*attr
, char *buf
)
143 struct uio_device
*idev
= dev_get_drvdata(dev
);
145 return sprintf(buf
, "%s\n", idev
->info
->version
);
149 static DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
151 static ssize_t
show_event(struct device
*dev
,
152 struct device_attribute
*attr
, char *buf
)
154 struct uio_device
*idev
= dev_get_drvdata(dev
);
156 return sprintf(buf
, "%u\n",
157 (unsigned int)atomic_read(&idev
->event
));
161 static DEVICE_ATTR(event
, S_IRUGO
, show_event
, NULL
);
163 static struct attribute
*uio_attrs
[] = {
165 &dev_attr_version
.attr
,
166 &dev_attr_event
.attr
,
170 static struct attribute_group uio_attr_grp
= {
177 static int uio_dev_add_attributes(struct uio_device
*idev
)
185 ret
= sysfs_create_group(&idev
->dev
->kobj
, &uio_attr_grp
);
189 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
190 mem
= &idev
->info
->mem
[mi
];
195 idev
->map_dir
= kobject_create_and_add("maps",
200 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
203 kobject_init(&map
->kobj
, &map_attr_type
);
206 ret
= kobject_add(&map
->kobj
, idev
->map_dir
, "map%d", mi
);
209 ret
= kobject_uevent(&map
->kobj
, KOBJ_ADD
);
217 for (mi
--; mi
>=0; mi
--) {
218 mem
= &idev
->info
->mem
[mi
];
220 kobject_put(&map
->kobj
);
222 kobject_put(idev
->map_dir
);
223 sysfs_remove_group(&idev
->dev
->kobj
, &uio_attr_grp
);
225 dev_err(idev
->dev
, "error creating sysfs files (%d)\n", ret
);
229 static void uio_dev_del_attributes(struct uio_device
*idev
)
233 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
234 mem
= &idev
->info
->mem
[mi
];
237 kobject_put(&mem
->map
->kobj
);
239 kobject_put(idev
->map_dir
);
240 sysfs_remove_group(&idev
->dev
->kobj
, &uio_attr_grp
);
243 static int uio_get_minor(struct uio_device
*idev
)
245 int retval
= -ENOMEM
;
248 mutex_lock(&minor_lock
);
249 if (idr_pre_get(&uio_idr
, GFP_KERNEL
) == 0)
252 retval
= idr_get_new(&uio_idr
, idev
, &id
);
254 if (retval
== -EAGAIN
)
258 idev
->minor
= id
& MAX_ID_MASK
;
260 mutex_unlock(&minor_lock
);
264 static void uio_free_minor(struct uio_device
*idev
)
266 mutex_lock(&minor_lock
);
267 idr_remove(&uio_idr
, idev
->minor
);
268 mutex_unlock(&minor_lock
);
272 * uio_event_notify - trigger an interrupt event
273 * @info: UIO device capabilities
275 void uio_event_notify(struct uio_info
*info
)
277 struct uio_device
*idev
= info
->uio_dev
;
279 atomic_inc(&idev
->event
);
280 wake_up_interruptible(&idev
->wait
);
281 kill_fasync(&idev
->async_queue
, SIGIO
, POLL_IN
);
283 EXPORT_SYMBOL_GPL(uio_event_notify
);
286 * uio_interrupt - hardware interrupt handler
287 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
288 * @dev_id: Pointer to the devices uio_device structure
290 static irqreturn_t
uio_interrupt(int irq
, void *dev_id
)
292 struct uio_device
*idev
= (struct uio_device
*)dev_id
;
293 irqreturn_t ret
= idev
->info
->handler(irq
, idev
->info
);
295 if (ret
== IRQ_HANDLED
)
296 uio_event_notify(idev
->info
);
301 struct uio_listener
{
302 struct uio_device
*dev
;
306 static int uio_open(struct inode
*inode
, struct file
*filep
)
308 struct uio_device
*idev
;
309 struct uio_listener
*listener
;
312 mutex_lock(&minor_lock
);
313 idev
= idr_find(&uio_idr
, iminor(inode
));
314 mutex_unlock(&minor_lock
);
320 if (!try_module_get(idev
->owner
)) {
325 listener
= kmalloc(sizeof(*listener
), GFP_KERNEL
);
328 goto err_alloc_listener
;
331 listener
->dev
= idev
;
332 listener
->event_count
= atomic_read(&idev
->event
);
333 filep
->private_data
= listener
;
335 if (idev
->info
->open
) {
336 ret
= idev
->info
->open(idev
->info
, inode
);
346 module_put(idev
->owner
);
352 static int uio_fasync(int fd
, struct file
*filep
, int on
)
354 struct uio_listener
*listener
= filep
->private_data
;
355 struct uio_device
*idev
= listener
->dev
;
357 return fasync_helper(fd
, filep
, on
, &idev
->async_queue
);
360 static int uio_release(struct inode
*inode
, struct file
*filep
)
363 struct uio_listener
*listener
= filep
->private_data
;
364 struct uio_device
*idev
= listener
->dev
;
366 if (idev
->info
->release
)
367 ret
= idev
->info
->release(idev
->info
, inode
);
369 module_put(idev
->owner
);
374 static unsigned int uio_poll(struct file
*filep
, poll_table
*wait
)
376 struct uio_listener
*listener
= filep
->private_data
;
377 struct uio_device
*idev
= listener
->dev
;
379 if (idev
->info
->irq
== UIO_IRQ_NONE
)
382 poll_wait(filep
, &idev
->wait
, wait
);
383 if (listener
->event_count
!= atomic_read(&idev
->event
))
384 return POLLIN
| POLLRDNORM
;
388 static ssize_t
uio_read(struct file
*filep
, char __user
*buf
,
389 size_t count
, loff_t
*ppos
)
391 struct uio_listener
*listener
= filep
->private_data
;
392 struct uio_device
*idev
= listener
->dev
;
393 DECLARE_WAITQUEUE(wait
, current
);
397 if (idev
->info
->irq
== UIO_IRQ_NONE
)
400 if (count
!= sizeof(s32
))
403 add_wait_queue(&idev
->wait
, &wait
);
406 set_current_state(TASK_INTERRUPTIBLE
);
408 event_count
= atomic_read(&idev
->event
);
409 if (event_count
!= listener
->event_count
) {
410 if (copy_to_user(buf
, &event_count
, count
))
413 listener
->event_count
= event_count
;
419 if (filep
->f_flags
& O_NONBLOCK
) {
424 if (signal_pending(current
)) {
425 retval
= -ERESTARTSYS
;
431 __set_current_state(TASK_RUNNING
);
432 remove_wait_queue(&idev
->wait
, &wait
);
437 static ssize_t
uio_write(struct file
*filep
, const char __user
*buf
,
438 size_t count
, loff_t
*ppos
)
440 struct uio_listener
*listener
= filep
->private_data
;
441 struct uio_device
*idev
= listener
->dev
;
445 if (idev
->info
->irq
== UIO_IRQ_NONE
)
448 if (count
!= sizeof(s32
))
451 if (!idev
->info
->irqcontrol
)
454 if (copy_from_user(&irq_on
, buf
, count
))
457 retval
= idev
->info
->irqcontrol(idev
->info
, irq_on
);
459 return retval
? retval
: sizeof(s32
);
462 static int uio_find_mem_index(struct vm_area_struct
*vma
)
465 struct uio_device
*idev
= vma
->vm_private_data
;
467 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
468 if (idev
->info
->mem
[mi
].size
== 0)
470 if (vma
->vm_pgoff
== mi
)
476 static void uio_vma_open(struct vm_area_struct
*vma
)
478 struct uio_device
*idev
= vma
->vm_private_data
;
482 static void uio_vma_close(struct vm_area_struct
*vma
)
484 struct uio_device
*idev
= vma
->vm_private_data
;
488 static int uio_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
490 struct uio_device
*idev
= vma
->vm_private_data
;
492 unsigned long offset
;
494 int mi
= uio_find_mem_index(vma
);
496 return VM_FAULT_SIGBUS
;
499 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
502 offset
= (vmf
->pgoff
- mi
) << PAGE_SHIFT
;
504 if (idev
->info
->mem
[mi
].memtype
== UIO_MEM_LOGICAL
)
505 page
= virt_to_page(idev
->info
->mem
[mi
].addr
+ offset
);
507 page
= vmalloc_to_page((void *)idev
->info
->mem
[mi
].addr
514 static struct vm_operations_struct uio_vm_ops
= {
515 .open
= uio_vma_open
,
516 .close
= uio_vma_close
,
517 .fault
= uio_vma_fault
,
520 static int uio_mmap_physical(struct vm_area_struct
*vma
)
522 struct uio_device
*idev
= vma
->vm_private_data
;
523 int mi
= uio_find_mem_index(vma
);
527 vma
->vm_flags
|= VM_IO
| VM_RESERVED
;
529 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
531 return remap_pfn_range(vma
,
533 idev
->info
->mem
[mi
].addr
>> PAGE_SHIFT
,
534 vma
->vm_end
- vma
->vm_start
,
538 static int uio_mmap_logical(struct vm_area_struct
*vma
)
540 vma
->vm_flags
|= VM_RESERVED
;
541 vma
->vm_ops
= &uio_vm_ops
;
546 static int uio_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
548 struct uio_listener
*listener
= filep
->private_data
;
549 struct uio_device
*idev
= listener
->dev
;
551 unsigned long requested_pages
, actual_pages
;
554 if (vma
->vm_end
< vma
->vm_start
)
557 vma
->vm_private_data
= idev
;
559 mi
= uio_find_mem_index(vma
);
563 requested_pages
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
564 actual_pages
= (idev
->info
->mem
[mi
].size
+ PAGE_SIZE
-1) >> PAGE_SHIFT
;
565 if (requested_pages
> actual_pages
)
568 if (idev
->info
->mmap
) {
569 ret
= idev
->info
->mmap(idev
->info
, vma
);
573 switch (idev
->info
->mem
[mi
].memtype
) {
575 return uio_mmap_physical(vma
);
576 case UIO_MEM_LOGICAL
:
577 case UIO_MEM_VIRTUAL
:
578 return uio_mmap_logical(vma
);
584 static const struct file_operations uio_fops
= {
585 .owner
= THIS_MODULE
,
587 .release
= uio_release
,
592 .fasync
= uio_fasync
,
595 static int uio_major_init(void)
597 uio_major
= register_chrdev(0, "uio", &uio_fops
);
603 static void uio_major_cleanup(void)
605 unregister_chrdev(uio_major
, "uio");
608 static int init_uio_class(void)
612 if (uio_class
!= NULL
) {
613 kref_get(&uio_class
->kref
);
617 /* This is the first time in here, set everything up properly */
618 ret
= uio_major_init();
622 uio_class
= kzalloc(sizeof(*uio_class
), GFP_KERNEL
);
628 kref_init(&uio_class
->kref
);
629 uio_class
->class = class_create(THIS_MODULE
, "uio");
630 if (IS_ERR(uio_class
->class)) {
631 ret
= IS_ERR(uio_class
->class);
632 printk(KERN_ERR
"class_create failed for uio\n");
633 goto err_class_create
;
646 static void release_uio_class(struct kref
*kref
)
648 /* Ok, we cheat as we know we only have one uio_class */
649 class_destroy(uio_class
->class);
655 static void uio_class_destroy(void)
658 kref_put(&uio_class
->kref
, release_uio_class
);
662 * uio_register_device - register a new userspace IO device
663 * @owner: module that creates the new device
664 * @parent: parent device
665 * @info: UIO device capabilities
667 * returns zero on success or a negative error code.
669 int __uio_register_device(struct module
*owner
,
670 struct device
*parent
,
671 struct uio_info
*info
)
673 struct uio_device
*idev
;
676 if (!parent
|| !info
|| !info
->name
|| !info
->version
)
679 info
->uio_dev
= NULL
;
681 ret
= init_uio_class();
685 idev
= kzalloc(sizeof(*idev
), GFP_KERNEL
);
693 init_waitqueue_head(&idev
->wait
);
694 atomic_set(&idev
->event
, 0);
696 ret
= uio_get_minor(idev
);
700 idev
->dev
= device_create(uio_class
->class, parent
,
701 MKDEV(uio_major
, idev
->minor
), idev
,
702 "uio%d", idev
->minor
);
703 if (IS_ERR(idev
->dev
)) {
704 printk(KERN_ERR
"UIO: device register failed\n");
705 ret
= PTR_ERR(idev
->dev
);
706 goto err_device_create
;
709 ret
= uio_dev_add_attributes(idev
);
711 goto err_uio_dev_add_attributes
;
713 info
->uio_dev
= idev
;
715 if (idev
->info
->irq
>= 0) {
716 ret
= request_irq(idev
->info
->irq
, uio_interrupt
,
717 idev
->info
->irq_flags
, idev
->info
->name
, idev
);
719 goto err_request_irq
;
725 uio_dev_del_attributes(idev
);
726 err_uio_dev_add_attributes
:
727 device_destroy(uio_class
->class, MKDEV(uio_major
, idev
->minor
));
729 uio_free_minor(idev
);
736 EXPORT_SYMBOL_GPL(__uio_register_device
);
739 * uio_unregister_device - unregister a industrial IO device
740 * @info: UIO device capabilities
743 void uio_unregister_device(struct uio_info
*info
)
745 struct uio_device
*idev
;
747 if (!info
|| !info
->uio_dev
)
750 idev
= info
->uio_dev
;
752 uio_free_minor(idev
);
755 free_irq(info
->irq
, idev
);
757 uio_dev_del_attributes(idev
);
759 dev_set_drvdata(idev
->dev
, NULL
);
760 device_destroy(uio_class
->class, MKDEV(uio_major
, idev
->minor
));
766 EXPORT_SYMBOL_GPL(uio_unregister_device
);
768 static int __init
uio_init(void)
773 static void __exit
uio_exit(void)
777 module_init(uio_init
)
778 module_exit(uio_exit
)
779 MODULE_LICENSE("GPL v2");