0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
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 "MicroTouch serial touchscreen driver"
0022
0023 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
0024 MODULE_DESCRIPTION(DRIVER_DESC);
0025 MODULE_LICENSE("GPL");
0026
0027
0028
0029
0030
0031 #define MTOUCH_FORMAT_TABLET_STATUS_BIT 0x80
0032 #define MTOUCH_FORMAT_TABLET_TOUCH_BIT 0x40
0033 #define MTOUCH_FORMAT_TABLET_LENGTH 5
0034 #define MTOUCH_RESPONSE_BEGIN_BYTE 0x01
0035 #define MTOUCH_RESPONSE_END_BYTE 0x0d
0036
0037
0038 #define MTOUCH_MAX_LENGTH 16
0039
0040 #define MTOUCH_MIN_XC 0
0041 #define MTOUCH_MAX_XC 0x3fff
0042 #define MTOUCH_MIN_YC 0
0043 #define MTOUCH_MAX_YC 0x3fff
0044
0045 #define MTOUCH_GET_XC(data) (((data[2])<<7) | data[1])
0046 #define MTOUCH_GET_YC(data) (((data[4])<<7) | data[3])
0047 #define MTOUCH_GET_TOUCHED(data) (MTOUCH_FORMAT_TABLET_TOUCH_BIT & data[0])
0048
0049
0050
0051
0052
0053 struct mtouch {
0054 struct input_dev *dev;
0055 struct serio *serio;
0056 int idx;
0057 unsigned char data[MTOUCH_MAX_LENGTH];
0058 char phys[32];
0059 };
0060
0061 static void mtouch_process_format_tablet(struct mtouch *mtouch)
0062 {
0063 struct input_dev *dev = mtouch->dev;
0064
0065 if (MTOUCH_FORMAT_TABLET_LENGTH == ++mtouch->idx) {
0066 input_report_abs(dev, ABS_X, MTOUCH_GET_XC(mtouch->data));
0067 input_report_abs(dev, ABS_Y, MTOUCH_MAX_YC - MTOUCH_GET_YC(mtouch->data));
0068 input_report_key(dev, BTN_TOUCH, MTOUCH_GET_TOUCHED(mtouch->data));
0069 input_sync(dev);
0070
0071 mtouch->idx = 0;
0072 }
0073 }
0074
0075 static void mtouch_process_response(struct mtouch *mtouch)
0076 {
0077 if (MTOUCH_RESPONSE_END_BYTE == mtouch->data[mtouch->idx++]) {
0078
0079 mtouch->idx = 0;
0080 } else if (MTOUCH_MAX_LENGTH == mtouch->idx) {
0081 printk(KERN_ERR "mtouch.c: too many response bytes\n");
0082 mtouch->idx = 0;
0083 }
0084 }
0085
0086 static irqreturn_t mtouch_interrupt(struct serio *serio,
0087 unsigned char data, unsigned int flags)
0088 {
0089 struct mtouch *mtouch = serio_get_drvdata(serio);
0090
0091 mtouch->data[mtouch->idx] = data;
0092
0093 if (MTOUCH_FORMAT_TABLET_STATUS_BIT & mtouch->data[0])
0094 mtouch_process_format_tablet(mtouch);
0095 else if (MTOUCH_RESPONSE_BEGIN_BYTE == mtouch->data[0])
0096 mtouch_process_response(mtouch);
0097 else
0098 printk(KERN_DEBUG "mtouch.c: unknown/unsynchronized data from device, byte %x\n",mtouch->data[0]);
0099
0100 return IRQ_HANDLED;
0101 }
0102
0103
0104
0105
0106
0107 static void mtouch_disconnect(struct serio *serio)
0108 {
0109 struct mtouch *mtouch = serio_get_drvdata(serio);
0110
0111 input_get_device(mtouch->dev);
0112 input_unregister_device(mtouch->dev);
0113 serio_close(serio);
0114 serio_set_drvdata(serio, NULL);
0115 input_put_device(mtouch->dev);
0116 kfree(mtouch);
0117 }
0118
0119
0120
0121
0122
0123
0124
0125 static int mtouch_connect(struct serio *serio, struct serio_driver *drv)
0126 {
0127 struct mtouch *mtouch;
0128 struct input_dev *input_dev;
0129 int err;
0130
0131 mtouch = kzalloc(sizeof(struct mtouch), GFP_KERNEL);
0132 input_dev = input_allocate_device();
0133 if (!mtouch || !input_dev) {
0134 err = -ENOMEM;
0135 goto fail1;
0136 }
0137
0138 mtouch->serio = serio;
0139 mtouch->dev = input_dev;
0140 snprintf(mtouch->phys, sizeof(mtouch->phys), "%s/input0", serio->phys);
0141
0142 input_dev->name = "MicroTouch Serial TouchScreen";
0143 input_dev->phys = mtouch->phys;
0144 input_dev->id.bustype = BUS_RS232;
0145 input_dev->id.vendor = SERIO_MICROTOUCH;
0146 input_dev->id.product = 0;
0147 input_dev->id.version = 0x0100;
0148 input_dev->dev.parent = &serio->dev;
0149 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0150 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
0151 input_set_abs_params(mtouch->dev, ABS_X, MTOUCH_MIN_XC, MTOUCH_MAX_XC, 0, 0);
0152 input_set_abs_params(mtouch->dev, ABS_Y, MTOUCH_MIN_YC, MTOUCH_MAX_YC, 0, 0);
0153
0154 serio_set_drvdata(serio, mtouch);
0155
0156 err = serio_open(serio, drv);
0157 if (err)
0158 goto fail2;
0159
0160 err = input_register_device(mtouch->dev);
0161 if (err)
0162 goto fail3;
0163
0164 return 0;
0165
0166 fail3: serio_close(serio);
0167 fail2: serio_set_drvdata(serio, NULL);
0168 fail1: input_free_device(input_dev);
0169 kfree(mtouch);
0170 return err;
0171 }
0172
0173
0174
0175
0176
0177 static const struct serio_device_id mtouch_serio_ids[] = {
0178 {
0179 .type = SERIO_RS232,
0180 .proto = SERIO_MICROTOUCH,
0181 .id = SERIO_ANY,
0182 .extra = SERIO_ANY,
0183 },
0184 { 0 }
0185 };
0186
0187 MODULE_DEVICE_TABLE(serio, mtouch_serio_ids);
0188
0189 static struct serio_driver mtouch_drv = {
0190 .driver = {
0191 .name = "mtouch",
0192 },
0193 .description = DRIVER_DESC,
0194 .id_table = mtouch_serio_ids,
0195 .interrupt = mtouch_interrupt,
0196 .connect = mtouch_connect,
0197 .disconnect = mtouch_disconnect,
0198 };
0199
0200 module_serio_driver(mtouch_drv);