1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Example code using uio_pci_generic</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="The Userspace I/O HOWTO"><link rel="up" href="uio_pci_generic.html" title="Chapter&#160;5.&#160;Generic PCI UIO driver"><link rel="prev" href="uio_pci_generic_userspace.html" title="Writing userspace driver using uio_pci_generic"><link rel="next" href="app1.html" title="Appendix&#160;A.&#160;Further information"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Example code using uio_pci_generic</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="uio_pci_generic_userspace.html">Prev</a>&#160;</td><th width="60%" align="center">Chapter&#160;5.&#160;Generic PCI UIO driver</th><td width="20%" align="right">&#160;<a accesskey="n" href="app1.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="uio_pci_generic_example"></a>Example code using uio_pci_generic</h2></div></div></div><p>
2Here is some sample userspace driver code using uio_pci_generic:
3</p><pre class="programlisting">
4#include &lt;stdlib.h&gt;
5#include &lt;stdio.h&gt;
6#include &lt;unistd.h&gt;
7#include &lt;sys/types.h&gt;
8#include &lt;sys/stat.h&gt;
9#include &lt;fcntl.h&gt;
10#include &lt;errno.h&gt;
11
12int main()
13{
14	int uiofd;
15	int configfd;
16	int err;
17	int i;
18	unsigned icount;
19	unsigned char command_high;
20
21	uiofd = open("/dev/uio0", O_RDONLY);
22	if (uiofd &lt; 0) {
23		perror("uio open:");
24		return errno;
25	}
26	configfd = open("/sys/class/uio/uio0/device/config", O_RDWR);
27	if (configfd &lt; 0) {
28		perror("config open:");
29		return errno;
30	}
31
32	/* Read and cache command value */
33	err = pread(configfd, &amp;command_high, 1, 5);
34	if (err != 1) {
35		perror("command config read:");
36		return errno;
37	}
38	command_high &amp;= ~0x4;
39
40	for(i = 0;; ++i) {
41		/* Print out a message, for debugging. */
42		if (i == 0)
43			fprintf(stderr, "Started uio test driver.\n");
44		else
45			fprintf(stderr, "Interrupts: %d\n", icount);
46
47		/****************************************/
48		/* Here we got an interrupt from the
49		   device. Do something to it. */
50		/****************************************/
51
52		/* Re-enable interrupts. */
53		err = pwrite(configfd, &amp;command_high, 1, 5);
54		if (err != 1) {
55			perror("config write:");
56			break;
57		}
58
59		/* Wait for next interrupt. */
60		err = read(uiofd, &amp;icount, 4);
61		if (err != 4) {
62			perror("uio read:");
63			break;
64		}
65
66	}
67	return errno;
68}
69
70</pre><p>
71	</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="uio_pci_generic_userspace.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="uio_pci_generic.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="app1.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Writing userspace driver using uio_pci_generic&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Appendix&#160;A.&#160;Further information</td></tr></table></div></body></html>
72