diff --git a/include/driver.h b/include/driver.h new file mode 100644 index 0000000..f594acd --- /dev/null +++ b/include/driver.h @@ -0,0 +1,34 @@ +#ifndef __DRIVER_H__ +#define __DRIVER_H__ + +#include + +struct driver; + +typedef int (*fp_open_t)(const struct driver *); +typedef int (*fp_close_t)(const struct driver *); +typedef int (*fp_read_t)(const struct driver *, char *, unsigned int); +typedef int (*fp_write_t)(const struct driver *, const char *, unsigned int); +typedef int (*fp_ioctl_t)(const struct driver *, unsigned int, unsigned int argc, va_list); + +struct driver_fp { + fp_open_t open; + fp_close_t close; + fp_read_t read; + fp_write_t write; + fp_ioctl_t ioctl; +}; + +struct driver { + const char *name; + const struct driver_fp *fp; + const void *dev; +}; + +int drv_open(const struct driver *drv); +int drv_close(const struct driver *drv); +int drv_read(const struct driver *drv, char *buffer, unsigned int length); +int drv_write(const struct driver *drv, const char *buffer, unsigned int length); +int drv_ioctl(const struct driver *drv, unsigned int cmd, unsigned int argc, ...); + +#endif diff --git a/src/driver.c b/src/driver.c new file mode 100644 index 0000000..3892c47 --- /dev/null +++ b/src/driver.c @@ -0,0 +1,67 @@ +#include +#include + +#include "driver.h" + +int drv_open(const struct driver *drv) +{ + int res = -1; + assert(drv != NULL); + + if(drv->fp->open) { + res = drv->fp->open(drv); + } + + return res; +} + +int drv_close(const struct driver *drv) +{ + int res = -1; + assert(drv != NULL); + + if(drv->fp->close) { + res = drv->fp->close(drv); + } + + return res; +} + +int drv_read(const struct driver *drv, char *buffer, unsigned int length) +{ + int res = -1; + assert(drv != NULL); + + if(drv->fp->read) { + res = drv->fp->read(drv, buffer, length); + } + + return res; +} + +int drv_write(const struct driver *drv, const char *buffer, unsigned int length) +{ + int res = -1; + assert(drv != NULL); + + if(drv->fp->write) { + res = drv->fp->write(drv, buffer, length); + } + + return res; +} + +int drv_ioctl(const struct driver *drv, unsigned int cmd, unsigned int argc, ...) +{ + int res = -1; + assert(drv != NULL); + + if(drv->fp->ioctl) { + va_list args; + va_start(args, argc); + res = drv->fp->ioctl(drv, cmd, argc, args); + va_end(args); + } + + return res; +}