blob: deaf232078122450ab31ccedf6a48515305012bd [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 Herrmann672bc4e2011-07-05 13:45:11 +020013#include <linux/device.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020014#include <linux/hid.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020015#include <linux/input.h>
David Herrmannfb51b442011-07-05 13:45:08 +020016#include <linux/module.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020017#include "hid-ids.h"
David Herrmannfb51b442011-07-05 13:45:08 +020018
19#define WIIMOTE_VERSION "0.1"
20#define WIIMOTE_NAME "Nintendo Wii Remote"
21
David Herrmanne894d0e2011-07-05 13:45:10 +020022struct wiimote_data {
23 struct hid_device *hdev;
David Herrmann672bc4e2011-07-05 13:45:11 +020024 struct input_dev *input;
David Herrmanne894d0e2011-07-05 13:45:10 +020025};
26
David Herrmann672bc4e2011-07-05 13:45:11 +020027static int wiimote_input_event(struct input_dev *dev, unsigned int type,
28 unsigned int code, int value)
29{
30 return 0;
31}
32
David Herrmann02fb72a2011-07-05 13:45:09 +020033static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
34 u8 *raw_data, int size)
35{
36 if (size < 1)
37 return -EINVAL;
38
39 return 0;
40}
41
David Herrmanne894d0e2011-07-05 13:45:10 +020042static struct wiimote_data *wiimote_create(struct hid_device *hdev)
43{
44 struct wiimote_data *wdata;
45
46 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
47 if (!wdata)
48 return NULL;
49
David Herrmann672bc4e2011-07-05 13:45:11 +020050 wdata->input = input_allocate_device();
51 if (!wdata->input) {
52 kfree(wdata);
53 return NULL;
54 }
55
David Herrmanne894d0e2011-07-05 13:45:10 +020056 wdata->hdev = hdev;
57 hid_set_drvdata(hdev, wdata);
58
David Herrmann672bc4e2011-07-05 13:45:11 +020059 input_set_drvdata(wdata->input, wdata);
60 wdata->input->event = wiimote_input_event;
61 wdata->input->dev.parent = &wdata->hdev->dev;
62 wdata->input->id.bustype = wdata->hdev->bus;
63 wdata->input->id.vendor = wdata->hdev->vendor;
64 wdata->input->id.product = wdata->hdev->product;
65 wdata->input->id.version = wdata->hdev->version;
66 wdata->input->name = WIIMOTE_NAME;
67
David Herrmanne894d0e2011-07-05 13:45:10 +020068 return wdata;
69}
70
71static void wiimote_destroy(struct wiimote_data *wdata)
72{
73 kfree(wdata);
74}
75
David Herrmann02fb72a2011-07-05 13:45:09 +020076static int wiimote_hid_probe(struct hid_device *hdev,
77 const struct hid_device_id *id)
78{
David Herrmanne894d0e2011-07-05 13:45:10 +020079 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +020080 int ret;
81
David Herrmanne894d0e2011-07-05 13:45:10 +020082 wdata = wiimote_create(hdev);
83 if (!wdata) {
84 hid_err(hdev, "Can't alloc device\n");
85 return -ENOMEM;
86 }
87
David Herrmann02fb72a2011-07-05 13:45:09 +020088 ret = hid_parse(hdev);
89 if (ret) {
90 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +020091 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +020092 }
93
94 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
95 if (ret) {
96 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +020097 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +020098 }
99
David Herrmann672bc4e2011-07-05 13:45:11 +0200100 ret = input_register_device(wdata->input);
101 if (ret) {
102 hid_err(hdev, "Cannot register input device\n");
103 goto err_stop;
104 }
105
David Herrmann02fb72a2011-07-05 13:45:09 +0200106 hid_info(hdev, "New device registered\n");
107 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +0200108
David Herrmann672bc4e2011-07-05 13:45:11 +0200109err_stop:
110 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +0200111err:
David Herrmann672bc4e2011-07-05 13:45:11 +0200112 input_free_device(wdata->input);
David Herrmanne894d0e2011-07-05 13:45:10 +0200113 wiimote_destroy(wdata);
114 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +0200115}
116
117static void wiimote_hid_remove(struct hid_device *hdev)
118{
David Herrmanne894d0e2011-07-05 13:45:10 +0200119 struct wiimote_data *wdata = hid_get_drvdata(hdev);
120
David Herrmann02fb72a2011-07-05 13:45:09 +0200121 hid_info(hdev, "Device removed\n");
122 hid_hw_stop(hdev);
David Herrmann672bc4e2011-07-05 13:45:11 +0200123 input_unregister_device(wdata->input);
David Herrmanne894d0e2011-07-05 13:45:10 +0200124 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +0200125}
126
127static const struct hid_device_id wiimote_hid_devices[] = {
128 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
129 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
130 { }
131};
132MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
133
134static struct hid_driver wiimote_hid_driver = {
135 .name = "wiimote",
136 .id_table = wiimote_hid_devices,
137 .probe = wiimote_hid_probe,
138 .remove = wiimote_hid_remove,
139 .raw_event = wiimote_hid_event,
140};
141
David Herrmannfb51b442011-07-05 13:45:08 +0200142static int __init wiimote_init(void)
143{
David Herrmann02fb72a2011-07-05 13:45:09 +0200144 int ret;
145
146 ret = hid_register_driver(&wiimote_hid_driver);
147 if (ret)
148 pr_err("Can't register wiimote hid driver\n");
149
150 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +0200151}
152
153static void __exit wiimote_exit(void)
154{
David Herrmann02fb72a2011-07-05 13:45:09 +0200155 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200156}
157
158module_init(wiimote_init);
159module_exit(wiimote_exit);
160MODULE_LICENSE("GPL");
161MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
162MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
163MODULE_VERSION(WIIMOTE_VERSION);