Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6/libata-dev.git] / drivers / hid / hid-lg2ff.c
blob3c31bc650e5d6e8b957618fb9d9dd306f677c22f
1 /*
2 * Force feedback support for Logitech RumblePad and Rumblepad 2
4 * Copyright (c) 2008 Anssi Hannula <anssi.hannula@gmail.com>
5 */
7 /*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/input.h>
25 #include <linux/slab.h>
26 #include <linux/usb.h>
27 #include <linux/hid.h>
29 #include "usbhid/usbhid.h"
30 #include "hid-lg.h"
32 struct lg2ff_device {
33 struct hid_report *report;
36 static int play_effect(struct input_dev *dev, void *data,
37 struct ff_effect *effect)
39 struct hid_device *hid = input_get_drvdata(dev);
40 struct lg2ff_device *lg2ff = data;
41 int weak, strong;
43 strong = effect->u.rumble.strong_magnitude;
44 weak = effect->u.rumble.weak_magnitude;
46 if (weak || strong) {
47 weak = weak * 0xff / 0xffff;
48 strong = strong * 0xff / 0xffff;
50 lg2ff->report->field[0]->value[0] = 0x51;
51 lg2ff->report->field[0]->value[2] = weak;
52 lg2ff->report->field[0]->value[4] = strong;
53 } else {
54 lg2ff->report->field[0]->value[0] = 0xf3;
55 lg2ff->report->field[0]->value[2] = 0x00;
56 lg2ff->report->field[0]->value[4] = 0x00;
59 usbhid_submit_report(hid, lg2ff->report, USB_DIR_OUT);
60 return 0;
63 int lg2ff_init(struct hid_device *hid)
65 struct lg2ff_device *lg2ff;
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 input_dev *dev = hidinput->input;
72 int error;
74 if (list_empty(report_list)) {
75 hid_err(hid, "no output report found\n");
76 return -ENODEV;
79 report = list_entry(report_list->next, struct hid_report, list);
81 if (report->maxfield < 1) {
82 hid_err(hid, "output report is empty\n");
83 return -ENODEV;
85 if (report->field[0]->report_count < 7) {
86 hid_err(hid, "not enough values in the field\n");
87 return -ENODEV;
90 lg2ff = kmalloc(sizeof(struct lg2ff_device), GFP_KERNEL);
91 if (!lg2ff)
92 return -ENOMEM;
94 set_bit(FF_RUMBLE, dev->ffbit);
96 error = input_ff_create_memless(dev, lg2ff, play_effect);
97 if (error) {
98 kfree(lg2ff);
99 return error;
102 lg2ff->report = report;
103 report->field[0]->value[0] = 0xf3;
104 report->field[0]->value[1] = 0x00;
105 report->field[0]->value[2] = 0x00;
106 report->field[0]->value[3] = 0x00;
107 report->field[0]->value[4] = 0x00;
108 report->field[0]->value[5] = 0x00;
109 report->field[0]->value[6] = 0x00;
111 usbhid_submit_report(hid, report, USB_DIR_OUT);
113 hid_info(hid, "Force feedback for Logitech RumblePad/Rumblepad 2 by Anssi Hannula <anssi.hannula@gmail.com>\n");
115 return 0;