hid-sony.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. /*
  2. * HID driver for Sony / PS2 / PS3 / PS4 BD devices.
  3. *
  4. * Copyright (c) 1999 Andreas Gal
  5. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  6. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  7. * Copyright (c) 2008 Jiri Slaby
  8. * Copyright (c) 2012 David Dillow <dave@thedillows.org>
  9. * Copyright (c) 2006-2013 Jiri Kosina
  10. * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
  11. * Copyright (c) 2014 Frank Praznik <frank.praznik@gmail.com>
  12. */
  13. /*
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. */
  19. /*
  20. * NOTE: in order for the Sony PS3 BD Remote Control to be found by
  21. * a Bluetooth host, the key combination Start+Enter has to be kept pressed
  22. * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
  23. *
  24. * There will be no PIN request from the device.
  25. */
  26. #include <linux/device.h>
  27. #include <linux/hid.h>
  28. #include <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/leds.h>
  31. #include <linux/power_supply.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/list.h>
  34. #include <linux/idr.h>
  35. #include <linux/input/mt.h>
  36. #include "hid-ids.h"
  37. #define VAIO_RDESC_CONSTANT BIT(0)
  38. #define SIXAXIS_CONTROLLER_USB BIT(1)
  39. #define SIXAXIS_CONTROLLER_BT BIT(2)
  40. #define BUZZ_CONTROLLER BIT(3)
  41. #define PS3REMOTE BIT(4)
  42. #define DUALSHOCK4_CONTROLLER_USB BIT(5)
  43. #define DUALSHOCK4_CONTROLLER_BT BIT(6)
  44. #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
  45. #define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
  46. DUALSHOCK4_CONTROLLER_BT)
  47. #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
  48. DUALSHOCK4_CONTROLLER)
  49. #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER)
  50. #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER)
  51. #define MAX_LEDS 4
  52. static __u8 sixaxis_rdesc[] = {
  53. 0x05, 0x01, /* Usage Page (Desktop), */
  54. 0x09, 0x04, /* Usage (Joystik), */
  55. 0xA1, 0x01, /* Collection (Application), */
  56. 0xA1, 0x02, /* Collection (Logical), */
  57. 0x85, 0x01, /* Report ID (1), */
  58. 0x75, 0x08, /* Report Size (8), */
  59. 0x95, 0x01, /* Report Count (1), */
  60. 0x15, 0x00, /* Logical Minimum (0), */
  61. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  62. 0x81, 0x03, /* Input (Constant, Variable), */
  63. 0x75, 0x01, /* Report Size (1), */
  64. 0x95, 0x13, /* Report Count (19), */
  65. 0x15, 0x00, /* Logical Minimum (0), */
  66. 0x25, 0x01, /* Logical Maximum (1), */
  67. 0x35, 0x00, /* Physical Minimum (0), */
  68. 0x45, 0x01, /* Physical Maximum (1), */
  69. 0x05, 0x09, /* Usage Page (Button), */
  70. 0x19, 0x01, /* Usage Minimum (01h), */
  71. 0x29, 0x13, /* Usage Maximum (13h), */
  72. 0x81, 0x02, /* Input (Variable), */
  73. 0x75, 0x01, /* Report Size (1), */
  74. 0x95, 0x0D, /* Report Count (13), */
  75. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  76. 0x81, 0x03, /* Input (Constant, Variable), */
  77. 0x15, 0x00, /* Logical Minimum (0), */
  78. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  79. 0x05, 0x01, /* Usage Page (Desktop), */
  80. 0x09, 0x01, /* Usage (Pointer), */
  81. 0xA1, 0x00, /* Collection (Physical), */
  82. 0x75, 0x08, /* Report Size (8), */
  83. 0x95, 0x04, /* Report Count (4), */
  84. 0x35, 0x00, /* Physical Minimum (0), */
  85. 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
  86. 0x09, 0x30, /* Usage (X), */
  87. 0x09, 0x31, /* Usage (Y), */
  88. 0x09, 0x32, /* Usage (Z), */
  89. 0x09, 0x35, /* Usage (Rz), */
  90. 0x81, 0x02, /* Input (Variable), */
  91. 0xC0, /* End Collection, */
  92. 0x05, 0x01, /* Usage Page (Desktop), */
  93. 0x95, 0x13, /* Report Count (19), */
  94. 0x09, 0x01, /* Usage (Pointer), */
  95. 0x81, 0x02, /* Input (Variable), */
  96. 0x95, 0x0C, /* Report Count (12), */
  97. 0x81, 0x01, /* Input (Constant), */
  98. 0x75, 0x10, /* Report Size (16), */
  99. 0x95, 0x04, /* Report Count (4), */
  100. 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
  101. 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
  102. 0x09, 0x01, /* Usage (Pointer), */
  103. 0x81, 0x02, /* Input (Variable), */
  104. 0xC0, /* End Collection, */
  105. 0xA1, 0x02, /* Collection (Logical), */
  106. 0x85, 0x02, /* Report ID (2), */
  107. 0x75, 0x08, /* Report Size (8), */
  108. 0x95, 0x30, /* Report Count (48), */
  109. 0x09, 0x01, /* Usage (Pointer), */
  110. 0xB1, 0x02, /* Feature (Variable), */
  111. 0xC0, /* End Collection, */
  112. 0xA1, 0x02, /* Collection (Logical), */
  113. 0x85, 0xEE, /* Report ID (238), */
  114. 0x75, 0x08, /* Report Size (8), */
  115. 0x95, 0x30, /* Report Count (48), */
  116. 0x09, 0x01, /* Usage (Pointer), */
  117. 0xB1, 0x02, /* Feature (Variable), */
  118. 0xC0, /* End Collection, */
  119. 0xA1, 0x02, /* Collection (Logical), */
  120. 0x85, 0xEF, /* Report ID (239), */
  121. 0x75, 0x08, /* Report Size (8), */
  122. 0x95, 0x30, /* Report Count (48), */
  123. 0x09, 0x01, /* Usage (Pointer), */
  124. 0xB1, 0x02, /* Feature (Variable), */
  125. 0xC0, /* End Collection, */
  126. 0xC0 /* End Collection */
  127. };
  128. /*
  129. * The default descriptor doesn't provide mapping for the accelerometers
  130. * or orientation sensors. This fixed descriptor maps the accelerometers
  131. * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
  132. * to usage values 0x43, 0x44 and 0x45.
  133. */
  134. static u8 dualshock4_usb_rdesc[] = {
  135. 0x05, 0x01, /* Usage Page (Desktop), */
  136. 0x09, 0x05, /* Usage (Gamepad), */
  137. 0xA1, 0x01, /* Collection (Application), */
  138. 0x85, 0x01, /* Report ID (1), */
  139. 0x09, 0x30, /* Usage (X), */
  140. 0x09, 0x31, /* Usage (Y), */
  141. 0x09, 0x32, /* Usage (Z), */
  142. 0x09, 0x35, /* Usage (Rz), */
  143. 0x15, 0x00, /* Logical Minimum (0), */
  144. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  145. 0x75, 0x08, /* Report Size (8), */
  146. 0x95, 0x04, /* Report Count (4), */
  147. 0x81, 0x02, /* Input (Variable), */
  148. 0x09, 0x39, /* Usage (Hat Switch), */
  149. 0x15, 0x00, /* Logical Minimum (0), */
  150. 0x25, 0x07, /* Logical Maximum (7), */
  151. 0x35, 0x00, /* Physical Minimum (0), */
  152. 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
  153. 0x65, 0x14, /* Unit (Degrees), */
  154. 0x75, 0x04, /* Report Size (4), */
  155. 0x95, 0x01, /* Report Count (1), */
  156. 0x81, 0x42, /* Input (Variable, Null State), */
  157. 0x65, 0x00, /* Unit, */
  158. 0x05, 0x09, /* Usage Page (Button), */
  159. 0x19, 0x01, /* Usage Minimum (01h), */
  160. 0x29, 0x0E, /* Usage Maximum (0Eh), */
  161. 0x15, 0x00, /* Logical Minimum (0), */
  162. 0x25, 0x01, /* Logical Maximum (1), */
  163. 0x75, 0x01, /* Report Size (1), */
  164. 0x95, 0x0E, /* Report Count (14), */
  165. 0x81, 0x02, /* Input (Variable), */
  166. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  167. 0x09, 0x20, /* Usage (20h), */
  168. 0x75, 0x06, /* Report Size (6), */
  169. 0x95, 0x01, /* Report Count (1), */
  170. 0x15, 0x00, /* Logical Minimum (0), */
  171. 0x25, 0x3F, /* Logical Maximum (63), */
  172. 0x81, 0x02, /* Input (Variable), */
  173. 0x05, 0x01, /* Usage Page (Desktop), */
  174. 0x09, 0x33, /* Usage (Rx), */
  175. 0x09, 0x34, /* Usage (Ry), */
  176. 0x15, 0x00, /* Logical Minimum (0), */
  177. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  178. 0x75, 0x08, /* Report Size (8), */
  179. 0x95, 0x02, /* Report Count (2), */
  180. 0x81, 0x02, /* Input (Variable), */
  181. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  182. 0x09, 0x21, /* Usage (21h), */
  183. 0x95, 0x03, /* Report Count (3), */
  184. 0x81, 0x02, /* Input (Variable), */
  185. 0x05, 0x01, /* Usage Page (Desktop), */
  186. 0x19, 0x40, /* Usage Minimum (40h), */
  187. 0x29, 0x42, /* Usage Maximum (42h), */
  188. 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
  189. 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
  190. 0x75, 0x10, /* Report Size (16), */
  191. 0x95, 0x03, /* Report Count (3), */
  192. 0x81, 0x02, /* Input (Variable), */
  193. 0x19, 0x43, /* Usage Minimum (43h), */
  194. 0x29, 0x45, /* Usage Maximum (45h), */
  195. 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
  196. 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
  197. 0x95, 0x03, /* Report Count (3), */
  198. 0x81, 0x02, /* Input (Variable), */
  199. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  200. 0x09, 0x21, /* Usage (21h), */
  201. 0x15, 0x00, /* Logical Minimum (0), */
  202. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  203. 0x75, 0x08, /* Report Size (8), */
  204. 0x95, 0x27, /* Report Count (39), */
  205. 0x81, 0x02, /* Input (Variable), */
  206. 0x85, 0x05, /* Report ID (5), */
  207. 0x09, 0x22, /* Usage (22h), */
  208. 0x95, 0x1F, /* Report Count (31), */
  209. 0x91, 0x02, /* Output (Variable), */
  210. 0x85, 0x04, /* Report ID (4), */
  211. 0x09, 0x23, /* Usage (23h), */
  212. 0x95, 0x24, /* Report Count (36), */
  213. 0xB1, 0x02, /* Feature (Variable), */
  214. 0x85, 0x02, /* Report ID (2), */
  215. 0x09, 0x24, /* Usage (24h), */
  216. 0x95, 0x24, /* Report Count (36), */
  217. 0xB1, 0x02, /* Feature (Variable), */
  218. 0x85, 0x08, /* Report ID (8), */
  219. 0x09, 0x25, /* Usage (25h), */
  220. 0x95, 0x03, /* Report Count (3), */
  221. 0xB1, 0x02, /* Feature (Variable), */
  222. 0x85, 0x10, /* Report ID (16), */
  223. 0x09, 0x26, /* Usage (26h), */
  224. 0x95, 0x04, /* Report Count (4), */
  225. 0xB1, 0x02, /* Feature (Variable), */
  226. 0x85, 0x11, /* Report ID (17), */
  227. 0x09, 0x27, /* Usage (27h), */
  228. 0x95, 0x02, /* Report Count (2), */
  229. 0xB1, 0x02, /* Feature (Variable), */
  230. 0x85, 0x12, /* Report ID (18), */
  231. 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
  232. 0x09, 0x21, /* Usage (21h), */
  233. 0x95, 0x0F, /* Report Count (15), */
  234. 0xB1, 0x02, /* Feature (Variable), */
  235. 0x85, 0x13, /* Report ID (19), */
  236. 0x09, 0x22, /* Usage (22h), */
  237. 0x95, 0x16, /* Report Count (22), */
  238. 0xB1, 0x02, /* Feature (Variable), */
  239. 0x85, 0x14, /* Report ID (20), */
  240. 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
  241. 0x09, 0x20, /* Usage (20h), */
  242. 0x95, 0x10, /* Report Count (16), */
  243. 0xB1, 0x02, /* Feature (Variable), */
  244. 0x85, 0x15, /* Report ID (21), */
  245. 0x09, 0x21, /* Usage (21h), */
  246. 0x95, 0x2C, /* Report Count (44), */
  247. 0xB1, 0x02, /* Feature (Variable), */
  248. 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
  249. 0x85, 0x80, /* Report ID (128), */
  250. 0x09, 0x20, /* Usage (20h), */
  251. 0x95, 0x06, /* Report Count (6), */
  252. 0xB1, 0x02, /* Feature (Variable), */
  253. 0x85, 0x81, /* Report ID (129), */
  254. 0x09, 0x21, /* Usage (21h), */
  255. 0x95, 0x06, /* Report Count (6), */
  256. 0xB1, 0x02, /* Feature (Variable), */
  257. 0x85, 0x82, /* Report ID (130), */
  258. 0x09, 0x22, /* Usage (22h), */
  259. 0x95, 0x05, /* Report Count (5), */
  260. 0xB1, 0x02, /* Feature (Variable), */
  261. 0x85, 0x83, /* Report ID (131), */
  262. 0x09, 0x23, /* Usage (23h), */
  263. 0x95, 0x01, /* Report Count (1), */
  264. 0xB1, 0x02, /* Feature (Variable), */
  265. 0x85, 0x84, /* Report ID (132), */
  266. 0x09, 0x24, /* Usage (24h), */
  267. 0x95, 0x04, /* Report Count (4), */
  268. 0xB1, 0x02, /* Feature (Variable), */
  269. 0x85, 0x85, /* Report ID (133), */
  270. 0x09, 0x25, /* Usage (25h), */
  271. 0x95, 0x06, /* Report Count (6), */
  272. 0xB1, 0x02, /* Feature (Variable), */
  273. 0x85, 0x86, /* Report ID (134), */
  274. 0x09, 0x26, /* Usage (26h), */
  275. 0x95, 0x06, /* Report Count (6), */
  276. 0xB1, 0x02, /* Feature (Variable), */
  277. 0x85, 0x87, /* Report ID (135), */
  278. 0x09, 0x27, /* Usage (27h), */
  279. 0x95, 0x23, /* Report Count (35), */
  280. 0xB1, 0x02, /* Feature (Variable), */
  281. 0x85, 0x88, /* Report ID (136), */
  282. 0x09, 0x28, /* Usage (28h), */
  283. 0x95, 0x22, /* Report Count (34), */
  284. 0xB1, 0x02, /* Feature (Variable), */
  285. 0x85, 0x89, /* Report ID (137), */
  286. 0x09, 0x29, /* Usage (29h), */
  287. 0x95, 0x02, /* Report Count (2), */
  288. 0xB1, 0x02, /* Feature (Variable), */
  289. 0x85, 0x90, /* Report ID (144), */
  290. 0x09, 0x30, /* Usage (30h), */
  291. 0x95, 0x05, /* Report Count (5), */
  292. 0xB1, 0x02, /* Feature (Variable), */
  293. 0x85, 0x91, /* Report ID (145), */
  294. 0x09, 0x31, /* Usage (31h), */
  295. 0x95, 0x03, /* Report Count (3), */
  296. 0xB1, 0x02, /* Feature (Variable), */
  297. 0x85, 0x92, /* Report ID (146), */
  298. 0x09, 0x32, /* Usage (32h), */
  299. 0x95, 0x03, /* Report Count (3), */
  300. 0xB1, 0x02, /* Feature (Variable), */
  301. 0x85, 0x93, /* Report ID (147), */
  302. 0x09, 0x33, /* Usage (33h), */
  303. 0x95, 0x0C, /* Report Count (12), */
  304. 0xB1, 0x02, /* Feature (Variable), */
  305. 0x85, 0xA0, /* Report ID (160), */
  306. 0x09, 0x40, /* Usage (40h), */
  307. 0x95, 0x06, /* Report Count (6), */
  308. 0xB1, 0x02, /* Feature (Variable), */
  309. 0x85, 0xA1, /* Report ID (161), */
  310. 0x09, 0x41, /* Usage (41h), */
  311. 0x95, 0x01, /* Report Count (1), */
  312. 0xB1, 0x02, /* Feature (Variable), */
  313. 0x85, 0xA2, /* Report ID (162), */
  314. 0x09, 0x42, /* Usage (42h), */
  315. 0x95, 0x01, /* Report Count (1), */
  316. 0xB1, 0x02, /* Feature (Variable), */
  317. 0x85, 0xA3, /* Report ID (163), */
  318. 0x09, 0x43, /* Usage (43h), */
  319. 0x95, 0x30, /* Report Count (48), */
  320. 0xB1, 0x02, /* Feature (Variable), */
  321. 0x85, 0xA4, /* Report ID (164), */
  322. 0x09, 0x44, /* Usage (44h), */
  323. 0x95, 0x0D, /* Report Count (13), */
  324. 0xB1, 0x02, /* Feature (Variable), */
  325. 0x85, 0xA5, /* Report ID (165), */
  326. 0x09, 0x45, /* Usage (45h), */
  327. 0x95, 0x15, /* Report Count (21), */
  328. 0xB1, 0x02, /* Feature (Variable), */
  329. 0x85, 0xA6, /* Report ID (166), */
  330. 0x09, 0x46, /* Usage (46h), */
  331. 0x95, 0x15, /* Report Count (21), */
  332. 0xB1, 0x02, /* Feature (Variable), */
  333. 0x85, 0xF0, /* Report ID (240), */
  334. 0x09, 0x47, /* Usage (47h), */
  335. 0x95, 0x3F, /* Report Count (63), */
  336. 0xB1, 0x02, /* Feature (Variable), */
  337. 0x85, 0xF1, /* Report ID (241), */
  338. 0x09, 0x48, /* Usage (48h), */
  339. 0x95, 0x3F, /* Report Count (63), */
  340. 0xB1, 0x02, /* Feature (Variable), */
  341. 0x85, 0xF2, /* Report ID (242), */
  342. 0x09, 0x49, /* Usage (49h), */
  343. 0x95, 0x0F, /* Report Count (15), */
  344. 0xB1, 0x02, /* Feature (Variable), */
  345. 0x85, 0xA7, /* Report ID (167), */
  346. 0x09, 0x4A, /* Usage (4Ah), */
  347. 0x95, 0x01, /* Report Count (1), */
  348. 0xB1, 0x02, /* Feature (Variable), */
  349. 0x85, 0xA8, /* Report ID (168), */
  350. 0x09, 0x4B, /* Usage (4Bh), */
  351. 0x95, 0x01, /* Report Count (1), */
  352. 0xB1, 0x02, /* Feature (Variable), */
  353. 0x85, 0xA9, /* Report ID (169), */
  354. 0x09, 0x4C, /* Usage (4Ch), */
  355. 0x95, 0x08, /* Report Count (8), */
  356. 0xB1, 0x02, /* Feature (Variable), */
  357. 0x85, 0xAA, /* Report ID (170), */
  358. 0x09, 0x4E, /* Usage (4Eh), */
  359. 0x95, 0x01, /* Report Count (1), */
  360. 0xB1, 0x02, /* Feature (Variable), */
  361. 0x85, 0xAB, /* Report ID (171), */
  362. 0x09, 0x4F, /* Usage (4Fh), */
  363. 0x95, 0x39, /* Report Count (57), */
  364. 0xB1, 0x02, /* Feature (Variable), */
  365. 0x85, 0xAC, /* Report ID (172), */
  366. 0x09, 0x50, /* Usage (50h), */
  367. 0x95, 0x39, /* Report Count (57), */
  368. 0xB1, 0x02, /* Feature (Variable), */
  369. 0x85, 0xAD, /* Report ID (173), */
  370. 0x09, 0x51, /* Usage (51h), */
  371. 0x95, 0x0B, /* Report Count (11), */
  372. 0xB1, 0x02, /* Feature (Variable), */
  373. 0x85, 0xAE, /* Report ID (174), */
  374. 0x09, 0x52, /* Usage (52h), */
  375. 0x95, 0x01, /* Report Count (1), */
  376. 0xB1, 0x02, /* Feature (Variable), */
  377. 0x85, 0xAF, /* Report ID (175), */
  378. 0x09, 0x53, /* Usage (53h), */
  379. 0x95, 0x02, /* Report Count (2), */
  380. 0xB1, 0x02, /* Feature (Variable), */
  381. 0x85, 0xB0, /* Report ID (176), */
  382. 0x09, 0x54, /* Usage (54h), */
  383. 0x95, 0x3F, /* Report Count (63), */
  384. 0xB1, 0x02, /* Feature (Variable), */
  385. 0xC0 /* End Collection */
  386. };
  387. /*
  388. * The default behavior of the Dualshock 4 is to send reports using report
  389. * type 1 when running over Bluetooth. However, when feature report 2 is
  390. * requested during the controller initialization it starts sending input
  391. * reports in report 17. Since report 17 is undefined in the default HID
  392. * descriptor the button and axis definitions must be moved to report 17 or
  393. * the HID layer won't process the received input.
  394. */
  395. static u8 dualshock4_bt_rdesc[] = {
  396. 0x05, 0x01, /* Usage Page (Desktop), */
  397. 0x09, 0x05, /* Usage (Gamepad), */
  398. 0xA1, 0x01, /* Collection (Application), */
  399. 0x85, 0x01, /* Report ID (1), */
  400. 0x75, 0x08, /* Report Size (8), */
  401. 0x95, 0x0A, /* Report Count (9), */
  402. 0x81, 0x02, /* Input (Variable), */
  403. 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
  404. 0x85, 0x02, /* Report ID (2), */
  405. 0x09, 0x24, /* Usage (24h), */
  406. 0x95, 0x24, /* Report Count (36), */
  407. 0xB1, 0x02, /* Feature (Variable), */
  408. 0x85, 0xA3, /* Report ID (163), */
  409. 0x09, 0x25, /* Usage (25h), */
  410. 0x95, 0x30, /* Report Count (48), */
  411. 0xB1, 0x02, /* Feature (Variable), */
  412. 0x85, 0x05, /* Report ID (5), */
  413. 0x09, 0x26, /* Usage (26h), */
  414. 0x95, 0x28, /* Report Count (40), */
  415. 0xB1, 0x02, /* Feature (Variable), */
  416. 0x85, 0x06, /* Report ID (6), */
  417. 0x09, 0x27, /* Usage (27h), */
  418. 0x95, 0x34, /* Report Count (52), */
  419. 0xB1, 0x02, /* Feature (Variable), */
  420. 0x85, 0x07, /* Report ID (7), */
  421. 0x09, 0x28, /* Usage (28h), */
  422. 0x95, 0x30, /* Report Count (48), */
  423. 0xB1, 0x02, /* Feature (Variable), */
  424. 0x85, 0x08, /* Report ID (8), */
  425. 0x09, 0x29, /* Usage (29h), */
  426. 0x95, 0x2F, /* Report Count (47), */
  427. 0xB1, 0x02, /* Feature (Variable), */
  428. 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
  429. 0x85, 0x03, /* Report ID (3), */
  430. 0x09, 0x21, /* Usage (21h), */
  431. 0x95, 0x26, /* Report Count (38), */
  432. 0xB1, 0x02, /* Feature (Variable), */
  433. 0x85, 0x04, /* Report ID (4), */
  434. 0x09, 0x22, /* Usage (22h), */
  435. 0x95, 0x2E, /* Report Count (46), */
  436. 0xB1, 0x02, /* Feature (Variable), */
  437. 0x85, 0xF0, /* Report ID (240), */
  438. 0x09, 0x47, /* Usage (47h), */
  439. 0x95, 0x3F, /* Report Count (63), */
  440. 0xB1, 0x02, /* Feature (Variable), */
  441. 0x85, 0xF1, /* Report ID (241), */
  442. 0x09, 0x48, /* Usage (48h), */
  443. 0x95, 0x3F, /* Report Count (63), */
  444. 0xB1, 0x02, /* Feature (Variable), */
  445. 0x85, 0xF2, /* Report ID (242), */
  446. 0x09, 0x49, /* Usage (49h), */
  447. 0x95, 0x0F, /* Report Count (15), */
  448. 0xB1, 0x02, /* Feature (Variable), */
  449. 0x85, 0x11, /* Report ID (17), */
  450. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  451. 0x09, 0x20, /* Usage (20h), */
  452. 0x95, 0x02, /* Report Count (2), */
  453. 0x81, 0x02, /* Input (Variable), */
  454. 0x05, 0x01, /* Usage Page (Desktop), */
  455. 0x09, 0x30, /* Usage (X), */
  456. 0x09, 0x31, /* Usage (Y), */
  457. 0x09, 0x32, /* Usage (Z), */
  458. 0x09, 0x35, /* Usage (Rz), */
  459. 0x15, 0x00, /* Logical Minimum (0), */
  460. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  461. 0x75, 0x08, /* Report Size (8), */
  462. 0x95, 0x04, /* Report Count (4), */
  463. 0x81, 0x02, /* Input (Variable), */
  464. 0x09, 0x39, /* Usage (Hat Switch), */
  465. 0x15, 0x00, /* Logical Minimum (0), */
  466. 0x25, 0x07, /* Logical Maximum (7), */
  467. 0x75, 0x04, /* Report Size (4), */
  468. 0x95, 0x01, /* Report Count (1), */
  469. 0x81, 0x42, /* Input (Variable, Null State), */
  470. 0x05, 0x09, /* Usage Page (Button), */
  471. 0x19, 0x01, /* Usage Minimum (01h), */
  472. 0x29, 0x0E, /* Usage Maximum (0Eh), */
  473. 0x15, 0x00, /* Logical Minimum (0), */
  474. 0x25, 0x01, /* Logical Maximum (1), */
  475. 0x75, 0x01, /* Report Size (1), */
  476. 0x95, 0x0E, /* Report Count (14), */
  477. 0x81, 0x02, /* Input (Variable), */
  478. 0x75, 0x06, /* Report Size (6), */
  479. 0x95, 0x01, /* Report Count (1), */
  480. 0x81, 0x01, /* Input (Constant), */
  481. 0x05, 0x01, /* Usage Page (Desktop), */
  482. 0x09, 0x33, /* Usage (Rx), */
  483. 0x09, 0x34, /* Usage (Ry), */
  484. 0x15, 0x00, /* Logical Minimum (0), */
  485. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  486. 0x75, 0x08, /* Report Size (8), */
  487. 0x95, 0x02, /* Report Count (2), */
  488. 0x81, 0x02, /* Input (Variable), */
  489. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  490. 0x09, 0x20, /* Usage (20h), */
  491. 0x95, 0x03, /* Report Count (3), */
  492. 0x81, 0x02, /* Input (Variable), */
  493. 0x05, 0x01, /* Usage Page (Desktop), */
  494. 0x19, 0x40, /* Usage Minimum (40h), */
  495. 0x29, 0x42, /* Usage Maximum (42h), */
  496. 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
  497. 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
  498. 0x75, 0x10, /* Report Size (16), */
  499. 0x95, 0x03, /* Report Count (3), */
  500. 0x81, 0x02, /* Input (Variable), */
  501. 0x19, 0x43, /* Usage Minimum (43h), */
  502. 0x29, 0x45, /* Usage Maximum (45h), */
  503. 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
  504. 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
  505. 0x95, 0x03, /* Report Count (3), */
  506. 0x81, 0x02, /* Input (Variable), */
  507. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  508. 0x09, 0x20, /* Usage (20h), */
  509. 0x15, 0x00, /* Logical Minimum (0), */
  510. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  511. 0x75, 0x08, /* Report Size (8), */
  512. 0x95, 0x31, /* Report Count (51), */
  513. 0x81, 0x02, /* Input (Variable), */
  514. 0x09, 0x21, /* Usage (21h), */
  515. 0x75, 0x08, /* Report Size (8), */
  516. 0x95, 0x4D, /* Report Count (77), */
  517. 0x91, 0x02, /* Output (Variable), */
  518. 0x85, 0x12, /* Report ID (18), */
  519. 0x09, 0x22, /* Usage (22h), */
  520. 0x95, 0x8D, /* Report Count (141), */
  521. 0x81, 0x02, /* Input (Variable), */
  522. 0x09, 0x23, /* Usage (23h), */
  523. 0x91, 0x02, /* Output (Variable), */
  524. 0x85, 0x13, /* Report ID (19), */
  525. 0x09, 0x24, /* Usage (24h), */
  526. 0x95, 0xCD, /* Report Count (205), */
  527. 0x81, 0x02, /* Input (Variable), */
  528. 0x09, 0x25, /* Usage (25h), */
  529. 0x91, 0x02, /* Output (Variable), */
  530. 0x85, 0x14, /* Report ID (20), */
  531. 0x09, 0x26, /* Usage (26h), */
  532. 0x96, 0x0D, 0x01, /* Report Count (269), */
  533. 0x81, 0x02, /* Input (Variable), */
  534. 0x09, 0x27, /* Usage (27h), */
  535. 0x91, 0x02, /* Output (Variable), */
  536. 0x85, 0x15, /* Report ID (21), */
  537. 0x09, 0x28, /* Usage (28h), */
  538. 0x96, 0x4D, 0x01, /* Report Count (333), */
  539. 0x81, 0x02, /* Input (Variable), */
  540. 0x09, 0x29, /* Usage (29h), */
  541. 0x91, 0x02, /* Output (Variable), */
  542. 0x85, 0x16, /* Report ID (22), */
  543. 0x09, 0x2A, /* Usage (2Ah), */
  544. 0x96, 0x8D, 0x01, /* Report Count (397), */
  545. 0x81, 0x02, /* Input (Variable), */
  546. 0x09, 0x2B, /* Usage (2Bh), */
  547. 0x91, 0x02, /* Output (Variable), */
  548. 0x85, 0x17, /* Report ID (23), */
  549. 0x09, 0x2C, /* Usage (2Ch), */
  550. 0x96, 0xCD, 0x01, /* Report Count (461), */
  551. 0x81, 0x02, /* Input (Variable), */
  552. 0x09, 0x2D, /* Usage (2Dh), */
  553. 0x91, 0x02, /* Output (Variable), */
  554. 0x85, 0x18, /* Report ID (24), */
  555. 0x09, 0x2E, /* Usage (2Eh), */
  556. 0x96, 0x0D, 0x02, /* Report Count (525), */
  557. 0x81, 0x02, /* Input (Variable), */
  558. 0x09, 0x2F, /* Usage (2Fh), */
  559. 0x91, 0x02, /* Output (Variable), */
  560. 0x85, 0x19, /* Report ID (25), */
  561. 0x09, 0x30, /* Usage (30h), */
  562. 0x96, 0x22, 0x02, /* Report Count (546), */
  563. 0x81, 0x02, /* Input (Variable), */
  564. 0x09, 0x31, /* Usage (31h), */
  565. 0x91, 0x02, /* Output (Variable), */
  566. 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
  567. 0x85, 0x82, /* Report ID (130), */
  568. 0x09, 0x22, /* Usage (22h), */
  569. 0x95, 0x3F, /* Report Count (63), */
  570. 0xB1, 0x02, /* Feature (Variable), */
  571. 0x85, 0x83, /* Report ID (131), */
  572. 0x09, 0x23, /* Usage (23h), */
  573. 0xB1, 0x02, /* Feature (Variable), */
  574. 0x85, 0x84, /* Report ID (132), */
  575. 0x09, 0x24, /* Usage (24h), */
  576. 0xB1, 0x02, /* Feature (Variable), */
  577. 0x85, 0x90, /* Report ID (144), */
  578. 0x09, 0x30, /* Usage (30h), */
  579. 0xB1, 0x02, /* Feature (Variable), */
  580. 0x85, 0x91, /* Report ID (145), */
  581. 0x09, 0x31, /* Usage (31h), */
  582. 0xB1, 0x02, /* Feature (Variable), */
  583. 0x85, 0x92, /* Report ID (146), */
  584. 0x09, 0x32, /* Usage (32h), */
  585. 0xB1, 0x02, /* Feature (Variable), */
  586. 0x85, 0x93, /* Report ID (147), */
  587. 0x09, 0x33, /* Usage (33h), */
  588. 0xB1, 0x02, /* Feature (Variable), */
  589. 0x85, 0xA0, /* Report ID (160), */
  590. 0x09, 0x40, /* Usage (40h), */
  591. 0xB1, 0x02, /* Feature (Variable), */
  592. 0x85, 0xA4, /* Report ID (164), */
  593. 0x09, 0x44, /* Usage (44h), */
  594. 0xB1, 0x02, /* Feature (Variable), */
  595. 0xC0 /* End Collection */
  596. };
  597. static __u8 ps3remote_rdesc[] = {
  598. 0x05, 0x01, /* GUsagePage Generic Desktop */
  599. 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
  600. 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
  601. /* Use collection 1 for joypad buttons */
  602. 0xA1, 0x02, /* MCollection Logical (interrelated data) */
  603. /* Ignore the 1st byte, maybe it is used for a controller
  604. * number but it's not needed for correct operation */
  605. 0x75, 0x08, /* GReportSize 0x08 [8] */
  606. 0x95, 0x01, /* GReportCount 0x01 [1] */
  607. 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
  608. /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
  609. * buttons multiple keypresses are allowed */
  610. 0x05, 0x09, /* GUsagePage Button */
  611. 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
  612. 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
  613. 0x14, /* GLogicalMinimum [0] */
  614. 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
  615. 0x75, 0x01, /* GReportSize 0x01 [1] */
  616. 0x95, 0x18, /* GReportCount 0x18 [24] */
  617. 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
  618. 0xC0, /* MEndCollection */
  619. /* Use collection 2 for remote control buttons */
  620. 0xA1, 0x02, /* MCollection Logical (interrelated data) */
  621. /* 5th byte is used for remote control buttons */
  622. 0x05, 0x09, /* GUsagePage Button */
  623. 0x18, /* LUsageMinimum [No button pressed] */
  624. 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
  625. 0x14, /* GLogicalMinimum [0] */
  626. 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
  627. 0x75, 0x08, /* GReportSize 0x08 [8] */
  628. 0x95, 0x01, /* GReportCount 0x01 [1] */
  629. 0x80, /* MInput */
  630. /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
  631. * 0xff and 11th is for press indication */
  632. 0x75, 0x08, /* GReportSize 0x08 [8] */
  633. 0x95, 0x06, /* GReportCount 0x06 [6] */
  634. 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
  635. /* 12th byte is for battery strength */
  636. 0x05, 0x06, /* GUsagePage Generic Device Controls */
  637. 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
  638. 0x14, /* GLogicalMinimum [0] */
  639. 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
  640. 0x75, 0x08, /* GReportSize 0x08 [8] */
  641. 0x95, 0x01, /* GReportCount 0x01 [1] */
  642. 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
  643. 0xC0, /* MEndCollection */
  644. 0xC0 /* MEndCollection [Game Pad] */
  645. };
  646. static const unsigned int ps3remote_keymap_joypad_buttons[] = {
  647. [0x01] = KEY_SELECT,
  648. [0x02] = BTN_THUMBL, /* L3 */
  649. [0x03] = BTN_THUMBR, /* R3 */
  650. [0x04] = BTN_START,
  651. [0x05] = KEY_UP,
  652. [0x06] = KEY_RIGHT,
  653. [0x07] = KEY_DOWN,
  654. [0x08] = KEY_LEFT,
  655. [0x09] = BTN_TL2, /* L2 */
  656. [0x0a] = BTN_TR2, /* R2 */
  657. [0x0b] = BTN_TL, /* L1 */
  658. [0x0c] = BTN_TR, /* R1 */
  659. [0x0d] = KEY_OPTION, /* options/triangle */
  660. [0x0e] = KEY_BACK, /* back/circle */
  661. [0x0f] = BTN_0, /* cross */
  662. [0x10] = KEY_SCREEN, /* view/square */
  663. [0x11] = KEY_HOMEPAGE, /* PS button */
  664. [0x14] = KEY_ENTER,
  665. };
  666. static const unsigned int ps3remote_keymap_remote_buttons[] = {
  667. [0x00] = KEY_1,
  668. [0x01] = KEY_2,
  669. [0x02] = KEY_3,
  670. [0x03] = KEY_4,
  671. [0x04] = KEY_5,
  672. [0x05] = KEY_6,
  673. [0x06] = KEY_7,
  674. [0x07] = KEY_8,
  675. [0x08] = KEY_9,
  676. [0x09] = KEY_0,
  677. [0x0e] = KEY_ESC, /* return */
  678. [0x0f] = KEY_CLEAR,
  679. [0x16] = KEY_EJECTCD,
  680. [0x1a] = KEY_MENU, /* top menu */
  681. [0x28] = KEY_TIME,
  682. [0x30] = KEY_PREVIOUS,
  683. [0x31] = KEY_NEXT,
  684. [0x32] = KEY_PLAY,
  685. [0x33] = KEY_REWIND, /* scan back */
  686. [0x34] = KEY_FORWARD, /* scan forward */
  687. [0x38] = KEY_STOP,
  688. [0x39] = KEY_PAUSE,
  689. [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
  690. [0x60] = KEY_FRAMEBACK, /* slow/step back */
  691. [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
  692. [0x63] = KEY_SUBTITLE,
  693. [0x64] = KEY_AUDIO,
  694. [0x65] = KEY_ANGLE,
  695. [0x70] = KEY_INFO, /* display */
  696. [0x80] = KEY_BLUE,
  697. [0x81] = KEY_RED,
  698. [0x82] = KEY_GREEN,
  699. [0x83] = KEY_YELLOW,
  700. };
  701. static const unsigned int buzz_keymap[] = {
  702. /*
  703. * The controller has 4 remote buzzers, each with one LED and 5
  704. * buttons.
  705. *
  706. * We use the mapping chosen by the controller, which is:
  707. *
  708. * Key Offset
  709. * -------------------
  710. * Buzz 1
  711. * Blue 5
  712. * Orange 4
  713. * Green 3
  714. * Yellow 2
  715. *
  716. * So, for example, the orange button on the third buzzer is mapped to
  717. * BTN_TRIGGER_HAPPY14
  718. */
  719. [ 1] = BTN_TRIGGER_HAPPY1,
  720. [ 2] = BTN_TRIGGER_HAPPY2,
  721. [ 3] = BTN_TRIGGER_HAPPY3,
  722. [ 4] = BTN_TRIGGER_HAPPY4,
  723. [ 5] = BTN_TRIGGER_HAPPY5,
  724. [ 6] = BTN_TRIGGER_HAPPY6,
  725. [ 7] = BTN_TRIGGER_HAPPY7,
  726. [ 8] = BTN_TRIGGER_HAPPY8,
  727. [ 9] = BTN_TRIGGER_HAPPY9,
  728. [10] = BTN_TRIGGER_HAPPY10,
  729. [11] = BTN_TRIGGER_HAPPY11,
  730. [12] = BTN_TRIGGER_HAPPY12,
  731. [13] = BTN_TRIGGER_HAPPY13,
  732. [14] = BTN_TRIGGER_HAPPY14,
  733. [15] = BTN_TRIGGER_HAPPY15,
  734. [16] = BTN_TRIGGER_HAPPY16,
  735. [17] = BTN_TRIGGER_HAPPY17,
  736. [18] = BTN_TRIGGER_HAPPY18,
  737. [19] = BTN_TRIGGER_HAPPY19,
  738. [20] = BTN_TRIGGER_HAPPY20,
  739. };
  740. static enum power_supply_property sony_battery_props[] = {
  741. POWER_SUPPLY_PROP_PRESENT,
  742. POWER_SUPPLY_PROP_CAPACITY,
  743. POWER_SUPPLY_PROP_SCOPE,
  744. POWER_SUPPLY_PROP_STATUS,
  745. };
  746. struct sixaxis_led {
  747. __u8 time_enabled; /* the total time the led is active (0xff means forever) */
  748. __u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
  749. __u8 enabled;
  750. __u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
  751. __u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
  752. } __packed;
  753. struct sixaxis_rumble {
  754. __u8 padding;
  755. __u8 right_duration; /* Right motor duration (0xff means forever) */
  756. __u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
  757. __u8 left_duration; /* Left motor duration (0xff means forever) */
  758. __u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
  759. } __packed;
  760. struct sixaxis_output_report {
  761. __u8 report_id;
  762. struct sixaxis_rumble rumble;
  763. __u8 padding[4];
  764. __u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
  765. struct sixaxis_led led[4]; /* LEDx at (4 - x) */
  766. struct sixaxis_led _reserved; /* LED5, not actually soldered */
  767. } __packed;
  768. union sixaxis_output_report_01 {
  769. struct sixaxis_output_report data;
  770. __u8 buf[36];
  771. };
  772. #define DS4_REPORT_0x02_SIZE 37
  773. #define DS4_REPORT_0x05_SIZE 32
  774. #define DS4_REPORT_0x11_SIZE 78
  775. #define DS4_REPORT_0x81_SIZE 7
  776. #define SIXAXIS_REPORT_0xF2_SIZE 18
  777. static spinlock_t sony_dev_list_lock;
  778. static LIST_HEAD(sony_device_list);
  779. static DEFINE_IDA(sony_device_id_allocator);
  780. struct sony_sc {
  781. spinlock_t lock;
  782. struct list_head list_node;
  783. struct hid_device *hdev;
  784. struct led_classdev *leds[MAX_LEDS];
  785. unsigned long quirks;
  786. struct work_struct state_worker;
  787. struct power_supply battery;
  788. int device_id;
  789. __u8 *output_report_dmabuf;
  790. #ifdef CONFIG_SONY_FF
  791. __u8 left;
  792. __u8 right;
  793. #endif
  794. __u8 mac_address[6];
  795. __u8 worker_initialized;
  796. __u8 cable_state;
  797. __u8 battery_charging;
  798. __u8 battery_capacity;
  799. __u8 led_state[MAX_LEDS];
  800. __u8 led_delay_on[MAX_LEDS];
  801. __u8 led_delay_off[MAX_LEDS];
  802. __u8 led_count;
  803. };
  804. static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
  805. unsigned int *rsize)
  806. {
  807. *rsize = sizeof(sixaxis_rdesc);
  808. return sixaxis_rdesc;
  809. }
  810. static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
  811. unsigned int *rsize)
  812. {
  813. *rsize = sizeof(ps3remote_rdesc);
  814. return ps3remote_rdesc;
  815. }
  816. static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
  817. struct hid_field *field, struct hid_usage *usage,
  818. unsigned long **bit, int *max)
  819. {
  820. unsigned int key = usage->hid & HID_USAGE;
  821. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  822. return -1;
  823. switch (usage->collection_index) {
  824. case 1:
  825. if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
  826. return -1;
  827. key = ps3remote_keymap_joypad_buttons[key];
  828. if (!key)
  829. return -1;
  830. break;
  831. case 2:
  832. if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
  833. return -1;
  834. key = ps3remote_keymap_remote_buttons[key];
  835. if (!key)
  836. return -1;
  837. break;
  838. default:
  839. return -1;
  840. }
  841. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  842. return 1;
  843. }
  844. static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  845. unsigned int *rsize)
  846. {
  847. struct sony_sc *sc = hid_get_drvdata(hdev);
  848. /*
  849. * Some Sony RF receivers wrongly declare the mouse pointer as a
  850. * a constant non-data variable.
  851. */
  852. if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
  853. /* usage page: generic desktop controls */
  854. /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
  855. /* usage: mouse */
  856. rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
  857. /* input (usage page for x,y axes): constant, variable, relative */
  858. rdesc[54] == 0x81 && rdesc[55] == 0x07) {
  859. hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
  860. /* input: data, variable, relative */
  861. rdesc[55] = 0x06;
  862. }
  863. /*
  864. * The default Dualshock 4 USB descriptor doesn't assign
  865. * the gyroscope values to corresponding axes so we need a
  866. * modified one.
  867. */
  868. if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
  869. hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
  870. rdesc = dualshock4_usb_rdesc;
  871. *rsize = sizeof(dualshock4_usb_rdesc);
  872. } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
  873. hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
  874. rdesc = dualshock4_bt_rdesc;
  875. *rsize = sizeof(dualshock4_bt_rdesc);
  876. }
  877. if (sc->quirks & SIXAXIS_CONTROLLER)
  878. return sixaxis_fixup(hdev, rdesc, rsize);
  879. if (sc->quirks & PS3REMOTE)
  880. return ps3remote_fixup(hdev, rdesc, rsize);
  881. return rdesc;
  882. }
  883. static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
  884. {
  885. static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
  886. unsigned long flags;
  887. __u8 cable_state, battery_capacity, battery_charging;
  888. /*
  889. * The sixaxis is charging if the battery value is 0xee
  890. * and it is fully charged if the value is 0xef.
  891. * It does not report the actual level while charging so it
  892. * is set to 100% while charging is in progress.
  893. */
  894. if (rd[30] >= 0xee) {
  895. battery_capacity = 100;
  896. battery_charging = !(rd[30] & 0x01);
  897. cable_state = 1;
  898. } else {
  899. __u8 index = rd[30] <= 5 ? rd[30] : 5;
  900. battery_capacity = sixaxis_battery_capacity[index];
  901. battery_charging = 0;
  902. cable_state = 0;
  903. }
  904. spin_lock_irqsave(&sc->lock, flags);
  905. sc->cable_state = cable_state;
  906. sc->battery_capacity = battery_capacity;
  907. sc->battery_charging = battery_charging;
  908. spin_unlock_irqrestore(&sc->lock, flags);
  909. }
  910. static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
  911. {
  912. struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
  913. struct hid_input, list);
  914. struct input_dev *input_dev = hidinput->input;
  915. unsigned long flags;
  916. int n, offset;
  917. __u8 cable_state, battery_capacity, battery_charging;
  918. /*
  919. * Battery and touchpad data starts at byte 30 in the USB report and
  920. * 32 in Bluetooth report.
  921. */
  922. offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
  923. /*
  924. * The lower 4 bits of byte 30 contain the battery level
  925. * and the 5th bit contains the USB cable state.
  926. */
  927. cable_state = (rd[offset] >> 4) & 0x01;
  928. battery_capacity = rd[offset] & 0x0F;
  929. /*
  930. * When a USB power source is connected the battery level ranges from
  931. * 0 to 10, and when running on battery power it ranges from 0 to 9.
  932. * A battery level above 10 when plugged in means charge completed.
  933. */
  934. if (!cable_state || battery_capacity > 10)
  935. battery_charging = 0;
  936. else
  937. battery_charging = 1;
  938. if (!cable_state)
  939. battery_capacity++;
  940. if (battery_capacity > 10)
  941. battery_capacity = 10;
  942. battery_capacity *= 10;
  943. spin_lock_irqsave(&sc->lock, flags);
  944. sc->cable_state = cable_state;
  945. sc->battery_capacity = battery_capacity;
  946. sc->battery_charging = battery_charging;
  947. spin_unlock_irqrestore(&sc->lock, flags);
  948. offset += 5;
  949. /*
  950. * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
  951. * and 37 on Bluetooth.
  952. * The first 7 bits of the first byte is a counter and bit 8 is a touch
  953. * indicator that is 0 when pressed and 1 when not pressed.
  954. * The next 3 bytes are two 12 bit touch coordinates, X and Y.
  955. * The data for the second touch is in the same format and immediatly
  956. * follows the data for the first.
  957. */
  958. for (n = 0; n < 2; n++) {
  959. __u16 x, y;
  960. x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
  961. y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
  962. input_mt_slot(input_dev, n);
  963. input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
  964. !(rd[offset] >> 7));
  965. input_report_abs(input_dev, ABS_MT_POSITION_X, x);
  966. input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
  967. offset += 4;
  968. }
  969. }
  970. static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
  971. __u8 *rd, int size)
  972. {
  973. struct sony_sc *sc = hid_get_drvdata(hdev);
  974. /*
  975. * Sixaxis HID report has acclerometers/gyro with MSByte first, this
  976. * has to be BYTE_SWAPPED before passing up to joystick interface
  977. */
  978. if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
  979. swap(rd[41], rd[42]);
  980. swap(rd[43], rd[44]);
  981. swap(rd[45], rd[46]);
  982. swap(rd[47], rd[48]);
  983. sixaxis_parse_report(sc, rd, size);
  984. } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
  985. size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
  986. && rd[0] == 0x11 && size == 78)) {
  987. dualshock4_parse_report(sc, rd, size);
  988. }
  989. return 0;
  990. }
  991. static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
  992. struct hid_field *field, struct hid_usage *usage,
  993. unsigned long **bit, int *max)
  994. {
  995. struct sony_sc *sc = hid_get_drvdata(hdev);
  996. if (sc->quirks & BUZZ_CONTROLLER) {
  997. unsigned int key = usage->hid & HID_USAGE;
  998. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  999. return -1;
  1000. switch (usage->collection_index) {
  1001. case 1:
  1002. if (key >= ARRAY_SIZE(buzz_keymap))
  1003. return -1;
  1004. key = buzz_keymap[key];
  1005. if (!key)
  1006. return -1;
  1007. break;
  1008. default:
  1009. return -1;
  1010. }
  1011. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  1012. return 1;
  1013. }
  1014. if (sc->quirks & PS3REMOTE)
  1015. return ps3remote_mapping(hdev, hi, field, usage, bit, max);
  1016. /* Let hid-core decide for the others */
  1017. return 0;
  1018. }
  1019. static int sony_register_touchpad(struct hid_input *hi, int touch_count,
  1020. int w, int h)
  1021. {
  1022. struct input_dev *input_dev = hi->input;
  1023. int ret;
  1024. ret = input_mt_init_slots(input_dev, touch_count, 0);
  1025. if (ret < 0)
  1026. return ret;
  1027. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
  1028. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
  1029. return 0;
  1030. }
  1031. static void sony_input_configured(struct hid_device *hdev,
  1032. struct hid_input *hidinput)
  1033. {
  1034. struct sony_sc *sc = hid_get_drvdata(hdev);
  1035. /*
  1036. * The Dualshock 4 touchpad supports 2 touches and has a
  1037. * resolution of 1920x942 (44.86 dots/mm).
  1038. */
  1039. if (sc->quirks & DUALSHOCK4_CONTROLLER) {
  1040. if (sony_register_touchpad(hidinput, 2, 1920, 942) != 0)
  1041. hid_err(sc->hdev,
  1042. "Unable to initialize multi-touch slots\n");
  1043. }
  1044. }
  1045. /*
  1046. * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
  1047. * to "operational". Without this, the ps3 controller will not report any
  1048. * events.
  1049. */
  1050. static int sixaxis_set_operational_usb(struct hid_device *hdev)
  1051. {
  1052. int ret;
  1053. char *buf = kmalloc(18, GFP_KERNEL);
  1054. if (!buf)
  1055. return -ENOMEM;
  1056. ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
  1057. HID_REQ_GET_REPORT);
  1058. if (ret < 0)
  1059. hid_err(hdev, "can't set operational mode\n");
  1060. kfree(buf);
  1061. return ret;
  1062. }
  1063. static int sixaxis_set_operational_bt(struct hid_device *hdev)
  1064. {
  1065. static const __u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
  1066. __u8 *buf;
  1067. int ret;
  1068. buf = kmemdup(report, sizeof(report), GFP_KERNEL);
  1069. if (!buf)
  1070. return -ENOMEM;
  1071. ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
  1072. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  1073. kfree(buf);
  1074. return ret;
  1075. }
  1076. /*
  1077. * Requesting feature report 0x02 in Bluetooth mode changes the state of the
  1078. * controller so that it sends full input reports of type 0x11.
  1079. */
  1080. static int dualshock4_set_operational_bt(struct hid_device *hdev)
  1081. {
  1082. __u8 *buf;
  1083. int ret;
  1084. buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
  1085. if (!buf)
  1086. return -ENOMEM;
  1087. ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
  1088. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  1089. kfree(buf);
  1090. return ret;
  1091. }
  1092. static void sixaxis_set_leds_from_id(int id, __u8 values[MAX_LEDS])
  1093. {
  1094. static const __u8 sixaxis_leds[10][4] = {
  1095. { 0x01, 0x00, 0x00, 0x00 },
  1096. { 0x00, 0x01, 0x00, 0x00 },
  1097. { 0x00, 0x00, 0x01, 0x00 },
  1098. { 0x00, 0x00, 0x00, 0x01 },
  1099. { 0x01, 0x00, 0x00, 0x01 },
  1100. { 0x00, 0x01, 0x00, 0x01 },
  1101. { 0x00, 0x00, 0x01, 0x01 },
  1102. { 0x01, 0x00, 0x01, 0x01 },
  1103. { 0x00, 0x01, 0x01, 0x01 },
  1104. { 0x01, 0x01, 0x01, 0x01 }
  1105. };
  1106. BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
  1107. if (id < 0)
  1108. return;
  1109. id %= 10;
  1110. memcpy(values, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
  1111. }
  1112. static void dualshock4_set_leds_from_id(int id, __u8 values[MAX_LEDS])
  1113. {
  1114. /* The first 4 color/index entries match what the PS4 assigns */
  1115. static const __u8 color_code[7][3] = {
  1116. /* Blue */ { 0x00, 0x00, 0x01 },
  1117. /* Red */ { 0x01, 0x00, 0x00 },
  1118. /* Green */ { 0x00, 0x01, 0x00 },
  1119. /* Pink */ { 0x02, 0x00, 0x01 },
  1120. /* Orange */ { 0x02, 0x01, 0x00 },
  1121. /* Teal */ { 0x00, 0x01, 0x01 },
  1122. /* White */ { 0x01, 0x01, 0x01 }
  1123. };
  1124. BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
  1125. if (id < 0)
  1126. return;
  1127. id %= 7;
  1128. memcpy(values, color_code[id], sizeof(color_code[id]));
  1129. }
  1130. static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
  1131. {
  1132. struct list_head *report_list =
  1133. &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
  1134. struct hid_report *report = list_entry(report_list->next,
  1135. struct hid_report, list);
  1136. __s32 *value = report->field[0]->value;
  1137. value[0] = 0x00;
  1138. value[1] = leds[0] ? 0xff : 0x00;
  1139. value[2] = leds[1] ? 0xff : 0x00;
  1140. value[3] = leds[2] ? 0xff : 0x00;
  1141. value[4] = leds[3] ? 0xff : 0x00;
  1142. value[5] = 0x00;
  1143. value[6] = 0x00;
  1144. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  1145. }
  1146. static void sony_set_leds(struct sony_sc *sc, const __u8 *leds, int count)
  1147. {
  1148. int n;
  1149. BUG_ON(count > MAX_LEDS);
  1150. if (sc->quirks & BUZZ_CONTROLLER && count == 4) {
  1151. buzz_set_leds(sc->hdev, leds);
  1152. } else {
  1153. for (n = 0; n < count; n++)
  1154. sc->led_state[n] = leds[n];
  1155. schedule_work(&sc->state_worker);
  1156. }
  1157. }
  1158. static void sony_led_set_brightness(struct led_classdev *led,
  1159. enum led_brightness value)
  1160. {
  1161. struct device *dev = led->dev->parent;
  1162. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  1163. struct sony_sc *drv_data;
  1164. int n;
  1165. int force_update;
  1166. drv_data = hid_get_drvdata(hdev);
  1167. if (!drv_data) {
  1168. hid_err(hdev, "No device data\n");
  1169. return;
  1170. }
  1171. /*
  1172. * The Sixaxis on USB will override any LED settings sent to it
  1173. * and keep flashing all of the LEDs until the PS button is pressed.
  1174. * Updates, even if redundant, must be always be sent to the
  1175. * controller to avoid having to toggle the state of an LED just to
  1176. * stop the flashing later on.
  1177. */
  1178. force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
  1179. for (n = 0; n < drv_data->led_count; n++) {
  1180. if (led == drv_data->leds[n] && (force_update ||
  1181. (value != drv_data->led_state[n] ||
  1182. drv_data->led_delay_on[n] ||
  1183. drv_data->led_delay_off[n]))) {
  1184. drv_data->led_state[n] = value;
  1185. /* Setting the brightness stops the blinking */
  1186. drv_data->led_delay_on[n] = 0;
  1187. drv_data->led_delay_off[n] = 0;
  1188. sony_set_leds(drv_data, drv_data->led_state,
  1189. drv_data->led_count);
  1190. break;
  1191. }
  1192. }
  1193. }
  1194. static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
  1195. {
  1196. struct device *dev = led->dev->parent;
  1197. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  1198. struct sony_sc *drv_data;
  1199. int n;
  1200. drv_data = hid_get_drvdata(hdev);
  1201. if (!drv_data) {
  1202. hid_err(hdev, "No device data\n");
  1203. return LED_OFF;
  1204. }
  1205. for (n = 0; n < drv_data->led_count; n++) {
  1206. if (led == drv_data->leds[n])
  1207. return drv_data->led_state[n];
  1208. }
  1209. return LED_OFF;
  1210. }
  1211. static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
  1212. unsigned long *delay_off)
  1213. {
  1214. struct device *dev = led->dev->parent;
  1215. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  1216. struct sony_sc *drv_data = hid_get_drvdata(hdev);
  1217. int n;
  1218. __u8 new_on, new_off;
  1219. if (!drv_data) {
  1220. hid_err(hdev, "No device data\n");
  1221. return -EINVAL;
  1222. }
  1223. /* Max delay is 255 deciseconds or 2550 milliseconds */
  1224. if (*delay_on > 2550)
  1225. *delay_on = 2550;
  1226. if (*delay_off > 2550)
  1227. *delay_off = 2550;
  1228. /* Blink at 1 Hz if both values are zero */
  1229. if (!*delay_on && !*delay_off)
  1230. *delay_on = *delay_off = 500;
  1231. new_on = *delay_on / 10;
  1232. new_off = *delay_off / 10;
  1233. for (n = 0; n < drv_data->led_count; n++) {
  1234. if (led == drv_data->leds[n])
  1235. break;
  1236. }
  1237. /* This LED is not registered on this device */
  1238. if (n >= drv_data->led_count)
  1239. return -EINVAL;
  1240. /* Don't schedule work if the values didn't change */
  1241. if (new_on != drv_data->led_delay_on[n] ||
  1242. new_off != drv_data->led_delay_off[n]) {
  1243. drv_data->led_delay_on[n] = new_on;
  1244. drv_data->led_delay_off[n] = new_off;
  1245. schedule_work(&drv_data->state_worker);
  1246. }
  1247. return 0;
  1248. }
  1249. static void sony_leds_remove(struct sony_sc *sc)
  1250. {
  1251. struct led_classdev *led;
  1252. int n;
  1253. BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
  1254. for (n = 0; n < sc->led_count; n++) {
  1255. led = sc->leds[n];
  1256. sc->leds[n] = NULL;
  1257. if (!led)
  1258. continue;
  1259. led_classdev_unregister(led);
  1260. kfree(led);
  1261. }
  1262. sc->led_count = 0;
  1263. }
  1264. static int sony_leds_init(struct sony_sc *sc)
  1265. {
  1266. struct hid_device *hdev = sc->hdev;
  1267. int n, ret = 0;
  1268. int use_ds4_names;
  1269. struct led_classdev *led;
  1270. size_t name_sz;
  1271. char *name;
  1272. size_t name_len;
  1273. const char *name_fmt;
  1274. static const char * const ds4_name_str[] = { "red", "green", "blue",
  1275. "global" };
  1276. __u8 initial_values[MAX_LEDS] = { 0 };
  1277. __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
  1278. __u8 use_hw_blink[MAX_LEDS] = { 0 };
  1279. BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
  1280. if (sc->quirks & BUZZ_CONTROLLER) {
  1281. sc->led_count = 4;
  1282. use_ds4_names = 0;
  1283. name_len = strlen("::buzz#");
  1284. name_fmt = "%s::buzz%d";
  1285. /* Validate expected report characteristics. */
  1286. if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
  1287. return -ENODEV;
  1288. } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
  1289. dualshock4_set_leds_from_id(sc->device_id, initial_values);
  1290. initial_values[3] = 1;
  1291. sc->led_count = 4;
  1292. memset(max_brightness, 255, 3);
  1293. use_hw_blink[3] = 1;
  1294. use_ds4_names = 1;
  1295. name_len = 0;
  1296. name_fmt = "%s:%s";
  1297. } else {
  1298. sixaxis_set_leds_from_id(sc->device_id, initial_values);
  1299. sc->led_count = 4;
  1300. memset(use_hw_blink, 1, 4);
  1301. use_ds4_names = 0;
  1302. name_len = strlen("::sony#");
  1303. name_fmt = "%s::sony%d";
  1304. }
  1305. /*
  1306. * Clear LEDs as we have no way of reading their initial state. This is
  1307. * only relevant if the driver is loaded after somebody actively set the
  1308. * LEDs to on
  1309. */
  1310. sony_set_leds(sc, initial_values, sc->led_count);
  1311. name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
  1312. for (n = 0; n < sc->led_count; n++) {
  1313. if (use_ds4_names)
  1314. name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
  1315. led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
  1316. if (!led) {
  1317. hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
  1318. ret = -ENOMEM;
  1319. goto error_leds;
  1320. }
  1321. name = (void *)(&led[1]);
  1322. if (use_ds4_names)
  1323. snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
  1324. ds4_name_str[n]);
  1325. else
  1326. snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
  1327. led->name = name;
  1328. led->brightness = initial_values[n];
  1329. led->max_brightness = max_brightness[n];
  1330. led->brightness_get = sony_led_get_brightness;
  1331. led->brightness_set = sony_led_set_brightness;
  1332. if (use_hw_blink[n])
  1333. led->blink_set = sony_led_blink_set;
  1334. sc->leds[n] = led;
  1335. ret = led_classdev_register(&hdev->dev, led);
  1336. if (ret) {
  1337. hid_err(hdev, "Failed to register LED %d\n", n);
  1338. sc->leds[n] = NULL;
  1339. kfree(led);
  1340. goto error_leds;
  1341. }
  1342. }
  1343. return ret;
  1344. error_leds:
  1345. sony_leds_remove(sc);
  1346. return ret;
  1347. }
  1348. static void sixaxis_state_worker(struct work_struct *work)
  1349. {
  1350. static const union sixaxis_output_report_01 default_report = {
  1351. .buf = {
  1352. 0x01,
  1353. 0x00, 0xff, 0x00, 0xff, 0x00,
  1354. 0x00, 0x00, 0x00, 0x00, 0x00,
  1355. 0xff, 0x27, 0x10, 0x00, 0x32,
  1356. 0xff, 0x27, 0x10, 0x00, 0x32,
  1357. 0xff, 0x27, 0x10, 0x00, 0x32,
  1358. 0xff, 0x27, 0x10, 0x00, 0x32,
  1359. 0x00, 0x00, 0x00, 0x00, 0x00
  1360. }
  1361. };
  1362. struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
  1363. struct sixaxis_output_report *report =
  1364. (struct sixaxis_output_report *)sc->output_report_dmabuf;
  1365. int n;
  1366. /* Initialize the report with default values */
  1367. memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
  1368. #ifdef CONFIG_SONY_FF
  1369. report->rumble.right_motor_on = sc->right ? 1 : 0;
  1370. report->rumble.left_motor_force = sc->left;
  1371. #endif
  1372. report->leds_bitmap |= sc->led_state[0] << 1;
  1373. report->leds_bitmap |= sc->led_state[1] << 2;
  1374. report->leds_bitmap |= sc->led_state[2] << 3;
  1375. report->leds_bitmap |= sc->led_state[3] << 4;
  1376. /* Set flag for all leds off, required for 3rd party INTEC controller */
  1377. if ((report->leds_bitmap & 0x1E) == 0)
  1378. report->leds_bitmap |= 0x20;
  1379. /*
  1380. * The LEDs in the report are indexed in reverse order to their
  1381. * corresponding light on the controller.
  1382. * Index 0 = LED 4, index 1 = LED 3, etc...
  1383. *
  1384. * In the case of both delay values being zero (blinking disabled) the
  1385. * default report values should be used or the controller LED will be
  1386. * always off.
  1387. */
  1388. for (n = 0; n < 4; n++) {
  1389. if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
  1390. report->led[3 - n].duty_off = sc->led_delay_off[n];
  1391. report->led[3 - n].duty_on = sc->led_delay_on[n];
  1392. }
  1393. }
  1394. hid_hw_raw_request(sc->hdev, report->report_id, (__u8 *)report,
  1395. sizeof(struct sixaxis_output_report),
  1396. HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
  1397. }
  1398. static void dualshock4_state_worker(struct work_struct *work)
  1399. {
  1400. struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
  1401. struct hid_device *hdev = sc->hdev;
  1402. __u8 *buf = sc->output_report_dmabuf;
  1403. int offset;
  1404. if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
  1405. memset(buf, 0, DS4_REPORT_0x05_SIZE);
  1406. buf[0] = 0x05;
  1407. buf[1] = 0xFF;
  1408. offset = 4;
  1409. } else {
  1410. memset(buf, 0, DS4_REPORT_0x11_SIZE);
  1411. buf[0] = 0x11;
  1412. buf[1] = 0xB0;
  1413. buf[3] = 0x0F;
  1414. offset = 6;
  1415. }
  1416. #ifdef CONFIG_SONY_FF
  1417. buf[offset++] = sc->right;
  1418. buf[offset++] = sc->left;
  1419. #else
  1420. offset += 2;
  1421. #endif
  1422. /* LED 3 is the global control */
  1423. if (sc->led_state[3]) {
  1424. buf[offset++] = sc->led_state[0];
  1425. buf[offset++] = sc->led_state[1];
  1426. buf[offset++] = sc->led_state[2];
  1427. } else {
  1428. offset += 3;
  1429. }
  1430. /* If both delay values are zero the DualShock 4 disables blinking. */
  1431. buf[offset++] = sc->led_delay_on[3];
  1432. buf[offset++] = sc->led_delay_off[3];
  1433. if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
  1434. hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
  1435. else
  1436. hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
  1437. HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
  1438. }
  1439. static int sony_allocate_output_report(struct sony_sc *sc)
  1440. {
  1441. if (sc->quirks & SIXAXIS_CONTROLLER)
  1442. sc->output_report_dmabuf =
  1443. kmalloc(sizeof(union sixaxis_output_report_01),
  1444. GFP_KERNEL);
  1445. else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
  1446. sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
  1447. GFP_KERNEL);
  1448. else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
  1449. sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
  1450. GFP_KERNEL);
  1451. else
  1452. return 0;
  1453. if (!sc->output_report_dmabuf)
  1454. return -ENOMEM;
  1455. return 0;
  1456. }
  1457. #ifdef CONFIG_SONY_FF
  1458. static int sony_play_effect(struct input_dev *dev, void *data,
  1459. struct ff_effect *effect)
  1460. {
  1461. struct hid_device *hid = input_get_drvdata(dev);
  1462. struct sony_sc *sc = hid_get_drvdata(hid);
  1463. if (effect->type != FF_RUMBLE)
  1464. return 0;
  1465. sc->left = effect->u.rumble.strong_magnitude / 256;
  1466. sc->right = effect->u.rumble.weak_magnitude / 256;
  1467. schedule_work(&sc->state_worker);
  1468. return 0;
  1469. }
  1470. static int sony_init_ff(struct sony_sc *sc)
  1471. {
  1472. struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
  1473. struct hid_input, list);
  1474. struct input_dev *input_dev = hidinput->input;
  1475. input_set_capability(input_dev, EV_FF, FF_RUMBLE);
  1476. return input_ff_create_memless(input_dev, NULL, sony_play_effect);
  1477. }
  1478. #else
  1479. static int sony_init_ff(struct sony_sc *sc)
  1480. {
  1481. return 0;
  1482. }
  1483. #endif
  1484. static int sony_battery_get_property(struct power_supply *psy,
  1485. enum power_supply_property psp,
  1486. union power_supply_propval *val)
  1487. {
  1488. struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
  1489. unsigned long flags;
  1490. int ret = 0;
  1491. u8 battery_charging, battery_capacity, cable_state;
  1492. spin_lock_irqsave(&sc->lock, flags);
  1493. battery_charging = sc->battery_charging;
  1494. battery_capacity = sc->battery_capacity;
  1495. cable_state = sc->cable_state;
  1496. spin_unlock_irqrestore(&sc->lock, flags);
  1497. switch (psp) {
  1498. case POWER_SUPPLY_PROP_PRESENT:
  1499. val->intval = 1;
  1500. break;
  1501. case POWER_SUPPLY_PROP_SCOPE:
  1502. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  1503. break;
  1504. case POWER_SUPPLY_PROP_CAPACITY:
  1505. val->intval = battery_capacity;
  1506. break;
  1507. case POWER_SUPPLY_PROP_STATUS:
  1508. if (battery_charging)
  1509. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  1510. else
  1511. if (battery_capacity == 100 && cable_state)
  1512. val->intval = POWER_SUPPLY_STATUS_FULL;
  1513. else
  1514. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  1515. break;
  1516. default:
  1517. ret = -EINVAL;
  1518. break;
  1519. }
  1520. return ret;
  1521. }
  1522. static int sony_battery_probe(struct sony_sc *sc)
  1523. {
  1524. struct hid_device *hdev = sc->hdev;
  1525. int ret;
  1526. /*
  1527. * Set the default battery level to 100% to avoid low battery warnings
  1528. * if the battery is polled before the first device report is received.
  1529. */
  1530. sc->battery_capacity = 100;
  1531. sc->battery.properties = sony_battery_props;
  1532. sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
  1533. sc->battery.get_property = sony_battery_get_property;
  1534. sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  1535. sc->battery.use_for_apm = 0;
  1536. sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%pMR",
  1537. sc->mac_address);
  1538. if (!sc->battery.name)
  1539. return -ENOMEM;
  1540. ret = power_supply_register(&hdev->dev, &sc->battery);
  1541. if (ret) {
  1542. hid_err(hdev, "Unable to register battery device\n");
  1543. goto err_free;
  1544. }
  1545. power_supply_powers(&sc->battery, &hdev->dev);
  1546. return 0;
  1547. err_free:
  1548. kfree(sc->battery.name);
  1549. sc->battery.name = NULL;
  1550. return ret;
  1551. }
  1552. static void sony_battery_remove(struct sony_sc *sc)
  1553. {
  1554. if (!sc->battery.name)
  1555. return;
  1556. power_supply_unregister(&sc->battery);
  1557. kfree(sc->battery.name);
  1558. sc->battery.name = NULL;
  1559. }
  1560. /*
  1561. * If a controller is plugged in via USB while already connected via Bluetooth
  1562. * it will show up as two devices. A global list of connected controllers and
  1563. * their MAC addresses is maintained to ensure that a device is only connected
  1564. * once.
  1565. */
  1566. static int sony_check_add_dev_list(struct sony_sc *sc)
  1567. {
  1568. struct sony_sc *entry;
  1569. unsigned long flags;
  1570. int ret;
  1571. spin_lock_irqsave(&sony_dev_list_lock, flags);
  1572. list_for_each_entry(entry, &sony_device_list, list_node) {
  1573. ret = memcmp(sc->mac_address, entry->mac_address,
  1574. sizeof(sc->mac_address));
  1575. if (!ret) {
  1576. ret = -EEXIST;
  1577. hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
  1578. sc->mac_address);
  1579. goto unlock;
  1580. }
  1581. }
  1582. ret = 0;
  1583. list_add(&(sc->list_node), &sony_device_list);
  1584. unlock:
  1585. spin_unlock_irqrestore(&sony_dev_list_lock, flags);
  1586. return ret;
  1587. }
  1588. static void sony_remove_dev_list(struct sony_sc *sc)
  1589. {
  1590. unsigned long flags;
  1591. if (sc->list_node.next) {
  1592. spin_lock_irqsave(&sony_dev_list_lock, flags);
  1593. list_del(&(sc->list_node));
  1594. spin_unlock_irqrestore(&sony_dev_list_lock, flags);
  1595. }
  1596. }
  1597. static int sony_get_bt_devaddr(struct sony_sc *sc)
  1598. {
  1599. int ret;
  1600. /* HIDP stores the device MAC address as a string in the uniq field. */
  1601. ret = strlen(sc->hdev->uniq);
  1602. if (ret != 17)
  1603. return -EINVAL;
  1604. ret = sscanf(sc->hdev->uniq,
  1605. "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  1606. &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
  1607. &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
  1608. if (ret != 6)
  1609. return -EINVAL;
  1610. return 0;
  1611. }
  1612. static int sony_check_add(struct sony_sc *sc)
  1613. {
  1614. __u8 *buf = NULL;
  1615. int n, ret;
  1616. if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
  1617. (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
  1618. /*
  1619. * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
  1620. * address from the uniq string where HIDP stores it.
  1621. * As uniq cannot be guaranteed to be a MAC address in all cases
  1622. * a failure of this function should not prevent the connection.
  1623. */
  1624. if (sony_get_bt_devaddr(sc) < 0) {
  1625. hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
  1626. return 0;
  1627. }
  1628. } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
  1629. buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
  1630. if (!buf)
  1631. return -ENOMEM;
  1632. /*
  1633. * The MAC address of a DS4 controller connected via USB can be
  1634. * retrieved with feature report 0x81. The address begins at
  1635. * offset 1.
  1636. */
  1637. ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
  1638. DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
  1639. HID_REQ_GET_REPORT);
  1640. if (ret != DS4_REPORT_0x81_SIZE) {
  1641. hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
  1642. ret = ret < 0 ? ret : -EINVAL;
  1643. goto out_free;
  1644. }
  1645. memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
  1646. } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
  1647. buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
  1648. if (!buf)
  1649. return -ENOMEM;
  1650. /*
  1651. * The MAC address of a Sixaxis controller connected via USB can
  1652. * be retrieved with feature report 0xf2. The address begins at
  1653. * offset 4.
  1654. */
  1655. ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
  1656. SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
  1657. HID_REQ_GET_REPORT);
  1658. if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
  1659. hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
  1660. ret = ret < 0 ? ret : -EINVAL;
  1661. goto out_free;
  1662. }
  1663. /*
  1664. * The Sixaxis device MAC in the report is big-endian and must
  1665. * be byte-swapped.
  1666. */
  1667. for (n = 0; n < 6; n++)
  1668. sc->mac_address[5-n] = buf[4+n];
  1669. } else {
  1670. return 0;
  1671. }
  1672. ret = sony_check_add_dev_list(sc);
  1673. out_free:
  1674. kfree(buf);
  1675. return ret;
  1676. }
  1677. static int sony_set_device_id(struct sony_sc *sc)
  1678. {
  1679. int ret;
  1680. /*
  1681. * Only DualShock 4 or Sixaxis controllers get an id.
  1682. * All others are set to -1.
  1683. */
  1684. if ((sc->quirks & SIXAXIS_CONTROLLER) ||
  1685. (sc->quirks & DUALSHOCK4_CONTROLLER)) {
  1686. ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
  1687. GFP_KERNEL);
  1688. if (ret < 0) {
  1689. sc->device_id = -1;
  1690. return ret;
  1691. }
  1692. sc->device_id = ret;
  1693. } else {
  1694. sc->device_id = -1;
  1695. }
  1696. return 0;
  1697. }
  1698. static void sony_release_device_id(struct sony_sc *sc)
  1699. {
  1700. if (sc->device_id >= 0) {
  1701. ida_simple_remove(&sony_device_id_allocator, sc->device_id);
  1702. sc->device_id = -1;
  1703. }
  1704. }
  1705. static inline void sony_init_work(struct sony_sc *sc,
  1706. void (*worker)(struct work_struct *))
  1707. {
  1708. if (!sc->worker_initialized)
  1709. INIT_WORK(&sc->state_worker, worker);
  1710. sc->worker_initialized = 1;
  1711. }
  1712. static inline void sony_cancel_work_sync(struct sony_sc *sc)
  1713. {
  1714. if (sc->worker_initialized)
  1715. cancel_work_sync(&sc->state_worker);
  1716. }
  1717. static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
  1718. {
  1719. int ret;
  1720. unsigned long quirks = id->driver_data;
  1721. struct sony_sc *sc;
  1722. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  1723. sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
  1724. if (sc == NULL) {
  1725. hid_err(hdev, "can't alloc sony descriptor\n");
  1726. return -ENOMEM;
  1727. }
  1728. sc->quirks = quirks;
  1729. hid_set_drvdata(hdev, sc);
  1730. sc->hdev = hdev;
  1731. ret = hid_parse(hdev);
  1732. if (ret) {
  1733. hid_err(hdev, "parse failed\n");
  1734. return ret;
  1735. }
  1736. if (sc->quirks & VAIO_RDESC_CONSTANT)
  1737. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  1738. else if (sc->quirks & SIXAXIS_CONTROLLER)
  1739. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  1740. ret = hid_hw_start(hdev, connect_mask);
  1741. if (ret) {
  1742. hid_err(hdev, "hw start failed\n");
  1743. return ret;
  1744. }
  1745. ret = sony_allocate_output_report(sc);
  1746. if (ret < 0) {
  1747. hid_err(hdev, "failed to allocate the output report buffer\n");
  1748. goto err_stop;
  1749. }
  1750. ret = sony_set_device_id(sc);
  1751. if (ret < 0) {
  1752. hid_err(hdev, "failed to allocate the device id\n");
  1753. goto err_stop;
  1754. }
  1755. if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
  1756. /*
  1757. * The Sony Sixaxis does not handle HID Output Reports on the
  1758. * Interrupt EP like it could, so we need to force HID Output
  1759. * Reports to use HID_REQ_SET_REPORT on the Control EP.
  1760. *
  1761. * There is also another issue about HID Output Reports via USB,
  1762. * the Sixaxis does not want the report_id as part of the data
  1763. * packet, so we have to discard buf[0] when sending the actual
  1764. * control message, even for numbered reports, humpf!
  1765. */
  1766. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  1767. hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
  1768. ret = sixaxis_set_operational_usb(hdev);
  1769. sony_init_work(sc, sixaxis_state_worker);
  1770. } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
  1771. /*
  1772. * The Sixaxis wants output reports sent on the ctrl endpoint
  1773. * when connected via Bluetooth.
  1774. */
  1775. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  1776. ret = sixaxis_set_operational_bt(hdev);
  1777. sony_init_work(sc, sixaxis_state_worker);
  1778. } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
  1779. if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
  1780. /*
  1781. * The DualShock 4 wants output reports sent on the ctrl
  1782. * endpoint when connected via Bluetooth.
  1783. */
  1784. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  1785. ret = dualshock4_set_operational_bt(hdev);
  1786. if (ret < 0) {
  1787. hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
  1788. goto err_stop;
  1789. }
  1790. }
  1791. sony_init_work(sc, dualshock4_state_worker);
  1792. } else {
  1793. ret = 0;
  1794. }
  1795. if (ret < 0)
  1796. goto err_stop;
  1797. ret = sony_check_add(sc);
  1798. if (ret < 0)
  1799. goto err_stop;
  1800. if (sc->quirks & SONY_LED_SUPPORT) {
  1801. ret = sony_leds_init(sc);
  1802. if (ret < 0)
  1803. goto err_stop;
  1804. }
  1805. if (sc->quirks & SONY_BATTERY_SUPPORT) {
  1806. ret = sony_battery_probe(sc);
  1807. if (ret < 0)
  1808. goto err_stop;
  1809. /* Open the device to receive reports with battery info */
  1810. ret = hid_hw_open(hdev);
  1811. if (ret < 0) {
  1812. hid_err(hdev, "hw open failed\n");
  1813. goto err_stop;
  1814. }
  1815. }
  1816. if (sc->quirks & SONY_FF_SUPPORT) {
  1817. ret = sony_init_ff(sc);
  1818. if (ret < 0)
  1819. goto err_close;
  1820. }
  1821. return 0;
  1822. err_close:
  1823. hid_hw_close(hdev);
  1824. err_stop:
  1825. if (sc->quirks & SONY_LED_SUPPORT)
  1826. sony_leds_remove(sc);
  1827. if (sc->quirks & SONY_BATTERY_SUPPORT)
  1828. sony_battery_remove(sc);
  1829. sony_cancel_work_sync(sc);
  1830. kfree(sc->output_report_dmabuf);
  1831. sony_remove_dev_list(sc);
  1832. sony_release_device_id(sc);
  1833. hid_hw_stop(hdev);
  1834. return ret;
  1835. }
  1836. static void sony_remove(struct hid_device *hdev)
  1837. {
  1838. struct sony_sc *sc = hid_get_drvdata(hdev);
  1839. if (sc->quirks & SONY_LED_SUPPORT)
  1840. sony_leds_remove(sc);
  1841. if (sc->quirks & SONY_BATTERY_SUPPORT) {
  1842. hid_hw_close(hdev);
  1843. sony_battery_remove(sc);
  1844. }
  1845. sony_cancel_work_sync(sc);
  1846. kfree(sc->output_report_dmabuf);
  1847. sony_remove_dev_list(sc);
  1848. sony_release_device_id(sc);
  1849. hid_hw_stop(hdev);
  1850. }
  1851. static const struct hid_device_id sony_devices[] = {
  1852. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  1853. .driver_data = SIXAXIS_CONTROLLER_USB },
  1854. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
  1855. .driver_data = SIXAXIS_CONTROLLER_USB },
  1856. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  1857. .driver_data = SIXAXIS_CONTROLLER_BT },
  1858. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
  1859. .driver_data = VAIO_RDESC_CONSTANT },
  1860. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
  1861. .driver_data = VAIO_RDESC_CONSTANT },
  1862. /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
  1863. * Logitech joystick from the device descriptor. */
  1864. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
  1865. .driver_data = BUZZ_CONTROLLER },
  1866. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
  1867. .driver_data = BUZZ_CONTROLLER },
  1868. /* PS3 BD Remote Control */
  1869. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
  1870. .driver_data = PS3REMOTE },
  1871. /* Logitech Harmony Adapter for PS3 */
  1872. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
  1873. .driver_data = PS3REMOTE },
  1874. /* SMK-Link PS3 BD Remote Control */
  1875. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
  1876. .driver_data = PS3REMOTE },
  1877. /* Sony Dualshock 4 controllers for PS4 */
  1878. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
  1879. .driver_data = DUALSHOCK4_CONTROLLER_USB },
  1880. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
  1881. .driver_data = DUALSHOCK4_CONTROLLER_BT },
  1882. { }
  1883. };
  1884. MODULE_DEVICE_TABLE(hid, sony_devices);
  1885. static struct hid_driver sony_driver = {
  1886. .name = "sony",
  1887. .id_table = sony_devices,
  1888. .input_mapping = sony_mapping,
  1889. .input_configured = sony_input_configured,
  1890. .probe = sony_probe,
  1891. .remove = sony_remove,
  1892. .report_fixup = sony_report_fixup,
  1893. .raw_event = sony_raw_event
  1894. };
  1895. static int __init sony_init(void)
  1896. {
  1897. dbg_hid("Sony:%s\n", __func__);
  1898. return hid_register_driver(&sony_driver);
  1899. }
  1900. static void __exit sony_exit(void)
  1901. {
  1902. dbg_hid("Sony:%s\n", __func__);
  1903. ida_destroy(&sony_device_id_allocator);
  1904. hid_unregister_driver(&sony_driver);
  1905. }
  1906. module_init(sony_init);
  1907. module_exit(sony_exit);
  1908. MODULE_LICENSE("GPL");