2 * Focaltech TouchPad PS/2 mouse driver
4 * Copyright (c) 2014 Red Hat Inc.
5 * Copyright (c) 2014 Mathias Gottschlag <mgottschlag@gmail.com>
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.
14 * Hans de Goede <hdegoede@redhat.com>
18 #include <linux/device.h>
19 #include <linux/libps2.h>
20 #include <linux/input/mt.h>
21 #include <linux/serio.h>
22 #include <linux/slab.h>
24 #include "focaltech.h"
26 static const char * const focaltech_pnp_ids[] = {
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.
39 int focaltech_detect(struct psmouse *psmouse, bool set_properties)
41 if (!psmouse_matches_pnp_id(psmouse, focaltech_pnp_ids))
45 psmouse->vendor = "FocalTech";
46 psmouse->name = "FocalTech Touchpad";
52 static void focaltech_reset(struct psmouse *psmouse)
54 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
55 psmouse_reset(psmouse);
58 #ifdef CONFIG_MOUSE_PS2_FOCALTECH
61 * Packet types - the numbers are not consecutive, so we might be missing
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 */
68 #define FOC_MAX_FINGERS 5
70 #define FOC_MAX_X 2431
71 #define FOC_MAX_Y 1663
74 * Current state of a single finger on the touchpad.
76 struct focaltech_finger_state {
77 /* The touchpad has generated a touch event for the finger */
81 * The touchpad has sent position data for the finger. The
82 * flag is 0 when the finger is not active, and there is a
83 * time between the first touch event for the finger and the
84 * following absolute position packet for the finger where the
85 * touchpad has declared the finger to be valid, but we do not
86 * have any valid position yet.
91 * Absolute position (from the bottom left corner) of the
99 * Description of the current state of the touchpad hardware.
101 struct focaltech_hw_state {
103 * The touchpad tracks the positions of the fingers for us,
104 * the array indices correspond to the finger indices returned
105 * in the report packages.
107 struct focaltech_finger_state fingers[FOC_MAX_FINGERS];
109 /* True if the clickpad has been pressed. */
113 struct focaltech_data {
114 unsigned int x_max, y_max;
115 struct focaltech_hw_state state;
118 static void focaltech_report_state(struct psmouse *psmouse)
120 struct focaltech_data *priv = psmouse->private;
121 struct focaltech_hw_state *state = &priv->state;
122 struct input_dev *dev = psmouse->dev;
125 for (i = 0; i < FOC_MAX_FINGERS; i++) {
126 struct focaltech_finger_state *finger = &state->fingers[i];
127 bool active = finger->active && finger->valid;
129 input_mt_slot(dev, i);
130 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
132 input_report_abs(dev, ABS_MT_POSITION_X, finger->x);
133 input_report_abs(dev, ABS_MT_POSITION_Y,
134 FOC_MAX_Y - finger->y);
137 input_mt_report_pointer_emulation(dev, true);
139 input_report_key(psmouse->dev, BTN_LEFT, state->pressed);
140 input_sync(psmouse->dev);
143 static void focaltech_process_touch_packet(struct psmouse *psmouse,
144 unsigned char *packet)
146 struct focaltech_data *priv = psmouse->private;
147 struct focaltech_hw_state *state = &priv->state;
148 unsigned char fingers = packet[1];
151 state->pressed = (packet[0] >> 4) & 1;
153 /* the second byte contains a bitmap of all fingers touching the pad */
154 for (i = 0; i < FOC_MAX_FINGERS; i++) {
155 state->fingers[i].active = fingers & 0x1;
156 if (!state->fingers[i].active) {
158 * Even when the finger becomes active again, we still
159 * will have to wait for the first valid position.
161 state->fingers[i].valid = false;
167 static void focaltech_process_abs_packet(struct psmouse *psmouse,
168 unsigned char *packet)
170 struct focaltech_data *priv = psmouse->private;
171 struct focaltech_hw_state *state = &priv->state;
174 finger = (packet[1] >> 4) - 1;
175 if (finger >= FOC_MAX_FINGERS) {
176 psmouse_err(psmouse, "Invalid finger in abs packet: %d\n",
181 state->pressed = (packet[0] >> 4) & 1;
184 * packet[5] contains some kind of tool size in the most
185 * significant nibble. 0xff is a special value (latching) that
186 * signals a large contact area.
188 if (packet[5] == 0xff) {
189 state->fingers[finger].valid = false;
193 state->fingers[finger].x = ((packet[1] & 0xf) << 8) | packet[2];
194 state->fingers[finger].y = (packet[3] << 8) | packet[4];
195 state->fingers[finger].valid = true;
198 static void focaltech_process_rel_packet(struct psmouse *psmouse,
199 unsigned char *packet)
201 struct focaltech_data *priv = psmouse->private;
202 struct focaltech_hw_state *state = &priv->state;
203 int finger1, finger2;
205 state->pressed = packet[0] >> 7;
206 finger1 = ((packet[0] >> 4) & 0x7) - 1;
207 if (finger1 < FOC_MAX_FINGERS) {
208 state->fingers[finger1].x += (char)packet[1];
209 state->fingers[finger1].y += (char)packet[2];
211 psmouse_err(psmouse, "First finger in rel packet invalid: %d\n",
216 * If there is an odd number of fingers, the last relative
217 * packet only contains one finger. In this case, the second
218 * finger index in the packet is 0 (we subtract 1 in the lines
219 * above to create array indices, so the finger will overflow
220 * and be above FOC_MAX_FINGERS).
222 finger2 = ((packet[3] >> 4) & 0x7) - 1;
223 if (finger2 < FOC_MAX_FINGERS) {
224 state->fingers[finger2].x += (char)packet[4];
225 state->fingers[finger2].y += (char)packet[5];
229 static void focaltech_process_packet(struct psmouse *psmouse)
231 unsigned char *packet = psmouse->packet;
233 switch (packet[0] & 0xf) {
235 focaltech_process_touch_packet(psmouse, packet);
239 focaltech_process_abs_packet(psmouse, packet);
243 focaltech_process_rel_packet(psmouse, packet);
247 psmouse_err(psmouse, "Unknown packet type: %02x\n", packet[0]);
251 focaltech_report_state(psmouse);
254 static psmouse_ret_t focaltech_process_byte(struct psmouse *psmouse)
256 if (psmouse->pktcnt >= 6) { /* Full packet received */
257 focaltech_process_packet(psmouse);
258 return PSMOUSE_FULL_PACKET;
262 * We might want to do some validation of the data here, but
263 * we do not know the protocol well enough
265 return PSMOUSE_GOOD_DATA;
268 static int focaltech_switch_protocol(struct psmouse *psmouse)
270 struct ps2dev *ps2dev = &psmouse->ps2dev;
271 unsigned char param[3];
274 if (ps2_command(ps2dev, param, 0x10f8))
277 if (ps2_command(ps2dev, param, 0x10f8))
280 if (ps2_command(ps2dev, param, 0x10f8))
284 if (ps2_command(ps2dev, param, 0x10f8))
287 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
290 if (ps2_command(ps2dev, param, PSMOUSE_CMD_ENABLE))
296 static void focaltech_disconnect(struct psmouse *psmouse)
298 focaltech_reset(psmouse);
299 kfree(psmouse->private);
300 psmouse->private = NULL;
303 static int focaltech_reconnect(struct psmouse *psmouse)
307 focaltech_reset(psmouse);
309 error = focaltech_switch_protocol(psmouse);
311 psmouse_err(psmouse, "Unable to initialize the device\n");
318 static void focaltech_set_input_params(struct psmouse *psmouse)
320 struct input_dev *dev = psmouse->dev;
321 struct focaltech_data *priv = psmouse->private;
324 * Undo part of setup done for us by psmouse core since touchpad
325 * is not a relative device.
327 __clear_bit(EV_REL, dev->evbit);
328 __clear_bit(REL_X, dev->relbit);
329 __clear_bit(REL_Y, dev->relbit);
330 __clear_bit(BTN_RIGHT, dev->keybit);
331 __clear_bit(BTN_MIDDLE, dev->keybit);
334 * Now set up our capabilities.
336 __set_bit(EV_ABS, dev->evbit);
337 input_set_abs_params(dev, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
338 input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
339 input_mt_init_slots(dev, 5, INPUT_MT_POINTER);
340 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
343 static int focaltech_read_register(struct ps2dev *ps2dev, int reg,
344 unsigned char *param)
346 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
350 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
353 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
356 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
360 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
363 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
369 static int focaltech_read_size(struct psmouse *psmouse)
371 struct ps2dev *ps2dev = &psmouse->ps2dev;
372 struct focaltech_data *priv = psmouse->private;
375 if (focaltech_read_register(ps2dev, 2, param))
378 /* not sure whether this is 100% correct */
379 priv->x_max = (unsigned char)param[1] * 128;
380 priv->y_max = (unsigned char)param[2] * 128;
384 int focaltech_init(struct psmouse *psmouse)
386 struct focaltech_data *priv;
389 psmouse->private = priv = kzalloc(sizeof(struct focaltech_data),
394 focaltech_reset(psmouse);
396 error = focaltech_read_size(psmouse);
399 "Unable to read the size of the touchpad\n");
403 error = focaltech_switch_protocol(psmouse);
405 psmouse_err(psmouse, "Unable to initialize the device\n");
409 focaltech_set_input_params(psmouse);
411 psmouse->protocol_handler = focaltech_process_byte;
412 psmouse->pktsize = 6;
413 psmouse->disconnect = focaltech_disconnect;
414 psmouse->reconnect = focaltech_reconnect;
415 psmouse->cleanup = focaltech_reset;
416 /* resync is not supported yet */
417 psmouse->resync_time = 0;
422 focaltech_reset(psmouse);
427 #else /* CONFIG_MOUSE_PS2_FOCALTECH */
429 int focaltech_init(struct psmouse *psmouse)
431 focaltech_reset(psmouse);
436 #endif /* CONFIG_MOUSE_PS2_FOCALTECH */