hid-alps.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2016 Masaki Ota <[email protected]>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/hid.h>
  7. #include <linux/input.h>
  8. #include <linux/input/mt.h>
  9. #include <linux/module.h>
  10. #include <asm/unaligned.h>
  11. #include "hid-ids.h"
  12. /* ALPS Device Product ID */
  13. #define HID_PRODUCT_ID_T3_BTNLESS 0xD0C0
  14. #define HID_PRODUCT_ID_COSMO 0x1202
  15. #define HID_PRODUCT_ID_U1_PTP_1 0x1207
  16. #define HID_PRODUCT_ID_U1 0x1209
  17. #define HID_PRODUCT_ID_U1_PTP_2 0x120A
  18. #define HID_PRODUCT_ID_U1_DUAL 0x120B
  19. #define HID_PRODUCT_ID_T4_BTNLESS 0x120C
  20. #define DEV_SINGLEPOINT 0x01
  21. #define DEV_DUALPOINT 0x02
  22. #define U1_MOUSE_REPORT_ID 0x01 /* Mouse data ReportID */
  23. #define U1_ABSOLUTE_REPORT_ID 0x03 /* Absolute data ReportID */
  24. #define U1_ABSOLUTE_REPORT_ID_SECD 0x02 /* FW-PTP Absolute data ReportID */
  25. #define U1_FEATURE_REPORT_ID 0x05 /* Feature ReportID */
  26. #define U1_SP_ABSOLUTE_REPORT_ID 0x06 /* Feature ReportID */
  27. #define U1_FEATURE_REPORT_LEN 0x08 /* Feature Report Length */
  28. #define U1_FEATURE_REPORT_LEN_ALL 0x0A
  29. #define U1_CMD_REGISTER_READ 0xD1
  30. #define U1_CMD_REGISTER_WRITE 0xD2
  31. #define U1_DEVTYPE_SP_SUPPORT 0x10 /* SP Support */
  32. #define U1_DISABLE_DEV 0x01
  33. #define U1_TP_ABS_MODE 0x02
  34. #define U1_SP_ABS_MODE 0x80
  35. #define ADDRESS_U1_DEV_CTRL_1 0x00800040
  36. #define ADDRESS_U1_DEVICE_TYP 0x00800043
  37. #define ADDRESS_U1_NUM_SENS_X 0x00800047
  38. #define ADDRESS_U1_NUM_SENS_Y 0x00800048
  39. #define ADDRESS_U1_PITCH_SENS_X 0x00800049
  40. #define ADDRESS_U1_PITCH_SENS_Y 0x0080004A
  41. #define ADDRESS_U1_RESO_DWN_ABS 0x0080004E
  42. #define ADDRESS_U1_PAD_BTN 0x00800052
  43. #define ADDRESS_U1_SP_BTN 0x0080009F
  44. #define T4_INPUT_REPORT_LEN sizeof(struct t4_input_report)
  45. #define T4_FEATURE_REPORT_LEN T4_INPUT_REPORT_LEN
  46. #define T4_FEATURE_REPORT_ID 7
  47. #define T4_CMD_REGISTER_READ 0x08
  48. #define T4_CMD_REGISTER_WRITE 0x07
  49. #define T4_ADDRESS_BASE 0xC2C0
  50. #define PRM_SYS_CONFIG_1 (T4_ADDRESS_BASE + 0x0002)
  51. #define T4_PRM_FEED_CONFIG_1 (T4_ADDRESS_BASE + 0x0004)
  52. #define T4_PRM_FEED_CONFIG_4 (T4_ADDRESS_BASE + 0x001A)
  53. #define T4_PRM_ID_CONFIG_3 (T4_ADDRESS_BASE + 0x00B0)
  54. #define T4_FEEDCFG4_ADVANCED_ABS_ENABLE 0x01
  55. #define T4_I2C_ABS 0x78
  56. #define T4_COUNT_PER_ELECTRODE 256
  57. #define MAX_TOUCHES 5
  58. enum dev_num {
  59. U1,
  60. T4,
  61. UNKNOWN,
  62. };
  63. /**
  64. * struct alps_dev
  65. *
  66. * @input: pointer to the kernel input device
  67. * @input2: pointer to the kernel input2 device
  68. * @hdev: pointer to the struct hid_device
  69. *
  70. * @dev_type: device type
  71. * @max_fingers: total number of fingers
  72. * @has_sp: boolean of sp existense
  73. * @sp_btn_info: button information
  74. * @x_active_len_mm: active area length of X (mm)
  75. * @y_active_len_mm: active area length of Y (mm)
  76. * @x_max: maximum x coordinate value
  77. * @y_max: maximum y coordinate value
  78. * @x_min: minimum x coordinate value
  79. * @y_min: minimum y coordinate value
  80. * @btn_cnt: number of buttons
  81. * @sp_btn_cnt: number of stick buttons
  82. */
  83. struct alps_dev {
  84. struct input_dev *input;
  85. struct input_dev *input2;
  86. struct hid_device *hdev;
  87. enum dev_num dev_type;
  88. u8 max_fingers;
  89. u8 has_sp;
  90. u8 sp_btn_info;
  91. u32 x_active_len_mm;
  92. u32 y_active_len_mm;
  93. u32 x_max;
  94. u32 y_max;
  95. u32 x_min;
  96. u32 y_min;
  97. u32 btn_cnt;
  98. u32 sp_btn_cnt;
  99. };
  100. struct t4_contact_data {
  101. u8 palm;
  102. u8 x_lo;
  103. u8 x_hi;
  104. u8 y_lo;
  105. u8 y_hi;
  106. };
  107. struct t4_input_report {
  108. u8 reportID;
  109. u8 numContacts;
  110. struct t4_contact_data contact[5];
  111. u8 button;
  112. u8 track[5];
  113. u8 zx[5], zy[5];
  114. u8 palmTime[5];
  115. u8 kilroy;
  116. u16 timeStamp;
  117. };
  118. static u16 t4_calc_check_sum(u8 *buffer,
  119. unsigned long offset, unsigned long length)
  120. {
  121. u16 sum1 = 0xFF, sum2 = 0xFF;
  122. unsigned long i = 0;
  123. if (offset + length >= 50)
  124. return 0;
  125. while (length > 0) {
  126. u32 tlen = length > 20 ? 20 : length;
  127. length -= tlen;
  128. do {
  129. sum1 += buffer[offset + i];
  130. sum2 += sum1;
  131. i++;
  132. } while (--tlen > 0);
  133. sum1 = (sum1 & 0xFF) + (sum1 >> 8);
  134. sum2 = (sum2 & 0xFF) + (sum2 >> 8);
  135. }
  136. sum1 = (sum1 & 0xFF) + (sum1 >> 8);
  137. sum2 = (sum2 & 0xFF) + (sum2 >> 8);
  138. return(sum2 << 8 | sum1);
  139. }
  140. static int t4_read_write_register(struct hid_device *hdev, u32 address,
  141. u8 *read_val, u8 write_val, bool read_flag)
  142. {
  143. int ret;
  144. u16 check_sum;
  145. u8 *input;
  146. u8 *readbuf = NULL;
  147. input = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
  148. if (!input)
  149. return -ENOMEM;
  150. input[0] = T4_FEATURE_REPORT_ID;
  151. if (read_flag) {
  152. input[1] = T4_CMD_REGISTER_READ;
  153. input[8] = 0x00;
  154. } else {
  155. input[1] = T4_CMD_REGISTER_WRITE;
  156. input[8] = write_val;
  157. }
  158. put_unaligned_le32(address, input + 2);
  159. input[6] = 1;
  160. input[7] = 0;
  161. /* Calculate the checksum */
  162. check_sum = t4_calc_check_sum(input, 1, 8);
  163. input[9] = (u8)check_sum;
  164. input[10] = (u8)(check_sum >> 8);
  165. input[11] = 0;
  166. ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, input,
  167. T4_FEATURE_REPORT_LEN,
  168. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  169. if (ret < 0) {
  170. dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
  171. goto exit;
  172. }
  173. if (read_flag) {
  174. readbuf = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
  175. if (!readbuf) {
  176. ret = -ENOMEM;
  177. goto exit;
  178. }
  179. ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, readbuf,
  180. T4_FEATURE_REPORT_LEN,
  181. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  182. if (ret < 0) {
  183. dev_err(&hdev->dev, "failed read register (%d)\n", ret);
  184. goto exit_readbuf;
  185. }
  186. ret = -EINVAL;
  187. if (*(u32 *)&readbuf[6] != address) {
  188. dev_err(&hdev->dev, "read register address error (%x,%x)\n",
  189. *(u32 *)&readbuf[6], address);
  190. goto exit_readbuf;
  191. }
  192. if (*(u16 *)&readbuf[10] != 1) {
  193. dev_err(&hdev->dev, "read register size error (%x)\n",
  194. *(u16 *)&readbuf[10]);
  195. goto exit_readbuf;
  196. }
  197. check_sum = t4_calc_check_sum(readbuf, 6, 7);
  198. if (*(u16 *)&readbuf[13] != check_sum) {
  199. dev_err(&hdev->dev, "read register checksum error (%x,%x)\n",
  200. *(u16 *)&readbuf[13], check_sum);
  201. goto exit_readbuf;
  202. }
  203. *read_val = readbuf[12];
  204. }
  205. ret = 0;
  206. exit_readbuf:
  207. kfree(readbuf);
  208. exit:
  209. kfree(input);
  210. return ret;
  211. }
  212. static int u1_read_write_register(struct hid_device *hdev, u32 address,
  213. u8 *read_val, u8 write_val, bool read_flag)
  214. {
  215. int ret, i;
  216. u8 check_sum;
  217. u8 *input;
  218. u8 *readbuf;
  219. input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
  220. if (!input)
  221. return -ENOMEM;
  222. input[0] = U1_FEATURE_REPORT_ID;
  223. if (read_flag) {
  224. input[1] = U1_CMD_REGISTER_READ;
  225. input[6] = 0x00;
  226. } else {
  227. input[1] = U1_CMD_REGISTER_WRITE;
  228. input[6] = write_val;
  229. }
  230. put_unaligned_le32(address, input + 2);
  231. /* Calculate the checksum */
  232. check_sum = U1_FEATURE_REPORT_LEN_ALL;
  233. for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
  234. check_sum += input[i];
  235. input[7] = check_sum;
  236. ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
  237. U1_FEATURE_REPORT_LEN,
  238. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  239. if (ret < 0) {
  240. dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
  241. goto exit;
  242. }
  243. if (read_flag) {
  244. readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
  245. if (!readbuf) {
  246. ret = -ENOMEM;
  247. goto exit;
  248. }
  249. ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
  250. U1_FEATURE_REPORT_LEN,
  251. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  252. if (ret < 0) {
  253. dev_err(&hdev->dev, "failed read register (%d)\n", ret);
  254. kfree(readbuf);
  255. goto exit;
  256. }
  257. *read_val = readbuf[6];
  258. kfree(readbuf);
  259. }
  260. ret = 0;
  261. exit:
  262. kfree(input);
  263. return ret;
  264. }
  265. static int t4_raw_event(struct alps_dev *hdata, u8 *data, int size)
  266. {
  267. unsigned int x, y, z;
  268. int i;
  269. struct t4_input_report *p_report = (struct t4_input_report *)data;
  270. if (!data)
  271. return 0;
  272. for (i = 0; i < hdata->max_fingers; i++) {
  273. x = p_report->contact[i].x_hi << 8 | p_report->contact[i].x_lo;
  274. y = p_report->contact[i].y_hi << 8 | p_report->contact[i].y_lo;
  275. y = hdata->y_max - y + hdata->y_min;
  276. z = (p_report->contact[i].palm < 0x80 &&
  277. p_report->contact[i].palm > 0) * 62;
  278. if (x == 0xffff) {
  279. x = 0;
  280. y = 0;
  281. z = 0;
  282. }
  283. input_mt_slot(hdata->input, i);
  284. input_mt_report_slot_state(hdata->input,
  285. MT_TOOL_FINGER, z != 0);
  286. if (!z)
  287. continue;
  288. input_report_abs(hdata->input, ABS_MT_POSITION_X, x);
  289. input_report_abs(hdata->input, ABS_MT_POSITION_Y, y);
  290. input_report_abs(hdata->input, ABS_MT_PRESSURE, z);
  291. }
  292. input_mt_sync_frame(hdata->input);
  293. input_report_key(hdata->input, BTN_LEFT, p_report->button);
  294. input_sync(hdata->input);
  295. return 1;
  296. }
  297. static int u1_raw_event(struct alps_dev *hdata, u8 *data, int size)
  298. {
  299. unsigned int x, y, z;
  300. int i;
  301. short sp_x, sp_y;
  302. if (!data)
  303. return 0;
  304. switch (data[0]) {
  305. case U1_MOUSE_REPORT_ID:
  306. break;
  307. case U1_FEATURE_REPORT_ID:
  308. break;
  309. case U1_ABSOLUTE_REPORT_ID:
  310. case U1_ABSOLUTE_REPORT_ID_SECD:
  311. for (i = 0; i < hdata->max_fingers; i++) {
  312. u8 *contact = &data[i * 5];
  313. x = get_unaligned_le16(contact + 3);
  314. y = get_unaligned_le16(contact + 5);
  315. z = contact[7] & 0x7F;
  316. input_mt_slot(hdata->input, i);
  317. if (z != 0) {
  318. input_mt_report_slot_state(hdata->input,
  319. MT_TOOL_FINGER, 1);
  320. input_report_abs(hdata->input,
  321. ABS_MT_POSITION_X, x);
  322. input_report_abs(hdata->input,
  323. ABS_MT_POSITION_Y, y);
  324. input_report_abs(hdata->input,
  325. ABS_MT_PRESSURE, z);
  326. } else {
  327. input_mt_report_slot_inactive(hdata->input);
  328. }
  329. }
  330. input_mt_sync_frame(hdata->input);
  331. input_report_key(hdata->input, BTN_LEFT,
  332. data[1] & 0x1);
  333. input_report_key(hdata->input, BTN_RIGHT,
  334. (data[1] & 0x2));
  335. input_report_key(hdata->input, BTN_MIDDLE,
  336. (data[1] & 0x4));
  337. input_sync(hdata->input);
  338. return 1;
  339. case U1_SP_ABSOLUTE_REPORT_ID:
  340. sp_x = get_unaligned_le16(data+2);
  341. sp_y = get_unaligned_le16(data+4);
  342. sp_x = sp_x / 8;
  343. sp_y = sp_y / 8;
  344. input_report_rel(hdata->input2, REL_X, sp_x);
  345. input_report_rel(hdata->input2, REL_Y, sp_y);
  346. input_report_key(hdata->input2, BTN_LEFT,
  347. data[1] & 0x1);
  348. input_report_key(hdata->input2, BTN_RIGHT,
  349. (data[1] & 0x2));
  350. input_report_key(hdata->input2, BTN_MIDDLE,
  351. (data[1] & 0x4));
  352. input_sync(hdata->input2);
  353. return 1;
  354. }
  355. return 0;
  356. }
  357. static int alps_raw_event(struct hid_device *hdev,
  358. struct hid_report *report, u8 *data, int size)
  359. {
  360. int ret = 0;
  361. struct alps_dev *hdata = hid_get_drvdata(hdev);
  362. switch (hdev->product) {
  363. case HID_PRODUCT_ID_T4_BTNLESS:
  364. ret = t4_raw_event(hdata, data, size);
  365. break;
  366. default:
  367. ret = u1_raw_event(hdata, data, size);
  368. break;
  369. }
  370. return ret;
  371. }
  372. static int __maybe_unused alps_post_reset(struct hid_device *hdev)
  373. {
  374. int ret = -1;
  375. struct alps_dev *data = hid_get_drvdata(hdev);
  376. switch (data->dev_type) {
  377. case T4:
  378. ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
  379. NULL, T4_I2C_ABS, false);
  380. if (ret < 0) {
  381. dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n",
  382. ret);
  383. goto exit;
  384. }
  385. ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4,
  386. NULL, T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
  387. if (ret < 0) {
  388. dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n",
  389. ret);
  390. goto exit;
  391. }
  392. break;
  393. case U1:
  394. ret = u1_read_write_register(hdev,
  395. ADDRESS_U1_DEV_CTRL_1, NULL,
  396. U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
  397. if (ret < 0) {
  398. dev_err(&hdev->dev, "failed to change TP mode (%d)\n",
  399. ret);
  400. goto exit;
  401. }
  402. break;
  403. default:
  404. break;
  405. }
  406. exit:
  407. return ret;
  408. }
  409. static int __maybe_unused alps_post_resume(struct hid_device *hdev)
  410. {
  411. return alps_post_reset(hdev);
  412. }
  413. static int u1_init(struct hid_device *hdev, struct alps_dev *pri_data)
  414. {
  415. int ret;
  416. u8 tmp, dev_ctrl, sen_line_num_x, sen_line_num_y;
  417. u8 pitch_x, pitch_y, resolution;
  418. /* Device initialization */
  419. ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
  420. &dev_ctrl, 0, true);
  421. if (ret < 0) {
  422. dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
  423. goto exit;
  424. }
  425. dev_ctrl &= ~U1_DISABLE_DEV;
  426. dev_ctrl |= U1_TP_ABS_MODE;
  427. ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
  428. NULL, dev_ctrl, false);
  429. if (ret < 0) {
  430. dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
  431. goto exit;
  432. }
  433. ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
  434. &sen_line_num_x, 0, true);
  435. if (ret < 0) {
  436. dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
  437. goto exit;
  438. }
  439. ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
  440. &sen_line_num_y, 0, true);
  441. if (ret < 0) {
  442. dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
  443. goto exit;
  444. }
  445. ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
  446. &pitch_x, 0, true);
  447. if (ret < 0) {
  448. dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
  449. goto exit;
  450. }
  451. ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
  452. &pitch_y, 0, true);
  453. if (ret < 0) {
  454. dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
  455. goto exit;
  456. }
  457. ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
  458. &resolution, 0, true);
  459. if (ret < 0) {
  460. dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
  461. goto exit;
  462. }
  463. pri_data->x_active_len_mm =
  464. (pitch_x * (sen_line_num_x - 1)) / 10;
  465. pri_data->y_active_len_mm =
  466. (pitch_y * (sen_line_num_y - 1)) / 10;
  467. pri_data->x_max =
  468. (resolution << 2) * (sen_line_num_x - 1);
  469. pri_data->x_min = 1;
  470. pri_data->y_max =
  471. (resolution << 2) * (sen_line_num_y - 1);
  472. pri_data->y_min = 1;
  473. ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
  474. &tmp, 0, true);
  475. if (ret < 0) {
  476. dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
  477. goto exit;
  478. }
  479. if ((tmp & 0x0F) == (tmp & 0xF0) >> 4) {
  480. pri_data->btn_cnt = (tmp & 0x0F);
  481. } else {
  482. /* Button pad */
  483. pri_data->btn_cnt = 1;
  484. }
  485. pri_data->has_sp = 0;
  486. /* Check StickPointer device */
  487. ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
  488. &tmp, 0, true);
  489. if (ret < 0) {
  490. dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
  491. goto exit;
  492. }
  493. if (tmp & U1_DEVTYPE_SP_SUPPORT) {
  494. dev_ctrl |= U1_SP_ABS_MODE;
  495. ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
  496. NULL, dev_ctrl, false);
  497. if (ret < 0) {
  498. dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
  499. goto exit;
  500. }
  501. ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
  502. &pri_data->sp_btn_info, 0, true);
  503. if (ret < 0) {
  504. dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
  505. goto exit;
  506. }
  507. pri_data->has_sp = 1;
  508. }
  509. pri_data->max_fingers = 5;
  510. exit:
  511. return ret;
  512. }
  513. static int T4_init(struct hid_device *hdev, struct alps_dev *pri_data)
  514. {
  515. int ret;
  516. u8 tmp, sen_line_num_x, sen_line_num_y;
  517. ret = t4_read_write_register(hdev, T4_PRM_ID_CONFIG_3, &tmp, 0, true);
  518. if (ret < 0) {
  519. dev_err(&hdev->dev, "failed T4_PRM_ID_CONFIG_3 (%d)\n", ret);
  520. goto exit;
  521. }
  522. sen_line_num_x = 16 + ((tmp & 0x0F) | (tmp & 0x08 ? 0xF0 : 0));
  523. sen_line_num_y = 12 + (((tmp & 0xF0) >> 4) | (tmp & 0x80 ? 0xF0 : 0));
  524. pri_data->x_max = sen_line_num_x * T4_COUNT_PER_ELECTRODE;
  525. pri_data->x_min = T4_COUNT_PER_ELECTRODE;
  526. pri_data->y_max = sen_line_num_y * T4_COUNT_PER_ELECTRODE;
  527. pri_data->y_min = T4_COUNT_PER_ELECTRODE;
  528. pri_data->x_active_len_mm = pri_data->y_active_len_mm = 0;
  529. pri_data->btn_cnt = 1;
  530. ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, &tmp, 0, true);
  531. if (ret < 0) {
  532. dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
  533. goto exit;
  534. }
  535. tmp |= 0x02;
  536. ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, NULL, tmp, false);
  537. if (ret < 0) {
  538. dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
  539. goto exit;
  540. }
  541. ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
  542. NULL, T4_I2C_ABS, false);
  543. if (ret < 0) {
  544. dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n", ret);
  545. goto exit;
  546. }
  547. ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4, NULL,
  548. T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
  549. if (ret < 0) {
  550. dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n", ret);
  551. goto exit;
  552. }
  553. pri_data->max_fingers = 5;
  554. pri_data->has_sp = 0;
  555. exit:
  556. return ret;
  557. }
  558. static int alps_sp_open(struct input_dev *dev)
  559. {
  560. struct hid_device *hid = input_get_drvdata(dev);
  561. return hid_hw_open(hid);
  562. }
  563. static void alps_sp_close(struct input_dev *dev)
  564. {
  565. struct hid_device *hid = input_get_drvdata(dev);
  566. hid_hw_close(hid);
  567. }
  568. static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
  569. {
  570. struct alps_dev *data = hid_get_drvdata(hdev);
  571. struct input_dev *input = hi->input, *input2;
  572. int ret;
  573. int res_x, res_y, i;
  574. data->input = input;
  575. hid_dbg(hdev, "Opening low level driver\n");
  576. ret = hid_hw_open(hdev);
  577. if (ret)
  578. return ret;
  579. /* Allow incoming hid reports */
  580. hid_device_io_start(hdev);
  581. switch (data->dev_type) {
  582. case T4:
  583. ret = T4_init(hdev, data);
  584. break;
  585. case U1:
  586. ret = u1_init(hdev, data);
  587. break;
  588. default:
  589. break;
  590. }
  591. if (ret)
  592. goto exit;
  593. __set_bit(EV_ABS, input->evbit);
  594. input_set_abs_params(input, ABS_MT_POSITION_X,
  595. data->x_min, data->x_max, 0, 0);
  596. input_set_abs_params(input, ABS_MT_POSITION_Y,
  597. data->y_min, data->y_max, 0, 0);
  598. if (data->x_active_len_mm && data->y_active_len_mm) {
  599. res_x = (data->x_max - 1) / data->x_active_len_mm;
  600. res_y = (data->y_max - 1) / data->y_active_len_mm;
  601. input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
  602. input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
  603. }
  604. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
  605. input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
  606. __set_bit(EV_KEY, input->evbit);
  607. if (data->btn_cnt == 1)
  608. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  609. for (i = 0; i < data->btn_cnt; i++)
  610. __set_bit(BTN_LEFT + i, input->keybit);
  611. /* Stick device initialization */
  612. if (data->has_sp) {
  613. input2 = input_allocate_device();
  614. if (!input2) {
  615. ret = -ENOMEM;
  616. goto exit;
  617. }
  618. data->input2 = input2;
  619. input2->phys = input->phys;
  620. input2->name = "DualPoint Stick";
  621. input2->id.bustype = BUS_I2C;
  622. input2->id.vendor = input->id.vendor;
  623. input2->id.product = input->id.product;
  624. input2->id.version = input->id.version;
  625. input2->dev.parent = input->dev.parent;
  626. input_set_drvdata(input2, hdev);
  627. input2->open = alps_sp_open;
  628. input2->close = alps_sp_close;
  629. __set_bit(EV_KEY, input2->evbit);
  630. data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
  631. for (i = 0; i < data->sp_btn_cnt; i++)
  632. __set_bit(BTN_LEFT + i, input2->keybit);
  633. __set_bit(EV_REL, input2->evbit);
  634. __set_bit(REL_X, input2->relbit);
  635. __set_bit(REL_Y, input2->relbit);
  636. __set_bit(INPUT_PROP_POINTER, input2->propbit);
  637. __set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
  638. if (input_register_device(data->input2)) {
  639. input_free_device(input2);
  640. ret = -ENOENT;
  641. goto exit;
  642. }
  643. }
  644. exit:
  645. hid_device_io_stop(hdev);
  646. hid_hw_close(hdev);
  647. return ret;
  648. }
  649. static int alps_input_mapping(struct hid_device *hdev,
  650. struct hid_input *hi, struct hid_field *field,
  651. struct hid_usage *usage, unsigned long **bit, int *max)
  652. {
  653. return -1;
  654. }
  655. static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
  656. {
  657. struct alps_dev *data = NULL;
  658. int ret;
  659. data = devm_kzalloc(&hdev->dev, sizeof(struct alps_dev), GFP_KERNEL);
  660. if (!data)
  661. return -ENOMEM;
  662. data->hdev = hdev;
  663. hid_set_drvdata(hdev, data);
  664. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  665. ret = hid_parse(hdev);
  666. if (ret) {
  667. hid_err(hdev, "parse failed\n");
  668. return ret;
  669. }
  670. switch (hdev->product) {
  671. case HID_DEVICE_ID_ALPS_T4_BTNLESS:
  672. data->dev_type = T4;
  673. break;
  674. case HID_DEVICE_ID_ALPS_U1_DUAL:
  675. case HID_DEVICE_ID_ALPS_U1:
  676. case HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY:
  677. data->dev_type = U1;
  678. break;
  679. default:
  680. data->dev_type = UNKNOWN;
  681. }
  682. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  683. if (ret) {
  684. hid_err(hdev, "hw start failed\n");
  685. return ret;
  686. }
  687. return 0;
  688. }
  689. static void alps_remove(struct hid_device *hdev)
  690. {
  691. hid_hw_stop(hdev);
  692. }
  693. static const struct hid_device_id alps_id[] = {
  694. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
  695. USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
  696. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
  697. USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1) },
  698. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
  699. USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY) },
  700. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
  701. USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_T4_BTNLESS) },
  702. { }
  703. };
  704. MODULE_DEVICE_TABLE(hid, alps_id);
  705. static struct hid_driver alps_driver = {
  706. .name = "hid-alps",
  707. .id_table = alps_id,
  708. .probe = alps_probe,
  709. .remove = alps_remove,
  710. .raw_event = alps_raw_event,
  711. .input_mapping = alps_input_mapping,
  712. .input_configured = alps_input_configured,
  713. #ifdef CONFIG_PM
  714. .resume = alps_post_resume,
  715. .reset_resume = alps_post_reset,
  716. #endif
  717. };
  718. module_hid_driver(alps_driver);
  719. MODULE_AUTHOR("Masaki Ota <[email protected]>");
  720. MODULE_DESCRIPTION("ALPS HID driver");
  721. MODULE_LICENSE("GPL");