HID: wiimote: Add wiimote send function
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hid / hid-wiimote.c
blob811ed8921013aad9f45421edb2f370ce2a374553
1 /*
2 * HID driver for Nintendo Wiimote devices
3 * Copyright (c) 2011 David Herrmann
4 */
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.
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>
18 #include "hid-ids.h"
20 #define WIIMOTE_VERSION "0.1"
21 #define WIIMOTE_NAME "Nintendo Wii Remote"
23 struct wiimote_data {
24 atomic_t ready;
25 struct hid_device *hdev;
26 struct input_dev *input;
29 static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
30 size_t count)
32 __u8 *buf;
33 ssize_t ret;
35 if (!hdev->hid_output_raw_report)
36 return -ENODEV;
38 buf = kmemdup(buffer, count, GFP_KERNEL);
39 if (!buf)
40 return -ENOMEM;
42 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
44 kfree(buf);
45 return ret;
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))
54 return -EBUSY;
55 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
56 smp_rmb();
58 return 0;
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))
67 return -EBUSY;
68 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
69 smp_rmb();
71 if (size < 1)
72 return -EINVAL;
74 return 0;
77 static struct wiimote_data *wiimote_create(struct hid_device *hdev)
79 struct wiimote_data *wdata;
81 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
82 if (!wdata)
83 return NULL;
85 wdata->input = input_allocate_device();
86 if (!wdata->input) {
87 kfree(wdata);
88 return NULL;
91 wdata->hdev = hdev;
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;
103 return wdata;
106 static void wiimote_destroy(struct wiimote_data *wdata)
108 kfree(wdata);
111 static int wiimote_hid_probe(struct hid_device *hdev,
112 const struct hid_device_id *id)
114 struct wiimote_data *wdata;
115 int ret;
117 wdata = wiimote_create(hdev);
118 if (!wdata) {
119 hid_err(hdev, "Can't alloc device\n");
120 return -ENOMEM;
123 ret = hid_parse(hdev);
124 if (ret) {
125 hid_err(hdev, "HID parse failed\n");
126 goto err;
129 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
130 if (ret) {
131 hid_err(hdev, "HW start failed\n");
132 goto err;
135 ret = input_register_device(wdata->input);
136 if (ret) {
137 hid_err(hdev, "Cannot register input device\n");
138 goto err_stop;
141 /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
142 smp_wmb();
143 atomic_set(&wdata->ready, 1);
144 hid_info(hdev, "New device registered\n");
145 return 0;
147 err_stop:
148 hid_hw_stop(hdev);
149 err:
150 input_free_device(wdata->input);
151 wiimote_destroy(wdata);
152 return ret;
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");
160 hid_hw_stop(hdev);
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 = {
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,
180 static int __init wiimote_init(void)
182 int ret;
184 ret = hid_register_driver(&wiimote_hid_driver);
185 if (ret)
186 pr_err("Can't register wiimote hid driver\n");
188 return ret;
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);