summaryrefslogtreecommitdiffstats
path: root/target/linux/olpc/files/drivers/input/mouse/olpc.c
blob: dc575ac06296279fd779d225a661100ff97c93e2 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 * OLPC touchpad PS/2 mouse driver
 *
 * Copyright (c) 2006 One Laptop Per Child, inc.
 * Authors Zephaniah E. Hull and Andres Salomon <dilinger@laptop.org>
 *
 * This driver is partly based on the ALPS driver, which is:
 *
 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
 *
 * 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.
 */

/*
 * The touchpad on the OLPC is fairly wide, with the entire area usable
 * as a tablet ("PT mode"), and the center 1/3rd also usable as a touchpad
 * ("GS mode").
 *
 * Earlier version of the device had simultaneous reporting; however, that
 * was removed.  Instead, the device now reports packets in one mode, and
 * tells the driver when a mode switch needs to happen.
 */

#include <linux/input.h>
#include <linux/serio.h>
#include <linux/libps2.h>
#include <linux/delay.h>
#include <asm/olpc.h>

#include "psmouse.h"
#include "olpc.h"

static int tpdebug;
module_param(tpdebug, int, 0644);

#define OLPC_GS		1	/* The GS sensor. */
#define OLPC_PT		2	/* The PT sensor. */

static struct olpc_model_info olpc_model_data[] = {
	{ { 0x67, 0x00, 0x00 }, OLPC_GS | OLPC_PT }, /* unknown ID */
	{ { 0x67, 0x00, 0x0a }, OLPC_GS | OLPC_PT }, /* pre-B1 */
	{ { 0x67, 0x00, 0x14 }, OLPC_GS }, /* B1.*/
	{ { 0x67, 0x00, 0x28 }, OLPC_GS | OLPC_PT }, /* B2 */
	{ { 0x67, 0x00, 0x3c }, OLPC_GS | OLPC_PT }, /* B2-2 */
	{ { 0x67, 0x00, 0x50 }, OLPC_GS | OLPC_PT }, /* C1 */
};

#define OLPC_PKT_PT	0xcf
#define OLPC_PKT_GS	0xff

static int olpc_absolute_mode(struct psmouse *psmouse, int mode);

/*
 * OLPC absolute Mode - single-mode format
 *
 * byte 0:      1    1    0    0    1    1     1     1
 * byte 1:      0   x6   x5   x4   x3   x2    x1    x0
 * byte 2(PT):  0    0   x9   x8   x7    ? pt-dsw gs-dsw
 * byte 2(GS):  0  x10   x9   x8   x7    ? gs-dsw pt-dsw
 * byte 3:      0   y9   y8   y7    1    0   swr   swl
 * byte 4:      0   y6   y5   y4   y3   y2    y1    y0
 * byte 5:      0   z6   z5   z4   z3   z2    z1    z0
 *
 * ?'s are not defined in the protocol spec, may vary between models.
 *
 * swr/swl are the left/right buttons.
 *
 * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
 * pen/finger
 */

static void olpc_process_packet_gspt(struct psmouse *psmouse)
{
	struct olpc_data *priv = psmouse->private;
	unsigned char *packet = psmouse->packet;
	struct input_dev *dev = psmouse->dev;
	struct input_dev *dev2 = priv->dev2;
	int x, y, z, gs_down = 0, pt_down = 0, left, right;
	struct timeval now_tv;
	s64 now_ns;

	left = packet[3] & 1;
	right = packet[3] & 2;
	x = packet[1] | ((packet[2] & 0x78) << 4);
	y = packet[4] | ((packet[3] & 0x70) << 3);
	z = packet[5];

	if (psmouse->packet[0] == OLPC_PKT_GS) {
		pt_down = !!(packet[2] & 1);
		gs_down = !!(packet[2] & 2);
	} else if (psmouse->packet[0] == OLPC_PKT_PT) {
		gs_down = !!(packet[2] & 1);
		pt_down = !!(packet[2] & 2);
	}

	/*
	 * XXX: Kludge.
	 * If it's been more than 30ms since the last packet,
	 * assume that there was a lift we were never told about.
	 */
	do_gettimeofday(&now_tv);
	now_ns = timeval_to_ns (&now_tv);
	if (now_ns >= priv->late) {
		input_report_key(dev, BTN_TOUCH, 0);
		input_report_key(dev, BTN_TOOL_PEN, 0);
		input_report_key(dev2, BTN_TOUCH, 0);
		input_report_key(dev2, BTN_TOOL_FINGER, 0);

		input_sync(dev);
		input_sync(dev2);
	}

	priv->late = now_ns + (30 * NSEC_PER_MSEC);


	if (tpdebug) {
		printk(KERN_DEBUG "%s %02x %02x %02x %02x %02x %02x\n",
		       __FUNCTION__, psmouse->packet[0], psmouse->packet[1],
		       psmouse->packet[2], psmouse->packet[3], psmouse->packet[4],
		       psmouse->packet[5]);
		printk(KERN_DEBUG "l=%d r=%d p=%d g=%d x=%d y=%d z=%d\n",
		       left, right, pt_down, gs_down, x, y, z);
	}

	if (psmouse->packet[0] == OLPC_PKT_PT) {
		input_report_key(dev, BTN_LEFT, left);
		input_report_key(dev, BTN_RIGHT, right);
	} else if (psmouse->packet[0] == OLPC_PKT_GS) {
		input_report_key(dev, BTN_LEFT, left);
		input_report_key(dev, BTN_RIGHT, right);
		input_report_key(dev2, BTN_LEFT, left);
		input_report_key(dev2, BTN_RIGHT, right);
	}

	input_report_key(dev, BTN_TOUCH, pt_down);
	input_report_key(dev, BTN_TOOL_PEN, pt_down);
	input_report_key(dev2, BTN_TOUCH, gs_down);
	input_report_key(dev2, BTN_TOOL_FINGER, gs_down);

	input_report_abs(dev2, ABS_PRESSURE, z);

	if (psmouse->packet[0] == OLPC_PKT_PT && pt_down) {
		input_report_abs(dev, ABS_X, x);
		input_report_abs(dev, ABS_Y, y);
	} else if (psmouse->packet[0] == OLPC_PKT_GS && gs_down) {
		input_report_abs(dev2, ABS_X, x);
		input_report_abs(dev2, ABS_Y, y);
	}

	input_sync(dev);
	input_sync(dev2);

	if (priv->pending_mode == OLPC_GS &&
			psmouse->packet[0] == OLPC_PKT_PT && pt_down) {
		priv->pending_mode = 0;
		cancel_delayed_work(&priv->mode_switch);
	}

	if (priv->i->flags & (OLPC_PT|OLPC_GS)) {
		int pending = 0;
		if (psmouse->packet[0] == OLPC_PKT_PT && !pt_down)
			pending = OLPC_GS;
		else if (psmouse->packet[0] == OLPC_PKT_GS && pt_down)
			pending = OLPC_PT;

		if (priv->current_mode == pending) {
			priv->pending_mode = 0;
			pending = priv->current_mode;
		}
		else if (priv->pending_mode != pending) {
			priv->pending_mode = pending;
			if (tpdebug)
				printk(KERN_WARNING "Scheduling mode switch to %s.\n",
						pending == OLPC_GS ? "GS" : "PT");

			/*
			 * Apply a de-bounce when switching from PT to GS, to allow for
			 * spurious PT-up packets.
			 */
			if (priv->pending_mode == OLPC_GS)
				queue_delayed_work(kpsmoused_wq, &priv->mode_switch, msecs_to_jiffies(50));
			else
				queue_delayed_work(kpsmoused_wq, &priv->mode_switch, 0);
		}
	}
}

static psmouse_ret_t olpc_process_byte(struct psmouse *psmouse)
{
	psmouse_ret_t ret = PSMOUSE_BAD_DATA;

	if (psmouse->packet[0] != OLPC_PKT_PT &&
	    psmouse->packet[0] != OLPC_PKT_GS)
		goto out;

	/* Bytes 2 - 6 should have 0 in the highest bit */
	if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
			(psmouse->packet[psmouse->pktcnt - 1] & 0x80))
		goto out;

	if (psmouse->pktcnt == 6) {
		olpc_process_packet_gspt(psmouse);
		ret = PSMOUSE_FULL_PACKET;
		goto out;
	}

	ret = PSMOUSE_GOOD_DATA;
out:
	if (ret != PSMOUSE_GOOD_DATA && ret != PSMOUSE_FULL_PACKET)
		printk(KERN_DEBUG "%s: (%d) %02x %02x %02x %02x %02x %02x\n",
		       __FUNCTION__, psmouse->pktcnt, psmouse->packet[0],
			   psmouse->packet[1], psmouse->packet[2],
			   psmouse->packet[3], psmouse->packet[4],
		       psmouse->packet[5]);
	return ret;
}

static struct olpc_model_info *olpc_get_model(struct psmouse *psmouse)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	unsigned char param[4];
	int i;

	/*
	 * Now try "E7 report". Allowed responses are in
	 * olpc_model_data[].signature
	 */
	if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21))
		return NULL;

	param[0] = param[1] = param[2] = 0xff;
	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
		return NULL;

	pr_debug("olpc.c(%d): E7 report: %2.2x %2.2x %2.2x",
		__LINE__, param[0], param[1], param[2]);

	for (i = 0; i < ARRAY_SIZE(olpc_model_data); i++) {
		if (!memcmp(param, olpc_model_data[i].signature,
				sizeof(olpc_model_data[i].signature))) {
			printk(KERN_INFO __FILE__ ": OLPC touchpad revision 0x%x.\n", param[2]);
			return olpc_model_data + i;
		}
	}

	/* 
	 * ALPS creates new IDs pretty frequently; rather than listing them
	 * all, just assume they support the defaults.  We've set aside the
	 * first entry of olpc_model_data as the catch-all.
	 */
	if (!memcmp(param, olpc_model_data[0].signature, 2)) {
		printk(KERN_INFO __FILE__ ": unknown ALPS revision %x, assuming default flags.\n", param[2]);
		return &olpc_model_data[0];
	}

	return NULL;
}

static int olpc_find_mode(struct psmouse *psmouse)
{
	struct olpc_data *priv = psmouse->private;
	int mode = priv->i->flags;

	if (mode & OLPC_GS)
		mode = OLPC_GS;
	else if (mode & OLPC_PT)
		mode = OLPC_PT;
	else
		mode = -1;

	return mode;
}

/*
 * Touchpad should be disabled before calling this!
 */
static int olpc_new_mode(struct psmouse *psmouse, int mode)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	struct olpc_data *priv = psmouse->private;
	unsigned char param;
	int ret;

	if (tpdebug)
		printk(KERN_WARNING __FILE__ ": Switching to %d. [%lu]\n", mode, jiffies);

	if ((ret = ps2_command(ps2dev, &param, 0x01F2)))
		goto failed;
	if ((ret = ps2_command(ps2dev, &param, 0x01F2)))
		goto failed;
	if ((ret = ps2_command(ps2dev, &param, 0x01F2)))
		goto failed;

	switch (mode) {
	default:
		printk(KERN_WARNING __FILE__ ": Invalid mode %d. Defaulting to OLPC_GS.\n", mode);
	case OLPC_GS:
		ret = ps2_command(ps2dev, NULL, 0xE6);
		break;
	case OLPC_PT:
		ret = ps2_command(ps2dev, NULL, 0xE7);
		break;
	}
	if (ret)
		goto failed;

	/* XXX: This is a bit hacky, make sure this isn't screwing stuff up. */
	psmouse->pktcnt = psmouse->out_of_sync = 0;
	psmouse->last = jiffies;
	psmouse->state = PSMOUSE_ACTIVATED;

	if ((ret = ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)))
		goto failed;

	priv->current_mode = mode;
	priv->pending_mode = 0;
	if (tpdebug)
		printk(KERN_WARNING __FILE__ ": Switched to mode %d successful.\n", mode);

failed:
	if (ret)
		printk(KERN_WARNING __FILE__ ": Mode switch to %d failed! (%d) [%lu]\n", mode, ret, jiffies);
	return ret;
}

static int olpc_absolute_mode(struct psmouse *psmouse, int mode)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;

	/* Switch to 'Advanced mode.', four disables in a row. */
	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
			ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
			ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
			ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
		return -1;
	
	return olpc_new_mode(psmouse, mode);
}

/*
 * olpc_poll() - poll the touchpad for current motion packet.
 * Used in resync.
 * Note: We can't poll, so always return failure.
 */
static int olpc_poll(struct psmouse *psmouse)
{
	return -1;
}

static int olpc_reconnect(struct psmouse *psmouse)
{
	struct olpc_data *priv = psmouse->private;
	int mode;

	if (olpc_rev_after(OLPC_REV_B2))
		if (psmouse->ps2dev.serio->dev.power.power_state.event != PM_EVENT_ON)
			return 0;

	psmouse_reset(psmouse);

	if (!(priv->i = olpc_get_model(psmouse)))
		return -1;

	mode = olpc_find_mode(psmouse);
	if (mode < 0)
		return -1;

	if (olpc_absolute_mode(psmouse, mode)) {
		printk(KERN_ERR __FILE__ ": Failed to reenable absolute mode.\n");
		return -1;
	}

	return 0;
}

static void olpc_disconnect(struct psmouse *psmouse)
{
	struct olpc_data *priv = psmouse->private;

	psmouse_reset(psmouse);
	input_unregister_device(priv->dev2);
	kfree(priv);
}

static void olpc_mode_switch(struct work_struct *w)
{
	struct delayed_work *work = container_of(w, struct delayed_work, work);
	struct olpc_data *priv = container_of(work, struct olpc_data, mode_switch);
	struct psmouse *psmouse = priv->psmouse;
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	int pending_mode, ret;

	if (priv->pending_mode == priv->current_mode) {
		priv->pending_mode = 0;
		printk (KERN_DEBUG __FILE__ ": In switch_mode, no target mode.\n");
		return;
	}

	if (tpdebug)
		printk(KERN_WARNING __FILE__ ": Disable for switch to %d. [%lu]\n", priv->pending_mode, jiffies);

	/* XXX: This is a bit hacky, make sure this isn't screwing stuff up. */
	psmouse->state = PSMOUSE_INITIALIZING;

	ret = ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE);
	if (ret) {
		/* XXX: if this ever fails, we need to do a full reset! */
		printk(KERN_WARNING __FILE__ ": Disable failed for switch to %d. (%d) [%lu]\n", priv->pending_mode, ret, jiffies);
		return;
	}

	/*
	 * ALPS tells us that it may take up to 20msec for the disable to
	 * take effect; however, ps2_command() will wait up to 200msec for
	 * the ACK to come back (and I'm assuming that by the time the
	 * hardware sends back its ACK, it has stopped sending bytes).
	 */
	pending_mode = priv->pending_mode;

	if (olpc_new_mode(psmouse, priv->pending_mode))
		goto bad;

	/*
	 * Deal with a potential race condition.
	 *
	 * If there is a brief tap of a stylus or a fingernail that
	 * triggers a mode switch to PT mode, and the stylus/fingernail is
	 * lifted after the DISABLE above, but before we reenable in the new mode,
	 * then we can get stuck in PT mode.
	 */
	if (pending_mode == OLPC_PT) {
		priv->pending_mode = OLPC_GS;
		queue_delayed_work(kpsmoused_wq, &priv->mode_switch, msecs_to_jiffies(50));
	}

	return;

bad:
	printk(KERN_WARNING __FILE__ ": Failure to switch modes, resetting device...\n");
	olpc_reconnect(psmouse);
}

int olpc_init(struct psmouse *psmouse)
{
	struct olpc_data *priv;
	struct input_dev *dev = psmouse->dev;
	struct input_dev *dev2;
	int mode;

	priv = kzalloc(sizeof(struct olpc_data), GFP_KERNEL);
	dev2 = input_allocate_device();
	if (!priv || !dev2)
		goto init_fail;

	psmouse->private = priv;
	priv->dev2 = dev2;
	priv->psmouse = psmouse;

	psmouse_reset(psmouse);
	if (!(priv->i = olpc_get_model(psmouse)))
		goto init_fail;

	mode = olpc_find_mode(psmouse);
	if (mode < 0) {
		printk(KERN_ERR __FILE__ ": Failed to identify proper mode\n");
		goto init_fail;
	}

	if (olpc_absolute_mode(psmouse, mode)) {
		printk(KERN_ERR __FILE__ ": Failed to enable absolute mode\n");
		goto init_fail;
	}

	/*
	 * Unset some of the default bits for things we don't have.
	 */
	dev->evbit[LONG(EV_REL)] &= ~BIT(EV_REL);
	dev->relbit[LONG(REL_X)] &= ~(BIT(REL_X) | BIT(REL_Y));
	dev->keybit[LONG(BTN_MIDDLE)] &= ~BIT(BTN_MIDDLE);

	dev->evbit[LONG(EV_KEY)] |= BIT(EV_KEY);
	dev->keybit[LONG(BTN_TOUCH)] |= BIT(BTN_TOUCH);
	dev->keybit[LONG(BTN_TOOL_PEN)] |= BIT(BTN_TOOL_PEN);
	dev->keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT);

	dev->evbit[LONG(EV_ABS)] |= BIT(EV_ABS);
	input_set_abs_params(dev, ABS_X, 2, 1000, 0, 0);
	input_set_abs_params(dev, ABS_Y, 0, 717, 0, 0);

	snprintf(priv->phys, sizeof(priv->phys),
		"%s/input1", psmouse->ps2dev.serio->phys);
	dev2->phys = priv->phys;
	dev2->name = "OLPC ALPS GlideSensor";
	dev2->id.bustype = BUS_I8042;
	dev2->id.vendor  = 0x0002;
	dev2->id.product = PSMOUSE_OLPC;
	dev2->id.version = 0x0000;

	dev2->evbit[LONG(EV_KEY)] |= BIT(EV_KEY);
	dev2->keybit[LONG(BTN_TOUCH)] |= BIT(BTN_TOUCH);
	dev2->keybit[LONG(BTN_TOOL_FINGER)] |= BIT(BTN_TOOL_FINGER);
	dev2->keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT);

	dev2->evbit[LONG(EV_ABS)] |= BIT(EV_ABS);
	input_set_abs_params(dev2, ABS_X, 350, 512, 0, 0);
	input_set_abs_params(dev2, ABS_Y, 70, 325, 0, 0);
	input_set_abs_params(dev2, ABS_PRESSURE, 0, 63, 0, 0);

	if (input_register_device(dev2)) {
		printk(KERN_ERR __FILE__ ": Failed to register GlideSensor\n");
		goto init_fail;
	}

	psmouse->protocol_handler = olpc_process_byte;
	psmouse->poll = olpc_poll;
	psmouse->disconnect = olpc_disconnect;
	psmouse->reconnect = olpc_reconnect;
	psmouse->pktsize = 6;

	/* Disable the idle resync. */
	psmouse->resync_time = 0;
	/* Reset after a lot of bad bytes. */
	psmouse->resetafter = 1024;

	INIT_DELAYED_WORK(&priv->mode_switch, olpc_mode_switch);

	return 0;

init_fail:
	input_free_device(dev2);
	kfree(priv);
	return -1;
}

int olpc_detect(struct psmouse *psmouse, int set_properties)
{
	if (!olpc_get_model(psmouse))
		return -1;

	if (set_properties) {
		psmouse->vendor = "ALPS";
		psmouse->name = "PenTablet";
		psmouse->model = 0;
	}
	return 0;
}