uio-howto.tmpl 20 KB

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