/*1* Force feedback support for DragonRise Inc. game controllers2*3* From what I have gathered, these devices are mass produced in China and are4* distributed under several vendors. They often share the same design as5* the original PlayStation DualShock controller.6*7* 0079:0006 "DragonRise Inc. Generic USB Joystick "8* - tested with a Tesun USB-703 game controller.9*10* Copyright (c) 2009 Richard Walmsley <[email protected]>11*/1213/*14* This program is free software; you can redistribute it and/or modify15* it under the terms of the GNU General Public License as published by16* the Free Software Foundation; either version 2 of the License, or17* (at your option) any later version.18*19* This program is distributed in the hope that it will be useful,20* but WITHOUT ANY WARRANTY; without even the implied warranty of21* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the22* GNU General Public License for more details.23*24* You should have received a copy of the GNU General Public License25* along with this program; if not, write to the Free Software26* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA27*/2829#include <linux/input.h>30#include <linux/slab.h>31#include <linux/usb.h>32#include <linux/hid.h>3334#include "hid-ids.h"3536#ifdef CONFIG_DRAGONRISE_FF37#include "usbhid/usbhid.h"3839struct drff_device {40struct hid_report *report;41};4243static int drff_play(struct input_dev *dev, void *data,44struct ff_effect *effect)45{46struct hid_device *hid = input_get_drvdata(dev);47struct drff_device *drff = data;48int strong, weak;4950strong = effect->u.rumble.strong_magnitude;51weak = effect->u.rumble.weak_magnitude;5253dbg_hid("called with 0x%04x 0x%04x", strong, weak);5455if (strong || weak) {56strong = strong * 0xff / 0xffff;57weak = weak * 0xff / 0xffff;5859/* While reverse engineering this device, I found that when60this value is set, it causes the strong rumble to function61at a near maximum speed, so we'll bypass it. */62if (weak == 0x0a)63weak = 0x0b;6465drff->report->field[0]->value[0] = 0x51;66drff->report->field[0]->value[1] = 0x00;67drff->report->field[0]->value[2] = weak;68drff->report->field[0]->value[4] = strong;69usbhid_submit_report(hid, drff->report, USB_DIR_OUT);7071drff->report->field[0]->value[0] = 0xfa;72drff->report->field[0]->value[1] = 0xfe;73} else {74drff->report->field[0]->value[0] = 0xf3;75drff->report->field[0]->value[1] = 0x00;76}7778drff->report->field[0]->value[2] = 0x00;79drff->report->field[0]->value[4] = 0x00;80dbg_hid("running with 0x%02x 0x%02x", strong, weak);81usbhid_submit_report(hid, drff->report, USB_DIR_OUT);8283return 0;84}8586static int drff_init(struct hid_device *hid)87{88struct drff_device *drff;89struct hid_report *report;90struct hid_input *hidinput = list_first_entry(&hid->inputs,91struct hid_input, list);92struct list_head *report_list =93&hid->report_enum[HID_OUTPUT_REPORT].report_list;94struct input_dev *dev = hidinput->input;95int error;9697if (list_empty(report_list)) {98hid_err(hid, "no output reports found\n");99return -ENODEV;100}101102report = list_first_entry(report_list, struct hid_report, list);103if (report->maxfield < 1) {104hid_err(hid, "no fields in the report\n");105return -ENODEV;106}107108if (report->field[0]->report_count < 7) {109hid_err(hid, "not enough values in the field\n");110return -ENODEV;111}112113drff = kzalloc(sizeof(struct drff_device), GFP_KERNEL);114if (!drff)115return -ENOMEM;116117set_bit(FF_RUMBLE, dev->ffbit);118119error = input_ff_create_memless(dev, drff, drff_play);120if (error) {121kfree(drff);122return error;123}124125drff->report = report;126drff->report->field[0]->value[0] = 0xf3;127drff->report->field[0]->value[1] = 0x00;128drff->report->field[0]->value[2] = 0x00;129drff->report->field[0]->value[3] = 0x00;130drff->report->field[0]->value[4] = 0x00;131drff->report->field[0]->value[5] = 0x00;132drff->report->field[0]->value[6] = 0x00;133usbhid_submit_report(hid, drff->report, USB_DIR_OUT);134135hid_info(hid, "Force Feedback for DragonRise Inc. "136"game controllers by Richard Walmsley <[email protected]>\n");137138return 0;139}140#else141static inline int drff_init(struct hid_device *hid)142{143return 0;144}145#endif146147/*148* The original descriptor of joystick with PID 0x0011, represented by DVTech PC149* JS19. It seems both copied from another device and a result of confusion150* either about the specification or about the program used to create the151* descriptor. In any case, it's a wonder it works on Windows.152*153* Usage Page (Desktop), ; Generic desktop controls (01h)154* Usage (Joystik), ; Joystik (04h, application collection)155* Collection (Application),156* Collection (Logical),157* Report Size (8),158* Report Count (5),159* Logical Minimum (0),160* Logical Maximum (255),161* Physical Minimum (0),162* Physical Maximum (255),163* Usage (X), ; X (30h, dynamic value)164* Usage (X), ; X (30h, dynamic value)165* Usage (X), ; X (30h, dynamic value)166* Usage (X), ; X (30h, dynamic value)167* Usage (Y), ; Y (31h, dynamic value)168* Input (Variable),169* Report Size (4),170* Report Count (1),171* Logical Maximum (7),172* Physical Maximum (315),173* Unit (Degrees),174* Usage (00h),175* Input (Variable, Null State),176* Unit,177* Report Size (1),178* Report Count (10),179* Logical Maximum (1),180* Physical Maximum (1),181* Usage Page (Button), ; Button (09h)182* Usage Minimum (01h),183* Usage Maximum (0Ah),184* Input (Variable),185* Usage Page (FF00h), ; FF00h, vendor-defined186* Report Size (1),187* Report Count (10),188* Logical Maximum (1),189* Physical Maximum (1),190* Usage (01h),191* Input (Variable),192* End Collection,193* Collection (Logical),194* Report Size (8),195* Report Count (4),196* Physical Maximum (255),197* Logical Maximum (255),198* Usage (02h),199* Output (Variable),200* End Collection,201* End Collection202*/203204/* Size of the original descriptor of the PID 0x0011 joystick */205#define PID0011_RDESC_ORIG_SIZE 101206207/* Fixed report descriptor for PID 0x011 joystick */208static __u8 pid0011_rdesc_fixed[] = {2090x05, 0x01, /* Usage Page (Desktop), */2100x09, 0x04, /* Usage (Joystik), */2110xA1, 0x01, /* Collection (Application), */2120xA1, 0x02, /* Collection (Logical), */2130x14, /* Logical Minimum (0), */2140x75, 0x08, /* Report Size (8), */2150x95, 0x03, /* Report Count (3), */2160x81, 0x01, /* Input (Constant), */2170x26, 0xFF, 0x00, /* Logical Maximum (255), */2180x95, 0x02, /* Report Count (2), */2190x09, 0x30, /* Usage (X), */2200x09, 0x31, /* Usage (Y), */2210x81, 0x02, /* Input (Variable), */2220x75, 0x01, /* Report Size (1), */2230x95, 0x04, /* Report Count (4), */2240x81, 0x01, /* Input (Constant), */2250x25, 0x01, /* Logical Maximum (1), */2260x95, 0x0A, /* Report Count (10), */2270x05, 0x09, /* Usage Page (Button), */2280x19, 0x01, /* Usage Minimum (01h), */2290x29, 0x0A, /* Usage Maximum (0Ah), */2300x81, 0x02, /* Input (Variable), */2310x95, 0x0A, /* Report Count (10), */2320x81, 0x01, /* Input (Constant), */2330xC0, /* End Collection, */2340xC0 /* End Collection */235};236237static __u8 *dr_report_fixup(struct hid_device *hdev, __u8 *rdesc,238unsigned int *rsize)239{240switch (hdev->product) {241case 0x0011:242if (*rsize == PID0011_RDESC_ORIG_SIZE) {243rdesc = pid0011_rdesc_fixed;244*rsize = sizeof(pid0011_rdesc_fixed);245}246break;247}248return rdesc;249}250251static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)252{253int ret;254255dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");256257ret = hid_parse(hdev);258if (ret) {259hid_err(hdev, "parse failed\n");260goto err;261}262263ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);264if (ret) {265hid_err(hdev, "hw start failed\n");266goto err;267}268269switch (hdev->product) {270case 0x0006:271ret = drff_init(hdev);272if (ret) {273dev_err(&hdev->dev, "force feedback init failed\n");274hid_hw_stop(hdev);275goto err;276}277break;278}279280return 0;281err:282return ret;283}284285static const struct hid_device_id dr_devices[] = {286{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006), },287{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011), },288{ }289};290MODULE_DEVICE_TABLE(hid, dr_devices);291292static struct hid_driver dr_driver = {293.name = "dragonrise",294.id_table = dr_devices,295.report_fixup = dr_report_fixup,296.probe = dr_probe,297};298299static int __init dr_init(void)300{301return hid_register_driver(&dr_driver);302}303304static void __exit dr_exit(void)305{306hid_unregister_driver(&dr_driver);307}308309module_init(dr_init);310module_exit(dr_exit);311MODULE_LICENSE("GPL");312313314