blob: 59a08933c73c08e712165c104283b3aab12f6db1 [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 Herrmannbe1ecd62011-09-06 13:50:33 +020085 WIIPROTO_REQ_WMEM = 0x16,
86 WIIPROTO_REQ_RMEM = 0x17,
David Herrmannc87019e2011-08-17 11:43:24 +020087 WIIPROTO_REQ_STATUS = 0x20,
David Herrmannbe1ecd62011-09-06 13:50:33 +020088 WIIPROTO_REQ_DATA = 0x21,
David Herrmannc87019e2011-08-17 11:43:24 +020089 WIIPROTO_REQ_RETURN = 0x22,
David Herrmann1abb9ad2011-07-05 13:45:16 +020090 WIIPROTO_REQ_DRM_K = 0x30,
David Herrmannefcf9182011-09-06 13:50:29 +020091 WIIPROTO_REQ_DRM_KA = 0x31,
David Herrmann7336b9f2011-09-06 13:50:32 +020092 WIIPROTO_REQ_DRM_KE = 0x32,
David Herrmannefcf9182011-09-06 13:50:29 +020093 WIIPROTO_REQ_DRM_KAI = 0x33,
David Herrmann7336b9f2011-09-06 13:50:32 +020094 WIIPROTO_REQ_DRM_KEE = 0x34,
David Herrmannefcf9182011-09-06 13:50:29 +020095 WIIPROTO_REQ_DRM_KAE = 0x35,
David Herrmannf363e4f2011-09-06 13:50:30 +020096 WIIPROTO_REQ_DRM_KIE = 0x36,
David Herrmannefcf9182011-09-06 13:50:29 +020097 WIIPROTO_REQ_DRM_KAIE = 0x37,
David Herrmann7336b9f2011-09-06 13:50:32 +020098 WIIPROTO_REQ_DRM_E = 0x3d,
David Herrmannefcf9182011-09-06 13:50:29 +020099 WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
100 WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
David Herrmann1abb9ad2011-07-05 13:45:16 +0200101};
102
103enum wiiproto_keys {
104 WIIPROTO_KEY_LEFT,
105 WIIPROTO_KEY_RIGHT,
106 WIIPROTO_KEY_UP,
107 WIIPROTO_KEY_DOWN,
108 WIIPROTO_KEY_PLUS,
109 WIIPROTO_KEY_MINUS,
110 WIIPROTO_KEY_ONE,
111 WIIPROTO_KEY_TWO,
112 WIIPROTO_KEY_A,
113 WIIPROTO_KEY_B,
114 WIIPROTO_KEY_HOME,
115 WIIPROTO_KEY_COUNT
116};
117
118static __u16 wiiproto_keymap[] = {
119 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
120 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
121 KEY_UP, /* WIIPROTO_KEY_UP */
122 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
123 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
124 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
125 BTN_1, /* WIIPROTO_KEY_ONE */
126 BTN_2, /* WIIPROTO_KEY_TWO */
127 BTN_A, /* WIIPROTO_KEY_A */
128 BTN_B, /* WIIPROTO_KEY_B */
129 BTN_MODE, /* WIIPROTO_KEY_HOME */
130};
131
David Herrmann29d28062011-09-06 13:50:34 +0200132/* requires the state.lock spinlock to be held */
133static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
134 __u32 opt)
135{
136 return wdata->state.cmd == cmd && wdata->state.opt == opt;
137}
138
139/* requires the state.lock spinlock to be held */
140static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
141{
142 wdata->state.cmd = WIIPROTO_REQ_NULL;
143 complete(&wdata->state.ready);
144}
145
146static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
147{
148 return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
149}
150
151/* requires the state.lock spinlock to be held */
152static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
153 __u32 opt)
154{
155 INIT_COMPLETION(wdata->state.ready);
156 wdata->state.cmd = cmd;
157 wdata->state.opt = opt;
158}
159
160static inline void wiimote_cmd_release(struct wiimote_data *wdata)
161{
162 mutex_unlock(&wdata->state.sync);
163}
164
165static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
166{
167 int ret;
168
169 ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
170 if (ret < 0)
171 return -ERESTARTSYS;
172 else if (ret == 0)
173 return -EIO;
174 else
175 return 0;
176}
177
David Herrmann0c218f142011-07-05 13:45:13 +0200178static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
179 size_t count)
180{
181 __u8 *buf;
182 ssize_t ret;
183
184 if (!hdev->hid_output_raw_report)
185 return -ENODEV;
186
187 buf = kmemdup(buffer, count, GFP_KERNEL);
188 if (!buf)
189 return -ENOMEM;
190
191 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
192
193 kfree(buf);
194 return ret;
195}
196
David Herrmann23c063c2011-07-05 13:45:14 +0200197static void wiimote_worker(struct work_struct *work)
198{
199 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
200 worker);
201 unsigned long flags;
202
203 spin_lock_irqsave(&wdata->qlock, flags);
204
205 while (wdata->head != wdata->tail) {
206 spin_unlock_irqrestore(&wdata->qlock, flags);
207 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
208 wdata->outq[wdata->tail].size);
209 spin_lock_irqsave(&wdata->qlock, flags);
210
211 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
212 }
213
214 spin_unlock_irqrestore(&wdata->qlock, flags);
215}
216
217static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
218 size_t count)
219{
220 unsigned long flags;
221 __u8 newhead;
222
223 if (count > HID_MAX_BUFFER_SIZE) {
224 hid_warn(wdata->hdev, "Sending too large output report\n");
225 return;
226 }
227
228 /*
229 * Copy new request into our output queue and check whether the
230 * queue is full. If it is full, discard this request.
231 * If it is empty we need to start a new worker that will
232 * send out the buffer to the hid device.
233 * If the queue is not empty, then there must be a worker
234 * that is currently sending out our buffer and this worker
235 * will reschedule itself until the queue is empty.
236 */
237
238 spin_lock_irqsave(&wdata->qlock, flags);
239
240 memcpy(wdata->outq[wdata->head].data, buffer, count);
241 wdata->outq[wdata->head].size = count;
242 newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
243
244 if (wdata->head == wdata->tail) {
245 wdata->head = newhead;
246 schedule_work(&wdata->worker);
247 } else if (newhead != wdata->tail) {
248 wdata->head = newhead;
249 } else {
250 hid_warn(wdata->hdev, "Output queue is full");
251 }
252
253 spin_unlock_irqrestore(&wdata->qlock, flags);
254}
255
David Herrmannc003ec22011-09-06 13:50:26 +0200256/*
257 * This sets the rumble bit on the given output report if rumble is
258 * currently enabled.
259 * \cmd1 must point to the second byte in the output report => &cmd[1]
260 * This must be called on nearly every output report before passing it
261 * into the output queue!
262 */
263static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
264{
265 if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
266 *cmd1 |= 0x01;
267}
268
269static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
270{
271 __u8 cmd[2];
272
273 rumble = !!rumble;
274 if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
275 return;
276
277 if (rumble)
278 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
279 else
280 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
281
282 cmd[0] = WIIPROTO_REQ_RUMBLE;
283 cmd[1] = 0;
284
285 wiiproto_keep_rumble(wdata, &cmd[1]);
286 wiimote_queue(wdata, cmd, sizeof(cmd));
287}
288
David Herrmanndb308342011-07-05 13:45:17 +0200289static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
290{
291 __u8 cmd[2];
292
David Herrmann32a0d9a2011-07-05 13:45:18 +0200293 leds &= WIIPROTO_FLAGS_LEDS;
294 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
295 return;
296 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
297
David Herrmanndb308342011-07-05 13:45:17 +0200298 cmd[0] = WIIPROTO_REQ_LED;
299 cmd[1] = 0;
300
301 if (leds & WIIPROTO_FLAG_LED1)
302 cmd[1] |= 0x10;
303 if (leds & WIIPROTO_FLAG_LED2)
304 cmd[1] |= 0x20;
305 if (leds & WIIPROTO_FLAG_LED3)
306 cmd[1] |= 0x40;
307 if (leds & WIIPROTO_FLAG_LED4)
308 cmd[1] |= 0x80;
309
David Herrmannc003ec22011-09-06 13:50:26 +0200310 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmanndb308342011-07-05 13:45:17 +0200311 wiimote_queue(wdata, cmd, sizeof(cmd));
312}
313
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200314/*
315 * Check what peripherals of the wiimote are currently
316 * active and select a proper DRM that supports all of
317 * the requested data inputs.
318 */
319static __u8 select_drm(struct wiimote_data *wdata)
320{
David Herrmannf363e4f2011-09-06 13:50:30 +0200321 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
322
323 if (ir == WIIPROTO_FLAG_IR_BASIC) {
324 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
325 return WIIPROTO_REQ_DRM_KAIE;
326 else
327 return WIIPROTO_REQ_DRM_KIE;
328 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
329 return WIIPROTO_REQ_DRM_KAI;
330 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
331 return WIIPROTO_REQ_DRM_SKAI1;
332 } else {
333 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
334 return WIIPROTO_REQ_DRM_KA;
335 else
336 return WIIPROTO_REQ_DRM_K;
337 }
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200338}
339
340static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
341{
342 __u8 cmd[3];
343
344 if (drm == WIIPROTO_REQ_NULL)
345 drm = select_drm(wdata);
346
347 cmd[0] = WIIPROTO_REQ_DRM;
348 cmd[1] = 0;
349 cmd[2] = drm;
350
David Herrmannc003ec22011-09-06 13:50:26 +0200351 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200352 wiimote_queue(wdata, cmd, sizeof(cmd));
353}
354
David Herrmann98a558a2011-09-06 13:50:28 +0200355static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
356{
357 accel = !!accel;
358 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
359 return;
360
361 if (accel)
362 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
363 else
364 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
365
366 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
367}
368
David Herrmannbe1ecd62011-09-06 13:50:33 +0200369#define wiiproto_req_wreg(wdata, os, buf, sz) \
370 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
371
372#define wiiproto_req_weeprom(wdata, os, buf, sz) \
373 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
374
375static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
376 __u32 offset, const __u8 *buf, __u8 size)
377{
378 __u8 cmd[22];
379
380 if (size > 16 || size == 0) {
381 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
382 return;
383 }
384
385 memset(cmd, 0, sizeof(cmd));
386 cmd[0] = WIIPROTO_REQ_WMEM;
387 cmd[2] = (offset >> 16) & 0xff;
388 cmd[3] = (offset >> 8) & 0xff;
389 cmd[4] = offset & 0xff;
390 cmd[5] = size;
391 memcpy(&cmd[6], buf, size);
392
393 if (!eeprom)
394 cmd[1] |= 0x04;
395
396 wiiproto_keep_rumble(wdata, &cmd[1]);
397 wiimote_queue(wdata, cmd, sizeof(cmd));
398}
399
David Herrmann33e84012011-09-06 13:50:35 +0200400/* requries the cmd-mutex to be held */
401static int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
402 const __u8 *wmem, __u8 size)
403{
404 unsigned long flags;
405 int ret;
406
407 spin_lock_irqsave(&wdata->state.lock, flags);
408 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
409 wiiproto_req_wreg(wdata, offset, wmem, size);
410 spin_unlock_irqrestore(&wdata->state.lock, flags);
411
412 ret = wiimote_cmd_wait(wdata);
413 if (!ret && wdata->state.cmd_err)
414 ret = -EIO;
415
416 return ret;
417}
418
David Herrmann23a5a4a2011-08-17 11:43:22 +0200419static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
420{
421 struct wiimote_data *wdata;
422 struct device *dev = led_dev->dev->parent;
423 int i;
424 unsigned long flags;
425 bool value = false;
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200426
David Herrmann23a5a4a2011-08-17 11:43:22 +0200427 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
428
429 for (i = 0; i < 4; ++i) {
430 if (wdata->leds[i] == led_dev) {
431 spin_lock_irqsave(&wdata->state.lock, flags);
432 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
433 spin_unlock_irqrestore(&wdata->state.lock, flags);
434 break;
435 }
436 }
437
438 return value ? LED_FULL : LED_OFF;
439}
440
441static void wiimote_leds_set(struct led_classdev *led_dev,
442 enum led_brightness value)
443{
444 struct wiimote_data *wdata;
445 struct device *dev = led_dev->dev->parent;
446 int i;
447 unsigned long flags;
448 __u8 state, flag;
449
450 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
451
452 for (i = 0; i < 4; ++i) {
453 if (wdata->leds[i] == led_dev) {
454 flag = WIIPROTO_FLAG_LED(i + 1);
455 spin_lock_irqsave(&wdata->state.lock, flags);
456 state = wdata->state.flags;
457 if (value == LED_OFF)
458 wiiproto_req_leds(wdata, state & ~flag);
459 else
460 wiiproto_req_leds(wdata, state | flag);
461 spin_unlock_irqrestore(&wdata->state.lock, flags);
462 break;
463 }
464 }
465}
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200466
David Herrmannd020be92011-09-06 13:50:27 +0200467static int wiimote_ff_play(struct input_dev *dev, void *data,
468 struct ff_effect *eff)
David Herrmann672bc4e2011-07-05 13:45:11 +0200469{
David Herrmannd020be92011-09-06 13:50:27 +0200470 struct wiimote_data *wdata = input_get_drvdata(dev);
471 __u8 value;
472 unsigned long flags;
473
474 /*
475 * The wiimote supports only a single rumble motor so if any magnitude
476 * is set to non-zero then we start the rumble motor. If both are set to
477 * zero, we stop the rumble motor.
478 */
479
480 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
481 value = 1;
482 else
483 value = 0;
484
485 spin_lock_irqsave(&wdata->state.lock, flags);
486 wiiproto_req_rumble(wdata, value);
487 spin_unlock_irqrestore(&wdata->state.lock, flags);
488
David Herrmann672bc4e2011-07-05 13:45:11 +0200489 return 0;
490}
491
David Herrmann26af1742011-08-17 11:43:21 +0200492static int wiimote_input_open(struct input_dev *dev)
493{
494 struct wiimote_data *wdata = input_get_drvdata(dev);
495
496 return hid_hw_open(wdata->hdev);
497}
498
499static void wiimote_input_close(struct input_dev *dev)
500{
501 struct wiimote_data *wdata = input_get_drvdata(dev);
502
503 hid_hw_close(wdata->hdev);
504}
505
David Herrmann98a558a2011-09-06 13:50:28 +0200506static int wiimote_accel_open(struct input_dev *dev)
507{
508 struct wiimote_data *wdata = input_get_drvdata(dev);
509 int ret;
510 unsigned long flags;
511
512 ret = hid_hw_open(wdata->hdev);
513 if (ret)
514 return ret;
515
516 spin_lock_irqsave(&wdata->state.lock, flags);
517 wiiproto_req_accel(wdata, true);
518 spin_unlock_irqrestore(&wdata->state.lock, flags);
519
520 return 0;
521}
522
523static void wiimote_accel_close(struct input_dev *dev)
524{
525 struct wiimote_data *wdata = input_get_drvdata(dev);
526 unsigned long flags;
527
528 spin_lock_irqsave(&wdata->state.lock, flags);
529 wiiproto_req_accel(wdata, false);
530 spin_unlock_irqrestore(&wdata->state.lock, flags);
531
532 hid_hw_close(wdata->hdev);
533}
534
David Herrmann1abb9ad2011-07-05 13:45:16 +0200535static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
536{
537 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
538 !!(payload[0] & 0x01));
539 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
540 !!(payload[0] & 0x02));
541 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
542 !!(payload[0] & 0x04));
543 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
544 !!(payload[0] & 0x08));
545 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
546 !!(payload[0] & 0x10));
547 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
548 !!(payload[1] & 0x01));
549 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
550 !!(payload[1] & 0x02));
551 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
552 !!(payload[1] & 0x04));
553 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
554 !!(payload[1] & 0x08));
555 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
556 !!(payload[1] & 0x10));
557 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
558 !!(payload[1] & 0x80));
559 input_sync(wdata->input);
560}
561
David Herrmannefcf9182011-09-06 13:50:29 +0200562static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
563{
564 __u16 x, y, z;
565
566 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
567 return;
568
569 /*
570 * payload is: BB BB XX YY ZZ
571 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
572 * contain the upper 8 bits of each value. The lower 2 bits are
573 * contained in the buttons data BB BB.
574 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
575 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
576 * accel value and bit 6 is the second bit of the Z value.
577 * The first bit of Y and Z values is not available and always set to 0.
578 * 0x200 is returned on no movement.
579 */
580
581 x = payload[2] << 2;
582 y = payload[3] << 2;
583 z = payload[4] << 2;
584
585 x |= (payload[0] >> 5) & 0x3;
586 y |= (payload[1] >> 4) & 0x2;
587 z |= (payload[1] >> 5) & 0x2;
588
589 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
590 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
591 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
592 input_sync(wdata->accel);
593}
594
David Herrmanneac39e72011-09-06 13:50:31 +0200595#define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
596 ABS_HAT0X, ABS_HAT0Y)
597#define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
598 ABS_HAT1X, ABS_HAT1Y)
599#define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
600 ABS_HAT2X, ABS_HAT2Y)
601#define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
602 ABS_HAT3X, ABS_HAT3Y)
603
604static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
605 bool packed, __u8 xid, __u8 yid)
606{
607 __u16 x, y;
608
609 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
610 return;
611
612 /*
613 * Basic IR data is encoded into 3 bytes. The first two bytes are the
614 * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits
615 * of both.
616 * If data is packed, then the 3rd byte is put first and slightly
617 * reordered. This allows to interleave packed and non-packed data to
618 * have two IR sets in 5 bytes instead of 6.
619 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
620 */
621
622 if (packed) {
623 x = ir[1] << 2;
624 y = ir[2] << 2;
625
626 x |= ir[0] & 0x3;
627 y |= (ir[0] >> 2) & 0x3;
628 } else {
629 x = ir[0] << 2;
630 y = ir[1] << 2;
631
632 x |= (ir[2] >> 4) & 0x3;
633 y |= (ir[2] >> 6) & 0x3;
634 }
635
636 input_report_abs(wdata->ir, xid, x);
637 input_report_abs(wdata->ir, yid, y);
638}
David Herrmannefcf9182011-09-06 13:50:29 +0200639
David Herrmannc87019e2011-08-17 11:43:24 +0200640static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
641{
642 handler_keys(wdata, payload);
643
644 /* on status reports the drm is reset so we need to resend the drm */
645 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
646}
647
David Herrmannbe1ecd62011-09-06 13:50:33 +0200648static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
649{
650 handler_keys(wdata, payload);
651}
652
David Herrmannc87019e2011-08-17 11:43:24 +0200653static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
654{
655 __u8 err = payload[3];
656 __u8 cmd = payload[2];
657
658 handler_keys(wdata, payload);
659
David Herrmann33e84012011-09-06 13:50:35 +0200660 if (wiimote_cmd_pending(wdata, cmd, 0)) {
661 wdata->state.cmd_err = err;
662 wiimote_cmd_complete(wdata);
663 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +0200664 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
665 cmd);
David Herrmann33e84012011-09-06 13:50:35 +0200666 }
David Herrmannc87019e2011-08-17 11:43:24 +0200667}
668
David Herrmannefcf9182011-09-06 13:50:29 +0200669static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
670{
671 handler_keys(wdata, payload);
672 handler_accel(wdata, payload);
673}
674
David Herrmann7336b9f2011-09-06 13:50:32 +0200675static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
676{
677 handler_keys(wdata, payload);
678}
679
David Herrmannefcf9182011-09-06 13:50:29 +0200680static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
681{
682 handler_keys(wdata, payload);
683 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200684 ir_to_input0(wdata, &payload[5], false);
685 ir_to_input1(wdata, &payload[8], false);
686 ir_to_input2(wdata, &payload[11], false);
687 ir_to_input3(wdata, &payload[14], false);
688 input_sync(wdata->ir);
689}
690
David Herrmann7336b9f2011-09-06 13:50:32 +0200691static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
692{
693 handler_keys(wdata, payload);
694}
695
David Herrmanneac39e72011-09-06 13:50:31 +0200696static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
697{
698 handler_keys(wdata, payload);
699 ir_to_input0(wdata, &payload[2], false);
700 ir_to_input1(wdata, &payload[4], true);
701 ir_to_input2(wdata, &payload[7], false);
702 ir_to_input3(wdata, &payload[9], true);
703 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200704}
705
706static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
707{
708 handler_keys(wdata, payload);
709 handler_accel(wdata, payload);
710}
711
712static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
713{
714 handler_keys(wdata, payload);
715 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200716 ir_to_input0(wdata, &payload[5], false);
717 ir_to_input1(wdata, &payload[7], true);
718 ir_to_input2(wdata, &payload[10], false);
719 ir_to_input3(wdata, &payload[12], true);
720 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200721}
722
David Herrmann7336b9f2011-09-06 13:50:32 +0200723static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
724{
725}
726
David Herrmannefcf9182011-09-06 13:50:29 +0200727static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
728{
729 handler_keys(wdata, payload);
730
731 wdata->state.accel_split[0] = payload[2];
732 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
733 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +0200734
735 ir_to_input0(wdata, &payload[3], false);
736 ir_to_input1(wdata, &payload[12], false);
737 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200738}
739
740static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
741{
742 __u8 buf[5];
743
744 handler_keys(wdata, payload);
745
746 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
747 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
748
749 buf[0] = 0;
750 buf[1] = 0;
751 buf[2] = wdata->state.accel_split[0];
752 buf[3] = payload[2];
753 buf[4] = wdata->state.accel_split[1];
754 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +0200755
756 ir_to_input2(wdata, &payload[3], false);
757 ir_to_input3(wdata, &payload[12], false);
758 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200759}
760
David Herrmanna4d19192011-07-05 13:45:15 +0200761struct wiiproto_handler {
762 __u8 id;
763 size_t size;
764 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
765};
766
767static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200768 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmannbe1ecd62011-09-06 13:50:33 +0200769 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmannc87019e2011-08-17 11:43:24 +0200770 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200771 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +0200772 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann7336b9f2011-09-06 13:50:32 +0200773 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmannefcf9182011-09-06 13:50:29 +0200774 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann7336b9f2011-09-06 13:50:32 +0200775 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmannefcf9182011-09-06 13:50:29 +0200776 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmanneac39e72011-09-06 13:50:31 +0200777 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmannefcf9182011-09-06 13:50:29 +0200778 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann7336b9f2011-09-06 13:50:32 +0200779 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +0200780 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
781 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +0200782 { .id = 0 }
783};
784
David Herrmann02fb72a2011-07-05 13:45:09 +0200785static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
786 u8 *raw_data, int size)
787{
David Herrmann4d36e972011-07-05 13:45:12 +0200788 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200789 struct wiiproto_handler *h;
790 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +0200791 unsigned long flags;
David Herrmann7336b9f2011-09-06 13:50:32 +0200792 bool handled = false;
David Herrmann4d36e972011-07-05 13:45:12 +0200793
David Herrmann02fb72a2011-07-05 13:45:09 +0200794 if (size < 1)
795 return -EINVAL;
796
David Herrmann32a0d9a2011-07-05 13:45:18 +0200797 spin_lock_irqsave(&wdata->state.lock, flags);
798
David Herrmanna4d19192011-07-05 13:45:15 +0200799 for (i = 0; handlers[i].id; ++i) {
800 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +0200801 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +0200802 h->func(wdata, &raw_data[1]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200803 handled = true;
804 }
David Herrmanna4d19192011-07-05 13:45:15 +0200805 }
806
David Herrmann7336b9f2011-09-06 13:50:32 +0200807 if (!handled)
808 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
809 size);
810
David Herrmann32a0d9a2011-07-05 13:45:18 +0200811 spin_unlock_irqrestore(&wdata->state.lock, flags);
812
David Herrmann02fb72a2011-07-05 13:45:09 +0200813 return 0;
814}
815
David Herrmann23a5a4a2011-08-17 11:43:22 +0200816static void wiimote_leds_destroy(struct wiimote_data *wdata)
817{
818 int i;
819 struct led_classdev *led;
820
821 for (i = 0; i < 4; ++i) {
822 if (wdata->leds[i]) {
823 led = wdata->leds[i];
824 wdata->leds[i] = NULL;
825 led_classdev_unregister(led);
826 kfree(led);
827 }
828 }
829}
830
831static int wiimote_leds_create(struct wiimote_data *wdata)
832{
833 int i, ret;
834 struct device *dev = &wdata->hdev->dev;
835 size_t namesz = strlen(dev_name(dev)) + 9;
836 struct led_classdev *led;
837 char *name;
838
839 for (i = 0; i < 4; ++i) {
840 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
841 if (!led) {
842 ret = -ENOMEM;
843 goto err;
844 }
845 name = (void*)&led[1];
846 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
847 led->name = name;
848 led->brightness = 0;
849 led->max_brightness = 1;
850 led->brightness_get = wiimote_leds_get;
851 led->brightness_set = wiimote_leds_set;
852
853 ret = led_classdev_register(dev, led);
854 if (ret) {
855 kfree(led);
856 goto err;
857 }
858 wdata->leds[i] = led;
859 }
860
861 return 0;
862
863err:
864 wiimote_leds_destroy(wdata);
865 return ret;
866}
867
David Herrmanne894d0e2011-07-05 13:45:10 +0200868static struct wiimote_data *wiimote_create(struct hid_device *hdev)
869{
870 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +0200871 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +0200872
873 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
874 if (!wdata)
875 return NULL;
876
David Herrmann672bc4e2011-07-05 13:45:11 +0200877 wdata->input = input_allocate_device();
David Herrmann98a558a2011-09-06 13:50:28 +0200878 if (!wdata->input)
879 goto err;
David Herrmann672bc4e2011-07-05 13:45:11 +0200880
David Herrmanne894d0e2011-07-05 13:45:10 +0200881 wdata->hdev = hdev;
882 hid_set_drvdata(hdev, wdata);
883
David Herrmann672bc4e2011-07-05 13:45:11 +0200884 input_set_drvdata(wdata->input, wdata);
David Herrmann26af1742011-08-17 11:43:21 +0200885 wdata->input->open = wiimote_input_open;
886 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +0200887 wdata->input->dev.parent = &wdata->hdev->dev;
888 wdata->input->id.bustype = wdata->hdev->bus;
889 wdata->input->id.vendor = wdata->hdev->vendor;
890 wdata->input->id.product = wdata->hdev->product;
891 wdata->input->id.version = wdata->hdev->version;
892 wdata->input->name = WIIMOTE_NAME;
893
David Herrmann1abb9ad2011-07-05 13:45:16 +0200894 set_bit(EV_KEY, wdata->input->evbit);
895 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
896 set_bit(wiiproto_keymap[i], wdata->input->keybit);
897
David Herrmannd020be92011-09-06 13:50:27 +0200898 set_bit(FF_RUMBLE, wdata->input->ffbit);
David Herrmann98a558a2011-09-06 13:50:28 +0200899 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
900 goto err_input;
901
902 wdata->accel = input_allocate_device();
903 if (!wdata->accel)
904 goto err_input;
905
906 input_set_drvdata(wdata->accel, wdata);
907 wdata->accel->open = wiimote_accel_open;
908 wdata->accel->close = wiimote_accel_close;
909 wdata->accel->dev.parent = &wdata->hdev->dev;
910 wdata->accel->id.bustype = wdata->hdev->bus;
911 wdata->accel->id.vendor = wdata->hdev->vendor;
912 wdata->accel->id.product = wdata->hdev->product;
913 wdata->accel->id.version = wdata->hdev->version;
914 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
915
916 set_bit(EV_ABS, wdata->accel->evbit);
917 set_bit(ABS_RX, wdata->accel->absbit);
918 set_bit(ABS_RY, wdata->accel->absbit);
919 set_bit(ABS_RZ, wdata->accel->absbit);
920 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
921 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
922 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
David Herrmannd020be92011-09-06 13:50:27 +0200923
David Herrmannf363e4f2011-09-06 13:50:30 +0200924 wdata->ir = input_allocate_device();
925 if (!wdata->ir)
926 goto err_ir;
927
928 input_set_drvdata(wdata->ir, wdata);
929 wdata->ir->dev.parent = &wdata->hdev->dev;
930 wdata->ir->id.bustype = wdata->hdev->bus;
931 wdata->ir->id.vendor = wdata->hdev->vendor;
932 wdata->ir->id.product = wdata->hdev->product;
933 wdata->ir->id.version = wdata->hdev->version;
934 wdata->ir->name = WIIMOTE_NAME " IR";
935
936 set_bit(EV_ABS, wdata->ir->evbit);
937 set_bit(ABS_HAT0X, wdata->ir->absbit);
938 set_bit(ABS_HAT0Y, wdata->ir->absbit);
939 set_bit(ABS_HAT1X, wdata->ir->absbit);
940 set_bit(ABS_HAT1Y, wdata->ir->absbit);
941 set_bit(ABS_HAT2X, wdata->ir->absbit);
942 set_bit(ABS_HAT2Y, wdata->ir->absbit);
943 set_bit(ABS_HAT3X, wdata->ir->absbit);
944 set_bit(ABS_HAT3Y, wdata->ir->absbit);
945 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
946 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
947 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
948 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
949 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
950 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
951 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
952 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
953
David Herrmann23c063c2011-07-05 13:45:14 +0200954 spin_lock_init(&wdata->qlock);
955 INIT_WORK(&wdata->worker, wiimote_worker);
956
David Herrmann32a0d9a2011-07-05 13:45:18 +0200957 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +0200958 init_completion(&wdata->state.ready);
959 mutex_init(&wdata->state.sync);
David Herrmann32a0d9a2011-07-05 13:45:18 +0200960
David Herrmanne894d0e2011-07-05 13:45:10 +0200961 return wdata;
David Herrmann98a558a2011-09-06 13:50:28 +0200962
David Herrmannf363e4f2011-09-06 13:50:30 +0200963err_ir:
964 input_free_device(wdata->accel);
David Herrmann98a558a2011-09-06 13:50:28 +0200965err_input:
966 input_free_device(wdata->input);
967err:
968 kfree(wdata);
969 return NULL;
David Herrmanne894d0e2011-07-05 13:45:10 +0200970}
971
972static void wiimote_destroy(struct wiimote_data *wdata)
973{
David Herrmann23a5a4a2011-08-17 11:43:22 +0200974 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +0200975
David Herrmann98a558a2011-09-06 13:50:28 +0200976 input_unregister_device(wdata->accel);
David Herrmannf363e4f2011-09-06 13:50:30 +0200977 input_unregister_device(wdata->ir);
David Herrmann3989ef62011-08-17 11:43:20 +0200978 input_unregister_device(wdata->input);
979 cancel_work_sync(&wdata->worker);
980 hid_hw_stop(wdata->hdev);
981
David Herrmanne894d0e2011-07-05 13:45:10 +0200982 kfree(wdata);
983}
984
David Herrmann02fb72a2011-07-05 13:45:09 +0200985static int wiimote_hid_probe(struct hid_device *hdev,
986 const struct hid_device_id *id)
987{
David Herrmanne894d0e2011-07-05 13:45:10 +0200988 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +0200989 int ret;
990
David Herrmanne894d0e2011-07-05 13:45:10 +0200991 wdata = wiimote_create(hdev);
992 if (!wdata) {
993 hid_err(hdev, "Can't alloc device\n");
994 return -ENOMEM;
995 }
996
David Herrmann02fb72a2011-07-05 13:45:09 +0200997 ret = hid_parse(hdev);
998 if (ret) {
999 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001000 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001001 }
1002
1003 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1004 if (ret) {
1005 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001006 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001007 }
1008
David Herrmann98a558a2011-09-06 13:50:28 +02001009 ret = input_register_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001010 if (ret) {
1011 hid_err(hdev, "Cannot register input device\n");
1012 goto err_stop;
1013 }
1014
David Herrmannf363e4f2011-09-06 13:50:30 +02001015 ret = input_register_device(wdata->ir);
1016 if (ret) {
1017 hid_err(hdev, "Cannot register input device\n");
1018 goto err_ir;
1019 }
1020
David Herrmann98a558a2011-09-06 13:50:28 +02001021 ret = input_register_device(wdata->input);
1022 if (ret) {
1023 hid_err(hdev, "Cannot register input device\n");
1024 goto err_input;
1025 }
1026
David Herrmann23a5a4a2011-08-17 11:43:22 +02001027 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001028 if (ret)
1029 goto err_free;
1030
David Herrmann02fb72a2011-07-05 13:45:09 +02001031 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001032
1033 /* by default set led1 after device initialization */
1034 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +02001035 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001036 spin_unlock_irq(&wdata->state.lock);
1037
David Herrmann02fb72a2011-07-05 13:45:09 +02001038 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001039
David Herrmann3989ef62011-08-17 11:43:20 +02001040err_free:
1041 wiimote_destroy(wdata);
1042 return ret;
1043
David Herrmann98a558a2011-09-06 13:50:28 +02001044err_input:
David Herrmannf363e4f2011-09-06 13:50:30 +02001045 input_unregister_device(wdata->ir);
1046 wdata->ir = NULL;
1047err_ir:
David Herrmann98a558a2011-09-06 13:50:28 +02001048 input_unregister_device(wdata->accel);
1049 wdata->accel = NULL;
David Herrmann672bc4e2011-07-05 13:45:11 +02001050err_stop:
1051 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001052err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001053 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001054 input_free_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001055 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +02001056 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001057 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001058}
1059
1060static void wiimote_hid_remove(struct hid_device *hdev)
1061{
David Herrmanne894d0e2011-07-05 13:45:10 +02001062 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1063
David Herrmann02fb72a2011-07-05 13:45:09 +02001064 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001065 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001066}
1067
1068static const struct hid_device_id wiimote_hid_devices[] = {
1069 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1070 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1071 { }
1072};
1073MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1074
1075static struct hid_driver wiimote_hid_driver = {
1076 .name = "wiimote",
1077 .id_table = wiimote_hid_devices,
1078 .probe = wiimote_hid_probe,
1079 .remove = wiimote_hid_remove,
1080 .raw_event = wiimote_hid_event,
1081};
1082
David Herrmannfb51b442011-07-05 13:45:08 +02001083static int __init wiimote_init(void)
1084{
David Herrmann02fb72a2011-07-05 13:45:09 +02001085 int ret;
1086
1087 ret = hid_register_driver(&wiimote_hid_driver);
1088 if (ret)
1089 pr_err("Can't register wiimote hid driver\n");
1090
1091 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +02001092}
1093
1094static void __exit wiimote_exit(void)
1095{
David Herrmann02fb72a2011-07-05 13:45:09 +02001096 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +02001097}
1098
1099module_init(wiimote_init);
1100module_exit(wiimote_exit);
1101MODULE_LICENSE("GPL");
1102MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1103MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
1104MODULE_VERSION(WIIMOTE_VERSION);