blob: 4e6dc6e26523a22a8d109d4d71fe68d89628641b [file] [log] [blame]
Anssi Hannulac17f9c92008-04-01 01:51:11 +02001/*
2 * Force feedback support for Logitech Rumblepad 2
3 *
4 * Copyright (c) 2008 Anssi Hannula <anssi.hannula@gmail.com>
5 */
6
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.
12 *
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.
17 *
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
21 */
22
23
24#include <linux/input.h>
25#include <linux/usb.h>
26#include <linux/hid.h>
Jiri Slaby606bd0a2008-07-04 23:06:45 +020027
28#include "usbhid/usbhid.h"
29#include "hid-lg.h"
Anssi Hannulac17f9c92008-04-01 01:51:11 +020030
31struct lg2ff_device {
32 struct hid_report *report;
33};
34
35static int play_effect(struct input_dev *dev, void *data,
36 struct ff_effect *effect)
37{
38 struct hid_device *hid = input_get_drvdata(dev);
39 struct lg2ff_device *lg2ff = data;
40 int weak, strong;
41
42 strong = effect->u.rumble.strong_magnitude;
43 weak = effect->u.rumble.weak_magnitude;
44
45 if (weak || strong) {
46 weak = weak * 0xff / 0xffff;
47 strong = strong * 0xff / 0xffff;
48
49 lg2ff->report->field[0]->value[0] = 0x51;
50 lg2ff->report->field[0]->value[2] = weak;
51 lg2ff->report->field[0]->value[4] = strong;
52 } else {
53 lg2ff->report->field[0]->value[0] = 0xf3;
54 lg2ff->report->field[0]->value[2] = 0x00;
55 lg2ff->report->field[0]->value[4] = 0x00;
56 }
57
58 usbhid_submit_report(hid, lg2ff->report, USB_DIR_OUT);
59 return 0;
60}
61
Jiri Slaby606bd0a2008-07-04 23:06:45 +020062int lg2ff_init(struct hid_device *hid)
Anssi Hannulac17f9c92008-04-01 01:51:11 +020063{
64 struct lg2ff_device *lg2ff;
65 struct hid_report *report;
66 struct hid_input *hidinput = list_entry(hid->inputs.next,
67 struct hid_input, list);
68 struct list_head *report_list =
69 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
70 struct input_dev *dev = hidinput->input;
71 int error;
72
73 if (list_empty(report_list)) {
Jiri Slaby79575012008-09-18 12:23:34 +020074 dev_err(&hid->dev, "no output report found\n");
Anssi Hannulac17f9c92008-04-01 01:51:11 +020075 return -ENODEV;
76 }
77
78 report = list_entry(report_list->next, struct hid_report, list);
79
80 if (report->maxfield < 1) {
Jiri Slaby79575012008-09-18 12:23:34 +020081 dev_err(&hid->dev, "output report is empty\n");
Anssi Hannulac17f9c92008-04-01 01:51:11 +020082 return -ENODEV;
83 }
84 if (report->field[0]->report_count < 7) {
Jiri Slaby79575012008-09-18 12:23:34 +020085 dev_err(&hid->dev, "not enough values in the field\n");
Anssi Hannulac17f9c92008-04-01 01:51:11 +020086 return -ENODEV;
87 }
88
89 lg2ff = kmalloc(sizeof(struct lg2ff_device), GFP_KERNEL);
90 if (!lg2ff)
91 return -ENOMEM;
92
93 set_bit(FF_RUMBLE, dev->ffbit);
94
95 error = input_ff_create_memless(dev, lg2ff, play_effect);
96 if (error) {
97 kfree(lg2ff);
98 return error;
99 }
100
101 lg2ff->report = report;
102 report->field[0]->value[0] = 0xf3;
103 report->field[0]->value[1] = 0x00;
104 report->field[0]->value[2] = 0x00;
105 report->field[0]->value[3] = 0x00;
106 report->field[0]->value[4] = 0x00;
107 report->field[0]->value[5] = 0x00;
108 report->field[0]->value[6] = 0x00;
109
110 usbhid_submit_report(hid, report, USB_DIR_OUT);
111
Jiri Slaby79575012008-09-18 12:23:34 +0200112 dev_info(&hid->dev, "Force feedback for Logitech Rumblepad 2 by "
Anssi Hannulac17f9c92008-04-01 01:51:11 +0200113 "Anssi Hannula <anssi.hannula@gmail.com>\n");
114
115 return 0;
116}