hid-logitech-dj.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HID driver for Logitech receivers
  4. *
  5. * Copyright (c) 2011 Logitech
  6. */
  7. #include <linux/device.h>
  8. #include <linux/hid.h>
  9. #include <linux/module.h>
  10. #include <linux/kfifo.h>
  11. #include <linux/delay.h>
  12. #include <linux/usb.h> /* For to_usb_interface for kvm extra intf check */
  13. #include <asm/unaligned.h>
  14. #include "hid-ids.h"
  15. #define DJ_MAX_PAIRED_DEVICES 7
  16. #define DJ_MAX_NUMBER_NOTIFS 8
  17. #define DJ_RECEIVER_INDEX 0
  18. #define DJ_DEVICE_INDEX_MIN 1
  19. #define DJ_DEVICE_INDEX_MAX 7
  20. #define DJREPORT_SHORT_LENGTH 15
  21. #define DJREPORT_LONG_LENGTH 32
  22. #define REPORT_ID_DJ_SHORT 0x20
  23. #define REPORT_ID_DJ_LONG 0x21
  24. #define REPORT_ID_HIDPP_SHORT 0x10
  25. #define REPORT_ID_HIDPP_LONG 0x11
  26. #define REPORT_ID_HIDPP_VERY_LONG 0x12
  27. #define HIDPP_REPORT_SHORT_LENGTH 7
  28. #define HIDPP_REPORT_LONG_LENGTH 20
  29. #define HIDPP_RECEIVER_INDEX 0xff
  30. #define REPORT_TYPE_RFREPORT_FIRST 0x01
  31. #define REPORT_TYPE_RFREPORT_LAST 0x1F
  32. /* Command Switch to DJ mode */
  33. #define REPORT_TYPE_CMD_SWITCH 0x80
  34. #define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
  35. #define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
  36. #define TIMEOUT_NO_KEEPALIVE 0x00
  37. /* Command to Get the list of Paired devices */
  38. #define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
  39. /* Device Paired Notification */
  40. #define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
  41. #define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
  42. #define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
  43. #define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
  44. #define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
  45. #define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
  46. #define DEVICE_PAIRED_RF_REPORT_TYPE 0x03
  47. /* Device Un-Paired Notification */
  48. #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
  49. /* Connection Status Notification */
  50. #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
  51. #define CONNECTION_STATUS_PARAM_STATUS 0x00
  52. #define STATUS_LINKLOSS 0x01
  53. /* Error Notification */
  54. #define REPORT_TYPE_NOTIF_ERROR 0x7F
  55. #define NOTIF_ERROR_PARAM_ETYPE 0x00
  56. #define ETYPE_KEEPALIVE_TIMEOUT 0x01
  57. /* supported DJ HID && RF report types */
  58. #define REPORT_TYPE_KEYBOARD 0x01
  59. #define REPORT_TYPE_MOUSE 0x02
  60. #define REPORT_TYPE_CONSUMER_CONTROL 0x03
  61. #define REPORT_TYPE_SYSTEM_CONTROL 0x04
  62. #define REPORT_TYPE_MEDIA_CENTER 0x08
  63. #define REPORT_TYPE_LEDS 0x0E
  64. /* RF Report types bitfield */
  65. #define STD_KEYBOARD BIT(1)
  66. #define STD_MOUSE BIT(2)
  67. #define MULTIMEDIA BIT(3)
  68. #define POWER_KEYS BIT(4)
  69. #define MEDIA_CENTER BIT(8)
  70. #define KBD_LEDS BIT(14)
  71. /* Fake (bitnr > NUMBER_OF_HID_REPORTS) bit to track HID++ capability */
  72. #define HIDPP BIT_ULL(63)
  73. /* HID++ Device Connected Notification */
  74. #define REPORT_TYPE_NOTIF_DEVICE_CONNECTED 0x41
  75. #define HIDPP_PARAM_PROTO_TYPE 0x00
  76. #define HIDPP_PARAM_DEVICE_INFO 0x01
  77. #define HIDPP_PARAM_EQUAD_LSB 0x02
  78. #define HIDPP_PARAM_EQUAD_MSB 0x03
  79. #define HIDPP_PARAM_27MHZ_DEVID 0x03
  80. #define HIDPP_DEVICE_TYPE_MASK GENMASK(3, 0)
  81. #define HIDPP_LINK_STATUS_MASK BIT(6)
  82. #define HIDPP_MANUFACTURER_MASK BIT(7)
  83. #define HIDPP_DEVICE_TYPE_KEYBOARD 1
  84. #define HIDPP_DEVICE_TYPE_MOUSE 2
  85. #define HIDPP_SET_REGISTER 0x80
  86. #define HIDPP_GET_LONG_REGISTER 0x83
  87. #define HIDPP_REG_CONNECTION_STATE 0x02
  88. #define HIDPP_REG_PAIRING_INFORMATION 0xB5
  89. #define HIDPP_PAIRING_INFORMATION 0x20
  90. #define HIDPP_FAKE_DEVICE_ARRIVAL 0x02
  91. enum recvr_type {
  92. recvr_type_dj,
  93. recvr_type_hidpp,
  94. recvr_type_gaming_hidpp,
  95. recvr_type_mouse_only,
  96. recvr_type_27mhz,
  97. recvr_type_bluetooth,
  98. };
  99. struct dj_report {
  100. u8 report_id;
  101. u8 device_index;
  102. u8 report_type;
  103. u8 report_params[DJREPORT_SHORT_LENGTH - 3];
  104. };
  105. struct hidpp_event {
  106. u8 report_id;
  107. u8 device_index;
  108. u8 sub_id;
  109. u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
  110. } __packed;
  111. struct dj_receiver_dev {
  112. struct hid_device *mouse;
  113. struct hid_device *keyboard;
  114. struct hid_device *hidpp;
  115. struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
  116. DJ_DEVICE_INDEX_MIN];
  117. struct list_head list;
  118. struct kref kref;
  119. struct work_struct work;
  120. struct kfifo notif_fifo;
  121. unsigned long last_query; /* in jiffies */
  122. bool ready;
  123. enum recvr_type type;
  124. unsigned int unnumbered_application;
  125. spinlock_t lock;
  126. };
  127. struct dj_device {
  128. struct hid_device *hdev;
  129. struct dj_receiver_dev *dj_receiver_dev;
  130. u64 reports_supported;
  131. u8 device_index;
  132. };
  133. #define WORKITEM_TYPE_EMPTY 0
  134. #define WORKITEM_TYPE_PAIRED 1
  135. #define WORKITEM_TYPE_UNPAIRED 2
  136. #define WORKITEM_TYPE_UNKNOWN 255
  137. struct dj_workitem {
  138. u8 type; /* WORKITEM_TYPE_* */
  139. u8 device_index;
  140. u8 device_type;
  141. u8 quad_id_msb;
  142. u8 quad_id_lsb;
  143. u64 reports_supported;
  144. };
  145. /* Keyboard descriptor (1) */
  146. static const char kbd_descriptor[] = {
  147. 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
  148. 0x09, 0x06, /* USAGE (Keyboard) */
  149. 0xA1, 0x01, /* COLLECTION (Application) */
  150. 0x85, 0x01, /* REPORT_ID (1) */
  151. 0x95, 0x08, /* REPORT_COUNT (8) */
  152. 0x75, 0x01, /* REPORT_SIZE (1) */
  153. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  154. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  155. 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
  156. 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
  157. 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
  158. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  159. 0x95, 0x06, /* REPORT_COUNT (6) */
  160. 0x75, 0x08, /* REPORT_SIZE (8) */
  161. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  162. 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
  163. 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
  164. 0x19, 0x00, /* USAGE_MINIMUM (no event) */
  165. 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
  166. 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
  167. 0x85, 0x0e, /* REPORT_ID (14) */
  168. 0x05, 0x08, /* USAGE PAGE (LED page) */
  169. 0x95, 0x05, /* REPORT COUNT (5) */
  170. 0x75, 0x01, /* REPORT SIZE (1) */
  171. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  172. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  173. 0x19, 0x01, /* USAGE MINIMUM (1) */
  174. 0x29, 0x05, /* USAGE MAXIMUM (5) */
  175. 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
  176. 0x95, 0x01, /* REPORT COUNT (1) */
  177. 0x75, 0x03, /* REPORT SIZE (3) */
  178. 0x91, 0x01, /* OUTPUT (Constant) */
  179. 0xC0
  180. };
  181. /* Mouse descriptor (2) */
  182. static const char mse_descriptor[] = {
  183. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  184. 0x09, 0x02, /* USAGE (Mouse) */
  185. 0xA1, 0x01, /* COLLECTION (Application) */
  186. 0x85, 0x02, /* REPORT_ID = 2 */
  187. 0x09, 0x01, /* USAGE (pointer) */
  188. 0xA1, 0x00, /* COLLECTION (physical) */
  189. 0x05, 0x09, /* USAGE_PAGE (buttons) */
  190. 0x19, 0x01, /* USAGE_MIN (1) */
  191. 0x29, 0x10, /* USAGE_MAX (16) */
  192. 0x15, 0x00, /* LOGICAL_MIN (0) */
  193. 0x25, 0x01, /* LOGICAL_MAX (1) */
  194. 0x95, 0x10, /* REPORT_COUNT (16) */
  195. 0x75, 0x01, /* REPORT_SIZE (1) */
  196. 0x81, 0x02, /* INPUT (data var abs) */
  197. 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
  198. 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
  199. 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
  200. 0x75, 0x0C, /* REPORT_SIZE (12) */
  201. 0x95, 0x02, /* REPORT_COUNT (2) */
  202. 0x09, 0x30, /* USAGE (X) */
  203. 0x09, 0x31, /* USAGE (Y) */
  204. 0x81, 0x06, /* INPUT */
  205. 0x15, 0x81, /* LOGICAL_MIN (-127) */
  206. 0x25, 0x7F, /* LOGICAL_MAX (127) */
  207. 0x75, 0x08, /* REPORT_SIZE (8) */
  208. 0x95, 0x01, /* REPORT_COUNT (1) */
  209. 0x09, 0x38, /* USAGE (wheel) */
  210. 0x81, 0x06, /* INPUT */
  211. 0x05, 0x0C, /* USAGE_PAGE(consumer) */
  212. 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
  213. 0x95, 0x01, /* REPORT_COUNT (1) */
  214. 0x81, 0x06, /* INPUT */
  215. 0xC0, /* END_COLLECTION */
  216. 0xC0, /* END_COLLECTION */
  217. };
  218. /* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
  219. static const char mse_27mhz_descriptor[] = {
  220. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  221. 0x09, 0x02, /* USAGE (Mouse) */
  222. 0xA1, 0x01, /* COLLECTION (Application) */
  223. 0x85, 0x02, /* REPORT_ID = 2 */
  224. 0x09, 0x01, /* USAGE (pointer) */
  225. 0xA1, 0x00, /* COLLECTION (physical) */
  226. 0x05, 0x09, /* USAGE_PAGE (buttons) */
  227. 0x19, 0x01, /* USAGE_MIN (1) */
  228. 0x29, 0x08, /* USAGE_MAX (8) */
  229. 0x15, 0x00, /* LOGICAL_MIN (0) */
  230. 0x25, 0x01, /* LOGICAL_MAX (1) */
  231. 0x95, 0x08, /* REPORT_COUNT (8) */
  232. 0x75, 0x01, /* REPORT_SIZE (1) */
  233. 0x81, 0x02, /* INPUT (data var abs) */
  234. 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
  235. 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
  236. 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
  237. 0x75, 0x0C, /* REPORT_SIZE (12) */
  238. 0x95, 0x02, /* REPORT_COUNT (2) */
  239. 0x09, 0x30, /* USAGE (X) */
  240. 0x09, 0x31, /* USAGE (Y) */
  241. 0x81, 0x06, /* INPUT */
  242. 0x15, 0x81, /* LOGICAL_MIN (-127) */
  243. 0x25, 0x7F, /* LOGICAL_MAX (127) */
  244. 0x75, 0x08, /* REPORT_SIZE (8) */
  245. 0x95, 0x01, /* REPORT_COUNT (1) */
  246. 0x09, 0x38, /* USAGE (wheel) */
  247. 0x81, 0x06, /* INPUT */
  248. 0x05, 0x0C, /* USAGE_PAGE(consumer) */
  249. 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
  250. 0x95, 0x01, /* REPORT_COUNT (1) */
  251. 0x81, 0x06, /* INPUT */
  252. 0xC0, /* END_COLLECTION */
  253. 0xC0, /* END_COLLECTION */
  254. };
  255. /* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
  256. static const char mse_bluetooth_descriptor[] = {
  257. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  258. 0x09, 0x02, /* USAGE (Mouse) */
  259. 0xA1, 0x01, /* COLLECTION (Application) */
  260. 0x85, 0x02, /* REPORT_ID = 2 */
  261. 0x09, 0x01, /* USAGE (pointer) */
  262. 0xA1, 0x00, /* COLLECTION (physical) */
  263. 0x05, 0x09, /* USAGE_PAGE (buttons) */
  264. 0x19, 0x01, /* USAGE_MIN (1) */
  265. 0x29, 0x08, /* USAGE_MAX (8) */
  266. 0x15, 0x00, /* LOGICAL_MIN (0) */
  267. 0x25, 0x01, /* LOGICAL_MAX (1) */
  268. 0x95, 0x08, /* REPORT_COUNT (8) */
  269. 0x75, 0x01, /* REPORT_SIZE (1) */
  270. 0x81, 0x02, /* INPUT (data var abs) */
  271. 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
  272. 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
  273. 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
  274. 0x75, 0x0C, /* REPORT_SIZE (12) */
  275. 0x95, 0x02, /* REPORT_COUNT (2) */
  276. 0x09, 0x30, /* USAGE (X) */
  277. 0x09, 0x31, /* USAGE (Y) */
  278. 0x81, 0x06, /* INPUT */
  279. 0x15, 0x81, /* LOGICAL_MIN (-127) */
  280. 0x25, 0x7F, /* LOGICAL_MAX (127) */
  281. 0x75, 0x08, /* REPORT_SIZE (8) */
  282. 0x95, 0x01, /* REPORT_COUNT (1) */
  283. 0x09, 0x38, /* USAGE (wheel) */
  284. 0x81, 0x06, /* INPUT */
  285. 0x05, 0x0C, /* USAGE_PAGE(consumer) */
  286. 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
  287. 0x15, 0xF9, /* LOGICAL_MIN (-7) */
  288. 0x25, 0x07, /* LOGICAL_MAX (7) */
  289. 0x75, 0x04, /* REPORT_SIZE (4) */
  290. 0x95, 0x01, /* REPORT_COUNT (1) */
  291. 0x81, 0x06, /* INPUT */
  292. 0x05, 0x09, /* USAGE_PAGE (buttons) */
  293. 0x19, 0x09, /* USAGE_MIN (9) */
  294. 0x29, 0x0C, /* USAGE_MAX (12) */
  295. 0x15, 0x00, /* LOGICAL_MIN (0) */
  296. 0x25, 0x01, /* LOGICAL_MAX (1) */
  297. 0x75, 0x01, /* REPORT_SIZE (1) */
  298. 0x95, 0x04, /* REPORT_COUNT (4) */
  299. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  300. 0xC0, /* END_COLLECTION */
  301. 0xC0, /* END_COLLECTION */
  302. };
  303. /* Gaming Mouse descriptor (2) */
  304. static const char mse_high_res_descriptor[] = {
  305. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  306. 0x09, 0x02, /* USAGE (Mouse) */
  307. 0xA1, 0x01, /* COLLECTION (Application) */
  308. 0x85, 0x02, /* REPORT_ID = 2 */
  309. 0x09, 0x01, /* USAGE (pointer) */
  310. 0xA1, 0x00, /* COLLECTION (physical) */
  311. 0x05, 0x09, /* USAGE_PAGE (buttons) */
  312. 0x19, 0x01, /* USAGE_MIN (1) */
  313. 0x29, 0x10, /* USAGE_MAX (16) */
  314. 0x15, 0x00, /* LOGICAL_MIN (0) */
  315. 0x25, 0x01, /* LOGICAL_MAX (1) */
  316. 0x95, 0x10, /* REPORT_COUNT (16) */
  317. 0x75, 0x01, /* REPORT_SIZE (1) */
  318. 0x81, 0x02, /* INPUT (data var abs) */
  319. 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
  320. 0x16, 0x01, 0x80, /* LOGICAL_MIN (-32767) */
  321. 0x26, 0xFF, 0x7F, /* LOGICAL_MAX (32767) */
  322. 0x75, 0x10, /* REPORT_SIZE (16) */
  323. 0x95, 0x02, /* REPORT_COUNT (2) */
  324. 0x09, 0x30, /* USAGE (X) */
  325. 0x09, 0x31, /* USAGE (Y) */
  326. 0x81, 0x06, /* INPUT */
  327. 0x15, 0x81, /* LOGICAL_MIN (-127) */
  328. 0x25, 0x7F, /* LOGICAL_MAX (127) */
  329. 0x75, 0x08, /* REPORT_SIZE (8) */
  330. 0x95, 0x01, /* REPORT_COUNT (1) */
  331. 0x09, 0x38, /* USAGE (wheel) */
  332. 0x81, 0x06, /* INPUT */
  333. 0x05, 0x0C, /* USAGE_PAGE(consumer) */
  334. 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
  335. 0x95, 0x01, /* REPORT_COUNT (1) */
  336. 0x81, 0x06, /* INPUT */
  337. 0xC0, /* END_COLLECTION */
  338. 0xC0, /* END_COLLECTION */
  339. };
  340. /* Consumer Control descriptor (3) */
  341. static const char consumer_descriptor[] = {
  342. 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
  343. 0x09, 0x01, /* USAGE (Consumer Control) */
  344. 0xA1, 0x01, /* COLLECTION (Application) */
  345. 0x85, 0x03, /* REPORT_ID = 3 */
  346. 0x75, 0x10, /* REPORT_SIZE (16) */
  347. 0x95, 0x02, /* REPORT_COUNT (2) */
  348. 0x15, 0x01, /* LOGICAL_MIN (1) */
  349. 0x26, 0xFF, 0x02, /* LOGICAL_MAX (767) */
  350. 0x19, 0x01, /* USAGE_MIN (1) */
  351. 0x2A, 0xFF, 0x02, /* USAGE_MAX (767) */
  352. 0x81, 0x00, /* INPUT (Data Ary Abs) */
  353. 0xC0, /* END_COLLECTION */
  354. }; /* */
  355. /* System control descriptor (4) */
  356. static const char syscontrol_descriptor[] = {
  357. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  358. 0x09, 0x80, /* USAGE (System Control) */
  359. 0xA1, 0x01, /* COLLECTION (Application) */
  360. 0x85, 0x04, /* REPORT_ID = 4 */
  361. 0x75, 0x02, /* REPORT_SIZE (2) */
  362. 0x95, 0x01, /* REPORT_COUNT (1) */
  363. 0x15, 0x01, /* LOGICAL_MIN (1) */
  364. 0x25, 0x03, /* LOGICAL_MAX (3) */
  365. 0x09, 0x82, /* USAGE (System Sleep) */
  366. 0x09, 0x81, /* USAGE (System Power Down) */
  367. 0x09, 0x83, /* USAGE (System Wake Up) */
  368. 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
  369. 0x75, 0x06, /* REPORT_SIZE (6) */
  370. 0x81, 0x03, /* INPUT (Cnst Var Abs) */
  371. 0xC0, /* END_COLLECTION */
  372. };
  373. /* Media descriptor (8) */
  374. static const char media_descriptor[] = {
  375. 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
  376. 0x09, 0x88, /* Usage 0x0088 */
  377. 0xa1, 0x01, /* BeginCollection */
  378. 0x85, 0x08, /* Report ID 8 */
  379. 0x19, 0x01, /* Usage Min 0x0001 */
  380. 0x29, 0xff, /* Usage Max 0x00ff */
  381. 0x15, 0x01, /* Logical Min 1 */
  382. 0x26, 0xff, 0x00, /* Logical Max 255 */
  383. 0x75, 0x08, /* Report Size 8 */
  384. 0x95, 0x01, /* Report Count 1 */
  385. 0x81, 0x00, /* Input */
  386. 0xc0, /* EndCollection */
  387. }; /* */
  388. /* HIDPP descriptor */
  389. static const char hidpp_descriptor[] = {
  390. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  391. 0x09, 0x01, /* Usage (Vendor Usage 1) */
  392. 0xa1, 0x01, /* Collection (Application) */
  393. 0x85, 0x10, /* Report ID (16) */
  394. 0x75, 0x08, /* Report Size (8) */
  395. 0x95, 0x06, /* Report Count (6) */
  396. 0x15, 0x00, /* Logical Minimum (0) */
  397. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  398. 0x09, 0x01, /* Usage (Vendor Usage 1) */
  399. 0x81, 0x00, /* Input (Data,Arr,Abs) */
  400. 0x09, 0x01, /* Usage (Vendor Usage 1) */
  401. 0x91, 0x00, /* Output (Data,Arr,Abs) */
  402. 0xc0, /* End Collection */
  403. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  404. 0x09, 0x02, /* Usage (Vendor Usage 2) */
  405. 0xa1, 0x01, /* Collection (Application) */
  406. 0x85, 0x11, /* Report ID (17) */
  407. 0x75, 0x08, /* Report Size (8) */
  408. 0x95, 0x13, /* Report Count (19) */
  409. 0x15, 0x00, /* Logical Minimum (0) */
  410. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  411. 0x09, 0x02, /* Usage (Vendor Usage 2) */
  412. 0x81, 0x00, /* Input (Data,Arr,Abs) */
  413. 0x09, 0x02, /* Usage (Vendor Usage 2) */
  414. 0x91, 0x00, /* Output (Data,Arr,Abs) */
  415. 0xc0, /* End Collection */
  416. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  417. 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
  418. 0xa1, 0x01, /* Collection (Application) */
  419. 0x85, 0x20, /* Report ID (32) */
  420. 0x75, 0x08, /* Report Size (8) */
  421. 0x95, 0x0e, /* Report Count (14) */
  422. 0x15, 0x00, /* Logical Minimum (0) */
  423. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  424. 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
  425. 0x81, 0x00, /* Input (Data,Arr,Abs) */
  426. 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
  427. 0x91, 0x00, /* Output (Data,Arr,Abs) */
  428. 0x85, 0x21, /* Report ID (33) */
  429. 0x95, 0x1f, /* Report Count (31) */
  430. 0x15, 0x00, /* Logical Minimum (0) */
  431. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  432. 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
  433. 0x81, 0x00, /* Input (Data,Arr,Abs) */
  434. 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
  435. 0x91, 0x00, /* Output (Data,Arr,Abs) */
  436. 0xc0, /* End Collection */
  437. };
  438. /* Maximum size of all defined hid reports in bytes (including report id) */
  439. #define MAX_REPORT_SIZE 8
  440. /* Make sure all descriptors are present here */
  441. #define MAX_RDESC_SIZE \
  442. (sizeof(kbd_descriptor) + \
  443. sizeof(mse_bluetooth_descriptor) + \
  444. sizeof(consumer_descriptor) + \
  445. sizeof(syscontrol_descriptor) + \
  446. sizeof(media_descriptor) + \
  447. sizeof(hidpp_descriptor))
  448. /* Number of possible hid report types that can be created by this driver.
  449. *
  450. * Right now, RF report types have the same report types (or report id's)
  451. * than the hid report created from those RF reports. In the future
  452. * this doesnt have to be true.
  453. *
  454. * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
  455. * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
  456. * reports and consumer control, etc. If a new RF report is created, it doesn't
  457. * has to have the same report id as its corresponding hid report, so an
  458. * translation may have to take place for future report types.
  459. */
  460. #define NUMBER_OF_HID_REPORTS 32
  461. static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
  462. [1] = 8, /* Standard keyboard */
  463. [2] = 8, /* Standard mouse */
  464. [3] = 5, /* Consumer control */
  465. [4] = 2, /* System control */
  466. [8] = 2, /* Media Center */
  467. };
  468. #define LOGITECH_DJ_INTERFACE_NUMBER 0x02
  469. static struct hid_ll_driver logi_dj_ll_driver;
  470. static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
  471. static void delayedwork_callback(struct work_struct *work);
  472. static LIST_HEAD(dj_hdev_list);
  473. static DEFINE_MUTEX(dj_hdev_list_lock);
  474. /*
  475. * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
  476. * compatibility they have multiple USB interfaces. On HID++ receivers we need
  477. * to listen for input reports on both interfaces. The functions below are used
  478. * to create a single struct dj_receiver_dev for all interfaces belonging to
  479. * a single USB-device / receiver.
  480. */
  481. static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
  482. enum recvr_type type)
  483. {
  484. struct dj_receiver_dev *djrcv_dev;
  485. char sep;
  486. /*
  487. * The bluetooth receiver contains a built-in hub and has separate
  488. * USB-devices for the keyboard and mouse interfaces.
  489. */
  490. sep = (type == recvr_type_bluetooth) ? '.' : '/';
  491. /* Try to find an already-probed interface from the same device */
  492. list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
  493. if (djrcv_dev->mouse &&
  494. hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
  495. kref_get(&djrcv_dev->kref);
  496. return djrcv_dev;
  497. }
  498. if (djrcv_dev->keyboard &&
  499. hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
  500. kref_get(&djrcv_dev->kref);
  501. return djrcv_dev;
  502. }
  503. if (djrcv_dev->hidpp &&
  504. hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
  505. kref_get(&djrcv_dev->kref);
  506. return djrcv_dev;
  507. }
  508. }
  509. return NULL;
  510. }
  511. static void dj_release_receiver_dev(struct kref *kref)
  512. {
  513. struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
  514. list_del(&djrcv_dev->list);
  515. kfifo_free(&djrcv_dev->notif_fifo);
  516. kfree(djrcv_dev);
  517. }
  518. static void dj_put_receiver_dev(struct hid_device *hdev)
  519. {
  520. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  521. mutex_lock(&dj_hdev_list_lock);
  522. if (djrcv_dev->mouse == hdev)
  523. djrcv_dev->mouse = NULL;
  524. if (djrcv_dev->keyboard == hdev)
  525. djrcv_dev->keyboard = NULL;
  526. if (djrcv_dev->hidpp == hdev)
  527. djrcv_dev->hidpp = NULL;
  528. kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
  529. mutex_unlock(&dj_hdev_list_lock);
  530. }
  531. static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
  532. enum recvr_type type,
  533. unsigned int application,
  534. bool is_hidpp)
  535. {
  536. struct dj_receiver_dev *djrcv_dev;
  537. mutex_lock(&dj_hdev_list_lock);
  538. djrcv_dev = dj_find_receiver_dev(hdev, type);
  539. if (!djrcv_dev) {
  540. djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
  541. if (!djrcv_dev)
  542. goto out;
  543. INIT_WORK(&djrcv_dev->work, delayedwork_callback);
  544. spin_lock_init(&djrcv_dev->lock);
  545. if (kfifo_alloc(&djrcv_dev->notif_fifo,
  546. DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
  547. GFP_KERNEL)) {
  548. kfree(djrcv_dev);
  549. djrcv_dev = NULL;
  550. goto out;
  551. }
  552. kref_init(&djrcv_dev->kref);
  553. list_add_tail(&djrcv_dev->list, &dj_hdev_list);
  554. djrcv_dev->last_query = jiffies;
  555. djrcv_dev->type = type;
  556. }
  557. if (application == HID_GD_KEYBOARD)
  558. djrcv_dev->keyboard = hdev;
  559. if (application == HID_GD_MOUSE)
  560. djrcv_dev->mouse = hdev;
  561. if (is_hidpp)
  562. djrcv_dev->hidpp = hdev;
  563. hid_set_drvdata(hdev, djrcv_dev);
  564. out:
  565. mutex_unlock(&dj_hdev_list_lock);
  566. return djrcv_dev;
  567. }
  568. static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
  569. struct dj_workitem *workitem)
  570. {
  571. /* Called in delayed work context */
  572. struct dj_device *dj_dev;
  573. unsigned long flags;
  574. spin_lock_irqsave(&djrcv_dev->lock, flags);
  575. dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
  576. djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
  577. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  578. if (dj_dev != NULL) {
  579. hid_destroy_device(dj_dev->hdev);
  580. kfree(dj_dev);
  581. } else {
  582. hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
  583. __func__);
  584. }
  585. }
  586. static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
  587. struct dj_workitem *workitem)
  588. {
  589. /* Called in delayed work context */
  590. struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
  591. struct hid_device *dj_hiddev;
  592. struct dj_device *dj_dev;
  593. u8 device_index = workitem->device_index;
  594. unsigned long flags;
  595. /* Device index goes from 1 to 6, we need 3 bytes to store the
  596. * semicolon, the index, and a null terminator
  597. */
  598. unsigned char tmpstr[3];
  599. /* We are the only one ever adding a device, no need to lock */
  600. if (djrcv_dev->paired_dj_devices[device_index]) {
  601. /* The device is already known. No need to reallocate it. */
  602. dbg_hid("%s: device is already known\n", __func__);
  603. return;
  604. }
  605. dj_hiddev = hid_allocate_device();
  606. if (IS_ERR(dj_hiddev)) {
  607. hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
  608. return;
  609. }
  610. dj_hiddev->ll_driver = &logi_dj_ll_driver;
  611. dj_hiddev->dev.parent = &djrcv_hdev->dev;
  612. dj_hiddev->bus = BUS_USB;
  613. dj_hiddev->vendor = djrcv_hdev->vendor;
  614. dj_hiddev->product = (workitem->quad_id_msb << 8) |
  615. workitem->quad_id_lsb;
  616. if (workitem->device_type) {
  617. const char *type_str = "Device";
  618. switch (workitem->device_type) {
  619. case 0x01: type_str = "Keyboard"; break;
  620. case 0x02: type_str = "Mouse"; break;
  621. case 0x03: type_str = "Numpad"; break;
  622. case 0x04: type_str = "Presenter"; break;
  623. case 0x07: type_str = "Remote Control"; break;
  624. case 0x08: type_str = "Trackball"; break;
  625. case 0x09: type_str = "Touchpad"; break;
  626. }
  627. snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
  628. "Logitech Wireless %s PID:%04x",
  629. type_str, dj_hiddev->product);
  630. } else {
  631. snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
  632. "Logitech Wireless Device PID:%04x",
  633. dj_hiddev->product);
  634. }
  635. if (djrcv_dev->type == recvr_type_27mhz)
  636. dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
  637. else
  638. dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
  639. memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
  640. snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
  641. strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
  642. dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
  643. if (!dj_dev) {
  644. hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
  645. goto dj_device_allocate_fail;
  646. }
  647. dj_dev->reports_supported = workitem->reports_supported;
  648. dj_dev->hdev = dj_hiddev;
  649. dj_dev->dj_receiver_dev = djrcv_dev;
  650. dj_dev->device_index = device_index;
  651. dj_hiddev->driver_data = dj_dev;
  652. spin_lock_irqsave(&djrcv_dev->lock, flags);
  653. djrcv_dev->paired_dj_devices[device_index] = dj_dev;
  654. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  655. if (hid_add_device(dj_hiddev)) {
  656. hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
  657. goto hid_add_device_fail;
  658. }
  659. return;
  660. hid_add_device_fail:
  661. spin_lock_irqsave(&djrcv_dev->lock, flags);
  662. djrcv_dev->paired_dj_devices[device_index] = NULL;
  663. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  664. kfree(dj_dev);
  665. dj_device_allocate_fail:
  666. hid_destroy_device(dj_hiddev);
  667. }
  668. static void delayedwork_callback(struct work_struct *work)
  669. {
  670. struct dj_receiver_dev *djrcv_dev =
  671. container_of(work, struct dj_receiver_dev, work);
  672. struct dj_workitem workitem;
  673. unsigned long flags;
  674. int count;
  675. int retval;
  676. dbg_hid("%s\n", __func__);
  677. spin_lock_irqsave(&djrcv_dev->lock, flags);
  678. /*
  679. * Since we attach to multiple interfaces, we may get scheduled before
  680. * we are bound to the HID++ interface, catch this.
  681. */
  682. if (!djrcv_dev->ready) {
  683. pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
  684. __func__);
  685. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  686. return;
  687. }
  688. count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
  689. if (count != sizeof(workitem)) {
  690. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  691. return;
  692. }
  693. if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
  694. schedule_work(&djrcv_dev->work);
  695. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  696. switch (workitem.type) {
  697. case WORKITEM_TYPE_PAIRED:
  698. logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
  699. break;
  700. case WORKITEM_TYPE_UNPAIRED:
  701. logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
  702. break;
  703. case WORKITEM_TYPE_UNKNOWN:
  704. retval = logi_dj_recv_query_paired_devices(djrcv_dev);
  705. if (retval) {
  706. hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
  707. __func__, retval);
  708. }
  709. break;
  710. case WORKITEM_TYPE_EMPTY:
  711. dbg_hid("%s: device list is empty\n", __func__);
  712. break;
  713. }
  714. }
  715. /*
  716. * Sometimes we receive reports for which we do not have a paired dj_device
  717. * associated with the device_index or report-type to forward the report to.
  718. * This means that the original "device paired" notification corresponding
  719. * to the dj_device never arrived to this driver. Possible reasons for this are:
  720. * 1) hid-core discards all packets coming from a device during probe().
  721. * 2) if the receiver is plugged into a KVM switch then the pairing reports
  722. * are only forwarded to it if the focus is on this PC.
  723. * This function deals with this by re-asking the receiver for the list of
  724. * connected devices in the delayed work callback.
  725. * This function MUST be called with djrcv->lock held.
  726. */
  727. static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
  728. {
  729. struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
  730. /* Rate limit queries done because of unhandled reports to 2/sec */
  731. if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
  732. return;
  733. kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
  734. schedule_work(&djrcv_dev->work);
  735. }
  736. static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
  737. struct dj_report *dj_report)
  738. {
  739. /* We are called from atomic context (tasklet && djrcv->lock held) */
  740. struct dj_workitem workitem = {
  741. .device_index = dj_report->device_index,
  742. };
  743. switch (dj_report->report_type) {
  744. case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
  745. workitem.type = WORKITEM_TYPE_PAIRED;
  746. if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
  747. SPFUNCTION_DEVICE_LIST_EMPTY) {
  748. workitem.type = WORKITEM_TYPE_EMPTY;
  749. break;
  750. }
  751. fallthrough;
  752. case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
  753. workitem.quad_id_msb =
  754. dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
  755. workitem.quad_id_lsb =
  756. dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
  757. workitem.reports_supported = get_unaligned_le32(
  758. dj_report->report_params +
  759. DEVICE_PAIRED_RF_REPORT_TYPE);
  760. workitem.reports_supported |= HIDPP;
  761. if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
  762. workitem.type = WORKITEM_TYPE_UNPAIRED;
  763. break;
  764. default:
  765. logi_dj_recv_queue_unknown_work(djrcv_dev);
  766. return;
  767. }
  768. kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
  769. schedule_work(&djrcv_dev->work);
  770. }
  771. /*
  772. * Some quad/bluetooth keyboards have a builtin touchpad in this case we see
  773. * only 1 paired device with a device_type of REPORT_TYPE_KEYBOARD. For the
  774. * touchpad to work we must also forward mouse input reports to the dj_hiddev
  775. * created for the keyboard (instead of forwarding them to a second paired
  776. * device with a device_type of REPORT_TYPE_MOUSE as we normally would).
  777. */
  778. static const u16 kbd_builtin_touchpad_ids[] = {
  779. 0xb309, /* Dinovo Edge */
  780. 0xb30c, /* Dinovo Mini */
  781. };
  782. static void logi_hidpp_dev_conn_notif_equad(struct hid_device *hdev,
  783. struct hidpp_event *hidpp_report,
  784. struct dj_workitem *workitem)
  785. {
  786. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  787. int i, id;
  788. workitem->type = WORKITEM_TYPE_PAIRED;
  789. workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
  790. HIDPP_DEVICE_TYPE_MASK;
  791. workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
  792. workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
  793. switch (workitem->device_type) {
  794. case REPORT_TYPE_KEYBOARD:
  795. workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
  796. POWER_KEYS | MEDIA_CENTER |
  797. HIDPP;
  798. id = (workitem->quad_id_msb << 8) | workitem->quad_id_lsb;
  799. for (i = 0; i < ARRAY_SIZE(kbd_builtin_touchpad_ids); i++) {
  800. if (id == kbd_builtin_touchpad_ids[i]) {
  801. workitem->reports_supported |= STD_MOUSE;
  802. break;
  803. }
  804. }
  805. break;
  806. case REPORT_TYPE_MOUSE:
  807. workitem->reports_supported |= STD_MOUSE | HIDPP;
  808. if (djrcv_dev->type == recvr_type_mouse_only)
  809. workitem->reports_supported |= MULTIMEDIA;
  810. break;
  811. }
  812. }
  813. static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
  814. struct hidpp_event *hidpp_report,
  815. struct dj_workitem *workitem)
  816. {
  817. workitem->type = WORKITEM_TYPE_PAIRED;
  818. workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
  819. switch (hidpp_report->device_index) {
  820. case 1: /* Index 1 is always a mouse */
  821. case 2: /* Index 2 is always a mouse */
  822. workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
  823. workitem->reports_supported |= STD_MOUSE | HIDPP;
  824. break;
  825. case 3: /* Index 3 is always the keyboard */
  826. case 4: /* Index 4 is used for an optional separate numpad */
  827. workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
  828. workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
  829. POWER_KEYS | HIDPP;
  830. break;
  831. default:
  832. hid_warn(hdev, "%s: unexpected device-index %d", __func__,
  833. hidpp_report->device_index);
  834. }
  835. }
  836. static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
  837. struct hidpp_event *hidpp_report)
  838. {
  839. /* We are called from atomic context (tasklet && djrcv->lock held) */
  840. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  841. const char *device_type = "UNKNOWN";
  842. struct dj_workitem workitem = {
  843. .type = WORKITEM_TYPE_EMPTY,
  844. .device_index = hidpp_report->device_index,
  845. };
  846. switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
  847. case 0x01:
  848. device_type = "Bluetooth";
  849. /* Bluetooth connect packet contents is the same as (e)QUAD */
  850. logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
  851. if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
  852. HIDPP_MANUFACTURER_MASK)) {
  853. hid_info(hdev, "Non Logitech device connected on slot %d\n",
  854. hidpp_report->device_index);
  855. workitem.reports_supported &= ~HIDPP;
  856. }
  857. break;
  858. case 0x02:
  859. device_type = "27 Mhz";
  860. logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
  861. break;
  862. case 0x03:
  863. device_type = "QUAD or eQUAD";
  864. logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
  865. break;
  866. case 0x04:
  867. device_type = "eQUAD step 4 DJ";
  868. logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
  869. break;
  870. case 0x05:
  871. device_type = "DFU Lite";
  872. break;
  873. case 0x06:
  874. device_type = "eQUAD step 4 Lite";
  875. logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
  876. break;
  877. case 0x07:
  878. device_type = "eQUAD step 4 Gaming";
  879. logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
  880. workitem.reports_supported |= STD_KEYBOARD;
  881. break;
  882. case 0x08:
  883. device_type = "eQUAD step 4 for gamepads";
  884. break;
  885. case 0x0a:
  886. device_type = "eQUAD nano Lite";
  887. logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
  888. break;
  889. case 0x0c:
  890. device_type = "eQUAD Lightspeed 1";
  891. logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
  892. workitem.reports_supported |= STD_KEYBOARD;
  893. break;
  894. case 0x0d:
  895. device_type = "eQUAD Lightspeed 1.1";
  896. logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
  897. workitem.reports_supported |= STD_KEYBOARD;
  898. break;
  899. case 0x0f:
  900. case 0x11:
  901. device_type = "eQUAD Lightspeed 1.2";
  902. logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
  903. workitem.reports_supported |= STD_KEYBOARD;
  904. break;
  905. }
  906. /* custom receiver device (eg. powerplay) */
  907. if (hidpp_report->device_index == 7) {
  908. workitem.reports_supported |= HIDPP;
  909. }
  910. if (workitem.type == WORKITEM_TYPE_EMPTY) {
  911. hid_warn(hdev,
  912. "unusable device of type %s (0x%02x) connected on slot %d",
  913. device_type,
  914. hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
  915. hidpp_report->device_index);
  916. return;
  917. }
  918. hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
  919. device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
  920. hidpp_report->device_index);
  921. kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
  922. schedule_work(&djrcv_dev->work);
  923. }
  924. static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
  925. struct dj_report *dj_report)
  926. {
  927. /* We are called from atomic context (tasklet && djrcv->lock held) */
  928. unsigned int i;
  929. u8 reportbuffer[MAX_REPORT_SIZE];
  930. struct dj_device *djdev;
  931. djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
  932. memset(reportbuffer, 0, sizeof(reportbuffer));
  933. for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
  934. if (djdev->reports_supported & (1 << i)) {
  935. reportbuffer[0] = i;
  936. if (hid_input_report(djdev->hdev,
  937. HID_INPUT_REPORT,
  938. reportbuffer,
  939. hid_reportid_size_map[i], 1)) {
  940. dbg_hid("hid_input_report error sending null "
  941. "report\n");
  942. }
  943. }
  944. }
  945. }
  946. static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
  947. struct dj_report *dj_report)
  948. {
  949. /* We are called from atomic context (tasklet && djrcv->lock held) */
  950. struct dj_device *dj_device;
  951. dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
  952. if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
  953. (hid_reportid_size_map[dj_report->report_type] == 0)) {
  954. dbg_hid("invalid report type:%x\n", dj_report->report_type);
  955. return;
  956. }
  957. if (hid_input_report(dj_device->hdev,
  958. HID_INPUT_REPORT, &dj_report->report_type,
  959. hid_reportid_size_map[dj_report->report_type], 1)) {
  960. dbg_hid("hid_input_report error\n");
  961. }
  962. }
  963. static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
  964. int size)
  965. {
  966. /* We are called from atomic context (tasklet && djrcv->lock held) */
  967. if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
  968. dbg_hid("hid_input_report error\n");
  969. }
  970. static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
  971. u8 *data, int size)
  972. {
  973. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  974. struct dj_device *dj_dev;
  975. unsigned long flags;
  976. u8 report = data[0];
  977. int i;
  978. if (report > REPORT_TYPE_RFREPORT_LAST) {
  979. hid_err(hdev, "Unexpected input report number %d\n", report);
  980. return;
  981. }
  982. spin_lock_irqsave(&djrcv_dev->lock, flags);
  983. for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
  984. dj_dev = djrcv_dev->paired_dj_devices[i];
  985. if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
  986. logi_dj_recv_forward_report(dj_dev, data, size);
  987. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  988. return;
  989. }
  990. }
  991. logi_dj_recv_queue_unknown_work(djrcv_dev);
  992. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  993. dbg_hid("No dj-devs handling input report number %d\n", report);
  994. }
  995. static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
  996. struct dj_report *dj_report)
  997. {
  998. struct hid_device *hdev = djrcv_dev->hidpp;
  999. struct hid_report *report;
  1000. struct hid_report_enum *output_report_enum;
  1001. u8 *data = (u8 *)(&dj_report->device_index);
  1002. unsigned int i;
  1003. output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
  1004. report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
  1005. if (!report) {
  1006. hid_err(hdev, "%s: unable to find dj report\n", __func__);
  1007. return -ENODEV;
  1008. }
  1009. for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
  1010. report->field[0]->value[i] = data[i];
  1011. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  1012. return 0;
  1013. }
  1014. static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
  1015. {
  1016. static const u8 template[] = {
  1017. REPORT_ID_HIDPP_SHORT,
  1018. HIDPP_RECEIVER_INDEX,
  1019. HIDPP_SET_REGISTER,
  1020. HIDPP_REG_CONNECTION_STATE,
  1021. HIDPP_FAKE_DEVICE_ARRIVAL,
  1022. 0x00, 0x00
  1023. };
  1024. u8 *hidpp_report;
  1025. int retval;
  1026. hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
  1027. if (!hidpp_report)
  1028. return -ENOMEM;
  1029. retval = hid_hw_raw_request(djrcv_dev->hidpp,
  1030. REPORT_ID_HIDPP_SHORT,
  1031. hidpp_report, sizeof(template),
  1032. HID_OUTPUT_REPORT,
  1033. HID_REQ_SET_REPORT);
  1034. kfree(hidpp_report);
  1035. return (retval < 0) ? retval : 0;
  1036. }
  1037. static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
  1038. {
  1039. struct dj_report *dj_report;
  1040. int retval;
  1041. djrcv_dev->last_query = jiffies;
  1042. if (djrcv_dev->type != recvr_type_dj)
  1043. return logi_dj_recv_query_hidpp_devices(djrcv_dev);
  1044. dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
  1045. if (!dj_report)
  1046. return -ENOMEM;
  1047. dj_report->report_id = REPORT_ID_DJ_SHORT;
  1048. dj_report->device_index = HIDPP_RECEIVER_INDEX;
  1049. dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
  1050. retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
  1051. kfree(dj_report);
  1052. return retval;
  1053. }
  1054. static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
  1055. unsigned timeout)
  1056. {
  1057. struct hid_device *hdev = djrcv_dev->hidpp;
  1058. struct dj_report *dj_report;
  1059. u8 *buf;
  1060. int retval = 0;
  1061. dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
  1062. if (!dj_report)
  1063. return -ENOMEM;
  1064. if (djrcv_dev->type == recvr_type_dj) {
  1065. dj_report->report_id = REPORT_ID_DJ_SHORT;
  1066. dj_report->device_index = HIDPP_RECEIVER_INDEX;
  1067. dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
  1068. dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
  1069. dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
  1070. (u8)timeout;
  1071. retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
  1072. /*
  1073. * Ugly sleep to work around a USB 3.0 bug when the receiver is
  1074. * still processing the "switch-to-dj" command while we send an
  1075. * other command.
  1076. * 50 msec should gives enough time to the receiver to be ready.
  1077. */
  1078. msleep(50);
  1079. }
  1080. /*
  1081. * Magical bits to set up hidpp notifications when the dj devices
  1082. * are connected/disconnected.
  1083. *
  1084. * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
  1085. * than DJREPORT_SHORT_LENGTH.
  1086. */
  1087. buf = (u8 *)dj_report;
  1088. memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
  1089. buf[0] = REPORT_ID_HIDPP_SHORT;
  1090. buf[1] = HIDPP_RECEIVER_INDEX;
  1091. buf[2] = 0x80;
  1092. buf[3] = 0x00;
  1093. buf[4] = 0x00;
  1094. buf[5] = 0x09;
  1095. buf[6] = 0x00;
  1096. hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
  1097. HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
  1098. HID_REQ_SET_REPORT);
  1099. kfree(dj_report);
  1100. return retval;
  1101. }
  1102. static int logi_dj_ll_open(struct hid_device *hid)
  1103. {
  1104. dbg_hid("%s: %s\n", __func__, hid->phys);
  1105. return 0;
  1106. }
  1107. static void logi_dj_ll_close(struct hid_device *hid)
  1108. {
  1109. dbg_hid("%s: %s\n", __func__, hid->phys);
  1110. }
  1111. /*
  1112. * Register 0xB5 is "pairing information". It is solely intended for the
  1113. * receiver, so do not overwrite the device index.
  1114. */
  1115. static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
  1116. HIDPP_RECEIVER_INDEX,
  1117. HIDPP_GET_LONG_REGISTER,
  1118. HIDPP_REG_PAIRING_INFORMATION };
  1119. static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
  1120. HIDPP_RECEIVER_INDEX,
  1121. HIDPP_GET_LONG_REGISTER,
  1122. HIDPP_REG_PAIRING_INFORMATION };
  1123. static int logi_dj_ll_raw_request(struct hid_device *hid,
  1124. unsigned char reportnum, __u8 *buf,
  1125. size_t count, unsigned char report_type,
  1126. int reqtype)
  1127. {
  1128. struct dj_device *djdev = hid->driver_data;
  1129. struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
  1130. u8 *out_buf;
  1131. int ret;
  1132. if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
  1133. (buf[0] == REPORT_ID_HIDPP_LONG) ||
  1134. (buf[0] == REPORT_ID_HIDPP_VERY_LONG)) {
  1135. if (count < 2)
  1136. return -EINVAL;
  1137. /* special case where we should not overwrite
  1138. * the device_index */
  1139. if (count == 7 && !memcmp(buf, unifying_pairing_query,
  1140. sizeof(unifying_pairing_query)))
  1141. buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
  1142. else
  1143. buf[1] = djdev->device_index;
  1144. return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
  1145. count, report_type, reqtype);
  1146. }
  1147. if (buf[0] != REPORT_TYPE_LEDS)
  1148. return -EINVAL;
  1149. if (djrcv_dev->type != recvr_type_dj && count >= 2) {
  1150. if (!djrcv_dev->keyboard) {
  1151. hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
  1152. return 0;
  1153. }
  1154. /* usbhid overrides the report ID and ignores the first byte */
  1155. return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
  1156. report_type, reqtype);
  1157. }
  1158. out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
  1159. if (!out_buf)
  1160. return -ENOMEM;
  1161. if (count > DJREPORT_SHORT_LENGTH - 2)
  1162. count = DJREPORT_SHORT_LENGTH - 2;
  1163. out_buf[0] = REPORT_ID_DJ_SHORT;
  1164. out_buf[1] = djdev->device_index;
  1165. memcpy(out_buf + 2, buf, count);
  1166. ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
  1167. DJREPORT_SHORT_LENGTH, report_type, reqtype);
  1168. kfree(out_buf);
  1169. return ret;
  1170. }
  1171. static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
  1172. {
  1173. memcpy(rdesc + *rsize, data, size);
  1174. *rsize += size;
  1175. }
  1176. static int logi_dj_ll_parse(struct hid_device *hid)
  1177. {
  1178. struct dj_device *djdev = hid->driver_data;
  1179. unsigned int rsize = 0;
  1180. char *rdesc;
  1181. int retval;
  1182. dbg_hid("%s\n", __func__);
  1183. djdev->hdev->version = 0x0111;
  1184. djdev->hdev->country = 0x00;
  1185. rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
  1186. if (!rdesc)
  1187. return -ENOMEM;
  1188. if (djdev->reports_supported & STD_KEYBOARD) {
  1189. dbg_hid("%s: sending a kbd descriptor, reports_supported: %llx\n",
  1190. __func__, djdev->reports_supported);
  1191. rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
  1192. }
  1193. if (djdev->reports_supported & STD_MOUSE) {
  1194. dbg_hid("%s: sending a mouse descriptor, reports_supported: %llx\n",
  1195. __func__, djdev->reports_supported);
  1196. if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp ||
  1197. djdev->dj_receiver_dev->type == recvr_type_mouse_only)
  1198. rdcat(rdesc, &rsize, mse_high_res_descriptor,
  1199. sizeof(mse_high_res_descriptor));
  1200. else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
  1201. rdcat(rdesc, &rsize, mse_27mhz_descriptor,
  1202. sizeof(mse_27mhz_descriptor));
  1203. else if (djdev->dj_receiver_dev->type == recvr_type_bluetooth)
  1204. rdcat(rdesc, &rsize, mse_bluetooth_descriptor,
  1205. sizeof(mse_bluetooth_descriptor));
  1206. else
  1207. rdcat(rdesc, &rsize, mse_descriptor,
  1208. sizeof(mse_descriptor));
  1209. }
  1210. if (djdev->reports_supported & MULTIMEDIA) {
  1211. dbg_hid("%s: sending a multimedia report descriptor: %llx\n",
  1212. __func__, djdev->reports_supported);
  1213. rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
  1214. }
  1215. if (djdev->reports_supported & POWER_KEYS) {
  1216. dbg_hid("%s: sending a power keys report descriptor: %llx\n",
  1217. __func__, djdev->reports_supported);
  1218. rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
  1219. }
  1220. if (djdev->reports_supported & MEDIA_CENTER) {
  1221. dbg_hid("%s: sending a media center report descriptor: %llx\n",
  1222. __func__, djdev->reports_supported);
  1223. rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
  1224. }
  1225. if (djdev->reports_supported & KBD_LEDS) {
  1226. dbg_hid("%s: need to send kbd leds report descriptor: %llx\n",
  1227. __func__, djdev->reports_supported);
  1228. }
  1229. if (djdev->reports_supported & HIDPP) {
  1230. dbg_hid("%s: sending a HID++ descriptor, reports_supported: %llx\n",
  1231. __func__, djdev->reports_supported);
  1232. rdcat(rdesc, &rsize, hidpp_descriptor,
  1233. sizeof(hidpp_descriptor));
  1234. }
  1235. retval = hid_parse_report(hid, rdesc, rsize);
  1236. kfree(rdesc);
  1237. return retval;
  1238. }
  1239. static int logi_dj_ll_start(struct hid_device *hid)
  1240. {
  1241. dbg_hid("%s\n", __func__);
  1242. return 0;
  1243. }
  1244. static void logi_dj_ll_stop(struct hid_device *hid)
  1245. {
  1246. dbg_hid("%s\n", __func__);
  1247. }
  1248. static struct hid_ll_driver logi_dj_ll_driver = {
  1249. .parse = logi_dj_ll_parse,
  1250. .start = logi_dj_ll_start,
  1251. .stop = logi_dj_ll_stop,
  1252. .open = logi_dj_ll_open,
  1253. .close = logi_dj_ll_close,
  1254. .raw_request = logi_dj_ll_raw_request,
  1255. };
  1256. static int logi_dj_dj_event(struct hid_device *hdev,
  1257. struct hid_report *report, u8 *data,
  1258. int size)
  1259. {
  1260. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  1261. struct dj_report *dj_report = (struct dj_report *) data;
  1262. unsigned long flags;
  1263. /*
  1264. * Here we receive all data coming from iface 2, there are 3 cases:
  1265. *
  1266. * 1) Data is intended for this driver i. e. data contains arrival,
  1267. * departure, etc notifications, in which case we queue them for delayed
  1268. * processing by the work queue. We return 1 to hid-core as no further
  1269. * processing is required from it.
  1270. *
  1271. * 2) Data informs a connection change, if the change means rf link
  1272. * loss, then we must send a null report to the upper layer to discard
  1273. * potentially pressed keys that may be repeated forever by the input
  1274. * layer. Return 1 to hid-core as no further processing is required.
  1275. *
  1276. * 3) Data is an actual input event from a paired DJ device in which
  1277. * case we forward it to the correct hid device (via hid_input_report()
  1278. * ) and return 1 so hid-core does not anything else with it.
  1279. */
  1280. if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
  1281. (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
  1282. /*
  1283. * Device index is wrong, bail out.
  1284. * This driver can ignore safely the receiver notifications,
  1285. * so ignore those reports too.
  1286. */
  1287. if (dj_report->device_index != DJ_RECEIVER_INDEX)
  1288. hid_err(hdev, "%s: invalid device index:%d\n",
  1289. __func__, dj_report->device_index);
  1290. return false;
  1291. }
  1292. spin_lock_irqsave(&djrcv_dev->lock, flags);
  1293. if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
  1294. /* received an event for an unknown device, bail out */
  1295. logi_dj_recv_queue_notification(djrcv_dev, dj_report);
  1296. goto out;
  1297. }
  1298. switch (dj_report->report_type) {
  1299. case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
  1300. /* pairing notifications are handled above the switch */
  1301. break;
  1302. case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
  1303. logi_dj_recv_queue_notification(djrcv_dev, dj_report);
  1304. break;
  1305. case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
  1306. if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
  1307. STATUS_LINKLOSS) {
  1308. logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
  1309. }
  1310. break;
  1311. default:
  1312. logi_dj_recv_forward_dj(djrcv_dev, dj_report);
  1313. }
  1314. out:
  1315. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  1316. return true;
  1317. }
  1318. static int logi_dj_hidpp_event(struct hid_device *hdev,
  1319. struct hid_report *report, u8 *data,
  1320. int size)
  1321. {
  1322. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  1323. struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
  1324. struct dj_device *dj_dev;
  1325. unsigned long flags;
  1326. u8 device_index = hidpp_report->device_index;
  1327. if (device_index == HIDPP_RECEIVER_INDEX) {
  1328. /* special case were the device wants to know its unifying
  1329. * name */
  1330. if (size == HIDPP_REPORT_LONG_LENGTH &&
  1331. !memcmp(data, unifying_pairing_answer,
  1332. sizeof(unifying_pairing_answer)))
  1333. device_index = (data[4] & 0x0F) + 1;
  1334. else
  1335. return false;
  1336. }
  1337. /*
  1338. * Data is from the HID++ collection, in this case, we forward the
  1339. * data to the corresponding child dj device and return 0 to hid-core
  1340. * so he data also goes to the hidraw device of the receiver. This
  1341. * allows a user space application to implement the full HID++ routing
  1342. * via the receiver.
  1343. */
  1344. if ((device_index < DJ_DEVICE_INDEX_MIN) ||
  1345. (device_index > DJ_DEVICE_INDEX_MAX)) {
  1346. /*
  1347. * Device index is wrong, bail out.
  1348. * This driver can ignore safely the receiver notifications,
  1349. * so ignore those reports too.
  1350. */
  1351. hid_err(hdev, "%s: invalid device index:%d\n", __func__,
  1352. hidpp_report->device_index);
  1353. return false;
  1354. }
  1355. spin_lock_irqsave(&djrcv_dev->lock, flags);
  1356. dj_dev = djrcv_dev->paired_dj_devices[device_index];
  1357. /*
  1358. * With 27 MHz receivers, we do not get an explicit unpair event,
  1359. * remove the old device if the user has paired a *different* device.
  1360. */
  1361. if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
  1362. hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
  1363. hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
  1364. hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
  1365. dj_dev->hdev->product) {
  1366. struct dj_workitem workitem = {
  1367. .device_index = hidpp_report->device_index,
  1368. .type = WORKITEM_TYPE_UNPAIRED,
  1369. };
  1370. kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
  1371. /* logi_hidpp_recv_queue_notif will queue the work */
  1372. dj_dev = NULL;
  1373. }
  1374. if (dj_dev) {
  1375. logi_dj_recv_forward_report(dj_dev, data, size);
  1376. } else {
  1377. if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
  1378. logi_hidpp_recv_queue_notif(hdev, hidpp_report);
  1379. else
  1380. logi_dj_recv_queue_unknown_work(djrcv_dev);
  1381. }
  1382. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  1383. return false;
  1384. }
  1385. static int logi_dj_raw_event(struct hid_device *hdev,
  1386. struct hid_report *report, u8 *data,
  1387. int size)
  1388. {
  1389. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  1390. dbg_hid("%s, size:%d\n", __func__, size);
  1391. if (!djrcv_dev)
  1392. return 0;
  1393. if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
  1394. if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
  1395. /*
  1396. * For the keyboard, we can reuse the same report by
  1397. * using the second byte which is constant in the USB
  1398. * HID report descriptor.
  1399. */
  1400. data[1] = data[0];
  1401. data[0] = REPORT_TYPE_KEYBOARD;
  1402. logi_dj_recv_forward_input_report(hdev, data, size);
  1403. /* restore previous state */
  1404. data[0] = data[1];
  1405. data[1] = 0;
  1406. }
  1407. /*
  1408. * Mouse-only receivers send unnumbered mouse data. The 27 MHz
  1409. * receiver uses 6 byte packets, the nano receiver 8 bytes.
  1410. */
  1411. if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
  1412. size <= 8) {
  1413. u8 mouse_report[9];
  1414. /* Prepend report id */
  1415. mouse_report[0] = REPORT_TYPE_MOUSE;
  1416. memcpy(mouse_report + 1, data, size);
  1417. logi_dj_recv_forward_input_report(hdev, mouse_report,
  1418. size + 1);
  1419. }
  1420. return false;
  1421. }
  1422. switch (data[0]) {
  1423. case REPORT_ID_DJ_SHORT:
  1424. if (size != DJREPORT_SHORT_LENGTH) {
  1425. hid_err(hdev, "Short DJ report bad size (%d)", size);
  1426. return false;
  1427. }
  1428. return logi_dj_dj_event(hdev, report, data, size);
  1429. case REPORT_ID_DJ_LONG:
  1430. if (size != DJREPORT_LONG_LENGTH) {
  1431. hid_err(hdev, "Long DJ report bad size (%d)", size);
  1432. return false;
  1433. }
  1434. return logi_dj_dj_event(hdev, report, data, size);
  1435. case REPORT_ID_HIDPP_SHORT:
  1436. if (size != HIDPP_REPORT_SHORT_LENGTH) {
  1437. hid_err(hdev, "Short HID++ report bad size (%d)", size);
  1438. return false;
  1439. }
  1440. return logi_dj_hidpp_event(hdev, report, data, size);
  1441. case REPORT_ID_HIDPP_LONG:
  1442. if (size != HIDPP_REPORT_LONG_LENGTH) {
  1443. hid_err(hdev, "Long HID++ report bad size (%d)", size);
  1444. return false;
  1445. }
  1446. return logi_dj_hidpp_event(hdev, report, data, size);
  1447. }
  1448. logi_dj_recv_forward_input_report(hdev, data, size);
  1449. return false;
  1450. }
  1451. static int logi_dj_probe(struct hid_device *hdev,
  1452. const struct hid_device_id *id)
  1453. {
  1454. struct hid_report_enum *rep_enum;
  1455. struct hid_report *rep;
  1456. struct dj_receiver_dev *djrcv_dev;
  1457. struct usb_interface *intf;
  1458. unsigned int no_dj_interfaces = 0;
  1459. bool has_hidpp = false;
  1460. unsigned long flags;
  1461. int retval;
  1462. /*
  1463. * Call to usbhid to fetch the HID descriptors of the current
  1464. * interface subsequently call to the hid/hid-core to parse the
  1465. * fetched descriptors.
  1466. */
  1467. retval = hid_parse(hdev);
  1468. if (retval) {
  1469. hid_err(hdev, "%s: parse failed\n", __func__);
  1470. return retval;
  1471. }
  1472. /*
  1473. * Some KVMs add an extra interface for e.g. mouse emulation. If we
  1474. * treat these as logitech-dj interfaces then this causes input events
  1475. * reported through this extra interface to not be reported correctly.
  1476. * To avoid this, we treat these as generic-hid devices.
  1477. */
  1478. switch (id->driver_data) {
  1479. case recvr_type_dj: no_dj_interfaces = 3; break;
  1480. case recvr_type_hidpp: no_dj_interfaces = 2; break;
  1481. case recvr_type_gaming_hidpp: no_dj_interfaces = 3; break;
  1482. case recvr_type_mouse_only: no_dj_interfaces = 2; break;
  1483. case recvr_type_27mhz: no_dj_interfaces = 2; break;
  1484. case recvr_type_bluetooth: no_dj_interfaces = 2; break;
  1485. }
  1486. if (hid_is_usb(hdev)) {
  1487. intf = to_usb_interface(hdev->dev.parent);
  1488. if (intf && intf->altsetting->desc.bInterfaceNumber >=
  1489. no_dj_interfaces) {
  1490. hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
  1491. return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  1492. }
  1493. }
  1494. rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
  1495. /* no input reports, bail out */
  1496. if (list_empty(&rep_enum->report_list))
  1497. return -ENODEV;
  1498. /*
  1499. * Check for the HID++ application.
  1500. * Note: we should theoretically check for HID++ and DJ
  1501. * collections, but this will do.
  1502. */
  1503. list_for_each_entry(rep, &rep_enum->report_list, list) {
  1504. if (rep->application == 0xff000001)
  1505. has_hidpp = true;
  1506. }
  1507. /*
  1508. * Ignore interfaces without DJ/HID++ collection, they will not carry
  1509. * any data, dont create any hid_device for them.
  1510. */
  1511. if (!has_hidpp && id->driver_data == recvr_type_dj)
  1512. return -ENODEV;
  1513. /* get the current application attached to the node */
  1514. rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
  1515. djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
  1516. rep->application, has_hidpp);
  1517. if (!djrcv_dev) {
  1518. hid_err(hdev, "%s: dj_get_receiver_dev failed\n", __func__);
  1519. return -ENOMEM;
  1520. }
  1521. if (!rep_enum->numbered)
  1522. djrcv_dev->unnumbered_application = rep->application;
  1523. /* Starts the usb device and connects to upper interfaces hiddev and
  1524. * hidraw */
  1525. retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
  1526. if (retval) {
  1527. hid_err(hdev, "%s: hid_hw_start returned error\n", __func__);
  1528. goto hid_hw_start_fail;
  1529. }
  1530. if (has_hidpp) {
  1531. retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
  1532. if (retval < 0) {
  1533. hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
  1534. __func__, retval);
  1535. goto switch_to_dj_mode_fail;
  1536. }
  1537. }
  1538. /* This is enabling the polling urb on the IN endpoint */
  1539. retval = hid_hw_open(hdev);
  1540. if (retval < 0) {
  1541. hid_err(hdev, "%s: hid_hw_open returned error:%d\n",
  1542. __func__, retval);
  1543. goto llopen_failed;
  1544. }
  1545. /* Allow incoming packets to arrive: */
  1546. hid_device_io_start(hdev);
  1547. if (has_hidpp) {
  1548. spin_lock_irqsave(&djrcv_dev->lock, flags);
  1549. djrcv_dev->ready = true;
  1550. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  1551. retval = logi_dj_recv_query_paired_devices(djrcv_dev);
  1552. if (retval < 0) {
  1553. hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
  1554. __func__, retval);
  1555. /*
  1556. * This can happen with a KVM, let the probe succeed,
  1557. * logi_dj_recv_queue_unknown_work will retry later.
  1558. */
  1559. }
  1560. }
  1561. return 0;
  1562. llopen_failed:
  1563. switch_to_dj_mode_fail:
  1564. hid_hw_stop(hdev);
  1565. hid_hw_start_fail:
  1566. dj_put_receiver_dev(hdev);
  1567. return retval;
  1568. }
  1569. #ifdef CONFIG_PM
  1570. static int logi_dj_reset_resume(struct hid_device *hdev)
  1571. {
  1572. int retval;
  1573. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  1574. if (!djrcv_dev || djrcv_dev->hidpp != hdev)
  1575. return 0;
  1576. retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
  1577. if (retval < 0) {
  1578. hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
  1579. __func__, retval);
  1580. }
  1581. return 0;
  1582. }
  1583. #endif
  1584. static void logi_dj_remove(struct hid_device *hdev)
  1585. {
  1586. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  1587. struct dj_device *dj_dev;
  1588. unsigned long flags;
  1589. int i;
  1590. dbg_hid("%s\n", __func__);
  1591. if (!djrcv_dev)
  1592. return hid_hw_stop(hdev);
  1593. /*
  1594. * This ensures that if the work gets requeued from another
  1595. * interface of the same receiver it will be a no-op.
  1596. */
  1597. spin_lock_irqsave(&djrcv_dev->lock, flags);
  1598. djrcv_dev->ready = false;
  1599. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  1600. cancel_work_sync(&djrcv_dev->work);
  1601. hid_hw_close(hdev);
  1602. hid_hw_stop(hdev);
  1603. /*
  1604. * For proper operation we need access to all interfaces, so we destroy
  1605. * the paired devices when we're unbound from any interface.
  1606. *
  1607. * Note we may still be bound to other interfaces, sharing the same
  1608. * djrcv_dev, so we need locking here.
  1609. */
  1610. for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
  1611. spin_lock_irqsave(&djrcv_dev->lock, flags);
  1612. dj_dev = djrcv_dev->paired_dj_devices[i];
  1613. djrcv_dev->paired_dj_devices[i] = NULL;
  1614. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  1615. if (dj_dev != NULL) {
  1616. hid_destroy_device(dj_dev->hdev);
  1617. kfree(dj_dev);
  1618. }
  1619. }
  1620. dj_put_receiver_dev(hdev);
  1621. }
  1622. static const struct hid_device_id logi_dj_receivers[] = {
  1623. {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1624. USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
  1625. .driver_data = recvr_type_dj},
  1626. {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1627. USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
  1628. .driver_data = recvr_type_dj},
  1629. { /* Logitech Nano mouse only receiver */
  1630. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1631. USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
  1632. .driver_data = recvr_type_mouse_only},
  1633. { /* Logitech Nano (non DJ) receiver */
  1634. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1635. USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
  1636. .driver_data = recvr_type_hidpp},
  1637. { /* Logitech G700(s) receiver (0xc531) */
  1638. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1639. 0xc531),
  1640. .driver_data = recvr_type_gaming_hidpp},
  1641. { /* Logitech G602 receiver (0xc537) */
  1642. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1643. 0xc537),
  1644. .driver_data = recvr_type_gaming_hidpp},
  1645. { /* Logitech lightspeed receiver (0xc539) */
  1646. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1647. USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_LIGHTSPEED_1),
  1648. .driver_data = recvr_type_gaming_hidpp},
  1649. { /* Logitech lightspeed receiver (0xc53f) */
  1650. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1651. USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_LIGHTSPEED_1_1),
  1652. .driver_data = recvr_type_gaming_hidpp},
  1653. { /* Logitech 27 MHz HID++ 1.0 receiver (0xc513) */
  1654. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER),
  1655. .driver_data = recvr_type_27mhz},
  1656. { /* Logitech powerplay receiver (0xc53a) */
  1657. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1658. USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_POWERPLAY),
  1659. .driver_data = recvr_type_gaming_hidpp},
  1660. { /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
  1661. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1662. USB_DEVICE_ID_S510_RECEIVER_2),
  1663. .driver_data = recvr_type_27mhz},
  1664. { /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
  1665. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1666. USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
  1667. .driver_data = recvr_type_27mhz},
  1668. { /* Logitech MX5000 HID++ / bluetooth receiver keyboard intf. */
  1669. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1670. 0xc70e),
  1671. .driver_data = recvr_type_bluetooth},
  1672. { /* Logitech MX5000 HID++ / bluetooth receiver mouse intf. */
  1673. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1674. 0xc70a),
  1675. .driver_data = recvr_type_bluetooth},
  1676. { /* Logitech MX5500 HID++ / bluetooth receiver keyboard intf. */
  1677. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1678. 0xc71b),
  1679. .driver_data = recvr_type_bluetooth},
  1680. { /* Logitech MX5500 HID++ / bluetooth receiver mouse intf. */
  1681. HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  1682. 0xc71c),
  1683. .driver_data = recvr_type_bluetooth},
  1684. {}
  1685. };
  1686. MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
  1687. static struct hid_driver logi_djreceiver_driver = {
  1688. .name = "logitech-djreceiver",
  1689. .id_table = logi_dj_receivers,
  1690. .probe = logi_dj_probe,
  1691. .remove = logi_dj_remove,
  1692. .raw_event = logi_dj_raw_event,
  1693. #ifdef CONFIG_PM
  1694. .reset_resume = logi_dj_reset_resume,
  1695. #endif
  1696. };
  1697. module_hid_driver(logi_djreceiver_driver);
  1698. MODULE_LICENSE("GPL");
  1699. MODULE_AUTHOR("Logitech");
  1700. MODULE_AUTHOR("Nestor Lopez Casado");
  1701. MODULE_AUTHOR("nlopezcasad@logitech.com");