--- /dev/null
+#include <stdio.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <dirent.h>
+#include <sys/types.h>
+
+#define BUFF_SMALL 32
+#define BUFF_MID 256
+#define min(a, b) ((a) > (b) ? (b) : (a))
+
+int status;
+
+int open_device(char* path) {
+ status = open(path, O_RDWR);
+ if (status == -1) {
+ perror("open()");
+ return -1;
+ }
+
+ return status;
+}
+
+void wait_for_interrupts(int device_fd)
+{
+ read(device_fd, NULL, 1);
+}
+
+int disable_interrupts(int device_fd)
+{
+ char buff[4]; // we have to write 32 bit number
+ strncpy(buff, "0", 4);
+
+ status = write(device_fd, buff, 4);
+ if (status == -1) {
+ perror("write()");
+ return -1;
+ }
+
+ return status;
+}
+
+int enable_interrupts(int device_fd)
+{
+ char buff[4]; // we have to write 32 bit number
+ strncpy(buff, "1", 4);
+
+ status = write(device_fd, buff, 4);
+ if (status == -1) {
+ perror("write()");
+ return -1;
+ }
+
+ return status;
+}
+
+void list_available_mem_regions(char* device)
+{
+ char path[] = "/sys/class/uio/";
+ char subdir[] = "/maps/";
+ char directory[BUFF_MID];
+ memset(directory, '\0', BUFF_MID);
+
+ DIR *dip;
+ struct dirent *dit;
+
+ strncat(directory, path, strlen(path));
+ strncat(directory, device, min(strlen(device), 8));
+ strncat(directory, subdir, strlen(subdir));
+
+ dip = opendir(directory);
+ if (dip == NULL) {
+ perror("opendir");
+ return;
+ }
+
+ while ((dit = readdir(dip)) != NULL) {
+ if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
+ printf(" %s\n", dit->d_name);
+ }
+ }
+
+ status = closedir(dip);
+ if (status == -1) {
+ perror("closedir()");
+ return;
+ }
+
+}
+
+
+void list_available_io_ports(char *device)
+{
+ char path[] = "/sys/class/uio/";
+ char subdir[] = "/portio/";
+ char directory[BUFF_MID];
+ memset(directory, '\0', BUFF_MID);
+
+ DIR *dip;
+ struct dirent *dit;
+
+ strncat(directory, path, strlen(path));
+ strncat(directory, device, min(strlen(device), 8));
+ strncat(directory, subdir, strlen(subdir));
+
+ status = access(directory, F_OK);
+ if (status == -1) {
+ printf(" There are no IO port available\n");
+ return;
+ }
+
+ dip = opendir(directory);
+ if (dip == NULL) {
+ perror("opendir");
+ return;
+ }
+
+ while ((dit = readdir(dip)) != NULL) {
+ if (strcmp(dit->d_name, ".") && strcmp(dit->d_name, "..")) {
+ printf(" %s\n", dit->d_name);
+ }
+ }
+
+ status = closedir(dip);
+ if (status == -1) {
+ perror("closedir()");
+ return;
+ }
+
+}
+
+
+int main(int argc, char* argv[])
+{
+ int device_fd;
+ char buff[BUFF_SMALL];
+ memset(buff, '\0', BUFF_SMALL);
+
+ if (argc < 2) {
+ printf("Usage: %s UIO_DEVICE\n UIO_DEVICE\tname of uio device in /dev\n", argv[0]);
+ return 1;
+ }
+
+ strncat(buff, "/dev/", 5);
+ strncat(buff, argv[1], min(strlen(argv[1]), 8));
+
+ printf("Opening %s\n", buff);
+
+ device_fd = open_device(buff);
+ if (device_fd != -1) {
+ printf("Tring to enable interrupts\n");
+ status = enable_interrupts(device_fd);
+ if (status != -1) {
+ printf(" Probably OK\n");
+ }
+
+ printf("Tring to disable interrupts\n");
+ status = disable_interrupts(device_fd);
+ if (status != -1) {
+ printf(" Probably OK\n");
+ }
+ }
+
+
+ printf("Checking for available memory regions exported by the UIO driver\n");
+ list_available_mem_regions(argv[1]);
+
+ printf("Checking for available IO ports exported by the UIO driver\n");
+ list_available_io_ports(argv[1]);
+
+ return 0;
+}