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