1 /*
2  *  Force feedback support for Betop based devices
3  *
4  *  The devices are distributed under various names and the same USB device ID
5  *  can be used in both adapters and actual game controllers.
6  *
7  *  0x11c2:0x2208 "BTP2185 BFM mode Joystick"
8  *   - tested with BTP2185 BFM Mode.
9  *
10  *  0x11C0:0x5506 "BTP2185 PC mode Joystick"
11  *   - tested with BTP2185 PC Mode.
12  *
13  *  0x8380:0x1850 "BTP2185 V2 PC mode USB Gamepad"
14  *   - tested with BTP2185 PC Mode with another version.
15  *
16  *  0x20bc:0x5500 "BTP2185 V2 BFM mode Joystick"
17  *   - tested with BTP2171s.
18  *  Copyright (c) 2014 Huang Bo <huangbobupt@163.com>
19  */
20 
21 /*
22  * This program is free software; you can redistribute it and/or modify it
23  * under the terms of the GNU General Public License as published by the Free
24  * Software Foundation; either version 2 of the License, or (at your option)
25  * any later version.
26  */
27 
28 
29 #include <linux/input.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/hid.h>
33 
34 #include "hid-ids.h"
35 
36 struct betopff_device {
37 	struct hid_report *report;
38 };
39 
hid_betopff_play(struct input_dev * dev,void * data,struct ff_effect * effect)40 static int hid_betopff_play(struct input_dev *dev, void *data,
41 			 struct ff_effect *effect)
42 {
43 	struct hid_device *hid = input_get_drvdata(dev);
44 	struct betopff_device *betopff = data;
45 	__u16 left, right;
46 
47 	left = effect->u.rumble.strong_magnitude;
48 	right = effect->u.rumble.weak_magnitude;
49 
50 	betopff->report->field[2]->value[0] = left / 256;
51 	betopff->report->field[3]->value[0] = right / 256;
52 
53 	hid_hw_request(hid, betopff->report, HID_REQ_SET_REPORT);
54 
55 	return 0;
56 }
57 
betopff_init(struct hid_device * hid)58 static int betopff_init(struct hid_device *hid)
59 {
60 	struct betopff_device *betopff;
61 	struct hid_report *report;
62 	struct hid_input *hidinput;
63 	struct list_head *report_list =
64 			&hid->report_enum[HID_OUTPUT_REPORT].report_list;
65 	struct input_dev *dev;
66 	int error;
67 	int i, j;
68 
69 	if (list_empty(&hid->inputs)) {
70 		hid_err(hid, "no inputs found\n");
71 		return -ENODEV;
72 	}
73 
74 	hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
75 	dev = hidinput->input;
76 
77 	if (list_empty(report_list)) {
78 		hid_err(hid, "no output reports found\n");
79 		return -ENODEV;
80 	}
81 
82 	report = list_first_entry(report_list, struct hid_report, list);
83 	/*
84 	 * Actually there are 4 fields for 4 Bytes as below:
85 	 * -----------------------------------------
86 	 * Byte0  Byte1  Byte2	  Byte3
87 	 * 0x00   0x00   left_motor right_motor
88 	 * -----------------------------------------
89 	 * Do init them with default value.
90 	 */
91 	if (report->maxfield < 4) {
92 		hid_err(hid, "not enough fields in the report: %d\n",
93 				report->maxfield);
94 		return -ENODEV;
95 	}
96 	for (i = 0; i < report->maxfield; i++) {
97 		if (report->field[i]->report_count < 1) {
98 			hid_err(hid, "no values in the field\n");
99 			return -ENODEV;
100 		}
101 		for (j = 0; j < report->field[i]->report_count; j++) {
102 			report->field[i]->value[j] = 0x00;
103 		}
104 	}
105 
106 	betopff = kzalloc(sizeof(*betopff), GFP_KERNEL);
107 	if (!betopff)
108 		return -ENOMEM;
109 
110 	set_bit(FF_RUMBLE, dev->ffbit);
111 
112 	error = input_ff_create_memless(dev, betopff, hid_betopff_play);
113 	if (error) {
114 		kfree(betopff);
115 		return error;
116 	}
117 
118 	betopff->report = report;
119 	hid_hw_request(hid, betopff->report, HID_REQ_SET_REPORT);
120 
121 	hid_info(hid, "Force feedback for betop devices by huangbo <huangbobupt@163.com>\n");
122 
123 	return 0;
124 }
125 
betop_probe(struct hid_device * hdev,const struct hid_device_id * id)126 static int betop_probe(struct hid_device *hdev, const struct hid_device_id *id)
127 {
128 	int ret;
129 
130 	if (id->driver_data)
131 		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
132 
133 	ret = hid_parse(hdev);
134 	if (ret) {
135 		hid_err(hdev, "parse failed\n");
136 		goto err;
137 	}
138 
139 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
140 	if (ret) {
141 		hid_err(hdev, "hw start failed\n");
142 		goto err;
143 	}
144 
145 	betopff_init(hdev);
146 
147 	return 0;
148 err:
149 	return ret;
150 }
151 
152 static const struct hid_device_id betop_devices[] = {
153 	{ HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185BFM, 0x2208) },
154 	{ HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185PC, 0x5506) },
155 	{ HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2PC, 0x1850) },
156 	{ HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2BFM, 0x5500) },
157 	{ }
158 };
159 MODULE_DEVICE_TABLE(hid, betop_devices);
160 
161 static struct hid_driver betop_driver = {
162 	.name = "betop",
163 	.id_table = betop_devices,
164 	.probe = betop_probe,
165 };
166 module_hid_driver(betop_driver);
167 
168 MODULE_LICENSE("GPL");
169