From: Rostislav Lisovy Date: Sun, 13 Feb 2011 02:08:53 +0000 (+0100) Subject: Simple userspace program reading some files exported by UIO driver (tested and seems... X-Git-Url: http://rtime.felk.cvut.cz/gitweb/mf6xx.git/commitdiff_plain/bd214ac63511a11f2722daca73a3f36bf7354bf4 Simple userspace program reading some files exported by UIO driver (tested and seems to be working) --- diff --git a/src/uio/userspace/test_application/Makefile b/src/uio/userspace/test_application/Makefile new file mode 100644 index 0000000..c8958d1 --- /dev/null +++ b/src/uio/userspace/test_application/Makefile @@ -0,0 +1,5 @@ +FLAGS=-Wall -pedantic + +all: + gcc main.c $(FLAGS) -std=c99 -o main + diff --git a/src/uio/userspace/test_application/main b/src/uio/userspace/test_application/main new file mode 100755 index 0000000..ee65be1 Binary files /dev/null and b/src/uio/userspace/test_application/main differ diff --git a/src/uio/userspace/test_application/main.c b/src/uio/userspace/test_application/main.c new file mode 100644 index 0000000..10226e9 --- /dev/null +++ b/src/uio/userspace/test_application/main.c @@ -0,0 +1,174 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +}