hid-playstation.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID driver for Sony DualSense(TM) controller.
  4. *
  5. * Copyright (c) 2020-2022 Sony Interactive Entertainment
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/crc32.h>
  9. #include <linux/device.h>
  10. #include <linux/hid.h>
  11. #include <linux/idr.h>
  12. #include <linux/input/mt.h>
  13. #include <linux/leds.h>
  14. #include <linux/led-class-multicolor.h>
  15. #include <linux/module.h>
  16. #include <linux/unaligned.h>
  17. #include "hid-ids.h"
  18. /* List of connected playstation devices. */
  19. static DEFINE_MUTEX(ps_devices_lock);
  20. static LIST_HEAD(ps_devices_list);
  21. static DEFINE_IDA(ps_player_id_allocator);
  22. #define HID_PLAYSTATION_VERSION_PATCH 0x8000
  23. enum PS_TYPE {
  24. PS_TYPE_PS4_DUALSHOCK4,
  25. PS_TYPE_PS5_DUALSENSE,
  26. };
  27. /* Base class for playstation devices. */
  28. struct ps_device {
  29. struct list_head list;
  30. struct hid_device *hdev;
  31. spinlock_t lock;
  32. uint32_t player_id;
  33. struct power_supply_desc battery_desc;
  34. struct power_supply *battery;
  35. uint8_t battery_capacity;
  36. int battery_status;
  37. const char *input_dev_name; /* Name of primary input device. */
  38. uint8_t mac_address[6]; /* Note: stored in little endian order. */
  39. uint32_t hw_version;
  40. uint32_t fw_version;
  41. int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size);
  42. void (*remove)(struct ps_device *dev);
  43. };
  44. /* Calibration data for playstation motion sensors. */
  45. struct ps_calibration_data {
  46. int abs_code;
  47. short bias;
  48. int sens_numer;
  49. int sens_denom;
  50. };
  51. struct ps_led_info {
  52. const char *name;
  53. const char *color;
  54. int max_brightness;
  55. enum led_brightness (*brightness_get)(struct led_classdev *cdev);
  56. int (*brightness_set)(struct led_classdev *cdev, enum led_brightness);
  57. int (*blink_set)(struct led_classdev *led, unsigned long *on, unsigned long *off);
  58. };
  59. /* Seed values for DualShock4 / DualSense CRC32 for different report types. */
  60. #define PS_INPUT_CRC32_SEED 0xA1
  61. #define PS_OUTPUT_CRC32_SEED 0xA2
  62. #define PS_FEATURE_CRC32_SEED 0xA3
  63. #define DS_INPUT_REPORT_USB 0x01
  64. #define DS_INPUT_REPORT_USB_SIZE 64
  65. #define DS_INPUT_REPORT_BT 0x31
  66. #define DS_INPUT_REPORT_BT_SIZE 78
  67. #define DS_OUTPUT_REPORT_USB 0x02
  68. #define DS_OUTPUT_REPORT_USB_SIZE 63
  69. #define DS_OUTPUT_REPORT_BT 0x31
  70. #define DS_OUTPUT_REPORT_BT_SIZE 78
  71. #define DS_FEATURE_REPORT_CALIBRATION 0x05
  72. #define DS_FEATURE_REPORT_CALIBRATION_SIZE 41
  73. #define DS_FEATURE_REPORT_PAIRING_INFO 0x09
  74. #define DS_FEATURE_REPORT_PAIRING_INFO_SIZE 20
  75. #define DS_FEATURE_REPORT_FIRMWARE_INFO 0x20
  76. #define DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE 64
  77. /* Button masks for DualSense input report. */
  78. #define DS_BUTTONS0_HAT_SWITCH GENMASK(3, 0)
  79. #define DS_BUTTONS0_SQUARE BIT(4)
  80. #define DS_BUTTONS0_CROSS BIT(5)
  81. #define DS_BUTTONS0_CIRCLE BIT(6)
  82. #define DS_BUTTONS0_TRIANGLE BIT(7)
  83. #define DS_BUTTONS1_L1 BIT(0)
  84. #define DS_BUTTONS1_R1 BIT(1)
  85. #define DS_BUTTONS1_L2 BIT(2)
  86. #define DS_BUTTONS1_R2 BIT(3)
  87. #define DS_BUTTONS1_CREATE BIT(4)
  88. #define DS_BUTTONS1_OPTIONS BIT(5)
  89. #define DS_BUTTONS1_L3 BIT(6)
  90. #define DS_BUTTONS1_R3 BIT(7)
  91. #define DS_BUTTONS2_PS_HOME BIT(0)
  92. #define DS_BUTTONS2_TOUCHPAD BIT(1)
  93. #define DS_BUTTONS2_MIC_MUTE BIT(2)
  94. /* Status field of DualSense input report. */
  95. #define DS_STATUS_BATTERY_CAPACITY GENMASK(3, 0)
  96. #define DS_STATUS_CHARGING GENMASK(7, 4)
  97. #define DS_STATUS_CHARGING_SHIFT 4
  98. /* Feature version from DualSense Firmware Info report. */
  99. #define DS_FEATURE_VERSION(major, minor) ((major & 0xff) << 8 | (minor & 0xff))
  100. /*
  101. * Status of a DualSense touch point contact.
  102. * Contact IDs, with highest bit set are 'inactive'
  103. * and any associated data is then invalid.
  104. */
  105. #define DS_TOUCH_POINT_INACTIVE BIT(7)
  106. /* Magic value required in tag field of Bluetooth output report. */
  107. #define DS_OUTPUT_TAG 0x10
  108. /* Flags for DualSense output report. */
  109. #define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0)
  110. #define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1)
  111. #define DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE BIT(0)
  112. #define DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE BIT(1)
  113. #define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2)
  114. #define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3)
  115. #define DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE BIT(4)
  116. #define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1)
  117. #define DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2 BIT(2)
  118. #define DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE BIT(4)
  119. #define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1)
  120. /* DualSense hardware limits */
  121. #define DS_ACC_RES_PER_G 8192
  122. #define DS_ACC_RANGE (4*DS_ACC_RES_PER_G)
  123. #define DS_GYRO_RES_PER_DEG_S 1024
  124. #define DS_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S)
  125. #define DS_TOUCHPAD_WIDTH 1920
  126. #define DS_TOUCHPAD_HEIGHT 1080
  127. struct dualsense {
  128. struct ps_device base;
  129. struct input_dev *gamepad;
  130. struct input_dev *sensors;
  131. struct input_dev *touchpad;
  132. /* Update version is used as a feature/capability version. */
  133. uint16_t update_version;
  134. /* Calibration data for accelerometer and gyroscope. */
  135. struct ps_calibration_data accel_calib_data[3];
  136. struct ps_calibration_data gyro_calib_data[3];
  137. /* Timestamp for sensor data */
  138. bool sensor_timestamp_initialized;
  139. uint32_t prev_sensor_timestamp;
  140. uint32_t sensor_timestamp_us;
  141. /* Compatible rumble state */
  142. bool use_vibration_v2;
  143. bool update_rumble;
  144. uint8_t motor_left;
  145. uint8_t motor_right;
  146. /* RGB lightbar */
  147. struct led_classdev_mc lightbar;
  148. bool update_lightbar;
  149. uint8_t lightbar_red;
  150. uint8_t lightbar_green;
  151. uint8_t lightbar_blue;
  152. /* Microphone */
  153. bool update_mic_mute;
  154. bool mic_muted;
  155. bool last_btn_mic_state;
  156. /* Player leds */
  157. bool update_player_leds;
  158. uint8_t player_leds_state;
  159. struct led_classdev player_leds[5];
  160. struct work_struct output_worker;
  161. bool output_worker_initialized;
  162. void *output_report_dmabuf;
  163. uint8_t output_seq; /* Sequence number for output report. */
  164. };
  165. struct dualsense_touch_point {
  166. uint8_t contact;
  167. uint8_t x_lo;
  168. uint8_t x_hi:4, y_lo:4;
  169. uint8_t y_hi;
  170. } __packed;
  171. static_assert(sizeof(struct dualsense_touch_point) == 4);
  172. /* Main DualSense input report excluding any BT/USB specific headers. */
  173. struct dualsense_input_report {
  174. uint8_t x, y;
  175. uint8_t rx, ry;
  176. uint8_t z, rz;
  177. uint8_t seq_number;
  178. uint8_t buttons[4];
  179. uint8_t reserved[4];
  180. /* Motion sensors */
  181. __le16 gyro[3]; /* x, y, z */
  182. __le16 accel[3]; /* x, y, z */
  183. __le32 sensor_timestamp;
  184. uint8_t reserved2;
  185. /* Touchpad */
  186. struct dualsense_touch_point points[2];
  187. uint8_t reserved3[12];
  188. uint8_t status;
  189. uint8_t reserved4[10];
  190. } __packed;
  191. /* Common input report size shared equals the size of the USB report minus 1 byte for ReportID. */
  192. static_assert(sizeof(struct dualsense_input_report) == DS_INPUT_REPORT_USB_SIZE - 1);
  193. /* Common data between DualSense BT/USB main output report. */
  194. struct dualsense_output_report_common {
  195. uint8_t valid_flag0;
  196. uint8_t valid_flag1;
  197. /* For DualShock 4 compatibility mode. */
  198. uint8_t motor_right;
  199. uint8_t motor_left;
  200. /* Audio controls */
  201. uint8_t reserved[4];
  202. uint8_t mute_button_led;
  203. uint8_t power_save_control;
  204. uint8_t reserved2[28];
  205. /* LEDs and lightbar */
  206. uint8_t valid_flag2;
  207. uint8_t reserved3[2];
  208. uint8_t lightbar_setup;
  209. uint8_t led_brightness;
  210. uint8_t player_leds;
  211. uint8_t lightbar_red;
  212. uint8_t lightbar_green;
  213. uint8_t lightbar_blue;
  214. } __packed;
  215. static_assert(sizeof(struct dualsense_output_report_common) == 47);
  216. struct dualsense_output_report_bt {
  217. uint8_t report_id; /* 0x31 */
  218. uint8_t seq_tag;
  219. uint8_t tag;
  220. struct dualsense_output_report_common common;
  221. uint8_t reserved[24];
  222. __le32 crc32;
  223. } __packed;
  224. static_assert(sizeof(struct dualsense_output_report_bt) == DS_OUTPUT_REPORT_BT_SIZE);
  225. struct dualsense_output_report_usb {
  226. uint8_t report_id; /* 0x02 */
  227. struct dualsense_output_report_common common;
  228. uint8_t reserved[15];
  229. } __packed;
  230. static_assert(sizeof(struct dualsense_output_report_usb) == DS_OUTPUT_REPORT_USB_SIZE);
  231. /*
  232. * The DualSense has a main output report used to control most features. It is
  233. * largely the same between Bluetooth and USB except for different headers and CRC.
  234. * This structure hide the differences between the two to simplify sending output reports.
  235. */
  236. struct dualsense_output_report {
  237. uint8_t *data; /* Start of data */
  238. uint8_t len; /* Size of output report */
  239. /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
  240. struct dualsense_output_report_bt *bt;
  241. /* Points to USB data payload in case for a USB report else NULL. */
  242. struct dualsense_output_report_usb *usb;
  243. /* Points to common section of report, so past any headers. */
  244. struct dualsense_output_report_common *common;
  245. };
  246. #define DS4_INPUT_REPORT_USB 0x01
  247. #define DS4_INPUT_REPORT_USB_SIZE 64
  248. #define DS4_INPUT_REPORT_BT_MINIMAL 0x01
  249. #define DS4_INPUT_REPORT_BT_MINIMAL_SIZE 10
  250. #define DS4_INPUT_REPORT_BT 0x11
  251. #define DS4_INPUT_REPORT_BT_SIZE 78
  252. #define DS4_OUTPUT_REPORT_USB 0x05
  253. #define DS4_OUTPUT_REPORT_USB_SIZE 32
  254. #define DS4_OUTPUT_REPORT_BT 0x11
  255. #define DS4_OUTPUT_REPORT_BT_SIZE 78
  256. #define DS4_FEATURE_REPORT_CALIBRATION 0x02
  257. #define DS4_FEATURE_REPORT_CALIBRATION_SIZE 37
  258. #define DS4_FEATURE_REPORT_CALIBRATION_BT 0x05
  259. #define DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE 41
  260. #define DS4_FEATURE_REPORT_FIRMWARE_INFO 0xa3
  261. #define DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE 49
  262. #define DS4_FEATURE_REPORT_PAIRING_INFO 0x12
  263. #define DS4_FEATURE_REPORT_PAIRING_INFO_SIZE 16
  264. /*
  265. * Status of a DualShock4 touch point contact.
  266. * Contact IDs, with highest bit set are 'inactive'
  267. * and any associated data is then invalid.
  268. */
  269. #define DS4_TOUCH_POINT_INACTIVE BIT(7)
  270. /* Status field of DualShock4 input report. */
  271. #define DS4_STATUS0_BATTERY_CAPACITY GENMASK(3, 0)
  272. #define DS4_STATUS0_CABLE_STATE BIT(4)
  273. /* Battery status within batery_status field. */
  274. #define DS4_BATTERY_STATUS_FULL 11
  275. /* Status1 bit2 contains dongle connection state:
  276. * 0 = connectd
  277. * 1 = disconnected
  278. */
  279. #define DS4_STATUS1_DONGLE_STATE BIT(2)
  280. /* The lower 6 bits of hw_control of the Bluetooth main output report
  281. * control the interval at which Dualshock 4 reports data:
  282. * 0x00 - 1ms
  283. * 0x01 - 1ms
  284. * 0x02 - 2ms
  285. * 0x3E - 62ms
  286. * 0x3F - disabled
  287. */
  288. #define DS4_OUTPUT_HWCTL_BT_POLL_MASK 0x3F
  289. /* Default to 4ms poll interval, which is same as USB (not adjustable). */
  290. #define DS4_BT_DEFAULT_POLL_INTERVAL_MS 4
  291. #define DS4_OUTPUT_HWCTL_CRC32 0x40
  292. #define DS4_OUTPUT_HWCTL_HID 0x80
  293. /* Flags for DualShock4 output report. */
  294. #define DS4_OUTPUT_VALID_FLAG0_MOTOR 0x01
  295. #define DS4_OUTPUT_VALID_FLAG0_LED 0x02
  296. #define DS4_OUTPUT_VALID_FLAG0_LED_BLINK 0x04
  297. /* DualShock4 hardware limits */
  298. #define DS4_ACC_RES_PER_G 8192
  299. #define DS4_ACC_RANGE (4*DS_ACC_RES_PER_G)
  300. #define DS4_GYRO_RES_PER_DEG_S 1024
  301. #define DS4_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S)
  302. #define DS4_LIGHTBAR_MAX_BLINK 255 /* 255 centiseconds */
  303. #define DS4_TOUCHPAD_WIDTH 1920
  304. #define DS4_TOUCHPAD_HEIGHT 942
  305. enum dualshock4_dongle_state {
  306. DONGLE_DISCONNECTED,
  307. DONGLE_CALIBRATING,
  308. DONGLE_CONNECTED,
  309. DONGLE_DISABLED
  310. };
  311. struct dualshock4 {
  312. struct ps_device base;
  313. struct input_dev *gamepad;
  314. struct input_dev *sensors;
  315. struct input_dev *touchpad;
  316. /* Calibration data for accelerometer and gyroscope. */
  317. struct ps_calibration_data accel_calib_data[3];
  318. struct ps_calibration_data gyro_calib_data[3];
  319. /* Only used on dongle to track state transitions. */
  320. enum dualshock4_dongle_state dongle_state;
  321. /* Used during calibration. */
  322. struct work_struct dongle_hotplug_worker;
  323. /* Timestamp for sensor data */
  324. bool sensor_timestamp_initialized;
  325. uint32_t prev_sensor_timestamp;
  326. uint32_t sensor_timestamp_us;
  327. /* Bluetooth poll interval */
  328. bool update_bt_poll_interval;
  329. uint8_t bt_poll_interval;
  330. bool update_rumble;
  331. uint8_t motor_left;
  332. uint8_t motor_right;
  333. /* Lightbar leds */
  334. bool update_lightbar;
  335. bool update_lightbar_blink;
  336. bool lightbar_enabled; /* For use by global LED control. */
  337. uint8_t lightbar_red;
  338. uint8_t lightbar_green;
  339. uint8_t lightbar_blue;
  340. uint8_t lightbar_blink_on; /* In increments of 10ms. */
  341. uint8_t lightbar_blink_off; /* In increments of 10ms. */
  342. struct led_classdev lightbar_leds[4];
  343. struct work_struct output_worker;
  344. bool output_worker_initialized;
  345. void *output_report_dmabuf;
  346. };
  347. struct dualshock4_touch_point {
  348. uint8_t contact;
  349. uint8_t x_lo;
  350. uint8_t x_hi:4, y_lo:4;
  351. uint8_t y_hi;
  352. } __packed;
  353. static_assert(sizeof(struct dualshock4_touch_point) == 4);
  354. struct dualshock4_touch_report {
  355. uint8_t timestamp;
  356. struct dualshock4_touch_point points[2];
  357. } __packed;
  358. static_assert(sizeof(struct dualshock4_touch_report) == 9);
  359. /* Main DualShock4 input report excluding any BT/USB specific headers. */
  360. struct dualshock4_input_report_common {
  361. uint8_t x, y;
  362. uint8_t rx, ry;
  363. uint8_t buttons[3];
  364. uint8_t z, rz;
  365. /* Motion sensors */
  366. __le16 sensor_timestamp;
  367. uint8_t sensor_temperature;
  368. __le16 gyro[3]; /* x, y, z */
  369. __le16 accel[3]; /* x, y, z */
  370. uint8_t reserved2[5];
  371. uint8_t status[2];
  372. uint8_t reserved3;
  373. } __packed;
  374. static_assert(sizeof(struct dualshock4_input_report_common) == 32);
  375. struct dualshock4_input_report_usb {
  376. uint8_t report_id; /* 0x01 */
  377. struct dualshock4_input_report_common common;
  378. uint8_t num_touch_reports;
  379. struct dualshock4_touch_report touch_reports[3];
  380. uint8_t reserved[3];
  381. } __packed;
  382. static_assert(sizeof(struct dualshock4_input_report_usb) == DS4_INPUT_REPORT_USB_SIZE);
  383. struct dualshock4_input_report_bt {
  384. uint8_t report_id; /* 0x11 */
  385. uint8_t reserved[2];
  386. struct dualshock4_input_report_common common;
  387. uint8_t num_touch_reports;
  388. struct dualshock4_touch_report touch_reports[4]; /* BT has 4 compared to 3 for USB */
  389. uint8_t reserved2[2];
  390. __le32 crc32;
  391. } __packed;
  392. static_assert(sizeof(struct dualshock4_input_report_bt) == DS4_INPUT_REPORT_BT_SIZE);
  393. /* Common data between Bluetooth and USB DualShock4 output reports. */
  394. struct dualshock4_output_report_common {
  395. uint8_t valid_flag0;
  396. uint8_t valid_flag1;
  397. uint8_t reserved;
  398. uint8_t motor_right;
  399. uint8_t motor_left;
  400. uint8_t lightbar_red;
  401. uint8_t lightbar_green;
  402. uint8_t lightbar_blue;
  403. uint8_t lightbar_blink_on;
  404. uint8_t lightbar_blink_off;
  405. } __packed;
  406. struct dualshock4_output_report_usb {
  407. uint8_t report_id; /* 0x5 */
  408. struct dualshock4_output_report_common common;
  409. uint8_t reserved[21];
  410. } __packed;
  411. static_assert(sizeof(struct dualshock4_output_report_usb) == DS4_OUTPUT_REPORT_USB_SIZE);
  412. struct dualshock4_output_report_bt {
  413. uint8_t report_id; /* 0x11 */
  414. uint8_t hw_control;
  415. uint8_t audio_control;
  416. struct dualshock4_output_report_common common;
  417. uint8_t reserved[61];
  418. __le32 crc32;
  419. } __packed;
  420. static_assert(sizeof(struct dualshock4_output_report_bt) == DS4_OUTPUT_REPORT_BT_SIZE);
  421. /*
  422. * The DualShock4 has a main output report used to control most features. It is
  423. * largely the same between Bluetooth and USB except for different headers and CRC.
  424. * This structure hide the differences between the two to simplify sending output reports.
  425. */
  426. struct dualshock4_output_report {
  427. uint8_t *data; /* Start of data */
  428. uint8_t len; /* Size of output report */
  429. /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
  430. struct dualshock4_output_report_bt *bt;
  431. /* Points to USB data payload in case for a USB report else NULL. */
  432. struct dualshock4_output_report_usb *usb;
  433. /* Points to common section of report, so past any headers. */
  434. struct dualshock4_output_report_common *common;
  435. };
  436. /*
  437. * Common gamepad buttons across DualShock 3 / 4 and DualSense.
  438. * Note: for device with a touchpad, touchpad button is not included
  439. * as it will be part of the touchpad device.
  440. */
  441. static const int ps_gamepad_buttons[] = {
  442. BTN_WEST, /* Square */
  443. BTN_NORTH, /* Triangle */
  444. BTN_EAST, /* Circle */
  445. BTN_SOUTH, /* Cross */
  446. BTN_TL, /* L1 */
  447. BTN_TR, /* R1 */
  448. BTN_TL2, /* L2 */
  449. BTN_TR2, /* R2 */
  450. BTN_SELECT, /* Create (PS5) / Share (PS4) */
  451. BTN_START, /* Option */
  452. BTN_THUMBL, /* L3 */
  453. BTN_THUMBR, /* R3 */
  454. BTN_MODE, /* PS Home */
  455. };
  456. static const struct {int x; int y; } ps_gamepad_hat_mapping[] = {
  457. {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
  458. {0, 0},
  459. };
  460. static int dualshock4_get_calibration_data(struct dualshock4 *ds4);
  461. static inline void dualsense_schedule_work(struct dualsense *ds);
  462. static inline void dualshock4_schedule_work(struct dualshock4 *ds4);
  463. static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue);
  464. static void dualshock4_set_default_lightbar_colors(struct dualshock4 *ds4);
  465. /*
  466. * Add a new ps_device to ps_devices if it doesn't exist.
  467. * Return error on duplicate device, which can happen if the same
  468. * device is connected using both Bluetooth and USB.
  469. */
  470. static int ps_devices_list_add(struct ps_device *dev)
  471. {
  472. struct ps_device *entry;
  473. mutex_lock(&ps_devices_lock);
  474. list_for_each_entry(entry, &ps_devices_list, list) {
  475. if (!memcmp(entry->mac_address, dev->mac_address, sizeof(dev->mac_address))) {
  476. hid_err(dev->hdev, "Duplicate device found for MAC address %pMR.\n",
  477. dev->mac_address);
  478. mutex_unlock(&ps_devices_lock);
  479. return -EEXIST;
  480. }
  481. }
  482. list_add_tail(&dev->list, &ps_devices_list);
  483. mutex_unlock(&ps_devices_lock);
  484. return 0;
  485. }
  486. static int ps_devices_list_remove(struct ps_device *dev)
  487. {
  488. mutex_lock(&ps_devices_lock);
  489. list_del(&dev->list);
  490. mutex_unlock(&ps_devices_lock);
  491. return 0;
  492. }
  493. static int ps_device_set_player_id(struct ps_device *dev)
  494. {
  495. int ret = ida_alloc(&ps_player_id_allocator, GFP_KERNEL);
  496. if (ret < 0)
  497. return ret;
  498. dev->player_id = ret;
  499. return 0;
  500. }
  501. static void ps_device_release_player_id(struct ps_device *dev)
  502. {
  503. ida_free(&ps_player_id_allocator, dev->player_id);
  504. dev->player_id = U32_MAX;
  505. }
  506. static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix)
  507. {
  508. struct input_dev *input_dev;
  509. input_dev = devm_input_allocate_device(&hdev->dev);
  510. if (!input_dev)
  511. return ERR_PTR(-ENOMEM);
  512. input_dev->id.bustype = hdev->bus;
  513. input_dev->id.vendor = hdev->vendor;
  514. input_dev->id.product = hdev->product;
  515. input_dev->id.version = hdev->version;
  516. input_dev->uniq = hdev->uniq;
  517. if (name_suffix) {
  518. input_dev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", hdev->name,
  519. name_suffix);
  520. if (!input_dev->name)
  521. return ERR_PTR(-ENOMEM);
  522. } else {
  523. input_dev->name = hdev->name;
  524. }
  525. input_set_drvdata(input_dev, hdev);
  526. return input_dev;
  527. }
  528. static enum power_supply_property ps_power_supply_props[] = {
  529. POWER_SUPPLY_PROP_STATUS,
  530. POWER_SUPPLY_PROP_PRESENT,
  531. POWER_SUPPLY_PROP_CAPACITY,
  532. POWER_SUPPLY_PROP_SCOPE,
  533. };
  534. static int ps_battery_get_property(struct power_supply *psy,
  535. enum power_supply_property psp,
  536. union power_supply_propval *val)
  537. {
  538. struct ps_device *dev = power_supply_get_drvdata(psy);
  539. uint8_t battery_capacity;
  540. int battery_status;
  541. unsigned long flags;
  542. int ret = 0;
  543. spin_lock_irqsave(&dev->lock, flags);
  544. battery_capacity = dev->battery_capacity;
  545. battery_status = dev->battery_status;
  546. spin_unlock_irqrestore(&dev->lock, flags);
  547. switch (psp) {
  548. case POWER_SUPPLY_PROP_STATUS:
  549. val->intval = battery_status;
  550. break;
  551. case POWER_SUPPLY_PROP_PRESENT:
  552. val->intval = 1;
  553. break;
  554. case POWER_SUPPLY_PROP_CAPACITY:
  555. val->intval = battery_capacity;
  556. break;
  557. case POWER_SUPPLY_PROP_SCOPE:
  558. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  559. break;
  560. default:
  561. ret = -EINVAL;
  562. break;
  563. }
  564. return ret;
  565. }
  566. static int ps_device_register_battery(struct ps_device *dev)
  567. {
  568. struct power_supply *battery;
  569. struct power_supply_config battery_cfg = { .drv_data = dev };
  570. int ret;
  571. dev->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  572. dev->battery_desc.properties = ps_power_supply_props;
  573. dev->battery_desc.num_properties = ARRAY_SIZE(ps_power_supply_props);
  574. dev->battery_desc.get_property = ps_battery_get_property;
  575. dev->battery_desc.name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
  576. "ps-controller-battery-%pMR", dev->mac_address);
  577. if (!dev->battery_desc.name)
  578. return -ENOMEM;
  579. battery = devm_power_supply_register(&dev->hdev->dev, &dev->battery_desc, &battery_cfg);
  580. if (IS_ERR(battery)) {
  581. ret = PTR_ERR(battery);
  582. hid_err(dev->hdev, "Unable to register battery device: %d\n", ret);
  583. return ret;
  584. }
  585. dev->battery = battery;
  586. ret = power_supply_powers(dev->battery, &dev->hdev->dev);
  587. if (ret) {
  588. hid_err(dev->hdev, "Unable to activate battery device: %d\n", ret);
  589. return ret;
  590. }
  591. return 0;
  592. }
  593. /* Compute crc32 of HID data and compare against expected CRC. */
  594. static bool ps_check_crc32(uint8_t seed, uint8_t *data, size_t len, uint32_t report_crc)
  595. {
  596. uint32_t crc;
  597. crc = crc32_le(0xFFFFFFFF, &seed, 1);
  598. crc = ~crc32_le(crc, data, len);
  599. return crc == report_crc;
  600. }
  601. static struct input_dev *ps_gamepad_create(struct hid_device *hdev,
  602. int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
  603. {
  604. struct input_dev *gamepad;
  605. unsigned int i;
  606. int ret;
  607. gamepad = ps_allocate_input_dev(hdev, NULL);
  608. if (IS_ERR(gamepad))
  609. return ERR_CAST(gamepad);
  610. input_set_abs_params(gamepad, ABS_X, 0, 255, 0, 0);
  611. input_set_abs_params(gamepad, ABS_Y, 0, 255, 0, 0);
  612. input_set_abs_params(gamepad, ABS_Z, 0, 255, 0, 0);
  613. input_set_abs_params(gamepad, ABS_RX, 0, 255, 0, 0);
  614. input_set_abs_params(gamepad, ABS_RY, 0, 255, 0, 0);
  615. input_set_abs_params(gamepad, ABS_RZ, 0, 255, 0, 0);
  616. input_set_abs_params(gamepad, ABS_HAT0X, -1, 1, 0, 0);
  617. input_set_abs_params(gamepad, ABS_HAT0Y, -1, 1, 0, 0);
  618. for (i = 0; i < ARRAY_SIZE(ps_gamepad_buttons); i++)
  619. input_set_capability(gamepad, EV_KEY, ps_gamepad_buttons[i]);
  620. #if IS_ENABLED(CONFIG_PLAYSTATION_FF)
  621. if (play_effect) {
  622. input_set_capability(gamepad, EV_FF, FF_RUMBLE);
  623. input_ff_create_memless(gamepad, NULL, play_effect);
  624. }
  625. #endif
  626. ret = input_register_device(gamepad);
  627. if (ret)
  628. return ERR_PTR(ret);
  629. return gamepad;
  630. }
  631. static int ps_get_report(struct hid_device *hdev, uint8_t report_id, uint8_t *buf, size_t size,
  632. bool check_crc)
  633. {
  634. int ret;
  635. ret = hid_hw_raw_request(hdev, report_id, buf, size, HID_FEATURE_REPORT,
  636. HID_REQ_GET_REPORT);
  637. if (ret < 0) {
  638. hid_err(hdev, "Failed to retrieve feature with reportID %d: %d\n", report_id, ret);
  639. return ret;
  640. }
  641. if (ret != size) {
  642. hid_err(hdev, "Invalid byte count transferred, expected %zu got %d\n", size, ret);
  643. return -EINVAL;
  644. }
  645. if (buf[0] != report_id) {
  646. hid_err(hdev, "Invalid reportID received, expected %d got %d\n", report_id, buf[0]);
  647. return -EINVAL;
  648. }
  649. if (hdev->bus == BUS_BLUETOOTH && check_crc) {
  650. /* Last 4 bytes contains crc32. */
  651. uint8_t crc_offset = size - 4;
  652. uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]);
  653. if (!ps_check_crc32(PS_FEATURE_CRC32_SEED, buf, crc_offset, report_crc)) {
  654. hid_err(hdev, "CRC check failed for reportID=%d\n", report_id);
  655. return -EILSEQ;
  656. }
  657. }
  658. return 0;
  659. }
  660. static int ps_led_register(struct ps_device *ps_dev, struct led_classdev *led,
  661. const struct ps_led_info *led_info)
  662. {
  663. int ret;
  664. if (led_info->name) {
  665. led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL,
  666. "%s:%s:%s", ps_dev->input_dev_name, led_info->color, led_info->name);
  667. } else {
  668. /* Backwards compatible mode for hid-sony, but not compliant with LED class spec. */
  669. led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL,
  670. "%s:%s", ps_dev->input_dev_name, led_info->color);
  671. }
  672. if (!led->name)
  673. return -ENOMEM;
  674. led->brightness = 0;
  675. led->max_brightness = led_info->max_brightness;
  676. led->flags = LED_CORE_SUSPENDRESUME;
  677. led->brightness_get = led_info->brightness_get;
  678. led->brightness_set_blocking = led_info->brightness_set;
  679. led->blink_set = led_info->blink_set;
  680. ret = devm_led_classdev_register(&ps_dev->hdev->dev, led);
  681. if (ret) {
  682. hid_err(ps_dev->hdev, "Failed to register LED %s: %d\n", led_info->name, ret);
  683. return ret;
  684. }
  685. return 0;
  686. }
  687. /* Register a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */
  688. static int ps_lightbar_register(struct ps_device *ps_dev, struct led_classdev_mc *lightbar_mc_dev,
  689. int (*brightness_set)(struct led_classdev *, enum led_brightness))
  690. {
  691. struct hid_device *hdev = ps_dev->hdev;
  692. struct mc_subled *mc_led_info;
  693. struct led_classdev *led_cdev;
  694. int ret;
  695. mc_led_info = devm_kmalloc_array(&hdev->dev, 3, sizeof(*mc_led_info),
  696. GFP_KERNEL | __GFP_ZERO);
  697. if (!mc_led_info)
  698. return -ENOMEM;
  699. mc_led_info[0].color_index = LED_COLOR_ID_RED;
  700. mc_led_info[1].color_index = LED_COLOR_ID_GREEN;
  701. mc_led_info[2].color_index = LED_COLOR_ID_BLUE;
  702. lightbar_mc_dev->subled_info = mc_led_info;
  703. lightbar_mc_dev->num_colors = 3;
  704. led_cdev = &lightbar_mc_dev->led_cdev;
  705. led_cdev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s:rgb:indicator",
  706. ps_dev->input_dev_name);
  707. if (!led_cdev->name)
  708. return -ENOMEM;
  709. led_cdev->brightness = 255;
  710. led_cdev->max_brightness = 255;
  711. led_cdev->brightness_set_blocking = brightness_set;
  712. ret = devm_led_classdev_multicolor_register(&hdev->dev, lightbar_mc_dev);
  713. if (ret < 0) {
  714. hid_err(hdev, "Cannot register multicolor LED device\n");
  715. return ret;
  716. }
  717. return 0;
  718. }
  719. static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res,
  720. int gyro_range, int gyro_res)
  721. {
  722. struct input_dev *sensors;
  723. int ret;
  724. sensors = ps_allocate_input_dev(hdev, "Motion Sensors");
  725. if (IS_ERR(sensors))
  726. return ERR_CAST(sensors);
  727. __set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit);
  728. __set_bit(EV_MSC, sensors->evbit);
  729. __set_bit(MSC_TIMESTAMP, sensors->mscbit);
  730. /* Accelerometer */
  731. input_set_abs_params(sensors, ABS_X, -accel_range, accel_range, 16, 0);
  732. input_set_abs_params(sensors, ABS_Y, -accel_range, accel_range, 16, 0);
  733. input_set_abs_params(sensors, ABS_Z, -accel_range, accel_range, 16, 0);
  734. input_abs_set_res(sensors, ABS_X, accel_res);
  735. input_abs_set_res(sensors, ABS_Y, accel_res);
  736. input_abs_set_res(sensors, ABS_Z, accel_res);
  737. /* Gyroscope */
  738. input_set_abs_params(sensors, ABS_RX, -gyro_range, gyro_range, 16, 0);
  739. input_set_abs_params(sensors, ABS_RY, -gyro_range, gyro_range, 16, 0);
  740. input_set_abs_params(sensors, ABS_RZ, -gyro_range, gyro_range, 16, 0);
  741. input_abs_set_res(sensors, ABS_RX, gyro_res);
  742. input_abs_set_res(sensors, ABS_RY, gyro_res);
  743. input_abs_set_res(sensors, ABS_RZ, gyro_res);
  744. ret = input_register_device(sensors);
  745. if (ret)
  746. return ERR_PTR(ret);
  747. return sensors;
  748. }
  749. static struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width, int height,
  750. unsigned int num_contacts)
  751. {
  752. struct input_dev *touchpad;
  753. int ret;
  754. touchpad = ps_allocate_input_dev(hdev, "Touchpad");
  755. if (IS_ERR(touchpad))
  756. return ERR_CAST(touchpad);
  757. /* Map button underneath touchpad to BTN_LEFT. */
  758. input_set_capability(touchpad, EV_KEY, BTN_LEFT);
  759. __set_bit(INPUT_PROP_BUTTONPAD, touchpad->propbit);
  760. input_set_abs_params(touchpad, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
  761. input_set_abs_params(touchpad, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
  762. ret = input_mt_init_slots(touchpad, num_contacts, INPUT_MT_POINTER);
  763. if (ret)
  764. return ERR_PTR(ret);
  765. ret = input_register_device(touchpad);
  766. if (ret)
  767. return ERR_PTR(ret);
  768. return touchpad;
  769. }
  770. static ssize_t firmware_version_show(struct device *dev,
  771. struct device_attribute
  772. *attr, char *buf)
  773. {
  774. struct hid_device *hdev = to_hid_device(dev);
  775. struct ps_device *ps_dev = hid_get_drvdata(hdev);
  776. return sysfs_emit(buf, "0x%08x\n", ps_dev->fw_version);
  777. }
  778. static DEVICE_ATTR_RO(firmware_version);
  779. static ssize_t hardware_version_show(struct device *dev,
  780. struct device_attribute
  781. *attr, char *buf)
  782. {
  783. struct hid_device *hdev = to_hid_device(dev);
  784. struct ps_device *ps_dev = hid_get_drvdata(hdev);
  785. return sysfs_emit(buf, "0x%08x\n", ps_dev->hw_version);
  786. }
  787. static DEVICE_ATTR_RO(hardware_version);
  788. static struct attribute *ps_device_attrs[] = {
  789. &dev_attr_firmware_version.attr,
  790. &dev_attr_hardware_version.attr,
  791. NULL
  792. };
  793. ATTRIBUTE_GROUPS(ps_device);
  794. static int dualsense_get_calibration_data(struct dualsense *ds)
  795. {
  796. struct hid_device *hdev = ds->base.hdev;
  797. short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
  798. short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
  799. short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
  800. short gyro_speed_plus, gyro_speed_minus;
  801. short acc_x_plus, acc_x_minus;
  802. short acc_y_plus, acc_y_minus;
  803. short acc_z_plus, acc_z_minus;
  804. int speed_2x;
  805. int range_2g;
  806. int ret = 0;
  807. int i;
  808. uint8_t *buf;
  809. buf = kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL);
  810. if (!buf)
  811. return -ENOMEM;
  812. ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_CALIBRATION, buf,
  813. DS_FEATURE_REPORT_CALIBRATION_SIZE, true);
  814. if (ret) {
  815. hid_err(ds->base.hdev, "Failed to retrieve DualSense calibration info: %d\n", ret);
  816. goto err_free;
  817. }
  818. gyro_pitch_bias = get_unaligned_le16(&buf[1]);
  819. gyro_yaw_bias = get_unaligned_le16(&buf[3]);
  820. gyro_roll_bias = get_unaligned_le16(&buf[5]);
  821. gyro_pitch_plus = get_unaligned_le16(&buf[7]);
  822. gyro_pitch_minus = get_unaligned_le16(&buf[9]);
  823. gyro_yaw_plus = get_unaligned_le16(&buf[11]);
  824. gyro_yaw_minus = get_unaligned_le16(&buf[13]);
  825. gyro_roll_plus = get_unaligned_le16(&buf[15]);
  826. gyro_roll_minus = get_unaligned_le16(&buf[17]);
  827. gyro_speed_plus = get_unaligned_le16(&buf[19]);
  828. gyro_speed_minus = get_unaligned_le16(&buf[21]);
  829. acc_x_plus = get_unaligned_le16(&buf[23]);
  830. acc_x_minus = get_unaligned_le16(&buf[25]);
  831. acc_y_plus = get_unaligned_le16(&buf[27]);
  832. acc_y_minus = get_unaligned_le16(&buf[29]);
  833. acc_z_plus = get_unaligned_le16(&buf[31]);
  834. acc_z_minus = get_unaligned_le16(&buf[33]);
  835. /*
  836. * Set gyroscope calibration and normalization parameters.
  837. * Data values will be normalized to 1/DS_GYRO_RES_PER_DEG_S degree/s.
  838. */
  839. speed_2x = (gyro_speed_plus + gyro_speed_minus);
  840. ds->gyro_calib_data[0].abs_code = ABS_RX;
  841. ds->gyro_calib_data[0].bias = 0;
  842. ds->gyro_calib_data[0].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
  843. ds->gyro_calib_data[0].sens_denom = abs(gyro_pitch_plus - gyro_pitch_bias) +
  844. abs(gyro_pitch_minus - gyro_pitch_bias);
  845. ds->gyro_calib_data[1].abs_code = ABS_RY;
  846. ds->gyro_calib_data[1].bias = 0;
  847. ds->gyro_calib_data[1].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
  848. ds->gyro_calib_data[1].sens_denom = abs(gyro_yaw_plus - gyro_yaw_bias) +
  849. abs(gyro_yaw_minus - gyro_yaw_bias);
  850. ds->gyro_calib_data[2].abs_code = ABS_RZ;
  851. ds->gyro_calib_data[2].bias = 0;
  852. ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
  853. ds->gyro_calib_data[2].sens_denom = abs(gyro_roll_plus - gyro_roll_bias) +
  854. abs(gyro_roll_minus - gyro_roll_bias);
  855. /*
  856. * Sanity check gyro calibration data. This is needed to prevent crashes
  857. * during report handling of virtual, clone or broken devices not implementing
  858. * calibration data properly.
  859. */
  860. for (i = 0; i < ARRAY_SIZE(ds->gyro_calib_data); i++) {
  861. if (ds->gyro_calib_data[i].sens_denom == 0) {
  862. hid_warn(hdev, "Invalid gyro calibration data for axis (%d), disabling calibration.",
  863. ds->gyro_calib_data[i].abs_code);
  864. ds->gyro_calib_data[i].bias = 0;
  865. ds->gyro_calib_data[i].sens_numer = DS_GYRO_RANGE;
  866. ds->gyro_calib_data[i].sens_denom = S16_MAX;
  867. }
  868. }
  869. /*
  870. * Set accelerometer calibration and normalization parameters.
  871. * Data values will be normalized to 1/DS_ACC_RES_PER_G g.
  872. */
  873. range_2g = acc_x_plus - acc_x_minus;
  874. ds->accel_calib_data[0].abs_code = ABS_X;
  875. ds->accel_calib_data[0].bias = acc_x_plus - range_2g / 2;
  876. ds->accel_calib_data[0].sens_numer = 2*DS_ACC_RES_PER_G;
  877. ds->accel_calib_data[0].sens_denom = range_2g;
  878. range_2g = acc_y_plus - acc_y_minus;
  879. ds->accel_calib_data[1].abs_code = ABS_Y;
  880. ds->accel_calib_data[1].bias = acc_y_plus - range_2g / 2;
  881. ds->accel_calib_data[1].sens_numer = 2*DS_ACC_RES_PER_G;
  882. ds->accel_calib_data[1].sens_denom = range_2g;
  883. range_2g = acc_z_plus - acc_z_minus;
  884. ds->accel_calib_data[2].abs_code = ABS_Z;
  885. ds->accel_calib_data[2].bias = acc_z_plus - range_2g / 2;
  886. ds->accel_calib_data[2].sens_numer = 2*DS_ACC_RES_PER_G;
  887. ds->accel_calib_data[2].sens_denom = range_2g;
  888. /*
  889. * Sanity check accelerometer calibration data. This is needed to prevent crashes
  890. * during report handling of virtual, clone or broken devices not implementing calibration
  891. * data properly.
  892. */
  893. for (i = 0; i < ARRAY_SIZE(ds->accel_calib_data); i++) {
  894. if (ds->accel_calib_data[i].sens_denom == 0) {
  895. hid_warn(hdev, "Invalid accelerometer calibration data for axis (%d), disabling calibration.",
  896. ds->accel_calib_data[i].abs_code);
  897. ds->accel_calib_data[i].bias = 0;
  898. ds->accel_calib_data[i].sens_numer = DS_ACC_RANGE;
  899. ds->accel_calib_data[i].sens_denom = S16_MAX;
  900. }
  901. }
  902. err_free:
  903. kfree(buf);
  904. return ret;
  905. }
  906. static int dualsense_get_firmware_info(struct dualsense *ds)
  907. {
  908. uint8_t *buf;
  909. int ret;
  910. buf = kzalloc(DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL);
  911. if (!buf)
  912. return -ENOMEM;
  913. ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_FIRMWARE_INFO, buf,
  914. DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, true);
  915. if (ret) {
  916. hid_err(ds->base.hdev, "Failed to retrieve DualSense firmware info: %d\n", ret);
  917. goto err_free;
  918. }
  919. ds->base.hw_version = get_unaligned_le32(&buf[24]);
  920. ds->base.fw_version = get_unaligned_le32(&buf[28]);
  921. /* Update version is some kind of feature version. It is distinct from
  922. * the firmware version as there can be many different variations of a
  923. * controller over time with the same physical shell, but with different
  924. * PCBs and other internal changes. The update version (internal name) is
  925. * used as a means to detect what features are available and change behavior.
  926. * Note: the version is different between DualSense and DualSense Edge.
  927. */
  928. ds->update_version = get_unaligned_le16(&buf[44]);
  929. err_free:
  930. kfree(buf);
  931. return ret;
  932. }
  933. static int dualsense_get_mac_address(struct dualsense *ds)
  934. {
  935. uint8_t *buf;
  936. int ret = 0;
  937. buf = kzalloc(DS_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL);
  938. if (!buf)
  939. return -ENOMEM;
  940. ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_PAIRING_INFO, buf,
  941. DS_FEATURE_REPORT_PAIRING_INFO_SIZE, true);
  942. if (ret) {
  943. hid_err(ds->base.hdev, "Failed to retrieve DualSense pairing info: %d\n", ret);
  944. goto err_free;
  945. }
  946. memcpy(ds->base.mac_address, &buf[1], sizeof(ds->base.mac_address));
  947. err_free:
  948. kfree(buf);
  949. return ret;
  950. }
  951. static int dualsense_lightbar_set_brightness(struct led_classdev *cdev,
  952. enum led_brightness brightness)
  953. {
  954. struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
  955. struct dualsense *ds = container_of(mc_cdev, struct dualsense, lightbar);
  956. uint8_t red, green, blue;
  957. led_mc_calc_color_components(mc_cdev, brightness);
  958. red = mc_cdev->subled_info[0].brightness;
  959. green = mc_cdev->subled_info[1].brightness;
  960. blue = mc_cdev->subled_info[2].brightness;
  961. dualsense_set_lightbar(ds, red, green, blue);
  962. return 0;
  963. }
  964. static enum led_brightness dualsense_player_led_get_brightness(struct led_classdev *led)
  965. {
  966. struct hid_device *hdev = to_hid_device(led->dev->parent);
  967. struct dualsense *ds = hid_get_drvdata(hdev);
  968. return !!(ds->player_leds_state & BIT(led - ds->player_leds));
  969. }
  970. static int dualsense_player_led_set_brightness(struct led_classdev *led, enum led_brightness value)
  971. {
  972. struct hid_device *hdev = to_hid_device(led->dev->parent);
  973. struct dualsense *ds = hid_get_drvdata(hdev);
  974. unsigned long flags;
  975. unsigned int led_index;
  976. spin_lock_irqsave(&ds->base.lock, flags);
  977. led_index = led - ds->player_leds;
  978. if (value == LED_OFF)
  979. ds->player_leds_state &= ~BIT(led_index);
  980. else
  981. ds->player_leds_state |= BIT(led_index);
  982. ds->update_player_leds = true;
  983. spin_unlock_irqrestore(&ds->base.lock, flags);
  984. dualsense_schedule_work(ds);
  985. return 0;
  986. }
  987. static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp,
  988. void *buf)
  989. {
  990. struct hid_device *hdev = ds->base.hdev;
  991. if (hdev->bus == BUS_BLUETOOTH) {
  992. struct dualsense_output_report_bt *bt = buf;
  993. memset(bt, 0, sizeof(*bt));
  994. bt->report_id = DS_OUTPUT_REPORT_BT;
  995. bt->tag = DS_OUTPUT_TAG; /* Tag must be set. Exact meaning is unclear. */
  996. /*
  997. * Highest 4-bit is a sequence number, which needs to be increased
  998. * every report. Lowest 4-bit is tag and can be zero for now.
  999. */
  1000. bt->seq_tag = (ds->output_seq << 4) | 0x0;
  1001. if (++ds->output_seq == 16)
  1002. ds->output_seq = 0;
  1003. rp->data = buf;
  1004. rp->len = sizeof(*bt);
  1005. rp->bt = bt;
  1006. rp->usb = NULL;
  1007. rp->common = &bt->common;
  1008. } else { /* USB */
  1009. struct dualsense_output_report_usb *usb = buf;
  1010. memset(usb, 0, sizeof(*usb));
  1011. usb->report_id = DS_OUTPUT_REPORT_USB;
  1012. rp->data = buf;
  1013. rp->len = sizeof(*usb);
  1014. rp->bt = NULL;
  1015. rp->usb = usb;
  1016. rp->common = &usb->common;
  1017. }
  1018. }
  1019. static inline void dualsense_schedule_work(struct dualsense *ds)
  1020. {
  1021. unsigned long flags;
  1022. spin_lock_irqsave(&ds->base.lock, flags);
  1023. if (ds->output_worker_initialized)
  1024. schedule_work(&ds->output_worker);
  1025. spin_unlock_irqrestore(&ds->base.lock, flags);
  1026. }
  1027. /*
  1028. * Helper function to send DualSense output reports. Applies a CRC at the end of a report
  1029. * for Bluetooth reports.
  1030. */
  1031. static void dualsense_send_output_report(struct dualsense *ds,
  1032. struct dualsense_output_report *report)
  1033. {
  1034. struct hid_device *hdev = ds->base.hdev;
  1035. /* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */
  1036. if (report->bt) {
  1037. uint32_t crc;
  1038. uint8_t seed = PS_OUTPUT_CRC32_SEED;
  1039. crc = crc32_le(0xFFFFFFFF, &seed, 1);
  1040. crc = ~crc32_le(crc, report->data, report->len - 4);
  1041. report->bt->crc32 = cpu_to_le32(crc);
  1042. }
  1043. hid_hw_output_report(hdev, report->data, report->len);
  1044. }
  1045. static void dualsense_output_worker(struct work_struct *work)
  1046. {
  1047. struct dualsense *ds = container_of(work, struct dualsense, output_worker);
  1048. struct dualsense_output_report report;
  1049. struct dualsense_output_report_common *common;
  1050. unsigned long flags;
  1051. dualsense_init_output_report(ds, &report, ds->output_report_dmabuf);
  1052. common = report.common;
  1053. spin_lock_irqsave(&ds->base.lock, flags);
  1054. if (ds->update_rumble) {
  1055. /* Select classic rumble style haptics and enable it. */
  1056. common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT;
  1057. if (ds->use_vibration_v2)
  1058. common->valid_flag2 |= DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2;
  1059. else
  1060. common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION;
  1061. common->motor_left = ds->motor_left;
  1062. common->motor_right = ds->motor_right;
  1063. ds->update_rumble = false;
  1064. }
  1065. if (ds->update_lightbar) {
  1066. common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE;
  1067. common->lightbar_red = ds->lightbar_red;
  1068. common->lightbar_green = ds->lightbar_green;
  1069. common->lightbar_blue = ds->lightbar_blue;
  1070. ds->update_lightbar = false;
  1071. }
  1072. if (ds->update_player_leds) {
  1073. common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE;
  1074. common->player_leds = ds->player_leds_state;
  1075. ds->update_player_leds = false;
  1076. }
  1077. if (ds->update_mic_mute) {
  1078. common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE;
  1079. common->mute_button_led = ds->mic_muted;
  1080. if (ds->mic_muted) {
  1081. /* Disable microphone */
  1082. common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE;
  1083. common->power_save_control |= DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
  1084. } else {
  1085. /* Enable microphone */
  1086. common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE;
  1087. common->power_save_control &= ~DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
  1088. }
  1089. ds->update_mic_mute = false;
  1090. }
  1091. spin_unlock_irqrestore(&ds->base.lock, flags);
  1092. dualsense_send_output_report(ds, &report);
  1093. }
  1094. static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *report,
  1095. u8 *data, int size)
  1096. {
  1097. struct hid_device *hdev = ps_dev->hdev;
  1098. struct dualsense *ds = container_of(ps_dev, struct dualsense, base);
  1099. struct dualsense_input_report *ds_report;
  1100. uint8_t battery_data, battery_capacity, charging_status, value;
  1101. int battery_status;
  1102. uint32_t sensor_timestamp;
  1103. bool btn_mic_state;
  1104. unsigned long flags;
  1105. int i;
  1106. /*
  1107. * DualSense in USB uses the full HID report for reportID 1, but
  1108. * Bluetooth uses a minimal HID report for reportID 1 and reports
  1109. * the full report using reportID 49.
  1110. */
  1111. if (hdev->bus == BUS_USB && report->id == DS_INPUT_REPORT_USB &&
  1112. size == DS_INPUT_REPORT_USB_SIZE) {
  1113. ds_report = (struct dualsense_input_report *)&data[1];
  1114. } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS_INPUT_REPORT_BT &&
  1115. size == DS_INPUT_REPORT_BT_SIZE) {
  1116. /* Last 4 bytes of input report contain crc32 */
  1117. uint32_t report_crc = get_unaligned_le32(&data[size - 4]);
  1118. if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) {
  1119. hid_err(hdev, "DualSense input CRC's check failed\n");
  1120. return -EILSEQ;
  1121. }
  1122. ds_report = (struct dualsense_input_report *)&data[2];
  1123. } else {
  1124. hid_err(hdev, "Unhandled reportID=%d\n", report->id);
  1125. return -1;
  1126. }
  1127. input_report_abs(ds->gamepad, ABS_X, ds_report->x);
  1128. input_report_abs(ds->gamepad, ABS_Y, ds_report->y);
  1129. input_report_abs(ds->gamepad, ABS_RX, ds_report->rx);
  1130. input_report_abs(ds->gamepad, ABS_RY, ds_report->ry);
  1131. input_report_abs(ds->gamepad, ABS_Z, ds_report->z);
  1132. input_report_abs(ds->gamepad, ABS_RZ, ds_report->rz);
  1133. value = ds_report->buttons[0] & DS_BUTTONS0_HAT_SWITCH;
  1134. if (value >= ARRAY_SIZE(ps_gamepad_hat_mapping))
  1135. value = 8; /* center */
  1136. input_report_abs(ds->gamepad, ABS_HAT0X, ps_gamepad_hat_mapping[value].x);
  1137. input_report_abs(ds->gamepad, ABS_HAT0Y, ps_gamepad_hat_mapping[value].y);
  1138. input_report_key(ds->gamepad, BTN_WEST, ds_report->buttons[0] & DS_BUTTONS0_SQUARE);
  1139. input_report_key(ds->gamepad, BTN_SOUTH, ds_report->buttons[0] & DS_BUTTONS0_CROSS);
  1140. input_report_key(ds->gamepad, BTN_EAST, ds_report->buttons[0] & DS_BUTTONS0_CIRCLE);
  1141. input_report_key(ds->gamepad, BTN_NORTH, ds_report->buttons[0] & DS_BUTTONS0_TRIANGLE);
  1142. input_report_key(ds->gamepad, BTN_TL, ds_report->buttons[1] & DS_BUTTONS1_L1);
  1143. input_report_key(ds->gamepad, BTN_TR, ds_report->buttons[1] & DS_BUTTONS1_R1);
  1144. input_report_key(ds->gamepad, BTN_TL2, ds_report->buttons[1] & DS_BUTTONS1_L2);
  1145. input_report_key(ds->gamepad, BTN_TR2, ds_report->buttons[1] & DS_BUTTONS1_R2);
  1146. input_report_key(ds->gamepad, BTN_SELECT, ds_report->buttons[1] & DS_BUTTONS1_CREATE);
  1147. input_report_key(ds->gamepad, BTN_START, ds_report->buttons[1] & DS_BUTTONS1_OPTIONS);
  1148. input_report_key(ds->gamepad, BTN_THUMBL, ds_report->buttons[1] & DS_BUTTONS1_L3);
  1149. input_report_key(ds->gamepad, BTN_THUMBR, ds_report->buttons[1] & DS_BUTTONS1_R3);
  1150. input_report_key(ds->gamepad, BTN_MODE, ds_report->buttons[2] & DS_BUTTONS2_PS_HOME);
  1151. input_sync(ds->gamepad);
  1152. /*
  1153. * The DualSense has an internal microphone, which can be muted through a mute button
  1154. * on the device. The driver is expected to read the button state and program the device
  1155. * to mute/unmute audio at the hardware level.
  1156. */
  1157. btn_mic_state = !!(ds_report->buttons[2] & DS_BUTTONS2_MIC_MUTE);
  1158. if (btn_mic_state && !ds->last_btn_mic_state) {
  1159. spin_lock_irqsave(&ps_dev->lock, flags);
  1160. ds->update_mic_mute = true;
  1161. ds->mic_muted = !ds->mic_muted; /* toggle */
  1162. spin_unlock_irqrestore(&ps_dev->lock, flags);
  1163. /* Schedule updating of microphone state at hardware level. */
  1164. dualsense_schedule_work(ds);
  1165. }
  1166. ds->last_btn_mic_state = btn_mic_state;
  1167. /* Parse and calibrate gyroscope data. */
  1168. for (i = 0; i < ARRAY_SIZE(ds_report->gyro); i++) {
  1169. int raw_data = (short)le16_to_cpu(ds_report->gyro[i]);
  1170. int calib_data = mult_frac(ds->gyro_calib_data[i].sens_numer,
  1171. raw_data, ds->gyro_calib_data[i].sens_denom);
  1172. input_report_abs(ds->sensors, ds->gyro_calib_data[i].abs_code, calib_data);
  1173. }
  1174. /* Parse and calibrate accelerometer data. */
  1175. for (i = 0; i < ARRAY_SIZE(ds_report->accel); i++) {
  1176. int raw_data = (short)le16_to_cpu(ds_report->accel[i]);
  1177. int calib_data = mult_frac(ds->accel_calib_data[i].sens_numer,
  1178. raw_data - ds->accel_calib_data[i].bias,
  1179. ds->accel_calib_data[i].sens_denom);
  1180. input_report_abs(ds->sensors, ds->accel_calib_data[i].abs_code, calib_data);
  1181. }
  1182. /* Convert timestamp (in 0.33us unit) to timestamp_us */
  1183. sensor_timestamp = le32_to_cpu(ds_report->sensor_timestamp);
  1184. if (!ds->sensor_timestamp_initialized) {
  1185. ds->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp, 3);
  1186. ds->sensor_timestamp_initialized = true;
  1187. } else {
  1188. uint32_t delta;
  1189. if (ds->prev_sensor_timestamp > sensor_timestamp)
  1190. delta = (U32_MAX - ds->prev_sensor_timestamp + sensor_timestamp + 1);
  1191. else
  1192. delta = sensor_timestamp - ds->prev_sensor_timestamp;
  1193. ds->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta, 3);
  1194. }
  1195. ds->prev_sensor_timestamp = sensor_timestamp;
  1196. input_event(ds->sensors, EV_MSC, MSC_TIMESTAMP, ds->sensor_timestamp_us);
  1197. input_sync(ds->sensors);
  1198. for (i = 0; i < ARRAY_SIZE(ds_report->points); i++) {
  1199. struct dualsense_touch_point *point = &ds_report->points[i];
  1200. bool active = (point->contact & DS_TOUCH_POINT_INACTIVE) ? false : true;
  1201. input_mt_slot(ds->touchpad, i);
  1202. input_mt_report_slot_state(ds->touchpad, MT_TOOL_FINGER, active);
  1203. if (active) {
  1204. int x = (point->x_hi << 8) | point->x_lo;
  1205. int y = (point->y_hi << 4) | point->y_lo;
  1206. input_report_abs(ds->touchpad, ABS_MT_POSITION_X, x);
  1207. input_report_abs(ds->touchpad, ABS_MT_POSITION_Y, y);
  1208. }
  1209. }
  1210. input_mt_sync_frame(ds->touchpad);
  1211. input_report_key(ds->touchpad, BTN_LEFT, ds_report->buttons[2] & DS_BUTTONS2_TOUCHPAD);
  1212. input_sync(ds->touchpad);
  1213. battery_data = ds_report->status & DS_STATUS_BATTERY_CAPACITY;
  1214. charging_status = (ds_report->status & DS_STATUS_CHARGING) >> DS_STATUS_CHARGING_SHIFT;
  1215. switch (charging_status) {
  1216. case 0x0:
  1217. /*
  1218. * Each unit of battery data corresponds to 10%
  1219. * 0 = 0-9%, 1 = 10-19%, .. and 10 = 100%
  1220. */
  1221. battery_capacity = min(battery_data * 10 + 5, 100);
  1222. battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
  1223. break;
  1224. case 0x1:
  1225. battery_capacity = min(battery_data * 10 + 5, 100);
  1226. battery_status = POWER_SUPPLY_STATUS_CHARGING;
  1227. break;
  1228. case 0x2:
  1229. battery_capacity = 100;
  1230. battery_status = POWER_SUPPLY_STATUS_FULL;
  1231. break;
  1232. case 0xa: /* voltage or temperature out of range */
  1233. case 0xb: /* temperature error */
  1234. battery_capacity = 0;
  1235. battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1236. break;
  1237. case 0xf: /* charging error */
  1238. default:
  1239. battery_capacity = 0;
  1240. battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
  1241. }
  1242. spin_lock_irqsave(&ps_dev->lock, flags);
  1243. ps_dev->battery_capacity = battery_capacity;
  1244. ps_dev->battery_status = battery_status;
  1245. spin_unlock_irqrestore(&ps_dev->lock, flags);
  1246. return 0;
  1247. }
  1248. static int dualsense_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
  1249. {
  1250. struct hid_device *hdev = input_get_drvdata(dev);
  1251. struct dualsense *ds = hid_get_drvdata(hdev);
  1252. unsigned long flags;
  1253. if (effect->type != FF_RUMBLE)
  1254. return 0;
  1255. spin_lock_irqsave(&ds->base.lock, flags);
  1256. ds->update_rumble = true;
  1257. ds->motor_left = effect->u.rumble.strong_magnitude / 256;
  1258. ds->motor_right = effect->u.rumble.weak_magnitude / 256;
  1259. spin_unlock_irqrestore(&ds->base.lock, flags);
  1260. dualsense_schedule_work(ds);
  1261. return 0;
  1262. }
  1263. static void dualsense_remove(struct ps_device *ps_dev)
  1264. {
  1265. struct dualsense *ds = container_of(ps_dev, struct dualsense, base);
  1266. unsigned long flags;
  1267. spin_lock_irqsave(&ds->base.lock, flags);
  1268. ds->output_worker_initialized = false;
  1269. spin_unlock_irqrestore(&ds->base.lock, flags);
  1270. cancel_work_sync(&ds->output_worker);
  1271. }
  1272. static int dualsense_reset_leds(struct dualsense *ds)
  1273. {
  1274. struct dualsense_output_report report;
  1275. uint8_t *buf;
  1276. buf = kzalloc(sizeof(struct dualsense_output_report_bt), GFP_KERNEL);
  1277. if (!buf)
  1278. return -ENOMEM;
  1279. dualsense_init_output_report(ds, &report, buf);
  1280. /*
  1281. * On Bluetooth the DualSense outputs an animation on the lightbar
  1282. * during startup and maintains a color afterwards. We need to explicitly
  1283. * reconfigure the lightbar before we can do any programming later on.
  1284. * In USB the lightbar is not on by default, but redoing the setup there
  1285. * doesn't hurt.
  1286. */
  1287. report.common->valid_flag2 = DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE;
  1288. report.common->lightbar_setup = DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT; /* Fade light out. */
  1289. dualsense_send_output_report(ds, &report);
  1290. kfree(buf);
  1291. return 0;
  1292. }
  1293. static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue)
  1294. {
  1295. unsigned long flags;
  1296. spin_lock_irqsave(&ds->base.lock, flags);
  1297. ds->update_lightbar = true;
  1298. ds->lightbar_red = red;
  1299. ds->lightbar_green = green;
  1300. ds->lightbar_blue = blue;
  1301. spin_unlock_irqrestore(&ds->base.lock, flags);
  1302. dualsense_schedule_work(ds);
  1303. }
  1304. static void dualsense_set_player_leds(struct dualsense *ds)
  1305. {
  1306. /*
  1307. * The DualSense controller has a row of 5 LEDs used for player ids.
  1308. * Behavior on the PlayStation 5 console is to center the player id
  1309. * across the LEDs, so e.g. player 1 would be "--x--" with x being 'on'.
  1310. * Follow a similar mapping here.
  1311. */
  1312. static const int player_ids[5] = {
  1313. BIT(2),
  1314. BIT(3) | BIT(1),
  1315. BIT(4) | BIT(2) | BIT(0),
  1316. BIT(4) | BIT(3) | BIT(1) | BIT(0),
  1317. BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0)
  1318. };
  1319. uint8_t player_id = ds->base.player_id % ARRAY_SIZE(player_ids);
  1320. ds->update_player_leds = true;
  1321. ds->player_leds_state = player_ids[player_id];
  1322. dualsense_schedule_work(ds);
  1323. }
  1324. static struct ps_device *dualsense_create(struct hid_device *hdev)
  1325. {
  1326. struct dualsense *ds;
  1327. struct ps_device *ps_dev;
  1328. uint8_t max_output_report_size;
  1329. int i, ret;
  1330. static const struct ps_led_info player_leds_info[] = {
  1331. { LED_FUNCTION_PLAYER1, "white", 1, dualsense_player_led_get_brightness,
  1332. dualsense_player_led_set_brightness },
  1333. { LED_FUNCTION_PLAYER2, "white", 1, dualsense_player_led_get_brightness,
  1334. dualsense_player_led_set_brightness },
  1335. { LED_FUNCTION_PLAYER3, "white", 1, dualsense_player_led_get_brightness,
  1336. dualsense_player_led_set_brightness },
  1337. { LED_FUNCTION_PLAYER4, "white", 1, dualsense_player_led_get_brightness,
  1338. dualsense_player_led_set_brightness },
  1339. { LED_FUNCTION_PLAYER5, "white", 1, dualsense_player_led_get_brightness,
  1340. dualsense_player_led_set_brightness }
  1341. };
  1342. ds = devm_kzalloc(&hdev->dev, sizeof(*ds), GFP_KERNEL);
  1343. if (!ds)
  1344. return ERR_PTR(-ENOMEM);
  1345. /*
  1346. * Patch version to allow userspace to distinguish between
  1347. * hid-generic vs hid-playstation axis and button mapping.
  1348. */
  1349. hdev->version |= HID_PLAYSTATION_VERSION_PATCH;
  1350. ps_dev = &ds->base;
  1351. ps_dev->hdev = hdev;
  1352. spin_lock_init(&ps_dev->lock);
  1353. ps_dev->battery_capacity = 100; /* initial value until parse_report. */
  1354. ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
  1355. ps_dev->parse_report = dualsense_parse_report;
  1356. ps_dev->remove = dualsense_remove;
  1357. INIT_WORK(&ds->output_worker, dualsense_output_worker);
  1358. ds->output_worker_initialized = true;
  1359. hid_set_drvdata(hdev, ds);
  1360. max_output_report_size = sizeof(struct dualsense_output_report_bt);
  1361. ds->output_report_dmabuf = devm_kzalloc(&hdev->dev, max_output_report_size, GFP_KERNEL);
  1362. if (!ds->output_report_dmabuf)
  1363. return ERR_PTR(-ENOMEM);
  1364. ret = dualsense_get_mac_address(ds);
  1365. if (ret) {
  1366. hid_err(hdev, "Failed to get MAC address from DualSense\n");
  1367. return ERR_PTR(ret);
  1368. }
  1369. snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds->base.mac_address);
  1370. ret = dualsense_get_firmware_info(ds);
  1371. if (ret) {
  1372. hid_err(hdev, "Failed to get firmware info from DualSense\n");
  1373. return ERR_PTR(ret);
  1374. }
  1375. /* Original DualSense firmware simulated classic controller rumble through
  1376. * its new haptics hardware. It felt different from classic rumble users
  1377. * were used to. Since then new firmwares were introduced to change behavior
  1378. * and make this new 'v2' behavior default on PlayStation and other platforms.
  1379. * The original DualSense requires a new enough firmware as bundled with PS5
  1380. * software released in 2021. DualSense edge supports it out of the box.
  1381. * Both devices also support the old mode, but it is not really used.
  1382. */
  1383. if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER) {
  1384. /* Feature version 2.21 introduced new vibration method. */
  1385. ds->use_vibration_v2 = ds->update_version >= DS_FEATURE_VERSION(2, 21);
  1386. } else if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER_2) {
  1387. ds->use_vibration_v2 = true;
  1388. }
  1389. ret = ps_devices_list_add(ps_dev);
  1390. if (ret)
  1391. return ERR_PTR(ret);
  1392. ret = dualsense_get_calibration_data(ds);
  1393. if (ret) {
  1394. hid_err(hdev, "Failed to get calibration data from DualSense\n");
  1395. goto err;
  1396. }
  1397. ds->gamepad = ps_gamepad_create(hdev, dualsense_play_effect);
  1398. if (IS_ERR(ds->gamepad)) {
  1399. ret = PTR_ERR(ds->gamepad);
  1400. goto err;
  1401. }
  1402. /* Use gamepad input device name as primary device name for e.g. LEDs */
  1403. ps_dev->input_dev_name = dev_name(&ds->gamepad->dev);
  1404. ds->sensors = ps_sensors_create(hdev, DS_ACC_RANGE, DS_ACC_RES_PER_G,
  1405. DS_GYRO_RANGE, DS_GYRO_RES_PER_DEG_S);
  1406. if (IS_ERR(ds->sensors)) {
  1407. ret = PTR_ERR(ds->sensors);
  1408. goto err;
  1409. }
  1410. ds->touchpad = ps_touchpad_create(hdev, DS_TOUCHPAD_WIDTH, DS_TOUCHPAD_HEIGHT, 2);
  1411. if (IS_ERR(ds->touchpad)) {
  1412. ret = PTR_ERR(ds->touchpad);
  1413. goto err;
  1414. }
  1415. ret = ps_device_register_battery(ps_dev);
  1416. if (ret)
  1417. goto err;
  1418. /*
  1419. * The hardware may have control over the LEDs (e.g. in Bluetooth on startup).
  1420. * Reset the LEDs (lightbar, mute, player leds), so we can control them
  1421. * from software.
  1422. */
  1423. ret = dualsense_reset_leds(ds);
  1424. if (ret)
  1425. goto err;
  1426. ret = ps_lightbar_register(ps_dev, &ds->lightbar, dualsense_lightbar_set_brightness);
  1427. if (ret)
  1428. goto err;
  1429. /* Set default lightbar color. */
  1430. dualsense_set_lightbar(ds, 0, 0, 128); /* blue */
  1431. for (i = 0; i < ARRAY_SIZE(player_leds_info); i++) {
  1432. const struct ps_led_info *led_info = &player_leds_info[i];
  1433. ret = ps_led_register(ps_dev, &ds->player_leds[i], led_info);
  1434. if (ret < 0)
  1435. goto err;
  1436. }
  1437. ret = ps_device_set_player_id(ps_dev);
  1438. if (ret) {
  1439. hid_err(hdev, "Failed to assign player id for DualSense: %d\n", ret);
  1440. goto err;
  1441. }
  1442. /* Set player LEDs to our player id. */
  1443. dualsense_set_player_leds(ds);
  1444. /*
  1445. * Reporting hardware and firmware is important as there are frequent updates, which
  1446. * can change behavior.
  1447. */
  1448. hid_info(hdev, "Registered DualSense controller hw_version=0x%08x fw_version=0x%08x\n",
  1449. ds->base.hw_version, ds->base.fw_version);
  1450. return &ds->base;
  1451. err:
  1452. ps_devices_list_remove(ps_dev);
  1453. return ERR_PTR(ret);
  1454. }
  1455. static void dualshock4_dongle_calibration_work(struct work_struct *work)
  1456. {
  1457. struct dualshock4 *ds4 = container_of(work, struct dualshock4, dongle_hotplug_worker);
  1458. unsigned long flags;
  1459. enum dualshock4_dongle_state dongle_state;
  1460. int ret;
  1461. ret = dualshock4_get_calibration_data(ds4);
  1462. if (ret < 0) {
  1463. /* This call is very unlikely to fail for the dongle. When it
  1464. * fails we are probably in a very bad state, so mark the
  1465. * dongle as disabled. We will re-enable the dongle if a new
  1466. * DS4 hotplug is detect from sony_raw_event as any issues
  1467. * are likely resolved then (the dongle is quite stupid).
  1468. */
  1469. hid_err(ds4->base.hdev, "DualShock 4 USB dongle: calibration failed, disabling device\n");
  1470. dongle_state = DONGLE_DISABLED;
  1471. } else {
  1472. hid_info(ds4->base.hdev, "DualShock 4 USB dongle: calibration completed\n");
  1473. dongle_state = DONGLE_CONNECTED;
  1474. }
  1475. spin_lock_irqsave(&ds4->base.lock, flags);
  1476. ds4->dongle_state = dongle_state;
  1477. spin_unlock_irqrestore(&ds4->base.lock, flags);
  1478. }
  1479. static int dualshock4_get_calibration_data(struct dualshock4 *ds4)
  1480. {
  1481. struct hid_device *hdev = ds4->base.hdev;
  1482. short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
  1483. short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
  1484. short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
  1485. short gyro_speed_plus, gyro_speed_minus;
  1486. short acc_x_plus, acc_x_minus;
  1487. short acc_y_plus, acc_y_minus;
  1488. short acc_z_plus, acc_z_minus;
  1489. int speed_2x;
  1490. int range_2g;
  1491. int ret = 0;
  1492. int i;
  1493. uint8_t *buf;
  1494. if (ds4->base.hdev->bus == BUS_USB) {
  1495. int retries;
  1496. buf = kzalloc(DS4_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL);
  1497. if (!buf) {
  1498. ret = -ENOMEM;
  1499. goto transfer_failed;
  1500. }
  1501. /* We should normally receive the feature report data we asked
  1502. * for, but hidraw applications such as Steam can issue feature
  1503. * reports as well. In particular for Dongle reconnects, Steam
  1504. * and this function are competing resulting in often receiving
  1505. * data for a different HID report, so retry a few times.
  1506. */
  1507. for (retries = 0; retries < 3; retries++) {
  1508. ret = ps_get_report(hdev, DS4_FEATURE_REPORT_CALIBRATION, buf,
  1509. DS4_FEATURE_REPORT_CALIBRATION_SIZE, true);
  1510. if (ret) {
  1511. if (retries < 2) {
  1512. hid_warn(hdev, "Retrying DualShock 4 get calibration report (0x02) request\n");
  1513. continue;
  1514. }
  1515. hid_warn(hdev, "Failed to retrieve DualShock4 calibration info: %d\n", ret);
  1516. ret = -EILSEQ;
  1517. goto transfer_failed;
  1518. } else {
  1519. break;
  1520. }
  1521. }
  1522. } else { /* Bluetooth */
  1523. buf = kzalloc(DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE, GFP_KERNEL);
  1524. if (!buf) {
  1525. ret = -ENOMEM;
  1526. goto transfer_failed;
  1527. }
  1528. ret = ps_get_report(hdev, DS4_FEATURE_REPORT_CALIBRATION_BT, buf,
  1529. DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE, true);
  1530. if (ret) {
  1531. hid_warn(hdev, "Failed to retrieve DualShock4 calibration info: %d\n", ret);
  1532. goto transfer_failed;
  1533. }
  1534. }
  1535. /* Transfer succeeded - parse the calibration data received. */
  1536. gyro_pitch_bias = get_unaligned_le16(&buf[1]);
  1537. gyro_yaw_bias = get_unaligned_le16(&buf[3]);
  1538. gyro_roll_bias = get_unaligned_le16(&buf[5]);
  1539. if (ds4->base.hdev->bus == BUS_USB) {
  1540. gyro_pitch_plus = get_unaligned_le16(&buf[7]);
  1541. gyro_pitch_minus = get_unaligned_le16(&buf[9]);
  1542. gyro_yaw_plus = get_unaligned_le16(&buf[11]);
  1543. gyro_yaw_minus = get_unaligned_le16(&buf[13]);
  1544. gyro_roll_plus = get_unaligned_le16(&buf[15]);
  1545. gyro_roll_minus = get_unaligned_le16(&buf[17]);
  1546. } else {
  1547. /* BT + Dongle */
  1548. gyro_pitch_plus = get_unaligned_le16(&buf[7]);
  1549. gyro_yaw_plus = get_unaligned_le16(&buf[9]);
  1550. gyro_roll_plus = get_unaligned_le16(&buf[11]);
  1551. gyro_pitch_minus = get_unaligned_le16(&buf[13]);
  1552. gyro_yaw_minus = get_unaligned_le16(&buf[15]);
  1553. gyro_roll_minus = get_unaligned_le16(&buf[17]);
  1554. }
  1555. gyro_speed_plus = get_unaligned_le16(&buf[19]);
  1556. gyro_speed_minus = get_unaligned_le16(&buf[21]);
  1557. acc_x_plus = get_unaligned_le16(&buf[23]);
  1558. acc_x_minus = get_unaligned_le16(&buf[25]);
  1559. acc_y_plus = get_unaligned_le16(&buf[27]);
  1560. acc_y_minus = get_unaligned_le16(&buf[29]);
  1561. acc_z_plus = get_unaligned_le16(&buf[31]);
  1562. acc_z_minus = get_unaligned_le16(&buf[33]);
  1563. /* Done parsing the buffer, so let's free it. */
  1564. kfree(buf);
  1565. /*
  1566. * Set gyroscope calibration and normalization parameters.
  1567. * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s.
  1568. */
  1569. speed_2x = (gyro_speed_plus + gyro_speed_minus);
  1570. ds4->gyro_calib_data[0].abs_code = ABS_RX;
  1571. ds4->gyro_calib_data[0].bias = 0;
  1572. ds4->gyro_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
  1573. ds4->gyro_calib_data[0].sens_denom = abs(gyro_pitch_plus - gyro_pitch_bias) +
  1574. abs(gyro_pitch_minus - gyro_pitch_bias);
  1575. ds4->gyro_calib_data[1].abs_code = ABS_RY;
  1576. ds4->gyro_calib_data[1].bias = 0;
  1577. ds4->gyro_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
  1578. ds4->gyro_calib_data[1].sens_denom = abs(gyro_yaw_plus - gyro_yaw_bias) +
  1579. abs(gyro_yaw_minus - gyro_yaw_bias);
  1580. ds4->gyro_calib_data[2].abs_code = ABS_RZ;
  1581. ds4->gyro_calib_data[2].bias = 0;
  1582. ds4->gyro_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
  1583. ds4->gyro_calib_data[2].sens_denom = abs(gyro_roll_plus - gyro_roll_bias) +
  1584. abs(gyro_roll_minus - gyro_roll_bias);
  1585. /*
  1586. * Set accelerometer calibration and normalization parameters.
  1587. * Data values will be normalized to 1/DS4_ACC_RES_PER_G g.
  1588. */
  1589. range_2g = acc_x_plus - acc_x_minus;
  1590. ds4->accel_calib_data[0].abs_code = ABS_X;
  1591. ds4->accel_calib_data[0].bias = acc_x_plus - range_2g / 2;
  1592. ds4->accel_calib_data[0].sens_numer = 2*DS4_ACC_RES_PER_G;
  1593. ds4->accel_calib_data[0].sens_denom = range_2g;
  1594. range_2g = acc_y_plus - acc_y_minus;
  1595. ds4->accel_calib_data[1].abs_code = ABS_Y;
  1596. ds4->accel_calib_data[1].bias = acc_y_plus - range_2g / 2;
  1597. ds4->accel_calib_data[1].sens_numer = 2*DS4_ACC_RES_PER_G;
  1598. ds4->accel_calib_data[1].sens_denom = range_2g;
  1599. range_2g = acc_z_plus - acc_z_minus;
  1600. ds4->accel_calib_data[2].abs_code = ABS_Z;
  1601. ds4->accel_calib_data[2].bias = acc_z_plus - range_2g / 2;
  1602. ds4->accel_calib_data[2].sens_numer = 2*DS4_ACC_RES_PER_G;
  1603. ds4->accel_calib_data[2].sens_denom = range_2g;
  1604. transfer_failed:
  1605. /*
  1606. * Sanity check gyro calibration data. This is needed to prevent crashes
  1607. * during report handling of virtual, clone or broken devices not implementing
  1608. * calibration data properly.
  1609. */
  1610. for (i = 0; i < ARRAY_SIZE(ds4->gyro_calib_data); i++) {
  1611. if (ds4->gyro_calib_data[i].sens_denom == 0) {
  1612. ds4->gyro_calib_data[i].abs_code = ABS_RX + i;
  1613. hid_warn(hdev, "Invalid gyro calibration data for axis (%d), disabling calibration.",
  1614. ds4->gyro_calib_data[i].abs_code);
  1615. ds4->gyro_calib_data[i].bias = 0;
  1616. ds4->gyro_calib_data[i].sens_numer = DS4_GYRO_RANGE;
  1617. ds4->gyro_calib_data[i].sens_denom = S16_MAX;
  1618. }
  1619. }
  1620. /*
  1621. * Sanity check accelerometer calibration data. This is needed to prevent crashes
  1622. * during report handling of virtual, clone or broken devices not implementing calibration
  1623. * data properly.
  1624. */
  1625. for (i = 0; i < ARRAY_SIZE(ds4->accel_calib_data); i++) {
  1626. if (ds4->accel_calib_data[i].sens_denom == 0) {
  1627. ds4->accel_calib_data[i].abs_code = ABS_X + i;
  1628. hid_warn(hdev, "Invalid accelerometer calibration data for axis (%d), disabling calibration.",
  1629. ds4->accel_calib_data[i].abs_code);
  1630. ds4->accel_calib_data[i].bias = 0;
  1631. ds4->accel_calib_data[i].sens_numer = DS4_ACC_RANGE;
  1632. ds4->accel_calib_data[i].sens_denom = S16_MAX;
  1633. }
  1634. }
  1635. return ret;
  1636. }
  1637. static int dualshock4_get_firmware_info(struct dualshock4 *ds4)
  1638. {
  1639. uint8_t *buf;
  1640. int ret;
  1641. buf = kzalloc(DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL);
  1642. if (!buf)
  1643. return -ENOMEM;
  1644. /* Note USB and BT support the same feature report, but this report
  1645. * lacks CRC support, so must be disabled in ps_get_report.
  1646. */
  1647. ret = ps_get_report(ds4->base.hdev, DS4_FEATURE_REPORT_FIRMWARE_INFO, buf,
  1648. DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE, false);
  1649. if (ret) {
  1650. hid_err(ds4->base.hdev, "Failed to retrieve DualShock4 firmware info: %d\n", ret);
  1651. goto err_free;
  1652. }
  1653. ds4->base.hw_version = get_unaligned_le16(&buf[35]);
  1654. ds4->base.fw_version = get_unaligned_le16(&buf[41]);
  1655. err_free:
  1656. kfree(buf);
  1657. return ret;
  1658. }
  1659. static int dualshock4_get_mac_address(struct dualshock4 *ds4)
  1660. {
  1661. struct hid_device *hdev = ds4->base.hdev;
  1662. uint8_t *buf;
  1663. int ret = 0;
  1664. if (hdev->bus == BUS_USB) {
  1665. buf = kzalloc(DS4_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL);
  1666. if (!buf)
  1667. return -ENOMEM;
  1668. ret = ps_get_report(hdev, DS4_FEATURE_REPORT_PAIRING_INFO, buf,
  1669. DS4_FEATURE_REPORT_PAIRING_INFO_SIZE, false);
  1670. if (ret) {
  1671. hid_err(hdev, "Failed to retrieve DualShock4 pairing info: %d\n", ret);
  1672. goto err_free;
  1673. }
  1674. memcpy(ds4->base.mac_address, &buf[1], sizeof(ds4->base.mac_address));
  1675. } else {
  1676. /* Rely on HIDP for Bluetooth */
  1677. if (strlen(hdev->uniq) != 17)
  1678. return -EINVAL;
  1679. ret = sscanf(hdev->uniq, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  1680. &ds4->base.mac_address[5], &ds4->base.mac_address[4],
  1681. &ds4->base.mac_address[3], &ds4->base.mac_address[2],
  1682. &ds4->base.mac_address[1], &ds4->base.mac_address[0]);
  1683. if (ret != sizeof(ds4->base.mac_address))
  1684. return -EINVAL;
  1685. return 0;
  1686. }
  1687. err_free:
  1688. kfree(buf);
  1689. return ret;
  1690. }
  1691. static enum led_brightness dualshock4_led_get_brightness(struct led_classdev *led)
  1692. {
  1693. struct hid_device *hdev = to_hid_device(led->dev->parent);
  1694. struct dualshock4 *ds4 = hid_get_drvdata(hdev);
  1695. unsigned int led_index;
  1696. led_index = led - ds4->lightbar_leds;
  1697. switch (led_index) {
  1698. case 0:
  1699. return ds4->lightbar_red;
  1700. case 1:
  1701. return ds4->lightbar_green;
  1702. case 2:
  1703. return ds4->lightbar_blue;
  1704. case 3:
  1705. return ds4->lightbar_enabled;
  1706. }
  1707. return -1;
  1708. }
  1709. static int dualshock4_led_set_blink(struct led_classdev *led, unsigned long *delay_on,
  1710. unsigned long *delay_off)
  1711. {
  1712. struct hid_device *hdev = to_hid_device(led->dev->parent);
  1713. struct dualshock4 *ds4 = hid_get_drvdata(hdev);
  1714. unsigned long flags;
  1715. spin_lock_irqsave(&ds4->base.lock, flags);
  1716. if (!*delay_on && !*delay_off) {
  1717. /* Default to 1 Hz (50 centiseconds on, 50 centiseconds off). */
  1718. ds4->lightbar_blink_on = 50;
  1719. ds4->lightbar_blink_off = 50;
  1720. } else {
  1721. /* Blink delays in centiseconds. */
  1722. ds4->lightbar_blink_on = min_t(unsigned long, *delay_on/10, DS4_LIGHTBAR_MAX_BLINK);
  1723. ds4->lightbar_blink_off = min_t(unsigned long, *delay_off/10, DS4_LIGHTBAR_MAX_BLINK);
  1724. }
  1725. ds4->update_lightbar_blink = true;
  1726. spin_unlock_irqrestore(&ds4->base.lock, flags);
  1727. dualshock4_schedule_work(ds4);
  1728. /* Report scaled values back to LED subsystem */
  1729. *delay_on = ds4->lightbar_blink_on * 10;
  1730. *delay_off = ds4->lightbar_blink_off * 10;
  1731. return 0;
  1732. }
  1733. static int dualshock4_led_set_brightness(struct led_classdev *led, enum led_brightness value)
  1734. {
  1735. struct hid_device *hdev = to_hid_device(led->dev->parent);
  1736. struct dualshock4 *ds4 = hid_get_drvdata(hdev);
  1737. unsigned long flags;
  1738. unsigned int led_index;
  1739. spin_lock_irqsave(&ds4->base.lock, flags);
  1740. led_index = led - ds4->lightbar_leds;
  1741. switch (led_index) {
  1742. case 0:
  1743. ds4->lightbar_red = value;
  1744. break;
  1745. case 1:
  1746. ds4->lightbar_green = value;
  1747. break;
  1748. case 2:
  1749. ds4->lightbar_blue = value;
  1750. break;
  1751. case 3:
  1752. ds4->lightbar_enabled = !!value;
  1753. /* brightness = 0 also cancels blinking in Linux. */
  1754. if (!ds4->lightbar_enabled) {
  1755. ds4->lightbar_blink_off = 0;
  1756. ds4->lightbar_blink_on = 0;
  1757. ds4->update_lightbar_blink = true;
  1758. }
  1759. }
  1760. ds4->update_lightbar = true;
  1761. spin_unlock_irqrestore(&ds4->base.lock, flags);
  1762. dualshock4_schedule_work(ds4);
  1763. return 0;
  1764. }
  1765. static void dualshock4_init_output_report(struct dualshock4 *ds4,
  1766. struct dualshock4_output_report *rp, void *buf)
  1767. {
  1768. struct hid_device *hdev = ds4->base.hdev;
  1769. if (hdev->bus == BUS_BLUETOOTH) {
  1770. struct dualshock4_output_report_bt *bt = buf;
  1771. memset(bt, 0, sizeof(*bt));
  1772. bt->report_id = DS4_OUTPUT_REPORT_BT;
  1773. rp->data = buf;
  1774. rp->len = sizeof(*bt);
  1775. rp->bt = bt;
  1776. rp->usb = NULL;
  1777. rp->common = &bt->common;
  1778. } else { /* USB */
  1779. struct dualshock4_output_report_usb *usb = buf;
  1780. memset(usb, 0, sizeof(*usb));
  1781. usb->report_id = DS4_OUTPUT_REPORT_USB;
  1782. rp->data = buf;
  1783. rp->len = sizeof(*usb);
  1784. rp->bt = NULL;
  1785. rp->usb = usb;
  1786. rp->common = &usb->common;
  1787. }
  1788. }
  1789. static void dualshock4_output_worker(struct work_struct *work)
  1790. {
  1791. struct dualshock4 *ds4 = container_of(work, struct dualshock4, output_worker);
  1792. struct dualshock4_output_report report;
  1793. struct dualshock4_output_report_common *common;
  1794. unsigned long flags;
  1795. dualshock4_init_output_report(ds4, &report, ds4->output_report_dmabuf);
  1796. common = report.common;
  1797. spin_lock_irqsave(&ds4->base.lock, flags);
  1798. /*
  1799. * Some 3rd party gamepads expect updates to rumble and lightbar
  1800. * together, and setting one may cancel the other.
  1801. *
  1802. * Let's maximise compatibility by always sending rumble and lightbar
  1803. * updates together, even when only one has been scheduled, resulting
  1804. * in:
  1805. *
  1806. * ds4->valid_flag0 >= 0x03
  1807. *
  1808. * Hopefully this will maximise compatibility with third-party pads.
  1809. *
  1810. * Any further update bits, such as 0x04 for lightbar blinking, will
  1811. * be or'd on top of this like before.
  1812. */
  1813. if (ds4->update_rumble || ds4->update_lightbar) {
  1814. ds4->update_rumble = true; /* 0x01 */
  1815. ds4->update_lightbar = true; /* 0x02 */
  1816. }
  1817. if (ds4->update_rumble) {
  1818. /* Select classic rumble style haptics and enable it. */
  1819. common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_MOTOR;
  1820. common->motor_left = ds4->motor_left;
  1821. common->motor_right = ds4->motor_right;
  1822. ds4->update_rumble = false;
  1823. }
  1824. if (ds4->update_lightbar) {
  1825. common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED;
  1826. /* Comptabile behavior with hid-sony, which used a dummy global LED to
  1827. * allow enabling/disabling the lightbar. The global LED maps to
  1828. * lightbar_enabled.
  1829. */
  1830. common->lightbar_red = ds4->lightbar_enabled ? ds4->lightbar_red : 0;
  1831. common->lightbar_green = ds4->lightbar_enabled ? ds4->lightbar_green : 0;
  1832. common->lightbar_blue = ds4->lightbar_enabled ? ds4->lightbar_blue : 0;
  1833. ds4->update_lightbar = false;
  1834. }
  1835. if (ds4->update_lightbar_blink) {
  1836. common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED_BLINK;
  1837. common->lightbar_blink_on = ds4->lightbar_blink_on;
  1838. common->lightbar_blink_off = ds4->lightbar_blink_off;
  1839. ds4->update_lightbar_blink = false;
  1840. }
  1841. spin_unlock_irqrestore(&ds4->base.lock, flags);
  1842. /* Bluetooth packets need additional flags as well as a CRC in the last 4 bytes. */
  1843. if (report.bt) {
  1844. uint32_t crc;
  1845. uint8_t seed = PS_OUTPUT_CRC32_SEED;
  1846. /* Hardware control flags need to set to let the device know
  1847. * there is HID data as well as CRC.
  1848. */
  1849. report.bt->hw_control = DS4_OUTPUT_HWCTL_HID | DS4_OUTPUT_HWCTL_CRC32;
  1850. if (ds4->update_bt_poll_interval) {
  1851. report.bt->hw_control |= ds4->bt_poll_interval;
  1852. ds4->update_bt_poll_interval = false;
  1853. }
  1854. crc = crc32_le(0xFFFFFFFF, &seed, 1);
  1855. crc = ~crc32_le(crc, report.data, report.len - 4);
  1856. report.bt->crc32 = cpu_to_le32(crc);
  1857. }
  1858. hid_hw_output_report(ds4->base.hdev, report.data, report.len);
  1859. }
  1860. static int dualshock4_parse_report(struct ps_device *ps_dev, struct hid_report *report,
  1861. u8 *data, int size)
  1862. {
  1863. struct hid_device *hdev = ps_dev->hdev;
  1864. struct dualshock4 *ds4 = container_of(ps_dev, struct dualshock4, base);
  1865. struct dualshock4_input_report_common *ds4_report;
  1866. struct dualshock4_touch_report *touch_reports;
  1867. uint8_t battery_capacity, num_touch_reports, value;
  1868. int battery_status, i, j;
  1869. uint16_t sensor_timestamp;
  1870. unsigned long flags;
  1871. bool is_minimal = false;
  1872. /*
  1873. * DualShock4 in USB uses the full HID report for reportID 1, but
  1874. * Bluetooth uses a minimal HID report for reportID 1 and reports
  1875. * the full report using reportID 17.
  1876. */
  1877. if (hdev->bus == BUS_USB && report->id == DS4_INPUT_REPORT_USB &&
  1878. size == DS4_INPUT_REPORT_USB_SIZE) {
  1879. struct dualshock4_input_report_usb *usb = (struct dualshock4_input_report_usb *)data;
  1880. ds4_report = &usb->common;
  1881. num_touch_reports = usb->num_touch_reports;
  1882. touch_reports = usb->touch_reports;
  1883. } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS4_INPUT_REPORT_BT &&
  1884. size == DS4_INPUT_REPORT_BT_SIZE) {
  1885. struct dualshock4_input_report_bt *bt = (struct dualshock4_input_report_bt *)data;
  1886. uint32_t report_crc = get_unaligned_le32(&bt->crc32);
  1887. /* Last 4 bytes of input report contains CRC. */
  1888. if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) {
  1889. hid_err(hdev, "DualShock4 input CRC's check failed\n");
  1890. return -EILSEQ;
  1891. }
  1892. ds4_report = &bt->common;
  1893. num_touch_reports = bt->num_touch_reports;
  1894. touch_reports = bt->touch_reports;
  1895. } else if (hdev->bus == BUS_BLUETOOTH &&
  1896. report->id == DS4_INPUT_REPORT_BT_MINIMAL &&
  1897. size == DS4_INPUT_REPORT_BT_MINIMAL_SIZE) {
  1898. /* Some third-party pads never switch to the full 0x11 report.
  1899. * The short 0x01 report is 10 bytes long:
  1900. * u8 report_id == 0x01
  1901. * u8 first_bytes_of_full_report[9]
  1902. * So let's reuse the full report parser, and stop it after
  1903. * parsing the buttons.
  1904. */
  1905. ds4_report = (struct dualshock4_input_report_common *)&data[1];
  1906. is_minimal = true;
  1907. } else {
  1908. hid_err(hdev, "Unhandled reportID=%d\n", report->id);
  1909. return -1;
  1910. }
  1911. input_report_abs(ds4->gamepad, ABS_X, ds4_report->x);
  1912. input_report_abs(ds4->gamepad, ABS_Y, ds4_report->y);
  1913. input_report_abs(ds4->gamepad, ABS_RX, ds4_report->rx);
  1914. input_report_abs(ds4->gamepad, ABS_RY, ds4_report->ry);
  1915. input_report_abs(ds4->gamepad, ABS_Z, ds4_report->z);
  1916. input_report_abs(ds4->gamepad, ABS_RZ, ds4_report->rz);
  1917. value = ds4_report->buttons[0] & DS_BUTTONS0_HAT_SWITCH;
  1918. if (value >= ARRAY_SIZE(ps_gamepad_hat_mapping))
  1919. value = 8; /* center */
  1920. input_report_abs(ds4->gamepad, ABS_HAT0X, ps_gamepad_hat_mapping[value].x);
  1921. input_report_abs(ds4->gamepad, ABS_HAT0Y, ps_gamepad_hat_mapping[value].y);
  1922. input_report_key(ds4->gamepad, BTN_WEST, ds4_report->buttons[0] & DS_BUTTONS0_SQUARE);
  1923. input_report_key(ds4->gamepad, BTN_SOUTH, ds4_report->buttons[0] & DS_BUTTONS0_CROSS);
  1924. input_report_key(ds4->gamepad, BTN_EAST, ds4_report->buttons[0] & DS_BUTTONS0_CIRCLE);
  1925. input_report_key(ds4->gamepad, BTN_NORTH, ds4_report->buttons[0] & DS_BUTTONS0_TRIANGLE);
  1926. input_report_key(ds4->gamepad, BTN_TL, ds4_report->buttons[1] & DS_BUTTONS1_L1);
  1927. input_report_key(ds4->gamepad, BTN_TR, ds4_report->buttons[1] & DS_BUTTONS1_R1);
  1928. input_report_key(ds4->gamepad, BTN_TL2, ds4_report->buttons[1] & DS_BUTTONS1_L2);
  1929. input_report_key(ds4->gamepad, BTN_TR2, ds4_report->buttons[1] & DS_BUTTONS1_R2);
  1930. input_report_key(ds4->gamepad, BTN_SELECT, ds4_report->buttons[1] & DS_BUTTONS1_CREATE);
  1931. input_report_key(ds4->gamepad, BTN_START, ds4_report->buttons[1] & DS_BUTTONS1_OPTIONS);
  1932. input_report_key(ds4->gamepad, BTN_THUMBL, ds4_report->buttons[1] & DS_BUTTONS1_L3);
  1933. input_report_key(ds4->gamepad, BTN_THUMBR, ds4_report->buttons[1] & DS_BUTTONS1_R3);
  1934. input_report_key(ds4->gamepad, BTN_MODE, ds4_report->buttons[2] & DS_BUTTONS2_PS_HOME);
  1935. input_sync(ds4->gamepad);
  1936. if (is_minimal)
  1937. return 0;
  1938. /* Parse and calibrate gyroscope data. */
  1939. for (i = 0; i < ARRAY_SIZE(ds4_report->gyro); i++) {
  1940. int raw_data = (short)le16_to_cpu(ds4_report->gyro[i]);
  1941. int calib_data = mult_frac(ds4->gyro_calib_data[i].sens_numer,
  1942. raw_data, ds4->gyro_calib_data[i].sens_denom);
  1943. input_report_abs(ds4->sensors, ds4->gyro_calib_data[i].abs_code, calib_data);
  1944. }
  1945. /* Parse and calibrate accelerometer data. */
  1946. for (i = 0; i < ARRAY_SIZE(ds4_report->accel); i++) {
  1947. int raw_data = (short)le16_to_cpu(ds4_report->accel[i]);
  1948. int calib_data = mult_frac(ds4->accel_calib_data[i].sens_numer,
  1949. raw_data - ds4->accel_calib_data[i].bias,
  1950. ds4->accel_calib_data[i].sens_denom);
  1951. input_report_abs(ds4->sensors, ds4->accel_calib_data[i].abs_code, calib_data);
  1952. }
  1953. /* Convert timestamp (in 5.33us unit) to timestamp_us */
  1954. sensor_timestamp = le16_to_cpu(ds4_report->sensor_timestamp);
  1955. if (!ds4->sensor_timestamp_initialized) {
  1956. ds4->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp*16, 3);
  1957. ds4->sensor_timestamp_initialized = true;
  1958. } else {
  1959. uint16_t delta;
  1960. if (ds4->prev_sensor_timestamp > sensor_timestamp)
  1961. delta = (U16_MAX - ds4->prev_sensor_timestamp + sensor_timestamp + 1);
  1962. else
  1963. delta = sensor_timestamp - ds4->prev_sensor_timestamp;
  1964. ds4->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta*16, 3);
  1965. }
  1966. ds4->prev_sensor_timestamp = sensor_timestamp;
  1967. input_event(ds4->sensors, EV_MSC, MSC_TIMESTAMP, ds4->sensor_timestamp_us);
  1968. input_sync(ds4->sensors);
  1969. for (i = 0; i < num_touch_reports; i++) {
  1970. struct dualshock4_touch_report *touch_report = &touch_reports[i];
  1971. for (j = 0; j < ARRAY_SIZE(touch_report->points); j++) {
  1972. struct dualshock4_touch_point *point = &touch_report->points[j];
  1973. bool active = (point->contact & DS4_TOUCH_POINT_INACTIVE) ? false : true;
  1974. input_mt_slot(ds4->touchpad, j);
  1975. input_mt_report_slot_state(ds4->touchpad, MT_TOOL_FINGER, active);
  1976. if (active) {
  1977. int x = (point->x_hi << 8) | point->x_lo;
  1978. int y = (point->y_hi << 4) | point->y_lo;
  1979. input_report_abs(ds4->touchpad, ABS_MT_POSITION_X, x);
  1980. input_report_abs(ds4->touchpad, ABS_MT_POSITION_Y, y);
  1981. }
  1982. }
  1983. input_mt_sync_frame(ds4->touchpad);
  1984. input_sync(ds4->touchpad);
  1985. }
  1986. input_report_key(ds4->touchpad, BTN_LEFT, ds4_report->buttons[2] & DS_BUTTONS2_TOUCHPAD);
  1987. /*
  1988. * Interpretation of the battery_capacity data depends on the cable state.
  1989. * When no cable is connected (bit4 is 0):
  1990. * - 0:10: percentage in units of 10%.
  1991. * When a cable is plugged in:
  1992. * - 0-10: percentage in units of 10%.
  1993. * - 11: battery is full
  1994. * - 14: not charging due to Voltage or temperature error
  1995. * - 15: charge error
  1996. */
  1997. if (ds4_report->status[0] & DS4_STATUS0_CABLE_STATE) {
  1998. uint8_t battery_data = ds4_report->status[0] & DS4_STATUS0_BATTERY_CAPACITY;
  1999. if (battery_data < 10) {
  2000. /* Take the mid-point for each battery capacity value,
  2001. * because on the hardware side 0 = 0-9%, 1=10-19%, etc.
  2002. * This matches official platform behavior, which does
  2003. * the same.
  2004. */
  2005. battery_capacity = battery_data * 10 + 5;
  2006. battery_status = POWER_SUPPLY_STATUS_CHARGING;
  2007. } else if (battery_data == 10) {
  2008. battery_capacity = 100;
  2009. battery_status = POWER_SUPPLY_STATUS_CHARGING;
  2010. } else if (battery_data == DS4_BATTERY_STATUS_FULL) {
  2011. battery_capacity = 100;
  2012. battery_status = POWER_SUPPLY_STATUS_FULL;
  2013. } else { /* 14, 15 and undefined values */
  2014. battery_capacity = 0;
  2015. battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
  2016. }
  2017. } else {
  2018. uint8_t battery_data = ds4_report->status[0] & DS4_STATUS0_BATTERY_CAPACITY;
  2019. if (battery_data < 10)
  2020. battery_capacity = battery_data * 10 + 5;
  2021. else /* 10 */
  2022. battery_capacity = 100;
  2023. battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
  2024. }
  2025. spin_lock_irqsave(&ps_dev->lock, flags);
  2026. ps_dev->battery_capacity = battery_capacity;
  2027. ps_dev->battery_status = battery_status;
  2028. spin_unlock_irqrestore(&ps_dev->lock, flags);
  2029. return 0;
  2030. }
  2031. static int dualshock4_dongle_parse_report(struct ps_device *ps_dev, struct hid_report *report,
  2032. u8 *data, int size)
  2033. {
  2034. struct dualshock4 *ds4 = container_of(ps_dev, struct dualshock4, base);
  2035. bool connected = false;
  2036. /* The dongle reports data using the main USB report (0x1) no matter whether a controller
  2037. * is connected with mostly zeros. The report does contain dongle status, which we use to
  2038. * determine if a controller is connected and if so we forward to the regular DualShock4
  2039. * parsing code.
  2040. */
  2041. if (data[0] == DS4_INPUT_REPORT_USB && size == DS4_INPUT_REPORT_USB_SIZE) {
  2042. struct dualshock4_input_report_common *ds4_report = (struct dualshock4_input_report_common *)&data[1];
  2043. unsigned long flags;
  2044. connected = ds4_report->status[1] & DS4_STATUS1_DONGLE_STATE ? false : true;
  2045. if (ds4->dongle_state == DONGLE_DISCONNECTED && connected) {
  2046. hid_info(ps_dev->hdev, "DualShock 4 USB dongle: controller connected\n");
  2047. dualshock4_set_default_lightbar_colors(ds4);
  2048. spin_lock_irqsave(&ps_dev->lock, flags);
  2049. ds4->dongle_state = DONGLE_CALIBRATING;
  2050. spin_unlock_irqrestore(&ps_dev->lock, flags);
  2051. schedule_work(&ds4->dongle_hotplug_worker);
  2052. /* Don't process the report since we don't have
  2053. * calibration data, but let hidraw have it anyway.
  2054. */
  2055. return 0;
  2056. } else if ((ds4->dongle_state == DONGLE_CONNECTED ||
  2057. ds4->dongle_state == DONGLE_DISABLED) && !connected) {
  2058. hid_info(ps_dev->hdev, "DualShock 4 USB dongle: controller disconnected\n");
  2059. spin_lock_irqsave(&ps_dev->lock, flags);
  2060. ds4->dongle_state = DONGLE_DISCONNECTED;
  2061. spin_unlock_irqrestore(&ps_dev->lock, flags);
  2062. /* Return 0, so hidraw can get the report. */
  2063. return 0;
  2064. } else if (ds4->dongle_state == DONGLE_CALIBRATING ||
  2065. ds4->dongle_state == DONGLE_DISABLED ||
  2066. ds4->dongle_state == DONGLE_DISCONNECTED) {
  2067. /* Return 0, so hidraw can get the report. */
  2068. return 0;
  2069. }
  2070. }
  2071. if (connected)
  2072. return dualshock4_parse_report(ps_dev, report, data, size);
  2073. return 0;
  2074. }
  2075. static int dualshock4_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
  2076. {
  2077. struct hid_device *hdev = input_get_drvdata(dev);
  2078. struct dualshock4 *ds4 = hid_get_drvdata(hdev);
  2079. unsigned long flags;
  2080. if (effect->type != FF_RUMBLE)
  2081. return 0;
  2082. spin_lock_irqsave(&ds4->base.lock, flags);
  2083. ds4->update_rumble = true;
  2084. ds4->motor_left = effect->u.rumble.strong_magnitude / 256;
  2085. ds4->motor_right = effect->u.rumble.weak_magnitude / 256;
  2086. spin_unlock_irqrestore(&ds4->base.lock, flags);
  2087. dualshock4_schedule_work(ds4);
  2088. return 0;
  2089. }
  2090. static void dualshock4_remove(struct ps_device *ps_dev)
  2091. {
  2092. struct dualshock4 *ds4 = container_of(ps_dev, struct dualshock4, base);
  2093. unsigned long flags;
  2094. spin_lock_irqsave(&ds4->base.lock, flags);
  2095. ds4->output_worker_initialized = false;
  2096. spin_unlock_irqrestore(&ds4->base.lock, flags);
  2097. cancel_work_sync(&ds4->output_worker);
  2098. if (ps_dev->hdev->product == USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE)
  2099. cancel_work_sync(&ds4->dongle_hotplug_worker);
  2100. }
  2101. static inline void dualshock4_schedule_work(struct dualshock4 *ds4)
  2102. {
  2103. unsigned long flags;
  2104. spin_lock_irqsave(&ds4->base.lock, flags);
  2105. if (ds4->output_worker_initialized)
  2106. schedule_work(&ds4->output_worker);
  2107. spin_unlock_irqrestore(&ds4->base.lock, flags);
  2108. }
  2109. static void dualshock4_set_bt_poll_interval(struct dualshock4 *ds4, uint8_t interval)
  2110. {
  2111. ds4->bt_poll_interval = interval;
  2112. ds4->update_bt_poll_interval = true;
  2113. dualshock4_schedule_work(ds4);
  2114. }
  2115. /* Set default lightbar color based on player. */
  2116. static void dualshock4_set_default_lightbar_colors(struct dualshock4 *ds4)
  2117. {
  2118. /* Use same player colors as PlayStation 4.
  2119. * Array of colors is in RGB.
  2120. */
  2121. static const int player_colors[4][3] = {
  2122. { 0x00, 0x00, 0x40 }, /* Blue */
  2123. { 0x40, 0x00, 0x00 }, /* Red */
  2124. { 0x00, 0x40, 0x00 }, /* Green */
  2125. { 0x20, 0x00, 0x20 } /* Pink */
  2126. };
  2127. uint8_t player_id = ds4->base.player_id % ARRAY_SIZE(player_colors);
  2128. ds4->lightbar_enabled = true;
  2129. ds4->lightbar_red = player_colors[player_id][0];
  2130. ds4->lightbar_green = player_colors[player_id][1];
  2131. ds4->lightbar_blue = player_colors[player_id][2];
  2132. ds4->update_lightbar = true;
  2133. dualshock4_schedule_work(ds4);
  2134. }
  2135. static struct ps_device *dualshock4_create(struct hid_device *hdev)
  2136. {
  2137. struct dualshock4 *ds4;
  2138. struct ps_device *ps_dev;
  2139. uint8_t max_output_report_size;
  2140. int i, ret;
  2141. /* The DualShock4 has an RGB lightbar, which the original hid-sony driver
  2142. * exposed as a set of 4 LEDs for the 3 color channels and a global control.
  2143. * Ideally this should have used the multi-color LED class, which didn't exist
  2144. * yet. In addition the driver used a naming scheme not compliant with the LED
  2145. * naming spec by using "<mac_address>:<color>", which contained many colons.
  2146. * We use a more compliant by using "<device_name>:<color>" name now. Ideally
  2147. * would have been "<device_name>:<color>:indicator", but that would break
  2148. * existing applications (e.g. Android). Nothing matches against MAC address.
  2149. */
  2150. static const struct ps_led_info lightbar_leds_info[] = {
  2151. { NULL, "red", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness },
  2152. { NULL, "green", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness },
  2153. { NULL, "blue", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness },
  2154. { NULL, "global", 1, dualshock4_led_get_brightness, dualshock4_led_set_brightness,
  2155. dualshock4_led_set_blink },
  2156. };
  2157. ds4 = devm_kzalloc(&hdev->dev, sizeof(*ds4), GFP_KERNEL);
  2158. if (!ds4)
  2159. return ERR_PTR(-ENOMEM);
  2160. /*
  2161. * Patch version to allow userspace to distinguish between
  2162. * hid-generic vs hid-playstation axis and button mapping.
  2163. */
  2164. hdev->version |= HID_PLAYSTATION_VERSION_PATCH;
  2165. ps_dev = &ds4->base;
  2166. ps_dev->hdev = hdev;
  2167. spin_lock_init(&ps_dev->lock);
  2168. ps_dev->battery_capacity = 100; /* initial value until parse_report. */
  2169. ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
  2170. ps_dev->parse_report = dualshock4_parse_report;
  2171. ps_dev->remove = dualshock4_remove;
  2172. INIT_WORK(&ds4->output_worker, dualshock4_output_worker);
  2173. ds4->output_worker_initialized = true;
  2174. hid_set_drvdata(hdev, ds4);
  2175. max_output_report_size = sizeof(struct dualshock4_output_report_bt);
  2176. ds4->output_report_dmabuf = devm_kzalloc(&hdev->dev, max_output_report_size, GFP_KERNEL);
  2177. if (!ds4->output_report_dmabuf)
  2178. return ERR_PTR(-ENOMEM);
  2179. if (hdev->product == USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE) {
  2180. ds4->dongle_state = DONGLE_DISCONNECTED;
  2181. INIT_WORK(&ds4->dongle_hotplug_worker, dualshock4_dongle_calibration_work);
  2182. /* Override parse report for dongle specific hotplug handling. */
  2183. ps_dev->parse_report = dualshock4_dongle_parse_report;
  2184. }
  2185. ret = dualshock4_get_mac_address(ds4);
  2186. if (ret) {
  2187. hid_err(hdev, "Failed to get MAC address from DualShock4\n");
  2188. return ERR_PTR(ret);
  2189. }
  2190. snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds4->base.mac_address);
  2191. ret = dualshock4_get_firmware_info(ds4);
  2192. if (ret) {
  2193. hid_warn(hdev, "Failed to get firmware info from DualShock4\n");
  2194. hid_warn(hdev, "HW/FW version data in sysfs will be invalid.\n");
  2195. }
  2196. ret = ps_devices_list_add(ps_dev);
  2197. if (ret)
  2198. return ERR_PTR(ret);
  2199. ret = dualshock4_get_calibration_data(ds4);
  2200. if (ret) {
  2201. hid_warn(hdev, "Failed to get calibration data from DualShock4\n");
  2202. hid_warn(hdev, "Gyroscope and accelerometer will be inaccurate.\n");
  2203. }
  2204. ds4->gamepad = ps_gamepad_create(hdev, dualshock4_play_effect);
  2205. if (IS_ERR(ds4->gamepad)) {
  2206. ret = PTR_ERR(ds4->gamepad);
  2207. goto err;
  2208. }
  2209. /* Use gamepad input device name as primary device name for e.g. LEDs */
  2210. ps_dev->input_dev_name = dev_name(&ds4->gamepad->dev);
  2211. ds4->sensors = ps_sensors_create(hdev, DS4_ACC_RANGE, DS4_ACC_RES_PER_G,
  2212. DS4_GYRO_RANGE, DS4_GYRO_RES_PER_DEG_S);
  2213. if (IS_ERR(ds4->sensors)) {
  2214. ret = PTR_ERR(ds4->sensors);
  2215. goto err;
  2216. }
  2217. ds4->touchpad = ps_touchpad_create(hdev, DS4_TOUCHPAD_WIDTH, DS4_TOUCHPAD_HEIGHT, 2);
  2218. if (IS_ERR(ds4->touchpad)) {
  2219. ret = PTR_ERR(ds4->touchpad);
  2220. goto err;
  2221. }
  2222. ret = ps_device_register_battery(ps_dev);
  2223. if (ret)
  2224. goto err;
  2225. for (i = 0; i < ARRAY_SIZE(lightbar_leds_info); i++) {
  2226. const struct ps_led_info *led_info = &lightbar_leds_info[i];
  2227. ret = ps_led_register(ps_dev, &ds4->lightbar_leds[i], led_info);
  2228. if (ret < 0)
  2229. goto err;
  2230. }
  2231. dualshock4_set_bt_poll_interval(ds4, DS4_BT_DEFAULT_POLL_INTERVAL_MS);
  2232. ret = ps_device_set_player_id(ps_dev);
  2233. if (ret) {
  2234. hid_err(hdev, "Failed to assign player id for DualShock4: %d\n", ret);
  2235. goto err;
  2236. }
  2237. dualshock4_set_default_lightbar_colors(ds4);
  2238. /*
  2239. * Reporting hardware and firmware is important as there are frequent updates, which
  2240. * can change behavior.
  2241. */
  2242. hid_info(hdev, "Registered DualShock4 controller hw_version=0x%08x fw_version=0x%08x\n",
  2243. ds4->base.hw_version, ds4->base.fw_version);
  2244. return &ds4->base;
  2245. err:
  2246. ps_devices_list_remove(ps_dev);
  2247. return ERR_PTR(ret);
  2248. }
  2249. static int ps_raw_event(struct hid_device *hdev, struct hid_report *report,
  2250. u8 *data, int size)
  2251. {
  2252. struct ps_device *dev = hid_get_drvdata(hdev);
  2253. if (dev && dev->parse_report)
  2254. return dev->parse_report(dev, report, data, size);
  2255. return 0;
  2256. }
  2257. static int ps_probe(struct hid_device *hdev, const struct hid_device_id *id)
  2258. {
  2259. struct ps_device *dev;
  2260. int ret;
  2261. ret = hid_parse(hdev);
  2262. if (ret) {
  2263. hid_err(hdev, "Parse failed\n");
  2264. return ret;
  2265. }
  2266. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  2267. if (ret) {
  2268. hid_err(hdev, "Failed to start HID device\n");
  2269. return ret;
  2270. }
  2271. ret = hid_hw_open(hdev);
  2272. if (ret) {
  2273. hid_err(hdev, "Failed to open HID device\n");
  2274. goto err_stop;
  2275. }
  2276. if (id->driver_data == PS_TYPE_PS4_DUALSHOCK4) {
  2277. dev = dualshock4_create(hdev);
  2278. if (IS_ERR(dev)) {
  2279. hid_err(hdev, "Failed to create dualshock4.\n");
  2280. ret = PTR_ERR(dev);
  2281. goto err_close;
  2282. }
  2283. } else if (id->driver_data == PS_TYPE_PS5_DUALSENSE) {
  2284. dev = dualsense_create(hdev);
  2285. if (IS_ERR(dev)) {
  2286. hid_err(hdev, "Failed to create dualsense.\n");
  2287. ret = PTR_ERR(dev);
  2288. goto err_close;
  2289. }
  2290. }
  2291. return ret;
  2292. err_close:
  2293. hid_hw_close(hdev);
  2294. err_stop:
  2295. hid_hw_stop(hdev);
  2296. return ret;
  2297. }
  2298. static void ps_remove(struct hid_device *hdev)
  2299. {
  2300. struct ps_device *dev = hid_get_drvdata(hdev);
  2301. ps_devices_list_remove(dev);
  2302. ps_device_release_player_id(dev);
  2303. if (dev->remove)
  2304. dev->remove(dev);
  2305. hid_hw_close(hdev);
  2306. hid_hw_stop(hdev);
  2307. }
  2308. static const struct hid_device_id ps_devices[] = {
  2309. /* Sony DualShock 4 controllers for PS4 */
  2310. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
  2311. .driver_data = PS_TYPE_PS4_DUALSHOCK4 },
  2312. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
  2313. .driver_data = PS_TYPE_PS4_DUALSHOCK4 },
  2314. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
  2315. .driver_data = PS_TYPE_PS4_DUALSHOCK4 },
  2316. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
  2317. .driver_data = PS_TYPE_PS4_DUALSHOCK4 },
  2318. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE),
  2319. .driver_data = PS_TYPE_PS4_DUALSHOCK4 },
  2320. /* Sony DualSense controllers for PS5 */
  2321. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER),
  2322. .driver_data = PS_TYPE_PS5_DUALSENSE },
  2323. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER),
  2324. .driver_data = PS_TYPE_PS5_DUALSENSE },
  2325. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER_2),
  2326. .driver_data = PS_TYPE_PS5_DUALSENSE },
  2327. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER_2),
  2328. .driver_data = PS_TYPE_PS5_DUALSENSE },
  2329. { }
  2330. };
  2331. MODULE_DEVICE_TABLE(hid, ps_devices);
  2332. static struct hid_driver ps_driver = {
  2333. .name = "playstation",
  2334. .id_table = ps_devices,
  2335. .probe = ps_probe,
  2336. .remove = ps_remove,
  2337. .raw_event = ps_raw_event,
  2338. .driver = {
  2339. .dev_groups = ps_device_groups,
  2340. },
  2341. };
  2342. static int __init ps_init(void)
  2343. {
  2344. return hid_register_driver(&ps_driver);
  2345. }
  2346. static void __exit ps_exit(void)
  2347. {
  2348. hid_unregister_driver(&ps_driver);
  2349. ida_destroy(&ps_player_id_allocator);
  2350. }
  2351. module_init(ps_init);
  2352. module_exit(ps_exit);
  2353. MODULE_AUTHOR("Sony Interactive Entertainment");
  2354. MODULE_DESCRIPTION("HID Driver for PlayStation peripherals.");
  2355. MODULE_LICENSE("GPL");