hid-logitech-hidpp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * HIDPP protocol for Logitech Unifying receivers
  3. *
  4. * Copyright (c) 2011 Logitech (c)
  5. * Copyright (c) 2012-2013 Google (c)
  6. * Copyright (c) 2013-2014 Red Hat Inc.
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; version 2 of the License.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/device.h>
  15. #include <linux/hid.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/sched.h>
  19. #include <linux/kfifo.h>
  20. #include <linux/input/mt.h>
  21. #include <asm/unaligned.h>
  22. #include "hid-ids.h"
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  25. MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
  26. #define REPORT_ID_HIDPP_SHORT 0x10
  27. #define REPORT_ID_HIDPP_LONG 0x11
  28. #define HIDPP_REPORT_SHORT_LENGTH 7
  29. #define HIDPP_REPORT_LONG_LENGTH 20
  30. #define HIDPP_QUIRK_CLASS_WTP BIT(0)
  31. /*
  32. * There are two hidpp protocols in use, the first version hidpp10 is known
  33. * as register access protocol or RAP, the second version hidpp20 is known as
  34. * feature access protocol or FAP
  35. *
  36. * Most older devices (including the Unifying usb receiver) use the RAP protocol
  37. * where as most newer devices use the FAP protocol. Both protocols are
  38. * compatible with the underlying transport, which could be usb, Unifiying, or
  39. * bluetooth. The message lengths are defined by the hid vendor specific report
  40. * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
  41. * the HIDPP_LONG report type (total message length 20 bytes)
  42. *
  43. * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
  44. * messages. The Unifying receiver itself responds to RAP messages (device index
  45. * is 0xFF for the receiver), and all messages (short or long) with a device
  46. * index between 1 and 6 are passed untouched to the corresponding paired
  47. * Unifying device.
  48. *
  49. * The paired device can be RAP or FAP, it will receive the message untouched
  50. * from the Unifiying receiver.
  51. */
  52. struct fap {
  53. u8 feature_index;
  54. u8 funcindex_clientid;
  55. u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
  56. };
  57. struct rap {
  58. u8 sub_id;
  59. u8 reg_address;
  60. u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
  61. };
  62. struct hidpp_report {
  63. u8 report_id;
  64. u8 device_index;
  65. union {
  66. struct fap fap;
  67. struct rap rap;
  68. u8 rawbytes[sizeof(struct fap)];
  69. };
  70. } __packed;
  71. struct hidpp_device {
  72. struct hid_device *hid_dev;
  73. struct mutex send_mutex;
  74. void *send_receive_buf;
  75. wait_queue_head_t wait;
  76. bool answer_available;
  77. u8 protocol_major;
  78. u8 protocol_minor;
  79. void *private_data;
  80. unsigned long quirks;
  81. };
  82. #define HIDPP_ERROR 0x8f
  83. #define HIDPP_ERROR_SUCCESS 0x00
  84. #define HIDPP_ERROR_INVALID_SUBID 0x01
  85. #define HIDPP_ERROR_INVALID_ADRESS 0x02
  86. #define HIDPP_ERROR_INVALID_VALUE 0x03
  87. #define HIDPP_ERROR_CONNECT_FAIL 0x04
  88. #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
  89. #define HIDPP_ERROR_ALREADY_EXISTS 0x06
  90. #define HIDPP_ERROR_BUSY 0x07
  91. #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
  92. #define HIDPP_ERROR_RESOURCE_ERROR 0x09
  93. #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
  94. #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
  95. #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
  96. static int __hidpp_send_report(struct hid_device *hdev,
  97. struct hidpp_report *hidpp_report)
  98. {
  99. int fields_count, ret;
  100. switch (hidpp_report->report_id) {
  101. case REPORT_ID_HIDPP_SHORT:
  102. fields_count = HIDPP_REPORT_SHORT_LENGTH;
  103. break;
  104. case REPORT_ID_HIDPP_LONG:
  105. fields_count = HIDPP_REPORT_LONG_LENGTH;
  106. break;
  107. default:
  108. return -ENODEV;
  109. }
  110. /*
  111. * set the device_index as the receiver, it will be overwritten by
  112. * hid_hw_request if needed
  113. */
  114. hidpp_report->device_index = 0xff;
  115. ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
  116. (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
  117. HID_REQ_SET_REPORT);
  118. return ret == fields_count ? 0 : -1;
  119. }
  120. static int hidpp_send_message_sync(struct hidpp_device *hidpp,
  121. struct hidpp_report *message,
  122. struct hidpp_report *response)
  123. {
  124. int ret;
  125. mutex_lock(&hidpp->send_mutex);
  126. hidpp->send_receive_buf = response;
  127. hidpp->answer_available = false;
  128. /*
  129. * So that we can later validate the answer when it arrives
  130. * in hidpp_raw_event
  131. */
  132. *response = *message;
  133. ret = __hidpp_send_report(hidpp->hid_dev, message);
  134. if (ret) {
  135. dbg_hid("__hidpp_send_report returned err: %d\n", ret);
  136. memset(response, 0, sizeof(struct hidpp_report));
  137. goto exit;
  138. }
  139. if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
  140. 5*HZ)) {
  141. dbg_hid("%s:timeout waiting for response\n", __func__);
  142. memset(response, 0, sizeof(struct hidpp_report));
  143. ret = -ETIMEDOUT;
  144. }
  145. if (response->report_id == REPORT_ID_HIDPP_SHORT &&
  146. response->fap.feature_index == HIDPP_ERROR) {
  147. ret = response->fap.params[1];
  148. dbg_hid("__hidpp_send_report got hidpp error %02X\n", ret);
  149. goto exit;
  150. }
  151. exit:
  152. mutex_unlock(&hidpp->send_mutex);
  153. return ret;
  154. }
  155. static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
  156. u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
  157. struct hidpp_report *response)
  158. {
  159. struct hidpp_report *message = kzalloc(sizeof(struct hidpp_report),
  160. GFP_KERNEL);
  161. int ret;
  162. if (param_count > sizeof(message->fap.params))
  163. return -EINVAL;
  164. message->report_id = REPORT_ID_HIDPP_LONG;
  165. message->fap.feature_index = feat_index;
  166. message->fap.funcindex_clientid = funcindex_clientid;
  167. memcpy(&message->fap.params, params, param_count);
  168. ret = hidpp_send_message_sync(hidpp, message, response);
  169. kfree(message);
  170. return ret;
  171. }
  172. static inline bool hidpp_match_answer(struct hidpp_report *question,
  173. struct hidpp_report *answer)
  174. {
  175. return (answer->fap.feature_index == question->fap.feature_index) &&
  176. (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
  177. }
  178. static inline bool hidpp_match_error(struct hidpp_report *question,
  179. struct hidpp_report *answer)
  180. {
  181. return (answer->fap.feature_index == HIDPP_ERROR) &&
  182. (answer->fap.funcindex_clientid == question->fap.feature_index) &&
  183. (answer->fap.params[0] == question->fap.funcindex_clientid);
  184. }
  185. /* -------------------------------------------------------------------------- */
  186. /* 0x0000: Root */
  187. /* -------------------------------------------------------------------------- */
  188. #define HIDPP_PAGE_ROOT 0x0000
  189. #define HIDPP_PAGE_ROOT_IDX 0x00
  190. #define CMD_ROOT_GET_FEATURE 0x01
  191. #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
  192. static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
  193. u8 *feature_index, u8 *feature_type)
  194. {
  195. struct hidpp_report response;
  196. int ret;
  197. u8 params[2] = { feature >> 8, feature & 0x00FF };
  198. ret = hidpp_send_fap_command_sync(hidpp,
  199. HIDPP_PAGE_ROOT_IDX,
  200. CMD_ROOT_GET_FEATURE,
  201. params, 2, &response);
  202. if (ret)
  203. return ret;
  204. *feature_index = response.fap.params[0];
  205. *feature_type = response.fap.params[1];
  206. return ret;
  207. }
  208. static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
  209. {
  210. struct hidpp_report response;
  211. int ret;
  212. ret = hidpp_send_fap_command_sync(hidpp,
  213. HIDPP_PAGE_ROOT_IDX,
  214. CMD_ROOT_GET_PROTOCOL_VERSION,
  215. NULL, 0, &response);
  216. if (ret == 1) {
  217. hidpp->protocol_major = 1;
  218. hidpp->protocol_minor = 0;
  219. return 0;
  220. }
  221. if (ret)
  222. return -ret;
  223. hidpp->protocol_major = response.fap.params[0];
  224. hidpp->protocol_minor = response.fap.params[1];
  225. return ret;
  226. }
  227. static bool hidpp_is_connected(struct hidpp_device *hidpp)
  228. {
  229. int ret;
  230. ret = hidpp_root_get_protocol_version(hidpp);
  231. if (!ret)
  232. hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
  233. hidpp->protocol_major, hidpp->protocol_minor);
  234. return ret == 0;
  235. }
  236. /* -------------------------------------------------------------------------- */
  237. /* 0x0005: GetDeviceNameType */
  238. /* -------------------------------------------------------------------------- */
  239. #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
  240. #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
  241. #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
  242. #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
  243. static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
  244. u8 feature_index, u8 *nameLength)
  245. {
  246. struct hidpp_report response;
  247. int ret;
  248. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  249. CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
  250. if (ret)
  251. return -ret;
  252. *nameLength = response.fap.params[0];
  253. return ret;
  254. }
  255. static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
  256. u8 feature_index, u8 char_index, char *device_name, int len_buf)
  257. {
  258. struct hidpp_report response;
  259. int ret, i;
  260. int count;
  261. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  262. CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
  263. &response);
  264. if (ret)
  265. return -ret;
  266. if (response.report_id == REPORT_ID_HIDPP_LONG)
  267. count = HIDPP_REPORT_LONG_LENGTH - 4;
  268. else
  269. count = HIDPP_REPORT_SHORT_LENGTH - 4;
  270. if (len_buf < count)
  271. count = len_buf;
  272. for (i = 0; i < count; i++)
  273. device_name[i] = response.fap.params[i];
  274. return count;
  275. }
  276. static char *hidpp_get_device_name(struct hidpp_device *hidpp, u8 *name_length)
  277. {
  278. u8 feature_type;
  279. u8 feature_index;
  280. u8 __name_length;
  281. char *name;
  282. unsigned index = 0;
  283. int ret;
  284. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
  285. &feature_index, &feature_type);
  286. if (ret)
  287. goto out_err;
  288. ret = hidpp_devicenametype_get_count(hidpp, feature_index,
  289. &__name_length);
  290. if (ret)
  291. goto out_err;
  292. name = kzalloc(__name_length + 1, GFP_KERNEL);
  293. if (!name)
  294. goto out_err;
  295. *name_length = __name_length + 1;
  296. while (index < __name_length)
  297. index += hidpp_devicenametype_get_device_name(hidpp,
  298. feature_index, index, name + index,
  299. __name_length - index);
  300. return name;
  301. out_err:
  302. *name_length = 0;
  303. return NULL;
  304. }
  305. /* -------------------------------------------------------------------------- */
  306. /* 0x6100: TouchPadRawXY */
  307. /* -------------------------------------------------------------------------- */
  308. #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
  309. #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
  310. #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
  311. #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
  312. struct hidpp_touchpad_raw_info {
  313. u16 x_size;
  314. u16 y_size;
  315. u8 z_range;
  316. u8 area_range;
  317. u8 timestamp_unit;
  318. u8 maxcontacts;
  319. u8 origin;
  320. u16 res;
  321. };
  322. struct hidpp_touchpad_raw_xy_finger {
  323. u8 contact_type;
  324. u8 contact_status;
  325. u16 x;
  326. u16 y;
  327. u8 z;
  328. u8 area;
  329. u8 finger_id;
  330. };
  331. struct hidpp_touchpad_raw_xy {
  332. u16 timestamp;
  333. struct hidpp_touchpad_raw_xy_finger fingers[2];
  334. u8 spurious_flag;
  335. u8 end_of_frame;
  336. u8 finger_count;
  337. u8 button;
  338. };
  339. static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
  340. u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
  341. {
  342. struct hidpp_report response;
  343. int ret;
  344. u8 *params = (u8 *)response.fap.params;
  345. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  346. CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
  347. if (ret)
  348. return -ret;
  349. raw_info->x_size = get_unaligned_be16(&params[0]);
  350. raw_info->y_size = get_unaligned_be16(&params[2]);
  351. raw_info->z_range = params[4];
  352. raw_info->area_range = params[5];
  353. raw_info->maxcontacts = params[7];
  354. raw_info->origin = params[8];
  355. /* res is given in unit per inch */
  356. raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
  357. return ret;
  358. }
  359. /* ************************************************************************** */
  360. /* */
  361. /* Device Support */
  362. /* */
  363. /* ************************************************************************** */
  364. /* -------------------------------------------------------------------------- */
  365. /* Touchpad HID++ devices */
  366. /* -------------------------------------------------------------------------- */
  367. struct wtp_data {
  368. struct input_dev *input;
  369. u16 x_size, y_size;
  370. u8 finger_count;
  371. u8 mt_feature_index;
  372. u8 button_feature_index;
  373. u8 maxcontacts;
  374. bool flip_y;
  375. unsigned int resolution;
  376. };
  377. static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  378. struct hid_field *field, struct hid_usage *usage,
  379. unsigned long **bit, int *max)
  380. {
  381. return -1;
  382. }
  383. static void wtp_input_configured(struct hid_device *hdev,
  384. struct hid_input *hidinput)
  385. {
  386. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  387. struct wtp_data *wd = hidpp->private_data;
  388. struct input_dev *input_dev = hidinput->input;
  389. __set_bit(EV_ABS, input_dev->evbit);
  390. __set_bit(EV_KEY, input_dev->evbit);
  391. __clear_bit(EV_REL, input_dev->evbit);
  392. __clear_bit(EV_LED, input_dev->evbit);
  393. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
  394. input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
  395. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
  396. input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
  397. /* Max pressure is not given by the devices, pick one */
  398. input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
  399. input_set_capability(input_dev, EV_KEY, BTN_LEFT);
  400. __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
  401. input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
  402. INPUT_MT_DROP_UNUSED);
  403. wd->input = input_dev;
  404. }
  405. static void wtp_touch_event(struct wtp_data *wd,
  406. struct hidpp_touchpad_raw_xy_finger *touch_report)
  407. {
  408. int slot;
  409. if (!touch_report->finger_id || touch_report->contact_type)
  410. /* no actual data */
  411. return;
  412. slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
  413. input_mt_slot(wd->input, slot);
  414. input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
  415. touch_report->contact_status);
  416. if (touch_report->contact_status) {
  417. input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
  418. touch_report->x);
  419. input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
  420. wd->flip_y ? wd->y_size - touch_report->y :
  421. touch_report->y);
  422. input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
  423. touch_report->area);
  424. }
  425. }
  426. static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
  427. struct hidpp_touchpad_raw_xy *raw)
  428. {
  429. struct wtp_data *wd = hidpp->private_data;
  430. int i;
  431. for (i = 0; i < 2; i++)
  432. wtp_touch_event(wd, &(raw->fingers[i]));
  433. if (raw->end_of_frame)
  434. input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
  435. if (raw->end_of_frame || raw->finger_count <= 2) {
  436. input_mt_sync_frame(wd->input);
  437. input_sync(wd->input);
  438. }
  439. }
  440. static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
  441. {
  442. struct wtp_data *wd = hidpp->private_data;
  443. u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
  444. (data[7] >> 4) * (data[7] >> 4)) / 2;
  445. u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
  446. (data[13] >> 4) * (data[13] >> 4)) / 2;
  447. struct hidpp_touchpad_raw_xy raw = {
  448. .timestamp = data[1],
  449. .fingers = {
  450. {
  451. .contact_type = 0,
  452. .contact_status = !!data[7],
  453. .x = get_unaligned_le16(&data[3]),
  454. .y = get_unaligned_le16(&data[5]),
  455. .z = c1_area,
  456. .area = c1_area,
  457. .finger_id = data[2],
  458. }, {
  459. .contact_type = 0,
  460. .contact_status = !!data[13],
  461. .x = get_unaligned_le16(&data[9]),
  462. .y = get_unaligned_le16(&data[11]),
  463. .z = c2_area,
  464. .area = c2_area,
  465. .finger_id = data[8],
  466. }
  467. },
  468. .finger_count = wd->maxcontacts,
  469. .spurious_flag = 0,
  470. .end_of_frame = (data[0] >> 7) == 0,
  471. .button = data[0] & 0x01,
  472. };
  473. wtp_send_raw_xy_event(hidpp, &raw);
  474. return 1;
  475. }
  476. static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
  477. {
  478. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  479. struct wtp_data *wd = hidpp->private_data;
  480. if (!wd || !wd->input || (data[0] != 0x02) || size < 21)
  481. return 1;
  482. return wtp_mouse_raw_xy_event(hidpp, &data[7]);
  483. }
  484. static int wtp_get_config(struct hidpp_device *hidpp)
  485. {
  486. struct wtp_data *wd = hidpp->private_data;
  487. struct hidpp_touchpad_raw_info raw_info = {0};
  488. u8 feature_type;
  489. int ret;
  490. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
  491. &wd->mt_feature_index, &feature_type);
  492. if (ret)
  493. /* means that the device is not powered up */
  494. return ret;
  495. ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
  496. &raw_info);
  497. if (ret)
  498. return ret;
  499. wd->x_size = raw_info.x_size;
  500. wd->y_size = raw_info.y_size;
  501. wd->maxcontacts = raw_info.maxcontacts;
  502. wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
  503. wd->resolution = raw_info.res;
  504. return 0;
  505. }
  506. static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
  507. {
  508. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  509. struct wtp_data *wd;
  510. wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
  511. GFP_KERNEL);
  512. if (!wd)
  513. return -ENOMEM;
  514. hidpp->private_data = wd;
  515. return 0;
  516. };
  517. /* -------------------------------------------------------------------------- */
  518. /* Generic HID++ devices */
  519. /* -------------------------------------------------------------------------- */
  520. static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  521. struct hid_field *field, struct hid_usage *usage,
  522. unsigned long **bit, int *max)
  523. {
  524. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  525. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  526. return wtp_input_mapping(hdev, hi, field, usage, bit, max);
  527. return 0;
  528. }
  529. static void hidpp_input_configured(struct hid_device *hdev,
  530. struct hid_input *hidinput)
  531. {
  532. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  533. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  534. wtp_input_configured(hdev, hidinput);
  535. }
  536. static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
  537. int size)
  538. {
  539. struct hidpp_report *question = hidpp->send_receive_buf;
  540. struct hidpp_report *answer = hidpp->send_receive_buf;
  541. struct hidpp_report *report = (struct hidpp_report *)data;
  542. /*
  543. * If the mutex is locked then we have a pending answer from a
  544. * previoulsly sent command
  545. */
  546. if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
  547. /*
  548. * Check for a correct hidpp20 answer or the corresponding
  549. * error
  550. */
  551. if (hidpp_match_answer(question, report) ||
  552. hidpp_match_error(question, report)) {
  553. *answer = *report;
  554. hidpp->answer_available = true;
  555. wake_up(&hidpp->wait);
  556. /*
  557. * This was an answer to a command that this driver sent
  558. * We return 1 to hid-core to avoid forwarding the
  559. * command upstream as it has been treated by the driver
  560. */
  561. return 1;
  562. }
  563. }
  564. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  565. return wtp_raw_event(hidpp->hid_dev, data, size);
  566. return 0;
  567. }
  568. static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
  569. u8 *data, int size)
  570. {
  571. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  572. switch (data[0]) {
  573. case REPORT_ID_HIDPP_LONG:
  574. if (size != HIDPP_REPORT_LONG_LENGTH) {
  575. hid_err(hdev, "received hid++ report of bad size (%d)",
  576. size);
  577. return 1;
  578. }
  579. return hidpp_raw_hidpp_event(hidpp, data, size);
  580. case REPORT_ID_HIDPP_SHORT:
  581. if (size != HIDPP_REPORT_SHORT_LENGTH) {
  582. hid_err(hdev, "received hid++ report of bad size (%d)",
  583. size);
  584. return 1;
  585. }
  586. return hidpp_raw_hidpp_event(hidpp, data, size);
  587. }
  588. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  589. return wtp_raw_event(hdev, data, size);
  590. return 0;
  591. }
  592. static void hidpp_overwrite_name(struct hid_device *hdev)
  593. {
  594. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  595. char *name;
  596. u8 name_length;
  597. name = hidpp_get_device_name(hidpp, &name_length);
  598. if (!name)
  599. hid_err(hdev, "unable to retrieve the name of the device");
  600. else
  601. snprintf(hdev->name, sizeof(hdev->name), "%s", name);
  602. kfree(name);
  603. }
  604. static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
  605. {
  606. struct hidpp_device *hidpp;
  607. int ret;
  608. bool connected;
  609. hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
  610. GFP_KERNEL);
  611. if (!hidpp)
  612. return -ENOMEM;
  613. hidpp->hid_dev = hdev;
  614. hid_set_drvdata(hdev, hidpp);
  615. hidpp->quirks = id->driver_data;
  616. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  617. ret = wtp_allocate(hdev, id);
  618. if (ret)
  619. return ret;
  620. }
  621. mutex_init(&hidpp->send_mutex);
  622. init_waitqueue_head(&hidpp->wait);
  623. ret = hid_parse(hdev);
  624. if (ret) {
  625. hid_err(hdev, "%s:parse failed\n", __func__);
  626. goto hid_parse_fail;
  627. }
  628. /* Allow incoming packets */
  629. hid_device_io_start(hdev);
  630. connected = hidpp_is_connected(hidpp);
  631. if (!connected) {
  632. hid_err(hdev, "Device not connected");
  633. goto hid_parse_fail;
  634. }
  635. /* the device is connected, we can ask for its name */
  636. hid_info(hdev, "HID++ %u.%u device connected.\n",
  637. hidpp->protocol_major, hidpp->protocol_minor);
  638. hidpp_overwrite_name(hdev);
  639. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  640. ret = wtp_get_config(hidpp);
  641. if (ret)
  642. goto hid_parse_fail;
  643. }
  644. /* Block incoming packets */
  645. hid_device_io_stop(hdev);
  646. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  647. if (ret) {
  648. hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
  649. goto hid_hw_start_fail;
  650. }
  651. return ret;
  652. hid_hw_start_fail:
  653. hid_parse_fail:
  654. mutex_destroy(&hidpp->send_mutex);
  655. hid_set_drvdata(hdev, NULL);
  656. return ret;
  657. }
  658. static void hidpp_remove(struct hid_device *hdev)
  659. {
  660. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  661. mutex_destroy(&hidpp->send_mutex);
  662. hid_hw_stop(hdev);
  663. }
  664. static const struct hid_device_id hidpp_devices[] = {
  665. { /* wireless touchpad T651 */
  666. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
  667. USB_DEVICE_ID_LOGITECH_T651),
  668. .driver_data = HIDPP_QUIRK_CLASS_WTP },
  669. {}
  670. };
  671. MODULE_DEVICE_TABLE(hid, hidpp_devices);
  672. static struct hid_driver hidpp_driver = {
  673. .name = "logitech-hidpp-device",
  674. .id_table = hidpp_devices,
  675. .probe = hidpp_probe,
  676. .remove = hidpp_remove,
  677. .raw_event = hidpp_raw_event,
  678. .input_configured = hidpp_input_configured,
  679. .input_mapping = hidpp_input_mapping,
  680. };
  681. module_hid_driver(hidpp_driver);