David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 1 | /* |
| 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 Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 13 | #include <linux/device.h> |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 14 | #include <linux/hid.h> |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 15 | #include <linux/input.h> |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 16 | #include <linux/module.h> |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 17 | #include "hid-ids.h" |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 18 | |
| 19 | #define WIIMOTE_VERSION "0.1" |
| 20 | #define WIIMOTE_NAME "Nintendo Wii Remote" |
| 21 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 22 | struct wiimote_data { |
| 23 | struct hid_device *hdev; |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 24 | struct input_dev *input; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 27 | static int wiimote_input_event(struct input_dev *dev, unsigned int type, |
| 28 | unsigned int code, int value) |
| 29 | { |
| 30 | return 0; |
| 31 | } |
| 32 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 33 | static 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 Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 42 | static 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 Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 50 | wdata->input = input_allocate_device(); |
| 51 | if (!wdata->input) { |
| 52 | kfree(wdata); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 56 | wdata->hdev = hdev; |
| 57 | hid_set_drvdata(hdev, wdata); |
| 58 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 59 | 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 Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 68 | return wdata; |
| 69 | } |
| 70 | |
| 71 | static void wiimote_destroy(struct wiimote_data *wdata) |
| 72 | { |
| 73 | kfree(wdata); |
| 74 | } |
| 75 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 76 | static int wiimote_hid_probe(struct hid_device *hdev, |
| 77 | const struct hid_device_id *id) |
| 78 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 79 | struct wiimote_data *wdata; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 80 | int ret; |
| 81 | |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 82 | wdata = wiimote_create(hdev); |
| 83 | if (!wdata) { |
| 84 | hid_err(hdev, "Can't alloc device\n"); |
| 85 | return -ENOMEM; |
| 86 | } |
| 87 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 88 | ret = hid_parse(hdev); |
| 89 | if (ret) { |
| 90 | hid_err(hdev, "HID parse failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 91 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 95 | if (ret) { |
| 96 | hid_err(hdev, "HW start failed\n"); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 97 | goto err; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 98 | } |
| 99 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 100 | 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 Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 106 | hid_info(hdev, "New device registered\n"); |
| 107 | return 0; |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 108 | |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 109 | err_stop: |
| 110 | hid_hw_stop(hdev); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 111 | err: |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 112 | input_free_device(wdata->input); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 113 | wiimote_destroy(wdata); |
| 114 | return ret; |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void wiimote_hid_remove(struct hid_device *hdev) |
| 118 | { |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 119 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
| 120 | |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 121 | hid_info(hdev, "Device removed\n"); |
| 122 | hid_hw_stop(hdev); |
David Herrmann | 672bc4e | 2011-07-05 13:45:11 +0200 | [diff] [blame^] | 123 | input_unregister_device(wdata->input); |
David Herrmann | e894d0e | 2011-07-05 13:45:10 +0200 | [diff] [blame] | 124 | wiimote_destroy(wdata); |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static const struct hid_device_id wiimote_hid_devices[] = { |
| 128 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, |
| 129 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, |
| 130 | { } |
| 131 | }; |
| 132 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); |
| 133 | |
| 134 | static 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 Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 142 | static int __init wiimote_init(void) |
| 143 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 144 | 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 Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void __exit wiimote_exit(void) |
| 154 | { |
David Herrmann | 02fb72a | 2011-07-05 13:45:09 +0200 | [diff] [blame] | 155 | hid_unregister_driver(&wiimote_hid_driver); |
David Herrmann | fb51b44 | 2011-07-05 13:45:08 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | module_init(wiimote_init); |
| 159 | module_exit(wiimote_exit); |
| 160 | MODULE_LICENSE("GPL"); |
| 161 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
| 162 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); |
| 163 | MODULE_VERSION(WIIMOTE_VERSION); |