sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}(hhparenthuba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget(/translations/zh_CN/driver-api/uio-howtomodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}(hhhh2ubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/zh_TW/driver-api/uio-howtomodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}(hhhhFubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/it_IT/driver-api/uio-howtomodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}(hhhhZubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/ja_JP/driver-api/uio-howtomodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}(hhhhnubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/ko_KR/driver-api/uio-howtomodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}(hhhhubah}(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}(hhhhhhhNhNubah}(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}(hhhhhhhNhNubah}(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}(hhhhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hhh](h)}(hDateh]hDate}(hhhhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh)}(h 2006-12-11 h]h)}(h 2006-12-11h]h 2006-12-11}(hj 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,hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj'hhhhhK ubh)}(hhh](h)}(h Translationsh]h Translations}(hj=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 }(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}(hhhjThhhNhNubah}(h]h ]h"]h$]h&]refurimailto:hjk@hansjkoch.deuh1jRhjIubh.}(h.hjIhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj8hhubeh}(h] translationsah ]h"] translationsah$]h&]uh1hhj'hhhhhK ubh)}(hhh](h)}(hPrefaceh]hPreface}(hj|hjzhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjwhhhhhKubh)}(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.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjwhhubh)}(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.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjwhhubh)}(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:}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK"hjwhhubh 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.}(hjhjhhhNhNubah}(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.}(hjhjhhhNhNubah}(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.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK,hjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]bullet-uh1jhhhK'hjwhhubeh}(h]prefaceah ]h"]prefaceah$]h&]uh1hhj'hhhhhKubh)}(hhh](h)}(hAcknowledgmentsh]hAcknowledgments}(hjhjhhhNhNubah}(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$hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK1hjhhubeh}(h]acknowledgmentsah ]h"]acknowledgmentsah$]h&]uh1hhj'hhhhhK/ubh)}(hhh](h)}(hFeedbackh]hFeedback}(hj=hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj8hhhhhK6ubh)}(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 }(hyFind something wrong with this document? (Or perhaps something right?) I would love to hear from you. Please email me at hjIhhhNhNubjS)}(hhjk@hansjkoch.deh]hhjk@hansjkoch.de}(hhhjRhhhNhNubah}(h]h ]h"]h$]h&]refurimailto:hjk@hansjkoch.deuh1jRhjIubh.}(hjhhjIhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK8hj8hhubeh}(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}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj|hhhhhKhj|hhubj)}(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.}(hjhjhhhNhNubah}(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.}(hjhjhhhNhNubah}(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.}(hjhjhhhNhNubah}(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.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKGhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhK@hj|hhubh)}(hhh](h)}(h How UIO worksh]h How UIO works}(hj 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 }(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}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh for the first device, and }(h for the first device, and hjhhhNhNubj)}(h ``/dev/uio1``h]h /dev/uio1}(hhhj3hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, }(h, hjhhhNhNubj)}(h ``/dev/uio2``h]h /dev/uio2}(hhhjFhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh" and so on for subsequent devices.}(h" 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}(hhhjchhhNhNubah}(h]h ]h"]h$]h&]uh1jhj_ubh; is used to access the address space of the card. Just use }(h; is used to access the address space of the card. Just use hj_hhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjxh]hmmap()}(hhhjzhhhNhNubah}(h]h ](xrefcc-funceh"]h$]h&]uh1jhjvubah}(h]h ]h"]h$]h&]refdocdriver-api/uio-howto refdomainjreftypefunc refexplicitrefwarn reftargetmmapuh1hhhhKQhj_ubh3 to access registers or RAM locations of your card.}(h3 to access registers or RAM locations of your card.hj_hhhNhNubeh}(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 }(h'Interrupts are handled by reading from hjhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh . A blocking }(h . A blocking hjhhhNhNubh)}(h:c:func:`read()`h]j)}(hjh]hread()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjreaduh1hhhhKThjubh from }(h from hjhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh> will return as soon as an interrupt occurs. You can also use }(h> will return as soon as an interrupt occurs. You can also use hjhhhNhNubh)}(h:c:func:`select()`h]j)}(hjh]hselect()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjselectuh1hhhhKThjubh on }(h on hjhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh7 to wait for an interrupt. The integer value read from }(h7 to wait for an interrupt. The integer value read from hjhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hhhj.hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhk represents the total interrupt count. You can use this number to figure out if you missed some interrupts.}(hk 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.}(hjIhjGhhhNhNubah}(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 }(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 hjUhhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hhhj^hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh will call the }(h will call the hjUhhhNhNubh)}(h:c:func:`irqcontrol()`h]j)}(hjsh]h irqcontrol()}(hhhjuhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjqubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj irqcontroluh1hhhhKfhjUubh 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 }(h 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 hjUhhhNhNubh)}(h:c:func:`irqcontrol()`h]j)}(hjh]h irqcontrol()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj irqcontroluh1hhhhKfhjUubh, }(h, hjUhhhNhNubh)}(h:c:func:`write()`h]j)}(hjh]hwrite()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjwriteuh1hhhhKfhjUubh will return with }(h will return with hjUhhhNhNubj)}(h ``-ENOSYS``h]h-ENOSYS}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh.}(hjhhjUhhhNhNubeh}(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.}(hjhjhhhNhNubah}(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 }(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()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjuio_event_notifyuh1hhhhKthjubh" from the timer’s event handler.}(h 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.}(hj8hj6hhhNhNubah}(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:}(hjFhjDhhhNhNubah}(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}(hhhj]hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjYubh\: The name of your device. It is recommended to use the name of your kernel module for this.}(h\: The name of your device. It is recommended to use the name of your kernel module for this.hjYhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjUubah}(h]h ]h"]h$]h&]uh1jhjRhhhhhNubj)}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh: 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: 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.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj|ubah}(h]h ]h"]h$]h&]uh1jhjRhhhhhNubj)}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhd: The total number of interrupts handled by the driver since the last time the device node was read.}(hd: 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&]uh1jhjRhhhhhNubeh}(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 }(h"These attributes appear under the hjhhhNhNubj)}(h``/sys/class/uio/uioX``h]h/sys/class/uio/uioX}(hhhjhhhNhNubah}(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.}(h 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.}(hjhjhhhNhNubah}(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 }(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/}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh). Subsequent mappings create directories }(h). Subsequent mappings create directories hjhhhNhNubj)}(h ``map1/``h]hmap1/}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, }(h, hjhhhNhNubj)}(h ``map2/``h]hmap2/}(hhhj/hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhT, and so on. These directories will only appear if the size of the mapping is not 0.}(hT, 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 }(hEach hjHhhhNhNubj)}(h ``mapX/``h]hmapX/}(hhhjQhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjHubhL directory contains four read-only files that show attributes of the memory:}(hL directory contains four read-only files that show attributes of the memory:hjHhhhNhNubeh}(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}(hhhjuhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjqubh: 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: 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.hjqhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjmubah}(h]h ]h"]h$]h&]uh1jhjjhhhhhNubj)}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh+: The address of memory that can be mapped.}(h+: The address of memory that can be mapped.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjjhhhhhNubj)}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh7: The size, in bytes, of the memory pointed to by addr.}(h7: The size, in bytes, of the memory pointed to by addr.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjjhhhhhNubj)}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhH: The offset, in bytes, that has to be added to the pointer returned by }(hH: The offset, in bytes, that has to be added to the pointer returned by hjhhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjh]hmmap()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc 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 }(h 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)}(hj#h]hmmap()}(hhhj%hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj!ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjmmapuh1hhhhKhjubhH are always page aligned, so it is good style to always add this offset.}(hH 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&]uh1jhjjhhhhhNubeh}(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 }(hJFrom userspace, the different mappings are distinguished by adjusting the hjWhhhNhNubj)}(h ``offset``h]hoffset}(hhhj`hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjWubh parameter of the }(h parameter of the hjWhhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjuh]hmmap()}(hhhjwhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjsubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjmmapuh1hhhhKhjWubh\ call. To map the memory of mapping N, you have to use N times the page size as your offset:}(h\ call. To map the memory of mapping N, you have to use N times the page size as your offset:hjWhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh literal_block)}(hoffset = N * getpagesize();h]hoffset = N * getpagesize();}(hhhjubah}(h]h ]h"]h$]h&] xml:spacepreserveuh1jhhhKhjhhubh)}(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 }(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()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjiopermuh1hhhhKhjubh, }(h, hjhhhNhNubh)}(h:c:func:`iopl()`h]j)}(hjh]hiopl()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjiopluh1hhhhKhjubh, }(hjhjubh)}(h:c:func:`inb()`h]j)}(hjh]hinb()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjinbuh1hhhhKhjubh, }(h, hjhhhNhNubh)}(h:c:func:`outb()`h]j)}(hj%h]houtb()}(hhhj'hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj#ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjoutbuh1hhhhKhjubh, and similar functions.}(h, 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 }(hISince these ioport regions can not be mapped, they will not appear under hjMhhhNhNubj)}(h``/sys/class/uio/uioX/maps/``h]h/sys/class/uio/uioX/maps/}(hhhjVhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubh 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 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.hjMhhhNhNubeh}(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 }(h-To address this situation, the new directory hjohhhNhNubj)}(h``/sys/class/uio/uioX/portio/``h]h/sys/class/uio/uioX/portio/}(hhhjxhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjoubh 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 }(h 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 hjohhhNhNubj)}(h ``port0``h]hport0}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjoubh, }(h, hjohhhNhNubj)}(h ``port1``h]hport1}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjoubh$, and so on, will appear underneath }(h$, and so on, will appear underneath hjohhhNhNubj)}(h``/sys/class/uio/uioX/portio/``h]h/sys/class/uio/uioX/portio/}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjoubh.}(hjhhjohhhNhNubeh}(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 }(hEach hjhhhNhNubj)}(h ``portX/``h]hportX/}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhb directory contains four read-only files that show name, start, size, and type of the port region:}(hb 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}(hhhjhhhNhNubah}(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.}(h: 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}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh : The first port of this region.}(h : The first port of this region.hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(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}(hhhjD hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj@ ubh%: The number of ports in this region.}(h%: 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}(hhhjk hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjg ubh': A string describing the type of port.}(h': A string describing the type of port.hjg hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjc ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jhhhKhjhhubeh}(h] how-uio-worksah ]h"] how uio worksah$]h&]uh1hhj|hhhhhKJubeh}(h] about-uioah ]h"] about uioah$]h&]uh1hhhhhhhhKint (*mmap)(struct uio_info *info, struct vm_area_struct *vma)}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh": Optional. If you need a special }(h": Optional. If you need a special hj hhhNhNubh)}(h:c:func:`mmap()`h]j)}(hj h]hmmap()}(hhhj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjmmapuh1hhhhKhj ubhB function, you can set it here. If this pointer is not NULL, your }(hB function, you can set it here. If this pointer is not NULL, your hj hhhNhNubh)}(h:c:func:`mmap()`h]j)}(hj h]hmmap()}(hhhj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjmmapuh1hhhhKhj ubh, will be called instead of the built-in one.}(h, will be called instead of the built-in one.hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj 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)}(hhhj4 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj0 ubh,: Optional. You might want to have your own }(h,: Optional. You might want to have your own hj0 hhhNhNubh)}(h:c:func:`open()`h]j)}(hjI h]hopen()}(hhhjK hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjG ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjopenuh1hhhhKhj0 ubhC, e.g. to enable interrupts only when your device is actually used.}(hC, e.g. to enable interrupts only when your device is actually used.hj0 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)}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj{ ubh#: Optional. If you define your own }(h#: Optional. If you define your own hj{ hhhNhNubh)}(h:c:func:`open()`h]j)}(hj h]hopen()}(hhhj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjopenuh1hhhhMhj{ ubh', you will probably also want a custom }(h', you will probably also want a custom hj{ hhhNhNubh)}(h:c:func:`release()`h]j)}(hj h]h release()}(hhhj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjreleaseuh1hhhhMhj{ ubh function.}(h function.hj{ hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjw 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)}(hhhj 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 }(h`: 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}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh1, you can implement this function. The parameter }(h1, you can implement this function. The parameter hj hhhNhNubj)}(h ``irq_on``h]hirq_on}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh6 will be 0 to disable interrupts and 1 to enable them.}(h6 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 }(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 hj9 hhhNhNubj)}(h``struct uio_mem``h]hstruct uio_mem}(hhhjB hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9 ubh in the }(h in the hj9 hhhNhNubj)}(h ``mem[]``h]hmem[]}(hhhjU hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9 ubh0 array. Here’s a description of the fields of }(h. array. Here's a description of the fields of hj9 hhhNhNubj)}(h``struct uio_mem``h]hstruct uio_mem}(hhhjh hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9 ubh:}(h:hj9 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}(hhhj 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.}(hi: 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}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh/: Required if the mapping is used. Set this to }(h/: Required if the mapping is used. Set this to hj hhhNhNubj)}(h``UIO_MEM_PHYS``h]h UIO_MEM_PHYS}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh< if you have physical memory on your card to be mapped. Use }(h< if you have physical memory on your card to be mapped. Use hj hhhNhNubj)}(h``UIO_MEM_LOGICAL``h]hUIO_MEM_LOGICAL}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh) for logical memory (e.g. allocated with }(h) for logical memory (e.g. allocated with hj hhhNhNubh)}(h:c:func:`__get_free_pages()`h]j)}(hj h]h__get_free_pages()}(hhhj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj__get_free_pagesuh1hhhhMhj ubh$ but not kmalloc()). There’s also }(h" but not kmalloc()). There's also hj hhhNhNubj)}(h``UIO_MEM_VIRTUAL``h]hUIO_MEM_VIRTUAL}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh for virtual memory.}(h for virtual memory.hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj 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}(hhhj7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj3ubh{: Required if the mapping is used. Fill in the address of your memory block. This address is the one that appears in sysfs.}(h{: Required if the mapping is used. Fill in the address of your memory block. This address is the one that appears in sysfs.hj3hhhNhNubeh}(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}(hhhj^hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubh,: Fill in the size of the memory block that }(h,: Fill in the size of the memory block that hjZhhhNhNubj)}(h``addr``h]haddr}(hhhjqhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubh points to. If }(h points to. If hjZhhhNhNubj)}(h``size``h]hsize}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubh: is zero, the mapping is considered unused. Note that you }(h: is zero, the mapping is considered unused. Note that you hjZhhhNhNubhemphasis)}(h*must*h]hmust}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubh initialize }(h initialize hjZhhhNhNubj)}(h``size``h]hsize}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubh# with zero for all unused mappings.}(h# with zero for all unused mappings.hjZhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjVubah}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh: If you have to access this memory region from within your kernel module, you will want to map it internally by using something like }(h: If you have to access this memory region from within your kernel module, you will want to map it internally by using something like hjhhhNhNubh)}(h:c:func:`ioremap()`h]j)}(hjh]h ioremap()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjioremapuh1hhhhM#hjubhb. Addresses returned by this function cannot be mapped to user space, so you must not store it in }(hb. Addresses returned by this function cannot be mapped to user space, so you must not store it in hjhhhNhNubj)}(h``addr``h]haddr}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. Use }(h. Use hjhhhNhNubj)}(h``internal_addr``h]h internal_addr}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh% instead to remember such an address.}(h% instead to remember such an address.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM#hjubah}(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 }(hPlease do not touch the hjBhhhNhNubj)}(h``map``h]hmap}(hhhjKhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjBubh element of }(h element of hjBhhhNhNubj)}(h``struct uio_mem``h]hstruct uio_mem}(hhhj^hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjBubh`! It is used by the UIO framework to set up sysfs files for this mapping. Simply leave it alone.}(h`! It is used by the UIO framework to set up sysfs files for this mapping. Simply leave it alone.hjBhhhNhNubeh}(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 }(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 hjwhhhNhNubj)}(h``struct uio_port``h]hstruct uio_port}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjwubh in the }(h in the hjwhhhNhNubj)}(h ``port[]``h]hport[]}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjwubh0 array. Here’s a description of the fields of }(h. array. Here's a description of the fields of hjwhhhNhNubj)}(h``struct uio_port``h]hstruct uio_port}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjwubh:}(hjz hjwhhhNhNubeh}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh=: Required. Set this to one of the predefined constants. Use }(h=: Required. Set this to one of the predefined constants. Use hjhhhNhNubj)}(h``UIO_PORT_X86``h]h UIO_PORT_X86}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, for the ioports found in x86 architectures.}(h, for the ioports found in x86 architectures.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM5hjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh[: Required if the port region is used. Fill in the number of the first port of this region.}(h[: 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&]uh1jhjhhhhhNubj)}(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}(hhhj*hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubh1: Fill in the number of ports in this region. If }(h1: Fill in the number of ports in this region. If hj&hhhNhNubj)}(h``size``h]hsize}(hhhj=hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubh9 is zero, the region is considered unused. Note that you }(h9 is zero, the region is considered unused. Note that you hj&hhhNhNubj)}(h*must*h]hmust}(hhhjPhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubh initialize }(h initialize hj&hhhNhNubj)}(h``size``h]hsize}(hhhjchhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubh" with zero for all unused regions.}(h" with zero for all unused regions.hj&hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM<hj"ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(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 }(hPlease do not touch the hjhhhNhNubj)}(h ``portio``h]hportio}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh element of }(h element of hjhhhNhNubj)}(h``struct uio_port``h]hstruct uio_port}(hhhjhhhNhNubah}(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.}(hj! 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&]uh1hhj hhhhhKubh)}(hhh](h)}(hAdding an interrupt handlerh]hAdding an interrupt handler}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMEubh)}(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 }(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 hjhhhNhNubj)}(h*have*h]hhave}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhA to perform after each interrupt, then your handler can be empty.}(hA to perform after each interrupt, then your handler can be empty.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMGhjhhubh)}(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 }(h%If, on the other hand, your hardware hjhhhNhNubj)}(h*needs*h]hneeds}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh< some action to be performed after each interrupt, then you }(h< some action to be performed after each interrupt, then you hjhhhNhNubj)}(h*must*h]hmust}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh 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 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.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMMhjhhubh)}(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.}(hj/hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMShjhhubh)}(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.}(hj=hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMXhjhhubh)}(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.}(hjKhjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMbhjhhubeh}(h]adding-an-interrupt-handlerah ]h"]adding an interrupt handlerah$]h&]uh1hhj hhhhhMEubh)}(hhh](h)}(h#Using uio_pdrv for platform devicesh]h#Using uio_pdrv for platform devices}(hjdhjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj_hhhhhMhubh)}(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 }(hyIn many cases, UIO drivers for platform devices can be handled in a generic way. In the same place where you define your hjphhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hhhjyhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjpubhA, you simply also implement your interrupt handler and fill your }(hA, you simply also implement your interrupt handler and fill your hjphhhNhNubj)}(h``struct uio_info``h]hstruct uio_info}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjpubh. A pointer to this }(h. A pointer to this hjphhhNhNubj)}(h``struct uio_info``h]hstruct uio_info}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjpubh is then used as }(h is then used as hjphhhNhNubj)}(h``platform_data``h]h platform_data}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjpubh for your platform device.}(h for your platform device.hjphhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMjhj_hhubh)}(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 }(h$You also need to set up an array of hjhhhNhNubj)}(h``struct resource``h]hstruct resource}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhl containing addresses and sizes of your memory mappings. This information is passed to the driver using the }(hl containing addresses and sizes of your memory mappings. This information is passed to the driver using the hjhhhNhNubj)}(h ``.resource``h]h .resource}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh and }(h and hjhhhNhNubj)}(h``.num_resources``h]h.num_resources}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh elements of }(h elements of hjhhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh.}(hjhhjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMqhj_hhubh)}(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 }(hYou now have to set the hj%hhhNhNubj)}(h ``.name``h]h.name}(hhhj.hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj%ubh element of }(h element of hj%hhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hhhjAhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj%ubh to }(h to hj%hhhNhNubj)}(h``"uio_pdrv"``h]h "uio_pdrv"}(hhhjThhhNhNubah}(h]h ]h"]h$]h&]uh1jhj%ubhJ to use the generic UIO platform device driver. This driver will fill the }(hJ to use the generic UIO platform device driver. This driver will fill the hj%hhhNhNubj)}(h ``mem[]``h]hmem[]}(hhhjghhhNhNubah}(h]h ]h"]h$]h&]uh1jhj%ubhA array according to the resources given, and register the device.}(hA array according to the resources given, and register the device.hj%hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMvhj_hhubh)}(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.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM{hj_hhubeh}(h]#using-uio-pdrv-for-platform-devicesah ]h"]#using uio_pdrv for platform devicesah$]h&]uh1hhj hhhhhMhubh)}(hhh](h)}(h*Using uio_pdrv_genirq for platform devicesh]h*Using uio_pdrv_genirq for platform devices}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(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 }(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhE one step further and use a generic interrupt handler. That’s what }(hC one step further and use a generic interrupt handler. That's what hjhhhNhNubj)}(h``uio_pdrv_genirq``h]huio_pdrv_genirq}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh does.}(h does.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(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 }(h=The setup for this driver is the same as described above for hjhhhNhNubj)}(h ``uio_pdrv``h]huio_pdrv}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh=, except that you do not implement an interrupt handler. The }(h=, except that you do not implement an interrupt handler. The hjhhhNhNubj)}(h ``.handler``h]h.handler}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh element of }(h element of hjhhhNhNubj)}(h``struct uio_info``h]hstruct uio_info}(hhhj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh must remain }(h must remain hjhhhNhNubj)}(h``NULL``h]hNULL}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. The }(h. The hjhhhNhNubj)}(h``.irq_flags``h]h .irq_flags}(hhhj1hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh element must not contain }(h element must not contain hjhhhNhNubj)}(h``IRQF_SHARED``h]h IRQF_SHARED}(hhhjDhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh.}(hjhhjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hmYou will set the ``.name`` element of ``struct platform_device`` to ``"uio_pdrv_genirq"`` to use this driver.h](hYou will set the }(hYou will set the hj\hhhNhNubj)}(h ``.name``h]h.name}(hhhjehhhNhNubah}(h]h ]h"]h$]h&]uh1jhj\ubh element of }(h element of hj\hhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hhhjxhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj\ubh to }(h to hj\hhhNhNubj)}(h``"uio_pdrv_genirq"``h]h"uio_pdrv_genirq"}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj\ubh to use this driver.}(h to use this driver.hj\hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(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 }(h!The generic interrupt handler of hjhhhNhNubj)}(h``uio_pdrv_genirq``h]huio_pdrv_genirq}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. will simply disable the interrupt line using }(h. will simply disable the interrupt line using hjhhhNhNubh)}(h:c:func:`disable_irq_nosync()`h]j)}(hjh]hdisable_irq_nosync()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjdisable_irq_nosyncuh1hhhhMhjubh. After doing its work, userspace can reenable the interrupt by writing 0x00000001 to the UIO device file. The driver already implements an }(h. After doing its work, userspace can reenable the interrupt by writing 0x00000001 to the UIO device file. The driver already implements an hjhhhNhNubh)}(h:c:func:`irq_control()`h]j)}(hjh]h irq_control()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj irq_controluh1hhhhMhjubh8 to make this possible, you must not implement your own.}(h8 to make this possible, you must not implement your own.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(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 }(hUsing hjhhhNhNubj)}(h``uio_pdrv_genirq``h]huio_pdrv_genirq}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh 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 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.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(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 }(hRWhen used in a device-tree enabled system, the driver needs to be probed with the hj0hhhNhNubj)}(h ``"of_id"``h]h"of_id"}(hhhj9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj0ubh module parameter set to the }(h module parameter set to the hj0hhhNhNubj)}(h``"compatible"``h]h "compatible"}(hhhjLhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj0ubh 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 }(h 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 hj0hhhNhNubj)}(h``"linux,uio-name"``h]h"linux,uio-name"}(hhhj_hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj0ubh! may be specified in the DT node.}(h! may be specified in the DT node.hj0hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]*using-uio-pdrv-genirq-for-platform-devicesah ]h"]*using uio_pdrv_genirq for platform devicesah$]h&]uh1hhj hhhhhMubh)}(hhh](h)}(h*Using uio_dmem_genirq for platform devicesh]h*Using uio_dmem_genirq for platform devices}(hjhjhhhNhNubah}(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 }(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh* driver provides a way to accomplish this.}(h* 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 }(h/This driver is used in a similar manner to the hjhhhNhNubj)}(h``"uio_pdrv_genirq"``h]h"uio_pdrv_genirq"}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh= driver with respect to interrupt configuration and handling.}(h= driver with respect to interrupt configuration and handling.hjhhhNhNubeh}(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 }(hSet the hjhhhNhNubj)}(h ``.name``h]h.name}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh element of }(h element of hjhhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh to }(h to hjhhhNhNubj)}(h``"uio_dmem_genirq"``h]h"uio_dmem_genirq"}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh to use this driver.}(h to use this driver.hjhhhNhNubeh}(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 }(h$When using this driver, fill in the hjhhhNhNubj)}(h``.platform_data``h]h.platform_data}(hhhj&hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh element of }(h element of hjhhhNhNubj)}(h``struct platform_device``h]hstruct platform_device}(hhhj9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, which is of type }(h, which is of type hjhhhNhNubj)}(h ``struct uio_dmem_genirq_pdata``h]hstruct uio_dmem_genirq_pdata}(hhhjLhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh+ and which contains the following elements:}(h+ and which contains the following elements:hjhhhNhNubeh}(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}(hhhjphhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubh!: The same structure used as the }(h!: The same structure used as the hjlhhhNhNubj)}(h``uio_pdrv_genirq``h]huio_pdrv_genirq}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubh platform data}(h platform datahjlhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhubah}(h]h ]h"]h$]h&]uh1jhjehhhhhNubj)}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhR: Pointer to list of sizes of dynamic memory regions to be mapped into user space.}(hR: 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&]uh1jhjehhhhhNubj)}(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh: Number of elements in }(h: Number of elements in hjhhhNhNubj)}(h``dynamic_region_sizes``h]hdynamic_region_sizes}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh array.}(h array.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjehhhhhNubeh}(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 }(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 hj hhhNhNubj)}(h``MAX_UIO_MAPS``h]h MAX_UIO_MAPS}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh.}(hjhhj hhhNhNubeh}(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, }(hGThe dynamic memory regions will be allocated when the UIO device file, hj*hhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hhhj3hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj*ubh is opened. Similar to static memory resources, the memory region information for dynamic regions is then visible via sysfs at }(h is opened. Similar to static memory resources, the memory region information for dynamic regions is then visible via sysfs at hj*hhhNhNubj)}(h#``/sys/class/uio/uioX/maps/mapY/*``h]h/sys/class/uio/uioX/maps/mapY/*}(hhhjFhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj*ubh. 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. 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.hj*hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]*using-uio-dmem-genirq-for-platform-devicesah ]h"]*using uio_dmem_genirq for platform devicesah$]h&]uh1hhj 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}(hjthjrhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjohhhhhMubh)}(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.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjohhubh)}(hhh](h)}(h)Getting information about your UIO deviceh]h)Getting information about your UIO device}(hjhjhhhNhNubah}(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 }(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}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh and }(h and hjhhhNhNubj)}(h ``version``h]hversion}(hhhjhhhNhNubah}(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.}(hg 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.}(hjhjhhhNhNubah}(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 }(hThere is a tool called hjhhhNhNubj)}(h ``lsuio``h]hlsuio}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhC that lists UIO devices and their attributes. It is available here:}(hC that lists UIO devices and their attributes. It is available here:hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h1http://www.osadl.org/projects/downloads/UIO/user/h]jS)}(hjh]h1http://www.osadl.org/projects/downloads/UIO/user/}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]refurijuh1jRhjubah}(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 }(hWith hjhhhNhNubj)}(h ``lsuio``h]hlsuio}(hhhj%hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh you can quickly check if your kernel module is loaded and which attributes it exports. Have a look at the manpage for details.}(h you can quickly check if your kernel module is loaded and which attributes it exports. Have a look at the manpage for details.hjhhhNhNubeh}(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 }(hThe source code of hj>hhhNhNubj)}(h ``lsuio``h]hlsuio}(hhhjGhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj>ubhO can serve as an example for getting information about an UIO device. The file }(hO 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}(hhhjZhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj>ubhI contains a lot of functions you could use in your userspace driver code.}(hI 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&]uh1hhjohhhhhMubh)}(hhh](h)}(hmmap() device memoryh]hmmap() device memory}(hjhj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj{hhhhhMubh)}(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 }(hqAfter 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()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjmmapuh1hhhhMhjubh+ to map the device’s memory to userspace.}(h) to map the device's memory to userspace.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj{hhubh)}(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 }(hThe parameter hjhhhNhNubj)}(h ``offset``h]hoffset}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh of the }(h of the hjhhhNhNubh)}(h:c:func:`mmap()`h]j)}(hjh]hmmap()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc 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:}(h 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&]uh1hhhhMhj{hhubj)}(hoffset = N * getpagesize();h]hoffset = N * getpagesize();}(hhhjubah}(h]h ]h"]h$]h&]jjuh1jhhhMhj{hhubh)}(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 }(hGN starts from zero, so if you've got only one memory range to map, set hjhhhNhNubj)}(h``offset = 0``h]h offset = 0}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh`. A drawback of this technique is that memory is always mapped beginning with its start address.}(h`. A drawback of this technique is that memory is always mapped beginning with its start address.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj{hhubeh}(h]mmap-device-memoryah ]h"]mmap() device memoryah$]h&]uh1hhjohhhhhMubh)}(hhh](h)}(hWaiting for interruptsh]hWaiting for interrupts}(hjBhj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj=hhhhhMubh)}(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.}(hjPhjNhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj=hhubh)}(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}(hhhj`hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj\ubh is a read-only file. A }(h is a read-only file. A hj\hhhNhNubh)}(h:c:func:`read()`h]j)}(hjuh]hread()}(hhhjwhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjsubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjreaduh1hhhhMhj\ubhT will always block until an interrupt occurs. There is only one legal value for the }(hT will always block until an interrupt occurs. There is only one legal value for the hj\hhhNhNubj)}(h ``count``h]hcount}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj\ubh parameter of }(h parameter of hj\hhhNhNubh)}(h:c:func:`read()`h]j)}(hjh]hread()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjreaduh1hhhhMhj\ubhK, and that is the size of a signed 32 bit integer (4). Any other value for }(hK, and that is the size of a signed 32 bit integer (4). Any other value for hj\hhhNhNubj)}(h ``count``h]hcount}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj\ubh causes }(h causes hj\hhhNhNubh)}(h:c:func:`read()`h]j)}(hjh]hread()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjreaduh1hhhhMhj\ubh 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 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.hj\hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj=hhubh)}(h5You can also use :c:func:`select()` on ``/dev/uioX``.h](hYou can also use }(hYou can also use hj hhhNhNubh)}(h:c:func:`select()`h]j)}(hjh]hselect()}(hhhjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjselectuh1hhhhMhj ubh on }(h on hj hhhNhNubj)}(h ``/dev/uioX``h]h /dev/uioX}(hhhj8hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh.}(hjhhj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj=hhubeh}(h]waiting-for-interruptsah ]h"]waiting for interruptsah$]h&]uh1hhjohhhhhMubeh}(h]writing-a-driver-in-userspaceah ]h"]writing a driver in userspaceah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hGeneric PCI UIO driverh]hGeneric PCI UIO driver}(hjehjchhhNhNubah}(h]h ]h"]h$]h&]uh1hhj`hhhhhMubh)}(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.}(hjshjqhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj`hhubh)}(hhh](h)}(h&Making the driver recognize the deviceh]h&Making the driver recognize the device}(hjhjhhhNhNubah}(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:}(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&]uh1hhhhMhjhhubj)}(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}(hhhjubah}(h]h ]h"]h$]h&]jjuh1jhhhM 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:}(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#hjhhubj)}(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}(hhhjubah}(h]h ]h"]h$]h&]jjuh1jhhhM(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:}(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+hjhhubj)}(h.ls -l /sys/bus/pci/devices/0000:00:19.0/driverh]h.ls -l /sys/bus/pci/devices/0000:00:19.0/driver}(hhhjubah}(h]h ]h"]h$]h&]jjuh1jhhhM.hjhhubh)}(h"Which if successful should print::h]h!Which if successful should print:}(h!Which if successful should print:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM0hjhhubj)}(hC.../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generich]hC.../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic}(hhhjubah}(h]h ]h"]h$]h&]jjuh1jhhhM2hjhhubh)}(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:}(h{Note that the generic driver will not bind to old PCI 2.2 devices. If binding the device failed, run the following command:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM4hjhhubj)}(hdmesgh]hdmesg}(hhhjubah}(h]h ]h"]h$]h&]jjuh1jhhhM7hjhhubh)}(h+and look in the output for failure reasons.h]h+and look in the output for failure reasons.}(hj#hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM9hjhhubeh}(h]&making-the-driver-recognize-the-deviceah ]h"]h$]&making the driver recognize the deviceah&]uh1hhj`hhhhhM referencedKubh)}(hhh](h)}(h$Things to know about uio_pci_generich]h$Things to know about uio_pci_generic}(hj=hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj8hhhhhM<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.}(hjKhjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM>hj8hhubh)}(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.}(hjYhjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMEhj8hhubeh}(h]$things-to-know-about-uio-pci-genericah ]h"]$things to know about uio_pci_genericah$]h&]uh1hhj`hhhhhM<ubh)}(hhh](h)}(h.Writing userspace driver using uio_pci_generich]h.Writing userspace driver using uio_pci_generic}(hjrhjphhhNhNubah}(h]h ]h"]h$]h&]uh1hhjmhhhhhMKubh)}(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.}(hjhj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMMhjmhhubeh}(h].writing-userspace-driver-using-uio-pci-genericah ]h"].writing userspace driver using uio_pci_genericah$]h&]uh1hhj`hhhhhMKubh)}(hhh](h)}(h"Example code using uio_pci_generich]h"Example code using uio_pci_generic}(hjhjhhhNhNubah}(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:}(h@Here is some sample userspace driver code using uio_pci_generic:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMThjhhubj)}(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; }}(hhhjubah}(h]h ]h"]h$]h&]jjuh1jhhhMVhjhhubeh}(h]"example-code-using-uio-pci-genericah ]h"]"example code using uio_pci_genericah$]h&]uh1hhj`hhhhhMRubeh}(h]generic-pci-uio-driverah ]h"]generic pci uio driverah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hGeneric Hyper-V UIO driverh]hGeneric Hyper-V UIO driver}(hjhjhhhNhNubah}(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.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hhh](h)}(h&Making the driver recognize the deviceh]h&Making the driver recognize the device}(hjhjhhhNhNubah}(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:}(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:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(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}(hhhjubah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(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:}(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:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(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}(hhhj.ubah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(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:}(hwYou can verify that the device has been bound to the driver by looking for it in sysfs, for example like the following:(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hHls -l /sys/bus/vmbus/devices/ed963694-e847-4b2a-85af-bc9cfc11d6f3/driverh]hHls -l /sys/bus/vmbus/devices/ed963694-e847-4b2a-85af-bc9cfc11d6f3/driver}(hhhjKubah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(h"Which if successful should print::h]h!Which if successful should print:}(h!Which if successful should print:hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(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}(hhhjhubah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubeh}(h]id1ah ]h"]h$]j5ah&]uh1hhjhhhhhMj7Kubh)}(hhh](h)}(h#Things to know about uio_hv_generich]h#Things to know about uio_hv_generic}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj}hhhhhMubh)}(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.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj}hhubh)}(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.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj}hhubhdefinition_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:}(hjhjhhhNhNubah}(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}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(h(Guest to host interrupt signalling pagesh]h)}(hjh]h(Guest to host interrupt signalling pages}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hGuest to host monitor pageh]h)}(hjh]hGuest to host monitor page}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hNetwork receive buffer regionh]h)}(hjh]hNetwork receive buffer region}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hNetwork send buffer region h]h)}(hNetwork send buffer regionh]hNetwork send buffer region}(hj1hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+ubah}(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&]uh1jhj}hhhNhNubh)}(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:}(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:hjahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj}hhubj)}(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}(hhhjpubah}(h]h ]h"]h$]h&]jjuh1jhhhMhj}hhubeh}(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}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubj)}(hhh](j)}(h*`OSADL homepage. `_ h]h)}(h)`OSADL homepage. `_h](jS)}(hjh]hOSADL homepage.}(hOSADL homepage.hjhhhNhNubah}(h]h ]h"]h$]h&]nameOSADL homepage.refurihttp://www.osadl.orguh1jRhjubhtarget)}(h h]h}(h]osadl-homepageah ]h"]osadl homepage.ah$]h&]refurijuh1jj7Khjubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h2`Linutronix homepage. `_h]h)}(hjh](jS)}(hjh]hLinutronix homepage.}(hLinutronix homepage.hjhhhNhNubah}(h]h ]h"]h$]h&]nameLinutronix homepage.jhttp://www.linutronix.deuh1jRhjubj)}(h h]h}(h]linutronix-homepageah ]h"]linutronix homepage.ah$]h&]refurijuh1jj7Khjubeh}(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_handlerjAerror_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.confapep_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_spacefile_insertion_enabled raw_enabledKline_length_limitM'syntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_link embed_imagesenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(jjjyjvjtjqjj j5j2jqjnj j j j jljijjj\jYjjj}jzjdjaj]jZjxjuj:j7jUjRjj&making the driver recognize the deviceNjjjgjjjjjjjjjjjjjju nametypes}(jNjyNjtNjNj5NjqNj Nj NjlNjNj\NjNj}NjdNj]NjxNj:NjUNjNjNjjNjNjNjNjNjNjjuh}(jhjvj'jqj8j jwj2jjnj8j j|j jjij jj jYjjj_jzjjajjZjojujj7j{jRj=jj`j1jjgj8jjmjjjjjxjjj}jjjjjju footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}jOKsRparse_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”.}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]jxalevelKtypeINFOsourcehlineMuh1jhjhhhhhMubj)}(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)}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypejsourcehlineKuh1jhjubetransform_messages] transformerN include_log] decorationNhhub.