2 * HID driver for Nintendo Wiimote devices
3 * Copyright (c) 2011 David Herrmann
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)
13 #include <linux/atomic.h>
14 #include <linux/device.h>
15 #include <linux/hid.h>
16 #include <linux/input.h>
17 #include <linux/module.h>
20 #define WIIMOTE_VERSION "0.1"
21 #define WIIMOTE_NAME "Nintendo Wii Remote"
25 struct hid_device
*hdev
;
26 struct input_dev
*input
;
29 static ssize_t
wiimote_hid_send(struct hid_device
*hdev
, __u8
*buffer
,
35 if (!hdev
->hid_output_raw_report
)
38 buf
= kmemdup(buffer
, count
, GFP_KERNEL
);
42 ret
= hdev
->hid_output_raw_report(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
48 static int wiimote_input_event(struct input_dev
*dev
, unsigned int type
,
49 unsigned int code
, int value
)
51 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
53 if (!atomic_read(&wdata
->ready
))
55 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
61 static int wiimote_hid_event(struct hid_device
*hdev
, struct hid_report
*report
,
62 u8
*raw_data
, int size
)
64 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
66 if (!atomic_read(&wdata
->ready
))
68 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
77 static struct wiimote_data
*wiimote_create(struct hid_device
*hdev
)
79 struct wiimote_data
*wdata
;
81 wdata
= kzalloc(sizeof(*wdata
), GFP_KERNEL
);
85 wdata
->input
= input_allocate_device();
92 hid_set_drvdata(hdev
, wdata
);
94 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
;
106 static void wiimote_destroy(struct wiimote_data
*wdata
)
111 static int wiimote_hid_probe(struct hid_device
*hdev
,
112 const struct hid_device_id
*id
)
114 struct wiimote_data
*wdata
;
117 wdata
= wiimote_create(hdev
);
119 hid_err(hdev
, "Can't alloc device\n");
123 ret
= hid_parse(hdev
);
125 hid_err(hdev
, "HID parse failed\n");
129 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
131 hid_err(hdev
, "HW start failed\n");
135 ret
= input_register_device(wdata
->input
);
137 hid_err(hdev
, "Cannot register input device\n");
141 /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
143 atomic_set(&wdata
->ready
, 1);
144 hid_info(hdev
, "New device registered\n");
150 input_free_device(wdata
->input
);
151 wiimote_destroy(wdata
);
155 static void wiimote_hid_remove(struct hid_device
*hdev
)
157 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
159 hid_info(hdev
, "Device removed\n");
161 input_unregister_device(wdata
->input
);
162 wiimote_destroy(wdata
);
165 static const struct hid_device_id wiimote_hid_devices
[] = {
166 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO
,
167 USB_DEVICE_ID_NINTENDO_WIIMOTE
) },
170 MODULE_DEVICE_TABLE(hid
, wiimote_hid_devices
);
172 static struct hid_driver wiimote_hid_driver
= {
174 .id_table
= wiimote_hid_devices
,
175 .probe
= wiimote_hid_probe
,
176 .remove
= wiimote_hid_remove
,
177 .raw_event
= wiimote_hid_event
,
180 static int __init
wiimote_init(void)
184 ret
= hid_register_driver(&wiimote_hid_driver
);
186 pr_err("Can't register wiimote hid driver\n");
191 static void __exit
wiimote_exit(void)
193 hid_unregister_driver(&wiimote_hid_driver
);
196 module_init(wiimote_init
);
197 module_exit(wiimote_exit
);
198 MODULE_LICENSE("GPL");
199 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
200 MODULE_DESCRIPTION(WIIMOTE_NAME
" Device Driver");
201 MODULE_VERSION(WIIMOTE_VERSION
);