Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Sahara TouchIT-213 serial touchscreen driver
0004  *
0005  * Copyright (c) 2007-2008 Claudio Nieder <private@claudio.ch>
0006  *
0007  * Based on Touchright driver (drivers/input/touchscreen/touchright.c)
0008  * Copyright (c) 2006 Rick Koch <n1gp@hotmail.com>
0009  * Copyright (c) 2004 Vojtech Pavlik
0010  * and Dan Streetman <ddstreet@ieee.org>
0011  */
0012 
0013 
0014 #include <linux/errno.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/slab.h>
0018 #include <linux/input.h>
0019 #include <linux/serio.h>
0020 
0021 #define DRIVER_DESC "Sahara TouchIT-213 serial touchscreen driver"
0022 
0023 MODULE_AUTHOR("Claudio Nieder <private@claudio.ch>");
0024 MODULE_DESCRIPTION(DRIVER_DESC);
0025 MODULE_LICENSE("GPL");
0026 
0027 /*
0028  * Definitions & global arrays.
0029  */
0030 
0031 /*
0032  * Data is received through COM1 at 9600bit/s,8bit,no parity in packets
0033  * of 5 byte each.
0034  *
0035  *   +--------+   +--------+   +--------+   +--------+   +--------+
0036  *   |1000000p|   |0xxxxxxx|   |0xxxxxxx|   |0yyyyyyy|   |0yyyyyyy|
0037  *   +--------+   +--------+   +--------+   +--------+   +--------+
0038  *                    MSB          LSB          MSB          LSB
0039  *
0040  * The value of p is 1 as long as the screen is touched and 0 when
0041  * reporting the location where touching stopped, e.g. where the pen was
0042  * lifted from the screen.
0043  *
0044  * When holding the screen in landscape mode as the BIOS text output is
0045  * presented, x is the horizontal axis with values growing from left to
0046  * right and y is the vertical axis with values growing from top to
0047  * bottom.
0048  *
0049  * When holding the screen in portrait mode with the Sahara logo in its
0050  * correct position, x ist the vertical axis with values growing from
0051  * top to bottom and y is the horizontal axis with values growing from
0052  * right to left.
0053  */
0054 
0055 #define T213_FORMAT_TOUCH_BIT   0x01
0056 #define T213_FORMAT_STATUS_BYTE 0x80
0057 #define T213_FORMAT_STATUS_MASK ~T213_FORMAT_TOUCH_BIT
0058 
0059 /*
0060  * On my Sahara Touch-IT 213 I have observed x values from 0 to 0x7f0
0061  * and y values from 0x1d to 0x7e9, so the actual measurement is
0062  * probably done with an 11 bit precision.
0063  */
0064 #define T213_MIN_XC 0
0065 #define T213_MAX_XC 0x07ff
0066 #define T213_MIN_YC 0
0067 #define T213_MAX_YC 0x07ff
0068 
0069 /*
0070  * Per-touchscreen data.
0071  */
0072 
0073 struct touchit213 {
0074     struct input_dev *dev;
0075     struct serio *serio;
0076     int idx;
0077     unsigned char csum;
0078     unsigned char data[5];
0079     char phys[32];
0080 };
0081 
0082 static irqreturn_t touchit213_interrupt(struct serio *serio,
0083         unsigned char data, unsigned int flags)
0084 {
0085     struct touchit213 *touchit213 = serio_get_drvdata(serio);
0086     struct input_dev *dev = touchit213->dev;
0087 
0088     touchit213->data[touchit213->idx] = data;
0089 
0090     switch (touchit213->idx++) {
0091     case 0:
0092         if ((touchit213->data[0] & T213_FORMAT_STATUS_MASK) !=
0093                 T213_FORMAT_STATUS_BYTE) {
0094             pr_debug("unsynchronized data: 0x%02x\n", data);
0095             touchit213->idx = 0;
0096         }
0097         break;
0098 
0099     case 4:
0100         touchit213->idx = 0;
0101         input_report_abs(dev, ABS_X,
0102             (touchit213->data[1] << 7) | touchit213->data[2]);
0103         input_report_abs(dev, ABS_Y,
0104             (touchit213->data[3] << 7) | touchit213->data[4]);
0105         input_report_key(dev, BTN_TOUCH,
0106             touchit213->data[0] & T213_FORMAT_TOUCH_BIT);
0107         input_sync(dev);
0108         break;
0109     }
0110 
0111     return IRQ_HANDLED;
0112 }
0113 
0114 /*
0115  * touchit213_disconnect() is the opposite of touchit213_connect()
0116  */
0117 
0118 static void touchit213_disconnect(struct serio *serio)
0119 {
0120     struct touchit213 *touchit213 = serio_get_drvdata(serio);
0121 
0122     input_get_device(touchit213->dev);
0123     input_unregister_device(touchit213->dev);
0124     serio_close(serio);
0125     serio_set_drvdata(serio, NULL);
0126     input_put_device(touchit213->dev);
0127     kfree(touchit213);
0128 }
0129 
0130 /*
0131  * touchit213_connect() is the routine that is called when someone adds a
0132  * new serio device that supports the Touchright protocol and registers it as
0133  * an input device.
0134  */
0135 
0136 static int touchit213_connect(struct serio *serio, struct serio_driver *drv)
0137 {
0138     struct touchit213 *touchit213;
0139     struct input_dev *input_dev;
0140     int err;
0141 
0142     touchit213 = kzalloc(sizeof(struct touchit213), GFP_KERNEL);
0143     input_dev = input_allocate_device();
0144     if (!touchit213 || !input_dev) {
0145         err = -ENOMEM;
0146         goto fail1;
0147     }
0148 
0149     touchit213->serio = serio;
0150     touchit213->dev = input_dev;
0151     snprintf(touchit213->phys, sizeof(touchit213->phys),
0152          "%s/input0", serio->phys);
0153 
0154     input_dev->name = "Sahara Touch-iT213 Serial TouchScreen";
0155     input_dev->phys = touchit213->phys;
0156     input_dev->id.bustype = BUS_RS232;
0157     input_dev->id.vendor = SERIO_TOUCHIT213;
0158     input_dev->id.product = 0;
0159     input_dev->id.version = 0x0100;
0160     input_dev->dev.parent = &serio->dev;
0161     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0162     input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
0163     input_set_abs_params(touchit213->dev, ABS_X,
0164                  T213_MIN_XC, T213_MAX_XC, 0, 0);
0165     input_set_abs_params(touchit213->dev, ABS_Y,
0166                  T213_MIN_YC, T213_MAX_YC, 0, 0);
0167 
0168     serio_set_drvdata(serio, touchit213);
0169 
0170     err = serio_open(serio, drv);
0171     if (err)
0172         goto fail2;
0173 
0174     err = input_register_device(touchit213->dev);
0175     if (err)
0176         goto fail3;
0177 
0178     return 0;
0179 
0180  fail3: serio_close(serio);
0181  fail2: serio_set_drvdata(serio, NULL);
0182  fail1: input_free_device(input_dev);
0183     kfree(touchit213);
0184     return err;
0185 }
0186 
0187 /*
0188  * The serio driver structure.
0189  */
0190 
0191 static const struct serio_device_id touchit213_serio_ids[] = {
0192     {
0193         .type   = SERIO_RS232,
0194         .proto  = SERIO_TOUCHIT213,
0195         .id = SERIO_ANY,
0196         .extra  = SERIO_ANY,
0197     },
0198     { 0 }
0199 };
0200 
0201 MODULE_DEVICE_TABLE(serio, touchit213_serio_ids);
0202 
0203 static struct serio_driver touchit213_drv = {
0204     .driver     = {
0205         .name   = "touchit213",
0206     },
0207     .description    = DRIVER_DESC,
0208     .id_table   = touchit213_serio_ids,
0209     .interrupt  = touchit213_interrupt,
0210     .connect    = touchit213_connect,
0211     .disconnect = touchit213_disconnect,
0212 };
0213 
0214 module_serio_driver(touchit213_drv);