hid-bigbenff.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 <[email protected]>
  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. spinlock_t lock;
  169. bool removed;
  170. u8 led_state; /* LED1 = 1 .. LED4 = 8 */
  171. u8 right_motor_on; /* right motor off/on 0/1 */
  172. u8 left_motor_force; /* left motor force 0-255 */
  173. struct led_classdev *leds[NUM_LEDS];
  174. bool work_led;
  175. bool work_ff;
  176. struct work_struct worker;
  177. };
  178. static inline void bigben_schedule_work(struct bigben_device *bigben)
  179. {
  180. unsigned long flags;
  181. spin_lock_irqsave(&bigben->lock, flags);
  182. if (!bigben->removed)
  183. schedule_work(&bigben->worker);
  184. spin_unlock_irqrestore(&bigben->lock, flags);
  185. }
  186. static void bigben_worker(struct work_struct *work)
  187. {
  188. struct bigben_device *bigben = container_of(work,
  189. struct bigben_device, worker);
  190. struct hid_field *report_field = bigben->report->field[0];
  191. bool do_work_led = false;
  192. bool do_work_ff = false;
  193. u8 *buf;
  194. u32 len;
  195. unsigned long flags;
  196. buf = hid_alloc_report_buf(bigben->report, GFP_KERNEL);
  197. if (!buf)
  198. return;
  199. len = hid_report_len(bigben->report);
  200. /* LED work */
  201. spin_lock_irqsave(&bigben->lock, flags);
  202. if (bigben->work_led) {
  203. bigben->work_led = false;
  204. do_work_led = true;
  205. report_field->value[0] = 0x01; /* 1 = led message */
  206. report_field->value[1] = 0x08; /* reserved value, always 8 */
  207. report_field->value[2] = bigben->led_state;
  208. report_field->value[3] = 0x00; /* padding */
  209. report_field->value[4] = 0x00; /* padding */
  210. report_field->value[5] = 0x00; /* padding */
  211. report_field->value[6] = 0x00; /* padding */
  212. report_field->value[7] = 0x00; /* padding */
  213. hid_output_report(bigben->report, buf);
  214. }
  215. spin_unlock_irqrestore(&bigben->lock, flags);
  216. if (do_work_led) {
  217. hid_hw_raw_request(bigben->hid, bigben->report->id, buf, len,
  218. bigben->report->type, HID_REQ_SET_REPORT);
  219. }
  220. /* FF work */
  221. spin_lock_irqsave(&bigben->lock, flags);
  222. if (bigben->work_ff) {
  223. bigben->work_ff = false;
  224. do_work_ff = true;
  225. report_field->value[0] = 0x02; /* 2 = rumble effect message */
  226. report_field->value[1] = 0x08; /* reserved value, always 8 */
  227. report_field->value[2] = bigben->right_motor_on;
  228. report_field->value[3] = bigben->left_motor_force;
  229. report_field->value[4] = 0xff; /* duration 0-254 (255 = nonstop) */
  230. report_field->value[5] = 0x00; /* padding */
  231. report_field->value[6] = 0x00; /* padding */
  232. report_field->value[7] = 0x00; /* padding */
  233. hid_output_report(bigben->report, buf);
  234. }
  235. spin_unlock_irqrestore(&bigben->lock, flags);
  236. if (do_work_ff) {
  237. hid_hw_raw_request(bigben->hid, bigben->report->id, buf, len,
  238. bigben->report->type, HID_REQ_SET_REPORT);
  239. }
  240. kfree(buf);
  241. }
  242. static int hid_bigben_play_effect(struct input_dev *dev, void *data,
  243. struct ff_effect *effect)
  244. {
  245. struct hid_device *hid = input_get_drvdata(dev);
  246. struct bigben_device *bigben = hid_get_drvdata(hid);
  247. u8 right_motor_on;
  248. u8 left_motor_force;
  249. unsigned long flags;
  250. if (!bigben) {
  251. hid_err(hid, "no device data\n");
  252. return 0;
  253. }
  254. if (effect->type != FF_RUMBLE)
  255. return 0;
  256. right_motor_on = effect->u.rumble.weak_magnitude ? 1 : 0;
  257. left_motor_force = effect->u.rumble.strong_magnitude / 256;
  258. if (right_motor_on != bigben->right_motor_on ||
  259. left_motor_force != bigben->left_motor_force) {
  260. spin_lock_irqsave(&bigben->lock, flags);
  261. bigben->right_motor_on = right_motor_on;
  262. bigben->left_motor_force = left_motor_force;
  263. bigben->work_ff = true;
  264. spin_unlock_irqrestore(&bigben->lock, flags);
  265. bigben_schedule_work(bigben);
  266. }
  267. return 0;
  268. }
  269. static void bigben_set_led(struct led_classdev *led,
  270. enum led_brightness value)
  271. {
  272. struct device *dev = led->dev->parent;
  273. struct hid_device *hid = to_hid_device(dev);
  274. struct bigben_device *bigben = hid_get_drvdata(hid);
  275. int n;
  276. bool work;
  277. unsigned long flags;
  278. if (!bigben) {
  279. hid_err(hid, "no device data\n");
  280. return;
  281. }
  282. for (n = 0; n < NUM_LEDS; n++) {
  283. if (led == bigben->leds[n]) {
  284. spin_lock_irqsave(&bigben->lock, flags);
  285. if (value == LED_OFF) {
  286. work = (bigben->led_state & BIT(n));
  287. bigben->led_state &= ~BIT(n);
  288. } else {
  289. work = !(bigben->led_state & BIT(n));
  290. bigben->led_state |= BIT(n);
  291. }
  292. spin_unlock_irqrestore(&bigben->lock, flags);
  293. if (work) {
  294. bigben->work_led = true;
  295. bigben_schedule_work(bigben);
  296. }
  297. return;
  298. }
  299. }
  300. }
  301. static enum led_brightness bigben_get_led(struct led_classdev *led)
  302. {
  303. struct device *dev = led->dev->parent;
  304. struct hid_device *hid = to_hid_device(dev);
  305. struct bigben_device *bigben = hid_get_drvdata(hid);
  306. int n;
  307. if (!bigben) {
  308. hid_err(hid, "no device data\n");
  309. return LED_OFF;
  310. }
  311. for (n = 0; n < NUM_LEDS; n++) {
  312. if (led == bigben->leds[n])
  313. return (bigben->led_state & BIT(n)) ? LED_ON : LED_OFF;
  314. }
  315. return LED_OFF;
  316. }
  317. static void bigben_remove(struct hid_device *hid)
  318. {
  319. struct bigben_device *bigben = hid_get_drvdata(hid);
  320. unsigned long flags;
  321. spin_lock_irqsave(&bigben->lock, flags);
  322. bigben->removed = true;
  323. spin_unlock_irqrestore(&bigben->lock, flags);
  324. cancel_work_sync(&bigben->worker);
  325. hid_hw_stop(hid);
  326. }
  327. static int bigben_probe(struct hid_device *hid,
  328. const struct hid_device_id *id)
  329. {
  330. struct bigben_device *bigben;
  331. struct hid_input *hidinput;
  332. struct led_classdev *led;
  333. char *name;
  334. size_t name_sz;
  335. int n, error;
  336. bigben = devm_kzalloc(&hid->dev, sizeof(*bigben), GFP_KERNEL);
  337. if (!bigben)
  338. return -ENOMEM;
  339. hid_set_drvdata(hid, bigben);
  340. bigben->hid = hid;
  341. bigben->removed = false;
  342. error = hid_parse(hid);
  343. if (error) {
  344. hid_err(hid, "parse failed\n");
  345. return error;
  346. }
  347. error = hid_hw_start(hid, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  348. if (error) {
  349. hid_err(hid, "hw start failed\n");
  350. return error;
  351. }
  352. bigben->report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 8);
  353. if (!bigben->report) {
  354. hid_err(hid, "no output report found\n");
  355. error = -ENODEV;
  356. goto error_hw_stop;
  357. }
  358. if (list_empty(&hid->inputs)) {
  359. hid_err(hid, "no inputs found\n");
  360. error = -ENODEV;
  361. goto error_hw_stop;
  362. }
  363. hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
  364. set_bit(FF_RUMBLE, hidinput->input->ffbit);
  365. INIT_WORK(&bigben->worker, bigben_worker);
  366. spin_lock_init(&bigben->lock);
  367. error = input_ff_create_memless(hidinput->input, NULL,
  368. hid_bigben_play_effect);
  369. if (error)
  370. goto error_hw_stop;
  371. name_sz = strlen(dev_name(&hid->dev)) + strlen(":red:bigben#") + 1;
  372. for (n = 0; n < NUM_LEDS; n++) {
  373. led = devm_kzalloc(
  374. &hid->dev,
  375. sizeof(struct led_classdev) + name_sz,
  376. GFP_KERNEL
  377. );
  378. if (!led) {
  379. error = -ENOMEM;
  380. goto error_hw_stop;
  381. }
  382. name = (void *)(&led[1]);
  383. snprintf(name, name_sz,
  384. "%s:red:bigben%d",
  385. dev_name(&hid->dev), n + 1
  386. );
  387. led->name = name;
  388. led->brightness = (n == 0) ? LED_ON : LED_OFF;
  389. led->max_brightness = 1;
  390. led->brightness_get = bigben_get_led;
  391. led->brightness_set = bigben_set_led;
  392. bigben->leds[n] = led;
  393. error = devm_led_classdev_register(&hid->dev, led);
  394. if (error)
  395. goto error_hw_stop;
  396. }
  397. /* initial state: LED1 is on, no rumble effect */
  398. bigben->led_state = BIT(0);
  399. bigben->right_motor_on = 0;
  400. bigben->left_motor_force = 0;
  401. bigben->work_led = true;
  402. bigben->work_ff = true;
  403. bigben_schedule_work(bigben);
  404. hid_info(hid, "LED and force feedback support for BigBen gamepad\n");
  405. return 0;
  406. error_hw_stop:
  407. hid_hw_stop(hid);
  408. return error;
  409. }
  410. static __u8 *bigben_report_fixup(struct hid_device *hid, __u8 *rdesc,
  411. unsigned int *rsize)
  412. {
  413. if (*rsize == PID0902_RDESC_ORIG_SIZE) {
  414. rdesc = pid0902_rdesc_fixed;
  415. *rsize = sizeof(pid0902_rdesc_fixed);
  416. } else
  417. hid_warn(hid, "unexpected rdesc, please submit for review\n");
  418. return rdesc;
  419. }
  420. static const struct hid_device_id bigben_devices[] = {
  421. { HID_USB_DEVICE(USB_VENDOR_ID_BIGBEN, USB_DEVICE_ID_BIGBEN_PS3OFMINIPAD) },
  422. { }
  423. };
  424. MODULE_DEVICE_TABLE(hid, bigben_devices);
  425. static struct hid_driver bigben_driver = {
  426. .name = "bigben",
  427. .id_table = bigben_devices,
  428. .probe = bigben_probe,
  429. .report_fixup = bigben_report_fixup,
  430. .remove = bigben_remove,
  431. };
  432. module_hid_driver(bigben_driver);
  433. MODULE_LICENSE("GPL");