Commit | Line | Data |
---|---|---|
fb51b443 DH |
1 | /* |
2 | * HID driver for Nintendo Wiimote devices | |
3 | * Copyright (c) 2011 David Herrmann | |
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 | ||
672bc4e0 | 13 | #include <linux/device.h> |
02fb72a0 | 14 | #include <linux/hid.h> |
672bc4e0 | 15 | #include <linux/input.h> |
23a5a4a3 | 16 | #include <linux/leds.h> |
fb51b443 | 17 | #include <linux/module.h> |
23c063cb | 18 | #include <linux/spinlock.h> |
02fb72a0 | 19 | #include "hid-ids.h" |
fb51b443 DH |
20 | |
21 | #define WIIMOTE_VERSION "0.1" | |
22 | #define WIIMOTE_NAME "Nintendo Wii Remote" | |
23c063cb DH |
23 | #define WIIMOTE_BUFSIZE 32 |
24 | ||
25 | struct wiimote_buf { | |
26 | __u8 data[HID_MAX_BUFFER_SIZE]; | |
27 | size_t size; | |
28 | }; | |
fb51b443 | 29 | |
32a0d9a5 DH |
30 | struct wiimote_state { |
31 | spinlock_t lock; | |
32 | __u8 flags; | |
efcf9188 | 33 | __u8 accel_split[2]; |
32a0d9a5 DH |
34 | }; |
35 | ||
e894d0e3 DH |
36 | struct wiimote_data { |
37 | struct hid_device *hdev; | |
672bc4e0 | 38 | struct input_dev *input; |
23a5a4a3 | 39 | struct led_classdev *leds[4]; |
98a558ae | 40 | struct input_dev *accel; |
23c063cb DH |
41 | |
42 | spinlock_t qlock; | |
43 | __u8 head; | |
44 | __u8 tail; | |
45 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; | |
46 | struct work_struct worker; | |
32a0d9a5 DH |
47 | |
48 | struct wiimote_state state; | |
e894d0e3 DH |
49 | }; |
50 | ||
c003ec21 DH |
51 | #define WIIPROTO_FLAG_LED1 0x01 |
52 | #define WIIPROTO_FLAG_LED2 0x02 | |
53 | #define WIIPROTO_FLAG_LED3 0x04 | |
54 | #define WIIPROTO_FLAG_LED4 0x08 | |
55 | #define WIIPROTO_FLAG_RUMBLE 0x10 | |
98a558ae | 56 | #define WIIPROTO_FLAG_ACCEL 0x20 |
32a0d9a5 DH |
57 | #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ |
58 | WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) | |
db308346 | 59 | |
23a5a4a3 DH |
60 | /* return flag for led \num */ |
61 | #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1)) | |
62 | ||
1abb9ad3 | 63 | enum wiiproto_reqs { |
2cb5e4bc | 64 | WIIPROTO_REQ_NULL = 0x0, |
c003ec21 | 65 | WIIPROTO_REQ_RUMBLE = 0x10, |
db308346 | 66 | WIIPROTO_REQ_LED = 0x11, |
2cb5e4bc | 67 | WIIPROTO_REQ_DRM = 0x12, |
c87019e4 DH |
68 | WIIPROTO_REQ_STATUS = 0x20, |
69 | WIIPROTO_REQ_RETURN = 0x22, | |
1abb9ad3 | 70 | WIIPROTO_REQ_DRM_K = 0x30, |
efcf9188 DH |
71 | WIIPROTO_REQ_DRM_KA = 0x31, |
72 | WIIPROTO_REQ_DRM_KAI = 0x33, | |
73 | WIIPROTO_REQ_DRM_KAE = 0x35, | |
74 | WIIPROTO_REQ_DRM_KAIE = 0x37, | |
75 | WIIPROTO_REQ_DRM_SKAI1 = 0x3e, | |
76 | WIIPROTO_REQ_DRM_SKAI2 = 0x3f, | |
1abb9ad3 DH |
77 | }; |
78 | ||
79 | enum wiiproto_keys { | |
80 | WIIPROTO_KEY_LEFT, | |
81 | WIIPROTO_KEY_RIGHT, | |
82 | WIIPROTO_KEY_UP, | |
83 | WIIPROTO_KEY_DOWN, | |
84 | WIIPROTO_KEY_PLUS, | |
85 | WIIPROTO_KEY_MINUS, | |
86 | WIIPROTO_KEY_ONE, | |
87 | WIIPROTO_KEY_TWO, | |
88 | WIIPROTO_KEY_A, | |
89 | WIIPROTO_KEY_B, | |
90 | WIIPROTO_KEY_HOME, | |
91 | WIIPROTO_KEY_COUNT | |
92 | }; | |
93 | ||
94 | static __u16 wiiproto_keymap[] = { | |
95 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ | |
96 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ | |
97 | KEY_UP, /* WIIPROTO_KEY_UP */ | |
98 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ | |
99 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ | |
100 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ | |
101 | BTN_1, /* WIIPROTO_KEY_ONE */ | |
102 | BTN_2, /* WIIPROTO_KEY_TWO */ | |
103 | BTN_A, /* WIIPROTO_KEY_A */ | |
104 | BTN_B, /* WIIPROTO_KEY_B */ | |
105 | BTN_MODE, /* WIIPROTO_KEY_HOME */ | |
106 | }; | |
107 | ||
0c218f14 DH |
108 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
109 | size_t count) | |
110 | { | |
111 | __u8 *buf; | |
112 | ssize_t ret; | |
113 | ||
114 | if (!hdev->hid_output_raw_report) | |
115 | return -ENODEV; | |
116 | ||
117 | buf = kmemdup(buffer, count, GFP_KERNEL); | |
118 | if (!buf) | |
119 | return -ENOMEM; | |
120 | ||
121 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); | |
122 | ||
123 | kfree(buf); | |
124 | return ret; | |
125 | } | |
126 | ||
23c063cb DH |
127 | static void wiimote_worker(struct work_struct *work) |
128 | { | |
129 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, | |
130 | worker); | |
131 | unsigned long flags; | |
132 | ||
133 | spin_lock_irqsave(&wdata->qlock, flags); | |
134 | ||
135 | while (wdata->head != wdata->tail) { | |
136 | spin_unlock_irqrestore(&wdata->qlock, flags); | |
137 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, | |
138 | wdata->outq[wdata->tail].size); | |
139 | spin_lock_irqsave(&wdata->qlock, flags); | |
140 | ||
141 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; | |
142 | } | |
143 | ||
144 | spin_unlock_irqrestore(&wdata->qlock, flags); | |
145 | } | |
146 | ||
147 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, | |
148 | size_t count) | |
149 | { | |
150 | unsigned long flags; | |
151 | __u8 newhead; | |
152 | ||
153 | if (count > HID_MAX_BUFFER_SIZE) { | |
154 | hid_warn(wdata->hdev, "Sending too large output report\n"); | |
155 | return; | |
156 | } | |
157 | ||
158 | /* | |
159 | * Copy new request into our output queue and check whether the | |
160 | * queue is full. If it is full, discard this request. | |
161 | * If it is empty we need to start a new worker that will | |
162 | * send out the buffer to the hid device. | |
163 | * If the queue is not empty, then there must be a worker | |
164 | * that is currently sending out our buffer and this worker | |
165 | * will reschedule itself until the queue is empty. | |
166 | */ | |
167 | ||
168 | spin_lock_irqsave(&wdata->qlock, flags); | |
169 | ||
170 | memcpy(wdata->outq[wdata->head].data, buffer, count); | |
171 | wdata->outq[wdata->head].size = count; | |
172 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; | |
173 | ||
174 | if (wdata->head == wdata->tail) { | |
175 | wdata->head = newhead; | |
176 | schedule_work(&wdata->worker); | |
177 | } else if (newhead != wdata->tail) { | |
178 | wdata->head = newhead; | |
179 | } else { | |
180 | hid_warn(wdata->hdev, "Output queue is full"); | |
181 | } | |
182 | ||
183 | spin_unlock_irqrestore(&wdata->qlock, flags); | |
184 | } | |
185 | ||
c003ec21 DH |
186 | /* |
187 | * This sets the rumble bit on the given output report if rumble is | |
188 | * currently enabled. | |
189 | * \cmd1 must point to the second byte in the output report => &cmd[1] | |
190 | * This must be called on nearly every output report before passing it | |
191 | * into the output queue! | |
192 | */ | |
193 | static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1) | |
194 | { | |
195 | if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE) | |
196 | *cmd1 |= 0x01; | |
197 | } | |
198 | ||
199 | static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble) | |
200 | { | |
201 | __u8 cmd[2]; | |
202 | ||
203 | rumble = !!rumble; | |
204 | if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE)) | |
205 | return; | |
206 | ||
207 | if (rumble) | |
208 | wdata->state.flags |= WIIPROTO_FLAG_RUMBLE; | |
209 | else | |
210 | wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE; | |
211 | ||
212 | cmd[0] = WIIPROTO_REQ_RUMBLE; | |
213 | cmd[1] = 0; | |
214 | ||
215 | wiiproto_keep_rumble(wdata, &cmd[1]); | |
216 | wiimote_queue(wdata, cmd, sizeof(cmd)); | |
217 | } | |
218 | ||
db308346 DH |
219 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
220 | { | |
221 | __u8 cmd[2]; | |
222 | ||
32a0d9a5 DH |
223 | leds &= WIIPROTO_FLAGS_LEDS; |
224 | if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds) | |
225 | return; | |
226 | wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds; | |
227 | ||
db308346 DH |
228 | cmd[0] = WIIPROTO_REQ_LED; |
229 | cmd[1] = 0; | |
230 | ||
231 | if (leds & WIIPROTO_FLAG_LED1) | |
232 | cmd[1] |= 0x10; | |
233 | if (leds & WIIPROTO_FLAG_LED2) | |
234 | cmd[1] |= 0x20; | |
235 | if (leds & WIIPROTO_FLAG_LED3) | |
236 | cmd[1] |= 0x40; | |
237 | if (leds & WIIPROTO_FLAG_LED4) | |
238 | cmd[1] |= 0x80; | |
239 | ||
c003ec21 | 240 | wiiproto_keep_rumble(wdata, &cmd[1]); |
db308346 DH |
241 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
242 | } | |
243 | ||
2cb5e4bc DH |
244 | /* |
245 | * Check what peripherals of the wiimote are currently | |
246 | * active and select a proper DRM that supports all of | |
247 | * the requested data inputs. | |
248 | */ | |
249 | static __u8 select_drm(struct wiimote_data *wdata) | |
250 | { | |
efcf9188 DH |
251 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) |
252 | return WIIPROTO_REQ_DRM_KA; | |
253 | else | |
254 | return WIIPROTO_REQ_DRM_K; | |
2cb5e4bc DH |
255 | } |
256 | ||
257 | static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) | |
258 | { | |
259 | __u8 cmd[3]; | |
260 | ||
261 | if (drm == WIIPROTO_REQ_NULL) | |
262 | drm = select_drm(wdata); | |
263 | ||
264 | cmd[0] = WIIPROTO_REQ_DRM; | |
265 | cmd[1] = 0; | |
266 | cmd[2] = drm; | |
267 | ||
c003ec21 | 268 | wiiproto_keep_rumble(wdata, &cmd[1]); |
2cb5e4bc DH |
269 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
270 | } | |
271 | ||
98a558ae DH |
272 | static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel) |
273 | { | |
274 | accel = !!accel; | |
275 | if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) | |
276 | return; | |
277 | ||
278 | if (accel) | |
279 | wdata->state.flags |= WIIPROTO_FLAG_ACCEL; | |
280 | else | |
281 | wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL; | |
282 | ||
283 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | |
284 | } | |
285 | ||
23a5a4a3 DH |
286 | static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev) |
287 | { | |
288 | struct wiimote_data *wdata; | |
289 | struct device *dev = led_dev->dev->parent; | |
290 | int i; | |
291 | unsigned long flags; | |
292 | bool value = false; | |
293 | ||
294 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); | |
295 | ||
296 | for (i = 0; i < 4; ++i) { | |
297 | if (wdata->leds[i] == led_dev) { | |
298 | spin_lock_irqsave(&wdata->state.lock, flags); | |
299 | value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1); | |
300 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
301 | break; | |
302 | } | |
303 | } | |
304 | ||
305 | return value ? LED_FULL : LED_OFF; | |
306 | } | |
307 | ||
308 | static void wiimote_leds_set(struct led_classdev *led_dev, | |
309 | enum led_brightness value) | |
310 | { | |
311 | struct wiimote_data *wdata; | |
312 | struct device *dev = led_dev->dev->parent; | |
313 | int i; | |
314 | unsigned long flags; | |
315 | __u8 state, flag; | |
316 | ||
317 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); | |
318 | ||
319 | for (i = 0; i < 4; ++i) { | |
320 | if (wdata->leds[i] == led_dev) { | |
321 | flag = WIIPROTO_FLAG_LED(i + 1); | |
322 | spin_lock_irqsave(&wdata->state.lock, flags); | |
323 | state = wdata->state.flags; | |
324 | if (value == LED_OFF) | |
325 | wiiproto_req_leds(wdata, state & ~flag); | |
326 | else | |
327 | wiiproto_req_leds(wdata, state | flag); | |
328 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
329 | break; | |
330 | } | |
331 | } | |
332 | } | |
3c1c2fce | 333 | |
d020be92 DH |
334 | static int wiimote_ff_play(struct input_dev *dev, void *data, |
335 | struct ff_effect *eff) | |
672bc4e0 | 336 | { |
d020be92 DH |
337 | struct wiimote_data *wdata = input_get_drvdata(dev); |
338 | __u8 value; | |
339 | unsigned long flags; | |
340 | ||
341 | /* | |
342 | * The wiimote supports only a single rumble motor so if any magnitude | |
343 | * is set to non-zero then we start the rumble motor. If both are set to | |
344 | * zero, we stop the rumble motor. | |
345 | */ | |
346 | ||
347 | if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) | |
348 | value = 1; | |
349 | else | |
350 | value = 0; | |
351 | ||
352 | spin_lock_irqsave(&wdata->state.lock, flags); | |
353 | wiiproto_req_rumble(wdata, value); | |
354 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
355 | ||
672bc4e0 DH |
356 | return 0; |
357 | } | |
358 | ||
26af1748 DH |
359 | static int wiimote_input_open(struct input_dev *dev) |
360 | { | |
361 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
362 | ||
363 | return hid_hw_open(wdata->hdev); | |
364 | } | |
365 | ||
366 | static void wiimote_input_close(struct input_dev *dev) | |
367 | { | |
368 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
369 | ||
370 | hid_hw_close(wdata->hdev); | |
371 | } | |
372 | ||
98a558ae DH |
373 | static int wiimote_accel_open(struct input_dev *dev) |
374 | { | |
375 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
376 | int ret; | |
377 | unsigned long flags; | |
378 | ||
379 | ret = hid_hw_open(wdata->hdev); | |
380 | if (ret) | |
381 | return ret; | |
382 | ||
383 | spin_lock_irqsave(&wdata->state.lock, flags); | |
384 | wiiproto_req_accel(wdata, true); | |
385 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | static void wiimote_accel_close(struct input_dev *dev) | |
391 | { | |
392 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
393 | unsigned long flags; | |
394 | ||
395 | spin_lock_irqsave(&wdata->state.lock, flags); | |
396 | wiiproto_req_accel(wdata, false); | |
397 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
398 | ||
399 | hid_hw_close(wdata->hdev); | |
400 | } | |
401 | ||
1abb9ad3 DH |
402 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
403 | { | |
404 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], | |
405 | !!(payload[0] & 0x01)); | |
406 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], | |
407 | !!(payload[0] & 0x02)); | |
408 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], | |
409 | !!(payload[0] & 0x04)); | |
410 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], | |
411 | !!(payload[0] & 0x08)); | |
412 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], | |
413 | !!(payload[0] & 0x10)); | |
414 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], | |
415 | !!(payload[1] & 0x01)); | |
416 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], | |
417 | !!(payload[1] & 0x02)); | |
418 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], | |
419 | !!(payload[1] & 0x04)); | |
420 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], | |
421 | !!(payload[1] & 0x08)); | |
422 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], | |
423 | !!(payload[1] & 0x10)); | |
424 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], | |
425 | !!(payload[1] & 0x80)); | |
426 | input_sync(wdata->input); | |
427 | } | |
428 | ||
efcf9188 DH |
429 | static void handler_accel(struct wiimote_data *wdata, const __u8 *payload) |
430 | { | |
431 | __u16 x, y, z; | |
432 | ||
433 | if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) | |
434 | return; | |
435 | ||
436 | /* | |
437 | * payload is: BB BB XX YY ZZ | |
438 | * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ | |
439 | * contain the upper 8 bits of each value. The lower 2 bits are | |
440 | * contained in the buttons data BB BB. | |
441 | * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the | |
442 | * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y | |
443 | * accel value and bit 6 is the second bit of the Z value. | |
444 | * The first bit of Y and Z values is not available and always set to 0. | |
445 | * 0x200 is returned on no movement. | |
446 | */ | |
447 | ||
448 | x = payload[2] << 2; | |
449 | y = payload[3] << 2; | |
450 | z = payload[4] << 2; | |
451 | ||
452 | x |= (payload[0] >> 5) & 0x3; | |
453 | y |= (payload[1] >> 4) & 0x2; | |
454 | z |= (payload[1] >> 5) & 0x2; | |
455 | ||
456 | input_report_abs(wdata->accel, ABS_RX, x - 0x200); | |
457 | input_report_abs(wdata->accel, ABS_RY, y - 0x200); | |
458 | input_report_abs(wdata->accel, ABS_RZ, z - 0x200); | |
459 | input_sync(wdata->accel); | |
460 | } | |
461 | ||
462 | ||
c87019e4 DH |
463 | static void handler_status(struct wiimote_data *wdata, const __u8 *payload) |
464 | { | |
465 | handler_keys(wdata, payload); | |
466 | ||
467 | /* on status reports the drm is reset so we need to resend the drm */ | |
468 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | |
469 | } | |
470 | ||
471 | static void handler_return(struct wiimote_data *wdata, const __u8 *payload) | |
472 | { | |
473 | __u8 err = payload[3]; | |
474 | __u8 cmd = payload[2]; | |
475 | ||
476 | handler_keys(wdata, payload); | |
477 | ||
478 | if (err) | |
479 | hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err, | |
480 | cmd); | |
481 | } | |
482 | ||
efcf9188 DH |
483 | static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload) |
484 | { | |
485 | handler_keys(wdata, payload); | |
486 | handler_accel(wdata, payload); | |
487 | } | |
488 | ||
489 | static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload) | |
490 | { | |
491 | handler_keys(wdata, payload); | |
492 | handler_accel(wdata, payload); | |
493 | } | |
494 | ||
495 | static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload) | |
496 | { | |
497 | handler_keys(wdata, payload); | |
498 | handler_accel(wdata, payload); | |
499 | } | |
500 | ||
501 | static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload) | |
502 | { | |
503 | handler_keys(wdata, payload); | |
504 | handler_accel(wdata, payload); | |
505 | } | |
506 | ||
507 | static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload) | |
508 | { | |
509 | handler_keys(wdata, payload); | |
510 | ||
511 | wdata->state.accel_split[0] = payload[2]; | |
512 | wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20); | |
513 | wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80); | |
514 | } | |
515 | ||
516 | static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload) | |
517 | { | |
518 | __u8 buf[5]; | |
519 | ||
520 | handler_keys(wdata, payload); | |
521 | ||
522 | wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02); | |
523 | wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08); | |
524 | ||
525 | buf[0] = 0; | |
526 | buf[1] = 0; | |
527 | buf[2] = wdata->state.accel_split[0]; | |
528 | buf[3] = payload[2]; | |
529 | buf[4] = wdata->state.accel_split[1]; | |
530 | handler_accel(wdata, buf); | |
531 | } | |
532 | ||
a4d19197 DH |
533 | struct wiiproto_handler { |
534 | __u8 id; | |
535 | size_t size; | |
536 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); | |
537 | }; | |
538 | ||
539 | static struct wiiproto_handler handlers[] = { | |
c87019e4 DH |
540 | { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status }, |
541 | { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return }, | |
1abb9ad3 | 542 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
efcf9188 DH |
543 | { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA }, |
544 | { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI }, | |
545 | { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE }, | |
546 | { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE }, | |
547 | { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 }, | |
548 | { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 }, | |
a4d19197 DH |
549 | { .id = 0 } |
550 | }; | |
551 | ||
02fb72a0 DH |
552 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
553 | u8 *raw_data, int size) | |
554 | { | |
4d36e975 | 555 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
a4d19197 DH |
556 | struct wiiproto_handler *h; |
557 | int i; | |
32a0d9a5 | 558 | unsigned long flags; |
4d36e975 | 559 | |
02fb72a0 DH |
560 | if (size < 1) |
561 | return -EINVAL; | |
562 | ||
32a0d9a5 DH |
563 | spin_lock_irqsave(&wdata->state.lock, flags); |
564 | ||
a4d19197 DH |
565 | for (i = 0; handlers[i].id; ++i) { |
566 | h = &handlers[i]; | |
567 | if (h->id == raw_data[0] && h->size < size) | |
568 | h->func(wdata, &raw_data[1]); | |
569 | } | |
570 | ||
32a0d9a5 DH |
571 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
572 | ||
02fb72a0 DH |
573 | return 0; |
574 | } | |
575 | ||
23a5a4a3 DH |
576 | static void wiimote_leds_destroy(struct wiimote_data *wdata) |
577 | { | |
578 | int i; | |
579 | struct led_classdev *led; | |
580 | ||
581 | for (i = 0; i < 4; ++i) { | |
582 | if (wdata->leds[i]) { | |
583 | led = wdata->leds[i]; | |
584 | wdata->leds[i] = NULL; | |
585 | led_classdev_unregister(led); | |
586 | kfree(led); | |
587 | } | |
588 | } | |
589 | } | |
590 | ||
591 | static int wiimote_leds_create(struct wiimote_data *wdata) | |
592 | { | |
593 | int i, ret; | |
594 | struct device *dev = &wdata->hdev->dev; | |
595 | size_t namesz = strlen(dev_name(dev)) + 9; | |
596 | struct led_classdev *led; | |
597 | char *name; | |
598 | ||
599 | for (i = 0; i < 4; ++i) { | |
600 | led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL); | |
601 | if (!led) { | |
602 | ret = -ENOMEM; | |
603 | goto err; | |
604 | } | |
605 | name = (void*)&led[1]; | |
606 | snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i); | |
607 | led->name = name; | |
608 | led->brightness = 0; | |
609 | led->max_brightness = 1; | |
610 | led->brightness_get = wiimote_leds_get; | |
611 | led->brightness_set = wiimote_leds_set; | |
612 | ||
613 | ret = led_classdev_register(dev, led); | |
614 | if (ret) { | |
615 | kfree(led); | |
616 | goto err; | |
617 | } | |
618 | wdata->leds[i] = led; | |
619 | } | |
620 | ||
621 | return 0; | |
622 | ||
623 | err: | |
624 | wiimote_leds_destroy(wdata); | |
625 | return ret; | |
626 | } | |
627 | ||
e894d0e3 DH |
628 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
629 | { | |
630 | struct wiimote_data *wdata; | |
1abb9ad3 | 631 | int i; |
e894d0e3 DH |
632 | |
633 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); | |
634 | if (!wdata) | |
635 | return NULL; | |
636 | ||
672bc4e0 | 637 | wdata->input = input_allocate_device(); |
98a558ae DH |
638 | if (!wdata->input) |
639 | goto err; | |
672bc4e0 | 640 | |
e894d0e3 DH |
641 | wdata->hdev = hdev; |
642 | hid_set_drvdata(hdev, wdata); | |
643 | ||
672bc4e0 | 644 | input_set_drvdata(wdata->input, wdata); |
26af1748 DH |
645 | wdata->input->open = wiimote_input_open; |
646 | wdata->input->close = wiimote_input_close; | |
672bc4e0 DH |
647 | wdata->input->dev.parent = &wdata->hdev->dev; |
648 | wdata->input->id.bustype = wdata->hdev->bus; | |
649 | wdata->input->id.vendor = wdata->hdev->vendor; | |
650 | wdata->input->id.product = wdata->hdev->product; | |
651 | wdata->input->id.version = wdata->hdev->version; | |
652 | wdata->input->name = WIIMOTE_NAME; | |
653 | ||
1abb9ad3 DH |
654 | set_bit(EV_KEY, wdata->input->evbit); |
655 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) | |
656 | set_bit(wiiproto_keymap[i], wdata->input->keybit); | |
657 | ||
d020be92 | 658 | set_bit(FF_RUMBLE, wdata->input->ffbit); |
98a558ae DH |
659 | if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play)) |
660 | goto err_input; | |
661 | ||
662 | wdata->accel = input_allocate_device(); | |
663 | if (!wdata->accel) | |
664 | goto err_input; | |
665 | ||
666 | input_set_drvdata(wdata->accel, wdata); | |
667 | wdata->accel->open = wiimote_accel_open; | |
668 | wdata->accel->close = wiimote_accel_close; | |
669 | wdata->accel->dev.parent = &wdata->hdev->dev; | |
670 | wdata->accel->id.bustype = wdata->hdev->bus; | |
671 | wdata->accel->id.vendor = wdata->hdev->vendor; | |
672 | wdata->accel->id.product = wdata->hdev->product; | |
673 | wdata->accel->id.version = wdata->hdev->version; | |
674 | wdata->accel->name = WIIMOTE_NAME " Accelerometer"; | |
675 | ||
676 | set_bit(EV_ABS, wdata->accel->evbit); | |
677 | set_bit(ABS_RX, wdata->accel->absbit); | |
678 | set_bit(ABS_RY, wdata->accel->absbit); | |
679 | set_bit(ABS_RZ, wdata->accel->absbit); | |
680 | input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4); | |
681 | input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4); | |
682 | input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4); | |
d020be92 | 683 | |
23c063cb DH |
684 | spin_lock_init(&wdata->qlock); |
685 | INIT_WORK(&wdata->worker, wiimote_worker); | |
686 | ||
32a0d9a5 DH |
687 | spin_lock_init(&wdata->state.lock); |
688 | ||
e894d0e3 | 689 | return wdata; |
98a558ae DH |
690 | |
691 | err_input: | |
692 | input_free_device(wdata->input); | |
693 | err: | |
694 | kfree(wdata); | |
695 | return NULL; | |
e894d0e3 DH |
696 | } |
697 | ||
698 | static void wiimote_destroy(struct wiimote_data *wdata) | |
699 | { | |
23a5a4a3 | 700 | wiimote_leds_destroy(wdata); |
3989ef6c | 701 | |
98a558ae | 702 | input_unregister_device(wdata->accel); |
3989ef6c DH |
703 | input_unregister_device(wdata->input); |
704 | cancel_work_sync(&wdata->worker); | |
705 | hid_hw_stop(wdata->hdev); | |
706 | ||
e894d0e3 DH |
707 | kfree(wdata); |
708 | } | |
709 | ||
02fb72a0 DH |
710 | static int wiimote_hid_probe(struct hid_device *hdev, |
711 | const struct hid_device_id *id) | |
fb51b443 | 712 | { |
e894d0e3 | 713 | struct wiimote_data *wdata; |
02fb72a0 DH |
714 | int ret; |
715 | ||
e894d0e3 DH |
716 | wdata = wiimote_create(hdev); |
717 | if (!wdata) { | |
718 | hid_err(hdev, "Can't alloc device\n"); | |
719 | return -ENOMEM; | |
720 | } | |
721 | ||
02fb72a0 DH |
722 | ret = hid_parse(hdev); |
723 | if (ret) { | |
724 | hid_err(hdev, "HID parse failed\n"); | |
e894d0e3 | 725 | goto err; |
02fb72a0 DH |
726 | } |
727 | ||
728 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); | |
729 | if (ret) { | |
730 | hid_err(hdev, "HW start failed\n"); | |
e894d0e3 | 731 | goto err; |
02fb72a0 DH |
732 | } |
733 | ||
98a558ae | 734 | ret = input_register_device(wdata->accel); |
672bc4e0 DH |
735 | if (ret) { |
736 | hid_err(hdev, "Cannot register input device\n"); | |
737 | goto err_stop; | |
738 | } | |
739 | ||
98a558ae DH |
740 | ret = input_register_device(wdata->input); |
741 | if (ret) { | |
742 | hid_err(hdev, "Cannot register input device\n"); | |
743 | goto err_input; | |
744 | } | |
745 | ||
23a5a4a3 | 746 | ret = wiimote_leds_create(wdata); |
3989ef6c DH |
747 | if (ret) |
748 | goto err_free; | |
749 | ||
02fb72a0 | 750 | hid_info(hdev, "New device registered\n"); |
32a0d9a5 DH |
751 | |
752 | /* by default set led1 after device initialization */ | |
753 | spin_lock_irq(&wdata->state.lock); | |
db308346 | 754 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
32a0d9a5 DH |
755 | spin_unlock_irq(&wdata->state.lock); |
756 | ||
fb51b443 | 757 | return 0; |
e894d0e3 | 758 | |
3989ef6c DH |
759 | err_free: |
760 | wiimote_destroy(wdata); | |
761 | return ret; | |
762 | ||
98a558ae DH |
763 | err_input: |
764 | input_unregister_device(wdata->accel); | |
765 | wdata->accel = NULL; | |
672bc4e0 DH |
766 | err_stop: |
767 | hid_hw_stop(hdev); | |
e894d0e3 | 768 | err: |
98a558ae | 769 | input_free_device(wdata->accel); |
672bc4e0 | 770 | input_free_device(wdata->input); |
3989ef6c | 771 | kfree(wdata); |
e894d0e3 | 772 | return ret; |
fb51b443 DH |
773 | } |
774 | ||
02fb72a0 DH |
775 | static void wiimote_hid_remove(struct hid_device *hdev) |
776 | { | |
e894d0e3 DH |
777 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
778 | ||
02fb72a0 | 779 | hid_info(hdev, "Device removed\n"); |
e894d0e3 | 780 | wiimote_destroy(wdata); |
02fb72a0 DH |
781 | } |
782 | ||
783 | static const struct hid_device_id wiimote_hid_devices[] = { | |
784 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, | |
785 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, | |
786 | { } | |
787 | }; | |
788 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); | |
789 | ||
790 | static struct hid_driver wiimote_hid_driver = { | |
791 | .name = "wiimote", | |
792 | .id_table = wiimote_hid_devices, | |
793 | .probe = wiimote_hid_probe, | |
794 | .remove = wiimote_hid_remove, | |
795 | .raw_event = wiimote_hid_event, | |
796 | }; | |
797 | ||
798 | static int __init wiimote_init(void) | |
799 | { | |
800 | int ret; | |
801 | ||
802 | ret = hid_register_driver(&wiimote_hid_driver); | |
803 | if (ret) | |
804 | pr_err("Can't register wiimote hid driver\n"); | |
805 | ||
806 | return ret; | |
807 | } | |
808 | ||
fb51b443 DH |
809 | static void __exit wiimote_exit(void) |
810 | { | |
02fb72a0 | 811 | hid_unregister_driver(&wiimote_hid_driver); |
fb51b443 DH |
812 | } |
813 | ||
814 | module_init(wiimote_init); | |
815 | module_exit(wiimote_exit); | |
816 | MODULE_LICENSE("GPL"); | |
817 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); | |
818 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); | |
819 | MODULE_VERSION(WIIMOTE_VERSION); |