blob: 919abbaba84095d5d8b07f695b587e6310fcbcde [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);
David Herrmann0b6815d2011-11-17 14:12:06 +0100860 wiiext_handle(wdata, &payload[2]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200861}
862
David Herrmannefcf9182011-09-06 13:50:29 +0200863static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
864{
865 handler_keys(wdata, payload);
866 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200867 ir_to_input0(wdata, &payload[5], false);
868 ir_to_input1(wdata, &payload[8], false);
869 ir_to_input2(wdata, &payload[11], false);
870 ir_to_input3(wdata, &payload[14], false);
871 input_sync(wdata->ir);
872}
873
David Herrmann7336b9f2011-09-06 13:50:32 +0200874static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
875{
876 handler_keys(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +0100877 wiiext_handle(wdata, &payload[2]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200878}
879
David Herrmanneac39e72011-09-06 13:50:31 +0200880static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
881{
882 handler_keys(wdata, payload);
883 ir_to_input0(wdata, &payload[2], false);
884 ir_to_input1(wdata, &payload[4], true);
885 ir_to_input2(wdata, &payload[7], false);
886 ir_to_input3(wdata, &payload[9], true);
887 input_sync(wdata->ir);
David Herrmann0b6815d2011-11-17 14:12:06 +0100888 wiiext_handle(wdata, &payload[12]);
David Herrmannefcf9182011-09-06 13:50:29 +0200889}
890
891static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
892{
893 handler_keys(wdata, payload);
894 handler_accel(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +0100895 wiiext_handle(wdata, &payload[5]);
David Herrmannefcf9182011-09-06 13:50:29 +0200896}
897
898static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
899{
900 handler_keys(wdata, payload);
901 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200902 ir_to_input0(wdata, &payload[5], false);
903 ir_to_input1(wdata, &payload[7], true);
904 ir_to_input2(wdata, &payload[10], false);
905 ir_to_input3(wdata, &payload[12], true);
906 input_sync(wdata->ir);
David Herrmann0b6815d2011-11-17 14:12:06 +0100907 wiiext_handle(wdata, &payload[15]);
David Herrmannefcf9182011-09-06 13:50:29 +0200908}
909
David Herrmann7336b9f2011-09-06 13:50:32 +0200910static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
911{
David Herrmann0b6815d2011-11-17 14:12:06 +0100912 wiiext_handle(wdata, payload);
David Herrmann7336b9f2011-09-06 13:50:32 +0200913}
914
David Herrmannefcf9182011-09-06 13:50:29 +0200915static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
916{
917 handler_keys(wdata, payload);
918
919 wdata->state.accel_split[0] = payload[2];
920 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
921 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +0200922
923 ir_to_input0(wdata, &payload[3], false);
924 ir_to_input1(wdata, &payload[12], false);
925 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200926}
927
928static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
929{
930 __u8 buf[5];
931
932 handler_keys(wdata, payload);
933
934 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
935 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
936
937 buf[0] = 0;
938 buf[1] = 0;
939 buf[2] = wdata->state.accel_split[0];
940 buf[3] = payload[2];
941 buf[4] = wdata->state.accel_split[1];
942 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +0200943
944 ir_to_input2(wdata, &payload[3], false);
945 ir_to_input3(wdata, &payload[12], false);
946 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200947}
948
David Herrmanna4d19192011-07-05 13:45:15 +0200949struct wiiproto_handler {
950 __u8 id;
951 size_t size;
952 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
953};
954
955static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200956 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmannbe1ecd62011-09-06 13:50:33 +0200957 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmannc87019e2011-08-17 11:43:24 +0200958 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200959 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +0200960 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann7336b9f2011-09-06 13:50:32 +0200961 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmannefcf9182011-09-06 13:50:29 +0200962 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann7336b9f2011-09-06 13:50:32 +0200963 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmannefcf9182011-09-06 13:50:29 +0200964 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmanneac39e72011-09-06 13:50:31 +0200965 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmannefcf9182011-09-06 13:50:29 +0200966 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann7336b9f2011-09-06 13:50:32 +0200967 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +0200968 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
969 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +0200970 { .id = 0 }
971};
972
David Herrmann02fb72a2011-07-05 13:45:09 +0200973static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
974 u8 *raw_data, int size)
975{
David Herrmann4d36e972011-07-05 13:45:12 +0200976 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200977 struct wiiproto_handler *h;
978 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +0200979 unsigned long flags;
David Herrmann7336b9f2011-09-06 13:50:32 +0200980 bool handled = false;
David Herrmann4d36e972011-07-05 13:45:12 +0200981
David Herrmann02fb72a2011-07-05 13:45:09 +0200982 if (size < 1)
983 return -EINVAL;
984
David Herrmann32a0d9a2011-07-05 13:45:18 +0200985 spin_lock_irqsave(&wdata->state.lock, flags);
986
David Herrmanna4d19192011-07-05 13:45:15 +0200987 for (i = 0; handlers[i].id; ++i) {
988 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +0200989 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +0200990 h->func(wdata, &raw_data[1]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200991 handled = true;
992 }
David Herrmanna4d19192011-07-05 13:45:15 +0200993 }
994
David Herrmann7336b9f2011-09-06 13:50:32 +0200995 if (!handled)
996 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
997 size);
998
David Herrmann32a0d9a2011-07-05 13:45:18 +0200999 spin_unlock_irqrestore(&wdata->state.lock, flags);
1000
David Herrmann02fb72a2011-07-05 13:45:09 +02001001 return 0;
1002}
1003
David Herrmann23a5a4a2011-08-17 11:43:22 +02001004static void wiimote_leds_destroy(struct wiimote_data *wdata)
1005{
1006 int i;
1007 struct led_classdev *led;
1008
1009 for (i = 0; i < 4; ++i) {
1010 if (wdata->leds[i]) {
1011 led = wdata->leds[i];
1012 wdata->leds[i] = NULL;
1013 led_classdev_unregister(led);
1014 kfree(led);
1015 }
1016 }
1017}
1018
1019static int wiimote_leds_create(struct wiimote_data *wdata)
1020{
1021 int i, ret;
1022 struct device *dev = &wdata->hdev->dev;
1023 size_t namesz = strlen(dev_name(dev)) + 9;
1024 struct led_classdev *led;
1025 char *name;
1026
1027 for (i = 0; i < 4; ++i) {
1028 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
1029 if (!led) {
1030 ret = -ENOMEM;
1031 goto err;
1032 }
1033 name = (void*)&led[1];
1034 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
1035 led->name = name;
1036 led->brightness = 0;
1037 led->max_brightness = 1;
1038 led->brightness_get = wiimote_leds_get;
1039 led->brightness_set = wiimote_leds_set;
1040
1041 ret = led_classdev_register(dev, led);
1042 if (ret) {
1043 kfree(led);
1044 goto err;
1045 }
1046 wdata->leds[i] = led;
1047 }
1048
1049 return 0;
1050
1051err:
1052 wiimote_leds_destroy(wdata);
1053 return ret;
1054}
1055
David Herrmanne894d0e2011-07-05 13:45:10 +02001056static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1057{
1058 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +02001059 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +02001060
1061 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1062 if (!wdata)
1063 return NULL;
1064
David Herrmann672bc4e2011-07-05 13:45:11 +02001065 wdata->input = input_allocate_device();
David Herrmann98a558a2011-09-06 13:50:28 +02001066 if (!wdata->input)
1067 goto err;
David Herrmann672bc4e2011-07-05 13:45:11 +02001068
David Herrmanne894d0e2011-07-05 13:45:10 +02001069 wdata->hdev = hdev;
1070 hid_set_drvdata(hdev, wdata);
1071
David Herrmann672bc4e2011-07-05 13:45:11 +02001072 input_set_drvdata(wdata->input, wdata);
David Herrmann26af1742011-08-17 11:43:21 +02001073 wdata->input->open = wiimote_input_open;
1074 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +02001075 wdata->input->dev.parent = &wdata->hdev->dev;
1076 wdata->input->id.bustype = wdata->hdev->bus;
1077 wdata->input->id.vendor = wdata->hdev->vendor;
1078 wdata->input->id.product = wdata->hdev->product;
1079 wdata->input->id.version = wdata->hdev->version;
1080 wdata->input->name = WIIMOTE_NAME;
1081
David Herrmann1abb9ad2011-07-05 13:45:16 +02001082 set_bit(EV_KEY, wdata->input->evbit);
1083 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
1084 set_bit(wiiproto_keymap[i], wdata->input->keybit);
1085
David Herrmannd020be92011-09-06 13:50:27 +02001086 set_bit(FF_RUMBLE, wdata->input->ffbit);
David Herrmann98a558a2011-09-06 13:50:28 +02001087 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
1088 goto err_input;
1089
1090 wdata->accel = input_allocate_device();
1091 if (!wdata->accel)
1092 goto err_input;
1093
1094 input_set_drvdata(wdata->accel, wdata);
1095 wdata->accel->open = wiimote_accel_open;
1096 wdata->accel->close = wiimote_accel_close;
1097 wdata->accel->dev.parent = &wdata->hdev->dev;
1098 wdata->accel->id.bustype = wdata->hdev->bus;
1099 wdata->accel->id.vendor = wdata->hdev->vendor;
1100 wdata->accel->id.product = wdata->hdev->product;
1101 wdata->accel->id.version = wdata->hdev->version;
1102 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
1103
1104 set_bit(EV_ABS, wdata->accel->evbit);
1105 set_bit(ABS_RX, wdata->accel->absbit);
1106 set_bit(ABS_RY, wdata->accel->absbit);
1107 set_bit(ABS_RZ, wdata->accel->absbit);
1108 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
1109 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
1110 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
David Herrmannd020be92011-09-06 13:50:27 +02001111
David Herrmannf363e4f2011-09-06 13:50:30 +02001112 wdata->ir = input_allocate_device();
1113 if (!wdata->ir)
1114 goto err_ir;
1115
1116 input_set_drvdata(wdata->ir, wdata);
David Herrmann0370d7c2011-09-06 13:50:37 +02001117 wdata->ir->open = wiimote_ir_open;
1118 wdata->ir->close = wiimote_ir_close;
David Herrmannf363e4f2011-09-06 13:50:30 +02001119 wdata->ir->dev.parent = &wdata->hdev->dev;
1120 wdata->ir->id.bustype = wdata->hdev->bus;
1121 wdata->ir->id.vendor = wdata->hdev->vendor;
1122 wdata->ir->id.product = wdata->hdev->product;
1123 wdata->ir->id.version = wdata->hdev->version;
1124 wdata->ir->name = WIIMOTE_NAME " IR";
1125
1126 set_bit(EV_ABS, wdata->ir->evbit);
1127 set_bit(ABS_HAT0X, wdata->ir->absbit);
1128 set_bit(ABS_HAT0Y, wdata->ir->absbit);
1129 set_bit(ABS_HAT1X, wdata->ir->absbit);
1130 set_bit(ABS_HAT1Y, wdata->ir->absbit);
1131 set_bit(ABS_HAT2X, wdata->ir->absbit);
1132 set_bit(ABS_HAT2Y, wdata->ir->absbit);
1133 set_bit(ABS_HAT3X, wdata->ir->absbit);
1134 set_bit(ABS_HAT3Y, wdata->ir->absbit);
1135 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1136 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1137 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1138 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1139 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1140 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1141 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1142 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1143
David Herrmann23c063c2011-07-05 13:45:14 +02001144 spin_lock_init(&wdata->qlock);
1145 INIT_WORK(&wdata->worker, wiimote_worker);
1146
David Herrmann32a0d9a2011-07-05 13:45:18 +02001147 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001148 init_completion(&wdata->state.ready);
1149 mutex_init(&wdata->state.sync);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001150
David Herrmanne894d0e2011-07-05 13:45:10 +02001151 return wdata;
David Herrmann98a558a2011-09-06 13:50:28 +02001152
David Herrmannf363e4f2011-09-06 13:50:30 +02001153err_ir:
1154 input_free_device(wdata->accel);
David Herrmann98a558a2011-09-06 13:50:28 +02001155err_input:
1156 input_free_device(wdata->input);
1157err:
1158 kfree(wdata);
1159 return NULL;
David Herrmanne894d0e2011-07-05 13:45:10 +02001160}
1161
1162static void wiimote_destroy(struct wiimote_data *wdata)
1163{
David Herrmann43e5e7c2011-11-17 14:12:10 +01001164 wiidebug_deinit(wdata);
David Herrmanncb992212011-11-17 14:12:01 +01001165 wiiext_deinit(wdata);
David Herrmann23a5a4a2011-08-17 11:43:22 +02001166 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001167
David Herrmann6591d752011-09-06 13:50:39 +02001168 power_supply_unregister(&wdata->battery);
David Herrmann98a558a2011-09-06 13:50:28 +02001169 input_unregister_device(wdata->accel);
David Herrmannf363e4f2011-09-06 13:50:30 +02001170 input_unregister_device(wdata->ir);
David Herrmann3989ef62011-08-17 11:43:20 +02001171 input_unregister_device(wdata->input);
1172 cancel_work_sync(&wdata->worker);
1173 hid_hw_stop(wdata->hdev);
1174
David Herrmanne894d0e2011-07-05 13:45:10 +02001175 kfree(wdata);
1176}
1177
David Herrmann02fb72a2011-07-05 13:45:09 +02001178static int wiimote_hid_probe(struct hid_device *hdev,
1179 const struct hid_device_id *id)
1180{
David Herrmanne894d0e2011-07-05 13:45:10 +02001181 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001182 int ret;
1183
David Herrmanne894d0e2011-07-05 13:45:10 +02001184 wdata = wiimote_create(hdev);
1185 if (!wdata) {
1186 hid_err(hdev, "Can't alloc device\n");
1187 return -ENOMEM;
1188 }
1189
David Herrmann02fb72a2011-07-05 13:45:09 +02001190 ret = hid_parse(hdev);
1191 if (ret) {
1192 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001193 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001194 }
1195
1196 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1197 if (ret) {
1198 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001199 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001200 }
1201
David Herrmann98a558a2011-09-06 13:50:28 +02001202 ret = input_register_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001203 if (ret) {
1204 hid_err(hdev, "Cannot register input device\n");
1205 goto err_stop;
1206 }
1207
David Herrmannf363e4f2011-09-06 13:50:30 +02001208 ret = input_register_device(wdata->ir);
1209 if (ret) {
1210 hid_err(hdev, "Cannot register input device\n");
1211 goto err_ir;
1212 }
1213
David Herrmann98a558a2011-09-06 13:50:28 +02001214 ret = input_register_device(wdata->input);
1215 if (ret) {
1216 hid_err(hdev, "Cannot register input device\n");
1217 goto err_input;
1218 }
1219
David Herrmann6591d752011-09-06 13:50:39 +02001220 wdata->battery.properties = wiimote_battery_props;
1221 wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props);
1222 wdata->battery.get_property = wiimote_battery_get_property;
1223 wdata->battery.name = "wiimote_battery";
1224 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1225 wdata->battery.use_for_apm = 0;
1226
1227 ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
1228 if (ret) {
1229 hid_err(hdev, "Cannot register battery device\n");
1230 goto err_battery;
1231 }
1232
David Herrmann23a5a4a2011-08-17 11:43:22 +02001233 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001234 if (ret)
1235 goto err_free;
1236
David Herrmanncb992212011-11-17 14:12:01 +01001237 ret = wiiext_init(wdata);
1238 if (ret)
1239 goto err_free;
1240
David Herrmann43e5e7c2011-11-17 14:12:10 +01001241 ret = wiidebug_init(wdata);
1242 if (ret)
1243 goto err_free;
1244
David Herrmann02fb72a2011-07-05 13:45:09 +02001245 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001246
1247 /* by default set led1 after device initialization */
1248 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +02001249 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001250 spin_unlock_irq(&wdata->state.lock);
1251
David Herrmann02fb72a2011-07-05 13:45:09 +02001252 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001253
David Herrmann3989ef62011-08-17 11:43:20 +02001254err_free:
1255 wiimote_destroy(wdata);
1256 return ret;
1257
David Herrmann6591d752011-09-06 13:50:39 +02001258err_battery:
1259 input_unregister_device(wdata->input);
1260 wdata->input = NULL;
David Herrmann98a558a2011-09-06 13:50:28 +02001261err_input:
David Herrmannf363e4f2011-09-06 13:50:30 +02001262 input_unregister_device(wdata->ir);
1263 wdata->ir = NULL;
1264err_ir:
David Herrmann98a558a2011-09-06 13:50:28 +02001265 input_unregister_device(wdata->accel);
1266 wdata->accel = NULL;
David Herrmann672bc4e2011-07-05 13:45:11 +02001267err_stop:
1268 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001269err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001270 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001271 input_free_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001272 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +02001273 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001274 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001275}
1276
1277static void wiimote_hid_remove(struct hid_device *hdev)
1278{
David Herrmanne894d0e2011-07-05 13:45:10 +02001279 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1280
David Herrmann02fb72a2011-07-05 13:45:09 +02001281 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001282 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001283}
1284
1285static const struct hid_device_id wiimote_hid_devices[] = {
1286 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1287 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1288 { }
1289};
1290MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1291
1292static struct hid_driver wiimote_hid_driver = {
1293 .name = "wiimote",
1294 .id_table = wiimote_hid_devices,
1295 .probe = wiimote_hid_probe,
1296 .remove = wiimote_hid_remove,
1297 .raw_event = wiimote_hid_event,
1298};
1299
David Herrmannfb51b442011-07-05 13:45:08 +02001300static int __init wiimote_init(void)
1301{
David Herrmann02fb72a2011-07-05 13:45:09 +02001302 int ret;
1303
1304 ret = hid_register_driver(&wiimote_hid_driver);
1305 if (ret)
1306 pr_err("Can't register wiimote hid driver\n");
1307
1308 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +02001309}
1310
1311static void __exit wiimote_exit(void)
1312{
David Herrmann02fb72a2011-07-05 13:45:09 +02001313 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +02001314}
1315
1316module_init(wiimote_init);
1317module_exit(wiimote_exit);
1318MODULE_LICENSE("GPL");
1319MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1320MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
1321MODULE_VERSION(WIIMOTE_VERSION);