hid-sony.c 91 KB

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