Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Force feedback support for various HID compliant devices by ThrustMaster: |
| 3 | * ThrustMaster FireStorm Dual Power 2 |
| 4 | * and possibly others whose device ids haven't been added. |
| 5 | * |
| 6 | * Modified to support ThrustMaster devices by Zinx Verituse |
| 7 | * on 2003-01-25 from the Logitech force feedback driver, |
| 8 | * which is by Johann Deneux. |
| 9 | * |
| 10 | * Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org> |
| 11 | * Copyright (c) 2002 Johann Deneux |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 | */ |
| 29 | |
| 30 | #include <linux/input.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #undef DEBUG |
| 33 | #include <linux/usb.h> |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "hid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* Usages for thrustmaster devices I know about */ |
| 38 | #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | struct tmff_device { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct hid_report *report; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | struct hid_field *rumble; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Anssi Hannula | dc76c91 | 2006-07-19 01:40:55 -0400 | [diff] [blame] | 46 | /* Changes values from 0 to 0xffff into values from minimum to maximum */ |
| 47 | static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum) |
| 48 | { |
| 49 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Anssi Hannula | dc76c91 | 2006-07-19 01:40:55 -0400 | [diff] [blame] | 51 | ret = (in * (maximum - minimum) / 0xffff) + minimum; |
| 52 | if (ret < minimum) |
| 53 | return minimum; |
| 54 | if (ret > maximum) |
| 55 | return maximum; |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect) |
| 60 | { |
| 61 | struct hid_device *hid = dev->private; |
| 62 | struct tmff_device *tmff = data; |
| 63 | int left, right; /* Rumbling */ |
| 64 | |
| 65 | left = hid_tmff_scale(effect->u.rumble.weak_magnitude, |
| 66 | tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); |
| 67 | right = hid_tmff_scale(effect->u.rumble.strong_magnitude, |
| 68 | tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); |
| 69 | |
| 70 | tmff->rumble->value[0] = left; |
| 71 | tmff->rumble->value[1] = right; |
| 72 | dbg("(left,right)=(%08x, %08x)", left, right); |
| 73 | hid_submit_report(hid, tmff->report, USB_DIR_OUT); |
| 74 | |
| 75 | return 0; |
| 76 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | int hid_tmff_init(struct hid_device *hid) |
| 79 | { |
Anssi Hannula | dc76c91 | 2006-07-19 01:40:55 -0400 | [diff] [blame] | 80 | struct tmff_device *tmff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | struct list_head *pos; |
| 82 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 83 | struct input_dev *input_dev = hidinput->input; |
Anssi Hannula | dc76c91 | 2006-07-19 01:40:55 -0400 | [diff] [blame] | 84 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Anssi Hannula | dc76c91 | 2006-07-19 01:40:55 -0400 | [diff] [blame] | 86 | tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL); |
| 87 | if (!tmff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return -ENOMEM; |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* Find the report to use */ |
| 91 | __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) { |
| 92 | struct hid_report *report = (struct hid_report *)pos; |
| 93 | int fieldnum; |
| 94 | |
| 95 | for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) { |
| 96 | struct hid_field *field = report->field[fieldnum]; |
| 97 | |
| 98 | if (field->maxusage <= 0) |
| 99 | continue; |
| 100 | |
| 101 | switch (field->usage[0].hid) { |
| 102 | case THRUSTMASTER_USAGE_RUMBLE_LR: |
| 103 | if (field->report_count < 2) { |
| 104 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with report_count < 2"); |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | if (field->logical_maximum == field->logical_minimum) { |
| 109 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with logical_maximum == logical_minimum"); |
| 110 | continue; |
| 111 | } |
| 112 | |
Anssi Hannula | dc76c91 | 2006-07-19 01:40:55 -0400 | [diff] [blame] | 113 | if (tmff->report && tmff->report != report) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report"); |
| 115 | continue; |
| 116 | } |
| 117 | |
Anssi Hannula | dc76c91 | 2006-07-19 01:40:55 -0400 | [diff] [blame] | 118 | if (tmff->rumble && tmff->rumble != field) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR"); |
| 120 | continue; |
| 121 | } |
| 122 | |
Anssi Hannula | dc76c91 | 2006-07-19 01:40:55 -0400 | [diff] [blame] | 123 | tmff->report = report; |
| 124 | tmff->rumble = field; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 126 | set_bit(FF_RUMBLE, input_dev->ffbit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | break; |
| 128 | |
| 129 | default: |
| 130 | warn("ignoring unknown output usage %08x", field->usage[0].hid); |
| 131 | continue; |
| 132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Anssi Hannula | dc76c91 | 2006-07-19 01:40:55 -0400 | [diff] [blame] | 136 | error = input_ff_create_memless(input_dev, tmff, hid_tmff_play); |
| 137 | if (error) { |
| 138 | kfree(tmff); |
| 139 | return error; |
| 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>"); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |