18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Force feedback support for DragonRise Inc. game controllers
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * From what I have gathered, these devices are mass produced in China and are
68c2ecf20Sopenharmony_ci * distributed under several vendors. They often share the same design as
78c2ecf20Sopenharmony_ci * the original PlayStation DualShock controller.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * 0079:0006 "DragonRise Inc.   Generic   USB  Joystick  "
108c2ecf20Sopenharmony_ci *  - tested with a Tesun USB-703 game controller.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Copyright (c) 2009 Richard Walmsley <richwalm@gmail.com>
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/input.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/hid.h>
218c2ecf20Sopenharmony_ci#include <linux/module.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include "hid-ids.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#ifdef CONFIG_DRAGONRISE_FF
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct drff_device {
288c2ecf20Sopenharmony_ci	struct hid_report *report;
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic int drff_play(struct input_dev *dev, void *data,
328c2ecf20Sopenharmony_ci				 struct ff_effect *effect)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct hid_device *hid = input_get_drvdata(dev);
358c2ecf20Sopenharmony_ci	struct drff_device *drff = data;
368c2ecf20Sopenharmony_ci	int strong, weak;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	strong = effect->u.rumble.strong_magnitude;
398c2ecf20Sopenharmony_ci	weak = effect->u.rumble.weak_magnitude;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	dbg_hid("called with 0x%04x 0x%04x", strong, weak);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	if (strong || weak) {
448c2ecf20Sopenharmony_ci		strong = strong * 0xff / 0xffff;
458c2ecf20Sopenharmony_ci		weak = weak * 0xff / 0xffff;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci		/* While reverse engineering this device, I found that when
488c2ecf20Sopenharmony_ci		   this value is set, it causes the strong rumble to function
498c2ecf20Sopenharmony_ci		   at a near maximum speed, so we'll bypass it. */
508c2ecf20Sopenharmony_ci		if (weak == 0x0a)
518c2ecf20Sopenharmony_ci			weak = 0x0b;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci		drff->report->field[0]->value[0] = 0x51;
548c2ecf20Sopenharmony_ci		drff->report->field[0]->value[1] = 0x00;
558c2ecf20Sopenharmony_ci		drff->report->field[0]->value[2] = weak;
568c2ecf20Sopenharmony_ci		drff->report->field[0]->value[4] = strong;
578c2ecf20Sopenharmony_ci		hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci		drff->report->field[0]->value[0] = 0xfa;
608c2ecf20Sopenharmony_ci		drff->report->field[0]->value[1] = 0xfe;
618c2ecf20Sopenharmony_ci	} else {
628c2ecf20Sopenharmony_ci		drff->report->field[0]->value[0] = 0xf3;
638c2ecf20Sopenharmony_ci		drff->report->field[0]->value[1] = 0x00;
648c2ecf20Sopenharmony_ci	}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	drff->report->field[0]->value[2] = 0x00;
678c2ecf20Sopenharmony_ci	drff->report->field[0]->value[4] = 0x00;
688c2ecf20Sopenharmony_ci	dbg_hid("running with 0x%02x 0x%02x", strong, weak);
698c2ecf20Sopenharmony_ci	hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	return 0;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic int drff_init(struct hid_device *hid)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct drff_device *drff;
778c2ecf20Sopenharmony_ci	struct hid_report *report;
788c2ecf20Sopenharmony_ci	struct hid_input *hidinput;
798c2ecf20Sopenharmony_ci	struct list_head *report_list =
808c2ecf20Sopenharmony_ci			&hid->report_enum[HID_OUTPUT_REPORT].report_list;
818c2ecf20Sopenharmony_ci	struct input_dev *dev;
828c2ecf20Sopenharmony_ci	int error;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (list_empty(&hid->inputs)) {
858c2ecf20Sopenharmony_ci		hid_err(hid, "no inputs found\n");
868c2ecf20Sopenharmony_ci		return -ENODEV;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci	hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
898c2ecf20Sopenharmony_ci	dev = hidinput->input;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (list_empty(report_list)) {
928c2ecf20Sopenharmony_ci		hid_err(hid, "no output reports found\n");
938c2ecf20Sopenharmony_ci		return -ENODEV;
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	report = list_first_entry(report_list, struct hid_report, list);
978c2ecf20Sopenharmony_ci	if (report->maxfield < 1) {
988c2ecf20Sopenharmony_ci		hid_err(hid, "no fields in the report\n");
998c2ecf20Sopenharmony_ci		return -ENODEV;
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	if (report->field[0]->report_count < 7) {
1038c2ecf20Sopenharmony_ci		hid_err(hid, "not enough values in the field\n");
1048c2ecf20Sopenharmony_ci		return -ENODEV;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	drff = kzalloc(sizeof(struct drff_device), GFP_KERNEL);
1088c2ecf20Sopenharmony_ci	if (!drff)
1098c2ecf20Sopenharmony_ci		return -ENOMEM;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	set_bit(FF_RUMBLE, dev->ffbit);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	error = input_ff_create_memless(dev, drff, drff_play);
1148c2ecf20Sopenharmony_ci	if (error) {
1158c2ecf20Sopenharmony_ci		kfree(drff);
1168c2ecf20Sopenharmony_ci		return error;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	drff->report = report;
1208c2ecf20Sopenharmony_ci	drff->report->field[0]->value[0] = 0xf3;
1218c2ecf20Sopenharmony_ci	drff->report->field[0]->value[1] = 0x00;
1228c2ecf20Sopenharmony_ci	drff->report->field[0]->value[2] = 0x00;
1238c2ecf20Sopenharmony_ci	drff->report->field[0]->value[3] = 0x00;
1248c2ecf20Sopenharmony_ci	drff->report->field[0]->value[4] = 0x00;
1258c2ecf20Sopenharmony_ci	drff->report->field[0]->value[5] = 0x00;
1268c2ecf20Sopenharmony_ci	drff->report->field[0]->value[6] = 0x00;
1278c2ecf20Sopenharmony_ci	hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	hid_info(hid, "Force Feedback for DragonRise Inc. "
1308c2ecf20Sopenharmony_ci		 "game controllers by Richard Walmsley <richwalm@gmail.com>\n");
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	return 0;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci#else
1358c2ecf20Sopenharmony_cistatic inline int drff_init(struct hid_device *hid)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	return 0;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci#endif
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/*
1428c2ecf20Sopenharmony_ci * The original descriptor of joystick with PID 0x0011, represented by DVTech PC
1438c2ecf20Sopenharmony_ci * JS19. It seems both copied from another device and a result of confusion
1448c2ecf20Sopenharmony_ci * either about the specification or about the program used to create the
1458c2ecf20Sopenharmony_ci * descriptor. In any case, it's a wonder it works on Windows.
1468c2ecf20Sopenharmony_ci *
1478c2ecf20Sopenharmony_ci *  Usage Page (Desktop),             ; Generic desktop controls (01h)
1488c2ecf20Sopenharmony_ci *  Usage (Joystick),                 ; Joystick (04h, application collection)
1498c2ecf20Sopenharmony_ci *  Collection (Application),
1508c2ecf20Sopenharmony_ci *    Collection (Logical),
1518c2ecf20Sopenharmony_ci *      Report Size (8),
1528c2ecf20Sopenharmony_ci *      Report Count (5),
1538c2ecf20Sopenharmony_ci *      Logical Minimum (0),
1548c2ecf20Sopenharmony_ci *      Logical Maximum (255),
1558c2ecf20Sopenharmony_ci *      Physical Minimum (0),
1568c2ecf20Sopenharmony_ci *      Physical Maximum (255),
1578c2ecf20Sopenharmony_ci *      Usage (X),                    ; X (30h, dynamic value)
1588c2ecf20Sopenharmony_ci *      Usage (X),                    ; X (30h, dynamic value)
1598c2ecf20Sopenharmony_ci *      Usage (X),                    ; X (30h, dynamic value)
1608c2ecf20Sopenharmony_ci *      Usage (X),                    ; X (30h, dynamic value)
1618c2ecf20Sopenharmony_ci *      Usage (Y),                    ; Y (31h, dynamic value)
1628c2ecf20Sopenharmony_ci *      Input (Variable),
1638c2ecf20Sopenharmony_ci *      Report Size (4),
1648c2ecf20Sopenharmony_ci *      Report Count (1),
1658c2ecf20Sopenharmony_ci *      Logical Maximum (7),
1668c2ecf20Sopenharmony_ci *      Physical Maximum (315),
1678c2ecf20Sopenharmony_ci *      Unit (Degrees),
1688c2ecf20Sopenharmony_ci *      Usage (00h),
1698c2ecf20Sopenharmony_ci *      Input (Variable, Null State),
1708c2ecf20Sopenharmony_ci *      Unit,
1718c2ecf20Sopenharmony_ci *      Report Size (1),
1728c2ecf20Sopenharmony_ci *      Report Count (10),
1738c2ecf20Sopenharmony_ci *      Logical Maximum (1),
1748c2ecf20Sopenharmony_ci *      Physical Maximum (1),
1758c2ecf20Sopenharmony_ci *      Usage Page (Button),          ; Button (09h)
1768c2ecf20Sopenharmony_ci *      Usage Minimum (01h),
1778c2ecf20Sopenharmony_ci *      Usage Maximum (0Ah),
1788c2ecf20Sopenharmony_ci *      Input (Variable),
1798c2ecf20Sopenharmony_ci *      Usage Page (FF00h),           ; FF00h, vendor-defined
1808c2ecf20Sopenharmony_ci *      Report Size (1),
1818c2ecf20Sopenharmony_ci *      Report Count (10),
1828c2ecf20Sopenharmony_ci *      Logical Maximum (1),
1838c2ecf20Sopenharmony_ci *      Physical Maximum (1),
1848c2ecf20Sopenharmony_ci *      Usage (01h),
1858c2ecf20Sopenharmony_ci *      Input (Variable),
1868c2ecf20Sopenharmony_ci *    End Collection,
1878c2ecf20Sopenharmony_ci *    Collection (Logical),
1888c2ecf20Sopenharmony_ci *      Report Size (8),
1898c2ecf20Sopenharmony_ci *      Report Count (4),
1908c2ecf20Sopenharmony_ci *      Physical Maximum (255),
1918c2ecf20Sopenharmony_ci *      Logical Maximum (255),
1928c2ecf20Sopenharmony_ci *      Usage (02h),
1938c2ecf20Sopenharmony_ci *      Output (Variable),
1948c2ecf20Sopenharmony_ci *    End Collection,
1958c2ecf20Sopenharmony_ci *  End Collection
1968c2ecf20Sopenharmony_ci */
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/* Size of the original descriptor of the PID 0x0011 joystick */
1998c2ecf20Sopenharmony_ci#define PID0011_RDESC_ORIG_SIZE	101
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/* Fixed report descriptor for PID 0x011 joystick */
2028c2ecf20Sopenharmony_cistatic __u8 pid0011_rdesc_fixed[] = {
2038c2ecf20Sopenharmony_ci	0x05, 0x01,         /*  Usage Page (Desktop),           */
2048c2ecf20Sopenharmony_ci	0x09, 0x04,         /*  Usage (Joystick),               */
2058c2ecf20Sopenharmony_ci	0xA1, 0x01,         /*  Collection (Application),       */
2068c2ecf20Sopenharmony_ci	0xA1, 0x02,         /*      Collection (Logical),       */
2078c2ecf20Sopenharmony_ci	0x14,               /*          Logical Minimum (0),    */
2088c2ecf20Sopenharmony_ci	0x75, 0x08,         /*          Report Size (8),        */
2098c2ecf20Sopenharmony_ci	0x95, 0x03,         /*          Report Count (3),       */
2108c2ecf20Sopenharmony_ci	0x81, 0x01,         /*          Input (Constant),       */
2118c2ecf20Sopenharmony_ci	0x26, 0xFF, 0x00,   /*          Logical Maximum (255),  */
2128c2ecf20Sopenharmony_ci	0x95, 0x02,         /*          Report Count (2),       */
2138c2ecf20Sopenharmony_ci	0x09, 0x30,         /*          Usage (X),              */
2148c2ecf20Sopenharmony_ci	0x09, 0x31,         /*          Usage (Y),              */
2158c2ecf20Sopenharmony_ci	0x81, 0x02,         /*          Input (Variable),       */
2168c2ecf20Sopenharmony_ci	0x75, 0x01,         /*          Report Size (1),        */
2178c2ecf20Sopenharmony_ci	0x95, 0x04,         /*          Report Count (4),       */
2188c2ecf20Sopenharmony_ci	0x81, 0x01,         /*          Input (Constant),       */
2198c2ecf20Sopenharmony_ci	0x25, 0x01,         /*          Logical Maximum (1),    */
2208c2ecf20Sopenharmony_ci	0x95, 0x0A,         /*          Report Count (10),      */
2218c2ecf20Sopenharmony_ci	0x05, 0x09,         /*          Usage Page (Button),    */
2228c2ecf20Sopenharmony_ci	0x19, 0x01,         /*          Usage Minimum (01h),    */
2238c2ecf20Sopenharmony_ci	0x29, 0x0A,         /*          Usage Maximum (0Ah),    */
2248c2ecf20Sopenharmony_ci	0x81, 0x02,         /*          Input (Variable),       */
2258c2ecf20Sopenharmony_ci	0x95, 0x0A,         /*          Report Count (10),      */
2268c2ecf20Sopenharmony_ci	0x81, 0x01,         /*          Input (Constant),       */
2278c2ecf20Sopenharmony_ci	0xC0,               /*      End Collection,             */
2288c2ecf20Sopenharmony_ci	0xC0                /*  End Collection                  */
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic __u8 *dr_report_fixup(struct hid_device *hdev, __u8 *rdesc,
2328c2ecf20Sopenharmony_ci				unsigned int *rsize)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	switch (hdev->product) {
2358c2ecf20Sopenharmony_ci	case 0x0011:
2368c2ecf20Sopenharmony_ci		if (*rsize == PID0011_RDESC_ORIG_SIZE) {
2378c2ecf20Sopenharmony_ci			rdesc = pid0011_rdesc_fixed;
2388c2ecf20Sopenharmony_ci			*rsize = sizeof(pid0011_rdesc_fixed);
2398c2ecf20Sopenharmony_ci		}
2408c2ecf20Sopenharmony_ci		break;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci	return rdesc;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci#define map_abs(c)      hid_map_usage(hi, usage, bit, max, EV_ABS, (c))
2468c2ecf20Sopenharmony_ci#define map_rel(c)      hid_map_usage(hi, usage, bit, max, EV_REL, (c))
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic int dr_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2498c2ecf20Sopenharmony_ci			    struct hid_field *field, struct hid_usage *usage,
2508c2ecf20Sopenharmony_ci			    unsigned long **bit, int *max)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	switch (usage->hid) {
2538c2ecf20Sopenharmony_ci	/*
2548c2ecf20Sopenharmony_ci	 * revert to the old hid-input behavior where axes
2558c2ecf20Sopenharmony_ci	 * can be randomly assigned when hid->usage is reused.
2568c2ecf20Sopenharmony_ci	 */
2578c2ecf20Sopenharmony_ci	case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
2588c2ecf20Sopenharmony_ci	case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
2598c2ecf20Sopenharmony_ci		if (field->flags & HID_MAIN_ITEM_RELATIVE)
2608c2ecf20Sopenharmony_ci			map_rel(usage->hid & 0xf);
2618c2ecf20Sopenharmony_ci		else
2628c2ecf20Sopenharmony_ci			map_abs(usage->hid & 0xf);
2638c2ecf20Sopenharmony_ci		return 1;
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	return 0;
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	int ret;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	ret = hid_parse(hdev);
2768c2ecf20Sopenharmony_ci	if (ret) {
2778c2ecf20Sopenharmony_ci		hid_err(hdev, "parse failed\n");
2788c2ecf20Sopenharmony_ci		goto err;
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
2828c2ecf20Sopenharmony_ci	if (ret) {
2838c2ecf20Sopenharmony_ci		hid_err(hdev, "hw start failed\n");
2848c2ecf20Sopenharmony_ci		goto err;
2858c2ecf20Sopenharmony_ci	}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	switch (hdev->product) {
2888c2ecf20Sopenharmony_ci	case 0x0006:
2898c2ecf20Sopenharmony_ci		ret = drff_init(hdev);
2908c2ecf20Sopenharmony_ci		if (ret) {
2918c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "force feedback init failed\n");
2928c2ecf20Sopenharmony_ci			hid_hw_stop(hdev);
2938c2ecf20Sopenharmony_ci			goto err;
2948c2ecf20Sopenharmony_ci		}
2958c2ecf20Sopenharmony_ci		break;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	return 0;
2998c2ecf20Sopenharmony_cierr:
3008c2ecf20Sopenharmony_ci	return ret;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic const struct hid_device_id dr_devices[] = {
3048c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006),  },
3058c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011),  },
3068c2ecf20Sopenharmony_ci	{ }
3078c2ecf20Sopenharmony_ci};
3088c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, dr_devices);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic struct hid_driver dr_driver = {
3118c2ecf20Sopenharmony_ci	.name = "dragonrise",
3128c2ecf20Sopenharmony_ci	.id_table = dr_devices,
3138c2ecf20Sopenharmony_ci	.report_fixup = dr_report_fixup,
3148c2ecf20Sopenharmony_ci	.probe = dr_probe,
3158c2ecf20Sopenharmony_ci	.input_mapping = dr_input_mapping,
3168c2ecf20Sopenharmony_ci};
3178c2ecf20Sopenharmony_cimodule_hid_driver(dr_driver);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
320