uio-howto.tmpl 28 KB

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