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>
24 <pubdate>2006-12-11</pubdate>
27 <para>This HOWTO describes concept and usage of Linux kernel's
28 Userspace I/O system.</para>
33 <revnumber>0.3</revnumber>
34 <date>2007-04-29</date>
35 <authorinitials>hjk</authorinitials>
36 <revremark>Added section about userspace drivers.</revremark>
39 <revnumber>0.2</revnumber>
40 <date>2007-02-13</date>
41 <authorinitials>hjk</authorinitials>
42 <revremark>Update after multiple mappings were added.</revremark>
45 <revnumber>0.1</revnumber>
46 <date>2006-12-11</date>
47 <authorinitials>hjk</authorinitials>
48 <revremark>First draft.</revremark>
53 <chapter id="aboutthisdoc">
54 <?dbhtml filename="about.html"?>
55 <title>About this document</title>
57 <sect1 id="copyright">
58 <?dbhtml filename="copyright.html"?>
59 <title>Copyright and License</title>
61 Copyright (c) 2006 by Hans-Jürgen Koch.</para>
63 This documentation is Free Software licensed under the terms of the
68 <sect1 id="translations">
69 <?dbhtml filename="translations.html"?>
70 <title>Translations</title>
72 <para>If you know of any translations for this document, or you are
73 interested in translating it, please email me
74 <email>hjk@linutronix.de</email>.
79 <title>Preface</title>
81 For many types of devices, creating a Linux kernel driver is
82 overkill. All that is really needed is some way to handle an
83 interrupt and provide access to the memory space of the
84 device. The logic of controlling the device does not
85 necessarily have to be within the kernel, as the device does
86 not need to take advantage of any of other resources that the
87 kernel provides. One such common class of devices that are
88 like this are for industrial I/O cards.
91 To address this situation, the userspace I/O system (UIO) was
92 designed. For typical industrial I/O cards, only a very small
93 kernel module is needed. The main part of the driver will run in
94 user space. This simplifies development and reduces the risk of
95 serious bugs within a kernel module.
100 <title>Acknowledgments</title>
101 <para>I'd like to thank Thomas Gleixner and Benedikt Spranger of
102 Linutronix, who have not only written most of the UIO code, but also
103 helped greatly writing this HOWTO by giving me all kinds of background
107 <sect1 id="feedback">
108 <title>Feedback</title>
109 <para>Find something wrong with this document? (Or perhaps something
110 right?) I would love to hear from you. Please email me at
111 <email>hjk@linutronix.de</email>.</para>
116 <?dbhtml filename="about.html"?>
117 <title>About UIO</title>
119 <para>If you use UIO for your card's driver, here's what you get:</para>
123 <para>only one small kernel module to write and maintain.</para>
126 <para>develop the main part of your driver in user space,
127 with all the tools and libraries you're used to.</para>
130 <para>bugs in your driver won't crash the kernel.</para>
133 <para>updates of your driver can take place without recompiling
138 <sect1 id="how_uio_works">
139 <title>How UIO works</title>
141 Each UIO device is accessed through a device file and several
142 sysfs attribute files. The device file will be called
143 <filename>/dev/uio0</filename> for the first device, and
144 <filename>/dev/uio1</filename>, <filename>/dev/uio2</filename>
145 and so on for subsequent devices.
148 <para><filename>/dev/uioX</filename> is used to access the
149 address space of the card. Just use
150 <function>mmap()</function> to access registers or RAM
151 locations of your card.
155 Interrupts are handled by reading from
156 <filename>/dev/uioX</filename>. A blocking
157 <function>read()</function> from
158 <filename>/dev/uioX</filename> will return as soon as an
159 interrupt occurs. You can also use
160 <function>select()</function> on
161 <filename>/dev/uioX</filename> to wait for an interrupt. The
162 integer value read from <filename>/dev/uioX</filename>
163 represents the total interrupt count. You can use this number
164 to figure out if you missed some interrupts.
168 To handle interrupts properly, your custom kernel module can
169 provide its own interrupt handler. It will automatically be
170 called by the built-in handler.
174 For cards that don't generate interrupts but need to be
175 polled, there is the possibility to set up a timer that
176 triggers the interrupt handler at configurable time intervals.
177 See <filename>drivers/uio/uio_dummy.c</filename> for an
178 example of this technique.
182 Each driver provides attributes that are used to read or write
183 variables. These attributes are accessible through sysfs
184 files. A custom kernel driver module can add its own
185 attributes to the device owned by the uio driver, but not added
186 to the UIO device itself at this time. This might change in the
187 future if it would be found to be useful.
191 The following standard attributes are provided by the UIO
197 <filename>name</filename>: The name of your device. It is
198 recommended to use the name of your kernel module for this.
203 <filename>version</filename>: A version string defined by your
204 driver. This allows the user space part of your driver to deal
205 with different versions of the kernel module.
210 <filename>event</filename>: The total number of interrupts
211 handled by the driver since the last time the device node was
217 These attributes appear under the
218 <filename>/sys/class/uio/uioX</filename> directory. Please
219 note that this directory might be a symlink, and not a real
220 directory. Any userspace code that accesses it must be able
224 Each UIO device can make one or more memory regions available for
225 memory mapping. This is necessary because some industrial I/O cards
226 require access to more than one PCI memory region in a driver.
229 Each mapping has its own directory in sysfs, the first mapping
230 appears as <filename>/sys/class/uio/uioX/maps/map0/</filename>.
231 Subsequent mappings create directories <filename>map1/</filename>,
232 <filename>map2/</filename>, and so on. These directories will only
233 appear if the size of the mapping is not 0.
236 Each <filename>mapX/</filename> directory contains two read-only files
237 that show start address and size of the memory:
242 <filename>addr</filename>: The address of memory that can be mapped.
247 <filename>size</filename>: The size, in bytes, of the memory
254 From userspace, the different mappings are distinguished by adjusting
255 the <varname>offset</varname> parameter of the
256 <function>mmap()</function> call. To map the memory of mapping N, you
257 have to use N times the page size as your offset:
259 <programlisting format="linespecific">
260 offset = N * getpagesize();
266 <chapter id="using-uio_dummy" xreflabel="Using uio_dummy">
267 <?dbhtml filename="using-uio_dummy.html"?>
268 <title>Using uio_dummy</title>
270 Well, there is no real use for uio_dummy. Its only purpose is
271 to test most parts of the UIO system (everything except
272 hardware interrupts), and to serve as an example for the
273 kernel module that you will have to write yourself.
276 <sect1 id="what_uio_dummy_does">
277 <title>What uio_dummy does</title>
279 The kernel module <filename>uio_dummy.ko</filename> creates a
280 device that uses a timer to generate periodic interrupts. The
281 interrupt handler does nothing but increment a counter. The
282 driver adds two custom attributes, <varname>count</varname>
283 and <varname>freq</varname>, that appear under
284 <filename>/sys/devices/platform/uio_dummy/</filename>.
288 The attribute <varname>count</varname> can be read and
289 written. The associated file
290 <filename>/sys/devices/platform/uio_dummy/count</filename>
291 appears as a normal text file and contains the total number of
292 timer interrupts. If you look at it (e.g. using
293 <function>cat</function>), you'll notice it is slowly counting
298 The attribute <varname>freq</varname> can be read and written.
300 <filename>/sys/devices/platform/uio_dummy/freq</filename>
301 represents the number of system timer ticks between two timer
302 interrupts. The default value of <varname>freq</varname> is
303 the value of the kernel variable <varname>HZ</varname>, which
304 gives you an interval of one second. Lower values will
305 increase the frequency. Try the following:
307 <programlisting format="linespecific">
308 cd /sys/devices/platform/uio_dummy/
312 Use <function>cat count</function> to see how the interrupt
318 <chapter id="custom_kernel_module" xreflabel="Writing your own kernel module">
319 <?dbhtml filename="custom_kernel_module.html"?>
320 <title>Writing your own kernel module</title>
322 Please have a look at <filename>uio_dummy.c</filename> as an
323 example. The following paragraphs explain the different
324 sections of this file.
327 <sect1 id="uio_info">
328 <title>struct uio_info</title>
330 This structure tells the framework the details of your driver,
331 Some of the members are required, others are optional.
336 <varname>char *name</varname>: Required. The name of your driver as
337 it will appear in sysfs. I recommend using the name of your module for this.
341 <varname>char *version</varname>: Required. This string appears in
342 <filename>/sys/class/uio/uioX/version</filename>.
346 <varname>struct uio_mem mem[ MAX_UIO_MAPS ]</varname>: Required if you
347 have memory that can be mapped with <function>mmap()</function>. For each
348 mapping you need to fill one of the <varname>uio_mem</varname> structures.
349 See the description below for details.
353 <varname>long irq</varname>: Required. If your hardware generates an
354 interrupt, it's your modules task to determine the irq number during
355 initialization. If you don't have a hardware generated interrupt but
356 want to trigger the interrupt handler in some other way, set
357 <varname>irq</varname> to <varname>UIO_IRQ_CUSTOM</varname>. The
358 uio_dummy module does this as it triggers the event mechanism in a timer
359 routine. If you had no interrupt at all, you could set
360 <varname>irq</varname> to <varname>UIO_IRQ_NONE</varname>, though this
365 <varname>unsigned long irq_flags</varname>: Required if you've set
366 <varname>irq</varname> to a hardware interrupt number. The flags given
367 here will be used in the call to <function>request_irq()</function>.
371 <varname>int (*mmap)(struct uio_info *info, struct vm_area_struct
372 *vma)</varname>: Optional. If you need a special
373 <function>mmap()</function> function, you can set it here. If this
374 pointer is not NULL, your <function>mmap()</function> will be called
375 instead of the built-in one.
379 <varname>int (*open)(struct uio_info *info, struct inode *inode)
380 </varname>: Optional. You might want to have your own
381 <function>open()</function>, e.g. to enable interrupts only when your
382 device is actually used.
386 <varname>int (*release)(struct uio_info *info, struct inode *inode)
387 </varname>: Optional. If you define your own
388 <function>open()</function>, you will probably also want a custom
389 <function>release()</function> function.
394 Usually, your device will have one or more memory regions that can be mapped
395 to user space. For each region, you have to set up a
396 <varname>struct uio_mem</varname> in the <varname>mem[]</varname> array.
397 Here's a description of the fields of <varname>struct uio_mem</varname>:
402 <varname>int memtype</varname>: Required if the mapping is used. Set this to
403 <varname>UIO_MEM_PHYS</varname> if you you have physical memory on your
404 card to be mapped. Use <varname>UIO_MEM_LOGICAL</varname> for logical
405 memory (e.g. allocated with <function>kmalloc()</function>). There's also
406 <varname>UIO_MEM_VIRTUAL</varname> for virtual memory.
410 <varname>unsigned long addr</varname>: Required if the mapping is used.
411 Fill in the address of your memory block. This address is the one that
416 <varname>unsigned long size</varname>: Fill in the size of the
417 memory block that <varname>addr</varname> points to. If <varname>size</varname>
418 is zero, the mapping is considered unused. Note that you
419 <emphasis>must</emphasis> initialize <varname>size</varname> with zero for
424 <varname>void *internal_addr</varname>: If you have to access this memory
425 region from within your kernel module, you will want to map it internally by
426 using something like <function>ioremap()</function>. Addresses
427 returned by this function cannot be mapped to user space, so you must not
428 store it in <varname>addr</varname>. Use <varname>internal_addr</varname>
429 instead to remember such an address.
434 Please do not touch the <varname>kobj</varname> element of
435 <varname>struct uio_mem</varname>! It is used by the UIO framework
436 to set up sysfs files for this mapping. Simply leave it alone.
440 <sect1 id="adding_irq_handler">
441 <title>Adding an interrupt handler</title>
443 What you need to do in your interrupt handler depends on your
444 hardware and on how you want to handle it. You should try to
445 keep the amount of code in your kernel interrupt handler low.
446 If your hardware requires no action that you
447 <emphasis>have</emphasis> to perform after each interrupt,
448 then your handler can be empty.</para> <para>If, on the other
449 hand, your hardware <emphasis>needs</emphasis> some action to
450 be performed after each interrupt, then you
451 <emphasis>must</emphasis> do it in your kernel module. Note
452 that you cannot rely on the userspace part of your driver. Your
453 userspace program can terminate at any time, possibly leaving
454 your hardware in a state where proper interrupt handling is
459 There might also be applications where you want to read data
460 from your hardware at each interrupt and buffer it in a piece
461 of kernel memory you've allocated for that purpose. With this
462 technique you could avoid loss of data if your userspace
463 program misses an interrupt.
467 A note on shared interrupts: Your driver should support
468 interrupt sharing whenever this is possible. It is possible if
469 and only if your driver can detect whether your hardware has
470 triggered the interrupt or not. This is usually done by looking
471 at an interrupt status register. If your driver sees that the
472 IRQ bit is actually set, it will perform its actions, and the
473 handler returns IRQ_HANDLED. If the driver detects that it was
474 not your hardware that caused the interrupt, it will do nothing
475 and return IRQ_NONE, allowing the kernel to call the next
476 possible interrupt handler.
480 If you decide not to support shared interrupts, your card
481 won't work in computers with no free interrupts. As this
482 frequently happens on the PC platform, you can save yourself a
483 lot of trouble by supporting interrupt sharing.
489 <chapter id="userspace_driver" xreflabel="Writing a driver in user space">
490 <?dbhtml filename="userspace_driver.html"?>
491 <title>Writing a driver in userspace</title>
493 Once you have a working kernel module for your hardware, you can
494 write the userspace part of your driver. You don't need any special
495 libraries, your driver can be written in any reasonable language,
496 you can use floating point numbers and so on. In short, you can
497 use all the tools and libraries you'd normally use for writing a
498 userspace application.
501 <sect1 id="getting_uio_information">
502 <title>Getting information about your UIO device</title>
504 Information about all UIO devices is available in sysfs. The
505 first thing you should do in your driver is check
506 <varname>name</varname> and <varname>version</varname> to
507 make sure your talking to the right device and that its kernel
508 driver has the version you expect.
511 You should also make sure that the memory mapping you need
512 exists and has the size you expect.
515 There is a tool called <varname>lsuio</varname> that lists
516 UIO devices and their attributes. It is available here:
519 <ulink url="http://www.osadl.org/projects/downloads/UIO/user/">
520 http://www.osadl.org/projects/downloads/UIO/user/</ulink>
523 With <varname>lsuio</varname> you can quickly check if your
524 kernel module is loaded and which attributes it exports.
525 Have a look at the manpage for details.
528 The source code of <varname>lsuio</varname> can serve as an
529 example for getting information about an UIO device.
530 The file <filename>uio_helper.c</filename> contains a lot of
531 functions you could use in your userspace driver code.
535 <sect1 id="mmap_device_memory">
536 <title>mmap() device memory</title>
538 After you made sure you've got the right device with the
539 memory mappings you need, all you have to do is to call
540 <function>mmap()</function> to map the device's memory
544 The parameter <varname>offset</varname> of the
545 <function>mmap()</function> call has a special meaning
546 for UIO devices: It is used to select which mapping of
547 your device you want to map. To map the memory of
548 mapping N, you have to use N times the page size as
551 <programlisting format="linespecific">
552 offset = N * getpagesize();
555 N starts from zero, so if you've got only one memory
556 range to map, set <varname>offset = 0</varname>.
557 A drawback of this technique is that memory is always
558 mapped beginning with its start address.
562 <sect1 id="wait_for_interrupts">
563 <title>Waiting for interrupts</title>
565 After you successfully mapped your devices memory, you
566 can access it like an ordinary array. Usually, you will
567 perform some initialization. After that, your hardware
568 starts working and will generate an interrupt as soon
569 as it's finished, has some data available, or needs your
570 attention because an error occured.
573 <filename>/dev/uioX</filename> is a read-only file. A
574 <function>read()</function> will always block until an
575 interrupt occurs. There is only one legal value for the
576 <varname>count</varname> parameter of
577 <function>read()</function>, and that is the size of a
578 signed 32 bit integer (4). Any other value for
579 <varname>count</varname> causes <function>read()</function>
580 to fail. The signed 32 bit integer read is the interrupt
581 count of your device. If the value is one more than the value
582 you read the last time, everything is OK. If the difference
583 is greater than one, you missed interrupts.
586 You can also use <function>select()</function> on
587 <filename>/dev/uioX</filename>.
594 <title>Further information</title>
597 <ulink url="http://www.osadl.org">
598 OSADL homepage.</ulink>
601 <ulink url="http://www.linutronix.de">
602 Linutronix homepage.</ulink>