Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Force feedback support for ACRUX game controllers |
| 4 | * |
| 5 | * From what I have gathered, these devices are mass produced in China |
| 6 | * by several vendors. They often share the same design as the original |
| 7 | * Xbox 360 controller. |
| 8 | * |
| 9 | * 1a34:0802 "ACRUX USB GAMEPAD 8116" |
Sergei Kolzun | b55ebc2 | 2011-08-04 00:25:57 -0700 | [diff] [blame] | 10 | * - tested with an EXEQ EQ-PCU-02090 game controller. |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 11 | * |
| 12 | * Copyright (c) 2010 Sergei Kolzun <x0r@dv-life.ru> |
| 13 | */ |
| 14 | |
| 15 | /* |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/input.h> |
| 19 | #include <linux/slab.h> |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 20 | #include <linux/hid.h> |
Paul Gortmaker | 8f86a2c | 2011-07-03 13:39:48 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 22 | |
| 23 | #include "hid-ids.h" |
Dmitry Torokhov | 0ae4381 | 2011-03-11 00:27:34 -0800 | [diff] [blame] | 24 | |
| 25 | #ifdef CONFIG_HID_ACRUX_FF |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 26 | |
| 27 | struct axff_device { |
| 28 | struct hid_report *report; |
| 29 | }; |
| 30 | |
| 31 | static int axff_play(struct input_dev *dev, void *data, struct ff_effect *effect) |
| 32 | { |
| 33 | struct hid_device *hid = input_get_drvdata(dev); |
| 34 | struct axff_device *axff = data; |
Sergei Kolzun | b55ebc2 | 2011-08-04 00:25:57 -0700 | [diff] [blame] | 35 | struct hid_report *report = axff->report; |
| 36 | int field_count = 0; |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 37 | int left, right; |
Sergei Kolzun | b55ebc2 | 2011-08-04 00:25:57 -0700 | [diff] [blame] | 38 | int i, j; |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 39 | |
| 40 | left = effect->u.rumble.strong_magnitude; |
| 41 | right = effect->u.rumble.weak_magnitude; |
| 42 | |
| 43 | dbg_hid("called with 0x%04x 0x%04x", left, right); |
| 44 | |
| 45 | left = left * 0xff / 0xffff; |
| 46 | right = right * 0xff / 0xffff; |
| 47 | |
Sergei Kolzun | b55ebc2 | 2011-08-04 00:25:57 -0700 | [diff] [blame] | 48 | for (i = 0; i < report->maxfield; i++) { |
| 49 | for (j = 0; j < report->field[i]->report_count; j++) { |
| 50 | report->field[i]->value[j] = |
| 51 | field_count % 2 ? right : left; |
| 52 | field_count++; |
| 53 | } |
| 54 | } |
| 55 | |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 56 | dbg_hid("running with 0x%02x 0x%02x", left, right); |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 57 | hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT); |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int axff_init(struct hid_device *hid) |
| 63 | { |
| 64 | struct axff_device *axff; |
| 65 | struct hid_report *report; |
Alan Stern | d9d4b1e | 2019-10-03 14:53:59 -0400 | [diff] [blame] | 66 | struct hid_input *hidinput; |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 67 | struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list; |
Alan Stern | d9d4b1e | 2019-10-03 14:53:59 -0400 | [diff] [blame] | 68 | struct input_dev *dev; |
Sergei Kolzun | b55ebc2 | 2011-08-04 00:25:57 -0700 | [diff] [blame] | 69 | int field_count = 0; |
| 70 | int i, j; |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 71 | int error; |
| 72 | |
Alan Stern | d9d4b1e | 2019-10-03 14:53:59 -0400 | [diff] [blame] | 73 | if (list_empty(&hid->inputs)) { |
| 74 | hid_err(hid, "no inputs found\n"); |
| 75 | return -ENODEV; |
| 76 | } |
| 77 | hidinput = list_first_entry(&hid->inputs, struct hid_input, list); |
| 78 | dev = hidinput->input; |
| 79 | |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 80 | if (list_empty(report_list)) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 81 | hid_err(hid, "no output reports found\n"); |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 82 | return -ENODEV; |
| 83 | } |
| 84 | |
| 85 | report = list_first_entry(report_list, struct hid_report, list); |
Sergei Kolzun | b55ebc2 | 2011-08-04 00:25:57 -0700 | [diff] [blame] | 86 | for (i = 0; i < report->maxfield; i++) { |
| 87 | for (j = 0; j < report->field[i]->report_count; j++) { |
| 88 | report->field[i]->value[j] = 0x00; |
| 89 | field_count++; |
| 90 | } |
| 91 | } |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 92 | |
Tristan Rice | e17f5d76 | 2013-11-12 19:06:23 +0100 | [diff] [blame] | 93 | if (field_count < 4 && hid->product != 0xf705) { |
Sergei Kolzun | b55ebc2 | 2011-08-04 00:25:57 -0700 | [diff] [blame] | 94 | hid_err(hid, "not enough fields in the report: %d\n", |
| 95 | field_count); |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 96 | return -ENODEV; |
| 97 | } |
| 98 | |
| 99 | axff = kzalloc(sizeof(struct axff_device), GFP_KERNEL); |
| 100 | if (!axff) |
| 101 | return -ENOMEM; |
| 102 | |
| 103 | set_bit(FF_RUMBLE, dev->ffbit); |
| 104 | |
| 105 | error = input_ff_create_memless(dev, axff, axff_play); |
| 106 | if (error) |
| 107 | goto err_free_mem; |
| 108 | |
| 109 | axff->report = report; |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 110 | hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT); |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 111 | |
Sergei Kolzun | b55ebc2 | 2011-08-04 00:25:57 -0700 | [diff] [blame] | 112 | hid_info(hid, "Force Feedback for ACRUX game controllers by Sergei Kolzun <x0r@dv-life.ru>\n"); |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | |
| 116 | err_free_mem: |
| 117 | kfree(axff); |
| 118 | return error; |
| 119 | } |
Dmitry Torokhov | 0ae4381 | 2011-03-11 00:27:34 -0800 | [diff] [blame] | 120 | #else |
| 121 | static inline int axff_init(struct hid_device *hid) |
| 122 | { |
| 123 | return 0; |
| 124 | } |
| 125 | #endif |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 126 | |
| 127 | static int ax_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 128 | { |
| 129 | int error; |
| 130 | |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 131 | dev_dbg(&hdev->dev, "ACRUX HID hardware probe...\n"); |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 132 | |
| 133 | error = hid_parse(hdev); |
| 134 | if (error) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 135 | hid_err(hdev, "parse failed\n"); |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 136 | return error; |
| 137 | } |
| 138 | |
| 139 | error = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); |
| 140 | if (error) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 141 | hid_err(hdev, "hw start failed\n"); |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 142 | return error; |
| 143 | } |
| 144 | |
| 145 | error = axff_init(hdev); |
| 146 | if (error) { |
| 147 | /* |
| 148 | * Do not fail device initialization completely as device |
| 149 | * may still be partially operable, just warn. |
| 150 | */ |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 151 | hid_warn(hdev, |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 152 | "Failed to enable force feedback support, error: %d\n", |
| 153 | error); |
| 154 | } |
| 155 | |
Dmitry Torokhov | 0ae4381 | 2011-03-11 00:27:34 -0800 | [diff] [blame] | 156 | /* |
| 157 | * We need to start polling device right away, otherwise |
| 158 | * it will go into a coma. |
| 159 | */ |
| 160 | error = hid_hw_open(hdev); |
| 161 | if (error) { |
| 162 | dev_err(&hdev->dev, "hw open failed\n"); |
Axel Lin | b30d89d | 2011-07-14 13:07:51 +0800 | [diff] [blame] | 163 | hid_hw_stop(hdev); |
Dmitry Torokhov | 0ae4381 | 2011-03-11 00:27:34 -0800 | [diff] [blame] | 164 | return error; |
| 165 | } |
| 166 | |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Dmitry Torokhov | 0ae4381 | 2011-03-11 00:27:34 -0800 | [diff] [blame] | 170 | static void ax_remove(struct hid_device *hdev) |
| 171 | { |
| 172 | hid_hw_close(hdev); |
| 173 | hid_hw_stop(hdev); |
| 174 | } |
| 175 | |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 176 | static const struct hid_device_id ax_devices[] = { |
| 177 | { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802), }, |
Tristan Rice | e17f5d76 | 2013-11-12 19:06:23 +0100 | [diff] [blame] | 178 | { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0xf705), }, |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 179 | { } |
| 180 | }; |
| 181 | MODULE_DEVICE_TABLE(hid, ax_devices); |
| 182 | |
| 183 | static struct hid_driver ax_driver = { |
Dmitry Torokhov | 0ae4381 | 2011-03-11 00:27:34 -0800 | [diff] [blame] | 184 | .name = "acrux", |
| 185 | .id_table = ax_devices, |
| 186 | .probe = ax_probe, |
| 187 | .remove = ax_remove, |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 188 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 189 | module_hid_driver(ax_driver); |
Sergei Kolzun | c0dbcc3 | 2010-07-19 12:13:23 +0200 | [diff] [blame] | 190 | |
| 191 | MODULE_AUTHOR("Sergei Kolzun"); |
| 192 | MODULE_DESCRIPTION("Force feedback support for ACRUX game controllers"); |
| 193 | MODULE_LICENSE("GPL"); |