Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Force feedback support for GreenAsia (Product ID 0x12) based devices |
| 4 | * |
| 5 | * The devices are distributed under various names and the same USB device ID |
| 6 | * can be used in many game controllers. |
| 7 | * |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 8 | * 0e8f:0012 "GreenAsia Inc. USB Joystick " |
| 9 | * - tested with MANTA Warior MM816 and SpeedLink Strike2 SL-6635. |
| 10 | * |
| 11 | * Copyright (c) 2008 Lukasz Lubojanski <lukasz@lubojanski.info> |
| 12 | */ |
| 13 | |
| 14 | /* |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/input.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 19 | #include <linux/hid.h> |
Paul Gortmaker | 8f86a2c | 2011-07-03 13:39:48 -0400 | [diff] [blame] | 20 | #include <linux/module.h> |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 21 | #include "hid-ids.h" |
Jiri Kosina | 0f6f431 | 2009-05-15 15:46:44 +0200 | [diff] [blame] | 22 | |
| 23 | #ifdef CONFIG_GREENASIA_FF |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 24 | |
| 25 | struct gaff_device { |
| 26 | struct hid_report *report; |
| 27 | }; |
| 28 | |
| 29 | static int hid_gaff_play(struct input_dev *dev, void *data, |
| 30 | struct ff_effect *effect) |
| 31 | { |
| 32 | struct hid_device *hid = input_get_drvdata(dev); |
| 33 | struct gaff_device *gaff = data; |
| 34 | int left, right; |
| 35 | |
| 36 | left = effect->u.rumble.strong_magnitude; |
| 37 | right = effect->u.rumble.weak_magnitude; |
| 38 | |
| 39 | dbg_hid("called with 0x%04x 0x%04x", left, right); |
| 40 | |
| 41 | left = left * 0xfe / 0xffff; |
| 42 | right = right * 0xfe / 0xffff; |
| 43 | |
| 44 | gaff->report->field[0]->value[0] = 0x51; |
| 45 | gaff->report->field[0]->value[1] = 0x0; |
| 46 | gaff->report->field[0]->value[2] = right; |
| 47 | gaff->report->field[0]->value[3] = 0; |
| 48 | gaff->report->field[0]->value[4] = left; |
| 49 | gaff->report->field[0]->value[5] = 0; |
| 50 | dbg_hid("running with 0x%02x 0x%02x", left, right); |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 51 | hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 52 | |
| 53 | gaff->report->field[0]->value[0] = 0xfa; |
| 54 | gaff->report->field[0]->value[1] = 0xfe; |
| 55 | gaff->report->field[0]->value[2] = 0x0; |
| 56 | gaff->report->field[0]->value[4] = 0x0; |
| 57 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 58 | hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int gaff_init(struct hid_device *hid) |
| 64 | { |
| 65 | struct gaff_device *gaff; |
| 66 | struct hid_report *report; |
| 67 | struct hid_input *hidinput = list_entry(hid->inputs.next, |
| 68 | struct hid_input, list); |
| 69 | struct list_head *report_list = |
| 70 | &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 71 | struct list_head *report_ptr = report_list; |
| 72 | struct input_dev *dev = hidinput->input; |
| 73 | int error; |
| 74 | |
| 75 | if (list_empty(report_list)) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 76 | hid_err(hid, "no output reports found\n"); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 77 | return -ENODEV; |
| 78 | } |
| 79 | |
| 80 | report_ptr = report_ptr->next; |
| 81 | |
| 82 | report = list_entry(report_ptr, struct hid_report, list); |
| 83 | if (report->maxfield < 1) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 84 | hid_err(hid, "no fields in the report\n"); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 85 | return -ENODEV; |
| 86 | } |
| 87 | |
| 88 | if (report->field[0]->report_count < 6) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 89 | hid_err(hid, "not enough values in the field\n"); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 90 | return -ENODEV; |
| 91 | } |
| 92 | |
| 93 | gaff = kzalloc(sizeof(struct gaff_device), GFP_KERNEL); |
| 94 | if (!gaff) |
| 95 | return -ENOMEM; |
| 96 | |
| 97 | set_bit(FF_RUMBLE, dev->ffbit); |
| 98 | |
| 99 | error = input_ff_create_memless(dev, gaff, hid_gaff_play); |
| 100 | if (error) { |
| 101 | kfree(gaff); |
| 102 | return error; |
| 103 | } |
| 104 | |
| 105 | gaff->report = report; |
| 106 | gaff->report->field[0]->value[0] = 0x51; |
| 107 | gaff->report->field[0]->value[1] = 0x00; |
| 108 | gaff->report->field[0]->value[2] = 0x00; |
| 109 | gaff->report->field[0]->value[3] = 0x00; |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 110 | hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 111 | |
| 112 | gaff->report->field[0]->value[0] = 0xfa; |
| 113 | gaff->report->field[0]->value[1] = 0xfe; |
| 114 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 115 | hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 116 | |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 117 | hid_info(hid, "Force Feedback for GreenAsia 0x12 devices by Lukasz Lubojanski <lukasz@lubojanski.info>\n"); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 118 | |
| 119 | return 0; |
| 120 | } |
Jiri Kosina | 0f6f431 | 2009-05-15 15:46:44 +0200 | [diff] [blame] | 121 | #else |
| 122 | static inline int gaff_init(struct hid_device *hdev) |
| 123 | { |
| 124 | return 0; |
| 125 | } |
| 126 | #endif |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 127 | |
| 128 | static int ga_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 129 | { |
| 130 | int ret; |
| 131 | |
| 132 | dev_dbg(&hdev->dev, "Greenasia HID hardware probe..."); |
| 133 | |
| 134 | ret = hid_parse(hdev); |
| 135 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 136 | hid_err(hdev, "parse failed\n"); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 137 | goto err; |
| 138 | } |
| 139 | |
| 140 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); |
| 141 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 142 | hid_err(hdev, "hw start failed\n"); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 143 | goto err; |
| 144 | } |
| 145 | |
| 146 | gaff_init(hdev); |
| 147 | |
| 148 | return 0; |
| 149 | err: |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static const struct hid_device_id ga_devices[] = { |
| 154 | { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012), }, |
| 155 | { } |
| 156 | }; |
| 157 | MODULE_DEVICE_TABLE(hid, ga_devices); |
| 158 | |
| 159 | static struct hid_driver ga_driver = { |
| 160 | .name = "greenasia", |
| 161 | .id_table = ga_devices, |
| 162 | .probe = ga_probe, |
| 163 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 164 | module_hid_driver(ga_driver); |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 165 | |
Lukasz Lubojanski | 42859e0 | 2008-12-11 22:07:59 +0100 | [diff] [blame] | 166 | MODULE_LICENSE("GPL"); |