xref: /openbmc/linux/drivers/hid/hid-sony.c (revision f04d5140)
1 /*
2  *  HID driver for some sony "special" devices
3  *
4  *  Copyright (c) 1999 Andreas Gal
5  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7  *  Copyright (c) 2008 Jiri Slaby
8  *  Copyright (c) 2006-2008 Jiri Kosina
9  *  Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
10  */
11 
12 /*
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 2 of the License, or (at your option)
16  * any later version.
17  */
18 
19 #include <linux/device.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
24 
25 #include "hid-ids.h"
26 
27 #define VAIO_RDESC_CONSTANT     (1 << 0)
28 #define SIXAXIS_CONTROLLER_USB  (1 << 1)
29 #define SIXAXIS_CONTROLLER_BT   (1 << 2)
30 #define BUZZ_CONTROLLER         (1 << 3)
31 
32 static const u8 sixaxis_rdesc_fixup[] = {
33 	0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
34 	0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
35 	0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
36 };
37 
38 static const u8 sixaxis_rdesc_fixup2[] = {
39 	0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
40 	0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
41 	0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
42 	0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
43 	0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
44 	0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
45 	0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
46 	0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
47 	0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
48 	0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
49 	0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
50 	0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
51 	0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
52 	0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
53 	0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
54 	0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
55 	0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
56 	0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
57 	0xb1, 0x02, 0xc0, 0xc0,
58 };
59 
60 static const unsigned int buzz_keymap[] = {
61 	/* The controller has 4 remote buzzers, each with one LED and 5
62 	 * buttons.
63 	 *
64 	 * We use the mapping chosen by the controller, which is:
65 	 *
66 	 * Key          Offset
67 	 * -------------------
68 	 * Buzz              1
69 	 * Blue              5
70 	 * Orange            4
71 	 * Green             3
72 	 * Yellow            2
73 	 *
74 	 * So, for example, the orange button on the third buzzer is mapped to
75 	 * BTN_TRIGGER_HAPPY14
76 	 */
77 	[ 1] = BTN_TRIGGER_HAPPY1,
78 	[ 2] = BTN_TRIGGER_HAPPY2,
79 	[ 3] = BTN_TRIGGER_HAPPY3,
80 	[ 4] = BTN_TRIGGER_HAPPY4,
81 	[ 5] = BTN_TRIGGER_HAPPY5,
82 	[ 6] = BTN_TRIGGER_HAPPY6,
83 	[ 7] = BTN_TRIGGER_HAPPY7,
84 	[ 8] = BTN_TRIGGER_HAPPY8,
85 	[ 9] = BTN_TRIGGER_HAPPY9,
86 	[10] = BTN_TRIGGER_HAPPY10,
87 	[11] = BTN_TRIGGER_HAPPY11,
88 	[12] = BTN_TRIGGER_HAPPY12,
89 	[13] = BTN_TRIGGER_HAPPY13,
90 	[14] = BTN_TRIGGER_HAPPY14,
91 	[15] = BTN_TRIGGER_HAPPY15,
92 	[16] = BTN_TRIGGER_HAPPY16,
93 	[17] = BTN_TRIGGER_HAPPY17,
94 	[18] = BTN_TRIGGER_HAPPY18,
95 	[19] = BTN_TRIGGER_HAPPY19,
96 	[20] = BTN_TRIGGER_HAPPY20,
97 };
98 
99 struct sony_sc {
100 	unsigned long quirks;
101 
102 	void *extra;
103 };
104 
105 struct buzz_extra {
106 #ifdef CONFIG_LEDS_CLASS
107 	int led_state;
108 	struct led_classdev *leds[4];
109 #endif
110 };
111 
112 /* Sony Vaio VGX has wrongly mouse pointer declared as constant */
113 static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
114 		unsigned int *rsize)
115 {
116 	struct sony_sc *sc = hid_get_drvdata(hdev);
117 
118 	/*
119 	 * Some Sony RF receivers wrongly declare the mouse pointer as a
120 	 * a constant non-data variable.
121 	 */
122 	if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
123 	    /* usage page: generic desktop controls */
124 	    /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
125 	    /* usage: mouse */
126 	    rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
127 	    /* input (usage page for x,y axes): constant, variable, relative */
128 	    rdesc[54] == 0x81 && rdesc[55] == 0x07) {
129 		hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
130 		/* input: data, variable, relative */
131 		rdesc[55] = 0x06;
132 	}
133 
134 	/* The HID descriptor exposed over BT has a trailing zero byte */
135 	if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
136 			((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
137 			rdesc[83] == 0x75) {
138 		hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
139 		memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
140 			sizeof(sixaxis_rdesc_fixup));
141 	} else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
142 		   *rsize > sizeof(sixaxis_rdesc_fixup2)) {
143 		hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
144 			 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
145 		*rsize = sizeof(sixaxis_rdesc_fixup2);
146 		memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
147 	}
148 	return rdesc;
149 }
150 
151 static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
152 		__u8 *rd, int size)
153 {
154 	struct sony_sc *sc = hid_get_drvdata(hdev);
155 
156 	/* Sixaxis HID report has acclerometers/gyro with MSByte first, this
157 	 * has to be BYTE_SWAPPED before passing up to joystick interface
158 	 */
159 	if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
160 			rd[0] == 0x01 && size == 49) {
161 		swap(rd[41], rd[42]);
162 		swap(rd[43], rd[44]);
163 		swap(rd[45], rd[46]);
164 		swap(rd[47], rd[48]);
165 	}
166 
167 	return 0;
168 }
169 
170 static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
171 			struct hid_field *field, struct hid_usage *usage,
172 			unsigned long **bit, int *max)
173 {
174 	struct sony_sc *sc = hid_get_drvdata(hdev);
175 
176 	if (sc->quirks & BUZZ_CONTROLLER) {
177 		unsigned int key = usage->hid & HID_USAGE;
178 
179 		if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
180 			return -1;
181 
182 		switch (usage->collection_index) {
183 		case 1:
184 			if (key >= ARRAY_SIZE(buzz_keymap))
185 				return -1;
186 
187 			key = buzz_keymap[key];
188 			if (!key)
189 				return -1;
190 			break;
191 		default:
192 			return -1;
193 		}
194 
195 		hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
196 		return 1;
197 	}
198 
199 	return -1;
200 }
201 
202 /*
203  * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
204  * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
205  * so we need to override that forcing HID Output Reports on the Control EP.
206  *
207  * There is also another issue about HID Output Reports via USB, the Sixaxis
208  * does not want the report_id as part of the data packet, so we have to
209  * discard buf[0] when sending the actual control message, even for numbered
210  * reports, humpf!
211  */
212 static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
213 		size_t count, unsigned char report_type)
214 {
215 	struct usb_interface *intf = to_usb_interface(hid->dev.parent);
216 	struct usb_device *dev = interface_to_usbdev(intf);
217 	struct usb_host_interface *interface = intf->cur_altsetting;
218 	int report_id = buf[0];
219 	int ret;
220 
221 	if (report_type == HID_OUTPUT_REPORT) {
222 		/* Don't send the Report ID */
223 		buf++;
224 		count--;
225 	}
226 
227 	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
228 		HID_REQ_SET_REPORT,
229 		USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
230 		((report_type + 1) << 8) | report_id,
231 		interface->desc.bInterfaceNumber, buf, count,
232 		USB_CTRL_SET_TIMEOUT);
233 
234 	/* Count also the Report ID, in case of an Output report. */
235 	if (ret > 0 && report_type == HID_OUTPUT_REPORT)
236 		ret++;
237 
238 	return ret;
239 }
240 
241 /*
242  * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
243  * to "operational".  Without this, the ps3 controller will not report any
244  * events.
245  */
246 static int sixaxis_set_operational_usb(struct hid_device *hdev)
247 {
248 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
249 	struct usb_device *dev = interface_to_usbdev(intf);
250 	__u16 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
251 	int ret;
252 	char *buf = kmalloc(18, GFP_KERNEL);
253 
254 	if (!buf)
255 		return -ENOMEM;
256 
257 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
258 				 HID_REQ_GET_REPORT,
259 				 USB_DIR_IN | USB_TYPE_CLASS |
260 				 USB_RECIP_INTERFACE,
261 				 (3 << 8) | 0xf2, ifnum, buf, 17,
262 				 USB_CTRL_GET_TIMEOUT);
263 	if (ret < 0)
264 		hid_err(hdev, "can't set operational mode\n");
265 
266 	kfree(buf);
267 
268 	return ret;
269 }
270 
271 static int sixaxis_set_operational_bt(struct hid_device *hdev)
272 {
273 	unsigned char buf[] = { 0xf4,  0x42, 0x03, 0x00, 0x00 };
274 	return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
275 }
276 
277 #ifdef CONFIG_LEDS_CLASS
278 static void buzz_set_leds(struct hid_device *hdev, int leds)
279 {
280 	struct list_head *report_list =
281 		&hdev->report_enum[HID_OUTPUT_REPORT].report_list;
282 	struct hid_report *report = list_entry(report_list->next,
283 		struct hid_report, list);
284 	__s32 *value = report->field[0]->value;
285 
286 	value[0] = 0x00;
287 	value[1] = (leds & 1) ? 0xff : 0x00;
288 	value[2] = (leds & 2) ? 0xff : 0x00;
289 	value[3] = (leds & 4) ? 0xff : 0x00;
290 	value[4] = (leds & 8) ? 0xff : 0x00;
291 	value[5] = 0x00;
292 	value[6] = 0x00;
293 	hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
294 }
295 
296 static void buzz_led_set_brightness(struct led_classdev *led,
297 				    enum led_brightness value)
298 {
299 	struct device *dev = led->dev->parent;
300 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
301 	struct sony_sc *drv_data;
302 	struct buzz_extra *buzz;
303 
304 	int n;
305 
306 	drv_data = hid_get_drvdata(hdev);
307 	if (!drv_data || !drv_data->extra) {
308 		hid_err(hdev, "No device data\n");
309 		return;
310 	}
311 	buzz = drv_data->extra;
312 
313 	for (n = 0; n < 4; n++) {
314 		if (led == buzz->leds[n]) {
315 			int on = !! (buzz->led_state & (1 << n));
316 			if (value == LED_OFF && on) {
317 				buzz->led_state &= ~(1 << n);
318 				buzz_set_leds(hdev, buzz->led_state);
319 			} else if (value != LED_OFF && !on) {
320 				buzz->led_state |= (1 << n);
321 				buzz_set_leds(hdev, buzz->led_state);
322 			}
323 			break;
324 		}
325 	}
326 }
327 
328 static enum led_brightness buzz_led_get_brightness(struct led_classdev *led)
329 {
330 	struct device *dev = led->dev->parent;
331 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
332 	struct sony_sc *drv_data;
333 	struct buzz_extra *buzz;
334 
335 	int n;
336 	int on = 0;
337 
338 	drv_data = hid_get_drvdata(hdev);
339 	if (!drv_data || !drv_data->extra) {
340 		hid_err(hdev, "No device data\n");
341 		return LED_OFF;
342 	}
343 	buzz = drv_data->extra;
344 
345 	for (n = 0; n < 4; n++) {
346 		if (led == buzz->leds[n]) {
347 			on = !! (buzz->led_state & (1 << n));
348 			break;
349 		}
350 	}
351 
352 	return on ? LED_FULL : LED_OFF;
353 }
354 #endif
355 
356 static int buzz_init(struct hid_device *hdev)
357 {
358 	struct sony_sc *drv_data;
359 	struct buzz_extra *buzz;
360 	int ret = 0;
361 
362 	drv_data = hid_get_drvdata(hdev);
363 	BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
364 
365 	buzz = kzalloc(sizeof(*buzz), GFP_KERNEL);
366 	if (!buzz) {
367 		hid_err(hdev, "Insufficient memory, cannot allocate driver data\n");
368 		return -ENOMEM;
369 	}
370 	drv_data->extra = buzz;
371 
372 	/* Clear LEDs as we have no way of reading their initial state. This is
373 	 * only relevant if the driver is loaded after somebody actively set the
374 	 * LEDs to on */
375 	buzz_set_leds(hdev, 0x00);
376 
377 #ifdef CONFIG_LEDS_CLASS
378 	{
379 		int n;
380 		struct led_classdev *led;
381 		size_t name_sz;
382 		char *name;
383 
384 		name_sz = strlen(dev_name(&hdev->dev)) + strlen("::buzz#") + 1;
385 
386 		for (n = 0; n < 4; n++) {
387 			led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
388 			if (!led) {
389 				hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
390 				goto error_leds;
391 			}
392 
393 			name = (void *)(&led[1]);
394 			snprintf(name, name_sz, "%s::buzz%d", dev_name(&hdev->dev), n + 1);
395 			led->name = name;
396 			led->brightness = 0;
397 			led->max_brightness = 1;
398 			led->brightness_get = buzz_led_get_brightness;
399 			led->brightness_set = buzz_led_set_brightness;
400 
401 			if (led_classdev_register(&hdev->dev, led)) {
402 				hid_err(hdev, "Failed to register LED %d\n", n);
403 				kfree(led);
404 				goto error_leds;
405 			}
406 
407 			buzz->leds[n] = led;
408 		}
409 	}
410 #endif
411 
412 	return ret;
413 
414 #ifdef CONFIG_LEDS_CLASS
415 error_leds:
416 	{
417 		int n;
418 		struct led_classdev *led;
419 
420 		for (n = 0; n < 4; n++) {
421 			led = buzz->leds[n];
422 			buzz->leds[n] = NULL;
423 			if (!led)
424 				continue;
425 			led_classdev_unregister(led);
426 			kfree(led);
427 		}
428 	}
429 
430 	kfree(drv_data->extra);
431 	drv_data->extra = NULL;
432 	return ret;
433 #endif
434 }
435 
436 static void buzz_remove(struct hid_device *hdev)
437 {
438 	struct sony_sc *drv_data;
439 	struct buzz_extra *buzz;
440 
441 	drv_data = hid_get_drvdata(hdev);
442 	BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
443 
444 	buzz = drv_data->extra;
445 
446 #ifdef CONFIG_LEDS_CLASS
447 	{
448 		int n;
449 		struct led_classdev *led;
450 
451 		for (n = 0; n < 4; n++) {
452 			led = buzz->leds[n];
453 			buzz->leds[n] = NULL;
454 			if (!led)
455 				continue;
456 			led_classdev_unregister(led);
457 			kfree(led);
458 		}
459 	}
460 #endif
461 
462 	kfree(drv_data->extra);
463 	drv_data->extra = NULL;
464 }
465 
466 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
467 {
468 	int ret;
469 	unsigned long quirks = id->driver_data;
470 	struct sony_sc *sc;
471 	unsigned int connect_mask = HID_CONNECT_DEFAULT;
472 
473 	sc = kzalloc(sizeof(*sc), GFP_KERNEL);
474 	if (sc == NULL) {
475 		hid_err(hdev, "can't alloc sony descriptor\n");
476 		return -ENOMEM;
477 	}
478 
479 	sc->quirks = quirks;
480 	hid_set_drvdata(hdev, sc);
481 
482 	ret = hid_parse(hdev);
483 	if (ret) {
484 		hid_err(hdev, "parse failed\n");
485 		goto err_free;
486 	}
487 
488 	if (sc->quirks & VAIO_RDESC_CONSTANT)
489 		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
490 	else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
491 		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
492 	else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
493 		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
494 
495 	ret = hid_hw_start(hdev, connect_mask);
496 	if (ret) {
497 		hid_err(hdev, "hw start failed\n");
498 		goto err_free;
499 	}
500 
501 	if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
502 		hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
503 		ret = sixaxis_set_operational_usb(hdev);
504 	}
505 	else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
506 		ret = sixaxis_set_operational_bt(hdev);
507 	else if (sc->quirks & BUZZ_CONTROLLER)
508 		ret = buzz_init(hdev);
509 	else
510 		ret = 0;
511 
512 	if (ret < 0)
513 		goto err_stop;
514 
515 	return 0;
516 err_stop:
517 	hid_hw_stop(hdev);
518 err_free:
519 	kfree(sc);
520 	return ret;
521 }
522 
523 static void sony_remove(struct hid_device *hdev)
524 {
525 	struct sony_sc *sc = hid_get_drvdata(hdev);
526 
527 	if (sc->quirks & BUZZ_CONTROLLER)
528 		buzz_remove(hdev);
529 
530 	hid_hw_stop(hdev);
531 	kfree(sc);
532 }
533 
534 static const struct hid_device_id sony_devices[] = {
535 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
536 		.driver_data = SIXAXIS_CONTROLLER_USB },
537 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
538 		.driver_data = SIXAXIS_CONTROLLER_USB },
539 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
540 		.driver_data = SIXAXIS_CONTROLLER_BT },
541 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
542 		.driver_data = VAIO_RDESC_CONSTANT },
543 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
544 		.driver_data = VAIO_RDESC_CONSTANT },
545 	/* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
546 	 * Logitech joystick from the device descriptor. */
547 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
548 		.driver_data = BUZZ_CONTROLLER },
549 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
550 		.driver_data = BUZZ_CONTROLLER },
551 	{ }
552 };
553 MODULE_DEVICE_TABLE(hid, sony_devices);
554 
555 static struct hid_driver sony_driver = {
556 	.name          = "sony",
557 	.id_table      = sony_devices,
558 	.input_mapping = sony_mapping,
559 	.probe         = sony_probe,
560 	.remove        = sony_remove,
561 	.report_fixup  = sony_report_fixup,
562 	.raw_event     = sony_raw_event
563 };
564 module_hid_driver(sony_driver);
565 
566 MODULE_LICENSE("GPL");
567