hid-sony.c 96 KB

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