fpgafs/libfpga/libfpga.c

201 lines
3.9 KiB
C

/*********************************************
* 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;
}