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