diff options
Diffstat (limited to 'drivers/hid/hid-wiimote-core.c')
-rw-r--r-- | drivers/hid/hid-wiimote-core.c | 1316 |
1 files changed, 1316 insertions, 0 deletions
diff --git a/drivers/hid/hid-wiimote-core.c b/drivers/hid/hid-wiimote-core.c new file mode 100644 index 00000000000..61881b35c67 --- /dev/null +++ b/drivers/hid/hid-wiimote-core.c | |||
@@ -0,0 +1,1316 @@ | |||
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 | |||
13 | #include <linux/completion.h> | ||
14 | #include <linux/device.h> | ||
15 | #include <linux/hid.h> | ||
16 | #include <linux/input.h> | ||
17 | #include <linux/leds.h> | ||
18 | #include <linux/module.h> | ||
19 | #include <linux/mutex.h> | ||
20 | #include <linux/power_supply.h> | ||
21 | #include <linux/spinlock.h> | ||
22 | #include "hid-ids.h" | ||
23 | #include "hid-wiimote.h" | ||
24 | |||
25 | enum wiiproto_keys { | ||
26 | WIIPROTO_KEY_LEFT, | ||
27 | WIIPROTO_KEY_RIGHT, | ||
28 | WIIPROTO_KEY_UP, | ||
29 | WIIPROTO_KEY_DOWN, | ||
30 | WIIPROTO_KEY_PLUS, | ||
31 | WIIPROTO_KEY_MINUS, | ||
32 | WIIPROTO_KEY_ONE, | ||
33 | WIIPROTO_KEY_TWO, | ||
34 | WIIPROTO_KEY_A, | ||
35 | WIIPROTO_KEY_B, | ||
36 | WIIPROTO_KEY_HOME, | ||
37 | WIIPROTO_KEY_COUNT | ||
38 | }; | ||
39 | |||
40 | static __u16 wiiproto_keymap[] = { | ||
41 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ | ||
42 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ | ||
43 | KEY_UP, /* WIIPROTO_KEY_UP */ | ||
44 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ | ||
45 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ | ||
46 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ | ||
47 | BTN_1, /* WIIPROTO_KEY_ONE */ | ||
48 | BTN_2, /* WIIPROTO_KEY_TWO */ | ||
49 | BTN_A, /* WIIPROTO_KEY_A */ | ||
50 | BTN_B, /* WIIPROTO_KEY_B */ | ||
51 | BTN_MODE, /* WIIPROTO_KEY_HOME */ | ||
52 | }; | ||
53 | |||
54 | static enum power_supply_property wiimote_battery_props[] = { | ||
55 | POWER_SUPPLY_PROP_CAPACITY | ||
56 | }; | ||
57 | |||
58 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, | ||
59 | size_t count) | ||
60 | { | ||
61 | __u8 *buf; | ||
62 | ssize_t ret; | ||
63 | |||
64 | if (!hdev->hid_output_raw_report) | ||
65 | return -ENODEV; | ||
66 | |||
67 | buf = kmemdup(buffer, count, GFP_KERNEL); | ||
68 | if (!buf) | ||
69 | return -ENOMEM; | ||
70 | |||
71 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); | ||
72 | |||
73 | kfree(buf); | ||
74 | return ret; | ||
75 | } | ||
76 | |||
77 | static void wiimote_worker(struct work_struct *work) | ||
78 | { | ||
79 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, | ||
80 | worker); | ||
81 | unsigned long flags; | ||
82 | |||
83 | spin_lock_irqsave(&wdata->qlock, flags); | ||
84 | |||
85 | while (wdata->head != wdata->tail) { | ||
86 | spin_unlock_irqrestore(&wdata->qlock, flags); | ||
87 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, | ||
88 | wdata->outq[wdata->tail].size); | ||
89 | spin_lock_irqsave(&wdata->qlock, flags); | ||
90 | |||
91 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; | ||
92 | } | ||
93 | |||
94 | spin_unlock_irqrestore(&wdata->qlock, flags); | ||
95 | } | ||
96 | |||
97 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, | ||
98 | size_t count) | ||
99 | { | ||
100 | unsigned long flags; | ||
101 | __u8 newhead; | ||
102 | |||
103 | if (count > HID_MAX_BUFFER_SIZE) { | ||
104 | hid_warn(wdata->hdev, "Sending too large output report\n"); | ||
105 | return; | ||
106 | } | ||
107 | |||
108 | /* | ||
109 | * Copy new request into our output queue and check whether the | ||
110 | * queue is full. If it is full, discard this request. | ||
111 | * If it is empty we need to start a new worker that will | ||
112 | * send out the buffer to the hid device. | ||
113 | * If the queue is not empty, then there must be a worker | ||
114 | * that is currently sending out our buffer and this worker | ||
115 | * will reschedule itself until the queue is empty. | ||
116 | */ | ||
117 | |||
118 | spin_lock_irqsave(&wdata->qlock, flags); | ||
119 | |||
120 | memcpy(wdata->outq[wdata->head].data, buffer, count); | ||
121 | wdata->outq[wdata->head].size = count; | ||
122 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; | ||
123 | |||
124 | if (wdata->head == wdata->tail) { | ||
125 | wdata->head = newhead; | ||
126 | schedule_work(&wdata->worker); | ||
127 | } else if (newhead != wdata->tail) { | ||
128 | wdata->head = newhead; | ||
129 | } else { | ||
130 | hid_warn(wdata->hdev, "Output queue is full"); | ||
131 | } | ||
132 | |||
133 | spin_unlock_irqrestore(&wdata->qlock, flags); | ||
134 | } | ||
135 | |||
136 | /* | ||
137 | * This sets the rumble bit on the given output report if rumble is | ||
138 | * currently enabled. | ||
139 | * \cmd1 must point to the second byte in the output report => &cmd[1] | ||
140 | * This must be called on nearly every output report before passing it | ||
141 | * into the output queue! | ||
142 | */ | ||
143 | static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1) | ||
144 | { | ||
145 | if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE) | ||
146 | *cmd1 |= 0x01; | ||
147 | } | ||
148 | |||
149 | static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble) | ||
150 | { | ||
151 | __u8 cmd[2]; | ||
152 | |||
153 | rumble = !!rumble; | ||
154 | if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE)) | ||
155 | return; | ||
156 | |||
157 | if (rumble) | ||
158 | wdata->state.flags |= WIIPROTO_FLAG_RUMBLE; | ||
159 | else | ||
160 | wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE; | ||
161 | |||
162 | cmd[0] = WIIPROTO_REQ_RUMBLE; | ||
163 | cmd[1] = 0; | ||
164 | |||
165 | wiiproto_keep_rumble(wdata, &cmd[1]); | ||
166 | wiimote_queue(wdata, cmd, sizeof(cmd)); | ||
167 | } | ||
168 | |||
169 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) | ||
170 | { | ||
171 | __u8 cmd[2]; | ||
172 | |||
173 | leds &= WIIPROTO_FLAGS_LEDS; | ||
174 | if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds) | ||
175 | return; | ||
176 | wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds; | ||
177 | |||
178 | cmd[0] = WIIPROTO_REQ_LED; | ||
179 | cmd[1] = 0; | ||
180 | |||
181 | if (leds & WIIPROTO_FLAG_LED1) | ||
182 | cmd[1] |= 0x10; | ||
183 | if (leds & WIIPROTO_FLAG_LED2) | ||
184 | cmd[1] |= 0x20; | ||
185 | if (leds & WIIPROTO_FLAG_LED3) | ||
186 | cmd[1] |= 0x40; | ||
187 | if (leds & WIIPROTO_FLAG_LED4) | ||
188 | cmd[1] |= 0x80; | ||
189 | |||
190 | wiiproto_keep_rumble(wdata, &cmd[1]); | ||
191 | wiimote_queue(wdata, cmd, sizeof(cmd)); | ||
192 | } | ||
193 | |||
194 | /* | ||
195 | * Check what peripherals of the wiimote are currently | ||
196 | * active and select a proper DRM that supports all of | ||
197 | * the requested data inputs. | ||
198 | */ | ||
199 | static __u8 select_drm(struct wiimote_data *wdata) | ||
200 | { | ||
201 | __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR; | ||
202 | bool ext = wiiext_active(wdata); | ||
203 | |||
204 | if (ir == WIIPROTO_FLAG_IR_BASIC) { | ||
205 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) | ||
206 | return WIIPROTO_REQ_DRM_KAIE; | ||
207 | else | ||
208 | return WIIPROTO_REQ_DRM_KIE; | ||
209 | } else if (ir == WIIPROTO_FLAG_IR_EXT) { | ||
210 | return WIIPROTO_REQ_DRM_KAI; | ||
211 | } else if (ir == WIIPROTO_FLAG_IR_FULL) { | ||
212 | return WIIPROTO_REQ_DRM_SKAI1; | ||
213 | } else { | ||
214 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) { | ||
215 | if (ext) | ||
216 | return WIIPROTO_REQ_DRM_KAE; | ||
217 | else | ||
218 | return WIIPROTO_REQ_DRM_KA; | ||
219 | } else { | ||
220 | if (ext) | ||
221 | return WIIPROTO_REQ_DRM_KE; | ||
222 | else | ||
223 | return WIIPROTO_REQ_DRM_K; | ||
224 | } | ||
225 | } | ||
226 | } | ||
227 | |||
228 | void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) | ||
229 | { | ||
230 | __u8 cmd[3]; | ||
231 | |||
232 | if (drm == WIIPROTO_REQ_NULL) | ||
233 | drm = select_drm(wdata); | ||
234 | |||
235 | cmd[0] = WIIPROTO_REQ_DRM; | ||
236 | cmd[1] = 0; | ||
237 | cmd[2] = drm; | ||
238 | |||
239 | wdata->state.drm = drm; | ||
240 | wiiproto_keep_rumble(wdata, &cmd[1]); | ||
241 | wiimote_queue(wdata, cmd, sizeof(cmd)); | ||
242 | } | ||
243 | |||
244 | static void wiiproto_req_status(struct wiimote_data *wdata) | ||
245 | { | ||
246 | __u8 cmd[2]; | ||
247 | |||
248 | cmd[0] = WIIPROTO_REQ_SREQ; | ||
249 | cmd[1] = 0; | ||
250 | |||
251 | wiiproto_keep_rumble(wdata, &cmd[1]); | ||
252 | wiimote_queue(wdata, cmd, sizeof(cmd)); | ||
253 | } | ||
254 | |||
255 | static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel) | ||
256 | { | ||
257 | accel = !!accel; | ||
258 | if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) | ||
259 | return; | ||
260 | |||
261 | if (accel) | ||
262 | wdata->state.flags |= WIIPROTO_FLAG_ACCEL; | ||
263 | else | ||
264 | wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL; | ||
265 | |||
266 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | ||
267 | } | ||
268 | |||
269 | static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags) | ||
270 | { | ||
271 | __u8 cmd[2]; | ||
272 | |||
273 | cmd[0] = WIIPROTO_REQ_IR1; | ||
274 | cmd[1] = flags; | ||
275 | |||
276 | wiiproto_keep_rumble(wdata, &cmd[1]); | ||
277 | wiimote_queue(wdata, cmd, sizeof(cmd)); | ||
278 | } | ||
279 | |||
280 | static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags) | ||
281 | { | ||
282 | __u8 cmd[2]; | ||
283 | |||
284 | cmd[0] = WIIPROTO_REQ_IR2; | ||
285 | cmd[1] = flags; | ||
286 | |||
287 | wiiproto_keep_rumble(wdata, &cmd[1]); | ||
288 | wiimote_queue(wdata, cmd, sizeof(cmd)); | ||
289 | } | ||
290 | |||
291 | #define wiiproto_req_wreg(wdata, os, buf, sz) \ | ||
292 | wiiproto_req_wmem((wdata), false, (os), (buf), (sz)) | ||
293 | |||
294 | #define wiiproto_req_weeprom(wdata, os, buf, sz) \ | ||
295 | wiiproto_req_wmem((wdata), true, (os), (buf), (sz)) | ||
296 | |||
297 | static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom, | ||
298 | __u32 offset, const __u8 *buf, __u8 size) | ||
299 | { | ||
300 | __u8 cmd[22]; | ||
301 | |||
302 | if (size > 16 || size == 0) { | ||
303 | hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size); | ||
304 | return; | ||
305 | } | ||
306 | |||
307 | memset(cmd, 0, sizeof(cmd)); | ||
308 | cmd[0] = WIIPROTO_REQ_WMEM; | ||
309 | cmd[2] = (offset >> 16) & 0xff; | ||
310 | cmd[3] = (offset >> 8) & 0xff; | ||
311 | cmd[4] = offset & 0xff; | ||
312 | cmd[5] = size; | ||
313 | memcpy(&cmd[6], buf, size); | ||
314 | |||
315 | if (!eeprom) | ||
316 | cmd[1] |= 0x04; | ||
317 | |||
318 | wiiproto_keep_rumble(wdata, &cmd[1]); | ||
319 | wiimote_queue(wdata, cmd, sizeof(cmd)); | ||
320 | } | ||
321 | |||
322 | void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset, | ||
323 | __u16 size) | ||
324 | { | ||
325 | __u8 cmd[7]; | ||
326 | |||
327 | if (size == 0) { | ||
328 | hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size); | ||
329 | return; | ||
330 | } | ||
331 | |||
332 | cmd[0] = WIIPROTO_REQ_RMEM; | ||
333 | cmd[1] = 0; | ||
334 | cmd[2] = (offset >> 16) & 0xff; | ||
335 | cmd[3] = (offset >> 8) & 0xff; | ||
336 | cmd[4] = offset & 0xff; | ||
337 | cmd[5] = (size >> 8) & 0xff; | ||
338 | cmd[6] = size & 0xff; | ||
339 | |||
340 | if (!eeprom) | ||
341 | cmd[1] |= 0x04; | ||
342 | |||
343 | wiiproto_keep_rumble(wdata, &cmd[1]); | ||
344 | wiimote_queue(wdata, cmd, sizeof(cmd)); | ||
345 | } | ||
346 | |||
347 | /* requries the cmd-mutex to be held */ | ||
348 | int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, | ||
349 | const __u8 *wmem, __u8 size) | ||
350 | { | ||
351 | unsigned long flags; | ||
352 | int ret; | ||
353 | |||
354 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
355 | wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0); | ||
356 | wiiproto_req_wreg(wdata, offset, wmem, size); | ||
357 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
358 | |||
359 | ret = wiimote_cmd_wait(wdata); | ||
360 | if (!ret && wdata->state.cmd_err) | ||
361 | ret = -EIO; | ||
362 | |||
363 | return ret; | ||
364 | } | ||
365 | |||
366 | /* requries the cmd-mutex to be held */ | ||
367 | ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem, | ||
368 | __u8 size) | ||
369 | { | ||
370 | unsigned long flags; | ||
371 | ssize_t ret; | ||
372 | |||
373 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
374 | wdata->state.cmd_read_size = size; | ||
375 | wdata->state.cmd_read_buf = rmem; | ||
376 | wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff); | ||
377 | wiiproto_req_rreg(wdata, offset, size); | ||
378 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
379 | |||
380 | ret = wiimote_cmd_wait(wdata); | ||
381 | |||
382 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
383 | wdata->state.cmd_read_buf = NULL; | ||
384 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
385 | |||
386 | if (!ret) { | ||
387 | if (wdata->state.cmd_read_size == 0) | ||
388 | ret = -EIO; | ||
389 | else | ||
390 | ret = wdata->state.cmd_read_size; | ||
391 | } | ||
392 | |||
393 | return ret; | ||
394 | } | ||
395 | |||
396 | static int wiimote_battery_get_property(struct power_supply *psy, | ||
397 | enum power_supply_property psp, | ||
398 | union power_supply_propval *val) | ||
399 | { | ||
400 | struct wiimote_data *wdata = container_of(psy, | ||
401 | struct wiimote_data, battery); | ||
402 | int ret = 0, state; | ||
403 | unsigned long flags; | ||
404 | |||
405 | ret = wiimote_cmd_acquire(wdata); | ||
406 | if (ret) | ||
407 | return ret; | ||
408 | |||
409 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
410 | wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0); | ||
411 | wiiproto_req_status(wdata); | ||
412 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
413 | |||
414 | ret = wiimote_cmd_wait(wdata); | ||
415 | state = wdata->state.cmd_battery; | ||
416 | wiimote_cmd_release(wdata); | ||
417 | |||
418 | if (ret) | ||
419 | return ret; | ||
420 | |||
421 | switch (psp) { | ||
422 | case POWER_SUPPLY_PROP_CAPACITY: | ||
423 | val->intval = state * 100 / 255; | ||
424 | break; | ||
425 | default: | ||
426 | ret = -EINVAL; | ||
427 | break; | ||
428 | } | ||
429 | |||
430 | return ret; | ||
431 | } | ||
432 | |||
433 | static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode) | ||
434 | { | ||
435 | int ret; | ||
436 | unsigned long flags; | ||
437 | __u8 format = 0; | ||
438 | static const __u8 data_enable[] = { 0x01 }; | ||
439 | static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01, | ||
440 | 0x00, 0xaa, 0x00, 0x64 }; | ||
441 | static const __u8 data_sens2[] = { 0x63, 0x03 }; | ||
442 | static const __u8 data_fin[] = { 0x08 }; | ||
443 | |||
444 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
445 | |||
446 | if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) { | ||
447 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
448 | return 0; | ||
449 | } | ||
450 | |||
451 | if (mode == 0) { | ||
452 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; | ||
453 | wiiproto_req_ir1(wdata, 0); | ||
454 | wiiproto_req_ir2(wdata, 0); | ||
455 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | ||
456 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
457 | return 0; | ||
458 | } | ||
459 | |||
460 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
461 | |||
462 | ret = wiimote_cmd_acquire(wdata); | ||
463 | if (ret) | ||
464 | return ret; | ||
465 | |||
466 | /* send PIXEL CLOCK ENABLE cmd first */ | ||
467 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
468 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0); | ||
469 | wiiproto_req_ir1(wdata, 0x06); | ||
470 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
471 | |||
472 | ret = wiimote_cmd_wait(wdata); | ||
473 | if (ret) | ||
474 | goto unlock; | ||
475 | if (wdata->state.cmd_err) { | ||
476 | ret = -EIO; | ||
477 | goto unlock; | ||
478 | } | ||
479 | |||
480 | /* enable IR LOGIC */ | ||
481 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
482 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0); | ||
483 | wiiproto_req_ir2(wdata, 0x06); | ||
484 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
485 | |||
486 | ret = wiimote_cmd_wait(wdata); | ||
487 | if (ret) | ||
488 | goto unlock; | ||
489 | if (wdata->state.cmd_err) { | ||
490 | ret = -EIO; | ||
491 | goto unlock; | ||
492 | } | ||
493 | |||
494 | /* enable IR cam but do not make it send data, yet */ | ||
495 | ret = wiimote_cmd_write(wdata, 0xb00030, data_enable, | ||
496 | sizeof(data_enable)); | ||
497 | if (ret) | ||
498 | goto unlock; | ||
499 | |||
500 | /* write first sensitivity block */ | ||
501 | ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1, | ||
502 | sizeof(data_sens1)); | ||
503 | if (ret) | ||
504 | goto unlock; | ||
505 | |||
506 | /* write second sensitivity block */ | ||
507 | ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2, | ||
508 | sizeof(data_sens2)); | ||
509 | if (ret) | ||
510 | goto unlock; | ||
511 | |||
512 | /* put IR cam into desired state */ | ||
513 | switch (mode) { | ||
514 | case WIIPROTO_FLAG_IR_FULL: | ||
515 | format = 5; | ||
516 | break; | ||
517 | case WIIPROTO_FLAG_IR_EXT: | ||
518 | format = 3; | ||
519 | break; | ||
520 | case WIIPROTO_FLAG_IR_BASIC: | ||
521 | format = 1; | ||
522 | break; | ||
523 | } | ||
524 | ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format)); | ||
525 | if (ret) | ||
526 | goto unlock; | ||
527 | |||
528 | /* make IR cam send data */ | ||
529 | ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin)); | ||
530 | if (ret) | ||
531 | goto unlock; | ||
532 | |||
533 | /* request new DRM mode compatible to IR mode */ | ||
534 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
535 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; | ||
536 | wdata->state.flags |= mode & WIIPROTO_FLAGS_IR; | ||
537 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | ||
538 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
539 | |||
540 | unlock: | ||
541 | wiimote_cmd_release(wdata); | ||
542 | return ret; | ||
543 | } | ||
544 | |||
545 | static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev) | ||
546 | { | ||
547 | struct wiimote_data *wdata; | ||
548 | struct device *dev = led_dev->dev->parent; | ||
549 | int i; | ||
550 | unsigned long flags; | ||
551 | bool value = false; | ||
552 | |||
553 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); | ||
554 | |||
555 | for (i = 0; i < 4; ++i) { | ||
556 | if (wdata->leds[i] == led_dev) { | ||
557 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
558 | value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1); | ||
559 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
560 | break; | ||
561 | } | ||
562 | } | ||
563 | |||
564 | return value ? LED_FULL : LED_OFF; | ||
565 | } | ||
566 | |||
567 | static void wiimote_leds_set(struct led_classdev *led_dev, | ||
568 | enum led_brightness value) | ||
569 | { | ||
570 | struct wiimote_data *wdata; | ||
571 | struct device *dev = led_dev->dev->parent; | ||
572 | int i; | ||
573 | unsigned long flags; | ||
574 | __u8 state, flag; | ||
575 | |||
576 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); | ||
577 | |||
578 | for (i = 0; i < 4; ++i) { | ||
579 | if (wdata->leds[i] == led_dev) { | ||
580 | flag = WIIPROTO_FLAG_LED(i + 1); | ||
581 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
582 | state = wdata->state.flags; | ||
583 | if (value == LED_OFF) | ||
584 | wiiproto_req_leds(wdata, state & ~flag); | ||
585 | else | ||
586 | wiiproto_req_leds(wdata, state | flag); | ||
587 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
588 | break; | ||
589 | } | ||
590 | } | ||
591 | } | ||
592 | |||
593 | static int wiimote_ff_play(struct input_dev *dev, void *data, | ||
594 | struct ff_effect *eff) | ||
595 | { | ||
596 | struct wiimote_data *wdata = input_get_drvdata(dev); | ||
597 | __u8 value; | ||
598 | unsigned long flags; | ||
599 | |||
600 | /* | ||
601 | * The wiimote supports only a single rumble motor so if any magnitude | ||
602 | * is set to non-zero then we start the rumble motor. If both are set to | ||
603 | * zero, we stop the rumble motor. | ||
604 | */ | ||
605 | |||
606 | if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) | ||
607 | value = 1; | ||
608 | else | ||
609 | value = 0; | ||
610 | |||
611 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
612 | wiiproto_req_rumble(wdata, value); | ||
613 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
614 | |||
615 | return 0; | ||
616 | } | ||
617 | |||
618 | static int wiimote_input_open(struct input_dev *dev) | ||
619 | { | ||
620 | struct wiimote_data *wdata = input_get_drvdata(dev); | ||
621 | |||
622 | return hid_hw_open(wdata->hdev); | ||
623 | } | ||
624 | |||
625 | static void wiimote_input_close(struct input_dev *dev) | ||
626 | { | ||
627 | struct wiimote_data *wdata = input_get_drvdata(dev); | ||
628 | |||
629 | hid_hw_close(wdata->hdev); | ||
630 | } | ||
631 | |||
632 | static int wiimote_accel_open(struct input_dev *dev) | ||
633 | { | ||
634 | struct wiimote_data *wdata = input_get_drvdata(dev); | ||
635 | int ret; | ||
636 | unsigned long flags; | ||
637 | |||
638 | ret = hid_hw_open(wdata->hdev); | ||
639 | if (ret) | ||
640 | return ret; | ||
641 | |||
642 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
643 | wiiproto_req_accel(wdata, true); | ||
644 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
645 | |||
646 | return 0; | ||
647 | } | ||
648 | |||
649 | static void wiimote_accel_close(struct input_dev *dev) | ||
650 | { | ||
651 | struct wiimote_data *wdata = input_get_drvdata(dev); | ||
652 | unsigned long flags; | ||
653 | |||
654 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
655 | wiiproto_req_accel(wdata, false); | ||
656 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
657 | |||
658 | hid_hw_close(wdata->hdev); | ||
659 | } | ||
660 | |||
661 | static int wiimote_ir_open(struct input_dev *dev) | ||
662 | { | ||
663 | struct wiimote_data *wdata = input_get_drvdata(dev); | ||
664 | int ret; | ||
665 | |||
666 | ret = hid_hw_open(wdata->hdev); | ||
667 | if (ret) | ||
668 | return ret; | ||
669 | |||
670 | ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC); | ||
671 | if (ret) { | ||
672 | hid_hw_close(wdata->hdev); | ||
673 | return ret; | ||
674 | } | ||
675 | |||
676 | return 0; | ||
677 | } | ||
678 | |||
679 | static void wiimote_ir_close(struct input_dev *dev) | ||
680 | { | ||
681 | struct wiimote_data *wdata = input_get_drvdata(dev); | ||
682 | |||
683 | wiimote_init_ir(wdata, 0); | ||
684 | hid_hw_close(wdata->hdev); | ||
685 | } | ||
686 | |||
687 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) | ||
688 | { | ||
689 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], | ||
690 | !!(payload[0] & 0x01)); | ||
691 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], | ||
692 | !!(payload[0] & 0x02)); | ||
693 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], | ||
694 | !!(payload[0] & 0x04)); | ||
695 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], | ||
696 | !!(payload[0] & 0x08)); | ||
697 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], | ||
698 | !!(payload[0] & 0x10)); | ||
699 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], | ||
700 | !!(payload[1] & 0x01)); | ||
701 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], | ||
702 | !!(payload[1] & 0x02)); | ||
703 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], | ||
704 | !!(payload[1] & 0x04)); | ||
705 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], | ||
706 | !!(payload[1] & 0x08)); | ||
707 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], | ||
708 | !!(payload[1] & 0x10)); | ||
709 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], | ||
710 | !!(payload[1] & 0x80)); | ||
711 | input_sync(wdata->input); | ||
712 | } | ||
713 | |||
714 | static void handler_accel(struct wiimote_data *wdata, const __u8 *payload) | ||
715 | { | ||
716 | __u16 x, y, z; | ||
717 | |||
718 | if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) | ||
719 | return; | ||
720 | |||
721 | /* | ||
722 | * payload is: BB BB XX YY ZZ | ||
723 | * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ | ||
724 | * contain the upper 8 bits of each value. The lower 2 bits are | ||
725 | * contained in the buttons data BB BB. | ||
726 | * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the | ||
727 | * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y | ||
728 | * accel value and bit 6 is the second bit of the Z value. | ||
729 | * The first bit of Y and Z values is not available and always set to 0. | ||
730 | * 0x200 is returned on no movement. | ||
731 | */ | ||
732 | |||
733 | x = payload[2] << 2; | ||
734 | y = payload[3] << 2; | ||
735 | z = payload[4] << 2; | ||
736 | |||
737 | x |= (payload[0] >> 5) & 0x3; | ||
738 | y |= (payload[1] >> 4) & 0x2; | ||
739 | z |= (payload[1] >> 5) & 0x2; | ||
740 | |||
741 | input_report_abs(wdata->accel, ABS_RX, x - 0x200); | ||
742 | input_report_abs(wdata->accel, ABS_RY, y - 0x200); | ||
743 | input_report_abs(wdata->accel, ABS_RZ, z - 0x200); | ||
744 | input_sync(wdata->accel); | ||
745 | } | ||
746 | |||
747 | #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ | ||
748 | ABS_HAT0X, ABS_HAT0Y) | ||
749 | #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ | ||
750 | ABS_HAT1X, ABS_HAT1Y) | ||
751 | #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ | ||
752 | ABS_HAT2X, ABS_HAT2Y) | ||
753 | #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ | ||
754 | ABS_HAT3X, ABS_HAT3Y) | ||
755 | |||
756 | static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir, | ||
757 | bool packed, __u8 xid, __u8 yid) | ||
758 | { | ||
759 | __u16 x, y; | ||
760 | |||
761 | if (!(wdata->state.flags & WIIPROTO_FLAGS_IR)) | ||
762 | return; | ||
763 | |||
764 | /* | ||
765 | * Basic IR data is encoded into 3 bytes. The first two bytes are the | ||
766 | * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits | ||
767 | * of both. | ||
768 | * If data is packed, then the 3rd byte is put first and slightly | ||
769 | * reordered. This allows to interleave packed and non-packed data to | ||
770 | * have two IR sets in 5 bytes instead of 6. | ||
771 | * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev. | ||
772 | */ | ||
773 | |||
774 | if (packed) { | ||
775 | x = ir[1] << 2; | ||
776 | y = ir[2] << 2; | ||
777 | |||
778 | x |= ir[0] & 0x3; | ||
779 | y |= (ir[0] >> 2) & 0x3; | ||
780 | } else { | ||
781 | x = ir[0] << 2; | ||
782 | y = ir[1] << 2; | ||
783 | |||
784 | x |= (ir[2] >> 4) & 0x3; | ||
785 | y |= (ir[2] >> 6) & 0x3; | ||
786 | } | ||
787 | |||
788 | input_report_abs(wdata->ir, xid, x); | ||
789 | input_report_abs(wdata->ir, yid, y); | ||
790 | } | ||
791 | |||
792 | static void handler_status(struct wiimote_data *wdata, const __u8 *payload) | ||
793 | { | ||
794 | handler_keys(wdata, payload); | ||
795 | |||
796 | /* on status reports the drm is reset so we need to resend the drm */ | ||
797 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | ||
798 | |||
799 | wiiext_event(wdata, payload[2] & 0x02); | ||
800 | |||
801 | if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) { | ||
802 | wdata->state.cmd_battery = payload[5]; | ||
803 | wiimote_cmd_complete(wdata); | ||
804 | } | ||
805 | } | ||
806 | |||
807 | static void handler_data(struct wiimote_data *wdata, const __u8 *payload) | ||
808 | { | ||
809 | __u16 offset = payload[3] << 8 | payload[4]; | ||
810 | __u8 size = (payload[2] >> 4) + 1; | ||
811 | __u8 err = payload[2] & 0x0f; | ||
812 | |||
813 | handler_keys(wdata, payload); | ||
814 | |||
815 | if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) { | ||
816 | if (err) | ||
817 | size = 0; | ||
818 | else if (size > wdata->state.cmd_read_size) | ||
819 | size = wdata->state.cmd_read_size; | ||
820 | |||
821 | wdata->state.cmd_read_size = size; | ||
822 | if (wdata->state.cmd_read_buf) | ||
823 | memcpy(wdata->state.cmd_read_buf, &payload[5], size); | ||
824 | wiimote_cmd_complete(wdata); | ||
825 | } | ||
826 | } | ||
827 | |||
828 | static void handler_return(struct wiimote_data *wdata, const __u8 *payload) | ||
829 | { | ||
830 | __u8 err = payload[3]; | ||
831 | __u8 cmd = payload[2]; | ||
832 | |||
833 | handler_keys(wdata, payload); | ||
834 | |||
835 | if (wiimote_cmd_pending(wdata, cmd, 0)) { | ||
836 | wdata->state.cmd_err = err; | ||
837 | wiimote_cmd_complete(wdata); | ||
838 | } else if (err) { | ||
839 | hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err, | ||
840 | cmd); | ||
841 | } | ||
842 | } | ||
843 | |||
844 | static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload) | ||
845 | { | ||
846 | handler_keys(wdata, payload); | ||
847 | handler_accel(wdata, payload); | ||
848 | } | ||
849 | |||
850 | static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload) | ||
851 | { | ||
852 | handler_keys(wdata, payload); | ||
853 | wiiext_handle(wdata, &payload[2]); | ||
854 | } | ||
855 | |||
856 | static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload) | ||
857 | { | ||
858 | handler_keys(wdata, payload); | ||
859 | handler_accel(wdata, payload); | ||
860 | ir_to_input0(wdata, &payload[5], false); | ||
861 | ir_to_input1(wdata, &payload[8], false); | ||
862 | ir_to_input2(wdata, &payload[11], false); | ||
863 | ir_to_input3(wdata, &payload[14], false); | ||
864 | input_sync(wdata->ir); | ||
865 | } | ||
866 | |||
867 | static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload) | ||
868 | { | ||
869 | handler_keys(wdata, payload); | ||
870 | wiiext_handle(wdata, &payload[2]); | ||
871 | } | ||
872 | |||
873 | static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload) | ||
874 | { | ||
875 | handler_keys(wdata, payload); | ||
876 | ir_to_input0(wdata, &payload[2], false); | ||
877 | ir_to_input1(wdata, &payload[4], true); | ||
878 | ir_to_input2(wdata, &payload[7], false); | ||
879 | ir_to_input3(wdata, &payload[9], true); | ||
880 | input_sync(wdata->ir); | ||
881 | wiiext_handle(wdata, &payload[12]); | ||
882 | } | ||
883 | |||
884 | static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload) | ||
885 | { | ||
886 | handler_keys(wdata, payload); | ||
887 | handler_accel(wdata, payload); | ||
888 | wiiext_handle(wdata, &payload[5]); | ||
889 | } | ||
890 | |||
891 | static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload) | ||
892 | { | ||
893 | handler_keys(wdata, payload); | ||
894 | handler_accel(wdata, payload); | ||
895 | ir_to_input0(wdata, &payload[5], false); | ||
896 | ir_to_input1(wdata, &payload[7], true); | ||
897 | ir_to_input2(wdata, &payload[10], false); | ||
898 | ir_to_input3(wdata, &payload[12], true); | ||
899 | input_sync(wdata->ir); | ||
900 | wiiext_handle(wdata, &payload[15]); | ||
901 | } | ||
902 | |||
903 | static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload) | ||
904 | { | ||
905 | wiiext_handle(wdata, payload); | ||
906 | } | ||
907 | |||
908 | static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload) | ||
909 | { | ||
910 | handler_keys(wdata, payload); | ||
911 | |||
912 | wdata->state.accel_split[0] = payload[2]; | ||
913 | wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20); | ||
914 | wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80); | ||
915 | |||
916 | ir_to_input0(wdata, &payload[3], false); | ||
917 | ir_to_input1(wdata, &payload[12], false); | ||
918 | input_sync(wdata->ir); | ||
919 | } | ||
920 | |||
921 | static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload) | ||
922 | { | ||
923 | __u8 buf[5]; | ||
924 | |||
925 | handler_keys(wdata, payload); | ||
926 | |||
927 | wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02); | ||
928 | wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08); | ||
929 | |||
930 | buf[0] = 0; | ||
931 | buf[1] = 0; | ||
932 | buf[2] = wdata->state.accel_split[0]; | ||
933 | buf[3] = payload[2]; | ||
934 | buf[4] = wdata->state.accel_split[1]; | ||
935 | handler_accel(wdata, buf); | ||
936 | |||
937 | ir_to_input2(wdata, &payload[3], false); | ||
938 | ir_to_input3(wdata, &payload[12], false); | ||
939 | input_sync(wdata->ir); | ||
940 | } | ||
941 | |||
942 | struct wiiproto_handler { | ||
943 | __u8 id; | ||
944 | size_t size; | ||
945 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); | ||
946 | }; | ||
947 | |||
948 | static struct wiiproto_handler handlers[] = { | ||
949 | { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status }, | ||
950 | { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data }, | ||
951 | { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return }, | ||
952 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, | ||
953 | { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA }, | ||
954 | { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE }, | ||
955 | { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI }, | ||
956 | { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE }, | ||
957 | { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE }, | ||
958 | { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE }, | ||
959 | { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE }, | ||
960 | { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E }, | ||
961 | { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 }, | ||
962 | { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 }, | ||
963 | { .id = 0 } | ||
964 | }; | ||
965 | |||
966 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, | ||
967 | u8 *raw_data, int size) | ||
968 | { | ||
969 | struct wiimote_data *wdata = hid_get_drvdata(hdev); | ||
970 | struct wiiproto_handler *h; | ||
971 | int i; | ||
972 | unsigned long flags; | ||
973 | bool handled = false; | ||
974 | |||
975 | if (size < 1) | ||
976 | return -EINVAL; | ||
977 | |||
978 | spin_lock_irqsave(&wdata->state.lock, flags); | ||
979 | |||
980 | for (i = 0; handlers[i].id; ++i) { | ||
981 | h = &handlers[i]; | ||
982 | if (h->id == raw_data[0] && h->size < size) { | ||
983 | h->func(wdata, &raw_data[1]); | ||
984 | handled = true; | ||
985 | } | ||
986 | } | ||
987 | |||
988 | if (!handled) | ||
989 | hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0], | ||
990 | size); | ||
991 | |||
992 | spin_unlock_irqrestore(&wdata->state.lock, flags); | ||
993 | |||
994 | return 0; | ||
995 | } | ||
996 | |||
997 | static void wiimote_leds_destroy(struct wiimote_data *wdata) | ||
998 | { | ||
999 | int i; | ||
1000 | struct led_classdev *led; | ||
1001 | |||
1002 | for (i = 0; i < 4; ++i) { | ||
1003 | if (wdata->leds[i]) { | ||
1004 | led = wdata->leds[i]; | ||
1005 | wdata->leds[i] = NULL; | ||
1006 | led_classdev_unregister(led); | ||
1007 | kfree(led); | ||
1008 | } | ||
1009 | } | ||
1010 | } | ||
1011 | |||
1012 | static int wiimote_leds_create(struct wiimote_data *wdata) | ||
1013 | { | ||
1014 | int i, ret; | ||
1015 | struct device *dev = &wdata->hdev->dev; | ||
1016 | size_t namesz = strlen(dev_name(dev)) + 9; | ||
1017 | struct led_classdev *led; | ||
1018 | char *name; | ||
1019 | |||
1020 | for (i = 0; i < 4; ++i) { | ||
1021 | led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL); | ||
1022 | if (!led) { | ||
1023 | ret = -ENOMEM; | ||
1024 | goto err; | ||
1025 | } | ||
1026 | name = (void*)&led[1]; | ||
1027 | snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i); | ||
1028 | led->name = name; | ||
1029 | led->brightness = 0; | ||
1030 | led->max_brightness = 1; | ||
1031 | led->brightness_get = wiimote_leds_get; | ||
1032 | led->brightness_set = wiimote_leds_set; | ||
1033 | |||
1034 | ret = led_classdev_register(dev, led); | ||
1035 | if (ret) { | ||
1036 | kfree(led); | ||
1037 | goto err; | ||
1038 | } | ||
1039 | wdata->leds[i] = led; | ||
1040 | } | ||
1041 | |||
1042 | return 0; | ||
1043 | |||
1044 | err: | ||
1045 | wiimote_leds_destroy(wdata); | ||
1046 | return ret; | ||
1047 | } | ||
1048 | |||
1049 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) | ||
1050 | { | ||
1051 | struct wiimote_data *wdata; | ||
1052 | int i; | ||
1053 | |||
1054 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); | ||
1055 | if (!wdata) | ||
1056 | return NULL; | ||
1057 | |||
1058 | wdata->input = input_allocate_device(); | ||
1059 | if (!wdata->input) | ||
1060 | goto err; | ||
1061 | |||
1062 | wdata->hdev = hdev; | ||
1063 | hid_set_drvdata(hdev, wdata); | ||
1064 | |||
1065 | input_set_drvdata(wdata->input, wdata); | ||
1066 | wdata->input->open = wiimote_input_open; | ||
1067 | wdata->input->close = wiimote_input_close; | ||
1068 | wdata->input->dev.parent = &wdata->hdev->dev; | ||
1069 | wdata->input->id.bustype = wdata->hdev->bus; | ||
1070 | wdata->input->id.vendor = wdata->hdev->vendor; | ||
1071 | wdata->input->id.product = wdata->hdev->product; | ||
1072 | wdata->input->id.version = wdata->hdev->version; | ||
1073 | wdata->input->name = WIIMOTE_NAME; | ||
1074 | |||
1075 | set_bit(EV_KEY, wdata->input->evbit); | ||
1076 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) | ||
1077 | set_bit(wiiproto_keymap[i], wdata->input->keybit); | ||
1078 | |||
1079 | set_bit(FF_RUMBLE, wdata->input->ffbit); | ||
1080 | if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play)) | ||
1081 | goto err_input; | ||
1082 | |||
1083 | wdata->accel = input_allocate_device(); | ||
1084 | if (!wdata->accel) | ||
1085 | goto err_input; | ||
1086 | |||
1087 | input_set_drvdata(wdata->accel, wdata); | ||
1088 | wdata->accel->open = wiimote_accel_open; | ||
1089 | wdata->accel->close = wiimote_accel_close; | ||
1090 | wdata->accel->dev.parent = &wdata->hdev->dev; | ||
1091 | wdata->accel->id.bustype = wdata->hdev->bus; | ||
1092 | wdata->accel->id.vendor = wdata->hdev->vendor; | ||
1093 | wdata->accel->id.product = wdata->hdev->product; | ||
1094 | wdata->accel->id.version = wdata->hdev->version; | ||
1095 | wdata->accel->name = WIIMOTE_NAME " Accelerometer"; | ||
1096 | |||
1097 | set_bit(EV_ABS, wdata->accel->evbit); | ||
1098 | set_bit(ABS_RX, wdata->accel->absbit); | ||
1099 | set_bit(ABS_RY, wdata->accel->absbit); | ||
1100 | set_bit(ABS_RZ, wdata->accel->absbit); | ||
1101 | input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4); | ||
1102 | input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4); | ||
1103 | input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4); | ||
1104 | |||
1105 | wdata->ir = input_allocate_device(); | ||
1106 | if (!wdata->ir) | ||
1107 | goto err_ir; | ||
1108 | |||
1109 | input_set_drvdata(wdata->ir, wdata); | ||
1110 | wdata->ir->open = wiimote_ir_open; | ||
1111 | wdata->ir->close = wiimote_ir_close; | ||
1112 | wdata->ir->dev.parent = &wdata->hdev->dev; | ||
1113 | wdata->ir->id.bustype = wdata->hdev->bus; | ||
1114 | wdata->ir->id.vendor = wdata->hdev->vendor; | ||
1115 | wdata->ir->id.product = wdata->hdev->product; | ||
1116 | wdata->ir->id.version = wdata->hdev->version; | ||
1117 | wdata->ir->name = WIIMOTE_NAME " IR"; | ||
1118 | |||
1119 | set_bit(EV_ABS, wdata->ir->evbit); | ||
1120 | set_bit(ABS_HAT0X, wdata->ir->absbit); | ||
1121 | set_bit(ABS_HAT0Y, wdata->ir->absbit); | ||
1122 | set_bit(ABS_HAT1X, wdata->ir->absbit); | ||
1123 | set_bit(ABS_HAT1Y, wdata->ir->absbit); | ||
1124 | set_bit(ABS_HAT2X, wdata->ir->absbit); | ||
1125 | set_bit(ABS_HAT2Y, wdata->ir->absbit); | ||
1126 | set_bit(ABS_HAT3X, wdata->ir->absbit); | ||
1127 | set_bit(ABS_HAT3Y, wdata->ir->absbit); | ||
1128 | input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4); | ||
1129 | input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4); | ||
1130 | input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4); | ||
1131 | input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4); | ||
1132 | input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4); | ||
1133 | input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4); | ||
1134 | input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4); | ||
1135 | input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4); | ||
1136 | |||
1137 | spin_lock_init(&wdata->qlock); | ||
1138 | INIT_WORK(&wdata->worker, wiimote_worker); | ||
1139 | |||
1140 | spin_lock_init(&wdata->state.lock); | ||
1141 | init_completion(&wdata->state.ready); | ||
1142 | mutex_init(&wdata->state.sync); | ||
1143 | wdata->state.drm = WIIPROTO_REQ_DRM_K; | ||
1144 | |||
1145 | return wdata; | ||
1146 | |||
1147 | err_ir: | ||
1148 | input_free_device(wdata->accel); | ||
1149 | err_input: | ||
1150 | input_free_device(wdata->input); | ||
1151 | err: | ||
1152 | kfree(wdata); | ||
1153 | return NULL; | ||
1154 | } | ||
1155 | |||
1156 | static void wiimote_destroy(struct wiimote_data *wdata) | ||
1157 | { | ||
1158 | wiidebug_deinit(wdata); | ||
1159 | wiiext_deinit(wdata); | ||
1160 | wiimote_leds_destroy(wdata); | ||
1161 | |||
1162 | power_supply_unregister(&wdata->battery); | ||
1163 | input_unregister_device(wdata->accel); | ||
1164 | input_unregister_device(wdata->ir); | ||
1165 | input_unregister_device(wdata->input); | ||
1166 | cancel_work_sync(&wdata->worker); | ||
1167 | hid_hw_stop(wdata->hdev); | ||
1168 | |||
1169 | kfree(wdata); | ||
1170 | } | ||
1171 | |||
1172 | static int wiimote_hid_probe(struct hid_device *hdev, | ||
1173 | const struct hid_device_id *id) | ||
1174 | { | ||
1175 | struct wiimote_data *wdata; | ||
1176 | int ret; | ||
1177 | |||
1178 | hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS; | ||
1179 | |||
1180 | wdata = wiimote_create(hdev); | ||
1181 | if (!wdata) { | ||
1182 | hid_err(hdev, "Can't alloc device\n"); | ||
1183 | return -ENOMEM; | ||
1184 | } | ||
1185 | |||
1186 | ret = hid_parse(hdev); | ||
1187 | if (ret) { | ||
1188 | hid_err(hdev, "HID parse failed\n"); | ||
1189 | goto err; | ||
1190 | } | ||
1191 | |||
1192 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); | ||
1193 | if (ret) { | ||
1194 | hid_err(hdev, "HW start failed\n"); | ||
1195 | goto err; | ||
1196 | } | ||
1197 | |||
1198 | ret = input_register_device(wdata->accel); | ||
1199 | if (ret) { | ||
1200 | hid_err(hdev, "Cannot register input device\n"); | ||
1201 | goto err_stop; | ||
1202 | } | ||
1203 | |||
1204 | ret = input_register_device(wdata->ir); | ||
1205 | if (ret) { | ||
1206 | hid_err(hdev, "Cannot register input device\n"); | ||
1207 | goto err_ir; | ||
1208 | } | ||
1209 | |||
1210 | ret = input_register_device(wdata->input); | ||
1211 | if (ret) { | ||
1212 | hid_err(hdev, "Cannot register input device\n"); | ||
1213 | goto err_input; | ||
1214 | } | ||
1215 | |||
1216 | wdata->battery.properties = wiimote_battery_props; | ||
1217 | wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props); | ||
1218 | wdata->battery.get_property = wiimote_battery_get_property; | ||
1219 | wdata->battery.name = "wiimote_battery"; | ||
1220 | wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY; | ||
1221 | wdata->battery.use_for_apm = 0; | ||
1222 | |||
1223 | ret = power_supply_register(&wdata->hdev->dev, &wdata->battery); | ||
1224 | if (ret) { | ||
1225 | hid_err(hdev, "Cannot register battery device\n"); | ||
1226 | goto err_battery; | ||
1227 | } | ||
1228 | |||
1229 | ret = wiimote_leds_create(wdata); | ||
1230 | if (ret) | ||
1231 | goto err_free; | ||
1232 | |||
1233 | ret = wiiext_init(wdata); | ||
1234 | if (ret) | ||
1235 | goto err_free; | ||
1236 | |||
1237 | ret = wiidebug_init(wdata); | ||
1238 | if (ret) | ||
1239 | goto err_free; | ||
1240 | |||
1241 | hid_info(hdev, "New device registered\n"); | ||
1242 | |||
1243 | /* by default set led1 after device initialization */ | ||
1244 | spin_lock_irq(&wdata->state.lock); | ||
1245 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); | ||
1246 | spin_unlock_irq(&wdata->state.lock); | ||
1247 | |||
1248 | return 0; | ||
1249 | |||
1250 | err_free: | ||
1251 | wiimote_destroy(wdata); | ||
1252 | return ret; | ||
1253 | |||
1254 | err_battery: | ||
1255 | input_unregister_device(wdata->input); | ||
1256 | wdata->input = NULL; | ||
1257 | err_input: | ||
1258 | input_unregister_device(wdata->ir); | ||
1259 | wdata->ir = NULL; | ||
1260 | err_ir: | ||
1261 | input_unregister_device(wdata->accel); | ||
1262 | wdata->accel = NULL; | ||
1263 | err_stop: | ||
1264 | hid_hw_stop(hdev); | ||
1265 | err: | ||
1266 | input_free_device(wdata->ir); | ||
1267 | input_free_device(wdata->accel); | ||
1268 | input_free_device(wdata->input); | ||
1269 | kfree(wdata); | ||
1270 | return ret; | ||
1271 | } | ||
1272 | |||
1273 | static void wiimote_hid_remove(struct hid_device *hdev) | ||
1274 | { | ||
1275 | struct wiimote_data *wdata = hid_get_drvdata(hdev); | ||
1276 | |||
1277 | hid_info(hdev, "Device removed\n"); | ||
1278 | wiimote_destroy(wdata); | ||
1279 | } | ||
1280 | |||
1281 | static const struct hid_device_id wiimote_hid_devices[] = { | ||
1282 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, | ||
1283 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, | ||
1284 | { } | ||
1285 | }; | ||
1286 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); | ||
1287 | |||
1288 | static struct hid_driver wiimote_hid_driver = { | ||
1289 | .name = "wiimote", | ||
1290 | .id_table = wiimote_hid_devices, | ||
1291 | .probe = wiimote_hid_probe, | ||
1292 | .remove = wiimote_hid_remove, | ||
1293 | .raw_event = wiimote_hid_event, | ||
1294 | }; | ||
1295 | |||
1296 | static int __init wiimote_init(void) | ||
1297 | { | ||
1298 | int ret; | ||
1299 | |||
1300 | ret = hid_register_driver(&wiimote_hid_driver); | ||
1301 | if (ret) | ||
1302 | pr_err("Can't register wiimote hid driver\n"); | ||
1303 | |||
1304 | return ret; | ||
1305 | } | ||
1306 | |||
1307 | static void __exit wiimote_exit(void) | ||
1308 | { | ||
1309 | hid_unregister_driver(&wiimote_hid_driver); | ||
1310 | } | ||
1311 | |||
1312 | module_init(wiimote_init); | ||
1313 | module_exit(wiimote_exit); | ||
1314 | MODULE_LICENSE("GPL"); | ||
1315 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); | ||
1316 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); | ||