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