blob: 811ed8921013aad9f45421edb2f370ce2a374553 [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 Herrmann02fb72a2011-07-05 13:45:09 +020018#include "hid-ids.h"
David Herrmannfb51b442011-07-05 13:45:08 +020019
20#define WIIMOTE_VERSION "0.1"
21#define WIIMOTE_NAME "Nintendo Wii Remote"
22
David Herrmanne894d0e2011-07-05 13:45:10 +020023struct wiimote_data {
David Herrmann4d36e972011-07-05 13:45:12 +020024 atomic_t ready;
David Herrmanne894d0e2011-07-05 13:45:10 +020025 struct hid_device *hdev;
David Herrmann672bc4e2011-07-05 13:45:11 +020026 struct input_dev *input;
David Herrmanne894d0e2011-07-05 13:45:10 +020027};
28
David Herrmann0c218f12011-07-05 13:45:13 +020029static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
30 size_t count)
31{
32 __u8 *buf;
33 ssize_t ret;
34
35 if (!hdev->hid_output_raw_report)
36 return -ENODEV;
37
38 buf = kmemdup(buffer, count, GFP_KERNEL);
39 if (!buf)
40 return -ENOMEM;
41
42 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
43
44 kfree(buf);
45 return ret;
46}
47
David Herrmann672bc4e2011-07-05 13:45:11 +020048static int wiimote_input_event(struct input_dev *dev, unsigned int type,
49 unsigned int code, int value)
50{
David Herrmann4d36e972011-07-05 13:45:12 +020051 struct wiimote_data *wdata = input_get_drvdata(dev);
52
53 if (!atomic_read(&wdata->ready))
54 return -EBUSY;
55 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
56 smp_rmb();
57
David Herrmann672bc4e2011-07-05 13:45:11 +020058 return 0;
59}
60
David Herrmann02fb72a2011-07-05 13:45:09 +020061static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
62 u8 *raw_data, int size)
63{
David Herrmann4d36e972011-07-05 13:45:12 +020064 struct wiimote_data *wdata = hid_get_drvdata(hdev);
65
66 if (!atomic_read(&wdata->ready))
67 return -EBUSY;
68 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
69 smp_rmb();
70
David Herrmann02fb72a2011-07-05 13:45:09 +020071 if (size < 1)
72 return -EINVAL;
73
74 return 0;
75}
76
David Herrmanne894d0e2011-07-05 13:45:10 +020077static struct wiimote_data *wiimote_create(struct hid_device *hdev)
78{
79 struct wiimote_data *wdata;
80
81 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
82 if (!wdata)
83 return NULL;
84
David Herrmann672bc4e2011-07-05 13:45:11 +020085 wdata->input = input_allocate_device();
86 if (!wdata->input) {
87 kfree(wdata);
88 return NULL;
89 }
90
David Herrmanne894d0e2011-07-05 13:45:10 +020091 wdata->hdev = hdev;
92 hid_set_drvdata(hdev, wdata);
93
David Herrmann672bc4e2011-07-05 13:45:11 +020094 input_set_drvdata(wdata->input, wdata);
95 wdata->input->event = wiimote_input_event;
96 wdata->input->dev.parent = &wdata->hdev->dev;
97 wdata->input->id.bustype = wdata->hdev->bus;
98 wdata->input->id.vendor = wdata->hdev->vendor;
99 wdata->input->id.product = wdata->hdev->product;
100 wdata->input->id.version = wdata->hdev->version;
101 wdata->input->name = WIIMOTE_NAME;
102
David Herrmanne894d0e2011-07-05 13:45:10 +0200103 return wdata;
104}
105
106static void wiimote_destroy(struct wiimote_data *wdata)
107{
108 kfree(wdata);
109}
110
David Herrmann02fb72a2011-07-05 13:45:09 +0200111static int wiimote_hid_probe(struct hid_device *hdev,
112 const struct hid_device_id *id)
113{
David Herrmanne894d0e2011-07-05 13:45:10 +0200114 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +0200115 int ret;
116
David Herrmanne894d0e2011-07-05 13:45:10 +0200117 wdata = wiimote_create(hdev);
118 if (!wdata) {
119 hid_err(hdev, "Can't alloc device\n");
120 return -ENOMEM;
121 }
122
David Herrmann02fb72a2011-07-05 13:45:09 +0200123 ret = hid_parse(hdev);
124 if (ret) {
125 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200126 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200127 }
128
129 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
130 if (ret) {
131 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200132 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200133 }
134
David Herrmann672bc4e2011-07-05 13:45:11 +0200135 ret = input_register_device(wdata->input);
136 if (ret) {
137 hid_err(hdev, "Cannot register input device\n");
138 goto err_stop;
139 }
140
David Herrmann4d36e972011-07-05 13:45:12 +0200141 /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
142 smp_wmb();
143 atomic_set(&wdata->ready, 1);
David Herrmann02fb72a2011-07-05 13:45:09 +0200144 hid_info(hdev, "New device registered\n");
145 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +0200146
David Herrmann672bc4e2011-07-05 13:45:11 +0200147err_stop:
148 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +0200149err:
David Herrmann672bc4e2011-07-05 13:45:11 +0200150 input_free_device(wdata->input);
David Herrmanne894d0e2011-07-05 13:45:10 +0200151 wiimote_destroy(wdata);
152 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +0200153}
154
155static void wiimote_hid_remove(struct hid_device *hdev)
156{
David Herrmanne894d0e2011-07-05 13:45:10 +0200157 struct wiimote_data *wdata = hid_get_drvdata(hdev);
158
David Herrmann02fb72a2011-07-05 13:45:09 +0200159 hid_info(hdev, "Device removed\n");
160 hid_hw_stop(hdev);
David Herrmann672bc4e2011-07-05 13:45:11 +0200161 input_unregister_device(wdata->input);
David Herrmanne894d0e2011-07-05 13:45:10 +0200162 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +0200163}
164
165static const struct hid_device_id wiimote_hid_devices[] = {
166 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
167 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
168 { }
169};
170MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
171
172static struct hid_driver wiimote_hid_driver = {
173 .name = "wiimote",
174 .id_table = wiimote_hid_devices,
175 .probe = wiimote_hid_probe,
176 .remove = wiimote_hid_remove,
177 .raw_event = wiimote_hid_event,
178};
179
David Herrmannfb51b442011-07-05 13:45:08 +0200180static int __init wiimote_init(void)
181{
David Herrmann02fb72a2011-07-05 13:45:09 +0200182 int ret;
183
184 ret = hid_register_driver(&wiimote_hid_driver);
185 if (ret)
186 pr_err("Can't register wiimote hid driver\n");
187
188 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +0200189}
190
191static void __exit wiimote_exit(void)
192{
David Herrmann02fb72a2011-07-05 13:45:09 +0200193 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200194}
195
196module_init(wiimote_init);
197module_exit(wiimote_exit);
198MODULE_LICENSE("GPL");
199MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
200MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
201MODULE_VERSION(WIIMOTE_VERSION);