blob: 3416f69302cd44bb440ffbc92527cea3435acb94 [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 Herrmann672bc4e2011-07-05 13:45:11 +020029static int wiimote_input_event(struct input_dev *dev, unsigned int type,
30 unsigned int code, int value)
31{
David Herrmann4d36e972011-07-05 13:45:12 +020032 struct wiimote_data *wdata = input_get_drvdata(dev);
33
34 if (!atomic_read(&wdata->ready))
35 return -EBUSY;
36 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
37 smp_rmb();
38
David Herrmann672bc4e2011-07-05 13:45:11 +020039 return 0;
40}
41
David Herrmann02fb72a2011-07-05 13:45:09 +020042static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
43 u8 *raw_data, int size)
44{
David Herrmann4d36e972011-07-05 13:45:12 +020045 struct wiimote_data *wdata = hid_get_drvdata(hdev);
46
47 if (!atomic_read(&wdata->ready))
48 return -EBUSY;
49 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
50 smp_rmb();
51
David Herrmann02fb72a2011-07-05 13:45:09 +020052 if (size < 1)
53 return -EINVAL;
54
55 return 0;
56}
57
David Herrmanne894d0e2011-07-05 13:45:10 +020058static struct wiimote_data *wiimote_create(struct hid_device *hdev)
59{
60 struct wiimote_data *wdata;
61
62 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
63 if (!wdata)
64 return NULL;
65
David Herrmann672bc4e2011-07-05 13:45:11 +020066 wdata->input = input_allocate_device();
67 if (!wdata->input) {
68 kfree(wdata);
69 return NULL;
70 }
71
David Herrmanne894d0e2011-07-05 13:45:10 +020072 wdata->hdev = hdev;
73 hid_set_drvdata(hdev, wdata);
74
David Herrmann672bc4e2011-07-05 13:45:11 +020075 input_set_drvdata(wdata->input, wdata);
76 wdata->input->event = wiimote_input_event;
77 wdata->input->dev.parent = &wdata->hdev->dev;
78 wdata->input->id.bustype = wdata->hdev->bus;
79 wdata->input->id.vendor = wdata->hdev->vendor;
80 wdata->input->id.product = wdata->hdev->product;
81 wdata->input->id.version = wdata->hdev->version;
82 wdata->input->name = WIIMOTE_NAME;
83
David Herrmanne894d0e2011-07-05 13:45:10 +020084 return wdata;
85}
86
87static void wiimote_destroy(struct wiimote_data *wdata)
88{
89 kfree(wdata);
90}
91
David Herrmann02fb72a2011-07-05 13:45:09 +020092static int wiimote_hid_probe(struct hid_device *hdev,
93 const struct hid_device_id *id)
94{
David Herrmanne894d0e2011-07-05 13:45:10 +020095 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +020096 int ret;
97
David Herrmanne894d0e2011-07-05 13:45:10 +020098 wdata = wiimote_create(hdev);
99 if (!wdata) {
100 hid_err(hdev, "Can't alloc device\n");
101 return -ENOMEM;
102 }
103
David Herrmann02fb72a2011-07-05 13:45:09 +0200104 ret = hid_parse(hdev);
105 if (ret) {
106 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200107 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200108 }
109
110 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
111 if (ret) {
112 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200113 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200114 }
115
David Herrmann672bc4e2011-07-05 13:45:11 +0200116 ret = input_register_device(wdata->input);
117 if (ret) {
118 hid_err(hdev, "Cannot register input device\n");
119 goto err_stop;
120 }
121
David Herrmann4d36e972011-07-05 13:45:12 +0200122 /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
123 smp_wmb();
124 atomic_set(&wdata->ready, 1);
David Herrmann02fb72a2011-07-05 13:45:09 +0200125 hid_info(hdev, "New device registered\n");
126 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +0200127
David Herrmann672bc4e2011-07-05 13:45:11 +0200128err_stop:
129 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +0200130err:
David Herrmann672bc4e2011-07-05 13:45:11 +0200131 input_free_device(wdata->input);
David Herrmanne894d0e2011-07-05 13:45:10 +0200132 wiimote_destroy(wdata);
133 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +0200134}
135
136static void wiimote_hid_remove(struct hid_device *hdev)
137{
David Herrmanne894d0e2011-07-05 13:45:10 +0200138 struct wiimote_data *wdata = hid_get_drvdata(hdev);
139
David Herrmann02fb72a2011-07-05 13:45:09 +0200140 hid_info(hdev, "Device removed\n");
141 hid_hw_stop(hdev);
David Herrmann672bc4e2011-07-05 13:45:11 +0200142 input_unregister_device(wdata->input);
David Herrmanne894d0e2011-07-05 13:45:10 +0200143 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +0200144}
145
146static const struct hid_device_id wiimote_hid_devices[] = {
147 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
148 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
149 { }
150};
151MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
152
153static struct hid_driver wiimote_hid_driver = {
154 .name = "wiimote",
155 .id_table = wiimote_hid_devices,
156 .probe = wiimote_hid_probe,
157 .remove = wiimote_hid_remove,
158 .raw_event = wiimote_hid_event,
159};
160
David Herrmannfb51b442011-07-05 13:45:08 +0200161static int __init wiimote_init(void)
162{
David Herrmann02fb72a2011-07-05 13:45:09 +0200163 int ret;
164
165 ret = hid_register_driver(&wiimote_hid_driver);
166 if (ret)
167 pr_err("Can't register wiimote hid driver\n");
168
169 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +0200170}
171
172static void __exit wiimote_exit(void)
173{
David Herrmann02fb72a2011-07-05 13:45:09 +0200174 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200175}
176
177module_init(wiimote_init);
178module_exit(wiimote_exit);
179MODULE_LICENSE("GPL");
180MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
181MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
182MODULE_VERSION(WIIMOTE_VERSION);