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 | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 13 | #include <linux/device.h> |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 14 | #include <linux/hid.h> |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 15 | #include <linux/input.h> |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 16 | #include <linux/leds.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 { |
| 36 | struct hid_device *hdev; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 37 | struct input_dev *input; |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 38 | struct led_classdev *leds[4]; |
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 | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 56 | /* return flag for led \num */ |
| 57 | #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1)) |
| 58 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 59 | enum wiiproto_reqs { |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 60 | WIIPROTO_REQ_NULL = 0x0, |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 61 | WIIPROTO_REQ_LED = 0x11, |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 62 | WIIPROTO_REQ_DRM = 0x12, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 63 | WIIPROTO_REQ_STATUS = 0x20, |
| 64 | WIIPROTO_REQ_RETURN = 0x22, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 65 | WIIPROTO_REQ_DRM_K = 0x30, |
| 66 | }; |
| 67 | |
| 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 |
| 81 | }; |
| 82 | |
| 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 */ |
| 95 | }; |
| 96 | |
David Herrmann | 0c218f1 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 97 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
| 98 | size_t count) |
| 99 | { |
| 100 | __u8 *buf; |
| 101 | ssize_t ret; |
| 102 | |
| 103 | if (!hdev->hid_output_raw_report) |
| 104 | return -ENODEV; |
| 105 | |
| 106 | buf = kmemdup(buffer, count, GFP_KERNEL); |
| 107 | if (!buf) |
| 108 | return -ENOMEM; |
| 109 | |
| 110 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); |
| 111 | |
| 112 | kfree(buf); |
| 113 | return ret; |
| 114 | } |
| 115 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 116 | static void wiimote_worker(struct work_struct *work) |
| 117 | { |
| 118 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 119 | worker); |
| 120 | unsigned long flags; |
| 121 | |
| 122 | spin_lock_irqsave(&wdata->qlock, flags); |
| 123 | |
| 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); |
| 129 | |
| 130 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; |
| 131 | } |
| 132 | |
| 133 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 134 | } |
| 135 | |
| 136 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, |
| 137 | size_t count) |
| 138 | { |
| 139 | unsigned long flags; |
| 140 | __u8 newhead; |
| 141 | |
| 142 | if (count > HID_MAX_BUFFER_SIZE) { |
| 143 | hid_warn(wdata->hdev, "Sending too large output report\n"); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | /* |
| 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. |
| 155 | */ |
| 156 | |
| 157 | spin_lock_irqsave(&wdata->qlock, flags); |
| 158 | |
| 159 | memcpy(wdata->outq[wdata->head].data, buffer, count); |
| 160 | wdata->outq[wdata->head].size = count; |
| 161 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; |
| 162 | |
| 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"); |
| 170 | } |
| 171 | |
| 172 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 173 | } |
| 174 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 175 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
| 176 | { |
| 177 | __u8 cmd[2]; |
| 178 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 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; |
| 183 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 184 | cmd[0] = WIIPROTO_REQ_LED; |
| 185 | cmd[1] = 0; |
| 186 | |
| 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; |
| 195 | |
| 196 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 197 | } |
| 198 | |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 199 | /* |
| 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. |
| 203 | */ |
| 204 | static __u8 select_drm(struct wiimote_data *wdata) |
| 205 | { |
| 206 | return WIIPROTO_REQ_DRM_K; |
| 207 | } |
| 208 | |
| 209 | static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) |
| 210 | { |
| 211 | __u8 cmd[3]; |
| 212 | |
| 213 | if (drm == WIIPROTO_REQ_NULL) |
| 214 | drm = select_drm(wdata); |
| 215 | |
| 216 | cmd[0] = WIIPROTO_REQ_DRM; |
| 217 | cmd[1] = 0; |
| 218 | cmd[2] = drm; |
| 219 | |
| 220 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 221 | } |
| 222 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 223 | static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev) |
| 224 | { |
| 225 | struct wiimote_data *wdata; |
| 226 | struct device *dev = led_dev->dev->parent; |
| 227 | int i; |
| 228 | unsigned long flags; |
| 229 | bool value = false; |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 230 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 231 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); |
| 232 | |
| 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; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return value ? LED_FULL : LED_OFF; |
| 243 | } |
| 244 | |
| 245 | static void wiimote_leds_set(struct led_classdev *led_dev, |
| 246 | enum led_brightness value) |
| 247 | { |
| 248 | struct wiimote_data *wdata; |
| 249 | struct device *dev = led_dev->dev->parent; |
| 250 | int i; |
| 251 | unsigned long flags; |
| 252 | __u8 state, flag; |
| 253 | |
| 254 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); |
| 255 | |
| 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; |
| 267 | } |
| 268 | } |
| 269 | } |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 270 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 271 | static int wiimote_input_event(struct input_dev *dev, unsigned int type, |
| 272 | unsigned int code, int value) |
| 273 | { |
| 274 | return 0; |
| 275 | } |
| 276 | |
David Herrmann | 26af174 | 2011-08-17 11:43:21 +0200 | [diff] [blame] | 277 | static int wiimote_input_open(struct input_dev *dev) |
| 278 | { |
| 279 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 280 | |
| 281 | return hid_hw_open(wdata->hdev); |
| 282 | } |
| 283 | |
| 284 | static void wiimote_input_close(struct input_dev *dev) |
| 285 | { |
| 286 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 287 | |
| 288 | hid_hw_close(wdata->hdev); |
| 289 | } |
| 290 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 291 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
| 292 | { |
| 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); |
| 316 | } |
| 317 | |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 318 | static void handler_status(struct wiimote_data *wdata, const __u8 *payload) |
| 319 | { |
| 320 | handler_keys(wdata, payload); |
| 321 | |
| 322 | /* on status reports the drm is reset so we need to resend the drm */ |
| 323 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 324 | } |
| 325 | |
| 326 | static void handler_return(struct wiimote_data *wdata, const __u8 *payload) |
| 327 | { |
| 328 | __u8 err = payload[3]; |
| 329 | __u8 cmd = payload[2]; |
| 330 | |
| 331 | handler_keys(wdata, payload); |
| 332 | |
| 333 | if (err) |
| 334 | hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err, |
| 335 | cmd); |
| 336 | } |
| 337 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 338 | struct wiiproto_handler { |
| 339 | __u8 id; |
| 340 | size_t size; |
| 341 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); |
| 342 | }; |
| 343 | |
| 344 | static struct wiiproto_handler handlers[] = { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 345 | { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status }, |
| 346 | { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return }, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 347 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 348 | { .id = 0 } |
| 349 | }; |
| 350 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 351 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 352 | u8 *raw_data, int size) |
| 353 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 354 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 355 | struct wiiproto_handler *h; |
| 356 | int i; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 357 | unsigned long flags; |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 358 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 359 | if (size < 1) |
| 360 | return -EINVAL; |
| 361 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 362 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 363 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 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]); |
| 368 | } |
| 369 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 370 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 371 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 375 | static void wiimote_leds_destroy(struct wiimote_data *wdata) |
| 376 | { |
| 377 | int i; |
| 378 | struct led_classdev *led; |
| 379 | |
| 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); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | static int wiimote_leds_create(struct wiimote_data *wdata) |
| 391 | { |
| 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; |
| 397 | |
| 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; |
| 403 | } |
| 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; |
| 411 | |
| 412 | ret = led_classdev_register(dev, led); |
| 413 | if (ret) { |
| 414 | kfree(led); |
| 415 | goto err; |
| 416 | } |
| 417 | wdata->leds[i] = led; |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | |
| 422 | err: |
| 423 | wiimote_leds_destroy(wdata); |
| 424 | return ret; |
| 425 | } |
| 426 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 427 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 428 | { |
| 429 | struct wiimote_data *wdata; |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 430 | int i; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 431 | |
| 432 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 433 | if (!wdata) |
| 434 | return NULL; |
| 435 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 436 | wdata->input = input_allocate_device(); |
| 437 | if (!wdata->input) { |
| 438 | kfree(wdata); |
| 439 | return NULL; |
| 440 | } |
| 441 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 442 | wdata->hdev = hdev; |
| 443 | hid_set_drvdata(hdev, wdata); |
| 444 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 445 | input_set_drvdata(wdata->input, wdata); |
| 446 | wdata->input->event = wiimote_input_event; |
David Herrmann | 26af174 | 2011-08-17 11:43:21 +0200 | [diff] [blame] | 447 | wdata->input->open = wiimote_input_open; |
| 448 | wdata->input->close = wiimote_input_close; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 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; |
| 455 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 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); |
| 459 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 460 | spin_lock_init(&wdata->qlock); |
| 461 | INIT_WORK(&wdata->worker, wiimote_worker); |
| 462 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 463 | spin_lock_init(&wdata->state.lock); |
| 464 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 465 | return wdata; |
| 466 | } |
| 467 | |
| 468 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 469 | { |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 470 | wiimote_leds_destroy(wdata); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 471 | |
| 472 | input_unregister_device(wdata->input); |
| 473 | cancel_work_sync(&wdata->worker); |
| 474 | hid_hw_stop(wdata->hdev); |
| 475 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 476 | kfree(wdata); |
| 477 | } |
| 478 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 479 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 480 | const struct hid_device_id *id) |
| 481 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 482 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 483 | int ret; |
| 484 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 485 | wdata = wiimote_create(hdev); |
| 486 | if (!wdata) { |
| 487 | hid_err(hdev, "Can't alloc device\n"); |
| 488 | return -ENOMEM; |
| 489 | } |
| 490 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 491 | ret = hid_parse(hdev); |
| 492 | if (ret) { |
| 493 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 494 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 498 | if (ret) { |
| 499 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 500 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 501 | } |
| 502 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 503 | ret = input_register_device(wdata->input); |
| 504 | if (ret) { |
| 505 | hid_err(hdev, "Cannot register input device\n"); |
| 506 | goto err_stop; |
| 507 | } |
| 508 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 509 | ret = wiimote_leds_create(wdata); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 510 | if (ret) |
| 511 | goto err_free; |
| 512 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 513 | hid_info(hdev, "New device registered\n"); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 514 | |
| 515 | /* by default set led1 after device initialization */ |
| 516 | spin_lock_irq(&wdata->state.lock); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 517 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 518 | spin_unlock_irq(&wdata->state.lock); |
| 519 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 520 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 521 | |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 522 | err_free: |
| 523 | wiimote_destroy(wdata); |
| 524 | return ret; |
| 525 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 526 | err_stop: |
| 527 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 528 | err: |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 529 | input_free_device(wdata->input); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 530 | kfree(wdata); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 531 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 535 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 536 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 537 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 538 | hid_info(hdev, "Device removed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 539 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 543 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 544 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
| 545 | { } |
| 546 | }; |
| 547 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 548 | |
| 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, |
| 555 | }; |
| 556 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 557 | static int __init wiimote_init(void) |
| 558 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 559 | int ret; |
| 560 | |
| 561 | ret = hid_register_driver(&wiimote_hid_driver); |
| 562 | if (ret) |
| 563 | pr_err("Can't register wiimote hid driver\n"); |
| 564 | |
| 565 | return ret; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static void __exit wiimote_exit(void) |
| 569 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 570 | hid_unregister_driver(&wiimote_hid_driver); |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 571 | } |
| 572 | |
| 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); |