-
Notifications
You must be signed in to change notification settings - Fork 4
/
acpi.patch
4139 lines (4120 loc) · 108 KB
/
acpi.patch
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
From e69519a8701cd692f2c84371b1abc0bfbc64b70c Mon Sep 17 00:00:00 2001
From: Jake Day <[email protected]>
Date: Thu, 6 Jun 2019 13:40:15 -0400
Subject: [PATCH 01/12] surface-acpi
---
drivers/acpi/acpica/dsopcode.c | 2 +-
drivers/acpi/acpica/exfield.c | 12 +-
drivers/platform/x86/Kconfig | 97 +
drivers/platform/x86/Makefile | 1 +
drivers/platform/x86/surface_acpi.c | 3828 +++++++++++++++++++++++++++
drivers/tty/serdev/core.c | 90 +-
6 files changed, 4024 insertions(+), 6 deletions(-)
create mode 100644 drivers/platform/x86/surface_acpi.c
diff --git a/drivers/acpi/acpica/dsopcode.c b/drivers/acpi/acpica/dsopcode.c
index 10f32b62608e..7b2a4987f050 100644
--- a/drivers/acpi/acpica/dsopcode.c
+++ b/drivers/acpi/acpica/dsopcode.c
@@ -123,7 +123,7 @@ acpi_ds_init_buffer_field(u16 aml_opcode,
/* Offset is in bits, count is in bits */
- field_flags = AML_FIELD_ACCESS_BYTE;
+ field_flags = AML_FIELD_ACCESS_BUFFER;
bit_offset = offset;
bit_count = (u32) length_desc->integer.value;
diff --git a/drivers/acpi/acpica/exfield.c b/drivers/acpi/acpica/exfield.c
index d3d2dbfba680..0b7f617a6e9b 100644
--- a/drivers/acpi/acpica/exfield.c
+++ b/drivers/acpi/acpica/exfield.c
@@ -109,6 +109,7 @@ acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
union acpi_operand_object *buffer_desc;
void *buffer;
u32 buffer_length;
+ u8 field_flags;
ACPI_FUNCTION_TRACE_PTR(ex_read_data_from_field, obj_desc);
@@ -157,11 +158,16 @@ acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
* Note: Field.length is in bits.
*/
buffer_length =
- (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
+ (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
+ field_flags = obj_desc->common_field.field_flags;
- if (buffer_length > acpi_gbl_integer_byte_width) {
+ if (buffer_length > acpi_gbl_integer_byte_width ||
+ (field_flags & AML_FIELD_ACCESS_TYPE_MASK) == AML_FIELD_ACCESS_BUFFER) {
- /* Field is too large for an Integer, create a Buffer instead */
+ /*
+ * Field is either too large for an Integer, or a actually of type
+ * buffer, so create a Buffer.
+ */
buffer_desc = acpi_ut_create_buffer_object(buffer_length);
if (!buffer_desc) {
diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index a1ed13183559..501d06d4f196 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -622,6 +622,103 @@ config THINKPAD_ACPI_HOTKEY_POLL
If you are not sure, say Y here. The driver enables polling only if
it is strictly necessary to do so.
+config SURFACE_ACPI
+ depends on ACPI
+ tristate "Microsoft Surface ACPI/Platform Drivers"
+ ---help---
+ ACPI and platform drivers for Microsoft Surface devices.
+
+config SURFACE_ACPI_SSH
+ bool "Surface Serial Hub Driver"
+ depends on SURFACE_ACPI
+ depends on X86_INTEL_LPSS
+ depends on SERIAL_8250_DW
+ depends on SERIAL_8250_DMA
+ depends on SERIAL_DEV_CTRL_TTYPORT
+ select CRC_CCITT
+ default y
+ ---help---
+ Surface Serial Hub driver for 5th generation (or later) Microsoft
+ Surface devices.
+
+ This is the base driver for the embedded serial controller found on
+ 5th generation (and later) Microsoft Surface devices (e.g. Book 2,
+ Laptop, Laptop 2, Pro 2017, Pro 6, ...). This driver itself only
+ provides access to the embedded controller and subsequent drivers are
+ required for the respective functionalities.
+
+ If you have a 5th generation (or later) Microsoft Surface device, say
+ Y or M here.
+
+config SURFACE_ACPI_SSH_DEBUG_DEVICE
+ bool "Surface Serial Hub Debug Device"
+ depends on SURFACE_ACPI_SSH
+ default n
+ ---help---
+ Debug device for direct communication with the embedded controller
+ found on 5th generation (and later) Microsoft Surface devices (e.g.
+ Book 2, Laptop, Laptop 2, Pro 2017, Pro 6, ...) via sysfs.
+
+ If you are not sure, say N here.
+
+config SURFACE_ACPI_SAN
+ bool "Surface ACPI Notify Driver"
+ depends on SURFACE_ACPI_SSH
+ default y
+ ---help---
+ Surface ACPI Notify driver for 5th generation (or later) Microsoft
+ Surface devices.
+
+ This driver enables basic ACPI events and requests, such as battery
+ status requests/events, thermal events, lid status, and possibly more,
+ which would otherwise not work on these devices.
+
+ If you are not sure, say Y here.
+
+config SURFACE_ACPI_VHF
+ bool "Surface Virtual HID Framework Driver"
+ depends on SURFACE_ACPI_SSH
+ depends on HID
+ default y
+ ---help---
+ Surface Virtual HID Framework driver for 5th generation (or later)
+ Microsoft Surface devices.
+
+ This driver provides support for the Microsoft Virtual HID framework,
+ which is required for the Surface Laptop (1 and newer) keyboard.
+
+ If you are not sure, say Y here.
+
+config SURFACE_ACPI_DTX
+ bool "Surface Detachment System (DTX) Driver"
+ depends on SURFACE_ACPI_SSH
+ depends on INPUT
+ default y
+ ---help---
+ Surface Detachment System (DTX) driver for the Microsoft Surface Book
+ 2. This driver provides support for proper detachment handling in
+ user-space, status-events relating to the base and support for
+ the safe-guard keeping the base attached when the discrete GPU
+ contained in it is running via the special /dev/surface-dtx device.
+
+ Also provides a standard input device to provide SW_TABLET_MODE events
+ upon device mode change.
+
+ If you are not sure, say Y here.
+
+config SURFACE_ACPI_SID
+ bool "Surface Platform Integration Driver"
+ depends on SURFACE_ACPI_SSH
+ default y
+ ---help---
+ Surface Platform Integration Driver for the Microsoft Surface Devices.
+ Currently only supports the Surface Book 2. This driver provides suport
+ for setting performance-modes via the perf_mode sysfs attribute.
+ Performance-modes directly influence the fan-profile of the device,
+ allowing to choose between higher performance or quieter operation.
+
+ If you are not sure, say Y here.
+
config SENSORS_HDAPS
tristate "Thinkpad Hard Drive Active Protection System (hdaps)"
depends on INPUT
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index 86cb76677bc8..be1ce92a365d 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -39,6 +39,7 @@ obj-$(CONFIG_TC1100_WMI) += tc1100-wmi.o
obj-$(CONFIG_SONY_LAPTOP) += sony-laptop.o
obj-$(CONFIG_IDEAPAD_LAPTOP) += ideapad-laptop.o
obj-$(CONFIG_THINKPAD_ACPI) += thinkpad_acpi.o
+obj-$(CONFIG_SURFACE_ACPI) += surface_acpi.o
obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o
obj-$(CONFIG_FUJITSU_LAPTOP) += fujitsu-laptop.o
obj-$(CONFIG_FUJITSU_TABLET) += fujitsu-tablet.o
diff --git a/drivers/platform/x86/surface_acpi.c b/drivers/platform/x86/surface_acpi.c
new file mode 100644
index 000000000000..ab793f6774a0
--- /dev/null
+++ b/drivers/platform/x86/surface_acpi.c
@@ -0,0 +1,3828 @@
+#include <asm/unaligned.h>
+#include <linux/acpi.h>
+#include <linux/completion.h>
+#include <linux/crc-ccitt.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/dmaengine.h>
+#include <linux/fs.h>
+#include <linux/hid.h>
+#include <linux/input.h>
+#include <linux/ioctl.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/kfifo.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/poll.h>
+#include <linux/rculist.h>
+#include <linux/refcount.h>
+#include <linux/serdev.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/types.h>
+#include <linux/workqueue.h>
+
+
+#define USB_VENDOR_ID_MICROSOFT 0x045e
+#define USB_DEVICE_ID_MS_VHF 0xf001
+#define USB_DEVICE_ID_MS_SURFACE_BASE_2_INTEGRATION 0x0922
+
+#define SG5_PARAM_PERM (S_IRUGO | S_IWUSR)
+
+
+/*************************************************************************
+ * Surface Serial Hub driver (cross-driver interface)
+ */
+
+#ifdef CONFIG_SURFACE_ACPI_SSH
+
+/*
+ * Maximum request payload size in bytes.
+ * Value based on ACPI (255 bytes minus header/status bytes).
+ */
+#define SURFACEGEN5_MAX_RQST_PAYLOAD (255 - 10)
+
+/*
+ * Maximum response payload size in bytes.
+ * Value based on ACPI (255 bytes minus header/status bytes).
+ */
+#define SURFACEGEN5_MAX_RQST_RESPONSE (255 - 4)
+
+#define SURFACEGEN5_RQID_EVENT_BITS 5
+
+#define SURFACEGEN5_EVENT_IMMEDIATE ((unsigned long) -1)
+
+
+struct surfacegen5_buf {
+ u8 cap;
+ u8 len;
+ u8 *data;
+};
+
+struct surfacegen5_rqst {
+ u8 tc;
+ u8 iid;
+ u8 cid;
+ u8 snc;
+ u8 cdl;
+ u8 *pld;
+};
+
+struct surfacegen5_event {
+ u16 rqid;
+ u8 tc;
+ u8 iid;
+ u8 cid;
+ u8 len;
+ u8 *pld;
+};
+
+
+typedef int (*surfacegen5_ec_event_handler_fn)(struct surfacegen5_event *event, void *data);
+typedef unsigned long (*surfacegen5_ec_event_handler_delay)(struct surfacegen5_event *event, void *data);
+
+struct device_link *surfacegen5_ec_consumer_add(struct device *consumer, u32 flags);
+int surfacegen5_ec_consumer_remove(struct device_link *link);
+
+int surfacegen5_ec_rqst(const struct surfacegen5_rqst *rqst, struct surfacegen5_buf *result);
+
+int surfacegen5_ec_enable_event_source(u8 tc, u8 unknown, u16 rqid);
+int surfacegen5_ec_disable_event_source(u8 tc, u8 unknown, u16 rqid);
+int surfacegen5_ec_remove_event_handler(u16 rqid);
+int surfacegen5_ec_set_event_handler(u16 rqid, surfacegen5_ec_event_handler_fn fn, void *data);
+int surfacegen5_ec_set_delayed_event_handler(u16 rqid,
+ surfacegen5_ec_event_handler_fn fn,
+ surfacegen5_ec_event_handler_delay delay, void *data);
+
+#endif /* CONFIG_SURFACE_ACPI_SSH */
+
+
+/*************************************************************************
+ * Surface Serial Hub Debug Device (cross-driver interface)
+ */
+
+#ifdef CONFIG_SURFACE_ACPI_SSH
+
+int surfacegen5_ssh_sysfs_register(struct device *dev);
+void surfacegen5_ssh_sysfs_unregister(struct device *dev);
+
+#endif /* CONFIG_SURFACE_ACPI_SSH */
+
+
+/*************************************************************************
+ * Surface Serial Hub driver (private implementation)
+ */
+
+#ifdef CONFIG_SURFACE_ACPI_SSH
+
+#define SG5_RQST_TAG_FULL "surfacegen5_ec_rqst: "
+#define SG5_RQST_TAG "rqst: "
+#define SG5_EVENT_TAG "event: "
+#define SG5_RECV_TAG "recv: "
+
+#define SG5_SUPPORTED_FLOW_CONTROL_MASK (~((u8) ACPI_UART_FLOW_CONTROL_HW))
+
+#define SG5_BYTELEN_SYNC 2
+#define SG5_BYTELEN_TERM 2
+#define SG5_BYTELEN_CRC 2
+#define SG5_BYTELEN_CTRL 4 // command-header, ACK, or RETRY
+#define SG5_BYTELEN_CMDFRAME 8 // without payload
+
+#define SG5_MAX_WRITE ( \
+ SG5_BYTELEN_SYNC \
+ + SG5_BYTELEN_CTRL \
+ + SG5_BYTELEN_CRC \
+ + SG5_BYTELEN_CMDFRAME \
+ + SURFACEGEN5_MAX_RQST_PAYLOAD \
+ + SG5_BYTELEN_CRC \
+)
+
+#define SG5_MSG_LEN_CTRL ( \
+ SG5_BYTELEN_SYNC \
+ + SG5_BYTELEN_CTRL \
+ + SG5_BYTELEN_CRC \
+ + SG5_BYTELEN_TERM \
+)
+
+#define SG5_MSG_LEN_CMD_BASE ( \
+ SG5_BYTELEN_SYNC \
+ + SG5_BYTELEN_CTRL \
+ + SG5_BYTELEN_CRC \
+ + SG5_BYTELEN_CRC \
+) // without payload and command-frame
+
+#define SG5_WRITE_TIMEOUT msecs_to_jiffies(1000)
+#define SG5_READ_TIMEOUT msecs_to_jiffies(1000)
+#define SG5_NUM_RETRY 3
+
+#define SG5_WRITE_BUF_LEN SG5_MAX_WRITE
+#define SG5_READ_BUF_LEN 512 // must be power of 2
+#define SG5_EVAL_BUF_LEN SG5_MAX_WRITE // also works for reading
+
+#define SG5_FRAME_TYPE_CMD 0x80
+#define SG5_FRAME_TYPE_ACK 0x40
+#define SG5_FRAME_TYPE_RETRY 0x04
+
+#define SG5_FRAME_OFFS_CTRL SG5_BYTELEN_SYNC
+#define SG5_FRAME_OFFS_CTRL_CRC (SG5_FRAME_OFFS_CTRL + SG5_BYTELEN_CTRL)
+#define SG5_FRAME_OFFS_TERM (SG5_FRAME_OFFS_CTRL_CRC + SG5_BYTELEN_CRC)
+#define SG5_FRAME_OFFS_CMD SG5_FRAME_OFFS_TERM // either TERM or CMD
+#define SG5_FRAME_OFFS_CMD_PLD (SG5_FRAME_OFFS_CMD + SG5_BYTELEN_CMDFRAME)
+
+/*
+ * A note on Request IDs (RQIDs):
+ * 0x0000 is not a valid RQID
+ * 0x0001 is valid, but reserved for Surface Laptop keyboard events
+ */
+#define SG5_NUM_EVENT_TYPES ((1 << SURFACEGEN5_RQID_EVENT_BITS) - 1)
+
+/*
+ * Sync: aa 55
+ * Terminate: ff ff
+ *
+ * Request Message: sync cmd-hdr crc(cmd-hdr) cmd-rqst-frame crc(cmd-rqst-frame)
+ * Ack Message: sync ack crc(ack) terminate
+ * Retry Message: sync retry crc(retry) terminate
+ * Response Message: sync cmd-hdr crc(cmd-hdr) cmd-resp-frame crc(cmd-resp-frame)
+ *
+ * Command Header: 80 LEN 00 SEQ
+ * Ack: 40 00 00 SEQ
+ * Retry: 04 00 00 00
+ * Command Request Frame: 80 RTC 01 00 RIID RQID RCID PLD
+ * Command Response Frame: 80 RTC 00 01 RIID RQID RCID PLD
+ */
+
+struct surfacegen5_frame_ctrl {
+ u8 type;
+ u8 len; // without crc
+ u8 pad;
+ u8 seq;
+} __packed;
+
+struct surfacegen5_frame_cmd {
+ u8 type;
+ u8 tc;
+ u8 unknown1;
+ u8 unknown2;
+ u8 iid;
+ u8 rqid_lo; // id for request/response matching (low byte)
+ u8 rqid_hi; // id for request/response matching (high byte)
+ u8 cid;
+} __packed;
+
+
+enum surfacegen5_ec_state {
+ SG5_EC_UNINITIALIZED,
+ SG5_EC_INITIALIZED,
+ SG5_EC_SUSPENDED,
+};
+
+struct surfacegen5_ec_counters {
+ u8 seq; // control sequence id
+ u16 rqid; // id for request/response matching
+};
+
+struct surfacegen5_ec_writer {
+ u8 *data;
+ u8 *ptr;
+} __packed;
+
+enum surfacegen5_ec_receiver_state {
+ SG5_RCV_DISCARD,
+ SG5_RCV_CONTROL,
+ SG5_RCV_COMMAND,
+};
+
+struct surfacegen5_ec_receiver {
+ spinlock_t lock;
+ enum surfacegen5_ec_receiver_state state;
+ struct completion signal;
+ struct kfifo fifo;
+ struct {
+ bool pld;
+ u8 seq;
+ u16 rqid;
+ } expect;
+ struct {
+ u16 cap;
+ u16 len;
+ u8 *ptr;
+ } eval_buf;
+};
+
+struct surfacegen5_ec_event_handler {
+ surfacegen5_ec_event_handler_fn handler;
+ surfacegen5_ec_event_handler_delay delay;
+ void *data;
+};
+
+struct surfacegen5_ec_events {
+ spinlock_t lock;
+ struct workqueue_struct *queue_ack;
+ struct workqueue_struct *queue_evt;
+ struct surfacegen5_ec_event_handler handler[SG5_NUM_EVENT_TYPES];
+};
+
+struct surfacegen5_ec {
+ struct mutex lock;
+ enum surfacegen5_ec_state state;
+ struct serdev_device *serdev;
+ struct surfacegen5_ec_counters counter;
+ struct surfacegen5_ec_writer writer;
+ struct surfacegen5_ec_receiver receiver;
+ struct surfacegen5_ec_events events;
+};
+
+struct surfacegen5_fifo_packet {
+ u8 type; // packet type (ACK/RETRY/CMD)
+ u8 seq;
+ u8 len;
+};
+
+struct surfacegen5_event_work {
+ refcount_t refcount;
+ struct surfacegen5_ec *ec;
+ struct work_struct work_ack;
+ struct delayed_work work_evt;
+ struct surfacegen5_event event;
+ u8 seq;
+};
+
+
+static struct surfacegen5_ec surfacegen5_ec = {
+ .lock = __MUTEX_INITIALIZER(surfacegen5_ec.lock),
+ .state = SG5_EC_UNINITIALIZED,
+ .serdev = NULL,
+ .counter = {
+ .seq = 0,
+ .rqid = 0,
+ },
+ .writer = {
+ .data = NULL,
+ .ptr = NULL,
+ },
+ .receiver = {
+ .lock = __SPIN_LOCK_UNLOCKED(),
+ .state = SG5_RCV_DISCARD,
+ .expect = {},
+ },
+ .events = {
+ .lock = __SPIN_LOCK_UNLOCKED(),
+ .handler = {},
+ }
+};
+
+
+static int surfacegen5_ec_rqst_unlocked(struct surfacegen5_ec *ec,
+ const struct surfacegen5_rqst *rqst,
+ struct surfacegen5_buf *result);
+
+
+inline static struct surfacegen5_ec *surfacegen5_ec_acquire(void)
+{
+ struct surfacegen5_ec *ec = &surfacegen5_ec;
+
+ mutex_lock(&ec->lock);
+ return ec;
+}
+
+inline static void surfacegen5_ec_release(struct surfacegen5_ec *ec)
+{
+ mutex_unlock(&ec->lock);
+}
+
+inline static struct surfacegen5_ec *surfacegen5_ec_acquire_init(void)
+{
+ struct surfacegen5_ec *ec = surfacegen5_ec_acquire();
+
+ if (ec->state == SG5_EC_UNINITIALIZED) {
+ surfacegen5_ec_release(ec);
+ return NULL;
+ }
+
+ return ec;
+}
+
+struct device_link *surfacegen5_ec_consumer_add(struct device *consumer, u32 flags)
+{
+ struct surfacegen5_ec *ec;
+ struct device_link *link;
+
+ ec = surfacegen5_ec_acquire_init();
+ if (!ec) {
+ return ERR_PTR(-ENXIO);
+ }
+
+ link = device_link_add(consumer, &ec->serdev->dev, flags);
+
+ surfacegen5_ec_release(ec);
+ return link;
+}
+
+int surfacegen5_ec_consumer_remove(struct device_link *link)
+{
+ struct surfacegen5_ec *ec = surfacegen5_ec_acquire_init();
+ if (!ec) {
+ return -ENXIO;
+ }
+
+ device_link_del(link);
+
+ surfacegen5_ec_release(ec);
+ return 0;
+}
+
+
+inline static u16 surfacegen5_rqid_to_rqst(u16 rqid) {
+ return rqid << SURFACEGEN5_RQID_EVENT_BITS;
+}
+
+inline static bool surfacegen5_rqid_is_event(u16 rqid) {
+ const u16 mask = (1 << SURFACEGEN5_RQID_EVENT_BITS) - 1;
+ return rqid != 0 && (rqid | mask) == mask;
+}
+
+int surfacegen5_ec_enable_event_source(u8 tc, u8 unknown, u16 rqid)
+{
+ struct surfacegen5_ec *ec;
+
+ u8 pld[4] = { tc, unknown, rqid & 0xff, rqid >> 8 };
+ u8 buf[1] = { 0x00 };
+
+ struct surfacegen5_rqst rqst = {
+ .tc = 0x01,
+ .iid = 0x00,
+ .cid = 0x0b,
+ .snc = 0x01,
+ .cdl = 0x04,
+ .pld = pld,
+ };
+
+ struct surfacegen5_buf result = {
+ result.cap = ARRAY_SIZE(buf),
+ result.len = 0,
+ result.data = buf,
+ };
+
+ int status;
+
+ // only allow RQIDs that lie within event spectrum
+ if (!surfacegen5_rqid_is_event(rqid)) {
+ return -EINVAL;
+ }
+
+ ec = surfacegen5_ec_acquire_init();
+ if (!ec) {
+ printk(KERN_WARNING SG5_RQST_TAG_FULL "embedded controller is uninitialized\n");
+ return -ENXIO;
+ }
+
+ if (ec->state == SG5_EC_SUSPENDED) {
+ dev_warn(&ec->serdev->dev, SG5_RQST_TAG "embedded controller is suspended\n");
+
+ surfacegen5_ec_release(ec);
+ return -EPERM;
+ }
+
+ status = surfacegen5_ec_rqst_unlocked(ec, &rqst, &result);
+
+ if (buf[0] != 0x00) {
+ dev_warn(&ec->serdev->dev,
+ "unexpected result while enabling event source: 0x%02x\n",
+ buf[0]);
+ }
+
+ surfacegen5_ec_release(ec);
+ return status;
+
+}
+
+int surfacegen5_ec_disable_event_source(u8 tc, u8 unknown, u16 rqid)
+{
+ struct surfacegen5_ec *ec;
+
+ u8 pld[4] = { tc, unknown, rqid & 0xff, rqid >> 8 };
+ u8 buf[1] = { 0x00 };
+
+ struct surfacegen5_rqst rqst = {
+ .tc = 0x01,
+ .iid = 0x00,
+ .cid = 0x0c,
+ .snc = 0x01,
+ .cdl = 0x04,
+ .pld = pld,
+ };
+
+ struct surfacegen5_buf result = {
+ result.cap = ARRAY_SIZE(buf),
+ result.len = 0,
+ result.data = buf,
+ };
+
+ int status;
+
+ // only allow RQIDs that lie within event spectrum
+ if (!surfacegen5_rqid_is_event(rqid)) {
+ return -EINVAL;
+ }
+
+ ec = surfacegen5_ec_acquire_init();
+ if (!ec) {
+ printk(KERN_WARNING SG5_RQST_TAG_FULL "embedded controller is uninitialized\n");
+ return -ENXIO;
+ }
+
+ if (ec->state == SG5_EC_SUSPENDED) {
+ dev_warn(&ec->serdev->dev, SG5_RQST_TAG "embedded controller is suspended\n");
+
+ surfacegen5_ec_release(ec);
+ return -EPERM;
+ }
+
+ status = surfacegen5_ec_rqst_unlocked(ec, &rqst, &result);
+
+ if (buf[0] != 0x00) {
+ dev_warn(&ec->serdev->dev,
+ "unexpected result while disabling event source: 0x%02x\n",
+ buf[0]);
+ }
+
+ surfacegen5_ec_release(ec);
+ return status;
+}
+
+int surfacegen5_ec_set_delayed_event_handler(
+ u16 rqid, surfacegen5_ec_event_handler_fn fn,
+ surfacegen5_ec_event_handler_delay delay,
+ void *data)
+{
+ struct surfacegen5_ec *ec;
+ unsigned long flags;
+
+ if (!surfacegen5_rqid_is_event(rqid)) {
+ return -EINVAL;
+ }
+
+ ec = surfacegen5_ec_acquire_init();
+ if (!ec) {
+ return -ENXIO;
+ }
+
+ spin_lock_irqsave(&ec->events.lock, flags);
+
+ // 0 is not a valid event RQID
+ ec->events.handler[rqid - 1].handler = fn;
+ ec->events.handler[rqid - 1].delay = delay;
+ ec->events.handler[rqid - 1].data = data;
+
+ spin_unlock_irqrestore(&ec->events.lock, flags);
+ surfacegen5_ec_release(ec);
+
+ return 0;
+}
+
+int surfacegen5_ec_set_event_handler(
+ u16 rqid, surfacegen5_ec_event_handler_fn fn, void *data)
+{
+ return surfacegen5_ec_set_delayed_event_handler(rqid, fn, NULL, data);
+}
+
+int surfacegen5_ec_remove_event_handler(u16 rqid)
+{
+ struct surfacegen5_ec *ec;
+ unsigned long flags;
+
+ if (!surfacegen5_rqid_is_event(rqid)) {
+ return -EINVAL;
+ }
+
+ ec = surfacegen5_ec_acquire_init();
+ if (!ec) {
+ return -ENXIO;
+ }
+
+ spin_lock_irqsave(&ec->events.lock, flags);
+
+ // 0 is not a valid event RQID
+ ec->events.handler[rqid - 1].handler = NULL;
+ ec->events.handler[rqid - 1].delay = NULL;
+ ec->events.handler[rqid - 1].data = NULL;
+
+ spin_unlock_irqrestore(&ec->events.lock, flags);
+ surfacegen5_ec_release(ec);
+
+ /*
+ * Make sure that the handler is not in use any more after we've
+ * removed it.
+ */
+ flush_workqueue(ec->events.queue_evt);
+
+ return 0;
+}
+
+
+inline static u16 surfacegen5_ssh_crc(const u8 *buf, size_t size)
+{
+ return crc_ccitt_false(0xffff, buf, size);
+}
+
+inline static void surfacegen5_ssh_write_u16(struct surfacegen5_ec_writer *writer, u16 in)
+{
+ put_unaligned_le16(in, writer->ptr);
+ writer->ptr += 2;
+}
+
+inline static void surfacegen5_ssh_write_crc(struct surfacegen5_ec_writer *writer,
+ const u8 *buf, size_t size)
+{
+ surfacegen5_ssh_write_u16(writer, surfacegen5_ssh_crc(buf, size));
+}
+
+inline static void surfacegen5_ssh_write_syn(struct surfacegen5_ec_writer *writer)
+{
+ u8 *w = writer->ptr;
+
+ *w++ = 0xaa;
+ *w++ = 0x55;
+
+ writer->ptr = w;
+}
+
+inline static void surfacegen5_ssh_write_ter(struct surfacegen5_ec_writer *writer)
+{
+ u8 *w = writer->ptr;
+
+ *w++ = 0xff;
+ *w++ = 0xff;
+
+ writer->ptr = w;
+}
+
+inline static void surfacegen5_ssh_write_buf(struct surfacegen5_ec_writer *writer,
+ u8 *in, size_t len)
+{
+ writer->ptr = memcpy(writer->ptr, in, len) + len;
+}
+
+inline static void surfacegen5_ssh_write_hdr(struct surfacegen5_ec_writer *writer,
+ const struct surfacegen5_rqst *rqst,
+ struct surfacegen5_ec *ec)
+{
+ struct surfacegen5_frame_ctrl *hdr = (struct surfacegen5_frame_ctrl *)writer->ptr;
+ u8 *begin = writer->ptr;
+
+ hdr->type = SG5_FRAME_TYPE_CMD;
+ hdr->len = SG5_BYTELEN_CMDFRAME + rqst->cdl; // without CRC
+ hdr->pad = 0x00;
+ hdr->seq = ec->counter.seq;
+
+ writer->ptr += sizeof(*hdr);
+
+ surfacegen5_ssh_write_crc(writer, begin, writer->ptr - begin);
+}
+
+inline static void surfacegen5_ssh_write_cmd(struct surfacegen5_ec_writer *writer,
+ const struct surfacegen5_rqst *rqst,
+ struct surfacegen5_ec *ec)
+{
+ struct surfacegen5_frame_cmd *cmd = (struct surfacegen5_frame_cmd *)writer->ptr;
+ u8 *begin = writer->ptr;
+
+ u16 rqid = surfacegen5_rqid_to_rqst(ec->counter.rqid);
+ u8 rqid_lo = rqid & 0xFF;
+ u8 rqid_hi = rqid >> 8;
+
+ cmd->type = SG5_FRAME_TYPE_CMD;
+ cmd->tc = rqst->tc;
+ cmd->unknown1 = 0x01;
+ cmd->unknown2 = 0x00;
+ cmd->iid = rqst->iid;
+ cmd->rqid_lo = rqid_lo;
+ cmd->rqid_hi = rqid_hi;
+ cmd->cid = rqst->cid;
+
+ writer->ptr += sizeof(*cmd);
+
+ surfacegen5_ssh_write_buf(writer, rqst->pld, rqst->cdl);
+ surfacegen5_ssh_write_crc(writer, begin, writer->ptr - begin);
+}
+
+inline static void surfacegen5_ssh_write_ack(struct surfacegen5_ec_writer *writer, u8 seq)
+{
+ struct surfacegen5_frame_ctrl *ack = (struct surfacegen5_frame_ctrl *)writer->ptr;
+ u8 *begin = writer->ptr;
+
+ ack->type = SG5_FRAME_TYPE_ACK;
+ ack->len = 0x00;
+ ack->pad = 0x00;
+ ack->seq = seq;
+
+ writer->ptr += sizeof(*ack);
+
+ surfacegen5_ssh_write_crc(writer, begin, writer->ptr - begin);
+}
+
+inline static void surfacegen5_ssh_writer_reset(struct surfacegen5_ec_writer *writer)
+{
+ writer->ptr = writer->data;
+}
+
+inline static int surfacegen5_ssh_writer_flush(struct surfacegen5_ec *ec)
+{
+ struct surfacegen5_ec_writer *writer = &ec->writer;
+ struct serdev_device *serdev = ec->serdev;
+ int status;
+
+ size_t len = writer->ptr - writer->data;
+
+ dev_dbg(&ec->serdev->dev, "sending message\n");
+ print_hex_dump_debug("send: ", DUMP_PREFIX_OFFSET, 16, 1,
+ writer->data, writer->ptr - writer->data, false);
+
+ status = serdev_device_write(serdev, writer->data, len, SG5_WRITE_TIMEOUT);
+ return status >= 0 ? 0 : status;
+}
+
+inline static void surfacegen5_ssh_write_msg_cmd(struct surfacegen5_ec *ec,
+ const struct surfacegen5_rqst *rqst)
+{
+ surfacegen5_ssh_writer_reset(&ec->writer);
+ surfacegen5_ssh_write_syn(&ec->writer);
+ surfacegen5_ssh_write_hdr(&ec->writer, rqst, ec);
+ surfacegen5_ssh_write_cmd(&ec->writer, rqst, ec);
+}
+
+inline static void surfacegen5_ssh_write_msg_ack(struct surfacegen5_ec *ec, u8 seq)
+{
+ surfacegen5_ssh_writer_reset(&ec->writer);
+ surfacegen5_ssh_write_syn(&ec->writer);
+ surfacegen5_ssh_write_ack(&ec->writer, seq);
+ surfacegen5_ssh_write_ter(&ec->writer);
+}
+
+inline static void surfacegen5_ssh_receiver_restart(struct surfacegen5_ec *ec,
+ const struct surfacegen5_rqst *rqst)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&ec->receiver.lock, flags);
+ reinit_completion(&ec->receiver.signal);
+ ec->receiver.state = SG5_RCV_CONTROL;
+ ec->receiver.expect.pld = rqst->snc;
+ ec->receiver.expect.seq = ec->counter.seq;
+ ec->receiver.expect.rqid = surfacegen5_rqid_to_rqst(ec->counter.rqid);
+ ec->receiver.eval_buf.len = 0;
+ spin_unlock_irqrestore(&ec->receiver.lock, flags);
+}
+
+inline static void surfacegen5_ssh_receiver_discard(struct surfacegen5_ec *ec)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&ec->receiver.lock, flags);
+ ec->receiver.state = SG5_RCV_DISCARD;
+ ec->receiver.eval_buf.len = 0;
+ kfifo_reset(&ec->receiver.fifo);
+ spin_unlock_irqrestore(&ec->receiver.lock, flags);
+}
+
+static int surfacegen5_ec_rqst_unlocked(struct surfacegen5_ec *ec,
+ const struct surfacegen5_rqst *rqst,
+ struct surfacegen5_buf *result)
+{
+ struct device *dev = &ec->serdev->dev;
+ struct surfacegen5_fifo_packet packet = {};
+ int status;
+ int try;
+ unsigned int rem;
+
+ if (rqst->cdl > SURFACEGEN5_MAX_RQST_PAYLOAD) {
+ dev_err(dev, SG5_RQST_TAG "request payload too large\n");
+ return -EINVAL;
+ }
+
+ // write command in buffer, we may need it multiple times
+ surfacegen5_ssh_write_msg_cmd(ec, rqst);
+ surfacegen5_ssh_receiver_restart(ec, rqst);
+
+ // send command, try to get an ack response
+ for (try = 0; try < SG5_NUM_RETRY; try++) {
+ status = surfacegen5_ssh_writer_flush(ec);
+ if (status) {
+ goto ec_rqst_out;
+ }
+
+ rem = wait_for_completion_timeout(&ec->receiver.signal, SG5_READ_TIMEOUT);
+ if (rem) {
+ // completion assures valid packet, thus ignore returned length
+ (void) !kfifo_out(&ec->receiver.fifo, &packet, sizeof(packet));
+
+ if (packet.type == SG5_FRAME_TYPE_ACK) {
+ break;
+ }
+ }
+ }
+
+ // check if we ran out of tries?
+ if (try >= SG5_NUM_RETRY) {
+ dev_err(dev, SG5_RQST_TAG "communication failed %d times, giving up\n", try);
+ status = -EIO;
+ goto ec_rqst_out;
+ }
+
+ ec->counter.seq += 1;
+ ec->counter.rqid += 1;
+
+ // get command response/payload
+ if (rqst->snc && result) {
+ rem = wait_for_completion_timeout(&ec->receiver.signal, SG5_READ_TIMEOUT);
+ if (rem) {
+ // completion assures valid packet, thus ignore returned length
+ (void) !kfifo_out(&ec->receiver.fifo, &packet, sizeof(packet));
+
+ if (result->cap < packet.len) {
+ status = -EINVAL;
+ goto ec_rqst_out;
+ }
+
+ // completion assures valid packet, thus ignore returned length
+ (void) !kfifo_out(&ec->receiver.fifo, result->data, packet.len);
+ result->len = packet.len;
+ } else {
+ dev_err(dev, SG5_RQST_TAG "communication timed out\n");
+ status = -EIO;
+ goto ec_rqst_out;
+ }
+
+ // send ACK
+ surfacegen5_ssh_write_msg_ack(ec, packet.seq);
+ status = surfacegen5_ssh_writer_flush(ec);
+ if (status) {
+ goto ec_rqst_out;
+ }
+ }
+
+ec_rqst_out:
+ surfacegen5_ssh_receiver_discard(ec);
+ return status;
+}
+