ftmac100: fix skb truesize underestimation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hid / hid-wiimote.c
blob85a02e5f9fe873e5097a55861ec55c77f50ee05f
1 /*
2 * HID driver for Nintendo Wiimote devices
3 * Copyright (c) 2011 David Herrmann
4 */
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.
13 #include <linux/device.h>
14 #include <linux/hid.h>
15 #include <linux/input.h>
16 #include <linux/leds.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include "hid-ids.h"
21 #define WIIMOTE_VERSION "0.1"
22 #define WIIMOTE_NAME "Nintendo Wii Remote"
23 #define WIIMOTE_BUFSIZE 32
25 struct wiimote_buf {
26 __u8 data[HID_MAX_BUFFER_SIZE];
27 size_t size;
30 struct wiimote_state {
31 spinlock_t lock;
32 __u8 flags;
35 struct wiimote_data {
36 struct hid_device *hdev;
37 struct input_dev *input;
38 struct led_classdev *leds[4];
40 spinlock_t qlock;
41 __u8 head;
42 __u8 tail;
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)
56 /* return flag for led \num */
57 #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
59 enum wiiproto_reqs {
60 WIIPROTO_REQ_NULL = 0x0,
61 WIIPROTO_REQ_LED = 0x11,
62 WIIPROTO_REQ_DRM = 0x12,
63 WIIPROTO_REQ_STATUS = 0x20,
64 WIIPROTO_REQ_RETURN = 0x22,
65 WIIPROTO_REQ_DRM_K = 0x30,
68 enum wiiproto_keys {
69 WIIPROTO_KEY_LEFT,
70 WIIPROTO_KEY_RIGHT,
71 WIIPROTO_KEY_UP,
72 WIIPROTO_KEY_DOWN,
73 WIIPROTO_KEY_PLUS,
74 WIIPROTO_KEY_MINUS,
75 WIIPROTO_KEY_ONE,
76 WIIPROTO_KEY_TWO,
77 WIIPROTO_KEY_A,
78 WIIPROTO_KEY_B,
79 WIIPROTO_KEY_HOME,
80 WIIPROTO_KEY_COUNT
83 static __u16 wiiproto_keymap[] = {
84 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
85 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
86 KEY_UP, /* WIIPROTO_KEY_UP */
87 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
88 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
89 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
90 BTN_1, /* WIIPROTO_KEY_ONE */
91 BTN_2, /* WIIPROTO_KEY_TWO */
92 BTN_A, /* WIIPROTO_KEY_A */
93 BTN_B, /* WIIPROTO_KEY_B */
94 BTN_MODE, /* WIIPROTO_KEY_HOME */
97 static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
98 size_t count)
100 __u8 *buf;
101 ssize_t ret;
103 if (!hdev->hid_output_raw_report)
104 return -ENODEV;
106 buf = kmemdup(buffer, count, GFP_KERNEL);
107 if (!buf)
108 return -ENOMEM;
110 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
112 kfree(buf);
113 return ret;
116 static void wiimote_worker(struct work_struct *work)
118 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
119 worker);
120 unsigned long flags;
122 spin_lock_irqsave(&wdata->qlock, flags);
124 while (wdata->head != wdata->tail) {
125 spin_unlock_irqrestore(&wdata->qlock, flags);
126 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
127 wdata->outq[wdata->tail].size);
128 spin_lock_irqsave(&wdata->qlock, flags);
130 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
133 spin_unlock_irqrestore(&wdata->qlock, flags);
136 static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
137 size_t count)
139 unsigned long flags;
140 __u8 newhead;
142 if (count > HID_MAX_BUFFER_SIZE) {
143 hid_warn(wdata->hdev, "Sending too large output report\n");
144 return;
148 * Copy new request into our output queue and check whether the
149 * queue is full. If it is full, discard this request.
150 * If it is empty we need to start a new worker that will
151 * send out the buffer to the hid device.
152 * If the queue is not empty, then there must be a worker
153 * that is currently sending out our buffer and this worker
154 * will reschedule itself until the queue is empty.
157 spin_lock_irqsave(&wdata->qlock, flags);
159 memcpy(wdata->outq[wdata->head].data, buffer, count);
160 wdata->outq[wdata->head].size = count;
161 newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
163 if (wdata->head == wdata->tail) {
164 wdata->head = newhead;
165 schedule_work(&wdata->worker);
166 } else if (newhead != wdata->tail) {
167 wdata->head = newhead;
168 } else {
169 hid_warn(wdata->hdev, "Output queue is full");
172 spin_unlock_irqrestore(&wdata->qlock, flags);
175 static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
177 __u8 cmd[2];
179 leds &= WIIPROTO_FLAGS_LEDS;
180 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
181 return;
182 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
184 cmd[0] = WIIPROTO_REQ_LED;
185 cmd[1] = 0;
187 if (leds & WIIPROTO_FLAG_LED1)
188 cmd[1] |= 0x10;
189 if (leds & WIIPROTO_FLAG_LED2)
190 cmd[1] |= 0x20;
191 if (leds & WIIPROTO_FLAG_LED3)
192 cmd[1] |= 0x40;
193 if (leds & WIIPROTO_FLAG_LED4)
194 cmd[1] |= 0x80;
196 wiimote_queue(wdata, cmd, sizeof(cmd));
200 * Check what peripherals of the wiimote are currently
201 * active and select a proper DRM that supports all of
202 * the requested data inputs.
204 static __u8 select_drm(struct wiimote_data *wdata)
206 return WIIPROTO_REQ_DRM_K;
209 static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
211 __u8 cmd[3];
213 if (drm == WIIPROTO_REQ_NULL)
214 drm = select_drm(wdata);
216 cmd[0] = WIIPROTO_REQ_DRM;
217 cmd[1] = 0;
218 cmd[2] = drm;
220 wiimote_queue(wdata, cmd, sizeof(cmd));
223 static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
225 struct wiimote_data *wdata;
226 struct device *dev = led_dev->dev->parent;
227 int i;
228 unsigned long flags;
229 bool value = false;
231 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
233 for (i = 0; i < 4; ++i) {
234 if (wdata->leds[i] == led_dev) {
235 spin_lock_irqsave(&wdata->state.lock, flags);
236 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
237 spin_unlock_irqrestore(&wdata->state.lock, flags);
238 break;
242 return value ? LED_FULL : LED_OFF;
245 static void wiimote_leds_set(struct led_classdev *led_dev,
246 enum led_brightness value)
248 struct wiimote_data *wdata;
249 struct device *dev = led_dev->dev->parent;
250 int i;
251 unsigned long flags;
252 __u8 state, flag;
254 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
256 for (i = 0; i < 4; ++i) {
257 if (wdata->leds[i] == led_dev) {
258 flag = WIIPROTO_FLAG_LED(i + 1);
259 spin_lock_irqsave(&wdata->state.lock, flags);
260 state = wdata->state.flags;
261 if (value == LED_OFF)
262 wiiproto_req_leds(wdata, state & ~flag);
263 else
264 wiiproto_req_leds(wdata, state | flag);
265 spin_unlock_irqrestore(&wdata->state.lock, flags);
266 break;
271 static int wiimote_input_event(struct input_dev *dev, unsigned int type,
272 unsigned int code, int value)
274 return 0;
277 static int wiimote_input_open(struct input_dev *dev)
279 struct wiimote_data *wdata = input_get_drvdata(dev);
281 return hid_hw_open(wdata->hdev);
284 static void wiimote_input_close(struct input_dev *dev)
286 struct wiimote_data *wdata = input_get_drvdata(dev);
288 hid_hw_close(wdata->hdev);
291 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
293 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
294 !!(payload[0] & 0x01));
295 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
296 !!(payload[0] & 0x02));
297 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
298 !!(payload[0] & 0x04));
299 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
300 !!(payload[0] & 0x08));
301 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
302 !!(payload[0] & 0x10));
303 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
304 !!(payload[1] & 0x01));
305 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
306 !!(payload[1] & 0x02));
307 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
308 !!(payload[1] & 0x04));
309 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
310 !!(payload[1] & 0x08));
311 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
312 !!(payload[1] & 0x10));
313 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
314 !!(payload[1] & 0x80));
315 input_sync(wdata->input);
318 static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
320 handler_keys(wdata, payload);
322 /* on status reports the drm is reset so we need to resend the drm */
323 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
326 static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
328 __u8 err = payload[3];
329 __u8 cmd = payload[2];
331 handler_keys(wdata, payload);
333 if (err)
334 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
335 cmd);
338 struct wiiproto_handler {
339 __u8 id;
340 size_t size;
341 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
344 static struct wiiproto_handler handlers[] = {
345 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
346 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
347 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
348 { .id = 0 }
351 static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
352 u8 *raw_data, int size)
354 struct wiimote_data *wdata = hid_get_drvdata(hdev);
355 struct wiiproto_handler *h;
356 int i;
357 unsigned long flags;
359 if (size < 1)
360 return -EINVAL;
362 spin_lock_irqsave(&wdata->state.lock, flags);
364 for (i = 0; handlers[i].id; ++i) {
365 h = &handlers[i];
366 if (h->id == raw_data[0] && h->size < size)
367 h->func(wdata, &raw_data[1]);
370 spin_unlock_irqrestore(&wdata->state.lock, flags);
372 return 0;
375 static void wiimote_leds_destroy(struct wiimote_data *wdata)
377 int i;
378 struct led_classdev *led;
380 for (i = 0; i < 4; ++i) {
381 if (wdata->leds[i]) {
382 led = wdata->leds[i];
383 wdata->leds[i] = NULL;
384 led_classdev_unregister(led);
385 kfree(led);
390 static int wiimote_leds_create(struct wiimote_data *wdata)
392 int i, ret;
393 struct device *dev = &wdata->hdev->dev;
394 size_t namesz = strlen(dev_name(dev)) + 9;
395 struct led_classdev *led;
396 char *name;
398 for (i = 0; i < 4; ++i) {
399 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
400 if (!led) {
401 ret = -ENOMEM;
402 goto err;
404 name = (void*)&led[1];
405 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
406 led->name = name;
407 led->brightness = 0;
408 led->max_brightness = 1;
409 led->brightness_get = wiimote_leds_get;
410 led->brightness_set = wiimote_leds_set;
412 ret = led_classdev_register(dev, led);
413 if (ret) {
414 kfree(led);
415 goto err;
417 wdata->leds[i] = led;
420 return 0;
422 err:
423 wiimote_leds_destroy(wdata);
424 return ret;
427 static struct wiimote_data *wiimote_create(struct hid_device *hdev)
429 struct wiimote_data *wdata;
430 int i;
432 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
433 if (!wdata)
434 return NULL;
436 wdata->input = input_allocate_device();
437 if (!wdata->input) {
438 kfree(wdata);
439 return NULL;
442 wdata->hdev = hdev;
443 hid_set_drvdata(hdev, wdata);
445 input_set_drvdata(wdata->input, wdata);
446 wdata->input->event = wiimote_input_event;
447 wdata->input->open = wiimote_input_open;
448 wdata->input->close = wiimote_input_close;
449 wdata->input->dev.parent = &wdata->hdev->dev;
450 wdata->input->id.bustype = wdata->hdev->bus;
451 wdata->input->id.vendor = wdata->hdev->vendor;
452 wdata->input->id.product = wdata->hdev->product;
453 wdata->input->id.version = wdata->hdev->version;
454 wdata->input->name = WIIMOTE_NAME;
456 set_bit(EV_KEY, wdata->input->evbit);
457 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
458 set_bit(wiiproto_keymap[i], wdata->input->keybit);
460 spin_lock_init(&wdata->qlock);
461 INIT_WORK(&wdata->worker, wiimote_worker);
463 spin_lock_init(&wdata->state.lock);
465 return wdata;
468 static void wiimote_destroy(struct wiimote_data *wdata)
470 wiimote_leds_destroy(wdata);
472 input_unregister_device(wdata->input);
473 cancel_work_sync(&wdata->worker);
474 hid_hw_stop(wdata->hdev);
476 kfree(wdata);
479 static int wiimote_hid_probe(struct hid_device *hdev,
480 const struct hid_device_id *id)
482 struct wiimote_data *wdata;
483 int ret;
485 wdata = wiimote_create(hdev);
486 if (!wdata) {
487 hid_err(hdev, "Can't alloc device\n");
488 return -ENOMEM;
491 ret = hid_parse(hdev);
492 if (ret) {
493 hid_err(hdev, "HID parse failed\n");
494 goto err;
497 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
498 if (ret) {
499 hid_err(hdev, "HW start failed\n");
500 goto err;
503 ret = input_register_device(wdata->input);
504 if (ret) {
505 hid_err(hdev, "Cannot register input device\n");
506 goto err_stop;
509 ret = wiimote_leds_create(wdata);
510 if (ret)
511 goto err_free;
513 hid_info(hdev, "New device registered\n");
515 /* by default set led1 after device initialization */
516 spin_lock_irq(&wdata->state.lock);
517 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
518 spin_unlock_irq(&wdata->state.lock);
520 return 0;
522 err_free:
523 wiimote_destroy(wdata);
524 return ret;
526 err_stop:
527 hid_hw_stop(hdev);
528 err:
529 input_free_device(wdata->input);
530 kfree(wdata);
531 return ret;
534 static void wiimote_hid_remove(struct hid_device *hdev)
536 struct wiimote_data *wdata = hid_get_drvdata(hdev);
538 hid_info(hdev, "Device removed\n");
539 wiimote_destroy(wdata);
542 static const struct hid_device_id wiimote_hid_devices[] = {
543 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
544 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
547 MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
549 static struct hid_driver wiimote_hid_driver = {
550 .name = "wiimote",
551 .id_table = wiimote_hid_devices,
552 .probe = wiimote_hid_probe,
553 .remove = wiimote_hid_remove,
554 .raw_event = wiimote_hid_event,
557 static int __init wiimote_init(void)
559 int ret;
561 ret = hid_register_driver(&wiimote_hid_driver);
562 if (ret)
563 pr_err("Can't register wiimote hid driver\n");
565 return ret;
568 static void __exit wiimote_exit(void)
570 hid_unregister_driver(&wiimote_hid_driver);
573 module_init(wiimote_init);
574 module_exit(wiimote_exit);
575 MODULE_LICENSE("GPL");
576 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
577 MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
578 MODULE_VERSION(WIIMOTE_VERSION);