blob: 76739c07fa3c8143c9b23c4964c9a009e5e3cfa8 [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 Herrmannfb51b442011-07-05 13:45:08 +020023
David Herrmannb22e00f2011-09-06 13:50:40 +020024#define WIIMOTE_VERSION "0.2"
David Herrmannfb51b442011-07-05 13:45:08 +020025#define WIIMOTE_NAME "Nintendo Wii Remote"
David Herrmann23c063c2011-07-05 13:45:14 +020026#define WIIMOTE_BUFSIZE 32
27
28struct wiimote_buf {
29 __u8 data[HID_MAX_BUFFER_SIZE];
30 size_t size;
31};
David Herrmannfb51b442011-07-05 13:45:08 +020032
David Herrmann32a0d9a2011-07-05 13:45:18 +020033struct wiimote_state {
34 spinlock_t lock;
35 __u8 flags;
David Herrmannefcf9182011-09-06 13:50:29 +020036 __u8 accel_split[2];
David Herrmann29d28062011-09-06 13:50:34 +020037
38 /* synchronous cmd requests */
39 struct mutex sync;
40 struct completion ready;
41 int cmd;
42 __u32 opt;
David Herrmann33e84012011-09-06 13:50:35 +020043
44 /* results of synchronous requests */
David Herrmanne3979a92011-09-06 13:50:38 +020045 __u8 cmd_battery;
David Herrmann33e84012011-09-06 13:50:35 +020046 __u8 cmd_err;
David Herrmann32a0d9a2011-07-05 13:45:18 +020047};
48
David Herrmanne894d0e2011-07-05 13:45:10 +020049struct wiimote_data {
50 struct hid_device *hdev;
David Herrmann672bc4e2011-07-05 13:45:11 +020051 struct input_dev *input;
David Herrmann23a5a4a2011-08-17 11:43:22 +020052 struct led_classdev *leds[4];
David Herrmann98a558a2011-09-06 13:50:28 +020053 struct input_dev *accel;
David Herrmannf363e4f2011-09-06 13:50:30 +020054 struct input_dev *ir;
David Herrmann6591d752011-09-06 13:50:39 +020055 struct power_supply battery;
David Herrmann23c063c2011-07-05 13:45:14 +020056
57 spinlock_t qlock;
58 __u8 head;
59 __u8 tail;
60 struct wiimote_buf outq[WIIMOTE_BUFSIZE];
61 struct work_struct worker;
David Herrmann32a0d9a2011-07-05 13:45:18 +020062
63 struct wiimote_state state;
David Herrmanne894d0e2011-07-05 13:45:10 +020064};
65
David Herrmannc003ec22011-09-06 13:50:26 +020066#define WIIPROTO_FLAG_LED1 0x01
67#define WIIPROTO_FLAG_LED2 0x02
68#define WIIPROTO_FLAG_LED3 0x04
69#define WIIPROTO_FLAG_LED4 0x08
70#define WIIPROTO_FLAG_RUMBLE 0x10
David Herrmann98a558a2011-09-06 13:50:28 +020071#define WIIPROTO_FLAG_ACCEL 0x20
David Herrmannf363e4f2011-09-06 13:50:30 +020072#define WIIPROTO_FLAG_IR_BASIC 0x40
73#define WIIPROTO_FLAG_IR_EXT 0x80
74#define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */
David Herrmann32a0d9a2011-07-05 13:45:18 +020075#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
76 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
David Herrmannf363e4f2011-09-06 13:50:30 +020077#define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \
78 WIIPROTO_FLAG_IR_FULL)
David Herrmanndb308342011-07-05 13:45:17 +020079
David Herrmann23a5a4a2011-08-17 11:43:22 +020080/* return flag for led \num */
81#define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
82
David Herrmann1abb9ad2011-07-05 13:45:16 +020083enum wiiproto_reqs {
David Herrmann2cb5e4b2011-08-17 11:43:23 +020084 WIIPROTO_REQ_NULL = 0x0,
David Herrmannc003ec22011-09-06 13:50:26 +020085 WIIPROTO_REQ_RUMBLE = 0x10,
David Herrmanndb308342011-07-05 13:45:17 +020086 WIIPROTO_REQ_LED = 0x11,
David Herrmann2cb5e4b2011-08-17 11:43:23 +020087 WIIPROTO_REQ_DRM = 0x12,
David Herrmannfc221cd2011-09-06 13:50:36 +020088 WIIPROTO_REQ_IR1 = 0x13,
David Herrmanne3979a92011-09-06 13:50:38 +020089 WIIPROTO_REQ_SREQ = 0x15,
David Herrmannbe1ecd62011-09-06 13:50:33 +020090 WIIPROTO_REQ_WMEM = 0x16,
91 WIIPROTO_REQ_RMEM = 0x17,
David Herrmannfc221cd2011-09-06 13:50:36 +020092 WIIPROTO_REQ_IR2 = 0x1a,
David Herrmannc87019e2011-08-17 11:43:24 +020093 WIIPROTO_REQ_STATUS = 0x20,
David Herrmannbe1ecd62011-09-06 13:50:33 +020094 WIIPROTO_REQ_DATA = 0x21,
David Herrmannc87019e2011-08-17 11:43:24 +020095 WIIPROTO_REQ_RETURN = 0x22,
David Herrmann1abb9ad2011-07-05 13:45:16 +020096 WIIPROTO_REQ_DRM_K = 0x30,
David Herrmannefcf9182011-09-06 13:50:29 +020097 WIIPROTO_REQ_DRM_KA = 0x31,
David Herrmann7336b9f2011-09-06 13:50:32 +020098 WIIPROTO_REQ_DRM_KE = 0x32,
David Herrmannefcf9182011-09-06 13:50:29 +020099 WIIPROTO_REQ_DRM_KAI = 0x33,
David Herrmann7336b9f2011-09-06 13:50:32 +0200100 WIIPROTO_REQ_DRM_KEE = 0x34,
David Herrmannefcf9182011-09-06 13:50:29 +0200101 WIIPROTO_REQ_DRM_KAE = 0x35,
David Herrmannf363e4f2011-09-06 13:50:30 +0200102 WIIPROTO_REQ_DRM_KIE = 0x36,
David Herrmannefcf9182011-09-06 13:50:29 +0200103 WIIPROTO_REQ_DRM_KAIE = 0x37,
David Herrmann7336b9f2011-09-06 13:50:32 +0200104 WIIPROTO_REQ_DRM_E = 0x3d,
David Herrmannefcf9182011-09-06 13:50:29 +0200105 WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
106 WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
David Herrmann1abb9ad2011-07-05 13:45:16 +0200107};
108
109enum wiiproto_keys {
110 WIIPROTO_KEY_LEFT,
111 WIIPROTO_KEY_RIGHT,
112 WIIPROTO_KEY_UP,
113 WIIPROTO_KEY_DOWN,
114 WIIPROTO_KEY_PLUS,
115 WIIPROTO_KEY_MINUS,
116 WIIPROTO_KEY_ONE,
117 WIIPROTO_KEY_TWO,
118 WIIPROTO_KEY_A,
119 WIIPROTO_KEY_B,
120 WIIPROTO_KEY_HOME,
121 WIIPROTO_KEY_COUNT
122};
123
124static __u16 wiiproto_keymap[] = {
125 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
126 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
127 KEY_UP, /* WIIPROTO_KEY_UP */
128 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
129 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
130 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
131 BTN_1, /* WIIPROTO_KEY_ONE */
132 BTN_2, /* WIIPROTO_KEY_TWO */
133 BTN_A, /* WIIPROTO_KEY_A */
134 BTN_B, /* WIIPROTO_KEY_B */
135 BTN_MODE, /* WIIPROTO_KEY_HOME */
136};
137
David Herrmann6591d752011-09-06 13:50:39 +0200138static enum power_supply_property wiimote_battery_props[] = {
139 POWER_SUPPLY_PROP_CAPACITY
140};
141
David Herrmann29d28062011-09-06 13:50:34 +0200142/* requires the state.lock spinlock to be held */
143static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
144 __u32 opt)
145{
146 return wdata->state.cmd == cmd && wdata->state.opt == opt;
147}
148
149/* requires the state.lock spinlock to be held */
150static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
151{
152 wdata->state.cmd = WIIPROTO_REQ_NULL;
153 complete(&wdata->state.ready);
154}
155
156static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
157{
158 return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
159}
160
161/* requires the state.lock spinlock to be held */
162static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
163 __u32 opt)
164{
165 INIT_COMPLETION(wdata->state.ready);
166 wdata->state.cmd = cmd;
167 wdata->state.opt = opt;
168}
169
170static inline void wiimote_cmd_release(struct wiimote_data *wdata)
171{
172 mutex_unlock(&wdata->state.sync);
173}
174
175static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
176{
177 int ret;
178
179 ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
180 if (ret < 0)
181 return -ERESTARTSYS;
182 else if (ret == 0)
183 return -EIO;
184 else
185 return 0;
186}
187
David Herrmann0c218f12011-07-05 13:45:13 +0200188static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
189 size_t count)
190{
191 __u8 *buf;
192 ssize_t ret;
193
194 if (!hdev->hid_output_raw_report)
195 return -ENODEV;
196
197 buf = kmemdup(buffer, count, GFP_KERNEL);
198 if (!buf)
199 return -ENOMEM;
200
201 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
202
203 kfree(buf);
204 return ret;
205}
206
David Herrmann23c063c2011-07-05 13:45:14 +0200207static void wiimote_worker(struct work_struct *work)
208{
209 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
210 worker);
211 unsigned long flags;
212
213 spin_lock_irqsave(&wdata->qlock, flags);
214
215 while (wdata->head != wdata->tail) {
216 spin_unlock_irqrestore(&wdata->qlock, flags);
217 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
218 wdata->outq[wdata->tail].size);
219 spin_lock_irqsave(&wdata->qlock, flags);
220
221 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
222 }
223
224 spin_unlock_irqrestore(&wdata->qlock, flags);
225}
226
227static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
228 size_t count)
229{
230 unsigned long flags;
231 __u8 newhead;
232
233 if (count > HID_MAX_BUFFER_SIZE) {
234 hid_warn(wdata->hdev, "Sending too large output report\n");
235 return;
236 }
237
238 /*
239 * Copy new request into our output queue and check whether the
240 * queue is full. If it is full, discard this request.
241 * If it is empty we need to start a new worker that will
242 * send out the buffer to the hid device.
243 * If the queue is not empty, then there must be a worker
244 * that is currently sending out our buffer and this worker
245 * will reschedule itself until the queue is empty.
246 */
247
248 spin_lock_irqsave(&wdata->qlock, flags);
249
250 memcpy(wdata->outq[wdata->head].data, buffer, count);
251 wdata->outq[wdata->head].size = count;
252 newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
253
254 if (wdata->head == wdata->tail) {
255 wdata->head = newhead;
256 schedule_work(&wdata->worker);
257 } else if (newhead != wdata->tail) {
258 wdata->head = newhead;
259 } else {
260 hid_warn(wdata->hdev, "Output queue is full");
261 }
262
263 spin_unlock_irqrestore(&wdata->qlock, flags);
264}
265
David Herrmannc003ec22011-09-06 13:50:26 +0200266/*
267 * This sets the rumble bit on the given output report if rumble is
268 * currently enabled.
269 * \cmd1 must point to the second byte in the output report => &cmd[1]
270 * This must be called on nearly every output report before passing it
271 * into the output queue!
272 */
273static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
274{
275 if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
276 *cmd1 |= 0x01;
277}
278
279static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
280{
281 __u8 cmd[2];
282
283 rumble = !!rumble;
284 if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
285 return;
286
287 if (rumble)
288 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
289 else
290 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
291
292 cmd[0] = WIIPROTO_REQ_RUMBLE;
293 cmd[1] = 0;
294
295 wiiproto_keep_rumble(wdata, &cmd[1]);
296 wiimote_queue(wdata, cmd, sizeof(cmd));
297}
298
David Herrmanndb308342011-07-05 13:45:17 +0200299static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
300{
301 __u8 cmd[2];
302
David Herrmann32a0d9a2011-07-05 13:45:18 +0200303 leds &= WIIPROTO_FLAGS_LEDS;
304 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
305 return;
306 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
307
David Herrmanndb308342011-07-05 13:45:17 +0200308 cmd[0] = WIIPROTO_REQ_LED;
309 cmd[1] = 0;
310
311 if (leds & WIIPROTO_FLAG_LED1)
312 cmd[1] |= 0x10;
313 if (leds & WIIPROTO_FLAG_LED2)
314 cmd[1] |= 0x20;
315 if (leds & WIIPROTO_FLAG_LED3)
316 cmd[1] |= 0x40;
317 if (leds & WIIPROTO_FLAG_LED4)
318 cmd[1] |= 0x80;
319
David Herrmannc003ec22011-09-06 13:50:26 +0200320 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmanndb308342011-07-05 13:45:17 +0200321 wiimote_queue(wdata, cmd, sizeof(cmd));
322}
323
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200324/*
325 * Check what peripherals of the wiimote are currently
326 * active and select a proper DRM that supports all of
327 * the requested data inputs.
328 */
329static __u8 select_drm(struct wiimote_data *wdata)
330{
David Herrmannf363e4f2011-09-06 13:50:30 +0200331 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
332
333 if (ir == WIIPROTO_FLAG_IR_BASIC) {
334 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
335 return WIIPROTO_REQ_DRM_KAIE;
336 else
337 return WIIPROTO_REQ_DRM_KIE;
338 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
339 return WIIPROTO_REQ_DRM_KAI;
340 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
341 return WIIPROTO_REQ_DRM_SKAI1;
342 } else {
343 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
344 return WIIPROTO_REQ_DRM_KA;
345 else
346 return WIIPROTO_REQ_DRM_K;
347 }
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200348}
349
350static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
351{
352 __u8 cmd[3];
353
354 if (drm == WIIPROTO_REQ_NULL)
355 drm = select_drm(wdata);
356
357 cmd[0] = WIIPROTO_REQ_DRM;
358 cmd[1] = 0;
359 cmd[2] = drm;
360
David Herrmannc003ec22011-09-06 13:50:26 +0200361 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200362 wiimote_queue(wdata, cmd, sizeof(cmd));
363}
364
David Herrmanne3979a92011-09-06 13:50:38 +0200365static void wiiproto_req_status(struct wiimote_data *wdata)
366{
367 __u8 cmd[2];
368
369 cmd[0] = WIIPROTO_REQ_SREQ;
370 cmd[1] = 0;
371
372 wiiproto_keep_rumble(wdata, &cmd[1]);
373 wiimote_queue(wdata, cmd, sizeof(cmd));
374}
375
David Herrmann98a558a2011-09-06 13:50:28 +0200376static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
377{
378 accel = !!accel;
379 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
380 return;
381
382 if (accel)
383 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
384 else
385 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
386
387 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
388}
389
David Herrmannfc221cd2011-09-06 13:50:36 +0200390static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
391{
392 __u8 cmd[2];
393
394 cmd[0] = WIIPROTO_REQ_IR1;
395 cmd[1] = flags;
396
397 wiiproto_keep_rumble(wdata, &cmd[1]);
398 wiimote_queue(wdata, cmd, sizeof(cmd));
399}
400
401static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
402{
403 __u8 cmd[2];
404
405 cmd[0] = WIIPROTO_REQ_IR2;
406 cmd[1] = flags;
407
408 wiiproto_keep_rumble(wdata, &cmd[1]);
409 wiimote_queue(wdata, cmd, sizeof(cmd));
410}
411
David Herrmannbe1ecd62011-09-06 13:50:33 +0200412#define wiiproto_req_wreg(wdata, os, buf, sz) \
413 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
414
415#define wiiproto_req_weeprom(wdata, os, buf, sz) \
416 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
417
418static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
419 __u32 offset, const __u8 *buf, __u8 size)
420{
421 __u8 cmd[22];
422
423 if (size > 16 || size == 0) {
424 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
425 return;
426 }
427
428 memset(cmd, 0, sizeof(cmd));
429 cmd[0] = WIIPROTO_REQ_WMEM;
430 cmd[2] = (offset >> 16) & 0xff;
431 cmd[3] = (offset >> 8) & 0xff;
432 cmd[4] = offset & 0xff;
433 cmd[5] = size;
434 memcpy(&cmd[6], buf, size);
435
436 if (!eeprom)
437 cmd[1] |= 0x04;
438
439 wiiproto_keep_rumble(wdata, &cmd[1]);
440 wiimote_queue(wdata, cmd, sizeof(cmd));
441}
442
David Herrmann33e84012011-09-06 13:50:35 +0200443/* requries the cmd-mutex to be held */
444static int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
445 const __u8 *wmem, __u8 size)
446{
447 unsigned long flags;
448 int ret;
449
450 spin_lock_irqsave(&wdata->state.lock, flags);
451 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
452 wiiproto_req_wreg(wdata, offset, wmem, size);
453 spin_unlock_irqrestore(&wdata->state.lock, flags);
454
455 ret = wiimote_cmd_wait(wdata);
456 if (!ret && wdata->state.cmd_err)
457 ret = -EIO;
458
459 return ret;
460}
461
David Herrmann6591d752011-09-06 13:50:39 +0200462static int wiimote_battery_get_property(struct power_supply *psy,
463 enum power_supply_property psp,
464 union power_supply_propval *val)
465{
466 struct wiimote_data *wdata = container_of(psy,
467 struct wiimote_data, battery);
468 int ret = 0, state;
469 unsigned long flags;
470
471 ret = wiimote_cmd_acquire(wdata);
472 if (ret)
473 return ret;
474
475 spin_lock_irqsave(&wdata->state.lock, flags);
476 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
477 wiiproto_req_status(wdata);
478 spin_unlock_irqrestore(&wdata->state.lock, flags);
479
480 ret = wiimote_cmd_wait(wdata);
481 state = wdata->state.cmd_battery;
482 wiimote_cmd_release(wdata);
483
484 if (ret)
485 return ret;
486
487 switch (psp) {
488 case POWER_SUPPLY_PROP_CAPACITY:
489 val->intval = state * 100 / 255;
490 break;
491 default:
492 ret = -EINVAL;
493 break;
494 }
495
496 return ret;
497}
498
David Herrmannfc221cd2011-09-06 13:50:36 +0200499static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
500{
501 int ret;
502 unsigned long flags;
503 __u8 format = 0;
504 static const __u8 data_enable[] = { 0x01 };
505 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
506 0x00, 0xaa, 0x00, 0x64 };
507 static const __u8 data_sens2[] = { 0x63, 0x03 };
508 static const __u8 data_fin[] = { 0x08 };
509
510 spin_lock_irqsave(&wdata->state.lock, flags);
511
512 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
513 spin_unlock_irqrestore(&wdata->state.lock, flags);
514 return 0;
515 }
516
517 if (mode == 0) {
518 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
519 wiiproto_req_ir1(wdata, 0);
520 wiiproto_req_ir2(wdata, 0);
521 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
522 spin_unlock_irqrestore(&wdata->state.lock, flags);
523 return 0;
524 }
525
526 spin_unlock_irqrestore(&wdata->state.lock, flags);
527
528 ret = wiimote_cmd_acquire(wdata);
529 if (ret)
530 return ret;
531
532 /* send PIXEL CLOCK ENABLE cmd first */
533 spin_lock_irqsave(&wdata->state.lock, flags);
534 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
535 wiiproto_req_ir1(wdata, 0x06);
536 spin_unlock_irqrestore(&wdata->state.lock, flags);
537
538 ret = wiimote_cmd_wait(wdata);
539 if (ret)
540 goto unlock;
541 if (wdata->state.cmd_err) {
542 ret = -EIO;
543 goto unlock;
544 }
545
546 /* enable IR LOGIC */
547 spin_lock_irqsave(&wdata->state.lock, flags);
548 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
549 wiiproto_req_ir2(wdata, 0x06);
550 spin_unlock_irqrestore(&wdata->state.lock, flags);
551
552 ret = wiimote_cmd_wait(wdata);
553 if (ret)
554 goto unlock;
555 if (wdata->state.cmd_err) {
556 ret = -EIO;
557 goto unlock;
558 }
559
560 /* enable IR cam but do not make it send data, yet */
561 ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
562 sizeof(data_enable));
563 if (ret)
564 goto unlock;
565
566 /* write first sensitivity block */
567 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
568 sizeof(data_sens1));
569 if (ret)
570 goto unlock;
571
572 /* write second sensitivity block */
573 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
574 sizeof(data_sens2));
575 if (ret)
576 goto unlock;
577
578 /* put IR cam into desired state */
579 switch (mode) {
580 case WIIPROTO_FLAG_IR_FULL:
581 format = 5;
582 break;
583 case WIIPROTO_FLAG_IR_EXT:
584 format = 3;
585 break;
586 case WIIPROTO_FLAG_IR_BASIC:
587 format = 1;
588 break;
589 }
590 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
591 if (ret)
592 goto unlock;
593
594 /* make IR cam send data */
595 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
596 if (ret)
597 goto unlock;
598
599 /* request new DRM mode compatible to IR mode */
600 spin_lock_irqsave(&wdata->state.lock, flags);
601 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
602 wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
603 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
604 spin_unlock_irqrestore(&wdata->state.lock, flags);
605
606unlock:
607 wiimote_cmd_release(wdata);
608 return ret;
609}
610
David Herrmann23a5a4a2011-08-17 11:43:22 +0200611static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
612{
613 struct wiimote_data *wdata;
614 struct device *dev = led_dev->dev->parent;
615 int i;
616 unsigned long flags;
617 bool value = false;
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200618
David Herrmann23a5a4a2011-08-17 11:43:22 +0200619 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
620
621 for (i = 0; i < 4; ++i) {
622 if (wdata->leds[i] == led_dev) {
623 spin_lock_irqsave(&wdata->state.lock, flags);
624 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
625 spin_unlock_irqrestore(&wdata->state.lock, flags);
626 break;
627 }
628 }
629
630 return value ? LED_FULL : LED_OFF;
631}
632
633static void wiimote_leds_set(struct led_classdev *led_dev,
634 enum led_brightness value)
635{
636 struct wiimote_data *wdata;
637 struct device *dev = led_dev->dev->parent;
638 int i;
639 unsigned long flags;
640 __u8 state, flag;
641
642 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
643
644 for (i = 0; i < 4; ++i) {
645 if (wdata->leds[i] == led_dev) {
646 flag = WIIPROTO_FLAG_LED(i + 1);
647 spin_lock_irqsave(&wdata->state.lock, flags);
648 state = wdata->state.flags;
649 if (value == LED_OFF)
650 wiiproto_req_leds(wdata, state & ~flag);
651 else
652 wiiproto_req_leds(wdata, state | flag);
653 spin_unlock_irqrestore(&wdata->state.lock, flags);
654 break;
655 }
656 }
657}
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200658
David Herrmannd020be92011-09-06 13:50:27 +0200659static int wiimote_ff_play(struct input_dev *dev, void *data,
660 struct ff_effect *eff)
David Herrmann672bc4e2011-07-05 13:45:11 +0200661{
David Herrmannd020be92011-09-06 13:50:27 +0200662 struct wiimote_data *wdata = input_get_drvdata(dev);
663 __u8 value;
664 unsigned long flags;
665
666 /*
667 * The wiimote supports only a single rumble motor so if any magnitude
668 * is set to non-zero then we start the rumble motor. If both are set to
669 * zero, we stop the rumble motor.
670 */
671
672 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
673 value = 1;
674 else
675 value = 0;
676
677 spin_lock_irqsave(&wdata->state.lock, flags);
678 wiiproto_req_rumble(wdata, value);
679 spin_unlock_irqrestore(&wdata->state.lock, flags);
680
David Herrmann672bc4e2011-07-05 13:45:11 +0200681 return 0;
682}
683
David Herrmann26af1742011-08-17 11:43:21 +0200684static int wiimote_input_open(struct input_dev *dev)
685{
686 struct wiimote_data *wdata = input_get_drvdata(dev);
687
688 return hid_hw_open(wdata->hdev);
689}
690
691static void wiimote_input_close(struct input_dev *dev)
692{
693 struct wiimote_data *wdata = input_get_drvdata(dev);
694
695 hid_hw_close(wdata->hdev);
696}
697
David Herrmann98a558a2011-09-06 13:50:28 +0200698static int wiimote_accel_open(struct input_dev *dev)
699{
700 struct wiimote_data *wdata = input_get_drvdata(dev);
701 int ret;
702 unsigned long flags;
703
704 ret = hid_hw_open(wdata->hdev);
705 if (ret)
706 return ret;
707
708 spin_lock_irqsave(&wdata->state.lock, flags);
709 wiiproto_req_accel(wdata, true);
710 spin_unlock_irqrestore(&wdata->state.lock, flags);
711
712 return 0;
713}
714
715static void wiimote_accel_close(struct input_dev *dev)
716{
717 struct wiimote_data *wdata = input_get_drvdata(dev);
718 unsigned long flags;
719
720 spin_lock_irqsave(&wdata->state.lock, flags);
721 wiiproto_req_accel(wdata, false);
722 spin_unlock_irqrestore(&wdata->state.lock, flags);
723
724 hid_hw_close(wdata->hdev);
725}
726
David Herrmann0370d7c2011-09-06 13:50:37 +0200727static int wiimote_ir_open(struct input_dev *dev)
728{
729 struct wiimote_data *wdata = input_get_drvdata(dev);
730 int ret;
731
732 ret = hid_hw_open(wdata->hdev);
733 if (ret)
734 return ret;
735
736 ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
737 if (ret) {
738 hid_hw_close(wdata->hdev);
739 return ret;
740 }
741
742 return 0;
743}
744
745static void wiimote_ir_close(struct input_dev *dev)
746{
747 struct wiimote_data *wdata = input_get_drvdata(dev);
748
749 wiimote_init_ir(wdata, 0);
750 hid_hw_close(wdata->hdev);
751}
752
David Herrmann1abb9ad2011-07-05 13:45:16 +0200753static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
754{
755 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
756 !!(payload[0] & 0x01));
757 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
758 !!(payload[0] & 0x02));
759 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
760 !!(payload[0] & 0x04));
761 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
762 !!(payload[0] & 0x08));
763 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
764 !!(payload[0] & 0x10));
765 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
766 !!(payload[1] & 0x01));
767 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
768 !!(payload[1] & 0x02));
769 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
770 !!(payload[1] & 0x04));
771 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
772 !!(payload[1] & 0x08));
773 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
774 !!(payload[1] & 0x10));
775 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
776 !!(payload[1] & 0x80));
777 input_sync(wdata->input);
778}
779
David Herrmannefcf9182011-09-06 13:50:29 +0200780static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
781{
782 __u16 x, y, z;
783
784 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
785 return;
786
787 /*
788 * payload is: BB BB XX YY ZZ
789 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
790 * contain the upper 8 bits of each value. The lower 2 bits are
791 * contained in the buttons data BB BB.
792 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
793 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
794 * accel value and bit 6 is the second bit of the Z value.
795 * The first bit of Y and Z values is not available and always set to 0.
796 * 0x200 is returned on no movement.
797 */
798
799 x = payload[2] << 2;
800 y = payload[3] << 2;
801 z = payload[4] << 2;
802
803 x |= (payload[0] >> 5) & 0x3;
804 y |= (payload[1] >> 4) & 0x2;
805 z |= (payload[1] >> 5) & 0x2;
806
807 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
808 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
809 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
810 input_sync(wdata->accel);
811}
812
David Herrmanneac39e72011-09-06 13:50:31 +0200813#define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
814 ABS_HAT0X, ABS_HAT0Y)
815#define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
816 ABS_HAT1X, ABS_HAT1Y)
817#define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
818 ABS_HAT2X, ABS_HAT2Y)
819#define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
820 ABS_HAT3X, ABS_HAT3Y)
821
822static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
823 bool packed, __u8 xid, __u8 yid)
824{
825 __u16 x, y;
826
827 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
828 return;
829
830 /*
831 * Basic IR data is encoded into 3 bytes. The first two bytes are the
832 * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits
833 * of both.
834 * If data is packed, then the 3rd byte is put first and slightly
835 * reordered. This allows to interleave packed and non-packed data to
836 * have two IR sets in 5 bytes instead of 6.
837 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
838 */
839
840 if (packed) {
841 x = ir[1] << 2;
842 y = ir[2] << 2;
843
844 x |= ir[0] & 0x3;
845 y |= (ir[0] >> 2) & 0x3;
846 } else {
847 x = ir[0] << 2;
848 y = ir[1] << 2;
849
850 x |= (ir[2] >> 4) & 0x3;
851 y |= (ir[2] >> 6) & 0x3;
852 }
853
854 input_report_abs(wdata->ir, xid, x);
855 input_report_abs(wdata->ir, yid, y);
856}
David Herrmannefcf9182011-09-06 13:50:29 +0200857
David Herrmannc87019e2011-08-17 11:43:24 +0200858static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
859{
860 handler_keys(wdata, payload);
861
862 /* on status reports the drm is reset so we need to resend the drm */
863 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
David Herrmanne3979a92011-09-06 13:50:38 +0200864
865 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) {
866 wdata->state.cmd_battery = payload[5];
867 wiimote_cmd_complete(wdata);
868 }
David Herrmannc87019e2011-08-17 11:43:24 +0200869}
870
David Herrmannbe1ecd62011-09-06 13:50:33 +0200871static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
872{
873 handler_keys(wdata, payload);
874}
875
David Herrmannc87019e2011-08-17 11:43:24 +0200876static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
877{
878 __u8 err = payload[3];
879 __u8 cmd = payload[2];
880
881 handler_keys(wdata, payload);
882
David Herrmann33e84012011-09-06 13:50:35 +0200883 if (wiimote_cmd_pending(wdata, cmd, 0)) {
884 wdata->state.cmd_err = err;
885 wiimote_cmd_complete(wdata);
886 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +0200887 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
888 cmd);
David Herrmann33e84012011-09-06 13:50:35 +0200889 }
David Herrmannc87019e2011-08-17 11:43:24 +0200890}
891
David Herrmannefcf9182011-09-06 13:50:29 +0200892static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
893{
894 handler_keys(wdata, payload);
895 handler_accel(wdata, payload);
896}
897
David Herrmann7336b9f2011-09-06 13:50:32 +0200898static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
899{
900 handler_keys(wdata, payload);
901}
902
David Herrmannefcf9182011-09-06 13:50:29 +0200903static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
904{
905 handler_keys(wdata, payload);
906 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200907 ir_to_input0(wdata, &payload[5], false);
908 ir_to_input1(wdata, &payload[8], false);
909 ir_to_input2(wdata, &payload[11], false);
910 ir_to_input3(wdata, &payload[14], false);
911 input_sync(wdata->ir);
912}
913
David Herrmann7336b9f2011-09-06 13:50:32 +0200914static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
915{
916 handler_keys(wdata, payload);
917}
918
David Herrmanneac39e72011-09-06 13:50:31 +0200919static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
920{
921 handler_keys(wdata, payload);
922 ir_to_input0(wdata, &payload[2], false);
923 ir_to_input1(wdata, &payload[4], true);
924 ir_to_input2(wdata, &payload[7], false);
925 ir_to_input3(wdata, &payload[9], true);
926 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200927}
928
929static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
930{
931 handler_keys(wdata, payload);
932 handler_accel(wdata, payload);
933}
934
935static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
936{
937 handler_keys(wdata, payload);
938 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200939 ir_to_input0(wdata, &payload[5], false);
940 ir_to_input1(wdata, &payload[7], true);
941 ir_to_input2(wdata, &payload[10], false);
942 ir_to_input3(wdata, &payload[12], true);
943 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200944}
945
David Herrmann7336b9f2011-09-06 13:50:32 +0200946static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
947{
948}
949
David Herrmannefcf9182011-09-06 13:50:29 +0200950static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
951{
952 handler_keys(wdata, payload);
953
954 wdata->state.accel_split[0] = payload[2];
955 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
956 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +0200957
958 ir_to_input0(wdata, &payload[3], false);
959 ir_to_input1(wdata, &payload[12], false);
960 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200961}
962
963static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
964{
965 __u8 buf[5];
966
967 handler_keys(wdata, payload);
968
969 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
970 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
971
972 buf[0] = 0;
973 buf[1] = 0;
974 buf[2] = wdata->state.accel_split[0];
975 buf[3] = payload[2];
976 buf[4] = wdata->state.accel_split[1];
977 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +0200978
979 ir_to_input2(wdata, &payload[3], false);
980 ir_to_input3(wdata, &payload[12], false);
981 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200982}
983
David Herrmanna4d19192011-07-05 13:45:15 +0200984struct wiiproto_handler {
985 __u8 id;
986 size_t size;
987 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
988};
989
990static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200991 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmannbe1ecd62011-09-06 13:50:33 +0200992 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmannc87019e2011-08-17 11:43:24 +0200993 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200994 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +0200995 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann7336b9f2011-09-06 13:50:32 +0200996 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmannefcf9182011-09-06 13:50:29 +0200997 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann7336b9f2011-09-06 13:50:32 +0200998 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmannefcf9182011-09-06 13:50:29 +0200999 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmanneac39e72011-09-06 13:50:31 +02001000 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmannefcf9182011-09-06 13:50:29 +02001001 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann7336b9f2011-09-06 13:50:32 +02001002 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +02001003 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
1004 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +02001005 { .id = 0 }
1006};
1007
David Herrmann02fb72a2011-07-05 13:45:09 +02001008static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
1009 u8 *raw_data, int size)
1010{
David Herrmann4d36e972011-07-05 13:45:12 +02001011 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +02001012 struct wiiproto_handler *h;
1013 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001014 unsigned long flags;
David Herrmann7336b9f2011-09-06 13:50:32 +02001015 bool handled = false;
David Herrmann4d36e972011-07-05 13:45:12 +02001016
David Herrmann02fb72a2011-07-05 13:45:09 +02001017 if (size < 1)
1018 return -EINVAL;
1019
David Herrmann32a0d9a2011-07-05 13:45:18 +02001020 spin_lock_irqsave(&wdata->state.lock, flags);
1021
David Herrmanna4d19192011-07-05 13:45:15 +02001022 for (i = 0; handlers[i].id; ++i) {
1023 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +02001024 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +02001025 h->func(wdata, &raw_data[1]);
David Herrmann7336b9f2011-09-06 13:50:32 +02001026 handled = true;
1027 }
David Herrmanna4d19192011-07-05 13:45:15 +02001028 }
1029
David Herrmann7336b9f2011-09-06 13:50:32 +02001030 if (!handled)
1031 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
1032 size);
1033
David Herrmann32a0d9a2011-07-05 13:45:18 +02001034 spin_unlock_irqrestore(&wdata->state.lock, flags);
1035
David Herrmann02fb72a2011-07-05 13:45:09 +02001036 return 0;
1037}
1038
David Herrmann23a5a4a2011-08-17 11:43:22 +02001039static void wiimote_leds_destroy(struct wiimote_data *wdata)
1040{
1041 int i;
1042 struct led_classdev *led;
1043
1044 for (i = 0; i < 4; ++i) {
1045 if (wdata->leds[i]) {
1046 led = wdata->leds[i];
1047 wdata->leds[i] = NULL;
1048 led_classdev_unregister(led);
1049 kfree(led);
1050 }
1051 }
1052}
1053
1054static int wiimote_leds_create(struct wiimote_data *wdata)
1055{
1056 int i, ret;
1057 struct device *dev = &wdata->hdev->dev;
1058 size_t namesz = strlen(dev_name(dev)) + 9;
1059 struct led_classdev *led;
1060 char *name;
1061
1062 for (i = 0; i < 4; ++i) {
1063 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
1064 if (!led) {
1065 ret = -ENOMEM;
1066 goto err;
1067 }
1068 name = (void*)&led[1];
1069 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
1070 led->name = name;
1071 led->brightness = 0;
1072 led->max_brightness = 1;
1073 led->brightness_get = wiimote_leds_get;
1074 led->brightness_set = wiimote_leds_set;
1075
1076 ret = led_classdev_register(dev, led);
1077 if (ret) {
1078 kfree(led);
1079 goto err;
1080 }
1081 wdata->leds[i] = led;
1082 }
1083
1084 return 0;
1085
1086err:
1087 wiimote_leds_destroy(wdata);
1088 return ret;
1089}
1090
David Herrmanne894d0e2011-07-05 13:45:10 +02001091static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1092{
1093 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +02001094 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +02001095
1096 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1097 if (!wdata)
1098 return NULL;
1099
David Herrmann672bc4e2011-07-05 13:45:11 +02001100 wdata->input = input_allocate_device();
David Herrmann98a558a2011-09-06 13:50:28 +02001101 if (!wdata->input)
1102 goto err;
David Herrmann672bc4e2011-07-05 13:45:11 +02001103
David Herrmanne894d0e2011-07-05 13:45:10 +02001104 wdata->hdev = hdev;
1105 hid_set_drvdata(hdev, wdata);
1106
David Herrmann672bc4e2011-07-05 13:45:11 +02001107 input_set_drvdata(wdata->input, wdata);
David Herrmann26af1742011-08-17 11:43:21 +02001108 wdata->input->open = wiimote_input_open;
1109 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +02001110 wdata->input->dev.parent = &wdata->hdev->dev;
1111 wdata->input->id.bustype = wdata->hdev->bus;
1112 wdata->input->id.vendor = wdata->hdev->vendor;
1113 wdata->input->id.product = wdata->hdev->product;
1114 wdata->input->id.version = wdata->hdev->version;
1115 wdata->input->name = WIIMOTE_NAME;
1116
David Herrmann1abb9ad2011-07-05 13:45:16 +02001117 set_bit(EV_KEY, wdata->input->evbit);
1118 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
1119 set_bit(wiiproto_keymap[i], wdata->input->keybit);
1120
David Herrmannd020be92011-09-06 13:50:27 +02001121 set_bit(FF_RUMBLE, wdata->input->ffbit);
David Herrmann98a558a2011-09-06 13:50:28 +02001122 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
1123 goto err_input;
1124
1125 wdata->accel = input_allocate_device();
1126 if (!wdata->accel)
1127 goto err_input;
1128
1129 input_set_drvdata(wdata->accel, wdata);
1130 wdata->accel->open = wiimote_accel_open;
1131 wdata->accel->close = wiimote_accel_close;
1132 wdata->accel->dev.parent = &wdata->hdev->dev;
1133 wdata->accel->id.bustype = wdata->hdev->bus;
1134 wdata->accel->id.vendor = wdata->hdev->vendor;
1135 wdata->accel->id.product = wdata->hdev->product;
1136 wdata->accel->id.version = wdata->hdev->version;
1137 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
1138
1139 set_bit(EV_ABS, wdata->accel->evbit);
1140 set_bit(ABS_RX, wdata->accel->absbit);
1141 set_bit(ABS_RY, wdata->accel->absbit);
1142 set_bit(ABS_RZ, wdata->accel->absbit);
1143 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
1144 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
1145 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
David Herrmannd020be92011-09-06 13:50:27 +02001146
David Herrmannf363e4f2011-09-06 13:50:30 +02001147 wdata->ir = input_allocate_device();
1148 if (!wdata->ir)
1149 goto err_ir;
1150
1151 input_set_drvdata(wdata->ir, wdata);
David Herrmann0370d7c2011-09-06 13:50:37 +02001152 wdata->ir->open = wiimote_ir_open;
1153 wdata->ir->close = wiimote_ir_close;
David Herrmannf363e4f2011-09-06 13:50:30 +02001154 wdata->ir->dev.parent = &wdata->hdev->dev;
1155 wdata->ir->id.bustype = wdata->hdev->bus;
1156 wdata->ir->id.vendor = wdata->hdev->vendor;
1157 wdata->ir->id.product = wdata->hdev->product;
1158 wdata->ir->id.version = wdata->hdev->version;
1159 wdata->ir->name = WIIMOTE_NAME " IR";
1160
1161 set_bit(EV_ABS, wdata->ir->evbit);
1162 set_bit(ABS_HAT0X, wdata->ir->absbit);
1163 set_bit(ABS_HAT0Y, wdata->ir->absbit);
1164 set_bit(ABS_HAT1X, wdata->ir->absbit);
1165 set_bit(ABS_HAT1Y, wdata->ir->absbit);
1166 set_bit(ABS_HAT2X, wdata->ir->absbit);
1167 set_bit(ABS_HAT2Y, wdata->ir->absbit);
1168 set_bit(ABS_HAT3X, wdata->ir->absbit);
1169 set_bit(ABS_HAT3Y, wdata->ir->absbit);
1170 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1171 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1172 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1173 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1174 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1175 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1176 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1177 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1178
David Herrmann23c063c2011-07-05 13:45:14 +02001179 spin_lock_init(&wdata->qlock);
1180 INIT_WORK(&wdata->worker, wiimote_worker);
1181
David Herrmann32a0d9a2011-07-05 13:45:18 +02001182 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001183 init_completion(&wdata->state.ready);
1184 mutex_init(&wdata->state.sync);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001185
David Herrmanne894d0e2011-07-05 13:45:10 +02001186 return wdata;
David Herrmann98a558a2011-09-06 13:50:28 +02001187
David Herrmannf363e4f2011-09-06 13:50:30 +02001188err_ir:
1189 input_free_device(wdata->accel);
David Herrmann98a558a2011-09-06 13:50:28 +02001190err_input:
1191 input_free_device(wdata->input);
1192err:
1193 kfree(wdata);
1194 return NULL;
David Herrmanne894d0e2011-07-05 13:45:10 +02001195}
1196
1197static void wiimote_destroy(struct wiimote_data *wdata)
1198{
David Herrmann23a5a4a2011-08-17 11:43:22 +02001199 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001200
David Herrmann6591d752011-09-06 13:50:39 +02001201 power_supply_unregister(&wdata->battery);
David Herrmann98a558a2011-09-06 13:50:28 +02001202 input_unregister_device(wdata->accel);
David Herrmannf363e4f2011-09-06 13:50:30 +02001203 input_unregister_device(wdata->ir);
David Herrmann3989ef62011-08-17 11:43:20 +02001204 input_unregister_device(wdata->input);
1205 cancel_work_sync(&wdata->worker);
1206 hid_hw_stop(wdata->hdev);
1207
David Herrmanne894d0e2011-07-05 13:45:10 +02001208 kfree(wdata);
1209}
1210
David Herrmann02fb72a2011-07-05 13:45:09 +02001211static int wiimote_hid_probe(struct hid_device *hdev,
1212 const struct hid_device_id *id)
1213{
David Herrmanne894d0e2011-07-05 13:45:10 +02001214 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001215 int ret;
1216
David Herrmanne894d0e2011-07-05 13:45:10 +02001217 wdata = wiimote_create(hdev);
1218 if (!wdata) {
1219 hid_err(hdev, "Can't alloc device\n");
1220 return -ENOMEM;
1221 }
1222
David Herrmann02fb72a2011-07-05 13:45:09 +02001223 ret = hid_parse(hdev);
1224 if (ret) {
1225 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001226 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001227 }
1228
1229 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1230 if (ret) {
1231 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001232 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001233 }
1234
David Herrmann98a558a2011-09-06 13:50:28 +02001235 ret = input_register_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001236 if (ret) {
1237 hid_err(hdev, "Cannot register input device\n");
1238 goto err_stop;
1239 }
1240
David Herrmannf363e4f2011-09-06 13:50:30 +02001241 ret = input_register_device(wdata->ir);
1242 if (ret) {
1243 hid_err(hdev, "Cannot register input device\n");
1244 goto err_ir;
1245 }
1246
David Herrmann98a558a2011-09-06 13:50:28 +02001247 ret = input_register_device(wdata->input);
1248 if (ret) {
1249 hid_err(hdev, "Cannot register input device\n");
1250 goto err_input;
1251 }
1252
David Herrmann6591d752011-09-06 13:50:39 +02001253 wdata->battery.properties = wiimote_battery_props;
1254 wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props);
1255 wdata->battery.get_property = wiimote_battery_get_property;
1256 wdata->battery.name = "wiimote_battery";
1257 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1258 wdata->battery.use_for_apm = 0;
1259
1260 ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
1261 if (ret) {
1262 hid_err(hdev, "Cannot register battery device\n");
1263 goto err_battery;
1264 }
1265
David Herrmann23a5a4a2011-08-17 11:43:22 +02001266 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001267 if (ret)
1268 goto err_free;
1269
David Herrmann02fb72a2011-07-05 13:45:09 +02001270 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001271
1272 /* by default set led1 after device initialization */
1273 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +02001274 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001275 spin_unlock_irq(&wdata->state.lock);
1276
David Herrmann02fb72a2011-07-05 13:45:09 +02001277 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001278
David Herrmann3989ef62011-08-17 11:43:20 +02001279err_free:
1280 wiimote_destroy(wdata);
1281 return ret;
1282
David Herrmann6591d752011-09-06 13:50:39 +02001283err_battery:
1284 input_unregister_device(wdata->input);
1285 wdata->input = NULL;
David Herrmann98a558a2011-09-06 13:50:28 +02001286err_input:
David Herrmannf363e4f2011-09-06 13:50:30 +02001287 input_unregister_device(wdata->ir);
1288 wdata->ir = NULL;
1289err_ir:
David Herrmann98a558a2011-09-06 13:50:28 +02001290 input_unregister_device(wdata->accel);
1291 wdata->accel = NULL;
David Herrmann672bc4e2011-07-05 13:45:11 +02001292err_stop:
1293 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001294err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001295 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001296 input_free_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001297 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +02001298 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001299 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001300}
1301
1302static void wiimote_hid_remove(struct hid_device *hdev)
1303{
David Herrmanne894d0e2011-07-05 13:45:10 +02001304 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1305
David Herrmann02fb72a2011-07-05 13:45:09 +02001306 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001307 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001308}
1309
1310static const struct hid_device_id wiimote_hid_devices[] = {
1311 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1312 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1313 { }
1314};
1315MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1316
1317static struct hid_driver wiimote_hid_driver = {
1318 .name = "wiimote",
1319 .id_table = wiimote_hid_devices,
1320 .probe = wiimote_hid_probe,
1321 .remove = wiimote_hid_remove,
1322 .raw_event = wiimote_hid_event,
1323};
1324
David Herrmannfb51b442011-07-05 13:45:08 +02001325static int __init wiimote_init(void)
1326{
David Herrmann02fb72a2011-07-05 13:45:09 +02001327 int ret;
1328
1329 ret = hid_register_driver(&wiimote_hid_driver);
1330 if (ret)
1331 pr_err("Can't register wiimote hid driver\n");
1332
1333 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +02001334}
1335
1336static void __exit wiimote_exit(void)
1337{
David Herrmann02fb72a2011-07-05 13:45:09 +02001338 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +02001339}
1340
1341module_init(wiimote_init);
1342module_exit(wiimote_exit);
1343MODULE_LICENSE("GPL");
1344MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1345MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
1346MODULE_VERSION(WIIMOTE_VERSION);