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