Commit 0d10a8c5 authored by Alessandro Rubini's avatar Alessandro Rubini

lib: added internal sysfs function; check version in init

parent 70db5b2a
......@@ -39,6 +39,7 @@ int fdelay_init(void)
glob_t glob_dev, glob_sys;
struct __fdelay_board *b;
int i, j;
uint32_t v;
/* Look for boards in /dev: old and new pathnames: only one matches */
glob("/dev/zio-fd-*-0-0-ctrl", 0, NULL, &glob_dev);
......@@ -77,7 +78,20 @@ int fdelay_init(void)
}
globfree(&glob_dev);
globfree(&glob_sys);
return i;
/* Now, if at least one board is there, check the version */
if (fd_nboards == 0)
return 0;
if (fdelay_sysfs_get(fd_boards, "version", &v) < 0)
return -1;
if (v != FDELAY_VERSION) {
fprintf(stderr, "%s: version mismatch, lib(%i) != drv(%i)\n",
__func__, FDELAY_VERSION, v);
errno = EIO;
return -1;
}
return fd_nboards;
}
/* Free and check */
......
......@@ -11,6 +11,7 @@
*/
#ifndef __FDELAY_H__
#define __FDELAY_H__
#include <stdint.h>
/* Opaque data type used as token */
struct fdelay_board;
......@@ -40,5 +41,56 @@ static inline int fdelay_is_verbose(void)
#define __define_board(b, ub) struct __fdelay_board *b = (void *)(ub)
/* These two from ../tools/fdelay-raw.h, used internally */
static inline int __fdelay_sysfs_get(char *path, uint32_t *resp)
{
FILE *f = fopen(path, "r");
if (!f)
return -1;
if (fscanf(f, "%i", resp) != 1) {
fclose(f);
errno = EINVAL;
return -1;
}
fclose(f);
return 0;
}
static inline int __fdelay_sysfs_set(char *path, uint32_t *value)
{
FILE *f = fopen(path, "w");
if (!f)
return -1;
if (fprintf(f, "%i\n", *value) < 2) {
fclose(f);
errno = EINVAL;
return -1;
}
fclose(f);
return 0;
}
/* And these two for the board structure */
static inline int fdelay_sysfs_get(struct __fdelay_board *b, char *name,
uint32_t *resp)
{
char pathname[128];
sprintf(pathname, "%s/%s", b->sysbase, name);
return __fdelay_sysfs_get(pathname, resp);
}
static inline int fdelay_sysfs_set(struct __fdelay_board *b, char *name,
uint32_t *value)
{
char pathname[128];
sprintf(pathname, "%s/%s", b->sysbase, name);
return __fdelay_sysfs_set(pathname, value);
}
#endif /* FDELAY_INTERNAL */
#endif /* __FDELAY_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment