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 | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 13 | #include <linux/atomic.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 | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 19 | #include "hid-ids.h" |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 20 | |
| 21 | #define WIIMOTE_VERSION "0.1" |
| 22 | #define WIIMOTE_NAME "Nintendo Wii Remote" |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 23 | #define WIIMOTE_BUFSIZE 32 |
| 24 | |
| 25 | struct wiimote_buf { |
| 26 | __u8 data[HID_MAX_BUFFER_SIZE]; |
| 27 | size_t size; |
| 28 | }; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 29 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 30 | struct wiimote_state { |
| 31 | spinlock_t lock; |
| 32 | __u8 flags; |
| 33 | }; |
| 34 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 35 | struct wiimote_data { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 36 | atomic_t ready; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 37 | struct hid_device *hdev; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 38 | struct input_dev *input; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 39 | |
| 40 | spinlock_t qlock; |
| 41 | __u8 head; |
| 42 | __u8 tail; |
| 43 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; |
| 44 | struct work_struct worker; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 45 | |
| 46 | struct wiimote_state state; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 49 | #define WIIPROTO_FLAG_LED1 0x01 |
| 50 | #define WIIPROTO_FLAG_LED2 0x02 |
| 51 | #define WIIPROTO_FLAG_LED3 0x04 |
| 52 | #define WIIPROTO_FLAG_LED4 0x08 |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 53 | #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ |
| 54 | WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 55 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 56 | enum wiiproto_reqs { |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 57 | WIIPROTO_REQ_LED = 0x11, |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 58 | WIIPROTO_REQ_DRM_K = 0x30, |
| 59 | }; |
| 60 | |
| 61 | enum wiiproto_keys { |
| 62 | WIIPROTO_KEY_LEFT, |
| 63 | WIIPROTO_KEY_RIGHT, |
| 64 | WIIPROTO_KEY_UP, |
| 65 | WIIPROTO_KEY_DOWN, |
| 66 | WIIPROTO_KEY_PLUS, |
| 67 | WIIPROTO_KEY_MINUS, |
| 68 | WIIPROTO_KEY_ONE, |
| 69 | WIIPROTO_KEY_TWO, |
| 70 | WIIPROTO_KEY_A, |
| 71 | WIIPROTO_KEY_B, |
| 72 | WIIPROTO_KEY_HOME, |
| 73 | WIIPROTO_KEY_COUNT |
| 74 | }; |
| 75 | |
| 76 | static __u16 wiiproto_keymap[] = { |
| 77 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ |
| 78 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ |
| 79 | KEY_UP, /* WIIPROTO_KEY_UP */ |
| 80 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ |
| 81 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ |
| 82 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ |
| 83 | BTN_1, /* WIIPROTO_KEY_ONE */ |
| 84 | BTN_2, /* WIIPROTO_KEY_TWO */ |
| 85 | BTN_A, /* WIIPROTO_KEY_A */ |
| 86 | BTN_B, /* WIIPROTO_KEY_B */ |
| 87 | BTN_MODE, /* WIIPROTO_KEY_HOME */ |
| 88 | }; |
| 89 | |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 90 | #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \ |
| 91 | dev)) |
| 92 | |
David Herrmann | 0c218f1 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 93 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
| 94 | size_t count) |
| 95 | { |
| 96 | __u8 *buf; |
| 97 | ssize_t ret; |
| 98 | |
| 99 | if (!hdev->hid_output_raw_report) |
| 100 | return -ENODEV; |
| 101 | |
| 102 | buf = kmemdup(buffer, count, GFP_KERNEL); |
| 103 | if (!buf) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); |
| 107 | |
| 108 | kfree(buf); |
| 109 | return ret; |
| 110 | } |
| 111 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 112 | static void wiimote_worker(struct work_struct *work) |
| 113 | { |
| 114 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 115 | worker); |
| 116 | unsigned long flags; |
| 117 | |
| 118 | spin_lock_irqsave(&wdata->qlock, flags); |
| 119 | |
| 120 | while (wdata->head != wdata->tail) { |
| 121 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 122 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, |
| 123 | wdata->outq[wdata->tail].size); |
| 124 | spin_lock_irqsave(&wdata->qlock, flags); |
| 125 | |
| 126 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; |
| 127 | } |
| 128 | |
| 129 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 130 | } |
| 131 | |
| 132 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, |
| 133 | size_t count) |
| 134 | { |
| 135 | unsigned long flags; |
| 136 | __u8 newhead; |
| 137 | |
| 138 | if (count > HID_MAX_BUFFER_SIZE) { |
| 139 | hid_warn(wdata->hdev, "Sending too large output report\n"); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Copy new request into our output queue and check whether the |
| 145 | * queue is full. If it is full, discard this request. |
| 146 | * If it is empty we need to start a new worker that will |
| 147 | * send out the buffer to the hid device. |
| 148 | * If the queue is not empty, then there must be a worker |
| 149 | * that is currently sending out our buffer and this worker |
| 150 | * will reschedule itself until the queue is empty. |
| 151 | */ |
| 152 | |
| 153 | spin_lock_irqsave(&wdata->qlock, flags); |
| 154 | |
| 155 | memcpy(wdata->outq[wdata->head].data, buffer, count); |
| 156 | wdata->outq[wdata->head].size = count; |
| 157 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; |
| 158 | |
| 159 | if (wdata->head == wdata->tail) { |
| 160 | wdata->head = newhead; |
| 161 | schedule_work(&wdata->worker); |
| 162 | } else if (newhead != wdata->tail) { |
| 163 | wdata->head = newhead; |
| 164 | } else { |
| 165 | hid_warn(wdata->hdev, "Output queue is full"); |
| 166 | } |
| 167 | |
| 168 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 169 | } |
| 170 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 171 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
| 172 | { |
| 173 | __u8 cmd[2]; |
| 174 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 175 | leds &= WIIPROTO_FLAGS_LEDS; |
| 176 | if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds) |
| 177 | return; |
| 178 | wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds; |
| 179 | |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 180 | cmd[0] = WIIPROTO_REQ_LED; |
| 181 | cmd[1] = 0; |
| 182 | |
| 183 | if (leds & WIIPROTO_FLAG_LED1) |
| 184 | cmd[1] |= 0x10; |
| 185 | if (leds & WIIPROTO_FLAG_LED2) |
| 186 | cmd[1] |= 0x20; |
| 187 | if (leds & WIIPROTO_FLAG_LED3) |
| 188 | cmd[1] |= 0x40; |
| 189 | if (leds & WIIPROTO_FLAG_LED4) |
| 190 | cmd[1] |= 0x80; |
| 191 | |
| 192 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
| 193 | } |
| 194 | |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 195 | #define wiifs_led_show_set(num) \ |
| 196 | static ssize_t wiifs_led_show_##num(struct device *dev, \ |
| 197 | struct device_attribute *attr, char *buf) \ |
| 198 | { \ |
| 199 | struct wiimote_data *wdata = dev_to_wii(dev); \ |
| 200 | unsigned long flags; \ |
| 201 | int state; \ |
| 202 | \ |
| 203 | if (!atomic_read(&wdata->ready)) \ |
| 204 | return -EBUSY; \ |
| 205 | \ |
| 206 | spin_lock_irqsave(&wdata->state.lock, flags); \ |
| 207 | state = !!(wdata->state.flags & WIIPROTO_FLAG_LED##num); \ |
| 208 | spin_unlock_irqrestore(&wdata->state.lock, flags); \ |
| 209 | \ |
| 210 | return sprintf(buf, "%d\n", state); \ |
| 211 | } \ |
| 212 | static ssize_t wiifs_led_set_##num(struct device *dev, \ |
| 213 | struct device_attribute *attr, const char *buf, size_t count) \ |
| 214 | { \ |
| 215 | struct wiimote_data *wdata = dev_to_wii(dev); \ |
| 216 | int tmp = simple_strtoul(buf, NULL, 10); \ |
| 217 | unsigned long flags; \ |
| 218 | __u8 state; \ |
| 219 | \ |
| 220 | if (!atomic_read(&wdata->ready)) \ |
| 221 | return -EBUSY; \ |
| 222 | \ |
| 223 | spin_lock_irqsave(&wdata->state.lock, flags); \ |
| 224 | \ |
| 225 | state = wdata->state.flags; \ |
| 226 | \ |
| 227 | if (tmp) \ |
| 228 | wiiproto_req_leds(wdata, state | WIIPROTO_FLAG_LED##num);\ |
| 229 | else \ |
| 230 | wiiproto_req_leds(wdata, state & ~WIIPROTO_FLAG_LED##num);\ |
| 231 | \ |
| 232 | spin_unlock_irqrestore(&wdata->state.lock, flags); \ |
| 233 | \ |
| 234 | return count; \ |
| 235 | } \ |
| 236 | static DEVICE_ATTR(led##num, S_IRUGO | S_IWUSR, wiifs_led_show_##num, \ |
| 237 | wiifs_led_set_##num) |
| 238 | |
| 239 | wiifs_led_show_set(1); |
| 240 | wiifs_led_show_set(2); |
| 241 | wiifs_led_show_set(3); |
| 242 | wiifs_led_show_set(4); |
| 243 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 244 | static int wiimote_input_event(struct input_dev *dev, unsigned int type, |
| 245 | unsigned int code, int value) |
| 246 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 247 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 248 | |
| 249 | if (!atomic_read(&wdata->ready)) |
| 250 | return -EBUSY; |
| 251 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ |
| 252 | smp_rmb(); |
| 253 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 257 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
| 258 | { |
| 259 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], |
| 260 | !!(payload[0] & 0x01)); |
| 261 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], |
| 262 | !!(payload[0] & 0x02)); |
| 263 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], |
| 264 | !!(payload[0] & 0x04)); |
| 265 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], |
| 266 | !!(payload[0] & 0x08)); |
| 267 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], |
| 268 | !!(payload[0] & 0x10)); |
| 269 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], |
| 270 | !!(payload[1] & 0x01)); |
| 271 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], |
| 272 | !!(payload[1] & 0x02)); |
| 273 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], |
| 274 | !!(payload[1] & 0x04)); |
| 275 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], |
| 276 | !!(payload[1] & 0x08)); |
| 277 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], |
| 278 | !!(payload[1] & 0x10)); |
| 279 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], |
| 280 | !!(payload[1] & 0x80)); |
| 281 | input_sync(wdata->input); |
| 282 | } |
| 283 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 284 | struct wiiproto_handler { |
| 285 | __u8 id; |
| 286 | size_t size; |
| 287 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); |
| 288 | }; |
| 289 | |
| 290 | static struct wiiproto_handler handlers[] = { |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 291 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 292 | { .id = 0 } |
| 293 | }; |
| 294 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 295 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 296 | u8 *raw_data, int size) |
| 297 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 298 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 299 | struct wiiproto_handler *h; |
| 300 | int i; |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 301 | unsigned long flags; |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 302 | |
| 303 | if (!atomic_read(&wdata->ready)) |
| 304 | return -EBUSY; |
| 305 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ |
| 306 | smp_rmb(); |
| 307 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 308 | if (size < 1) |
| 309 | return -EINVAL; |
| 310 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 311 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 312 | |
David Herrmann | a4d1919 | 2011-07-05 13:45:15 +0200 | [diff] [blame] | 313 | for (i = 0; handlers[i].id; ++i) { |
| 314 | h = &handlers[i]; |
| 315 | if (h->id == raw_data[0] && h->size < size) |
| 316 | h->func(wdata, &raw_data[1]); |
| 317 | } |
| 318 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 319 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 320 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 324 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 325 | { |
| 326 | struct wiimote_data *wdata; |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 327 | int i; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 328 | |
| 329 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 330 | if (!wdata) |
| 331 | return NULL; |
| 332 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 333 | wdata->input = input_allocate_device(); |
| 334 | if (!wdata->input) { |
| 335 | kfree(wdata); |
| 336 | return NULL; |
| 337 | } |
| 338 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 339 | wdata->hdev = hdev; |
| 340 | hid_set_drvdata(hdev, wdata); |
| 341 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 342 | input_set_drvdata(wdata->input, wdata); |
| 343 | wdata->input->event = wiimote_input_event; |
| 344 | wdata->input->dev.parent = &wdata->hdev->dev; |
| 345 | wdata->input->id.bustype = wdata->hdev->bus; |
| 346 | wdata->input->id.vendor = wdata->hdev->vendor; |
| 347 | wdata->input->id.product = wdata->hdev->product; |
| 348 | wdata->input->id.version = wdata->hdev->version; |
| 349 | wdata->input->name = WIIMOTE_NAME; |
| 350 | |
David Herrmann | 1abb9ad | 2011-07-05 13:45:16 +0200 | [diff] [blame] | 351 | set_bit(EV_KEY, wdata->input->evbit); |
| 352 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) |
| 353 | set_bit(wiiproto_keymap[i], wdata->input->keybit); |
| 354 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 355 | spin_lock_init(&wdata->qlock); |
| 356 | INIT_WORK(&wdata->worker, wiimote_worker); |
| 357 | |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 358 | spin_lock_init(&wdata->state.lock); |
| 359 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 360 | return wdata; |
| 361 | } |
| 362 | |
| 363 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 364 | { |
| 365 | kfree(wdata); |
| 366 | } |
| 367 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 368 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 369 | const struct hid_device_id *id) |
| 370 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 371 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 372 | int ret; |
| 373 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 374 | wdata = wiimote_create(hdev); |
| 375 | if (!wdata) { |
| 376 | hid_err(hdev, "Can't alloc device\n"); |
| 377 | return -ENOMEM; |
| 378 | } |
| 379 | |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 380 | ret = device_create_file(&hdev->dev, &dev_attr_led1); |
| 381 | if (ret) |
| 382 | goto err; |
| 383 | ret = device_create_file(&hdev->dev, &dev_attr_led2); |
| 384 | if (ret) |
| 385 | goto err; |
| 386 | ret = device_create_file(&hdev->dev, &dev_attr_led3); |
| 387 | if (ret) |
| 388 | goto err; |
| 389 | ret = device_create_file(&hdev->dev, &dev_attr_led4); |
| 390 | if (ret) |
| 391 | goto err; |
| 392 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 393 | ret = hid_parse(hdev); |
| 394 | if (ret) { |
| 395 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 396 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 400 | if (ret) { |
| 401 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 402 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 403 | } |
| 404 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 405 | ret = input_register_device(wdata->input); |
| 406 | if (ret) { |
| 407 | hid_err(hdev, "Cannot register input device\n"); |
| 408 | goto err_stop; |
| 409 | } |
| 410 | |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 411 | /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */ |
| 412 | smp_wmb(); |
| 413 | atomic_set(&wdata->ready, 1); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 414 | hid_info(hdev, "New device registered\n"); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 415 | |
| 416 | /* by default set led1 after device initialization */ |
| 417 | spin_lock_irq(&wdata->state.lock); |
David Herrmann | db30834 | 2011-07-05 13:45:17 +0200 | [diff] [blame] | 418 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
David Herrmann | 32a0d9a | 2011-07-05 13:45:18 +0200 | [diff] [blame] | 419 | spin_unlock_irq(&wdata->state.lock); |
| 420 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 421 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 422 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 423 | err_stop: |
| 424 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 425 | err: |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 426 | input_free_device(wdata->input); |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 427 | device_remove_file(&hdev->dev, &dev_attr_led1); |
| 428 | device_remove_file(&hdev->dev, &dev_attr_led2); |
| 429 | device_remove_file(&hdev->dev, &dev_attr_led3); |
| 430 | device_remove_file(&hdev->dev, &dev_attr_led4); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 431 | wiimote_destroy(wdata); |
| 432 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 436 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 437 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 438 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 439 | hid_info(hdev, "Device removed\n"); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 440 | |
David Herrmann | 3c1c2fc | 2011-07-05 13:45:19 +0200 | [diff] [blame] | 441 | device_remove_file(&hdev->dev, &dev_attr_led1); |
| 442 | device_remove_file(&hdev->dev, &dev_attr_led2); |
| 443 | device_remove_file(&hdev->dev, &dev_attr_led3); |
| 444 | device_remove_file(&hdev->dev, &dev_attr_led4); |
| 445 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 446 | hid_hw_stop(hdev); |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 447 | input_unregister_device(wdata->input); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame] | 448 | |
| 449 | cancel_work_sync(&wdata->worker); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 450 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 454 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 455 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
| 456 | { } |
| 457 | }; |
| 458 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 459 | |
| 460 | static struct hid_driver wiimote_hid_driver = { |
| 461 | .name = "wiimote", |
| 462 | .id_table = wiimote_hid_devices, |
| 463 | .probe = wiimote_hid_probe, |
| 464 | .remove = wiimote_hid_remove, |
| 465 | .raw_event = wiimote_hid_event, |
| 466 | }; |
| 467 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 468 | static int __init wiimote_init(void) |
| 469 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 470 | int ret; |
| 471 | |
| 472 | ret = hid_register_driver(&wiimote_hid_driver); |
| 473 | if (ret) |
| 474 | pr_err("Can't register wiimote hid driver\n"); |
| 475 | |
| 476 | return ret; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void __exit wiimote_exit(void) |
| 480 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 481 | hid_unregister_driver(&wiimote_hid_driver); |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | module_init(wiimote_init); |
| 485 | module_exit(wiimote_exit); |
| 486 | MODULE_LICENSE("GPL"); |
| 487 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
| 488 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); |
| 489 | MODULE_VERSION(WIIMOTE_VERSION); |