| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 1 | /* | 
| Anssi Hannula | 5edc41e | 2007-09-19 16:13:20 +0200 | [diff] [blame] | 2 | *  Force feedback support for PantherLord/GreenAsia 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 | *  0810:0001 "Twin USB Joystick" | 
|  | 8 | *   - tested with PantherLord USB/PS2 2in1 Adapter | 
|  | 9 | *   - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT) | 
|  | 10 | * | 
|  | 11 | *  0e8f:0003 "GreenAsia Inc.    USB Joystick     " | 
| Anssi Hannula | 27a9c17 | 2009-02-27 23:47:00 +0200 | [diff] [blame] | 12 | *   - tested with König Gaming gamepad | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 13 | * | 
| Anssi Hannula | 27a9c17 | 2009-02-27 23:47:00 +0200 | [diff] [blame] | 14 | *  0e8f:0003 "GASIA USB Gamepad" | 
|  | 15 | *   - another version of the König gamepad | 
|  | 16 | * | 
|  | 17 | *  Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com> | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 18 | */ | 
|  | 19 |  | 
|  | 20 | /* | 
|  | 21 | * This program is free software; you can redistribute it and/or modify | 
|  | 22 | * it under the terms of the GNU General Public License as published by | 
|  | 23 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 24 | * (at your option) any later version. | 
|  | 25 | * | 
|  | 26 | * This program is distributed in the hope that it will be useful, | 
|  | 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 29 | * GNU General Public License for more details. | 
|  | 30 | * | 
|  | 31 | * You should have received a copy of the GNU General Public License | 
|  | 32 | * along with this program; if not, write to the Free Software | 
|  | 33 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 
|  | 34 | */ | 
|  | 35 |  | 
|  | 36 |  | 
|  | 37 | /* #define DEBUG */ | 
|  | 38 |  | 
|  | 39 | #define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg) | 
|  | 40 |  | 
|  | 41 | #include <linux/input.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 43 | #include <linux/usb.h> | 
|  | 44 | #include <linux/hid.h> | 
| Jiri Slaby | 5f02229 | 2008-09-18 19:43:32 +0200 | [diff] [blame] | 45 |  | 
|  | 46 | #include "hid-ids.h" | 
|  | 47 |  | 
|  | 48 | #ifdef CONFIG_PANTHERLORD_FF | 
|  | 49 | #include "usbhid/usbhid.h" | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 50 |  | 
|  | 51 | struct plff_device { | 
|  | 52 | struct hid_report *report; | 
| Anssi Hannula | 27a9c17 | 2009-02-27 23:47:00 +0200 | [diff] [blame] | 53 | s32 *strong; | 
|  | 54 | s32 *weak; | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 55 | }; | 
|  | 56 |  | 
|  | 57 | static int hid_plff_play(struct input_dev *dev, void *data, | 
|  | 58 | struct ff_effect *effect) | 
|  | 59 | { | 
| Dmitry Torokhov | e071298 | 2007-05-09 10:17:31 +0200 | [diff] [blame] | 60 | struct hid_device *hid = input_get_drvdata(dev); | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 61 | struct plff_device *plff = data; | 
|  | 62 | int left, right; | 
|  | 63 |  | 
|  | 64 | left = effect->u.rumble.strong_magnitude; | 
|  | 65 | right = effect->u.rumble.weak_magnitude; | 
|  | 66 | debug("called with 0x%04x 0x%04x", left, right); | 
|  | 67 |  | 
|  | 68 | left = left * 0x7f / 0xffff; | 
|  | 69 | right = right * 0x7f / 0xffff; | 
|  | 70 |  | 
| Anssi Hannula | 27a9c17 | 2009-02-27 23:47:00 +0200 | [diff] [blame] | 71 | *plff->strong = left; | 
|  | 72 | *plff->weak = right; | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 73 | debug("running with 0x%02x 0x%02x", left, right); | 
|  | 74 | usbhid_submit_report(hid, plff->report, USB_DIR_OUT); | 
|  | 75 |  | 
|  | 76 | return 0; | 
|  | 77 | } | 
|  | 78 |  | 
| Jiri Slaby | 5f02229 | 2008-09-18 19:43:32 +0200 | [diff] [blame] | 79 | static int plff_init(struct hid_device *hid) | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 80 | { | 
|  | 81 | struct plff_device *plff; | 
|  | 82 | struct hid_report *report; | 
|  | 83 | struct hid_input *hidinput; | 
|  | 84 | struct list_head *report_list = | 
|  | 85 | &hid->report_enum[HID_OUTPUT_REPORT].report_list; | 
|  | 86 | struct list_head *report_ptr = report_list; | 
|  | 87 | struct input_dev *dev; | 
|  | 88 | int error; | 
| Anssi Hannula | 27a9c17 | 2009-02-27 23:47:00 +0200 | [diff] [blame] | 89 | s32 *strong; | 
|  | 90 | s32 *weak; | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 91 |  | 
| Anssi Hannula | 5edc41e | 2007-09-19 16:13:20 +0200 | [diff] [blame] | 92 | /* The device contains one output report per physical device, all | 
|  | 93 | containing 1 field, which contains 4 ff00.0002 usages and 4 16bit | 
|  | 94 | absolute values. | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 95 |  | 
| Anssi Hannula | 5edc41e | 2007-09-19 16:13:20 +0200 | [diff] [blame] | 96 | The input reports also contain a field which contains | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 97 | 8 ff00.0001 usages and 8 boolean values. Their meaning is | 
| Anssi Hannula | 27a9c17 | 2009-02-27 23:47:00 +0200 | [diff] [blame] | 98 | currently unknown. | 
|  | 99 |  | 
|  | 100 | A version of the 0e8f:0003 exists that has all the values in | 
|  | 101 | separate fields and misses the extra input field, thus resembling | 
|  | 102 | Zeroplus (hid-zpff) devices. | 
|  | 103 | */ | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 104 |  | 
|  | 105 | if (list_empty(report_list)) { | 
| Jiri Slaby | 7957501 | 2008-09-18 12:23:34 +0200 | [diff] [blame] | 106 | dev_err(&hid->dev, "no output reports found\n"); | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 107 | return -ENODEV; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | list_for_each_entry(hidinput, &hid->inputs, list) { | 
|  | 111 |  | 
|  | 112 | report_ptr = report_ptr->next; | 
|  | 113 |  | 
|  | 114 | if (report_ptr == report_list) { | 
| Jiri Slaby | 7957501 | 2008-09-18 12:23:34 +0200 | [diff] [blame] | 115 | dev_err(&hid->dev, "required output report is " | 
|  | 116 | "missing\n"); | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 117 | return -ENODEV; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | report = list_entry(report_ptr, struct hid_report, list); | 
|  | 121 | if (report->maxfield < 1) { | 
| Jiri Slaby | 7957501 | 2008-09-18 12:23:34 +0200 | [diff] [blame] | 122 | dev_err(&hid->dev, "no fields in the report\n"); | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 123 | return -ENODEV; | 
|  | 124 | } | 
|  | 125 |  | 
| Anssi Hannula | 27a9c17 | 2009-02-27 23:47:00 +0200 | [diff] [blame] | 126 | if (report->field[0]->report_count >= 4) { | 
|  | 127 | report->field[0]->value[0] = 0x00; | 
|  | 128 | report->field[0]->value[1] = 0x00; | 
|  | 129 | strong = &report->field[0]->value[2]; | 
|  | 130 | weak = &report->field[0]->value[3]; | 
|  | 131 | debug("detected single-field device"); | 
|  | 132 | } else if (report->maxfield >= 4 && report->field[0]->maxusage == 1 && | 
|  | 133 | report->field[0]->usage[0].hid == (HID_UP_LED | 0x43)) { | 
|  | 134 | report->field[0]->value[0] = 0x00; | 
|  | 135 | report->field[1]->value[0] = 0x00; | 
|  | 136 | strong = &report->field[2]->value[0]; | 
|  | 137 | weak = &report->field[3]->value[0]; | 
|  | 138 | debug("detected 4-field device"); | 
|  | 139 | } else { | 
|  | 140 | dev_err(&hid->dev, "not enough fields or values\n"); | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 141 | return -ENODEV; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL); | 
|  | 145 | if (!plff) | 
|  | 146 | return -ENOMEM; | 
|  | 147 |  | 
|  | 148 | dev = hidinput->input; | 
|  | 149 |  | 
|  | 150 | set_bit(FF_RUMBLE, dev->ffbit); | 
|  | 151 |  | 
|  | 152 | error = input_ff_create_memless(dev, plff, hid_plff_play); | 
|  | 153 | if (error) { | 
|  | 154 | kfree(plff); | 
|  | 155 | return error; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | plff->report = report; | 
| Anssi Hannula | 27a9c17 | 2009-02-27 23:47:00 +0200 | [diff] [blame] | 159 | plff->strong = strong; | 
|  | 160 | plff->weak = weak; | 
|  | 161 |  | 
|  | 162 | *strong = 0x00; | 
|  | 163 | *weak = 0x00; | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 164 | usbhid_submit_report(hid, plff->report, USB_DIR_OUT); | 
|  | 165 | } | 
|  | 166 |  | 
| Jiri Slaby | 7957501 | 2008-09-18 12:23:34 +0200 | [diff] [blame] | 167 | dev_info(&hid->dev, "Force feedback for PantherLord/GreenAsia " | 
| Anssi Hannula | 5edc41e | 2007-09-19 16:13:20 +0200 | [diff] [blame] | 168 | "devices by Anssi Hannula <anssi.hannula@gmail.com>\n"); | 
| Anssi Hannula | 20eb127 | 2007-01-11 16:51:18 +0200 | [diff] [blame] | 169 |  | 
|  | 170 | return 0; | 
|  | 171 | } | 
| Jiri Slaby | 5f02229 | 2008-09-18 19:43:32 +0200 | [diff] [blame] | 172 | #else | 
|  | 173 | static inline int plff_init(struct hid_device *hid) | 
|  | 174 | { | 
|  | 175 | return 0; | 
|  | 176 | } | 
|  | 177 | #endif | 
|  | 178 |  | 
|  | 179 | static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id) | 
|  | 180 | { | 
|  | 181 | int ret; | 
|  | 182 |  | 
|  | 183 | if (id->driver_data) | 
|  | 184 | hdev->quirks |= HID_QUIRK_MULTI_INPUT; | 
|  | 185 |  | 
|  | 186 | ret = hid_parse(hdev); | 
|  | 187 | if (ret) { | 
|  | 188 | dev_err(&hdev->dev, "parse failed\n"); | 
|  | 189 | goto err; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); | 
|  | 193 | if (ret) { | 
|  | 194 | dev_err(&hdev->dev, "hw start failed\n"); | 
|  | 195 | goto err; | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | plff_init(hdev); | 
|  | 199 |  | 
|  | 200 | return 0; | 
|  | 201 | err: | 
|  | 202 | return ret; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | static const struct hid_device_id pl_devices[] = { | 
|  | 206 | { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR), | 
|  | 207 | .driver_data = 1 }, /* Twin USB Joystick */ | 
| Jiri Kosina | 578f3a3 | 2008-11-20 15:55:38 +0100 | [diff] [blame] | 208 | { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR), | 
|  | 209 | .driver_data = 1 }, /* Twin USB Joystick */ | 
| Anssi Hannula | 27a9c17 | 2009-02-27 23:47:00 +0200 | [diff] [blame] | 210 | { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), }, | 
| Jiri Slaby | 5f02229 | 2008-09-18 19:43:32 +0200 | [diff] [blame] | 211 | { } | 
|  | 212 | }; | 
|  | 213 | MODULE_DEVICE_TABLE(hid, pl_devices); | 
|  | 214 |  | 
|  | 215 | static struct hid_driver pl_driver = { | 
|  | 216 | .name = "pantherlord", | 
|  | 217 | .id_table = pl_devices, | 
|  | 218 | .probe = pl_probe, | 
|  | 219 | }; | 
|  | 220 |  | 
| Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 221 | static int __init pl_init(void) | 
| Jiri Slaby | 5f02229 | 2008-09-18 19:43:32 +0200 | [diff] [blame] | 222 | { | 
|  | 223 | return hid_register_driver(&pl_driver); | 
|  | 224 | } | 
|  | 225 |  | 
| Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 226 | static void __exit pl_exit(void) | 
| Jiri Slaby | 5f02229 | 2008-09-18 19:43:32 +0200 | [diff] [blame] | 227 | { | 
|  | 228 | hid_unregister_driver(&pl_driver); | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | module_init(pl_init); | 
|  | 232 | module_exit(pl_exit); | 
|  | 233 | MODULE_LICENSE("GPL"); |