Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 1 | /* |
Hendrik Iben | 2c6118e | 2010-10-04 15:39:49 +0200 | [diff] [blame] | 2 | * Force feedback support for Logitech RumblePad and Rumblepad 2 |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 26 | #include <linux/hid.h> |
Jiri Slaby | 606bd0a | 2008-07-04 23:06:45 +0200 | [diff] [blame] | 27 | |
Jiri Slaby | 606bd0a | 2008-07-04 23:06:45 +0200 | [diff] [blame] | 28 | #include "hid-lg.h" |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 29 | |
| 30 | struct lg2ff_device { |
| 31 | struct hid_report *report; |
| 32 | }; |
| 33 | |
| 34 | static int play_effect(struct input_dev *dev, void *data, |
| 35 | struct ff_effect *effect) |
| 36 | { |
| 37 | struct hid_device *hid = input_get_drvdata(dev); |
| 38 | struct lg2ff_device *lg2ff = data; |
| 39 | int weak, strong; |
| 40 | |
| 41 | strong = effect->u.rumble.strong_magnitude; |
| 42 | weak = effect->u.rumble.weak_magnitude; |
| 43 | |
| 44 | if (weak || strong) { |
| 45 | weak = weak * 0xff / 0xffff; |
| 46 | strong = strong * 0xff / 0xffff; |
| 47 | |
| 48 | lg2ff->report->field[0]->value[0] = 0x51; |
| 49 | lg2ff->report->field[0]->value[2] = weak; |
| 50 | lg2ff->report->field[0]->value[4] = strong; |
| 51 | } else { |
| 52 | lg2ff->report->field[0]->value[0] = 0xf3; |
| 53 | lg2ff->report->field[0]->value[2] = 0x00; |
| 54 | lg2ff->report->field[0]->value[4] = 0x00; |
| 55 | } |
| 56 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 57 | hid_hw_request(hid, lg2ff->report, HID_REQ_SET_REPORT); |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
Jiri Slaby | 606bd0a | 2008-07-04 23:06:45 +0200 | [diff] [blame] | 61 | int lg2ff_init(struct hid_device *hid) |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 62 | { |
| 63 | struct lg2ff_device *lg2ff; |
| 64 | struct hid_report *report; |
| 65 | struct hid_input *hidinput = list_entry(hid->inputs.next, |
| 66 | struct hid_input, list); |
| 67 | struct list_head *report_list = |
| 68 | &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 69 | struct input_dev *dev = hidinput->input; |
| 70 | int error; |
| 71 | |
| 72 | if (list_empty(report_list)) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 73 | hid_err(hid, "no output report found\n"); |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 74 | return -ENODEV; |
| 75 | } |
| 76 | |
| 77 | report = list_entry(report_list->next, struct hid_report, list); |
| 78 | |
| 79 | if (report->maxfield < 1) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 80 | hid_err(hid, "output report is empty\n"); |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 81 | return -ENODEV; |
| 82 | } |
| 83 | if (report->field[0]->report_count < 7) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 84 | hid_err(hid, "not enough values in the field\n"); |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 85 | return -ENODEV; |
| 86 | } |
| 87 | |
| 88 | lg2ff = kmalloc(sizeof(struct lg2ff_device), GFP_KERNEL); |
| 89 | if (!lg2ff) |
| 90 | return -ENOMEM; |
| 91 | |
| 92 | set_bit(FF_RUMBLE, dev->ffbit); |
| 93 | |
| 94 | error = input_ff_create_memless(dev, lg2ff, play_effect); |
| 95 | if (error) { |
| 96 | kfree(lg2ff); |
| 97 | return error; |
| 98 | } |
| 99 | |
| 100 | lg2ff->report = report; |
| 101 | report->field[0]->value[0] = 0xf3; |
| 102 | report->field[0]->value[1] = 0x00; |
| 103 | report->field[0]->value[2] = 0x00; |
| 104 | report->field[0]->value[3] = 0x00; |
| 105 | report->field[0]->value[4] = 0x00; |
| 106 | report->field[0]->value[5] = 0x00; |
| 107 | report->field[0]->value[6] = 0x00; |
| 108 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 109 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 110 | |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 111 | hid_info(hid, "Force feedback for Logitech RumblePad/Rumblepad 2 by Anssi Hannula <anssi.hannula@gmail.com>\n"); |
Anssi Hannula | c17f9c9 | 2008-04-01 01:51:11 +0200 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |