1
0
mirror of https://github.com/m42uko/linux-fsia6b.git synced 2022-05-12 18:36:03 +02:00

Add proper support for switch layouts

Also, clean up the code somewhat.
This commit is contained in:
Markus Koch 2019-05-01 10:56:26 +02:00
parent 3dd72431e3
commit dcc9841d93

118
fsia6b.c
View File

@ -1,7 +1,8 @@
// SPDX-License-Identifier: GPL-2.0
/*
*
* FS-iA6B iBus RC receiver kernel driver
* Copyright (C) 2018 Markus Koch <markus@notsyncing.net>
* Copyright (C) 2018 - 2019 Markus Koch <markus@notsyncing.net>
*
* 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
@ -18,11 +19,23 @@
*
*/
/*
* This driver will provide all 14 channels of the FlySky FS-ia6B RC receiver
* as analog values.
*
* Additionally, the channels can be converted to discrete switch values.
* By default, it is configured for the offical FS-i6 remote control.
* If you use a different hardware configuration, you can configure it
* using the `switch_config` parameter.
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/input.h>
#include <linux/serio.h>
#include <linux/slab.h>
#include <linux/device.h>
#define SERIO_FSIA6B 100 /* TODO: This should be moved to the serio header */
@ -33,7 +46,11 @@ MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
#define IBUS_SERVO_COUNT 14
#define IBUS_SWITCH_OFFSET 6
static char *switch_config = "00000022320000";
module_param(switch_config, charp, 0444);
MODULE_PARM_DESC(switch_config,
"Amount of switch positions per channel (14 characters, 0-3)");
static int fsia6b_axes[IBUS_SERVO_COUNT] = {
ABS_X, ABS_Y,
@ -62,25 +79,20 @@ struct fsia6b {
char phys[32];
};
static void fsia6b_serio_write_wakeup(struct serio *serio)
{
struct fsia6b *fsia6b = serio_get_drvdata(serio);
printk(KERN_INFO "fsia6b: write wakeup.\n");
}
static irqreturn_t fsia6b_serio_irq(struct serio *serio,
unsigned char data, unsigned int flags)
{
struct fsia6b *fsia6b = serio_get_drvdata(serio);
int i;
int sw_state;
int sw_id = BTN_0;
fsia6b->packet.ibuf = (data << 8) | ((fsia6b->packet.ibuf >> 8) & 0xFF);
switch (fsia6b->packet.state) {
case SYNC:
if (fsia6b->packet.ibuf == 0x4020) {
if (fsia6b->packet.ibuf == 0x4020)
fsia6b->packet.state = COLLECT;
}
break;
case COLLECT:
@ -88,7 +100,8 @@ static irqreturn_t fsia6b_serio_irq(struct serio *serio,
break;
case PROCESS:
fsia6b->packet.channel[fsia6b->packet.offset] = fsia6b->packet.ibuf;
fsia6b->packet.channel[fsia6b->packet.offset] =
fsia6b->packet.ibuf;
fsia6b->packet.offset++;
if (fsia6b->packet.offset == IBUS_SERVO_COUNT) {
@ -97,9 +110,28 @@ static irqreturn_t fsia6b_serio_irq(struct serio *serio,
for (i = 0; i < IBUS_SERVO_COUNT; ++i) {
input_report_abs(fsia6b->dev, fsia6b_axes[i],
fsia6b->packet.channel[i]);
if (i >= IBUS_SWITCH_OFFSET) {
input_report_key(fsia6b->dev, BTN_1 + i*1,
(fsia6b->packet.channel[i] > 1900));
sw_state = 0;
if (fsia6b->packet.channel[i] > 1900)
sw_state = 1;
else if (fsia6b->packet.channel[i] < 1100)
sw_state = 2;
switch (switch_config[i]) {
case '3':
input_report_key(fsia6b->dev,
sw_id++,
sw_state == 0);
/* fall-through */
case '2':
input_report_key(fsia6b->dev,
sw_id++,
sw_state == 1);
/* fall-through */
case '1':
input_report_key(fsia6b->dev,
sw_id++,
sw_state == 2);
}
}
input_sync(fsia6b->dev);
@ -112,20 +144,13 @@ static irqreturn_t fsia6b_serio_irq(struct serio *serio,
return IRQ_HANDLED;
}
int fsia6b_init_device(struct fsia6b *fsia6b)
{
printk(KERN_INFO "fsia6b: init.\n");
return 0;
}
static int fsia6b_serio_connect(struct serio *serio, struct serio_driver *drv)
{
struct fsia6b *fsia6b;
struct input_dev *input_dev;
int err;
int i;
printk(KERN_INFO "fsia6b: connect.\n");
int i, j;
int sw_id = BTN_0;
fsia6b = kzalloc(sizeof(struct fsia6b), GFP_KERNEL);
if (!fsia6b)
@ -161,25 +186,35 @@ static int fsia6b_serio_connect(struct serio *serio, struct serio_driver *drv)
input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
for (i = 0; i < IBUS_SERVO_COUNT; ++i) {
input_set_abs_params(input_dev, fsia6b_axes[i], 1000, 2000, 2, 2);
if (i >= IBUS_SWITCH_OFFSET) {
input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_1 + i);
input_set_abs_params(input_dev, fsia6b_axes[i],
1000, 2000, 2, 2);
}
// Register switch configuration
for (i = 0; i < IBUS_SERVO_COUNT; ++i) {
if (((switch_config[i] == '\0') && (i != IBUS_SERVO_COUNT)) ||
(switch_config[i] < '0') ||
(switch_config[i] > '3')) {
dev_err(&fsia6b->dev->dev,
"Invalid switch configuration supplied for fsia6b.\n");
return -EINVAL;
}
for (j = '1'; j <= switch_config[i]; ++j) {
input_dev->keybit[BIT_WORD(BTN_0)] |=
BIT_MASK(sw_id++);
}
}
err = input_register_device(fsia6b->dev);
if (err)
goto fail2;
err = fsia6b_init_device(fsia6b);
if (err)
goto fail3;
return 0;
fail3: input_free_device(input_dev);
fail2: serio_close(serio);
fail1: serio_set_drvdata(serio, NULL);
fail2: serio_close(serio);
fail1: serio_set_drvdata(serio, NULL);
kfree(fsia6b);
return err;
}
@ -188,8 +223,6 @@ static void fsia6b_serio_disconnect(struct serio *serio)
{
struct fsia6b *fsia6b = serio_get_drvdata(serio);
printk(KERN_INFO "fsia6b: disconnect.\n");
serio_close(serio);
serio_set_drvdata(serio, NULL);
input_unregister_device(fsia6b->dev);
@ -197,13 +230,13 @@ static void fsia6b_serio_disconnect(struct serio *serio)
}
static const struct serio_device_id fsia6b_serio_ids[] = {
{
.type = SERIO_RS232,
.proto = SERIO_FSIA6B,
.id = SERIO_ANY,
.extra = SERIO_ANY,
},
{ 0 }
{
.type = SERIO_RS232,
.proto = SERIO_FSIA6B,
.id = SERIO_ANY,
.extra = SERIO_ANY,
},
{ 0 }
};
MODULE_DEVICE_TABLE(serio, fsia6b_serio_ids);
@ -214,7 +247,6 @@ struct serio_driver fsia6b_serio_drv = {
},
.description = DRIVER_DESC,
.id_table = fsia6b_serio_ids,
.write_wakeup = fsia6b_serio_write_wakeup,
.interrupt = fsia6b_serio_irq,
.connect = fsia6b_serio_connect,
.disconnect = fsia6b_serio_disconnect