blob: 2ca8bfd350ad3f0a8ab3fd59f8df975e9a7d79c8 [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 Herrmann0c218f12011-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;
David Herrmanncb992212011-11-17 14:12:01 +0100204 bool ext = wiiext_active(wdata);
David Herrmannf363e4f2011-09-06 13:50:30 +0200205
206 if (ir == WIIPROTO_FLAG_IR_BASIC) {
207 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
208 return WIIPROTO_REQ_DRM_KAIE;
209 else
210 return WIIPROTO_REQ_DRM_KIE;
211 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
212 return WIIPROTO_REQ_DRM_KAI;
213 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
214 return WIIPROTO_REQ_DRM_SKAI1;
215 } else {
David Herrmanncb992212011-11-17 14:12:01 +0100216 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
217 if (ext)
218 return WIIPROTO_REQ_DRM_KAE;
219 else
220 return WIIPROTO_REQ_DRM_KA;
221 } else {
222 if (ext)
223 return WIIPROTO_REQ_DRM_KE;
224 else
225 return WIIPROTO_REQ_DRM_K;
226 }
David Herrmannf363e4f2011-09-06 13:50:30 +0200227 }
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200228}
229
David Herrmann7e274402011-11-17 14:11:59 +0100230void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200231{
232 __u8 cmd[3];
233
234 if (drm == WIIPROTO_REQ_NULL)
235 drm = select_drm(wdata);
236
237 cmd[0] = WIIPROTO_REQ_DRM;
238 cmd[1] = 0;
239 cmd[2] = drm;
240
David Herrmannc003ec22011-09-06 13:50:26 +0200241 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200242 wiimote_queue(wdata, cmd, sizeof(cmd));
243}
244
David Herrmanne3979a92011-09-06 13:50:38 +0200245static void wiiproto_req_status(struct wiimote_data *wdata)
246{
247 __u8 cmd[2];
248
249 cmd[0] = WIIPROTO_REQ_SREQ;
250 cmd[1] = 0;
251
252 wiiproto_keep_rumble(wdata, &cmd[1]);
253 wiimote_queue(wdata, cmd, sizeof(cmd));
254}
255
David Herrmann98a558a2011-09-06 13:50:28 +0200256static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
257{
258 accel = !!accel;
259 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
260 return;
261
262 if (accel)
263 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
264 else
265 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
266
267 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
268}
269
David Herrmannfc221cd2011-09-06 13:50:36 +0200270static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
271{
272 __u8 cmd[2];
273
274 cmd[0] = WIIPROTO_REQ_IR1;
275 cmd[1] = flags;
276
277 wiiproto_keep_rumble(wdata, &cmd[1]);
278 wiimote_queue(wdata, cmd, sizeof(cmd));
279}
280
281static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
282{
283 __u8 cmd[2];
284
285 cmd[0] = WIIPROTO_REQ_IR2;
286 cmd[1] = flags;
287
288 wiiproto_keep_rumble(wdata, &cmd[1]);
289 wiimote_queue(wdata, cmd, sizeof(cmd));
290}
291
David Herrmannbe1ecd62011-09-06 13:50:33 +0200292#define wiiproto_req_wreg(wdata, os, buf, sz) \
293 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
294
295#define wiiproto_req_weeprom(wdata, os, buf, sz) \
296 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
297
298static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
299 __u32 offset, const __u8 *buf, __u8 size)
300{
301 __u8 cmd[22];
302
303 if (size > 16 || size == 0) {
304 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
305 return;
306 }
307
308 memset(cmd, 0, sizeof(cmd));
309 cmd[0] = WIIPROTO_REQ_WMEM;
310 cmd[2] = (offset >> 16) & 0xff;
311 cmd[3] = (offset >> 8) & 0xff;
312 cmd[4] = offset & 0xff;
313 cmd[5] = size;
314 memcpy(&cmd[6], buf, size);
315
316 if (!eeprom)
317 cmd[1] |= 0x04;
318
319 wiiproto_keep_rumble(wdata, &cmd[1]);
320 wiimote_queue(wdata, cmd, sizeof(cmd));
321}
322
David Herrmannfad8c0e2011-11-17 14:12:00 +0100323#define wiiproto_req_rreg(wdata, os, sz) \
324 wiiproto_req_rmem((wdata), false, (os), (sz))
325
326#define wiiproto_req_reeprom(wdata, os, sz) \
327 wiiproto_req_rmem((wdata), true, (os), (sz))
328
329static void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
330 __u32 offset, __u16 size)
331{
332 __u8 cmd[7];
333
334 if (size == 0) {
335 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
336 return;
337 }
338
339 cmd[0] = WIIPROTO_REQ_RMEM;
340 cmd[1] = 0;
341 cmd[2] = (offset >> 16) & 0xff;
342 cmd[3] = (offset >> 8) & 0xff;
343 cmd[4] = offset & 0xff;
344 cmd[5] = (size >> 8) & 0xff;
345 cmd[6] = size & 0xff;
346
347 if (!eeprom)
348 cmd[1] |= 0x04;
349
350 wiiproto_keep_rumble(wdata, &cmd[1]);
351 wiimote_queue(wdata, cmd, sizeof(cmd));
352}
353
David Herrmann33e84012011-09-06 13:50:35 +0200354/* requries the cmd-mutex to be held */
David Herrmann7e274402011-11-17 14:11:59 +0100355int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
David Herrmann33e84012011-09-06 13:50:35 +0200356 const __u8 *wmem, __u8 size)
357{
358 unsigned long flags;
359 int ret;
360
361 spin_lock_irqsave(&wdata->state.lock, flags);
362 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
363 wiiproto_req_wreg(wdata, offset, wmem, size);
364 spin_unlock_irqrestore(&wdata->state.lock, flags);
365
366 ret = wiimote_cmd_wait(wdata);
367 if (!ret && wdata->state.cmd_err)
368 ret = -EIO;
369
370 return ret;
371}
372
David Herrmannfad8c0e2011-11-17 14:12:00 +0100373/* requries the cmd-mutex to be held */
374ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
375 __u8 size)
376{
377 unsigned long flags;
378 ssize_t ret;
379
380 spin_lock_irqsave(&wdata->state.lock, flags);
381 wdata->state.cmd_read_size = size;
382 wdata->state.cmd_read_buf = rmem;
383 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
384 wiiproto_req_rreg(wdata, offset, size);
385 spin_unlock_irqrestore(&wdata->state.lock, flags);
386
387 ret = wiimote_cmd_wait(wdata);
388
389 spin_lock_irqsave(&wdata->state.lock, flags);
390 wdata->state.cmd_read_buf = NULL;
391 spin_unlock_irqrestore(&wdata->state.lock, flags);
392
393 if (!ret) {
394 if (wdata->state.cmd_read_size == 0)
395 ret = -EIO;
396 else
397 ret = wdata->state.cmd_read_size;
398 }
399
400 return ret;
401}
402
David Herrmann6591d752011-09-06 13:50:39 +0200403static int wiimote_battery_get_property(struct power_supply *psy,
404 enum power_supply_property psp,
405 union power_supply_propval *val)
406{
407 struct wiimote_data *wdata = container_of(psy,
408 struct wiimote_data, battery);
409 int ret = 0, state;
410 unsigned long flags;
411
412 ret = wiimote_cmd_acquire(wdata);
413 if (ret)
414 return ret;
415
416 spin_lock_irqsave(&wdata->state.lock, flags);
417 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
418 wiiproto_req_status(wdata);
419 spin_unlock_irqrestore(&wdata->state.lock, flags);
420
421 ret = wiimote_cmd_wait(wdata);
422 state = wdata->state.cmd_battery;
423 wiimote_cmd_release(wdata);
424
425 if (ret)
426 return ret;
427
428 switch (psp) {
429 case POWER_SUPPLY_PROP_CAPACITY:
430 val->intval = state * 100 / 255;
431 break;
432 default:
433 ret = -EINVAL;
434 break;
435 }
436
437 return ret;
438}
439
David Herrmannfc221cd2011-09-06 13:50:36 +0200440static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
441{
442 int ret;
443 unsigned long flags;
444 __u8 format = 0;
445 static const __u8 data_enable[] = { 0x01 };
446 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
447 0x00, 0xaa, 0x00, 0x64 };
448 static const __u8 data_sens2[] = { 0x63, 0x03 };
449 static const __u8 data_fin[] = { 0x08 };
450
451 spin_lock_irqsave(&wdata->state.lock, flags);
452
453 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
454 spin_unlock_irqrestore(&wdata->state.lock, flags);
455 return 0;
456 }
457
458 if (mode == 0) {
459 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
460 wiiproto_req_ir1(wdata, 0);
461 wiiproto_req_ir2(wdata, 0);
462 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
463 spin_unlock_irqrestore(&wdata->state.lock, flags);
464 return 0;
465 }
466
467 spin_unlock_irqrestore(&wdata->state.lock, flags);
468
469 ret = wiimote_cmd_acquire(wdata);
470 if (ret)
471 return ret;
472
473 /* send PIXEL CLOCK ENABLE cmd first */
474 spin_lock_irqsave(&wdata->state.lock, flags);
475 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
476 wiiproto_req_ir1(wdata, 0x06);
477 spin_unlock_irqrestore(&wdata->state.lock, flags);
478
479 ret = wiimote_cmd_wait(wdata);
480 if (ret)
481 goto unlock;
482 if (wdata->state.cmd_err) {
483 ret = -EIO;
484 goto unlock;
485 }
486
487 /* enable IR LOGIC */
488 spin_lock_irqsave(&wdata->state.lock, flags);
489 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
490 wiiproto_req_ir2(wdata, 0x06);
491 spin_unlock_irqrestore(&wdata->state.lock, flags);
492
493 ret = wiimote_cmd_wait(wdata);
494 if (ret)
495 goto unlock;
496 if (wdata->state.cmd_err) {
497 ret = -EIO;
498 goto unlock;
499 }
500
501 /* enable IR cam but do not make it send data, yet */
502 ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
503 sizeof(data_enable));
504 if (ret)
505 goto unlock;
506
507 /* write first sensitivity block */
508 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
509 sizeof(data_sens1));
510 if (ret)
511 goto unlock;
512
513 /* write second sensitivity block */
514 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
515 sizeof(data_sens2));
516 if (ret)
517 goto unlock;
518
519 /* put IR cam into desired state */
520 switch (mode) {
521 case WIIPROTO_FLAG_IR_FULL:
522 format = 5;
523 break;
524 case WIIPROTO_FLAG_IR_EXT:
525 format = 3;
526 break;
527 case WIIPROTO_FLAG_IR_BASIC:
528 format = 1;
529 break;
530 }
531 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
532 if (ret)
533 goto unlock;
534
535 /* make IR cam send data */
536 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
537 if (ret)
538 goto unlock;
539
540 /* request new DRM mode compatible to IR mode */
541 spin_lock_irqsave(&wdata->state.lock, flags);
542 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
543 wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
544 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
545 spin_unlock_irqrestore(&wdata->state.lock, flags);
546
547unlock:
548 wiimote_cmd_release(wdata);
549 return ret;
550}
551
David Herrmann23a5a4a2011-08-17 11:43:22 +0200552static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
553{
554 struct wiimote_data *wdata;
555 struct device *dev = led_dev->dev->parent;
556 int i;
557 unsigned long flags;
558 bool value = false;
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200559
David Herrmann23a5a4a2011-08-17 11:43:22 +0200560 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
561
562 for (i = 0; i < 4; ++i) {
563 if (wdata->leds[i] == led_dev) {
564 spin_lock_irqsave(&wdata->state.lock, flags);
565 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
566 spin_unlock_irqrestore(&wdata->state.lock, flags);
567 break;
568 }
569 }
570
571 return value ? LED_FULL : LED_OFF;
572}
573
574static void wiimote_leds_set(struct led_classdev *led_dev,
575 enum led_brightness value)
576{
577 struct wiimote_data *wdata;
578 struct device *dev = led_dev->dev->parent;
579 int i;
580 unsigned long flags;
581 __u8 state, flag;
582
583 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
584
585 for (i = 0; i < 4; ++i) {
586 if (wdata->leds[i] == led_dev) {
587 flag = WIIPROTO_FLAG_LED(i + 1);
588 spin_lock_irqsave(&wdata->state.lock, flags);
589 state = wdata->state.flags;
590 if (value == LED_OFF)
591 wiiproto_req_leds(wdata, state & ~flag);
592 else
593 wiiproto_req_leds(wdata, state | flag);
594 spin_unlock_irqrestore(&wdata->state.lock, flags);
595 break;
596 }
597 }
598}
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200599
David Herrmannd020be92011-09-06 13:50:27 +0200600static int wiimote_ff_play(struct input_dev *dev, void *data,
601 struct ff_effect *eff)
David Herrmann672bc4e2011-07-05 13:45:11 +0200602{
David Herrmannd020be92011-09-06 13:50:27 +0200603 struct wiimote_data *wdata = input_get_drvdata(dev);
604 __u8 value;
605 unsigned long flags;
606
607 /*
608 * The wiimote supports only a single rumble motor so if any magnitude
609 * is set to non-zero then we start the rumble motor. If both are set to
610 * zero, we stop the rumble motor.
611 */
612
613 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
614 value = 1;
615 else
616 value = 0;
617
618 spin_lock_irqsave(&wdata->state.lock, flags);
619 wiiproto_req_rumble(wdata, value);
620 spin_unlock_irqrestore(&wdata->state.lock, flags);
621
David Herrmann672bc4e2011-07-05 13:45:11 +0200622 return 0;
623}
624
David Herrmann26af1742011-08-17 11:43:21 +0200625static int wiimote_input_open(struct input_dev *dev)
626{
627 struct wiimote_data *wdata = input_get_drvdata(dev);
628
629 return hid_hw_open(wdata->hdev);
630}
631
632static void wiimote_input_close(struct input_dev *dev)
633{
634 struct wiimote_data *wdata = input_get_drvdata(dev);
635
636 hid_hw_close(wdata->hdev);
637}
638
David Herrmann98a558a2011-09-06 13:50:28 +0200639static int wiimote_accel_open(struct input_dev *dev)
640{
641 struct wiimote_data *wdata = input_get_drvdata(dev);
642 int ret;
643 unsigned long flags;
644
645 ret = hid_hw_open(wdata->hdev);
646 if (ret)
647 return ret;
648
649 spin_lock_irqsave(&wdata->state.lock, flags);
650 wiiproto_req_accel(wdata, true);
651 spin_unlock_irqrestore(&wdata->state.lock, flags);
652
653 return 0;
654}
655
656static void wiimote_accel_close(struct input_dev *dev)
657{
658 struct wiimote_data *wdata = input_get_drvdata(dev);
659 unsigned long flags;
660
661 spin_lock_irqsave(&wdata->state.lock, flags);
662 wiiproto_req_accel(wdata, false);
663 spin_unlock_irqrestore(&wdata->state.lock, flags);
664
665 hid_hw_close(wdata->hdev);
666}
667
David Herrmann0370d7c2011-09-06 13:50:37 +0200668static int wiimote_ir_open(struct input_dev *dev)
669{
670 struct wiimote_data *wdata = input_get_drvdata(dev);
671 int ret;
672
673 ret = hid_hw_open(wdata->hdev);
674 if (ret)
675 return ret;
676
677 ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
678 if (ret) {
679 hid_hw_close(wdata->hdev);
680 return ret;
681 }
682
683 return 0;
684}
685
686static void wiimote_ir_close(struct input_dev *dev)
687{
688 struct wiimote_data *wdata = input_get_drvdata(dev);
689
690 wiimote_init_ir(wdata, 0);
691 hid_hw_close(wdata->hdev);
692}
693
David Herrmann1abb9ad2011-07-05 13:45:16 +0200694static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
695{
696 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
697 !!(payload[0] & 0x01));
698 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
699 !!(payload[0] & 0x02));
700 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
701 !!(payload[0] & 0x04));
702 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
703 !!(payload[0] & 0x08));
704 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
705 !!(payload[0] & 0x10));
706 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
707 !!(payload[1] & 0x01));
708 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
709 !!(payload[1] & 0x02));
710 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
711 !!(payload[1] & 0x04));
712 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
713 !!(payload[1] & 0x08));
714 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
715 !!(payload[1] & 0x10));
716 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
717 !!(payload[1] & 0x80));
718 input_sync(wdata->input);
719}
720
David Herrmannefcf9182011-09-06 13:50:29 +0200721static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
722{
723 __u16 x, y, z;
724
725 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
726 return;
727
728 /*
729 * payload is: BB BB XX YY ZZ
730 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
731 * contain the upper 8 bits of each value. The lower 2 bits are
732 * contained in the buttons data BB BB.
733 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
734 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
735 * accel value and bit 6 is the second bit of the Z value.
736 * The first bit of Y and Z values is not available and always set to 0.
737 * 0x200 is returned on no movement.
738 */
739
740 x = payload[2] << 2;
741 y = payload[3] << 2;
742 z = payload[4] << 2;
743
744 x |= (payload[0] >> 5) & 0x3;
745 y |= (payload[1] >> 4) & 0x2;
746 z |= (payload[1] >> 5) & 0x2;
747
748 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
749 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
750 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
751 input_sync(wdata->accel);
752}
753
David Herrmanneac39e72011-09-06 13:50:31 +0200754#define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
755 ABS_HAT0X, ABS_HAT0Y)
756#define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
757 ABS_HAT1X, ABS_HAT1Y)
758#define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
759 ABS_HAT2X, ABS_HAT2Y)
760#define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
761 ABS_HAT3X, ABS_HAT3Y)
762
763static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
764 bool packed, __u8 xid, __u8 yid)
765{
766 __u16 x, y;
767
768 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
769 return;
770
771 /*
772 * Basic IR data is encoded into 3 bytes. The first two bytes are the
773 * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits
774 * of both.
775 * If data is packed, then the 3rd byte is put first and slightly
776 * reordered. This allows to interleave packed and non-packed data to
777 * have two IR sets in 5 bytes instead of 6.
778 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
779 */
780
781 if (packed) {
782 x = ir[1] << 2;
783 y = ir[2] << 2;
784
785 x |= ir[0] & 0x3;
786 y |= (ir[0] >> 2) & 0x3;
787 } else {
788 x = ir[0] << 2;
789 y = ir[1] << 2;
790
791 x |= (ir[2] >> 4) & 0x3;
792 y |= (ir[2] >> 6) & 0x3;
793 }
794
795 input_report_abs(wdata->ir, xid, x);
796 input_report_abs(wdata->ir, yid, y);
797}
David Herrmannefcf9182011-09-06 13:50:29 +0200798
David Herrmannc87019e2011-08-17 11:43:24 +0200799static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
800{
801 handler_keys(wdata, payload);
802
803 /* on status reports the drm is reset so we need to resend the drm */
804 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
David Herrmanne3979a92011-09-06 13:50:38 +0200805
David Herrmanncb992212011-11-17 14:12:01 +0100806 wiiext_event(wdata, payload[2] & 0x02);
807
David Herrmanne3979a92011-09-06 13:50:38 +0200808 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) {
809 wdata->state.cmd_battery = payload[5];
810 wiimote_cmd_complete(wdata);
811 }
David Herrmannc87019e2011-08-17 11:43:24 +0200812}
813
David Herrmannbe1ecd62011-09-06 13:50:33 +0200814static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
815{
David Herrmannfad8c0e2011-11-17 14:12:00 +0100816 __u16 offset = payload[3] << 8 | payload[4];
817 __u8 size = (payload[2] >> 4) + 1;
818 __u8 err = payload[2] & 0x0f;
819
David Herrmannbe1ecd62011-09-06 13:50:33 +0200820 handler_keys(wdata, payload);
David Herrmannfad8c0e2011-11-17 14:12:00 +0100821
822 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
823 if (err)
824 size = 0;
825 else if (size > wdata->state.cmd_read_size)
826 size = wdata->state.cmd_read_size;
827
828 wdata->state.cmd_read_size = size;
829 if (wdata->state.cmd_read_buf)
830 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
831 wiimote_cmd_complete(wdata);
832 }
David Herrmannbe1ecd62011-09-06 13:50:33 +0200833}
834
David Herrmannc87019e2011-08-17 11:43:24 +0200835static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
836{
837 __u8 err = payload[3];
838 __u8 cmd = payload[2];
839
840 handler_keys(wdata, payload);
841
David Herrmann33e84012011-09-06 13:50:35 +0200842 if (wiimote_cmd_pending(wdata, cmd, 0)) {
843 wdata->state.cmd_err = err;
844 wiimote_cmd_complete(wdata);
845 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +0200846 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
847 cmd);
David Herrmann33e84012011-09-06 13:50:35 +0200848 }
David Herrmannc87019e2011-08-17 11:43:24 +0200849}
850
David Herrmannefcf9182011-09-06 13:50:29 +0200851static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
852{
853 handler_keys(wdata, payload);
854 handler_accel(wdata, payload);
855}
856
David Herrmann7336b9f2011-09-06 13:50:32 +0200857static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
858{
859 handler_keys(wdata, payload);
860}
861
David Herrmannefcf9182011-09-06 13:50:29 +0200862static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
863{
864 handler_keys(wdata, payload);
865 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200866 ir_to_input0(wdata, &payload[5], false);
867 ir_to_input1(wdata, &payload[8], false);
868 ir_to_input2(wdata, &payload[11], false);
869 ir_to_input3(wdata, &payload[14], false);
870 input_sync(wdata->ir);
871}
872
David Herrmann7336b9f2011-09-06 13:50:32 +0200873static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
874{
875 handler_keys(wdata, payload);
876}
877
David Herrmanneac39e72011-09-06 13:50:31 +0200878static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
879{
880 handler_keys(wdata, payload);
881 ir_to_input0(wdata, &payload[2], false);
882 ir_to_input1(wdata, &payload[4], true);
883 ir_to_input2(wdata, &payload[7], false);
884 ir_to_input3(wdata, &payload[9], true);
885 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200886}
887
888static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
889{
890 handler_keys(wdata, payload);
891 handler_accel(wdata, payload);
892}
893
894static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
895{
896 handler_keys(wdata, payload);
897 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200898 ir_to_input0(wdata, &payload[5], false);
899 ir_to_input1(wdata, &payload[7], true);
900 ir_to_input2(wdata, &payload[10], false);
901 ir_to_input3(wdata, &payload[12], true);
902 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200903}
904
David Herrmann7336b9f2011-09-06 13:50:32 +0200905static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
906{
907}
908
David Herrmannefcf9182011-09-06 13:50:29 +0200909static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
910{
911 handler_keys(wdata, payload);
912
913 wdata->state.accel_split[0] = payload[2];
914 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
915 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +0200916
917 ir_to_input0(wdata, &payload[3], false);
918 ir_to_input1(wdata, &payload[12], false);
919 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200920}
921
922static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
923{
924 __u8 buf[5];
925
926 handler_keys(wdata, payload);
927
928 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
929 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
930
931 buf[0] = 0;
932 buf[1] = 0;
933 buf[2] = wdata->state.accel_split[0];
934 buf[3] = payload[2];
935 buf[4] = wdata->state.accel_split[1];
936 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +0200937
938 ir_to_input2(wdata, &payload[3], false);
939 ir_to_input3(wdata, &payload[12], false);
940 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200941}
942
David Herrmanna4d19192011-07-05 13:45:15 +0200943struct wiiproto_handler {
944 __u8 id;
945 size_t size;
946 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
947};
948
949static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200950 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmannbe1ecd62011-09-06 13:50:33 +0200951 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmannc87019e2011-08-17 11:43:24 +0200952 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200953 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +0200954 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann7336b9f2011-09-06 13:50:32 +0200955 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmannefcf9182011-09-06 13:50:29 +0200956 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann7336b9f2011-09-06 13:50:32 +0200957 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmannefcf9182011-09-06 13:50:29 +0200958 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmanneac39e72011-09-06 13:50:31 +0200959 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmannefcf9182011-09-06 13:50:29 +0200960 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann7336b9f2011-09-06 13:50:32 +0200961 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +0200962 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
963 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +0200964 { .id = 0 }
965};
966
David Herrmann02fb72a2011-07-05 13:45:09 +0200967static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
968 u8 *raw_data, int size)
969{
David Herrmann4d36e972011-07-05 13:45:12 +0200970 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200971 struct wiiproto_handler *h;
972 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +0200973 unsigned long flags;
David Herrmann7336b9f2011-09-06 13:50:32 +0200974 bool handled = false;
David Herrmann4d36e972011-07-05 13:45:12 +0200975
David Herrmann02fb72a2011-07-05 13:45:09 +0200976 if (size < 1)
977 return -EINVAL;
978
David Herrmann32a0d9a2011-07-05 13:45:18 +0200979 spin_lock_irqsave(&wdata->state.lock, flags);
980
David Herrmanna4d19192011-07-05 13:45:15 +0200981 for (i = 0; handlers[i].id; ++i) {
982 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +0200983 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +0200984 h->func(wdata, &raw_data[1]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200985 handled = true;
986 }
David Herrmanna4d19192011-07-05 13:45:15 +0200987 }
988
David Herrmann7336b9f2011-09-06 13:50:32 +0200989 if (!handled)
990 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
991 size);
992
David Herrmann32a0d9a2011-07-05 13:45:18 +0200993 spin_unlock_irqrestore(&wdata->state.lock, flags);
994
David Herrmann02fb72a2011-07-05 13:45:09 +0200995 return 0;
996}
997
David Herrmann23a5a4a2011-08-17 11:43:22 +0200998static void wiimote_leds_destroy(struct wiimote_data *wdata)
999{
1000 int i;
1001 struct led_classdev *led;
1002
1003 for (i = 0; i < 4; ++i) {
1004 if (wdata->leds[i]) {
1005 led = wdata->leds[i];
1006 wdata->leds[i] = NULL;
1007 led_classdev_unregister(led);
1008 kfree(led);
1009 }
1010 }
1011}
1012
1013static int wiimote_leds_create(struct wiimote_data *wdata)
1014{
1015 int i, ret;
1016 struct device *dev = &wdata->hdev->dev;
1017 size_t namesz = strlen(dev_name(dev)) + 9;
1018 struct led_classdev *led;
1019 char *name;
1020
1021 for (i = 0; i < 4; ++i) {
1022 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
1023 if (!led) {
1024 ret = -ENOMEM;
1025 goto err;
1026 }
1027 name = (void*)&led[1];
1028 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
1029 led->name = name;
1030 led->brightness = 0;
1031 led->max_brightness = 1;
1032 led->brightness_get = wiimote_leds_get;
1033 led->brightness_set = wiimote_leds_set;
1034
1035 ret = led_classdev_register(dev, led);
1036 if (ret) {
1037 kfree(led);
1038 goto err;
1039 }
1040 wdata->leds[i] = led;
1041 }
1042
1043 return 0;
1044
1045err:
1046 wiimote_leds_destroy(wdata);
1047 return ret;
1048}
1049
David Herrmanne894d0e2011-07-05 13:45:10 +02001050static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1051{
1052 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +02001053 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +02001054
1055 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1056 if (!wdata)
1057 return NULL;
1058
David Herrmann672bc4e2011-07-05 13:45:11 +02001059 wdata->input = input_allocate_device();
David Herrmann98a558a2011-09-06 13:50:28 +02001060 if (!wdata->input)
1061 goto err;
David Herrmann672bc4e2011-07-05 13:45:11 +02001062
David Herrmanne894d0e2011-07-05 13:45:10 +02001063 wdata->hdev = hdev;
1064 hid_set_drvdata(hdev, wdata);
1065
David Herrmann672bc4e2011-07-05 13:45:11 +02001066 input_set_drvdata(wdata->input, wdata);
David Herrmann26af1742011-08-17 11:43:21 +02001067 wdata->input->open = wiimote_input_open;
1068 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +02001069 wdata->input->dev.parent = &wdata->hdev->dev;
1070 wdata->input->id.bustype = wdata->hdev->bus;
1071 wdata->input->id.vendor = wdata->hdev->vendor;
1072 wdata->input->id.product = wdata->hdev->product;
1073 wdata->input->id.version = wdata->hdev->version;
1074 wdata->input->name = WIIMOTE_NAME;
1075
David Herrmann1abb9ad2011-07-05 13:45:16 +02001076 set_bit(EV_KEY, wdata->input->evbit);
1077 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
1078 set_bit(wiiproto_keymap[i], wdata->input->keybit);
1079
David Herrmannd020be92011-09-06 13:50:27 +02001080 set_bit(FF_RUMBLE, wdata->input->ffbit);
David Herrmann98a558a2011-09-06 13:50:28 +02001081 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
1082 goto err_input;
1083
1084 wdata->accel = input_allocate_device();
1085 if (!wdata->accel)
1086 goto err_input;
1087
1088 input_set_drvdata(wdata->accel, wdata);
1089 wdata->accel->open = wiimote_accel_open;
1090 wdata->accel->close = wiimote_accel_close;
1091 wdata->accel->dev.parent = &wdata->hdev->dev;
1092 wdata->accel->id.bustype = wdata->hdev->bus;
1093 wdata->accel->id.vendor = wdata->hdev->vendor;
1094 wdata->accel->id.product = wdata->hdev->product;
1095 wdata->accel->id.version = wdata->hdev->version;
1096 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
1097
1098 set_bit(EV_ABS, wdata->accel->evbit);
1099 set_bit(ABS_RX, wdata->accel->absbit);
1100 set_bit(ABS_RY, wdata->accel->absbit);
1101 set_bit(ABS_RZ, wdata->accel->absbit);
1102 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
1103 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
1104 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
David Herrmannd020be92011-09-06 13:50:27 +02001105
David Herrmannf363e4f2011-09-06 13:50:30 +02001106 wdata->ir = input_allocate_device();
1107 if (!wdata->ir)
1108 goto err_ir;
1109
1110 input_set_drvdata(wdata->ir, wdata);
David Herrmann0370d7c2011-09-06 13:50:37 +02001111 wdata->ir->open = wiimote_ir_open;
1112 wdata->ir->close = wiimote_ir_close;
David Herrmannf363e4f2011-09-06 13:50:30 +02001113 wdata->ir->dev.parent = &wdata->hdev->dev;
1114 wdata->ir->id.bustype = wdata->hdev->bus;
1115 wdata->ir->id.vendor = wdata->hdev->vendor;
1116 wdata->ir->id.product = wdata->hdev->product;
1117 wdata->ir->id.version = wdata->hdev->version;
1118 wdata->ir->name = WIIMOTE_NAME " IR";
1119
1120 set_bit(EV_ABS, wdata->ir->evbit);
1121 set_bit(ABS_HAT0X, wdata->ir->absbit);
1122 set_bit(ABS_HAT0Y, wdata->ir->absbit);
1123 set_bit(ABS_HAT1X, wdata->ir->absbit);
1124 set_bit(ABS_HAT1Y, wdata->ir->absbit);
1125 set_bit(ABS_HAT2X, wdata->ir->absbit);
1126 set_bit(ABS_HAT2Y, wdata->ir->absbit);
1127 set_bit(ABS_HAT3X, wdata->ir->absbit);
1128 set_bit(ABS_HAT3Y, wdata->ir->absbit);
1129 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1130 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1131 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1132 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1133 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1134 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1135 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1136 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1137
David Herrmann23c063c2011-07-05 13:45:14 +02001138 spin_lock_init(&wdata->qlock);
1139 INIT_WORK(&wdata->worker, wiimote_worker);
1140
David Herrmann32a0d9a2011-07-05 13:45:18 +02001141 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001142 init_completion(&wdata->state.ready);
1143 mutex_init(&wdata->state.sync);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001144
David Herrmanne894d0e2011-07-05 13:45:10 +02001145 return wdata;
David Herrmann98a558a2011-09-06 13:50:28 +02001146
David Herrmannf363e4f2011-09-06 13:50:30 +02001147err_ir:
1148 input_free_device(wdata->accel);
David Herrmann98a558a2011-09-06 13:50:28 +02001149err_input:
1150 input_free_device(wdata->input);
1151err:
1152 kfree(wdata);
1153 return NULL;
David Herrmanne894d0e2011-07-05 13:45:10 +02001154}
1155
1156static void wiimote_destroy(struct wiimote_data *wdata)
1157{
David Herrmanncb992212011-11-17 14:12:01 +01001158 wiiext_deinit(wdata);
David Herrmann23a5a4a2011-08-17 11:43:22 +02001159 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001160
David Herrmann6591d752011-09-06 13:50:39 +02001161 power_supply_unregister(&wdata->battery);
David Herrmann98a558a2011-09-06 13:50:28 +02001162 input_unregister_device(wdata->accel);
David Herrmannf363e4f2011-09-06 13:50:30 +02001163 input_unregister_device(wdata->ir);
David Herrmann3989ef62011-08-17 11:43:20 +02001164 input_unregister_device(wdata->input);
1165 cancel_work_sync(&wdata->worker);
1166 hid_hw_stop(wdata->hdev);
1167
David Herrmanne894d0e2011-07-05 13:45:10 +02001168 kfree(wdata);
1169}
1170
David Herrmann02fb72a2011-07-05 13:45:09 +02001171static int wiimote_hid_probe(struct hid_device *hdev,
1172 const struct hid_device_id *id)
1173{
David Herrmanne894d0e2011-07-05 13:45:10 +02001174 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001175 int ret;
1176
David Herrmanne894d0e2011-07-05 13:45:10 +02001177 wdata = wiimote_create(hdev);
1178 if (!wdata) {
1179 hid_err(hdev, "Can't alloc device\n");
1180 return -ENOMEM;
1181 }
1182
David Herrmann02fb72a2011-07-05 13:45:09 +02001183 ret = hid_parse(hdev);
1184 if (ret) {
1185 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001186 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001187 }
1188
1189 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1190 if (ret) {
1191 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001192 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001193 }
1194
David Herrmann98a558a2011-09-06 13:50:28 +02001195 ret = input_register_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001196 if (ret) {
1197 hid_err(hdev, "Cannot register input device\n");
1198 goto err_stop;
1199 }
1200
David Herrmannf363e4f2011-09-06 13:50:30 +02001201 ret = input_register_device(wdata->ir);
1202 if (ret) {
1203 hid_err(hdev, "Cannot register input device\n");
1204 goto err_ir;
1205 }
1206
David Herrmann98a558a2011-09-06 13:50:28 +02001207 ret = input_register_device(wdata->input);
1208 if (ret) {
1209 hid_err(hdev, "Cannot register input device\n");
1210 goto err_input;
1211 }
1212
David Herrmann6591d752011-09-06 13:50:39 +02001213 wdata->battery.properties = wiimote_battery_props;
1214 wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props);
1215 wdata->battery.get_property = wiimote_battery_get_property;
1216 wdata->battery.name = "wiimote_battery";
1217 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1218 wdata->battery.use_for_apm = 0;
1219
1220 ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
1221 if (ret) {
1222 hid_err(hdev, "Cannot register battery device\n");
1223 goto err_battery;
1224 }
1225
David Herrmann23a5a4a2011-08-17 11:43:22 +02001226 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001227 if (ret)
1228 goto err_free;
1229
David Herrmanncb992212011-11-17 14:12:01 +01001230 ret = wiiext_init(wdata);
1231 if (ret)
1232 goto err_free;
1233
David Herrmann02fb72a2011-07-05 13:45:09 +02001234 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001235
1236 /* by default set led1 after device initialization */
1237 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +02001238 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001239 spin_unlock_irq(&wdata->state.lock);
1240
David Herrmann02fb72a2011-07-05 13:45:09 +02001241 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001242
David Herrmann3989ef62011-08-17 11:43:20 +02001243err_free:
1244 wiimote_destroy(wdata);
1245 return ret;
1246
David Herrmann6591d752011-09-06 13:50:39 +02001247err_battery:
1248 input_unregister_device(wdata->input);
1249 wdata->input = NULL;
David Herrmann98a558a2011-09-06 13:50:28 +02001250err_input:
David Herrmannf363e4f2011-09-06 13:50:30 +02001251 input_unregister_device(wdata->ir);
1252 wdata->ir = NULL;
1253err_ir:
David Herrmann98a558a2011-09-06 13:50:28 +02001254 input_unregister_device(wdata->accel);
1255 wdata->accel = NULL;
David Herrmann672bc4e2011-07-05 13:45:11 +02001256err_stop:
1257 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001258err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001259 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001260 input_free_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001261 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +02001262 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001263 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001264}
1265
1266static void wiimote_hid_remove(struct hid_device *hdev)
1267{
David Herrmanne894d0e2011-07-05 13:45:10 +02001268 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1269
David Herrmann02fb72a2011-07-05 13:45:09 +02001270 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001271 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001272}
1273
1274static const struct hid_device_id wiimote_hid_devices[] = {
1275 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1276 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1277 { }
1278};
1279MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1280
1281static struct hid_driver wiimote_hid_driver = {
1282 .name = "wiimote",
1283 .id_table = wiimote_hid_devices,
1284 .probe = wiimote_hid_probe,
1285 .remove = wiimote_hid_remove,
1286 .raw_event = wiimote_hid_event,
1287};
1288
David Herrmannfb51b442011-07-05 13:45:08 +02001289static int __init wiimote_init(void)
1290{
David Herrmann02fb72a2011-07-05 13:45:09 +02001291 int ret;
1292
1293 ret = hid_register_driver(&wiimote_hid_driver);
1294 if (ret)
1295 pr_err("Can't register wiimote hid driver\n");
1296
1297 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +02001298}
1299
1300static void __exit wiimote_exit(void)
1301{
David Herrmann02fb72a2011-07-05 13:45:09 +02001302 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +02001303}
1304
1305module_init(wiimote_init);
1306module_exit(wiimote_exit);
1307MODULE_LICENSE("GPL");
1308MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1309MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
1310MODULE_VERSION(WIIMOTE_VERSION);