1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Focaltech TouchPad PS/2 mouse driver
5 * Copyright (c) 2014 Red Hat Inc.
6 * Copyright (c) 2014 Mathias Gottschlag <mgottschlag@gmail.com>
10 * Hans de Goede <hdegoede@redhat.com>
14 #include <linux/device.h>
15 #include <linux/libps2.h>
16 #include <linux/input/mt.h>
17 #include <linux/serio.h>
18 #include <linux/slab.h>
20 #include "focaltech.h"
22 static const char * const focaltech_pnp_ids[] = {
30 * Even if the kernel is built without support for Focaltech PS/2 touchpads (or
31 * when the real driver fails to recognize the device), we still have to detect
32 * them in order to avoid further detection attempts confusing the touchpad.
33 * This way it at least works in PS/2 mouse compatibility mode.
35 int focaltech_detect(struct psmouse *psmouse, bool set_properties)
37 if (!psmouse_matches_pnp_id(psmouse, focaltech_pnp_ids))
41 psmouse->vendor = "FocalTech";
42 psmouse->name = "Touchpad";
48 #ifdef CONFIG_MOUSE_PS2_FOCALTECH
51 * Packet types - the numbers are not consecutive, so we might be missing
54 #define FOC_TOUCH 0x3 /* bitmap of active fingers */
55 #define FOC_ABS 0x6 /* absolute position of one finger */
56 #define FOC_REL 0x9 /* relative position of 1-2 fingers */
58 #define FOC_MAX_FINGERS 5
61 * Current state of a single finger on the touchpad.
63 struct focaltech_finger_state {
64 /* The touchpad has generated a touch event for the finger */
68 * The touchpad has sent position data for the finger. The
69 * flag is 0 when the finger is not active, and there is a
70 * time between the first touch event for the finger and the
71 * following absolute position packet for the finger where the
72 * touchpad has declared the finger to be valid, but we do not
73 * have any valid position yet.
78 * Absolute position (from the bottom left corner) of the
86 * Description of the current state of the touchpad hardware.
88 struct focaltech_hw_state {
90 * The touchpad tracks the positions of the fingers for us,
91 * the array indices correspond to the finger indices returned
92 * in the report packages.
94 struct focaltech_finger_state fingers[FOC_MAX_FINGERS];
97 * Finger width 0-7 and 15 for a very big contact area.
98 * 15 value stays until the finger is released.
99 * Width is reported only in absolute packets.
100 * Since hardware reports width only for last touching finger,
101 * there is no need to store width for every specific finger,
102 * so we keep only last value reported.
106 /* True if the clickpad has been pressed. */
110 struct focaltech_data {
111 unsigned int x_max, y_max;
112 struct focaltech_hw_state state;
115 static void focaltech_report_state(struct psmouse *psmouse)
117 struct focaltech_data *priv = psmouse->private;
118 struct focaltech_hw_state *state = &priv->state;
119 struct input_dev *dev = psmouse->dev;
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;
126 input_mt_slot(dev, i);
127 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
129 unsigned int clamped_x, clamped_y;
131 * The touchpad might report invalid data, so we clamp
132 * the resulting values so that we do not confuse
135 clamped_x = clamp(finger->x, 0U, priv->x_max);
136 clamped_y = clamp(finger->y, 0U, priv->y_max);
137 input_report_abs(dev, ABS_MT_POSITION_X, clamped_x);
138 input_report_abs(dev, ABS_MT_POSITION_Y,
139 priv->y_max - clamped_y);
140 input_report_abs(dev, ABS_TOOL_WIDTH, state->width);
143 input_mt_report_pointer_emulation(dev, true);
145 input_report_key(dev, BTN_LEFT, state->pressed);
149 static void focaltech_process_touch_packet(struct psmouse *psmouse,
150 unsigned char *packet)
152 struct focaltech_data *priv = psmouse->private;
153 struct focaltech_hw_state *state = &priv->state;
154 unsigned char fingers = packet[1];
157 state->pressed = (packet[0] >> 4) & 1;
159 /* the second byte contains a bitmap of all fingers touching the pad */
160 for (i = 0; i < FOC_MAX_FINGERS; i++) {
161 state->fingers[i].active = fingers & 0x1;
162 if (!state->fingers[i].active) {
164 * Even when the finger becomes active again, we still
165 * will have to wait for the first valid position.
167 state->fingers[i].valid = false;
173 static void focaltech_process_abs_packet(struct psmouse *psmouse,
174 unsigned char *packet)
176 struct focaltech_data *priv = psmouse->private;
177 struct focaltech_hw_state *state = &priv->state;
180 finger = (packet[1] >> 4) - 1;
181 if (finger >= FOC_MAX_FINGERS) {
182 psmouse_err(psmouse, "Invalid finger in abs packet: %d\n",
187 state->pressed = (packet[0] >> 4) & 1;
189 state->fingers[finger].x = ((packet[1] & 0xf) << 8) | packet[2];
190 state->fingers[finger].y = (packet[3] << 8) | packet[4];
191 state->width = packet[5] >> 4;
192 state->fingers[finger].valid = true;
195 static void focaltech_process_rel_packet(struct psmouse *psmouse,
196 unsigned char *packet)
198 struct focaltech_data *priv = psmouse->private;
199 struct focaltech_hw_state *state = &priv->state;
200 int finger1, finger2;
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];
208 psmouse_err(psmouse, "First finger in rel packet invalid: %d\n",
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).
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];
226 static void focaltech_process_packet(struct psmouse *psmouse)
228 unsigned char *packet = psmouse->packet;
230 switch (packet[0] & 0xf) {
232 focaltech_process_touch_packet(psmouse, packet);
236 focaltech_process_abs_packet(psmouse, packet);
240 focaltech_process_rel_packet(psmouse, packet);
244 psmouse_err(psmouse, "Unknown packet type: %02x\n", packet[0]);
248 focaltech_report_state(psmouse);
251 static psmouse_ret_t focaltech_process_byte(struct psmouse *psmouse)
253 if (psmouse->pktcnt >= 6) { /* Full packet received */
254 focaltech_process_packet(psmouse);
255 return PSMOUSE_FULL_PACKET;
259 * We might want to do some validation of the data here, but
260 * we do not know the protocol well enough
262 return PSMOUSE_GOOD_DATA;
265 static int focaltech_switch_protocol(struct psmouse *psmouse)
267 struct ps2dev *ps2dev = &psmouse->ps2dev;
268 unsigned char param[3];
271 if (ps2_command(ps2dev, param, 0x10f8))
274 if (ps2_command(ps2dev, param, 0x10f8))
277 if (ps2_command(ps2dev, param, 0x10f8))
281 if (ps2_command(ps2dev, param, 0x10f8))
284 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
287 if (ps2_command(ps2dev, param, PSMOUSE_CMD_ENABLE))
293 static void focaltech_reset(struct psmouse *psmouse)
295 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
296 psmouse_reset(psmouse);
299 static void focaltech_disconnect(struct psmouse *psmouse)
301 focaltech_reset(psmouse);
302 kfree(psmouse->private);
303 psmouse->private = NULL;
306 static int focaltech_reconnect(struct psmouse *psmouse)
310 focaltech_reset(psmouse);
312 error = focaltech_switch_protocol(psmouse);
314 psmouse_err(psmouse, "Unable to initialize the device\n");
321 static void focaltech_set_input_params(struct psmouse *psmouse)
323 struct input_dev *dev = psmouse->dev;
324 struct focaltech_data *priv = psmouse->private;
327 * Undo part of setup done for us by psmouse core since touchpad
328 * is not a relative device.
330 __clear_bit(EV_REL, dev->evbit);
331 __clear_bit(REL_X, dev->relbit);
332 __clear_bit(REL_Y, dev->relbit);
333 __clear_bit(BTN_RIGHT, dev->keybit);
334 __clear_bit(BTN_MIDDLE, dev->keybit);
337 * Now set up our capabilities.
339 __set_bit(EV_ABS, dev->evbit);
340 input_set_abs_params(dev, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
341 input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
342 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
343 input_mt_init_slots(dev, 5, INPUT_MT_POINTER);
344 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
347 static int focaltech_read_register(struct ps2dev *ps2dev, int reg,
348 unsigned char *param)
350 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
354 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
357 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
360 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
364 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
367 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
373 static int focaltech_read_size(struct psmouse *psmouse)
375 struct ps2dev *ps2dev = &psmouse->ps2dev;
376 struct focaltech_data *priv = psmouse->private;
379 if (focaltech_read_register(ps2dev, 2, param))
382 /* not sure whether this is 100% correct */
383 priv->x_max = (unsigned char)param[1] * 128;
384 priv->y_max = (unsigned char)param[2] * 128;
389 static void focaltech_set_resolution(struct psmouse *psmouse,
390 unsigned int resolution)
392 /* not supported yet */
395 static void focaltech_set_rate(struct psmouse *psmouse, unsigned int rate)
397 /* not supported yet */
400 static void focaltech_set_scale(struct psmouse *psmouse,
401 enum psmouse_scale scale)
403 /* not supported yet */
406 int focaltech_init(struct psmouse *psmouse)
408 struct focaltech_data *priv;
411 psmouse->private = priv = kzalloc(sizeof(struct focaltech_data),
416 focaltech_reset(psmouse);
418 error = focaltech_read_size(psmouse);
421 "Unable to read the size of the touchpad\n");
425 error = focaltech_switch_protocol(psmouse);
427 psmouse_err(psmouse, "Unable to initialize the device\n");
431 focaltech_set_input_params(psmouse);
433 psmouse->protocol_handler = focaltech_process_byte;
434 psmouse->pktsize = 6;
435 psmouse->disconnect = focaltech_disconnect;
436 psmouse->reconnect = focaltech_reconnect;
437 psmouse->cleanup = focaltech_reset;
438 /* resync is not supported yet */
439 psmouse->resync_time = 0;
441 * rate/resolution/scale changes are not supported yet, and
442 * the generic implementations of these functions seem to
443 * confuse some touchpads
445 psmouse->set_resolution = focaltech_set_resolution;
446 psmouse->set_rate = focaltech_set_rate;
447 psmouse->set_scale = focaltech_set_scale;
452 focaltech_reset(psmouse);
456 #endif /* CONFIG_MOUSE_PS2_FOCALTECH */