uio-howto.rst 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. =======================
  2. The Userspace I/O HOWTO
  3. =======================
  4. :Author: Hans-Jürgen Koch Linux developer, Linutronix
  5. :Date: 2006-12-11
  6. About this document
  7. ===================
  8. Translations
  9. ------------
  10. If you know of any translations for this document, or you are interested
  11. in translating it, please email me hjk@hansjkoch.de.
  12. Preface
  13. -------
  14. For many types of devices, creating a Linux kernel driver is overkill.
  15. All that is really needed is some way to handle an interrupt and provide
  16. access to the memory space of the device. The logic of controlling the
  17. device does not necessarily have to be within the kernel, as the device
  18. does not need to take advantage of any of other resources that the
  19. kernel provides. One such common class of devices that are like this are
  20. for industrial I/O cards.
  21. To address this situation, the userspace I/O system (UIO) was designed.
  22. For typical industrial I/O cards, only a very small kernel module is
  23. needed. The main part of the driver will run in user space. This
  24. simplifies development and reduces the risk of serious bugs within a
  25. kernel module.
  26. Please note that UIO is not an universal driver interface. Devices that
  27. are already handled well by other kernel subsystems (like networking or
  28. serial or USB) are no candidates for an UIO driver. Hardware that is
  29. ideally suited for an UIO driver fulfills all of the following:
  30. - The device has memory that can be mapped. The device can be
  31. controlled completely by writing to this memory.
  32. - The device usually generates interrupts.
  33. - The device does not fit into one of the standard kernel subsystems.
  34. Acknowledgments
  35. ---------------
  36. I'd like to thank Thomas Gleixner and Benedikt Spranger of Linutronix,
  37. who have not only written most of the UIO code, but also helped greatly
  38. writing this HOWTO by giving me all kinds of background information.
  39. Feedback
  40. --------
  41. Find something wrong with this document? (Or perhaps something right?) I
  42. would love to hear from you. Please email me at hjk@hansjkoch.de.
  43. About UIO
  44. =========
  45. If you use UIO for your card's driver, here's what you get:
  46. - only one small kernel module to write and maintain.
  47. - develop the main part of your driver in user space, with all the
  48. tools and libraries you're used to.
  49. - bugs in your driver won't crash the kernel.
  50. - updates of your driver can take place without recompiling the kernel.
  51. How UIO works
  52. -------------
  53. Each UIO device is accessed through a device file and several sysfs
  54. attribute files. The device file will be called ``/dev/uio0`` for the
  55. first device, and ``/dev/uio1``, ``/dev/uio2`` and so on for subsequent
  56. devices.
  57. ``/dev/uioX`` is used to access the address space of the card. Just use
  58. :c:func:`mmap()` to access registers or RAM locations of your card.
  59. Interrupts are handled by reading from ``/dev/uioX``. A blocking
  60. :c:func:`read()` from ``/dev/uioX`` will return as soon as an
  61. interrupt occurs. You can also use :c:func:`select()` on
  62. ``/dev/uioX`` to wait for an interrupt. The integer value read from
  63. ``/dev/uioX`` represents the total interrupt count. You can use this
  64. number to figure out if you missed some interrupts.
  65. For some hardware that has more than one interrupt source internally,
  66. but not separate IRQ mask and status registers, there might be
  67. situations where userspace cannot determine what the interrupt source
  68. was if the kernel handler disables them by writing to the chip's IRQ
  69. register. In such a case, the kernel has to disable the IRQ completely
  70. to leave the chip's register untouched. Now the userspace part can
  71. determine the cause of the interrupt, but it cannot re-enable
  72. interrupts. Another cornercase is chips where re-enabling interrupts is
  73. a read-modify-write operation to a combined IRQ status/acknowledge
  74. register. This would be racy if a new interrupt occurred simultaneously.
  75. To address these problems, UIO also implements a write() function. It is
  76. normally not used and can be ignored for hardware that has only a single
  77. interrupt source or has separate IRQ mask and status registers. If you
  78. need it, however, a write to ``/dev/uioX`` will call the
  79. :c:func:`irqcontrol()` function implemented by the driver. You have
  80. to write a 32-bit value that is usually either 0 or 1 to disable or
  81. enable interrupts. If a driver does not implement
  82. :c:func:`irqcontrol()`, :c:func:`write()` will return with
  83. ``-ENOSYS``.
  84. To handle interrupts properly, your custom kernel module can provide its
  85. own interrupt handler. It will automatically be called by the built-in
  86. handler.
  87. For cards that don't generate interrupts but need to be polled, there is
  88. the possibility to set up a timer that triggers the interrupt handler at
  89. configurable time intervals. This interrupt simulation is done by
  90. calling :c:func:`uio_event_notify()` from the timer's event
  91. handler.
  92. Each driver provides attributes that are used to read or write
  93. variables. These attributes are accessible through sysfs files. A custom
  94. kernel driver module can add its own attributes to the device owned by
  95. the uio driver, but not added to the UIO device itself at this time.
  96. This might change in the future if it would be found to be useful.
  97. The following standard attributes are provided by the UIO framework:
  98. - ``name``: The name of your device. It is recommended to use the name
  99. of your kernel module for this.
  100. - ``version``: A version string defined by your driver. This allows the
  101. user space part of your driver to deal with different versions of the
  102. kernel module.
  103. - ``event``: The total number of interrupts handled by the driver since
  104. the last time the device node was read.
  105. These attributes appear under the ``/sys/class/uio/uioX`` directory.
  106. Please note that this directory might be a symlink, and not a real
  107. directory. Any userspace code that accesses it must be able to handle
  108. this.
  109. Each UIO device can make one or more memory regions available for memory
  110. mapping. This is necessary because some industrial I/O cards require
  111. access to more than one PCI memory region in a driver.
  112. Each mapping has its own directory in sysfs, the first mapping appears
  113. as ``/sys/class/uio/uioX/maps/map0/``. Subsequent mappings create
  114. directories ``map1/``, ``map2/``, and so on. These directories will only
  115. appear if the size of the mapping is not 0.
  116. Each ``mapX/`` directory contains four read-only files that show
  117. attributes of the memory:
  118. - ``name``: A string identifier for this mapping. This is optional, the
  119. string can be empty. Drivers can set this to make it easier for
  120. userspace to find the correct mapping.
  121. - ``addr``: The address of memory that can be mapped.
  122. - ``size``: The size, in bytes, of the memory pointed to by addr.
  123. - ``offset``: The offset, in bytes, that has to be added to the pointer
  124. returned by :c:func:`mmap()` to get to the actual device memory.
  125. This is important if the device's memory is not page aligned.
  126. Remember that pointers returned by :c:func:`mmap()` are always
  127. page aligned, so it is good style to always add this offset.
  128. From userspace, the different mappings are distinguished by adjusting
  129. the ``offset`` parameter of the :c:func:`mmap()` call. To map the
  130. memory of mapping N, you have to use N times the page size as your
  131. offset::
  132. offset = N * getpagesize();
  133. Sometimes there is hardware with memory-like regions that can not be
  134. mapped with the technique described here, but there are still ways to
  135. access them from userspace. The most common example are x86 ioports. On
  136. x86 systems, userspace can access these ioports using
  137. :c:func:`ioperm()`, :c:func:`iopl()`, :c:func:`inb()`,
  138. :c:func:`outb()`, and similar functions.
  139. Since these ioport regions can not be mapped, they will not appear under
  140. ``/sys/class/uio/uioX/maps/`` like the normal memory described above.
  141. Without information about the port regions a hardware has to offer, it
  142. becomes difficult for the userspace part of the driver to find out which
  143. ports belong to which UIO device.
  144. To address this situation, the new directory
  145. ``/sys/class/uio/uioX/portio/`` was added. It only exists if the driver
  146. wants to pass information about one or more port regions to userspace.
  147. If that is the case, subdirectories named ``port0``, ``port1``, and so
  148. on, will appear underneath ``/sys/class/uio/uioX/portio/``.
  149. Each ``portX/`` directory contains four read-only files that show name,
  150. start, size, and type of the port region:
  151. - ``name``: A string identifier for this port region. The string is
  152. optional and can be empty. Drivers can set it to make it easier for
  153. userspace to find a certain port region.
  154. - ``start``: The first port of this region.
  155. - ``size``: The number of ports in this region.
  156. - ``porttype``: A string describing the type of port.
  157. Writing your own kernel module
  158. ==============================
  159. Please have a look at ``uio_cif.c`` as an example. The following
  160. paragraphs explain the different sections of this file.
  161. struct uio_info
  162. ---------------
  163. This structure tells the framework the details of your driver, Some of
  164. the members are required, others are optional.
  165. - ``const char *name``: Required. The name of your driver as it will
  166. appear in sysfs. I recommend using the name of your module for this.
  167. - ``const char *version``: Required. This string appears in
  168. ``/sys/class/uio/uioX/version``.
  169. - ``struct uio_mem mem[ MAX_UIO_MAPS ]``: Required if you have memory
  170. that can be mapped with :c:func:`mmap()`. For each mapping you
  171. need to fill one of the ``uio_mem`` structures. See the description
  172. below for details.
  173. - ``struct uio_port port[ MAX_UIO_PORTS_REGIONS ]``: Required if you
  174. want to pass information about ioports to userspace. For each port
  175. region you need to fill one of the ``uio_port`` structures. See the
  176. description below for details.
  177. - ``long irq``: Required. If your hardware generates an interrupt, it's
  178. your modules task to determine the irq number during initialization.
  179. If you don't have a hardware generated interrupt but want to trigger
  180. the interrupt handler in some other way, set ``irq`` to
  181. ``UIO_IRQ_CUSTOM``. If you had no interrupt at all, you could set
  182. ``irq`` to ``UIO_IRQ_NONE``, though this rarely makes sense.
  183. - ``unsigned long irq_flags``: Required if you've set ``irq`` to a
  184. hardware interrupt number. The flags given here will be used in the
  185. call to :c:func:`request_irq()`.
  186. - ``int (*mmap)(struct uio_info *info, struct vm_area_struct *vma)``:
  187. Optional. If you need a special :c:func:`mmap()`
  188. function, you can set it here. If this pointer is not NULL, your
  189. :c:func:`mmap()` will be called instead of the built-in one.
  190. - ``int (*open)(struct uio_info *info, struct inode *inode)``:
  191. Optional. You might want to have your own :c:func:`open()`,
  192. e.g. to enable interrupts only when your device is actually used.
  193. - ``int (*release)(struct uio_info *info, struct inode *inode)``:
  194. Optional. If you define your own :c:func:`open()`, you will
  195. probably also want a custom :c:func:`release()` function.
  196. - ``int (*irqcontrol)(struct uio_info *info, s32 irq_on)``:
  197. Optional. If you need to be able to enable or disable interrupts
  198. from userspace by writing to ``/dev/uioX``, you can implement this
  199. function. The parameter ``irq_on`` will be 0 to disable interrupts
  200. and 1 to enable them.
  201. Usually, your device will have one or more memory regions that can be
  202. mapped to user space. For each region, you have to set up a
  203. ``struct uio_mem`` in the ``mem[]`` array. Here's a description of the
  204. fields of ``struct uio_mem``:
  205. - ``const char *name``: Optional. Set this to help identify the memory
  206. region, it will show up in the corresponding sysfs node.
  207. - ``int memtype``: Required if the mapping is used. Set this to
  208. ``UIO_MEM_PHYS`` if you have physical memory on your card to be
  209. mapped. Use ``UIO_MEM_LOGICAL`` for logical memory (e.g. allocated
  210. with :c:func:`__get_free_pages()` but not kmalloc()). There's also
  211. ``UIO_MEM_VIRTUAL`` for virtual memory.
  212. - ``phys_addr_t addr``: Required if the mapping is used. Fill in the
  213. address of your memory block. This address is the one that appears in
  214. sysfs.
  215. - ``resource_size_t size``: Fill in the size of the memory block that
  216. ``addr`` points to. If ``size`` is zero, the mapping is considered
  217. unused. Note that you *must* initialize ``size`` with zero for all
  218. unused mappings.
  219. - ``void *internal_addr``: If you have to access this memory region
  220. from within your kernel module, you will want to map it internally by
  221. using something like :c:func:`ioremap()`. Addresses returned by
  222. this function cannot be mapped to user space, so you must not store
  223. it in ``addr``. Use ``internal_addr`` instead to remember such an
  224. address.
  225. Please do not touch the ``map`` element of ``struct uio_mem``! It is
  226. used by the UIO framework to set up sysfs files for this mapping. Simply
  227. leave it alone.
  228. Sometimes, your device can have one or more port regions which can not
  229. be mapped to userspace. But if there are other possibilities for
  230. userspace to access these ports, it makes sense to make information
  231. about the ports available in sysfs. For each region, you have to set up
  232. a ``struct uio_port`` in the ``port[]`` array. Here's a description of
  233. the fields of ``struct uio_port``:
  234. - ``char *porttype``: Required. Set this to one of the predefined
  235. constants. Use ``UIO_PORT_X86`` for the ioports found in x86
  236. architectures.
  237. - ``unsigned long start``: Required if the port region is used. Fill in
  238. the number of the first port of this region.
  239. - ``unsigned long size``: Fill in the number of ports in this region.
  240. If ``size`` is zero, the region is considered unused. Note that you
  241. *must* initialize ``size`` with zero for all unused regions.
  242. Please do not touch the ``portio`` element of ``struct uio_port``! It is
  243. used internally by the UIO framework to set up sysfs files for this
  244. region. Simply leave it alone.
  245. Adding an interrupt handler
  246. ---------------------------
  247. What you need to do in your interrupt handler depends on your hardware
  248. and on how you want to handle it. You should try to keep the amount of
  249. code in your kernel interrupt handler low. If your hardware requires no
  250. action that you *have* to perform after each interrupt, then your
  251. handler can be empty.
  252. If, on the other hand, your hardware *needs* some action to be performed
  253. after each interrupt, then you *must* do it in your kernel module. Note
  254. that you cannot rely on the userspace part of your driver. Your
  255. userspace program can terminate at any time, possibly leaving your
  256. hardware in a state where proper interrupt handling is still required.
  257. There might also be applications where you want to read data from your
  258. hardware at each interrupt and buffer it in a piece of kernel memory
  259. you've allocated for that purpose. With this technique you could avoid
  260. loss of data if your userspace program misses an interrupt.
  261. A note on shared interrupts: Your driver should support interrupt
  262. sharing whenever this is possible. It is possible if and only if your
  263. driver can detect whether your hardware has triggered the interrupt or
  264. not. This is usually done by looking at an interrupt status register. If
  265. your driver sees that the IRQ bit is actually set, it will perform its
  266. actions, and the handler returns IRQ_HANDLED. If the driver detects
  267. that it was not your hardware that caused the interrupt, it will do
  268. nothing and return IRQ_NONE, allowing the kernel to call the next
  269. possible interrupt handler.
  270. If you decide not to support shared interrupts, your card won't work in
  271. computers with no free interrupts. As this frequently happens on the PC
  272. platform, you can save yourself a lot of trouble by supporting interrupt
  273. sharing.
  274. Using uio_pdrv for platform devices
  275. -----------------------------------
  276. In many cases, UIO drivers for platform devices can be handled in a
  277. generic way. In the same place where you define your
  278. ``struct platform_device``, you simply also implement your interrupt
  279. handler and fill your ``struct uio_info``. A pointer to this
  280. ``struct uio_info`` is then used as ``platform_data`` for your platform
  281. device.
  282. You also need to set up an array of ``struct resource`` containing
  283. addresses and sizes of your memory mappings. This information is passed
  284. to the driver using the ``.resource`` and ``.num_resources`` elements of
  285. ``struct platform_device``.
  286. You now have to set the ``.name`` element of ``struct platform_device``
  287. to ``"uio_pdrv"`` to use the generic UIO platform device driver. This
  288. driver will fill the ``mem[]`` array according to the resources given,
  289. and register the device.
  290. The advantage of this approach is that you only have to edit a file you
  291. need to edit anyway. You do not have to create an extra driver.
  292. Using uio_pdrv_genirq for platform devices
  293. ------------------------------------------
  294. Especially in embedded devices, you frequently find chips where the irq
  295. pin is tied to its own dedicated interrupt line. In such cases, where
  296. you can be really sure the interrupt is not shared, we can take the
  297. concept of ``uio_pdrv`` one step further and use a generic interrupt
  298. handler. That's what ``uio_pdrv_genirq`` does.
  299. The setup for this driver is the same as described above for
  300. ``uio_pdrv``, except that you do not implement an interrupt handler. The
  301. ``.handler`` element of ``struct uio_info`` must remain ``NULL``. The
  302. ``.irq_flags`` element must not contain ``IRQF_SHARED``.
  303. You will set the ``.name`` element of ``struct platform_device`` to
  304. ``"uio_pdrv_genirq"`` to use this driver.
  305. The generic interrupt handler of ``uio_pdrv_genirq`` will simply disable
  306. the interrupt line using :c:func:`disable_irq_nosync()`. After
  307. doing its work, userspace can reenable the interrupt by writing
  308. 0x00000001 to the UIO device file. The driver already implements an
  309. :c:func:`irq_control()` to make this possible, you must not
  310. implement your own.
  311. Using ``uio_pdrv_genirq`` not only saves a few lines of interrupt
  312. handler code. You also do not need to know anything about the chip's
  313. internal registers to create the kernel part of the driver. All you need
  314. to know is the irq number of the pin the chip is connected to.
  315. When used in a device-tree enabled system, the driver needs to be
  316. probed with the ``"of_id"`` module parameter set to the ``"compatible"``
  317. string of the node the driver is supposed to handle. By default, the
  318. node's name (without the unit address) is exposed as name for the
  319. UIO device in userspace. To set a custom name, a property named
  320. ``"linux,uio-name"`` may be specified in the DT node.
  321. Using uio_dmem_genirq for platform devices
  322. ------------------------------------------
  323. In addition to statically allocated memory ranges, they may also be a
  324. desire to use dynamically allocated regions in a user space driver. In
  325. particular, being able to access memory made available through the
  326. dma-mapping API, may be particularly useful. The ``uio_dmem_genirq``
  327. driver provides a way to accomplish this.
  328. This driver is used in a similar manner to the ``"uio_pdrv_genirq"``
  329. driver with respect to interrupt configuration and handling.
  330. Set the ``.name`` element of ``struct platform_device`` to
  331. ``"uio_dmem_genirq"`` to use this driver.
  332. When using this driver, fill in the ``.platform_data`` element of
  333. ``struct platform_device``, which is of type
  334. ``struct uio_dmem_genirq_pdata`` and which contains the following
  335. elements:
  336. - ``struct uio_info uioinfo``: The same structure used as the
  337. ``uio_pdrv_genirq`` platform data
  338. - ``unsigned int *dynamic_region_sizes``: Pointer to list of sizes of
  339. dynamic memory regions to be mapped into user space.
  340. - ``unsigned int num_dynamic_regions``: Number of elements in
  341. ``dynamic_region_sizes`` array.
  342. The dynamic regions defined in the platform data will be appended to the
  343. `` mem[] `` array after the platform device resources, which implies
  344. that the total number of static and dynamic memory regions cannot exceed
  345. ``MAX_UIO_MAPS``.
  346. The dynamic memory regions will be allocated when the UIO device file,
  347. ``/dev/uioX`` is opened. Similar to static memory resources, the memory
  348. region information for dynamic regions is then visible via sysfs at
  349. ``/sys/class/uio/uioX/maps/mapY/*``. The dynamic memory regions will be
  350. freed when the UIO device file is closed. When no processes are holding
  351. the device file open, the address returned to userspace is ~0.
  352. Writing a driver in userspace
  353. =============================
  354. Once you have a working kernel module for your hardware, you can write
  355. the userspace part of your driver. You don't need any special libraries,
  356. your driver can be written in any reasonable language, you can use
  357. floating point numbers and so on. In short, you can use all the tools
  358. and libraries you'd normally use for writing a userspace application.
  359. Getting information about your UIO device
  360. -----------------------------------------
  361. Information about all UIO devices is available in sysfs. The first thing
  362. you should do in your driver is check ``name`` and ``version`` to make
  363. sure you're talking to the right device and that its kernel driver has
  364. the version you expect.
  365. You should also make sure that the memory mapping you need exists and
  366. has the size you expect.
  367. There is a tool called ``lsuio`` that lists UIO devices and their
  368. attributes. It is available here:
  369. http://www.osadl.org/projects/downloads/UIO/user/
  370. With ``lsuio`` you can quickly check if your kernel module is loaded and
  371. which attributes it exports. Have a look at the manpage for details.
  372. The source code of ``lsuio`` can serve as an example for getting
  373. information about an UIO device. The file ``uio_helper.c`` contains a
  374. lot of functions you could use in your userspace driver code.
  375. mmap() device memory
  376. --------------------
  377. After you made sure you've got the right device with the memory mappings
  378. you need, all you have to do is to call :c:func:`mmap()` to map the
  379. device's memory to userspace.
  380. The parameter ``offset`` of the :c:func:`mmap()` call has a special
  381. meaning for UIO devices: It is used to select which mapping of your
  382. device you want to map. To map the memory of mapping N, you have to use
  383. N times the page size as your offset::
  384. offset = N * getpagesize();
  385. N starts from zero, so if you've got only one memory range to map, set
  386. ``offset = 0``. A drawback of this technique is that memory is always
  387. mapped beginning with its start address.
  388. Waiting for interrupts
  389. ----------------------
  390. After you successfully mapped your devices memory, you can access it
  391. like an ordinary array. Usually, you will perform some initialization.
  392. After that, your hardware starts working and will generate an interrupt
  393. as soon as it's finished, has some data available, or needs your
  394. attention because an error occurred.
  395. ``/dev/uioX`` is a read-only file. A :c:func:`read()` will always
  396. block until an interrupt occurs. There is only one legal value for the
  397. ``count`` parameter of :c:func:`read()`, and that is the size of a
  398. signed 32 bit integer (4). Any other value for ``count`` causes
  399. :c:func:`read()` to fail. The signed 32 bit integer read is the
  400. interrupt count of your device. If the value is one more than the value
  401. you read the last time, everything is OK. If the difference is greater
  402. than one, you missed interrupts.
  403. You can also use :c:func:`select()` on ``/dev/uioX``.
  404. Generic PCI UIO driver
  405. ======================
  406. The generic driver is a kernel module named uio_pci_generic. It can
  407. work with any device compliant to PCI 2.3 (circa 2002) and any compliant
  408. PCI Express device. Using this, you only need to write the userspace
  409. driver, removing the need to write a hardware-specific kernel module.
  410. Making the driver recognize the device
  411. --------------------------------------
  412. Since the driver does not declare any device ids, it will not get loaded
  413. automatically and will not automatically bind to any devices, you must
  414. load it and allocate id to the driver yourself. For example::
  415. modprobe uio_pci_generic
  416. echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id
  417. If there already is a hardware specific kernel driver for your device,
  418. the generic driver still won't bind to it, in this case if you want to
  419. use the generic driver (why would you?) you'll have to manually unbind
  420. the hardware specific driver and bind the generic driver, like this::
  421. echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
  422. echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind
  423. You can verify that the device has been bound to the driver by looking
  424. for it in sysfs, for example like the following::
  425. ls -l /sys/bus/pci/devices/0000:00:19.0/driver
  426. Which if successful should print::
  427. .../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic
  428. Note that the generic driver will not bind to old PCI 2.2 devices. If
  429. binding the device failed, run the following command::
  430. dmesg
  431. and look in the output for failure reasons.
  432. Things to know about uio_pci_generic
  433. ------------------------------------
  434. Interrupts are handled using the Interrupt Disable bit in the PCI
  435. command register and Interrupt Status bit in the PCI status register.
  436. All devices compliant to PCI 2.3 (circa 2002) and all compliant PCI
  437. Express devices should support these bits. uio_pci_generic detects
  438. this support, and won't bind to devices which do not support the
  439. Interrupt Disable Bit in the command register.
  440. On each interrupt, uio_pci_generic sets the Interrupt Disable bit.
  441. This prevents the device from generating further interrupts until the
  442. bit is cleared. The userspace driver should clear this bit before
  443. blocking and waiting for more interrupts.
  444. Writing userspace driver using uio_pci_generic
  445. ------------------------------------------------
  446. Userspace driver can use pci sysfs interface, or the libpci library that
  447. wraps it, to talk to the device and to re-enable interrupts by writing
  448. to the command register.
  449. Example code using uio_pci_generic
  450. ----------------------------------
  451. Here is some sample userspace driver code using uio_pci_generic::
  452. #include <stdlib.h>
  453. #include <stdio.h>
  454. #include <unistd.h>
  455. #include <sys/types.h>
  456. #include <sys/stat.h>
  457. #include <fcntl.h>
  458. #include <errno.h>
  459. int main()
  460. {
  461. int uiofd;
  462. int configfd;
  463. int err;
  464. int i;
  465. unsigned icount;
  466. unsigned char command_high;
  467. uiofd = open("/dev/uio0", O_RDONLY);
  468. if (uiofd < 0) {
  469. perror("uio open:");
  470. return errno;
  471. }
  472. configfd = open("/sys/class/uio/uio0/device/config", O_RDWR);
  473. if (configfd < 0) {
  474. perror("config open:");
  475. return errno;
  476. }
  477. /* Read and cache command value */
  478. err = pread(configfd, &command_high, 1, 5);
  479. if (err != 1) {
  480. perror("command config read:");
  481. return errno;
  482. }
  483. command_high &= ~0x4;
  484. for(i = 0;; ++i) {
  485. /* Print out a message, for debugging. */
  486. if (i == 0)
  487. fprintf(stderr, "Started uio test driver.\n");
  488. else
  489. fprintf(stderr, "Interrupts: %d\n", icount);
  490. /****************************************/
  491. /* Here we got an interrupt from the
  492. device. Do something to it. */
  493. /****************************************/
  494. /* Re-enable interrupts. */
  495. err = pwrite(configfd, &command_high, 1, 5);
  496. if (err != 1) {
  497. perror("config write:");
  498. break;
  499. }
  500. /* Wait for next interrupt. */
  501. err = read(uiofd, &icount, 4);
  502. if (err != 4) {
  503. perror("uio read:");
  504. break;
  505. }
  506. }
  507. return errno;
  508. }
  509. Generic Hyper-V UIO driver
  510. ==========================
  511. The generic driver is a kernel module named uio_hv_generic. It
  512. supports devices on the Hyper-V VMBus similar to uio_pci_generic on
  513. PCI bus.
  514. Making the driver recognize the device
  515. --------------------------------------
  516. Since the driver does not declare any device GUID's, it will not get
  517. loaded automatically and will not automatically bind to any devices, you
  518. must load it and allocate id to the driver yourself. For example, to use
  519. the network device class GUID::
  520. modprobe uio_hv_generic
  521. echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" > /sys/bus/vmbus/drivers/uio_hv_generic/new_id
  522. If there already is a hardware specific kernel driver for the device,
  523. the generic driver still won't bind to it, in this case if you want to
  524. use the generic driver for a userspace library you'll have to manually unbind
  525. the hardware specific driver and bind the generic driver, using the device specific GUID
  526. like this::
  527. echo -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/hv_netvsc/unbind
  528. echo -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/uio_hv_generic/bind
  529. You can verify that the device has been bound to the driver by looking
  530. for it in sysfs, for example like the following::
  531. ls -l /sys/bus/vmbus/devices/ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver
  532. Which if successful should print::
  533. .../ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver -> ../../../bus/vmbus/drivers/uio_hv_generic
  534. Things to know about uio_hv_generic
  535. -----------------------------------
  536. On each interrupt, uio_hv_generic sets the Interrupt Disable bit. This
  537. prevents the device from generating further interrupts until the bit is
  538. cleared. The userspace driver should clear this bit before blocking and
  539. waiting for more interrupts.
  540. When host rescinds a device, the interrupt file descriptor is marked down
  541. and any reads of the interrupt file descriptor will return -EIO. Similar
  542. to a closed socket or disconnected serial device.
  543. The vmbus device regions are mapped into uio device resources:
  544. 0) Channel ring buffers: guest to host and host to guest
  545. 1) Guest to host interrupt signalling pages
  546. 2) Guest to host monitor page
  547. 3) Network receive buffer region
  548. 4) Network send buffer region
  549. If a subchannel is created by a request to host, then the uio_hv_generic
  550. device driver will create a sysfs binary file for the per-channel ring buffer.
  551. For example::
  552. /sys/bus/vmbus/devices/3811fe4d-0fa0-4b62-981a-74fc1084c757/channels/21/ring
  553. Further information
  554. ===================
  555. - `OSADL homepage. <http://www.osadl.org>`_
  556. - `Linutronix homepage. <http://www.linutronix.de>`_