ioc: kernel-driver: implement keyboard driver

This commit is contained in:
Markus Koch 2019-11-01 19:51:37 +01:00
parent 1e4ba2c739
commit 13d25c6866

View File

@ -6,6 +6,9 @@
#include <linux/of_device.h>
#include <linux/serdev.h>
#include <linux/types.h>
#include <linux/input.h>
// #define ENABLE_DEBUG ENABLE_DEBUG
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Markus Koch");
@ -22,18 +25,86 @@ static struct of_device_id lw35ioc_dt_ids[] = {
};
MODULE_DEVICE_TABLE(of, lw35ioc_dt_ids);
/* Only used for initialization, not for actualy keycode mapping */
static int keycode_map[] = {
KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z,
KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9,
KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9,
KEY_F10, KEY_F11, KEY_F12,
KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, KEY_PAGEUP, KEY_PAGEDOWN,
KEY_102ND, KEY_INSERT, KEY_HOME, KEY_END, KEY_PRINT,
KEY_CAPSLOCK, KEY_LEFTCTRL, KEY_LEFTSHIFT, KEY_LEFTALT, KEY_TAB,
KEY_MINUS, KEY_COMMA, KEY_SEMICOLON, KEY_RIGHTBRACE, KEY_LEFTBRACE,
KEY_GRAVE, KEY_SPACE, KEY_EQUAL, KEY_DOT, KEY_SLASH, KEY_APOSTROPHE,
KEY_BACKSLASH,
KEY_DELETE, KEY_ESC, KEY_ENTER, KEY_BACKSPACE,
KEY_SYSRQ
};
enum lw35ioc_state {SYNC, TYPE, KEYBOARD, PRINTER};
struct lw35ioc_packet {
enum lw35ioc_state state;
};
struct lw35ioc_priv {
struct serdev_device *serdev;
struct input_dev *input_dev;
// USER VARS HERE
struct lw35ioc_packet packet;
};
static int lw35ioc_receive_buf(struct serdev_device *serdev,
const unsigned char *data,
size_t count)
const unsigned char *data,
size_t count)
{
dev_info(&serdev->dev, "RX: %c\n", data[0]);
return 1; // TODO: return proper value
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
unsigned char c;
int ev;
int i;
#ifdef ENABLE_DEBUG
dev_info(&serdev->dev, "IRQ.\n");
#endif
for (i = 0; i < count; ++i) {
c = data[i];
#ifdef ENABLE_DEBUG
dev_info(&serdev->dev, " RX [%d]: %02x\n", priv->packet.state, c);
#endif
switch (priv->packet.state) {
case SYNC:
if (c == 0xFF)
priv->packet.state = TYPE;
break;
case TYPE:
if (c == 0x55)
priv->packet.state = KEYBOARD;
else if (c == 0xAA)
priv->packet.state = PRINTER;
else
priv->packet.state = SYNC;
break;
case KEYBOARD:
if (c > 127) {
c -= 128;
ev = 1;
} else {
ev = 0;
}
input_report_key(priv->input_dev, c, ev);
input_sync(priv->input_dev);
priv->packet.state = SYNC;
break;
case PRINTER:
dev_info(&serdev->dev, "Printer not yet implemented.\n");
priv->packet.state = SYNC;
break;
}
}
return i;
}
static void lw35ioc_write_wakeup(struct serdev_device *serdev)
@ -51,39 +122,70 @@ static int lw35ioc_probe(struct serdev_device *serdev)
{
struct lw35ioc_priv *priv = NULL;
int ret;
int i;
u32 speed = 9600;
struct input_dev *input_dev;
printk(KERN_INFO "lw35ioc: probe\n");
priv = devm_kzalloc(&serdev->dev,
sizeof(struct lw35ioc_priv),
GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv = devm_kzalloc(&serdev->dev,
sizeof(struct lw35ioc_priv),
GFP_KERNEL);
if (!priv) {
printk(KERN_ERR "lw35ioc: Not enough memory.\n");
return -ENOMEM;
}
priv->serdev = serdev;
priv->packet.state = SYNC;
serdev_device_set_drvdata(serdev, priv);
serdev_device_set_client_ops(serdev, &lw35ioc_serdev_ops);
input_dev = devm_input_allocate_device(&serdev->dev);
if (!input_dev) {
dev_err(&serdev->dev, "Unable to allocate input device.\n");
return -ENOMEM;
}
priv->input_dev = input_dev;
input_dev->name = "lw35ioc";
input_dev->phys = "lw35ioc/input1";
input_set_capability(input_dev, EV_MSC, MSC_SCAN);
__set_bit(EV_KEY, input_dev->evbit);
__set_bit(EV_REP, input_dev->evbit);
for (i = 0; i < ARRAY_SIZE(keycode_map); i++)
__set_bit(keycode_map[i], input_dev->keybit);
ret = input_register_device(input_dev);
if (ret) {
dev_err(&serdev->dev, "Unable to register input device.\n");
goto fail0;
}
ret = serdev_device_open(serdev);
if (ret) {
dev_err(&serdev->dev, "Unable to open device.\n");
goto fail0;
goto fail1;
}
speed = serdev_device_set_baudrate(serdev, speed);
dev_info(&serdev->dev, "Using baudrate: %u\n", speed);
dev_info(&serdev->dev, "Loaded driver\n");
dev_info(&serdev->dev, "Loaded driver.\n");
return 0;
fail1:
input_unregister_device(input_dev);
fail0:
devm_kfree(&serdev->dev, input_dev);
return ret;
}
static void lw35ioc_remove(struct serdev_device *serdev)
{
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
serdev_device_close(priv->serdev);
input_unregister_device(priv->input_dev);
dev_info(&serdev->dev, "remove\n");
return;
}