hid-logitech-hidpp.c 43 KB

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