uio-howto.tmpl 21 KB

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