hid-sony.c 103 KB

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