142859e0bSLukasz Lubojanski /* 242859e0bSLukasz Lubojanski * Force feedback support for GreenAsia (Product ID 0x12) based devices 342859e0bSLukasz Lubojanski * 442859e0bSLukasz Lubojanski * The devices are distributed under various names and the same USB device ID 542859e0bSLukasz Lubojanski * can be used in many game controllers. 642859e0bSLukasz Lubojanski * 742859e0bSLukasz Lubojanski * 842859e0bSLukasz Lubojanski * 0e8f:0012 "GreenAsia Inc. USB Joystick " 942859e0bSLukasz Lubojanski * - tested with MANTA Warior MM816 and SpeedLink Strike2 SL-6635. 1042859e0bSLukasz Lubojanski * 1142859e0bSLukasz Lubojanski * Copyright (c) 2008 Lukasz Lubojanski <lukasz@lubojanski.info> 1242859e0bSLukasz Lubojanski */ 1342859e0bSLukasz Lubojanski 1442859e0bSLukasz Lubojanski /* 1542859e0bSLukasz Lubojanski * This program is free software; you can redistribute it and/or modify 1642859e0bSLukasz Lubojanski * it under the terms of the GNU General Public License as published by 1742859e0bSLukasz Lubojanski * the Free Software Foundation; either version 2 of the License, or 1842859e0bSLukasz Lubojanski * (at your option) any later version. 1942859e0bSLukasz Lubojanski * 2042859e0bSLukasz Lubojanski * This program is distributed in the hope that it will be useful, 2142859e0bSLukasz Lubojanski * but WITHOUT ANY WARRANTY; without even the implied warranty of 2242859e0bSLukasz Lubojanski * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2342859e0bSLukasz Lubojanski * GNU General Public License for more details. 2442859e0bSLukasz Lubojanski * 2542859e0bSLukasz Lubojanski * You should have received a copy of the GNU General Public License 2642859e0bSLukasz Lubojanski * along with this program; if not, write to the Free Software 2742859e0bSLukasz Lubojanski * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 2842859e0bSLukasz Lubojanski */ 2942859e0bSLukasz Lubojanski 3042859e0bSLukasz Lubojanski #include <linux/input.h> 315a0e3ad6STejun Heo #include <linux/slab.h> 3242859e0bSLukasz Lubojanski #include <linux/usb.h> 3342859e0bSLukasz Lubojanski #include <linux/hid.h> 34*8f86a2c3SPaul Gortmaker #include <linux/module.h> 3542859e0bSLukasz Lubojanski #include "hid-ids.h" 360f6f4319SJiri Kosina 370f6f4319SJiri Kosina #ifdef CONFIG_GREENASIA_FF 3842859e0bSLukasz Lubojanski #include "usbhid/usbhid.h" 3942859e0bSLukasz Lubojanski 4042859e0bSLukasz Lubojanski struct gaff_device { 4142859e0bSLukasz Lubojanski struct hid_report *report; 4242859e0bSLukasz Lubojanski }; 4342859e0bSLukasz Lubojanski 4442859e0bSLukasz Lubojanski static int hid_gaff_play(struct input_dev *dev, void *data, 4542859e0bSLukasz Lubojanski struct ff_effect *effect) 4642859e0bSLukasz Lubojanski { 4742859e0bSLukasz Lubojanski struct hid_device *hid = input_get_drvdata(dev); 4842859e0bSLukasz Lubojanski struct gaff_device *gaff = data; 4942859e0bSLukasz Lubojanski int left, right; 5042859e0bSLukasz Lubojanski 5142859e0bSLukasz Lubojanski left = effect->u.rumble.strong_magnitude; 5242859e0bSLukasz Lubojanski right = effect->u.rumble.weak_magnitude; 5342859e0bSLukasz Lubojanski 5442859e0bSLukasz Lubojanski dbg_hid("called with 0x%04x 0x%04x", left, right); 5542859e0bSLukasz Lubojanski 5642859e0bSLukasz Lubojanski left = left * 0xfe / 0xffff; 5742859e0bSLukasz Lubojanski right = right * 0xfe / 0xffff; 5842859e0bSLukasz Lubojanski 5942859e0bSLukasz Lubojanski gaff->report->field[0]->value[0] = 0x51; 6042859e0bSLukasz Lubojanski gaff->report->field[0]->value[1] = 0x0; 6142859e0bSLukasz Lubojanski gaff->report->field[0]->value[2] = right; 6242859e0bSLukasz Lubojanski gaff->report->field[0]->value[3] = 0; 6342859e0bSLukasz Lubojanski gaff->report->field[0]->value[4] = left; 6442859e0bSLukasz Lubojanski gaff->report->field[0]->value[5] = 0; 6542859e0bSLukasz Lubojanski dbg_hid("running with 0x%02x 0x%02x", left, right); 6642859e0bSLukasz Lubojanski usbhid_submit_report(hid, gaff->report, USB_DIR_OUT); 6742859e0bSLukasz Lubojanski 6842859e0bSLukasz Lubojanski gaff->report->field[0]->value[0] = 0xfa; 6942859e0bSLukasz Lubojanski gaff->report->field[0]->value[1] = 0xfe; 7042859e0bSLukasz Lubojanski gaff->report->field[0]->value[2] = 0x0; 7142859e0bSLukasz Lubojanski gaff->report->field[0]->value[4] = 0x0; 7242859e0bSLukasz Lubojanski 7342859e0bSLukasz Lubojanski usbhid_submit_report(hid, gaff->report, USB_DIR_OUT); 7442859e0bSLukasz Lubojanski 7542859e0bSLukasz Lubojanski return 0; 7642859e0bSLukasz Lubojanski } 7742859e0bSLukasz Lubojanski 7842859e0bSLukasz Lubojanski static int gaff_init(struct hid_device *hid) 7942859e0bSLukasz Lubojanski { 8042859e0bSLukasz Lubojanski struct gaff_device *gaff; 8142859e0bSLukasz Lubojanski struct hid_report *report; 8242859e0bSLukasz Lubojanski struct hid_input *hidinput = list_entry(hid->inputs.next, 8342859e0bSLukasz Lubojanski struct hid_input, list); 8442859e0bSLukasz Lubojanski struct list_head *report_list = 8542859e0bSLukasz Lubojanski &hid->report_enum[HID_OUTPUT_REPORT].report_list; 8642859e0bSLukasz Lubojanski struct list_head *report_ptr = report_list; 8742859e0bSLukasz Lubojanski struct input_dev *dev = hidinput->input; 8842859e0bSLukasz Lubojanski int error; 8942859e0bSLukasz Lubojanski 9042859e0bSLukasz Lubojanski if (list_empty(report_list)) { 914291ee30SJoe Perches hid_err(hid, "no output reports found\n"); 9242859e0bSLukasz Lubojanski return -ENODEV; 9342859e0bSLukasz Lubojanski } 9442859e0bSLukasz Lubojanski 9542859e0bSLukasz Lubojanski report_ptr = report_ptr->next; 9642859e0bSLukasz Lubojanski 9742859e0bSLukasz Lubojanski report = list_entry(report_ptr, struct hid_report, list); 9842859e0bSLukasz Lubojanski if (report->maxfield < 1) { 994291ee30SJoe Perches hid_err(hid, "no fields in the report\n"); 10042859e0bSLukasz Lubojanski return -ENODEV; 10142859e0bSLukasz Lubojanski } 10242859e0bSLukasz Lubojanski 10342859e0bSLukasz Lubojanski if (report->field[0]->report_count < 6) { 1044291ee30SJoe Perches hid_err(hid, "not enough values in the field\n"); 10542859e0bSLukasz Lubojanski return -ENODEV; 10642859e0bSLukasz Lubojanski } 10742859e0bSLukasz Lubojanski 10842859e0bSLukasz Lubojanski gaff = kzalloc(sizeof(struct gaff_device), GFP_KERNEL); 10942859e0bSLukasz Lubojanski if (!gaff) 11042859e0bSLukasz Lubojanski return -ENOMEM; 11142859e0bSLukasz Lubojanski 11242859e0bSLukasz Lubojanski set_bit(FF_RUMBLE, dev->ffbit); 11342859e0bSLukasz Lubojanski 11442859e0bSLukasz Lubojanski error = input_ff_create_memless(dev, gaff, hid_gaff_play); 11542859e0bSLukasz Lubojanski if (error) { 11642859e0bSLukasz Lubojanski kfree(gaff); 11742859e0bSLukasz Lubojanski return error; 11842859e0bSLukasz Lubojanski } 11942859e0bSLukasz Lubojanski 12042859e0bSLukasz Lubojanski gaff->report = report; 12142859e0bSLukasz Lubojanski gaff->report->field[0]->value[0] = 0x51; 12242859e0bSLukasz Lubojanski gaff->report->field[0]->value[1] = 0x00; 12342859e0bSLukasz Lubojanski gaff->report->field[0]->value[2] = 0x00; 12442859e0bSLukasz Lubojanski gaff->report->field[0]->value[3] = 0x00; 12542859e0bSLukasz Lubojanski usbhid_submit_report(hid, gaff->report, USB_DIR_OUT); 12642859e0bSLukasz Lubojanski 12742859e0bSLukasz Lubojanski gaff->report->field[0]->value[0] = 0xfa; 12842859e0bSLukasz Lubojanski gaff->report->field[0]->value[1] = 0xfe; 12942859e0bSLukasz Lubojanski 13042859e0bSLukasz Lubojanski usbhid_submit_report(hid, gaff->report, USB_DIR_OUT); 13142859e0bSLukasz Lubojanski 1324291ee30SJoe Perches hid_info(hid, "Force Feedback for GreenAsia 0x12 devices by Lukasz Lubojanski <lukasz@lubojanski.info>\n"); 13342859e0bSLukasz Lubojanski 13442859e0bSLukasz Lubojanski return 0; 13542859e0bSLukasz Lubojanski } 1360f6f4319SJiri Kosina #else 1370f6f4319SJiri Kosina static inline int gaff_init(struct hid_device *hdev) 1380f6f4319SJiri Kosina { 1390f6f4319SJiri Kosina return 0; 1400f6f4319SJiri Kosina } 1410f6f4319SJiri Kosina #endif 14242859e0bSLukasz Lubojanski 14342859e0bSLukasz Lubojanski static int ga_probe(struct hid_device *hdev, const struct hid_device_id *id) 14442859e0bSLukasz Lubojanski { 14542859e0bSLukasz Lubojanski int ret; 14642859e0bSLukasz Lubojanski 14742859e0bSLukasz Lubojanski dev_dbg(&hdev->dev, "Greenasia HID hardware probe..."); 14842859e0bSLukasz Lubojanski 14942859e0bSLukasz Lubojanski ret = hid_parse(hdev); 15042859e0bSLukasz Lubojanski if (ret) { 1514291ee30SJoe Perches hid_err(hdev, "parse failed\n"); 15242859e0bSLukasz Lubojanski goto err; 15342859e0bSLukasz Lubojanski } 15442859e0bSLukasz Lubojanski 15542859e0bSLukasz Lubojanski ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); 15642859e0bSLukasz Lubojanski if (ret) { 1574291ee30SJoe Perches hid_err(hdev, "hw start failed\n"); 15842859e0bSLukasz Lubojanski goto err; 15942859e0bSLukasz Lubojanski } 16042859e0bSLukasz Lubojanski 16142859e0bSLukasz Lubojanski gaff_init(hdev); 16242859e0bSLukasz Lubojanski 16342859e0bSLukasz Lubojanski return 0; 16442859e0bSLukasz Lubojanski err: 16542859e0bSLukasz Lubojanski return ret; 16642859e0bSLukasz Lubojanski } 16742859e0bSLukasz Lubojanski 16842859e0bSLukasz Lubojanski static const struct hid_device_id ga_devices[] = { 16942859e0bSLukasz Lubojanski { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012), }, 17042859e0bSLukasz Lubojanski { } 17142859e0bSLukasz Lubojanski }; 17242859e0bSLukasz Lubojanski MODULE_DEVICE_TABLE(hid, ga_devices); 17342859e0bSLukasz Lubojanski 17442859e0bSLukasz Lubojanski static struct hid_driver ga_driver = { 17542859e0bSLukasz Lubojanski .name = "greenasia", 17642859e0bSLukasz Lubojanski .id_table = ga_devices, 17742859e0bSLukasz Lubojanski .probe = ga_probe, 17842859e0bSLukasz Lubojanski }; 17942859e0bSLukasz Lubojanski 18042859e0bSLukasz Lubojanski static int __init ga_init(void) 18142859e0bSLukasz Lubojanski { 18242859e0bSLukasz Lubojanski return hid_register_driver(&ga_driver); 18342859e0bSLukasz Lubojanski } 18442859e0bSLukasz Lubojanski 18542859e0bSLukasz Lubojanski static void __exit ga_exit(void) 18642859e0bSLukasz Lubojanski { 18742859e0bSLukasz Lubojanski hid_unregister_driver(&ga_driver); 18842859e0bSLukasz Lubojanski } 18942859e0bSLukasz Lubojanski 19042859e0bSLukasz Lubojanski module_init(ga_init); 19142859e0bSLukasz Lubojanski module_exit(ga_exit); 19242859e0bSLukasz Lubojanski MODULE_LICENSE("GPL"); 193