hid-alps.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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_FEATURE_REPORT_ID 0x05 /* Feature ReportID */
  29. #define U1_SP_ABSOLUTE_REPORT_ID 0x06 /* Feature ReportID */
  30. #define U1_FEATURE_REPORT_LEN 0x08 /* Feature Report Length */
  31. #define U1_FEATURE_REPORT_LEN_ALL 0x0A
  32. #define U1_CMD_REGISTER_READ 0xD1
  33. #define U1_CMD_REGISTER_WRITE 0xD2
  34. #define U1_DEVTYPE_SP_SUPPORT 0x10 /* SP Support */
  35. #define U1_DISABLE_DEV 0x01
  36. #define U1_TP_ABS_MODE 0x02
  37. #define U1_SP_ABS_MODE 0x80
  38. #define ADDRESS_U1_DEV_CTRL_1 0x00800040
  39. #define ADDRESS_U1_DEVICE_TYP 0x00800043
  40. #define ADDRESS_U1_NUM_SENS_X 0x00800047
  41. #define ADDRESS_U1_NUM_SENS_Y 0x00800048
  42. #define ADDRESS_U1_PITCH_SENS_X 0x00800049
  43. #define ADDRESS_U1_PITCH_SENS_Y 0x0080004A
  44. #define ADDRESS_U1_RESO_DWN_ABS 0x0080004E
  45. #define ADDRESS_U1_PAD_BTN 0x00800052
  46. #define ADDRESS_U1_SP_BTN 0x0080009F
  47. #define MAX_TOUCHES 5
  48. /**
  49. * struct u1_data
  50. *
  51. * @input: pointer to the kernel input device
  52. * @input2: pointer to the kernel input2 device
  53. * @hdev: pointer to the struct hid_device
  54. *
  55. * @dev_ctrl: device control parameter
  56. * @dev_type: device type
  57. * @sen_line_num_x: number of sensor line of X
  58. * @sen_line_num_y: number of sensor line of Y
  59. * @pitch_x: sensor pitch of X
  60. * @pitch_y: sensor pitch of Y
  61. * @resolution: resolution
  62. * @btn_info: button information
  63. * @x_active_len_mm: active area length of X (mm)
  64. * @y_active_len_mm: active area length of Y (mm)
  65. * @x_max: maximum x coordinate value
  66. * @y_max: maximum y coordinate value
  67. * @btn_cnt: number of buttons
  68. * @sp_btn_cnt: number of stick buttons
  69. */
  70. struct u1_dev {
  71. struct input_dev *input;
  72. struct input_dev *input2;
  73. struct hid_device *hdev;
  74. u8 dev_ctrl;
  75. u8 dev_type;
  76. u8 sen_line_num_x;
  77. u8 sen_line_num_y;
  78. u8 pitch_x;
  79. u8 pitch_y;
  80. u8 resolution;
  81. u8 btn_info;
  82. u8 sp_btn_info;
  83. u32 x_active_len_mm;
  84. u32 y_active_len_mm;
  85. u32 x_max;
  86. u32 y_max;
  87. u32 btn_cnt;
  88. u32 sp_btn_cnt;
  89. };
  90. static int u1_read_write_register(struct hid_device *hdev, u32 address,
  91. u8 *read_val, u8 write_val, bool read_flag)
  92. {
  93. int ret, i;
  94. u8 check_sum;
  95. u8 *input;
  96. u8 *readbuf;
  97. input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
  98. if (!input)
  99. return -ENOMEM;
  100. input[0] = U1_FEATURE_REPORT_ID;
  101. if (read_flag) {
  102. input[1] = U1_CMD_REGISTER_READ;
  103. input[6] = 0x00;
  104. } else {
  105. input[1] = U1_CMD_REGISTER_WRITE;
  106. input[6] = write_val;
  107. }
  108. put_unaligned_le32(address, input + 2);
  109. /* Calculate the checksum */
  110. check_sum = U1_FEATURE_REPORT_LEN_ALL;
  111. for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
  112. check_sum += input[i];
  113. input[7] = check_sum;
  114. ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
  115. U1_FEATURE_REPORT_LEN,
  116. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  117. if (ret < 0) {
  118. dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
  119. goto exit;
  120. }
  121. if (read_flag) {
  122. readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
  123. if (!readbuf) {
  124. ret = -ENOMEM;
  125. goto exit;
  126. }
  127. ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
  128. U1_FEATURE_REPORT_LEN,
  129. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  130. if (ret < 0) {
  131. dev_err(&hdev->dev, "failed read register (%d)\n", ret);
  132. kfree(readbuf);
  133. goto exit;
  134. }
  135. *read_val = readbuf[6];
  136. kfree(readbuf);
  137. }
  138. ret = 0;
  139. exit:
  140. kfree(input);
  141. return ret;
  142. }
  143. static int alps_raw_event(struct hid_device *hdev,
  144. struct hid_report *report, u8 *data, int size)
  145. {
  146. unsigned int x, y, z;
  147. int i;
  148. short sp_x, sp_y;
  149. struct u1_dev *hdata = hid_get_drvdata(hdev);
  150. switch (data[0]) {
  151. case U1_MOUSE_REPORT_ID:
  152. break;
  153. case U1_FEATURE_REPORT_ID:
  154. break;
  155. case U1_ABSOLUTE_REPORT_ID:
  156. for (i = 0; i < MAX_TOUCHES; i++) {
  157. u8 *contact = &data[i * 5];
  158. x = get_unaligned_le16(contact + 3);
  159. y = get_unaligned_le16(contact + 5);
  160. z = contact[7] & 0x7F;
  161. input_mt_slot(hdata->input, i);
  162. if (z != 0) {
  163. input_mt_report_slot_state(hdata->input,
  164. MT_TOOL_FINGER, 1);
  165. input_report_abs(hdata->input,
  166. ABS_MT_POSITION_X, x);
  167. input_report_abs(hdata->input,
  168. ABS_MT_POSITION_Y, y);
  169. input_report_abs(hdata->input,
  170. ABS_MT_PRESSURE, z);
  171. } else {
  172. input_mt_report_slot_state(hdata->input,
  173. MT_TOOL_FINGER, 0);
  174. }
  175. }
  176. input_mt_sync_frame(hdata->input);
  177. input_report_key(hdata->input, BTN_LEFT,
  178. data[1] & 0x1);
  179. input_report_key(hdata->input, BTN_RIGHT,
  180. (data[1] & 0x2));
  181. input_report_key(hdata->input, BTN_MIDDLE,
  182. (data[1] & 0x4));
  183. input_sync(hdata->input);
  184. return 1;
  185. case U1_SP_ABSOLUTE_REPORT_ID:
  186. sp_x = get_unaligned_le16(data+2);
  187. sp_y = get_unaligned_le16(data+4);
  188. sp_x = sp_x / 8;
  189. sp_y = sp_y / 8;
  190. input_report_rel(hdata->input2, REL_X, sp_x);
  191. input_report_rel(hdata->input2, REL_Y, sp_y);
  192. input_report_key(hdata->input2, BTN_LEFT,
  193. data[1] & 0x1);
  194. input_report_key(hdata->input2, BTN_RIGHT,
  195. (data[1] & 0x2));
  196. input_report_key(hdata->input2, BTN_MIDDLE,
  197. (data[1] & 0x4));
  198. input_sync(hdata->input2);
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. #ifdef CONFIG_PM
  204. static int alps_post_reset(struct hid_device *hdev)
  205. {
  206. return u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
  207. NULL, U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
  208. }
  209. static int alps_post_resume(struct hid_device *hdev)
  210. {
  211. return u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
  212. NULL, U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
  213. }
  214. #endif /* CONFIG_PM */
  215. static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
  216. {
  217. struct u1_dev *data = hid_get_drvdata(hdev);
  218. struct input_dev *input = hi->input, *input2;
  219. struct u1_dev devInfo;
  220. int ret;
  221. int res_x, res_y, i;
  222. data->input = input;
  223. hid_dbg(hdev, "Opening low level driver\n");
  224. ret = hid_hw_open(hdev);
  225. if (ret)
  226. return ret;
  227. /* Allow incoming hid reports */
  228. hid_device_io_start(hdev);
  229. /* Device initialization */
  230. ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
  231. &devInfo.dev_ctrl, 0, true);
  232. if (ret < 0) {
  233. dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
  234. goto exit;
  235. }
  236. devInfo.dev_ctrl &= ~U1_DISABLE_DEV;
  237. devInfo.dev_ctrl |= U1_TP_ABS_MODE;
  238. ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
  239. NULL, devInfo.dev_ctrl, false);
  240. if (ret < 0) {
  241. dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
  242. goto exit;
  243. }
  244. ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
  245. &devInfo.sen_line_num_x, 0, true);
  246. if (ret < 0) {
  247. dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
  248. goto exit;
  249. }
  250. ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
  251. &devInfo.sen_line_num_y, 0, true);
  252. if (ret < 0) {
  253. dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
  254. goto exit;
  255. }
  256. ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
  257. &devInfo.pitch_x, 0, true);
  258. if (ret < 0) {
  259. dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
  260. goto exit;
  261. }
  262. ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
  263. &devInfo.pitch_y, 0, true);
  264. if (ret < 0) {
  265. dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
  266. goto exit;
  267. }
  268. ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
  269. &devInfo.resolution, 0, true);
  270. if (ret < 0) {
  271. dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
  272. goto exit;
  273. }
  274. ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
  275. &devInfo.btn_info, 0, true);
  276. if (ret < 0) {
  277. dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
  278. goto exit;
  279. }
  280. /* Check StickPointer device */
  281. ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
  282. &devInfo.dev_type, 0, true);
  283. if (ret < 0) {
  284. dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
  285. goto exit;
  286. }
  287. devInfo.x_active_len_mm =
  288. (devInfo.pitch_x * (devInfo.sen_line_num_x - 1)) / 10;
  289. devInfo.y_active_len_mm =
  290. (devInfo.pitch_y * (devInfo.sen_line_num_y - 1)) / 10;
  291. devInfo.x_max =
  292. (devInfo.resolution << 2) * (devInfo.sen_line_num_x - 1);
  293. devInfo.y_max =
  294. (devInfo.resolution << 2) * (devInfo.sen_line_num_y - 1);
  295. __set_bit(EV_ABS, input->evbit);
  296. input_set_abs_params(input, ABS_MT_POSITION_X, 1, devInfo.x_max, 0, 0);
  297. input_set_abs_params(input, ABS_MT_POSITION_Y, 1, devInfo.y_max, 0, 0);
  298. if (devInfo.x_active_len_mm && devInfo.y_active_len_mm) {
  299. res_x = (devInfo.x_max - 1) / devInfo.x_active_len_mm;
  300. res_y = (devInfo.y_max - 1) / devInfo.y_active_len_mm;
  301. input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
  302. input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
  303. }
  304. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
  305. input_mt_init_slots(input, MAX_TOUCHES, INPUT_MT_POINTER);
  306. __set_bit(EV_KEY, input->evbit);
  307. if ((devInfo.btn_info & 0x0F) == (devInfo.btn_info & 0xF0) >> 4) {
  308. devInfo.btn_cnt = (devInfo.btn_info & 0x0F);
  309. } else {
  310. /* Button pad */
  311. devInfo.btn_cnt = 1;
  312. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  313. }
  314. for (i = 0; i < devInfo.btn_cnt; i++)
  315. __set_bit(BTN_LEFT + i, input->keybit);
  316. /* Stick device initialization */
  317. if (devInfo.dev_type & U1_DEVTYPE_SP_SUPPORT) {
  318. input2 = input_allocate_device();
  319. if (!input2) {
  320. ret = -ENOMEM;
  321. goto exit;
  322. }
  323. data->input2 = input2;
  324. devInfo.dev_ctrl |= U1_SP_ABS_MODE;
  325. ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
  326. NULL, devInfo.dev_ctrl, false);
  327. if (ret < 0) {
  328. dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
  329. input_free_device(input2);
  330. goto exit;
  331. }
  332. ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
  333. &devInfo.sp_btn_info, 0, true);
  334. if (ret < 0) {
  335. dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
  336. input_free_device(input2);
  337. goto exit;
  338. }
  339. input2->phys = input->phys;
  340. input2->name = "DualPoint Stick";
  341. input2->id.bustype = BUS_I2C;
  342. input2->id.vendor = input->id.vendor;
  343. input2->id.product = input->id.product;
  344. input2->id.version = input->id.version;
  345. input2->dev.parent = input->dev.parent;
  346. __set_bit(EV_KEY, input2->evbit);
  347. devInfo.sp_btn_cnt = (devInfo.sp_btn_info & 0x0F);
  348. for (i = 0; i < devInfo.sp_btn_cnt; i++)
  349. __set_bit(BTN_LEFT + i, input2->keybit);
  350. __set_bit(EV_REL, input2->evbit);
  351. __set_bit(REL_X, input2->relbit);
  352. __set_bit(REL_Y, input2->relbit);
  353. __set_bit(INPUT_PROP_POINTER, input2->propbit);
  354. __set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
  355. ret = input_register_device(data->input2);
  356. if (ret) {
  357. input_free_device(input2);
  358. goto exit;
  359. }
  360. }
  361. exit:
  362. hid_device_io_stop(hdev);
  363. hid_hw_close(hdev);
  364. return ret;
  365. }
  366. static int alps_input_mapping(struct hid_device *hdev,
  367. struct hid_input *hi, struct hid_field *field,
  368. struct hid_usage *usage, unsigned long **bit, int *max)
  369. {
  370. return -1;
  371. }
  372. static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
  373. {
  374. struct u1_dev *data = NULL;
  375. int ret;
  376. data = devm_kzalloc(&hdev->dev, sizeof(struct u1_dev), GFP_KERNEL);
  377. if (!data)
  378. return -ENOMEM;
  379. data->hdev = hdev;
  380. hid_set_drvdata(hdev, data);
  381. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  382. ret = hid_parse(hdev);
  383. if (ret) {
  384. hid_err(hdev, "parse failed\n");
  385. return ret;
  386. }
  387. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  388. if (ret) {
  389. hid_err(hdev, "hw start failed\n");
  390. return ret;
  391. }
  392. return 0;
  393. }
  394. static void alps_remove(struct hid_device *hdev)
  395. {
  396. hid_hw_stop(hdev);
  397. }
  398. static const struct hid_device_id alps_id[] = {
  399. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
  400. USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
  401. { }
  402. };
  403. MODULE_DEVICE_TABLE(hid, alps_id);
  404. static struct hid_driver alps_driver = {
  405. .name = "hid-alps",
  406. .id_table = alps_id,
  407. .probe = alps_probe,
  408. .remove = alps_remove,
  409. .raw_event = alps_raw_event,
  410. .input_mapping = alps_input_mapping,
  411. .input_configured = alps_input_configured,
  412. #ifdef CONFIG_PM
  413. .resume = alps_post_resume,
  414. .reset_resume = alps_post_reset,
  415. #endif
  416. };
  417. module_hid_driver(alps_driver);
  418. MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
  419. MODULE_DESCRIPTION("ALPS HID driver");
  420. MODULE_LICENSE("GPL");