2 * HID driver for Nintendo Wiimote devices
3 * Copyright (c) 2011 David Herrmann
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)
13 #include <linux/atomic.h>
14 #include <linux/device.h>
15 #include <linux/hid.h>
16 #include <linux/input.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
21 #define WIIMOTE_VERSION "0.1"
22 #define WIIMOTE_NAME "Nintendo Wii Remote"
23 #define WIIMOTE_BUFSIZE 32
26 __u8 data
[HID_MAX_BUFFER_SIZE
];
30 struct wiimote_state
{
37 struct hid_device
*hdev
;
38 struct input_dev
*input
;
43 struct wiimote_buf outq
[WIIMOTE_BUFSIZE
];
44 struct work_struct worker
;
46 struct wiimote_state state
;
49 #define WIIPROTO_FLAG_LED1 0x01
50 #define WIIPROTO_FLAG_LED2 0x02
51 #define WIIPROTO_FLAG_LED3 0x04
52 #define WIIPROTO_FLAG_LED4 0x08
53 #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
54 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
57 WIIPROTO_REQ_LED
= 0x11,
58 WIIPROTO_REQ_DRM_K
= 0x30,
76 static __u16 wiiproto_keymap
[] = {
77 KEY_LEFT
, /* WIIPROTO_KEY_LEFT */
78 KEY_RIGHT
, /* WIIPROTO_KEY_RIGHT */
79 KEY_UP
, /* WIIPROTO_KEY_UP */
80 KEY_DOWN
, /* WIIPROTO_KEY_DOWN */
81 KEY_NEXT
, /* WIIPROTO_KEY_PLUS */
82 KEY_PREVIOUS
, /* WIIPROTO_KEY_MINUS */
83 BTN_1
, /* WIIPROTO_KEY_ONE */
84 BTN_2
, /* WIIPROTO_KEY_TWO */
85 BTN_A
, /* WIIPROTO_KEY_A */
86 BTN_B
, /* WIIPROTO_KEY_B */
87 BTN_MODE
, /* WIIPROTO_KEY_HOME */
90 static ssize_t
wiimote_hid_send(struct hid_device
*hdev
, __u8
*buffer
,
96 if (!hdev
->hid_output_raw_report
)
99 buf
= kmemdup(buffer
, count
, GFP_KERNEL
);
103 ret
= hdev
->hid_output_raw_report(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
109 static void wiimote_worker(struct work_struct
*work
)
111 struct wiimote_data
*wdata
= container_of(work
, struct wiimote_data
,
115 spin_lock_irqsave(&wdata
->qlock
, flags
);
117 while (wdata
->head
!= wdata
->tail
) {
118 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
119 wiimote_hid_send(wdata
->hdev
, wdata
->outq
[wdata
->tail
].data
,
120 wdata
->outq
[wdata
->tail
].size
);
121 spin_lock_irqsave(&wdata
->qlock
, flags
);
123 wdata
->tail
= (wdata
->tail
+ 1) % WIIMOTE_BUFSIZE
;
126 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
129 static void wiimote_queue(struct wiimote_data
*wdata
, const __u8
*buffer
,
135 if (count
> HID_MAX_BUFFER_SIZE
) {
136 hid_warn(wdata
->hdev
, "Sending too large output report\n");
141 * Copy new request into our output queue and check whether the
142 * queue is full. If it is full, discard this request.
143 * If it is empty we need to start a new worker that will
144 * send out the buffer to the hid device.
145 * If the queue is not empty, then there must be a worker
146 * that is currently sending out our buffer and this worker
147 * will reschedule itself until the queue is empty.
150 spin_lock_irqsave(&wdata
->qlock
, flags
);
152 memcpy(wdata
->outq
[wdata
->head
].data
, buffer
, count
);
153 wdata
->outq
[wdata
->head
].size
= count
;
154 newhead
= (wdata
->head
+ 1) % WIIMOTE_BUFSIZE
;
156 if (wdata
->head
== wdata
->tail
) {
157 wdata
->head
= newhead
;
158 schedule_work(&wdata
->worker
);
159 } else if (newhead
!= wdata
->tail
) {
160 wdata
->head
= newhead
;
162 hid_warn(wdata
->hdev
, "Output queue is full");
165 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
168 static void wiiproto_req_leds(struct wiimote_data
*wdata
, int leds
)
172 leds
&= WIIPROTO_FLAGS_LEDS
;
173 if ((wdata
->state
.flags
& WIIPROTO_FLAGS_LEDS
) == leds
)
175 wdata
->state
.flags
= (wdata
->state
.flags
& ~WIIPROTO_FLAGS_LEDS
) | leds
;
177 cmd
[0] = WIIPROTO_REQ_LED
;
180 if (leds
& WIIPROTO_FLAG_LED1
)
182 if (leds
& WIIPROTO_FLAG_LED2
)
184 if (leds
& WIIPROTO_FLAG_LED3
)
186 if (leds
& WIIPROTO_FLAG_LED4
)
189 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
192 static int wiimote_input_event(struct input_dev
*dev
, unsigned int type
,
193 unsigned int code
, int value
)
195 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
197 if (!atomic_read(&wdata
->ready
))
199 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
205 static void handler_keys(struct wiimote_data
*wdata
, const __u8
*payload
)
207 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_LEFT
],
208 !!(payload
[0] & 0x01));
209 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_RIGHT
],
210 !!(payload
[0] & 0x02));
211 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_DOWN
],
212 !!(payload
[0] & 0x04));
213 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_UP
],
214 !!(payload
[0] & 0x08));
215 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_PLUS
],
216 !!(payload
[0] & 0x10));
217 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_TWO
],
218 !!(payload
[1] & 0x01));
219 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_ONE
],
220 !!(payload
[1] & 0x02));
221 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_B
],
222 !!(payload
[1] & 0x04));
223 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_A
],
224 !!(payload
[1] & 0x08));
225 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_MINUS
],
226 !!(payload
[1] & 0x10));
227 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_HOME
],
228 !!(payload
[1] & 0x80));
229 input_sync(wdata
->input
);
232 struct wiiproto_handler
{
235 void (*func
)(struct wiimote_data
*wdata
, const __u8
*payload
);
238 static struct wiiproto_handler handlers
[] = {
239 { .id
= WIIPROTO_REQ_DRM_K
, .size
= 2, .func
= handler_keys
},
243 static int wiimote_hid_event(struct hid_device
*hdev
, struct hid_report
*report
,
244 u8
*raw_data
, int size
)
246 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
247 struct wiiproto_handler
*h
;
251 if (!atomic_read(&wdata
->ready
))
253 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
259 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
261 for (i
= 0; handlers
[i
].id
; ++i
) {
263 if (h
->id
== raw_data
[0] && h
->size
< size
)
264 h
->func(wdata
, &raw_data
[1]);
267 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
272 static struct wiimote_data
*wiimote_create(struct hid_device
*hdev
)
274 struct wiimote_data
*wdata
;
277 wdata
= kzalloc(sizeof(*wdata
), GFP_KERNEL
);
281 wdata
->input
= input_allocate_device();
288 hid_set_drvdata(hdev
, wdata
);
290 input_set_drvdata(wdata
->input
, wdata
);
291 wdata
->input
->event
= wiimote_input_event
;
292 wdata
->input
->dev
.parent
= &wdata
->hdev
->dev
;
293 wdata
->input
->id
.bustype
= wdata
->hdev
->bus
;
294 wdata
->input
->id
.vendor
= wdata
->hdev
->vendor
;
295 wdata
->input
->id
.product
= wdata
->hdev
->product
;
296 wdata
->input
->id
.version
= wdata
->hdev
->version
;
297 wdata
->input
->name
= WIIMOTE_NAME
;
299 set_bit(EV_KEY
, wdata
->input
->evbit
);
300 for (i
= 0; i
< WIIPROTO_KEY_COUNT
; ++i
)
301 set_bit(wiiproto_keymap
[i
], wdata
->input
->keybit
);
303 spin_lock_init(&wdata
->qlock
);
304 INIT_WORK(&wdata
->worker
, wiimote_worker
);
306 spin_lock_init(&wdata
->state
.lock
);
311 static void wiimote_destroy(struct wiimote_data
*wdata
)
316 static int wiimote_hid_probe(struct hid_device
*hdev
,
317 const struct hid_device_id
*id
)
319 struct wiimote_data
*wdata
;
322 wdata
= wiimote_create(hdev
);
324 hid_err(hdev
, "Can't alloc device\n");
328 ret
= hid_parse(hdev
);
330 hid_err(hdev
, "HID parse failed\n");
334 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
336 hid_err(hdev
, "HW start failed\n");
340 ret
= input_register_device(wdata
->input
);
342 hid_err(hdev
, "Cannot register input device\n");
346 /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
348 atomic_set(&wdata
->ready
, 1);
349 hid_info(hdev
, "New device registered\n");
351 /* by default set led1 after device initialization */
352 spin_lock_irq(&wdata
->state
.lock
);
353 wiiproto_req_leds(wdata
, WIIPROTO_FLAG_LED1
);
354 spin_unlock_irq(&wdata
->state
.lock
);
361 input_free_device(wdata
->input
);
362 wiimote_destroy(wdata
);
366 static void wiimote_hid_remove(struct hid_device
*hdev
)
368 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
370 hid_info(hdev
, "Device removed\n");
373 input_unregister_device(wdata
->input
);
375 cancel_work_sync(&wdata
->worker
);
376 wiimote_destroy(wdata
);
379 static const struct hid_device_id wiimote_hid_devices
[] = {
380 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO
,
381 USB_DEVICE_ID_NINTENDO_WIIMOTE
) },
384 MODULE_DEVICE_TABLE(hid
, wiimote_hid_devices
);
386 static struct hid_driver wiimote_hid_driver
= {
388 .id_table
= wiimote_hid_devices
,
389 .probe
= wiimote_hid_probe
,
390 .remove
= wiimote_hid_remove
,
391 .raw_event
= wiimote_hid_event
,
394 static int __init
wiimote_init(void)
398 ret
= hid_register_driver(&wiimote_hid_driver
);
400 pr_err("Can't register wiimote hid driver\n");
405 static void __exit
wiimote_exit(void)
407 hid_unregister_driver(&wiimote_hid_driver
);
410 module_init(wiimote_init
);
411 module_exit(wiimote_exit
);
412 MODULE_LICENSE("GPL");
413 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
414 MODULE_DESCRIPTION(WIIMOTE_NAME
" Device Driver");
415 MODULE_VERSION(WIIMOTE_VERSION
);