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 | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 17 | #include <linux/module.h> |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 18 | #include <linux/mutex.h> |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 20 | #include "hid-ids.h" |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 21 | #include "hid-wiimote.h" |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 22 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 23 | /* output queue handling */ |
| 24 | |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 25 | static int wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
| 26 | size_t count) |
David Herrmann | 0c218f1 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 27 | { |
| 28 | __u8 *buf; |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 29 | int ret; |
David Herrmann | 0c218f1 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 30 | |
| 31 | if (!hdev->hid_output_raw_report) |
| 32 | return -ENODEV; |
| 33 | |
| 34 | buf = kmemdup(buffer, count, GFP_KERNEL); |
| 35 | if (!buf) |
| 36 | return -ENOMEM; |
| 37 | |
| 38 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); |
| 39 | |
| 40 | kfree(buf); |
| 41 | return ret; |
| 42 | } |
| 43 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 44 | static void wiimote_queue_worker(struct work_struct *work) |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 45 | { |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 46 | struct wiimote_queue *queue = container_of(work, struct wiimote_queue, |
| 47 | worker); |
| 48 | struct wiimote_data *wdata = container_of(queue, struct wiimote_data, |
| 49 | queue); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 50 | unsigned long flags; |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 51 | int ret; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 52 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 53 | spin_lock_irqsave(&wdata->queue.lock, flags); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 54 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 55 | while (wdata->queue.head != wdata->queue.tail) { |
| 56 | spin_unlock_irqrestore(&wdata->queue.lock, flags); |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 57 | ret = wiimote_hid_send(wdata->hdev, |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 58 | wdata->queue.outq[wdata->queue.tail].data, |
| 59 | wdata->queue.outq[wdata->queue.tail].size); |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 60 | if (ret < 0) { |
| 61 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 62 | wiimote_cmd_abort(wdata); |
| 63 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 64 | } |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 65 | spin_lock_irqsave(&wdata->queue.lock, flags); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 66 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 67 | wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 68 | } |
| 69 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 70 | spin_unlock_irqrestore(&wdata->queue.lock, flags); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, |
| 74 | size_t count) |
| 75 | { |
| 76 | unsigned long flags; |
| 77 | __u8 newhead; |
| 78 | |
| 79 | if (count > HID_MAX_BUFFER_SIZE) { |
| 80 | hid_warn(wdata->hdev, "Sending too large output report\n"); |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 81 | |
| 82 | spin_lock_irqsave(&wdata->queue.lock, flags); |
| 83 | goto out_error; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Copy new request into our output queue and check whether the |
| 88 | * queue is full. If it is full, discard this request. |
| 89 | * If it is empty we need to start a new worker that will |
| 90 | * send out the buffer to the hid device. |
| 91 | * If the queue is not empty, then there must be a worker |
| 92 | * that is currently sending out our buffer and this worker |
| 93 | * will reschedule itself until the queue is empty. |
| 94 | */ |
| 95 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 96 | spin_lock_irqsave(&wdata->queue.lock, flags); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 97 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 98 | memcpy(wdata->queue.outq[wdata->queue.head].data, buffer, count); |
| 99 | wdata->queue.outq[wdata->queue.head].size = count; |
| 100 | newhead = (wdata->queue.head + 1) % WIIMOTE_BUFSIZE; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 101 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 102 | if (wdata->queue.head == wdata->queue.tail) { |
| 103 | wdata->queue.head = newhead; |
| 104 | schedule_work(&wdata->queue.worker); |
| 105 | } else if (newhead != wdata->queue.tail) { |
| 106 | wdata->queue.head = newhead; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 107 | } else { |
| 108 | hid_warn(wdata->hdev, "Output queue is full"); |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 109 | goto out_error; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 110 | } |
| 111 | |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 112 | goto out_unlock; |
| 113 | |
| 114 | out_error: |
| 115 | wiimote_cmd_abort(wdata); |
| 116 | out_unlock: |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 117 | spin_unlock_irqrestore(&wdata->queue.lock, flags); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 118 | } |
| 119 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 120 | /* |
| 121 | * This sets the rumble bit on the given output report if rumble is |
| 122 | * currently enabled. |
| 123 | * \cmd1 must point to the second byte in the output report => &cmd[1] |
| 124 | * This must be called on nearly every output report before passing it |
| 125 | * into the output queue! |
| 126 | */ |
| 127 | static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1) |
| 128 | { |
| 129 | if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE) |
| 130 | *cmd1 |= 0x01; |
| 131 | } |
| 132 | |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 133 | void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble) |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 134 | { |
| 135 | __u8 cmd[2]; |
| 136 | |
| 137 | rumble = !!rumble; |
| 138 | if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE)) |
| 139 | return; |
| 140 | |
| 141 | if (rumble) |
| 142 | wdata->state.flags |= WIIPROTO_FLAG_RUMBLE; |
| 143 | else |
| 144 | wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE; |
| 145 | |
| 146 | cmd[0] = WIIPROTO_REQ_RUMBLE; |
| 147 | cmd[1] = 0; |
| 148 | |
| 149 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 150 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 151 | } |
| 152 | |
David Herrmann | 6c5ae018 | 2013-05-05 23:12:54 +0200 | [diff] [blame] | 153 | void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 154 | { |
| 155 | __u8 cmd[2]; |
| 156 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 157 | leds &= WIIPROTO_FLAGS_LEDS; |
| 158 | if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds) |
| 159 | return; |
| 160 | wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds; |
| 161 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 162 | cmd[0] = WIIPROTO_REQ_LED; |
| 163 | cmd[1] = 0; |
| 164 | |
| 165 | if (leds & WIIPROTO_FLAG_LED1) |
| 166 | cmd[1] |= 0x10; |
| 167 | if (leds & WIIPROTO_FLAG_LED2) |
| 168 | cmd[1] |= 0x20; |
| 169 | if (leds & WIIPROTO_FLAG_LED3) |
| 170 | cmd[1] |= 0x40; |
| 171 | if (leds & WIIPROTO_FLAG_LED4) |
| 172 | cmd[1] |= 0x80; |
| 173 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 174 | wiiproto_keep_rumble(wdata, &cmd[1]); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 175 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 176 | } |
| 177 | |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 178 | /* |
| 179 | * Check what peripherals of the wiimote are currently |
| 180 | * active and select a proper DRM that supports all of |
| 181 | * the requested data inputs. |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 182 | * |
| 183 | * Not all combinations are actually supported. The following |
| 184 | * combinations work only with limitations: |
| 185 | * - IR cam in extended or full mode disables any data transmission |
| 186 | * of extension controllers. There is no DRM mode that supports |
| 187 | * extension bytes plus extended/full IR. |
| 188 | * - IR cam with accelerometer and extension *_EXT8 is not supported. |
| 189 | * However, all extensions that need *_EXT8 are devices that don't |
| 190 | * support IR cameras. Hence, this shouldn't happen under normal |
| 191 | * operation. |
| 192 | * - *_EXT16 is only supported in combination with buttons and |
| 193 | * accelerometer. No IR or similar can be active simultaneously. As |
| 194 | * above, all modules that require it are mutually exclusive with |
| 195 | * IR/etc. so this doesn't matter. |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 196 | */ |
| 197 | static __u8 select_drm(struct wiimote_data *wdata) |
| 198 | { |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 199 | __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR; |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 200 | bool ext; |
| 201 | |
| 202 | ext = (wdata->state.flags & WIIPROTO_FLAG_EXT_USED) || |
| 203 | (wdata->state.flags & WIIPROTO_FLAG_MP_USED); |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 204 | |
| 205 | if (ir == WIIPROTO_FLAG_IR_BASIC) { |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 206 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) { |
| 207 | if (ext) |
| 208 | return WIIPROTO_REQ_DRM_KAIE; |
| 209 | else |
| 210 | return WIIPROTO_REQ_DRM_KAI; |
| 211 | } else { |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 212 | return WIIPROTO_REQ_DRM_KIE; |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 213 | } |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 214 | } else if (ir == WIIPROTO_FLAG_IR_EXT) { |
| 215 | return WIIPROTO_REQ_DRM_KAI; |
| 216 | } else if (ir == WIIPROTO_FLAG_IR_FULL) { |
| 217 | return WIIPROTO_REQ_DRM_SKAI1; |
| 218 | } else { |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 219 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) { |
| 220 | if (ext) |
| 221 | return WIIPROTO_REQ_DRM_KAE; |
| 222 | else |
| 223 | return WIIPROTO_REQ_DRM_KA; |
| 224 | } else { |
| 225 | if (ext) |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 226 | return WIIPROTO_REQ_DRM_KEE; |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 227 | else |
| 228 | return WIIPROTO_REQ_DRM_K; |
| 229 | } |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 230 | } |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 231 | } |
| 232 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 233 | void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 234 | { |
| 235 | __u8 cmd[3]; |
| 236 | |
| 237 | if (drm == WIIPROTO_REQ_NULL) |
| 238 | drm = select_drm(wdata); |
| 239 | |
| 240 | cmd[0] = WIIPROTO_REQ_DRM; |
| 241 | cmd[1] = 0; |
| 242 | cmd[2] = drm; |
| 243 | |
David Herrmann | 43d782a | 2011-11-17 14:12:12 +0100 | [diff] [blame] | 244 | wdata->state.drm = drm; |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 245 | wiiproto_keep_rumble(wdata, &cmd[1]); |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 246 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 247 | } |
| 248 | |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 249 | void wiiproto_req_status(struct wiimote_data *wdata) |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 250 | { |
| 251 | __u8 cmd[2]; |
| 252 | |
| 253 | cmd[0] = WIIPROTO_REQ_SREQ; |
| 254 | cmd[1] = 0; |
| 255 | |
| 256 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 257 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 258 | } |
| 259 | |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 260 | void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel) |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 261 | { |
| 262 | accel = !!accel; |
| 263 | if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) |
| 264 | return; |
| 265 | |
| 266 | if (accel) |
| 267 | wdata->state.flags |= WIIPROTO_FLAG_ACCEL; |
| 268 | else |
| 269 | wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL; |
| 270 | |
| 271 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 272 | } |
| 273 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 274 | void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags) |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame] | 275 | { |
| 276 | __u8 cmd[2]; |
| 277 | |
| 278 | cmd[0] = WIIPROTO_REQ_IR1; |
| 279 | cmd[1] = flags; |
| 280 | |
| 281 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 282 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 283 | } |
| 284 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 285 | void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags) |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame] | 286 | { |
| 287 | __u8 cmd[2]; |
| 288 | |
| 289 | cmd[0] = WIIPROTO_REQ_IR2; |
| 290 | cmd[1] = flags; |
| 291 | |
| 292 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 293 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 294 | } |
| 295 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 296 | #define wiiproto_req_wreg(wdata, os, buf, sz) \ |
| 297 | wiiproto_req_wmem((wdata), false, (os), (buf), (sz)) |
| 298 | |
| 299 | #define wiiproto_req_weeprom(wdata, os, buf, sz) \ |
| 300 | wiiproto_req_wmem((wdata), true, (os), (buf), (sz)) |
| 301 | |
| 302 | static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom, |
| 303 | __u32 offset, const __u8 *buf, __u8 size) |
| 304 | { |
| 305 | __u8 cmd[22]; |
| 306 | |
| 307 | if (size > 16 || size == 0) { |
| 308 | hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | memset(cmd, 0, sizeof(cmd)); |
| 313 | cmd[0] = WIIPROTO_REQ_WMEM; |
| 314 | cmd[2] = (offset >> 16) & 0xff; |
| 315 | cmd[3] = (offset >> 8) & 0xff; |
| 316 | cmd[4] = offset & 0xff; |
| 317 | cmd[5] = size; |
| 318 | memcpy(&cmd[6], buf, size); |
| 319 | |
| 320 | if (!eeprom) |
| 321 | cmd[1] |= 0x04; |
| 322 | |
| 323 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 324 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 325 | } |
| 326 | |
David Herrmann | 1d3452c | 2011-11-17 14:12:11 +0100 | [diff] [blame] | 327 | void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset, |
| 328 | __u16 size) |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 329 | { |
| 330 | __u8 cmd[7]; |
| 331 | |
| 332 | if (size == 0) { |
| 333 | hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | cmd[0] = WIIPROTO_REQ_RMEM; |
| 338 | cmd[1] = 0; |
| 339 | cmd[2] = (offset >> 16) & 0xff; |
| 340 | cmd[3] = (offset >> 8) & 0xff; |
| 341 | cmd[4] = offset & 0xff; |
| 342 | cmd[5] = (size >> 8) & 0xff; |
| 343 | cmd[6] = size & 0xff; |
| 344 | |
| 345 | if (!eeprom) |
| 346 | cmd[1] |= 0x04; |
| 347 | |
| 348 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 349 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 350 | } |
| 351 | |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 352 | /* requries the cmd-mutex to be held */ |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 353 | int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 354 | const __u8 *wmem, __u8 size) |
| 355 | { |
| 356 | unsigned long flags; |
| 357 | int ret; |
| 358 | |
| 359 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 360 | wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0); |
| 361 | wiiproto_req_wreg(wdata, offset, wmem, size); |
| 362 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 363 | |
| 364 | ret = wiimote_cmd_wait(wdata); |
| 365 | if (!ret && wdata->state.cmd_err) |
| 366 | ret = -EIO; |
| 367 | |
| 368 | return ret; |
| 369 | } |
| 370 | |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 371 | /* requries the cmd-mutex to be held */ |
| 372 | ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem, |
| 373 | __u8 size) |
| 374 | { |
| 375 | unsigned long flags; |
| 376 | ssize_t ret; |
| 377 | |
| 378 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 379 | wdata->state.cmd_read_size = size; |
| 380 | wdata->state.cmd_read_buf = rmem; |
| 381 | wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff); |
| 382 | wiiproto_req_rreg(wdata, offset, size); |
| 383 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 384 | |
| 385 | ret = wiimote_cmd_wait(wdata); |
| 386 | |
| 387 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 388 | wdata->state.cmd_read_buf = NULL; |
| 389 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 390 | |
| 391 | if (!ret) { |
| 392 | if (wdata->state.cmd_read_size == 0) |
| 393 | ret = -EIO; |
| 394 | else |
| 395 | ret = wdata->state.cmd_read_size; |
| 396 | } |
| 397 | |
| 398 | return ret; |
| 399 | } |
| 400 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 401 | /* requires the cmd-mutex to be held */ |
| 402 | static int wiimote_cmd_init_ext(struct wiimote_data *wdata) |
| 403 | { |
| 404 | __u8 wmem; |
| 405 | int ret; |
| 406 | |
| 407 | /* initialize extension */ |
| 408 | wmem = 0x55; |
| 409 | ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem)); |
| 410 | if (ret) |
| 411 | return ret; |
| 412 | |
| 413 | /* disable default encryption */ |
| 414 | wmem = 0x0; |
| 415 | ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem)); |
| 416 | if (ret) |
| 417 | return ret; |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | /* requires the cmd-mutex to be held */ |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 423 | static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata, __u8 *rmem) |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 424 | { |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 425 | int ret; |
| 426 | |
| 427 | /* read extension ID */ |
| 428 | ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6); |
| 429 | if (ret != 6) |
| 430 | return WIIMOTE_EXT_NONE; |
| 431 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 432 | hid_dbg(wdata->hdev, "extension ID: %02x:%02x %02x:%02x %02x:%02x\n", |
| 433 | rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]); |
| 434 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 435 | if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff && |
| 436 | rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff) |
| 437 | return WIIMOTE_EXT_NONE; |
| 438 | |
| 439 | return WIIMOTE_EXT_UNKNOWN; |
| 440 | } |
| 441 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 442 | /* requires the cmd-mutex to be held */ |
| 443 | static int wiimote_cmd_init_mp(struct wiimote_data *wdata) |
| 444 | { |
| 445 | __u8 wmem; |
| 446 | int ret; |
| 447 | |
| 448 | /* initialize MP */ |
| 449 | wmem = 0x55; |
| 450 | ret = wiimote_cmd_write(wdata, 0xa600f0, &wmem, sizeof(wmem)); |
| 451 | if (ret) |
| 452 | return ret; |
| 453 | |
| 454 | /* disable default encryption */ |
| 455 | wmem = 0x0; |
| 456 | ret = wiimote_cmd_write(wdata, 0xa600fb, &wmem, sizeof(wmem)); |
| 457 | if (ret) |
| 458 | return ret; |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | /* requires the cmd-mutex to be held */ |
| 464 | static bool wiimote_cmd_map_mp(struct wiimote_data *wdata, __u8 exttype) |
| 465 | { |
| 466 | __u8 wmem; |
| 467 | |
| 468 | /* map MP with correct pass-through mode */ |
| 469 | switch (exttype) { |
| 470 | default: |
| 471 | wmem = 0x04; |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | return wiimote_cmd_write(wdata, 0xa600fe, &wmem, sizeof(wmem)); |
| 476 | } |
| 477 | |
| 478 | /* requires the cmd-mutex to be held */ |
| 479 | static bool wiimote_cmd_read_mp(struct wiimote_data *wdata, __u8 *rmem) |
| 480 | { |
| 481 | int ret; |
| 482 | |
| 483 | /* read motion plus ID */ |
| 484 | ret = wiimote_cmd_read(wdata, 0xa600fa, rmem, 6); |
| 485 | if (ret != 6) |
| 486 | return false; |
| 487 | |
| 488 | hid_dbg(wdata->hdev, "motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n", |
| 489 | rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]); |
| 490 | |
| 491 | if (rmem[5] == 0x05) |
| 492 | return true; |
| 493 | |
| 494 | hid_info(wdata->hdev, "unknown motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n", |
| 495 | rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]); |
| 496 | |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | /* requires the cmd-mutex to be held */ |
| 501 | static __u8 wiimote_cmd_read_mp_mapped(struct wiimote_data *wdata) |
| 502 | { |
| 503 | int ret; |
| 504 | __u8 rmem[6]; |
| 505 | |
| 506 | /* read motion plus ID */ |
| 507 | ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6); |
| 508 | if (ret != 6) |
| 509 | return WIIMOTE_MP_NONE; |
| 510 | |
| 511 | hid_dbg(wdata->hdev, "mapped motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n", |
| 512 | rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]); |
| 513 | |
| 514 | if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff && |
| 515 | rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff) |
| 516 | return WIIMOTE_MP_NONE; |
| 517 | |
| 518 | if (rmem[4] == 0x04 && rmem[5] == 0x05) |
| 519 | return WIIMOTE_MP_SINGLE; |
| 520 | else if (rmem[4] == 0x05 && rmem[5] == 0x05) |
| 521 | return WIIMOTE_MP_PASSTHROUGH_NUNCHUK; |
| 522 | else if (rmem[4] == 0x07 && rmem[5] == 0x05) |
| 523 | return WIIMOTE_MP_PASSTHROUGH_CLASSIC; |
| 524 | |
| 525 | return WIIMOTE_MP_UNKNOWN; |
| 526 | } |
| 527 | |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 528 | /* device module handling */ |
| 529 | |
| 530 | static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = { |
| 531 | [WIIMOTE_DEV_PENDING] = (const __u8[]){ |
| 532 | WIIMOD_NULL, |
| 533 | }, |
| 534 | [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){ |
| 535 | WIIMOD_NULL, |
| 536 | }, |
| 537 | [WIIMOTE_DEV_GENERIC] = (const __u8[]){ |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 538 | WIIMOD_KEYS, |
| 539 | WIIMOD_RUMBLE, |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 540 | WIIMOD_BATTERY, |
David Herrmann | 6c5ae018 | 2013-05-05 23:12:54 +0200 | [diff] [blame] | 541 | WIIMOD_LED1, |
| 542 | WIIMOD_LED2, |
| 543 | WIIMOD_LED3, |
| 544 | WIIMOD_LED4, |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 545 | WIIMOD_ACCEL, |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 546 | WIIMOD_IR, |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 547 | WIIMOD_NULL, |
| 548 | }, |
| 549 | [WIIMOTE_DEV_GEN10] = (const __u8[]){ |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 550 | WIIMOD_KEYS, |
| 551 | WIIMOD_RUMBLE, |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 552 | WIIMOD_BATTERY, |
David Herrmann | 6c5ae018 | 2013-05-05 23:12:54 +0200 | [diff] [blame] | 553 | WIIMOD_LED1, |
| 554 | WIIMOD_LED2, |
| 555 | WIIMOD_LED3, |
| 556 | WIIMOD_LED4, |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 557 | WIIMOD_ACCEL, |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 558 | WIIMOD_IR, |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 559 | WIIMOD_NULL, |
| 560 | }, |
| 561 | [WIIMOTE_DEV_GEN20] = (const __u8[]){ |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 562 | WIIMOD_KEYS, |
| 563 | WIIMOD_RUMBLE, |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 564 | WIIMOD_BATTERY, |
David Herrmann | 6c5ae018 | 2013-05-05 23:12:54 +0200 | [diff] [blame] | 565 | WIIMOD_LED1, |
| 566 | WIIMOD_LED2, |
| 567 | WIIMOD_LED3, |
| 568 | WIIMOD_LED4, |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 569 | WIIMOD_ACCEL, |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 570 | WIIMOD_IR, |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 571 | WIIMOD_NULL, |
| 572 | }, |
| 573 | }; |
| 574 | |
| 575 | static void wiimote_modules_load(struct wiimote_data *wdata, |
| 576 | unsigned int devtype) |
| 577 | { |
| 578 | bool need_input = false; |
| 579 | const __u8 *mods, *iter; |
| 580 | const struct wiimod_ops *ops; |
| 581 | int ret; |
| 582 | |
| 583 | mods = wiimote_devtype_mods[devtype]; |
| 584 | |
| 585 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 586 | if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) { |
| 587 | need_input = true; |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (need_input) { |
| 593 | wdata->input = input_allocate_device(); |
| 594 | if (!wdata->input) |
| 595 | return; |
| 596 | |
| 597 | input_set_drvdata(wdata->input, wdata); |
| 598 | wdata->input->dev.parent = &wdata->hdev->dev; |
| 599 | wdata->input->id.bustype = wdata->hdev->bus; |
| 600 | wdata->input->id.vendor = wdata->hdev->vendor; |
| 601 | wdata->input->id.product = wdata->hdev->product; |
| 602 | wdata->input->id.version = wdata->hdev->version; |
| 603 | wdata->input->name = WIIMOTE_NAME; |
| 604 | } |
| 605 | |
| 606 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 607 | ops = wiimod_table[*iter]; |
| 608 | if (!ops->probe) |
| 609 | continue; |
| 610 | |
| 611 | ret = ops->probe(ops, wdata); |
| 612 | if (ret) |
| 613 | goto error; |
| 614 | } |
| 615 | |
| 616 | if (wdata->input) { |
| 617 | ret = input_register_device(wdata->input); |
| 618 | if (ret) |
| 619 | goto error; |
| 620 | } |
| 621 | |
| 622 | spin_lock_irq(&wdata->state.lock); |
| 623 | wdata->state.devtype = devtype; |
| 624 | spin_unlock_irq(&wdata->state.lock); |
| 625 | return; |
| 626 | |
| 627 | error: |
| 628 | for ( ; iter-- != mods; ) { |
| 629 | ops = wiimod_table[*iter]; |
| 630 | if (ops->remove) |
| 631 | ops->remove(ops, wdata); |
| 632 | } |
| 633 | |
| 634 | if (wdata->input) { |
| 635 | input_free_device(wdata->input); |
| 636 | wdata->input = NULL; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | static void wiimote_modules_unload(struct wiimote_data *wdata) |
| 641 | { |
| 642 | const __u8 *mods, *iter; |
| 643 | const struct wiimod_ops *ops; |
| 644 | unsigned long flags; |
| 645 | |
| 646 | mods = wiimote_devtype_mods[wdata->state.devtype]; |
| 647 | |
| 648 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 649 | wdata->state.devtype = WIIMOTE_DEV_UNKNOWN; |
| 650 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 651 | |
| 652 | /* find end of list */ |
| 653 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) |
| 654 | /* empty */ ; |
| 655 | |
| 656 | if (wdata->input) { |
| 657 | input_get_device(wdata->input); |
| 658 | input_unregister_device(wdata->input); |
| 659 | } |
| 660 | |
| 661 | for ( ; iter-- != mods; ) { |
| 662 | ops = wiimod_table[*iter]; |
| 663 | if (ops->remove) |
| 664 | ops->remove(ops, wdata); |
| 665 | } |
| 666 | |
| 667 | if (wdata->input) { |
| 668 | input_put_device(wdata->input); |
| 669 | wdata->input = NULL; |
| 670 | } |
| 671 | } |
| 672 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 673 | /* device extension handling */ |
| 674 | |
| 675 | static void wiimote_ext_load(struct wiimote_data *wdata, unsigned int ext) |
| 676 | { |
| 677 | unsigned long flags; |
| 678 | const struct wiimod_ops *ops; |
| 679 | int ret; |
| 680 | |
| 681 | ops = wiimod_ext_table[ext]; |
| 682 | |
| 683 | if (ops->probe) { |
| 684 | ret = ops->probe(ops, wdata); |
| 685 | if (ret) |
| 686 | ext = WIIMOTE_EXT_UNKNOWN; |
| 687 | } |
| 688 | |
| 689 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 690 | wdata->state.exttype = ext; |
| 691 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 692 | } |
| 693 | |
| 694 | static void wiimote_ext_unload(struct wiimote_data *wdata) |
| 695 | { |
| 696 | unsigned long flags; |
| 697 | const struct wiimod_ops *ops; |
| 698 | |
| 699 | ops = wiimod_ext_table[wdata->state.exttype]; |
| 700 | |
| 701 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 702 | wdata->state.exttype = WIIMOTE_EXT_UNKNOWN; |
| 703 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED; |
| 704 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 705 | |
| 706 | if (ops->remove) |
| 707 | ops->remove(ops, wdata); |
| 708 | } |
| 709 | |
| 710 | static void wiimote_mp_load(struct wiimote_data *wdata) |
| 711 | { |
| 712 | unsigned long flags; |
| 713 | const struct wiimod_ops *ops; |
| 714 | int ret; |
| 715 | __u8 mode = 2; |
| 716 | |
| 717 | ops = &wiimod_mp; |
| 718 | if (ops->probe) { |
| 719 | ret = ops->probe(ops, wdata); |
| 720 | if (ret) |
| 721 | mode = 1; |
| 722 | } |
| 723 | |
| 724 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 725 | wdata->state.mp = mode; |
| 726 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 727 | } |
| 728 | |
| 729 | static void wiimote_mp_unload(struct wiimote_data *wdata) |
| 730 | { |
| 731 | unsigned long flags; |
| 732 | const struct wiimod_ops *ops; |
| 733 | |
| 734 | if (wdata->state.mp < 2) |
| 735 | return; |
| 736 | |
| 737 | ops = &wiimod_mp; |
| 738 | |
| 739 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 740 | wdata->state.mp = 0; |
| 741 | wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED; |
| 742 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 743 | |
| 744 | if (ops->remove) |
| 745 | ops->remove(ops, wdata); |
| 746 | } |
| 747 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 748 | /* device (re-)initialization and detection */ |
| 749 | |
| 750 | static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = { |
| 751 | [WIIMOTE_DEV_PENDING] = "Pending", |
| 752 | [WIIMOTE_DEV_UNKNOWN] = "Unknown", |
| 753 | [WIIMOTE_DEV_GENERIC] = "Generic", |
| 754 | [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)", |
| 755 | [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)", |
| 756 | }; |
| 757 | |
| 758 | /* Try to guess the device type based on all collected information. We |
| 759 | * first try to detect by static extension types, then VID/PID and the |
| 760 | * device name. If we cannot detect the device, we use |
| 761 | * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */ |
| 762 | static void wiimote_init_set_type(struct wiimote_data *wdata, |
| 763 | __u8 exttype) |
| 764 | { |
| 765 | __u8 devtype = WIIMOTE_DEV_GENERIC; |
| 766 | __u16 vendor, product; |
| 767 | const char *name; |
| 768 | |
| 769 | vendor = wdata->hdev->vendor; |
| 770 | product = wdata->hdev->product; |
| 771 | name = wdata->hdev->name; |
| 772 | |
| 773 | if (!strcmp(name, "Nintendo RVL-CNT-01")) { |
| 774 | devtype = WIIMOTE_DEV_GEN10; |
| 775 | goto done; |
| 776 | } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) { |
| 777 | devtype = WIIMOTE_DEV_GEN20; |
| 778 | goto done; |
| 779 | } |
| 780 | |
| 781 | if (vendor == USB_VENDOR_ID_NINTENDO) { |
| 782 | if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) { |
| 783 | devtype = WIIMOTE_DEV_GEN10; |
| 784 | goto done; |
| 785 | } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) { |
| 786 | devtype = WIIMOTE_DEV_GEN20; |
| 787 | goto done; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | done: |
| 792 | if (devtype == WIIMOTE_DEV_GENERIC) |
| 793 | hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n", |
| 794 | name, vendor, product, exttype); |
| 795 | else |
| 796 | hid_info(wdata->hdev, "detected device: %s\n", |
| 797 | wiimote_devtype_names[devtype]); |
| 798 | |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 799 | wiimote_modules_load(wdata, devtype); |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static void wiimote_init_detect(struct wiimote_data *wdata) |
| 803 | { |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 804 | __u8 exttype = WIIMOTE_EXT_NONE, extdata[6]; |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 805 | bool ext; |
| 806 | int ret; |
| 807 | |
| 808 | wiimote_cmd_acquire_noint(wdata); |
| 809 | |
| 810 | spin_lock_irq(&wdata->state.lock); |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 811 | wdata->state.devtype = WIIMOTE_DEV_UNKNOWN; |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 812 | wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0); |
| 813 | wiiproto_req_status(wdata); |
| 814 | spin_unlock_irq(&wdata->state.lock); |
| 815 | |
| 816 | ret = wiimote_cmd_wait_noint(wdata); |
| 817 | if (ret) |
| 818 | goto out_release; |
| 819 | |
| 820 | spin_lock_irq(&wdata->state.lock); |
| 821 | ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED; |
| 822 | spin_unlock_irq(&wdata->state.lock); |
| 823 | |
| 824 | if (!ext) |
| 825 | goto out_release; |
| 826 | |
| 827 | wiimote_cmd_init_ext(wdata); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 828 | exttype = wiimote_cmd_read_ext(wdata, extdata); |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 829 | |
| 830 | out_release: |
| 831 | wiimote_cmd_release(wdata); |
| 832 | wiimote_init_set_type(wdata, exttype); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 833 | /* schedule MP timer */ |
| 834 | mod_timer(&wdata->timer, jiffies + HZ * 4); |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * MP hotplug events are not generated by the wiimote. Therefore, we need |
| 839 | * polling to detect it. We use a 4s interval for polling MP registers. This |
| 840 | * seems reasonable considering applications can trigger it manually via |
| 841 | * sysfs requests. |
| 842 | */ |
| 843 | static void wiimote_init_poll_mp(struct wiimote_data *wdata) |
| 844 | { |
| 845 | bool mp; |
| 846 | __u8 mpdata[6]; |
| 847 | |
| 848 | wiimote_cmd_acquire_noint(wdata); |
| 849 | wiimote_cmd_init_mp(wdata); |
| 850 | mp = wiimote_cmd_read_mp(wdata, mpdata); |
| 851 | wiimote_cmd_release(wdata); |
| 852 | |
| 853 | /* load/unload MP module if it changed */ |
| 854 | if (mp) { |
| 855 | if (!wdata->state.mp) { |
| 856 | hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n"); |
| 857 | wiimote_mp_load(wdata); |
| 858 | } |
| 859 | } else if (wdata->state.mp) { |
| 860 | wiimote_mp_unload(wdata); |
| 861 | } |
| 862 | |
| 863 | mod_timer(&wdata->timer, jiffies + HZ * 4); |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Check whether the wiimote is in the expected state. The extension registers |
| 868 | * may change during hotplug and initialization so we might get hotplug events |
| 869 | * that we caused by remapping some memory. |
| 870 | * We use some heuristics here to check known states. If the wiimote is in the |
| 871 | * expected state, we can ignore the hotplug event. |
| 872 | * |
| 873 | * Returns "true" if the device is in expected state, "false" if we should |
| 874 | * redo hotplug handling and extension initialization. |
| 875 | */ |
| 876 | static bool wiimote_init_check(struct wiimote_data *wdata) |
| 877 | { |
| 878 | __u32 flags; |
| 879 | __u8 type, data[6]; |
| 880 | bool ret, poll_mp; |
| 881 | |
| 882 | spin_lock_irq(&wdata->state.lock); |
| 883 | flags = wdata->state.flags; |
| 884 | spin_unlock_irq(&wdata->state.lock); |
| 885 | |
| 886 | wiimote_cmd_acquire_noint(wdata); |
| 887 | |
| 888 | /* If MP is used and active, but the extension is not, we expect: |
| 889 | * read_mp_mapped() == WIIMOTE_MP_SINGLE |
| 890 | * state.flags == !EXT_ACTIVE && !MP_PLUGGED && MP_ACTIVE |
| 891 | * We do not check EXT_PLUGGED because it might change during |
| 892 | * initialization of MP without extensions. |
| 893 | * - If MP is unplugged/replugged, read_mp_mapped() fails |
| 894 | * - If EXT is plugged, MP_PLUGGED will get set */ |
| 895 | if (wdata->state.exttype == WIIMOTE_EXT_NONE && |
| 896 | wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) { |
| 897 | type = wiimote_cmd_read_mp_mapped(wdata); |
| 898 | ret = type == WIIMOTE_MP_SINGLE; |
| 899 | |
| 900 | spin_lock_irq(&wdata->state.lock); |
| 901 | ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE); |
| 902 | ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED); |
| 903 | ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE); |
| 904 | spin_unlock_irq(&wdata->state.lock); |
| 905 | |
| 906 | if (!ret) |
| 907 | hid_dbg(wdata->hdev, "state left: !EXT && MP\n"); |
| 908 | |
| 909 | /* while MP is mapped, we get EXT_PLUGGED events */ |
| 910 | poll_mp = false; |
| 911 | |
| 912 | goto out_release; |
| 913 | } |
| 914 | |
| 915 | /* If MP is unused, but the extension port is used, we expect: |
| 916 | * read_ext == state.exttype |
| 917 | * state.flags == !MP_ACTIVE && EXT_ACTIVE |
| 918 | * - If MP is plugged/unplugged, our timer detects it |
| 919 | * - If EXT is unplugged/replugged, EXT_ACTIVE will become unset */ |
| 920 | if (!(flags & WIIPROTO_FLAG_MP_USED) && |
| 921 | wdata->state.exttype != WIIMOTE_EXT_NONE) { |
| 922 | type = wiimote_cmd_read_ext(wdata, data); |
| 923 | ret = type == wdata->state.exttype; |
| 924 | |
| 925 | spin_lock_irq(&wdata->state.lock); |
| 926 | ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE); |
| 927 | ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE); |
| 928 | spin_unlock_irq(&wdata->state.lock); |
| 929 | |
| 930 | if (!ret) |
| 931 | hid_dbg(wdata->hdev, "state left: EXT && !MP\n"); |
| 932 | |
| 933 | /* poll MP for hotplug events */ |
| 934 | poll_mp = true; |
| 935 | |
| 936 | goto out_release; |
| 937 | } |
| 938 | |
| 939 | /* If neither MP nor an extension are used, we expect: |
| 940 | * read_ext() == WIIMOTE_EXT_NONE |
| 941 | * state.flags == !MP_ACTIVE && !EXT_ACTIVE && !EXT_PLUGGED |
| 942 | * No need to perform any action in this case as everything is |
| 943 | * disabled already. |
| 944 | * - If MP is plugged/unplugged, our timer detects it |
| 945 | * - If EXT is plugged, EXT_PLUGGED will be set */ |
| 946 | if (!(flags & WIIPROTO_FLAG_MP_USED) && |
| 947 | wdata->state.exttype == WIIMOTE_EXT_NONE) { |
| 948 | type = wiimote_cmd_read_ext(wdata, data); |
| 949 | ret = type == wdata->state.exttype; |
| 950 | |
| 951 | spin_lock_irq(&wdata->state.lock); |
| 952 | ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE); |
| 953 | ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE); |
| 954 | ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED); |
| 955 | spin_unlock_irq(&wdata->state.lock); |
| 956 | |
| 957 | if (!ret) |
| 958 | hid_dbg(wdata->hdev, "state left: !EXT && !MP\n"); |
| 959 | |
| 960 | /* poll MP for hotplug events */ |
| 961 | poll_mp = true; |
| 962 | |
| 963 | goto out_release; |
| 964 | } |
| 965 | |
| 966 | /* The trickiest part is if both EXT and MP are active. We cannot read |
| 967 | * the EXT ID, anymore, because MP is mapped over it. However, we use |
| 968 | * a handy trick here: |
| 969 | * - EXT_ACTIVE is unset whenever !MP_PLUGGED is sent |
| 970 | * MP_PLUGGED might be re-sent again before we are scheduled, but |
| 971 | * EXT_ACTIVE will stay unset. |
| 972 | * So it is enough to check for mp_mapped() and MP_ACTIVE and |
| 973 | * EXT_ACTIVE. EXT_PLUGGED is a sanity check. */ |
| 974 | if (wdata->state.exttype != WIIMOTE_EXT_NONE && |
| 975 | wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) { |
| 976 | type = wiimote_cmd_read_mp_mapped(wdata); |
| 977 | ret = type != WIIMOTE_MP_NONE; |
| 978 | ret = ret && type != WIIMOTE_MP_UNKNOWN; |
| 979 | ret = ret && type != WIIMOTE_MP_SINGLE; |
| 980 | |
| 981 | spin_lock_irq(&wdata->state.lock); |
| 982 | ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED); |
| 983 | ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE); |
| 984 | ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE); |
| 985 | spin_unlock_irq(&wdata->state.lock); |
| 986 | |
| 987 | if (!ret) |
| 988 | hid_dbg(wdata->hdev, "state left: EXT && MP\n"); |
| 989 | |
| 990 | /* while MP is mapped, we get EXT_PLUGGED events */ |
| 991 | poll_mp = false; |
| 992 | |
| 993 | goto out_release; |
| 994 | } |
| 995 | |
| 996 | /* unknown state */ |
| 997 | ret = false; |
| 998 | |
| 999 | out_release: |
| 1000 | wiimote_cmd_release(wdata); |
| 1001 | |
| 1002 | /* only poll for MP if requested and if state didn't change */ |
| 1003 | if (ret && poll_mp) |
| 1004 | wiimote_init_poll_mp(wdata); |
| 1005 | |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
| 1009 | static const char *wiimote_exttype_names[WIIMOTE_EXT_NUM] = { |
| 1010 | [WIIMOTE_EXT_NONE] = "None", |
| 1011 | [WIIMOTE_EXT_UNKNOWN] = "Unknown", |
| 1012 | }; |
| 1013 | |
| 1014 | /* |
| 1015 | * Handle hotplug events |
| 1016 | * If we receive an hotplug event and the device-check failed, we deinitialize |
| 1017 | * the extension ports, re-read all extension IDs and set the device into |
| 1018 | * the desired state. This involves mapping MP into the main extension |
| 1019 | * registers, setting up extension passthrough modes and initializing the |
| 1020 | * requested extensions. |
| 1021 | */ |
| 1022 | static void wiimote_init_hotplug(struct wiimote_data *wdata) |
| 1023 | { |
| 1024 | __u8 exttype, extdata[6], mpdata[6]; |
| 1025 | __u32 flags; |
| 1026 | bool mp; |
| 1027 | |
| 1028 | hid_dbg(wdata->hdev, "detect extensions..\n"); |
| 1029 | |
| 1030 | wiimote_cmd_acquire_noint(wdata); |
| 1031 | |
| 1032 | spin_lock_irq(&wdata->state.lock); |
| 1033 | |
| 1034 | /* get state snapshot that we will then work on */ |
| 1035 | flags = wdata->state.flags; |
| 1036 | |
| 1037 | /* disable event forwarding temporarily */ |
| 1038 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE; |
| 1039 | wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE; |
| 1040 | |
| 1041 | spin_unlock_irq(&wdata->state.lock); |
| 1042 | |
| 1043 | /* init extension and MP (deactivates current extension or MP) */ |
| 1044 | wiimote_cmd_init_ext(wdata); |
| 1045 | wiimote_cmd_init_mp(wdata); |
| 1046 | mp = wiimote_cmd_read_mp(wdata, mpdata); |
| 1047 | exttype = wiimote_cmd_read_ext(wdata, extdata); |
| 1048 | |
| 1049 | wiimote_cmd_release(wdata); |
| 1050 | |
| 1051 | /* load/unload extension module if it changed */ |
| 1052 | if (exttype != wdata->state.exttype) { |
| 1053 | /* unload previous extension */ |
| 1054 | wiimote_ext_unload(wdata); |
| 1055 | |
| 1056 | if (exttype == WIIMOTE_EXT_UNKNOWN) { |
| 1057 | hid_info(wdata->hdev, "cannot detect extension; %02x:%02x %02x:%02x %02x:%02x\n", |
| 1058 | extdata[0], extdata[1], extdata[2], |
| 1059 | extdata[3], extdata[4], extdata[5]); |
| 1060 | } else if (exttype == WIIMOTE_EXT_NONE) { |
| 1061 | spin_lock_irq(&wdata->state.lock); |
| 1062 | wdata->state.exttype = WIIMOTE_EXT_NONE; |
| 1063 | spin_unlock_irq(&wdata->state.lock); |
| 1064 | } else { |
| 1065 | hid_info(wdata->hdev, "detected extension: %s\n", |
| 1066 | wiimote_exttype_names[exttype]); |
| 1067 | /* try loading new extension */ |
| 1068 | wiimote_ext_load(wdata, exttype); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | /* load/unload MP module if it changed */ |
| 1073 | if (mp) { |
| 1074 | if (!wdata->state.mp) { |
| 1075 | hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n"); |
| 1076 | wiimote_mp_load(wdata); |
| 1077 | } |
| 1078 | } else if (wdata->state.mp) { |
| 1079 | wiimote_mp_unload(wdata); |
| 1080 | } |
| 1081 | |
| 1082 | /* if MP is not used, do not map or activate it */ |
| 1083 | if (!(flags & WIIPROTO_FLAG_MP_USED)) |
| 1084 | mp = false; |
| 1085 | |
| 1086 | /* map MP into main extension registers if used */ |
| 1087 | if (mp) { |
| 1088 | wiimote_cmd_acquire_noint(wdata); |
| 1089 | wiimote_cmd_map_mp(wdata, exttype); |
| 1090 | wiimote_cmd_release(wdata); |
| 1091 | |
| 1092 | /* delete MP hotplug timer */ |
| 1093 | del_timer_sync(&wdata->timer); |
| 1094 | } else { |
| 1095 | /* reschedule MP hotplug timer */ |
| 1096 | mod_timer(&wdata->timer, jiffies + HZ * 4); |
| 1097 | } |
| 1098 | |
| 1099 | spin_lock_irq(&wdata->state.lock); |
| 1100 | |
| 1101 | /* enable data forwarding again and set expected hotplug state */ |
| 1102 | if (mp) { |
| 1103 | wdata->state.flags |= WIIPROTO_FLAG_MP_ACTIVE; |
| 1104 | if (wdata->state.exttype == WIIMOTE_EXT_NONE) { |
| 1105 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED; |
| 1106 | wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED; |
| 1107 | } else { |
| 1108 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED; |
| 1109 | wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED; |
| 1110 | wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE; |
| 1111 | } |
| 1112 | } else if (wdata->state.exttype != WIIMOTE_EXT_NONE) { |
| 1113 | wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE; |
| 1114 | } |
| 1115 | |
| 1116 | /* request status report for hotplug state updates */ |
| 1117 | wiiproto_req_status(wdata); |
| 1118 | |
| 1119 | spin_unlock_irq(&wdata->state.lock); |
| 1120 | |
| 1121 | hid_dbg(wdata->hdev, "detected extensions: MP: %d EXT: %d\n", |
| 1122 | wdata->state.mp, wdata->state.exttype); |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | static void wiimote_init_worker(struct work_struct *work) |
| 1126 | { |
| 1127 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 1128 | init_worker); |
| 1129 | |
| 1130 | if (wdata->state.devtype == WIIMOTE_DEV_PENDING) |
| 1131 | wiimote_init_detect(wdata); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1132 | if (!wiimote_init_check(wdata)) |
| 1133 | wiimote_init_hotplug(wdata); |
| 1134 | } |
| 1135 | |
| 1136 | void __wiimote_schedule(struct wiimote_data *wdata) |
| 1137 | { |
| 1138 | if (!(wdata->state.flags & WIIPROTO_FLAG_EXITING)) |
| 1139 | schedule_work(&wdata->init_worker); |
| 1140 | } |
| 1141 | |
| 1142 | static void wiimote_schedule(struct wiimote_data *wdata) |
| 1143 | { |
| 1144 | unsigned long flags; |
| 1145 | |
| 1146 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1147 | __wiimote_schedule(wdata); |
| 1148 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1149 | } |
| 1150 | |
| 1151 | static void wiimote_init_timeout(unsigned long arg) |
| 1152 | { |
| 1153 | struct wiimote_data *wdata = (void*)arg; |
| 1154 | |
| 1155 | wiimote_schedule(wdata); |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | /* protocol handlers */ |
| 1159 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 1160 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
| 1161 | { |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 1162 | const __u8 *iter, *mods; |
| 1163 | const struct wiimod_ops *ops; |
| 1164 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1165 | ops = wiimod_ext_table[wdata->state.exttype]; |
| 1166 | if (ops->in_keys) { |
| 1167 | ops->in_keys(wdata, payload); |
| 1168 | return; |
| 1169 | } |
| 1170 | |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 1171 | mods = wiimote_devtype_mods[wdata->state.devtype]; |
| 1172 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 1173 | ops = wiimod_table[*iter]; |
| 1174 | if (ops->in_keys) { |
| 1175 | ops->in_keys(wdata, payload); |
| 1176 | break; |
| 1177 | } |
| 1178 | } |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 1179 | } |
| 1180 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1181 | static void handler_accel(struct wiimote_data *wdata, const __u8 *payload) |
| 1182 | { |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 1183 | const __u8 *iter, *mods; |
| 1184 | const struct wiimod_ops *ops; |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1185 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1186 | ops = wiimod_ext_table[wdata->state.exttype]; |
| 1187 | if (ops->in_accel) { |
| 1188 | ops->in_accel(wdata, payload); |
| 1189 | return; |
| 1190 | } |
| 1191 | |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 1192 | mods = wiimote_devtype_mods[wdata->state.devtype]; |
| 1193 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 1194 | ops = wiimod_table[*iter]; |
| 1195 | if (ops->in_accel) { |
| 1196 | ops->in_accel(wdata, payload); |
| 1197 | break; |
| 1198 | } |
| 1199 | } |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1202 | static bool valid_ext_handler(const struct wiimod_ops *ops, size_t len) |
| 1203 | { |
| 1204 | if (!ops->in_ext) |
| 1205 | return false; |
| 1206 | if ((ops->flags & WIIMOD_FLAG_EXT8) && len < 8) |
| 1207 | return false; |
| 1208 | if ((ops->flags & WIIMOD_FLAG_EXT16) && len < 16) |
| 1209 | return false; |
| 1210 | |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
| 1214 | static void handler_ext(struct wiimote_data *wdata, const __u8 *payload, |
| 1215 | size_t len) |
| 1216 | { |
| 1217 | const __u8 *iter, *mods; |
| 1218 | const struct wiimod_ops *ops; |
| 1219 | bool is_mp; |
| 1220 | |
| 1221 | if (len < 6) |
| 1222 | return; |
| 1223 | |
| 1224 | /* if MP is active, track MP slot hotplugging */ |
| 1225 | if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) { |
| 1226 | /* this bit is set for invalid events (eg. during hotplug) */ |
| 1227 | if (payload[5] & 0x01) |
| 1228 | return; |
| 1229 | |
| 1230 | if (payload[4] & 0x01) { |
| 1231 | if (!(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED)) { |
| 1232 | hid_dbg(wdata->hdev, "MP hotplug: 1\n"); |
| 1233 | wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED; |
| 1234 | __wiimote_schedule(wdata); |
| 1235 | } |
| 1236 | } else { |
| 1237 | if (wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED) { |
| 1238 | hid_dbg(wdata->hdev, "MP hotplug: 0\n"); |
| 1239 | wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED; |
| 1240 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE; |
| 1241 | __wiimote_schedule(wdata); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | /* detect MP data that is sent interleaved with EXT data */ |
| 1246 | is_mp = payload[5] & 0x02; |
| 1247 | } else { |
| 1248 | is_mp = false; |
| 1249 | } |
| 1250 | |
| 1251 | /* ignore EXT events if no extension is active */ |
| 1252 | if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE) && !is_mp) |
| 1253 | return; |
| 1254 | |
| 1255 | /* try forwarding to extension handler, first */ |
| 1256 | ops = wiimod_ext_table[wdata->state.exttype]; |
| 1257 | if (is_mp && ops->in_mp) { |
| 1258 | ops->in_mp(wdata, payload); |
| 1259 | return; |
| 1260 | } else if (!is_mp && valid_ext_handler(ops, len)) { |
| 1261 | ops->in_ext(wdata, payload); |
| 1262 | return; |
| 1263 | } |
| 1264 | |
| 1265 | /* try forwarding to MP handler */ |
| 1266 | ops = &wiimod_mp; |
| 1267 | if (is_mp && ops->in_mp) { |
| 1268 | ops->in_mp(wdata, payload); |
| 1269 | return; |
| 1270 | } else if (!is_mp && valid_ext_handler(ops, len)) { |
| 1271 | ops->in_ext(wdata, payload); |
| 1272 | return; |
| 1273 | } |
| 1274 | |
| 1275 | /* try forwarding to loaded modules */ |
| 1276 | mods = wiimote_devtype_mods[wdata->state.devtype]; |
| 1277 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 1278 | ops = wiimod_table[*iter]; |
| 1279 | if (is_mp && ops->in_mp) { |
| 1280 | ops->in_mp(wdata, payload); |
| 1281 | return; |
| 1282 | } else if (!is_mp && valid_ext_handler(ops, len)) { |
| 1283 | ops->in_ext(wdata, payload); |
| 1284 | return; |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 1289 | #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0) |
| 1290 | #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1) |
| 1291 | #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2) |
| 1292 | #define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3) |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1293 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 1294 | static void handler_ir(struct wiimote_data *wdata, const __u8 *payload, |
| 1295 | bool packed, unsigned int id) |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1296 | { |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 1297 | const __u8 *iter, *mods; |
| 1298 | const struct wiimod_ops *ops; |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1299 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1300 | ops = wiimod_ext_table[wdata->state.exttype]; |
| 1301 | if (ops->in_ir) { |
| 1302 | ops->in_ir(wdata, payload, packed, id); |
| 1303 | return; |
| 1304 | } |
| 1305 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 1306 | mods = wiimote_devtype_mods[wdata->state.devtype]; |
| 1307 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 1308 | ops = wiimod_table[*iter]; |
| 1309 | if (ops->in_ir) { |
| 1310 | ops->in_ir(wdata, payload, packed, id); |
| 1311 | break; |
| 1312 | } |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1313 | } |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1314 | } |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1315 | |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1316 | /* reduced status report with "BB BB" key data only */ |
| 1317 | static void handler_status_K(struct wiimote_data *wdata, |
| 1318 | const __u8 *payload) |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 1319 | { |
| 1320 | handler_keys(wdata, payload); |
| 1321 | |
| 1322 | /* on status reports the drm is reset so we need to resend the drm */ |
| 1323 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* extended status report with "BB BB LF 00 00 VV" data */ |
| 1327 | static void handler_status(struct wiimote_data *wdata, const __u8 *payload) |
| 1328 | { |
| 1329 | handler_status_K(wdata, payload); |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 1330 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1331 | /* update extension status */ |
| 1332 | if (payload[2] & 0x02) { |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1333 | if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED)) { |
| 1334 | hid_dbg(wdata->hdev, "EXT hotplug: 1\n"); |
| 1335 | wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED; |
| 1336 | __wiimote_schedule(wdata); |
| 1337 | } |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1338 | } else { |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1339 | if (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED) { |
| 1340 | hid_dbg(wdata->hdev, "EXT hotplug: 0\n"); |
| 1341 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED; |
| 1342 | wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED; |
| 1343 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE; |
| 1344 | wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE; |
| 1345 | __wiimote_schedule(wdata); |
| 1346 | } |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1347 | } |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 1348 | |
David Herrmann | 6b80bb9 | 2013-05-05 23:12:49 +0200 | [diff] [blame] | 1349 | wdata->state.cmd_battery = payload[5]; |
| 1350 | if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 1351 | wiimote_cmd_complete(wdata); |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1354 | /* reduced generic report with "BB BB" key data only */ |
| 1355 | static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload) |
| 1356 | { |
| 1357 | handler_keys(wdata, payload); |
| 1358 | } |
| 1359 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 1360 | static void handler_data(struct wiimote_data *wdata, const __u8 *payload) |
| 1361 | { |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 1362 | __u16 offset = payload[3] << 8 | payload[4]; |
| 1363 | __u8 size = (payload[2] >> 4) + 1; |
| 1364 | __u8 err = payload[2] & 0x0f; |
| 1365 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 1366 | handler_keys(wdata, payload); |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 1367 | |
| 1368 | if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) { |
| 1369 | if (err) |
| 1370 | size = 0; |
| 1371 | else if (size > wdata->state.cmd_read_size) |
| 1372 | size = wdata->state.cmd_read_size; |
| 1373 | |
| 1374 | wdata->state.cmd_read_size = size; |
| 1375 | if (wdata->state.cmd_read_buf) |
| 1376 | memcpy(wdata->state.cmd_read_buf, &payload[5], size); |
| 1377 | wiimote_cmd_complete(wdata); |
| 1378 | } |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 1379 | } |
| 1380 | |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 1381 | static void handler_return(struct wiimote_data *wdata, const __u8 *payload) |
| 1382 | { |
| 1383 | __u8 err = payload[3]; |
| 1384 | __u8 cmd = payload[2]; |
| 1385 | |
| 1386 | handler_keys(wdata, payload); |
| 1387 | |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 1388 | if (wiimote_cmd_pending(wdata, cmd, 0)) { |
| 1389 | wdata->state.cmd_err = err; |
| 1390 | wiimote_cmd_complete(wdata); |
| 1391 | } else if (err) { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 1392 | hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err, |
| 1393 | cmd); |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 1394 | } |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 1395 | } |
| 1396 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1397 | static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload) |
| 1398 | { |
| 1399 | handler_keys(wdata, payload); |
| 1400 | handler_accel(wdata, payload); |
| 1401 | } |
| 1402 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1403 | static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload) |
| 1404 | { |
| 1405 | handler_keys(wdata, payload); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1406 | handler_ext(wdata, &payload[2], 8); |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1407 | } |
| 1408 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1409 | static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload) |
| 1410 | { |
| 1411 | handler_keys(wdata, payload); |
| 1412 | handler_accel(wdata, payload); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1413 | ir_to_input0(wdata, &payload[5], false); |
| 1414 | ir_to_input1(wdata, &payload[8], false); |
| 1415 | ir_to_input2(wdata, &payload[11], false); |
| 1416 | ir_to_input3(wdata, &payload[14], false); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1419 | static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload) |
| 1420 | { |
| 1421 | handler_keys(wdata, payload); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1422 | handler_ext(wdata, &payload[2], 19); |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1425 | static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload) |
| 1426 | { |
| 1427 | handler_keys(wdata, payload); |
| 1428 | ir_to_input0(wdata, &payload[2], false); |
| 1429 | ir_to_input1(wdata, &payload[4], true); |
| 1430 | ir_to_input2(wdata, &payload[7], false); |
| 1431 | ir_to_input3(wdata, &payload[9], true); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1432 | handler_ext(wdata, &payload[12], 9); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload) |
| 1436 | { |
| 1437 | handler_keys(wdata, payload); |
| 1438 | handler_accel(wdata, payload); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1439 | handler_ext(wdata, &payload[5], 16); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload) |
| 1443 | { |
| 1444 | handler_keys(wdata, payload); |
| 1445 | handler_accel(wdata, payload); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1446 | ir_to_input0(wdata, &payload[5], false); |
| 1447 | ir_to_input1(wdata, &payload[7], true); |
| 1448 | ir_to_input2(wdata, &payload[10], false); |
| 1449 | ir_to_input3(wdata, &payload[12], true); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1450 | handler_ext(wdata, &payload[15], 6); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1453 | static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload) |
| 1454 | { |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1455 | handler_ext(wdata, payload, 21); |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1456 | } |
| 1457 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1458 | static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload) |
| 1459 | { |
| 1460 | handler_keys(wdata, payload); |
| 1461 | |
| 1462 | wdata->state.accel_split[0] = payload[2]; |
| 1463 | wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20); |
| 1464 | wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1465 | |
| 1466 | ir_to_input0(wdata, &payload[3], false); |
| 1467 | ir_to_input1(wdata, &payload[12], false); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload) |
| 1471 | { |
| 1472 | __u8 buf[5]; |
| 1473 | |
| 1474 | handler_keys(wdata, payload); |
| 1475 | |
| 1476 | wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02); |
| 1477 | wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08); |
| 1478 | |
| 1479 | buf[0] = 0; |
| 1480 | buf[1] = 0; |
| 1481 | buf[2] = wdata->state.accel_split[0]; |
| 1482 | buf[3] = payload[2]; |
| 1483 | buf[4] = wdata->state.accel_split[1]; |
| 1484 | handler_accel(wdata, buf); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1485 | |
| 1486 | ir_to_input2(wdata, &payload[3], false); |
| 1487 | ir_to_input3(wdata, &payload[12], false); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1488 | } |
| 1489 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1490 | struct wiiproto_handler { |
| 1491 | __u8 id; |
| 1492 | size_t size; |
| 1493 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); |
| 1494 | }; |
| 1495 | |
| 1496 | static struct wiiproto_handler handlers[] = { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 1497 | { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1498 | { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K }, |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 1499 | { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1500 | { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K }, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 1501 | { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1502 | { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K }, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 1503 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1504 | { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1505 | { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1506 | { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1507 | { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1508 | { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1509 | { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1510 | { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1511 | { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1512 | { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1513 | { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K }, |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1514 | { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1515 | { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1516 | { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1517 | { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1518 | { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1519 | { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 }, |
| 1520 | { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 }, |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1521 | { .id = 0 } |
| 1522 | }; |
| 1523 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1524 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 1525 | u8 *raw_data, int size) |
| 1526 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 1527 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1528 | struct wiiproto_handler *h; |
| 1529 | int i; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1530 | unsigned long flags; |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 1531 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1532 | if (size < 1) |
| 1533 | return -EINVAL; |
| 1534 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1535 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1536 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1537 | for (i = 0; handlers[i].id; ++i) { |
| 1538 | h = &handlers[i]; |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1539 | if (h->id == raw_data[0] && h->size < size) { |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1540 | h->func(wdata, &raw_data[1]); |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1541 | break; |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1542 | } |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1543 | } |
| 1544 | |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 1545 | if (!handlers[i].id) |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1546 | hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0], |
| 1547 | size); |
| 1548 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1549 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1550 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1551 | return 0; |
| 1552 | } |
| 1553 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1554 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 1555 | { |
| 1556 | struct wiimote_data *wdata; |
| 1557 | |
| 1558 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 1559 | if (!wdata) |
| 1560 | return NULL; |
| 1561 | |
| 1562 | wdata->hdev = hdev; |
| 1563 | hid_set_drvdata(hdev, wdata); |
| 1564 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 1565 | spin_lock_init(&wdata->queue.lock); |
| 1566 | INIT_WORK(&wdata->queue.worker, wiimote_queue_worker); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 1567 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1568 | spin_lock_init(&wdata->state.lock); |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 1569 | init_completion(&wdata->state.ready); |
| 1570 | mutex_init(&wdata->state.sync); |
David Herrmann | 43d782a | 2011-11-17 14:12:12 +0100 | [diff] [blame] | 1571 | wdata->state.drm = WIIPROTO_REQ_DRM_K; |
David Herrmann | 6b80bb9 | 2013-05-05 23:12:49 +0200 | [diff] [blame] | 1572 | wdata->state.cmd_battery = 0xff; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1573 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1574 | INIT_WORK(&wdata->init_worker, wiimote_init_worker); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1575 | setup_timer(&wdata->timer, wiimote_init_timeout, (long)wdata); |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1576 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1577 | return wdata; |
| 1578 | } |
| 1579 | |
| 1580 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 1581 | { |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1582 | unsigned long flags; |
| 1583 | |
David Herrmann | 43e5e7c | 2011-11-17 14:12:10 +0100 | [diff] [blame] | 1584 | wiidebug_deinit(wdata); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1585 | |
| 1586 | /* prevent init_worker from being scheduled again */ |
| 1587 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1588 | wdata->state.flags |= WIIPROTO_FLAG_EXITING; |
| 1589 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1590 | |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 1591 | cancel_work_sync(&wdata->init_worker); |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1592 | del_timer_sync(&wdata->timer); |
| 1593 | |
| 1594 | wiimote_mp_unload(wdata); |
| 1595 | wiimote_ext_unload(wdata); |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 1596 | wiimote_modules_unload(wdata); |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 1597 | cancel_work_sync(&wdata->queue.worker); |
David Herrmann | 5682b1a | 2013-05-05 23:12:47 +0200 | [diff] [blame] | 1598 | hid_hw_close(wdata->hdev); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1599 | hid_hw_stop(wdata->hdev); |
| 1600 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1601 | kfree(wdata); |
| 1602 | } |
| 1603 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1604 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 1605 | const struct hid_device_id *id) |
| 1606 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1607 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1608 | int ret; |
| 1609 | |
David Herrmann | 90120d6 | 2011-11-17 14:12:14 +0100 | [diff] [blame] | 1610 | hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS; |
| 1611 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1612 | wdata = wiimote_create(hdev); |
| 1613 | if (!wdata) { |
| 1614 | hid_err(hdev, "Can't alloc device\n"); |
| 1615 | return -ENOMEM; |
| 1616 | } |
| 1617 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1618 | ret = hid_parse(hdev); |
| 1619 | if (ret) { |
| 1620 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1621 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 1625 | if (ret) { |
| 1626 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1627 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1628 | } |
| 1629 | |
David Herrmann | 5682b1a | 2013-05-05 23:12:47 +0200 | [diff] [blame] | 1630 | ret = hid_hw_open(hdev); |
| 1631 | if (ret) { |
| 1632 | hid_err(hdev, "cannot start hardware I/O\n"); |
| 1633 | goto err_stop; |
| 1634 | } |
| 1635 | |
David Herrmann | 43e5e7c | 2011-11-17 14:12:10 +0100 | [diff] [blame] | 1636 | ret = wiidebug_init(wdata); |
| 1637 | if (ret) |
| 1638 | goto err_free; |
| 1639 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1640 | hid_info(hdev, "New device registered\n"); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1641 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1642 | /* schedule device detection */ |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame^] | 1643 | wiimote_schedule(wdata); |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1644 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1645 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1646 | |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1647 | err_free: |
| 1648 | wiimote_destroy(wdata); |
| 1649 | return ret; |
| 1650 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1651 | err_stop: |
| 1652 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1653 | err: |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1654 | input_free_device(wdata->ir); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1655 | input_free_device(wdata->accel); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1656 | kfree(wdata); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1657 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 1661 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1662 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 1663 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1664 | hid_info(hdev, "Device removed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1665 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 1669 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 1670 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
David Herrmann | a33042f | 2013-04-02 19:58:35 +0200 | [diff] [blame] | 1671 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 1672 | USB_DEVICE_ID_NINTENDO_WIIMOTE2) }, |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1673 | { } |
| 1674 | }; |
| 1675 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 1676 | |
| 1677 | static struct hid_driver wiimote_hid_driver = { |
| 1678 | .name = "wiimote", |
| 1679 | .id_table = wiimote_hid_devices, |
| 1680 | .probe = wiimote_hid_probe, |
| 1681 | .remove = wiimote_hid_remove, |
| 1682 | .raw_event = wiimote_hid_event, |
| 1683 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 1684 | module_hid_driver(wiimote_hid_driver); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1685 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1686 | MODULE_LICENSE("GPL"); |
| 1687 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
David Herrmann | 92eda7e | 2013-05-05 23:12:45 +0200 | [diff] [blame] | 1688 | MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals"); |