From f4300c72549b313f12811c0563bca494d62340cb Mon Sep 17 00:00:00 2001 From: Stathis Kamperis Date: Sun, 17 Feb 2008 23:54:47 +0200 Subject: [PATCH] Initial import of minimal character device driver based on Brett's Lymn contributions --- cdev/mydev.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cdev/mydev.h | 28 +++++++++++++++++++ cdev/test.c | 27 +++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 cdev/mydev.c create mode 100644 cdev/mydev.h create mode 100644 cdev/test.c diff --git a/cdev/mydev.c b/cdev/mydev.c new file mode 100644 index 0000000..4ec652b --- /dev/null +++ b/cdev/mydev.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +struct mydev_softc { + struct device mydev_dev; +}; + +/* Autoconfiguration glue */ +void mydevattach(struct device *parent, struct device *self, void *aux); +int mydevopen(dev_t device, int flags, int fmt, struct lwp *process); +int mydevclose(dev_t device, int flags, int fmt, struct lwp *process); +int mydevioctl(dev_t device, u_long command, caddr_t data, + int flags, struct lwp *process); + +/* just define the character device handlers because that is all we need */ +const struct cdevsw mydev_cdevsw = { + mydevopen, + mydevclose, + noread, + nowrite, + mydevioctl, + nostop, + notty, + nopoll, + nommap, + nokqfilter, + 0 /* int d_type; */ +}; + +/* + * Attach for autoconfig to find. + */ +void +mydevattach(struct device *parent, struct device *self, void *aux) +{ + /* nothing to do for mydev, this is where resources that + need to be allocated/initialised before open is called + can be set up */ +} + +/* + * Handle an open request on the device. + */ +int +mydevopen(dev_t device, int flags, int fmt, struct lwp *process) +{ + return 0; /* this always succeeds */ +} + +/* + * Handle the close request for the device. + */ +int +mydevclose(dev_t device, int flags, int fmt, struct lwp *process) +{ + return 0; /* again this always succeeds */ +} + +/* + * Handle the ioctl for the device + */ +int +mydevioctl(dev_t device, u_long command, caddr_t data, int flags, + struct lwp *process) +{ + int error; + struct mydev_params *params = (struct mydev_params *)data; + + error = 0; + switch (command) { + case MYDEVTEST: + printf("Got number of %d and string of %s\n", + params->number, params->string); + break; + + default: + error = ENODEV; + } + + return (error); +} diff --git a/cdev/mydev.h b/cdev/mydev.h new file mode 100644 index 0000000..7eca045 --- /dev/null +++ b/cdev/mydev.h @@ -0,0 +1,28 @@ +/* + * + * Definitions for the Mydev pseudo device. + * + */ +#include +#include + +#ifndef MYDEV_H +#define MYDEV_H 1 + +struct mydev_params +{ + int number; + char string[80]; +}; + +#define MYDEVTEST _IOW('S', 0x1, struct mydev_params) + +#ifdef _KERNEL + +/* + * Put kernel inter-module interfaces here, this + * pseudo device has none. + */ + +#endif +#endif diff --git a/cdev/test.c b/cdev/test.c new file mode 100644 index 0000000..4030152 --- /dev/null +++ b/cdev/test.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include + +int main() +{ + struct mydev_params params; + int mydev_dev; + + params.number = 42; + strcpy(params.string, "Hello World"); + + if ((mydev_dev = open("/dev/mydev", O_RDONLY, 0)) < 0) { + fprintf(stderr, "Failed to open /dev/skel\n"); + exit(2); + } + + if (ioctl(mydev_dev, MYDEVTEST, ¶ms) < 0) { + perror("ioctl failed"); + exit(2); + } + + exit(0); +} -- 2.11.4.GIT