uio-howto.tmpl 19 KB

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