11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * ALPS touchpad PS/2 mouse driver 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au> 5963f626dSPeter Osterlund * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com> 61da177e4SLinus Torvalds * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru> 71da177e4SLinus Torvalds * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz> 81d9f2626SSebastian Kapfer * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net> 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * ALPS detection, tap switching and status querying info is taken from 111da177e4SLinus Torvalds * tpconfig utility (by C. Scott Ananian and Bruce Kall). 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify it 141da177e4SLinus Torvalds * under the terms of the GNU General Public License version 2 as published by 151da177e4SLinus Torvalds * the Free Software Foundation. 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 185a0e3ad6STejun Heo #include <linux/slab.h> 191da177e4SLinus Torvalds #include <linux/input.h> 2001ce661fSSeth Forshee #include <linux/input/mt.h> 211da177e4SLinus Torvalds #include <linux/serio.h> 221da177e4SLinus Torvalds #include <linux/libps2.h> 23073e570dSHans de Goede #include <linux/dmi.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include "psmouse.h" 261da177e4SLinus Torvalds #include "alps.h" 271da177e4SLinus Torvalds 2825bded7cSSeth Forshee /* 2925bded7cSSeth Forshee * Definitions for ALPS version 3 and 4 command mode protocol 3025bded7cSSeth Forshee */ 3125bded7cSSeth Forshee #define ALPS_CMD_NIBBLE_10 0x01f2 3225bded7cSSeth Forshee 33cd401204SKevin Cernekee #define ALPS_REG_BASE_RUSHMORE 0xc2c0 34dae928ecSPali Rohár #define ALPS_REG_BASE_V7 0xc2c0 35cd401204SKevin Cernekee #define ALPS_REG_BASE_PINNACLE 0x0000 36cd401204SKevin Cernekee 3725bded7cSSeth Forshee static const struct alps_nibble_commands alps_v3_nibble_commands[] = { 3825bded7cSSeth Forshee { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */ 3925bded7cSSeth Forshee { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */ 4025bded7cSSeth Forshee { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */ 4125bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */ 4225bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */ 4325bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */ 4425bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */ 4525bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */ 4625bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */ 4725bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */ 4825bded7cSSeth Forshee { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */ 4925bded7cSSeth Forshee { PSMOUSE_CMD_SETRES, 0x00 }, /* b */ 5025bded7cSSeth Forshee { PSMOUSE_CMD_SETRES, 0x01 }, /* c */ 5125bded7cSSeth Forshee { PSMOUSE_CMD_SETRES, 0x02 }, /* d */ 5225bded7cSSeth Forshee { PSMOUSE_CMD_SETRES, 0x03 }, /* e */ 5325bded7cSSeth Forshee { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */ 5425bded7cSSeth Forshee }; 5525bded7cSSeth Forshee 5625bded7cSSeth Forshee static const struct alps_nibble_commands alps_v4_nibble_commands[] = { 5725bded7cSSeth Forshee { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */ 5825bded7cSSeth Forshee { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */ 5925bded7cSSeth Forshee { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */ 6025bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */ 6125bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */ 6225bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */ 6325bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */ 6425bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */ 6525bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */ 6625bded7cSSeth Forshee { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */ 6725bded7cSSeth Forshee { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */ 6825bded7cSSeth Forshee { PSMOUSE_CMD_SETRES, 0x00 }, /* b */ 6925bded7cSSeth Forshee { PSMOUSE_CMD_SETRES, 0x01 }, /* c */ 7025bded7cSSeth Forshee { PSMOUSE_CMD_SETRES, 0x02 }, /* d */ 7125bded7cSSeth Forshee { PSMOUSE_CMD_SETRES, 0x03 }, /* e */ 7225bded7cSSeth Forshee { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */ 7325bded7cSSeth Forshee }; 7425bded7cSSeth Forshee 7595f75e91SYunkang Tang static const struct alps_nibble_commands alps_v6_nibble_commands[] = { 7695f75e91SYunkang Tang { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */ 7795f75e91SYunkang Tang { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */ 7895f75e91SYunkang Tang { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */ 7995f75e91SYunkang Tang { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */ 8095f75e91SYunkang Tang { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */ 8195f75e91SYunkang Tang { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */ 8295f75e91SYunkang Tang { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */ 8395f75e91SYunkang Tang { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */ 8495f75e91SYunkang Tang { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */ 8595f75e91SYunkang Tang { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */ 8695f75e91SYunkang Tang { PSMOUSE_CMD_SETRES, 0x00 }, /* a */ 8795f75e91SYunkang Tang { PSMOUSE_CMD_SETRES, 0x01 }, /* b */ 8895f75e91SYunkang Tang { PSMOUSE_CMD_SETRES, 0x02 }, /* c */ 8995f75e91SYunkang Tang { PSMOUSE_CMD_SETRES, 0x03 }, /* d */ 9095f75e91SYunkang Tang { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */ 9195f75e91SYunkang Tang { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */ 9295f75e91SYunkang Tang }; 9395f75e91SYunkang Tang 9425bded7cSSeth Forshee 9571bb21b6SMaxim Levitsky #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */ 9671bb21b6SMaxim Levitsky #define ALPS_PASS 0x04 /* device has a pass-through port */ 9771bb21b6SMaxim Levitsky 9871bb21b6SMaxim Levitsky #define ALPS_WHEEL 0x08 /* hardware wheel present */ 9971bb21b6SMaxim Levitsky #define ALPS_FW_BK_1 0x10 /* front & back buttons present */ 10071bb21b6SMaxim Levitsky #define ALPS_FW_BK_2 0x20 /* front & back buttons present */ 10171bb21b6SMaxim Levitsky #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */ 1021d9f2626SSebastian Kapfer #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with 1031d9f2626SSebastian Kapfer 6-byte ALPS packet */ 10419556219SHans de Goede #define ALPS_STICK_BITS 0x100 /* separate stick button bits */ 1053808843cSYunkang Tang #define ALPS_BUTTONPAD 0x200 /* device is a clickpad */ 1067ad8a106SBen Gamari #define ALPS_DUALPOINT_WITH_PRESSURE 0x400 /* device can report trackpoint pressure */ 1071da177e4SLinus Torvalds 108e38de678SHelge Deller static const struct alps_model_info alps_model_data[] = { 109626b9da0SDmitry Torokhov /* 110626b9da0SDmitry Torokhov * XXX This entry is suspicious. First byte has zero lower nibble, 111626b9da0SDmitry Torokhov * which is what a normal mouse would report. Also, the value 0x0e 112626b9da0SDmitry Torokhov * isn't valid per PS/2 spec. 113626b9da0SDmitry Torokhov */ 114a3cbfd56SPali Rohár { { 0x20, 0x02, 0x0e }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, 115626b9da0SDmitry Torokhov 116a3cbfd56SPali Rohár { { 0x22, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, 117a3cbfd56SPali Rohár { { 0x22, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */ 118a3cbfd56SPali Rohár { { 0x32, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */ 119a3cbfd56SPali Rohár { { 0x33, 0x02, 0x0a }, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */ 120a3cbfd56SPali Rohár { { 0x52, 0x01, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, 1218326bb57SDmitry Torokhov ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */ 122a3cbfd56SPali Rohár { { 0x53, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, 123a3cbfd56SPali Rohár { { 0x53, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, 124a3cbfd56SPali Rohár { { 0x60, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */ 125a3cbfd56SPali Rohár { { 0x62, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xcf, 0xcf, 126a3cbfd56SPali Rohár ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Dell Latitude E5500, E6400, E6500, Precision M4400 */ 127a3cbfd56SPali Rohár { { 0x63, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, 128a3cbfd56SPali Rohár { { 0x63, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, 129a3cbfd56SPali Rohár { { 0x63, 0x02, 0x28 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */ 130a3cbfd56SPali Rohár { { 0x63, 0x02, 0x3c }, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */ 131a3cbfd56SPali Rohár { { 0x63, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */ 132a3cbfd56SPali Rohár { { 0x63, 0x02, 0x64 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, 133a3cbfd56SPali Rohár { { 0x63, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */ 134a3cbfd56SPali Rohár { { 0x73, 0x00, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */ 135a3cbfd56SPali Rohár { { 0x73, 0x00, 0x14 }, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */ 136a3cbfd56SPali Rohár { { 0x73, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, 137a3cbfd56SPali Rohár { { 0x73, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */ 138a3cbfd56SPali Rohár { { 0x73, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */ 1391da177e4SLinus Torvalds }; 1401da177e4SLinus Torvalds 1413296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_protocol_data = { 1424621c966SPali Rohár ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE 1433296f71cSDmitry Torokhov }; 1443296f71cSDmitry Torokhov 1453296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v3_rushmore_data = { 1464621c966SPali Rohár ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE 1473296f71cSDmitry Torokhov }; 1483296f71cSDmitry Torokhov 14909c398bcSPali Rohár static const struct alps_protocol_info alps_v4_protocol_data = { 15009c398bcSPali Rohár ALPS_PROTO_V4, 0x8f, 0x8f, 0 15109c398bcSPali Rohár }; 15209c398bcSPali Rohár 1533296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v5_protocol_data = { 1543296f71cSDmitry Torokhov ALPS_PROTO_V5, 0xc8, 0xd8, 0 1553296f71cSDmitry Torokhov }; 1563296f71cSDmitry Torokhov 1573296f71cSDmitry Torokhov static const struct alps_protocol_info alps_v7_protocol_data = { 1584621c966SPali Rohár ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE 1593296f71cSDmitry Torokhov }; 1603296f71cSDmitry Torokhov 1613db5b9f7SMasaki Ota static const struct alps_protocol_info alps_v8_protocol_data = { 1623db5b9f7SMasaki Ota ALPS_PROTO_V8, 0x18, 0x18, 0 1633db5b9f7SMasaki Ota }; 1643db5b9f7SMasaki Ota 165c9815232SPali Rohár static const struct alps_protocol_info alps_v9_protocol_data = { 166c9815232SPali Rohár ALPS_PROTO_V9, 0xc8, 0xc8, 0 167c9815232SPali Rohár }; 168c9815232SPali Rohár 16919556219SHans de Goede /* 17019556219SHans de Goede * Some v2 models report the stick buttons in separate bits 17119556219SHans de Goede */ 17219556219SHans de Goede static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = { 17319556219SHans de Goede #if defined(CONFIG_DMI) && defined(CONFIG_X86) 17419556219SHans de Goede { 17519556219SHans de Goede /* Extrapolated from other entries */ 17619556219SHans de Goede .matches = { 17719556219SHans de Goede DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 17819556219SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"), 17919556219SHans de Goede }, 18019556219SHans de Goede }, 18119556219SHans de Goede { 18219556219SHans de Goede /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */ 18319556219SHans de Goede .matches = { 18419556219SHans de Goede DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 18519556219SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"), 18619556219SHans de Goede }, 18719556219SHans de Goede }, 18819556219SHans de Goede { 18919556219SHans de Goede /* Reported-by: Hans de Goede <hdegoede@redhat.com> */ 19019556219SHans de Goede .matches = { 19119556219SHans de Goede DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 19219556219SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"), 19319556219SHans de Goede }, 19419556219SHans de Goede }, 19519556219SHans de Goede { 19619556219SHans de Goede /* Extrapolated from other entries */ 19719556219SHans de Goede .matches = { 19819556219SHans de Goede DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 19919556219SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"), 20019556219SHans de Goede }, 20119556219SHans de Goede }, 20219556219SHans de Goede #endif 20319556219SHans de Goede { } 20419556219SHans de Goede }; 20519556219SHans de Goede 20624af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv, 20724af5cb9SKevin Cernekee struct input_dev *dev1); 208688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv, 20924af5cb9SKevin Cernekee struct input_dev *dev1); 2108eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv, 2118eccd393SMasaki Ota struct input_dev *dev1); 2123db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv, 2133db5b9f7SMasaki Ota struct input_dev *dev1); 21424af5cb9SKevin Cernekee 2155fb94e9cSMauro Carvalho Chehab /* Packet formats are described in Documentation/input/devices/alps.rst */ 2161da177e4SLinus Torvalds 21799df65e7SKevin Cernekee static bool alps_is_valid_first_byte(struct alps_data *priv, 2181d9f2626SSebastian Kapfer unsigned char data) 2191d9f2626SSebastian Kapfer { 22099df65e7SKevin Cernekee return (data & priv->mask0) == priv->byte0; 2211d9f2626SSebastian Kapfer } 2221d9f2626SSebastian Kapfer 22304aae283SPali Rohár static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2, 2241d9f2626SSebastian Kapfer int left, int right, int middle) 2251d9f2626SSebastian Kapfer { 2261d9f2626SSebastian Kapfer struct input_dev *dev; 2271d9f2626SSebastian Kapfer 2281d9f2626SSebastian Kapfer /* 2291d9f2626SSebastian Kapfer * If shared button has already been reported on the 2301d9f2626SSebastian Kapfer * other device (dev2) then this event should be also 2311d9f2626SSebastian Kapfer * sent through that device. 2321d9f2626SSebastian Kapfer */ 23304aae283SPali Rohár dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1; 2341d9f2626SSebastian Kapfer input_report_key(dev, BTN_LEFT, left); 2351d9f2626SSebastian Kapfer 23604aae283SPali Rohár dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1; 2371d9f2626SSebastian Kapfer input_report_key(dev, BTN_RIGHT, right); 2381d9f2626SSebastian Kapfer 23904aae283SPali Rohár dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1; 2401d9f2626SSebastian Kapfer input_report_key(dev, BTN_MIDDLE, middle); 2411d9f2626SSebastian Kapfer 2421d9f2626SSebastian Kapfer /* 2431d9f2626SSebastian Kapfer * Sync the _other_ device now, we'll do the first 2441d9f2626SSebastian Kapfer * device later once we report the rest of the events. 2451d9f2626SSebastian Kapfer */ 24604aae283SPali Rohár if (dev2) 2471d9f2626SSebastian Kapfer input_sync(dev2); 2481d9f2626SSebastian Kapfer } 2491d9f2626SSebastian Kapfer 25025bded7cSSeth Forshee static void alps_process_packet_v1_v2(struct psmouse *psmouse) 2511da177e4SLinus Torvalds { 2521da177e4SLinus Torvalds struct alps_data *priv = psmouse->private; 2531da177e4SLinus Torvalds unsigned char *packet = psmouse->packet; 2542e5b636bSDmitry Torokhov struct input_dev *dev = psmouse->dev; 2552e5b636bSDmitry Torokhov struct input_dev *dev2 = priv->dev2; 2561da177e4SLinus Torvalds int x, y, z, ges, fin, left, right, middle; 257c30b4c10SIvan Casado Ruiz int back = 0, forward = 0; 2581da177e4SLinus Torvalds 25999df65e7SKevin Cernekee if (priv->proto_version == ALPS_PROTO_V1) { 260d2f4012fSYotam Medini left = packet[2] & 0x10; 261d2f4012fSYotam Medini right = packet[2] & 0x08; 2621da177e4SLinus Torvalds middle = 0; 2631da177e4SLinus Torvalds x = packet[1] | ((packet[0] & 0x07) << 7); 2641da177e4SLinus Torvalds y = packet[4] | ((packet[3] & 0x07) << 7); 2651da177e4SLinus Torvalds z = packet[5]; 2661da177e4SLinus Torvalds } else { 2671da177e4SLinus Torvalds left = packet[3] & 1; 2681da177e4SLinus Torvalds right = packet[3] & 2; 2691da177e4SLinus Torvalds middle = packet[3] & 4; 2701da177e4SLinus Torvalds x = packet[1] | ((packet[2] & 0x78) << (7 - 3)); 2711da177e4SLinus Torvalds y = packet[4] | ((packet[3] & 0x70) << (7 - 4)); 2721da177e4SLinus Torvalds z = packet[5]; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 27599df65e7SKevin Cernekee if (priv->flags & ALPS_FW_BK_1) { 2763c00bb96SLaszlo Kajan back = packet[0] & 0x10; 2773c00bb96SLaszlo Kajan forward = packet[2] & 4; 278c30b4c10SIvan Casado Ruiz } 279c30b4c10SIvan Casado Ruiz 28099df65e7SKevin Cernekee if (priv->flags & ALPS_FW_BK_2) { 281c30b4c10SIvan Casado Ruiz back = packet[3] & 4; 282c30b4c10SIvan Casado Ruiz forward = packet[2] & 4; 283c30b4c10SIvan Casado Ruiz if ((middle = forward && back)) 284c30b4c10SIvan Casado Ruiz forward = back = 0; 285c30b4c10SIvan Casado Ruiz } 286c30b4c10SIvan Casado Ruiz 2871da177e4SLinus Torvalds ges = packet[2] & 1; 2881da177e4SLinus Torvalds fin = packet[2] & 2; 2891da177e4SLinus Torvalds 29099df65e7SKevin Cernekee if ((priv->flags & ALPS_DUALPOINT) && z == 127) { 2911da177e4SLinus Torvalds input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x)); 2921da177e4SLinus Torvalds input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y)); 293d7ed5d88SUlrich Dangel 29404aae283SPali Rohár alps_report_buttons(dev2, dev, left, right, middle); 295d7ed5d88SUlrich Dangel 2961da177e4SLinus Torvalds input_sync(dev2); 2971da177e4SLinus Torvalds return; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 30019556219SHans de Goede /* Some models have separate stick button bits */ 30119556219SHans de Goede if (priv->flags & ALPS_STICK_BITS) { 30292bac83dSHans de Goede left |= packet[0] & 1; 30392bac83dSHans de Goede right |= packet[0] & 2; 30492bac83dSHans de Goede middle |= packet[0] & 4; 30592bac83dSHans de Goede } 30692bac83dSHans de Goede 30704aae283SPali Rohár alps_report_buttons(dev, dev2, left, right, middle); 308d7ed5d88SUlrich Dangel 3091da177e4SLinus Torvalds /* Convert hardware tap to a reasonable Z value */ 31071bb21b6SMaxim Levitsky if (ges && !fin) 31171bb21b6SMaxim Levitsky z = 40; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds /* 3141da177e4SLinus Torvalds * A "tap and drag" operation is reported by the hardware as a transition 3151da177e4SLinus Torvalds * from (!fin && ges) to (fin && ges). This should be translated to the 3161da177e4SLinus Torvalds * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually. 3171da177e4SLinus Torvalds */ 3181da177e4SLinus Torvalds if (ges && fin && !priv->prev_fin) { 3191da177e4SLinus Torvalds input_report_abs(dev, ABS_X, x); 3201da177e4SLinus Torvalds input_report_abs(dev, ABS_Y, y); 3211da177e4SLinus Torvalds input_report_abs(dev, ABS_PRESSURE, 0); 3221da177e4SLinus Torvalds input_report_key(dev, BTN_TOOL_FINGER, 0); 3231da177e4SLinus Torvalds input_sync(dev); 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds priv->prev_fin = fin; 3261da177e4SLinus Torvalds 32771bb21b6SMaxim Levitsky if (z > 30) 32871bb21b6SMaxim Levitsky input_report_key(dev, BTN_TOUCH, 1); 32971bb21b6SMaxim Levitsky if (z < 25) 33071bb21b6SMaxim Levitsky input_report_key(dev, BTN_TOUCH, 0); 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds if (z > 0) { 3331da177e4SLinus Torvalds input_report_abs(dev, ABS_X, x); 3341da177e4SLinus Torvalds input_report_abs(dev, ABS_Y, y); 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds input_report_abs(dev, ABS_PRESSURE, z); 3381da177e4SLinus Torvalds input_report_key(dev, BTN_TOOL_FINGER, z > 0); 3391da177e4SLinus Torvalds 34099df65e7SKevin Cernekee if (priv->flags & ALPS_WHEEL) 341e6c047b9SVojtech Pavlik input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07)); 3421da177e4SLinus Torvalds 34399df65e7SKevin Cernekee if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) { 344c30b4c10SIvan Casado Ruiz input_report_key(dev, BTN_FORWARD, forward); 345c30b4c10SIvan Casado Ruiz input_report_key(dev, BTN_BACK, back); 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 34899df65e7SKevin Cernekee if (priv->flags & ALPS_FOUR_BUTTONS) { 34971bb21b6SMaxim Levitsky input_report_key(dev, BTN_0, packet[2] & 4); 35071bb21b6SMaxim Levitsky input_report_key(dev, BTN_1, packet[0] & 0x10); 35171bb21b6SMaxim Levitsky input_report_key(dev, BTN_2, packet[3] & 4); 35271bb21b6SMaxim Levitsky input_report_key(dev, BTN_3, packet[0] & 0x20); 35371bb21b6SMaxim Levitsky } 35471bb21b6SMaxim Levitsky 3551da177e4SLinus Torvalds input_sync(dev); 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 358036e6c7bSHans de Goede static void alps_get_bitmap_points(unsigned int map, 359036e6c7bSHans de Goede struct alps_bitmap_point *low, 360036e6c7bSHans de Goede struct alps_bitmap_point *high, 361036e6c7bSHans de Goede int *fingers) 362036e6c7bSHans de Goede { 363036e6c7bSHans de Goede struct alps_bitmap_point *point; 364036e6c7bSHans de Goede int i, bit, prev_bit = 0; 365036e6c7bSHans de Goede 366036e6c7bSHans de Goede point = low; 367036e6c7bSHans de Goede for (i = 0; map != 0; i++, map >>= 1) { 368036e6c7bSHans de Goede bit = map & 1; 369036e6c7bSHans de Goede if (bit) { 370036e6c7bSHans de Goede if (!prev_bit) { 371036e6c7bSHans de Goede point->start_bit = i; 372105affbfSHans de Goede point->num_bits = 0; 373036e6c7bSHans de Goede (*fingers)++; 374036e6c7bSHans de Goede } 375036e6c7bSHans de Goede point->num_bits++; 376036e6c7bSHans de Goede } else { 377036e6c7bSHans de Goede if (prev_bit) 378036e6c7bSHans de Goede point = high; 379036e6c7bSHans de Goede } 380036e6c7bSHans de Goede prev_bit = bit; 381036e6c7bSHans de Goede } 382036e6c7bSHans de Goede } 383036e6c7bSHans de Goede 384ee65d4b3SYunkang Tang /* 385dccf1dd8SHans de Goede * Process bitmap data from semi-mt protocols. Returns the number of 38601ce661fSSeth Forshee * fingers detected. A return value of 0 means at least one of the 38701ce661fSSeth Forshee * bitmaps was empty. 38801ce661fSSeth Forshee * 38901ce661fSSeth Forshee * The bitmaps don't have enough data to track fingers, so this function 39001ce661fSSeth Forshee * only generates points representing a bounding box of all contacts. 39102d04254SHans de Goede * These points are returned in fields->mt when the return value 39201ce661fSSeth Forshee * is greater than 0. 39301ce661fSSeth Forshee */ 3947a9f73e7SKevin Cernekee static int alps_process_bitmap(struct alps_data *priv, 39502d04254SHans de Goede struct alps_fields *fields) 39601ce661fSSeth Forshee { 3974dd26573SHans de Goede int i, fingers_x = 0, fingers_y = 0, fingers, closest; 39801ce661fSSeth Forshee struct alps_bitmap_point x_low = {0,}, x_high = {0,}; 39901ce661fSSeth Forshee struct alps_bitmap_point y_low = {0,}, y_high = {0,}; 4004dd26573SHans de Goede struct input_mt_pos corner[4]; 40101ce661fSSeth Forshee 40202d04254SHans de Goede if (!fields->x_map || !fields->y_map) 40301ce661fSSeth Forshee return 0; 40401ce661fSSeth Forshee 40502d04254SHans de Goede alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x); 40602d04254SHans de Goede alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y); 40701ce661fSSeth Forshee 40801ce661fSSeth Forshee /* 40901ce661fSSeth Forshee * Fingers can overlap, so we use the maximum count of fingers 41001ce661fSSeth Forshee * on either axis as the finger count. 41101ce661fSSeth Forshee */ 41201ce661fSSeth Forshee fingers = max(fingers_x, fingers_y); 41301ce661fSSeth Forshee 41401ce661fSSeth Forshee /* 41520bea68bSHans de Goede * If an axis reports only a single contact, we have overlapping or 41620bea68bSHans de Goede * adjacent fingers. Divide the single contact between the two points. 41701ce661fSSeth Forshee */ 41801ce661fSSeth Forshee if (fingers_x == 1) { 41928835f45SHans de Goede i = (x_low.num_bits - 1) / 2; 42001ce661fSSeth Forshee x_low.num_bits = x_low.num_bits - i; 42101ce661fSSeth Forshee x_high.start_bit = x_low.start_bit + i; 42201ce661fSSeth Forshee x_high.num_bits = max(i, 1); 42320bea68bSHans de Goede } 42420bea68bSHans de Goede if (fingers_y == 1) { 42528835f45SHans de Goede i = (y_low.num_bits - 1) / 2; 42601ce661fSSeth Forshee y_low.num_bits = y_low.num_bits - i; 42701ce661fSSeth Forshee y_high.start_bit = y_low.start_bit + i; 42801ce661fSSeth Forshee y_high.num_bits = max(i, 1); 42901ce661fSSeth Forshee } 43001ce661fSSeth Forshee 4314dd26573SHans de Goede /* top-left corner */ 4324dd26573SHans de Goede corner[0].x = 43302d04254SHans de Goede (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) / 4347a9f73e7SKevin Cernekee (2 * (priv->x_bits - 1)); 4354dd26573SHans de Goede corner[0].y = 43602d04254SHans de Goede (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) / 4377a9f73e7SKevin Cernekee (2 * (priv->y_bits - 1)); 43801ce661fSSeth Forshee 4394dd26573SHans de Goede /* top-right corner */ 4404dd26573SHans de Goede corner[1].x = 44102d04254SHans de Goede (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) / 4427a9f73e7SKevin Cernekee (2 * (priv->x_bits - 1)); 4434dd26573SHans de Goede corner[1].y = 4444dd26573SHans de Goede (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) / 4454dd26573SHans de Goede (2 * (priv->y_bits - 1)); 4464dd26573SHans de Goede 4474dd26573SHans de Goede /* bottom-right corner */ 4484dd26573SHans de Goede corner[2].x = 4494dd26573SHans de Goede (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) / 4504dd26573SHans de Goede (2 * (priv->x_bits - 1)); 4514dd26573SHans de Goede corner[2].y = 45202d04254SHans de Goede (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) / 4537a9f73e7SKevin Cernekee (2 * (priv->y_bits - 1)); 45401ce661fSSeth Forshee 4554dd26573SHans de Goede /* bottom-left corner */ 4564dd26573SHans de Goede corner[3].x = 4574dd26573SHans de Goede (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) / 4584dd26573SHans de Goede (2 * (priv->x_bits - 1)); 4594dd26573SHans de Goede corner[3].y = 46001ce661fSSeth Forshee (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) / 46101ce661fSSeth Forshee (2 * (priv->y_bits - 1)); 46201ce661fSSeth Forshee 463dccf1dd8SHans de Goede /* x-bitmap order is reversed on v5 touchpads */ 464dccf1dd8SHans de Goede if (priv->proto_version == ALPS_PROTO_V5) { 465dccf1dd8SHans de Goede for (i = 0; i < 4; i++) 466dccf1dd8SHans de Goede corner[i].x = priv->x_max - corner[i].x; 46740e8f53bSHans de Goede } 46840e8f53bSHans de Goede 469dccf1dd8SHans de Goede /* y-bitmap order is reversed on v3 and v4 touchpads */ 470dccf1dd8SHans de Goede if (priv->proto_version == ALPS_PROTO_V3 || 471dccf1dd8SHans de Goede priv->proto_version == ALPS_PROTO_V4) { 4724dd26573SHans de Goede for (i = 0; i < 4; i++) 4734dd26573SHans de Goede corner[i].y = priv->y_max - corner[i].y; 47401ce661fSSeth Forshee } 47501ce661fSSeth Forshee 4764dd26573SHans de Goede /* 4774dd26573SHans de Goede * We only select a corner for the second touch once per 2 finger 4784dd26573SHans de Goede * touch sequence to avoid the chosen corner (and thus the coordinates) 4794dd26573SHans de Goede * jumping around when the first touch is in the middle. 4804dd26573SHans de Goede */ 4814dd26573SHans de Goede if (priv->second_touch == -1) { 4824dd26573SHans de Goede /* Find corner closest to our st coordinates */ 4834dd26573SHans de Goede closest = 0x7fffffff; 4844dd26573SHans de Goede for (i = 0; i < 4; i++) { 4854dd26573SHans de Goede int dx = fields->st.x - corner[i].x; 4864dd26573SHans de Goede int dy = fields->st.y - corner[i].y; 4874dd26573SHans de Goede int distance = dx * dx + dy * dy; 4884dd26573SHans de Goede 4894dd26573SHans de Goede if (distance < closest) { 4904dd26573SHans de Goede priv->second_touch = i; 4914dd26573SHans de Goede closest = distance; 4924dd26573SHans de Goede } 4934dd26573SHans de Goede } 4944dd26573SHans de Goede /* And select the opposite corner to use for the 2nd touch */ 4954dd26573SHans de Goede priv->second_touch = (priv->second_touch + 2) % 4; 4964dd26573SHans de Goede } 4974dd26573SHans de Goede 4984dd26573SHans de Goede fields->mt[0] = fields->st; 4994dd26573SHans de Goede fields->mt[1] = corner[priv->second_touch]; 5004dd26573SHans de Goede 50101ce661fSSeth Forshee return fingers; 50201ce661fSSeth Forshee } 50301ce661fSSeth Forshee 504cdf333efSHans de Goede static void alps_set_slot(struct input_dev *dev, int slot, int x, int y) 50501ce661fSSeth Forshee { 50601ce661fSSeth Forshee input_mt_slot(dev, slot); 507cdf333efSHans de Goede input_mt_report_slot_state(dev, MT_TOOL_FINGER, true); 50801ce661fSSeth Forshee input_report_abs(dev, ABS_MT_POSITION_X, x); 50901ce661fSSeth Forshee input_report_abs(dev, ABS_MT_POSITION_Y, y); 51001ce661fSSeth Forshee } 51101ce661fSSeth Forshee 512cdf333efSHans de Goede static void alps_report_mt_data(struct psmouse *psmouse, int n) 51301ce661fSSeth Forshee { 51402d04254SHans de Goede struct alps_data *priv = psmouse->private; 51502d04254SHans de Goede struct input_dev *dev = psmouse->dev; 51602d04254SHans de Goede struct alps_fields *f = &priv->f; 517cdf333efSHans de Goede int i, slot[MAX_TOUCHES]; 51802d04254SHans de Goede 519448c7f38SHenrik Rydberg input_mt_assign_slots(dev, slot, f->mt, n, 0); 520cdf333efSHans de Goede for (i = 0; i < n; i++) 521cdf333efSHans de Goede alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y); 522cdf333efSHans de Goede 523cdf333efSHans de Goede input_mt_sync_frame(dev); 52401ce661fSSeth Forshee } 52501ce661fSSeth Forshee 52668c21870SHans de Goede static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers) 52768c21870SHans de Goede { 52868c21870SHans de Goede struct alps_data *priv = psmouse->private; 52968c21870SHans de Goede struct input_dev *dev = psmouse->dev; 53068c21870SHans de Goede struct alps_fields *f = &priv->f; 53168c21870SHans de Goede 53268c21870SHans de Goede /* Use st data when we don't have mt data */ 53368c21870SHans de Goede if (fingers < 2) { 53468c21870SHans de Goede f->mt[0].x = f->st.x; 53568c21870SHans de Goede f->mt[0].y = f->st.y; 53668c21870SHans de Goede fingers = f->pressure > 0 ? 1 : 0; 5374dd26573SHans de Goede priv->second_touch = -1; 53868c21870SHans de Goede } 53968c21870SHans de Goede 5401662c033SHans de Goede if (fingers >= 1) 5411662c033SHans de Goede alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y); 5421662c033SHans de Goede if (fingers >= 2) 5431662c033SHans de Goede alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y); 5441662c033SHans de Goede input_mt_sync_frame(dev); 54568c21870SHans de Goede 54668c21870SHans de Goede input_mt_report_finger_count(dev, fingers); 54768c21870SHans de Goede 54868c21870SHans de Goede input_report_key(dev, BTN_LEFT, f->left); 54968c21870SHans de Goede input_report_key(dev, BTN_RIGHT, f->right); 55068c21870SHans de Goede input_report_key(dev, BTN_MIDDLE, f->middle); 55168c21870SHans de Goede 55268c21870SHans de Goede input_report_abs(dev, ABS_PRESSURE, f->pressure); 55368c21870SHans de Goede 55468c21870SHans de Goede input_sync(dev); 55568c21870SHans de Goede } 55668c21870SHans de Goede 55725bded7cSSeth Forshee static void alps_process_trackstick_packet_v3(struct psmouse *psmouse) 55825bded7cSSeth Forshee { 55925bded7cSSeth Forshee struct alps_data *priv = psmouse->private; 56025bded7cSSeth Forshee unsigned char *packet = psmouse->packet; 56125bded7cSSeth Forshee struct input_dev *dev = priv->dev2; 56225bded7cSSeth Forshee int x, y, z, left, right, middle; 56325bded7cSSeth Forshee 56434412ba2SPali Rohár /* It should be a DualPoint when received trackstick packet */ 56534412ba2SPali Rohár if (!(priv->flags & ALPS_DUALPOINT)) { 56634412ba2SPali Rohár psmouse_warn(psmouse, 56734412ba2SPali Rohár "Rejected trackstick packet from non DualPoint device"); 56834412ba2SPali Rohár return; 56934412ba2SPali Rohár } 57034412ba2SPali Rohár 57125bded7cSSeth Forshee /* Sanity check packet */ 57225bded7cSSeth Forshee if (!(packet[0] & 0x40)) { 57325bded7cSSeth Forshee psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n"); 57425bded7cSSeth Forshee return; 57525bded7cSSeth Forshee } 57625bded7cSSeth Forshee 57725bded7cSSeth Forshee /* 57825bded7cSSeth Forshee * There's a special packet that seems to indicate the end 57925bded7cSSeth Forshee * of a stream of trackstick data. Filter these out. 58025bded7cSSeth Forshee */ 58125bded7cSSeth Forshee if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f) 58225bded7cSSeth Forshee return; 58325bded7cSSeth Forshee 58425bded7cSSeth Forshee x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f)); 58525bded7cSSeth Forshee y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f)); 5869c71b2c5SPali Rohár z = packet[4] & 0x7f; 58725bded7cSSeth Forshee 58825bded7cSSeth Forshee /* 58925bded7cSSeth Forshee * The x and y values tend to be quite large, and when used 59025bded7cSSeth Forshee * alone the trackstick is difficult to use. Scale them down 59125bded7cSSeth Forshee * to compensate. 59225bded7cSSeth Forshee */ 59325bded7cSSeth Forshee x /= 8; 59425bded7cSSeth Forshee y /= 8; 59525bded7cSSeth Forshee 59625bded7cSSeth Forshee input_report_rel(dev, REL_X, x); 59725bded7cSSeth Forshee input_report_rel(dev, REL_Y, -y); 5984621c966SPali Rohár input_report_abs(dev, ABS_PRESSURE, z); 59925bded7cSSeth Forshee 60025bded7cSSeth Forshee /* 60125bded7cSSeth Forshee * Most ALPS models report the trackstick buttons in the touchpad 60225bded7cSSeth Forshee * packets, but a few report them here. No reliable way has been 60325bded7cSSeth Forshee * found to differentiate between the models upfront, so we enable 60425bded7cSSeth Forshee * the quirk in response to seeing a button press in the trackstick 60525bded7cSSeth Forshee * packet. 60625bded7cSSeth Forshee */ 60725bded7cSSeth Forshee left = packet[3] & 0x01; 60825bded7cSSeth Forshee right = packet[3] & 0x02; 60925bded7cSSeth Forshee middle = packet[3] & 0x04; 61025bded7cSSeth Forshee 61125bded7cSSeth Forshee if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) && 61225bded7cSSeth Forshee (left || right || middle)) 61325bded7cSSeth Forshee priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS; 61425bded7cSSeth Forshee 61525bded7cSSeth Forshee if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) { 61625bded7cSSeth Forshee input_report_key(dev, BTN_LEFT, left); 61725bded7cSSeth Forshee input_report_key(dev, BTN_RIGHT, right); 61825bded7cSSeth Forshee input_report_key(dev, BTN_MIDDLE, middle); 61925bded7cSSeth Forshee } 62025bded7cSSeth Forshee 62125bded7cSSeth Forshee input_sync(dev); 62225bded7cSSeth Forshee return; 62325bded7cSSeth Forshee } 62425bded7cSSeth Forshee 625f85e5001SKevin Cernekee static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p) 626f85e5001SKevin Cernekee { 627f85e5001SKevin Cernekee f->left = !!(p[3] & 0x01); 628f85e5001SKevin Cernekee f->right = !!(p[3] & 0x02); 629f85e5001SKevin Cernekee f->middle = !!(p[3] & 0x04); 630f85e5001SKevin Cernekee 631f85e5001SKevin Cernekee f->ts_left = !!(p[3] & 0x10); 632f85e5001SKevin Cernekee f->ts_right = !!(p[3] & 0x20); 633f85e5001SKevin Cernekee f->ts_middle = !!(p[3] & 0x40); 634f85e5001SKevin Cernekee } 635f85e5001SKevin Cernekee 63638c11eaaSHans de Goede static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p, 637ee65d4b3SYunkang Tang struct psmouse *psmouse) 638f85e5001SKevin Cernekee { 639f85e5001SKevin Cernekee f->first_mp = !!(p[4] & 0x40); 640f85e5001SKevin Cernekee f->is_mp = !!(p[0] & 0x40); 641f85e5001SKevin Cernekee 642a839cd57SHans de Goede if (f->is_mp) { 643f85e5001SKevin Cernekee f->fingers = (p[5] & 0x3) + 1; 644f85e5001SKevin Cernekee f->x_map = ((p[4] & 0x7e) << 8) | 645f85e5001SKevin Cernekee ((p[1] & 0x7f) << 2) | 646f85e5001SKevin Cernekee ((p[0] & 0x30) >> 4); 647f85e5001SKevin Cernekee f->y_map = ((p[3] & 0x70) << 4) | 648f85e5001SKevin Cernekee ((p[2] & 0x7f) << 1) | 649f85e5001SKevin Cernekee (p[4] & 0x01); 650a839cd57SHans de Goede } else { 65102d04254SHans de Goede f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) | 652f85e5001SKevin Cernekee ((p[0] & 0x30) >> 4); 65302d04254SHans de Goede f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f); 65402d04254SHans de Goede f->pressure = p[5] & 0x7f; 655f85e5001SKevin Cernekee 656f85e5001SKevin Cernekee alps_decode_buttons_v3(f, p); 657a839cd57SHans de Goede } 65838c11eaaSHans de Goede 65938c11eaaSHans de Goede return 0; 660f85e5001SKevin Cernekee } 661f85e5001SKevin Cernekee 66238c11eaaSHans de Goede static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p, 663ee65d4b3SYunkang Tang struct psmouse *psmouse) 6641302bac3SKevin Cernekee { 665aab9cf7bSHans de Goede f->first_mp = !!(p[4] & 0x40); 666f105e34aSYunkang Tang f->is_mp = !!(p[5] & 0x40); 667aab9cf7bSHans de Goede 668a839cd57SHans de Goede if (f->is_mp) { 669f105e34aSYunkang Tang f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1; 670aab9cf7bSHans de Goede f->x_map = ((p[5] & 0x10) << 11) | 671aab9cf7bSHans de Goede ((p[4] & 0x7e) << 8) | 672aab9cf7bSHans de Goede ((p[1] & 0x7f) << 2) | 673aab9cf7bSHans de Goede ((p[0] & 0x30) >> 4); 674aab9cf7bSHans de Goede f->y_map = ((p[5] & 0x20) << 6) | 675aab9cf7bSHans de Goede ((p[3] & 0x70) << 4) | 676aab9cf7bSHans de Goede ((p[2] & 0x7f) << 1) | 677aab9cf7bSHans de Goede (p[4] & 0x01); 678a839cd57SHans de Goede } else { 679aab9cf7bSHans de Goede f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) | 680aab9cf7bSHans de Goede ((p[0] & 0x30) >> 4); 681aab9cf7bSHans de Goede f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f); 682aab9cf7bSHans de Goede f->pressure = p[5] & 0x7f; 683aab9cf7bSHans de Goede 684aab9cf7bSHans de Goede alps_decode_buttons_v3(f, p); 685a839cd57SHans de Goede } 68638c11eaaSHans de Goede 68738c11eaaSHans de Goede return 0; 6881302bac3SKevin Cernekee } 6891302bac3SKevin Cernekee 69038c11eaaSHans de Goede static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p, 691ee65d4b3SYunkang Tang struct psmouse *psmouse) 69275af9e56SDave Turvene { 693ee65d4b3SYunkang Tang u64 palm_data = 0; 694ee65d4b3SYunkang Tang struct alps_data *priv = psmouse->private; 695ee65d4b3SYunkang Tang 69675af9e56SDave Turvene f->first_mp = !!(p[0] & 0x02); 69775af9e56SDave Turvene f->is_mp = !!(p[0] & 0x20); 69875af9e56SDave Turvene 699ee65d4b3SYunkang Tang if (!f->is_mp) { 70002d04254SHans de Goede f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7)); 70102d04254SHans de Goede f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3)); 70202d04254SHans de Goede f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f; 70375af9e56SDave Turvene alps_decode_buttons_v3(f, p); 704ee65d4b3SYunkang Tang } else { 705ee65d4b3SYunkang Tang f->fingers = ((p[0] & 0x6) >> 1 | 706ee65d4b3SYunkang Tang (p[0] & 0x10) >> 2); 707ee65d4b3SYunkang Tang 708ee65d4b3SYunkang Tang palm_data = (p[1] & 0x7f) | 709ee65d4b3SYunkang Tang ((p[2] & 0x7f) << 7) | 710ee65d4b3SYunkang Tang ((p[4] & 0x7f) << 14) | 711ee65d4b3SYunkang Tang ((p[5] & 0x7f) << 21) | 712ee65d4b3SYunkang Tang ((p[3] & 0x07) << 28) | 713ee65d4b3SYunkang Tang (((u64)p[3] & 0x70) << 27) | 714ee65d4b3SYunkang Tang (((u64)p[0] & 0x01) << 34); 715ee65d4b3SYunkang Tang 716ee65d4b3SYunkang Tang /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */ 717ee65d4b3SYunkang Tang f->y_map = palm_data & (BIT(priv->y_bits) - 1); 718ee65d4b3SYunkang Tang 719ee65d4b3SYunkang Tang /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */ 720ee65d4b3SYunkang Tang f->x_map = (palm_data >> priv->y_bits) & 721ee65d4b3SYunkang Tang (BIT(priv->x_bits) - 1); 722ee65d4b3SYunkang Tang } 72338c11eaaSHans de Goede 72438c11eaaSHans de Goede return 0; 72575af9e56SDave Turvene } 72675af9e56SDave Turvene 727ee65d4b3SYunkang Tang static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse) 72825bded7cSSeth Forshee { 72925bded7cSSeth Forshee struct alps_data *priv = psmouse->private; 73025bded7cSSeth Forshee unsigned char *packet = psmouse->packet; 73125bded7cSSeth Forshee struct input_dev *dev2 = priv->dev2; 73202d04254SHans de Goede struct alps_fields *f = &priv->f; 73302d04254SHans de Goede int fingers = 0; 734f85e5001SKevin Cernekee 73502d04254SHans de Goede memset(f, 0, sizeof(*f)); 73602d04254SHans de Goede 73702d04254SHans de Goede priv->decode_fields(f, packet, psmouse); 73825bded7cSSeth Forshee 73925bded7cSSeth Forshee /* 74001ce661fSSeth Forshee * There's no single feature of touchpad position and bitmap packets 74101ce661fSSeth Forshee * that can be used to distinguish between them. We rely on the fact 74201ce661fSSeth Forshee * that a bitmap packet should always follow a position packet with 74301ce661fSSeth Forshee * bit 6 of packet[4] set. 74425bded7cSSeth Forshee */ 74525bded7cSSeth Forshee if (priv->multi_packet) { 74625bded7cSSeth Forshee /* 74725bded7cSSeth Forshee * Sometimes a position packet will indicate a multi-packet 74825bded7cSSeth Forshee * sequence, but then what follows is another position 74925bded7cSSeth Forshee * packet. Check for this, and when it happens process the 75025bded7cSSeth Forshee * position packet as usual. 75125bded7cSSeth Forshee */ 75202d04254SHans de Goede if (f->is_mp) { 75302d04254SHans de Goede fingers = f->fingers; 75444b77f38SHans de Goede /* 75544b77f38SHans de Goede * Bitmap processing uses position packet's coordinate 75644b77f38SHans de Goede * data, so we need to do decode it first. 75744b77f38SHans de Goede */ 75844b77f38SHans de Goede priv->decode_fields(f, priv->multi_data, psmouse); 75902d04254SHans de Goede if (alps_process_bitmap(priv, f) == 0) 76020bea68bSHans de Goede fingers = 0; /* Use st data */ 76101ce661fSSeth Forshee } else { 76201ce661fSSeth Forshee priv->multi_packet = 0; 76325bded7cSSeth Forshee } 76425bded7cSSeth Forshee } 76525bded7cSSeth Forshee 76601ce661fSSeth Forshee /* 76701ce661fSSeth Forshee * Bit 6 of byte 0 is not usually set in position packets. The only 76801ce661fSSeth Forshee * times it seems to be set is in situations where the data is 76901ce661fSSeth Forshee * suspect anyway, e.g. a palm resting flat on the touchpad. Given 77001ce661fSSeth Forshee * this combined with the fact that this bit is useful for filtering 77101ce661fSSeth Forshee * out misidentified bitmap packets, we reject anything with this 77201ce661fSSeth Forshee * bit set. 77301ce661fSSeth Forshee */ 77402d04254SHans de Goede if (f->is_mp) 77501ce661fSSeth Forshee return; 77601ce661fSSeth Forshee 77702d04254SHans de Goede if (!priv->multi_packet && f->first_mp) { 77825bded7cSSeth Forshee priv->multi_packet = 1; 77901ce661fSSeth Forshee memcpy(priv->multi_data, packet, sizeof(priv->multi_data)); 78001ce661fSSeth Forshee return; 78101ce661fSSeth Forshee } 78201ce661fSSeth Forshee 78325bded7cSSeth Forshee priv->multi_packet = 0; 78425bded7cSSeth Forshee 78525bded7cSSeth Forshee /* 78625bded7cSSeth Forshee * Sometimes the hardware sends a single packet with z = 0 78725bded7cSSeth Forshee * in the middle of a stream. Real releases generate packets 78825bded7cSSeth Forshee * with x, y, and z all zero, so these seem to be flukes. 78925bded7cSSeth Forshee * Ignore them. 79025bded7cSSeth Forshee */ 79102d04254SHans de Goede if (f->st.x && f->st.y && !f->pressure) 79225bded7cSSeth Forshee return; 79325bded7cSSeth Forshee 79468c21870SHans de Goede alps_report_semi_mt_data(psmouse, fingers); 79525bded7cSSeth Forshee 79634412ba2SPali Rohár if ((priv->flags & ALPS_DUALPOINT) && 79734412ba2SPali Rohár !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) { 79802d04254SHans de Goede input_report_key(dev2, BTN_LEFT, f->ts_left); 79902d04254SHans de Goede input_report_key(dev2, BTN_RIGHT, f->ts_right); 80002d04254SHans de Goede input_report_key(dev2, BTN_MIDDLE, f->ts_middle); 80125bded7cSSeth Forshee input_sync(dev2); 80225bded7cSSeth Forshee } 80325bded7cSSeth Forshee } 80425bded7cSSeth Forshee 80525bded7cSSeth Forshee static void alps_process_packet_v3(struct psmouse *psmouse) 80625bded7cSSeth Forshee { 80725bded7cSSeth Forshee unsigned char *packet = psmouse->packet; 80825bded7cSSeth Forshee 80925bded7cSSeth Forshee /* 81025bded7cSSeth Forshee * v3 protocol packets come in three types, two representing 81125bded7cSSeth Forshee * touchpad data and one representing trackstick data. 81225bded7cSSeth Forshee * Trackstick packets seem to be distinguished by always 81325bded7cSSeth Forshee * having 0x3f in the last byte. This value has never been 81425bded7cSSeth Forshee * observed in the last byte of either of the other types 81525bded7cSSeth Forshee * of packets. 81625bded7cSSeth Forshee */ 81725bded7cSSeth Forshee if (packet[5] == 0x3f) { 81825bded7cSSeth Forshee alps_process_trackstick_packet_v3(psmouse); 81925bded7cSSeth Forshee return; 82025bded7cSSeth Forshee } 82125bded7cSSeth Forshee 822ee65d4b3SYunkang Tang alps_process_touchpad_packet_v3_v5(psmouse); 82325bded7cSSeth Forshee } 82425bded7cSSeth Forshee 82595f75e91SYunkang Tang static void alps_process_packet_v6(struct psmouse *psmouse) 82695f75e91SYunkang Tang { 82795f75e91SYunkang Tang struct alps_data *priv = psmouse->private; 82895f75e91SYunkang Tang unsigned char *packet = psmouse->packet; 82995f75e91SYunkang Tang struct input_dev *dev = psmouse->dev; 83095f75e91SYunkang Tang struct input_dev *dev2 = priv->dev2; 8311ef85805SDmitry Torokhov int x, y, z; 83295f75e91SYunkang Tang 83395f75e91SYunkang Tang /* 83495f75e91SYunkang Tang * We can use Byte5 to distinguish if the packet is from Touchpad 83595f75e91SYunkang Tang * or Trackpoint. 83695f75e91SYunkang Tang * Touchpad: 0 - 0x7E 83795f75e91SYunkang Tang * Trackpoint: 0x7F 83895f75e91SYunkang Tang */ 83995f75e91SYunkang Tang if (packet[5] == 0x7F) { 84095f75e91SYunkang Tang /* It should be a DualPoint when received Trackpoint packet */ 84134412ba2SPali Rohár if (!(priv->flags & ALPS_DUALPOINT)) { 84234412ba2SPali Rohár psmouse_warn(psmouse, 84334412ba2SPali Rohár "Rejected trackstick packet from non DualPoint device"); 84495f75e91SYunkang Tang return; 84534412ba2SPali Rohár } 84695f75e91SYunkang Tang 84795f75e91SYunkang Tang /* Trackpoint packet */ 84895f75e91SYunkang Tang x = packet[1] | ((packet[3] & 0x20) << 2); 84995f75e91SYunkang Tang y = packet[2] | ((packet[3] & 0x40) << 1); 85095f75e91SYunkang Tang z = packet[4]; 85195f75e91SYunkang Tang 85295f75e91SYunkang Tang /* To prevent the cursor jump when finger lifted */ 85395f75e91SYunkang Tang if (x == 0x7F && y == 0x7F && z == 0x7F) 85495f75e91SYunkang Tang x = y = z = 0; 85595f75e91SYunkang Tang 85695f75e91SYunkang Tang /* Divide 4 since trackpoint's speed is too fast */ 85795f75e91SYunkang Tang input_report_rel(dev2, REL_X, (char)x / 4); 85895f75e91SYunkang Tang input_report_rel(dev2, REL_Y, -((char)y / 4)); 85995f75e91SYunkang Tang 8601ef85805SDmitry Torokhov psmouse_report_standard_buttons(dev2, packet[3]); 86195f75e91SYunkang Tang 86295f75e91SYunkang Tang input_sync(dev2); 86395f75e91SYunkang Tang return; 86495f75e91SYunkang Tang } 86595f75e91SYunkang Tang 86695f75e91SYunkang Tang /* Touchpad packet */ 86795f75e91SYunkang Tang x = packet[1] | ((packet[3] & 0x78) << 4); 86895f75e91SYunkang Tang y = packet[2] | ((packet[4] & 0x78) << 4); 86995f75e91SYunkang Tang z = packet[5]; 87095f75e91SYunkang Tang 87195f75e91SYunkang Tang if (z > 30) 87295f75e91SYunkang Tang input_report_key(dev, BTN_TOUCH, 1); 87395f75e91SYunkang Tang if (z < 25) 87495f75e91SYunkang Tang input_report_key(dev, BTN_TOUCH, 0); 87595f75e91SYunkang Tang 87695f75e91SYunkang Tang if (z > 0) { 87795f75e91SYunkang Tang input_report_abs(dev, ABS_X, x); 87895f75e91SYunkang Tang input_report_abs(dev, ABS_Y, y); 87995f75e91SYunkang Tang } 88095f75e91SYunkang Tang 88195f75e91SYunkang Tang input_report_abs(dev, ABS_PRESSURE, z); 88295f75e91SYunkang Tang input_report_key(dev, BTN_TOOL_FINGER, z > 0); 88395f75e91SYunkang Tang 88495f75e91SYunkang Tang /* v6 touchpad does not have middle button */ 8851ef85805SDmitry Torokhov packet[3] &= ~BIT(2); 8861ef85805SDmitry Torokhov psmouse_report_standard_buttons(dev2, packet[3]); 88795f75e91SYunkang Tang 88895f75e91SYunkang Tang input_sync(dev); 88995f75e91SYunkang Tang } 89095f75e91SYunkang Tang 89125bded7cSSeth Forshee static void alps_process_packet_v4(struct psmouse *psmouse) 89225bded7cSSeth Forshee { 8933b7e09faSGeorge Pantalos struct alps_data *priv = psmouse->private; 89425bded7cSSeth Forshee unsigned char *packet = psmouse->packet; 89502d04254SHans de Goede struct alps_fields *f = &priv->f; 89668c21870SHans de Goede int offset; 8973b7e09faSGeorge Pantalos 8983b7e09faSGeorge Pantalos /* 8993b7e09faSGeorge Pantalos * v4 has a 6-byte encoding for bitmap data, but this data is 9003b7e09faSGeorge Pantalos * broken up between 3 normal packets. Use priv->multi_packet to 9013b7e09faSGeorge Pantalos * track our position in the bitmap packet. 9023b7e09faSGeorge Pantalos */ 9033b7e09faSGeorge Pantalos if (packet[6] & 0x40) { 9043b7e09faSGeorge Pantalos /* sync, reset position */ 9053b7e09faSGeorge Pantalos priv->multi_packet = 0; 9063b7e09faSGeorge Pantalos } 9073b7e09faSGeorge Pantalos 9083b7e09faSGeorge Pantalos if (WARN_ON_ONCE(priv->multi_packet > 2)) 9093b7e09faSGeorge Pantalos return; 9103b7e09faSGeorge Pantalos 9113b7e09faSGeorge Pantalos offset = 2 * priv->multi_packet; 9123b7e09faSGeorge Pantalos priv->multi_data[offset] = packet[6]; 9133b7e09faSGeorge Pantalos priv->multi_data[offset + 1] = packet[7]; 9143b7e09faSGeorge Pantalos 91544b77f38SHans de Goede f->left = !!(packet[4] & 0x01); 91644b77f38SHans de Goede f->right = !!(packet[4] & 0x02); 91744b77f38SHans de Goede 91844b77f38SHans de Goede f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) | 91944b77f38SHans de Goede ((packet[0] & 0x30) >> 4); 92044b77f38SHans de Goede f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f); 92144b77f38SHans de Goede f->pressure = packet[5] & 0x7f; 92244b77f38SHans de Goede 9233b7e09faSGeorge Pantalos if (++priv->multi_packet > 2) { 9243b7e09faSGeorge Pantalos priv->multi_packet = 0; 9253b7e09faSGeorge Pantalos 92602d04254SHans de Goede f->x_map = ((priv->multi_data[2] & 0x1f) << 10) | 9273b7e09faSGeorge Pantalos ((priv->multi_data[3] & 0x60) << 3) | 9283b7e09faSGeorge Pantalos ((priv->multi_data[0] & 0x3f) << 2) | 9293b7e09faSGeorge Pantalos ((priv->multi_data[1] & 0x60) >> 5); 93002d04254SHans de Goede f->y_map = ((priv->multi_data[5] & 0x01) << 10) | 9313b7e09faSGeorge Pantalos ((priv->multi_data[3] & 0x1f) << 5) | 9323b7e09faSGeorge Pantalos (priv->multi_data[1] & 0x1f); 9333b7e09faSGeorge Pantalos 93402d04254SHans de Goede f->fingers = alps_process_bitmap(priv, f); 9353b7e09faSGeorge Pantalos } 93625bded7cSSeth Forshee 93768c21870SHans de Goede alps_report_semi_mt_data(psmouse, f->fingers); 93825bded7cSSeth Forshee } 93925bded7cSSeth Forshee 9403808843cSYunkang Tang static bool alps_is_valid_package_v7(struct psmouse *psmouse) 9413808843cSYunkang Tang { 9423808843cSYunkang Tang switch (psmouse->pktcnt) { 9433808843cSYunkang Tang case 3: 9443808843cSYunkang Tang return (psmouse->packet[2] & 0x40) == 0x40; 9453808843cSYunkang Tang case 4: 9463808843cSYunkang Tang return (psmouse->packet[3] & 0x48) == 0x48; 9473808843cSYunkang Tang case 6: 9483808843cSYunkang Tang return (psmouse->packet[5] & 0x40) == 0x00; 9493808843cSYunkang Tang } 9503808843cSYunkang Tang return true; 9513808843cSYunkang Tang } 9523808843cSYunkang Tang 9533808843cSYunkang Tang static unsigned char alps_get_packet_id_v7(char *byte) 9543808843cSYunkang Tang { 9553808843cSYunkang Tang unsigned char packet_id; 9563808843cSYunkang Tang 9573808843cSYunkang Tang if (byte[4] & 0x40) 9583808843cSYunkang Tang packet_id = V7_PACKET_ID_TWO; 9593808843cSYunkang Tang else if (byte[4] & 0x01) 9603808843cSYunkang Tang packet_id = V7_PACKET_ID_MULTI; 9613808843cSYunkang Tang else if ((byte[0] & 0x10) && !(byte[4] & 0x43)) 9623808843cSYunkang Tang packet_id = V7_PACKET_ID_NEW; 9633808843cSYunkang Tang else if (byte[1] == 0x00 && byte[4] == 0x00) 9643808843cSYunkang Tang packet_id = V7_PACKET_ID_IDLE; 9653808843cSYunkang Tang else 9663808843cSYunkang Tang packet_id = V7_PACKET_ID_UNKNOWN; 9673808843cSYunkang Tang 9683808843cSYunkang Tang return packet_id; 9693808843cSYunkang Tang } 9703808843cSYunkang Tang 9713808843cSYunkang Tang static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt, 9723808843cSYunkang Tang unsigned char *pkt, 9733808843cSYunkang Tang unsigned char pkt_id) 9743808843cSYunkang Tang { 9753808843cSYunkang Tang mt[0].x = ((pkt[2] & 0x80) << 4); 9763808843cSYunkang Tang mt[0].x |= ((pkt[2] & 0x3F) << 5); 9773808843cSYunkang Tang mt[0].x |= ((pkt[3] & 0x30) >> 1); 9783808843cSYunkang Tang mt[0].x |= (pkt[3] & 0x07); 9793808843cSYunkang Tang mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07); 9803808843cSYunkang Tang 9813808843cSYunkang Tang mt[1].x = ((pkt[3] & 0x80) << 4); 9823808843cSYunkang Tang mt[1].x |= ((pkt[4] & 0x80) << 3); 9833808843cSYunkang Tang mt[1].x |= ((pkt[4] & 0x3F) << 4); 9843808843cSYunkang Tang mt[1].y = ((pkt[5] & 0x80) << 3); 9853808843cSYunkang Tang mt[1].y |= ((pkt[5] & 0x3F) << 4); 9863808843cSYunkang Tang 9873808843cSYunkang Tang switch (pkt_id) { 9883808843cSYunkang Tang case V7_PACKET_ID_TWO: 9893808843cSYunkang Tang mt[1].x &= ~0x000F; 9903808843cSYunkang Tang mt[1].y |= 0x000F; 99172eceab7SHans de Goede /* Detect false-postive touches where x & y report max value */ 99272eceab7SHans de Goede if (mt[1].y == 0x7ff && mt[1].x == 0xff0) { 99372eceab7SHans de Goede mt[1].x = 0; 99472eceab7SHans de Goede /* y gets set to 0 at the end of this function */ 99572eceab7SHans de Goede } 9963808843cSYunkang Tang break; 9973808843cSYunkang Tang 9983808843cSYunkang Tang case V7_PACKET_ID_MULTI: 9993808843cSYunkang Tang mt[1].x &= ~0x003F; 10003808843cSYunkang Tang mt[1].y &= ~0x0020; 10013808843cSYunkang Tang mt[1].y |= ((pkt[4] & 0x02) << 4); 10023808843cSYunkang Tang mt[1].y |= 0x001F; 10033808843cSYunkang Tang break; 10043808843cSYunkang Tang 10053808843cSYunkang Tang case V7_PACKET_ID_NEW: 10063808843cSYunkang Tang mt[1].x &= ~0x003F; 10073808843cSYunkang Tang mt[1].x |= (pkt[0] & 0x20); 10083808843cSYunkang Tang mt[1].y |= 0x000F; 10093808843cSYunkang Tang break; 10103808843cSYunkang Tang } 10113808843cSYunkang Tang 10123808843cSYunkang Tang mt[0].y = 0x7FF - mt[0].y; 10133808843cSYunkang Tang mt[1].y = 0x7FF - mt[1].y; 10143808843cSYunkang Tang } 10153808843cSYunkang Tang 10163808843cSYunkang Tang static int alps_get_mt_count(struct input_mt_pos *mt) 10173808843cSYunkang Tang { 10187091c443SHans de Goede int i, fingers = 0; 10193808843cSYunkang Tang 10207091c443SHans de Goede for (i = 0; i < MAX_TOUCHES; i++) { 10217091c443SHans de Goede if (mt[i].x != 0 || mt[i].y != 0) 10227091c443SHans de Goede fingers++; 10237091c443SHans de Goede } 10243808843cSYunkang Tang 10257091c443SHans de Goede return fingers; 10263808843cSYunkang Tang } 10273808843cSYunkang Tang 10283808843cSYunkang Tang static int alps_decode_packet_v7(struct alps_fields *f, 10293808843cSYunkang Tang unsigned char *p, 10303808843cSYunkang Tang struct psmouse *psmouse) 10313808843cSYunkang Tang { 1032d27eb793SHans de Goede struct alps_data *priv = psmouse->private; 10333808843cSYunkang Tang unsigned char pkt_id; 10343808843cSYunkang Tang 10353808843cSYunkang Tang pkt_id = alps_get_packet_id_v7(p); 10363808843cSYunkang Tang if (pkt_id == V7_PACKET_ID_IDLE) 10373808843cSYunkang Tang return 0; 10383808843cSYunkang Tang if (pkt_id == V7_PACKET_ID_UNKNOWN) 10393808843cSYunkang Tang return -1; 10408b238115SHans de Goede /* 10418b238115SHans de Goede * NEW packets are send to indicate a discontinuity in the finger 10428b238115SHans de Goede * coordinate reporting. Specifically a finger may have moved from 10438b238115SHans de Goede * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for 10448b238115SHans de Goede * us. 10458b238115SHans de Goede * 10468b238115SHans de Goede * NEW packets have 3 problems: 10478b238115SHans de Goede * 1) They do not contain middle / right button info (on non clickpads) 10488b238115SHans de Goede * this can be worked around by preserving the old button state 10498b238115SHans de Goede * 2) They do not contain an accurate fingercount, and they are 10508b238115SHans de Goede * typically send when the number of fingers changes. We cannot use 10518b238115SHans de Goede * the old finger count as that may mismatch with the amount of 10528b238115SHans de Goede * touch coordinates we've available in the NEW packet 10538b238115SHans de Goede * 3) Their x data for the second touch is inaccurate leading to 10548b238115SHans de Goede * a possible jump of the x coordinate by 16 units when the first 10558b238115SHans de Goede * non NEW packet comes in 10568b238115SHans de Goede * Since problems 2 & 3 cannot be worked around, just ignore them. 10578b238115SHans de Goede */ 10588b238115SHans de Goede if (pkt_id == V7_PACKET_ID_NEW) 10598b238115SHans de Goede return 1; 10603808843cSYunkang Tang 10613808843cSYunkang Tang alps_get_finger_coordinate_v7(f->mt, p, pkt_id); 10623808843cSYunkang Tang 10633808843cSYunkang Tang if (pkt_id == V7_PACKET_ID_TWO) 10643808843cSYunkang Tang f->fingers = alps_get_mt_count(f->mt); 10658b238115SHans de Goede else /* pkt_id == V7_PACKET_ID_MULTI */ 10663808843cSYunkang Tang f->fingers = 3 + (p[5] & 0x03); 10673808843cSYunkang Tang 1068d27eb793SHans de Goede f->left = (p[0] & 0x80) >> 7; 1069d27eb793SHans de Goede if (priv->flags & ALPS_BUTTONPAD) { 1070d27eb793SHans de Goede if (p[0] & 0x20) 1071d27eb793SHans de Goede f->fingers++; 1072d27eb793SHans de Goede if (p[0] & 0x10) 1073d27eb793SHans de Goede f->fingers++; 1074d27eb793SHans de Goede } else { 1075d27eb793SHans de Goede f->right = (p[0] & 0x20) >> 5; 1076d27eb793SHans de Goede f->middle = (p[0] & 0x10) >> 4; 1077d27eb793SHans de Goede } 1078d27eb793SHans de Goede 10797091c443SHans de Goede /* Sometimes a single touch is reported in mt[1] rather then mt[0] */ 10807091c443SHans de Goede if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) { 10817091c443SHans de Goede f->mt[0].x = f->mt[1].x; 10827091c443SHans de Goede f->mt[0].y = f->mt[1].y; 10837091c443SHans de Goede f->mt[1].x = 0; 10847091c443SHans de Goede f->mt[1].y = 0; 10857091c443SHans de Goede } 10867091c443SHans de Goede 10873808843cSYunkang Tang return 0; 10883808843cSYunkang Tang } 10893808843cSYunkang Tang 10903808843cSYunkang Tang static void alps_process_trackstick_packet_v7(struct psmouse *psmouse) 10913808843cSYunkang Tang { 10923808843cSYunkang Tang struct alps_data *priv = psmouse->private; 10933808843cSYunkang Tang unsigned char *packet = psmouse->packet; 10943808843cSYunkang Tang struct input_dev *dev2 = priv->dev2; 10951ef85805SDmitry Torokhov int x, y, z; 10963808843cSYunkang Tang 109734412ba2SPali Rohár /* It should be a DualPoint when received trackstick packet */ 109834412ba2SPali Rohár if (!(priv->flags & ALPS_DUALPOINT)) { 109934412ba2SPali Rohár psmouse_warn(psmouse, 110034412ba2SPali Rohár "Rejected trackstick packet from non DualPoint device"); 110134412ba2SPali Rohár return; 110234412ba2SPali Rohár } 110334412ba2SPali Rohár 11043808843cSYunkang Tang x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2); 11053808843cSYunkang Tang y = (packet[3] & 0x07) | (packet[4] & 0xb8) | 11063808843cSYunkang Tang ((packet[3] & 0x20) << 1); 11073808843cSYunkang Tang z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1); 11083808843cSYunkang Tang 1109088df2ccSHans de Goede input_report_rel(dev2, REL_X, (char)x); 1110088df2ccSHans de Goede input_report_rel(dev2, REL_Y, -((char)y)); 11114621c966SPali Rohár input_report_abs(dev2, ABS_PRESSURE, z); 11123808843cSYunkang Tang 11131ef85805SDmitry Torokhov psmouse_report_standard_buttons(dev2, packet[1]); 11143808843cSYunkang Tang 11153808843cSYunkang Tang input_sync(dev2); 11163808843cSYunkang Tang } 11173808843cSYunkang Tang 11183808843cSYunkang Tang static void alps_process_touchpad_packet_v7(struct psmouse *psmouse) 11193808843cSYunkang Tang { 11203808843cSYunkang Tang struct alps_data *priv = psmouse->private; 11213808843cSYunkang Tang struct input_dev *dev = psmouse->dev; 11223808843cSYunkang Tang struct alps_fields *f = &priv->f; 11233808843cSYunkang Tang 11243808843cSYunkang Tang memset(f, 0, sizeof(*f)); 11253808843cSYunkang Tang 11263808843cSYunkang Tang if (priv->decode_fields(f, psmouse->packet, psmouse)) 11273808843cSYunkang Tang return; 11283808843cSYunkang Tang 11293808843cSYunkang Tang alps_report_mt_data(psmouse, alps_get_mt_count(f->mt)); 11303808843cSYunkang Tang 11313808843cSYunkang Tang input_mt_report_finger_count(dev, f->fingers); 11323808843cSYunkang Tang 11333808843cSYunkang Tang input_report_key(dev, BTN_LEFT, f->left); 11343808843cSYunkang Tang input_report_key(dev, BTN_RIGHT, f->right); 11353808843cSYunkang Tang input_report_key(dev, BTN_MIDDLE, f->middle); 11363808843cSYunkang Tang 11373808843cSYunkang Tang input_sync(dev); 11383808843cSYunkang Tang } 11393808843cSYunkang Tang 11403808843cSYunkang Tang static void alps_process_packet_v7(struct psmouse *psmouse) 11413808843cSYunkang Tang { 11423808843cSYunkang Tang unsigned char *packet = psmouse->packet; 11433808843cSYunkang Tang 11443808843cSYunkang Tang if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06) 11453808843cSYunkang Tang alps_process_trackstick_packet_v7(psmouse); 11463808843cSYunkang Tang else 11473808843cSYunkang Tang alps_process_touchpad_packet_v7(psmouse); 11483808843cSYunkang Tang } 11493808843cSYunkang Tang 115023fce365SPaul Donohue static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte) 11513db5b9f7SMasaki Ota { 115223fce365SPaul Donohue enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE; 11533db5b9f7SMasaki Ota 11544777ac22SBen Gamari switch (byte[3] & 0x30) { 11554777ac22SBen Gamari case 0x00: 115623fce365SPaul Donohue if (SS4_IS_IDLE_V2(byte)) { 11573db5b9f7SMasaki Ota pkt_id = SS4_PACKET_ID_IDLE; 11583db5b9f7SMasaki Ota } else { 11594777ac22SBen Gamari pkt_id = SS4_PACKET_ID_ONE; 11604777ac22SBen Gamari } 11614777ac22SBen Gamari break; 11624777ac22SBen Gamari case 0x10: 11634777ac22SBen Gamari /* two-finger finger positions */ 11644777ac22SBen Gamari pkt_id = SS4_PACKET_ID_TWO; 11654777ac22SBen Gamari break; 11664777ac22SBen Gamari case 0x20: 11674777ac22SBen Gamari /* stick pointer */ 11684777ac22SBen Gamari pkt_id = SS4_PACKET_ID_STICK; 11694777ac22SBen Gamari break; 11704777ac22SBen Gamari case 0x30: 11714777ac22SBen Gamari /* third and fourth finger positions */ 11723db5b9f7SMasaki Ota pkt_id = SS4_PACKET_ID_MULTI; 11734777ac22SBen Gamari break; 11743db5b9f7SMasaki Ota } 11753db5b9f7SMasaki Ota 11763db5b9f7SMasaki Ota return pkt_id; 11773db5b9f7SMasaki Ota } 11783db5b9f7SMasaki Ota 11793db5b9f7SMasaki Ota static int alps_decode_ss4_v2(struct alps_fields *f, 11803db5b9f7SMasaki Ota unsigned char *p, struct psmouse *psmouse) 11813db5b9f7SMasaki Ota { 11823db5b9f7SMasaki Ota struct alps_data *priv = psmouse->private; 118323fce365SPaul Donohue enum SS4_PACKET_ID pkt_id; 11843db5b9f7SMasaki Ota unsigned int no_data_x, no_data_y; 11853db5b9f7SMasaki Ota 11863db5b9f7SMasaki Ota pkt_id = alps_get_pkt_id_ss4_v2(p); 11873db5b9f7SMasaki Ota 11883db5b9f7SMasaki Ota /* Current packet is 1Finger coordinate packet */ 11893db5b9f7SMasaki Ota switch (pkt_id) { 11903db5b9f7SMasaki Ota case SS4_PACKET_ID_ONE: 11913db5b9f7SMasaki Ota f->mt[0].x = SS4_1F_X_V2(p); 11923db5b9f7SMasaki Ota f->mt[0].y = SS4_1F_Y_V2(p); 11933db5b9f7SMasaki Ota f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f; 1194a8317763SBen Gamari /* 1195a8317763SBen Gamari * When a button is held the device will give us events 1196a8317763SBen Gamari * with x, y, and pressure of 0. This causes annoying jumps 1197a8317763SBen Gamari * if a touch is released while the button is held. 1198a8317763SBen Gamari * Handle this by claiming zero contacts. 1199a8317763SBen Gamari */ 1200a8317763SBen Gamari f->fingers = f->pressure > 0 ? 1 : 0; 12013db5b9f7SMasaki Ota f->first_mp = 0; 12023db5b9f7SMasaki Ota f->is_mp = 0; 12033db5b9f7SMasaki Ota break; 12043db5b9f7SMasaki Ota 12053db5b9f7SMasaki Ota case SS4_PACKET_ID_TWO: 12063db5b9f7SMasaki Ota if (priv->flags & ALPS_BUTTONPAD) { 12074a646580SMasaki Ota if (IS_SS4PLUS_DEV(priv->dev_id)) { 12084a646580SMasaki Ota f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0); 12094a646580SMasaki Ota f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1); 12104a646580SMasaki Ota } else { 12113db5b9f7SMasaki Ota f->mt[0].x = SS4_BTL_MF_X_V2(p, 0); 12123db5b9f7SMasaki Ota f->mt[1].x = SS4_BTL_MF_X_V2(p, 1); 12134a646580SMasaki Ota } 12144a646580SMasaki Ota f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0); 12153db5b9f7SMasaki Ota f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1); 12163db5b9f7SMasaki Ota } else { 12174a646580SMasaki Ota if (IS_SS4PLUS_DEV(priv->dev_id)) { 12184a646580SMasaki Ota f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0); 12194a646580SMasaki Ota f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1); 12204a646580SMasaki Ota } else { 12213db5b9f7SMasaki Ota f->mt[0].x = SS4_STD_MF_X_V2(p, 0); 12223db5b9f7SMasaki Ota f->mt[1].x = SS4_STD_MF_X_V2(p, 1); 12234a646580SMasaki Ota } 12244a646580SMasaki Ota f->mt[0].y = SS4_STD_MF_Y_V2(p, 0); 12253db5b9f7SMasaki Ota f->mt[1].y = SS4_STD_MF_Y_V2(p, 1); 12263db5b9f7SMasaki Ota } 12273db5b9f7SMasaki Ota f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0; 12283db5b9f7SMasaki Ota 12293db5b9f7SMasaki Ota if (SS4_IS_MF_CONTINUE(p)) { 12303db5b9f7SMasaki Ota f->first_mp = 1; 12313db5b9f7SMasaki Ota } else { 12323db5b9f7SMasaki Ota f->fingers = 2; 12333db5b9f7SMasaki Ota f->first_mp = 0; 12343db5b9f7SMasaki Ota } 12353db5b9f7SMasaki Ota f->is_mp = 0; 12363db5b9f7SMasaki Ota 12373db5b9f7SMasaki Ota break; 12383db5b9f7SMasaki Ota 12393db5b9f7SMasaki Ota case SS4_PACKET_ID_MULTI: 12403db5b9f7SMasaki Ota if (priv->flags & ALPS_BUTTONPAD) { 12414a646580SMasaki Ota if (IS_SS4PLUS_DEV(priv->dev_id)) { 12424d94e776SNir Perry f->mt[2].x = SS4_PLUS_BTL_MF_X_V2(p, 0); 12434d94e776SNir Perry f->mt[3].x = SS4_PLUS_BTL_MF_X_V2(p, 1); 12444d94e776SNir Perry no_data_x = SS4_PLUS_MFPACKET_NO_AX_BL; 12454a646580SMasaki Ota } else { 12463db5b9f7SMasaki Ota f->mt[2].x = SS4_BTL_MF_X_V2(p, 0); 12473db5b9f7SMasaki Ota f->mt[3].x = SS4_BTL_MF_X_V2(p, 1); 12484d94e776SNir Perry no_data_x = SS4_MFPACKET_NO_AX_BL; 12494a646580SMasaki Ota } 12504d94e776SNir Perry no_data_y = SS4_MFPACKET_NO_AY_BL; 12514a646580SMasaki Ota 12524a646580SMasaki Ota f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0); 12533db5b9f7SMasaki Ota f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1); 12543db5b9f7SMasaki Ota } else { 12554a646580SMasaki Ota if (IS_SS4PLUS_DEV(priv->dev_id)) { 12564d94e776SNir Perry f->mt[2].x = SS4_PLUS_STD_MF_X_V2(p, 0); 12574d94e776SNir Perry f->mt[3].x = SS4_PLUS_STD_MF_X_V2(p, 1); 12584d94e776SNir Perry no_data_x = SS4_PLUS_MFPACKET_NO_AX; 12594a646580SMasaki Ota } else { 12604d94e776SNir Perry f->mt[2].x = SS4_STD_MF_X_V2(p, 0); 12614d94e776SNir Perry f->mt[3].x = SS4_STD_MF_X_V2(p, 1); 12624d94e776SNir Perry no_data_x = SS4_MFPACKET_NO_AX; 12634a646580SMasaki Ota } 12644d94e776SNir Perry no_data_y = SS4_MFPACKET_NO_AY; 12654d94e776SNir Perry 12663db5b9f7SMasaki Ota f->mt[2].y = SS4_STD_MF_Y_V2(p, 0); 12673db5b9f7SMasaki Ota f->mt[3].y = SS4_STD_MF_Y_V2(p, 1); 12683db5b9f7SMasaki Ota } 12693db5b9f7SMasaki Ota 12703db5b9f7SMasaki Ota f->first_mp = 0; 12713db5b9f7SMasaki Ota f->is_mp = 1; 12723db5b9f7SMasaki Ota 12733db5b9f7SMasaki Ota if (SS4_IS_5F_DETECTED(p)) { 12743db5b9f7SMasaki Ota f->fingers = 5; 12753db5b9f7SMasaki Ota } else if (f->mt[3].x == no_data_x && 12763db5b9f7SMasaki Ota f->mt[3].y == no_data_y) { 12773db5b9f7SMasaki Ota f->mt[3].x = 0; 12783db5b9f7SMasaki Ota f->mt[3].y = 0; 12793db5b9f7SMasaki Ota f->fingers = 3; 12803db5b9f7SMasaki Ota } else { 12813db5b9f7SMasaki Ota f->fingers = 4; 12823db5b9f7SMasaki Ota } 12833db5b9f7SMasaki Ota break; 12843db5b9f7SMasaki Ota 12854777ac22SBen Gamari case SS4_PACKET_ID_STICK: 12867229c58cSPaul Donohue /* 12877229c58cSPaul Donohue * x, y, and pressure are decoded in 12887229c58cSPaul Donohue * alps_process_packet_ss4_v2() 12897229c58cSPaul Donohue */ 12907229c58cSPaul Donohue f->first_mp = 0; 12917229c58cSPaul Donohue f->is_mp = 0; 12924777ac22SBen Gamari break; 12934777ac22SBen Gamari 12943db5b9f7SMasaki Ota case SS4_PACKET_ID_IDLE: 12953db5b9f7SMasaki Ota default: 12963db5b9f7SMasaki Ota memset(f, 0, sizeof(struct alps_fields)); 12973db5b9f7SMasaki Ota break; 12983db5b9f7SMasaki Ota } 12993db5b9f7SMasaki Ota 13004777ac22SBen Gamari /* handle buttons */ 13014777ac22SBen Gamari if (pkt_id == SS4_PACKET_ID_STICK) { 13024777ac22SBen Gamari f->ts_left = !!(SS4_BTN_V2(p) & 0x01); 13034777ac22SBen Gamari f->ts_right = !!(SS4_BTN_V2(p) & 0x02); 13044777ac22SBen Gamari f->ts_middle = !!(SS4_BTN_V2(p) & 0x04); 13054777ac22SBen Gamari } else { 13063db5b9f7SMasaki Ota f->left = !!(SS4_BTN_V2(p) & 0x01); 13073db5b9f7SMasaki Ota if (!(priv->flags & ALPS_BUTTONPAD)) { 13083db5b9f7SMasaki Ota f->right = !!(SS4_BTN_V2(p) & 0x02); 13093db5b9f7SMasaki Ota f->middle = !!(SS4_BTN_V2(p) & 0x04); 13103db5b9f7SMasaki Ota } 13114777ac22SBen Gamari } 13123db5b9f7SMasaki Ota 13133db5b9f7SMasaki Ota return 0; 13143db5b9f7SMasaki Ota } 13153db5b9f7SMasaki Ota 13163db5b9f7SMasaki Ota static void alps_process_packet_ss4_v2(struct psmouse *psmouse) 13173db5b9f7SMasaki Ota { 13183db5b9f7SMasaki Ota struct alps_data *priv = psmouse->private; 13193db5b9f7SMasaki Ota unsigned char *packet = psmouse->packet; 13203db5b9f7SMasaki Ota struct input_dev *dev = psmouse->dev; 13214777ac22SBen Gamari struct input_dev *dev2 = priv->dev2; 13223db5b9f7SMasaki Ota struct alps_fields *f = &priv->f; 13233db5b9f7SMasaki Ota 13243db5b9f7SMasaki Ota memset(f, 0, sizeof(struct alps_fields)); 13253db5b9f7SMasaki Ota priv->decode_fields(f, packet, psmouse); 13263db5b9f7SMasaki Ota if (priv->multi_packet) { 13273db5b9f7SMasaki Ota /* 13283db5b9f7SMasaki Ota * Sometimes the first packet will indicate a multi-packet 13293db5b9f7SMasaki Ota * sequence, but sometimes the next multi-packet would not 13303db5b9f7SMasaki Ota * come. Check for this, and when it happens process the 13313db5b9f7SMasaki Ota * position packet as usual. 13323db5b9f7SMasaki Ota */ 13333db5b9f7SMasaki Ota if (f->is_mp) { 13343db5b9f7SMasaki Ota /* Now process the 1st packet */ 13353db5b9f7SMasaki Ota priv->decode_fields(f, priv->multi_data, psmouse); 13363db5b9f7SMasaki Ota } else { 13373db5b9f7SMasaki Ota priv->multi_packet = 0; 13383db5b9f7SMasaki Ota } 13393db5b9f7SMasaki Ota } 13403db5b9f7SMasaki Ota 13413db5b9f7SMasaki Ota /* 13423db5b9f7SMasaki Ota * "f.is_mp" would always be '0' after merging the 1st and 2nd packet. 13433db5b9f7SMasaki Ota * When it is set, it means 2nd packet comes without 1st packet come. 13443db5b9f7SMasaki Ota */ 13453db5b9f7SMasaki Ota if (f->is_mp) 13463db5b9f7SMasaki Ota return; 13473db5b9f7SMasaki Ota 13483db5b9f7SMasaki Ota /* Save the first packet */ 13493db5b9f7SMasaki Ota if (!priv->multi_packet && f->first_mp) { 13503db5b9f7SMasaki Ota priv->multi_packet = 1; 13513db5b9f7SMasaki Ota memcpy(priv->multi_data, packet, sizeof(priv->multi_data)); 13523db5b9f7SMasaki Ota return; 13533db5b9f7SMasaki Ota } 13543db5b9f7SMasaki Ota 13553db5b9f7SMasaki Ota priv->multi_packet = 0; 13563db5b9f7SMasaki Ota 1357864db929SPaul Donohue /* Report trackstick */ 1358864db929SPaul Donohue if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) { 13597229c58cSPaul Donohue if (!(priv->flags & ALPS_DUALPOINT)) { 13607229c58cSPaul Donohue psmouse_warn(psmouse, 13617229c58cSPaul Donohue "Rejected trackstick packet from non DualPoint device"); 13627229c58cSPaul Donohue return; 13637229c58cSPaul Donohue } 13647229c58cSPaul Donohue 136523fce365SPaul Donohue input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet)); 136623fce365SPaul Donohue input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet)); 136723fce365SPaul Donohue input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet)); 13687229c58cSPaul Donohue 1369864db929SPaul Donohue input_report_key(dev2, BTN_LEFT, f->ts_left); 1370864db929SPaul Donohue input_report_key(dev2, BTN_RIGHT, f->ts_right); 1371864db929SPaul Donohue input_report_key(dev2, BTN_MIDDLE, f->ts_middle); 13727229c58cSPaul Donohue 1373864db929SPaul Donohue input_sync(dev2); 1374864db929SPaul Donohue return; 1375864db929SPaul Donohue } 1376864db929SPaul Donohue 1377864db929SPaul Donohue /* Report touchpad */ 13783db5b9f7SMasaki Ota alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4); 13793db5b9f7SMasaki Ota 13803db5b9f7SMasaki Ota input_mt_report_finger_count(dev, f->fingers); 13813db5b9f7SMasaki Ota 13823db5b9f7SMasaki Ota input_report_key(dev, BTN_LEFT, f->left); 13833db5b9f7SMasaki Ota input_report_key(dev, BTN_RIGHT, f->right); 13843db5b9f7SMasaki Ota input_report_key(dev, BTN_MIDDLE, f->middle); 13853db5b9f7SMasaki Ota 13863db5b9f7SMasaki Ota input_report_abs(dev, ABS_PRESSURE, f->pressure); 13873db5b9f7SMasaki Ota input_sync(dev); 13883db5b9f7SMasaki Ota } 13893db5b9f7SMasaki Ota 13903db5b9f7SMasaki Ota static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse) 13913db5b9f7SMasaki Ota { 13923db5b9f7SMasaki Ota if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08)) 13933db5b9f7SMasaki Ota return false; 13943db5b9f7SMasaki Ota if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0)) 13953db5b9f7SMasaki Ota return false; 13963db5b9f7SMasaki Ota return true; 13973db5b9f7SMasaki Ota } 13983db5b9f7SMasaki Ota 139904aae283SPali Rohár static DEFINE_MUTEX(alps_mutex); 140004aae283SPali Rohár 140104aae283SPali Rohár static void alps_register_bare_ps2_mouse(struct work_struct *work) 140204aae283SPali Rohár { 140304aae283SPali Rohár struct alps_data *priv = 140404aae283SPali Rohár container_of(work, struct alps_data, dev3_register_work.work); 140504aae283SPali Rohár struct psmouse *psmouse = priv->psmouse; 140604aae283SPali Rohár struct input_dev *dev3; 140704aae283SPali Rohár int error = 0; 140804aae283SPali Rohár 140904aae283SPali Rohár mutex_lock(&alps_mutex); 141004aae283SPali Rohár 141104aae283SPali Rohár if (priv->dev3) 141204aae283SPali Rohár goto out; 141304aae283SPali Rohár 141404aae283SPali Rohár dev3 = input_allocate_device(); 141504aae283SPali Rohár if (!dev3) { 141604aae283SPali Rohár psmouse_err(psmouse, "failed to allocate secondary device\n"); 141704aae283SPali Rohár error = -ENOMEM; 141804aae283SPali Rohár goto out; 141904aae283SPali Rohár } 142004aae283SPali Rohár 142104aae283SPali Rohár snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s", 142204aae283SPali Rohár psmouse->ps2dev.serio->phys, 142304aae283SPali Rohár (priv->dev2 ? "input2" : "input1")); 142404aae283SPali Rohár dev3->phys = priv->phys3; 142504aae283SPali Rohár 142604aae283SPali Rohár /* 142704aae283SPali Rohár * format of input device name is: "protocol vendor name" 142804aae283SPali Rohár * see function psmouse_switch_protocol() in psmouse-base.c 142904aae283SPali Rohár */ 143004aae283SPali Rohár dev3->name = "PS/2 ALPS Mouse"; 143104aae283SPali Rohár 143204aae283SPali Rohár dev3->id.bustype = BUS_I8042; 143304aae283SPali Rohár dev3->id.vendor = 0x0002; 143404aae283SPali Rohár dev3->id.product = PSMOUSE_PS2; 143504aae283SPali Rohár dev3->id.version = 0x0000; 143604aae283SPali Rohár dev3->dev.parent = &psmouse->ps2dev.serio->dev; 143704aae283SPali Rohár 143804aae283SPali Rohár input_set_capability(dev3, EV_REL, REL_X); 143904aae283SPali Rohár input_set_capability(dev3, EV_REL, REL_Y); 144004aae283SPali Rohár input_set_capability(dev3, EV_KEY, BTN_LEFT); 144104aae283SPali Rohár input_set_capability(dev3, EV_KEY, BTN_RIGHT); 144204aae283SPali Rohár input_set_capability(dev3, EV_KEY, BTN_MIDDLE); 144304aae283SPali Rohár 144404aae283SPali Rohár __set_bit(INPUT_PROP_POINTER, dev3->propbit); 144504aae283SPali Rohár 144604aae283SPali Rohár error = input_register_device(dev3); 144704aae283SPali Rohár if (error) { 144804aae283SPali Rohár psmouse_err(psmouse, 144904aae283SPali Rohár "failed to register secondary device: %d\n", 145004aae283SPali Rohár error); 145104aae283SPali Rohár input_free_device(dev3); 145204aae283SPali Rohár goto out; 145304aae283SPali Rohár } 145404aae283SPali Rohár 145504aae283SPali Rohár priv->dev3 = dev3; 145604aae283SPali Rohár 145704aae283SPali Rohár out: 145804aae283SPali Rohár /* 145904aae283SPali Rohár * Save the error code so that we can detect that we 146004aae283SPali Rohár * already tried to create the device. 146104aae283SPali Rohár */ 146204aae283SPali Rohár if (error) 146304aae283SPali Rohár priv->dev3 = ERR_PTR(error); 146404aae283SPali Rohár 146504aae283SPali Rohár mutex_unlock(&alps_mutex); 146604aae283SPali Rohár } 146704aae283SPali Rohár 146859c30afbSHans de Goede static void alps_report_bare_ps2_packet(struct psmouse *psmouse, 14691d9f2626SSebastian Kapfer unsigned char packet[], 14701d9f2626SSebastian Kapfer bool report_buttons) 14711d9f2626SSebastian Kapfer { 147259c30afbSHans de Goede struct alps_data *priv = psmouse->private; 14736bcca19fSHans de Goede struct input_dev *dev, *dev2 = NULL; 147459c30afbSHans de Goede 1475e3a79212SHans de Goede /* Figure out which device to use to report the bare packet */ 1476e3a79212SHans de Goede if (priv->proto_version == ALPS_PROTO_V2 && 1477e3a79212SHans de Goede (priv->flags & ALPS_DUALPOINT)) { 1478e3a79212SHans de Goede /* On V2 devices the DualPoint Stick reports bare packets */ 1479e3a79212SHans de Goede dev = priv->dev2; 14806bcca19fSHans de Goede dev2 = psmouse->dev; 1481e3a79212SHans de Goede } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) { 148259c30afbSHans de Goede /* Register dev3 mouse if we received PS/2 packet first time */ 148359c30afbSHans de Goede if (!IS_ERR(priv->dev3)) 148459c30afbSHans de Goede psmouse_queue_work(psmouse, &priv->dev3_register_work, 148559c30afbSHans de Goede 0); 148659c30afbSHans de Goede return; 148759c30afbSHans de Goede } else { 148859c30afbSHans de Goede dev = priv->dev3; 148959c30afbSHans de Goede } 149059c30afbSHans de Goede 14911d9f2626SSebastian Kapfer if (report_buttons) 14926bcca19fSHans de Goede alps_report_buttons(dev, dev2, 14931d9f2626SSebastian Kapfer packet[0] & 1, packet[0] & 2, packet[0] & 4); 14941d9f2626SSebastian Kapfer 14951ef85805SDmitry Torokhov psmouse_report_standard_motion(dev, packet); 14961d9f2626SSebastian Kapfer 149704aae283SPali Rohár input_sync(dev); 14981d9f2626SSebastian Kapfer } 14991d9f2626SSebastian Kapfer 15001d9f2626SSebastian Kapfer static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse) 15011da177e4SLinus Torvalds { 15021da177e4SLinus Torvalds struct alps_data *priv = psmouse->private; 15031da177e4SLinus Torvalds 15041d9f2626SSebastian Kapfer if (psmouse->pktcnt < 6) 15051d9f2626SSebastian Kapfer return PSMOUSE_GOOD_DATA; 15061d9f2626SSebastian Kapfer 15071d9f2626SSebastian Kapfer if (psmouse->pktcnt == 6) { 15081d9f2626SSebastian Kapfer /* 15091d9f2626SSebastian Kapfer * Start a timer to flush the packet if it ends up last 15101d9f2626SSebastian Kapfer * 6-byte packet in the stream. Timer needs to fire 15111d9f2626SSebastian Kapfer * psmouse core times out itself. 20 ms should be enough 15121d9f2626SSebastian Kapfer * to decide if we are getting more data or not. 15131d9f2626SSebastian Kapfer */ 15141d9f2626SSebastian Kapfer mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20)); 15151d9f2626SSebastian Kapfer return PSMOUSE_GOOD_DATA; 15161d9f2626SSebastian Kapfer } 15171d9f2626SSebastian Kapfer 15181d9f2626SSebastian Kapfer del_timer(&priv->timer); 15191d9f2626SSebastian Kapfer 15201d9f2626SSebastian Kapfer if (psmouse->packet[6] & 0x80) { 15211d9f2626SSebastian Kapfer 15221d9f2626SSebastian Kapfer /* 15231d9f2626SSebastian Kapfer * Highest bit is set - that means we either had 15241d9f2626SSebastian Kapfer * complete ALPS packet and this is start of the 15251d9f2626SSebastian Kapfer * next packet or we got garbage. 15261d9f2626SSebastian Kapfer */ 15271d9f2626SSebastian Kapfer 15281d9f2626SSebastian Kapfer if (((psmouse->packet[3] | 15291d9f2626SSebastian Kapfer psmouse->packet[4] | 15301d9f2626SSebastian Kapfer psmouse->packet[5]) & 0x80) || 153199df65e7SKevin Cernekee (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) { 1532b5d21704SDmitry Torokhov psmouse_dbg(psmouse, 15333b112923SAndy Shevchenko "refusing packet %4ph (suspected interleaved ps/2)\n", 15343b112923SAndy Shevchenko psmouse->packet + 3); 15351d9f2626SSebastian Kapfer return PSMOUSE_BAD_DATA; 15361d9f2626SSebastian Kapfer } 15371d9f2626SSebastian Kapfer 153824af5cb9SKevin Cernekee priv->process_packet(psmouse); 15391d9f2626SSebastian Kapfer 15401d9f2626SSebastian Kapfer /* Continue with the next packet */ 15411d9f2626SSebastian Kapfer psmouse->packet[0] = psmouse->packet[6]; 15421d9f2626SSebastian Kapfer psmouse->pktcnt = 1; 15431d9f2626SSebastian Kapfer 15441d9f2626SSebastian Kapfer } else { 15451d9f2626SSebastian Kapfer 15461d9f2626SSebastian Kapfer /* 15471d9f2626SSebastian Kapfer * High bit is 0 - that means that we indeed got a PS/2 15481d9f2626SSebastian Kapfer * packet in the middle of ALPS packet. 15491d9f2626SSebastian Kapfer * 15501d9f2626SSebastian Kapfer * There is also possibility that we got 6-byte ALPS 15511d9f2626SSebastian Kapfer * packet followed by 3-byte packet from trackpoint. We 15521d9f2626SSebastian Kapfer * can not distinguish between these 2 scenarios but 1553b5d21704SDmitry Torokhov * because the latter is unlikely to happen in course of 15541d9f2626SSebastian Kapfer * normal operation (user would need to press all 15551d9f2626SSebastian Kapfer * buttons on the pad and start moving trackpoint 15561d9f2626SSebastian Kapfer * without touching the pad surface) we assume former. 15571d9f2626SSebastian Kapfer * Even if we are wrong the wost thing that would happen 15581d9f2626SSebastian Kapfer * the cursor would jump but we should not get protocol 1559b5d21704SDmitry Torokhov * de-synchronization. 15601d9f2626SSebastian Kapfer */ 15611d9f2626SSebastian Kapfer 156259c30afbSHans de Goede alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3], 156359c30afbSHans de Goede false); 15641d9f2626SSebastian Kapfer 15651d9f2626SSebastian Kapfer /* 15661d9f2626SSebastian Kapfer * Continue with the standard ALPS protocol handling, 15671d9f2626SSebastian Kapfer * but make sure we won't process it as an interleaved 15681d9f2626SSebastian Kapfer * packet again, which may happen if all buttons are 15691d9f2626SSebastian Kapfer * pressed. To avoid this let's reset the 4th bit which 15701d9f2626SSebastian Kapfer * is normally 1. 15711d9f2626SSebastian Kapfer */ 15721d9f2626SSebastian Kapfer psmouse->packet[3] = psmouse->packet[6] & 0xf7; 15731d9f2626SSebastian Kapfer psmouse->pktcnt = 4; 15741d9f2626SSebastian Kapfer } 15751d9f2626SSebastian Kapfer 15761d9f2626SSebastian Kapfer return PSMOUSE_GOOD_DATA; 15771d9f2626SSebastian Kapfer } 15781d9f2626SSebastian Kapfer 157917a58edcSKees Cook static void alps_flush_packet(struct timer_list *t) 15801d9f2626SSebastian Kapfer { 158117a58edcSKees Cook struct alps_data *priv = from_timer(priv, t, timer); 158217a58edcSKees Cook struct psmouse *psmouse = priv->psmouse; 15831d9f2626SSebastian Kapfer 15841d9f2626SSebastian Kapfer serio_pause_rx(psmouse->ps2dev.serio); 15851d9f2626SSebastian Kapfer 1586b46615feSSeth Forshee if (psmouse->pktcnt == psmouse->pktsize) { 15871d9f2626SSebastian Kapfer 15881d9f2626SSebastian Kapfer /* 15891d9f2626SSebastian Kapfer * We did not any more data in reasonable amount of time. 15901d9f2626SSebastian Kapfer * Validate the last 3 bytes and process as a standard 15911d9f2626SSebastian Kapfer * ALPS packet. 15921d9f2626SSebastian Kapfer */ 15931d9f2626SSebastian Kapfer if ((psmouse->packet[3] | 15941d9f2626SSebastian Kapfer psmouse->packet[4] | 15951d9f2626SSebastian Kapfer psmouse->packet[5]) & 0x80) { 1596b5d21704SDmitry Torokhov psmouse_dbg(psmouse, 15973b112923SAndy Shevchenko "refusing packet %3ph (suspected interleaved ps/2)\n", 15983b112923SAndy Shevchenko psmouse->packet + 3); 15991d9f2626SSebastian Kapfer } else { 160024af5cb9SKevin Cernekee priv->process_packet(psmouse); 16011d9f2626SSebastian Kapfer } 16021d9f2626SSebastian Kapfer psmouse->pktcnt = 0; 16031d9f2626SSebastian Kapfer } 16041d9f2626SSebastian Kapfer 16051d9f2626SSebastian Kapfer serio_continue_rx(psmouse->ps2dev.serio); 16061d9f2626SSebastian Kapfer } 16071d9f2626SSebastian Kapfer 16081d9f2626SSebastian Kapfer static psmouse_ret_t alps_process_byte(struct psmouse *psmouse) 16091d9f2626SSebastian Kapfer { 16101d9f2626SSebastian Kapfer struct alps_data *priv = psmouse->private; 16111d9f2626SSebastian Kapfer 16124ab8f7f3SPali Rohár /* 16134ab8f7f3SPali Rohár * Check if we are dealing with a bare PS/2 packet, presumably from 16144ab8f7f3SPali Rohár * a device connected to the external PS/2 port. Because bare PS/2 16154ab8f7f3SPali Rohár * protocol does not have enough constant bits to self-synchronize 16164ab8f7f3SPali Rohár * properly we only do this if the device is fully synchronized. 16173db5b9f7SMasaki Ota * Can not distinguish V8's first byte from PS/2 packet's 16184ab8f7f3SPali Rohár */ 16193db5b9f7SMasaki Ota if (priv->proto_version != ALPS_PROTO_V8 && 16203db5b9f7SMasaki Ota !psmouse->out_of_sync_cnt && 16213db5b9f7SMasaki Ota (psmouse->packet[0] & 0xc8) == 0x08) { 16223db5b9f7SMasaki Ota 16231da177e4SLinus Torvalds if (psmouse->pktcnt == 3) { 162459c30afbSHans de Goede alps_report_bare_ps2_packet(psmouse, psmouse->packet, 16251d9f2626SSebastian Kapfer true); 16261da177e4SLinus Torvalds return PSMOUSE_FULL_PACKET; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds return PSMOUSE_GOOD_DATA; 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds 16311d9f2626SSebastian Kapfer /* Check for PS/2 packet stuffed in the middle of ALPS packet. */ 16321d9f2626SSebastian Kapfer 163399df65e7SKevin Cernekee if ((priv->flags & ALPS_PS2_INTERLEAVED) && 16341d9f2626SSebastian Kapfer psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) { 16351d9f2626SSebastian Kapfer return alps_handle_interleaved_ps2(psmouse); 16361d9f2626SSebastian Kapfer } 16371d9f2626SSebastian Kapfer 163899df65e7SKevin Cernekee if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) { 1639b5d21704SDmitry Torokhov psmouse_dbg(psmouse, 1640b5d21704SDmitry Torokhov "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n", 164199df65e7SKevin Cernekee psmouse->packet[0], priv->mask0, priv->byte0); 16421da177e4SLinus Torvalds return PSMOUSE_BAD_DATA; 16431d9f2626SSebastian Kapfer } 16441da177e4SLinus Torvalds 1645b46615feSSeth Forshee /* Bytes 2 - pktsize should have 0 in the highest bit */ 1646a7ef82aeSPali Rohár if (priv->proto_version < ALPS_PROTO_V5 && 164775af9e56SDave Turvene psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize && 16481d9f2626SSebastian Kapfer (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) { 1649b5d21704SDmitry Torokhov psmouse_dbg(psmouse, "refusing packet[%i] = %x\n", 1650b5d21704SDmitry Torokhov psmouse->pktcnt - 1, 1651b5d21704SDmitry Torokhov psmouse->packet[psmouse->pktcnt - 1]); 1652a7ef82aeSPali Rohár 1653fb2dd7a6SDmitry Torokhov if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE && 1654a7ef82aeSPali Rohár psmouse->pktcnt == psmouse->pktsize) { 1655a7ef82aeSPali Rohár /* 1656a7ef82aeSPali Rohár * Some Dell boxes, such as Latitude E6440 or E7440 1657a7ef82aeSPali Rohár * with closed lid, quite often smash last byte of 1658a7ef82aeSPali Rohár * otherwise valid packet with 0xff. Given that the 1659a7ef82aeSPali Rohár * next packet is very likely to be valid let's 1660a7ef82aeSPali Rohár * report PSMOUSE_FULL_PACKET but not process data, 1661a7ef82aeSPali Rohár * rather than reporting PSMOUSE_BAD_DATA and 1662a7ef82aeSPali Rohár * filling the logs. 1663a7ef82aeSPali Rohár */ 1664a7ef82aeSPali Rohár return PSMOUSE_FULL_PACKET; 1665a7ef82aeSPali Rohár } 1666a7ef82aeSPali Rohár 16671da177e4SLinus Torvalds return PSMOUSE_BAD_DATA; 16681d9f2626SSebastian Kapfer } 16691da177e4SLinus Torvalds 16703db5b9f7SMasaki Ota if ((priv->proto_version == ALPS_PROTO_V7 && 16713db5b9f7SMasaki Ota !alps_is_valid_package_v7(psmouse)) || 16723db5b9f7SMasaki Ota (priv->proto_version == ALPS_PROTO_V8 && 16733db5b9f7SMasaki Ota !alps_is_valid_package_ss4_v2(psmouse))) { 16743808843cSYunkang Tang psmouse_dbg(psmouse, "refusing packet[%i] = %x\n", 16753808843cSYunkang Tang psmouse->pktcnt - 1, 16763808843cSYunkang Tang psmouse->packet[psmouse->pktcnt - 1]); 16773808843cSYunkang Tang return PSMOUSE_BAD_DATA; 16783808843cSYunkang Tang } 16793808843cSYunkang Tang 1680b46615feSSeth Forshee if (psmouse->pktcnt == psmouse->pktsize) { 168124af5cb9SKevin Cernekee priv->process_packet(psmouse); 16821da177e4SLinus Torvalds return PSMOUSE_FULL_PACKET; 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds return PSMOUSE_GOOD_DATA; 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds 168825bded7cSSeth Forshee static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble) 168925bded7cSSeth Forshee { 169025bded7cSSeth Forshee struct ps2dev *ps2dev = &psmouse->ps2dev; 169125bded7cSSeth Forshee struct alps_data *priv = psmouse->private; 169225bded7cSSeth Forshee int command; 169325bded7cSSeth Forshee unsigned char *param; 169425bded7cSSeth Forshee unsigned char dummy[4]; 169525bded7cSSeth Forshee 169625bded7cSSeth Forshee BUG_ON(nibble > 0xf); 169725bded7cSSeth Forshee 169825bded7cSSeth Forshee command = priv->nibble_commands[nibble].command; 169925bded7cSSeth Forshee param = (command & 0x0f00) ? 170025bded7cSSeth Forshee dummy : (unsigned char *)&priv->nibble_commands[nibble].data; 170125bded7cSSeth Forshee 170225bded7cSSeth Forshee if (ps2_command(ps2dev, param, command)) 170325bded7cSSeth Forshee return -1; 170425bded7cSSeth Forshee 170525bded7cSSeth Forshee return 0; 170625bded7cSSeth Forshee } 170725bded7cSSeth Forshee 170825bded7cSSeth Forshee static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr) 170925bded7cSSeth Forshee { 171025bded7cSSeth Forshee struct ps2dev *ps2dev = &psmouse->ps2dev; 171125bded7cSSeth Forshee struct alps_data *priv = psmouse->private; 171225bded7cSSeth Forshee int i, nibble; 171325bded7cSSeth Forshee 171425bded7cSSeth Forshee if (ps2_command(ps2dev, NULL, priv->addr_command)) 171525bded7cSSeth Forshee return -1; 171625bded7cSSeth Forshee 171725bded7cSSeth Forshee for (i = 12; i >= 0; i -= 4) { 171825bded7cSSeth Forshee nibble = (addr >> i) & 0xf; 171925bded7cSSeth Forshee if (alps_command_mode_send_nibble(psmouse, nibble)) 172025bded7cSSeth Forshee return -1; 172125bded7cSSeth Forshee } 172225bded7cSSeth Forshee 172325bded7cSSeth Forshee return 0; 172425bded7cSSeth Forshee } 172525bded7cSSeth Forshee 172625bded7cSSeth Forshee static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr) 172725bded7cSSeth Forshee { 172825bded7cSSeth Forshee struct ps2dev *ps2dev = &psmouse->ps2dev; 172925bded7cSSeth Forshee unsigned char param[4]; 173025bded7cSSeth Forshee 173125bded7cSSeth Forshee if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) 173225bded7cSSeth Forshee return -1; 173325bded7cSSeth Forshee 173425bded7cSSeth Forshee /* 173525bded7cSSeth Forshee * The address being read is returned in the first two bytes 173625bded7cSSeth Forshee * of the result. Check that this address matches the expected 173725bded7cSSeth Forshee * address. 173825bded7cSSeth Forshee */ 173925bded7cSSeth Forshee if (addr != ((param[0] << 8) | param[1])) 174025bded7cSSeth Forshee return -1; 174125bded7cSSeth Forshee 174225bded7cSSeth Forshee return param[2]; 174325bded7cSSeth Forshee } 174425bded7cSSeth Forshee 174525bded7cSSeth Forshee static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr) 174625bded7cSSeth Forshee { 174725bded7cSSeth Forshee if (alps_command_mode_set_addr(psmouse, addr)) 174825bded7cSSeth Forshee return -1; 174925bded7cSSeth Forshee return __alps_command_mode_read_reg(psmouse, addr); 175025bded7cSSeth Forshee } 175125bded7cSSeth Forshee 175225bded7cSSeth Forshee static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value) 175325bded7cSSeth Forshee { 175425bded7cSSeth Forshee if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf)) 175525bded7cSSeth Forshee return -1; 175625bded7cSSeth Forshee if (alps_command_mode_send_nibble(psmouse, value & 0xf)) 175725bded7cSSeth Forshee return -1; 175825bded7cSSeth Forshee return 0; 175925bded7cSSeth Forshee } 176025bded7cSSeth Forshee 176125bded7cSSeth Forshee static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr, 176225bded7cSSeth Forshee u8 value) 176325bded7cSSeth Forshee { 176425bded7cSSeth Forshee if (alps_command_mode_set_addr(psmouse, addr)) 176525bded7cSSeth Forshee return -1; 176625bded7cSSeth Forshee return __alps_command_mode_write_reg(psmouse, value); 176725bded7cSSeth Forshee } 176825bded7cSSeth Forshee 176924ba9707SKevin Cernekee static int alps_rpt_cmd(struct psmouse *psmouse, int init_command, 177024ba9707SKevin Cernekee int repeated_command, unsigned char *param) 177124ba9707SKevin Cernekee { 177224ba9707SKevin Cernekee struct ps2dev *ps2dev = &psmouse->ps2dev; 177324ba9707SKevin Cernekee 177424ba9707SKevin Cernekee param[0] = 0; 177524ba9707SKevin Cernekee if (init_command && ps2_command(ps2dev, param, init_command)) 177624ba9707SKevin Cernekee return -EIO; 177724ba9707SKevin Cernekee 177824ba9707SKevin Cernekee if (ps2_command(ps2dev, NULL, repeated_command) || 177924ba9707SKevin Cernekee ps2_command(ps2dev, NULL, repeated_command) || 178024ba9707SKevin Cernekee ps2_command(ps2dev, NULL, repeated_command)) 178124ba9707SKevin Cernekee return -EIO; 178224ba9707SKevin Cernekee 178324ba9707SKevin Cernekee param[0] = param[1] = param[2] = 0xff; 178424ba9707SKevin Cernekee if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) 178524ba9707SKevin Cernekee return -EIO; 178624ba9707SKevin Cernekee 178739fbe585SDmitry Torokhov psmouse_dbg(psmouse, "%2.2X report: %3ph\n", 178839fbe585SDmitry Torokhov repeated_command, param); 178924ba9707SKevin Cernekee return 0; 179024ba9707SKevin Cernekee } 179124ba9707SKevin Cernekee 17923808843cSYunkang Tang static bool alps_check_valid_firmware_id(unsigned char id[]) 17933808843cSYunkang Tang { 17943808843cSYunkang Tang if (id[0] == 0x73) 17953808843cSYunkang Tang return true; 17963808843cSYunkang Tang 17973808843cSYunkang Tang if (id[0] == 0x88 && 17983808843cSYunkang Tang (id[1] == 0x07 || 17993808843cSYunkang Tang id[1] == 0x08 || 18003808843cSYunkang Tang (id[1] & 0xf0) == 0xb0 || 18013808843cSYunkang Tang (id[1] & 0xf0) == 0xc0)) { 18023808843cSYunkang Tang return true; 18033808843cSYunkang Tang } 18043808843cSYunkang Tang 18053808843cSYunkang Tang return false; 18063808843cSYunkang Tang } 18073808843cSYunkang Tang 1808d18e53fcSKevin Cernekee static int alps_enter_command_mode(struct psmouse *psmouse) 180925bded7cSSeth Forshee { 181025bded7cSSeth Forshee unsigned char param[4]; 181125bded7cSSeth Forshee 181224ba9707SKevin Cernekee if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) { 181325bded7cSSeth Forshee psmouse_err(psmouse, "failed to enter command mode\n"); 181425bded7cSSeth Forshee return -1; 181525bded7cSSeth Forshee } 181625bded7cSSeth Forshee 18173808843cSYunkang Tang if (!alps_check_valid_firmware_id(param)) { 181825bded7cSSeth Forshee psmouse_dbg(psmouse, 181924ba9707SKevin Cernekee "unknown response while entering command mode\n"); 182025bded7cSSeth Forshee return -1; 182125bded7cSSeth Forshee } 182225bded7cSSeth Forshee return 0; 182325bded7cSSeth Forshee } 182425bded7cSSeth Forshee 182525bded7cSSeth Forshee static inline int alps_exit_command_mode(struct psmouse *psmouse) 182625bded7cSSeth Forshee { 182725bded7cSSeth Forshee struct ps2dev *ps2dev = &psmouse->ps2dev; 182825bded7cSSeth Forshee if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) 182925bded7cSSeth Forshee return -1; 183025bded7cSSeth Forshee return 0; 183125bded7cSSeth Forshee } 183225bded7cSSeth Forshee 18331da177e4SLinus Torvalds /* 18341da177e4SLinus Torvalds * For DualPoint devices select the device that should respond to 18351da177e4SLinus Torvalds * subsequent commands. It looks like glidepad is behind stickpointer, 18361da177e4SLinus Torvalds * I'd thought it would be other way around... 18371da177e4SLinus Torvalds */ 183825bded7cSSeth Forshee static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable) 18391da177e4SLinus Torvalds { 18401da177e4SLinus Torvalds struct ps2dev *ps2dev = &psmouse->ps2dev; 18411da177e4SLinus Torvalds int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11; 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds if (ps2_command(ps2dev, NULL, cmd) || 18441da177e4SLinus Torvalds ps2_command(ps2dev, NULL, cmd) || 18451da177e4SLinus Torvalds ps2_command(ps2dev, NULL, cmd) || 18461da177e4SLinus Torvalds ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE)) 18471da177e4SLinus Torvalds return -1; 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds /* we may get 3 more bytes, just ignore them */ 1850c611763dSDmitry Torokhov ps2_drain(ps2dev, 3, 100); 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds return 0; 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds 185525bded7cSSeth Forshee static int alps_absolute_mode_v1_v2(struct psmouse *psmouse) 18561da177e4SLinus Torvalds { 18571da177e4SLinus Torvalds struct ps2dev *ps2dev = &psmouse->ps2dev; 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds /* Try ALPS magic knock - 4 disable before enable */ 18601da177e4SLinus Torvalds if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || 18611da177e4SLinus Torvalds ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || 18621da177e4SLinus Torvalds ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || 18631da177e4SLinus Torvalds ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || 18641da177e4SLinus Torvalds ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) 18651da177e4SLinus Torvalds return -1; 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* 18681da177e4SLinus Torvalds * Switch mouse to poll (remote) mode so motion data will not 18691da177e4SLinus Torvalds * get in our way 18701da177e4SLinus Torvalds */ 1871ad56814fSGuenter Roeck return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL); 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 187495f75e91SYunkang Tang static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word) 187595f75e91SYunkang Tang { 187695f75e91SYunkang Tang int i, nibble; 187795f75e91SYunkang Tang 187895f75e91SYunkang Tang /* 187995f75e91SYunkang Tang * b0-b11 are valid bits, send sequence is inverse. 188095f75e91SYunkang Tang * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1 188195f75e91SYunkang Tang */ 188295f75e91SYunkang Tang for (i = 0; i <= 8; i += 4) { 188395f75e91SYunkang Tang nibble = (word >> i) & 0xf; 188495f75e91SYunkang Tang if (alps_command_mode_send_nibble(psmouse, nibble)) 188595f75e91SYunkang Tang return -1; 188695f75e91SYunkang Tang } 188795f75e91SYunkang Tang 188895f75e91SYunkang Tang return 0; 188995f75e91SYunkang Tang } 189095f75e91SYunkang Tang 189195f75e91SYunkang Tang static int alps_monitor_mode_write_reg(struct psmouse *psmouse, 189295f75e91SYunkang Tang u16 addr, u16 value) 189395f75e91SYunkang Tang { 189495f75e91SYunkang Tang struct ps2dev *ps2dev = &psmouse->ps2dev; 189595f75e91SYunkang Tang 189695f75e91SYunkang Tang /* 0x0A0 is the command to write the word */ 189795f75e91SYunkang Tang if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) || 189895f75e91SYunkang Tang alps_monitor_mode_send_word(psmouse, 0x0A0) || 189995f75e91SYunkang Tang alps_monitor_mode_send_word(psmouse, addr) || 190095f75e91SYunkang Tang alps_monitor_mode_send_word(psmouse, value) || 190195f75e91SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE)) 190295f75e91SYunkang Tang return -1; 190395f75e91SYunkang Tang 190495f75e91SYunkang Tang return 0; 190595f75e91SYunkang Tang } 190695f75e91SYunkang Tang 190795f75e91SYunkang Tang static int alps_monitor_mode(struct psmouse *psmouse, bool enable) 190895f75e91SYunkang Tang { 190995f75e91SYunkang Tang struct ps2dev *ps2dev = &psmouse->ps2dev; 191095f75e91SYunkang Tang 191195f75e91SYunkang Tang if (enable) { 191295f75e91SYunkang Tang /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */ 191395f75e91SYunkang Tang if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) || 191495f75e91SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) || 191595f75e91SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || 191695f75e91SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || 191795f75e91SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || 191895f75e91SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || 191995f75e91SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || 192095f75e91SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO)) 192195f75e91SYunkang Tang return -1; 192295f75e91SYunkang Tang } else { 192395f75e91SYunkang Tang /* EC to exit monitor mode */ 192495f75e91SYunkang Tang if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP)) 192595f75e91SYunkang Tang return -1; 192695f75e91SYunkang Tang } 192795f75e91SYunkang Tang 192895f75e91SYunkang Tang return 0; 192995f75e91SYunkang Tang } 193095f75e91SYunkang Tang 193195f75e91SYunkang Tang static int alps_absolute_mode_v6(struct psmouse *psmouse) 193295f75e91SYunkang Tang { 193395f75e91SYunkang Tang u16 reg_val = 0x181; 193495f75e91SYunkang Tang int ret = -1; 193595f75e91SYunkang Tang 193695f75e91SYunkang Tang /* enter monitor mode, to write the register */ 193795f75e91SYunkang Tang if (alps_monitor_mode(psmouse, true)) 193895f75e91SYunkang Tang return -1; 193995f75e91SYunkang Tang 194095f75e91SYunkang Tang ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val); 194195f75e91SYunkang Tang 194295f75e91SYunkang Tang if (alps_monitor_mode(psmouse, false)) 194395f75e91SYunkang Tang ret = -1; 194495f75e91SYunkang Tang 194595f75e91SYunkang Tang return ret; 194695f75e91SYunkang Tang } 194795f75e91SYunkang Tang 19481da177e4SLinus Torvalds static int alps_get_status(struct psmouse *psmouse, char *param) 19491da177e4SLinus Torvalds { 19501da177e4SLinus Torvalds /* Get status: 0xF5 0xF5 0xF5 0xE9 */ 195124ba9707SKevin Cernekee if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param)) 19521da177e4SLinus Torvalds return -1; 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds return 0; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds /* 19581da177e4SLinus Torvalds * Turn touchpad tapping on or off. The sequences are: 19591da177e4SLinus Torvalds * 0xE9 0xF5 0xF5 0xF3 0x0A to enable, 19601da177e4SLinus Torvalds * 0xE9 0xF5 0xF5 0xE8 0x00 to disable. 19611da177e4SLinus Torvalds * My guess that 0xE9 (GetInfo) is here as a sync point. 19621da177e4SLinus Torvalds * For models that also have stickpointer (DualPoints) its tapping 19631da177e4SLinus Torvalds * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but 19641da177e4SLinus Torvalds * we don't fiddle with it. 19651da177e4SLinus Torvalds */ 19661da177e4SLinus Torvalds static int alps_tap_mode(struct psmouse *psmouse, int enable) 19671da177e4SLinus Torvalds { 19681da177e4SLinus Torvalds struct ps2dev *ps2dev = &psmouse->ps2dev; 19691da177e4SLinus Torvalds int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES; 19701da177e4SLinus Torvalds unsigned char tap_arg = enable ? 0x0A : 0x00; 19711da177e4SLinus Torvalds unsigned char param[4]; 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) || 19741da177e4SLinus Torvalds ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || 19751da177e4SLinus Torvalds ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) || 19761da177e4SLinus Torvalds ps2_command(ps2dev, &tap_arg, cmd)) 19771da177e4SLinus Torvalds return -1; 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds if (alps_get_status(psmouse, param)) 19801da177e4SLinus Torvalds return -1; 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds return 0; 19831da177e4SLinus Torvalds } 19841da177e4SLinus Torvalds 1985f0d5c6f4SDmitry Torokhov /* 1986f0d5c6f4SDmitry Torokhov * alps_poll() - poll the touchpad for current motion packet. 1987f0d5c6f4SDmitry Torokhov * Used in resync. 1988f0d5c6f4SDmitry Torokhov */ 1989f0d5c6f4SDmitry Torokhov static int alps_poll(struct psmouse *psmouse) 1990f0d5c6f4SDmitry Torokhov { 1991f0d5c6f4SDmitry Torokhov struct alps_data *priv = psmouse->private; 1992b46615feSSeth Forshee unsigned char buf[sizeof(psmouse->packet)]; 1993b7802c5cSDmitry Torokhov bool poll_failed; 1994f0d5c6f4SDmitry Torokhov 199599df65e7SKevin Cernekee if (priv->flags & ALPS_PASS) 199625bded7cSSeth Forshee alps_passthrough_mode_v2(psmouse, true); 1997f0d5c6f4SDmitry Torokhov 1998f0d5c6f4SDmitry Torokhov poll_failed = ps2_command(&psmouse->ps2dev, buf, 1999f0d5c6f4SDmitry Torokhov PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0; 2000f0d5c6f4SDmitry Torokhov 200199df65e7SKevin Cernekee if (priv->flags & ALPS_PASS) 200225bded7cSSeth Forshee alps_passthrough_mode_v2(psmouse, false); 2003f0d5c6f4SDmitry Torokhov 200499df65e7SKevin Cernekee if (poll_failed || (buf[0] & priv->mask0) != priv->byte0) 2005f0d5c6f4SDmitry Torokhov return -1; 2006f0d5c6f4SDmitry Torokhov 2007f0d5c6f4SDmitry Torokhov if ((psmouse->badbyte & 0xc8) == 0x08) { 2008f0d5c6f4SDmitry Torokhov /* 2009f0d5c6f4SDmitry Torokhov * Poll the track stick ... 2010f0d5c6f4SDmitry Torokhov */ 2011f0d5c6f4SDmitry Torokhov if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8))) 2012f0d5c6f4SDmitry Torokhov return -1; 2013f0d5c6f4SDmitry Torokhov } 2014f0d5c6f4SDmitry Torokhov 2015f0d5c6f4SDmitry Torokhov memcpy(psmouse->packet, buf, sizeof(buf)); 2016f0d5c6f4SDmitry Torokhov return 0; 2017f0d5c6f4SDmitry Torokhov } 2018f0d5c6f4SDmitry Torokhov 201925bded7cSSeth Forshee static int alps_hw_init_v1_v2(struct psmouse *psmouse) 20201da177e4SLinus Torvalds { 20211da177e4SLinus Torvalds struct alps_data *priv = psmouse->private; 20221da177e4SLinus Torvalds 202399df65e7SKevin Cernekee if ((priv->flags & ALPS_PASS) && 202425bded7cSSeth Forshee alps_passthrough_mode_v2(psmouse, true)) { 20251da177e4SLinus Torvalds return -1; 2026b7802c5cSDmitry Torokhov } 20271da177e4SLinus Torvalds 2028b7802c5cSDmitry Torokhov if (alps_tap_mode(psmouse, true)) { 2029b5d21704SDmitry Torokhov psmouse_warn(psmouse, "Failed to enable hardware tapping\n"); 20301da177e4SLinus Torvalds return -1; 2031963f626dSPeter Osterlund } 20321da177e4SLinus Torvalds 203325bded7cSSeth Forshee if (alps_absolute_mode_v1_v2(psmouse)) { 2034b5d21704SDmitry Torokhov psmouse_err(psmouse, "Failed to enable absolute mode\n"); 20351da177e4SLinus Torvalds return -1; 20361da177e4SLinus Torvalds } 20371da177e4SLinus Torvalds 203899df65e7SKevin Cernekee if ((priv->flags & ALPS_PASS) && 203925bded7cSSeth Forshee alps_passthrough_mode_v2(psmouse, false)) { 20401da177e4SLinus Torvalds return -1; 2041b7802c5cSDmitry Torokhov } 20421da177e4SLinus Torvalds 20431e0c5b12SDmitry Torokhov /* ALPS needs stream mode, otherwise it won't report any data */ 20441e0c5b12SDmitry Torokhov if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) { 2045b5d21704SDmitry Torokhov psmouse_err(psmouse, "Failed to enable stream mode\n"); 20461e0c5b12SDmitry Torokhov return -1; 20471e0c5b12SDmitry Torokhov } 20481e0c5b12SDmitry Torokhov 20491e0c5b12SDmitry Torokhov return 0; 20501e0c5b12SDmitry Torokhov } 20511e0c5b12SDmitry Torokhov 2052d91abc21SPali Rohár /* Must be in passthrough mode when calling this function */ 2053d91abc21SPali Rohár static int alps_trackstick_enter_extended_mode_v3_v6(struct psmouse *psmouse) 205495f75e91SYunkang Tang { 205595f75e91SYunkang Tang unsigned char param[2] = {0xC8, 0x14}; 205695f75e91SYunkang Tang 205795f75e91SYunkang Tang if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || 205895f75e91SYunkang Tang ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || 205995f75e91SYunkang Tang ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) || 206095f75e91SYunkang Tang ps2_command(&psmouse->ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) || 206195f75e91SYunkang Tang ps2_command(&psmouse->ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE)) 206295f75e91SYunkang Tang return -1; 206395f75e91SYunkang Tang 2064d91abc21SPali Rohár return 0; 2065d91abc21SPali Rohár } 2066d91abc21SPali Rohár 2067d91abc21SPali Rohár static int alps_hw_init_v6(struct psmouse *psmouse) 2068d91abc21SPali Rohár { 2069d91abc21SPali Rohár int ret; 2070d91abc21SPali Rohár 2071d91abc21SPali Rohár /* Enter passthrough mode to let trackpoint enter 6byte raw mode */ 2072d91abc21SPali Rohár if (alps_passthrough_mode_v2(psmouse, true)) 2073d91abc21SPali Rohár return -1; 2074d91abc21SPali Rohár 2075d91abc21SPali Rohár ret = alps_trackstick_enter_extended_mode_v3_v6(psmouse); 2076d91abc21SPali Rohár 207795f75e91SYunkang Tang if (alps_passthrough_mode_v2(psmouse, false)) 207895f75e91SYunkang Tang return -1; 207995f75e91SYunkang Tang 2080d91abc21SPali Rohár if (ret) 2081d91abc21SPali Rohár return ret; 2082d91abc21SPali Rohár 208395f75e91SYunkang Tang if (alps_absolute_mode_v6(psmouse)) { 208495f75e91SYunkang Tang psmouse_err(psmouse, "Failed to enable absolute mode\n"); 208595f75e91SYunkang Tang return -1; 208695f75e91SYunkang Tang } 208795f75e91SYunkang Tang 208895f75e91SYunkang Tang return 0; 208995f75e91SYunkang Tang } 209095f75e91SYunkang Tang 209125bded7cSSeth Forshee /* 2092cd401204SKevin Cernekee * Enable or disable passthrough mode to the trackstick. 209325bded7cSSeth Forshee */ 2094cd401204SKevin Cernekee static int alps_passthrough_mode_v3(struct psmouse *psmouse, 2095cd401204SKevin Cernekee int reg_base, bool enable) 209625bded7cSSeth Forshee { 2097cd401204SKevin Cernekee int reg_val, ret = -1; 209825bded7cSSeth Forshee 2099d18e53fcSKevin Cernekee if (alps_enter_command_mode(psmouse)) 210025bded7cSSeth Forshee return -1; 210125bded7cSSeth Forshee 2102cd401204SKevin Cernekee reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008); 2103cd401204SKevin Cernekee if (reg_val == -1) 2104cd401204SKevin Cernekee goto error; 2105cd401204SKevin Cernekee 210625bded7cSSeth Forshee if (enable) 210725bded7cSSeth Forshee reg_val |= 0x01; 210825bded7cSSeth Forshee else 210925bded7cSSeth Forshee reg_val &= ~0x01; 211025bded7cSSeth Forshee 2111cd401204SKevin Cernekee ret = __alps_command_mode_write_reg(psmouse, reg_val); 211225bded7cSSeth Forshee 2113cd401204SKevin Cernekee error: 2114cd401204SKevin Cernekee if (alps_exit_command_mode(psmouse)) 2115cd401204SKevin Cernekee ret = -1; 2116cd401204SKevin Cernekee return ret; 211725bded7cSSeth Forshee } 211825bded7cSSeth Forshee 211925bded7cSSeth Forshee /* Must be in command mode when calling this function */ 212025bded7cSSeth Forshee static int alps_absolute_mode_v3(struct psmouse *psmouse) 212125bded7cSSeth Forshee { 212225bded7cSSeth Forshee int reg_val; 212325bded7cSSeth Forshee 212425bded7cSSeth Forshee reg_val = alps_command_mode_read_reg(psmouse, 0x0004); 212525bded7cSSeth Forshee if (reg_val == -1) 212625bded7cSSeth Forshee return -1; 212725bded7cSSeth Forshee 212825bded7cSSeth Forshee reg_val |= 0x06; 212925bded7cSSeth Forshee if (__alps_command_mode_write_reg(psmouse, reg_val)) 213025bded7cSSeth Forshee return -1; 213125bded7cSSeth Forshee 213225bded7cSSeth Forshee return 0; 213325bded7cSSeth Forshee } 213425bded7cSSeth Forshee 2135dae928ecSPali Rohár static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base) 213625bded7cSSeth Forshee { 2137cd401204SKevin Cernekee int ret = -EIO, reg_val; 213825bded7cSSeth Forshee 2139d18e53fcSKevin Cernekee if (alps_enter_command_mode(psmouse)) 214025bded7cSSeth Forshee goto error; 214125bded7cSSeth Forshee 2142cd401204SKevin Cernekee reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08); 214325bded7cSSeth Forshee if (reg_val == -1) 214425bded7cSSeth Forshee goto error; 2145cd401204SKevin Cernekee 2146cd401204SKevin Cernekee /* bit 7: trackstick is present */ 2147cd401204SKevin Cernekee ret = reg_val & 0x80 ? 0 : -ENODEV; 2148cd401204SKevin Cernekee 2149cd401204SKevin Cernekee error: 2150cd401204SKevin Cernekee alps_exit_command_mode(psmouse); 2151cd401204SKevin Cernekee return ret; 2152cd401204SKevin Cernekee } 2153cd401204SKevin Cernekee 2154cd401204SKevin Cernekee static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base) 2155cd401204SKevin Cernekee { 2156cd401204SKevin Cernekee int ret = 0; 2157d91abc21SPali Rohár int reg_val; 2158cd401204SKevin Cernekee unsigned char param[4]; 2159cd401204SKevin Cernekee 2160d91abc21SPali Rohár /* 2161d91abc21SPali Rohár * We need to configure trackstick to report data for touchpad in 2162d91abc21SPali Rohár * extended format. And also we need to tell touchpad to expect data 2163d91abc21SPali Rohár * from trackstick in extended format. Without this configuration 2164d91abc21SPali Rohár * trackstick packets sent from touchpad are in basic format which is 2165d91abc21SPali Rohár * different from what we expect. 2166d91abc21SPali Rohár */ 2167d91abc21SPali Rohár 2168cd401204SKevin Cernekee if (alps_passthrough_mode_v3(psmouse, reg_base, true)) 2169cd401204SKevin Cernekee return -EIO; 217025bded7cSSeth Forshee 217125bded7cSSeth Forshee /* 217225bded7cSSeth Forshee * E7 report for the trackstick 217325bded7cSSeth Forshee * 217425bded7cSSeth Forshee * There have been reports of failures to seem to trace back 217525bded7cSSeth Forshee * to the above trackstick check failing. When these occur 217625bded7cSSeth Forshee * this E7 report fails, so when that happens we continue 217725bded7cSSeth Forshee * with the assumption that there isn't a trackstick after 217825bded7cSSeth Forshee * all. 217925bded7cSSeth Forshee */ 2180cd401204SKevin Cernekee if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) { 2181a09221e8SDmitry Torokhov psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n"); 2182cd401204SKevin Cernekee ret = -ENODEV; 218325bded7cSSeth Forshee } else { 218439fbe585SDmitry Torokhov psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param); 2185d91abc21SPali Rohár if (alps_trackstick_enter_extended_mode_v3_v6(psmouse)) { 2186d91abc21SPali Rohár psmouse_err(psmouse, "Failed to enter into trackstick extended mode\n"); 2187cd401204SKevin Cernekee ret = -EIO; 2188cd401204SKevin Cernekee } 2189d91abc21SPali Rohár } 2190cd401204SKevin Cernekee 2191cd401204SKevin Cernekee if (alps_passthrough_mode_v3(psmouse, reg_base, false)) 2192d91abc21SPali Rohár return -EIO; 2193d91abc21SPali Rohár 2194d91abc21SPali Rohár if (ret) 2195d91abc21SPali Rohár return ret; 2196d91abc21SPali Rohár 2197d91abc21SPali Rohár if (alps_enter_command_mode(psmouse)) 2198d91abc21SPali Rohár return -EIO; 2199d91abc21SPali Rohár 2200d91abc21SPali Rohár reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08); 2201d91abc21SPali Rohár if (reg_val == -1) { 2202cd401204SKevin Cernekee ret = -EIO; 2203d91abc21SPali Rohár } else { 2204d91abc21SPali Rohár /* 2205d91abc21SPali Rohár * Tell touchpad that trackstick is now in extended mode. 2206d91abc21SPali Rohár * If bit 1 isn't set the packet format is different. 2207d91abc21SPali Rohár */ 2208d91abc21SPali Rohár reg_val |= BIT(1); 2209d91abc21SPali Rohár if (__alps_command_mode_write_reg(psmouse, reg_val)) 2210d91abc21SPali Rohár ret = -EIO; 2211d91abc21SPali Rohár } 2212d91abc21SPali Rohár 2213d91abc21SPali Rohár if (alps_exit_command_mode(psmouse)) 2214d91abc21SPali Rohár return -EIO; 2215cd401204SKevin Cernekee 2216cd401204SKevin Cernekee return ret; 2217cd401204SKevin Cernekee } 2218cd401204SKevin Cernekee 2219cd401204SKevin Cernekee static int alps_hw_init_v3(struct psmouse *psmouse) 2220cd401204SKevin Cernekee { 22214b1af853SPali Rohár struct alps_data *priv = psmouse->private; 2222cd401204SKevin Cernekee struct ps2dev *ps2dev = &psmouse->ps2dev; 2223cd401204SKevin Cernekee int reg_val; 2224cd401204SKevin Cernekee unsigned char param[4]; 2225cd401204SKevin Cernekee 22264b1af853SPali Rohár if ((priv->flags & ALPS_DUALPOINT) && 2227cd401204SKevin Cernekee alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO) 2228cd401204SKevin Cernekee goto error; 2229cd401204SKevin Cernekee 2230d18e53fcSKevin Cernekee if (alps_enter_command_mode(psmouse) || 2231cd401204SKevin Cernekee alps_absolute_mode_v3(psmouse)) { 223225bded7cSSeth Forshee psmouse_err(psmouse, "Failed to enter absolute mode\n"); 223325bded7cSSeth Forshee goto error; 223425bded7cSSeth Forshee } 223525bded7cSSeth Forshee 223625bded7cSSeth Forshee reg_val = alps_command_mode_read_reg(psmouse, 0x0006); 223725bded7cSSeth Forshee if (reg_val == -1) 223825bded7cSSeth Forshee goto error; 223925bded7cSSeth Forshee if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01)) 224025bded7cSSeth Forshee goto error; 224125bded7cSSeth Forshee 224225bded7cSSeth Forshee reg_val = alps_command_mode_read_reg(psmouse, 0x0007); 224325bded7cSSeth Forshee if (reg_val == -1) 224425bded7cSSeth Forshee goto error; 224525bded7cSSeth Forshee if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01)) 224625bded7cSSeth Forshee goto error; 224725bded7cSSeth Forshee 224825bded7cSSeth Forshee if (alps_command_mode_read_reg(psmouse, 0x0144) == -1) 224925bded7cSSeth Forshee goto error; 225025bded7cSSeth Forshee if (__alps_command_mode_write_reg(psmouse, 0x04)) 225125bded7cSSeth Forshee goto error; 225225bded7cSSeth Forshee 225325bded7cSSeth Forshee if (alps_command_mode_read_reg(psmouse, 0x0159) == -1) 225425bded7cSSeth Forshee goto error; 225525bded7cSSeth Forshee if (__alps_command_mode_write_reg(psmouse, 0x03)) 225625bded7cSSeth Forshee goto error; 225725bded7cSSeth Forshee 225825bded7cSSeth Forshee if (alps_command_mode_read_reg(psmouse, 0x0163) == -1) 225925bded7cSSeth Forshee goto error; 226025bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03)) 226125bded7cSSeth Forshee goto error; 226225bded7cSSeth Forshee 226325bded7cSSeth Forshee if (alps_command_mode_read_reg(psmouse, 0x0162) == -1) 226425bded7cSSeth Forshee goto error; 226525bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04)) 226625bded7cSSeth Forshee goto error; 226725bded7cSSeth Forshee 226825bded7cSSeth Forshee alps_exit_command_mode(psmouse); 226925bded7cSSeth Forshee 227025bded7cSSeth Forshee /* Set rate and enable data reporting */ 227125bded7cSSeth Forshee param[0] = 0x64; 227225bded7cSSeth Forshee if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) || 227325bded7cSSeth Forshee ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) { 227425bded7cSSeth Forshee psmouse_err(psmouse, "Failed to enable data reporting\n"); 227525bded7cSSeth Forshee return -1; 227625bded7cSSeth Forshee } 227725bded7cSSeth Forshee 227825bded7cSSeth Forshee return 0; 227925bded7cSSeth Forshee 228025bded7cSSeth Forshee error: 228125bded7cSSeth Forshee /* 228225bded7cSSeth Forshee * Leaving the touchpad in command mode will essentially render 228325bded7cSSeth Forshee * it unusable until the machine reboots, so exit it here just 228425bded7cSSeth Forshee * to be safe 228525bded7cSSeth Forshee */ 228625bded7cSSeth Forshee alps_exit_command_mode(psmouse); 228725bded7cSSeth Forshee return -1; 228825bded7cSSeth Forshee } 228925bded7cSSeth Forshee 2290f3f33c67SHans de Goede static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch) 2291f3f33c67SHans de Goede { 2292f3f33c67SHans de Goede int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys; 2293f3f33c67SHans de Goede struct alps_data *priv = psmouse->private; 2294f3f33c67SHans de Goede 2295f3f33c67SHans de Goede reg = alps_command_mode_read_reg(psmouse, reg_pitch); 2296f3f33c67SHans de Goede if (reg < 0) 2297f3f33c67SHans de Goede return reg; 2298f3f33c67SHans de Goede 2299f3f33c67SHans de Goede x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */ 2300f3f33c67SHans de Goede x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */ 2301f3f33c67SHans de Goede 2302f3f33c67SHans de Goede y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */ 2303f3f33c67SHans de Goede y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */ 2304f3f33c67SHans de Goede 2305f3f33c67SHans de Goede reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1); 2306f3f33c67SHans de Goede if (reg < 0) 2307f3f33c67SHans de Goede return reg; 2308f3f33c67SHans de Goede 2309f3f33c67SHans de Goede x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */ 2310f3f33c67SHans de Goede x_electrode = 17 + x_electrode; 2311f3f33c67SHans de Goede 2312f3f33c67SHans de Goede y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */ 2313f3f33c67SHans de Goede y_electrode = 13 + y_electrode; 2314f3f33c67SHans de Goede 2315f3f33c67SHans de Goede x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */ 2316f3f33c67SHans de Goede y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */ 2317f3f33c67SHans de Goede 2318f3f33c67SHans de Goede priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */ 2319f3f33c67SHans de Goede priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */ 2320f3f33c67SHans de Goede 2321f3f33c67SHans de Goede psmouse_dbg(psmouse, 2322f3f33c67SHans de Goede "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n", 2323f3f33c67SHans de Goede x_pitch, y_pitch, x_electrode, y_electrode, 2324f3f33c67SHans de Goede x_phys / 10, y_phys / 10, priv->x_res, priv->y_res); 2325f3f33c67SHans de Goede 2326f3f33c67SHans de Goede return 0; 2327f3f33c67SHans de Goede } 2328f3f33c67SHans de Goede 23291302bac3SKevin Cernekee static int alps_hw_init_rushmore_v3(struct psmouse *psmouse) 23301302bac3SKevin Cernekee { 2331cd401204SKevin Cernekee struct alps_data *priv = psmouse->private; 23321302bac3SKevin Cernekee struct ps2dev *ps2dev = &psmouse->ps2dev; 23331302bac3SKevin Cernekee int reg_val, ret = -1; 23341302bac3SKevin Cernekee 2335cd401204SKevin Cernekee if (priv->flags & ALPS_DUALPOINT) { 2336cd401204SKevin Cernekee reg_val = alps_setup_trackstick_v3(psmouse, 2337cd401204SKevin Cernekee ALPS_REG_BASE_RUSHMORE); 2338cd401204SKevin Cernekee if (reg_val == -EIO) 2339cd401204SKevin Cernekee goto error; 2340cd401204SKevin Cernekee } 2341cd401204SKevin Cernekee 2342d18e53fcSKevin Cernekee if (alps_enter_command_mode(psmouse) || 23431302bac3SKevin Cernekee alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 || 23441302bac3SKevin Cernekee alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00)) 23451302bac3SKevin Cernekee goto error; 23461302bac3SKevin Cernekee 2347f3f33c67SHans de Goede if (alps_get_v3_v7_resolution(psmouse, 0xc2da)) 2348f3f33c67SHans de Goede goto error; 2349f3f33c67SHans de Goede 23501302bac3SKevin Cernekee reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6); 23511302bac3SKevin Cernekee if (reg_val == -1) 23521302bac3SKevin Cernekee goto error; 23531302bac3SKevin Cernekee if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd)) 23541302bac3SKevin Cernekee goto error; 23551302bac3SKevin Cernekee 23561302bac3SKevin Cernekee if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64)) 23571302bac3SKevin Cernekee goto error; 23581302bac3SKevin Cernekee 23591302bac3SKevin Cernekee /* enter absolute mode */ 23601302bac3SKevin Cernekee reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4); 23611302bac3SKevin Cernekee if (reg_val == -1) 23621302bac3SKevin Cernekee goto error; 23631302bac3SKevin Cernekee if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02)) 23641302bac3SKevin Cernekee goto error; 23651302bac3SKevin Cernekee 23661302bac3SKevin Cernekee alps_exit_command_mode(psmouse); 23671302bac3SKevin Cernekee return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE); 23681302bac3SKevin Cernekee 23691302bac3SKevin Cernekee error: 23701302bac3SKevin Cernekee alps_exit_command_mode(psmouse); 23711302bac3SKevin Cernekee return ret; 23721302bac3SKevin Cernekee } 23731302bac3SKevin Cernekee 237425bded7cSSeth Forshee /* Must be in command mode when calling this function */ 237525bded7cSSeth Forshee static int alps_absolute_mode_v4(struct psmouse *psmouse) 237625bded7cSSeth Forshee { 237725bded7cSSeth Forshee int reg_val; 237825bded7cSSeth Forshee 237925bded7cSSeth Forshee reg_val = alps_command_mode_read_reg(psmouse, 0x0004); 238025bded7cSSeth Forshee if (reg_val == -1) 238125bded7cSSeth Forshee return -1; 238225bded7cSSeth Forshee 238325bded7cSSeth Forshee reg_val |= 0x02; 238425bded7cSSeth Forshee if (__alps_command_mode_write_reg(psmouse, reg_val)) 238525bded7cSSeth Forshee return -1; 238625bded7cSSeth Forshee 238725bded7cSSeth Forshee return 0; 238825bded7cSSeth Forshee } 238925bded7cSSeth Forshee 239025bded7cSSeth Forshee static int alps_hw_init_v4(struct psmouse *psmouse) 239125bded7cSSeth Forshee { 239225bded7cSSeth Forshee struct ps2dev *ps2dev = &psmouse->ps2dev; 239325bded7cSSeth Forshee unsigned char param[4]; 239425bded7cSSeth Forshee 2395d18e53fcSKevin Cernekee if (alps_enter_command_mode(psmouse)) 239625bded7cSSeth Forshee goto error; 239725bded7cSSeth Forshee 239825bded7cSSeth Forshee if (alps_absolute_mode_v4(psmouse)) { 239925bded7cSSeth Forshee psmouse_err(psmouse, "Failed to enter absolute mode\n"); 240025bded7cSSeth Forshee goto error; 240125bded7cSSeth Forshee } 240225bded7cSSeth Forshee 240325bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c)) 240425bded7cSSeth Forshee goto error; 240525bded7cSSeth Forshee 240625bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03)) 240725bded7cSSeth Forshee goto error; 240825bded7cSSeth Forshee 240925bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03)) 241025bded7cSSeth Forshee goto error; 241125bded7cSSeth Forshee 241225bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15)) 241325bded7cSSeth Forshee goto error; 241425bded7cSSeth Forshee 241525bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01)) 241625bded7cSSeth Forshee goto error; 241725bded7cSSeth Forshee 241825bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03)) 241925bded7cSSeth Forshee goto error; 242025bded7cSSeth Forshee 242125bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03)) 242225bded7cSSeth Forshee goto error; 242325bded7cSSeth Forshee 242425bded7cSSeth Forshee if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03)) 242525bded7cSSeth Forshee goto error; 242625bded7cSSeth Forshee 242725bded7cSSeth Forshee alps_exit_command_mode(psmouse); 242825bded7cSSeth Forshee 242925bded7cSSeth Forshee /* 243025bded7cSSeth Forshee * This sequence changes the output from a 9-byte to an 243125bded7cSSeth Forshee * 8-byte format. All the same data seems to be present, 243225bded7cSSeth Forshee * just in a more compact format. 243325bded7cSSeth Forshee */ 243425bded7cSSeth Forshee param[0] = 0xc8; 243525bded7cSSeth Forshee param[1] = 0x64; 243625bded7cSSeth Forshee param[2] = 0x50; 243725bded7cSSeth Forshee if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) || 243825bded7cSSeth Forshee ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) || 243925bded7cSSeth Forshee ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) || 244025bded7cSSeth Forshee ps2_command(ps2dev, param, PSMOUSE_CMD_GETID)) 244125bded7cSSeth Forshee return -1; 244225bded7cSSeth Forshee 244325bded7cSSeth Forshee /* Set rate and enable data reporting */ 244425bded7cSSeth Forshee param[0] = 0x64; 244525bded7cSSeth Forshee if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) || 244625bded7cSSeth Forshee ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) { 244725bded7cSSeth Forshee psmouse_err(psmouse, "Failed to enable data reporting\n"); 244825bded7cSSeth Forshee return -1; 244925bded7cSSeth Forshee } 245025bded7cSSeth Forshee 245125bded7cSSeth Forshee return 0; 245225bded7cSSeth Forshee 245325bded7cSSeth Forshee error: 245425bded7cSSeth Forshee /* 245525bded7cSSeth Forshee * Leaving the touchpad in command mode will essentially render 245625bded7cSSeth Forshee * it unusable until the machine reboots, so exit it here just 245725bded7cSSeth Forshee * to be safe 245825bded7cSSeth Forshee */ 245925bded7cSSeth Forshee alps_exit_command_mode(psmouse); 246025bded7cSSeth Forshee return -1; 246125bded7cSSeth Forshee } 246225bded7cSSeth Forshee 24633db5b9f7SMasaki Ota static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse, 24643db5b9f7SMasaki Ota unsigned char index, unsigned char otp[]) 24653db5b9f7SMasaki Ota { 24663db5b9f7SMasaki Ota struct ps2dev *ps2dev = &psmouse->ps2dev; 24673db5b9f7SMasaki Ota 24683db5b9f7SMasaki Ota switch (index) { 24693db5b9f7SMasaki Ota case 0: 24703db5b9f7SMasaki Ota if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) || 24713db5b9f7SMasaki Ota ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) || 24723db5b9f7SMasaki Ota ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO)) 24733db5b9f7SMasaki Ota return -1; 24743db5b9f7SMasaki Ota 24753db5b9f7SMasaki Ota break; 24763db5b9f7SMasaki Ota 24773db5b9f7SMasaki Ota case 1: 24783db5b9f7SMasaki Ota if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) || 24793db5b9f7SMasaki Ota ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) || 24803db5b9f7SMasaki Ota ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO)) 24813db5b9f7SMasaki Ota return -1; 24823db5b9f7SMasaki Ota 24833db5b9f7SMasaki Ota break; 24843db5b9f7SMasaki Ota } 24853db5b9f7SMasaki Ota 24863db5b9f7SMasaki Ota return 0; 24873db5b9f7SMasaki Ota } 24883db5b9f7SMasaki Ota 248907f19e3dSFengguang Wu static int alps_update_device_area_ss4_v2(unsigned char otp[][4], 24903db5b9f7SMasaki Ota struct alps_data *priv) 24913db5b9f7SMasaki Ota { 24923db5b9f7SMasaki Ota int num_x_electrode; 24933db5b9f7SMasaki Ota int num_y_electrode; 24943db5b9f7SMasaki Ota int x_pitch, y_pitch, x_phys, y_phys; 24953db5b9f7SMasaki Ota 2496e7348396SMasaki Ota if (IS_SS4PLUS_DEV(priv->dev_id)) { 2497e7348396SMasaki Ota num_x_electrode = 2498e7348396SMasaki Ota SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F); 2499e7348396SMasaki Ota num_y_electrode = 2500e7348396SMasaki Ota SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F); 25013db5b9f7SMasaki Ota 2502e7348396SMasaki Ota priv->x_max = 2503e7348396SMasaki Ota (num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE; 2504e7348396SMasaki Ota priv->y_max = 2505e7348396SMasaki Ota (num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE; 2506e7348396SMasaki Ota 2507e7348396SMasaki Ota x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM; 2508e7348396SMasaki Ota y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM; 2509e7348396SMasaki Ota 2510e7348396SMasaki Ota } else { 2511e7348396SMasaki Ota num_x_electrode = 2512e7348396SMasaki Ota SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F); 2513e7348396SMasaki Ota num_y_electrode = 2514e7348396SMasaki Ota SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F); 2515e7348396SMasaki Ota 2516e7348396SMasaki Ota priv->x_max = 2517e7348396SMasaki Ota (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE; 2518e7348396SMasaki Ota priv->y_max = 2519e7348396SMasaki Ota (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE; 25203db5b9f7SMasaki Ota 25213db5b9f7SMasaki Ota x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM; 25223db5b9f7SMasaki Ota y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM; 2523e7348396SMasaki Ota } 25243db5b9f7SMasaki Ota 25253db5b9f7SMasaki Ota x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */ 25263db5b9f7SMasaki Ota y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */ 25273db5b9f7SMasaki Ota 25283db5b9f7SMasaki Ota priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */ 25293db5b9f7SMasaki Ota priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */ 25303db5b9f7SMasaki Ota 25313db5b9f7SMasaki Ota return 0; 25323db5b9f7SMasaki Ota } 25333db5b9f7SMasaki Ota 253407f19e3dSFengguang Wu static int alps_update_btn_info_ss4_v2(unsigned char otp[][4], 253507f19e3dSFengguang Wu struct alps_data *priv) 25363db5b9f7SMasaki Ota { 25373db5b9f7SMasaki Ota unsigned char is_btnless; 25383db5b9f7SMasaki Ota 2539e7348396SMasaki Ota if (IS_SS4PLUS_DEV(priv->dev_id)) 2540e7348396SMasaki Ota is_btnless = (otp[1][0] >> 1) & 0x01; 2541e7348396SMasaki Ota else 25423db5b9f7SMasaki Ota is_btnless = (otp[1][1] >> 3) & 0x01; 25433db5b9f7SMasaki Ota 25443db5b9f7SMasaki Ota if (is_btnless) 25453db5b9f7SMasaki Ota priv->flags |= ALPS_BUTTONPAD; 25463db5b9f7SMasaki Ota 25473db5b9f7SMasaki Ota return 0; 25483db5b9f7SMasaki Ota } 25493db5b9f7SMasaki Ota 2550e7348396SMasaki Ota static int alps_update_dual_info_ss4_v2(unsigned char otp[][4], 2551567b9b54SMasaki Ota struct alps_data *priv, 2552567b9b54SMasaki Ota struct psmouse *psmouse) 2553e7348396SMasaki Ota { 2554e7348396SMasaki Ota bool is_dual = false; 2555567b9b54SMasaki Ota int reg_val = 0; 2556567b9b54SMasaki Ota struct ps2dev *ps2dev = &psmouse->ps2dev; 2557e7348396SMasaki Ota 2558567b9b54SMasaki Ota if (IS_SS4PLUS_DEV(priv->dev_id)) { 2559e7348396SMasaki Ota is_dual = (otp[0][0] >> 4) & 0x01; 2560e7348396SMasaki Ota 2561567b9b54SMasaki Ota if (!is_dual) { 2562567b9b54SMasaki Ota /* For support TrackStick of Thinkpad L/E series */ 2563567b9b54SMasaki Ota if (alps_exit_command_mode(psmouse) == 0 && 2564567b9b54SMasaki Ota alps_enter_command_mode(psmouse) == 0) { 2565567b9b54SMasaki Ota reg_val = alps_command_mode_read_reg(psmouse, 2566567b9b54SMasaki Ota 0xD7); 2567567b9b54SMasaki Ota } 2568567b9b54SMasaki Ota alps_exit_command_mode(psmouse); 2569567b9b54SMasaki Ota ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE); 2570567b9b54SMasaki Ota 2571567b9b54SMasaki Ota if (reg_val == 0x0C || reg_val == 0x1D) 2572567b9b54SMasaki Ota is_dual = true; 2573567b9b54SMasaki Ota } 2574567b9b54SMasaki Ota } 2575567b9b54SMasaki Ota 2576e7348396SMasaki Ota if (is_dual) 2577e7348396SMasaki Ota priv->flags |= ALPS_DUALPOINT | 2578e7348396SMasaki Ota ALPS_DUALPOINT_WITH_PRESSURE; 2579e7348396SMasaki Ota 2580e7348396SMasaki Ota return 0; 2581e7348396SMasaki Ota } 2582e7348396SMasaki Ota 25833db5b9f7SMasaki Ota static int alps_set_defaults_ss4_v2(struct psmouse *psmouse, 25843db5b9f7SMasaki Ota struct alps_data *priv) 25853db5b9f7SMasaki Ota { 25863db5b9f7SMasaki Ota unsigned char otp[2][4]; 25873db5b9f7SMasaki Ota 25883db5b9f7SMasaki Ota memset(otp, 0, sizeof(otp)); 25893db5b9f7SMasaki Ota 25904a646580SMasaki Ota if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) || 25914a646580SMasaki Ota alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0])) 25923db5b9f7SMasaki Ota return -1; 25933db5b9f7SMasaki Ota 25943db5b9f7SMasaki Ota alps_update_device_area_ss4_v2(otp, priv); 25953db5b9f7SMasaki Ota 25963db5b9f7SMasaki Ota alps_update_btn_info_ss4_v2(otp, priv); 25973db5b9f7SMasaki Ota 2598567b9b54SMasaki Ota alps_update_dual_info_ss4_v2(otp, priv, psmouse); 2599e7348396SMasaki Ota 26003db5b9f7SMasaki Ota return 0; 26013db5b9f7SMasaki Ota } 26023db5b9f7SMasaki Ota 2603ee65d4b3SYunkang Tang static int alps_dolphin_get_device_area(struct psmouse *psmouse, 2604ee65d4b3SYunkang Tang struct alps_data *priv) 2605ee65d4b3SYunkang Tang { 2606ee65d4b3SYunkang Tang struct ps2dev *ps2dev = &psmouse->ps2dev; 2607ee65d4b3SYunkang Tang unsigned char param[4] = {0}; 2608ee65d4b3SYunkang Tang int num_x_electrode, num_y_electrode; 2609ee65d4b3SYunkang Tang 2610ee65d4b3SYunkang Tang if (alps_enter_command_mode(psmouse)) 2611ee65d4b3SYunkang Tang return -1; 2612ee65d4b3SYunkang Tang 2613ee65d4b3SYunkang Tang param[0] = 0x0a; 2614ee65d4b3SYunkang Tang if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) || 2615ee65d4b3SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) || 2616ee65d4b3SYunkang Tang ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) || 2617ee65d4b3SYunkang Tang ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) || 2618ee65d4b3SYunkang Tang ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE)) 2619ee65d4b3SYunkang Tang return -1; 2620ee65d4b3SYunkang Tang 2621ee65d4b3SYunkang Tang if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) 2622ee65d4b3SYunkang Tang return -1; 2623ee65d4b3SYunkang Tang 2624ee65d4b3SYunkang Tang /* 2625ee65d4b3SYunkang Tang * Dolphin's sensor line number is not fixed. It can be calculated 2626ee65d4b3SYunkang Tang * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET. 2627ee65d4b3SYunkang Tang * Further more, we can get device's x_max and y_max by multiplying 2628ee65d4b3SYunkang Tang * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE. 2629ee65d4b3SYunkang Tang * 2630ee65d4b3SYunkang Tang * e.g. When we get register's sensor_x = 11 & sensor_y = 8, 2631ee65d4b3SYunkang Tang * real sensor line number X = 11 + 8 = 19, and 2632ee65d4b3SYunkang Tang * real sensor line number Y = 8 + 1 = 9. 2633ee65d4b3SYunkang Tang * So, x_max = (19 - 1) * 64 = 1152, and 2634ee65d4b3SYunkang Tang * y_max = (9 - 1) * 64 = 512. 2635ee65d4b3SYunkang Tang */ 2636ee65d4b3SYunkang Tang num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F); 2637ee65d4b3SYunkang Tang num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F); 2638ee65d4b3SYunkang Tang priv->x_bits = num_x_electrode; 2639ee65d4b3SYunkang Tang priv->y_bits = num_y_electrode; 2640ee65d4b3SYunkang Tang priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE; 2641ee65d4b3SYunkang Tang priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE; 2642ee65d4b3SYunkang Tang 2643ee65d4b3SYunkang Tang if (alps_exit_command_mode(psmouse)) 2644ee65d4b3SYunkang Tang return -1; 2645ee65d4b3SYunkang Tang 2646ee65d4b3SYunkang Tang return 0; 2647ee65d4b3SYunkang Tang } 2648ee65d4b3SYunkang Tang 264975af9e56SDave Turvene static int alps_hw_init_dolphin_v1(struct psmouse *psmouse) 265075af9e56SDave Turvene { 265175af9e56SDave Turvene struct ps2dev *ps2dev = &psmouse->ps2dev; 265275af9e56SDave Turvene unsigned char param[2]; 265375af9e56SDave Turvene 265475af9e56SDave Turvene /* This is dolphin "v1" as empirically defined by florin9doi */ 265575af9e56SDave Turvene param[0] = 0x64; 265675af9e56SDave Turvene param[1] = 0x28; 265775af9e56SDave Turvene 265875af9e56SDave Turvene if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) || 265975af9e56SDave Turvene ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) || 266075af9e56SDave Turvene ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE)) 266175af9e56SDave Turvene return -1; 266275af9e56SDave Turvene 266375af9e56SDave Turvene return 0; 266475af9e56SDave Turvene } 266575af9e56SDave Turvene 26663808843cSYunkang Tang static int alps_hw_init_v7(struct psmouse *psmouse) 26673808843cSYunkang Tang { 26683808843cSYunkang Tang struct ps2dev *ps2dev = &psmouse->ps2dev; 26693808843cSYunkang Tang int reg_val, ret = -1; 26703808843cSYunkang Tang 26713808843cSYunkang Tang if (alps_enter_command_mode(psmouse) || 26723808843cSYunkang Tang alps_command_mode_read_reg(psmouse, 0xc2d9) == -1) 26733808843cSYunkang Tang goto error; 26743808843cSYunkang Tang 2675f3f33c67SHans de Goede if (alps_get_v3_v7_resolution(psmouse, 0xc397)) 2676f3f33c67SHans de Goede goto error; 2677f3f33c67SHans de Goede 26783808843cSYunkang Tang if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64)) 26793808843cSYunkang Tang goto error; 26803808843cSYunkang Tang 26813808843cSYunkang Tang reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4); 26823808843cSYunkang Tang if (reg_val == -1) 26833808843cSYunkang Tang goto error; 26843808843cSYunkang Tang if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02)) 26853808843cSYunkang Tang goto error; 26863808843cSYunkang Tang 26873808843cSYunkang Tang alps_exit_command_mode(psmouse); 26883808843cSYunkang Tang return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE); 26893808843cSYunkang Tang 26903808843cSYunkang Tang error: 26913808843cSYunkang Tang alps_exit_command_mode(psmouse); 26923808843cSYunkang Tang return ret; 26933808843cSYunkang Tang } 26943808843cSYunkang Tang 26953db5b9f7SMasaki Ota static int alps_hw_init_ss4_v2(struct psmouse *psmouse) 26963db5b9f7SMasaki Ota { 26973db5b9f7SMasaki Ota struct ps2dev *ps2dev = &psmouse->ps2dev; 26983db5b9f7SMasaki Ota char param[2] = {0x64, 0x28}; 26993db5b9f7SMasaki Ota int ret = -1; 27003db5b9f7SMasaki Ota 27013db5b9f7SMasaki Ota /* enter absolute mode */ 27023db5b9f7SMasaki Ota if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) || 27033db5b9f7SMasaki Ota ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) || 27043db5b9f7SMasaki Ota ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) || 27053db5b9f7SMasaki Ota ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE)) { 27063db5b9f7SMasaki Ota goto error; 27073db5b9f7SMasaki Ota } 27083db5b9f7SMasaki Ota 27093db5b9f7SMasaki Ota /* T.B.D. Decread noise packet number, delete in the future */ 27103db5b9f7SMasaki Ota if (alps_exit_command_mode(psmouse) || 27113db5b9f7SMasaki Ota alps_enter_command_mode(psmouse) || 27123db5b9f7SMasaki Ota alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) { 27133db5b9f7SMasaki Ota goto error; 27143db5b9f7SMasaki Ota } 27153db5b9f7SMasaki Ota alps_exit_command_mode(psmouse); 27163db5b9f7SMasaki Ota 27173db5b9f7SMasaki Ota return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE); 27183db5b9f7SMasaki Ota 27193db5b9f7SMasaki Ota error: 27203db5b9f7SMasaki Ota alps_exit_command_mode(psmouse); 27213db5b9f7SMasaki Ota return ret; 27223db5b9f7SMasaki Ota } 27233db5b9f7SMasaki Ota 27243296f71cSDmitry Torokhov static int alps_set_protocol(struct psmouse *psmouse, 27253296f71cSDmitry Torokhov struct alps_data *priv, 27263296f71cSDmitry Torokhov const struct alps_protocol_info *protocol) 272725bded7cSSeth Forshee { 27283296f71cSDmitry Torokhov psmouse->private = priv; 27293296f71cSDmitry Torokhov 273017a58edcSKees Cook timer_setup(&priv->timer, alps_flush_packet, 0); 27313296f71cSDmitry Torokhov 27323296f71cSDmitry Torokhov priv->proto_version = protocol->version; 27333296f71cSDmitry Torokhov priv->byte0 = protocol->byte0; 27343296f71cSDmitry Torokhov priv->mask0 = protocol->mask0; 27353296f71cSDmitry Torokhov priv->flags = protocol->flags; 2736f673ceb1SKevin Cernekee 27377a9f73e7SKevin Cernekee priv->x_max = 2000; 27387a9f73e7SKevin Cernekee priv->y_max = 1400; 27397a9f73e7SKevin Cernekee priv->x_bits = 15; 27407a9f73e7SKevin Cernekee priv->y_bits = 11; 27417a9f73e7SKevin Cernekee 274299df65e7SKevin Cernekee switch (priv->proto_version) { 274325bded7cSSeth Forshee case ALPS_PROTO_V1: 274425bded7cSSeth Forshee case ALPS_PROTO_V2: 274524af5cb9SKevin Cernekee priv->hw_init = alps_hw_init_v1_v2; 274624af5cb9SKevin Cernekee priv->process_packet = alps_process_packet_v1_v2; 274724af5cb9SKevin Cernekee priv->set_abs_params = alps_set_abs_params_st; 274895f75e91SYunkang Tang priv->x_max = 1023; 274995f75e91SYunkang Tang priv->y_max = 767; 275019556219SHans de Goede if (dmi_check_system(alps_dmi_has_separate_stick_buttons)) 275119556219SHans de Goede priv->flags |= ALPS_STICK_BITS; 275225bded7cSSeth Forshee break; 2753fb2dd7a6SDmitry Torokhov 275425bded7cSSeth Forshee case ALPS_PROTO_V3: 275524af5cb9SKevin Cernekee priv->hw_init = alps_hw_init_v3; 275624af5cb9SKevin Cernekee priv->process_packet = alps_process_packet_v3; 2757688ea364SHans de Goede priv->set_abs_params = alps_set_abs_params_semi_mt; 2758f85e5001SKevin Cernekee priv->decode_fields = alps_decode_pinnacle; 275950e8b216SKevin Cernekee priv->nibble_commands = alps_v3_nibble_commands; 276050e8b216SKevin Cernekee priv->addr_command = PSMOUSE_CMD_RESET_WRAP; 27614b1af853SPali Rohár 27624b1af853SPali Rohár if (alps_probe_trackstick_v3_v7(psmouse, 27634b1af853SPali Rohár ALPS_REG_BASE_PINNACLE) < 0) 27644b1af853SPali Rohár priv->flags &= ~ALPS_DUALPOINT; 27654b1af853SPali Rohár 276625bded7cSSeth Forshee break; 2767fb2dd7a6SDmitry Torokhov 2768fb2dd7a6SDmitry Torokhov case ALPS_PROTO_V3_RUSHMORE: 2769fb2dd7a6SDmitry Torokhov priv->hw_init = alps_hw_init_rushmore_v3; 2770fb2dd7a6SDmitry Torokhov priv->process_packet = alps_process_packet_v3; 2771688ea364SHans de Goede priv->set_abs_params = alps_set_abs_params_semi_mt; 2772fb2dd7a6SDmitry Torokhov priv->decode_fields = alps_decode_rushmore; 2773fb2dd7a6SDmitry Torokhov priv->nibble_commands = alps_v3_nibble_commands; 2774fb2dd7a6SDmitry Torokhov priv->addr_command = PSMOUSE_CMD_RESET_WRAP; 2775fb2dd7a6SDmitry Torokhov priv->x_bits = 16; 2776fb2dd7a6SDmitry Torokhov priv->y_bits = 12; 27773296f71cSDmitry Torokhov 2778dae928ecSPali Rohár if (alps_probe_trackstick_v3_v7(psmouse, 27793296f71cSDmitry Torokhov ALPS_REG_BASE_RUSHMORE) < 0) 27803296f71cSDmitry Torokhov priv->flags &= ~ALPS_DUALPOINT; 27813296f71cSDmitry Torokhov 2782fb2dd7a6SDmitry Torokhov break; 2783fb2dd7a6SDmitry Torokhov 278425bded7cSSeth Forshee case ALPS_PROTO_V4: 278524af5cb9SKevin Cernekee priv->hw_init = alps_hw_init_v4; 278624af5cb9SKevin Cernekee priv->process_packet = alps_process_packet_v4; 2787688ea364SHans de Goede priv->set_abs_params = alps_set_abs_params_semi_mt; 278850e8b216SKevin Cernekee priv->nibble_commands = alps_v4_nibble_commands; 278950e8b216SKevin Cernekee priv->addr_command = PSMOUSE_CMD_DISABLE; 279025bded7cSSeth Forshee break; 27913296f71cSDmitry Torokhov 279275af9e56SDave Turvene case ALPS_PROTO_V5: 279375af9e56SDave Turvene priv->hw_init = alps_hw_init_dolphin_v1; 2794ee65d4b3SYunkang Tang priv->process_packet = alps_process_touchpad_packet_v3_v5; 279575af9e56SDave Turvene priv->decode_fields = alps_decode_dolphin; 2796688ea364SHans de Goede priv->set_abs_params = alps_set_abs_params_semi_mt; 279775af9e56SDave Turvene priv->nibble_commands = alps_v3_nibble_commands; 279875af9e56SDave Turvene priv->addr_command = PSMOUSE_CMD_RESET_WRAP; 279975af9e56SDave Turvene priv->x_bits = 23; 280075af9e56SDave Turvene priv->y_bits = 12; 2801c164c147SDmitry Torokhov 2802c164c147SDmitry Torokhov if (alps_dolphin_get_device_area(psmouse, priv)) 2803c164c147SDmitry Torokhov return -EIO; 2804c164c147SDmitry Torokhov 280575af9e56SDave Turvene break; 28063296f71cSDmitry Torokhov 280795f75e91SYunkang Tang case ALPS_PROTO_V6: 280895f75e91SYunkang Tang priv->hw_init = alps_hw_init_v6; 280995f75e91SYunkang Tang priv->process_packet = alps_process_packet_v6; 281095f75e91SYunkang Tang priv->set_abs_params = alps_set_abs_params_st; 281195f75e91SYunkang Tang priv->nibble_commands = alps_v6_nibble_commands; 281295f75e91SYunkang Tang priv->x_max = 2047; 281395f75e91SYunkang Tang priv->y_max = 1535; 281495f75e91SYunkang Tang break; 28153296f71cSDmitry Torokhov 28163808843cSYunkang Tang case ALPS_PROTO_V7: 28173808843cSYunkang Tang priv->hw_init = alps_hw_init_v7; 28183808843cSYunkang Tang priv->process_packet = alps_process_packet_v7; 28193808843cSYunkang Tang priv->decode_fields = alps_decode_packet_v7; 28208eccd393SMasaki Ota priv->set_abs_params = alps_set_abs_params_v7; 28213808843cSYunkang Tang priv->nibble_commands = alps_v3_nibble_commands; 28223808843cSYunkang Tang priv->addr_command = PSMOUSE_CMD_RESET_WRAP; 2823c164c147SDmitry Torokhov priv->x_max = 0xfff; 2824c164c147SDmitry Torokhov priv->y_max = 0x7ff; 28253808843cSYunkang Tang 28263808843cSYunkang Tang if (priv->fw_ver[1] != 0xba) 28273808843cSYunkang Tang priv->flags |= ALPS_BUTTONPAD; 28283296f71cSDmitry Torokhov 2829dae928ecSPali Rohár if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0) 2830dae928ecSPali Rohár priv->flags &= ~ALPS_DUALPOINT; 2831dae928ecSPali Rohár 28323808843cSYunkang Tang break; 28333db5b9f7SMasaki Ota 28343db5b9f7SMasaki Ota case ALPS_PROTO_V8: 28353db5b9f7SMasaki Ota priv->hw_init = alps_hw_init_ss4_v2; 28363db5b9f7SMasaki Ota priv->process_packet = alps_process_packet_ss4_v2; 28373db5b9f7SMasaki Ota priv->decode_fields = alps_decode_ss4_v2; 28383db5b9f7SMasaki Ota priv->set_abs_params = alps_set_abs_params_ss4_v2; 28393db5b9f7SMasaki Ota priv->nibble_commands = alps_v3_nibble_commands; 28403db5b9f7SMasaki Ota priv->addr_command = PSMOUSE_CMD_RESET_WRAP; 28413db5b9f7SMasaki Ota 28423db5b9f7SMasaki Ota if (alps_set_defaults_ss4_v2(psmouse, priv)) 28433db5b9f7SMasaki Ota return -EIO; 28443db5b9f7SMasaki Ota 28453db5b9f7SMasaki Ota break; 284625bded7cSSeth Forshee } 28473296f71cSDmitry Torokhov 28483296f71cSDmitry Torokhov return 0; 284925bded7cSSeth Forshee } 285025bded7cSSeth Forshee 28513296f71cSDmitry Torokhov static const struct alps_protocol_info *alps_match_table(unsigned char *e7, 28523296f71cSDmitry Torokhov unsigned char *ec) 28532e992cc0SKevin Cernekee { 2854b5d6b851SKevin Cernekee const struct alps_model_info *model; 28552e992cc0SKevin Cernekee int i; 28562e992cc0SKevin Cernekee 2857b5d6b851SKevin Cernekee for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) { 2858b5d6b851SKevin Cernekee model = &alps_model_data[i]; 2859b5d6b851SKevin Cernekee 2860a3cbfd56SPali Rohár if (!memcmp(e7, model->signature, sizeof(model->signature))) 28613296f71cSDmitry Torokhov return &model->protocol_info; 2862b5d6b851SKevin Cernekee } 2863b5d6b851SKevin Cernekee 28643296f71cSDmitry Torokhov return NULL; 2865b5d6b851SKevin Cernekee } 2866b5d6b851SKevin Cernekee 2867b5d6b851SKevin Cernekee static int alps_identify(struct psmouse *psmouse, struct alps_data *priv) 2868b5d6b851SKevin Cernekee { 28693296f71cSDmitry Torokhov const struct alps_protocol_info *protocol; 2870b5d6b851SKevin Cernekee unsigned char e6[4], e7[4], ec[4]; 2871a09221e8SDmitry Torokhov int error; 2872b5d6b851SKevin Cernekee 28732e992cc0SKevin Cernekee /* 28742e992cc0SKevin Cernekee * First try "E6 report". 28752e992cc0SKevin Cernekee * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed. 28762e992cc0SKevin Cernekee * The bits 0-2 of the first byte will be 1s if some buttons are 28772e992cc0SKevin Cernekee * pressed. 28782e992cc0SKevin Cernekee */ 2879b5d6b851SKevin Cernekee if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES, 2880b5d6b851SKevin Cernekee PSMOUSE_CMD_SETSCALE11, e6)) 2881b5d6b851SKevin Cernekee return -EIO; 28822e992cc0SKevin Cernekee 2883b5d6b851SKevin Cernekee if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100)) 2884b5d6b851SKevin Cernekee return -EINVAL; 28852e992cc0SKevin Cernekee 28862e992cc0SKevin Cernekee /* 2887b5d6b851SKevin Cernekee * Now get the "E7" and "EC" reports. These will uniquely identify 2888b5d6b851SKevin Cernekee * most ALPS touchpads. 28892e992cc0SKevin Cernekee */ 2890b5d6b851SKevin Cernekee if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES, 2891b5d6b851SKevin Cernekee PSMOUSE_CMD_SETSCALE21, e7) || 2892b5d6b851SKevin Cernekee alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES, 2893b5d6b851SKevin Cernekee PSMOUSE_CMD_RESET_WRAP, ec) || 2894b5d6b851SKevin Cernekee alps_exit_command_mode(psmouse)) 2895b5d6b851SKevin Cernekee return -EIO; 28962e992cc0SKevin Cernekee 28973296f71cSDmitry Torokhov protocol = alps_match_table(e7, ec); 28983296f71cSDmitry Torokhov if (!protocol) { 289909c398bcSPali Rohár if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 && 290009c398bcSPali Rohár ec[2] == 0x8a) { 290109c398bcSPali Rohár protocol = &alps_v4_protocol_data; 290209c398bcSPali Rohár } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 && 29033296f71cSDmitry Torokhov ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) { 29043296f71cSDmitry Torokhov protocol = &alps_v5_protocol_data; 29053296f71cSDmitry Torokhov } else if (ec[0] == 0x88 && 29063296f71cSDmitry Torokhov ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) { 29073296f71cSDmitry Torokhov protocol = &alps_v7_protocol_data; 29083296f71cSDmitry Torokhov } else if (ec[0] == 0x88 && ec[1] == 0x08) { 29093296f71cSDmitry Torokhov protocol = &alps_v3_rushmore_data; 29103296f71cSDmitry Torokhov } else if (ec[0] == 0x88 && ec[1] == 0x07 && 29113296f71cSDmitry Torokhov ec[2] >= 0x90 && ec[2] <= 0x9d) { 29123296f71cSDmitry Torokhov protocol = &alps_v3_protocol_data; 29133db5b9f7SMasaki Ota } else if (e7[0] == 0x73 && e7[1] == 0x03 && 2914e7348396SMasaki Ota (e7[2] == 0x14 || e7[2] == 0x28)) { 2915aeaa881fSBen Gamari protocol = &alps_v8_protocol_data; 2916c9815232SPali Rohár } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) { 2917c9815232SPali Rohár protocol = &alps_v9_protocol_data; 2918c9815232SPali Rohár psmouse_warn(psmouse, 2919c9815232SPali Rohár "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n", 2920c9815232SPali Rohár e7, ec); 2921c9815232SPali Rohár return -EINVAL; 29223296f71cSDmitry Torokhov } else { 29233296f71cSDmitry Torokhov psmouse_dbg(psmouse, 29243296f71cSDmitry Torokhov "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec); 29253296f71cSDmitry Torokhov return -EINVAL; 29263296f71cSDmitry Torokhov } 29273296f71cSDmitry Torokhov } 29283296f71cSDmitry Torokhov 2929a09221e8SDmitry Torokhov if (priv) { 2930e7348396SMasaki Ota /* Save Device ID and Firmware version */ 2931e7348396SMasaki Ota memcpy(priv->dev_id, e7, 3); 2932c0cd17f6SHans de Goede memcpy(priv->fw_ver, ec, 3); 2933a09221e8SDmitry Torokhov error = alps_set_protocol(psmouse, priv, protocol); 2934a09221e8SDmitry Torokhov if (error) 2935a09221e8SDmitry Torokhov return error; 2936a09221e8SDmitry Torokhov } 2937c0cd17f6SHans de Goede 2938a09221e8SDmitry Torokhov return 0; 29392e992cc0SKevin Cernekee } 29402e992cc0SKevin Cernekee 29411e0c5b12SDmitry Torokhov static int alps_reconnect(struct psmouse *psmouse) 29421e0c5b12SDmitry Torokhov { 2943b5d6b851SKevin Cernekee struct alps_data *priv = psmouse->private; 294471bb21b6SMaxim Levitsky 29451e0c5b12SDmitry Torokhov psmouse_reset(psmouse); 29461e0c5b12SDmitry Torokhov 2947b5d6b851SKevin Cernekee if (alps_identify(psmouse, priv) < 0) 29481e0c5b12SDmitry Torokhov return -1; 29491e0c5b12SDmitry Torokhov 295024af5cb9SKevin Cernekee return priv->hw_init(psmouse); 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds static void alps_disconnect(struct psmouse *psmouse) 29541da177e4SLinus Torvalds { 29551da177e4SLinus Torvalds struct alps_data *priv = psmouse->private; 29562e5b636bSDmitry Torokhov 29571da177e4SLinus Torvalds psmouse_reset(psmouse); 29581d9f2626SSebastian Kapfer del_timer_sync(&priv->timer); 295904aae283SPali Rohár if (priv->dev2) 29602e5b636bSDmitry Torokhov input_unregister_device(priv->dev2); 296104aae283SPali Rohár if (!IS_ERR_OR_NULL(priv->dev3)) 296204aae283SPali Rohár input_unregister_device(priv->dev3); 29631da177e4SLinus Torvalds kfree(priv); 29641da177e4SLinus Torvalds } 29651da177e4SLinus Torvalds 296624af5cb9SKevin Cernekee static void alps_set_abs_params_st(struct alps_data *priv, 296724af5cb9SKevin Cernekee struct input_dev *dev1) 296824af5cb9SKevin Cernekee { 296995f75e91SYunkang Tang input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0); 297095f75e91SYunkang Tang input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0); 29718eccd393SMasaki Ota input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0); 297224af5cb9SKevin Cernekee } 297324af5cb9SKevin Cernekee 29748eccd393SMasaki Ota static void alps_set_abs_params_mt_common(struct alps_data *priv, 297524af5cb9SKevin Cernekee struct input_dev *dev1) 297624af5cb9SKevin Cernekee { 29777a9f73e7SKevin Cernekee input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0); 29787a9f73e7SKevin Cernekee input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0); 297924af5cb9SKevin Cernekee 2980f3f33c67SHans de Goede input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res); 2981f3f33c67SHans de Goede input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res); 2982f3f33c67SHans de Goede 298324af5cb9SKevin Cernekee set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit); 298424af5cb9SKevin Cernekee set_bit(BTN_TOOL_QUADTAP, dev1->keybit); 29858eccd393SMasaki Ota } 29863808843cSYunkang Tang 2987688ea364SHans de Goede static void alps_set_abs_params_semi_mt(struct alps_data *priv, 29888eccd393SMasaki Ota struct input_dev *dev1) 29898eccd393SMasaki Ota { 29908eccd393SMasaki Ota alps_set_abs_params_mt_common(priv, dev1); 29918eccd393SMasaki Ota input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0); 29928eccd393SMasaki Ota 29938eccd393SMasaki Ota input_mt_init_slots(dev1, MAX_TOUCHES, 29948eccd393SMasaki Ota INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | 29951662c033SHans de Goede INPUT_MT_SEMI_MT); 29968eccd393SMasaki Ota } 29978eccd393SMasaki Ota 29988eccd393SMasaki Ota static void alps_set_abs_params_v7(struct alps_data *priv, 29998eccd393SMasaki Ota struct input_dev *dev1) 30008eccd393SMasaki Ota { 30018eccd393SMasaki Ota alps_set_abs_params_mt_common(priv, dev1); 30028d289842SMasaki Ota set_bit(BTN_TOOL_QUINTTAP, dev1->keybit); 30038eccd393SMasaki Ota 30048eccd393SMasaki Ota input_mt_init_slots(dev1, MAX_TOUCHES, 30058eccd393SMasaki Ota INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | 30068eccd393SMasaki Ota INPUT_MT_TRACK); 30073db5b9f7SMasaki Ota 30083db5b9f7SMasaki Ota set_bit(BTN_TOOL_QUINTTAP, dev1->keybit); 30093db5b9f7SMasaki Ota } 30103db5b9f7SMasaki Ota 30113db5b9f7SMasaki Ota static void alps_set_abs_params_ss4_v2(struct alps_data *priv, 30123db5b9f7SMasaki Ota struct input_dev *dev1) 30133db5b9f7SMasaki Ota { 30143db5b9f7SMasaki Ota alps_set_abs_params_mt_common(priv, dev1); 30153db5b9f7SMasaki Ota input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0); 30163db5b9f7SMasaki Ota set_bit(BTN_TOOL_QUINTTAP, dev1->keybit); 30173db5b9f7SMasaki Ota 30183db5b9f7SMasaki Ota input_mt_init_slots(dev1, MAX_TOUCHES, 30193db5b9f7SMasaki Ota INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | 30203db5b9f7SMasaki Ota INPUT_MT_TRACK); 302124af5cb9SKevin Cernekee } 302224af5cb9SKevin Cernekee 30231da177e4SLinus Torvalds int alps_init(struct psmouse *psmouse) 30241da177e4SLinus Torvalds { 3025a09221e8SDmitry Torokhov struct alps_data *priv = psmouse->private; 302604aae283SPali Rohár struct input_dev *dev1 = psmouse->dev; 3027a09221e8SDmitry Torokhov int error; 30281da177e4SLinus Torvalds 3029a09221e8SDmitry Torokhov error = priv->hw_init(psmouse); 3030a09221e8SDmitry Torokhov if (error) 30311da177e4SLinus Torvalds goto init_fail; 30321da177e4SLinus Torvalds 30337105d2eaSDmitry Torokhov /* 30347105d2eaSDmitry Torokhov * Undo part of setup done for us by psmouse core since touchpad 30357105d2eaSDmitry Torokhov * is not a relative device. 30367105d2eaSDmitry Torokhov */ 30377105d2eaSDmitry Torokhov __clear_bit(EV_REL, dev1->evbit); 30387105d2eaSDmitry Torokhov __clear_bit(REL_X, dev1->relbit); 30397105d2eaSDmitry Torokhov __clear_bit(REL_Y, dev1->relbit); 30407105d2eaSDmitry Torokhov 30417105d2eaSDmitry Torokhov /* 30427105d2eaSDmitry Torokhov * Now set up our capabilities. 30437105d2eaSDmitry Torokhov */ 30447b19ada2SJiri Slaby dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY); 30457b19ada2SJiri Slaby dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH); 30467b19ada2SJiri Slaby dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER); 304771bb21b6SMaxim Levitsky dev1->keybit[BIT_WORD(BTN_LEFT)] |= 304871bb21b6SMaxim Levitsky BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT); 30491da177e4SLinus Torvalds 30507b19ada2SJiri Slaby dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS); 305125bded7cSSeth Forshee 305224af5cb9SKevin Cernekee priv->set_abs_params(priv, dev1); 30531da177e4SLinus Torvalds 305499df65e7SKevin Cernekee if (priv->flags & ALPS_WHEEL) { 30557b19ada2SJiri Slaby dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL); 30567b19ada2SJiri Slaby dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL); 30571da177e4SLinus Torvalds } 30581da177e4SLinus Torvalds 305999df65e7SKevin Cernekee if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) { 30607b19ada2SJiri Slaby dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD); 30617b19ada2SJiri Slaby dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK); 30621da177e4SLinus Torvalds } 30631da177e4SLinus Torvalds 306499df65e7SKevin Cernekee if (priv->flags & ALPS_FOUR_BUTTONS) { 306571bb21b6SMaxim Levitsky dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0); 306671bb21b6SMaxim Levitsky dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1); 306771bb21b6SMaxim Levitsky dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2); 306871bb21b6SMaxim Levitsky dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3); 30693808843cSYunkang Tang } else if (priv->flags & ALPS_BUTTONPAD) { 30703808843cSYunkang Tang set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit); 30713808843cSYunkang Tang clear_bit(BTN_RIGHT, dev1->keybit); 307271bb21b6SMaxim Levitsky } else { 307371bb21b6SMaxim Levitsky dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE); 307471bb21b6SMaxim Levitsky } 307571bb21b6SMaxim Levitsky 3076dfba8600SPali Rohár if (priv->flags & ALPS_DUALPOINT) { 307704aae283SPali Rohár struct input_dev *dev2; 307804aae283SPali Rohár 307904aae283SPali Rohár dev2 = input_allocate_device(); 308004aae283SPali Rohár if (!dev2) { 308104aae283SPali Rohár psmouse_err(psmouse, 308204aae283SPali Rohár "failed to allocate trackstick device\n"); 308304aae283SPali Rohár error = -ENOMEM; 308404aae283SPali Rohár goto init_fail; 308504aae283SPali Rohár } 308604aae283SPali Rohár 308704aae283SPali Rohár snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1", 308804aae283SPali Rohár psmouse->ps2dev.serio->phys); 308904aae283SPali Rohár dev2->phys = priv->phys2; 309004aae283SPali Rohár 3091dfba8600SPali Rohár /* 3092dfba8600SPali Rohár * format of input device name is: "protocol vendor name" 3093dfba8600SPali Rohár * see function psmouse_switch_protocol() in psmouse-base.c 3094dfba8600SPali Rohár */ 3095dfba8600SPali Rohár dev2->name = "AlpsPS/2 ALPS DualPoint Stick"; 3096dfba8600SPali Rohár 30972e5b636bSDmitry Torokhov dev2->id.bustype = BUS_I8042; 30982e5b636bSDmitry Torokhov dev2->id.vendor = 0x0002; 309904aae283SPali Rohár dev2->id.product = PSMOUSE_ALPS; 310004aae283SPali Rohár dev2->id.version = priv->proto_version; 31011db3a345SDmitry Torokhov dev2->dev.parent = &psmouse->ps2dev.serio->dev; 31021da177e4SLinus Torvalds 310304aae283SPali Rohár input_set_capability(dev2, EV_REL, REL_X); 310404aae283SPali Rohár input_set_capability(dev2, EV_REL, REL_Y); 31057ad8a106SBen Gamari if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) { 31067ad8a106SBen Gamari input_set_capability(dev2, EV_ABS, ABS_PRESSURE); 31077ad8a106SBen Gamari input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0); 31087ad8a106SBen Gamari } 310904aae283SPali Rohár input_set_capability(dev2, EV_KEY, BTN_LEFT); 311004aae283SPali Rohár input_set_capability(dev2, EV_KEY, BTN_RIGHT); 311104aae283SPali Rohár input_set_capability(dev2, EV_KEY, BTN_MIDDLE); 31121da177e4SLinus Torvalds 311301d4cd5cSHans de Goede __set_bit(INPUT_PROP_POINTER, dev2->propbit); 31147611392fSHans de Goede __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit); 31157611392fSHans de Goede 311604aae283SPali Rohár error = input_register_device(dev2); 311704aae283SPali Rohár if (error) { 311804aae283SPali Rohár psmouse_err(psmouse, 311904aae283SPali Rohár "failed to register trackstick device: %d\n", 312004aae283SPali Rohár error); 312104aae283SPali Rohár input_free_device(dev2); 3122f42649e8SDmitry Torokhov goto init_fail; 312304aae283SPali Rohár } 312404aae283SPali Rohár 312504aae283SPali Rohár priv->dev2 = dev2; 312604aae283SPali Rohár } 312704aae283SPali Rohár 312804aae283SPali Rohár priv->psmouse = psmouse; 312904aae283SPali Rohár 313004aae283SPali Rohár INIT_DELAYED_WORK(&priv->dev3_register_work, 313104aae283SPali Rohár alps_register_bare_ps2_mouse); 31321da177e4SLinus Torvalds 31331da177e4SLinus Torvalds psmouse->protocol_handler = alps_process_byte; 3134f0d5c6f4SDmitry Torokhov psmouse->poll = alps_poll; 31351da177e4SLinus Torvalds psmouse->disconnect = alps_disconnect; 31361da177e4SLinus Torvalds psmouse->reconnect = alps_reconnect; 313799df65e7SKevin Cernekee psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6; 31381da177e4SLinus Torvalds 3139f0d5c6f4SDmitry Torokhov /* We are having trouble resyncing ALPS touchpads so disable it for now */ 3140f0d5c6f4SDmitry Torokhov psmouse->resync_time = 0; 3141f0d5c6f4SDmitry Torokhov 31429d720b34SPali Rohár /* Allow 2 invalid packets without resetting device */ 31439d720b34SPali Rohár psmouse->resetafter = psmouse->pktsize * 2; 31449d720b34SPali Rohár 31451da177e4SLinus Torvalds return 0; 31461da177e4SLinus Torvalds 31471da177e4SLinus Torvalds init_fail: 3148f42649e8SDmitry Torokhov psmouse_reset(psmouse); 3149a09221e8SDmitry Torokhov /* 3150a09221e8SDmitry Torokhov * Even though we did not allocate psmouse->private we do free 3151a09221e8SDmitry Torokhov * it here. 3152a09221e8SDmitry Torokhov */ 3153a09221e8SDmitry Torokhov kfree(psmouse->private); 31541e0c5b12SDmitry Torokhov psmouse->private = NULL; 3155a09221e8SDmitry Torokhov return error; 31561da177e4SLinus Torvalds } 31571da177e4SLinus Torvalds 3158b7802c5cSDmitry Torokhov int alps_detect(struct psmouse *psmouse, bool set_properties) 31591da177e4SLinus Torvalds { 3160a09221e8SDmitry Torokhov struct alps_data *priv; 3161a09221e8SDmitry Torokhov int error; 31621da177e4SLinus Torvalds 3163a09221e8SDmitry Torokhov error = alps_identify(psmouse, NULL); 3164a09221e8SDmitry Torokhov if (error) 3165a09221e8SDmitry Torokhov return error; 3166a09221e8SDmitry Torokhov 3167a09221e8SDmitry Torokhov /* 3168a09221e8SDmitry Torokhov * Reset the device to make sure it is fully operational: 3169a09221e8SDmitry Torokhov * on some laptops, like certain Dell Latitudes, we may 3170a09221e8SDmitry Torokhov * fail to properly detect presence of trackstick if device 3171a09221e8SDmitry Torokhov * has not been reset. 3172a09221e8SDmitry Torokhov */ 3173a09221e8SDmitry Torokhov psmouse_reset(psmouse); 3174a09221e8SDmitry Torokhov 3175a09221e8SDmitry Torokhov priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL); 3176a09221e8SDmitry Torokhov if (!priv) 3177a09221e8SDmitry Torokhov return -ENOMEM; 3178a09221e8SDmitry Torokhov 3179a09221e8SDmitry Torokhov error = alps_identify(psmouse, priv); 318093050db2SDmitry Torokhov if (error) { 318193050db2SDmitry Torokhov kfree(priv); 3182a09221e8SDmitry Torokhov return error; 318393050db2SDmitry Torokhov } 31841da177e4SLinus Torvalds 31851da177e4SLinus Torvalds if (set_properties) { 31861da177e4SLinus Torvalds psmouse->vendor = "ALPS"; 3187a09221e8SDmitry Torokhov psmouse->name = priv->flags & ALPS_DUALPOINT ? 3188968ac842SDmitry Torokhov "DualPoint TouchPad" : "GlidePoint"; 3189a09221e8SDmitry Torokhov psmouse->model = priv->proto_version; 3190a09221e8SDmitry Torokhov } else { 3191a09221e8SDmitry Torokhov /* 3192a09221e8SDmitry Torokhov * Destroy alps_data structure we allocated earlier since 3193a09221e8SDmitry Torokhov * this was just a "trial run". Otherwise we'll keep it 3194a09221e8SDmitry Torokhov * to be used by alps_init() which has to be called if 3195a09221e8SDmitry Torokhov * we succeed and set_properties is true. 3196a09221e8SDmitry Torokhov */ 3197a09221e8SDmitry Torokhov kfree(priv); 3198a09221e8SDmitry Torokhov psmouse->private = NULL; 31991da177e4SLinus Torvalds } 3200a09221e8SDmitry Torokhov 32011da177e4SLinus Torvalds return 0; 32021da177e4SLinus Torvalds } 32031da177e4SLinus Torvalds 3204