uio-howto.tmpl 20 KB

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