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. |
| 182 | */ |
| 183 | static __u8 select_drm(struct wiimote_data *wdata) |
| 184 | { |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 185 | __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR; |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 186 | bool ext = wiiext_active(wdata); |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 187 | |
| 188 | if (ir == WIIPROTO_FLAG_IR_BASIC) { |
| 189 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) |
| 190 | return WIIPROTO_REQ_DRM_KAIE; |
| 191 | else |
| 192 | return WIIPROTO_REQ_DRM_KIE; |
| 193 | } else if (ir == WIIPROTO_FLAG_IR_EXT) { |
| 194 | return WIIPROTO_REQ_DRM_KAI; |
| 195 | } else if (ir == WIIPROTO_FLAG_IR_FULL) { |
| 196 | return WIIPROTO_REQ_DRM_SKAI1; |
| 197 | } else { |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 198 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) { |
| 199 | if (ext) |
| 200 | return WIIPROTO_REQ_DRM_KAE; |
| 201 | else |
| 202 | return WIIPROTO_REQ_DRM_KA; |
| 203 | } else { |
| 204 | if (ext) |
| 205 | return WIIPROTO_REQ_DRM_KE; |
| 206 | else |
| 207 | return WIIPROTO_REQ_DRM_K; |
| 208 | } |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 209 | } |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 210 | } |
| 211 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 212 | void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 213 | { |
| 214 | __u8 cmd[3]; |
| 215 | |
| 216 | if (drm == WIIPROTO_REQ_NULL) |
| 217 | drm = select_drm(wdata); |
| 218 | |
| 219 | cmd[0] = WIIPROTO_REQ_DRM; |
| 220 | cmd[1] = 0; |
| 221 | cmd[2] = drm; |
| 222 | |
David Herrmann | 43d782a | 2011-11-17 14:12:12 +0100 | [diff] [blame] | 223 | wdata->state.drm = drm; |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 224 | wiiproto_keep_rumble(wdata, &cmd[1]); |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 225 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 226 | } |
| 227 | |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 228 | void wiiproto_req_status(struct wiimote_data *wdata) |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 229 | { |
| 230 | __u8 cmd[2]; |
| 231 | |
| 232 | cmd[0] = WIIPROTO_REQ_SREQ; |
| 233 | cmd[1] = 0; |
| 234 | |
| 235 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 236 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 237 | } |
| 238 | |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 239 | void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel) |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 240 | { |
| 241 | accel = !!accel; |
| 242 | if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) |
| 243 | return; |
| 244 | |
| 245 | if (accel) |
| 246 | wdata->state.flags |= WIIPROTO_FLAG_ACCEL; |
| 247 | else |
| 248 | wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL; |
| 249 | |
| 250 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 251 | } |
| 252 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame^] | 253 | void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags) |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame] | 254 | { |
| 255 | __u8 cmd[2]; |
| 256 | |
| 257 | cmd[0] = WIIPROTO_REQ_IR1; |
| 258 | cmd[1] = flags; |
| 259 | |
| 260 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 261 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 262 | } |
| 263 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame^] | 264 | void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags) |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame] | 265 | { |
| 266 | __u8 cmd[2]; |
| 267 | |
| 268 | cmd[0] = WIIPROTO_REQ_IR2; |
| 269 | cmd[1] = flags; |
| 270 | |
| 271 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 272 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 273 | } |
| 274 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 275 | #define wiiproto_req_wreg(wdata, os, buf, sz) \ |
| 276 | wiiproto_req_wmem((wdata), false, (os), (buf), (sz)) |
| 277 | |
| 278 | #define wiiproto_req_weeprom(wdata, os, buf, sz) \ |
| 279 | wiiproto_req_wmem((wdata), true, (os), (buf), (sz)) |
| 280 | |
| 281 | static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom, |
| 282 | __u32 offset, const __u8 *buf, __u8 size) |
| 283 | { |
| 284 | __u8 cmd[22]; |
| 285 | |
| 286 | if (size > 16 || size == 0) { |
| 287 | hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | memset(cmd, 0, sizeof(cmd)); |
| 292 | cmd[0] = WIIPROTO_REQ_WMEM; |
| 293 | cmd[2] = (offset >> 16) & 0xff; |
| 294 | cmd[3] = (offset >> 8) & 0xff; |
| 295 | cmd[4] = offset & 0xff; |
| 296 | cmd[5] = size; |
| 297 | memcpy(&cmd[6], buf, size); |
| 298 | |
| 299 | if (!eeprom) |
| 300 | cmd[1] |= 0x04; |
| 301 | |
| 302 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 303 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 304 | } |
| 305 | |
David Herrmann | 1d3452c | 2011-11-17 14:12:11 +0100 | [diff] [blame] | 306 | void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset, |
| 307 | __u16 size) |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 308 | { |
| 309 | __u8 cmd[7]; |
| 310 | |
| 311 | if (size == 0) { |
| 312 | hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | cmd[0] = WIIPROTO_REQ_RMEM; |
| 317 | cmd[1] = 0; |
| 318 | cmd[2] = (offset >> 16) & 0xff; |
| 319 | cmd[3] = (offset >> 8) & 0xff; |
| 320 | cmd[4] = offset & 0xff; |
| 321 | cmd[5] = (size >> 8) & 0xff; |
| 322 | cmd[6] = size & 0xff; |
| 323 | |
| 324 | if (!eeprom) |
| 325 | cmd[1] |= 0x04; |
| 326 | |
| 327 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 328 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 329 | } |
| 330 | |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 331 | /* requries the cmd-mutex to be held */ |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 332 | int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 333 | const __u8 *wmem, __u8 size) |
| 334 | { |
| 335 | unsigned long flags; |
| 336 | int ret; |
| 337 | |
| 338 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 339 | wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0); |
| 340 | wiiproto_req_wreg(wdata, offset, wmem, size); |
| 341 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 342 | |
| 343 | ret = wiimote_cmd_wait(wdata); |
| 344 | if (!ret && wdata->state.cmd_err) |
| 345 | ret = -EIO; |
| 346 | |
| 347 | return ret; |
| 348 | } |
| 349 | |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 350 | /* requries the cmd-mutex to be held */ |
| 351 | ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem, |
| 352 | __u8 size) |
| 353 | { |
| 354 | unsigned long flags; |
| 355 | ssize_t ret; |
| 356 | |
| 357 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 358 | wdata->state.cmd_read_size = size; |
| 359 | wdata->state.cmd_read_buf = rmem; |
| 360 | wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff); |
| 361 | wiiproto_req_rreg(wdata, offset, size); |
| 362 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 363 | |
| 364 | ret = wiimote_cmd_wait(wdata); |
| 365 | |
| 366 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 367 | wdata->state.cmd_read_buf = NULL; |
| 368 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 369 | |
| 370 | if (!ret) { |
| 371 | if (wdata->state.cmd_read_size == 0) |
| 372 | ret = -EIO; |
| 373 | else |
| 374 | ret = wdata->state.cmd_read_size; |
| 375 | } |
| 376 | |
| 377 | return ret; |
| 378 | } |
| 379 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 380 | /* requires the cmd-mutex to be held */ |
| 381 | static int wiimote_cmd_init_ext(struct wiimote_data *wdata) |
| 382 | { |
| 383 | __u8 wmem; |
| 384 | int ret; |
| 385 | |
| 386 | /* initialize extension */ |
| 387 | wmem = 0x55; |
| 388 | ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem)); |
| 389 | if (ret) |
| 390 | return ret; |
| 391 | |
| 392 | /* disable default encryption */ |
| 393 | wmem = 0x0; |
| 394 | ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem)); |
| 395 | if (ret) |
| 396 | return ret; |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /* requires the cmd-mutex to be held */ |
| 402 | static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata) |
| 403 | { |
| 404 | __u8 rmem[6]; |
| 405 | int ret; |
| 406 | |
| 407 | /* read extension ID */ |
| 408 | ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6); |
| 409 | if (ret != 6) |
| 410 | return WIIMOTE_EXT_NONE; |
| 411 | |
| 412 | if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff && |
| 413 | rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff) |
| 414 | return WIIMOTE_EXT_NONE; |
| 415 | |
| 416 | return WIIMOTE_EXT_UNKNOWN; |
| 417 | } |
| 418 | |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 419 | /* device module handling */ |
| 420 | |
| 421 | static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = { |
| 422 | [WIIMOTE_DEV_PENDING] = (const __u8[]){ |
| 423 | WIIMOD_NULL, |
| 424 | }, |
| 425 | [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){ |
| 426 | WIIMOD_NULL, |
| 427 | }, |
| 428 | [WIIMOTE_DEV_GENERIC] = (const __u8[]){ |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 429 | WIIMOD_KEYS, |
| 430 | WIIMOD_RUMBLE, |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 431 | WIIMOD_BATTERY, |
David Herrmann | 6c5ae01 | 2013-05-05 23:12:54 +0200 | [diff] [blame] | 432 | WIIMOD_LED1, |
| 433 | WIIMOD_LED2, |
| 434 | WIIMOD_LED3, |
| 435 | WIIMOD_LED4, |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 436 | WIIMOD_ACCEL, |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame^] | 437 | WIIMOD_IR, |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 438 | WIIMOD_NULL, |
| 439 | }, |
| 440 | [WIIMOTE_DEV_GEN10] = (const __u8[]){ |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 441 | WIIMOD_KEYS, |
| 442 | WIIMOD_RUMBLE, |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 443 | WIIMOD_BATTERY, |
David Herrmann | 6c5ae01 | 2013-05-05 23:12:54 +0200 | [diff] [blame] | 444 | WIIMOD_LED1, |
| 445 | WIIMOD_LED2, |
| 446 | WIIMOD_LED3, |
| 447 | WIIMOD_LED4, |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 448 | WIIMOD_ACCEL, |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame^] | 449 | WIIMOD_IR, |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 450 | WIIMOD_NULL, |
| 451 | }, |
| 452 | [WIIMOTE_DEV_GEN20] = (const __u8[]){ |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 453 | WIIMOD_KEYS, |
| 454 | WIIMOD_RUMBLE, |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 455 | WIIMOD_BATTERY, |
David Herrmann | 6c5ae01 | 2013-05-05 23:12:54 +0200 | [diff] [blame] | 456 | WIIMOD_LED1, |
| 457 | WIIMOD_LED2, |
| 458 | WIIMOD_LED3, |
| 459 | WIIMOD_LED4, |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 460 | WIIMOD_ACCEL, |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame^] | 461 | WIIMOD_IR, |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 462 | WIIMOD_NULL, |
| 463 | }, |
| 464 | }; |
| 465 | |
| 466 | static void wiimote_modules_load(struct wiimote_data *wdata, |
| 467 | unsigned int devtype) |
| 468 | { |
| 469 | bool need_input = false; |
| 470 | const __u8 *mods, *iter; |
| 471 | const struct wiimod_ops *ops; |
| 472 | int ret; |
| 473 | |
| 474 | mods = wiimote_devtype_mods[devtype]; |
| 475 | |
| 476 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 477 | if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) { |
| 478 | need_input = true; |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if (need_input) { |
| 484 | wdata->input = input_allocate_device(); |
| 485 | if (!wdata->input) |
| 486 | return; |
| 487 | |
| 488 | input_set_drvdata(wdata->input, wdata); |
| 489 | wdata->input->dev.parent = &wdata->hdev->dev; |
| 490 | wdata->input->id.bustype = wdata->hdev->bus; |
| 491 | wdata->input->id.vendor = wdata->hdev->vendor; |
| 492 | wdata->input->id.product = wdata->hdev->product; |
| 493 | wdata->input->id.version = wdata->hdev->version; |
| 494 | wdata->input->name = WIIMOTE_NAME; |
| 495 | } |
| 496 | |
| 497 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 498 | ops = wiimod_table[*iter]; |
| 499 | if (!ops->probe) |
| 500 | continue; |
| 501 | |
| 502 | ret = ops->probe(ops, wdata); |
| 503 | if (ret) |
| 504 | goto error; |
| 505 | } |
| 506 | |
| 507 | if (wdata->input) { |
| 508 | ret = input_register_device(wdata->input); |
| 509 | if (ret) |
| 510 | goto error; |
| 511 | } |
| 512 | |
| 513 | spin_lock_irq(&wdata->state.lock); |
| 514 | wdata->state.devtype = devtype; |
| 515 | spin_unlock_irq(&wdata->state.lock); |
| 516 | return; |
| 517 | |
| 518 | error: |
| 519 | for ( ; iter-- != mods; ) { |
| 520 | ops = wiimod_table[*iter]; |
| 521 | if (ops->remove) |
| 522 | ops->remove(ops, wdata); |
| 523 | } |
| 524 | |
| 525 | if (wdata->input) { |
| 526 | input_free_device(wdata->input); |
| 527 | wdata->input = NULL; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | static void wiimote_modules_unload(struct wiimote_data *wdata) |
| 532 | { |
| 533 | const __u8 *mods, *iter; |
| 534 | const struct wiimod_ops *ops; |
| 535 | unsigned long flags; |
| 536 | |
| 537 | mods = wiimote_devtype_mods[wdata->state.devtype]; |
| 538 | |
| 539 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 540 | wdata->state.devtype = WIIMOTE_DEV_UNKNOWN; |
| 541 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 542 | |
| 543 | /* find end of list */ |
| 544 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) |
| 545 | /* empty */ ; |
| 546 | |
| 547 | if (wdata->input) { |
| 548 | input_get_device(wdata->input); |
| 549 | input_unregister_device(wdata->input); |
| 550 | } |
| 551 | |
| 552 | for ( ; iter-- != mods; ) { |
| 553 | ops = wiimod_table[*iter]; |
| 554 | if (ops->remove) |
| 555 | ops->remove(ops, wdata); |
| 556 | } |
| 557 | |
| 558 | if (wdata->input) { |
| 559 | input_put_device(wdata->input); |
| 560 | wdata->input = NULL; |
| 561 | } |
| 562 | } |
| 563 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 564 | /* device (re-)initialization and detection */ |
| 565 | |
| 566 | static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = { |
| 567 | [WIIMOTE_DEV_PENDING] = "Pending", |
| 568 | [WIIMOTE_DEV_UNKNOWN] = "Unknown", |
| 569 | [WIIMOTE_DEV_GENERIC] = "Generic", |
| 570 | [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)", |
| 571 | [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)", |
| 572 | }; |
| 573 | |
| 574 | /* Try to guess the device type based on all collected information. We |
| 575 | * first try to detect by static extension types, then VID/PID and the |
| 576 | * device name. If we cannot detect the device, we use |
| 577 | * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */ |
| 578 | static void wiimote_init_set_type(struct wiimote_data *wdata, |
| 579 | __u8 exttype) |
| 580 | { |
| 581 | __u8 devtype = WIIMOTE_DEV_GENERIC; |
| 582 | __u16 vendor, product; |
| 583 | const char *name; |
| 584 | |
| 585 | vendor = wdata->hdev->vendor; |
| 586 | product = wdata->hdev->product; |
| 587 | name = wdata->hdev->name; |
| 588 | |
| 589 | if (!strcmp(name, "Nintendo RVL-CNT-01")) { |
| 590 | devtype = WIIMOTE_DEV_GEN10; |
| 591 | goto done; |
| 592 | } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) { |
| 593 | devtype = WIIMOTE_DEV_GEN20; |
| 594 | goto done; |
| 595 | } |
| 596 | |
| 597 | if (vendor == USB_VENDOR_ID_NINTENDO) { |
| 598 | if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) { |
| 599 | devtype = WIIMOTE_DEV_GEN10; |
| 600 | goto done; |
| 601 | } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) { |
| 602 | devtype = WIIMOTE_DEV_GEN20; |
| 603 | goto done; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | done: |
| 608 | if (devtype == WIIMOTE_DEV_GENERIC) |
| 609 | hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n", |
| 610 | name, vendor, product, exttype); |
| 611 | else |
| 612 | hid_info(wdata->hdev, "detected device: %s\n", |
| 613 | wiimote_devtype_names[devtype]); |
| 614 | |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 615 | wiimote_modules_load(wdata, devtype); |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static void wiimote_init_detect(struct wiimote_data *wdata) |
| 619 | { |
| 620 | __u8 exttype = WIIMOTE_EXT_NONE; |
| 621 | bool ext; |
| 622 | int ret; |
| 623 | |
| 624 | wiimote_cmd_acquire_noint(wdata); |
| 625 | |
| 626 | spin_lock_irq(&wdata->state.lock); |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 627 | wdata->state.devtype = WIIMOTE_DEV_UNKNOWN; |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 628 | wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0); |
| 629 | wiiproto_req_status(wdata); |
| 630 | spin_unlock_irq(&wdata->state.lock); |
| 631 | |
| 632 | ret = wiimote_cmd_wait_noint(wdata); |
| 633 | if (ret) |
| 634 | goto out_release; |
| 635 | |
| 636 | spin_lock_irq(&wdata->state.lock); |
| 637 | ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED; |
| 638 | spin_unlock_irq(&wdata->state.lock); |
| 639 | |
| 640 | if (!ext) |
| 641 | goto out_release; |
| 642 | |
| 643 | wiimote_cmd_init_ext(wdata); |
| 644 | exttype = wiimote_cmd_read_ext(wdata); |
| 645 | |
| 646 | out_release: |
| 647 | wiimote_cmd_release(wdata); |
| 648 | wiimote_init_set_type(wdata, exttype); |
| 649 | } |
| 650 | |
| 651 | static void wiimote_init_worker(struct work_struct *work) |
| 652 | { |
| 653 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 654 | init_worker); |
| 655 | |
| 656 | if (wdata->state.devtype == WIIMOTE_DEV_PENDING) |
| 657 | wiimote_init_detect(wdata); |
| 658 | } |
| 659 | |
| 660 | /* protocol handlers */ |
| 661 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 662 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
| 663 | { |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 664 | const __u8 *iter, *mods; |
| 665 | const struct wiimod_ops *ops; |
| 666 | |
| 667 | mods = wiimote_devtype_mods[wdata->state.devtype]; |
| 668 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 669 | ops = wiimod_table[*iter]; |
| 670 | if (ops->in_keys) { |
| 671 | ops->in_keys(wdata, payload); |
| 672 | break; |
| 673 | } |
| 674 | } |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 675 | } |
| 676 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 677 | static void handler_accel(struct wiimote_data *wdata, const __u8 *payload) |
| 678 | { |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 679 | const __u8 *iter, *mods; |
| 680 | const struct wiimod_ops *ops; |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 681 | |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 682 | mods = wiimote_devtype_mods[wdata->state.devtype]; |
| 683 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 684 | ops = wiimod_table[*iter]; |
| 685 | if (ops->in_accel) { |
| 686 | ops->in_accel(wdata, payload); |
| 687 | break; |
| 688 | } |
| 689 | } |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 690 | } |
| 691 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame^] | 692 | #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0) |
| 693 | #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1) |
| 694 | #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2) |
| 695 | #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] | 696 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame^] | 697 | static void handler_ir(struct wiimote_data *wdata, const __u8 *payload, |
| 698 | bool packed, unsigned int id) |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 699 | { |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame^] | 700 | const __u8 *iter, *mods; |
| 701 | const struct wiimod_ops *ops; |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 702 | |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame^] | 703 | mods = wiimote_devtype_mods[wdata->state.devtype]; |
| 704 | for (iter = mods; *iter != WIIMOD_NULL; ++iter) { |
| 705 | ops = wiimod_table[*iter]; |
| 706 | if (ops->in_ir) { |
| 707 | ops->in_ir(wdata, payload, packed, id); |
| 708 | break; |
| 709 | } |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 710 | } |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 711 | } |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 712 | |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 713 | /* reduced status report with "BB BB" key data only */ |
| 714 | static void handler_status_K(struct wiimote_data *wdata, |
| 715 | const __u8 *payload) |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 716 | { |
| 717 | handler_keys(wdata, payload); |
| 718 | |
| 719 | /* on status reports the drm is reset so we need to resend the drm */ |
| 720 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /* extended status report with "BB BB LF 00 00 VV" data */ |
| 724 | static void handler_status(struct wiimote_data *wdata, const __u8 *payload) |
| 725 | { |
| 726 | handler_status_K(wdata, payload); |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 727 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 728 | /* update extension status */ |
| 729 | if (payload[2] & 0x02) { |
| 730 | wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED; |
| 731 | wiiext_event(wdata, true); |
| 732 | } else { |
| 733 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED; |
| 734 | wiiext_event(wdata, false); |
| 735 | } |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 736 | |
David Herrmann | 6b80bb9 | 2013-05-05 23:12:49 +0200 | [diff] [blame] | 737 | wdata->state.cmd_battery = payload[5]; |
| 738 | if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 739 | wiimote_cmd_complete(wdata); |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 740 | } |
| 741 | |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 742 | /* reduced generic report with "BB BB" key data only */ |
| 743 | static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload) |
| 744 | { |
| 745 | handler_keys(wdata, payload); |
| 746 | } |
| 747 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 748 | static void handler_data(struct wiimote_data *wdata, const __u8 *payload) |
| 749 | { |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 750 | __u16 offset = payload[3] << 8 | payload[4]; |
| 751 | __u8 size = (payload[2] >> 4) + 1; |
| 752 | __u8 err = payload[2] & 0x0f; |
| 753 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 754 | handler_keys(wdata, payload); |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 755 | |
| 756 | if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) { |
| 757 | if (err) |
| 758 | size = 0; |
| 759 | else if (size > wdata->state.cmd_read_size) |
| 760 | size = wdata->state.cmd_read_size; |
| 761 | |
| 762 | wdata->state.cmd_read_size = size; |
| 763 | if (wdata->state.cmd_read_buf) |
| 764 | memcpy(wdata->state.cmd_read_buf, &payload[5], size); |
| 765 | wiimote_cmd_complete(wdata); |
| 766 | } |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 767 | } |
| 768 | |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 769 | static void handler_return(struct wiimote_data *wdata, const __u8 *payload) |
| 770 | { |
| 771 | __u8 err = payload[3]; |
| 772 | __u8 cmd = payload[2]; |
| 773 | |
| 774 | handler_keys(wdata, payload); |
| 775 | |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 776 | if (wiimote_cmd_pending(wdata, cmd, 0)) { |
| 777 | wdata->state.cmd_err = err; |
| 778 | wiimote_cmd_complete(wdata); |
| 779 | } else if (err) { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 780 | hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err, |
| 781 | cmd); |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 782 | } |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 783 | } |
| 784 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 785 | static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload) |
| 786 | { |
| 787 | handler_keys(wdata, payload); |
| 788 | handler_accel(wdata, payload); |
| 789 | } |
| 790 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 791 | static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload) |
| 792 | { |
| 793 | handler_keys(wdata, payload); |
David Herrmann | 0b6815d | 2011-11-17 14:12:06 +0100 | [diff] [blame] | 794 | wiiext_handle(wdata, &payload[2]); |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 795 | } |
| 796 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 797 | static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload) |
| 798 | { |
| 799 | handler_keys(wdata, payload); |
| 800 | handler_accel(wdata, payload); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 801 | ir_to_input0(wdata, &payload[5], false); |
| 802 | ir_to_input1(wdata, &payload[8], false); |
| 803 | ir_to_input2(wdata, &payload[11], false); |
| 804 | ir_to_input3(wdata, &payload[14], false); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 805 | } |
| 806 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 807 | static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload) |
| 808 | { |
| 809 | handler_keys(wdata, payload); |
David Herrmann | 0b6815d | 2011-11-17 14:12:06 +0100 | [diff] [blame] | 810 | wiiext_handle(wdata, &payload[2]); |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 811 | } |
| 812 | |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 813 | static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload) |
| 814 | { |
| 815 | handler_keys(wdata, payload); |
| 816 | ir_to_input0(wdata, &payload[2], false); |
| 817 | ir_to_input1(wdata, &payload[4], true); |
| 818 | ir_to_input2(wdata, &payload[7], false); |
| 819 | ir_to_input3(wdata, &payload[9], true); |
David Herrmann | 0b6815d | 2011-11-17 14:12:06 +0100 | [diff] [blame] | 820 | wiiext_handle(wdata, &payload[12]); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload) |
| 824 | { |
| 825 | handler_keys(wdata, payload); |
| 826 | handler_accel(wdata, payload); |
David Herrmann | 0b6815d | 2011-11-17 14:12:06 +0100 | [diff] [blame] | 827 | wiiext_handle(wdata, &payload[5]); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload) |
| 831 | { |
| 832 | handler_keys(wdata, payload); |
| 833 | handler_accel(wdata, payload); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 834 | ir_to_input0(wdata, &payload[5], false); |
| 835 | ir_to_input1(wdata, &payload[7], true); |
| 836 | ir_to_input2(wdata, &payload[10], false); |
| 837 | ir_to_input3(wdata, &payload[12], true); |
David Herrmann | 0b6815d | 2011-11-17 14:12:06 +0100 | [diff] [blame] | 838 | wiiext_handle(wdata, &payload[15]); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 839 | } |
| 840 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 841 | static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload) |
| 842 | { |
David Herrmann | 0b6815d | 2011-11-17 14:12:06 +0100 | [diff] [blame] | 843 | wiiext_handle(wdata, payload); |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 844 | } |
| 845 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 846 | static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload) |
| 847 | { |
| 848 | handler_keys(wdata, payload); |
| 849 | |
| 850 | wdata->state.accel_split[0] = payload[2]; |
| 851 | wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20); |
| 852 | wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 853 | |
| 854 | ir_to_input0(wdata, &payload[3], false); |
| 855 | ir_to_input1(wdata, &payload[12], false); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload) |
| 859 | { |
| 860 | __u8 buf[5]; |
| 861 | |
| 862 | handler_keys(wdata, payload); |
| 863 | |
| 864 | wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02); |
| 865 | wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08); |
| 866 | |
| 867 | buf[0] = 0; |
| 868 | buf[1] = 0; |
| 869 | buf[2] = wdata->state.accel_split[0]; |
| 870 | buf[3] = payload[2]; |
| 871 | buf[4] = wdata->state.accel_split[1]; |
| 872 | handler_accel(wdata, buf); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 873 | |
| 874 | ir_to_input2(wdata, &payload[3], false); |
| 875 | ir_to_input3(wdata, &payload[12], false); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 876 | } |
| 877 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 878 | struct wiiproto_handler { |
| 879 | __u8 id; |
| 880 | size_t size; |
| 881 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); |
| 882 | }; |
| 883 | |
| 884 | static struct wiiproto_handler handlers[] = { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 885 | { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 886 | { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K }, |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 887 | { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 888 | { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K }, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 889 | { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 890 | { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K }, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 891 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 892 | { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 893 | { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 894 | { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 895 | { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 896 | { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 897 | { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 898 | { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 899 | { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 900 | { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 901 | { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K }, |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 902 | { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 903 | { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 904 | { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE }, |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 905 | { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 906 | { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 907 | { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 }, |
| 908 | { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 }, |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 909 | { .id = 0 } |
| 910 | }; |
| 911 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 912 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 913 | u8 *raw_data, int size) |
| 914 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 915 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 916 | struct wiiproto_handler *h; |
| 917 | int i; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 918 | unsigned long flags; |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 919 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 920 | if (size < 1) |
| 921 | return -EINVAL; |
| 922 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 923 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 924 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 925 | for (i = 0; handlers[i].id; ++i) { |
| 926 | h = &handlers[i]; |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 927 | if (h->id == raw_data[0] && h->size < size) { |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 928 | h->func(wdata, &raw_data[1]); |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 929 | break; |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 930 | } |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 931 | } |
| 932 | |
David Herrmann | 2d44e3d | 2013-04-02 19:58:36 +0200 | [diff] [blame] | 933 | if (!handlers[i].id) |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 934 | hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0], |
| 935 | size); |
| 936 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 937 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 938 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 939 | return 0; |
| 940 | } |
| 941 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 942 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 943 | { |
| 944 | struct wiimote_data *wdata; |
| 945 | |
| 946 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 947 | if (!wdata) |
| 948 | return NULL; |
| 949 | |
| 950 | wdata->hdev = hdev; |
| 951 | hid_set_drvdata(hdev, wdata); |
| 952 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 953 | spin_lock_init(&wdata->queue.lock); |
| 954 | INIT_WORK(&wdata->queue.worker, wiimote_queue_worker); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 955 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 956 | spin_lock_init(&wdata->state.lock); |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 957 | init_completion(&wdata->state.ready); |
| 958 | mutex_init(&wdata->state.sync); |
David Herrmann | 43d782a | 2011-11-17 14:12:12 +0100 | [diff] [blame] | 959 | wdata->state.drm = WIIPROTO_REQ_DRM_K; |
David Herrmann | 6b80bb9 | 2013-05-05 23:12:49 +0200 | [diff] [blame] | 960 | wdata->state.cmd_battery = 0xff; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 961 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 962 | INIT_WORK(&wdata->init_worker, wiimote_init_worker); |
| 963 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 964 | return wdata; |
| 965 | } |
| 966 | |
| 967 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 968 | { |
David Herrmann | 43e5e7c | 2011-11-17 14:12:10 +0100 | [diff] [blame] | 969 | wiidebug_deinit(wdata); |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 970 | wiiext_deinit(wdata); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 971 | |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 972 | cancel_work_sync(&wdata->init_worker); |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 973 | wiimote_modules_unload(wdata); |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 974 | cancel_work_sync(&wdata->queue.worker); |
David Herrmann | 5682b1a | 2013-05-05 23:12:47 +0200 | [diff] [blame] | 975 | hid_hw_close(wdata->hdev); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 976 | hid_hw_stop(wdata->hdev); |
| 977 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 978 | kfree(wdata); |
| 979 | } |
| 980 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 981 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 982 | const struct hid_device_id *id) |
| 983 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 984 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 985 | int ret; |
| 986 | |
David Herrmann | 90120d6 | 2011-11-17 14:12:14 +0100 | [diff] [blame] | 987 | hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS; |
| 988 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 989 | wdata = wiimote_create(hdev); |
| 990 | if (!wdata) { |
| 991 | hid_err(hdev, "Can't alloc device\n"); |
| 992 | return -ENOMEM; |
| 993 | } |
| 994 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 995 | ret = hid_parse(hdev); |
| 996 | if (ret) { |
| 997 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 998 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 1002 | if (ret) { |
| 1003 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1004 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
David Herrmann | 5682b1a | 2013-05-05 23:12:47 +0200 | [diff] [blame] | 1007 | ret = hid_hw_open(hdev); |
| 1008 | if (ret) { |
| 1009 | hid_err(hdev, "cannot start hardware I/O\n"); |
| 1010 | goto err_stop; |
| 1011 | } |
| 1012 | |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 1013 | ret = wiiext_init(wdata); |
| 1014 | if (ret) |
| 1015 | goto err_free; |
| 1016 | |
David Herrmann | 43e5e7c | 2011-11-17 14:12:10 +0100 | [diff] [blame] | 1017 | ret = wiidebug_init(wdata); |
| 1018 | if (ret) |
| 1019 | goto err_free; |
| 1020 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1021 | hid_info(hdev, "New device registered\n"); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1022 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 1023 | /* schedule device detection */ |
| 1024 | schedule_work(&wdata->init_worker); |
| 1025 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1026 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1027 | |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1028 | err_free: |
| 1029 | wiimote_destroy(wdata); |
| 1030 | return ret; |
| 1031 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1032 | err_stop: |
| 1033 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1034 | err: |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1035 | input_free_device(wdata->ir); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1036 | input_free_device(wdata->accel); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1037 | kfree(wdata); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1038 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 1042 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1043 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 1044 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1045 | hid_info(hdev, "Device removed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1046 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 1050 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 1051 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
David Herrmann | a33042f | 2013-04-02 19:58:35 +0200 | [diff] [blame] | 1052 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 1053 | USB_DEVICE_ID_NINTENDO_WIIMOTE2) }, |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1054 | { } |
| 1055 | }; |
| 1056 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 1057 | |
| 1058 | static struct hid_driver wiimote_hid_driver = { |
| 1059 | .name = "wiimote", |
| 1060 | .id_table = wiimote_hid_devices, |
| 1061 | .probe = wiimote_hid_probe, |
| 1062 | .remove = wiimote_hid_remove, |
| 1063 | .raw_event = wiimote_hid_event, |
| 1064 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 1065 | module_hid_driver(wiimote_hid_driver); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1066 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1067 | MODULE_LICENSE("GPL"); |
| 1068 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
David Herrmann | 92eda7e | 2013-05-05 23:12:45 +0200 | [diff] [blame] | 1069 | MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals"); |