blob: bfc50493ec6b14cc894128b0142c57fa549982c9 [file] [log] [blame]
David Herrmannfb51b442011-07-05 13:45:08 +02001/*
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 Herrmann4d36e972011-07-05 13:45:12 +020013#include <linux/atomic.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020014#include <linux/device.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020015#include <linux/hid.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020016#include <linux/input.h>
David Herrmannfb51b442011-07-05 13:45:08 +020017#include <linux/module.h>
David Herrmann23c063c2011-07-05 13:45:14 +020018#include <linux/spinlock.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020019#include "hid-ids.h"
David Herrmannfb51b442011-07-05 13:45:08 +020020
21#define WIIMOTE_VERSION "0.1"
22#define WIIMOTE_NAME "Nintendo Wii Remote"
David Herrmann23c063c2011-07-05 13:45:14 +020023#define WIIMOTE_BUFSIZE 32
24
25struct wiimote_buf {
26 __u8 data[HID_MAX_BUFFER_SIZE];
27 size_t size;
28};
David Herrmannfb51b442011-07-05 13:45:08 +020029
David Herrmanne894d0e2011-07-05 13:45:10 +020030struct wiimote_data {
David Herrmann4d36e972011-07-05 13:45:12 +020031 atomic_t ready;
David Herrmanne894d0e2011-07-05 13:45:10 +020032 struct hid_device *hdev;
David Herrmann672bc4e2011-07-05 13:45:11 +020033 struct input_dev *input;
David Herrmann23c063c2011-07-05 13:45:14 +020034
35 spinlock_t qlock;
36 __u8 head;
37 __u8 tail;
38 struct wiimote_buf outq[WIIMOTE_BUFSIZE];
39 struct work_struct worker;
David Herrmanne894d0e2011-07-05 13:45:10 +020040};
41
David Herrmann0c218f12011-07-05 13:45:13 +020042static 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 Herrmann23c063c2011-07-05 13:45:14 +020061static 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
81static 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 Herrmann672bc4e2011-07-05 13:45:11 +0200120static int wiimote_input_event(struct input_dev *dev, unsigned int type,
121 unsigned int code, int value)
122{
David Herrmann4d36e972011-07-05 13:45:12 +0200123 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 Herrmann672bc4e2011-07-05 13:45:11 +0200130 return 0;
131}
132
David Herrmann02fb72a2011-07-05 13:45:09 +0200133static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
134 u8 *raw_data, int size)
135{
David Herrmann4d36e972011-07-05 13:45:12 +0200136 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 Herrmann02fb72a2011-07-05 13:45:09 +0200143 if (size < 1)
144 return -EINVAL;
145
146 return 0;
147}
148
David Herrmanne894d0e2011-07-05 13:45:10 +0200149static 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 Herrmann672bc4e2011-07-05 13:45:11 +0200157 wdata->input = input_allocate_device();
158 if (!wdata->input) {
159 kfree(wdata);
160 return NULL;
161 }
162
David Herrmanne894d0e2011-07-05 13:45:10 +0200163 wdata->hdev = hdev;
164 hid_set_drvdata(hdev, wdata);
165
David Herrmann672bc4e2011-07-05 13:45:11 +0200166 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 Herrmann23c063c2011-07-05 13:45:14 +0200175 spin_lock_init(&wdata->qlock);
176 INIT_WORK(&wdata->worker, wiimote_worker);
177
David Herrmanne894d0e2011-07-05 13:45:10 +0200178 return wdata;
179}
180
181static void wiimote_destroy(struct wiimote_data *wdata)
182{
183 kfree(wdata);
184}
185
David Herrmann02fb72a2011-07-05 13:45:09 +0200186static int wiimote_hid_probe(struct hid_device *hdev,
187 const struct hid_device_id *id)
188{
David Herrmanne894d0e2011-07-05 13:45:10 +0200189 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +0200190 int ret;
191
David Herrmanne894d0e2011-07-05 13:45:10 +0200192 wdata = wiimote_create(hdev);
193 if (!wdata) {
194 hid_err(hdev, "Can't alloc device\n");
195 return -ENOMEM;
196 }
197
David Herrmann02fb72a2011-07-05 13:45:09 +0200198 ret = hid_parse(hdev);
199 if (ret) {
200 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200201 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200202 }
203
204 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
205 if (ret) {
206 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200207 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200208 }
209
David Herrmann672bc4e2011-07-05 13:45:11 +0200210 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 Herrmann4d36e972011-07-05 13:45:12 +0200216 /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
217 smp_wmb();
218 atomic_set(&wdata->ready, 1);
David Herrmann02fb72a2011-07-05 13:45:09 +0200219 hid_info(hdev, "New device registered\n");
220 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +0200221
David Herrmann672bc4e2011-07-05 13:45:11 +0200222err_stop:
223 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +0200224err:
David Herrmann672bc4e2011-07-05 13:45:11 +0200225 input_free_device(wdata->input);
David Herrmanne894d0e2011-07-05 13:45:10 +0200226 wiimote_destroy(wdata);
227 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +0200228}
229
230static void wiimote_hid_remove(struct hid_device *hdev)
231{
David Herrmanne894d0e2011-07-05 13:45:10 +0200232 struct wiimote_data *wdata = hid_get_drvdata(hdev);
233
David Herrmann02fb72a2011-07-05 13:45:09 +0200234 hid_info(hdev, "Device removed\n");
David Herrmann23c063c2011-07-05 13:45:14 +0200235
David Herrmann02fb72a2011-07-05 13:45:09 +0200236 hid_hw_stop(hdev);
David Herrmann672bc4e2011-07-05 13:45:11 +0200237 input_unregister_device(wdata->input);
David Herrmann23c063c2011-07-05 13:45:14 +0200238
239 cancel_work_sync(&wdata->worker);
David Herrmanne894d0e2011-07-05 13:45:10 +0200240 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +0200241}
242
243static const struct hid_device_id wiimote_hid_devices[] = {
244 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
245 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
246 { }
247};
248MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
249
250static 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 Herrmannfb51b442011-07-05 13:45:08 +0200258static int __init wiimote_init(void)
259{
David Herrmann02fb72a2011-07-05 13:45:09 +0200260 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 Herrmannfb51b442011-07-05 13:45:08 +0200267}
268
269static void __exit wiimote_exit(void)
270{
David Herrmann02fb72a2011-07-05 13:45:09 +0200271 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200272}
273
274module_init(wiimote_init);
275module_exit(wiimote_exit);
276MODULE_LICENSE("GPL");
277MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
278MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
279MODULE_VERSION(WIIMOTE_VERSION);