hid-logitech-hidpp.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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. static bool disable_raw_mode;
  27. module_param(disable_raw_mode, bool, 0644);
  28. MODULE_PARM_DESC(disable_raw_mode,
  29. "Disable Raw mode reporting for touchpads and keep firmware gestures.");
  30. #define REPORT_ID_HIDPP_SHORT 0x10
  31. #define REPORT_ID_HIDPP_LONG 0x11
  32. #define HIDPP_REPORT_SHORT_LENGTH 7
  33. #define HIDPP_REPORT_LONG_LENGTH 20
  34. #define HIDPP_QUIRK_CLASS_WTP BIT(0)
  35. #define HIDPP_QUIRK_CLASS_M560 BIT(1)
  36. /* bits 2..20 are reserved for classes */
  37. #define HIDPP_QUIRK_DELAYED_INIT BIT(21)
  38. #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
  39. /*
  40. * There are two hidpp protocols in use, the first version hidpp10 is known
  41. * as register access protocol or RAP, the second version hidpp20 is known as
  42. * feature access protocol or FAP
  43. *
  44. * Most older devices (including the Unifying usb receiver) use the RAP protocol
  45. * where as most newer devices use the FAP protocol. Both protocols are
  46. * compatible with the underlying transport, which could be usb, Unifiying, or
  47. * bluetooth. The message lengths are defined by the hid vendor specific report
  48. * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
  49. * the HIDPP_LONG report type (total message length 20 bytes)
  50. *
  51. * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
  52. * messages. The Unifying receiver itself responds to RAP messages (device index
  53. * is 0xFF for the receiver), and all messages (short or long) with a device
  54. * index between 1 and 6 are passed untouched to the corresponding paired
  55. * Unifying device.
  56. *
  57. * The paired device can be RAP or FAP, it will receive the message untouched
  58. * from the Unifiying receiver.
  59. */
  60. struct fap {
  61. u8 feature_index;
  62. u8 funcindex_clientid;
  63. u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
  64. };
  65. struct rap {
  66. u8 sub_id;
  67. u8 reg_address;
  68. u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
  69. };
  70. struct hidpp_report {
  71. u8 report_id;
  72. u8 device_index;
  73. union {
  74. struct fap fap;
  75. struct rap rap;
  76. u8 rawbytes[sizeof(struct fap)];
  77. };
  78. } __packed;
  79. struct hidpp_device {
  80. struct hid_device *hid_dev;
  81. struct mutex send_mutex;
  82. void *send_receive_buf;
  83. char *name; /* will never be NULL and should not be freed */
  84. wait_queue_head_t wait;
  85. bool answer_available;
  86. u8 protocol_major;
  87. u8 protocol_minor;
  88. void *private_data;
  89. struct work_struct work;
  90. struct kfifo delayed_work_fifo;
  91. atomic_t connected;
  92. struct input_dev *delayed_input;
  93. unsigned long quirks;
  94. };
  95. /* HID++ 1.0 error codes */
  96. #define HIDPP_ERROR 0x8f
  97. #define HIDPP_ERROR_SUCCESS 0x00
  98. #define HIDPP_ERROR_INVALID_SUBID 0x01
  99. #define HIDPP_ERROR_INVALID_ADRESS 0x02
  100. #define HIDPP_ERROR_INVALID_VALUE 0x03
  101. #define HIDPP_ERROR_CONNECT_FAIL 0x04
  102. #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
  103. #define HIDPP_ERROR_ALREADY_EXISTS 0x06
  104. #define HIDPP_ERROR_BUSY 0x07
  105. #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
  106. #define HIDPP_ERROR_RESOURCE_ERROR 0x09
  107. #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
  108. #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
  109. #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
  110. /* HID++ 2.0 error codes */
  111. #define HIDPP20_ERROR 0xff
  112. static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
  113. static int __hidpp_send_report(struct hid_device *hdev,
  114. struct hidpp_report *hidpp_report)
  115. {
  116. int fields_count, ret;
  117. switch (hidpp_report->report_id) {
  118. case REPORT_ID_HIDPP_SHORT:
  119. fields_count = HIDPP_REPORT_SHORT_LENGTH;
  120. break;
  121. case REPORT_ID_HIDPP_LONG:
  122. fields_count = HIDPP_REPORT_LONG_LENGTH;
  123. break;
  124. default:
  125. return -ENODEV;
  126. }
  127. /*
  128. * set the device_index as the receiver, it will be overwritten by
  129. * hid_hw_request if needed
  130. */
  131. hidpp_report->device_index = 0xff;
  132. ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
  133. (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
  134. HID_REQ_SET_REPORT);
  135. return ret == fields_count ? 0 : -1;
  136. }
  137. /**
  138. * hidpp_send_message_sync() returns 0 in case of success, and something else
  139. * in case of a failure.
  140. * - If ' something else' is positive, that means that an error has been raised
  141. * by the protocol itself.
  142. * - If ' something else' is negative, that means that we had a classic error
  143. * (-ENOMEM, -EPIPE, etc...)
  144. */
  145. static int hidpp_send_message_sync(struct hidpp_device *hidpp,
  146. struct hidpp_report *message,
  147. struct hidpp_report *response)
  148. {
  149. int ret;
  150. mutex_lock(&hidpp->send_mutex);
  151. hidpp->send_receive_buf = response;
  152. hidpp->answer_available = false;
  153. /*
  154. * So that we can later validate the answer when it arrives
  155. * in hidpp_raw_event
  156. */
  157. *response = *message;
  158. ret = __hidpp_send_report(hidpp->hid_dev, message);
  159. if (ret) {
  160. dbg_hid("__hidpp_send_report returned err: %d\n", ret);
  161. memset(response, 0, sizeof(struct hidpp_report));
  162. goto exit;
  163. }
  164. if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
  165. 5*HZ)) {
  166. dbg_hid("%s:timeout waiting for response\n", __func__);
  167. memset(response, 0, sizeof(struct hidpp_report));
  168. ret = -ETIMEDOUT;
  169. }
  170. if (response->report_id == REPORT_ID_HIDPP_SHORT &&
  171. response->rap.sub_id == HIDPP_ERROR) {
  172. ret = response->rap.params[1];
  173. dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
  174. goto exit;
  175. }
  176. if (response->report_id == REPORT_ID_HIDPP_LONG &&
  177. response->fap.feature_index == HIDPP20_ERROR) {
  178. ret = response->fap.params[1];
  179. dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
  180. goto exit;
  181. }
  182. exit:
  183. mutex_unlock(&hidpp->send_mutex);
  184. return ret;
  185. }
  186. static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
  187. u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
  188. struct hidpp_report *response)
  189. {
  190. struct hidpp_report *message;
  191. int ret;
  192. if (param_count > sizeof(message->fap.params))
  193. return -EINVAL;
  194. message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
  195. if (!message)
  196. return -ENOMEM;
  197. message->report_id = REPORT_ID_HIDPP_LONG;
  198. message->fap.feature_index = feat_index;
  199. message->fap.funcindex_clientid = funcindex_clientid;
  200. memcpy(&message->fap.params, params, param_count);
  201. ret = hidpp_send_message_sync(hidpp, message, response);
  202. kfree(message);
  203. return ret;
  204. }
  205. static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
  206. u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
  207. struct hidpp_report *response)
  208. {
  209. struct hidpp_report *message;
  210. int ret;
  211. if ((report_id != REPORT_ID_HIDPP_SHORT) &&
  212. (report_id != REPORT_ID_HIDPP_LONG))
  213. return -EINVAL;
  214. if (param_count > sizeof(message->rap.params))
  215. return -EINVAL;
  216. message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
  217. if (!message)
  218. return -ENOMEM;
  219. message->report_id = report_id;
  220. message->rap.sub_id = sub_id;
  221. message->rap.reg_address = reg_address;
  222. memcpy(&message->rap.params, params, param_count);
  223. ret = hidpp_send_message_sync(hidpp_dev, message, response);
  224. kfree(message);
  225. return ret;
  226. }
  227. static void delayed_work_cb(struct work_struct *work)
  228. {
  229. struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
  230. work);
  231. hidpp_connect_event(hidpp);
  232. }
  233. static inline bool hidpp_match_answer(struct hidpp_report *question,
  234. struct hidpp_report *answer)
  235. {
  236. return (answer->fap.feature_index == question->fap.feature_index) &&
  237. (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
  238. }
  239. static inline bool hidpp_match_error(struct hidpp_report *question,
  240. struct hidpp_report *answer)
  241. {
  242. return ((answer->rap.sub_id == HIDPP_ERROR) ||
  243. (answer->fap.feature_index == HIDPP20_ERROR)) &&
  244. (answer->fap.funcindex_clientid == question->fap.feature_index) &&
  245. (answer->fap.params[0] == question->fap.funcindex_clientid);
  246. }
  247. static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
  248. {
  249. return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
  250. (report->rap.sub_id == 0x41);
  251. }
  252. /**
  253. * hidpp_prefix_name() prefixes the current given name with "Logitech ".
  254. */
  255. static void hidpp_prefix_name(char **name, int name_length)
  256. {
  257. #define PREFIX_LENGTH 9 /* "Logitech " */
  258. int new_length;
  259. char *new_name;
  260. if (name_length > PREFIX_LENGTH &&
  261. strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
  262. /* The prefix has is already in the name */
  263. return;
  264. new_length = PREFIX_LENGTH + name_length;
  265. new_name = kzalloc(new_length, GFP_KERNEL);
  266. if (!new_name)
  267. return;
  268. snprintf(new_name, new_length, "Logitech %s", *name);
  269. kfree(*name);
  270. *name = new_name;
  271. }
  272. /* -------------------------------------------------------------------------- */
  273. /* HIDP++ 1.0 commands */
  274. /* -------------------------------------------------------------------------- */
  275. #define HIDPP_SET_REGISTER 0x80
  276. #define HIDPP_GET_REGISTER 0x81
  277. #define HIDPP_SET_LONG_REGISTER 0x82
  278. #define HIDPP_GET_LONG_REGISTER 0x83
  279. #define HIDPP_REG_PAIRING_INFORMATION 0xB5
  280. #define DEVICE_NAME 0x40
  281. static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
  282. {
  283. struct hidpp_report response;
  284. int ret;
  285. /* hid-logitech-dj is in charge of setting the right device index */
  286. u8 params[1] = { DEVICE_NAME };
  287. char *name;
  288. int len;
  289. ret = hidpp_send_rap_command_sync(hidpp_dev,
  290. REPORT_ID_HIDPP_SHORT,
  291. HIDPP_GET_LONG_REGISTER,
  292. HIDPP_REG_PAIRING_INFORMATION,
  293. params, 1, &response);
  294. if (ret)
  295. return NULL;
  296. len = response.rap.params[1];
  297. if (2 + len > sizeof(response.rap.params))
  298. return NULL;
  299. name = kzalloc(len + 1, GFP_KERNEL);
  300. if (!name)
  301. return NULL;
  302. memcpy(name, &response.rap.params[2], len);
  303. /* include the terminating '\0' */
  304. hidpp_prefix_name(&name, len + 1);
  305. return name;
  306. }
  307. /* -------------------------------------------------------------------------- */
  308. /* 0x0000: Root */
  309. /* -------------------------------------------------------------------------- */
  310. #define HIDPP_PAGE_ROOT 0x0000
  311. #define HIDPP_PAGE_ROOT_IDX 0x00
  312. #define CMD_ROOT_GET_FEATURE 0x01
  313. #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
  314. static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
  315. u8 *feature_index, u8 *feature_type)
  316. {
  317. struct hidpp_report response;
  318. int ret;
  319. u8 params[2] = { feature >> 8, feature & 0x00FF };
  320. ret = hidpp_send_fap_command_sync(hidpp,
  321. HIDPP_PAGE_ROOT_IDX,
  322. CMD_ROOT_GET_FEATURE,
  323. params, 2, &response);
  324. if (ret)
  325. return ret;
  326. *feature_index = response.fap.params[0];
  327. *feature_type = response.fap.params[1];
  328. return ret;
  329. }
  330. static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
  331. {
  332. struct hidpp_report response;
  333. int ret;
  334. ret = hidpp_send_fap_command_sync(hidpp,
  335. HIDPP_PAGE_ROOT_IDX,
  336. CMD_ROOT_GET_PROTOCOL_VERSION,
  337. NULL, 0, &response);
  338. if (ret == HIDPP_ERROR_INVALID_SUBID) {
  339. hidpp->protocol_major = 1;
  340. hidpp->protocol_minor = 0;
  341. return 0;
  342. }
  343. /* the device might not be connected */
  344. if (ret == HIDPP_ERROR_RESOURCE_ERROR)
  345. return -EIO;
  346. if (ret > 0) {
  347. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  348. __func__, ret);
  349. return -EPROTO;
  350. }
  351. if (ret)
  352. return ret;
  353. hidpp->protocol_major = response.fap.params[0];
  354. hidpp->protocol_minor = response.fap.params[1];
  355. return ret;
  356. }
  357. static bool hidpp_is_connected(struct hidpp_device *hidpp)
  358. {
  359. int ret;
  360. ret = hidpp_root_get_protocol_version(hidpp);
  361. if (!ret)
  362. hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
  363. hidpp->protocol_major, hidpp->protocol_minor);
  364. return ret == 0;
  365. }
  366. /* -------------------------------------------------------------------------- */
  367. /* 0x0005: GetDeviceNameType */
  368. /* -------------------------------------------------------------------------- */
  369. #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
  370. #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
  371. #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
  372. #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
  373. static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
  374. u8 feature_index, u8 *nameLength)
  375. {
  376. struct hidpp_report response;
  377. int ret;
  378. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  379. CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
  380. if (ret > 0) {
  381. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  382. __func__, ret);
  383. return -EPROTO;
  384. }
  385. if (ret)
  386. return ret;
  387. *nameLength = response.fap.params[0];
  388. return ret;
  389. }
  390. static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
  391. u8 feature_index, u8 char_index, char *device_name, int len_buf)
  392. {
  393. struct hidpp_report response;
  394. int ret, i;
  395. int count;
  396. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  397. CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
  398. &response);
  399. if (ret > 0) {
  400. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  401. __func__, ret);
  402. return -EPROTO;
  403. }
  404. if (ret)
  405. return ret;
  406. if (response.report_id == REPORT_ID_HIDPP_LONG)
  407. count = HIDPP_REPORT_LONG_LENGTH - 4;
  408. else
  409. count = HIDPP_REPORT_SHORT_LENGTH - 4;
  410. if (len_buf < count)
  411. count = len_buf;
  412. for (i = 0; i < count; i++)
  413. device_name[i] = response.fap.params[i];
  414. return count;
  415. }
  416. static char *hidpp_get_device_name(struct hidpp_device *hidpp)
  417. {
  418. u8 feature_type;
  419. u8 feature_index;
  420. u8 __name_length;
  421. char *name;
  422. unsigned index = 0;
  423. int ret;
  424. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
  425. &feature_index, &feature_type);
  426. if (ret)
  427. return NULL;
  428. ret = hidpp_devicenametype_get_count(hidpp, feature_index,
  429. &__name_length);
  430. if (ret)
  431. return NULL;
  432. name = kzalloc(__name_length + 1, GFP_KERNEL);
  433. if (!name)
  434. return NULL;
  435. while (index < __name_length) {
  436. ret = hidpp_devicenametype_get_device_name(hidpp,
  437. feature_index, index, name + index,
  438. __name_length - index);
  439. if (ret <= 0) {
  440. kfree(name);
  441. return NULL;
  442. }
  443. index += ret;
  444. }
  445. /* include the terminating '\0' */
  446. hidpp_prefix_name(&name, __name_length + 1);
  447. return name;
  448. }
  449. /* -------------------------------------------------------------------------- */
  450. /* 0x6100: TouchPadRawXY */
  451. /* -------------------------------------------------------------------------- */
  452. #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
  453. #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
  454. #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
  455. #define EVENT_TOUCHPAD_RAW_XY 0x00
  456. #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
  457. #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
  458. struct hidpp_touchpad_raw_info {
  459. u16 x_size;
  460. u16 y_size;
  461. u8 z_range;
  462. u8 area_range;
  463. u8 timestamp_unit;
  464. u8 maxcontacts;
  465. u8 origin;
  466. u16 res;
  467. };
  468. struct hidpp_touchpad_raw_xy_finger {
  469. u8 contact_type;
  470. u8 contact_status;
  471. u16 x;
  472. u16 y;
  473. u8 z;
  474. u8 area;
  475. u8 finger_id;
  476. };
  477. struct hidpp_touchpad_raw_xy {
  478. u16 timestamp;
  479. struct hidpp_touchpad_raw_xy_finger fingers[2];
  480. u8 spurious_flag;
  481. u8 end_of_frame;
  482. u8 finger_count;
  483. u8 button;
  484. };
  485. static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
  486. u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
  487. {
  488. struct hidpp_report response;
  489. int ret;
  490. u8 *params = (u8 *)response.fap.params;
  491. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  492. CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
  493. if (ret > 0) {
  494. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  495. __func__, ret);
  496. return -EPROTO;
  497. }
  498. if (ret)
  499. return ret;
  500. raw_info->x_size = get_unaligned_be16(&params[0]);
  501. raw_info->y_size = get_unaligned_be16(&params[2]);
  502. raw_info->z_range = params[4];
  503. raw_info->area_range = params[5];
  504. raw_info->maxcontacts = params[7];
  505. raw_info->origin = params[8];
  506. /* res is given in unit per inch */
  507. raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
  508. return ret;
  509. }
  510. static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
  511. u8 feature_index, bool send_raw_reports,
  512. bool sensor_enhanced_settings)
  513. {
  514. struct hidpp_report response;
  515. /*
  516. * Params:
  517. * bit 0 - enable raw
  518. * bit 1 - 16bit Z, no area
  519. * bit 2 - enhanced sensitivity
  520. * bit 3 - width, height (4 bits each) instead of area
  521. * bit 4 - send raw + gestures (degrades smoothness)
  522. * remaining bits - reserved
  523. */
  524. u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
  525. return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
  526. CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
  527. }
  528. static void hidpp_touchpad_touch_event(u8 *data,
  529. struct hidpp_touchpad_raw_xy_finger *finger)
  530. {
  531. u8 x_m = data[0] << 2;
  532. u8 y_m = data[2] << 2;
  533. finger->x = x_m << 6 | data[1];
  534. finger->y = y_m << 6 | data[3];
  535. finger->contact_type = data[0] >> 6;
  536. finger->contact_status = data[2] >> 6;
  537. finger->z = data[4];
  538. finger->area = data[5];
  539. finger->finger_id = data[6] >> 4;
  540. }
  541. static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
  542. u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
  543. {
  544. memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
  545. raw_xy->end_of_frame = data[8] & 0x01;
  546. raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
  547. raw_xy->finger_count = data[15] & 0x0f;
  548. raw_xy->button = (data[8] >> 2) & 0x01;
  549. if (raw_xy->finger_count) {
  550. hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
  551. hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
  552. }
  553. }
  554. /* ************************************************************************** */
  555. /* */
  556. /* Device Support */
  557. /* */
  558. /* ************************************************************************** */
  559. /* -------------------------------------------------------------------------- */
  560. /* Touchpad HID++ devices */
  561. /* -------------------------------------------------------------------------- */
  562. #define WTP_MANUAL_RESOLUTION 39
  563. struct wtp_data {
  564. struct input_dev *input;
  565. u16 x_size, y_size;
  566. u8 finger_count;
  567. u8 mt_feature_index;
  568. u8 button_feature_index;
  569. u8 maxcontacts;
  570. bool flip_y;
  571. unsigned int resolution;
  572. };
  573. static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  574. struct hid_field *field, struct hid_usage *usage,
  575. unsigned long **bit, int *max)
  576. {
  577. return -1;
  578. }
  579. static void wtp_populate_input(struct hidpp_device *hidpp,
  580. struct input_dev *input_dev, bool origin_is_hid_core)
  581. {
  582. struct wtp_data *wd = hidpp->private_data;
  583. __set_bit(EV_ABS, input_dev->evbit);
  584. __set_bit(EV_KEY, input_dev->evbit);
  585. __clear_bit(EV_REL, input_dev->evbit);
  586. __clear_bit(EV_LED, input_dev->evbit);
  587. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
  588. input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
  589. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
  590. input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
  591. /* Max pressure is not given by the devices, pick one */
  592. input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
  593. input_set_capability(input_dev, EV_KEY, BTN_LEFT);
  594. if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
  595. input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
  596. else
  597. __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
  598. input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
  599. INPUT_MT_DROP_UNUSED);
  600. wd->input = input_dev;
  601. }
  602. static void wtp_touch_event(struct wtp_data *wd,
  603. struct hidpp_touchpad_raw_xy_finger *touch_report)
  604. {
  605. int slot;
  606. if (!touch_report->finger_id || touch_report->contact_type)
  607. /* no actual data */
  608. return;
  609. slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
  610. input_mt_slot(wd->input, slot);
  611. input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
  612. touch_report->contact_status);
  613. if (touch_report->contact_status) {
  614. input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
  615. touch_report->x);
  616. input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
  617. wd->flip_y ? wd->y_size - touch_report->y :
  618. touch_report->y);
  619. input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
  620. touch_report->area);
  621. }
  622. }
  623. static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
  624. struct hidpp_touchpad_raw_xy *raw)
  625. {
  626. struct wtp_data *wd = hidpp->private_data;
  627. int i;
  628. for (i = 0; i < 2; i++)
  629. wtp_touch_event(wd, &(raw->fingers[i]));
  630. if (raw->end_of_frame &&
  631. !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
  632. input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
  633. if (raw->end_of_frame || raw->finger_count <= 2) {
  634. input_mt_sync_frame(wd->input);
  635. input_sync(wd->input);
  636. }
  637. }
  638. static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
  639. {
  640. struct wtp_data *wd = hidpp->private_data;
  641. u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
  642. (data[7] >> 4) * (data[7] >> 4)) / 2;
  643. u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
  644. (data[13] >> 4) * (data[13] >> 4)) / 2;
  645. struct hidpp_touchpad_raw_xy raw = {
  646. .timestamp = data[1],
  647. .fingers = {
  648. {
  649. .contact_type = 0,
  650. .contact_status = !!data[7],
  651. .x = get_unaligned_le16(&data[3]),
  652. .y = get_unaligned_le16(&data[5]),
  653. .z = c1_area,
  654. .area = c1_area,
  655. .finger_id = data[2],
  656. }, {
  657. .contact_type = 0,
  658. .contact_status = !!data[13],
  659. .x = get_unaligned_le16(&data[9]),
  660. .y = get_unaligned_le16(&data[11]),
  661. .z = c2_area,
  662. .area = c2_area,
  663. .finger_id = data[8],
  664. }
  665. },
  666. .finger_count = wd->maxcontacts,
  667. .spurious_flag = 0,
  668. .end_of_frame = (data[0] >> 7) == 0,
  669. .button = data[0] & 0x01,
  670. };
  671. wtp_send_raw_xy_event(hidpp, &raw);
  672. return 1;
  673. }
  674. static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
  675. {
  676. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  677. struct wtp_data *wd = hidpp->private_data;
  678. struct hidpp_report *report = (struct hidpp_report *)data;
  679. struct hidpp_touchpad_raw_xy raw;
  680. if (!wd || !wd->input)
  681. return 1;
  682. switch (data[0]) {
  683. case 0x02:
  684. if (size < 2) {
  685. hid_err(hdev, "Received HID report of bad size (%d)",
  686. size);
  687. return 1;
  688. }
  689. if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
  690. input_event(wd->input, EV_KEY, BTN_LEFT,
  691. !!(data[1] & 0x01));
  692. input_event(wd->input, EV_KEY, BTN_RIGHT,
  693. !!(data[1] & 0x02));
  694. input_sync(wd->input);
  695. return 0;
  696. } else {
  697. if (size < 21)
  698. return 1;
  699. return wtp_mouse_raw_xy_event(hidpp, &data[7]);
  700. }
  701. case REPORT_ID_HIDPP_LONG:
  702. /* size is already checked in hidpp_raw_event. */
  703. if ((report->fap.feature_index != wd->mt_feature_index) ||
  704. (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
  705. return 1;
  706. hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
  707. wtp_send_raw_xy_event(hidpp, &raw);
  708. return 0;
  709. }
  710. return 0;
  711. }
  712. static int wtp_get_config(struct hidpp_device *hidpp)
  713. {
  714. struct wtp_data *wd = hidpp->private_data;
  715. struct hidpp_touchpad_raw_info raw_info = {0};
  716. u8 feature_type;
  717. int ret;
  718. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
  719. &wd->mt_feature_index, &feature_type);
  720. if (ret)
  721. /* means that the device is not powered up */
  722. return ret;
  723. ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
  724. &raw_info);
  725. if (ret)
  726. return ret;
  727. wd->x_size = raw_info.x_size;
  728. wd->y_size = raw_info.y_size;
  729. wd->maxcontacts = raw_info.maxcontacts;
  730. wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
  731. wd->resolution = raw_info.res;
  732. if (!wd->resolution)
  733. wd->resolution = WTP_MANUAL_RESOLUTION;
  734. return 0;
  735. }
  736. static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
  737. {
  738. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  739. struct wtp_data *wd;
  740. wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
  741. GFP_KERNEL);
  742. if (!wd)
  743. return -ENOMEM;
  744. hidpp->private_data = wd;
  745. return 0;
  746. };
  747. static int wtp_connect(struct hid_device *hdev, bool connected)
  748. {
  749. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  750. struct wtp_data *wd = hidpp->private_data;
  751. int ret;
  752. if (!connected)
  753. return 0;
  754. if (!wd->x_size) {
  755. ret = wtp_get_config(hidpp);
  756. if (ret) {
  757. hid_err(hdev, "Can not get wtp config: %d\n", ret);
  758. return ret;
  759. }
  760. }
  761. return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
  762. true, true);
  763. }
  764. /* ------------------------------------------------------------------------- */
  765. /* Logitech M560 devices */
  766. /* ------------------------------------------------------------------------- */
  767. /*
  768. * Logitech M560 protocol overview
  769. *
  770. * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
  771. * the sides buttons are pressed, it sends some keyboard keys events
  772. * instead of buttons ones.
  773. * To complicate things further, the middle button keys sequence
  774. * is different from the odd press and the even press.
  775. *
  776. * forward button -> Super_R
  777. * backward button -> Super_L+'d' (press only)
  778. * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
  779. * 2nd time: left-click (press only)
  780. * NB: press-only means that when the button is pressed, the
  781. * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
  782. * together sequentially; instead when the button is released, no event is
  783. * generated !
  784. *
  785. * With the command
  786. * 10<xx>0a 3500af03 (where <xx> is the mouse id),
  787. * the mouse reacts differently:
  788. * - it never sends a keyboard key event
  789. * - for the three mouse button it sends:
  790. * middle button press 11<xx>0a 3500af00...
  791. * side 1 button (forward) press 11<xx>0a 3500b000...
  792. * side 2 button (backward) press 11<xx>0a 3500ae00...
  793. * middle/side1/side2 button release 11<xx>0a 35000000...
  794. */
  795. static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
  796. struct m560_private_data {
  797. struct input_dev *input;
  798. };
  799. /* how buttons are mapped in the report */
  800. #define M560_MOUSE_BTN_LEFT 0x01
  801. #define M560_MOUSE_BTN_RIGHT 0x02
  802. #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
  803. #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
  804. #define M560_SUB_ID 0x0a
  805. #define M560_BUTTON_MODE_REGISTER 0x35
  806. static int m560_send_config_command(struct hid_device *hdev, bool connected)
  807. {
  808. struct hidpp_report response;
  809. struct hidpp_device *hidpp_dev;
  810. hidpp_dev = hid_get_drvdata(hdev);
  811. if (!connected)
  812. return -ENODEV;
  813. return hidpp_send_rap_command_sync(
  814. hidpp_dev,
  815. REPORT_ID_HIDPP_SHORT,
  816. M560_SUB_ID,
  817. M560_BUTTON_MODE_REGISTER,
  818. (u8 *)m560_config_parameter,
  819. sizeof(m560_config_parameter),
  820. &response
  821. );
  822. }
  823. static int m560_allocate(struct hid_device *hdev)
  824. {
  825. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  826. struct m560_private_data *d;
  827. d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
  828. GFP_KERNEL);
  829. if (!d)
  830. return -ENOMEM;
  831. hidpp->private_data = d;
  832. return 0;
  833. };
  834. static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
  835. {
  836. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  837. struct m560_private_data *mydata = hidpp->private_data;
  838. /* sanity check */
  839. if (!mydata || !mydata->input) {
  840. hid_err(hdev, "error in parameter\n");
  841. return -EINVAL;
  842. }
  843. if (size < 7) {
  844. hid_err(hdev, "error in report\n");
  845. return 0;
  846. }
  847. if (data[0] == REPORT_ID_HIDPP_LONG &&
  848. data[2] == M560_SUB_ID && data[6] == 0x00) {
  849. /*
  850. * m560 mouse report for middle, forward and backward button
  851. *
  852. * data[0] = 0x11
  853. * data[1] = device-id
  854. * data[2] = 0x0a
  855. * data[5] = 0xaf -> middle
  856. * 0xb0 -> forward
  857. * 0xae -> backward
  858. * 0x00 -> release all
  859. * data[6] = 0x00
  860. */
  861. switch (data[5]) {
  862. case 0xaf:
  863. input_report_key(mydata->input, BTN_MIDDLE, 1);
  864. break;
  865. case 0xb0:
  866. input_report_key(mydata->input, BTN_FORWARD, 1);
  867. break;
  868. case 0xae:
  869. input_report_key(mydata->input, BTN_BACK, 1);
  870. break;
  871. case 0x00:
  872. input_report_key(mydata->input, BTN_BACK, 0);
  873. input_report_key(mydata->input, BTN_FORWARD, 0);
  874. input_report_key(mydata->input, BTN_MIDDLE, 0);
  875. break;
  876. default:
  877. hid_err(hdev, "error in report\n");
  878. return 0;
  879. }
  880. input_sync(mydata->input);
  881. } else if (data[0] == 0x02) {
  882. /*
  883. * Logitech M560 mouse report
  884. *
  885. * data[0] = type (0x02)
  886. * data[1..2] = buttons
  887. * data[3..5] = xy
  888. * data[6] = wheel
  889. */
  890. int v;
  891. input_report_key(mydata->input, BTN_LEFT,
  892. !!(data[1] & M560_MOUSE_BTN_LEFT));
  893. input_report_key(mydata->input, BTN_RIGHT,
  894. !!(data[1] & M560_MOUSE_BTN_RIGHT));
  895. if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
  896. input_report_rel(mydata->input, REL_HWHEEL, -1);
  897. else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
  898. input_report_rel(mydata->input, REL_HWHEEL, 1);
  899. v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
  900. input_report_rel(mydata->input, REL_X, v);
  901. v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
  902. input_report_rel(mydata->input, REL_Y, v);
  903. v = hid_snto32(data[6], 8);
  904. input_report_rel(mydata->input, REL_WHEEL, v);
  905. input_sync(mydata->input);
  906. }
  907. return 1;
  908. }
  909. static void m560_populate_input(struct hidpp_device *hidpp,
  910. struct input_dev *input_dev, bool origin_is_hid_core)
  911. {
  912. struct m560_private_data *mydata = hidpp->private_data;
  913. mydata->input = input_dev;
  914. __set_bit(EV_KEY, mydata->input->evbit);
  915. __set_bit(BTN_MIDDLE, mydata->input->keybit);
  916. __set_bit(BTN_RIGHT, mydata->input->keybit);
  917. __set_bit(BTN_LEFT, mydata->input->keybit);
  918. __set_bit(BTN_BACK, mydata->input->keybit);
  919. __set_bit(BTN_FORWARD, mydata->input->keybit);
  920. __set_bit(EV_REL, mydata->input->evbit);
  921. __set_bit(REL_X, mydata->input->relbit);
  922. __set_bit(REL_Y, mydata->input->relbit);
  923. __set_bit(REL_WHEEL, mydata->input->relbit);
  924. __set_bit(REL_HWHEEL, mydata->input->relbit);
  925. }
  926. static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  927. struct hid_field *field, struct hid_usage *usage,
  928. unsigned long **bit, int *max)
  929. {
  930. return -1;
  931. }
  932. /* -------------------------------------------------------------------------- */
  933. /* Generic HID++ devices */
  934. /* -------------------------------------------------------------------------- */
  935. static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  936. struct hid_field *field, struct hid_usage *usage,
  937. unsigned long **bit, int *max)
  938. {
  939. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  940. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  941. return wtp_input_mapping(hdev, hi, field, usage, bit, max);
  942. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
  943. field->application != HID_GD_MOUSE)
  944. return m560_input_mapping(hdev, hi, field, usage, bit, max);
  945. return 0;
  946. }
  947. static void hidpp_populate_input(struct hidpp_device *hidpp,
  948. struct input_dev *input, bool origin_is_hid_core)
  949. {
  950. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  951. wtp_populate_input(hidpp, input, origin_is_hid_core);
  952. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
  953. m560_populate_input(hidpp, input, origin_is_hid_core);
  954. }
  955. static void hidpp_input_configured(struct hid_device *hdev,
  956. struct hid_input *hidinput)
  957. {
  958. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  959. struct input_dev *input = hidinput->input;
  960. hidpp_populate_input(hidpp, input, true);
  961. }
  962. static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
  963. int size)
  964. {
  965. struct hidpp_report *question = hidpp->send_receive_buf;
  966. struct hidpp_report *answer = hidpp->send_receive_buf;
  967. struct hidpp_report *report = (struct hidpp_report *)data;
  968. /*
  969. * If the mutex is locked then we have a pending answer from a
  970. * previously sent command.
  971. */
  972. if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
  973. /*
  974. * Check for a correct hidpp20 answer or the corresponding
  975. * error
  976. */
  977. if (hidpp_match_answer(question, report) ||
  978. hidpp_match_error(question, report)) {
  979. *answer = *report;
  980. hidpp->answer_available = true;
  981. wake_up(&hidpp->wait);
  982. /*
  983. * This was an answer to a command that this driver sent
  984. * We return 1 to hid-core to avoid forwarding the
  985. * command upstream as it has been treated by the driver
  986. */
  987. return 1;
  988. }
  989. }
  990. if (unlikely(hidpp_report_is_connect_event(report))) {
  991. atomic_set(&hidpp->connected,
  992. !(report->rap.params[0] & (1 << 6)));
  993. if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) &&
  994. (schedule_work(&hidpp->work) == 0))
  995. dbg_hid("%s: connect event already queued\n", __func__);
  996. return 1;
  997. }
  998. return 0;
  999. }
  1000. static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
  1001. u8 *data, int size)
  1002. {
  1003. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1004. int ret = 0;
  1005. /* Generic HID++ processing. */
  1006. switch (data[0]) {
  1007. case REPORT_ID_HIDPP_LONG:
  1008. if (size != HIDPP_REPORT_LONG_LENGTH) {
  1009. hid_err(hdev, "received hid++ report of bad size (%d)",
  1010. size);
  1011. return 1;
  1012. }
  1013. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  1014. break;
  1015. case REPORT_ID_HIDPP_SHORT:
  1016. if (size != HIDPP_REPORT_SHORT_LENGTH) {
  1017. hid_err(hdev, "received hid++ report of bad size (%d)",
  1018. size);
  1019. return 1;
  1020. }
  1021. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  1022. break;
  1023. }
  1024. /* If no report is available for further processing, skip calling
  1025. * raw_event of subclasses. */
  1026. if (ret != 0)
  1027. return ret;
  1028. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  1029. return wtp_raw_event(hdev, data, size);
  1030. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
  1031. return m560_raw_event(hdev, data, size);
  1032. return 0;
  1033. }
  1034. static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
  1035. {
  1036. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1037. char *name;
  1038. if (use_unifying)
  1039. /*
  1040. * the device is connected through an Unifying receiver, and
  1041. * might not be already connected.
  1042. * Ask the receiver for its name.
  1043. */
  1044. name = hidpp_get_unifying_name(hidpp);
  1045. else
  1046. name = hidpp_get_device_name(hidpp);
  1047. if (!name)
  1048. hid_err(hdev, "unable to retrieve the name of the device");
  1049. else
  1050. snprintf(hdev->name, sizeof(hdev->name), "%s", name);
  1051. kfree(name);
  1052. }
  1053. static int hidpp_input_open(struct input_dev *dev)
  1054. {
  1055. struct hid_device *hid = input_get_drvdata(dev);
  1056. return hid_hw_open(hid);
  1057. }
  1058. static void hidpp_input_close(struct input_dev *dev)
  1059. {
  1060. struct hid_device *hid = input_get_drvdata(dev);
  1061. hid_hw_close(hid);
  1062. }
  1063. static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
  1064. {
  1065. struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
  1066. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1067. if (!input_dev)
  1068. return NULL;
  1069. input_set_drvdata(input_dev, hdev);
  1070. input_dev->open = hidpp_input_open;
  1071. input_dev->close = hidpp_input_close;
  1072. input_dev->name = hidpp->name;
  1073. input_dev->phys = hdev->phys;
  1074. input_dev->uniq = hdev->uniq;
  1075. input_dev->id.bustype = hdev->bus;
  1076. input_dev->id.vendor = hdev->vendor;
  1077. input_dev->id.product = hdev->product;
  1078. input_dev->id.version = hdev->version;
  1079. input_dev->dev.parent = &hdev->dev;
  1080. return input_dev;
  1081. }
  1082. static void hidpp_connect_event(struct hidpp_device *hidpp)
  1083. {
  1084. struct hid_device *hdev = hidpp->hid_dev;
  1085. int ret = 0;
  1086. bool connected = atomic_read(&hidpp->connected);
  1087. struct input_dev *input;
  1088. char *name, *devm_name;
  1089. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  1090. ret = wtp_connect(hdev, connected);
  1091. if (ret)
  1092. return;
  1093. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
  1094. ret = m560_send_config_command(hdev, connected);
  1095. if (ret)
  1096. return;
  1097. }
  1098. if (!connected || hidpp->delayed_input)
  1099. return;
  1100. if (!hidpp->protocol_major) {
  1101. ret = !hidpp_is_connected(hidpp);
  1102. if (ret) {
  1103. hid_err(hdev, "Can not get the protocol version.\n");
  1104. return;
  1105. }
  1106. }
  1107. /* the device is already connected, we can ask for its name and
  1108. * protocol */
  1109. hid_info(hdev, "HID++ %u.%u device connected.\n",
  1110. hidpp->protocol_major, hidpp->protocol_minor);
  1111. if (!hidpp->name || hidpp->name == hdev->name) {
  1112. name = hidpp_get_device_name(hidpp);
  1113. if (!name) {
  1114. hid_err(hdev,
  1115. "unable to retrieve the name of the device");
  1116. return;
  1117. }
  1118. devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
  1119. kfree(name);
  1120. if (!devm_name)
  1121. return;
  1122. hidpp->name = devm_name;
  1123. }
  1124. input = hidpp_allocate_input(hdev);
  1125. if (!input) {
  1126. hid_err(hdev, "cannot allocate new input device: %d\n", ret);
  1127. return;
  1128. }
  1129. hidpp_populate_input(hidpp, input, false);
  1130. ret = input_register_device(input);
  1131. if (ret)
  1132. input_free_device(input);
  1133. hidpp->delayed_input = input;
  1134. }
  1135. static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
  1136. {
  1137. struct hidpp_device *hidpp;
  1138. int ret;
  1139. bool connected;
  1140. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  1141. hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
  1142. GFP_KERNEL);
  1143. if (!hidpp)
  1144. return -ENOMEM;
  1145. hidpp->hid_dev = hdev;
  1146. hidpp->name = hdev->name;
  1147. hid_set_drvdata(hdev, hidpp);
  1148. hidpp->quirks = id->driver_data;
  1149. if (disable_raw_mode) {
  1150. hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
  1151. hidpp->quirks &= ~HIDPP_QUIRK_DELAYED_INIT;
  1152. }
  1153. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  1154. ret = wtp_allocate(hdev, id);
  1155. if (ret)
  1156. goto allocate_fail;
  1157. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
  1158. ret = m560_allocate(hdev);
  1159. if (ret)
  1160. goto allocate_fail;
  1161. }
  1162. INIT_WORK(&hidpp->work, delayed_work_cb);
  1163. mutex_init(&hidpp->send_mutex);
  1164. init_waitqueue_head(&hidpp->wait);
  1165. ret = hid_parse(hdev);
  1166. if (ret) {
  1167. hid_err(hdev, "%s:parse failed\n", __func__);
  1168. goto hid_parse_fail;
  1169. }
  1170. /* Allow incoming packets */
  1171. hid_device_io_start(hdev);
  1172. connected = hidpp_is_connected(hidpp);
  1173. if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
  1174. if (!connected) {
  1175. ret = -ENODEV;
  1176. hid_err(hdev, "Device not connected");
  1177. hid_device_io_stop(hdev);
  1178. goto hid_parse_fail;
  1179. }
  1180. hid_info(hdev, "HID++ %u.%u device connected.\n",
  1181. hidpp->protocol_major, hidpp->protocol_minor);
  1182. }
  1183. hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
  1184. atomic_set(&hidpp->connected, connected);
  1185. if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
  1186. ret = wtp_get_config(hidpp);
  1187. if (ret)
  1188. goto hid_parse_fail;
  1189. }
  1190. /* Block incoming packets */
  1191. hid_device_io_stop(hdev);
  1192. if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
  1193. connect_mask &= ~HID_CONNECT_HIDINPUT;
  1194. ret = hid_hw_start(hdev, connect_mask);
  1195. if (ret) {
  1196. hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
  1197. goto hid_hw_start_fail;
  1198. }
  1199. if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) {
  1200. /* Allow incoming packets */
  1201. hid_device_io_start(hdev);
  1202. hidpp_connect_event(hidpp);
  1203. }
  1204. return ret;
  1205. hid_hw_start_fail:
  1206. hid_parse_fail:
  1207. cancel_work_sync(&hidpp->work);
  1208. mutex_destroy(&hidpp->send_mutex);
  1209. allocate_fail:
  1210. hid_set_drvdata(hdev, NULL);
  1211. return ret;
  1212. }
  1213. static void hidpp_remove(struct hid_device *hdev)
  1214. {
  1215. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1216. cancel_work_sync(&hidpp->work);
  1217. mutex_destroy(&hidpp->send_mutex);
  1218. hid_hw_stop(hdev);
  1219. }
  1220. static const struct hid_device_id hidpp_devices[] = {
  1221. { /* wireless touchpad */
  1222. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  1223. USB_VENDOR_ID_LOGITECH, 0x4011),
  1224. .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
  1225. HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
  1226. { /* wireless touchpad T650 */
  1227. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  1228. USB_VENDOR_ID_LOGITECH, 0x4101),
  1229. .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
  1230. { /* wireless touchpad T651 */
  1231. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
  1232. USB_DEVICE_ID_LOGITECH_T651),
  1233. .driver_data = HIDPP_QUIRK_CLASS_WTP },
  1234. { /* Mouse logitech M560 */
  1235. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  1236. USB_VENDOR_ID_LOGITECH, 0x402d),
  1237. .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
  1238. { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  1239. USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
  1240. {}
  1241. };
  1242. MODULE_DEVICE_TABLE(hid, hidpp_devices);
  1243. static struct hid_driver hidpp_driver = {
  1244. .name = "logitech-hidpp-device",
  1245. .id_table = hidpp_devices,
  1246. .probe = hidpp_probe,
  1247. .remove = hidpp_remove,
  1248. .raw_event = hidpp_raw_event,
  1249. .input_configured = hidpp_input_configured,
  1250. .input_mapping = hidpp_input_mapping,
  1251. };
  1252. module_hid_driver(hidpp_driver);