33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/module.h>
36 #include <linux/serio.h>
44 #define ILLEGAL_XY 999999
48 MODULE_PARM_DESC(tpdebug,
"enable debugging, dumping packets to KERN_DEBUG.");
50 static int recalib_delta = 100;
53 "packets containing a delta this large will be discarded, and a "
54 "recalibration may be scheduled.");
56 static int jumpy_delay = 20;
59 "delay (ms) before recal after jumpiness detected");
61 static int spew_delay = 1;
64 "delay (ms) before recal after packet spew detected");
66 static int recal_guard_time;
69 "interval (ms) during which recal will be restarted if packet received");
71 static int post_interrupt_delay = 40;
74 "delay (ms) before recal after recal interrupt detected");
76 static bool autorecal =
true;
80 static char hgpk_mode_name[16];
83 "default hgpk mode: mouse, glidesensor or pentablet");
87 static const char *
const hgpk_mode_names[] = {
93 static int hgpk_mode_from_name(
const char *
buf,
int len)
97 for (i = 0; i <
ARRAY_SIZE(hgpk_mode_names); i++) {
98 const char *
name = hgpk_mode_names[
i];
109 static int approx_half(
int curr,
int prev)
111 int belowhalf, abovehalf;
113 if (curr < 5 || prev < 5)
116 belowhalf = (prev * 8) / 20;
117 abovehalf = (prev * 12) / 20;
119 return belowhalf < curr && curr <= abovehalf;
131 bool do_recal =
false;
137 if (avx > recalib_delta ||
138 (avx > recalib_delta / 2 && ((avx / 4) > priv->
xlast))) {
141 }
else if (approx_half(avx, priv->
xbigj)) {
142 psmouse_warn(psmouse,
"detected secondary %dpx jump in x\n", x);
152 if (avy > recalib_delta ||
153 (avy > recalib_delta / 2 && ((avy / 4) > priv->
ylast))) {
156 }
else if (approx_half(avy, priv->
ybigj)) {
157 psmouse_warn(psmouse,
"detected secondary %dpx jump in y\n", y);
170 if (do_recal && jumpy_delay) {
179 static void hgpk_reset_spew_detection(
struct hgpk_data *priv)
188 static void hgpk_reset_hack_state(
struct psmouse *psmouse)
196 hgpk_reset_spew_detection(priv);
214 static void hgpk_spewing_hack(
struct psmouse *psmouse,
215 int l,
int r,
int x,
int y)
228 if (
abs(x) > 3 ||
abs(y) > 3) {
230 hgpk_reset_spew_detection(priv);
309 static bool hgpk_is_byte_valid(
struct psmouse *psmouse,
unsigned char *
packet)
312 int pktcnt = psmouse->
pktcnt;
315 switch (priv->
mode) {
317 valid = (packet[0] & 0x0C) == 0x08;
321 valid = pktcnt == 1 ?
322 packet[0] ==
HGPK_GS : !(packet[pktcnt - 1] & 0x80);
326 valid = pktcnt == 1 ?
327 packet[0] ==
HGPK_PT : !(packet[pktcnt - 1] & 0x80);
337 "bad data, mode %d (%d) %*ph\n",
343 static void hgpk_process_advanced_packet(
struct psmouse *psmouse)
346 struct input_dev *
idev = psmouse->
dev;
347 unsigned char *packet = psmouse->
packet;
348 int down = !!(packet[2] & 2);
349 int left = !!(packet[3] & 1);
350 int right = !!(packet[3] & 2);
351 int x = packet[1] | ((packet[2] & 0x78) << 4);
352 int y = packet[4] | ((packet[3] & 0x70) << 3);
355 int pt_down = !!(packet[2] & 1);
356 int finger_down = !!(packet[2] & 2);
362 pt_down, finger_down, z);
377 input_report_key(idev,
BTN_LEFT, left);
378 input_report_key(idev,
BTN_RIGHT, right);
385 hgpk_reset_hack_state(psmouse);
409 int x_diff = priv->
abs_x -
x;
410 int y_diff = priv->
abs_y -
y;
411 if (hgpk_discard_decay_hack(psmouse, x_diff, y_diff)) {
416 hgpk_spewing_hack(psmouse, left, right, x_diff, y_diff);
419 input_report_abs(idev,
ABS_X, x);
420 input_report_abs(idev,
ABS_Y, y);
428 static void hgpk_process_simple_packet(
struct psmouse *psmouse)
430 struct input_dev *
dev = psmouse->
dev;
431 unsigned char *packet = psmouse->
packet;
432 int left = packet[0] & 1;
433 int right = (packet[0] >> 1) & 1;
434 int x = packet[1] - ((packet[0] << 4) & 0x100);
435 int y = ((packet[0] << 3) & 0x100) - packet[2];
437 if (packet[0] & 0xc0)
439 "overflow -- 0x%02x 0x%02x 0x%02x\n",
440 packet[0], packet[1], packet[2]);
442 if (hgpk_discard_decay_hack(psmouse, x, y)) {
448 hgpk_spewing_hack(psmouse, left, right, x, y);
463 static psmouse_ret_t hgpk_process_byte(
struct psmouse *psmouse)
467 if (!hgpk_is_byte_valid(psmouse, psmouse->
packet))
472 hgpk_process_simple_packet(psmouse);
474 hgpk_process_advanced_packet(psmouse);
485 "packet inside calibration window, queueing another recalibration\n");
495 static int hgpk_select_mode(
struct psmouse *psmouse)
506 const int advanced_init[] = {
512 switch (priv->
mode) {
522 for (i = 0; i <
ARRAY_SIZE(advanced_init); i++)
541 static void hgpk_setup_input_device(
struct input_dev *
input,
542 struct input_dev *old_input,
546 input->name = old_input->name;
547 input->phys = old_input->phys;
548 input->id = old_input->id;
549 input->dev.parent = old_input->dev.parent;
552 memset(input->evbit, 0,
sizeof(input->evbit));
553 memset(input->relbit, 0,
sizeof(input->relbit));
554 memset(input->keybit, 0,
sizeof(input->keybit));
578 input_set_abs_params(input,
ABS_X, 0, 399, 0, 0);
579 input_set_abs_params(input,
ABS_Y, 0, 290, 0, 0);
582 input_abs_set_res(input,
ABS_X, 8);
583 input_abs_set_res(input,
ABS_Y, 8);
593 input_set_abs_params(input,
ABS_X, 0, 999, 0, 0);
594 input_set_abs_params(input,
ABS_Y, 5, 239, 0, 0);
597 input_abs_set_res(input,
ABS_X, 6);
598 input_abs_set_res(input,
ABS_Y, 8);
606 static int hgpk_reset_device(
struct psmouse *psmouse,
bool recalibrate)
613 struct ps2dev *ps2dev = &psmouse->
ps2dev;
627 err = hgpk_select_mode(psmouse);
633 hgpk_reset_hack_state(psmouse);
638 static int hgpk_force_recalibrate(
struct psmouse *psmouse)
648 psmouse_dbg(psmouse,
"recalibration disabled, ignoring\n");
652 psmouse_dbg(psmouse,
"recalibrating touchpad..\n");
658 err = hgpk_reset_device(psmouse,
true);
679 if (recal_guard_time)
695 static int hgpk_toggle_powersave(
struct psmouse *psmouse,
int enable)
697 struct ps2dev *ps2dev = &psmouse->
ps2dev;
715 for (timeo = 20; timeo > 0; timeo--) {
722 err = hgpk_reset_device(psmouse,
false);
749 static int hgpk_poll(
struct psmouse *psmouse)
755 static int hgpk_reconnect(
struct psmouse *psmouse)
764 if (olpc_board_at_least(olpc_board(0xb2)))
765 if (psmouse->
ps2dev.serio->dev.power.power_state.event !=
770 return hgpk_reset_device(psmouse,
false);
773 static ssize_t hgpk_show_powered(
struct psmouse *psmouse,
void *
data,
char *buf)
780 static ssize_t hgpk_set_powered(
struct psmouse *psmouse,
void *
data,
781 const char *buf,
size_t count)
799 err = hgpk_toggle_powersave(psmouse, value);
804 return err ? err :
count;
808 hgpk_show_powered, hgpk_set_powered,
false);
810 static ssize_t attr_show_mode(
struct psmouse *psmouse,
void *data,
char *buf)
814 return sprintf(buf,
"%s\n", hgpk_mode_names[priv->
mode]);
817 static ssize_t attr_set_mode(
struct psmouse *psmouse,
void *data,
818 const char *buf,
size_t len)
822 enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len);
823 struct input_dev *old_dev = psmouse->
dev;
824 struct input_dev *new_dev;
830 if (old_mode == new_mode)
833 new_dev = input_allocate_device();
840 priv->
mode = new_mode;
841 err = hgpk_reset_device(psmouse,
false);
843 goto err_try_restore;
845 hgpk_setup_input_device(new_dev, old_dev, new_mode);
849 err = input_register_device(new_dev);
851 goto err_try_restore;
853 psmouse->
dev = new_dev;
854 input_unregister_device(old_dev);
859 input_free_device(new_dev);
860 priv->
mode = old_mode;
861 hgpk_reset_device(psmouse,
false);
867 attr_show_mode, attr_set_mode);
869 static ssize_t hgpk_trigger_recal_show(
struct psmouse *psmouse,
870 void *data,
char *buf)
875 static ssize_t hgpk_trigger_recal(
struct psmouse *psmouse,
void *data,
876 const char *buf,
size_t count)
899 hgpk_trigger_recal_show, hgpk_trigger_recal,
false);
901 static void hgpk_disconnect(
struct psmouse *psmouse)
906 &psmouse_attr_powered.dattr);
908 &psmouse_attr_hgpk_mode.dattr);
912 &psmouse_attr_recalibrate.dattr);
922 struct psmouse *psmouse = priv->
psmouse;
924 if (hgpk_force_recalibrate(psmouse))
928 static int hgpk_register(
struct psmouse *psmouse)
935 psmouse->
poll = hgpk_poll;
944 hgpk_setup_input_device(psmouse->
dev,
NULL, priv->
mode);
947 &psmouse_attr_powered.dattr);
949 psmouse_err(psmouse,
"Failed creating 'powered' sysfs node\n");
954 &psmouse_attr_hgpk_mode.dattr);
957 "Failed creating 'hgpk_mode' sysfs node\n");
958 goto err_remove_powered;
964 &psmouse_attr_recalibrate.dattr);
967 "Failed creating 'recalibrate' sysfs node\n");
968 goto err_remove_mode;
976 &psmouse_attr_hgpk_mode.dattr);
979 &psmouse_attr_powered.dattr);
998 priv->
mode = hgpk_default_mode;
1001 err = hgpk_reset_device(psmouse,
false);
1005 err = hgpk_register(psmouse);
1017 static enum hgpk_model_t hgpk_get_model(
struct psmouse *psmouse)
1019 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1020 unsigned char param[3];
1033 if (param[0] != 0x67 || param[1] != 0x00)
1036 psmouse_info(psmouse,
"OLPC touchpad revision 0x%x\n", param[2]);
1045 version = hgpk_get_model(psmouse);
1049 if (set_properties) {
1050 psmouse->
vendor =
"ALPS";
1051 psmouse->
name =
"HGPK";
1060 hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name,
1065 sizeof(hgpk_mode_name));