| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 1 | /* | 
| Arjan Opmeer | 3f8c0df | 2009-04-18 19:05:40 -0700 | [diff] [blame] | 2 |  * Elantech Touchpad driver (v6) | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 3 |  * | 
| Arjan Opmeer | 3f8c0df | 2009-04-18 19:05:40 -0700 | [diff] [blame] | 4 |  * Copyright (C) 2007-2009 Arjan Opmeer <arjan@opmeer.net> | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 5 |  * | 
 | 6 |  * This program is free software; you can redistribute it and/or modify it | 
 | 7 |  * under the terms of the GNU General Public License version 2 as published | 
 | 8 |  * by the Free Software Foundation. | 
 | 9 |  * | 
 | 10 |  * Trademarks are the property of their respective owners. | 
 | 11 |  */ | 
 | 12 |  | 
 | 13 | #include <linux/delay.h> | 
 | 14 | #include <linux/module.h> | 
 | 15 | #include <linux/input.h> | 
 | 16 | #include <linux/serio.h> | 
 | 17 | #include <linux/libps2.h> | 
 | 18 | #include "psmouse.h" | 
 | 19 | #include "elantech.h" | 
 | 20 |  | 
 | 21 | #define elantech_debug(format, arg...)				\ | 
 | 22 | 	do {							\ | 
 | 23 | 		if (etd->debug)					\ | 
 | 24 | 			printk(KERN_DEBUG format, ##arg);	\ | 
 | 25 | 	} while (0) | 
 | 26 |  | 
 | 27 | /* | 
 | 28 |  * Send a Synaptics style sliced query command | 
 | 29 |  */ | 
 | 30 | static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, | 
 | 31 | 				unsigned char *param) | 
 | 32 | { | 
 | 33 | 	if (psmouse_sliced_command(psmouse, c) || | 
 | 34 | 	    ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) { | 
 | 35 | 		pr_err("elantech.c: synaptics_send_cmd query 0x%02x failed.\n", c); | 
 | 36 | 		return -1; | 
 | 37 | 	} | 
 | 38 |  | 
 | 39 | 	return 0; | 
 | 40 | } | 
 | 41 |  | 
 | 42 | /* | 
 | 43 |  * A retrying version of ps2_command | 
 | 44 |  */ | 
 | 45 | static int elantech_ps2_command(struct psmouse *psmouse, | 
 | 46 | 				unsigned char *param, int command) | 
 | 47 | { | 
 | 48 | 	struct ps2dev *ps2dev = &psmouse->ps2dev; | 
 | 49 | 	struct elantech_data *etd = psmouse->private; | 
 | 50 | 	int rc; | 
 | 51 | 	int tries = ETP_PS2_COMMAND_TRIES; | 
 | 52 |  | 
 | 53 | 	do { | 
 | 54 | 		rc = ps2_command(ps2dev, param, command); | 
 | 55 | 		if (rc == 0) | 
 | 56 | 			break; | 
 | 57 | 		tries--; | 
 | 58 | 		elantech_debug("elantech.c: retrying ps2 command 0x%02x (%d).\n", | 
 | 59 | 			command, tries); | 
 | 60 | 		msleep(ETP_PS2_COMMAND_DELAY); | 
 | 61 | 	} while (tries > 0); | 
 | 62 |  | 
 | 63 | 	if (rc) | 
 | 64 | 		pr_err("elantech.c: ps2 command 0x%02x failed.\n", command); | 
 | 65 |  | 
 | 66 | 	return rc; | 
 | 67 | } | 
 | 68 |  | 
 | 69 | /* | 
 | 70 |  * Send an Elantech style special command to read a value from a register | 
 | 71 |  */ | 
 | 72 | static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg, | 
 | 73 | 				unsigned char *val) | 
 | 74 | { | 
 | 75 | 	struct elantech_data *etd = psmouse->private; | 
 | 76 | 	unsigned char param[3]; | 
 | 77 | 	int rc = 0; | 
 | 78 |  | 
 | 79 | 	if (reg < 0x10 || reg > 0x26) | 
 | 80 | 		return -1; | 
 | 81 |  | 
 | 82 | 	if (reg > 0x11 && reg < 0x20) | 
 | 83 | 		return -1; | 
 | 84 |  | 
 | 85 | 	switch (etd->hw_version) { | 
 | 86 | 	case 1: | 
 | 87 | 		if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) || | 
 | 88 | 		    psmouse_sliced_command(psmouse, reg) || | 
 | 89 | 		    ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) { | 
 | 90 | 			rc = -1; | 
 | 91 | 		} | 
 | 92 | 		break; | 
 | 93 |  | 
 | 94 | 	case 2: | 
 | 95 | 		if (elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) || | 
 | 96 | 		    elantech_ps2_command(psmouse,  NULL, ETP_REGISTER_READ) || | 
 | 97 | 		    elantech_ps2_command(psmouse,  NULL, ETP_PS2_CUSTOM_COMMAND) || | 
 | 98 | 		    elantech_ps2_command(psmouse,  NULL, reg) || | 
 | 99 | 		    elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) { | 
 | 100 | 			rc = -1; | 
 | 101 | 		} | 
 | 102 | 		break; | 
 | 103 | 	} | 
 | 104 |  | 
 | 105 | 	if (rc) | 
 | 106 | 		pr_err("elantech.c: failed to read register 0x%02x.\n", reg); | 
 | 107 | 	else | 
 | 108 | 		*val = param[0]; | 
 | 109 |  | 
 | 110 | 	return rc; | 
 | 111 | } | 
 | 112 |  | 
 | 113 | /* | 
 | 114 |  * Send an Elantech style special command to write a register with a value | 
 | 115 |  */ | 
 | 116 | static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg, | 
 | 117 | 				unsigned char val) | 
 | 118 | { | 
 | 119 | 	struct elantech_data *etd = psmouse->private; | 
 | 120 | 	int rc = 0; | 
 | 121 |  | 
 | 122 | 	if (reg < 0x10 || reg > 0x26) | 
 | 123 | 		return -1; | 
 | 124 |  | 
 | 125 | 	if (reg > 0x11 && reg < 0x20) | 
 | 126 | 		return -1; | 
 | 127 |  | 
 | 128 | 	switch (etd->hw_version) { | 
 | 129 | 	case 1: | 
 | 130 | 		if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) || | 
 | 131 | 		    psmouse_sliced_command(psmouse, reg) || | 
 | 132 | 		    psmouse_sliced_command(psmouse, val) || | 
 | 133 | 		    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) { | 
 | 134 | 			rc = -1; | 
 | 135 | 		} | 
 | 136 | 		break; | 
 | 137 |  | 
 | 138 | 	case 2: | 
 | 139 | 		if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) || | 
 | 140 | 		    elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) || | 
 | 141 | 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) || | 
 | 142 | 		    elantech_ps2_command(psmouse, NULL, reg) || | 
 | 143 | 		    elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) || | 
 | 144 | 		    elantech_ps2_command(psmouse, NULL, val) || | 
 | 145 | 		    elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) { | 
 | 146 | 			rc = -1; | 
 | 147 | 		} | 
 | 148 | 		break; | 
 | 149 | 	} | 
 | 150 |  | 
 | 151 | 	if (rc) | 
 | 152 | 		pr_err("elantech.c: failed to write register 0x%02x with value 0x%02x.\n", | 
 | 153 | 			reg, val); | 
 | 154 |  | 
 | 155 | 	return rc; | 
 | 156 | } | 
 | 157 |  | 
 | 158 | /* | 
 | 159 |  * Dump a complete mouse movement packet to the syslog | 
 | 160 |  */ | 
 | 161 | static void elantech_packet_dump(unsigned char *packet, int size) | 
 | 162 | { | 
 | 163 | 	int	i; | 
 | 164 |  | 
 | 165 | 	printk(KERN_DEBUG "elantech.c: PS/2 packet ["); | 
 | 166 | 	for (i = 0; i < size; i++) | 
 | 167 | 		printk("%s0x%02x ", (i) ? ", " : " ", packet[i]); | 
 | 168 | 	printk("]\n"); | 
 | 169 | } | 
 | 170 |  | 
 | 171 | /* | 
 | 172 |  * Interpret complete data packets and report absolute mode input events for | 
 | 173 |  * hardware version 1. (4 byte packets) | 
 | 174 |  */ | 
 | 175 | static void elantech_report_absolute_v1(struct psmouse *psmouse) | 
 | 176 | { | 
 | 177 | 	struct input_dev *dev = psmouse->dev; | 
 | 178 | 	struct elantech_data *etd = psmouse->private; | 
 | 179 | 	unsigned char *packet = psmouse->packet; | 
 | 180 | 	int fingers; | 
| Arjan Opmeer | 3f8c0df | 2009-04-18 19:05:40 -0700 | [diff] [blame] | 181 | 	static int old_fingers; | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 182 |  | 
 | 183 | 	if (etd->fw_version_maj == 0x01) { | 
 | 184 | 		/* byte 0:  D   U  p1  p2   1  p3   R   L | 
 | 185 | 		   byte 1:  f   0  th  tw  x9  x8  y9  y8 */ | 
 | 186 | 		fingers = ((packet[1] & 0x80) >> 7) + | 
 | 187 | 				((packet[1] & 0x30) >> 4); | 
 | 188 | 	} else { | 
 | 189 | 		/* byte 0: n1  n0  p2  p1   1  p3   R   L | 
 | 190 | 		   byte 1:  0   0   0   0  x9  x8  y9  y8 */ | 
 | 191 | 		fingers = (packet[0] & 0xc0) >> 6; | 
 | 192 | 	} | 
 | 193 |  | 
| Arjan Opmeer | 3f8c0df | 2009-04-18 19:05:40 -0700 | [diff] [blame] | 194 | 	if (etd->jumpy_cursor) { | 
 | 195 | 		/* Discard packets that are likely to have bogus coordinates */ | 
 | 196 | 		if (fingers > old_fingers) { | 
 | 197 | 			elantech_debug("elantech.c: discarding packet\n"); | 
 | 198 | 			goto discard_packet_v1; | 
 | 199 | 		} | 
 | 200 | 	} | 
 | 201 |  | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 202 | 	input_report_key(dev, BTN_TOUCH, fingers != 0); | 
 | 203 |  | 
 | 204 | 	/* byte 2: x7  x6  x5  x4  x3  x2  x1  x0 | 
 | 205 | 	   byte 3: y7  y6  y5  y4  y3  y2  y1  y0 */ | 
 | 206 | 	if (fingers) { | 
 | 207 | 		input_report_abs(dev, ABS_X, | 
 | 208 | 			((packet[1] & 0x0c) << 6) | packet[2]); | 
 | 209 | 		input_report_abs(dev, ABS_Y, ETP_YMAX_V1 - | 
 | 210 | 			(((packet[1] & 0x03) << 8) | packet[3])); | 
 | 211 | 	} | 
 | 212 |  | 
 | 213 | 	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1); | 
 | 214 | 	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2); | 
 | 215 | 	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3); | 
 | 216 | 	input_report_key(dev, BTN_LEFT, packet[0] & 0x01); | 
 | 217 | 	input_report_key(dev, BTN_RIGHT, packet[0] & 0x02); | 
 | 218 |  | 
 | 219 | 	if ((etd->fw_version_maj == 0x01) && | 
 | 220 | 	    (etd->capabilities & ETP_CAP_HAS_ROCKER)) { | 
 | 221 | 		/* rocker up */ | 
 | 222 | 		input_report_key(dev, BTN_FORWARD, packet[0] & 0x40); | 
 | 223 | 		/* rocker down */ | 
 | 224 | 		input_report_key(dev, BTN_BACK, packet[0] & 0x80); | 
 | 225 | 	} | 
 | 226 |  | 
 | 227 | 	input_sync(dev); | 
| Arjan Opmeer | 3f8c0df | 2009-04-18 19:05:40 -0700 | [diff] [blame] | 228 |  | 
 | 229 |  discard_packet_v1: | 
 | 230 | 	old_fingers = fingers; | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 231 | } | 
 | 232 |  | 
 | 233 | /* | 
 | 234 |  * Interpret complete data packets and report absolute mode input events for | 
 | 235 |  * hardware version 2. (6 byte packets) | 
 | 236 |  */ | 
 | 237 | static void elantech_report_absolute_v2(struct psmouse *psmouse) | 
 | 238 | { | 
 | 239 | 	struct input_dev *dev = psmouse->dev; | 
 | 240 | 	unsigned char *packet = psmouse->packet; | 
 | 241 | 	int fingers, x1, y1, x2, y2; | 
 | 242 |  | 
 | 243 | 	/* byte 0: n1  n0   .   .   .   .   R   L */ | 
 | 244 | 	fingers = (packet[0] & 0xc0) >> 6; | 
 | 245 | 	input_report_key(dev, BTN_TOUCH, fingers != 0); | 
 | 246 |  | 
 | 247 | 	switch (fingers) { | 
 | 248 | 	case 1: | 
 | 249 | 		/* byte 1: x15 x14 x13 x12 x11 x10 x9  x8 | 
 | 250 | 		   byte 2: x7  x6  x5  x4  x4  x2  x1  x0 */ | 
 | 251 | 		input_report_abs(dev, ABS_X, (packet[1] << 8) | packet[2]); | 
 | 252 | 		/* byte 4: y15 y14 y13 y12 y11 y10 y8  y8 | 
 | 253 | 		   byte 5: y7  y6  y5  y4  y3  y2  y1  y0 */ | 
 | 254 | 		input_report_abs(dev, ABS_Y, ETP_YMAX_V2 - | 
 | 255 | 			((packet[4] << 8) | packet[5])); | 
 | 256 | 		break; | 
 | 257 |  | 
 | 258 | 	case 2: | 
 | 259 | 		/* The coordinate of each finger is reported separately with | 
 | 260 | 		   a lower resolution for two finger touches */ | 
 | 261 | 		/* byte 0:  .   .  ay8 ax8  .   .   .   . | 
 | 262 | 		   byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0 */ | 
 | 263 | 		x1 = ((packet[0] & 0x10) << 4) | packet[1]; | 
 | 264 | 		/* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */ | 
 | 265 | 		y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]); | 
 | 266 | 		/* byte 3:  .   .  by8 bx8  .   .   .   . | 
 | 267 | 		   byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0 */ | 
 | 268 | 		x2 = ((packet[3] & 0x10) << 4) | packet[4]; | 
 | 269 | 		/* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */ | 
 | 270 | 		y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]); | 
 | 271 | 		/* For compatibility with the X Synaptics driver scale up one | 
 | 272 | 		   coordinate and report as ordinary mouse movent */ | 
 | 273 | 		input_report_abs(dev, ABS_X, x1 << 2); | 
 | 274 | 		input_report_abs(dev, ABS_Y, y1 << 2); | 
 | 275 | 		/* For compatibility with the proprietary X Elantech driver | 
 | 276 | 		   report both coordinates as hat coordinates */ | 
 | 277 | 		input_report_abs(dev, ABS_HAT0X, x1); | 
 | 278 | 		input_report_abs(dev, ABS_HAT0Y, y1); | 
 | 279 | 		input_report_abs(dev, ABS_HAT1X, x2); | 
 | 280 | 		input_report_abs(dev, ABS_HAT1Y, y2); | 
 | 281 | 		break; | 
 | 282 | 	} | 
 | 283 |  | 
 | 284 | 	input_report_key(dev, BTN_TOOL_FINGER, fingers == 1); | 
 | 285 | 	input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2); | 
 | 286 | 	input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3); | 
 | 287 | 	input_report_key(dev, BTN_LEFT, packet[0] & 0x01); | 
 | 288 | 	input_report_key(dev, BTN_RIGHT, packet[0] & 0x02); | 
 | 289 |  | 
 | 290 | 	input_sync(dev); | 
 | 291 | } | 
 | 292 |  | 
 | 293 | static int elantech_check_parity_v1(struct psmouse *psmouse) | 
 | 294 | { | 
 | 295 | 	struct elantech_data *etd = psmouse->private; | 
 | 296 | 	unsigned char *packet = psmouse->packet; | 
 | 297 | 	unsigned char p1, p2, p3; | 
 | 298 |  | 
 | 299 | 	/* Parity bits are placed differently */ | 
 | 300 | 	if (etd->fw_version_maj == 0x01) { | 
 | 301 | 		/* byte 0:  D   U  p1  p2   1  p3   R   L */ | 
 | 302 | 		p1 = (packet[0] & 0x20) >> 5; | 
 | 303 | 		p2 = (packet[0] & 0x10) >> 4; | 
 | 304 | 	} else { | 
 | 305 | 		/* byte 0: n1  n0  p2  p1   1  p3   R   L */ | 
 | 306 | 		p1 = (packet[0] & 0x10) >> 4; | 
 | 307 | 		p2 = (packet[0] & 0x20) >> 5; | 
 | 308 | 	} | 
 | 309 |  | 
 | 310 | 	p3 = (packet[0] & 0x04) >> 2; | 
 | 311 |  | 
 | 312 | 	return etd->parity[packet[1]] == p1 && | 
 | 313 | 	       etd->parity[packet[2]] == p2 && | 
 | 314 | 	       etd->parity[packet[3]] == p3; | 
 | 315 | } | 
 | 316 |  | 
 | 317 | /* | 
 | 318 |  * Process byte stream from mouse and handle complete packets | 
 | 319 |  */ | 
 | 320 | static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse) | 
 | 321 | { | 
 | 322 | 	struct elantech_data *etd = psmouse->private; | 
 | 323 |  | 
 | 324 | 	if (psmouse->pktcnt < psmouse->pktsize) | 
 | 325 | 		return PSMOUSE_GOOD_DATA; | 
 | 326 |  | 
 | 327 | 	if (etd->debug > 1) | 
 | 328 | 		elantech_packet_dump(psmouse->packet, psmouse->pktsize); | 
 | 329 |  | 
 | 330 | 	switch (etd->hw_version) { | 
 | 331 | 	case 1: | 
 | 332 | 		if (etd->paritycheck && !elantech_check_parity_v1(psmouse)) | 
 | 333 | 			return PSMOUSE_BAD_DATA; | 
 | 334 |  | 
 | 335 | 		elantech_report_absolute_v1(psmouse); | 
 | 336 | 		break; | 
 | 337 |  | 
 | 338 | 	case 2: | 
 | 339 | 		/* We don't know how to check parity in protocol v2 */ | 
 | 340 | 		elantech_report_absolute_v2(psmouse); | 
 | 341 | 		break; | 
 | 342 | 	} | 
 | 343 |  | 
 | 344 | 	return PSMOUSE_FULL_PACKET; | 
 | 345 | } | 
 | 346 |  | 
 | 347 | /* | 
 | 348 |  * Put the touchpad into absolute mode | 
 | 349 |  */ | 
 | 350 | static int elantech_set_absolute_mode(struct psmouse *psmouse) | 
 | 351 | { | 
 | 352 | 	struct elantech_data *etd = psmouse->private; | 
 | 353 | 	unsigned char val; | 
 | 354 | 	int tries = ETP_READ_BACK_TRIES; | 
 | 355 | 	int rc = 0; | 
 | 356 |  | 
 | 357 | 	switch (etd->hw_version) { | 
 | 358 | 	case 1: | 
 | 359 | 		etd->reg_10 = 0x16; | 
 | 360 | 		etd->reg_11 = 0x8f; | 
 | 361 | 		if (elantech_write_reg(psmouse, 0x10, etd->reg_10) || | 
 | 362 | 		    elantech_write_reg(psmouse, 0x11, etd->reg_11)) { | 
 | 363 | 			rc = -1; | 
 | 364 | 		} | 
 | 365 | 		break; | 
 | 366 |  | 
 | 367 | 	case 2: | 
 | 368 | 					/* Windows driver values */ | 
 | 369 | 		etd->reg_10 = 0x54; | 
 | 370 | 		etd->reg_11 = 0x88;	/* 0x8a */ | 
 | 371 | 		etd->reg_21 = 0x60;	/* 0x00 */ | 
 | 372 | 		if (elantech_write_reg(psmouse, 0x10, etd->reg_10) || | 
 | 373 | 		    elantech_write_reg(psmouse, 0x11, etd->reg_11) || | 
 | 374 | 		    elantech_write_reg(psmouse, 0x21, etd->reg_21)) { | 
 | 375 | 			rc = -1; | 
 | 376 | 			break; | 
 | 377 | 		} | 
| Arjan Opmeer | b2546df | 2009-04-18 19:10:17 -0700 | [diff] [blame] | 378 | 	} | 
 | 379 |  | 
 | 380 | 	if (rc == 0) { | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 381 | 		/* | 
| Arjan Opmeer | b2546df | 2009-04-18 19:10:17 -0700 | [diff] [blame] | 382 | 		 * Read back reg 0x10. For hardware version 1 we must make | 
 | 383 | 		 * sure the absolute mode bit is set. For hardware version 2 | 
 | 384 | 		 * the touchpad is probably initalising and not ready until | 
 | 385 | 		 * we read back the value we just wrote. | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 386 | 		 */ | 
 | 387 | 		do { | 
 | 388 | 			rc = elantech_read_reg(psmouse, 0x10, &val); | 
 | 389 | 			if (rc == 0) | 
 | 390 | 				break; | 
 | 391 | 			tries--; | 
 | 392 | 			elantech_debug("elantech.c: retrying read (%d).\n", | 
| Arjan Opmeer | b2546df | 2009-04-18 19:10:17 -0700 | [diff] [blame] | 393 | 					tries); | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 394 | 			msleep(ETP_READ_BACK_DELAY); | 
 | 395 | 		} while (tries > 0); | 
| Arjan Opmeer | b2546df | 2009-04-18 19:10:17 -0700 | [diff] [blame] | 396 |  | 
 | 397 | 		if (rc) { | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 398 | 			pr_err("elantech.c: failed to read back register 0x10.\n"); | 
| Arjan Opmeer | b2546df | 2009-04-18 19:10:17 -0700 | [diff] [blame] | 399 | 		} else if (etd->hw_version == 1 && | 
 | 400 | 			   !(val & ETP_R10_ABSOLUTE_MODE)) { | 
 | 401 | 			pr_err("elantech.c: touchpad refuses " | 
 | 402 | 				"to switch to absolute mode.\n"); | 
 | 403 | 			rc = -1; | 
 | 404 | 		} | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 405 | 	} | 
 | 406 |  | 
 | 407 | 	if (rc) | 
 | 408 | 		pr_err("elantech.c: failed to initialise registers.\n"); | 
 | 409 |  | 
 | 410 | 	return rc; | 
 | 411 | } | 
 | 412 |  | 
 | 413 | /* | 
 | 414 |  * Set the appropriate event bits for the input subsystem | 
 | 415 |  */ | 
 | 416 | static void elantech_set_input_params(struct psmouse *psmouse) | 
 | 417 | { | 
 | 418 | 	struct input_dev *dev = psmouse->dev; | 
 | 419 | 	struct elantech_data *etd = psmouse->private; | 
 | 420 |  | 
 | 421 | 	__set_bit(EV_KEY, dev->evbit); | 
 | 422 | 	__set_bit(EV_ABS, dev->evbit); | 
| Dmitry Torokhov | c7a1f3c | 2009-11-16 22:12:21 -0800 | [diff] [blame] | 423 | 	__clear_bit(EV_REL, dev->evbit); | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 424 |  | 
 | 425 | 	__set_bit(BTN_LEFT, dev->keybit); | 
 | 426 | 	__set_bit(BTN_RIGHT, dev->keybit); | 
 | 427 |  | 
 | 428 | 	__set_bit(BTN_TOUCH, dev->keybit); | 
 | 429 | 	__set_bit(BTN_TOOL_FINGER, dev->keybit); | 
 | 430 | 	__set_bit(BTN_TOOL_DOUBLETAP, dev->keybit); | 
 | 431 | 	__set_bit(BTN_TOOL_TRIPLETAP, dev->keybit); | 
 | 432 |  | 
 | 433 | 	switch (etd->hw_version) { | 
 | 434 | 	case 1: | 
 | 435 | 		/* Rocker button */ | 
 | 436 | 		if ((etd->fw_version_maj == 0x01) && | 
 | 437 | 		    (etd->capabilities & ETP_CAP_HAS_ROCKER)) { | 
 | 438 | 			__set_bit(BTN_FORWARD, dev->keybit); | 
 | 439 | 			__set_bit(BTN_BACK, dev->keybit); | 
 | 440 | 		} | 
 | 441 | 		input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0); | 
 | 442 | 		input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0); | 
 | 443 | 		break; | 
 | 444 |  | 
 | 445 | 	case 2: | 
 | 446 | 		input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0); | 
 | 447 | 		input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0); | 
 | 448 | 		input_set_abs_params(dev, ABS_HAT0X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0); | 
 | 449 | 		input_set_abs_params(dev, ABS_HAT0Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0); | 
 | 450 | 		input_set_abs_params(dev, ABS_HAT1X, ETP_2FT_XMIN, ETP_2FT_XMAX, 0, 0); | 
 | 451 | 		input_set_abs_params(dev, ABS_HAT1Y, ETP_2FT_YMIN, ETP_2FT_YMAX, 0, 0); | 
 | 452 | 		break; | 
 | 453 | 	} | 
 | 454 | } | 
 | 455 |  | 
 | 456 | struct elantech_attr_data { | 
 | 457 | 	size_t		field_offset; | 
 | 458 | 	unsigned char	reg; | 
 | 459 | }; | 
 | 460 |  | 
 | 461 | /* | 
 | 462 |  * Display a register value by reading a sysfs entry | 
 | 463 |  */ | 
 | 464 | static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data, | 
 | 465 | 					char *buf) | 
 | 466 | { | 
 | 467 | 	struct elantech_data *etd = psmouse->private; | 
 | 468 | 	struct elantech_attr_data *attr = data; | 
 | 469 | 	unsigned char *reg = (unsigned char *) etd + attr->field_offset; | 
 | 470 | 	int rc = 0; | 
 | 471 |  | 
 | 472 | 	if (attr->reg) | 
 | 473 | 		rc = elantech_read_reg(psmouse, attr->reg, reg); | 
 | 474 |  | 
 | 475 | 	return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg); | 
 | 476 | } | 
 | 477 |  | 
 | 478 | /* | 
 | 479 |  * Write a register value by writing a sysfs entry | 
 | 480 |  */ | 
 | 481 | static ssize_t elantech_set_int_attr(struct psmouse *psmouse, | 
 | 482 | 				     void *data, const char *buf, size_t count) | 
 | 483 | { | 
 | 484 | 	struct elantech_data *etd = psmouse->private; | 
 | 485 | 	struct elantech_attr_data *attr = data; | 
 | 486 | 	unsigned char *reg = (unsigned char *) etd + attr->field_offset; | 
 | 487 | 	unsigned long value; | 
 | 488 | 	int err; | 
 | 489 |  | 
 | 490 | 	err = strict_strtoul(buf, 16, &value); | 
 | 491 | 	if (err) | 
 | 492 | 		return err; | 
 | 493 |  | 
 | 494 | 	if (value > 0xff) | 
 | 495 | 		return -EINVAL; | 
 | 496 |  | 
 | 497 | 	/* Do we need to preserve some bits for version 2 hardware too? */ | 
 | 498 | 	if (etd->hw_version == 1) { | 
 | 499 | 		if (attr->reg == 0x10) | 
 | 500 | 			/* Force absolute mode always on */ | 
 | 501 | 			value |= ETP_R10_ABSOLUTE_MODE; | 
 | 502 | 		else if (attr->reg == 0x11) | 
 | 503 | 			/* Force 4 byte mode always on */ | 
 | 504 | 			value |= ETP_R11_4_BYTE_MODE; | 
 | 505 | 	} | 
 | 506 |  | 
 | 507 | 	if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0) | 
 | 508 | 		*reg = value; | 
 | 509 |  | 
 | 510 | 	return count; | 
 | 511 | } | 
 | 512 |  | 
 | 513 | #define ELANTECH_INT_ATTR(_name, _register)				\ | 
 | 514 | 	static struct elantech_attr_data elantech_attr_##_name = {	\ | 
 | 515 | 		.field_offset = offsetof(struct elantech_data, _name),	\ | 
 | 516 | 		.reg = _register,					\ | 
 | 517 | 	};								\ | 
 | 518 | 	PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,			\ | 
 | 519 | 			    &elantech_attr_##_name,			\ | 
 | 520 | 			    elantech_show_int_attr,			\ | 
 | 521 | 			    elantech_set_int_attr) | 
 | 522 |  | 
 | 523 | ELANTECH_INT_ATTR(reg_10, 0x10); | 
 | 524 | ELANTECH_INT_ATTR(reg_11, 0x11); | 
 | 525 | ELANTECH_INT_ATTR(reg_20, 0x20); | 
 | 526 | ELANTECH_INT_ATTR(reg_21, 0x21); | 
 | 527 | ELANTECH_INT_ATTR(reg_22, 0x22); | 
 | 528 | ELANTECH_INT_ATTR(reg_23, 0x23); | 
 | 529 | ELANTECH_INT_ATTR(reg_24, 0x24); | 
 | 530 | ELANTECH_INT_ATTR(reg_25, 0x25); | 
 | 531 | ELANTECH_INT_ATTR(reg_26, 0x26); | 
 | 532 | ELANTECH_INT_ATTR(debug, 0); | 
 | 533 | ELANTECH_INT_ATTR(paritycheck, 0); | 
 | 534 |  | 
 | 535 | static struct attribute *elantech_attrs[] = { | 
 | 536 | 	&psmouse_attr_reg_10.dattr.attr, | 
 | 537 | 	&psmouse_attr_reg_11.dattr.attr, | 
 | 538 | 	&psmouse_attr_reg_20.dattr.attr, | 
 | 539 | 	&psmouse_attr_reg_21.dattr.attr, | 
 | 540 | 	&psmouse_attr_reg_22.dattr.attr, | 
 | 541 | 	&psmouse_attr_reg_23.dattr.attr, | 
 | 542 | 	&psmouse_attr_reg_24.dattr.attr, | 
 | 543 | 	&psmouse_attr_reg_25.dattr.attr, | 
 | 544 | 	&psmouse_attr_reg_26.dattr.attr, | 
 | 545 | 	&psmouse_attr_debug.dattr.attr, | 
 | 546 | 	&psmouse_attr_paritycheck.dattr.attr, | 
 | 547 | 	NULL | 
 | 548 | }; | 
 | 549 |  | 
 | 550 | static struct attribute_group elantech_attr_group = { | 
 | 551 | 	.attrs = elantech_attrs, | 
 | 552 | }; | 
 | 553 |  | 
 | 554 | /* | 
 | 555 |  * Use magic knock to detect Elantech touchpad | 
 | 556 |  */ | 
| Dmitry Torokhov | b7802c5 | 2009-09-09 19:13:20 -0700 | [diff] [blame] | 557 | int elantech_detect(struct psmouse *psmouse, bool set_properties) | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 558 | { | 
 | 559 | 	struct ps2dev *ps2dev = &psmouse->ps2dev; | 
 | 560 | 	unsigned char param[3]; | 
 | 561 |  | 
 | 562 | 	ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS); | 
 | 563 |  | 
 | 564 | 	if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_DISABLE) || | 
 | 565 | 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) || | 
 | 566 | 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) || | 
 | 567 | 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) || | 
 | 568 | 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) { | 
| Arjan Opmeer | 9ab7b25 | 2009-02-28 13:52:40 -0800 | [diff] [blame] | 569 | 		pr_debug("elantech.c: sending Elantech magic knock failed.\n"); | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 570 | 		return -1; | 
 | 571 | 	} | 
 | 572 |  | 
 | 573 | 	/* | 
 | 574 | 	 * Report this in case there are Elantech models that use a different | 
 | 575 | 	 * set of magic numbers | 
 | 576 | 	 */ | 
 | 577 | 	if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) { | 
| Arjan Opmeer | 9ab7b25 | 2009-02-28 13:52:40 -0800 | [diff] [blame] | 578 | 		pr_debug("elantech.c: " | 
 | 579 | 			 "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n", | 
 | 580 | 			 param[0], param[1], param[2]); | 
 | 581 | 		return -1; | 
 | 582 | 	} | 
 | 583 |  | 
 | 584 | 	/* | 
 | 585 | 	 * Query touchpad's firmware version and see if it reports known | 
 | 586 | 	 * value to avoid mis-detection. Logitech mice are known to respond | 
 | 587 | 	 * to Elantech magic knock and there might be more. | 
 | 588 | 	 */ | 
 | 589 | 	if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) { | 
 | 590 | 		pr_debug("elantech.c: failed to query firmware version.\n"); | 
 | 591 | 		return -1; | 
 | 592 | 	} | 
 | 593 |  | 
 | 594 | 	pr_debug("elantech.c: Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n", | 
 | 595 | 		 param[0], param[1], param[2]); | 
 | 596 |  | 
 | 597 | 	if (param[0] == 0 || param[1] != 0) { | 
 | 598 | 		pr_debug("elantech.c: Probably not a real Elantech touchpad. Aborting.\n"); | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 599 | 		return -1; | 
 | 600 | 	} | 
 | 601 |  | 
 | 602 | 	if (set_properties) { | 
 | 603 | 		psmouse->vendor = "Elantech"; | 
 | 604 | 		psmouse->name = "Touchpad"; | 
 | 605 | 	} | 
 | 606 |  | 
 | 607 | 	return 0; | 
 | 608 | } | 
 | 609 |  | 
 | 610 | /* | 
 | 611 |  * Clean up sysfs entries when disconnecting | 
 | 612 |  */ | 
 | 613 | static void elantech_disconnect(struct psmouse *psmouse) | 
 | 614 | { | 
 | 615 | 	sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, | 
 | 616 | 			   &elantech_attr_group); | 
 | 617 | 	kfree(psmouse->private); | 
 | 618 | 	psmouse->private = NULL; | 
 | 619 | } | 
 | 620 |  | 
 | 621 | /* | 
 | 622 |  * Put the touchpad back into absolute mode when reconnecting | 
 | 623 |  */ | 
 | 624 | static int elantech_reconnect(struct psmouse *psmouse) | 
 | 625 | { | 
 | 626 | 	if (elantech_detect(psmouse, 0)) | 
 | 627 | 		return -1; | 
 | 628 |  | 
 | 629 | 	if (elantech_set_absolute_mode(psmouse)) { | 
 | 630 | 		pr_err("elantech.c: failed to put touchpad back into absolute mode.\n"); | 
 | 631 | 		return -1; | 
 | 632 | 	} | 
 | 633 |  | 
 | 634 | 	return 0; | 
 | 635 | } | 
 | 636 |  | 
 | 637 | /* | 
 | 638 |  * Initialize the touchpad and create sysfs entries | 
 | 639 |  */ | 
 | 640 | int elantech_init(struct psmouse *psmouse) | 
 | 641 | { | 
 | 642 | 	struct elantech_data *etd; | 
 | 643 | 	int i, error; | 
 | 644 | 	unsigned char param[3]; | 
 | 645 |  | 
| Arjan Opmeer | 9ab7b25 | 2009-02-28 13:52:40 -0800 | [diff] [blame] | 646 | 	psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL); | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 647 | 	if (!etd) | 
 | 648 | 		return -1; | 
 | 649 |  | 
 | 650 | 	etd->parity[0] = 1; | 
 | 651 | 	for (i = 1; i < 256; i++) | 
 | 652 | 		etd->parity[i] = etd->parity[i & (i - 1)] ^ 1; | 
 | 653 |  | 
 | 654 | 	/* | 
| Arjan Opmeer | 9ab7b25 | 2009-02-28 13:52:40 -0800 | [diff] [blame] | 655 | 	 * Do the version query again so we can store the result | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 656 | 	 */ | 
 | 657 | 	if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) { | 
 | 658 | 		pr_err("elantech.c: failed to query firmware version.\n"); | 
 | 659 | 		goto init_fail; | 
 | 660 | 	} | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 661 | 	etd->fw_version_maj = param[0]; | 
 | 662 | 	etd->fw_version_min = param[2]; | 
 | 663 |  | 
 | 664 | 	/* | 
 | 665 | 	 * Assume every version greater than this is new EeePC style | 
 | 666 | 	 * hardware with 6 byte packets | 
 | 667 | 	 */ | 
 | 668 | 	if (etd->fw_version_maj >= 0x02 && etd->fw_version_min >= 0x30) { | 
 | 669 | 		etd->hw_version = 2; | 
 | 670 | 		/* For now show extra debug information */ | 
 | 671 | 		etd->debug = 1; | 
 | 672 | 		/* Don't know how to do parity checking for version 2 */ | 
 | 673 | 		etd->paritycheck = 0; | 
 | 674 | 	} else { | 
 | 675 | 		etd->hw_version = 1; | 
 | 676 | 		etd->paritycheck = 1; | 
 | 677 | 	} | 
 | 678 | 	pr_info("elantech.c: assuming hardware version %d, firmware version %d.%d\n", | 
 | 679 | 		etd->hw_version, etd->fw_version_maj, etd->fw_version_min); | 
 | 680 |  | 
 | 681 | 	if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) { | 
 | 682 | 		pr_err("elantech.c: failed to query capabilities.\n"); | 
 | 683 | 		goto init_fail; | 
 | 684 | 	} | 
 | 685 | 	pr_info("elantech.c: Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n", | 
 | 686 | 		param[0], param[1], param[2]); | 
 | 687 | 	etd->capabilities = param[0]; | 
 | 688 |  | 
| Arjan Opmeer | 3f8c0df | 2009-04-18 19:05:40 -0700 | [diff] [blame] | 689 | 	/* | 
 | 690 | 	 * This firmware seems to suffer from misreporting coordinates when | 
 | 691 | 	 * a touch action starts causing the mouse cursor or scrolled page | 
 | 692 | 	 * to jump. Enable a workaround. | 
 | 693 | 	 */ | 
 | 694 | 	if (etd->fw_version_maj == 0x02 && etd->fw_version_min == 0x22) { | 
 | 695 | 		pr_info("elantech.c: firmware version 2.34 detected, " | 
 | 696 | 			"enabling jumpy cursor workaround\n"); | 
 | 697 | 		etd->jumpy_cursor = 1; | 
 | 698 | 	} | 
 | 699 |  | 
| Arjan Opmeer | 2a0bd75 | 2008-10-16 22:10:19 -0400 | [diff] [blame] | 700 | 	if (elantech_set_absolute_mode(psmouse)) { | 
 | 701 | 		pr_err("elantech.c: failed to put touchpad into absolute mode.\n"); | 
 | 702 | 		goto init_fail; | 
 | 703 | 	} | 
 | 704 |  | 
 | 705 | 	elantech_set_input_params(psmouse); | 
 | 706 |  | 
 | 707 | 	error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj, | 
 | 708 | 				   &elantech_attr_group); | 
 | 709 | 	if (error) { | 
 | 710 | 		pr_err("elantech.c: failed to create sysfs attributes, error: %d.\n", | 
 | 711 | 			error); | 
 | 712 | 		goto init_fail; | 
 | 713 | 	} | 
 | 714 |  | 
 | 715 | 	psmouse->protocol_handler = elantech_process_byte; | 
 | 716 | 	psmouse->disconnect = elantech_disconnect; | 
 | 717 | 	psmouse->reconnect = elantech_reconnect; | 
 | 718 | 	psmouse->pktsize = etd->hw_version == 2 ? 6 : 4; | 
 | 719 |  | 
 | 720 | 	return 0; | 
 | 721 |  | 
 | 722 |  init_fail: | 
 | 723 | 	kfree(etd); | 
 | 724 | 	return -1; | 
 | 725 | } |