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