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)
61 static ssize_t
map_attr_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
64 struct uio_map
*map
= to_map(kobj
);
65 struct uio_mem
*mem
= map
->mem
;
67 if (strncmp(attr
->attr
.name
, "addr", 4) == 0)
68 return sprintf(buf
, "0x%lx\n", mem
->addr
);
70 if (strncmp(attr
->attr
.name
, "size", 4) == 0)
71 return sprintf(buf
, "0x%lx\n", mem
->size
);
76 static struct kobj_attribute attr_attribute
=
77 __ATTR(addr
, S_IRUGO
, map_attr_show
, NULL
);
78 static struct kobj_attribute size_attribute
=
79 __ATTR(size
, S_IRUGO
, map_attr_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 struct kobj_type map_attr_type
= {
94 .release
= map_release
,
95 .default_attrs
= attrs
,
98 static ssize_t
show_name(struct device
*dev
,
99 struct device_attribute
*attr
, char *buf
)
101 struct uio_device
*idev
= dev_get_drvdata(dev
);
103 return sprintf(buf
, "%s\n", idev
->info
->name
);
107 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
109 static ssize_t
show_version(struct device
*dev
,
110 struct device_attribute
*attr
, char *buf
)
112 struct uio_device
*idev
= dev_get_drvdata(dev
);
114 return sprintf(buf
, "%s\n", idev
->info
->version
);
118 static DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
120 static ssize_t
show_event(struct device
*dev
,
121 struct device_attribute
*attr
, char *buf
)
123 struct uio_device
*idev
= dev_get_drvdata(dev
);
125 return sprintf(buf
, "%u\n",
126 (unsigned int)atomic_read(&idev
->event
));
130 static DEVICE_ATTR(event
, S_IRUGO
, show_event
, NULL
);
132 static struct attribute
*uio_attrs
[] = {
134 &dev_attr_version
.attr
,
135 &dev_attr_event
.attr
,
139 static struct attribute_group uio_attr_grp
= {
146 static int uio_dev_add_attributes(struct uio_device
*idev
)
154 ret
= sysfs_create_group(&idev
->dev
->kobj
, &uio_attr_grp
);
158 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
159 mem
= &idev
->info
->mem
[mi
];
164 idev
->map_dir
= kobject_create_and_add("maps",
169 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
172 kobject_init(&map
->kobj
, &map_attr_type
);
175 ret
= kobject_add(&map
->kobj
, idev
->map_dir
, "map%d", mi
);
178 ret
= kobject_uevent(&map
->kobj
, KOBJ_ADD
);
186 for (mi
--; mi
>=0; mi
--) {
187 mem
= &idev
->info
->mem
[mi
];
189 kobject_put(&map
->kobj
);
191 kobject_put(idev
->map_dir
);
192 sysfs_remove_group(&idev
->dev
->kobj
, &uio_attr_grp
);
194 dev_err(idev
->dev
, "error creating sysfs files (%d)\n", ret
);
198 static void uio_dev_del_attributes(struct uio_device
*idev
)
202 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
203 mem
= &idev
->info
->mem
[mi
];
206 kobject_put(&mem
->map
->kobj
);
208 kobject_put(idev
->map_dir
);
209 sysfs_remove_group(&idev
->dev
->kobj
, &uio_attr_grp
);
212 static int uio_get_minor(struct uio_device
*idev
)
214 static DEFINE_MUTEX(minor_lock
);
215 int retval
= -ENOMEM
;
218 mutex_lock(&minor_lock
);
219 if (idr_pre_get(&uio_idr
, GFP_KERNEL
) == 0)
222 retval
= idr_get_new(&uio_idr
, idev
, &id
);
224 if (retval
== -EAGAIN
)
228 idev
->minor
= id
& MAX_ID_MASK
;
230 mutex_unlock(&minor_lock
);
234 static void uio_free_minor(struct uio_device
*idev
)
236 idr_remove(&uio_idr
, idev
->minor
);
240 * uio_event_notify - trigger an interrupt event
241 * @info: UIO device capabilities
243 void uio_event_notify(struct uio_info
*info
)
245 struct uio_device
*idev
= info
->uio_dev
;
247 atomic_inc(&idev
->event
);
248 wake_up_interruptible(&idev
->wait
);
249 kill_fasync(&idev
->async_queue
, SIGIO
, POLL_IN
);
251 EXPORT_SYMBOL_GPL(uio_event_notify
);
254 * uio_interrupt - hardware interrupt handler
255 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
256 * @dev_id: Pointer to the devices uio_device structure
258 static irqreturn_t
uio_interrupt(int irq
, void *dev_id
)
260 struct uio_device
*idev
= (struct uio_device
*)dev_id
;
261 irqreturn_t ret
= idev
->info
->handler(irq
, idev
->info
);
263 if (ret
== IRQ_HANDLED
)
264 uio_event_notify(idev
->info
);
269 struct uio_listener
{
270 struct uio_device
*dev
;
274 static int uio_open(struct inode
*inode
, struct file
*filep
)
276 struct uio_device
*idev
;
277 struct uio_listener
*listener
;
280 idev
= idr_find(&uio_idr
, iminor(inode
));
284 listener
= kmalloc(sizeof(*listener
), GFP_KERNEL
);
288 listener
->dev
= idev
;
289 listener
->event_count
= atomic_read(&idev
->event
);
290 filep
->private_data
= listener
;
292 if (idev
->info
->open
) {
293 if (!try_module_get(idev
->owner
))
295 ret
= idev
->info
->open(idev
->info
, inode
);
296 module_put(idev
->owner
);
305 static int uio_fasync(int fd
, struct file
*filep
, int on
)
307 struct uio_listener
*listener
= filep
->private_data
;
308 struct uio_device
*idev
= listener
->dev
;
310 return fasync_helper(fd
, filep
, on
, &idev
->async_queue
);
313 static int uio_release(struct inode
*inode
, struct file
*filep
)
316 struct uio_listener
*listener
= filep
->private_data
;
317 struct uio_device
*idev
= listener
->dev
;
319 if (idev
->info
->release
) {
320 if (!try_module_get(idev
->owner
))
322 ret
= idev
->info
->release(idev
->info
, inode
);
323 module_put(idev
->owner
);
325 if (filep
->f_flags
& FASYNC
)
326 ret
= uio_fasync(-1, filep
, 0);
331 static unsigned int uio_poll(struct file
*filep
, poll_table
*wait
)
333 struct uio_listener
*listener
= filep
->private_data
;
334 struct uio_device
*idev
= listener
->dev
;
336 if (idev
->info
->irq
== UIO_IRQ_NONE
)
339 poll_wait(filep
, &idev
->wait
, wait
);
340 if (listener
->event_count
!= atomic_read(&idev
->event
))
341 return POLLIN
| POLLRDNORM
;
345 static ssize_t
uio_read(struct file
*filep
, char __user
*buf
,
346 size_t count
, loff_t
*ppos
)
348 struct uio_listener
*listener
= filep
->private_data
;
349 struct uio_device
*idev
= listener
->dev
;
350 DECLARE_WAITQUEUE(wait
, current
);
354 if (idev
->info
->irq
== UIO_IRQ_NONE
)
357 if (count
!= sizeof(s32
))
360 add_wait_queue(&idev
->wait
, &wait
);
363 set_current_state(TASK_INTERRUPTIBLE
);
365 event_count
= atomic_read(&idev
->event
);
366 if (event_count
!= listener
->event_count
) {
367 if (copy_to_user(buf
, &event_count
, count
))
370 listener
->event_count
= event_count
;
376 if (filep
->f_flags
& O_NONBLOCK
) {
381 if (signal_pending(current
)) {
382 retval
= -ERESTARTSYS
;
388 __set_current_state(TASK_RUNNING
);
389 remove_wait_queue(&idev
->wait
, &wait
);
394 static int uio_find_mem_index(struct vm_area_struct
*vma
)
397 struct uio_device
*idev
= vma
->vm_private_data
;
399 for (mi
= 0; mi
< MAX_UIO_MAPS
; mi
++) {
400 if (idev
->info
->mem
[mi
].size
== 0)
402 if (vma
->vm_pgoff
== mi
)
408 static void uio_vma_open(struct vm_area_struct
*vma
)
410 struct uio_device
*idev
= vma
->vm_private_data
;
414 static void uio_vma_close(struct vm_area_struct
*vma
)
416 struct uio_device
*idev
= vma
->vm_private_data
;
420 static struct page
*uio_vma_nopage(struct vm_area_struct
*vma
,
421 unsigned long address
, int *type
)
423 struct uio_device
*idev
= vma
->vm_private_data
;
424 struct page
* page
= NOPAGE_SIGBUS
;
426 int mi
= uio_find_mem_index(vma
);
430 if (idev
->info
->mem
[mi
].memtype
== UIO_MEM_LOGICAL
)
431 page
= virt_to_page(idev
->info
->mem
[mi
].addr
);
433 page
= vmalloc_to_page((void*)idev
->info
->mem
[mi
].addr
);
436 *type
= VM_FAULT_MINOR
;
440 static struct vm_operations_struct uio_vm_ops
= {
441 .open
= uio_vma_open
,
442 .close
= uio_vma_close
,
443 .nopage
= uio_vma_nopage
,
446 static int uio_mmap_physical(struct vm_area_struct
*vma
)
448 struct uio_device
*idev
= vma
->vm_private_data
;
449 int mi
= uio_find_mem_index(vma
);
453 vma
->vm_flags
|= VM_IO
| VM_RESERVED
;
455 return remap_pfn_range(vma
,
457 idev
->info
->mem
[mi
].addr
>> PAGE_SHIFT
,
458 vma
->vm_end
- vma
->vm_start
,
462 static int uio_mmap_logical(struct vm_area_struct
*vma
)
464 vma
->vm_flags
|= VM_RESERVED
;
465 vma
->vm_ops
= &uio_vm_ops
;
470 static int uio_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
472 struct uio_listener
*listener
= filep
->private_data
;
473 struct uio_device
*idev
= listener
->dev
;
475 unsigned long requested_pages
, actual_pages
;
478 if (vma
->vm_end
< vma
->vm_start
)
481 vma
->vm_private_data
= idev
;
483 mi
= uio_find_mem_index(vma
);
487 requested_pages
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
488 actual_pages
= (idev
->info
->mem
[mi
].size
+ PAGE_SIZE
-1) >> PAGE_SHIFT
;
489 if (requested_pages
> actual_pages
)
492 if (idev
->info
->mmap
) {
493 if (!try_module_get(idev
->owner
))
495 ret
= idev
->info
->mmap(idev
->info
, vma
);
496 module_put(idev
->owner
);
500 switch (idev
->info
->mem
[mi
].memtype
) {
502 return uio_mmap_physical(vma
);
503 case UIO_MEM_LOGICAL
:
504 case UIO_MEM_VIRTUAL
:
505 return uio_mmap_logical(vma
);
511 static const struct file_operations uio_fops
= {
512 .owner
= THIS_MODULE
,
514 .release
= uio_release
,
518 .fasync
= uio_fasync
,
521 static int uio_major_init(void)
523 uio_major
= register_chrdev(0, "uio", &uio_fops
);
529 static void uio_major_cleanup(void)
531 unregister_chrdev(uio_major
, "uio");
534 static int init_uio_class(void)
538 if (uio_class
!= NULL
) {
539 kref_get(&uio_class
->kref
);
543 /* This is the first time in here, set everything up properly */
544 ret
= uio_major_init();
548 uio_class
= kzalloc(sizeof(*uio_class
), GFP_KERNEL
);
554 kref_init(&uio_class
->kref
);
555 uio_class
->class = class_create(THIS_MODULE
, "uio");
556 if (IS_ERR(uio_class
->class)) {
557 ret
= IS_ERR(uio_class
->class);
558 printk(KERN_ERR
"class_create failed for uio\n");
559 goto err_class_create
;
572 static void release_uio_class(struct kref
*kref
)
574 /* Ok, we cheat as we know we only have one uio_class */
575 class_destroy(uio_class
->class);
581 static void uio_class_destroy(void)
584 kref_put(&uio_class
->kref
, release_uio_class
);
588 * uio_register_device - register a new userspace IO device
589 * @owner: module that creates the new device
590 * @parent: parent device
591 * @info: UIO device capabilities
593 * returns zero on success or a negative error code.
595 int __uio_register_device(struct module
*owner
,
596 struct device
*parent
,
597 struct uio_info
*info
)
599 struct uio_device
*idev
;
602 if (!parent
|| !info
|| !info
->name
|| !info
->version
)
605 info
->uio_dev
= NULL
;
607 ret
= init_uio_class();
611 idev
= kzalloc(sizeof(*idev
), GFP_KERNEL
);
619 init_waitqueue_head(&idev
->wait
);
620 atomic_set(&idev
->event
, 0);
622 ret
= uio_get_minor(idev
);
626 idev
->dev
= device_create(uio_class
->class, parent
,
627 MKDEV(uio_major
, idev
->minor
),
628 "uio%d", idev
->minor
);
629 if (IS_ERR(idev
->dev
)) {
630 printk(KERN_ERR
"UIO: device register failed\n");
631 ret
= PTR_ERR(idev
->dev
);
632 goto err_device_create
;
634 dev_set_drvdata(idev
->dev
, idev
);
636 ret
= uio_dev_add_attributes(idev
);
638 goto err_uio_dev_add_attributes
;
640 info
->uio_dev
= idev
;
642 if (idev
->info
->irq
>= 0) {
643 ret
= request_irq(idev
->info
->irq
, uio_interrupt
,
644 idev
->info
->irq_flags
, idev
->info
->name
, idev
);
646 goto err_request_irq
;
652 uio_dev_del_attributes(idev
);
653 err_uio_dev_add_attributes
:
654 device_destroy(uio_class
->class, MKDEV(uio_major
, idev
->minor
));
656 uio_free_minor(idev
);
663 EXPORT_SYMBOL_GPL(__uio_register_device
);
666 * uio_unregister_device - unregister a industrial IO device
667 * @info: UIO device capabilities
670 void uio_unregister_device(struct uio_info
*info
)
672 struct uio_device
*idev
;
674 if (!info
|| !info
->uio_dev
)
677 idev
= info
->uio_dev
;
679 uio_free_minor(idev
);
682 free_irq(info
->irq
, idev
);
684 uio_dev_del_attributes(idev
);
686 dev_set_drvdata(idev
->dev
, NULL
);
687 device_destroy(uio_class
->class, MKDEV(uio_major
, idev
->minor
));
693 EXPORT_SYMBOL_GPL(uio_unregister_device
);
695 static int __init
uio_init(void)
700 static void __exit
uio_exit(void)
704 module_init(uio_init
)
705 module_exit(uio_exit
)
706 MODULE_LICENSE("GPL v2");