hid-sony.c 96 KB

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