blob: 42257acfeb73fba84c2912db29f337a63ab0c706 [file] [log] [blame]
Jussi Kivilinnafac733f2009-05-13 11:54:38 +03001/*
2 * Force feedback support for SmartJoy PLUS PS2->USB adapter
3 *
4 * Copyright (c) 2009 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
5 *
6 * Based of hid-pl.c and hid-gaff.c
7 * Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
8 * Copyright (c) 2008 Lukasz Lubojanski <lukasz@lubojanski.info>
9 */
10
11/*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27/* #define DEBUG */
28
29#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jussi Kivilinnafac733f2009-05-13 11:54:38 +030031#include <linux/usb.h>
32#include <linux/hid.h>
Paul Gortmaker8f86a2c2011-07-03 13:39:48 -040033#include <linux/module.h>
Jussi Kivilinnafac733f2009-05-13 11:54:38 +030034#include "hid-ids.h"
35
36#ifdef CONFIG_SMARTJOYPLUS_FF
37#include "usbhid/usbhid.h"
38
39struct sjoyff_device {
40 struct hid_report *report;
41};
42
43static int hid_sjoyff_play(struct input_dev *dev, void *data,
44 struct ff_effect *effect)
45{
46 struct hid_device *hid = input_get_drvdata(dev);
47 struct sjoyff_device *sjoyff = data;
48 u32 left, right;
49
50 left = effect->u.rumble.strong_magnitude;
51 right = effect->u.rumble.weak_magnitude;
52 dev_dbg(&dev->dev, "called with 0x%08x 0x%08x\n", left, right);
53
54 left = left * 0xff / 0xffff;
55 right = (right != 0); /* on/off only */
56
57 sjoyff->report->field[0]->value[1] = right;
58 sjoyff->report->field[0]->value[2] = left;
59 dev_dbg(&dev->dev, "running with 0x%02x 0x%02x\n", left, right);
60 usbhid_submit_report(hid, sjoyff->report, USB_DIR_OUT);
61
62 return 0;
63}
64
65static int sjoyff_init(struct hid_device *hid)
66{
67 struct sjoyff_device *sjoyff;
68 struct hid_report *report;
Sean Youngad395cc2011-08-14 20:42:05 +010069 struct hid_input *hidinput;
Jussi Kivilinnafac733f2009-05-13 11:54:38 +030070 struct list_head *report_list =
71 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
72 struct list_head *report_ptr = report_list;
73 struct input_dev *dev;
74 int error;
75
76 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -080077 hid_err(hid, "no output reports found\n");
Jussi Kivilinnafac733f2009-05-13 11:54:38 +030078 return -ENODEV;
79 }
80
Sean Youngad395cc2011-08-14 20:42:05 +010081 list_for_each_entry(hidinput, &hid->inputs, list) {
82 report_ptr = report_ptr->next;
Jussi Kivilinnafac733f2009-05-13 11:54:38 +030083
Sean Youngad395cc2011-08-14 20:42:05 +010084 if (report_ptr == report_list) {
85 hid_err(hid, "required output report is missing\n");
86 return -ENODEV;
87 }
88
89 report = list_entry(report_ptr, struct hid_report, list);
90 if (report->maxfield < 1) {
91 hid_err(hid, "no fields in the report\n");
92 return -ENODEV;
93 }
94
95 if (report->field[0]->report_count < 3) {
96 hid_err(hid, "not enough values in the field\n");
97 return -ENODEV;
98 }
99
100 sjoyff = kzalloc(sizeof(struct sjoyff_device), GFP_KERNEL);
101 if (!sjoyff)
102 return -ENOMEM;
103
104 dev = hidinput->input;
105
106 set_bit(FF_RUMBLE, dev->ffbit);
107
108 error = input_ff_create_memless(dev, sjoyff, hid_sjoyff_play);
109 if (error) {
110 kfree(sjoyff);
111 return error;
112 }
113
114 sjoyff->report = report;
115 sjoyff->report->field[0]->value[0] = 0x01;
116 sjoyff->report->field[0]->value[1] = 0x00;
117 sjoyff->report->field[0]->value[2] = 0x00;
118 usbhid_submit_report(hid, sjoyff->report, USB_DIR_OUT);
Jussi Kivilinnafac733f2009-05-13 11:54:38 +0300119 }
120
Joe Perches4291ee32010-12-09 19:29:03 -0800121 hid_info(hid, "Force feedback for SmartJoy PLUS PS2/USB adapter\n");
Jussi Kivilinnafac733f2009-05-13 11:54:38 +0300122
123 return 0;
124}
125#else
126static inline int sjoyff_init(struct hid_device *hid)
127{
128 return 0;
129}
130#endif
131
132static int sjoy_probe(struct hid_device *hdev, const struct hid_device_id *id)
133{
134 int ret;
135
Sean Youngad395cc2011-08-14 20:42:05 +0100136 hdev->quirks |= id->driver_data;
137
Jussi Kivilinnafac733f2009-05-13 11:54:38 +0300138 ret = hid_parse(hdev);
139 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800140 hid_err(hdev, "parse failed\n");
Jussi Kivilinnafac733f2009-05-13 11:54:38 +0300141 goto err;
142 }
143
144 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
145 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800146 hid_err(hdev, "hw start failed\n");
Jussi Kivilinnafac733f2009-05-13 11:54:38 +0300147 goto err;
148 }
149
150 sjoyff_init(hdev);
151
152 return 0;
153err:
154 return ret;
155}
156
157static const struct hid_device_id sjoy_devices[] = {
Sean Young42fc04e2012-02-18 12:53:44 +0000158 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_3_PRO),
159 .driver_data = HID_QUIRK_NOGET },
Sean Young1bcc2062011-10-20 21:26:21 +0100160 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_DUAL_BOX_PRO),
161 .driver_data = HID_QUIRK_MULTI_INPUT | HID_QUIRK_NOGET |
162 HID_QUIRK_SKIP_OUTPUT_REPORTS },
163 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_5_PRO),
164 .driver_data = HID_QUIRK_MULTI_INPUT | HID_QUIRK_NOGET |
165 HID_QUIRK_SKIP_OUTPUT_REPORTS },
Jussi Kivilinnafac733f2009-05-13 11:54:38 +0300166 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
Sean Young42fc04e2012-02-18 12:53:44 +0000167 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SUPER_JOY_BOX_3) },
Sean Youngad395cc2011-08-14 20:42:05 +0100168 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD),
Sean Young42fc04e2012-02-18 12:53:44 +0000169 .driver_data = HID_QUIRK_MULTI_INPUT |
Sean Youngad395cc2011-08-14 20:42:05 +0100170 HID_QUIRK_SKIP_OUTPUT_REPORTS },
Jussi Kivilinnafac733f2009-05-13 11:54:38 +0300171 { }
172};
173MODULE_DEVICE_TABLE(hid, sjoy_devices);
174
175static struct hid_driver sjoy_driver = {
176 .name = "smartjoyplus",
177 .id_table = sjoy_devices,
178 .probe = sjoy_probe,
179};
180
Peter Huewea24f423b2009-07-02 19:08:38 +0200181static int __init sjoy_init(void)
Jussi Kivilinnafac733f2009-05-13 11:54:38 +0300182{
183 return hid_register_driver(&sjoy_driver);
184}
185
Peter Huewea24f423b2009-07-02 19:08:38 +0200186static void __exit sjoy_exit(void)
Jussi Kivilinnafac733f2009-05-13 11:54:38 +0300187{
188 hid_unregister_driver(&sjoy_driver);
189}
190
191module_init(sjoy_init);
192module_exit(sjoy_exit);
193MODULE_LICENSE("GPL");
194MODULE_AUTHOR("Jussi Kivilinna");
195