hid-logitech-hidpp.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  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/input.h>
  16. #include <linux/usb.h>
  17. #include <linux/hid.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/sched.h>
  21. #include <linux/kfifo.h>
  22. #include <linux/input/mt.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/atomic.h>
  25. #include <linux/fixp-arith.h>
  26. #include <asm/unaligned.h>
  27. #include "usbhid/usbhid.h"
  28. #include "hid-ids.h"
  29. MODULE_LICENSE("GPL");
  30. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  31. MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
  32. static bool disable_raw_mode;
  33. module_param(disable_raw_mode, bool, 0644);
  34. MODULE_PARM_DESC(disable_raw_mode,
  35. "Disable Raw mode reporting for touchpads and keep firmware gestures.");
  36. static bool disable_tap_to_click;
  37. module_param(disable_tap_to_click, bool, 0644);
  38. MODULE_PARM_DESC(disable_tap_to_click,
  39. "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
  40. #define REPORT_ID_HIDPP_SHORT 0x10
  41. #define REPORT_ID_HIDPP_LONG 0x11
  42. #define REPORT_ID_HIDPP_VERY_LONG 0x12
  43. #define HIDPP_REPORT_SHORT_LENGTH 7
  44. #define HIDPP_REPORT_LONG_LENGTH 20
  45. #define HIDPP_REPORT_VERY_LONG_LENGTH 64
  46. #define HIDPP_QUIRK_CLASS_WTP BIT(0)
  47. #define HIDPP_QUIRK_CLASS_M560 BIT(1)
  48. #define HIDPP_QUIRK_CLASS_K400 BIT(2)
  49. #define HIDPP_QUIRK_CLASS_G920 BIT(3)
  50. /* bits 2..20 are reserved for classes */
  51. #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21)
  52. #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
  53. #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
  54. #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
  55. #define HIDPP_QUIRK_DELAYED_INIT (HIDPP_QUIRK_NO_HIDINPUT | \
  56. HIDPP_QUIRK_CONNECT_EVENTS)
  57. /*
  58. * There are two hidpp protocols in use, the first version hidpp10 is known
  59. * as register access protocol or RAP, the second version hidpp20 is known as
  60. * feature access protocol or FAP
  61. *
  62. * Most older devices (including the Unifying usb receiver) use the RAP protocol
  63. * where as most newer devices use the FAP protocol. Both protocols are
  64. * compatible with the underlying transport, which could be usb, Unifiying, or
  65. * bluetooth. The message lengths are defined by the hid vendor specific report
  66. * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
  67. * the HIDPP_LONG report type (total message length 20 bytes)
  68. *
  69. * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
  70. * messages. The Unifying receiver itself responds to RAP messages (device index
  71. * is 0xFF for the receiver), and all messages (short or long) with a device
  72. * index between 1 and 6 are passed untouched to the corresponding paired
  73. * Unifying device.
  74. *
  75. * The paired device can be RAP or FAP, it will receive the message untouched
  76. * from the Unifiying receiver.
  77. */
  78. struct fap {
  79. u8 feature_index;
  80. u8 funcindex_clientid;
  81. u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
  82. };
  83. struct rap {
  84. u8 sub_id;
  85. u8 reg_address;
  86. u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
  87. };
  88. struct hidpp_report {
  89. u8 report_id;
  90. u8 device_index;
  91. union {
  92. struct fap fap;
  93. struct rap rap;
  94. u8 rawbytes[sizeof(struct fap)];
  95. };
  96. } __packed;
  97. struct hidpp_device {
  98. struct hid_device *hid_dev;
  99. struct mutex send_mutex;
  100. void *send_receive_buf;
  101. char *name; /* will never be NULL and should not be freed */
  102. wait_queue_head_t wait;
  103. bool answer_available;
  104. u8 protocol_major;
  105. u8 protocol_minor;
  106. void *private_data;
  107. struct work_struct work;
  108. struct kfifo delayed_work_fifo;
  109. atomic_t connected;
  110. struct input_dev *delayed_input;
  111. unsigned long quirks;
  112. };
  113. /* HID++ 1.0 error codes */
  114. #define HIDPP_ERROR 0x8f
  115. #define HIDPP_ERROR_SUCCESS 0x00
  116. #define HIDPP_ERROR_INVALID_SUBID 0x01
  117. #define HIDPP_ERROR_INVALID_ADRESS 0x02
  118. #define HIDPP_ERROR_INVALID_VALUE 0x03
  119. #define HIDPP_ERROR_CONNECT_FAIL 0x04
  120. #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
  121. #define HIDPP_ERROR_ALREADY_EXISTS 0x06
  122. #define HIDPP_ERROR_BUSY 0x07
  123. #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
  124. #define HIDPP_ERROR_RESOURCE_ERROR 0x09
  125. #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
  126. #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
  127. #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
  128. /* HID++ 2.0 error codes */
  129. #define HIDPP20_ERROR 0xff
  130. static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
  131. static int __hidpp_send_report(struct hid_device *hdev,
  132. struct hidpp_report *hidpp_report)
  133. {
  134. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  135. int fields_count, ret;
  136. hidpp = hid_get_drvdata(hdev);
  137. switch (hidpp_report->report_id) {
  138. case REPORT_ID_HIDPP_SHORT:
  139. fields_count = HIDPP_REPORT_SHORT_LENGTH;
  140. break;
  141. case REPORT_ID_HIDPP_LONG:
  142. fields_count = HIDPP_REPORT_LONG_LENGTH;
  143. break;
  144. case REPORT_ID_HIDPP_VERY_LONG:
  145. fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
  146. break;
  147. default:
  148. return -ENODEV;
  149. }
  150. /*
  151. * set the device_index as the receiver, it will be overwritten by
  152. * hid_hw_request if needed
  153. */
  154. hidpp_report->device_index = 0xff;
  155. if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
  156. ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
  157. } else {
  158. ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
  159. (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
  160. HID_REQ_SET_REPORT);
  161. }
  162. return ret == fields_count ? 0 : -1;
  163. }
  164. /**
  165. * hidpp_send_message_sync() returns 0 in case of success, and something else
  166. * in case of a failure.
  167. * - If ' something else' is positive, that means that an error has been raised
  168. * by the protocol itself.
  169. * - If ' something else' is negative, that means that we had a classic error
  170. * (-ENOMEM, -EPIPE, etc...)
  171. */
  172. static int hidpp_send_message_sync(struct hidpp_device *hidpp,
  173. struct hidpp_report *message,
  174. struct hidpp_report *response)
  175. {
  176. int ret;
  177. mutex_lock(&hidpp->send_mutex);
  178. hidpp->send_receive_buf = response;
  179. hidpp->answer_available = false;
  180. /*
  181. * So that we can later validate the answer when it arrives
  182. * in hidpp_raw_event
  183. */
  184. *response = *message;
  185. ret = __hidpp_send_report(hidpp->hid_dev, message);
  186. if (ret) {
  187. dbg_hid("__hidpp_send_report returned err: %d\n", ret);
  188. memset(response, 0, sizeof(struct hidpp_report));
  189. goto exit;
  190. }
  191. if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
  192. 5*HZ)) {
  193. dbg_hid("%s:timeout waiting for response\n", __func__);
  194. memset(response, 0, sizeof(struct hidpp_report));
  195. ret = -ETIMEDOUT;
  196. }
  197. if (response->report_id == REPORT_ID_HIDPP_SHORT &&
  198. response->rap.sub_id == HIDPP_ERROR) {
  199. ret = response->rap.params[1];
  200. dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
  201. goto exit;
  202. }
  203. if ((response->report_id == REPORT_ID_HIDPP_LONG ||
  204. response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
  205. response->fap.feature_index == HIDPP20_ERROR) {
  206. ret = response->fap.params[1];
  207. dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
  208. goto exit;
  209. }
  210. exit:
  211. mutex_unlock(&hidpp->send_mutex);
  212. return ret;
  213. }
  214. static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
  215. u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
  216. struct hidpp_report *response)
  217. {
  218. struct hidpp_report *message;
  219. int ret;
  220. if (param_count > sizeof(message->fap.params))
  221. return -EINVAL;
  222. message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
  223. if (!message)
  224. return -ENOMEM;
  225. if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
  226. message->report_id = REPORT_ID_HIDPP_VERY_LONG;
  227. else
  228. message->report_id = REPORT_ID_HIDPP_LONG;
  229. message->fap.feature_index = feat_index;
  230. message->fap.funcindex_clientid = funcindex_clientid;
  231. memcpy(&message->fap.params, params, param_count);
  232. ret = hidpp_send_message_sync(hidpp, message, response);
  233. kfree(message);
  234. return ret;
  235. }
  236. static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
  237. u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
  238. struct hidpp_report *response)
  239. {
  240. struct hidpp_report *message;
  241. int ret, max_count;
  242. switch (report_id) {
  243. case REPORT_ID_HIDPP_SHORT:
  244. max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
  245. break;
  246. case REPORT_ID_HIDPP_LONG:
  247. max_count = HIDPP_REPORT_LONG_LENGTH - 4;
  248. break;
  249. case REPORT_ID_HIDPP_VERY_LONG:
  250. max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
  251. break;
  252. default:
  253. return -EINVAL;
  254. }
  255. if (param_count > max_count)
  256. return -EINVAL;
  257. message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
  258. if (!message)
  259. return -ENOMEM;
  260. message->report_id = report_id;
  261. message->rap.sub_id = sub_id;
  262. message->rap.reg_address = reg_address;
  263. memcpy(&message->rap.params, params, param_count);
  264. ret = hidpp_send_message_sync(hidpp_dev, message, response);
  265. kfree(message);
  266. return ret;
  267. }
  268. static void delayed_work_cb(struct work_struct *work)
  269. {
  270. struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
  271. work);
  272. hidpp_connect_event(hidpp);
  273. }
  274. static inline bool hidpp_match_answer(struct hidpp_report *question,
  275. struct hidpp_report *answer)
  276. {
  277. return (answer->fap.feature_index == question->fap.feature_index) &&
  278. (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
  279. }
  280. static inline bool hidpp_match_error(struct hidpp_report *question,
  281. struct hidpp_report *answer)
  282. {
  283. return ((answer->rap.sub_id == HIDPP_ERROR) ||
  284. (answer->fap.feature_index == HIDPP20_ERROR)) &&
  285. (answer->fap.funcindex_clientid == question->fap.feature_index) &&
  286. (answer->fap.params[0] == question->fap.funcindex_clientid);
  287. }
  288. static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
  289. {
  290. return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
  291. (report->rap.sub_id == 0x41);
  292. }
  293. /**
  294. * hidpp_prefix_name() prefixes the current given name with "Logitech ".
  295. */
  296. static void hidpp_prefix_name(char **name, int name_length)
  297. {
  298. #define PREFIX_LENGTH 9 /* "Logitech " */
  299. int new_length;
  300. char *new_name;
  301. if (name_length > PREFIX_LENGTH &&
  302. strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
  303. /* The prefix has is already in the name */
  304. return;
  305. new_length = PREFIX_LENGTH + name_length;
  306. new_name = kzalloc(new_length, GFP_KERNEL);
  307. if (!new_name)
  308. return;
  309. snprintf(new_name, new_length, "Logitech %s", *name);
  310. kfree(*name);
  311. *name = new_name;
  312. }
  313. /* -------------------------------------------------------------------------- */
  314. /* HIDP++ 1.0 commands */
  315. /* -------------------------------------------------------------------------- */
  316. #define HIDPP_SET_REGISTER 0x80
  317. #define HIDPP_GET_REGISTER 0x81
  318. #define HIDPP_SET_LONG_REGISTER 0x82
  319. #define HIDPP_GET_LONG_REGISTER 0x83
  320. #define HIDPP_REG_PAIRING_INFORMATION 0xB5
  321. #define DEVICE_NAME 0x40
  322. static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
  323. {
  324. struct hidpp_report response;
  325. int ret;
  326. /* hid-logitech-dj is in charge of setting the right device index */
  327. u8 params[1] = { DEVICE_NAME };
  328. char *name;
  329. int len;
  330. ret = hidpp_send_rap_command_sync(hidpp_dev,
  331. REPORT_ID_HIDPP_SHORT,
  332. HIDPP_GET_LONG_REGISTER,
  333. HIDPP_REG_PAIRING_INFORMATION,
  334. params, 1, &response);
  335. if (ret)
  336. return NULL;
  337. len = response.rap.params[1];
  338. if (2 + len > sizeof(response.rap.params))
  339. return NULL;
  340. name = kzalloc(len + 1, GFP_KERNEL);
  341. if (!name)
  342. return NULL;
  343. memcpy(name, &response.rap.params[2], len);
  344. /* include the terminating '\0' */
  345. hidpp_prefix_name(&name, len + 1);
  346. return name;
  347. }
  348. /* -------------------------------------------------------------------------- */
  349. /* 0x0000: Root */
  350. /* -------------------------------------------------------------------------- */
  351. #define HIDPP_PAGE_ROOT 0x0000
  352. #define HIDPP_PAGE_ROOT_IDX 0x00
  353. #define CMD_ROOT_GET_FEATURE 0x01
  354. #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
  355. static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
  356. u8 *feature_index, u8 *feature_type)
  357. {
  358. struct hidpp_report response;
  359. int ret;
  360. u8 params[2] = { feature >> 8, feature & 0x00FF };
  361. ret = hidpp_send_fap_command_sync(hidpp,
  362. HIDPP_PAGE_ROOT_IDX,
  363. CMD_ROOT_GET_FEATURE,
  364. params, 2, &response);
  365. if (ret)
  366. return ret;
  367. *feature_index = response.fap.params[0];
  368. *feature_type = response.fap.params[1];
  369. return ret;
  370. }
  371. static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
  372. {
  373. struct hidpp_report response;
  374. int ret;
  375. ret = hidpp_send_fap_command_sync(hidpp,
  376. HIDPP_PAGE_ROOT_IDX,
  377. CMD_ROOT_GET_PROTOCOL_VERSION,
  378. NULL, 0, &response);
  379. if (ret == HIDPP_ERROR_INVALID_SUBID) {
  380. hidpp->protocol_major = 1;
  381. hidpp->protocol_minor = 0;
  382. return 0;
  383. }
  384. /* the device might not be connected */
  385. if (ret == HIDPP_ERROR_RESOURCE_ERROR)
  386. return -EIO;
  387. if (ret > 0) {
  388. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  389. __func__, ret);
  390. return -EPROTO;
  391. }
  392. if (ret)
  393. return ret;
  394. hidpp->protocol_major = response.fap.params[0];
  395. hidpp->protocol_minor = response.fap.params[1];
  396. return ret;
  397. }
  398. static bool hidpp_is_connected(struct hidpp_device *hidpp)
  399. {
  400. int ret;
  401. ret = hidpp_root_get_protocol_version(hidpp);
  402. if (!ret)
  403. hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
  404. hidpp->protocol_major, hidpp->protocol_minor);
  405. return ret == 0;
  406. }
  407. /* -------------------------------------------------------------------------- */
  408. /* 0x0005: GetDeviceNameType */
  409. /* -------------------------------------------------------------------------- */
  410. #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
  411. #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
  412. #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
  413. #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
  414. static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
  415. u8 feature_index, u8 *nameLength)
  416. {
  417. struct hidpp_report response;
  418. int ret;
  419. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  420. CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
  421. if (ret > 0) {
  422. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  423. __func__, ret);
  424. return -EPROTO;
  425. }
  426. if (ret)
  427. return ret;
  428. *nameLength = response.fap.params[0];
  429. return ret;
  430. }
  431. static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
  432. u8 feature_index, u8 char_index, char *device_name, int len_buf)
  433. {
  434. struct hidpp_report response;
  435. int ret, i;
  436. int count;
  437. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  438. CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
  439. &response);
  440. if (ret > 0) {
  441. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  442. __func__, ret);
  443. return -EPROTO;
  444. }
  445. if (ret)
  446. return ret;
  447. switch (response.report_id) {
  448. case REPORT_ID_HIDPP_VERY_LONG:
  449. count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
  450. break;
  451. case REPORT_ID_HIDPP_LONG:
  452. count = HIDPP_REPORT_LONG_LENGTH - 4;
  453. break;
  454. case REPORT_ID_HIDPP_SHORT:
  455. count = HIDPP_REPORT_SHORT_LENGTH - 4;
  456. break;
  457. default:
  458. return -EPROTO;
  459. }
  460. if (len_buf < count)
  461. count = len_buf;
  462. for (i = 0; i < count; i++)
  463. device_name[i] = response.fap.params[i];
  464. return count;
  465. }
  466. static char *hidpp_get_device_name(struct hidpp_device *hidpp)
  467. {
  468. u8 feature_type;
  469. u8 feature_index;
  470. u8 __name_length;
  471. char *name;
  472. unsigned index = 0;
  473. int ret;
  474. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
  475. &feature_index, &feature_type);
  476. if (ret)
  477. return NULL;
  478. ret = hidpp_devicenametype_get_count(hidpp, feature_index,
  479. &__name_length);
  480. if (ret)
  481. return NULL;
  482. name = kzalloc(__name_length + 1, GFP_KERNEL);
  483. if (!name)
  484. return NULL;
  485. while (index < __name_length) {
  486. ret = hidpp_devicenametype_get_device_name(hidpp,
  487. feature_index, index, name + index,
  488. __name_length - index);
  489. if (ret <= 0) {
  490. kfree(name);
  491. return NULL;
  492. }
  493. index += ret;
  494. }
  495. /* include the terminating '\0' */
  496. hidpp_prefix_name(&name, __name_length + 1);
  497. return name;
  498. }
  499. /* -------------------------------------------------------------------------- */
  500. /* 0x6010: Touchpad FW items */
  501. /* -------------------------------------------------------------------------- */
  502. #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
  503. #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
  504. struct hidpp_touchpad_fw_items {
  505. uint8_t presence;
  506. uint8_t desired_state;
  507. uint8_t state;
  508. uint8_t persistent;
  509. };
  510. /**
  511. * send a set state command to the device by reading the current items->state
  512. * field. items is then filled with the current state.
  513. */
  514. static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
  515. u8 feature_index,
  516. struct hidpp_touchpad_fw_items *items)
  517. {
  518. struct hidpp_report response;
  519. int ret;
  520. u8 *params = (u8 *)response.fap.params;
  521. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  522. CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
  523. if (ret > 0) {
  524. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  525. __func__, ret);
  526. return -EPROTO;
  527. }
  528. if (ret)
  529. return ret;
  530. items->presence = params[0];
  531. items->desired_state = params[1];
  532. items->state = params[2];
  533. items->persistent = params[3];
  534. return 0;
  535. }
  536. /* -------------------------------------------------------------------------- */
  537. /* 0x6100: TouchPadRawXY */
  538. /* -------------------------------------------------------------------------- */
  539. #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
  540. #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
  541. #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
  542. #define EVENT_TOUCHPAD_RAW_XY 0x00
  543. #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
  544. #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
  545. struct hidpp_touchpad_raw_info {
  546. u16 x_size;
  547. u16 y_size;
  548. u8 z_range;
  549. u8 area_range;
  550. u8 timestamp_unit;
  551. u8 maxcontacts;
  552. u8 origin;
  553. u16 res;
  554. };
  555. struct hidpp_touchpad_raw_xy_finger {
  556. u8 contact_type;
  557. u8 contact_status;
  558. u16 x;
  559. u16 y;
  560. u8 z;
  561. u8 area;
  562. u8 finger_id;
  563. };
  564. struct hidpp_touchpad_raw_xy {
  565. u16 timestamp;
  566. struct hidpp_touchpad_raw_xy_finger fingers[2];
  567. u8 spurious_flag;
  568. u8 end_of_frame;
  569. u8 finger_count;
  570. u8 button;
  571. };
  572. static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
  573. u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
  574. {
  575. struct hidpp_report response;
  576. int ret;
  577. u8 *params = (u8 *)response.fap.params;
  578. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  579. CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
  580. if (ret > 0) {
  581. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  582. __func__, ret);
  583. return -EPROTO;
  584. }
  585. if (ret)
  586. return ret;
  587. raw_info->x_size = get_unaligned_be16(&params[0]);
  588. raw_info->y_size = get_unaligned_be16(&params[2]);
  589. raw_info->z_range = params[4];
  590. raw_info->area_range = params[5];
  591. raw_info->maxcontacts = params[7];
  592. raw_info->origin = params[8];
  593. /* res is given in unit per inch */
  594. raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
  595. return ret;
  596. }
  597. static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
  598. u8 feature_index, bool send_raw_reports,
  599. bool sensor_enhanced_settings)
  600. {
  601. struct hidpp_report response;
  602. /*
  603. * Params:
  604. * bit 0 - enable raw
  605. * bit 1 - 16bit Z, no area
  606. * bit 2 - enhanced sensitivity
  607. * bit 3 - width, height (4 bits each) instead of area
  608. * bit 4 - send raw + gestures (degrades smoothness)
  609. * remaining bits - reserved
  610. */
  611. u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
  612. return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
  613. CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
  614. }
  615. static void hidpp_touchpad_touch_event(u8 *data,
  616. struct hidpp_touchpad_raw_xy_finger *finger)
  617. {
  618. u8 x_m = data[0] << 2;
  619. u8 y_m = data[2] << 2;
  620. finger->x = x_m << 6 | data[1];
  621. finger->y = y_m << 6 | data[3];
  622. finger->contact_type = data[0] >> 6;
  623. finger->contact_status = data[2] >> 6;
  624. finger->z = data[4];
  625. finger->area = data[5];
  626. finger->finger_id = data[6] >> 4;
  627. }
  628. static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
  629. u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
  630. {
  631. memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
  632. raw_xy->end_of_frame = data[8] & 0x01;
  633. raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
  634. raw_xy->finger_count = data[15] & 0x0f;
  635. raw_xy->button = (data[8] >> 2) & 0x01;
  636. if (raw_xy->finger_count) {
  637. hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
  638. hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
  639. }
  640. }
  641. /* -------------------------------------------------------------------------- */
  642. /* 0x8123: Force feedback support */
  643. /* -------------------------------------------------------------------------- */
  644. #define HIDPP_FF_GET_INFO 0x01
  645. #define HIDPP_FF_RESET_ALL 0x11
  646. #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
  647. #define HIDPP_FF_SET_EFFECT_STATE 0x31
  648. #define HIDPP_FF_DESTROY_EFFECT 0x41
  649. #define HIDPP_FF_GET_APERTURE 0x51
  650. #define HIDPP_FF_SET_APERTURE 0x61
  651. #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
  652. #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
  653. #define HIDPP_FF_EFFECT_STATE_GET 0x00
  654. #define HIDPP_FF_EFFECT_STATE_STOP 0x01
  655. #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
  656. #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
  657. #define HIDPP_FF_EFFECT_CONSTANT 0x00
  658. #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
  659. #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
  660. #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
  661. #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
  662. #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
  663. #define HIDPP_FF_EFFECT_SPRING 0x06
  664. #define HIDPP_FF_EFFECT_DAMPER 0x07
  665. #define HIDPP_FF_EFFECT_FRICTION 0x08
  666. #define HIDPP_FF_EFFECT_INERTIA 0x09
  667. #define HIDPP_FF_EFFECT_RAMP 0x0A
  668. #define HIDPP_FF_EFFECT_AUTOSTART 0x80
  669. #define HIDPP_FF_EFFECTID_NONE -1
  670. #define HIDPP_FF_EFFECTID_AUTOCENTER -2
  671. #define HIDPP_FF_MAX_PARAMS 20
  672. #define HIDPP_FF_RESERVED_SLOTS 1
  673. struct hidpp_ff_private_data {
  674. struct hidpp_device *hidpp;
  675. u8 feature_index;
  676. u8 version;
  677. u16 gain;
  678. s16 range;
  679. u8 slot_autocenter;
  680. u8 num_effects;
  681. int *effect_ids;
  682. struct workqueue_struct *wq;
  683. atomic_t workqueue_size;
  684. };
  685. struct hidpp_ff_work_data {
  686. struct work_struct work;
  687. struct hidpp_ff_private_data *data;
  688. int effect_id;
  689. u8 command;
  690. u8 params[HIDPP_FF_MAX_PARAMS];
  691. u8 size;
  692. };
  693. static const signed short hiddpp_ff_effects[] = {
  694. FF_CONSTANT,
  695. FF_PERIODIC,
  696. FF_SINE,
  697. FF_SQUARE,
  698. FF_SAW_UP,
  699. FF_SAW_DOWN,
  700. FF_TRIANGLE,
  701. FF_SPRING,
  702. FF_DAMPER,
  703. FF_AUTOCENTER,
  704. FF_GAIN,
  705. -1
  706. };
  707. static const signed short hiddpp_ff_effects_v2[] = {
  708. FF_RAMP,
  709. FF_FRICTION,
  710. FF_INERTIA,
  711. -1
  712. };
  713. static const u8 HIDPP_FF_CONDITION_CMDS[] = {
  714. HIDPP_FF_EFFECT_SPRING,
  715. HIDPP_FF_EFFECT_FRICTION,
  716. HIDPP_FF_EFFECT_DAMPER,
  717. HIDPP_FF_EFFECT_INERTIA
  718. };
  719. static const char *HIDPP_FF_CONDITION_NAMES[] = {
  720. "spring",
  721. "friction",
  722. "damper",
  723. "inertia"
  724. };
  725. static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
  726. {
  727. int i;
  728. for (i = 0; i < data->num_effects; i++)
  729. if (data->effect_ids[i] == effect_id)
  730. return i+1;
  731. return 0;
  732. }
  733. static void hidpp_ff_work_handler(struct work_struct *w)
  734. {
  735. struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
  736. struct hidpp_ff_private_data *data = wd->data;
  737. struct hidpp_report response;
  738. u8 slot;
  739. int ret;
  740. /* add slot number if needed */
  741. switch (wd->effect_id) {
  742. case HIDPP_FF_EFFECTID_AUTOCENTER:
  743. wd->params[0] = data->slot_autocenter;
  744. break;
  745. case HIDPP_FF_EFFECTID_NONE:
  746. /* leave slot as zero */
  747. break;
  748. default:
  749. /* find current slot for effect */
  750. wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
  751. break;
  752. }
  753. /* send command and wait for reply */
  754. ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
  755. wd->command, wd->params, wd->size, &response);
  756. if (ret) {
  757. hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
  758. goto out;
  759. }
  760. /* parse return data */
  761. switch (wd->command) {
  762. case HIDPP_FF_DOWNLOAD_EFFECT:
  763. slot = response.fap.params[0];
  764. if (slot > 0 && slot <= data->num_effects) {
  765. if (wd->effect_id >= 0)
  766. /* regular effect uploaded */
  767. data->effect_ids[slot-1] = wd->effect_id;
  768. else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
  769. /* autocenter spring uploaded */
  770. data->slot_autocenter = slot;
  771. }
  772. break;
  773. case HIDPP_FF_DESTROY_EFFECT:
  774. if (wd->effect_id >= 0)
  775. /* regular effect destroyed */
  776. data->effect_ids[wd->params[0]-1] = -1;
  777. else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
  778. /* autocenter spring destoyed */
  779. data->slot_autocenter = 0;
  780. break;
  781. case HIDPP_FF_SET_GLOBAL_GAINS:
  782. data->gain = (wd->params[0] << 8) + wd->params[1];
  783. break;
  784. case HIDPP_FF_SET_APERTURE:
  785. data->range = (wd->params[0] << 8) + wd->params[1];
  786. break;
  787. default:
  788. /* no action needed */
  789. break;
  790. }
  791. out:
  792. atomic_dec(&data->workqueue_size);
  793. kfree(wd);
  794. }
  795. static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
  796. {
  797. struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
  798. int s;
  799. if (!wd)
  800. return -ENOMEM;
  801. INIT_WORK(&wd->work, hidpp_ff_work_handler);
  802. wd->data = data;
  803. wd->effect_id = effect_id;
  804. wd->command = command;
  805. wd->size = size;
  806. memcpy(wd->params, params, size);
  807. atomic_inc(&data->workqueue_size);
  808. queue_work(data->wq, &wd->work);
  809. /* warn about excessive queue size */
  810. s = atomic_read(&data->workqueue_size);
  811. if (s >= 20 && s % 20 == 0)
  812. hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
  813. return 0;
  814. }
  815. static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
  816. {
  817. struct hidpp_ff_private_data *data = dev->ff->private;
  818. u8 params[20];
  819. u8 size;
  820. int force;
  821. /* set common parameters */
  822. params[2] = effect->replay.length >> 8;
  823. params[3] = effect->replay.length & 255;
  824. params[4] = effect->replay.delay >> 8;
  825. params[5] = effect->replay.delay & 255;
  826. switch (effect->type) {
  827. case FF_CONSTANT:
  828. force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  829. params[1] = HIDPP_FF_EFFECT_CONSTANT;
  830. params[6] = force >> 8;
  831. params[7] = force & 255;
  832. params[8] = effect->u.constant.envelope.attack_level >> 7;
  833. params[9] = effect->u.constant.envelope.attack_length >> 8;
  834. params[10] = effect->u.constant.envelope.attack_length & 255;
  835. params[11] = effect->u.constant.envelope.fade_level >> 7;
  836. params[12] = effect->u.constant.envelope.fade_length >> 8;
  837. params[13] = effect->u.constant.envelope.fade_length & 255;
  838. size = 14;
  839. dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
  840. effect->u.constant.level,
  841. effect->direction, force);
  842. dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
  843. effect->u.constant.envelope.attack_level,
  844. effect->u.constant.envelope.attack_length,
  845. effect->u.constant.envelope.fade_level,
  846. effect->u.constant.envelope.fade_length);
  847. break;
  848. case FF_PERIODIC:
  849. {
  850. switch (effect->u.periodic.waveform) {
  851. case FF_SINE:
  852. params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
  853. break;
  854. case FF_SQUARE:
  855. params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
  856. break;
  857. case FF_SAW_UP:
  858. params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
  859. break;
  860. case FF_SAW_DOWN:
  861. params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
  862. break;
  863. case FF_TRIANGLE:
  864. params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
  865. break;
  866. default:
  867. hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
  868. return -EINVAL;
  869. }
  870. force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  871. params[6] = effect->u.periodic.magnitude >> 8;
  872. params[7] = effect->u.periodic.magnitude & 255;
  873. params[8] = effect->u.periodic.offset >> 8;
  874. params[9] = effect->u.periodic.offset & 255;
  875. params[10] = effect->u.periodic.period >> 8;
  876. params[11] = effect->u.periodic.period & 255;
  877. params[12] = effect->u.periodic.phase >> 8;
  878. params[13] = effect->u.periodic.phase & 255;
  879. params[14] = effect->u.periodic.envelope.attack_level >> 7;
  880. params[15] = effect->u.periodic.envelope.attack_length >> 8;
  881. params[16] = effect->u.periodic.envelope.attack_length & 255;
  882. params[17] = effect->u.periodic.envelope.fade_level >> 7;
  883. params[18] = effect->u.periodic.envelope.fade_length >> 8;
  884. params[19] = effect->u.periodic.envelope.fade_length & 255;
  885. size = 20;
  886. dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
  887. effect->u.periodic.magnitude, effect->direction,
  888. effect->u.periodic.offset,
  889. effect->u.periodic.period,
  890. effect->u.periodic.phase);
  891. dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
  892. effect->u.periodic.envelope.attack_level,
  893. effect->u.periodic.envelope.attack_length,
  894. effect->u.periodic.envelope.fade_level,
  895. effect->u.periodic.envelope.fade_length);
  896. break;
  897. }
  898. case FF_RAMP:
  899. params[1] = HIDPP_FF_EFFECT_RAMP;
  900. force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  901. params[6] = force >> 8;
  902. params[7] = force & 255;
  903. force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  904. params[8] = force >> 8;
  905. params[9] = force & 255;
  906. params[10] = effect->u.ramp.envelope.attack_level >> 7;
  907. params[11] = effect->u.ramp.envelope.attack_length >> 8;
  908. params[12] = effect->u.ramp.envelope.attack_length & 255;
  909. params[13] = effect->u.ramp.envelope.fade_level >> 7;
  910. params[14] = effect->u.ramp.envelope.fade_length >> 8;
  911. params[15] = effect->u.ramp.envelope.fade_length & 255;
  912. size = 16;
  913. dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
  914. effect->u.ramp.start_level,
  915. effect->u.ramp.end_level,
  916. effect->direction, force);
  917. dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
  918. effect->u.ramp.envelope.attack_level,
  919. effect->u.ramp.envelope.attack_length,
  920. effect->u.ramp.envelope.fade_level,
  921. effect->u.ramp.envelope.fade_length);
  922. break;
  923. case FF_FRICTION:
  924. case FF_INERTIA:
  925. case FF_SPRING:
  926. case FF_DAMPER:
  927. params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
  928. params[6] = effect->u.condition[0].left_saturation >> 9;
  929. params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
  930. params[8] = effect->u.condition[0].left_coeff >> 8;
  931. params[9] = effect->u.condition[0].left_coeff & 255;
  932. params[10] = effect->u.condition[0].deadband >> 9;
  933. params[11] = (effect->u.condition[0].deadband >> 1) & 255;
  934. params[12] = effect->u.condition[0].center >> 8;
  935. params[13] = effect->u.condition[0].center & 255;
  936. params[14] = effect->u.condition[0].right_coeff >> 8;
  937. params[15] = effect->u.condition[0].right_coeff & 255;
  938. params[16] = effect->u.condition[0].right_saturation >> 9;
  939. params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
  940. size = 18;
  941. dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
  942. HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
  943. effect->u.condition[0].left_coeff,
  944. effect->u.condition[0].left_saturation,
  945. effect->u.condition[0].right_coeff,
  946. effect->u.condition[0].right_saturation);
  947. dbg_hid(" deadband=%d, center=%d\n",
  948. effect->u.condition[0].deadband,
  949. effect->u.condition[0].center);
  950. break;
  951. default:
  952. hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
  953. return -EINVAL;
  954. }
  955. return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
  956. }
  957. static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
  958. {
  959. struct hidpp_ff_private_data *data = dev->ff->private;
  960. u8 params[2];
  961. params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
  962. dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
  963. return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
  964. }
  965. static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
  966. {
  967. struct hidpp_ff_private_data *data = dev->ff->private;
  968. u8 slot = 0;
  969. dbg_hid("Erasing effect %d.\n", effect_id);
  970. return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
  971. }
  972. static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
  973. {
  974. struct hidpp_ff_private_data *data = dev->ff->private;
  975. u8 params[18];
  976. dbg_hid("Setting autocenter to %d.\n", magnitude);
  977. /* start a standard spring effect */
  978. params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
  979. /* zero delay and duration */
  980. params[2] = params[3] = params[4] = params[5] = 0;
  981. /* set coeff to 25% of saturation */
  982. params[8] = params[14] = magnitude >> 11;
  983. params[9] = params[15] = (magnitude >> 3) & 255;
  984. params[6] = params[16] = magnitude >> 9;
  985. params[7] = params[17] = (magnitude >> 1) & 255;
  986. /* zero deadband and center */
  987. params[10] = params[11] = params[12] = params[13] = 0;
  988. hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
  989. }
  990. static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
  991. {
  992. struct hidpp_ff_private_data *data = dev->ff->private;
  993. u8 params[4];
  994. dbg_hid("Setting gain to %d.\n", gain);
  995. params[0] = gain >> 8;
  996. params[1] = gain & 255;
  997. params[2] = 0; /* no boost */
  998. params[3] = 0;
  999. hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
  1000. }
  1001. static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
  1002. {
  1003. struct hid_device *hid = to_hid_device(dev);
  1004. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  1005. struct input_dev *idev = hidinput->input;
  1006. struct hidpp_ff_private_data *data = idev->ff->private;
  1007. return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
  1008. }
  1009. static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1010. {
  1011. struct hid_device *hid = to_hid_device(dev);
  1012. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  1013. struct input_dev *idev = hidinput->input;
  1014. struct hidpp_ff_private_data *data = idev->ff->private;
  1015. u8 params[2];
  1016. int range = simple_strtoul(buf, NULL, 10);
  1017. range = clamp(range, 180, 900);
  1018. params[0] = range >> 8;
  1019. params[1] = range & 0x00FF;
  1020. hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
  1021. return count;
  1022. }
  1023. static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
  1024. static void hidpp_ff_destroy(struct ff_device *ff)
  1025. {
  1026. struct hidpp_ff_private_data *data = ff->private;
  1027. kfree(data->effect_ids);
  1028. }
  1029. static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
  1030. {
  1031. struct hid_device *hid = hidpp->hid_dev;
  1032. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  1033. struct input_dev *dev = hidinput->input;
  1034. const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
  1035. const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
  1036. struct ff_device *ff;
  1037. struct hidpp_report response;
  1038. struct hidpp_ff_private_data *data;
  1039. int error, j, num_slots;
  1040. u8 version;
  1041. if (!dev) {
  1042. hid_err(hid, "Struct input_dev not set!\n");
  1043. return -EINVAL;
  1044. }
  1045. /* Get firmware release */
  1046. version = bcdDevice & 255;
  1047. /* Set supported force feedback capabilities */
  1048. for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
  1049. set_bit(hiddpp_ff_effects[j], dev->ffbit);
  1050. if (version > 1)
  1051. for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
  1052. set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
  1053. /* Read number of slots available in device */
  1054. error = hidpp_send_fap_command_sync(hidpp, feature_index,
  1055. HIDPP_FF_GET_INFO, NULL, 0, &response);
  1056. if (error) {
  1057. if (error < 0)
  1058. return error;
  1059. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1060. __func__, error);
  1061. return -EPROTO;
  1062. }
  1063. num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
  1064. error = input_ff_create(dev, num_slots);
  1065. if (error) {
  1066. hid_err(dev, "Failed to create FF device!\n");
  1067. return error;
  1068. }
  1069. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1070. if (!data)
  1071. return -ENOMEM;
  1072. data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
  1073. if (!data->effect_ids) {
  1074. kfree(data);
  1075. return -ENOMEM;
  1076. }
  1077. data->hidpp = hidpp;
  1078. data->feature_index = feature_index;
  1079. data->version = version;
  1080. data->slot_autocenter = 0;
  1081. data->num_effects = num_slots;
  1082. for (j = 0; j < num_slots; j++)
  1083. data->effect_ids[j] = -1;
  1084. ff = dev->ff;
  1085. ff->private = data;
  1086. ff->upload = hidpp_ff_upload_effect;
  1087. ff->erase = hidpp_ff_erase_effect;
  1088. ff->playback = hidpp_ff_playback;
  1089. ff->set_gain = hidpp_ff_set_gain;
  1090. ff->set_autocenter = hidpp_ff_set_autocenter;
  1091. ff->destroy = hidpp_ff_destroy;
  1092. /* reset all forces */
  1093. error = hidpp_send_fap_command_sync(hidpp, feature_index,
  1094. HIDPP_FF_RESET_ALL, NULL, 0, &response);
  1095. /* Read current Range */
  1096. error = hidpp_send_fap_command_sync(hidpp, feature_index,
  1097. HIDPP_FF_GET_APERTURE, NULL, 0, &response);
  1098. if (error)
  1099. hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
  1100. data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
  1101. /* Create sysfs interface */
  1102. error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
  1103. if (error)
  1104. hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
  1105. /* Read the current gain values */
  1106. error = hidpp_send_fap_command_sync(hidpp, feature_index,
  1107. HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
  1108. if (error)
  1109. hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
  1110. data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
  1111. /* ignore boost value at response.fap.params[2] */
  1112. /* init the hardware command queue */
  1113. data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
  1114. atomic_set(&data->workqueue_size, 0);
  1115. /* initialize with zero autocenter to get wheel in usable state */
  1116. hidpp_ff_set_autocenter(dev, 0);
  1117. hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version);
  1118. return 0;
  1119. }
  1120. static int hidpp_ff_deinit(struct hid_device *hid)
  1121. {
  1122. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  1123. struct input_dev *dev = hidinput->input;
  1124. struct hidpp_ff_private_data *data;
  1125. if (!dev) {
  1126. hid_err(hid, "Struct input_dev not found!\n");
  1127. return -EINVAL;
  1128. }
  1129. hid_info(hid, "Unloading HID++ force feedback.\n");
  1130. data = dev->ff->private;
  1131. if (!data) {
  1132. hid_err(hid, "Private data not found!\n");
  1133. return -EINVAL;
  1134. }
  1135. destroy_workqueue(data->wq);
  1136. device_remove_file(&hid->dev, &dev_attr_range);
  1137. return 0;
  1138. }
  1139. /* ************************************************************************** */
  1140. /* */
  1141. /* Device Support */
  1142. /* */
  1143. /* ************************************************************************** */
  1144. /* -------------------------------------------------------------------------- */
  1145. /* Touchpad HID++ devices */
  1146. /* -------------------------------------------------------------------------- */
  1147. #define WTP_MANUAL_RESOLUTION 39
  1148. struct wtp_data {
  1149. struct input_dev *input;
  1150. u16 x_size, y_size;
  1151. u8 finger_count;
  1152. u8 mt_feature_index;
  1153. u8 button_feature_index;
  1154. u8 maxcontacts;
  1155. bool flip_y;
  1156. unsigned int resolution;
  1157. };
  1158. static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  1159. struct hid_field *field, struct hid_usage *usage,
  1160. unsigned long **bit, int *max)
  1161. {
  1162. return -1;
  1163. }
  1164. static void wtp_populate_input(struct hidpp_device *hidpp,
  1165. struct input_dev *input_dev, bool origin_is_hid_core)
  1166. {
  1167. struct wtp_data *wd = hidpp->private_data;
  1168. __set_bit(EV_ABS, input_dev->evbit);
  1169. __set_bit(EV_KEY, input_dev->evbit);
  1170. __clear_bit(EV_REL, input_dev->evbit);
  1171. __clear_bit(EV_LED, input_dev->evbit);
  1172. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
  1173. input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
  1174. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
  1175. input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
  1176. /* Max pressure is not given by the devices, pick one */
  1177. input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
  1178. input_set_capability(input_dev, EV_KEY, BTN_LEFT);
  1179. if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
  1180. input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
  1181. else
  1182. __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
  1183. input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
  1184. INPUT_MT_DROP_UNUSED);
  1185. wd->input = input_dev;
  1186. }
  1187. static void wtp_touch_event(struct wtp_data *wd,
  1188. struct hidpp_touchpad_raw_xy_finger *touch_report)
  1189. {
  1190. int slot;
  1191. if (!touch_report->finger_id || touch_report->contact_type)
  1192. /* no actual data */
  1193. return;
  1194. slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
  1195. input_mt_slot(wd->input, slot);
  1196. input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
  1197. touch_report->contact_status);
  1198. if (touch_report->contact_status) {
  1199. input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
  1200. touch_report->x);
  1201. input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
  1202. wd->flip_y ? wd->y_size - touch_report->y :
  1203. touch_report->y);
  1204. input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
  1205. touch_report->area);
  1206. }
  1207. }
  1208. static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
  1209. struct hidpp_touchpad_raw_xy *raw)
  1210. {
  1211. struct wtp_data *wd = hidpp->private_data;
  1212. int i;
  1213. for (i = 0; i < 2; i++)
  1214. wtp_touch_event(wd, &(raw->fingers[i]));
  1215. if (raw->end_of_frame &&
  1216. !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
  1217. input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
  1218. if (raw->end_of_frame || raw->finger_count <= 2) {
  1219. input_mt_sync_frame(wd->input);
  1220. input_sync(wd->input);
  1221. }
  1222. }
  1223. static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
  1224. {
  1225. struct wtp_data *wd = hidpp->private_data;
  1226. u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
  1227. (data[7] >> 4) * (data[7] >> 4)) / 2;
  1228. u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
  1229. (data[13] >> 4) * (data[13] >> 4)) / 2;
  1230. struct hidpp_touchpad_raw_xy raw = {
  1231. .timestamp = data[1],
  1232. .fingers = {
  1233. {
  1234. .contact_type = 0,
  1235. .contact_status = !!data[7],
  1236. .x = get_unaligned_le16(&data[3]),
  1237. .y = get_unaligned_le16(&data[5]),
  1238. .z = c1_area,
  1239. .area = c1_area,
  1240. .finger_id = data[2],
  1241. }, {
  1242. .contact_type = 0,
  1243. .contact_status = !!data[13],
  1244. .x = get_unaligned_le16(&data[9]),
  1245. .y = get_unaligned_le16(&data[11]),
  1246. .z = c2_area,
  1247. .area = c2_area,
  1248. .finger_id = data[8],
  1249. }
  1250. },
  1251. .finger_count = wd->maxcontacts,
  1252. .spurious_flag = 0,
  1253. .end_of_frame = (data[0] >> 7) == 0,
  1254. .button = data[0] & 0x01,
  1255. };
  1256. wtp_send_raw_xy_event(hidpp, &raw);
  1257. return 1;
  1258. }
  1259. static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
  1260. {
  1261. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1262. struct wtp_data *wd = hidpp->private_data;
  1263. struct hidpp_report *report = (struct hidpp_report *)data;
  1264. struct hidpp_touchpad_raw_xy raw;
  1265. if (!wd || !wd->input)
  1266. return 1;
  1267. switch (data[0]) {
  1268. case 0x02:
  1269. if (size < 2) {
  1270. hid_err(hdev, "Received HID report of bad size (%d)",
  1271. size);
  1272. return 1;
  1273. }
  1274. if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
  1275. input_event(wd->input, EV_KEY, BTN_LEFT,
  1276. !!(data[1] & 0x01));
  1277. input_event(wd->input, EV_KEY, BTN_RIGHT,
  1278. !!(data[1] & 0x02));
  1279. input_sync(wd->input);
  1280. return 0;
  1281. } else {
  1282. if (size < 21)
  1283. return 1;
  1284. return wtp_mouse_raw_xy_event(hidpp, &data[7]);
  1285. }
  1286. case REPORT_ID_HIDPP_LONG:
  1287. /* size is already checked in hidpp_raw_event. */
  1288. if ((report->fap.feature_index != wd->mt_feature_index) ||
  1289. (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
  1290. return 1;
  1291. hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
  1292. wtp_send_raw_xy_event(hidpp, &raw);
  1293. return 0;
  1294. }
  1295. return 0;
  1296. }
  1297. static int wtp_get_config(struct hidpp_device *hidpp)
  1298. {
  1299. struct wtp_data *wd = hidpp->private_data;
  1300. struct hidpp_touchpad_raw_info raw_info = {0};
  1301. u8 feature_type;
  1302. int ret;
  1303. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
  1304. &wd->mt_feature_index, &feature_type);
  1305. if (ret)
  1306. /* means that the device is not powered up */
  1307. return ret;
  1308. ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
  1309. &raw_info);
  1310. if (ret)
  1311. return ret;
  1312. wd->x_size = raw_info.x_size;
  1313. wd->y_size = raw_info.y_size;
  1314. wd->maxcontacts = raw_info.maxcontacts;
  1315. wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
  1316. wd->resolution = raw_info.res;
  1317. if (!wd->resolution)
  1318. wd->resolution = WTP_MANUAL_RESOLUTION;
  1319. return 0;
  1320. }
  1321. static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
  1322. {
  1323. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1324. struct wtp_data *wd;
  1325. wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
  1326. GFP_KERNEL);
  1327. if (!wd)
  1328. return -ENOMEM;
  1329. hidpp->private_data = wd;
  1330. return 0;
  1331. };
  1332. static int wtp_connect(struct hid_device *hdev, bool connected)
  1333. {
  1334. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1335. struct wtp_data *wd = hidpp->private_data;
  1336. int ret;
  1337. if (!connected)
  1338. return 0;
  1339. if (!wd->x_size) {
  1340. ret = wtp_get_config(hidpp);
  1341. if (ret) {
  1342. hid_err(hdev, "Can not get wtp config: %d\n", ret);
  1343. return ret;
  1344. }
  1345. }
  1346. return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
  1347. true, true);
  1348. }
  1349. /* ------------------------------------------------------------------------- */
  1350. /* Logitech M560 devices */
  1351. /* ------------------------------------------------------------------------- */
  1352. /*
  1353. * Logitech M560 protocol overview
  1354. *
  1355. * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
  1356. * the sides buttons are pressed, it sends some keyboard keys events
  1357. * instead of buttons ones.
  1358. * To complicate things further, the middle button keys sequence
  1359. * is different from the odd press and the even press.
  1360. *
  1361. * forward button -> Super_R
  1362. * backward button -> Super_L+'d' (press only)
  1363. * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
  1364. * 2nd time: left-click (press only)
  1365. * NB: press-only means that when the button is pressed, the
  1366. * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
  1367. * together sequentially; instead when the button is released, no event is
  1368. * generated !
  1369. *
  1370. * With the command
  1371. * 10<xx>0a 3500af03 (where <xx> is the mouse id),
  1372. * the mouse reacts differently:
  1373. * - it never sends a keyboard key event
  1374. * - for the three mouse button it sends:
  1375. * middle button press 11<xx>0a 3500af00...
  1376. * side 1 button (forward) press 11<xx>0a 3500b000...
  1377. * side 2 button (backward) press 11<xx>0a 3500ae00...
  1378. * middle/side1/side2 button release 11<xx>0a 35000000...
  1379. */
  1380. static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
  1381. struct m560_private_data {
  1382. struct input_dev *input;
  1383. };
  1384. /* how buttons are mapped in the report */
  1385. #define M560_MOUSE_BTN_LEFT 0x01
  1386. #define M560_MOUSE_BTN_RIGHT 0x02
  1387. #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
  1388. #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
  1389. #define M560_SUB_ID 0x0a
  1390. #define M560_BUTTON_MODE_REGISTER 0x35
  1391. static int m560_send_config_command(struct hid_device *hdev, bool connected)
  1392. {
  1393. struct hidpp_report response;
  1394. struct hidpp_device *hidpp_dev;
  1395. hidpp_dev = hid_get_drvdata(hdev);
  1396. if (!connected)
  1397. return -ENODEV;
  1398. return hidpp_send_rap_command_sync(
  1399. hidpp_dev,
  1400. REPORT_ID_HIDPP_SHORT,
  1401. M560_SUB_ID,
  1402. M560_BUTTON_MODE_REGISTER,
  1403. (u8 *)m560_config_parameter,
  1404. sizeof(m560_config_parameter),
  1405. &response
  1406. );
  1407. }
  1408. static int m560_allocate(struct hid_device *hdev)
  1409. {
  1410. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1411. struct m560_private_data *d;
  1412. d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
  1413. GFP_KERNEL);
  1414. if (!d)
  1415. return -ENOMEM;
  1416. hidpp->private_data = d;
  1417. return 0;
  1418. };
  1419. static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
  1420. {
  1421. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1422. struct m560_private_data *mydata = hidpp->private_data;
  1423. /* sanity check */
  1424. if (!mydata || !mydata->input) {
  1425. hid_err(hdev, "error in parameter\n");
  1426. return -EINVAL;
  1427. }
  1428. if (size < 7) {
  1429. hid_err(hdev, "error in report\n");
  1430. return 0;
  1431. }
  1432. if (data[0] == REPORT_ID_HIDPP_LONG &&
  1433. data[2] == M560_SUB_ID && data[6] == 0x00) {
  1434. /*
  1435. * m560 mouse report for middle, forward and backward button
  1436. *
  1437. * data[0] = 0x11
  1438. * data[1] = device-id
  1439. * data[2] = 0x0a
  1440. * data[5] = 0xaf -> middle
  1441. * 0xb0 -> forward
  1442. * 0xae -> backward
  1443. * 0x00 -> release all
  1444. * data[6] = 0x00
  1445. */
  1446. switch (data[5]) {
  1447. case 0xaf:
  1448. input_report_key(mydata->input, BTN_MIDDLE, 1);
  1449. break;
  1450. case 0xb0:
  1451. input_report_key(mydata->input, BTN_FORWARD, 1);
  1452. break;
  1453. case 0xae:
  1454. input_report_key(mydata->input, BTN_BACK, 1);
  1455. break;
  1456. case 0x00:
  1457. input_report_key(mydata->input, BTN_BACK, 0);
  1458. input_report_key(mydata->input, BTN_FORWARD, 0);
  1459. input_report_key(mydata->input, BTN_MIDDLE, 0);
  1460. break;
  1461. default:
  1462. hid_err(hdev, "error in report\n");
  1463. return 0;
  1464. }
  1465. input_sync(mydata->input);
  1466. } else if (data[0] == 0x02) {
  1467. /*
  1468. * Logitech M560 mouse report
  1469. *
  1470. * data[0] = type (0x02)
  1471. * data[1..2] = buttons
  1472. * data[3..5] = xy
  1473. * data[6] = wheel
  1474. */
  1475. int v;
  1476. input_report_key(mydata->input, BTN_LEFT,
  1477. !!(data[1] & M560_MOUSE_BTN_LEFT));
  1478. input_report_key(mydata->input, BTN_RIGHT,
  1479. !!(data[1] & M560_MOUSE_BTN_RIGHT));
  1480. if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
  1481. input_report_rel(mydata->input, REL_HWHEEL, -1);
  1482. else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
  1483. input_report_rel(mydata->input, REL_HWHEEL, 1);
  1484. v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
  1485. input_report_rel(mydata->input, REL_X, v);
  1486. v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
  1487. input_report_rel(mydata->input, REL_Y, v);
  1488. v = hid_snto32(data[6], 8);
  1489. input_report_rel(mydata->input, REL_WHEEL, v);
  1490. input_sync(mydata->input);
  1491. }
  1492. return 1;
  1493. }
  1494. static void m560_populate_input(struct hidpp_device *hidpp,
  1495. struct input_dev *input_dev, bool origin_is_hid_core)
  1496. {
  1497. struct m560_private_data *mydata = hidpp->private_data;
  1498. mydata->input = input_dev;
  1499. __set_bit(EV_KEY, mydata->input->evbit);
  1500. __set_bit(BTN_MIDDLE, mydata->input->keybit);
  1501. __set_bit(BTN_RIGHT, mydata->input->keybit);
  1502. __set_bit(BTN_LEFT, mydata->input->keybit);
  1503. __set_bit(BTN_BACK, mydata->input->keybit);
  1504. __set_bit(BTN_FORWARD, mydata->input->keybit);
  1505. __set_bit(EV_REL, mydata->input->evbit);
  1506. __set_bit(REL_X, mydata->input->relbit);
  1507. __set_bit(REL_Y, mydata->input->relbit);
  1508. __set_bit(REL_WHEEL, mydata->input->relbit);
  1509. __set_bit(REL_HWHEEL, mydata->input->relbit);
  1510. }
  1511. static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  1512. struct hid_field *field, struct hid_usage *usage,
  1513. unsigned long **bit, int *max)
  1514. {
  1515. return -1;
  1516. }
  1517. /* ------------------------------------------------------------------------- */
  1518. /* Logitech K400 devices */
  1519. /* ------------------------------------------------------------------------- */
  1520. /*
  1521. * The Logitech K400 keyboard has an embedded touchpad which is seen
  1522. * as a mouse from the OS point of view. There is a hardware shortcut to disable
  1523. * tap-to-click but the setting is not remembered accross reset, annoying some
  1524. * users.
  1525. *
  1526. * We can toggle this feature from the host by using the feature 0x6010:
  1527. * Touchpad FW items
  1528. */
  1529. struct k400_private_data {
  1530. u8 feature_index;
  1531. };
  1532. static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
  1533. {
  1534. struct k400_private_data *k400 = hidpp->private_data;
  1535. struct hidpp_touchpad_fw_items items = {};
  1536. int ret;
  1537. u8 feature_type;
  1538. if (!k400->feature_index) {
  1539. ret = hidpp_root_get_feature(hidpp,
  1540. HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
  1541. &k400->feature_index, &feature_type);
  1542. if (ret)
  1543. /* means that the device is not powered up */
  1544. return ret;
  1545. }
  1546. ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
  1547. if (ret)
  1548. return ret;
  1549. return 0;
  1550. }
  1551. static int k400_allocate(struct hid_device *hdev)
  1552. {
  1553. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1554. struct k400_private_data *k400;
  1555. k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
  1556. GFP_KERNEL);
  1557. if (!k400)
  1558. return -ENOMEM;
  1559. hidpp->private_data = k400;
  1560. return 0;
  1561. };
  1562. static int k400_connect(struct hid_device *hdev, bool connected)
  1563. {
  1564. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1565. if (!connected)
  1566. return 0;
  1567. if (!disable_tap_to_click)
  1568. return 0;
  1569. return k400_disable_tap_to_click(hidpp);
  1570. }
  1571. /* ------------------------------------------------------------------------- */
  1572. /* Logitech G920 Driving Force Racing Wheel for Xbox One */
  1573. /* ------------------------------------------------------------------------- */
  1574. #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
  1575. static int g920_get_config(struct hidpp_device *hidpp)
  1576. {
  1577. u8 feature_type;
  1578. u8 feature_index;
  1579. int ret;
  1580. /* Find feature and store for later use */
  1581. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
  1582. &feature_index, &feature_type);
  1583. if (ret)
  1584. return ret;
  1585. ret = hidpp_ff_init(hidpp, feature_index);
  1586. if (ret)
  1587. hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
  1588. ret);
  1589. return 0;
  1590. }
  1591. /* -------------------------------------------------------------------------- */
  1592. /* Generic HID++ devices */
  1593. /* -------------------------------------------------------------------------- */
  1594. static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  1595. struct hid_field *field, struct hid_usage *usage,
  1596. unsigned long **bit, int *max)
  1597. {
  1598. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1599. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  1600. return wtp_input_mapping(hdev, hi, field, usage, bit, max);
  1601. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
  1602. field->application != HID_GD_MOUSE)
  1603. return m560_input_mapping(hdev, hi, field, usage, bit, max);
  1604. return 0;
  1605. }
  1606. static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  1607. struct hid_field *field, struct hid_usage *usage,
  1608. unsigned long **bit, int *max)
  1609. {
  1610. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1611. /* Ensure that Logitech G920 is not given a default fuzz/flat value */
  1612. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  1613. if (usage->type == EV_ABS && (usage->code == ABS_X ||
  1614. usage->code == ABS_Y || usage->code == ABS_Z ||
  1615. usage->code == ABS_RZ)) {
  1616. field->application = HID_GD_MULTIAXIS;
  1617. }
  1618. }
  1619. return 0;
  1620. }
  1621. static void hidpp_populate_input(struct hidpp_device *hidpp,
  1622. struct input_dev *input, bool origin_is_hid_core)
  1623. {
  1624. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  1625. wtp_populate_input(hidpp, input, origin_is_hid_core);
  1626. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
  1627. m560_populate_input(hidpp, input, origin_is_hid_core);
  1628. }
  1629. static int hidpp_input_configured(struct hid_device *hdev,
  1630. struct hid_input *hidinput)
  1631. {
  1632. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1633. struct input_dev *input = hidinput->input;
  1634. hidpp_populate_input(hidpp, input, true);
  1635. return 0;
  1636. }
  1637. static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
  1638. int size)
  1639. {
  1640. struct hidpp_report *question = hidpp->send_receive_buf;
  1641. struct hidpp_report *answer = hidpp->send_receive_buf;
  1642. struct hidpp_report *report = (struct hidpp_report *)data;
  1643. /*
  1644. * If the mutex is locked then we have a pending answer from a
  1645. * previously sent command.
  1646. */
  1647. if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
  1648. /*
  1649. * Check for a correct hidpp20 answer or the corresponding
  1650. * error
  1651. */
  1652. if (hidpp_match_answer(question, report) ||
  1653. hidpp_match_error(question, report)) {
  1654. *answer = *report;
  1655. hidpp->answer_available = true;
  1656. wake_up(&hidpp->wait);
  1657. /*
  1658. * This was an answer to a command that this driver sent
  1659. * We return 1 to hid-core to avoid forwarding the
  1660. * command upstream as it has been treated by the driver
  1661. */
  1662. return 1;
  1663. }
  1664. }
  1665. if (unlikely(hidpp_report_is_connect_event(report))) {
  1666. atomic_set(&hidpp->connected,
  1667. !(report->rap.params[0] & (1 << 6)));
  1668. if ((hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) &&
  1669. (schedule_work(&hidpp->work) == 0))
  1670. dbg_hid("%s: connect event already queued\n", __func__);
  1671. return 1;
  1672. }
  1673. return 0;
  1674. }
  1675. static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
  1676. u8 *data, int size)
  1677. {
  1678. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1679. int ret = 0;
  1680. /* Generic HID++ processing. */
  1681. switch (data[0]) {
  1682. case REPORT_ID_HIDPP_VERY_LONG:
  1683. if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
  1684. hid_err(hdev, "received hid++ report of bad size (%d)",
  1685. size);
  1686. return 1;
  1687. }
  1688. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  1689. break;
  1690. case REPORT_ID_HIDPP_LONG:
  1691. if (size != HIDPP_REPORT_LONG_LENGTH) {
  1692. hid_err(hdev, "received hid++ report of bad size (%d)",
  1693. size);
  1694. return 1;
  1695. }
  1696. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  1697. break;
  1698. case REPORT_ID_HIDPP_SHORT:
  1699. if (size != HIDPP_REPORT_SHORT_LENGTH) {
  1700. hid_err(hdev, "received hid++ report of bad size (%d)",
  1701. size);
  1702. return 1;
  1703. }
  1704. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  1705. break;
  1706. }
  1707. /* If no report is available for further processing, skip calling
  1708. * raw_event of subclasses. */
  1709. if (ret != 0)
  1710. return ret;
  1711. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  1712. return wtp_raw_event(hdev, data, size);
  1713. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
  1714. return m560_raw_event(hdev, data, size);
  1715. return 0;
  1716. }
  1717. static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
  1718. {
  1719. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1720. char *name;
  1721. if (use_unifying)
  1722. /*
  1723. * the device is connected through an Unifying receiver, and
  1724. * might not be already connected.
  1725. * Ask the receiver for its name.
  1726. */
  1727. name = hidpp_get_unifying_name(hidpp);
  1728. else
  1729. name = hidpp_get_device_name(hidpp);
  1730. if (!name) {
  1731. hid_err(hdev, "unable to retrieve the name of the device");
  1732. } else {
  1733. dbg_hid("HID++: Got name: %s\n", name);
  1734. snprintf(hdev->name, sizeof(hdev->name), "%s", name);
  1735. }
  1736. kfree(name);
  1737. }
  1738. static int hidpp_input_open(struct input_dev *dev)
  1739. {
  1740. struct hid_device *hid = input_get_drvdata(dev);
  1741. return hid_hw_open(hid);
  1742. }
  1743. static void hidpp_input_close(struct input_dev *dev)
  1744. {
  1745. struct hid_device *hid = input_get_drvdata(dev);
  1746. hid_hw_close(hid);
  1747. }
  1748. static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
  1749. {
  1750. struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
  1751. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1752. if (!input_dev)
  1753. return NULL;
  1754. input_set_drvdata(input_dev, hdev);
  1755. input_dev->open = hidpp_input_open;
  1756. input_dev->close = hidpp_input_close;
  1757. input_dev->name = hidpp->name;
  1758. input_dev->phys = hdev->phys;
  1759. input_dev->uniq = hdev->uniq;
  1760. input_dev->id.bustype = hdev->bus;
  1761. input_dev->id.vendor = hdev->vendor;
  1762. input_dev->id.product = hdev->product;
  1763. input_dev->id.version = hdev->version;
  1764. input_dev->dev.parent = &hdev->dev;
  1765. return input_dev;
  1766. }
  1767. static void hidpp_connect_event(struct hidpp_device *hidpp)
  1768. {
  1769. struct hid_device *hdev = hidpp->hid_dev;
  1770. int ret = 0;
  1771. bool connected = atomic_read(&hidpp->connected);
  1772. struct input_dev *input;
  1773. char *name, *devm_name;
  1774. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  1775. ret = wtp_connect(hdev, connected);
  1776. if (ret)
  1777. return;
  1778. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
  1779. ret = m560_send_config_command(hdev, connected);
  1780. if (ret)
  1781. return;
  1782. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
  1783. ret = k400_connect(hdev, connected);
  1784. if (ret)
  1785. return;
  1786. }
  1787. if (!connected || hidpp->delayed_input)
  1788. return;
  1789. /* the device is already connected, we can ask for its name and
  1790. * protocol */
  1791. if (!hidpp->protocol_major) {
  1792. ret = !hidpp_is_connected(hidpp);
  1793. if (ret) {
  1794. hid_err(hdev, "Can not get the protocol version.\n");
  1795. return;
  1796. }
  1797. hid_info(hdev, "HID++ %u.%u device connected.\n",
  1798. hidpp->protocol_major, hidpp->protocol_minor);
  1799. }
  1800. if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT))
  1801. /* if HID created the input nodes for us, we can stop now */
  1802. return;
  1803. if (!hidpp->name || hidpp->name == hdev->name) {
  1804. name = hidpp_get_device_name(hidpp);
  1805. if (!name) {
  1806. hid_err(hdev,
  1807. "unable to retrieve the name of the device");
  1808. return;
  1809. }
  1810. devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
  1811. kfree(name);
  1812. if (!devm_name)
  1813. return;
  1814. hidpp->name = devm_name;
  1815. }
  1816. input = hidpp_allocate_input(hdev);
  1817. if (!input) {
  1818. hid_err(hdev, "cannot allocate new input device: %d\n", ret);
  1819. return;
  1820. }
  1821. hidpp_populate_input(hidpp, input, false);
  1822. ret = input_register_device(input);
  1823. if (ret)
  1824. input_free_device(input);
  1825. hidpp->delayed_input = input;
  1826. }
  1827. static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
  1828. {
  1829. struct hidpp_device *hidpp;
  1830. int ret;
  1831. bool connected;
  1832. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  1833. hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
  1834. GFP_KERNEL);
  1835. if (!hidpp)
  1836. return -ENOMEM;
  1837. hidpp->hid_dev = hdev;
  1838. hidpp->name = hdev->name;
  1839. hid_set_drvdata(hdev, hidpp);
  1840. hidpp->quirks = id->driver_data;
  1841. if (disable_raw_mode) {
  1842. hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
  1843. hidpp->quirks &= ~HIDPP_QUIRK_CONNECT_EVENTS;
  1844. hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
  1845. }
  1846. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  1847. ret = wtp_allocate(hdev, id);
  1848. if (ret)
  1849. goto allocate_fail;
  1850. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
  1851. ret = m560_allocate(hdev);
  1852. if (ret)
  1853. goto allocate_fail;
  1854. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
  1855. ret = k400_allocate(hdev);
  1856. if (ret)
  1857. goto allocate_fail;
  1858. }
  1859. INIT_WORK(&hidpp->work, delayed_work_cb);
  1860. mutex_init(&hidpp->send_mutex);
  1861. init_waitqueue_head(&hidpp->wait);
  1862. ret = hid_parse(hdev);
  1863. if (ret) {
  1864. hid_err(hdev, "%s:parse failed\n", __func__);
  1865. goto hid_parse_fail;
  1866. }
  1867. if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
  1868. connect_mask &= ~HID_CONNECT_HIDINPUT;
  1869. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  1870. ret = hid_hw_start(hdev, connect_mask);
  1871. if (ret) {
  1872. hid_err(hdev, "hw start failed\n");
  1873. goto hid_hw_start_fail;
  1874. }
  1875. ret = hid_hw_open(hdev);
  1876. if (ret < 0) {
  1877. dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
  1878. __func__, ret);
  1879. hid_hw_stop(hdev);
  1880. goto hid_hw_start_fail;
  1881. }
  1882. }
  1883. /* Allow incoming packets */
  1884. hid_device_io_start(hdev);
  1885. connected = hidpp_is_connected(hidpp);
  1886. if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
  1887. if (!connected) {
  1888. ret = -ENODEV;
  1889. hid_err(hdev, "Device not connected");
  1890. goto hid_hw_open_failed;
  1891. }
  1892. hid_info(hdev, "HID++ %u.%u device connected.\n",
  1893. hidpp->protocol_major, hidpp->protocol_minor);
  1894. }
  1895. hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
  1896. atomic_set(&hidpp->connected, connected);
  1897. if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
  1898. ret = wtp_get_config(hidpp);
  1899. if (ret)
  1900. goto hid_hw_open_failed;
  1901. } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
  1902. ret = g920_get_config(hidpp);
  1903. if (ret)
  1904. goto hid_hw_open_failed;
  1905. }
  1906. /* Block incoming packets */
  1907. hid_device_io_stop(hdev);
  1908. if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
  1909. ret = hid_hw_start(hdev, connect_mask);
  1910. if (ret) {
  1911. hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
  1912. goto hid_hw_start_fail;
  1913. }
  1914. }
  1915. if (hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) {
  1916. /* Allow incoming packets */
  1917. hid_device_io_start(hdev);
  1918. hidpp_connect_event(hidpp);
  1919. }
  1920. return ret;
  1921. hid_hw_open_failed:
  1922. hid_device_io_stop(hdev);
  1923. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  1924. hid_hw_close(hdev);
  1925. hid_hw_stop(hdev);
  1926. }
  1927. hid_hw_start_fail:
  1928. hid_parse_fail:
  1929. cancel_work_sync(&hidpp->work);
  1930. mutex_destroy(&hidpp->send_mutex);
  1931. allocate_fail:
  1932. hid_set_drvdata(hdev, NULL);
  1933. return ret;
  1934. }
  1935. static void hidpp_remove(struct hid_device *hdev)
  1936. {
  1937. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1938. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  1939. hidpp_ff_deinit(hdev);
  1940. hid_hw_close(hdev);
  1941. }
  1942. hid_hw_stop(hdev);
  1943. cancel_work_sync(&hidpp->work);
  1944. mutex_destroy(&hidpp->send_mutex);
  1945. }
  1946. static const struct hid_device_id hidpp_devices[] = {
  1947. { /* wireless touchpad */
  1948. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  1949. USB_VENDOR_ID_LOGITECH, 0x4011),
  1950. .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
  1951. HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
  1952. { /* wireless touchpad T650 */
  1953. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  1954. USB_VENDOR_ID_LOGITECH, 0x4101),
  1955. .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
  1956. { /* wireless touchpad T651 */
  1957. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
  1958. USB_DEVICE_ID_LOGITECH_T651),
  1959. .driver_data = HIDPP_QUIRK_CLASS_WTP },
  1960. { /* Mouse logitech M560 */
  1961. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  1962. USB_VENDOR_ID_LOGITECH, 0x402d),
  1963. .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
  1964. { /* Keyboard logitech K400 */
  1965. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  1966. USB_VENDOR_ID_LOGITECH, 0x4024),
  1967. .driver_data = HIDPP_QUIRK_CONNECT_EVENTS | HIDPP_QUIRK_CLASS_K400 },
  1968. { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  1969. USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
  1970. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
  1971. .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
  1972. {}
  1973. };
  1974. MODULE_DEVICE_TABLE(hid, hidpp_devices);
  1975. static struct hid_driver hidpp_driver = {
  1976. .name = "logitech-hidpp-device",
  1977. .id_table = hidpp_devices,
  1978. .probe = hidpp_probe,
  1979. .remove = hidpp_remove,
  1980. .raw_event = hidpp_raw_event,
  1981. .input_configured = hidpp_input_configured,
  1982. .input_mapping = hidpp_input_mapping,
  1983. .input_mapped = hidpp_input_mapped,
  1984. };
  1985. module_hid_driver(hidpp_driver);