0001 =======================
0002 The Userspace I/O HOWTO
0003 =======================
0004
0005 :Author: Hans-Jürgen Koch Linux developer, Linutronix
0006 :Date: 2006-12-11
0007
0008 About this document
0009 ===================
0010
0011 Translations
0012 ------------
0013
0014 If you know of any translations for this document, or you are interested
0015 in translating it, please email me hjk@hansjkoch.de.
0016
0017 Preface
0018 -------
0019
0020 For many types of devices, creating a Linux kernel driver is overkill.
0021 All that is really needed is some way to handle an interrupt and provide
0022 access to the memory space of the device. The logic of controlling the
0023 device does not necessarily have to be within the kernel, as the device
0024 does not need to take advantage of any of other resources that the
0025 kernel provides. One such common class of devices that are like this are
0026 for industrial I/O cards.
0027
0028 To address this situation, the userspace I/O system (UIO) was designed.
0029 For typical industrial I/O cards, only a very small kernel module is
0030 needed. The main part of the driver will run in user space. This
0031 simplifies development and reduces the risk of serious bugs within a
0032 kernel module.
0033
0034 Please note that UIO is not an universal driver interface. Devices that
0035 are already handled well by other kernel subsystems (like networking or
0036 serial or USB) are no candidates for an UIO driver. Hardware that is
0037 ideally suited for an UIO driver fulfills all of the following:
0038
0039 - The device has memory that can be mapped. The device can be
0040 controlled completely by writing to this memory.
0041
0042 - The device usually generates interrupts.
0043
0044 - The device does not fit into one of the standard kernel subsystems.
0045
0046 Acknowledgments
0047 ---------------
0048
0049 I'd like to thank Thomas Gleixner and Benedikt Spranger of Linutronix,
0050 who have not only written most of the UIO code, but also helped greatly
0051 writing this HOWTO by giving me all kinds of background information.
0052
0053 Feedback
0054 --------
0055
0056 Find something wrong with this document? (Or perhaps something right?) I
0057 would love to hear from you. Please email me at hjk@hansjkoch.de.
0058
0059 About UIO
0060 =========
0061
0062 If you use UIO for your card's driver, here's what you get:
0063
0064 - only one small kernel module to write and maintain.
0065
0066 - develop the main part of your driver in user space, with all the
0067 tools and libraries you're used to.
0068
0069 - bugs in your driver won't crash the kernel.
0070
0071 - updates of your driver can take place without recompiling the kernel.
0072
0073 How UIO works
0074 -------------
0075
0076 Each UIO device is accessed through a device file and several sysfs
0077 attribute files. The device file will be called ``/dev/uio0`` for the
0078 first device, and ``/dev/uio1``, ``/dev/uio2`` and so on for subsequent
0079 devices.
0080
0081 ``/dev/uioX`` is used to access the address space of the card. Just use
0082 :c:func:`mmap()` to access registers or RAM locations of your card.
0083
0084 Interrupts are handled by reading from ``/dev/uioX``. A blocking
0085 :c:func:`read()` from ``/dev/uioX`` will return as soon as an
0086 interrupt occurs. You can also use :c:func:`select()` on
0087 ``/dev/uioX`` to wait for an interrupt. The integer value read from
0088 ``/dev/uioX`` represents the total interrupt count. You can use this
0089 number to figure out if you missed some interrupts.
0090
0091 For some hardware that has more than one interrupt source internally,
0092 but not separate IRQ mask and status registers, there might be
0093 situations where userspace cannot determine what the interrupt source
0094 was if the kernel handler disables them by writing to the chip's IRQ
0095 register. In such a case, the kernel has to disable the IRQ completely
0096 to leave the chip's register untouched. Now the userspace part can
0097 determine the cause of the interrupt, but it cannot re-enable
0098 interrupts. Another cornercase is chips where re-enabling interrupts is
0099 a read-modify-write operation to a combined IRQ status/acknowledge
0100 register. This would be racy if a new interrupt occurred simultaneously.
0101
0102 To address these problems, UIO also implements a write() function. It is
0103 normally not used and can be ignored for hardware that has only a single
0104 interrupt source or has separate IRQ mask and status registers. If you
0105 need it, however, a write to ``/dev/uioX`` will call the
0106 :c:func:`irqcontrol()` function implemented by the driver. You have
0107 to write a 32-bit value that is usually either 0 or 1 to disable or
0108 enable interrupts. If a driver does not implement
0109 :c:func:`irqcontrol()`, :c:func:`write()` will return with
0110 ``-ENOSYS``.
0111
0112 To handle interrupts properly, your custom kernel module can provide its
0113 own interrupt handler. It will automatically be called by the built-in
0114 handler.
0115
0116 For cards that don't generate interrupts but need to be polled, there is
0117 the possibility to set up a timer that triggers the interrupt handler at
0118 configurable time intervals. This interrupt simulation is done by
0119 calling :c:func:`uio_event_notify()` from the timer's event
0120 handler.
0121
0122 Each driver provides attributes that are used to read or write
0123 variables. These attributes are accessible through sysfs files. A custom
0124 kernel driver module can add its own attributes to the device owned by
0125 the uio driver, but not added to the UIO device itself at this time.
0126 This might change in the future if it would be found to be useful.
0127
0128 The following standard attributes are provided by the UIO framework:
0129
0130 - ``name``: The name of your device. It is recommended to use the name
0131 of your kernel module for this.
0132
0133 - ``version``: A version string defined by your driver. This allows the
0134 user space part of your driver to deal with different versions of the
0135 kernel module.
0136
0137 - ``event``: The total number of interrupts handled by the driver since
0138 the last time the device node was read.
0139
0140 These attributes appear under the ``/sys/class/uio/uioX`` directory.
0141 Please note that this directory might be a symlink, and not a real
0142 directory. Any userspace code that accesses it must be able to handle
0143 this.
0144
0145 Each UIO device can make one or more memory regions available for memory
0146 mapping. This is necessary because some industrial I/O cards require
0147 access to more than one PCI memory region in a driver.
0148
0149 Each mapping has its own directory in sysfs, the first mapping appears
0150 as ``/sys/class/uio/uioX/maps/map0/``. Subsequent mappings create
0151 directories ``map1/``, ``map2/``, and so on. These directories will only
0152 appear if the size of the mapping is not 0.
0153
0154 Each ``mapX/`` directory contains four read-only files that show
0155 attributes of the memory:
0156
0157 - ``name``: A string identifier for this mapping. This is optional, the
0158 string can be empty. Drivers can set this to make it easier for
0159 userspace to find the correct mapping.
0160
0161 - ``addr``: The address of memory that can be mapped.
0162
0163 - ``size``: The size, in bytes, of the memory pointed to by addr.
0164
0165 - ``offset``: The offset, in bytes, that has to be added to the pointer
0166 returned by :c:func:`mmap()` to get to the actual device memory.
0167 This is important if the device's memory is not page aligned.
0168 Remember that pointers returned by :c:func:`mmap()` are always
0169 page aligned, so it is good style to always add this offset.
0170
0171 From userspace, the different mappings are distinguished by adjusting
0172 the ``offset`` parameter of the :c:func:`mmap()` call. To map the
0173 memory of mapping N, you have to use N times the page size as your
0174 offset::
0175
0176 offset = N * getpagesize();
0177
0178 Sometimes there is hardware with memory-like regions that can not be
0179 mapped with the technique described here, but there are still ways to
0180 access them from userspace. The most common example are x86 ioports. On
0181 x86 systems, userspace can access these ioports using
0182 :c:func:`ioperm()`, :c:func:`iopl()`, :c:func:`inb()`,
0183 :c:func:`outb()`, and similar functions.
0184
0185 Since these ioport regions can not be mapped, they will not appear under
0186 ``/sys/class/uio/uioX/maps/`` like the normal memory described above.
0187 Without information about the port regions a hardware has to offer, it
0188 becomes difficult for the userspace part of the driver to find out which
0189 ports belong to which UIO device.
0190
0191 To address this situation, the new directory
0192 ``/sys/class/uio/uioX/portio/`` was added. It only exists if the driver
0193 wants to pass information about one or more port regions to userspace.
0194 If that is the case, subdirectories named ``port0``, ``port1``, and so
0195 on, will appear underneath ``/sys/class/uio/uioX/portio/``.
0196
0197 Each ``portX/`` directory contains four read-only files that show name,
0198 start, size, and type of the port region:
0199
0200 - ``name``: A string identifier for this port region. The string is
0201 optional and can be empty. Drivers can set it to make it easier for
0202 userspace to find a certain port region.
0203
0204 - ``start``: The first port of this region.
0205
0206 - ``size``: The number of ports in this region.
0207
0208 - ``porttype``: A string describing the type of port.
0209
0210 Writing your own kernel module
0211 ==============================
0212
0213 Please have a look at ``uio_cif.c`` as an example. The following
0214 paragraphs explain the different sections of this file.
0215
0216 struct uio_info
0217 ---------------
0218
0219 This structure tells the framework the details of your driver, Some of
0220 the members are required, others are optional.
0221
0222 - ``const char *name``: Required. The name of your driver as it will
0223 appear in sysfs. I recommend using the name of your module for this.
0224
0225 - ``const char *version``: Required. This string appears in
0226 ``/sys/class/uio/uioX/version``.
0227
0228 - ``struct uio_mem mem[ MAX_UIO_MAPS ]``: Required if you have memory
0229 that can be mapped with :c:func:`mmap()`. For each mapping you
0230 need to fill one of the ``uio_mem`` structures. See the description
0231 below for details.
0232
0233 - ``struct uio_port port[ MAX_UIO_PORTS_REGIONS ]``: Required if you
0234 want to pass information about ioports to userspace. For each port
0235 region you need to fill one of the ``uio_port`` structures. See the
0236 description below for details.
0237
0238 - ``long irq``: Required. If your hardware generates an interrupt, it's
0239 your modules task to determine the irq number during initialization.
0240 If you don't have a hardware generated interrupt but want to trigger
0241 the interrupt handler in some other way, set ``irq`` to
0242 ``UIO_IRQ_CUSTOM``. If you had no interrupt at all, you could set
0243 ``irq`` to ``UIO_IRQ_NONE``, though this rarely makes sense.
0244
0245 - ``unsigned long irq_flags``: Required if you've set ``irq`` to a
0246 hardware interrupt number. The flags given here will be used in the
0247 call to :c:func:`request_irq()`.
0248
0249 - ``int (*mmap)(struct uio_info *info, struct vm_area_struct *vma)``:
0250 Optional. If you need a special :c:func:`mmap()`
0251 function, you can set it here. If this pointer is not NULL, your
0252 :c:func:`mmap()` will be called instead of the built-in one.
0253
0254 - ``int (*open)(struct uio_info *info, struct inode *inode)``:
0255 Optional. You might want to have your own :c:func:`open()`,
0256 e.g. to enable interrupts only when your device is actually used.
0257
0258 - ``int (*release)(struct uio_info *info, struct inode *inode)``:
0259 Optional. If you define your own :c:func:`open()`, you will
0260 probably also want a custom :c:func:`release()` function.
0261
0262 - ``int (*irqcontrol)(struct uio_info *info, s32 irq_on)``:
0263 Optional. If you need to be able to enable or disable interrupts
0264 from userspace by writing to ``/dev/uioX``, you can implement this
0265 function. The parameter ``irq_on`` will be 0 to disable interrupts
0266 and 1 to enable them.
0267
0268 Usually, your device will have one or more memory regions that can be
0269 mapped to user space. For each region, you have to set up a
0270 ``struct uio_mem`` in the ``mem[]`` array. Here's a description of the
0271 fields of ``struct uio_mem``:
0272
0273 - ``const char *name``: Optional. Set this to help identify the memory
0274 region, it will show up in the corresponding sysfs node.
0275
0276 - ``int memtype``: Required if the mapping is used. Set this to
0277 ``UIO_MEM_PHYS`` if you have physical memory on your card to be
0278 mapped. Use ``UIO_MEM_LOGICAL`` for logical memory (e.g. allocated
0279 with :c:func:`__get_free_pages()` but not kmalloc()). There's also
0280 ``UIO_MEM_VIRTUAL`` for virtual memory.
0281
0282 - ``phys_addr_t addr``: Required if the mapping is used. Fill in the
0283 address of your memory block. This address is the one that appears in
0284 sysfs.
0285
0286 - ``resource_size_t size``: Fill in the size of the memory block that
0287 ``addr`` points to. If ``size`` is zero, the mapping is considered
0288 unused. Note that you *must* initialize ``size`` with zero for all
0289 unused mappings.
0290
0291 - ``void *internal_addr``: If you have to access this memory region
0292 from within your kernel module, you will want to map it internally by
0293 using something like :c:func:`ioremap()`. Addresses returned by
0294 this function cannot be mapped to user space, so you must not store
0295 it in ``addr``. Use ``internal_addr`` instead to remember such an
0296 address.
0297
0298 Please do not touch the ``map`` element of ``struct uio_mem``! It is
0299 used by the UIO framework to set up sysfs files for this mapping. Simply
0300 leave it alone.
0301
0302 Sometimes, your device can have one or more port regions which can not
0303 be mapped to userspace. But if there are other possibilities for
0304 userspace to access these ports, it makes sense to make information
0305 about the ports available in sysfs. For each region, you have to set up
0306 a ``struct uio_port`` in the ``port[]`` array. Here's a description of
0307 the fields of ``struct uio_port``:
0308
0309 - ``char *porttype``: Required. Set this to one of the predefined
0310 constants. Use ``UIO_PORT_X86`` for the ioports found in x86
0311 architectures.
0312
0313 - ``unsigned long start``: Required if the port region is used. Fill in
0314 the number of the first port of this region.
0315
0316 - ``unsigned long size``: Fill in the number of ports in this region.
0317 If ``size`` is zero, the region is considered unused. Note that you
0318 *must* initialize ``size`` with zero for all unused regions.
0319
0320 Please do not touch the ``portio`` element of ``struct uio_port``! It is
0321 used internally by the UIO framework to set up sysfs files for this
0322 region. Simply leave it alone.
0323
0324 Adding an interrupt handler
0325 ---------------------------
0326
0327 What you need to do in your interrupt handler depends on your hardware
0328 and on how you want to handle it. You should try to keep the amount of
0329 code in your kernel interrupt handler low. If your hardware requires no
0330 action that you *have* to perform after each interrupt, then your
0331 handler can be empty.
0332
0333 If, on the other hand, your hardware *needs* some action to be performed
0334 after each interrupt, then you *must* do it in your kernel module. Note
0335 that you cannot rely on the userspace part of your driver. Your
0336 userspace program can terminate at any time, possibly leaving your
0337 hardware in a state where proper interrupt handling is still required.
0338
0339 There might also be applications where you want to read data from your
0340 hardware at each interrupt and buffer it in a piece of kernel memory
0341 you've allocated for that purpose. With this technique you could avoid
0342 loss of data if your userspace program misses an interrupt.
0343
0344 A note on shared interrupts: Your driver should support interrupt
0345 sharing whenever this is possible. It is possible if and only if your
0346 driver can detect whether your hardware has triggered the interrupt or
0347 not. This is usually done by looking at an interrupt status register. If
0348 your driver sees that the IRQ bit is actually set, it will perform its
0349 actions, and the handler returns IRQ_HANDLED. If the driver detects
0350 that it was not your hardware that caused the interrupt, it will do
0351 nothing and return IRQ_NONE, allowing the kernel to call the next
0352 possible interrupt handler.
0353
0354 If you decide not to support shared interrupts, your card won't work in
0355 computers with no free interrupts. As this frequently happens on the PC
0356 platform, you can save yourself a lot of trouble by supporting interrupt
0357 sharing.
0358
0359 Using uio_pdrv for platform devices
0360 -----------------------------------
0361
0362 In many cases, UIO drivers for platform devices can be handled in a
0363 generic way. In the same place where you define your
0364 ``struct platform_device``, you simply also implement your interrupt
0365 handler and fill your ``struct uio_info``. A pointer to this
0366 ``struct uio_info`` is then used as ``platform_data`` for your platform
0367 device.
0368
0369 You also need to set up an array of ``struct resource`` containing
0370 addresses and sizes of your memory mappings. This information is passed
0371 to the driver using the ``.resource`` and ``.num_resources`` elements of
0372 ``struct platform_device``.
0373
0374 You now have to set the ``.name`` element of ``struct platform_device``
0375 to ``"uio_pdrv"`` to use the generic UIO platform device driver. This
0376 driver will fill the ``mem[]`` array according to the resources given,
0377 and register the device.
0378
0379 The advantage of this approach is that you only have to edit a file you
0380 need to edit anyway. You do not have to create an extra driver.
0381
0382 Using uio_pdrv_genirq for platform devices
0383 ------------------------------------------
0384
0385 Especially in embedded devices, you frequently find chips where the irq
0386 pin is tied to its own dedicated interrupt line. In such cases, where
0387 you can be really sure the interrupt is not shared, we can take the
0388 concept of ``uio_pdrv`` one step further and use a generic interrupt
0389 handler. That's what ``uio_pdrv_genirq`` does.
0390
0391 The setup for this driver is the same as described above for
0392 ``uio_pdrv``, except that you do not implement an interrupt handler. The
0393 ``.handler`` element of ``struct uio_info`` must remain ``NULL``. The
0394 ``.irq_flags`` element must not contain ``IRQF_SHARED``.
0395
0396 You will set the ``.name`` element of ``struct platform_device`` to
0397 ``"uio_pdrv_genirq"`` to use this driver.
0398
0399 The generic interrupt handler of ``uio_pdrv_genirq`` will simply disable
0400 the interrupt line using :c:func:`disable_irq_nosync()`. After
0401 doing its work, userspace can reenable the interrupt by writing
0402 0x00000001 to the UIO device file. The driver already implements an
0403 :c:func:`irq_control()` to make this possible, you must not
0404 implement your own.
0405
0406 Using ``uio_pdrv_genirq`` not only saves a few lines of interrupt
0407 handler code. You also do not need to know anything about the chip's
0408 internal registers to create the kernel part of the driver. All you need
0409 to know is the irq number of the pin the chip is connected to.
0410
0411 When used in a device-tree enabled system, the driver needs to be
0412 probed with the ``"of_id"`` module parameter set to the ``"compatible"``
0413 string of the node the driver is supposed to handle. By default, the
0414 node's name (without the unit address) is exposed as name for the
0415 UIO device in userspace. To set a custom name, a property named
0416 ``"linux,uio-name"`` may be specified in the DT node.
0417
0418 Using uio_dmem_genirq for platform devices
0419 ------------------------------------------
0420
0421 In addition to statically allocated memory ranges, they may also be a
0422 desire to use dynamically allocated regions in a user space driver. In
0423 particular, being able to access memory made available through the
0424 dma-mapping API, may be particularly useful. The ``uio_dmem_genirq``
0425 driver provides a way to accomplish this.
0426
0427 This driver is used in a similar manner to the ``"uio_pdrv_genirq"``
0428 driver with respect to interrupt configuration and handling.
0429
0430 Set the ``.name`` element of ``struct platform_device`` to
0431 ``"uio_dmem_genirq"`` to use this driver.
0432
0433 When using this driver, fill in the ``.platform_data`` element of
0434 ``struct platform_device``, which is of type
0435 ``struct uio_dmem_genirq_pdata`` and which contains the following
0436 elements:
0437
0438 - ``struct uio_info uioinfo``: The same structure used as the
0439 ``uio_pdrv_genirq`` platform data
0440
0441 - ``unsigned int *dynamic_region_sizes``: Pointer to list of sizes of
0442 dynamic memory regions to be mapped into user space.
0443
0444 - ``unsigned int num_dynamic_regions``: Number of elements in
0445 ``dynamic_region_sizes`` array.
0446
0447 The dynamic regions defined in the platform data will be appended to the
0448 `` mem[] `` array after the platform device resources, which implies
0449 that the total number of static and dynamic memory regions cannot exceed
0450 ``MAX_UIO_MAPS``.
0451
0452 The dynamic memory regions will be allocated when the UIO device file,
0453 ``/dev/uioX`` is opened. Similar to static memory resources, the memory
0454 region information for dynamic regions is then visible via sysfs at
0455 ``/sys/class/uio/uioX/maps/mapY/*``. The dynamic memory regions will be
0456 freed when the UIO device file is closed. When no processes are holding
0457 the device file open, the address returned to userspace is ~0.
0458
0459 Writing a driver in userspace
0460 =============================
0461
0462 Once you have a working kernel module for your hardware, you can write
0463 the userspace part of your driver. You don't need any special libraries,
0464 your driver can be written in any reasonable language, you can use
0465 floating point numbers and so on. In short, you can use all the tools
0466 and libraries you'd normally use for writing a userspace application.
0467
0468 Getting information about your UIO device
0469 -----------------------------------------
0470
0471 Information about all UIO devices is available in sysfs. The first thing
0472 you should do in your driver is check ``name`` and ``version`` to make
0473 sure you're talking to the right device and that its kernel driver has
0474 the version you expect.
0475
0476 You should also make sure that the memory mapping you need exists and
0477 has the size you expect.
0478
0479 There is a tool called ``lsuio`` that lists UIO devices and their
0480 attributes. It is available here:
0481
0482 http://www.osadl.org/projects/downloads/UIO/user/
0483
0484 With ``lsuio`` you can quickly check if your kernel module is loaded and
0485 which attributes it exports. Have a look at the manpage for details.
0486
0487 The source code of ``lsuio`` can serve as an example for getting
0488 information about an UIO device. The file ``uio_helper.c`` contains a
0489 lot of functions you could use in your userspace driver code.
0490
0491 mmap() device memory
0492 --------------------
0493
0494 After you made sure you've got the right device with the memory mappings
0495 you need, all you have to do is to call :c:func:`mmap()` to map the
0496 device's memory to userspace.
0497
0498 The parameter ``offset`` of the :c:func:`mmap()` call has a special
0499 meaning for UIO devices: It is used to select which mapping of your
0500 device you want to map. To map the memory of mapping N, you have to use
0501 N times the page size as your offset::
0502
0503 offset = N * getpagesize();
0504
0505 N starts from zero, so if you've got only one memory range to map, set
0506 ``offset = 0``. A drawback of this technique is that memory is always
0507 mapped beginning with its start address.
0508
0509 Waiting for interrupts
0510 ----------------------
0511
0512 After you successfully mapped your devices memory, you can access it
0513 like an ordinary array. Usually, you will perform some initialization.
0514 After that, your hardware starts working and will generate an interrupt
0515 as soon as it's finished, has some data available, or needs your
0516 attention because an error occurred.
0517
0518 ``/dev/uioX`` is a read-only file. A :c:func:`read()` will always
0519 block until an interrupt occurs. There is only one legal value for the
0520 ``count`` parameter of :c:func:`read()`, and that is the size of a
0521 signed 32 bit integer (4). Any other value for ``count`` causes
0522 :c:func:`read()` to fail. The signed 32 bit integer read is the
0523 interrupt count of your device. If the value is one more than the value
0524 you read the last time, everything is OK. If the difference is greater
0525 than one, you missed interrupts.
0526
0527 You can also use :c:func:`select()` on ``/dev/uioX``.
0528
0529 Generic PCI UIO driver
0530 ======================
0531
0532 The generic driver is a kernel module named uio_pci_generic. It can
0533 work with any device compliant to PCI 2.3 (circa 2002) and any compliant
0534 PCI Express device. Using this, you only need to write the userspace
0535 driver, removing the need to write a hardware-specific kernel module.
0536
0537 Making the driver recognize the device
0538 --------------------------------------
0539
0540 Since the driver does not declare any device ids, it will not get loaded
0541 automatically and will not automatically bind to any devices, you must
0542 load it and allocate id to the driver yourself. For example::
0543
0544 modprobe uio_pci_generic
0545 echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id
0546
0547 If there already is a hardware specific kernel driver for your device,
0548 the generic driver still won't bind to it, in this case if you want to
0549 use the generic driver (why would you?) you'll have to manually unbind
0550 the hardware specific driver and bind the generic driver, like this::
0551
0552 echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
0553 echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind
0554
0555 You can verify that the device has been bound to the driver by looking
0556 for it in sysfs, for example like the following::
0557
0558 ls -l /sys/bus/pci/devices/0000:00:19.0/driver
0559
0560 Which if successful should print::
0561
0562 .../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic
0563
0564 Note that the generic driver will not bind to old PCI 2.2 devices. If
0565 binding the device failed, run the following command::
0566
0567 dmesg
0568
0569 and look in the output for failure reasons.
0570
0571 Things to know about uio_pci_generic
0572 ------------------------------------
0573
0574 Interrupts are handled using the Interrupt Disable bit in the PCI
0575 command register and Interrupt Status bit in the PCI status register.
0576 All devices compliant to PCI 2.3 (circa 2002) and all compliant PCI
0577 Express devices should support these bits. uio_pci_generic detects
0578 this support, and won't bind to devices which do not support the
0579 Interrupt Disable Bit in the command register.
0580
0581 On each interrupt, uio_pci_generic sets the Interrupt Disable bit.
0582 This prevents the device from generating further interrupts until the
0583 bit is cleared. The userspace driver should clear this bit before
0584 blocking and waiting for more interrupts.
0585
0586 Writing userspace driver using uio_pci_generic
0587 ------------------------------------------------
0588
0589 Userspace driver can use pci sysfs interface, or the libpci library that
0590 wraps it, to talk to the device and to re-enable interrupts by writing
0591 to the command register.
0592
0593 Example code using uio_pci_generic
0594 ----------------------------------
0595
0596 Here is some sample userspace driver code using uio_pci_generic::
0597
0598 #include <stdlib.h>
0599 #include <stdio.h>
0600 #include <unistd.h>
0601 #include <sys/types.h>
0602 #include <sys/stat.h>
0603 #include <fcntl.h>
0604 #include <errno.h>
0605
0606 int main()
0607 {
0608 int uiofd;
0609 int configfd;
0610 int err;
0611 int i;
0612 unsigned icount;
0613 unsigned char command_high;
0614
0615 uiofd = open("/dev/uio0", O_RDONLY);
0616 if (uiofd < 0) {
0617 perror("uio open:");
0618 return errno;
0619 }
0620 configfd = open("/sys/class/uio/uio0/device/config", O_RDWR);
0621 if (configfd < 0) {
0622 perror("config open:");
0623 return errno;
0624 }
0625
0626 /* Read and cache command value */
0627 err = pread(configfd, &command_high, 1, 5);
0628 if (err != 1) {
0629 perror("command config read:");
0630 return errno;
0631 }
0632 command_high &= ~0x4;
0633
0634 for(i = 0;; ++i) {
0635 /* Print out a message, for debugging. */
0636 if (i == 0)
0637 fprintf(stderr, "Started uio test driver.\n");
0638 else
0639 fprintf(stderr, "Interrupts: %d\n", icount);
0640
0641 /****************************************/
0642 /* Here we got an interrupt from the
0643 device. Do something to it. */
0644 /****************************************/
0645
0646 /* Re-enable interrupts. */
0647 err = pwrite(configfd, &command_high, 1, 5);
0648 if (err != 1) {
0649 perror("config write:");
0650 break;
0651 }
0652
0653 /* Wait for next interrupt. */
0654 err = read(uiofd, &icount, 4);
0655 if (err != 4) {
0656 perror("uio read:");
0657 break;
0658 }
0659
0660 }
0661 return errno;
0662 }
0663
0664 Generic Hyper-V UIO driver
0665 ==========================
0666
0667 The generic driver is a kernel module named uio_hv_generic. It
0668 supports devices on the Hyper-V VMBus similar to uio_pci_generic on
0669 PCI bus.
0670
0671 Making the driver recognize the device
0672 --------------------------------------
0673
0674 Since the driver does not declare any device GUID's, it will not get
0675 loaded automatically and will not automatically bind to any devices, you
0676 must load it and allocate id to the driver yourself. For example, to use
0677 the network device class GUID::
0678
0679 modprobe uio_hv_generic
0680 echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" > /sys/bus/vmbus/drivers/uio_hv_generic/new_id
0681
0682 If there already is a hardware specific kernel driver for the device,
0683 the generic driver still won't bind to it, in this case if you want to
0684 use the generic driver for a userspace library you'll have to manually unbind
0685 the hardware specific driver and bind the generic driver, using the device specific GUID
0686 like this::
0687
0688 echo -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/hv_netvsc/unbind
0689 echo -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/uio_hv_generic/bind
0690
0691 You can verify that the device has been bound to the driver by looking
0692 for it in sysfs, for example like the following::
0693
0694 ls -l /sys/bus/vmbus/devices/ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver
0695
0696 Which if successful should print::
0697
0698 .../ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver -> ../../../bus/vmbus/drivers/uio_hv_generic
0699
0700 Things to know about uio_hv_generic
0701 -----------------------------------
0702
0703 On each interrupt, uio_hv_generic sets the Interrupt Disable bit. This
0704 prevents the device from generating further interrupts until the bit is
0705 cleared. The userspace driver should clear this bit before blocking and
0706 waiting for more interrupts.
0707
0708 When host rescinds a device, the interrupt file descriptor is marked down
0709 and any reads of the interrupt file descriptor will return -EIO. Similar
0710 to a closed socket or disconnected serial device.
0711
0712 The vmbus device regions are mapped into uio device resources:
0713 0) Channel ring buffers: guest to host and host to guest
0714 1) Guest to host interrupt signalling pages
0715 2) Guest to host monitor page
0716 3) Network receive buffer region
0717 4) Network send buffer region
0718
0719 If a subchannel is created by a request to host, then the uio_hv_generic
0720 device driver will create a sysfs binary file for the per-channel ring buffer.
0721 For example::
0722
0723 /sys/bus/vmbus/devices/3811fe4d-0fa0-4b62-981a-74fc1084c757/channels/21/ring
0724
0725 Further information
0726 ===================
0727
0728 - `OSADL homepage. <http://www.osadl.org>`_
0729
0730 - `Linutronix homepage. <http://www.linutronix.de>`_