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
This commit is contained in:
ben 2007-09-11 09:09:18 +00:00
parent 8efdba0c21
commit 11762c3dab
3 changed files with 222 additions and 0 deletions

13
libfpga/Makefile Normal file
View File

@ -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.*

200
libfpga/libfpga.c Normal file
View File

@ -0,0 +1,200 @@
/*********************************************
* Benjamin Krill <ben@codiert.org>
*********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
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;
}

9
libfpga/libfpga.h Normal file
View File

@ -0,0 +1,9 @@
/*********************************************
* Benjamin Krill <ben@codiert.org>
*********************************************/
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);