Compare commits

..

No commits in common. "0da970d40fdab8dbb39563e1e8e01b4aaa590c56" and "101117d0bc69d2693f6489793edf2300f2814b1a" have entirely different histories.

2 changed files with 9 additions and 352 deletions

View File

@ -8,9 +8,6 @@
#include <linux/types.h> #include <linux/types.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/gpio/consumer.h> #include <linux/gpio/consumer.h>
#include <linux/uaccess.h>
#include <linux/sched.h>
#include <linux/poll.h>
// #define ENABLE_DEBUG ENABLE_DEBUG // #define ENABLE_DEBUG ENABLE_DEBUG
@ -23,20 +20,12 @@ static int debug_level = 0;
module_param(debug_level, int, 0); module_param(debug_level, int, 0);
MODULE_PARM_DESC(debug_level, "Debug level"); 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[] = { static struct of_device_id lw35ioc_dt_ids[] = {
{ .compatible = "brother,lw35", }, { .compatible = "brother,lw35", },
{ /* sentinel */ } { /* sentinel */ }
}; };
MODULE_DEVICE_TABLE(of, lw35ioc_dt_ids); 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 */ /* Only used for initialization, not for actualy keycode mapping */
static int keycode_map[] = { 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_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
@ -55,7 +44,7 @@ static int keycode_map[] = {
KEY_SYSRQ KEY_SYSRQ
}; };
enum lw35ioc_state {PASSIVE = 0, PROG_KBD, PROG_PRI, BYPASS, ACTIVE, STATE_COUNT}; enum lw35ioc_state {PASSIVE, PASSTHROUGH, ACTIVE};
enum lw35ioc_packet_state {SYNC, TYPE, KEYBOARD, PRINTER}; enum lw35ioc_packet_state {SYNC, TYPE, KEYBOARD, PRINTER};
@ -63,24 +52,10 @@ struct lw35ioc_packet {
enum lw35ioc_packet_state state; 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 lw35ioc_priv {
struct serdev_device *serdev; struct serdev_device *serdev;
struct input_dev *input_dev; struct input_dev *input_dev;
struct gpio_desc *gpio_rst; /* GPIO to reset both AVRs */ 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; enum lw35ioc_state state;
struct lw35ioc_packet packet; struct lw35ioc_packet packet;
@ -143,33 +118,15 @@ static int lw35ioc_receive_buf(struct serdev_device *serdev,
size_t count) size_t count)
{ {
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev); struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
int i;
if (priv->state == ACTIVE) if (priv->state == ACTIVE)
return lw35ioc_process_buf(serdev, data, count); 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 */ return count; /* Pretend we read all data */
} }
static void lw35ioc_write_wakeup(struct serdev_device *serdev) static void lw35ioc_write_wakeup(struct serdev_device *serdev)
{ {
dev_info(&serdev->dev, "Write wakeup\n");
// schedule work here // schedule work here
} }
@ -183,224 +140,19 @@ static void lw35ioc_set_hw_reset(struct serdev_device *serdev,
{ {
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev); struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
gpiod_set_value(priv->gpio_rst, !!reset_active); 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) static int lw35ioc_probe(struct serdev_device *serdev)
{ {
struct lw35ioc_priv *priv = NULL; struct lw35ioc_priv *priv = NULL;
int ret = 0; int ret;
int i; int i;
u32 speed = 19200; u32 speed = 19200;
struct input_dev *input_dev; struct input_dev *input_dev;
printk(KERN_INFO "lw35ioc: probe\n"); printk(KERN_INFO "lw35ioc: probe\n");
if (static_serdev) {
printk(KERN_ERR "lw35ioc: Can only be loaded once.\n");
goto fail_multicheck;
}
/* Allocate memory */ /* Allocate memory */
priv = devm_kzalloc(&serdev->dev, priv = devm_kzalloc(&serdev->dev,
sizeof(struct lw35ioc_priv), sizeof(struct lw35ioc_priv),
@ -409,13 +161,8 @@ static int lw35ioc_probe(struct serdev_device *serdev)
printk(KERN_ERR "lw35ioc: Not enough memory.\n"); printk(KERN_ERR "lw35ioc: Not enough memory.\n");
return -ENOMEM; return -ENOMEM;
} }
priv->state = PASSIVE; priv->state = PASSIVE;
priv->uart_buf_rx_rd = priv->uart_buf_rx;
priv->uart_buf_rx_wr = priv->uart_buf_rx;
priv->serdev = serdev; priv->serdev = serdev;
static_serdev = serdev;
priv->packet.state = SYNC; priv->packet.state = SYNC;
serdev_device_set_drvdata(serdev, priv); serdev_device_set_drvdata(serdev, priv);
serdev_device_set_client_ops(serdev, &lw35ioc_serdev_ops); serdev_device_set_client_ops(serdev, &lw35ioc_serdev_ops);
@ -453,23 +200,14 @@ static int lw35ioc_probe(struct serdev_device *serdev)
priv->gpio_rst = gpiod_get_index(&serdev->dev, priv->gpio_rst = gpiod_get_index(&serdev->dev,
"rst", "rst",
0, 0,
GPIOD_OUT_HIGH); GPIOD_OUT_LOW);
lw35ioc_set_hw_reset(serdev, 0);
/* 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)) { if (IS_ERR(priv->gpio_rst)) {
dev_err(&serdev->dev, "Unable to get reset GPIO.\n"); dev_err(&serdev->dev, "Unable to get reset GPIO.\n");
ret = -ENODEV; /* TODO */ ret = -ENODEV;
goto fail_gpio; goto fail_gpio;
} }
lw35ioc_reset_ioc_hw(serdev);
dev_info(&serdev->dev, "Loaded driver.\n"); dev_info(&serdev->dev, "Loaded driver.\n");
return 0; return 0;
@ -478,7 +216,6 @@ fail_serdev:
input_unregister_device(input_dev); input_unregister_device(input_dev);
fail_input: fail_input:
devm_kfree(&serdev->dev, input_dev); devm_kfree(&serdev->dev, input_dev);
fail_multicheck:
return ret; return ret;
} }
@ -486,11 +223,6 @@ static void lw35ioc_remove(struct serdev_device *serdev)
{ {
struct lw35ioc_priv *priv = serdev_device_get_drvdata(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); lw35ioc_set_hw_reset(serdev, 1);
gpiod_put(priv->gpio_rst); gpiod_put(priv->gpio_rst);
serdev_device_close(priv->serdev); serdev_device_close(priv->serdev);
@ -511,3 +243,4 @@ static struct serdev_device_driver lw35ioc_driver = {
}; };
module_serdev_device_driver(lw35ioc_driver); module_serdev_device_driver(lw35ioc_driver);

View File

@ -1,76 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stropts.h>
#include <sys/stat.h>
#include <fcntl.h>
#define IOCTL_SET_MODE 42
enum lw35ioc_state {PASSIVE = 0, PROG_KBD, PROG_PRI, BYPASS, ACTIVE, STATE_COUNT};
int f;
static void help(int argc, char *argv[])
{
fprintf(stderr,
"usage: %s <command>\n\n"
" command: set-mode <mode>\n"
" mode: passive, active, rfu-kbd, rfu-pri, bypass\n",
argv[0]);
}
static int set_mode(int argc, char *argv[])
{
int mode = -1;
if (argc < 3) {
help(argc, argv);
return 10;
}
if (!strcmp(argv[2], "passive"))
mode = PASSIVE;
else if (!strcmp(argv[2], "rfu-kbd"))
mode = PROG_KBD;
else if (!strcmp(argv[2], "rfu-pri"))
mode = PROG_PRI;
else if (!strcmp(argv[2], "bypass"))
mode = BYPASS;
else if (!strcmp(argv[2], "active"))
mode = ACTIVE;
if (mode == -1) {
fprintf(stderr, "Error: Invalid mode.\n");
return 10;
}
ioctl(f, IOCTL_SET_MODE, mode);
return 0;
}
int main(int argc, char *argv[])
{
int ret;
if (argc < 2) {
help(argc, argv);
return 1;
}
f = open("/dev/lw35", O_RDWR);
if (f == -1) {
fprintf(stderr, "Error opening lw35ioc device.\n");
return 3;
}
if (!strcmp(argv[1], "set-mode")) {
ret = set_mode(argc, argv);
} else {
fprintf(stderr, "Unknown command.\n");
ret = 2;
}
close(f);
return ret;
}