HID: use hid-plff driver for GreenAsia 0e8f:0003 devices
[safe/jmp/linux-2.6] / drivers / hid / usbhid / hid-plff.c
1 /*
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     "
12  *   - tested with Köng Gaming gamepad
13  *
14  *  Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
15  */
16
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  */
32
33
34 /* #define DEBUG */
35
36 #define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
37
38 #include <linux/input.h>
39 #include <linux/usb.h>
40 #include <linux/hid.h>
41 #include "usbhid.h"
42
43 struct plff_device {
44         struct hid_report *report;
45 };
46
47 static int hid_plff_play(struct input_dev *dev, void *data,
48                          struct ff_effect *effect)
49 {
50         struct hid_device *hid = input_get_drvdata(dev);
51         struct plff_device *plff = data;
52         int left, right;
53
54         left = effect->u.rumble.strong_magnitude;
55         right = effect->u.rumble.weak_magnitude;
56         debug("called with 0x%04x 0x%04x", left, right);
57
58         left = left * 0x7f / 0xffff;
59         right = right * 0x7f / 0xffff;
60
61         plff->report->field[0]->value[2] = left;
62         plff->report->field[0]->value[3] = right;
63         debug("running with 0x%02x 0x%02x", left, right);
64         usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
65
66         return 0;
67 }
68
69 int hid_plff_init(struct hid_device *hid)
70 {
71         struct plff_device *plff;
72         struct hid_report *report;
73         struct hid_input *hidinput;
74         struct list_head *report_list =
75                         &hid->report_enum[HID_OUTPUT_REPORT].report_list;
76         struct list_head *report_ptr = report_list;
77         struct input_dev *dev;
78         int error;
79
80         /* The device contains one output report per physical device, all
81            containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
82            absolute values.
83
84            The input reports also contain a field which contains
85            8 ff00.0001 usages and 8 boolean values. Their meaning is
86            currently unknown. */
87
88         if (list_empty(report_list)) {
89                 printk(KERN_ERR "hid-plff: no output reports found\n");
90                 return -ENODEV;
91         }
92
93         list_for_each_entry(hidinput, &hid->inputs, list) {
94
95                 report_ptr = report_ptr->next;
96
97                 if (report_ptr == report_list) {
98                         printk(KERN_ERR "hid-plff: required output report is missing\n");
99                         return -ENODEV;
100                 }
101
102                 report = list_entry(report_ptr, struct hid_report, list);
103                 if (report->maxfield < 1) {
104                         printk(KERN_ERR "hid-plff: no fields in the report\n");
105                         return -ENODEV;
106                 }
107
108                 if (report->field[0]->report_count < 4) {
109                         printk(KERN_ERR "hid-plff: not enough values in the field\n");
110                         return -ENODEV;
111                 }
112
113                 plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
114                 if (!plff)
115                         return -ENOMEM;
116
117                 dev = hidinput->input;
118
119                 set_bit(FF_RUMBLE, dev->ffbit);
120
121                 error = input_ff_create_memless(dev, plff, hid_plff_play);
122                 if (error) {
123                         kfree(plff);
124                         return error;
125                 }
126
127                 plff->report = report;
128                 plff->report->field[0]->value[0] = 0x00;
129                 plff->report->field[0]->value[1] = 0x00;
130                 plff->report->field[0]->value[2] = 0x00;
131                 plff->report->field[0]->value[3] = 0x00;
132                 usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
133         }
134
135         printk(KERN_INFO "hid-plff: Force feedback for PantherLord/GreenAsia "
136                "devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
137
138         return 0;
139 }