hid-sony.c 97 KB

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