UIO: User-space I/O その3

  • $kernel source$/Documentation/DocBook/uio-howto.tmpl

The Userspace I/O HOWTOの和訳でもしようかと欲す。
殆ど Webの解説と同じなんだけどさ。

HOWTO和訳してサンプルコード入れてみたいと思っていたんだけど、まだ実行できず。

The Userspace I/O HOWTO

The Userspace I/O HOWTO
Hans-Jürgen Koch
Linutronix



This HOWTO describes concept and usage of Linux kernel's Userspace I/O system.


                                                                                                                                                              • -

Table of Contents
1. Copyright and License
2. How UIO works
3. What uio_dummy does
4. struct uio_info

1.1. Copyright and License

Copyright (c) 2006 by Hans-Jürgen Koch.

This documentation is Free Software licensed under the terms of the GPL version 2.

If you know of any translations for this document, or you are interested in translating it, please email me .

For many types of devices, creating a Linux kernel driver is overkill.
All that is really needed is some way to handle an interrupt and provide access to the memory space of the device.
The logic of controlling the device does not necessarily have to be within the kernel, as the device does not need to take advantage of any of other resources that the kernel provides.
One such common class of devices that are like this are for industrial I/O cards.

To address this situation, the userspace I/O system (UIO) was designed.
For typical industrial I/O cards, only a very small kernel module is needed.
The main part of the driver will run in user space.
This simplifies development and reduces the risk of serious bugs within a kernel module.

I'd like to thank Thomas Gleixner and Benedikt Spranger of Linutronix, who have not only written most of the UIO code, but also helped greatly writing this HOWTO by giving me all kinds of background information.

Find something wrong with this document? (Or perhaps something right?) I would love to hear from you.
Please email me at .

If you use UIO for your card's driver, here's what you get:

  • only one small kernel module to write and maintain.
  • develop the main part of your driver in user space, with all the tools and libraries you're used to.
  • bugs in your driver won't crash the kernel.
  • updates of your driver can take place without recompiling the kernel.
2.1. How UIO works

Each UIO device is accessed through a device file and several sysfs attribute files.
The device file will be called /dev/uio0 for the first device, and /dev/uio1, /dev/uio2 and so on for subsequent devices.


/dev/uioX is used to access the address space of the card.
Just use mmap() to access registers or RAM locations of your card.

Interrupts are handled by reading from /dev/uioX.
A blocking read() from /dev/uioX will return as soon as an interrupt occurs.
You can also use select() on /dev/uioX to wait for an interrupt.
The integer value read from /dev/uioX represents the total interrupt count.
You can use this number to figure out if you missed some interrupts.

To handle interrupts properly, your custom kernel module can provide its own interrupt handler.
It will automatically be called by the built-in handler.

For cards that don't generate interrupts but need to be polled, there is the possibility to set up a timer that triggers the interrupt handler at configurable time intervals.
See drivers/uio/uio_dummy.c for an example of this technique.

Each driver provides attributes that are used to read or write variables.
These attributes are accessible through sysfs files.
A custom kernel driver module can add its own attributes to the device owned by the uio driver, but not added to the UIO device itself at this time.
This might change in the future if it would be found to be useful.

The following standard attributes are provided by the UIO framework:

name
The name of your device. It is recommended to use the name of your kernel module for this.
version
A version string defined by your driver. This allows the user space part of your driver to deal with different versions of the kernel module.
event
The total number of interrupts handled by the driver since the last time the device node was read.

These attributes appear under the /sys/class/uio/uioX directory.
Please note that this directory might be a symlink, and not a real directory.
Any userspace code that accesses it must be able to handle this.

Each UIO device can make one or more memory regions available for memory mapping.
This is necessary because some industrial I/O cards require access to more than one PCI memory region in a driver.

Each mapping has its own directory in sysfs, the first mapping appears as /sys/class/uio/uioX/maps/map0/.
Subsequent mappings create directories map1/, map2/, and so on.
These directories will only appear if the size of the mapping is not 0.

Each mapX/ directory contains two read-only files that show start address and size of the memory:

addr
The address of memory that can be mapped.
size
The size, in bytes, of the memory pointed to by addr.

From userspace, the different mappings are distinguished by adjusting the offset parameter of the mmap() call.
To map the memory of mapping N, you have to use N times the page size as your offset:

offset = N * getpagesize();Well, there is no real use for uio_dummy.
Its only purpose is to test most parts of the UIO system (everything except hardware interrupts), and to serve as an example for the kernel module that you will have to write yourself.

3.1. What uio_dummy does

The kernel module uio_dummy.ko creates a device that uses a timer to generate periodic interrupts.
The interrupt handler does nothing but increment a counter.
The driver adds two custom attributes, count and freq, that appear under /sys/devices/platform/uio_dummy/.

The attribute count can be read and written.
The associated file /sys/devices/platform/uio_dummy/count appears as a normal text file and contains the total number of timer interrupts.
If you look at it (e.g. using cat), you'll notice it is slowly counting up.

The attribute freq can be read and written.
The content of /sys/devices/platform/uio_dummy/freq represents the number of system timer ticks between two timer interrupts.
The default value of freq is the value of the kernel variable HZ, which gives you an interval of one second.
Lower values will increase the frequency. Try the following:

cd /sys/devices/platform/uio_dummy/
echo 100 > freqUse cat count to see how the interrupt frequency changes. 

Please have a look at uio_dummy.c as an example.
The following paragraphs explain the different sections of this file.

4.1. struct uio_info

This structure tells the framework the details of your driver, Some of the members are required, others are optional.

char *name
Required. The name of your driver as it will appear in sysfs. I recommend using the name of your module for this.
char *version
Required. This string appears in /sys/class/uio/uioX/version.
struct uio_mem mem[ MAX_UIO_MAPS ]
Required if you have memory that can be mapped with mmap(). For each mapping you need to fill one of the uio_mem structures. See the description below for details.
long irq
Required. If your hardware generates an interrupt, it's your modules task to determine the irq number during initialization. If you don't have a hardware generated interrupt but want to trigger the interrupt handler in some other way, set irq to UIO_IRQ_CUSTOM. The uio_dummy module does this as it triggers the event mechanism in a timer routine. If you had no interrupt at all, you could set irq to UIO_IRQ_NONE, though this rarely makes sense.
unsigned long irq_flags
Required if you've set irq to a hardware interrupt number. The flags given here will be used in the call to request_irq().
int (*mmap)(struct uio_info *info, struct vm_area_struct *vma)
Optional. If you need a special mmap() function, you can set it here. If this pointer is not NULL, your mmap() will be called instead of the built-in one.
int (*open)(struct uio_info *info, struct inode *inode)
Optional. You might want to have your own open(), e.g. to enable interrupts only when your device is actually used.
int (*release)(struct uio_info *info, struct inode *inode)
Optional. If you define your own open(), you will probably also want a custom release() function.

Usually, your device will have one or more memory regions that can be mapped to user space.
For each region, you have to set up a struct uio_mem in the mem[] array.
Here's a description of the fields of struct uio_mem: