]>
Commit | Line | Data |
---|---|---|
27f06942 DH |
1 | /* |
2 | * Device Modules for Nintendo Wii / Wii U HID Driver | |
3 | * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com> | |
4 | */ | |
5 | ||
6 | /* | |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the Free | |
9 | * Software Foundation; either version 2 of the License, or (at your option) | |
10 | * any later version. | |
11 | */ | |
12 | ||
13 | /* | |
14 | * Wiimote Modules | |
15 | * Nintendo devices provide different peripherals and many new devices lack | |
16 | * initial features like the IR camera. Therefore, each peripheral device is | |
17 | * implemented as an independent module and we probe on each device only the | |
18 | * modules for the hardware that really is available. | |
19 | * | |
20 | * Module registration is sequential. Unregistration is done in reverse order. | |
21 | * After device detection, the needed modules are loaded. Users can trigger | |
22 | * re-detection which causes all modules to be unloaded and then reload the | |
23 | * modules for the new detected device. | |
24 | * | |
25 | * wdata->input is a shared input device. It is always initialized prior to | |
26 | * module registration. If at least one registered module is marked as | |
27 | * WIIMOD_FLAG_INPUT, then the input device will get registered after all | |
28 | * modules were registered. | |
29 | * Please note that it is unregistered _before_ the "remove" callbacks are | |
30 | * called. This guarantees that no input interaction is done, anymore. However, | |
31 | * the wiimote core keeps a reference to the input device so it is freed only | |
32 | * after all modules were removed. It is safe to send events to unregistered | |
33 | * input devices. | |
34 | */ | |
35 | ||
36 | #include <linux/device.h> | |
37 | #include <linux/hid.h> | |
38 | #include <linux/input.h> | |
39 | #include <linux/spinlock.h> | |
40 | #include "hid-wiimote.h" | |
41 | ||
20cef813 DH |
42 | /* |
43 | * Keys | |
44 | * The initial Wii Remote provided a bunch of buttons that are reported as | |
45 | * part of the core protocol. Many later devices dropped these and report | |
46 | * invalid data in the core button reports. Load this only on devices which | |
47 | * correctly send button reports. | |
48 | * It uses the shared input device. | |
49 | */ | |
50 | ||
51 | static const __u16 wiimod_keys_map[] = { | |
52 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ | |
53 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ | |
54 | KEY_UP, /* WIIPROTO_KEY_UP */ | |
55 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ | |
56 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ | |
57 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ | |
58 | BTN_1, /* WIIPROTO_KEY_ONE */ | |
59 | BTN_2, /* WIIPROTO_KEY_TWO */ | |
60 | BTN_A, /* WIIPROTO_KEY_A */ | |
61 | BTN_B, /* WIIPROTO_KEY_B */ | |
62 | BTN_MODE, /* WIIPROTO_KEY_HOME */ | |
63 | }; | |
64 | ||
65 | static void wiimod_keys_in_keys(struct wiimote_data *wdata, const __u8 *keys) | |
66 | { | |
67 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_LEFT], | |
68 | !!(keys[0] & 0x01)); | |
69 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_RIGHT], | |
70 | !!(keys[0] & 0x02)); | |
71 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_DOWN], | |
72 | !!(keys[0] & 0x04)); | |
73 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_UP], | |
74 | !!(keys[0] & 0x08)); | |
75 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_PLUS], | |
76 | !!(keys[0] & 0x10)); | |
77 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_TWO], | |
78 | !!(keys[1] & 0x01)); | |
79 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_ONE], | |
80 | !!(keys[1] & 0x02)); | |
81 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_B], | |
82 | !!(keys[1] & 0x04)); | |
83 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_A], | |
84 | !!(keys[1] & 0x08)); | |
85 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_MINUS], | |
86 | !!(keys[1] & 0x10)); | |
87 | input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_HOME], | |
88 | !!(keys[1] & 0x80)); | |
89 | input_sync(wdata->input); | |
90 | } | |
91 | ||
92 | static int wiimod_keys_probe(const struct wiimod_ops *ops, | |
93 | struct wiimote_data *wdata) | |
94 | { | |
95 | unsigned int i; | |
96 | ||
97 | set_bit(EV_KEY, wdata->input->evbit); | |
98 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) | |
99 | set_bit(wiimod_keys_map[i], wdata->input->keybit); | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | static const struct wiimod_ops wiimod_keys = { | |
105 | .flags = WIIMOD_FLAG_INPUT, | |
106 | .arg = 0, | |
107 | .probe = wiimod_keys_probe, | |
108 | .remove = NULL, | |
109 | .in_keys = wiimod_keys_in_keys, | |
110 | }; | |
111 | ||
112 | /* | |
113 | * Rumble | |
114 | * Nearly all devices provide a rumble feature. A small motor for | |
115 | * force-feedback effects. We provide an FF_RUMBLE memless ff device on the | |
116 | * shared input device if this module is loaded. | |
117 | * The rumble motor is controlled via a flag on almost every output report so | |
118 | * the wiimote core handles the rumble flag. But if a device doesn't provide | |
119 | * the rumble motor, this flag shouldn't be set. | |
120 | */ | |
121 | ||
122 | static int wiimod_rumble_play(struct input_dev *dev, void *data, | |
123 | struct ff_effect *eff) | |
124 | { | |
125 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
126 | __u8 value; | |
127 | unsigned long flags; | |
128 | ||
129 | /* | |
130 | * The wiimote supports only a single rumble motor so if any magnitude | |
131 | * is set to non-zero then we start the rumble motor. If both are set to | |
132 | * zero, we stop the rumble motor. | |
133 | */ | |
134 | ||
135 | if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) | |
136 | value = 1; | |
137 | else | |
138 | value = 0; | |
139 | ||
140 | spin_lock_irqsave(&wdata->state.lock, flags); | |
141 | wiiproto_req_rumble(wdata, value); | |
142 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | static int wiimod_rumble_probe(const struct wiimod_ops *ops, | |
148 | struct wiimote_data *wdata) | |
149 | { | |
150 | set_bit(FF_RUMBLE, wdata->input->ffbit); | |
151 | if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play)) | |
152 | return -ENOMEM; | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
157 | static void wiimod_rumble_remove(const struct wiimod_ops *ops, | |
158 | struct wiimote_data *wdata) | |
159 | { | |
160 | unsigned long flags; | |
161 | ||
162 | spin_lock_irqsave(&wdata->state.lock, flags); | |
163 | wiiproto_req_rumble(wdata, 0); | |
164 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
165 | } | |
166 | ||
167 | static const struct wiimod_ops wiimod_rumble = { | |
168 | .flags = WIIMOD_FLAG_INPUT, | |
169 | .arg = 0, | |
170 | .probe = wiimod_rumble_probe, | |
171 | .remove = wiimod_rumble_remove, | |
172 | }; | |
173 | ||
27f06942 DH |
174 | /* module table */ |
175 | ||
176 | const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = { | |
20cef813 DH |
177 | [WIIMOD_KEYS] = &wiimod_keys, |
178 | [WIIMOD_RUMBLE] = &wiimod_rumble, | |
27f06942 | 179 | }; |