GNU Linux-libre 4.14.257-gnu1
[releases.git] / drivers / hid / hid-betopff.c
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
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
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 field_count = 0;
67         int error;
68         int i, j;
69
70         if (list_empty(&hid->inputs)) {
71                 hid_err(hid, "no inputs found\n");
72                 return -ENODEV;
73         }
74
75         hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
76         dev = hidinput->input;
77
78         if (list_empty(report_list)) {
79                 hid_err(hid, "no output reports found\n");
80                 return -ENODEV;
81         }
82
83         report = list_first_entry(report_list, struct hid_report, list);
84         /*
85          * Actually there are 4 fields for 4 Bytes as below:
86          * -----------------------------------------
87          * Byte0  Byte1  Byte2    Byte3
88          * 0x00   0x00   left_motor right_motor
89          * -----------------------------------------
90          * Do init them with default value.
91          */
92         for (i = 0; i < report->maxfield; i++) {
93                 for (j = 0; j < report->field[i]->report_count; j++) {
94                         report->field[i]->value[j] = 0x00;
95                         field_count++;
96                 }
97         }
98
99         if (field_count < 4) {
100                 hid_err(hid, "not enough fields in the report: %d\n",
101                                 field_count);
102                 return -ENODEV;
103         }
104
105         betopff = kzalloc(sizeof(*betopff), GFP_KERNEL);
106         if (!betopff)
107                 return -ENOMEM;
108
109         set_bit(FF_RUMBLE, dev->ffbit);
110
111         error = input_ff_create_memless(dev, betopff, hid_betopff_play);
112         if (error) {
113                 kfree(betopff);
114                 return error;
115         }
116
117         betopff->report = report;
118         hid_hw_request(hid, betopff->report, HID_REQ_SET_REPORT);
119
120         hid_info(hid, "Force feedback for betop devices by huangbo <huangbobupt@163.com>\n");
121
122         return 0;
123 }
124
125 static int betop_probe(struct hid_device *hdev, const struct hid_device_id *id)
126 {
127         int ret;
128
129         if (id->driver_data)
130                 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
131
132         ret = hid_parse(hdev);
133         if (ret) {
134                 hid_err(hdev, "parse failed\n");
135                 goto err;
136         }
137
138         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
139         if (ret) {
140                 hid_err(hdev, "hw start failed\n");
141                 goto err;
142         }
143
144         betopff_init(hdev);
145
146         return 0;
147 err:
148         return ret;
149 }
150
151 static const struct hid_device_id betop_devices[] = {
152         { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185BFM, 0x2208) },
153         { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185PC, 0x5506) },
154         { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2PC, 0x1850) },
155         { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2BFM, 0x5500) },
156         { }
157 };
158 MODULE_DEVICE_TABLE(hid, betop_devices);
159
160 static struct hid_driver betop_driver = {
161         .name = "betop",
162         .id_table = betop_devices,
163         .probe = betop_probe,
164 };
165 module_hid_driver(betop_driver);
166
167 MODULE_LICENSE("GPL");