lw35-upgrade/ioc/kernel-driver/lw35ioc.c

514 lines
13 KiB
C

#include <linux/device.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/serdev.h>
#include <linux/types.h>
#include <linux/input.h>
#include <linux/gpio/consumer.h>
#include <linux/uaccess.h>
#include <linux/sched.h>
#include <linux/poll.h>
// #define ENABLE_DEBUG ENABLE_DEBUG
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Markus Koch");
MODULE_DESCRIPTION("LW-35 I/O Controller Driver");
MODULE_VERSION("0.1");
static int debug_level = 0;
module_param(debug_level, int, 0);
MODULE_PARM_DESC(debug_level, "Debug level");
/* Only for chrdev... man they really suck! */
static struct serdev_device *static_serdev = NULL;
static DECLARE_WAIT_QUEUE_HEAD(wq);
static struct of_device_id lw35ioc_dt_ids[] = {
{ .compatible = "brother,lw35", },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, lw35ioc_dt_ids);
/* Chardev */
#define DEVICE_NAME "lw35"
#define CLASS_NAME "lw35ioc"
/* 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 {PASSIVE = 0, PROG_KBD, PROG_PRI, BYPASS, ACTIVE, STATE_COUNT};
enum lw35ioc_packet_state {SYNC, TYPE, KEYBOARD, PRINTER};
struct lw35ioc_packet {
enum lw35ioc_packet_state state;
};
struct lw35ioc_chardev {
int major_number;
struct class* class;
struct device* device;
int open_count;
};
#define UART_BUF_SIZE 256
struct lw35ioc_priv {
struct serdev_device *serdev;
struct input_dev *input_dev;
struct gpio_desc *gpio_rst; /* GPIO to reset both AVRs */
struct lw35ioc_chardev chardev;
char uart_buf_rx[UART_BUF_SIZE];
char *uart_buf_rx_rd;
char *uart_buf_rx_wr;
enum lw35ioc_state state;
struct lw35ioc_packet packet;
};
static int lw35ioc_process_buf(struct serdev_device *serdev,
const unsigned char *data,
size_t count)
{
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 int lw35ioc_receive_buf(struct serdev_device *serdev,
const unsigned char *data,
size_t count)
{
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
int i;
if (priv->state == ACTIVE)
return lw35ioc_process_buf(serdev, data, count);
for (i = 0; i < count; ++i) {
#ifdef ENABLE_DEBUG
dev_info(&serdev->dev, " RX [PSV]: %02x\n", data[i]);
#endif
*(priv->uart_buf_rx_wr) = data[i];
if (priv->uart_buf_rx_wr == priv->uart_buf_rx + UART_BUF_SIZE - 1)
priv->uart_buf_rx_wr = priv->uart_buf_rx;
else
priv->uart_buf_rx_wr++;
if (priv->uart_buf_rx_rd == priv->uart_buf_rx_wr) {
dev_err(&serdev->dev, "UART RX buffer overflow.\n");
}
}
wake_up_interruptible(&wq);
return count; /* Pretend we read all data */
}
static void lw35ioc_write_wakeup(struct serdev_device *serdev)
{
// schedule work here
}
static struct serdev_device_ops lw35ioc_serdev_ops = {
.receive_buf = lw35ioc_receive_buf,
.write_wakeup = lw35ioc_write_wakeup,
};
static void lw35ioc_set_hw_reset(struct serdev_device *serdev,
int reset_active)
{
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
gpiod_set_value(priv->gpio_rst, !!reset_active);
}
static void lw35ioc_reset_ioc_hw(struct serdev_device *serdev)
{
dev_info(&serdev->dev, "Resetting IOC...\n");
lw35ioc_set_hw_reset(serdev, 1);
usleep_range(90000, 100000);
lw35ioc_set_hw_reset(serdev, 0);
usleep_range(90000, 100000);
}
/* Chardev */
static int lw35ioc_chardev_open(struct inode *inodep, struct file *filep) {
struct serdev_device *serdev = static_serdev;
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
filep->private_data = serdev;
if (!priv->chardev.open_count) {
dev_info(&serdev->dev, "Opened chardev.\n");
if (priv->state != ACTIVE &&
priv->state != PASSIVE) {
lw35ioc_reset_ioc_hw(serdev);
switch (priv->state) {
case PROG_KBD:
serdev_device_write(serdev, "X", 1, 0);
dev_info(&serdev->dev, "Put ioc into programming mode: keyboard.\n");
break;
case PROG_PRI:
serdev_device_write(serdev, "Y", 1, 0);
dev_info(&serdev->dev, "Put ioc into programming mode: printer.\n");
break;
case BYPASS:
serdev_device_write(serdev, "XX", 2, 0);
dev_info(&serdev->dev, "Put ioc into bypass mode.\n");
break;
default:
/* For other modes, we just wait for the reset to be over */
usleep_range(1000000, 1010000);
break;
}
}
} else {
dev_err(&serdev->dev, "Rejected. Device may only be opened once.\n");
//return -EBUSY;
}
priv->chardev.open_count++;
return 0;
}
static ssize_t lw35ioc_chardev_read(struct file *filep, char *buffer, size_t len, loff_t *offset) {
struct serdev_device *serdev = filep->private_data;
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
int error_count;
int available;
available = priv->uart_buf_rx_wr - priv->uart_buf_rx_rd;
if (available < 0)
available = 1;
if (!available) { /* Wait for data to become available */
wait_event_interruptible(wq, priv->uart_buf_rx_rd != priv->uart_buf_rx_wr);
available = priv->uart_buf_rx_wr - priv->uart_buf_rx_rd;
if (available < 0)
available = 1;
if (!available)
return -EAGAIN;
}
if (available < len)
len = available;
error_count = copy_to_user(buffer, priv->uart_buf_rx_rd, len);
len -= error_count;
priv->uart_buf_rx_rd += len;
if (priv->uart_buf_rx_rd >= priv->uart_buf_rx + UART_BUF_SIZE) {
priv->uart_buf_rx_rd -= UART_BUF_SIZE;
}
if (error_count == 0) {
return len;
} else {
dev_err(&serdev->dev, "Failed to read %d characters to chardev.\n",
error_count);
return -EFAULT;
}
}
char msg[256]; // TODO
static ssize_t lw35ioc_chardev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset) {
struct serdev_device *serdev = filep->private_data;
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
if (len > 256) {
len = 256;
}
copy_from_user(msg, buffer, len);
serdev_device_write_buf(serdev, msg, len);
return len;
}
static int lw35ioc_chardev_release(struct inode *inodep, struct file *filep){
struct serdev_device *serdev = filep->private_data;
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
priv->chardev.open_count--;
dev_info(&serdev->dev, "Device successfully closed\n");
return 0;
}
#define IOCTL_SET_MODE 42
static long lw35ioc_chardev_ioctl(struct file *filep, unsigned int cmd,
unsigned long arg)
{
struct serdev_device *serdev = filep->private_data;
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
switch (cmd) {
case IOCTL_SET_MODE:
if (arg >= STATE_COUNT || arg < 0) {
dev_err(&serdev->dev, "Invalid driver mode: %lu", arg);
return -1;
}
priv->state = arg;
dev_info(&serdev->dev, "Switched driver mode to: %lu. Will be applied with next open().\n", arg);
break;
default:
dev_info(&serdev->dev, "Ignoring IOCTL %u(%lu)\n", cmd, arg);
break;
}
return 0;
}
__poll_t poll(struct file *filep, struct poll_table_struct *ptab)
{
struct serdev_device *serdev = filep->private_data;
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
poll_wait(filep, &wq, ptab);
if (priv->uart_buf_rx_wr != priv->uart_buf_rx_rd)
return EPOLLIN | EPOLLRDNORM;
return 0;
}
static struct file_operations fops =
{
.open = lw35ioc_chardev_open,
.read = lw35ioc_chardev_read,
.write = lw35ioc_chardev_write,
.release = lw35ioc_chardev_release,
.unlocked_ioctl = lw35ioc_chardev_ioctl,
.poll = poll,
};
static int lw35ioc_cdev_init(struct serdev_device *serdev)
{
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
priv->chardev.open_count = 0;
// Try to dynamically allocate a major number for the device
priv->chardev.major_number = register_chrdev(0, DEVICE_NAME, &fops);
if (priv->chardev.major_number < 0) {
dev_err(&serdev->dev, "Failed to register a major number\n");
return priv->chardev.major_number;
}
dev_info(&serdev->dev, "Registered correctly with major number %d\n", priv->chardev.major_number);
// Register the device class
priv->chardev.class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(priv->chardev.class)) {
unregister_chrdev(priv->chardev.major_number, DEVICE_NAME);
dev_err(&serdev->dev, "Failed to register device class\n");
return PTR_ERR(priv->chardev.class);
}
dev_info(&serdev->dev, "Device class registered correctly\n");
// Register the device driver
priv->chardev.device = device_create(priv->chardev.class, NULL, MKDEV(priv->chardev.major_number, 0), NULL, DEVICE_NAME);
if (IS_ERR(priv->chardev.device)) {
class_destroy(priv->chardev.class);
unregister_chrdev(priv->chardev.major_number, DEVICE_NAME);
dev_err(&serdev->dev, "Failed to create the device\n");
return PTR_ERR(priv->chardev.device);
}
dev_err(&serdev->dev, "Device class created correctly\n");
return 0;
}
static int lw35ioc_probe(struct serdev_device *serdev)
{
struct lw35ioc_priv *priv = NULL;
int ret = 0;
int i;
u32 speed = 19200;
struct input_dev *input_dev;
printk(KERN_INFO "lw35ioc: probe\n");
if (static_serdev) {
printk(KERN_ERR "lw35ioc: Can only be loaded once.\n");
goto fail_multicheck;
}
/* Allocate memory */
priv = devm_kzalloc(&serdev->dev,
sizeof(struct lw35ioc_priv),
GFP_KERNEL);
if (!priv) {
printk(KERN_ERR "lw35ioc: Not enough memory.\n");
return -ENOMEM;
}
priv->state = PASSIVE;
priv->uart_buf_rx_rd = priv->uart_buf_rx;
priv->uart_buf_rx_wr = priv->uart_buf_rx;
priv->serdev = serdev;
static_serdev = serdev;
priv->packet.state = SYNC;
serdev_device_set_drvdata(serdev, priv);
serdev_device_set_client_ops(serdev, &lw35ioc_serdev_ops);
/* Register input device */
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 fail_input;
}
ret = serdev_device_open(serdev);
if (ret) {
dev_err(&serdev->dev, "Unable to open device.\n");
goto fail_serdev;
}
speed = serdev_device_set_baudrate(serdev, speed);
dev_info(&serdev->dev, "Using baudrate: %u\n", speed);
/* Register GPIO */
priv->gpio_rst = gpiod_get_index(&serdev->dev,
"rst",
0,
GPIOD_OUT_HIGH);
/* Register character device */
ret = lw35ioc_cdev_init(serdev);
if (ret) {
dev_err(&serdev->dev, "Unable to initialize character device.\n");
goto fail_gpio;
}
if (IS_ERR(priv->gpio_rst)) {
dev_err(&serdev->dev, "Unable to get reset GPIO.\n");
ret = -ENODEV; /* TODO */
goto fail_gpio;
}
lw35ioc_reset_ioc_hw(serdev);
dev_info(&serdev->dev, "Loaded driver.\n");
return 0;
fail_gpio:
fail_serdev:
input_unregister_device(input_dev);
fail_input:
devm_kfree(&serdev->dev, input_dev);
fail_multicheck:
return ret;
}
static void lw35ioc_remove(struct serdev_device *serdev)
{
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
device_destroy(priv->chardev.class, MKDEV(priv->chardev.major_number, 0));
class_unregister(priv->chardev.class);
class_destroy(priv->chardev.class);
unregister_chrdev(priv->chardev.major_number, DEVICE_NAME);
lw35ioc_set_hw_reset(serdev, 1);
gpiod_put(priv->gpio_rst);
serdev_device_close(priv->serdev);
input_unregister_device(priv->input_dev);
dev_info(&serdev->dev, "remove\n");
return;
}
static struct serdev_device_driver lw35ioc_driver = {
.probe = lw35ioc_probe,
.remove = lw35ioc_remove,
.driver = {
.name = "lw35ioc",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(lw35ioc_dt_ids),
},
};
module_serdev_device_driver(lw35ioc_driver);