sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget(/translations/zh_CN/driver-api/uio-howtomodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/zh_TW/driver-api/uio-howtomodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/it_IT/driver-api/uio-howtomodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/ja_JP/driver-api/uio-howtomodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/ko_KR/driver-api/uio-howtomodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/sp_SP/driver-api/uio-howtomodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(hThe Userspace I/O HOWTOh]hThe Userspace I/O HOWTO}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhB/var/lib/git/docbuild/linux/Documentation/driver-api/uio-howto.rsthKubh field_list)}(hhh](hfield)}(hhh](h field_name)}(hAuthorh]hAuthor}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh field_body)}(h-Hans-Jürgen Koch Linux developer, Linutronixh]h paragraph)}(hhh]h-Hans-Jürgen Koch Linux developer, Linutronix}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hhh](h)}(hDateh]hDate}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh)}(h 2006-12-11 h]h)}(h 2006-12-11h]h 2006-12-11}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubeh}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hAbout this documenth]hAbout this document}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj'hhhhhK ubh)}(hhh](h)}(h Translationsh]h Translations}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj8hhhhhK ubh)}(h}If you know of any translations for this document, or you are interested in translating it, please email me hjk@hansjkoch.de.h](hlIf you know of any translations for this document, or you are interested in translating it, please email me }(hjIhhhNhNubh reference)}(hhjk@hansjkoch.deh]hhjk@hansjkoch.de}(hjShhhNhNubah}(h]h ]h"]h$]h&]refurimailto:hjk@hansjkoch.deuh1jQhjIubh.}(hjIhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj8hhubeh}(h] translationsah ]h"] translationsah$]h&]uh1hhj'hhhhhK ubh)}(hhh](h)}(hPrefaceh]hPreface}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjuhhhhhKubh)}(hXFor many types of devices, creating a Linux kernel driver is overkill. All that is really needed is some way to handle an interrupt and provide access to the memory space of the device. The logic of controlling the device does not necessarily have to be within the kernel, as the device does not need to take advantage of any of other resources that the kernel provides. One such common class of devices that are like this are for industrial I/O cards.h]hXFor many types of devices, creating a Linux kernel driver is overkill. All that is really needed is some way to handle an interrupt and provide access to the memory space of the device. The logic of controlling the device does not necessarily have to be within the kernel, as the device does not need to take advantage of any of other resources that the kernel provides. One such common class of devices that are like this are for industrial I/O cards.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjuhhubh)}(hX!To address this situation, the userspace I/O system (UIO) was designed. For typical industrial I/O cards, only a very small kernel module is needed. The main part of the driver will run in user space. This simplifies development and reduces the risk of serious bugs within a kernel module.h]hX!To address this situation, the userspace I/O system (UIO) was designed. For typical industrial I/O cards, only a very small kernel module is needed. The main part of the driver will run in user space. This simplifies development and reduces the risk of serious bugs within a kernel module.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjuhhubh)}(hXPlease note that UIO is not an universal driver interface. Devices that are already handled well by other kernel subsystems (like networking or serial or USB) are no candidates for an UIO driver. Hardware that is ideally suited for an UIO driver fulfills all of the following:h]hXPlease note that UIO is not an universal driver interface. Devices that are already handled well by other kernel subsystems (like networking or serial or USB) are no candidates for an UIO driver. Hardware that is ideally suited for an UIO driver fulfills all of the following:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK"hjuhhubh bullet_list)}(hhh](h list_item)}(hmThe device has memory that can be mapped. The device can be controlled completely by writing to this memory. h]h)}(hlThe device has memory that can be mapped. The device can be controlled completely by writing to this memory.h]hlThe device has memory that can be mapped. The device can be controlled completely by writing to this memory.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK'hjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h)The device usually generates interrupts. h]h)}(h(The device usually generates interrupts.h]h(The device usually generates interrupts.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK*hjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hDThe device does not fit into one of the standard kernel subsystems. h]h)}(hCThe device does not fit into one of the standard kernel subsystems.h]hCThe device does not fit into one of the standard kernel subsystems.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK,hjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]bullet-uh1jhhhK'hjuhhubeh}(h]prefaceah ]h"]prefaceah$]h&]uh1hhj'hhhhhKubh)}(hhh](h)}(hAcknowledgmentsh]hAcknowledgments}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK/ubh)}(hI'd like to thank Thomas Gleixner and Benedikt Spranger of Linutronix, who have not only written most of the UIO code, but also helped greatly writing this HOWTO by giving me all kinds of background information.h]hI’d like to thank Thomas Gleixner and Benedikt Spranger of Linutronix, who have not only written most of the UIO code, but also helped greatly writing this HOWTO by giving me all kinds of background information.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK1hjhhubeh}(h]acknowledgmentsah ]h"]acknowledgmentsah$]h&]uh1hhj'hhhhhK/ubh)}(hhh](h)}(hFeedbackh]hFeedback}(hj9hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj6hhhhhK6ubh)}(hFind something wrong with this document? (Or perhaps something right?) I would love to hear from you. Please email me at hjk@hansjkoch.de.h](hyFind something wrong with this document? (Or perhaps something right?) I would love to hear from you. Please email me at }(hjGhhhNhNubjR)}(hhjk@hansjkoch.deh]hhjk@hansjkoch.de}(hjOhhhNhNubah}(h]h ]h"]h$]h&]refurimailto:hjk@hansjkoch.deuh1jQhjGubh.}(hjGhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK8hj6hhubeh}(h]feedbackah ]h"]feedbackah$]h&]uh1hhj'hhhhhK6ubeh}(h]about-this-documentah ]h"]about this documentah$]h&]uh1hhhhhhhhK ubh)}(hhh](h)}(h About UIOh]h About UIO}(hj|hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjyhhhhhKhjyhhubj)}(hhh](j)}(h4only one small kernel module to write and maintain. h]h)}(h3only one small kernel module to write and maintain.h]h3only one small kernel module to write and maintain.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK@hjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hedevelop the main part of your driver in user space, with all the tools and libraries you're used to. h]h)}(hddevelop the main part of your driver in user space, with all the tools and libraries you're used to.h]hfdevelop the main part of your driver in user space, with all the tools and libraries you’re used to.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKBhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h,bugs in your driver won't crash the kernel. h]h)}(h+bugs in your driver won't crash the kernel.h]h-bugs in your driver won’t crash the kernel.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKEhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hFupdates of your driver can take place without recompiling the kernel. h]h)}(hEupdates of your driver can take place without recompiling the kernel.h]hEupdates of your driver can take place without recompiling the kernel.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKGhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhK@hjyhhubh)}(hhh](h)}(h How UIO worksh]h How UIO works}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKJubh)}(hEach UIO device is accessed through a device file and several sysfs attribute files. The device file will be called ``/dev/uio0`` for the first device, and ``/dev/uio1``, ``/dev/uio2`` and so on for subsequent devices.h](htEach UIO device is accessed through a device file and several sysfs attribute files. The device file will be called }(hjhhhNhNubhliteral)}(h ``/dev/uio0``h]h /dev/uio0}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh for the first device, and }(hjhhhNhNubj)}(h ``/dev/uio1``h]h /dev/uio1}(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, }(hjhhhNhNubj)}(h ``/dev/uio2``h]h /dev/uio2}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh" and so on for subsequent devices.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKLhjhhubh)}(h``/dev/uioX`` is used to access the address space of the card. Just use :c:func:`mmap()` to access registers or RAM locations of your card.h](j)}(h ``/dev/uioX``h]h /dev/uioX}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjXubh; is used to access the address space of the card. Just use }(hjXhhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjph]hmmap()}(hjrhhhNhNubah}(h]h ](xrefcc-funceh"]h$]h&]uh1jhjnubah}(h]h ]h"]h$]h&]refdocdriver-api/uio-howto refdomainj}reftypefunc refexplicitrefwarn reftargetmmapuh1hhhhKQhjXubh3 to access registers or RAM locations of your card.}(hjXhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKQhjhhubh)}(hXtInterrupts are handled by reading from ``/dev/uioX``. A blocking :c:func:`read()` from ``/dev/uioX`` will return as soon as an interrupt occurs. You can also use :c:func:`select()` on ``/dev/uioX`` to wait for an interrupt. The integer value read from ``/dev/uioX`` represents the total interrupt count. You can use this number to figure out if you missed some interrupts.h](h'Interrupts are handled by reading from }(hjhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh . A blocking }(hjhhhNhNubh)}(h:c:func:`read()`h]j)}(hjh]hread()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjreaduh1hhhhKThjubh from }(hjhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh> will return as soon as an interrupt occurs. You can also use }(hjhhhNhNubh)}(h:c:func:`select()`h]j)}(hjh]hselect()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjselectuh1hhhhKThjubh on }(hjhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh7 to wait for an interrupt. The integer value read from }(hjhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhk represents the total interrupt count. You can use this number to figure out if you missed some interrupts.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKThjhhubh)}(hXFor some hardware that has more than one interrupt source internally, but not separate IRQ mask and status registers, there might be situations where userspace cannot determine what the interrupt source was if the kernel handler disables them by writing to the chip's IRQ register. In such a case, the kernel has to disable the IRQ completely to leave the chip's register untouched. Now the userspace part can determine the cause of the interrupt, but it cannot re-enable interrupts. Another cornercase is chips where re-enabling interrupts is a read-modify-write operation to a combined IRQ status/acknowledge register. This would be racy if a new interrupt occurred simultaneously.h]hXFor some hardware that has more than one interrupt source internally, but not separate IRQ mask and status registers, there might be situations where userspace cannot determine what the interrupt source was if the kernel handler disables them by writing to the chip’s IRQ register. In such a case, the kernel has to disable the IRQ completely to leave the chip’s register untouched. Now the userspace part can determine the cause of the interrupt, but it cannot re-enable interrupts. Another cornercase is chips where re-enabling interrupts is a read-modify-write operation to a combined IRQ status/acknowledge register. This would be racy if a new interrupt occurred simultaneously.}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK[hjhhubh)}(hXTo address these problems, UIO also implements a write() function. It is normally not used and can be ignored for hardware that has only a single interrupt source or has separate IRQ mask and status registers. If you need it, however, a write to ``/dev/uioX`` will call the :c:func:`irqcontrol()` function implemented by the driver. You have to write a 32-bit value that is usually either 0 or 1 to disable or enable interrupts. If a driver does not implement :c:func:`irqcontrol()`, :c:func:`write()` will return with ``-ENOSYS``.h](hTo address these problems, UIO also implements a write() function. It is normally not used and can be ignored for hardware that has only a single interrupt source or has separate IRQ mask and status registers. If you need it, however, a write to }(hjEhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjEubh will call the }(hjEhhhNhNubh)}(h:c:func:`irqcontrol()`h]j)}(hjah]h irqcontrol()}(hjchhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhj_ubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnj irqcontroluh1hhhhKfhjEubh function implemented by the driver. You have to write a 32-bit value that is usually either 0 or 1 to disable or enable interrupts. If a driver does not implement }(hjEhhhNhNubh)}(h:c:func:`irqcontrol()`h]j)}(hjh]h irqcontrol()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnj irqcontroluh1hhhhKfhjEubh, }(hjEhhhNhNubh)}(h:c:func:`write()`h]j)}(hjh]hwrite()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjwriteuh1hhhhKfhjEubh will return with }(hjEhhhNhNubj)}(h ``-ENOSYS``h]h-ENOSYS}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjEubh.}(hjEhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKfhjhhubh)}(hTo handle interrupts properly, your custom kernel module can provide its own interrupt handler. It will automatically be called by the built-in handler.h]hTo handle interrupts properly, your custom kernel module can provide its own interrupt handler. It will automatically be called by the built-in handler.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKphjhhubh)}(hXFor cards that don't generate interrupts but need to be polled, there is the possibility to set up a timer that triggers the interrupt handler at configurable time intervals. This interrupt simulation is done by calling :c:func:`uio_event_notify()` from the timer's event handler.h](hFor cards that don’t generate interrupts but need to be polled, there is the possibility to set up a timer that triggers the interrupt handler at configurable time intervals. This interrupt simulation is done by calling }(hjhhhNhNubh)}(h:c:func:`uio_event_notify()`h]j)}(hjh]huio_event_notify()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjuio_event_notifyuh1hhhhKthjubh" from the timer’s event handler.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKthjhhubh)}(hXVEach driver provides attributes that are used to read or write variables. These attributes are accessible through sysfs files. A custom kernel driver module can add its own attributes to the device owned by the uio driver, but not added to the UIO device itself at this time. This might change in the future if it would be found to be useful.h]hXVEach driver provides attributes that are used to read or write variables. These attributes are accessible through sysfs files. A custom kernel driver module can add its own attributes to the device owned by the uio driver, but not added to the UIO device itself at this time. This might change in the future if it would be found to be useful.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKzhjhhubh)}(hDThe following standard attributes are provided by the UIO framework:h]hDThe following standard attributes are provided by the UIO framework:}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hhh](j)}(he``name``: The name of your device. It is recommended to use the name of your kernel module for this. h]h)}(hd``name``: The name of your device. It is recommended to use the name of your kernel module for this.h](j)}(h``name``h]hname}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjBubh\: The name of your device. It is recommended to use the name of your kernel module for this.}(hjBhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj>ubah}(h]h ]h"]h$]h&]uh1jhj;hhhhhNubj)}(h``version``: A version string defined by your driver. This allows the user space part of your driver to deal with different versions of the kernel module. h]h)}(h``version``: A version string defined by your driver. This allows the user space part of your driver to deal with different versions of the kernel module.h](j)}(h ``version``h]hversion}(hjlhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjhubh: A version string defined by your driver. This allows the user space part of your driver to deal with different versions of the kernel module.}(hjhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjdubah}(h]h ]h"]h$]h&]uh1jhj;hhhhhNubj)}(hn``event``: The total number of interrupts handled by the driver since the last time the device node was read. h]h)}(hm``event``: The total number of interrupts handled by the driver since the last time the device node was read.h](j)}(h ``event``h]hevent}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhd: The total number of interrupts handled by the driver since the last time the device node was read.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhj;hhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhKhjhhubh)}(hThese attributes appear under the ``/sys/class/uio/uioX`` directory. Please note that this directory might be a symlink, and not a real directory. Any userspace code that accesses it must be able to handle this.h](h"These attributes appear under the }(hjhhhNhNubj)}(h``/sys/class/uio/uioX``h]h/sys/class/uio/uioX}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh directory. Please note that this directory might be a symlink, and not a real directory. Any userspace code that accesses it must be able to handle this.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hEach UIO device can make one or more memory regions available for memory mapping. This is necessary because some industrial I/O cards require access to more than one PCI memory region in a driver.h]hEach UIO device can make one or more memory regions available for memory mapping. This is necessary because some industrial I/O cards require access to more than one PCI memory region in a driver.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hEach mapping has its own directory in sysfs, the first mapping appears as ``/sys/class/uio/uioX/maps/map0/``. Subsequent mappings create directories ``map1/``, ``map2/``, and so on. These directories will only appear if the size of the mapping is not 0.h](hJEach mapping has its own directory in sysfs, the first mapping appears as }(hjhhhNhNubj)}(h"``/sys/class/uio/uioX/maps/map0/``h]h/sys/class/uio/uioX/maps/map0/}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh). Subsequent mappings create directories }(hjhhhNhNubj)}(h ``map1/``h]hmap1/}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, }(hjhhhNhNubj)}(h ``map2/``h]hmap2/}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhT, and so on. These directories will only appear if the size of the mapping is not 0.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hZEach ``mapX/`` directory contains four read-only files that show attributes of the memory:h](hEach }(hj(hhhNhNubj)}(h ``mapX/``h]hmapX/}(hj0hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj(ubhL directory contains four read-only files that show attributes of the memory:}(hj(hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hhh](j)}(h``name``: A string identifier for this mapping. This is optional, the string can be empty. Drivers can set this to make it easier for userspace to find the correct mapping. h]h)}(h``name``: A string identifier for this mapping. This is optional, the string can be empty. Drivers can set this to make it easier for userspace to find the correct mapping.h](j)}(h``name``h]hname}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1jhjOubh: A string identifier for this mapping. This is optional, the string can be empty. Drivers can set this to make it easier for userspace to find the correct mapping.}(hjOhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjKubah}(h]h ]h"]h$]h&]uh1jhjHhhhhhNubj)}(h4``addr``: The address of memory that can be mapped. h]h)}(h3``addr``: The address of memory that can be mapped.h](j)}(h``addr``h]haddr}(hjyhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjuubh+: The address of memory that can be mapped.}(hjuhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjqubah}(h]h ]h"]h$]h&]uh1jhjHhhhhhNubj)}(h@``size``: The size, in bytes, of the memory pointed to by addr. h]h)}(h?``size``: The size, in bytes, of the memory pointed to by addr.h](j)}(h``size``h]hsize}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh7: The size, in bytes, of the memory pointed to by addr.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjHhhhhhNubj)}(hXA``offset``: The offset, in bytes, that has to be added to the pointer returned by :c:func:`mmap()` to get to the actual device memory. This is important if the device's memory is not page aligned. Remember that pointers returned by :c:func:`mmap()` are always page aligned, so it is good style to always add this offset. h]h)}(hX@``offset``: The offset, in bytes, that has to be added to the pointer returned by :c:func:`mmap()` to get to the actual device memory. This is important if the device's memory is not page aligned. Remember that pointers returned by :c:func:`mmap()` are always page aligned, so it is good style to always add this offset.h](j)}(h ``offset``h]hoffset}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhH: The offset, in bytes, that has to be added to the pointer returned by }(hjhhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjh]hmmap()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjmmapuh1hhhhKhjubh to get to the actual device memory. This is important if the device’s memory is not page aligned. Remember that pointers returned by }(hjhhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjh]hmmap()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjmmapuh1hhhhKhjubhH are always page aligned, so it is good style to always add this offset.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjHhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhKhjhhubh)}(hFrom userspace, the different mappings are distinguished by adjusting the ``offset`` parameter of the :c:func:`mmap()` call. To map the memory of mapping N, you have to use N times the page size as your offset::h](hJFrom userspace, the different mappings are distinguished by adjusting the }(hj/hhhNhNubj)}(h ``offset``h]hoffset}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj/ubh parameter of the }(hj/hhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjKh]hmmap()}(hjMhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjIubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjmmapuh1hhhhKhj/ubh\ call. To map the memory of mapping N, you have to use N times the page size as your offset:}(hj/hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh literal_block)}(hoffset = N * getpagesize();h]hoffset = N * getpagesize();}hjtsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1jrhhhKhjhhubh)}(hXhSometimes there is hardware with memory-like regions that can not be mapped with the technique described here, but there are still ways to access them from userspace. The most common example are x86 ioports. On x86 systems, userspace can access these ioports using :c:func:`ioperm()`, :c:func:`iopl()`, :c:func:`inb()`, :c:func:`outb()`, and similar functions.h](hX Sometimes there is hardware with memory-like regions that can not be mapped with the technique described here, but there are still ways to access them from userspace. The most common example are x86 ioports. On x86 systems, userspace can access these ioports using }(hjhhhNhNubh)}(h:c:func:`ioperm()`h]j)}(hjh]hioperm()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjiopermuh1hhhhKhjubh, }(hjhhhNhNubh)}(h:c:func:`iopl()`h]j)}(hjh]hiopl()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjiopluh1hhhhKhjubh, }hjsbh)}(h:c:func:`inb()`h]j)}(hjh]hinb()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjinbuh1hhhhKhjubh, }(hjhhhNhNubh)}(h:c:func:`outb()`h]j)}(hjh]houtb()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjoutbuh1hhhhKhjubh, and similar functions.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hX@Since these ioport regions can not be mapped, they will not appear under ``/sys/class/uio/uioX/maps/`` like the normal memory described above. Without information about the port regions a hardware has to offer, it becomes difficult for the userspace part of the driver to find out which ports belong to which UIO device.h](hISince these ioport regions can not be mapped, they will not appear under }(hjhhhNhNubj)}(h``/sys/class/uio/uioX/maps/``h]h/sys/class/uio/uioX/maps/}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh like the normal memory described above. Without information about the port regions a hardware has to offer, it becomes difficult for the userspace part of the driver to find out which ports belong to which UIO device.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hX>To address this situation, the new directory ``/sys/class/uio/uioX/portio/`` was added. It only exists if the driver wants to pass information about one or more port regions to userspace. If that is the case, subdirectories named ``port0``, ``port1``, and so on, will appear underneath ``/sys/class/uio/uioX/portio/``.h](h-To address this situation, the new directory }(hj>hhhNhNubj)}(h``/sys/class/uio/uioX/portio/``h]h/sys/class/uio/uioX/portio/}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj>ubh was added. It only exists if the driver wants to pass information about one or more port regions to userspace. If that is the case, subdirectories named }(hj>hhhNhNubj)}(h ``port0``h]hport0}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj>ubh, }(hj>hhhNhNubj)}(h ``port1``h]hport1}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj>ubh$, and so on, will appear underneath }(hj>hhhNhNubj)}(h``/sys/class/uio/uioX/portio/``h]h/sys/class/uio/uioX/portio/}(hj|hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj>ubh.}(hj>hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hqEach ``portX/`` directory contains four read-only files that show name, start, size, and type of the port region:h](hEach }(hjhhhNhNubj)}(h ``portX/``h]hportX/}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhb directory contains four read-only files that show name, start, size, and type of the port region:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hhh](j)}(h``name``: A string identifier for this port region. The string is optional and can be empty. Drivers can set it to make it easier for userspace to find a certain port region. h]h)}(h``name``: A string identifier for this port region. The string is optional and can be empty. Drivers can set it to make it easier for userspace to find a certain port region.h](j)}(h``name``h]hname}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh: A string identifier for this port region. The string is optional and can be empty. Drivers can set it to make it easier for userspace to find a certain port region.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h*``start``: The first port of this region. h]h)}(h)``start``: The first port of this region.h](j)}(h ``start``h]hstart}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh : The first port of this region.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h.``size``: The number of ports in this region. h]h)}(h-``size``: The number of ports in this region.h](j)}(h``size``h]hsize}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh%: The number of ports in this region.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h4``porttype``: A string describing the type of port. h]h)}(h3``porttype``: A string describing the type of port.h](j)}(h ``porttype``h]hporttype}(hj1 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj- ubh': A string describing the type of port.}(hj- hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj) ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhKhjhhubeh}(h] how-uio-worksah ]h"] how uio worksah$]h&]uh1hhjyhhhhhKJubeh}(h] about-uioah ]h"] about uioah$]h&]uh1hhhhhhhhKint (*mmap)(struct uio_info *info, struct vm_area_struct *vma)}(hjz hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjv ubh": Optional. If you need a special }(hjv hhhNhNubh)}(h:c:func:`mmap()`h]j)}(hj h]hmmap()}(hj hhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjmmapuh1hhhhKhjv ubhB function, you can set it here. If this pointer is not NULL, your }(hjv hhhNhNubh)}(h:c:func:`mmap()`h]j)}(hj h]hmmap()}(hj hhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjmmapuh1hhhhKhjv ubh, will be called instead of the built-in one.}(hjv hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjr ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(h``int (*open)(struct uio_info *info, struct inode *inode)``: Optional. You might want to have your own :c:func:`open()`, e.g. to enable interrupts only when your device is actually used. h]h)}(h``int (*open)(struct uio_info *info, struct inode *inode)``: Optional. You might want to have your own :c:func:`open()`, e.g. to enable interrupts only when your device is actually used.h](j)}(h;``int (*open)(struct uio_info *info, struct inode *inode)``h]h7int (*open)(struct uio_info *info, struct inode *inode)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh,: Optional. You might want to have your own }(hj hhhNhNubh)}(h:c:func:`open()`h]j)}(hj h]hopen()}(hj hhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjopenuh1hhhhKhj ubhC, e.g. to enable interrupts only when your device is actually used.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(h``int (*release)(struct uio_info *info, struct inode *inode)``: Optional. If you define your own :c:func:`open()`, you will probably also want a custom :c:func:`release()` function. h]h)}(h``int (*release)(struct uio_info *info, struct inode *inode)``: Optional. If you define your own :c:func:`open()`, you will probably also want a custom :c:func:`release()` function.h](j)}(h>``int (*release)(struct uio_info *info, struct inode *inode)``h]h:int (*release)(struct uio_info *info, struct inode *inode)}(hj/ hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ ubh#: Optional. If you define your own }(hj+ hhhNhNubh)}(h:c:func:`open()`h]j)}(hjC h]hopen()}(hjE hhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjA ubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjopenuh1hhhhMhj+ ubh', you will probably also want a custom }(hj+ hhhNhNubh)}(h:c:func:`release()`h]j)}(hjf h]h release()}(hjh hhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjd ubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjreleaseuh1hhhhMhj+ ubh function.}(hj+ hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj' ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(hX``int (*irqcontrol)(struct uio_info *info, s32 irq_on)``: Optional. If you need to be able to enable or disable interrupts from userspace by writing to ``/dev/uioX``, you can implement this function. The parameter ``irq_on`` will be 0 to disable interrupts and 1 to enable them. h]h)}(hX``int (*irqcontrol)(struct uio_info *info, s32 irq_on)``: Optional. If you need to be able to enable or disable interrupts from userspace by writing to ``/dev/uioX``, you can implement this function. The parameter ``irq_on`` will be 0 to disable interrupts and 1 to enable them.h](j)}(h8``int (*irqcontrol)(struct uio_info *info, s32 irq_on)``h]h4int (*irqcontrol)(struct uio_info *info, s32 irq_on)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh`: Optional. If you need to be able to enable or disable interrupts from userspace by writing to }(hj hhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh1, you can implement this function. The parameter }(hj hhhNhNubj)}(h ``irq_on``h]hirq_on}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh6 will be 0 to disable interrupts and 1 to enable them.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhKhj hhubh)}(hUsually, your device will have one or more memory regions that can be mapped to user space. For each region, you have to set up a ``struct uio_mem`` in the ``mem[]`` array. Here's a description of the fields of ``struct uio_mem``:h](hUsually, your device will have one or more memory regions that can be mapped to user space. For each region, you have to set up a }(hj hhhNhNubj)}(h``struct uio_mem``h]hstruct uio_mem}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh in the }(hj hhhNhNubj)}(h ``mem[]``h]hmem[]}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh0 array. Here’s a description of the fields of }(hj hhhNhNubj)}(h``struct uio_mem``h]hstruct uio_mem}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh:}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj hhubj)}(hhh](j)}(h~``const char *name``: Optional. Set this to help identify the memory region, it will show up in the corresponding sysfs node. h]h)}(h}``const char *name``: Optional. Set this to help identify the memory region, it will show up in the corresponding sysfs node.h](j)}(h``const char *name``h]hconst char *name}(hj2 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj. ubhi: Optional. Set this to help identify the memory region, it will show up in the corresponding sysfs node.}(hj. hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj* ubah}(h]h ]h"]h$]h&]uh1jhj' hhhhhNubj)}(hX,``int memtype``: Required if the mapping is used. Set this to ``UIO_MEM_PHYS`` if you have physical memory on your card to be mapped. Use ``UIO_MEM_LOGICAL`` for logical memory (e.g. allocated with :c:func:`__get_free_pages()` but not kmalloc()). There's also ``UIO_MEM_VIRTUAL`` for virtual memory. h]h)}(hX+``int memtype``: Required if the mapping is used. Set this to ``UIO_MEM_PHYS`` if you have physical memory on your card to be mapped. Use ``UIO_MEM_LOGICAL`` for logical memory (e.g. allocated with :c:func:`__get_free_pages()` but not kmalloc()). There's also ``UIO_MEM_VIRTUAL`` for virtual memory.h](j)}(h``int memtype``h]h int memtype}(hjX hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjT ubh/: Required if the mapping is used. Set this to }(hjT hhhNhNubj)}(h``UIO_MEM_PHYS``h]h UIO_MEM_PHYS}(hjj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjT ubh< if you have physical memory on your card to be mapped. Use }(hjT hhhNhNubj)}(h``UIO_MEM_LOGICAL``h]hUIO_MEM_LOGICAL}(hj| hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjT ubh) for logical memory (e.g. allocated with }(hjT hhhNhNubh)}(h:c:func:`__get_free_pages()`h]j)}(hj h]h__get_free_pages()}(hj hhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnj__get_free_pagesuh1hhhhMhjT ubh$ but not kmalloc()). There’s also }(hjT hhhNhNubj)}(h``UIO_MEM_VIRTUAL``h]hUIO_MEM_VIRTUAL}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjT ubh for virtual memory.}(hjT hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjP ubah}(h]h ]h"]h$]h&]uh1jhj' hhhhhNubj)}(h``phys_addr_t addr``: Required if the mapping is used. Fill in the address of your memory block. This address is the one that appears in sysfs. h]h)}(h``phys_addr_t addr``: Required if the mapping is used. Fill in the address of your memory block. This address is the one that appears in sysfs.h](j)}(h``phys_addr_t addr``h]hphys_addr_t addr}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh{: Required if the mapping is used. Fill in the address of your memory block. This address is the one that appears in sysfs.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj' hhhhhNubj)}(h``resource_size_t size``: Fill in the size of the memory block that ``addr`` points to. If ``size`` is zero, the mapping is considered unused. Note that you *must* initialize ``size`` with zero for all unused mappings. h]h)}(h``resource_size_t size``: Fill in the size of the memory block that ``addr`` points to. If ``size`` is zero, the mapping is considered unused. Note that you *must* initialize ``size`` with zero for all unused mappings.h](j)}(h``resource_size_t size``h]hresource_size_t size}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh,: Fill in the size of the memory block that }(hj hhhNhNubj)}(h``addr``h]haddr}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh points to. If }(hj hhhNhNubj)}(h``size``h]hsize}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh: is zero, the mapping is considered unused. Note that you }(hj hhhNhNubhemphasis)}(h*must*h]hmust}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1j3hj ubh initialize }(hj hhhNhNubj)}(h``size``h]hsize}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh# with zero for all unused mappings.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj' hhhhhNubj)}(hXW``void *internal_addr``: If you have to access this memory region from within your kernel module, you will want to map it internally by using something like :c:func:`ioremap()`. Addresses returned by this function cannot be mapped to user space, so you must not store it in ``addr``. Use ``internal_addr`` instead to remember such an address. h]h)}(hXV``void *internal_addr``: If you have to access this memory region from within your kernel module, you will want to map it internally by using something like :c:func:`ioremap()`. Addresses returned by this function cannot be mapped to user space, so you must not store it in ``addr``. Use ``internal_addr`` instead to remember such an address.h](j)}(h``void *internal_addr``h]hvoid *internal_addr}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjiubh: If you have to access this memory region from within your kernel module, you will want to map it internally by using something like }(hjihhhNhNubh)}(h:c:func:`ioremap()`h]j)}(hjh]h ioremap()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjioremapuh1hhhhM#hjiubhb. Addresses returned by this function cannot be mapped to user space, so you must not store it in }(hjihhhNhNubj)}(h``addr``h]haddr}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjiubh. Use }(hjihhhNhNubj)}(h``internal_addr``h]h internal_addr}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjiubh% instead to remember such an address.}(hjihhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM#hjeubah}(h]h ]h"]h$]h&]uh1jhj' hhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhMhj hhubh)}(hPlease do not touch the ``map`` element of ``struct uio_mem``! It is used by the UIO framework to set up sysfs files for this mapping. Simply leave it alone.h](hPlease do not touch the }(hjhhhNhNubj)}(h``map``h]hmap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh element of }(hjhhhNhNubj)}(h``struct uio_mem``h]hstruct uio_mem}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh`! It is used by the UIO framework to set up sysfs files for this mapping. Simply leave it alone.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM*hj hhubh)}(hX}Sometimes, your device can have one or more port regions which can not be mapped to userspace. But if there are other possibilities for userspace to access these ports, it makes sense to make information about the ports available in sysfs. For each region, you have to set up a ``struct uio_port`` in the ``port[]`` array. Here's a description of the fields of ``struct uio_port``:h](hXSometimes, your device can have one or more port regions which can not be mapped to userspace. But if there are other possibilities for userspace to access these ports, it makes sense to make information about the ports available in sysfs. For each region, you have to set up a }(hj hhhNhNubj)}(h``struct uio_port``h]hstruct uio_port}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh in the }(hj hhhNhNubj)}(h ``port[]``h]hport[]}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh0 array. Here’s a description of the fields of }(hj hhhNhNubj)}(h``struct uio_port``h]hstruct uio_port}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh:}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM.hj hhubj)}(hhh](j)}(h``char *porttype``: Required. Set this to one of the predefined constants. Use ``UIO_PORT_X86`` for the ioports found in x86 architectures. h]h)}(h``char *porttype``: Required. Set this to one of the predefined constants. Use ``UIO_PORT_X86`` for the ioports found in x86 architectures.h](j)}(h``char *porttype``h]hchar *porttype}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh=: Required. Set this to one of the predefined constants. Use }(hjUhhhNhNubj)}(h``UIO_PORT_X86``h]h UIO_PORT_X86}(hjkhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh, for the ioports found in x86 architectures.}(hjUhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM5hjQubah}(h]h ]h"]h$]h&]uh1jhjNhhhhhNubj)}(hs``unsigned long start``: Required if the port region is used. Fill in the number of the first port of this region. h]h)}(hr``unsigned long start``: Required if the port region is used. Fill in the number of the first port of this region.h](j)}(h``unsigned long start``h]hunsigned long start}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh[: Required if the port region is used. Fill in the number of the first port of this region.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM9hjubah}(h]h ]h"]h$]h&]uh1jhjNhhhhhNubj)}(h``unsigned long size``: Fill in the number of ports in this region. If ``size`` is zero, the region is considered unused. Note that you *must* initialize ``size`` with zero for all unused regions. h]h)}(h``unsigned long size``: Fill in the number of ports in this region. If ``size`` is zero, the region is considered unused. Note that you *must* initialize ``size`` with zero for all unused regions.h](j)}(h``unsigned long size``h]hunsigned long size}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh1: Fill in the number of ports in this region. If }(hjhhhNhNubj)}(h``size``h]hsize}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh9 is zero, the region is considered unused. Note that you }(hjhhhNhNubj4)}(h*must*h]hmust}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j3hjubh initialize }(hjhhhNhNubj)}(h``size``h]hsize}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh" with zero for all unused regions.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM<hjubah}(h]h ]h"]h$]h&]uh1jhjNhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhM5hj hhubh)}(hPlease do not touch the ``portio`` element of ``struct uio_port``! It is used internally by the UIO framework to set up sysfs files for this region. Simply leave it alone.h](hPlease do not touch the }(hjhhhNhNubj)}(h ``portio``h]hportio}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh element of }(hjhhhNhNubj)}(h``struct uio_port``h]hstruct uio_port}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhj! It is used internally by the UIO framework to set up sysfs files for this region. Simply leave it alone.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM@hj hhubeh}(h]struct-uio-infoah ]h"]struct uio_infoah$]h&]uh1hhje hhhhhKubh)}(hhh](h)}(hAdding an interrupt handlerh]hAdding an interrupt handler}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjKhhhhhMEubh)}(hX-What you need to do in your interrupt handler depends on your hardware and on how you want to handle it. You should try to keep the amount of code in your kernel interrupt handler low. If your hardware requires no action that you *have* to perform after each interrupt, then your handler can be empty.h](hWhat you need to do in your interrupt handler depends on your hardware and on how you want to handle it. You should try to keep the amount of code in your kernel interrupt handler low. If your hardware requires no action that you }(hj\hhhNhNubj4)}(h*have*h]hhave}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1j3hj\ubhA to perform after each interrupt, then your handler can be empty.}(hj\hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMGhjKhhubh)}(hXZIf, on the other hand, your hardware *needs* some action to be performed after each interrupt, then you *must* do it in your kernel module. Note that you cannot rely on the userspace part of your driver. Your userspace program can terminate at any time, possibly leaving your hardware in a state where proper interrupt handling is still required.h](h%If, on the other hand, your hardware }(hj|hhhNhNubj4)}(h*needs*h]hneeds}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j3hj|ubh< some action to be performed after each interrupt, then you }(hj|hhhNhNubj4)}(h*must*h]hmust}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j3hj|ubh do it in your kernel module. Note that you cannot rely on the userspace part of your driver. Your userspace program can terminate at any time, possibly leaving your hardware in a state where proper interrupt handling is still required.}(hj|hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMMhjKhhubh)}(hXThere might also be applications where you want to read data from your hardware at each interrupt and buffer it in a piece of kernel memory you've allocated for that purpose. With this technique you could avoid loss of data if your userspace program misses an interrupt.h]hXThere might also be applications where you want to read data from your hardware at each interrupt and buffer it in a piece of kernel memory you’ve allocated for that purpose. With this technique you could avoid loss of data if your userspace program misses an interrupt.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMShjKhhubh)}(hXDA note on shared interrupts: Your driver should support interrupt sharing whenever this is possible. It is possible if and only if your driver can detect whether your hardware has triggered the interrupt or not. This is usually done by looking at an interrupt status register. If your driver sees that the IRQ bit is actually set, it will perform its actions, and the handler returns IRQ_HANDLED. If the driver detects that it was not your hardware that caused the interrupt, it will do nothing and return IRQ_NONE, allowing the kernel to call the next possible interrupt handler.h]hXDA note on shared interrupts: Your driver should support interrupt sharing whenever this is possible. It is possible if and only if your driver can detect whether your hardware has triggered the interrupt or not. This is usually done by looking at an interrupt status register. If your driver sees that the IRQ bit is actually set, it will perform its actions, and the handler returns IRQ_HANDLED. If the driver detects that it was not your hardware that caused the interrupt, it will do nothing and return IRQ_NONE, allowing the kernel to call the next possible interrupt handler.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMXhjKhhubh)}(hIf you decide not to support shared interrupts, your card won't work in computers with no free interrupts. As this frequently happens on the PC platform, you can save yourself a lot of trouble by supporting interrupt sharing.h]hIf you decide not to support shared interrupts, your card won’t work in computers with no free interrupts. As this frequently happens on the PC platform, you can save yourself a lot of trouble by supporting interrupt sharing.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMbhjKhhubeh}(h]adding-an-interrupt-handlerah ]h"]adding an interrupt handlerah$]h&]uh1hhje hhhhhMEubh)}(hhh](h)}(h#Using uio_pdrv for platform devicesh]h#Using uio_pdrv for platform devices}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMhubh)}(hXJIn many cases, UIO drivers for platform devices can be handled in a generic way. In the same place where you define your ``struct platform_device``, you simply also implement your interrupt handler and fill your ``struct uio_info``. A pointer to this ``struct uio_info`` is then used as ``platform_data`` for your platform device.h](hyIn many cases, UIO drivers for platform devices can be handled in a generic way. In the same place where you define your }(hjhhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhA, you simply also implement your interrupt handler and fill your }(hjhhhNhNubj)}(h``struct uio_info``h]hstruct uio_info}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. A pointer to this }(hjhhhNhNubj)}(h``struct uio_info``h]hstruct uio_info}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh is then used as }(hjhhhNhNubj)}(h``platform_data``h]h platform_data}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh for your platform device.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMjhjhhubh)}(hYou also need to set up an array of ``struct resource`` containing addresses and sizes of your memory mappings. This information is passed to the driver using the ``.resource`` and ``.num_resources`` elements of ``struct platform_device``.h](h$You also need to set up an array of }(hjGhhhNhNubj)}(h``struct resource``h]hstruct resource}(hjOhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjGubhl containing addresses and sizes of your memory mappings. This information is passed to the driver using the }(hjGhhhNhNubj)}(h ``.resource``h]h .resource}(hjahhhNhNubah}(h]h ]h"]h$]h&]uh1jhjGubh and }(hjGhhhNhNubj)}(h``.num_resources``h]h.num_resources}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1jhjGubh elements of }(hjGhhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjGubh.}(hjGhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMqhjhhubh)}(hYou now have to set the ``.name`` element of ``struct platform_device`` to ``"uio_pdrv"`` to use the generic UIO platform device driver. This driver will fill the ``mem[]`` array according to the resources given, and register the device.h](hYou now have to set the }(hjhhhNhNubj)}(h ``.name``h]h.name}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh element of }(hjhhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh to }(hjhhhNhNubj)}(h``"uio_pdrv"``h]h "uio_pdrv"}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhJ to use the generic UIO platform device driver. This driver will fill the }(hjhhhNhNubj)}(h ``mem[]``h]hmem[]}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhA array according to the resources given, and register the device.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMvhjhhubh)}(hThe advantage of this approach is that you only have to edit a file you need to edit anyway. You do not have to create an extra driver.h]hThe advantage of this approach is that you only have to edit a file you need to edit anyway. You do not have to create an extra driver.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM{hjhhubeh}(h]#using-uio-pdrv-for-platform-devicesah ]h"]#using uio_pdrv for platform devicesah$]h&]uh1hhje hhhhhMhubh)}(hhh](h)}(h*Using uio_pdrv_genirq for platform devicesh]h*Using uio_pdrv_genirq for platform devices}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hXEEspecially in embedded devices, you frequently find chips where the irq pin is tied to its own dedicated interrupt line. In such cases, where you can be really sure the interrupt is not shared, we can take the concept of ``uio_pdrv`` one step further and use a generic interrupt handler. That's what ``uio_pdrv_genirq`` does.h](hEspecially in embedded devices, you frequently find chips where the irq pin is tied to its own dedicated interrupt line. In such cases, where you can be really sure the interrupt is not shared, we can take the concept of }(hjhhhNhNubj)}(h ``uio_pdrv``h]huio_pdrv}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhE one step further and use a generic interrupt handler. That’s what }(hjhhhNhNubj)}(h``uio_pdrv_genirq``h]huio_pdrv_genirq}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh does.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXThe setup for this driver is the same as described above for ``uio_pdrv``, except that you do not implement an interrupt handler. The ``.handler`` element of ``struct uio_info`` must remain ``NULL``. The ``.irq_flags`` element must not contain ``IRQF_SHARED``.h](h=The setup for this driver is the same as described above for }(hjLhhhNhNubj)}(h ``uio_pdrv``h]huio_pdrv}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh=, except that you do not implement an interrupt handler. The }(hjLhhhNhNubj)}(h ``.handler``h]h.handler}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh element of }(hjLhhhNhNubj)}(h``struct uio_info``h]hstruct uio_info}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh must remain }(hjLhhhNhNubj)}(h``NULL``h]hNULL}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh. The }(hjLhhhNhNubj)}(h``.irq_flags``h]h .irq_flags}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh element must not contain }(hjLhhhNhNubj)}(h``IRQF_SHARED``h]h IRQF_SHARED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh.}(hjLhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hmYou will set the ``.name`` element of ``struct platform_device`` to ``"uio_pdrv_genirq"`` to use this driver.h](hYou will set the }(hjhhhNhNubj)}(h ``.name``h]h.name}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh element of }(hjhhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh to }(hjhhhNhNubj)}(h``"uio_pdrv_genirq"``h]h"uio_pdrv_genirq"}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh to use this driver.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hX[The generic interrupt handler of ``uio_pdrv_genirq`` will simply disable the interrupt line using :c:func:`disable_irq_nosync()`. After doing its work, userspace can reenable the interrupt by writing 0x00000001 to the UIO device file. The driver already implements an :c:func:`irq_control()` to make this possible, you must not implement your own.h](h!The generic interrupt handler of }(hj hhhNhNubj)}(h``uio_pdrv_genirq``h]huio_pdrv_genirq}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh. will simply disable the interrupt line using }(hj hhhNhNubh)}(h:c:func:`disable_irq_nosync()`h]j)}(hj&h]hdisable_irq_nosync()}(hj(hhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhj$ubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjdisable_irq_nosyncuh1hhhhMhj ubh. After doing its work, userspace can reenable the interrupt by writing 0x00000001 to the UIO device file. The driver already implements an }(hj hhhNhNubh)}(h:c:func:`irq_control()`h]j)}(hjIh]h irq_control()}(hjKhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjGubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnj irq_controluh1hhhhMhj ubh8 to make this possible, you must not implement your own.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXUsing ``uio_pdrv_genirq`` not only saves a few lines of interrupt handler code. You also do not need to know anything about the chip's internal registers to create the kernel part of the driver. All you need to know is the irq number of the pin the chip is connected to.h](hUsing }(hjphhhNhNubj)}(h``uio_pdrv_genirq``h]huio_pdrv_genirq}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjpubh not only saves a few lines of interrupt handler code. You also do not need to know anything about the chip’s internal registers to create the kernel part of the driver. All you need to know is the irq number of the pin the chip is connected to.}(hjphhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXWhen used in a device-tree enabled system, the driver needs to be probed with the ``"of_id"`` module parameter set to the ``"compatible"`` string of the node the driver is supposed to handle. By default, the node's name (without the unit address) is exposed as name for the UIO device in userspace. To set a custom name, a property named ``"linux,uio-name"`` may be specified in the DT node.h](hRWhen used in a device-tree enabled system, the driver needs to be probed with the }(hjhhhNhNubj)}(h ``"of_id"``h]h"of_id"}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh module parameter set to the }(hjhhhNhNubj)}(h``"compatible"``h]h "compatible"}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh string of the node the driver is supposed to handle. By default, the node’s name (without the unit address) is exposed as name for the UIO device in userspace. To set a custom name, a property named }(hjhhhNhNubj)}(h``"linux,uio-name"``h]h"linux,uio-name"}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh! may be specified in the DT node.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]*using-uio-pdrv-genirq-for-platform-devicesah ]h"]*using uio_pdrv_genirq for platform devicesah$]h&]uh1hhje hhhhhMubh)}(hhh](h)}(h*Using uio_dmem_genirq for platform devicesh]h*Using uio_dmem_genirq for platform devices}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hX>In addition to statically allocated memory ranges, they may also be a desire to use dynamically allocated regions in a user space driver. In particular, being able to access memory made available through the dma-mapping API, may be particularly useful. The ``uio_dmem_genirq`` driver provides a way to accomplish this.h](hXIn addition to statically allocated memory ranges, they may also be a desire to use dynamically allocated regions in a user space driver. In particular, being able to access memory made available through the dma-mapping API, may be particularly useful. The }(hjhhhNhNubj)}(h``uio_dmem_genirq``h]huio_dmem_genirq}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh* driver provides a way to accomplish this.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hThis driver is used in a similar manner to the ``"uio_pdrv_genirq"`` driver with respect to interrupt configuration and handling.h](h/This driver is used in a similar manner to the }(hj hhhNhNubj)}(h``"uio_pdrv_genirq"``h]h"uio_pdrv_genirq"}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh= driver with respect to interrupt configuration and handling.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hdSet the ``.name`` element of ``struct platform_device`` to ``"uio_dmem_genirq"`` to use this driver.h](hSet the }(hj-hhhNhNubj)}(h ``.name``h]h.name}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-ubh element of }(hj-hhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-ubh to }(hj-hhhNhNubj)}(h``"uio_dmem_genirq"``h]h"uio_dmem_genirq"}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-ubh to use this driver.}(hj-hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hWhen using this driver, fill in the ``.platform_data`` element of ``struct platform_device``, which is of type ``struct uio_dmem_genirq_pdata`` and which contains the following elements:h](h$When using this driver, fill in the }(hjqhhhNhNubj)}(h``.platform_data``h]h.platform_data}(hjyhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjqubh element of }(hjqhhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjqubh, which is of type }(hjqhhhNhNubj)}(h ``struct uio_dmem_genirq_pdata``h]hstruct uio_dmem_genirq_pdata}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjqubh+ and which contains the following elements:}(hjqhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hhh](j)}(h^``struct uio_info uioinfo``: The same structure used as the ``uio_pdrv_genirq`` platform data h]h)}(h]``struct uio_info uioinfo``: The same structure used as the ``uio_pdrv_genirq`` platform datah](j)}(h``struct uio_info uioinfo``h]hstruct uio_info uioinfo}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh!: The same structure used as the }(hjhhhNhNubj)}(h``uio_pdrv_genirq``h]huio_pdrv_genirq}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh platform data}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hy``unsigned int *dynamic_region_sizes``: Pointer to list of sizes of dynamic memory regions to be mapped into user space. h]h)}(hx``unsigned int *dynamic_region_sizes``: Pointer to list of sizes of dynamic memory regions to be mapped into user space.h](j)}(h&``unsigned int *dynamic_region_sizes``h]h"unsigned int *dynamic_region_sizes}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhR: Pointer to list of sizes of dynamic memory regions to be mapped into user space.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h\``unsigned int num_dynamic_regions``: Number of elements in ``dynamic_region_sizes`` array. h]h)}(h[``unsigned int num_dynamic_regions``: Number of elements in ``dynamic_region_sizes`` array.h](j)}(h$``unsigned int num_dynamic_regions``h]h unsigned int num_dynamic_regions}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh: Number of elements in }(hjhhhNhNubj)}(h``dynamic_region_sizes``h]hdynamic_region_sizes}(hj0hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh array.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hThe dynamic regions defined in the platform data will be appended to the `` mem[] `` array after the platform device resources, which implies that the total number of static and dynamic memory regions cannot exceed ``MAX_UIO_MAPS``.h](hThe dynamic regions defined in the platform data will be appended to the `` mem[] `` array after the platform device resources, which implies that the total number of static and dynamic memory regions cannot exceed }(hjThhhNhNubj)}(h``MAX_UIO_MAPS``h]h MAX_UIO_MAPS}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubh.}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hXThe dynamic memory regions will be allocated when the UIO device file, ``/dev/uioX`` is opened. Similar to static memory resources, the memory region information for dynamic regions is then visible via sysfs at ``/sys/class/uio/uioX/maps/mapY/*``. The dynamic memory regions will be freed when the UIO device file is closed. When no processes are holding the device file open, the address returned to userspace is ~0.h](hGThe dynamic memory regions will be allocated when the UIO device file, }(hjthhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hj|hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjtubh is opened. Similar to static memory resources, the memory region information for dynamic regions is then visible via sysfs at }(hjthhhNhNubj)}(h#``/sys/class/uio/uioX/maps/mapY/*``h]h/sys/class/uio/uioX/maps/mapY/*}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjtubh. The dynamic memory regions will be freed when the UIO device file is closed. When no processes are holding the device file open, the address returned to userspace is ~0.}(hjthhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]*using-uio-dmem-genirq-for-platform-devicesah ]h"]*using uio_dmem_genirq for platform devicesah$]h&]uh1hhje hhhhhMubeh}(h]writing-your-own-kernel-moduleah ]h"]writing your own kernel moduleah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hWriting a driver in userspaceh]hWriting a driver in userspace}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hX^Once you have a working kernel module for your hardware, you can write the userspace part of your driver. You don't need any special libraries, your driver can be written in any reasonable language, you can use floating point numbers and so on. In short, you can use all the tools and libraries you'd normally use for writing a userspace application.h]hXbOnce you have a working kernel module for your hardware, you can write the userspace part of your driver. You don’t need any special libraries, your driver can be written in any reasonable language, you can use floating point numbers and so on. In short, you can use all the tools and libraries you’d normally use for writing a userspace application.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hhh](h)}(h)Getting information about your UIO deviceh]h)Getting information about your UIO device}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hInformation about all UIO devices is available in sysfs. The first thing you should do in your driver is check ``name`` and ``version`` to make sure you're talking to the right device and that its kernel driver has the version you expect.h](hoInformation about all UIO devices is available in sysfs. The first thing you should do in your driver is check }(hjhhhNhNubj)}(h``name``h]hname}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh and }(hjhhhNhNubj)}(h ``version``h]hversion}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhi to make sure you’re talking to the right device and that its kernel driver has the version you expect.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h^You should also make sure that the memory mapping you need exists and has the size you expect.h]h^You should also make sure that the memory mapping you need exists and has the size you expect.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hcThere is a tool called ``lsuio`` that lists UIO devices and their attributes. It is available here:h](hThere is a tool called }(hj&hhhNhNubj)}(h ``lsuio``h]hlsuio}(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubhC that lists UIO devices and their attributes. It is available here:}(hj&hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h1http://www.osadl.org/projects/downloads/UIO/user/h]jR)}(hjHh]h1http://www.osadl.org/projects/downloads/UIO/user/}(hjJhhhNhNubah}(h]h ]h"]h$]h&]refurijHuh1jQhjFubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hWith ``lsuio`` you can quickly check if your kernel module is loaded and which attributes it exports. Have a look at the manpage for details.h](hWith }(hj^hhhNhNubj)}(h ``lsuio``h]hlsuio}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj^ubh you can quickly check if your kernel module is loaded and which attributes it exports. Have a look at the manpage for details.}(hj^hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hThe source code of ``lsuio`` can serve as an example for getting information about an UIO device. The file ``uio_helper.c`` contains a lot of functions you could use in your userspace driver code.h](hThe source code of }(hj~hhhNhNubj)}(h ``lsuio``h]hlsuio}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj~ubhO can serve as an example for getting information about an UIO device. The file }(hj~hhhNhNubj)}(h``uio_helper.c``h]h uio_helper.c}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj~ubhI contains a lot of functions you could use in your userspace driver code.}(hj~hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h])getting-information-about-your-uio-deviceah ]h"])getting information about your uio deviceah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(hmmap() device memoryh]hmmap() device memory}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hAfter you made sure you've got the right device with the memory mappings you need, all you have to do is to call :c:func:`mmap()` to map the device's memory to userspace.h](hsAfter you made sure you’ve got the right device with the memory mappings you need, all you have to do is to call }(hjhhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjh]hmmap()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjmmapuh1hhhhMhjubh+ to map the device’s memory to userspace.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hThe parameter ``offset`` of the :c:func:`mmap()` call has a special meaning for UIO devices: It is used to select which mapping of your device you want to map. To map the memory of mapping N, you have to use N times the page size as your offset::h](hThe parameter }(hjhhhNhNubj)}(h ``offset``h]hoffset}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh of the }(hjhhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjh]hmmap()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjmmapuh1hhhhMhjubh call has a special meaning for UIO devices: It is used to select which mapping of your device you want to map. To map the memory of mapping N, you have to use N times the page size as your offset:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubjs)}(hoffset = N * getpagesize();h]hoffset = N * getpagesize();}hj=sbah}(h]h ]h"]h$]h&]jjuh1jrhhhMhjhhubh)}(hN starts from zero, so if you've got only one memory range to map, set ``offset = 0``. A drawback of this technique is that memory is always mapped beginning with its start address.h](hIN starts from zero, so if you’ve got only one memory range to map, set }(hjKhhhNhNubj)}(h``offset = 0``h]h offset = 0}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1jhjKubh`. A drawback of this technique is that memory is always mapped beginning with its start address.}(hjKhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]mmap-device-memoryah ]h"]mmap() device memoryah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(hWaiting for interruptsh]hWaiting for interrupts}(hjvhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjshhhhhMubh)}(hX9After you successfully mapped your devices memory, you can access it like an ordinary array. Usually, you will perform some initialization. After that, your hardware starts working and will generate an interrupt as soon as it's finished, has some data available, or needs your attention because an error occurred.h]hX;After you successfully mapped your devices memory, you can access it like an ordinary array. Usually, you will perform some initialization. After that, your hardware starts working and will generate an interrupt as soon as it’s finished, has some data available, or needs your attention because an error occurred.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjshhubh)}(hX``/dev/uioX`` is a read-only file. A :c:func:`read()` will always block until an interrupt occurs. There is only one legal value for the ``count`` parameter of :c:func:`read()`, and that is the size of a signed 32 bit integer (4). Any other value for ``count`` causes :c:func:`read()` to fail. The signed 32 bit integer read is the interrupt count of your device. If the value is one more than the value you read the last time, everything is OK. If the difference is greater than one, you missed interrupts.h](j)}(h ``/dev/uioX``h]h /dev/uioX}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh is a read-only file. A }(hjhhhNhNubh)}(h:c:func:`read()`h]j)}(hjh]hread()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjreaduh1hhhhMhjubhT will always block until an interrupt occurs. There is only one legal value for the }(hjhhhNhNubj)}(h ``count``h]hcount}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh parameter of }(hjhhhNhNubh)}(h:c:func:`read()`h]j)}(hjh]hread()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjreaduh1hhhhMhjubhK, and that is the size of a signed 32 bit integer (4). Any other value for }(hjhhhNhNubj)}(h ``count``h]hcount}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh causes }(hjhhhNhNubh)}(h:c:func:`read()`h]j)}(hjh]hread()}(hjhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjreaduh1hhhhMhjubh to fail. The signed 32 bit integer read is the interrupt count of your device. If the value is one more than the value you read the last time, everything is OK. If the difference is greater than one, you missed interrupts.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjshhubh)}(h5You can also use :c:func:`select()` on ``/dev/uioX``.h](hYou can also use }(hj;hhhNhNubh)}(h:c:func:`select()`h]j)}(hjEh]hselect()}(hjGhhhNhNubah}(h]h ](j|j}c-funceh"]h$]h&]uh1jhjCubah}(h]h ]h"]h$]h&]refdocj refdomainj}reftypefunc refexplicitrefwarnjselectuh1hhhhMhj;ubh on }(hj;hhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj;ubh.}(hj;hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjshhubeh}(h]waiting-for-interruptsah ]h"]waiting for interruptsah$]h&]uh1hhjhhhhhMubeh}(h]writing-a-driver-in-userspaceah ]h"]writing a driver in userspaceah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hGeneric PCI UIO driverh]hGeneric PCI UIO driver}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hXThe generic driver is a kernel module named uio_pci_generic. It can work with any device compliant to PCI 2.3 (circa 2002) and any compliant PCI Express device. Using this, you only need to write the userspace driver, removing the need to write a hardware-specific kernel module.h]hXThe generic driver is a kernel module named uio_pci_generic. It can work with any device compliant to PCI 2.3 (circa 2002) and any compliant PCI Express device. Using this, you only need to write the userspace driver, removing the need to write a hardware-specific kernel module.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hhh](h)}(h&Making the driver recognize the deviceh]h&Making the driver recognize the device}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hSince the driver does not declare any device ids, it will not get loaded automatically and will not automatically bind to any devices, you must load it and allocate id to the driver yourself. For example::h]hSince the driver does not declare any device ids, it will not get loaded automatically and will not automatically bind to any devices, you must load it and allocate id to the driver yourself. For example:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubjs)}(hWmodprobe uio_pci_generic echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_idh]hWmodprobe uio_pci_generic echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id}hjsbah}(h]h ]h"]h$]h&]jjuh1jrhhhM hjhhubh)}(hXIf there already is a hardware specific kernel driver for your device, the generic driver still won't bind to it, in this case if you want to use the generic driver (why would you?) you'll have to manually unbind the hardware specific driver and bind the generic driver, like this::h]hXIf there already is a hardware specific kernel driver for your device, the generic driver still won’t bind to it, in this case if you want to use the generic driver (why would you?) you’ll have to manually unbind the hardware specific driver and bind the generic driver, like this:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM#hjhhubjs)}(hzecho -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bindh]hzecho -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind}hjsbah}(h]h ]h"]h$]h&]jjuh1jrhhhM(hjhhubh)}(hxYou can verify that the device has been bound to the driver by looking for it in sysfs, for example like the following::h]hwYou can verify that the device has been bound to the driver by looking for it in sysfs, for example like the following:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM+hjhhubjs)}(h.ls -l /sys/bus/pci/devices/0000:00:19.0/driverh]h.ls -l /sys/bus/pci/devices/0000:00:19.0/driver}hjsbah}(h]h ]h"]h$]h&]jjuh1jrhhhM.hjhhubh)}(h"Which if successful should print::h]h!Which if successful should print:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM0hjhhubjs)}(hC.../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generich]hC.../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic}hj sbah}(h]h ]h"]h$]h&]jjuh1jrhhhM2hjhhubh)}(h|Note that the generic driver will not bind to old PCI 2.2 devices. If binding the device failed, run the following command::h]h{Note that the generic driver will not bind to old PCI 2.2 devices. If binding the device failed, run the following command:}(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM4hjhhubjs)}(hdmesgh]hdmesg}hj<sbah}(h]h ]h"]h$]h&]jjuh1jrhhhM7hjhhubh)}(h+and look in the output for failure reasons.h]h+and look in the output for failure reasons.}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM9hjhhubeh}(h]&making-the-driver-recognize-the-deviceah ]h"]h$]&making the driver recognize the deviceah&]uh1hhjhhhhhM referencedKubh)}(hhh](h)}(h$Things to know about uio_pci_generich]h$Things to know about uio_pci_generic}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjahhhhhM<ubh)}(hX~Interrupts are handled using the Interrupt Disable bit in the PCI command register and Interrupt Status bit in the PCI status register. All devices compliant to PCI 2.3 (circa 2002) and all compliant PCI Express devices should support these bits. uio_pci_generic detects this support, and won't bind to devices which do not support the Interrupt Disable Bit in the command register.h]hXInterrupts are handled using the Interrupt Disable bit in the PCI command register and Interrupt Status bit in the PCI status register. All devices compliant to PCI 2.3 (circa 2002) and all compliant PCI Express devices should support these bits. uio_pci_generic detects this support, and won’t bind to devices which do not support the Interrupt Disable Bit in the command register.}(hjrhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM>hjahhubh)}(hOn each interrupt, uio_pci_generic sets the Interrupt Disable bit. This prevents the device from generating further interrupts until the bit is cleared. The userspace driver should clear this bit before blocking and waiting for more interrupts.h]hOn each interrupt, uio_pci_generic sets the Interrupt Disable bit. This prevents the device from generating further interrupts until the bit is cleared. The userspace driver should clear this bit before blocking and waiting for more interrupts.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMEhjahhubeh}(h]$things-to-know-about-uio-pci-genericah ]h"]$things to know about uio_pci_genericah$]h&]uh1hhjhhhhhM<ubh)}(hhh](h)}(h.Writing userspace driver using uio_pci_generich]h.Writing userspace driver using uio_pci_generic}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMKubh)}(hUserspace driver can use pci sysfs interface, or the libpci library that wraps it, to talk to the device and to re-enable interrupts by writing to the command register.h]hUserspace driver can use pci sysfs interface, or the libpci library that wraps it, to talk to the device and to re-enable interrupts by writing to the command register.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMMhjhhubeh}(h].writing-userspace-driver-using-uio-pci-genericah ]h"].writing userspace driver using uio_pci_genericah$]h&]uh1hhjhhhhhMKubh)}(hhh](h)}(h"Example code using uio_pci_generich]h"Example code using uio_pci_generic}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMRubh)}(hAHere is some sample userspace driver code using uio_pci_generic::h]h@Here is some sample userspace driver code using uio_pci_generic:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMThjhhubjs)}(hX#include #include #include #include #include #include #include int main() { int uiofd; int configfd; int err; int i; unsigned icount; unsigned char command_high; uiofd = open("/dev/uio0", O_RDONLY); if (uiofd < 0) { perror("uio open:"); return errno; } configfd = open("/sys/class/uio/uio0/device/config", O_RDWR); if (configfd < 0) { perror("config open:"); return errno; } /* Read and cache command value */ err = pread(configfd, &command_high, 1, 5); if (err != 1) { perror("command config read:"); return errno; } command_high &= ~0x4; for(i = 0;; ++i) { /* Print out a message, for debugging. */ if (i == 0) fprintf(stderr, "Started uio test driver.\n"); else fprintf(stderr, "Interrupts: %d\n", icount); /****************************************/ /* Here we got an interrupt from the device. Do something to it. */ /****************************************/ /* Re-enable interrupts. */ err = pwrite(configfd, &command_high, 1, 5); if (err != 1) { perror("config write:"); break; } /* Wait for next interrupt. */ err = read(uiofd, &icount, 4); if (err != 4) { perror("uio read:"); break; } } return errno; }h]hX#include #include #include #include #include #include #include int main() { int uiofd; int configfd; int err; int i; unsigned icount; unsigned char command_high; uiofd = open("/dev/uio0", O_RDONLY); if (uiofd < 0) { perror("uio open:"); return errno; } configfd = open("/sys/class/uio/uio0/device/config", O_RDWR); if (configfd < 0) { perror("config open:"); return errno; } /* Read and cache command value */ err = pread(configfd, &command_high, 1, 5); if (err != 1) { perror("command config read:"); return errno; } command_high &= ~0x4; for(i = 0;; ++i) { /* Print out a message, for debugging. */ if (i == 0) fprintf(stderr, "Started uio test driver.\n"); else fprintf(stderr, "Interrupts: %d\n", icount); /****************************************/ /* Here we got an interrupt from the device. Do something to it. */ /****************************************/ /* Re-enable interrupts. */ err = pwrite(configfd, &command_high, 1, 5); if (err != 1) { perror("config write:"); break; } /* Wait for next interrupt. */ err = read(uiofd, &icount, 4); if (err != 4) { perror("uio read:"); break; } } return errno; }}hjsbah}(h]h ]h"]h$]h&]jjuh1jrhhhMVhjhhubeh}(h]"example-code-using-uio-pci-genericah ]h"]"example code using uio_pci_genericah$]h&]uh1hhjhhhhhMRubeh}(h]generic-pci-uio-driverah ]h"]generic pci uio driverah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hGeneric Hyper-V UIO driverh]hGeneric Hyper-V UIO driver}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hThe generic driver is a kernel module named uio_hv_generic. It supports devices on the Hyper-V VMBus similar to uio_pci_generic on PCI bus.h]hThe generic driver is a kernel module named uio_hv_generic. It supports devices on the Hyper-V VMBus similar to uio_pci_generic on PCI bus.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hhh](h)}(h&Making the driver recognize the deviceh]h&Making the driver recognize the device}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hSince the driver does not declare any device GUID's, it will not get loaded automatically and will not automatically bind to any devices, you must load it and allocate id to the driver yourself. For example, to use the network device class GUID::h]hSince the driver does not declare any device GUID’s, it will not get loaded automatically and will not automatically bind to any devices, you must load it and allocate id to the driver yourself. For example, to use the network device class GUID:}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubjs)}(hrmodprobe uio_hv_generic echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" > /sys/bus/vmbus/drivers/uio_hv_generic/new_idh]hrmodprobe uio_hv_generic echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" > /sys/bus/vmbus/drivers/uio_hv_generic/new_id}hj8sbah}(h]h ]h"]h$]h&]jjuh1jrhhhMhjhhubh)}(hX?If there already is a hardware specific kernel driver for the device, the generic driver still won't bind to it, in this case if you want to use the generic driver for a userspace library you'll have to manually unbind the hardware specific driver and bind the generic driver, using the device specific GUID like this::h]hXBIf there already is a hardware specific kernel driver for the device, the generic driver still won’t bind to it, in this case if you want to use the generic driver for a userspace library you’ll have to manually unbind the hardware specific driver and bind the generic driver, using the device specific GUID like this:}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubjs)}(hecho -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/hv_netvsc/unbind echo -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/uio_hv_generic/bindh]hecho -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/hv_netvsc/unbind echo -n ed963694-e847-4b2a-85af-bc9cfc11d6f3 > /sys/bus/vmbus/drivers/uio_hv_generic/bind}hjTsbah}(h]h ]h"]h$]h&]jjuh1jrhhhMhjhhubh)}(hxYou can verify that the device has been bound to the driver by looking for it in sysfs, for example like the following::h]hwYou can verify that the device has been bound to the driver by looking for it in sysfs, for example like the following:}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubjs)}(hHls -l /sys/bus/vmbus/devices/ed963694-e847-4b2a-85af-bc9cfc11d6f3/driverh]hHls -l /sys/bus/vmbus/devices/ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver}hjpsbah}(h]h ]h"]h$]h&]jjuh1jrhhhMhjhhubh)}(h"Which if successful should print::h]h!Which if successful should print:}(hj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubjs)}(h\.../ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver -> ../../../bus/vmbus/drivers/uio_hv_generich]h\.../ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver -> ../../../bus/vmbus/drivers/uio_hv_generic}hjsbah}(h]h ]h"]h$]h&]jjuh1jrhhhMhjhhubeh}(h]id1ah ]h"]h$]j^ah&]uh1hhjhhhhhMj`Kubh)}(hhh](h)}(h#Things to know about uio_hv_generich]h#Things to know about uio_hv_generic}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hOn each interrupt, uio_hv_generic sets the Interrupt Disable bit. This prevents the device from generating further interrupts until the bit is cleared. The userspace driver should clear this bit before blocking and waiting for more interrupts.h]hOn each interrupt, uio_hv_generic sets the Interrupt Disable bit. This prevents the device from generating further interrupts until the bit is cleared. The userspace driver should clear this bit before blocking and waiting for more interrupts.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hWhen host rescinds a device, the interrupt file descriptor is marked down and any reads of the interrupt file descriptor will return -EIO. Similar to a closed socket or disconnected serial device.h]hWhen host rescinds a device, the interrupt file descriptor is marked down and any reads of the interrupt file descriptor will return -EIO. Similar to a closed socket or disconnected serial device.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubhdefinition_list)}(hhh]hdefinition_list_item)}(hXThe vmbus device regions are mapped into uio device resources: 0) Channel ring buffers: guest to host and host to guest 1) Guest to host interrupt signalling pages 2) Guest to host monitor page 3) Network receive buffer region 4) Network send buffer region h](hterm)}(h>The vmbus device regions are mapped into uio device resources:h]h>The vmbus device regions are mapped into uio device resources:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhMhjubh definition)}(hhh]henumerated_list)}(hhh](j)}(h5Channel ring buffers: guest to host and host to guesth]h)}(hjh]h5Channel ring buffers: guest to host and host to guest}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(h(Guest to host interrupt signalling pagesh]h)}(hj h]h(Guest to host interrupt signalling pages}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hGuest to host monitor pageh]h)}(hj#h]hGuest to host monitor page}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj!ubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hNetwork receive buffer regionh]h)}(hj:h]hNetwork receive buffer region}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj8ubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hNetwork send buffer region h]h)}(hNetwork send buffer regionh]hNetwork send buffer region}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjOubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]enumtypearabicprefixhsuffix)startKuh1jhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubh)}(hIf a subchannel is created by a request to host, then the uio_hv_generic device driver will create a sysfs binary file for the per-channel ring buffer. For example::h]hIf a subchannel is created by a request to host, then the uio_hv_generic device driver will create a sysfs binary file for the per-channel ring buffer. For example:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubjs)}(hL/sys/bus/vmbus/devices/3811fe4d-0fa0-4b62-981a-74fc1084c757/channels/21/ringh]hL/sys/bus/vmbus/devices/3811fe4d-0fa0-4b62-981a-74fc1084c757/channels/21/ring}hjsbah}(h]h ]h"]h$]h&]jjuh1jrhhhMhjhhubeh}(h]#things-to-know-about-uio-hv-genericah ]h"]#things to know about uio_hv_genericah$]h&]uh1hhjhhhhhMubeh}(h]generic-hyper-v-uio-driverah ]h"]generic hyper-v uio driverah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hFurther informationh]hFurther information}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubj)}(hhh](j)}(h*`OSADL homepage. `_ h]h)}(h)`OSADL homepage. `_h](jR)}(hjh]hOSADL homepage.}(hjhhhNhNubah}(h]h ]h"]h$]h&]nameOSADL homepage.refurihttp://www.osadl.orguh1jQhjubhtarget)}(h h]h}(h]osadl-homepageah ]h"]osadl homepage.ah$]h&]refurijuh1jj`Khjubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h2`Linutronix homepage. `_h]h)}(hjh](jR)}(hjh]hLinutronix homepage.}(hjhhhNhNubah}(h]h ]h"]h$]h&]nameLinutronix homepage.jhttp://www.linutronix.deuh1jQhjubj)}(h h]h}(h]linutronix-homepageah ]h"]linutronix homepage.ah$]h&]refurijuh1jj`Khjubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubeh}(h]further-informationah ]h"]further informationah$]h&]uh1hhhhhhhhMubeh}(h]the-userspace-i-o-howtoah ]h"]the userspace i/o howtoah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjberror_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(j<j9jvjsjrjoj j j3j0jnjkjb j_ jZ jW jjjHjEjjjjjjjjjjjjjpjmjjjj&making the driver recognize the deviceNjjjjjjjjjjj4j1jjjju nametypes}(j<jvjrj j3jnjb jZ jjHjjjjjjjpjjjjjjjjj4jjuh}(j9hjsj'joj8j juj0jjkj6j_ jyjW jjje jEj jjKjjjj jjjjjjjmjjjsjjjZjjjajjjjjjjjjjj1jjjjju footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}jpKsRparse_messages](hsystem_message)}(hhh]h)}(hIDuplicate implicit target name: "making the driver recognize the device".h]hMDuplicate implicit target name: “making the driver recognize the device”.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]jalevelKtypeINFOsourcehlineMuh1jhjhhhhhMubj)}(hhh]h)}(h:Enumerated list start value not ordinal-1: "0" (ordinal 0)h]h>Enumerated list start value not ordinal-1: “0” (ordinal 0)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypejsourcehlineKuh1jhjubetransform_messages] transformerN include_log] decorationNhhub.