Loading...
1/*
2 * Hampshire serial touchscreen driver
3 *
4 * Copyright (c) 2010 Adam Bennett
5 * Based on the dynapro driver (c) Tias Guns
6 *
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
13 */
14
15/*
16 * 2010/04/08 Adam Bennett <abennett72@gmail.com>
17 * Copied dynapro.c and edited for Hampshire 4-byte protocol
18 */
19
20#include <linux/errno.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/input.h>
25#include <linux/serio.h>
26
27#define DRIVER_DESC "Hampshire serial touchscreen driver"
28
29MODULE_AUTHOR("Adam Bennett <abennett72@gmail.com>");
30MODULE_DESCRIPTION(DRIVER_DESC);
31MODULE_LICENSE("GPL");
32
33/*
34 * Definitions & global arrays.
35 */
36
37#define HAMPSHIRE_FORMAT_TOUCH_BIT 0x40
38#define HAMPSHIRE_FORMAT_LENGTH 4
39#define HAMPSHIRE_RESPONSE_BEGIN_BYTE 0x80
40
41#define HAMPSHIRE_MIN_XC 0
42#define HAMPSHIRE_MAX_XC 0x1000
43#define HAMPSHIRE_MIN_YC 0
44#define HAMPSHIRE_MAX_YC 0x1000
45
46#define HAMPSHIRE_GET_XC(data) (((data[3] & 0x0c) >> 2) | (data[1] << 2) | ((data[0] & 0x38) << 6))
47#define HAMPSHIRE_GET_YC(data) ((data[3] & 0x03) | (data[2] << 2) | ((data[0] & 0x07) << 9))
48#define HAMPSHIRE_GET_TOUCHED(data) (HAMPSHIRE_FORMAT_TOUCH_BIT & data[0])
49
50/*
51 * Per-touchscreen data.
52 */
53
54struct hampshire {
55 struct input_dev *dev;
56 struct serio *serio;
57 int idx;
58 unsigned char data[HAMPSHIRE_FORMAT_LENGTH];
59 char phys[32];
60};
61
62static void hampshire_process_data(struct hampshire *phampshire)
63{
64 struct input_dev *dev = phampshire->dev;
65
66 if (HAMPSHIRE_FORMAT_LENGTH == ++phampshire->idx) {
67 input_report_abs(dev, ABS_X, HAMPSHIRE_GET_XC(phampshire->data));
68 input_report_abs(dev, ABS_Y, HAMPSHIRE_GET_YC(phampshire->data));
69 input_report_key(dev, BTN_TOUCH,
70 HAMPSHIRE_GET_TOUCHED(phampshire->data));
71 input_sync(dev);
72
73 phampshire->idx = 0;
74 }
75}
76
77static irqreturn_t hampshire_interrupt(struct serio *serio,
78 unsigned char data, unsigned int flags)
79{
80 struct hampshire *phampshire = serio_get_drvdata(serio);
81
82 phampshire->data[phampshire->idx] = data;
83
84 if (HAMPSHIRE_RESPONSE_BEGIN_BYTE & phampshire->data[0])
85 hampshire_process_data(phampshire);
86 else
87 dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",
88 phampshire->data[0]);
89
90 return IRQ_HANDLED;
91}
92
93static void hampshire_disconnect(struct serio *serio)
94{
95 struct hampshire *phampshire = serio_get_drvdata(serio);
96
97 input_get_device(phampshire->dev);
98 input_unregister_device(phampshire->dev);
99 serio_close(serio);
100 serio_set_drvdata(serio, NULL);
101 input_put_device(phampshire->dev);
102 kfree(phampshire);
103}
104
105/*
106 * hampshire_connect() is the routine that is called when someone adds a
107 * new serio device that supports hampshire protocol and registers it as
108 * an input device. This is usually accomplished using inputattach.
109 */
110
111static int hampshire_connect(struct serio *serio, struct serio_driver *drv)
112{
113 struct hampshire *phampshire;
114 struct input_dev *input_dev;
115 int err;
116
117 phampshire = kzalloc(sizeof(struct hampshire), GFP_KERNEL);
118 input_dev = input_allocate_device();
119 if (!phampshire || !input_dev) {
120 err = -ENOMEM;
121 goto fail1;
122 }
123
124 phampshire->serio = serio;
125 phampshire->dev = input_dev;
126 snprintf(phampshire->phys, sizeof(phampshire->phys),
127 "%s/input0", serio->phys);
128
129 input_dev->name = "Hampshire Serial TouchScreen";
130 input_dev->phys = phampshire->phys;
131 input_dev->id.bustype = BUS_RS232;
132 input_dev->id.vendor = SERIO_HAMPSHIRE;
133 input_dev->id.product = 0;
134 input_dev->id.version = 0x0001;
135 input_dev->dev.parent = &serio->dev;
136 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
137 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
138 input_set_abs_params(phampshire->dev, ABS_X,
139 HAMPSHIRE_MIN_XC, HAMPSHIRE_MAX_XC, 0, 0);
140 input_set_abs_params(phampshire->dev, ABS_Y,
141 HAMPSHIRE_MIN_YC, HAMPSHIRE_MAX_YC, 0, 0);
142
143 serio_set_drvdata(serio, phampshire);
144
145 err = serio_open(serio, drv);
146 if (err)
147 goto fail2;
148
149 err = input_register_device(phampshire->dev);
150 if (err)
151 goto fail3;
152
153 return 0;
154
155 fail3: serio_close(serio);
156 fail2: serio_set_drvdata(serio, NULL);
157 fail1: input_free_device(input_dev);
158 kfree(phampshire);
159 return err;
160}
161
162/*
163 * The serio driver structure.
164 */
165
166static struct serio_device_id hampshire_serio_ids[] = {
167 {
168 .type = SERIO_RS232,
169 .proto = SERIO_HAMPSHIRE,
170 .id = SERIO_ANY,
171 .extra = SERIO_ANY,
172 },
173 { 0 }
174};
175
176MODULE_DEVICE_TABLE(serio, hampshire_serio_ids);
177
178static struct serio_driver hampshire_drv = {
179 .driver = {
180 .name = "hampshire",
181 },
182 .description = DRIVER_DESC,
183 .id_table = hampshire_serio_ids,
184 .interrupt = hampshire_interrupt,
185 .connect = hampshire_connect,
186 .disconnect = hampshire_disconnect,
187};
188
189module_serio_driver(hampshire_drv);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Hampshire serial touchscreen driver
4 *
5 * Copyright (c) 2010 Adam Bennett
6 * Based on the dynapro driver (c) Tias Guns
7 */
8
9
10/*
11 * 2010/04/08 Adam Bennett <abennett72@gmail.com>
12 * Copied dynapro.c and edited for Hampshire 4-byte protocol
13 */
14
15#include <linux/errno.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/input.h>
20#include <linux/serio.h>
21
22#define DRIVER_DESC "Hampshire serial touchscreen driver"
23
24MODULE_AUTHOR("Adam Bennett <abennett72@gmail.com>");
25MODULE_DESCRIPTION(DRIVER_DESC);
26MODULE_LICENSE("GPL");
27
28/*
29 * Definitions & global arrays.
30 */
31
32#define HAMPSHIRE_FORMAT_TOUCH_BIT 0x40
33#define HAMPSHIRE_FORMAT_LENGTH 4
34#define HAMPSHIRE_RESPONSE_BEGIN_BYTE 0x80
35
36#define HAMPSHIRE_MIN_XC 0
37#define HAMPSHIRE_MAX_XC 0x1000
38#define HAMPSHIRE_MIN_YC 0
39#define HAMPSHIRE_MAX_YC 0x1000
40
41#define HAMPSHIRE_GET_XC(data) (((data[3] & 0x0c) >> 2) | (data[1] << 2) | ((data[0] & 0x38) << 6))
42#define HAMPSHIRE_GET_YC(data) ((data[3] & 0x03) | (data[2] << 2) | ((data[0] & 0x07) << 9))
43#define HAMPSHIRE_GET_TOUCHED(data) (HAMPSHIRE_FORMAT_TOUCH_BIT & data[0])
44
45/*
46 * Per-touchscreen data.
47 */
48
49struct hampshire {
50 struct input_dev *dev;
51 struct serio *serio;
52 int idx;
53 unsigned char data[HAMPSHIRE_FORMAT_LENGTH];
54 char phys[32];
55};
56
57static void hampshire_process_data(struct hampshire *phampshire)
58{
59 struct input_dev *dev = phampshire->dev;
60
61 if (HAMPSHIRE_FORMAT_LENGTH == ++phampshire->idx) {
62 input_report_abs(dev, ABS_X, HAMPSHIRE_GET_XC(phampshire->data));
63 input_report_abs(dev, ABS_Y, HAMPSHIRE_GET_YC(phampshire->data));
64 input_report_key(dev, BTN_TOUCH,
65 HAMPSHIRE_GET_TOUCHED(phampshire->data));
66 input_sync(dev);
67
68 phampshire->idx = 0;
69 }
70}
71
72static irqreturn_t hampshire_interrupt(struct serio *serio,
73 unsigned char data, unsigned int flags)
74{
75 struct hampshire *phampshire = serio_get_drvdata(serio);
76
77 phampshire->data[phampshire->idx] = data;
78
79 if (HAMPSHIRE_RESPONSE_BEGIN_BYTE & phampshire->data[0])
80 hampshire_process_data(phampshire);
81 else
82 dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",
83 phampshire->data[0]);
84
85 return IRQ_HANDLED;
86}
87
88static void hampshire_disconnect(struct serio *serio)
89{
90 struct hampshire *phampshire = serio_get_drvdata(serio);
91
92 input_get_device(phampshire->dev);
93 input_unregister_device(phampshire->dev);
94 serio_close(serio);
95 serio_set_drvdata(serio, NULL);
96 input_put_device(phampshire->dev);
97 kfree(phampshire);
98}
99
100/*
101 * hampshire_connect() is the routine that is called when someone adds a
102 * new serio device that supports hampshire protocol and registers it as
103 * an input device. This is usually accomplished using inputattach.
104 */
105
106static int hampshire_connect(struct serio *serio, struct serio_driver *drv)
107{
108 struct hampshire *phampshire;
109 struct input_dev *input_dev;
110 int err;
111
112 phampshire = kzalloc(sizeof(struct hampshire), GFP_KERNEL);
113 input_dev = input_allocate_device();
114 if (!phampshire || !input_dev) {
115 err = -ENOMEM;
116 goto fail1;
117 }
118
119 phampshire->serio = serio;
120 phampshire->dev = input_dev;
121 snprintf(phampshire->phys, sizeof(phampshire->phys),
122 "%s/input0", serio->phys);
123
124 input_dev->name = "Hampshire Serial TouchScreen";
125 input_dev->phys = phampshire->phys;
126 input_dev->id.bustype = BUS_RS232;
127 input_dev->id.vendor = SERIO_HAMPSHIRE;
128 input_dev->id.product = 0;
129 input_dev->id.version = 0x0001;
130 input_dev->dev.parent = &serio->dev;
131 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
132 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
133 input_set_abs_params(phampshire->dev, ABS_X,
134 HAMPSHIRE_MIN_XC, HAMPSHIRE_MAX_XC, 0, 0);
135 input_set_abs_params(phampshire->dev, ABS_Y,
136 HAMPSHIRE_MIN_YC, HAMPSHIRE_MAX_YC, 0, 0);
137
138 serio_set_drvdata(serio, phampshire);
139
140 err = serio_open(serio, drv);
141 if (err)
142 goto fail2;
143
144 err = input_register_device(phampshire->dev);
145 if (err)
146 goto fail3;
147
148 return 0;
149
150 fail3: serio_close(serio);
151 fail2: serio_set_drvdata(serio, NULL);
152 fail1: input_free_device(input_dev);
153 kfree(phampshire);
154 return err;
155}
156
157/*
158 * The serio driver structure.
159 */
160
161static const struct serio_device_id hampshire_serio_ids[] = {
162 {
163 .type = SERIO_RS232,
164 .proto = SERIO_HAMPSHIRE,
165 .id = SERIO_ANY,
166 .extra = SERIO_ANY,
167 },
168 { 0 }
169};
170
171MODULE_DEVICE_TABLE(serio, hampshire_serio_ids);
172
173static struct serio_driver hampshire_drv = {
174 .driver = {
175 .name = "hampshire",
176 },
177 .description = DRIVER_DESC,
178 .id_table = hampshire_serio_ids,
179 .interrupt = hampshire_interrupt,
180 .connect = hampshire_connect,
181 .disconnect = hampshire_disconnect,
182};
183
184module_serio_driver(hampshire_drv);