1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/device.h>
19#include <linux/hid.h>
20#include <linux/module.h>
21#include <linux/slab.h>
22#include <linux/usb.h>
23
24#include "hid-ids.h"
25
26#define VAIO_RDESC_CONSTANT (1 << 0)
27#define SIXAXIS_CONTROLLER_USB (1 << 1)
28#define SIXAXIS_CONTROLLER_BT (1 << 2)
29
30static const u8 sixaxis_rdesc_fixup[] = {
31 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
32 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
33 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
34};
35
36static const u8 sixaxis_rdesc_fixup2[] = {
37 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
38 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
39 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
40 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
41 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
42 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
43 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
44 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
45 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
46 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
47 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
48 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
49 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
50 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
51 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
52 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
53 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
54 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
55 0xb1, 0x02, 0xc0, 0xc0,
56};
57
58struct sony_sc {
59 unsigned long quirks;
60};
61
62
63static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
64 unsigned int *rsize)
65{
66 struct sony_sc *sc = hid_get_drvdata(hdev);
67
68
69
70
71
72 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
73
74
75
76 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
77
78 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
79 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
80
81 rdesc[55] = 0x06;
82 }
83
84
85 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
86 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
87 rdesc[83] == 0x75) {
88 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
89 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
90 sizeof(sixaxis_rdesc_fixup));
91 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
92 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
93 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
94 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
95 *rsize = sizeof(sixaxis_rdesc_fixup2);
96 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
97 }
98 return rdesc;
99}
100
101static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
102 __u8 *rd, int size)
103{
104 struct sony_sc *sc = hid_get_drvdata(hdev);
105
106
107
108
109 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
110 rd[0] == 0x01 && size == 49) {
111 swap(rd[41], rd[42]);
112 swap(rd[43], rd[44]);
113 swap(rd[45], rd[46]);
114 swap(rd[47], rd[48]);
115 }
116
117 return 0;
118}
119
120
121
122
123
124
125static int sixaxis_set_operational_usb(struct hid_device *hdev)
126{
127 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
128 struct usb_device *dev = interface_to_usbdev(intf);
129 __u16 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
130 int ret;
131 char *buf = kmalloc(18, GFP_KERNEL);
132
133 if (!buf)
134 return -ENOMEM;
135
136 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
137 HID_REQ_GET_REPORT,
138 USB_DIR_IN | USB_TYPE_CLASS |
139 USB_RECIP_INTERFACE,
140 (3 << 8) | 0xf2, ifnum, buf, 17,
141 USB_CTRL_GET_TIMEOUT);
142 if (ret < 0)
143 hid_err(hdev, "can't set operational mode\n");
144
145 kfree(buf);
146
147 return ret;
148}
149
150static int sixaxis_set_operational_bt(struct hid_device *hdev)
151{
152 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
153 return hid_hw_raw_request(hdev, buf[0], buf, sizeof(buf),
154 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
155}
156
157static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
158{
159 int ret;
160 unsigned long quirks = id->driver_data;
161 struct sony_sc *sc;
162
163 sc = kzalloc(sizeof(*sc), GFP_KERNEL);
164 if (sc == NULL) {
165 hid_err(hdev, "can't alloc sony descriptor\n");
166 return -ENOMEM;
167 }
168
169 sc->quirks = quirks;
170 hid_set_drvdata(hdev, sc);
171
172 ret = hid_parse(hdev);
173 if (ret) {
174 hid_err(hdev, "parse failed\n");
175 goto err_free;
176 }
177
178 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
179 HID_CONNECT_HIDDEV_FORCE);
180 if (ret) {
181 hid_err(hdev, "hw start failed\n");
182 goto err_free;
183 }
184
185 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
186
187
188
189
190
191
192
193
194
195
196 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
197 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
198 ret = sixaxis_set_operational_usb(hdev);
199 }
200 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
201 ret = sixaxis_set_operational_bt(hdev);
202 else
203 ret = 0;
204
205 if (ret < 0)
206 goto err_stop;
207
208 return 0;
209err_stop:
210 hid_hw_stop(hdev);
211err_free:
212 kfree(sc);
213 return ret;
214}
215
216static void sony_remove(struct hid_device *hdev)
217{
218 hid_hw_stop(hdev);
219 kfree(hid_get_drvdata(hdev));
220}
221
222static const struct hid_device_id sony_devices[] = {
223 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
224 .driver_data = SIXAXIS_CONTROLLER_USB },
225 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
226 .driver_data = SIXAXIS_CONTROLLER_USB },
227 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
228 .driver_data = SIXAXIS_CONTROLLER_BT },
229 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
230 .driver_data = VAIO_RDESC_CONSTANT },
231 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
232 .driver_data = VAIO_RDESC_CONSTANT },
233 { }
234};
235MODULE_DEVICE_TABLE(hid, sony_devices);
236
237static struct hid_driver sony_driver = {
238 .name = "sony",
239 .id_table = sony_devices,
240 .probe = sony_probe,
241 .remove = sony_remove,
242 .report_fixup = sony_report_fixup,
243 .raw_event = sony_raw_event
244};
245module_hid_driver(sony_driver);
246
247MODULE_LICENSE("GPL");
248