0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 #include <linux/kernel.h>
0039 #include <linux/module.h>
0040 #include <linux/slab.h>
0041 #include <linux/input.h>
0042 #include <linux/serio.h>
0043
0044 #define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver"
0045
0046 MODULE_DESCRIPTION(DRIVER_DESC);
0047 MODULE_LICENSE("GPL");
0048
0049
0050
0051
0052
0053 #define TWIDJOY_MAX_LENGTH 5
0054
0055 static struct twidjoy_button_spec {
0056 int bitshift;
0057 int bitmask;
0058 int buttons[3];
0059 }
0060 twidjoy_buttons[] = {
0061 { 0, 3, { BTN_A, BTN_B, BTN_C } },
0062 { 2, 3, { BTN_X, BTN_Y, BTN_Z } },
0063 { 4, 3, { BTN_TL, BTN_TR, BTN_TR2 } },
0064 { 6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
0065 { 8, 1, { BTN_BASE5 } },
0066 { 9, 1, { BTN_BASE } },
0067 { 10, 1, { BTN_BASE3 } },
0068 { 11, 1, { BTN_BASE4 } },
0069 { 12, 1, { BTN_BASE2 } },
0070 { 13, 1, { BTN_BASE6 } },
0071 { 0, 0, { 0 } }
0072 };
0073
0074
0075
0076
0077
0078 struct twidjoy {
0079 struct input_dev *dev;
0080 int idx;
0081 unsigned char data[TWIDJOY_MAX_LENGTH];
0082 char phys[32];
0083 };
0084
0085
0086
0087
0088
0089
0090 static void twidjoy_process_packet(struct twidjoy *twidjoy)
0091 {
0092 struct input_dev *dev = twidjoy->dev;
0093 unsigned char *data = twidjoy->data;
0094 struct twidjoy_button_spec *bp;
0095 int button_bits, abs_x, abs_y;
0096
0097 button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
0098
0099 for (bp = twidjoy_buttons; bp->bitmask; bp++) {
0100 int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
0101 int i;
0102
0103 for (i = 0; i < bp->bitmask; i++)
0104 input_report_key(dev, bp->buttons[i], i+1 == value);
0105 }
0106
0107 abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
0108 if (data[4] & 0x08) abs_x -= 256;
0109
0110 abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
0111 if (data[3] & 0x02) abs_y -= 256;
0112
0113 input_report_abs(dev, ABS_X, -abs_x);
0114 input_report_abs(dev, ABS_Y, +abs_y);
0115
0116 input_sync(dev);
0117 }
0118
0119
0120
0121
0122
0123
0124
0125 static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
0126 {
0127 struct twidjoy *twidjoy = serio_get_drvdata(serio);
0128
0129
0130
0131
0132
0133 if ((data & 0x80) == 0)
0134 twidjoy->idx = 0;
0135 else if (twidjoy->idx == 0)
0136 return IRQ_HANDLED;
0137
0138 if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
0139 twidjoy->data[twidjoy->idx++] = data;
0140
0141 if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
0142 twidjoy_process_packet(twidjoy);
0143 twidjoy->idx = 0;
0144 }
0145
0146 return IRQ_HANDLED;
0147 }
0148
0149
0150
0151
0152
0153 static void twidjoy_disconnect(struct serio *serio)
0154 {
0155 struct twidjoy *twidjoy = serio_get_drvdata(serio);
0156
0157 serio_close(serio);
0158 serio_set_drvdata(serio, NULL);
0159 input_unregister_device(twidjoy->dev);
0160 kfree(twidjoy);
0161 }
0162
0163
0164
0165
0166
0167
0168
0169 static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
0170 {
0171 struct twidjoy_button_spec *bp;
0172 struct twidjoy *twidjoy;
0173 struct input_dev *input_dev;
0174 int err = -ENOMEM;
0175 int i;
0176
0177 twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);
0178 input_dev = input_allocate_device();
0179 if (!twidjoy || !input_dev)
0180 goto fail1;
0181
0182 twidjoy->dev = input_dev;
0183 snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
0184
0185 input_dev->name = "Handykey Twiddler";
0186 input_dev->phys = twidjoy->phys;
0187 input_dev->id.bustype = BUS_RS232;
0188 input_dev->id.vendor = SERIO_TWIDJOY;
0189 input_dev->id.product = 0x0001;
0190 input_dev->id.version = 0x0100;
0191 input_dev->dev.parent = &serio->dev;
0192
0193 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0194 input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);
0195 input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);
0196
0197 for (bp = twidjoy_buttons; bp->bitmask; bp++)
0198 for (i = 0; i < bp->bitmask; i++)
0199 set_bit(bp->buttons[i], input_dev->keybit);
0200
0201 serio_set_drvdata(serio, twidjoy);
0202
0203 err = serio_open(serio, drv);
0204 if (err)
0205 goto fail2;
0206
0207 err = input_register_device(twidjoy->dev);
0208 if (err)
0209 goto fail3;
0210
0211 return 0;
0212
0213 fail3: serio_close(serio);
0214 fail2: serio_set_drvdata(serio, NULL);
0215 fail1: input_free_device(input_dev);
0216 kfree(twidjoy);
0217 return err;
0218 }
0219
0220
0221
0222
0223
0224 static const struct serio_device_id twidjoy_serio_ids[] = {
0225 {
0226 .type = SERIO_RS232,
0227 .proto = SERIO_TWIDJOY,
0228 .id = SERIO_ANY,
0229 .extra = SERIO_ANY,
0230 },
0231 { 0 }
0232 };
0233
0234 MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
0235
0236 static struct serio_driver twidjoy_drv = {
0237 .driver = {
0238 .name = "twidjoy",
0239 },
0240 .description = DRIVER_DESC,
0241 .id_table = twidjoy_serio_ids,
0242 .interrupt = twidjoy_interrupt,
0243 .connect = twidjoy_connect,
0244 .disconnect = twidjoy_disconnect,
0245 };
0246
0247 module_serio_driver(twidjoy_drv);