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