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 | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 30 | struct wiimote_state { |
| 31 | spinlock_t lock; |
| 32 | __u8 flags; |
| 33 | }; |
| 34 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 35 | struct wiimote_data { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 36 | atomic_t ready; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 37 | struct hid_device *hdev; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 38 | struct input_dev *input; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 39 | |
| 40 | spinlock_t qlock; |
| 41 | __u8 head; |
| 42 | __u8 tail; |
| 43 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; |
| 44 | struct work_struct worker; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 45 | |
| 46 | struct wiimote_state state; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 49 | #define WIIPROTO_FLAG_LED1 0x01 |
| 50 | #define WIIPROTO_FLAG_LED2 0x02 |
| 51 | #define WIIPROTO_FLAG_LED3 0x04 |
| 52 | #define WIIPROTO_FLAG_LED4 0x08 |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 53 | #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ |
| 54 | WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 55 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 56 | enum wiiproto_reqs { |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 57 | WIIPROTO_REQ_LED = 0x11, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 58 | WIIPROTO_REQ_DRM_K = 0x30, |
| 59 | }; |
| 60 | |
| 61 | enum wiiproto_keys { |
| 62 | WIIPROTO_KEY_LEFT, |
| 63 | WIIPROTO_KEY_RIGHT, |
| 64 | WIIPROTO_KEY_UP, |
| 65 | WIIPROTO_KEY_DOWN, |
| 66 | WIIPROTO_KEY_PLUS, |
| 67 | WIIPROTO_KEY_MINUS, |
| 68 | WIIPROTO_KEY_ONE, |
| 69 | WIIPROTO_KEY_TWO, |
| 70 | WIIPROTO_KEY_A, |
| 71 | WIIPROTO_KEY_B, |
| 72 | WIIPROTO_KEY_HOME, |
| 73 | WIIPROTO_KEY_COUNT |
| 74 | }; |
| 75 | |
| 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 */ |
| 88 | }; |
| 89 | |
David Herrmann | 0c218f14 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 90 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
| 91 | size_t count) |
| 92 | { |
| 93 | __u8 *buf; |
| 94 | ssize_t ret; |
| 95 | |
| 96 | if (!hdev->hid_output_raw_report) |
| 97 | return -ENODEV; |
| 98 | |
| 99 | buf = kmemdup(buffer, count, GFP_KERNEL); |
| 100 | if (!buf) |
| 101 | return -ENOMEM; |
| 102 | |
| 103 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); |
| 104 | |
| 105 | kfree(buf); |
| 106 | return ret; |
| 107 | } |
| 108 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 109 | static void wiimote_worker(struct work_struct *work) |
| 110 | { |
| 111 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 112 | worker); |
| 113 | unsigned long flags; |
| 114 | |
| 115 | spin_lock_irqsave(&wdata->qlock, flags); |
| 116 | |
| 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); |
| 122 | |
| 123 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; |
| 124 | } |
| 125 | |
| 126 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 127 | } |
| 128 | |
| 129 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, |
| 130 | size_t count) |
| 131 | { |
| 132 | unsigned long flags; |
| 133 | __u8 newhead; |
| 134 | |
| 135 | if (count > HID_MAX_BUFFER_SIZE) { |
| 136 | hid_warn(wdata->hdev, "Sending too large output report\n"); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | /* |
| 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. |
| 148 | */ |
| 149 | |
| 150 | spin_lock_irqsave(&wdata->qlock, flags); |
| 151 | |
| 152 | memcpy(wdata->outq[wdata->head].data, buffer, count); |
| 153 | wdata->outq[wdata->head].size = count; |
| 154 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; |
| 155 | |
| 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; |
| 161 | } else { |
| 162 | hid_warn(wdata->hdev, "Output queue is full"); |
| 163 | } |
| 164 | |
| 165 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 166 | } |
| 167 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 168 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
| 169 | { |
| 170 | __u8 cmd[2]; |
| 171 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 172 | leds &= WIIPROTO_FLAGS_LEDS; |
| 173 | if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds) |
| 174 | return; |
| 175 | wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds; |
| 176 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 177 | cmd[0] = WIIPROTO_REQ_LED; |
| 178 | cmd[1] = 0; |
| 179 | |
| 180 | if (leds & WIIPROTO_FLAG_LED1) |
| 181 | cmd[1] |= 0x10; |
| 182 | if (leds & WIIPROTO_FLAG_LED2) |
| 183 | cmd[1] |= 0x20; |
| 184 | if (leds & WIIPROTO_FLAG_LED3) |
| 185 | cmd[1] |= 0x40; |
| 186 | if (leds & WIIPROTO_FLAG_LED4) |
| 187 | cmd[1] |= 0x80; |
| 188 | |
| 189 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 190 | } |
| 191 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 192 | static int wiimote_input_event(struct input_dev *dev, unsigned int type, |
| 193 | unsigned int code, int value) |
| 194 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 195 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 196 | |
| 197 | if (!atomic_read(&wdata->ready)) |
| 198 | return -EBUSY; |
| 199 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ |
| 200 | smp_rmb(); |
| 201 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 205 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
| 206 | { |
| 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); |
| 230 | } |
| 231 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 232 | struct wiiproto_handler { |
| 233 | __u8 id; |
| 234 | size_t size; |
| 235 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); |
| 236 | }; |
| 237 | |
| 238 | static struct wiiproto_handler handlers[] = { |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 239 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 240 | { .id = 0 } |
| 241 | }; |
| 242 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 243 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 244 | u8 *raw_data, int size) |
| 245 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 246 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 247 | struct wiiproto_handler *h; |
| 248 | int i; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 249 | unsigned long flags; |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 250 | |
| 251 | if (!atomic_read(&wdata->ready)) |
| 252 | return -EBUSY; |
| 253 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ |
| 254 | smp_rmb(); |
| 255 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 256 | if (size < 1) |
| 257 | return -EINVAL; |
| 258 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 259 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 260 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 261 | for (i = 0; handlers[i].id; ++i) { |
| 262 | h = &handlers[i]; |
| 263 | if (h->id == raw_data[0] && h->size < size) |
| 264 | h->func(wdata, &raw_data[1]); |
| 265 | } |
| 266 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 267 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 268 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 272 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 273 | { |
| 274 | struct wiimote_data *wdata; |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 275 | int i; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 276 | |
| 277 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 278 | if (!wdata) |
| 279 | return NULL; |
| 280 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 281 | wdata->input = input_allocate_device(); |
| 282 | if (!wdata->input) { |
| 283 | kfree(wdata); |
| 284 | return NULL; |
| 285 | } |
| 286 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 287 | wdata->hdev = hdev; |
| 288 | hid_set_drvdata(hdev, wdata); |
| 289 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 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; |
| 298 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 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); |
| 302 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 303 | spin_lock_init(&wdata->qlock); |
| 304 | INIT_WORK(&wdata->worker, wiimote_worker); |
| 305 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 306 | spin_lock_init(&wdata->state.lock); |
| 307 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 308 | return wdata; |
| 309 | } |
| 310 | |
| 311 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 312 | { |
| 313 | kfree(wdata); |
| 314 | } |
| 315 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 316 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 317 | const struct hid_device_id *id) |
| 318 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 319 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 320 | int ret; |
| 321 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 322 | wdata = wiimote_create(hdev); |
| 323 | if (!wdata) { |
| 324 | hid_err(hdev, "Can't alloc device\n"); |
| 325 | return -ENOMEM; |
| 326 | } |
| 327 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 328 | ret = hid_parse(hdev); |
| 329 | if (ret) { |
| 330 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 331 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 335 | if (ret) { |
| 336 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 337 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 338 | } |
| 339 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 340 | ret = input_register_device(wdata->input); |
| 341 | if (ret) { |
| 342 | hid_err(hdev, "Cannot register input device\n"); |
| 343 | goto err_stop; |
| 344 | } |
| 345 | |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 346 | /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */ |
| 347 | smp_wmb(); |
| 348 | atomic_set(&wdata->ready, 1); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 349 | hid_info(hdev, "New device registered\n"); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 350 | |
| 351 | /* by default set led1 after device initialization */ |
| 352 | spin_lock_irq(&wdata->state.lock); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 353 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame^] | 354 | spin_unlock_irq(&wdata->state.lock); |
| 355 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 356 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 357 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 358 | err_stop: |
| 359 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 360 | err: |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 361 | input_free_device(wdata->input); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 362 | wiimote_destroy(wdata); |
| 363 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 367 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 368 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 369 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 370 | hid_info(hdev, "Device removed\n"); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 371 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 372 | hid_hw_stop(hdev); |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 373 | input_unregister_device(wdata->input); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 374 | |
| 375 | cancel_work_sync(&wdata->worker); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 376 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 380 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 381 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
| 382 | { } |
| 383 | }; |
| 384 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 385 | |
| 386 | static struct hid_driver wiimote_hid_driver = { |
| 387 | .name = "wiimote", |
| 388 | .id_table = wiimote_hid_devices, |
| 389 | .probe = wiimote_hid_probe, |
| 390 | .remove = wiimote_hid_remove, |
| 391 | .raw_event = wiimote_hid_event, |
| 392 | }; |
| 393 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 394 | static int __init wiimote_init(void) |
| 395 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 396 | int ret; |
| 397 | |
| 398 | ret = hid_register_driver(&wiimote_hid_driver); |
| 399 | if (ret) |
| 400 | pr_err("Can't register wiimote hid driver\n"); |
| 401 | |
| 402 | return ret; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void __exit wiimote_exit(void) |
| 406 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 407 | hid_unregister_driver(&wiimote_hid_driver); |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 408 | } |
| 409 | |
| 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); |