hid-logitech-hidpp.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167
  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. #define HIDPP_QUIRK_CLASS_K750 BIT(4)
  51. /* bits 2..20 are reserved for classes */
  52. /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
  53. #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
  54. #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
  55. #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
  56. #define HIDPP_QUIRK_UNIFYING BIT(25)
  57. #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
  58. #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
  59. #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
  60. #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
  61. #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
  62. /*
  63. * There are two hidpp protocols in use, the first version hidpp10 is known
  64. * as register access protocol or RAP, the second version hidpp20 is known as
  65. * feature access protocol or FAP
  66. *
  67. * Most older devices (including the Unifying usb receiver) use the RAP protocol
  68. * where as most newer devices use the FAP protocol. Both protocols are
  69. * compatible with the underlying transport, which could be usb, Unifiying, or
  70. * bluetooth. The message lengths are defined by the hid vendor specific report
  71. * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
  72. * the HIDPP_LONG report type (total message length 20 bytes)
  73. *
  74. * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
  75. * messages. The Unifying receiver itself responds to RAP messages (device index
  76. * is 0xFF for the receiver), and all messages (short or long) with a device
  77. * index between 1 and 6 are passed untouched to the corresponding paired
  78. * Unifying device.
  79. *
  80. * The paired device can be RAP or FAP, it will receive the message untouched
  81. * from the Unifiying receiver.
  82. */
  83. struct fap {
  84. u8 feature_index;
  85. u8 funcindex_clientid;
  86. u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
  87. };
  88. struct rap {
  89. u8 sub_id;
  90. u8 reg_address;
  91. u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
  92. };
  93. struct hidpp_report {
  94. u8 report_id;
  95. u8 device_index;
  96. union {
  97. struct fap fap;
  98. struct rap rap;
  99. u8 rawbytes[sizeof(struct fap)];
  100. };
  101. } __packed;
  102. struct hidpp_battery {
  103. u8 feature_index;
  104. u8 solar_feature_index;
  105. struct power_supply_desc desc;
  106. struct power_supply *ps;
  107. char name[64];
  108. int status;
  109. int capacity;
  110. int level;
  111. bool online;
  112. };
  113. struct hidpp_device {
  114. struct hid_device *hid_dev;
  115. struct mutex send_mutex;
  116. void *send_receive_buf;
  117. char *name; /* will never be NULL and should not be freed */
  118. wait_queue_head_t wait;
  119. bool answer_available;
  120. u8 protocol_major;
  121. u8 protocol_minor;
  122. void *private_data;
  123. struct work_struct work;
  124. struct kfifo delayed_work_fifo;
  125. atomic_t connected;
  126. struct input_dev *delayed_input;
  127. unsigned long quirks;
  128. unsigned long capabilities;
  129. struct hidpp_battery battery;
  130. };
  131. /* HID++ 1.0 error codes */
  132. #define HIDPP_ERROR 0x8f
  133. #define HIDPP_ERROR_SUCCESS 0x00
  134. #define HIDPP_ERROR_INVALID_SUBID 0x01
  135. #define HIDPP_ERROR_INVALID_ADRESS 0x02
  136. #define HIDPP_ERROR_INVALID_VALUE 0x03
  137. #define HIDPP_ERROR_CONNECT_FAIL 0x04
  138. #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
  139. #define HIDPP_ERROR_ALREADY_EXISTS 0x06
  140. #define HIDPP_ERROR_BUSY 0x07
  141. #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
  142. #define HIDPP_ERROR_RESOURCE_ERROR 0x09
  143. #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
  144. #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
  145. #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
  146. /* HID++ 2.0 error codes */
  147. #define HIDPP20_ERROR 0xff
  148. static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
  149. static int __hidpp_send_report(struct hid_device *hdev,
  150. struct hidpp_report *hidpp_report)
  151. {
  152. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  153. int fields_count, ret;
  154. hidpp = hid_get_drvdata(hdev);
  155. switch (hidpp_report->report_id) {
  156. case REPORT_ID_HIDPP_SHORT:
  157. fields_count = HIDPP_REPORT_SHORT_LENGTH;
  158. break;
  159. case REPORT_ID_HIDPP_LONG:
  160. fields_count = HIDPP_REPORT_LONG_LENGTH;
  161. break;
  162. case REPORT_ID_HIDPP_VERY_LONG:
  163. fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
  164. break;
  165. default:
  166. return -ENODEV;
  167. }
  168. /*
  169. * set the device_index as the receiver, it will be overwritten by
  170. * hid_hw_request if needed
  171. */
  172. hidpp_report->device_index = 0xff;
  173. if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
  174. ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
  175. } else {
  176. ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
  177. (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
  178. HID_REQ_SET_REPORT);
  179. }
  180. return ret == fields_count ? 0 : -1;
  181. }
  182. /**
  183. * hidpp_send_message_sync() returns 0 in case of success, and something else
  184. * in case of a failure.
  185. * - If ' something else' is positive, that means that an error has been raised
  186. * by the protocol itself.
  187. * - If ' something else' is negative, that means that we had a classic error
  188. * (-ENOMEM, -EPIPE, etc...)
  189. */
  190. static int hidpp_send_message_sync(struct hidpp_device *hidpp,
  191. struct hidpp_report *message,
  192. struct hidpp_report *response)
  193. {
  194. int ret;
  195. mutex_lock(&hidpp->send_mutex);
  196. hidpp->send_receive_buf = response;
  197. hidpp->answer_available = false;
  198. /*
  199. * So that we can later validate the answer when it arrives
  200. * in hidpp_raw_event
  201. */
  202. *response = *message;
  203. ret = __hidpp_send_report(hidpp->hid_dev, message);
  204. if (ret) {
  205. dbg_hid("__hidpp_send_report returned err: %d\n", ret);
  206. memset(response, 0, sizeof(struct hidpp_report));
  207. goto exit;
  208. }
  209. if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
  210. 5*HZ)) {
  211. dbg_hid("%s:timeout waiting for response\n", __func__);
  212. memset(response, 0, sizeof(struct hidpp_report));
  213. ret = -ETIMEDOUT;
  214. }
  215. if (response->report_id == REPORT_ID_HIDPP_SHORT &&
  216. response->rap.sub_id == HIDPP_ERROR) {
  217. ret = response->rap.params[1];
  218. dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
  219. goto exit;
  220. }
  221. if ((response->report_id == REPORT_ID_HIDPP_LONG ||
  222. response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
  223. response->fap.feature_index == HIDPP20_ERROR) {
  224. ret = response->fap.params[1];
  225. dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
  226. goto exit;
  227. }
  228. exit:
  229. mutex_unlock(&hidpp->send_mutex);
  230. return ret;
  231. }
  232. static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
  233. u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
  234. struct hidpp_report *response)
  235. {
  236. struct hidpp_report *message;
  237. int ret;
  238. if (param_count > sizeof(message->fap.params))
  239. return -EINVAL;
  240. message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
  241. if (!message)
  242. return -ENOMEM;
  243. if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
  244. message->report_id = REPORT_ID_HIDPP_VERY_LONG;
  245. else
  246. message->report_id = REPORT_ID_HIDPP_LONG;
  247. message->fap.feature_index = feat_index;
  248. message->fap.funcindex_clientid = funcindex_clientid;
  249. memcpy(&message->fap.params, params, param_count);
  250. ret = hidpp_send_message_sync(hidpp, message, response);
  251. kfree(message);
  252. return ret;
  253. }
  254. static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
  255. u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
  256. struct hidpp_report *response)
  257. {
  258. struct hidpp_report *message;
  259. int ret, max_count;
  260. switch (report_id) {
  261. case REPORT_ID_HIDPP_SHORT:
  262. max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
  263. break;
  264. case REPORT_ID_HIDPP_LONG:
  265. max_count = HIDPP_REPORT_LONG_LENGTH - 4;
  266. break;
  267. case REPORT_ID_HIDPP_VERY_LONG:
  268. max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
  269. break;
  270. default:
  271. return -EINVAL;
  272. }
  273. if (param_count > max_count)
  274. return -EINVAL;
  275. message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
  276. if (!message)
  277. return -ENOMEM;
  278. message->report_id = report_id;
  279. message->rap.sub_id = sub_id;
  280. message->rap.reg_address = reg_address;
  281. memcpy(&message->rap.params, params, param_count);
  282. ret = hidpp_send_message_sync(hidpp_dev, message, response);
  283. kfree(message);
  284. return ret;
  285. }
  286. static void delayed_work_cb(struct work_struct *work)
  287. {
  288. struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
  289. work);
  290. hidpp_connect_event(hidpp);
  291. }
  292. static inline bool hidpp_match_answer(struct hidpp_report *question,
  293. struct hidpp_report *answer)
  294. {
  295. return (answer->fap.feature_index == question->fap.feature_index) &&
  296. (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
  297. }
  298. static inline bool hidpp_match_error(struct hidpp_report *question,
  299. struct hidpp_report *answer)
  300. {
  301. return ((answer->rap.sub_id == HIDPP_ERROR) ||
  302. (answer->fap.feature_index == HIDPP20_ERROR)) &&
  303. (answer->fap.funcindex_clientid == question->fap.feature_index) &&
  304. (answer->fap.params[0] == question->fap.funcindex_clientid);
  305. }
  306. static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
  307. {
  308. return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
  309. (report->rap.sub_id == 0x41);
  310. }
  311. /**
  312. * hidpp_prefix_name() prefixes the current given name with "Logitech ".
  313. */
  314. static void hidpp_prefix_name(char **name, int name_length)
  315. {
  316. #define PREFIX_LENGTH 9 /* "Logitech " */
  317. int new_length;
  318. char *new_name;
  319. if (name_length > PREFIX_LENGTH &&
  320. strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
  321. /* The prefix has is already in the name */
  322. return;
  323. new_length = PREFIX_LENGTH + name_length;
  324. new_name = kzalloc(new_length, GFP_KERNEL);
  325. if (!new_name)
  326. return;
  327. snprintf(new_name, new_length, "Logitech %s", *name);
  328. kfree(*name);
  329. *name = new_name;
  330. }
  331. /* -------------------------------------------------------------------------- */
  332. /* HIDP++ 1.0 commands */
  333. /* -------------------------------------------------------------------------- */
  334. #define HIDPP_SET_REGISTER 0x80
  335. #define HIDPP_GET_REGISTER 0x81
  336. #define HIDPP_SET_LONG_REGISTER 0x82
  337. #define HIDPP_GET_LONG_REGISTER 0x83
  338. #define HIDPP_REG_GENERAL 0x00
  339. static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
  340. {
  341. struct hidpp_report response;
  342. int ret;
  343. u8 params[3] = { 0 };
  344. ret = hidpp_send_rap_command_sync(hidpp_dev,
  345. REPORT_ID_HIDPP_SHORT,
  346. HIDPP_GET_REGISTER,
  347. HIDPP_REG_GENERAL,
  348. NULL, 0, &response);
  349. if (ret)
  350. return ret;
  351. memcpy(params, response.rap.params, 3);
  352. /* Set the battery bit */
  353. params[0] |= BIT(4);
  354. return hidpp_send_rap_command_sync(hidpp_dev,
  355. REPORT_ID_HIDPP_SHORT,
  356. HIDPP_SET_REGISTER,
  357. HIDPP_REG_GENERAL,
  358. params, 3, &response);
  359. }
  360. #define HIDPP_REG_BATTERY_STATUS 0x07
  361. static int hidpp10_battery_status_map_level(u8 param)
  362. {
  363. int level;
  364. switch (param) {
  365. case 1 ... 2:
  366. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  367. break;
  368. case 3 ... 4:
  369. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  370. break;
  371. case 5 ... 6:
  372. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  373. break;
  374. case 7:
  375. level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
  376. break;
  377. default:
  378. level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  379. }
  380. return level;
  381. }
  382. static int hidpp10_battery_status_map_status(u8 param)
  383. {
  384. int status;
  385. switch (param) {
  386. case 0x00:
  387. /* discharging (in use) */
  388. status = POWER_SUPPLY_STATUS_DISCHARGING;
  389. break;
  390. case 0x21: /* (standard) charging */
  391. case 0x24: /* fast charging */
  392. case 0x25: /* slow charging */
  393. status = POWER_SUPPLY_STATUS_CHARGING;
  394. break;
  395. case 0x26: /* topping charge */
  396. case 0x22: /* charge complete */
  397. status = POWER_SUPPLY_STATUS_FULL;
  398. break;
  399. case 0x20: /* unknown */
  400. status = POWER_SUPPLY_STATUS_UNKNOWN;
  401. break;
  402. /*
  403. * 0x01...0x1F = reserved (not charging)
  404. * 0x23 = charging error
  405. * 0x27..0xff = reserved
  406. */
  407. default:
  408. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  409. break;
  410. }
  411. return status;
  412. }
  413. static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
  414. {
  415. struct hidpp_report response;
  416. int ret, status;
  417. ret = hidpp_send_rap_command_sync(hidpp,
  418. REPORT_ID_HIDPP_SHORT,
  419. HIDPP_GET_REGISTER,
  420. HIDPP_REG_BATTERY_STATUS,
  421. NULL, 0, &response);
  422. if (ret)
  423. return ret;
  424. hidpp->battery.level =
  425. hidpp10_battery_status_map_level(response.rap.params[0]);
  426. status = hidpp10_battery_status_map_status(response.rap.params[1]);
  427. hidpp->battery.status = status;
  428. /* the capacity is only available when discharging or full */
  429. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  430. status == POWER_SUPPLY_STATUS_FULL;
  431. return 0;
  432. }
  433. #define HIDPP_REG_BATTERY_MILEAGE 0x0D
  434. static int hidpp10_battery_mileage_map_status(u8 param)
  435. {
  436. int status;
  437. switch (param >> 6) {
  438. case 0x00:
  439. /* discharging (in use) */
  440. status = POWER_SUPPLY_STATUS_DISCHARGING;
  441. break;
  442. case 0x01: /* charging */
  443. status = POWER_SUPPLY_STATUS_CHARGING;
  444. break;
  445. case 0x02: /* charge complete */
  446. status = POWER_SUPPLY_STATUS_FULL;
  447. break;
  448. /*
  449. * 0x03 = charging error
  450. */
  451. default:
  452. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  453. break;
  454. }
  455. return status;
  456. }
  457. static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
  458. {
  459. struct hidpp_report response;
  460. int ret, status;
  461. ret = hidpp_send_rap_command_sync(hidpp,
  462. REPORT_ID_HIDPP_SHORT,
  463. HIDPP_GET_REGISTER,
  464. HIDPP_REG_BATTERY_MILEAGE,
  465. NULL, 0, &response);
  466. if (ret)
  467. return ret;
  468. hidpp->battery.capacity = response.rap.params[0];
  469. status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
  470. hidpp->battery.status = status;
  471. /* the capacity is only available when discharging or full */
  472. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  473. status == POWER_SUPPLY_STATUS_FULL;
  474. return 0;
  475. }
  476. static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
  477. {
  478. struct hidpp_report *report = (struct hidpp_report *)data;
  479. int status, capacity, level;
  480. bool changed;
  481. if (report->report_id != REPORT_ID_HIDPP_SHORT)
  482. return 0;
  483. switch (report->rap.sub_id) {
  484. case HIDPP_REG_BATTERY_STATUS:
  485. capacity = hidpp->battery.capacity;
  486. level = hidpp10_battery_status_map_level(report->rawbytes[1]);
  487. status = hidpp10_battery_status_map_status(report->rawbytes[2]);
  488. break;
  489. case HIDPP_REG_BATTERY_MILEAGE:
  490. capacity = report->rap.params[0];
  491. level = hidpp->battery.level;
  492. status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
  493. break;
  494. default:
  495. return 0;
  496. }
  497. changed = capacity != hidpp->battery.capacity ||
  498. level != hidpp->battery.level ||
  499. status != hidpp->battery.status;
  500. /* the capacity is only available when discharging or full */
  501. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  502. status == POWER_SUPPLY_STATUS_FULL;
  503. if (changed) {
  504. hidpp->battery.level = level;
  505. hidpp->battery.status = status;
  506. if (hidpp->battery.ps)
  507. power_supply_changed(hidpp->battery.ps);
  508. }
  509. return 0;
  510. }
  511. #define HIDPP_REG_PAIRING_INFORMATION 0xB5
  512. #define HIDPP_EXTENDED_PAIRING 0x30
  513. #define HIDPP_DEVICE_NAME 0x40
  514. static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
  515. {
  516. struct hidpp_report response;
  517. int ret;
  518. u8 params[1] = { HIDPP_DEVICE_NAME };
  519. char *name;
  520. int len;
  521. ret = hidpp_send_rap_command_sync(hidpp_dev,
  522. REPORT_ID_HIDPP_SHORT,
  523. HIDPP_GET_LONG_REGISTER,
  524. HIDPP_REG_PAIRING_INFORMATION,
  525. params, 1, &response);
  526. if (ret)
  527. return NULL;
  528. len = response.rap.params[1];
  529. if (2 + len > sizeof(response.rap.params))
  530. return NULL;
  531. name = kzalloc(len + 1, GFP_KERNEL);
  532. if (!name)
  533. return NULL;
  534. memcpy(name, &response.rap.params[2], len);
  535. /* include the terminating '\0' */
  536. hidpp_prefix_name(&name, len + 1);
  537. return name;
  538. }
  539. static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
  540. {
  541. struct hidpp_report response;
  542. int ret;
  543. u8 params[1] = { HIDPP_EXTENDED_PAIRING };
  544. ret = hidpp_send_rap_command_sync(hidpp,
  545. REPORT_ID_HIDPP_SHORT,
  546. HIDPP_GET_LONG_REGISTER,
  547. HIDPP_REG_PAIRING_INFORMATION,
  548. params, 1, &response);
  549. if (ret)
  550. return ret;
  551. /*
  552. * We don't care about LE or BE, we will output it as a string
  553. * with %4phD, so we need to keep the order.
  554. */
  555. *serial = *((u32 *)&response.rap.params[1]);
  556. return 0;
  557. }
  558. static int hidpp_unifying_init(struct hidpp_device *hidpp)
  559. {
  560. struct hid_device *hdev = hidpp->hid_dev;
  561. const char *name;
  562. u32 serial;
  563. int ret;
  564. ret = hidpp_unifying_get_serial(hidpp, &serial);
  565. if (ret)
  566. return ret;
  567. snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
  568. hdev->product, &serial);
  569. dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
  570. name = hidpp_unifying_get_name(hidpp);
  571. if (!name)
  572. return -EIO;
  573. snprintf(hdev->name, sizeof(hdev->name), "%s", name);
  574. dbg_hid("HID++ Unifying: Got name: %s\n", name);
  575. kfree(name);
  576. return 0;
  577. }
  578. /* -------------------------------------------------------------------------- */
  579. /* 0x0000: Root */
  580. /* -------------------------------------------------------------------------- */
  581. #define HIDPP_PAGE_ROOT 0x0000
  582. #define HIDPP_PAGE_ROOT_IDX 0x00
  583. #define CMD_ROOT_GET_FEATURE 0x01
  584. #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
  585. static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
  586. u8 *feature_index, u8 *feature_type)
  587. {
  588. struct hidpp_report response;
  589. int ret;
  590. u8 params[2] = { feature >> 8, feature & 0x00FF };
  591. ret = hidpp_send_fap_command_sync(hidpp,
  592. HIDPP_PAGE_ROOT_IDX,
  593. CMD_ROOT_GET_FEATURE,
  594. params, 2, &response);
  595. if (ret)
  596. return ret;
  597. if (response.fap.params[0] == 0)
  598. return -ENOENT;
  599. *feature_index = response.fap.params[0];
  600. *feature_type = response.fap.params[1];
  601. return ret;
  602. }
  603. static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
  604. {
  605. const u8 ping_byte = 0x5a;
  606. u8 ping_data[3] = { 0, 0, ping_byte };
  607. struct hidpp_report response;
  608. int ret;
  609. ret = hidpp_send_rap_command_sync(hidpp,
  610. REPORT_ID_HIDPP_SHORT,
  611. HIDPP_PAGE_ROOT_IDX,
  612. CMD_ROOT_GET_PROTOCOL_VERSION,
  613. ping_data, sizeof(ping_data), &response);
  614. if (ret == HIDPP_ERROR_INVALID_SUBID) {
  615. hidpp->protocol_major = 1;
  616. hidpp->protocol_minor = 0;
  617. return 0;
  618. }
  619. /* the device might not be connected */
  620. if (ret == HIDPP_ERROR_RESOURCE_ERROR)
  621. return -EIO;
  622. if (ret > 0) {
  623. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  624. __func__, ret);
  625. return -EPROTO;
  626. }
  627. if (ret)
  628. return ret;
  629. if (response.rap.params[2] != ping_byte) {
  630. hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
  631. __func__, response.rap.params[2], ping_byte);
  632. return -EPROTO;
  633. }
  634. hidpp->protocol_major = response.rap.params[0];
  635. hidpp->protocol_minor = response.rap.params[1];
  636. return ret;
  637. }
  638. static bool hidpp_is_connected(struct hidpp_device *hidpp)
  639. {
  640. int ret;
  641. ret = hidpp_root_get_protocol_version(hidpp);
  642. if (!ret)
  643. hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
  644. hidpp->protocol_major, hidpp->protocol_minor);
  645. return ret == 0;
  646. }
  647. /* -------------------------------------------------------------------------- */
  648. /* 0x0005: GetDeviceNameType */
  649. /* -------------------------------------------------------------------------- */
  650. #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
  651. #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
  652. #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
  653. #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
  654. static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
  655. u8 feature_index, u8 *nameLength)
  656. {
  657. struct hidpp_report response;
  658. int ret;
  659. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  660. CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
  661. if (ret > 0) {
  662. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  663. __func__, ret);
  664. return -EPROTO;
  665. }
  666. if (ret)
  667. return ret;
  668. *nameLength = response.fap.params[0];
  669. return ret;
  670. }
  671. static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
  672. u8 feature_index, u8 char_index, char *device_name, int len_buf)
  673. {
  674. struct hidpp_report response;
  675. int ret, i;
  676. int count;
  677. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  678. CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
  679. &response);
  680. if (ret > 0) {
  681. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  682. __func__, ret);
  683. return -EPROTO;
  684. }
  685. if (ret)
  686. return ret;
  687. switch (response.report_id) {
  688. case REPORT_ID_HIDPP_VERY_LONG:
  689. count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
  690. break;
  691. case REPORT_ID_HIDPP_LONG:
  692. count = HIDPP_REPORT_LONG_LENGTH - 4;
  693. break;
  694. case REPORT_ID_HIDPP_SHORT:
  695. count = HIDPP_REPORT_SHORT_LENGTH - 4;
  696. break;
  697. default:
  698. return -EPROTO;
  699. }
  700. if (len_buf < count)
  701. count = len_buf;
  702. for (i = 0; i < count; i++)
  703. device_name[i] = response.fap.params[i];
  704. return count;
  705. }
  706. static char *hidpp_get_device_name(struct hidpp_device *hidpp)
  707. {
  708. u8 feature_type;
  709. u8 feature_index;
  710. u8 __name_length;
  711. char *name;
  712. unsigned index = 0;
  713. int ret;
  714. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
  715. &feature_index, &feature_type);
  716. if (ret)
  717. return NULL;
  718. ret = hidpp_devicenametype_get_count(hidpp, feature_index,
  719. &__name_length);
  720. if (ret)
  721. return NULL;
  722. name = kzalloc(__name_length + 1, GFP_KERNEL);
  723. if (!name)
  724. return NULL;
  725. while (index < __name_length) {
  726. ret = hidpp_devicenametype_get_device_name(hidpp,
  727. feature_index, index, name + index,
  728. __name_length - index);
  729. if (ret <= 0) {
  730. kfree(name);
  731. return NULL;
  732. }
  733. index += ret;
  734. }
  735. /* include the terminating '\0' */
  736. hidpp_prefix_name(&name, __name_length + 1);
  737. return name;
  738. }
  739. /* -------------------------------------------------------------------------- */
  740. /* 0x1000: Battery level status */
  741. /* -------------------------------------------------------------------------- */
  742. #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
  743. #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
  744. #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
  745. #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
  746. #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
  747. #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
  748. #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
  749. static int hidpp_map_battery_level(int capacity)
  750. {
  751. if (capacity < 11)
  752. return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  753. /*
  754. * The spec says this should be < 31 but some devices report 30
  755. * with brand new batteries and Windows reports 30 as "Good".
  756. */
  757. else if (capacity < 30)
  758. return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  759. else if (capacity < 81)
  760. return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  761. return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  762. }
  763. static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
  764. int *next_capacity,
  765. int *level)
  766. {
  767. int status;
  768. *capacity = data[0];
  769. *next_capacity = data[1];
  770. *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  771. /* When discharging, we can rely on the device reported capacity.
  772. * For all other states the device reports 0 (unknown).
  773. */
  774. switch (data[2]) {
  775. case 0: /* discharging (in use) */
  776. status = POWER_SUPPLY_STATUS_DISCHARGING;
  777. *level = hidpp_map_battery_level(*capacity);
  778. break;
  779. case 1: /* recharging */
  780. status = POWER_SUPPLY_STATUS_CHARGING;
  781. break;
  782. case 2: /* charge in final stage */
  783. status = POWER_SUPPLY_STATUS_CHARGING;
  784. break;
  785. case 3: /* charge complete */
  786. status = POWER_SUPPLY_STATUS_FULL;
  787. *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  788. *capacity = 100;
  789. break;
  790. case 4: /* recharging below optimal speed */
  791. status = POWER_SUPPLY_STATUS_CHARGING;
  792. break;
  793. /* 5 = invalid battery type
  794. 6 = thermal error
  795. 7 = other charging error */
  796. default:
  797. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  798. break;
  799. }
  800. return status;
  801. }
  802. static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
  803. u8 feature_index,
  804. int *status,
  805. int *capacity,
  806. int *next_capacity,
  807. int *level)
  808. {
  809. struct hidpp_report response;
  810. int ret;
  811. u8 *params = (u8 *)response.fap.params;
  812. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  813. CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
  814. NULL, 0, &response);
  815. /* Ignore these intermittent errors */
  816. if (ret == HIDPP_ERROR_RESOURCE_ERROR)
  817. return -EIO;
  818. if (ret > 0) {
  819. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  820. __func__, ret);
  821. return -EPROTO;
  822. }
  823. if (ret)
  824. return ret;
  825. *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
  826. next_capacity,
  827. level);
  828. return 0;
  829. }
  830. static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
  831. u8 feature_index)
  832. {
  833. struct hidpp_report response;
  834. int ret;
  835. u8 *params = (u8 *)response.fap.params;
  836. unsigned int level_count, flags;
  837. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  838. CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
  839. NULL, 0, &response);
  840. if (ret > 0) {
  841. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  842. __func__, ret);
  843. return -EPROTO;
  844. }
  845. if (ret)
  846. return ret;
  847. level_count = params[0];
  848. flags = params[1];
  849. if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
  850. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
  851. else
  852. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
  853. return 0;
  854. }
  855. static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
  856. {
  857. u8 feature_type;
  858. int ret;
  859. int status, capacity, next_capacity, level;
  860. if (hidpp->battery.feature_index == 0xff) {
  861. ret = hidpp_root_get_feature(hidpp,
  862. HIDPP_PAGE_BATTERY_LEVEL_STATUS,
  863. &hidpp->battery.feature_index,
  864. &feature_type);
  865. if (ret)
  866. return ret;
  867. }
  868. ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
  869. hidpp->battery.feature_index,
  870. &status, &capacity,
  871. &next_capacity, &level);
  872. if (ret)
  873. return ret;
  874. ret = hidpp20_batterylevel_get_battery_info(hidpp,
  875. hidpp->battery.feature_index);
  876. if (ret)
  877. return ret;
  878. hidpp->battery.status = status;
  879. hidpp->battery.capacity = capacity;
  880. hidpp->battery.level = level;
  881. /* the capacity is only available when discharging or full */
  882. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  883. status == POWER_SUPPLY_STATUS_FULL;
  884. return 0;
  885. }
  886. static int hidpp20_battery_event(struct hidpp_device *hidpp,
  887. u8 *data, int size)
  888. {
  889. struct hidpp_report *report = (struct hidpp_report *)data;
  890. int status, capacity, next_capacity, level;
  891. bool changed;
  892. if (report->fap.feature_index != hidpp->battery.feature_index ||
  893. report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
  894. return 0;
  895. status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
  896. &capacity,
  897. &next_capacity,
  898. &level);
  899. /* the capacity is only available when discharging or full */
  900. hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
  901. status == POWER_SUPPLY_STATUS_FULL;
  902. changed = capacity != hidpp->battery.capacity ||
  903. level != hidpp->battery.level ||
  904. status != hidpp->battery.status;
  905. if (changed) {
  906. hidpp->battery.level = level;
  907. hidpp->battery.capacity = capacity;
  908. hidpp->battery.status = status;
  909. if (hidpp->battery.ps)
  910. power_supply_changed(hidpp->battery.ps);
  911. }
  912. return 0;
  913. }
  914. static enum power_supply_property hidpp_battery_props[] = {
  915. POWER_SUPPLY_PROP_ONLINE,
  916. POWER_SUPPLY_PROP_STATUS,
  917. POWER_SUPPLY_PROP_SCOPE,
  918. POWER_SUPPLY_PROP_MODEL_NAME,
  919. POWER_SUPPLY_PROP_MANUFACTURER,
  920. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  921. 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
  922. 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
  923. };
  924. static int hidpp_battery_get_property(struct power_supply *psy,
  925. enum power_supply_property psp,
  926. union power_supply_propval *val)
  927. {
  928. struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
  929. int ret = 0;
  930. switch(psp) {
  931. case POWER_SUPPLY_PROP_STATUS:
  932. val->intval = hidpp->battery.status;
  933. break;
  934. case POWER_SUPPLY_PROP_CAPACITY:
  935. val->intval = hidpp->battery.capacity;
  936. break;
  937. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  938. val->intval = hidpp->battery.level;
  939. break;
  940. case POWER_SUPPLY_PROP_SCOPE:
  941. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  942. break;
  943. case POWER_SUPPLY_PROP_ONLINE:
  944. val->intval = hidpp->battery.online;
  945. break;
  946. case POWER_SUPPLY_PROP_MODEL_NAME:
  947. if (!strncmp(hidpp->name, "Logitech ", 9))
  948. val->strval = hidpp->name + 9;
  949. else
  950. val->strval = hidpp->name;
  951. break;
  952. case POWER_SUPPLY_PROP_MANUFACTURER:
  953. val->strval = "Logitech";
  954. break;
  955. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  956. val->strval = hidpp->hid_dev->uniq;
  957. break;
  958. default:
  959. ret = -EINVAL;
  960. break;
  961. }
  962. return ret;
  963. }
  964. /* -------------------------------------------------------------------------- */
  965. /* 0x4301: Solar Keyboard */
  966. /* -------------------------------------------------------------------------- */
  967. #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
  968. #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
  969. #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
  970. #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
  971. #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
  972. static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
  973. {
  974. struct hidpp_report response;
  975. u8 params[2] = { 1, 1 };
  976. u8 feature_type;
  977. int ret;
  978. if (hidpp->battery.feature_index == 0xff) {
  979. ret = hidpp_root_get_feature(hidpp,
  980. HIDPP_PAGE_SOLAR_KEYBOARD,
  981. &hidpp->battery.solar_feature_index,
  982. &feature_type);
  983. if (ret)
  984. return ret;
  985. }
  986. ret = hidpp_send_fap_command_sync(hidpp,
  987. hidpp->battery.solar_feature_index,
  988. CMD_SOLAR_SET_LIGHT_MEASURE,
  989. params, 2, &response);
  990. if (ret > 0) {
  991. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  992. __func__, ret);
  993. return -EPROTO;
  994. }
  995. if (ret)
  996. return ret;
  997. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
  998. return 0;
  999. }
  1000. static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
  1001. u8 *data, int size)
  1002. {
  1003. struct hidpp_report *report = (struct hidpp_report *)data;
  1004. int capacity, lux, status;
  1005. u8 function;
  1006. function = report->fap.funcindex_clientid;
  1007. if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
  1008. !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
  1009. function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
  1010. function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
  1011. return 0;
  1012. capacity = report->fap.params[0];
  1013. switch (function) {
  1014. case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
  1015. lux = (report->fap.params[1] << 8) | report->fap.params[2];
  1016. if (lux > 200)
  1017. status = POWER_SUPPLY_STATUS_CHARGING;
  1018. else
  1019. status = POWER_SUPPLY_STATUS_DISCHARGING;
  1020. break;
  1021. case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
  1022. default:
  1023. if (capacity < hidpp->battery.capacity)
  1024. status = POWER_SUPPLY_STATUS_DISCHARGING;
  1025. else
  1026. status = POWER_SUPPLY_STATUS_CHARGING;
  1027. }
  1028. if (capacity == 100)
  1029. status = POWER_SUPPLY_STATUS_FULL;
  1030. hidpp->battery.online = true;
  1031. if (capacity != hidpp->battery.capacity ||
  1032. status != hidpp->battery.status) {
  1033. hidpp->battery.capacity = capacity;
  1034. hidpp->battery.status = status;
  1035. if (hidpp->battery.ps)
  1036. power_supply_changed(hidpp->battery.ps);
  1037. }
  1038. return 0;
  1039. }
  1040. /* -------------------------------------------------------------------------- */
  1041. /* 0x6010: Touchpad FW items */
  1042. /* -------------------------------------------------------------------------- */
  1043. #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
  1044. #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
  1045. struct hidpp_touchpad_fw_items {
  1046. uint8_t presence;
  1047. uint8_t desired_state;
  1048. uint8_t state;
  1049. uint8_t persistent;
  1050. };
  1051. /**
  1052. * send a set state command to the device by reading the current items->state
  1053. * field. items is then filled with the current state.
  1054. */
  1055. static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
  1056. u8 feature_index,
  1057. struct hidpp_touchpad_fw_items *items)
  1058. {
  1059. struct hidpp_report response;
  1060. int ret;
  1061. u8 *params = (u8 *)response.fap.params;
  1062. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1063. CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
  1064. if (ret > 0) {
  1065. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1066. __func__, ret);
  1067. return -EPROTO;
  1068. }
  1069. if (ret)
  1070. return ret;
  1071. items->presence = params[0];
  1072. items->desired_state = params[1];
  1073. items->state = params[2];
  1074. items->persistent = params[3];
  1075. return 0;
  1076. }
  1077. /* -------------------------------------------------------------------------- */
  1078. /* 0x6100: TouchPadRawXY */
  1079. /* -------------------------------------------------------------------------- */
  1080. #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
  1081. #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
  1082. #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
  1083. #define EVENT_TOUCHPAD_RAW_XY 0x00
  1084. #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
  1085. #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
  1086. struct hidpp_touchpad_raw_info {
  1087. u16 x_size;
  1088. u16 y_size;
  1089. u8 z_range;
  1090. u8 area_range;
  1091. u8 timestamp_unit;
  1092. u8 maxcontacts;
  1093. u8 origin;
  1094. u16 res;
  1095. };
  1096. struct hidpp_touchpad_raw_xy_finger {
  1097. u8 contact_type;
  1098. u8 contact_status;
  1099. u16 x;
  1100. u16 y;
  1101. u8 z;
  1102. u8 area;
  1103. u8 finger_id;
  1104. };
  1105. struct hidpp_touchpad_raw_xy {
  1106. u16 timestamp;
  1107. struct hidpp_touchpad_raw_xy_finger fingers[2];
  1108. u8 spurious_flag;
  1109. u8 end_of_frame;
  1110. u8 finger_count;
  1111. u8 button;
  1112. };
  1113. static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
  1114. u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
  1115. {
  1116. struct hidpp_report response;
  1117. int ret;
  1118. u8 *params = (u8 *)response.fap.params;
  1119. ret = hidpp_send_fap_command_sync(hidpp, feature_index,
  1120. CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
  1121. if (ret > 0) {
  1122. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1123. __func__, ret);
  1124. return -EPROTO;
  1125. }
  1126. if (ret)
  1127. return ret;
  1128. raw_info->x_size = get_unaligned_be16(&params[0]);
  1129. raw_info->y_size = get_unaligned_be16(&params[2]);
  1130. raw_info->z_range = params[4];
  1131. raw_info->area_range = params[5];
  1132. raw_info->maxcontacts = params[7];
  1133. raw_info->origin = params[8];
  1134. /* res is given in unit per inch */
  1135. raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
  1136. return ret;
  1137. }
  1138. static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
  1139. u8 feature_index, bool send_raw_reports,
  1140. bool sensor_enhanced_settings)
  1141. {
  1142. struct hidpp_report response;
  1143. /*
  1144. * Params:
  1145. * bit 0 - enable raw
  1146. * bit 1 - 16bit Z, no area
  1147. * bit 2 - enhanced sensitivity
  1148. * bit 3 - width, height (4 bits each) instead of area
  1149. * bit 4 - send raw + gestures (degrades smoothness)
  1150. * remaining bits - reserved
  1151. */
  1152. u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
  1153. return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
  1154. CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
  1155. }
  1156. static void hidpp_touchpad_touch_event(u8 *data,
  1157. struct hidpp_touchpad_raw_xy_finger *finger)
  1158. {
  1159. u8 x_m = data[0] << 2;
  1160. u8 y_m = data[2] << 2;
  1161. finger->x = x_m << 6 | data[1];
  1162. finger->y = y_m << 6 | data[3];
  1163. finger->contact_type = data[0] >> 6;
  1164. finger->contact_status = data[2] >> 6;
  1165. finger->z = data[4];
  1166. finger->area = data[5];
  1167. finger->finger_id = data[6] >> 4;
  1168. }
  1169. static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
  1170. u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
  1171. {
  1172. memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
  1173. raw_xy->end_of_frame = data[8] & 0x01;
  1174. raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
  1175. raw_xy->finger_count = data[15] & 0x0f;
  1176. raw_xy->button = (data[8] >> 2) & 0x01;
  1177. if (raw_xy->finger_count) {
  1178. hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
  1179. hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
  1180. }
  1181. }
  1182. /* -------------------------------------------------------------------------- */
  1183. /* 0x8123: Force feedback support */
  1184. /* -------------------------------------------------------------------------- */
  1185. #define HIDPP_FF_GET_INFO 0x01
  1186. #define HIDPP_FF_RESET_ALL 0x11
  1187. #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
  1188. #define HIDPP_FF_SET_EFFECT_STATE 0x31
  1189. #define HIDPP_FF_DESTROY_EFFECT 0x41
  1190. #define HIDPP_FF_GET_APERTURE 0x51
  1191. #define HIDPP_FF_SET_APERTURE 0x61
  1192. #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
  1193. #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
  1194. #define HIDPP_FF_EFFECT_STATE_GET 0x00
  1195. #define HIDPP_FF_EFFECT_STATE_STOP 0x01
  1196. #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
  1197. #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
  1198. #define HIDPP_FF_EFFECT_CONSTANT 0x00
  1199. #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
  1200. #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
  1201. #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
  1202. #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
  1203. #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
  1204. #define HIDPP_FF_EFFECT_SPRING 0x06
  1205. #define HIDPP_FF_EFFECT_DAMPER 0x07
  1206. #define HIDPP_FF_EFFECT_FRICTION 0x08
  1207. #define HIDPP_FF_EFFECT_INERTIA 0x09
  1208. #define HIDPP_FF_EFFECT_RAMP 0x0A
  1209. #define HIDPP_FF_EFFECT_AUTOSTART 0x80
  1210. #define HIDPP_FF_EFFECTID_NONE -1
  1211. #define HIDPP_FF_EFFECTID_AUTOCENTER -2
  1212. #define HIDPP_FF_MAX_PARAMS 20
  1213. #define HIDPP_FF_RESERVED_SLOTS 1
  1214. struct hidpp_ff_private_data {
  1215. struct hidpp_device *hidpp;
  1216. u8 feature_index;
  1217. u8 version;
  1218. u16 gain;
  1219. s16 range;
  1220. u8 slot_autocenter;
  1221. u8 num_effects;
  1222. int *effect_ids;
  1223. struct workqueue_struct *wq;
  1224. atomic_t workqueue_size;
  1225. };
  1226. struct hidpp_ff_work_data {
  1227. struct work_struct work;
  1228. struct hidpp_ff_private_data *data;
  1229. int effect_id;
  1230. u8 command;
  1231. u8 params[HIDPP_FF_MAX_PARAMS];
  1232. u8 size;
  1233. };
  1234. static const signed short hiddpp_ff_effects[] = {
  1235. FF_CONSTANT,
  1236. FF_PERIODIC,
  1237. FF_SINE,
  1238. FF_SQUARE,
  1239. FF_SAW_UP,
  1240. FF_SAW_DOWN,
  1241. FF_TRIANGLE,
  1242. FF_SPRING,
  1243. FF_DAMPER,
  1244. FF_AUTOCENTER,
  1245. FF_GAIN,
  1246. -1
  1247. };
  1248. static const signed short hiddpp_ff_effects_v2[] = {
  1249. FF_RAMP,
  1250. FF_FRICTION,
  1251. FF_INERTIA,
  1252. -1
  1253. };
  1254. static const u8 HIDPP_FF_CONDITION_CMDS[] = {
  1255. HIDPP_FF_EFFECT_SPRING,
  1256. HIDPP_FF_EFFECT_FRICTION,
  1257. HIDPP_FF_EFFECT_DAMPER,
  1258. HIDPP_FF_EFFECT_INERTIA
  1259. };
  1260. static const char *HIDPP_FF_CONDITION_NAMES[] = {
  1261. "spring",
  1262. "friction",
  1263. "damper",
  1264. "inertia"
  1265. };
  1266. static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
  1267. {
  1268. int i;
  1269. for (i = 0; i < data->num_effects; i++)
  1270. if (data->effect_ids[i] == effect_id)
  1271. return i+1;
  1272. return 0;
  1273. }
  1274. static void hidpp_ff_work_handler(struct work_struct *w)
  1275. {
  1276. struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
  1277. struct hidpp_ff_private_data *data = wd->data;
  1278. struct hidpp_report response;
  1279. u8 slot;
  1280. int ret;
  1281. /* add slot number if needed */
  1282. switch (wd->effect_id) {
  1283. case HIDPP_FF_EFFECTID_AUTOCENTER:
  1284. wd->params[0] = data->slot_autocenter;
  1285. break;
  1286. case HIDPP_FF_EFFECTID_NONE:
  1287. /* leave slot as zero */
  1288. break;
  1289. default:
  1290. /* find current slot for effect */
  1291. wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
  1292. break;
  1293. }
  1294. /* send command and wait for reply */
  1295. ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
  1296. wd->command, wd->params, wd->size, &response);
  1297. if (ret) {
  1298. hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
  1299. goto out;
  1300. }
  1301. /* parse return data */
  1302. switch (wd->command) {
  1303. case HIDPP_FF_DOWNLOAD_EFFECT:
  1304. slot = response.fap.params[0];
  1305. if (slot > 0 && slot <= data->num_effects) {
  1306. if (wd->effect_id >= 0)
  1307. /* regular effect uploaded */
  1308. data->effect_ids[slot-1] = wd->effect_id;
  1309. else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
  1310. /* autocenter spring uploaded */
  1311. data->slot_autocenter = slot;
  1312. }
  1313. break;
  1314. case HIDPP_FF_DESTROY_EFFECT:
  1315. if (wd->effect_id >= 0)
  1316. /* regular effect destroyed */
  1317. data->effect_ids[wd->params[0]-1] = -1;
  1318. else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
  1319. /* autocenter spring destoyed */
  1320. data->slot_autocenter = 0;
  1321. break;
  1322. case HIDPP_FF_SET_GLOBAL_GAINS:
  1323. data->gain = (wd->params[0] << 8) + wd->params[1];
  1324. break;
  1325. case HIDPP_FF_SET_APERTURE:
  1326. data->range = (wd->params[0] << 8) + wd->params[1];
  1327. break;
  1328. default:
  1329. /* no action needed */
  1330. break;
  1331. }
  1332. out:
  1333. atomic_dec(&data->workqueue_size);
  1334. kfree(wd);
  1335. }
  1336. static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
  1337. {
  1338. struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
  1339. int s;
  1340. if (!wd)
  1341. return -ENOMEM;
  1342. INIT_WORK(&wd->work, hidpp_ff_work_handler);
  1343. wd->data = data;
  1344. wd->effect_id = effect_id;
  1345. wd->command = command;
  1346. wd->size = size;
  1347. memcpy(wd->params, params, size);
  1348. atomic_inc(&data->workqueue_size);
  1349. queue_work(data->wq, &wd->work);
  1350. /* warn about excessive queue size */
  1351. s = atomic_read(&data->workqueue_size);
  1352. if (s >= 20 && s % 20 == 0)
  1353. hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
  1354. return 0;
  1355. }
  1356. static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
  1357. {
  1358. struct hidpp_ff_private_data *data = dev->ff->private;
  1359. u8 params[20];
  1360. u8 size;
  1361. int force;
  1362. /* set common parameters */
  1363. params[2] = effect->replay.length >> 8;
  1364. params[3] = effect->replay.length & 255;
  1365. params[4] = effect->replay.delay >> 8;
  1366. params[5] = effect->replay.delay & 255;
  1367. switch (effect->type) {
  1368. case FF_CONSTANT:
  1369. force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  1370. params[1] = HIDPP_FF_EFFECT_CONSTANT;
  1371. params[6] = force >> 8;
  1372. params[7] = force & 255;
  1373. params[8] = effect->u.constant.envelope.attack_level >> 7;
  1374. params[9] = effect->u.constant.envelope.attack_length >> 8;
  1375. params[10] = effect->u.constant.envelope.attack_length & 255;
  1376. params[11] = effect->u.constant.envelope.fade_level >> 7;
  1377. params[12] = effect->u.constant.envelope.fade_length >> 8;
  1378. params[13] = effect->u.constant.envelope.fade_length & 255;
  1379. size = 14;
  1380. dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
  1381. effect->u.constant.level,
  1382. effect->direction, force);
  1383. dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
  1384. effect->u.constant.envelope.attack_level,
  1385. effect->u.constant.envelope.attack_length,
  1386. effect->u.constant.envelope.fade_level,
  1387. effect->u.constant.envelope.fade_length);
  1388. break;
  1389. case FF_PERIODIC:
  1390. {
  1391. switch (effect->u.periodic.waveform) {
  1392. case FF_SINE:
  1393. params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
  1394. break;
  1395. case FF_SQUARE:
  1396. params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
  1397. break;
  1398. case FF_SAW_UP:
  1399. params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
  1400. break;
  1401. case FF_SAW_DOWN:
  1402. params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
  1403. break;
  1404. case FF_TRIANGLE:
  1405. params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
  1406. break;
  1407. default:
  1408. hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
  1409. return -EINVAL;
  1410. }
  1411. force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  1412. params[6] = effect->u.periodic.magnitude >> 8;
  1413. params[7] = effect->u.periodic.magnitude & 255;
  1414. params[8] = effect->u.periodic.offset >> 8;
  1415. params[9] = effect->u.periodic.offset & 255;
  1416. params[10] = effect->u.periodic.period >> 8;
  1417. params[11] = effect->u.periodic.period & 255;
  1418. params[12] = effect->u.periodic.phase >> 8;
  1419. params[13] = effect->u.periodic.phase & 255;
  1420. params[14] = effect->u.periodic.envelope.attack_level >> 7;
  1421. params[15] = effect->u.periodic.envelope.attack_length >> 8;
  1422. params[16] = effect->u.periodic.envelope.attack_length & 255;
  1423. params[17] = effect->u.periodic.envelope.fade_level >> 7;
  1424. params[18] = effect->u.periodic.envelope.fade_length >> 8;
  1425. params[19] = effect->u.periodic.envelope.fade_length & 255;
  1426. size = 20;
  1427. dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
  1428. effect->u.periodic.magnitude, effect->direction,
  1429. effect->u.periodic.offset,
  1430. effect->u.periodic.period,
  1431. effect->u.periodic.phase);
  1432. dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
  1433. effect->u.periodic.envelope.attack_level,
  1434. effect->u.periodic.envelope.attack_length,
  1435. effect->u.periodic.envelope.fade_level,
  1436. effect->u.periodic.envelope.fade_length);
  1437. break;
  1438. }
  1439. case FF_RAMP:
  1440. params[1] = HIDPP_FF_EFFECT_RAMP;
  1441. force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  1442. params[6] = force >> 8;
  1443. params[7] = force & 255;
  1444. force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
  1445. params[8] = force >> 8;
  1446. params[9] = force & 255;
  1447. params[10] = effect->u.ramp.envelope.attack_level >> 7;
  1448. params[11] = effect->u.ramp.envelope.attack_length >> 8;
  1449. params[12] = effect->u.ramp.envelope.attack_length & 255;
  1450. params[13] = effect->u.ramp.envelope.fade_level >> 7;
  1451. params[14] = effect->u.ramp.envelope.fade_length >> 8;
  1452. params[15] = effect->u.ramp.envelope.fade_length & 255;
  1453. size = 16;
  1454. dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
  1455. effect->u.ramp.start_level,
  1456. effect->u.ramp.end_level,
  1457. effect->direction, force);
  1458. dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
  1459. effect->u.ramp.envelope.attack_level,
  1460. effect->u.ramp.envelope.attack_length,
  1461. effect->u.ramp.envelope.fade_level,
  1462. effect->u.ramp.envelope.fade_length);
  1463. break;
  1464. case FF_FRICTION:
  1465. case FF_INERTIA:
  1466. case FF_SPRING:
  1467. case FF_DAMPER:
  1468. params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
  1469. params[6] = effect->u.condition[0].left_saturation >> 9;
  1470. params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
  1471. params[8] = effect->u.condition[0].left_coeff >> 8;
  1472. params[9] = effect->u.condition[0].left_coeff & 255;
  1473. params[10] = effect->u.condition[0].deadband >> 9;
  1474. params[11] = (effect->u.condition[0].deadband >> 1) & 255;
  1475. params[12] = effect->u.condition[0].center >> 8;
  1476. params[13] = effect->u.condition[0].center & 255;
  1477. params[14] = effect->u.condition[0].right_coeff >> 8;
  1478. params[15] = effect->u.condition[0].right_coeff & 255;
  1479. params[16] = effect->u.condition[0].right_saturation >> 9;
  1480. params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
  1481. size = 18;
  1482. dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
  1483. HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
  1484. effect->u.condition[0].left_coeff,
  1485. effect->u.condition[0].left_saturation,
  1486. effect->u.condition[0].right_coeff,
  1487. effect->u.condition[0].right_saturation);
  1488. dbg_hid(" deadband=%d, center=%d\n",
  1489. effect->u.condition[0].deadband,
  1490. effect->u.condition[0].center);
  1491. break;
  1492. default:
  1493. hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
  1494. return -EINVAL;
  1495. }
  1496. return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
  1497. }
  1498. static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
  1499. {
  1500. struct hidpp_ff_private_data *data = dev->ff->private;
  1501. u8 params[2];
  1502. params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
  1503. dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
  1504. return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
  1505. }
  1506. static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
  1507. {
  1508. struct hidpp_ff_private_data *data = dev->ff->private;
  1509. u8 slot = 0;
  1510. dbg_hid("Erasing effect %d.\n", effect_id);
  1511. return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
  1512. }
  1513. static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
  1514. {
  1515. struct hidpp_ff_private_data *data = dev->ff->private;
  1516. u8 params[18];
  1517. dbg_hid("Setting autocenter to %d.\n", magnitude);
  1518. /* start a standard spring effect */
  1519. params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
  1520. /* zero delay and duration */
  1521. params[2] = params[3] = params[4] = params[5] = 0;
  1522. /* set coeff to 25% of saturation */
  1523. params[8] = params[14] = magnitude >> 11;
  1524. params[9] = params[15] = (magnitude >> 3) & 255;
  1525. params[6] = params[16] = magnitude >> 9;
  1526. params[7] = params[17] = (magnitude >> 1) & 255;
  1527. /* zero deadband and center */
  1528. params[10] = params[11] = params[12] = params[13] = 0;
  1529. hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
  1530. }
  1531. static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
  1532. {
  1533. struct hidpp_ff_private_data *data = dev->ff->private;
  1534. u8 params[4];
  1535. dbg_hid("Setting gain to %d.\n", gain);
  1536. params[0] = gain >> 8;
  1537. params[1] = gain & 255;
  1538. params[2] = 0; /* no boost */
  1539. params[3] = 0;
  1540. hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
  1541. }
  1542. static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
  1543. {
  1544. struct hid_device *hid = to_hid_device(dev);
  1545. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  1546. struct input_dev *idev = hidinput->input;
  1547. struct hidpp_ff_private_data *data = idev->ff->private;
  1548. return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
  1549. }
  1550. static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1551. {
  1552. struct hid_device *hid = to_hid_device(dev);
  1553. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  1554. struct input_dev *idev = hidinput->input;
  1555. struct hidpp_ff_private_data *data = idev->ff->private;
  1556. u8 params[2];
  1557. int range = simple_strtoul(buf, NULL, 10);
  1558. range = clamp(range, 180, 900);
  1559. params[0] = range >> 8;
  1560. params[1] = range & 0x00FF;
  1561. hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
  1562. return count;
  1563. }
  1564. static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
  1565. static void hidpp_ff_destroy(struct ff_device *ff)
  1566. {
  1567. struct hidpp_ff_private_data *data = ff->private;
  1568. kfree(data->effect_ids);
  1569. }
  1570. static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
  1571. {
  1572. struct hid_device *hid = hidpp->hid_dev;
  1573. struct hid_input *hidinput;
  1574. struct input_dev *dev;
  1575. const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
  1576. const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
  1577. struct ff_device *ff;
  1578. struct hidpp_report response;
  1579. struct hidpp_ff_private_data *data;
  1580. int error, j, num_slots;
  1581. u8 version;
  1582. if (list_empty(&hid->inputs)) {
  1583. hid_err(hid, "no inputs found\n");
  1584. return -ENODEV;
  1585. }
  1586. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  1587. dev = hidinput->input;
  1588. if (!dev) {
  1589. hid_err(hid, "Struct input_dev not set!\n");
  1590. return -EINVAL;
  1591. }
  1592. /* Get firmware release */
  1593. version = bcdDevice & 255;
  1594. /* Set supported force feedback capabilities */
  1595. for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
  1596. set_bit(hiddpp_ff_effects[j], dev->ffbit);
  1597. if (version > 1)
  1598. for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
  1599. set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
  1600. /* Read number of slots available in device */
  1601. error = hidpp_send_fap_command_sync(hidpp, feature_index,
  1602. HIDPP_FF_GET_INFO, NULL, 0, &response);
  1603. if (error) {
  1604. if (error < 0)
  1605. return error;
  1606. hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
  1607. __func__, error);
  1608. return -EPROTO;
  1609. }
  1610. num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
  1611. error = input_ff_create(dev, num_slots);
  1612. if (error) {
  1613. hid_err(dev, "Failed to create FF device!\n");
  1614. return error;
  1615. }
  1616. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1617. if (!data)
  1618. return -ENOMEM;
  1619. data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
  1620. if (!data->effect_ids) {
  1621. kfree(data);
  1622. return -ENOMEM;
  1623. }
  1624. data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
  1625. if (!data->wq) {
  1626. kfree(data->effect_ids);
  1627. kfree(data);
  1628. return -ENOMEM;
  1629. }
  1630. data->hidpp = hidpp;
  1631. data->feature_index = feature_index;
  1632. data->version = version;
  1633. data->slot_autocenter = 0;
  1634. data->num_effects = num_slots;
  1635. for (j = 0; j < num_slots; j++)
  1636. data->effect_ids[j] = -1;
  1637. ff = dev->ff;
  1638. ff->private = data;
  1639. ff->upload = hidpp_ff_upload_effect;
  1640. ff->erase = hidpp_ff_erase_effect;
  1641. ff->playback = hidpp_ff_playback;
  1642. ff->set_gain = hidpp_ff_set_gain;
  1643. ff->set_autocenter = hidpp_ff_set_autocenter;
  1644. ff->destroy = hidpp_ff_destroy;
  1645. /* reset all forces */
  1646. error = hidpp_send_fap_command_sync(hidpp, feature_index,
  1647. HIDPP_FF_RESET_ALL, NULL, 0, &response);
  1648. /* Read current Range */
  1649. error = hidpp_send_fap_command_sync(hidpp, feature_index,
  1650. HIDPP_FF_GET_APERTURE, NULL, 0, &response);
  1651. if (error)
  1652. hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
  1653. data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
  1654. /* Create sysfs interface */
  1655. error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
  1656. if (error)
  1657. hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
  1658. /* Read the current gain values */
  1659. error = hidpp_send_fap_command_sync(hidpp, feature_index,
  1660. HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
  1661. if (error)
  1662. hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
  1663. data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
  1664. /* ignore boost value at response.fap.params[2] */
  1665. /* init the hardware command queue */
  1666. atomic_set(&data->workqueue_size, 0);
  1667. /* initialize with zero autocenter to get wheel in usable state */
  1668. hidpp_ff_set_autocenter(dev, 0);
  1669. hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
  1670. version);
  1671. return 0;
  1672. }
  1673. static int hidpp_ff_deinit(struct hid_device *hid)
  1674. {
  1675. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  1676. struct input_dev *dev = hidinput->input;
  1677. struct hidpp_ff_private_data *data;
  1678. if (!dev) {
  1679. hid_err(hid, "Struct input_dev not found!\n");
  1680. return -EINVAL;
  1681. }
  1682. hid_info(hid, "Unloading HID++ force feedback.\n");
  1683. data = dev->ff->private;
  1684. if (!data) {
  1685. hid_err(hid, "Private data not found!\n");
  1686. return -EINVAL;
  1687. }
  1688. destroy_workqueue(data->wq);
  1689. device_remove_file(&hid->dev, &dev_attr_range);
  1690. return 0;
  1691. }
  1692. /* ************************************************************************** */
  1693. /* */
  1694. /* Device Support */
  1695. /* */
  1696. /* ************************************************************************** */
  1697. /* -------------------------------------------------------------------------- */
  1698. /* Touchpad HID++ devices */
  1699. /* -------------------------------------------------------------------------- */
  1700. #define WTP_MANUAL_RESOLUTION 39
  1701. struct wtp_data {
  1702. struct input_dev *input;
  1703. u16 x_size, y_size;
  1704. u8 finger_count;
  1705. u8 mt_feature_index;
  1706. u8 button_feature_index;
  1707. u8 maxcontacts;
  1708. bool flip_y;
  1709. unsigned int resolution;
  1710. };
  1711. static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  1712. struct hid_field *field, struct hid_usage *usage,
  1713. unsigned long **bit, int *max)
  1714. {
  1715. return -1;
  1716. }
  1717. static void wtp_populate_input(struct hidpp_device *hidpp,
  1718. struct input_dev *input_dev, bool origin_is_hid_core)
  1719. {
  1720. struct wtp_data *wd = hidpp->private_data;
  1721. __set_bit(EV_ABS, input_dev->evbit);
  1722. __set_bit(EV_KEY, input_dev->evbit);
  1723. __clear_bit(EV_REL, input_dev->evbit);
  1724. __clear_bit(EV_LED, input_dev->evbit);
  1725. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
  1726. input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
  1727. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
  1728. input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
  1729. /* Max pressure is not given by the devices, pick one */
  1730. input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
  1731. input_set_capability(input_dev, EV_KEY, BTN_LEFT);
  1732. if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
  1733. input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
  1734. else
  1735. __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
  1736. input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
  1737. INPUT_MT_DROP_UNUSED);
  1738. wd->input = input_dev;
  1739. }
  1740. static void wtp_touch_event(struct wtp_data *wd,
  1741. struct hidpp_touchpad_raw_xy_finger *touch_report)
  1742. {
  1743. int slot;
  1744. if (!touch_report->finger_id || touch_report->contact_type)
  1745. /* no actual data */
  1746. return;
  1747. slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
  1748. input_mt_slot(wd->input, slot);
  1749. input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
  1750. touch_report->contact_status);
  1751. if (touch_report->contact_status) {
  1752. input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
  1753. touch_report->x);
  1754. input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
  1755. wd->flip_y ? wd->y_size - touch_report->y :
  1756. touch_report->y);
  1757. input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
  1758. touch_report->area);
  1759. }
  1760. }
  1761. static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
  1762. struct hidpp_touchpad_raw_xy *raw)
  1763. {
  1764. struct wtp_data *wd = hidpp->private_data;
  1765. int i;
  1766. for (i = 0; i < 2; i++)
  1767. wtp_touch_event(wd, &(raw->fingers[i]));
  1768. if (raw->end_of_frame &&
  1769. !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
  1770. input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
  1771. if (raw->end_of_frame || raw->finger_count <= 2) {
  1772. input_mt_sync_frame(wd->input);
  1773. input_sync(wd->input);
  1774. }
  1775. }
  1776. static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
  1777. {
  1778. struct wtp_data *wd = hidpp->private_data;
  1779. u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
  1780. (data[7] >> 4) * (data[7] >> 4)) / 2;
  1781. u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
  1782. (data[13] >> 4) * (data[13] >> 4)) / 2;
  1783. struct hidpp_touchpad_raw_xy raw = {
  1784. .timestamp = data[1],
  1785. .fingers = {
  1786. {
  1787. .contact_type = 0,
  1788. .contact_status = !!data[7],
  1789. .x = get_unaligned_le16(&data[3]),
  1790. .y = get_unaligned_le16(&data[5]),
  1791. .z = c1_area,
  1792. .area = c1_area,
  1793. .finger_id = data[2],
  1794. }, {
  1795. .contact_type = 0,
  1796. .contact_status = !!data[13],
  1797. .x = get_unaligned_le16(&data[9]),
  1798. .y = get_unaligned_le16(&data[11]),
  1799. .z = c2_area,
  1800. .area = c2_area,
  1801. .finger_id = data[8],
  1802. }
  1803. },
  1804. .finger_count = wd->maxcontacts,
  1805. .spurious_flag = 0,
  1806. .end_of_frame = (data[0] >> 7) == 0,
  1807. .button = data[0] & 0x01,
  1808. };
  1809. wtp_send_raw_xy_event(hidpp, &raw);
  1810. return 1;
  1811. }
  1812. static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
  1813. {
  1814. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1815. struct wtp_data *wd = hidpp->private_data;
  1816. struct hidpp_report *report = (struct hidpp_report *)data;
  1817. struct hidpp_touchpad_raw_xy raw;
  1818. if (!wd || !wd->input)
  1819. return 1;
  1820. switch (data[0]) {
  1821. case 0x02:
  1822. if (size < 2) {
  1823. hid_err(hdev, "Received HID report of bad size (%d)",
  1824. size);
  1825. return 1;
  1826. }
  1827. if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
  1828. input_event(wd->input, EV_KEY, BTN_LEFT,
  1829. !!(data[1] & 0x01));
  1830. input_event(wd->input, EV_KEY, BTN_RIGHT,
  1831. !!(data[1] & 0x02));
  1832. input_sync(wd->input);
  1833. return 0;
  1834. } else {
  1835. if (size < 21)
  1836. return 1;
  1837. return wtp_mouse_raw_xy_event(hidpp, &data[7]);
  1838. }
  1839. case REPORT_ID_HIDPP_LONG:
  1840. /* size is already checked in hidpp_raw_event. */
  1841. if ((report->fap.feature_index != wd->mt_feature_index) ||
  1842. (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
  1843. return 1;
  1844. hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
  1845. wtp_send_raw_xy_event(hidpp, &raw);
  1846. return 0;
  1847. }
  1848. return 0;
  1849. }
  1850. static int wtp_get_config(struct hidpp_device *hidpp)
  1851. {
  1852. struct wtp_data *wd = hidpp->private_data;
  1853. struct hidpp_touchpad_raw_info raw_info = {0};
  1854. u8 feature_type;
  1855. int ret;
  1856. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
  1857. &wd->mt_feature_index, &feature_type);
  1858. if (ret)
  1859. /* means that the device is not powered up */
  1860. return ret;
  1861. ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
  1862. &raw_info);
  1863. if (ret)
  1864. return ret;
  1865. wd->x_size = raw_info.x_size;
  1866. wd->y_size = raw_info.y_size;
  1867. wd->maxcontacts = raw_info.maxcontacts;
  1868. wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
  1869. wd->resolution = raw_info.res;
  1870. if (!wd->resolution)
  1871. wd->resolution = WTP_MANUAL_RESOLUTION;
  1872. return 0;
  1873. }
  1874. static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
  1875. {
  1876. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1877. struct wtp_data *wd;
  1878. wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
  1879. GFP_KERNEL);
  1880. if (!wd)
  1881. return -ENOMEM;
  1882. hidpp->private_data = wd;
  1883. return 0;
  1884. };
  1885. static int wtp_connect(struct hid_device *hdev, bool connected)
  1886. {
  1887. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1888. struct wtp_data *wd = hidpp->private_data;
  1889. int ret;
  1890. if (!wd->x_size) {
  1891. ret = wtp_get_config(hidpp);
  1892. if (ret) {
  1893. hid_err(hdev, "Can not get wtp config: %d\n", ret);
  1894. return ret;
  1895. }
  1896. }
  1897. return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
  1898. true, true);
  1899. }
  1900. /* ------------------------------------------------------------------------- */
  1901. /* Logitech M560 devices */
  1902. /* ------------------------------------------------------------------------- */
  1903. /*
  1904. * Logitech M560 protocol overview
  1905. *
  1906. * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
  1907. * the sides buttons are pressed, it sends some keyboard keys events
  1908. * instead of buttons ones.
  1909. * To complicate things further, the middle button keys sequence
  1910. * is different from the odd press and the even press.
  1911. *
  1912. * forward button -> Super_R
  1913. * backward button -> Super_L+'d' (press only)
  1914. * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
  1915. * 2nd time: left-click (press only)
  1916. * NB: press-only means that when the button is pressed, the
  1917. * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
  1918. * together sequentially; instead when the button is released, no event is
  1919. * generated !
  1920. *
  1921. * With the command
  1922. * 10<xx>0a 3500af03 (where <xx> is the mouse id),
  1923. * the mouse reacts differently:
  1924. * - it never sends a keyboard key event
  1925. * - for the three mouse button it sends:
  1926. * middle button press 11<xx>0a 3500af00...
  1927. * side 1 button (forward) press 11<xx>0a 3500b000...
  1928. * side 2 button (backward) press 11<xx>0a 3500ae00...
  1929. * middle/side1/side2 button release 11<xx>0a 35000000...
  1930. */
  1931. static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
  1932. struct m560_private_data {
  1933. struct input_dev *input;
  1934. };
  1935. /* how buttons are mapped in the report */
  1936. #define M560_MOUSE_BTN_LEFT 0x01
  1937. #define M560_MOUSE_BTN_RIGHT 0x02
  1938. #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
  1939. #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
  1940. #define M560_SUB_ID 0x0a
  1941. #define M560_BUTTON_MODE_REGISTER 0x35
  1942. static int m560_send_config_command(struct hid_device *hdev, bool connected)
  1943. {
  1944. struct hidpp_report response;
  1945. struct hidpp_device *hidpp_dev;
  1946. hidpp_dev = hid_get_drvdata(hdev);
  1947. return hidpp_send_rap_command_sync(
  1948. hidpp_dev,
  1949. REPORT_ID_HIDPP_SHORT,
  1950. M560_SUB_ID,
  1951. M560_BUTTON_MODE_REGISTER,
  1952. (u8 *)m560_config_parameter,
  1953. sizeof(m560_config_parameter),
  1954. &response
  1955. );
  1956. }
  1957. static int m560_allocate(struct hid_device *hdev)
  1958. {
  1959. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1960. struct m560_private_data *d;
  1961. d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
  1962. GFP_KERNEL);
  1963. if (!d)
  1964. return -ENOMEM;
  1965. hidpp->private_data = d;
  1966. return 0;
  1967. };
  1968. static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
  1969. {
  1970. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  1971. struct m560_private_data *mydata = hidpp->private_data;
  1972. /* sanity check */
  1973. if (!mydata || !mydata->input) {
  1974. hid_err(hdev, "error in parameter\n");
  1975. return -EINVAL;
  1976. }
  1977. if (size < 7) {
  1978. hid_err(hdev, "error in report\n");
  1979. return 0;
  1980. }
  1981. if (data[0] == REPORT_ID_HIDPP_LONG &&
  1982. data[2] == M560_SUB_ID && data[6] == 0x00) {
  1983. /*
  1984. * m560 mouse report for middle, forward and backward button
  1985. *
  1986. * data[0] = 0x11
  1987. * data[1] = device-id
  1988. * data[2] = 0x0a
  1989. * data[5] = 0xaf -> middle
  1990. * 0xb0 -> forward
  1991. * 0xae -> backward
  1992. * 0x00 -> release all
  1993. * data[6] = 0x00
  1994. */
  1995. switch (data[5]) {
  1996. case 0xaf:
  1997. input_report_key(mydata->input, BTN_MIDDLE, 1);
  1998. break;
  1999. case 0xb0:
  2000. input_report_key(mydata->input, BTN_FORWARD, 1);
  2001. break;
  2002. case 0xae:
  2003. input_report_key(mydata->input, BTN_BACK, 1);
  2004. break;
  2005. case 0x00:
  2006. input_report_key(mydata->input, BTN_BACK, 0);
  2007. input_report_key(mydata->input, BTN_FORWARD, 0);
  2008. input_report_key(mydata->input, BTN_MIDDLE, 0);
  2009. break;
  2010. default:
  2011. hid_err(hdev, "error in report\n");
  2012. return 0;
  2013. }
  2014. input_sync(mydata->input);
  2015. } else if (data[0] == 0x02) {
  2016. /*
  2017. * Logitech M560 mouse report
  2018. *
  2019. * data[0] = type (0x02)
  2020. * data[1..2] = buttons
  2021. * data[3..5] = xy
  2022. * data[6] = wheel
  2023. */
  2024. int v;
  2025. input_report_key(mydata->input, BTN_LEFT,
  2026. !!(data[1] & M560_MOUSE_BTN_LEFT));
  2027. input_report_key(mydata->input, BTN_RIGHT,
  2028. !!(data[1] & M560_MOUSE_BTN_RIGHT));
  2029. if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
  2030. input_report_rel(mydata->input, REL_HWHEEL, -1);
  2031. else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
  2032. input_report_rel(mydata->input, REL_HWHEEL, 1);
  2033. v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
  2034. input_report_rel(mydata->input, REL_X, v);
  2035. v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
  2036. input_report_rel(mydata->input, REL_Y, v);
  2037. v = hid_snto32(data[6], 8);
  2038. input_report_rel(mydata->input, REL_WHEEL, v);
  2039. input_sync(mydata->input);
  2040. }
  2041. return 1;
  2042. }
  2043. static void m560_populate_input(struct hidpp_device *hidpp,
  2044. struct input_dev *input_dev, bool origin_is_hid_core)
  2045. {
  2046. struct m560_private_data *mydata = hidpp->private_data;
  2047. mydata->input = input_dev;
  2048. __set_bit(EV_KEY, mydata->input->evbit);
  2049. __set_bit(BTN_MIDDLE, mydata->input->keybit);
  2050. __set_bit(BTN_RIGHT, mydata->input->keybit);
  2051. __set_bit(BTN_LEFT, mydata->input->keybit);
  2052. __set_bit(BTN_BACK, mydata->input->keybit);
  2053. __set_bit(BTN_FORWARD, mydata->input->keybit);
  2054. __set_bit(EV_REL, mydata->input->evbit);
  2055. __set_bit(REL_X, mydata->input->relbit);
  2056. __set_bit(REL_Y, mydata->input->relbit);
  2057. __set_bit(REL_WHEEL, mydata->input->relbit);
  2058. __set_bit(REL_HWHEEL, mydata->input->relbit);
  2059. }
  2060. static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  2061. struct hid_field *field, struct hid_usage *usage,
  2062. unsigned long **bit, int *max)
  2063. {
  2064. return -1;
  2065. }
  2066. /* ------------------------------------------------------------------------- */
  2067. /* Logitech K400 devices */
  2068. /* ------------------------------------------------------------------------- */
  2069. /*
  2070. * The Logitech K400 keyboard has an embedded touchpad which is seen
  2071. * as a mouse from the OS point of view. There is a hardware shortcut to disable
  2072. * tap-to-click but the setting is not remembered accross reset, annoying some
  2073. * users.
  2074. *
  2075. * We can toggle this feature from the host by using the feature 0x6010:
  2076. * Touchpad FW items
  2077. */
  2078. struct k400_private_data {
  2079. u8 feature_index;
  2080. };
  2081. static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
  2082. {
  2083. struct k400_private_data *k400 = hidpp->private_data;
  2084. struct hidpp_touchpad_fw_items items = {};
  2085. int ret;
  2086. u8 feature_type;
  2087. if (!k400->feature_index) {
  2088. ret = hidpp_root_get_feature(hidpp,
  2089. HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
  2090. &k400->feature_index, &feature_type);
  2091. if (ret)
  2092. /* means that the device is not powered up */
  2093. return ret;
  2094. }
  2095. ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
  2096. if (ret)
  2097. return ret;
  2098. return 0;
  2099. }
  2100. static int k400_allocate(struct hid_device *hdev)
  2101. {
  2102. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2103. struct k400_private_data *k400;
  2104. k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
  2105. GFP_KERNEL);
  2106. if (!k400)
  2107. return -ENOMEM;
  2108. hidpp->private_data = k400;
  2109. return 0;
  2110. };
  2111. static int k400_connect(struct hid_device *hdev, bool connected)
  2112. {
  2113. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2114. if (!disable_tap_to_click)
  2115. return 0;
  2116. return k400_disable_tap_to_click(hidpp);
  2117. }
  2118. /* ------------------------------------------------------------------------- */
  2119. /* Logitech G920 Driving Force Racing Wheel for Xbox One */
  2120. /* ------------------------------------------------------------------------- */
  2121. #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
  2122. static int g920_get_config(struct hidpp_device *hidpp)
  2123. {
  2124. u8 feature_type;
  2125. u8 feature_index;
  2126. int ret;
  2127. /* Find feature and store for later use */
  2128. ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
  2129. &feature_index, &feature_type);
  2130. if (ret)
  2131. return ret;
  2132. ret = hidpp_ff_init(hidpp, feature_index);
  2133. if (ret)
  2134. hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
  2135. ret);
  2136. return 0;
  2137. }
  2138. /* -------------------------------------------------------------------------- */
  2139. /* Generic HID++ devices */
  2140. /* -------------------------------------------------------------------------- */
  2141. static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  2142. struct hid_field *field, struct hid_usage *usage,
  2143. unsigned long **bit, int *max)
  2144. {
  2145. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2146. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  2147. return wtp_input_mapping(hdev, hi, field, usage, bit, max);
  2148. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
  2149. field->application != HID_GD_MOUSE)
  2150. return m560_input_mapping(hdev, hi, field, usage, bit, max);
  2151. return 0;
  2152. }
  2153. static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  2154. struct hid_field *field, struct hid_usage *usage,
  2155. unsigned long **bit, int *max)
  2156. {
  2157. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2158. /* Ensure that Logitech G920 is not given a default fuzz/flat value */
  2159. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  2160. if (usage->type == EV_ABS && (usage->code == ABS_X ||
  2161. usage->code == ABS_Y || usage->code == ABS_Z ||
  2162. usage->code == ABS_RZ)) {
  2163. field->application = HID_GD_MULTIAXIS;
  2164. }
  2165. }
  2166. return 0;
  2167. }
  2168. static void hidpp_populate_input(struct hidpp_device *hidpp,
  2169. struct input_dev *input, bool origin_is_hid_core)
  2170. {
  2171. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  2172. wtp_populate_input(hidpp, input, origin_is_hid_core);
  2173. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
  2174. m560_populate_input(hidpp, input, origin_is_hid_core);
  2175. }
  2176. static int hidpp_input_configured(struct hid_device *hdev,
  2177. struct hid_input *hidinput)
  2178. {
  2179. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2180. struct input_dev *input = hidinput->input;
  2181. hidpp_populate_input(hidpp, input, true);
  2182. return 0;
  2183. }
  2184. static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
  2185. int size)
  2186. {
  2187. struct hidpp_report *question = hidpp->send_receive_buf;
  2188. struct hidpp_report *answer = hidpp->send_receive_buf;
  2189. struct hidpp_report *report = (struct hidpp_report *)data;
  2190. int ret;
  2191. /*
  2192. * If the mutex is locked then we have a pending answer from a
  2193. * previously sent command.
  2194. */
  2195. if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
  2196. /*
  2197. * Check for a correct hidpp20 answer or the corresponding
  2198. * error
  2199. */
  2200. if (hidpp_match_answer(question, report) ||
  2201. hidpp_match_error(question, report)) {
  2202. *answer = *report;
  2203. hidpp->answer_available = true;
  2204. wake_up(&hidpp->wait);
  2205. /*
  2206. * This was an answer to a command that this driver sent
  2207. * We return 1 to hid-core to avoid forwarding the
  2208. * command upstream as it has been treated by the driver
  2209. */
  2210. return 1;
  2211. }
  2212. }
  2213. if (unlikely(hidpp_report_is_connect_event(report))) {
  2214. atomic_set(&hidpp->connected,
  2215. !(report->rap.params[0] & (1 << 6)));
  2216. if (schedule_work(&hidpp->work) == 0)
  2217. dbg_hid("%s: connect event already queued\n", __func__);
  2218. return 1;
  2219. }
  2220. if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
  2221. ret = hidpp20_battery_event(hidpp, data, size);
  2222. if (ret != 0)
  2223. return ret;
  2224. ret = hidpp_solar_battery_event(hidpp, data, size);
  2225. if (ret != 0)
  2226. return ret;
  2227. }
  2228. if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
  2229. ret = hidpp10_battery_event(hidpp, data, size);
  2230. if (ret != 0)
  2231. return ret;
  2232. }
  2233. return 0;
  2234. }
  2235. static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
  2236. u8 *data, int size)
  2237. {
  2238. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2239. int ret = 0;
  2240. /* Generic HID++ processing. */
  2241. switch (data[0]) {
  2242. case REPORT_ID_HIDPP_VERY_LONG:
  2243. if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
  2244. hid_err(hdev, "received hid++ report of bad size (%d)",
  2245. size);
  2246. return 1;
  2247. }
  2248. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  2249. break;
  2250. case REPORT_ID_HIDPP_LONG:
  2251. if (size != HIDPP_REPORT_LONG_LENGTH) {
  2252. hid_err(hdev, "received hid++ report of bad size (%d)",
  2253. size);
  2254. return 1;
  2255. }
  2256. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  2257. break;
  2258. case REPORT_ID_HIDPP_SHORT:
  2259. if (size != HIDPP_REPORT_SHORT_LENGTH) {
  2260. hid_err(hdev, "received hid++ report of bad size (%d)",
  2261. size);
  2262. return 1;
  2263. }
  2264. ret = hidpp_raw_hidpp_event(hidpp, data, size);
  2265. break;
  2266. }
  2267. /* If no report is available for further processing, skip calling
  2268. * raw_event of subclasses. */
  2269. if (ret != 0)
  2270. return ret;
  2271. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
  2272. return wtp_raw_event(hdev, data, size);
  2273. else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
  2274. return m560_raw_event(hdev, data, size);
  2275. return 0;
  2276. }
  2277. static int hidpp_initialize_battery(struct hidpp_device *hidpp)
  2278. {
  2279. static atomic_t battery_no = ATOMIC_INIT(0);
  2280. struct power_supply_config cfg = { .drv_data = hidpp };
  2281. struct power_supply_desc *desc = &hidpp->battery.desc;
  2282. enum power_supply_property *battery_props;
  2283. struct hidpp_battery *battery;
  2284. unsigned int num_battery_props;
  2285. unsigned long n;
  2286. int ret;
  2287. if (hidpp->battery.ps)
  2288. return 0;
  2289. hidpp->battery.feature_index = 0xff;
  2290. hidpp->battery.solar_feature_index = 0xff;
  2291. if (hidpp->protocol_major >= 2) {
  2292. if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
  2293. ret = hidpp_solar_request_battery_event(hidpp);
  2294. else
  2295. ret = hidpp20_query_battery_info(hidpp);
  2296. if (ret)
  2297. return ret;
  2298. hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
  2299. } else {
  2300. ret = hidpp10_query_battery_status(hidpp);
  2301. if (ret) {
  2302. ret = hidpp10_query_battery_mileage(hidpp);
  2303. if (ret)
  2304. return -ENOENT;
  2305. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
  2306. } else {
  2307. hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
  2308. }
  2309. hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
  2310. }
  2311. battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
  2312. hidpp_battery_props,
  2313. sizeof(hidpp_battery_props),
  2314. GFP_KERNEL);
  2315. if (!battery_props)
  2316. return -ENOMEM;
  2317. num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
  2318. if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
  2319. battery_props[num_battery_props++] =
  2320. POWER_SUPPLY_PROP_CAPACITY;
  2321. if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
  2322. battery_props[num_battery_props++] =
  2323. POWER_SUPPLY_PROP_CAPACITY_LEVEL;
  2324. battery = &hidpp->battery;
  2325. n = atomic_inc_return(&battery_no) - 1;
  2326. desc->properties = battery_props;
  2327. desc->num_properties = num_battery_props;
  2328. desc->get_property = hidpp_battery_get_property;
  2329. sprintf(battery->name, "hidpp_battery_%ld", n);
  2330. desc->name = battery->name;
  2331. desc->type = POWER_SUPPLY_TYPE_BATTERY;
  2332. desc->use_for_apm = 0;
  2333. battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
  2334. &battery->desc,
  2335. &cfg);
  2336. if (IS_ERR(battery->ps))
  2337. return PTR_ERR(battery->ps);
  2338. power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
  2339. return ret;
  2340. }
  2341. static void hidpp_overwrite_name(struct hid_device *hdev)
  2342. {
  2343. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2344. char *name;
  2345. if (hidpp->protocol_major < 2)
  2346. return;
  2347. name = hidpp_get_device_name(hidpp);
  2348. if (!name) {
  2349. hid_err(hdev, "unable to retrieve the name of the device");
  2350. } else {
  2351. dbg_hid("HID++: Got name: %s\n", name);
  2352. snprintf(hdev->name, sizeof(hdev->name), "%s", name);
  2353. }
  2354. kfree(name);
  2355. }
  2356. static int hidpp_input_open(struct input_dev *dev)
  2357. {
  2358. struct hid_device *hid = input_get_drvdata(dev);
  2359. return hid_hw_open(hid);
  2360. }
  2361. static void hidpp_input_close(struct input_dev *dev)
  2362. {
  2363. struct hid_device *hid = input_get_drvdata(dev);
  2364. hid_hw_close(hid);
  2365. }
  2366. static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
  2367. {
  2368. struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
  2369. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2370. if (!input_dev)
  2371. return NULL;
  2372. input_set_drvdata(input_dev, hdev);
  2373. input_dev->open = hidpp_input_open;
  2374. input_dev->close = hidpp_input_close;
  2375. input_dev->name = hidpp->name;
  2376. input_dev->phys = hdev->phys;
  2377. input_dev->uniq = hdev->uniq;
  2378. input_dev->id.bustype = hdev->bus;
  2379. input_dev->id.vendor = hdev->vendor;
  2380. input_dev->id.product = hdev->product;
  2381. input_dev->id.version = hdev->version;
  2382. input_dev->dev.parent = &hdev->dev;
  2383. return input_dev;
  2384. }
  2385. static void hidpp_connect_event(struct hidpp_device *hidpp)
  2386. {
  2387. struct hid_device *hdev = hidpp->hid_dev;
  2388. int ret = 0;
  2389. bool connected = atomic_read(&hidpp->connected);
  2390. struct input_dev *input;
  2391. char *name, *devm_name;
  2392. if (!connected) {
  2393. if (hidpp->battery.ps) {
  2394. hidpp->battery.online = false;
  2395. hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
  2396. hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  2397. power_supply_changed(hidpp->battery.ps);
  2398. }
  2399. return;
  2400. }
  2401. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  2402. ret = wtp_connect(hdev, connected);
  2403. if (ret)
  2404. return;
  2405. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
  2406. ret = m560_send_config_command(hdev, connected);
  2407. if (ret)
  2408. return;
  2409. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
  2410. ret = k400_connect(hdev, connected);
  2411. if (ret)
  2412. return;
  2413. }
  2414. /* the device is already connected, we can ask for its name and
  2415. * protocol */
  2416. if (!hidpp->protocol_major) {
  2417. ret = !hidpp_is_connected(hidpp);
  2418. if (ret) {
  2419. hid_err(hdev, "Can not get the protocol version.\n");
  2420. return;
  2421. }
  2422. hid_info(hdev, "HID++ %u.%u device connected.\n",
  2423. hidpp->protocol_major, hidpp->protocol_minor);
  2424. }
  2425. if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
  2426. name = hidpp_get_device_name(hidpp);
  2427. if (!name) {
  2428. hid_err(hdev,
  2429. "unable to retrieve the name of the device");
  2430. return;
  2431. }
  2432. devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
  2433. kfree(name);
  2434. if (!devm_name)
  2435. return;
  2436. hidpp->name = devm_name;
  2437. }
  2438. hidpp_initialize_battery(hidpp);
  2439. /* forward current battery state */
  2440. if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
  2441. hidpp10_enable_battery_reporting(hidpp);
  2442. if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
  2443. hidpp10_query_battery_mileage(hidpp);
  2444. else
  2445. hidpp10_query_battery_status(hidpp);
  2446. } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
  2447. hidpp20_query_battery_info(hidpp);
  2448. }
  2449. if (hidpp->battery.ps)
  2450. power_supply_changed(hidpp->battery.ps);
  2451. if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
  2452. /* if the input nodes are already created, we can stop now */
  2453. return;
  2454. input = hidpp_allocate_input(hdev);
  2455. if (!input) {
  2456. hid_err(hdev, "cannot allocate new input device: %d\n", ret);
  2457. return;
  2458. }
  2459. hidpp_populate_input(hidpp, input, false);
  2460. ret = input_register_device(input);
  2461. if (ret)
  2462. input_free_device(input);
  2463. hidpp->delayed_input = input;
  2464. }
  2465. static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
  2466. static struct attribute *sysfs_attrs[] = {
  2467. &dev_attr_builtin_power_supply.attr,
  2468. NULL
  2469. };
  2470. static const struct attribute_group ps_attribute_group = {
  2471. .attrs = sysfs_attrs
  2472. };
  2473. static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
  2474. {
  2475. struct hidpp_device *hidpp;
  2476. int ret;
  2477. bool connected;
  2478. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  2479. hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
  2480. GFP_KERNEL);
  2481. if (!hidpp)
  2482. return -ENOMEM;
  2483. hidpp->hid_dev = hdev;
  2484. hidpp->name = hdev->name;
  2485. hid_set_drvdata(hdev, hidpp);
  2486. hidpp->quirks = id->driver_data;
  2487. if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
  2488. hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
  2489. if (disable_raw_mode) {
  2490. hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
  2491. hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
  2492. }
  2493. if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
  2494. ret = wtp_allocate(hdev, id);
  2495. if (ret)
  2496. goto allocate_fail;
  2497. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
  2498. ret = m560_allocate(hdev);
  2499. if (ret)
  2500. goto allocate_fail;
  2501. } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
  2502. ret = k400_allocate(hdev);
  2503. if (ret)
  2504. goto allocate_fail;
  2505. }
  2506. INIT_WORK(&hidpp->work, delayed_work_cb);
  2507. mutex_init(&hidpp->send_mutex);
  2508. init_waitqueue_head(&hidpp->wait);
  2509. /* indicates we are handling the battery properties in the kernel */
  2510. ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
  2511. if (ret)
  2512. hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
  2513. hdev->name);
  2514. ret = hid_parse(hdev);
  2515. if (ret) {
  2516. hid_err(hdev, "%s:parse failed\n", __func__);
  2517. goto hid_parse_fail;
  2518. }
  2519. if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
  2520. connect_mask &= ~HID_CONNECT_HIDINPUT;
  2521. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  2522. ret = hid_hw_start(hdev, connect_mask);
  2523. if (ret) {
  2524. hid_err(hdev, "hw start failed\n");
  2525. goto hid_hw_start_fail;
  2526. }
  2527. ret = hid_hw_open(hdev);
  2528. if (ret < 0) {
  2529. dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
  2530. __func__, ret);
  2531. hid_hw_stop(hdev);
  2532. goto hid_hw_start_fail;
  2533. }
  2534. }
  2535. /* Allow incoming packets */
  2536. hid_device_io_start(hdev);
  2537. if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
  2538. hidpp_unifying_init(hidpp);
  2539. connected = hidpp_is_connected(hidpp);
  2540. atomic_set(&hidpp->connected, connected);
  2541. if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
  2542. if (!connected) {
  2543. ret = -ENODEV;
  2544. hid_err(hdev, "Device not connected");
  2545. goto hid_hw_open_failed;
  2546. }
  2547. hid_info(hdev, "HID++ %u.%u device connected.\n",
  2548. hidpp->protocol_major, hidpp->protocol_minor);
  2549. hidpp_overwrite_name(hdev);
  2550. }
  2551. if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
  2552. ret = wtp_get_config(hidpp);
  2553. if (ret)
  2554. goto hid_hw_open_failed;
  2555. } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
  2556. ret = g920_get_config(hidpp);
  2557. if (ret)
  2558. goto hid_hw_open_failed;
  2559. }
  2560. /* Block incoming packets */
  2561. hid_device_io_stop(hdev);
  2562. if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
  2563. ret = hid_hw_start(hdev, connect_mask);
  2564. if (ret) {
  2565. hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
  2566. goto hid_hw_start_fail;
  2567. }
  2568. }
  2569. /* Allow incoming packets */
  2570. hid_device_io_start(hdev);
  2571. hidpp_connect_event(hidpp);
  2572. return ret;
  2573. hid_hw_open_failed:
  2574. hid_device_io_stop(hdev);
  2575. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  2576. hid_hw_close(hdev);
  2577. hid_hw_stop(hdev);
  2578. }
  2579. hid_hw_start_fail:
  2580. hid_parse_fail:
  2581. sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
  2582. cancel_work_sync(&hidpp->work);
  2583. mutex_destroy(&hidpp->send_mutex);
  2584. allocate_fail:
  2585. hid_set_drvdata(hdev, NULL);
  2586. return ret;
  2587. }
  2588. static void hidpp_remove(struct hid_device *hdev)
  2589. {
  2590. struct hidpp_device *hidpp = hid_get_drvdata(hdev);
  2591. sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
  2592. if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
  2593. hidpp_ff_deinit(hdev);
  2594. hid_hw_close(hdev);
  2595. }
  2596. hid_hw_stop(hdev);
  2597. cancel_work_sync(&hidpp->work);
  2598. mutex_destroy(&hidpp->send_mutex);
  2599. }
  2600. static const struct hid_device_id hidpp_devices[] = {
  2601. { /* wireless touchpad */
  2602. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  2603. USB_VENDOR_ID_LOGITECH, 0x4011),
  2604. .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
  2605. HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
  2606. { /* wireless touchpad T650 */
  2607. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  2608. USB_VENDOR_ID_LOGITECH, 0x4101),
  2609. .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
  2610. { /* wireless touchpad T651 */
  2611. HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
  2612. USB_DEVICE_ID_LOGITECH_T651),
  2613. .driver_data = HIDPP_QUIRK_CLASS_WTP },
  2614. { /* Mouse logitech M560 */
  2615. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  2616. USB_VENDOR_ID_LOGITECH, 0x402d),
  2617. .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
  2618. { /* Keyboard logitech K400 */
  2619. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  2620. USB_VENDOR_ID_LOGITECH, 0x4024),
  2621. .driver_data = HIDPP_QUIRK_CLASS_K400 },
  2622. { /* Solar Keyboard Logitech K750 */
  2623. HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  2624. USB_VENDOR_ID_LOGITECH, 0x4002),
  2625. .driver_data = HIDPP_QUIRK_CLASS_K750 },
  2626. { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
  2627. USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
  2628. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
  2629. .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
  2630. {}
  2631. };
  2632. MODULE_DEVICE_TABLE(hid, hidpp_devices);
  2633. static struct hid_driver hidpp_driver = {
  2634. .name = "logitech-hidpp-device",
  2635. .id_table = hidpp_devices,
  2636. .probe = hidpp_probe,
  2637. .remove = hidpp_remove,
  2638. .raw_event = hidpp_raw_event,
  2639. .input_configured = hidpp_input_configured,
  2640. .input_mapping = hidpp_input_mapping,
  2641. .input_mapped = hidpp_input_mapped,
  2642. };
  2643. module_hid_driver(hidpp_driver);