hid-sony.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID driver for Sony / PS2 / PS3 / PS4 BD devices.
  4. *
  5. * Copyright (c) 1999 Andreas Gal
  6. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  7. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  8. * Copyright (c) 2008 Jiri Slaby
  9. * Copyright (c) 2012 David Dillow <dave@thedillows.org>
  10. * Copyright (c) 2006-2013 Jiri Kosina
  11. * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
  12. * Copyright (c) 2014-2016 Frank Praznik <frank.praznik@gmail.com>
  13. * Copyright (c) 2018 Todd Kelner
  14. * Copyright (c) 2020-2021 Pascal Giard <pascal.giard@etsmtl.ca>
  15. * Copyright (c) 2020 Sanjay Govind <sanjay.govind9@gmail.com>
  16. * Copyright (c) 2021 Daniel Nguyen <daniel.nguyen.1@ens.etsmtl.ca>
  17. */
  18. /*
  19. */
  20. /*
  21. * NOTE: in order for the Sony PS3 BD Remote Control to be found by
  22. * a Bluetooth host, the key combination Start+Enter has to be kept pressed
  23. * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
  24. *
  25. * There will be no PIN request from the device.
  26. */
  27. #include <linux/device.h>
  28. #include <linux/hid.h>
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/leds.h>
  32. #include <linux/power_supply.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/list.h>
  35. #include <linux/idr.h>
  36. #include <linux/input/mt.h>
  37. #include <linux/crc32.h>
  38. #include <linux/usb.h>
  39. #include <linux/timer.h>
  40. #include <linux/unaligned.h>
  41. #include "hid-ids.h"
  42. #define VAIO_RDESC_CONSTANT BIT(0)
  43. #define SIXAXIS_CONTROLLER_USB BIT(1)
  44. #define SIXAXIS_CONTROLLER_BT BIT(2)
  45. #define BUZZ_CONTROLLER BIT(3)
  46. #define PS3REMOTE BIT(4)
  47. #define MOTION_CONTROLLER_USB BIT(5)
  48. #define MOTION_CONTROLLER_BT BIT(6)
  49. #define NAVIGATION_CONTROLLER_USB BIT(7)
  50. #define NAVIGATION_CONTROLLER_BT BIT(8)
  51. #define SINO_LITE_CONTROLLER BIT(9)
  52. #define FUTUREMAX_DANCE_MAT BIT(10)
  53. #define NSG_MR5U_REMOTE_BT BIT(11)
  54. #define NSG_MR7U_REMOTE_BT BIT(12)
  55. #define SHANWAN_GAMEPAD BIT(13)
  56. #define GH_GUITAR_CONTROLLER BIT(14)
  57. #define GHL_GUITAR_PS3WIIU BIT(15)
  58. #define GHL_GUITAR_PS4 BIT(16)
  59. #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
  60. #define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
  61. #define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
  62. NAVIGATION_CONTROLLER_BT)
  63. #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
  64. MOTION_CONTROLLER | NAVIGATION_CONTROLLER)
  65. #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
  66. #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | MOTION_CONTROLLER)
  67. #define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT)
  68. #define NSG_MRXU_REMOTE (NSG_MR5U_REMOTE_BT | NSG_MR7U_REMOTE_BT)
  69. #define MAX_LEDS 4
  70. #define NSG_MRXU_MAX_X 1667
  71. #define NSG_MRXU_MAX_Y 1868
  72. /* The PS3/Wii U dongles require a poke every 10 seconds, but the PS4
  73. * requires one every 8 seconds. Using 8 seconds for all for simplicity.
  74. */
  75. #define GHL_GUITAR_POKE_INTERVAL 8 /* In seconds */
  76. #define GUITAR_TILT_USAGE 44
  77. /* Magic data taken from GHLtarUtility:
  78. * https://github.com/ghlre/GHLtarUtility/blob/master/PS3Guitar.cs
  79. * Note: The Wii U and PS3 dongles happen to share the same!
  80. */
  81. static const char ghl_ps3wiiu_magic_data[] = {
  82. 0x02, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00
  83. };
  84. /* Magic data for the PS4 dongles sniffed with a USB protocol
  85. * analyzer.
  86. */
  87. static const char ghl_ps4_magic_data[] = {
  88. 0x30, 0x02, 0x08, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00
  89. };
  90. /* PS/3 Motion controller */
  91. static const u8 motion_rdesc[] = {
  92. 0x05, 0x01, /* Usage Page (Desktop), */
  93. 0x09, 0x04, /* Usage (Joystick), */
  94. 0xA1, 0x01, /* Collection (Application), */
  95. 0xA1, 0x02, /* Collection (Logical), */
  96. 0x85, 0x01, /* Report ID (1), */
  97. 0x75, 0x01, /* Report Size (1), */
  98. 0x95, 0x15, /* Report Count (21), */
  99. 0x15, 0x00, /* Logical Minimum (0), */
  100. 0x25, 0x01, /* Logical Maximum (1), */
  101. 0x35, 0x00, /* Physical Minimum (0), */
  102. 0x45, 0x01, /* Physical Maximum (1), */
  103. 0x05, 0x09, /* Usage Page (Button), */
  104. 0x19, 0x01, /* Usage Minimum (01h), */
  105. 0x29, 0x15, /* Usage Maximum (15h), */
  106. 0x81, 0x02, /* Input (Variable), * Buttons */
  107. 0x95, 0x0B, /* Report Count (11), */
  108. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  109. 0x81, 0x03, /* Input (Constant, Variable), * Padding */
  110. 0x15, 0x00, /* Logical Minimum (0), */
  111. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  112. 0x05, 0x01, /* Usage Page (Desktop), */
  113. 0xA1, 0x00, /* Collection (Physical), */
  114. 0x75, 0x08, /* Report Size (8), */
  115. 0x95, 0x01, /* Report Count (1), */
  116. 0x35, 0x00, /* Physical Minimum (0), */
  117. 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
  118. 0x09, 0x30, /* Usage (X), */
  119. 0x81, 0x02, /* Input (Variable), * Trigger */
  120. 0xC0, /* End Collection, */
  121. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  122. 0x75, 0x08, /* Report Size (8), */
  123. 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
  124. 0x81, 0x02, /* Input (Variable), */
  125. 0x05, 0x01, /* Usage Page (Desktop), */
  126. 0x75, 0x10, /* Report Size (16), */
  127. 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
  128. 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
  129. 0x95, 0x03, /* Report Count (3), * 3x Accels */
  130. 0x09, 0x33, /* Usage (rX), */
  131. 0x09, 0x34, /* Usage (rY), */
  132. 0x09, 0x35, /* Usage (rZ), */
  133. 0x81, 0x02, /* Input (Variable), */
  134. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  135. 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
  136. 0x81, 0x02, /* Input (Variable), */
  137. 0x05, 0x01, /* Usage Page (Desktop), */
  138. 0x09, 0x01, /* Usage (Pointer), */
  139. 0x95, 0x03, /* Report Count (3), * 3x Gyros */
  140. 0x81, 0x02, /* Input (Variable), */
  141. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  142. 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
  143. 0x81, 0x02, /* Input (Variable), */
  144. 0x75, 0x0C, /* Report Size (12), */
  145. 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
  146. 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
  147. 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
  148. 0x81, 0x02, /* Input (Variable), */
  149. 0x75, 0x08, /* Report Size (8), */
  150. 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
  151. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  152. 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
  153. 0x81, 0x02, /* Input (Variable), */
  154. 0x75, 0x08, /* Report Size (8), */
  155. 0x95, 0x30, /* Report Count (48), */
  156. 0x09, 0x01, /* Usage (Pointer), */
  157. 0x91, 0x02, /* Output (Variable), */
  158. 0x75, 0x08, /* Report Size (8), */
  159. 0x95, 0x30, /* Report Count (48), */
  160. 0x09, 0x01, /* Usage (Pointer), */
  161. 0xB1, 0x02, /* Feature (Variable), */
  162. 0xC0, /* End Collection, */
  163. 0xA1, 0x02, /* Collection (Logical), */
  164. 0x85, 0x02, /* Report ID (2), */
  165. 0x75, 0x08, /* Report Size (8), */
  166. 0x95, 0x30, /* Report Count (48), */
  167. 0x09, 0x01, /* Usage (Pointer), */
  168. 0xB1, 0x02, /* Feature (Variable), */
  169. 0xC0, /* End Collection, */
  170. 0xA1, 0x02, /* Collection (Logical), */
  171. 0x85, 0xEE, /* Report ID (238), */
  172. 0x75, 0x08, /* Report Size (8), */
  173. 0x95, 0x30, /* Report Count (48), */
  174. 0x09, 0x01, /* Usage (Pointer), */
  175. 0xB1, 0x02, /* Feature (Variable), */
  176. 0xC0, /* End Collection, */
  177. 0xA1, 0x02, /* Collection (Logical), */
  178. 0x85, 0xEF, /* Report ID (239), */
  179. 0x75, 0x08, /* Report Size (8), */
  180. 0x95, 0x30, /* Report Count (48), */
  181. 0x09, 0x01, /* Usage (Pointer), */
  182. 0xB1, 0x02, /* Feature (Variable), */
  183. 0xC0, /* End Collection, */
  184. 0xC0 /* End Collection */
  185. };
  186. static const u8 ps3remote_rdesc[] = {
  187. 0x05, 0x01, /* GUsagePage Generic Desktop */
  188. 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
  189. 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
  190. /* Use collection 1 for joypad buttons */
  191. 0xA1, 0x02, /* MCollection Logical (interrelated data) */
  192. /*
  193. * Ignore the 1st byte, maybe it is used for a controller
  194. * number but it's not needed for correct operation
  195. */
  196. 0x75, 0x08, /* GReportSize 0x08 [8] */
  197. 0x95, 0x01, /* GReportCount 0x01 [1] */
  198. 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
  199. /*
  200. * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
  201. * buttons multiple keypresses are allowed
  202. */
  203. 0x05, 0x09, /* GUsagePage Button */
  204. 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
  205. 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
  206. 0x14, /* GLogicalMinimum [0] */
  207. 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
  208. 0x75, 0x01, /* GReportSize 0x01 [1] */
  209. 0x95, 0x18, /* GReportCount 0x18 [24] */
  210. 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
  211. 0xC0, /* MEndCollection */
  212. /* Use collection 2 for remote control buttons */
  213. 0xA1, 0x02, /* MCollection Logical (interrelated data) */
  214. /* 5th byte is used for remote control buttons */
  215. 0x05, 0x09, /* GUsagePage Button */
  216. 0x18, /* LUsageMinimum [No button pressed] */
  217. 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
  218. 0x14, /* GLogicalMinimum [0] */
  219. 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
  220. 0x75, 0x08, /* GReportSize 0x08 [8] */
  221. 0x95, 0x01, /* GReportCount 0x01 [1] */
  222. 0x80, /* MInput */
  223. /*
  224. * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
  225. * 0xff and 11th is for press indication
  226. */
  227. 0x75, 0x08, /* GReportSize 0x08 [8] */
  228. 0x95, 0x06, /* GReportCount 0x06 [6] */
  229. 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
  230. /* 12th byte is for battery strength */
  231. 0x05, 0x06, /* GUsagePage Generic Device Controls */
  232. 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
  233. 0x14, /* GLogicalMinimum [0] */
  234. 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
  235. 0x75, 0x08, /* GReportSize 0x08 [8] */
  236. 0x95, 0x01, /* GReportCount 0x01 [1] */
  237. 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
  238. 0xC0, /* MEndCollection */
  239. 0xC0 /* MEndCollection [Game Pad] */
  240. };
  241. static const unsigned int ps3remote_keymap_joypad_buttons[] = {
  242. [0x01] = KEY_SELECT,
  243. [0x02] = BTN_THUMBL, /* L3 */
  244. [0x03] = BTN_THUMBR, /* R3 */
  245. [0x04] = BTN_START,
  246. [0x05] = KEY_UP,
  247. [0x06] = KEY_RIGHT,
  248. [0x07] = KEY_DOWN,
  249. [0x08] = KEY_LEFT,
  250. [0x09] = BTN_TL2, /* L2 */
  251. [0x0a] = BTN_TR2, /* R2 */
  252. [0x0b] = BTN_TL, /* L1 */
  253. [0x0c] = BTN_TR, /* R1 */
  254. [0x0d] = KEY_OPTION, /* options/triangle */
  255. [0x0e] = KEY_BACK, /* back/circle */
  256. [0x0f] = BTN_0, /* cross */
  257. [0x10] = KEY_SCREEN, /* view/square */
  258. [0x11] = KEY_HOMEPAGE, /* PS button */
  259. [0x14] = KEY_ENTER,
  260. };
  261. static const unsigned int ps3remote_keymap_remote_buttons[] = {
  262. [0x00] = KEY_1,
  263. [0x01] = KEY_2,
  264. [0x02] = KEY_3,
  265. [0x03] = KEY_4,
  266. [0x04] = KEY_5,
  267. [0x05] = KEY_6,
  268. [0x06] = KEY_7,
  269. [0x07] = KEY_8,
  270. [0x08] = KEY_9,
  271. [0x09] = KEY_0,
  272. [0x0e] = KEY_ESC, /* return */
  273. [0x0f] = KEY_CLEAR,
  274. [0x16] = KEY_EJECTCD,
  275. [0x1a] = KEY_MENU, /* top menu */
  276. [0x28] = KEY_TIME,
  277. [0x30] = KEY_PREVIOUS,
  278. [0x31] = KEY_NEXT,
  279. [0x32] = KEY_PLAY,
  280. [0x33] = KEY_REWIND, /* scan back */
  281. [0x34] = KEY_FORWARD, /* scan forward */
  282. [0x38] = KEY_STOP,
  283. [0x39] = KEY_PAUSE,
  284. [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
  285. [0x60] = KEY_FRAMEBACK, /* slow/step back */
  286. [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
  287. [0x63] = KEY_SUBTITLE,
  288. [0x64] = KEY_AUDIO,
  289. [0x65] = KEY_ANGLE,
  290. [0x70] = KEY_INFO, /* display */
  291. [0x80] = KEY_BLUE,
  292. [0x81] = KEY_RED,
  293. [0x82] = KEY_GREEN,
  294. [0x83] = KEY_YELLOW,
  295. };
  296. static const unsigned int buzz_keymap[] = {
  297. /*
  298. * The controller has 4 remote buzzers, each with one LED and 5
  299. * buttons.
  300. *
  301. * We use the mapping chosen by the controller, which is:
  302. *
  303. * Key Offset
  304. * -------------------
  305. * Buzz 1
  306. * Blue 5
  307. * Orange 4
  308. * Green 3
  309. * Yellow 2
  310. *
  311. * So, for example, the orange button on the third buzzer is mapped to
  312. * BTN_TRIGGER_HAPPY14
  313. */
  314. [1] = BTN_TRIGGER_HAPPY1,
  315. [2] = BTN_TRIGGER_HAPPY2,
  316. [3] = BTN_TRIGGER_HAPPY3,
  317. [4] = BTN_TRIGGER_HAPPY4,
  318. [5] = BTN_TRIGGER_HAPPY5,
  319. [6] = BTN_TRIGGER_HAPPY6,
  320. [7] = BTN_TRIGGER_HAPPY7,
  321. [8] = BTN_TRIGGER_HAPPY8,
  322. [9] = BTN_TRIGGER_HAPPY9,
  323. [10] = BTN_TRIGGER_HAPPY10,
  324. [11] = BTN_TRIGGER_HAPPY11,
  325. [12] = BTN_TRIGGER_HAPPY12,
  326. [13] = BTN_TRIGGER_HAPPY13,
  327. [14] = BTN_TRIGGER_HAPPY14,
  328. [15] = BTN_TRIGGER_HAPPY15,
  329. [16] = BTN_TRIGGER_HAPPY16,
  330. [17] = BTN_TRIGGER_HAPPY17,
  331. [18] = BTN_TRIGGER_HAPPY18,
  332. [19] = BTN_TRIGGER_HAPPY19,
  333. [20] = BTN_TRIGGER_HAPPY20,
  334. };
  335. /* The Navigation controller is a partial DS3 and uses the same HID report
  336. * and hence the same keymap indices, however not all axes/buttons
  337. * are physically present. We use the same axis and button mapping as
  338. * the DS3, which uses the Linux gamepad spec.
  339. */
  340. static const unsigned int navigation_absmap[] = {
  341. [0x30] = ABS_X,
  342. [0x31] = ABS_Y,
  343. [0x33] = ABS_Z, /* L2 */
  344. };
  345. /* Buttons not physically available on the device, but still available
  346. * in the reports are explicitly set to 0 for documentation purposes.
  347. */
  348. static const unsigned int navigation_keymap[] = {
  349. [0x01] = 0, /* Select */
  350. [0x02] = BTN_THUMBL, /* L3 */
  351. [0x03] = 0, /* R3 */
  352. [0x04] = 0, /* Start */
  353. [0x05] = BTN_DPAD_UP, /* Up */
  354. [0x06] = BTN_DPAD_RIGHT, /* Right */
  355. [0x07] = BTN_DPAD_DOWN, /* Down */
  356. [0x08] = BTN_DPAD_LEFT, /* Left */
  357. [0x09] = BTN_TL2, /* L2 */
  358. [0x0a] = 0, /* R2 */
  359. [0x0b] = BTN_TL, /* L1 */
  360. [0x0c] = 0, /* R1 */
  361. [0x0d] = BTN_NORTH, /* Triangle */
  362. [0x0e] = BTN_EAST, /* Circle */
  363. [0x0f] = BTN_SOUTH, /* Cross */
  364. [0x10] = BTN_WEST, /* Square */
  365. [0x11] = BTN_MODE, /* PS */
  366. };
  367. static const unsigned int sixaxis_absmap[] = {
  368. [0x30] = ABS_X,
  369. [0x31] = ABS_Y,
  370. [0x32] = ABS_RX, /* right stick X */
  371. [0x35] = ABS_RY, /* right stick Y */
  372. };
  373. static const unsigned int sixaxis_keymap[] = {
  374. [0x01] = BTN_SELECT, /* Select */
  375. [0x02] = BTN_THUMBL, /* L3 */
  376. [0x03] = BTN_THUMBR, /* R3 */
  377. [0x04] = BTN_START, /* Start */
  378. [0x05] = BTN_DPAD_UP, /* Up */
  379. [0x06] = BTN_DPAD_RIGHT, /* Right */
  380. [0x07] = BTN_DPAD_DOWN, /* Down */
  381. [0x08] = BTN_DPAD_LEFT, /* Left */
  382. [0x09] = BTN_TL2, /* L2 */
  383. [0x0a] = BTN_TR2, /* R2 */
  384. [0x0b] = BTN_TL, /* L1 */
  385. [0x0c] = BTN_TR, /* R1 */
  386. [0x0d] = BTN_NORTH, /* Triangle */
  387. [0x0e] = BTN_EAST, /* Circle */
  388. [0x0f] = BTN_SOUTH, /* Cross */
  389. [0x10] = BTN_WEST, /* Square */
  390. [0x11] = BTN_MODE, /* PS */
  391. };
  392. static enum power_supply_property sony_battery_props[] = {
  393. POWER_SUPPLY_PROP_PRESENT,
  394. POWER_SUPPLY_PROP_CAPACITY,
  395. POWER_SUPPLY_PROP_SCOPE,
  396. POWER_SUPPLY_PROP_STATUS,
  397. };
  398. struct sixaxis_led {
  399. u8 time_enabled; /* the total time the led is active (0xff means forever) */
  400. u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
  401. u8 enabled;
  402. u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
  403. u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
  404. } __packed;
  405. struct sixaxis_rumble {
  406. u8 padding;
  407. u8 right_duration; /* Right motor duration (0xff means forever) */
  408. u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
  409. u8 left_duration; /* Left motor duration (0xff means forever) */
  410. u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
  411. } __packed;
  412. struct sixaxis_output_report {
  413. u8 report_id;
  414. struct sixaxis_rumble rumble;
  415. u8 padding[4];
  416. u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
  417. struct sixaxis_led led[4]; /* LEDx at (4 - x) */
  418. struct sixaxis_led _reserved; /* LED5, not actually soldered */
  419. } __packed;
  420. union sixaxis_output_report_01 {
  421. struct sixaxis_output_report data;
  422. u8 buf[36];
  423. };
  424. struct motion_output_report_02 {
  425. u8 type, zero;
  426. u8 r, g, b;
  427. u8 zero2;
  428. u8 rumble;
  429. };
  430. #define SIXAXIS_REPORT_0xF2_SIZE 17
  431. #define SIXAXIS_REPORT_0xF5_SIZE 8
  432. #define MOTION_REPORT_0x02_SIZE 49
  433. #define SENSOR_SUFFIX " Motion Sensors"
  434. #define TOUCHPAD_SUFFIX " Touchpad"
  435. #define SIXAXIS_INPUT_REPORT_ACC_X_OFFSET 41
  436. #define SIXAXIS_ACC_RES_PER_G 113
  437. static DEFINE_SPINLOCK(sony_dev_list_lock);
  438. static LIST_HEAD(sony_device_list);
  439. static DEFINE_IDA(sony_device_id_allocator);
  440. enum sony_worker {
  441. SONY_WORKER_STATE
  442. };
  443. struct sony_sc {
  444. spinlock_t lock;
  445. struct list_head list_node;
  446. struct hid_device *hdev;
  447. struct input_dev *touchpad;
  448. struct input_dev *sensor_dev;
  449. struct led_classdev *leds[MAX_LEDS];
  450. unsigned long quirks;
  451. struct work_struct state_worker;
  452. void (*send_output_report)(struct sony_sc *);
  453. struct power_supply *battery;
  454. struct power_supply_desc battery_desc;
  455. int device_id;
  456. u8 *output_report_dmabuf;
  457. #ifdef CONFIG_SONY_FF
  458. u8 left;
  459. u8 right;
  460. #endif
  461. u8 mac_address[6];
  462. u8 state_worker_initialized;
  463. u8 defer_initialization;
  464. u8 battery_capacity;
  465. int battery_status;
  466. u8 led_state[MAX_LEDS];
  467. u8 led_delay_on[MAX_LEDS];
  468. u8 led_delay_off[MAX_LEDS];
  469. u8 led_count;
  470. /* GH Live */
  471. struct urb *ghl_urb;
  472. struct timer_list ghl_poke_timer;
  473. };
  474. static void sony_set_leds(struct sony_sc *sc);
  475. static inline void sony_schedule_work(struct sony_sc *sc,
  476. enum sony_worker which)
  477. {
  478. unsigned long flags;
  479. switch (which) {
  480. case SONY_WORKER_STATE:
  481. spin_lock_irqsave(&sc->lock, flags);
  482. if (!sc->defer_initialization && sc->state_worker_initialized)
  483. schedule_work(&sc->state_worker);
  484. spin_unlock_irqrestore(&sc->lock, flags);
  485. break;
  486. }
  487. }
  488. static void ghl_magic_poke_cb(struct urb *urb)
  489. {
  490. struct sony_sc *sc = urb->context;
  491. if (urb->status < 0)
  492. hid_err(sc->hdev, "URB transfer failed : %d", urb->status);
  493. mod_timer(&sc->ghl_poke_timer, jiffies + GHL_GUITAR_POKE_INTERVAL*HZ);
  494. }
  495. static void ghl_magic_poke(struct timer_list *t)
  496. {
  497. int ret;
  498. struct sony_sc *sc = from_timer(sc, t, ghl_poke_timer);
  499. ret = usb_submit_urb(sc->ghl_urb, GFP_ATOMIC);
  500. if (ret < 0)
  501. hid_err(sc->hdev, "usb_submit_urb failed: %d", ret);
  502. }
  503. static int ghl_init_urb(struct sony_sc *sc, struct usb_device *usbdev,
  504. const char ghl_magic_data[], u16 poke_size)
  505. {
  506. struct usb_ctrlrequest *cr;
  507. u8 *databuf;
  508. unsigned int pipe;
  509. u16 ghl_magic_value = (((HID_OUTPUT_REPORT + 1) << 8) | ghl_magic_data[0]);
  510. pipe = usb_sndctrlpipe(usbdev, 0);
  511. cr = devm_kzalloc(&sc->hdev->dev, sizeof(*cr), GFP_ATOMIC);
  512. if (cr == NULL)
  513. return -ENOMEM;
  514. databuf = devm_kzalloc(&sc->hdev->dev, poke_size, GFP_ATOMIC);
  515. if (databuf == NULL)
  516. return -ENOMEM;
  517. cr->bRequestType =
  518. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT;
  519. cr->bRequest = USB_REQ_SET_CONFIGURATION;
  520. cr->wValue = cpu_to_le16(ghl_magic_value);
  521. cr->wIndex = 0;
  522. cr->wLength = cpu_to_le16(poke_size);
  523. memcpy(databuf, ghl_magic_data, poke_size);
  524. usb_fill_control_urb(
  525. sc->ghl_urb, usbdev, pipe,
  526. (unsigned char *) cr, databuf, poke_size,
  527. ghl_magic_poke_cb, sc);
  528. return 0;
  529. }
  530. static int guitar_mapping(struct hid_device *hdev, struct hid_input *hi,
  531. struct hid_field *field, struct hid_usage *usage,
  532. unsigned long **bit, int *max)
  533. {
  534. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
  535. unsigned int abs = usage->hid & HID_USAGE;
  536. if (abs == GUITAR_TILT_USAGE) {
  537. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, ABS_RY);
  538. return 1;
  539. }
  540. }
  541. return 0;
  542. }
  543. static const u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
  544. unsigned int *rsize)
  545. {
  546. *rsize = sizeof(motion_rdesc);
  547. return motion_rdesc;
  548. }
  549. static const u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
  550. unsigned int *rsize)
  551. {
  552. *rsize = sizeof(ps3remote_rdesc);
  553. return ps3remote_rdesc;
  554. }
  555. static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
  556. struct hid_field *field, struct hid_usage *usage,
  557. unsigned long **bit, int *max)
  558. {
  559. unsigned int key = usage->hid & HID_USAGE;
  560. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  561. return -1;
  562. switch (usage->collection_index) {
  563. case 1:
  564. if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
  565. return -1;
  566. key = ps3remote_keymap_joypad_buttons[key];
  567. if (!key)
  568. return -1;
  569. break;
  570. case 2:
  571. if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
  572. return -1;
  573. key = ps3remote_keymap_remote_buttons[key];
  574. if (!key)
  575. return -1;
  576. break;
  577. default:
  578. return -1;
  579. }
  580. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  581. return 1;
  582. }
  583. static int navigation_mapping(struct hid_device *hdev, struct hid_input *hi,
  584. struct hid_field *field, struct hid_usage *usage,
  585. unsigned long **bit, int *max)
  586. {
  587. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
  588. unsigned int key = usage->hid & HID_USAGE;
  589. if (key >= ARRAY_SIZE(sixaxis_keymap))
  590. return -1;
  591. key = navigation_keymap[key];
  592. if (!key)
  593. return -1;
  594. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  595. return 1;
  596. } else if (usage->hid == HID_GD_POINTER) {
  597. /* See comment in sixaxis_mapping, basically the L2 (and R2)
  598. * triggers are reported through GD Pointer.
  599. * In addition we ignore any analog button 'axes' and only
  600. * support digital buttons.
  601. */
  602. switch (usage->usage_index) {
  603. case 8: /* L2 */
  604. usage->hid = HID_GD_Z;
  605. break;
  606. default:
  607. return -1;
  608. }
  609. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf);
  610. return 1;
  611. } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
  612. unsigned int abs = usage->hid & HID_USAGE;
  613. if (abs >= ARRAY_SIZE(navigation_absmap))
  614. return -1;
  615. abs = navigation_absmap[abs];
  616. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
  617. return 1;
  618. }
  619. return -1;
  620. }
  621. static int sixaxis_mapping(struct hid_device *hdev, struct hid_input *hi,
  622. struct hid_field *field, struct hid_usage *usage,
  623. unsigned long **bit, int *max)
  624. {
  625. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
  626. unsigned int key = usage->hid & HID_USAGE;
  627. if (key >= ARRAY_SIZE(sixaxis_keymap))
  628. return -1;
  629. key = sixaxis_keymap[key];
  630. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  631. return 1;
  632. } else if (usage->hid == HID_GD_POINTER) {
  633. /* The DS3 provides analog values for most buttons and even
  634. * for HAT axes through GD Pointer. L2 and R2 are reported
  635. * among these as well instead of as GD Z / RZ. Remap L2
  636. * and R2 and ignore other analog 'button axes' as there is
  637. * no good way for reporting them.
  638. */
  639. switch (usage->usage_index) {
  640. case 8: /* L2 */
  641. usage->hid = HID_GD_Z;
  642. break;
  643. case 9: /* R2 */
  644. usage->hid = HID_GD_RZ;
  645. break;
  646. default:
  647. return -1;
  648. }
  649. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf);
  650. return 1;
  651. } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
  652. unsigned int abs = usage->hid & HID_USAGE;
  653. if (abs >= ARRAY_SIZE(sixaxis_absmap))
  654. return -1;
  655. abs = sixaxis_absmap[abs];
  656. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
  657. return 1;
  658. }
  659. return -1;
  660. }
  661. static const u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
  662. unsigned int *rsize)
  663. {
  664. struct sony_sc *sc = hid_get_drvdata(hdev);
  665. if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT))
  666. return rdesc;
  667. /*
  668. * Some Sony RF receivers wrongly declare the mouse pointer as a
  669. * a constant non-data variable.
  670. */
  671. if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
  672. /* usage page: generic desktop controls */
  673. /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
  674. /* usage: mouse */
  675. rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
  676. /* input (usage page for x,y axes): constant, variable, relative */
  677. rdesc[54] == 0x81 && rdesc[55] == 0x07) {
  678. hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
  679. /* input: data, variable, relative */
  680. rdesc[55] = 0x06;
  681. }
  682. if (sc->quirks & MOTION_CONTROLLER)
  683. return motion_fixup(hdev, rdesc, rsize);
  684. if (sc->quirks & PS3REMOTE)
  685. return ps3remote_fixup(hdev, rdesc, rsize);
  686. /*
  687. * Some knock-off USB dongles incorrectly report their button count
  688. * as 13 instead of 16 causing three non-functional buttons.
  689. */
  690. if ((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize >= 45 &&
  691. /* Report Count (13) */
  692. rdesc[23] == 0x95 && rdesc[24] == 0x0D &&
  693. /* Usage Maximum (13) */
  694. rdesc[37] == 0x29 && rdesc[38] == 0x0D &&
  695. /* Report Count (3) */
  696. rdesc[43] == 0x95 && rdesc[44] == 0x03) {
  697. hid_info(hdev, "Fixing up USB dongle report descriptor\n");
  698. rdesc[24] = 0x10;
  699. rdesc[38] = 0x10;
  700. rdesc[44] = 0x00;
  701. }
  702. return rdesc;
  703. }
  704. static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
  705. {
  706. static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
  707. unsigned long flags;
  708. int offset;
  709. u8 battery_capacity;
  710. int battery_status;
  711. /*
  712. * The sixaxis is charging if the battery value is 0xee
  713. * and it is fully charged if the value is 0xef.
  714. * It does not report the actual level while charging so it
  715. * is set to 100% while charging is in progress.
  716. */
  717. offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
  718. if (rd[offset] >= 0xee) {
  719. battery_capacity = 100;
  720. battery_status = (rd[offset] & 0x01) ? POWER_SUPPLY_STATUS_FULL : POWER_SUPPLY_STATUS_CHARGING;
  721. } else {
  722. u8 index = rd[offset] <= 5 ? rd[offset] : 5;
  723. battery_capacity = sixaxis_battery_capacity[index];
  724. battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
  725. }
  726. spin_lock_irqsave(&sc->lock, flags);
  727. sc->battery_capacity = battery_capacity;
  728. sc->battery_status = battery_status;
  729. spin_unlock_irqrestore(&sc->lock, flags);
  730. if (sc->quirks & SIXAXIS_CONTROLLER) {
  731. int val;
  732. offset = SIXAXIS_INPUT_REPORT_ACC_X_OFFSET;
  733. val = ((rd[offset+1] << 8) | rd[offset]) - 511;
  734. input_report_abs(sc->sensor_dev, ABS_X, val);
  735. /* Y and Z are swapped and inversed */
  736. val = 511 - ((rd[offset+5] << 8) | rd[offset+4]);
  737. input_report_abs(sc->sensor_dev, ABS_Y, val);
  738. val = 511 - ((rd[offset+3] << 8) | rd[offset+2]);
  739. input_report_abs(sc->sensor_dev, ABS_Z, val);
  740. input_sync(sc->sensor_dev);
  741. }
  742. }
  743. static void nsg_mrxu_parse_report(struct sony_sc *sc, u8 *rd, int size)
  744. {
  745. int n, offset, relx, rely;
  746. u8 active;
  747. /*
  748. * The NSG-MRxU multi-touch trackpad data starts at offset 1 and
  749. * the touch-related data starts at offset 2.
  750. * For the first byte, bit 0 is set when touchpad button is pressed.
  751. * Bit 2 is set when a touch is active and the drag (Fn) key is pressed.
  752. * This drag key is mapped to BTN_LEFT. It is operational only when a
  753. * touch point is active.
  754. * Bit 4 is set when only the first touch point is active.
  755. * Bit 6 is set when only the second touch point is active.
  756. * Bits 5 and 7 are set when both touch points are active.
  757. * The next 3 bytes are two 12 bit X/Y coordinates for the first touch.
  758. * The following byte, offset 5, has the touch width and length.
  759. * Bits 0-4=X (width), bits 5-7=Y (length).
  760. * A signed relative X coordinate is at offset 6.
  761. * The bytes at offset 7-9 are the second touch X/Y coordinates.
  762. * Offset 10 has the second touch width and length.
  763. * Offset 11 has the relative Y coordinate.
  764. */
  765. offset = 1;
  766. input_report_key(sc->touchpad, BTN_LEFT, rd[offset] & 0x0F);
  767. active = (rd[offset] >> 4);
  768. relx = (s8) rd[offset+5];
  769. rely = ((s8) rd[offset+10]) * -1;
  770. offset++;
  771. for (n = 0; n < 2; n++) {
  772. u16 x, y;
  773. u8 contactx, contacty;
  774. x = rd[offset] | ((rd[offset+1] & 0x0F) << 8);
  775. y = ((rd[offset+1] & 0xF0) >> 4) | (rd[offset+2] << 4);
  776. input_mt_slot(sc->touchpad, n);
  777. input_mt_report_slot_state(sc->touchpad, MT_TOOL_FINGER, active & 0x03);
  778. if (active & 0x03) {
  779. contactx = rd[offset+3] & 0x0F;
  780. contacty = rd[offset+3] >> 4;
  781. input_report_abs(sc->touchpad, ABS_MT_TOUCH_MAJOR,
  782. max(contactx, contacty));
  783. input_report_abs(sc->touchpad, ABS_MT_TOUCH_MINOR,
  784. min(contactx, contacty));
  785. input_report_abs(sc->touchpad, ABS_MT_ORIENTATION,
  786. (bool) (contactx > contacty));
  787. input_report_abs(sc->touchpad, ABS_MT_POSITION_X, x);
  788. input_report_abs(sc->touchpad, ABS_MT_POSITION_Y,
  789. NSG_MRXU_MAX_Y - y);
  790. /*
  791. * The relative coordinates belong to the first touch
  792. * point, when present, or to the second touch point
  793. * when the first is not active.
  794. */
  795. if ((n == 0) || ((n == 1) && (active & 0x01))) {
  796. input_report_rel(sc->touchpad, REL_X, relx);
  797. input_report_rel(sc->touchpad, REL_Y, rely);
  798. }
  799. }
  800. offset += 5;
  801. active >>= 2;
  802. }
  803. input_mt_sync_frame(sc->touchpad);
  804. input_sync(sc->touchpad);
  805. }
  806. static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
  807. u8 *rd, int size)
  808. {
  809. struct sony_sc *sc = hid_get_drvdata(hdev);
  810. /*
  811. * Sixaxis HID report has acclerometers/gyro with MSByte first, this
  812. * has to be BYTE_SWAPPED before passing up to joystick interface
  813. */
  814. if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
  815. /*
  816. * When connected via Bluetooth the Sixaxis occasionally sends
  817. * a report with the second byte 0xff and the rest zeroed.
  818. *
  819. * This report does not reflect the actual state of the
  820. * controller must be ignored to avoid generating false input
  821. * events.
  822. */
  823. if (rd[1] == 0xff)
  824. return -EINVAL;
  825. swap(rd[41], rd[42]);
  826. swap(rd[43], rd[44]);
  827. swap(rd[45], rd[46]);
  828. swap(rd[47], rd[48]);
  829. sixaxis_parse_report(sc, rd, size);
  830. } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
  831. sixaxis_parse_report(sc, rd, size);
  832. } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
  833. size == 49) {
  834. sixaxis_parse_report(sc, rd, size);
  835. } else if ((sc->quirks & NSG_MRXU_REMOTE) && rd[0] == 0x02) {
  836. nsg_mrxu_parse_report(sc, rd, size);
  837. return 1;
  838. }
  839. if (sc->defer_initialization) {
  840. sc->defer_initialization = 0;
  841. sony_schedule_work(sc, SONY_WORKER_STATE);
  842. }
  843. return 0;
  844. }
  845. static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
  846. struct hid_field *field, struct hid_usage *usage,
  847. unsigned long **bit, int *max)
  848. {
  849. struct sony_sc *sc = hid_get_drvdata(hdev);
  850. if (sc->quirks & BUZZ_CONTROLLER) {
  851. unsigned int key = usage->hid & HID_USAGE;
  852. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  853. return -1;
  854. switch (usage->collection_index) {
  855. case 1:
  856. if (key >= ARRAY_SIZE(buzz_keymap))
  857. return -1;
  858. key = buzz_keymap[key];
  859. if (!key)
  860. return -1;
  861. break;
  862. default:
  863. return -1;
  864. }
  865. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  866. return 1;
  867. }
  868. if (sc->quirks & PS3REMOTE)
  869. return ps3remote_mapping(hdev, hi, field, usage, bit, max);
  870. if (sc->quirks & NAVIGATION_CONTROLLER)
  871. return navigation_mapping(hdev, hi, field, usage, bit, max);
  872. if (sc->quirks & SIXAXIS_CONTROLLER)
  873. return sixaxis_mapping(hdev, hi, field, usage, bit, max);
  874. if (sc->quirks & GH_GUITAR_CONTROLLER)
  875. return guitar_mapping(hdev, hi, field, usage, bit, max);
  876. /* Let hid-core decide for the others */
  877. return 0;
  878. }
  879. static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
  880. int w, int h, int touch_major, int touch_minor, int orientation)
  881. {
  882. size_t name_sz;
  883. char *name;
  884. int ret;
  885. sc->touchpad = devm_input_allocate_device(&sc->hdev->dev);
  886. if (!sc->touchpad)
  887. return -ENOMEM;
  888. input_set_drvdata(sc->touchpad, sc);
  889. sc->touchpad->dev.parent = &sc->hdev->dev;
  890. sc->touchpad->phys = sc->hdev->phys;
  891. sc->touchpad->uniq = sc->hdev->uniq;
  892. sc->touchpad->id.bustype = sc->hdev->bus;
  893. sc->touchpad->id.vendor = sc->hdev->vendor;
  894. sc->touchpad->id.product = sc->hdev->product;
  895. sc->touchpad->id.version = sc->hdev->version;
  896. /* This suffix was originally apended when hid-sony also
  897. * supported DS4 devices. The DS4 was implemented using multiple
  898. * evdev nodes and hence had the need to separete them out using
  899. * a suffix. Other devices which were added later like Sony TV remotes
  900. * inhirited this suffix.
  901. */
  902. name_sz = strlen(sc->hdev->name) + sizeof(TOUCHPAD_SUFFIX);
  903. name = devm_kzalloc(&sc->hdev->dev, name_sz, GFP_KERNEL);
  904. if (!name)
  905. return -ENOMEM;
  906. snprintf(name, name_sz, "%s" TOUCHPAD_SUFFIX, sc->hdev->name);
  907. sc->touchpad->name = name;
  908. /* We map the button underneath the touchpad to BTN_LEFT. */
  909. __set_bit(EV_KEY, sc->touchpad->evbit);
  910. __set_bit(BTN_LEFT, sc->touchpad->keybit);
  911. __set_bit(INPUT_PROP_BUTTONPAD, sc->touchpad->propbit);
  912. input_set_abs_params(sc->touchpad, ABS_MT_POSITION_X, 0, w, 0, 0);
  913. input_set_abs_params(sc->touchpad, ABS_MT_POSITION_Y, 0, h, 0, 0);
  914. if (touch_major > 0) {
  915. input_set_abs_params(sc->touchpad, ABS_MT_TOUCH_MAJOR,
  916. 0, touch_major, 0, 0);
  917. if (touch_minor > 0)
  918. input_set_abs_params(sc->touchpad, ABS_MT_TOUCH_MINOR,
  919. 0, touch_minor, 0, 0);
  920. if (orientation > 0)
  921. input_set_abs_params(sc->touchpad, ABS_MT_ORIENTATION,
  922. 0, orientation, 0, 0);
  923. }
  924. if (sc->quirks & NSG_MRXU_REMOTE) {
  925. __set_bit(EV_REL, sc->touchpad->evbit);
  926. }
  927. ret = input_mt_init_slots(sc->touchpad, touch_count, INPUT_MT_POINTER);
  928. if (ret < 0)
  929. return ret;
  930. ret = input_register_device(sc->touchpad);
  931. if (ret < 0)
  932. return ret;
  933. return 0;
  934. }
  935. static int sony_register_sensors(struct sony_sc *sc)
  936. {
  937. size_t name_sz;
  938. char *name;
  939. int ret;
  940. sc->sensor_dev = devm_input_allocate_device(&sc->hdev->dev);
  941. if (!sc->sensor_dev)
  942. return -ENOMEM;
  943. input_set_drvdata(sc->sensor_dev, sc);
  944. sc->sensor_dev->dev.parent = &sc->hdev->dev;
  945. sc->sensor_dev->phys = sc->hdev->phys;
  946. sc->sensor_dev->uniq = sc->hdev->uniq;
  947. sc->sensor_dev->id.bustype = sc->hdev->bus;
  948. sc->sensor_dev->id.vendor = sc->hdev->vendor;
  949. sc->sensor_dev->id.product = sc->hdev->product;
  950. sc->sensor_dev->id.version = sc->hdev->version;
  951. /* Append a suffix to the controller name as there are various
  952. * DS4 compatible non-Sony devices with different names.
  953. */
  954. name_sz = strlen(sc->hdev->name) + sizeof(SENSOR_SUFFIX);
  955. name = devm_kzalloc(&sc->hdev->dev, name_sz, GFP_KERNEL);
  956. if (!name)
  957. return -ENOMEM;
  958. snprintf(name, name_sz, "%s" SENSOR_SUFFIX, sc->hdev->name);
  959. sc->sensor_dev->name = name;
  960. if (sc->quirks & SIXAXIS_CONTROLLER) {
  961. /* For the DS3 we only support the accelerometer, which works
  962. * quite well even without calibration. The device also has
  963. * a 1-axis gyro, but it is very difficult to manage from within
  964. * the driver even to get data, the sensor is inaccurate and
  965. * the behavior is very different between hardware revisions.
  966. */
  967. input_set_abs_params(sc->sensor_dev, ABS_X, -512, 511, 4, 0);
  968. input_set_abs_params(sc->sensor_dev, ABS_Y, -512, 511, 4, 0);
  969. input_set_abs_params(sc->sensor_dev, ABS_Z, -512, 511, 4, 0);
  970. input_abs_set_res(sc->sensor_dev, ABS_X, SIXAXIS_ACC_RES_PER_G);
  971. input_abs_set_res(sc->sensor_dev, ABS_Y, SIXAXIS_ACC_RES_PER_G);
  972. input_abs_set_res(sc->sensor_dev, ABS_Z, SIXAXIS_ACC_RES_PER_G);
  973. }
  974. __set_bit(INPUT_PROP_ACCELEROMETER, sc->sensor_dev->propbit);
  975. ret = input_register_device(sc->sensor_dev);
  976. if (ret < 0)
  977. return ret;
  978. return 0;
  979. }
  980. /*
  981. * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
  982. * to "operational". Without this, the ps3 controller will not report any
  983. * events.
  984. */
  985. static int sixaxis_set_operational_usb(struct hid_device *hdev)
  986. {
  987. struct sony_sc *sc = hid_get_drvdata(hdev);
  988. const int buf_size =
  989. max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
  990. u8 *buf;
  991. int ret;
  992. buf = kmalloc(buf_size, GFP_KERNEL);
  993. if (!buf)
  994. return -ENOMEM;
  995. ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
  996. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  997. if (ret < 0) {
  998. hid_err(hdev, "can't set operational mode: step 1\n");
  999. goto out;
  1000. }
  1001. /*
  1002. * Some compatible controllers like the Speedlink Strike FX and
  1003. * Gasia need another query plus an USB interrupt to get operational.
  1004. */
  1005. ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
  1006. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  1007. if (ret < 0) {
  1008. hid_err(hdev, "can't set operational mode: step 2\n");
  1009. goto out;
  1010. }
  1011. /*
  1012. * But the USB interrupt would cause SHANWAN controllers to
  1013. * start rumbling non-stop, so skip step 3 for these controllers.
  1014. */
  1015. if (sc->quirks & SHANWAN_GAMEPAD)
  1016. goto out;
  1017. ret = hid_hw_output_report(hdev, buf, 1);
  1018. if (ret < 0) {
  1019. hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
  1020. ret = 0;
  1021. }
  1022. out:
  1023. kfree(buf);
  1024. return ret;
  1025. }
  1026. static int sixaxis_set_operational_bt(struct hid_device *hdev)
  1027. {
  1028. static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
  1029. u8 *buf;
  1030. int ret;
  1031. buf = kmemdup(report, sizeof(report), GFP_KERNEL);
  1032. if (!buf)
  1033. return -ENOMEM;
  1034. ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
  1035. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  1036. kfree(buf);
  1037. return ret;
  1038. }
  1039. static void sixaxis_set_leds_from_id(struct sony_sc *sc)
  1040. {
  1041. static const u8 sixaxis_leds[10][4] = {
  1042. { 0x01, 0x00, 0x00, 0x00 },
  1043. { 0x00, 0x01, 0x00, 0x00 },
  1044. { 0x00, 0x00, 0x01, 0x00 },
  1045. { 0x00, 0x00, 0x00, 0x01 },
  1046. { 0x01, 0x00, 0x00, 0x01 },
  1047. { 0x00, 0x01, 0x00, 0x01 },
  1048. { 0x00, 0x00, 0x01, 0x01 },
  1049. { 0x01, 0x00, 0x01, 0x01 },
  1050. { 0x00, 0x01, 0x01, 0x01 },
  1051. { 0x01, 0x01, 0x01, 0x01 }
  1052. };
  1053. int id = sc->device_id;
  1054. BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
  1055. if (id < 0)
  1056. return;
  1057. id %= 10;
  1058. memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
  1059. }
  1060. static void buzz_set_leds(struct sony_sc *sc)
  1061. {
  1062. struct hid_device *hdev = sc->hdev;
  1063. struct list_head *report_list =
  1064. &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
  1065. struct hid_report *report = list_entry(report_list->next,
  1066. struct hid_report, list);
  1067. s32 *value = report->field[0]->value;
  1068. BUILD_BUG_ON(MAX_LEDS < 4);
  1069. value[0] = 0x00;
  1070. value[1] = sc->led_state[0] ? 0xff : 0x00;
  1071. value[2] = sc->led_state[1] ? 0xff : 0x00;
  1072. value[3] = sc->led_state[2] ? 0xff : 0x00;
  1073. value[4] = sc->led_state[3] ? 0xff : 0x00;
  1074. value[5] = 0x00;
  1075. value[6] = 0x00;
  1076. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  1077. }
  1078. static void sony_set_leds(struct sony_sc *sc)
  1079. {
  1080. if (!(sc->quirks & BUZZ_CONTROLLER))
  1081. sony_schedule_work(sc, SONY_WORKER_STATE);
  1082. else
  1083. buzz_set_leds(sc);
  1084. }
  1085. static void sony_led_set_brightness(struct led_classdev *led,
  1086. enum led_brightness value)
  1087. {
  1088. struct device *dev = led->dev->parent;
  1089. struct hid_device *hdev = to_hid_device(dev);
  1090. struct sony_sc *drv_data;
  1091. int n;
  1092. int force_update;
  1093. drv_data = hid_get_drvdata(hdev);
  1094. if (!drv_data) {
  1095. hid_err(hdev, "No device data\n");
  1096. return;
  1097. }
  1098. /*
  1099. * The Sixaxis on USB will override any LED settings sent to it
  1100. * and keep flashing all of the LEDs until the PS button is pressed.
  1101. * Updates, even if redundant, must be always be sent to the
  1102. * controller to avoid having to toggle the state of an LED just to
  1103. * stop the flashing later on.
  1104. */
  1105. force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
  1106. for (n = 0; n < drv_data->led_count; n++) {
  1107. if (led == drv_data->leds[n] && (force_update ||
  1108. (value != drv_data->led_state[n] ||
  1109. drv_data->led_delay_on[n] ||
  1110. drv_data->led_delay_off[n]))) {
  1111. drv_data->led_state[n] = value;
  1112. /* Setting the brightness stops the blinking */
  1113. drv_data->led_delay_on[n] = 0;
  1114. drv_data->led_delay_off[n] = 0;
  1115. sony_set_leds(drv_data);
  1116. break;
  1117. }
  1118. }
  1119. }
  1120. static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
  1121. {
  1122. struct device *dev = led->dev->parent;
  1123. struct hid_device *hdev = to_hid_device(dev);
  1124. struct sony_sc *drv_data;
  1125. int n;
  1126. drv_data = hid_get_drvdata(hdev);
  1127. if (!drv_data) {
  1128. hid_err(hdev, "No device data\n");
  1129. return LED_OFF;
  1130. }
  1131. for (n = 0; n < drv_data->led_count; n++) {
  1132. if (led == drv_data->leds[n])
  1133. return drv_data->led_state[n];
  1134. }
  1135. return LED_OFF;
  1136. }
  1137. static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
  1138. unsigned long *delay_off)
  1139. {
  1140. struct device *dev = led->dev->parent;
  1141. struct hid_device *hdev = to_hid_device(dev);
  1142. struct sony_sc *drv_data = hid_get_drvdata(hdev);
  1143. int n;
  1144. u8 new_on, new_off;
  1145. if (!drv_data) {
  1146. hid_err(hdev, "No device data\n");
  1147. return -EINVAL;
  1148. }
  1149. /* Max delay is 255 deciseconds or 2550 milliseconds */
  1150. if (*delay_on > 2550)
  1151. *delay_on = 2550;
  1152. if (*delay_off > 2550)
  1153. *delay_off = 2550;
  1154. /* Blink at 1 Hz if both values are zero */
  1155. if (!*delay_on && !*delay_off)
  1156. *delay_on = *delay_off = 500;
  1157. new_on = *delay_on / 10;
  1158. new_off = *delay_off / 10;
  1159. for (n = 0; n < drv_data->led_count; n++) {
  1160. if (led == drv_data->leds[n])
  1161. break;
  1162. }
  1163. /* This LED is not registered on this device */
  1164. if (n >= drv_data->led_count)
  1165. return -EINVAL;
  1166. /* Don't schedule work if the values didn't change */
  1167. if (new_on != drv_data->led_delay_on[n] ||
  1168. new_off != drv_data->led_delay_off[n]) {
  1169. drv_data->led_delay_on[n] = new_on;
  1170. drv_data->led_delay_off[n] = new_off;
  1171. sony_schedule_work(drv_data, SONY_WORKER_STATE);
  1172. }
  1173. return 0;
  1174. }
  1175. static int sony_leds_init(struct sony_sc *sc)
  1176. {
  1177. struct hid_device *hdev = sc->hdev;
  1178. int n, ret = 0;
  1179. int use_color_names;
  1180. struct led_classdev *led;
  1181. size_t name_sz;
  1182. char *name;
  1183. size_t name_len;
  1184. const char *name_fmt;
  1185. static const char * const color_name_str[] = { "red", "green", "blue",
  1186. "global" };
  1187. u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
  1188. u8 use_hw_blink[MAX_LEDS] = { 0 };
  1189. BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
  1190. if (sc->quirks & BUZZ_CONTROLLER) {
  1191. sc->led_count = 4;
  1192. use_color_names = 0;
  1193. name_len = strlen("::buzz#");
  1194. name_fmt = "%s::buzz%d";
  1195. /* Validate expected report characteristics. */
  1196. if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
  1197. return -ENODEV;
  1198. } else if (sc->quirks & MOTION_CONTROLLER) {
  1199. sc->led_count = 3;
  1200. memset(max_brightness, 255, 3);
  1201. use_color_names = 1;
  1202. name_len = 0;
  1203. name_fmt = "%s:%s";
  1204. } else if (sc->quirks & NAVIGATION_CONTROLLER) {
  1205. static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
  1206. memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
  1207. sc->led_count = 1;
  1208. memset(use_hw_blink, 1, 4);
  1209. use_color_names = 0;
  1210. name_len = strlen("::sony#");
  1211. name_fmt = "%s::sony%d";
  1212. } else {
  1213. sixaxis_set_leds_from_id(sc);
  1214. sc->led_count = 4;
  1215. memset(use_hw_blink, 1, 4);
  1216. use_color_names = 0;
  1217. name_len = strlen("::sony#");
  1218. name_fmt = "%s::sony%d";
  1219. }
  1220. /*
  1221. * Clear LEDs as we have no way of reading their initial state. This is
  1222. * only relevant if the driver is loaded after somebody actively set the
  1223. * LEDs to on
  1224. */
  1225. sony_set_leds(sc);
  1226. name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
  1227. for (n = 0; n < sc->led_count; n++) {
  1228. if (use_color_names)
  1229. name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_name_str[n]) + 2;
  1230. led = devm_kzalloc(&hdev->dev, sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
  1231. if (!led) {
  1232. hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
  1233. return -ENOMEM;
  1234. }
  1235. name = (void *)(&led[1]);
  1236. if (use_color_names)
  1237. snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
  1238. color_name_str[n]);
  1239. else
  1240. snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
  1241. led->name = name;
  1242. led->brightness = sc->led_state[n];
  1243. led->max_brightness = max_brightness[n];
  1244. led->flags = LED_CORE_SUSPENDRESUME;
  1245. led->brightness_get = sony_led_get_brightness;
  1246. led->brightness_set = sony_led_set_brightness;
  1247. if (use_hw_blink[n])
  1248. led->blink_set = sony_led_blink_set;
  1249. sc->leds[n] = led;
  1250. ret = devm_led_classdev_register(&hdev->dev, led);
  1251. if (ret) {
  1252. hid_err(hdev, "Failed to register LED %d\n", n);
  1253. return ret;
  1254. }
  1255. }
  1256. return 0;
  1257. }
  1258. static void sixaxis_send_output_report(struct sony_sc *sc)
  1259. {
  1260. static const union sixaxis_output_report_01 default_report = {
  1261. .buf = {
  1262. 0x01,
  1263. 0x01, 0xff, 0x00, 0xff, 0x00,
  1264. 0x00, 0x00, 0x00, 0x00, 0x00,
  1265. 0xff, 0x27, 0x10, 0x00, 0x32,
  1266. 0xff, 0x27, 0x10, 0x00, 0x32,
  1267. 0xff, 0x27, 0x10, 0x00, 0x32,
  1268. 0xff, 0x27, 0x10, 0x00, 0x32,
  1269. 0x00, 0x00, 0x00, 0x00, 0x00
  1270. }
  1271. };
  1272. struct sixaxis_output_report *report =
  1273. (struct sixaxis_output_report *)sc->output_report_dmabuf;
  1274. int n;
  1275. /* Initialize the report with default values */
  1276. memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
  1277. #ifdef CONFIG_SONY_FF
  1278. report->rumble.right_motor_on = sc->right ? 1 : 0;
  1279. report->rumble.left_motor_force = sc->left;
  1280. #endif
  1281. report->leds_bitmap |= sc->led_state[0] << 1;
  1282. report->leds_bitmap |= sc->led_state[1] << 2;
  1283. report->leds_bitmap |= sc->led_state[2] << 3;
  1284. report->leds_bitmap |= sc->led_state[3] << 4;
  1285. /* Set flag for all leds off, required for 3rd party INTEC controller */
  1286. if ((report->leds_bitmap & 0x1E) == 0)
  1287. report->leds_bitmap |= 0x20;
  1288. /*
  1289. * The LEDs in the report are indexed in reverse order to their
  1290. * corresponding light on the controller.
  1291. * Index 0 = LED 4, index 1 = LED 3, etc...
  1292. *
  1293. * In the case of both delay values being zero (blinking disabled) the
  1294. * default report values should be used or the controller LED will be
  1295. * always off.
  1296. */
  1297. for (n = 0; n < 4; n++) {
  1298. if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
  1299. report->led[3 - n].duty_off = sc->led_delay_off[n];
  1300. report->led[3 - n].duty_on = sc->led_delay_on[n];
  1301. }
  1302. }
  1303. /* SHANWAN controllers require output reports via intr channel */
  1304. if (sc->quirks & SHANWAN_GAMEPAD)
  1305. hid_hw_output_report(sc->hdev, (u8 *)report,
  1306. sizeof(struct sixaxis_output_report));
  1307. else
  1308. hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
  1309. sizeof(struct sixaxis_output_report),
  1310. HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
  1311. }
  1312. static void motion_send_output_report(struct sony_sc *sc)
  1313. {
  1314. struct hid_device *hdev = sc->hdev;
  1315. struct motion_output_report_02 *report =
  1316. (struct motion_output_report_02 *)sc->output_report_dmabuf;
  1317. memset(report, 0, MOTION_REPORT_0x02_SIZE);
  1318. report->type = 0x02; /* set leds */
  1319. report->r = sc->led_state[0];
  1320. report->g = sc->led_state[1];
  1321. report->b = sc->led_state[2];
  1322. #ifdef CONFIG_SONY_FF
  1323. report->rumble = max(sc->right, sc->left);
  1324. #endif
  1325. hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
  1326. }
  1327. #ifdef CONFIG_SONY_FF
  1328. static inline void sony_send_output_report(struct sony_sc *sc)
  1329. {
  1330. if (sc->send_output_report)
  1331. sc->send_output_report(sc);
  1332. }
  1333. #endif
  1334. static void sony_state_worker(struct work_struct *work)
  1335. {
  1336. struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
  1337. sc->send_output_report(sc);
  1338. }
  1339. static int sony_allocate_output_report(struct sony_sc *sc)
  1340. {
  1341. if ((sc->quirks & SIXAXIS_CONTROLLER) ||
  1342. (sc->quirks & NAVIGATION_CONTROLLER))
  1343. sc->output_report_dmabuf =
  1344. devm_kmalloc(&sc->hdev->dev,
  1345. sizeof(union sixaxis_output_report_01),
  1346. GFP_KERNEL);
  1347. else if (sc->quirks & MOTION_CONTROLLER)
  1348. sc->output_report_dmabuf = devm_kmalloc(&sc->hdev->dev,
  1349. MOTION_REPORT_0x02_SIZE,
  1350. GFP_KERNEL);
  1351. else
  1352. return 0;
  1353. if (!sc->output_report_dmabuf)
  1354. return -ENOMEM;
  1355. return 0;
  1356. }
  1357. #ifdef CONFIG_SONY_FF
  1358. static int sony_play_effect(struct input_dev *dev, void *data,
  1359. struct ff_effect *effect)
  1360. {
  1361. struct hid_device *hid = input_get_drvdata(dev);
  1362. struct sony_sc *sc = hid_get_drvdata(hid);
  1363. if (effect->type != FF_RUMBLE)
  1364. return 0;
  1365. sc->left = effect->u.rumble.strong_magnitude / 256;
  1366. sc->right = effect->u.rumble.weak_magnitude / 256;
  1367. sony_schedule_work(sc, SONY_WORKER_STATE);
  1368. return 0;
  1369. }
  1370. static int sony_init_ff(struct sony_sc *sc)
  1371. {
  1372. struct hid_input *hidinput;
  1373. struct input_dev *input_dev;
  1374. if (list_empty(&sc->hdev->inputs)) {
  1375. hid_err(sc->hdev, "no inputs found\n");
  1376. return -ENODEV;
  1377. }
  1378. hidinput = list_entry(sc->hdev->inputs.next, struct hid_input, list);
  1379. input_dev = hidinput->input;
  1380. input_set_capability(input_dev, EV_FF, FF_RUMBLE);
  1381. return input_ff_create_memless(input_dev, NULL, sony_play_effect);
  1382. }
  1383. #else
  1384. static int sony_init_ff(struct sony_sc *sc)
  1385. {
  1386. return 0;
  1387. }
  1388. #endif
  1389. static int sony_battery_get_property(struct power_supply *psy,
  1390. enum power_supply_property psp,
  1391. union power_supply_propval *val)
  1392. {
  1393. struct sony_sc *sc = power_supply_get_drvdata(psy);
  1394. unsigned long flags;
  1395. int ret = 0;
  1396. u8 battery_capacity;
  1397. int battery_status;
  1398. spin_lock_irqsave(&sc->lock, flags);
  1399. battery_capacity = sc->battery_capacity;
  1400. battery_status = sc->battery_status;
  1401. spin_unlock_irqrestore(&sc->lock, flags);
  1402. switch (psp) {
  1403. case POWER_SUPPLY_PROP_PRESENT:
  1404. val->intval = 1;
  1405. break;
  1406. case POWER_SUPPLY_PROP_SCOPE:
  1407. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  1408. break;
  1409. case POWER_SUPPLY_PROP_CAPACITY:
  1410. val->intval = battery_capacity;
  1411. break;
  1412. case POWER_SUPPLY_PROP_STATUS:
  1413. val->intval = battery_status;
  1414. break;
  1415. default:
  1416. ret = -EINVAL;
  1417. break;
  1418. }
  1419. return ret;
  1420. }
  1421. static int sony_battery_probe(struct sony_sc *sc, int append_dev_id)
  1422. {
  1423. const char *battery_str_fmt = append_dev_id ?
  1424. "sony_controller_battery_%pMR_%i" :
  1425. "sony_controller_battery_%pMR";
  1426. struct power_supply_config psy_cfg = { .drv_data = sc, };
  1427. struct hid_device *hdev = sc->hdev;
  1428. int ret;
  1429. /*
  1430. * Set the default battery level to 100% to avoid low battery warnings
  1431. * if the battery is polled before the first device report is received.
  1432. */
  1433. sc->battery_capacity = 100;
  1434. sc->battery_desc.properties = sony_battery_props;
  1435. sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
  1436. sc->battery_desc.get_property = sony_battery_get_property;
  1437. sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  1438. sc->battery_desc.use_for_apm = 0;
  1439. sc->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  1440. battery_str_fmt, sc->mac_address, sc->device_id);
  1441. if (!sc->battery_desc.name)
  1442. return -ENOMEM;
  1443. sc->battery = devm_power_supply_register(&hdev->dev, &sc->battery_desc,
  1444. &psy_cfg);
  1445. if (IS_ERR(sc->battery)) {
  1446. ret = PTR_ERR(sc->battery);
  1447. hid_err(hdev, "Unable to register battery device\n");
  1448. return ret;
  1449. }
  1450. power_supply_powers(sc->battery, &hdev->dev);
  1451. return 0;
  1452. }
  1453. /*
  1454. * If a controller is plugged in via USB while already connected via Bluetooth
  1455. * it will show up as two devices. A global list of connected controllers and
  1456. * their MAC addresses is maintained to ensure that a device is only connected
  1457. * once.
  1458. *
  1459. * Some USB-only devices masquerade as Sixaxis controllers and all have the
  1460. * same dummy Bluetooth address, so a comparison of the connection type is
  1461. * required. Devices are only rejected in the case where two devices have
  1462. * matching Bluetooth addresses on different bus types.
  1463. */
  1464. static inline int sony_compare_connection_type(struct sony_sc *sc0,
  1465. struct sony_sc *sc1)
  1466. {
  1467. const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE);
  1468. const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE);
  1469. return sc0_not_bt == sc1_not_bt;
  1470. }
  1471. static int sony_check_add_dev_list(struct sony_sc *sc)
  1472. {
  1473. struct sony_sc *entry;
  1474. unsigned long flags;
  1475. int ret;
  1476. spin_lock_irqsave(&sony_dev_list_lock, flags);
  1477. list_for_each_entry(entry, &sony_device_list, list_node) {
  1478. ret = memcmp(sc->mac_address, entry->mac_address,
  1479. sizeof(sc->mac_address));
  1480. if (!ret) {
  1481. if (sony_compare_connection_type(sc, entry)) {
  1482. ret = 1;
  1483. } else {
  1484. ret = -EEXIST;
  1485. hid_info(sc->hdev,
  1486. "controller with MAC address %pMR already connected\n",
  1487. sc->mac_address);
  1488. }
  1489. goto unlock;
  1490. }
  1491. }
  1492. ret = 0;
  1493. list_add(&(sc->list_node), &sony_device_list);
  1494. unlock:
  1495. spin_unlock_irqrestore(&sony_dev_list_lock, flags);
  1496. return ret;
  1497. }
  1498. static void sony_remove_dev_list(struct sony_sc *sc)
  1499. {
  1500. unsigned long flags;
  1501. if (sc->list_node.next) {
  1502. spin_lock_irqsave(&sony_dev_list_lock, flags);
  1503. list_del(&(sc->list_node));
  1504. spin_unlock_irqrestore(&sony_dev_list_lock, flags);
  1505. }
  1506. }
  1507. static int sony_get_bt_devaddr(struct sony_sc *sc)
  1508. {
  1509. int ret;
  1510. /* HIDP stores the device MAC address as a string in the uniq field. */
  1511. ret = strlen(sc->hdev->uniq);
  1512. if (ret != 17)
  1513. return -EINVAL;
  1514. ret = sscanf(sc->hdev->uniq,
  1515. "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  1516. &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
  1517. &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
  1518. if (ret != 6)
  1519. return -EINVAL;
  1520. return 0;
  1521. }
  1522. static int sony_check_add(struct sony_sc *sc)
  1523. {
  1524. u8 *buf = NULL;
  1525. int n, ret;
  1526. if ((sc->quirks & MOTION_CONTROLLER_BT) ||
  1527. (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
  1528. (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
  1529. /*
  1530. * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
  1531. * address from the uniq string where HIDP stores it.
  1532. * As uniq cannot be guaranteed to be a MAC address in all cases
  1533. * a failure of this function should not prevent the connection.
  1534. */
  1535. if (sony_get_bt_devaddr(sc) < 0) {
  1536. hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
  1537. return 0;
  1538. }
  1539. } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
  1540. (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
  1541. buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
  1542. if (!buf)
  1543. return -ENOMEM;
  1544. /*
  1545. * The MAC address of a Sixaxis controller connected via USB can
  1546. * be retrieved with feature report 0xf2. The address begins at
  1547. * offset 4.
  1548. */
  1549. ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
  1550. SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
  1551. HID_REQ_GET_REPORT);
  1552. if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
  1553. hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
  1554. ret = ret < 0 ? ret : -EINVAL;
  1555. goto out_free;
  1556. }
  1557. /*
  1558. * The Sixaxis device MAC in the report is big-endian and must
  1559. * be byte-swapped.
  1560. */
  1561. for (n = 0; n < 6; n++)
  1562. sc->mac_address[5-n] = buf[4+n];
  1563. snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
  1564. "%pMR", sc->mac_address);
  1565. } else {
  1566. return 0;
  1567. }
  1568. ret = sony_check_add_dev_list(sc);
  1569. out_free:
  1570. kfree(buf);
  1571. return ret;
  1572. }
  1573. static int sony_set_device_id(struct sony_sc *sc)
  1574. {
  1575. int ret;
  1576. /*
  1577. * Only Sixaxis controllers get an id.
  1578. * All others are set to -1.
  1579. */
  1580. if (sc->quirks & SIXAXIS_CONTROLLER) {
  1581. ret = ida_alloc(&sony_device_id_allocator, GFP_KERNEL);
  1582. if (ret < 0) {
  1583. sc->device_id = -1;
  1584. return ret;
  1585. }
  1586. sc->device_id = ret;
  1587. } else {
  1588. sc->device_id = -1;
  1589. }
  1590. return 0;
  1591. }
  1592. static void sony_release_device_id(struct sony_sc *sc)
  1593. {
  1594. if (sc->device_id >= 0) {
  1595. ida_free(&sony_device_id_allocator, sc->device_id);
  1596. sc->device_id = -1;
  1597. }
  1598. }
  1599. static inline void sony_init_output_report(struct sony_sc *sc,
  1600. void (*send_output_report)(struct sony_sc *))
  1601. {
  1602. sc->send_output_report = send_output_report;
  1603. if (!sc->state_worker_initialized)
  1604. INIT_WORK(&sc->state_worker, sony_state_worker);
  1605. sc->state_worker_initialized = 1;
  1606. }
  1607. static inline void sony_cancel_work_sync(struct sony_sc *sc)
  1608. {
  1609. unsigned long flags;
  1610. if (sc->state_worker_initialized) {
  1611. spin_lock_irqsave(&sc->lock, flags);
  1612. sc->state_worker_initialized = 0;
  1613. spin_unlock_irqrestore(&sc->lock, flags);
  1614. cancel_work_sync(&sc->state_worker);
  1615. }
  1616. }
  1617. static int sony_input_configured(struct hid_device *hdev,
  1618. struct hid_input *hidinput)
  1619. {
  1620. struct sony_sc *sc = hid_get_drvdata(hdev);
  1621. int append_dev_id;
  1622. int ret;
  1623. ret = sony_set_device_id(sc);
  1624. if (ret < 0) {
  1625. hid_err(hdev, "failed to allocate the device id\n");
  1626. goto err_stop;
  1627. }
  1628. ret = append_dev_id = sony_check_add(sc);
  1629. if (ret < 0)
  1630. goto err_stop;
  1631. ret = sony_allocate_output_report(sc);
  1632. if (ret < 0) {
  1633. hid_err(hdev, "failed to allocate the output report buffer\n");
  1634. goto err_stop;
  1635. }
  1636. if (sc->quirks & NAVIGATION_CONTROLLER_USB) {
  1637. /*
  1638. * The Sony Sixaxis does not handle HID Output Reports on the
  1639. * Interrupt EP like it could, so we need to force HID Output
  1640. * Reports to use HID_REQ_SET_REPORT on the Control EP.
  1641. *
  1642. * There is also another issue about HID Output Reports via USB,
  1643. * the Sixaxis does not want the report_id as part of the data
  1644. * packet, so we have to discard buf[0] when sending the actual
  1645. * control message, even for numbered reports, humpf!
  1646. *
  1647. * Additionally, the Sixaxis on USB isn't properly initialized
  1648. * until the PS logo button is pressed and as such won't retain
  1649. * any state set by an output report, so the initial
  1650. * configuration report is deferred until the first input
  1651. * report arrives.
  1652. */
  1653. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  1654. hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
  1655. sc->defer_initialization = 1;
  1656. ret = sixaxis_set_operational_usb(hdev);
  1657. if (ret < 0) {
  1658. hid_err(hdev, "Failed to set controller into operational mode\n");
  1659. goto err_stop;
  1660. }
  1661. sony_init_output_report(sc, sixaxis_send_output_report);
  1662. } else if (sc->quirks & NAVIGATION_CONTROLLER_BT) {
  1663. /*
  1664. * The Navigation controller wants output reports sent on the ctrl
  1665. * endpoint when connected via Bluetooth.
  1666. */
  1667. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  1668. ret = sixaxis_set_operational_bt(hdev);
  1669. if (ret < 0) {
  1670. hid_err(hdev, "Failed to set controller into operational mode\n");
  1671. goto err_stop;
  1672. }
  1673. sony_init_output_report(sc, sixaxis_send_output_report);
  1674. } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
  1675. /*
  1676. * The Sony Sixaxis does not handle HID Output Reports on the
  1677. * Interrupt EP and the device only becomes active when the
  1678. * PS button is pressed. See comment for Navigation controller
  1679. * above for more details.
  1680. */
  1681. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  1682. hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
  1683. sc->defer_initialization = 1;
  1684. ret = sixaxis_set_operational_usb(hdev);
  1685. if (ret < 0) {
  1686. hid_err(hdev, "Failed to set controller into operational mode\n");
  1687. goto err_stop;
  1688. }
  1689. ret = sony_register_sensors(sc);
  1690. if (ret) {
  1691. hid_err(sc->hdev,
  1692. "Unable to initialize motion sensors: %d\n", ret);
  1693. goto err_stop;
  1694. }
  1695. sony_init_output_report(sc, sixaxis_send_output_report);
  1696. } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
  1697. /*
  1698. * The Sixaxis wants output reports sent on the ctrl endpoint
  1699. * when connected via Bluetooth.
  1700. */
  1701. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  1702. ret = sixaxis_set_operational_bt(hdev);
  1703. if (ret < 0) {
  1704. hid_err(hdev, "Failed to set controller into operational mode\n");
  1705. goto err_stop;
  1706. }
  1707. ret = sony_register_sensors(sc);
  1708. if (ret) {
  1709. hid_err(sc->hdev,
  1710. "Unable to initialize motion sensors: %d\n", ret);
  1711. goto err_stop;
  1712. }
  1713. sony_init_output_report(sc, sixaxis_send_output_report);
  1714. } else if (sc->quirks & NSG_MRXU_REMOTE) {
  1715. /*
  1716. * The NSG-MRxU touchpad supports 2 touches and has a
  1717. * resolution of 1667x1868
  1718. */
  1719. ret = sony_register_touchpad(sc, 2,
  1720. NSG_MRXU_MAX_X, NSG_MRXU_MAX_Y, 15, 15, 1);
  1721. if (ret) {
  1722. hid_err(sc->hdev,
  1723. "Unable to initialize multi-touch slots: %d\n",
  1724. ret);
  1725. goto err_stop;
  1726. }
  1727. } else if (sc->quirks & MOTION_CONTROLLER) {
  1728. sony_init_output_report(sc, motion_send_output_report);
  1729. }
  1730. if (sc->quirks & SONY_LED_SUPPORT) {
  1731. ret = sony_leds_init(sc);
  1732. if (ret < 0)
  1733. goto err_stop;
  1734. }
  1735. if (sc->quirks & SONY_BATTERY_SUPPORT) {
  1736. ret = sony_battery_probe(sc, append_dev_id);
  1737. if (ret < 0)
  1738. goto err_stop;
  1739. /* Open the device to receive reports with battery info */
  1740. ret = hid_hw_open(hdev);
  1741. if (ret < 0) {
  1742. hid_err(hdev, "hw open failed\n");
  1743. goto err_stop;
  1744. }
  1745. }
  1746. if (sc->quirks & SONY_FF_SUPPORT) {
  1747. ret = sony_init_ff(sc);
  1748. if (ret < 0)
  1749. goto err_close;
  1750. }
  1751. return 0;
  1752. err_close:
  1753. hid_hw_close(hdev);
  1754. err_stop:
  1755. sony_cancel_work_sync(sc);
  1756. sony_remove_dev_list(sc);
  1757. sony_release_device_id(sc);
  1758. return ret;
  1759. }
  1760. static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
  1761. {
  1762. int ret;
  1763. unsigned long quirks = id->driver_data;
  1764. struct sony_sc *sc;
  1765. struct usb_device *usbdev;
  1766. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  1767. if (!strcmp(hdev->name, "FutureMax Dance Mat"))
  1768. quirks |= FUTUREMAX_DANCE_MAT;
  1769. if (!strcmp(hdev->name, "SHANWAN PS3 GamePad") ||
  1770. !strcmp(hdev->name, "ShanWan PS(R) Ga`epad"))
  1771. quirks |= SHANWAN_GAMEPAD;
  1772. sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
  1773. if (sc == NULL) {
  1774. hid_err(hdev, "can't alloc sony descriptor\n");
  1775. return -ENOMEM;
  1776. }
  1777. spin_lock_init(&sc->lock);
  1778. sc->quirks = quirks;
  1779. hid_set_drvdata(hdev, sc);
  1780. sc->hdev = hdev;
  1781. ret = hid_parse(hdev);
  1782. if (ret) {
  1783. hid_err(hdev, "parse failed\n");
  1784. return ret;
  1785. }
  1786. if (sc->quirks & VAIO_RDESC_CONSTANT)
  1787. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  1788. else if (sc->quirks & SIXAXIS_CONTROLLER)
  1789. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  1790. /* Patch the hw version on DS3 compatible devices, so applications can
  1791. * distinguish between the default HID mappings and the mappings defined
  1792. * by the Linux game controller spec. This is important for the SDL2
  1793. * library, which has a game controller database, which uses device ids
  1794. * in combination with version as a key.
  1795. */
  1796. if (sc->quirks & SIXAXIS_CONTROLLER)
  1797. hdev->version |= 0x8000;
  1798. ret = hid_hw_start(hdev, connect_mask);
  1799. if (ret) {
  1800. hid_err(hdev, "hw start failed\n");
  1801. return ret;
  1802. }
  1803. /* sony_input_configured can fail, but this doesn't result
  1804. * in hid_hw_start failures (intended). Check whether
  1805. * the HID layer claimed the device else fail.
  1806. * We don't know the actual reason for the failure, most
  1807. * likely it is due to EEXIST in case of double connection
  1808. * of USB and Bluetooth, but could have been due to ENOMEM
  1809. * or other reasons as well.
  1810. */
  1811. if (!(hdev->claimed & HID_CLAIMED_INPUT)) {
  1812. hid_err(hdev, "failed to claim input\n");
  1813. ret = -ENODEV;
  1814. goto err;
  1815. }
  1816. if (sc->quirks & (GHL_GUITAR_PS3WIIU | GHL_GUITAR_PS4)) {
  1817. if (!hid_is_usb(hdev)) {
  1818. ret = -EINVAL;
  1819. goto err;
  1820. }
  1821. usbdev = to_usb_device(sc->hdev->dev.parent->parent);
  1822. sc->ghl_urb = usb_alloc_urb(0, GFP_ATOMIC);
  1823. if (!sc->ghl_urb) {
  1824. ret = -ENOMEM;
  1825. goto err;
  1826. }
  1827. if (sc->quirks & GHL_GUITAR_PS3WIIU)
  1828. ret = ghl_init_urb(sc, usbdev, ghl_ps3wiiu_magic_data,
  1829. ARRAY_SIZE(ghl_ps3wiiu_magic_data));
  1830. else if (sc->quirks & GHL_GUITAR_PS4)
  1831. ret = ghl_init_urb(sc, usbdev, ghl_ps4_magic_data,
  1832. ARRAY_SIZE(ghl_ps4_magic_data));
  1833. if (ret) {
  1834. hid_err(hdev, "error preparing URB\n");
  1835. goto err;
  1836. }
  1837. timer_setup(&sc->ghl_poke_timer, ghl_magic_poke, 0);
  1838. mod_timer(&sc->ghl_poke_timer,
  1839. jiffies + GHL_GUITAR_POKE_INTERVAL*HZ);
  1840. }
  1841. return ret;
  1842. err:
  1843. usb_free_urb(sc->ghl_urb);
  1844. hid_hw_stop(hdev);
  1845. return ret;
  1846. }
  1847. static void sony_remove(struct hid_device *hdev)
  1848. {
  1849. struct sony_sc *sc = hid_get_drvdata(hdev);
  1850. if (sc->quirks & (GHL_GUITAR_PS3WIIU | GHL_GUITAR_PS4)) {
  1851. del_timer_sync(&sc->ghl_poke_timer);
  1852. usb_free_urb(sc->ghl_urb);
  1853. }
  1854. hid_hw_close(hdev);
  1855. sony_cancel_work_sync(sc);
  1856. sony_remove_dev_list(sc);
  1857. sony_release_device_id(sc);
  1858. hid_hw_stop(hdev);
  1859. }
  1860. #ifdef CONFIG_PM
  1861. static int sony_suspend(struct hid_device *hdev, pm_message_t message)
  1862. {
  1863. #ifdef CONFIG_SONY_FF
  1864. /* On suspend stop any running force-feedback events */
  1865. if (SONY_FF_SUPPORT) {
  1866. struct sony_sc *sc = hid_get_drvdata(hdev);
  1867. sc->left = sc->right = 0;
  1868. sony_send_output_report(sc);
  1869. }
  1870. #endif
  1871. return 0;
  1872. }
  1873. static int sony_resume(struct hid_device *hdev)
  1874. {
  1875. struct sony_sc *sc = hid_get_drvdata(hdev);
  1876. /*
  1877. * The Sixaxis and navigation controllers on USB need to be
  1878. * reinitialized on resume or they won't behave properly.
  1879. */
  1880. if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
  1881. (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
  1882. sixaxis_set_operational_usb(sc->hdev);
  1883. sc->defer_initialization = 1;
  1884. }
  1885. return 0;
  1886. }
  1887. #endif
  1888. static const struct hid_device_id sony_devices[] = {
  1889. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  1890. .driver_data = SIXAXIS_CONTROLLER_USB },
  1891. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
  1892. .driver_data = NAVIGATION_CONTROLLER_USB },
  1893. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
  1894. .driver_data = NAVIGATION_CONTROLLER_BT },
  1895. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
  1896. .driver_data = MOTION_CONTROLLER_USB },
  1897. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
  1898. .driver_data = MOTION_CONTROLLER_BT },
  1899. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  1900. .driver_data = SIXAXIS_CONTROLLER_BT },
  1901. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
  1902. .driver_data = VAIO_RDESC_CONSTANT },
  1903. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
  1904. .driver_data = VAIO_RDESC_CONSTANT },
  1905. /*
  1906. * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
  1907. * Logitech joystick from the device descriptor.
  1908. */
  1909. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
  1910. .driver_data = BUZZ_CONTROLLER },
  1911. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
  1912. .driver_data = BUZZ_CONTROLLER },
  1913. /* PS3 BD Remote Control */
  1914. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
  1915. .driver_data = PS3REMOTE },
  1916. /* Logitech Harmony Adapter for PS3 */
  1917. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
  1918. .driver_data = PS3REMOTE },
  1919. /* SMK-Link PS3 BD Remote Control */
  1920. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
  1921. .driver_data = PS3REMOTE },
  1922. /* Nyko Core Controller for PS3 */
  1923. { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
  1924. .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
  1925. /* SMK-Link NSG-MR5U Remote Control */
  1926. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_NSG_MR5U_REMOTE),
  1927. .driver_data = NSG_MR5U_REMOTE_BT },
  1928. /* SMK-Link NSG-MR7U Remote Control */
  1929. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_NSG_MR7U_REMOTE),
  1930. .driver_data = NSG_MR7U_REMOTE_BT },
  1931. /* Guitar Hero Live PS3 and Wii U guitar dongles */
  1932. { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3WIIU_GHLIVE_DONGLE),
  1933. .driver_data = GHL_GUITAR_PS3WIIU | GH_GUITAR_CONTROLLER },
  1934. /* Guitar Hero PC Guitar Dongle */
  1935. { HID_USB_DEVICE(USB_VENDOR_ID_REDOCTANE, USB_DEVICE_ID_REDOCTANE_GUITAR_DONGLE),
  1936. .driver_data = GH_GUITAR_CONTROLLER },
  1937. /* Guitar Hero PS3 World Tour Guitar Dongle */
  1938. { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_GUITAR_DONGLE),
  1939. .driver_data = GH_GUITAR_CONTROLLER },
  1940. /* Guitar Hero Live PS4 guitar dongles */
  1941. { HID_USB_DEVICE(USB_VENDOR_ID_REDOCTANE, USB_DEVICE_ID_REDOCTANE_PS4_GHLIVE_DONGLE),
  1942. .driver_data = GHL_GUITAR_PS4 | GH_GUITAR_CONTROLLER },
  1943. { }
  1944. };
  1945. MODULE_DEVICE_TABLE(hid, sony_devices);
  1946. static struct hid_driver sony_driver = {
  1947. .name = "sony",
  1948. .id_table = sony_devices,
  1949. .input_mapping = sony_mapping,
  1950. .input_configured = sony_input_configured,
  1951. .probe = sony_probe,
  1952. .remove = sony_remove,
  1953. .report_fixup = sony_report_fixup,
  1954. .raw_event = sony_raw_event,
  1955. #ifdef CONFIG_PM
  1956. .suspend = sony_suspend,
  1957. .resume = sony_resume,
  1958. .reset_resume = sony_resume,
  1959. #endif
  1960. };
  1961. static int __init sony_init(void)
  1962. {
  1963. dbg_hid("Sony:%s\n", __func__);
  1964. return hid_register_driver(&sony_driver);
  1965. }
  1966. static void __exit sony_exit(void)
  1967. {
  1968. dbg_hid("Sony:%s\n", __func__);
  1969. hid_unregister_driver(&sony_driver);
  1970. ida_destroy(&sony_device_id_allocator);
  1971. }
  1972. module_init(sony_init);
  1973. module_exit(sony_exit);
  1974. MODULE_DESCRIPTION("HID driver for Sony / PS2 / PS3 / PS4 BD devices");
  1975. MODULE_LICENSE("GPL");