1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * FS-iA6B iBus RC receiver driver 4 * 5 * This driver provides all 14 channels of the FlySky FS-ia6B RC receiver 6 * as analog values. 7 * 8 * Additionally, the channels can be converted to discrete switch values. 9 * By default, it is configured for the offical FS-i6 remote control. 10 * If you use a different hardware configuration, you can configure it 11 * using the `switch_config` parameter. 12 */ 13 14 #include <linux/device.h> 15 #include <linux/input.h> 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include <linux/serio.h> 19 #include <linux/slab.h> 20 #include <linux/types.h> 21 22 #define DRIVER_DESC "FS-iA6B iBus RC receiver" 23 24 MODULE_AUTHOR("Markus Koch <markus@notsyncing.net>"); 25 MODULE_DESCRIPTION(DRIVER_DESC); 26 MODULE_LICENSE("GPL"); 27 28 #define IBUS_SERVO_COUNT 14 29 30 static char *switch_config = "00000022320000"; 31 module_param(switch_config, charp, 0444); 32 MODULE_PARM_DESC(switch_config, 33 "Amount of switch positions per channel (14 characters, 0-3)"); 34 35 static int fsia6b_axes[IBUS_SERVO_COUNT] = { 36 ABS_X, ABS_Y, 37 ABS_Z, ABS_RX, 38 ABS_RY, ABS_RZ, 39 ABS_HAT0X, ABS_HAT0Y, 40 ABS_HAT1X, ABS_HAT1Y, 41 ABS_HAT2X, ABS_HAT2Y, 42 ABS_HAT3X, ABS_HAT3Y 43 }; 44 45 enum ibus_state { SYNC, COLLECT, PROCESS }; 46 47 struct ibus_packet { 48 enum ibus_state state; 49 50 int offset; 51 u16 ibuf; 52 u16 channel[IBUS_SERVO_COUNT]; 53 }; 54 55 struct fsia6b { 56 struct input_dev *dev; 57 struct ibus_packet packet; 58 59 char phys[32]; 60 }; 61 62 static irqreturn_t fsia6b_serio_irq(struct serio *serio, 63 unsigned char data, unsigned int flags) 64 { 65 struct fsia6b *fsia6b = serio_get_drvdata(serio); 66 int i; 67 int sw_state; 68 int sw_id = BTN_0; 69 70 fsia6b->packet.ibuf = (data << 8) | ((fsia6b->packet.ibuf >> 8) & 0xFF); 71 72 switch (fsia6b->packet.state) { 73 case SYNC: 74 if (fsia6b->packet.ibuf == 0x4020) 75 fsia6b->packet.state = COLLECT; 76 break; 77 78 case COLLECT: 79 fsia6b->packet.state = PROCESS; 80 break; 81 82 case PROCESS: 83 fsia6b->packet.channel[fsia6b->packet.offset] = 84 fsia6b->packet.ibuf; 85 fsia6b->packet.offset++; 86 87 if (fsia6b->packet.offset == IBUS_SERVO_COUNT) { 88 fsia6b->packet.offset = 0; 89 fsia6b->packet.state = SYNC; 90 for (i = 0; i < IBUS_SERVO_COUNT; ++i) { 91 input_report_abs(fsia6b->dev, fsia6b_axes[i], 92 fsia6b->packet.channel[i]); 93 94 sw_state = 0; 95 if (fsia6b->packet.channel[i] > 1900) 96 sw_state = 1; 97 else if (fsia6b->packet.channel[i] < 1100) 98 sw_state = 2; 99 100 switch (switch_config[i]) { 101 case '3': 102 input_report_key(fsia6b->dev, 103 sw_id++, 104 sw_state == 0); 105 fallthrough; 106 case '2': 107 input_report_key(fsia6b->dev, 108 sw_id++, 109 sw_state == 1); 110 fallthrough; 111 case '1': 112 input_report_key(fsia6b->dev, 113 sw_id++, 114 sw_state == 2); 115 } 116 } 117 input_sync(fsia6b->dev); 118 } else { 119 fsia6b->packet.state = COLLECT; 120 } 121 break; 122 } 123 124 return IRQ_HANDLED; 125 } 126 127 static int fsia6b_serio_connect(struct serio *serio, struct serio_driver *drv) 128 { 129 struct fsia6b *fsia6b; 130 struct input_dev *input_dev; 131 int err; 132 int i, j; 133 int sw_id = 0; 134 135 fsia6b = kzalloc(sizeof(*fsia6b), GFP_KERNEL); 136 if (!fsia6b) 137 return -ENOMEM; 138 139 fsia6b->packet.ibuf = 0; 140 fsia6b->packet.offset = 0; 141 fsia6b->packet.state = SYNC; 142 143 serio_set_drvdata(serio, fsia6b); 144 145 input_dev = input_allocate_device(); 146 if (!input_dev) { 147 err = -ENOMEM; 148 goto fail1; 149 } 150 fsia6b->dev = input_dev; 151 152 snprintf(fsia6b->phys, sizeof(fsia6b->phys), "%s/input0", serio->phys); 153 154 input_dev->name = DRIVER_DESC; 155 input_dev->phys = fsia6b->phys; 156 input_dev->id.bustype = BUS_RS232; 157 input_dev->id.vendor = SERIO_FSIA6B; 158 input_dev->id.product = serio->id.id; 159 input_dev->id.version = 0x0100; 160 input_dev->dev.parent = &serio->dev; 161 162 for (i = 0; i < IBUS_SERVO_COUNT; i++) 163 input_set_abs_params(input_dev, fsia6b_axes[i], 164 1000, 2000, 2, 2); 165 166 /* Register switch configuration */ 167 for (i = 0; i < IBUS_SERVO_COUNT; i++) { 168 if (switch_config[i] < '0' || switch_config[i] > '3') { 169 dev_err(&fsia6b->dev->dev, 170 "Invalid switch configuration supplied for fsia6b.\n"); 171 err = -EINVAL; 172 goto fail2; 173 } 174 175 for (j = '1'; j <= switch_config[i]; j++) { 176 input_set_capability(input_dev, EV_KEY, BTN_0 + sw_id); 177 sw_id++; 178 } 179 } 180 181 err = serio_open(serio, drv); 182 if (err) 183 goto fail2; 184 185 err = input_register_device(fsia6b->dev); 186 if (err) 187 goto fail3; 188 189 return 0; 190 191 fail3: serio_close(serio); 192 fail2: input_free_device(input_dev); 193 fail1: serio_set_drvdata(serio, NULL); 194 kfree(fsia6b); 195 return err; 196 } 197 198 static void fsia6b_serio_disconnect(struct serio *serio) 199 { 200 struct fsia6b *fsia6b = serio_get_drvdata(serio); 201 202 serio_close(serio); 203 serio_set_drvdata(serio, NULL); 204 input_unregister_device(fsia6b->dev); 205 kfree(fsia6b); 206 } 207 208 static const struct serio_device_id fsia6b_serio_ids[] = { 209 { 210 .type = SERIO_RS232, 211 .proto = SERIO_FSIA6B, 212 .id = SERIO_ANY, 213 .extra = SERIO_ANY, 214 }, 215 { 0 } 216 }; 217 218 MODULE_DEVICE_TABLE(serio, fsia6b_serio_ids); 219 220 static struct serio_driver fsia6b_serio_drv = { 221 .driver = { 222 .name = "fsia6b" 223 }, 224 .description = DRIVER_DESC, 225 .id_table = fsia6b_serio_ids, 226 .interrupt = fsia6b_serio_irq, 227 .connect = fsia6b_serio_connect, 228 .disconnect = fsia6b_serio_disconnect 229 }; 230 231 module_serio_driver(fsia6b_serio_drv) 232