uio-howto.tmpl 29 KB

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