2009-09-14 02:17:59

by Mike Frysinger

[permalink] [raw]
Subject: [PATCH] input/keyboard: new OpenCores Keyboard Controller driver

From: Javier Herrero <[email protected]>

Driver for the keyboard hardware documented here:
http://www.opencores.org/project,keyboardcontroller

Signed-off-by: Javier Herrero <[email protected]>
Signed-off-by: Bryan Wu <[email protected]>
Signed-off-by: Mike Frysinger <[email protected]>
---
drivers/input/keyboard/Kconfig | 9 ++
drivers/input/keyboard/Makefile | 1 +
drivers/input/keyboard/opencores-kbd.c | 196 ++++++++++++++++++++++++++++++++
3 files changed, 206 insertions(+), 0 deletions(-)
create mode 100644 drivers/input/keyboard/opencores-kbd.c

diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index a6b989a..f0f4852 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -310,6 +310,15 @@ config KEYBOARD_SUNKBD
To compile this driver as a module, choose M here: the
module will be called sunkbd.

+config KEYBOARD_OPENCORES
+ tristate "OpenCores Keyboard Controller"
+ help
+ Say Y here if you want to use the OpenCores Keyboard Controller
+ http://www.opencores.org/project,keyboardcontroller
+
+ To compile this driver as a module, choose M here; the
+ module will be called opencores-kbd.
+
config KEYBOARD_SH_KEYSC
tristate "SuperH KEYSC keypad support"
depends on SUPERH
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index b5b5eae..d333bc6 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o
obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
+obj-$(CONFIG_KEYBOARD_OPENCORES) += opencores-kbd.o
obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o
obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o
obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_keysc.o
diff --git a/drivers/input/keyboard/opencores-kbd.c b/drivers/input/keyboard/opencores-kbd.c
new file mode 100644
index 0000000..29223c5
--- /dev/null
+++ b/drivers/input/keyboard/opencores-kbd.c
@@ -0,0 +1,196 @@
+/*
+ * File: drivers/input/keyboard/opencores-kbd.c
+ * Based on: bf54x-keys.c
+ * Author: Javier Herrero <[email protected]>
+ *
+ * Created:
+ * Description: OpenCores Keyboard Controller Driver
+ * http://www.opencores.org/projects.cgi/web/keyboardcontroller/overview
+ *
+ * Modified:
+ * Copyright 2007 HV Sistemas S.L.
+ *
+ * Bugs: Enter bugs at http://blackfin.uclinux.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see the file COPYING, or write
+ * to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+#include <linux/module.h>
+#include <linux/version.h>
+
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/sched.h>
+#include <linux/pm.h>
+#include <linux/sysctl.h>
+#include <linux/proc_fs.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/input.h>
+#include <linux/irq.h>
+#include <linux/io.h>
+
+#define DRV_NAME "opencores-kbd"
+#define NUM_KEYS 128
+
+struct opencores_kbd {
+ struct input_dev *input;
+ struct resource *addr_res;
+ struct resource *irq_res;
+ unsigned short *keycode;
+};
+
+static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
+{
+ unsigned char c;
+ struct platform_device *pdev = dev_id;
+ struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
+ struct input_dev *input = opencores_kbd->input;
+
+ c = readb((void *)opencores_kbd->addr_res->start);
+ input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
+ input_sync(input);
+
+ return IRQ_HANDLED;
+}
+
+static int __devinit opencores_kbd_probe(struct platform_device *pdev)
+{
+ struct input_dev *input;
+ struct opencores_kbd *opencores_kbd;
+ int i, error;
+
+ opencores_kbd = kzalloc(sizeof(*opencores_kbd), GFP_KERNEL);
+ if (!opencores_kbd)
+ return -ENOMEM;
+
+ opencores_kbd->keycode = kmalloc(NUM_KEYS * sizeof(unsigned short), GFP_KERNEL);
+ if (!opencores_kbd->keycode) {
+ error = -ENOMEM;
+ goto out;
+ }
+
+ platform_set_drvdata(pdev, opencores_kbd);
+
+ opencores_kbd->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ opencores_kbd->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+
+ if (opencores_kbd->addr_res == NULL || opencores_kbd->irq_res == NULL) {
+ printk(KERN_ERR "insufficient resources\n");
+ error = -ENOENT;
+ goto out1;
+ }
+
+ error = request_irq(opencores_kbd->irq_res->start, &opencores_kbd_isr, IRQF_TRIGGER_RISING, pdev->name, pdev);
+ if (error) {
+ printk(KERN_ERR DRV_NAME ": Unable to claim irq %d; error %d\n", opencores_kbd->irq_res->start, error);
+ goto out2;
+ }
+
+ input = input_allocate_device();
+ if (!input) {
+ error = -ENOMEM;
+ goto out3;
+ }
+
+ opencores_kbd->input = input;
+
+ input->name = pdev->name;
+ input->phys = "opencores-kbd/input0";
+ input->dev.parent = &pdev->dev;
+
+ input_set_drvdata(input, opencores_kbd);
+
+ input->id.bustype = BUS_HOST;
+ input->id.vendor = 0x0001;
+ input->id.product = 0x0001;
+ input->id.version = 0x0100;
+
+ input->keycodesize = sizeof(*opencores_kbd->keycode);
+ input->keycodemax = NUM_KEYS;
+ input->keycode = opencores_kbd->keycode;
+
+ __set_bit(EV_KEY, input->evbit);
+
+ for (i = 0; i < input->keycodemax; i++) {
+ opencores_kbd->keycode[i] = i;
+ __set_bit(opencores_kbd->keycode[i] & KEY_MAX, input->keybit);
+ }
+ __clear_bit(KEY_RESERVED, input->keybit);
+
+ error = input_register_device(opencores_kbd->input);
+ if (error) {
+ printk(KERN_ERR DRV_NAME ": Unable to register input device (%d)\n", error);
+ goto out2;
+ }
+
+ return 0;
+
+out3:
+ input_free_device(input);
+out2:
+ free_irq(opencores_kbd->irq_res->start, pdev);
+out1:
+ kfree(opencores_kbd->keycode);
+out:
+ kfree(opencores_kbd);
+ platform_set_drvdata(pdev, NULL);
+
+ return error;
+}
+
+static int __devexit opencores_kbd_remove(struct platform_device *pdev)
+{
+ struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
+
+ free_irq(opencores_kbd->irq_res->start, pdev);
+
+ input_unregister_device(opencores_kbd->input);
+
+ kfree(opencores_kbd->keycode);
+ kfree(opencores_kbd);
+ platform_set_drvdata(pdev, NULL);
+
+ return 0;
+}
+
+struct platform_driver opencores_kbd_device_driver = {
+ .probe = opencores_kbd_probe,
+ .remove = __devexit_p(opencores_kbd_remove),
+ .driver = {
+ .name = DRV_NAME,
+ }
+};
+
+static int __init opencores_kbd_init(void)
+{
+ return platform_driver_register(&opencores_kbd_device_driver);
+}
+
+static void __exit opencores_kbd_exit(void)
+{
+ platform_driver_unregister(&opencores_kbd_device_driver);
+}
+
+module_init(opencores_kbd_init);
+module_exit(opencores_kbd_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Javier Herrero <[email protected]");
+MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");
--
1.6.4.2


2009-09-14 06:17:51

by Andrey Panin

[permalink] [raw]
Subject: Re: [PATCH] input/keyboard: new OpenCores Keyboard Controller driver

On 256, 09 13, 2009 at 10:17:58 -0400, Mike Frysinger wrote:
> From: Javier Herrero <[email protected]>
>
> Driver for the keyboard hardware documented here:
> http://www.opencores.org/project,keyboardcontroller
>
> Signed-off-by: Javier Herrero <[email protected]>
> Signed-off-by: Bryan Wu <[email protected]>
> Signed-off-by: Mike Frysinger <[email protected]>
> ---

> +static int __devinit opencores_kbd_probe(struct platform_device *pdev)
> +{
> + struct input_dev *input;
> + struct opencores_kbd *opencores_kbd;
> + int i, error;
> +
> + opencores_kbd = kzalloc(sizeof(*opencores_kbd), GFP_KERNEL);
> + if (!opencores_kbd)
> + return -ENOMEM;
> +
> + opencores_kbd->keycode = kmalloc(NUM_KEYS * sizeof(unsigned short), GFP_KERNEL);
> + if (!opencores_kbd->keycode) {
> + error = -ENOMEM;
> + goto out;
> + }
> +
> + platform_set_drvdata(pdev, opencores_kbd);
> +
> + opencores_kbd->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + opencores_kbd->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
> +
> + if (opencores_kbd->addr_res == NULL || opencores_kbd->irq_res == NULL) {
> + printk(KERN_ERR "insufficient resources\n");
> + error = -ENOENT;
> + goto out1;
> + }
> +
> + error = request_irq(opencores_kbd->irq_res->start, &opencores_kbd_isr, IRQF_TRIGGER_RISING, pdev->name, pdev);

What if interrupt will trigger right here ? It's common pattern for input drivers to
request irq after input_allocate_device().

> + if (error) {
> + printk(KERN_ERR DRV_NAME ": Unable to claim irq %d; error %d\n", opencores_kbd->irq_res->start, error);
> + goto out2;

Looks like out1 should be used here, otherwise you will try to free irq which
was not registered yet.

> + }
> +
> + input = input_allocate_device();
> + if (!input) {
> + error = -ENOMEM;
> + goto out3;

out2 ?

> + }
> +
> + opencores_kbd->input = input;
> +
> + input->name = pdev->name;
> + input->phys = "opencores-kbd/input0";
> + input->dev.parent = &pdev->dev;
> +
> + input_set_drvdata(input, opencores_kbd);
> +
> + input->id.bustype = BUS_HOST;
> + input->id.vendor = 0x0001;
> + input->id.product = 0x0001;
> + input->id.version = 0x0100;
> +
> + input->keycodesize = sizeof(*opencores_kbd->keycode);
> + input->keycodemax = NUM_KEYS;
> + input->keycode = opencores_kbd->keycode;
> +
> + __set_bit(EV_KEY, input->evbit);
> +
> + for (i = 0; i < input->keycodemax; i++) {
> + opencores_kbd->keycode[i] = i;
> + __set_bit(opencores_kbd->keycode[i] & KEY_MAX, input->keybit);
> + }
> + __clear_bit(KEY_RESERVED, input->keybit);
> +
> + error = input_register_device(opencores_kbd->input);
> + if (error) {
> + printk(KERN_ERR DRV_NAME ": Unable to register input device (%d)\n", error);
> + goto out2;

out3 ?

> + }
> +
> + return 0;
> +
> +out3:
> + input_free_device(input);
> +out2:
> + free_irq(opencores_kbd->irq_res->start, pdev);
> +out1:
> + kfree(opencores_kbd->keycode);
> +out:
> + kfree(opencores_kbd);
> + platform_set_drvdata(pdev, NULL);
> +
> + return error;
> +}

2009-09-14 07:04:26

by Mike Frysinger

[permalink] [raw]
Subject: [PATCH v2] input/keyboard: new OpenCores Keyboard Controller driver

From: Javier Herrero <[email protected]>

Driver for the keyboard hardware documented here:
http://www.opencores.org/project,keyboardcontroller

Signed-off-by: Javier Herrero <[email protected]>
Signed-off-by: Bryan Wu <[email protected]>
Signed-off-by: Mike Frysinger <[email protected]>
---
v2
- simpler file comment block
- trim unused headers
- switch to pr_* funcs
- clean up probe() based on Andrey Panin's feedback
- add missing "static" to opencores_kbd_device_driver
- add missing ">" to MODULE_AUTHOR

drivers/input/keyboard/Kconfig | 9 ++
drivers/input/keyboard/Makefile | 1 +
drivers/input/keyboard/opencores-kbd.c | 166 ++++++++++++++++++++++++++++++++
3 files changed, 176 insertions(+), 0 deletions(-)
create mode 100644 drivers/input/keyboard/opencores-kbd.c

diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index a6b989a..f0f4852 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -310,6 +310,15 @@ config KEYBOARD_SUNKBD
To compile this driver as a module, choose M here: the
module will be called sunkbd.

+config KEYBOARD_OPENCORES
+ tristate "OpenCores Keyboard Controller"
+ help
+ Say Y here if you want to use the OpenCores Keyboard Controller
+ http://www.opencores.org/project,keyboardcontroller
+
+ To compile this driver as a module, choose M here; the
+ module will be called opencores-kbd.
+
config KEYBOARD_SH_KEYSC
tristate "SuperH KEYSC keypad support"
depends on SUPERH
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index b5b5eae..d333bc6 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o
obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
+obj-$(CONFIG_KEYBOARD_OPENCORES) += opencores-kbd.o
obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o
obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o
obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_keysc.o
diff --git a/drivers/input/keyboard/opencores-kbd.c b/drivers/input/keyboard/opencores-kbd.c
new file mode 100644
index 0000000..10f011a
--- /dev/null
+++ b/drivers/input/keyboard/opencores-kbd.c
@@ -0,0 +1,166 @@
+/*
+ * OpenCores Keyboard Controller Driver
+ * http://www.opencores.org/project,keyboardcontroller
+ *
+ * Copyright 2007-2009 HV Sistemas S.L.
+ *
+ * Licensed under the GPL-2 or later.
+ */
+
+#define DRV_NAME "opencores-kbd"
+#define pr_fmt(fmt) DRV_NAME ": " fmt
+
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+#define NUM_KEYS 128
+
+struct opencores_kbd {
+ struct input_dev *input;
+ struct resource *addr_res;
+ struct resource *irq_res;
+ unsigned short *keycode;
+};
+
+static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
+{
+ unsigned char c;
+ struct platform_device *pdev = dev_id;
+ struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
+ struct input_dev *input = opencores_kbd->input;
+
+ c = readb((void *)opencores_kbd->addr_res->start);
+ input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
+ input_sync(input);
+
+ return IRQ_HANDLED;
+}
+
+static int __devinit opencores_kbd_probe(struct platform_device *pdev)
+{
+ struct input_dev *input;
+ struct opencores_kbd *opencores_kbd;
+ struct resource *addr_res, *irq_res;
+ int i, error;
+
+ addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+ if (addr_res == NULL || irq_res == NULL) {
+ pr_err("missing board resources\n");
+ return -ENOENT;
+ }
+
+ opencores_kbd = kzalloc(sizeof(*opencores_kbd), GFP_KERNEL);
+ if (!opencores_kbd)
+ return -ENOMEM;
+
+ opencores_kbd->keycode = kmalloc(NUM_KEYS * sizeof(unsigned short), GFP_KERNEL);
+ if (!opencores_kbd->keycode) {
+ error = -ENOMEM;
+ goto err_mem;
+ }
+ opencores_kbd->addr_res = addr_res;
+ opencores_kbd->irq_res = irq_res;
+ platform_set_drvdata(pdev, opencores_kbd);
+
+ input = input_allocate_device();
+ if (!input) {
+ error = -ENOMEM;
+ goto err_in_alloc;
+ }
+
+ opencores_kbd->input = input;
+
+ input->name = pdev->name;
+ input->phys = "opencores-kbd/input0";
+ input->dev.parent = &pdev->dev;
+
+ input_set_drvdata(input, opencores_kbd);
+
+ input->id.bustype = BUS_HOST;
+ input->id.vendor = 0x0001;
+ input->id.product = 0x0001;
+ input->id.version = 0x0100;
+
+ input->keycodesize = sizeof(*opencores_kbd->keycode);
+ input->keycodemax = NUM_KEYS;
+ input->keycode = opencores_kbd->keycode;
+
+ __set_bit(EV_KEY, input->evbit);
+
+ for (i = 0; i < input->keycodemax; i++) {
+ opencores_kbd->keycode[i] = i;
+ __set_bit(opencores_kbd->keycode[i] & KEY_MAX, input->keybit);
+ }
+ __clear_bit(KEY_RESERVED, input->keybit);
+
+ error = input_register_device(input);
+ if (error) {
+ pr_err("unable to register input device\n");
+ goto err_in_reg;
+ }
+
+ error = request_irq(irq_res->start, &opencores_kbd_isr, IRQF_TRIGGER_RISING, pdev->name, pdev);
+ if (error) {
+ pr_err("unable to claim irq %d\n", irq_res->start);
+ goto err_irq;
+ }
+
+ return 0;
+
+ err_irq:
+ input_unregister_device(input);
+ err_in_reg:
+ input_free_device(input);
+ err_in_alloc:
+ kfree(opencores_kbd->keycode);
+ err_mem:
+ kfree(opencores_kbd);
+ platform_set_drvdata(pdev, NULL);
+
+ return error;
+}
+
+static int __devexit opencores_kbd_remove(struct platform_device *pdev)
+{
+ struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
+
+ free_irq(opencores_kbd->irq_res->start, pdev);
+
+ input_unregister_device(opencores_kbd->input);
+
+ kfree(opencores_kbd->keycode);
+ kfree(opencores_kbd);
+ platform_set_drvdata(pdev, NULL);
+
+ return 0;
+}
+
+static struct platform_driver opencores_kbd_device_driver = {
+ .probe = opencores_kbd_probe,
+ .remove = __devexit_p(opencores_kbd_remove),
+ .driver = {
+ .name = DRV_NAME,
+ },
+};
+
+static int __init opencores_kbd_init(void)
+{
+ return platform_driver_register(&opencores_kbd_device_driver);
+}
+module_init(opencores_kbd_init);
+
+static void __exit opencores_kbd_exit(void)
+{
+ platform_driver_unregister(&opencores_kbd_device_driver);
+}
+module_exit(opencores_kbd_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Javier Herrero <[email protected]>");
+MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");
--
1.6.4.2

2009-09-14 07:25:35

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH v2] input/keyboard: new OpenCores Keyboard Controller driver

On Mon, 2009-09-14 at 03:04 -0400, Mike Frysinger wrote:
> From: Javier Herrero <[email protected]>
> +#define pr_fmt(fmt) DRV_NAME ": " fmt

perhaps:
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

2009-09-14 17:40:07

by Mike Frysinger

[permalink] [raw]
Subject: [PATCH v3] input/keyboard: new OpenCores Keyboard Controller driver

From: Javier Herrero <[email protected]>

Driver for the keyboard hardware documented here:
http://www.opencores.org/project,keyboardcontroller

Signed-off-by: Javier Herrero <[email protected]>
Signed-off-by: Bryan Wu <[email protected]>
Signed-off-by: Mike Frysinger <[email protected]>
---
v3
- use KBUILD_MODNAME pointed out by Joe Perches

drivers/input/keyboard/Kconfig | 9 ++
drivers/input/keyboard/Makefile | 1 +
drivers/input/keyboard/opencores-kbd.c | 165 ++++++++++++++++++++++++++++++++
3 files changed, 175 insertions(+), 0 deletions(-)
create mode 100644 drivers/input/keyboard/opencores-kbd.c

diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index a6b989a..f0f4852 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -310,6 +310,15 @@ config KEYBOARD_SUNKBD
To compile this driver as a module, choose M here: the
module will be called sunkbd.

+config KEYBOARD_OPENCORES
+ tristate "OpenCores Keyboard Controller"
+ help
+ Say Y here if you want to use the OpenCores Keyboard Controller
+ http://www.opencores.org/project,keyboardcontroller
+
+ To compile this driver as a module, choose M here; the
+ module will be called opencores-kbd.
+
config KEYBOARD_SH_KEYSC
tristate "SuperH KEYSC keypad support"
depends on SUPERH
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index b5b5eae..d333bc6 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o
obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
+obj-$(CONFIG_KEYBOARD_OPENCORES) += opencores-kbd.o
obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o
obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o
obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_keysc.o
diff --git a/drivers/input/keyboard/opencores-kbd.c b/drivers/input/keyboard/opencores-kbd.c
new file mode 100644
index 0000000..be74ebb
--- /dev/null
+++ b/drivers/input/keyboard/opencores-kbd.c
@@ -0,0 +1,165 @@
+/*
+ * OpenCores Keyboard Controller Driver
+ * http://www.opencores.org/project,keyboardcontroller
+ *
+ * Copyright 2007-2009 HV Sistemas S.L.
+ *
+ * Licensed under the GPL-2 or later.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+#define NUM_KEYS 128
+
+struct opencores_kbd {
+ struct input_dev *input;
+ struct resource *addr_res;
+ struct resource *irq_res;
+ unsigned short *keycode;
+};
+
+static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
+{
+ unsigned char c;
+ struct platform_device *pdev = dev_id;
+ struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
+ struct input_dev *input = opencores_kbd->input;
+
+ c = readb((void *)opencores_kbd->addr_res->start);
+ input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
+ input_sync(input);
+
+ return IRQ_HANDLED;
+}
+
+static int __devinit opencores_kbd_probe(struct platform_device *pdev)
+{
+ struct input_dev *input;
+ struct opencores_kbd *opencores_kbd;
+ struct resource *addr_res, *irq_res;
+ int i, error;
+
+ addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+ if (addr_res == NULL || irq_res == NULL) {
+ pr_err("missing board resources\n");
+ return -ENOENT;
+ }
+
+ opencores_kbd = kzalloc(sizeof(*opencores_kbd), GFP_KERNEL);
+ if (!opencores_kbd)
+ return -ENOMEM;
+
+ opencores_kbd->keycode = kmalloc(NUM_KEYS * sizeof(unsigned short), GFP_KERNEL);
+ if (!opencores_kbd->keycode) {
+ error = -ENOMEM;
+ goto err_mem;
+ }
+ opencores_kbd->addr_res = addr_res;
+ opencores_kbd->irq_res = irq_res;
+ platform_set_drvdata(pdev, opencores_kbd);
+
+ input = input_allocate_device();
+ if (!input) {
+ error = -ENOMEM;
+ goto err_in_alloc;
+ }
+
+ opencores_kbd->input = input;
+
+ input->name = pdev->name;
+ input->phys = "opencores-kbd/input0";
+ input->dev.parent = &pdev->dev;
+
+ input_set_drvdata(input, opencores_kbd);
+
+ input->id.bustype = BUS_HOST;
+ input->id.vendor = 0x0001;
+ input->id.product = 0x0001;
+ input->id.version = 0x0100;
+
+ input->keycodesize = sizeof(*opencores_kbd->keycode);
+ input->keycodemax = NUM_KEYS;
+ input->keycode = opencores_kbd->keycode;
+
+ __set_bit(EV_KEY, input->evbit);
+
+ for (i = 0; i < input->keycodemax; i++) {
+ opencores_kbd->keycode[i] = i;
+ __set_bit(opencores_kbd->keycode[i] & KEY_MAX, input->keybit);
+ }
+ __clear_bit(KEY_RESERVED, input->keybit);
+
+ error = input_register_device(input);
+ if (error) {
+ pr_err("unable to register input device\n");
+ goto err_in_reg;
+ }
+
+ error = request_irq(irq_res->start, &opencores_kbd_isr, IRQF_TRIGGER_RISING, pdev->name, pdev);
+ if (error) {
+ pr_err("unable to claim irq %d\n", irq_res->start);
+ goto err_irq;
+ }
+
+ return 0;
+
+ err_irq:
+ input_unregister_device(input);
+ err_in_reg:
+ input_free_device(input);
+ err_in_alloc:
+ kfree(opencores_kbd->keycode);
+ err_mem:
+ kfree(opencores_kbd);
+ platform_set_drvdata(pdev, NULL);
+
+ return error;
+}
+
+static int __devexit opencores_kbd_remove(struct platform_device *pdev)
+{
+ struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
+
+ free_irq(opencores_kbd->irq_res->start, pdev);
+
+ input_unregister_device(opencores_kbd->input);
+
+ kfree(opencores_kbd->keycode);
+ kfree(opencores_kbd);
+ platform_set_drvdata(pdev, NULL);
+
+ return 0;
+}
+
+static struct platform_driver opencores_kbd_device_driver = {
+ .probe = opencores_kbd_probe,
+ .remove = __devexit_p(opencores_kbd_remove),
+ .driver = {
+ .name = KBUILD_MODNAME,
+ },
+};
+
+static int __init opencores_kbd_init(void)
+{
+ return platform_driver_register(&opencores_kbd_device_driver);
+}
+module_init(opencores_kbd_init);
+
+static void __exit opencores_kbd_exit(void)
+{
+ platform_driver_unregister(&opencores_kbd_device_driver);
+}
+module_exit(opencores_kbd_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Javier Herrero <[email protected]>");
+MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");
--
1.6.4.2

2009-09-14 17:49:58

by Dmitry Torokhov

[permalink] [raw]
Subject: Re: [PATCH v3] input/keyboard: new OpenCores Keyboard Controller driver

Hi Mike,

On Monday 14 September 2009 10:40:03 am Mike Frysinger wrote:
> +
> +struct opencores_kbd {
> + struct input_dev *input;
> + struct resource *addr_res;
> + struct resource *irq_res;
> + unsigned short *keycode;
> +};

Why do we allocate keycode table separately form the main structure?

I think I still have some reservations with the notion that we can just
have exact "scancode" - KEY_* mapping and hardware producers will adjust
the hardware to follow the deriver but I guess it's OK...


--
Dmitry

2009-09-14 18:10:18

by Mike Frysinger

[permalink] [raw]
Subject: Re: [PATCH v3] input/keyboard: new OpenCores Keyboard Controller driver

On Mon, Sep 14, 2009 at 13:49, Dmitry Torokhov wrote:
> On Monday 14 September 2009 10:40:03 am Mike Frysinger wrote:
>> +struct opencores_kbd {
>> +     struct input_dev *input;
>> +     struct resource *addr_res;
>> +     struct resource *irq_res;
>> +     unsigned short *keycode;
>> +};
>
> Why do we allocate keycode table separately form the main structure?

the double alloc looked a little funny, but i didnt dive deep into the
details. but as you point this out, it seems to make sense to me.
any problems with that change Javier ?

i.e. we do:
struct ... { ... unsigned short keycode[NUM_KEYS]; }
rather than doing two calls to kmalloc

> I think I still have some reservations with the notion that we can just
> have exact "scancode" - KEY_* mapping and hardware producers will adjust
> the hardware to follow the deriver but I guess it's OK...

considering this is a piece of "hardware" implemented in FPGAs, i
think it's ok too. if someone really needs more flexibility, then
they're free to extend the driver and submit a patch :).
-mike

2009-09-14 18:25:35

by Javier Herrero

[permalink] [raw]
Subject: Re: [PATCH v3] input/keyboard: new OpenCores Keyboard Controller driver

Hello,

It has a bit long since last time I touched the driver, so I should also
try to refresh my memory about it :). I suppose that you're right in the
double allocation issue (I took another keyboard driver as a starting
point and probably the double allocation was already there...), so feel
free to introduce the change and I will test it as soon as I can.

About the exact scancode - key mapping, the reason is that since the
FPGA opencores device already implements a translation table, I found
that another translation table sounded a bit redundant.

Best regards,

Javier

Mike Frysinger escribió:
> On Mon, Sep 14, 2009 at 13:49, Dmitry Torokhov wrote:
>> On Monday 14 September 2009 10:40:03 am Mike Frysinger wrote:
>>> +struct opencores_kbd {
>>> + struct input_dev *input;
>>> + struct resource *addr_res;
>>> + struct resource *irq_res;
>>> + unsigned short *keycode;
>>> +};
>> Why do we allocate keycode table separately form the main structure?
>
> the double alloc looked a little funny, but i didnt dive deep into the
> details. but as you point this out, it seems to make sense to me.
> any problems with that change Javier ?
>
> i.e. we do:
> struct ... { ... unsigned short keycode[NUM_KEYS]; }
> rather than doing two calls to kmalloc
>
>> I think I still have some reservations with the notion that we can just
>> have exact "scancode" - KEY_* mapping and hardware producers will adjust
>> the hardware to follow the deriver but I guess it's OK...
>
> considering this is a piece of "hardware" implemented in FPGAs, i
> think it's ok too. if someone really needs more flexibility, then
> they're free to extend the driver and submit a patch :).
> -mike
>
>

--
------------------------------------------------------------------------
Javier Herrero EMAIL: [email protected]
HV Sistemas S.L. PHONE: +34 949 336 806
Los Charcones, 17A FAX: +34 949 336 792
19170 El Casar - Guadalajara - Spain WEB: http://www.hvsistemas.com

2009-09-15 05:52:34

by Dmitry Torokhov

[permalink] [raw]
Subject: Re: [PATCH v3] input/keyboard: new OpenCores Keyboard Controller driver

On Mon, Sep 14, 2009 at 08:18:48PM +0200, Javier Herrero wrote:
> Hello,
>
> It has a bit long since last time I touched the driver, so I should also
> try to refresh my memory about it :). I suppose that you're right in the
> double allocation issue (I took another keyboard driver as a starting
> point and probably the double allocation was already there...), so feel
> free to introduce the change and I will test it as soon as I can.
>
> About the exact scancode - key mapping, the reason is that since the
> FPGA opencores device already implements a translation table, I found
> that another translation table sounded a bit redundant.
>

OK, below is what I have now... One concern though - don't we need to do
request_mem_region/ioremap for the addr_res?

--
Dmitry

Input: add driver for OpenCores Keyboard Controller

From: Javier Herrero <[email protected]>

Driver for the keyboard hardware documented here:
http://www.opencores.org/project,keyboardcontroller

Signed-off-by: Javier Herrero <[email protected]>
Signed-off-by: Bryan Wu <[email protected]>
Signed-off-by: Mike Frysinger <[email protected]>
Signed-off-by: Dmitry Torokhov <[email protected]>
---

drivers/input/keyboard/Kconfig | 9 ++
drivers/input/keyboard/Makefile | 1
drivers/input/keyboard/opencores-kbd.c | 162 ++++++++++++++++++++++++++++++++
3 files changed, 172 insertions(+), 0 deletions(-)
create mode 100644 drivers/input/keyboard/opencores-kbd.c


diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index 3525c19..e363077 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -311,6 +311,15 @@ config KEYBOARD_SUNKBD
To compile this driver as a module, choose M here: the
module will be called sunkbd.

+config KEYBOARD_OPENCORES
+ tristate "OpenCores Keyboard Controller"
+ help
+ Say Y here if you want to use the OpenCores Keyboard Controller
+ http://www.opencores.org/project,keyboardcontroller
+
+ To compile this driver as a module, choose M here; the
+ module will be called opencores-kbd.
+
config KEYBOARD_SH_KEYSC
tristate "SuperH KEYSC keypad support"
depends on SUPERH
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index 8a7a22b..ab35ac3 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o
obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
+obj-$(CONFIG_KEYBOARD_OPENCORES) += opencores-kbd.o
obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o
obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o
obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_keysc.o
diff --git a/drivers/input/keyboard/opencores-kbd.c b/drivers/input/keyboard/opencores-kbd.c
new file mode 100644
index 0000000..1bff086
--- /dev/null
+++ b/drivers/input/keyboard/opencores-kbd.c
@@ -0,0 +1,162 @@
+/*
+ * OpenCores Keyboard Controller Driver
+ * http://www.opencores.org/project,keyboardcontroller
+ *
+ * Copyright 2007-2009 HV Sistemas S.L.
+ *
+ * Licensed under the GPL-2 or later.
+ */
+
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+#define NUM_KEYS 128
+
+struct opencores_kbd {
+ struct input_dev *input;
+ void __iomem *addr;
+ int irq;
+ struct resource *irq_res;
+ unsigned short keycodes[NUM_KEYS];
+};
+
+static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
+{
+ struct opencores_kbd *opencores_kbd = dev_id;
+ struct input_dev *input = opencores_kbd->input;
+ unsigned char c;
+
+ c = readb(opencores_kbd->addr);
+ input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
+ input_sync(input);
+
+ return IRQ_HANDLED;
+}
+
+static int __devinit opencores_kbd_probe(struct platform_device *pdev)
+{
+ struct input_dev *input;
+ struct opencores_kbd *opencores_kbd;
+ struct resource *addr_res;
+ int irq;
+ int i, error;
+
+ addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!addr_res) {
+ dev_err(&pdev->dev, "missing board memory resource\n");
+ return -EINVAL;
+ }
+
+ irq = platform_get_irq(pdev, 0);
+ if (irq < 0) {
+ dev_err(&pdev->dev, "missing board IRQ resource\n");
+ return -EINVAL;
+ }
+
+ opencores_kbd = kzalloc(sizeof(*opencores_kbd), GFP_KERNEL);
+ input = input_allocate_device();
+ if (!opencores_kbd || !input) {
+ dev_err(&pdev->dev, "failed to allocate device structures\n");
+ error = -ENOMEM;
+ goto err_free_mem;
+ }
+
+ opencores_kbd->input = input;
+ opencores_kbd->addr = (void __force __iomem *)addr_res->start;
+ opencores_kbd->irq = irq;
+
+ input->name = pdev->name;
+ input->phys = "opencores-kbd/input0";
+ input->dev.parent = &pdev->dev;
+
+ input_set_drvdata(input, opencores_kbd);
+
+ input->id.bustype = BUS_HOST;
+ input->id.vendor = 0x0001;
+ input->id.product = 0x0001;
+ input->id.version = 0x0100;
+
+ input->keycode = opencores_kbd->keycodes;
+ input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
+ input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
+
+ __set_bit(EV_KEY, input->evbit);
+
+ for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
+ /*
+ * OpenCores controller happens to have scancodes match
+ * our KEY_* definitions.
+ */
+ opencores_kbd->keycodes[i] = i;
+ __set_bit(opencores_kbd->keycodes[i], input->keybit);
+ }
+ __clear_bit(KEY_RESERVED, input->keybit);
+
+ error = request_irq(irq, &opencores_kbd_isr,
+ IRQF_TRIGGER_RISING, pdev->name, opencores_kbd);
+ if (error) {
+ dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
+ goto err_free_mem;
+ }
+
+ error = input_register_device(input);
+ if (error) {
+ dev_err(&pdev->dev, "unable to register input device\n");
+ goto err_free_irq;
+ }
+
+ platform_set_drvdata(pdev, opencores_kbd);
+
+ return 0;
+
+ err_free_irq:
+ free_irq(irq, opencores_kbd);
+ err_free_mem:
+ input_free_device(input);
+ kfree(opencores_kbd);
+
+ return error;
+}
+
+static int __devexit opencores_kbd_remove(struct platform_device *pdev)
+{
+ struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
+
+ free_irq(opencores_kbd->irq, opencores_kbd);
+
+ input_unregister_device(opencores_kbd->input);
+ kfree(opencores_kbd);
+
+ platform_set_drvdata(pdev, NULL);
+
+ return 0;
+}
+
+static struct platform_driver opencores_kbd_device_driver = {
+ .probe = opencores_kbd_probe,
+ .remove = __devexit_p(opencores_kbd_remove),
+ .driver = {
+ .name = KBUILD_MODNAME,
+ },
+};
+
+static int __init opencores_kbd_init(void)
+{
+ return platform_driver_register(&opencores_kbd_device_driver);
+}
+module_init(opencores_kbd_init);
+
+static void __exit opencores_kbd_exit(void)
+{
+ platform_driver_unregister(&opencores_kbd_device_driver);
+}
+module_exit(opencores_kbd_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Javier Herrero <[email protected]>");
+MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");

2009-09-15 11:16:48

by Mike Frysinger

[permalink] [raw]
Subject: Re: [PATCH v3] input/keyboard: new OpenCores Keyboard Controller driver

On Tue, Sep 15, 2009 at 01:52, Dmitry Torokhov wrote:
> On Mon, Sep 14, 2009 at 08:18:48PM +0200, Javier Herrero wrote:
>> It has a bit long since last time I touched the driver, so I should also
>> try to refresh my memory about it :). I suppose that you're right in the
>> double allocation issue (I took another keyboard driver as a starting
>> point and probably the double allocation was already there...), so feel
>> free to introduce the change and I will test it as soon as I can.
>>
>> About the exact scancode - key mapping, the reason is that since the
>> FPGA opencores device already implements a translation table, I found
>> that another translation table sounded a bit redundant.
>
> OK, below is what I have now... One concern though - don't we need to do
> request_mem_region/ioremap for the addr_res?

i think so ... these operations are nops on a Blackfin CPU which is
probably why it "just works".

> +struct opencores_kbd {
> +       struct input_dev *input;
> +       void __iomem *addr;
> +       int irq;
> +       struct resource *irq_res;

the irq_res member is no longer needed

+#define NUM_KEYS 128
> +       unsigned short keycodes[NUM_KEYS];

since this is the only usage of NUM_KEYS, could just inline it now ...

> +       error = request_irq(irq, &opencores_kbd_isr,
> +                           IRQF_TRIGGER_RISING, pdev->name, opencores_kbd);
> +       if (error) {
> +               dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
> +               goto err_free_mem;
> +       }
> +
> +       error = input_register_device(input);
> +       if (error) {
> +               dev_err(&pdev->dev, "unable to register input device\n");
> +               goto err_free_irq;
> +       }

the input layer can handle input even if it's not registered ?
-mike

2009-09-15 16:24:13

by Dmitry Torokhov

[permalink] [raw]
Subject: Re: [PATCH v3] input/keyboard: new OpenCores Keyboard Controller driver

On Tue, Sep 15, 2009 at 07:16:25AM -0400, Mike Frysinger wrote:
> On Tue, Sep 15, 2009 at 01:52, Dmitry Torokhov wrote:
> > On Mon, Sep 14, 2009 at 08:18:48PM +0200, Javier Herrero wrote:
> >> It has a bit long since last time I touched the driver, so I should also
> >> try to refresh my memory about it :). I suppose that you're right in the
> >> double allocation issue (I took another keyboard driver as a starting
> >> point and probably the double allocation was already there...), so feel
> >> free to introduce the change and I will test it as soon as I can.
> >>
> >> About the exact scancode - key mapping, the reason is that since the
> >> FPGA opencores device already implements a translation table, I found
> >> that another translation table sounded a bit redundant.
> >
> > OK, below is what I have now... One concern though - don't we need to do
> > request_mem_region/ioremap for the addr_res?
>
> i think so ... these operations are nops on a Blackfin CPU which is
> probably why it "just works".
>

Surely request_mem_region is not a nop? I think even if uoremap is a nop
we need to convert the driver since it does not have to be on a
Blackfin, does it?

> > +struct opencores_kbd {
> > + ? ? ? struct input_dev *input;
> > + ? ? ? void __iomem *addr;
> > + ? ? ? int irq;
> > + ? ? ? struct resource *irq_res;
>
> the irq_res member is no longer needed
>
> +#define NUM_KEYS 128
> > + ? ? ? unsigned short keycodes[NUM_KEYS];
>
> since this is the only usage of NUM_KEYS, could just inline it now ...
>
> > + ? ? ? error = request_irq(irq, &opencores_kbd_isr,
> > + ? ? ? ? ? ? ? ? ? ? ? ? ? IRQF_TRIGGER_RISING, pdev->name, opencores_kbd);
> > + ? ? ? if (error) {
> > + ? ? ? ? ? ? ? dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
> > + ? ? ? ? ? ? ? goto err_free_mem;
> > + ? ? ? }
> > +
> > + ? ? ? error = input_register_device(input);
> > + ? ? ? if (error) {
> > + ? ? ? ? ? ? ? dev_err(&pdev->dev, "unable to register input device\n");
> > + ? ? ? ? ? ? ? goto err_free_irq;
> > + ? ? ? }
>
> the input layer can handle input even if it's not registered ?

The device can survive input events as soon as it was allocated with
input_allocate_device() but of course the event will not get anywhere.
This is a property that I intend to keep since it is very convenient.

--
Dmitry

2009-09-15 16:39:29

by Mike Frysinger

[permalink] [raw]
Subject: Re: [PATCH v3] input/keyboard: new OpenCores Keyboard Controller driver

On Tue, Sep 15, 2009 at 12:23, Dmitry Torokhov wrote:
> On Tue, Sep 15, 2009 at 07:16:25AM -0400, Mike Frysinger wrote:
>> On Tue, Sep 15, 2009 at 01:52, Dmitry Torokhov wrote:
>> > OK, below is what I have now... One concern though - don't we need to do
>> > request_mem_region/ioremap for the addr_res?
>>
>> i think so ... these operations are nops on a Blackfin CPU which is
>> probably why it "just works".
>
> Surely request_mem_region is not a nop? I think even if uoremap is a nop
> we need to convert the driver since it does not have to be on a
> Blackfin, does it?

i wasnt suggesting we not add these calls. i was just stating why
they hadnt already been added -- things work fine on a Blackfin system
w/out ioremap calls because we're a no-mmu system.

>> the input layer can handle input even if it's not registered ?
>
> The device can survive input events as soon as it was allocated with
> input_allocate_device() but of course the event will not get anywhere.
> This is a property that I intend to keep since it is very convenient.

that's fine, i was just curious
-mike

2009-09-16 02:00:45

by Mike Frysinger

[permalink] [raw]
Subject: [PATCH v4] input/keyboard: new OpenCores Keyboard Controller driver

From: Javier Herrero <[email protected]>

Driver for the keyboard hardware documented here:
http://www.opencores.org/project,keyboardcontroller

Signed-off-by: Javier Herrero <[email protected]>
Signed-off-by: Bryan Wu <[email protected]>
Signed-off-by: Mike Frysinger <[email protected]>
Signed-off-by: Dmitry Torokhov <[email protected]>
---
v4
- add request_mem_region/ioremap

drivers/input/keyboard/Kconfig | 9 ++
drivers/input/keyboard/Makefile | 1 +
drivers/input/keyboard/opencores-kbd.c | 175 ++++++++++++++++++++++++++++++++
3 files changed, 185 insertions(+), 0 deletions(-)
create mode 100644 drivers/input/keyboard/opencores-kbd.c

diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index 3525c19..e363077 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -311,6 +311,15 @@ config KEYBOARD_SUNKBD
To compile this driver as a module, choose M here: the
module will be called sunkbd.

+config KEYBOARD_OPENCORES
+ tristate "OpenCores Keyboard Controller"
+ help
+ Say Y here if you want to use the OpenCores Keyboard Controller
+ http://www.opencores.org/project,keyboardcontroller
+
+ To compile this driver as a module, choose M here; the
+ module will be called opencores-kbd.
+
config KEYBOARD_SH_KEYSC
tristate "SuperH KEYSC keypad support"
depends on SUPERH
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index 8a7a22b..ab35ac3 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o
obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
+obj-$(CONFIG_KEYBOARD_OPENCORES) += opencores-kbd.o
obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o
obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o
obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_keysc.o
diff --git a/drivers/input/keyboard/opencores-kbd.c b/drivers/input/keyboard/opencores-kbd.c
new file mode 100644
index 0000000..3b9dd87
--- /dev/null
+++ b/drivers/input/keyboard/opencores-kbd.c
@@ -0,0 +1,175 @@
+/*
+ * OpenCores Keyboard Controller Driver
+ * http://www.opencores.org/project,keyboardcontroller
+ *
+ * Copyright 2007-2009 HV Sistemas S.L.
+ *
+ * Licensed under the GPL-2 or later.
+ */
+
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+struct opencores_kbd {
+ struct input_dev *input;
+ void __iomem *addr;
+ int irq;
+ unsigned short keycodes[128];
+};
+
+static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
+{
+ struct opencores_kbd *opencores_kbd = dev_id;
+ struct input_dev *input = opencores_kbd->input;
+ unsigned char c;
+
+ c = readb(opencores_kbd->addr);
+ input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
+ input_sync(input);
+
+ return IRQ_HANDLED;
+}
+
+static int __devinit opencores_kbd_probe(struct platform_device *pdev)
+{
+ struct input_dev *input;
+ struct opencores_kbd *opencores_kbd;
+ struct resource *res;
+ int irq, i, error;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!res) {
+ dev_err(&pdev->dev, "missing board memory resource\n");
+ return -EINVAL;
+ }
+
+ irq = platform_get_irq(pdev, 0);
+ if (irq < 0) {
+ dev_err(&pdev->dev, "missing board IRQ resource\n");
+ return -EINVAL;
+ }
+
+ opencores_kbd = kzalloc(sizeof(*opencores_kbd), GFP_KERNEL);
+ input = input_allocate_device();
+ if (!opencores_kbd || !input) {
+ dev_err(&pdev->dev, "failed to allocate device structures\n");
+ error = -ENOMEM;
+ goto err_free_mem;
+ }
+
+ res = request_mem_region(res->start, resource_size(res), pdev->name);
+ if (!res) {
+ dev_err(&pdev->dev, "failed to request I/O memory\n");
+ error = -EBUSY;
+ goto err_free_mem;
+ }
+
+ opencores_kbd->addr = ioremap(res->start, resource_size(res));
+ if (!opencores_kbd->addr) {
+ dev_err(&pdev->dev, "failed to remap I/O memory\n");
+ error = -ENXIO;
+ goto err_rel_mem;
+ }
+
+ opencores_kbd->input = input;
+ opencores_kbd->irq = irq;
+
+ input->name = pdev->name;
+ input->phys = "opencores-kbd/input0";
+ input->dev.parent = &pdev->dev;
+
+ input_set_drvdata(input, opencores_kbd);
+
+ input->id.bustype = BUS_HOST;
+ input->id.vendor = 0x0001;
+ input->id.product = 0x0001;
+ input->id.version = 0x0100;
+
+ input->keycode = opencores_kbd->keycodes;
+ input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
+ input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
+
+ __set_bit(EV_KEY, input->evbit);
+
+ for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
+ /*
+ * OpenCores controller happens to have scancodes match
+ * our KEY_* definitions.
+ */
+ opencores_kbd->keycodes[i] = i;
+ __set_bit(opencores_kbd->keycodes[i], input->keybit);
+ }
+ __clear_bit(KEY_RESERVED, input->keybit);
+
+ error = request_irq(irq, &opencores_kbd_isr,
+ IRQF_TRIGGER_RISING, pdev->name, opencores_kbd);
+ if (error) {
+ dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
+ goto err_unmap_mem;
+ }
+
+ error = input_register_device(input);
+ if (error) {
+ dev_err(&pdev->dev, "unable to register input device\n");
+ goto err_free_irq;
+ }
+
+ platform_set_drvdata(pdev, opencores_kbd);
+
+ return 0;
+
+ err_free_irq:
+ free_irq(irq, opencores_kbd);
+ err_unmap_mem:
+ iounmap(opencores_kbd->addr);
+ err_rel_mem:
+ release_mem_region(res->start, resource_size(res));
+ err_free_mem:
+ input_free_device(input);
+ kfree(opencores_kbd);
+
+ return error;
+}
+
+static int __devexit opencores_kbd_remove(struct platform_device *pdev)
+{
+ struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
+
+ free_irq(opencores_kbd->irq, opencores_kbd);
+
+ input_unregister_device(opencores_kbd->input);
+ kfree(opencores_kbd);
+
+ platform_set_drvdata(pdev, NULL);
+
+ return 0;
+}
+
+static struct platform_driver opencores_kbd_device_driver = {
+ .probe = opencores_kbd_probe,
+ .remove = __devexit_p(opencores_kbd_remove),
+ .driver = {
+ .name = "opencores-kbd",
+ },
+};
+
+static int __init opencores_kbd_init(void)
+{
+ return platform_driver_register(&opencores_kbd_device_driver);
+}
+module_init(opencores_kbd_init);
+
+static void __exit opencores_kbd_exit(void)
+{
+ platform_driver_unregister(&opencores_kbd_device_driver);
+}
+module_exit(opencores_kbd_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Javier Herrero <[email protected]>");
+MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");
--
1.6.5.rc1

2009-09-16 03:45:58

by Dmitry Torokhov

[permalink] [raw]
Subject: Re: [PATCH v4] input/keyboard: new OpenCores Keyboard Controller driver

Hi Mike,

On Tue, Sep 15, 2009 at 10:00:40PM -0400, Mike Frysinger wrote:
> +
> +static int __devexit opencores_kbd_remove(struct platform_device *pdev)
> +{
> + struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
> +
> + free_irq(opencores_kbd->irq, opencores_kbd);
> +
> + input_unregister_device(opencores_kbd->input);
> + kfree(opencores_kbd);

You need to do iounmap and free the memory region here as well.
> +
> + platform_set_drvdata(pdev, NULL);
> +
> + return 0;
> +}
> +

--
Dmitry

2009-09-16 03:57:50

by Mike Frysinger

[permalink] [raw]
Subject: Re: [PATCH v4] input/keyboard: new OpenCores Keyboard Controller driver

On Tue, Sep 15, 2009 at 23:45, Dmitry Torokhov wrote:
> On Tue, Sep 15, 2009 at 10:00:40PM -0400, Mike Frysinger wrote:
>> +
>> +static int __devexit opencores_kbd_remove(struct platform_device *pdev)
>> +{
>> +     struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
>> +
>> +     free_irq(opencores_kbd->irq, opencores_kbd);
>> +
>> +     input_unregister_device(opencores_kbd->input);
>> +     kfree(opencores_kbd);
>
> You need to do iounmap and free the memory region here as well.

ugh, i remembered i would have to do this while writing the initial
code, but forgot while double checking the different error paths
-mike

2009-09-16 04:10:05

by Mike Frysinger

[permalink] [raw]
Subject: [PATCH v5] input/keyboard: new OpenCores Keyboard Controller driver

From: Javier Herrero <[email protected]>

Driver for the keyboard hardware documented here:
http://www.opencores.org/project,keyboardcontroller

Signed-off-by: Javier Herrero <[email protected]>
Signed-off-by: Bryan Wu <[email protected]>
Signed-off-by: Mike Frysinger <[email protected]>
Signed-off-by: Dmitry Torokhov <[email protected]>
---
v5
- add missing memory release in remove

drivers/input/keyboard/Kconfig | 9 ++
drivers/input/keyboard/Makefile | 1 +
drivers/input/keyboard/opencores-kbd.c | 180 ++++++++++++++++++++++++++++++++
3 files changed, 190 insertions(+), 0 deletions(-)
create mode 100644 drivers/input/keyboard/opencores-kbd.c

diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index 3525c19..e363077 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -311,6 +311,15 @@ config KEYBOARD_SUNKBD
To compile this driver as a module, choose M here: the
module will be called sunkbd.

+config KEYBOARD_OPENCORES
+ tristate "OpenCores Keyboard Controller"
+ help
+ Say Y here if you want to use the OpenCores Keyboard Controller
+ http://www.opencores.org/project,keyboardcontroller
+
+ To compile this driver as a module, choose M here; the
+ module will be called opencores-kbd.
+
config KEYBOARD_SH_KEYSC
tristate "SuperH KEYSC keypad support"
depends on SUPERH
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index 8a7a22b..ab35ac3 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o
obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
+obj-$(CONFIG_KEYBOARD_OPENCORES) += opencores-kbd.o
obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o
obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o
obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_keysc.o
diff --git a/drivers/input/keyboard/opencores-kbd.c b/drivers/input/keyboard/opencores-kbd.c
new file mode 100644
index 0000000..78cccdd
--- /dev/null
+++ b/drivers/input/keyboard/opencores-kbd.c
@@ -0,0 +1,180 @@
+/*
+ * OpenCores Keyboard Controller Driver
+ * http://www.opencores.org/project,keyboardcontroller
+ *
+ * Copyright 2007-2009 HV Sistemas S.L.
+ *
+ * Licensed under the GPL-2 or later.
+ */
+
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+struct opencores_kbd {
+ struct input_dev *input;
+ struct resource *addr_res;
+ void __iomem *addr;
+ int irq;
+ unsigned short keycodes[128];
+};
+
+static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
+{
+ struct opencores_kbd *opencores_kbd = dev_id;
+ struct input_dev *input = opencores_kbd->input;
+ unsigned char c;
+
+ c = readb(opencores_kbd->addr);
+ input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
+ input_sync(input);
+
+ return IRQ_HANDLED;
+}
+
+static int __devinit opencores_kbd_probe(struct platform_device *pdev)
+{
+ struct input_dev *input;
+ struct opencores_kbd *opencores_kbd;
+ struct resource *res;
+ int irq, i, error;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!res) {
+ dev_err(&pdev->dev, "missing board memory resource\n");
+ return -EINVAL;
+ }
+
+ irq = platform_get_irq(pdev, 0);
+ if (irq < 0) {
+ dev_err(&pdev->dev, "missing board IRQ resource\n");
+ return -EINVAL;
+ }
+
+ opencores_kbd = kzalloc(sizeof(*opencores_kbd), GFP_KERNEL);
+ input = input_allocate_device();
+ if (!opencores_kbd || !input) {
+ dev_err(&pdev->dev, "failed to allocate device structures\n");
+ error = -ENOMEM;
+ goto err_free_mem;
+ }
+
+ opencores_kbd->addr_res = res;
+ res = request_mem_region(res->start, resource_size(res), pdev->name);
+ if (!res) {
+ dev_err(&pdev->dev, "failed to request I/O memory\n");
+ error = -EBUSY;
+ goto err_free_mem;
+ }
+
+ opencores_kbd->addr = ioremap(res->start, resource_size(res));
+ if (!opencores_kbd->addr) {
+ dev_err(&pdev->dev, "failed to remap I/O memory\n");
+ error = -ENXIO;
+ goto err_rel_mem;
+ }
+
+ opencores_kbd->input = input;
+ opencores_kbd->irq = irq;
+
+ input->name = pdev->name;
+ input->phys = "opencores-kbd/input0";
+ input->dev.parent = &pdev->dev;
+
+ input_set_drvdata(input, opencores_kbd);
+
+ input->id.bustype = BUS_HOST;
+ input->id.vendor = 0x0001;
+ input->id.product = 0x0001;
+ input->id.version = 0x0100;
+
+ input->keycode = opencores_kbd->keycodes;
+ input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
+ input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
+
+ __set_bit(EV_KEY, input->evbit);
+
+ for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
+ /*
+ * OpenCores controller happens to have scancodes match
+ * our KEY_* definitions.
+ */
+ opencores_kbd->keycodes[i] = i;
+ __set_bit(opencores_kbd->keycodes[i], input->keybit);
+ }
+ __clear_bit(KEY_RESERVED, input->keybit);
+
+ error = request_irq(irq, &opencores_kbd_isr,
+ IRQF_TRIGGER_RISING, pdev->name, opencores_kbd);
+ if (error) {
+ dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
+ goto err_unmap_mem;
+ }
+
+ error = input_register_device(input);
+ if (error) {
+ dev_err(&pdev->dev, "unable to register input device\n");
+ goto err_free_irq;
+ }
+
+ platform_set_drvdata(pdev, opencores_kbd);
+
+ return 0;
+
+ err_free_irq:
+ free_irq(irq, opencores_kbd);
+ err_unmap_mem:
+ iounmap(opencores_kbd->addr);
+ err_rel_mem:
+ release_mem_region(res->start, resource_size(res));
+ err_free_mem:
+ input_free_device(input);
+ kfree(opencores_kbd);
+
+ return error;
+}
+
+static int __devexit opencores_kbd_remove(struct platform_device *pdev)
+{
+ struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
+
+ free_irq(opencores_kbd->irq, opencores_kbd);
+
+ iounmap(opencores_kbd->addr);
+ release_mem_region(opencores_kbd->addr_res->start,
+ resource_size(opencores_kbd->addr_res));
+ input_unregister_device(opencores_kbd->input);
+ kfree(opencores_kbd);
+
+ platform_set_drvdata(pdev, NULL);
+
+ return 0;
+}
+
+static struct platform_driver opencores_kbd_device_driver = {
+ .probe = opencores_kbd_probe,
+ .remove = __devexit_p(opencores_kbd_remove),
+ .driver = {
+ .name = "opencores-kbd",
+ },
+};
+
+static int __init opencores_kbd_init(void)
+{
+ return platform_driver_register(&opencores_kbd_device_driver);
+}
+module_init(opencores_kbd_init);
+
+static void __exit opencores_kbd_exit(void)
+{
+ platform_driver_unregister(&opencores_kbd_device_driver);
+}
+module_exit(opencores_kbd_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Javier Herrero <[email protected]>");
+MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");
--
1.6.5.rc1