David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1 | /* |
| 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 Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 13 | #include <linux/atomic.h> |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 14 | #include <linux/device.h> |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 15 | #include <linux/hid.h> |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 16 | #include <linux/input.h> |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 17 | #include <linux/module.h> |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 19 | #include "hid-ids.h" |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 20 | |
| 21 | #define WIIMOTE_VERSION "0.1" |
| 22 | #define WIIMOTE_NAME "Nintendo Wii Remote" |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 23 | #define WIIMOTE_BUFSIZE 32 |
| 24 | |
| 25 | struct wiimote_buf { |
| 26 | __u8 data[HID_MAX_BUFFER_SIZE]; |
| 27 | size_t size; |
| 28 | }; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 29 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 30 | struct wiimote_data { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 31 | atomic_t ready; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 32 | struct hid_device *hdev; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 33 | struct input_dev *input; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 34 | |
| 35 | spinlock_t qlock; |
| 36 | __u8 head; |
| 37 | __u8 tail; |
| 38 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; |
| 39 | struct work_struct worker; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame^] | 42 | enum wiiproto_reqs { |
| 43 | WIIPROTO_REQ_DRM_K = 0x30, |
| 44 | }; |
| 45 | |
| 46 | enum wiiproto_keys { |
| 47 | WIIPROTO_KEY_LEFT, |
| 48 | WIIPROTO_KEY_RIGHT, |
| 49 | WIIPROTO_KEY_UP, |
| 50 | WIIPROTO_KEY_DOWN, |
| 51 | WIIPROTO_KEY_PLUS, |
| 52 | WIIPROTO_KEY_MINUS, |
| 53 | WIIPROTO_KEY_ONE, |
| 54 | WIIPROTO_KEY_TWO, |
| 55 | WIIPROTO_KEY_A, |
| 56 | WIIPROTO_KEY_B, |
| 57 | WIIPROTO_KEY_HOME, |
| 58 | WIIPROTO_KEY_COUNT |
| 59 | }; |
| 60 | |
| 61 | static __u16 wiiproto_keymap[] = { |
| 62 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ |
| 63 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ |
| 64 | KEY_UP, /* WIIPROTO_KEY_UP */ |
| 65 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ |
| 66 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ |
| 67 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ |
| 68 | BTN_1, /* WIIPROTO_KEY_ONE */ |
| 69 | BTN_2, /* WIIPROTO_KEY_TWO */ |
| 70 | BTN_A, /* WIIPROTO_KEY_A */ |
| 71 | BTN_B, /* WIIPROTO_KEY_B */ |
| 72 | BTN_MODE, /* WIIPROTO_KEY_HOME */ |
| 73 | }; |
| 74 | |
David Herrmann | 0c218f14 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 75 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
| 76 | size_t count) |
| 77 | { |
| 78 | __u8 *buf; |
| 79 | ssize_t ret; |
| 80 | |
| 81 | if (!hdev->hid_output_raw_report) |
| 82 | return -ENODEV; |
| 83 | |
| 84 | buf = kmemdup(buffer, count, GFP_KERNEL); |
| 85 | if (!buf) |
| 86 | return -ENOMEM; |
| 87 | |
| 88 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); |
| 89 | |
| 90 | kfree(buf); |
| 91 | return ret; |
| 92 | } |
| 93 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 94 | static void wiimote_worker(struct work_struct *work) |
| 95 | { |
| 96 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 97 | worker); |
| 98 | unsigned long flags; |
| 99 | |
| 100 | spin_lock_irqsave(&wdata->qlock, flags); |
| 101 | |
| 102 | while (wdata->head != wdata->tail) { |
| 103 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 104 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, |
| 105 | wdata->outq[wdata->tail].size); |
| 106 | spin_lock_irqsave(&wdata->qlock, flags); |
| 107 | |
| 108 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; |
| 109 | } |
| 110 | |
| 111 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 112 | } |
| 113 | |
| 114 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, |
| 115 | size_t count) |
| 116 | { |
| 117 | unsigned long flags; |
| 118 | __u8 newhead; |
| 119 | |
| 120 | if (count > HID_MAX_BUFFER_SIZE) { |
| 121 | hid_warn(wdata->hdev, "Sending too large output report\n"); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Copy new request into our output queue and check whether the |
| 127 | * queue is full. If it is full, discard this request. |
| 128 | * If it is empty we need to start a new worker that will |
| 129 | * send out the buffer to the hid device. |
| 130 | * If the queue is not empty, then there must be a worker |
| 131 | * that is currently sending out our buffer and this worker |
| 132 | * will reschedule itself until the queue is empty. |
| 133 | */ |
| 134 | |
| 135 | spin_lock_irqsave(&wdata->qlock, flags); |
| 136 | |
| 137 | memcpy(wdata->outq[wdata->head].data, buffer, count); |
| 138 | wdata->outq[wdata->head].size = count; |
| 139 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; |
| 140 | |
| 141 | if (wdata->head == wdata->tail) { |
| 142 | wdata->head = newhead; |
| 143 | schedule_work(&wdata->worker); |
| 144 | } else if (newhead != wdata->tail) { |
| 145 | wdata->head = newhead; |
| 146 | } else { |
| 147 | hid_warn(wdata->hdev, "Output queue is full"); |
| 148 | } |
| 149 | |
| 150 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 151 | } |
| 152 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 153 | static int wiimote_input_event(struct input_dev *dev, unsigned int type, |
| 154 | unsigned int code, int value) |
| 155 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 156 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 157 | |
| 158 | if (!atomic_read(&wdata->ready)) |
| 159 | return -EBUSY; |
| 160 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ |
| 161 | smp_rmb(); |
| 162 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame^] | 166 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
| 167 | { |
| 168 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], |
| 169 | !!(payload[0] & 0x01)); |
| 170 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], |
| 171 | !!(payload[0] & 0x02)); |
| 172 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], |
| 173 | !!(payload[0] & 0x04)); |
| 174 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], |
| 175 | !!(payload[0] & 0x08)); |
| 176 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], |
| 177 | !!(payload[0] & 0x10)); |
| 178 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], |
| 179 | !!(payload[1] & 0x01)); |
| 180 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], |
| 181 | !!(payload[1] & 0x02)); |
| 182 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], |
| 183 | !!(payload[1] & 0x04)); |
| 184 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], |
| 185 | !!(payload[1] & 0x08)); |
| 186 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], |
| 187 | !!(payload[1] & 0x10)); |
| 188 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], |
| 189 | !!(payload[1] & 0x80)); |
| 190 | input_sync(wdata->input); |
| 191 | } |
| 192 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 193 | struct wiiproto_handler { |
| 194 | __u8 id; |
| 195 | size_t size; |
| 196 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); |
| 197 | }; |
| 198 | |
| 199 | static struct wiiproto_handler handlers[] = { |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame^] | 200 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 201 | { .id = 0 } |
| 202 | }; |
| 203 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 204 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 205 | u8 *raw_data, int size) |
| 206 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 207 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 208 | struct wiiproto_handler *h; |
| 209 | int i; |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 210 | |
| 211 | if (!atomic_read(&wdata->ready)) |
| 212 | return -EBUSY; |
| 213 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ |
| 214 | smp_rmb(); |
| 215 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 216 | if (size < 1) |
| 217 | return -EINVAL; |
| 218 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 219 | for (i = 0; handlers[i].id; ++i) { |
| 220 | h = &handlers[i]; |
| 221 | if (h->id == raw_data[0] && h->size < size) |
| 222 | h->func(wdata, &raw_data[1]); |
| 223 | } |
| 224 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 228 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 229 | { |
| 230 | struct wiimote_data *wdata; |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame^] | 231 | int i; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 232 | |
| 233 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 234 | if (!wdata) |
| 235 | return NULL; |
| 236 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 237 | wdata->input = input_allocate_device(); |
| 238 | if (!wdata->input) { |
| 239 | kfree(wdata); |
| 240 | return NULL; |
| 241 | } |
| 242 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 243 | wdata->hdev = hdev; |
| 244 | hid_set_drvdata(hdev, wdata); |
| 245 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 246 | input_set_drvdata(wdata->input, wdata); |
| 247 | wdata->input->event = wiimote_input_event; |
| 248 | wdata->input->dev.parent = &wdata->hdev->dev; |
| 249 | wdata->input->id.bustype = wdata->hdev->bus; |
| 250 | wdata->input->id.vendor = wdata->hdev->vendor; |
| 251 | wdata->input->id.product = wdata->hdev->product; |
| 252 | wdata->input->id.version = wdata->hdev->version; |
| 253 | wdata->input->name = WIIMOTE_NAME; |
| 254 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame^] | 255 | set_bit(EV_KEY, wdata->input->evbit); |
| 256 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) |
| 257 | set_bit(wiiproto_keymap[i], wdata->input->keybit); |
| 258 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 259 | spin_lock_init(&wdata->qlock); |
| 260 | INIT_WORK(&wdata->worker, wiimote_worker); |
| 261 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 262 | return wdata; |
| 263 | } |
| 264 | |
| 265 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 266 | { |
| 267 | kfree(wdata); |
| 268 | } |
| 269 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 270 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 271 | const struct hid_device_id *id) |
| 272 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 273 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 274 | int ret; |
| 275 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 276 | wdata = wiimote_create(hdev); |
| 277 | if (!wdata) { |
| 278 | hid_err(hdev, "Can't alloc device\n"); |
| 279 | return -ENOMEM; |
| 280 | } |
| 281 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 282 | ret = hid_parse(hdev); |
| 283 | if (ret) { |
| 284 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 285 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 289 | if (ret) { |
| 290 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 291 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 292 | } |
| 293 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 294 | ret = input_register_device(wdata->input); |
| 295 | if (ret) { |
| 296 | hid_err(hdev, "Cannot register input device\n"); |
| 297 | goto err_stop; |
| 298 | } |
| 299 | |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 300 | /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */ |
| 301 | smp_wmb(); |
| 302 | atomic_set(&wdata->ready, 1); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 303 | hid_info(hdev, "New device registered\n"); |
| 304 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 305 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 306 | err_stop: |
| 307 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 308 | err: |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 309 | input_free_device(wdata->input); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 310 | wiimote_destroy(wdata); |
| 311 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 315 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 316 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 317 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 318 | hid_info(hdev, "Device removed\n"); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 319 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 320 | hid_hw_stop(hdev); |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 321 | input_unregister_device(wdata->input); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 322 | |
| 323 | cancel_work_sync(&wdata->worker); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 324 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 328 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 329 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
| 330 | { } |
| 331 | }; |
| 332 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 333 | |
| 334 | static struct hid_driver wiimote_hid_driver = { |
| 335 | .name = "wiimote", |
| 336 | .id_table = wiimote_hid_devices, |
| 337 | .probe = wiimote_hid_probe, |
| 338 | .remove = wiimote_hid_remove, |
| 339 | .raw_event = wiimote_hid_event, |
| 340 | }; |
| 341 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 342 | static int __init wiimote_init(void) |
| 343 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 344 | int ret; |
| 345 | |
| 346 | ret = hid_register_driver(&wiimote_hid_driver); |
| 347 | if (ret) |
| 348 | pr_err("Can't register wiimote hid driver\n"); |
| 349 | |
| 350 | return ret; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void __exit wiimote_exit(void) |
| 354 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 355 | hid_unregister_driver(&wiimote_hid_driver); |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | module_init(wiimote_init); |
| 359 | module_exit(wiimote_exit); |
| 360 | MODULE_LICENSE("GPL"); |
| 361 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
| 362 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); |
| 363 | MODULE_VERSION(WIIMOTE_VERSION); |