blob: e8fafe8785a7a6aea33114762d34dc4081df9805 [file] [log] [blame]
Hans de Goede3ace3682014-09-12 17:24:47 -07001/*
2 * Focaltech TouchPad PS/2 mouse driver
3 *
4 * Copyright (c) 2014 Red Hat Inc.
Mathias Gottschlag05be1d02014-12-29 09:26:35 -08005 * Copyright (c) 2014 Mathias Gottschlag <mgottschlag@gmail.com>
Hans de Goede3ace3682014-09-12 17:24:47 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * Red Hat authors:
13 *
14 * Hans de Goede <hdegoede@redhat.com>
15 */
16
Hans de Goede3ace3682014-09-12 17:24:47 -070017
18#include <linux/device.h>
19#include <linux/libps2.h>
Mathias Gottschlag05be1d02014-12-29 09:26:35 -080020#include <linux/input/mt.h>
21#include <linux/serio.h>
22#include <linux/slab.h>
Hans de Goede3ace3682014-09-12 17:24:47 -070023#include "psmouse.h"
Mathias Gottschlag05be1d02014-12-29 09:26:35 -080024#include "focaltech.h"
Hans de Goede3ace3682014-09-12 17:24:47 -070025
26static const char * const focaltech_pnp_ids[] = {
27 "FLT0101",
28 "FLT0102",
29 "FLT0103",
30 NULL
31};
32
Mathias Gottschlag05be1d02014-12-29 09:26:35 -080033/*
34 * Even if the kernel is built without support for Focaltech PS/2 touchpads (or
35 * when the real driver fails to recognize the device), we still have to detect
36 * them in order to avoid further detection attempts confusing the touchpad.
37 * This way it at least works in PS/2 mouse compatibility mode.
38 */
Hans de Goede3ace3682014-09-12 17:24:47 -070039int focaltech_detect(struct psmouse *psmouse, bool set_properties)
40{
41 if (!psmouse_matches_pnp_id(psmouse, focaltech_pnp_ids))
42 return -ENODEV;
43
44 if (set_properties) {
45 psmouse->vendor = "FocalTech";
Mathias Gottschlag05be1d02014-12-29 09:26:35 -080046 psmouse->name = "FocalTech Touchpad";
Hans de Goede3ace3682014-09-12 17:24:47 -070047 }
48
49 return 0;
50}
51
Mathias Gottschlag05be1d02014-12-29 09:26:35 -080052static void focaltech_reset(struct psmouse *psmouse)
Hans de Goede3ace3682014-09-12 17:24:47 -070053{
54 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
55 psmouse_reset(psmouse);
Mathias Gottschlag05be1d02014-12-29 09:26:35 -080056}
57
58#ifdef CONFIG_MOUSE_PS2_FOCALTECH
59
60/*
61 * Packet types - the numbers are not consecutive, so we might be missing
62 * something here.
63 */
64#define FOC_TOUCH 0x3 /* bitmap of active fingers */
65#define FOC_ABS 0x6 /* absolute position of one finger */
66#define FOC_REL 0x9 /* relative position of 1-2 fingers */
67
68#define FOC_MAX_FINGERS 5
69
Mathias Gottschlag05be1d02014-12-29 09:26:35 -080070/*
71 * Current state of a single finger on the touchpad.
72 */
73struct focaltech_finger_state {
74 /* The touchpad has generated a touch event for the finger */
75 bool active;
76
77 /*
78 * The touchpad has sent position data for the finger. The
79 * flag is 0 when the finger is not active, and there is a
80 * time between the first touch event for the finger and the
81 * following absolute position packet for the finger where the
82 * touchpad has declared the finger to be valid, but we do not
83 * have any valid position yet.
84 */
85 bool valid;
86
87 /*
88 * Absolute position (from the bottom left corner) of the
89 * finger.
90 */
91 unsigned int x;
92 unsigned int y;
93};
94
95/*
96 * Description of the current state of the touchpad hardware.
97 */
98struct focaltech_hw_state {
99 /*
100 * The touchpad tracks the positions of the fingers for us,
101 * the array indices correspond to the finger indices returned
102 * in the report packages.
103 */
104 struct focaltech_finger_state fingers[FOC_MAX_FINGERS];
105
106 /* True if the clickpad has been pressed. */
107 bool pressed;
108};
109
110struct focaltech_data {
111 unsigned int x_max, y_max;
112 struct focaltech_hw_state state;
113};
114
115static void focaltech_report_state(struct psmouse *psmouse)
116{
117 struct focaltech_data *priv = psmouse->private;
118 struct focaltech_hw_state *state = &priv->state;
119 struct input_dev *dev = psmouse->dev;
120 int i;
121
122 for (i = 0; i < FOC_MAX_FINGERS; i++) {
123 struct focaltech_finger_state *finger = &state->fingers[i];
124 bool active = finger->active && finger->valid;
125
126 input_mt_slot(dev, i);
127 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
128 if (active) {
129 input_report_abs(dev, ABS_MT_POSITION_X, finger->x);
130 input_report_abs(dev, ABS_MT_POSITION_Y,
Mathias Gottschlag3e984522015-03-07 13:26:31 -0800131 priv->y_max - finger->y);
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800132 }
133 }
134 input_mt_report_pointer_emulation(dev, true);
135
136 input_report_key(psmouse->dev, BTN_LEFT, state->pressed);
137 input_sync(psmouse->dev);
138}
139
140static void focaltech_process_touch_packet(struct psmouse *psmouse,
141 unsigned char *packet)
142{
143 struct focaltech_data *priv = psmouse->private;
144 struct focaltech_hw_state *state = &priv->state;
145 unsigned char fingers = packet[1];
146 int i;
147
148 state->pressed = (packet[0] >> 4) & 1;
149
150 /* the second byte contains a bitmap of all fingers touching the pad */
151 for (i = 0; i < FOC_MAX_FINGERS; i++) {
152 state->fingers[i].active = fingers & 0x1;
153 if (!state->fingers[i].active) {
154 /*
155 * Even when the finger becomes active again, we still
156 * will have to wait for the first valid position.
157 */
158 state->fingers[i].valid = false;
159 }
160 fingers >>= 1;
161 }
162}
163
164static void focaltech_process_abs_packet(struct psmouse *psmouse,
165 unsigned char *packet)
166{
167 struct focaltech_data *priv = psmouse->private;
168 struct focaltech_hw_state *state = &priv->state;
169 unsigned int finger;
170
171 finger = (packet[1] >> 4) - 1;
172 if (finger >= FOC_MAX_FINGERS) {
173 psmouse_err(psmouse, "Invalid finger in abs packet: %d\n",
174 finger);
175 return;
176 }
177
178 state->pressed = (packet[0] >> 4) & 1;
179
180 /*
181 * packet[5] contains some kind of tool size in the most
182 * significant nibble. 0xff is a special value (latching) that
183 * signals a large contact area.
184 */
185 if (packet[5] == 0xff) {
186 state->fingers[finger].valid = false;
187 return;
188 }
189
190 state->fingers[finger].x = ((packet[1] & 0xf) << 8) | packet[2];
191 state->fingers[finger].y = (packet[3] << 8) | packet[4];
192 state->fingers[finger].valid = true;
193}
194
195static void focaltech_process_rel_packet(struct psmouse *psmouse,
196 unsigned char *packet)
197{
198 struct focaltech_data *priv = psmouse->private;
199 struct focaltech_hw_state *state = &priv->state;
200 int finger1, finger2;
201
202 state->pressed = packet[0] >> 7;
203 finger1 = ((packet[0] >> 4) & 0x7) - 1;
204 if (finger1 < FOC_MAX_FINGERS) {
205 state->fingers[finger1].x += (char)packet[1];
206 state->fingers[finger1].y += (char)packet[2];
207 } else {
208 psmouse_err(psmouse, "First finger in rel packet invalid: %d\n",
209 finger1);
210 }
211
212 /*
213 * If there is an odd number of fingers, the last relative
214 * packet only contains one finger. In this case, the second
215 * finger index in the packet is 0 (we subtract 1 in the lines
216 * above to create array indices, so the finger will overflow
217 * and be above FOC_MAX_FINGERS).
218 */
219 finger2 = ((packet[3] >> 4) & 0x7) - 1;
220 if (finger2 < FOC_MAX_FINGERS) {
221 state->fingers[finger2].x += (char)packet[4];
222 state->fingers[finger2].y += (char)packet[5];
223 }
224}
225
226static void focaltech_process_packet(struct psmouse *psmouse)
227{
228 unsigned char *packet = psmouse->packet;
229
230 switch (packet[0] & 0xf) {
231 case FOC_TOUCH:
232 focaltech_process_touch_packet(psmouse, packet);
233 break;
234
235 case FOC_ABS:
236 focaltech_process_abs_packet(psmouse, packet);
237 break;
238
239 case FOC_REL:
240 focaltech_process_rel_packet(psmouse, packet);
241 break;
242
243 default:
244 psmouse_err(psmouse, "Unknown packet type: %02x\n", packet[0]);
245 break;
246 }
247
248 focaltech_report_state(psmouse);
249}
250
251static psmouse_ret_t focaltech_process_byte(struct psmouse *psmouse)
252{
253 if (psmouse->pktcnt >= 6) { /* Full packet received */
254 focaltech_process_packet(psmouse);
255 return PSMOUSE_FULL_PACKET;
256 }
257
258 /*
259 * We might want to do some validation of the data here, but
260 * we do not know the protocol well enough
261 */
262 return PSMOUSE_GOOD_DATA;
263}
264
265static int focaltech_switch_protocol(struct psmouse *psmouse)
266{
267 struct ps2dev *ps2dev = &psmouse->ps2dev;
268 unsigned char param[3];
269
270 param[0] = 0;
271 if (ps2_command(ps2dev, param, 0x10f8))
272 return -EIO;
273
274 if (ps2_command(ps2dev, param, 0x10f8))
275 return -EIO;
276
277 if (ps2_command(ps2dev, param, 0x10f8))
278 return -EIO;
279
280 param[0] = 1;
281 if (ps2_command(ps2dev, param, 0x10f8))
282 return -EIO;
283
284 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
285 return -EIO;
286
287 if (ps2_command(ps2dev, param, PSMOUSE_CMD_ENABLE))
288 return -EIO;
Hans de Goede3ace3682014-09-12 17:24:47 -0700289
290 return 0;
291}
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800292
293static void focaltech_disconnect(struct psmouse *psmouse)
294{
295 focaltech_reset(psmouse);
296 kfree(psmouse->private);
297 psmouse->private = NULL;
298}
299
300static int focaltech_reconnect(struct psmouse *psmouse)
301{
302 int error;
303
304 focaltech_reset(psmouse);
305
306 error = focaltech_switch_protocol(psmouse);
307 if (error) {
308 psmouse_err(psmouse, "Unable to initialize the device\n");
309 return error;
310 }
311
312 return 0;
313}
314
315static void focaltech_set_input_params(struct psmouse *psmouse)
316{
317 struct input_dev *dev = psmouse->dev;
318 struct focaltech_data *priv = psmouse->private;
319
320 /*
321 * Undo part of setup done for us by psmouse core since touchpad
322 * is not a relative device.
323 */
324 __clear_bit(EV_REL, dev->evbit);
325 __clear_bit(REL_X, dev->relbit);
326 __clear_bit(REL_Y, dev->relbit);
327 __clear_bit(BTN_RIGHT, dev->keybit);
328 __clear_bit(BTN_MIDDLE, dev->keybit);
329
330 /*
331 * Now set up our capabilities.
332 */
333 __set_bit(EV_ABS, dev->evbit);
334 input_set_abs_params(dev, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
335 input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
336 input_mt_init_slots(dev, 5, INPUT_MT_POINTER);
337 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
338}
339
340static int focaltech_read_register(struct ps2dev *ps2dev, int reg,
341 unsigned char *param)
342{
343 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
344 return -EIO;
345
346 param[0] = 0;
347 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
348 return -EIO;
349
350 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
351 return -EIO;
352
353 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
354 return -EIO;
355
356 param[0] = reg;
357 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
358 return -EIO;
359
360 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
361 return -EIO;
362
363 return 0;
364}
365
366static int focaltech_read_size(struct psmouse *psmouse)
367{
368 struct ps2dev *ps2dev = &psmouse->ps2dev;
369 struct focaltech_data *priv = psmouse->private;
370 char param[3];
371
372 if (focaltech_read_register(ps2dev, 2, param))
373 return -EIO;
374
375 /* not sure whether this is 100% correct */
376 priv->x_max = (unsigned char)param[1] * 128;
377 priv->y_max = (unsigned char)param[2] * 128;
378
379 return 0;
380}
381int focaltech_init(struct psmouse *psmouse)
382{
383 struct focaltech_data *priv;
384 int error;
385
386 psmouse->private = priv = kzalloc(sizeof(struct focaltech_data),
387 GFP_KERNEL);
388 if (!priv)
389 return -ENOMEM;
390
391 focaltech_reset(psmouse);
392
393 error = focaltech_read_size(psmouse);
394 if (error) {
395 psmouse_err(psmouse,
396 "Unable to read the size of the touchpad\n");
397 goto fail;
398 }
399
400 error = focaltech_switch_protocol(psmouse);
401 if (error) {
402 psmouse_err(psmouse, "Unable to initialize the device\n");
403 goto fail;
404 }
405
406 focaltech_set_input_params(psmouse);
407
408 psmouse->protocol_handler = focaltech_process_byte;
409 psmouse->pktsize = 6;
410 psmouse->disconnect = focaltech_disconnect;
411 psmouse->reconnect = focaltech_reconnect;
412 psmouse->cleanup = focaltech_reset;
413 /* resync is not supported yet */
414 psmouse->resync_time = 0;
415
416 return 0;
417
418fail:
419 focaltech_reset(psmouse);
420 kfree(priv);
421 return error;
422}
423
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800424#else /* CONFIG_MOUSE_PS2_FOCALTECH */
425
426int focaltech_init(struct psmouse *psmouse)
427{
428 focaltech_reset(psmouse);
429
430 return 0;
431}
432
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800433#endif /* CONFIG_MOUSE_PS2_FOCALTECH */