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 | c003ec2 | 2011-09-06 13:50:26 +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 |
| 53 | #define WIIPROTO_FLAG_RUMBLE 0x10 |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 54 | #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ |
| 55 | WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 56 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 57 | /* return flag for led \num */ |
| 58 | #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1)) |
| 59 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 60 | enum wiiproto_reqs { |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 61 | WIIPROTO_REQ_NULL = 0x0, |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 62 | WIIPROTO_REQ_RUMBLE = 0x10, |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 63 | WIIPROTO_REQ_LED = 0x11, |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 64 | WIIPROTO_REQ_DRM = 0x12, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 65 | WIIPROTO_REQ_STATUS = 0x20, |
| 66 | WIIPROTO_REQ_RETURN = 0x22, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 67 | WIIPROTO_REQ_DRM_K = 0x30, |
| 68 | }; |
| 69 | |
| 70 | enum wiiproto_keys { |
| 71 | WIIPROTO_KEY_LEFT, |
| 72 | WIIPROTO_KEY_RIGHT, |
| 73 | WIIPROTO_KEY_UP, |
| 74 | WIIPROTO_KEY_DOWN, |
| 75 | WIIPROTO_KEY_PLUS, |
| 76 | WIIPROTO_KEY_MINUS, |
| 77 | WIIPROTO_KEY_ONE, |
| 78 | WIIPROTO_KEY_TWO, |
| 79 | WIIPROTO_KEY_A, |
| 80 | WIIPROTO_KEY_B, |
| 81 | WIIPROTO_KEY_HOME, |
| 82 | WIIPROTO_KEY_COUNT |
| 83 | }; |
| 84 | |
| 85 | static __u16 wiiproto_keymap[] = { |
| 86 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ |
| 87 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ |
| 88 | KEY_UP, /* WIIPROTO_KEY_UP */ |
| 89 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ |
| 90 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ |
| 91 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ |
| 92 | BTN_1, /* WIIPROTO_KEY_ONE */ |
| 93 | BTN_2, /* WIIPROTO_KEY_TWO */ |
| 94 | BTN_A, /* WIIPROTO_KEY_A */ |
| 95 | BTN_B, /* WIIPROTO_KEY_B */ |
| 96 | BTN_MODE, /* WIIPROTO_KEY_HOME */ |
| 97 | }; |
| 98 | |
David Herrmann | 0c218f1 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 99 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
| 100 | size_t count) |
| 101 | { |
| 102 | __u8 *buf; |
| 103 | ssize_t ret; |
| 104 | |
| 105 | if (!hdev->hid_output_raw_report) |
| 106 | return -ENODEV; |
| 107 | |
| 108 | buf = kmemdup(buffer, count, GFP_KERNEL); |
| 109 | if (!buf) |
| 110 | return -ENOMEM; |
| 111 | |
| 112 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); |
| 113 | |
| 114 | kfree(buf); |
| 115 | return ret; |
| 116 | } |
| 117 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 118 | static void wiimote_worker(struct work_struct *work) |
| 119 | { |
| 120 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 121 | worker); |
| 122 | unsigned long flags; |
| 123 | |
| 124 | spin_lock_irqsave(&wdata->qlock, flags); |
| 125 | |
| 126 | while (wdata->head != wdata->tail) { |
| 127 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 128 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, |
| 129 | wdata->outq[wdata->tail].size); |
| 130 | spin_lock_irqsave(&wdata->qlock, flags); |
| 131 | |
| 132 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; |
| 133 | } |
| 134 | |
| 135 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 136 | } |
| 137 | |
| 138 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, |
| 139 | size_t count) |
| 140 | { |
| 141 | unsigned long flags; |
| 142 | __u8 newhead; |
| 143 | |
| 144 | if (count > HID_MAX_BUFFER_SIZE) { |
| 145 | hid_warn(wdata->hdev, "Sending too large output report\n"); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Copy new request into our output queue and check whether the |
| 151 | * queue is full. If it is full, discard this request. |
| 152 | * If it is empty we need to start a new worker that will |
| 153 | * send out the buffer to the hid device. |
| 154 | * If the queue is not empty, then there must be a worker |
| 155 | * that is currently sending out our buffer and this worker |
| 156 | * will reschedule itself until the queue is empty. |
| 157 | */ |
| 158 | |
| 159 | spin_lock_irqsave(&wdata->qlock, flags); |
| 160 | |
| 161 | memcpy(wdata->outq[wdata->head].data, buffer, count); |
| 162 | wdata->outq[wdata->head].size = count; |
| 163 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; |
| 164 | |
| 165 | if (wdata->head == wdata->tail) { |
| 166 | wdata->head = newhead; |
| 167 | schedule_work(&wdata->worker); |
| 168 | } else if (newhead != wdata->tail) { |
| 169 | wdata->head = newhead; |
| 170 | } else { |
| 171 | hid_warn(wdata->hdev, "Output queue is full"); |
| 172 | } |
| 173 | |
| 174 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 175 | } |
| 176 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 177 | /* |
| 178 | * This sets the rumble bit on the given output report if rumble is |
| 179 | * currently enabled. |
| 180 | * \cmd1 must point to the second byte in the output report => &cmd[1] |
| 181 | * This must be called on nearly every output report before passing it |
| 182 | * into the output queue! |
| 183 | */ |
| 184 | static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1) |
| 185 | { |
| 186 | if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE) |
| 187 | *cmd1 |= 0x01; |
| 188 | } |
| 189 | |
| 190 | static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble) |
| 191 | { |
| 192 | __u8 cmd[2]; |
| 193 | |
| 194 | rumble = !!rumble; |
| 195 | if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE)) |
| 196 | return; |
| 197 | |
| 198 | if (rumble) |
| 199 | wdata->state.flags |= WIIPROTO_FLAG_RUMBLE; |
| 200 | else |
| 201 | wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE; |
| 202 | |
| 203 | cmd[0] = WIIPROTO_REQ_RUMBLE; |
| 204 | cmd[1] = 0; |
| 205 | |
| 206 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 207 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 208 | } |
| 209 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 210 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
| 211 | { |
| 212 | __u8 cmd[2]; |
| 213 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 214 | leds &= WIIPROTO_FLAGS_LEDS; |
| 215 | if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds) |
| 216 | return; |
| 217 | wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds; |
| 218 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 219 | cmd[0] = WIIPROTO_REQ_LED; |
| 220 | cmd[1] = 0; |
| 221 | |
| 222 | if (leds & WIIPROTO_FLAG_LED1) |
| 223 | cmd[1] |= 0x10; |
| 224 | if (leds & WIIPROTO_FLAG_LED2) |
| 225 | cmd[1] |= 0x20; |
| 226 | if (leds & WIIPROTO_FLAG_LED3) |
| 227 | cmd[1] |= 0x40; |
| 228 | if (leds & WIIPROTO_FLAG_LED4) |
| 229 | cmd[1] |= 0x80; |
| 230 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 231 | wiiproto_keep_rumble(wdata, &cmd[1]); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 232 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 233 | } |
| 234 | |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 235 | /* |
| 236 | * Check what peripherals of the wiimote are currently |
| 237 | * active and select a proper DRM that supports all of |
| 238 | * the requested data inputs. |
| 239 | */ |
| 240 | static __u8 select_drm(struct wiimote_data *wdata) |
| 241 | { |
| 242 | return WIIPROTO_REQ_DRM_K; |
| 243 | } |
| 244 | |
| 245 | static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) |
| 246 | { |
| 247 | __u8 cmd[3]; |
| 248 | |
| 249 | if (drm == WIIPROTO_REQ_NULL) |
| 250 | drm = select_drm(wdata); |
| 251 | |
| 252 | cmd[0] = WIIPROTO_REQ_DRM; |
| 253 | cmd[1] = 0; |
| 254 | cmd[2] = drm; |
| 255 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 256 | wiiproto_keep_rumble(wdata, &cmd[1]); |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 257 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 258 | } |
| 259 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 260 | static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev) |
| 261 | { |
| 262 | struct wiimote_data *wdata; |
| 263 | struct device *dev = led_dev->dev->parent; |
| 264 | int i; |
| 265 | unsigned long flags; |
| 266 | bool value = false; |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 267 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 268 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); |
| 269 | |
| 270 | for (i = 0; i < 4; ++i) { |
| 271 | if (wdata->leds[i] == led_dev) { |
| 272 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 273 | value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1); |
| 274 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return value ? LED_FULL : LED_OFF; |
| 280 | } |
| 281 | |
| 282 | static void wiimote_leds_set(struct led_classdev *led_dev, |
| 283 | enum led_brightness value) |
| 284 | { |
| 285 | struct wiimote_data *wdata; |
| 286 | struct device *dev = led_dev->dev->parent; |
| 287 | int i; |
| 288 | unsigned long flags; |
| 289 | __u8 state, flag; |
| 290 | |
| 291 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); |
| 292 | |
| 293 | for (i = 0; i < 4; ++i) { |
| 294 | if (wdata->leds[i] == led_dev) { |
| 295 | flag = WIIPROTO_FLAG_LED(i + 1); |
| 296 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 297 | state = wdata->state.flags; |
| 298 | if (value == LED_OFF) |
| 299 | wiiproto_req_leds(wdata, state & ~flag); |
| 300 | else |
| 301 | wiiproto_req_leds(wdata, state | flag); |
| 302 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | } |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 307 | |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame^] | 308 | static int wiimote_ff_play(struct input_dev *dev, void *data, |
| 309 | struct ff_effect *eff) |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 310 | { |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame^] | 311 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 312 | __u8 value; |
| 313 | unsigned long flags; |
| 314 | |
| 315 | /* |
| 316 | * The wiimote supports only a single rumble motor so if any magnitude |
| 317 | * is set to non-zero then we start the rumble motor. If both are set to |
| 318 | * zero, we stop the rumble motor. |
| 319 | */ |
| 320 | |
| 321 | if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) |
| 322 | value = 1; |
| 323 | else |
| 324 | value = 0; |
| 325 | |
| 326 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 327 | wiiproto_req_rumble(wdata, value); |
| 328 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 329 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
David Herrmann | 26af174 | 2011-08-17 11:43:21 +0200 | [diff] [blame] | 333 | static int wiimote_input_open(struct input_dev *dev) |
| 334 | { |
| 335 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 336 | |
| 337 | return hid_hw_open(wdata->hdev); |
| 338 | } |
| 339 | |
| 340 | static void wiimote_input_close(struct input_dev *dev) |
| 341 | { |
| 342 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 343 | |
| 344 | hid_hw_close(wdata->hdev); |
| 345 | } |
| 346 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 347 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
| 348 | { |
| 349 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], |
| 350 | !!(payload[0] & 0x01)); |
| 351 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], |
| 352 | !!(payload[0] & 0x02)); |
| 353 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], |
| 354 | !!(payload[0] & 0x04)); |
| 355 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], |
| 356 | !!(payload[0] & 0x08)); |
| 357 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], |
| 358 | !!(payload[0] & 0x10)); |
| 359 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], |
| 360 | !!(payload[1] & 0x01)); |
| 361 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], |
| 362 | !!(payload[1] & 0x02)); |
| 363 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], |
| 364 | !!(payload[1] & 0x04)); |
| 365 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], |
| 366 | !!(payload[1] & 0x08)); |
| 367 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], |
| 368 | !!(payload[1] & 0x10)); |
| 369 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], |
| 370 | !!(payload[1] & 0x80)); |
| 371 | input_sync(wdata->input); |
| 372 | } |
| 373 | |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 374 | static void handler_status(struct wiimote_data *wdata, const __u8 *payload) |
| 375 | { |
| 376 | handler_keys(wdata, payload); |
| 377 | |
| 378 | /* on status reports the drm is reset so we need to resend the drm */ |
| 379 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 380 | } |
| 381 | |
| 382 | static void handler_return(struct wiimote_data *wdata, const __u8 *payload) |
| 383 | { |
| 384 | __u8 err = payload[3]; |
| 385 | __u8 cmd = payload[2]; |
| 386 | |
| 387 | handler_keys(wdata, payload); |
| 388 | |
| 389 | if (err) |
| 390 | hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err, |
| 391 | cmd); |
| 392 | } |
| 393 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 394 | struct wiiproto_handler { |
| 395 | __u8 id; |
| 396 | size_t size; |
| 397 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); |
| 398 | }; |
| 399 | |
| 400 | static struct wiiproto_handler handlers[] = { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 401 | { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status }, |
| 402 | { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return }, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 403 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 404 | { .id = 0 } |
| 405 | }; |
| 406 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 407 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 408 | u8 *raw_data, int size) |
| 409 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 410 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 411 | struct wiiproto_handler *h; |
| 412 | int i; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 413 | unsigned long flags; |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 414 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 415 | if (size < 1) |
| 416 | return -EINVAL; |
| 417 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 418 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 419 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 420 | for (i = 0; handlers[i].id; ++i) { |
| 421 | h = &handlers[i]; |
| 422 | if (h->id == raw_data[0] && h->size < size) |
| 423 | h->func(wdata, &raw_data[1]); |
| 424 | } |
| 425 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 426 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 427 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 431 | static void wiimote_leds_destroy(struct wiimote_data *wdata) |
| 432 | { |
| 433 | int i; |
| 434 | struct led_classdev *led; |
| 435 | |
| 436 | for (i = 0; i < 4; ++i) { |
| 437 | if (wdata->leds[i]) { |
| 438 | led = wdata->leds[i]; |
| 439 | wdata->leds[i] = NULL; |
| 440 | led_classdev_unregister(led); |
| 441 | kfree(led); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | static int wiimote_leds_create(struct wiimote_data *wdata) |
| 447 | { |
| 448 | int i, ret; |
| 449 | struct device *dev = &wdata->hdev->dev; |
| 450 | size_t namesz = strlen(dev_name(dev)) + 9; |
| 451 | struct led_classdev *led; |
| 452 | char *name; |
| 453 | |
| 454 | for (i = 0; i < 4; ++i) { |
| 455 | led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL); |
| 456 | if (!led) { |
| 457 | ret = -ENOMEM; |
| 458 | goto err; |
| 459 | } |
| 460 | name = (void*)&led[1]; |
| 461 | snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i); |
| 462 | led->name = name; |
| 463 | led->brightness = 0; |
| 464 | led->max_brightness = 1; |
| 465 | led->brightness_get = wiimote_leds_get; |
| 466 | led->brightness_set = wiimote_leds_set; |
| 467 | |
| 468 | ret = led_classdev_register(dev, led); |
| 469 | if (ret) { |
| 470 | kfree(led); |
| 471 | goto err; |
| 472 | } |
| 473 | wdata->leds[i] = led; |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | |
| 478 | err: |
| 479 | wiimote_leds_destroy(wdata); |
| 480 | return ret; |
| 481 | } |
| 482 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 483 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 484 | { |
| 485 | struct wiimote_data *wdata; |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 486 | int i; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 487 | |
| 488 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 489 | if (!wdata) |
| 490 | return NULL; |
| 491 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 492 | wdata->input = input_allocate_device(); |
| 493 | if (!wdata->input) { |
| 494 | kfree(wdata); |
| 495 | return NULL; |
| 496 | } |
| 497 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 498 | wdata->hdev = hdev; |
| 499 | hid_set_drvdata(hdev, wdata); |
| 500 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 501 | input_set_drvdata(wdata->input, wdata); |
David Herrmann | 26af174 | 2011-08-17 11:43:21 +0200 | [diff] [blame] | 502 | wdata->input->open = wiimote_input_open; |
| 503 | wdata->input->close = wiimote_input_close; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 504 | wdata->input->dev.parent = &wdata->hdev->dev; |
| 505 | wdata->input->id.bustype = wdata->hdev->bus; |
| 506 | wdata->input->id.vendor = wdata->hdev->vendor; |
| 507 | wdata->input->id.product = wdata->hdev->product; |
| 508 | wdata->input->id.version = wdata->hdev->version; |
| 509 | wdata->input->name = WIIMOTE_NAME; |
| 510 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 511 | set_bit(EV_KEY, wdata->input->evbit); |
| 512 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) |
| 513 | set_bit(wiiproto_keymap[i], wdata->input->keybit); |
| 514 | |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame^] | 515 | set_bit(FF_RUMBLE, wdata->input->ffbit); |
| 516 | if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play)) { |
| 517 | input_free_device(wdata->input); |
| 518 | kfree(wdata); |
| 519 | return NULL; |
| 520 | } |
| 521 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 522 | spin_lock_init(&wdata->qlock); |
| 523 | INIT_WORK(&wdata->worker, wiimote_worker); |
| 524 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 525 | spin_lock_init(&wdata->state.lock); |
| 526 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 527 | return wdata; |
| 528 | } |
| 529 | |
| 530 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 531 | { |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 532 | wiimote_leds_destroy(wdata); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 533 | |
| 534 | input_unregister_device(wdata->input); |
| 535 | cancel_work_sync(&wdata->worker); |
| 536 | hid_hw_stop(wdata->hdev); |
| 537 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 538 | kfree(wdata); |
| 539 | } |
| 540 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 541 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 542 | const struct hid_device_id *id) |
| 543 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 544 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 545 | int ret; |
| 546 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 547 | wdata = wiimote_create(hdev); |
| 548 | if (!wdata) { |
| 549 | hid_err(hdev, "Can't alloc device\n"); |
| 550 | return -ENOMEM; |
| 551 | } |
| 552 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 553 | ret = hid_parse(hdev); |
| 554 | if (ret) { |
| 555 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 556 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 560 | if (ret) { |
| 561 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 562 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 563 | } |
| 564 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 565 | ret = input_register_device(wdata->input); |
| 566 | if (ret) { |
| 567 | hid_err(hdev, "Cannot register input device\n"); |
| 568 | goto err_stop; |
| 569 | } |
| 570 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 571 | ret = wiimote_leds_create(wdata); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 572 | if (ret) |
| 573 | goto err_free; |
| 574 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 575 | hid_info(hdev, "New device registered\n"); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 576 | |
| 577 | /* by default set led1 after device initialization */ |
| 578 | spin_lock_irq(&wdata->state.lock); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 579 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 580 | spin_unlock_irq(&wdata->state.lock); |
| 581 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 582 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 583 | |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 584 | err_free: |
| 585 | wiimote_destroy(wdata); |
| 586 | return ret; |
| 587 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 588 | err_stop: |
| 589 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 590 | err: |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 591 | input_free_device(wdata->input); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 592 | kfree(wdata); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 593 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 597 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 598 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 599 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 600 | hid_info(hdev, "Device removed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 601 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 605 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 606 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
| 607 | { } |
| 608 | }; |
| 609 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 610 | |
| 611 | static struct hid_driver wiimote_hid_driver = { |
| 612 | .name = "wiimote", |
| 613 | .id_table = wiimote_hid_devices, |
| 614 | .probe = wiimote_hid_probe, |
| 615 | .remove = wiimote_hid_remove, |
| 616 | .raw_event = wiimote_hid_event, |
| 617 | }; |
| 618 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 619 | static int __init wiimote_init(void) |
| 620 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 621 | int ret; |
| 622 | |
| 623 | ret = hid_register_driver(&wiimote_hid_driver); |
| 624 | if (ret) |
| 625 | pr_err("Can't register wiimote hid driver\n"); |
| 626 | |
| 627 | return ret; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static void __exit wiimote_exit(void) |
| 631 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 632 | hid_unregister_driver(&wiimote_hid_driver); |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | module_init(wiimote_init); |
| 636 | module_exit(wiimote_exit); |
| 637 | MODULE_LICENSE("GPL"); |
| 638 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
| 639 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); |
| 640 | MODULE_VERSION(WIIMOTE_VERSION); |