1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" []>
7 <title>The Userspace I/O HOWTO</title>
10 <firstname>Hans-Jürgen</firstname>
11 <surname>Koch</surname>
12 <authorblurb><para>Linux developer, Linutronix</para></authorblurb>
15 <ulink url="http://www.linutronix.de">Linutronix</ulink>
19 <email>hjk@linutronix.de</email>
25 <year>2006-2008</year>
26 <holder>Hans-Jürgen Koch.</holder>
31 This documentation is Free Software licensed under the terms of the
36 <pubdate>2006-12-11</pubdate>
39 <para>This HOWTO describes concept and usage of Linux kernel's
40 Userspace I/O system.</para>
45 <revnumber>0.6</revnumber>
46 <date>2008-12-05</date>
47 <authorinitials>hjk</authorinitials>
48 <revremark>Added description of portio sysfs attributes.</revremark>
51 <revnumber>0.5</revnumber>
52 <date>2008-05-22</date>
53 <authorinitials>hjk</authorinitials>
54 <revremark>Added description of write() function.</revremark>
57 <revnumber>0.4</revnumber>
58 <date>2007-11-26</date>
59 <authorinitials>hjk</authorinitials>
60 <revremark>Removed section about uio_dummy.</revremark>
63 <revnumber>0.3</revnumber>
64 <date>2007-04-29</date>
65 <authorinitials>hjk</authorinitials>
66 <revremark>Added section about userspace drivers.</revremark>
69 <revnumber>0.2</revnumber>
70 <date>2007-02-13</date>
71 <authorinitials>hjk</authorinitials>
72 <revremark>Update after multiple mappings were added.</revremark>
75 <revnumber>0.1</revnumber>
76 <date>2006-12-11</date>
77 <authorinitials>hjk</authorinitials>
78 <revremark>First draft.</revremark>
83 <chapter id="aboutthisdoc">
84 <?dbhtml filename="aboutthis.html"?>
85 <title>About this document</title>
87 <sect1 id="translations">
88 <?dbhtml filename="translations.html"?>
89 <title>Translations</title>
91 <para>If you know of any translations for this document, or you are
92 interested in translating it, please email me
93 <email>hjk@linutronix.de</email>.
98 <title>Preface</title>
100 For many types of devices, creating a Linux kernel driver is
101 overkill. All that is really needed is some way to handle an
102 interrupt and provide access to the memory space of the
103 device. The logic of controlling the device does not
104 necessarily have to be within the kernel, as the device does
105 not need to take advantage of any of other resources that the
106 kernel provides. One such common class of devices that are
107 like this are for industrial I/O cards.
110 To address this situation, the userspace I/O system (UIO) was
111 designed. For typical industrial I/O cards, only a very small
112 kernel module is needed. The main part of the driver will run in
113 user space. This simplifies development and reduces the risk of
114 serious bugs within a kernel module.
117 Please note that UIO is not an universal driver interface. Devices
118 that are already handled well by other kernel subsystems (like
119 networking or serial or USB) are no candidates for an UIO driver.
120 Hardware that is ideally suited for an UIO driver fulfills all of
125 <para>The device has memory that can be mapped. The device can be
126 controlled completely by writing to this memory.</para>
129 <para>The device usually generates interrupts.</para>
132 <para>The device does not fit into one of the standard kernel
139 <title>Acknowledgments</title>
140 <para>I'd like to thank Thomas Gleixner and Benedikt Spranger of
141 Linutronix, who have not only written most of the UIO code, but also
142 helped greatly writing this HOWTO by giving me all kinds of background
146 <sect1 id="feedback">
147 <title>Feedback</title>
148 <para>Find something wrong with this document? (Or perhaps something
149 right?) I would love to hear from you. Please email me at
150 <email>hjk@linutronix.de</email>.</para>
155 <?dbhtml filename="about.html"?>
156 <title>About UIO</title>
158 <para>If you use UIO for your card's driver, here's what you get:</para>
162 <para>only one small kernel module to write and maintain.</para>
165 <para>develop the main part of your driver in user space,
166 with all the tools and libraries you're used to.</para>
169 <para>bugs in your driver won't crash the kernel.</para>
172 <para>updates of your driver can take place without recompiling
177 <sect1 id="how_uio_works">
178 <title>How UIO works</title>
180 Each UIO device is accessed through a device file and several
181 sysfs attribute files. The device file will be called
182 <filename>/dev/uio0</filename> for the first device, and
183 <filename>/dev/uio1</filename>, <filename>/dev/uio2</filename>
184 and so on for subsequent devices.
187 <para><filename>/dev/uioX</filename> is used to access the
188 address space of the card. Just use
189 <function>mmap()</function> to access registers or RAM
190 locations of your card.
194 Interrupts are handled by reading from
195 <filename>/dev/uioX</filename>. A blocking
196 <function>read()</function> from
197 <filename>/dev/uioX</filename> will return as soon as an
198 interrupt occurs. You can also use
199 <function>select()</function> on
200 <filename>/dev/uioX</filename> to wait for an interrupt. The
201 integer value read from <filename>/dev/uioX</filename>
202 represents the total interrupt count. You can use this number
203 to figure out if you missed some interrupts.
206 For some hardware that has more than one interrupt source internally,
207 but not separate IRQ mask and status registers, there might be
208 situations where userspace cannot determine what the interrupt source
209 was if the kernel handler disables them by writing to the chip's IRQ
210 register. In such a case, the kernel has to disable the IRQ completely
211 to leave the chip's register untouched. Now the userspace part can
212 determine the cause of the interrupt, but it cannot re-enable
213 interrupts. Another cornercase is chips where re-enabling interrupts
214 is a read-modify-write operation to a combined IRQ status/acknowledge
215 register. This would be racy if a new interrupt occurred
219 To address these problems, UIO also implements a write() function. It
220 is normally not used and can be ignored for hardware that has only a
221 single interrupt source or has separate IRQ mask and status registers.
222 If you need it, however, a write to <filename>/dev/uioX</filename>
223 will call the <function>irqcontrol()</function> function implemented
224 by the driver. You have to write a 32-bit value that is usually either
225 0 or 1 to disable or enable interrupts. If a driver does not implement
226 <function>irqcontrol()</function>, <function>write()</function> will
227 return with <varname>-ENOSYS</varname>.
231 To handle interrupts properly, your custom kernel module can
232 provide its own interrupt handler. It will automatically be
233 called by the built-in handler.
237 For cards that don't generate interrupts but need to be
238 polled, there is the possibility to set up a timer that
239 triggers the interrupt handler at configurable time intervals.
240 This interrupt simulation is done by calling
241 <function>uio_event_notify()</function>
242 from the timer's event handler.
246 Each driver provides attributes that are used to read or write
247 variables. These attributes are accessible through sysfs
248 files. A custom kernel driver module can add its own
249 attributes to the device owned by the uio driver, but not added
250 to the UIO device itself at this time. This might change in the
251 future if it would be found to be useful.
255 The following standard attributes are provided by the UIO
261 <filename>name</filename>: The name of your device. It is
262 recommended to use the name of your kernel module for this.
267 <filename>version</filename>: A version string defined by your
268 driver. This allows the user space part of your driver to deal
269 with different versions of the kernel module.
274 <filename>event</filename>: The total number of interrupts
275 handled by the driver since the last time the device node was
281 These attributes appear under the
282 <filename>/sys/class/uio/uioX</filename> directory. Please
283 note that this directory might be a symlink, and not a real
284 directory. Any userspace code that accesses it must be able
288 Each UIO device can make one or more memory regions available for
289 memory mapping. This is necessary because some industrial I/O cards
290 require access to more than one PCI memory region in a driver.
293 Each mapping has its own directory in sysfs, the first mapping
294 appears as <filename>/sys/class/uio/uioX/maps/map0/</filename>.
295 Subsequent mappings create directories <filename>map1/</filename>,
296 <filename>map2/</filename>, and so on. These directories will only
297 appear if the size of the mapping is not 0.
300 Each <filename>mapX/</filename> directory contains two read-only files
301 that show start address and size of the memory:
306 <filename>addr</filename>: The address of memory that can be mapped.
311 <filename>size</filename>: The size, in bytes, of the memory
318 From userspace, the different mappings are distinguished by adjusting
319 the <varname>offset</varname> parameter of the
320 <function>mmap()</function> call. To map the memory of mapping N, you
321 have to use N times the page size as your offset:
323 <programlisting format="linespecific">
324 offset = N * getpagesize();
328 Sometimes there is hardware with memory-like regions that can not be
329 mapped with the technique described here, but there are still ways to
330 access them from userspace. The most common example are x86 ioports.
331 On x86 systems, userspace can access these ioports using
332 <function>ioperm()</function>, <function>iopl()</function>,
333 <function>inb()</function>, <function>outb()</function>, and similar
337 Since these ioport regions can not be mapped, they will not appear under
338 <filename>/sys/class/uio/uioX/maps/</filename> like the normal memory
339 described above. Without information about the port regions a hardware
340 has to offer, it becomes difficult for the userspace part of the
341 driver to find out which ports belong to which UIO device.
344 To address this situation, the new directory
345 <filename>/sys/class/uio/uioX/portio/</filename> was added. It only
346 exists if the driver wants to pass information about one or more port
347 regions to userspace. If that is the case, subdirectories named
348 <filename>port0</filename>, <filename>port1</filename>, and so on,
349 will appear underneath
350 <filename>/sys/class/uio/uioX/portio/</filename>.
353 Each <filename>portX/</filename> directory contains three read-only
354 files that show start, size, and type of the port region:
359 <filename>start</filename>: The first port of this region.
364 <filename>size</filename>: The number of ports in this region.
369 <filename>porttype</filename>: A string describing the type of port.
378 <chapter id="custom_kernel_module" xreflabel="Writing your own kernel module">
379 <?dbhtml filename="custom_kernel_module.html"?>
380 <title>Writing your own kernel module</title>
382 Please have a look at <filename>uio_cif.c</filename> as an
383 example. The following paragraphs explain the different
384 sections of this file.
387 <sect1 id="uio_info">
388 <title>struct uio_info</title>
390 This structure tells the framework the details of your driver,
391 Some of the members are required, others are optional.
396 <varname>const char *name</varname>: Required. The name of your driver as
397 it will appear in sysfs. I recommend using the name of your module for this.
401 <varname>const char *version</varname>: Required. This string appears in
402 <filename>/sys/class/uio/uioX/version</filename>.
406 <varname>struct uio_mem mem[ MAX_UIO_MAPS ]</varname>: Required if you
407 have memory that can be mapped with <function>mmap()</function>. For each
408 mapping you need to fill one of the <varname>uio_mem</varname> structures.
409 See the description below for details.
413 <varname>struct uio_port port[ MAX_UIO_PORTS_REGIONS ]</varname>: Required
414 if you want to pass information about ioports to userspace. For each port
415 region you need to fill one of the <varname>uio_port</varname> structures.
416 See the description below for details.
420 <varname>long irq</varname>: Required. If your hardware generates an
421 interrupt, it's your modules task to determine the irq number during
422 initialization. If you don't have a hardware generated interrupt but
423 want to trigger the interrupt handler in some other way, set
424 <varname>irq</varname> to <varname>UIO_IRQ_CUSTOM</varname>.
425 If you had no interrupt at all, you could set
426 <varname>irq</varname> to <varname>UIO_IRQ_NONE</varname>, though this
431 <varname>unsigned long irq_flags</varname>: Required if you've set
432 <varname>irq</varname> to a hardware interrupt number. The flags given
433 here will be used in the call to <function>request_irq()</function>.
437 <varname>int (*mmap)(struct uio_info *info, struct vm_area_struct
438 *vma)</varname>: Optional. If you need a special
439 <function>mmap()</function> function, you can set it here. If this
440 pointer is not NULL, your <function>mmap()</function> will be called
441 instead of the built-in one.
445 <varname>int (*open)(struct uio_info *info, struct inode *inode)
446 </varname>: Optional. You might want to have your own
447 <function>open()</function>, e.g. to enable interrupts only when your
448 device is actually used.
452 <varname>int (*release)(struct uio_info *info, struct inode *inode)
453 </varname>: Optional. If you define your own
454 <function>open()</function>, you will probably also want a custom
455 <function>release()</function> function.
459 <varname>int (*irqcontrol)(struct uio_info *info, s32 irq_on)
460 </varname>: Optional. If you need to be able to enable or disable
461 interrupts from userspace by writing to <filename>/dev/uioX</filename>,
462 you can implement this function. The parameter <varname>irq_on</varname>
463 will be 0 to disable interrupts and 1 to enable them.
468 Usually, your device will have one or more memory regions that can be mapped
469 to user space. For each region, you have to set up a
470 <varname>struct uio_mem</varname> in the <varname>mem[]</varname> array.
471 Here's a description of the fields of <varname>struct uio_mem</varname>:
476 <varname>int memtype</varname>: Required if the mapping is used. Set this to
477 <varname>UIO_MEM_PHYS</varname> if you you have physical memory on your
478 card to be mapped. Use <varname>UIO_MEM_LOGICAL</varname> for logical
479 memory (e.g. allocated with <function>kmalloc()</function>). There's also
480 <varname>UIO_MEM_VIRTUAL</varname> for virtual memory.
484 <varname>unsigned long addr</varname>: Required if the mapping is used.
485 Fill in the address of your memory block. This address is the one that
490 <varname>unsigned long size</varname>: Fill in the size of the
491 memory block that <varname>addr</varname> points to. If <varname>size</varname>
492 is zero, the mapping is considered unused. Note that you
493 <emphasis>must</emphasis> initialize <varname>size</varname> with zero for
498 <varname>void *internal_addr</varname>: If you have to access this memory
499 region from within your kernel module, you will want to map it internally by
500 using something like <function>ioremap()</function>. Addresses
501 returned by this function cannot be mapped to user space, so you must not
502 store it in <varname>addr</varname>. Use <varname>internal_addr</varname>
503 instead to remember such an address.
508 Please do not touch the <varname>kobj</varname> element of
509 <varname>struct uio_mem</varname>! It is used by the UIO framework
510 to set up sysfs files for this mapping. Simply leave it alone.
514 Sometimes, your device can have one or more port regions which can not be
515 mapped to userspace. But if there are other possibilities for userspace to
516 access these ports, it makes sense to make information about the ports
517 available in sysfs. For each region, you have to set up a
518 <varname>struct uio_port</varname> in the <varname>port[]</varname> array.
519 Here's a description of the fields of <varname>struct uio_port</varname>:
524 <varname>char *porttype</varname>: Required. Set this to one of the predefined
525 constants. Use <varname>UIO_PORT_X86</varname> for the ioports found in x86
530 <varname>unsigned long start</varname>: Required if the port region is used.
531 Fill in the number of the first port of this region.
535 <varname>unsigned long size</varname>: Fill in the number of ports in this
536 region. If <varname>size</varname> is zero, the region is considered unused.
537 Note that you <emphasis>must</emphasis> initialize <varname>size</varname>
538 with zero for all unused regions.
543 Please do not touch the <varname>portio</varname> element of
544 <varname>struct uio_port</varname>! It is used internally by the UIO
545 framework to set up sysfs files for this region. Simply leave it alone.
550 <sect1 id="adding_irq_handler">
551 <title>Adding an interrupt handler</title>
553 What you need to do in your interrupt handler depends on your
554 hardware and on how you want to handle it. You should try to
555 keep the amount of code in your kernel interrupt handler low.
556 If your hardware requires no action that you
557 <emphasis>have</emphasis> to perform after each interrupt,
558 then your handler can be empty.</para> <para>If, on the other
559 hand, your hardware <emphasis>needs</emphasis> some action to
560 be performed after each interrupt, then you
561 <emphasis>must</emphasis> do it in your kernel module. Note
562 that you cannot rely on the userspace part of your driver. Your
563 userspace program can terminate at any time, possibly leaving
564 your hardware in a state where proper interrupt handling is
569 There might also be applications where you want to read data
570 from your hardware at each interrupt and buffer it in a piece
571 of kernel memory you've allocated for that purpose. With this
572 technique you could avoid loss of data if your userspace
573 program misses an interrupt.
577 A note on shared interrupts: Your driver should support
578 interrupt sharing whenever this is possible. It is possible if
579 and only if your driver can detect whether your hardware has
580 triggered the interrupt or not. This is usually done by looking
581 at an interrupt status register. If your driver sees that the
582 IRQ bit is actually set, it will perform its actions, and the
583 handler returns IRQ_HANDLED. If the driver detects that it was
584 not your hardware that caused the interrupt, it will do nothing
585 and return IRQ_NONE, allowing the kernel to call the next
586 possible interrupt handler.
590 If you decide not to support shared interrupts, your card
591 won't work in computers with no free interrupts. As this
592 frequently happens on the PC platform, you can save yourself a
593 lot of trouble by supporting interrupt sharing.
599 <chapter id="userspace_driver" xreflabel="Writing a driver in user space">
600 <?dbhtml filename="userspace_driver.html"?>
601 <title>Writing a driver in userspace</title>
603 Once you have a working kernel module for your hardware, you can
604 write the userspace part of your driver. You don't need any special
605 libraries, your driver can be written in any reasonable language,
606 you can use floating point numbers and so on. In short, you can
607 use all the tools and libraries you'd normally use for writing a
608 userspace application.
611 <sect1 id="getting_uio_information">
612 <title>Getting information about your UIO device</title>
614 Information about all UIO devices is available in sysfs. The
615 first thing you should do in your driver is check
616 <varname>name</varname> and <varname>version</varname> to
617 make sure your talking to the right device and that its kernel
618 driver has the version you expect.
621 You should also make sure that the memory mapping you need
622 exists and has the size you expect.
625 There is a tool called <varname>lsuio</varname> that lists
626 UIO devices and their attributes. It is available here:
629 <ulink url="http://www.osadl.org/projects/downloads/UIO/user/">
630 http://www.osadl.org/projects/downloads/UIO/user/</ulink>
633 With <varname>lsuio</varname> you can quickly check if your
634 kernel module is loaded and which attributes it exports.
635 Have a look at the manpage for details.
638 The source code of <varname>lsuio</varname> can serve as an
639 example for getting information about an UIO device.
640 The file <filename>uio_helper.c</filename> contains a lot of
641 functions you could use in your userspace driver code.
645 <sect1 id="mmap_device_memory">
646 <title>mmap() device memory</title>
648 After you made sure you've got the right device with the
649 memory mappings you need, all you have to do is to call
650 <function>mmap()</function> to map the device's memory
654 The parameter <varname>offset</varname> of the
655 <function>mmap()</function> call has a special meaning
656 for UIO devices: It is used to select which mapping of
657 your device you want to map. To map the memory of
658 mapping N, you have to use N times the page size as
661 <programlisting format="linespecific">
662 offset = N * getpagesize();
665 N starts from zero, so if you've got only one memory
666 range to map, set <varname>offset = 0</varname>.
667 A drawback of this technique is that memory is always
668 mapped beginning with its start address.
672 <sect1 id="wait_for_interrupts">
673 <title>Waiting for interrupts</title>
675 After you successfully mapped your devices memory, you
676 can access it like an ordinary array. Usually, you will
677 perform some initialization. After that, your hardware
678 starts working and will generate an interrupt as soon
679 as it's finished, has some data available, or needs your
680 attention because an error occured.
683 <filename>/dev/uioX</filename> is a read-only file. A
684 <function>read()</function> will always block until an
685 interrupt occurs. There is only one legal value for the
686 <varname>count</varname> parameter of
687 <function>read()</function>, and that is the size of a
688 signed 32 bit integer (4). Any other value for
689 <varname>count</varname> causes <function>read()</function>
690 to fail. The signed 32 bit integer read is the interrupt
691 count of your device. If the value is one more than the value
692 you read the last time, everything is OK. If the difference
693 is greater than one, you missed interrupts.
696 You can also use <function>select()</function> on
697 <filename>/dev/uioX</filename>.
704 <title>Further information</title>
707 <ulink url="http://www.osadl.org">
708 OSADL homepage.</ulink>
711 <ulink url="http://www.linutronix.de">
712 Linutronix homepage.</ulink>