Commit 6f2ac009f29bcbd468a7a2017912dd090abd1348

Authored by Brian Swetland
Committed by Dmitry Torokhov
1 parent cb696e7cf2

Input: goldfish - virtual input event driver

This device is a direct pipe from "hardware" to the input event subsystem,
allowing us to avoid having to route "keypad" style events through an
AT keyboard driver (gross!).

As with the other submissions this driver is cross architecture.

Signed-off-by: Mike A. Chan <mikechan@google.com>
[Tided up to work on x86]
Signed-off-by: Sheng Yang <sheng@linux.intel.com>
Signed-off-by: Yunhong Jiang <yunhong.jiang@intel.com>
Signed-off-by: Xiaohui Xin <xiaohui.xin@intel.com>
Signed-off-by: Jun Nakajima <jun.nakajima@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
[Ported to 3.4]
Signed-off-by: Tom Keel <thomas.keel@intel.com>
[Cleaned up for 3.7 and submission]
Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

Showing 3 changed files with 205 additions and 0 deletions Side-by-side Diff

drivers/input/keyboard/Kconfig
... ... @@ -479,6 +479,16 @@
479 479 To compile this driver as a module, choose M here: the
480 480 module will be called samsung-keypad.
481 481  
  482 +config KEYBOARD_GOLDFISH_EVENTS
  483 + depends on GOLDFISH
  484 + tristate "Generic Input Event device for Goldfish"
  485 + help
  486 + Say Y here to get an input event device for the Goldfish virtual
  487 + device emulator.
  488 +
  489 + To compile this driver as a module, choose M here: the
  490 + module will be called goldfish-events.
  491 +
482 492 config KEYBOARD_STOWAWAY
483 493 tristate "Stowaway keyboard"
484 494 select SERIO
drivers/input/keyboard/Makefile
... ... @@ -13,6 +13,7 @@
13 13 obj-$(CONFIG_KEYBOARD_BFIN) += bf54x-keys.o
14 14 obj-$(CONFIG_KEYBOARD_DAVINCI) += davinci_keyscan.o
15 15 obj-$(CONFIG_KEYBOARD_EP93XX) += ep93xx_keypad.o
  16 +obj-$(CONFIG_KEYBOARD_GOLDFISH_EVENTS) += goldfish_events.o
16 17 obj-$(CONFIG_KEYBOARD_GPIO) += gpio_keys.o
17 18 obj-$(CONFIG_KEYBOARD_GPIO_POLLED) += gpio_keys_polled.o
18 19 obj-$(CONFIG_KEYBOARD_TCA6416) += tca6416-keypad.o
drivers/input/keyboard/goldfish_events.c
  1 +/*
  2 + * Copyright (C) 2007 Google, Inc.
  3 + * Copyright (C) 2012 Intel, Inc.
  4 + *
  5 + * This software is licensed under the terms of the GNU General Public
  6 + * License version 2, as published by the Free Software Foundation, and
  7 + * may be copied, distributed, and modified under those terms.
  8 + *
  9 + * This program is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + */
  15 +
  16 +#include <linux/module.h>
  17 +#include <linux/init.h>
  18 +#include <linux/interrupt.h>
  19 +#include <linux/types.h>
  20 +#include <linux/input.h>
  21 +#include <linux/kernel.h>
  22 +#include <linux/platform_device.h>
  23 +#include <linux/slab.h>
  24 +#include <linux/irq.h>
  25 +#include <linux/io.h>
  26 +
  27 +enum {
  28 + REG_READ = 0x00,
  29 + REG_SET_PAGE = 0x00,
  30 + REG_LEN = 0x04,
  31 + REG_DATA = 0x08,
  32 +
  33 + PAGE_NAME = 0x00000,
  34 + PAGE_EVBITS = 0x10000,
  35 + PAGE_ABSDATA = 0x20000 | EV_ABS,
  36 +};
  37 +
  38 +struct event_dev {
  39 + struct input_dev *input;
  40 + int irq;
  41 + void __iomem *addr;
  42 + char name[0];
  43 +};
  44 +
  45 +static irqreturn_t events_interrupt(int irq, void *dev_id)
  46 +{
  47 + struct event_dev *edev = dev_id;
  48 + unsigned type, code, value;
  49 +
  50 + type = __raw_readl(edev->addr + REG_READ);
  51 + code = __raw_readl(edev->addr + REG_READ);
  52 + value = __raw_readl(edev->addr + REG_READ);
  53 +
  54 + input_event(edev->input, type, code, value);
  55 + input_sync(edev->input);
  56 + return IRQ_HANDLED;
  57 +}
  58 +
  59 +static void events_import_bits(struct event_dev *edev,
  60 + unsigned long bits[], unsigned type, size_t count)
  61 +{
  62 + void __iomem *addr = edev->addr;
  63 + int i, j;
  64 + size_t size;
  65 + uint8_t val;
  66 +
  67 + __raw_writel(PAGE_EVBITS | type, addr + REG_SET_PAGE);
  68 +
  69 + size = __raw_readl(addr + REG_LEN) * 8;
  70 + if (size < count)
  71 + count = size;
  72 +
  73 + addr += REG_DATA;
  74 + for (i = 0; i < count; i += 8) {
  75 + val = __raw_readb(addr++);
  76 + for (j = 0; j < 8; j++)
  77 + if (val & 1 << j)
  78 + set_bit(i + j, bits);
  79 + }
  80 +}
  81 +
  82 +static void events_import_abs_params(struct event_dev *edev)
  83 +{
  84 + struct input_dev *input_dev = edev->input;
  85 + void __iomem *addr = edev->addr;
  86 + u32 val[4];
  87 + int count;
  88 + int i, j;
  89 +
  90 + __raw_writel(PAGE_ABSDATA, addr + REG_SET_PAGE);
  91 +
  92 + count = __raw_readl(addr + REG_LEN) / sizeof(val);
  93 + if (count > ABS_MAX)
  94 + count = ABS_MAX;
  95 +
  96 + for (i = 0; i < count; i++) {
  97 + if (!test_bit(i, input_dev->absbit))
  98 + continue;
  99 +
  100 + for (j = 0; j < ARRAY_SIZE(val); j++) {
  101 + int offset = (i * ARRAY_SIZE(val) + j) * sizeof(u32);
  102 + val[j] = __raw_readl(edev->addr + REG_DATA + offset);
  103 + }
  104 +
  105 + input_set_abs_params(input_dev, i,
  106 + val[0], val[1], val[2], val[3]);
  107 + }
  108 +}
  109 +
  110 +static int events_probe(struct platform_device *pdev)
  111 +{
  112 + struct input_dev *input_dev;
  113 + struct event_dev *edev;
  114 + struct resource *res;
  115 + unsigned keymapnamelen;
  116 + void __iomem *addr;
  117 + int irq;
  118 + int i;
  119 + int error;
  120 +
  121 + irq = platform_get_irq(pdev, 0);
  122 + if (irq < 0)
  123 + return -EINVAL;
  124 +
  125 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  126 + if (!res)
  127 + return -EINVAL;
  128 +
  129 + addr = devm_ioremap(&pdev->dev, res->start, 4096);
  130 + if (!addr)
  131 + return -ENOMEM;
  132 +
  133 + __raw_writel(PAGE_NAME, addr + REG_SET_PAGE);
  134 + keymapnamelen = __raw_readl(addr + REG_LEN);
  135 +
  136 + edev = devm_kzalloc(&pdev->dev,
  137 + sizeof(struct event_dev) + keymapnamelen + 1,
  138 + GFP_KERNEL);
  139 + if (!edev)
  140 + return -ENOMEM;
  141 +
  142 + input_dev = devm_input_allocate_device(&pdev->dev);
  143 + if (!input_dev)
  144 + return -ENOMEM;
  145 +
  146 + edev->input = input_dev;
  147 + edev->addr = addr;
  148 + edev->irq = irq;
  149 +
  150 + for (i = 0; i < keymapnamelen; i++)
  151 + edev->name[i] = __raw_readb(edev->addr + REG_DATA + i);
  152 +
  153 + pr_debug("events_probe() keymap=%s\n", edev->name);
  154 +
  155 + input_dev->name = edev->name;
  156 + input_dev->id.bustype = BUS_HOST;
  157 +
  158 + events_import_bits(edev, input_dev->evbit, EV_SYN, EV_MAX);
  159 + events_import_bits(edev, input_dev->keybit, EV_KEY, KEY_MAX);
  160 + events_import_bits(edev, input_dev->relbit, EV_REL, REL_MAX);
  161 + events_import_bits(edev, input_dev->absbit, EV_ABS, ABS_MAX);
  162 + events_import_bits(edev, input_dev->mscbit, EV_MSC, MSC_MAX);
  163 + events_import_bits(edev, input_dev->ledbit, EV_LED, LED_MAX);
  164 + events_import_bits(edev, input_dev->sndbit, EV_SND, SND_MAX);
  165 + events_import_bits(edev, input_dev->ffbit, EV_FF, FF_MAX);
  166 + events_import_bits(edev, input_dev->swbit, EV_SW, SW_MAX);
  167 +
  168 + events_import_abs_params(edev);
  169 +
  170 + error = devm_request_irq(&pdev->dev, edev->irq, events_interrupt, 0,
  171 + "goldfish-events-keypad", edev);
  172 + if (error)
  173 + return error;
  174 +
  175 + error = input_register_device(input_dev);
  176 + if (error)
  177 + return error;
  178 +
  179 + return 0;
  180 +}
  181 +
  182 +static struct platform_driver events_driver = {
  183 + .probe = events_probe,
  184 + .driver = {
  185 + .owner = THIS_MODULE,
  186 + .name = "goldfish_events",
  187 + },
  188 +};
  189 +
  190 +module_platform_driver(events_driver);
  191 +
  192 +MODULE_AUTHOR("Brian Swetland");
  193 +MODULE_DESCRIPTION("Goldfish Event Device");
  194 +MODULE_LICENSE("GPL");