Hans J. Koch | e3e0a28 | 2006-12-11 16:59:59 +0100 | [diff] [blame] | 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" []> |
| 4 | |
| 5 | <book id="index"> |
| 6 | <bookinfo> |
| 7 | <title>The Userspace I/O HOWTO</title> |
| 8 | |
| 9 | <author> |
| 10 | <firstname>Hans-Jürgen</firstname> |
| 11 | <surname>Koch</surname> |
| 12 | <authorblurb><para>Linux developer, Linutronix</para></authorblurb> |
| 13 | <affiliation> |
| 14 | <orgname> |
| 15 | <ulink url="http://www.linutronix.de">Linutronix</ulink> |
| 16 | </orgname> |
| 17 | |
| 18 | <address> |
| 19 | <email>hjk@linutronix.de</email> |
| 20 | </address> |
| 21 | </affiliation> |
| 22 | </author> |
| 23 | |
| 24 | <pubdate>2006-12-11</pubdate> |
| 25 | |
| 26 | <abstract> |
| 27 | <para>This HOWTO describes concept and usage of Linux kernel's |
| 28 | Userspace I/O system.</para> |
| 29 | </abstract> |
| 30 | |
| 31 | <revhistory> |
| 32 | <revision> |
| 33 | <revnumber>0.3</revnumber> |
| 34 | <date>2007-04-29</date> |
| 35 | <authorinitials>hjk</authorinitials> |
| 36 | <revremark>Added section about userspace drivers.</revremark> |
| 37 | </revision> |
| 38 | <revision> |
| 39 | <revnumber>0.2</revnumber> |
| 40 | <date>2007-02-13</date> |
| 41 | <authorinitials>hjk</authorinitials> |
| 42 | <revremark>Update after multiple mappings were added.</revremark> |
| 43 | </revision> |
| 44 | <revision> |
| 45 | <revnumber>0.1</revnumber> |
| 46 | <date>2006-12-11</date> |
| 47 | <authorinitials>hjk</authorinitials> |
| 48 | <revremark>First draft.</revremark> |
| 49 | </revision> |
| 50 | </revhistory> |
| 51 | </bookinfo> |
| 52 | |
| 53 | <chapter id="aboutthisdoc"> |
| 54 | <?dbhtml filename="about.html"?> |
| 55 | <title>About this document</title> |
| 56 | |
| 57 | <sect1 id="copyright"> |
| 58 | <?dbhtml filename="copyright.html"?> |
| 59 | <title>Copyright and License</title> |
| 60 | <para> |
| 61 | Copyright (c) 2006 by Hans-Jürgen Koch.</para> |
| 62 | <para> |
| 63 | This documentation is Free Software licensed under the terms of the |
| 64 | GPL version 2. |
| 65 | </para> |
| 66 | </sect1> |
| 67 | |
| 68 | <sect1 id="translations"> |
| 69 | <?dbhtml filename="translations.html"?> |
| 70 | <title>Translations</title> |
| 71 | |
| 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>. |
| 75 | </para> |
| 76 | </sect1> |
| 77 | |
| 78 | <sect1 id="preface"> |
| 79 | <title>Preface</title> |
| 80 | <para> |
| 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. |
| 89 | </para> |
| 90 | <para> |
| 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. |
| 96 | </para> |
| 97 | </sect1> |
| 98 | |
| 99 | <sect1 id="thanks"> |
| 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 |
| 104 | information.</para> |
| 105 | </sect1> |
| 106 | |
| 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> |
| 112 | </sect1> |
| 113 | </chapter> |
| 114 | |
| 115 | <chapter id="about"> |
| 116 | <?dbhtml filename="about.html"?> |
| 117 | <title>About UIO</title> |
| 118 | |
| 119 | <para>If you use UIO for your card's driver, here's what you get:</para> |
| 120 | |
| 121 | <itemizedlist> |
| 122 | <listitem> |
| 123 | <para>only one small kernel module to write and maintain.</para> |
| 124 | </listitem> |
| 125 | <listitem> |
| 126 | <para>develop the main part of your driver in user space, |
| 127 | with all the tools and libraries you're used to.</para> |
| 128 | </listitem> |
| 129 | <listitem> |
| 130 | <para>bugs in your driver won't crash the kernel.</para> |
| 131 | </listitem> |
| 132 | <listitem> |
| 133 | <para>updates of your driver can take place without recompiling |
| 134 | the kernel.</para> |
| 135 | </listitem> |
Hans J. Koch | e3e0a28 | 2006-12-11 16:59:59 +0100 | [diff] [blame] | 136 | </itemizedlist> |
| 137 | |
| 138 | <sect1 id="how_uio_works"> |
| 139 | <title>How UIO works</title> |
| 140 | <para> |
| 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. |
| 146 | </para> |
| 147 | |
| 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. |
| 152 | </para> |
| 153 | |
| 154 | <para> |
| 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. |
| 165 | </para> |
| 166 | |
| 167 | <para> |
| 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. |
| 171 | </para> |
| 172 | |
| 173 | <para> |
| 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. |
| 179 | </para> |
| 180 | |
| 181 | <para> |
| 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. |
| 188 | </para> |
| 189 | |
| 190 | <para> |
| 191 | The following standard attributes are provided by the UIO |
| 192 | framework: |
| 193 | </para> |
| 194 | <itemizedlist> |
| 195 | <listitem> |
| 196 | <para> |
| 197 | <filename>name</filename>: The name of your device. It is |
| 198 | recommended to use the name of your kernel module for this. |
| 199 | </para> |
| 200 | </listitem> |
| 201 | <listitem> |
| 202 | <para> |
| 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. |
| 206 | </para> |
| 207 | </listitem> |
| 208 | <listitem> |
| 209 | <para> |
| 210 | <filename>event</filename>: The total number of interrupts |
| 211 | handled by the driver since the last time the device node was |
| 212 | read. |
| 213 | </para> |
| 214 | </listitem> |
| 215 | </itemizedlist> |
| 216 | <para> |
| 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 |
| 221 | to handle this. |
| 222 | </para> |
| 223 | <para> |
| 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. |
| 227 | </para> |
| 228 | <para> |
| 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. |
| 234 | </para> |
| 235 | <para> |
| 236 | Each <filename>mapX/</filename> directory contains two read-only files |
| 237 | that show start address and size of the memory: |
| 238 | </para> |
| 239 | <itemizedlist> |
| 240 | <listitem> |
| 241 | <para> |
| 242 | <filename>addr</filename>: The address of memory that can be mapped. |
| 243 | </para> |
| 244 | </listitem> |
| 245 | <listitem> |
| 246 | <para> |
| 247 | <filename>size</filename>: The size, in bytes, of the memory |
| 248 | pointed to by addr. |
| 249 | </para> |
| 250 | </listitem> |
| 251 | </itemizedlist> |
| 252 | |
| 253 | <para> |
| 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: |
| 258 | </para> |
| 259 | <programlisting format="linespecific"> |
| 260 | offset = N * getpagesize(); |
| 261 | </programlisting> |
| 262 | |
| 263 | </sect1> |
| 264 | </chapter> |
| 265 | |
| 266 | <chapter id="using-uio_dummy" xreflabel="Using uio_dummy"> |
| 267 | <?dbhtml filename="using-uio_dummy.html"?> |
| 268 | <title>Using uio_dummy</title> |
| 269 | <para> |
| 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. |
| 274 | </para> |
| 275 | |
| 276 | <sect1 id="what_uio_dummy_does"> |
| 277 | <title>What uio_dummy does</title> |
| 278 | <para> |
| 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>. |
| 285 | </para> |
| 286 | |
| 287 | <para> |
| 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 |
| 294 | up. |
| 295 | </para> |
| 296 | |
| 297 | <para> |
| 298 | The attribute <varname>freq</varname> can be read and written. |
| 299 | The content of |
| 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: |
| 306 | </para> |
| 307 | <programlisting format="linespecific"> |
| 308 | cd /sys/devices/platform/uio_dummy/ |
| 309 | echo 100 > freq |
| 310 | </programlisting> |
| 311 | <para> |
| 312 | Use <function>cat count</function> to see how the interrupt |
| 313 | frequency changes. |
| 314 | </para> |
| 315 | </sect1> |
| 316 | </chapter> |
| 317 | |
| 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> |
| 321 | <para> |
| 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. |
| 325 | </para> |
| 326 | |
| 327 | <sect1 id="uio_info"> |
| 328 | <title>struct uio_info</title> |
| 329 | <para> |
| 330 | This structure tells the framework the details of your driver, |
| 331 | Some of the members are required, others are optional. |
| 332 | </para> |
| 333 | |
| 334 | <itemizedlist> |
| 335 | <listitem><para> |
| 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. |
| 338 | </para></listitem> |
| 339 | |
| 340 | <listitem><para> |
| 341 | <varname>char *version</varname>: Required. This string appears in |
| 342 | <filename>/sys/class/uio/uioX/version</filename>. |
| 343 | </para></listitem> |
| 344 | |
| 345 | <listitem><para> |
| 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. |
| 350 | </para></listitem> |
| 351 | |
| 352 | <listitem><para> |
| 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 |
| 361 | rarely makes sense. |
| 362 | </para></listitem> |
| 363 | |
| 364 | <listitem><para> |
| 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>. |
| 368 | </para></listitem> |
| 369 | |
| 370 | <listitem><para> |
| 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. |
| 376 | </para></listitem> |
| 377 | |
| 378 | <listitem><para> |
| 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. |
| 383 | </para></listitem> |
| 384 | |
| 385 | <listitem><para> |
| 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. |
| 390 | </para></listitem> |
| 391 | </itemizedlist> |
| 392 | |
| 393 | <para> |
| 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>: |
| 398 | </para> |
| 399 | |
| 400 | <itemizedlist> |
| 401 | <listitem><para> |
| 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. |
| 407 | </para></listitem> |
| 408 | |
| 409 | <listitem><para> |
| 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 |
| 412 | appears in sysfs. |
| 413 | </para></listitem> |
| 414 | |
| 415 | <listitem><para> |
| 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 |
| 420 | all unused mappings. |
| 421 | </para></listitem> |
| 422 | |
| 423 | <listitem><para> |
| 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. |
| 430 | </para></listitem> |
| 431 | </itemizedlist> |
| 432 | |
| 433 | <para> |
| 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. |
| 437 | </para> |
| 438 | </sect1> |
| 439 | |
| 440 | <sect1 id="adding_irq_handler"> |
| 441 | <title>Adding an interrupt handler</title> |
| 442 | <para> |
| 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 |
| 455 | still required. |
| 456 | </para> |
| 457 | |
| 458 | <para> |
| 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. |
| 464 | </para> |
| 465 | |
| 466 | <para> |
| 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. |
| 477 | </para> |
| 478 | |
| 479 | <para> |
| 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. |
| 484 | </para> |
| 485 | </sect1> |
| 486 | |
| 487 | </chapter> |
| 488 | |
| 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> |
| 492 | <para> |
| 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. |
| 499 | </para> |
| 500 | |
| 501 | <sect1 id="getting_uio_information"> |
| 502 | <title>Getting information about your UIO device</title> |
| 503 | <para> |
| 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. |
| 509 | </para> |
| 510 | <para> |
| 511 | You should also make sure that the memory mapping you need |
| 512 | exists and has the size you expect. |
| 513 | </para> |
| 514 | <para> |
| 515 | There is a tool called <varname>lsuio</varname> that lists |
| 516 | UIO devices and their attributes. It is available here: |
| 517 | </para> |
| 518 | <para> |
| 519 | <ulink url="http://www.osadl.org/projects/downloads/UIO/user/"> |
| 520 | http://www.osadl.org/projects/downloads/UIO/user/</ulink> |
| 521 | </para> |
| 522 | <para> |
| 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. |
| 526 | </para> |
| 527 | <para> |
| 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. |
| 532 | </para> |
| 533 | </sect1> |
| 534 | |
| 535 | <sect1 id="mmap_device_memory"> |
| 536 | <title>mmap() device memory</title> |
| 537 | <para> |
| 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 |
| 541 | to userspace. |
| 542 | </para> |
| 543 | <para> |
| 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 |
| 549 | your offset: |
| 550 | </para> |
| 551 | <programlisting format="linespecific"> |
| 552 | offset = N * getpagesize(); |
| 553 | </programlisting> |
| 554 | <para> |
| 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. |
| 559 | </para> |
| 560 | </sect1> |
| 561 | |
| 562 | <sect1 id="wait_for_interrupts"> |
| 563 | <title>Waiting for interrupts</title> |
| 564 | <para> |
| 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. |
| 571 | </para> |
| 572 | <para> |
| 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. |
| 584 | </para> |
| 585 | <para> |
| 586 | You can also use <function>select()</function> on |
| 587 | <filename>/dev/uioX</filename>. |
| 588 | </para> |
| 589 | </sect1> |
| 590 | |
| 591 | </chapter> |
| 592 | |
| 593 | <appendix id="app1"> |
| 594 | <title>Further information</title> |
| 595 | <itemizedlist> |
| 596 | <listitem><para> |
| 597 | <ulink url="http://www.osadl.org"> |
| 598 | OSADL homepage.</ulink> |
| 599 | </para></listitem> |
| 600 | <listitem><para> |
| 601 | <ulink url="http://www.linutronix.de"> |
| 602 | Linutronix homepage.</ulink> |
| 603 | </para></listitem> |
| 604 | </itemizedlist> |
| 605 | </appendix> |
| 606 | |
| 607 | </book> |