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 | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 30 | struct wiimote_data { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 31 | atomic_t ready; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 32 | struct hid_device *hdev; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 33 | struct input_dev *input; |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame^] | 34 | |
| 35 | spinlock_t qlock; |
| 36 | __u8 head; |
| 37 | __u8 tail; |
| 38 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; |
| 39 | struct work_struct worker; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
David Herrmann | 0c218f1 | 2011-07-05 13:45:13 +0200 | [diff] [blame] | 42 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
| 43 | size_t count) |
| 44 | { |
| 45 | __u8 *buf; |
| 46 | ssize_t ret; |
| 47 | |
| 48 | if (!hdev->hid_output_raw_report) |
| 49 | return -ENODEV; |
| 50 | |
| 51 | buf = kmemdup(buffer, count, GFP_KERNEL); |
| 52 | if (!buf) |
| 53 | return -ENOMEM; |
| 54 | |
| 55 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); |
| 56 | |
| 57 | kfree(buf); |
| 58 | return ret; |
| 59 | } |
| 60 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame^] | 61 | static void wiimote_worker(struct work_struct *work) |
| 62 | { |
| 63 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, |
| 64 | worker); |
| 65 | unsigned long flags; |
| 66 | |
| 67 | spin_lock_irqsave(&wdata->qlock, flags); |
| 68 | |
| 69 | while (wdata->head != wdata->tail) { |
| 70 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 71 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, |
| 72 | wdata->outq[wdata->tail].size); |
| 73 | spin_lock_irqsave(&wdata->qlock, flags); |
| 74 | |
| 75 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; |
| 76 | } |
| 77 | |
| 78 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 79 | } |
| 80 | |
| 81 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, |
| 82 | size_t count) |
| 83 | { |
| 84 | unsigned long flags; |
| 85 | __u8 newhead; |
| 86 | |
| 87 | if (count > HID_MAX_BUFFER_SIZE) { |
| 88 | hid_warn(wdata->hdev, "Sending too large output report\n"); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Copy new request into our output queue and check whether the |
| 94 | * queue is full. If it is full, discard this request. |
| 95 | * If it is empty we need to start a new worker that will |
| 96 | * send out the buffer to the hid device. |
| 97 | * If the queue is not empty, then there must be a worker |
| 98 | * that is currently sending out our buffer and this worker |
| 99 | * will reschedule itself until the queue is empty. |
| 100 | */ |
| 101 | |
| 102 | spin_lock_irqsave(&wdata->qlock, flags); |
| 103 | |
| 104 | memcpy(wdata->outq[wdata->head].data, buffer, count); |
| 105 | wdata->outq[wdata->head].size = count; |
| 106 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; |
| 107 | |
| 108 | if (wdata->head == wdata->tail) { |
| 109 | wdata->head = newhead; |
| 110 | schedule_work(&wdata->worker); |
| 111 | } else if (newhead != wdata->tail) { |
| 112 | wdata->head = newhead; |
| 113 | } else { |
| 114 | hid_warn(wdata->hdev, "Output queue is full"); |
| 115 | } |
| 116 | |
| 117 | spin_unlock_irqrestore(&wdata->qlock, flags); |
| 118 | } |
| 119 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 120 | static int wiimote_input_event(struct input_dev *dev, unsigned int type, |
| 121 | unsigned int code, int value) |
| 122 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 123 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 124 | |
| 125 | if (!atomic_read(&wdata->ready)) |
| 126 | return -EBUSY; |
| 127 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ |
| 128 | smp_rmb(); |
| 129 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 133 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
| 134 | u8 *raw_data, int size) |
| 135 | { |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 136 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 137 | |
| 138 | if (!atomic_read(&wdata->ready)) |
| 139 | return -EBUSY; |
| 140 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ |
| 141 | smp_rmb(); |
| 142 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 143 | if (size < 1) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 149 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
| 150 | { |
| 151 | struct wiimote_data *wdata; |
| 152 | |
| 153 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); |
| 154 | if (!wdata) |
| 155 | return NULL; |
| 156 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 157 | wdata->input = input_allocate_device(); |
| 158 | if (!wdata->input) { |
| 159 | kfree(wdata); |
| 160 | return NULL; |
| 161 | } |
| 162 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 163 | wdata->hdev = hdev; |
| 164 | hid_set_drvdata(hdev, wdata); |
| 165 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 166 | input_set_drvdata(wdata->input, wdata); |
| 167 | wdata->input->event = wiimote_input_event; |
| 168 | wdata->input->dev.parent = &wdata->hdev->dev; |
| 169 | wdata->input->id.bustype = wdata->hdev->bus; |
| 170 | wdata->input->id.vendor = wdata->hdev->vendor; |
| 171 | wdata->input->id.product = wdata->hdev->product; |
| 172 | wdata->input->id.version = wdata->hdev->version; |
| 173 | wdata->input->name = WIIMOTE_NAME; |
| 174 | |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame^] | 175 | spin_lock_init(&wdata->qlock); |
| 176 | INIT_WORK(&wdata->worker, wiimote_worker); |
| 177 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 178 | return wdata; |
| 179 | } |
| 180 | |
| 181 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 182 | { |
| 183 | kfree(wdata); |
| 184 | } |
| 185 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 186 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 187 | const struct hid_device_id *id) |
| 188 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 189 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 190 | int ret; |
| 191 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 192 | wdata = wiimote_create(hdev); |
| 193 | if (!wdata) { |
| 194 | hid_err(hdev, "Can't alloc device\n"); |
| 195 | return -ENOMEM; |
| 196 | } |
| 197 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 198 | ret = hid_parse(hdev); |
| 199 | if (ret) { |
| 200 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 201 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 205 | if (ret) { |
| 206 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 207 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 208 | } |
| 209 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 210 | ret = input_register_device(wdata->input); |
| 211 | if (ret) { |
| 212 | hid_err(hdev, "Cannot register input device\n"); |
| 213 | goto err_stop; |
| 214 | } |
| 215 | |
David Herrmann | 4d36e97 | 2011-07-05 13:45:12 +0200 | [diff] [blame] | 216 | /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */ |
| 217 | smp_wmb(); |
| 218 | atomic_set(&wdata->ready, 1); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 219 | hid_info(hdev, "New device registered\n"); |
| 220 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 221 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 222 | err_stop: |
| 223 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 224 | err: |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 225 | input_free_device(wdata->input); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 226 | wiimote_destroy(wdata); |
| 227 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 231 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 232 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 233 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 234 | hid_info(hdev, "Device removed\n"); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame^] | 235 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 236 | hid_hw_stop(hdev); |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame] | 237 | input_unregister_device(wdata->input); |
David Herrmann | 23c063c | 2011-07-05 13:45:14 +0200 | [diff] [blame^] | 238 | |
| 239 | cancel_work_sync(&wdata->worker); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 240 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 244 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 245 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
| 246 | { } |
| 247 | }; |
| 248 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 249 | |
| 250 | static struct hid_driver wiimote_hid_driver = { |
| 251 | .name = "wiimote", |
| 252 | .id_table = wiimote_hid_devices, |
| 253 | .probe = wiimote_hid_probe, |
| 254 | .remove = wiimote_hid_remove, |
| 255 | .raw_event = wiimote_hid_event, |
| 256 | }; |
| 257 | |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 258 | static int __init wiimote_init(void) |
| 259 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 260 | int ret; |
| 261 | |
| 262 | ret = hid_register_driver(&wiimote_hid_driver); |
| 263 | if (ret) |
| 264 | pr_err("Can't register wiimote hid driver\n"); |
| 265 | |
| 266 | return ret; |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void __exit wiimote_exit(void) |
| 270 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 271 | hid_unregister_driver(&wiimote_hid_driver); |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | module_init(wiimote_init); |
| 275 | module_exit(wiimote_exit); |
| 276 | MODULE_LICENSE("GPL"); |
| 277 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
| 278 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); |
| 279 | MODULE_VERSION(WIIMOTE_VERSION); |