-
Notifications
You must be signed in to change notification settings - Fork 102
/
tcpm.c
6657 lines (5880 loc) · 176 KB
/
tcpm.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
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2015-2017 Google, Inc
*
* USB Power Delivery protocol stack.
*/
#include <linux/completion.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/hrtimer.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/power_supply.h>
#include <linux/proc_fs.h>
#include <linux/property.h>
#include <linux/sched/clock.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/usb.h>
#include <linux/usb/pd.h>
#include <linux/usb/pd_ado.h>
#include <linux/usb/pd_bdo.h>
#include <linux/usb/pd_ext_sdb.h>
#include <linux/usb/pd_vdo.h>
#include <linux/usb/role.h>
#include <linux/usb/tcpm.h>
#include <linux/usb/typec_altmode.h>
#include <uapi/linux/sched/types.h>
#define FOREACH_STATE(S) \
S(INVALID_STATE), \
S(TOGGLING), \
S(SRC_UNATTACHED), \
S(SRC_ATTACH_WAIT), \
S(SRC_ATTACHED), \
S(SRC_STARTUP), \
S(SRC_SEND_CAPABILITIES), \
S(SRC_SEND_CAPABILITIES_TIMEOUT), \
S(SRC_NEGOTIATE_CAPABILITIES), \
S(SRC_TRANSITION_SUPPLY), \
S(SRC_READY), \
S(SRC_WAIT_NEW_CAPABILITIES), \
\
S(SNK_UNATTACHED), \
S(SNK_ATTACH_WAIT), \
S(SNK_DEBOUNCED), \
S(SNK_ATTACHED), \
S(SNK_STARTUP), \
S(SNK_DISCOVERY), \
S(SNK_DISCOVERY_DEBOUNCE), \
S(SNK_DISCOVERY_DEBOUNCE_DONE), \
S(SNK_WAIT_CAPABILITIES), \
S(SNK_NEGOTIATE_CAPABILITIES), \
S(SNK_NEGOTIATE_PPS_CAPABILITIES), \
S(SNK_TRANSITION_SINK), \
S(SNK_TRANSITION_SINK_VBUS), \
S(SNK_READY), \
\
S(ACC_UNATTACHED), \
S(DEBUG_ACC_ATTACHED), \
S(AUDIO_ACC_ATTACHED), \
S(AUDIO_ACC_DEBOUNCE), \
\
S(HARD_RESET_SEND), \
S(HARD_RESET_START), \
S(SRC_HARD_RESET_VBUS_OFF), \
S(SRC_HARD_RESET_VBUS_ON), \
S(SNK_HARD_RESET_SINK_OFF), \
S(SNK_HARD_RESET_WAIT_VBUS), \
S(SNK_HARD_RESET_SINK_ON), \
\
S(SOFT_RESET), \
S(SRC_SOFT_RESET_WAIT_SNK_TX), \
S(SNK_SOFT_RESET), \
S(SOFT_RESET_SEND), \
\
S(DR_SWAP_ACCEPT), \
S(DR_SWAP_SEND), \
S(DR_SWAP_SEND_TIMEOUT), \
S(DR_SWAP_CANCEL), \
S(DR_SWAP_CHANGE_DR), \
\
S(PR_SWAP_ACCEPT), \
S(PR_SWAP_SEND), \
S(PR_SWAP_SEND_TIMEOUT), \
S(PR_SWAP_CANCEL), \
S(PR_SWAP_START), \
S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
S(PR_SWAP_SRC_SNK_SINK_ON), \
S(PR_SWAP_SNK_SRC_SINK_OFF), \
S(PR_SWAP_SNK_SRC_SOURCE_ON), \
S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \
\
S(VCONN_SWAP_ACCEPT), \
S(VCONN_SWAP_SEND), \
S(VCONN_SWAP_SEND_TIMEOUT), \
S(VCONN_SWAP_CANCEL), \
S(VCONN_SWAP_START), \
S(VCONN_SWAP_WAIT_FOR_VCONN), \
S(VCONN_SWAP_TURN_ON_VCONN), \
S(VCONN_SWAP_TURN_OFF_VCONN), \
\
S(FR_SWAP_SEND), \
S(FR_SWAP_SEND_TIMEOUT), \
S(FR_SWAP_SNK_SRC_TRANSITION_TO_OFF), \
S(FR_SWAP_SNK_SRC_NEW_SINK_READY), \
S(FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED), \
S(FR_SWAP_CANCEL), \
\
S(SNK_TRY), \
S(SNK_TRY_WAIT), \
S(SNK_TRY_WAIT_DEBOUNCE), \
S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \
S(SRC_TRYWAIT), \
S(SRC_TRYWAIT_DEBOUNCE), \
S(SRC_TRYWAIT_UNATTACHED), \
\
S(SRC_TRY), \
S(SRC_TRY_WAIT), \
S(SRC_TRY_DEBOUNCE), \
S(SNK_TRYWAIT), \
S(SNK_TRYWAIT_DEBOUNCE), \
S(SNK_TRYWAIT_VBUS), \
S(BIST_RX), \
\
S(GET_STATUS_SEND), \
S(GET_STATUS_SEND_TIMEOUT), \
S(GET_PPS_STATUS_SEND), \
S(GET_PPS_STATUS_SEND_TIMEOUT), \
\
S(GET_SINK_CAP), \
S(GET_SINK_CAP_TIMEOUT), \
\
S(ERROR_RECOVERY), \
S(PORT_RESET), \
S(PORT_RESET_WAIT_OFF), \
\
S(AMS_START), \
S(CHUNK_NOT_SUPP)
#define FOREACH_AMS(S) \
S(NONE_AMS), \
S(POWER_NEGOTIATION), \
S(GOTOMIN), \
S(SOFT_RESET_AMS), \
S(HARD_RESET), \
S(CABLE_RESET), \
S(GET_SOURCE_CAPABILITIES), \
S(GET_SINK_CAPABILITIES), \
S(POWER_ROLE_SWAP), \
S(FAST_ROLE_SWAP), \
S(DATA_ROLE_SWAP), \
S(VCONN_SWAP), \
S(SOURCE_ALERT), \
S(GETTING_SOURCE_EXTENDED_CAPABILITIES),\
S(GETTING_SOURCE_SINK_STATUS), \
S(GETTING_BATTERY_CAPABILITIES), \
S(GETTING_BATTERY_STATUS), \
S(GETTING_MANUFACTURER_INFORMATION), \
S(SECURITY), \
S(FIRMWARE_UPDATE), \
S(DISCOVER_IDENTITY), \
S(SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY), \
S(DISCOVER_SVIDS), \
S(DISCOVER_MODES), \
S(DFP_TO_UFP_ENTER_MODE), \
S(DFP_TO_UFP_EXIT_MODE), \
S(DFP_TO_CABLE_PLUG_ENTER_MODE), \
S(DFP_TO_CABLE_PLUG_EXIT_MODE), \
S(ATTENTION), \
S(BIST), \
S(UNSTRUCTURED_VDMS), \
S(STRUCTURED_VDMS), \
S(COUNTRY_INFO), \
S(COUNTRY_CODES)
#define GENERATE_ENUM(e) e
#define GENERATE_STRING(s) #s
enum tcpm_state {
FOREACH_STATE(GENERATE_ENUM)
};
static const char * const tcpm_states[] = {
FOREACH_STATE(GENERATE_STRING)
};
enum tcpm_ams {
FOREACH_AMS(GENERATE_ENUM)
};
static const char * const tcpm_ams_str[] = {
FOREACH_AMS(GENERATE_STRING)
};
enum vdm_states {
VDM_STATE_ERR_BUSY = -3,
VDM_STATE_ERR_SEND = -2,
VDM_STATE_ERR_TMOUT = -1,
VDM_STATE_DONE = 0,
/* Anything >0 represents an active state */
VDM_STATE_READY = 1,
VDM_STATE_BUSY = 2,
VDM_STATE_WAIT_RSP_BUSY = 3,
VDM_STATE_SEND_MESSAGE = 4,
};
enum pd_msg_request {
PD_MSG_NONE = 0,
PD_MSG_CTRL_REJECT,
PD_MSG_CTRL_WAIT,
PD_MSG_CTRL_NOT_SUPP,
PD_MSG_DATA_SINK_CAP,
PD_MSG_DATA_SOURCE_CAP,
};
enum adev_actions {
ADEV_NONE = 0,
ADEV_NOTIFY_USB_AND_QUEUE_VDM,
ADEV_QUEUE_VDM,
ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL,
ADEV_ATTENTION,
};
/*
* Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap.
* Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
* Version 1.2"
*/
enum frs_typec_current {
FRS_NOT_SUPPORTED,
FRS_DEFAULT_POWER,
FRS_5V_1P5A,
FRS_5V_3A,
};
/* Events from low level driver */
#define TCPM_CC_EVENT BIT(0)
#define TCPM_VBUS_EVENT BIT(1)
#define TCPM_RESET_EVENT BIT(2)
#define TCPM_FRS_EVENT BIT(3)
#define TCPM_SOURCING_VBUS BIT(4)
#define LOG_BUFFER_ENTRIES 1024
#define LOG_BUFFER_ENTRY_SIZE 128
/* Alternate mode support */
#define SVID_DISCOVERY_MAX 16
#define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX)
#define GET_SINK_CAP_RETRY_MS 100
#define SEND_DISCOVER_RETRY_MS 100
struct pd_mode_data {
int svid_index; /* current SVID index */
int nsvids;
u16 svids[SVID_DISCOVERY_MAX];
int altmodes; /* number of alternate modes */
struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX];
};
/*
* @min_volt: Actual min voltage at the local port
* @req_min_volt: Requested min voltage to the port partner
* @max_volt: Actual max voltage at the local port
* @req_max_volt: Requested max voltage to the port partner
* @max_curr: Actual max current at the local port
* @req_max_curr: Requested max current of the port partner
* @req_out_volt: Requested output voltage to the port partner
* @req_op_curr: Requested operating current to the port partner
* @supported: Parter has at least one APDO hence supports PPS
* @active: PPS mode is active
*/
struct pd_pps_data {
u32 min_volt;
u32 req_min_volt;
u32 max_volt;
u32 req_max_volt;
u32 max_curr;
u32 req_max_curr;
u32 req_out_volt;
u32 req_op_curr;
bool supported;
bool active;
};
struct tcpm_port {
struct device *dev;
struct mutex lock; /* tcpm state machine lock */
struct kthread_worker *wq;
struct typec_capability typec_caps;
struct typec_port *typec_port;
struct tcpc_dev *tcpc;
struct usb_role_switch *role_sw;
enum typec_role vconn_role;
enum typec_role pwr_role;
enum typec_data_role data_role;
enum typec_pwr_opmode pwr_opmode;
struct usb_pd_identity partner_ident;
struct typec_partner_desc partner_desc;
struct typec_partner *partner;
enum typec_cc_status cc_req;
enum typec_cc_status src_rp; /* work only if pd_supported == false */
enum typec_cc_status cc1;
enum typec_cc_status cc2;
enum typec_cc_polarity polarity;
bool attached;
bool connected;
bool registered;
bool pd_supported;
enum typec_port_type port_type;
/*
* Set to true when vbus is greater than VSAFE5V min.
* Set to false when vbus falls below vSinkDisconnect max threshold.
*/
bool vbus_present;
/*
* Set to true when vbus is less than VSAFE0V max.
* Set to false when vbus is greater than VSAFE0V max.
*/
bool vbus_vsafe0v;
bool vbus_never_low;
bool vbus_source;
bool vbus_charge;
/* Set to true when Discover_Identity Command is expected to be sent in Ready states. */
bool send_discover;
bool op_vsafe5v;
int try_role;
int try_snk_count;
int try_src_count;
enum pd_msg_request queued_message;
enum tcpm_state enter_state;
enum tcpm_state prev_state;
enum tcpm_state state;
enum tcpm_state delayed_state;
ktime_t delayed_runtime;
unsigned long delay_ms;
spinlock_t pd_event_lock;
u32 pd_events;
struct kthread_work event_work;
struct hrtimer state_machine_timer;
struct kthread_work state_machine;
struct hrtimer vdm_state_machine_timer;
struct kthread_work vdm_state_machine;
struct hrtimer enable_frs_timer;
struct kthread_work enable_frs;
struct hrtimer send_discover_timer;
struct kthread_work send_discover_work;
bool state_machine_running;
/* Set to true when VDM State Machine has following actions. */
bool vdm_sm_running;
struct completion tx_complete;
enum tcpm_transmit_status tx_status;
struct mutex swap_lock; /* swap command lock */
bool swap_pending;
bool non_pd_role_swap;
struct completion swap_complete;
int swap_status;
unsigned int negotiated_rev;
unsigned int message_id;
unsigned int caps_count;
unsigned int hard_reset_count;
bool pd_capable;
bool explicit_contract;
unsigned int rx_msgid;
/* USB PD objects */
struct usb_power_delivery *pd;
struct usb_power_delivery_capabilities *port_source_caps;
struct usb_power_delivery_capabilities *port_sink_caps;
struct usb_power_delivery *partner_pd;
struct usb_power_delivery_capabilities *partner_source_caps;
struct usb_power_delivery_capabilities *partner_sink_caps;
/* Partner capabilities/requests */
u32 sink_request;
u32 source_caps[PDO_MAX_OBJECTS];
unsigned int nr_source_caps;
u32 sink_caps[PDO_MAX_OBJECTS];
unsigned int nr_sink_caps;
/* Local capabilities */
u32 src_pdo[PDO_MAX_OBJECTS];
unsigned int nr_src_pdo;
u32 snk_pdo[PDO_MAX_OBJECTS];
unsigned int nr_snk_pdo;
u32 snk_vdo_v1[VDO_MAX_OBJECTS];
unsigned int nr_snk_vdo_v1;
u32 snk_vdo[VDO_MAX_OBJECTS];
unsigned int nr_snk_vdo;
unsigned int operating_snk_mw;
bool update_sink_caps;
/* Requested current / voltage to the port partner */
u32 req_current_limit;
u32 req_supply_voltage;
/* Actual current / voltage limit of the local port */
u32 current_limit;
u32 supply_voltage;
/* Used to export TA voltage and current */
struct power_supply *psy;
struct power_supply_desc psy_desc;
enum power_supply_usb_type usb_type;
u32 bist_request;
/* PD state for Vendor Defined Messages */
enum vdm_states vdm_state;
u32 vdm_retries;
/* next Vendor Defined Message to send */
u32 vdo_data[VDO_MAX_SIZE];
u8 vdo_count;
/* VDO to retry if UFP responder replied busy */
u32 vdo_retry;
/* PPS */
struct pd_pps_data pps_data;
struct completion pps_complete;
bool pps_pending;
int pps_status;
/* Alternate mode data */
struct pd_mode_data mode_data;
struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX];
struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX];
/* Deadline in jiffies to exit src_try_wait state */
unsigned long max_wait;
/* port belongs to a self powered device */
bool self_powered;
/* Sink FRS */
enum frs_typec_current new_source_frs_current;
/* Sink caps have been queried */
bool sink_cap_done;
/* Collision Avoidance and Atomic Message Sequence */
enum tcpm_state upcoming_state;
enum tcpm_ams ams;
enum tcpm_ams next_ams;
bool in_ams;
/* Auto vbus discharge status */
bool auto_vbus_discharge_enabled;
/*
* When set, port requests PD_P_SNK_STDBY_MW upon entering SNK_DISCOVERY and
* the actual current limit after RX of PD_CTRL_PSRDY for PD link,
* SNK_READY for non-pd link.
*/
bool slow_charger_loop;
#ifdef CONFIG_DEBUG_FS
struct dentry *dentry;
struct mutex logbuffer_lock; /* log buffer access lock */
int logbuffer_head;
int logbuffer_tail;
u8 *logbuffer[LOG_BUFFER_ENTRIES];
#endif
};
struct pd_rx_event {
struct kthread_work work;
struct tcpm_port *port;
struct pd_message msg;
};
static const char * const pd_rev[] = {
[PD_REV10] = "rev1",
[PD_REV20] = "rev2",
[PD_REV30] = "rev3",
};
#define tcpm_cc_is_sink(cc) \
((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
(cc) == TYPEC_CC_RP_3_0)
#define tcpm_port_is_sink(port) \
((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
(tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
#define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
#define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
#define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
#define tcpm_port_is_source(port) \
((tcpm_cc_is_source((port)->cc1) && \
!tcpm_cc_is_source((port)->cc2)) || \
(tcpm_cc_is_source((port)->cc2) && \
!tcpm_cc_is_source((port)->cc1)))
#define tcpm_port_is_debug(port) \
(tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
#define tcpm_port_is_audio(port) \
(tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
#define tcpm_port_is_audio_detached(port) \
((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
(tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
#define tcpm_try_snk(port) \
((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
(port)->port_type == TYPEC_PORT_DRP)
#define tcpm_try_src(port) \
((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
(port)->port_type == TYPEC_PORT_DRP)
#define tcpm_data_role_for_source(port) \
((port)->typec_caps.data == TYPEC_PORT_UFP ? \
TYPEC_DEVICE : TYPEC_HOST)
#define tcpm_data_role_for_sink(port) \
((port)->typec_caps.data == TYPEC_PORT_DFP ? \
TYPEC_HOST : TYPEC_DEVICE)
#define tcpm_sink_tx_ok(port) \
(tcpm_port_is_sink(port) && \
((port)->cc1 == TYPEC_CC_RP_3_0 || (port)->cc2 == TYPEC_CC_RP_3_0))
#define tcpm_wait_for_discharge(port) \
(((port)->auto_vbus_discharge_enabled && !(port)->vbus_vsafe0v) ? PD_T_SAFE_0V : 0)
static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
{
if (port->port_type == TYPEC_PORT_DRP) {
if (port->try_role == TYPEC_SINK)
return SNK_UNATTACHED;
else if (port->try_role == TYPEC_SOURCE)
return SRC_UNATTACHED;
/* Fall through to return SRC_UNATTACHED */
} else if (port->port_type == TYPEC_PORT_SNK) {
return SNK_UNATTACHED;
}
return SRC_UNATTACHED;
}
static bool tcpm_port_is_disconnected(struct tcpm_port *port)
{
return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
port->cc2 == TYPEC_CC_OPEN) ||
(port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
port->cc1 == TYPEC_CC_OPEN) ||
(port->polarity == TYPEC_POLARITY_CC2 &&
port->cc2 == TYPEC_CC_OPEN)));
}
/*
* Logging
*/
#ifdef CONFIG_DEBUG_FS
static bool tcpm_log_full(struct tcpm_port *port)
{
return port->logbuffer_tail ==
(port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
}
__printf(2, 0)
static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
{
char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
u64 ts_nsec = local_clock();
unsigned long rem_nsec;
mutex_lock(&port->logbuffer_lock);
if (!port->logbuffer[port->logbuffer_head]) {
port->logbuffer[port->logbuffer_head] =
kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
if (!port->logbuffer[port->logbuffer_head]) {
mutex_unlock(&port->logbuffer_lock);
return;
}
}
vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
if (tcpm_log_full(port)) {
port->logbuffer_head = max(port->logbuffer_head - 1, 0);
strcpy(tmpbuffer, "overflow");
}
if (port->logbuffer_head < 0 ||
port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
dev_warn(port->dev,
"Bad log buffer index %d\n", port->logbuffer_head);
goto abort;
}
if (!port->logbuffer[port->logbuffer_head]) {
dev_warn(port->dev,
"Log buffer index %d is NULL\n", port->logbuffer_head);
goto abort;
}
rem_nsec = do_div(ts_nsec, 1000000000);
scnprintf(port->logbuffer[port->logbuffer_head],
LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
(unsigned long)ts_nsec, rem_nsec / 1000,
tmpbuffer);
port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
abort:
mutex_unlock(&port->logbuffer_lock);
}
__printf(2, 3)
static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
{
va_list args;
/* Do not log while disconnected and unattached */
if (tcpm_port_is_disconnected(port) &&
(port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
port->state == TOGGLING))
return;
va_start(args, fmt);
_tcpm_log(port, fmt, args);
va_end(args);
}
__printf(2, 3)
static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
_tcpm_log(port, fmt, args);
va_end(args);
}
static void tcpm_log_source_caps(struct tcpm_port *port)
{
int i;
for (i = 0; i < port->nr_source_caps; i++) {
u32 pdo = port->source_caps[i];
enum pd_pdo_type type = pdo_type(pdo);
char msg[64];
switch (type) {
case PDO_TYPE_FIXED:
scnprintf(msg, sizeof(msg),
"%u mV, %u mA [%s%s%s%s%s%s]",
pdo_fixed_voltage(pdo),
pdo_max_current(pdo),
(pdo & PDO_FIXED_DUAL_ROLE) ?
"R" : "",
(pdo & PDO_FIXED_SUSPEND) ?
"S" : "",
(pdo & PDO_FIXED_HIGHER_CAP) ?
"H" : "",
(pdo & PDO_FIXED_USB_COMM) ?
"U" : "",
(pdo & PDO_FIXED_DATA_SWAP) ?
"D" : "",
(pdo & PDO_FIXED_EXTPOWER) ?
"E" : "");
break;
case PDO_TYPE_VAR:
scnprintf(msg, sizeof(msg),
"%u-%u mV, %u mA",
pdo_min_voltage(pdo),
pdo_max_voltage(pdo),
pdo_max_current(pdo));
break;
case PDO_TYPE_BATT:
scnprintf(msg, sizeof(msg),
"%u-%u mV, %u mW",
pdo_min_voltage(pdo),
pdo_max_voltage(pdo),
pdo_max_power(pdo));
break;
case PDO_TYPE_APDO:
if (pdo_apdo_type(pdo) == APDO_TYPE_PPS)
scnprintf(msg, sizeof(msg),
"%u-%u mV, %u mA",
pdo_pps_apdo_min_voltage(pdo),
pdo_pps_apdo_max_voltage(pdo),
pdo_pps_apdo_max_current(pdo));
else
strcpy(msg, "undefined APDO");
break;
default:
strcpy(msg, "undefined");
break;
}
tcpm_log(port, " PDO %d: type %d, %s",
i, type, msg);
}
}
static int tcpm_debug_show(struct seq_file *s, void *v)
{
struct tcpm_port *port = (struct tcpm_port *)s->private;
int tail;
mutex_lock(&port->logbuffer_lock);
tail = port->logbuffer_tail;
while (tail != port->logbuffer_head) {
seq_printf(s, "%s\n", port->logbuffer[tail]);
tail = (tail + 1) % LOG_BUFFER_ENTRIES;
}
if (!seq_has_overflowed(s))
port->logbuffer_tail = tail;
mutex_unlock(&port->logbuffer_lock);
return 0;
}
DEFINE_SHOW_ATTRIBUTE(tcpm_debug);
static void tcpm_debugfs_init(struct tcpm_port *port)
{
char name[NAME_MAX];
mutex_init(&port->logbuffer_lock);
snprintf(name, NAME_MAX, "tcpm-%s", dev_name(port->dev));
port->dentry = debugfs_create_dir(name, usb_debug_root);
debugfs_create_file("log", S_IFREG | 0444, port->dentry, port,
&tcpm_debug_fops);
}
static void tcpm_debugfs_exit(struct tcpm_port *port)
{
int i;
mutex_lock(&port->logbuffer_lock);
for (i = 0; i < LOG_BUFFER_ENTRIES; i++) {
kfree(port->logbuffer[i]);
port->logbuffer[i] = NULL;
}
mutex_unlock(&port->logbuffer_lock);
debugfs_remove(port->dentry);
}
#else
__printf(2, 3)
static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
__printf(2, 3)
static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
static void tcpm_log_source_caps(struct tcpm_port *port) { }
static void tcpm_debugfs_init(const struct tcpm_port *port) { }
static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
#endif
static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
{
tcpm_log(port, "cc:=%d", cc);
port->cc_req = cc;
port->tcpc->set_cc(port->tcpc, cc);
}
static int tcpm_enable_auto_vbus_discharge(struct tcpm_port *port, bool enable)
{
int ret = 0;
if (port->tcpc->enable_auto_vbus_discharge) {
ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, enable);
tcpm_log_force(port, "%s vbus discharge ret:%d", enable ? "enable" : "disable",
ret);
if (!ret)
port->auto_vbus_discharge_enabled = enable;
}
return ret;
}
static void tcpm_apply_rc(struct tcpm_port *port)
{
/*
* TCPCI: Move to APPLY_RC state to prevent disconnect during PR_SWAP
* when Vbus auto discharge on disconnect is enabled.
*/
if (port->tcpc->enable_auto_vbus_discharge && port->tcpc->apply_rc) {
tcpm_log(port, "Apply_RC");
port->tcpc->apply_rc(port->tcpc, port->cc_req, port->polarity);
tcpm_enable_auto_vbus_discharge(port, false);
}
}
/*
* Determine RP value to set based on maximum current supported
* by a port if configured as source.
* Returns CC value to report to link partner.
*/
static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
{
const u32 *src_pdo = port->src_pdo;
int nr_pdo = port->nr_src_pdo;
int i;
if (!port->pd_supported)
return port->src_rp;
/*
* Search for first entry with matching voltage.
* It should report the maximum supported current.
*/
for (i = 0; i < nr_pdo; i++) {
const u32 pdo = src_pdo[i];
if (pdo_type(pdo) == PDO_TYPE_FIXED &&
pdo_fixed_voltage(pdo) == 5000) {
unsigned int curr = pdo_max_current(pdo);
if (curr >= 3000)
return TYPEC_CC_RP_3_0;
else if (curr >= 1500)
return TYPEC_CC_RP_1_5;
return TYPEC_CC_RP_DEF;
}
}
return TYPEC_CC_RP_DEF;
}
static void tcpm_ams_finish(struct tcpm_port *port)
{
tcpm_log(port, "AMS %s finished", tcpm_ams_str[port->ams]);
if (port->pd_capable && port->pwr_role == TYPEC_SOURCE) {
if (port->negotiated_rev >= PD_REV30)
tcpm_set_cc(port, SINK_TX_OK);
else
tcpm_set_cc(port, SINK_TX_NG);
} else if (port->pwr_role == TYPEC_SOURCE) {
tcpm_set_cc(port, tcpm_rp_cc(port));
}
port->in_ams = false;
port->ams = NONE_AMS;
}
static int tcpm_pd_transmit(struct tcpm_port *port,
enum tcpm_transmit_type type,
const struct pd_message *msg)
{
unsigned long timeout;
int ret;
if (msg)
tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
else
tcpm_log(port, "PD TX, type: %#x", type);
reinit_completion(&port->tx_complete);
ret = port->tcpc->pd_transmit(port->tcpc, type, msg, port->negotiated_rev);
if (ret < 0)
return ret;
mutex_unlock(&port->lock);
timeout = wait_for_completion_timeout(&port->tx_complete,
msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
mutex_lock(&port->lock);
if (!timeout)
return -ETIMEDOUT;
switch (port->tx_status) {
case TCPC_TX_SUCCESS:
port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
/*
* USB PD rev 2.0, 8.3.2.2.1:
* USB PD rev 3.0, 8.3.2.1.3:
* "... Note that every AMS is Interruptible until the first
* Message in the sequence has been successfully sent (GoodCRC
* Message received)."
*/
if (port->ams != NONE_AMS)
port->in_ams = true;
break;
case TCPC_TX_DISCARDED:
ret = -EAGAIN;
break;
case TCPC_TX_FAILED:
default:
ret = -EIO;
break;
}
/* Some AMS don't expect responses. Finish them here. */
if (port->ams == ATTENTION || port->ams == SOURCE_ALERT)
tcpm_ams_finish(port);
return ret;
}
void tcpm_pd_transmit_complete(struct tcpm_port *port,
enum tcpm_transmit_status status)
{
tcpm_log(port, "PD TX complete, status: %u", status);
port->tx_status = status;
complete(&port->tx_complete);
}
EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
static int tcpm_mux_set(struct tcpm_port *port, int state,
enum usb_role usb_role,
enum typec_orientation orientation)
{
int ret;
tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d",
state, usb_role, orientation);
ret = typec_set_orientation(port->typec_port, orientation);
if (ret)
return ret;
if (port->role_sw) {
ret = usb_role_switch_set_role(port->role_sw, usb_role);
if (ret)
return ret;
}
return typec_set_mode(port->typec_port, state);
}
static int tcpm_set_polarity(struct tcpm_port *port,
enum typec_cc_polarity polarity)
{
int ret;
tcpm_log(port, "polarity %d", polarity);
ret = port->tcpc->set_polarity(port->tcpc, polarity);
if (ret < 0)
return ret;
port->polarity = polarity;
return 0;
}
static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
{
int ret;
tcpm_log(port, "vconn:=%d", enable);
ret = port->tcpc->set_vconn(port->tcpc, enable);
if (!ret) {
port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
typec_set_vconn_role(port->typec_port, port->vconn_role);
}
return ret;
}
static u32 tcpm_get_current_limit(struct tcpm_port *port)
{
enum typec_cc_status cc;
u32 limit;
cc = port->polarity ? port->cc2 : port->cc1;
switch (cc) {
case TYPEC_CC_RP_1_5:
limit = 1500;
break;
case TYPEC_CC_RP_3_0:
limit = 3000;
break;