1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/device.h>
28#include <linux/hid.h>
29#include <linux/module.h>
30#include <linux/slab.h>
31#include <linux/usb.h>
32#include <linux/leds.h>
33
34#include "hid-ids.h"
35
36#define VAIO_RDESC_CONSTANT BIT(0)
37#define SIXAXIS_CONTROLLER_USB BIT(1)
38#define SIXAXIS_CONTROLLER_BT BIT(2)
39#define BUZZ_CONTROLLER BIT(3)
40#define PS3REMOTE BIT(4)
41#define DUALSHOCK4_CONTROLLER_USB BIT(5)
42#define DUALSHOCK4_CONTROLLER_BT BIT(6)
43
44#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB)
45#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER_USB | DUALSHOCK4_CONTROLLER_USB)
46
47#define MAX_LEDS 4
48
49static const u8 sixaxis_rdesc_fixup[] = {
50 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
51 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
52 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
53};
54
55static const u8 sixaxis_rdesc_fixup2[] = {
56 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
57 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
58 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
59 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
60 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
61 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
62 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
63 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
64 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
65 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
66 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
67 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
68 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
69 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
70 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
71 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
72 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
73 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
74 0xb1, 0x02, 0xc0, 0xc0,
75};
76
77
78
79
80
81
82static u8 dualshock4_usb_rdesc[] = {
83 0x05, 0x01,
84 0x09, 0x05,
85 0xA1, 0x01,
86 0x85, 0x01,
87 0x09, 0x30,
88 0x09, 0x31,
89 0x09, 0x32,
90 0x09, 0x35,
91 0x15, 0x00,
92 0x26, 0xFF, 0x00,
93 0x75, 0x08,
94 0x95, 0x04,
95 0x81, 0x02,
96 0x09, 0x39,
97 0x15, 0x00,
98 0x25, 0x07,
99 0x35, 0x00,
100 0x46, 0x3B, 0x01,
101 0x65, 0x14,
102 0x75, 0x04,
103 0x95, 0x01,
104 0x81, 0x42,
105 0x65, 0x00,
106 0x05, 0x09,
107 0x19, 0x01,
108 0x29, 0x0E,
109 0x15, 0x00,
110 0x25, 0x01,
111 0x75, 0x01,
112 0x95, 0x0E,
113 0x81, 0x02,
114 0x06, 0x00, 0xFF,
115 0x09, 0x20,
116 0x75, 0x06,
117 0x95, 0x01,
118 0x15, 0x00,
119 0x25, 0x7F,
120 0x81, 0x02,
121 0x05, 0x01,
122 0x09, 0x33,
123 0x09, 0x34,
124 0x15, 0x00,
125 0x26, 0xFF, 0x00,
126 0x75, 0x08,
127 0x95, 0x02,
128 0x81, 0x02,
129 0x06, 0x00, 0xFF,
130 0x09, 0x21,
131 0x95, 0x03,
132 0x81, 0x02,
133 0x05, 0x01,
134 0x19, 0x40,
135 0x29, 0x42,
136 0x16, 0x00, 0x80,
137 0x26, 0x00, 0x7F,
138 0x75, 0x10,
139 0x95, 0x03,
140 0x81, 0x02,
141 0x19, 0x43,
142 0x29, 0x45,
143 0x16, 0xFF, 0xBF,
144 0x26, 0x00, 0x40,
145 0x95, 0x03,
146 0x81, 0x02,
147 0x06, 0x00, 0xFF,
148 0x09, 0x21,
149 0x15, 0x00,
150 0x25, 0xFF,
151 0x75, 0x08,
152 0x95, 0x27,
153 0x81, 0x02,
154 0x85, 0x05,
155 0x09, 0x22,
156 0x95, 0x1F,
157 0x91, 0x02,
158 0x85, 0x04,
159 0x09, 0x23,
160 0x95, 0x24,
161 0xB1, 0x02,
162 0x85, 0x02,
163 0x09, 0x24,
164 0x95, 0x24,
165 0xB1, 0x02,
166 0x85, 0x08,
167 0x09, 0x25,
168 0x95, 0x03,
169 0xB1, 0x02,
170 0x85, 0x10,
171 0x09, 0x26,
172 0x95, 0x04,
173 0xB1, 0x02,
174 0x85, 0x11,
175 0x09, 0x27,
176 0x95, 0x02,
177 0xB1, 0x02,
178 0x85, 0x12,
179 0x06, 0x02, 0xFF,
180 0x09, 0x21,
181 0x95, 0x0F,
182 0xB1, 0x02,
183 0x85, 0x13,
184 0x09, 0x22,
185 0x95, 0x16,
186 0xB1, 0x02,
187 0x85, 0x14,
188 0x06, 0x05, 0xFF,
189 0x09, 0x20,
190 0x95, 0x10,
191 0xB1, 0x02,
192 0x85, 0x15,
193 0x09, 0x21,
194 0x95, 0x2C,
195 0xB1, 0x02,
196 0x06, 0x80, 0xFF,
197 0x85, 0x80,
198 0x09, 0x20,
199 0x95, 0x06,
200 0xB1, 0x02,
201 0x85, 0x81,
202 0x09, 0x21,
203 0x95, 0x06,
204 0xB1, 0x02,
205 0x85, 0x82,
206 0x09, 0x22,
207 0x95, 0x05,
208 0xB1, 0x02,
209 0x85, 0x83,
210 0x09, 0x23,
211 0x95, 0x01,
212 0xB1, 0x02,
213 0x85, 0x84,
214 0x09, 0x24,
215 0x95, 0x04,
216 0xB1, 0x02,
217 0x85, 0x85,
218 0x09, 0x25,
219 0x95, 0x06,
220 0xB1, 0x02,
221 0x85, 0x86,
222 0x09, 0x26,
223 0x95, 0x06,
224 0xB1, 0x02,
225 0x85, 0x87,
226 0x09, 0x27,
227 0x95, 0x23,
228 0xB1, 0x02,
229 0x85, 0x88,
230 0x09, 0x28,
231 0x95, 0x22,
232 0xB1, 0x02,
233 0x85, 0x89,
234 0x09, 0x29,
235 0x95, 0x02,
236 0xB1, 0x02,
237 0x85, 0x90,
238 0x09, 0x30,
239 0x95, 0x05,
240 0xB1, 0x02,
241 0x85, 0x91,
242 0x09, 0x31,
243 0x95, 0x03,
244 0xB1, 0x02,
245 0x85, 0x92,
246 0x09, 0x32,
247 0x95, 0x03,
248 0xB1, 0x02,
249 0x85, 0x93,
250 0x09, 0x33,
251 0x95, 0x0C,
252 0xB1, 0x02,
253 0x85, 0xA0,
254 0x09, 0x40,
255 0x95, 0x06,
256 0xB1, 0x02,
257 0x85, 0xA1,
258 0x09, 0x41,
259 0x95, 0x01,
260 0xB1, 0x02,
261 0x85, 0xA2,
262 0x09, 0x42,
263 0x95, 0x01,
264 0xB1, 0x02,
265 0x85, 0xA3,
266 0x09, 0x43,
267 0x95, 0x30,
268 0xB1, 0x02,
269 0x85, 0xA4,
270 0x09, 0x44,
271 0x95, 0x0D,
272 0xB1, 0x02,
273 0x85, 0xA5,
274 0x09, 0x45,
275 0x95, 0x15,
276 0xB1, 0x02,
277 0x85, 0xA6,
278 0x09, 0x46,
279 0x95, 0x15,
280 0xB1, 0x02,
281 0x85, 0xF0,
282 0x09, 0x47,
283 0x95, 0x3F,
284 0xB1, 0x02,
285 0x85, 0xF1,
286 0x09, 0x48,
287 0x95, 0x3F,
288 0xB1, 0x02,
289 0x85, 0xF2,
290 0x09, 0x49,
291 0x95, 0x0F,
292 0xB1, 0x02,
293 0x85, 0xA7,
294 0x09, 0x4A,
295 0x95, 0x01,
296 0xB1, 0x02,
297 0x85, 0xA8,
298 0x09, 0x4B,
299 0x95, 0x01,
300 0xB1, 0x02,
301 0x85, 0xA9,
302 0x09, 0x4C,
303 0x95, 0x08,
304 0xB1, 0x02,
305 0x85, 0xAA,
306 0x09, 0x4E,
307 0x95, 0x01,
308 0xB1, 0x02,
309 0x85, 0xAB,
310 0x09, 0x4F,
311 0x95, 0x39,
312 0xB1, 0x02,
313 0x85, 0xAC,
314 0x09, 0x50,
315 0x95, 0x39,
316 0xB1, 0x02,
317 0x85, 0xAD,
318 0x09, 0x51,
319 0x95, 0x0B,
320 0xB1, 0x02,
321 0x85, 0xAE,
322 0x09, 0x52,
323 0x95, 0x01,
324 0xB1, 0x02,
325 0x85, 0xAF,
326 0x09, 0x53,
327 0x95, 0x02,
328 0xB1, 0x02,
329 0x85, 0xB0,
330 0x09, 0x54,
331 0x95, 0x3F,
332 0xB1, 0x02,
333 0xC0
334};
335
336static __u8 ps3remote_rdesc[] = {
337 0x05, 0x01,
338 0x09, 0x05,
339 0xA1, 0x01,
340
341
342 0xA1, 0x02,
343
344
345
346 0x75, 0x08,
347 0x95, 0x01,
348 0x81, 0x01,
349
350
351
352 0x05, 0x09,
353 0x19, 0x01,
354 0x29, 0x18,
355 0x14,
356 0x25, 0x01,
357 0x75, 0x01,
358 0x95, 0x18,
359 0x81, 0x02,
360
361 0xC0,
362
363
364 0xA1, 0x02,
365
366
367 0x05, 0x09,
368 0x18,
369 0x29, 0xFE,
370 0x14,
371 0x26, 0xFE, 0x00,
372 0x75, 0x08,
373 0x95, 0x01,
374 0x80,
375
376
377
378 0x75, 0x08,
379 0x95, 0x06,
380 0x81, 0x01,
381
382
383 0x05, 0x06,
384 0x09, 0x20,
385 0x14,
386 0x25, 0x05,
387 0x75, 0x08,
388 0x95, 0x01,
389 0x81, 0x02,
390
391 0xC0,
392
393 0xC0
394};
395
396static const unsigned int ps3remote_keymap_joypad_buttons[] = {
397 [0x01] = KEY_SELECT,
398 [0x02] = BTN_THUMBL,
399 [0x03] = BTN_THUMBR,
400 [0x04] = BTN_START,
401 [0x05] = KEY_UP,
402 [0x06] = KEY_RIGHT,
403 [0x07] = KEY_DOWN,
404 [0x08] = KEY_LEFT,
405 [0x09] = BTN_TL2,
406 [0x0a] = BTN_TR2,
407 [0x0b] = BTN_TL,
408 [0x0c] = BTN_TR,
409 [0x0d] = KEY_OPTION,
410 [0x0e] = KEY_BACK,
411 [0x0f] = BTN_0,
412 [0x10] = KEY_SCREEN,
413 [0x11] = KEY_HOMEPAGE,
414 [0x14] = KEY_ENTER,
415};
416static const unsigned int ps3remote_keymap_remote_buttons[] = {
417 [0x00] = KEY_1,
418 [0x01] = KEY_2,
419 [0x02] = KEY_3,
420 [0x03] = KEY_4,
421 [0x04] = KEY_5,
422 [0x05] = KEY_6,
423 [0x06] = KEY_7,
424 [0x07] = KEY_8,
425 [0x08] = KEY_9,
426 [0x09] = KEY_0,
427 [0x0e] = KEY_ESC,
428 [0x0f] = KEY_CLEAR,
429 [0x16] = KEY_EJECTCD,
430 [0x1a] = KEY_MENU,
431 [0x28] = KEY_TIME,
432 [0x30] = KEY_PREVIOUS,
433 [0x31] = KEY_NEXT,
434 [0x32] = KEY_PLAY,
435 [0x33] = KEY_REWIND,
436 [0x34] = KEY_FORWARD,
437 [0x38] = KEY_STOP,
438 [0x39] = KEY_PAUSE,
439 [0x40] = KEY_CONTEXT_MENU,
440 [0x60] = KEY_FRAMEBACK,
441 [0x61] = KEY_FRAMEFORWARD,
442 [0x63] = KEY_SUBTITLE,
443 [0x64] = KEY_AUDIO,
444 [0x65] = KEY_ANGLE,
445 [0x70] = KEY_INFO,
446 [0x80] = KEY_BLUE,
447 [0x81] = KEY_RED,
448 [0x82] = KEY_GREEN,
449 [0x83] = KEY_YELLOW,
450};
451
452static const unsigned int buzz_keymap[] = {
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469 [ 1] = BTN_TRIGGER_HAPPY1,
470 [ 2] = BTN_TRIGGER_HAPPY2,
471 [ 3] = BTN_TRIGGER_HAPPY3,
472 [ 4] = BTN_TRIGGER_HAPPY4,
473 [ 5] = BTN_TRIGGER_HAPPY5,
474 [ 6] = BTN_TRIGGER_HAPPY6,
475 [ 7] = BTN_TRIGGER_HAPPY7,
476 [ 8] = BTN_TRIGGER_HAPPY8,
477 [ 9] = BTN_TRIGGER_HAPPY9,
478 [10] = BTN_TRIGGER_HAPPY10,
479 [11] = BTN_TRIGGER_HAPPY11,
480 [12] = BTN_TRIGGER_HAPPY12,
481 [13] = BTN_TRIGGER_HAPPY13,
482 [14] = BTN_TRIGGER_HAPPY14,
483 [15] = BTN_TRIGGER_HAPPY15,
484 [16] = BTN_TRIGGER_HAPPY16,
485 [17] = BTN_TRIGGER_HAPPY17,
486 [18] = BTN_TRIGGER_HAPPY18,
487 [19] = BTN_TRIGGER_HAPPY19,
488 [20] = BTN_TRIGGER_HAPPY20,
489};
490
491struct sony_sc {
492 struct hid_device *hdev;
493 struct led_classdev *leds[MAX_LEDS];
494 struct hid_report *output_report;
495 unsigned long quirks;
496 struct work_struct state_worker;
497
498#ifdef CONFIG_SONY_FF
499 __u8 left;
500 __u8 right;
501#endif
502
503 __u8 worker_initialized;
504 __u8 led_state[MAX_LEDS];
505 __u8 led_count;
506};
507
508static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
509 unsigned int *rsize)
510{
511 *rsize = sizeof(ps3remote_rdesc);
512 return ps3remote_rdesc;
513}
514
515static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
516 struct hid_field *field, struct hid_usage *usage,
517 unsigned long **bit, int *max)
518{
519 unsigned int key = usage->hid & HID_USAGE;
520
521 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
522 return -1;
523
524 switch (usage->collection_index) {
525 case 1:
526 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
527 return -1;
528
529 key = ps3remote_keymap_joypad_buttons[key];
530 if (!key)
531 return -1;
532 break;
533 case 2:
534 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
535 return -1;
536
537 key = ps3remote_keymap_remote_buttons[key];
538 if (!key)
539 return -1;
540 break;
541 default:
542 return -1;
543 }
544
545 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
546 return 1;
547}
548
549
550
551static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
552 unsigned int *rsize)
553{
554 struct sony_sc *sc = hid_get_drvdata(hdev);
555
556
557
558
559
560 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
561
562
563
564 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
565
566 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
567 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
568
569 rdesc[55] = 0x06;
570 }
571
572
573
574
575
576
577 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
578 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
579 rdesc = dualshock4_usb_rdesc;
580 *rsize = sizeof(dualshock4_usb_rdesc);
581 }
582
583
584 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
585 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
586 rdesc[83] == 0x75) {
587 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
588 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
589 sizeof(sixaxis_rdesc_fixup));
590 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
591 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
592 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
593 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
594 *rsize = sizeof(sixaxis_rdesc_fixup2);
595 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
596 }
597
598 if (sc->quirks & PS3REMOTE)
599 return ps3remote_fixup(hdev, rdesc, rsize);
600
601 return rdesc;
602}
603
604static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
605 __u8 *rd, int size)
606{
607 struct sony_sc *sc = hid_get_drvdata(hdev);
608
609
610
611
612 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
613 rd[0] == 0x01 && size == 49) {
614 swap(rd[41], rd[42]);
615 swap(rd[43], rd[44]);
616 swap(rd[45], rd[46]);
617 swap(rd[47], rd[48]);
618 }
619
620 return 0;
621}
622
623static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
624 struct hid_field *field, struct hid_usage *usage,
625 unsigned long **bit, int *max)
626{
627 struct sony_sc *sc = hid_get_drvdata(hdev);
628
629 if (sc->quirks & BUZZ_CONTROLLER) {
630 unsigned int key = usage->hid & HID_USAGE;
631
632 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
633 return -1;
634
635 switch (usage->collection_index) {
636 case 1:
637 if (key >= ARRAY_SIZE(buzz_keymap))
638 return -1;
639
640 key = buzz_keymap[key];
641 if (!key)
642 return -1;
643 break;
644 default:
645 return -1;
646 }
647
648 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
649 return 1;
650 }
651
652 if (sc->quirks & PS3REMOTE)
653 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
654
655
656 return 0;
657}
658
659
660
661
662
663
664
665
666
667
668
669static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
670 size_t count, unsigned char report_type)
671{
672 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
673 struct usb_device *dev = interface_to_usbdev(intf);
674 struct usb_host_interface *interface = intf->cur_altsetting;
675 int report_id = buf[0];
676 int ret;
677
678 if (report_type == HID_OUTPUT_REPORT) {
679
680 buf++;
681 count--;
682 }
683
684 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
685 HID_REQ_SET_REPORT,
686 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
687 ((report_type + 1) << 8) | report_id,
688 interface->desc.bInterfaceNumber, buf, count,
689 USB_CTRL_SET_TIMEOUT);
690
691
692 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
693 ret++;
694
695 return ret;
696}
697
698
699
700
701
702
703static int sixaxis_set_operational_usb(struct hid_device *hdev)
704{
705 int ret;
706 char *buf = kmalloc(18, GFP_KERNEL);
707
708 if (!buf)
709 return -ENOMEM;
710
711 ret = hdev->hid_get_raw_report(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT);
712
713 if (ret < 0)
714 hid_err(hdev, "can't set operational mode\n");
715
716 kfree(buf);
717
718 return ret;
719}
720
721static int sixaxis_set_operational_bt(struct hid_device *hdev)
722{
723 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
724 return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
725}
726
727static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
728{
729 struct list_head *report_list =
730 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
731 struct hid_report *report = list_entry(report_list->next,
732 struct hid_report, list);
733 __s32 *value = report->field[0]->value;
734
735 value[0] = 0x00;
736 value[1] = leds[0] ? 0xff : 0x00;
737 value[2] = leds[1] ? 0xff : 0x00;
738 value[3] = leds[2] ? 0xff : 0x00;
739 value[4] = leds[3] ? 0xff : 0x00;
740 value[5] = 0x00;
741 value[6] = 0x00;
742 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
743}
744
745static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
746{
747 struct sony_sc *drv_data = hid_get_drvdata(hdev);
748 int n;
749
750 BUG_ON(count > MAX_LEDS);
751
752 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
753 buzz_set_leds(hdev, leds);
754 } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) ||
755 (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) {
756 for (n = 0; n < count; n++)
757 drv_data->led_state[n] = leds[n];
758 schedule_work(&drv_data->state_worker);
759 }
760}
761
762static void sony_led_set_brightness(struct led_classdev *led,
763 enum led_brightness value)
764{
765 struct device *dev = led->dev->parent;
766 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
767 struct sony_sc *drv_data;
768
769 int n;
770
771 drv_data = hid_get_drvdata(hdev);
772 if (!drv_data) {
773 hid_err(hdev, "No device data\n");
774 return;
775 }
776
777 for (n = 0; n < drv_data->led_count; n++) {
778 if (led == drv_data->leds[n]) {
779 if (value != drv_data->led_state[n]) {
780 drv_data->led_state[n] = value;
781 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
782 }
783 break;
784 }
785 }
786}
787
788static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
789{
790 struct device *dev = led->dev->parent;
791 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
792 struct sony_sc *drv_data;
793
794 int n;
795 int on = 0;
796
797 drv_data = hid_get_drvdata(hdev);
798 if (!drv_data) {
799 hid_err(hdev, "No device data\n");
800 return LED_OFF;
801 }
802
803 for (n = 0; n < drv_data->led_count; n++) {
804 if (led == drv_data->leds[n]) {
805 on = !!(drv_data->led_state[n]);
806 break;
807 }
808 }
809
810 return on ? LED_FULL : LED_OFF;
811}
812
813static void sony_leds_remove(struct hid_device *hdev)
814{
815 struct sony_sc *drv_data;
816 struct led_classdev *led;
817 int n;
818
819 drv_data = hid_get_drvdata(hdev);
820 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
821
822 for (n = 0; n < drv_data->led_count; n++) {
823 led = drv_data->leds[n];
824 drv_data->leds[n] = NULL;
825 if (!led)
826 continue;
827 led_classdev_unregister(led);
828 kfree(led);
829 }
830
831 drv_data->led_count = 0;
832}
833
834static int sony_leds_init(struct hid_device *hdev)
835{
836 struct sony_sc *drv_data;
837 int n, ret = 0;
838 int max_brightness;
839 int use_colors;
840 struct led_classdev *led;
841 size_t name_sz;
842 char *name;
843 size_t name_len;
844 const char *name_fmt;
845 static const char * const color_str[] = { "red", "green", "blue" };
846 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
847
848 drv_data = hid_get_drvdata(hdev);
849 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
850
851 if (drv_data->quirks & BUZZ_CONTROLLER) {
852 drv_data->led_count = 4;
853 max_brightness = 1;
854 use_colors = 0;
855 name_len = strlen("::buzz#");
856 name_fmt = "%s::buzz%d";
857
858 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
859 return -ENODEV;
860 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) {
861 drv_data->led_count = 3;
862 max_brightness = 255;
863 use_colors = 1;
864 name_len = 0;
865 name_fmt = "%s:%s";
866 } else {
867 drv_data->led_count = 4;
868 max_brightness = 1;
869 use_colors = 0;
870 name_len = strlen("::sony#");
871 name_fmt = "%s::sony%d";
872 }
873
874
875
876
877 sony_set_leds(hdev, initial_values, drv_data->led_count);
878
879 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
880
881 for (n = 0; n < drv_data->led_count; n++) {
882
883 if (use_colors)
884 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
885
886 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
887 if (!led) {
888 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
889 ret = -ENOMEM;
890 goto error_leds;
891 }
892
893 name = (void *)(&led[1]);
894 if (use_colors)
895 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
896 else
897 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
898 led->name = name;
899 led->brightness = 0;
900 led->max_brightness = max_brightness;
901 led->brightness_get = sony_led_get_brightness;
902 led->brightness_set = sony_led_set_brightness;
903
904 ret = led_classdev_register(&hdev->dev, led);
905 if (ret) {
906 hid_err(hdev, "Failed to register LED %d\n", n);
907 kfree(led);
908 goto error_leds;
909 }
910
911 drv_data->leds[n] = led;
912 }
913
914 return ret;
915
916error_leds:
917 sony_leds_remove(hdev);
918
919 return ret;
920}
921
922static void sixaxis_state_worker(struct work_struct *work)
923{
924 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
925 unsigned char buf[] = {
926 0x01,
927 0x00, 0xff, 0x00, 0xff, 0x00,
928 0x00, 0x00, 0x00, 0x00, 0x00,
929 0xff, 0x27, 0x10, 0x00, 0x32,
930 0xff, 0x27, 0x10, 0x00, 0x32,
931 0xff, 0x27, 0x10, 0x00, 0x32,
932 0xff, 0x27, 0x10, 0x00, 0x32,
933 0x00, 0x00, 0x00, 0x00, 0x00
934 };
935
936#ifdef CONFIG_SONY_FF
937 buf[3] = sc->right ? 1 : 0;
938 buf[5] = sc->left;
939#endif
940
941 buf[10] |= sc->led_state[0] << 1;
942 buf[10] |= sc->led_state[1] << 2;
943 buf[10] |= sc->led_state[2] << 3;
944 buf[10] |= sc->led_state[3] << 4;
945
946 sc->hdev->hid_output_raw_report(sc->hdev, buf, sizeof(buf),
947 HID_OUTPUT_REPORT);
948}
949
950static void dualshock4_state_worker(struct work_struct *work)
951{
952 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
953 struct hid_device *hdev = sc->hdev;
954 struct hid_report *report = sc->output_report;
955 __s32 *value = report->field[0]->value;
956
957 value[0] = 0x03;
958
959#ifdef CONFIG_SONY_FF
960 value[3] = sc->right;
961 value[4] = sc->left;
962#endif
963
964 value[5] = sc->led_state[0];
965 value[6] = sc->led_state[1];
966 value[7] = sc->led_state[2];
967
968 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
969}
970
971#ifdef CONFIG_SONY_FF
972static int sony_play_effect(struct input_dev *dev, void *data,
973 struct ff_effect *effect)
974{
975 struct hid_device *hid = input_get_drvdata(dev);
976 struct sony_sc *sc = hid_get_drvdata(hid);
977
978 if (effect->type != FF_RUMBLE)
979 return 0;
980
981 sc->left = effect->u.rumble.strong_magnitude / 256;
982 sc->right = effect->u.rumble.weak_magnitude / 256;
983
984 schedule_work(&sc->state_worker);
985 return 0;
986}
987
988static int sony_init_ff(struct hid_device *hdev)
989{
990 struct hid_input *hidinput = list_entry(hdev->inputs.next,
991 struct hid_input, list);
992 struct input_dev *input_dev = hidinput->input;
993
994 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
995 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
996}
997
998#else
999static int sony_init_ff(struct hid_device *hdev)
1000{
1001 return 0;
1002}
1003#endif
1004
1005static int sony_set_output_report(struct sony_sc *sc, int req_id, int req_size)
1006{
1007 struct list_head *head, *list;
1008 struct hid_report *report;
1009 struct hid_device *hdev = sc->hdev;
1010
1011 list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1012
1013 list_for_each(head, list) {
1014 report = list_entry(head, struct hid_report, list);
1015
1016 if (report->id == req_id) {
1017 if (report->size < req_size) {
1018 hid_err(hdev, "Output report 0x%02x (%i bits) is smaller than requested size (%i bits)\n",
1019 req_id, report->size, req_size);
1020 return -EINVAL;
1021 }
1022 sc->output_report = report;
1023 return 0;
1024 }
1025 }
1026
1027 hid_err(hdev, "Unable to locate output report 0x%02x\n", req_id);
1028
1029 return -EINVAL;
1030}
1031
1032static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1033{
1034 int ret;
1035 unsigned long quirks = id->driver_data;
1036 struct sony_sc *sc;
1037 unsigned int connect_mask = HID_CONNECT_DEFAULT;
1038
1039 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
1040 if (sc == NULL) {
1041 hid_err(hdev, "can't alloc sony descriptor\n");
1042 return -ENOMEM;
1043 }
1044
1045 sc->quirks = quirks;
1046 hid_set_drvdata(hdev, sc);
1047 sc->hdev = hdev;
1048
1049 ret = hid_parse(hdev);
1050 if (ret) {
1051 hid_err(hdev, "parse failed\n");
1052 return ret;
1053 }
1054
1055 if (sc->quirks & VAIO_RDESC_CONSTANT)
1056 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1057 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1058 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1059 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1060 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1061
1062 ret = hid_hw_start(hdev, connect_mask);
1063 if (ret) {
1064 hid_err(hdev, "hw start failed\n");
1065 return ret;
1066 }
1067
1068 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1069 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
1070 ret = sixaxis_set_operational_usb(hdev);
1071
1072 sc->worker_initialized = 1;
1073 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
1074 }
1075 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1076 ret = sixaxis_set_operational_bt(hdev);
1077 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1078
1079 ret = sony_set_output_report(sc, 0x05, 248);
1080 if (ret < 0)
1081 goto err_stop;
1082
1083 sc->worker_initialized = 1;
1084 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1085 } else {
1086 ret = 0;
1087 }
1088
1089 if (ret < 0)
1090 goto err_stop;
1091
1092 if (sc->quirks & SONY_LED_SUPPORT) {
1093 ret = sony_leds_init(hdev);
1094 if (ret < 0)
1095 goto err_stop;
1096 }
1097
1098 if (sc->quirks & SONY_FF_SUPPORT) {
1099 ret = sony_init_ff(hdev);
1100 if (ret < 0)
1101 goto err_stop;
1102 }
1103
1104 return 0;
1105err_stop:
1106 if (sc->quirks & SONY_LED_SUPPORT)
1107 sony_leds_remove(hdev);
1108 hid_hw_stop(hdev);
1109 return ret;
1110}
1111
1112static void sony_remove(struct hid_device *hdev)
1113{
1114 struct sony_sc *sc = hid_get_drvdata(hdev);
1115
1116 if (sc->quirks & SONY_LED_SUPPORT)
1117 sony_leds_remove(hdev);
1118
1119 if (sc->worker_initialized)
1120 cancel_work_sync(&sc->state_worker);
1121
1122 hid_hw_stop(hdev);
1123}
1124
1125static const struct hid_device_id sony_devices[] = {
1126 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1127 .driver_data = SIXAXIS_CONTROLLER_USB },
1128 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1129 .driver_data = SIXAXIS_CONTROLLER_USB },
1130 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1131 .driver_data = SIXAXIS_CONTROLLER_BT },
1132 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1133 .driver_data = VAIO_RDESC_CONSTANT },
1134 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1135 .driver_data = VAIO_RDESC_CONSTANT },
1136
1137
1138 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1139 .driver_data = BUZZ_CONTROLLER },
1140 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1141 .driver_data = BUZZ_CONTROLLER },
1142
1143 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1144 .driver_data = PS3REMOTE },
1145
1146 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1147 .driver_data = PS3REMOTE },
1148
1149 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
1150 .driver_data = DUALSHOCK4_CONTROLLER_USB },
1151 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
1152 .driver_data = DUALSHOCK4_CONTROLLER_BT },
1153 { }
1154};
1155MODULE_DEVICE_TABLE(hid, sony_devices);
1156
1157static struct hid_driver sony_driver = {
1158 .name = "sony",
1159 .id_table = sony_devices,
1160 .input_mapping = sony_mapping,
1161 .probe = sony_probe,
1162 .remove = sony_remove,
1163 .report_fixup = sony_report_fixup,
1164 .raw_event = sony_raw_event
1165};
1166module_hid_driver(sony_driver);
1167
1168MODULE_LICENSE("GPL");
1169