hid-bigbenff.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * LED & force feedback support for BigBen Interactive
  4. *
  5. * 0x146b:0x0902 "Bigben Interactive Bigben Game Pad"
  6. * "Kid-friendly Wired Controller" PS3OFMINIPAD SONY
  7. * sold for use with the PS3
  8. *
  9. * Copyright (c) 2018 Hanno Zulla <kontakt@hanno.de>
  10. */
  11. #include <linux/input.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/leds.h>
  15. #include <linux/hid.h>
  16. #include "hid-ids.h"
  17. /*
  18. * The original descriptor for 0x146b:0x0902
  19. *
  20. * 0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
  21. * 0x09, 0x05, // Usage (Game Pad)
  22. * 0xA1, 0x01, // Collection (Application)
  23. * 0x15, 0x00, // Logical Minimum (0)
  24. * 0x25, 0x01, // Logical Maximum (1)
  25. * 0x35, 0x00, // Physical Minimum (0)
  26. * 0x45, 0x01, // Physical Maximum (1)
  27. * 0x75, 0x01, // Report Size (1)
  28. * 0x95, 0x0D, // Report Count (13)
  29. * 0x05, 0x09, // Usage Page (Button)
  30. * 0x19, 0x01, // Usage Minimum (0x01)
  31. * 0x29, 0x0D, // Usage Maximum (0x0D)
  32. * 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
  33. * 0x95, 0x03, // Report Count (3)
  34. * 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
  35. * 0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
  36. * 0x25, 0x07, // Logical Maximum (7)
  37. * 0x46, 0x3B, 0x01, // Physical Maximum (315)
  38. * 0x75, 0x04, // Report Size (4)
  39. * 0x95, 0x01, // Report Count (1)
  40. * 0x65, 0x14, // Unit (System: English Rotation, Length: Centimeter)
  41. * 0x09, 0x39, // Usage (Hat switch)
  42. * 0x81, 0x42, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,Null State)
  43. * 0x65, 0x00, // Unit (None)
  44. * 0x95, 0x01, // Report Count (1)
  45. * 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
  46. * 0x26, 0xFF, 0x00, // Logical Maximum (255)
  47. * 0x46, 0xFF, 0x00, // Physical Maximum (255)
  48. * 0x09, 0x30, // Usage (X)
  49. * 0x09, 0x31, // Usage (Y)
  50. * 0x09, 0x32, // Usage (Z)
  51. * 0x09, 0x35, // Usage (Rz)
  52. * 0x75, 0x08, // Report Size (8)
  53. * 0x95, 0x04, // Report Count (4)
  54. * 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
  55. * 0x06, 0x00, 0xFF, // Usage Page (Vendor Defined 0xFF00)
  56. * 0x09, 0x20, // Usage (0x20)
  57. * 0x09, 0x21, // Usage (0x21)
  58. * 0x09, 0x22, // Usage (0x22)
  59. * 0x09, 0x23, // Usage (0x23)
  60. * 0x09, 0x24, // Usage (0x24)
  61. * 0x09, 0x25, // Usage (0x25)
  62. * 0x09, 0x26, // Usage (0x26)
  63. * 0x09, 0x27, // Usage (0x27)
  64. * 0x09, 0x28, // Usage (0x28)
  65. * 0x09, 0x29, // Usage (0x29)
  66. * 0x09, 0x2A, // Usage (0x2A)
  67. * 0x09, 0x2B, // Usage (0x2B)
  68. * 0x95, 0x0C, // Report Count (12)
  69. * 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
  70. * 0x0A, 0x21, 0x26, // Usage (0x2621)
  71. * 0x95, 0x08, // Report Count (8)
  72. * 0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
  73. * 0x0A, 0x21, 0x26, // Usage (0x2621)
  74. * 0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
  75. * 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  76. * 0x46, 0xFF, 0x03, // Physical Maximum (1023)
  77. * 0x09, 0x2C, // Usage (0x2C)
  78. * 0x09, 0x2D, // Usage (0x2D)
  79. * 0x09, 0x2E, // Usage (0x2E)
  80. * 0x09, 0x2F, // Usage (0x2F)
  81. * 0x75, 0x10, // Report Size (16)
  82. * 0x95, 0x04, // Report Count (4)
  83. * 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
  84. * 0xC0, // End Collection
  85. */
  86. #define PID0902_RDESC_ORIG_SIZE 137
  87. /*
  88. * The fixed descriptor for 0x146b:0x0902
  89. *
  90. * - map buttons according to gamepad.rst
  91. * - assign right stick from Z/Rz to Rx/Ry
  92. * - map previously unused analog trigger data to Z/RZ
  93. * - simplify feature and output descriptor
  94. */
  95. static __u8 pid0902_rdesc_fixed[] = {
  96. 0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
  97. 0x09, 0x05, /* Usage (Game Pad) */
  98. 0xA1, 0x01, /* Collection (Application) */
  99. 0x15, 0x00, /* Logical Minimum (0) */
  100. 0x25, 0x01, /* Logical Maximum (1) */
  101. 0x35, 0x00, /* Physical Minimum (0) */
  102. 0x45, 0x01, /* Physical Maximum (1) */
  103. 0x75, 0x01, /* Report Size (1) */
  104. 0x95, 0x0D, /* Report Count (13) */
  105. 0x05, 0x09, /* Usage Page (Button) */
  106. 0x09, 0x05, /* Usage (BTN_WEST) */
  107. 0x09, 0x01, /* Usage (BTN_SOUTH) */
  108. 0x09, 0x02, /* Usage (BTN_EAST) */
  109. 0x09, 0x04, /* Usage (BTN_NORTH) */
  110. 0x09, 0x07, /* Usage (BTN_TL) */
  111. 0x09, 0x08, /* Usage (BTN_TR) */
  112. 0x09, 0x09, /* Usage (BTN_TL2) */
  113. 0x09, 0x0A, /* Usage (BTN_TR2) */
  114. 0x09, 0x0B, /* Usage (BTN_SELECT) */
  115. 0x09, 0x0C, /* Usage (BTN_START) */
  116. 0x09, 0x0E, /* Usage (BTN_THUMBL) */
  117. 0x09, 0x0F, /* Usage (BTN_THUMBR) */
  118. 0x09, 0x0D, /* Usage (BTN_MODE) */
  119. 0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */
  120. 0x75, 0x01, /* Report Size (1) */
  121. 0x95, 0x03, /* Report Count (3) */
  122. 0x81, 0x01, /* Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) */
  123. 0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
  124. 0x25, 0x07, /* Logical Maximum (7) */
  125. 0x46, 0x3B, 0x01, /* Physical Maximum (315) */
  126. 0x75, 0x04, /* Report Size (4) */
  127. 0x95, 0x01, /* Report Count (1) */
  128. 0x65, 0x14, /* Unit (System: English Rotation, Length: Centimeter) */
  129. 0x09, 0x39, /* Usage (Hat switch) */
  130. 0x81, 0x42, /* Input (Data,Var,Abs,No Wrap,Linear,Preferred State,Null State) */
  131. 0x65, 0x00, /* Unit (None) */
  132. 0x95, 0x01, /* Report Count (1) */
  133. 0x81, 0x01, /* Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) */
  134. 0x26, 0xFF, 0x00, /* Logical Maximum (255) */
  135. 0x46, 0xFF, 0x00, /* Physical Maximum (255) */
  136. 0x09, 0x30, /* Usage (X) */
  137. 0x09, 0x31, /* Usage (Y) */
  138. 0x09, 0x33, /* Usage (Rx) */
  139. 0x09, 0x34, /* Usage (Ry) */
  140. 0x75, 0x08, /* Report Size (8) */
  141. 0x95, 0x04, /* Report Count (4) */
  142. 0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */
  143. 0x95, 0x0A, /* Report Count (10) */
  144. 0x81, 0x01, /* Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) */
  145. 0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
  146. 0x26, 0xFF, 0x00, /* Logical Maximum (255) */
  147. 0x46, 0xFF, 0x00, /* Physical Maximum (255) */
  148. 0x09, 0x32, /* Usage (Z) */
  149. 0x09, 0x35, /* Usage (Rz) */
  150. 0x95, 0x02, /* Report Count (2) */
  151. 0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */
  152. 0x95, 0x08, /* Report Count (8) */
  153. 0x81, 0x01, /* Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) */
  154. 0x06, 0x00, 0xFF, /* Usage Page (Vendor Defined 0xFF00) */
  155. 0xB1, 0x02, /* Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) */
  156. 0x0A, 0x21, 0x26, /* Usage (0x2621) */
  157. 0x95, 0x08, /* Report Count (8) */
  158. 0x91, 0x02, /* Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) */
  159. 0x0A, 0x21, 0x26, /* Usage (0x2621) */
  160. 0x95, 0x08, /* Report Count (8) */
  161. 0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */
  162. 0xC0, /* End Collection */
  163. };
  164. #define NUM_LEDS 4
  165. struct bigben_device {
  166. struct hid_device *hid;
  167. struct hid_report *report;
  168. bool removed;
  169. u8 led_state; /* LED1 = 1 .. LED4 = 8 */
  170. u8 right_motor_on; /* right motor off/on 0/1 */
  171. u8 left_motor_force; /* left motor force 0-255 */
  172. struct led_classdev *leds[NUM_LEDS];
  173. bool work_led;
  174. bool work_ff;
  175. struct work_struct worker;
  176. };
  177. static void bigben_worker(struct work_struct *work)
  178. {
  179. struct bigben_device *bigben = container_of(work,
  180. struct bigben_device, worker);
  181. struct hid_field *report_field = bigben->report->field[0];
  182. if (bigben->removed || !report_field)
  183. return;
  184. if (bigben->work_led) {
  185. bigben->work_led = false;
  186. report_field->value[0] = 0x01; /* 1 = led message */
  187. report_field->value[1] = 0x08; /* reserved value, always 8 */
  188. report_field->value[2] = bigben->led_state;
  189. report_field->value[3] = 0x00; /* padding */
  190. report_field->value[4] = 0x00; /* padding */
  191. report_field->value[5] = 0x00; /* padding */
  192. report_field->value[6] = 0x00; /* padding */
  193. report_field->value[7] = 0x00; /* padding */
  194. hid_hw_request(bigben->hid, bigben->report, HID_REQ_SET_REPORT);
  195. }
  196. if (bigben->work_ff) {
  197. bigben->work_ff = false;
  198. report_field->value[0] = 0x02; /* 2 = rumble effect message */
  199. report_field->value[1] = 0x08; /* reserved value, always 8 */
  200. report_field->value[2] = bigben->right_motor_on;
  201. report_field->value[3] = bigben->left_motor_force;
  202. report_field->value[4] = 0xff; /* duration 0-254 (255 = nonstop) */
  203. report_field->value[5] = 0x00; /* padding */
  204. report_field->value[6] = 0x00; /* padding */
  205. report_field->value[7] = 0x00; /* padding */
  206. hid_hw_request(bigben->hid, bigben->report, HID_REQ_SET_REPORT);
  207. }
  208. }
  209. static int hid_bigben_play_effect(struct input_dev *dev, void *data,
  210. struct ff_effect *effect)
  211. {
  212. struct hid_device *hid = input_get_drvdata(dev);
  213. struct bigben_device *bigben = hid_get_drvdata(hid);
  214. u8 right_motor_on;
  215. u8 left_motor_force;
  216. if (!bigben) {
  217. hid_err(hid, "no device data\n");
  218. return 0;
  219. }
  220. if (effect->type != FF_RUMBLE)
  221. return 0;
  222. right_motor_on = effect->u.rumble.weak_magnitude ? 1 : 0;
  223. left_motor_force = effect->u.rumble.strong_magnitude / 256;
  224. if (right_motor_on != bigben->right_motor_on ||
  225. left_motor_force != bigben->left_motor_force) {
  226. bigben->right_motor_on = right_motor_on;
  227. bigben->left_motor_force = left_motor_force;
  228. bigben->work_ff = true;
  229. schedule_work(&bigben->worker);
  230. }
  231. return 0;
  232. }
  233. static void bigben_set_led(struct led_classdev *led,
  234. enum led_brightness value)
  235. {
  236. struct device *dev = led->dev->parent;
  237. struct hid_device *hid = to_hid_device(dev);
  238. struct bigben_device *bigben = hid_get_drvdata(hid);
  239. int n;
  240. bool work;
  241. if (!bigben) {
  242. hid_err(hid, "no device data\n");
  243. return;
  244. }
  245. for (n = 0; n < NUM_LEDS; n++) {
  246. if (led == bigben->leds[n]) {
  247. if (value == LED_OFF) {
  248. work = (bigben->led_state & BIT(n));
  249. bigben->led_state &= ~BIT(n);
  250. } else {
  251. work = !(bigben->led_state & BIT(n));
  252. bigben->led_state |= BIT(n);
  253. }
  254. if (work) {
  255. bigben->work_led = true;
  256. schedule_work(&bigben->worker);
  257. }
  258. return;
  259. }
  260. }
  261. }
  262. static enum led_brightness bigben_get_led(struct led_classdev *led)
  263. {
  264. struct device *dev = led->dev->parent;
  265. struct hid_device *hid = to_hid_device(dev);
  266. struct bigben_device *bigben = hid_get_drvdata(hid);
  267. int n;
  268. if (!bigben) {
  269. hid_err(hid, "no device data\n");
  270. return LED_OFF;
  271. }
  272. for (n = 0; n < NUM_LEDS; n++) {
  273. if (led == bigben->leds[n])
  274. return (bigben->led_state & BIT(n)) ? LED_ON : LED_OFF;
  275. }
  276. return LED_OFF;
  277. }
  278. static void bigben_remove(struct hid_device *hid)
  279. {
  280. struct bigben_device *bigben = hid_get_drvdata(hid);
  281. bigben->removed = true;
  282. cancel_work_sync(&bigben->worker);
  283. hid_hw_stop(hid);
  284. }
  285. static int bigben_probe(struct hid_device *hid,
  286. const struct hid_device_id *id)
  287. {
  288. struct bigben_device *bigben;
  289. struct hid_input *hidinput;
  290. struct list_head *report_list;
  291. struct led_classdev *led;
  292. char *name;
  293. size_t name_sz;
  294. int n, error;
  295. bigben = devm_kzalloc(&hid->dev, sizeof(*bigben), GFP_KERNEL);
  296. if (!bigben)
  297. return -ENOMEM;
  298. hid_set_drvdata(hid, bigben);
  299. bigben->hid = hid;
  300. bigben->removed = false;
  301. error = hid_parse(hid);
  302. if (error) {
  303. hid_err(hid, "parse failed\n");
  304. return error;
  305. }
  306. error = hid_hw_start(hid, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  307. if (error) {
  308. hid_err(hid, "hw start failed\n");
  309. return error;
  310. }
  311. report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  312. bigben->report = list_entry(report_list->next,
  313. struct hid_report, list);
  314. hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
  315. set_bit(FF_RUMBLE, hidinput->input->ffbit);
  316. INIT_WORK(&bigben->worker, bigben_worker);
  317. error = input_ff_create_memless(hidinput->input, NULL,
  318. hid_bigben_play_effect);
  319. if (error)
  320. goto error_hw_stop;
  321. name_sz = strlen(dev_name(&hid->dev)) + strlen(":red:bigben#") + 1;
  322. for (n = 0; n < NUM_LEDS; n++) {
  323. led = devm_kzalloc(
  324. &hid->dev,
  325. sizeof(struct led_classdev) + name_sz,
  326. GFP_KERNEL
  327. );
  328. if (!led) {
  329. error = -ENOMEM;
  330. goto error_hw_stop;
  331. }
  332. name = (void *)(&led[1]);
  333. snprintf(name, name_sz,
  334. "%s:red:bigben%d",
  335. dev_name(&hid->dev), n + 1
  336. );
  337. led->name = name;
  338. led->brightness = (n == 0) ? LED_ON : LED_OFF;
  339. led->max_brightness = 1;
  340. led->brightness_get = bigben_get_led;
  341. led->brightness_set = bigben_set_led;
  342. bigben->leds[n] = led;
  343. error = devm_led_classdev_register(&hid->dev, led);
  344. if (error)
  345. goto error_hw_stop;
  346. }
  347. /* initial state: LED1 is on, no rumble effect */
  348. bigben->led_state = BIT(0);
  349. bigben->right_motor_on = 0;
  350. bigben->left_motor_force = 0;
  351. bigben->work_led = true;
  352. bigben->work_ff = true;
  353. schedule_work(&bigben->worker);
  354. hid_info(hid, "LED and force feedback support for BigBen gamepad\n");
  355. return 0;
  356. error_hw_stop:
  357. hid_hw_stop(hid);
  358. return error;
  359. }
  360. static __u8 *bigben_report_fixup(struct hid_device *hid, __u8 *rdesc,
  361. unsigned int *rsize)
  362. {
  363. if (*rsize == PID0902_RDESC_ORIG_SIZE) {
  364. rdesc = pid0902_rdesc_fixed;
  365. *rsize = sizeof(pid0902_rdesc_fixed);
  366. } else
  367. hid_warn(hid, "unexpected rdesc, please submit for review\n");
  368. return rdesc;
  369. }
  370. static const struct hid_device_id bigben_devices[] = {
  371. { HID_USB_DEVICE(USB_VENDOR_ID_BIGBEN, USB_DEVICE_ID_BIGBEN_PS3OFMINIPAD) },
  372. { }
  373. };
  374. MODULE_DEVICE_TABLE(hid, bigben_devices);
  375. static struct hid_driver bigben_driver = {
  376. .name = "bigben",
  377. .id_table = bigben_devices,
  378. .probe = bigben_probe,
  379. .report_fixup = bigben_report_fixup,
  380. .remove = bigben_remove,
  381. };
  382. module_hid_driver(bigben_driver);
  383. MODULE_LICENSE("GPL");