blob: f7f8b7ff7dec7c0bb400edcf463ed33f04616af6 [file] [log] [blame]
David Herrmannfb51b442011-07-05 13:45:08 +02001/*
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
David Herrmann29d28062011-09-06 13:50:34 +020013#include <linux/completion.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020014#include <linux/device.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020015#include <linux/hid.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020016#include <linux/input.h>
David Herrmann23a5a4a2011-08-17 11:43:22 +020017#include <linux/leds.h>
David Herrmannfb51b442011-07-05 13:45:08 +020018#include <linux/module.h>
David Herrmann29d28062011-09-06 13:50:34 +020019#include <linux/mutex.h>
David Herrmann6591d752011-09-06 13:50:39 +020020#include <linux/power_supply.h>
David Herrmann23c063c2011-07-05 13:45:14 +020021#include <linux/spinlock.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020022#include "hid-ids.h"
David Herrmann7e274402011-11-17 14:11:59 +010023#include "hid-wiimote.h"
David Herrmannfb51b442011-07-05 13:45:08 +020024
David Herrmannb22e00f2011-09-06 13:50:40 +020025#define WIIMOTE_VERSION "0.2"
David Herrmann1abb9ad2011-07-05 13:45:16 +020026
27enum wiiproto_keys {
28 WIIPROTO_KEY_LEFT,
29 WIIPROTO_KEY_RIGHT,
30 WIIPROTO_KEY_UP,
31 WIIPROTO_KEY_DOWN,
32 WIIPROTO_KEY_PLUS,
33 WIIPROTO_KEY_MINUS,
34 WIIPROTO_KEY_ONE,
35 WIIPROTO_KEY_TWO,
36 WIIPROTO_KEY_A,
37 WIIPROTO_KEY_B,
38 WIIPROTO_KEY_HOME,
39 WIIPROTO_KEY_COUNT
40};
41
42static __u16 wiiproto_keymap[] = {
43 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
44 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
45 KEY_UP, /* WIIPROTO_KEY_UP */
46 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
47 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
48 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
49 BTN_1, /* WIIPROTO_KEY_ONE */
50 BTN_2, /* WIIPROTO_KEY_TWO */
51 BTN_A, /* WIIPROTO_KEY_A */
52 BTN_B, /* WIIPROTO_KEY_B */
53 BTN_MODE, /* WIIPROTO_KEY_HOME */
54};
55
David Herrmann6591d752011-09-06 13:50:39 +020056static enum power_supply_property wiimote_battery_props[] = {
57 POWER_SUPPLY_PROP_CAPACITY
58};
59
David Herrmann0c218f142011-07-05 13:45:13 +020060static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
61 size_t count)
62{
63 __u8 *buf;
64 ssize_t ret;
65
66 if (!hdev->hid_output_raw_report)
67 return -ENODEV;
68
69 buf = kmemdup(buffer, count, GFP_KERNEL);
70 if (!buf)
71 return -ENOMEM;
72
73 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
74
75 kfree(buf);
76 return ret;
77}
78
David Herrmann23c063c2011-07-05 13:45:14 +020079static void wiimote_worker(struct work_struct *work)
80{
81 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
82 worker);
83 unsigned long flags;
84
85 spin_lock_irqsave(&wdata->qlock, flags);
86
87 while (wdata->head != wdata->tail) {
88 spin_unlock_irqrestore(&wdata->qlock, flags);
89 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
90 wdata->outq[wdata->tail].size);
91 spin_lock_irqsave(&wdata->qlock, flags);
92
93 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
94 }
95
96 spin_unlock_irqrestore(&wdata->qlock, flags);
97}
98
99static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
100 size_t count)
101{
102 unsigned long flags;
103 __u8 newhead;
104
105 if (count > HID_MAX_BUFFER_SIZE) {
106 hid_warn(wdata->hdev, "Sending too large output report\n");
107 return;
108 }
109
110 /*
111 * Copy new request into our output queue and check whether the
112 * queue is full. If it is full, discard this request.
113 * If it is empty we need to start a new worker that will
114 * send out the buffer to the hid device.
115 * If the queue is not empty, then there must be a worker
116 * that is currently sending out our buffer and this worker
117 * will reschedule itself until the queue is empty.
118 */
119
120 spin_lock_irqsave(&wdata->qlock, flags);
121
122 memcpy(wdata->outq[wdata->head].data, buffer, count);
123 wdata->outq[wdata->head].size = count;
124 newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
125
126 if (wdata->head == wdata->tail) {
127 wdata->head = newhead;
128 schedule_work(&wdata->worker);
129 } else if (newhead != wdata->tail) {
130 wdata->head = newhead;
131 } else {
132 hid_warn(wdata->hdev, "Output queue is full");
133 }
134
135 spin_unlock_irqrestore(&wdata->qlock, flags);
136}
137
David Herrmannc003ec22011-09-06 13:50:26 +0200138/*
139 * This sets the rumble bit on the given output report if rumble is
140 * currently enabled.
141 * \cmd1 must point to the second byte in the output report => &cmd[1]
142 * This must be called on nearly every output report before passing it
143 * into the output queue!
144 */
145static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
146{
147 if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
148 *cmd1 |= 0x01;
149}
150
151static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
152{
153 __u8 cmd[2];
154
155 rumble = !!rumble;
156 if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
157 return;
158
159 if (rumble)
160 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
161 else
162 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
163
164 cmd[0] = WIIPROTO_REQ_RUMBLE;
165 cmd[1] = 0;
166
167 wiiproto_keep_rumble(wdata, &cmd[1]);
168 wiimote_queue(wdata, cmd, sizeof(cmd));
169}
170
David Herrmanndb308342011-07-05 13:45:17 +0200171static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
172{
173 __u8 cmd[2];
174
David Herrmann32a0d9a2011-07-05 13:45:18 +0200175 leds &= WIIPROTO_FLAGS_LEDS;
176 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
177 return;
178 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
179
David Herrmanndb308342011-07-05 13:45:17 +0200180 cmd[0] = WIIPROTO_REQ_LED;
181 cmd[1] = 0;
182
183 if (leds & WIIPROTO_FLAG_LED1)
184 cmd[1] |= 0x10;
185 if (leds & WIIPROTO_FLAG_LED2)
186 cmd[1] |= 0x20;
187 if (leds & WIIPROTO_FLAG_LED3)
188 cmd[1] |= 0x40;
189 if (leds & WIIPROTO_FLAG_LED4)
190 cmd[1] |= 0x80;
191
David Herrmannc003ec22011-09-06 13:50:26 +0200192 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmanndb308342011-07-05 13:45:17 +0200193 wiimote_queue(wdata, cmd, sizeof(cmd));
194}
195
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200196/*
197 * Check what peripherals of the wiimote are currently
198 * active and select a proper DRM that supports all of
199 * the requested data inputs.
200 */
201static __u8 select_drm(struct wiimote_data *wdata)
202{
David Herrmannf363e4f2011-09-06 13:50:30 +0200203 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
204
205 if (ir == WIIPROTO_FLAG_IR_BASIC) {
206 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
207 return WIIPROTO_REQ_DRM_KAIE;
208 else
209 return WIIPROTO_REQ_DRM_KIE;
210 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
211 return WIIPROTO_REQ_DRM_KAI;
212 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
213 return WIIPROTO_REQ_DRM_SKAI1;
214 } else {
215 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
216 return WIIPROTO_REQ_DRM_KA;
217 else
218 return WIIPROTO_REQ_DRM_K;
219 }
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200220}
221
David Herrmann7e274402011-11-17 14:11:59 +0100222void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200223{
224 __u8 cmd[3];
225
226 if (drm == WIIPROTO_REQ_NULL)
227 drm = select_drm(wdata);
228
229 cmd[0] = WIIPROTO_REQ_DRM;
230 cmd[1] = 0;
231 cmd[2] = drm;
232
David Herrmannc003ec22011-09-06 13:50:26 +0200233 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200234 wiimote_queue(wdata, cmd, sizeof(cmd));
235}
236
David Herrmanne3979a92011-09-06 13:50:38 +0200237static void wiiproto_req_status(struct wiimote_data *wdata)
238{
239 __u8 cmd[2];
240
241 cmd[0] = WIIPROTO_REQ_SREQ;
242 cmd[1] = 0;
243
244 wiiproto_keep_rumble(wdata, &cmd[1]);
245 wiimote_queue(wdata, cmd, sizeof(cmd));
246}
247
David Herrmann98a558a2011-09-06 13:50:28 +0200248static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
249{
250 accel = !!accel;
251 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
252 return;
253
254 if (accel)
255 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
256 else
257 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
258
259 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
260}
261
David Herrmannfc221cd2011-09-06 13:50:36 +0200262static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
263{
264 __u8 cmd[2];
265
266 cmd[0] = WIIPROTO_REQ_IR1;
267 cmd[1] = flags;
268
269 wiiproto_keep_rumble(wdata, &cmd[1]);
270 wiimote_queue(wdata, cmd, sizeof(cmd));
271}
272
273static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
274{
275 __u8 cmd[2];
276
277 cmd[0] = WIIPROTO_REQ_IR2;
278 cmd[1] = flags;
279
280 wiiproto_keep_rumble(wdata, &cmd[1]);
281 wiimote_queue(wdata, cmd, sizeof(cmd));
282}
283
David Herrmannbe1ecd62011-09-06 13:50:33 +0200284#define wiiproto_req_wreg(wdata, os, buf, sz) \
285 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
286
287#define wiiproto_req_weeprom(wdata, os, buf, sz) \
288 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
289
290static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
291 __u32 offset, const __u8 *buf, __u8 size)
292{
293 __u8 cmd[22];
294
295 if (size > 16 || size == 0) {
296 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
297 return;
298 }
299
300 memset(cmd, 0, sizeof(cmd));
301 cmd[0] = WIIPROTO_REQ_WMEM;
302 cmd[2] = (offset >> 16) & 0xff;
303 cmd[3] = (offset >> 8) & 0xff;
304 cmd[4] = offset & 0xff;
305 cmd[5] = size;
306 memcpy(&cmd[6], buf, size);
307
308 if (!eeprom)
309 cmd[1] |= 0x04;
310
311 wiiproto_keep_rumble(wdata, &cmd[1]);
312 wiimote_queue(wdata, cmd, sizeof(cmd));
313}
314
David Herrmannfad8c0e2011-11-17 14:12:00 +0100315#define wiiproto_req_rreg(wdata, os, sz) \
316 wiiproto_req_rmem((wdata), false, (os), (sz))
317
318#define wiiproto_req_reeprom(wdata, os, sz) \
319 wiiproto_req_rmem((wdata), true, (os), (sz))
320
321static void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
322 __u32 offset, __u16 size)
323{
324 __u8 cmd[7];
325
326 if (size == 0) {
327 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
328 return;
329 }
330
331 cmd[0] = WIIPROTO_REQ_RMEM;
332 cmd[1] = 0;
333 cmd[2] = (offset >> 16) & 0xff;
334 cmd[3] = (offset >> 8) & 0xff;
335 cmd[4] = offset & 0xff;
336 cmd[5] = (size >> 8) & 0xff;
337 cmd[6] = size & 0xff;
338
339 if (!eeprom)
340 cmd[1] |= 0x04;
341
342 wiiproto_keep_rumble(wdata, &cmd[1]);
343 wiimote_queue(wdata, cmd, sizeof(cmd));
344}
345
David Herrmann33e84012011-09-06 13:50:35 +0200346/* requries the cmd-mutex to be held */
David Herrmann7e274402011-11-17 14:11:59 +0100347int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
David Herrmann33e84012011-09-06 13:50:35 +0200348 const __u8 *wmem, __u8 size)
349{
350 unsigned long flags;
351 int ret;
352
353 spin_lock_irqsave(&wdata->state.lock, flags);
354 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
355 wiiproto_req_wreg(wdata, offset, wmem, size);
356 spin_unlock_irqrestore(&wdata->state.lock, flags);
357
358 ret = wiimote_cmd_wait(wdata);
359 if (!ret && wdata->state.cmd_err)
360 ret = -EIO;
361
362 return ret;
363}
364
David Herrmannfad8c0e2011-11-17 14:12:00 +0100365/* requries the cmd-mutex to be held */
366ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
367 __u8 size)
368{
369 unsigned long flags;
370 ssize_t ret;
371
372 spin_lock_irqsave(&wdata->state.lock, flags);
373 wdata->state.cmd_read_size = size;
374 wdata->state.cmd_read_buf = rmem;
375 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
376 wiiproto_req_rreg(wdata, offset, size);
377 spin_unlock_irqrestore(&wdata->state.lock, flags);
378
379 ret = wiimote_cmd_wait(wdata);
380
381 spin_lock_irqsave(&wdata->state.lock, flags);
382 wdata->state.cmd_read_buf = NULL;
383 spin_unlock_irqrestore(&wdata->state.lock, flags);
384
385 if (!ret) {
386 if (wdata->state.cmd_read_size == 0)
387 ret = -EIO;
388 else
389 ret = wdata->state.cmd_read_size;
390 }
391
392 return ret;
393}
394
David Herrmann6591d752011-09-06 13:50:39 +0200395static int wiimote_battery_get_property(struct power_supply *psy,
396 enum power_supply_property psp,
397 union power_supply_propval *val)
398{
399 struct wiimote_data *wdata = container_of(psy,
400 struct wiimote_data, battery);
401 int ret = 0, state;
402 unsigned long flags;
403
404 ret = wiimote_cmd_acquire(wdata);
405 if (ret)
406 return ret;
407
408 spin_lock_irqsave(&wdata->state.lock, flags);
409 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
410 wiiproto_req_status(wdata);
411 spin_unlock_irqrestore(&wdata->state.lock, flags);
412
413 ret = wiimote_cmd_wait(wdata);
414 state = wdata->state.cmd_battery;
415 wiimote_cmd_release(wdata);
416
417 if (ret)
418 return ret;
419
420 switch (psp) {
421 case POWER_SUPPLY_PROP_CAPACITY:
422 val->intval = state * 100 / 255;
423 break;
424 default:
425 ret = -EINVAL;
426 break;
427 }
428
429 return ret;
430}
431
David Herrmannfc221cd2011-09-06 13:50:36 +0200432static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
433{
434 int ret;
435 unsigned long flags;
436 __u8 format = 0;
437 static const __u8 data_enable[] = { 0x01 };
438 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
439 0x00, 0xaa, 0x00, 0x64 };
440 static const __u8 data_sens2[] = { 0x63, 0x03 };
441 static const __u8 data_fin[] = { 0x08 };
442
443 spin_lock_irqsave(&wdata->state.lock, flags);
444
445 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
446 spin_unlock_irqrestore(&wdata->state.lock, flags);
447 return 0;
448 }
449
450 if (mode == 0) {
451 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
452 wiiproto_req_ir1(wdata, 0);
453 wiiproto_req_ir2(wdata, 0);
454 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
455 spin_unlock_irqrestore(&wdata->state.lock, flags);
456 return 0;
457 }
458
459 spin_unlock_irqrestore(&wdata->state.lock, flags);
460
461 ret = wiimote_cmd_acquire(wdata);
462 if (ret)
463 return ret;
464
465 /* send PIXEL CLOCK ENABLE cmd first */
466 spin_lock_irqsave(&wdata->state.lock, flags);
467 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
468 wiiproto_req_ir1(wdata, 0x06);
469 spin_unlock_irqrestore(&wdata->state.lock, flags);
470
471 ret = wiimote_cmd_wait(wdata);
472 if (ret)
473 goto unlock;
474 if (wdata->state.cmd_err) {
475 ret = -EIO;
476 goto unlock;
477 }
478
479 /* enable IR LOGIC */
480 spin_lock_irqsave(&wdata->state.lock, flags);
481 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
482 wiiproto_req_ir2(wdata, 0x06);
483 spin_unlock_irqrestore(&wdata->state.lock, flags);
484
485 ret = wiimote_cmd_wait(wdata);
486 if (ret)
487 goto unlock;
488 if (wdata->state.cmd_err) {
489 ret = -EIO;
490 goto unlock;
491 }
492
493 /* enable IR cam but do not make it send data, yet */
494 ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
495 sizeof(data_enable));
496 if (ret)
497 goto unlock;
498
499 /* write first sensitivity block */
500 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
501 sizeof(data_sens1));
502 if (ret)
503 goto unlock;
504
505 /* write second sensitivity block */
506 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
507 sizeof(data_sens2));
508 if (ret)
509 goto unlock;
510
511 /* put IR cam into desired state */
512 switch (mode) {
513 case WIIPROTO_FLAG_IR_FULL:
514 format = 5;
515 break;
516 case WIIPROTO_FLAG_IR_EXT:
517 format = 3;
518 break;
519 case WIIPROTO_FLAG_IR_BASIC:
520 format = 1;
521 break;
522 }
523 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
524 if (ret)
525 goto unlock;
526
527 /* make IR cam send data */
528 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
529 if (ret)
530 goto unlock;
531
532 /* request new DRM mode compatible to IR mode */
533 spin_lock_irqsave(&wdata->state.lock, flags);
534 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
535 wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
536 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
537 spin_unlock_irqrestore(&wdata->state.lock, flags);
538
539unlock:
540 wiimote_cmd_release(wdata);
541 return ret;
542}
543
David Herrmann23a5a4a2011-08-17 11:43:22 +0200544static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
545{
546 struct wiimote_data *wdata;
547 struct device *dev = led_dev->dev->parent;
548 int i;
549 unsigned long flags;
550 bool value = false;
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200551
David Herrmann23a5a4a2011-08-17 11:43:22 +0200552 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
553
554 for (i = 0; i < 4; ++i) {
555 if (wdata->leds[i] == led_dev) {
556 spin_lock_irqsave(&wdata->state.lock, flags);
557 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
558 spin_unlock_irqrestore(&wdata->state.lock, flags);
559 break;
560 }
561 }
562
563 return value ? LED_FULL : LED_OFF;
564}
565
566static void wiimote_leds_set(struct led_classdev *led_dev,
567 enum led_brightness value)
568{
569 struct wiimote_data *wdata;
570 struct device *dev = led_dev->dev->parent;
571 int i;
572 unsigned long flags;
573 __u8 state, flag;
574
575 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
576
577 for (i = 0; i < 4; ++i) {
578 if (wdata->leds[i] == led_dev) {
579 flag = WIIPROTO_FLAG_LED(i + 1);
580 spin_lock_irqsave(&wdata->state.lock, flags);
581 state = wdata->state.flags;
582 if (value == LED_OFF)
583 wiiproto_req_leds(wdata, state & ~flag);
584 else
585 wiiproto_req_leds(wdata, state | flag);
586 spin_unlock_irqrestore(&wdata->state.lock, flags);
587 break;
588 }
589 }
590}
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200591
David Herrmannd020be92011-09-06 13:50:27 +0200592static int wiimote_ff_play(struct input_dev *dev, void *data,
593 struct ff_effect *eff)
David Herrmann672bc4e2011-07-05 13:45:11 +0200594{
David Herrmannd020be92011-09-06 13:50:27 +0200595 struct wiimote_data *wdata = input_get_drvdata(dev);
596 __u8 value;
597 unsigned long flags;
598
599 /*
600 * The wiimote supports only a single rumble motor so if any magnitude
601 * is set to non-zero then we start the rumble motor. If both are set to
602 * zero, we stop the rumble motor.
603 */
604
605 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
606 value = 1;
607 else
608 value = 0;
609
610 spin_lock_irqsave(&wdata->state.lock, flags);
611 wiiproto_req_rumble(wdata, value);
612 spin_unlock_irqrestore(&wdata->state.lock, flags);
613
David Herrmann672bc4e2011-07-05 13:45:11 +0200614 return 0;
615}
616
David Herrmann26af1742011-08-17 11:43:21 +0200617static int wiimote_input_open(struct input_dev *dev)
618{
619 struct wiimote_data *wdata = input_get_drvdata(dev);
620
621 return hid_hw_open(wdata->hdev);
622}
623
624static void wiimote_input_close(struct input_dev *dev)
625{
626 struct wiimote_data *wdata = input_get_drvdata(dev);
627
628 hid_hw_close(wdata->hdev);
629}
630
David Herrmann98a558a2011-09-06 13:50:28 +0200631static int wiimote_accel_open(struct input_dev *dev)
632{
633 struct wiimote_data *wdata = input_get_drvdata(dev);
634 int ret;
635 unsigned long flags;
636
637 ret = hid_hw_open(wdata->hdev);
638 if (ret)
639 return ret;
640
641 spin_lock_irqsave(&wdata->state.lock, flags);
642 wiiproto_req_accel(wdata, true);
643 spin_unlock_irqrestore(&wdata->state.lock, flags);
644
645 return 0;
646}
647
648static void wiimote_accel_close(struct input_dev *dev)
649{
650 struct wiimote_data *wdata = input_get_drvdata(dev);
651 unsigned long flags;
652
653 spin_lock_irqsave(&wdata->state.lock, flags);
654 wiiproto_req_accel(wdata, false);
655 spin_unlock_irqrestore(&wdata->state.lock, flags);
656
657 hid_hw_close(wdata->hdev);
658}
659
David Herrmann0370d7c2011-09-06 13:50:37 +0200660static int wiimote_ir_open(struct input_dev *dev)
661{
662 struct wiimote_data *wdata = input_get_drvdata(dev);
663 int ret;
664
665 ret = hid_hw_open(wdata->hdev);
666 if (ret)
667 return ret;
668
669 ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
670 if (ret) {
671 hid_hw_close(wdata->hdev);
672 return ret;
673 }
674
675 return 0;
676}
677
678static void wiimote_ir_close(struct input_dev *dev)
679{
680 struct wiimote_data *wdata = input_get_drvdata(dev);
681
682 wiimote_init_ir(wdata, 0);
683 hid_hw_close(wdata->hdev);
684}
685
David Herrmann1abb9ad2011-07-05 13:45:16 +0200686static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
687{
688 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
689 !!(payload[0] & 0x01));
690 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
691 !!(payload[0] & 0x02));
692 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
693 !!(payload[0] & 0x04));
694 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
695 !!(payload[0] & 0x08));
696 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
697 !!(payload[0] & 0x10));
698 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
699 !!(payload[1] & 0x01));
700 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
701 !!(payload[1] & 0x02));
702 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
703 !!(payload[1] & 0x04));
704 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
705 !!(payload[1] & 0x08));
706 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
707 !!(payload[1] & 0x10));
708 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
709 !!(payload[1] & 0x80));
710 input_sync(wdata->input);
711}
712
David Herrmannefcf9182011-09-06 13:50:29 +0200713static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
714{
715 __u16 x, y, z;
716
717 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
718 return;
719
720 /*
721 * payload is: BB BB XX YY ZZ
722 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
723 * contain the upper 8 bits of each value. The lower 2 bits are
724 * contained in the buttons data BB BB.
725 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
726 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
727 * accel value and bit 6 is the second bit of the Z value.
728 * The first bit of Y and Z values is not available and always set to 0.
729 * 0x200 is returned on no movement.
730 */
731
732 x = payload[2] << 2;
733 y = payload[3] << 2;
734 z = payload[4] << 2;
735
736 x |= (payload[0] >> 5) & 0x3;
737 y |= (payload[1] >> 4) & 0x2;
738 z |= (payload[1] >> 5) & 0x2;
739
740 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
741 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
742 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
743 input_sync(wdata->accel);
744}
745
David Herrmanneac39e72011-09-06 13:50:31 +0200746#define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
747 ABS_HAT0X, ABS_HAT0Y)
748#define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
749 ABS_HAT1X, ABS_HAT1Y)
750#define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
751 ABS_HAT2X, ABS_HAT2Y)
752#define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
753 ABS_HAT3X, ABS_HAT3Y)
754
755static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
756 bool packed, __u8 xid, __u8 yid)
757{
758 __u16 x, y;
759
760 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
761 return;
762
763 /*
764 * Basic IR data is encoded into 3 bytes. The first two bytes are the
765 * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits
766 * of both.
767 * If data is packed, then the 3rd byte is put first and slightly
768 * reordered. This allows to interleave packed and non-packed data to
769 * have two IR sets in 5 bytes instead of 6.
770 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
771 */
772
773 if (packed) {
774 x = ir[1] << 2;
775 y = ir[2] << 2;
776
777 x |= ir[0] & 0x3;
778 y |= (ir[0] >> 2) & 0x3;
779 } else {
780 x = ir[0] << 2;
781 y = ir[1] << 2;
782
783 x |= (ir[2] >> 4) & 0x3;
784 y |= (ir[2] >> 6) & 0x3;
785 }
786
787 input_report_abs(wdata->ir, xid, x);
788 input_report_abs(wdata->ir, yid, y);
789}
David Herrmannefcf9182011-09-06 13:50:29 +0200790
David Herrmannc87019e2011-08-17 11:43:24 +0200791static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
792{
793 handler_keys(wdata, payload);
794
795 /* on status reports the drm is reset so we need to resend the drm */
796 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
David Herrmanne3979a92011-09-06 13:50:38 +0200797
798 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) {
799 wdata->state.cmd_battery = payload[5];
800 wiimote_cmd_complete(wdata);
801 }
David Herrmannc87019e2011-08-17 11:43:24 +0200802}
803
David Herrmannbe1ecd62011-09-06 13:50:33 +0200804static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
805{
David Herrmannfad8c0e2011-11-17 14:12:00 +0100806 __u16 offset = payload[3] << 8 | payload[4];
807 __u8 size = (payload[2] >> 4) + 1;
808 __u8 err = payload[2] & 0x0f;
809
David Herrmannbe1ecd62011-09-06 13:50:33 +0200810 handler_keys(wdata, payload);
David Herrmannfad8c0e2011-11-17 14:12:00 +0100811
812 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
813 if (err)
814 size = 0;
815 else if (size > wdata->state.cmd_read_size)
816 size = wdata->state.cmd_read_size;
817
818 wdata->state.cmd_read_size = size;
819 if (wdata->state.cmd_read_buf)
820 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
821 wiimote_cmd_complete(wdata);
822 }
David Herrmannbe1ecd62011-09-06 13:50:33 +0200823}
824
David Herrmannc87019e2011-08-17 11:43:24 +0200825static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
826{
827 __u8 err = payload[3];
828 __u8 cmd = payload[2];
829
830 handler_keys(wdata, payload);
831
David Herrmann33e84012011-09-06 13:50:35 +0200832 if (wiimote_cmd_pending(wdata, cmd, 0)) {
833 wdata->state.cmd_err = err;
834 wiimote_cmd_complete(wdata);
835 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +0200836 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
837 cmd);
David Herrmann33e84012011-09-06 13:50:35 +0200838 }
David Herrmannc87019e2011-08-17 11:43:24 +0200839}
840
David Herrmannefcf9182011-09-06 13:50:29 +0200841static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
842{
843 handler_keys(wdata, payload);
844 handler_accel(wdata, payload);
845}
846
David Herrmann7336b9f2011-09-06 13:50:32 +0200847static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
848{
849 handler_keys(wdata, payload);
850}
851
David Herrmannefcf9182011-09-06 13:50:29 +0200852static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
853{
854 handler_keys(wdata, payload);
855 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200856 ir_to_input0(wdata, &payload[5], false);
857 ir_to_input1(wdata, &payload[8], false);
858 ir_to_input2(wdata, &payload[11], false);
859 ir_to_input3(wdata, &payload[14], false);
860 input_sync(wdata->ir);
861}
862
David Herrmann7336b9f2011-09-06 13:50:32 +0200863static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
864{
865 handler_keys(wdata, payload);
866}
867
David Herrmanneac39e72011-09-06 13:50:31 +0200868static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
869{
870 handler_keys(wdata, payload);
871 ir_to_input0(wdata, &payload[2], false);
872 ir_to_input1(wdata, &payload[4], true);
873 ir_to_input2(wdata, &payload[7], false);
874 ir_to_input3(wdata, &payload[9], true);
875 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200876}
877
878static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
879{
880 handler_keys(wdata, payload);
881 handler_accel(wdata, payload);
882}
883
884static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
885{
886 handler_keys(wdata, payload);
887 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200888 ir_to_input0(wdata, &payload[5], false);
889 ir_to_input1(wdata, &payload[7], true);
890 ir_to_input2(wdata, &payload[10], false);
891 ir_to_input3(wdata, &payload[12], true);
892 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200893}
894
David Herrmann7336b9f2011-09-06 13:50:32 +0200895static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
896{
897}
898
David Herrmannefcf9182011-09-06 13:50:29 +0200899static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
900{
901 handler_keys(wdata, payload);
902
903 wdata->state.accel_split[0] = payload[2];
904 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
905 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +0200906
907 ir_to_input0(wdata, &payload[3], false);
908 ir_to_input1(wdata, &payload[12], false);
909 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200910}
911
912static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
913{
914 __u8 buf[5];
915
916 handler_keys(wdata, payload);
917
918 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
919 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
920
921 buf[0] = 0;
922 buf[1] = 0;
923 buf[2] = wdata->state.accel_split[0];
924 buf[3] = payload[2];
925 buf[4] = wdata->state.accel_split[1];
926 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +0200927
928 ir_to_input2(wdata, &payload[3], false);
929 ir_to_input3(wdata, &payload[12], false);
930 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200931}
932
David Herrmanna4d19192011-07-05 13:45:15 +0200933struct wiiproto_handler {
934 __u8 id;
935 size_t size;
936 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
937};
938
939static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200940 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmannbe1ecd62011-09-06 13:50:33 +0200941 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmannc87019e2011-08-17 11:43:24 +0200942 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200943 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +0200944 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann7336b9f2011-09-06 13:50:32 +0200945 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmannefcf9182011-09-06 13:50:29 +0200946 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann7336b9f2011-09-06 13:50:32 +0200947 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmannefcf9182011-09-06 13:50:29 +0200948 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmanneac39e72011-09-06 13:50:31 +0200949 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmannefcf9182011-09-06 13:50:29 +0200950 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann7336b9f2011-09-06 13:50:32 +0200951 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +0200952 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
953 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +0200954 { .id = 0 }
955};
956
David Herrmann02fb72a2011-07-05 13:45:09 +0200957static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
958 u8 *raw_data, int size)
959{
David Herrmann4d36e972011-07-05 13:45:12 +0200960 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200961 struct wiiproto_handler *h;
962 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +0200963 unsigned long flags;
David Herrmann7336b9f2011-09-06 13:50:32 +0200964 bool handled = false;
David Herrmann4d36e972011-07-05 13:45:12 +0200965
David Herrmann02fb72a2011-07-05 13:45:09 +0200966 if (size < 1)
967 return -EINVAL;
968
David Herrmann32a0d9a2011-07-05 13:45:18 +0200969 spin_lock_irqsave(&wdata->state.lock, flags);
970
David Herrmanna4d19192011-07-05 13:45:15 +0200971 for (i = 0; handlers[i].id; ++i) {
972 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +0200973 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +0200974 h->func(wdata, &raw_data[1]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200975 handled = true;
976 }
David Herrmanna4d19192011-07-05 13:45:15 +0200977 }
978
David Herrmann7336b9f2011-09-06 13:50:32 +0200979 if (!handled)
980 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
981 size);
982
David Herrmann32a0d9a2011-07-05 13:45:18 +0200983 spin_unlock_irqrestore(&wdata->state.lock, flags);
984
David Herrmann02fb72a2011-07-05 13:45:09 +0200985 return 0;
986}
987
David Herrmann23a5a4a2011-08-17 11:43:22 +0200988static void wiimote_leds_destroy(struct wiimote_data *wdata)
989{
990 int i;
991 struct led_classdev *led;
992
993 for (i = 0; i < 4; ++i) {
994 if (wdata->leds[i]) {
995 led = wdata->leds[i];
996 wdata->leds[i] = NULL;
997 led_classdev_unregister(led);
998 kfree(led);
999 }
1000 }
1001}
1002
1003static int wiimote_leds_create(struct wiimote_data *wdata)
1004{
1005 int i, ret;
1006 struct device *dev = &wdata->hdev->dev;
1007 size_t namesz = strlen(dev_name(dev)) + 9;
1008 struct led_classdev *led;
1009 char *name;
1010
1011 for (i = 0; i < 4; ++i) {
1012 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
1013 if (!led) {
1014 ret = -ENOMEM;
1015 goto err;
1016 }
1017 name = (void*)&led[1];
1018 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
1019 led->name = name;
1020 led->brightness = 0;
1021 led->max_brightness = 1;
1022 led->brightness_get = wiimote_leds_get;
1023 led->brightness_set = wiimote_leds_set;
1024
1025 ret = led_classdev_register(dev, led);
1026 if (ret) {
1027 kfree(led);
1028 goto err;
1029 }
1030 wdata->leds[i] = led;
1031 }
1032
1033 return 0;
1034
1035err:
1036 wiimote_leds_destroy(wdata);
1037 return ret;
1038}
1039
David Herrmanne894d0e2011-07-05 13:45:10 +02001040static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1041{
1042 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +02001043 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +02001044
1045 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1046 if (!wdata)
1047 return NULL;
1048
David Herrmann672bc4e2011-07-05 13:45:11 +02001049 wdata->input = input_allocate_device();
David Herrmann98a558a2011-09-06 13:50:28 +02001050 if (!wdata->input)
1051 goto err;
David Herrmann672bc4e2011-07-05 13:45:11 +02001052
David Herrmanne894d0e2011-07-05 13:45:10 +02001053 wdata->hdev = hdev;
1054 hid_set_drvdata(hdev, wdata);
1055
David Herrmann672bc4e2011-07-05 13:45:11 +02001056 input_set_drvdata(wdata->input, wdata);
David Herrmann26af1742011-08-17 11:43:21 +02001057 wdata->input->open = wiimote_input_open;
1058 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +02001059 wdata->input->dev.parent = &wdata->hdev->dev;
1060 wdata->input->id.bustype = wdata->hdev->bus;
1061 wdata->input->id.vendor = wdata->hdev->vendor;
1062 wdata->input->id.product = wdata->hdev->product;
1063 wdata->input->id.version = wdata->hdev->version;
1064 wdata->input->name = WIIMOTE_NAME;
1065
David Herrmann1abb9ad2011-07-05 13:45:16 +02001066 set_bit(EV_KEY, wdata->input->evbit);
1067 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
1068 set_bit(wiiproto_keymap[i], wdata->input->keybit);
1069
David Herrmannd020be92011-09-06 13:50:27 +02001070 set_bit(FF_RUMBLE, wdata->input->ffbit);
David Herrmann98a558a2011-09-06 13:50:28 +02001071 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
1072 goto err_input;
1073
1074 wdata->accel = input_allocate_device();
1075 if (!wdata->accel)
1076 goto err_input;
1077
1078 input_set_drvdata(wdata->accel, wdata);
1079 wdata->accel->open = wiimote_accel_open;
1080 wdata->accel->close = wiimote_accel_close;
1081 wdata->accel->dev.parent = &wdata->hdev->dev;
1082 wdata->accel->id.bustype = wdata->hdev->bus;
1083 wdata->accel->id.vendor = wdata->hdev->vendor;
1084 wdata->accel->id.product = wdata->hdev->product;
1085 wdata->accel->id.version = wdata->hdev->version;
1086 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
1087
1088 set_bit(EV_ABS, wdata->accel->evbit);
1089 set_bit(ABS_RX, wdata->accel->absbit);
1090 set_bit(ABS_RY, wdata->accel->absbit);
1091 set_bit(ABS_RZ, wdata->accel->absbit);
1092 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
1093 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
1094 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
David Herrmannd020be92011-09-06 13:50:27 +02001095
David Herrmannf363e4f2011-09-06 13:50:30 +02001096 wdata->ir = input_allocate_device();
1097 if (!wdata->ir)
1098 goto err_ir;
1099
1100 input_set_drvdata(wdata->ir, wdata);
David Herrmann0370d7c2011-09-06 13:50:37 +02001101 wdata->ir->open = wiimote_ir_open;
1102 wdata->ir->close = wiimote_ir_close;
David Herrmannf363e4f2011-09-06 13:50:30 +02001103 wdata->ir->dev.parent = &wdata->hdev->dev;
1104 wdata->ir->id.bustype = wdata->hdev->bus;
1105 wdata->ir->id.vendor = wdata->hdev->vendor;
1106 wdata->ir->id.product = wdata->hdev->product;
1107 wdata->ir->id.version = wdata->hdev->version;
1108 wdata->ir->name = WIIMOTE_NAME " IR";
1109
1110 set_bit(EV_ABS, wdata->ir->evbit);
1111 set_bit(ABS_HAT0X, wdata->ir->absbit);
1112 set_bit(ABS_HAT0Y, wdata->ir->absbit);
1113 set_bit(ABS_HAT1X, wdata->ir->absbit);
1114 set_bit(ABS_HAT1Y, wdata->ir->absbit);
1115 set_bit(ABS_HAT2X, wdata->ir->absbit);
1116 set_bit(ABS_HAT2Y, wdata->ir->absbit);
1117 set_bit(ABS_HAT3X, wdata->ir->absbit);
1118 set_bit(ABS_HAT3Y, wdata->ir->absbit);
1119 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1120 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1121 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1122 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1123 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1124 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1125 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1126 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1127
David Herrmann23c063c2011-07-05 13:45:14 +02001128 spin_lock_init(&wdata->qlock);
1129 INIT_WORK(&wdata->worker, wiimote_worker);
1130
David Herrmann32a0d9a2011-07-05 13:45:18 +02001131 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001132 init_completion(&wdata->state.ready);
1133 mutex_init(&wdata->state.sync);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001134
David Herrmanne894d0e2011-07-05 13:45:10 +02001135 return wdata;
David Herrmann98a558a2011-09-06 13:50:28 +02001136
David Herrmannf363e4f2011-09-06 13:50:30 +02001137err_ir:
1138 input_free_device(wdata->accel);
David Herrmann98a558a2011-09-06 13:50:28 +02001139err_input:
1140 input_free_device(wdata->input);
1141err:
1142 kfree(wdata);
1143 return NULL;
David Herrmanne894d0e2011-07-05 13:45:10 +02001144}
1145
1146static void wiimote_destroy(struct wiimote_data *wdata)
1147{
David Herrmann23a5a4a2011-08-17 11:43:22 +02001148 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001149
David Herrmann6591d752011-09-06 13:50:39 +02001150 power_supply_unregister(&wdata->battery);
David Herrmann98a558a2011-09-06 13:50:28 +02001151 input_unregister_device(wdata->accel);
David Herrmannf363e4f2011-09-06 13:50:30 +02001152 input_unregister_device(wdata->ir);
David Herrmann3989ef62011-08-17 11:43:20 +02001153 input_unregister_device(wdata->input);
1154 cancel_work_sync(&wdata->worker);
1155 hid_hw_stop(wdata->hdev);
1156
David Herrmanne894d0e2011-07-05 13:45:10 +02001157 kfree(wdata);
1158}
1159
David Herrmann02fb72a2011-07-05 13:45:09 +02001160static int wiimote_hid_probe(struct hid_device *hdev,
1161 const struct hid_device_id *id)
1162{
David Herrmanne894d0e2011-07-05 13:45:10 +02001163 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001164 int ret;
1165
David Herrmanne894d0e2011-07-05 13:45:10 +02001166 wdata = wiimote_create(hdev);
1167 if (!wdata) {
1168 hid_err(hdev, "Can't alloc device\n");
1169 return -ENOMEM;
1170 }
1171
David Herrmann02fb72a2011-07-05 13:45:09 +02001172 ret = hid_parse(hdev);
1173 if (ret) {
1174 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001175 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001176 }
1177
1178 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1179 if (ret) {
1180 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001181 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001182 }
1183
David Herrmann98a558a2011-09-06 13:50:28 +02001184 ret = input_register_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001185 if (ret) {
1186 hid_err(hdev, "Cannot register input device\n");
1187 goto err_stop;
1188 }
1189
David Herrmannf363e4f2011-09-06 13:50:30 +02001190 ret = input_register_device(wdata->ir);
1191 if (ret) {
1192 hid_err(hdev, "Cannot register input device\n");
1193 goto err_ir;
1194 }
1195
David Herrmann98a558a2011-09-06 13:50:28 +02001196 ret = input_register_device(wdata->input);
1197 if (ret) {
1198 hid_err(hdev, "Cannot register input device\n");
1199 goto err_input;
1200 }
1201
David Herrmann6591d752011-09-06 13:50:39 +02001202 wdata->battery.properties = wiimote_battery_props;
1203 wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props);
1204 wdata->battery.get_property = wiimote_battery_get_property;
1205 wdata->battery.name = "wiimote_battery";
1206 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1207 wdata->battery.use_for_apm = 0;
1208
1209 ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
1210 if (ret) {
1211 hid_err(hdev, "Cannot register battery device\n");
1212 goto err_battery;
1213 }
1214
David Herrmann23a5a4a2011-08-17 11:43:22 +02001215 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001216 if (ret)
1217 goto err_free;
1218
David Herrmann02fb72a2011-07-05 13:45:09 +02001219 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001220
1221 /* by default set led1 after device initialization */
1222 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +02001223 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001224 spin_unlock_irq(&wdata->state.lock);
1225
David Herrmann02fb72a2011-07-05 13:45:09 +02001226 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001227
David Herrmann3989ef62011-08-17 11:43:20 +02001228err_free:
1229 wiimote_destroy(wdata);
1230 return ret;
1231
David Herrmann6591d752011-09-06 13:50:39 +02001232err_battery:
1233 input_unregister_device(wdata->input);
1234 wdata->input = NULL;
David Herrmann98a558a2011-09-06 13:50:28 +02001235err_input:
David Herrmannf363e4f2011-09-06 13:50:30 +02001236 input_unregister_device(wdata->ir);
1237 wdata->ir = NULL;
1238err_ir:
David Herrmann98a558a2011-09-06 13:50:28 +02001239 input_unregister_device(wdata->accel);
1240 wdata->accel = NULL;
David Herrmann672bc4e2011-07-05 13:45:11 +02001241err_stop:
1242 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001243err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001244 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001245 input_free_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001246 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +02001247 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001248 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001249}
1250
1251static void wiimote_hid_remove(struct hid_device *hdev)
1252{
David Herrmanne894d0e2011-07-05 13:45:10 +02001253 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1254
David Herrmann02fb72a2011-07-05 13:45:09 +02001255 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001256 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001257}
1258
1259static const struct hid_device_id wiimote_hid_devices[] = {
1260 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1261 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1262 { }
1263};
1264MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1265
1266static struct hid_driver wiimote_hid_driver = {
1267 .name = "wiimote",
1268 .id_table = wiimote_hid_devices,
1269 .probe = wiimote_hid_probe,
1270 .remove = wiimote_hid_remove,
1271 .raw_event = wiimote_hid_event,
1272};
1273
David Herrmannfb51b442011-07-05 13:45:08 +02001274static int __init wiimote_init(void)
1275{
David Herrmann02fb72a2011-07-05 13:45:09 +02001276 int ret;
1277
1278 ret = hid_register_driver(&wiimote_hid_driver);
1279 if (ret)
1280 pr_err("Can't register wiimote hid driver\n");
1281
1282 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +02001283}
1284
1285static void __exit wiimote_exit(void)
1286{
David Herrmann02fb72a2011-07-05 13:45:09 +02001287 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +02001288}
1289
1290module_init(wiimote_init);
1291module_exit(wiimote_exit);
1292MODULE_LICENSE("GPL");
1293MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1294MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
1295MODULE_VERSION(WIIMOTE_VERSION);