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