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