hid-sony.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053
  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-2016 Frank Praznik <frank.praznik@gmail.com>
  12. * Copyright (c) 2018 Todd Kelner
  13. */
  14. /*
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the Free
  17. * Software Foundation; either version 2 of the License, or (at your option)
  18. * any later version.
  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 <asm/unaligned.h>
  39. #include "hid-ids.h"
  40. #define VAIO_RDESC_CONSTANT BIT(0)
  41. #define SIXAXIS_CONTROLLER_USB BIT(1)
  42. #define SIXAXIS_CONTROLLER_BT BIT(2)
  43. #define BUZZ_CONTROLLER BIT(3)
  44. #define PS3REMOTE BIT(4)
  45. #define DUALSHOCK4_CONTROLLER_USB BIT(5)
  46. #define DUALSHOCK4_CONTROLLER_BT BIT(6)
  47. #define DUALSHOCK4_DONGLE BIT(7)
  48. #define MOTION_CONTROLLER_USB BIT(8)
  49. #define MOTION_CONTROLLER_BT BIT(9)
  50. #define NAVIGATION_CONTROLLER_USB BIT(10)
  51. #define NAVIGATION_CONTROLLER_BT BIT(11)
  52. #define SINO_LITE_CONTROLLER BIT(12)
  53. #define FUTUREMAX_DANCE_MAT BIT(13)
  54. #define NSG_MR5U_REMOTE_BT BIT(14)
  55. #define NSG_MR7U_REMOTE_BT BIT(15)
  56. #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
  57. #define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
  58. #define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
  59. NAVIGATION_CONTROLLER_BT)
  60. #define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
  61. DUALSHOCK4_CONTROLLER_BT | \
  62. DUALSHOCK4_DONGLE)
  63. #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
  64. DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
  65. NAVIGATION_CONTROLLER)
  66. #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
  67. MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
  68. #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
  69. MOTION_CONTROLLER)
  70. #define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_BT |\
  71. MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT)
  72. #define NSG_MRXU_REMOTE (NSG_MR5U_REMOTE_BT | NSG_MR7U_REMOTE_BT)
  73. #define MAX_LEDS 4
  74. #define NSG_MRXU_MAX_X 1667
  75. #define NSG_MRXU_MAX_Y 1868
  76. /* PS/3 Motion controller */
  77. static u8 motion_rdesc[] = {
  78. 0x05, 0x01, /* Usage Page (Desktop), */
  79. 0x09, 0x04, /* Usage (Joystick), */
  80. 0xA1, 0x01, /* Collection (Application), */
  81. 0xA1, 0x02, /* Collection (Logical), */
  82. 0x85, 0x01, /* Report ID (1), */
  83. 0x75, 0x01, /* Report Size (1), */
  84. 0x95, 0x15, /* Report Count (21), */
  85. 0x15, 0x00, /* Logical Minimum (0), */
  86. 0x25, 0x01, /* Logical Maximum (1), */
  87. 0x35, 0x00, /* Physical Minimum (0), */
  88. 0x45, 0x01, /* Physical Maximum (1), */
  89. 0x05, 0x09, /* Usage Page (Button), */
  90. 0x19, 0x01, /* Usage Minimum (01h), */
  91. 0x29, 0x15, /* Usage Maximum (15h), */
  92. 0x81, 0x02, /* Input (Variable), * Buttons */
  93. 0x95, 0x0B, /* Report Count (11), */
  94. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  95. 0x81, 0x03, /* Input (Constant, Variable), * Padding */
  96. 0x15, 0x00, /* Logical Minimum (0), */
  97. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  98. 0x05, 0x01, /* Usage Page (Desktop), */
  99. 0xA1, 0x00, /* Collection (Physical), */
  100. 0x75, 0x08, /* Report Size (8), */
  101. 0x95, 0x01, /* Report Count (1), */
  102. 0x35, 0x00, /* Physical Minimum (0), */
  103. 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
  104. 0x09, 0x30, /* Usage (X), */
  105. 0x81, 0x02, /* Input (Variable), * Trigger */
  106. 0xC0, /* End Collection, */
  107. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  108. 0x75, 0x08, /* Report Size (8), */
  109. 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
  110. 0x81, 0x02, /* Input (Variable), */
  111. 0x05, 0x01, /* Usage Page (Desktop), */
  112. 0x75, 0x10, /* Report Size (16), */
  113. 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
  114. 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
  115. 0x95, 0x03, /* Report Count (3), * 3x Accels */
  116. 0x09, 0x33, /* Usage (rX), */
  117. 0x09, 0x34, /* Usage (rY), */
  118. 0x09, 0x35, /* Usage (rZ), */
  119. 0x81, 0x02, /* Input (Variable), */
  120. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  121. 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
  122. 0x81, 0x02, /* Input (Variable), */
  123. 0x05, 0x01, /* Usage Page (Desktop), */
  124. 0x09, 0x01, /* Usage (Pointer), */
  125. 0x95, 0x03, /* Report Count (3), * 3x Gyros */
  126. 0x81, 0x02, /* Input (Variable), */
  127. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  128. 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
  129. 0x81, 0x02, /* Input (Variable), */
  130. 0x75, 0x0C, /* Report Size (12), */
  131. 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
  132. 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
  133. 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
  134. 0x81, 0x02, /* Input (Variable), */
  135. 0x75, 0x08, /* Report Size (8), */
  136. 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
  137. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  138. 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
  139. 0x81, 0x02, /* Input (Variable), */
  140. 0x75, 0x08, /* Report Size (8), */
  141. 0x95, 0x30, /* Report Count (48), */
  142. 0x09, 0x01, /* Usage (Pointer), */
  143. 0x91, 0x02, /* Output (Variable), */
  144. 0x75, 0x08, /* Report Size (8), */
  145. 0x95, 0x30, /* Report Count (48), */
  146. 0x09, 0x01, /* Usage (Pointer), */
  147. 0xB1, 0x02, /* Feature (Variable), */
  148. 0xC0, /* End Collection, */
  149. 0xA1, 0x02, /* Collection (Logical), */
  150. 0x85, 0x02, /* Report ID (2), */
  151. 0x75, 0x08, /* Report Size (8), */
  152. 0x95, 0x30, /* Report Count (48), */
  153. 0x09, 0x01, /* Usage (Pointer), */
  154. 0xB1, 0x02, /* Feature (Variable), */
  155. 0xC0, /* End Collection, */
  156. 0xA1, 0x02, /* Collection (Logical), */
  157. 0x85, 0xEE, /* Report ID (238), */
  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, 0xEF, /* Report ID (239), */
  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. 0xC0 /* End Collection */
  171. };
  172. static u8 ps3remote_rdesc[] = {
  173. 0x05, 0x01, /* GUsagePage Generic Desktop */
  174. 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
  175. 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
  176. /* Use collection 1 for joypad buttons */
  177. 0xA1, 0x02, /* MCollection Logical (interrelated data) */
  178. /*
  179. * Ignore the 1st byte, maybe it is used for a controller
  180. * number but it's not needed for correct operation
  181. */
  182. 0x75, 0x08, /* GReportSize 0x08 [8] */
  183. 0x95, 0x01, /* GReportCount 0x01 [1] */
  184. 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
  185. /*
  186. * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
  187. * buttons multiple keypresses are allowed
  188. */
  189. 0x05, 0x09, /* GUsagePage Button */
  190. 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
  191. 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
  192. 0x14, /* GLogicalMinimum [0] */
  193. 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
  194. 0x75, 0x01, /* GReportSize 0x01 [1] */
  195. 0x95, 0x18, /* GReportCount 0x18 [24] */
  196. 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
  197. 0xC0, /* MEndCollection */
  198. /* Use collection 2 for remote control buttons */
  199. 0xA1, 0x02, /* MCollection Logical (interrelated data) */
  200. /* 5th byte is used for remote control buttons */
  201. 0x05, 0x09, /* GUsagePage Button */
  202. 0x18, /* LUsageMinimum [No button pressed] */
  203. 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
  204. 0x14, /* GLogicalMinimum [0] */
  205. 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
  206. 0x75, 0x08, /* GReportSize 0x08 [8] */
  207. 0x95, 0x01, /* GReportCount 0x01 [1] */
  208. 0x80, /* MInput */
  209. /*
  210. * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
  211. * 0xff and 11th is for press indication
  212. */
  213. 0x75, 0x08, /* GReportSize 0x08 [8] */
  214. 0x95, 0x06, /* GReportCount 0x06 [6] */
  215. 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
  216. /* 12th byte is for battery strength */
  217. 0x05, 0x06, /* GUsagePage Generic Device Controls */
  218. 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
  219. 0x14, /* GLogicalMinimum [0] */
  220. 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
  221. 0x75, 0x08, /* GReportSize 0x08 [8] */
  222. 0x95, 0x01, /* GReportCount 0x01 [1] */
  223. 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
  224. 0xC0, /* MEndCollection */
  225. 0xC0 /* MEndCollection [Game Pad] */
  226. };
  227. static const unsigned int ps3remote_keymap_joypad_buttons[] = {
  228. [0x01] = KEY_SELECT,
  229. [0x02] = BTN_THUMBL, /* L3 */
  230. [0x03] = BTN_THUMBR, /* R3 */
  231. [0x04] = BTN_START,
  232. [0x05] = KEY_UP,
  233. [0x06] = KEY_RIGHT,
  234. [0x07] = KEY_DOWN,
  235. [0x08] = KEY_LEFT,
  236. [0x09] = BTN_TL2, /* L2 */
  237. [0x0a] = BTN_TR2, /* R2 */
  238. [0x0b] = BTN_TL, /* L1 */
  239. [0x0c] = BTN_TR, /* R1 */
  240. [0x0d] = KEY_OPTION, /* options/triangle */
  241. [0x0e] = KEY_BACK, /* back/circle */
  242. [0x0f] = BTN_0, /* cross */
  243. [0x10] = KEY_SCREEN, /* view/square */
  244. [0x11] = KEY_HOMEPAGE, /* PS button */
  245. [0x14] = KEY_ENTER,
  246. };
  247. static const unsigned int ps3remote_keymap_remote_buttons[] = {
  248. [0x00] = KEY_1,
  249. [0x01] = KEY_2,
  250. [0x02] = KEY_3,
  251. [0x03] = KEY_4,
  252. [0x04] = KEY_5,
  253. [0x05] = KEY_6,
  254. [0x06] = KEY_7,
  255. [0x07] = KEY_8,
  256. [0x08] = KEY_9,
  257. [0x09] = KEY_0,
  258. [0x0e] = KEY_ESC, /* return */
  259. [0x0f] = KEY_CLEAR,
  260. [0x16] = KEY_EJECTCD,
  261. [0x1a] = KEY_MENU, /* top menu */
  262. [0x28] = KEY_TIME,
  263. [0x30] = KEY_PREVIOUS,
  264. [0x31] = KEY_NEXT,
  265. [0x32] = KEY_PLAY,
  266. [0x33] = KEY_REWIND, /* scan back */
  267. [0x34] = KEY_FORWARD, /* scan forward */
  268. [0x38] = KEY_STOP,
  269. [0x39] = KEY_PAUSE,
  270. [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
  271. [0x60] = KEY_FRAMEBACK, /* slow/step back */
  272. [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
  273. [0x63] = KEY_SUBTITLE,
  274. [0x64] = KEY_AUDIO,
  275. [0x65] = KEY_ANGLE,
  276. [0x70] = KEY_INFO, /* display */
  277. [0x80] = KEY_BLUE,
  278. [0x81] = KEY_RED,
  279. [0x82] = KEY_GREEN,
  280. [0x83] = KEY_YELLOW,
  281. };
  282. static const unsigned int buzz_keymap[] = {
  283. /*
  284. * The controller has 4 remote buzzers, each with one LED and 5
  285. * buttons.
  286. *
  287. * We use the mapping chosen by the controller, which is:
  288. *
  289. * Key Offset
  290. * -------------------
  291. * Buzz 1
  292. * Blue 5
  293. * Orange 4
  294. * Green 3
  295. * Yellow 2
  296. *
  297. * So, for example, the orange button on the third buzzer is mapped to
  298. * BTN_TRIGGER_HAPPY14
  299. */
  300. [1] = BTN_TRIGGER_HAPPY1,
  301. [2] = BTN_TRIGGER_HAPPY2,
  302. [3] = BTN_TRIGGER_HAPPY3,
  303. [4] = BTN_TRIGGER_HAPPY4,
  304. [5] = BTN_TRIGGER_HAPPY5,
  305. [6] = BTN_TRIGGER_HAPPY6,
  306. [7] = BTN_TRIGGER_HAPPY7,
  307. [8] = BTN_TRIGGER_HAPPY8,
  308. [9] = BTN_TRIGGER_HAPPY9,
  309. [10] = BTN_TRIGGER_HAPPY10,
  310. [11] = BTN_TRIGGER_HAPPY11,
  311. [12] = BTN_TRIGGER_HAPPY12,
  312. [13] = BTN_TRIGGER_HAPPY13,
  313. [14] = BTN_TRIGGER_HAPPY14,
  314. [15] = BTN_TRIGGER_HAPPY15,
  315. [16] = BTN_TRIGGER_HAPPY16,
  316. [17] = BTN_TRIGGER_HAPPY17,
  317. [18] = BTN_TRIGGER_HAPPY18,
  318. [19] = BTN_TRIGGER_HAPPY19,
  319. [20] = BTN_TRIGGER_HAPPY20,
  320. };
  321. /* The Navigation controller is a partial DS3 and uses the same HID report
  322. * and hence the same keymap indices, however not not all axes/buttons
  323. * are physically present. We use the same axis and button mapping as
  324. * the DS3, which uses the Linux gamepad spec.
  325. */
  326. static const unsigned int navigation_absmap[] = {
  327. [0x30] = ABS_X,
  328. [0x31] = ABS_Y,
  329. [0x33] = ABS_Z, /* L2 */
  330. };
  331. /* Buttons not physically available on the device, but still available
  332. * in the reports are explicitly set to 0 for documentation purposes.
  333. */
  334. static const unsigned int navigation_keymap[] = {
  335. [0x01] = 0, /* Select */
  336. [0x02] = BTN_THUMBL, /* L3 */
  337. [0x03] = 0, /* R3 */
  338. [0x04] = 0, /* Start */
  339. [0x05] = BTN_DPAD_UP, /* Up */
  340. [0x06] = BTN_DPAD_RIGHT, /* Right */
  341. [0x07] = BTN_DPAD_DOWN, /* Down */
  342. [0x08] = BTN_DPAD_LEFT, /* Left */
  343. [0x09] = BTN_TL2, /* L2 */
  344. [0x0a] = 0, /* R2 */
  345. [0x0b] = BTN_TL, /* L1 */
  346. [0x0c] = 0, /* R1 */
  347. [0x0d] = BTN_NORTH, /* Triangle */
  348. [0x0e] = BTN_EAST, /* Circle */
  349. [0x0f] = BTN_SOUTH, /* Cross */
  350. [0x10] = BTN_WEST, /* Square */
  351. [0x11] = BTN_MODE, /* PS */
  352. };
  353. static const unsigned int sixaxis_absmap[] = {
  354. [0x30] = ABS_X,
  355. [0x31] = ABS_Y,
  356. [0x32] = ABS_RX, /* right stick X */
  357. [0x35] = ABS_RY, /* right stick Y */
  358. };
  359. static const unsigned int sixaxis_keymap[] = {
  360. [0x01] = BTN_SELECT, /* Select */
  361. [0x02] = BTN_THUMBL, /* L3 */
  362. [0x03] = BTN_THUMBR, /* R3 */
  363. [0x04] = BTN_START, /* Start */
  364. [0x05] = BTN_DPAD_UP, /* Up */
  365. [0x06] = BTN_DPAD_RIGHT, /* Right */
  366. [0x07] = BTN_DPAD_DOWN, /* Down */
  367. [0x08] = BTN_DPAD_LEFT, /* Left */
  368. [0x09] = BTN_TL2, /* L2 */
  369. [0x0a] = BTN_TR2, /* R2 */
  370. [0x0b] = BTN_TL, /* L1 */
  371. [0x0c] = BTN_TR, /* R1 */
  372. [0x0d] = BTN_NORTH, /* Triangle */
  373. [0x0e] = BTN_EAST, /* Circle */
  374. [0x0f] = BTN_SOUTH, /* Cross */
  375. [0x10] = BTN_WEST, /* Square */
  376. [0x11] = BTN_MODE, /* PS */
  377. };
  378. static const unsigned int ds4_absmap[] = {
  379. [0x30] = ABS_X,
  380. [0x31] = ABS_Y,
  381. [0x32] = ABS_RX, /* right stick X */
  382. [0x33] = ABS_Z, /* L2 */
  383. [0x34] = ABS_RZ, /* R2 */
  384. [0x35] = ABS_RY, /* right stick Y */
  385. };
  386. static const unsigned int ds4_keymap[] = {
  387. [0x1] = BTN_WEST, /* Square */
  388. [0x2] = BTN_SOUTH, /* Cross */
  389. [0x3] = BTN_EAST, /* Circle */
  390. [0x4] = BTN_NORTH, /* Triangle */
  391. [0x5] = BTN_TL, /* L1 */
  392. [0x6] = BTN_TR, /* R1 */
  393. [0x7] = BTN_TL2, /* L2 */
  394. [0x8] = BTN_TR2, /* R2 */
  395. [0x9] = BTN_SELECT, /* Share */
  396. [0xa] = BTN_START, /* Options */
  397. [0xb] = BTN_THUMBL, /* L3 */
  398. [0xc] = BTN_THUMBR, /* R3 */
  399. [0xd] = BTN_MODE, /* PS */
  400. };
  401. static const struct {int x; int y; } ds4_hat_mapping[] = {
  402. {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
  403. {0, 0}
  404. };
  405. static enum power_supply_property sony_battery_props[] = {
  406. POWER_SUPPLY_PROP_PRESENT,
  407. POWER_SUPPLY_PROP_CAPACITY,
  408. POWER_SUPPLY_PROP_SCOPE,
  409. POWER_SUPPLY_PROP_STATUS,
  410. };
  411. struct sixaxis_led {
  412. u8 time_enabled; /* the total time the led is active (0xff means forever) */
  413. u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
  414. u8 enabled;
  415. u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
  416. u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
  417. } __packed;
  418. struct sixaxis_rumble {
  419. u8 padding;
  420. u8 right_duration; /* Right motor duration (0xff means forever) */
  421. u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
  422. u8 left_duration; /* Left motor duration (0xff means forever) */
  423. u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
  424. } __packed;
  425. struct sixaxis_output_report {
  426. u8 report_id;
  427. struct sixaxis_rumble rumble;
  428. u8 padding[4];
  429. u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
  430. struct sixaxis_led led[4]; /* LEDx at (4 - x) */
  431. struct sixaxis_led _reserved; /* LED5, not actually soldered */
  432. } __packed;
  433. union sixaxis_output_report_01 {
  434. struct sixaxis_output_report data;
  435. u8 buf[36];
  436. };
  437. struct motion_output_report_02 {
  438. u8 type, zero;
  439. u8 r, g, b;
  440. u8 zero2;
  441. u8 rumble;
  442. };
  443. #define DS4_FEATURE_REPORT_0x02_SIZE 37
  444. #define DS4_FEATURE_REPORT_0x05_SIZE 41
  445. #define DS4_FEATURE_REPORT_0x81_SIZE 7
  446. #define DS4_FEATURE_REPORT_0xA3_SIZE 49
  447. #define DS4_INPUT_REPORT_0x11_SIZE 78
  448. #define DS4_OUTPUT_REPORT_0x05_SIZE 32
  449. #define DS4_OUTPUT_REPORT_0x11_SIZE 78
  450. #define SIXAXIS_REPORT_0xF2_SIZE 17
  451. #define SIXAXIS_REPORT_0xF5_SIZE 8
  452. #define MOTION_REPORT_0x02_SIZE 49
  453. /* Offsets relative to USB input report (0x1). Bluetooth (0x11) requires an
  454. * additional +2.
  455. */
  456. #define DS4_INPUT_REPORT_AXIS_OFFSET 1
  457. #define DS4_INPUT_REPORT_BUTTON_OFFSET 5
  458. #define DS4_INPUT_REPORT_TIMESTAMP_OFFSET 10
  459. #define DS4_INPUT_REPORT_GYRO_X_OFFSET 13
  460. #define DS4_INPUT_REPORT_BATTERY_OFFSET 30
  461. #define DS4_INPUT_REPORT_TOUCHPAD_OFFSET 33
  462. #define SENSOR_SUFFIX " Motion Sensors"
  463. #define DS4_TOUCHPAD_SUFFIX " Touchpad"
  464. /* Default to 4ms poll interval, which is same as USB (not adjustable). */
  465. #define DS4_BT_DEFAULT_POLL_INTERVAL_MS 4
  466. #define DS4_BT_MAX_POLL_INTERVAL_MS 62
  467. #define DS4_GYRO_RES_PER_DEG_S 1024
  468. #define DS4_ACC_RES_PER_G 8192
  469. #define SIXAXIS_INPUT_REPORT_ACC_X_OFFSET 41
  470. #define SIXAXIS_ACC_RES_PER_G 113
  471. static DEFINE_SPINLOCK(sony_dev_list_lock);
  472. static LIST_HEAD(sony_device_list);
  473. static DEFINE_IDA(sony_device_id_allocator);
  474. /* Used for calibration of DS4 accelerometer and gyro. */
  475. struct ds4_calibration_data {
  476. int abs_code;
  477. short bias;
  478. /* Calibration requires scaling against a sensitivity value, which is a
  479. * float. Store sensitivity as a fraction to limit floating point
  480. * calculations until final calibration.
  481. */
  482. int sens_numer;
  483. int sens_denom;
  484. };
  485. enum ds4_dongle_state {
  486. DONGLE_DISCONNECTED,
  487. DONGLE_CALIBRATING,
  488. DONGLE_CONNECTED,
  489. DONGLE_DISABLED
  490. };
  491. enum sony_worker {
  492. SONY_WORKER_STATE,
  493. SONY_WORKER_HOTPLUG
  494. };
  495. struct sony_sc {
  496. spinlock_t lock;
  497. struct list_head list_node;
  498. struct hid_device *hdev;
  499. struct input_dev *touchpad;
  500. struct input_dev *sensor_dev;
  501. struct led_classdev *leds[MAX_LEDS];
  502. unsigned long quirks;
  503. struct work_struct hotplug_worker;
  504. struct work_struct state_worker;
  505. void (*send_output_report)(struct sony_sc *);
  506. struct power_supply *battery;
  507. struct power_supply_desc battery_desc;
  508. int device_id;
  509. unsigned fw_version;
  510. unsigned hw_version;
  511. u8 *output_report_dmabuf;
  512. #ifdef CONFIG_SONY_FF
  513. u8 left;
  514. u8 right;
  515. #endif
  516. u8 mac_address[6];
  517. u8 hotplug_worker_initialized;
  518. u8 state_worker_initialized;
  519. u8 defer_initialization;
  520. u8 cable_state;
  521. u8 battery_charging;
  522. u8 battery_capacity;
  523. u8 led_state[MAX_LEDS];
  524. u8 led_delay_on[MAX_LEDS];
  525. u8 led_delay_off[MAX_LEDS];
  526. u8 led_count;
  527. bool timestamp_initialized;
  528. u16 prev_timestamp;
  529. unsigned int timestamp_us;
  530. u8 ds4_bt_poll_interval;
  531. enum ds4_dongle_state ds4_dongle_state;
  532. /* DS4 calibration data */
  533. struct ds4_calibration_data ds4_calib_data[6];
  534. };
  535. static void sony_set_leds(struct sony_sc *sc);
  536. static inline void sony_schedule_work(struct sony_sc *sc,
  537. enum sony_worker which)
  538. {
  539. unsigned long flags;
  540. switch (which) {
  541. case SONY_WORKER_STATE:
  542. spin_lock_irqsave(&sc->lock, flags);
  543. if (!sc->defer_initialization && sc->state_worker_initialized)
  544. schedule_work(&sc->state_worker);
  545. spin_unlock_irqrestore(&sc->lock, flags);
  546. break;
  547. case SONY_WORKER_HOTPLUG:
  548. if (sc->hotplug_worker_initialized)
  549. schedule_work(&sc->hotplug_worker);
  550. break;
  551. }
  552. }
  553. static ssize_t ds4_show_poll_interval(struct device *dev,
  554. struct device_attribute
  555. *attr, char *buf)
  556. {
  557. struct hid_device *hdev = to_hid_device(dev);
  558. struct sony_sc *sc = hid_get_drvdata(hdev);
  559. return snprintf(buf, PAGE_SIZE, "%i\n", sc->ds4_bt_poll_interval);
  560. }
  561. static ssize_t ds4_store_poll_interval(struct device *dev,
  562. struct device_attribute *attr,
  563. const char *buf, size_t count)
  564. {
  565. struct hid_device *hdev = to_hid_device(dev);
  566. struct sony_sc *sc = hid_get_drvdata(hdev);
  567. unsigned long flags;
  568. u8 interval;
  569. if (kstrtou8(buf, 0, &interval))
  570. return -EINVAL;
  571. if (interval > DS4_BT_MAX_POLL_INTERVAL_MS)
  572. return -EINVAL;
  573. spin_lock_irqsave(&sc->lock, flags);
  574. sc->ds4_bt_poll_interval = interval;
  575. spin_unlock_irqrestore(&sc->lock, flags);
  576. sony_schedule_work(sc, SONY_WORKER_STATE);
  577. return count;
  578. }
  579. static DEVICE_ATTR(bt_poll_interval, 0644, ds4_show_poll_interval,
  580. ds4_store_poll_interval);
  581. static ssize_t sony_show_firmware_version(struct device *dev,
  582. struct device_attribute
  583. *attr, char *buf)
  584. {
  585. struct hid_device *hdev = to_hid_device(dev);
  586. struct sony_sc *sc = hid_get_drvdata(hdev);
  587. return snprintf(buf, PAGE_SIZE, "0x%04x\n", sc->fw_version);
  588. }
  589. static DEVICE_ATTR(firmware_version, 0444, sony_show_firmware_version, NULL);
  590. static ssize_t sony_show_hardware_version(struct device *dev,
  591. struct device_attribute
  592. *attr, char *buf)
  593. {
  594. struct hid_device *hdev = to_hid_device(dev);
  595. struct sony_sc *sc = hid_get_drvdata(hdev);
  596. return snprintf(buf, PAGE_SIZE, "0x%04x\n", sc->hw_version);
  597. }
  598. static DEVICE_ATTR(hardware_version, 0444, sony_show_hardware_version, NULL);
  599. static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
  600. unsigned int *rsize)
  601. {
  602. *rsize = sizeof(motion_rdesc);
  603. return motion_rdesc;
  604. }
  605. static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
  606. unsigned int *rsize)
  607. {
  608. *rsize = sizeof(ps3remote_rdesc);
  609. return ps3remote_rdesc;
  610. }
  611. static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
  612. struct hid_field *field, struct hid_usage *usage,
  613. unsigned long **bit, int *max)
  614. {
  615. unsigned int key = usage->hid & HID_USAGE;
  616. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  617. return -1;
  618. switch (usage->collection_index) {
  619. case 1:
  620. if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
  621. return -1;
  622. key = ps3remote_keymap_joypad_buttons[key];
  623. if (!key)
  624. return -1;
  625. break;
  626. case 2:
  627. if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
  628. return -1;
  629. key = ps3remote_keymap_remote_buttons[key];
  630. if (!key)
  631. return -1;
  632. break;
  633. default:
  634. return -1;
  635. }
  636. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  637. return 1;
  638. }
  639. static int navigation_mapping(struct hid_device *hdev, struct hid_input *hi,
  640. struct hid_field *field, struct hid_usage *usage,
  641. unsigned long **bit, int *max)
  642. {
  643. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
  644. unsigned int key = usage->hid & HID_USAGE;
  645. if (key >= ARRAY_SIZE(sixaxis_keymap))
  646. return -1;
  647. key = navigation_keymap[key];
  648. if (!key)
  649. return -1;
  650. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  651. return 1;
  652. } else if (usage->hid == HID_GD_POINTER) {
  653. /* See comment in sixaxis_mapping, basically the L2 (and R2)
  654. * triggers are reported through GD Pointer.
  655. * In addition we ignore any analog button 'axes' and only
  656. * support digital buttons.
  657. */
  658. switch (usage->usage_index) {
  659. case 8: /* L2 */
  660. usage->hid = HID_GD_Z;
  661. break;
  662. default:
  663. return -1;
  664. }
  665. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf);
  666. return 1;
  667. } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
  668. unsigned int abs = usage->hid & HID_USAGE;
  669. if (abs >= ARRAY_SIZE(navigation_absmap))
  670. return -1;
  671. abs = navigation_absmap[abs];
  672. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
  673. return 1;
  674. }
  675. return -1;
  676. }
  677. static int sixaxis_mapping(struct hid_device *hdev, struct hid_input *hi,
  678. struct hid_field *field, struct hid_usage *usage,
  679. unsigned long **bit, int *max)
  680. {
  681. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
  682. unsigned int key = usage->hid & HID_USAGE;
  683. if (key >= ARRAY_SIZE(sixaxis_keymap))
  684. return -1;
  685. key = sixaxis_keymap[key];
  686. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  687. return 1;
  688. } else if (usage->hid == HID_GD_POINTER) {
  689. /* The DS3 provides analog values for most buttons and even
  690. * for HAT axes through GD Pointer. L2 and R2 are reported
  691. * among these as well instead of as GD Z / RZ. Remap L2
  692. * and R2 and ignore other analog 'button axes' as there is
  693. * no good way for reporting them.
  694. */
  695. switch (usage->usage_index) {
  696. case 8: /* L2 */
  697. usage->hid = HID_GD_Z;
  698. break;
  699. case 9: /* R2 */
  700. usage->hid = HID_GD_RZ;
  701. break;
  702. default:
  703. return -1;
  704. }
  705. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf);
  706. return 1;
  707. } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
  708. unsigned int abs = usage->hid & HID_USAGE;
  709. if (abs >= ARRAY_SIZE(sixaxis_absmap))
  710. return -1;
  711. abs = sixaxis_absmap[abs];
  712. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
  713. return 1;
  714. }
  715. return -1;
  716. }
  717. static int ds4_mapping(struct hid_device *hdev, struct hid_input *hi,
  718. struct hid_field *field, struct hid_usage *usage,
  719. unsigned long **bit, int *max)
  720. {
  721. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
  722. unsigned int key = usage->hid & HID_USAGE;
  723. if (key >= ARRAY_SIZE(ds4_keymap))
  724. return -1;
  725. key = ds4_keymap[key];
  726. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  727. return 1;
  728. } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
  729. unsigned int abs = usage->hid & HID_USAGE;
  730. /* Let the HID parser deal with the HAT. */
  731. if (usage->hid == HID_GD_HATSWITCH)
  732. return 0;
  733. if (abs >= ARRAY_SIZE(ds4_absmap))
  734. return -1;
  735. abs = ds4_absmap[abs];
  736. hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
  737. return 1;
  738. }
  739. return 0;
  740. }
  741. static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
  742. unsigned int *rsize)
  743. {
  744. struct sony_sc *sc = hid_get_drvdata(hdev);
  745. if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT))
  746. return rdesc;
  747. /*
  748. * Some Sony RF receivers wrongly declare the mouse pointer as a
  749. * a constant non-data variable.
  750. */
  751. if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
  752. /* usage page: generic desktop controls */
  753. /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
  754. /* usage: mouse */
  755. rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
  756. /* input (usage page for x,y axes): constant, variable, relative */
  757. rdesc[54] == 0x81 && rdesc[55] == 0x07) {
  758. hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
  759. /* input: data, variable, relative */
  760. rdesc[55] = 0x06;
  761. }
  762. if (sc->quirks & MOTION_CONTROLLER)
  763. return motion_fixup(hdev, rdesc, rsize);
  764. if (sc->quirks & PS3REMOTE)
  765. return ps3remote_fixup(hdev, rdesc, rsize);
  766. /*
  767. * Some knock-off USB dongles incorrectly report their button count
  768. * as 13 instead of 16 causing three non-functional buttons.
  769. */
  770. if ((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize >= 45 &&
  771. /* Report Count (13) */
  772. rdesc[23] == 0x95 && rdesc[24] == 0x0D &&
  773. /* Usage Maximum (13) */
  774. rdesc[37] == 0x29 && rdesc[38] == 0x0D &&
  775. /* Report Count (3) */
  776. rdesc[43] == 0x95 && rdesc[44] == 0x03) {
  777. hid_info(hdev, "Fixing up USB dongle report descriptor\n");
  778. rdesc[24] = 0x10;
  779. rdesc[38] = 0x10;
  780. rdesc[44] = 0x00;
  781. }
  782. return rdesc;
  783. }
  784. static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
  785. {
  786. static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
  787. unsigned long flags;
  788. int offset;
  789. u8 cable_state, battery_capacity, battery_charging;
  790. /*
  791. * The sixaxis is charging if the battery value is 0xee
  792. * and it is fully charged if the value is 0xef.
  793. * It does not report the actual level while charging so it
  794. * is set to 100% while charging is in progress.
  795. */
  796. offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
  797. if (rd[offset] >= 0xee) {
  798. battery_capacity = 100;
  799. battery_charging = !(rd[offset] & 0x01);
  800. cable_state = 1;
  801. } else {
  802. u8 index = rd[offset] <= 5 ? rd[offset] : 5;
  803. battery_capacity = sixaxis_battery_capacity[index];
  804. battery_charging = 0;
  805. cable_state = 0;
  806. }
  807. spin_lock_irqsave(&sc->lock, flags);
  808. sc->cable_state = cable_state;
  809. sc->battery_capacity = battery_capacity;
  810. sc->battery_charging = battery_charging;
  811. spin_unlock_irqrestore(&sc->lock, flags);
  812. if (sc->quirks & SIXAXIS_CONTROLLER) {
  813. int val;
  814. offset = SIXAXIS_INPUT_REPORT_ACC_X_OFFSET;
  815. val = ((rd[offset+1] << 8) | rd[offset]) - 511;
  816. input_report_abs(sc->sensor_dev, ABS_X, val);
  817. /* Y and Z are swapped and inversed */
  818. val = 511 - ((rd[offset+5] << 8) | rd[offset+4]);
  819. input_report_abs(sc->sensor_dev, ABS_Y, val);
  820. val = 511 - ((rd[offset+3] << 8) | rd[offset+2]);
  821. input_report_abs(sc->sensor_dev, ABS_Z, val);
  822. input_sync(sc->sensor_dev);
  823. }
  824. }
  825. static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size)
  826. {
  827. struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
  828. struct hid_input, list);
  829. struct input_dev *input_dev = hidinput->input;
  830. unsigned long flags;
  831. int n, m, offset, num_touch_data, max_touch_data;
  832. u8 cable_state, battery_capacity, battery_charging;
  833. u16 timestamp;
  834. /* When using Bluetooth the header is 2 bytes longer, so skip these. */
  835. int data_offset = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 2 : 0;
  836. /* Second bit of third button byte is for the touchpad button. */
  837. offset = data_offset + DS4_INPUT_REPORT_BUTTON_OFFSET;
  838. input_report_key(sc->touchpad, BTN_LEFT, rd[offset+2] & 0x2);
  839. /*
  840. * The default behavior of the Dualshock 4 is to send reports using
  841. * report type 1 when running over Bluetooth. However, when feature
  842. * report 2 is requested during the controller initialization it starts
  843. * sending input reports in report 17. Since report 17 is undefined
  844. * in the default HID descriptor, the HID layer won't generate events.
  845. * While it is possible (and this was done before) to fixup the HID
  846. * descriptor to add this mapping, it was better to do this manually.
  847. * The reason is there were various pieces software both open and closed
  848. * source, relying on the descriptors to be the same across various
  849. * operating systems. If the descriptors wouldn't match some
  850. * applications e.g. games on Wine would not be able to function due
  851. * to different descriptors, which such applications are not parsing.
  852. */
  853. if (rd[0] == 17) {
  854. int value;
  855. offset = data_offset + DS4_INPUT_REPORT_AXIS_OFFSET;
  856. input_report_abs(input_dev, ABS_X, rd[offset]);
  857. input_report_abs(input_dev, ABS_Y, rd[offset+1]);
  858. input_report_abs(input_dev, ABS_RX, rd[offset+2]);
  859. input_report_abs(input_dev, ABS_RY, rd[offset+3]);
  860. value = rd[offset+4] & 0xf;
  861. if (value > 7)
  862. value = 8; /* Center 0, 0 */
  863. input_report_abs(input_dev, ABS_HAT0X, ds4_hat_mapping[value].x);
  864. input_report_abs(input_dev, ABS_HAT0Y, ds4_hat_mapping[value].y);
  865. input_report_key(input_dev, BTN_WEST, rd[offset+4] & 0x10);
  866. input_report_key(input_dev, BTN_SOUTH, rd[offset+4] & 0x20);
  867. input_report_key(input_dev, BTN_EAST, rd[offset+4] & 0x40);
  868. input_report_key(input_dev, BTN_NORTH, rd[offset+4] & 0x80);
  869. input_report_key(input_dev, BTN_TL, rd[offset+5] & 0x1);
  870. input_report_key(input_dev, BTN_TR, rd[offset+5] & 0x2);
  871. input_report_key(input_dev, BTN_TL2, rd[offset+5] & 0x4);
  872. input_report_key(input_dev, BTN_TR2, rd[offset+5] & 0x8);
  873. input_report_key(input_dev, BTN_SELECT, rd[offset+5] & 0x10);
  874. input_report_key(input_dev, BTN_START, rd[offset+5] & 0x20);
  875. input_report_key(input_dev, BTN_THUMBL, rd[offset+5] & 0x40);
  876. input_report_key(input_dev, BTN_THUMBR, rd[offset+5] & 0x80);
  877. input_report_key(input_dev, BTN_MODE, rd[offset+6] & 0x1);
  878. input_report_abs(input_dev, ABS_Z, rd[offset+7]);
  879. input_report_abs(input_dev, ABS_RZ, rd[offset+8]);
  880. input_sync(input_dev);
  881. }
  882. /* Convert timestamp (in 5.33us unit) to timestamp_us */
  883. offset = data_offset + DS4_INPUT_REPORT_TIMESTAMP_OFFSET;
  884. timestamp = get_unaligned_le16(&rd[offset]);
  885. if (!sc->timestamp_initialized) {
  886. sc->timestamp_us = ((unsigned int)timestamp * 16) / 3;
  887. sc->timestamp_initialized = true;
  888. } else {
  889. u16 delta;
  890. if (sc->prev_timestamp > timestamp)
  891. delta = (U16_MAX - sc->prev_timestamp + timestamp + 1);
  892. else
  893. delta = timestamp - sc->prev_timestamp;
  894. sc->timestamp_us += (delta * 16) / 3;
  895. }
  896. sc->prev_timestamp = timestamp;
  897. input_event(sc->sensor_dev, EV_MSC, MSC_TIMESTAMP, sc->timestamp_us);
  898. offset = data_offset + DS4_INPUT_REPORT_GYRO_X_OFFSET;
  899. for (n = 0; n < 6; n++) {
  900. /* Store data in int for more precision during mult_frac. */
  901. int raw_data = (short)((rd[offset+1] << 8) | rd[offset]);
  902. struct ds4_calibration_data *calib = &sc->ds4_calib_data[n];
  903. /* High precision is needed during calibration, but the
  904. * calibrated values are within 32-bit.
  905. * Note: we swap numerator 'x' and 'numer' in mult_frac for
  906. * precision reasons so we don't need 64-bit.
  907. */
  908. int calib_data = mult_frac(calib->sens_numer,
  909. raw_data - calib->bias,
  910. calib->sens_denom);
  911. input_report_abs(sc->sensor_dev, calib->abs_code, calib_data);
  912. offset += 2;
  913. }
  914. input_sync(sc->sensor_dev);
  915. /*
  916. * The lower 4 bits of byte 30 (or 32 for BT) contain the battery level
  917. * and the 5th bit contains the USB cable state.
  918. */
  919. offset = data_offset + DS4_INPUT_REPORT_BATTERY_OFFSET;
  920. cable_state = (rd[offset] >> 4) & 0x01;
  921. battery_capacity = rd[offset] & 0x0F;
  922. /*
  923. * When a USB power source is connected the battery level ranges from
  924. * 0 to 10, and when running on battery power it ranges from 0 to 9.
  925. * A battery level above 10 when plugged in means charge completed.
  926. */
  927. if (!cable_state || battery_capacity > 10)
  928. battery_charging = 0;
  929. else
  930. battery_charging = 1;
  931. if (!cable_state)
  932. battery_capacity++;
  933. if (battery_capacity > 10)
  934. battery_capacity = 10;
  935. battery_capacity *= 10;
  936. spin_lock_irqsave(&sc->lock, flags);
  937. sc->cable_state = cable_state;
  938. sc->battery_capacity = battery_capacity;
  939. sc->battery_charging = battery_charging;
  940. spin_unlock_irqrestore(&sc->lock, flags);
  941. /*
  942. * The Dualshock 4 multi-touch trackpad data starts at offset 33 on USB
  943. * and 35 on Bluetooth.
  944. * The first byte indicates the number of touch data in the report.
  945. * Trackpad data starts 2 bytes later (e.g. 35 for USB).
  946. */
  947. offset = data_offset + DS4_INPUT_REPORT_TOUCHPAD_OFFSET;
  948. max_touch_data = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 4 : 3;
  949. if (rd[offset] > 0 && rd[offset] <= max_touch_data)
  950. num_touch_data = rd[offset];
  951. else
  952. num_touch_data = 1;
  953. offset += 1;
  954. for (m = 0; m < num_touch_data; m++) {
  955. /* Skip past timestamp */
  956. offset += 1;
  957. /*
  958. * The first 7 bits of the first byte is a counter and bit 8 is
  959. * a touch indicator that is 0 when pressed and 1 when not
  960. * pressed.
  961. * The next 3 bytes are two 12 bit touch coordinates, X and Y.
  962. * The data for the second touch is in the same format and
  963. * immediately follows the data for the first.
  964. */
  965. for (n = 0; n < 2; n++) {
  966. u16 x, y;
  967. bool active;
  968. x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
  969. y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
  970. active = !(rd[offset] >> 7);
  971. input_mt_slot(sc->touchpad, n);
  972. input_mt_report_slot_state(sc->touchpad, MT_TOOL_FINGER, active);
  973. if (active) {
  974. input_report_abs(sc->touchpad, ABS_MT_POSITION_X, x);
  975. input_report_abs(sc->touchpad, ABS_MT_POSITION_Y, y);
  976. }
  977. offset += 4;
  978. }
  979. input_mt_sync_frame(sc->touchpad);
  980. input_sync(sc->touchpad);
  981. }
  982. }
  983. static void nsg_mrxu_parse_report(struct sony_sc *sc, u8 *rd, int size)
  984. {
  985. int n, offset, relx, rely;
  986. u8 active;
  987. /*
  988. * The NSG-MRxU multi-touch trackpad data starts at offset 1 and
  989. * the touch-related data starts at offset 2.
  990. * For the first byte, bit 0 is set when touchpad button is pressed.
  991. * Bit 2 is set when a touch is active and the drag (Fn) key is pressed.
  992. * This drag key is mapped to BTN_LEFT. It is operational only when a
  993. * touch point is active.
  994. * Bit 4 is set when only the first touch point is active.
  995. * Bit 6 is set when only the second touch point is active.
  996. * Bits 5 and 7 are set when both touch points are active.
  997. * The next 3 bytes are two 12 bit X/Y coordinates for the first touch.
  998. * The following byte, offset 5, has the touch width and length.
  999. * Bits 0-4=X (width), bits 5-7=Y (length).
  1000. * A signed relative X coordinate is at offset 6.
  1001. * The bytes at offset 7-9 are the second touch X/Y coordinates.
  1002. * Offset 10 has the second touch width and length.
  1003. * Offset 11 has the relative Y coordinate.
  1004. */
  1005. offset = 1;
  1006. input_report_key(sc->touchpad, BTN_LEFT, rd[offset] & 0x0F);
  1007. active = (rd[offset] >> 4);
  1008. relx = (s8) rd[offset+5];
  1009. rely = ((s8) rd[offset+10]) * -1;
  1010. offset++;
  1011. for (n = 0; n < 2; n++) {
  1012. u16 x, y;
  1013. u8 contactx, contacty;
  1014. x = rd[offset] | ((rd[offset+1] & 0x0F) << 8);
  1015. y = ((rd[offset+1] & 0xF0) >> 4) | (rd[offset+2] << 4);
  1016. input_mt_slot(sc->touchpad, n);
  1017. input_mt_report_slot_state(sc->touchpad, MT_TOOL_FINGER, active & 0x03);
  1018. if (active & 0x03) {
  1019. contactx = rd[offset+3] & 0x0F;
  1020. contacty = rd[offset+3] >> 4;
  1021. input_report_abs(sc->touchpad, ABS_MT_TOUCH_MAJOR,
  1022. max(contactx, contacty));
  1023. input_report_abs(sc->touchpad, ABS_MT_TOUCH_MINOR,
  1024. min(contactx, contacty));
  1025. input_report_abs(sc->touchpad, ABS_MT_ORIENTATION,
  1026. (bool) (contactx > contacty));
  1027. input_report_abs(sc->touchpad, ABS_MT_POSITION_X, x);
  1028. input_report_abs(sc->touchpad, ABS_MT_POSITION_Y,
  1029. NSG_MRXU_MAX_Y - y);
  1030. /*
  1031. * The relative coordinates belong to the first touch
  1032. * point, when present, or to the second touch point
  1033. * when the first is not active.
  1034. */
  1035. if ((n == 0) || ((n == 1) && (active & 0x01))) {
  1036. input_report_rel(sc->touchpad, REL_X, relx);
  1037. input_report_rel(sc->touchpad, REL_Y, rely);
  1038. }
  1039. }
  1040. offset += 5;
  1041. active >>= 2;
  1042. }
  1043. input_mt_sync_frame(sc->touchpad);
  1044. input_sync(sc->touchpad);
  1045. }
  1046. static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
  1047. u8 *rd, int size)
  1048. {
  1049. struct sony_sc *sc = hid_get_drvdata(hdev);
  1050. /*
  1051. * Sixaxis HID report has acclerometers/gyro with MSByte first, this
  1052. * has to be BYTE_SWAPPED before passing up to joystick interface
  1053. */
  1054. if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
  1055. /*
  1056. * When connected via Bluetooth the Sixaxis occasionally sends
  1057. * a report with the second byte 0xff and the rest zeroed.
  1058. *
  1059. * This report does not reflect the actual state of the
  1060. * controller must be ignored to avoid generating false input
  1061. * events.
  1062. */
  1063. if (rd[1] == 0xff)
  1064. return -EINVAL;
  1065. swap(rd[41], rd[42]);
  1066. swap(rd[43], rd[44]);
  1067. swap(rd[45], rd[46]);
  1068. swap(rd[47], rd[48]);
  1069. sixaxis_parse_report(sc, rd, size);
  1070. } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
  1071. sixaxis_parse_report(sc, rd, size);
  1072. } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
  1073. size == 49) {
  1074. sixaxis_parse_report(sc, rd, size);
  1075. } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
  1076. size == 64) {
  1077. dualshock4_parse_report(sc, rd, size);
  1078. } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && rd[0] == 0x11 &&
  1079. size == 78)) {
  1080. /* CRC check */
  1081. u8 bthdr = 0xA1;
  1082. u32 crc;
  1083. u32 report_crc;
  1084. crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
  1085. crc = ~crc32_le(crc, rd, DS4_INPUT_REPORT_0x11_SIZE-4);
  1086. report_crc = get_unaligned_le32(&rd[DS4_INPUT_REPORT_0x11_SIZE-4]);
  1087. if (crc != report_crc) {
  1088. hid_dbg(sc->hdev, "DualShock 4 input report's CRC check failed, received crc 0x%0x != 0x%0x\n",
  1089. report_crc, crc);
  1090. return -EILSEQ;
  1091. }
  1092. dualshock4_parse_report(sc, rd, size);
  1093. } else if ((sc->quirks & DUALSHOCK4_DONGLE) && rd[0] == 0x01 &&
  1094. size == 64) {
  1095. unsigned long flags;
  1096. enum ds4_dongle_state dongle_state;
  1097. /*
  1098. * In the case of a DS4 USB dongle, bit[2] of byte 31 indicates
  1099. * if a DS4 is actually connected (indicated by '0').
  1100. * For non-dongle, this bit is always 0 (connected).
  1101. */
  1102. bool connected = (rd[31] & 0x04) ? false : true;
  1103. spin_lock_irqsave(&sc->lock, flags);
  1104. dongle_state = sc->ds4_dongle_state;
  1105. spin_unlock_irqrestore(&sc->lock, flags);
  1106. /*
  1107. * The dongle always sends input reports even when no
  1108. * DS4 is attached. When a DS4 is connected, we need to
  1109. * obtain calibration data before we can use it.
  1110. * The code below tracks dongle state and kicks of
  1111. * calibration when needed and only allows us to process
  1112. * input if a DS4 is actually connected.
  1113. */
  1114. if (dongle_state == DONGLE_DISCONNECTED && connected) {
  1115. hid_info(sc->hdev, "DualShock 4 USB dongle: controller connected\n");
  1116. sony_set_leds(sc);
  1117. spin_lock_irqsave(&sc->lock, flags);
  1118. sc->ds4_dongle_state = DONGLE_CALIBRATING;
  1119. spin_unlock_irqrestore(&sc->lock, flags);
  1120. sony_schedule_work(sc, SONY_WORKER_HOTPLUG);
  1121. /* Don't process the report since we don't have
  1122. * calibration data, but let hidraw have it anyway.
  1123. */
  1124. return 0;
  1125. } else if ((dongle_state == DONGLE_CONNECTED ||
  1126. dongle_state == DONGLE_DISABLED) && !connected) {
  1127. hid_info(sc->hdev, "DualShock 4 USB dongle: controller disconnected\n");
  1128. spin_lock_irqsave(&sc->lock, flags);
  1129. sc->ds4_dongle_state = DONGLE_DISCONNECTED;
  1130. spin_unlock_irqrestore(&sc->lock, flags);
  1131. /* Return 0, so hidraw can get the report. */
  1132. return 0;
  1133. } else if (dongle_state == DONGLE_CALIBRATING ||
  1134. dongle_state == DONGLE_DISABLED ||
  1135. dongle_state == DONGLE_DISCONNECTED) {
  1136. /* Return 0, so hidraw can get the report. */
  1137. return 0;
  1138. }
  1139. dualshock4_parse_report(sc, rd, size);
  1140. } else if ((sc->quirks & NSG_MRXU_REMOTE) && rd[0] == 0x02) {
  1141. nsg_mrxu_parse_report(sc, rd, size);
  1142. return 1;
  1143. }
  1144. if (sc->defer_initialization) {
  1145. sc->defer_initialization = 0;
  1146. sony_schedule_work(sc, SONY_WORKER_STATE);
  1147. }
  1148. return 0;
  1149. }
  1150. static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
  1151. struct hid_field *field, struct hid_usage *usage,
  1152. unsigned long **bit, int *max)
  1153. {
  1154. struct sony_sc *sc = hid_get_drvdata(hdev);
  1155. if (sc->quirks & BUZZ_CONTROLLER) {
  1156. unsigned int key = usage->hid & HID_USAGE;
  1157. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  1158. return -1;
  1159. switch (usage->collection_index) {
  1160. case 1:
  1161. if (key >= ARRAY_SIZE(buzz_keymap))
  1162. return -1;
  1163. key = buzz_keymap[key];
  1164. if (!key)
  1165. return -1;
  1166. break;
  1167. default:
  1168. return -1;
  1169. }
  1170. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
  1171. return 1;
  1172. }
  1173. if (sc->quirks & PS3REMOTE)
  1174. return ps3remote_mapping(hdev, hi, field, usage, bit, max);
  1175. if (sc->quirks & NAVIGATION_CONTROLLER)
  1176. return navigation_mapping(hdev, hi, field, usage, bit, max);
  1177. if (sc->quirks & SIXAXIS_CONTROLLER)
  1178. return sixaxis_mapping(hdev, hi, field, usage, bit, max);
  1179. if (sc->quirks & DUALSHOCK4_CONTROLLER)
  1180. return ds4_mapping(hdev, hi, field, usage, bit, max);
  1181. /* Let hid-core decide for the others */
  1182. return 0;
  1183. }
  1184. static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
  1185. int w, int h, int touch_major, int touch_minor, int orientation)
  1186. {
  1187. size_t name_sz;
  1188. char *name;
  1189. int ret;
  1190. sc->touchpad = devm_input_allocate_device(&sc->hdev->dev);
  1191. if (!sc->touchpad)
  1192. return -ENOMEM;
  1193. input_set_drvdata(sc->touchpad, sc);
  1194. sc->touchpad->dev.parent = &sc->hdev->dev;
  1195. sc->touchpad->phys = sc->hdev->phys;
  1196. sc->touchpad->uniq = sc->hdev->uniq;
  1197. sc->touchpad->id.bustype = sc->hdev->bus;
  1198. sc->touchpad->id.vendor = sc->hdev->vendor;
  1199. sc->touchpad->id.product = sc->hdev->product;
  1200. sc->touchpad->id.version = sc->hdev->version;
  1201. /* Append a suffix to the controller name as there are various
  1202. * DS4 compatible non-Sony devices with different names.
  1203. */
  1204. name_sz = strlen(sc->hdev->name) + sizeof(DS4_TOUCHPAD_SUFFIX);
  1205. name = devm_kzalloc(&sc->hdev->dev, name_sz, GFP_KERNEL);
  1206. if (!name)
  1207. return -ENOMEM;
  1208. snprintf(name, name_sz, "%s" DS4_TOUCHPAD_SUFFIX, sc->hdev->name);
  1209. sc->touchpad->name = name;
  1210. /* We map the button underneath the touchpad to BTN_LEFT. */
  1211. __set_bit(EV_KEY, sc->touchpad->evbit);
  1212. __set_bit(BTN_LEFT, sc->touchpad->keybit);
  1213. __set_bit(INPUT_PROP_BUTTONPAD, sc->touchpad->propbit);
  1214. input_set_abs_params(sc->touchpad, ABS_MT_POSITION_X, 0, w, 0, 0);
  1215. input_set_abs_params(sc->touchpad, ABS_MT_POSITION_Y, 0, h, 0, 0);
  1216. if (touch_major > 0) {
  1217. input_set_abs_params(sc->touchpad, ABS_MT_TOUCH_MAJOR,
  1218. 0, touch_major, 0, 0);
  1219. if (touch_minor > 0)
  1220. input_set_abs_params(sc->touchpad, ABS_MT_TOUCH_MINOR,
  1221. 0, touch_minor, 0, 0);
  1222. if (orientation > 0)
  1223. input_set_abs_params(sc->touchpad, ABS_MT_ORIENTATION,
  1224. 0, orientation, 0, 0);
  1225. }
  1226. if (sc->quirks & NSG_MRXU_REMOTE) {
  1227. __set_bit(EV_REL, sc->touchpad->evbit);
  1228. }
  1229. ret = input_mt_init_slots(sc->touchpad, touch_count, INPUT_MT_POINTER);
  1230. if (ret < 0)
  1231. return ret;
  1232. ret = input_register_device(sc->touchpad);
  1233. if (ret < 0)
  1234. return ret;
  1235. return 0;
  1236. }
  1237. static int sony_register_sensors(struct sony_sc *sc)
  1238. {
  1239. size_t name_sz;
  1240. char *name;
  1241. int ret;
  1242. int range;
  1243. sc->sensor_dev = devm_input_allocate_device(&sc->hdev->dev);
  1244. if (!sc->sensor_dev)
  1245. return -ENOMEM;
  1246. input_set_drvdata(sc->sensor_dev, sc);
  1247. sc->sensor_dev->dev.parent = &sc->hdev->dev;
  1248. sc->sensor_dev->phys = sc->hdev->phys;
  1249. sc->sensor_dev->uniq = sc->hdev->uniq;
  1250. sc->sensor_dev->id.bustype = sc->hdev->bus;
  1251. sc->sensor_dev->id.vendor = sc->hdev->vendor;
  1252. sc->sensor_dev->id.product = sc->hdev->product;
  1253. sc->sensor_dev->id.version = sc->hdev->version;
  1254. /* Append a suffix to the controller name as there are various
  1255. * DS4 compatible non-Sony devices with different names.
  1256. */
  1257. name_sz = strlen(sc->hdev->name) + sizeof(SENSOR_SUFFIX);
  1258. name = devm_kzalloc(&sc->hdev->dev, name_sz, GFP_KERNEL);
  1259. if (!name)
  1260. return -ENOMEM;
  1261. snprintf(name, name_sz, "%s" SENSOR_SUFFIX, sc->hdev->name);
  1262. sc->sensor_dev->name = name;
  1263. if (sc->quirks & SIXAXIS_CONTROLLER) {
  1264. /* For the DS3 we only support the accelerometer, which works
  1265. * quite well even without calibration. The device also has
  1266. * a 1-axis gyro, but it is very difficult to manage from within
  1267. * the driver even to get data, the sensor is inaccurate and
  1268. * the behavior is very different between hardware revisions.
  1269. */
  1270. input_set_abs_params(sc->sensor_dev, ABS_X, -512, 511, 4, 0);
  1271. input_set_abs_params(sc->sensor_dev, ABS_Y, -512, 511, 4, 0);
  1272. input_set_abs_params(sc->sensor_dev, ABS_Z, -512, 511, 4, 0);
  1273. input_abs_set_res(sc->sensor_dev, ABS_X, SIXAXIS_ACC_RES_PER_G);
  1274. input_abs_set_res(sc->sensor_dev, ABS_Y, SIXAXIS_ACC_RES_PER_G);
  1275. input_abs_set_res(sc->sensor_dev, ABS_Z, SIXAXIS_ACC_RES_PER_G);
  1276. } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
  1277. range = DS4_ACC_RES_PER_G*4;
  1278. input_set_abs_params(sc->sensor_dev, ABS_X, -range, range, 16, 0);
  1279. input_set_abs_params(sc->sensor_dev, ABS_Y, -range, range, 16, 0);
  1280. input_set_abs_params(sc->sensor_dev, ABS_Z, -range, range, 16, 0);
  1281. input_abs_set_res(sc->sensor_dev, ABS_X, DS4_ACC_RES_PER_G);
  1282. input_abs_set_res(sc->sensor_dev, ABS_Y, DS4_ACC_RES_PER_G);
  1283. input_abs_set_res(sc->sensor_dev, ABS_Z, DS4_ACC_RES_PER_G);
  1284. range = DS4_GYRO_RES_PER_DEG_S*2048;
  1285. input_set_abs_params(sc->sensor_dev, ABS_RX, -range, range, 16, 0);
  1286. input_set_abs_params(sc->sensor_dev, ABS_RY, -range, range, 16, 0);
  1287. input_set_abs_params(sc->sensor_dev, ABS_RZ, -range, range, 16, 0);
  1288. input_abs_set_res(sc->sensor_dev, ABS_RX, DS4_GYRO_RES_PER_DEG_S);
  1289. input_abs_set_res(sc->sensor_dev, ABS_RY, DS4_GYRO_RES_PER_DEG_S);
  1290. input_abs_set_res(sc->sensor_dev, ABS_RZ, DS4_GYRO_RES_PER_DEG_S);
  1291. __set_bit(EV_MSC, sc->sensor_dev->evbit);
  1292. __set_bit(MSC_TIMESTAMP, sc->sensor_dev->mscbit);
  1293. }
  1294. __set_bit(INPUT_PROP_ACCELEROMETER, sc->sensor_dev->propbit);
  1295. ret = input_register_device(sc->sensor_dev);
  1296. if (ret < 0)
  1297. return ret;
  1298. return 0;
  1299. }
  1300. /*
  1301. * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
  1302. * to "operational". Without this, the ps3 controller will not report any
  1303. * events.
  1304. */
  1305. static int sixaxis_set_operational_usb(struct hid_device *hdev)
  1306. {
  1307. const int buf_size =
  1308. max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
  1309. u8 *buf;
  1310. int ret;
  1311. buf = kmalloc(buf_size, GFP_KERNEL);
  1312. if (!buf)
  1313. return -ENOMEM;
  1314. ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
  1315. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  1316. if (ret < 0) {
  1317. hid_err(hdev, "can't set operational mode: step 1\n");
  1318. goto out;
  1319. }
  1320. /*
  1321. * Some compatible controllers like the Speedlink Strike FX and
  1322. * Gasia need another query plus an USB interrupt to get operational.
  1323. */
  1324. ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
  1325. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  1326. if (ret < 0) {
  1327. hid_err(hdev, "can't set operational mode: step 2\n");
  1328. goto out;
  1329. }
  1330. /*
  1331. * But the USB interrupt would cause SHANWAN controllers to
  1332. * start rumbling non-stop.
  1333. */
  1334. if (strcmp(hdev->name, "SHANWAN PS3 GamePad")) {
  1335. ret = hid_hw_output_report(hdev, buf, 1);
  1336. if (ret < 0) {
  1337. hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
  1338. ret = 0;
  1339. }
  1340. }
  1341. out:
  1342. kfree(buf);
  1343. return ret;
  1344. }
  1345. static int sixaxis_set_operational_bt(struct hid_device *hdev)
  1346. {
  1347. static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
  1348. u8 *buf;
  1349. int ret;
  1350. buf = kmemdup(report, sizeof(report), GFP_KERNEL);
  1351. if (!buf)
  1352. return -ENOMEM;
  1353. ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
  1354. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  1355. kfree(buf);
  1356. return ret;
  1357. }
  1358. /*
  1359. * Request DS4 calibration data for the motion sensors.
  1360. * For Bluetooth this also affects the operating mode (see below).
  1361. */
  1362. static int dualshock4_get_calibration_data(struct sony_sc *sc)
  1363. {
  1364. u8 *buf;
  1365. int ret;
  1366. short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
  1367. short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
  1368. short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
  1369. short gyro_speed_plus, gyro_speed_minus;
  1370. short acc_x_plus, acc_x_minus;
  1371. short acc_y_plus, acc_y_minus;
  1372. short acc_z_plus, acc_z_minus;
  1373. int speed_2x;
  1374. int range_2g;
  1375. /* For Bluetooth we use a different request, which supports CRC.
  1376. * Note: in Bluetooth mode feature report 0x02 also changes the state
  1377. * of the controller, so that it sends input reports of type 0x11.
  1378. */
  1379. if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
  1380. buf = kmalloc(DS4_FEATURE_REPORT_0x02_SIZE, GFP_KERNEL);
  1381. if (!buf)
  1382. return -ENOMEM;
  1383. ret = hid_hw_raw_request(sc->hdev, 0x02, buf,
  1384. DS4_FEATURE_REPORT_0x02_SIZE,
  1385. HID_FEATURE_REPORT,
  1386. HID_REQ_GET_REPORT);
  1387. if (ret < 0)
  1388. goto err_stop;
  1389. } else {
  1390. u8 bthdr = 0xA3;
  1391. u32 crc;
  1392. u32 report_crc;
  1393. int retries;
  1394. buf = kmalloc(DS4_FEATURE_REPORT_0x05_SIZE, GFP_KERNEL);
  1395. if (!buf)
  1396. return -ENOMEM;
  1397. for (retries = 0; retries < 3; retries++) {
  1398. ret = hid_hw_raw_request(sc->hdev, 0x05, buf,
  1399. DS4_FEATURE_REPORT_0x05_SIZE,
  1400. HID_FEATURE_REPORT,
  1401. HID_REQ_GET_REPORT);
  1402. if (ret < 0)
  1403. goto err_stop;
  1404. /* CRC check */
  1405. crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
  1406. crc = ~crc32_le(crc, buf, DS4_FEATURE_REPORT_0x05_SIZE-4);
  1407. report_crc = get_unaligned_le32(&buf[DS4_FEATURE_REPORT_0x05_SIZE-4]);
  1408. if (crc != report_crc) {
  1409. hid_warn(sc->hdev, "DualShock 4 calibration report's CRC check failed, received crc 0x%0x != 0x%0x\n",
  1410. report_crc, crc);
  1411. if (retries < 2) {
  1412. hid_warn(sc->hdev, "Retrying DualShock 4 get calibration report request\n");
  1413. continue;
  1414. } else {
  1415. ret = -EILSEQ;
  1416. goto err_stop;
  1417. }
  1418. } else {
  1419. break;
  1420. }
  1421. }
  1422. }
  1423. gyro_pitch_bias = get_unaligned_le16(&buf[1]);
  1424. gyro_yaw_bias = get_unaligned_le16(&buf[3]);
  1425. gyro_roll_bias = get_unaligned_le16(&buf[5]);
  1426. if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
  1427. gyro_pitch_plus = get_unaligned_le16(&buf[7]);
  1428. gyro_pitch_minus = get_unaligned_le16(&buf[9]);
  1429. gyro_yaw_plus = get_unaligned_le16(&buf[11]);
  1430. gyro_yaw_minus = get_unaligned_le16(&buf[13]);
  1431. gyro_roll_plus = get_unaligned_le16(&buf[15]);
  1432. gyro_roll_minus = get_unaligned_le16(&buf[17]);
  1433. } else {
  1434. /* BT + Dongle */
  1435. gyro_pitch_plus = get_unaligned_le16(&buf[7]);
  1436. gyro_yaw_plus = get_unaligned_le16(&buf[9]);
  1437. gyro_roll_plus = get_unaligned_le16(&buf[11]);
  1438. gyro_pitch_minus = get_unaligned_le16(&buf[13]);
  1439. gyro_yaw_minus = get_unaligned_le16(&buf[15]);
  1440. gyro_roll_minus = get_unaligned_le16(&buf[17]);
  1441. }
  1442. gyro_speed_plus = get_unaligned_le16(&buf[19]);
  1443. gyro_speed_minus = get_unaligned_le16(&buf[21]);
  1444. acc_x_plus = get_unaligned_le16(&buf[23]);
  1445. acc_x_minus = get_unaligned_le16(&buf[25]);
  1446. acc_y_plus = get_unaligned_le16(&buf[27]);
  1447. acc_y_minus = get_unaligned_le16(&buf[29]);
  1448. acc_z_plus = get_unaligned_le16(&buf[31]);
  1449. acc_z_minus = get_unaligned_le16(&buf[33]);
  1450. /* Set gyroscope calibration and normalization parameters.
  1451. * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s.
  1452. */
  1453. speed_2x = (gyro_speed_plus + gyro_speed_minus);
  1454. sc->ds4_calib_data[0].abs_code = ABS_RX;
  1455. sc->ds4_calib_data[0].bias = gyro_pitch_bias;
  1456. sc->ds4_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
  1457. sc->ds4_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus;
  1458. sc->ds4_calib_data[1].abs_code = ABS_RY;
  1459. sc->ds4_calib_data[1].bias = gyro_yaw_bias;
  1460. sc->ds4_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
  1461. sc->ds4_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus;
  1462. sc->ds4_calib_data[2].abs_code = ABS_RZ;
  1463. sc->ds4_calib_data[2].bias = gyro_roll_bias;
  1464. sc->ds4_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
  1465. sc->ds4_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus;
  1466. /* Set accelerometer calibration and normalization parameters.
  1467. * Data values will be normalized to 1/DS4_ACC_RES_PER_G G.
  1468. */
  1469. range_2g = acc_x_plus - acc_x_minus;
  1470. sc->ds4_calib_data[3].abs_code = ABS_X;
  1471. sc->ds4_calib_data[3].bias = acc_x_plus - range_2g / 2;
  1472. sc->ds4_calib_data[3].sens_numer = 2*DS4_ACC_RES_PER_G;
  1473. sc->ds4_calib_data[3].sens_denom = range_2g;
  1474. range_2g = acc_y_plus - acc_y_minus;
  1475. sc->ds4_calib_data[4].abs_code = ABS_Y;
  1476. sc->ds4_calib_data[4].bias = acc_y_plus - range_2g / 2;
  1477. sc->ds4_calib_data[4].sens_numer = 2*DS4_ACC_RES_PER_G;
  1478. sc->ds4_calib_data[4].sens_denom = range_2g;
  1479. range_2g = acc_z_plus - acc_z_minus;
  1480. sc->ds4_calib_data[5].abs_code = ABS_Z;
  1481. sc->ds4_calib_data[5].bias = acc_z_plus - range_2g / 2;
  1482. sc->ds4_calib_data[5].sens_numer = 2*DS4_ACC_RES_PER_G;
  1483. sc->ds4_calib_data[5].sens_denom = range_2g;
  1484. err_stop:
  1485. kfree(buf);
  1486. return ret;
  1487. }
  1488. static void dualshock4_calibration_work(struct work_struct *work)
  1489. {
  1490. struct sony_sc *sc = container_of(work, struct sony_sc, hotplug_worker);
  1491. unsigned long flags;
  1492. enum ds4_dongle_state dongle_state;
  1493. int ret;
  1494. ret = dualshock4_get_calibration_data(sc);
  1495. if (ret < 0) {
  1496. /* This call is very unlikely to fail for the dongle. When it
  1497. * fails we are probably in a very bad state, so mark the
  1498. * dongle as disabled. We will re-enable the dongle if a new
  1499. * DS4 hotplug is detect from sony_raw_event as any issues
  1500. * are likely resolved then (the dongle is quite stupid).
  1501. */
  1502. hid_err(sc->hdev, "DualShock 4 USB dongle: calibration failed, disabling device\n");
  1503. dongle_state = DONGLE_DISABLED;
  1504. } else {
  1505. hid_info(sc->hdev, "DualShock 4 USB dongle: calibration completed\n");
  1506. dongle_state = DONGLE_CONNECTED;
  1507. }
  1508. spin_lock_irqsave(&sc->lock, flags);
  1509. sc->ds4_dongle_state = dongle_state;
  1510. spin_unlock_irqrestore(&sc->lock, flags);
  1511. }
  1512. static int dualshock4_get_version_info(struct sony_sc *sc)
  1513. {
  1514. u8 *buf;
  1515. int ret;
  1516. buf = kmalloc(DS4_FEATURE_REPORT_0xA3_SIZE, GFP_KERNEL);
  1517. if (!buf)
  1518. return -ENOMEM;
  1519. ret = hid_hw_raw_request(sc->hdev, 0xA3, buf,
  1520. DS4_FEATURE_REPORT_0xA3_SIZE,
  1521. HID_FEATURE_REPORT,
  1522. HID_REQ_GET_REPORT);
  1523. if (ret < 0) {
  1524. kfree(buf);
  1525. return ret;
  1526. }
  1527. sc->hw_version = get_unaligned_le16(&buf[35]);
  1528. sc->fw_version = get_unaligned_le16(&buf[41]);
  1529. kfree(buf);
  1530. return 0;
  1531. }
  1532. static void sixaxis_set_leds_from_id(struct sony_sc *sc)
  1533. {
  1534. static const u8 sixaxis_leds[10][4] = {
  1535. { 0x01, 0x00, 0x00, 0x00 },
  1536. { 0x00, 0x01, 0x00, 0x00 },
  1537. { 0x00, 0x00, 0x01, 0x00 },
  1538. { 0x00, 0x00, 0x00, 0x01 },
  1539. { 0x01, 0x00, 0x00, 0x01 },
  1540. { 0x00, 0x01, 0x00, 0x01 },
  1541. { 0x00, 0x00, 0x01, 0x01 },
  1542. { 0x01, 0x00, 0x01, 0x01 },
  1543. { 0x00, 0x01, 0x01, 0x01 },
  1544. { 0x01, 0x01, 0x01, 0x01 }
  1545. };
  1546. int id = sc->device_id;
  1547. BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
  1548. if (id < 0)
  1549. return;
  1550. id %= 10;
  1551. memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
  1552. }
  1553. static void dualshock4_set_leds_from_id(struct sony_sc *sc)
  1554. {
  1555. /* The first 4 color/index entries match what the PS4 assigns */
  1556. static const u8 color_code[7][3] = {
  1557. /* Blue */ { 0x00, 0x00, 0x40 },
  1558. /* Red */ { 0x40, 0x00, 0x00 },
  1559. /* Green */ { 0x00, 0x40, 0x00 },
  1560. /* Pink */ { 0x20, 0x00, 0x20 },
  1561. /* Orange */ { 0x02, 0x01, 0x00 },
  1562. /* Teal */ { 0x00, 0x01, 0x01 },
  1563. /* White */ { 0x01, 0x01, 0x01 }
  1564. };
  1565. int id = sc->device_id;
  1566. BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
  1567. if (id < 0)
  1568. return;
  1569. id %= 7;
  1570. memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
  1571. }
  1572. static void buzz_set_leds(struct sony_sc *sc)
  1573. {
  1574. struct hid_device *hdev = sc->hdev;
  1575. struct list_head *report_list =
  1576. &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
  1577. struct hid_report *report = list_entry(report_list->next,
  1578. struct hid_report, list);
  1579. s32 *value = report->field[0]->value;
  1580. BUILD_BUG_ON(MAX_LEDS < 4);
  1581. value[0] = 0x00;
  1582. value[1] = sc->led_state[0] ? 0xff : 0x00;
  1583. value[2] = sc->led_state[1] ? 0xff : 0x00;
  1584. value[3] = sc->led_state[2] ? 0xff : 0x00;
  1585. value[4] = sc->led_state[3] ? 0xff : 0x00;
  1586. value[5] = 0x00;
  1587. value[6] = 0x00;
  1588. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  1589. }
  1590. static void sony_set_leds(struct sony_sc *sc)
  1591. {
  1592. if (!(sc->quirks & BUZZ_CONTROLLER))
  1593. sony_schedule_work(sc, SONY_WORKER_STATE);
  1594. else
  1595. buzz_set_leds(sc);
  1596. }
  1597. static void sony_led_set_brightness(struct led_classdev *led,
  1598. enum led_brightness value)
  1599. {
  1600. struct device *dev = led->dev->parent;
  1601. struct hid_device *hdev = to_hid_device(dev);
  1602. struct sony_sc *drv_data;
  1603. int n;
  1604. int force_update;
  1605. drv_data = hid_get_drvdata(hdev);
  1606. if (!drv_data) {
  1607. hid_err(hdev, "No device data\n");
  1608. return;
  1609. }
  1610. /*
  1611. * The Sixaxis on USB will override any LED settings sent to it
  1612. * and keep flashing all of the LEDs until the PS button is pressed.
  1613. * Updates, even if redundant, must be always be sent to the
  1614. * controller to avoid having to toggle the state of an LED just to
  1615. * stop the flashing later on.
  1616. */
  1617. force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
  1618. for (n = 0; n < drv_data->led_count; n++) {
  1619. if (led == drv_data->leds[n] && (force_update ||
  1620. (value != drv_data->led_state[n] ||
  1621. drv_data->led_delay_on[n] ||
  1622. drv_data->led_delay_off[n]))) {
  1623. drv_data->led_state[n] = value;
  1624. /* Setting the brightness stops the blinking */
  1625. drv_data->led_delay_on[n] = 0;
  1626. drv_data->led_delay_off[n] = 0;
  1627. sony_set_leds(drv_data);
  1628. break;
  1629. }
  1630. }
  1631. }
  1632. static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
  1633. {
  1634. struct device *dev = led->dev->parent;
  1635. struct hid_device *hdev = to_hid_device(dev);
  1636. struct sony_sc *drv_data;
  1637. int n;
  1638. drv_data = hid_get_drvdata(hdev);
  1639. if (!drv_data) {
  1640. hid_err(hdev, "No device data\n");
  1641. return LED_OFF;
  1642. }
  1643. for (n = 0; n < drv_data->led_count; n++) {
  1644. if (led == drv_data->leds[n])
  1645. return drv_data->led_state[n];
  1646. }
  1647. return LED_OFF;
  1648. }
  1649. static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
  1650. unsigned long *delay_off)
  1651. {
  1652. struct device *dev = led->dev->parent;
  1653. struct hid_device *hdev = to_hid_device(dev);
  1654. struct sony_sc *drv_data = hid_get_drvdata(hdev);
  1655. int n;
  1656. u8 new_on, new_off;
  1657. if (!drv_data) {
  1658. hid_err(hdev, "No device data\n");
  1659. return -EINVAL;
  1660. }
  1661. /* Max delay is 255 deciseconds or 2550 milliseconds */
  1662. if (*delay_on > 2550)
  1663. *delay_on = 2550;
  1664. if (*delay_off > 2550)
  1665. *delay_off = 2550;
  1666. /* Blink at 1 Hz if both values are zero */
  1667. if (!*delay_on && !*delay_off)
  1668. *delay_on = *delay_off = 500;
  1669. new_on = *delay_on / 10;
  1670. new_off = *delay_off / 10;
  1671. for (n = 0; n < drv_data->led_count; n++) {
  1672. if (led == drv_data->leds[n])
  1673. break;
  1674. }
  1675. /* This LED is not registered on this device */
  1676. if (n >= drv_data->led_count)
  1677. return -EINVAL;
  1678. /* Don't schedule work if the values didn't change */
  1679. if (new_on != drv_data->led_delay_on[n] ||
  1680. new_off != drv_data->led_delay_off[n]) {
  1681. drv_data->led_delay_on[n] = new_on;
  1682. drv_data->led_delay_off[n] = new_off;
  1683. sony_schedule_work(drv_data, SONY_WORKER_STATE);
  1684. }
  1685. return 0;
  1686. }
  1687. static int sony_leds_init(struct sony_sc *sc)
  1688. {
  1689. struct hid_device *hdev = sc->hdev;
  1690. int n, ret = 0;
  1691. int use_ds4_names;
  1692. struct led_classdev *led;
  1693. size_t name_sz;
  1694. char *name;
  1695. size_t name_len;
  1696. const char *name_fmt;
  1697. static const char * const ds4_name_str[] = { "red", "green", "blue",
  1698. "global" };
  1699. u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
  1700. u8 use_hw_blink[MAX_LEDS] = { 0 };
  1701. BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
  1702. if (sc->quirks & BUZZ_CONTROLLER) {
  1703. sc->led_count = 4;
  1704. use_ds4_names = 0;
  1705. name_len = strlen("::buzz#");
  1706. name_fmt = "%s::buzz%d";
  1707. /* Validate expected report characteristics. */
  1708. if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
  1709. return -ENODEV;
  1710. } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
  1711. dualshock4_set_leds_from_id(sc);
  1712. sc->led_state[3] = 1;
  1713. sc->led_count = 4;
  1714. memset(max_brightness, 255, 3);
  1715. use_hw_blink[3] = 1;
  1716. use_ds4_names = 1;
  1717. name_len = 0;
  1718. name_fmt = "%s:%s";
  1719. } else if (sc->quirks & MOTION_CONTROLLER) {
  1720. sc->led_count = 3;
  1721. memset(max_brightness, 255, 3);
  1722. use_ds4_names = 1;
  1723. name_len = 0;
  1724. name_fmt = "%s:%s";
  1725. } else if (sc->quirks & NAVIGATION_CONTROLLER) {
  1726. static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
  1727. memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
  1728. sc->led_count = 1;
  1729. memset(use_hw_blink, 1, 4);
  1730. use_ds4_names = 0;
  1731. name_len = strlen("::sony#");
  1732. name_fmt = "%s::sony%d";
  1733. } else {
  1734. sixaxis_set_leds_from_id(sc);
  1735. sc->led_count = 4;
  1736. memset(use_hw_blink, 1, 4);
  1737. use_ds4_names = 0;
  1738. name_len = strlen("::sony#");
  1739. name_fmt = "%s::sony%d";
  1740. }
  1741. /*
  1742. * Clear LEDs as we have no way of reading their initial state. This is
  1743. * only relevant if the driver is loaded after somebody actively set the
  1744. * LEDs to on
  1745. */
  1746. sony_set_leds(sc);
  1747. name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
  1748. for (n = 0; n < sc->led_count; n++) {
  1749. if (use_ds4_names)
  1750. name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
  1751. led = devm_kzalloc(&hdev->dev, sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
  1752. if (!led) {
  1753. hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
  1754. return -ENOMEM;
  1755. }
  1756. name = (void *)(&led[1]);
  1757. if (use_ds4_names)
  1758. snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
  1759. ds4_name_str[n]);
  1760. else
  1761. snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
  1762. led->name = name;
  1763. led->brightness = sc->led_state[n];
  1764. led->max_brightness = max_brightness[n];
  1765. led->flags = LED_CORE_SUSPENDRESUME;
  1766. led->brightness_get = sony_led_get_brightness;
  1767. led->brightness_set = sony_led_set_brightness;
  1768. if (use_hw_blink[n])
  1769. led->blink_set = sony_led_blink_set;
  1770. sc->leds[n] = led;
  1771. ret = devm_led_classdev_register(&hdev->dev, led);
  1772. if (ret) {
  1773. hid_err(hdev, "Failed to register LED %d\n", n);
  1774. return ret;
  1775. }
  1776. }
  1777. return 0;
  1778. }
  1779. static void sixaxis_send_output_report(struct sony_sc *sc)
  1780. {
  1781. static const union sixaxis_output_report_01 default_report = {
  1782. .buf = {
  1783. 0x01,
  1784. 0x01, 0xff, 0x00, 0xff, 0x00,
  1785. 0x00, 0x00, 0x00, 0x00, 0x00,
  1786. 0xff, 0x27, 0x10, 0x00, 0x32,
  1787. 0xff, 0x27, 0x10, 0x00, 0x32,
  1788. 0xff, 0x27, 0x10, 0x00, 0x32,
  1789. 0xff, 0x27, 0x10, 0x00, 0x32,
  1790. 0x00, 0x00, 0x00, 0x00, 0x00
  1791. }
  1792. };
  1793. struct sixaxis_output_report *report =
  1794. (struct sixaxis_output_report *)sc->output_report_dmabuf;
  1795. int n;
  1796. /* Initialize the report with default values */
  1797. memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
  1798. #ifdef CONFIG_SONY_FF
  1799. report->rumble.right_motor_on = sc->right ? 1 : 0;
  1800. report->rumble.left_motor_force = sc->left;
  1801. #endif
  1802. report->leds_bitmap |= sc->led_state[0] << 1;
  1803. report->leds_bitmap |= sc->led_state[1] << 2;
  1804. report->leds_bitmap |= sc->led_state[2] << 3;
  1805. report->leds_bitmap |= sc->led_state[3] << 4;
  1806. /* Set flag for all leds off, required for 3rd party INTEC controller */
  1807. if ((report->leds_bitmap & 0x1E) == 0)
  1808. report->leds_bitmap |= 0x20;
  1809. /*
  1810. * The LEDs in the report are indexed in reverse order to their
  1811. * corresponding light on the controller.
  1812. * Index 0 = LED 4, index 1 = LED 3, etc...
  1813. *
  1814. * In the case of both delay values being zero (blinking disabled) the
  1815. * default report values should be used or the controller LED will be
  1816. * always off.
  1817. */
  1818. for (n = 0; n < 4; n++) {
  1819. if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
  1820. report->led[3 - n].duty_off = sc->led_delay_off[n];
  1821. report->led[3 - n].duty_on = sc->led_delay_on[n];
  1822. }
  1823. }
  1824. hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
  1825. sizeof(struct sixaxis_output_report),
  1826. HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
  1827. }
  1828. static void dualshock4_send_output_report(struct sony_sc *sc)
  1829. {
  1830. struct hid_device *hdev = sc->hdev;
  1831. u8 *buf = sc->output_report_dmabuf;
  1832. int offset;
  1833. /*
  1834. * NOTE: The lower 6 bits of buf[1] field of the Bluetooth report
  1835. * control the interval at which Dualshock 4 reports data:
  1836. * 0x00 - 1ms
  1837. * 0x01 - 1ms
  1838. * 0x02 - 2ms
  1839. * 0x3E - 62ms
  1840. * 0x3F - disabled
  1841. */
  1842. if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
  1843. memset(buf, 0, DS4_OUTPUT_REPORT_0x05_SIZE);
  1844. buf[0] = 0x05;
  1845. buf[1] = 0x07; /* blink + LEDs + motor */
  1846. offset = 4;
  1847. } else {
  1848. memset(buf, 0, DS4_OUTPUT_REPORT_0x11_SIZE);
  1849. buf[0] = 0x11;
  1850. buf[1] = 0xC0 /* HID + CRC */ | sc->ds4_bt_poll_interval;
  1851. buf[3] = 0x07; /* blink + LEDs + motor */
  1852. offset = 6;
  1853. }
  1854. #ifdef CONFIG_SONY_FF
  1855. buf[offset++] = sc->right;
  1856. buf[offset++] = sc->left;
  1857. #else
  1858. offset += 2;
  1859. #endif
  1860. /* LED 3 is the global control */
  1861. if (sc->led_state[3]) {
  1862. buf[offset++] = sc->led_state[0];
  1863. buf[offset++] = sc->led_state[1];
  1864. buf[offset++] = sc->led_state[2];
  1865. } else {
  1866. offset += 3;
  1867. }
  1868. /* If both delay values are zero the DualShock 4 disables blinking. */
  1869. buf[offset++] = sc->led_delay_on[3];
  1870. buf[offset++] = sc->led_delay_off[3];
  1871. if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
  1872. hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x05_SIZE);
  1873. else {
  1874. /* CRC generation */
  1875. u8 bthdr = 0xA2;
  1876. u32 crc;
  1877. crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
  1878. crc = ~crc32_le(crc, buf, DS4_OUTPUT_REPORT_0x11_SIZE-4);
  1879. put_unaligned_le32(crc, &buf[74]);
  1880. hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x11_SIZE);
  1881. }
  1882. }
  1883. static void motion_send_output_report(struct sony_sc *sc)
  1884. {
  1885. struct hid_device *hdev = sc->hdev;
  1886. struct motion_output_report_02 *report =
  1887. (struct motion_output_report_02 *)sc->output_report_dmabuf;
  1888. memset(report, 0, MOTION_REPORT_0x02_SIZE);
  1889. report->type = 0x02; /* set leds */
  1890. report->r = sc->led_state[0];
  1891. report->g = sc->led_state[1];
  1892. report->b = sc->led_state[2];
  1893. #ifdef CONFIG_SONY_FF
  1894. report->rumble = max(sc->right, sc->left);
  1895. #endif
  1896. hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
  1897. }
  1898. static inline void sony_send_output_report(struct sony_sc *sc)
  1899. {
  1900. if (sc->send_output_report)
  1901. sc->send_output_report(sc);
  1902. }
  1903. static void sony_state_worker(struct work_struct *work)
  1904. {
  1905. struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
  1906. sc->send_output_report(sc);
  1907. }
  1908. static int sony_allocate_output_report(struct sony_sc *sc)
  1909. {
  1910. if ((sc->quirks & SIXAXIS_CONTROLLER) ||
  1911. (sc->quirks & NAVIGATION_CONTROLLER))
  1912. sc->output_report_dmabuf =
  1913. devm_kmalloc(&sc->hdev->dev,
  1914. sizeof(union sixaxis_output_report_01),
  1915. GFP_KERNEL);
  1916. else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
  1917. sc->output_report_dmabuf = devm_kmalloc(&sc->hdev->dev,
  1918. DS4_OUTPUT_REPORT_0x11_SIZE,
  1919. GFP_KERNEL);
  1920. else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
  1921. sc->output_report_dmabuf = devm_kmalloc(&sc->hdev->dev,
  1922. DS4_OUTPUT_REPORT_0x05_SIZE,
  1923. GFP_KERNEL);
  1924. else if (sc->quirks & MOTION_CONTROLLER)
  1925. sc->output_report_dmabuf = devm_kmalloc(&sc->hdev->dev,
  1926. MOTION_REPORT_0x02_SIZE,
  1927. GFP_KERNEL);
  1928. else
  1929. return 0;
  1930. if (!sc->output_report_dmabuf)
  1931. return -ENOMEM;
  1932. return 0;
  1933. }
  1934. #ifdef CONFIG_SONY_FF
  1935. static int sony_play_effect(struct input_dev *dev, void *data,
  1936. struct ff_effect *effect)
  1937. {
  1938. struct hid_device *hid = input_get_drvdata(dev);
  1939. struct sony_sc *sc = hid_get_drvdata(hid);
  1940. if (effect->type != FF_RUMBLE)
  1941. return 0;
  1942. sc->left = effect->u.rumble.strong_magnitude / 256;
  1943. sc->right = effect->u.rumble.weak_magnitude / 256;
  1944. sony_schedule_work(sc, SONY_WORKER_STATE);
  1945. return 0;
  1946. }
  1947. static int sony_init_ff(struct sony_sc *sc)
  1948. {
  1949. struct hid_input *hidinput;
  1950. struct input_dev *input_dev;
  1951. if (list_empty(&sc->hdev->inputs)) {
  1952. hid_err(sc->hdev, "no inputs found\n");
  1953. return -ENODEV;
  1954. }
  1955. hidinput = list_entry(sc->hdev->inputs.next, struct hid_input, list);
  1956. input_dev = hidinput->input;
  1957. input_set_capability(input_dev, EV_FF, FF_RUMBLE);
  1958. return input_ff_create_memless(input_dev, NULL, sony_play_effect);
  1959. }
  1960. #else
  1961. static int sony_init_ff(struct sony_sc *sc)
  1962. {
  1963. return 0;
  1964. }
  1965. #endif
  1966. static int sony_battery_get_property(struct power_supply *psy,
  1967. enum power_supply_property psp,
  1968. union power_supply_propval *val)
  1969. {
  1970. struct sony_sc *sc = power_supply_get_drvdata(psy);
  1971. unsigned long flags;
  1972. int ret = 0;
  1973. u8 battery_charging, battery_capacity, cable_state;
  1974. spin_lock_irqsave(&sc->lock, flags);
  1975. battery_charging = sc->battery_charging;
  1976. battery_capacity = sc->battery_capacity;
  1977. cable_state = sc->cable_state;
  1978. spin_unlock_irqrestore(&sc->lock, flags);
  1979. switch (psp) {
  1980. case POWER_SUPPLY_PROP_PRESENT:
  1981. val->intval = 1;
  1982. break;
  1983. case POWER_SUPPLY_PROP_SCOPE:
  1984. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  1985. break;
  1986. case POWER_SUPPLY_PROP_CAPACITY:
  1987. val->intval = battery_capacity;
  1988. break;
  1989. case POWER_SUPPLY_PROP_STATUS:
  1990. if (battery_charging)
  1991. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  1992. else
  1993. if (battery_capacity == 100 && cable_state)
  1994. val->intval = POWER_SUPPLY_STATUS_FULL;
  1995. else
  1996. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  1997. break;
  1998. default:
  1999. ret = -EINVAL;
  2000. break;
  2001. }
  2002. return ret;
  2003. }
  2004. static int sony_battery_probe(struct sony_sc *sc, int append_dev_id)
  2005. {
  2006. const char *battery_str_fmt = append_dev_id ?
  2007. "sony_controller_battery_%pMR_%i" :
  2008. "sony_controller_battery_%pMR";
  2009. struct power_supply_config psy_cfg = { .drv_data = sc, };
  2010. struct hid_device *hdev = sc->hdev;
  2011. int ret;
  2012. /*
  2013. * Set the default battery level to 100% to avoid low battery warnings
  2014. * if the battery is polled before the first device report is received.
  2015. */
  2016. sc->battery_capacity = 100;
  2017. sc->battery_desc.properties = sony_battery_props;
  2018. sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
  2019. sc->battery_desc.get_property = sony_battery_get_property;
  2020. sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  2021. sc->battery_desc.use_for_apm = 0;
  2022. sc->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  2023. battery_str_fmt, sc->mac_address, sc->device_id);
  2024. if (!sc->battery_desc.name)
  2025. return -ENOMEM;
  2026. sc->battery = devm_power_supply_register(&hdev->dev, &sc->battery_desc,
  2027. &psy_cfg);
  2028. if (IS_ERR(sc->battery)) {
  2029. ret = PTR_ERR(sc->battery);
  2030. hid_err(hdev, "Unable to register battery device\n");
  2031. return ret;
  2032. }
  2033. power_supply_powers(sc->battery, &hdev->dev);
  2034. return 0;
  2035. }
  2036. /*
  2037. * If a controller is plugged in via USB while already connected via Bluetooth
  2038. * it will show up as two devices. A global list of connected controllers and
  2039. * their MAC addresses is maintained to ensure that a device is only connected
  2040. * once.
  2041. *
  2042. * Some USB-only devices masquerade as Sixaxis controllers and all have the
  2043. * same dummy Bluetooth address, so a comparison of the connection type is
  2044. * required. Devices are only rejected in the case where two devices have
  2045. * matching Bluetooth addresses on different bus types.
  2046. */
  2047. static inline int sony_compare_connection_type(struct sony_sc *sc0,
  2048. struct sony_sc *sc1)
  2049. {
  2050. const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE);
  2051. const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE);
  2052. return sc0_not_bt == sc1_not_bt;
  2053. }
  2054. static int sony_check_add_dev_list(struct sony_sc *sc)
  2055. {
  2056. struct sony_sc *entry;
  2057. unsigned long flags;
  2058. int ret;
  2059. spin_lock_irqsave(&sony_dev_list_lock, flags);
  2060. list_for_each_entry(entry, &sony_device_list, list_node) {
  2061. ret = memcmp(sc->mac_address, entry->mac_address,
  2062. sizeof(sc->mac_address));
  2063. if (!ret) {
  2064. if (sony_compare_connection_type(sc, entry)) {
  2065. ret = 1;
  2066. } else {
  2067. ret = -EEXIST;
  2068. hid_info(sc->hdev,
  2069. "controller with MAC address %pMR already connected\n",
  2070. sc->mac_address);
  2071. }
  2072. goto unlock;
  2073. }
  2074. }
  2075. ret = 0;
  2076. list_add(&(sc->list_node), &sony_device_list);
  2077. unlock:
  2078. spin_unlock_irqrestore(&sony_dev_list_lock, flags);
  2079. return ret;
  2080. }
  2081. static void sony_remove_dev_list(struct sony_sc *sc)
  2082. {
  2083. unsigned long flags;
  2084. if (sc->list_node.next) {
  2085. spin_lock_irqsave(&sony_dev_list_lock, flags);
  2086. list_del(&(sc->list_node));
  2087. spin_unlock_irqrestore(&sony_dev_list_lock, flags);
  2088. }
  2089. }
  2090. static int sony_get_bt_devaddr(struct sony_sc *sc)
  2091. {
  2092. int ret;
  2093. /* HIDP stores the device MAC address as a string in the uniq field. */
  2094. ret = strlen(sc->hdev->uniq);
  2095. if (ret != 17)
  2096. return -EINVAL;
  2097. ret = sscanf(sc->hdev->uniq,
  2098. "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  2099. &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
  2100. &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
  2101. if (ret != 6)
  2102. return -EINVAL;
  2103. return 0;
  2104. }
  2105. static int sony_check_add(struct sony_sc *sc)
  2106. {
  2107. u8 *buf = NULL;
  2108. int n, ret;
  2109. if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
  2110. (sc->quirks & MOTION_CONTROLLER_BT) ||
  2111. (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
  2112. (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
  2113. /*
  2114. * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
  2115. * address from the uniq string where HIDP stores it.
  2116. * As uniq cannot be guaranteed to be a MAC address in all cases
  2117. * a failure of this function should not prevent the connection.
  2118. */
  2119. if (sony_get_bt_devaddr(sc) < 0) {
  2120. hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
  2121. return 0;
  2122. }
  2123. } else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
  2124. buf = kmalloc(DS4_FEATURE_REPORT_0x81_SIZE, GFP_KERNEL);
  2125. if (!buf)
  2126. return -ENOMEM;
  2127. /*
  2128. * The MAC address of a DS4 controller connected via USB can be
  2129. * retrieved with feature report 0x81. The address begins at
  2130. * offset 1.
  2131. */
  2132. ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
  2133. DS4_FEATURE_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
  2134. HID_REQ_GET_REPORT);
  2135. if (ret != DS4_FEATURE_REPORT_0x81_SIZE) {
  2136. hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
  2137. ret = ret < 0 ? ret : -EINVAL;
  2138. goto out_free;
  2139. }
  2140. memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
  2141. snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
  2142. "%pMR", sc->mac_address);
  2143. } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
  2144. (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
  2145. buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
  2146. if (!buf)
  2147. return -ENOMEM;
  2148. /*
  2149. * The MAC address of a Sixaxis controller connected via USB can
  2150. * be retrieved with feature report 0xf2. The address begins at
  2151. * offset 4.
  2152. */
  2153. ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
  2154. SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
  2155. HID_REQ_GET_REPORT);
  2156. if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
  2157. hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
  2158. ret = ret < 0 ? ret : -EINVAL;
  2159. goto out_free;
  2160. }
  2161. /*
  2162. * The Sixaxis device MAC in the report is big-endian and must
  2163. * be byte-swapped.
  2164. */
  2165. for (n = 0; n < 6; n++)
  2166. sc->mac_address[5-n] = buf[4+n];
  2167. snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
  2168. "%pMR", sc->mac_address);
  2169. } else {
  2170. return 0;
  2171. }
  2172. ret = sony_check_add_dev_list(sc);
  2173. out_free:
  2174. kfree(buf);
  2175. return ret;
  2176. }
  2177. static int sony_set_device_id(struct sony_sc *sc)
  2178. {
  2179. int ret;
  2180. /*
  2181. * Only DualShock 4 or Sixaxis controllers get an id.
  2182. * All others are set to -1.
  2183. */
  2184. if ((sc->quirks & SIXAXIS_CONTROLLER) ||
  2185. (sc->quirks & DUALSHOCK4_CONTROLLER)) {
  2186. ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
  2187. GFP_KERNEL);
  2188. if (ret < 0) {
  2189. sc->device_id = -1;
  2190. return ret;
  2191. }
  2192. sc->device_id = ret;
  2193. } else {
  2194. sc->device_id = -1;
  2195. }
  2196. return 0;
  2197. }
  2198. static void sony_release_device_id(struct sony_sc *sc)
  2199. {
  2200. if (sc->device_id >= 0) {
  2201. ida_simple_remove(&sony_device_id_allocator, sc->device_id);
  2202. sc->device_id = -1;
  2203. }
  2204. }
  2205. static inline void sony_init_output_report(struct sony_sc *sc,
  2206. void (*send_output_report)(struct sony_sc *))
  2207. {
  2208. sc->send_output_report = send_output_report;
  2209. if (!sc->state_worker_initialized)
  2210. INIT_WORK(&sc->state_worker, sony_state_worker);
  2211. sc->state_worker_initialized = 1;
  2212. }
  2213. static inline void sony_cancel_work_sync(struct sony_sc *sc)
  2214. {
  2215. unsigned long flags;
  2216. if (sc->hotplug_worker_initialized)
  2217. cancel_work_sync(&sc->hotplug_worker);
  2218. if (sc->state_worker_initialized) {
  2219. spin_lock_irqsave(&sc->lock, flags);
  2220. sc->state_worker_initialized = 0;
  2221. spin_unlock_irqrestore(&sc->lock, flags);
  2222. cancel_work_sync(&sc->state_worker);
  2223. }
  2224. }
  2225. static int sony_input_configured(struct hid_device *hdev,
  2226. struct hid_input *hidinput)
  2227. {
  2228. struct sony_sc *sc = hid_get_drvdata(hdev);
  2229. int append_dev_id;
  2230. int ret;
  2231. ret = sony_set_device_id(sc);
  2232. if (ret < 0) {
  2233. hid_err(hdev, "failed to allocate the device id\n");
  2234. goto err_stop;
  2235. }
  2236. ret = append_dev_id = sony_check_add(sc);
  2237. if (ret < 0)
  2238. goto err_stop;
  2239. ret = sony_allocate_output_report(sc);
  2240. if (ret < 0) {
  2241. hid_err(hdev, "failed to allocate the output report buffer\n");
  2242. goto err_stop;
  2243. }
  2244. if (sc->quirks & NAVIGATION_CONTROLLER_USB) {
  2245. /*
  2246. * The Sony Sixaxis does not handle HID Output Reports on the
  2247. * Interrupt EP like it could, so we need to force HID Output
  2248. * Reports to use HID_REQ_SET_REPORT on the Control EP.
  2249. *
  2250. * There is also another issue about HID Output Reports via USB,
  2251. * the Sixaxis does not want the report_id as part of the data
  2252. * packet, so we have to discard buf[0] when sending the actual
  2253. * control message, even for numbered reports, humpf!
  2254. *
  2255. * Additionally, the Sixaxis on USB isn't properly initialized
  2256. * until the PS logo button is pressed and as such won't retain
  2257. * any state set by an output report, so the initial
  2258. * configuration report is deferred until the first input
  2259. * report arrives.
  2260. */
  2261. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  2262. hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
  2263. sc->defer_initialization = 1;
  2264. ret = sixaxis_set_operational_usb(hdev);
  2265. if (ret < 0) {
  2266. hid_err(hdev, "Failed to set controller into operational mode\n");
  2267. goto err_stop;
  2268. }
  2269. sony_init_output_report(sc, sixaxis_send_output_report);
  2270. } else if (sc->quirks & NAVIGATION_CONTROLLER_BT) {
  2271. /*
  2272. * The Navigation controller wants output reports sent on the ctrl
  2273. * endpoint when connected via Bluetooth.
  2274. */
  2275. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  2276. ret = sixaxis_set_operational_bt(hdev);
  2277. if (ret < 0) {
  2278. hid_err(hdev, "Failed to set controller into operational mode\n");
  2279. goto err_stop;
  2280. }
  2281. sony_init_output_report(sc, sixaxis_send_output_report);
  2282. } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
  2283. /*
  2284. * The Sony Sixaxis does not handle HID Output Reports on the
  2285. * Interrupt EP and the device only becomes active when the
  2286. * PS button is pressed. See comment for Navigation controller
  2287. * above for more details.
  2288. */
  2289. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  2290. hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
  2291. sc->defer_initialization = 1;
  2292. ret = sixaxis_set_operational_usb(hdev);
  2293. if (ret < 0) {
  2294. hid_err(hdev, "Failed to set controller into operational mode\n");
  2295. goto err_stop;
  2296. }
  2297. ret = sony_register_sensors(sc);
  2298. if (ret) {
  2299. hid_err(sc->hdev,
  2300. "Unable to initialize motion sensors: %d\n", ret);
  2301. goto err_stop;
  2302. }
  2303. sony_init_output_report(sc, sixaxis_send_output_report);
  2304. } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
  2305. /*
  2306. * The Sixaxis wants output reports sent on the ctrl endpoint
  2307. * when connected via Bluetooth.
  2308. */
  2309. hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
  2310. ret = sixaxis_set_operational_bt(hdev);
  2311. if (ret < 0) {
  2312. hid_err(hdev, "Failed to set controller into operational mode\n");
  2313. goto err_stop;
  2314. }
  2315. ret = sony_register_sensors(sc);
  2316. if (ret) {
  2317. hid_err(sc->hdev,
  2318. "Unable to initialize motion sensors: %d\n", ret);
  2319. goto err_stop;
  2320. }
  2321. sony_init_output_report(sc, sixaxis_send_output_report);
  2322. } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
  2323. ret = dualshock4_get_calibration_data(sc);
  2324. if (ret < 0) {
  2325. hid_err(hdev, "Failed to get calibration data from Dualshock 4\n");
  2326. goto err_stop;
  2327. }
  2328. ret = dualshock4_get_version_info(sc);
  2329. if (ret < 0) {
  2330. hid_err(sc->hdev, "Failed to get version data from Dualshock 4\n");
  2331. goto err_stop;
  2332. }
  2333. ret = device_create_file(&sc->hdev->dev, &dev_attr_firmware_version);
  2334. if (ret) {
  2335. /* Make zero for cleanup reasons of sysfs entries. */
  2336. sc->fw_version = 0;
  2337. sc->hw_version = 0;
  2338. hid_err(sc->hdev, "can't create sysfs firmware_version attribute err: %d\n", ret);
  2339. goto err_stop;
  2340. }
  2341. ret = device_create_file(&sc->hdev->dev, &dev_attr_hardware_version);
  2342. if (ret) {
  2343. sc->hw_version = 0;
  2344. hid_err(sc->hdev, "can't create sysfs hardware_version attribute err: %d\n", ret);
  2345. goto err_stop;
  2346. }
  2347. /*
  2348. * The Dualshock 4 touchpad supports 2 touches and has a
  2349. * resolution of 1920x942 (44.86 dots/mm).
  2350. */
  2351. ret = sony_register_touchpad(sc, 2, 1920, 942, 0, 0, 0);
  2352. if (ret) {
  2353. hid_err(sc->hdev,
  2354. "Unable to initialize multi-touch slots: %d\n",
  2355. ret);
  2356. goto err_stop;
  2357. }
  2358. ret = sony_register_sensors(sc);
  2359. if (ret) {
  2360. hid_err(sc->hdev,
  2361. "Unable to initialize motion sensors: %d\n", ret);
  2362. goto err_stop;
  2363. }
  2364. if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
  2365. sc->ds4_bt_poll_interval = DS4_BT_DEFAULT_POLL_INTERVAL_MS;
  2366. ret = device_create_file(&sc->hdev->dev, &dev_attr_bt_poll_interval);
  2367. if (ret)
  2368. hid_warn(sc->hdev,
  2369. "can't create sysfs bt_poll_interval attribute err: %d\n",
  2370. ret);
  2371. }
  2372. if (sc->quirks & DUALSHOCK4_DONGLE) {
  2373. INIT_WORK(&sc->hotplug_worker, dualshock4_calibration_work);
  2374. sc->hotplug_worker_initialized = 1;
  2375. sc->ds4_dongle_state = DONGLE_DISCONNECTED;
  2376. }
  2377. sony_init_output_report(sc, dualshock4_send_output_report);
  2378. } else if (sc->quirks & NSG_MRXU_REMOTE) {
  2379. /*
  2380. * The NSG-MRxU touchpad supports 2 touches and has a
  2381. * resolution of 1667x1868
  2382. */
  2383. ret = sony_register_touchpad(sc, 2,
  2384. NSG_MRXU_MAX_X, NSG_MRXU_MAX_Y, 15, 15, 1);
  2385. if (ret) {
  2386. hid_err(sc->hdev,
  2387. "Unable to initialize multi-touch slots: %d\n",
  2388. ret);
  2389. goto err_stop;
  2390. }
  2391. } else if (sc->quirks & MOTION_CONTROLLER) {
  2392. sony_init_output_report(sc, motion_send_output_report);
  2393. } else {
  2394. ret = 0;
  2395. }
  2396. if (sc->quirks & SONY_LED_SUPPORT) {
  2397. ret = sony_leds_init(sc);
  2398. if (ret < 0)
  2399. goto err_stop;
  2400. }
  2401. if (sc->quirks & SONY_BATTERY_SUPPORT) {
  2402. ret = sony_battery_probe(sc, append_dev_id);
  2403. if (ret < 0)
  2404. goto err_stop;
  2405. /* Open the device to receive reports with battery info */
  2406. ret = hid_hw_open(hdev);
  2407. if (ret < 0) {
  2408. hid_err(hdev, "hw open failed\n");
  2409. goto err_stop;
  2410. }
  2411. }
  2412. if (sc->quirks & SONY_FF_SUPPORT) {
  2413. ret = sony_init_ff(sc);
  2414. if (ret < 0)
  2415. goto err_close;
  2416. }
  2417. return 0;
  2418. err_close:
  2419. hid_hw_close(hdev);
  2420. err_stop:
  2421. /* Piggy back on the default ds4_bt_ poll_interval to determine
  2422. * if we need to remove the file as we don't know for sure if we
  2423. * executed that logic.
  2424. */
  2425. if (sc->ds4_bt_poll_interval)
  2426. device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval);
  2427. if (sc->fw_version)
  2428. device_remove_file(&sc->hdev->dev, &dev_attr_firmware_version);
  2429. if (sc->hw_version)
  2430. device_remove_file(&sc->hdev->dev, &dev_attr_hardware_version);
  2431. sony_cancel_work_sync(sc);
  2432. sony_remove_dev_list(sc);
  2433. sony_release_device_id(sc);
  2434. return ret;
  2435. }
  2436. static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
  2437. {
  2438. int ret;
  2439. unsigned long quirks = id->driver_data;
  2440. struct sony_sc *sc;
  2441. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  2442. if (!strcmp(hdev->name, "FutureMax Dance Mat"))
  2443. quirks |= FUTUREMAX_DANCE_MAT;
  2444. sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
  2445. if (sc == NULL) {
  2446. hid_err(hdev, "can't alloc sony descriptor\n");
  2447. return -ENOMEM;
  2448. }
  2449. spin_lock_init(&sc->lock);
  2450. sc->quirks = quirks;
  2451. hid_set_drvdata(hdev, sc);
  2452. sc->hdev = hdev;
  2453. ret = hid_parse(hdev);
  2454. if (ret) {
  2455. hid_err(hdev, "parse failed\n");
  2456. return ret;
  2457. }
  2458. if (sc->quirks & VAIO_RDESC_CONSTANT)
  2459. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  2460. else if (sc->quirks & SIXAXIS_CONTROLLER)
  2461. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  2462. /* Patch the hw version on DS3/4 compatible devices, so applications can
  2463. * distinguish between the default HID mappings and the mappings defined
  2464. * by the Linux game controller spec. This is important for the SDL2
  2465. * library, which has a game controller database, which uses device ids
  2466. * in combination with version as a key.
  2467. */
  2468. if (sc->quirks & (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER))
  2469. hdev->version |= 0x8000;
  2470. ret = hid_hw_start(hdev, connect_mask);
  2471. if (ret) {
  2472. hid_err(hdev, "hw start failed\n");
  2473. return ret;
  2474. }
  2475. /* sony_input_configured can fail, but this doesn't result
  2476. * in hid_hw_start failures (intended). Check whether
  2477. * the HID layer claimed the device else fail.
  2478. * We don't know the actual reason for the failure, most
  2479. * likely it is due to EEXIST in case of double connection
  2480. * of USB and Bluetooth, but could have been due to ENOMEM
  2481. * or other reasons as well.
  2482. */
  2483. if (!(hdev->claimed & HID_CLAIMED_INPUT)) {
  2484. hid_err(hdev, "failed to claim input\n");
  2485. hid_hw_stop(hdev);
  2486. return -ENODEV;
  2487. }
  2488. return ret;
  2489. }
  2490. static void sony_remove(struct hid_device *hdev)
  2491. {
  2492. struct sony_sc *sc = hid_get_drvdata(hdev);
  2493. hid_hw_close(hdev);
  2494. if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
  2495. device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval);
  2496. if (sc->fw_version)
  2497. device_remove_file(&sc->hdev->dev, &dev_attr_firmware_version);
  2498. if (sc->hw_version)
  2499. device_remove_file(&sc->hdev->dev, &dev_attr_hardware_version);
  2500. sony_cancel_work_sync(sc);
  2501. sony_remove_dev_list(sc);
  2502. sony_release_device_id(sc);
  2503. hid_hw_stop(hdev);
  2504. }
  2505. #ifdef CONFIG_PM
  2506. static int sony_suspend(struct hid_device *hdev, pm_message_t message)
  2507. {
  2508. #ifdef CONFIG_SONY_FF
  2509. /* On suspend stop any running force-feedback events */
  2510. if (SONY_FF_SUPPORT) {
  2511. struct sony_sc *sc = hid_get_drvdata(hdev);
  2512. sc->left = sc->right = 0;
  2513. sony_send_output_report(sc);
  2514. }
  2515. #endif
  2516. return 0;
  2517. }
  2518. static int sony_resume(struct hid_device *hdev)
  2519. {
  2520. struct sony_sc *sc = hid_get_drvdata(hdev);
  2521. /*
  2522. * The Sixaxis and navigation controllers on USB need to be
  2523. * reinitialized on resume or they won't behave properly.
  2524. */
  2525. if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
  2526. (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
  2527. sixaxis_set_operational_usb(sc->hdev);
  2528. sc->defer_initialization = 1;
  2529. }
  2530. return 0;
  2531. }
  2532. #endif
  2533. static const struct hid_device_id sony_devices[] = {
  2534. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  2535. .driver_data = SIXAXIS_CONTROLLER_USB },
  2536. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
  2537. .driver_data = NAVIGATION_CONTROLLER_USB },
  2538. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
  2539. .driver_data = NAVIGATION_CONTROLLER_BT },
  2540. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
  2541. .driver_data = MOTION_CONTROLLER_USB },
  2542. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
  2543. .driver_data = MOTION_CONTROLLER_BT },
  2544. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  2545. .driver_data = SIXAXIS_CONTROLLER_BT },
  2546. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
  2547. .driver_data = VAIO_RDESC_CONSTANT },
  2548. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
  2549. .driver_data = VAIO_RDESC_CONSTANT },
  2550. /*
  2551. * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
  2552. * Logitech joystick from the device descriptor.
  2553. */
  2554. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
  2555. .driver_data = BUZZ_CONTROLLER },
  2556. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
  2557. .driver_data = BUZZ_CONTROLLER },
  2558. /* PS3 BD Remote Control */
  2559. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
  2560. .driver_data = PS3REMOTE },
  2561. /* Logitech Harmony Adapter for PS3 */
  2562. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
  2563. .driver_data = PS3REMOTE },
  2564. /* SMK-Link PS3 BD Remote Control */
  2565. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
  2566. .driver_data = PS3REMOTE },
  2567. /* Sony Dualshock 4 controllers for PS4 */
  2568. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
  2569. .driver_data = DUALSHOCK4_CONTROLLER_USB },
  2570. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
  2571. .driver_data = DUALSHOCK4_CONTROLLER_BT },
  2572. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
  2573. .driver_data = DUALSHOCK4_CONTROLLER_USB },
  2574. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
  2575. .driver_data = DUALSHOCK4_CONTROLLER_BT },
  2576. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE),
  2577. .driver_data = DUALSHOCK4_DONGLE },
  2578. /* Nyko Core Controller for PS3 */
  2579. { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
  2580. .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
  2581. /* SMK-Link NSG-MR5U Remote Control */
  2582. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_NSG_MR5U_REMOTE),
  2583. .driver_data = NSG_MR5U_REMOTE_BT },
  2584. /* SMK-Link NSG-MR7U Remote Control */
  2585. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_NSG_MR7U_REMOTE),
  2586. .driver_data = NSG_MR7U_REMOTE_BT },
  2587. { }
  2588. };
  2589. MODULE_DEVICE_TABLE(hid, sony_devices);
  2590. static struct hid_driver sony_driver = {
  2591. .name = "sony",
  2592. .id_table = sony_devices,
  2593. .input_mapping = sony_mapping,
  2594. .input_configured = sony_input_configured,
  2595. .probe = sony_probe,
  2596. .remove = sony_remove,
  2597. .report_fixup = sony_report_fixup,
  2598. .raw_event = sony_raw_event,
  2599. #ifdef CONFIG_PM
  2600. .suspend = sony_suspend,
  2601. .resume = sony_resume,
  2602. .reset_resume = sony_resume,
  2603. #endif
  2604. };
  2605. static int __init sony_init(void)
  2606. {
  2607. dbg_hid("Sony:%s\n", __func__);
  2608. return hid_register_driver(&sony_driver);
  2609. }
  2610. static void __exit sony_exit(void)
  2611. {
  2612. dbg_hid("Sony:%s\n", __func__);
  2613. hid_unregister_driver(&sony_driver);
  2614. ida_destroy(&sony_device_id_allocator);
  2615. }
  2616. module_init(sony_init);
  2617. module_exit(sony_exit);
  2618. MODULE_LICENSE("GPL");