hid-alps.c 20 KB

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