From be59ddb43fbf039ed0bc848ac56b7f72f7a619c8 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Fri, 15 Feb 2019 11:07:10 +0106 Subject: [PATCH 5/7] hello: add led toggling via chardev ioctl Compile userspace application with: $ gcc -Wall -Werror hello-led.c -ohello-led Signed-off-by: John Ogness --- hello-led.c | 38 ++++++++++++++++++++++++++++++++++++++ hello.c | 22 ++++++++++++++++++++++ hello.h | 7 +++++++ 3 files changed, 67 insertions(+) create mode 100644 hello-led.c create mode 100644 hello.h diff --git a/hello-led.c b/hello-led.c new file mode 100644 index 0000000..43230cd --- /dev/null +++ b/hello-led.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "hello.h" + +int main(int argc, char *argv[]) +{ + int fd; + + if (argc != 3) { + fprintf(stderr, "usage: %s \n", argv[0]); + return 1; + } + + fd = open(argv[1], O_RDWR); + if (fd < 0) { + fprintf(stderr, "open() failed: %s\n", strerror(errno)); + return 1; + } + + if (strcmp(argv[2], "heartbeat") == 0) + ioctl(fd, HELLO_IOCTL_TOGGLE_LED, 0); + else if (strcmp(argv[2], "net") == 0) + ioctl(fd, HELLO_IOCTL_TOGGLE_LED, 1); + else if (strcmp(argv[2], "disk") == 0) + ioctl(fd, HELLO_IOCTL_TOGGLE_LED, 2); + else + fprintf(stderr, "unknown led\n"); + + close(fd); + + return 0; +} diff --git a/hello.c b/hello.c index f664121..c42c75f 100644 --- a/hello.c +++ b/hello.c @@ -7,6 +7,7 @@ #include #include #include +#include "hello.h" struct hello_dev { struct device *dev; @@ -95,12 +96,33 @@ static ssize_t hello_write(struct file *f, const char __user *u, size_t s, return s; } +static long hello_ioctl(struct file *f, unsigned int cmd, unsigned long arg) +{ + struct hello_dev *hello = f->private_data; + u32 val; + + dev_info(hello->dev, "%s: cmd=%u arg=%lu\n", __func__, cmd, arg); + + switch (cmd) { + case HELLO_IOCTL_TOGGLE_LED: + val = arg; + if (val >= 0 && val <= 2) + toggle_led(hello, val); + break; + default: + return -EINVAL; + } + + return 0; +} + static const struct file_operations hello_fops = { .owner = THIS_MODULE, .open = hello_open, .release = hello_release, .read = hello_read, .write = hello_write, + .unlocked_ioctl = hello_ioctl, }; static int hello_probe(struct pci_dev *pdev, const struct pci_device_id *ent) diff --git a/hello.h b/hello.h new file mode 100644 index 0000000..cb172d9 --- /dev/null +++ b/hello.h @@ -0,0 +1,7 @@ +#ifndef HELLO_H +#define HELLO_H + +#define HELLO_BASE 0x9F +#define HELLO_IOCTL_TOGGLE_LED _IOW(HELLO_BASE, 0x00, unsigned int) + +#endif /* HELLO_H */ -- 2.11.0