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