From 11762c3dabc6ed8667e103bc50e82700e1f29d75 Mon Sep 17 00:00:00 2001 From: ben Date: Tue, 11 Sep 2007 09:09:18 +0000 Subject: [PATCH] add userspace library git-svn-id: svn+ssh://en.codiert.org/home/staff/ben/dev/misc.svn/projects/fpgafs@393 766a2236-cff9-0310-b0c0-a81a5f92509a --- libfpga/Makefile | 13 +++ libfpga/libfpga.c | 200 ++++++++++++++++++++++++++++++++++++++++++++++ libfpga/libfpga.h | 9 +++ 3 files changed, 222 insertions(+) create mode 100644 libfpga/Makefile create mode 100644 libfpga/libfpga.c create mode 100644 libfpga/libfpga.h diff --git a/libfpga/Makefile b/libfpga/Makefile new file mode 100644 index 0000000..7515edc --- /dev/null +++ b/libfpga/Makefile @@ -0,0 +1,13 @@ + +CFLAGS=-c -fPIC + +CC = gcc + +SRCS = libfpga.c +OBJS = $(SRCS:%.c=%.o) + +libfpga: ${OBJS} + ${CC} -shared -Wl,-soname,$@.so.1 -o $@.so.1.0.1 $^ + +clean: + rm *.o *.so.* diff --git a/libfpga/libfpga.c b/libfpga/libfpga.c new file mode 100644 index 0000000..d5d8c7c --- /dev/null +++ b/libfpga/libfpga.c @@ -0,0 +1,200 @@ +/********************************************* + * Benjamin Krill + *********************************************/ +#include +#include +#include +#include +#include +#include + +int fpgafs_create_context(int fd, const char *lldrv, const char *dir) +{ + char *cdir; + int res,fd1; + + /* create context directory */ + cdir=malloc(sizeof(dir) + 8); + sprintf(cdir, "/fpgafs/%s", dir); + if (mkdir(dir, 0777) == -1) { + printf("ERROR-1: %s\n", strerror(errno)); + free(cdir); + return -1; + } + + /* change to the context directory */ + if (chdir(cdir) == -1) { + printf("ERROR-2: %s\n", strerror(errno)); + free(cdir); + return -1; + } + + free(cdir); + /* open the load file */ + if ((fd1 = open("load", O_WRONLY, 0)) == -1) { + printf("ERROR-3: %s\n", strerror(errno)); + return -1; + } + + /* write the file into load */ + free(cdir); + cdir=malloc(1024); + while ((res=read(fd, cdir, 1024))) + write(fd1, cdir, res); + + free(cdir); + return 0; +} + +int fpgafs_send_data(const char *dir, const void *buf, size_t count) +{ + int fd, res; + char *cdir; + cdir=malloc(sizeof(dir) + 8); + sprintf(cdir, "/fpgafs/%s", dir); + + /* change to the context directory */ + if (chdir(cdir) == -1) { + printf("ERROR-1: %s\n", strerror(errno)); + free(cdir); + return -1; + } + + free(cdir); + /* open the load file */ + if ((fd = open("dout", O_RDONLY, 0)) == -1) { + printf("ERROR-2: %s\n", strerror(errno)); + return -1; + } + + /* write stuff into the file */ + if ((res=write(fd, buf, count)) == -1) { + printf("ERROR-3: %s\n", strerror(errno)); + return -1; + } + + return res; +} + +int fpgafs_recv_data(const char *dir, void *buf, size_t count) +{ + int fd, res; + char *cdir; + cdir=malloc(sizeof(dir) + 8); + sprintf(cdir, "/fpgafs/%s", dir); + + /* change to the context directory */ + if (chdir(cdir) == -1) { + printf("ERROR-1: %s\n", strerror(errno)); + free(cdir); + return -1; + } + + free(cdir); + /* open the load file */ + if ((fd = open("din", O_WRONLY, 0)) == -1) { + printf("ERROR-2: %s\n", strerror(errno)); + return -1; + } + + /* write stuff into the file */ + if ((res=read(fd, buf, count)) == -1) { + printf("ERROR-3: %s\n", strerror(errno)); + return -1; + } + + return res; +} + +int fpgafs_send_cmd(const char *dir, unsigned int cmd) +{ + int fd, res; + char *cdir; + cdir=malloc(sizeof(dir) + 8); + sprintf(cdir, "/fpgafs/%s", dir); + + /* change to the context directory */ + if (chdir(cdir) == -1) { + printf("ERROR-1: %s\n", strerror(errno)); + free(cdir); + return -1; + } + + free(cdir); + /* open the load file */ + if ((fd = open("cmd", O_WRONLY, 0)) == -1) { + printf("ERROR-2: %s\n", strerror(errno)); + return -1; + } + + /* write stuff into the file */ + if ((res=write(fd, (const char *)&cmd, 4)) == -1) { + printf("ERROR-3: %s\n", strerror(errno)); + return -1; + } + + return res; +} + +int fpgafs_get_stat(const char *dir) +{ + int fd; + char *cdir; + char buf[4]; + int res; + cdir=malloc(sizeof(dir) + 8); + sprintf(cdir, "/fpgafs/%s", dir); + + /* change to the context directory */ + if (chdir(cdir) == -1) { + printf("ERROR-1: %s\n", strerror(errno)); + free(cdir); + return -1; + } + + free(cdir); + /* open the load file */ + if ((fd = open("stat", O_RDONLY, 0)) == -1) { + printf("ERROR-2: %s\n", strerror(errno)); + return -1; + } + + /* write stuff into the file */ + if ((res=read(fd, buf, 4)) == -1) { + printf("ERROR-3: %s\n", strerror(errno)); + return -1; + } + + return (int)*(int*)&buf[0]; +} + +int fpgafs_set_lldrv(const char *dir, const char* name) +{ + int fd; + char *cdir; + int res; + cdir=malloc(sizeof(dir) + 8); + sprintf(cdir, "/fpgafs/%s", dir); + + /* change to the context directory */ + if (chdir(cdir) == -1) { + printf("ERROR-1: %s\n", strerror(errno)); + free(cdir); + return -1; + } + + free(cdir); + /* open the load file */ + if ((fd = open("lldrv", O_WRONLY, 0)) == -1) { + printf("ERROR-2: %s\n", strerror(errno)); + return -1; + } + + /* write stuff into the file */ + if ((res=write(fd, name, 5)) == -1) { + printf("ERROR-3: %s\n", strerror(errno)); + return -1; + } + + return 0; +} diff --git a/libfpga/libfpga.h b/libfpga/libfpga.h new file mode 100644 index 0000000..9fbbea0 --- /dev/null +++ b/libfpga/libfpga.h @@ -0,0 +1,9 @@ +/********************************************* + * Benjamin Krill + *********************************************/ +int fpgafs_create_context(int fd, const char *name) +int fpgafs_send_data(const void *buf, size_t count); +int fpgafs_recv_data(void *buf, size_t count); +int fpgafs_send_cmd(unsigned int cmd); +int fpgafs_get_stat(); +int fpgafs_set_lldrv(const char* name);