hid-logitech-hidpp.c 84 KB

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