-
Notifications
You must be signed in to change notification settings - Fork 111
/
hid-xpadneo.c
1380 lines (1125 loc) · 37.8 KB
/
hid-xpadneo.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define DRV_VER "@DO_NOT_CHANGE@"
/*
* Force feedback support for XBOX ONE S and X gamepads via Bluetooth
*
* This driver was developed for a student project at fortiss GmbH in Munich.
* Copyright (c) 2017 Florian Dollinger <dollinger.florian@gmx.de>
*/
#include <linux/hid.h>
#include <linux/power_supply.h>
#include <linux/input.h> /* ff_memless(), ... */
#include <linux/module.h> /* MODULE_*, module_*, ... */
#include <linux/slab.h> /* kzalloc(), kfree(), ... */
#include <linux/delay.h> /* mdelay(), ... */
#include "hid-ids.h" /* VENDOR_ID... */
#define DEBUG
/* Module Information */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Florian Dollinger <dollinger.florian@gmx.de>");
MODULE_DESCRIPTION("Linux kernel driver for Xbox ONE S+ gamepads (BT), incl. FF");
MODULE_VERSION(DRV_VER);
/* Module Parameters, located at /sys/module/hid_xpadneo/parameters */
/* NOTE:
* In general it is not guaranteed that a short variable is no more than
* 16 bit long in C, it depends on the computer architecure. But as a kernel
* module parameter it is, since <params.c> does use kstrtou16 for shorts
* since version 3.14
*/
#ifdef DEBUG
static u8 debug_level;
module_param(debug_level, byte, 0644);
MODULE_PARM_DESC(debug_level, "(u8) Debug information level: 0 (none) to 3+ (most verbose).");
#endif
static bool disable_ff;
module_param(disable_ff, bool, 0644);
MODULE_PARM_DESC(disable_ff, "(bool) Disable all force-feedback effects (rumble). 1: disable ff, 0: enable ff.");
static bool combined_z_axis;
module_param(combined_z_axis, bool, 0644);
MODULE_PARM_DESC(combined_z_axis, "(bool) Combine the triggers to form a single axis. 1: combine, 0: do not combine");
static u8 trigger_rumble_damping = 4;
module_param(trigger_rumble_damping, byte, 0644);
MODULE_PARM_DESC(trigger_rumble_damping, "(u8) Damp the trigger: 1 (none) to 2^8+ (max)");
static u16 fake_dev_version = 0x1130;
module_param(fake_dev_version, ushort, 0644);
MODULE_PARM_DESC(fake_dev_version, "(u16) Fake device version # to hide from SDL's mappings. 0x0001-0xFFFF: fake version, others: keep original");
/*
* Debug Printk
*
* Prints a debug message to kernel (dmesg)
* only if both is true, this is a DEBUG version and the
* debug_level-parameter is equal or higher than the level
* specified in hid_dbg_lvl
*/
#define DBG_LVL_NONE 0
#define DBG_LVL_FEW 1
#define DBG_LVL_SOME 2
#define DBG_LVL_ALL 3
#ifdef DEBUG
#define hid_dbg_lvl(lvl, fmt_hdev, fmt_str, ...) \
do { \
if (debug_level >= lvl) \
hid_printk(KERN_DEBUG, pr_fmt(fmt_hdev), \
pr_fmt(fmt_str), ##__VA_ARGS__); \
} while (0)
#define dbg_hex_dump_lvl(lvl, fmt_prefix, data, size) \
do { \
if (debug_level >= lvl) \
print_hex_dump(KERN_DEBUG, pr_fmt(fmt_prefix), \
DUMP_PREFIX_NONE, 32, 1, data, size, false); \
} while (0)
#else
#define hid_dbg_lvl(lvl, fmt_hdev, fmt_str, ...) \
no_printk(KERN_DEBUG pr_fmt(fmt_str), ##__VA_ARGS__)
#define dbg_hex_dump_lvl(lvl, fmt_prefix, data, size) \
no_printk(KERN_DEBUG pr_fmt(fmt_prefix))
#endif
static DEFINE_IDA(xpadneo_device_id_allocator);
/*
* FF Output Report
*
* This is the structure for the rumble output report. For more information
* about this structure please take a look in the hid-report description.
* Please notice that the structs are __packed, therefore there is no "padding"
* between the elements (they behave more like an array).
*
*/
enum {
FF_ENABLE_RIGHT = 0x01,
FF_ENABLE_LEFT = 0x02,
FF_ENABLE_RIGHT_TRIGGER = 0x04,
FF_ENABLE_LEFT_TRIGGER = 0x08,
FF_ENABLE_ALL = 0x0F
};
struct ff_data {
u8 enable_actuators;
u8 magnitude_left_trigger;
u8 magnitude_right_trigger;
u8 magnitude_left;
u8 magnitude_right;
u8 duration;
u8 start_delay;
u8 loop_count;
} __packed;
struct ff_report {
u8 report_id;
struct ff_data ff;
} __packed;
/* static variables are zeroed => empty initialization struct */
static const struct ff_data ff_clear;
/*
* Device Data
*
* We attach information to hdev, which is therefore nearly globally accessible
* via hid_get_drvdata(hdev). It is attached to the hid_device via
* hid_set_drvdata(hdev) at the probing function.
*/
enum report_type {
UNKNOWN,
LINUX,
WINDOWS
};
// TODO: avoid data duplication
const char* report_type_text[] = {
"unknown",
"linux/android",
"windows"
};
struct xpadneo_devdata {
/* mutual exclusion */
spinlock_t lock;
/* unique physical device id (randomly assigned) */
int id;
/* logical device interfaces */
struct hid_device *hdev;
struct input_dev *idev;
struct power_supply *batt;
/* report types */
enum report_type report_descriptor;
enum report_type report_behaviour;
/* battery information */
struct power_supply_desc batt_desc;
u8 ps_online;
u8 ps_present;
u8 ps_capacity_level;
u8 ps_status;
};
/*
* Force Feedback Callback
*
* This function is called by the Input Subsystem.
* The effect data is set in userspace and sent to the driver via ioctl.
*/
static int xpadneo_ff_play(struct input_dev *dev, void *data,
struct ff_effect *effect)
{
/* Q: where is drvdata set to hid_device?
* A: hid_hw_start (called in probe)
* -> hid_connect -> hidinput_connect
* -> hidinput_allocate (sets drvdata to hid_device)
*/
struct ff_report ff_package;
u16 weak, strong, direction, max, max_damped;
u8 mag_right, mag_left;
const int fractions_milli[]
= {1000, 962, 854, 691, 500, 309, 146, 38, 0};
const int proportions_idx_max = 8;
u8 index_left, index_right;
int fraction_TL, fraction_TR;
u8 trigger_rumble_damping_nonzero;
enum {
DIRECTION_DOWN = 0x0000,
DIRECTION_LEFT = 0x4000,
DIRECTION_UP = 0x8000,
DIRECTION_RIGHT = 0xC000,
};
struct hid_device *hdev = input_get_drvdata(dev);
if(disable_ff)
return 0;
if (effect->type != FF_RUMBLE)
return 0;
/* copy data from effect structure at the very beginning */
weak = effect->u.rumble.weak_magnitude;
strong = effect->u.rumble.strong_magnitude;
direction = effect->direction;
hid_dbg_lvl(DBG_LVL_FEW, hdev, "playing effect: strong: %#04x, weak: %#04x, direction: %#04x\n",
strong, weak, direction);
/* calculate the physical magnitudes */
mag_right = (u8)((weak & 0xFF00) >> 8); /* u16 to u8 */
mag_left = (u8)((strong & 0xFF00) >> 8); /* u16 to u8 */
/* get the proportions from a precalculated cosine table
* calculation goes like:
* cosine(a) * 1000 = {1000, 924, 707, 383, 0, -383, -707, -924, -1000}
* fractions_milli(a) = (1000 + (cosine * 1000)) / 2
*/
fraction_TL = 0;
fraction_TR = 0;
if (DIRECTION_LEFT <= direction && direction <= DIRECTION_RIGHT) {
index_left = (direction - DIRECTION_LEFT) >> 12;
index_right = proportions_idx_max - index_left;
fraction_TL = fractions_milli[index_left];
fraction_TR = fractions_milli[index_right];
}
/* we want to keep the rumbling at the triggers below the maximum
* of the weak and strong main rumble
*/
max = mag_right > mag_left ? mag_right : mag_left;
/* the user can change the damping at runtime, hence check the range */
trigger_rumble_damping_nonzero
= trigger_rumble_damping == 0 ? 1 : trigger_rumble_damping;
max_damped = max / trigger_rumble_damping_nonzero;
/* prepare ff package */
ff_package.ff = ff_clear;
ff_package.report_id = 0x03;
ff_package.ff.enable_actuators = FF_ENABLE_ALL;
ff_package.ff.magnitude_right = mag_right;
ff_package.ff.magnitude_left = mag_left;
ff_package.ff.magnitude_right_trigger
= (u8)((max_damped * fraction_TR) / 1000);
ff_package.ff.magnitude_left_trigger
= (u8)((max_damped * fraction_TL) / 1000);
hid_dbg_lvl(DBG_LVL_FEW, hdev,
"max: %#04x, prop_left: %#04x, prop_right: %#04x, left trigger: %#04x, right: %#04x\n",
max, fraction_TL, fraction_TR,
ff_package.ff.magnitude_left_trigger,
ff_package.ff.magnitude_right_trigger);
/* It is up to the Input-Subsystem to start and stop effects as needed.
* All WE need to do is to play the effect at least 32767 ms long.
* Take a look here:
* https://stackoverflow.com/questions/48034091/
* We therefore simply play the effect as long as possible, which is
* 2, 55s * 255 = 650, 25s ~ = 10min
*/
ff_package.ff.duration = 0xFF;
ff_package.ff.loop_count = 0xFF;
hid_hw_output_report(hdev, (u8 *)&ff_package, sizeof(ff_package));
return 0;
}
/*
* Device (Gamepad) Initialization
*/
static int xpadneo_initDevice(struct hid_device *hdev)
{
int error;
struct xpadneo_devdata *xdata = hid_get_drvdata(hdev);
struct input_dev *idev = xdata->idev;
struct ff_report ff_package;
/* TODO: outsource that */
ff_package.ff = ff_clear;
/* 'HELLO' FROM THE OTHER SIDE */
if (!disable_ff) {
ff_package.report_id = 0x03;
ff_package.ff.magnitude_right = 0x80;
ff_package.ff.magnitude_left = 0x40;
ff_package.ff.magnitude_right_trigger = 0x20;
ff_package.ff.magnitude_left_trigger = 0x20;
ff_package.ff.duration = 33;
ff_package.ff.enable_actuators = FF_ENABLE_RIGHT;
hid_hw_output_report(hdev, (u8 *)&ff_package, sizeof(ff_package));
mdelay(330);
ff_package.ff.enable_actuators = FF_ENABLE_LEFT;
hid_hw_output_report(hdev, (u8 *)&ff_package, sizeof(ff_package));
mdelay(330);
ff_package.ff.enable_actuators
= FF_ENABLE_RIGHT_TRIGGER | FF_ENABLE_LEFT_TRIGGER;
hid_hw_output_report(hdev, (u8 *)&ff_package, sizeof(ff_package));
mdelay(330);
}
/* Init Input System for Force Feedback (FF) */
input_set_capability(idev, EV_FF, FF_RUMBLE);
error = input_ff_create_memless(idev, NULL, xpadneo_ff_play);
if (error)
return error;
/*
* Set default values, otherwise tools which depend on the joystick
* subsystem, report arbitrary values until the first real event
* TODO: Is this really necessary?
*/
input_report_abs(idev, ABS_X, 0);
input_report_abs(idev, ABS_Y, 0);
input_report_abs(idev, ABS_Z, 0);
input_report_abs(idev, ABS_RX, 0);
input_report_abs(idev, ABS_RY, 0);
input_report_abs(idev, ABS_RZ, 0);
input_report_key(idev, BTN_A, 0);
input_report_key(idev, BTN_B, 0);
input_report_key(idev, BTN_X, 0);
input_report_key(idev, BTN_Y, 0);
input_report_key(idev, BTN_TR, 0);
input_report_key(idev, BTN_TL, 0);
input_report_key(idev, BTN_THUMBL, 0);
input_report_key(idev, BTN_THUMBR, 0);
input_report_key(idev, BTN_START, 0);
input_report_key(idev, BTN_MODE, 0);
input_report_key(idev, ABS_HAT0X, 0);
input_report_key(idev, ABS_HAT0Y, 0);
input_sync(idev);
/* TODO: - do not hardcode codes and values but
* keep them in the mapping structures
* - maybe initDevice isn't the right place
*/
return 0;
}
/* Callback function which return the available properties to userspace */
static int battery_get_property(struct power_supply *ps,
enum power_supply_property property, union power_supply_propval *val)
{
struct xpadneo_devdata *xdata = power_supply_get_drvdata(ps);
unsigned long flags;
u8 capacity_level, present, online, status;
spin_lock_irqsave(&xdata->lock, flags);
capacity_level = xdata->ps_capacity_level;
present = xdata->ps_present;
online = xdata->ps_online;
status = xdata->ps_status;
spin_unlock_irqrestore(&xdata->lock, flags);
switch (property) {
case POWER_SUPPLY_PROP_MANUFACTURER:
val->strval = "Microsoft";
break;
case POWER_SUPPLY_PROP_MODEL_NAME:
val->strval = "Xbox Wireless Controller";
break;
case POWER_SUPPLY_PROP_PRESENT:
val->intval = present;
break;
case POWER_SUPPLY_PROP_ONLINE:
val->intval = online;
break;
case POWER_SUPPLY_PROP_SCOPE:
val->intval = POWER_SUPPLY_SCOPE_DEVICE;
break;
case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
val->intval = capacity_level;
break;
case POWER_SUPPLY_PROP_STATUS:
val->intval = status;
break;
default:
return -EINVAL;
}
return 0;
}
static int xpadneo_initBatt(struct hid_device *hdev)
{
int ret = 0;
struct xpadneo_devdata *xdata = hid_get_drvdata(hdev);
static enum power_supply_property battery_props[] = {
/* is a power supply available? always true */
POWER_SUPPLY_PROP_PRESENT,
/* critical, low, normal, high, full */
POWER_SUPPLY_PROP_CAPACITY_LEVEL,
/* powers a specific device */
POWER_SUPPLY_PROP_SCOPE,
/* charging (full, plugged), not_charging */
POWER_SUPPLY_PROP_STATUS,
/* cstring - manufacturer name */
POWER_SUPPLY_PROP_MANUFACTURER,
/* cstring - model name */
POWER_SUPPLY_PROP_MODEL_NAME,
POWER_SUPPLY_PROP_ONLINE
};
struct power_supply_config ps_config = {
/* pass the xpadneo_data to the get_property function */
.drv_data = xdata
};
/* Set up power supply */
/* Set the battery capacity to 'full' until we get our first real
* battery event. Prevents false "critical low battery" notifications
*/
xdata->ps_capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
/* NOTE: hdev->uniq is meant to be the MAC address and hence
* it should be unique. Unfortunately, here it is not unique
* neither is it the bluetooth MAC address.
* As a solution we add an unique id for every gamepad.
*/
xdata->batt_desc.name = kasprintf(GFP_KERNEL, "xpadneo_batt_%pMR_%i",
hdev->uniq, xdata->id);
if (!xdata->batt_desc.name)
return -ENOMEM;
xdata->batt_desc.type = POWER_SUPPLY_TYPE_BATTERY;
/* Which properties of the battery are accessible? */
xdata->batt_desc.properties = battery_props;
xdata->batt_desc.num_properties = ARRAY_SIZE(battery_props);
/*
* We have to offer a function which returns the current
* property values we defined above. Make sure that
* the get_property functions covers all properties above.
*/
xdata->batt_desc.get_property = battery_get_property;
/* Advanced power management emulation */
xdata->batt_desc.use_for_apm = 0;
/* Register power supply for our gamepad device */
xdata->batt = devm_power_supply_register(&hdev->dev,
&xdata->batt_desc, &ps_config);
if (IS_ERR(xdata->batt)) {
ret = PTR_ERR(xdata->batt);
hid_err(hdev, "Unable to register battery device\n");
goto err_free;
} else {
hid_dbg_lvl(DBG_LVL_SOME, hdev, "battery registered\n");
}
power_supply_powers(xdata->batt, &hdev->dev);
err_free:
kfree(xdata->batt_desc.name);
xdata->batt_desc.name = NULL;
return ret;
}
enum mapping_behaviour {
MAP_IGNORE, /* Completely ignore this field */
MAP_AUTO, /* Do not really map it, let hid-core decide */
MAP_STATIC /* Map to the values given */
};
struct input_ev {
/* Map to which input event (EV_KEY, EV_ABS, ...)? */
u8 event_type;
/* Map to which input code (BTN_A, ABS_X, ...)? */
u16 input_code;
};
u8 map_hid_to_input_windows(struct hid_usage *usage, struct input_ev *map_to)
{
/*
* Windows report-descriptor (307 byte):
*
* 05 01 09 05 a1 01 85 01 09 01 a1 00 09 30 09 31 15 00 27 ff
* ff 00 00 95 02 75 10 81 02 c0 09 01 a1 00 09 33 09 34 15 00
* 27 ff ff 00 00 95 02 75 10 81 02 c0 05 01 09 32 15 00 26 ff
* 03 95 01 75 0a 81 02 15 00 25 00 75 06 95 01 81 03 05 01 09
* 35 15 00 26 ff 03 95 01 75 0a 81 02 15 00 25 00 75 06 95 01
* 81 03 05 01 09 39 15 01 25 08 35 00 46 3b 01 66 14 00 75 04
* 95 01 81 42 75 04 95 01 15 00 25 00 35 00 45 00 65 00 81 03
* 05 09 19 01 29 0a 15 00 25 01 75 01 95 0a 81 02 15 00 25 00
* 75 06 95 01 81 03 05 01 09 80 85 02 a1 00 09 85 15 00 25 01
* 95 01 75 01 81 02 15 00 25 00 75 07 95 01 81 03 c0 05 0f 09
* 21 85 03 a1 02 09 97 15 00 25 01 75 04 95 01 91 02 15 00 25
* 00 75 04 95 01 91 03 09 70 15 00 25 64 75 08 95 04 91 02 09
* 50 66 01 10 55 0e 15 00 26 ff 00 75 08 95 01 91 02 09 a7 15
* 00 26 ff 00 75 08 95 01 91 02 65 00 55 00 09 7c 15 00 26 ff
* 00 75 08 95 01 91 02 c0 85 04 05 06 09 20 15 00 26 ff 00 75
* 08 95 01 81 02 c0 00
*/
unsigned int hid_usage = usage->hid & HID_USAGE;
unsigned int hid_usage_page = usage->hid & HID_USAGE_PAGE;
switch (hid_usage_page) {
case HID_UP_BUTTON:
switch (hid_usage) {
case 0x01:
*map_to = (struct input_ev){EV_KEY, BTN_A};
return MAP_STATIC;
case 0x02:
*map_to = (struct input_ev){EV_KEY, BTN_B};
return MAP_STATIC;
case 0x03:
*map_to = (struct input_ev){EV_KEY, BTN_X};
return MAP_STATIC;
case 0x04:
*map_to = (struct input_ev){EV_KEY, BTN_Y};
return MAP_STATIC;
case 0x05:
*map_to = (struct input_ev){EV_KEY, BTN_TL};
return MAP_STATIC;
case 0x06:
*map_to = (struct input_ev){EV_KEY, BTN_TR};
return MAP_STATIC;
case 0x07:
*map_to = (struct input_ev){EV_KEY, BTN_SELECT};
return MAP_STATIC;
case 0x08:
*map_to = (struct input_ev){EV_KEY, BTN_START};
return MAP_STATIC;
case 0x09:
*map_to = (struct input_ev){EV_KEY, BTN_THUMBL};
return MAP_STATIC;
case 0x0A:
*map_to = (struct input_ev){EV_KEY, BTN_THUMBR};
return MAP_STATIC;
}
case HID_UP_GENDESK:
switch (hid_usage) {
case 0x30:
*map_to = (struct input_ev){EV_ABS, ABS_X};
return MAP_STATIC;
case 0x31:
*map_to = (struct input_ev){EV_ABS, ABS_Y};
return MAP_STATIC;
case 0x32:
*map_to = (struct input_ev){EV_ABS, ABS_Z};
return MAP_STATIC;
case 0x33:
*map_to = (struct input_ev){EV_ABS, ABS_RX};
return MAP_STATIC;
case 0x34:
*map_to = (struct input_ev){EV_ABS, ABS_RY};
return MAP_STATIC;
case 0x35:
*map_to = (struct input_ev){EV_ABS, ABS_RZ};
return MAP_STATIC;
case 0x39:
*map_to = (struct input_ev){0, 0};
return MAP_AUTO;
case 0x85:
*map_to = (struct input_ev){EV_KEY, BTN_MODE};
return MAP_STATIC;
}
}
return MAP_IGNORE;
}
u8 map_hid_to_input_linux(struct hid_usage *usage, struct input_ev *map_to)
{
/*
* Linux report-descriptor (335 byte):
*
* 05 01 09 05 a1 01 85 01 09 01 a1 00 09 30 09 31 15 00 27 ff
* ff 00 00 95 02 75 10 81 02 c0 09 01 a1 00 09 32 09 35 15 00
* 27 ff ff 00 00 95 02 75 10 81 02 c0 05 02 09 c5 15 00 26 ff
* 03 95 01 75 0a 81 02 15 00 25 00 75 06 95 01 81 03 05 02 09
* c4 15 00 26 ff 03 95 01 75 0a 81 02 15 00 25 00 75 06 95 01
* 81 03 05 01 09 39 15 01 25 08 35 00 46 3b 01 66 14 00 75 04
* 95 01 81 42 75 04 95 01 15 00 25 00 35 00 45 00 65 00 81 03
* 05 09 19 01 29 0f 15 00 25 01 75 01 95 0f 81 02 15 00 25 00
* 75 01 95 01 81 03 05 0c 0a 24 02 15 00 25 01 95 01 75 01 81
* 02 15 00 25 00 75 07 95 01 81 03 05 0c 09 01 85 02 a1 01 05
* 0c 0a 23 02 15 00 25 01 95 01 75 01 81 02 15 00 25 00 75 07
* 95 01 81 03 c0 05 0f 09 21 85 03 a1 02 09 97 15 00 25 01 75
* 04 95 01 91 02 15 00 25 00 75 04 95 01 91 03 09 70 15 00 25
* 64 75 08 95 04 91 02 09 50 66 01 10 55 0e 15 00 26 ff 00 75
* 08 95 01 91 02 09 a7 15 00 26 ff 00 75 08 95 01 91 02 65 00
* 55 00 09 7c 15 00 26 ff 00 75 08 95 01 91 02 c0 85 04 05 06
* 09 20 15 00 26 ff 00 75 08 95 01 81 02 c0 00
*/
unsigned int hid_usage = usage->hid & HID_USAGE;
unsigned int hid_usage_page = usage->hid & HID_USAGE_PAGE;
switch (hid_usage_page) {
case HID_UP_BUTTON:
switch (hid_usage) {
case 0x01:
*map_to = (struct input_ev){EV_KEY, BTN_A};
return MAP_STATIC;
case 0x02:
*map_to = (struct input_ev){EV_KEY, BTN_B};
return MAP_STATIC;
case 0x04:
*map_to = (struct input_ev){EV_KEY, BTN_X};
return MAP_STATIC;
case 0x05:
*map_to = (struct input_ev){EV_KEY, BTN_Y};
return MAP_STATIC;
case 0x07:
*map_to = (struct input_ev){EV_KEY, BTN_TL};
return MAP_STATIC;
case 0x08:
*map_to = (struct input_ev){EV_KEY, BTN_TR};
return MAP_STATIC;
case 0x0C:
*map_to = (struct input_ev){EV_KEY, BTN_START};
return MAP_STATIC;
case 0x0E:
*map_to = (struct input_ev){EV_KEY, BTN_THUMBL};
return MAP_STATIC;
case 0x0F:
*map_to = (struct input_ev){EV_KEY, BTN_THUMBR};
return MAP_STATIC;
}
case HID_UP_CONSUMER:
switch (hid_usage) {
case 0x223:
*map_to = (struct input_ev){EV_KEY, BTN_MODE};
return MAP_STATIC;
case 0x224:
*map_to = (struct input_ev){EV_KEY, BTN_SELECT};
return MAP_STATIC;
}
case HID_UP_GENDESK:
switch (hid_usage) {
case 0x30:
*map_to = (struct input_ev){EV_ABS, ABS_X};
return MAP_STATIC;
case 0x31:
*map_to = (struct input_ev){EV_ABS, ABS_Y};
return MAP_STATIC;
case 0x32:
*map_to = (struct input_ev){EV_ABS, ABS_RX};
return MAP_STATIC;
case 0x35:
*map_to = (struct input_ev){EV_ABS, ABS_RY};
return MAP_STATIC;
case 0x39:
*map_to = (struct input_ev){0, 0};
return MAP_AUTO;
}
case HID_UP_SIMULATION:
switch (hid_usage) {
case 0xC4:
*map_to = (struct input_ev){EV_ABS, ABS_RZ};
return MAP_STATIC;
case 0xC5:
*map_to = (struct input_ev){EV_ABS, ABS_Z};
return MAP_STATIC;
}
}
return MAP_IGNORE;
}
/*
* Input Mapping Hook
*
* Invoked at input registering before mapping an usage
* (called once for every hid-usage).
*/
static int xpadneo_mapping(struct hid_device *hdev, struct hid_input *hi,
struct hid_field *field, struct hid_usage *usage,
unsigned long **bit, int *max)
{
/* Return values */
enum {
RET_MAP_IGNORE = -1, /* completely ignore this input */
RET_MAP_AUTO, /* let hid-core autodetect the mapping */
RET_MAP_STATIC /* mapped by hand, no further processing */
};
struct input_ev map_to;
u8 (*perform_mapping)(struct hid_usage *usage, struct input_ev *map_to);
struct xpadneo_devdata *xdata = hid_get_drvdata(hdev);
switch (xdata->report_descriptor) {
case LINUX:
perform_mapping = map_hid_to_input_linux;
break;
case WINDOWS:
perform_mapping = map_hid_to_input_windows;
break;
default:
return RET_MAP_AUTO;
}
switch (perform_mapping(usage, &map_to)) {
case MAP_AUTO:
hid_dbg_lvl(DBG_LVL_FEW, hdev,
"UP: 0x%04X, USG: 0x%04X -> automatically\n",
usage->hid & HID_USAGE_PAGE, usage->hid & HID_USAGE);
return RET_MAP_AUTO;
case MAP_IGNORE:
hid_dbg_lvl(DBG_LVL_FEW, hdev,
"UP: 0x%04X, USG: 0x%04X -> ignored\n",
usage->hid & HID_USAGE_PAGE, usage->hid & HID_USAGE);
return RET_MAP_IGNORE;
case MAP_STATIC:
hid_dbg_lvl(DBG_LVL_FEW, hdev,
"UP: 0x%04X, USG: 0x%04X -> EV: 0x%03X, INP: 0x%03X\n",
usage->hid & HID_USAGE_PAGE, usage->hid & HID_USAGE,
map_to.event_type, map_to.input_code);
hid_map_usage_clear(hi, usage, bit, max,
map_to.event_type, map_to.input_code);
return RET_MAP_STATIC;
}
/* Something went wrong, ignore this field */
return RET_MAP_IGNORE;
}
/*
* Report Descriptor Fixup Hook
*
* You can either modify the original report in place and just
* return the original start address (rdesc) or you reserve a new
* one and return a pointer to it. In the latter, you mostly have to
* modify the rsize value too.
*/
static u8 *xpadneo_report_fixup(struct hid_device *hdev, u8 *rdesc,
unsigned int *rsize)
{
hid_dbg_lvl(DBG_LVL_SOME, hdev, "REPORT (DESCRIPTOR) FIXUP HOOK\n");
dbg_hex_dump_lvl(DBG_LVL_FEW, "xpadneo: report-descr: ", rdesc, *rsize);
return rdesc;
}
static void parse_raw_event_battery(struct hid_device *hdev, u8 *data,
int reportsize)
{
struct xpadneo_devdata *xdata = hid_get_drvdata(hdev);
unsigned long flags;
u8 capacity_level, present, online, status;
/* msb ID 04 lsb
* +---+---+---+---+---+---+---+---+
* | O | R | E | C | M | M | L | L |
* +---+---+---+---+---+---+---+---+
*
* O: Online
* R: Reserved / Unused
* E: Error (?) / Unknown
* C: Charging, I mean really charging the battery (P 'n C)
* not (only) the power cord powering the controller
* M M: Mode
* 00: Powered by USB
* 01: Powered by (disposable) batteries
* 10: Powered by Play 'n Charge battery pack (only, no cable)
* L L: Capacity Level
* 00: (Super) Critical
* 01: Low
* 10: Medium
* 11: Full
*/
/* I think "online" means whether the dev is online or shutting down */
online = (data[1] & 0x80) >> 7;
/* The _battery_ is only present if not powered by USB */
present = ((data[1] & 0x0C) != 0x00);
/* Capacity level, only valid as long as the battery is present */
switch (data[1] & 0x03) {
case 0x00:
capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
break;
case 0x01:
capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
break;
case 0x02:
capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
break;
case 0x03:
capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
break;
}
/* Is the (Play 'n Charge) battery charging right now? */
switch ((data[1] & 0x10) >> 4) {
case 0:
status = POWER_SUPPLY_STATUS_DISCHARGING;
break;
case 1:
status = POWER_SUPPLY_STATUS_CHARGING;
break;
}
spin_lock_irqsave(&xdata->lock, flags);
xdata->ps_status = status;
xdata->ps_capacity_level = capacity_level;
xdata->ps_online = online;
xdata->ps_present = present;
spin_unlock_irqrestore(&xdata->lock, flags);
power_supply_changed(xdata->batt);
}
static void check_report_behaviour(struct hid_device *hdev, u8 *data,
int reportsize)
{
struct xpadneo_devdata *xdata = hid_get_drvdata(hdev);
/*
* The length of the first input report with an ID of 0x01
* reveals which report-type the controller is actually
* sending (windows: 16, or linux: 17).
*/
if (xdata->report_behaviour == UNKNOWN) {
switch (reportsize) {
case 16:
xdata->report_behaviour = WINDOWS;
break;
case 17:
xdata->report_behaviour = LINUX;
break;
default:
xdata->report_behaviour = UNKNOWN;
break;
}
}
hid_dbg_lvl(DBG_LVL_SOME, hdev, "desc: %s, beh: %s\n",
report_type_text[xdata->report_descriptor],
report_type_text[xdata->report_behaviour]);
/* TODO:
* Maybe the best solution would be to replace the report descriptor
* in case that the wrong reports are sent. Unfortunately we do not
* know if the report descriptor is the right one until the first
* report is sent to us. At this time, the report_fixup hook is
* already over and the original descriptor is parsed into hdev
* i.e. report_enum and collection.
*
* The next best solution would be to replace the report with
* ID 0x01 with the right one in report_enum (and collection?).
* I don't know yet how this would works, perhaps like this:
* - create a new report struct
* - fill it by hand
* - add all neccessary fields (automatic way?)
*
* Another way to fix it is:
* - Register another report with a _new_ ID by hand
* (unfortunately we cannot use the same id again)
* - in raw_event: change the ID from 0x01 to the new one if
* necessary. leave it if not.
*
* What we currently do is:
* We examine every report and fire the input events by hand.
* That's not very generic.
*
*/
// TODO:
// * remove old report using list operations
// * create new one like they do in hid_register_report
// * add it to output_reports->report_list and array
}
/*
* HID Raw Event Hook
*/
int xpadneo_raw_event(struct hid_device *hdev, struct hid_report *report,
u8 *data, int reportsize)
{
/* Return Codes */
enum {
RAWEV_CONT_PROCESSING, /* Let the hid-core autodetect the event */
RAWEV_STOP_PROCESSING /* Stop further processing */
};
//hid_dbg_lvl(DBG_LVL_SOME, hdev, "RAW EVENT HOOK\n");
dbg_hex_dump_lvl(DBG_LVL_SOME, "xpadneo: raw_event: ", data, reportsize);
//hid_dbg_lvl(DBG_LVL_ALL, hdev, "report->size: %d\n", (report->size)/8);
//hid_dbg_lvl(DBG_LVL_ALL, hdev, "data size (wo id): %d\n", reportsize-1);
switch (report->id) {
case 01:
check_report_behaviour(hdev, data, reportsize);
break;
case 04:
parse_raw_event_battery(hdev, data, reportsize);
return RAWEV_STOP_PROCESSING;
}
/* Continue processing */
return RAWEV_CONT_PROCESSING;
}
void xpadneo_report(struct hid_device *hdev, struct hid_report *report)
{
hid_dbg_lvl(DBG_LVL_SOME, hdev, "REPORT HOOK\n");
}
/*
* Input Configured Hook
*
* We have to fix up the key-bitmap, because there is
* no DPAD_UP, _RIGHT, _DOWN, _LEFT on the device by default
*
*/
static int xpadneo_input_configured(struct hid_device *hdev,
struct hid_input *hi)
{
struct xpadneo_devdata *xdata = hid_get_drvdata(hdev);
/* set a pointer to the logical input device at the device structure */
xdata->idev = hi->input;
hid_dbg_lvl(DBG_LVL_SOME, hdev, "INPUT CONFIGURED HOOK\n");
if (fake_dev_version){
xdata->idev->id.version = (u16) fake_dev_version;