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 | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 13 | #include <linux/completion.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 | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 17 | #include <linux/leds.h> |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 18 | #include <linux/module.h> |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 19 | #include <linux/mutex.h> |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 20 | #include <linux/spinlock.h> |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 21 | #include "hid-ids.h" |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 22 | |
| 23 | #define WIIMOTE_VERSION "0.1" |
| 24 | #define WIIMOTE_NAME "Nintendo Wii Remote" |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 25 | #define WIIMOTE_BUFSIZE 32 |
| 26 | |
| 27 | struct wiimote_buf { |
| 28 | __u8 data[HID_MAX_BUFFER_SIZE]; |
| 29 | size_t size; |
| 30 | }; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 31 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 32 | struct wiimote_state { |
| 33 | spinlock_t lock; |
| 34 | __u8 flags; |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 35 | __u8 accel_split[2]; |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 36 | |
| 37 | /* synchronous cmd requests */ |
| 38 | struct mutex sync; |
| 39 | struct completion ready; |
| 40 | int cmd; |
| 41 | __u32 opt; |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 42 | |
| 43 | /* results of synchronous requests */ |
| 44 | __u8 cmd_err; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 47 | struct wiimote_data { |
| 48 | struct hid_device *hdev; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 49 | struct input_dev *input; |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 50 | struct led_classdev *leds[4]; |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 51 | struct input_dev *accel; |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 52 | struct input_dev *ir; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 53 | |
| 54 | spinlock_t qlock; |
| 55 | __u8 head; |
| 56 | __u8 tail; |
| 57 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; |
| 58 | struct work_struct worker; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 59 | |
| 60 | struct wiimote_state state; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 63 | #define WIIPROTO_FLAG_LED1 0x01 |
| 64 | #define WIIPROTO_FLAG_LED2 0x02 |
| 65 | #define WIIPROTO_FLAG_LED3 0x04 |
| 66 | #define WIIPROTO_FLAG_LED4 0x08 |
| 67 | #define WIIPROTO_FLAG_RUMBLE 0x10 |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 68 | #define WIIPROTO_FLAG_ACCEL 0x20 |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 69 | #define WIIPROTO_FLAG_IR_BASIC 0x40 |
| 70 | #define WIIPROTO_FLAG_IR_EXT 0x80 |
| 71 | #define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */ |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 72 | #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ |
| 73 | WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 74 | #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \ |
| 75 | WIIPROTO_FLAG_IR_FULL) |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 76 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 77 | /* return flag for led \num */ |
| 78 | #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1)) |
| 79 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 80 | enum wiiproto_reqs { |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 81 | WIIPROTO_REQ_NULL = 0x0, |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 82 | WIIPROTO_REQ_RUMBLE = 0x10, |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 83 | WIIPROTO_REQ_LED = 0x11, |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 84 | WIIPROTO_REQ_DRM = 0x12, |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame^] | 85 | WIIPROTO_REQ_IR1 = 0x13, |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 86 | WIIPROTO_REQ_WMEM = 0x16, |
| 87 | WIIPROTO_REQ_RMEM = 0x17, |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame^] | 88 | WIIPROTO_REQ_IR2 = 0x1a, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 89 | WIIPROTO_REQ_STATUS = 0x20, |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 90 | WIIPROTO_REQ_DATA = 0x21, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 91 | WIIPROTO_REQ_RETURN = 0x22, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 92 | WIIPROTO_REQ_DRM_K = 0x30, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 93 | WIIPROTO_REQ_DRM_KA = 0x31, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 94 | WIIPROTO_REQ_DRM_KE = 0x32, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 95 | WIIPROTO_REQ_DRM_KAI = 0x33, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 96 | WIIPROTO_REQ_DRM_KEE = 0x34, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 97 | WIIPROTO_REQ_DRM_KAE = 0x35, |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 98 | WIIPROTO_REQ_DRM_KIE = 0x36, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 99 | WIIPROTO_REQ_DRM_KAIE = 0x37, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 100 | WIIPROTO_REQ_DRM_E = 0x3d, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 101 | WIIPROTO_REQ_DRM_SKAI1 = 0x3e, |
| 102 | WIIPROTO_REQ_DRM_SKAI2 = 0x3f, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | enum wiiproto_keys { |
| 106 | WIIPROTO_KEY_LEFT, |
| 107 | WIIPROTO_KEY_RIGHT, |
| 108 | WIIPROTO_KEY_UP, |
| 109 | WIIPROTO_KEY_DOWN, |
| 110 | WIIPROTO_KEY_PLUS, |
| 111 | WIIPROTO_KEY_MINUS, |
| 112 | WIIPROTO_KEY_ONE, |
| 113 | WIIPROTO_KEY_TWO, |
| 114 | WIIPROTO_KEY_A, |
| 115 | WIIPROTO_KEY_B, |
| 116 | WIIPROTO_KEY_HOME, |
| 117 | WIIPROTO_KEY_COUNT |
| 118 | }; |
| 119 | |
| 120 | static __u16 wiiproto_keymap[] = { |
| 121 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ |
| 122 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ |
| 123 | KEY_UP, /* WIIPROTO_KEY_UP */ |
| 124 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ |
| 125 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ |
| 126 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ |
| 127 | BTN_1, /* WIIPROTO_KEY_ONE */ |
| 128 | BTN_2, /* WIIPROTO_KEY_TWO */ |
| 129 | BTN_A, /* WIIPROTO_KEY_A */ |
| 130 | BTN_B, /* WIIPROTO_KEY_B */ |
| 131 | BTN_MODE, /* WIIPROTO_KEY_HOME */ |
| 132 | }; |
| 133 | |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 134 | /* requires the state.lock spinlock to be held */ |
| 135 | static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd, |
| 136 | __u32 opt) |
| 137 | { |
| 138 | return wdata->state.cmd == cmd && wdata->state.opt == opt; |
| 139 | } |
| 140 | |
| 141 | /* requires the state.lock spinlock to be held */ |
| 142 | static inline void wiimote_cmd_complete(struct wiimote_data *wdata) |
| 143 | { |
| 144 | wdata->state.cmd = WIIPROTO_REQ_NULL; |
| 145 | complete(&wdata->state.ready); |
| 146 | } |
| 147 | |
| 148 | static inline int wiimote_cmd_acquire(struct wiimote_data *wdata) |
| 149 | { |
| 150 | return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0; |
| 151 | } |
| 152 | |
| 153 | /* requires the state.lock spinlock to be held */ |
| 154 | static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd, |
| 155 | __u32 opt) |
| 156 | { |
| 157 | INIT_COMPLETION(wdata->state.ready); |
| 158 | wdata->state.cmd = cmd; |
| 159 | wdata->state.opt = opt; |
| 160 | } |
| 161 | |
| 162 | static inline void wiimote_cmd_release(struct wiimote_data *wdata) |
| 163 | { |
| 164 | mutex_unlock(&wdata->state.sync); |
| 165 | } |
| 166 | |
| 167 | static inline int wiimote_cmd_wait(struct wiimote_data *wdata) |
| 168 | { |
| 169 | int ret; |
| 170 | |
| 171 | ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ); |
| 172 | if (ret < 0) |
| 173 | return -ERESTARTSYS; |
| 174 | else if (ret == 0) |
| 175 | return -EIO; |
| 176 | else |
| 177 | return 0; |
| 178 | } |
| 179 | |
David Herrmann | 0c218f1 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 180 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
| 181 | size_t count) |
| 182 | { |
| 183 | __u8 *buf; |
| 184 | ssize_t ret; |
| 185 | |
| 186 | if (!hdev->hid_output_raw_report) |
| 187 | return -ENODEV; |
| 188 | |
| 189 | buf = kmemdup(buffer, count, GFP_KERNEL); |
| 190 | if (!buf) |
| 191 | return -ENOMEM; |
| 192 | |
| 193 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); |
| 194 | |
| 195 | kfree(buf); |
| 196 | return ret; |
| 197 | } |
| 198 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 199 | static void wiimote_worker(struct work_struct *work) |
| 200 | { |
| 201 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 202 | worker); |
| 203 | unsigned long flags; |
| 204 | |
| 205 | spin_lock_irqsave(&wdata->qlock, flags); |
| 206 | |
| 207 | while (wdata->head != wdata->tail) { |
| 208 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 209 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, |
| 210 | wdata->outq[wdata->tail].size); |
| 211 | spin_lock_irqsave(&wdata->qlock, flags); |
| 212 | |
| 213 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; |
| 214 | } |
| 215 | |
| 216 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 217 | } |
| 218 | |
| 219 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, |
| 220 | size_t count) |
| 221 | { |
| 222 | unsigned long flags; |
| 223 | __u8 newhead; |
| 224 | |
| 225 | if (count > HID_MAX_BUFFER_SIZE) { |
| 226 | hid_warn(wdata->hdev, "Sending too large output report\n"); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Copy new request into our output queue and check whether the |
| 232 | * queue is full. If it is full, discard this request. |
| 233 | * If it is empty we need to start a new worker that will |
| 234 | * send out the buffer to the hid device. |
| 235 | * If the queue is not empty, then there must be a worker |
| 236 | * that is currently sending out our buffer and this worker |
| 237 | * will reschedule itself until the queue is empty. |
| 238 | */ |
| 239 | |
| 240 | spin_lock_irqsave(&wdata->qlock, flags); |
| 241 | |
| 242 | memcpy(wdata->outq[wdata->head].data, buffer, count); |
| 243 | wdata->outq[wdata->head].size = count; |
| 244 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; |
| 245 | |
| 246 | if (wdata->head == wdata->tail) { |
| 247 | wdata->head = newhead; |
| 248 | schedule_work(&wdata->worker); |
| 249 | } else if (newhead != wdata->tail) { |
| 250 | wdata->head = newhead; |
| 251 | } else { |
| 252 | hid_warn(wdata->hdev, "Output queue is full"); |
| 253 | } |
| 254 | |
| 255 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 256 | } |
| 257 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 258 | /* |
| 259 | * This sets the rumble bit on the given output report if rumble is |
| 260 | * currently enabled. |
| 261 | * \cmd1 must point to the second byte in the output report => &cmd[1] |
| 262 | * This must be called on nearly every output report before passing it |
| 263 | * into the output queue! |
| 264 | */ |
| 265 | static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1) |
| 266 | { |
| 267 | if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE) |
| 268 | *cmd1 |= 0x01; |
| 269 | } |
| 270 | |
| 271 | static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble) |
| 272 | { |
| 273 | __u8 cmd[2]; |
| 274 | |
| 275 | rumble = !!rumble; |
| 276 | if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE)) |
| 277 | return; |
| 278 | |
| 279 | if (rumble) |
| 280 | wdata->state.flags |= WIIPROTO_FLAG_RUMBLE; |
| 281 | else |
| 282 | wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE; |
| 283 | |
| 284 | cmd[0] = WIIPROTO_REQ_RUMBLE; |
| 285 | cmd[1] = 0; |
| 286 | |
| 287 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 288 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 289 | } |
| 290 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 291 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
| 292 | { |
| 293 | __u8 cmd[2]; |
| 294 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 295 | leds &= WIIPROTO_FLAGS_LEDS; |
| 296 | if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds) |
| 297 | return; |
| 298 | wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds; |
| 299 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 300 | cmd[0] = WIIPROTO_REQ_LED; |
| 301 | cmd[1] = 0; |
| 302 | |
| 303 | if (leds & WIIPROTO_FLAG_LED1) |
| 304 | cmd[1] |= 0x10; |
| 305 | if (leds & WIIPROTO_FLAG_LED2) |
| 306 | cmd[1] |= 0x20; |
| 307 | if (leds & WIIPROTO_FLAG_LED3) |
| 308 | cmd[1] |= 0x40; |
| 309 | if (leds & WIIPROTO_FLAG_LED4) |
| 310 | cmd[1] |= 0x80; |
| 311 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 312 | wiiproto_keep_rumble(wdata, &cmd[1]); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 313 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 314 | } |
| 315 | |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 316 | /* |
| 317 | * Check what peripherals of the wiimote are currently |
| 318 | * active and select a proper DRM that supports all of |
| 319 | * the requested data inputs. |
| 320 | */ |
| 321 | static __u8 select_drm(struct wiimote_data *wdata) |
| 322 | { |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 323 | __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR; |
| 324 | |
| 325 | if (ir == WIIPROTO_FLAG_IR_BASIC) { |
| 326 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) |
| 327 | return WIIPROTO_REQ_DRM_KAIE; |
| 328 | else |
| 329 | return WIIPROTO_REQ_DRM_KIE; |
| 330 | } else if (ir == WIIPROTO_FLAG_IR_EXT) { |
| 331 | return WIIPROTO_REQ_DRM_KAI; |
| 332 | } else if (ir == WIIPROTO_FLAG_IR_FULL) { |
| 333 | return WIIPROTO_REQ_DRM_SKAI1; |
| 334 | } else { |
| 335 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) |
| 336 | return WIIPROTO_REQ_DRM_KA; |
| 337 | else |
| 338 | return WIIPROTO_REQ_DRM_K; |
| 339 | } |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) |
| 343 | { |
| 344 | __u8 cmd[3]; |
| 345 | |
| 346 | if (drm == WIIPROTO_REQ_NULL) |
| 347 | drm = select_drm(wdata); |
| 348 | |
| 349 | cmd[0] = WIIPROTO_REQ_DRM; |
| 350 | cmd[1] = 0; |
| 351 | cmd[2] = drm; |
| 352 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 353 | wiiproto_keep_rumble(wdata, &cmd[1]); |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 354 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 355 | } |
| 356 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 357 | static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel) |
| 358 | { |
| 359 | accel = !!accel; |
| 360 | if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) |
| 361 | return; |
| 362 | |
| 363 | if (accel) |
| 364 | wdata->state.flags |= WIIPROTO_FLAG_ACCEL; |
| 365 | else |
| 366 | wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL; |
| 367 | |
| 368 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 369 | } |
| 370 | |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame^] | 371 | static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags) |
| 372 | { |
| 373 | __u8 cmd[2]; |
| 374 | |
| 375 | cmd[0] = WIIPROTO_REQ_IR1; |
| 376 | cmd[1] = flags; |
| 377 | |
| 378 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 379 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 380 | } |
| 381 | |
| 382 | static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags) |
| 383 | { |
| 384 | __u8 cmd[2]; |
| 385 | |
| 386 | cmd[0] = WIIPROTO_REQ_IR2; |
| 387 | cmd[1] = flags; |
| 388 | |
| 389 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 390 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 391 | } |
| 392 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 393 | #define wiiproto_req_wreg(wdata, os, buf, sz) \ |
| 394 | wiiproto_req_wmem((wdata), false, (os), (buf), (sz)) |
| 395 | |
| 396 | #define wiiproto_req_weeprom(wdata, os, buf, sz) \ |
| 397 | wiiproto_req_wmem((wdata), true, (os), (buf), (sz)) |
| 398 | |
| 399 | static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom, |
| 400 | __u32 offset, const __u8 *buf, __u8 size) |
| 401 | { |
| 402 | __u8 cmd[22]; |
| 403 | |
| 404 | if (size > 16 || size == 0) { |
| 405 | hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | memset(cmd, 0, sizeof(cmd)); |
| 410 | cmd[0] = WIIPROTO_REQ_WMEM; |
| 411 | cmd[2] = (offset >> 16) & 0xff; |
| 412 | cmd[3] = (offset >> 8) & 0xff; |
| 413 | cmd[4] = offset & 0xff; |
| 414 | cmd[5] = size; |
| 415 | memcpy(&cmd[6], buf, size); |
| 416 | |
| 417 | if (!eeprom) |
| 418 | cmd[1] |= 0x04; |
| 419 | |
| 420 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 421 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 422 | } |
| 423 | |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 424 | /* requries the cmd-mutex to be held */ |
| 425 | static int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, |
| 426 | const __u8 *wmem, __u8 size) |
| 427 | { |
| 428 | unsigned long flags; |
| 429 | int ret; |
| 430 | |
| 431 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 432 | wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0); |
| 433 | wiiproto_req_wreg(wdata, offset, wmem, size); |
| 434 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 435 | |
| 436 | ret = wiimote_cmd_wait(wdata); |
| 437 | if (!ret && wdata->state.cmd_err) |
| 438 | ret = -EIO; |
| 439 | |
| 440 | return ret; |
| 441 | } |
| 442 | |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame^] | 443 | static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode) |
| 444 | { |
| 445 | int ret; |
| 446 | unsigned long flags; |
| 447 | __u8 format = 0; |
| 448 | static const __u8 data_enable[] = { 0x01 }; |
| 449 | static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01, |
| 450 | 0x00, 0xaa, 0x00, 0x64 }; |
| 451 | static const __u8 data_sens2[] = { 0x63, 0x03 }; |
| 452 | static const __u8 data_fin[] = { 0x08 }; |
| 453 | |
| 454 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 455 | |
| 456 | if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) { |
| 457 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | if (mode == 0) { |
| 462 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; |
| 463 | wiiproto_req_ir1(wdata, 0); |
| 464 | wiiproto_req_ir2(wdata, 0); |
| 465 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 466 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 471 | |
| 472 | ret = wiimote_cmd_acquire(wdata); |
| 473 | if (ret) |
| 474 | return ret; |
| 475 | |
| 476 | /* send PIXEL CLOCK ENABLE cmd first */ |
| 477 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 478 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0); |
| 479 | wiiproto_req_ir1(wdata, 0x06); |
| 480 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 481 | |
| 482 | ret = wiimote_cmd_wait(wdata); |
| 483 | if (ret) |
| 484 | goto unlock; |
| 485 | if (wdata->state.cmd_err) { |
| 486 | ret = -EIO; |
| 487 | goto unlock; |
| 488 | } |
| 489 | |
| 490 | /* enable IR LOGIC */ |
| 491 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 492 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0); |
| 493 | wiiproto_req_ir2(wdata, 0x06); |
| 494 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 495 | |
| 496 | ret = wiimote_cmd_wait(wdata); |
| 497 | if (ret) |
| 498 | goto unlock; |
| 499 | if (wdata->state.cmd_err) { |
| 500 | ret = -EIO; |
| 501 | goto unlock; |
| 502 | } |
| 503 | |
| 504 | /* enable IR cam but do not make it send data, yet */ |
| 505 | ret = wiimote_cmd_write(wdata, 0xb00030, data_enable, |
| 506 | sizeof(data_enable)); |
| 507 | if (ret) |
| 508 | goto unlock; |
| 509 | |
| 510 | /* write first sensitivity block */ |
| 511 | ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1, |
| 512 | sizeof(data_sens1)); |
| 513 | if (ret) |
| 514 | goto unlock; |
| 515 | |
| 516 | /* write second sensitivity block */ |
| 517 | ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2, |
| 518 | sizeof(data_sens2)); |
| 519 | if (ret) |
| 520 | goto unlock; |
| 521 | |
| 522 | /* put IR cam into desired state */ |
| 523 | switch (mode) { |
| 524 | case WIIPROTO_FLAG_IR_FULL: |
| 525 | format = 5; |
| 526 | break; |
| 527 | case WIIPROTO_FLAG_IR_EXT: |
| 528 | format = 3; |
| 529 | break; |
| 530 | case WIIPROTO_FLAG_IR_BASIC: |
| 531 | format = 1; |
| 532 | break; |
| 533 | } |
| 534 | ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format)); |
| 535 | if (ret) |
| 536 | goto unlock; |
| 537 | |
| 538 | /* make IR cam send data */ |
| 539 | ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin)); |
| 540 | if (ret) |
| 541 | goto unlock; |
| 542 | |
| 543 | /* request new DRM mode compatible to IR mode */ |
| 544 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 545 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; |
| 546 | wdata->state.flags |= mode & WIIPROTO_FLAGS_IR; |
| 547 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 548 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 549 | |
| 550 | unlock: |
| 551 | wiimote_cmd_release(wdata); |
| 552 | return ret; |
| 553 | } |
| 554 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 555 | static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev) |
| 556 | { |
| 557 | struct wiimote_data *wdata; |
| 558 | struct device *dev = led_dev->dev->parent; |
| 559 | int i; |
| 560 | unsigned long flags; |
| 561 | bool value = false; |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 562 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 563 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); |
| 564 | |
| 565 | for (i = 0; i < 4; ++i) { |
| 566 | if (wdata->leds[i] == led_dev) { |
| 567 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 568 | value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1); |
| 569 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return value ? LED_FULL : LED_OFF; |
| 575 | } |
| 576 | |
| 577 | static void wiimote_leds_set(struct led_classdev *led_dev, |
| 578 | enum led_brightness value) |
| 579 | { |
| 580 | struct wiimote_data *wdata; |
| 581 | struct device *dev = led_dev->dev->parent; |
| 582 | int i; |
| 583 | unsigned long flags; |
| 584 | __u8 state, flag; |
| 585 | |
| 586 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); |
| 587 | |
| 588 | for (i = 0; i < 4; ++i) { |
| 589 | if (wdata->leds[i] == led_dev) { |
| 590 | flag = WIIPROTO_FLAG_LED(i + 1); |
| 591 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 592 | state = wdata->state.flags; |
| 593 | if (value == LED_OFF) |
| 594 | wiiproto_req_leds(wdata, state & ~flag); |
| 595 | else |
| 596 | wiiproto_req_leds(wdata, state | flag); |
| 597 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | } |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 602 | |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame] | 603 | static int wiimote_ff_play(struct input_dev *dev, void *data, |
| 604 | struct ff_effect *eff) |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 605 | { |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame] | 606 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 607 | __u8 value; |
| 608 | unsigned long flags; |
| 609 | |
| 610 | /* |
| 611 | * The wiimote supports only a single rumble motor so if any magnitude |
| 612 | * is set to non-zero then we start the rumble motor. If both are set to |
| 613 | * zero, we stop the rumble motor. |
| 614 | */ |
| 615 | |
| 616 | if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) |
| 617 | value = 1; |
| 618 | else |
| 619 | value = 0; |
| 620 | |
| 621 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 622 | wiiproto_req_rumble(wdata, value); |
| 623 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 624 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 625 | return 0; |
| 626 | } |
| 627 | |
David Herrmann | 26af174 | 2011-08-17 11:43:21 +0200 | [diff] [blame] | 628 | static int wiimote_input_open(struct input_dev *dev) |
| 629 | { |
| 630 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 631 | |
| 632 | return hid_hw_open(wdata->hdev); |
| 633 | } |
| 634 | |
| 635 | static void wiimote_input_close(struct input_dev *dev) |
| 636 | { |
| 637 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 638 | |
| 639 | hid_hw_close(wdata->hdev); |
| 640 | } |
| 641 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 642 | static int wiimote_accel_open(struct input_dev *dev) |
| 643 | { |
| 644 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 645 | int ret; |
| 646 | unsigned long flags; |
| 647 | |
| 648 | ret = hid_hw_open(wdata->hdev); |
| 649 | if (ret) |
| 650 | return ret; |
| 651 | |
| 652 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 653 | wiiproto_req_accel(wdata, true); |
| 654 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static void wiimote_accel_close(struct input_dev *dev) |
| 660 | { |
| 661 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 662 | unsigned long flags; |
| 663 | |
| 664 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 665 | wiiproto_req_accel(wdata, false); |
| 666 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 667 | |
| 668 | hid_hw_close(wdata->hdev); |
| 669 | } |
| 670 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 671 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
| 672 | { |
| 673 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], |
| 674 | !!(payload[0] & 0x01)); |
| 675 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], |
| 676 | !!(payload[0] & 0x02)); |
| 677 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], |
| 678 | !!(payload[0] & 0x04)); |
| 679 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], |
| 680 | !!(payload[0] & 0x08)); |
| 681 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], |
| 682 | !!(payload[0] & 0x10)); |
| 683 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], |
| 684 | !!(payload[1] & 0x01)); |
| 685 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], |
| 686 | !!(payload[1] & 0x02)); |
| 687 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], |
| 688 | !!(payload[1] & 0x04)); |
| 689 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], |
| 690 | !!(payload[1] & 0x08)); |
| 691 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], |
| 692 | !!(payload[1] & 0x10)); |
| 693 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], |
| 694 | !!(payload[1] & 0x80)); |
| 695 | input_sync(wdata->input); |
| 696 | } |
| 697 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 698 | static void handler_accel(struct wiimote_data *wdata, const __u8 *payload) |
| 699 | { |
| 700 | __u16 x, y, z; |
| 701 | |
| 702 | if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) |
| 703 | return; |
| 704 | |
| 705 | /* |
| 706 | * payload is: BB BB XX YY ZZ |
| 707 | * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ |
| 708 | * contain the upper 8 bits of each value. The lower 2 bits are |
| 709 | * contained in the buttons data BB BB. |
| 710 | * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the |
| 711 | * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y |
| 712 | * accel value and bit 6 is the second bit of the Z value. |
| 713 | * The first bit of Y and Z values is not available and always set to 0. |
| 714 | * 0x200 is returned on no movement. |
| 715 | */ |
| 716 | |
| 717 | x = payload[2] << 2; |
| 718 | y = payload[3] << 2; |
| 719 | z = payload[4] << 2; |
| 720 | |
| 721 | x |= (payload[0] >> 5) & 0x3; |
| 722 | y |= (payload[1] >> 4) & 0x2; |
| 723 | z |= (payload[1] >> 5) & 0x2; |
| 724 | |
| 725 | input_report_abs(wdata->accel, ABS_RX, x - 0x200); |
| 726 | input_report_abs(wdata->accel, ABS_RY, y - 0x200); |
| 727 | input_report_abs(wdata->accel, ABS_RZ, z - 0x200); |
| 728 | input_sync(wdata->accel); |
| 729 | } |
| 730 | |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 731 | #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ |
| 732 | ABS_HAT0X, ABS_HAT0Y) |
| 733 | #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ |
| 734 | ABS_HAT1X, ABS_HAT1Y) |
| 735 | #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ |
| 736 | ABS_HAT2X, ABS_HAT2Y) |
| 737 | #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ |
| 738 | ABS_HAT3X, ABS_HAT3Y) |
| 739 | |
| 740 | static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir, |
| 741 | bool packed, __u8 xid, __u8 yid) |
| 742 | { |
| 743 | __u16 x, y; |
| 744 | |
| 745 | if (!(wdata->state.flags & WIIPROTO_FLAGS_IR)) |
| 746 | return; |
| 747 | |
| 748 | /* |
| 749 | * Basic IR data is encoded into 3 bytes. The first two bytes are the |
| 750 | * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits |
| 751 | * of both. |
| 752 | * If data is packed, then the 3rd byte is put first and slightly |
| 753 | * reordered. This allows to interleave packed and non-packed data to |
| 754 | * have two IR sets in 5 bytes instead of 6. |
| 755 | * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev. |
| 756 | */ |
| 757 | |
| 758 | if (packed) { |
| 759 | x = ir[1] << 2; |
| 760 | y = ir[2] << 2; |
| 761 | |
| 762 | x |= ir[0] & 0x3; |
| 763 | y |= (ir[0] >> 2) & 0x3; |
| 764 | } else { |
| 765 | x = ir[0] << 2; |
| 766 | y = ir[1] << 2; |
| 767 | |
| 768 | x |= (ir[2] >> 4) & 0x3; |
| 769 | y |= (ir[2] >> 6) & 0x3; |
| 770 | } |
| 771 | |
| 772 | input_report_abs(wdata->ir, xid, x); |
| 773 | input_report_abs(wdata->ir, yid, y); |
| 774 | } |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 775 | |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 776 | static void handler_status(struct wiimote_data *wdata, const __u8 *payload) |
| 777 | { |
| 778 | handler_keys(wdata, payload); |
| 779 | |
| 780 | /* on status reports the drm is reset so we need to resend the drm */ |
| 781 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 782 | } |
| 783 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 784 | static void handler_data(struct wiimote_data *wdata, const __u8 *payload) |
| 785 | { |
| 786 | handler_keys(wdata, payload); |
| 787 | } |
| 788 | |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 789 | static void handler_return(struct wiimote_data *wdata, const __u8 *payload) |
| 790 | { |
| 791 | __u8 err = payload[3]; |
| 792 | __u8 cmd = payload[2]; |
| 793 | |
| 794 | handler_keys(wdata, payload); |
| 795 | |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 796 | if (wiimote_cmd_pending(wdata, cmd, 0)) { |
| 797 | wdata->state.cmd_err = err; |
| 798 | wiimote_cmd_complete(wdata); |
| 799 | } else if (err) { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 800 | hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err, |
| 801 | cmd); |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 802 | } |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 803 | } |
| 804 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 805 | static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload) |
| 806 | { |
| 807 | handler_keys(wdata, payload); |
| 808 | handler_accel(wdata, payload); |
| 809 | } |
| 810 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 811 | static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload) |
| 812 | { |
| 813 | handler_keys(wdata, payload); |
| 814 | } |
| 815 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 816 | static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload) |
| 817 | { |
| 818 | handler_keys(wdata, payload); |
| 819 | handler_accel(wdata, payload); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 820 | ir_to_input0(wdata, &payload[5], false); |
| 821 | ir_to_input1(wdata, &payload[8], false); |
| 822 | ir_to_input2(wdata, &payload[11], false); |
| 823 | ir_to_input3(wdata, &payload[14], false); |
| 824 | input_sync(wdata->ir); |
| 825 | } |
| 826 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 827 | static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload) |
| 828 | { |
| 829 | handler_keys(wdata, payload); |
| 830 | } |
| 831 | |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 832 | static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload) |
| 833 | { |
| 834 | handler_keys(wdata, payload); |
| 835 | ir_to_input0(wdata, &payload[2], false); |
| 836 | ir_to_input1(wdata, &payload[4], true); |
| 837 | ir_to_input2(wdata, &payload[7], false); |
| 838 | ir_to_input3(wdata, &payload[9], true); |
| 839 | input_sync(wdata->ir); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload) |
| 843 | { |
| 844 | handler_keys(wdata, payload); |
| 845 | handler_accel(wdata, payload); |
| 846 | } |
| 847 | |
| 848 | static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload) |
| 849 | { |
| 850 | handler_keys(wdata, payload); |
| 851 | handler_accel(wdata, payload); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 852 | ir_to_input0(wdata, &payload[5], false); |
| 853 | ir_to_input1(wdata, &payload[7], true); |
| 854 | ir_to_input2(wdata, &payload[10], false); |
| 855 | ir_to_input3(wdata, &payload[12], true); |
| 856 | input_sync(wdata->ir); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 857 | } |
| 858 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 859 | static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload) |
| 860 | { |
| 861 | } |
| 862 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 863 | static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload) |
| 864 | { |
| 865 | handler_keys(wdata, payload); |
| 866 | |
| 867 | wdata->state.accel_split[0] = payload[2]; |
| 868 | wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20); |
| 869 | wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 870 | |
| 871 | ir_to_input0(wdata, &payload[3], false); |
| 872 | ir_to_input1(wdata, &payload[12], false); |
| 873 | input_sync(wdata->ir); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload) |
| 877 | { |
| 878 | __u8 buf[5]; |
| 879 | |
| 880 | handler_keys(wdata, payload); |
| 881 | |
| 882 | wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02); |
| 883 | wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08); |
| 884 | |
| 885 | buf[0] = 0; |
| 886 | buf[1] = 0; |
| 887 | buf[2] = wdata->state.accel_split[0]; |
| 888 | buf[3] = payload[2]; |
| 889 | buf[4] = wdata->state.accel_split[1]; |
| 890 | handler_accel(wdata, buf); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 891 | |
| 892 | ir_to_input2(wdata, &payload[3], false); |
| 893 | ir_to_input3(wdata, &payload[12], false); |
| 894 | input_sync(wdata->ir); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 895 | } |
| 896 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 897 | struct wiiproto_handler { |
| 898 | __u8 id; |
| 899 | size_t size; |
| 900 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); |
| 901 | }; |
| 902 | |
| 903 | static struct wiiproto_handler handlers[] = { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 904 | { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status }, |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 905 | { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data }, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 906 | { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return }, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 907 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 908 | { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 909 | { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 910 | { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 911 | { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 912 | { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE }, |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 913 | { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 914 | { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 915 | { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 916 | { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 }, |
| 917 | { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 }, |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 918 | { .id = 0 } |
| 919 | }; |
| 920 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 921 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 922 | u8 *raw_data, int size) |
| 923 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 924 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 925 | struct wiiproto_handler *h; |
| 926 | int i; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 927 | unsigned long flags; |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 928 | bool handled = false; |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 929 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 930 | if (size < 1) |
| 931 | return -EINVAL; |
| 932 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 933 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 934 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 935 | for (i = 0; handlers[i].id; ++i) { |
| 936 | h = &handlers[i]; |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 937 | if (h->id == raw_data[0] && h->size < size) { |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 938 | h->func(wdata, &raw_data[1]); |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 939 | handled = true; |
| 940 | } |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 941 | } |
| 942 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 943 | if (!handled) |
| 944 | hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0], |
| 945 | size); |
| 946 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 947 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 948 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 949 | return 0; |
| 950 | } |
| 951 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 952 | static void wiimote_leds_destroy(struct wiimote_data *wdata) |
| 953 | { |
| 954 | int i; |
| 955 | struct led_classdev *led; |
| 956 | |
| 957 | for (i = 0; i < 4; ++i) { |
| 958 | if (wdata->leds[i]) { |
| 959 | led = wdata->leds[i]; |
| 960 | wdata->leds[i] = NULL; |
| 961 | led_classdev_unregister(led); |
| 962 | kfree(led); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | static int wiimote_leds_create(struct wiimote_data *wdata) |
| 968 | { |
| 969 | int i, ret; |
| 970 | struct device *dev = &wdata->hdev->dev; |
| 971 | size_t namesz = strlen(dev_name(dev)) + 9; |
| 972 | struct led_classdev *led; |
| 973 | char *name; |
| 974 | |
| 975 | for (i = 0; i < 4; ++i) { |
| 976 | led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL); |
| 977 | if (!led) { |
| 978 | ret = -ENOMEM; |
| 979 | goto err; |
| 980 | } |
| 981 | name = (void*)&led[1]; |
| 982 | snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i); |
| 983 | led->name = name; |
| 984 | led->brightness = 0; |
| 985 | led->max_brightness = 1; |
| 986 | led->brightness_get = wiimote_leds_get; |
| 987 | led->brightness_set = wiimote_leds_set; |
| 988 | |
| 989 | ret = led_classdev_register(dev, led); |
| 990 | if (ret) { |
| 991 | kfree(led); |
| 992 | goto err; |
| 993 | } |
| 994 | wdata->leds[i] = led; |
| 995 | } |
| 996 | |
| 997 | return 0; |
| 998 | |
| 999 | err: |
| 1000 | wiimote_leds_destroy(wdata); |
| 1001 | return ret; |
| 1002 | } |
| 1003 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1004 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 1005 | { |
| 1006 | struct wiimote_data *wdata; |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 1007 | int i; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1008 | |
| 1009 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 1010 | if (!wdata) |
| 1011 | return NULL; |
| 1012 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1013 | wdata->input = input_allocate_device(); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1014 | if (!wdata->input) |
| 1015 | goto err; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1016 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1017 | wdata->hdev = hdev; |
| 1018 | hid_set_drvdata(hdev, wdata); |
| 1019 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1020 | input_set_drvdata(wdata->input, wdata); |
David Herrmann | 26af174 | 2011-08-17 11:43:21 +0200 | [diff] [blame] | 1021 | wdata->input->open = wiimote_input_open; |
| 1022 | wdata->input->close = wiimote_input_close; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1023 | wdata->input->dev.parent = &wdata->hdev->dev; |
| 1024 | wdata->input->id.bustype = wdata->hdev->bus; |
| 1025 | wdata->input->id.vendor = wdata->hdev->vendor; |
| 1026 | wdata->input->id.product = wdata->hdev->product; |
| 1027 | wdata->input->id.version = wdata->hdev->version; |
| 1028 | wdata->input->name = WIIMOTE_NAME; |
| 1029 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 1030 | set_bit(EV_KEY, wdata->input->evbit); |
| 1031 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) |
| 1032 | set_bit(wiiproto_keymap[i], wdata->input->keybit); |
| 1033 | |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame] | 1034 | set_bit(FF_RUMBLE, wdata->input->ffbit); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1035 | if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play)) |
| 1036 | goto err_input; |
| 1037 | |
| 1038 | wdata->accel = input_allocate_device(); |
| 1039 | if (!wdata->accel) |
| 1040 | goto err_input; |
| 1041 | |
| 1042 | input_set_drvdata(wdata->accel, wdata); |
| 1043 | wdata->accel->open = wiimote_accel_open; |
| 1044 | wdata->accel->close = wiimote_accel_close; |
| 1045 | wdata->accel->dev.parent = &wdata->hdev->dev; |
| 1046 | wdata->accel->id.bustype = wdata->hdev->bus; |
| 1047 | wdata->accel->id.vendor = wdata->hdev->vendor; |
| 1048 | wdata->accel->id.product = wdata->hdev->product; |
| 1049 | wdata->accel->id.version = wdata->hdev->version; |
| 1050 | wdata->accel->name = WIIMOTE_NAME " Accelerometer"; |
| 1051 | |
| 1052 | set_bit(EV_ABS, wdata->accel->evbit); |
| 1053 | set_bit(ABS_RX, wdata->accel->absbit); |
| 1054 | set_bit(ABS_RY, wdata->accel->absbit); |
| 1055 | set_bit(ABS_RZ, wdata->accel->absbit); |
| 1056 | input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4); |
| 1057 | input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4); |
| 1058 | input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4); |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame] | 1059 | |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1060 | wdata->ir = input_allocate_device(); |
| 1061 | if (!wdata->ir) |
| 1062 | goto err_ir; |
| 1063 | |
| 1064 | input_set_drvdata(wdata->ir, wdata); |
| 1065 | wdata->ir->dev.parent = &wdata->hdev->dev; |
| 1066 | wdata->ir->id.bustype = wdata->hdev->bus; |
| 1067 | wdata->ir->id.vendor = wdata->hdev->vendor; |
| 1068 | wdata->ir->id.product = wdata->hdev->product; |
| 1069 | wdata->ir->id.version = wdata->hdev->version; |
| 1070 | wdata->ir->name = WIIMOTE_NAME " IR"; |
| 1071 | |
| 1072 | set_bit(EV_ABS, wdata->ir->evbit); |
| 1073 | set_bit(ABS_HAT0X, wdata->ir->absbit); |
| 1074 | set_bit(ABS_HAT0Y, wdata->ir->absbit); |
| 1075 | set_bit(ABS_HAT1X, wdata->ir->absbit); |
| 1076 | set_bit(ABS_HAT1Y, wdata->ir->absbit); |
| 1077 | set_bit(ABS_HAT2X, wdata->ir->absbit); |
| 1078 | set_bit(ABS_HAT2Y, wdata->ir->absbit); |
| 1079 | set_bit(ABS_HAT3X, wdata->ir->absbit); |
| 1080 | set_bit(ABS_HAT3Y, wdata->ir->absbit); |
| 1081 | input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4); |
| 1082 | input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4); |
| 1083 | input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4); |
| 1084 | input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4); |
| 1085 | input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4); |
| 1086 | input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4); |
| 1087 | input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4); |
| 1088 | input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4); |
| 1089 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 1090 | spin_lock_init(&wdata->qlock); |
| 1091 | INIT_WORK(&wdata->worker, wiimote_worker); |
| 1092 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1093 | spin_lock_init(&wdata->state.lock); |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 1094 | init_completion(&wdata->state.ready); |
| 1095 | mutex_init(&wdata->state.sync); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1096 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1097 | return wdata; |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1098 | |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1099 | err_ir: |
| 1100 | input_free_device(wdata->accel); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1101 | err_input: |
| 1102 | input_free_device(wdata->input); |
| 1103 | err: |
| 1104 | kfree(wdata); |
| 1105 | return NULL; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 1109 | { |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 1110 | wiimote_leds_destroy(wdata); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1111 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1112 | input_unregister_device(wdata->accel); |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1113 | input_unregister_device(wdata->ir); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1114 | input_unregister_device(wdata->input); |
| 1115 | cancel_work_sync(&wdata->worker); |
| 1116 | hid_hw_stop(wdata->hdev); |
| 1117 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1118 | kfree(wdata); |
| 1119 | } |
| 1120 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1121 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 1122 | const struct hid_device_id *id) |
| 1123 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1124 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1125 | int ret; |
| 1126 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1127 | wdata = wiimote_create(hdev); |
| 1128 | if (!wdata) { |
| 1129 | hid_err(hdev, "Can't alloc device\n"); |
| 1130 | return -ENOMEM; |
| 1131 | } |
| 1132 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1133 | ret = hid_parse(hdev); |
| 1134 | if (ret) { |
| 1135 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1136 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 1140 | if (ret) { |
| 1141 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1142 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1145 | ret = input_register_device(wdata->accel); |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1146 | if (ret) { |
| 1147 | hid_err(hdev, "Cannot register input device\n"); |
| 1148 | goto err_stop; |
| 1149 | } |
| 1150 | |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1151 | ret = input_register_device(wdata->ir); |
| 1152 | if (ret) { |
| 1153 | hid_err(hdev, "Cannot register input device\n"); |
| 1154 | goto err_ir; |
| 1155 | } |
| 1156 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1157 | ret = input_register_device(wdata->input); |
| 1158 | if (ret) { |
| 1159 | hid_err(hdev, "Cannot register input device\n"); |
| 1160 | goto err_input; |
| 1161 | } |
| 1162 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 1163 | ret = wiimote_leds_create(wdata); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1164 | if (ret) |
| 1165 | goto err_free; |
| 1166 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1167 | hid_info(hdev, "New device registered\n"); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1168 | |
| 1169 | /* by default set led1 after device initialization */ |
| 1170 | spin_lock_irq(&wdata->state.lock); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 1171 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1172 | spin_unlock_irq(&wdata->state.lock); |
| 1173 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1174 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1175 | |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1176 | err_free: |
| 1177 | wiimote_destroy(wdata); |
| 1178 | return ret; |
| 1179 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1180 | err_input: |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1181 | input_unregister_device(wdata->ir); |
| 1182 | wdata->ir = NULL; |
| 1183 | err_ir: |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1184 | input_unregister_device(wdata->accel); |
| 1185 | wdata->accel = NULL; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1186 | err_stop: |
| 1187 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1188 | err: |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1189 | input_free_device(wdata->ir); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1190 | input_free_device(wdata->accel); |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1191 | input_free_device(wdata->input); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1192 | kfree(wdata); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1193 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 1197 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1198 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 1199 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1200 | hid_info(hdev, "Device removed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1201 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 1205 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 1206 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
| 1207 | { } |
| 1208 | }; |
| 1209 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 1210 | |
| 1211 | static struct hid_driver wiimote_hid_driver = { |
| 1212 | .name = "wiimote", |
| 1213 | .id_table = wiimote_hid_devices, |
| 1214 | .probe = wiimote_hid_probe, |
| 1215 | .remove = wiimote_hid_remove, |
| 1216 | .raw_event = wiimote_hid_event, |
| 1217 | }; |
| 1218 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1219 | static int __init wiimote_init(void) |
| 1220 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1221 | int ret; |
| 1222 | |
| 1223 | ret = hid_register_driver(&wiimote_hid_driver); |
| 1224 | if (ret) |
| 1225 | pr_err("Can't register wiimote hid driver\n"); |
| 1226 | |
| 1227 | return ret; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | static void __exit wiimote_exit(void) |
| 1231 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1232 | hid_unregister_driver(&wiimote_hid_driver); |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | module_init(wiimote_init); |
| 1236 | module_exit(wiimote_exit); |
| 1237 | MODULE_LICENSE("GPL"); |
| 1238 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
| 1239 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); |
| 1240 | MODULE_VERSION(WIIMOTE_VERSION); |