blob: 35dc293db40e175d976e0ec438a33ef09ab5545e [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 Herrmann23c063c2011-07-05 13:45:14 +020020#include <linux/spinlock.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020021#include "hid-ids.h"
David Herrmannfb51b442011-07-05 13:45:08 +020022
23#define WIIMOTE_VERSION "0.1"
24#define WIIMOTE_NAME "Nintendo Wii Remote"
David Herrmann23c063c2011-07-05 13:45:14 +020025#define WIIMOTE_BUFSIZE 32
26
27struct wiimote_buf {
28 __u8 data[HID_MAX_BUFFER_SIZE];
29 size_t size;
30};
David Herrmannfb51b442011-07-05 13:45:08 +020031
David Herrmann32a0d9a2011-07-05 13:45:18 +020032struct wiimote_state {
33 spinlock_t lock;
34 __u8 flags;
David Herrmannefcf9182011-09-06 13:50:29 +020035 __u8 accel_split[2];
David Herrmann29d28062011-09-06 13:50:34 +020036
37 /* synchronous cmd requests */
38 struct mutex sync;
39 struct completion ready;
40 int cmd;
41 __u32 opt;
David Herrmann33e84012011-09-06 13:50:35 +020042
43 /* results of synchronous requests */
44 __u8 cmd_err;
David Herrmann32a0d9a2011-07-05 13:45:18 +020045};
46
David Herrmanne894d0e2011-07-05 13:45:10 +020047struct wiimote_data {
48 struct hid_device *hdev;
David Herrmann672bc4e2011-07-05 13:45:11 +020049 struct input_dev *input;
David Herrmann23a5a4a2011-08-17 11:43:22 +020050 struct led_classdev *leds[4];
David Herrmann98a558a2011-09-06 13:50:28 +020051 struct input_dev *accel;
David Herrmannf363e4f2011-09-06 13:50:30 +020052 struct input_dev *ir;
David Herrmann23c063c2011-07-05 13:45:14 +020053
54 spinlock_t qlock;
55 __u8 head;
56 __u8 tail;
57 struct wiimote_buf outq[WIIMOTE_BUFSIZE];
58 struct work_struct worker;
David Herrmann32a0d9a2011-07-05 13:45:18 +020059
60 struct wiimote_state state;
David Herrmanne894d0e2011-07-05 13:45:10 +020061};
62
David Herrmannc003ec22011-09-06 13:50:26 +020063#define WIIPROTO_FLAG_LED1 0x01
64#define WIIPROTO_FLAG_LED2 0x02
65#define WIIPROTO_FLAG_LED3 0x04
66#define WIIPROTO_FLAG_LED4 0x08
67#define WIIPROTO_FLAG_RUMBLE 0x10
David Herrmann98a558a2011-09-06 13:50:28 +020068#define WIIPROTO_FLAG_ACCEL 0x20
David Herrmannf363e4f2011-09-06 13:50:30 +020069#define WIIPROTO_FLAG_IR_BASIC 0x40
70#define WIIPROTO_FLAG_IR_EXT 0x80
71#define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */
David Herrmann32a0d9a2011-07-05 13:45:18 +020072#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
73 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
David Herrmannf363e4f2011-09-06 13:50:30 +020074#define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \
75 WIIPROTO_FLAG_IR_FULL)
David Herrmanndb308342011-07-05 13:45:17 +020076
David Herrmann23a5a4a2011-08-17 11:43:22 +020077/* return flag for led \num */
78#define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
79
David Herrmann1abb9ad2011-07-05 13:45:16 +020080enum wiiproto_reqs {
David Herrmann2cb5e4b2011-08-17 11:43:23 +020081 WIIPROTO_REQ_NULL = 0x0,
David Herrmannc003ec22011-09-06 13:50:26 +020082 WIIPROTO_REQ_RUMBLE = 0x10,
David Herrmanndb308342011-07-05 13:45:17 +020083 WIIPROTO_REQ_LED = 0x11,
David Herrmann2cb5e4b2011-08-17 11:43:23 +020084 WIIPROTO_REQ_DRM = 0x12,
David Herrmannfc221cd2011-09-06 13:50:36 +020085 WIIPROTO_REQ_IR1 = 0x13,
David Herrmannbe1ecd62011-09-06 13:50:33 +020086 WIIPROTO_REQ_WMEM = 0x16,
87 WIIPROTO_REQ_RMEM = 0x17,
David Herrmannfc221cd2011-09-06 13:50:36 +020088 WIIPROTO_REQ_IR2 = 0x1a,
David Herrmannc87019e2011-08-17 11:43:24 +020089 WIIPROTO_REQ_STATUS = 0x20,
David Herrmannbe1ecd62011-09-06 13:50:33 +020090 WIIPROTO_REQ_DATA = 0x21,
David Herrmannc87019e2011-08-17 11:43:24 +020091 WIIPROTO_REQ_RETURN = 0x22,
David Herrmann1abb9ad2011-07-05 13:45:16 +020092 WIIPROTO_REQ_DRM_K = 0x30,
David Herrmannefcf9182011-09-06 13:50:29 +020093 WIIPROTO_REQ_DRM_KA = 0x31,
David Herrmann7336b9f2011-09-06 13:50:32 +020094 WIIPROTO_REQ_DRM_KE = 0x32,
David Herrmannefcf9182011-09-06 13:50:29 +020095 WIIPROTO_REQ_DRM_KAI = 0x33,
David Herrmann7336b9f2011-09-06 13:50:32 +020096 WIIPROTO_REQ_DRM_KEE = 0x34,
David Herrmannefcf9182011-09-06 13:50:29 +020097 WIIPROTO_REQ_DRM_KAE = 0x35,
David Herrmannf363e4f2011-09-06 13:50:30 +020098 WIIPROTO_REQ_DRM_KIE = 0x36,
David Herrmannefcf9182011-09-06 13:50:29 +020099 WIIPROTO_REQ_DRM_KAIE = 0x37,
David Herrmann7336b9f2011-09-06 13:50:32 +0200100 WIIPROTO_REQ_DRM_E = 0x3d,
David Herrmannefcf9182011-09-06 13:50:29 +0200101 WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
102 WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
David Herrmann1abb9ad2011-07-05 13:45:16 +0200103};
104
105enum wiiproto_keys {
106 WIIPROTO_KEY_LEFT,
107 WIIPROTO_KEY_RIGHT,
108 WIIPROTO_KEY_UP,
109 WIIPROTO_KEY_DOWN,
110 WIIPROTO_KEY_PLUS,
111 WIIPROTO_KEY_MINUS,
112 WIIPROTO_KEY_ONE,
113 WIIPROTO_KEY_TWO,
114 WIIPROTO_KEY_A,
115 WIIPROTO_KEY_B,
116 WIIPROTO_KEY_HOME,
117 WIIPROTO_KEY_COUNT
118};
119
120static __u16 wiiproto_keymap[] = {
121 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
122 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
123 KEY_UP, /* WIIPROTO_KEY_UP */
124 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
125 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
126 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
127 BTN_1, /* WIIPROTO_KEY_ONE */
128 BTN_2, /* WIIPROTO_KEY_TWO */
129 BTN_A, /* WIIPROTO_KEY_A */
130 BTN_B, /* WIIPROTO_KEY_B */
131 BTN_MODE, /* WIIPROTO_KEY_HOME */
132};
133
David Herrmann29d28062011-09-06 13:50:34 +0200134/* requires the state.lock spinlock to be held */
135static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
136 __u32 opt)
137{
138 return wdata->state.cmd == cmd && wdata->state.opt == opt;
139}
140
141/* requires the state.lock spinlock to be held */
142static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
143{
144 wdata->state.cmd = WIIPROTO_REQ_NULL;
145 complete(&wdata->state.ready);
146}
147
148static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
149{
150 return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
151}
152
153/* requires the state.lock spinlock to be held */
154static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
155 __u32 opt)
156{
157 INIT_COMPLETION(wdata->state.ready);
158 wdata->state.cmd = cmd;
159 wdata->state.opt = opt;
160}
161
162static inline void wiimote_cmd_release(struct wiimote_data *wdata)
163{
164 mutex_unlock(&wdata->state.sync);
165}
166
167static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
168{
169 int ret;
170
171 ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
172 if (ret < 0)
173 return -ERESTARTSYS;
174 else if (ret == 0)
175 return -EIO;
176 else
177 return 0;
178}
179
David Herrmann0c218f12011-07-05 13:45:13 +0200180static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
181 size_t count)
182{
183 __u8 *buf;
184 ssize_t ret;
185
186 if (!hdev->hid_output_raw_report)
187 return -ENODEV;
188
189 buf = kmemdup(buffer, count, GFP_KERNEL);
190 if (!buf)
191 return -ENOMEM;
192
193 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
194
195 kfree(buf);
196 return ret;
197}
198
David Herrmann23c063c2011-07-05 13:45:14 +0200199static void wiimote_worker(struct work_struct *work)
200{
201 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
202 worker);
203 unsigned long flags;
204
205 spin_lock_irqsave(&wdata->qlock, flags);
206
207 while (wdata->head != wdata->tail) {
208 spin_unlock_irqrestore(&wdata->qlock, flags);
209 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
210 wdata->outq[wdata->tail].size);
211 spin_lock_irqsave(&wdata->qlock, flags);
212
213 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
214 }
215
216 spin_unlock_irqrestore(&wdata->qlock, flags);
217}
218
219static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
220 size_t count)
221{
222 unsigned long flags;
223 __u8 newhead;
224
225 if (count > HID_MAX_BUFFER_SIZE) {
226 hid_warn(wdata->hdev, "Sending too large output report\n");
227 return;
228 }
229
230 /*
231 * Copy new request into our output queue and check whether the
232 * queue is full. If it is full, discard this request.
233 * If it is empty we need to start a new worker that will
234 * send out the buffer to the hid device.
235 * If the queue is not empty, then there must be a worker
236 * that is currently sending out our buffer and this worker
237 * will reschedule itself until the queue is empty.
238 */
239
240 spin_lock_irqsave(&wdata->qlock, flags);
241
242 memcpy(wdata->outq[wdata->head].data, buffer, count);
243 wdata->outq[wdata->head].size = count;
244 newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
245
246 if (wdata->head == wdata->tail) {
247 wdata->head = newhead;
248 schedule_work(&wdata->worker);
249 } else if (newhead != wdata->tail) {
250 wdata->head = newhead;
251 } else {
252 hid_warn(wdata->hdev, "Output queue is full");
253 }
254
255 spin_unlock_irqrestore(&wdata->qlock, flags);
256}
257
David Herrmannc003ec22011-09-06 13:50:26 +0200258/*
259 * This sets the rumble bit on the given output report if rumble is
260 * currently enabled.
261 * \cmd1 must point to the second byte in the output report => &cmd[1]
262 * This must be called on nearly every output report before passing it
263 * into the output queue!
264 */
265static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
266{
267 if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
268 *cmd1 |= 0x01;
269}
270
271static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
272{
273 __u8 cmd[2];
274
275 rumble = !!rumble;
276 if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
277 return;
278
279 if (rumble)
280 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
281 else
282 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
283
284 cmd[0] = WIIPROTO_REQ_RUMBLE;
285 cmd[1] = 0;
286
287 wiiproto_keep_rumble(wdata, &cmd[1]);
288 wiimote_queue(wdata, cmd, sizeof(cmd));
289}
290
David Herrmanndb308342011-07-05 13:45:17 +0200291static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
292{
293 __u8 cmd[2];
294
David Herrmann32a0d9a2011-07-05 13:45:18 +0200295 leds &= WIIPROTO_FLAGS_LEDS;
296 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
297 return;
298 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
299
David Herrmanndb308342011-07-05 13:45:17 +0200300 cmd[0] = WIIPROTO_REQ_LED;
301 cmd[1] = 0;
302
303 if (leds & WIIPROTO_FLAG_LED1)
304 cmd[1] |= 0x10;
305 if (leds & WIIPROTO_FLAG_LED2)
306 cmd[1] |= 0x20;
307 if (leds & WIIPROTO_FLAG_LED3)
308 cmd[1] |= 0x40;
309 if (leds & WIIPROTO_FLAG_LED4)
310 cmd[1] |= 0x80;
311
David Herrmannc003ec22011-09-06 13:50:26 +0200312 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmanndb308342011-07-05 13:45:17 +0200313 wiimote_queue(wdata, cmd, sizeof(cmd));
314}
315
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200316/*
317 * Check what peripherals of the wiimote are currently
318 * active and select a proper DRM that supports all of
319 * the requested data inputs.
320 */
321static __u8 select_drm(struct wiimote_data *wdata)
322{
David Herrmannf363e4f2011-09-06 13:50:30 +0200323 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
324
325 if (ir == WIIPROTO_FLAG_IR_BASIC) {
326 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
327 return WIIPROTO_REQ_DRM_KAIE;
328 else
329 return WIIPROTO_REQ_DRM_KIE;
330 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
331 return WIIPROTO_REQ_DRM_KAI;
332 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
333 return WIIPROTO_REQ_DRM_SKAI1;
334 } else {
335 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
336 return WIIPROTO_REQ_DRM_KA;
337 else
338 return WIIPROTO_REQ_DRM_K;
339 }
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200340}
341
342static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
343{
344 __u8 cmd[3];
345
346 if (drm == WIIPROTO_REQ_NULL)
347 drm = select_drm(wdata);
348
349 cmd[0] = WIIPROTO_REQ_DRM;
350 cmd[1] = 0;
351 cmd[2] = drm;
352
David Herrmannc003ec22011-09-06 13:50:26 +0200353 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200354 wiimote_queue(wdata, cmd, sizeof(cmd));
355}
356
David Herrmann98a558a2011-09-06 13:50:28 +0200357static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
358{
359 accel = !!accel;
360 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
361 return;
362
363 if (accel)
364 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
365 else
366 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
367
368 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
369}
370
David Herrmannfc221cd2011-09-06 13:50:36 +0200371static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
372{
373 __u8 cmd[2];
374
375 cmd[0] = WIIPROTO_REQ_IR1;
376 cmd[1] = flags;
377
378 wiiproto_keep_rumble(wdata, &cmd[1]);
379 wiimote_queue(wdata, cmd, sizeof(cmd));
380}
381
382static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
383{
384 __u8 cmd[2];
385
386 cmd[0] = WIIPROTO_REQ_IR2;
387 cmd[1] = flags;
388
389 wiiproto_keep_rumble(wdata, &cmd[1]);
390 wiimote_queue(wdata, cmd, sizeof(cmd));
391}
392
David Herrmannbe1ecd62011-09-06 13:50:33 +0200393#define wiiproto_req_wreg(wdata, os, buf, sz) \
394 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
395
396#define wiiproto_req_weeprom(wdata, os, buf, sz) \
397 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
398
399static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
400 __u32 offset, const __u8 *buf, __u8 size)
401{
402 __u8 cmd[22];
403
404 if (size > 16 || size == 0) {
405 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
406 return;
407 }
408
409 memset(cmd, 0, sizeof(cmd));
410 cmd[0] = WIIPROTO_REQ_WMEM;
411 cmd[2] = (offset >> 16) & 0xff;
412 cmd[3] = (offset >> 8) & 0xff;
413 cmd[4] = offset & 0xff;
414 cmd[5] = size;
415 memcpy(&cmd[6], buf, size);
416
417 if (!eeprom)
418 cmd[1] |= 0x04;
419
420 wiiproto_keep_rumble(wdata, &cmd[1]);
421 wiimote_queue(wdata, cmd, sizeof(cmd));
422}
423
David Herrmann33e84012011-09-06 13:50:35 +0200424/* requries the cmd-mutex to be held */
425static int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
426 const __u8 *wmem, __u8 size)
427{
428 unsigned long flags;
429 int ret;
430
431 spin_lock_irqsave(&wdata->state.lock, flags);
432 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
433 wiiproto_req_wreg(wdata, offset, wmem, size);
434 spin_unlock_irqrestore(&wdata->state.lock, flags);
435
436 ret = wiimote_cmd_wait(wdata);
437 if (!ret && wdata->state.cmd_err)
438 ret = -EIO;
439
440 return ret;
441}
442
David Herrmannfc221cd2011-09-06 13:50:36 +0200443static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
444{
445 int ret;
446 unsigned long flags;
447 __u8 format = 0;
448 static const __u8 data_enable[] = { 0x01 };
449 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
450 0x00, 0xaa, 0x00, 0x64 };
451 static const __u8 data_sens2[] = { 0x63, 0x03 };
452 static const __u8 data_fin[] = { 0x08 };
453
454 spin_lock_irqsave(&wdata->state.lock, flags);
455
456 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
457 spin_unlock_irqrestore(&wdata->state.lock, flags);
458 return 0;
459 }
460
461 if (mode == 0) {
462 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
463 wiiproto_req_ir1(wdata, 0);
464 wiiproto_req_ir2(wdata, 0);
465 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
466 spin_unlock_irqrestore(&wdata->state.lock, flags);
467 return 0;
468 }
469
470 spin_unlock_irqrestore(&wdata->state.lock, flags);
471
472 ret = wiimote_cmd_acquire(wdata);
473 if (ret)
474 return ret;
475
476 /* send PIXEL CLOCK ENABLE cmd first */
477 spin_lock_irqsave(&wdata->state.lock, flags);
478 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
479 wiiproto_req_ir1(wdata, 0x06);
480 spin_unlock_irqrestore(&wdata->state.lock, flags);
481
482 ret = wiimote_cmd_wait(wdata);
483 if (ret)
484 goto unlock;
485 if (wdata->state.cmd_err) {
486 ret = -EIO;
487 goto unlock;
488 }
489
490 /* enable IR LOGIC */
491 spin_lock_irqsave(&wdata->state.lock, flags);
492 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
493 wiiproto_req_ir2(wdata, 0x06);
494 spin_unlock_irqrestore(&wdata->state.lock, flags);
495
496 ret = wiimote_cmd_wait(wdata);
497 if (ret)
498 goto unlock;
499 if (wdata->state.cmd_err) {
500 ret = -EIO;
501 goto unlock;
502 }
503
504 /* enable IR cam but do not make it send data, yet */
505 ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
506 sizeof(data_enable));
507 if (ret)
508 goto unlock;
509
510 /* write first sensitivity block */
511 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
512 sizeof(data_sens1));
513 if (ret)
514 goto unlock;
515
516 /* write second sensitivity block */
517 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
518 sizeof(data_sens2));
519 if (ret)
520 goto unlock;
521
522 /* put IR cam into desired state */
523 switch (mode) {
524 case WIIPROTO_FLAG_IR_FULL:
525 format = 5;
526 break;
527 case WIIPROTO_FLAG_IR_EXT:
528 format = 3;
529 break;
530 case WIIPROTO_FLAG_IR_BASIC:
531 format = 1;
532 break;
533 }
534 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
535 if (ret)
536 goto unlock;
537
538 /* make IR cam send data */
539 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
540 if (ret)
541 goto unlock;
542
543 /* request new DRM mode compatible to IR mode */
544 spin_lock_irqsave(&wdata->state.lock, flags);
545 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
546 wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
547 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
548 spin_unlock_irqrestore(&wdata->state.lock, flags);
549
550unlock:
551 wiimote_cmd_release(wdata);
552 return ret;
553}
554
David Herrmann23a5a4a2011-08-17 11:43:22 +0200555static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
556{
557 struct wiimote_data *wdata;
558 struct device *dev = led_dev->dev->parent;
559 int i;
560 unsigned long flags;
561 bool value = false;
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200562
David Herrmann23a5a4a2011-08-17 11:43:22 +0200563 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
564
565 for (i = 0; i < 4; ++i) {
566 if (wdata->leds[i] == led_dev) {
567 spin_lock_irqsave(&wdata->state.lock, flags);
568 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
569 spin_unlock_irqrestore(&wdata->state.lock, flags);
570 break;
571 }
572 }
573
574 return value ? LED_FULL : LED_OFF;
575}
576
577static void wiimote_leds_set(struct led_classdev *led_dev,
578 enum led_brightness value)
579{
580 struct wiimote_data *wdata;
581 struct device *dev = led_dev->dev->parent;
582 int i;
583 unsigned long flags;
584 __u8 state, flag;
585
586 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
587
588 for (i = 0; i < 4; ++i) {
589 if (wdata->leds[i] == led_dev) {
590 flag = WIIPROTO_FLAG_LED(i + 1);
591 spin_lock_irqsave(&wdata->state.lock, flags);
592 state = wdata->state.flags;
593 if (value == LED_OFF)
594 wiiproto_req_leds(wdata, state & ~flag);
595 else
596 wiiproto_req_leds(wdata, state | flag);
597 spin_unlock_irqrestore(&wdata->state.lock, flags);
598 break;
599 }
600 }
601}
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200602
David Herrmannd020be92011-09-06 13:50:27 +0200603static int wiimote_ff_play(struct input_dev *dev, void *data,
604 struct ff_effect *eff)
David Herrmann672bc4e2011-07-05 13:45:11 +0200605{
David Herrmannd020be92011-09-06 13:50:27 +0200606 struct wiimote_data *wdata = input_get_drvdata(dev);
607 __u8 value;
608 unsigned long flags;
609
610 /*
611 * The wiimote supports only a single rumble motor so if any magnitude
612 * is set to non-zero then we start the rumble motor. If both are set to
613 * zero, we stop the rumble motor.
614 */
615
616 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
617 value = 1;
618 else
619 value = 0;
620
621 spin_lock_irqsave(&wdata->state.lock, flags);
622 wiiproto_req_rumble(wdata, value);
623 spin_unlock_irqrestore(&wdata->state.lock, flags);
624
David Herrmann672bc4e2011-07-05 13:45:11 +0200625 return 0;
626}
627
David Herrmann26af1742011-08-17 11:43:21 +0200628static int wiimote_input_open(struct input_dev *dev)
629{
630 struct wiimote_data *wdata = input_get_drvdata(dev);
631
632 return hid_hw_open(wdata->hdev);
633}
634
635static void wiimote_input_close(struct input_dev *dev)
636{
637 struct wiimote_data *wdata = input_get_drvdata(dev);
638
639 hid_hw_close(wdata->hdev);
640}
641
David Herrmann98a558a2011-09-06 13:50:28 +0200642static int wiimote_accel_open(struct input_dev *dev)
643{
644 struct wiimote_data *wdata = input_get_drvdata(dev);
645 int ret;
646 unsigned long flags;
647
648 ret = hid_hw_open(wdata->hdev);
649 if (ret)
650 return ret;
651
652 spin_lock_irqsave(&wdata->state.lock, flags);
653 wiiproto_req_accel(wdata, true);
654 spin_unlock_irqrestore(&wdata->state.lock, flags);
655
656 return 0;
657}
658
659static void wiimote_accel_close(struct input_dev *dev)
660{
661 struct wiimote_data *wdata = input_get_drvdata(dev);
662 unsigned long flags;
663
664 spin_lock_irqsave(&wdata->state.lock, flags);
665 wiiproto_req_accel(wdata, false);
666 spin_unlock_irqrestore(&wdata->state.lock, flags);
667
668 hid_hw_close(wdata->hdev);
669}
670
David Herrmann1abb9ad2011-07-05 13:45:16 +0200671static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
672{
673 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
674 !!(payload[0] & 0x01));
675 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
676 !!(payload[0] & 0x02));
677 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
678 !!(payload[0] & 0x04));
679 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
680 !!(payload[0] & 0x08));
681 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
682 !!(payload[0] & 0x10));
683 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
684 !!(payload[1] & 0x01));
685 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
686 !!(payload[1] & 0x02));
687 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
688 !!(payload[1] & 0x04));
689 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
690 !!(payload[1] & 0x08));
691 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
692 !!(payload[1] & 0x10));
693 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
694 !!(payload[1] & 0x80));
695 input_sync(wdata->input);
696}
697
David Herrmannefcf9182011-09-06 13:50:29 +0200698static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
699{
700 __u16 x, y, z;
701
702 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
703 return;
704
705 /*
706 * payload is: BB BB XX YY ZZ
707 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
708 * contain the upper 8 bits of each value. The lower 2 bits are
709 * contained in the buttons data BB BB.
710 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
711 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
712 * accel value and bit 6 is the second bit of the Z value.
713 * The first bit of Y and Z values is not available and always set to 0.
714 * 0x200 is returned on no movement.
715 */
716
717 x = payload[2] << 2;
718 y = payload[3] << 2;
719 z = payload[4] << 2;
720
721 x |= (payload[0] >> 5) & 0x3;
722 y |= (payload[1] >> 4) & 0x2;
723 z |= (payload[1] >> 5) & 0x2;
724
725 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
726 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
727 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
728 input_sync(wdata->accel);
729}
730
David Herrmanneac39e72011-09-06 13:50:31 +0200731#define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
732 ABS_HAT0X, ABS_HAT0Y)
733#define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
734 ABS_HAT1X, ABS_HAT1Y)
735#define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
736 ABS_HAT2X, ABS_HAT2Y)
737#define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
738 ABS_HAT3X, ABS_HAT3Y)
739
740static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
741 bool packed, __u8 xid, __u8 yid)
742{
743 __u16 x, y;
744
745 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
746 return;
747
748 /*
749 * Basic IR data is encoded into 3 bytes. The first two bytes are the
750 * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits
751 * of both.
752 * If data is packed, then the 3rd byte is put first and slightly
753 * reordered. This allows to interleave packed and non-packed data to
754 * have two IR sets in 5 bytes instead of 6.
755 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
756 */
757
758 if (packed) {
759 x = ir[1] << 2;
760 y = ir[2] << 2;
761
762 x |= ir[0] & 0x3;
763 y |= (ir[0] >> 2) & 0x3;
764 } else {
765 x = ir[0] << 2;
766 y = ir[1] << 2;
767
768 x |= (ir[2] >> 4) & 0x3;
769 y |= (ir[2] >> 6) & 0x3;
770 }
771
772 input_report_abs(wdata->ir, xid, x);
773 input_report_abs(wdata->ir, yid, y);
774}
David Herrmannefcf9182011-09-06 13:50:29 +0200775
David Herrmannc87019e2011-08-17 11:43:24 +0200776static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
777{
778 handler_keys(wdata, payload);
779
780 /* on status reports the drm is reset so we need to resend the drm */
781 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
782}
783
David Herrmannbe1ecd62011-09-06 13:50:33 +0200784static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
785{
786 handler_keys(wdata, payload);
787}
788
David Herrmannc87019e2011-08-17 11:43:24 +0200789static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
790{
791 __u8 err = payload[3];
792 __u8 cmd = payload[2];
793
794 handler_keys(wdata, payload);
795
David Herrmann33e84012011-09-06 13:50:35 +0200796 if (wiimote_cmd_pending(wdata, cmd, 0)) {
797 wdata->state.cmd_err = err;
798 wiimote_cmd_complete(wdata);
799 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +0200800 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
801 cmd);
David Herrmann33e84012011-09-06 13:50:35 +0200802 }
David Herrmannc87019e2011-08-17 11:43:24 +0200803}
804
David Herrmannefcf9182011-09-06 13:50:29 +0200805static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
806{
807 handler_keys(wdata, payload);
808 handler_accel(wdata, payload);
809}
810
David Herrmann7336b9f2011-09-06 13:50:32 +0200811static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
812{
813 handler_keys(wdata, payload);
814}
815
David Herrmannefcf9182011-09-06 13:50:29 +0200816static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
817{
818 handler_keys(wdata, payload);
819 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200820 ir_to_input0(wdata, &payload[5], false);
821 ir_to_input1(wdata, &payload[8], false);
822 ir_to_input2(wdata, &payload[11], false);
823 ir_to_input3(wdata, &payload[14], false);
824 input_sync(wdata->ir);
825}
826
David Herrmann7336b9f2011-09-06 13:50:32 +0200827static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
828{
829 handler_keys(wdata, payload);
830}
831
David Herrmanneac39e72011-09-06 13:50:31 +0200832static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
833{
834 handler_keys(wdata, payload);
835 ir_to_input0(wdata, &payload[2], false);
836 ir_to_input1(wdata, &payload[4], true);
837 ir_to_input2(wdata, &payload[7], false);
838 ir_to_input3(wdata, &payload[9], true);
839 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200840}
841
842static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
843{
844 handler_keys(wdata, payload);
845 handler_accel(wdata, payload);
846}
847
848static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
849{
850 handler_keys(wdata, payload);
851 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200852 ir_to_input0(wdata, &payload[5], false);
853 ir_to_input1(wdata, &payload[7], true);
854 ir_to_input2(wdata, &payload[10], false);
855 ir_to_input3(wdata, &payload[12], true);
856 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200857}
858
David Herrmann7336b9f2011-09-06 13:50:32 +0200859static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
860{
861}
862
David Herrmannefcf9182011-09-06 13:50:29 +0200863static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
864{
865 handler_keys(wdata, payload);
866
867 wdata->state.accel_split[0] = payload[2];
868 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
869 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +0200870
871 ir_to_input0(wdata, &payload[3], false);
872 ir_to_input1(wdata, &payload[12], false);
873 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200874}
875
876static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
877{
878 __u8 buf[5];
879
880 handler_keys(wdata, payload);
881
882 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
883 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
884
885 buf[0] = 0;
886 buf[1] = 0;
887 buf[2] = wdata->state.accel_split[0];
888 buf[3] = payload[2];
889 buf[4] = wdata->state.accel_split[1];
890 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +0200891
892 ir_to_input2(wdata, &payload[3], false);
893 ir_to_input3(wdata, &payload[12], false);
894 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200895}
896
David Herrmanna4d19192011-07-05 13:45:15 +0200897struct wiiproto_handler {
898 __u8 id;
899 size_t size;
900 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
901};
902
903static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200904 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmannbe1ecd62011-09-06 13:50:33 +0200905 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmannc87019e2011-08-17 11:43:24 +0200906 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200907 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +0200908 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann7336b9f2011-09-06 13:50:32 +0200909 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmannefcf9182011-09-06 13:50:29 +0200910 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann7336b9f2011-09-06 13:50:32 +0200911 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmannefcf9182011-09-06 13:50:29 +0200912 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmanneac39e72011-09-06 13:50:31 +0200913 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmannefcf9182011-09-06 13:50:29 +0200914 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann7336b9f2011-09-06 13:50:32 +0200915 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +0200916 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
917 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +0200918 { .id = 0 }
919};
920
David Herrmann02fb72a2011-07-05 13:45:09 +0200921static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
922 u8 *raw_data, int size)
923{
David Herrmann4d36e972011-07-05 13:45:12 +0200924 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200925 struct wiiproto_handler *h;
926 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +0200927 unsigned long flags;
David Herrmann7336b9f2011-09-06 13:50:32 +0200928 bool handled = false;
David Herrmann4d36e972011-07-05 13:45:12 +0200929
David Herrmann02fb72a2011-07-05 13:45:09 +0200930 if (size < 1)
931 return -EINVAL;
932
David Herrmann32a0d9a2011-07-05 13:45:18 +0200933 spin_lock_irqsave(&wdata->state.lock, flags);
934
David Herrmanna4d19192011-07-05 13:45:15 +0200935 for (i = 0; handlers[i].id; ++i) {
936 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +0200937 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +0200938 h->func(wdata, &raw_data[1]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200939 handled = true;
940 }
David Herrmanna4d19192011-07-05 13:45:15 +0200941 }
942
David Herrmann7336b9f2011-09-06 13:50:32 +0200943 if (!handled)
944 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
945 size);
946
David Herrmann32a0d9a2011-07-05 13:45:18 +0200947 spin_unlock_irqrestore(&wdata->state.lock, flags);
948
David Herrmann02fb72a2011-07-05 13:45:09 +0200949 return 0;
950}
951
David Herrmann23a5a4a2011-08-17 11:43:22 +0200952static void wiimote_leds_destroy(struct wiimote_data *wdata)
953{
954 int i;
955 struct led_classdev *led;
956
957 for (i = 0; i < 4; ++i) {
958 if (wdata->leds[i]) {
959 led = wdata->leds[i];
960 wdata->leds[i] = NULL;
961 led_classdev_unregister(led);
962 kfree(led);
963 }
964 }
965}
966
967static int wiimote_leds_create(struct wiimote_data *wdata)
968{
969 int i, ret;
970 struct device *dev = &wdata->hdev->dev;
971 size_t namesz = strlen(dev_name(dev)) + 9;
972 struct led_classdev *led;
973 char *name;
974
975 for (i = 0; i < 4; ++i) {
976 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
977 if (!led) {
978 ret = -ENOMEM;
979 goto err;
980 }
981 name = (void*)&led[1];
982 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
983 led->name = name;
984 led->brightness = 0;
985 led->max_brightness = 1;
986 led->brightness_get = wiimote_leds_get;
987 led->brightness_set = wiimote_leds_set;
988
989 ret = led_classdev_register(dev, led);
990 if (ret) {
991 kfree(led);
992 goto err;
993 }
994 wdata->leds[i] = led;
995 }
996
997 return 0;
998
999err:
1000 wiimote_leds_destroy(wdata);
1001 return ret;
1002}
1003
David Herrmanne894d0e2011-07-05 13:45:10 +02001004static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1005{
1006 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +02001007 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +02001008
1009 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1010 if (!wdata)
1011 return NULL;
1012
David Herrmann672bc4e2011-07-05 13:45:11 +02001013 wdata->input = input_allocate_device();
David Herrmann98a558a2011-09-06 13:50:28 +02001014 if (!wdata->input)
1015 goto err;
David Herrmann672bc4e2011-07-05 13:45:11 +02001016
David Herrmanne894d0e2011-07-05 13:45:10 +02001017 wdata->hdev = hdev;
1018 hid_set_drvdata(hdev, wdata);
1019
David Herrmann672bc4e2011-07-05 13:45:11 +02001020 input_set_drvdata(wdata->input, wdata);
David Herrmann26af1742011-08-17 11:43:21 +02001021 wdata->input->open = wiimote_input_open;
1022 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +02001023 wdata->input->dev.parent = &wdata->hdev->dev;
1024 wdata->input->id.bustype = wdata->hdev->bus;
1025 wdata->input->id.vendor = wdata->hdev->vendor;
1026 wdata->input->id.product = wdata->hdev->product;
1027 wdata->input->id.version = wdata->hdev->version;
1028 wdata->input->name = WIIMOTE_NAME;
1029
David Herrmann1abb9ad2011-07-05 13:45:16 +02001030 set_bit(EV_KEY, wdata->input->evbit);
1031 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
1032 set_bit(wiiproto_keymap[i], wdata->input->keybit);
1033
David Herrmannd020be92011-09-06 13:50:27 +02001034 set_bit(FF_RUMBLE, wdata->input->ffbit);
David Herrmann98a558a2011-09-06 13:50:28 +02001035 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
1036 goto err_input;
1037
1038 wdata->accel = input_allocate_device();
1039 if (!wdata->accel)
1040 goto err_input;
1041
1042 input_set_drvdata(wdata->accel, wdata);
1043 wdata->accel->open = wiimote_accel_open;
1044 wdata->accel->close = wiimote_accel_close;
1045 wdata->accel->dev.parent = &wdata->hdev->dev;
1046 wdata->accel->id.bustype = wdata->hdev->bus;
1047 wdata->accel->id.vendor = wdata->hdev->vendor;
1048 wdata->accel->id.product = wdata->hdev->product;
1049 wdata->accel->id.version = wdata->hdev->version;
1050 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
1051
1052 set_bit(EV_ABS, wdata->accel->evbit);
1053 set_bit(ABS_RX, wdata->accel->absbit);
1054 set_bit(ABS_RY, wdata->accel->absbit);
1055 set_bit(ABS_RZ, wdata->accel->absbit);
1056 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
1057 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
1058 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
David Herrmannd020be92011-09-06 13:50:27 +02001059
David Herrmannf363e4f2011-09-06 13:50:30 +02001060 wdata->ir = input_allocate_device();
1061 if (!wdata->ir)
1062 goto err_ir;
1063
1064 input_set_drvdata(wdata->ir, wdata);
1065 wdata->ir->dev.parent = &wdata->hdev->dev;
1066 wdata->ir->id.bustype = wdata->hdev->bus;
1067 wdata->ir->id.vendor = wdata->hdev->vendor;
1068 wdata->ir->id.product = wdata->hdev->product;
1069 wdata->ir->id.version = wdata->hdev->version;
1070 wdata->ir->name = WIIMOTE_NAME " IR";
1071
1072 set_bit(EV_ABS, wdata->ir->evbit);
1073 set_bit(ABS_HAT0X, wdata->ir->absbit);
1074 set_bit(ABS_HAT0Y, wdata->ir->absbit);
1075 set_bit(ABS_HAT1X, wdata->ir->absbit);
1076 set_bit(ABS_HAT1Y, wdata->ir->absbit);
1077 set_bit(ABS_HAT2X, wdata->ir->absbit);
1078 set_bit(ABS_HAT2Y, wdata->ir->absbit);
1079 set_bit(ABS_HAT3X, wdata->ir->absbit);
1080 set_bit(ABS_HAT3Y, wdata->ir->absbit);
1081 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1082 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1083 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1084 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1085 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1086 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1087 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1088 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1089
David Herrmann23c063c2011-07-05 13:45:14 +02001090 spin_lock_init(&wdata->qlock);
1091 INIT_WORK(&wdata->worker, wiimote_worker);
1092
David Herrmann32a0d9a2011-07-05 13:45:18 +02001093 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001094 init_completion(&wdata->state.ready);
1095 mutex_init(&wdata->state.sync);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001096
David Herrmanne894d0e2011-07-05 13:45:10 +02001097 return wdata;
David Herrmann98a558a2011-09-06 13:50:28 +02001098
David Herrmannf363e4f2011-09-06 13:50:30 +02001099err_ir:
1100 input_free_device(wdata->accel);
David Herrmann98a558a2011-09-06 13:50:28 +02001101err_input:
1102 input_free_device(wdata->input);
1103err:
1104 kfree(wdata);
1105 return NULL;
David Herrmanne894d0e2011-07-05 13:45:10 +02001106}
1107
1108static void wiimote_destroy(struct wiimote_data *wdata)
1109{
David Herrmann23a5a4a2011-08-17 11:43:22 +02001110 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001111
David Herrmann98a558a2011-09-06 13:50:28 +02001112 input_unregister_device(wdata->accel);
David Herrmannf363e4f2011-09-06 13:50:30 +02001113 input_unregister_device(wdata->ir);
David Herrmann3989ef62011-08-17 11:43:20 +02001114 input_unregister_device(wdata->input);
1115 cancel_work_sync(&wdata->worker);
1116 hid_hw_stop(wdata->hdev);
1117
David Herrmanne894d0e2011-07-05 13:45:10 +02001118 kfree(wdata);
1119}
1120
David Herrmann02fb72a2011-07-05 13:45:09 +02001121static int wiimote_hid_probe(struct hid_device *hdev,
1122 const struct hid_device_id *id)
1123{
David Herrmanne894d0e2011-07-05 13:45:10 +02001124 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001125 int ret;
1126
David Herrmanne894d0e2011-07-05 13:45:10 +02001127 wdata = wiimote_create(hdev);
1128 if (!wdata) {
1129 hid_err(hdev, "Can't alloc device\n");
1130 return -ENOMEM;
1131 }
1132
David Herrmann02fb72a2011-07-05 13:45:09 +02001133 ret = hid_parse(hdev);
1134 if (ret) {
1135 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001136 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001137 }
1138
1139 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1140 if (ret) {
1141 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001142 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001143 }
1144
David Herrmann98a558a2011-09-06 13:50:28 +02001145 ret = input_register_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001146 if (ret) {
1147 hid_err(hdev, "Cannot register input device\n");
1148 goto err_stop;
1149 }
1150
David Herrmannf363e4f2011-09-06 13:50:30 +02001151 ret = input_register_device(wdata->ir);
1152 if (ret) {
1153 hid_err(hdev, "Cannot register input device\n");
1154 goto err_ir;
1155 }
1156
David Herrmann98a558a2011-09-06 13:50:28 +02001157 ret = input_register_device(wdata->input);
1158 if (ret) {
1159 hid_err(hdev, "Cannot register input device\n");
1160 goto err_input;
1161 }
1162
David Herrmann23a5a4a2011-08-17 11:43:22 +02001163 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001164 if (ret)
1165 goto err_free;
1166
David Herrmann02fb72a2011-07-05 13:45:09 +02001167 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001168
1169 /* by default set led1 after device initialization */
1170 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +02001171 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001172 spin_unlock_irq(&wdata->state.lock);
1173
David Herrmann02fb72a2011-07-05 13:45:09 +02001174 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001175
David Herrmann3989ef62011-08-17 11:43:20 +02001176err_free:
1177 wiimote_destroy(wdata);
1178 return ret;
1179
David Herrmann98a558a2011-09-06 13:50:28 +02001180err_input:
David Herrmannf363e4f2011-09-06 13:50:30 +02001181 input_unregister_device(wdata->ir);
1182 wdata->ir = NULL;
1183err_ir:
David Herrmann98a558a2011-09-06 13:50:28 +02001184 input_unregister_device(wdata->accel);
1185 wdata->accel = NULL;
David Herrmann672bc4e2011-07-05 13:45:11 +02001186err_stop:
1187 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001188err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001189 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001190 input_free_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001191 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +02001192 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001193 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001194}
1195
1196static void wiimote_hid_remove(struct hid_device *hdev)
1197{
David Herrmanne894d0e2011-07-05 13:45:10 +02001198 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1199
David Herrmann02fb72a2011-07-05 13:45:09 +02001200 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001201 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001202}
1203
1204static const struct hid_device_id wiimote_hid_devices[] = {
1205 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1206 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1207 { }
1208};
1209MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1210
1211static struct hid_driver wiimote_hid_driver = {
1212 .name = "wiimote",
1213 .id_table = wiimote_hid_devices,
1214 .probe = wiimote_hid_probe,
1215 .remove = wiimote_hid_remove,
1216 .raw_event = wiimote_hid_event,
1217};
1218
David Herrmannfb51b442011-07-05 13:45:08 +02001219static int __init wiimote_init(void)
1220{
David Herrmann02fb72a2011-07-05 13:45:09 +02001221 int ret;
1222
1223 ret = hid_register_driver(&wiimote_hid_driver);
1224 if (ret)
1225 pr_err("Can't register wiimote hid driver\n");
1226
1227 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +02001228}
1229
1230static void __exit wiimote_exit(void)
1231{
David Herrmann02fb72a2011-07-05 13:45:09 +02001232 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +02001233}
1234
1235module_init(wiimote_init);
1236module_exit(wiimote_exit);
1237MODULE_LICENSE("GPL");
1238MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1239MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
1240MODULE_VERSION(WIIMOTE_VERSION);