David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for Nintendo Wiimote devices |
| 3 | * Copyright (c) 2011 David Herrmann |
| 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 | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 17 | #include <linux/leds.h> |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 18 | #include <linux/module.h> |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 19 | #include <linux/mutex.h> |
David Herrmann | 6591d75 | 2011-09-06 13:50:39 +0200 | [diff] [blame] | 20 | #include <linux/power_supply.h> |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 21 | #include <linux/spinlock.h> |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 22 | #include "hid-ids.h" |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 23 | |
David Herrmann | b22e00f | 2011-09-06 13:50:40 +0200 | [diff] [blame] | 24 | #define WIIMOTE_VERSION "0.2" |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 25 | #define WIIMOTE_NAME "Nintendo Wii Remote" |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 26 | #define WIIMOTE_BUFSIZE 32 |
| 27 | |
| 28 | struct wiimote_buf { |
| 29 | __u8 data[HID_MAX_BUFFER_SIZE]; |
| 30 | size_t size; |
| 31 | }; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 32 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 33 | struct wiimote_state { |
| 34 | spinlock_t lock; |
| 35 | __u8 flags; |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 36 | __u8 accel_split[2]; |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 37 | |
| 38 | /* synchronous cmd requests */ |
| 39 | struct mutex sync; |
| 40 | struct completion ready; |
| 41 | int cmd; |
| 42 | __u32 opt; |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 43 | |
| 44 | /* results of synchronous requests */ |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 45 | __u8 cmd_battery; |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 46 | __u8 cmd_err; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 49 | struct wiimote_data { |
| 50 | struct hid_device *hdev; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 51 | struct input_dev *input; |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 52 | struct led_classdev *leds[4]; |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 53 | struct input_dev *accel; |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 54 | struct input_dev *ir; |
David Herrmann | 6591d75 | 2011-09-06 13:50:39 +0200 | [diff] [blame] | 55 | struct power_supply battery; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 56 | |
| 57 | spinlock_t qlock; |
| 58 | __u8 head; |
| 59 | __u8 tail; |
| 60 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; |
| 61 | struct work_struct worker; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 62 | |
| 63 | struct wiimote_state state; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 66 | #define WIIPROTO_FLAG_LED1 0x01 |
| 67 | #define WIIPROTO_FLAG_LED2 0x02 |
| 68 | #define WIIPROTO_FLAG_LED3 0x04 |
| 69 | #define WIIPROTO_FLAG_LED4 0x08 |
| 70 | #define WIIPROTO_FLAG_RUMBLE 0x10 |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 71 | #define WIIPROTO_FLAG_ACCEL 0x20 |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 72 | #define WIIPROTO_FLAG_IR_BASIC 0x40 |
| 73 | #define WIIPROTO_FLAG_IR_EXT 0x80 |
| 74 | #define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */ |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 75 | #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ |
| 76 | WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 77 | #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \ |
| 78 | WIIPROTO_FLAG_IR_FULL) |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 79 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 80 | /* return flag for led \num */ |
| 81 | #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1)) |
| 82 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 83 | enum wiiproto_reqs { |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 84 | WIIPROTO_REQ_NULL = 0x0, |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 85 | WIIPROTO_REQ_RUMBLE = 0x10, |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 86 | WIIPROTO_REQ_LED = 0x11, |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 87 | WIIPROTO_REQ_DRM = 0x12, |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame] | 88 | WIIPROTO_REQ_IR1 = 0x13, |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 89 | WIIPROTO_REQ_SREQ = 0x15, |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 90 | WIIPROTO_REQ_WMEM = 0x16, |
| 91 | WIIPROTO_REQ_RMEM = 0x17, |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame] | 92 | WIIPROTO_REQ_IR2 = 0x1a, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 93 | WIIPROTO_REQ_STATUS = 0x20, |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 94 | WIIPROTO_REQ_DATA = 0x21, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 95 | WIIPROTO_REQ_RETURN = 0x22, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 96 | WIIPROTO_REQ_DRM_K = 0x30, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 97 | WIIPROTO_REQ_DRM_KA = 0x31, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 98 | WIIPROTO_REQ_DRM_KE = 0x32, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 99 | WIIPROTO_REQ_DRM_KAI = 0x33, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 100 | WIIPROTO_REQ_DRM_KEE = 0x34, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 101 | WIIPROTO_REQ_DRM_KAE = 0x35, |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 102 | WIIPROTO_REQ_DRM_KIE = 0x36, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 103 | WIIPROTO_REQ_DRM_KAIE = 0x37, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 104 | WIIPROTO_REQ_DRM_E = 0x3d, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 105 | WIIPROTO_REQ_DRM_SKAI1 = 0x3e, |
| 106 | WIIPROTO_REQ_DRM_SKAI2 = 0x3f, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | enum wiiproto_keys { |
| 110 | WIIPROTO_KEY_LEFT, |
| 111 | WIIPROTO_KEY_RIGHT, |
| 112 | WIIPROTO_KEY_UP, |
| 113 | WIIPROTO_KEY_DOWN, |
| 114 | WIIPROTO_KEY_PLUS, |
| 115 | WIIPROTO_KEY_MINUS, |
| 116 | WIIPROTO_KEY_ONE, |
| 117 | WIIPROTO_KEY_TWO, |
| 118 | WIIPROTO_KEY_A, |
| 119 | WIIPROTO_KEY_B, |
| 120 | WIIPROTO_KEY_HOME, |
| 121 | WIIPROTO_KEY_COUNT |
| 122 | }; |
| 123 | |
| 124 | static __u16 wiiproto_keymap[] = { |
| 125 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ |
| 126 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ |
| 127 | KEY_UP, /* WIIPROTO_KEY_UP */ |
| 128 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ |
| 129 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ |
| 130 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ |
| 131 | BTN_1, /* WIIPROTO_KEY_ONE */ |
| 132 | BTN_2, /* WIIPROTO_KEY_TWO */ |
| 133 | BTN_A, /* WIIPROTO_KEY_A */ |
| 134 | BTN_B, /* WIIPROTO_KEY_B */ |
| 135 | BTN_MODE, /* WIIPROTO_KEY_HOME */ |
| 136 | }; |
| 137 | |
David Herrmann | 6591d75 | 2011-09-06 13:50:39 +0200 | [diff] [blame] | 138 | static enum power_supply_property wiimote_battery_props[] = { |
| 139 | POWER_SUPPLY_PROP_CAPACITY |
| 140 | }; |
| 141 | |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 142 | /* requires the state.lock spinlock to be held */ |
| 143 | static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd, |
| 144 | __u32 opt) |
| 145 | { |
| 146 | return wdata->state.cmd == cmd && wdata->state.opt == opt; |
| 147 | } |
| 148 | |
| 149 | /* requires the state.lock spinlock to be held */ |
| 150 | static inline void wiimote_cmd_complete(struct wiimote_data *wdata) |
| 151 | { |
| 152 | wdata->state.cmd = WIIPROTO_REQ_NULL; |
| 153 | complete(&wdata->state.ready); |
| 154 | } |
| 155 | |
| 156 | static inline int wiimote_cmd_acquire(struct wiimote_data *wdata) |
| 157 | { |
| 158 | return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0; |
| 159 | } |
| 160 | |
| 161 | /* requires the state.lock spinlock to be held */ |
| 162 | static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd, |
| 163 | __u32 opt) |
| 164 | { |
| 165 | INIT_COMPLETION(wdata->state.ready); |
| 166 | wdata->state.cmd = cmd; |
| 167 | wdata->state.opt = opt; |
| 168 | } |
| 169 | |
| 170 | static inline void wiimote_cmd_release(struct wiimote_data *wdata) |
| 171 | { |
| 172 | mutex_unlock(&wdata->state.sync); |
| 173 | } |
| 174 | |
| 175 | static inline int wiimote_cmd_wait(struct wiimote_data *wdata) |
| 176 | { |
| 177 | int ret; |
| 178 | |
| 179 | ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ); |
| 180 | if (ret < 0) |
| 181 | return -ERESTARTSYS; |
| 182 | else if (ret == 0) |
| 183 | return -EIO; |
| 184 | else |
| 185 | return 0; |
| 186 | } |
| 187 | |
David Herrmann | 0c218f1 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 188 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
| 189 | size_t count) |
| 190 | { |
| 191 | __u8 *buf; |
| 192 | ssize_t ret; |
| 193 | |
| 194 | if (!hdev->hid_output_raw_report) |
| 195 | return -ENODEV; |
| 196 | |
| 197 | buf = kmemdup(buffer, count, GFP_KERNEL); |
| 198 | if (!buf) |
| 199 | return -ENOMEM; |
| 200 | |
| 201 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); |
| 202 | |
| 203 | kfree(buf); |
| 204 | return ret; |
| 205 | } |
| 206 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 207 | static void wiimote_worker(struct work_struct *work) |
| 208 | { |
| 209 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 210 | worker); |
| 211 | unsigned long flags; |
| 212 | |
| 213 | spin_lock_irqsave(&wdata->qlock, flags); |
| 214 | |
| 215 | while (wdata->head != wdata->tail) { |
| 216 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 217 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, |
| 218 | wdata->outq[wdata->tail].size); |
| 219 | spin_lock_irqsave(&wdata->qlock, flags); |
| 220 | |
| 221 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; |
| 222 | } |
| 223 | |
| 224 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 225 | } |
| 226 | |
| 227 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, |
| 228 | size_t count) |
| 229 | { |
| 230 | unsigned long flags; |
| 231 | __u8 newhead; |
| 232 | |
| 233 | if (count > HID_MAX_BUFFER_SIZE) { |
| 234 | hid_warn(wdata->hdev, "Sending too large output report\n"); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Copy new request into our output queue and check whether the |
| 240 | * queue is full. If it is full, discard this request. |
| 241 | * If it is empty we need to start a new worker that will |
| 242 | * send out the buffer to the hid device. |
| 243 | * If the queue is not empty, then there must be a worker |
| 244 | * that is currently sending out our buffer and this worker |
| 245 | * will reschedule itself until the queue is empty. |
| 246 | */ |
| 247 | |
| 248 | spin_lock_irqsave(&wdata->qlock, flags); |
| 249 | |
| 250 | memcpy(wdata->outq[wdata->head].data, buffer, count); |
| 251 | wdata->outq[wdata->head].size = count; |
| 252 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; |
| 253 | |
| 254 | if (wdata->head == wdata->tail) { |
| 255 | wdata->head = newhead; |
| 256 | schedule_work(&wdata->worker); |
| 257 | } else if (newhead != wdata->tail) { |
| 258 | wdata->head = newhead; |
| 259 | } else { |
| 260 | hid_warn(wdata->hdev, "Output queue is full"); |
| 261 | } |
| 262 | |
| 263 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 264 | } |
| 265 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 266 | /* |
| 267 | * This sets the rumble bit on the given output report if rumble is |
| 268 | * currently enabled. |
| 269 | * \cmd1 must point to the second byte in the output report => &cmd[1] |
| 270 | * This must be called on nearly every output report before passing it |
| 271 | * into the output queue! |
| 272 | */ |
| 273 | static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1) |
| 274 | { |
| 275 | if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE) |
| 276 | *cmd1 |= 0x01; |
| 277 | } |
| 278 | |
| 279 | static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble) |
| 280 | { |
| 281 | __u8 cmd[2]; |
| 282 | |
| 283 | rumble = !!rumble; |
| 284 | if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE)) |
| 285 | return; |
| 286 | |
| 287 | if (rumble) |
| 288 | wdata->state.flags |= WIIPROTO_FLAG_RUMBLE; |
| 289 | else |
| 290 | wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE; |
| 291 | |
| 292 | cmd[0] = WIIPROTO_REQ_RUMBLE; |
| 293 | cmd[1] = 0; |
| 294 | |
| 295 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 296 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 297 | } |
| 298 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 299 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
| 300 | { |
| 301 | __u8 cmd[2]; |
| 302 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 303 | leds &= WIIPROTO_FLAGS_LEDS; |
| 304 | if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds) |
| 305 | return; |
| 306 | wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds; |
| 307 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 308 | cmd[0] = WIIPROTO_REQ_LED; |
| 309 | cmd[1] = 0; |
| 310 | |
| 311 | if (leds & WIIPROTO_FLAG_LED1) |
| 312 | cmd[1] |= 0x10; |
| 313 | if (leds & WIIPROTO_FLAG_LED2) |
| 314 | cmd[1] |= 0x20; |
| 315 | if (leds & WIIPROTO_FLAG_LED3) |
| 316 | cmd[1] |= 0x40; |
| 317 | if (leds & WIIPROTO_FLAG_LED4) |
| 318 | cmd[1] |= 0x80; |
| 319 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 320 | wiiproto_keep_rumble(wdata, &cmd[1]); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 321 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 322 | } |
| 323 | |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 324 | /* |
| 325 | * Check what peripherals of the wiimote are currently |
| 326 | * active and select a proper DRM that supports all of |
| 327 | * the requested data inputs. |
| 328 | */ |
| 329 | static __u8 select_drm(struct wiimote_data *wdata) |
| 330 | { |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 331 | __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR; |
| 332 | |
| 333 | if (ir == WIIPROTO_FLAG_IR_BASIC) { |
| 334 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) |
| 335 | return WIIPROTO_REQ_DRM_KAIE; |
| 336 | else |
| 337 | return WIIPROTO_REQ_DRM_KIE; |
| 338 | } else if (ir == WIIPROTO_FLAG_IR_EXT) { |
| 339 | return WIIPROTO_REQ_DRM_KAI; |
| 340 | } else if (ir == WIIPROTO_FLAG_IR_FULL) { |
| 341 | return WIIPROTO_REQ_DRM_SKAI1; |
| 342 | } else { |
| 343 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) |
| 344 | return WIIPROTO_REQ_DRM_KA; |
| 345 | else |
| 346 | return WIIPROTO_REQ_DRM_K; |
| 347 | } |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) |
| 351 | { |
| 352 | __u8 cmd[3]; |
| 353 | |
| 354 | if (drm == WIIPROTO_REQ_NULL) |
| 355 | drm = select_drm(wdata); |
| 356 | |
| 357 | cmd[0] = WIIPROTO_REQ_DRM; |
| 358 | cmd[1] = 0; |
| 359 | cmd[2] = drm; |
| 360 | |
David Herrmann | c003ec2 | 2011-09-06 13:50:26 +0200 | [diff] [blame] | 361 | wiiproto_keep_rumble(wdata, &cmd[1]); |
David Herrmann | 2cb5e4b | 2011-08-17 11:43:23 +0200 | [diff] [blame] | 362 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 363 | } |
| 364 | |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 365 | static void wiiproto_req_status(struct wiimote_data *wdata) |
| 366 | { |
| 367 | __u8 cmd[2]; |
| 368 | |
| 369 | cmd[0] = WIIPROTO_REQ_SREQ; |
| 370 | cmd[1] = 0; |
| 371 | |
| 372 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 373 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 374 | } |
| 375 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 376 | static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel) |
| 377 | { |
| 378 | accel = !!accel; |
| 379 | if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) |
| 380 | return; |
| 381 | |
| 382 | if (accel) |
| 383 | wdata->state.flags |= WIIPROTO_FLAG_ACCEL; |
| 384 | else |
| 385 | wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL; |
| 386 | |
| 387 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 388 | } |
| 389 | |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame] | 390 | static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags) |
| 391 | { |
| 392 | __u8 cmd[2]; |
| 393 | |
| 394 | cmd[0] = WIIPROTO_REQ_IR1; |
| 395 | cmd[1] = flags; |
| 396 | |
| 397 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 398 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 399 | } |
| 400 | |
| 401 | static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags) |
| 402 | { |
| 403 | __u8 cmd[2]; |
| 404 | |
| 405 | cmd[0] = WIIPROTO_REQ_IR2; |
| 406 | cmd[1] = flags; |
| 407 | |
| 408 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 409 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 410 | } |
| 411 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 412 | #define wiiproto_req_wreg(wdata, os, buf, sz) \ |
| 413 | wiiproto_req_wmem((wdata), false, (os), (buf), (sz)) |
| 414 | |
| 415 | #define wiiproto_req_weeprom(wdata, os, buf, sz) \ |
| 416 | wiiproto_req_wmem((wdata), true, (os), (buf), (sz)) |
| 417 | |
| 418 | static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom, |
| 419 | __u32 offset, const __u8 *buf, __u8 size) |
| 420 | { |
| 421 | __u8 cmd[22]; |
| 422 | |
| 423 | if (size > 16 || size == 0) { |
| 424 | hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | memset(cmd, 0, sizeof(cmd)); |
| 429 | cmd[0] = WIIPROTO_REQ_WMEM; |
| 430 | cmd[2] = (offset >> 16) & 0xff; |
| 431 | cmd[3] = (offset >> 8) & 0xff; |
| 432 | cmd[4] = offset & 0xff; |
| 433 | cmd[5] = size; |
| 434 | memcpy(&cmd[6], buf, size); |
| 435 | |
| 436 | if (!eeprom) |
| 437 | cmd[1] |= 0x04; |
| 438 | |
| 439 | wiiproto_keep_rumble(wdata, &cmd[1]); |
| 440 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 441 | } |
| 442 | |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 443 | /* requries the cmd-mutex to be held */ |
| 444 | static int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, |
| 445 | const __u8 *wmem, __u8 size) |
| 446 | { |
| 447 | unsigned long flags; |
| 448 | int ret; |
| 449 | |
| 450 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 451 | wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0); |
| 452 | wiiproto_req_wreg(wdata, offset, wmem, size); |
| 453 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 454 | |
| 455 | ret = wiimote_cmd_wait(wdata); |
| 456 | if (!ret && wdata->state.cmd_err) |
| 457 | ret = -EIO; |
| 458 | |
| 459 | return ret; |
| 460 | } |
| 461 | |
David Herrmann | 6591d75 | 2011-09-06 13:50:39 +0200 | [diff] [blame] | 462 | static int wiimote_battery_get_property(struct power_supply *psy, |
| 463 | enum power_supply_property psp, |
| 464 | union power_supply_propval *val) |
| 465 | { |
| 466 | struct wiimote_data *wdata = container_of(psy, |
| 467 | struct wiimote_data, battery); |
| 468 | int ret = 0, state; |
| 469 | unsigned long flags; |
| 470 | |
| 471 | ret = wiimote_cmd_acquire(wdata); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | |
| 475 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 476 | wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0); |
| 477 | wiiproto_req_status(wdata); |
| 478 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 479 | |
| 480 | ret = wiimote_cmd_wait(wdata); |
| 481 | state = wdata->state.cmd_battery; |
| 482 | wiimote_cmd_release(wdata); |
| 483 | |
| 484 | if (ret) |
| 485 | return ret; |
| 486 | |
| 487 | switch (psp) { |
| 488 | case POWER_SUPPLY_PROP_CAPACITY: |
| 489 | val->intval = state * 100 / 255; |
| 490 | break; |
| 491 | default: |
| 492 | ret = -EINVAL; |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
David Herrmann | fc221cd | 2011-09-06 13:50:36 +0200 | [diff] [blame] | 499 | static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode) |
| 500 | { |
| 501 | int ret; |
| 502 | unsigned long flags; |
| 503 | __u8 format = 0; |
| 504 | static const __u8 data_enable[] = { 0x01 }; |
| 505 | static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01, |
| 506 | 0x00, 0xaa, 0x00, 0x64 }; |
| 507 | static const __u8 data_sens2[] = { 0x63, 0x03 }; |
| 508 | static const __u8 data_fin[] = { 0x08 }; |
| 509 | |
| 510 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 511 | |
| 512 | if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) { |
| 513 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | if (mode == 0) { |
| 518 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; |
| 519 | wiiproto_req_ir1(wdata, 0); |
| 520 | wiiproto_req_ir2(wdata, 0); |
| 521 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 522 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 527 | |
| 528 | ret = wiimote_cmd_acquire(wdata); |
| 529 | if (ret) |
| 530 | return ret; |
| 531 | |
| 532 | /* send PIXEL CLOCK ENABLE cmd first */ |
| 533 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 534 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0); |
| 535 | wiiproto_req_ir1(wdata, 0x06); |
| 536 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 537 | |
| 538 | ret = wiimote_cmd_wait(wdata); |
| 539 | if (ret) |
| 540 | goto unlock; |
| 541 | if (wdata->state.cmd_err) { |
| 542 | ret = -EIO; |
| 543 | goto unlock; |
| 544 | } |
| 545 | |
| 546 | /* enable IR LOGIC */ |
| 547 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 548 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0); |
| 549 | wiiproto_req_ir2(wdata, 0x06); |
| 550 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 551 | |
| 552 | ret = wiimote_cmd_wait(wdata); |
| 553 | if (ret) |
| 554 | goto unlock; |
| 555 | if (wdata->state.cmd_err) { |
| 556 | ret = -EIO; |
| 557 | goto unlock; |
| 558 | } |
| 559 | |
| 560 | /* enable IR cam but do not make it send data, yet */ |
| 561 | ret = wiimote_cmd_write(wdata, 0xb00030, data_enable, |
| 562 | sizeof(data_enable)); |
| 563 | if (ret) |
| 564 | goto unlock; |
| 565 | |
| 566 | /* write first sensitivity block */ |
| 567 | ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1, |
| 568 | sizeof(data_sens1)); |
| 569 | if (ret) |
| 570 | goto unlock; |
| 571 | |
| 572 | /* write second sensitivity block */ |
| 573 | ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2, |
| 574 | sizeof(data_sens2)); |
| 575 | if (ret) |
| 576 | goto unlock; |
| 577 | |
| 578 | /* put IR cam into desired state */ |
| 579 | switch (mode) { |
| 580 | case WIIPROTO_FLAG_IR_FULL: |
| 581 | format = 5; |
| 582 | break; |
| 583 | case WIIPROTO_FLAG_IR_EXT: |
| 584 | format = 3; |
| 585 | break; |
| 586 | case WIIPROTO_FLAG_IR_BASIC: |
| 587 | format = 1; |
| 588 | break; |
| 589 | } |
| 590 | ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format)); |
| 591 | if (ret) |
| 592 | goto unlock; |
| 593 | |
| 594 | /* make IR cam send data */ |
| 595 | ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin)); |
| 596 | if (ret) |
| 597 | goto unlock; |
| 598 | |
| 599 | /* request new DRM mode compatible to IR mode */ |
| 600 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 601 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; |
| 602 | wdata->state.flags |= mode & WIIPROTO_FLAGS_IR; |
| 603 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 604 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 605 | |
| 606 | unlock: |
| 607 | wiimote_cmd_release(wdata); |
| 608 | return ret; |
| 609 | } |
| 610 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 611 | static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev) |
| 612 | { |
| 613 | struct wiimote_data *wdata; |
| 614 | struct device *dev = led_dev->dev->parent; |
| 615 | int i; |
| 616 | unsigned long flags; |
| 617 | bool value = false; |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 618 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 619 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); |
| 620 | |
| 621 | for (i = 0; i < 4; ++i) { |
| 622 | if (wdata->leds[i] == led_dev) { |
| 623 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 624 | value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1); |
| 625 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | return value ? LED_FULL : LED_OFF; |
| 631 | } |
| 632 | |
| 633 | static void wiimote_leds_set(struct led_classdev *led_dev, |
| 634 | enum led_brightness value) |
| 635 | { |
| 636 | struct wiimote_data *wdata; |
| 637 | struct device *dev = led_dev->dev->parent; |
| 638 | int i; |
| 639 | unsigned long flags; |
| 640 | __u8 state, flag; |
| 641 | |
| 642 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); |
| 643 | |
| 644 | for (i = 0; i < 4; ++i) { |
| 645 | if (wdata->leds[i] == led_dev) { |
| 646 | flag = WIIPROTO_FLAG_LED(i + 1); |
| 647 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 648 | state = wdata->state.flags; |
| 649 | if (value == LED_OFF) |
| 650 | wiiproto_req_leds(wdata, state & ~flag); |
| 651 | else |
| 652 | wiiproto_req_leds(wdata, state | flag); |
| 653 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | } |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 658 | |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame] | 659 | static int wiimote_ff_play(struct input_dev *dev, void *data, |
| 660 | struct ff_effect *eff) |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 661 | { |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame] | 662 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 663 | __u8 value; |
| 664 | unsigned long flags; |
| 665 | |
| 666 | /* |
| 667 | * The wiimote supports only a single rumble motor so if any magnitude |
| 668 | * is set to non-zero then we start the rumble motor. If both are set to |
| 669 | * zero, we stop the rumble motor. |
| 670 | */ |
| 671 | |
| 672 | if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) |
| 673 | value = 1; |
| 674 | else |
| 675 | value = 0; |
| 676 | |
| 677 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 678 | wiiproto_req_rumble(wdata, value); |
| 679 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 680 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
David Herrmann | 26af174 | 2011-08-17 11:43:21 +0200 | [diff] [blame] | 684 | static int wiimote_input_open(struct input_dev *dev) |
| 685 | { |
| 686 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 687 | |
| 688 | return hid_hw_open(wdata->hdev); |
| 689 | } |
| 690 | |
| 691 | static void wiimote_input_close(struct input_dev *dev) |
| 692 | { |
| 693 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 694 | |
| 695 | hid_hw_close(wdata->hdev); |
| 696 | } |
| 697 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 698 | static int wiimote_accel_open(struct input_dev *dev) |
| 699 | { |
| 700 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 701 | int ret; |
| 702 | unsigned long flags; |
| 703 | |
| 704 | ret = hid_hw_open(wdata->hdev); |
| 705 | if (ret) |
| 706 | return ret; |
| 707 | |
| 708 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 709 | wiiproto_req_accel(wdata, true); |
| 710 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | static void wiimote_accel_close(struct input_dev *dev) |
| 716 | { |
| 717 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 718 | unsigned long flags; |
| 719 | |
| 720 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 721 | wiiproto_req_accel(wdata, false); |
| 722 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 723 | |
| 724 | hid_hw_close(wdata->hdev); |
| 725 | } |
| 726 | |
David Herrmann | 0370d7c | 2011-09-06 13:50:37 +0200 | [diff] [blame] | 727 | static int wiimote_ir_open(struct input_dev *dev) |
| 728 | { |
| 729 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 730 | int ret; |
| 731 | |
| 732 | ret = hid_hw_open(wdata->hdev); |
| 733 | if (ret) |
| 734 | return ret; |
| 735 | |
| 736 | ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC); |
| 737 | if (ret) { |
| 738 | hid_hw_close(wdata->hdev); |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static void wiimote_ir_close(struct input_dev *dev) |
| 746 | { |
| 747 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 748 | |
| 749 | wiimote_init_ir(wdata, 0); |
| 750 | hid_hw_close(wdata->hdev); |
| 751 | } |
| 752 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 753 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
| 754 | { |
| 755 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], |
| 756 | !!(payload[0] & 0x01)); |
| 757 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], |
| 758 | !!(payload[0] & 0x02)); |
| 759 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], |
| 760 | !!(payload[0] & 0x04)); |
| 761 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], |
| 762 | !!(payload[0] & 0x08)); |
| 763 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], |
| 764 | !!(payload[0] & 0x10)); |
| 765 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], |
| 766 | !!(payload[1] & 0x01)); |
| 767 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], |
| 768 | !!(payload[1] & 0x02)); |
| 769 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], |
| 770 | !!(payload[1] & 0x04)); |
| 771 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], |
| 772 | !!(payload[1] & 0x08)); |
| 773 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], |
| 774 | !!(payload[1] & 0x10)); |
| 775 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], |
| 776 | !!(payload[1] & 0x80)); |
| 777 | input_sync(wdata->input); |
| 778 | } |
| 779 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 780 | static void handler_accel(struct wiimote_data *wdata, const __u8 *payload) |
| 781 | { |
| 782 | __u16 x, y, z; |
| 783 | |
| 784 | if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) |
| 785 | return; |
| 786 | |
| 787 | /* |
| 788 | * payload is: BB BB XX YY ZZ |
| 789 | * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ |
| 790 | * contain the upper 8 bits of each value. The lower 2 bits are |
| 791 | * contained in the buttons data BB BB. |
| 792 | * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the |
| 793 | * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y |
| 794 | * accel value and bit 6 is the second bit of the Z value. |
| 795 | * The first bit of Y and Z values is not available and always set to 0. |
| 796 | * 0x200 is returned on no movement. |
| 797 | */ |
| 798 | |
| 799 | x = payload[2] << 2; |
| 800 | y = payload[3] << 2; |
| 801 | z = payload[4] << 2; |
| 802 | |
| 803 | x |= (payload[0] >> 5) & 0x3; |
| 804 | y |= (payload[1] >> 4) & 0x2; |
| 805 | z |= (payload[1] >> 5) & 0x2; |
| 806 | |
| 807 | input_report_abs(wdata->accel, ABS_RX, x - 0x200); |
| 808 | input_report_abs(wdata->accel, ABS_RY, y - 0x200); |
| 809 | input_report_abs(wdata->accel, ABS_RZ, z - 0x200); |
| 810 | input_sync(wdata->accel); |
| 811 | } |
| 812 | |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 813 | #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ |
| 814 | ABS_HAT0X, ABS_HAT0Y) |
| 815 | #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ |
| 816 | ABS_HAT1X, ABS_HAT1Y) |
| 817 | #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ |
| 818 | ABS_HAT2X, ABS_HAT2Y) |
| 819 | #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ |
| 820 | ABS_HAT3X, ABS_HAT3Y) |
| 821 | |
| 822 | static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir, |
| 823 | bool packed, __u8 xid, __u8 yid) |
| 824 | { |
| 825 | __u16 x, y; |
| 826 | |
| 827 | if (!(wdata->state.flags & WIIPROTO_FLAGS_IR)) |
| 828 | return; |
| 829 | |
| 830 | /* |
| 831 | * Basic IR data is encoded into 3 bytes. The first two bytes are the |
| 832 | * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits |
| 833 | * of both. |
| 834 | * If data is packed, then the 3rd byte is put first and slightly |
| 835 | * reordered. This allows to interleave packed and non-packed data to |
| 836 | * have two IR sets in 5 bytes instead of 6. |
| 837 | * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev. |
| 838 | */ |
| 839 | |
| 840 | if (packed) { |
| 841 | x = ir[1] << 2; |
| 842 | y = ir[2] << 2; |
| 843 | |
| 844 | x |= ir[0] & 0x3; |
| 845 | y |= (ir[0] >> 2) & 0x3; |
| 846 | } else { |
| 847 | x = ir[0] << 2; |
| 848 | y = ir[1] << 2; |
| 849 | |
| 850 | x |= (ir[2] >> 4) & 0x3; |
| 851 | y |= (ir[2] >> 6) & 0x3; |
| 852 | } |
| 853 | |
| 854 | input_report_abs(wdata->ir, xid, x); |
| 855 | input_report_abs(wdata->ir, yid, y); |
| 856 | } |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 857 | |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 858 | static void handler_status(struct wiimote_data *wdata, const __u8 *payload) |
| 859 | { |
| 860 | handler_keys(wdata, payload); |
| 861 | |
| 862 | /* on status reports the drm is reset so we need to resend the drm */ |
| 863 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
David Herrmann | e3979a9 | 2011-09-06 13:50:38 +0200 | [diff] [blame] | 864 | |
| 865 | if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) { |
| 866 | wdata->state.cmd_battery = payload[5]; |
| 867 | wiimote_cmd_complete(wdata); |
| 868 | } |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 869 | } |
| 870 | |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 871 | static void handler_data(struct wiimote_data *wdata, const __u8 *payload) |
| 872 | { |
| 873 | handler_keys(wdata, payload); |
| 874 | } |
| 875 | |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 876 | static void handler_return(struct wiimote_data *wdata, const __u8 *payload) |
| 877 | { |
| 878 | __u8 err = payload[3]; |
| 879 | __u8 cmd = payload[2]; |
| 880 | |
| 881 | handler_keys(wdata, payload); |
| 882 | |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 883 | if (wiimote_cmd_pending(wdata, cmd, 0)) { |
| 884 | wdata->state.cmd_err = err; |
| 885 | wiimote_cmd_complete(wdata); |
| 886 | } else if (err) { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 887 | hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err, |
| 888 | cmd); |
David Herrmann | 33e8401 | 2011-09-06 13:50:35 +0200 | [diff] [blame] | 889 | } |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 890 | } |
| 891 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 892 | static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload) |
| 893 | { |
| 894 | handler_keys(wdata, payload); |
| 895 | handler_accel(wdata, payload); |
| 896 | } |
| 897 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 898 | static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload) |
| 899 | { |
| 900 | handler_keys(wdata, payload); |
| 901 | } |
| 902 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 903 | static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload) |
| 904 | { |
| 905 | handler_keys(wdata, payload); |
| 906 | handler_accel(wdata, payload); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 907 | ir_to_input0(wdata, &payload[5], false); |
| 908 | ir_to_input1(wdata, &payload[8], false); |
| 909 | ir_to_input2(wdata, &payload[11], false); |
| 910 | ir_to_input3(wdata, &payload[14], false); |
| 911 | input_sync(wdata->ir); |
| 912 | } |
| 913 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 914 | static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload) |
| 915 | { |
| 916 | handler_keys(wdata, payload); |
| 917 | } |
| 918 | |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 919 | static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload) |
| 920 | { |
| 921 | handler_keys(wdata, payload); |
| 922 | ir_to_input0(wdata, &payload[2], false); |
| 923 | ir_to_input1(wdata, &payload[4], true); |
| 924 | ir_to_input2(wdata, &payload[7], false); |
| 925 | ir_to_input3(wdata, &payload[9], true); |
| 926 | input_sync(wdata->ir); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload) |
| 930 | { |
| 931 | handler_keys(wdata, payload); |
| 932 | handler_accel(wdata, payload); |
| 933 | } |
| 934 | |
| 935 | static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload) |
| 936 | { |
| 937 | handler_keys(wdata, payload); |
| 938 | handler_accel(wdata, payload); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 939 | ir_to_input0(wdata, &payload[5], false); |
| 940 | ir_to_input1(wdata, &payload[7], true); |
| 941 | ir_to_input2(wdata, &payload[10], false); |
| 942 | ir_to_input3(wdata, &payload[12], true); |
| 943 | input_sync(wdata->ir); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 944 | } |
| 945 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 946 | static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload) |
| 947 | { |
| 948 | } |
| 949 | |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 950 | static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload) |
| 951 | { |
| 952 | handler_keys(wdata, payload); |
| 953 | |
| 954 | wdata->state.accel_split[0] = payload[2]; |
| 955 | wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20); |
| 956 | wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 957 | |
| 958 | ir_to_input0(wdata, &payload[3], false); |
| 959 | ir_to_input1(wdata, &payload[12], false); |
| 960 | input_sync(wdata->ir); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload) |
| 964 | { |
| 965 | __u8 buf[5]; |
| 966 | |
| 967 | handler_keys(wdata, payload); |
| 968 | |
| 969 | wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02); |
| 970 | wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08); |
| 971 | |
| 972 | buf[0] = 0; |
| 973 | buf[1] = 0; |
| 974 | buf[2] = wdata->state.accel_split[0]; |
| 975 | buf[3] = payload[2]; |
| 976 | buf[4] = wdata->state.accel_split[1]; |
| 977 | handler_accel(wdata, buf); |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 978 | |
| 979 | ir_to_input2(wdata, &payload[3], false); |
| 980 | ir_to_input3(wdata, &payload[12], false); |
| 981 | input_sync(wdata->ir); |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 982 | } |
| 983 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 984 | struct wiiproto_handler { |
| 985 | __u8 id; |
| 986 | size_t size; |
| 987 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); |
| 988 | }; |
| 989 | |
| 990 | static struct wiiproto_handler handlers[] = { |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 991 | { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status }, |
David Herrmann | be1ecd6 | 2011-09-06 13:50:33 +0200 | [diff] [blame] | 992 | { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data }, |
David Herrmann | c87019e | 2011-08-17 11:43:24 +0200 | [diff] [blame] | 993 | { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return }, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 994 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 995 | { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 996 | { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 997 | { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 998 | { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 999 | { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE }, |
David Herrmann | eac39e7 | 2011-09-06 13:50:31 +0200 | [diff] [blame] | 1000 | { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1001 | { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE }, |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1002 | { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E }, |
David Herrmann | efcf918 | 2011-09-06 13:50:29 +0200 | [diff] [blame] | 1003 | { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 }, |
| 1004 | { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 }, |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1005 | { .id = 0 } |
| 1006 | }; |
| 1007 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1008 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 1009 | u8 *raw_data, int size) |
| 1010 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 1011 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1012 | struct wiiproto_handler *h; |
| 1013 | int i; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1014 | unsigned long flags; |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1015 | bool handled = false; |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 1016 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1017 | if (size < 1) |
| 1018 | return -EINVAL; |
| 1019 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1020 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1021 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1022 | for (i = 0; handlers[i].id; ++i) { |
| 1023 | h = &handlers[i]; |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1024 | if (h->id == raw_data[0] && h->size < size) { |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1025 | h->func(wdata, &raw_data[1]); |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1026 | handled = true; |
| 1027 | } |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
David Herrmann | 7336b9f | 2011-09-06 13:50:32 +0200 | [diff] [blame] | 1030 | if (!handled) |
| 1031 | hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0], |
| 1032 | size); |
| 1033 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1034 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1035 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1036 | return 0; |
| 1037 | } |
| 1038 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 1039 | static void wiimote_leds_destroy(struct wiimote_data *wdata) |
| 1040 | { |
| 1041 | int i; |
| 1042 | struct led_classdev *led; |
| 1043 | |
| 1044 | for (i = 0; i < 4; ++i) { |
| 1045 | if (wdata->leds[i]) { |
| 1046 | led = wdata->leds[i]; |
| 1047 | wdata->leds[i] = NULL; |
| 1048 | led_classdev_unregister(led); |
| 1049 | kfree(led); |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | static int wiimote_leds_create(struct wiimote_data *wdata) |
| 1055 | { |
| 1056 | int i, ret; |
| 1057 | struct device *dev = &wdata->hdev->dev; |
| 1058 | size_t namesz = strlen(dev_name(dev)) + 9; |
| 1059 | struct led_classdev *led; |
| 1060 | char *name; |
| 1061 | |
| 1062 | for (i = 0; i < 4; ++i) { |
| 1063 | led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL); |
| 1064 | if (!led) { |
| 1065 | ret = -ENOMEM; |
| 1066 | goto err; |
| 1067 | } |
| 1068 | name = (void*)&led[1]; |
| 1069 | snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i); |
| 1070 | led->name = name; |
| 1071 | led->brightness = 0; |
| 1072 | led->max_brightness = 1; |
| 1073 | led->brightness_get = wiimote_leds_get; |
| 1074 | led->brightness_set = wiimote_leds_set; |
| 1075 | |
| 1076 | ret = led_classdev_register(dev, led); |
| 1077 | if (ret) { |
| 1078 | kfree(led); |
| 1079 | goto err; |
| 1080 | } |
| 1081 | wdata->leds[i] = led; |
| 1082 | } |
| 1083 | |
| 1084 | return 0; |
| 1085 | |
| 1086 | err: |
| 1087 | wiimote_leds_destroy(wdata); |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1091 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 1092 | { |
| 1093 | struct wiimote_data *wdata; |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 1094 | int i; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1095 | |
| 1096 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 1097 | if (!wdata) |
| 1098 | return NULL; |
| 1099 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1100 | wdata->input = input_allocate_device(); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1101 | if (!wdata->input) |
| 1102 | goto err; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1103 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1104 | wdata->hdev = hdev; |
| 1105 | hid_set_drvdata(hdev, wdata); |
| 1106 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1107 | input_set_drvdata(wdata->input, wdata); |
David Herrmann | 26af174 | 2011-08-17 11:43:21 +0200 | [diff] [blame] | 1108 | wdata->input->open = wiimote_input_open; |
| 1109 | wdata->input->close = wiimote_input_close; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1110 | wdata->input->dev.parent = &wdata->hdev->dev; |
| 1111 | wdata->input->id.bustype = wdata->hdev->bus; |
| 1112 | wdata->input->id.vendor = wdata->hdev->vendor; |
| 1113 | wdata->input->id.product = wdata->hdev->product; |
| 1114 | wdata->input->id.version = wdata->hdev->version; |
| 1115 | wdata->input->name = WIIMOTE_NAME; |
| 1116 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 1117 | set_bit(EV_KEY, wdata->input->evbit); |
| 1118 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) |
| 1119 | set_bit(wiiproto_keymap[i], wdata->input->keybit); |
| 1120 | |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame] | 1121 | set_bit(FF_RUMBLE, wdata->input->ffbit); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1122 | if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play)) |
| 1123 | goto err_input; |
| 1124 | |
| 1125 | wdata->accel = input_allocate_device(); |
| 1126 | if (!wdata->accel) |
| 1127 | goto err_input; |
| 1128 | |
| 1129 | input_set_drvdata(wdata->accel, wdata); |
| 1130 | wdata->accel->open = wiimote_accel_open; |
| 1131 | wdata->accel->close = wiimote_accel_close; |
| 1132 | wdata->accel->dev.parent = &wdata->hdev->dev; |
| 1133 | wdata->accel->id.bustype = wdata->hdev->bus; |
| 1134 | wdata->accel->id.vendor = wdata->hdev->vendor; |
| 1135 | wdata->accel->id.product = wdata->hdev->product; |
| 1136 | wdata->accel->id.version = wdata->hdev->version; |
| 1137 | wdata->accel->name = WIIMOTE_NAME " Accelerometer"; |
| 1138 | |
| 1139 | set_bit(EV_ABS, wdata->accel->evbit); |
| 1140 | set_bit(ABS_RX, wdata->accel->absbit); |
| 1141 | set_bit(ABS_RY, wdata->accel->absbit); |
| 1142 | set_bit(ABS_RZ, wdata->accel->absbit); |
| 1143 | input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4); |
| 1144 | input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4); |
| 1145 | input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4); |
David Herrmann | d020be9 | 2011-09-06 13:50:27 +0200 | [diff] [blame] | 1146 | |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1147 | wdata->ir = input_allocate_device(); |
| 1148 | if (!wdata->ir) |
| 1149 | goto err_ir; |
| 1150 | |
| 1151 | input_set_drvdata(wdata->ir, wdata); |
David Herrmann | 0370d7c | 2011-09-06 13:50:37 +0200 | [diff] [blame] | 1152 | wdata->ir->open = wiimote_ir_open; |
| 1153 | wdata->ir->close = wiimote_ir_close; |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1154 | wdata->ir->dev.parent = &wdata->hdev->dev; |
| 1155 | wdata->ir->id.bustype = wdata->hdev->bus; |
| 1156 | wdata->ir->id.vendor = wdata->hdev->vendor; |
| 1157 | wdata->ir->id.product = wdata->hdev->product; |
| 1158 | wdata->ir->id.version = wdata->hdev->version; |
| 1159 | wdata->ir->name = WIIMOTE_NAME " IR"; |
| 1160 | |
| 1161 | set_bit(EV_ABS, wdata->ir->evbit); |
| 1162 | set_bit(ABS_HAT0X, wdata->ir->absbit); |
| 1163 | set_bit(ABS_HAT0Y, wdata->ir->absbit); |
| 1164 | set_bit(ABS_HAT1X, wdata->ir->absbit); |
| 1165 | set_bit(ABS_HAT1Y, wdata->ir->absbit); |
| 1166 | set_bit(ABS_HAT2X, wdata->ir->absbit); |
| 1167 | set_bit(ABS_HAT2Y, wdata->ir->absbit); |
| 1168 | set_bit(ABS_HAT3X, wdata->ir->absbit); |
| 1169 | set_bit(ABS_HAT3Y, wdata->ir->absbit); |
| 1170 | input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4); |
| 1171 | input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4); |
| 1172 | input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4); |
| 1173 | input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4); |
| 1174 | input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4); |
| 1175 | input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4); |
| 1176 | input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4); |
| 1177 | input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4); |
| 1178 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 1179 | spin_lock_init(&wdata->qlock); |
| 1180 | INIT_WORK(&wdata->worker, wiimote_worker); |
| 1181 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1182 | spin_lock_init(&wdata->state.lock); |
David Herrmann | 29d2806 | 2011-09-06 13:50:34 +0200 | [diff] [blame] | 1183 | init_completion(&wdata->state.ready); |
| 1184 | mutex_init(&wdata->state.sync); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1185 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1186 | return wdata; |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1187 | |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1188 | err_ir: |
| 1189 | input_free_device(wdata->accel); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1190 | err_input: |
| 1191 | input_free_device(wdata->input); |
| 1192 | err: |
| 1193 | kfree(wdata); |
| 1194 | return NULL; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 1198 | { |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 1199 | wiimote_leds_destroy(wdata); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1200 | |
David Herrmann | 6591d75 | 2011-09-06 13:50:39 +0200 | [diff] [blame] | 1201 | power_supply_unregister(&wdata->battery); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1202 | input_unregister_device(wdata->accel); |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1203 | input_unregister_device(wdata->ir); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1204 | input_unregister_device(wdata->input); |
| 1205 | cancel_work_sync(&wdata->worker); |
| 1206 | hid_hw_stop(wdata->hdev); |
| 1207 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1208 | kfree(wdata); |
| 1209 | } |
| 1210 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1211 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 1212 | const struct hid_device_id *id) |
| 1213 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1214 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1215 | int ret; |
| 1216 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1217 | wdata = wiimote_create(hdev); |
| 1218 | if (!wdata) { |
| 1219 | hid_err(hdev, "Can't alloc device\n"); |
| 1220 | return -ENOMEM; |
| 1221 | } |
| 1222 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1223 | ret = hid_parse(hdev); |
| 1224 | if (ret) { |
| 1225 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1226 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 1230 | if (ret) { |
| 1231 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1232 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1233 | } |
| 1234 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1235 | ret = input_register_device(wdata->accel); |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1236 | if (ret) { |
| 1237 | hid_err(hdev, "Cannot register input device\n"); |
| 1238 | goto err_stop; |
| 1239 | } |
| 1240 | |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1241 | ret = input_register_device(wdata->ir); |
| 1242 | if (ret) { |
| 1243 | hid_err(hdev, "Cannot register input device\n"); |
| 1244 | goto err_ir; |
| 1245 | } |
| 1246 | |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1247 | ret = input_register_device(wdata->input); |
| 1248 | if (ret) { |
| 1249 | hid_err(hdev, "Cannot register input device\n"); |
| 1250 | goto err_input; |
| 1251 | } |
| 1252 | |
David Herrmann | 6591d75 | 2011-09-06 13:50:39 +0200 | [diff] [blame] | 1253 | wdata->battery.properties = wiimote_battery_props; |
| 1254 | wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props); |
| 1255 | wdata->battery.get_property = wiimote_battery_get_property; |
| 1256 | wdata->battery.name = "wiimote_battery"; |
| 1257 | wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY; |
| 1258 | wdata->battery.use_for_apm = 0; |
| 1259 | |
| 1260 | ret = power_supply_register(&wdata->hdev->dev, &wdata->battery); |
| 1261 | if (ret) { |
| 1262 | hid_err(hdev, "Cannot register battery device\n"); |
| 1263 | goto err_battery; |
| 1264 | } |
| 1265 | |
David Herrmann | 23a5a4a | 2011-08-17 11:43:22 +0200 | [diff] [blame] | 1266 | ret = wiimote_leds_create(wdata); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1267 | if (ret) |
| 1268 | goto err_free; |
| 1269 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1270 | hid_info(hdev, "New device registered\n"); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1271 | |
| 1272 | /* by default set led1 after device initialization */ |
| 1273 | spin_lock_irq(&wdata->state.lock); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 1274 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 1275 | spin_unlock_irq(&wdata->state.lock); |
| 1276 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1277 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1278 | |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1279 | err_free: |
| 1280 | wiimote_destroy(wdata); |
| 1281 | return ret; |
| 1282 | |
David Herrmann | 6591d75 | 2011-09-06 13:50:39 +0200 | [diff] [blame] | 1283 | err_battery: |
| 1284 | input_unregister_device(wdata->input); |
| 1285 | wdata->input = NULL; |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1286 | err_input: |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1287 | input_unregister_device(wdata->ir); |
| 1288 | wdata->ir = NULL; |
| 1289 | err_ir: |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1290 | input_unregister_device(wdata->accel); |
| 1291 | wdata->accel = NULL; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1292 | err_stop: |
| 1293 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1294 | err: |
David Herrmann | f363e4f | 2011-09-06 13:50:30 +0200 | [diff] [blame] | 1295 | input_free_device(wdata->ir); |
David Herrmann | 98a558a | 2011-09-06 13:50:28 +0200 | [diff] [blame] | 1296 | input_free_device(wdata->accel); |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 1297 | input_free_device(wdata->input); |
David Herrmann | 3989ef6 | 2011-08-17 11:43:20 +0200 | [diff] [blame] | 1298 | kfree(wdata); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1299 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 1303 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1304 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 1305 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1306 | hid_info(hdev, "Device removed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 1307 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 1311 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 1312 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
| 1313 | { } |
| 1314 | }; |
| 1315 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 1316 | |
| 1317 | static struct hid_driver wiimote_hid_driver = { |
| 1318 | .name = "wiimote", |
| 1319 | .id_table = wiimote_hid_devices, |
| 1320 | .probe = wiimote_hid_probe, |
| 1321 | .remove = wiimote_hid_remove, |
| 1322 | .raw_event = wiimote_hid_event, |
| 1323 | }; |
| 1324 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1325 | static int __init wiimote_init(void) |
| 1326 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1327 | int ret; |
| 1328 | |
| 1329 | ret = hid_register_driver(&wiimote_hid_driver); |
| 1330 | if (ret) |
| 1331 | pr_err("Can't register wiimote hid driver\n"); |
| 1332 | |
| 1333 | return ret; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | static void __exit wiimote_exit(void) |
| 1337 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 1338 | hid_unregister_driver(&wiimote_hid_driver); |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | module_init(wiimote_init); |
| 1342 | module_exit(wiimote_exit); |
| 1343 | MODULE_LICENSE("GPL"); |
| 1344 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
| 1345 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); |
| 1346 | MODULE_VERSION(WIIMOTE_VERSION); |