elo.c 9.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Elo serial touchscreen driver
 *
 * Copyright (c) 2004 Vojtech Pavlik
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

/*
 * This driver can handle serial Elo touchscreens using either the Elo standard
 * 'E271-2210' 10-byte protocol, Elo legacy 'E281A-4002' 6-byte protocol, Elo
 * legacy 'E271-140' 4-byte protocol and Elo legacy 'E261-280' 3-byte protocol.
 */

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/serio.h>
#include <linux/init.h>
26
#include <linux/ctype.h>
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36 37

#define DRIVER_DESC	"Elo serial touchscreen driver"

MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

/*
 * Definitions & global arrays.
 */

38 39
#define ELO_MAX_LENGTH		10

40 41 42 43
#define ELO10_PACKET_LEN	8
#define ELO10_TOUCH		0x03
#define ELO10_PRESSURE		0x80

44 45
#define ELO10_LEAD_BYTE		'U'

46 47
#define ELO10_ID_CMD		'i'

48
#define ELO10_TOUCH_PACKET	'T'
49 50
#define ELO10_ACK_PACKET	'A'
#define ELI10_ID_PACKET		'I'
L
Linus Torvalds 已提交
51 52 53 54 55 56

/*
 * Per-touchscreen data.
 */

struct elo {
57
	struct input_dev *dev;
L
Linus Torvalds 已提交
58
	struct serio *serio;
59 60
	struct mutex cmd_mutex;
	struct completion cmd_done;
L
Linus Torvalds 已提交
61 62
	int id;
	int idx;
63
	unsigned char expected_packet;
L
Linus Torvalds 已提交
64 65
	unsigned char csum;
	unsigned char data[ELO_MAX_LENGTH];
66
	unsigned char response[ELO10_PACKET_LEN];
L
Linus Torvalds 已提交
67 68 69
	char phys[32];
};

70
static void elo_process_data_10(struct elo *elo, unsigned char data, struct pt_regs *regs)
L
Linus Torvalds 已提交
71
{
72
	struct input_dev *dev = elo->dev;
L
Linus Torvalds 已提交
73

74
	elo->data[elo->idx] = data;
L
Linus Torvalds 已提交
75 76
	switch (elo->idx++) {
		case 0:
77 78 79
			elo->csum = 0xaa;
			if (data != ELO10_LEAD_BYTE) {
				pr_debug("elo: unsynchronized data: 0x%02x\n", data);
L
Linus Torvalds 已提交
80 81 82 83 84 85
				elo->idx = 0;
			}
			break;

		case 9:
			elo->idx = 0;
86 87 88 89 90
			if (data != elo->csum) {
				pr_debug("elo: bad checksum: 0x%02x, expected 0x%02x\n",
					 data, elo->csum);
				break;
			}
91 92 93 94
			if (elo->data[1] != elo->expected_packet) {
				if (elo->data[1] != ELO10_TOUCH_PACKET)
					pr_debug("elo: unexpected packet: 0x%02x\n",
						 elo->data[1]);
95 96
				break;
			}
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
			if (likely(elo->data[1] == ELO10_TOUCH_PACKET)) {
				input_regs(dev, regs);
				input_report_abs(dev, ABS_X, (elo->data[4] << 8) | elo->data[3]);
				input_report_abs(dev, ABS_Y, (elo->data[6] << 8) | elo->data[5]);
				if (elo->data[2] & ELO10_PRESSURE)
					input_report_abs(dev, ABS_PRESSURE,
							(elo->data[8] << 8) | elo->data[7]);
				input_report_key(dev, BTN_TOUCH, elo->data[2] & ELO10_TOUCH);
				input_sync(dev);
			} else if (elo->data[1] == ELO10_ACK_PACKET) {
				if (elo->data[2] == '0')
					elo->expected_packet = ELO10_TOUCH_PACKET;
				complete(&elo->cmd_done);
			} else {
				memcpy(elo->response, &elo->data[1], ELO10_PACKET_LEN);
				elo->expected_packet = ELO10_ACK_PACKET;
			}
L
Linus Torvalds 已提交
114 115
			break;
	}
116
	elo->csum += data;
L
Linus Torvalds 已提交
117 118
}

119
static void elo_process_data_6(struct elo *elo, unsigned char data, struct pt_regs *regs)
L
Linus Torvalds 已提交
120
{
121
	struct input_dev *dev = elo->dev;
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

	elo->data[elo->idx] = data;

	switch (elo->idx++) {

		case 0: if ((data & 0xc0) != 0xc0) elo->idx = 0; break;
		case 1: if ((data & 0xc0) != 0x80) elo->idx = 0; break;
		case 2: if ((data & 0xc0) != 0x40) elo->idx = 0; break;

		case 3:
			if (data & 0xc0) {
				elo->idx = 0;
				break;
			}

			input_regs(dev, regs);
			input_report_abs(dev, ABS_X, ((elo->data[0] & 0x3f) << 6) | (elo->data[1] & 0x3f));
			input_report_abs(dev, ABS_Y, ((elo->data[2] & 0x3f) << 6) | (elo->data[3] & 0x3f));

			if (elo->id == 2) {
				input_report_key(dev, BTN_TOUCH, 1);
				input_sync(dev);
				elo->idx = 0;
			}

			break;

		case 4:
			if (data) {
				input_sync(dev);
				elo->idx = 0;
			}
			break;

		case 5:
			if ((data & 0xf0) == 0) {
				input_report_abs(dev, ABS_PRESSURE, elo->data[5]);
159
				input_report_key(dev, BTN_TOUCH, !!elo->data[5]);
L
Linus Torvalds 已提交
160 161 162 163 164 165 166
			}
			input_sync(dev);
			elo->idx = 0;
			break;
	}
}

167
static void elo_process_data_3(struct elo *elo, unsigned char data, struct pt_regs *regs)
L
Linus Torvalds 已提交
168
{
169
	struct input_dev *dev = elo->dev;
L
Linus Torvalds 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

	elo->data[elo->idx] = data;

	switch (elo->idx++) {

		case 0:
			if ((data & 0x7f) != 0x01)
				elo->idx = 0;
			break;
		case 2:
			input_regs(dev, regs);
			input_report_key(dev, BTN_TOUCH, !(elo->data[1] & 0x80));
			input_report_abs(dev, ABS_X, elo->data[1]);
			input_report_abs(dev, ABS_Y, elo->data[2]);
			input_sync(dev);
			elo->idx = 0;
			break;
	}
}

static irqreturn_t elo_interrupt(struct serio *serio,
		unsigned char data, unsigned int flags, struct pt_regs *regs)
{
193
	struct elo *elo = serio_get_drvdata(serio);
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

	switch(elo->id) {
		case 0:
			elo_process_data_10(elo, data, regs);
			break;

		case 1:
		case 2:
			elo_process_data_6(elo, data, regs);
			break;

		case 3:
			elo_process_data_3(elo, data, regs);
			break;
	}

	return IRQ_HANDLED;
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
static int elo_command_10(struct elo *elo, unsigned char *packet)
{
	int rc = -1;
	int i;
	unsigned char csum = 0xaa + ELO10_LEAD_BYTE;

	mutex_lock(&elo->cmd_mutex);

	serio_pause_rx(elo->serio);
	elo->expected_packet = toupper(packet[0]);
	init_completion(&elo->cmd_done);
	serio_continue_rx(elo->serio);

	if (serio_write(elo->serio, ELO10_LEAD_BYTE))
		goto out;

	for (i = 0; i < ELO10_PACKET_LEN; i++) {
		csum += packet[i];
		if (serio_write(elo->serio, packet[i]))
			goto out;
	}

	if (serio_write(elo->serio, csum))
		goto out;

	wait_for_completion_timeout(&elo->cmd_done, HZ);

	if (elo->expected_packet == ELO10_TOUCH_PACKET) {
		/* We are back in reporting mode, the command was ACKed */
		memcpy(packet, elo->response, ELO10_PACKET_LEN);
		rc = 0;
	}

 out:
	mutex_unlock(&elo->cmd_mutex);
	return rc;
}

static int elo_setup_10(struct elo *elo)
{
	static const char *elo_types[] = { "Accu", "Dura", "Intelli", "Carroll" };
	struct input_dev *dev = elo->dev;
	unsigned char packet[ELO10_PACKET_LEN] = { ELO10_ID_CMD };

	if (elo_command_10(elo, packet))
		return -1;

	dev->id.version = (packet[5] << 8) | packet[4];

	input_set_abs_params(dev, ABS_X, 96, 4000, 0, 0);
	input_set_abs_params(dev, ABS_Y, 96, 4000, 0, 0);
	if (packet[3] & ELO10_PRESSURE)
		input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);

	printk(KERN_INFO "elo: %sTouch touchscreen, fw: %02x.%02x, "
		"features: %x02x, controller: 0x%02x\n",
		elo_types[(packet[1] -'0') & 0x03],
		packet[5], packet[4], packet[3], packet[7]);

	return 0;
}

L
Linus Torvalds 已提交
275 276 277 278 279 280
/*
 * elo_disconnect() is the opposite of elo_connect()
 */

static void elo_disconnect(struct serio *serio)
{
281
	struct elo *elo = serio_get_drvdata(serio);
L
Linus Torvalds 已提交
282

283
	input_get_device(elo->dev);
284
	input_unregister_device(elo->dev);
L
Linus Torvalds 已提交
285 286
	serio_close(serio);
	serio_set_drvdata(serio, NULL);
287
	input_put_device(elo->dev);
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296 297 298 299
	kfree(elo);
}

/*
 * elo_connect() is the routine that is called when someone adds a
 * new serio device that supports Gunze protocol and registers it as
 * an input device.
 */

static int elo_connect(struct serio *serio, struct serio_driver *drv)
{
	struct elo *elo;
300
	struct input_dev *input_dev;
L
Linus Torvalds 已提交
301 302
	int err;

303 304 305 306
	elo = kzalloc(sizeof(struct elo), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!elo || !input_dev) {
		err = -ENOMEM;
307
		goto fail1;
308
	}
L
Linus Torvalds 已提交
309

310 311 312
	elo->serio = serio;
	elo->id = serio->id.id;
	elo->dev = input_dev;
313 314 315
	elo->expected_packet = ELO10_TOUCH_PACKET;
	mutex_init(&elo->cmd_mutex);
	init_completion(&elo->cmd_done);
316
	snprintf(elo->phys, sizeof(elo->phys), "%s/input0", serio->phys);
L
Linus Torvalds 已提交
317

318 319 320 321 322 323 324 325
	input_dev->private = elo;
	input_dev->name = "Elo Serial TouchScreen";
	input_dev->phys = elo->phys;
	input_dev->id.bustype = BUS_RS232;
	input_dev->id.vendor = SERIO_ELO;
	input_dev->id.product = elo->id;
	input_dev->id.version = 0x0100;
	input_dev->cdev.dev = &serio->dev;
L
Linus Torvalds 已提交
326

327 328
	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
	input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
L
Linus Torvalds 已提交
329

330 331 332 333 334
	serio_set_drvdata(serio, elo);
	err = serio_open(serio, drv);
	if (err)
		goto fail2;

L
Linus Torvalds 已提交
335 336 337
	switch (elo->id) {

		case 0: /* 10-byte protocol */
338 339 340
			if (elo_setup_10(elo))
				goto fail3;

L
Linus Torvalds 已提交
341
			break;
342

L
Linus Torvalds 已提交
343
		case 1: /* 6-byte protocol */
344
			input_set_abs_params(input_dev, ABS_PRESSURE, 0, 15, 0, 0);
L
Linus Torvalds 已提交
345 346

		case 2: /* 4-byte protocol */
347 348
			input_set_abs_params(input_dev, ABS_X, 96, 4000, 0, 0);
			input_set_abs_params(input_dev, ABS_Y, 96, 4000, 0, 0);
L
Linus Torvalds 已提交
349 350 351
			break;

		case 3: /* 3-byte protocol */
352 353
			input_set_abs_params(input_dev, ABS_X, 0, 255, 0, 0);
			input_set_abs_params(input_dev, ABS_Y, 0, 255, 0, 0);
L
Linus Torvalds 已提交
354 355 356
			break;
	}

357 358 359
	err = input_register_device(elo->dev);
	if (err)
		goto fail3;
L
Linus Torvalds 已提交
360 361

	return 0;
362

363 364 365
 fail3: serio_close(serio);
 fail2:	serio_set_drvdata(serio, NULL);
 fail1:	input_free_device(input_dev);
366 367
	kfree(elo);
	return err;
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
}

/*
 * The serio driver structure.
 */

static struct serio_device_id elo_serio_ids[] = {
	{
		.type	= SERIO_RS232,
		.proto	= SERIO_ELO,
		.id	= SERIO_ANY,
		.extra	= SERIO_ANY,
	},
	{ 0 }
};

MODULE_DEVICE_TABLE(serio, elo_serio_ids);

static struct serio_driver elo_drv = {
	.driver		= {
		.name	= "elo",
	},
	.description	= DRIVER_DESC,
	.id_table	= elo_serio_ids,
	.interrupt	= elo_interrupt,
	.connect	= elo_connect,
	.disconnect	= elo_disconnect,
};

/*
 * The functions for inserting/removing us as a module.
 */

static int __init elo_init(void)
{
	serio_register_driver(&elo_drv);
	return 0;
}

static void __exit elo_exit(void)
{
	serio_unregister_driver(&elo_drv);
}

module_init(elo_init);
module_exit(elo_exit);