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
28
29#include <linux/device.h>
30#include <linux/hid.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/leds.h>
34#include <linux/power_supply.h>
35#include <linux/spinlock.h>
36#include <linux/list.h>
37#include <linux/idr.h>
38#include <linux/input/mt.h>
39
40#include "hid-ids.h"
41
42#define VAIO_RDESC_CONSTANT BIT(0)
43#define SIXAXIS_CONTROLLER_USB BIT(1)
44#define SIXAXIS_CONTROLLER_BT BIT(2)
45#define BUZZ_CONTROLLER BIT(3)
46#define PS3REMOTE BIT(4)
47#define DUALSHOCK4_CONTROLLER_USB BIT(5)
48#define DUALSHOCK4_CONTROLLER_BT BIT(6)
49#define MOTION_CONTROLLER_USB BIT(7)
50#define MOTION_CONTROLLER_BT BIT(8)
51#define NAVIGATION_CONTROLLER_USB BIT(9)
52#define NAVIGATION_CONTROLLER_BT BIT(10)
53
54#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
55#define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
56#define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
57 NAVIGATION_CONTROLLER_BT)
58#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
59 DUALSHOCK4_CONTROLLER_BT)
60#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
61 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
62 NAVIGATION_CONTROLLER)
63#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
64 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
65#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
66 MOTION_CONTROLLER)
67
68#define MAX_LEDS 4
69
70
71
72
73
74
75
76
77static __u8 sixaxis_rdesc[] = {
78 0x05, 0x01,
79 0x09, 0x04,
80 0xA1, 0x01,
81 0xA1, 0x02,
82 0x85, 0x01,
83 0x75, 0x08,
84 0x95, 0x01,
85 0x15, 0x00,
86 0x26, 0xFF, 0x00,
87 0x81, 0x03,
88 0x75, 0x01,
89 0x95, 0x13,
90 0x15, 0x00,
91 0x25, 0x01,
92 0x35, 0x00,
93 0x45, 0x01,
94 0x05, 0x09,
95 0x19, 0x01,
96 0x29, 0x13,
97 0x81, 0x02,
98 0x75, 0x01,
99 0x95, 0x0D,
100 0x06, 0x00, 0xFF,
101 0x81, 0x03,
102 0x15, 0x00,
103 0x26, 0xFF, 0x00,
104 0x05, 0x01,
105 0x09, 0x01,
106 0xA1, 0x00,
107 0x75, 0x08,
108 0x95, 0x04,
109 0x35, 0x00,
110 0x46, 0xFF, 0x00,
111 0x09, 0x30,
112 0x09, 0x31,
113 0x09, 0x32,
114 0x09, 0x35,
115 0x81, 0x02,
116 0xC0,
117 0x05, 0x01,
118 0x95, 0x13,
119 0x09, 0x01,
120 0x81, 0x02,
121 0x95, 0x0C,
122 0x81, 0x01,
123 0x75, 0x10,
124 0x95, 0x04,
125 0x26, 0xFF, 0x03,
126 0x46, 0xFF, 0x03,
127 0x09, 0x01,
128 0x81, 0x02,
129 0xC0,
130 0xA1, 0x02,
131 0x85, 0x02,
132 0x75, 0x08,
133 0x95, 0x30,
134 0x09, 0x01,
135 0xB1, 0x02,
136 0xC0,
137 0xA1, 0x02,
138 0x85, 0xEE,
139 0x75, 0x08,
140 0x95, 0x30,
141 0x09, 0x01,
142 0xB1, 0x02,
143 0xC0,
144 0xA1, 0x02,
145 0x85, 0xEF,
146 0x75, 0x08,
147 0x95, 0x30,
148 0x09, 0x01,
149 0xB1, 0x02,
150 0xC0,
151 0xC0
152};
153
154
155static __u8 motion_rdesc[] = {
156 0x05, 0x01,
157 0x09, 0x04,
158 0xA1, 0x01,
159 0xA1, 0x02,
160 0x85, 0x01,
161 0x75, 0x01,
162 0x95, 0x15,
163 0x15, 0x00,
164 0x25, 0x01,
165 0x35, 0x00,
166 0x45, 0x01,
167 0x05, 0x09,
168 0x19, 0x01,
169 0x29, 0x15,
170 0x81, 0x02,
171 0x95, 0x0B,
172 0x06, 0x00, 0xFF,
173 0x81, 0x03,
174 0x15, 0x00,
175 0x26, 0xFF, 0x00,
176 0x05, 0x01,
177 0xA1, 0x00,
178 0x75, 0x08,
179 0x95, 0x01,
180 0x35, 0x00,
181 0x46, 0xFF, 0x00,
182 0x09, 0x30,
183 0x81, 0x02,
184 0xC0,
185 0x06, 0x00, 0xFF,
186 0x75, 0x08,
187 0x95, 0x07,
188 0x81, 0x02,
189 0x05, 0x01,
190 0x75, 0x10,
191 0x46, 0xFF, 0xFF,
192 0x27, 0xFF, 0xFF, 0x00, 0x00,
193 0x95, 0x03,
194 0x09, 0x33,
195 0x09, 0x34,
196 0x09, 0x35,
197 0x81, 0x02,
198 0x06, 0x00, 0xFF,
199 0x95, 0x03,
200 0x81, 0x02,
201 0x05, 0x01,
202 0x09, 0x01,
203 0x95, 0x03,
204 0x81, 0x02,
205 0x06, 0x00, 0xFF,
206 0x95, 0x03,
207 0x81, 0x02,
208 0x75, 0x0C,
209 0x46, 0xFF, 0x0F,
210 0x26, 0xFF, 0x0F,
211 0x95, 0x04,
212 0x81, 0x02,
213 0x75, 0x08,
214 0x46, 0xFF, 0x00,
215 0x26, 0xFF, 0x00,
216 0x95, 0x06,
217 0x81, 0x02,
218 0x75, 0x08,
219 0x95, 0x30,
220 0x09, 0x01,
221 0x91, 0x02,
222 0x75, 0x08,
223 0x95, 0x30,
224 0x09, 0x01,
225 0xB1, 0x02,
226 0xC0,
227 0xA1, 0x02,
228 0x85, 0x02,
229 0x75, 0x08,
230 0x95, 0x30,
231 0x09, 0x01,
232 0xB1, 0x02,
233 0xC0,
234 0xA1, 0x02,
235 0x85, 0xEE,
236 0x75, 0x08,
237 0x95, 0x30,
238 0x09, 0x01,
239 0xB1, 0x02,
240 0xC0,
241 0xA1, 0x02,
242 0x85, 0xEF,
243 0x75, 0x08,
244 0x95, 0x30,
245 0x09, 0x01,
246 0xB1, 0x02,
247 0xC0,
248 0xC0
249};
250
251
252static __u8 navigation_rdesc[] = {
253 0x05, 0x01,
254 0x09, 0x04,
255 0xA1, 0x01,
256 0xA1, 0x02,
257 0x85, 0x01,
258 0x75, 0x08,
259 0x95, 0x01,
260 0x15, 0x00,
261 0x26, 0xFF, 0x00,
262 0x81, 0x03,
263 0x75, 0x01,
264 0x95, 0x13,
265 0x15, 0x00,
266 0x25, 0x01,
267 0x35, 0x00,
268 0x45, 0x01,
269 0x05, 0x09,
270 0x19, 0x01,
271 0x29, 0x13,
272 0x81, 0x02,
273 0x75, 0x01,
274 0x95, 0x0D,
275 0x06, 0x00, 0xFF,
276 0x81, 0x03,
277 0x15, 0x00,
278 0x26, 0xFF, 0x00,
279 0x05, 0x01,
280 0x09, 0x01,
281 0xA1, 0x00,
282 0x75, 0x08,
283 0x95, 0x02,
284 0x35, 0x00,
285 0x46, 0xFF, 0x00,
286 0x09, 0x30,
287 0x09, 0x31,
288 0x81, 0x02,
289 0xC0,
290 0x06, 0x00, 0xFF,
291 0x95, 0x06,
292 0x81, 0x03,
293 0x05, 0x01,
294 0x75, 0x08,
295 0x95, 0x05,
296 0x09, 0x01,
297 0x81, 0x02,
298 0x06, 0x00, 0xFF,
299 0x95, 0x01,
300 0x81, 0x02,
301 0x05, 0x01,
302 0x95, 0x01,
303 0x09, 0x01,
304 0x81, 0x02,
305 0x06, 0x00, 0xFF,
306 0x95, 0x1E,
307 0x81, 0x02,
308 0x75, 0x08,
309 0x95, 0x30,
310 0x09, 0x01,
311 0x91, 0x02,
312 0x75, 0x08,
313 0x95, 0x30,
314 0x09, 0x01,
315 0xB1, 0x02,
316 0xC0,
317 0xA1, 0x02,
318 0x85, 0x02,
319 0x75, 0x08,
320 0x95, 0x30,
321 0x09, 0x01,
322 0xB1, 0x02,
323 0xC0,
324 0xA1, 0x02,
325 0x85, 0xEE,
326 0x75, 0x08,
327 0x95, 0x30,
328 0x09, 0x01,
329 0xB1, 0x02,
330 0xC0,
331 0xA1, 0x02,
332 0x85, 0xEF,
333 0x75, 0x08,
334 0x95, 0x30,
335 0x09, 0x01,
336 0xB1, 0x02,
337 0xC0,
338 0xC0
339};
340
341
342
343
344
345
346
347static u8 dualshock4_usb_rdesc[] = {
348 0x05, 0x01,
349 0x09, 0x05,
350 0xA1, 0x01,
351 0x85, 0x01,
352 0x09, 0x30,
353 0x09, 0x31,
354 0x09, 0x32,
355 0x09, 0x35,
356 0x15, 0x00,
357 0x26, 0xFF, 0x00,
358 0x75, 0x08,
359 0x95, 0x04,
360 0x81, 0x02,
361 0x09, 0x39,
362 0x15, 0x00,
363 0x25, 0x07,
364 0x35, 0x00,
365 0x46, 0x3B, 0x01,
366 0x65, 0x14,
367 0x75, 0x04,
368 0x95, 0x01,
369 0x81, 0x42,
370 0x65, 0x00,
371 0x05, 0x09,
372 0x19, 0x01,
373 0x29, 0x0E,
374 0x15, 0x00,
375 0x25, 0x01,
376 0x75, 0x01,
377 0x95, 0x0E,
378 0x81, 0x02,
379 0x06, 0x00, 0xFF,
380 0x09, 0x20,
381 0x75, 0x06,
382 0x95, 0x01,
383 0x15, 0x00,
384 0x25, 0x3F,
385 0x81, 0x02,
386 0x05, 0x01,
387 0x09, 0x33,
388 0x09, 0x34,
389 0x15, 0x00,
390 0x26, 0xFF, 0x00,
391 0x75, 0x08,
392 0x95, 0x02,
393 0x81, 0x02,
394 0x06, 0x00, 0xFF,
395 0x09, 0x21,
396 0x95, 0x03,
397 0x81, 0x02,
398 0x05, 0x01,
399 0x19, 0x40,
400 0x29, 0x42,
401 0x16, 0x00, 0x80,
402 0x26, 0x00, 0x7F,
403 0x75, 0x10,
404 0x95, 0x03,
405 0x81, 0x02,
406 0x19, 0x43,
407 0x29, 0x45,
408 0x16, 0x00, 0xE0,
409 0x26, 0xFF, 0x1F,
410 0x95, 0x03,
411 0x81, 0x02,
412 0x06, 0x00, 0xFF,
413 0x09, 0x21,
414 0x15, 0x00,
415 0x26, 0xFF, 0x00,
416 0x75, 0x08,
417 0x95, 0x27,
418 0x81, 0x02,
419 0x85, 0x05,
420 0x09, 0x22,
421 0x95, 0x1F,
422 0x91, 0x02,
423 0x85, 0x04,
424 0x09, 0x23,
425 0x95, 0x24,
426 0xB1, 0x02,
427 0x85, 0x02,
428 0x09, 0x24,
429 0x95, 0x24,
430 0xB1, 0x02,
431 0x85, 0x08,
432 0x09, 0x25,
433 0x95, 0x03,
434 0xB1, 0x02,
435 0x85, 0x10,
436 0x09, 0x26,
437 0x95, 0x04,
438 0xB1, 0x02,
439 0x85, 0x11,
440 0x09, 0x27,
441 0x95, 0x02,
442 0xB1, 0x02,
443 0x85, 0x12,
444 0x06, 0x02, 0xFF,
445 0x09, 0x21,
446 0x95, 0x0F,
447 0xB1, 0x02,
448 0x85, 0x13,
449 0x09, 0x22,
450 0x95, 0x16,
451 0xB1, 0x02,
452 0x85, 0x14,
453 0x06, 0x05, 0xFF,
454 0x09, 0x20,
455 0x95, 0x10,
456 0xB1, 0x02,
457 0x85, 0x15,
458 0x09, 0x21,
459 0x95, 0x2C,
460 0xB1, 0x02,
461 0x06, 0x80, 0xFF,
462 0x85, 0x80,
463 0x09, 0x20,
464 0x95, 0x06,
465 0xB1, 0x02,
466 0x85, 0x81,
467 0x09, 0x21,
468 0x95, 0x06,
469 0xB1, 0x02,
470 0x85, 0x82,
471 0x09, 0x22,
472 0x95, 0x05,
473 0xB1, 0x02,
474 0x85, 0x83,
475 0x09, 0x23,
476 0x95, 0x01,
477 0xB1, 0x02,
478 0x85, 0x84,
479 0x09, 0x24,
480 0x95, 0x04,
481 0xB1, 0x02,
482 0x85, 0x85,
483 0x09, 0x25,
484 0x95, 0x06,
485 0xB1, 0x02,
486 0x85, 0x86,
487 0x09, 0x26,
488 0x95, 0x06,
489 0xB1, 0x02,
490 0x85, 0x87,
491 0x09, 0x27,
492 0x95, 0x23,
493 0xB1, 0x02,
494 0x85, 0x88,
495 0x09, 0x28,
496 0x95, 0x22,
497 0xB1, 0x02,
498 0x85, 0x89,
499 0x09, 0x29,
500 0x95, 0x02,
501 0xB1, 0x02,
502 0x85, 0x90,
503 0x09, 0x30,
504 0x95, 0x05,
505 0xB1, 0x02,
506 0x85, 0x91,
507 0x09, 0x31,
508 0x95, 0x03,
509 0xB1, 0x02,
510 0x85, 0x92,
511 0x09, 0x32,
512 0x95, 0x03,
513 0xB1, 0x02,
514 0x85, 0x93,
515 0x09, 0x33,
516 0x95, 0x0C,
517 0xB1, 0x02,
518 0x85, 0xA0,
519 0x09, 0x40,
520 0x95, 0x06,
521 0xB1, 0x02,
522 0x85, 0xA1,
523 0x09, 0x41,
524 0x95, 0x01,
525 0xB1, 0x02,
526 0x85, 0xA2,
527 0x09, 0x42,
528 0x95, 0x01,
529 0xB1, 0x02,
530 0x85, 0xA3,
531 0x09, 0x43,
532 0x95, 0x30,
533 0xB1, 0x02,
534 0x85, 0xA4,
535 0x09, 0x44,
536 0x95, 0x0D,
537 0xB1, 0x02,
538 0x85, 0xA5,
539 0x09, 0x45,
540 0x95, 0x15,
541 0xB1, 0x02,
542 0x85, 0xA6,
543 0x09, 0x46,
544 0x95, 0x15,
545 0xB1, 0x02,
546 0x85, 0xF0,
547 0x09, 0x47,
548 0x95, 0x3F,
549 0xB1, 0x02,
550 0x85, 0xF1,
551 0x09, 0x48,
552 0x95, 0x3F,
553 0xB1, 0x02,
554 0x85, 0xF2,
555 0x09, 0x49,
556 0x95, 0x0F,
557 0xB1, 0x02,
558 0x85, 0xA7,
559 0x09, 0x4A,
560 0x95, 0x01,
561 0xB1, 0x02,
562 0x85, 0xA8,
563 0x09, 0x4B,
564 0x95, 0x01,
565 0xB1, 0x02,
566 0x85, 0xA9,
567 0x09, 0x4C,
568 0x95, 0x08,
569 0xB1, 0x02,
570 0x85, 0xAA,
571 0x09, 0x4E,
572 0x95, 0x01,
573 0xB1, 0x02,
574 0x85, 0xAB,
575 0x09, 0x4F,
576 0x95, 0x39,
577 0xB1, 0x02,
578 0x85, 0xAC,
579 0x09, 0x50,
580 0x95, 0x39,
581 0xB1, 0x02,
582 0x85, 0xAD,
583 0x09, 0x51,
584 0x95, 0x0B,
585 0xB1, 0x02,
586 0x85, 0xAE,
587 0x09, 0x52,
588 0x95, 0x01,
589 0xB1, 0x02,
590 0x85, 0xAF,
591 0x09, 0x53,
592 0x95, 0x02,
593 0xB1, 0x02,
594 0x85, 0xB0,
595 0x09, 0x54,
596 0x95, 0x3F,
597 0xB1, 0x02,
598 0xC0
599};
600
601
602
603
604
605
606
607
608
609static u8 dualshock4_bt_rdesc[] = {
610 0x05, 0x01,
611 0x09, 0x05,
612 0xA1, 0x01,
613 0x85, 0x01,
614 0x75, 0x08,
615 0x95, 0x0A,
616 0x81, 0x02,
617 0x06, 0x04, 0xFF,
618 0x85, 0x02,
619 0x09, 0x24,
620 0x95, 0x24,
621 0xB1, 0x02,
622 0x85, 0xA3,
623 0x09, 0x25,
624 0x95, 0x30,
625 0xB1, 0x02,
626 0x85, 0x05,
627 0x09, 0x26,
628 0x95, 0x28,
629 0xB1, 0x02,
630 0x85, 0x06,
631 0x09, 0x27,
632 0x95, 0x34,
633 0xB1, 0x02,
634 0x85, 0x07,
635 0x09, 0x28,
636 0x95, 0x30,
637 0xB1, 0x02,
638 0x85, 0x08,
639 0x09, 0x29,
640 0x95, 0x2F,
641 0xB1, 0x02,
642 0x06, 0x03, 0xFF,
643 0x85, 0x03,
644 0x09, 0x21,
645 0x95, 0x26,
646 0xB1, 0x02,
647 0x85, 0x04,
648 0x09, 0x22,
649 0x95, 0x2E,
650 0xB1, 0x02,
651 0x85, 0xF0,
652 0x09, 0x47,
653 0x95, 0x3F,
654 0xB1, 0x02,
655 0x85, 0xF1,
656 0x09, 0x48,
657 0x95, 0x3F,
658 0xB1, 0x02,
659 0x85, 0xF2,
660 0x09, 0x49,
661 0x95, 0x0F,
662 0xB1, 0x02,
663 0x85, 0x11,
664 0x06, 0x00, 0xFF,
665 0x09, 0x20,
666 0x95, 0x02,
667 0x81, 0x02,
668 0x05, 0x01,
669 0x09, 0x30,
670 0x09, 0x31,
671 0x09, 0x32,
672 0x09, 0x35,
673 0x15, 0x00,
674 0x26, 0xFF, 0x00,
675 0x75, 0x08,
676 0x95, 0x04,
677 0x81, 0x02,
678 0x09, 0x39,
679 0x15, 0x00,
680 0x25, 0x07,
681 0x75, 0x04,
682 0x95, 0x01,
683 0x81, 0x42,
684 0x05, 0x09,
685 0x19, 0x01,
686 0x29, 0x0E,
687 0x15, 0x00,
688 0x25, 0x01,
689 0x75, 0x01,
690 0x95, 0x0E,
691 0x81, 0x02,
692 0x75, 0x06,
693 0x95, 0x01,
694 0x81, 0x01,
695 0x05, 0x01,
696 0x09, 0x33,
697 0x09, 0x34,
698 0x15, 0x00,
699 0x26, 0xFF, 0x00,
700 0x75, 0x08,
701 0x95, 0x02,
702 0x81, 0x02,
703 0x06, 0x00, 0xFF,
704 0x09, 0x20,
705 0x95, 0x03,
706 0x81, 0x02,
707 0x05, 0x01,
708 0x19, 0x40,
709 0x29, 0x42,
710 0x16, 0x00, 0x80,
711 0x26, 0x00, 0x7F,
712 0x75, 0x10,
713 0x95, 0x03,
714 0x81, 0x02,
715 0x19, 0x43,
716 0x29, 0x45,
717 0x16, 0x00, 0xE0,
718 0x26, 0xFF, 0x1F,
719 0x95, 0x03,
720 0x81, 0x02,
721 0x06, 0x00, 0xFF,
722 0x09, 0x20,
723 0x15, 0x00,
724 0x26, 0xFF, 0x00,
725 0x75, 0x08,
726 0x95, 0x31,
727 0x81, 0x02,
728 0x09, 0x21,
729 0x75, 0x08,
730 0x95, 0x4D,
731 0x91, 0x02,
732 0x85, 0x12,
733 0x09, 0x22,
734 0x95, 0x8D,
735 0x81, 0x02,
736 0x09, 0x23,
737 0x91, 0x02,
738 0x85, 0x13,
739 0x09, 0x24,
740 0x95, 0xCD,
741 0x81, 0x02,
742 0x09, 0x25,
743 0x91, 0x02,
744 0x85, 0x14,
745 0x09, 0x26,
746 0x96, 0x0D, 0x01,
747 0x81, 0x02,
748 0x09, 0x27,
749 0x91, 0x02,
750 0x85, 0x15,
751 0x09, 0x28,
752 0x96, 0x4D, 0x01,
753 0x81, 0x02,
754 0x09, 0x29,
755 0x91, 0x02,
756 0x85, 0x16,
757 0x09, 0x2A,
758 0x96, 0x8D, 0x01,
759 0x81, 0x02,
760 0x09, 0x2B,
761 0x91, 0x02,
762 0x85, 0x17,
763 0x09, 0x2C,
764 0x96, 0xCD, 0x01,
765 0x81, 0x02,
766 0x09, 0x2D,
767 0x91, 0x02,
768 0x85, 0x18,
769 0x09, 0x2E,
770 0x96, 0x0D, 0x02,
771 0x81, 0x02,
772 0x09, 0x2F,
773 0x91, 0x02,
774 0x85, 0x19,
775 0x09, 0x30,
776 0x96, 0x22, 0x02,
777 0x81, 0x02,
778 0x09, 0x31,
779 0x91, 0x02,
780 0x06, 0x80, 0xFF,
781 0x85, 0x82,
782 0x09, 0x22,
783 0x95, 0x3F,
784 0xB1, 0x02,
785 0x85, 0x83,
786 0x09, 0x23,
787 0xB1, 0x02,
788 0x85, 0x84,
789 0x09, 0x24,
790 0xB1, 0x02,
791 0x85, 0x90,
792 0x09, 0x30,
793 0xB1, 0x02,
794 0x85, 0x91,
795 0x09, 0x31,
796 0xB1, 0x02,
797 0x85, 0x92,
798 0x09, 0x32,
799 0xB1, 0x02,
800 0x85, 0x93,
801 0x09, 0x33,
802 0xB1, 0x02,
803 0x85, 0xA0,
804 0x09, 0x40,
805 0xB1, 0x02,
806 0x85, 0xA4,
807 0x09, 0x44,
808 0xB1, 0x02,
809 0xC0
810};
811
812static __u8 ps3remote_rdesc[] = {
813 0x05, 0x01,
814 0x09, 0x05,
815 0xA1, 0x01,
816
817
818 0xA1, 0x02,
819
820
821
822 0x75, 0x08,
823 0x95, 0x01,
824 0x81, 0x01,
825
826
827
828 0x05, 0x09,
829 0x19, 0x01,
830 0x29, 0x18,
831 0x14,
832 0x25, 0x01,
833 0x75, 0x01,
834 0x95, 0x18,
835 0x81, 0x02,
836
837 0xC0,
838
839
840 0xA1, 0x02,
841
842
843 0x05, 0x09,
844 0x18,
845 0x29, 0xFE,
846 0x14,
847 0x26, 0xFE, 0x00,
848 0x75, 0x08,
849 0x95, 0x01,
850 0x80,
851
852
853
854 0x75, 0x08,
855 0x95, 0x06,
856 0x81, 0x01,
857
858
859 0x05, 0x06,
860 0x09, 0x20,
861 0x14,
862 0x25, 0x05,
863 0x75, 0x08,
864 0x95, 0x01,
865 0x81, 0x02,
866
867 0xC0,
868
869 0xC0
870};
871
872static const unsigned int ps3remote_keymap_joypad_buttons[] = {
873 [0x01] = KEY_SELECT,
874 [0x02] = BTN_THUMBL,
875 [0x03] = BTN_THUMBR,
876 [0x04] = BTN_START,
877 [0x05] = KEY_UP,
878 [0x06] = KEY_RIGHT,
879 [0x07] = KEY_DOWN,
880 [0x08] = KEY_LEFT,
881 [0x09] = BTN_TL2,
882 [0x0a] = BTN_TR2,
883 [0x0b] = BTN_TL,
884 [0x0c] = BTN_TR,
885 [0x0d] = KEY_OPTION,
886 [0x0e] = KEY_BACK,
887 [0x0f] = BTN_0,
888 [0x10] = KEY_SCREEN,
889 [0x11] = KEY_HOMEPAGE,
890 [0x14] = KEY_ENTER,
891};
892static const unsigned int ps3remote_keymap_remote_buttons[] = {
893 [0x00] = KEY_1,
894 [0x01] = KEY_2,
895 [0x02] = KEY_3,
896 [0x03] = KEY_4,
897 [0x04] = KEY_5,
898 [0x05] = KEY_6,
899 [0x06] = KEY_7,
900 [0x07] = KEY_8,
901 [0x08] = KEY_9,
902 [0x09] = KEY_0,
903 [0x0e] = KEY_ESC,
904 [0x0f] = KEY_CLEAR,
905 [0x16] = KEY_EJECTCD,
906 [0x1a] = KEY_MENU,
907 [0x28] = KEY_TIME,
908 [0x30] = KEY_PREVIOUS,
909 [0x31] = KEY_NEXT,
910 [0x32] = KEY_PLAY,
911 [0x33] = KEY_REWIND,
912 [0x34] = KEY_FORWARD,
913 [0x38] = KEY_STOP,
914 [0x39] = KEY_PAUSE,
915 [0x40] = KEY_CONTEXT_MENU,
916 [0x60] = KEY_FRAMEBACK,
917 [0x61] = KEY_FRAMEFORWARD,
918 [0x63] = KEY_SUBTITLE,
919 [0x64] = KEY_AUDIO,
920 [0x65] = KEY_ANGLE,
921 [0x70] = KEY_INFO,
922 [0x80] = KEY_BLUE,
923 [0x81] = KEY_RED,
924 [0x82] = KEY_GREEN,
925 [0x83] = KEY_YELLOW,
926};
927
928static const unsigned int buzz_keymap[] = {
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946 [ 1] = BTN_TRIGGER_HAPPY1,
947 [ 2] = BTN_TRIGGER_HAPPY2,
948 [ 3] = BTN_TRIGGER_HAPPY3,
949 [ 4] = BTN_TRIGGER_HAPPY4,
950 [ 5] = BTN_TRIGGER_HAPPY5,
951 [ 6] = BTN_TRIGGER_HAPPY6,
952 [ 7] = BTN_TRIGGER_HAPPY7,
953 [ 8] = BTN_TRIGGER_HAPPY8,
954 [ 9] = BTN_TRIGGER_HAPPY9,
955 [10] = BTN_TRIGGER_HAPPY10,
956 [11] = BTN_TRIGGER_HAPPY11,
957 [12] = BTN_TRIGGER_HAPPY12,
958 [13] = BTN_TRIGGER_HAPPY13,
959 [14] = BTN_TRIGGER_HAPPY14,
960 [15] = BTN_TRIGGER_HAPPY15,
961 [16] = BTN_TRIGGER_HAPPY16,
962 [17] = BTN_TRIGGER_HAPPY17,
963 [18] = BTN_TRIGGER_HAPPY18,
964 [19] = BTN_TRIGGER_HAPPY19,
965 [20] = BTN_TRIGGER_HAPPY20,
966};
967
968static enum power_supply_property sony_battery_props[] = {
969 POWER_SUPPLY_PROP_PRESENT,
970 POWER_SUPPLY_PROP_CAPACITY,
971 POWER_SUPPLY_PROP_SCOPE,
972 POWER_SUPPLY_PROP_STATUS,
973};
974
975struct sixaxis_led {
976 __u8 time_enabled;
977 __u8 duty_length;
978 __u8 enabled;
979 __u8 duty_off;
980 __u8 duty_on;
981} __packed;
982
983struct sixaxis_rumble {
984 __u8 padding;
985 __u8 right_duration;
986 __u8 right_motor_on;
987 __u8 left_duration;
988 __u8 left_motor_force;
989} __packed;
990
991struct sixaxis_output_report {
992 __u8 report_id;
993 struct sixaxis_rumble rumble;
994 __u8 padding[4];
995 __u8 leds_bitmap;
996 struct sixaxis_led led[4];
997 struct sixaxis_led _reserved;
998} __packed;
999
1000union sixaxis_output_report_01 {
1001 struct sixaxis_output_report data;
1002 __u8 buf[36];
1003};
1004
1005struct motion_output_report_02 {
1006 u8 type, zero;
1007 u8 r, g, b;
1008 u8 zero2;
1009 u8 rumble;
1010};
1011
1012#define DS4_REPORT_0x02_SIZE 37
1013#define DS4_REPORT_0x05_SIZE 32
1014#define DS4_REPORT_0x11_SIZE 78
1015#define DS4_REPORT_0x81_SIZE 7
1016#define SIXAXIS_REPORT_0xF2_SIZE 17
1017#define SIXAXIS_REPORT_0xF5_SIZE 8
1018#define MOTION_REPORT_0x02_SIZE 49
1019
1020static DEFINE_SPINLOCK(sony_dev_list_lock);
1021static LIST_HEAD(sony_device_list);
1022static DEFINE_IDA(sony_device_id_allocator);
1023
1024struct sony_sc {
1025 spinlock_t lock;
1026 struct list_head list_node;
1027 struct hid_device *hdev;
1028 struct led_classdev *leds[MAX_LEDS];
1029 unsigned long quirks;
1030 struct work_struct state_worker;
1031 void(*send_output_report)(struct sony_sc*);
1032 struct power_supply *battery;
1033 struct power_supply_desc battery_desc;
1034 int device_id;
1035 __u8 *output_report_dmabuf;
1036
1037#ifdef CONFIG_SONY_FF
1038 __u8 left;
1039 __u8 right;
1040#endif
1041
1042 __u8 mac_address[6];
1043 __u8 worker_initialized;
1044 __u8 cable_state;
1045 __u8 battery_charging;
1046 __u8 battery_capacity;
1047 __u8 led_state[MAX_LEDS];
1048 __u8 resume_led_state[MAX_LEDS];
1049 __u8 led_delay_on[MAX_LEDS];
1050 __u8 led_delay_off[MAX_LEDS];
1051 __u8 led_count;
1052};
1053
1054static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
1055 unsigned int *rsize)
1056{
1057 *rsize = sizeof(sixaxis_rdesc);
1058 return sixaxis_rdesc;
1059}
1060
1061static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
1062 unsigned int *rsize)
1063{
1064 *rsize = sizeof(motion_rdesc);
1065 return motion_rdesc;
1066}
1067
1068static u8 *navigation_fixup(struct hid_device *hdev, u8 *rdesc,
1069 unsigned int *rsize)
1070{
1071 *rsize = sizeof(navigation_rdesc);
1072 return navigation_rdesc;
1073}
1074
1075static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
1076 unsigned int *rsize)
1077{
1078 *rsize = sizeof(ps3remote_rdesc);
1079 return ps3remote_rdesc;
1080}
1081
1082static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
1083 struct hid_field *field, struct hid_usage *usage,
1084 unsigned long **bit, int *max)
1085{
1086 unsigned int key = usage->hid & HID_USAGE;
1087
1088 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1089 return -1;
1090
1091 switch (usage->collection_index) {
1092 case 1:
1093 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
1094 return -1;
1095
1096 key = ps3remote_keymap_joypad_buttons[key];
1097 if (!key)
1098 return -1;
1099 break;
1100 case 2:
1101 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
1102 return -1;
1103
1104 key = ps3remote_keymap_remote_buttons[key];
1105 if (!key)
1106 return -1;
1107 break;
1108 default:
1109 return -1;
1110 }
1111
1112 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1113 return 1;
1114}
1115
1116static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1117 unsigned int *rsize)
1118{
1119 struct sony_sc *sc = hid_get_drvdata(hdev);
1120
1121
1122
1123
1124
1125 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1126
1127
1128
1129 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1130
1131 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
1132 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
1133
1134 rdesc[55] = 0x06;
1135 }
1136
1137
1138
1139
1140
1141
1142 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1143 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1144 rdesc = dualshock4_usb_rdesc;
1145 *rsize = sizeof(dualshock4_usb_rdesc);
1146 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
1147 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1148 rdesc = dualshock4_bt_rdesc;
1149 *rsize = sizeof(dualshock4_bt_rdesc);
1150 }
1151
1152 if (sc->quirks & SIXAXIS_CONTROLLER)
1153 return sixaxis_fixup(hdev, rdesc, rsize);
1154
1155 if (sc->quirks & MOTION_CONTROLLER)
1156 return motion_fixup(hdev, rdesc, rsize);
1157
1158 if (sc->quirks & NAVIGATION_CONTROLLER)
1159 return navigation_fixup(hdev, rdesc, rsize);
1160
1161 if (sc->quirks & PS3REMOTE)
1162 return ps3remote_fixup(hdev, rdesc, rsize);
1163
1164 return rdesc;
1165}
1166
1167static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1168{
1169 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
1170 unsigned long flags;
1171 int offset;
1172 __u8 cable_state, battery_capacity, battery_charging;
1173
1174
1175
1176
1177
1178
1179
1180 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
1181
1182 if (rd[offset] >= 0xee) {
1183 battery_capacity = 100;
1184 battery_charging = !(rd[offset] & 0x01);
1185 cable_state = 1;
1186 } else {
1187 __u8 index = rd[offset] <= 5 ? rd[offset] : 5;
1188 battery_capacity = sixaxis_battery_capacity[index];
1189 battery_charging = 0;
1190 cable_state = 0;
1191 }
1192
1193 spin_lock_irqsave(&sc->lock, flags);
1194 sc->cable_state = cable_state;
1195 sc->battery_capacity = battery_capacity;
1196 sc->battery_charging = battery_charging;
1197 spin_unlock_irqrestore(&sc->lock, flags);
1198}
1199
1200static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1201{
1202 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1203 struct hid_input, list);
1204 struct input_dev *input_dev = hidinput->input;
1205 unsigned long flags;
1206 int n, offset;
1207 __u8 cable_state, battery_capacity, battery_charging;
1208
1209
1210
1211
1212
1213 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1214
1215
1216
1217
1218
1219 cable_state = (rd[offset] >> 4) & 0x01;
1220 battery_capacity = rd[offset] & 0x0F;
1221
1222
1223
1224
1225
1226
1227 if (!cable_state || battery_capacity > 10)
1228 battery_charging = 0;
1229 else
1230 battery_charging = 1;
1231
1232 if (!cable_state)
1233 battery_capacity++;
1234 if (battery_capacity > 10)
1235 battery_capacity = 10;
1236
1237 battery_capacity *= 10;
1238
1239 spin_lock_irqsave(&sc->lock, flags);
1240 sc->cable_state = cable_state;
1241 sc->battery_capacity = battery_capacity;
1242 sc->battery_charging = battery_charging;
1243 spin_unlock_irqrestore(&sc->lock, flags);
1244
1245 offset += 5;
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256 for (n = 0; n < 2; n++) {
1257 __u16 x, y;
1258
1259 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1260 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1261
1262 input_mt_slot(input_dev, n);
1263 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1264 !(rd[offset] >> 7));
1265 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1266 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1267
1268 offset += 4;
1269 }
1270}
1271
1272static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1273 __u8 *rd, int size)
1274{
1275 struct sony_sc *sc = hid_get_drvdata(hdev);
1276
1277
1278
1279
1280
1281 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
1282
1283
1284
1285
1286
1287
1288
1289
1290 if (rd[1] == 0xff)
1291 return -EINVAL;
1292
1293 swap(rd[41], rd[42]);
1294 swap(rd[43], rd[44]);
1295 swap(rd[45], rd[46]);
1296 swap(rd[47], rd[48]);
1297
1298 sixaxis_parse_report(sc, rd, size);
1299 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1300 sixaxis_parse_report(sc, rd, size);
1301 } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1302 size == 49) {
1303 sixaxis_parse_report(sc, rd, size);
1304 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1305 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1306 && rd[0] == 0x11 && size == 78)) {
1307 dualshock4_parse_report(sc, rd, size);
1308 }
1309
1310 return 0;
1311}
1312
1313static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1314 struct hid_field *field, struct hid_usage *usage,
1315 unsigned long **bit, int *max)
1316{
1317 struct sony_sc *sc = hid_get_drvdata(hdev);
1318
1319 if (sc->quirks & BUZZ_CONTROLLER) {
1320 unsigned int key = usage->hid & HID_USAGE;
1321
1322 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1323 return -1;
1324
1325 switch (usage->collection_index) {
1326 case 1:
1327 if (key >= ARRAY_SIZE(buzz_keymap))
1328 return -1;
1329
1330 key = buzz_keymap[key];
1331 if (!key)
1332 return -1;
1333 break;
1334 default:
1335 return -1;
1336 }
1337
1338 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1339 return 1;
1340 }
1341
1342 if (sc->quirks & PS3REMOTE)
1343 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1344
1345
1346 return 0;
1347}
1348
1349static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1350 int w, int h)
1351{
1352 struct input_dev *input_dev = hi->input;
1353 int ret;
1354
1355 ret = input_mt_init_slots(input_dev, touch_count, 0);
1356 if (ret < 0)
1357 return ret;
1358
1359 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1360 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1361
1362 return 0;
1363}
1364
1365static int sony_input_configured(struct hid_device *hdev,
1366 struct hid_input *hidinput)
1367{
1368 struct sony_sc *sc = hid_get_drvdata(hdev);
1369 int ret;
1370
1371
1372
1373
1374
1375 if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1376 ret = sony_register_touchpad(hidinput, 2, 1920, 942);
1377 if (ret) {
1378 hid_err(sc->hdev,
1379 "Unable to initialize multi-touch slots: %d\n",
1380 ret);
1381 return ret;
1382 }
1383 }
1384
1385 return 0;
1386}
1387
1388
1389
1390
1391
1392
1393static int sixaxis_set_operational_usb(struct hid_device *hdev)
1394{
1395 const int buf_size =
1396 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
1397 __u8 *buf;
1398 int ret;
1399
1400 buf = kmalloc(buf_size, GFP_KERNEL);
1401 if (!buf)
1402 return -ENOMEM;
1403
1404 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1405 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1406 if (ret < 0) {
1407 hid_err(hdev, "can't set operational mode: step 1\n");
1408 goto out;
1409 }
1410
1411
1412
1413
1414
1415 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1416 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1417 if (ret < 0) {
1418 hid_err(hdev, "can't set operational mode: step 2\n");
1419 goto out;
1420 }
1421
1422 ret = hid_hw_output_report(hdev, buf, 1);
1423 if (ret < 0)
1424 hid_err(hdev, "can't set operational mode: step 3\n");
1425
1426out:
1427 kfree(buf);
1428
1429 return ret;
1430}
1431
1432static int sixaxis_set_operational_bt(struct hid_device *hdev)
1433{
1434 static const __u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1435 __u8 *buf;
1436 int ret;
1437
1438 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1439 if (!buf)
1440 return -ENOMEM;
1441
1442 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
1443 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
1444
1445 kfree(buf);
1446
1447 return ret;
1448}
1449
1450
1451
1452
1453
1454static int dualshock4_set_operational_bt(struct hid_device *hdev)
1455{
1456 __u8 *buf;
1457 int ret;
1458
1459 buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
1460 if (!buf)
1461 return -ENOMEM;
1462
1463 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
1464 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1465
1466 kfree(buf);
1467
1468 return ret;
1469}
1470
1471static void sixaxis_set_leds_from_id(struct sony_sc *sc)
1472{
1473 static const __u8 sixaxis_leds[10][4] = {
1474 { 0x01, 0x00, 0x00, 0x00 },
1475 { 0x00, 0x01, 0x00, 0x00 },
1476 { 0x00, 0x00, 0x01, 0x00 },
1477 { 0x00, 0x00, 0x00, 0x01 },
1478 { 0x01, 0x00, 0x00, 0x01 },
1479 { 0x00, 0x01, 0x00, 0x01 },
1480 { 0x00, 0x00, 0x01, 0x01 },
1481 { 0x01, 0x00, 0x01, 0x01 },
1482 { 0x00, 0x01, 0x01, 0x01 },
1483 { 0x01, 0x01, 0x01, 0x01 }
1484 };
1485
1486 int id = sc->device_id;
1487
1488 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
1489
1490 if (id < 0)
1491 return;
1492
1493 id %= 10;
1494 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
1495}
1496
1497static void dualshock4_set_leds_from_id(struct sony_sc *sc)
1498{
1499
1500 static const __u8 color_code[7][3] = {
1501 { 0x00, 0x00, 0x01 },
1502 { 0x01, 0x00, 0x00 },
1503 { 0x00, 0x01, 0x00 },
1504 { 0x02, 0x00, 0x01 },
1505 { 0x02, 0x01, 0x00 },
1506 { 0x00, 0x01, 0x01 },
1507 { 0x01, 0x01, 0x01 }
1508 };
1509
1510 int id = sc->device_id;
1511
1512 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
1513
1514 if (id < 0)
1515 return;
1516
1517 id %= 7;
1518 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
1519}
1520
1521static void buzz_set_leds(struct sony_sc *sc)
1522{
1523 struct hid_device *hdev = sc->hdev;
1524 struct list_head *report_list =
1525 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1526 struct hid_report *report = list_entry(report_list->next,
1527 struct hid_report, list);
1528 __s32 *value = report->field[0]->value;
1529
1530 BUILD_BUG_ON(MAX_LEDS < 4);
1531
1532 value[0] = 0x00;
1533 value[1] = sc->led_state[0] ? 0xff : 0x00;
1534 value[2] = sc->led_state[1] ? 0xff : 0x00;
1535 value[3] = sc->led_state[2] ? 0xff : 0x00;
1536 value[4] = sc->led_state[3] ? 0xff : 0x00;
1537 value[5] = 0x00;
1538 value[6] = 0x00;
1539 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1540}
1541
1542static void sony_set_leds(struct sony_sc *sc)
1543{
1544 if (!(sc->quirks & BUZZ_CONTROLLER))
1545 schedule_work(&sc->state_worker);
1546 else
1547 buzz_set_leds(sc);
1548}
1549
1550static void sony_led_set_brightness(struct led_classdev *led,
1551 enum led_brightness value)
1552{
1553 struct device *dev = led->dev->parent;
1554 struct hid_device *hdev = to_hid_device(dev);
1555 struct sony_sc *drv_data;
1556
1557 int n;
1558 int force_update;
1559
1560 drv_data = hid_get_drvdata(hdev);
1561 if (!drv_data) {
1562 hid_err(hdev, "No device data\n");
1563 return;
1564 }
1565
1566
1567
1568
1569
1570
1571
1572
1573 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1574
1575 for (n = 0; n < drv_data->led_count; n++) {
1576 if (led == drv_data->leds[n] && (force_update ||
1577 (value != drv_data->led_state[n] ||
1578 drv_data->led_delay_on[n] ||
1579 drv_data->led_delay_off[n]))) {
1580
1581 drv_data->led_state[n] = value;
1582
1583
1584 drv_data->led_delay_on[n] = 0;
1585 drv_data->led_delay_off[n] = 0;
1586
1587 sony_set_leds(drv_data);
1588 break;
1589 }
1590 }
1591}
1592
1593static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
1594{
1595 struct device *dev = led->dev->parent;
1596 struct hid_device *hdev = to_hid_device(dev);
1597 struct sony_sc *drv_data;
1598
1599 int n;
1600
1601 drv_data = hid_get_drvdata(hdev);
1602 if (!drv_data) {
1603 hid_err(hdev, "No device data\n");
1604 return LED_OFF;
1605 }
1606
1607 for (n = 0; n < drv_data->led_count; n++) {
1608 if (led == drv_data->leds[n])
1609 return drv_data->led_state[n];
1610 }
1611
1612 return LED_OFF;
1613}
1614
1615static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1616 unsigned long *delay_off)
1617{
1618 struct device *dev = led->dev->parent;
1619 struct hid_device *hdev = to_hid_device(dev);
1620 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1621 int n;
1622 __u8 new_on, new_off;
1623
1624 if (!drv_data) {
1625 hid_err(hdev, "No device data\n");
1626 return -EINVAL;
1627 }
1628
1629
1630 if (*delay_on > 2550)
1631 *delay_on = 2550;
1632 if (*delay_off > 2550)
1633 *delay_off = 2550;
1634
1635
1636 if (!*delay_on && !*delay_off)
1637 *delay_on = *delay_off = 500;
1638
1639 new_on = *delay_on / 10;
1640 new_off = *delay_off / 10;
1641
1642 for (n = 0; n < drv_data->led_count; n++) {
1643 if (led == drv_data->leds[n])
1644 break;
1645 }
1646
1647
1648 if (n >= drv_data->led_count)
1649 return -EINVAL;
1650
1651
1652 if (new_on != drv_data->led_delay_on[n] ||
1653 new_off != drv_data->led_delay_off[n]) {
1654 drv_data->led_delay_on[n] = new_on;
1655 drv_data->led_delay_off[n] = new_off;
1656 schedule_work(&drv_data->state_worker);
1657 }
1658
1659 return 0;
1660}
1661
1662static void sony_leds_remove(struct sony_sc *sc)
1663{
1664 struct led_classdev *led;
1665 int n;
1666
1667 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
1668
1669 for (n = 0; n < sc->led_count; n++) {
1670 led = sc->leds[n];
1671 sc->leds[n] = NULL;
1672 if (!led)
1673 continue;
1674 led_classdev_unregister(led);
1675 kfree(led);
1676 }
1677
1678 sc->led_count = 0;
1679}
1680
1681static int sony_leds_init(struct sony_sc *sc)
1682{
1683 struct hid_device *hdev = sc->hdev;
1684 int n, ret = 0;
1685 int use_ds4_names;
1686 struct led_classdev *led;
1687 size_t name_sz;
1688 char *name;
1689 size_t name_len;
1690 const char *name_fmt;
1691 static const char * const ds4_name_str[] = { "red", "green", "blue",
1692 "global" };
1693 __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1694 __u8 use_hw_blink[MAX_LEDS] = { 0 };
1695
1696 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
1697
1698 if (sc->quirks & BUZZ_CONTROLLER) {
1699 sc->led_count = 4;
1700 use_ds4_names = 0;
1701 name_len = strlen("::buzz#");
1702 name_fmt = "%s::buzz%d";
1703
1704 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1705 return -ENODEV;
1706 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1707 dualshock4_set_leds_from_id(sc);
1708 sc->led_state[3] = 1;
1709 sc->led_count = 4;
1710 memset(max_brightness, 255, 3);
1711 use_hw_blink[3] = 1;
1712 use_ds4_names = 1;
1713 name_len = 0;
1714 name_fmt = "%s:%s";
1715 } else if (sc->quirks & MOTION_CONTROLLER) {
1716 sc->led_count = 3;
1717 memset(max_brightness, 255, 3);
1718 use_ds4_names = 1;
1719 name_len = 0;
1720 name_fmt = "%s:%s";
1721 } else if (sc->quirks & NAVIGATION_CONTROLLER) {
1722 static const __u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
1723
1724 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1725 sc->led_count = 1;
1726 memset(use_hw_blink, 1, 4);
1727 use_ds4_names = 0;
1728 name_len = strlen("::sony#");
1729 name_fmt = "%s::sony%d";
1730 } else {
1731 sixaxis_set_leds_from_id(sc);
1732 sc->led_count = 4;
1733 memset(use_hw_blink, 1, 4);
1734 use_ds4_names = 0;
1735 name_len = strlen("::sony#");
1736 name_fmt = "%s::sony%d";
1737 }
1738
1739
1740
1741
1742
1743
1744 sony_set_leds(sc);
1745
1746 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
1747
1748 for (n = 0; n < sc->led_count; n++) {
1749
1750 if (use_ds4_names)
1751 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
1752
1753 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1754 if (!led) {
1755 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
1756 ret = -ENOMEM;
1757 goto error_leds;
1758 }
1759
1760 name = (void *)(&led[1]);
1761 if (use_ds4_names)
1762 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1763 ds4_name_str[n]);
1764 else
1765 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
1766 led->name = name;
1767 led->brightness = sc->led_state[n];
1768 led->max_brightness = max_brightness[n];
1769 led->brightness_get = sony_led_get_brightness;
1770 led->brightness_set = sony_led_set_brightness;
1771
1772 if (use_hw_blink[n])
1773 led->blink_set = sony_led_blink_set;
1774
1775 sc->leds[n] = led;
1776
1777 ret = led_classdev_register(&hdev->dev, led);
1778 if (ret) {
1779 hid_err(hdev, "Failed to register LED %d\n", n);
1780 sc->leds[n] = NULL;
1781 kfree(led);
1782 goto error_leds;
1783 }
1784 }
1785
1786 return ret;
1787
1788error_leds:
1789 sony_leds_remove(sc);
1790
1791 return ret;
1792}
1793
1794static void sixaxis_send_output_report(struct sony_sc *sc)
1795{
1796 static const union sixaxis_output_report_01 default_report = {
1797 .buf = {
1798 0x01,
1799 0x00, 0xff, 0x00, 0xff, 0x00,
1800 0x00, 0x00, 0x00, 0x00, 0x00,
1801 0xff, 0x27, 0x10, 0x00, 0x32,
1802 0xff, 0x27, 0x10, 0x00, 0x32,
1803 0xff, 0x27, 0x10, 0x00, 0x32,
1804 0xff, 0x27, 0x10, 0x00, 0x32,
1805 0x00, 0x00, 0x00, 0x00, 0x00
1806 }
1807 };
1808 struct sixaxis_output_report *report =
1809 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1810 int n;
1811
1812
1813 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
1814
1815#ifdef CONFIG_SONY_FF
1816 report->rumble.right_motor_on = sc->right ? 1 : 0;
1817 report->rumble.left_motor_force = sc->left;
1818#endif
1819
1820 report->leds_bitmap |= sc->led_state[0] << 1;
1821 report->leds_bitmap |= sc->led_state[1] << 2;
1822 report->leds_bitmap |= sc->led_state[2] << 3;
1823 report->leds_bitmap |= sc->led_state[3] << 4;
1824
1825
1826 if ((report->leds_bitmap & 0x1E) == 0)
1827 report->leds_bitmap |= 0x20;
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838 for (n = 0; n < 4; n++) {
1839 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
1840 report->led[3 - n].duty_off = sc->led_delay_off[n];
1841 report->led[3 - n].duty_on = sc->led_delay_on[n];
1842 }
1843 }
1844
1845 hid_hw_raw_request(sc->hdev, report->report_id, (__u8 *)report,
1846 sizeof(struct sixaxis_output_report),
1847 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
1848}
1849
1850static void dualshock4_send_output_report(struct sony_sc *sc)
1851{
1852 struct hid_device *hdev = sc->hdev;
1853 __u8 *buf = sc->output_report_dmabuf;
1854 int offset;
1855
1856 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1857 memset(buf, 0, DS4_REPORT_0x05_SIZE);
1858 buf[0] = 0x05;
1859 buf[1] = 0xFF;
1860 offset = 4;
1861 } else {
1862 memset(buf, 0, DS4_REPORT_0x11_SIZE);
1863 buf[0] = 0x11;
1864 buf[1] = 0x80;
1865 buf[3] = 0x0F;
1866 offset = 6;
1867 }
1868
1869#ifdef CONFIG_SONY_FF
1870 buf[offset++] = sc->right;
1871 buf[offset++] = sc->left;
1872#else
1873 offset += 2;
1874#endif
1875
1876
1877 if (sc->led_state[3]) {
1878 buf[offset++] = sc->led_state[0];
1879 buf[offset++] = sc->led_state[1];
1880 buf[offset++] = sc->led_state[2];
1881 } else {
1882 offset += 3;
1883 }
1884
1885
1886 buf[offset++] = sc->led_delay_on[3];
1887 buf[offset++] = sc->led_delay_off[3];
1888
1889 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1890 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
1891 else
1892 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
1893 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
1894}
1895
1896static void motion_send_output_report(struct sony_sc *sc)
1897{
1898 struct hid_device *hdev = sc->hdev;
1899 struct motion_output_report_02 *report =
1900 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1901
1902 memset(report, 0, MOTION_REPORT_0x02_SIZE);
1903
1904 report->type = 0x02;
1905 report->r = sc->led_state[0];
1906 report->g = sc->led_state[1];
1907 report->b = sc->led_state[2];
1908
1909#ifdef CONFIG_SONY_FF
1910 report->rumble = max(sc->right, sc->left);
1911#endif
1912
1913 hid_hw_output_report(hdev, (__u8 *)report, MOTION_REPORT_0x02_SIZE);
1914}
1915
1916static inline void sony_send_output_report(struct sony_sc *sc)
1917{
1918 if (sc->send_output_report)
1919 sc->send_output_report(sc);
1920}
1921
1922static void sony_state_worker(struct work_struct *work)
1923{
1924 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1925 sc->send_output_report(sc);
1926}
1927
1928static int sony_allocate_output_report(struct sony_sc *sc)
1929{
1930 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1931 (sc->quirks & NAVIGATION_CONTROLLER))
1932 sc->output_report_dmabuf =
1933 kmalloc(sizeof(union sixaxis_output_report_01),
1934 GFP_KERNEL);
1935 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1936 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1937 GFP_KERNEL);
1938 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1939 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1940 GFP_KERNEL);
1941 else if (sc->quirks & MOTION_CONTROLLER)
1942 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
1943 GFP_KERNEL);
1944 else
1945 return 0;
1946
1947 if (!sc->output_report_dmabuf)
1948 return -ENOMEM;
1949
1950 return 0;
1951}
1952
1953#ifdef CONFIG_SONY_FF
1954static int sony_play_effect(struct input_dev *dev, void *data,
1955 struct ff_effect *effect)
1956{
1957 struct hid_device *hid = input_get_drvdata(dev);
1958 struct sony_sc *sc = hid_get_drvdata(hid);
1959
1960 if (effect->type != FF_RUMBLE)
1961 return 0;
1962
1963 sc->left = effect->u.rumble.strong_magnitude / 256;
1964 sc->right = effect->u.rumble.weak_magnitude / 256;
1965
1966 schedule_work(&sc->state_worker);
1967 return 0;
1968}
1969
1970static int sony_init_ff(struct sony_sc *sc)
1971{
1972 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1973 struct hid_input, list);
1974 struct input_dev *input_dev = hidinput->input;
1975
1976 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1977 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1978}
1979
1980#else
1981static int sony_init_ff(struct sony_sc *sc)
1982{
1983 return 0;
1984}
1985
1986#endif
1987
1988static int sony_battery_get_property(struct power_supply *psy,
1989 enum power_supply_property psp,
1990 union power_supply_propval *val)
1991{
1992 struct sony_sc *sc = power_supply_get_drvdata(psy);
1993 unsigned long flags;
1994 int ret = 0;
1995 u8 battery_charging, battery_capacity, cable_state;
1996
1997 spin_lock_irqsave(&sc->lock, flags);
1998 battery_charging = sc->battery_charging;
1999 battery_capacity = sc->battery_capacity;
2000 cable_state = sc->cable_state;
2001 spin_unlock_irqrestore(&sc->lock, flags);
2002
2003 switch (psp) {
2004 case POWER_SUPPLY_PROP_PRESENT:
2005 val->intval = 1;
2006 break;
2007 case POWER_SUPPLY_PROP_SCOPE:
2008 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
2009 break;
2010 case POWER_SUPPLY_PROP_CAPACITY:
2011 val->intval = battery_capacity;
2012 break;
2013 case POWER_SUPPLY_PROP_STATUS:
2014 if (battery_charging)
2015 val->intval = POWER_SUPPLY_STATUS_CHARGING;
2016 else
2017 if (battery_capacity == 100 && cable_state)
2018 val->intval = POWER_SUPPLY_STATUS_FULL;
2019 else
2020 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2021 break;
2022 default:
2023 ret = -EINVAL;
2024 break;
2025 }
2026 return ret;
2027}
2028
2029static int sony_battery_probe(struct sony_sc *sc)
2030{
2031 struct power_supply_config psy_cfg = { .drv_data = sc, };
2032 struct hid_device *hdev = sc->hdev;
2033 int ret;
2034
2035
2036
2037
2038
2039 sc->battery_capacity = 100;
2040
2041 sc->battery_desc.properties = sony_battery_props;
2042 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2043 sc->battery_desc.get_property = sony_battery_get_property;
2044 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2045 sc->battery_desc.use_for_apm = 0;
2046 sc->battery_desc.name = kasprintf(GFP_KERNEL,
2047 "sony_controller_battery_%pMR",
2048 sc->mac_address);
2049 if (!sc->battery_desc.name)
2050 return -ENOMEM;
2051
2052 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2053 &psy_cfg);
2054 if (IS_ERR(sc->battery)) {
2055 ret = PTR_ERR(sc->battery);
2056 hid_err(hdev, "Unable to register battery device\n");
2057 goto err_free;
2058 }
2059
2060 power_supply_powers(sc->battery, &hdev->dev);
2061 return 0;
2062
2063err_free:
2064 kfree(sc->battery_desc.name);
2065 sc->battery_desc.name = NULL;
2066 return ret;
2067}
2068
2069static void sony_battery_remove(struct sony_sc *sc)
2070{
2071 if (!sc->battery_desc.name)
2072 return;
2073
2074 power_supply_unregister(sc->battery);
2075 kfree(sc->battery_desc.name);
2076 sc->battery_desc.name = NULL;
2077}
2078
2079
2080
2081
2082
2083
2084
2085static int sony_check_add_dev_list(struct sony_sc *sc)
2086{
2087 struct sony_sc *entry;
2088 unsigned long flags;
2089 int ret;
2090
2091 spin_lock_irqsave(&sony_dev_list_lock, flags);
2092
2093 list_for_each_entry(entry, &sony_device_list, list_node) {
2094 ret = memcmp(sc->mac_address, entry->mac_address,
2095 sizeof(sc->mac_address));
2096 if (!ret) {
2097 ret = -EEXIST;
2098 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
2099 sc->mac_address);
2100 goto unlock;
2101 }
2102 }
2103
2104 ret = 0;
2105 list_add(&(sc->list_node), &sony_device_list);
2106
2107unlock:
2108 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2109 return ret;
2110}
2111
2112static void sony_remove_dev_list(struct sony_sc *sc)
2113{
2114 unsigned long flags;
2115
2116 if (sc->list_node.next) {
2117 spin_lock_irqsave(&sony_dev_list_lock, flags);
2118 list_del(&(sc->list_node));
2119 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2120 }
2121}
2122
2123static int sony_get_bt_devaddr(struct sony_sc *sc)
2124{
2125 int ret;
2126
2127
2128 ret = strlen(sc->hdev->uniq);
2129 if (ret != 17)
2130 return -EINVAL;
2131
2132 ret = sscanf(sc->hdev->uniq,
2133 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2134 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2135 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2136
2137 if (ret != 6)
2138 return -EINVAL;
2139
2140 return 0;
2141}
2142
2143static int sony_check_add(struct sony_sc *sc)
2144{
2145 __u8 *buf = NULL;
2146 int n, ret;
2147
2148 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
2149 (sc->quirks & MOTION_CONTROLLER_BT) ||
2150 (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
2151 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2152
2153
2154
2155
2156
2157
2158 if (sony_get_bt_devaddr(sc) < 0) {
2159 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2160 return 0;
2161 }
2162 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
2163 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
2164 if (!buf)
2165 return -ENOMEM;
2166
2167
2168
2169
2170
2171
2172 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2173 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
2174 HID_REQ_GET_REPORT);
2175
2176 if (ret != DS4_REPORT_0x81_SIZE) {
2177 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
2178 ret = ret < 0 ? ret : -EINVAL;
2179 goto out_free;
2180 }
2181
2182 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
2183 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2184 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
2185 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2186 if (!buf)
2187 return -ENOMEM;
2188
2189
2190
2191
2192
2193
2194 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2195 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2196 HID_REQ_GET_REPORT);
2197
2198 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
2199 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
2200 ret = ret < 0 ? ret : -EINVAL;
2201 goto out_free;
2202 }
2203
2204
2205
2206
2207
2208 for (n = 0; n < 6; n++)
2209 sc->mac_address[5-n] = buf[4+n];
2210 } else {
2211 return 0;
2212 }
2213
2214 ret = sony_check_add_dev_list(sc);
2215
2216out_free:
2217
2218 kfree(buf);
2219
2220 return ret;
2221}
2222
2223static int sony_set_device_id(struct sony_sc *sc)
2224{
2225 int ret;
2226
2227
2228
2229
2230
2231 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2232 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2233 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2234 GFP_KERNEL);
2235 if (ret < 0) {
2236 sc->device_id = -1;
2237 return ret;
2238 }
2239 sc->device_id = ret;
2240 } else {
2241 sc->device_id = -1;
2242 }
2243
2244 return 0;
2245}
2246
2247static void sony_release_device_id(struct sony_sc *sc)
2248{
2249 if (sc->device_id >= 0) {
2250 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2251 sc->device_id = -1;
2252 }
2253}
2254
2255static inline void sony_init_output_report(struct sony_sc *sc,
2256 void(*send_output_report)(struct sony_sc*))
2257{
2258 sc->send_output_report = send_output_report;
2259
2260 if (!sc->worker_initialized)
2261 INIT_WORK(&sc->state_worker, sony_state_worker);
2262
2263 sc->worker_initialized = 1;
2264}
2265
2266static inline void sony_cancel_work_sync(struct sony_sc *sc)
2267{
2268 if (sc->worker_initialized)
2269 cancel_work_sync(&sc->state_worker);
2270}
2271
2272static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2273{
2274 int ret;
2275 unsigned long quirks = id->driver_data;
2276 struct sony_sc *sc;
2277 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2278
2279 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
2280 if (sc == NULL) {
2281 hid_err(hdev, "can't alloc sony descriptor\n");
2282 return -ENOMEM;
2283 }
2284
2285 spin_lock_init(&sc->lock);
2286
2287 sc->quirks = quirks;
2288 hid_set_drvdata(hdev, sc);
2289 sc->hdev = hdev;
2290
2291 ret = hid_parse(hdev);
2292 if (ret) {
2293 hid_err(hdev, "parse failed\n");
2294 return ret;
2295 }
2296
2297 if (sc->quirks & VAIO_RDESC_CONSTANT)
2298 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2299 else if (sc->quirks & SIXAXIS_CONTROLLER)
2300 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2301
2302 ret = hid_hw_start(hdev, connect_mask);
2303 if (ret) {
2304 hid_err(hdev, "hw start failed\n");
2305 return ret;
2306 }
2307
2308 ret = sony_set_device_id(sc);
2309 if (ret < 0) {
2310 hid_err(hdev, "failed to allocate the device id\n");
2311 goto err_stop;
2312 }
2313
2314 ret = sony_allocate_output_report(sc);
2315 if (ret < 0) {
2316 hid_err(hdev, "failed to allocate the output report buffer\n");
2317 goto err_stop;
2318 }
2319
2320 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2321 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2333 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
2334 ret = sixaxis_set_operational_usb(hdev);
2335 sony_init_output_report(sc, sixaxis_send_output_report);
2336 } else if ((sc->quirks & SIXAXIS_CONTROLLER_BT) ||
2337 (sc->quirks & NAVIGATION_CONTROLLER_BT)) {
2338
2339
2340
2341
2342 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2343 ret = sixaxis_set_operational_bt(hdev);
2344 sony_init_output_report(sc, sixaxis_send_output_report);
2345 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
2346 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
2347
2348
2349
2350
2351 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2352 ret = dualshock4_set_operational_bt(hdev);
2353 if (ret < 0) {
2354 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2355 goto err_stop;
2356 }
2357 }
2358
2359 sony_init_output_report(sc, dualshock4_send_output_report);
2360 } else if (sc->quirks & MOTION_CONTROLLER) {
2361 sony_init_output_report(sc, motion_send_output_report);
2362 } else {
2363 ret = 0;
2364 }
2365
2366 if (ret < 0)
2367 goto err_stop;
2368
2369 ret = sony_check_add(sc);
2370 if (ret < 0)
2371 goto err_stop;
2372
2373 if (sc->quirks & SONY_LED_SUPPORT) {
2374 ret = sony_leds_init(sc);
2375 if (ret < 0)
2376 goto err_stop;
2377 }
2378
2379 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2380 ret = sony_battery_probe(sc);
2381 if (ret < 0)
2382 goto err_stop;
2383
2384
2385 ret = hid_hw_open(hdev);
2386 if (ret < 0) {
2387 hid_err(hdev, "hw open failed\n");
2388 goto err_stop;
2389 }
2390 }
2391
2392 if (sc->quirks & SONY_FF_SUPPORT) {
2393 ret = sony_init_ff(sc);
2394 if (ret < 0)
2395 goto err_close;
2396 }
2397
2398 return 0;
2399err_close:
2400 hid_hw_close(hdev);
2401err_stop:
2402 if (sc->quirks & SONY_LED_SUPPORT)
2403 sony_leds_remove(sc);
2404 if (sc->quirks & SONY_BATTERY_SUPPORT)
2405 sony_battery_remove(sc);
2406 sony_cancel_work_sync(sc);
2407 kfree(sc->output_report_dmabuf);
2408 sony_remove_dev_list(sc);
2409 sony_release_device_id(sc);
2410 hid_hw_stop(hdev);
2411 return ret;
2412}
2413
2414static void sony_remove(struct hid_device *hdev)
2415{
2416 struct sony_sc *sc = hid_get_drvdata(hdev);
2417
2418 if (sc->quirks & SONY_LED_SUPPORT)
2419 sony_leds_remove(sc);
2420
2421 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2422 hid_hw_close(hdev);
2423 sony_battery_remove(sc);
2424 }
2425
2426 sony_cancel_work_sync(sc);
2427
2428 kfree(sc->output_report_dmabuf);
2429
2430 sony_remove_dev_list(sc);
2431
2432 sony_release_device_id(sc);
2433
2434 hid_hw_stop(hdev);
2435}
2436
2437#ifdef CONFIG_PM
2438
2439static int sony_suspend(struct hid_device *hdev, pm_message_t message)
2440{
2441
2442
2443
2444
2445 if (SONY_LED_SUPPORT || SONY_FF_SUPPORT) {
2446 struct sony_sc *sc = hid_get_drvdata(hdev);
2447
2448#ifdef CONFIG_SONY_FF
2449 sc->left = sc->right = 0;
2450#endif
2451
2452 memcpy(sc->resume_led_state, sc->led_state,
2453 sizeof(sc->resume_led_state));
2454 memset(sc->led_state, 0, sizeof(sc->led_state));
2455
2456 sony_send_output_report(sc);
2457 }
2458
2459 return 0;
2460}
2461
2462static int sony_resume(struct hid_device *hdev)
2463{
2464
2465 if (SONY_LED_SUPPORT) {
2466 struct sony_sc *sc = hid_get_drvdata(hdev);
2467
2468 memcpy(sc->led_state, sc->resume_led_state,
2469 sizeof(sc->led_state));
2470
2471
2472
2473
2474
2475 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2476 (sc->quirks & NAVIGATION_CONTROLLER_USB))
2477 sixaxis_set_operational_usb(sc->hdev);
2478
2479 sony_set_leds(sc);
2480 }
2481
2482 return 0;
2483}
2484
2485#endif
2486
2487static const struct hid_device_id sony_devices[] = {
2488 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2489 .driver_data = SIXAXIS_CONTROLLER_USB },
2490 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2491 .driver_data = NAVIGATION_CONTROLLER_USB },
2492 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2493 .driver_data = NAVIGATION_CONTROLLER_BT },
2494 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
2495 .driver_data = MOTION_CONTROLLER_USB },
2496 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
2497 .driver_data = MOTION_CONTROLLER_BT },
2498 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2499 .driver_data = SIXAXIS_CONTROLLER_BT },
2500 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2501 .driver_data = VAIO_RDESC_CONSTANT },
2502 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2503 .driver_data = VAIO_RDESC_CONSTANT },
2504
2505
2506 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2507 .driver_data = BUZZ_CONTROLLER },
2508 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2509 .driver_data = BUZZ_CONTROLLER },
2510
2511 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2512 .driver_data = PS3REMOTE },
2513
2514 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2515 .driver_data = PS3REMOTE },
2516
2517 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2518 .driver_data = PS3REMOTE },
2519
2520 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
2521 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2522 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
2523 .driver_data = DUALSHOCK4_CONTROLLER_BT },
2524 { }
2525};
2526MODULE_DEVICE_TABLE(hid, sony_devices);
2527
2528static struct hid_driver sony_driver = {
2529 .name = "sony",
2530 .id_table = sony_devices,
2531 .input_mapping = sony_mapping,
2532 .input_configured = sony_input_configured,
2533 .probe = sony_probe,
2534 .remove = sony_remove,
2535 .report_fixup = sony_report_fixup,
2536 .raw_event = sony_raw_event,
2537
2538#ifdef CONFIG_PM
2539 .suspend = sony_suspend,
2540 .resume = sony_resume,
2541 .reset_resume = sony_resume,
2542#endif
2543};
2544
2545static int __init sony_init(void)
2546{
2547 dbg_hid("Sony:%s\n", __func__);
2548
2549 return hid_register_driver(&sony_driver);
2550}
2551
2552static void __exit sony_exit(void)
2553{
2554 dbg_hid("Sony:%s\n", __func__);
2555
2556 hid_unregister_driver(&sony_driver);
2557 ida_destroy(&sony_device_id_allocator);
2558}
2559module_init(sony_init);
2560module_exit(sony_exit);
2561
2562MODULE_LICENSE("GPL");
2563