Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Elo serial touchscreen driver
0004  *
0005  * Copyright (c) 2004 Vojtech Pavlik
0006  */
0007 
0008 
0009 /*
0010  * This driver can handle serial Elo touchscreens using either the Elo standard
0011  * 'E271-2210' 10-byte protocol, Elo legacy 'E281A-4002' 6-byte protocol, Elo
0012  * legacy 'E271-140' 4-byte protocol and Elo legacy 'E261-280' 3-byte protocol.
0013  */
0014 
0015 #include <linux/errno.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/slab.h>
0019 #include <linux/input.h>
0020 #include <linux/serio.h>
0021 #include <linux/ctype.h>
0022 
0023 #define DRIVER_DESC "Elo serial touchscreen driver"
0024 
0025 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
0026 MODULE_DESCRIPTION(DRIVER_DESC);
0027 MODULE_LICENSE("GPL");
0028 
0029 /*
0030  * Definitions & global arrays.
0031  */
0032 
0033 #define ELO_MAX_LENGTH      10
0034 
0035 #define ELO10_PACKET_LEN    8
0036 #define ELO10_TOUCH     0x03
0037 #define ELO10_PRESSURE      0x80
0038 
0039 #define ELO10_LEAD_BYTE     'U'
0040 
0041 #define ELO10_ID_CMD        'i'
0042 
0043 #define ELO10_TOUCH_PACKET  'T'
0044 #define ELO10_ACK_PACKET    'A'
0045 #define ELI10_ID_PACKET     'I'
0046 
0047 /*
0048  * Per-touchscreen data.
0049  */
0050 
0051 struct elo {
0052     struct input_dev *dev;
0053     struct serio *serio;
0054     struct mutex cmd_mutex;
0055     struct completion cmd_done;
0056     int id;
0057     int idx;
0058     unsigned char expected_packet;
0059     unsigned char csum;
0060     unsigned char data[ELO_MAX_LENGTH];
0061     unsigned char response[ELO10_PACKET_LEN];
0062     char phys[32];
0063 };
0064 
0065 static void elo_process_data_10(struct elo *elo, unsigned char data)
0066 {
0067     struct input_dev *dev = elo->dev;
0068 
0069     elo->data[elo->idx] = data;
0070 
0071     switch (elo->idx++) {
0072     case 0:
0073         elo->csum = 0xaa;
0074         if (data != ELO10_LEAD_BYTE) {
0075             dev_dbg(&elo->serio->dev,
0076                 "unsynchronized data: 0x%02x\n", data);
0077             elo->idx = 0;
0078         }
0079         break;
0080 
0081     case 9:
0082         elo->idx = 0;
0083         if (data != elo->csum) {
0084             dev_dbg(&elo->serio->dev,
0085                 "bad checksum: 0x%02x, expected 0x%02x\n",
0086                  data, elo->csum);
0087             break;
0088         }
0089         if (elo->data[1] != elo->expected_packet) {
0090             if (elo->data[1] != ELO10_TOUCH_PACKET)
0091                 dev_dbg(&elo->serio->dev,
0092                     "unexpected packet: 0x%02x\n",
0093                      elo->data[1]);
0094             break;
0095         }
0096         if (likely(elo->data[1] == ELO10_TOUCH_PACKET)) {
0097             input_report_abs(dev, ABS_X, (elo->data[4] << 8) | elo->data[3]);
0098             input_report_abs(dev, ABS_Y, (elo->data[6] << 8) | elo->data[5]);
0099             if (elo->data[2] & ELO10_PRESSURE)
0100                 input_report_abs(dev, ABS_PRESSURE,
0101                         (elo->data[8] << 8) | elo->data[7]);
0102             input_report_key(dev, BTN_TOUCH, elo->data[2] & ELO10_TOUCH);
0103             input_sync(dev);
0104         } else if (elo->data[1] == ELO10_ACK_PACKET) {
0105             if (elo->data[2] == '0')
0106                 elo->expected_packet = ELO10_TOUCH_PACKET;
0107             complete(&elo->cmd_done);
0108         } else {
0109             memcpy(elo->response, &elo->data[1], ELO10_PACKET_LEN);
0110             elo->expected_packet = ELO10_ACK_PACKET;
0111         }
0112         break;
0113     }
0114     elo->csum += data;
0115 }
0116 
0117 static void elo_process_data_6(struct elo *elo, unsigned char data)
0118 {
0119     struct input_dev *dev = elo->dev;
0120 
0121     elo->data[elo->idx] = data;
0122 
0123     switch (elo->idx++) {
0124 
0125     case 0:
0126         if ((data & 0xc0) != 0xc0)
0127             elo->idx = 0;
0128         break;
0129 
0130     case 1:
0131         if ((data & 0xc0) != 0x80)
0132             elo->idx = 0;
0133         break;
0134 
0135     case 2:
0136         if ((data & 0xc0) != 0x40)
0137             elo->idx = 0;
0138         break;
0139 
0140     case 3:
0141         if (data & 0xc0) {
0142             elo->idx = 0;
0143             break;
0144         }
0145 
0146         input_report_abs(dev, ABS_X, ((elo->data[0] & 0x3f) << 6) | (elo->data[1] & 0x3f));
0147         input_report_abs(dev, ABS_Y, ((elo->data[2] & 0x3f) << 6) | (elo->data[3] & 0x3f));
0148 
0149         if (elo->id == 2) {
0150             input_report_key(dev, BTN_TOUCH, 1);
0151             input_sync(dev);
0152             elo->idx = 0;
0153         }
0154 
0155         break;
0156 
0157     case 4:
0158         if (data) {
0159             input_sync(dev);
0160             elo->idx = 0;
0161         }
0162         break;
0163 
0164     case 5:
0165         if ((data & 0xf0) == 0) {
0166             input_report_abs(dev, ABS_PRESSURE, elo->data[5]);
0167             input_report_key(dev, BTN_TOUCH, !!elo->data[5]);
0168         }
0169         input_sync(dev);
0170         elo->idx = 0;
0171         break;
0172     }
0173 }
0174 
0175 static void elo_process_data_3(struct elo *elo, unsigned char data)
0176 {
0177     struct input_dev *dev = elo->dev;
0178 
0179     elo->data[elo->idx] = data;
0180 
0181     switch (elo->idx++) {
0182 
0183     case 0:
0184         if ((data & 0x7f) != 0x01)
0185             elo->idx = 0;
0186         break;
0187     case 2:
0188         input_report_key(dev, BTN_TOUCH, !(elo->data[1] & 0x80));
0189         input_report_abs(dev, ABS_X, elo->data[1]);
0190         input_report_abs(dev, ABS_Y, elo->data[2]);
0191         input_sync(dev);
0192         elo->idx = 0;
0193         break;
0194     }
0195 }
0196 
0197 static irqreturn_t elo_interrupt(struct serio *serio,
0198         unsigned char data, unsigned int flags)
0199 {
0200     struct elo *elo = serio_get_drvdata(serio);
0201 
0202     switch (elo->id) {
0203     case 0:
0204         elo_process_data_10(elo, data);
0205         break;
0206 
0207     case 1:
0208     case 2:
0209         elo_process_data_6(elo, data);
0210         break;
0211 
0212     case 3:
0213         elo_process_data_3(elo, data);
0214         break;
0215     }
0216 
0217     return IRQ_HANDLED;
0218 }
0219 
0220 static int elo_command_10(struct elo *elo, unsigned char *packet)
0221 {
0222     int rc = -1;
0223     int i;
0224     unsigned char csum = 0xaa + ELO10_LEAD_BYTE;
0225 
0226     mutex_lock(&elo->cmd_mutex);
0227 
0228     serio_pause_rx(elo->serio);
0229     elo->expected_packet = toupper(packet[0]);
0230     init_completion(&elo->cmd_done);
0231     serio_continue_rx(elo->serio);
0232 
0233     if (serio_write(elo->serio, ELO10_LEAD_BYTE))
0234         goto out;
0235 
0236     for (i = 0; i < ELO10_PACKET_LEN; i++) {
0237         csum += packet[i];
0238         if (serio_write(elo->serio, packet[i]))
0239             goto out;
0240     }
0241 
0242     if (serio_write(elo->serio, csum))
0243         goto out;
0244 
0245     wait_for_completion_timeout(&elo->cmd_done, HZ);
0246 
0247     if (elo->expected_packet == ELO10_TOUCH_PACKET) {
0248         /* We are back in reporting mode, the command was ACKed */
0249         memcpy(packet, elo->response, ELO10_PACKET_LEN);
0250         rc = 0;
0251     }
0252 
0253  out:
0254     mutex_unlock(&elo->cmd_mutex);
0255     return rc;
0256 }
0257 
0258 static int elo_setup_10(struct elo *elo)
0259 {
0260     static const char *elo_types[] = { "Accu", "Dura", "Intelli", "Carroll" };
0261     struct input_dev *dev = elo->dev;
0262     unsigned char packet[ELO10_PACKET_LEN] = { ELO10_ID_CMD };
0263 
0264     if (elo_command_10(elo, packet))
0265         return -1;
0266 
0267     dev->id.version = (packet[5] << 8) | packet[4];
0268 
0269     input_set_abs_params(dev, ABS_X, 96, 4000, 0, 0);
0270     input_set_abs_params(dev, ABS_Y, 96, 4000, 0, 0);
0271     if (packet[3] & ELO10_PRESSURE)
0272         input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
0273 
0274     dev_info(&elo->serio->dev,
0275          "%sTouch touchscreen, fw: %02x.%02x, features: 0x%02x, controller: 0x%02x\n",
0276          elo_types[(packet[1] -'0') & 0x03],
0277          packet[5], packet[4], packet[3], packet[7]);
0278 
0279     return 0;
0280 }
0281 
0282 /*
0283  * elo_disconnect() is the opposite of elo_connect()
0284  */
0285 
0286 static void elo_disconnect(struct serio *serio)
0287 {
0288     struct elo *elo = serio_get_drvdata(serio);
0289 
0290     input_get_device(elo->dev);
0291     input_unregister_device(elo->dev);
0292     serio_close(serio);
0293     serio_set_drvdata(serio, NULL);
0294     input_put_device(elo->dev);
0295     kfree(elo);
0296 }
0297 
0298 /*
0299  * elo_connect() is the routine that is called when someone adds a
0300  * new serio device that supports Gunze protocol and registers it as
0301  * an input device.
0302  */
0303 
0304 static int elo_connect(struct serio *serio, struct serio_driver *drv)
0305 {
0306     struct elo *elo;
0307     struct input_dev *input_dev;
0308     int err;
0309 
0310     elo = kzalloc(sizeof(struct elo), GFP_KERNEL);
0311     input_dev = input_allocate_device();
0312     if (!elo || !input_dev) {
0313         err = -ENOMEM;
0314         goto fail1;
0315     }
0316 
0317     elo->serio = serio;
0318     elo->id = serio->id.id;
0319     elo->dev = input_dev;
0320     elo->expected_packet = ELO10_TOUCH_PACKET;
0321     mutex_init(&elo->cmd_mutex);
0322     init_completion(&elo->cmd_done);
0323     snprintf(elo->phys, sizeof(elo->phys), "%s/input0", serio->phys);
0324 
0325     input_dev->name = "Elo Serial TouchScreen";
0326     input_dev->phys = elo->phys;
0327     input_dev->id.bustype = BUS_RS232;
0328     input_dev->id.vendor = SERIO_ELO;
0329     input_dev->id.product = elo->id;
0330     input_dev->id.version = 0x0100;
0331     input_dev->dev.parent = &serio->dev;
0332 
0333     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0334     input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
0335 
0336     serio_set_drvdata(serio, elo);
0337     err = serio_open(serio, drv);
0338     if (err)
0339         goto fail2;
0340 
0341     switch (elo->id) {
0342 
0343     case 0: /* 10-byte protocol */
0344         if (elo_setup_10(elo)) {
0345             err = -EIO;
0346             goto fail3;
0347         }
0348 
0349         break;
0350 
0351     case 1: /* 6-byte protocol */
0352         input_set_abs_params(input_dev, ABS_PRESSURE, 0, 15, 0, 0);
0353         fallthrough;
0354 
0355     case 2: /* 4-byte protocol */
0356         input_set_abs_params(input_dev, ABS_X, 96, 4000, 0, 0);
0357         input_set_abs_params(input_dev, ABS_Y, 96, 4000, 0, 0);
0358         break;
0359 
0360     case 3: /* 3-byte protocol */
0361         input_set_abs_params(input_dev, ABS_X, 0, 255, 0, 0);
0362         input_set_abs_params(input_dev, ABS_Y, 0, 255, 0, 0);
0363         break;
0364     }
0365 
0366     err = input_register_device(elo->dev);
0367     if (err)
0368         goto fail3;
0369 
0370     return 0;
0371 
0372  fail3: serio_close(serio);
0373  fail2: serio_set_drvdata(serio, NULL);
0374  fail1: input_free_device(input_dev);
0375     kfree(elo);
0376     return err;
0377 }
0378 
0379 /*
0380  * The serio driver structure.
0381  */
0382 
0383 static const struct serio_device_id elo_serio_ids[] = {
0384     {
0385         .type   = SERIO_RS232,
0386         .proto  = SERIO_ELO,
0387         .id = SERIO_ANY,
0388         .extra  = SERIO_ANY,
0389     },
0390     { 0 }
0391 };
0392 
0393 MODULE_DEVICE_TABLE(serio, elo_serio_ids);
0394 
0395 static struct serio_driver elo_drv = {
0396     .driver     = {
0397         .name   = "elo",
0398     },
0399     .description    = DRIVER_DESC,
0400     .id_table   = elo_serio_ids,
0401     .interrupt  = elo_interrupt,
0402     .connect    = elo_connect,
0403     .disconnect = elo_disconnect,
0404 };
0405 
0406 module_serio_driver(elo_drv);