diff options
author | Linus Torvalds <torvalds@g5.osdl.org> | 2006-10-02 11:20:33 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-10-02 11:20:33 -0400 |
commit | a12f66fccf2e266ad197df142b5ebafc6a169a8c (patch) | |
tree | 9d0bc76f8aa9c42fb44ce5f5bf6b4b09f4efafed /drivers/usb | |
parent | 12dce6263d43daeb4e16fa4eb964c1c99fa4fa2e (diff) | |
parent | bb0885900de49b5822d7e8c91c1adf9a0fcc228b (diff) |
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input: (35 commits)
Input: wistron - add support for Acer TravelMate 2424NWXCi
Input: wistron - fix setting up special buttons
Input: add KEY_BLUETOOTH and KEY_WLAN definitions
Input: add new BUS_VIRTUAL bus type
Input: add driver for stowaway serial keyboards
Input: make input_register_handler() return error codes
Input: remove cruft that was needed for transition to sysfs
Input: fix input module refcounting
Input: constify input core
Input: libps2 - rearrange exports
Input: atkbd - support Microsoft Natural Elite Pro keyboards
Input: i8042 - disable MUX mode on Toshiba Equium A110
Input: i8042 - get rid of polling timer
Input: send key up events at disconnect
Input: constify psmouse driver
Input: i8042 - add Amoi to the MUX blacklist
Input: logips2pp - add sugnature 56 (Cordless MouseMan Wheel), cleanup
Input: add driver for Touchwin serial touchscreens
Input: add driver for Touchright serial touchscreens
Input: add driver for Penmount serial touchscreens
...
Diffstat (limited to 'drivers/usb')
-rw-r--r-- | drivers/usb/input/Kconfig | 18 | ||||
-rw-r--r-- | drivers/usb/input/Makefile | 5 | ||||
-rw-r--r-- | drivers/usb/input/fixp-arith.h | 87 | ||||
-rw-r--r-- | drivers/usb/input/hid-core.c | 5 | ||||
-rw-r--r-- | drivers/usb/input/hid-ff.c | 49 | ||||
-rw-r--r-- | drivers/usb/input/hid-input.c | 23 | ||||
-rw-r--r-- | drivers/usb/input/hid-lgff.c | 523 | ||||
-rw-r--r-- | drivers/usb/input/hid-pidff.c | 1330 | ||||
-rw-r--r-- | drivers/usb/input/hid-tmff.c | 399 | ||||
-rw-r--r-- | drivers/usb/input/hid-zpff.c | 110 | ||||
-rw-r--r-- | drivers/usb/input/hid.h | 32 | ||||
-rw-r--r-- | drivers/usb/input/pid.c | 295 | ||||
-rw-r--r-- | drivers/usb/input/pid.h | 62 |
13 files changed, 1606 insertions, 1332 deletions
diff --git a/drivers/usb/input/Kconfig b/drivers/usb/input/Kconfig index a102a58fe361..21cd22640080 100644 --- a/drivers/usb/input/Kconfig +++ b/drivers/usb/input/Kconfig | |||
@@ -60,16 +60,17 @@ config HID_FF | |||
60 | If unsure, say N. | 60 | If unsure, say N. |
61 | 61 | ||
62 | config HID_PID | 62 | config HID_PID |
63 | bool "PID Devices (Microsoft Sidewinder Force Feedback 2)" | 63 | bool "PID device support" |
64 | depends on HID_FF | 64 | depends on HID_FF |
65 | help | 65 | help |
66 | Say Y here if you have a PID-compliant joystick and wish to enable force | 66 | Say Y here if you have a PID-compliant device and wish to enable force |
67 | feedback for it. The Microsoft Sidewinder Force Feedback 2 is one such | 67 | feedback for it. Microsoft Sidewinder Force Feedback 2 is one of such |
68 | device. | 68 | devices. |
69 | 69 | ||
70 | config LOGITECH_FF | 70 | config LOGITECH_FF |
71 | bool "Logitech WingMan *3D support" | 71 | bool "Logitech WingMan *3D support" |
72 | depends on HID_FF | 72 | depends on HID_FF |
73 | select INPUT_FF_MEMLESS if USB_HID | ||
73 | help | 74 | help |
74 | Say Y here if you have one of these devices: | 75 | Say Y here if you have one of these devices: |
75 | - Logitech WingMan Cordless RumblePad | 76 | - Logitech WingMan Cordless RumblePad |
@@ -81,12 +82,21 @@ config LOGITECH_FF | |||
81 | config THRUSTMASTER_FF | 82 | config THRUSTMASTER_FF |
82 | bool "ThrustMaster FireStorm Dual Power 2 support (EXPERIMENTAL)" | 83 | bool "ThrustMaster FireStorm Dual Power 2 support (EXPERIMENTAL)" |
83 | depends on HID_FF && EXPERIMENTAL | 84 | depends on HID_FF && EXPERIMENTAL |
85 | select INPUT_FF_MEMLESS if USB_HID | ||
84 | help | 86 | help |
85 | Say Y here if you have a THRUSTMASTER FireStore Dual Power 2, | 87 | Say Y here if you have a THRUSTMASTER FireStore Dual Power 2, |
86 | and want to enable force feedback support for it. | 88 | and want to enable force feedback support for it. |
87 | Note: if you say N here, this device will still be supported, but without | 89 | Note: if you say N here, this device will still be supported, but without |
88 | force feedback. | 90 | force feedback. |
89 | 91 | ||
92 | config ZEROPLUS_FF | ||
93 | bool "Zeroplus based game controller support" | ||
94 | depends on HID_FF | ||
95 | select INPUT_FF_MEMLESS if USB_HID | ||
96 | help | ||
97 | Say Y here if you have a Zeroplus based game controller and want to | ||
98 | enable force feedback for it. | ||
99 | |||
90 | config USB_HIDDEV | 100 | config USB_HIDDEV |
91 | bool "/dev/hiddev raw HID device support" | 101 | bool "/dev/hiddev raw HID device support" |
92 | depends on USB_HID | 102 | depends on USB_HID |
diff --git a/drivers/usb/input/Makefile b/drivers/usb/input/Makefile index 48551be324ac..295f459d1079 100644 --- a/drivers/usb/input/Makefile +++ b/drivers/usb/input/Makefile | |||
@@ -15,7 +15,7 @@ ifeq ($(CONFIG_USB_HIDINPUT),y) | |||
15 | usbhid-objs += hid-input.o | 15 | usbhid-objs += hid-input.o |
16 | endif | 16 | endif |
17 | ifeq ($(CONFIG_HID_PID),y) | 17 | ifeq ($(CONFIG_HID_PID),y) |
18 | usbhid-objs += pid.o | 18 | usbhid-objs += hid-pidff.o |
19 | endif | 19 | endif |
20 | ifeq ($(CONFIG_LOGITECH_FF),y) | 20 | ifeq ($(CONFIG_LOGITECH_FF),y) |
21 | usbhid-objs += hid-lgff.o | 21 | usbhid-objs += hid-lgff.o |
@@ -23,6 +23,9 @@ endif | |||
23 | ifeq ($(CONFIG_THRUSTMASTER_FF),y) | 23 | ifeq ($(CONFIG_THRUSTMASTER_FF),y) |
24 | usbhid-objs += hid-tmff.o | 24 | usbhid-objs += hid-tmff.o |
25 | endif | 25 | endif |
26 | ifeq ($(CONFIG_ZEROPLUS_FF),y) | ||
27 | usbhid-objs += hid-zpff.o | ||
28 | endif | ||
26 | ifeq ($(CONFIG_HID_FF),y) | 29 | ifeq ($(CONFIG_HID_FF),y) |
27 | usbhid-objs += hid-ff.o | 30 | usbhid-objs += hid-ff.o |
28 | endif | 31 | endif |
diff --git a/drivers/usb/input/fixp-arith.h b/drivers/usb/input/fixp-arith.h deleted file mode 100644 index ed3d2da0c485..000000000000 --- a/drivers/usb/input/fixp-arith.h +++ /dev/null | |||
@@ -1,87 +0,0 @@ | |||
1 | #ifndef _FIXP_ARITH_H | ||
2 | #define _FIXP_ARITH_H | ||
3 | |||
4 | /* | ||
5 | * Simplistic fixed-point arithmetics. | ||
6 | * Hmm, I'm probably duplicating some code :( | ||
7 | * | ||
8 | * Copyright (c) 2002 Johann Deneux | ||
9 | */ | ||
10 | |||
11 | /* | ||
12 | * This program is free software; you can redistribute it and/or modify | ||
13 | * it under the terms of the GNU General Public License as published by | ||
14 | * the Free Software Foundation; either version 2 of the License, or | ||
15 | * (at your option) any later version. | ||
16 | * | ||
17 | * This program is distributed in the hope that it will be useful, | ||
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
20 | * GNU General Public License for more details. | ||
21 | * | ||
22 | * You should have received a copy of the GNU General Public License | ||
23 | * along with this program; if not, write to the Free Software | ||
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
25 | * | ||
26 | * Should you need to contact me, the author, you can do so by | ||
27 | * e-mail - mail your message to <deneux@ifrance.com> | ||
28 | */ | ||
29 | |||
30 | #include <linux/types.h> | ||
31 | |||
32 | /* The type representing fixed-point values */ | ||
33 | typedef s16 fixp_t; | ||
34 | |||
35 | #define FRAC_N 8 | ||
36 | #define FRAC_MASK ((1<<FRAC_N)-1) | ||
37 | |||
38 | /* Not to be used directly. Use fixp_{cos,sin} */ | ||
39 | static const fixp_t cos_table[46] = { | ||
40 | 0x0100, 0x00FF, 0x00FF, 0x00FE, 0x00FD, 0x00FC, 0x00FA, 0x00F8, | ||
41 | 0x00F6, 0x00F3, 0x00F0, 0x00ED, 0x00E9, 0x00E6, 0x00E2, 0x00DD, | ||
42 | 0x00D9, 0x00D4, 0x00CF, 0x00C9, 0x00C4, 0x00BE, 0x00B8, 0x00B1, | ||
43 | 0x00AB, 0x00A4, 0x009D, 0x0096, 0x008F, 0x0087, 0x0080, 0x0078, | ||
44 | 0x0070, 0x0068, 0x005F, 0x0057, 0x004F, 0x0046, 0x003D, 0x0035, | ||
45 | 0x002C, 0x0023, 0x001A, 0x0011, 0x0008, 0x0000 | ||
46 | }; | ||
47 | |||
48 | |||
49 | /* a: 123 -> 123.0 */ | ||
50 | static inline fixp_t fixp_new(s16 a) | ||
51 | { | ||
52 | return a<<FRAC_N; | ||
53 | } | ||
54 | |||
55 | /* a: 0xFFFF -> -1.0 | ||
56 | 0x8000 -> 1.0 | ||
57 | 0x0000 -> 0.0 | ||
58 | */ | ||
59 | static inline fixp_t fixp_new16(s16 a) | ||
60 | { | ||
61 | return ((s32)a)>>(16-FRAC_N); | ||
62 | } | ||
63 | |||
64 | static inline fixp_t fixp_cos(unsigned int degrees) | ||
65 | { | ||
66 | int quadrant = (degrees / 90) & 3; | ||
67 | unsigned int i = degrees % 90; | ||
68 | |||
69 | if (quadrant == 1 || quadrant == 3) | ||
70 | i = 90 - i; | ||
71 | |||
72 | i >>= 1; | ||
73 | |||
74 | return (quadrant == 1 || quadrant == 2)? -cos_table[i] : cos_table[i]; | ||
75 | } | ||
76 | |||
77 | static inline fixp_t fixp_sin(unsigned int degrees) | ||
78 | { | ||
79 | return -fixp_cos(degrees + 90); | ||
80 | } | ||
81 | |||
82 | static inline fixp_t fixp_mult(fixp_t a, fixp_t b) | ||
83 | { | ||
84 | return ((s32)(a*b))>>FRAC_N; | ||
85 | } | ||
86 | |||
87 | #endif | ||
diff --git a/drivers/usb/input/hid-core.c b/drivers/usb/input/hid-core.c index 81b1ea01a172..e0fd11605b43 100644 --- a/drivers/usb/input/hid-core.c +++ b/drivers/usb/input/hid-core.c | |||
@@ -543,8 +543,6 @@ static void hid_free_device(struct hid_device *device) | |||
543 | { | 543 | { |
544 | unsigned i,j; | 544 | unsigned i,j; |
545 | 545 | ||
546 | hid_ff_exit(device); | ||
547 | |||
548 | for (i = 0; i < HID_REPORT_TYPES; i++) { | 546 | for (i = 0; i < HID_REPORT_TYPES; i++) { |
549 | struct hid_report_enum *report_enum = device->report_enum + i; | 547 | struct hid_report_enum *report_enum = device->report_enum + i; |
550 | 548 | ||
@@ -1109,7 +1107,7 @@ int hid_set_field(struct hid_field *field, unsigned offset, __s32 value) | |||
1109 | /* | 1107 | /* |
1110 | * Find a report field with a specified HID usage. | 1108 | * Find a report field with a specified HID usage. |
1111 | */ | 1109 | */ |
1112 | 1110 | #if 0 | |
1113 | struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type) | 1111 | struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type) |
1114 | { | 1112 | { |
1115 | struct hid_report *report; | 1113 | struct hid_report *report; |
@@ -1121,6 +1119,7 @@ struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_u | |||
1121 | return report->field[i]; | 1119 | return report->field[i]; |
1122 | return NULL; | 1120 | return NULL; |
1123 | } | 1121 | } |
1122 | #endif /* 0 */ | ||
1124 | 1123 | ||
1125 | static int hid_submit_out(struct hid_device *hid) | 1124 | static int hid_submit_out(struct hid_device *hid) |
1126 | { | 1125 | { |
diff --git a/drivers/usb/input/hid-ff.c b/drivers/usb/input/hid-ff.c index d5c91ee67991..a8fc46c721c5 100644 --- a/drivers/usb/input/hid-ff.c +++ b/drivers/usb/input/hid-ff.c | |||
@@ -44,45 +44,38 @@ struct hid_ff_initializer { | |||
44 | int (*init)(struct hid_device*); | 44 | int (*init)(struct hid_device*); |
45 | }; | 45 | }; |
46 | 46 | ||
47 | /* | ||
48 | * We try pidff when no other driver is found because PID is the | ||
49 | * standards compliant way of implementing force feedback in HID. | ||
50 | * pidff_init() will quickly abort if the device doesn't appear to | ||
51 | * be a PID device | ||
52 | */ | ||
47 | static struct hid_ff_initializer inits[] = { | 53 | static struct hid_ff_initializer inits[] = { |
48 | #ifdef CONFIG_LOGITECH_FF | 54 | #ifdef CONFIG_LOGITECH_FF |
49 | {0x46d, 0xc211, hid_lgff_init}, // Logitech Cordless rumble pad | 55 | { 0x46d, 0xc211, hid_lgff_init }, /* Logitech Cordless rumble pad */ |
50 | {0x46d, 0xc283, hid_lgff_init}, // Logitech Wingman Force 3d | 56 | { 0x46d, 0xc283, hid_lgff_init }, /* Logitech Wingman Force 3d */ |
51 | {0x46d, 0xc295, hid_lgff_init}, // Logitech MOMO force wheel | 57 | { 0x46d, 0xc295, hid_lgff_init }, /* Logitech MOMO force wheel */ |
52 | {0x46d, 0xc219, hid_lgff_init}, // Logitech Cordless rumble pad 2 | 58 | { 0x46d, 0xc219, hid_lgff_init }, /* Logitech Cordless rumble pad 2 */ |
53 | #endif | ||
54 | #ifdef CONFIG_HID_PID | ||
55 | {0x45e, 0x001b, hid_pid_init}, | ||
56 | #endif | 59 | #endif |
57 | #ifdef CONFIG_THRUSTMASTER_FF | 60 | #ifdef CONFIG_THRUSTMASTER_FF |
58 | {0x44f, 0xb304, hid_tmff_init}, | 61 | { 0x44f, 0xb304, hid_tmff_init }, |
59 | #endif | 62 | #endif |
60 | {0, 0, NULL} /* Terminating entry */ | 63 | #ifdef CONFIG_ZEROPLUS_FF |
64 | { 0xc12, 0x0005, hid_zpff_init }, | ||
65 | { 0xc12, 0x0030, hid_zpff_init }, | ||
66 | #endif | ||
67 | { 0, 0, hid_pidff_init} /* Matches anything */ | ||
61 | }; | 68 | }; |
62 | 69 | ||
63 | static struct hid_ff_initializer *hid_get_ff_init(__u16 idVendor, | ||
64 | __u16 idProduct) | ||
65 | { | ||
66 | struct hid_ff_initializer *init; | ||
67 | for (init = inits; | ||
68 | init->idVendor | ||
69 | && !(init->idVendor == idVendor | ||
70 | && init->idProduct == idProduct); | ||
71 | init++); | ||
72 | |||
73 | return init->idVendor? init : NULL; | ||
74 | } | ||
75 | |||
76 | int hid_ff_init(struct hid_device* hid) | 70 | int hid_ff_init(struct hid_device* hid) |
77 | { | 71 | { |
78 | struct hid_ff_initializer *init; | 72 | struct hid_ff_initializer *init; |
73 | int vendor = le16_to_cpu(hid->dev->descriptor.idVendor); | ||
74 | int product = le16_to_cpu(hid->dev->descriptor.idProduct); | ||
79 | 75 | ||
80 | init = hid_get_ff_init(le16_to_cpu(hid->dev->descriptor.idVendor), | 76 | for (init = inits; init->idVendor; init++) |
81 | le16_to_cpu(hid->dev->descriptor.idProduct)); | 77 | if (init->idVendor == vendor && init->idProduct == product) |
78 | break; | ||
82 | 79 | ||
83 | if (!init) { | ||
84 | dbg("hid_ff_init could not find initializer"); | ||
85 | return -ENOSYS; | ||
86 | } | ||
87 | return init->init(hid); | 80 | return init->init(hid); |
88 | } | 81 | } |
diff --git a/drivers/usb/input/hid-input.c b/drivers/usb/input/hid-input.c index 7208839f2dbf..4c62afbeb430 100644 --- a/drivers/usb/input/hid-input.c +++ b/drivers/usb/input/hid-input.c | |||
@@ -65,11 +65,9 @@ static const struct { | |||
65 | #define map_rel(c) do { usage->code = c; usage->type = EV_REL; bit = input->relbit; max = REL_MAX; } while (0) | 65 | #define map_rel(c) do { usage->code = c; usage->type = EV_REL; bit = input->relbit; max = REL_MAX; } while (0) |
66 | #define map_key(c) do { usage->code = c; usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX; } while (0) | 66 | #define map_key(c) do { usage->code = c; usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX; } while (0) |
67 | #define map_led(c) do { usage->code = c; usage->type = EV_LED; bit = input->ledbit; max = LED_MAX; } while (0) | 67 | #define map_led(c) do { usage->code = c; usage->type = EV_LED; bit = input->ledbit; max = LED_MAX; } while (0) |
68 | #define map_ff(c) do { usage->code = c; usage->type = EV_FF; bit = input->ffbit; max = FF_MAX; } while (0) | ||
69 | 68 | ||
70 | #define map_abs_clear(c) do { map_abs(c); clear_bit(c, bit); } while (0) | 69 | #define map_abs_clear(c) do { map_abs(c); clear_bit(c, bit); } while (0) |
71 | #define map_key_clear(c) do { map_key(c); clear_bit(c, bit); } while (0) | 70 | #define map_key_clear(c) do { map_key(c); clear_bit(c, bit); } while (0) |
72 | #define map_ff_effect(c) do { set_bit(c, input->ffbit); } while (0) | ||
73 | 71 | ||
74 | #ifdef CONFIG_USB_HIDINPUT_POWERBOOK | 72 | #ifdef CONFIG_USB_HIDINPUT_POWERBOOK |
75 | 73 | ||
@@ -525,23 +523,7 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel | |||
525 | 523 | ||
526 | case HID_UP_PID: | 524 | case HID_UP_PID: |
527 | 525 | ||
528 | set_bit(EV_FF, input->evbit); | ||
529 | switch(usage->hid & HID_USAGE) { | 526 | switch(usage->hid & HID_USAGE) { |
530 | case 0x26: map_ff_effect(FF_CONSTANT); goto ignore; | ||
531 | case 0x27: map_ff_effect(FF_RAMP); goto ignore; | ||
532 | case 0x28: map_ff_effect(FF_CUSTOM); goto ignore; | ||
533 | case 0x30: map_ff_effect(FF_SQUARE); map_ff_effect(FF_PERIODIC); goto ignore; | ||
534 | case 0x31: map_ff_effect(FF_SINE); map_ff_effect(FF_PERIODIC); goto ignore; | ||
535 | case 0x32: map_ff_effect(FF_TRIANGLE); map_ff_effect(FF_PERIODIC); goto ignore; | ||
536 | case 0x33: map_ff_effect(FF_SAW_UP); map_ff_effect(FF_PERIODIC); goto ignore; | ||
537 | case 0x34: map_ff_effect(FF_SAW_DOWN); map_ff_effect(FF_PERIODIC); goto ignore; | ||
538 | case 0x40: map_ff_effect(FF_SPRING); goto ignore; | ||
539 | case 0x41: map_ff_effect(FF_DAMPER); goto ignore; | ||
540 | case 0x42: map_ff_effect(FF_INERTIA); goto ignore; | ||
541 | case 0x43: map_ff_effect(FF_FRICTION); goto ignore; | ||
542 | case 0x7e: map_ff(FF_GAIN); break; | ||
543 | case 0x83: input->ff_effects_max = field->value[0]; goto ignore; | ||
544 | case 0x98: map_ff(FF_AUTOCENTER); break; | ||
545 | case 0xa4: map_key_clear(BTN_DEAD); break; | 527 | case 0xa4: map_key_clear(BTN_DEAD); break; |
546 | default: goto ignore; | 528 | default: goto ignore; |
547 | } | 529 | } |
@@ -698,8 +680,7 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct | |||
698 | } | 680 | } |
699 | 681 | ||
700 | if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */ | 682 | if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */ |
701 | input->ff_effects_max = value; | 683 | dbg("Maximum Effects - %d",value); |
702 | dbg("Maximum Effects - %d",input->ff_effects_max); | ||
703 | return; | 684 | return; |
704 | } | 685 | } |
705 | 686 | ||
@@ -748,7 +729,7 @@ static int hidinput_input_event(struct input_dev *dev, unsigned int type, unsign | |||
748 | int offset; | 729 | int offset; |
749 | 730 | ||
750 | if (type == EV_FF) | 731 | if (type == EV_FF) |
751 | return hid_ff_event(hid, dev, type, code, value); | 732 | return input_ff_event(dev, type, code, value); |
752 | 733 | ||
753 | if (type != EV_LED) | 734 | if (type != EV_LED) |
754 | return -1; | 735 | return -1; |
diff --git a/drivers/usb/input/hid-lgff.c b/drivers/usb/input/hid-lgff.c index f07d44357ff1..93da222b6da8 100644 --- a/drivers/usb/input/hid-lgff.c +++ b/drivers/usb/input/hid-lgff.c | |||
@@ -1,12 +1,11 @@ | |||
1 | /* | 1 | /* |
2 | * $$ | ||
3 | * | ||
4 | * Force feedback support for hid-compliant for some of the devices from | 2 | * Force feedback support for hid-compliant for some of the devices from |
5 | * Logitech, namely: | 3 | * Logitech, namely: |
6 | * - WingMan Cordless RumblePad | 4 | * - WingMan Cordless RumblePad |
7 | * - WingMan Force 3D | 5 | * - WingMan Force 3D |
8 | * | 6 | * |
9 | * Copyright (c) 2002-2004 Johann Deneux | 7 | * Copyright (c) 2002-2004 Johann Deneux |
8 | * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com> | ||
10 | */ | 9 | */ |
11 | 10 | ||
12 | /* | 11 | /* |
@@ -29,495 +28,117 @@ | |||
29 | */ | 28 | */ |
30 | 29 | ||
31 | #include <linux/input.h> | 30 | #include <linux/input.h> |
32 | #include <linux/sched.h> | ||
33 | |||
34 | //#define DEBUG | ||
35 | #include <linux/usb.h> | 31 | #include <linux/usb.h> |
36 | |||
37 | #include <linux/circ_buf.h> | ||
38 | |||
39 | #include "hid.h" | 32 | #include "hid.h" |
40 | #include "fixp-arith.h" | ||
41 | |||
42 | |||
43 | /* Periodicity of the update */ | ||
44 | #define PERIOD (HZ/10) | ||
45 | |||
46 | #define RUN_AT(t) (jiffies + (t)) | ||
47 | |||
48 | /* Effect status */ | ||
49 | #define EFFECT_STARTED 0 /* Effect is going to play after some time | ||
50 | (ff_replay.delay) */ | ||
51 | #define EFFECT_PLAYING 1 /* Effect is being played */ | ||
52 | #define EFFECT_USED 2 | ||
53 | |||
54 | // For lgff_device::flags | ||
55 | #define DEVICE_CLOSING 0 /* The driver is being unitialised */ | ||
56 | |||
57 | /* Check that the current process can access an effect */ | ||
58 | #define CHECK_OWNERSHIP(effect) (current->pid == 0 \ | ||
59 | || effect.owner == current->pid) | ||
60 | |||
61 | #define LGFF_CHECK_OWNERSHIP(i, l) \ | ||
62 | (i>=0 && i<LGFF_EFFECTS \ | ||
63 | && test_bit(EFFECT_USED, l->effects[i].flags) \ | ||
64 | && CHECK_OWNERSHIP(l->effects[i])) | ||
65 | |||
66 | #define LGFF_EFFECTS 8 | ||
67 | 33 | ||
68 | struct device_type { | 34 | struct device_type { |
69 | u16 idVendor; | 35 | u16 idVendor; |
70 | u16 idProduct; | 36 | u16 idProduct; |
71 | signed short *ff; | 37 | const signed short *ff; |
72 | }; | ||
73 | |||
74 | struct lgff_effect { | ||
75 | pid_t owner; | ||
76 | |||
77 | struct ff_effect effect; | ||
78 | |||
79 | unsigned long flags[1]; | ||
80 | unsigned int count; /* Number of times left to play */ | ||
81 | unsigned long started_at; /* When the effect started to play */ | ||
82 | }; | ||
83 | |||
84 | struct lgff_device { | ||
85 | struct hid_device* hid; | ||
86 | |||
87 | struct hid_report* constant; | ||
88 | struct hid_report* rumble; | ||
89 | struct hid_report* condition; | ||
90 | |||
91 | struct lgff_effect effects[LGFF_EFFECTS]; | ||
92 | spinlock_t lock; /* device-level lock. Having locks on | ||
93 | a per-effect basis could be nice, but | ||
94 | isn't really necessary */ | ||
95 | |||
96 | unsigned long flags[1]; /* Contains various information about the | ||
97 | state of the driver for this device */ | ||
98 | |||
99 | struct timer_list timer; | ||
100 | }; | 38 | }; |
101 | 39 | ||
102 | /* Callbacks */ | 40 | static const signed short ff_rumble[] = { |
103 | static void hid_lgff_exit(struct hid_device* hid); | ||
104 | static int hid_lgff_event(struct hid_device *hid, struct input_dev *input, | ||
105 | unsigned int type, unsigned int code, int value); | ||
106 | static int hid_lgff_flush(struct input_dev *input, struct file *file); | ||
107 | static int hid_lgff_upload_effect(struct input_dev *input, | ||
108 | struct ff_effect *effect); | ||
109 | static int hid_lgff_erase(struct input_dev *input, int id); | ||
110 | |||
111 | /* Local functions */ | ||
112 | static void hid_lgff_input_init(struct hid_device* hid); | ||
113 | static void hid_lgff_timer(unsigned long timer_data); | ||
114 | static struct hid_report* hid_lgff_duplicate_report(struct hid_report*); | ||
115 | static void hid_lgff_delete_report(struct hid_report*); | ||
116 | |||
117 | static signed short ff_rumble[] = { | ||
118 | FF_RUMBLE, | 41 | FF_RUMBLE, |
119 | -1 | 42 | -1 |
120 | }; | 43 | }; |
121 | 44 | ||
122 | static signed short ff_joystick[] = { | 45 | static const signed short ff_joystick[] = { |
123 | FF_CONSTANT, | 46 | FF_CONSTANT, |
124 | -1 | 47 | -1 |
125 | }; | 48 | }; |
126 | 49 | ||
127 | static struct device_type devices[] = { | 50 | static const struct device_type devices[] = { |
128 | {0x046d, 0xc211, ff_rumble}, | 51 | { 0x046d, 0xc211, ff_rumble }, |
129 | {0x046d, 0xc219, ff_rumble}, | 52 | { 0x046d, 0xc219, ff_rumble }, |
130 | {0x046d, 0xc283, ff_joystick}, | 53 | { 0x046d, 0xc283, ff_joystick }, |
131 | {0x0000, 0x0000, ff_joystick} | 54 | { 0x0000, 0x0000, ff_joystick } |
132 | }; | 55 | }; |
133 | 56 | ||
57 | static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect) | ||
58 | { | ||
59 | struct hid_device *hid = dev->private; | ||
60 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | ||
61 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | ||
62 | int x, y; | ||
63 | unsigned int left, right; | ||
64 | |||
65 | #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff | ||
66 | |||
67 | switch (effect->type) { | ||
68 | case FF_CONSTANT: | ||
69 | x = effect->u.ramp.start_level + 0x7f; /* 0x7f is center */ | ||
70 | y = effect->u.ramp.end_level + 0x7f; | ||
71 | CLAMP(x); | ||
72 | CLAMP(y); | ||
73 | report->field[0]->value[0] = 0x51; | ||
74 | report->field[0]->value[1] = 0x08; | ||
75 | report->field[0]->value[2] = x; | ||
76 | report->field[0]->value[3] = y; | ||
77 | dbg("(x, y)=(%04x, %04x)", x, y); | ||
78 | hid_submit_report(hid, report, USB_DIR_OUT); | ||
79 | break; | ||
80 | |||
81 | case FF_RUMBLE: | ||
82 | right = effect->u.rumble.strong_magnitude; | ||
83 | left = effect->u.rumble.weak_magnitude; | ||
84 | right = right * 0xff / 0xffff; | ||
85 | left = left * 0xff / 0xffff; | ||
86 | CLAMP(left); | ||
87 | CLAMP(right); | ||
88 | report->field[0]->value[0] = 0x42; | ||
89 | report->field[0]->value[1] = 0x00; | ||
90 | report->field[0]->value[2] = left; | ||
91 | report->field[0]->value[3] = right; | ||
92 | dbg("(left, right)=(%04x, %04x)", left, right); | ||
93 | hid_submit_report(hid, report, USB_DIR_OUT); | ||
94 | break; | ||
95 | } | ||
96 | return 0; | ||
97 | } | ||
98 | |||
134 | int hid_lgff_init(struct hid_device* hid) | 99 | int hid_lgff_init(struct hid_device* hid) |
135 | { | 100 | { |
136 | struct lgff_device *private; | 101 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
137 | struct hid_report* report; | 102 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
138 | struct hid_field* field; | 103 | struct input_dev *dev = hidinput->input; |
104 | struct hid_report *report; | ||
105 | struct hid_field *field; | ||
106 | int error; | ||
107 | int i, j; | ||
139 | 108 | ||
140 | /* Find the report to use */ | 109 | /* Find the report to use */ |
141 | if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) { | 110 | if (list_empty(report_list)) { |
142 | err("No output report found"); | 111 | err("No output report found"); |
143 | return -1; | 112 | return -1; |
144 | } | 113 | } |
114 | |||
145 | /* Check that the report looks ok */ | 115 | /* Check that the report looks ok */ |
146 | report = (struct hid_report*)hid->report_enum[HID_OUTPUT_REPORT].report_list.next; | 116 | report = list_entry(report_list->next, struct hid_report, list); |
147 | if (!report) { | 117 | if (!report) { |
148 | err("NULL output report"); | 118 | err("NULL output report"); |
149 | return -1; | 119 | return -1; |
150 | } | 120 | } |
121 | |||
151 | field = report->field[0]; | 122 | field = report->field[0]; |
152 | if (!field) { | 123 | if (!field) { |
153 | err("NULL field"); | 124 | err("NULL field"); |
154 | return -1; | 125 | return -1; |
155 | } | 126 | } |
156 | 127 | ||
157 | private = kzalloc(sizeof(struct lgff_device), GFP_KERNEL); | 128 | for (i = 0; i < ARRAY_SIZE(devices); i++) { |
158 | if (!private) | 129 | if (dev->id.vendor == devices[i].idVendor && |
159 | return -1; | 130 | dev->id.product == devices[i].idProduct) { |
160 | hid->ff_private = private; | 131 | for (j = 0; devices[i].ff[j] >= 0; j++) |
161 | 132 | set_bit(devices[i].ff[j], dev->ffbit); | |
162 | /* Input init */ | 133 | break; |
163 | hid_lgff_input_init(hid); | ||
164 | |||
165 | |||
166 | private->constant = hid_lgff_duplicate_report(report); | ||
167 | if (!private->constant) { | ||
168 | kfree(private); | ||
169 | return -1; | ||
170 | } | ||
171 | private->constant->field[0]->value[0] = 0x51; | ||
172 | private->constant->field[0]->value[1] = 0x08; | ||
173 | private->constant->field[0]->value[2] = 0x7f; | ||
174 | private->constant->field[0]->value[3] = 0x7f; | ||
175 | |||
176 | private->rumble = hid_lgff_duplicate_report(report); | ||
177 | if (!private->rumble) { | ||
178 | hid_lgff_delete_report(private->constant); | ||
179 | kfree(private); | ||
180 | return -1; | ||
181 | } | ||
182 | private->rumble->field[0]->value[0] = 0x42; | ||
183 | |||
184 | |||
185 | private->condition = hid_lgff_duplicate_report(report); | ||
186 | if (!private->condition) { | ||
187 | hid_lgff_delete_report(private->rumble); | ||
188 | hid_lgff_delete_report(private->constant); | ||
189 | kfree(private); | ||
190 | return -1; | ||
191 | } | ||
192 | |||
193 | private->hid = hid; | ||
194 | |||
195 | spin_lock_init(&private->lock); | ||
196 | init_timer(&private->timer); | ||
197 | private->timer.data = (unsigned long)private; | ||
198 | private->timer.function = hid_lgff_timer; | ||
199 | |||
200 | /* Event and exit callbacks */ | ||
201 | hid->ff_exit = hid_lgff_exit; | ||
202 | hid->ff_event = hid_lgff_event; | ||
203 | |||
204 | /* Start the update task */ | ||
205 | private->timer.expires = RUN_AT(PERIOD); | ||
206 | add_timer(&private->timer); /*TODO: only run the timer when at least | ||
207 | one effect is playing */ | ||
208 | |||
209 | printk(KERN_INFO "Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n"); | ||
210 | |||
211 | return 0; | ||
212 | } | ||
213 | |||
214 | static struct hid_report* hid_lgff_duplicate_report(struct hid_report* report) | ||
215 | { | ||
216 | struct hid_report* ret; | ||
217 | |||
218 | ret = kmalloc(sizeof(struct lgff_device), GFP_KERNEL); | ||
219 | if (!ret) | ||
220 | return NULL; | ||
221 | *ret = *report; | ||
222 | |||
223 | ret->field[0] = kmalloc(sizeof(struct hid_field), GFP_KERNEL); | ||
224 | if (!ret->field[0]) { | ||
225 | kfree(ret); | ||
226 | return NULL; | ||
227 | } | ||
228 | *ret->field[0] = *report->field[0]; | ||
229 | |||
230 | ret->field[0]->value = kzalloc(sizeof(s32[8]), GFP_KERNEL); | ||
231 | if (!ret->field[0]->value) { | ||
232 | kfree(ret->field[0]); | ||
233 | kfree(ret); | ||
234 | return NULL; | ||
235 | } | ||
236 | |||
237 | return ret; | ||
238 | } | ||
239 | |||
240 | static void hid_lgff_delete_report(struct hid_report* report) | ||
241 | { | ||
242 | if (report) { | ||
243 | kfree(report->field[0]->value); | ||
244 | kfree(report->field[0]); | ||
245 | kfree(report); | ||
246 | } | ||
247 | } | ||
248 | |||
249 | static void hid_lgff_input_init(struct hid_device* hid) | ||
250 | { | ||
251 | struct device_type* dev = devices; | ||
252 | signed short* ff; | ||
253 | u16 idVendor = le16_to_cpu(hid->dev->descriptor.idVendor); | ||
254 | u16 idProduct = le16_to_cpu(hid->dev->descriptor.idProduct); | ||
255 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | ||
256 | struct input_dev *input_dev = hidinput->input; | ||
257 | |||
258 | while (dev->idVendor && (idVendor != dev->idVendor || idProduct != dev->idProduct)) | ||
259 | dev++; | ||
260 | |||
261 | for (ff = dev->ff; *ff >= 0; ff++) | ||
262 | set_bit(*ff, input_dev->ffbit); | ||
263 | |||
264 | input_dev->upload_effect = hid_lgff_upload_effect; | ||
265 | input_dev->flush = hid_lgff_flush; | ||
266 | |||
267 | set_bit(EV_FF, input_dev->evbit); | ||
268 | input_dev->ff_effects_max = LGFF_EFFECTS; | ||
269 | } | ||
270 | |||
271 | static void hid_lgff_exit(struct hid_device* hid) | ||
272 | { | ||
273 | struct lgff_device *lgff = hid->ff_private; | ||
274 | |||
275 | set_bit(DEVICE_CLOSING, lgff->flags); | ||
276 | del_timer_sync(&lgff->timer); | ||
277 | |||
278 | hid_lgff_delete_report(lgff->condition); | ||
279 | hid_lgff_delete_report(lgff->rumble); | ||
280 | hid_lgff_delete_report(lgff->constant); | ||
281 | |||
282 | kfree(lgff); | ||
283 | } | ||
284 | |||
285 | static int hid_lgff_event(struct hid_device *hid, struct input_dev* input, | ||
286 | unsigned int type, unsigned int code, int value) | ||
287 | { | ||
288 | struct lgff_device *lgff = hid->ff_private; | ||
289 | struct lgff_effect *effect = lgff->effects + code; | ||
290 | unsigned long flags; | ||
291 | |||
292 | if (type != EV_FF) return -EINVAL; | ||
293 | if (!LGFF_CHECK_OWNERSHIP(code, lgff)) return -EACCES; | ||
294 | if (value < 0) return -EINVAL; | ||
295 | |||
296 | spin_lock_irqsave(&lgff->lock, flags); | ||
297 | |||
298 | if (value > 0) { | ||
299 | if (test_bit(EFFECT_STARTED, effect->flags)) { | ||
300 | spin_unlock_irqrestore(&lgff->lock, flags); | ||
301 | return -EBUSY; | ||
302 | } | ||
303 | if (test_bit(EFFECT_PLAYING, effect->flags)) { | ||
304 | spin_unlock_irqrestore(&lgff->lock, flags); | ||
305 | return -EBUSY; | ||
306 | } | ||
307 | |||
308 | effect->count = value; | ||
309 | |||
310 | if (effect->effect.replay.delay) { | ||
311 | set_bit(EFFECT_STARTED, effect->flags); | ||
312 | } else { | ||
313 | set_bit(EFFECT_PLAYING, effect->flags); | ||
314 | } | ||
315 | effect->started_at = jiffies; | ||
316 | } | ||
317 | else { /* value == 0 */ | ||
318 | clear_bit(EFFECT_STARTED, effect->flags); | ||
319 | clear_bit(EFFECT_PLAYING, effect->flags); | ||
320 | } | ||
321 | |||
322 | spin_unlock_irqrestore(&lgff->lock, flags); | ||
323 | |||
324 | return 0; | ||
325 | |||
326 | } | ||
327 | |||
328 | /* Erase all effects this process owns */ | ||
329 | static int hid_lgff_flush(struct input_dev *dev, struct file *file) | ||
330 | { | ||
331 | struct hid_device *hid = dev->private; | ||
332 | struct lgff_device *lgff = hid->ff_private; | ||
333 | int i; | ||
334 | |||
335 | for (i=0; i<dev->ff_effects_max; ++i) { | ||
336 | |||
337 | /*NOTE: no need to lock here. The only times EFFECT_USED is | ||
338 | modified is when effects are uploaded or when an effect is | ||
339 | erased. But a process cannot close its dev/input/eventX fd | ||
340 | and perform ioctls on the same fd all at the same time */ | ||
341 | if ( current->pid == lgff->effects[i].owner | ||
342 | && test_bit(EFFECT_USED, lgff->effects[i].flags)) { | ||
343 | |||
344 | if (hid_lgff_erase(dev, i)) | ||
345 | warn("erase effect %d failed", i); | ||
346 | } | ||
347 | |||
348 | } | ||
349 | |||
350 | return 0; | ||
351 | } | ||
352 | |||
353 | static int hid_lgff_erase(struct input_dev *dev, int id) | ||
354 | { | ||
355 | struct hid_device *hid = dev->private; | ||
356 | struct lgff_device *lgff = hid->ff_private; | ||
357 | unsigned long flags; | ||
358 | |||
359 | if (!LGFF_CHECK_OWNERSHIP(id, lgff)) return -EACCES; | ||
360 | |||
361 | spin_lock_irqsave(&lgff->lock, flags); | ||
362 | lgff->effects[id].flags[0] = 0; | ||
363 | spin_unlock_irqrestore(&lgff->lock, flags); | ||
364 | |||
365 | return 0; | ||
366 | } | ||
367 | |||
368 | static int hid_lgff_upload_effect(struct input_dev* input, | ||
369 | struct ff_effect* effect) | ||
370 | { | ||
371 | struct hid_device *hid = input->private; | ||
372 | struct lgff_device *lgff = hid->ff_private; | ||
373 | struct lgff_effect new; | ||
374 | int id; | ||
375 | unsigned long flags; | ||
376 | |||
377 | dbg("ioctl rumble"); | ||
378 | |||
379 | if (!test_bit(effect->type, input->ffbit)) return -EINVAL; | ||
380 | |||
381 | spin_lock_irqsave(&lgff->lock, flags); | ||
382 | |||
383 | if (effect->id == -1) { | ||
384 | int i; | ||
385 | |||
386 | for (i=0; i<LGFF_EFFECTS && test_bit(EFFECT_USED, lgff->effects[i].flags); ++i); | ||
387 | if (i >= LGFF_EFFECTS) { | ||
388 | spin_unlock_irqrestore(&lgff->lock, flags); | ||
389 | return -ENOSPC; | ||
390 | } | 134 | } |
391 | |||
392 | effect->id = i; | ||
393 | lgff->effects[i].owner = current->pid; | ||
394 | lgff->effects[i].flags[0] = 0; | ||
395 | set_bit(EFFECT_USED, lgff->effects[i].flags); | ||
396 | } | 135 | } |
397 | else if (!LGFF_CHECK_OWNERSHIP(effect->id, lgff)) { | ||
398 | spin_unlock_irqrestore(&lgff->lock, flags); | ||
399 | return -EACCES; | ||
400 | } | ||
401 | |||
402 | id = effect->id; | ||
403 | new = lgff->effects[id]; | ||
404 | |||
405 | new.effect = *effect; | ||
406 | |||
407 | if (test_bit(EFFECT_STARTED, lgff->effects[id].flags) | ||
408 | || test_bit(EFFECT_STARTED, lgff->effects[id].flags)) { | ||
409 | |||
410 | /* Changing replay parameters is not allowed (for the time | ||
411 | being) */ | ||
412 | if (new.effect.replay.delay != lgff->effects[id].effect.replay.delay | ||
413 | || new.effect.replay.length != lgff->effects[id].effect.replay.length) { | ||
414 | spin_unlock_irqrestore(&lgff->lock, flags); | ||
415 | return -ENOSYS; | ||
416 | } | ||
417 | 136 | ||
418 | lgff->effects[id] = new; | 137 | error = input_ff_create_memless(dev, NULL, hid_lgff_play); |
138 | if (error) | ||
139 | return error; | ||
419 | 140 | ||
420 | } else { | 141 | printk(KERN_INFO "Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n"); |
421 | lgff->effects[id] = new; | ||
422 | } | ||
423 | 142 | ||
424 | spin_unlock_irqrestore(&lgff->lock, flags); | ||
425 | return 0; | 143 | return 0; |
426 | } | 144 | } |
427 | |||
428 | static void hid_lgff_timer(unsigned long timer_data) | ||
429 | { | ||
430 | struct lgff_device *lgff = (struct lgff_device*)timer_data; | ||
431 | struct hid_device *hid = lgff->hid; | ||
432 | unsigned long flags; | ||
433 | int x = 0x7f, y = 0x7f; // Coordinates of constant effects | ||
434 | unsigned int left = 0, right = 0; // Rumbling | ||
435 | int i; | ||
436 | |||
437 | spin_lock_irqsave(&lgff->lock, flags); | ||
438 | |||
439 | for (i=0; i<LGFF_EFFECTS; ++i) { | ||
440 | struct lgff_effect* effect = lgff->effects +i; | ||
441 | |||
442 | if (test_bit(EFFECT_PLAYING, effect->flags)) { | ||
443 | |||
444 | switch (effect->effect.type) { | ||
445 | case FF_CONSTANT: { | ||
446 | //TODO: handle envelopes | ||
447 | int degrees = effect->effect.direction * 360 >> 16; | ||
448 | x += fixp_mult(fixp_sin(degrees), | ||
449 | fixp_new16(effect->effect.u.constant.level)); | ||
450 | y += fixp_mult(-fixp_cos(degrees), | ||
451 | fixp_new16(effect->effect.u.constant.level)); | ||
452 | } break; | ||
453 | case FF_RUMBLE: | ||
454 | right += effect->effect.u.rumble.strong_magnitude; | ||
455 | left += effect->effect.u.rumble.weak_magnitude; | ||
456 | break; | ||
457 | }; | ||
458 | |||
459 | /* One run of the effect is finished playing */ | ||
460 | if (time_after(jiffies, | ||
461 | effect->started_at | ||
462 | + effect->effect.replay.delay*HZ/1000 | ||
463 | + effect->effect.replay.length*HZ/1000)) { | ||
464 | dbg("Finished playing once %d", i); | ||
465 | if (--effect->count <= 0) { | ||
466 | dbg("Stopped %d", i); | ||
467 | clear_bit(EFFECT_PLAYING, effect->flags); | ||
468 | } | ||
469 | else { | ||
470 | dbg("Start again %d", i); | ||
471 | if (effect->effect.replay.length != 0) { | ||
472 | clear_bit(EFFECT_PLAYING, effect->flags); | ||
473 | set_bit(EFFECT_STARTED, effect->flags); | ||
474 | } | ||
475 | effect->started_at = jiffies; | ||
476 | } | ||
477 | } | ||
478 | |||
479 | } else if (test_bit(EFFECT_STARTED, lgff->effects[i].flags)) { | ||
480 | /* Check if we should start playing the effect */ | ||
481 | if (time_after(jiffies, | ||
482 | lgff->effects[i].started_at | ||
483 | + lgff->effects[i].effect.replay.delay*HZ/1000)) { | ||
484 | dbg("Now playing %d", i); | ||
485 | clear_bit(EFFECT_STARTED, lgff->effects[i].flags); | ||
486 | set_bit(EFFECT_PLAYING, lgff->effects[i].flags); | ||
487 | } | ||
488 | } | ||
489 | } | ||
490 | |||
491 | #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff | ||
492 | |||
493 | // Clamp values | ||
494 | CLAMP(x); | ||
495 | CLAMP(y); | ||
496 | CLAMP(left); | ||
497 | CLAMP(right); | ||
498 | |||
499 | #undef CLAMP | ||
500 | |||
501 | if (x != lgff->constant->field[0]->value[2] | ||
502 | || y != lgff->constant->field[0]->value[3]) { | ||
503 | lgff->constant->field[0]->value[2] = x; | ||
504 | lgff->constant->field[0]->value[3] = y; | ||
505 | dbg("(x,y)=(%04x, %04x)", x, y); | ||
506 | hid_submit_report(hid, lgff->constant, USB_DIR_OUT); | ||
507 | } | ||
508 | |||
509 | if (left != lgff->rumble->field[0]->value[2] | ||
510 | || right != lgff->rumble->field[0]->value[3]) { | ||
511 | lgff->rumble->field[0]->value[2] = left; | ||
512 | lgff->rumble->field[0]->value[3] = right; | ||
513 | dbg("(left,right)=(%04x, %04x)", left, right); | ||
514 | hid_submit_report(hid, lgff->rumble, USB_DIR_OUT); | ||
515 | } | ||
516 | |||
517 | if (!test_bit(DEVICE_CLOSING, lgff->flags)) { | ||
518 | lgff->timer.expires = RUN_AT(PERIOD); | ||
519 | add_timer(&lgff->timer); | ||
520 | } | ||
521 | |||
522 | spin_unlock_irqrestore(&lgff->lock, flags); | ||
523 | } | ||
diff --git a/drivers/usb/input/hid-pidff.c b/drivers/usb/input/hid-pidff.c new file mode 100644 index 000000000000..5420c13eb8eb --- /dev/null +++ b/drivers/usb/input/hid-pidff.c | |||
@@ -0,0 +1,1330 @@ | |||
1 | /* | ||
2 | * Force feedback driver for USB HID PID compliant devices | ||
3 | * | ||
4 | * Copyright (c) 2005, 2006 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 | /* #define DEBUG */ | ||
24 | |||
25 | #define debug(format, arg...) pr_debug("hid-pidff: " format "\n" , ## arg) | ||
26 | |||
27 | #include <linux/sched.h> | ||
28 | #include <linux/input.h> | ||
29 | #include <linux/usb.h> | ||
30 | |||
31 | #include "hid.h" | ||
32 | |||
33 | #define PID_EFFECTS_MAX 64 | ||
34 | |||
35 | /* Report usage table used to put reports into an array */ | ||
36 | |||
37 | #define PID_SET_EFFECT 0 | ||
38 | #define PID_EFFECT_OPERATION 1 | ||
39 | #define PID_DEVICE_GAIN 2 | ||
40 | #define PID_POOL 3 | ||
41 | #define PID_BLOCK_LOAD 4 | ||
42 | #define PID_BLOCK_FREE 5 | ||
43 | #define PID_DEVICE_CONTROL 6 | ||
44 | #define PID_CREATE_NEW_EFFECT 7 | ||
45 | |||
46 | #define PID_REQUIRED_REPORTS 7 | ||
47 | |||
48 | #define PID_SET_ENVELOPE 8 | ||
49 | #define PID_SET_CONDITION 9 | ||
50 | #define PID_SET_PERIODIC 10 | ||
51 | #define PID_SET_CONSTANT 11 | ||
52 | #define PID_SET_RAMP 12 | ||
53 | static const u8 pidff_reports[] = { | ||
54 | 0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab, | ||
55 | 0x5a, 0x5f, 0x6e, 0x73, 0x74 | ||
56 | }; | ||
57 | |||
58 | /* device_control is really 0x95, but 0x96 specified as it is the usage of | ||
59 | the only field in that report */ | ||
60 | |||
61 | /* Value usage tables used to put fields and values into arrays */ | ||
62 | |||
63 | #define PID_EFFECT_BLOCK_INDEX 0 | ||
64 | |||
65 | #define PID_DURATION 1 | ||
66 | #define PID_GAIN 2 | ||
67 | #define PID_TRIGGER_BUTTON 3 | ||
68 | #define PID_TRIGGER_REPEAT_INT 4 | ||
69 | #define PID_DIRECTION_ENABLE 5 | ||
70 | #define PID_START_DELAY 6 | ||
71 | static const u8 pidff_set_effect[] = { | ||
72 | 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7 | ||
73 | }; | ||
74 | |||
75 | #define PID_ATTACK_LEVEL 1 | ||
76 | #define PID_ATTACK_TIME 2 | ||
77 | #define PID_FADE_LEVEL 3 | ||
78 | #define PID_FADE_TIME 4 | ||
79 | static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e }; | ||
80 | |||
81 | #define PID_PARAM_BLOCK_OFFSET 1 | ||
82 | #define PID_CP_OFFSET 2 | ||
83 | #define PID_POS_COEFFICIENT 3 | ||
84 | #define PID_NEG_COEFFICIENT 4 | ||
85 | #define PID_POS_SATURATION 5 | ||
86 | #define PID_NEG_SATURATION 6 | ||
87 | #define PID_DEAD_BAND 7 | ||
88 | static const u8 pidff_set_condition[] = { | ||
89 | 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65 | ||
90 | }; | ||
91 | |||
92 | #define PID_MAGNITUDE 1 | ||
93 | #define PID_OFFSET 2 | ||
94 | #define PID_PHASE 3 | ||
95 | #define PID_PERIOD 4 | ||
96 | static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 }; | ||
97 | static const u8 pidff_set_constant[] = { 0x22, 0x70 }; | ||
98 | |||
99 | #define PID_RAMP_START 1 | ||
100 | #define PID_RAMP_END 2 | ||
101 | static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 }; | ||
102 | |||
103 | #define PID_RAM_POOL_AVAILABLE 1 | ||
104 | static const u8 pidff_block_load[] = { 0x22, 0xac }; | ||
105 | |||
106 | #define PID_LOOP_COUNT 1 | ||
107 | static const u8 pidff_effect_operation[] = { 0x22, 0x7c }; | ||
108 | |||
109 | static const u8 pidff_block_free[] = { 0x22 }; | ||
110 | |||
111 | #define PID_DEVICE_GAIN_FIELD 0 | ||
112 | static const u8 pidff_device_gain[] = { 0x7e }; | ||
113 | |||
114 | #define PID_RAM_POOL_SIZE 0 | ||
115 | #define PID_SIMULTANEOUS_MAX 1 | ||
116 | #define PID_DEVICE_MANAGED_POOL 2 | ||
117 | static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 }; | ||
118 | |||
119 | /* Special field key tables used to put special field keys into arrays */ | ||
120 | |||
121 | #define PID_ENABLE_ACTUATORS 0 | ||
122 | #define PID_RESET 1 | ||
123 | static const u8 pidff_device_control[] = { 0x97, 0x9a }; | ||
124 | |||
125 | #define PID_CONSTANT 0 | ||
126 | #define PID_RAMP 1 | ||
127 | #define PID_SQUARE 2 | ||
128 | #define PID_SINE 3 | ||
129 | #define PID_TRIANGLE 4 | ||
130 | #define PID_SAW_UP 5 | ||
131 | #define PID_SAW_DOWN 6 | ||
132 | #define PID_SPRING 7 | ||
133 | #define PID_DAMPER 8 | ||
134 | #define PID_INERTIA 9 | ||
135 | #define PID_FRICTION 10 | ||
136 | static const u8 pidff_effect_types[] = { | ||
137 | 0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34, | ||
138 | 0x40, 0x41, 0x42, 0x43 | ||
139 | }; | ||
140 | |||
141 | #define PID_BLOCK_LOAD_SUCCESS 0 | ||
142 | #define PID_BLOCK_LOAD_FULL 1 | ||
143 | static const u8 pidff_block_load_status[] = { 0x8c, 0x8d }; | ||
144 | |||
145 | #define PID_EFFECT_START 0 | ||
146 | #define PID_EFFECT_STOP 1 | ||
147 | static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b }; | ||
148 | |||
149 | struct pidff_usage { | ||
150 | struct hid_field *field; | ||
151 | s32 *value; | ||
152 | }; | ||
153 | |||
154 | struct pidff_device { | ||
155 | struct hid_device *hid; | ||
156 | |||
157 | struct hid_report *reports[sizeof(pidff_reports)]; | ||
158 | |||
159 | struct pidff_usage set_effect[sizeof(pidff_set_effect)]; | ||
160 | struct pidff_usage set_envelope[sizeof(pidff_set_envelope)]; | ||
161 | struct pidff_usage set_condition[sizeof(pidff_set_condition)]; | ||
162 | struct pidff_usage set_periodic[sizeof(pidff_set_periodic)]; | ||
163 | struct pidff_usage set_constant[sizeof(pidff_set_constant)]; | ||
164 | struct pidff_usage set_ramp[sizeof(pidff_set_ramp)]; | ||
165 | |||
166 | struct pidff_usage device_gain[sizeof(pidff_device_gain)]; | ||
167 | struct pidff_usage block_load[sizeof(pidff_block_load)]; | ||
168 | struct pidff_usage pool[sizeof(pidff_pool)]; | ||
169 | struct pidff_usage effect_operation[sizeof(pidff_effect_operation)]; | ||
170 | struct pidff_usage block_free[sizeof(pidff_block_free)]; | ||
171 | |||
172 | /* Special field is a field that is not composed of | ||
173 | usage<->value pairs that pidff_usage values are */ | ||
174 | |||
175 | /* Special field in create_new_effect */ | ||
176 | struct hid_field *create_new_effect_type; | ||
177 | |||
178 | /* Special fields in set_effect */ | ||
179 | struct hid_field *set_effect_type; | ||
180 | struct hid_field *effect_direction; | ||
181 | |||
182 | /* Special field in device_control */ | ||
183 | struct hid_field *device_control; | ||
184 | |||
185 | /* Special field in block_load */ | ||
186 | struct hid_field *block_load_status; | ||
187 | |||
188 | /* Special field in effect_operation */ | ||
189 | struct hid_field *effect_operation_status; | ||
190 | |||
191 | int control_id[sizeof(pidff_device_control)]; | ||
192 | int type_id[sizeof(pidff_effect_types)]; | ||
193 | int status_id[sizeof(pidff_block_load_status)]; | ||
194 | int operation_id[sizeof(pidff_effect_operation_status)]; | ||
195 | |||
196 | int pid_id[PID_EFFECTS_MAX]; | ||
197 | }; | ||
198 | |||
199 | /* | ||
200 | * Scale an unsigned value with range 0..max for the given field | ||
201 | */ | ||
202 | static int pidff_rescale(int i, int max, struct hid_field *field) | ||
203 | { | ||
204 | return i * (field->logical_maximum - field->logical_minimum) / max + | ||
205 | field->logical_minimum; | ||
206 | } | ||
207 | |||
208 | /* | ||
209 | * Scale a signed value in range -0x8000..0x7fff for the given field | ||
210 | */ | ||
211 | static int pidff_rescale_signed(int i, struct hid_field *field) | ||
212 | { | ||
213 | return i == 0 ? 0 : i > | ||
214 | 0 ? i * field->logical_maximum / 0x7fff : i * | ||
215 | field->logical_minimum / -0x8000; | ||
216 | } | ||
217 | |||
218 | static void pidff_set(struct pidff_usage *usage, u16 value) | ||
219 | { | ||
220 | usage->value[0] = pidff_rescale(value, 0xffff, usage->field); | ||
221 | debug("calculated from %d to %d", value, usage->value[0]); | ||
222 | } | ||
223 | |||
224 | static void pidff_set_signed(struct pidff_usage *usage, s16 value) | ||
225 | { | ||
226 | if (usage->field->logical_minimum < 0) | ||
227 | usage->value[0] = pidff_rescale_signed(value, usage->field); | ||
228 | else { | ||
229 | if (value < 0) | ||
230 | usage->value[0] = | ||
231 | pidff_rescale(-value, 0x8000, usage->field); | ||
232 | else | ||
233 | usage->value[0] = | ||
234 | pidff_rescale(value, 0x7fff, usage->field); | ||
235 | } | ||
236 | debug("calculated from %d to %d", value, usage->value[0]); | ||
237 | } | ||
238 | |||
239 | /* | ||
240 | * Send envelope report to the device | ||
241 | */ | ||
242 | static void pidff_set_envelope_report(struct pidff_device *pidff, | ||
243 | struct ff_envelope *envelope) | ||
244 | { | ||
245 | pidff->set_envelope[PID_EFFECT_BLOCK_INDEX].value[0] = | ||
246 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; | ||
247 | |||
248 | pidff->set_envelope[PID_ATTACK_LEVEL].value[0] = | ||
249 | pidff_rescale(envelope->attack_level > | ||
250 | 0x7fff ? 0x7fff : envelope->attack_level, 0x7fff, | ||
251 | pidff->set_envelope[PID_ATTACK_LEVEL].field); | ||
252 | pidff->set_envelope[PID_FADE_LEVEL].value[0] = | ||
253 | pidff_rescale(envelope->fade_level > | ||
254 | 0x7fff ? 0x7fff : envelope->fade_level, 0x7fff, | ||
255 | pidff->set_envelope[PID_FADE_LEVEL].field); | ||
256 | |||
257 | pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length; | ||
258 | pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length; | ||
259 | |||
260 | debug("attack %u => %d", envelope->attack_level, | ||
261 | pidff->set_envelope[PID_ATTACK_LEVEL].value[0]); | ||
262 | |||
263 | hid_submit_report(pidff->hid, pidff->reports[PID_SET_ENVELOPE], | ||
264 | USB_DIR_OUT); | ||
265 | } | ||
266 | |||
267 | /* | ||
268 | * Test if the new envelope differs from old one | ||
269 | */ | ||
270 | static int pidff_needs_set_envelope(struct ff_envelope *envelope, | ||
271 | struct ff_envelope *old) | ||
272 | { | ||
273 | return envelope->attack_level != old->attack_level || | ||
274 | envelope->fade_level != old->fade_level || | ||
275 | envelope->attack_length != old->attack_length || | ||
276 | envelope->fade_length != old->fade_length; | ||
277 | } | ||
278 | |||
279 | /* | ||
280 | * Send constant force report to the device | ||
281 | */ | ||
282 | static void pidff_set_constant_force_report(struct pidff_device *pidff, | ||
283 | struct ff_effect *effect) | ||
284 | { | ||
285 | pidff->set_constant[PID_EFFECT_BLOCK_INDEX].value[0] = | ||
286 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; | ||
287 | pidff_set_signed(&pidff->set_constant[PID_MAGNITUDE], | ||
288 | effect->u.constant.level); | ||
289 | |||
290 | hid_submit_report(pidff->hid, pidff->reports[PID_SET_CONSTANT], | ||
291 | USB_DIR_OUT); | ||
292 | } | ||
293 | |||
294 | /* | ||
295 | * Test if the constant parameters have changed between effects | ||
296 | */ | ||
297 | static int pidff_needs_set_constant(struct ff_effect *effect, | ||
298 | struct ff_effect *old) | ||
299 | { | ||
300 | return effect->u.constant.level != old->u.constant.level; | ||
301 | } | ||
302 | |||
303 | /* | ||
304 | * Send set effect report to the device | ||
305 | */ | ||
306 | static void pidff_set_effect_report(struct pidff_device *pidff, | ||
307 | struct ff_effect *effect) | ||
308 | { | ||
309 | pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] = | ||
310 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; | ||
311 | pidff->set_effect_type->value[0] = | ||
312 | pidff->create_new_effect_type->value[0]; | ||
313 | pidff->set_effect[PID_DURATION].value[0] = effect->replay.length; | ||
314 | pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = effect->trigger.button; | ||
315 | pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = | ||
316 | effect->trigger.interval; | ||
317 | pidff->set_effect[PID_GAIN].value[0] = | ||
318 | pidff->set_effect[PID_GAIN].field->logical_maximum; | ||
319 | pidff->set_effect[PID_DIRECTION_ENABLE].value[0] = 1; | ||
320 | pidff->effect_direction->value[0] = | ||
321 | pidff_rescale(effect->direction, 0xffff, | ||
322 | pidff->effect_direction); | ||
323 | pidff->set_effect[PID_START_DELAY].value[0] = effect->replay.delay; | ||
324 | |||
325 | hid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT], | ||
326 | USB_DIR_OUT); | ||
327 | } | ||
328 | |||
329 | /* | ||
330 | * Test if the values used in set_effect have changed | ||
331 | */ | ||
332 | static int pidff_needs_set_effect(struct ff_effect *effect, | ||
333 | struct ff_effect *old) | ||
334 | { | ||
335 | return effect->replay.length != old->replay.length || | ||
336 | effect->trigger.interval != old->trigger.interval || | ||
337 | effect->trigger.button != old->trigger.button || | ||
338 | effect->direction != old->direction || | ||
339 | effect->replay.delay != old->replay.delay; | ||
340 | } | ||
341 | |||
342 | /* | ||
343 | * Send periodic effect report to the device | ||
344 | */ | ||
345 | static void pidff_set_periodic_report(struct pidff_device *pidff, | ||
346 | struct ff_effect *effect) | ||
347 | { | ||
348 | pidff->set_periodic[PID_EFFECT_BLOCK_INDEX].value[0] = | ||
349 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; | ||
350 | pidff_set_signed(&pidff->set_periodic[PID_MAGNITUDE], | ||
351 | effect->u.periodic.magnitude); | ||
352 | pidff_set_signed(&pidff->set_periodic[PID_OFFSET], | ||
353 | effect->u.periodic.offset); | ||
354 | pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase); | ||
355 | pidff->set_periodic[PID_PERIOD].value[0] = effect->u.periodic.period; | ||
356 | |||
357 | hid_submit_report(pidff->hid, pidff->reports[PID_SET_PERIODIC], | ||
358 | USB_DIR_OUT); | ||
359 | |||
360 | } | ||
361 | |||
362 | /* | ||
363 | * Test if periodic effect parameters have changed | ||
364 | */ | ||
365 | static int pidff_needs_set_periodic(struct ff_effect *effect, | ||
366 | struct ff_effect *old) | ||
367 | { | ||
368 | return effect->u.periodic.magnitude != old->u.periodic.magnitude || | ||
369 | effect->u.periodic.offset != old->u.periodic.offset || | ||
370 | effect->u.periodic.phase != old->u.periodic.phase || | ||
371 | effect->u.periodic.period != old->u.periodic.period; | ||
372 | } | ||
373 | |||
374 | /* | ||
375 | * Send condition effect reports to the device | ||
376 | */ | ||
377 | static void pidff_set_condition_report(struct pidff_device *pidff, | ||
378 | struct ff_effect *effect) | ||
379 | { | ||
380 | int i; | ||
381 | |||
382 | pidff->set_condition[PID_EFFECT_BLOCK_INDEX].value[0] = | ||
383 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; | ||
384 | |||
385 | for (i = 0; i < 2; i++) { | ||
386 | pidff->set_condition[PID_PARAM_BLOCK_OFFSET].value[0] = i; | ||
387 | pidff_set_signed(&pidff->set_condition[PID_CP_OFFSET], | ||
388 | effect->u.condition[i].center); | ||
389 | pidff_set_signed(&pidff->set_condition[PID_POS_COEFFICIENT], | ||
390 | effect->u.condition[i].right_coeff); | ||
391 | pidff_set_signed(&pidff->set_condition[PID_NEG_COEFFICIENT], | ||
392 | effect->u.condition[i].left_coeff); | ||
393 | pidff_set(&pidff->set_condition[PID_POS_SATURATION], | ||
394 | effect->u.condition[i].right_saturation); | ||
395 | pidff_set(&pidff->set_condition[PID_NEG_SATURATION], | ||
396 | effect->u.condition[i].left_saturation); | ||
397 | pidff_set(&pidff->set_condition[PID_DEAD_BAND], | ||
398 | effect->u.condition[i].deadband); | ||
399 | hid_wait_io(pidff->hid); | ||
400 | hid_submit_report(pidff->hid, pidff->reports[PID_SET_CONDITION], | ||
401 | USB_DIR_OUT); | ||
402 | } | ||
403 | } | ||
404 | |||
405 | /* | ||
406 | * Test if condition effect parameters have changed | ||
407 | */ | ||
408 | static int pidff_needs_set_condition(struct ff_effect *effect, | ||
409 | struct ff_effect *old) | ||
410 | { | ||
411 | int i; | ||
412 | int ret = 0; | ||
413 | |||
414 | for (i = 0; i < 2; i++) { | ||
415 | struct ff_condition_effect *cond = &effect->u.condition[i]; | ||
416 | struct ff_condition_effect *old_cond = &old->u.condition[i]; | ||
417 | |||
418 | ret |= cond->center != old_cond->center || | ||
419 | cond->right_coeff != old_cond->right_coeff || | ||
420 | cond->left_coeff != old_cond->left_coeff || | ||
421 | cond->right_saturation != old_cond->right_saturation || | ||
422 | cond->left_saturation != old_cond->left_saturation || | ||
423 | cond->deadband != old_cond->deadband; | ||
424 | } | ||
425 | |||
426 | return ret; | ||
427 | } | ||
428 | |||
429 | /* | ||
430 | * Send ramp force report to the device | ||
431 | */ | ||
432 | static void pidff_set_ramp_force_report(struct pidff_device *pidff, | ||
433 | struct ff_effect *effect) | ||
434 | { | ||
435 | pidff->set_ramp[PID_EFFECT_BLOCK_INDEX].value[0] = | ||
436 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; | ||
437 | pidff_set_signed(&pidff->set_ramp[PID_RAMP_START], | ||
438 | effect->u.ramp.start_level); | ||
439 | pidff_set_signed(&pidff->set_ramp[PID_RAMP_END], | ||
440 | effect->u.ramp.end_level); | ||
441 | hid_submit_report(pidff->hid, pidff->reports[PID_SET_RAMP], | ||
442 | USB_DIR_OUT); | ||
443 | } | ||
444 | |||
445 | /* | ||
446 | * Test if ramp force parameters have changed | ||
447 | */ | ||
448 | static int pidff_needs_set_ramp(struct ff_effect *effect, struct ff_effect *old) | ||
449 | { | ||
450 | return effect->u.ramp.start_level != old->u.ramp.start_level || | ||
451 | effect->u.ramp.end_level != old->u.ramp.end_level; | ||
452 | } | ||
453 | |||
454 | /* | ||
455 | * Send a request for effect upload to the device | ||
456 | * | ||
457 | * Returns 0 if device reported success, -ENOSPC if the device reported memory | ||
458 | * is full. Upon unknown response the function will retry for 60 times, if | ||
459 | * still unsuccessful -EIO is returned. | ||
460 | */ | ||
461 | static int pidff_request_effect_upload(struct pidff_device *pidff, int efnum) | ||
462 | { | ||
463 | int j; | ||
464 | |||
465 | pidff->create_new_effect_type->value[0] = efnum; | ||
466 | hid_submit_report(pidff->hid, pidff->reports[PID_CREATE_NEW_EFFECT], | ||
467 | USB_DIR_OUT); | ||
468 | debug("create_new_effect sent, type: %d", efnum); | ||
469 | |||
470 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] = 0; | ||
471 | pidff->block_load_status->value[0] = 0; | ||
472 | hid_wait_io(pidff->hid); | ||
473 | |||
474 | for (j = 0; j < 60; j++) { | ||
475 | debug("pid_block_load requested"); | ||
476 | hid_submit_report(pidff->hid, pidff->reports[PID_BLOCK_LOAD], | ||
477 | USB_DIR_IN); | ||
478 | hid_wait_io(pidff->hid); | ||
479 | if (pidff->block_load_status->value[0] == | ||
480 | pidff->status_id[PID_BLOCK_LOAD_SUCCESS]) { | ||
481 | debug("device reported free memory: %d bytes", | ||
482 | pidff->block_load[PID_RAM_POOL_AVAILABLE].value ? | ||
483 | pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1); | ||
484 | return 0; | ||
485 | } | ||
486 | if (pidff->block_load_status->value[0] == | ||
487 | pidff->status_id[PID_BLOCK_LOAD_FULL]) { | ||
488 | debug("not enough memory free: %d bytes", | ||
489 | pidff->block_load[PID_RAM_POOL_AVAILABLE].value ? | ||
490 | pidff->block_load[PID_RAM_POOL_AVAILABLE].value[0] : -1); | ||
491 | return -ENOSPC; | ||
492 | } | ||
493 | } | ||
494 | printk(KERN_ERR "hid-pidff: pid_block_load failed 60 times\n"); | ||
495 | return -EIO; | ||
496 | } | ||
497 | |||
498 | /* | ||
499 | * Play the effect with PID id n times | ||
500 | */ | ||
501 | static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n) | ||
502 | { | ||
503 | pidff->effect_operation[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id; | ||
504 | |||
505 | if (n == 0) { | ||
506 | pidff->effect_operation_status->value[0] = | ||
507 | pidff->operation_id[PID_EFFECT_STOP]; | ||
508 | } else { | ||
509 | pidff->effect_operation_status->value[0] = | ||
510 | pidff->operation_id[PID_EFFECT_START]; | ||
511 | pidff->effect_operation[PID_LOOP_COUNT].value[0] = n; | ||
512 | } | ||
513 | |||
514 | hid_wait_io(pidff->hid); | ||
515 | hid_submit_report(pidff->hid, pidff->reports[PID_EFFECT_OPERATION], | ||
516 | USB_DIR_OUT); | ||
517 | } | ||
518 | |||
519 | /** | ||
520 | * Play the effect with effect id @effect_id for @value times | ||
521 | */ | ||
522 | static int pidff_playback(struct input_dev *dev, int effect_id, int value) | ||
523 | { | ||
524 | struct pidff_device *pidff = dev->ff->private; | ||
525 | |||
526 | pidff_playback_pid(pidff, pidff->pid_id[effect_id], value); | ||
527 | |||
528 | return 0; | ||
529 | } | ||
530 | |||
531 | /* | ||
532 | * Erase effect with PID id | ||
533 | */ | ||
534 | static void pidff_erase_pid(struct pidff_device *pidff, int pid_id) | ||
535 | { | ||
536 | pidff->block_free[PID_EFFECT_BLOCK_INDEX].value[0] = pid_id; | ||
537 | hid_submit_report(pidff->hid, pidff->reports[PID_BLOCK_FREE], | ||
538 | USB_DIR_OUT); | ||
539 | } | ||
540 | |||
541 | /* | ||
542 | * Stop and erase effect with effect_id | ||
543 | */ | ||
544 | static int pidff_erase_effect(struct input_dev *dev, int effect_id) | ||
545 | { | ||
546 | struct pidff_device *pidff = dev->ff->private; | ||
547 | int pid_id = pidff->pid_id[effect_id]; | ||
548 | |||
549 | debug("starting to erase %d/%d", effect_id, pidff->pid_id[effect_id]); | ||
550 | pidff_playback_pid(pidff, pid_id, 0); | ||
551 | pidff_erase_pid(pidff, pid_id); | ||
552 | |||
553 | return 0; | ||
554 | } | ||
555 | |||
556 | /* | ||
557 | * Effect upload handler | ||
558 | */ | ||
559 | static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect, | ||
560 | struct ff_effect *old) | ||
561 | { | ||
562 | struct pidff_device *pidff = dev->ff->private; | ||
563 | int type_id; | ||
564 | int error; | ||
565 | |||
566 | switch (effect->type) { | ||
567 | case FF_CONSTANT: | ||
568 | if (!old) { | ||
569 | error = pidff_request_effect_upload(pidff, | ||
570 | pidff->type_id[PID_CONSTANT]); | ||
571 | if (error) | ||
572 | return error; | ||
573 | } | ||
574 | if (!old || pidff_needs_set_effect(effect, old)) | ||
575 | pidff_set_effect_report(pidff, effect); | ||
576 | if (!old || pidff_needs_set_constant(effect, old)) | ||
577 | pidff_set_constant_force_report(pidff, effect); | ||
578 | if (!old || | ||
579 | pidff_needs_set_envelope(&effect->u.constant.envelope, | ||
580 | &old->u.constant.envelope)) | ||
581 | pidff_set_envelope_report(pidff, | ||
582 | &effect->u.constant.envelope); | ||
583 | break; | ||
584 | |||
585 | case FF_PERIODIC: | ||
586 | if (!old) { | ||
587 | switch (effect->u.periodic.waveform) { | ||
588 | case FF_SQUARE: | ||
589 | type_id = PID_SQUARE; | ||
590 | break; | ||
591 | case FF_TRIANGLE: | ||
592 | type_id = PID_TRIANGLE; | ||
593 | break; | ||
594 | case FF_SINE: | ||
595 | type_id = PID_SINE; | ||
596 | break; | ||
597 | case FF_SAW_UP: | ||
598 | type_id = PID_SAW_UP; | ||
599 | break; | ||
600 | case FF_SAW_DOWN: | ||
601 | type_id = PID_SAW_DOWN; | ||
602 | break; | ||
603 | default: | ||
604 | printk(KERN_ERR | ||
605 | "hid-pidff: invalid waveform\n"); | ||
606 | return -EINVAL; | ||
607 | } | ||
608 | |||
609 | error = pidff_request_effect_upload(pidff, | ||
610 | pidff->type_id[type_id]); | ||
611 | if (error) | ||
612 | return error; | ||
613 | } | ||
614 | if (!old || pidff_needs_set_effect(effect, old)) | ||
615 | pidff_set_effect_report(pidff, effect); | ||
616 | if (!old || pidff_needs_set_periodic(effect, old)) | ||
617 | pidff_set_periodic_report(pidff, effect); | ||
618 | if (!old || | ||
619 | pidff_needs_set_envelope(&effect->u.periodic.envelope, | ||
620 | &old->u.periodic.envelope)) | ||
621 | pidff_set_envelope_report(pidff, | ||
622 | &effect->u.periodic.envelope); | ||
623 | break; | ||
624 | |||
625 | case FF_RAMP: | ||
626 | if (!old) { | ||
627 | error = pidff_request_effect_upload(pidff, | ||
628 | pidff->type_id[PID_RAMP]); | ||
629 | if (error) | ||
630 | return error; | ||
631 | } | ||
632 | if (!old || pidff_needs_set_effect(effect, old)) | ||
633 | pidff_set_effect_report(pidff, effect); | ||
634 | if (!old || pidff_needs_set_ramp(effect, old)) | ||
635 | pidff_set_ramp_force_report(pidff, effect); | ||
636 | if (!old || | ||
637 | pidff_needs_set_envelope(&effect->u.ramp.envelope, | ||
638 | &old->u.ramp.envelope)) | ||
639 | pidff_set_envelope_report(pidff, | ||
640 | &effect->u.ramp.envelope); | ||
641 | break; | ||
642 | |||
643 | case FF_SPRING: | ||
644 | if (!old) { | ||
645 | error = pidff_request_effect_upload(pidff, | ||
646 | pidff->type_id[PID_SPRING]); | ||
647 | if (error) | ||
648 | return error; | ||
649 | } | ||
650 | if (!old || pidff_needs_set_effect(effect, old)) | ||
651 | pidff_set_effect_report(pidff, effect); | ||
652 | if (!old || pidff_needs_set_condition(effect, old)) | ||
653 | pidff_set_condition_report(pidff, effect); | ||
654 | break; | ||
655 | |||
656 | case FF_FRICTION: | ||
657 | if (!old) { | ||
658 | error = pidff_request_effect_upload(pidff, | ||
659 | pidff->type_id[PID_FRICTION]); | ||
660 | if (error) | ||
661 | return error; | ||
662 | } | ||
663 | if (!old || pidff_needs_set_effect(effect, old)) | ||
664 | pidff_set_effect_report(pidff, effect); | ||
665 | if (!old || pidff_needs_set_condition(effect, old)) | ||
666 | pidff_set_condition_report(pidff, effect); | ||
667 | break; | ||
668 | |||
669 | case FF_DAMPER: | ||
670 | if (!old) { | ||
671 | error = pidff_request_effect_upload(pidff, | ||
672 | pidff->type_id[PID_DAMPER]); | ||
673 | if (error) | ||
674 | return error; | ||
675 | } | ||
676 | if (!old || pidff_needs_set_effect(effect, old)) | ||
677 | pidff_set_effect_report(pidff, effect); | ||
678 | if (!old || pidff_needs_set_condition(effect, old)) | ||
679 | pidff_set_condition_report(pidff, effect); | ||
680 | break; | ||
681 | |||
682 | case FF_INERTIA: | ||
683 | if (!old) { | ||
684 | error = pidff_request_effect_upload(pidff, | ||
685 | pidff->type_id[PID_INERTIA]); | ||
686 | if (error) | ||
687 | return error; | ||
688 | } | ||
689 | if (!old || pidff_needs_set_effect(effect, old)) | ||
690 | pidff_set_effect_report(pidff, effect); | ||
691 | if (!old || pidff_needs_set_condition(effect, old)) | ||
692 | pidff_set_condition_report(pidff, effect); | ||
693 | break; | ||
694 | |||
695 | default: | ||
696 | printk(KERN_ERR "hid-pidff: invalid type\n"); | ||
697 | return -EINVAL; | ||
698 | } | ||
699 | |||
700 | if (!old) | ||
701 | pidff->pid_id[effect->id] = | ||
702 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; | ||
703 | |||
704 | debug("uploaded"); | ||
705 | |||
706 | return 0; | ||
707 | } | ||
708 | |||
709 | /* | ||
710 | * set_gain() handler | ||
711 | */ | ||
712 | static void pidff_set_gain(struct input_dev *dev, u16 gain) | ||
713 | { | ||
714 | struct pidff_device *pidff = dev->ff->private; | ||
715 | |||
716 | pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], gain); | ||
717 | hid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN], | ||
718 | USB_DIR_OUT); | ||
719 | } | ||
720 | |||
721 | static void pidff_autocenter(struct pidff_device *pidff, u16 magnitude) | ||
722 | { | ||
723 | struct hid_field *field = | ||
724 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].field; | ||
725 | |||
726 | if (!magnitude) { | ||
727 | pidff_playback_pid(pidff, field->logical_minimum, 0); | ||
728 | return; | ||
729 | } | ||
730 | |||
731 | pidff_playback_pid(pidff, field->logical_minimum, 1); | ||
732 | |||
733 | pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] = | ||
734 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum; | ||
735 | pidff->set_effect_type->value[0] = pidff->type_id[PID_SPRING]; | ||
736 | pidff->set_effect[PID_DURATION].value[0] = 0; | ||
737 | pidff->set_effect[PID_TRIGGER_BUTTON].value[0] = 0; | ||
738 | pidff->set_effect[PID_TRIGGER_REPEAT_INT].value[0] = 0; | ||
739 | pidff_set(&pidff->set_effect[PID_GAIN], magnitude); | ||
740 | pidff->set_effect[PID_START_DELAY].value[0] = 0; | ||
741 | |||
742 | hid_submit_report(pidff->hid, pidff->reports[PID_SET_EFFECT], | ||
743 | USB_DIR_OUT); | ||
744 | } | ||
745 | |||
746 | /* | ||
747 | * pidff_set_autocenter() handler | ||
748 | */ | ||
749 | static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude) | ||
750 | { | ||
751 | struct pidff_device *pidff = dev->ff->private; | ||
752 | |||
753 | pidff_autocenter(pidff, magnitude); | ||
754 | } | ||
755 | |||
756 | /* | ||
757 | * Find fields from a report and fill a pidff_usage | ||
758 | */ | ||
759 | static int pidff_find_fields(struct pidff_usage *usage, const u8 *table, | ||
760 | struct hid_report *report, int count, int strict) | ||
761 | { | ||
762 | int i, j, k, found; | ||
763 | |||
764 | for (k = 0; k < count; k++) { | ||
765 | found = 0; | ||
766 | for (i = 0; i < report->maxfield; i++) { | ||
767 | if (report->field[i]->maxusage != | ||
768 | report->field[i]->report_count) { | ||
769 | debug("maxusage and report_count do not match, " | ||
770 | "skipping"); | ||
771 | continue; | ||
772 | } | ||
773 | for (j = 0; j < report->field[i]->maxusage; j++) { | ||
774 | if (report->field[i]->usage[j].hid == | ||
775 | (HID_UP_PID | table[k])) { | ||
776 | debug("found %d at %d->%d", k, i, j); | ||
777 | usage[k].field = report->field[i]; | ||
778 | usage[k].value = | ||
779 | &report->field[i]->value[j]; | ||
780 | found = 1; | ||
781 | break; | ||
782 | } | ||
783 | } | ||
784 | if (found) | ||
785 | break; | ||
786 | } | ||
787 | if (!found && strict) { | ||
788 | debug("failed to locate %d", k); | ||
789 | return -1; | ||
790 | } | ||
791 | } | ||
792 | return 0; | ||
793 | } | ||
794 | |||
795 | /* | ||
796 | * Return index into pidff_reports for the given usage | ||
797 | */ | ||
798 | static int pidff_check_usage(int usage) | ||
799 | { | ||
800 | int i; | ||
801 | |||
802 | for (i = 0; i < sizeof(pidff_reports); i++) | ||
803 | if (usage == (HID_UP_PID | pidff_reports[i])) | ||
804 | return i; | ||
805 | |||
806 | return -1; | ||
807 | } | ||
808 | |||
809 | /* | ||
810 | * Find the reports and fill pidff->reports[] | ||
811 | * report_type specifies either OUTPUT or FEATURE reports | ||
812 | */ | ||
813 | static void pidff_find_reports(struct hid_device *hid, int report_type, | ||
814 | struct pidff_device *pidff) | ||
815 | { | ||
816 | struct hid_report *report; | ||
817 | int i, ret; | ||
818 | |||
819 | list_for_each_entry(report, | ||
820 | &hid->report_enum[report_type].report_list, list) { | ||
821 | if (report->maxfield < 1) | ||
822 | continue; | ||
823 | ret = pidff_check_usage(report->field[0]->logical); | ||
824 | if (ret != -1) { | ||
825 | debug("found usage 0x%02x from field->logical", | ||
826 | pidff_reports[ret]); | ||
827 | pidff->reports[ret] = report; | ||
828 | continue; | ||
829 | } | ||
830 | |||
831 | /* | ||
832 | * Sometimes logical collections are stacked to indicate | ||
833 | * different usages for the report and the field, in which | ||
834 | * case we want the usage of the parent. However, Linux HID | ||
835 | * implementation hides this fact, so we have to dig it up | ||
836 | * ourselves | ||
837 | */ | ||
838 | i = report->field[0]->usage[0].collection_index; | ||
839 | if (i <= 0 || | ||
840 | hid->collection[i - 1].type != HID_COLLECTION_LOGICAL) | ||
841 | continue; | ||
842 | ret = pidff_check_usage(hid->collection[i - 1].usage); | ||
843 | if (ret != -1 && !pidff->reports[ret]) { | ||
844 | debug("found usage 0x%02x from collection array", | ||
845 | pidff_reports[ret]); | ||
846 | pidff->reports[ret] = report; | ||
847 | } | ||
848 | } | ||
849 | } | ||
850 | |||
851 | /* | ||
852 | * Test if the required reports have been found | ||
853 | */ | ||
854 | static int pidff_reports_ok(struct pidff_device *pidff) | ||
855 | { | ||
856 | int i; | ||
857 | |||
858 | for (i = 0; i <= PID_REQUIRED_REPORTS; i++) { | ||
859 | if (!pidff->reports[i]) { | ||
860 | debug("%d missing", i); | ||
861 | return 0; | ||
862 | } | ||
863 | } | ||
864 | |||
865 | return 1; | ||
866 | } | ||
867 | |||
868 | /* | ||
869 | * Find a field with a specific usage within a report | ||
870 | */ | ||
871 | static struct hid_field *pidff_find_special_field(struct hid_report *report, | ||
872 | int usage, int enforce_min) | ||
873 | { | ||
874 | int i; | ||
875 | |||
876 | for (i = 0; i < report->maxfield; i++) { | ||
877 | if (report->field[i]->logical == (HID_UP_PID | usage) && | ||
878 | report->field[i]->report_count > 0) { | ||
879 | if (!enforce_min || | ||
880 | report->field[i]->logical_minimum == 1) | ||
881 | return report->field[i]; | ||
882 | else { | ||
883 | printk(KERN_ERR "hid-pidff: logical_minimum " | ||
884 | "is not 1 as it should be\n"); | ||
885 | return NULL; | ||
886 | } | ||
887 | } | ||
888 | } | ||
889 | return NULL; | ||
890 | } | ||
891 | |||
892 | /* | ||
893 | * Fill a pidff->*_id struct table | ||
894 | */ | ||
895 | static int pidff_find_special_keys(int *keys, struct hid_field *fld, | ||
896 | const u8 *usagetable, int count) | ||
897 | { | ||
898 | |||
899 | int i, j; | ||
900 | int found = 0; | ||
901 | |||
902 | for (i = 0; i < count; i++) { | ||
903 | for (j = 0; j < fld->maxusage; j++) { | ||
904 | if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) { | ||
905 | keys[i] = j + 1; | ||
906 | found++; | ||
907 | break; | ||
908 | } | ||
909 | } | ||
910 | } | ||
911 | return found; | ||
912 | } | ||
913 | |||
914 | #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \ | ||
915 | pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \ | ||
916 | sizeof(pidff_ ## name)) | ||
917 | |||
918 | /* | ||
919 | * Find and check the special fields | ||
920 | */ | ||
921 | static int pidff_find_special_fields(struct pidff_device *pidff) | ||
922 | { | ||
923 | debug("finding special fields"); | ||
924 | |||
925 | pidff->create_new_effect_type = | ||
926 | pidff_find_special_field(pidff->reports[PID_CREATE_NEW_EFFECT], | ||
927 | 0x25, 1); | ||
928 | pidff->set_effect_type = | ||
929 | pidff_find_special_field(pidff->reports[PID_SET_EFFECT], | ||
930 | 0x25, 1); | ||
931 | pidff->effect_direction = | ||
932 | pidff_find_special_field(pidff->reports[PID_SET_EFFECT], | ||
933 | 0x57, 0); | ||
934 | pidff->device_control = | ||
935 | pidff_find_special_field(pidff->reports[PID_DEVICE_CONTROL], | ||
936 | 0x96, 1); | ||
937 | pidff->block_load_status = | ||
938 | pidff_find_special_field(pidff->reports[PID_BLOCK_LOAD], | ||
939 | 0x8b, 1); | ||
940 | pidff->effect_operation_status = | ||
941 | pidff_find_special_field(pidff->reports[PID_EFFECT_OPERATION], | ||
942 | 0x78, 1); | ||
943 | |||
944 | debug("search done"); | ||
945 | |||
946 | if (!pidff->create_new_effect_type || !pidff->set_effect_type) { | ||
947 | printk(KERN_ERR "hid-pidff: effect lists not found\n"); | ||
948 | return -1; | ||
949 | } | ||
950 | |||
951 | if (!pidff->effect_direction) { | ||
952 | printk(KERN_ERR "hid-pidff: direction field not found\n"); | ||
953 | return -1; | ||
954 | } | ||
955 | |||
956 | if (!pidff->device_control) { | ||
957 | printk(KERN_ERR "hid-pidff: device control field not found\n"); | ||
958 | return -1; | ||
959 | } | ||
960 | |||
961 | if (!pidff->block_load_status) { | ||
962 | printk(KERN_ERR | ||
963 | "hid-pidff: block load status field not found\n"); | ||
964 | return -1; | ||
965 | } | ||
966 | |||
967 | if (!pidff->effect_operation_status) { | ||
968 | printk(KERN_ERR | ||
969 | "hid-pidff: effect operation field not found\n"); | ||
970 | return -1; | ||
971 | } | ||
972 | |||
973 | pidff_find_special_keys(pidff->control_id, pidff->device_control, | ||
974 | pidff_device_control, | ||
975 | sizeof(pidff_device_control)); | ||
976 | |||
977 | PIDFF_FIND_SPECIAL_KEYS(control_id, device_control, device_control); | ||
978 | |||
979 | if (!PIDFF_FIND_SPECIAL_KEYS(type_id, create_new_effect_type, | ||
980 | effect_types)) { | ||
981 | printk(KERN_ERR "hid-pidff: no effect types found\n"); | ||
982 | return -1; | ||
983 | } | ||
984 | |||
985 | if (PIDFF_FIND_SPECIAL_KEYS(status_id, block_load_status, | ||
986 | block_load_status) != | ||
987 | sizeof(pidff_block_load_status)) { | ||
988 | printk(KERN_ERR | ||
989 | "hidpidff: block load status identifiers not found\n"); | ||
990 | return -1; | ||
991 | } | ||
992 | |||
993 | if (PIDFF_FIND_SPECIAL_KEYS(operation_id, effect_operation_status, | ||
994 | effect_operation_status) != | ||
995 | sizeof(pidff_effect_operation_status)) { | ||
996 | printk(KERN_ERR | ||
997 | "hidpidff: effect operation identifiers not found\n"); | ||
998 | return -1; | ||
999 | } | ||
1000 | |||
1001 | return 0; | ||
1002 | } | ||
1003 | |||
1004 | /** | ||
1005 | * Find the implemented effect types | ||
1006 | */ | ||
1007 | static int pidff_find_effects(struct pidff_device *pidff, | ||
1008 | struct input_dev *dev) | ||
1009 | { | ||
1010 | int i; | ||
1011 | |||
1012 | for (i = 0; i < sizeof(pidff_effect_types); i++) { | ||
1013 | int pidff_type = pidff->type_id[i]; | ||
1014 | if (pidff->set_effect_type->usage[pidff_type].hid != | ||
1015 | pidff->create_new_effect_type->usage[pidff_type].hid) { | ||
1016 | printk(KERN_ERR "hid-pidff: " | ||
1017 | "effect type number %d is invalid\n", i); | ||
1018 | return -1; | ||
1019 | } | ||
1020 | } | ||
1021 | |||
1022 | if (pidff->type_id[PID_CONSTANT]) | ||
1023 | set_bit(FF_CONSTANT, dev->ffbit); | ||
1024 | if (pidff->type_id[PID_RAMP]) | ||
1025 | set_bit(FF_RAMP, dev->ffbit); | ||
1026 | if (pidff->type_id[PID_SQUARE]) { | ||
1027 | set_bit(FF_SQUARE, dev->ffbit); | ||
1028 | set_bit(FF_PERIODIC, dev->ffbit); | ||
1029 | } | ||
1030 | if (pidff->type_id[PID_SINE]) { | ||
1031 | set_bit(FF_SINE, dev->ffbit); | ||
1032 | set_bit(FF_PERIODIC, dev->ffbit); | ||
1033 | } | ||
1034 | if (pidff->type_id[PID_TRIANGLE]) { | ||
1035 | set_bit(FF_TRIANGLE, dev->ffbit); | ||
1036 | set_bit(FF_PERIODIC, dev->ffbit); | ||
1037 | } | ||
1038 | if (pidff->type_id[PID_SAW_UP]) { | ||
1039 | set_bit(FF_SAW_UP, dev->ffbit); | ||
1040 | set_bit(FF_PERIODIC, dev->ffbit); | ||
1041 | } | ||
1042 | if (pidff->type_id[PID_SAW_DOWN]) { | ||
1043 | set_bit(FF_SAW_DOWN, dev->ffbit); | ||
1044 | set_bit(FF_PERIODIC, dev->ffbit); | ||
1045 | } | ||
1046 | if (pidff->type_id[PID_SPRING]) | ||
1047 | set_bit(FF_SPRING, dev->ffbit); | ||
1048 | if (pidff->type_id[PID_DAMPER]) | ||
1049 | set_bit(FF_DAMPER, dev->ffbit); | ||
1050 | if (pidff->type_id[PID_INERTIA]) | ||
1051 | set_bit(FF_INERTIA, dev->ffbit); | ||
1052 | if (pidff->type_id[PID_FRICTION]) | ||
1053 | set_bit(FF_FRICTION, dev->ffbit); | ||
1054 | |||
1055 | return 0; | ||
1056 | |||
1057 | } | ||
1058 | |||
1059 | #define PIDFF_FIND_FIELDS(name, report, strict) \ | ||
1060 | pidff_find_fields(pidff->name, pidff_ ## name, \ | ||
1061 | pidff->reports[report], \ | ||
1062 | sizeof(pidff_ ## name), strict) | ||
1063 | |||
1064 | /* | ||
1065 | * Fill and check the pidff_usages | ||
1066 | */ | ||
1067 | static int pidff_init_fields(struct pidff_device *pidff, struct input_dev *dev) | ||
1068 | { | ||
1069 | int envelope_ok = 0; | ||
1070 | |||
1071 | if (PIDFF_FIND_FIELDS(set_effect, PID_SET_EFFECT, 1)) { | ||
1072 | printk(KERN_ERR | ||
1073 | "hid-pidff: unknown set_effect report layout\n"); | ||
1074 | return -ENODEV; | ||
1075 | } | ||
1076 | |||
1077 | PIDFF_FIND_FIELDS(block_load, PID_BLOCK_LOAD, 0); | ||
1078 | if (!pidff->block_load[PID_EFFECT_BLOCK_INDEX].value) { | ||
1079 | printk(KERN_ERR | ||
1080 | "hid-pidff: unknown pid_block_load report layout\n"); | ||
1081 | return -ENODEV; | ||
1082 | } | ||
1083 | |||
1084 | if (PIDFF_FIND_FIELDS(effect_operation, PID_EFFECT_OPERATION, 1)) { | ||
1085 | printk(KERN_ERR | ||
1086 | "hid-pidff: unknown effect_operation report layout\n"); | ||
1087 | return -ENODEV; | ||
1088 | } | ||
1089 | |||
1090 | if (PIDFF_FIND_FIELDS(block_free, PID_BLOCK_FREE, 1)) { | ||
1091 | printk(KERN_ERR | ||
1092 | "hid-pidff: unknown pid_block_free report layout\n"); | ||
1093 | return -ENODEV; | ||
1094 | } | ||
1095 | |||
1096 | if (!PIDFF_FIND_FIELDS(set_envelope, PID_SET_ENVELOPE, 1)) | ||
1097 | envelope_ok = 1; | ||
1098 | |||
1099 | if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev)) | ||
1100 | return -ENODEV; | ||
1101 | |||
1102 | if (!envelope_ok) { | ||
1103 | if (test_and_clear_bit(FF_CONSTANT, dev->ffbit)) | ||
1104 | printk(KERN_WARNING "hid-pidff: " | ||
1105 | "has constant effect but no envelope\n"); | ||
1106 | if (test_and_clear_bit(FF_RAMP, dev->ffbit)) | ||
1107 | printk(KERN_WARNING "hid-pidff: " | ||
1108 | "has ramp effect but no envelope\n"); | ||
1109 | |||
1110 | if (test_and_clear_bit(FF_PERIODIC, dev->ffbit)) | ||
1111 | printk(KERN_WARNING "hid-pidff: " | ||
1112 | "has periodic effect but no envelope\n"); | ||
1113 | } | ||
1114 | |||
1115 | if (test_bit(FF_CONSTANT, dev->ffbit) && | ||
1116 | PIDFF_FIND_FIELDS(set_constant, PID_SET_CONSTANT, 1)) { | ||
1117 | printk(KERN_WARNING | ||
1118 | "hid-pidff: unknown constant effect layout\n"); | ||
1119 | clear_bit(FF_CONSTANT, dev->ffbit); | ||
1120 | } | ||
1121 | |||
1122 | if (test_bit(FF_RAMP, dev->ffbit) && | ||
1123 | PIDFF_FIND_FIELDS(set_ramp, PID_SET_RAMP, 1)) { | ||
1124 | printk(KERN_WARNING "hid-pidff: unknown ramp effect layout\n"); | ||
1125 | clear_bit(FF_RAMP, dev->ffbit); | ||
1126 | } | ||
1127 | |||
1128 | if ((test_bit(FF_SPRING, dev->ffbit) || | ||
1129 | test_bit(FF_DAMPER, dev->ffbit) || | ||
1130 | test_bit(FF_FRICTION, dev->ffbit) || | ||
1131 | test_bit(FF_INERTIA, dev->ffbit)) && | ||
1132 | PIDFF_FIND_FIELDS(set_condition, PID_SET_CONDITION, 1)) { | ||
1133 | printk(KERN_WARNING | ||
1134 | "hid-pidff: unknown condition effect layout\n"); | ||
1135 | clear_bit(FF_SPRING, dev->ffbit); | ||
1136 | clear_bit(FF_DAMPER, dev->ffbit); | ||
1137 | clear_bit(FF_FRICTION, dev->ffbit); | ||
1138 | clear_bit(FF_INERTIA, dev->ffbit); | ||
1139 | } | ||
1140 | |||
1141 | if (test_bit(FF_PERIODIC, dev->ffbit) && | ||
1142 | PIDFF_FIND_FIELDS(set_periodic, PID_SET_PERIODIC, 1)) { | ||
1143 | printk(KERN_WARNING | ||
1144 | "hid-pidff: unknown periodic effect layout\n"); | ||
1145 | clear_bit(FF_PERIODIC, dev->ffbit); | ||
1146 | } | ||
1147 | |||
1148 | PIDFF_FIND_FIELDS(pool, PID_POOL, 0); | ||
1149 | |||
1150 | if (!PIDFF_FIND_FIELDS(device_gain, PID_DEVICE_GAIN, 1)) | ||
1151 | set_bit(FF_GAIN, dev->ffbit); | ||
1152 | |||
1153 | return 0; | ||
1154 | } | ||
1155 | |||
1156 | /* | ||
1157 | * Reset the device | ||
1158 | */ | ||
1159 | static void pidff_reset(struct pidff_device *pidff) | ||
1160 | { | ||
1161 | struct hid_device *hid = pidff->hid; | ||
1162 | int i = 0; | ||
1163 | |||
1164 | pidff->device_control->value[0] = pidff->control_id[PID_RESET]; | ||
1165 | /* We reset twice as sometimes hid_wait_io isn't waiting long enough */ | ||
1166 | hid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT); | ||
1167 | hid_wait_io(hid); | ||
1168 | hid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT); | ||
1169 | hid_wait_io(hid); | ||
1170 | |||
1171 | pidff->device_control->value[0] = | ||
1172 | pidff->control_id[PID_ENABLE_ACTUATORS]; | ||
1173 | hid_submit_report(hid, pidff->reports[PID_DEVICE_CONTROL], USB_DIR_OUT); | ||
1174 | hid_wait_io(hid); | ||
1175 | |||
1176 | /* pool report is sometimes messed up, refetch it */ | ||
1177 | hid_submit_report(hid, pidff->reports[PID_POOL], USB_DIR_IN); | ||
1178 | hid_wait_io(hid); | ||
1179 | |||
1180 | if (pidff->pool[PID_SIMULTANEOUS_MAX].value) { | ||
1181 | int sim_effects = pidff->pool[PID_SIMULTANEOUS_MAX].value[0]; | ||
1182 | while (sim_effects < 2) { | ||
1183 | if (i++ > 20) { | ||
1184 | printk(KERN_WARNING "hid-pidff: device reports " | ||
1185 | "%d simultaneous effects\n", | ||
1186 | sim_effects); | ||
1187 | break; | ||
1188 | } | ||
1189 | debug("pid_pool requested again"); | ||
1190 | hid_submit_report(hid, pidff->reports[PID_POOL], | ||
1191 | USB_DIR_IN); | ||
1192 | hid_wait_io(hid); | ||
1193 | } | ||
1194 | } | ||
1195 | } | ||
1196 | |||
1197 | /* | ||
1198 | * Test if autocenter modification is using the supported method | ||
1199 | */ | ||
1200 | static int pidff_check_autocenter(struct pidff_device *pidff, | ||
1201 | struct input_dev *dev) | ||
1202 | { | ||
1203 | int error; | ||
1204 | |||
1205 | /* | ||
1206 | * Let's find out if autocenter modification is supported | ||
1207 | * Specification doesn't specify anything, so we request an | ||
1208 | * effect upload and cancel it immediately. If the approved | ||
1209 | * effect id was one above the minimum, then we assume the first | ||
1210 | * effect id is a built-in spring type effect used for autocenter | ||
1211 | */ | ||
1212 | |||
1213 | error = pidff_request_effect_upload(pidff, 1); | ||
1214 | if (error) { | ||
1215 | printk(KERN_ERR "hid-pidff: upload request failed\n"); | ||
1216 | return error; | ||
1217 | } | ||
1218 | |||
1219 | if (pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0] == | ||
1220 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + 1) { | ||
1221 | pidff_autocenter(pidff, 0xffff); | ||
1222 | set_bit(FF_AUTOCENTER, dev->ffbit); | ||
1223 | } else { | ||
1224 | printk(KERN_NOTICE "hid-pidff: " | ||
1225 | "device has unknown autocenter control method\n"); | ||
1226 | } | ||
1227 | |||
1228 | pidff_erase_pid(pidff, | ||
1229 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]); | ||
1230 | |||
1231 | return 0; | ||
1232 | |||
1233 | } | ||
1234 | |||
1235 | /* | ||
1236 | * Check if the device is PID and initialize it | ||
1237 | */ | ||
1238 | int hid_pidff_init(struct hid_device *hid) | ||
1239 | { | ||
1240 | struct pidff_device *pidff; | ||
1241 | struct hid_input *hidinput = list_entry(hid->inputs.next, | ||
1242 | struct hid_input, list); | ||
1243 | struct input_dev *dev = hidinput->input; | ||
1244 | struct ff_device *ff; | ||
1245 | int max_effects; | ||
1246 | int error; | ||
1247 | |||
1248 | debug("starting pid init"); | ||
1249 | |||
1250 | if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) { | ||
1251 | debug("not a PID device, no output report"); | ||
1252 | return -ENODEV; | ||
1253 | } | ||
1254 | |||
1255 | pidff = kzalloc(sizeof(*pidff), GFP_KERNEL); | ||
1256 | if (!pidff) | ||
1257 | return -ENOMEM; | ||
1258 | |||
1259 | pidff->hid = hid; | ||
1260 | |||
1261 | pidff_find_reports(hid, HID_OUTPUT_REPORT, pidff); | ||
1262 | pidff_find_reports(hid, HID_FEATURE_REPORT, pidff); | ||
1263 | |||
1264 | if (!pidff_reports_ok(pidff)) { | ||
1265 | debug("reports not ok, aborting"); | ||
1266 | error = -ENODEV; | ||
1267 | goto fail; | ||
1268 | } | ||
1269 | |||
1270 | error = pidff_init_fields(pidff, dev); | ||
1271 | if (error) | ||
1272 | goto fail; | ||
1273 | |||
1274 | pidff_reset(pidff); | ||
1275 | |||
1276 | if (test_bit(FF_GAIN, dev->ffbit)) { | ||
1277 | pidff_set(&pidff->device_gain[PID_DEVICE_GAIN_FIELD], 0xffff); | ||
1278 | hid_submit_report(pidff->hid, pidff->reports[PID_DEVICE_GAIN], | ||
1279 | USB_DIR_OUT); | ||
1280 | } | ||
1281 | |||
1282 | error = pidff_check_autocenter(pidff, dev); | ||
1283 | if (error) | ||
1284 | goto fail; | ||
1285 | |||
1286 | max_effects = | ||
1287 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_maximum - | ||
1288 | pidff->block_load[PID_EFFECT_BLOCK_INDEX].field->logical_minimum + | ||
1289 | 1; | ||
1290 | debug("max effects is %d", max_effects); | ||
1291 | |||
1292 | if (max_effects > PID_EFFECTS_MAX) | ||
1293 | max_effects = PID_EFFECTS_MAX; | ||
1294 | |||
1295 | if (pidff->pool[PID_SIMULTANEOUS_MAX].value) | ||
1296 | debug("max simultaneous effects is %d", | ||
1297 | pidff->pool[PID_SIMULTANEOUS_MAX].value[0]); | ||
1298 | |||
1299 | if (pidff->pool[PID_RAM_POOL_SIZE].value) | ||
1300 | debug("device memory size is %d bytes", | ||
1301 | pidff->pool[PID_RAM_POOL_SIZE].value[0]); | ||
1302 | |||
1303 | if (pidff->pool[PID_DEVICE_MANAGED_POOL].value && | ||
1304 | pidff->pool[PID_DEVICE_MANAGED_POOL].value[0] == 0) { | ||
1305 | printk(KERN_NOTICE "hid-pidff: " | ||
1306 | "device does not support device managed pool\n"); | ||
1307 | goto fail; | ||
1308 | } | ||
1309 | |||
1310 | error = input_ff_create(dev, max_effects); | ||
1311 | if (error) | ||
1312 | goto fail; | ||
1313 | |||
1314 | ff = dev->ff; | ||
1315 | ff->private = pidff; | ||
1316 | ff->upload = pidff_upload_effect; | ||
1317 | ff->erase = pidff_erase_effect; | ||
1318 | ff->set_gain = pidff_set_gain; | ||
1319 | ff->set_autocenter = pidff_set_autocenter; | ||
1320 | ff->playback = pidff_playback; | ||
1321 | |||
1322 | printk(KERN_INFO "Force feedback for USB HID PID devices by " | ||
1323 | "Anssi Hannula <anssi.hannula@gmail.com>\n"); | ||
1324 | |||
1325 | return 0; | ||
1326 | |||
1327 | fail: | ||
1328 | kfree(pidff); | ||
1329 | return error; | ||
1330 | } | ||
diff --git a/drivers/usb/input/hid-tmff.c b/drivers/usb/input/hid-tmff.c index 534425c69c0a..2d5be4c318ac 100644 --- a/drivers/usb/input/hid-tmff.c +++ b/drivers/usb/input/hid-tmff.c | |||
@@ -28,97 +28,65 @@ | |||
28 | */ | 28 | */ |
29 | 29 | ||
30 | #include <linux/input.h> | 30 | #include <linux/input.h> |
31 | #include <linux/sched.h> | ||
32 | 31 | ||
33 | #undef DEBUG | 32 | #undef DEBUG |
34 | #include <linux/usb.h> | 33 | #include <linux/usb.h> |
35 | 34 | ||
36 | #include <linux/circ_buf.h> | ||
37 | |||
38 | #include "hid.h" | 35 | #include "hid.h" |
39 | #include "fixp-arith.h" | ||
40 | 36 | ||
41 | /* Usages for thrustmaster devices I know about */ | 37 | /* Usages for thrustmaster devices I know about */ |
42 | #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb) | 38 | #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb) |
43 | #define DELAY_CALC(t,delay) ((t) + (delay)*HZ/1000) | ||
44 | |||
45 | /* Effect status */ | ||
46 | #define EFFECT_STARTED 0 /* Effect is going to play after some time */ | ||
47 | #define EFFECT_PLAYING 1 /* Effect is playing */ | ||
48 | #define EFFECT_USED 2 | ||
49 | |||
50 | /* For tmff_device::flags */ | ||
51 | #define DEVICE_CLOSING 0 /* The driver is being unitialised */ | ||
52 | |||
53 | /* Check that the current process can access an effect */ | ||
54 | #define CHECK_OWNERSHIP(effect) (current->pid == 0 \ | ||
55 | || effect.owner == current->pid) | ||
56 | |||
57 | #define TMFF_CHECK_ID(id) ((id) >= 0 && (id) < TMFF_EFFECTS) | ||
58 | 39 | ||
59 | #define TMFF_CHECK_OWNERSHIP(i, l) \ | ||
60 | (test_bit(EFFECT_USED, l->effects[i].flags) \ | ||
61 | && CHECK_OWNERSHIP(l->effects[i])) | ||
62 | |||
63 | #define TMFF_EFFECTS 8 | ||
64 | |||
65 | struct tmff_effect { | ||
66 | pid_t owner; | ||
67 | |||
68 | struct ff_effect effect; | ||
69 | |||
70 | unsigned long flags[1]; | ||
71 | unsigned int count; /* Number of times left to play */ | ||
72 | |||
73 | unsigned long play_at; /* When the effect starts to play */ | ||
74 | unsigned long stop_at; /* When the effect ends */ | ||
75 | }; | ||
76 | 40 | ||
77 | struct tmff_device { | 41 | struct tmff_device { |
78 | struct hid_device *hid; | ||
79 | |||
80 | struct hid_report *report; | 42 | struct hid_report *report; |
81 | |||
82 | struct hid_field *rumble; | 43 | struct hid_field *rumble; |
44 | }; | ||
83 | 45 | ||
84 | unsigned int effects_playing; | 46 | /* Changes values from 0 to 0xffff into values from minimum to maximum */ |
85 | struct tmff_effect effects[TMFF_EFFECTS]; | 47 | static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum) |
86 | spinlock_t lock; /* device-level lock. Having locks on | 48 | { |
87 | a per-effect basis could be nice, but | 49 | int ret; |
88 | isn't really necessary */ | ||
89 | 50 | ||
90 | unsigned long flags[1]; /* Contains various information about the | 51 | ret = (in * (maximum - minimum) / 0xffff) + minimum; |
91 | state of the driver for this device */ | 52 | if (ret < minimum) |
53 | return minimum; | ||
54 | if (ret > maximum) | ||
55 | return maximum; | ||
56 | return ret; | ||
57 | } | ||
92 | 58 | ||
93 | struct timer_list timer; | 59 | static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect) |
94 | }; | 60 | { |
61 | struct hid_device *hid = dev->private; | ||
62 | struct tmff_device *tmff = data; | ||
63 | int left, right; /* Rumbling */ | ||
95 | 64 | ||
96 | /* Callbacks */ | 65 | left = hid_tmff_scale(effect->u.rumble.weak_magnitude, |
97 | static void hid_tmff_exit(struct hid_device *hid); | 66 | tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); |
98 | static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, | 67 | right = hid_tmff_scale(effect->u.rumble.strong_magnitude, |
99 | unsigned int type, unsigned int code, int value); | 68 | tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); |
100 | static int hid_tmff_flush(struct input_dev *input, struct file *file); | ||
101 | static int hid_tmff_upload_effect(struct input_dev *input, | ||
102 | struct ff_effect *effect); | ||
103 | static int hid_tmff_erase(struct input_dev *input, int id); | ||
104 | 69 | ||
105 | /* Local functions */ | 70 | tmff->rumble->value[0] = left; |
106 | static void hid_tmff_recalculate_timer(struct tmff_device *tmff); | 71 | tmff->rumble->value[1] = right; |
107 | static void hid_tmff_timer(unsigned long timer_data); | 72 | dbg("(left,right)=(%08x, %08x)", left, right); |
73 | hid_submit_report(hid, tmff->report, USB_DIR_OUT); | ||
74 | |||
75 | return 0; | ||
76 | } | ||
108 | 77 | ||
109 | int hid_tmff_init(struct hid_device *hid) | 78 | int hid_tmff_init(struct hid_device *hid) |
110 | { | 79 | { |
111 | struct tmff_device *private; | 80 | struct tmff_device *tmff; |
112 | struct list_head *pos; | 81 | struct list_head *pos; |
113 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | 82 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
114 | struct input_dev *input_dev = hidinput->input; | 83 | struct input_dev *input_dev = hidinput->input; |
84 | int error; | ||
115 | 85 | ||
116 | private = kzalloc(sizeof(struct tmff_device), GFP_KERNEL); | 86 | tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL); |
117 | if (!private) | 87 | if (!tmff) |
118 | return -ENOMEM; | 88 | return -ENOMEM; |
119 | 89 | ||
120 | hid->ff_private = private; | ||
121 | |||
122 | /* Find the report to use */ | 90 | /* Find the report to use */ |
123 | __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) { | 91 | __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) { |
124 | struct hid_report *report = (struct hid_report *)pos; | 92 | struct hid_report *report = (struct hid_report *)pos; |
@@ -142,18 +110,18 @@ int hid_tmff_init(struct hid_device *hid) | |||
142 | continue; | 110 | continue; |
143 | } | 111 | } |
144 | 112 | ||
145 | if (private->report && private->report != report) { | 113 | if (tmff->report && tmff->report != report) { |
146 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report"); | 114 | warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report"); |
147 | continue; | 115 | continue; |
148 | } | 116 | } |
149 | 117 | ||
150 | if (private->rumble && private->rumble != field) { | 118 | if (tmff->rumble && tmff->rumble != field) { |
151 | warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR"); | 119 | warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR"); |
152 | continue; | 120 | continue; |
153 | } | 121 | } |
154 | 122 | ||
155 | private->report = report; | 123 | tmff->report = report; |
156 | private->rumble = field; | 124 | tmff->rumble = field; |
157 | 125 | ||
158 | set_bit(FF_RUMBLE, input_dev->ffbit); | 126 | set_bit(FF_RUMBLE, input_dev->ffbit); |
159 | break; | 127 | break; |
@@ -162,302 +130,17 @@ int hid_tmff_init(struct hid_device *hid) | |||
162 | warn("ignoring unknown output usage %08x", field->usage[0].hid); | 130 | warn("ignoring unknown output usage %08x", field->usage[0].hid); |
163 | continue; | 131 | continue; |
164 | } | 132 | } |
165 | |||
166 | /* Fallthrough to here only when a valid usage is found */ | ||
167 | input_dev->upload_effect = hid_tmff_upload_effect; | ||
168 | input_dev->flush = hid_tmff_flush; | ||
169 | |||
170 | set_bit(EV_FF, input_dev->evbit); | ||
171 | input_dev->ff_effects_max = TMFF_EFFECTS; | ||
172 | } | 133 | } |
173 | } | 134 | } |
174 | 135 | ||
175 | private->hid = hid; | 136 | error = input_ff_create_memless(input_dev, tmff, hid_tmff_play); |
176 | 137 | if (error) { | |
177 | spin_lock_init(&private->lock); | 138 | kfree(tmff); |
178 | init_timer(&private->timer); | 139 | return error; |
179 | private->timer.data = (unsigned long)private; | ||
180 | private->timer.function = hid_tmff_timer; | ||
181 | |||
182 | /* Event and exit callbacks */ | ||
183 | hid->ff_exit = hid_tmff_exit; | ||
184 | hid->ff_event = hid_tmff_event; | ||
185 | |||
186 | info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>"); | ||
187 | |||
188 | return 0; | ||
189 | } | ||
190 | |||
191 | static void hid_tmff_exit(struct hid_device *hid) | ||
192 | { | ||
193 | struct tmff_device *tmff = hid->ff_private; | ||
194 | unsigned long flags; | ||
195 | |||
196 | spin_lock_irqsave(&tmff->lock, flags); | ||
197 | |||
198 | set_bit(DEVICE_CLOSING, tmff->flags); | ||
199 | del_timer_sync(&tmff->timer); | ||
200 | |||
201 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
202 | |||
203 | kfree(tmff); | ||
204 | } | ||
205 | |||
206 | static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, | ||
207 | unsigned int type, unsigned int code, int value) | ||
208 | { | ||
209 | struct tmff_device *tmff = hid->ff_private; | ||
210 | struct tmff_effect *effect = &tmff->effects[code]; | ||
211 | unsigned long flags; | ||
212 | |||
213 | if (type != EV_FF) | ||
214 | return -EINVAL; | ||
215 | if (!TMFF_CHECK_ID(code)) | ||
216 | return -EINVAL; | ||
217 | if (!TMFF_CHECK_OWNERSHIP(code, tmff)) | ||
218 | return -EACCES; | ||
219 | if (value < 0) | ||
220 | return -EINVAL; | ||
221 | |||
222 | spin_lock_irqsave(&tmff->lock, flags); | ||
223 | |||
224 | if (value > 0) { | ||
225 | set_bit(EFFECT_STARTED, effect->flags); | ||
226 | clear_bit(EFFECT_PLAYING, effect->flags); | ||
227 | effect->count = value; | ||
228 | effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); | ||
229 | } else { | ||
230 | clear_bit(EFFECT_STARTED, effect->flags); | ||
231 | clear_bit(EFFECT_PLAYING, effect->flags); | ||
232 | } | ||
233 | |||
234 | hid_tmff_recalculate_timer(tmff); | ||
235 | |||
236 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
237 | |||
238 | return 0; | ||
239 | |||
240 | } | ||
241 | |||
242 | /* Erase all effects this process owns */ | ||
243 | |||
244 | static int hid_tmff_flush(struct input_dev *dev, struct file *file) | ||
245 | { | ||
246 | struct hid_device *hid = dev->private; | ||
247 | struct tmff_device *tmff = hid->ff_private; | ||
248 | int i; | ||
249 | |||
250 | for (i=0; i<dev->ff_effects_max; ++i) | ||
251 | |||
252 | /* NOTE: no need to lock here. The only times EFFECT_USED is | ||
253 | modified is when effects are uploaded or when an effect is | ||
254 | erased. But a process cannot close its dev/input/eventX fd | ||
255 | and perform ioctls on the same fd all at the same time */ | ||
256 | |||
257 | if (current->pid == tmff->effects[i].owner | ||
258 | && test_bit(EFFECT_USED, tmff->effects[i].flags)) | ||
259 | if (hid_tmff_erase(dev, i)) | ||
260 | warn("erase effect %d failed", i); | ||
261 | |||
262 | |||
263 | return 0; | ||
264 | } | ||
265 | |||
266 | static int hid_tmff_erase(struct input_dev *dev, int id) | ||
267 | { | ||
268 | struct hid_device *hid = dev->private; | ||
269 | struct tmff_device *tmff = hid->ff_private; | ||
270 | unsigned long flags; | ||
271 | |||
272 | if (!TMFF_CHECK_ID(id)) | ||
273 | return -EINVAL; | ||
274 | if (!TMFF_CHECK_OWNERSHIP(id, tmff)) | ||
275 | return -EACCES; | ||
276 | |||
277 | spin_lock_irqsave(&tmff->lock, flags); | ||
278 | |||
279 | tmff->effects[id].flags[0] = 0; | ||
280 | hid_tmff_recalculate_timer(tmff); | ||
281 | |||
282 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
283 | |||
284 | return 0; | ||
285 | } | ||
286 | |||
287 | static int hid_tmff_upload_effect(struct input_dev *input, | ||
288 | struct ff_effect *effect) | ||
289 | { | ||
290 | struct hid_device *hid = input->private; | ||
291 | struct tmff_device *tmff = hid->ff_private; | ||
292 | int id; | ||
293 | unsigned long flags; | ||
294 | |||
295 | if (!test_bit(effect->type, input->ffbit)) | ||
296 | return -EINVAL; | ||
297 | if (effect->id != -1 && !TMFF_CHECK_ID(effect->id)) | ||
298 | return -EINVAL; | ||
299 | |||
300 | spin_lock_irqsave(&tmff->lock, flags); | ||
301 | |||
302 | if (effect->id == -1) { | ||
303 | /* Find a free effect */ | ||
304 | for (id = 0; id < TMFF_EFFECTS && test_bit(EFFECT_USED, tmff->effects[id].flags); ++id); | ||
305 | |||
306 | if (id >= TMFF_EFFECTS) { | ||
307 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
308 | return -ENOSPC; | ||
309 | } | ||
310 | |||
311 | effect->id = id; | ||
312 | tmff->effects[id].owner = current->pid; | ||
313 | tmff->effects[id].flags[0] = 0; | ||
314 | set_bit(EFFECT_USED, tmff->effects[id].flags); | ||
315 | |||
316 | } else { | ||
317 | /* Re-uploading an owned effect, to change parameters */ | ||
318 | id = effect->id; | ||
319 | clear_bit(EFFECT_PLAYING, tmff->effects[id].flags); | ||
320 | } | 140 | } |
321 | 141 | ||
322 | tmff->effects[id].effect = *effect; | 142 | info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>"); |
323 | |||
324 | hid_tmff_recalculate_timer(tmff); | ||
325 | 143 | ||
326 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
327 | return 0; | 144 | return 0; |
328 | } | 145 | } |
329 | 146 | ||
330 | /* Start the timer for the next start/stop/delay */ | ||
331 | /* Always call this while tmff->lock is locked */ | ||
332 | |||
333 | static void hid_tmff_recalculate_timer(struct tmff_device *tmff) | ||
334 | { | ||
335 | int i; | ||
336 | int events = 0; | ||
337 | unsigned long next_time; | ||
338 | |||
339 | next_time = 0; /* Shut up compiler's incorrect warning */ | ||
340 | |||
341 | /* Find the next change in an effect's status */ | ||
342 | for (i = 0; i < TMFF_EFFECTS; ++i) { | ||
343 | struct tmff_effect *effect = &tmff->effects[i]; | ||
344 | unsigned long play_time; | ||
345 | |||
346 | if (!test_bit(EFFECT_STARTED, effect->flags)) | ||
347 | continue; | ||
348 | |||
349 | effect->stop_at = DELAY_CALC(effect->play_at, effect->effect.replay.length); | ||
350 | |||
351 | if (!test_bit(EFFECT_PLAYING, effect->flags)) | ||
352 | play_time = effect->play_at; | ||
353 | else | ||
354 | play_time = effect->stop_at; | ||
355 | |||
356 | events++; | ||
357 | |||
358 | if (time_after(jiffies, play_time)) | ||
359 | play_time = jiffies; | ||
360 | |||
361 | if (events == 1) | ||
362 | next_time = play_time; | ||
363 | else { | ||
364 | if (time_after(next_time, play_time)) | ||
365 | next_time = play_time; | ||
366 | } | ||
367 | } | ||
368 | |||
369 | if (!events && tmff->effects_playing) { | ||
370 | /* Treat all effects turning off as an event */ | ||
371 | events = 1; | ||
372 | next_time = jiffies; | ||
373 | } | ||
374 | |||
375 | if (!events) { | ||
376 | /* No events, no time, no need for a timer. */ | ||
377 | del_timer_sync(&tmff->timer); | ||
378 | return; | ||
379 | } | ||
380 | |||
381 | mod_timer(&tmff->timer, next_time); | ||
382 | } | ||
383 | |||
384 | /* Changes values from 0 to 0xffff into values from minimum to maximum */ | ||
385 | static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum) | ||
386 | { | ||
387 | int ret; | ||
388 | |||
389 | ret = (in * (maximum - minimum) / 0xffff) + minimum; | ||
390 | if (ret < minimum) | ||
391 | return minimum; | ||
392 | if (ret > maximum) | ||
393 | return maximum; | ||
394 | return ret; | ||
395 | } | ||
396 | |||
397 | static void hid_tmff_timer(unsigned long timer_data) | ||
398 | { | ||
399 | struct tmff_device *tmff = (struct tmff_device *) timer_data; | ||
400 | struct hid_device *hid = tmff->hid; | ||
401 | unsigned long flags; | ||
402 | int left = 0, right = 0; /* Rumbling */ | ||
403 | int i; | ||
404 | |||
405 | spin_lock_irqsave(&tmff->lock, flags); | ||
406 | |||
407 | tmff->effects_playing = 0; | ||
408 | |||
409 | for (i = 0; i < TMFF_EFFECTS; ++i) { | ||
410 | struct tmff_effect *effect = &tmff->effects[i]; | ||
411 | |||
412 | if (!test_bit(EFFECT_STARTED, effect->flags)) | ||
413 | continue; | ||
414 | |||
415 | if (!time_after(jiffies, effect->play_at)) | ||
416 | continue; | ||
417 | |||
418 | if (time_after(jiffies, effect->stop_at)) { | ||
419 | |||
420 | dbg("Finished playing once %d", i); | ||
421 | clear_bit(EFFECT_PLAYING, effect->flags); | ||
422 | |||
423 | if (--effect->count <= 0) { | ||
424 | dbg("Stopped %d", i); | ||
425 | clear_bit(EFFECT_STARTED, effect->flags); | ||
426 | continue; | ||
427 | } else { | ||
428 | dbg("Start again %d", i); | ||
429 | effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay); | ||
430 | continue; | ||
431 | } | ||
432 | } | ||
433 | |||
434 | ++tmff->effects_playing; | ||
435 | |||
436 | set_bit(EFFECT_PLAYING, effect->flags); | ||
437 | |||
438 | switch (effect->effect.type) { | ||
439 | case FF_RUMBLE: | ||
440 | right += effect->effect.u.rumble.strong_magnitude; | ||
441 | left += effect->effect.u.rumble.weak_magnitude; | ||
442 | break; | ||
443 | default: | ||
444 | BUG(); | ||
445 | break; | ||
446 | } | ||
447 | } | ||
448 | |||
449 | left = hid_tmff_scale(left, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); | ||
450 | right = hid_tmff_scale(right, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum); | ||
451 | |||
452 | if (left != tmff->rumble->value[0] || right != tmff->rumble->value[1]) { | ||
453 | tmff->rumble->value[0] = left; | ||
454 | tmff->rumble->value[1] = right; | ||
455 | dbg("(left,right)=(%08x, %08x)", left, right); | ||
456 | hid_submit_report(hid, tmff->report, USB_DIR_OUT); | ||
457 | } | ||
458 | |||
459 | if (!test_bit(DEVICE_CLOSING, tmff->flags)) | ||
460 | hid_tmff_recalculate_timer(tmff); | ||
461 | |||
462 | spin_unlock_irqrestore(&tmff->lock, flags); | ||
463 | } | ||
diff --git a/drivers/usb/input/hid-zpff.c b/drivers/usb/input/hid-zpff.c new file mode 100644 index 000000000000..d2ce3214572c --- /dev/null +++ b/drivers/usb/input/hid-zpff.c | |||
@@ -0,0 +1,110 @@ | |||
1 | /* | ||
2 | * Force feedback support for Zeroplus based devices | ||
3 | * | ||
4 | * Copyright (c) 2005, 2006 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 | /* #define DEBUG */ | ||
25 | |||
26 | #define debug(format, arg...) pr_debug("hid-zpff: " format "\n" , ## arg) | ||
27 | |||
28 | #include <linux/input.h> | ||
29 | #include <linux/usb.h> | ||
30 | #include "hid.h" | ||
31 | |||
32 | struct zpff_device { | ||
33 | struct hid_report *report; | ||
34 | }; | ||
35 | |||
36 | static int hid_zpff_play(struct input_dev *dev, void *data, | ||
37 | struct ff_effect *effect) | ||
38 | { | ||
39 | struct hid_device *hid = dev->private; | ||
40 | struct zpff_device *zpff = data; | ||
41 | int left, right; | ||
42 | |||
43 | /* | ||
44 | * The following is specified the other way around in the Zeroplus | ||
45 | * datasheet but the order below is correct for the XFX Executioner; | ||
46 | * however it is possible that the XFX Executioner is an exception | ||
47 | */ | ||
48 | |||
49 | left = effect->u.rumble.strong_magnitude; | ||
50 | right = effect->u.rumble.weak_magnitude; | ||
51 | debug("called with 0x%04x 0x%04x", left, right); | ||
52 | |||
53 | left = left * 0x7f / 0xffff; | ||
54 | right = right * 0x7f / 0xffff; | ||
55 | |||
56 | zpff->report->field[2]->value[0] = left; | ||
57 | zpff->report->field[3]->value[0] = right; | ||
58 | debug("running with 0x%02x 0x%02x", left, right); | ||
59 | hid_submit_report(hid, zpff->report, USB_DIR_OUT); | ||
60 | |||
61 | return 0; | ||
62 | } | ||
63 | |||
64 | int hid_zpff_init(struct hid_device *hid) | ||
65 | { | ||
66 | struct zpff_device *zpff; | ||
67 | struct hid_report *report; | ||
68 | struct hid_input *hidinput = list_entry(hid->inputs.next, | ||
69 | struct hid_input, list); | ||
70 | struct list_head *report_list = | ||
71 | &hid->report_enum[HID_OUTPUT_REPORT].report_list; | ||
72 | struct input_dev *dev = hidinput->input; | ||
73 | int error; | ||
74 | |||
75 | if (list_empty(report_list)) { | ||
76 | printk(KERN_ERR "hid-zpff: no output report found\n"); | ||
77 | return -ENODEV; | ||
78 | } | ||
79 | |||
80 | report = list_entry(report_list->next, struct hid_report, list); | ||
81 | |||
82 | if (report->maxfield < 4) { | ||
83 | printk(KERN_ERR "hid-zpff: not enough fields in report\n"); | ||
84 | return -ENODEV; | ||
85 | } | ||
86 | |||
87 | zpff = kzalloc(sizeof(struct zpff_device), GFP_KERNEL); | ||
88 | if (!zpff) | ||
89 | return -ENOMEM; | ||
90 | |||
91 | set_bit(FF_RUMBLE, dev->ffbit); | ||
92 | |||
93 | error = input_ff_create_memless(dev, zpff, hid_zpff_play); | ||
94 | if (error) { | ||
95 | kfree(zpff); | ||
96 | return error; | ||
97 | } | ||
98 | |||
99 | zpff->report = report; | ||
100 | zpff->report->field[0]->value[0] = 0x00; | ||
101 | zpff->report->field[1]->value[0] = 0x02; | ||
102 | zpff->report->field[2]->value[0] = 0x00; | ||
103 | zpff->report->field[3]->value[0] = 0x00; | ||
104 | hid_submit_report(hid, zpff->report, USB_DIR_OUT); | ||
105 | |||
106 | printk(KERN_INFO "Force feedback for Zeroplus based devices by " | ||
107 | "Anssi Hannula <anssi.hannula@gmail.com>\n"); | ||
108 | |||
109 | return 0; | ||
110 | } | ||
diff --git a/drivers/usb/input/hid.h b/drivers/usb/input/hid.h index 778e575de352..b03fd9b075df 100644 --- a/drivers/usb/input/hid.h +++ b/drivers/usb/input/hid.h | |||
@@ -449,11 +449,6 @@ struct hid_device { /* device report descriptor */ | |||
449 | char phys[64]; /* Device physical location */ | 449 | char phys[64]; /* Device physical location */ |
450 | char uniq[64]; /* Device unique identifier (serial #) */ | 450 | char uniq[64]; /* Device unique identifier (serial #) */ |
451 | 451 | ||
452 | void *ff_private; /* Private data for the force-feedback driver */ | ||
453 | void (*ff_exit)(struct hid_device*); /* Called by hid_exit_ff(hid) */ | ||
454 | int (*ff_event)(struct hid_device *hid, struct input_dev *input, | ||
455 | unsigned int type, unsigned int code, int value); | ||
456 | |||
457 | #ifdef CONFIG_USB_HIDINPUT_POWERBOOK | 452 | #ifdef CONFIG_USB_HIDINPUT_POWERBOOK |
458 | unsigned long pb_pressed_fn[NBITS(KEY_MAX)]; | 453 | unsigned long pb_pressed_fn[NBITS(KEY_MAX)]; |
459 | unsigned long pb_pressed_numlock[NBITS(KEY_MAX)]; | 454 | unsigned long pb_pressed_numlock[NBITS(KEY_MAX)]; |
@@ -521,29 +516,22 @@ void hid_close(struct hid_device *); | |||
521 | int hid_set_field(struct hid_field *, unsigned, __s32); | 516 | int hid_set_field(struct hid_field *, unsigned, __s32); |
522 | void hid_submit_report(struct hid_device *, struct hid_report *, unsigned char dir); | 517 | void hid_submit_report(struct hid_device *, struct hid_report *, unsigned char dir); |
523 | void hid_init_reports(struct hid_device *hid); | 518 | void hid_init_reports(struct hid_device *hid); |
524 | struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type); | ||
525 | int hid_wait_io(struct hid_device* hid); | 519 | int hid_wait_io(struct hid_device* hid); |
526 | 520 | ||
527 | 521 | ||
528 | #ifdef CONFIG_HID_FF | 522 | #ifdef CONFIG_HID_FF |
529 | int hid_ff_init(struct hid_device *hid); | 523 | int hid_ff_init(struct hid_device *hid); |
524 | |||
525 | int hid_lgff_init(struct hid_device *hid); | ||
526 | int hid_tmff_init(struct hid_device *hid); | ||
527 | int hid_zpff_init(struct hid_device *hid); | ||
528 | #ifdef CONFIG_HID_PID | ||
529 | int hid_pidff_init(struct hid_device *hid); | ||
530 | #else | ||
531 | static inline int hid_pidff_init(struct hid_device *hid) { return -ENODEV; } | ||
532 | #endif | ||
533 | |||
530 | #else | 534 | #else |
531 | static inline int hid_ff_init(struct hid_device *hid) { return -1; } | 535 | static inline int hid_ff_init(struct hid_device *hid) { return -1; } |
532 | #endif | 536 | #endif |
533 | static inline void hid_ff_exit(struct hid_device *hid) | ||
534 | { | ||
535 | if (hid->ff_exit) | ||
536 | hid->ff_exit(hid); | ||
537 | } | ||
538 | static inline int hid_ff_event(struct hid_device *hid, struct input_dev *input, | ||
539 | unsigned int type, unsigned int code, int value) | ||
540 | { | ||
541 | if (hid->ff_event) | ||
542 | return hid->ff_event(hid, input, type, code, value); | ||
543 | return -ENOSYS; | ||
544 | } | ||
545 | |||
546 | int hid_lgff_init(struct hid_device* hid); | ||
547 | int hid_tmff_init(struct hid_device* hid); | ||
548 | int hid_pid_init(struct hid_device* hid); | ||
549 | 537 | ||
diff --git a/drivers/usb/input/pid.c b/drivers/usb/input/pid.c deleted file mode 100644 index d9d9f656b8c9..000000000000 --- a/drivers/usb/input/pid.c +++ /dev/null | |||
@@ -1,295 +0,0 @@ | |||
1 | /* | ||
2 | * PID Force feedback support for hid devices. | ||
3 | * | ||
4 | * Copyright (c) 2002 Rodrigo Damazio. | ||
5 | * Portions by Johann Deneux and Bjorn Augustson | ||
6 | */ | ||
7 | |||
8 | /* | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Should you need to contact me, the author, you can do so by | ||
24 | * e-mail - mail your message to <rdamazio@lsi.usp.br> | ||
25 | */ | ||
26 | |||
27 | #include <linux/config.h> | ||
28 | #include <linux/module.h> | ||
29 | #include <linux/slab.h> | ||
30 | #include <linux/kernel.h> | ||
31 | #include <linux/init.h> | ||
32 | #include <linux/mm.h> | ||
33 | #include <linux/smp_lock.h> | ||
34 | #include <linux/spinlock.h> | ||
35 | #include <linux/input.h> | ||
36 | #include <linux/usb.h> | ||
37 | #include "hid.h" | ||
38 | #include "pid.h" | ||
39 | |||
40 | #define CHECK_OWNERSHIP(i, hid_pid) \ | ||
41 | ((i) < FF_EFFECTS_MAX && i >= 0 && \ | ||
42 | test_bit(FF_PID_FLAGS_USED, &hid_pid->effects[(i)].flags) && \ | ||
43 | (current->pid == 0 || \ | ||
44 | (hid_pid)->effects[(i)].owner == current->pid)) | ||
45 | |||
46 | /* Called when a transfer is completed */ | ||
47 | static void hid_pid_ctrl_out(struct urb *u, struct pt_regs *regs) | ||
48 | { | ||
49 | dev_dbg(&u->dev->dev, "hid_pid_ctrl_out - Transfer Completed\n"); | ||
50 | } | ||
51 | |||
52 | static void hid_pid_exit(struct hid_device *hid) | ||
53 | { | ||
54 | struct hid_ff_pid *private = hid->ff_private; | ||
55 | |||
56 | if (private->urbffout) { | ||
57 | usb_kill_urb(private->urbffout); | ||
58 | usb_free_urb(private->urbffout); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | static int pid_upload_periodic(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update) | ||
63 | { | ||
64 | dev_info(&pid->hid->dev->dev, "requested periodic force upload\n"); | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | static int pid_upload_constant(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update) | ||
69 | { | ||
70 | dev_info(&pid->hid->dev->dev, "requested constant force upload\n"); | ||
71 | return 0; | ||
72 | } | ||
73 | |||
74 | static int pid_upload_condition(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update) | ||
75 | { | ||
76 | dev_info(&pid->hid->dev->dev, "requested Condition force upload\n"); | ||
77 | return 0; | ||
78 | } | ||
79 | |||
80 | static int pid_upload_ramp(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update) | ||
81 | { | ||
82 | dev_info(&pid->hid->dev->dev, "request ramp force upload\n"); | ||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | static int hid_pid_event(struct hid_device *hid, struct input_dev *input, | ||
87 | unsigned int type, unsigned int code, int value) | ||
88 | { | ||
89 | dev_dbg(&hid->dev->dev, "PID event received: type=%d,code=%d,value=%d.\n", type, code, value); | ||
90 | |||
91 | if (type != EV_FF) | ||
92 | return -1; | ||
93 | |||
94 | return 0; | ||
95 | } | ||
96 | |||
97 | /* Lock must be held by caller */ | ||
98 | static void hid_pid_ctrl_playback(struct hid_device *hid, struct hid_pid_effect *effect, int play) | ||
99 | { | ||
100 | if (play) | ||
101 | set_bit(FF_PID_FLAGS_PLAYING, &effect->flags); | ||
102 | else | ||
103 | clear_bit(FF_PID_FLAGS_PLAYING, &effect->flags); | ||
104 | } | ||
105 | |||
106 | static int hid_pid_erase(struct input_dev *dev, int id) | ||
107 | { | ||
108 | struct hid_device *hid = dev->private; | ||
109 | struct hid_ff_pid *pid = hid->ff_private; | ||
110 | struct hid_field *field; | ||
111 | unsigned long flags; | ||
112 | int ret; | ||
113 | |||
114 | if (!CHECK_OWNERSHIP(id, pid)) | ||
115 | return -EACCES; | ||
116 | |||
117 | /* Find report */ | ||
118 | field = hid_find_field_by_usage(hid, HID_UP_PID | FF_PID_USAGE_BLOCK_FREE, | ||
119 | HID_OUTPUT_REPORT); | ||
120 | if (!field) { | ||
121 | dev_err(&hid->dev->dev, "couldn't find report\n"); | ||
122 | return -EIO; | ||
123 | } | ||
124 | |||
125 | ret = hid_set_field(field, 0, pid->effects[id].device_id); | ||
126 | if (ret) { | ||
127 | dev_err(&hid->dev->dev, "couldn't set field\n"); | ||
128 | return ret; | ||
129 | } | ||
130 | |||
131 | hid_submit_report(hid, field->report, USB_DIR_OUT); | ||
132 | |||
133 | spin_lock_irqsave(&pid->lock, flags); | ||
134 | hid_pid_ctrl_playback(hid, pid->effects + id, 0); | ||
135 | pid->effects[id].flags = 0; | ||
136 | spin_unlock_irqrestore(&pid->lock, flags); | ||
137 | |||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | /* Erase all effects this process owns */ | ||
142 | static int hid_pid_flush(struct input_dev *dev, struct file *file) | ||
143 | { | ||
144 | struct hid_device *hid = dev->private; | ||
145 | struct hid_ff_pid *pid = hid->ff_private; | ||
146 | int i; | ||
147 | |||
148 | /*NOTE: no need to lock here. The only times EFFECT_USED is | ||
149 | modified is when effects are uploaded or when an effect is | ||
150 | erased. But a process cannot close its dev/input/eventX fd | ||
151 | and perform ioctls on the same fd all at the same time */ | ||
152 | /*FIXME: multiple threads, anyone? */ | ||
153 | for (i = 0; i < dev->ff_effects_max; ++i) | ||
154 | if (current->pid == pid->effects[i].owner | ||
155 | && test_bit(FF_PID_FLAGS_USED, &pid->effects[i].flags)) | ||
156 | if (hid_pid_erase(dev, i)) | ||
157 | dev_warn(&hid->dev->dev, "erase effect %d failed", i); | ||
158 | |||
159 | return 0; | ||
160 | } | ||
161 | |||
162 | static int hid_pid_upload_effect(struct input_dev *dev, | ||
163 | struct ff_effect *effect) | ||
164 | { | ||
165 | struct hid_ff_pid *pid_private = (struct hid_ff_pid *)(dev->private); | ||
166 | int ret; | ||
167 | int is_update; | ||
168 | unsigned long flags; | ||
169 | |||
170 | dev_dbg(&pid_private->hid->dev->dev, "upload effect called: effect_type=%x\n", effect->type); | ||
171 | /* Check this effect type is supported by this device */ | ||
172 | if (!test_bit(effect->type, dev->ffbit)) { | ||
173 | dev_dbg(&pid_private->hid->dev->dev, | ||
174 | "invalid kind of effect requested.\n"); | ||
175 | return -EINVAL; | ||
176 | } | ||
177 | |||
178 | /* | ||
179 | * If we want to create a new effect, get a free id | ||
180 | */ | ||
181 | if (effect->id == -1) { | ||
182 | int id = 0; | ||
183 | |||
184 | // Spinlock so we don`t get a race condition when choosing IDs | ||
185 | spin_lock_irqsave(&pid_private->lock, flags); | ||
186 | |||
187 | while (id < FF_EFFECTS_MAX) | ||
188 | if (!test_and_set_bit(FF_PID_FLAGS_USED, &pid_private->effects[id++].flags)) | ||
189 | break; | ||
190 | |||
191 | if (id == FF_EFFECTS_MAX) { | ||
192 | spin_unlock_irqrestore(&pid_private->lock, flags); | ||
193 | // TEMP - We need to get ff_effects_max correctly first: || id >= dev->ff_effects_max) { | ||
194 | dev_dbg(&pid_private->hid->dev->dev, "Not enough device memory\n"); | ||
195 | return -ENOMEM; | ||
196 | } | ||
197 | |||
198 | effect->id = id; | ||
199 | dev_dbg(&pid_private->hid->dev->dev, "effect ID is %d.\n", id); | ||
200 | pid_private->effects[id].owner = current->pid; | ||
201 | pid_private->effects[id].flags = (1 << FF_PID_FLAGS_USED); | ||
202 | spin_unlock_irqrestore(&pid_private->lock, flags); | ||
203 | |||
204 | is_update = FF_PID_FALSE; | ||
205 | } else { | ||
206 | /* We want to update an effect */ | ||
207 | if (!CHECK_OWNERSHIP(effect->id, pid_private)) | ||
208 | return -EACCES; | ||
209 | |||
210 | /* Parameter type cannot be updated */ | ||
211 | if (effect->type != pid_private->effects[effect->id].effect.type) | ||
212 | return -EINVAL; | ||
213 | |||
214 | /* Check the effect is not already being updated */ | ||
215 | if (test_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags)) | ||
216 | return -EAGAIN; | ||
217 | |||
218 | is_update = FF_PID_TRUE; | ||
219 | } | ||
220 | |||
221 | /* | ||
222 | * Upload the effect | ||
223 | */ | ||
224 | switch (effect->type) { | ||
225 | case FF_PERIODIC: | ||
226 | ret = pid_upload_periodic(pid_private, effect, is_update); | ||
227 | break; | ||
228 | |||
229 | case FF_CONSTANT: | ||
230 | ret = pid_upload_constant(pid_private, effect, is_update); | ||
231 | break; | ||
232 | |||
233 | case FF_SPRING: | ||
234 | case FF_FRICTION: | ||
235 | case FF_DAMPER: | ||
236 | case FF_INERTIA: | ||
237 | ret = pid_upload_condition(pid_private, effect, is_update); | ||
238 | break; | ||
239 | |||
240 | case FF_RAMP: | ||
241 | ret = pid_upload_ramp(pid_private, effect, is_update); | ||
242 | break; | ||
243 | |||
244 | default: | ||
245 | dev_dbg(&pid_private->hid->dev->dev, | ||
246 | "invalid type of effect requested - %x.\n", | ||
247 | effect->type); | ||
248 | return -EINVAL; | ||
249 | } | ||
250 | /* If a packet was sent, forbid new updates until we are notified | ||
251 | * that the packet was updated | ||
252 | */ | ||
253 | if (ret == 0) | ||
254 | set_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags); | ||
255 | pid_private->effects[effect->id].effect = *effect; | ||
256 | return ret; | ||
257 | } | ||
258 | |||
259 | int hid_pid_init(struct hid_device *hid) | ||
260 | { | ||
261 | struct hid_ff_pid *private; | ||
262 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | ||
263 | struct input_dev *input_dev = hidinput->input; | ||
264 | |||
265 | private = hid->ff_private = kzalloc(sizeof(struct hid_ff_pid), GFP_KERNEL); | ||
266 | if (!private) | ||
267 | return -ENOMEM; | ||
268 | |||
269 | private->hid = hid; | ||
270 | |||
271 | hid->ff_exit = hid_pid_exit; | ||
272 | hid->ff_event = hid_pid_event; | ||
273 | |||
274 | /* Open output URB */ | ||
275 | if (!(private->urbffout = usb_alloc_urb(0, GFP_KERNEL))) { | ||
276 | kfree(private); | ||
277 | return -1; | ||
278 | } | ||
279 | |||
280 | usb_fill_control_urb(private->urbffout, hid->dev, 0, | ||
281 | (void *)&private->ffcr, private->ctrl_buffer, 8, | ||
282 | hid_pid_ctrl_out, hid); | ||
283 | |||
284 | input_dev->upload_effect = hid_pid_upload_effect; | ||
285 | input_dev->flush = hid_pid_flush; | ||
286 | input_dev->ff_effects_max = 8; // A random default | ||
287 | set_bit(EV_FF, input_dev->evbit); | ||
288 | set_bit(EV_FF_STATUS, input_dev->evbit); | ||
289 | |||
290 | spin_lock_init(&private->lock); | ||
291 | |||
292 | printk(KERN_INFO "Force feedback driver for PID devices by Rodrigo Damazio <rdamazio@lsi.usp.br>.\n"); | ||
293 | |||
294 | return 0; | ||
295 | } | ||
diff --git a/drivers/usb/input/pid.h b/drivers/usb/input/pid.h deleted file mode 100644 index a2cb9627ed0e..000000000000 --- a/drivers/usb/input/pid.h +++ /dev/null | |||
@@ -1,62 +0,0 @@ | |||
1 | /* | ||
2 | * PID Force feedback support for hid devices. | ||
3 | * | ||
4 | * Copyright (c) 2002 Rodrigo Damazio. | ||
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 | * Should you need to contact me, the author, you can do so by | ||
23 | * e-mail - mail your message to <rdamazio@lsi.usp.br> | ||
24 | */ | ||
25 | |||
26 | #define FF_EFFECTS_MAX 64 | ||
27 | |||
28 | #define FF_PID_FLAGS_USED 1 /* If the effect exists */ | ||
29 | #define FF_PID_FLAGS_UPDATING 2 /* If the effect is being updated */ | ||
30 | #define FF_PID_FLAGS_PLAYING 3 /* If the effect is currently being played */ | ||
31 | |||
32 | #define FF_PID_FALSE 0 | ||
33 | #define FF_PID_TRUE 1 | ||
34 | |||
35 | struct hid_pid_effect { | ||
36 | unsigned long flags; | ||
37 | pid_t owner; | ||
38 | unsigned int device_id; /* The device-assigned ID */ | ||
39 | struct ff_effect effect; | ||
40 | }; | ||
41 | |||
42 | struct hid_ff_pid { | ||
43 | struct hid_device *hid; | ||
44 | unsigned long gain; | ||
45 | |||
46 | struct urb *urbffout; | ||
47 | struct usb_ctrlrequest ffcr; | ||
48 | spinlock_t lock; | ||
49 | |||
50 | unsigned char ctrl_buffer[8]; | ||
51 | |||
52 | struct hid_pid_effect effects[FF_EFFECTS_MAX]; | ||
53 | }; | ||
54 | |||
55 | /* | ||
56 | * Constants from the PID usage table (still far from complete) | ||
57 | */ | ||
58 | |||
59 | #define FF_PID_USAGE_BLOCK_LOAD 0x89UL | ||
60 | #define FF_PID_USAGE_BLOCK_FREE 0x90UL | ||
61 | #define FF_PID_USAGE_NEW_EFFECT 0xABUL | ||
62 | #define FF_PID_USAGE_POOL_REPORT 0x7FUL | ||