From: Roman Bazalevsky <rvb@kerberos.rvb-home.lan>
Date: Mon, 20 Nov 2017 13:31:43 +0000 (+0300)
Subject: Thecus N2100 copy-on-button kernel module. Works with triggerhappy daemon.
X-Git-Url: https://git.rvb.name/n2100.git/commitdiff_plain/b935b61117fcb1898f140caa6beb59b0aa83a09a

Thecus N2100 copy-on-button kernel module. Works with triggerhappy daemon.
---

b935b61117fcb1898f140caa6beb59b0aa83a09a
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..0cce455
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,6 @@
+obj-m+=n2100_copy_button.o
+ 
+all:
+	make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
+clean:
+	make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
diff --git a/n2100_copy_button.c b/n2100_copy_button.c
new file mode 100644
index 0000000..6bb4fcc
--- /dev/null
+++ b/n2100_copy_button.c
@@ -0,0 +1,151 @@
+/*
+ *  Driver for buttons on GPIO lines not capable of generating interrupts
+ *
+ *  Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
+ *  Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
+ *
+ *  This file was based on: /drivers/input/misc/cobalt_btns.c
+ *	Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
+ *
+ *  also was based on: /drivers/input/keyboard/gpio_keys.c
+ *	Copyright 2005 Phil Blundell
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/input.h>
+#include <linux/input-polldev.h>
+#include <linux/ioport.h>
+#include <linux/gpio.h>
+#include <linux/gpio_keys.h>
+#include <linux/platform_device.h>
+#include <mach/hardware.h>
+
+#define DRV_NAME	"n2100-copy-button"
+
+struct gpio_keys_polled_device {
+	struct input_polled_dev *poll_dev;
+	struct device *dev;
+	int gpio;
+	int last_state;
+	int count;
+	int threshold;
+};
+
+static struct gpio_keys_polled_device bdev;
+
+static void gpio_keys_polled_check_state(struct input_dev *input,
+					 struct gpio_keys_polled_device *bdata)
+{
+	int state;
+	state = !!gpio_get_value(bdata->gpio);
+
+	if (state != bdata->last_state) {
+		input_event(input, EV_KEY, KEY_COPY,
+			    !state);
+		input_sync(input);
+		bdata->count = 0;
+		bdata->last_state = state;
+	}
+}
+
+static void gpio_keys_polled_poll(struct input_polled_dev *dev)
+{
+	struct gpio_keys_polled_device *bdev = dev->private;
+	struct input_dev *input = dev->input;
+
+	gpio_keys_polled_check_state(input, bdev);
+}
+
+static int __devinit gpio_keys_polled_init(void)
+{
+	struct input_polled_dev *poll_dev;
+	struct input_dev *input;
+	int error;
+	unsigned int gpio = N2100_COPY_BUTTON;
+	
+	poll_dev = input_allocate_polled_device();
+	if (!poll_dev) {
+		pr_err("no memory for polled device\n");
+		error = -ENOMEM;
+		goto err_free_bdev;
+	}
+
+	poll_dev->private = &bdev;
+	poll_dev->poll = gpio_keys_polled_poll;
+	poll_dev->poll_interval = 100;
+
+	input = poll_dev->input;
+
+	input->evbit[0] = BIT(EV_KEY);
+	input->name = "copy-button";
+	input->phys = DRV_NAME"/input0";
+//	input->dev.parent = &pdev->dev;
+
+	input->id.bustype = BUS_HOST;
+	input->id.vendor = 0x0001;
+	input->id.product = 0x0001;
+	input->id.version = 0x0100;
+
+	error = gpio_request(gpio,
+			     "N2100 copy button");
+	if (error) {
+		pr_err("unable to claim gpio %u, err=%d\n",
+			gpio, error);
+		goto err_free_gpio;
+	}
+
+	error = gpio_direction_input(gpio);
+	if (error) {
+		pr_err("unable to set direction on gpio %u, err=%d\n",
+			gpio, error);
+		goto err_free_gpio;
+	}
+
+	bdev.last_state = -1;
+	bdev.threshold = 2;
+
+	input_set_capability(input, EV_KEY, KEY_COPY);
+
+	bdev.poll_dev = poll_dev;
+
+	error = input_register_polled_device(poll_dev);
+	if (error) {
+		pr_err("unable to register polled device, err=%d\n",
+			error);
+		goto err_free_gpio;
+	}
+
+	/* report initial state of the buttons */
+	gpio_keys_polled_check_state(input, &bdev);
+
+	return 0;
+
+err_free_gpio:
+	gpio_free(N2100_COPY_BUTTON);
+	input_free_polled_device(poll_dev);
+
+err_free_bdev:
+	return error;
+}
+
+static void __devexit gpio_keys_polled_exit(void)
+{
+	input_unregister_polled_device(bdev.poll_dev);
+	gpio_free(N2100_COPY_BUTTON);
+	input_free_polled_device(bdev.poll_dev);
+}
+
+module_init(gpio_keys_polled_init);
+module_exit(gpio_keys_polled_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Roman Bazalevskiy <rvb@rvb.name>, Gabor Juhos <juhosg@openwrt.org>");
+MODULE_DESCRIPTION("Polled GPIO Copy Buttons driver for Thecus N2100");
+MODULE_ALIAS("platform:" DRV_NAME);