uio-howto.tmpl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. <book id="index">
  5. <bookinfo>
  6. <title>The Userspace I/O HOWTO</title>
  7. <author>
  8. <firstname>Hans-Jürgen</firstname>
  9. <surname>Koch</surname>
  10. <authorblurb><para>Linux developer, Linutronix</para></authorblurb>
  11. <affiliation>
  12. <orgname>
  13. <ulink url="http://www.linutronix.de">Linutronix</ulink>
  14. </orgname>
  15. <address>
  16. <email>hjk@linutronix.de</email>
  17. </address>
  18. </affiliation>
  19. </author>
  20. <copyright>
  21. <year>2006-2008</year>
  22. <holder>Hans-Jürgen Koch.</holder>
  23. </copyright>
  24. <legalnotice>
  25. <para>
  26. This documentation is Free Software licensed under the terms of the
  27. GPL version 2.
  28. </para>
  29. </legalnotice>
  30. <pubdate>2006-12-11</pubdate>
  31. <abstract>
  32. <para>This HOWTO describes concept and usage of Linux kernel's
  33. Userspace I/O system.</para>
  34. </abstract>
  35. <revhistory>
  36. <revision>
  37. <revnumber>0.6</revnumber>
  38. <date>2008-12-05</date>
  39. <authorinitials>hjk</authorinitials>
  40. <revremark>Added description of portio sysfs attributes.</revremark>
  41. </revision>
  42. <revision>
  43. <revnumber>0.5</revnumber>
  44. <date>2008-05-22</date>
  45. <authorinitials>hjk</authorinitials>
  46. <revremark>Added description of write() function.</revremark>
  47. </revision>
  48. <revision>
  49. <revnumber>0.4</revnumber>
  50. <date>2007-11-26</date>
  51. <authorinitials>hjk</authorinitials>
  52. <revremark>Removed section about uio_dummy.</revremark>
  53. </revision>
  54. <revision>
  55. <revnumber>0.3</revnumber>
  56. <date>2007-04-29</date>
  57. <authorinitials>hjk</authorinitials>
  58. <revremark>Added section about userspace drivers.</revremark>
  59. </revision>
  60. <revision>
  61. <revnumber>0.2</revnumber>
  62. <date>2007-02-13</date>
  63. <authorinitials>hjk</authorinitials>
  64. <revremark>Update after multiple mappings were added.</revremark>
  65. </revision>
  66. <revision>
  67. <revnumber>0.1</revnumber>
  68. <date>2006-12-11</date>
  69. <authorinitials>hjk</authorinitials>
  70. <revremark>First draft.</revremark>
  71. </revision>
  72. </revhistory>
  73. </bookinfo>
  74. <chapter id="aboutthisdoc">
  75. <?dbhtml filename="aboutthis.html"?>
  76. <title>About this document</title>
  77. <sect1 id="translations">
  78. <?dbhtml filename="translations.html"?>
  79. <title>Translations</title>
  80. <para>If you know of any translations for this document, or you are
  81. interested in translating it, please email me
  82. <email>hjk@linutronix.de</email>.
  83. </para>
  84. </sect1>
  85. <sect1 id="preface">
  86. <title>Preface</title>
  87. <para>
  88. For many types of devices, creating a Linux kernel driver is
  89. overkill. All that is really needed is some way to handle an
  90. interrupt and provide access to the memory space of the
  91. device. The logic of controlling the device does not
  92. necessarily have to be within the kernel, as the device does
  93. not need to take advantage of any of other resources that the
  94. kernel provides. One such common class of devices that are
  95. like this are for industrial I/O cards.
  96. </para>
  97. <para>
  98. To address this situation, the userspace I/O system (UIO) was
  99. designed. For typical industrial I/O cards, only a very small
  100. kernel module is needed. The main part of the driver will run in
  101. user space. This simplifies development and reduces the risk of
  102. serious bugs within a kernel module.
  103. </para>
  104. <para>
  105. Please note that UIO is not an universal driver interface. Devices
  106. that are already handled well by other kernel subsystems (like
  107. networking or serial or USB) are no candidates for an UIO driver.
  108. Hardware that is ideally suited for an UIO driver fulfills all of
  109. the following:
  110. </para>
  111. <itemizedlist>
  112. <listitem>
  113. <para>The device has memory that can be mapped. The device can be
  114. controlled completely by writing to this memory.</para>
  115. </listitem>
  116. <listitem>
  117. <para>The device usually generates interrupts.</para>
  118. </listitem>
  119. <listitem>
  120. <para>The device does not fit into one of the standard kernel
  121. subsystems.</para>
  122. </listitem>
  123. </itemizedlist>
  124. </sect1>
  125. <sect1 id="thanks">
  126. <title>Acknowledgments</title>
  127. <para>I'd like to thank Thomas Gleixner and Benedikt Spranger of
  128. Linutronix, who have not only written most of the UIO code, but also
  129. helped greatly writing this HOWTO by giving me all kinds of background
  130. information.</para>
  131. </sect1>
  132. <sect1 id="feedback">
  133. <title>Feedback</title>
  134. <para>Find something wrong with this document? (Or perhaps something
  135. right?) I would love to hear from you. Please email me at
  136. <email>hjk@linutronix.de</email>.</para>
  137. </sect1>
  138. </chapter>
  139. <chapter id="about">
  140. <?dbhtml filename="about.html"?>
  141. <title>About UIO</title>
  142. <para>If you use UIO for your card's driver, here's what you get:</para>
  143. <itemizedlist>
  144. <listitem>
  145. <para>only one small kernel module to write and maintain.</para>
  146. </listitem>
  147. <listitem>
  148. <para>develop the main part of your driver in user space,
  149. with all the tools and libraries you're used to.</para>
  150. </listitem>
  151. <listitem>
  152. <para>bugs in your driver won't crash the kernel.</para>
  153. </listitem>
  154. <listitem>
  155. <para>updates of your driver can take place without recompiling
  156. the kernel.</para>
  157. </listitem>
  158. </itemizedlist>
  159. <sect1 id="how_uio_works">
  160. <title>How UIO works</title>
  161. <para>
  162. Each UIO device is accessed through a device file and several
  163. sysfs attribute files. The device file will be called
  164. <filename>/dev/uio0</filename> for the first device, and
  165. <filename>/dev/uio1</filename>, <filename>/dev/uio2</filename>
  166. and so on for subsequent devices.
  167. </para>
  168. <para><filename>/dev/uioX</filename> is used to access the
  169. address space of the card. Just use
  170. <function>mmap()</function> to access registers or RAM
  171. locations of your card.
  172. </para>
  173. <para>
  174. Interrupts are handled by reading from
  175. <filename>/dev/uioX</filename>. A blocking
  176. <function>read()</function> from
  177. <filename>/dev/uioX</filename> will return as soon as an
  178. interrupt occurs. You can also use
  179. <function>select()</function> on
  180. <filename>/dev/uioX</filename> to wait for an interrupt. The
  181. integer value read from <filename>/dev/uioX</filename>
  182. represents the total interrupt count. You can use this number
  183. to figure out if you missed some interrupts.
  184. </para>
  185. <para>
  186. For some hardware that has more than one interrupt source internally,
  187. but not separate IRQ mask and status registers, there might be
  188. situations where userspace cannot determine what the interrupt source
  189. was if the kernel handler disables them by writing to the chip's IRQ
  190. register. In such a case, the kernel has to disable the IRQ completely
  191. to leave the chip's register untouched. Now the userspace part can
  192. determine the cause of the interrupt, but it cannot re-enable
  193. interrupts. Another cornercase is chips where re-enabling interrupts
  194. is a read-modify-write operation to a combined IRQ status/acknowledge
  195. register. This would be racy if a new interrupt occurred
  196. simultaneously.
  197. </para>
  198. <para>
  199. To address these problems, UIO also implements a write() function. It
  200. is normally not used and can be ignored for hardware that has only a
  201. single interrupt source or has separate IRQ mask and status registers.
  202. If you need it, however, a write to <filename>/dev/uioX</filename>
  203. will call the <function>irqcontrol()</function> function implemented
  204. by the driver. You have to write a 32-bit value that is usually either
  205. 0 or 1 to disable or enable interrupts. If a driver does not implement
  206. <function>irqcontrol()</function>, <function>write()</function> will
  207. return with <varname>-ENOSYS</varname>.
  208. </para>
  209. <para>
  210. To handle interrupts properly, your custom kernel module can
  211. provide its own interrupt handler. It will automatically be
  212. called by the built-in handler.
  213. </para>
  214. <para>
  215. For cards that don't generate interrupts but need to be
  216. polled, there is the possibility to set up a timer that
  217. triggers the interrupt handler at configurable time intervals.
  218. This interrupt simulation is done by calling
  219. <function>uio_event_notify()</function>
  220. from the timer's event handler.
  221. </para>
  222. <para>
  223. Each driver provides attributes that are used to read or write
  224. variables. These attributes are accessible through sysfs
  225. files. A custom kernel driver module can add its own
  226. attributes to the device owned by the uio driver, but not added
  227. to the UIO device itself at this time. This might change in the
  228. future if it would be found to be useful.
  229. </para>
  230. <para>
  231. The following standard attributes are provided by the UIO
  232. framework:
  233. </para>
  234. <itemizedlist>
  235. <listitem>
  236. <para>
  237. <filename>name</filename>: The name of your device. It is
  238. recommended to use the name of your kernel module for this.
  239. </para>
  240. </listitem>
  241. <listitem>
  242. <para>
  243. <filename>version</filename>: A version string defined by your
  244. driver. This allows the user space part of your driver to deal
  245. with different versions of the kernel module.
  246. </para>
  247. </listitem>
  248. <listitem>
  249. <para>
  250. <filename>event</filename>: The total number of interrupts
  251. handled by the driver since the last time the device node was
  252. read.
  253. </para>
  254. </listitem>
  255. </itemizedlist>
  256. <para>
  257. These attributes appear under the
  258. <filename>/sys/class/uio/uioX</filename> directory. Please
  259. note that this directory might be a symlink, and not a real
  260. directory. Any userspace code that accesses it must be able
  261. to handle this.
  262. </para>
  263. <para>
  264. Each UIO device can make one or more memory regions available for
  265. memory mapping. This is necessary because some industrial I/O cards
  266. require access to more than one PCI memory region in a driver.
  267. </para>
  268. <para>
  269. Each mapping has its own directory in sysfs, the first mapping
  270. appears as <filename>/sys/class/uio/uioX/maps/map0/</filename>.
  271. Subsequent mappings create directories <filename>map1/</filename>,
  272. <filename>map2/</filename>, and so on. These directories will only
  273. appear if the size of the mapping is not 0.
  274. </para>
  275. <para>
  276. Each <filename>mapX/</filename> directory contains two read-only files
  277. that show start address and size of the memory:
  278. </para>
  279. <itemizedlist>
  280. <listitem>
  281. <para>
  282. <filename>addr</filename>: The address of memory that can be mapped.
  283. </para>
  284. </listitem>
  285. <listitem>
  286. <para>
  287. <filename>size</filename>: The size, in bytes, of the memory
  288. pointed to by addr.
  289. </para>
  290. </listitem>
  291. </itemizedlist>
  292. <para>
  293. From userspace, the different mappings are distinguished by adjusting
  294. the <varname>offset</varname> parameter of the
  295. <function>mmap()</function> call. To map the memory of mapping N, you
  296. have to use N times the page size as your offset:
  297. </para>
  298. <programlisting format="linespecific">
  299. offset = N * getpagesize();
  300. </programlisting>
  301. <para>
  302. Sometimes there is hardware with memory-like regions that can not be
  303. mapped with the technique described here, but there are still ways to
  304. access them from userspace. The most common example are x86 ioports.
  305. On x86 systems, userspace can access these ioports using
  306. <function>ioperm()</function>, <function>iopl()</function>,
  307. <function>inb()</function>, <function>outb()</function>, and similar
  308. functions.
  309. </para>
  310. <para>
  311. Since these ioport regions can not be mapped, they will not appear under
  312. <filename>/sys/class/uio/uioX/maps/</filename> like the normal memory
  313. described above. Without information about the port regions a hardware
  314. has to offer, it becomes difficult for the userspace part of the
  315. driver to find out which ports belong to which UIO device.
  316. </para>
  317. <para>
  318. To address this situation, the new directory
  319. <filename>/sys/class/uio/uioX/portio/</filename> was added. It only
  320. exists if the driver wants to pass information about one or more port
  321. regions to userspace. If that is the case, subdirectories named
  322. <filename>port0</filename>, <filename>port1</filename>, and so on,
  323. will appear underneath
  324. <filename>/sys/class/uio/uioX/portio/</filename>.
  325. </para>
  326. <para>
  327. Each <filename>portX/</filename> directory contains three read-only
  328. files that show start, size, and type of the port region:
  329. </para>
  330. <itemizedlist>
  331. <listitem>
  332. <para>
  333. <filename>start</filename>: The first port of this region.
  334. </para>
  335. </listitem>
  336. <listitem>
  337. <para>
  338. <filename>size</filename>: The number of ports in this region.
  339. </para>
  340. </listitem>
  341. <listitem>
  342. <para>
  343. <filename>porttype</filename>: A string describing the type of port.
  344. </para>
  345. </listitem>
  346. </itemizedlist>
  347. </sect1>
  348. </chapter>
  349. <chapter id="custom_kernel_module" xreflabel="Writing your own kernel module">
  350. <?dbhtml filename="custom_kernel_module.html"?>
  351. <title>Writing your own kernel module</title>
  352. <para>
  353. Please have a look at <filename>uio_cif.c</filename> as an
  354. example. The following paragraphs explain the different
  355. sections of this file.
  356. </para>
  357. <sect1 id="uio_info">
  358. <title>struct uio_info</title>
  359. <para>
  360. This structure tells the framework the details of your driver,
  361. Some of the members are required, others are optional.
  362. </para>
  363. <itemizedlist>
  364. <listitem><para>
  365. <varname>const char *name</varname>: Required. The name of your driver as
  366. it will appear in sysfs. I recommend using the name of your module for this.
  367. </para></listitem>
  368. <listitem><para>
  369. <varname>const char *version</varname>: Required. This string appears in
  370. <filename>/sys/class/uio/uioX/version</filename>.
  371. </para></listitem>
  372. <listitem><para>
  373. <varname>struct uio_mem mem[ MAX_UIO_MAPS ]</varname>: Required if you
  374. have memory that can be mapped with <function>mmap()</function>. For each
  375. mapping you need to fill one of the <varname>uio_mem</varname> structures.
  376. See the description below for details.
  377. </para></listitem>
  378. <listitem><para>
  379. <varname>struct uio_port port[ MAX_UIO_PORTS_REGIONS ]</varname>: Required
  380. if you want to pass information about ioports to userspace. For each port
  381. region you need to fill one of the <varname>uio_port</varname> structures.
  382. See the description below for details.
  383. </para></listitem>
  384. <listitem><para>
  385. <varname>long irq</varname>: Required. If your hardware generates an
  386. interrupt, it's your modules task to determine the irq number during
  387. initialization. If you don't have a hardware generated interrupt but
  388. want to trigger the interrupt handler in some other way, set
  389. <varname>irq</varname> to <varname>UIO_IRQ_CUSTOM</varname>.
  390. If you had no interrupt at all, you could set
  391. <varname>irq</varname> to <varname>UIO_IRQ_NONE</varname>, though this
  392. rarely makes sense.
  393. </para></listitem>
  394. <listitem><para>
  395. <varname>unsigned long irq_flags</varname>: Required if you've set
  396. <varname>irq</varname> to a hardware interrupt number. The flags given
  397. here will be used in the call to <function>request_irq()</function>.
  398. </para></listitem>
  399. <listitem><para>
  400. <varname>int (*mmap)(struct uio_info *info, struct vm_area_struct
  401. *vma)</varname>: Optional. If you need a special
  402. <function>mmap()</function> function, you can set it here. If this
  403. pointer is not NULL, your <function>mmap()</function> will be called
  404. instead of the built-in one.
  405. </para></listitem>
  406. <listitem><para>
  407. <varname>int (*open)(struct uio_info *info, struct inode *inode)
  408. </varname>: Optional. You might want to have your own
  409. <function>open()</function>, e.g. to enable interrupts only when your
  410. device is actually used.
  411. </para></listitem>
  412. <listitem><para>
  413. <varname>int (*release)(struct uio_info *info, struct inode *inode)
  414. </varname>: Optional. If you define your own
  415. <function>open()</function>, you will probably also want a custom
  416. <function>release()</function> function.
  417. </para></listitem>
  418. <listitem><para>
  419. <varname>int (*irqcontrol)(struct uio_info *info, s32 irq_on)
  420. </varname>: Optional. If you need to be able to enable or disable
  421. interrupts from userspace by writing to <filename>/dev/uioX</filename>,
  422. you can implement this function. The parameter <varname>irq_on</varname>
  423. will be 0 to disable interrupts and 1 to enable them.
  424. </para></listitem>
  425. </itemizedlist>
  426. <para>
  427. Usually, your device will have one or more memory regions that can be mapped
  428. to user space. For each region, you have to set up a
  429. <varname>struct uio_mem</varname> in the <varname>mem[]</varname> array.
  430. Here's a description of the fields of <varname>struct uio_mem</varname>:
  431. </para>
  432. <itemizedlist>
  433. <listitem><para>
  434. <varname>int memtype</varname>: Required if the mapping is used. Set this to
  435. <varname>UIO_MEM_PHYS</varname> if you you have physical memory on your
  436. card to be mapped. Use <varname>UIO_MEM_LOGICAL</varname> for logical
  437. memory (e.g. allocated with <function>kmalloc()</function>). There's also
  438. <varname>UIO_MEM_VIRTUAL</varname> for virtual memory.
  439. </para></listitem>
  440. <listitem><para>
  441. <varname>unsigned long addr</varname>: Required if the mapping is used.
  442. Fill in the address of your memory block. This address is the one that
  443. appears in sysfs.
  444. </para></listitem>
  445. <listitem><para>
  446. <varname>unsigned long size</varname>: Fill in the size of the
  447. memory block that <varname>addr</varname> points to. If <varname>size</varname>
  448. is zero, the mapping is considered unused. Note that you
  449. <emphasis>must</emphasis> initialize <varname>size</varname> with zero for
  450. all unused mappings.
  451. </para></listitem>
  452. <listitem><para>
  453. <varname>void *internal_addr</varname>: If you have to access this memory
  454. region from within your kernel module, you will want to map it internally by
  455. using something like <function>ioremap()</function>. Addresses
  456. returned by this function cannot be mapped to user space, so you must not
  457. store it in <varname>addr</varname>. Use <varname>internal_addr</varname>
  458. instead to remember such an address.
  459. </para></listitem>
  460. </itemizedlist>
  461. <para>
  462. Please do not touch the <varname>kobj</varname> element of
  463. <varname>struct uio_mem</varname>! It is used by the UIO framework
  464. to set up sysfs files for this mapping. Simply leave it alone.
  465. </para>
  466. <para>
  467. Sometimes, your device can have one or more port regions which can not be
  468. mapped to userspace. But if there are other possibilities for userspace to
  469. access these ports, it makes sense to make information about the ports
  470. available in sysfs. For each region, you have to set up a
  471. <varname>struct uio_port</varname> in the <varname>port[]</varname> array.
  472. Here's a description of the fields of <varname>struct uio_port</varname>:
  473. </para>
  474. <itemizedlist>
  475. <listitem><para>
  476. <varname>char *porttype</varname>: Required. Set this to one of the predefined
  477. constants. Use <varname>UIO_PORT_X86</varname> for the ioports found in x86
  478. architectures.
  479. </para></listitem>
  480. <listitem><para>
  481. <varname>unsigned long start</varname>: Required if the port region is used.
  482. Fill in the number of the first port of this region.
  483. </para></listitem>
  484. <listitem><para>
  485. <varname>unsigned long size</varname>: Fill in the number of ports in this
  486. region. If <varname>size</varname> is zero, the region is considered unused.
  487. Note that you <emphasis>must</emphasis> initialize <varname>size</varname>
  488. with zero for all unused regions.
  489. </para></listitem>
  490. </itemizedlist>
  491. <para>
  492. Please do not touch the <varname>portio</varname> element of
  493. <varname>struct uio_port</varname>! It is used internally by the UIO
  494. framework to set up sysfs files for this region. Simply leave it alone.
  495. </para>
  496. </sect1>
  497. <sect1 id="adding_irq_handler">
  498. <title>Adding an interrupt handler</title>
  499. <para>
  500. What you need to do in your interrupt handler depends on your
  501. hardware and on how you want to handle it. You should try to
  502. keep the amount of code in your kernel interrupt handler low.
  503. If your hardware requires no action that you
  504. <emphasis>have</emphasis> to perform after each interrupt,
  505. then your handler can be empty.</para> <para>If, on the other
  506. hand, your hardware <emphasis>needs</emphasis> some action to
  507. be performed after each interrupt, then you
  508. <emphasis>must</emphasis> do it in your kernel module. Note
  509. that you cannot rely on the userspace part of your driver. Your
  510. userspace program can terminate at any time, possibly leaving
  511. your hardware in a state where proper interrupt handling is
  512. still required.
  513. </para>
  514. <para>
  515. There might also be applications where you want to read data
  516. from your hardware at each interrupt and buffer it in a piece
  517. of kernel memory you've allocated for that purpose. With this
  518. technique you could avoid loss of data if your userspace
  519. program misses an interrupt.
  520. </para>
  521. <para>
  522. A note on shared interrupts: Your driver should support
  523. interrupt sharing whenever this is possible. It is possible if
  524. and only if your driver can detect whether your hardware has
  525. triggered the interrupt or not. This is usually done by looking
  526. at an interrupt status register. If your driver sees that the
  527. IRQ bit is actually set, it will perform its actions, and the
  528. handler returns IRQ_HANDLED. If the driver detects that it was
  529. not your hardware that caused the interrupt, it will do nothing
  530. and return IRQ_NONE, allowing the kernel to call the next
  531. possible interrupt handler.
  532. </para>
  533. <para>
  534. If you decide not to support shared interrupts, your card
  535. won't work in computers with no free interrupts. As this
  536. frequently happens on the PC platform, you can save yourself a
  537. lot of trouble by supporting interrupt sharing.
  538. </para>
  539. </sect1>
  540. </chapter>
  541. <chapter id="userspace_driver" xreflabel="Writing a driver in user space">
  542. <?dbhtml filename="userspace_driver.html"?>
  543. <title>Writing a driver in userspace</title>
  544. <para>
  545. Once you have a working kernel module for your hardware, you can
  546. write the userspace part of your driver. You don't need any special
  547. libraries, your driver can be written in any reasonable language,
  548. you can use floating point numbers and so on. In short, you can
  549. use all the tools and libraries you'd normally use for writing a
  550. userspace application.
  551. </para>
  552. <sect1 id="getting_uio_information">
  553. <title>Getting information about your UIO device</title>
  554. <para>
  555. Information about all UIO devices is available in sysfs. The
  556. first thing you should do in your driver is check
  557. <varname>name</varname> and <varname>version</varname> to
  558. make sure your talking to the right device and that its kernel
  559. driver has the version you expect.
  560. </para>
  561. <para>
  562. You should also make sure that the memory mapping you need
  563. exists and has the size you expect.
  564. </para>
  565. <para>
  566. There is a tool called <varname>lsuio</varname> that lists
  567. UIO devices and their attributes. It is available here:
  568. </para>
  569. <para>
  570. <ulink url="http://www.osadl.org/projects/downloads/UIO/user/">
  571. http://www.osadl.org/projects/downloads/UIO/user/</ulink>
  572. </para>
  573. <para>
  574. With <varname>lsuio</varname> you can quickly check if your
  575. kernel module is loaded and which attributes it exports.
  576. Have a look at the manpage for details.
  577. </para>
  578. <para>
  579. The source code of <varname>lsuio</varname> can serve as an
  580. example for getting information about an UIO device.
  581. The file <filename>uio_helper.c</filename> contains a lot of
  582. functions you could use in your userspace driver code.
  583. </para>
  584. </sect1>
  585. <sect1 id="mmap_device_memory">
  586. <title>mmap() device memory</title>
  587. <para>
  588. After you made sure you've got the right device with the
  589. memory mappings you need, all you have to do is to call
  590. <function>mmap()</function> to map the device's memory
  591. to userspace.
  592. </para>
  593. <para>
  594. The parameter <varname>offset</varname> of the
  595. <function>mmap()</function> call has a special meaning
  596. for UIO devices: It is used to select which mapping of
  597. your device you want to map. To map the memory of
  598. mapping N, you have to use N times the page size as
  599. your offset:
  600. </para>
  601. <programlisting format="linespecific">
  602. offset = N * getpagesize();
  603. </programlisting>
  604. <para>
  605. N starts from zero, so if you've got only one memory
  606. range to map, set <varname>offset = 0</varname>.
  607. A drawback of this technique is that memory is always
  608. mapped beginning with its start address.
  609. </para>
  610. </sect1>
  611. <sect1 id="wait_for_interrupts">
  612. <title>Waiting for interrupts</title>
  613. <para>
  614. After you successfully mapped your devices memory, you
  615. can access it like an ordinary array. Usually, you will
  616. perform some initialization. After that, your hardware
  617. starts working and will generate an interrupt as soon
  618. as it's finished, has some data available, or needs your
  619. attention because an error occured.
  620. </para>
  621. <para>
  622. <filename>/dev/uioX</filename> is a read-only file. A
  623. <function>read()</function> will always block until an
  624. interrupt occurs. There is only one legal value for the
  625. <varname>count</varname> parameter of
  626. <function>read()</function>, and that is the size of a
  627. signed 32 bit integer (4). Any other value for
  628. <varname>count</varname> causes <function>read()</function>
  629. to fail. The signed 32 bit integer read is the interrupt
  630. count of your device. If the value is one more than the value
  631. you read the last time, everything is OK. If the difference
  632. is greater than one, you missed interrupts.
  633. </para>
  634. <para>
  635. You can also use <function>select()</function> on
  636. <filename>/dev/uioX</filename>.
  637. </para>
  638. </sect1>
  639. </chapter>
  640. <appendix id="app1">
  641. <title>Further information</title>
  642. <itemizedlist>
  643. <listitem><para>
  644. <ulink url="http://www.osadl.org">
  645. OSADL homepage.</ulink>
  646. </para></listitem>
  647. <listitem><para>
  648. <ulink url="http://www.linutronix.de">
  649. Linutronix homepage.</ulink>
  650. </para></listitem>
  651. </itemizedlist>
  652. </appendix>
  653. </book>