uio-howto.tmpl 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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@hansjkoch.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. <copyright>
  25. <year>2009</year>
  26. <holder>Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)</holder>
  27. </copyright>
  28. <legalnotice>
  29. <para>
  30. This documentation is Free Software licensed under the terms of the
  31. GPL version 2.
  32. </para>
  33. </legalnotice>
  34. <pubdate>2006-12-11</pubdate>
  35. <abstract>
  36. <para>This HOWTO describes concept and usage of Linux kernel's
  37. Userspace I/O system.</para>
  38. </abstract>
  39. <revhistory>
  40. <revision>
  41. <revnumber>0.9</revnumber>
  42. <date>2009-07-16</date>
  43. <authorinitials>mst</authorinitials>
  44. <revremark>Added generic pci driver
  45. </revremark>
  46. </revision>
  47. <revision>
  48. <revnumber>0.8</revnumber>
  49. <date>2008-12-24</date>
  50. <authorinitials>hjk</authorinitials>
  51. <revremark>Added name attributes in mem and portio sysfs directories.
  52. </revremark>
  53. </revision>
  54. <revision>
  55. <revnumber>0.7</revnumber>
  56. <date>2008-12-23</date>
  57. <authorinitials>hjk</authorinitials>
  58. <revremark>Added generic platform drivers and offset attribute.</revremark>
  59. </revision>
  60. <revision>
  61. <revnumber>0.6</revnumber>
  62. <date>2008-12-05</date>
  63. <authorinitials>hjk</authorinitials>
  64. <revremark>Added description of portio sysfs attributes.</revremark>
  65. </revision>
  66. <revision>
  67. <revnumber>0.5</revnumber>
  68. <date>2008-05-22</date>
  69. <authorinitials>hjk</authorinitials>
  70. <revremark>Added description of write() function.</revremark>
  71. </revision>
  72. <revision>
  73. <revnumber>0.4</revnumber>
  74. <date>2007-11-26</date>
  75. <authorinitials>hjk</authorinitials>
  76. <revremark>Removed section about uio_dummy.</revremark>
  77. </revision>
  78. <revision>
  79. <revnumber>0.3</revnumber>
  80. <date>2007-04-29</date>
  81. <authorinitials>hjk</authorinitials>
  82. <revremark>Added section about userspace drivers.</revremark>
  83. </revision>
  84. <revision>
  85. <revnumber>0.2</revnumber>
  86. <date>2007-02-13</date>
  87. <authorinitials>hjk</authorinitials>
  88. <revremark>Update after multiple mappings were added.</revremark>
  89. </revision>
  90. <revision>
  91. <revnumber>0.1</revnumber>
  92. <date>2006-12-11</date>
  93. <authorinitials>hjk</authorinitials>
  94. <revremark>First draft.</revremark>
  95. </revision>
  96. </revhistory>
  97. </bookinfo>
  98. <chapter id="aboutthisdoc">
  99. <?dbhtml filename="aboutthis.html"?>
  100. <title>About this document</title>
  101. <sect1 id="translations">
  102. <?dbhtml filename="translations.html"?>
  103. <title>Translations</title>
  104. <para>If you know of any translations for this document, or you are
  105. interested in translating it, please email me
  106. <email>hjk@hansjkoch.de</email>.
  107. </para>
  108. </sect1>
  109. <sect1 id="preface">
  110. <title>Preface</title>
  111. <para>
  112. For many types of devices, creating a Linux kernel driver is
  113. overkill. All that is really needed is some way to handle an
  114. interrupt and provide access to the memory space of the
  115. device. The logic of controlling the device does not
  116. necessarily have to be within the kernel, as the device does
  117. not need to take advantage of any of other resources that the
  118. kernel provides. One such common class of devices that are
  119. like this are for industrial I/O cards.
  120. </para>
  121. <para>
  122. To address this situation, the userspace I/O system (UIO) was
  123. designed. For typical industrial I/O cards, only a very small
  124. kernel module is needed. The main part of the driver will run in
  125. user space. This simplifies development and reduces the risk of
  126. serious bugs within a kernel module.
  127. </para>
  128. <para>
  129. Please note that UIO is not an universal driver interface. Devices
  130. that are already handled well by other kernel subsystems (like
  131. networking or serial or USB) are no candidates for an UIO driver.
  132. Hardware that is ideally suited for an UIO driver fulfills all of
  133. the following:
  134. </para>
  135. <itemizedlist>
  136. <listitem>
  137. <para>The device has memory that can be mapped. The device can be
  138. controlled completely by writing to this memory.</para>
  139. </listitem>
  140. <listitem>
  141. <para>The device usually generates interrupts.</para>
  142. </listitem>
  143. <listitem>
  144. <para>The device does not fit into one of the standard kernel
  145. subsystems.</para>
  146. </listitem>
  147. </itemizedlist>
  148. </sect1>
  149. <sect1 id="thanks">
  150. <title>Acknowledgments</title>
  151. <para>I'd like to thank Thomas Gleixner and Benedikt Spranger of
  152. Linutronix, who have not only written most of the UIO code, but also
  153. helped greatly writing this HOWTO by giving me all kinds of background
  154. information.</para>
  155. </sect1>
  156. <sect1 id="feedback">
  157. <title>Feedback</title>
  158. <para>Find something wrong with this document? (Or perhaps something
  159. right?) I would love to hear from you. Please email me at
  160. <email>hjk@hansjkoch.de</email>.</para>
  161. </sect1>
  162. </chapter>
  163. <chapter id="about">
  164. <?dbhtml filename="about.html"?>
  165. <title>About UIO</title>
  166. <para>If you use UIO for your card's driver, here's what you get:</para>
  167. <itemizedlist>
  168. <listitem>
  169. <para>only one small kernel module to write and maintain.</para>
  170. </listitem>
  171. <listitem>
  172. <para>develop the main part of your driver in user space,
  173. with all the tools and libraries you're used to.</para>
  174. </listitem>
  175. <listitem>
  176. <para>bugs in your driver won't crash the kernel.</para>
  177. </listitem>
  178. <listitem>
  179. <para>updates of your driver can take place without recompiling
  180. the kernel.</para>
  181. </listitem>
  182. </itemizedlist>
  183. <sect1 id="how_uio_works">
  184. <title>How UIO works</title>
  185. <para>
  186. Each UIO device is accessed through a device file and several
  187. sysfs attribute files. The device file will be called
  188. <filename>/dev/uio0</filename> for the first device, and
  189. <filename>/dev/uio1</filename>, <filename>/dev/uio2</filename>
  190. and so on for subsequent devices.
  191. </para>
  192. <para><filename>/dev/uioX</filename> is used to access the
  193. address space of the card. Just use
  194. <function>mmap()</function> to access registers or RAM
  195. locations of your card.
  196. </para>
  197. <para>
  198. Interrupts are handled by reading from
  199. <filename>/dev/uioX</filename>. A blocking
  200. <function>read()</function> from
  201. <filename>/dev/uioX</filename> will return as soon as an
  202. interrupt occurs. You can also use
  203. <function>select()</function> on
  204. <filename>/dev/uioX</filename> to wait for an interrupt. The
  205. integer value read from <filename>/dev/uioX</filename>
  206. represents the total interrupt count. You can use this number
  207. to figure out if you missed some interrupts.
  208. </para>
  209. <para>
  210. For some hardware that has more than one interrupt source internally,
  211. but not separate IRQ mask and status registers, there might be
  212. situations where userspace cannot determine what the interrupt source
  213. was if the kernel handler disables them by writing to the chip's IRQ
  214. register. In such a case, the kernel has to disable the IRQ completely
  215. to leave the chip's register untouched. Now the userspace part can
  216. determine the cause of the interrupt, but it cannot re-enable
  217. interrupts. Another cornercase is chips where re-enabling interrupts
  218. is a read-modify-write operation to a combined IRQ status/acknowledge
  219. register. This would be racy if a new interrupt occurred
  220. simultaneously.
  221. </para>
  222. <para>
  223. To address these problems, UIO also implements a write() function. It
  224. is normally not used and can be ignored for hardware that has only a
  225. single interrupt source or has separate IRQ mask and status registers.
  226. If you need it, however, a write to <filename>/dev/uioX</filename>
  227. will call the <function>irqcontrol()</function> function implemented
  228. by the driver. You have to write a 32-bit value that is usually either
  229. 0 or 1 to disable or enable interrupts. If a driver does not implement
  230. <function>irqcontrol()</function>, <function>write()</function> will
  231. return with <varname>-ENOSYS</varname>.
  232. </para>
  233. <para>
  234. To handle interrupts properly, your custom kernel module can
  235. provide its own interrupt handler. It will automatically be
  236. called by the built-in handler.
  237. </para>
  238. <para>
  239. For cards that don't generate interrupts but need to be
  240. polled, there is the possibility to set up a timer that
  241. triggers the interrupt handler at configurable time intervals.
  242. This interrupt simulation is done by calling
  243. <function>uio_event_notify()</function>
  244. from the timer's event handler.
  245. </para>
  246. <para>
  247. Each driver provides attributes that are used to read or write
  248. variables. These attributes are accessible through sysfs
  249. files. A custom kernel driver module can add its own
  250. attributes to the device owned by the uio driver, but not added
  251. to the UIO device itself at this time. This might change in the
  252. future if it would be found to be useful.
  253. </para>
  254. <para>
  255. The following standard attributes are provided by the UIO
  256. framework:
  257. </para>
  258. <itemizedlist>
  259. <listitem>
  260. <para>
  261. <filename>name</filename>: The name of your device. It is
  262. recommended to use the name of your kernel module for this.
  263. </para>
  264. </listitem>
  265. <listitem>
  266. <para>
  267. <filename>version</filename>: A version string defined by your
  268. driver. This allows the user space part of your driver to deal
  269. with different versions of the kernel module.
  270. </para>
  271. </listitem>
  272. <listitem>
  273. <para>
  274. <filename>event</filename>: The total number of interrupts
  275. handled by the driver since the last time the device node was
  276. read.
  277. </para>
  278. </listitem>
  279. </itemizedlist>
  280. <para>
  281. These attributes appear under the
  282. <filename>/sys/class/uio/uioX</filename> directory. Please
  283. note that this directory might be a symlink, and not a real
  284. directory. Any userspace code that accesses it must be able
  285. to handle this.
  286. </para>
  287. <para>
  288. Each UIO device can make one or more memory regions available for
  289. memory mapping. This is necessary because some industrial I/O cards
  290. require access to more than one PCI memory region in a driver.
  291. </para>
  292. <para>
  293. Each mapping has its own directory in sysfs, the first mapping
  294. appears as <filename>/sys/class/uio/uioX/maps/map0/</filename>.
  295. Subsequent mappings create directories <filename>map1/</filename>,
  296. <filename>map2/</filename>, and so on. These directories will only
  297. appear if the size of the mapping is not 0.
  298. </para>
  299. <para>
  300. Each <filename>mapX/</filename> directory contains four read-only files
  301. that show attributes of the memory:
  302. </para>
  303. <itemizedlist>
  304. <listitem>
  305. <para>
  306. <filename>name</filename>: A string identifier for this mapping. This
  307. is optional, the string can be empty. Drivers can set this to make it
  308. easier for userspace to find the correct mapping.
  309. </para>
  310. </listitem>
  311. <listitem>
  312. <para>
  313. <filename>addr</filename>: The address of memory that can be mapped.
  314. </para>
  315. </listitem>
  316. <listitem>
  317. <para>
  318. <filename>size</filename>: The size, in bytes, of the memory
  319. pointed to by addr.
  320. </para>
  321. </listitem>
  322. <listitem>
  323. <para>
  324. <filename>offset</filename>: The offset, in bytes, that has to be
  325. added to the pointer returned by <function>mmap()</function> to get
  326. to the actual device memory. This is important if the device's memory
  327. is not page aligned. Remember that pointers returned by
  328. <function>mmap()</function> are always page aligned, so it is good
  329. style to always add this offset.
  330. </para>
  331. </listitem>
  332. </itemizedlist>
  333. <para>
  334. From userspace, the different mappings are distinguished by adjusting
  335. the <varname>offset</varname> parameter of the
  336. <function>mmap()</function> call. To map the memory of mapping N, you
  337. have to use N times the page size as your offset:
  338. </para>
  339. <programlisting format="linespecific">
  340. offset = N * getpagesize();
  341. </programlisting>
  342. <para>
  343. Sometimes there is hardware with memory-like regions that can not be
  344. mapped with the technique described here, but there are still ways to
  345. access them from userspace. The most common example are x86 ioports.
  346. On x86 systems, userspace can access these ioports using
  347. <function>ioperm()</function>, <function>iopl()</function>,
  348. <function>inb()</function>, <function>outb()</function>, and similar
  349. functions.
  350. </para>
  351. <para>
  352. Since these ioport regions can not be mapped, they will not appear under
  353. <filename>/sys/class/uio/uioX/maps/</filename> like the normal memory
  354. described above. Without information about the port regions a hardware
  355. has to offer, it becomes difficult for the userspace part of the
  356. driver to find out which ports belong to which UIO device.
  357. </para>
  358. <para>
  359. To address this situation, the new directory
  360. <filename>/sys/class/uio/uioX/portio/</filename> was added. It only
  361. exists if the driver wants to pass information about one or more port
  362. regions to userspace. If that is the case, subdirectories named
  363. <filename>port0</filename>, <filename>port1</filename>, and so on,
  364. will appear underneath
  365. <filename>/sys/class/uio/uioX/portio/</filename>.
  366. </para>
  367. <para>
  368. Each <filename>portX/</filename> directory contains four read-only
  369. files that show name, start, size, and type of the port region:
  370. </para>
  371. <itemizedlist>
  372. <listitem>
  373. <para>
  374. <filename>name</filename>: A string identifier for this port region.
  375. The string is optional and can be empty. Drivers can set it to make it
  376. easier for userspace to find a certain port region.
  377. </para>
  378. </listitem>
  379. <listitem>
  380. <para>
  381. <filename>start</filename>: The first port of this region.
  382. </para>
  383. </listitem>
  384. <listitem>
  385. <para>
  386. <filename>size</filename>: The number of ports in this region.
  387. </para>
  388. </listitem>
  389. <listitem>
  390. <para>
  391. <filename>porttype</filename>: A string describing the type of port.
  392. </para>
  393. </listitem>
  394. </itemizedlist>
  395. </sect1>
  396. </chapter>
  397. <chapter id="custom_kernel_module" xreflabel="Writing your own kernel module">
  398. <?dbhtml filename="custom_kernel_module.html"?>
  399. <title>Writing your own kernel module</title>
  400. <para>
  401. Please have a look at <filename>uio_cif.c</filename> as an
  402. example. The following paragraphs explain the different
  403. sections of this file.
  404. </para>
  405. <sect1 id="uio_info">
  406. <title>struct uio_info</title>
  407. <para>
  408. This structure tells the framework the details of your driver,
  409. Some of the members are required, others are optional.
  410. </para>
  411. <itemizedlist>
  412. <listitem><para>
  413. <varname>const char *name</varname>: Required. The name of your driver as
  414. it will appear in sysfs. I recommend using the name of your module for this.
  415. </para></listitem>
  416. <listitem><para>
  417. <varname>const char *version</varname>: Required. This string appears in
  418. <filename>/sys/class/uio/uioX/version</filename>.
  419. </para></listitem>
  420. <listitem><para>
  421. <varname>struct uio_mem mem[ MAX_UIO_MAPS ]</varname>: Required if you
  422. have memory that can be mapped with <function>mmap()</function>. For each
  423. mapping you need to fill one of the <varname>uio_mem</varname> structures.
  424. See the description below for details.
  425. </para></listitem>
  426. <listitem><para>
  427. <varname>struct uio_port port[ MAX_UIO_PORTS_REGIONS ]</varname>: Required
  428. if you want to pass information about ioports to userspace. For each port
  429. region you need to fill one of the <varname>uio_port</varname> structures.
  430. See the description below for details.
  431. </para></listitem>
  432. <listitem><para>
  433. <varname>long irq</varname>: Required. If your hardware generates an
  434. interrupt, it's your modules task to determine the irq number during
  435. initialization. If you don't have a hardware generated interrupt but
  436. want to trigger the interrupt handler in some other way, set
  437. <varname>irq</varname> to <varname>UIO_IRQ_CUSTOM</varname>.
  438. If you had no interrupt at all, you could set
  439. <varname>irq</varname> to <varname>UIO_IRQ_NONE</varname>, though this
  440. rarely makes sense.
  441. </para></listitem>
  442. <listitem><para>
  443. <varname>unsigned long irq_flags</varname>: Required if you've set
  444. <varname>irq</varname> to a hardware interrupt number. The flags given
  445. here will be used in the call to <function>request_irq()</function>.
  446. </para></listitem>
  447. <listitem><para>
  448. <varname>int (*mmap)(struct uio_info *info, struct vm_area_struct
  449. *vma)</varname>: Optional. If you need a special
  450. <function>mmap()</function> function, you can set it here. If this
  451. pointer is not NULL, your <function>mmap()</function> will be called
  452. instead of the built-in one.
  453. </para></listitem>
  454. <listitem><para>
  455. <varname>int (*open)(struct uio_info *info, struct inode *inode)
  456. </varname>: Optional. You might want to have your own
  457. <function>open()</function>, e.g. to enable interrupts only when your
  458. device is actually used.
  459. </para></listitem>
  460. <listitem><para>
  461. <varname>int (*release)(struct uio_info *info, struct inode *inode)
  462. </varname>: Optional. If you define your own
  463. <function>open()</function>, you will probably also want a custom
  464. <function>release()</function> function.
  465. </para></listitem>
  466. <listitem><para>
  467. <varname>int (*irqcontrol)(struct uio_info *info, s32 irq_on)
  468. </varname>: Optional. If you need to be able to enable or disable
  469. interrupts from userspace by writing to <filename>/dev/uioX</filename>,
  470. you can implement this function. The parameter <varname>irq_on</varname>
  471. will be 0 to disable interrupts and 1 to enable them.
  472. </para></listitem>
  473. </itemizedlist>
  474. <para>
  475. Usually, your device will have one or more memory regions that can be mapped
  476. to user space. For each region, you have to set up a
  477. <varname>struct uio_mem</varname> in the <varname>mem[]</varname> array.
  478. Here's a description of the fields of <varname>struct uio_mem</varname>:
  479. </para>
  480. <itemizedlist>
  481. <listitem><para>
  482. <varname>const char *name</varname>: Optional. Set this to help identify
  483. the memory region, it will show up in the corresponding sysfs node.
  484. </para></listitem>
  485. <listitem><para>
  486. <varname>int memtype</varname>: Required if the mapping is used. Set this to
  487. <varname>UIO_MEM_PHYS</varname> if you you have physical memory on your
  488. card to be mapped. Use <varname>UIO_MEM_LOGICAL</varname> for logical
  489. memory (e.g. allocated with <function>kmalloc()</function>). There's also
  490. <varname>UIO_MEM_VIRTUAL</varname> for virtual memory.
  491. </para></listitem>
  492. <listitem><para>
  493. <varname>phys_addr_t addr</varname>: Required if the mapping is used.
  494. Fill in the address of your memory block. This address is the one that
  495. appears in sysfs.
  496. </para></listitem>
  497. <listitem><para>
  498. <varname>resource_size_t size</varname>: Fill in the size of the
  499. memory block that <varname>addr</varname> points to. If <varname>size</varname>
  500. is zero, the mapping is considered unused. Note that you
  501. <emphasis>must</emphasis> initialize <varname>size</varname> with zero for
  502. all unused mappings.
  503. </para></listitem>
  504. <listitem><para>
  505. <varname>void *internal_addr</varname>: If you have to access this memory
  506. region from within your kernel module, you will want to map it internally by
  507. using something like <function>ioremap()</function>. Addresses
  508. returned by this function cannot be mapped to user space, so you must not
  509. store it in <varname>addr</varname>. Use <varname>internal_addr</varname>
  510. instead to remember such an address.
  511. </para></listitem>
  512. </itemizedlist>
  513. <para>
  514. Please do not touch the <varname>map</varname> element of
  515. <varname>struct uio_mem</varname>! It is used by the UIO framework
  516. to set up sysfs files for this mapping. Simply leave it alone.
  517. </para>
  518. <para>
  519. Sometimes, your device can have one or more port regions which can not be
  520. mapped to userspace. But if there are other possibilities for userspace to
  521. access these ports, it makes sense to make information about the ports
  522. available in sysfs. For each region, you have to set up a
  523. <varname>struct uio_port</varname> in the <varname>port[]</varname> array.
  524. Here's a description of the fields of <varname>struct uio_port</varname>:
  525. </para>
  526. <itemizedlist>
  527. <listitem><para>
  528. <varname>char *porttype</varname>: Required. Set this to one of the predefined
  529. constants. Use <varname>UIO_PORT_X86</varname> for the ioports found in x86
  530. architectures.
  531. </para></listitem>
  532. <listitem><para>
  533. <varname>unsigned long start</varname>: Required if the port region is used.
  534. Fill in the number of the first port of this region.
  535. </para></listitem>
  536. <listitem><para>
  537. <varname>unsigned long size</varname>: Fill in the number of ports in this
  538. region. If <varname>size</varname> is zero, the region is considered unused.
  539. Note that you <emphasis>must</emphasis> initialize <varname>size</varname>
  540. with zero for all unused regions.
  541. </para></listitem>
  542. </itemizedlist>
  543. <para>
  544. Please do not touch the <varname>portio</varname> element of
  545. <varname>struct uio_port</varname>! It is used internally by the UIO
  546. framework to set up sysfs files for this region. Simply leave it alone.
  547. </para>
  548. </sect1>
  549. <sect1 id="adding_irq_handler">
  550. <title>Adding an interrupt handler</title>
  551. <para>
  552. What you need to do in your interrupt handler depends on your
  553. hardware and on how you want to handle it. You should try to
  554. keep the amount of code in your kernel interrupt handler low.
  555. If your hardware requires no action that you
  556. <emphasis>have</emphasis> to perform after each interrupt,
  557. then your handler can be empty.</para> <para>If, on the other
  558. hand, your hardware <emphasis>needs</emphasis> some action to
  559. be performed after each interrupt, then you
  560. <emphasis>must</emphasis> do it in your kernel module. Note
  561. that you cannot rely on the userspace part of your driver. Your
  562. userspace program can terminate at any time, possibly leaving
  563. your hardware in a state where proper interrupt handling is
  564. still required.
  565. </para>
  566. <para>
  567. There might also be applications where you want to read data
  568. from your hardware at each interrupt and buffer it in a piece
  569. of kernel memory you've allocated for that purpose. With this
  570. technique you could avoid loss of data if your userspace
  571. program misses an interrupt.
  572. </para>
  573. <para>
  574. A note on shared interrupts: Your driver should support
  575. interrupt sharing whenever this is possible. It is possible if
  576. and only if your driver can detect whether your hardware has
  577. triggered the interrupt or not. This is usually done by looking
  578. at an interrupt status register. If your driver sees that the
  579. IRQ bit is actually set, it will perform its actions, and the
  580. handler returns IRQ_HANDLED. If the driver detects that it was
  581. not your hardware that caused the interrupt, it will do nothing
  582. and return IRQ_NONE, allowing the kernel to call the next
  583. possible interrupt handler.
  584. </para>
  585. <para>
  586. If you decide not to support shared interrupts, your card
  587. won't work in computers with no free interrupts. As this
  588. frequently happens on the PC platform, you can save yourself a
  589. lot of trouble by supporting interrupt sharing.
  590. </para>
  591. </sect1>
  592. <sect1 id="using_uio_pdrv">
  593. <title>Using uio_pdrv for platform devices</title>
  594. <para>
  595. In many cases, UIO drivers for platform devices can be handled in a
  596. generic way. In the same place where you define your
  597. <varname>struct platform_device</varname>, you simply also implement
  598. your interrupt handler and fill your
  599. <varname>struct uio_info</varname>. A pointer to this
  600. <varname>struct uio_info</varname> is then used as
  601. <varname>platform_data</varname> for your platform device.
  602. </para>
  603. <para>
  604. You also need to set up an array of <varname>struct resource</varname>
  605. containing addresses and sizes of your memory mappings. This
  606. information is passed to the driver using the
  607. <varname>.resource</varname> and <varname>.num_resources</varname>
  608. elements of <varname>struct platform_device</varname>.
  609. </para>
  610. <para>
  611. You now have to set the <varname>.name</varname> element of
  612. <varname>struct platform_device</varname> to
  613. <varname>"uio_pdrv"</varname> to use the generic UIO platform device
  614. driver. This driver will fill the <varname>mem[]</varname> array
  615. according to the resources given, and register the device.
  616. </para>
  617. <para>
  618. The advantage of this approach is that you only have to edit a file
  619. you need to edit anyway. You do not have to create an extra driver.
  620. </para>
  621. </sect1>
  622. <sect1 id="using_uio_pdrv_genirq">
  623. <title>Using uio_pdrv_genirq for platform devices</title>
  624. <para>
  625. Especially in embedded devices, you frequently find chips where the
  626. irq pin is tied to its own dedicated interrupt line. In such cases,
  627. where you can be really sure the interrupt is not shared, we can take
  628. the concept of <varname>uio_pdrv</varname> one step further and use a
  629. generic interrupt handler. That's what
  630. <varname>uio_pdrv_genirq</varname> does.
  631. </para>
  632. <para>
  633. The setup for this driver is the same as described above for
  634. <varname>uio_pdrv</varname>, except that you do not implement an
  635. interrupt handler. The <varname>.handler</varname> element of
  636. <varname>struct uio_info</varname> must remain
  637. <varname>NULL</varname>. The <varname>.irq_flags</varname> element
  638. must not contain <varname>IRQF_SHARED</varname>.
  639. </para>
  640. <para>
  641. You will set the <varname>.name</varname> element of
  642. <varname>struct platform_device</varname> to
  643. <varname>"uio_pdrv_genirq"</varname> to use this driver.
  644. </para>
  645. <para>
  646. The generic interrupt handler of <varname>uio_pdrv_genirq</varname>
  647. will simply disable the interrupt line using
  648. <function>disable_irq_nosync()</function>. After doing its work,
  649. userspace can reenable the interrupt by writing 0x00000001 to the UIO
  650. device file. The driver already implements an
  651. <function>irq_control()</function> to make this possible, you must not
  652. implement your own.
  653. </para>
  654. <para>
  655. Using <varname>uio_pdrv_genirq</varname> not only saves a few lines of
  656. interrupt handler code. You also do not need to know anything about
  657. the chip's internal registers to create the kernel part of the driver.
  658. All you need to know is the irq number of the pin the chip is
  659. connected to.
  660. </para>
  661. </sect1>
  662. <sect1 id="using-uio_dmem_genirq">
  663. <title>Using uio_dmem_genirq for platform devices</title>
  664. <para>
  665. In addition to statically allocated memory ranges, they may also be
  666. a desire to use dynamically allocated regions in a user space driver.
  667. In particular, being able to access memory made available through the
  668. dma-mapping API, may be particularly useful. The
  669. <varname>uio_dmem_genirq</varname> driver provides a way to accomplish
  670. this.
  671. </para>
  672. <para>
  673. This driver is used in a similar manner to the
  674. <varname>"uio_pdrv_genirq"</varname> driver with respect to interrupt
  675. configuration and handling.
  676. </para>
  677. <para>
  678. Set the <varname>.name</varname> element of
  679. <varname>struct platform_device</varname> to
  680. <varname>"uio_dmem_genirq"</varname> to use this driver.
  681. </para>
  682. <para>
  683. When using this driver, fill in the <varname>.platform_data</varname>
  684. element of <varname>struct platform_device</varname>, which is of type
  685. <varname>struct uio_dmem_genirq_pdata</varname> and which contains the
  686. following elements:
  687. </para>
  688. <itemizedlist>
  689. <listitem><para><varname>struct uio_info uioinfo</varname>: The same
  690. structure used as the <varname>uio_pdrv_genirq</varname> platform
  691. data</para></listitem>
  692. <listitem><para><varname>unsigned int *dynamic_region_sizes</varname>:
  693. Pointer to list of sizes of dynamic memory regions to be mapped into
  694. user space.
  695. </para></listitem>
  696. <listitem><para><varname>unsigned int num_dynamic_regions</varname>:
  697. Number of elements in <varname>dynamic_region_sizes</varname> array.
  698. </para></listitem>
  699. </itemizedlist>
  700. <para>
  701. The dynamic regions defined in the platform data will be appended to
  702. the <varname> mem[] </varname> array after the platform device
  703. resources, which implies that the total number of static and dynamic
  704. memory regions cannot exceed <varname>MAX_UIO_MAPS</varname>.
  705. </para>
  706. <para>
  707. The dynamic memory regions will be allocated when the UIO device file,
  708. <varname>/dev/uioX</varname> is opened.
  709. Similar to static memory resources, the memory region information for
  710. dynamic regions is then visible via sysfs at
  711. <varname>/sys/class/uio/uioX/maps/mapY/*</varname>.
  712. The dynamic memory regions will be freed when the UIO device file is
  713. closed. When no processes are holding the device file open, the address
  714. returned to userspace is ~0.
  715. </para>
  716. </sect1>
  717. </chapter>
  718. <chapter id="userspace_driver" xreflabel="Writing a driver in user space">
  719. <?dbhtml filename="userspace_driver.html"?>
  720. <title>Writing a driver in userspace</title>
  721. <para>
  722. Once you have a working kernel module for your hardware, you can
  723. write the userspace part of your driver. You don't need any special
  724. libraries, your driver can be written in any reasonable language,
  725. you can use floating point numbers and so on. In short, you can
  726. use all the tools and libraries you'd normally use for writing a
  727. userspace application.
  728. </para>
  729. <sect1 id="getting_uio_information">
  730. <title>Getting information about your UIO device</title>
  731. <para>
  732. Information about all UIO devices is available in sysfs. The
  733. first thing you should do in your driver is check
  734. <varname>name</varname> and <varname>version</varname> to
  735. make sure your talking to the right device and that its kernel
  736. driver has the version you expect.
  737. </para>
  738. <para>
  739. You should also make sure that the memory mapping you need
  740. exists and has the size you expect.
  741. </para>
  742. <para>
  743. There is a tool called <varname>lsuio</varname> that lists
  744. UIO devices and their attributes. It is available here:
  745. </para>
  746. <para>
  747. <ulink url="http://www.osadl.org/projects/downloads/UIO/user/">
  748. http://www.osadl.org/projects/downloads/UIO/user/</ulink>
  749. </para>
  750. <para>
  751. With <varname>lsuio</varname> you can quickly check if your
  752. kernel module is loaded and which attributes it exports.
  753. Have a look at the manpage for details.
  754. </para>
  755. <para>
  756. The source code of <varname>lsuio</varname> can serve as an
  757. example for getting information about an UIO device.
  758. The file <filename>uio_helper.c</filename> contains a lot of
  759. functions you could use in your userspace driver code.
  760. </para>
  761. </sect1>
  762. <sect1 id="mmap_device_memory">
  763. <title>mmap() device memory</title>
  764. <para>
  765. After you made sure you've got the right device with the
  766. memory mappings you need, all you have to do is to call
  767. <function>mmap()</function> to map the device's memory
  768. to userspace.
  769. </para>
  770. <para>
  771. The parameter <varname>offset</varname> of the
  772. <function>mmap()</function> call has a special meaning
  773. for UIO devices: It is used to select which mapping of
  774. your device you want to map. To map the memory of
  775. mapping N, you have to use N times the page size as
  776. your offset:
  777. </para>
  778. <programlisting format="linespecific">
  779. offset = N * getpagesize();
  780. </programlisting>
  781. <para>
  782. N starts from zero, so if you've got only one memory
  783. range to map, set <varname>offset = 0</varname>.
  784. A drawback of this technique is that memory is always
  785. mapped beginning with its start address.
  786. </para>
  787. </sect1>
  788. <sect1 id="wait_for_interrupts">
  789. <title>Waiting for interrupts</title>
  790. <para>
  791. After you successfully mapped your devices memory, you
  792. can access it like an ordinary array. Usually, you will
  793. perform some initialization. After that, your hardware
  794. starts working and will generate an interrupt as soon
  795. as it's finished, has some data available, or needs your
  796. attention because an error occurred.
  797. </para>
  798. <para>
  799. <filename>/dev/uioX</filename> is a read-only file. A
  800. <function>read()</function> will always block until an
  801. interrupt occurs. There is only one legal value for the
  802. <varname>count</varname> parameter of
  803. <function>read()</function>, and that is the size of a
  804. signed 32 bit integer (4). Any other value for
  805. <varname>count</varname> causes <function>read()</function>
  806. to fail. The signed 32 bit integer read is the interrupt
  807. count of your device. If the value is one more than the value
  808. you read the last time, everything is OK. If the difference
  809. is greater than one, you missed interrupts.
  810. </para>
  811. <para>
  812. You can also use <function>select()</function> on
  813. <filename>/dev/uioX</filename>.
  814. </para>
  815. </sect1>
  816. </chapter>
  817. <chapter id="uio_pci_generic" xreflabel="Using Generic driver for PCI cards">
  818. <?dbhtml filename="uio_pci_generic.html"?>
  819. <title>Generic PCI UIO driver</title>
  820. <para>
  821. The generic driver is a kernel module named uio_pci_generic.
  822. It can work with any device compliant to PCI 2.3 (circa 2002) and
  823. any compliant PCI Express device. Using this, you only need to
  824. write the userspace driver, removing the need to write
  825. a hardware-specific kernel module.
  826. </para>
  827. <sect1 id="uio_pci_generic_binding">
  828. <title>Making the driver recognize the device</title>
  829. <para>
  830. Since the driver does not declare any device ids, it will not get loaded
  831. automatically and will not automatically bind to any devices, you must load it
  832. and allocate id to the driver yourself. For example:
  833. <programlisting>
  834. modprobe uio_pci_generic
  835. echo &quot;8086 10f5&quot; &gt; /sys/bus/pci/drivers/uio_pci_generic/new_id
  836. </programlisting>
  837. </para>
  838. <para>
  839. If there already is a hardware specific kernel driver for your device, the
  840. generic driver still won't bind to it, in this case if you want to use the
  841. generic driver (why would you?) you'll have to manually unbind the hardware
  842. specific driver and bind the generic driver, like this:
  843. <programlisting>
  844. echo -n 0000:00:19.0 &gt; /sys/bus/pci/drivers/e1000e/unbind
  845. echo -n 0000:00:19.0 &gt; /sys/bus/pci/drivers/uio_pci_generic/bind
  846. </programlisting>
  847. </para>
  848. <para>
  849. You can verify that the device has been bound to the driver
  850. by looking for it in sysfs, for example like the following:
  851. <programlisting>
  852. ls -l /sys/bus/pci/devices/0000:00:19.0/driver
  853. </programlisting>
  854. Which if successful should print
  855. <programlisting>
  856. .../0000:00:19.0/driver -&gt; ../../../bus/pci/drivers/uio_pci_generic
  857. </programlisting>
  858. Note that the generic driver will not bind to old PCI 2.2 devices.
  859. If binding the device failed, run the following command:
  860. <programlisting>
  861. dmesg
  862. </programlisting>
  863. and look in the output for failure reasons
  864. </para>
  865. </sect1>
  866. <sect1 id="uio_pci_generic_internals">
  867. <title>Things to know about uio_pci_generic</title>
  868. <para>
  869. Interrupts are handled using the Interrupt Disable bit in the PCI command
  870. register and Interrupt Status bit in the PCI status register. All devices
  871. compliant to PCI 2.3 (circa 2002) and all compliant PCI Express devices should
  872. support these bits. uio_pci_generic detects this support, and won't bind to
  873. devices which do not support the Interrupt Disable Bit in the command register.
  874. </para>
  875. <para>
  876. On each interrupt, uio_pci_generic sets the Interrupt Disable bit.
  877. This prevents the device from generating further interrupts
  878. until the bit is cleared. The userspace driver should clear this
  879. bit before blocking and waiting for more interrupts.
  880. </para>
  881. </sect1>
  882. <sect1 id="uio_pci_generic_userspace">
  883. <title>Writing userspace driver using uio_pci_generic</title>
  884. <para>
  885. Userspace driver can use pci sysfs interface, or the
  886. libpci libray that wraps it, to talk to the device and to
  887. re-enable interrupts by writing to the command register.
  888. </para>
  889. </sect1>
  890. <sect1 id="uio_pci_generic_example">
  891. <title>Example code using uio_pci_generic</title>
  892. <para>
  893. Here is some sample userspace driver code using uio_pci_generic:
  894. <programlisting>
  895. #include &lt;stdlib.h&gt;
  896. #include &lt;stdio.h&gt;
  897. #include &lt;unistd.h&gt;
  898. #include &lt;sys/types.h&gt;
  899. #include &lt;sys/stat.h&gt;
  900. #include &lt;fcntl.h&gt;
  901. #include &lt;errno.h&gt;
  902. int main()
  903. {
  904. int uiofd;
  905. int configfd;
  906. int err;
  907. int i;
  908. unsigned icount;
  909. unsigned char command_high;
  910. uiofd = open(&quot;/dev/uio0&quot;, O_RDONLY);
  911. if (uiofd &lt; 0) {
  912. perror(&quot;uio open:&quot;);
  913. return errno;
  914. }
  915. configfd = open(&quot;/sys/class/uio/uio0/device/config&quot;, O_RDWR);
  916. if (configfd &lt; 0) {
  917. perror(&quot;config open:&quot;);
  918. return errno;
  919. }
  920. /* Read and cache command value */
  921. err = pread(configfd, &amp;command_high, 1, 5);
  922. if (err != 1) {
  923. perror(&quot;command config read:&quot;);
  924. return errno;
  925. }
  926. command_high &amp;= ~0x4;
  927. for(i = 0;; ++i) {
  928. /* Print out a message, for debugging. */
  929. if (i == 0)
  930. fprintf(stderr, &quot;Started uio test driver.\n&quot;);
  931. else
  932. fprintf(stderr, &quot;Interrupts: %d\n&quot;, icount);
  933. /****************************************/
  934. /* Here we got an interrupt from the
  935. device. Do something to it. */
  936. /****************************************/
  937. /* Re-enable interrupts. */
  938. err = pwrite(configfd, &amp;command_high, 1, 5);
  939. if (err != 1) {
  940. perror(&quot;config write:&quot;);
  941. break;
  942. }
  943. /* Wait for next interrupt. */
  944. err = read(uiofd, &amp;icount, 4);
  945. if (err != 4) {
  946. perror(&quot;uio read:&quot;);
  947. break;
  948. }
  949. }
  950. return errno;
  951. }
  952. </programlisting>
  953. </para>
  954. </sect1>
  955. </chapter>
  956. <appendix id="app1">
  957. <title>Further information</title>
  958. <itemizedlist>
  959. <listitem><para>
  960. <ulink url="http://www.osadl.org">
  961. OSADL homepage.</ulink>
  962. </para></listitem>
  963. <listitem><para>
  964. <ulink url="http://www.linutronix.de">
  965. Linutronix homepage.</ulink>
  966. </para></listitem>
  967. </itemizedlist>
  968. </appendix>
  969. </book>