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 5. 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 A. 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> </td><th width="60%" align="center">Chapter 5. Generic PCI UIO driver</th><td width="20%" align="right"> <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 <stdlib.h> 5#include <stdio.h> 6#include <unistd.h> 7#include <sys/types.h> 8#include <sys/stat.h> 9#include <fcntl.h> 10#include <errno.h> 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 < 0) { 23 perror("uio open:"); 24 return errno; 25 } 26 configfd = open("/sys/class/uio/uio0/device/config", O_RDWR); 27 if (configfd < 0) { 28 perror("config open:"); 29 return errno; 30 } 31 32 /* Read and cache command value */ 33 err = pread(configfd, &command_high, 1, 5); 34 if (err != 1) { 35 perror("command config read:"); 36 return errno; 37 } 38 command_high &= ~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, &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, &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> </td><td width="20%" align="center"><a accesskey="u" href="uio_pci_generic.html">Up</a></td><td width="40%" align="right"> <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 </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Appendix A. Further information</td></tr></table></div></body></html> 72