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