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 struct uio_sysfs_entry
{
71 struct attribute attr
;
72 ssize_t (*show
)(struct uio_mem
*, char *);
73 ssize_t (*store
)(struct uio_mem
*, const char *, size_t);
76 static struct uio_sysfs_entry addr_attribute
=
77 __ATTR(addr
, S_IRUGO
, map_addr_show
, NULL
);
78 static struct uio_sysfs_entry size_attribute
=
79 __ATTR(size
, S_IRUGO
, map_size_show
, NULL
);
81 static struct attribute
*attrs
[] = {
84 NULL
, /* need to NULL terminate the list of attributes */
87 static void map_release(struct kobject
*kobj
)
89 struct uio_map
*map
= to_map(kobj
);
93 static ssize_t
map_type_show(struct kobject
*kobj
, struct attribute
*attr
,
96 struct uio_map
*map
= to_map(kobj
);
97 struct uio_mem
*mem
= map
->mem
;
98 struct uio_sysfs_entry
*entry
;
100 entry
= container_of(attr
, struct uio_sysfs_entry
, attr
);
105 return entry
->show(mem
, buf
);
108 static struct sysfs_ops uio_sysfs_ops
= {
109 .show
= map_type_show
,
112 static struct kobj_type map_attr_type
= {
113 .release
= map_release
,
114 .sysfs_ops
= &uio_sysfs_ops
,
115 .default_attrs
= attrs
,
118 static ssize_t
show_name(struct device
*dev
,
119 struct device_attribute
*attr
, char *buf
)
121 struct uio_device
*idev
= dev_get_drvdata(dev
);
123 return sprintf(buf
, "%s\n", idev
->info
->name
);
127 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
129 static ssize_t
show_version(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
->version
);
138 static DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
140 static ssize_t
show_event(struct device
*dev
,
141 struct device_attribute
*attr
, char *buf
)
143 struct uio_device
*idev
= dev_get_drvdata(dev
);
145 return sprintf(buf
, "%u\n",
146 (unsigned int)atomic_read(&idev
->event
));
150 static DEVICE_ATTR(event
, S_IRUGO
, show_event
, NULL
);
152 static struct attribute
*uio_attrs
[] = {
154 &dev_attr_version
.attr
,
155 &dev_attr_event
.attr
,
159 static struct attribute_group uio_attr_grp
= {
166 static int uio_dev_add_attributes(struct uio_device
*idev
)
174 ret
= sysfs_create_group(&idev
->dev
->kobj
, &uio_attr_grp
);
178 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
179 mem
= &idev
->info
->mem
[mi
];
184 idev
->map_dir
= kobject_create_and_add("maps",
189 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
192 kobject_init(&map
->kobj
, &map_attr_type
);
195 ret
= kobject_add(&map
->kobj
, idev
->map_dir
, "map%d", mi
);
198 ret
= kobject_uevent(&map
->kobj
, KOBJ_ADD
);
206 for (mi
--; mi
>=0; mi
--) {
207 mem
= &idev
->info
->mem
[mi
];
209 kobject_put(&map
->kobj
);
211 kobject_put(idev
->map_dir
);
212 sysfs_remove_group(&idev
->dev
->kobj
, &uio_attr_grp
);
214 dev_err(idev
->dev
, "error creating sysfs files (%d)\n", ret
);
218 static void uio_dev_del_attributes(struct uio_device
*idev
)
222 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
223 mem
= &idev
->info
->mem
[mi
];
226 kobject_put(&mem
->map
->kobj
);
228 kobject_put(idev
->map_dir
);
229 sysfs_remove_group(&idev
->dev
->kobj
, &uio_attr_grp
);
232 static int uio_get_minor(struct uio_device
*idev
)
234 static DEFINE_MUTEX(minor_lock
);
235 int retval
= -ENOMEM
;
238 mutex_lock(&minor_lock
);
239 if (idr_pre_get(&uio_idr
, GFP_KERNEL
) == 0)
242 retval
= idr_get_new(&uio_idr
, idev
, &id
);
244 if (retval
== -EAGAIN
)
248 idev
->minor
= id
& MAX_ID_MASK
;
250 mutex_unlock(&minor_lock
);
254 static void uio_free_minor(struct uio_device
*idev
)
256 idr_remove(&uio_idr
, idev
->minor
);
260 * uio_event_notify - trigger an interrupt event
261 * @info: UIO device capabilities
263 void uio_event_notify(struct uio_info
*info
)
265 struct uio_device
*idev
= info
->uio_dev
;
267 atomic_inc(&idev
->event
);
268 wake_up_interruptible(&idev
->wait
);
269 kill_fasync(&idev
->async_queue
, SIGIO
, POLL_IN
);
271 EXPORT_SYMBOL_GPL(uio_event_notify
);
274 * uio_interrupt - hardware interrupt handler
275 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
276 * @dev_id: Pointer to the devices uio_device structure
278 static irqreturn_t
uio_interrupt(int irq
, void *dev_id
)
280 struct uio_device
*idev
= (struct uio_device
*)dev_id
;
281 irqreturn_t ret
= idev
->info
->handler(irq
, idev
->info
);
283 if (ret
== IRQ_HANDLED
)
284 uio_event_notify(idev
->info
);
289 struct uio_listener
{
290 struct uio_device
*dev
;
294 static int uio_open(struct inode
*inode
, struct file
*filep
)
296 struct uio_device
*idev
;
297 struct uio_listener
*listener
;
300 idev
= idr_find(&uio_idr
, iminor(inode
));
304 if (!try_module_get(idev
->owner
))
307 listener
= kmalloc(sizeof(*listener
), GFP_KERNEL
);
310 goto err_alloc_listener
;
313 listener
->dev
= idev
;
314 listener
->event_count
= atomic_read(&idev
->event
);
315 filep
->private_data
= listener
;
317 if (idev
->info
->open
) {
318 ret
= idev
->info
->open(idev
->info
, inode
);
330 module_put(idev
->owner
);
335 static int uio_fasync(int fd
, struct file
*filep
, int on
)
337 struct uio_listener
*listener
= filep
->private_data
;
338 struct uio_device
*idev
= listener
->dev
;
340 return fasync_helper(fd
, filep
, on
, &idev
->async_queue
);
343 static int uio_release(struct inode
*inode
, struct file
*filep
)
346 struct uio_listener
*listener
= filep
->private_data
;
347 struct uio_device
*idev
= listener
->dev
;
349 if (idev
->info
->release
)
350 ret
= idev
->info
->release(idev
->info
, inode
);
352 module_put(idev
->owner
);
354 if (filep
->f_flags
& FASYNC
)
355 ret
= uio_fasync(-1, filep
, 0);
360 static unsigned int uio_poll(struct file
*filep
, poll_table
*wait
)
362 struct uio_listener
*listener
= filep
->private_data
;
363 struct uio_device
*idev
= listener
->dev
;
365 if (idev
->info
->irq
== UIO_IRQ_NONE
)
368 poll_wait(filep
, &idev
->wait
, wait
);
369 if (listener
->event_count
!= atomic_read(&idev
->event
))
370 return POLLIN
| POLLRDNORM
;
374 static ssize_t
uio_read(struct file
*filep
, char __user
*buf
,
375 size_t count
, loff_t
*ppos
)
377 struct uio_listener
*listener
= filep
->private_data
;
378 struct uio_device
*idev
= listener
->dev
;
379 DECLARE_WAITQUEUE(wait
, current
);
383 if (idev
->info
->irq
== UIO_IRQ_NONE
)
386 if (count
!= sizeof(s32
))
389 add_wait_queue(&idev
->wait
, &wait
);
392 set_current_state(TASK_INTERRUPTIBLE
);
394 event_count
= atomic_read(&idev
->event
);
395 if (event_count
!= listener
->event_count
) {
396 if (copy_to_user(buf
, &event_count
, count
))
399 listener
->event_count
= event_count
;
405 if (filep
->f_flags
& O_NONBLOCK
) {
410 if (signal_pending(current
)) {
411 retval
= -ERESTARTSYS
;
417 __set_current_state(TASK_RUNNING
);
418 remove_wait_queue(&idev
->wait
, &wait
);
423 static int uio_find_mem_index(struct vm_area_struct
*vma
)
426 struct uio_device
*idev
= vma
->vm_private_data
;
428 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
429 if (idev
->info
->mem
[mi
].size
== 0)
431 if (vma
->vm_pgoff
== mi
)
437 static void uio_vma_open(struct vm_area_struct
*vma
)
439 struct uio_device
*idev
= vma
->vm_private_data
;
443 static void uio_vma_close(struct vm_area_struct
*vma
)
445 struct uio_device
*idev
= vma
->vm_private_data
;
449 static int uio_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
451 struct uio_device
*idev
= vma
->vm_private_data
;
454 int mi
= uio_find_mem_index(vma
);
456 return VM_FAULT_SIGBUS
;
458 if (idev
->info
->mem
[mi
].memtype
== UIO_MEM_LOGICAL
)
459 page
= virt_to_page(idev
->info
->mem
[mi
].addr
);
461 page
= vmalloc_to_page((void*)idev
->info
->mem
[mi
].addr
);
467 static struct vm_operations_struct uio_vm_ops
= {
468 .open
= uio_vma_open
,
469 .close
= uio_vma_close
,
470 .fault
= uio_vma_fault
,
473 static int uio_mmap_physical(struct vm_area_struct
*vma
)
475 struct uio_device
*idev
= vma
->vm_private_data
;
476 int mi
= uio_find_mem_index(vma
);
480 vma
->vm_flags
|= VM_IO
| VM_RESERVED
;
482 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
484 return remap_pfn_range(vma
,
486 idev
->info
->mem
[mi
].addr
>> PAGE_SHIFT
,
487 vma
->vm_end
- vma
->vm_start
,
491 static int uio_mmap_logical(struct vm_area_struct
*vma
)
493 vma
->vm_flags
|= VM_RESERVED
;
494 vma
->vm_ops
= &uio_vm_ops
;
499 static int uio_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
501 struct uio_listener
*listener
= filep
->private_data
;
502 struct uio_device
*idev
= listener
->dev
;
504 unsigned long requested_pages
, actual_pages
;
507 if (vma
->vm_end
< vma
->vm_start
)
510 vma
->vm_private_data
= idev
;
512 mi
= uio_find_mem_index(vma
);
516 requested_pages
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
517 actual_pages
= (idev
->info
->mem
[mi
].size
+ PAGE_SIZE
-1) >> PAGE_SHIFT
;
518 if (requested_pages
> actual_pages
)
521 if (idev
->info
->mmap
) {
522 ret
= idev
->info
->mmap(idev
->info
, vma
);
526 switch (idev
->info
->mem
[mi
].memtype
) {
528 return uio_mmap_physical(vma
);
529 case UIO_MEM_LOGICAL
:
530 case UIO_MEM_VIRTUAL
:
531 return uio_mmap_logical(vma
);
537 static const struct file_operations uio_fops
= {
538 .owner
= THIS_MODULE
,
540 .release
= uio_release
,
544 .fasync
= uio_fasync
,
547 static int uio_major_init(void)
549 uio_major
= register_chrdev(0, "uio", &uio_fops
);
555 static void uio_major_cleanup(void)
557 unregister_chrdev(uio_major
, "uio");
560 static int init_uio_class(void)
564 if (uio_class
!= NULL
) {
565 kref_get(&uio_class
->kref
);
569 /* This is the first time in here, set everything up properly */
570 ret
= uio_major_init();
574 uio_class
= kzalloc(sizeof(*uio_class
), GFP_KERNEL
);
580 kref_init(&uio_class
->kref
);
581 uio_class
->class = class_create(THIS_MODULE
, "uio");
582 if (IS_ERR(uio_class
->class)) {
583 ret
= IS_ERR(uio_class
->class);
584 printk(KERN_ERR
"class_create failed for uio\n");
585 goto err_class_create
;
598 static void release_uio_class(struct kref
*kref
)
600 /* Ok, we cheat as we know we only have one uio_class */
601 class_destroy(uio_class
->class);
607 static void uio_class_destroy(void)
610 kref_put(&uio_class
->kref
, release_uio_class
);
614 * uio_register_device - register a new userspace IO device
615 * @owner: module that creates the new device
616 * @parent: parent device
617 * @info: UIO device capabilities
619 * returns zero on success or a negative error code.
621 int __uio_register_device(struct module
*owner
,
622 struct device
*parent
,
623 struct uio_info
*info
)
625 struct uio_device
*idev
;
628 if (!parent
|| !info
|| !info
->name
|| !info
->version
)
631 info
->uio_dev
= NULL
;
633 ret
= init_uio_class();
637 idev
= kzalloc(sizeof(*idev
), GFP_KERNEL
);
645 init_waitqueue_head(&idev
->wait
);
646 atomic_set(&idev
->event
, 0);
648 ret
= uio_get_minor(idev
);
652 idev
->dev
= device_create(uio_class
->class, parent
,
653 MKDEV(uio_major
, idev
->minor
),
654 "uio%d", idev
->minor
);
655 if (IS_ERR(idev
->dev
)) {
656 printk(KERN_ERR
"UIO: device register failed\n");
657 ret
= PTR_ERR(idev
->dev
);
658 goto err_device_create
;
660 dev_set_drvdata(idev
->dev
, idev
);
662 ret
= uio_dev_add_attributes(idev
);
664 goto err_uio_dev_add_attributes
;
666 info
->uio_dev
= idev
;
668 if (idev
->info
->irq
>= 0) {
669 ret
= request_irq(idev
->info
->irq
, uio_interrupt
,
670 idev
->info
->irq_flags
, idev
->info
->name
, idev
);
672 goto err_request_irq
;
678 uio_dev_del_attributes(idev
);
679 err_uio_dev_add_attributes
:
680 device_destroy(uio_class
->class, MKDEV(uio_major
, idev
->minor
));
682 uio_free_minor(idev
);
689 EXPORT_SYMBOL_GPL(__uio_register_device
);
692 * uio_unregister_device - unregister a industrial IO device
693 * @info: UIO device capabilities
696 void uio_unregister_device(struct uio_info
*info
)
698 struct uio_device
*idev
;
700 if (!info
|| !info
->uio_dev
)
703 idev
= info
->uio_dev
;
705 uio_free_minor(idev
);
708 free_irq(info
->irq
, idev
);
710 uio_dev_del_attributes(idev
);
712 dev_set_drvdata(idev
->dev
, NULL
);
713 device_destroy(uio_class
->class, MKDEV(uio_major
, idev
->minor
));
719 EXPORT_SYMBOL_GPL(uio_unregister_device
);
721 static int __init
uio_init(void)
726 static void __exit
uio_exit(void)
730 module_init(uio_init
)
731 module_exit(uio_exit
)
732 MODULE_LICENSE("GPL v2");