blob: ff7cf129710f9e913c0f48956caa50265096e9f9 [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 Herrmann02fb72a2011-07-05 13:45:09 +020013#include <linux/hid.h>
David Herrmannfb51b442011-07-05 13:45:08 +020014#include <linux/module.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020015#include "hid-ids.h"
David Herrmannfb51b442011-07-05 13:45:08 +020016
17#define WIIMOTE_VERSION "0.1"
18#define WIIMOTE_NAME "Nintendo Wii Remote"
19
David Herrmanne894d0e2011-07-05 13:45:10 +020020struct wiimote_data {
21 struct hid_device *hdev;
22};
23
David Herrmann02fb72a2011-07-05 13:45:09 +020024static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
25 u8 *raw_data, int size)
26{
27 if (size < 1)
28 return -EINVAL;
29
30 return 0;
31}
32
David Herrmanne894d0e2011-07-05 13:45:10 +020033static struct wiimote_data *wiimote_create(struct hid_device *hdev)
34{
35 struct wiimote_data *wdata;
36
37 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
38 if (!wdata)
39 return NULL;
40
41 wdata->hdev = hdev;
42 hid_set_drvdata(hdev, wdata);
43
44 return wdata;
45}
46
47static void wiimote_destroy(struct wiimote_data *wdata)
48{
49 kfree(wdata);
50}
51
David Herrmann02fb72a2011-07-05 13:45:09 +020052static int wiimote_hid_probe(struct hid_device *hdev,
53 const struct hid_device_id *id)
54{
David Herrmanne894d0e2011-07-05 13:45:10 +020055 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +020056 int ret;
57
David Herrmanne894d0e2011-07-05 13:45:10 +020058 wdata = wiimote_create(hdev);
59 if (!wdata) {
60 hid_err(hdev, "Can't alloc device\n");
61 return -ENOMEM;
62 }
63
David Herrmann02fb72a2011-07-05 13:45:09 +020064 ret = hid_parse(hdev);
65 if (ret) {
66 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +020067 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +020068 }
69
70 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
71 if (ret) {
72 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +020073 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +020074 }
75
76 hid_info(hdev, "New device registered\n");
77 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +020078
79err:
80 wiimote_destroy(wdata);
81 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +020082}
83
84static void wiimote_hid_remove(struct hid_device *hdev)
85{
David Herrmanne894d0e2011-07-05 13:45:10 +020086 struct wiimote_data *wdata = hid_get_drvdata(hdev);
87
David Herrmann02fb72a2011-07-05 13:45:09 +020088 hid_info(hdev, "Device removed\n");
89 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +020090 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +020091}
92
93static const struct hid_device_id wiimote_hid_devices[] = {
94 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
95 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
96 { }
97};
98MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
99
100static struct hid_driver wiimote_hid_driver = {
101 .name = "wiimote",
102 .id_table = wiimote_hid_devices,
103 .probe = wiimote_hid_probe,
104 .remove = wiimote_hid_remove,
105 .raw_event = wiimote_hid_event,
106};
107
David Herrmannfb51b442011-07-05 13:45:08 +0200108static int __init wiimote_init(void)
109{
David Herrmann02fb72a2011-07-05 13:45:09 +0200110 int ret;
111
112 ret = hid_register_driver(&wiimote_hid_driver);
113 if (ret)
114 pr_err("Can't register wiimote hid driver\n");
115
116 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +0200117}
118
119static void __exit wiimote_exit(void)
120{
David Herrmann02fb72a2011-07-05 13:45:09 +0200121 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200122}
123
124module_init(wiimote_init);
125module_exit(wiimote_exit);
126MODULE_LICENSE("GPL");
127MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
128MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
129MODULE_VERSION(WIIMOTE_VERSION);