UIO: User-space I/O その2

本家のドキュメントを読んでみる。

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


これが The Userspace I/O HOWTO

  • $kernel source$/drivers/uio/uio_cif.c


こっちが "UIO Hilscher CIF card driver"という UIOのサンプルドライバ

uio_cif.c

/*
 * UIO Hilscher CIF card driver
 *
 * (C) 2007 Hans J. Koch <hjk@linutronix.de>
 * Original code (C) 2005 Benedikt Spranger <b.spranger@linutronix.de>
 *
 * Licensed under GPL version 2 only.
 *
 */

#include <linux/device.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/uio_driver.h>

#include <asm/io.h>

#ifndef PCI_DEVICE_ID_PLX_9030
#define PCI_DEVICE_ID_PLX_9030	0x9030
#endif

#define PLX9030_INTCSR		0x4C
#define INTSCR_INT1_ENABLE	0x01
#define INTSCR_INT1_STATUS	0x04
#define INT1_ENABLED_AND_ACTIVE	(INTSCR_INT1_ENABLE | INTSCR_INT1_STATUS)

#define PCI_SUBVENDOR_ID_PEP	0x1518
#define CIF_SUBDEVICE_PROFIBUS	0x430
#define CIF_SUBDEVICE_DEVICENET	0x432


static irqreturn_t hilscher_handler(int irq, struct uio_info *dev_info)
{
	void __iomem *plx_intscr = dev_info->mem[0].internal_addr
					+ PLX9030_INTCSR;

	if ((ioread8(plx_intscr) & INT1_ENABLED_AND_ACTIVE)
	    != INT1_ENABLED_AND_ACTIVE)
		return IRQ_NONE;

	/* Disable interrupt */
	iowrite8(ioread8(plx_intscr) & ~INTSCR_INT1_ENABLE, plx_intscr);
	return IRQ_HANDLED;
}

static int __devinit hilscher_pci_probe(struct pci_dev *dev,
					const struct pci_device_id *id)
{
	struct uio_info *info;

	info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
	if (!info)
		return -ENOMEM;

	if (pci_enable_device(dev))
		goto out_free;

	if (pci_request_regions(dev, "hilscher"))
		goto out_disable;

	info->mem[0].addr = pci_resource_start(dev, 0);
	if (!info->mem[0].addr)
		goto out_release;
	info->mem[0].internal_addr = ioremap(pci_resource_start(dev, 0),
					     pci_resource_len(dev, 0));
	if (!info->mem[0].internal_addr)
		goto out_release;

	info->mem[0].size = pci_resource_len(dev, 0);
	info->mem[0].memtype = UIO_MEM_PHYS;
	info->mem[1].addr = pci_resource_start(dev, 2);
	info->mem[1].size = pci_resource_len(dev, 2);
	info->mem[1].memtype = UIO_MEM_PHYS;
	switch (id->subdevice) {
		case CIF_SUBDEVICE_PROFIBUS:
			info->name = "CIF_Profibus";
			break;
		case CIF_SUBDEVICE_DEVICENET:
			info->name = "CIF_Devicenet";
			break;
		default:
			info->name = "CIF_???";
	}
	info->version = "0.0.1";
	info->irq = dev->irq;
	info->irq_flags = IRQF_DISABLED | IRQF_SHARED;
	info->handler = hilscher_handler;

	if (uio_register_device(&dev->dev, info))
		goto out_unmap;

	pci_set_drvdata(dev, info);

	return 0;
out_unmap:
	iounmap(info->mem[0].internal_addr);
out_release:
	pci_release_regions(dev);
out_disable:
	pci_disable_device(dev);
out_free:
	kfree (info);
	return -ENODEV;
}

static void hilscher_pci_remove(struct pci_dev *dev)
{
	struct uio_info *info = pci_get_drvdata(dev);

	uio_unregister_device(info);
	pci_release_regions(dev);
	pci_disable_device(dev);
	pci_set_drvdata(dev, NULL);
	iounmap(info->mem[0].internal_addr);

	kfree (info);
}

static struct pci_device_id hilscher_pci_ids[] = {
	{
		.vendor =	PCI_VENDOR_ID_PLX,
		.device =	PCI_DEVICE_ID_PLX_9030,
		.subvendor =	PCI_SUBVENDOR_ID_PEP,
		.subdevice =	CIF_SUBDEVICE_PROFIBUS,
	},
	{
		.vendor =	PCI_VENDOR_ID_PLX,
		.device =	PCI_DEVICE_ID_PLX_9030,
		.subvendor =	PCI_SUBVENDOR_ID_PEP,
		.subdevice =	CIF_SUBDEVICE_DEVICENET,
	},
	{ 0, }
};

static struct pci_driver hilscher_pci_driver = {
	.name = "hilscher",
	.id_table = hilscher_pci_ids,
	.probe = hilscher_pci_probe,
	.remove = hilscher_pci_remove,
};

static int __init hilscher_init_module(void)
{
	return pci_register_driver(&hilscher_pci_driver);
}

static void __exit hilscher_exit_module(void)
{
	pci_unregister_driver(&hilscher_pci_driver);
}

module_init(hilscher_init_module);
module_exit(hilscher_exit_module);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Hans J. Koch, Benedikt Spranger");

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: