1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2015-2017 Google, Inc
4 *
5 * USB Power Delivery protocol stack.
6 */
7
8 #include <linux/completion.h>
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/hrtimer.h>
12 #include <linux/jiffies.h>
13 #include <linux/kernel.h>
14 #include <linux/kthread.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/power_supply.h>
18 #include <linux/proc_fs.h>
19 #include <linux/property.h>
20 #include <linux/sched/clock.h>
21 #include <linux/seq_file.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/usb.h>
25 #include <linux/usb/pd.h>
26 #include <linux/usb/pd_ado.h>
27 #include <linux/usb/pd_bdo.h>
28 #include <linux/usb/pd_ext_sdb.h>
29 #include <linux/usb/pd_vdo.h>
30 #include <linux/usb/role.h>
31 #include <linux/usb/tcpm.h>
32 #include <linux/usb/typec_altmode.h>
33
34 #include <uapi/linux/sched/types.h>
35
36 #define FOREACH_STATE(S) \
37 S(INVALID_STATE), \
38 S(TOGGLING), \
39 S(CHECK_CONTAMINANT), \
40 S(SRC_UNATTACHED), \
41 S(SRC_ATTACH_WAIT), \
42 S(SRC_ATTACHED), \
43 S(SRC_STARTUP), \
44 S(SRC_SEND_CAPABILITIES), \
45 S(SRC_SEND_CAPABILITIES_TIMEOUT), \
46 S(SRC_NEGOTIATE_CAPABILITIES), \
47 S(SRC_TRANSITION_SUPPLY), \
48 S(SRC_READY), \
49 S(SRC_WAIT_NEW_CAPABILITIES), \
50 \
51 S(SNK_UNATTACHED), \
52 S(SNK_ATTACH_WAIT), \
53 S(SNK_DEBOUNCED), \
54 S(SNK_ATTACHED), \
55 S(SNK_STARTUP), \
56 S(SNK_DISCOVERY), \
57 S(SNK_DISCOVERY_DEBOUNCE), \
58 S(SNK_DISCOVERY_DEBOUNCE_DONE), \
59 S(SNK_WAIT_CAPABILITIES), \
60 S(SNK_NEGOTIATE_CAPABILITIES), \
61 S(SNK_NEGOTIATE_PPS_CAPABILITIES), \
62 S(SNK_TRANSITION_SINK), \
63 S(SNK_TRANSITION_SINK_VBUS), \
64 S(SNK_READY), \
65 \
66 S(ACC_UNATTACHED), \
67 S(DEBUG_ACC_ATTACHED), \
68 S(AUDIO_ACC_ATTACHED), \
69 S(AUDIO_ACC_DEBOUNCE), \
70 \
71 S(HARD_RESET_SEND), \
72 S(HARD_RESET_START), \
73 S(SRC_HARD_RESET_VBUS_OFF), \
74 S(SRC_HARD_RESET_VBUS_ON), \
75 S(SNK_HARD_RESET_SINK_OFF), \
76 S(SNK_HARD_RESET_WAIT_VBUS), \
77 S(SNK_HARD_RESET_SINK_ON), \
78 \
79 S(SOFT_RESET), \
80 S(SRC_SOFT_RESET_WAIT_SNK_TX), \
81 S(SNK_SOFT_RESET), \
82 S(SOFT_RESET_SEND), \
83 \
84 S(DR_SWAP_ACCEPT), \
85 S(DR_SWAP_SEND), \
86 S(DR_SWAP_SEND_TIMEOUT), \
87 S(DR_SWAP_CANCEL), \
88 S(DR_SWAP_CHANGE_DR), \
89 \
90 S(PR_SWAP_ACCEPT), \
91 S(PR_SWAP_SEND), \
92 S(PR_SWAP_SEND_TIMEOUT), \
93 S(PR_SWAP_CANCEL), \
94 S(PR_SWAP_START), \
95 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
96 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
97 S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
98 S(PR_SWAP_SRC_SNK_SINK_ON), \
99 S(PR_SWAP_SNK_SRC_SINK_OFF), \
100 S(PR_SWAP_SNK_SRC_SOURCE_ON), \
101 S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \
102 \
103 S(VCONN_SWAP_ACCEPT), \
104 S(VCONN_SWAP_SEND), \
105 S(VCONN_SWAP_SEND_TIMEOUT), \
106 S(VCONN_SWAP_CANCEL), \
107 S(VCONN_SWAP_START), \
108 S(VCONN_SWAP_WAIT_FOR_VCONN), \
109 S(VCONN_SWAP_TURN_ON_VCONN), \
110 S(VCONN_SWAP_TURN_OFF_VCONN), \
111 \
112 S(FR_SWAP_SEND), \
113 S(FR_SWAP_SEND_TIMEOUT), \
114 S(FR_SWAP_SNK_SRC_TRANSITION_TO_OFF), \
115 S(FR_SWAP_SNK_SRC_NEW_SINK_READY), \
116 S(FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED), \
117 S(FR_SWAP_CANCEL), \
118 \
119 S(SNK_TRY), \
120 S(SNK_TRY_WAIT), \
121 S(SNK_TRY_WAIT_DEBOUNCE), \
122 S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \
123 S(SRC_TRYWAIT), \
124 S(SRC_TRYWAIT_DEBOUNCE), \
125 S(SRC_TRYWAIT_UNATTACHED), \
126 \
127 S(SRC_TRY), \
128 S(SRC_TRY_WAIT), \
129 S(SRC_TRY_DEBOUNCE), \
130 S(SNK_TRYWAIT), \
131 S(SNK_TRYWAIT_DEBOUNCE), \
132 S(SNK_TRYWAIT_VBUS), \
133 S(BIST_RX), \
134 \
135 S(GET_STATUS_SEND), \
136 S(GET_STATUS_SEND_TIMEOUT), \
137 S(GET_PPS_STATUS_SEND), \
138 S(GET_PPS_STATUS_SEND_TIMEOUT), \
139 \
140 S(GET_SINK_CAP), \
141 S(GET_SINK_CAP_TIMEOUT), \
142 \
143 S(ERROR_RECOVERY), \
144 S(PORT_RESET), \
145 S(PORT_RESET_WAIT_OFF), \
146 \
147 S(AMS_START), \
148 S(CHUNK_NOT_SUPP)
149
150 #define FOREACH_AMS(S) \
151 S(NONE_AMS), \
152 S(POWER_NEGOTIATION), \
153 S(GOTOMIN), \
154 S(SOFT_RESET_AMS), \
155 S(HARD_RESET), \
156 S(CABLE_RESET), \
157 S(GET_SOURCE_CAPABILITIES), \
158 S(GET_SINK_CAPABILITIES), \
159 S(POWER_ROLE_SWAP), \
160 S(FAST_ROLE_SWAP), \
161 S(DATA_ROLE_SWAP), \
162 S(VCONN_SWAP), \
163 S(SOURCE_ALERT), \
164 S(GETTING_SOURCE_EXTENDED_CAPABILITIES),\
165 S(GETTING_SOURCE_SINK_STATUS), \
166 S(GETTING_BATTERY_CAPABILITIES), \
167 S(GETTING_BATTERY_STATUS), \
168 S(GETTING_MANUFACTURER_INFORMATION), \
169 S(SECURITY), \
170 S(FIRMWARE_UPDATE), \
171 S(DISCOVER_IDENTITY), \
172 S(SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY), \
173 S(DISCOVER_SVIDS), \
174 S(DISCOVER_MODES), \
175 S(DFP_TO_UFP_ENTER_MODE), \
176 S(DFP_TO_UFP_EXIT_MODE), \
177 S(DFP_TO_CABLE_PLUG_ENTER_MODE), \
178 S(DFP_TO_CABLE_PLUG_EXIT_MODE), \
179 S(ATTENTION), \
180 S(BIST), \
181 S(UNSTRUCTURED_VDMS), \
182 S(STRUCTURED_VDMS), \
183 S(COUNTRY_INFO), \
184 S(COUNTRY_CODES)
185
186 #define GENERATE_ENUM(e) e
187 #define GENERATE_STRING(s) #s
188
189 enum tcpm_state {
190 FOREACH_STATE(GENERATE_ENUM)
191 };
192
193 static const char * const tcpm_states[] = {
194 FOREACH_STATE(GENERATE_STRING)
195 };
196
197 enum tcpm_ams {
198 FOREACH_AMS(GENERATE_ENUM)
199 };
200
201 static const char * const tcpm_ams_str[] = {
202 FOREACH_AMS(GENERATE_STRING)
203 };
204
205 enum vdm_states {
206 VDM_STATE_ERR_BUSY = -3,
207 VDM_STATE_ERR_SEND = -2,
208 VDM_STATE_ERR_TMOUT = -1,
209 VDM_STATE_DONE = 0,
210 /* Anything >0 represents an active state */
211 VDM_STATE_READY = 1,
212 VDM_STATE_BUSY = 2,
213 VDM_STATE_WAIT_RSP_BUSY = 3,
214 VDM_STATE_SEND_MESSAGE = 4,
215 };
216
217 enum pd_msg_request {
218 PD_MSG_NONE = 0,
219 PD_MSG_CTRL_REJECT,
220 PD_MSG_CTRL_WAIT,
221 PD_MSG_CTRL_NOT_SUPP,
222 PD_MSG_DATA_SINK_CAP,
223 PD_MSG_DATA_SOURCE_CAP,
224 };
225
226 enum adev_actions {
227 ADEV_NONE = 0,
228 ADEV_NOTIFY_USB_AND_QUEUE_VDM,
229 ADEV_QUEUE_VDM,
230 ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL,
231 ADEV_ATTENTION,
232 };
233
234 /*
235 * Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap.
236 * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
237 * Version 1.2"
238 */
239 enum frs_typec_current {
240 FRS_NOT_SUPPORTED,
241 FRS_DEFAULT_POWER,
242 FRS_5V_1P5A,
243 FRS_5V_3A,
244 };
245
246 /* Events from low level driver */
247
248 #define TCPM_CC_EVENT BIT(0)
249 #define TCPM_VBUS_EVENT BIT(1)
250 #define TCPM_RESET_EVENT BIT(2)
251 #define TCPM_FRS_EVENT BIT(3)
252 #define TCPM_SOURCING_VBUS BIT(4)
253 #define TCPM_PORT_CLEAN BIT(5)
254
255 #define LOG_BUFFER_ENTRIES 1024
256 #define LOG_BUFFER_ENTRY_SIZE 128
257
258 /* Alternate mode support */
259
260 #define SVID_DISCOVERY_MAX 16
261 #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX)
262
263 #define GET_SINK_CAP_RETRY_MS 100
264 #define SEND_DISCOVER_RETRY_MS 100
265
266 struct pd_mode_data {
267 int svid_index; /* current SVID index */
268 int nsvids;
269 u16 svids[SVID_DISCOVERY_MAX];
270 int altmodes; /* number of alternate modes */
271 struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX];
272 };
273
274 /*
275 * @min_volt: Actual min voltage at the local port
276 * @req_min_volt: Requested min voltage to the port partner
277 * @max_volt: Actual max voltage at the local port
278 * @req_max_volt: Requested max voltage to the port partner
279 * @max_curr: Actual max current at the local port
280 * @req_max_curr: Requested max current of the port partner
281 * @req_out_volt: Requested output voltage to the port partner
282 * @req_op_curr: Requested operating current to the port partner
283 * @supported: Parter has at least one APDO hence supports PPS
284 * @active: PPS mode is active
285 */
286 struct pd_pps_data {
287 u32 min_volt;
288 u32 req_min_volt;
289 u32 max_volt;
290 u32 req_max_volt;
291 u32 max_curr;
292 u32 req_max_curr;
293 u32 req_out_volt;
294 u32 req_op_curr;
295 bool supported;
296 bool active;
297 };
298
299 struct tcpm_port {
300 struct device *dev;
301
302 struct mutex lock; /* tcpm state machine lock */
303 struct kthread_worker *wq;
304
305 struct typec_capability typec_caps;
306 struct typec_port *typec_port;
307
308 struct tcpc_dev *tcpc;
309 struct usb_role_switch *role_sw;
310
311 enum typec_role vconn_role;
312 enum typec_role pwr_role;
313 enum typec_data_role data_role;
314 enum typec_pwr_opmode pwr_opmode;
315
316 struct usb_pd_identity partner_ident;
317 struct typec_partner_desc partner_desc;
318 struct typec_partner *partner;
319
320 enum typec_cc_status cc_req;
321 enum typec_cc_status src_rp; /* work only if pd_supported == false */
322
323 enum typec_cc_status cc1;
324 enum typec_cc_status cc2;
325 enum typec_cc_polarity polarity;
326
327 bool attached;
328 bool connected;
329 bool registered;
330 bool pd_supported;
331 enum typec_port_type port_type;
332
333 /*
334 * Set to true when vbus is greater than VSAFE5V min.
335 * Set to false when vbus falls below vSinkDisconnect max threshold.
336 */
337 bool vbus_present;
338
339 /*
340 * Set to true when vbus is less than VSAFE0V max.
341 * Set to false when vbus is greater than VSAFE0V max.
342 */
343 bool vbus_vsafe0v;
344
345 bool vbus_never_low;
346 bool vbus_source;
347 bool vbus_charge;
348
349 /* Set to true when Discover_Identity Command is expected to be sent in Ready states. */
350 bool send_discover;
351 bool op_vsafe5v;
352
353 int try_role;
354 int try_snk_count;
355 int try_src_count;
356
357 enum pd_msg_request queued_message;
358
359 enum tcpm_state enter_state;
360 enum tcpm_state prev_state;
361 enum tcpm_state state;
362 enum tcpm_state delayed_state;
363 ktime_t delayed_runtime;
364 unsigned long delay_ms;
365
366 spinlock_t pd_event_lock;
367 u32 pd_events;
368
369 struct kthread_work event_work;
370 struct hrtimer state_machine_timer;
371 struct kthread_work state_machine;
372 struct hrtimer vdm_state_machine_timer;
373 struct kthread_work vdm_state_machine;
374 struct hrtimer enable_frs_timer;
375 struct kthread_work enable_frs;
376 struct hrtimer send_discover_timer;
377 struct kthread_work send_discover_work;
378 bool state_machine_running;
379 /* Set to true when VDM State Machine has following actions. */
380 bool vdm_sm_running;
381
382 struct completion tx_complete;
383 enum tcpm_transmit_status tx_status;
384
385 struct mutex swap_lock; /* swap command lock */
386 bool swap_pending;
387 bool non_pd_role_swap;
388 struct completion swap_complete;
389 int swap_status;
390
391 unsigned int negotiated_rev;
392 unsigned int message_id;
393 unsigned int caps_count;
394 unsigned int hard_reset_count;
395 bool pd_capable;
396 bool explicit_contract;
397 unsigned int rx_msgid;
398
399 /* USB PD objects */
400 struct usb_power_delivery *pd;
401 struct usb_power_delivery_capabilities *port_source_caps;
402 struct usb_power_delivery_capabilities *port_sink_caps;
403 struct usb_power_delivery *partner_pd;
404 struct usb_power_delivery_capabilities *partner_source_caps;
405 struct usb_power_delivery_capabilities *partner_sink_caps;
406
407 /* Partner capabilities/requests */
408 u32 sink_request;
409 u32 source_caps[PDO_MAX_OBJECTS];
410 unsigned int nr_source_caps;
411 u32 sink_caps[PDO_MAX_OBJECTS];
412 unsigned int nr_sink_caps;
413
414 /* Local capabilities */
415 u32 src_pdo[PDO_MAX_OBJECTS];
416 unsigned int nr_src_pdo;
417 u32 snk_pdo[PDO_MAX_OBJECTS];
418 unsigned int nr_snk_pdo;
419 u32 snk_vdo_v1[VDO_MAX_OBJECTS];
420 unsigned int nr_snk_vdo_v1;
421 u32 snk_vdo[VDO_MAX_OBJECTS];
422 unsigned int nr_snk_vdo;
423
424 unsigned int operating_snk_mw;
425 bool update_sink_caps;
426
427 /* Requested current / voltage to the port partner */
428 u32 req_current_limit;
429 u32 req_supply_voltage;
430 /* Actual current / voltage limit of the local port */
431 u32 current_limit;
432 u32 supply_voltage;
433
434 /* Used to export TA voltage and current */
435 struct power_supply *psy;
436 struct power_supply_desc psy_desc;
437 enum power_supply_usb_type usb_type;
438
439 u32 bist_request;
440
441 /* PD state for Vendor Defined Messages */
442 enum vdm_states vdm_state;
443 u32 vdm_retries;
444 /* next Vendor Defined Message to send */
445 u32 vdo_data[VDO_MAX_SIZE];
446 u8 vdo_count;
447 /* VDO to retry if UFP responder replied busy */
448 u32 vdo_retry;
449
450 /* PPS */
451 struct pd_pps_data pps_data;
452 struct completion pps_complete;
453 bool pps_pending;
454 int pps_status;
455
456 /* Alternate mode data */
457 struct pd_mode_data mode_data;
458 struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX];
459 struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX];
460
461 /* Deadline in jiffies to exit src_try_wait state */
462 unsigned long max_wait;
463
464 /* port belongs to a self powered device */
465 bool self_powered;
466
467 /* Sink FRS */
468 enum frs_typec_current new_source_frs_current;
469
470 /* Sink caps have been queried */
471 bool sink_cap_done;
472
473 /* Collision Avoidance and Atomic Message Sequence */
474 enum tcpm_state upcoming_state;
475 enum tcpm_ams ams;
476 enum tcpm_ams next_ams;
477 bool in_ams;
478
479 /* Auto vbus discharge status */
480 bool auto_vbus_discharge_enabled;
481
482 /*
483 * When set, port requests PD_P_SNK_STDBY_MW upon entering SNK_DISCOVERY and
484 * the actual current limit after RX of PD_CTRL_PSRDY for PD link,
485 * SNK_READY for non-pd link.
486 */
487 bool slow_charger_loop;
488
489 /*
490 * When true indicates that the lower level drivers indicate potential presence
491 * of contaminant in the connector pins based on the tcpm state machine
492 * transitions.
493 */
494 bool potential_contaminant;
495 #ifdef CONFIG_DEBUG_FS
496 struct dentry *dentry;
497 struct mutex logbuffer_lock; /* log buffer access lock */
498 int logbuffer_head;
499 int logbuffer_tail;
500 u8 *logbuffer[LOG_BUFFER_ENTRIES];
501 #endif
502 };
503
504 struct pd_rx_event {
505 struct kthread_work work;
506 struct tcpm_port *port;
507 struct pd_message msg;
508 };
509
510 static const char * const pd_rev[] = {
511 [PD_REV10] = "rev1",
512 [PD_REV20] = "rev2",
513 [PD_REV30] = "rev3",
514 };
515
516 #define tcpm_cc_is_sink(cc) \
517 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
518 (cc) == TYPEC_CC_RP_3_0)
519
520 #define tcpm_port_is_sink(port) \
521 ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
522 (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
523
524 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
525 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
526 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
527
528 #define tcpm_port_is_source(port) \
529 ((tcpm_cc_is_source((port)->cc1) && \
530 !tcpm_cc_is_source((port)->cc2)) || \
531 (tcpm_cc_is_source((port)->cc2) && \
532 !tcpm_cc_is_source((port)->cc1)))
533
534 #define tcpm_port_is_debug(port) \
535 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
536
537 #define tcpm_port_is_audio(port) \
538 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
539
540 #define tcpm_port_is_audio_detached(port) \
541 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
542 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
543
544 #define tcpm_try_snk(port) \
545 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
546 (port)->port_type == TYPEC_PORT_DRP)
547
548 #define tcpm_try_src(port) \
549 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
550 (port)->port_type == TYPEC_PORT_DRP)
551
552 #define tcpm_data_role_for_source(port) \
553 ((port)->typec_caps.data == TYPEC_PORT_UFP ? \
554 TYPEC_DEVICE : TYPEC_HOST)
555
556 #define tcpm_data_role_for_sink(port) \
557 ((port)->typec_caps.data == TYPEC_PORT_DFP ? \
558 TYPEC_HOST : TYPEC_DEVICE)
559
560 #define tcpm_sink_tx_ok(port) \
561 (tcpm_port_is_sink(port) && \
562 ((port)->cc1 == TYPEC_CC_RP_3_0 || (port)->cc2 == TYPEC_CC_RP_3_0))
563
564 #define tcpm_wait_for_discharge(port) \
565 (((port)->auto_vbus_discharge_enabled && !(port)->vbus_vsafe0v) ? PD_T_SAFE_0V : 0)
566
tcpm_default_state(struct tcpm_port * port)567 static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
568 {
569 if (port->port_type == TYPEC_PORT_DRP) {
570 if (port->try_role == TYPEC_SINK)
571 return SNK_UNATTACHED;
572 else if (port->try_role == TYPEC_SOURCE)
573 return SRC_UNATTACHED;
574 /* Fall through to return SRC_UNATTACHED */
575 } else if (port->port_type == TYPEC_PORT_SNK) {
576 return SNK_UNATTACHED;
577 }
578 return SRC_UNATTACHED;
579 }
580
tcpm_port_is_disconnected(struct tcpm_port * port)581 static bool tcpm_port_is_disconnected(struct tcpm_port *port)
582 {
583 return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
584 port->cc2 == TYPEC_CC_OPEN) ||
585 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
586 port->cc1 == TYPEC_CC_OPEN) ||
587 (port->polarity == TYPEC_POLARITY_CC2 &&
588 port->cc2 == TYPEC_CC_OPEN)));
589 }
590
591 /*
592 * Logging
593 */
594
595 #ifdef CONFIG_DEBUG_FS
596
tcpm_log_full(struct tcpm_port * port)597 static bool tcpm_log_full(struct tcpm_port *port)
598 {
599 return port->logbuffer_tail ==
600 (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
601 }
602
603 __printf(2, 0)
_tcpm_log(struct tcpm_port * port,const char * fmt,va_list args)604 static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
605 {
606 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
607 u64 ts_nsec = local_clock();
608 unsigned long rem_nsec;
609
610 mutex_lock(&port->logbuffer_lock);
611 if (!port->logbuffer[port->logbuffer_head]) {
612 port->logbuffer[port->logbuffer_head] =
613 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
614 if (!port->logbuffer[port->logbuffer_head]) {
615 mutex_unlock(&port->logbuffer_lock);
616 return;
617 }
618 }
619
620 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
621
622 if (tcpm_log_full(port)) {
623 port->logbuffer_head = max(port->logbuffer_head - 1, 0);
624 strcpy(tmpbuffer, "overflow");
625 }
626
627 if (port->logbuffer_head < 0 ||
628 port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
629 dev_warn(port->dev,
630 "Bad log buffer index %d\n", port->logbuffer_head);
631 goto abort;
632 }
633
634 if (!port->logbuffer[port->logbuffer_head]) {
635 dev_warn(port->dev,
636 "Log buffer index %d is NULL\n", port->logbuffer_head);
637 goto abort;
638 }
639
640 rem_nsec = do_div(ts_nsec, 1000000000);
641 scnprintf(port->logbuffer[port->logbuffer_head],
642 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
643 (unsigned long)ts_nsec, rem_nsec / 1000,
644 tmpbuffer);
645 port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
646
647 abort:
648 mutex_unlock(&port->logbuffer_lock);
649 }
650
651 __printf(2, 3)
tcpm_log(struct tcpm_port * port,const char * fmt,...)652 static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
653 {
654 va_list args;
655
656 /* Do not log while disconnected and unattached */
657 if (tcpm_port_is_disconnected(port) &&
658 (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
659 port->state == TOGGLING || port->state == CHECK_CONTAMINANT))
660 return;
661
662 va_start(args, fmt);
663 _tcpm_log(port, fmt, args);
664 va_end(args);
665 }
666
667 __printf(2, 3)
tcpm_log_force(struct tcpm_port * port,const char * fmt,...)668 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
669 {
670 va_list args;
671
672 va_start(args, fmt);
673 _tcpm_log(port, fmt, args);
674 va_end(args);
675 }
676
tcpm_log_source_caps(struct tcpm_port * port)677 static void tcpm_log_source_caps(struct tcpm_port *port)
678 {
679 int i;
680
681 for (i = 0; i < port->nr_source_caps; i++) {
682 u32 pdo = port->source_caps[i];
683 enum pd_pdo_type type = pdo_type(pdo);
684 char msg[64];
685
686 switch (type) {
687 case PDO_TYPE_FIXED:
688 scnprintf(msg, sizeof(msg),
689 "%u mV, %u mA [%s%s%s%s%s%s]",
690 pdo_fixed_voltage(pdo),
691 pdo_max_current(pdo),
692 (pdo & PDO_FIXED_DUAL_ROLE) ?
693 "R" : "",
694 (pdo & PDO_FIXED_SUSPEND) ?
695 "S" : "",
696 (pdo & PDO_FIXED_HIGHER_CAP) ?
697 "H" : "",
698 (pdo & PDO_FIXED_USB_COMM) ?
699 "U" : "",
700 (pdo & PDO_FIXED_DATA_SWAP) ?
701 "D" : "",
702 (pdo & PDO_FIXED_EXTPOWER) ?
703 "E" : "");
704 break;
705 case PDO_TYPE_VAR:
706 scnprintf(msg, sizeof(msg),
707 "%u-%u mV, %u mA",
708 pdo_min_voltage(pdo),
709 pdo_max_voltage(pdo),
710 pdo_max_current(pdo));
711 break;
712 case PDO_TYPE_BATT:
713 scnprintf(msg, sizeof(msg),
714 "%u-%u mV, %u mW",
715 pdo_min_voltage(pdo),
716 pdo_max_voltage(pdo),
717 pdo_max_power(pdo));
718 break;
719 case PDO_TYPE_APDO:
720 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS)
721 scnprintf(msg, sizeof(msg),
722 "%u-%u mV, %u mA",
723 pdo_pps_apdo_min_voltage(pdo),
724 pdo_pps_apdo_max_voltage(pdo),
725 pdo_pps_apdo_max_current(pdo));
726 else
727 strcpy(msg, "undefined APDO");
728 break;
729 default:
730 strcpy(msg, "undefined");
731 break;
732 }
733 tcpm_log(port, " PDO %d: type %d, %s",
734 i, type, msg);
735 }
736 }
737
tcpm_debug_show(struct seq_file * s,void * v)738 static int tcpm_debug_show(struct seq_file *s, void *v)
739 {
740 struct tcpm_port *port = s->private;
741 int tail;
742
743 mutex_lock(&port->logbuffer_lock);
744 tail = port->logbuffer_tail;
745 while (tail != port->logbuffer_head) {
746 seq_printf(s, "%s\n", port->logbuffer[tail]);
747 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
748 }
749 if (!seq_has_overflowed(s))
750 port->logbuffer_tail = tail;
751 mutex_unlock(&port->logbuffer_lock);
752
753 return 0;
754 }
755 DEFINE_SHOW_ATTRIBUTE(tcpm_debug);
756
tcpm_debugfs_init(struct tcpm_port * port)757 static void tcpm_debugfs_init(struct tcpm_port *port)
758 {
759 char name[NAME_MAX];
760
761 mutex_init(&port->logbuffer_lock);
762 snprintf(name, NAME_MAX, "tcpm-%s", dev_name(port->dev));
763 port->dentry = debugfs_create_dir(name, usb_debug_root);
764 debugfs_create_file("log", S_IFREG | 0444, port->dentry, port,
765 &tcpm_debug_fops);
766 }
767
tcpm_debugfs_exit(struct tcpm_port * port)768 static void tcpm_debugfs_exit(struct tcpm_port *port)
769 {
770 int i;
771
772 mutex_lock(&port->logbuffer_lock);
773 for (i = 0; i < LOG_BUFFER_ENTRIES; i++) {
774 kfree(port->logbuffer[i]);
775 port->logbuffer[i] = NULL;
776 }
777 mutex_unlock(&port->logbuffer_lock);
778
779 debugfs_remove(port->dentry);
780 }
781
782 #else
783
784 __printf(2, 3)
tcpm_log(const struct tcpm_port * port,const char * fmt,...)785 static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
786 __printf(2, 3)
tcpm_log_force(struct tcpm_port * port,const char * fmt,...)787 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
tcpm_log_source_caps(struct tcpm_port * port)788 static void tcpm_log_source_caps(struct tcpm_port *port) { }
tcpm_debugfs_init(const struct tcpm_port * port)789 static void tcpm_debugfs_init(const struct tcpm_port *port) { }
tcpm_debugfs_exit(const struct tcpm_port * port)790 static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
791
792 #endif
793
tcpm_set_cc(struct tcpm_port * port,enum typec_cc_status cc)794 static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
795 {
796 tcpm_log(port, "cc:=%d", cc);
797 port->cc_req = cc;
798 port->tcpc->set_cc(port->tcpc, cc);
799 }
800
tcpm_enable_auto_vbus_discharge(struct tcpm_port * port,bool enable)801 static int tcpm_enable_auto_vbus_discharge(struct tcpm_port *port, bool enable)
802 {
803 int ret = 0;
804
805 if (port->tcpc->enable_auto_vbus_discharge) {
806 ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, enable);
807 tcpm_log_force(port, "%s vbus discharge ret:%d", enable ? "enable" : "disable",
808 ret);
809 if (!ret)
810 port->auto_vbus_discharge_enabled = enable;
811 }
812
813 return ret;
814 }
815
tcpm_apply_rc(struct tcpm_port * port)816 static void tcpm_apply_rc(struct tcpm_port *port)
817 {
818 /*
819 * TCPCI: Move to APPLY_RC state to prevent disconnect during PR_SWAP
820 * when Vbus auto discharge on disconnect is enabled.
821 */
822 if (port->tcpc->enable_auto_vbus_discharge && port->tcpc->apply_rc) {
823 tcpm_log(port, "Apply_RC");
824 port->tcpc->apply_rc(port->tcpc, port->cc_req, port->polarity);
825 tcpm_enable_auto_vbus_discharge(port, false);
826 }
827 }
828
829 /*
830 * Determine RP value to set based on maximum current supported
831 * by a port if configured as source.
832 * Returns CC value to report to link partner.
833 */
tcpm_rp_cc(struct tcpm_port * port)834 static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
835 {
836 const u32 *src_pdo = port->src_pdo;
837 int nr_pdo = port->nr_src_pdo;
838 int i;
839
840 if (!port->pd_supported)
841 return port->src_rp;
842
843 /*
844 * Search for first entry with matching voltage.
845 * It should report the maximum supported current.
846 */
847 for (i = 0; i < nr_pdo; i++) {
848 const u32 pdo = src_pdo[i];
849
850 if (pdo_type(pdo) == PDO_TYPE_FIXED &&
851 pdo_fixed_voltage(pdo) == 5000) {
852 unsigned int curr = pdo_max_current(pdo);
853
854 if (curr >= 3000)
855 return TYPEC_CC_RP_3_0;
856 else if (curr >= 1500)
857 return TYPEC_CC_RP_1_5;
858 return TYPEC_CC_RP_DEF;
859 }
860 }
861
862 return TYPEC_CC_RP_DEF;
863 }
864
tcpm_ams_finish(struct tcpm_port * port)865 static void tcpm_ams_finish(struct tcpm_port *port)
866 {
867 tcpm_log(port, "AMS %s finished", tcpm_ams_str[port->ams]);
868
869 if (port->pd_capable && port->pwr_role == TYPEC_SOURCE) {
870 if (port->negotiated_rev >= PD_REV30)
871 tcpm_set_cc(port, SINK_TX_OK);
872 else
873 tcpm_set_cc(port, SINK_TX_NG);
874 } else if (port->pwr_role == TYPEC_SOURCE) {
875 tcpm_set_cc(port, tcpm_rp_cc(port));
876 }
877
878 port->in_ams = false;
879 port->ams = NONE_AMS;
880 }
881
tcpm_pd_transmit(struct tcpm_port * port,enum tcpm_transmit_type type,const struct pd_message * msg)882 static int tcpm_pd_transmit(struct tcpm_port *port,
883 enum tcpm_transmit_type type,
884 const struct pd_message *msg)
885 {
886 unsigned long timeout;
887 int ret;
888
889 if (msg)
890 tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
891 else
892 tcpm_log(port, "PD TX, type: %#x", type);
893
894 reinit_completion(&port->tx_complete);
895 ret = port->tcpc->pd_transmit(port->tcpc, type, msg, port->negotiated_rev);
896 if (ret < 0)
897 return ret;
898
899 mutex_unlock(&port->lock);
900 timeout = wait_for_completion_timeout(&port->tx_complete,
901 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
902 mutex_lock(&port->lock);
903 if (!timeout)
904 return -ETIMEDOUT;
905
906 switch (port->tx_status) {
907 case TCPC_TX_SUCCESS:
908 port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
909 /*
910 * USB PD rev 2.0, 8.3.2.2.1:
911 * USB PD rev 3.0, 8.3.2.1.3:
912 * "... Note that every AMS is Interruptible until the first
913 * Message in the sequence has been successfully sent (GoodCRC
914 * Message received)."
915 */
916 if (port->ams != NONE_AMS)
917 port->in_ams = true;
918 break;
919 case TCPC_TX_DISCARDED:
920 ret = -EAGAIN;
921 break;
922 case TCPC_TX_FAILED:
923 default:
924 ret = -EIO;
925 break;
926 }
927
928 /* Some AMS don't expect responses. Finish them here. */
929 if (port->ams == ATTENTION || port->ams == SOURCE_ALERT)
930 tcpm_ams_finish(port);
931
932 return ret;
933 }
934
tcpm_pd_transmit_complete(struct tcpm_port * port,enum tcpm_transmit_status status)935 void tcpm_pd_transmit_complete(struct tcpm_port *port,
936 enum tcpm_transmit_status status)
937 {
938 tcpm_log(port, "PD TX complete, status: %u", status);
939 port->tx_status = status;
940 complete(&port->tx_complete);
941 }
942 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
943
tcpm_mux_set(struct tcpm_port * port,int state,enum usb_role usb_role,enum typec_orientation orientation)944 static int tcpm_mux_set(struct tcpm_port *port, int state,
945 enum usb_role usb_role,
946 enum typec_orientation orientation)
947 {
948 int ret;
949
950 tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d",
951 state, usb_role, orientation);
952
953 ret = typec_set_orientation(port->typec_port, orientation);
954 if (ret)
955 return ret;
956
957 if (port->role_sw) {
958 ret = usb_role_switch_set_role(port->role_sw, usb_role);
959 if (ret)
960 return ret;
961 }
962
963 return typec_set_mode(port->typec_port, state);
964 }
965
tcpm_set_polarity(struct tcpm_port * port,enum typec_cc_polarity polarity)966 static int tcpm_set_polarity(struct tcpm_port *port,
967 enum typec_cc_polarity polarity)
968 {
969 int ret;
970
971 tcpm_log(port, "polarity %d", polarity);
972
973 ret = port->tcpc->set_polarity(port->tcpc, polarity);
974 if (ret < 0)
975 return ret;
976
977 port->polarity = polarity;
978
979 return 0;
980 }
981
tcpm_set_vconn(struct tcpm_port * port,bool enable)982 static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
983 {
984 int ret;
985
986 tcpm_log(port, "vconn:=%d", enable);
987
988 ret = port->tcpc->set_vconn(port->tcpc, enable);
989 if (!ret) {
990 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
991 typec_set_vconn_role(port->typec_port, port->vconn_role);
992 }
993
994 return ret;
995 }
996
tcpm_get_current_limit(struct tcpm_port * port)997 static u32 tcpm_get_current_limit(struct tcpm_port *port)
998 {
999 enum typec_cc_status cc;
1000 u32 limit;
1001
1002 cc = port->polarity ? port->cc2 : port->cc1;
1003 switch (cc) {
1004 case TYPEC_CC_RP_1_5:
1005 limit = 1500;
1006 break;
1007 case TYPEC_CC_RP_3_0:
1008 limit = 3000;
1009 break;
1010 case TYPEC_CC_RP_DEF:
1011 default:
1012 if (port->tcpc->get_current_limit)
1013 limit = port->tcpc->get_current_limit(port->tcpc);
1014 else
1015 limit = 0;
1016 break;
1017 }
1018
1019 return limit;
1020 }
1021
tcpm_set_current_limit(struct tcpm_port * port,u32 max_ma,u32 mv)1022 static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv)
1023 {
1024 int ret = -EOPNOTSUPP;
1025
1026 tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma);
1027
1028 port->supply_voltage = mv;
1029 port->current_limit = max_ma;
1030 power_supply_changed(port->psy);
1031
1032 if (port->tcpc->set_current_limit)
1033 ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv);
1034
1035 return ret;
1036 }
1037
tcpm_set_attached_state(struct tcpm_port * port,bool attached)1038 static int tcpm_set_attached_state(struct tcpm_port *port, bool attached)
1039 {
1040 return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role,
1041 port->data_role);
1042 }
1043
tcpm_set_roles(struct tcpm_port * port,bool attached,enum typec_role role,enum typec_data_role data)1044 static int tcpm_set_roles(struct tcpm_port *port, bool attached,
1045 enum typec_role role, enum typec_data_role data)
1046 {
1047 enum typec_orientation orientation;
1048 enum usb_role usb_role;
1049 int ret;
1050
1051 if (port->polarity == TYPEC_POLARITY_CC1)
1052 orientation = TYPEC_ORIENTATION_NORMAL;
1053 else
1054 orientation = TYPEC_ORIENTATION_REVERSE;
1055
1056 if (port->typec_caps.data == TYPEC_PORT_DRD) {
1057 if (data == TYPEC_HOST)
1058 usb_role = USB_ROLE_HOST;
1059 else
1060 usb_role = USB_ROLE_DEVICE;
1061 } else if (port->typec_caps.data == TYPEC_PORT_DFP) {
1062 if (data == TYPEC_HOST) {
1063 if (role == TYPEC_SOURCE)
1064 usb_role = USB_ROLE_HOST;
1065 else
1066 usb_role = USB_ROLE_NONE;
1067 } else {
1068 return -ENOTSUPP;
1069 }
1070 } else {
1071 if (data == TYPEC_DEVICE) {
1072 if (role == TYPEC_SINK)
1073 usb_role = USB_ROLE_DEVICE;
1074 else
1075 usb_role = USB_ROLE_NONE;
1076 } else {
1077 return -ENOTSUPP;
1078 }
1079 }
1080
1081 ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation);
1082 if (ret < 0)
1083 return ret;
1084
1085 ret = port->tcpc->set_roles(port->tcpc, attached, role, data);
1086 if (ret < 0)
1087 return ret;
1088
1089 port->pwr_role = role;
1090 port->data_role = data;
1091 typec_set_data_role(port->typec_port, data);
1092 typec_set_pwr_role(port->typec_port, role);
1093
1094 return 0;
1095 }
1096
tcpm_set_pwr_role(struct tcpm_port * port,enum typec_role role)1097 static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
1098 {
1099 int ret;
1100
1101 ret = port->tcpc->set_roles(port->tcpc, true, role,
1102 port->data_role);
1103 if (ret < 0)
1104 return ret;
1105
1106 port->pwr_role = role;
1107 typec_set_pwr_role(port->typec_port, role);
1108
1109 return 0;
1110 }
1111
1112 /*
1113 * Transform the PDO to be compliant to PD rev2.0.
1114 * Return 0 if the PDO type is not defined in PD rev2.0.
1115 * Otherwise, return the converted PDO.
1116 */
tcpm_forge_legacy_pdo(struct tcpm_port * port,u32 pdo,enum typec_role role)1117 static u32 tcpm_forge_legacy_pdo(struct tcpm_port *port, u32 pdo, enum typec_role role)
1118 {
1119 switch (pdo_type(pdo)) {
1120 case PDO_TYPE_FIXED:
1121 if (role == TYPEC_SINK)
1122 return pdo & ~PDO_FIXED_FRS_CURR_MASK;
1123 else
1124 return pdo & ~PDO_FIXED_UNCHUNK_EXT;
1125 case PDO_TYPE_VAR:
1126 case PDO_TYPE_BATT:
1127 return pdo;
1128 case PDO_TYPE_APDO:
1129 default:
1130 return 0;
1131 }
1132 }
1133
tcpm_pd_send_source_caps(struct tcpm_port * port)1134 static int tcpm_pd_send_source_caps(struct tcpm_port *port)
1135 {
1136 struct pd_message msg;
1137 u32 pdo;
1138 unsigned int i, nr_pdo = 0;
1139
1140 memset(&msg, 0, sizeof(msg));
1141
1142 for (i = 0; i < port->nr_src_pdo; i++) {
1143 if (port->negotiated_rev >= PD_REV30) {
1144 msg.payload[nr_pdo++] = cpu_to_le32(port->src_pdo[i]);
1145 } else {
1146 pdo = tcpm_forge_legacy_pdo(port, port->src_pdo[i], TYPEC_SOURCE);
1147 if (pdo)
1148 msg.payload[nr_pdo++] = cpu_to_le32(pdo);
1149 }
1150 }
1151
1152 if (!nr_pdo) {
1153 /* No source capabilities defined, sink only */
1154 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
1155 port->pwr_role,
1156 port->data_role,
1157 port->negotiated_rev,
1158 port->message_id, 0);
1159 } else {
1160 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
1161 port->pwr_role,
1162 port->data_role,
1163 port->negotiated_rev,
1164 port->message_id,
1165 nr_pdo);
1166 }
1167
1168 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1169 }
1170
tcpm_pd_send_sink_caps(struct tcpm_port * port)1171 static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
1172 {
1173 struct pd_message msg;
1174 u32 pdo;
1175 unsigned int i, nr_pdo = 0;
1176
1177 memset(&msg, 0, sizeof(msg));
1178
1179 for (i = 0; i < port->nr_snk_pdo; i++) {
1180 if (port->negotiated_rev >= PD_REV30) {
1181 msg.payload[nr_pdo++] = cpu_to_le32(port->snk_pdo[i]);
1182 } else {
1183 pdo = tcpm_forge_legacy_pdo(port, port->snk_pdo[i], TYPEC_SINK);
1184 if (pdo)
1185 msg.payload[nr_pdo++] = cpu_to_le32(pdo);
1186 }
1187 }
1188
1189 if (!nr_pdo) {
1190 /* No sink capabilities defined, source only */
1191 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
1192 port->pwr_role,
1193 port->data_role,
1194 port->negotiated_rev,
1195 port->message_id, 0);
1196 } else {
1197 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
1198 port->pwr_role,
1199 port->data_role,
1200 port->negotiated_rev,
1201 port->message_id,
1202 nr_pdo);
1203 }
1204
1205 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1206 }
1207
mod_tcpm_delayed_work(struct tcpm_port * port,unsigned int delay_ms)1208 static void mod_tcpm_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1209 {
1210 if (delay_ms) {
1211 hrtimer_start(&port->state_machine_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1212 } else {
1213 hrtimer_cancel(&port->state_machine_timer);
1214 kthread_queue_work(port->wq, &port->state_machine);
1215 }
1216 }
1217
mod_vdm_delayed_work(struct tcpm_port * port,unsigned int delay_ms)1218 static void mod_vdm_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1219 {
1220 if (delay_ms) {
1221 hrtimer_start(&port->vdm_state_machine_timer, ms_to_ktime(delay_ms),
1222 HRTIMER_MODE_REL);
1223 } else {
1224 hrtimer_cancel(&port->vdm_state_machine_timer);
1225 kthread_queue_work(port->wq, &port->vdm_state_machine);
1226 }
1227 }
1228
mod_enable_frs_delayed_work(struct tcpm_port * port,unsigned int delay_ms)1229 static void mod_enable_frs_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1230 {
1231 if (delay_ms) {
1232 hrtimer_start(&port->enable_frs_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1233 } else {
1234 hrtimer_cancel(&port->enable_frs_timer);
1235 kthread_queue_work(port->wq, &port->enable_frs);
1236 }
1237 }
1238
mod_send_discover_delayed_work(struct tcpm_port * port,unsigned int delay_ms)1239 static void mod_send_discover_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1240 {
1241 if (delay_ms) {
1242 hrtimer_start(&port->send_discover_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1243 } else {
1244 hrtimer_cancel(&port->send_discover_timer);
1245 kthread_queue_work(port->wq, &port->send_discover_work);
1246 }
1247 }
1248
tcpm_set_state(struct tcpm_port * port,enum tcpm_state state,unsigned int delay_ms)1249 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
1250 unsigned int delay_ms)
1251 {
1252 if (delay_ms) {
1253 tcpm_log(port, "pending state change %s -> %s @ %u ms [%s %s]",
1254 tcpm_states[port->state], tcpm_states[state], delay_ms,
1255 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]);
1256 port->delayed_state = state;
1257 mod_tcpm_delayed_work(port, delay_ms);
1258 port->delayed_runtime = ktime_add(ktime_get(), ms_to_ktime(delay_ms));
1259 port->delay_ms = delay_ms;
1260 } else {
1261 tcpm_log(port, "state change %s -> %s [%s %s]",
1262 tcpm_states[port->state], tcpm_states[state],
1263 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]);
1264 port->delayed_state = INVALID_STATE;
1265 port->prev_state = port->state;
1266 port->state = state;
1267 /*
1268 * Don't re-queue the state machine work item if we're currently
1269 * in the state machine and we're immediately changing states.
1270 * tcpm_state_machine_work() will continue running the state
1271 * machine.
1272 */
1273 if (!port->state_machine_running)
1274 mod_tcpm_delayed_work(port, 0);
1275 }
1276 }
1277
tcpm_set_state_cond(struct tcpm_port * port,enum tcpm_state state,unsigned int delay_ms)1278 static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state,
1279 unsigned int delay_ms)
1280 {
1281 if (port->enter_state == port->state)
1282 tcpm_set_state(port, state, delay_ms);
1283 else
1284 tcpm_log(port,
1285 "skipped %sstate change %s -> %s [%u ms], context state %s [%s %s]",
1286 delay_ms ? "delayed " : "",
1287 tcpm_states[port->state], tcpm_states[state],
1288 delay_ms, tcpm_states[port->enter_state],
1289 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]);
1290 }
1291
tcpm_queue_message(struct tcpm_port * port,enum pd_msg_request message)1292 static void tcpm_queue_message(struct tcpm_port *port,
1293 enum pd_msg_request message)
1294 {
1295 port->queued_message = message;
1296 mod_tcpm_delayed_work(port, 0);
1297 }
1298
tcpm_vdm_ams(struct tcpm_port * port)1299 static bool tcpm_vdm_ams(struct tcpm_port *port)
1300 {
1301 switch (port->ams) {
1302 case DISCOVER_IDENTITY:
1303 case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY:
1304 case DISCOVER_SVIDS:
1305 case DISCOVER_MODES:
1306 case DFP_TO_UFP_ENTER_MODE:
1307 case DFP_TO_UFP_EXIT_MODE:
1308 case DFP_TO_CABLE_PLUG_ENTER_MODE:
1309 case DFP_TO_CABLE_PLUG_EXIT_MODE:
1310 case ATTENTION:
1311 case UNSTRUCTURED_VDMS:
1312 case STRUCTURED_VDMS:
1313 break;
1314 default:
1315 return false;
1316 }
1317
1318 return true;
1319 }
1320
tcpm_ams_interruptible(struct tcpm_port * port)1321 static bool tcpm_ams_interruptible(struct tcpm_port *port)
1322 {
1323 switch (port->ams) {
1324 /* Interruptible AMS */
1325 case NONE_AMS:
1326 case SECURITY:
1327 case FIRMWARE_UPDATE:
1328 case DISCOVER_IDENTITY:
1329 case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY:
1330 case DISCOVER_SVIDS:
1331 case DISCOVER_MODES:
1332 case DFP_TO_UFP_ENTER_MODE:
1333 case DFP_TO_UFP_EXIT_MODE:
1334 case DFP_TO_CABLE_PLUG_ENTER_MODE:
1335 case DFP_TO_CABLE_PLUG_EXIT_MODE:
1336 case UNSTRUCTURED_VDMS:
1337 case STRUCTURED_VDMS:
1338 case COUNTRY_INFO:
1339 case COUNTRY_CODES:
1340 break;
1341 /* Non-Interruptible AMS */
1342 default:
1343 if (port->in_ams)
1344 return false;
1345 break;
1346 }
1347
1348 return true;
1349 }
1350
tcpm_ams_start(struct tcpm_port * port,enum tcpm_ams ams)1351 static int tcpm_ams_start(struct tcpm_port *port, enum tcpm_ams ams)
1352 {
1353 int ret = 0;
1354
1355 tcpm_log(port, "AMS %s start", tcpm_ams_str[ams]);
1356
1357 if (!tcpm_ams_interruptible(port) &&
1358 !(ams == HARD_RESET || ams == SOFT_RESET_AMS)) {
1359 port->upcoming_state = INVALID_STATE;
1360 tcpm_log(port, "AMS %s not interruptible, aborting",
1361 tcpm_ams_str[port->ams]);
1362 return -EAGAIN;
1363 }
1364
1365 if (port->pwr_role == TYPEC_SOURCE) {
1366 enum typec_cc_status cc_req = port->cc_req;
1367
1368 port->ams = ams;
1369
1370 if (ams == HARD_RESET) {
1371 tcpm_set_cc(port, tcpm_rp_cc(port));
1372 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
1373 tcpm_set_state(port, HARD_RESET_START, 0);
1374 return ret;
1375 } else if (ams == SOFT_RESET_AMS) {
1376 if (!port->explicit_contract)
1377 tcpm_set_cc(port, tcpm_rp_cc(port));
1378 tcpm_set_state(port, SOFT_RESET_SEND, 0);
1379 return ret;
1380 } else if (tcpm_vdm_ams(port)) {
1381 /* tSinkTx is enforced in vdm_run_state_machine */
1382 if (port->negotiated_rev >= PD_REV30)
1383 tcpm_set_cc(port, SINK_TX_NG);
1384 return ret;
1385 }
1386
1387 if (port->negotiated_rev >= PD_REV30)
1388 tcpm_set_cc(port, SINK_TX_NG);
1389
1390 switch (port->state) {
1391 case SRC_READY:
1392 case SRC_STARTUP:
1393 case SRC_SOFT_RESET_WAIT_SNK_TX:
1394 case SOFT_RESET:
1395 case SOFT_RESET_SEND:
1396 if (port->negotiated_rev >= PD_REV30)
1397 tcpm_set_state(port, AMS_START,
1398 cc_req == SINK_TX_OK ?
1399 PD_T_SINK_TX : 0);
1400 else
1401 tcpm_set_state(port, AMS_START, 0);
1402 break;
1403 default:
1404 if (port->negotiated_rev >= PD_REV30)
1405 tcpm_set_state(port, SRC_READY,
1406 cc_req == SINK_TX_OK ?
1407 PD_T_SINK_TX : 0);
1408 else
1409 tcpm_set_state(port, SRC_READY, 0);
1410 break;
1411 }
1412 } else {
1413 if (port->negotiated_rev >= PD_REV30 &&
1414 !tcpm_sink_tx_ok(port) &&
1415 ams != SOFT_RESET_AMS &&
1416 ams != HARD_RESET) {
1417 port->upcoming_state = INVALID_STATE;
1418 tcpm_log(port, "Sink TX No Go");
1419 return -EAGAIN;
1420 }
1421
1422 port->ams = ams;
1423
1424 if (ams == HARD_RESET) {
1425 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
1426 tcpm_set_state(port, HARD_RESET_START, 0);
1427 return ret;
1428 } else if (tcpm_vdm_ams(port)) {
1429 return ret;
1430 }
1431
1432 if (port->state == SNK_READY ||
1433 port->state == SNK_SOFT_RESET)
1434 tcpm_set_state(port, AMS_START, 0);
1435 else
1436 tcpm_set_state(port, SNK_READY, 0);
1437 }
1438
1439 return ret;
1440 }
1441
1442 /*
1443 * VDM/VDO handling functions
1444 */
tcpm_queue_vdm(struct tcpm_port * port,const u32 header,const u32 * data,int cnt)1445 static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
1446 const u32 *data, int cnt)
1447 {
1448 u32 vdo_hdr = port->vdo_data[0];
1449
1450 WARN_ON(!mutex_is_locked(&port->lock));
1451
1452 /* If is sending discover_identity, handle received message first */
1453 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMD(vdo_hdr) == CMD_DISCOVER_IDENT) {
1454 port->send_discover = true;
1455 mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS);
1456 } else {
1457 /* Make sure we are not still processing a previous VDM packet */
1458 WARN_ON(port->vdm_state > VDM_STATE_DONE);
1459 }
1460
1461 port->vdo_count = cnt + 1;
1462 port->vdo_data[0] = header;
1463 memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt);
1464 /* Set ready, vdm state machine will actually send */
1465 port->vdm_retries = 0;
1466 port->vdm_state = VDM_STATE_READY;
1467 port->vdm_sm_running = true;
1468
1469 mod_vdm_delayed_work(port, 0);
1470 }
1471
tcpm_queue_vdm_unlocked(struct tcpm_port * port,const u32 header,const u32 * data,int cnt)1472 static void tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
1473 const u32 *data, int cnt)
1474 {
1475 mutex_lock(&port->lock);
1476 tcpm_queue_vdm(port, header, data, cnt);
1477 mutex_unlock(&port->lock);
1478 }
1479
svdm_consume_identity(struct tcpm_port * port,const u32 * p,int cnt)1480 static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt)
1481 {
1482 u32 vdo = p[VDO_INDEX_IDH];
1483 u32 product = p[VDO_INDEX_PRODUCT];
1484
1485 memset(&port->mode_data, 0, sizeof(port->mode_data));
1486
1487 port->partner_ident.id_header = vdo;
1488 port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT];
1489 port->partner_ident.product = product;
1490
1491 typec_partner_set_identity(port->partner);
1492
1493 tcpm_log(port, "Identity: %04x:%04x.%04x",
1494 PD_IDH_VID(vdo),
1495 PD_PRODUCT_PID(product), product & 0xffff);
1496 }
1497
svdm_consume_svids(struct tcpm_port * port,const u32 * p,int cnt)1498 static bool svdm_consume_svids(struct tcpm_port *port, const u32 *p, int cnt)
1499 {
1500 struct pd_mode_data *pmdata = &port->mode_data;
1501 int i;
1502
1503 for (i = 1; i < cnt; i++) {
1504 u16 svid;
1505
1506 svid = (p[i] >> 16) & 0xffff;
1507 if (!svid)
1508 return false;
1509
1510 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
1511 goto abort;
1512
1513 pmdata->svids[pmdata->nsvids++] = svid;
1514 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
1515
1516 svid = p[i] & 0xffff;
1517 if (!svid)
1518 return false;
1519
1520 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
1521 goto abort;
1522
1523 pmdata->svids[pmdata->nsvids++] = svid;
1524 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
1525 }
1526
1527 /*
1528 * PD3.0 Spec 6.4.4.3.2: The SVIDs are returned 2 per VDO (see Table
1529 * 6-43), and can be returned maximum 6 VDOs per response (see Figure
1530 * 6-19). If the Respondersupports 12 or more SVID then the Discover
1531 * SVIDs Command Shall be executed multiple times until a Discover
1532 * SVIDs VDO is returned ending either with a SVID value of 0x0000 in
1533 * the last part of the last VDO or with a VDO containing two SVIDs
1534 * with values of 0x0000.
1535 *
1536 * However, some odd dockers support SVIDs less than 12 but without
1537 * 0x0000 in the last VDO, so we need to break the Discover SVIDs
1538 * request and return false here.
1539 */
1540 return cnt == 7;
1541 abort:
1542 tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX);
1543 return false;
1544 }
1545
svdm_consume_modes(struct tcpm_port * port,const u32 * p,int cnt)1546 static void svdm_consume_modes(struct tcpm_port *port, const u32 *p, int cnt)
1547 {
1548 struct pd_mode_data *pmdata = &port->mode_data;
1549 struct typec_altmode_desc *paltmode;
1550 int i;
1551
1552 if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) {
1553 /* Already logged in svdm_consume_svids() */
1554 return;
1555 }
1556
1557 for (i = 1; i < cnt; i++) {
1558 paltmode = &pmdata->altmode_desc[pmdata->altmodes];
1559 memset(paltmode, 0, sizeof(*paltmode));
1560
1561 paltmode->svid = pmdata->svids[pmdata->svid_index];
1562 paltmode->mode = i;
1563 paltmode->vdo = p[i];
1564
1565 tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x",
1566 pmdata->altmodes, paltmode->svid,
1567 paltmode->mode, paltmode->vdo);
1568
1569 pmdata->altmodes++;
1570 }
1571 }
1572
tcpm_register_partner_altmodes(struct tcpm_port * port)1573 static void tcpm_register_partner_altmodes(struct tcpm_port *port)
1574 {
1575 struct pd_mode_data *modep = &port->mode_data;
1576 struct typec_altmode *altmode;
1577 int i;
1578
1579 for (i = 0; i < modep->altmodes; i++) {
1580 altmode = typec_partner_register_altmode(port->partner,
1581 &modep->altmode_desc[i]);
1582 if (IS_ERR(altmode)) {
1583 tcpm_log(port, "Failed to register partner SVID 0x%04x",
1584 modep->altmode_desc[i].svid);
1585 altmode = NULL;
1586 }
1587 port->partner_altmode[i] = altmode;
1588 }
1589 }
1590
1591 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
1592
tcpm_pd_svdm(struct tcpm_port * port,struct typec_altmode * adev,const u32 * p,int cnt,u32 * response,enum adev_actions * adev_action)1593 static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev,
1594 const u32 *p, int cnt, u32 *response,
1595 enum adev_actions *adev_action)
1596 {
1597 struct typec_port *typec = port->typec_port;
1598 struct typec_altmode *pdev;
1599 struct pd_mode_data *modep;
1600 int svdm_version;
1601 int rlen = 0;
1602 int cmd_type;
1603 int cmd;
1604 int i;
1605
1606 cmd_type = PD_VDO_CMDT(p[0]);
1607 cmd = PD_VDO_CMD(p[0]);
1608
1609 tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1610 p[0], cmd_type, cmd, cnt);
1611
1612 modep = &port->mode_data;
1613
1614 pdev = typec_match_altmode(port->partner_altmode, ALTMODE_DISCOVERY_MAX,
1615 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
1616
1617 svdm_version = typec_get_negotiated_svdm_version(typec);
1618 if (svdm_version < 0)
1619 return 0;
1620
1621 switch (cmd_type) {
1622 case CMDT_INIT:
1623 switch (cmd) {
1624 case CMD_DISCOVER_IDENT:
1625 if (PD_VDO_VID(p[0]) != USB_SID_PD)
1626 break;
1627
1628 if (PD_VDO_SVDM_VER(p[0]) < svdm_version) {
1629 typec_partner_set_svdm_version(port->partner,
1630 PD_VDO_SVDM_VER(p[0]));
1631 svdm_version = PD_VDO_SVDM_VER(p[0]);
1632 }
1633
1634 port->ams = DISCOVER_IDENTITY;
1635 /*
1636 * PD2.0 Spec 6.10.3: respond with NAK as DFP (data host)
1637 * PD3.1 Spec 6.4.4.2.5.1: respond with NAK if "invalid field" or
1638 * "wrong configuation" or "Unrecognized"
1639 */
1640 if ((port->data_role == TYPEC_DEVICE || svdm_version >= SVDM_VER_2_0) &&
1641 port->nr_snk_vdo) {
1642 if (svdm_version < SVDM_VER_2_0) {
1643 for (i = 0; i < port->nr_snk_vdo_v1; i++)
1644 response[i + 1] = port->snk_vdo_v1[i];
1645 rlen = port->nr_snk_vdo_v1 + 1;
1646
1647 } else {
1648 for (i = 0; i < port->nr_snk_vdo; i++)
1649 response[i + 1] = port->snk_vdo[i];
1650 rlen = port->nr_snk_vdo + 1;
1651 }
1652 }
1653 break;
1654 case CMD_DISCOVER_SVID:
1655 port->ams = DISCOVER_SVIDS;
1656 break;
1657 case CMD_DISCOVER_MODES:
1658 port->ams = DISCOVER_MODES;
1659 break;
1660 case CMD_ENTER_MODE:
1661 port->ams = DFP_TO_UFP_ENTER_MODE;
1662 break;
1663 case CMD_EXIT_MODE:
1664 port->ams = DFP_TO_UFP_EXIT_MODE;
1665 break;
1666 case CMD_ATTENTION:
1667 /* Attention command does not have response */
1668 *adev_action = ADEV_ATTENTION;
1669 return 0;
1670 default:
1671 break;
1672 }
1673 if (rlen >= 1) {
1674 response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK);
1675 } else if (rlen == 0) {
1676 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1677 rlen = 1;
1678 } else {
1679 response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY);
1680 rlen = 1;
1681 }
1682 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1683 (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec)));
1684 break;
1685 case CMDT_RSP_ACK:
1686 /* silently drop message if we are not connected */
1687 if (IS_ERR_OR_NULL(port->partner))
1688 break;
1689
1690 tcpm_ams_finish(port);
1691
1692 switch (cmd) {
1693 case CMD_DISCOVER_IDENT:
1694 if (PD_VDO_SVDM_VER(p[0]) < svdm_version)
1695 typec_partner_set_svdm_version(port->partner,
1696 PD_VDO_SVDM_VER(p[0]));
1697 /* 6.4.4.3.1 */
1698 svdm_consume_identity(port, p, cnt);
1699 response[0] = VDO(USB_SID_PD, 1, typec_get_negotiated_svdm_version(typec),
1700 CMD_DISCOVER_SVID);
1701 rlen = 1;
1702 break;
1703 case CMD_DISCOVER_SVID:
1704 /* 6.4.4.3.2 */
1705 if (svdm_consume_svids(port, p, cnt)) {
1706 response[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID);
1707 rlen = 1;
1708 } else if (modep->nsvids && supports_modal(port)) {
1709 response[0] = VDO(modep->svids[0], 1, svdm_version,
1710 CMD_DISCOVER_MODES);
1711 rlen = 1;
1712 }
1713 break;
1714 case CMD_DISCOVER_MODES:
1715 /* 6.4.4.3.3 */
1716 svdm_consume_modes(port, p, cnt);
1717 modep->svid_index++;
1718 if (modep->svid_index < modep->nsvids) {
1719 u16 svid = modep->svids[modep->svid_index];
1720 response[0] = VDO(svid, 1, svdm_version, CMD_DISCOVER_MODES);
1721 rlen = 1;
1722 } else {
1723 tcpm_register_partner_altmodes(port);
1724 }
1725 break;
1726 case CMD_ENTER_MODE:
1727 if (adev && pdev)
1728 *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL;
1729 return 0;
1730 case CMD_EXIT_MODE:
1731 if (adev && pdev) {
1732 /* Back to USB Operation */
1733 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM;
1734 return 0;
1735 }
1736 break;
1737 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
1738 break;
1739 default:
1740 /* Unrecognized SVDM */
1741 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1742 rlen = 1;
1743 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1744 (VDO_SVDM_VERS(svdm_version));
1745 break;
1746 }
1747 break;
1748 case CMDT_RSP_NAK:
1749 tcpm_ams_finish(port);
1750 switch (cmd) {
1751 case CMD_DISCOVER_IDENT:
1752 case CMD_DISCOVER_SVID:
1753 case CMD_DISCOVER_MODES:
1754 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
1755 break;
1756 case CMD_ENTER_MODE:
1757 /* Back to USB Operation */
1758 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM;
1759 return 0;
1760 default:
1761 /* Unrecognized SVDM */
1762 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1763 rlen = 1;
1764 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1765 (VDO_SVDM_VERS(svdm_version));
1766 break;
1767 }
1768 break;
1769 default:
1770 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1771 rlen = 1;
1772 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1773 (VDO_SVDM_VERS(svdm_version));
1774 break;
1775 }
1776
1777 /* Informing the alternate mode drivers about everything */
1778 *adev_action = ADEV_QUEUE_VDM;
1779 return rlen;
1780 }
1781
1782 static void tcpm_pd_handle_msg(struct tcpm_port *port,
1783 enum pd_msg_request message,
1784 enum tcpm_ams ams);
1785
tcpm_handle_vdm_request(struct tcpm_port * port,const __le32 * payload,int cnt)1786 static void tcpm_handle_vdm_request(struct tcpm_port *port,
1787 const __le32 *payload, int cnt)
1788 {
1789 enum adev_actions adev_action = ADEV_NONE;
1790 struct typec_altmode *adev;
1791 u32 p[PD_MAX_PAYLOAD];
1792 u32 response[8] = { };
1793 int i, rlen = 0;
1794
1795 for (i = 0; i < cnt; i++)
1796 p[i] = le32_to_cpu(payload[i]);
1797
1798 adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX,
1799 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
1800
1801 if (port->vdm_state == VDM_STATE_BUSY) {
1802 /* If UFP responded busy retry after timeout */
1803 if (PD_VDO_CMDT(p[0]) == CMDT_RSP_BUSY) {
1804 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
1805 port->vdo_retry = (p[0] & ~VDO_CMDT_MASK) |
1806 CMDT_INIT;
1807 mod_vdm_delayed_work(port, PD_T_VDM_BUSY);
1808 return;
1809 }
1810 port->vdm_state = VDM_STATE_DONE;
1811 }
1812
1813 if (PD_VDO_SVDM(p[0]) && (adev || tcpm_vdm_ams(port) || port->nr_snk_vdo)) {
1814 /*
1815 * Here a SVDM is received (INIT or RSP or unknown). Set the vdm_sm_running in
1816 * advance because we are dropping the lock but may send VDMs soon.
1817 * For the cases of INIT received:
1818 * - If no response to send, it will be cleared later in this function.
1819 * - If there are responses to send, it will be cleared in the state machine.
1820 * For the cases of RSP received:
1821 * - If no further INIT to send, it will be cleared later in this function.
1822 * - Otherwise, it will be cleared in the state machine if timeout or it will go
1823 * back here until no further INIT to send.
1824 * For the cases of unknown type received:
1825 * - We will send NAK and the flag will be cleared in the state machine.
1826 */
1827 port->vdm_sm_running = true;
1828 rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action);
1829 } else {
1830 if (port->negotiated_rev >= PD_REV30)
1831 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
1832 }
1833
1834 /*
1835 * We are done with any state stored in the port struct now, except
1836 * for any port struct changes done by the tcpm_queue_vdm() call
1837 * below, which is a separate operation.
1838 *
1839 * So we can safely release the lock here; and we MUST release the
1840 * lock here to avoid an AB BA lock inversion:
1841 *
1842 * If we keep the lock here then the lock ordering in this path is:
1843 * 1. tcpm_pd_rx_handler take the tcpm port lock
1844 * 2. One of the typec_altmode_* calls below takes the alt-mode's lock
1845 *
1846 * And we also have this ordering:
1847 * 1. alt-mode driver takes the alt-mode's lock
1848 * 2. alt-mode driver calls tcpm_altmode_enter which takes the
1849 * tcpm port lock
1850 *
1851 * Dropping our lock here avoids this.
1852 */
1853 mutex_unlock(&port->lock);
1854
1855 if (adev) {
1856 switch (adev_action) {
1857 case ADEV_NONE:
1858 break;
1859 case ADEV_NOTIFY_USB_AND_QUEUE_VDM:
1860 WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, NULL));
1861 typec_altmode_vdm(adev, p[0], &p[1], cnt);
1862 break;
1863 case ADEV_QUEUE_VDM:
1864 typec_altmode_vdm(adev, p[0], &p[1], cnt);
1865 break;
1866 case ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL:
1867 if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) {
1868 int svdm_version = typec_get_negotiated_svdm_version(
1869 port->typec_port);
1870 if (svdm_version < 0)
1871 break;
1872
1873 response[0] = VDO(adev->svid, 1, svdm_version,
1874 CMD_EXIT_MODE);
1875 response[0] |= VDO_OPOS(adev->mode);
1876 rlen = 1;
1877 }
1878 break;
1879 case ADEV_ATTENTION:
1880 if (typec_altmode_attention(adev, p[1]))
1881 tcpm_log(port, "typec_altmode_attention no port partner altmode");
1882 break;
1883 }
1884 }
1885
1886 /*
1887 * We must re-take the lock here to balance the unlock in
1888 * tcpm_pd_rx_handler, note that no changes, other then the
1889 * tcpm_queue_vdm call, are made while the lock is held again.
1890 * All that is done after the call is unwinding the call stack until
1891 * we return to tcpm_pd_rx_handler and do the unlock there.
1892 */
1893 mutex_lock(&port->lock);
1894
1895 if (rlen > 0)
1896 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
1897 else
1898 port->vdm_sm_running = false;
1899 }
1900
tcpm_send_vdm(struct tcpm_port * port,u32 vid,int cmd,const u32 * data,int count)1901 static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
1902 const u32 *data, int count)
1903 {
1904 int svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
1905 u32 header;
1906
1907 if (svdm_version < 0)
1908 return;
1909
1910 if (WARN_ON(count > VDO_MAX_SIZE - 1))
1911 count = VDO_MAX_SIZE - 1;
1912
1913 /* set VDM header with VID & CMD */
1914 header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
1915 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION),
1916 svdm_version, cmd);
1917 tcpm_queue_vdm(port, header, data, count);
1918 }
1919
vdm_ready_timeout(u32 vdm_hdr)1920 static unsigned int vdm_ready_timeout(u32 vdm_hdr)
1921 {
1922 unsigned int timeout;
1923 int cmd = PD_VDO_CMD(vdm_hdr);
1924
1925 /* its not a structured VDM command */
1926 if (!PD_VDO_SVDM(vdm_hdr))
1927 return PD_T_VDM_UNSTRUCTURED;
1928
1929 switch (PD_VDO_CMDT(vdm_hdr)) {
1930 case CMDT_INIT:
1931 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1932 timeout = PD_T_VDM_WAIT_MODE_E;
1933 else
1934 timeout = PD_T_VDM_SNDR_RSP;
1935 break;
1936 default:
1937 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1938 timeout = PD_T_VDM_E_MODE;
1939 else
1940 timeout = PD_T_VDM_RCVR_RSP;
1941 break;
1942 }
1943 return timeout;
1944 }
1945
vdm_run_state_machine(struct tcpm_port * port)1946 static void vdm_run_state_machine(struct tcpm_port *port)
1947 {
1948 struct pd_message msg;
1949 int i, res = 0;
1950 u32 vdo_hdr = port->vdo_data[0];
1951
1952 switch (port->vdm_state) {
1953 case VDM_STATE_READY:
1954 /* Only transmit VDM if attached */
1955 if (!port->attached) {
1956 port->vdm_state = VDM_STATE_ERR_BUSY;
1957 break;
1958 }
1959
1960 /*
1961 * if there's traffic or we're not in PDO ready state don't send
1962 * a VDM.
1963 */
1964 if (port->state != SRC_READY && port->state != SNK_READY) {
1965 port->vdm_sm_running = false;
1966 break;
1967 }
1968
1969 /* TODO: AMS operation for Unstructured VDM */
1970 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) {
1971 switch (PD_VDO_CMD(vdo_hdr)) {
1972 case CMD_DISCOVER_IDENT:
1973 res = tcpm_ams_start(port, DISCOVER_IDENTITY);
1974 if (res == 0) {
1975 port->send_discover = false;
1976 } else if (res == -EAGAIN) {
1977 port->vdo_data[0] = 0;
1978 mod_send_discover_delayed_work(port,
1979 SEND_DISCOVER_RETRY_MS);
1980 }
1981 break;
1982 case CMD_DISCOVER_SVID:
1983 res = tcpm_ams_start(port, DISCOVER_SVIDS);
1984 break;
1985 case CMD_DISCOVER_MODES:
1986 res = tcpm_ams_start(port, DISCOVER_MODES);
1987 break;
1988 case CMD_ENTER_MODE:
1989 res = tcpm_ams_start(port, DFP_TO_UFP_ENTER_MODE);
1990 break;
1991 case CMD_EXIT_MODE:
1992 res = tcpm_ams_start(port, DFP_TO_UFP_EXIT_MODE);
1993 break;
1994 case CMD_ATTENTION:
1995 res = tcpm_ams_start(port, ATTENTION);
1996 break;
1997 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
1998 res = tcpm_ams_start(port, STRUCTURED_VDMS);
1999 break;
2000 default:
2001 res = -EOPNOTSUPP;
2002 break;
2003 }
2004
2005 if (res < 0) {
2006 port->vdm_state = VDM_STATE_ERR_BUSY;
2007 return;
2008 }
2009 }
2010
2011 port->vdm_state = VDM_STATE_SEND_MESSAGE;
2012 mod_vdm_delayed_work(port, (port->negotiated_rev >= PD_REV30 &&
2013 port->pwr_role == TYPEC_SOURCE &&
2014 PD_VDO_SVDM(vdo_hdr) &&
2015 PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) ?
2016 PD_T_SINK_TX : 0);
2017 break;
2018 case VDM_STATE_WAIT_RSP_BUSY:
2019 port->vdo_data[0] = port->vdo_retry;
2020 port->vdo_count = 1;
2021 port->vdm_state = VDM_STATE_READY;
2022 tcpm_ams_finish(port);
2023 break;
2024 case VDM_STATE_BUSY:
2025 port->vdm_state = VDM_STATE_ERR_TMOUT;
2026 if (port->ams != NONE_AMS)
2027 tcpm_ams_finish(port);
2028 break;
2029 case VDM_STATE_ERR_SEND:
2030 /*
2031 * A partner which does not support USB PD will not reply,
2032 * so this is not a fatal error. At the same time, some
2033 * devices may not return GoodCRC under some circumstances,
2034 * so we need to retry.
2035 */
2036 if (port->vdm_retries < 3) {
2037 tcpm_log(port, "VDM Tx error, retry");
2038 port->vdm_retries++;
2039 port->vdm_state = VDM_STATE_READY;
2040 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT)
2041 tcpm_ams_finish(port);
2042 } else {
2043 tcpm_ams_finish(port);
2044 }
2045 break;
2046 case VDM_STATE_SEND_MESSAGE:
2047 /* Prepare and send VDM */
2048 memset(&msg, 0, sizeof(msg));
2049 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
2050 port->pwr_role,
2051 port->data_role,
2052 port->negotiated_rev,
2053 port->message_id, port->vdo_count);
2054 for (i = 0; i < port->vdo_count; i++)
2055 msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
2056 res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
2057 if (res < 0) {
2058 port->vdm_state = VDM_STATE_ERR_SEND;
2059 } else {
2060 unsigned long timeout;
2061
2062 port->vdm_retries = 0;
2063 port->vdo_data[0] = 0;
2064 port->vdm_state = VDM_STATE_BUSY;
2065 timeout = vdm_ready_timeout(vdo_hdr);
2066 mod_vdm_delayed_work(port, timeout);
2067 }
2068 break;
2069 default:
2070 break;
2071 }
2072 }
2073
vdm_state_machine_work(struct kthread_work * work)2074 static void vdm_state_machine_work(struct kthread_work *work)
2075 {
2076 struct tcpm_port *port = container_of(work, struct tcpm_port, vdm_state_machine);
2077 enum vdm_states prev_state;
2078
2079 mutex_lock(&port->lock);
2080
2081 /*
2082 * Continue running as long as the port is not busy and there was
2083 * a state change.
2084 */
2085 do {
2086 prev_state = port->vdm_state;
2087 vdm_run_state_machine(port);
2088 } while (port->vdm_state != prev_state &&
2089 port->vdm_state != VDM_STATE_BUSY &&
2090 port->vdm_state != VDM_STATE_SEND_MESSAGE);
2091
2092 if (port->vdm_state < VDM_STATE_READY)
2093 port->vdm_sm_running = false;
2094
2095 mutex_unlock(&port->lock);
2096 }
2097
2098 enum pdo_err {
2099 PDO_NO_ERR,
2100 PDO_ERR_NO_VSAFE5V,
2101 PDO_ERR_VSAFE5V_NOT_FIRST,
2102 PDO_ERR_PDO_TYPE_NOT_IN_ORDER,
2103 PDO_ERR_FIXED_NOT_SORTED,
2104 PDO_ERR_VARIABLE_BATT_NOT_SORTED,
2105 PDO_ERR_DUPE_PDO,
2106 PDO_ERR_PPS_APDO_NOT_SORTED,
2107 PDO_ERR_DUPE_PPS_APDO,
2108 };
2109
2110 static const char * const pdo_err_msg[] = {
2111 [PDO_ERR_NO_VSAFE5V] =
2112 " err: source/sink caps should at least have vSafe5V",
2113 [PDO_ERR_VSAFE5V_NOT_FIRST] =
2114 " err: vSafe5V Fixed Supply Object Shall always be the first object",
2115 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] =
2116 " err: PDOs should be in the following order: Fixed; Battery; Variable",
2117 [PDO_ERR_FIXED_NOT_SORTED] =
2118 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
2119 [PDO_ERR_VARIABLE_BATT_NOT_SORTED] =
2120 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
2121 [PDO_ERR_DUPE_PDO] =
2122 " err: Variable/Batt supply pdos cannot have same min/max voltage",
2123 [PDO_ERR_PPS_APDO_NOT_SORTED] =
2124 " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
2125 [PDO_ERR_DUPE_PPS_APDO] =
2126 " err: Programmable power supply apdos cannot have same min/max voltage and max current",
2127 };
2128
tcpm_caps_err(struct tcpm_port * port,const u32 * pdo,unsigned int nr_pdo)2129 static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo,
2130 unsigned int nr_pdo)
2131 {
2132 unsigned int i;
2133
2134 /* Should at least contain vSafe5v */
2135 if (nr_pdo < 1)
2136 return PDO_ERR_NO_VSAFE5V;
2137
2138 /* The vSafe5V Fixed Supply Object Shall always be the first object */
2139 if (pdo_type(pdo[0]) != PDO_TYPE_FIXED ||
2140 pdo_fixed_voltage(pdo[0]) != VSAFE5V)
2141 return PDO_ERR_VSAFE5V_NOT_FIRST;
2142
2143 for (i = 1; i < nr_pdo; i++) {
2144 if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) {
2145 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER;
2146 } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) {
2147 enum pd_pdo_type type = pdo_type(pdo[i]);
2148
2149 switch (type) {
2150 /*
2151 * The remaining Fixed Supply Objects, if
2152 * present, shall be sent in voltage order;
2153 * lowest to highest.
2154 */
2155 case PDO_TYPE_FIXED:
2156 if (pdo_fixed_voltage(pdo[i]) <=
2157 pdo_fixed_voltage(pdo[i - 1]))
2158 return PDO_ERR_FIXED_NOT_SORTED;
2159 break;
2160 /*
2161 * The Battery Supply Objects and Variable
2162 * supply, if present shall be sent in Minimum
2163 * Voltage order; lowest to highest.
2164 */
2165 case PDO_TYPE_VAR:
2166 case PDO_TYPE_BATT:
2167 if (pdo_min_voltage(pdo[i]) <
2168 pdo_min_voltage(pdo[i - 1]))
2169 return PDO_ERR_VARIABLE_BATT_NOT_SORTED;
2170 else if ((pdo_min_voltage(pdo[i]) ==
2171 pdo_min_voltage(pdo[i - 1])) &&
2172 (pdo_max_voltage(pdo[i]) ==
2173 pdo_max_voltage(pdo[i - 1])))
2174 return PDO_ERR_DUPE_PDO;
2175 break;
2176 /*
2177 * The Programmable Power Supply APDOs, if present,
2178 * shall be sent in Maximum Voltage order;
2179 * lowest to highest.
2180 */
2181 case PDO_TYPE_APDO:
2182 if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS)
2183 break;
2184
2185 if (pdo_pps_apdo_max_voltage(pdo[i]) <
2186 pdo_pps_apdo_max_voltage(pdo[i - 1]))
2187 return PDO_ERR_PPS_APDO_NOT_SORTED;
2188 else if (pdo_pps_apdo_min_voltage(pdo[i]) ==
2189 pdo_pps_apdo_min_voltage(pdo[i - 1]) &&
2190 pdo_pps_apdo_max_voltage(pdo[i]) ==
2191 pdo_pps_apdo_max_voltage(pdo[i - 1]) &&
2192 pdo_pps_apdo_max_current(pdo[i]) ==
2193 pdo_pps_apdo_max_current(pdo[i - 1]))
2194 return PDO_ERR_DUPE_PPS_APDO;
2195 break;
2196 default:
2197 tcpm_log_force(port, " Unknown pdo type");
2198 }
2199 }
2200 }
2201
2202 return PDO_NO_ERR;
2203 }
2204
tcpm_validate_caps(struct tcpm_port * port,const u32 * pdo,unsigned int nr_pdo)2205 static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo,
2206 unsigned int nr_pdo)
2207 {
2208 enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo);
2209
2210 if (err_index != PDO_NO_ERR) {
2211 tcpm_log_force(port, " %s", pdo_err_msg[err_index]);
2212 return -EINVAL;
2213 }
2214
2215 return 0;
2216 }
2217
tcpm_altmode_enter(struct typec_altmode * altmode,u32 * vdo)2218 static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo)
2219 {
2220 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2221 int svdm_version;
2222 u32 header;
2223
2224 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2225 if (svdm_version < 0)
2226 return svdm_version;
2227
2228 header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE);
2229 header |= VDO_OPOS(altmode->mode);
2230
2231 tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0);
2232 return 0;
2233 }
2234
tcpm_altmode_exit(struct typec_altmode * altmode)2235 static int tcpm_altmode_exit(struct typec_altmode *altmode)
2236 {
2237 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2238 int svdm_version;
2239 u32 header;
2240
2241 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2242 if (svdm_version < 0)
2243 return svdm_version;
2244
2245 header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE);
2246 header |= VDO_OPOS(altmode->mode);
2247
2248 tcpm_queue_vdm_unlocked(port, header, NULL, 0);
2249 return 0;
2250 }
2251
tcpm_altmode_vdm(struct typec_altmode * altmode,u32 header,const u32 * data,int count)2252 static int tcpm_altmode_vdm(struct typec_altmode *altmode,
2253 u32 header, const u32 *data, int count)
2254 {
2255 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2256
2257 tcpm_queue_vdm_unlocked(port, header, data, count - 1);
2258
2259 return 0;
2260 }
2261
2262 static const struct typec_altmode_ops tcpm_altmode_ops = {
2263 .enter = tcpm_altmode_enter,
2264 .exit = tcpm_altmode_exit,
2265 .vdm = tcpm_altmode_vdm,
2266 };
2267
2268 /*
2269 * PD (data, control) command handling functions
2270 */
ready_state(struct tcpm_port * port)2271 static inline enum tcpm_state ready_state(struct tcpm_port *port)
2272 {
2273 if (port->pwr_role == TYPEC_SOURCE)
2274 return SRC_READY;
2275 else
2276 return SNK_READY;
2277 }
2278
2279 static int tcpm_pd_send_control(struct tcpm_port *port,
2280 enum pd_ctrl_msg_type type);
2281
tcpm_handle_alert(struct tcpm_port * port,const __le32 * payload,int cnt)2282 static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload,
2283 int cnt)
2284 {
2285 u32 p0 = le32_to_cpu(payload[0]);
2286 unsigned int type = usb_pd_ado_type(p0);
2287
2288 if (!type) {
2289 tcpm_log(port, "Alert message received with no type");
2290 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
2291 return;
2292 }
2293
2294 /* Just handling non-battery alerts for now */
2295 if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) {
2296 if (port->pwr_role == TYPEC_SOURCE) {
2297 port->upcoming_state = GET_STATUS_SEND;
2298 tcpm_ams_start(port, GETTING_SOURCE_SINK_STATUS);
2299 } else {
2300 /*
2301 * Do not check SinkTxOk here in case the Source doesn't set its Rp to
2302 * SinkTxOk in time.
2303 */
2304 port->ams = GETTING_SOURCE_SINK_STATUS;
2305 tcpm_set_state(port, GET_STATUS_SEND, 0);
2306 }
2307 } else {
2308 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
2309 }
2310 }
2311
tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port * port,enum typec_pwr_opmode mode,bool pps_active,u32 requested_vbus_voltage)2312 static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port,
2313 enum typec_pwr_opmode mode, bool pps_active,
2314 u32 requested_vbus_voltage)
2315 {
2316 int ret;
2317
2318 if (!port->tcpc->set_auto_vbus_discharge_threshold)
2319 return 0;
2320
2321 ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, mode, pps_active,
2322 requested_vbus_voltage);
2323 tcpm_log_force(port,
2324 "set_auto_vbus_discharge_threshold mode:%d pps_active:%c vbus:%u ret:%d",
2325 mode, pps_active ? 'y' : 'n', requested_vbus_voltage, ret);
2326
2327 return ret;
2328 }
2329
tcpm_pd_handle_state(struct tcpm_port * port,enum tcpm_state state,enum tcpm_ams ams,unsigned int delay_ms)2330 static void tcpm_pd_handle_state(struct tcpm_port *port,
2331 enum tcpm_state state,
2332 enum tcpm_ams ams,
2333 unsigned int delay_ms)
2334 {
2335 switch (port->state) {
2336 case SRC_READY:
2337 case SNK_READY:
2338 port->ams = ams;
2339 tcpm_set_state(port, state, delay_ms);
2340 break;
2341 /* 8.3.3.4.1.1 and 6.8.1 power transitioning */
2342 case SNK_TRANSITION_SINK:
2343 case SNK_TRANSITION_SINK_VBUS:
2344 case SRC_TRANSITION_SUPPLY:
2345 tcpm_set_state(port, HARD_RESET_SEND, 0);
2346 break;
2347 default:
2348 if (!tcpm_ams_interruptible(port)) {
2349 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
2350 SRC_SOFT_RESET_WAIT_SNK_TX :
2351 SNK_SOFT_RESET,
2352 0);
2353 } else {
2354 /* process the Message 6.8.1 */
2355 port->upcoming_state = state;
2356 port->next_ams = ams;
2357 tcpm_set_state(port, ready_state(port), delay_ms);
2358 }
2359 break;
2360 }
2361 }
2362
tcpm_pd_handle_msg(struct tcpm_port * port,enum pd_msg_request message,enum tcpm_ams ams)2363 static void tcpm_pd_handle_msg(struct tcpm_port *port,
2364 enum pd_msg_request message,
2365 enum tcpm_ams ams)
2366 {
2367 switch (port->state) {
2368 case SRC_READY:
2369 case SNK_READY:
2370 port->ams = ams;
2371 tcpm_queue_message(port, message);
2372 break;
2373 /* PD 3.0 Spec 8.3.3.4.1.1 and 6.8.1 */
2374 case SNK_TRANSITION_SINK:
2375 case SNK_TRANSITION_SINK_VBUS:
2376 case SRC_TRANSITION_SUPPLY:
2377 tcpm_set_state(port, HARD_RESET_SEND, 0);
2378 break;
2379 default:
2380 if (!tcpm_ams_interruptible(port)) {
2381 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
2382 SRC_SOFT_RESET_WAIT_SNK_TX :
2383 SNK_SOFT_RESET,
2384 0);
2385 } else {
2386 port->next_ams = ams;
2387 tcpm_set_state(port, ready_state(port), 0);
2388 /* 6.8.1 process the Message */
2389 tcpm_queue_message(port, message);
2390 }
2391 break;
2392 }
2393 }
2394
tcpm_register_source_caps(struct tcpm_port * port)2395 static int tcpm_register_source_caps(struct tcpm_port *port)
2396 {
2397 struct usb_power_delivery_desc desc = { port->negotiated_rev };
2398 struct usb_power_delivery_capabilities_desc caps = { };
2399 struct usb_power_delivery_capabilities *cap;
2400
2401 if (!port->partner_pd)
2402 port->partner_pd = usb_power_delivery_register(NULL, &desc);
2403 if (IS_ERR(port->partner_pd))
2404 return PTR_ERR(port->partner_pd);
2405
2406 memcpy(caps.pdo, port->source_caps, sizeof(u32) * port->nr_source_caps);
2407 caps.role = TYPEC_SOURCE;
2408
2409 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps);
2410 if (IS_ERR(cap))
2411 return PTR_ERR(cap);
2412
2413 port->partner_source_caps = cap;
2414
2415 return 0;
2416 }
2417
tcpm_register_sink_caps(struct tcpm_port * port)2418 static int tcpm_register_sink_caps(struct tcpm_port *port)
2419 {
2420 struct usb_power_delivery_desc desc = { port->negotiated_rev };
2421 struct usb_power_delivery_capabilities_desc caps = { };
2422 struct usb_power_delivery_capabilities *cap;
2423
2424 if (!port->partner_pd)
2425 port->partner_pd = usb_power_delivery_register(NULL, &desc);
2426 if (IS_ERR(port->partner_pd))
2427 return PTR_ERR(port->partner_pd);
2428
2429 memcpy(caps.pdo, port->sink_caps, sizeof(u32) * port->nr_sink_caps);
2430 caps.role = TYPEC_SINK;
2431
2432 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps);
2433 if (IS_ERR(cap))
2434 return PTR_ERR(cap);
2435
2436 port->partner_sink_caps = cap;
2437
2438 return 0;
2439 }
2440
tcpm_pd_data_request(struct tcpm_port * port,const struct pd_message * msg)2441 static void tcpm_pd_data_request(struct tcpm_port *port,
2442 const struct pd_message *msg)
2443 {
2444 enum pd_data_msg_type type = pd_header_type_le(msg->header);
2445 unsigned int cnt = pd_header_cnt_le(msg->header);
2446 unsigned int rev = pd_header_rev_le(msg->header);
2447 unsigned int i;
2448 enum frs_typec_current partner_frs_current;
2449 bool frs_enable;
2450 int ret;
2451
2452 if (tcpm_vdm_ams(port) && type != PD_DATA_VENDOR_DEF) {
2453 port->vdm_state = VDM_STATE_ERR_BUSY;
2454 tcpm_ams_finish(port);
2455 mod_vdm_delayed_work(port, 0);
2456 }
2457
2458 switch (type) {
2459 case PD_DATA_SOURCE_CAP:
2460 for (i = 0; i < cnt; i++)
2461 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
2462
2463 port->nr_source_caps = cnt;
2464
2465 tcpm_log_source_caps(port);
2466
2467 tcpm_validate_caps(port, port->source_caps,
2468 port->nr_source_caps);
2469
2470 tcpm_register_source_caps(port);
2471
2472 /*
2473 * Adjust revision in subsequent message headers, as required,
2474 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
2475 * support Rev 1.0 so just do nothing in that scenario.
2476 */
2477 if (rev == PD_REV10) {
2478 if (port->ams == GET_SOURCE_CAPABILITIES)
2479 tcpm_ams_finish(port);
2480 break;
2481 }
2482
2483 if (rev < PD_MAX_REV)
2484 port->negotiated_rev = rev;
2485
2486 if (port->pwr_role == TYPEC_SOURCE) {
2487 if (port->ams == GET_SOURCE_CAPABILITIES)
2488 tcpm_pd_handle_state(port, SRC_READY, NONE_AMS, 0);
2489 /* Unexpected Source Capabilities */
2490 else
2491 tcpm_pd_handle_msg(port,
2492 port->negotiated_rev < PD_REV30 ?
2493 PD_MSG_CTRL_REJECT :
2494 PD_MSG_CTRL_NOT_SUPP,
2495 NONE_AMS);
2496 } else if (port->state == SNK_WAIT_CAPABILITIES) {
2497 /*
2498 * This message may be received even if VBUS is not
2499 * present. This is quite unexpected; see USB PD
2500 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
2501 * However, at the same time, we must be ready to
2502 * receive this message and respond to it 15ms after
2503 * receiving PS_RDY during power swap operations, no matter
2504 * if VBUS is available or not (USB PD specification,
2505 * section 6.5.9.2).
2506 * So we need to accept the message either way,
2507 * but be prepared to keep waiting for VBUS after it was
2508 * handled.
2509 */
2510 port->ams = POWER_NEGOTIATION;
2511 port->in_ams = true;
2512 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
2513 } else {
2514 if (port->ams == GET_SOURCE_CAPABILITIES)
2515 tcpm_ams_finish(port);
2516 tcpm_pd_handle_state(port, SNK_NEGOTIATE_CAPABILITIES,
2517 POWER_NEGOTIATION, 0);
2518 }
2519 break;
2520 case PD_DATA_REQUEST:
2521 /*
2522 * Adjust revision in subsequent message headers, as required,
2523 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
2524 * support Rev 1.0 so just reject in that scenario.
2525 */
2526 if (rev == PD_REV10) {
2527 tcpm_pd_handle_msg(port,
2528 port->negotiated_rev < PD_REV30 ?
2529 PD_MSG_CTRL_REJECT :
2530 PD_MSG_CTRL_NOT_SUPP,
2531 NONE_AMS);
2532 break;
2533 }
2534
2535 if (rev < PD_MAX_REV)
2536 port->negotiated_rev = rev;
2537
2538 if (port->pwr_role != TYPEC_SOURCE || cnt != 1) {
2539 tcpm_pd_handle_msg(port,
2540 port->negotiated_rev < PD_REV30 ?
2541 PD_MSG_CTRL_REJECT :
2542 PD_MSG_CTRL_NOT_SUPP,
2543 NONE_AMS);
2544 break;
2545 }
2546
2547 port->sink_request = le32_to_cpu(msg->payload[0]);
2548
2549 if (port->vdm_sm_running && port->explicit_contract) {
2550 tcpm_pd_handle_msg(port, PD_MSG_CTRL_WAIT, port->ams);
2551 break;
2552 }
2553
2554 if (port->state == SRC_SEND_CAPABILITIES)
2555 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
2556 else
2557 tcpm_pd_handle_state(port, SRC_NEGOTIATE_CAPABILITIES,
2558 POWER_NEGOTIATION, 0);
2559 break;
2560 case PD_DATA_SINK_CAP:
2561 /* We don't do anything with this at the moment... */
2562 for (i = 0; i < cnt; i++)
2563 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
2564
2565 partner_frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >>
2566 PDO_FIXED_FRS_CURR_SHIFT;
2567 frs_enable = partner_frs_current && (partner_frs_current <=
2568 port->new_source_frs_current);
2569 tcpm_log(port,
2570 "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c",
2571 partner_frs_current, port->new_source_frs_current, frs_enable ? 'y' : 'n');
2572 if (frs_enable) {
2573 ret = port->tcpc->enable_frs(port->tcpc, true);
2574 tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret);
2575 }
2576
2577 port->nr_sink_caps = cnt;
2578 port->sink_cap_done = true;
2579 tcpm_register_sink_caps(port);
2580
2581 if (port->ams == GET_SINK_CAPABILITIES)
2582 tcpm_set_state(port, ready_state(port), 0);
2583 /* Unexpected Sink Capabilities */
2584 else
2585 tcpm_pd_handle_msg(port,
2586 port->negotiated_rev < PD_REV30 ?
2587 PD_MSG_CTRL_REJECT :
2588 PD_MSG_CTRL_NOT_SUPP,
2589 NONE_AMS);
2590 break;
2591 case PD_DATA_VENDOR_DEF:
2592 tcpm_handle_vdm_request(port, msg->payload, cnt);
2593 break;
2594 case PD_DATA_BIST:
2595 port->bist_request = le32_to_cpu(msg->payload[0]);
2596 tcpm_pd_handle_state(port, BIST_RX, BIST, 0);
2597 break;
2598 case PD_DATA_ALERT:
2599 if (port->state != SRC_READY && port->state != SNK_READY)
2600 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ?
2601 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET,
2602 NONE_AMS, 0);
2603 else
2604 tcpm_handle_alert(port, msg->payload, cnt);
2605 break;
2606 case PD_DATA_BATT_STATUS:
2607 case PD_DATA_GET_COUNTRY_INFO:
2608 /* Currently unsupported */
2609 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ?
2610 PD_MSG_CTRL_REJECT :
2611 PD_MSG_CTRL_NOT_SUPP,
2612 NONE_AMS);
2613 break;
2614 default:
2615 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ?
2616 PD_MSG_CTRL_REJECT :
2617 PD_MSG_CTRL_NOT_SUPP,
2618 NONE_AMS);
2619 tcpm_log(port, "Unrecognized data message type %#x", type);
2620 break;
2621 }
2622 }
2623
tcpm_pps_complete(struct tcpm_port * port,int result)2624 static void tcpm_pps_complete(struct tcpm_port *port, int result)
2625 {
2626 if (port->pps_pending) {
2627 port->pps_status = result;
2628 port->pps_pending = false;
2629 complete(&port->pps_complete);
2630 }
2631 }
2632
tcpm_pd_ctrl_request(struct tcpm_port * port,const struct pd_message * msg)2633 static void tcpm_pd_ctrl_request(struct tcpm_port *port,
2634 const struct pd_message *msg)
2635 {
2636 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
2637 enum tcpm_state next_state;
2638
2639 /*
2640 * Stop VDM state machine if interrupted by other Messages while NOT_SUPP is allowed in
2641 * VDM AMS if waiting for VDM responses and will be handled later.
2642 */
2643 if (tcpm_vdm_ams(port) && type != PD_CTRL_NOT_SUPP && type != PD_CTRL_GOOD_CRC) {
2644 port->vdm_state = VDM_STATE_ERR_BUSY;
2645 tcpm_ams_finish(port);
2646 mod_vdm_delayed_work(port, 0);
2647 }
2648
2649 switch (type) {
2650 case PD_CTRL_GOOD_CRC:
2651 case PD_CTRL_PING:
2652 break;
2653 case PD_CTRL_GET_SOURCE_CAP:
2654 tcpm_pd_handle_msg(port, PD_MSG_DATA_SOURCE_CAP, GET_SOURCE_CAPABILITIES);
2655 break;
2656 case PD_CTRL_GET_SINK_CAP:
2657 tcpm_pd_handle_msg(port, PD_MSG_DATA_SINK_CAP, GET_SINK_CAPABILITIES);
2658 break;
2659 case PD_CTRL_GOTO_MIN:
2660 break;
2661 case PD_CTRL_PS_RDY:
2662 switch (port->state) {
2663 case SNK_TRANSITION_SINK:
2664 if (port->vbus_present) {
2665 tcpm_set_current_limit(port,
2666 port->req_current_limit,
2667 port->req_supply_voltage);
2668 port->explicit_contract = true;
2669 tcpm_set_auto_vbus_discharge_threshold(port,
2670 TYPEC_PWR_MODE_PD,
2671 port->pps_data.active,
2672 port->supply_voltage);
2673 tcpm_set_state(port, SNK_READY, 0);
2674 } else {
2675 /*
2676 * Seen after power swap. Keep waiting for VBUS
2677 * in a transitional state.
2678 */
2679 tcpm_set_state(port,
2680 SNK_TRANSITION_SINK_VBUS, 0);
2681 }
2682 break;
2683 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
2684 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
2685 break;
2686 case PR_SWAP_SNK_SRC_SINK_OFF:
2687 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
2688 break;
2689 case VCONN_SWAP_WAIT_FOR_VCONN:
2690 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
2691 break;
2692 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
2693 tcpm_set_state(port, FR_SWAP_SNK_SRC_NEW_SINK_READY, 0);
2694 break;
2695 default:
2696 tcpm_pd_handle_state(port,
2697 port->pwr_role == TYPEC_SOURCE ?
2698 SRC_SOFT_RESET_WAIT_SNK_TX :
2699 SNK_SOFT_RESET,
2700 NONE_AMS, 0);
2701 break;
2702 }
2703 break;
2704 case PD_CTRL_REJECT:
2705 case PD_CTRL_WAIT:
2706 case PD_CTRL_NOT_SUPP:
2707 switch (port->state) {
2708 case SNK_NEGOTIATE_CAPABILITIES:
2709 /* USB PD specification, Figure 8-43 */
2710 if (port->explicit_contract)
2711 next_state = SNK_READY;
2712 else
2713 next_state = SNK_WAIT_CAPABILITIES;
2714
2715 /* Threshold was relaxed before sending Request. Restore it back. */
2716 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
2717 port->pps_data.active,
2718 port->supply_voltage);
2719 tcpm_set_state(port, next_state, 0);
2720 break;
2721 case SNK_NEGOTIATE_PPS_CAPABILITIES:
2722 /* Revert data back from any requested PPS updates */
2723 port->pps_data.req_out_volt = port->supply_voltage;
2724 port->pps_data.req_op_curr = port->current_limit;
2725 port->pps_status = (type == PD_CTRL_WAIT ?
2726 -EAGAIN : -EOPNOTSUPP);
2727
2728 /* Threshold was relaxed before sending Request. Restore it back. */
2729 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
2730 port->pps_data.active,
2731 port->supply_voltage);
2732
2733 tcpm_set_state(port, SNK_READY, 0);
2734 break;
2735 case DR_SWAP_SEND:
2736 port->swap_status = (type == PD_CTRL_WAIT ?
2737 -EAGAIN : -EOPNOTSUPP);
2738 tcpm_set_state(port, DR_SWAP_CANCEL, 0);
2739 break;
2740 case PR_SWAP_SEND:
2741 port->swap_status = (type == PD_CTRL_WAIT ?
2742 -EAGAIN : -EOPNOTSUPP);
2743 tcpm_set_state(port, PR_SWAP_CANCEL, 0);
2744 break;
2745 case VCONN_SWAP_SEND:
2746 port->swap_status = (type == PD_CTRL_WAIT ?
2747 -EAGAIN : -EOPNOTSUPP);
2748 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
2749 break;
2750 case FR_SWAP_SEND:
2751 tcpm_set_state(port, FR_SWAP_CANCEL, 0);
2752 break;
2753 case GET_SINK_CAP:
2754 port->sink_cap_done = true;
2755 tcpm_set_state(port, ready_state(port), 0);
2756 break;
2757 /*
2758 * Some port partners do not support GET_STATUS, avoid soft reset the link to
2759 * prevent redundant power re-negotiation
2760 */
2761 case GET_STATUS_SEND:
2762 tcpm_set_state(port, ready_state(port), 0);
2763 break;
2764 case SRC_READY:
2765 case SNK_READY:
2766 if (port->vdm_state > VDM_STATE_READY) {
2767 port->vdm_state = VDM_STATE_DONE;
2768 if (tcpm_vdm_ams(port))
2769 tcpm_ams_finish(port);
2770 mod_vdm_delayed_work(port, 0);
2771 break;
2772 }
2773 fallthrough;
2774 default:
2775 tcpm_pd_handle_state(port,
2776 port->pwr_role == TYPEC_SOURCE ?
2777 SRC_SOFT_RESET_WAIT_SNK_TX :
2778 SNK_SOFT_RESET,
2779 NONE_AMS, 0);
2780 break;
2781 }
2782 break;
2783 case PD_CTRL_ACCEPT:
2784 switch (port->state) {
2785 case SNK_NEGOTIATE_CAPABILITIES:
2786 port->pps_data.active = false;
2787 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
2788 break;
2789 case SNK_NEGOTIATE_PPS_CAPABILITIES:
2790 port->pps_data.active = true;
2791 port->pps_data.min_volt = port->pps_data.req_min_volt;
2792 port->pps_data.max_volt = port->pps_data.req_max_volt;
2793 port->pps_data.max_curr = port->pps_data.req_max_curr;
2794 port->req_supply_voltage = port->pps_data.req_out_volt;
2795 port->req_current_limit = port->pps_data.req_op_curr;
2796 power_supply_changed(port->psy);
2797 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
2798 break;
2799 case SOFT_RESET_SEND:
2800 if (port->ams == SOFT_RESET_AMS)
2801 tcpm_ams_finish(port);
2802 if (port->pwr_role == TYPEC_SOURCE) {
2803 port->upcoming_state = SRC_SEND_CAPABILITIES;
2804 tcpm_ams_start(port, POWER_NEGOTIATION);
2805 } else {
2806 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2807 }
2808 break;
2809 case DR_SWAP_SEND:
2810 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
2811 break;
2812 case PR_SWAP_SEND:
2813 tcpm_set_state(port, PR_SWAP_START, 0);
2814 break;
2815 case VCONN_SWAP_SEND:
2816 tcpm_set_state(port, VCONN_SWAP_START, 0);
2817 break;
2818 case FR_SWAP_SEND:
2819 tcpm_set_state(port, FR_SWAP_SNK_SRC_TRANSITION_TO_OFF, 0);
2820 break;
2821 default:
2822 tcpm_pd_handle_state(port,
2823 port->pwr_role == TYPEC_SOURCE ?
2824 SRC_SOFT_RESET_WAIT_SNK_TX :
2825 SNK_SOFT_RESET,
2826 NONE_AMS, 0);
2827 break;
2828 }
2829 break;
2830 case PD_CTRL_SOFT_RESET:
2831 port->ams = SOFT_RESET_AMS;
2832 tcpm_set_state(port, SOFT_RESET, 0);
2833 break;
2834 case PD_CTRL_DR_SWAP:
2835 /*
2836 * XXX
2837 * 6.3.9: If an alternate mode is active, a request to swap
2838 * alternate modes shall trigger a port reset.
2839 */
2840 if (port->typec_caps.data != TYPEC_PORT_DRD) {
2841 tcpm_pd_handle_msg(port,
2842 port->negotiated_rev < PD_REV30 ?
2843 PD_MSG_CTRL_REJECT :
2844 PD_MSG_CTRL_NOT_SUPP,
2845 NONE_AMS);
2846 } else {
2847 if (port->send_discover) {
2848 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
2849 break;
2850 }
2851
2852 tcpm_pd_handle_state(port, DR_SWAP_ACCEPT, DATA_ROLE_SWAP, 0);
2853 }
2854 break;
2855 case PD_CTRL_PR_SWAP:
2856 if (port->port_type != TYPEC_PORT_DRP) {
2857 tcpm_pd_handle_msg(port,
2858 port->negotiated_rev < PD_REV30 ?
2859 PD_MSG_CTRL_REJECT :
2860 PD_MSG_CTRL_NOT_SUPP,
2861 NONE_AMS);
2862 } else {
2863 if (port->send_discover) {
2864 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
2865 break;
2866 }
2867
2868 tcpm_pd_handle_state(port, PR_SWAP_ACCEPT, POWER_ROLE_SWAP, 0);
2869 }
2870 break;
2871 case PD_CTRL_VCONN_SWAP:
2872 if (port->send_discover) {
2873 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
2874 break;
2875 }
2876
2877 tcpm_pd_handle_state(port, VCONN_SWAP_ACCEPT, VCONN_SWAP, 0);
2878 break;
2879 case PD_CTRL_GET_SOURCE_CAP_EXT:
2880 case PD_CTRL_GET_STATUS:
2881 case PD_CTRL_FR_SWAP:
2882 case PD_CTRL_GET_PPS_STATUS:
2883 case PD_CTRL_GET_COUNTRY_CODES:
2884 /* Currently not supported */
2885 tcpm_pd_handle_msg(port,
2886 port->negotiated_rev < PD_REV30 ?
2887 PD_MSG_CTRL_REJECT :
2888 PD_MSG_CTRL_NOT_SUPP,
2889 NONE_AMS);
2890 break;
2891 default:
2892 tcpm_pd_handle_msg(port,
2893 port->negotiated_rev < PD_REV30 ?
2894 PD_MSG_CTRL_REJECT :
2895 PD_MSG_CTRL_NOT_SUPP,
2896 NONE_AMS);
2897 tcpm_log(port, "Unrecognized ctrl message type %#x", type);
2898 break;
2899 }
2900 }
2901
tcpm_pd_ext_msg_request(struct tcpm_port * port,const struct pd_message * msg)2902 static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
2903 const struct pd_message *msg)
2904 {
2905 enum pd_ext_msg_type type = pd_header_type_le(msg->header);
2906 unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header);
2907
2908 /* stopping VDM state machine if interrupted by other Messages */
2909 if (tcpm_vdm_ams(port)) {
2910 port->vdm_state = VDM_STATE_ERR_BUSY;
2911 tcpm_ams_finish(port);
2912 mod_vdm_delayed_work(port, 0);
2913 }
2914
2915 if (!(le16_to_cpu(msg->ext_msg.header) & PD_EXT_HDR_CHUNKED)) {
2916 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2917 tcpm_log(port, "Unchunked extended messages unsupported");
2918 return;
2919 }
2920
2921 if (data_size > PD_EXT_MAX_CHUNK_DATA) {
2922 tcpm_pd_handle_state(port, CHUNK_NOT_SUPP, NONE_AMS, PD_T_CHUNK_NOT_SUPP);
2923 tcpm_log(port, "Chunk handling not yet supported");
2924 return;
2925 }
2926
2927 switch (type) {
2928 case PD_EXT_STATUS:
2929 case PD_EXT_PPS_STATUS:
2930 if (port->ams == GETTING_SOURCE_SINK_STATUS) {
2931 tcpm_ams_finish(port);
2932 tcpm_set_state(port, ready_state(port), 0);
2933 } else {
2934 /* unexpected Status or PPS_Status Message */
2935 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ?
2936 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET,
2937 NONE_AMS, 0);
2938 }
2939 break;
2940 case PD_EXT_SOURCE_CAP_EXT:
2941 case PD_EXT_GET_BATT_CAP:
2942 case PD_EXT_GET_BATT_STATUS:
2943 case PD_EXT_BATT_CAP:
2944 case PD_EXT_GET_MANUFACTURER_INFO:
2945 case PD_EXT_MANUFACTURER_INFO:
2946 case PD_EXT_SECURITY_REQUEST:
2947 case PD_EXT_SECURITY_RESPONSE:
2948 case PD_EXT_FW_UPDATE_REQUEST:
2949 case PD_EXT_FW_UPDATE_RESPONSE:
2950 case PD_EXT_COUNTRY_INFO:
2951 case PD_EXT_COUNTRY_CODES:
2952 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2953 break;
2954 default:
2955 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2956 tcpm_log(port, "Unrecognized extended message type %#x", type);
2957 break;
2958 }
2959 }
2960
tcpm_pd_rx_handler(struct kthread_work * work)2961 static void tcpm_pd_rx_handler(struct kthread_work *work)
2962 {
2963 struct pd_rx_event *event = container_of(work,
2964 struct pd_rx_event, work);
2965 const struct pd_message *msg = &event->msg;
2966 unsigned int cnt = pd_header_cnt_le(msg->header);
2967 struct tcpm_port *port = event->port;
2968
2969 mutex_lock(&port->lock);
2970
2971 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
2972 port->attached);
2973
2974 if (port->attached) {
2975 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
2976 unsigned int msgid = pd_header_msgid_le(msg->header);
2977
2978 /*
2979 * USB PD standard, 6.6.1.2:
2980 * "... if MessageID value in a received Message is the
2981 * same as the stored value, the receiver shall return a
2982 * GoodCRC Message with that MessageID value and drop
2983 * the Message (this is a retry of an already received
2984 * Message). Note: this shall not apply to the Soft_Reset
2985 * Message which always has a MessageID value of zero."
2986 */
2987 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
2988 goto done;
2989 port->rx_msgid = msgid;
2990
2991 /*
2992 * If both ends believe to be DFP/host, we have a data role
2993 * mismatch.
2994 */
2995 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
2996 (port->data_role == TYPEC_HOST)) {
2997 tcpm_log(port,
2998 "Data role mismatch, initiating error recovery");
2999 tcpm_set_state(port, ERROR_RECOVERY, 0);
3000 } else {
3001 if (le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR)
3002 tcpm_pd_ext_msg_request(port, msg);
3003 else if (cnt)
3004 tcpm_pd_data_request(port, msg);
3005 else
3006 tcpm_pd_ctrl_request(port, msg);
3007 }
3008 }
3009
3010 done:
3011 mutex_unlock(&port->lock);
3012 kfree(event);
3013 }
3014
tcpm_pd_receive(struct tcpm_port * port,const struct pd_message * msg)3015 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg)
3016 {
3017 struct pd_rx_event *event;
3018
3019 event = kzalloc(sizeof(*event), GFP_ATOMIC);
3020 if (!event)
3021 return;
3022
3023 kthread_init_work(&event->work, tcpm_pd_rx_handler);
3024 event->port = port;
3025 memcpy(&event->msg, msg, sizeof(*msg));
3026 kthread_queue_work(port->wq, &event->work);
3027 }
3028 EXPORT_SYMBOL_GPL(tcpm_pd_receive);
3029
tcpm_pd_send_control(struct tcpm_port * port,enum pd_ctrl_msg_type type)3030 static int tcpm_pd_send_control(struct tcpm_port *port,
3031 enum pd_ctrl_msg_type type)
3032 {
3033 struct pd_message msg;
3034
3035 memset(&msg, 0, sizeof(msg));
3036 msg.header = PD_HEADER_LE(type, port->pwr_role,
3037 port->data_role,
3038 port->negotiated_rev,
3039 port->message_id, 0);
3040
3041 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
3042 }
3043
3044 /*
3045 * Send queued message without affecting state.
3046 * Return true if state machine should go back to sleep,
3047 * false otherwise.
3048 */
tcpm_send_queued_message(struct tcpm_port * port)3049 static bool tcpm_send_queued_message(struct tcpm_port *port)
3050 {
3051 enum pd_msg_request queued_message;
3052 int ret;
3053
3054 do {
3055 queued_message = port->queued_message;
3056 port->queued_message = PD_MSG_NONE;
3057
3058 switch (queued_message) {
3059 case PD_MSG_CTRL_WAIT:
3060 tcpm_pd_send_control(port, PD_CTRL_WAIT);
3061 break;
3062 case PD_MSG_CTRL_REJECT:
3063 tcpm_pd_send_control(port, PD_CTRL_REJECT);
3064 break;
3065 case PD_MSG_CTRL_NOT_SUPP:
3066 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP);
3067 break;
3068 case PD_MSG_DATA_SINK_CAP:
3069 ret = tcpm_pd_send_sink_caps(port);
3070 if (ret < 0) {
3071 tcpm_log(port, "Unable to send snk caps, ret=%d", ret);
3072 tcpm_set_state(port, SNK_SOFT_RESET, 0);
3073 }
3074 tcpm_ams_finish(port);
3075 break;
3076 case PD_MSG_DATA_SOURCE_CAP:
3077 ret = tcpm_pd_send_source_caps(port);
3078 if (ret < 0) {
3079 tcpm_log(port,
3080 "Unable to send src caps, ret=%d",
3081 ret);
3082 tcpm_set_state(port, SOFT_RESET_SEND, 0);
3083 } else if (port->pwr_role == TYPEC_SOURCE) {
3084 tcpm_ams_finish(port);
3085 tcpm_set_state(port, HARD_RESET_SEND,
3086 PD_T_SENDER_RESPONSE);
3087 } else {
3088 tcpm_ams_finish(port);
3089 }
3090 break;
3091 default:
3092 break;
3093 }
3094 } while (port->queued_message != PD_MSG_NONE);
3095
3096 if (port->delayed_state != INVALID_STATE) {
3097 if (ktime_after(port->delayed_runtime, ktime_get())) {
3098 mod_tcpm_delayed_work(port, ktime_to_ms(ktime_sub(port->delayed_runtime,
3099 ktime_get())));
3100 return true;
3101 }
3102 port->delayed_state = INVALID_STATE;
3103 }
3104 return false;
3105 }
3106
tcpm_pd_check_request(struct tcpm_port * port)3107 static int tcpm_pd_check_request(struct tcpm_port *port)
3108 {
3109 u32 pdo, rdo = port->sink_request;
3110 unsigned int max, op, pdo_max, index;
3111 enum pd_pdo_type type;
3112
3113 index = rdo_index(rdo);
3114 if (!index || index > port->nr_src_pdo)
3115 return -EINVAL;
3116
3117 pdo = port->src_pdo[index - 1];
3118 type = pdo_type(pdo);
3119 switch (type) {
3120 case PDO_TYPE_FIXED:
3121 case PDO_TYPE_VAR:
3122 max = rdo_max_current(rdo);
3123 op = rdo_op_current(rdo);
3124 pdo_max = pdo_max_current(pdo);
3125
3126 if (op > pdo_max)
3127 return -EINVAL;
3128 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
3129 return -EINVAL;
3130
3131 if (type == PDO_TYPE_FIXED)
3132 tcpm_log(port,
3133 "Requested %u mV, %u mA for %u / %u mA",
3134 pdo_fixed_voltage(pdo), pdo_max, op, max);
3135 else
3136 tcpm_log(port,
3137 "Requested %u -> %u mV, %u mA for %u / %u mA",
3138 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
3139 pdo_max, op, max);
3140 break;
3141 case PDO_TYPE_BATT:
3142 max = rdo_max_power(rdo);
3143 op = rdo_op_power(rdo);
3144 pdo_max = pdo_max_power(pdo);
3145
3146 if (op > pdo_max)
3147 return -EINVAL;
3148 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
3149 return -EINVAL;
3150 tcpm_log(port,
3151 "Requested %u -> %u mV, %u mW for %u / %u mW",
3152 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
3153 pdo_max, op, max);
3154 break;
3155 default:
3156 return -EINVAL;
3157 }
3158
3159 port->op_vsafe5v = index == 1;
3160
3161 return 0;
3162 }
3163
3164 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
3165 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
3166
tcpm_pd_select_pdo(struct tcpm_port * port,int * sink_pdo,int * src_pdo)3167 static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
3168 int *src_pdo)
3169 {
3170 unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
3171 max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
3172 min_snk_mv = 0;
3173 int ret = -EINVAL;
3174
3175 port->pps_data.supported = false;
3176 port->usb_type = POWER_SUPPLY_USB_TYPE_PD;
3177 power_supply_changed(port->psy);
3178
3179 /*
3180 * Select the source PDO providing the most power which has a
3181 * matchig sink cap.
3182 */
3183 for (i = 0; i < port->nr_source_caps; i++) {
3184 u32 pdo = port->source_caps[i];
3185 enum pd_pdo_type type = pdo_type(pdo);
3186
3187 switch (type) {
3188 case PDO_TYPE_FIXED:
3189 max_src_mv = pdo_fixed_voltage(pdo);
3190 min_src_mv = max_src_mv;
3191 break;
3192 case PDO_TYPE_BATT:
3193 case PDO_TYPE_VAR:
3194 max_src_mv = pdo_max_voltage(pdo);
3195 min_src_mv = pdo_min_voltage(pdo);
3196 break;
3197 case PDO_TYPE_APDO:
3198 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) {
3199 port->pps_data.supported = true;
3200 port->usb_type =
3201 POWER_SUPPLY_USB_TYPE_PD_PPS;
3202 power_supply_changed(port->psy);
3203 }
3204 continue;
3205 default:
3206 tcpm_log(port, "Invalid source PDO type, ignoring");
3207 continue;
3208 }
3209
3210 switch (type) {
3211 case PDO_TYPE_FIXED:
3212 case PDO_TYPE_VAR:
3213 src_ma = pdo_max_current(pdo);
3214 src_mw = src_ma * min_src_mv / 1000;
3215 break;
3216 case PDO_TYPE_BATT:
3217 src_mw = pdo_max_power(pdo);
3218 break;
3219 case PDO_TYPE_APDO:
3220 continue;
3221 default:
3222 tcpm_log(port, "Invalid source PDO type, ignoring");
3223 continue;
3224 }
3225
3226 for (j = 0; j < port->nr_snk_pdo; j++) {
3227 pdo = port->snk_pdo[j];
3228
3229 switch (pdo_type(pdo)) {
3230 case PDO_TYPE_FIXED:
3231 max_snk_mv = pdo_fixed_voltage(pdo);
3232 min_snk_mv = max_snk_mv;
3233 break;
3234 case PDO_TYPE_BATT:
3235 case PDO_TYPE_VAR:
3236 max_snk_mv = pdo_max_voltage(pdo);
3237 min_snk_mv = pdo_min_voltage(pdo);
3238 break;
3239 case PDO_TYPE_APDO:
3240 continue;
3241 default:
3242 tcpm_log(port, "Invalid sink PDO type, ignoring");
3243 continue;
3244 }
3245
3246 if (max_src_mv <= max_snk_mv &&
3247 min_src_mv >= min_snk_mv) {
3248 /* Prefer higher voltages if available */
3249 if ((src_mw == max_mw && min_src_mv > max_mv) ||
3250 src_mw > max_mw) {
3251 *src_pdo = i;
3252 *sink_pdo = j;
3253 max_mw = src_mw;
3254 max_mv = min_src_mv;
3255 ret = 0;
3256 }
3257 }
3258 }
3259 }
3260
3261 return ret;
3262 }
3263
tcpm_pd_select_pps_apdo(struct tcpm_port * port)3264 static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port)
3265 {
3266 unsigned int i, src_ma, max_temp_mw = 0, max_op_ma, op_mw;
3267 unsigned int src_pdo = 0;
3268 u32 pdo, src;
3269
3270 for (i = 1; i < port->nr_source_caps; ++i) {
3271 pdo = port->source_caps[i];
3272
3273 switch (pdo_type(pdo)) {
3274 case PDO_TYPE_APDO:
3275 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
3276 tcpm_log(port, "Not PPS APDO (source), ignoring");
3277 continue;
3278 }
3279
3280 if (port->pps_data.req_out_volt > pdo_pps_apdo_max_voltage(pdo) ||
3281 port->pps_data.req_out_volt < pdo_pps_apdo_min_voltage(pdo))
3282 continue;
3283
3284 src_ma = pdo_pps_apdo_max_current(pdo);
3285 max_op_ma = min(src_ma, port->pps_data.req_op_curr);
3286 op_mw = max_op_ma * port->pps_data.req_out_volt / 1000;
3287 if (op_mw > max_temp_mw) {
3288 src_pdo = i;
3289 max_temp_mw = op_mw;
3290 }
3291 break;
3292 default:
3293 tcpm_log(port, "Not APDO type (source), ignoring");
3294 continue;
3295 }
3296 }
3297
3298 if (src_pdo) {
3299 src = port->source_caps[src_pdo];
3300
3301 port->pps_data.req_min_volt = pdo_pps_apdo_min_voltage(src);
3302 port->pps_data.req_max_volt = pdo_pps_apdo_max_voltage(src);
3303 port->pps_data.req_max_curr = pdo_pps_apdo_max_current(src);
3304 port->pps_data.req_op_curr = min(port->pps_data.req_max_curr,
3305 port->pps_data.req_op_curr);
3306 }
3307
3308 return src_pdo;
3309 }
3310
tcpm_pd_build_request(struct tcpm_port * port,u32 * rdo)3311 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo)
3312 {
3313 unsigned int mv, ma, mw, flags;
3314 unsigned int max_ma, max_mw;
3315 enum pd_pdo_type type;
3316 u32 pdo, matching_snk_pdo;
3317 int src_pdo_index = 0;
3318 int snk_pdo_index = 0;
3319 int ret;
3320
3321 ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index);
3322 if (ret < 0)
3323 return ret;
3324
3325 pdo = port->source_caps[src_pdo_index];
3326 matching_snk_pdo = port->snk_pdo[snk_pdo_index];
3327 type = pdo_type(pdo);
3328
3329 switch (type) {
3330 case PDO_TYPE_FIXED:
3331 mv = pdo_fixed_voltage(pdo);
3332 break;
3333 case PDO_TYPE_BATT:
3334 case PDO_TYPE_VAR:
3335 mv = pdo_min_voltage(pdo);
3336 break;
3337 default:
3338 tcpm_log(port, "Invalid PDO selected!");
3339 return -EINVAL;
3340 }
3341
3342 /* Select maximum available current within the sink pdo's limit */
3343 if (type == PDO_TYPE_BATT) {
3344 mw = min_power(pdo, matching_snk_pdo);
3345 ma = 1000 * mw / mv;
3346 } else {
3347 ma = min_current(pdo, matching_snk_pdo);
3348 mw = ma * mv / 1000;
3349 }
3350
3351 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
3352
3353 /* Set mismatch bit if offered power is less than operating power */
3354 max_ma = ma;
3355 max_mw = mw;
3356 if (mw < port->operating_snk_mw) {
3357 flags |= RDO_CAP_MISMATCH;
3358 if (type == PDO_TYPE_BATT &&
3359 (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)))
3360 max_mw = pdo_max_power(matching_snk_pdo);
3361 else if (pdo_max_current(matching_snk_pdo) >
3362 pdo_max_current(pdo))
3363 max_ma = pdo_max_current(matching_snk_pdo);
3364 }
3365
3366 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
3367 port->cc_req, port->cc1, port->cc2, port->vbus_source,
3368 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
3369 port->polarity);
3370
3371 if (type == PDO_TYPE_BATT) {
3372 *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags);
3373
3374 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
3375 src_pdo_index, mv, mw,
3376 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
3377 } else {
3378 *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags);
3379
3380 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
3381 src_pdo_index, mv, ma,
3382 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
3383 }
3384
3385 port->req_current_limit = ma;
3386 port->req_supply_voltage = mv;
3387
3388 return 0;
3389 }
3390
tcpm_pd_send_request(struct tcpm_port * port)3391 static int tcpm_pd_send_request(struct tcpm_port *port)
3392 {
3393 struct pd_message msg;
3394 int ret;
3395 u32 rdo;
3396
3397 ret = tcpm_pd_build_request(port, &rdo);
3398 if (ret < 0)
3399 return ret;
3400
3401 /*
3402 * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition.
3403 * It is safer to modify the threshold here.
3404 */
3405 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
3406
3407 memset(&msg, 0, sizeof(msg));
3408 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
3409 port->pwr_role,
3410 port->data_role,
3411 port->negotiated_rev,
3412 port->message_id, 1);
3413 msg.payload[0] = cpu_to_le32(rdo);
3414
3415 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
3416 }
3417
tcpm_pd_build_pps_request(struct tcpm_port * port,u32 * rdo)3418 static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo)
3419 {
3420 unsigned int out_mv, op_ma, op_mw, max_mv, max_ma, flags;
3421 unsigned int src_pdo_index;
3422
3423 src_pdo_index = tcpm_pd_select_pps_apdo(port);
3424 if (!src_pdo_index)
3425 return -EOPNOTSUPP;
3426
3427 max_mv = port->pps_data.req_max_volt;
3428 max_ma = port->pps_data.req_max_curr;
3429 out_mv = port->pps_data.req_out_volt;
3430 op_ma = port->pps_data.req_op_curr;
3431
3432 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
3433
3434 op_mw = (op_ma * out_mv) / 1000;
3435 if (op_mw < port->operating_snk_mw) {
3436 /*
3437 * Try raising current to meet power needs. If that's not enough
3438 * then try upping the voltage. If that's still not enough
3439 * then we've obviously chosen a PPS APDO which really isn't
3440 * suitable so abandon ship.
3441 */
3442 op_ma = (port->operating_snk_mw * 1000) / out_mv;
3443 if ((port->operating_snk_mw * 1000) % out_mv)
3444 ++op_ma;
3445 op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP);
3446
3447 if (op_ma > max_ma) {
3448 op_ma = max_ma;
3449 out_mv = (port->operating_snk_mw * 1000) / op_ma;
3450 if ((port->operating_snk_mw * 1000) % op_ma)
3451 ++out_mv;
3452 out_mv += RDO_PROG_VOLT_MV_STEP -
3453 (out_mv % RDO_PROG_VOLT_MV_STEP);
3454
3455 if (out_mv > max_mv) {
3456 tcpm_log(port, "Invalid PPS APDO selected!");
3457 return -EINVAL;
3458 }
3459 }
3460 }
3461
3462 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
3463 port->cc_req, port->cc1, port->cc2, port->vbus_source,
3464 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
3465 port->polarity);
3466
3467 *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags);
3468
3469 tcpm_log(port, "Requesting APDO %d: %u mV, %u mA",
3470 src_pdo_index, out_mv, op_ma);
3471
3472 port->pps_data.req_op_curr = op_ma;
3473 port->pps_data.req_out_volt = out_mv;
3474
3475 return 0;
3476 }
3477
tcpm_pd_send_pps_request(struct tcpm_port * port)3478 static int tcpm_pd_send_pps_request(struct tcpm_port *port)
3479 {
3480 struct pd_message msg;
3481 int ret;
3482 u32 rdo;
3483
3484 ret = tcpm_pd_build_pps_request(port, &rdo);
3485 if (ret < 0)
3486 return ret;
3487
3488 /* Relax the threshold as voltage will be adjusted right after Accept Message. */
3489 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
3490
3491 memset(&msg, 0, sizeof(msg));
3492 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
3493 port->pwr_role,
3494 port->data_role,
3495 port->negotiated_rev,
3496 port->message_id, 1);
3497 msg.payload[0] = cpu_to_le32(rdo);
3498
3499 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
3500 }
3501
tcpm_set_vbus(struct tcpm_port * port,bool enable)3502 static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
3503 {
3504 int ret;
3505
3506 if (enable && port->vbus_charge)
3507 return -EINVAL;
3508
3509 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
3510
3511 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
3512 if (ret < 0)
3513 return ret;
3514
3515 port->vbus_source = enable;
3516 return 0;
3517 }
3518
tcpm_set_charge(struct tcpm_port * port,bool charge)3519 static int tcpm_set_charge(struct tcpm_port *port, bool charge)
3520 {
3521 int ret;
3522
3523 if (charge && port->vbus_source)
3524 return -EINVAL;
3525
3526 if (charge != port->vbus_charge) {
3527 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
3528 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
3529 charge);
3530 if (ret < 0)
3531 return ret;
3532 }
3533 port->vbus_charge = charge;
3534 power_supply_changed(port->psy);
3535 return 0;
3536 }
3537
tcpm_start_toggling(struct tcpm_port * port,enum typec_cc_status cc)3538 static bool tcpm_start_toggling(struct tcpm_port *port, enum typec_cc_status cc)
3539 {
3540 int ret;
3541
3542 if (!port->tcpc->start_toggling)
3543 return false;
3544
3545 tcpm_log_force(port, "Start toggling");
3546 ret = port->tcpc->start_toggling(port->tcpc, port->port_type, cc);
3547 return ret == 0;
3548 }
3549
tcpm_init_vbus(struct tcpm_port * port)3550 static int tcpm_init_vbus(struct tcpm_port *port)
3551 {
3552 int ret;
3553
3554 ret = port->tcpc->set_vbus(port->tcpc, false, false);
3555 port->vbus_source = false;
3556 port->vbus_charge = false;
3557 return ret;
3558 }
3559
tcpm_init_vconn(struct tcpm_port * port)3560 static int tcpm_init_vconn(struct tcpm_port *port)
3561 {
3562 int ret;
3563
3564 ret = port->tcpc->set_vconn(port->tcpc, false);
3565 port->vconn_role = TYPEC_SINK;
3566 return ret;
3567 }
3568
tcpm_typec_connect(struct tcpm_port * port)3569 static void tcpm_typec_connect(struct tcpm_port *port)
3570 {
3571 if (!port->connected) {
3572 /* Make sure we don't report stale identity information */
3573 memset(&port->partner_ident, 0, sizeof(port->partner_ident));
3574 port->partner_desc.usb_pd = port->pd_capable;
3575 if (tcpm_port_is_debug(port))
3576 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
3577 else if (tcpm_port_is_audio(port))
3578 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
3579 else
3580 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
3581 port->partner = typec_register_partner(port->typec_port,
3582 &port->partner_desc);
3583 port->connected = true;
3584 typec_partner_set_usb_power_delivery(port->partner, port->partner_pd);
3585 }
3586 }
3587
tcpm_src_attach(struct tcpm_port * port)3588 static int tcpm_src_attach(struct tcpm_port *port)
3589 {
3590 enum typec_cc_polarity polarity =
3591 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
3592 : TYPEC_POLARITY_CC1;
3593 int ret;
3594
3595 if (port->attached)
3596 return 0;
3597
3598 ret = tcpm_set_polarity(port, polarity);
3599 if (ret < 0)
3600 return ret;
3601
3602 tcpm_enable_auto_vbus_discharge(port, true);
3603
3604 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, tcpm_data_role_for_source(port));
3605 if (ret < 0)
3606 return ret;
3607
3608 if (port->pd_supported) {
3609 ret = port->tcpc->set_pd_rx(port->tcpc, true);
3610 if (ret < 0)
3611 goto out_disable_mux;
3612 }
3613
3614 /*
3615 * USB Type-C specification, version 1.2,
3616 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
3617 * Enable VCONN only if the non-RD port is set to RA.
3618 */
3619 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
3620 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
3621 ret = tcpm_set_vconn(port, true);
3622 if (ret < 0)
3623 goto out_disable_pd;
3624 }
3625
3626 ret = tcpm_set_vbus(port, true);
3627 if (ret < 0)
3628 goto out_disable_vconn;
3629
3630 port->pd_capable = false;
3631
3632 port->partner = NULL;
3633
3634 port->attached = true;
3635 port->send_discover = true;
3636
3637 return 0;
3638
3639 out_disable_vconn:
3640 tcpm_set_vconn(port, false);
3641 out_disable_pd:
3642 if (port->pd_supported)
3643 port->tcpc->set_pd_rx(port->tcpc, false);
3644 out_disable_mux:
3645 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
3646 TYPEC_ORIENTATION_NONE);
3647 return ret;
3648 }
3649
tcpm_typec_disconnect(struct tcpm_port * port)3650 static void tcpm_typec_disconnect(struct tcpm_port *port)
3651 {
3652 if (port->connected) {
3653 typec_partner_set_usb_power_delivery(port->partner, NULL);
3654 typec_unregister_partner(port->partner);
3655 port->partner = NULL;
3656 port->connected = false;
3657 }
3658 }
3659
tcpm_unregister_altmodes(struct tcpm_port * port)3660 static void tcpm_unregister_altmodes(struct tcpm_port *port)
3661 {
3662 struct pd_mode_data *modep = &port->mode_data;
3663 int i;
3664
3665 for (i = 0; i < modep->altmodes; i++) {
3666 typec_unregister_altmode(port->partner_altmode[i]);
3667 port->partner_altmode[i] = NULL;
3668 }
3669
3670 memset(modep, 0, sizeof(*modep));
3671 }
3672
tcpm_set_partner_usb_comm_capable(struct tcpm_port * port,bool capable)3673 static void tcpm_set_partner_usb_comm_capable(struct tcpm_port *port, bool capable)
3674 {
3675 tcpm_log(port, "Setting usb_comm capable %s", capable ? "true" : "false");
3676
3677 if (port->tcpc->set_partner_usb_comm_capable)
3678 port->tcpc->set_partner_usb_comm_capable(port->tcpc, capable);
3679 }
3680
tcpm_reset_port(struct tcpm_port * port)3681 static void tcpm_reset_port(struct tcpm_port *port)
3682 {
3683 tcpm_enable_auto_vbus_discharge(port, false);
3684 port->in_ams = false;
3685 port->ams = NONE_AMS;
3686 port->vdm_sm_running = false;
3687 tcpm_unregister_altmodes(port);
3688 tcpm_typec_disconnect(port);
3689 port->attached = false;
3690 port->pd_capable = false;
3691 port->pps_data.supported = false;
3692 tcpm_set_partner_usb_comm_capable(port, false);
3693
3694 /*
3695 * First Rx ID should be 0; set this to a sentinel of -1 so that
3696 * we can check tcpm_pd_rx_handler() if we had seen it before.
3697 */
3698 port->rx_msgid = -1;
3699
3700 port->tcpc->set_pd_rx(port->tcpc, false);
3701 tcpm_init_vbus(port); /* also disables charging */
3702 tcpm_init_vconn(port);
3703 tcpm_set_current_limit(port, 0, 0);
3704 tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
3705 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
3706 TYPEC_ORIENTATION_NONE);
3707 tcpm_set_attached_state(port, false);
3708 port->try_src_count = 0;
3709 port->try_snk_count = 0;
3710 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
3711 power_supply_changed(port->psy);
3712 port->nr_sink_caps = 0;
3713 port->sink_cap_done = false;
3714 if (port->tcpc->enable_frs)
3715 port->tcpc->enable_frs(port->tcpc, false);
3716
3717 usb_power_delivery_unregister_capabilities(port->partner_sink_caps);
3718 port->partner_sink_caps = NULL;
3719 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
3720 port->partner_source_caps = NULL;
3721 usb_power_delivery_unregister(port->partner_pd);
3722 port->partner_pd = NULL;
3723 }
3724
tcpm_detach(struct tcpm_port * port)3725 static void tcpm_detach(struct tcpm_port *port)
3726 {
3727 if (tcpm_port_is_disconnected(port))
3728 port->hard_reset_count = 0;
3729
3730 port->try_src_count = 0;
3731 port->try_snk_count = 0;
3732
3733 if (!port->attached)
3734 return;
3735
3736 if (port->tcpc->set_bist_data) {
3737 tcpm_log(port, "disable BIST MODE TESTDATA");
3738 port->tcpc->set_bist_data(port->tcpc, false);
3739 }
3740
3741 tcpm_reset_port(port);
3742 }
3743
tcpm_src_detach(struct tcpm_port * port)3744 static void tcpm_src_detach(struct tcpm_port *port)
3745 {
3746 tcpm_detach(port);
3747 }
3748
tcpm_snk_attach(struct tcpm_port * port)3749 static int tcpm_snk_attach(struct tcpm_port *port)
3750 {
3751 int ret;
3752
3753 if (port->attached)
3754 return 0;
3755
3756 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
3757 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
3758 if (ret < 0)
3759 return ret;
3760
3761 tcpm_enable_auto_vbus_discharge(port, true);
3762
3763 ret = tcpm_set_roles(port, true, TYPEC_SINK, tcpm_data_role_for_sink(port));
3764 if (ret < 0)
3765 return ret;
3766
3767 port->pd_capable = false;
3768
3769 port->partner = NULL;
3770
3771 port->attached = true;
3772 port->send_discover = true;
3773
3774 return 0;
3775 }
3776
tcpm_snk_detach(struct tcpm_port * port)3777 static void tcpm_snk_detach(struct tcpm_port *port)
3778 {
3779 tcpm_detach(port);
3780 }
3781
tcpm_acc_attach(struct tcpm_port * port)3782 static int tcpm_acc_attach(struct tcpm_port *port)
3783 {
3784 int ret;
3785
3786 if (port->attached)
3787 return 0;
3788
3789 ret = tcpm_set_roles(port, true, TYPEC_SOURCE,
3790 tcpm_data_role_for_source(port));
3791 if (ret < 0)
3792 return ret;
3793
3794 port->partner = NULL;
3795
3796 tcpm_typec_connect(port);
3797
3798 port->attached = true;
3799
3800 return 0;
3801 }
3802
tcpm_acc_detach(struct tcpm_port * port)3803 static void tcpm_acc_detach(struct tcpm_port *port)
3804 {
3805 tcpm_detach(port);
3806 }
3807
hard_reset_state(struct tcpm_port * port)3808 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
3809 {
3810 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
3811 return HARD_RESET_SEND;
3812 if (port->pd_capable)
3813 return ERROR_RECOVERY;
3814 if (port->pwr_role == TYPEC_SOURCE)
3815 return SRC_UNATTACHED;
3816 if (port->state == SNK_WAIT_CAPABILITIES)
3817 return SNK_READY;
3818 return SNK_UNATTACHED;
3819 }
3820
unattached_state(struct tcpm_port * port)3821 static inline enum tcpm_state unattached_state(struct tcpm_port *port)
3822 {
3823 if (port->port_type == TYPEC_PORT_DRP) {
3824 if (port->pwr_role == TYPEC_SOURCE)
3825 return SRC_UNATTACHED;
3826 else
3827 return SNK_UNATTACHED;
3828 } else if (port->port_type == TYPEC_PORT_SRC) {
3829 return SRC_UNATTACHED;
3830 }
3831
3832 return SNK_UNATTACHED;
3833 }
3834
tcpm_swap_complete(struct tcpm_port * port,int result)3835 static void tcpm_swap_complete(struct tcpm_port *port, int result)
3836 {
3837 if (port->swap_pending) {
3838 port->swap_status = result;
3839 port->swap_pending = false;
3840 port->non_pd_role_swap = false;
3841 complete(&port->swap_complete);
3842 }
3843 }
3844
tcpm_get_pwr_opmode(enum typec_cc_status cc)3845 static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
3846 {
3847 switch (cc) {
3848 case TYPEC_CC_RP_1_5:
3849 return TYPEC_PWR_MODE_1_5A;
3850 case TYPEC_CC_RP_3_0:
3851 return TYPEC_PWR_MODE_3_0A;
3852 case TYPEC_CC_RP_DEF:
3853 default:
3854 return TYPEC_PWR_MODE_USB;
3855 }
3856 }
3857
tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)3858 static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)
3859 {
3860 switch (opmode) {
3861 case TYPEC_PWR_MODE_USB:
3862 return TYPEC_CC_RP_DEF;
3863 case TYPEC_PWR_MODE_1_5A:
3864 return TYPEC_CC_RP_1_5;
3865 case TYPEC_PWR_MODE_3_0A:
3866 case TYPEC_PWR_MODE_PD:
3867 default:
3868 return TYPEC_CC_RP_3_0;
3869 }
3870 }
3871
tcpm_set_initial_svdm_version(struct tcpm_port * port)3872 static void tcpm_set_initial_svdm_version(struct tcpm_port *port)
3873 {
3874 switch (port->negotiated_rev) {
3875 case PD_REV30:
3876 break;
3877 /*
3878 * 6.4.4.2.3 Structured VDM Version
3879 * 2.0 states "At this time, there is only one version (1.0) defined.
3880 * This field Shall be set to zero to indicate Version 1.0."
3881 * 3.0 states "This field Shall be set to 01b to indicate Version 2.0."
3882 * To ensure that we follow the Power Delivery revision we are currently
3883 * operating on, downgrade the SVDM version to the highest one supported
3884 * by the Power Delivery revision.
3885 */
3886 case PD_REV20:
3887 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0);
3888 break;
3889 default:
3890 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0);
3891 break;
3892 }
3893 }
3894
run_state_machine(struct tcpm_port * port)3895 static void run_state_machine(struct tcpm_port *port)
3896 {
3897 int ret;
3898 enum typec_pwr_opmode opmode;
3899 unsigned int msecs;
3900 enum tcpm_state upcoming_state;
3901
3902 if (port->tcpc->check_contaminant && port->state != CHECK_CONTAMINANT)
3903 port->potential_contaminant = ((port->enter_state == SRC_ATTACH_WAIT &&
3904 port->state == SRC_UNATTACHED) ||
3905 (port->enter_state == SNK_ATTACH_WAIT &&
3906 port->state == SNK_UNATTACHED));
3907
3908 port->enter_state = port->state;
3909 switch (port->state) {
3910 case TOGGLING:
3911 break;
3912 case CHECK_CONTAMINANT:
3913 port->tcpc->check_contaminant(port->tcpc);
3914 break;
3915 /* SRC states */
3916 case SRC_UNATTACHED:
3917 if (!port->non_pd_role_swap)
3918 tcpm_swap_complete(port, -ENOTCONN);
3919 tcpm_src_detach(port);
3920 if (port->potential_contaminant) {
3921 tcpm_set_state(port, CHECK_CONTAMINANT, 0);
3922 break;
3923 }
3924 if (tcpm_start_toggling(port, tcpm_rp_cc(port))) {
3925 tcpm_set_state(port, TOGGLING, 0);
3926 break;
3927 }
3928 tcpm_set_cc(port, tcpm_rp_cc(port));
3929 if (port->port_type == TYPEC_PORT_DRP)
3930 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
3931 break;
3932 case SRC_ATTACH_WAIT:
3933 if (tcpm_port_is_debug(port))
3934 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
3935 PD_T_CC_DEBOUNCE);
3936 else if (tcpm_port_is_audio(port))
3937 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
3938 PD_T_CC_DEBOUNCE);
3939 else if (tcpm_port_is_source(port) && port->vbus_vsafe0v)
3940 tcpm_set_state(port,
3941 tcpm_try_snk(port) ? SNK_TRY
3942 : SRC_ATTACHED,
3943 PD_T_CC_DEBOUNCE);
3944 break;
3945
3946 case SNK_TRY:
3947 port->try_snk_count++;
3948 /*
3949 * Requirements:
3950 * - Do not drive vconn or vbus
3951 * - Terminate CC pins (both) to Rd
3952 * Action:
3953 * - Wait for tDRPTry (PD_T_DRP_TRY).
3954 * Until then, ignore any state changes.
3955 */
3956 tcpm_set_cc(port, TYPEC_CC_RD);
3957 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
3958 break;
3959 case SNK_TRY_WAIT:
3960 if (tcpm_port_is_sink(port)) {
3961 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0);
3962 } else {
3963 tcpm_set_state(port, SRC_TRYWAIT, 0);
3964 port->max_wait = 0;
3965 }
3966 break;
3967 case SNK_TRY_WAIT_DEBOUNCE:
3968 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS,
3969 PD_T_TRY_CC_DEBOUNCE);
3970 break;
3971 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
3972 if (port->vbus_present && tcpm_port_is_sink(port))
3973 tcpm_set_state(port, SNK_ATTACHED, 0);
3974 else
3975 port->max_wait = 0;
3976 break;
3977 case SRC_TRYWAIT:
3978 tcpm_set_cc(port, tcpm_rp_cc(port));
3979 if (port->max_wait == 0) {
3980 port->max_wait = jiffies +
3981 msecs_to_jiffies(PD_T_DRP_TRY);
3982 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
3983 PD_T_DRP_TRY);
3984 } else {
3985 if (time_is_after_jiffies(port->max_wait))
3986 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
3987 jiffies_to_msecs(port->max_wait -
3988 jiffies));
3989 else
3990 tcpm_set_state(port, SNK_UNATTACHED, 0);
3991 }
3992 break;
3993 case SRC_TRYWAIT_DEBOUNCE:
3994 tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
3995 break;
3996 case SRC_TRYWAIT_UNATTACHED:
3997 tcpm_set_state(port, SNK_UNATTACHED, 0);
3998 break;
3999
4000 case SRC_ATTACHED:
4001 ret = tcpm_src_attach(port);
4002 tcpm_set_state(port, SRC_UNATTACHED,
4003 ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
4004 break;
4005 case SRC_STARTUP:
4006 opmode = tcpm_get_pwr_opmode(tcpm_rp_cc(port));
4007 typec_set_pwr_opmode(port->typec_port, opmode);
4008 port->pwr_opmode = TYPEC_PWR_MODE_USB;
4009 port->caps_count = 0;
4010 port->negotiated_rev = PD_MAX_REV;
4011 port->message_id = 0;
4012 port->rx_msgid = -1;
4013 port->explicit_contract = false;
4014 /* SNK -> SRC POWER/FAST_ROLE_SWAP finished */
4015 if (port->ams == POWER_ROLE_SWAP ||
4016 port->ams == FAST_ROLE_SWAP)
4017 tcpm_ams_finish(port);
4018 if (!port->pd_supported) {
4019 tcpm_set_state(port, SRC_READY, 0);
4020 break;
4021 }
4022 port->upcoming_state = SRC_SEND_CAPABILITIES;
4023 tcpm_ams_start(port, POWER_NEGOTIATION);
4024 break;
4025 case SRC_SEND_CAPABILITIES:
4026 port->caps_count++;
4027 if (port->caps_count > PD_N_CAPS_COUNT) {
4028 tcpm_set_state(port, SRC_READY, 0);
4029 break;
4030 }
4031 ret = tcpm_pd_send_source_caps(port);
4032 if (ret < 0) {
4033 tcpm_set_state(port, SRC_SEND_CAPABILITIES,
4034 PD_T_SEND_SOURCE_CAP);
4035 } else {
4036 /*
4037 * Per standard, we should clear the reset counter here.
4038 * However, that can result in state machine hang-ups.
4039 * Reset it only in READY state to improve stability.
4040 */
4041 /* port->hard_reset_count = 0; */
4042 port->caps_count = 0;
4043 port->pd_capable = true;
4044 tcpm_set_state_cond(port, SRC_SEND_CAPABILITIES_TIMEOUT,
4045 PD_T_SEND_SOURCE_CAP);
4046 }
4047 break;
4048 case SRC_SEND_CAPABILITIES_TIMEOUT:
4049 /*
4050 * Error recovery for a PD_DATA_SOURCE_CAP reply timeout.
4051 *
4052 * PD 2.0 sinks are supposed to accept src-capabilities with a
4053 * 3.0 header and simply ignore any src PDOs which the sink does
4054 * not understand such as PPS but some 2.0 sinks instead ignore
4055 * the entire PD_DATA_SOURCE_CAP message, causing contract
4056 * negotiation to fail.
4057 *
4058 * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try
4059 * sending src-capabilities with a lower PD revision to
4060 * make these broken sinks work.
4061 */
4062 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) {
4063 tcpm_set_state(port, HARD_RESET_SEND, 0);
4064 } else if (port->negotiated_rev > PD_REV20) {
4065 port->negotiated_rev--;
4066 port->hard_reset_count = 0;
4067 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
4068 } else {
4069 tcpm_set_state(port, hard_reset_state(port), 0);
4070 }
4071 break;
4072 case SRC_NEGOTIATE_CAPABILITIES:
4073 ret = tcpm_pd_check_request(port);
4074 if (ret < 0) {
4075 tcpm_pd_send_control(port, PD_CTRL_REJECT);
4076 if (!port->explicit_contract) {
4077 tcpm_set_state(port,
4078 SRC_WAIT_NEW_CAPABILITIES, 0);
4079 } else {
4080 tcpm_set_state(port, SRC_READY, 0);
4081 }
4082 } else {
4083 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4084 tcpm_set_partner_usb_comm_capable(port,
4085 !!(port->sink_request & RDO_USB_COMM));
4086 tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
4087 PD_T_SRC_TRANSITION);
4088 }
4089 break;
4090 case SRC_TRANSITION_SUPPLY:
4091 /* XXX: regulator_set_voltage(vbus, ...) */
4092 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
4093 port->explicit_contract = true;
4094 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
4095 port->pwr_opmode = TYPEC_PWR_MODE_PD;
4096 tcpm_set_state_cond(port, SRC_READY, 0);
4097 break;
4098 case SRC_READY:
4099 #if 1
4100 port->hard_reset_count = 0;
4101 #endif
4102 port->try_src_count = 0;
4103
4104 tcpm_swap_complete(port, 0);
4105 tcpm_typec_connect(port);
4106
4107 if (port->ams != NONE_AMS)
4108 tcpm_ams_finish(port);
4109 if (port->next_ams != NONE_AMS) {
4110 port->ams = port->next_ams;
4111 port->next_ams = NONE_AMS;
4112 }
4113
4114 /*
4115 * If previous AMS is interrupted, switch to the upcoming
4116 * state.
4117 */
4118 if (port->upcoming_state != INVALID_STATE) {
4119 upcoming_state = port->upcoming_state;
4120 port->upcoming_state = INVALID_STATE;
4121 tcpm_set_state(port, upcoming_state, 0);
4122 break;
4123 }
4124
4125 /*
4126 * 6.4.4.3.1 Discover Identity
4127 * "The Discover Identity Command Shall only be sent to SOP when there is an
4128 * Explicit Contract."
4129 * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using
4130 * port->explicit_contract to decide whether to send the command.
4131 */
4132 if (port->explicit_contract) {
4133 tcpm_set_initial_svdm_version(port);
4134 mod_send_discover_delayed_work(port, 0);
4135 } else {
4136 port->send_discover = false;
4137 }
4138
4139 /*
4140 * 6.3.5
4141 * Sending ping messages is not necessary if
4142 * - the source operates at vSafe5V
4143 * or
4144 * - The system is not operating in PD mode
4145 * or
4146 * - Both partners are connected using a Type-C connector
4147 *
4148 * There is no actual need to send PD messages since the local
4149 * port type-c and the spec does not clearly say whether PD is
4150 * possible when type-c is connected to Type-A/B
4151 */
4152 break;
4153 case SRC_WAIT_NEW_CAPABILITIES:
4154 /* Nothing to do... */
4155 break;
4156
4157 /* SNK states */
4158 case SNK_UNATTACHED:
4159 if (!port->non_pd_role_swap)
4160 tcpm_swap_complete(port, -ENOTCONN);
4161 tcpm_pps_complete(port, -ENOTCONN);
4162 tcpm_snk_detach(port);
4163 if (port->potential_contaminant) {
4164 tcpm_set_state(port, CHECK_CONTAMINANT, 0);
4165 break;
4166 }
4167 if (tcpm_start_toggling(port, TYPEC_CC_RD)) {
4168 tcpm_set_state(port, TOGGLING, 0);
4169 break;
4170 }
4171 tcpm_set_cc(port, TYPEC_CC_RD);
4172 if (port->port_type == TYPEC_PORT_DRP)
4173 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
4174 break;
4175 case SNK_ATTACH_WAIT:
4176 if ((port->cc1 == TYPEC_CC_OPEN &&
4177 port->cc2 != TYPEC_CC_OPEN) ||
4178 (port->cc1 != TYPEC_CC_OPEN &&
4179 port->cc2 == TYPEC_CC_OPEN))
4180 tcpm_set_state(port, SNK_DEBOUNCED,
4181 PD_T_CC_DEBOUNCE);
4182 else if (tcpm_port_is_disconnected(port))
4183 tcpm_set_state(port, SNK_UNATTACHED,
4184 PD_T_PD_DEBOUNCE);
4185 break;
4186 case SNK_DEBOUNCED:
4187 if (tcpm_port_is_disconnected(port))
4188 tcpm_set_state(port, SNK_UNATTACHED,
4189 PD_T_PD_DEBOUNCE);
4190 else if (port->vbus_present)
4191 tcpm_set_state(port,
4192 tcpm_try_src(port) ? SRC_TRY
4193 : SNK_ATTACHED,
4194 0);
4195 break;
4196 case SRC_TRY:
4197 port->try_src_count++;
4198 tcpm_set_cc(port, tcpm_rp_cc(port));
4199 port->max_wait = 0;
4200 tcpm_set_state(port, SRC_TRY_WAIT, 0);
4201 break;
4202 case SRC_TRY_WAIT:
4203 if (port->max_wait == 0) {
4204 port->max_wait = jiffies +
4205 msecs_to_jiffies(PD_T_DRP_TRY);
4206 msecs = PD_T_DRP_TRY;
4207 } else {
4208 if (time_is_after_jiffies(port->max_wait))
4209 msecs = jiffies_to_msecs(port->max_wait -
4210 jiffies);
4211 else
4212 msecs = 0;
4213 }
4214 tcpm_set_state(port, SNK_TRYWAIT, msecs);
4215 break;
4216 case SRC_TRY_DEBOUNCE:
4217 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
4218 break;
4219 case SNK_TRYWAIT:
4220 tcpm_set_cc(port, TYPEC_CC_RD);
4221 tcpm_set_state(port, SNK_TRYWAIT_VBUS, PD_T_CC_DEBOUNCE);
4222 break;
4223 case SNK_TRYWAIT_VBUS:
4224 /*
4225 * TCPM stays in this state indefinitely until VBUS
4226 * is detected as long as Rp is not detected for
4227 * more than a time period of tPDDebounce.
4228 */
4229 if (port->vbus_present && tcpm_port_is_sink(port)) {
4230 tcpm_set_state(port, SNK_ATTACHED, 0);
4231 break;
4232 }
4233 if (!tcpm_port_is_sink(port))
4234 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
4235 break;
4236 case SNK_TRYWAIT_DEBOUNCE:
4237 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
4238 break;
4239 case SNK_ATTACHED:
4240 ret = tcpm_snk_attach(port);
4241 if (ret < 0)
4242 tcpm_set_state(port, SNK_UNATTACHED, 0);
4243 else
4244 tcpm_set_state(port, SNK_STARTUP, 0);
4245 break;
4246 case SNK_STARTUP:
4247 opmode = tcpm_get_pwr_opmode(port->polarity ?
4248 port->cc2 : port->cc1);
4249 typec_set_pwr_opmode(port->typec_port, opmode);
4250 port->pwr_opmode = TYPEC_PWR_MODE_USB;
4251 port->negotiated_rev = PD_MAX_REV;
4252 port->message_id = 0;
4253 port->rx_msgid = -1;
4254 port->explicit_contract = false;
4255
4256 if (port->ams == POWER_ROLE_SWAP ||
4257 port->ams == FAST_ROLE_SWAP)
4258 /* SRC -> SNK POWER/FAST_ROLE_SWAP finished */
4259 tcpm_ams_finish(port);
4260
4261 tcpm_set_state(port, SNK_DISCOVERY, 0);
4262 break;
4263 case SNK_DISCOVERY:
4264 if (port->vbus_present) {
4265 u32 current_lim = tcpm_get_current_limit(port);
4266
4267 if (port->slow_charger_loop && (current_lim > PD_P_SNK_STDBY_MW / 5))
4268 current_lim = PD_P_SNK_STDBY_MW / 5;
4269 tcpm_set_current_limit(port, current_lim, 5000);
4270 /* Not sink vbus if operational current is 0mA */
4271 tcpm_set_charge(port, !!pdo_max_current(port->snk_pdo[0]));
4272
4273 if (!port->pd_supported)
4274 tcpm_set_state(port, SNK_READY, 0);
4275 else
4276 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
4277 break;
4278 }
4279 /*
4280 * For DRP, timeouts differ. Also, handling is supposed to be
4281 * different and much more complex (dead battery detection;
4282 * see USB power delivery specification, section 8.3.3.6.1.5.1).
4283 */
4284 tcpm_set_state(port, hard_reset_state(port),
4285 port->port_type == TYPEC_PORT_DRP ?
4286 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
4287 break;
4288 case SNK_DISCOVERY_DEBOUNCE:
4289 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
4290 PD_T_CC_DEBOUNCE);
4291 break;
4292 case SNK_DISCOVERY_DEBOUNCE_DONE:
4293 if (!tcpm_port_is_disconnected(port) &&
4294 tcpm_port_is_sink(port) &&
4295 ktime_after(port->delayed_runtime, ktime_get())) {
4296 tcpm_set_state(port, SNK_DISCOVERY,
4297 ktime_to_ms(ktime_sub(port->delayed_runtime, ktime_get())));
4298 break;
4299 }
4300 tcpm_set_state(port, unattached_state(port), 0);
4301 break;
4302 case SNK_WAIT_CAPABILITIES:
4303 ret = port->tcpc->set_pd_rx(port->tcpc, true);
4304 if (ret < 0) {
4305 tcpm_set_state(port, SNK_READY, 0);
4306 break;
4307 }
4308 /*
4309 * If VBUS has never been low, and we time out waiting
4310 * for source cap, try a soft reset first, in case we
4311 * were already in a stable contract before this boot.
4312 * Do this only once.
4313 */
4314 if (port->vbus_never_low) {
4315 port->vbus_never_low = false;
4316 tcpm_set_state(port, SNK_SOFT_RESET,
4317 PD_T_SINK_WAIT_CAP);
4318 } else {
4319 tcpm_set_state(port, hard_reset_state(port),
4320 PD_T_SINK_WAIT_CAP);
4321 }
4322 break;
4323 case SNK_NEGOTIATE_CAPABILITIES:
4324 port->pd_capable = true;
4325 tcpm_set_partner_usb_comm_capable(port,
4326 !!(port->source_caps[0] & PDO_FIXED_USB_COMM));
4327 port->hard_reset_count = 0;
4328 ret = tcpm_pd_send_request(port);
4329 if (ret < 0) {
4330 /* Restore back to the original state */
4331 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
4332 port->pps_data.active,
4333 port->supply_voltage);
4334 /* Let the Source send capabilities again. */
4335 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
4336 } else {
4337 tcpm_set_state_cond(port, hard_reset_state(port),
4338 PD_T_SENDER_RESPONSE);
4339 }
4340 break;
4341 case SNK_NEGOTIATE_PPS_CAPABILITIES:
4342 ret = tcpm_pd_send_pps_request(port);
4343 if (ret < 0) {
4344 /* Restore back to the original state */
4345 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
4346 port->pps_data.active,
4347 port->supply_voltage);
4348 port->pps_status = ret;
4349 /*
4350 * If this was called due to updates to sink
4351 * capabilities, and pps is no longer valid, we should
4352 * safely fall back to a standard PDO.
4353 */
4354 if (port->update_sink_caps)
4355 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
4356 else
4357 tcpm_set_state(port, SNK_READY, 0);
4358 } else {
4359 tcpm_set_state_cond(port, hard_reset_state(port),
4360 PD_T_SENDER_RESPONSE);
4361 }
4362 break;
4363 case SNK_TRANSITION_SINK:
4364 /* From the USB PD spec:
4365 * "The Sink Shall transition to Sink Standby before a positive or
4366 * negative voltage transition of VBUS. During Sink Standby
4367 * the Sink Shall reduce its power draw to pSnkStdby."
4368 *
4369 * This is not applicable to PPS though as the port can continue
4370 * to draw negotiated power without switching to standby.
4371 */
4372 if (port->supply_voltage != port->req_supply_voltage && !port->pps_data.active &&
4373 port->current_limit * port->supply_voltage / 1000 > PD_P_SNK_STDBY_MW) {
4374 u32 stdby_ma = PD_P_SNK_STDBY_MW * 1000 / port->supply_voltage;
4375
4376 tcpm_log(port, "Setting standby current %u mV @ %u mA",
4377 port->supply_voltage, stdby_ma);
4378 tcpm_set_current_limit(port, stdby_ma, port->supply_voltage);
4379 }
4380 fallthrough;
4381 case SNK_TRANSITION_SINK_VBUS:
4382 tcpm_set_state(port, hard_reset_state(port),
4383 PD_T_PS_TRANSITION);
4384 break;
4385 case SNK_READY:
4386 port->try_snk_count = 0;
4387 port->update_sink_caps = false;
4388 if (port->explicit_contract) {
4389 typec_set_pwr_opmode(port->typec_port,
4390 TYPEC_PWR_MODE_PD);
4391 port->pwr_opmode = TYPEC_PWR_MODE_PD;
4392 }
4393
4394 if (!port->pd_capable && port->slow_charger_loop)
4395 tcpm_set_current_limit(port, tcpm_get_current_limit(port), 5000);
4396 tcpm_swap_complete(port, 0);
4397 tcpm_typec_connect(port);
4398 mod_enable_frs_delayed_work(port, 0);
4399 tcpm_pps_complete(port, port->pps_status);
4400
4401 if (port->ams != NONE_AMS)
4402 tcpm_ams_finish(port);
4403 if (port->next_ams != NONE_AMS) {
4404 port->ams = port->next_ams;
4405 port->next_ams = NONE_AMS;
4406 }
4407
4408 /*
4409 * If previous AMS is interrupted, switch to the upcoming
4410 * state.
4411 */
4412 if (port->upcoming_state != INVALID_STATE) {
4413 upcoming_state = port->upcoming_state;
4414 port->upcoming_state = INVALID_STATE;
4415 tcpm_set_state(port, upcoming_state, 0);
4416 break;
4417 }
4418
4419 /*
4420 * 6.4.4.3.1 Discover Identity
4421 * "The Discover Identity Command Shall only be sent to SOP when there is an
4422 * Explicit Contract."
4423 * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using
4424 * port->explicit_contract.
4425 */
4426 if (port->explicit_contract) {
4427 tcpm_set_initial_svdm_version(port);
4428 mod_send_discover_delayed_work(port, 0);
4429 } else {
4430 port->send_discover = false;
4431 }
4432
4433 power_supply_changed(port->psy);
4434 break;
4435
4436 /* Accessory states */
4437 case ACC_UNATTACHED:
4438 tcpm_acc_detach(port);
4439 tcpm_set_state(port, SRC_UNATTACHED, 0);
4440 break;
4441 case DEBUG_ACC_ATTACHED:
4442 case AUDIO_ACC_ATTACHED:
4443 ret = tcpm_acc_attach(port);
4444 if (ret < 0)
4445 tcpm_set_state(port, ACC_UNATTACHED, 0);
4446 break;
4447 case AUDIO_ACC_DEBOUNCE:
4448 tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE);
4449 break;
4450
4451 /* Hard_Reset states */
4452 case HARD_RESET_SEND:
4453 if (port->ams != NONE_AMS)
4454 tcpm_ams_finish(port);
4455 /*
4456 * State machine will be directed to HARD_RESET_START,
4457 * thus set upcoming_state to INVALID_STATE.
4458 */
4459 port->upcoming_state = INVALID_STATE;
4460 tcpm_ams_start(port, HARD_RESET);
4461 break;
4462 case HARD_RESET_START:
4463 port->sink_cap_done = false;
4464 if (port->tcpc->enable_frs)
4465 port->tcpc->enable_frs(port->tcpc, false);
4466 port->hard_reset_count++;
4467 port->tcpc->set_pd_rx(port->tcpc, false);
4468 tcpm_unregister_altmodes(port);
4469 port->nr_sink_caps = 0;
4470 port->send_discover = true;
4471 if (port->pwr_role == TYPEC_SOURCE)
4472 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
4473 PD_T_PS_HARD_RESET);
4474 else
4475 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
4476 break;
4477 case SRC_HARD_RESET_VBUS_OFF:
4478 /*
4479 * 7.1.5 Response to Hard Resets
4480 * Hard Reset Signaling indicates a communication failure has occurred and the
4481 * Source Shall stop driving VCONN, Shall remove Rp from the VCONN pin and Shall
4482 * drive VBUS to vSafe0V as shown in Figure 7-9.
4483 */
4484 tcpm_set_vconn(port, false);
4485 tcpm_set_vbus(port, false);
4486 tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE,
4487 tcpm_data_role_for_source(port));
4488 /*
4489 * If tcpc fails to notify vbus off, TCPM will wait for PD_T_SAFE_0V +
4490 * PD_T_SRC_RECOVER before turning vbus back on.
4491 * From Table 7-12 Sequence Description for a Source Initiated Hard Reset:
4492 * 4. Policy Engine waits tPSHardReset after sending Hard Reset Signaling and then
4493 * tells the Device Policy Manager to instruct the power supply to perform a
4494 * Hard Reset. The transition to vSafe0V Shall occur within tSafe0V (t2).
4495 * 5. After tSrcRecover the Source applies power to VBUS in an attempt to
4496 * re-establish communication with the Sink and resume USB Default Operation.
4497 * The transition to vSafe5V Shall occur within tSrcTurnOn(t4).
4498 */
4499 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SAFE_0V + PD_T_SRC_RECOVER);
4500 break;
4501 case SRC_HARD_RESET_VBUS_ON:
4502 tcpm_set_vconn(port, true);
4503 tcpm_set_vbus(port, true);
4504 if (port->ams == HARD_RESET)
4505 tcpm_ams_finish(port);
4506 if (port->pd_supported)
4507 port->tcpc->set_pd_rx(port->tcpc, true);
4508 tcpm_set_attached_state(port, true);
4509 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
4510 break;
4511 case SNK_HARD_RESET_SINK_OFF:
4512 /* Do not discharge/disconnect during hard reseet */
4513 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
4514 memset(&port->pps_data, 0, sizeof(port->pps_data));
4515 tcpm_set_vconn(port, false);
4516 if (port->pd_capable)
4517 tcpm_set_charge(port, false);
4518 tcpm_set_roles(port, port->self_powered, TYPEC_SINK,
4519 tcpm_data_role_for_sink(port));
4520 /*
4521 * VBUS may or may not toggle, depending on the adapter.
4522 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
4523 * directly after timeout.
4524 */
4525 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
4526 break;
4527 case SNK_HARD_RESET_WAIT_VBUS:
4528 if (port->ams == HARD_RESET)
4529 tcpm_ams_finish(port);
4530 /* Assume we're disconnected if VBUS doesn't come back. */
4531 tcpm_set_state(port, SNK_UNATTACHED,
4532 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
4533 break;
4534 case SNK_HARD_RESET_SINK_ON:
4535 /* Note: There is no guarantee that VBUS is on in this state */
4536 /*
4537 * XXX:
4538 * The specification suggests that dual mode ports in sink
4539 * mode should transition to state PE_SRC_Transition_to_default.
4540 * See USB power delivery specification chapter 8.3.3.6.1.3.
4541 * This would mean to
4542 * - turn off VCONN, reset power supply
4543 * - request hardware reset
4544 * - turn on VCONN
4545 * - Transition to state PE_Src_Startup
4546 * SNK only ports shall transition to state Snk_Startup
4547 * (see chapter 8.3.3.3.8).
4548 * Similar, dual-mode ports in source mode should transition
4549 * to PE_SNK_Transition_to_default.
4550 */
4551 if (port->pd_capable) {
4552 tcpm_set_current_limit(port,
4553 tcpm_get_current_limit(port),
4554 5000);
4555 /* Not sink vbus if operational current is 0mA */
4556 tcpm_set_charge(port, !!pdo_max_current(port->snk_pdo[0]));
4557 }
4558 if (port->ams == HARD_RESET)
4559 tcpm_ams_finish(port);
4560 tcpm_set_attached_state(port, true);
4561 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
4562 tcpm_set_state(port, SNK_STARTUP, 0);
4563 break;
4564
4565 /* Soft_Reset states */
4566 case SOFT_RESET:
4567 port->message_id = 0;
4568 port->rx_msgid = -1;
4569 /* remove existing capabilities */
4570 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4571 port->partner_source_caps = NULL;
4572 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4573 tcpm_ams_finish(port);
4574 if (port->pwr_role == TYPEC_SOURCE) {
4575 port->upcoming_state = SRC_SEND_CAPABILITIES;
4576 tcpm_ams_start(port, POWER_NEGOTIATION);
4577 } else {
4578 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
4579 }
4580 break;
4581 case SRC_SOFT_RESET_WAIT_SNK_TX:
4582 case SNK_SOFT_RESET:
4583 if (port->ams != NONE_AMS)
4584 tcpm_ams_finish(port);
4585 port->upcoming_state = SOFT_RESET_SEND;
4586 tcpm_ams_start(port, SOFT_RESET_AMS);
4587 break;
4588 case SOFT_RESET_SEND:
4589 port->message_id = 0;
4590 port->rx_msgid = -1;
4591 /* remove existing capabilities */
4592 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4593 port->partner_source_caps = NULL;
4594 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET))
4595 tcpm_set_state_cond(port, hard_reset_state(port), 0);
4596 else
4597 tcpm_set_state_cond(port, hard_reset_state(port),
4598 PD_T_SENDER_RESPONSE);
4599 break;
4600
4601 /* DR_Swap states */
4602 case DR_SWAP_SEND:
4603 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP);
4604 if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20)
4605 port->send_discover = true;
4606 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
4607 PD_T_SENDER_RESPONSE);
4608 break;
4609 case DR_SWAP_ACCEPT:
4610 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4611 if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20)
4612 port->send_discover = true;
4613 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
4614 break;
4615 case DR_SWAP_SEND_TIMEOUT:
4616 tcpm_swap_complete(port, -ETIMEDOUT);
4617 port->send_discover = false;
4618 tcpm_ams_finish(port);
4619 tcpm_set_state(port, ready_state(port), 0);
4620 break;
4621 case DR_SWAP_CHANGE_DR:
4622 tcpm_unregister_altmodes(port);
4623 if (port->data_role == TYPEC_HOST)
4624 tcpm_set_roles(port, true, port->pwr_role,
4625 TYPEC_DEVICE);
4626 else
4627 tcpm_set_roles(port, true, port->pwr_role,
4628 TYPEC_HOST);
4629 tcpm_ams_finish(port);
4630 tcpm_set_state(port, ready_state(port), 0);
4631 break;
4632
4633 case FR_SWAP_SEND:
4634 if (tcpm_pd_send_control(port, PD_CTRL_FR_SWAP)) {
4635 tcpm_set_state(port, ERROR_RECOVERY, 0);
4636 break;
4637 }
4638 tcpm_set_state_cond(port, FR_SWAP_SEND_TIMEOUT, PD_T_SENDER_RESPONSE);
4639 break;
4640 case FR_SWAP_SEND_TIMEOUT:
4641 tcpm_set_state(port, ERROR_RECOVERY, 0);
4642 break;
4643 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
4644 tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_OFF);
4645 break;
4646 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
4647 if (port->vbus_source)
4648 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
4649 else
4650 tcpm_set_state(port, ERROR_RECOVERY, PD_T_RECEIVER_RESPONSE);
4651 break;
4652 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
4653 tcpm_set_pwr_role(port, TYPEC_SOURCE);
4654 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
4655 tcpm_set_state(port, ERROR_RECOVERY, 0);
4656 break;
4657 }
4658 tcpm_set_cc(port, tcpm_rp_cc(port));
4659 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START);
4660 break;
4661
4662 /* PR_Swap states */
4663 case PR_SWAP_ACCEPT:
4664 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4665 tcpm_set_state(port, PR_SWAP_START, 0);
4666 break;
4667 case PR_SWAP_SEND:
4668 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP);
4669 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
4670 PD_T_SENDER_RESPONSE);
4671 break;
4672 case PR_SWAP_SEND_TIMEOUT:
4673 tcpm_swap_complete(port, -ETIMEDOUT);
4674 tcpm_set_state(port, ready_state(port), 0);
4675 break;
4676 case PR_SWAP_START:
4677 tcpm_apply_rc(port);
4678 if (port->pwr_role == TYPEC_SOURCE)
4679 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
4680 PD_T_SRC_TRANSITION);
4681 else
4682 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
4683 break;
4684 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
4685 /*
4686 * Prevent vbus discharge circuit from turning on during PR_SWAP
4687 * as this is not a disconnect.
4688 */
4689 tcpm_set_vbus(port, false);
4690 port->explicit_contract = false;
4691 /* allow time for Vbus discharge, must be < tSrcSwapStdby */
4692 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
4693 PD_T_SRCSWAPSTDBY);
4694 break;
4695 case PR_SWAP_SRC_SNK_SOURCE_OFF:
4696 tcpm_set_cc(port, TYPEC_CC_RD);
4697 /* allow CC debounce */
4698 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED,
4699 PD_T_CC_DEBOUNCE);
4700 break;
4701 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
4702 /*
4703 * USB-PD standard, 6.2.1.4, Port Power Role:
4704 * "During the Power Role Swap Sequence, for the initial Source
4705 * Port, the Port Power Role field shall be set to Sink in the
4706 * PS_RDY Message indicating that the initial Source’s power
4707 * supply is turned off"
4708 */
4709 tcpm_set_pwr_role(port, TYPEC_SINK);
4710 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
4711 tcpm_set_state(port, ERROR_RECOVERY, 0);
4712 break;
4713 }
4714 tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_ON_PRS);
4715 break;
4716 case PR_SWAP_SRC_SNK_SINK_ON:
4717 tcpm_enable_auto_vbus_discharge(port, true);
4718 /* Set the vbus disconnect threshold for implicit contract */
4719 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
4720 tcpm_set_state(port, SNK_STARTUP, 0);
4721 break;
4722 case PR_SWAP_SNK_SRC_SINK_OFF:
4723 /* will be source, remove existing capabilities */
4724 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4725 port->partner_source_caps = NULL;
4726 /*
4727 * Prevent vbus discharge circuit from turning on during PR_SWAP
4728 * as this is not a disconnect.
4729 */
4730 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB,
4731 port->pps_data.active, 0);
4732 tcpm_set_charge(port, false);
4733 tcpm_set_state(port, hard_reset_state(port),
4734 PD_T_PS_SOURCE_OFF);
4735 break;
4736 case PR_SWAP_SNK_SRC_SOURCE_ON:
4737 tcpm_enable_auto_vbus_discharge(port, true);
4738 tcpm_set_cc(port, tcpm_rp_cc(port));
4739 tcpm_set_vbus(port, true);
4740 /*
4741 * allow time VBUS ramp-up, must be < tNewSrc
4742 * Also, this window overlaps with CC debounce as well.
4743 * So, Wait for the max of two which is PD_T_NEWSRC
4744 */
4745 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP,
4746 PD_T_NEWSRC);
4747 break;
4748 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP:
4749 /*
4750 * USB PD standard, 6.2.1.4:
4751 * "Subsequent Messages initiated by the Policy Engine,
4752 * such as the PS_RDY Message sent to indicate that Vbus
4753 * is ready, will have the Port Power Role field set to
4754 * Source."
4755 */
4756 tcpm_set_pwr_role(port, TYPEC_SOURCE);
4757 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
4758 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START);
4759 break;
4760
4761 case VCONN_SWAP_ACCEPT:
4762 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4763 tcpm_ams_finish(port);
4764 tcpm_set_state(port, VCONN_SWAP_START, 0);
4765 break;
4766 case VCONN_SWAP_SEND:
4767 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP);
4768 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
4769 PD_T_SENDER_RESPONSE);
4770 break;
4771 case VCONN_SWAP_SEND_TIMEOUT:
4772 tcpm_swap_complete(port, -ETIMEDOUT);
4773 tcpm_set_state(port, ready_state(port), 0);
4774 break;
4775 case VCONN_SWAP_START:
4776 if (port->vconn_role == TYPEC_SOURCE)
4777 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
4778 else
4779 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
4780 break;
4781 case VCONN_SWAP_WAIT_FOR_VCONN:
4782 tcpm_set_state(port, hard_reset_state(port),
4783 PD_T_VCONN_SOURCE_ON);
4784 break;
4785 case VCONN_SWAP_TURN_ON_VCONN:
4786 tcpm_set_vconn(port, true);
4787 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
4788 tcpm_set_state(port, ready_state(port), 0);
4789 break;
4790 case VCONN_SWAP_TURN_OFF_VCONN:
4791 tcpm_set_vconn(port, false);
4792 tcpm_set_state(port, ready_state(port), 0);
4793 break;
4794
4795 case DR_SWAP_CANCEL:
4796 case PR_SWAP_CANCEL:
4797 case VCONN_SWAP_CANCEL:
4798 tcpm_swap_complete(port, port->swap_status);
4799 if (port->pwr_role == TYPEC_SOURCE)
4800 tcpm_set_state(port, SRC_READY, 0);
4801 else
4802 tcpm_set_state(port, SNK_READY, 0);
4803 break;
4804 case FR_SWAP_CANCEL:
4805 if (port->pwr_role == TYPEC_SOURCE)
4806 tcpm_set_state(port, SRC_READY, 0);
4807 else
4808 tcpm_set_state(port, SNK_READY, 0);
4809 break;
4810
4811 case BIST_RX:
4812 switch (BDO_MODE_MASK(port->bist_request)) {
4813 case BDO_MODE_CARRIER2:
4814 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
4815 tcpm_set_state(port, unattached_state(port),
4816 PD_T_BIST_CONT_MODE);
4817 break;
4818 case BDO_MODE_TESTDATA:
4819 if (port->tcpc->set_bist_data) {
4820 tcpm_log(port, "Enable BIST MODE TESTDATA");
4821 port->tcpc->set_bist_data(port->tcpc, true);
4822 }
4823 break;
4824 default:
4825 break;
4826 }
4827 break;
4828 case GET_STATUS_SEND:
4829 tcpm_pd_send_control(port, PD_CTRL_GET_STATUS);
4830 tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT,
4831 PD_T_SENDER_RESPONSE);
4832 break;
4833 case GET_STATUS_SEND_TIMEOUT:
4834 tcpm_set_state(port, ready_state(port), 0);
4835 break;
4836 case GET_PPS_STATUS_SEND:
4837 tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS);
4838 tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT,
4839 PD_T_SENDER_RESPONSE);
4840 break;
4841 case GET_PPS_STATUS_SEND_TIMEOUT:
4842 tcpm_set_state(port, ready_state(port), 0);
4843 break;
4844 case GET_SINK_CAP:
4845 tcpm_pd_send_control(port, PD_CTRL_GET_SINK_CAP);
4846 tcpm_set_state(port, GET_SINK_CAP_TIMEOUT, PD_T_SENDER_RESPONSE);
4847 break;
4848 case GET_SINK_CAP_TIMEOUT:
4849 port->sink_cap_done = true;
4850 tcpm_set_state(port, ready_state(port), 0);
4851 break;
4852 case ERROR_RECOVERY:
4853 tcpm_swap_complete(port, -EPROTO);
4854 tcpm_pps_complete(port, -EPROTO);
4855 tcpm_set_state(port, PORT_RESET, 0);
4856 break;
4857 case PORT_RESET:
4858 tcpm_reset_port(port);
4859 tcpm_set_cc(port, tcpm_default_state(port) == SNK_UNATTACHED ?
4860 TYPEC_CC_RD : tcpm_rp_cc(port));
4861 tcpm_set_state(port, PORT_RESET_WAIT_OFF,
4862 PD_T_ERROR_RECOVERY);
4863 break;
4864 case PORT_RESET_WAIT_OFF:
4865 tcpm_set_state(port,
4866 tcpm_default_state(port),
4867 port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
4868 break;
4869
4870 /* AMS intermediate state */
4871 case AMS_START:
4872 if (port->upcoming_state == INVALID_STATE) {
4873 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
4874 SRC_READY : SNK_READY, 0);
4875 break;
4876 }
4877
4878 upcoming_state = port->upcoming_state;
4879 port->upcoming_state = INVALID_STATE;
4880 tcpm_set_state(port, upcoming_state, 0);
4881 break;
4882
4883 /* Chunk state */
4884 case CHUNK_NOT_SUPP:
4885 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP);
4886 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? SRC_READY : SNK_READY, 0);
4887 break;
4888 default:
4889 WARN(1, "Unexpected port state %d\n", port->state);
4890 break;
4891 }
4892 }
4893
tcpm_state_machine_work(struct kthread_work * work)4894 static void tcpm_state_machine_work(struct kthread_work *work)
4895 {
4896 struct tcpm_port *port = container_of(work, struct tcpm_port, state_machine);
4897 enum tcpm_state prev_state;
4898
4899 mutex_lock(&port->lock);
4900 port->state_machine_running = true;
4901
4902 if (port->queued_message && tcpm_send_queued_message(port))
4903 goto done;
4904
4905 /* If we were queued due to a delayed state change, update it now */
4906 if (port->delayed_state) {
4907 tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
4908 tcpm_states[port->state],
4909 tcpm_states[port->delayed_state], port->delay_ms);
4910 port->prev_state = port->state;
4911 port->state = port->delayed_state;
4912 port->delayed_state = INVALID_STATE;
4913 }
4914
4915 /*
4916 * Continue running as long as we have (non-delayed) state changes
4917 * to make.
4918 */
4919 do {
4920 prev_state = port->state;
4921 run_state_machine(port);
4922 if (port->queued_message)
4923 tcpm_send_queued_message(port);
4924 } while (port->state != prev_state && !port->delayed_state);
4925
4926 done:
4927 port->state_machine_running = false;
4928 mutex_unlock(&port->lock);
4929 }
4930
_tcpm_cc_change(struct tcpm_port * port,enum typec_cc_status cc1,enum typec_cc_status cc2)4931 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
4932 enum typec_cc_status cc2)
4933 {
4934 enum typec_cc_status old_cc1, old_cc2;
4935 enum tcpm_state new_state;
4936
4937 old_cc1 = port->cc1;
4938 old_cc2 = port->cc2;
4939 port->cc1 = cc1;
4940 port->cc2 = cc2;
4941
4942 tcpm_log_force(port,
4943 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
4944 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
4945 port->polarity,
4946 tcpm_port_is_disconnected(port) ? "disconnected"
4947 : "connected");
4948
4949 switch (port->state) {
4950 case TOGGLING:
4951 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
4952 tcpm_port_is_source(port))
4953 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
4954 else if (tcpm_port_is_sink(port))
4955 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
4956 break;
4957 case CHECK_CONTAMINANT:
4958 /* Wait for Toggling to be resumed */
4959 break;
4960 case SRC_UNATTACHED:
4961 case ACC_UNATTACHED:
4962 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
4963 tcpm_port_is_source(port))
4964 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
4965 break;
4966 case SRC_ATTACH_WAIT:
4967 if (tcpm_port_is_disconnected(port) ||
4968 tcpm_port_is_audio_detached(port))
4969 tcpm_set_state(port, SRC_UNATTACHED, 0);
4970 else if (cc1 != old_cc1 || cc2 != old_cc2)
4971 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
4972 break;
4973 case SRC_ATTACHED:
4974 case SRC_STARTUP:
4975 case SRC_SEND_CAPABILITIES:
4976 case SRC_READY:
4977 if (tcpm_port_is_disconnected(port) ||
4978 !tcpm_port_is_source(port)) {
4979 if (port->port_type == TYPEC_PORT_SRC)
4980 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port));
4981 else
4982 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
4983 }
4984 break;
4985 case SNK_UNATTACHED:
4986 if (tcpm_port_is_sink(port))
4987 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
4988 break;
4989 case SNK_ATTACH_WAIT:
4990 if ((port->cc1 == TYPEC_CC_OPEN &&
4991 port->cc2 != TYPEC_CC_OPEN) ||
4992 (port->cc1 != TYPEC_CC_OPEN &&
4993 port->cc2 == TYPEC_CC_OPEN))
4994 new_state = SNK_DEBOUNCED;
4995 else if (tcpm_port_is_disconnected(port))
4996 new_state = SNK_UNATTACHED;
4997 else
4998 break;
4999 if (new_state != port->delayed_state)
5000 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
5001 break;
5002 case SNK_DEBOUNCED:
5003 if (tcpm_port_is_disconnected(port))
5004 new_state = SNK_UNATTACHED;
5005 else if (port->vbus_present)
5006 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
5007 else
5008 new_state = SNK_UNATTACHED;
5009 if (new_state != port->delayed_state)
5010 tcpm_set_state(port, SNK_DEBOUNCED, 0);
5011 break;
5012 case SNK_READY:
5013 /*
5014 * EXIT condition is based primarily on vbus disconnect and CC is secondary.
5015 * "A port that has entered into USB PD communications with the Source and
5016 * has seen the CC voltage exceed vRd-USB may monitor the CC pin to detect
5017 * cable disconnect in addition to monitoring VBUS.
5018 *
5019 * A port that is monitoring the CC voltage for disconnect (but is not in
5020 * the process of a USB PD PR_Swap or USB PD FR_Swap) shall transition to
5021 * Unattached.SNK within tSinkDisconnect after the CC voltage remains below
5022 * vRd-USB for tPDDebounce."
5023 *
5024 * When set_auto_vbus_discharge_threshold is enabled, CC pins go
5025 * away before vbus decays to disconnect threshold. Allow
5026 * disconnect to be driven by vbus disconnect when auto vbus
5027 * discharge is enabled.
5028 */
5029 if (!port->auto_vbus_discharge_enabled && tcpm_port_is_disconnected(port))
5030 tcpm_set_state(port, unattached_state(port), 0);
5031 else if (!port->pd_capable &&
5032 (cc1 != old_cc1 || cc2 != old_cc2))
5033 tcpm_set_current_limit(port,
5034 tcpm_get_current_limit(port),
5035 5000);
5036 break;
5037
5038 case AUDIO_ACC_ATTACHED:
5039 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
5040 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
5041 break;
5042 case AUDIO_ACC_DEBOUNCE:
5043 if (tcpm_port_is_audio(port))
5044 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
5045 break;
5046
5047 case DEBUG_ACC_ATTACHED:
5048 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
5049 tcpm_set_state(port, ACC_UNATTACHED, 0);
5050 break;
5051
5052 case SNK_TRY:
5053 /* Do nothing, waiting for timeout */
5054 break;
5055
5056 case SNK_DISCOVERY:
5057 /* CC line is unstable, wait for debounce */
5058 if (tcpm_port_is_disconnected(port))
5059 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
5060 break;
5061 case SNK_DISCOVERY_DEBOUNCE:
5062 break;
5063
5064 case SRC_TRYWAIT:
5065 /* Hand over to state machine if needed */
5066 if (!port->vbus_present && tcpm_port_is_source(port))
5067 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
5068 break;
5069 case SRC_TRYWAIT_DEBOUNCE:
5070 if (port->vbus_present || !tcpm_port_is_source(port))
5071 tcpm_set_state(port, SRC_TRYWAIT, 0);
5072 break;
5073 case SNK_TRY_WAIT_DEBOUNCE:
5074 if (!tcpm_port_is_sink(port)) {
5075 port->max_wait = 0;
5076 tcpm_set_state(port, SRC_TRYWAIT, 0);
5077 }
5078 break;
5079 case SRC_TRY_WAIT:
5080 if (tcpm_port_is_source(port))
5081 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
5082 break;
5083 case SRC_TRY_DEBOUNCE:
5084 tcpm_set_state(port, SRC_TRY_WAIT, 0);
5085 break;
5086 case SNK_TRYWAIT_DEBOUNCE:
5087 if (tcpm_port_is_sink(port))
5088 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
5089 break;
5090 case SNK_TRYWAIT_VBUS:
5091 if (!tcpm_port_is_sink(port))
5092 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
5093 break;
5094 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
5095 if (!tcpm_port_is_sink(port))
5096 tcpm_set_state(port, SRC_TRYWAIT, PD_T_TRY_CC_DEBOUNCE);
5097 else
5098 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, 0);
5099 break;
5100 case SNK_TRYWAIT:
5101 /* Do nothing, waiting for tCCDebounce */
5102 break;
5103 case PR_SWAP_SNK_SRC_SINK_OFF:
5104 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
5105 case PR_SWAP_SRC_SNK_SOURCE_OFF:
5106 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
5107 case PR_SWAP_SNK_SRC_SOURCE_ON:
5108 /*
5109 * CC state change is expected in PR_SWAP
5110 * Ignore it.
5111 */
5112 break;
5113 case FR_SWAP_SEND:
5114 case FR_SWAP_SEND_TIMEOUT:
5115 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5116 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5117 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5118 /* Do nothing, CC change expected */
5119 break;
5120
5121 case PORT_RESET:
5122 case PORT_RESET_WAIT_OFF:
5123 /*
5124 * State set back to default mode once the timer completes.
5125 * Ignore CC changes here.
5126 */
5127 break;
5128 default:
5129 /*
5130 * While acting as sink and auto vbus discharge is enabled, Allow disconnect
5131 * to be driven by vbus disconnect.
5132 */
5133 if (tcpm_port_is_disconnected(port) && !(port->pwr_role == TYPEC_SINK &&
5134 port->auto_vbus_discharge_enabled))
5135 tcpm_set_state(port, unattached_state(port), 0);
5136 break;
5137 }
5138 }
5139
_tcpm_pd_vbus_on(struct tcpm_port * port)5140 static void _tcpm_pd_vbus_on(struct tcpm_port *port)
5141 {
5142 tcpm_log_force(port, "VBUS on");
5143 port->vbus_present = true;
5144 /*
5145 * When vbus_present is true i.e. Voltage at VBUS is greater than VSAFE5V implicitly
5146 * states that vbus is not at VSAFE0V, hence clear the vbus_vsafe0v flag here.
5147 */
5148 port->vbus_vsafe0v = false;
5149
5150 switch (port->state) {
5151 case SNK_TRANSITION_SINK_VBUS:
5152 port->explicit_contract = true;
5153 tcpm_set_state(port, SNK_READY, 0);
5154 break;
5155 case SNK_DISCOVERY:
5156 tcpm_set_state(port, SNK_DISCOVERY, 0);
5157 break;
5158
5159 case SNK_DEBOUNCED:
5160 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
5161 : SNK_ATTACHED,
5162 0);
5163 break;
5164 case SNK_HARD_RESET_WAIT_VBUS:
5165 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
5166 break;
5167 case SRC_ATTACHED:
5168 tcpm_set_state(port, SRC_STARTUP, 0);
5169 break;
5170 case SRC_HARD_RESET_VBUS_ON:
5171 tcpm_set_state(port, SRC_STARTUP, 0);
5172 break;
5173
5174 case SNK_TRY:
5175 /* Do nothing, waiting for timeout */
5176 break;
5177 case SRC_TRYWAIT:
5178 /* Do nothing, Waiting for Rd to be detected */
5179 break;
5180 case SRC_TRYWAIT_DEBOUNCE:
5181 tcpm_set_state(port, SRC_TRYWAIT, 0);
5182 break;
5183 case SNK_TRY_WAIT_DEBOUNCE:
5184 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
5185 break;
5186 case SNK_TRYWAIT:
5187 /* Do nothing, waiting for tCCDebounce */
5188 break;
5189 case SNK_TRYWAIT_VBUS:
5190 if (tcpm_port_is_sink(port))
5191 tcpm_set_state(port, SNK_ATTACHED, 0);
5192 break;
5193 case SNK_TRYWAIT_DEBOUNCE:
5194 /* Do nothing, waiting for Rp */
5195 break;
5196 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
5197 if (port->vbus_present && tcpm_port_is_sink(port))
5198 tcpm_set_state(port, SNK_ATTACHED, 0);
5199 break;
5200 case SRC_TRY_WAIT:
5201 case SRC_TRY_DEBOUNCE:
5202 /* Do nothing, waiting for sink detection */
5203 break;
5204 case FR_SWAP_SEND:
5205 case FR_SWAP_SEND_TIMEOUT:
5206 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5207 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5208 if (port->tcpc->frs_sourcing_vbus)
5209 port->tcpc->frs_sourcing_vbus(port->tcpc);
5210 break;
5211 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5212 if (port->tcpc->frs_sourcing_vbus)
5213 port->tcpc->frs_sourcing_vbus(port->tcpc);
5214 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
5215 break;
5216
5217 case PORT_RESET:
5218 case PORT_RESET_WAIT_OFF:
5219 /*
5220 * State set back to default mode once the timer completes.
5221 * Ignore vbus changes here.
5222 */
5223 break;
5224
5225 default:
5226 break;
5227 }
5228 }
5229
_tcpm_pd_vbus_off(struct tcpm_port * port)5230 static void _tcpm_pd_vbus_off(struct tcpm_port *port)
5231 {
5232 tcpm_log_force(port, "VBUS off");
5233 port->vbus_present = false;
5234 port->vbus_never_low = false;
5235 switch (port->state) {
5236 case SNK_HARD_RESET_SINK_OFF:
5237 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
5238 break;
5239 case HARD_RESET_SEND:
5240 break;
5241 case SNK_TRY:
5242 /* Do nothing, waiting for timeout */
5243 break;
5244 case SRC_TRYWAIT:
5245 /* Hand over to state machine if needed */
5246 if (tcpm_port_is_source(port))
5247 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
5248 break;
5249 case SNK_TRY_WAIT_DEBOUNCE:
5250 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
5251 break;
5252 case SNK_TRYWAIT:
5253 case SNK_TRYWAIT_VBUS:
5254 case SNK_TRYWAIT_DEBOUNCE:
5255 break;
5256 case SNK_ATTACH_WAIT:
5257 case SNK_DEBOUNCED:
5258 /* Do nothing, as TCPM is still waiting for vbus to reaach VSAFE5V to connect */
5259 break;
5260
5261 case SNK_NEGOTIATE_CAPABILITIES:
5262 break;
5263
5264 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
5265 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
5266 break;
5267
5268 case PR_SWAP_SNK_SRC_SINK_OFF:
5269 /* Do nothing, expected */
5270 break;
5271
5272 case PR_SWAP_SNK_SRC_SOURCE_ON:
5273 /*
5274 * Do nothing when vbus off notification is received.
5275 * TCPM can wait for PD_T_NEWSRC in PR_SWAP_SNK_SRC_SOURCE_ON
5276 * for the vbus source to ramp up.
5277 */
5278 break;
5279
5280 case PORT_RESET_WAIT_OFF:
5281 tcpm_set_state(port, tcpm_default_state(port), 0);
5282 break;
5283
5284 case SRC_TRY_WAIT:
5285 case SRC_TRY_DEBOUNCE:
5286 /* Do nothing, waiting for sink detection */
5287 break;
5288
5289 case SRC_STARTUP:
5290 case SRC_SEND_CAPABILITIES:
5291 case SRC_SEND_CAPABILITIES_TIMEOUT:
5292 case SRC_NEGOTIATE_CAPABILITIES:
5293 case SRC_TRANSITION_SUPPLY:
5294 case SRC_READY:
5295 case SRC_WAIT_NEW_CAPABILITIES:
5296 /*
5297 * Force to unattached state to re-initiate connection.
5298 * DRP port should move to Unattached.SNK instead of Unattached.SRC if
5299 * sink removed. Although sink removal here is due to source's vbus collapse,
5300 * treat it the same way for consistency.
5301 */
5302 if (port->port_type == TYPEC_PORT_SRC)
5303 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port));
5304 else
5305 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
5306 break;
5307
5308 case PORT_RESET:
5309 /*
5310 * State set back to default mode once the timer completes.
5311 * Ignore vbus changes here.
5312 */
5313 break;
5314
5315 case FR_SWAP_SEND:
5316 case FR_SWAP_SEND_TIMEOUT:
5317 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5318 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5319 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5320 /* Do nothing, vbus drop expected */
5321 break;
5322
5323 case SNK_HARD_RESET_WAIT_VBUS:
5324 /* Do nothing, its OK to receive vbus off events */
5325 break;
5326
5327 default:
5328 if (port->pwr_role == TYPEC_SINK && port->attached)
5329 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
5330 break;
5331 }
5332 }
5333
_tcpm_pd_vbus_vsafe0v(struct tcpm_port * port)5334 static void _tcpm_pd_vbus_vsafe0v(struct tcpm_port *port)
5335 {
5336 tcpm_log_force(port, "VBUS VSAFE0V");
5337 port->vbus_vsafe0v = true;
5338 switch (port->state) {
5339 case SRC_HARD_RESET_VBUS_OFF:
5340 /*
5341 * After establishing the vSafe0V voltage condition on VBUS, the Source Shall wait
5342 * tSrcRecover before re-applying VCONN and restoring VBUS to vSafe5V.
5343 */
5344 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
5345 break;
5346 case SRC_ATTACH_WAIT:
5347 if (tcpm_port_is_source(port))
5348 tcpm_set_state(port, tcpm_try_snk(port) ? SNK_TRY : SRC_ATTACHED,
5349 PD_T_CC_DEBOUNCE);
5350 break;
5351 case SRC_STARTUP:
5352 case SRC_SEND_CAPABILITIES:
5353 case SRC_SEND_CAPABILITIES_TIMEOUT:
5354 case SRC_NEGOTIATE_CAPABILITIES:
5355 case SRC_TRANSITION_SUPPLY:
5356 case SRC_READY:
5357 case SRC_WAIT_NEW_CAPABILITIES:
5358 if (port->auto_vbus_discharge_enabled) {
5359 if (port->port_type == TYPEC_PORT_SRC)
5360 tcpm_set_state(port, SRC_UNATTACHED, 0);
5361 else
5362 tcpm_set_state(port, SNK_UNATTACHED, 0);
5363 }
5364 break;
5365 case PR_SWAP_SNK_SRC_SINK_OFF:
5366 case PR_SWAP_SNK_SRC_SOURCE_ON:
5367 /* Do nothing, vsafe0v is expected during transition */
5368 break;
5369 case SNK_ATTACH_WAIT:
5370 case SNK_DEBOUNCED:
5371 /*Do nothing, still waiting for VSAFE5V for connect */
5372 break;
5373 case SNK_HARD_RESET_WAIT_VBUS:
5374 /* Do nothing, its OK to receive vbus off events */
5375 break;
5376 default:
5377 if (port->pwr_role == TYPEC_SINK && port->auto_vbus_discharge_enabled)
5378 tcpm_set_state(port, SNK_UNATTACHED, 0);
5379 break;
5380 }
5381 }
5382
_tcpm_pd_hard_reset(struct tcpm_port * port)5383 static void _tcpm_pd_hard_reset(struct tcpm_port *port)
5384 {
5385 tcpm_log_force(port, "Received hard reset");
5386 if (port->bist_request == BDO_MODE_TESTDATA && port->tcpc->set_bist_data)
5387 port->tcpc->set_bist_data(port->tcpc, false);
5388
5389 if (port->ams != NONE_AMS)
5390 port->ams = NONE_AMS;
5391 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
5392 port->ams = HARD_RESET;
5393 /*
5394 * If we keep receiving hard reset requests, executing the hard reset
5395 * must have failed. Revert to error recovery if that happens.
5396 */
5397 tcpm_set_state(port,
5398 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
5399 HARD_RESET_START : ERROR_RECOVERY,
5400 0);
5401 }
5402
tcpm_pd_event_handler(struct kthread_work * work)5403 static void tcpm_pd_event_handler(struct kthread_work *work)
5404 {
5405 struct tcpm_port *port = container_of(work, struct tcpm_port,
5406 event_work);
5407 u32 events;
5408
5409 mutex_lock(&port->lock);
5410
5411 spin_lock(&port->pd_event_lock);
5412 while (port->pd_events) {
5413 events = port->pd_events;
5414 port->pd_events = 0;
5415 spin_unlock(&port->pd_event_lock);
5416 if (events & TCPM_RESET_EVENT)
5417 _tcpm_pd_hard_reset(port);
5418 if (events & TCPM_VBUS_EVENT) {
5419 bool vbus;
5420
5421 vbus = port->tcpc->get_vbus(port->tcpc);
5422 if (vbus) {
5423 _tcpm_pd_vbus_on(port);
5424 } else {
5425 _tcpm_pd_vbus_off(port);
5426 /*
5427 * When TCPC does not support detecting vsafe0v voltage level,
5428 * treat vbus absent as vsafe0v. Else invoke is_vbus_vsafe0v
5429 * to see if vbus has discharge to VSAFE0V.
5430 */
5431 if (!port->tcpc->is_vbus_vsafe0v ||
5432 port->tcpc->is_vbus_vsafe0v(port->tcpc))
5433 _tcpm_pd_vbus_vsafe0v(port);
5434 }
5435 }
5436 if (events & TCPM_CC_EVENT) {
5437 enum typec_cc_status cc1, cc2;
5438
5439 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
5440 _tcpm_cc_change(port, cc1, cc2);
5441 }
5442 if (events & TCPM_FRS_EVENT) {
5443 if (port->state == SNK_READY) {
5444 int ret;
5445
5446 port->upcoming_state = FR_SWAP_SEND;
5447 ret = tcpm_ams_start(port, FAST_ROLE_SWAP);
5448 if (ret == -EAGAIN)
5449 port->upcoming_state = INVALID_STATE;
5450 } else {
5451 tcpm_log(port, "Discarding FRS_SIGNAL! Not in sink ready");
5452 }
5453 }
5454 if (events & TCPM_SOURCING_VBUS) {
5455 tcpm_log(port, "sourcing vbus");
5456 /*
5457 * In fast role swap case TCPC autonomously sources vbus. Set vbus_source
5458 * true as TCPM wouldn't have called tcpm_set_vbus.
5459 *
5460 * When vbus is sourced on the command on TCPM i.e. TCPM called
5461 * tcpm_set_vbus to source vbus, vbus_source would already be true.
5462 */
5463 port->vbus_source = true;
5464 _tcpm_pd_vbus_on(port);
5465 }
5466 if (events & TCPM_PORT_CLEAN) {
5467 tcpm_log(port, "port clean");
5468 if (port->state == CHECK_CONTAMINANT) {
5469 if (tcpm_start_toggling(port, tcpm_rp_cc(port)))
5470 tcpm_set_state(port, TOGGLING, 0);
5471 else
5472 tcpm_set_state(port, tcpm_default_state(port), 0);
5473 }
5474 }
5475
5476 spin_lock(&port->pd_event_lock);
5477 }
5478 spin_unlock(&port->pd_event_lock);
5479 mutex_unlock(&port->lock);
5480 }
5481
tcpm_cc_change(struct tcpm_port * port)5482 void tcpm_cc_change(struct tcpm_port *port)
5483 {
5484 spin_lock(&port->pd_event_lock);
5485 port->pd_events |= TCPM_CC_EVENT;
5486 spin_unlock(&port->pd_event_lock);
5487 kthread_queue_work(port->wq, &port->event_work);
5488 }
5489 EXPORT_SYMBOL_GPL(tcpm_cc_change);
5490
tcpm_vbus_change(struct tcpm_port * port)5491 void tcpm_vbus_change(struct tcpm_port *port)
5492 {
5493 spin_lock(&port->pd_event_lock);
5494 port->pd_events |= TCPM_VBUS_EVENT;
5495 spin_unlock(&port->pd_event_lock);
5496 kthread_queue_work(port->wq, &port->event_work);
5497 }
5498 EXPORT_SYMBOL_GPL(tcpm_vbus_change);
5499
tcpm_pd_hard_reset(struct tcpm_port * port)5500 void tcpm_pd_hard_reset(struct tcpm_port *port)
5501 {
5502 spin_lock(&port->pd_event_lock);
5503 port->pd_events = TCPM_RESET_EVENT;
5504 spin_unlock(&port->pd_event_lock);
5505 kthread_queue_work(port->wq, &port->event_work);
5506 }
5507 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
5508
tcpm_sink_frs(struct tcpm_port * port)5509 void tcpm_sink_frs(struct tcpm_port *port)
5510 {
5511 spin_lock(&port->pd_event_lock);
5512 port->pd_events |= TCPM_FRS_EVENT;
5513 spin_unlock(&port->pd_event_lock);
5514 kthread_queue_work(port->wq, &port->event_work);
5515 }
5516 EXPORT_SYMBOL_GPL(tcpm_sink_frs);
5517
tcpm_sourcing_vbus(struct tcpm_port * port)5518 void tcpm_sourcing_vbus(struct tcpm_port *port)
5519 {
5520 spin_lock(&port->pd_event_lock);
5521 port->pd_events |= TCPM_SOURCING_VBUS;
5522 spin_unlock(&port->pd_event_lock);
5523 kthread_queue_work(port->wq, &port->event_work);
5524 }
5525 EXPORT_SYMBOL_GPL(tcpm_sourcing_vbus);
5526
tcpm_port_clean(struct tcpm_port * port)5527 void tcpm_port_clean(struct tcpm_port *port)
5528 {
5529 spin_lock(&port->pd_event_lock);
5530 port->pd_events |= TCPM_PORT_CLEAN;
5531 spin_unlock(&port->pd_event_lock);
5532 kthread_queue_work(port->wq, &port->event_work);
5533 }
5534 EXPORT_SYMBOL_GPL(tcpm_port_clean);
5535
tcpm_port_is_toggling(struct tcpm_port * port)5536 bool tcpm_port_is_toggling(struct tcpm_port *port)
5537 {
5538 return port->port_type == TYPEC_PORT_DRP && port->state == TOGGLING;
5539 }
5540 EXPORT_SYMBOL_GPL(tcpm_port_is_toggling);
5541
tcpm_enable_frs_work(struct kthread_work * work)5542 static void tcpm_enable_frs_work(struct kthread_work *work)
5543 {
5544 struct tcpm_port *port = container_of(work, struct tcpm_port, enable_frs);
5545 int ret;
5546
5547 mutex_lock(&port->lock);
5548 /* Not FRS capable */
5549 if (!port->connected || port->port_type != TYPEC_PORT_DRP ||
5550 port->pwr_opmode != TYPEC_PWR_MODE_PD ||
5551 !port->tcpc->enable_frs ||
5552 /* Sink caps queried */
5553 port->sink_cap_done || port->negotiated_rev < PD_REV30)
5554 goto unlock;
5555
5556 /* Send when the state machine is idle */
5557 if (port->state != SNK_READY || port->vdm_sm_running || port->send_discover)
5558 goto resched;
5559
5560 port->upcoming_state = GET_SINK_CAP;
5561 ret = tcpm_ams_start(port, GET_SINK_CAPABILITIES);
5562 if (ret == -EAGAIN) {
5563 port->upcoming_state = INVALID_STATE;
5564 } else {
5565 port->sink_cap_done = true;
5566 goto unlock;
5567 }
5568 resched:
5569 mod_enable_frs_delayed_work(port, GET_SINK_CAP_RETRY_MS);
5570 unlock:
5571 mutex_unlock(&port->lock);
5572 }
5573
tcpm_send_discover_work(struct kthread_work * work)5574 static void tcpm_send_discover_work(struct kthread_work *work)
5575 {
5576 struct tcpm_port *port = container_of(work, struct tcpm_port, send_discover_work);
5577
5578 mutex_lock(&port->lock);
5579 /* No need to send DISCOVER_IDENTITY anymore */
5580 if (!port->send_discover)
5581 goto unlock;
5582
5583 if (port->data_role == TYPEC_DEVICE && port->negotiated_rev < PD_REV30) {
5584 port->send_discover = false;
5585 goto unlock;
5586 }
5587
5588 /* Retry if the port is not idle */
5589 if ((port->state != SRC_READY && port->state != SNK_READY) || port->vdm_sm_running) {
5590 mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS);
5591 goto unlock;
5592 }
5593
5594 tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0);
5595
5596 unlock:
5597 mutex_unlock(&port->lock);
5598 }
5599
tcpm_dr_set(struct typec_port * p,enum typec_data_role data)5600 static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
5601 {
5602 struct tcpm_port *port = typec_get_drvdata(p);
5603 int ret;
5604
5605 mutex_lock(&port->swap_lock);
5606 mutex_lock(&port->lock);
5607
5608 if (port->typec_caps.data != TYPEC_PORT_DRD) {
5609 ret = -EINVAL;
5610 goto port_unlock;
5611 }
5612 if (port->state != SRC_READY && port->state != SNK_READY) {
5613 ret = -EAGAIN;
5614 goto port_unlock;
5615 }
5616
5617 if (port->data_role == data) {
5618 ret = 0;
5619 goto port_unlock;
5620 }
5621
5622 /*
5623 * XXX
5624 * 6.3.9: If an alternate mode is active, a request to swap
5625 * alternate modes shall trigger a port reset.
5626 * Reject data role swap request in this case.
5627 */
5628
5629 if (!port->pd_capable) {
5630 /*
5631 * If the partner is not PD capable, reset the port to
5632 * trigger a role change. This can only work if a preferred
5633 * role is configured, and if it matches the requested role.
5634 */
5635 if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
5636 port->try_role == port->pwr_role) {
5637 ret = -EINVAL;
5638 goto port_unlock;
5639 }
5640 port->non_pd_role_swap = true;
5641 tcpm_set_state(port, PORT_RESET, 0);
5642 } else {
5643 port->upcoming_state = DR_SWAP_SEND;
5644 ret = tcpm_ams_start(port, DATA_ROLE_SWAP);
5645 if (ret == -EAGAIN) {
5646 port->upcoming_state = INVALID_STATE;
5647 goto port_unlock;
5648 }
5649 }
5650
5651 port->swap_status = 0;
5652 port->swap_pending = true;
5653 reinit_completion(&port->swap_complete);
5654 mutex_unlock(&port->lock);
5655
5656 if (!wait_for_completion_timeout(&port->swap_complete,
5657 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
5658 ret = -ETIMEDOUT;
5659 else
5660 ret = port->swap_status;
5661
5662 port->non_pd_role_swap = false;
5663 goto swap_unlock;
5664
5665 port_unlock:
5666 mutex_unlock(&port->lock);
5667 swap_unlock:
5668 mutex_unlock(&port->swap_lock);
5669 return ret;
5670 }
5671
tcpm_pr_set(struct typec_port * p,enum typec_role role)5672 static int tcpm_pr_set(struct typec_port *p, enum typec_role role)
5673 {
5674 struct tcpm_port *port = typec_get_drvdata(p);
5675 int ret;
5676
5677 mutex_lock(&port->swap_lock);
5678 mutex_lock(&port->lock);
5679
5680 if (port->port_type != TYPEC_PORT_DRP) {
5681 ret = -EINVAL;
5682 goto port_unlock;
5683 }
5684 if (port->state != SRC_READY && port->state != SNK_READY) {
5685 ret = -EAGAIN;
5686 goto port_unlock;
5687 }
5688
5689 if (role == port->pwr_role) {
5690 ret = 0;
5691 goto port_unlock;
5692 }
5693
5694 port->upcoming_state = PR_SWAP_SEND;
5695 ret = tcpm_ams_start(port, POWER_ROLE_SWAP);
5696 if (ret == -EAGAIN) {
5697 port->upcoming_state = INVALID_STATE;
5698 goto port_unlock;
5699 }
5700
5701 port->swap_status = 0;
5702 port->swap_pending = true;
5703 reinit_completion(&port->swap_complete);
5704 mutex_unlock(&port->lock);
5705
5706 if (!wait_for_completion_timeout(&port->swap_complete,
5707 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
5708 ret = -ETIMEDOUT;
5709 else
5710 ret = port->swap_status;
5711
5712 goto swap_unlock;
5713
5714 port_unlock:
5715 mutex_unlock(&port->lock);
5716 swap_unlock:
5717 mutex_unlock(&port->swap_lock);
5718 return ret;
5719 }
5720
tcpm_vconn_set(struct typec_port * p,enum typec_role role)5721 static int tcpm_vconn_set(struct typec_port *p, enum typec_role role)
5722 {
5723 struct tcpm_port *port = typec_get_drvdata(p);
5724 int ret;
5725
5726 mutex_lock(&port->swap_lock);
5727 mutex_lock(&port->lock);
5728
5729 if (port->state != SRC_READY && port->state != SNK_READY) {
5730 ret = -EAGAIN;
5731 goto port_unlock;
5732 }
5733
5734 if (role == port->vconn_role) {
5735 ret = 0;
5736 goto port_unlock;
5737 }
5738
5739 port->upcoming_state = VCONN_SWAP_SEND;
5740 ret = tcpm_ams_start(port, VCONN_SWAP);
5741 if (ret == -EAGAIN) {
5742 port->upcoming_state = INVALID_STATE;
5743 goto port_unlock;
5744 }
5745
5746 port->swap_status = 0;
5747 port->swap_pending = true;
5748 reinit_completion(&port->swap_complete);
5749 mutex_unlock(&port->lock);
5750
5751 if (!wait_for_completion_timeout(&port->swap_complete,
5752 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
5753 ret = -ETIMEDOUT;
5754 else
5755 ret = port->swap_status;
5756
5757 goto swap_unlock;
5758
5759 port_unlock:
5760 mutex_unlock(&port->lock);
5761 swap_unlock:
5762 mutex_unlock(&port->swap_lock);
5763 return ret;
5764 }
5765
tcpm_try_role(struct typec_port * p,int role)5766 static int tcpm_try_role(struct typec_port *p, int role)
5767 {
5768 struct tcpm_port *port = typec_get_drvdata(p);
5769 struct tcpc_dev *tcpc = port->tcpc;
5770 int ret = 0;
5771
5772 mutex_lock(&port->lock);
5773 if (tcpc->try_role)
5774 ret = tcpc->try_role(tcpc, role);
5775 if (!ret)
5776 port->try_role = role;
5777 port->try_src_count = 0;
5778 port->try_snk_count = 0;
5779 mutex_unlock(&port->lock);
5780
5781 return ret;
5782 }
5783
tcpm_pps_set_op_curr(struct tcpm_port * port,u16 req_op_curr)5784 static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 req_op_curr)
5785 {
5786 unsigned int target_mw;
5787 int ret;
5788
5789 mutex_lock(&port->swap_lock);
5790 mutex_lock(&port->lock);
5791
5792 if (!port->pps_data.active) {
5793 ret = -EOPNOTSUPP;
5794 goto port_unlock;
5795 }
5796
5797 if (port->state != SNK_READY) {
5798 ret = -EAGAIN;
5799 goto port_unlock;
5800 }
5801
5802 if (req_op_curr > port->pps_data.max_curr) {
5803 ret = -EINVAL;
5804 goto port_unlock;
5805 }
5806
5807 target_mw = (req_op_curr * port->supply_voltage) / 1000;
5808 if (target_mw < port->operating_snk_mw) {
5809 ret = -EINVAL;
5810 goto port_unlock;
5811 }
5812
5813 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
5814 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
5815 if (ret == -EAGAIN) {
5816 port->upcoming_state = INVALID_STATE;
5817 goto port_unlock;
5818 }
5819
5820 /* Round down operating current to align with PPS valid steps */
5821 req_op_curr = req_op_curr - (req_op_curr % RDO_PROG_CURR_MA_STEP);
5822
5823 reinit_completion(&port->pps_complete);
5824 port->pps_data.req_op_curr = req_op_curr;
5825 port->pps_status = 0;
5826 port->pps_pending = true;
5827 mutex_unlock(&port->lock);
5828
5829 if (!wait_for_completion_timeout(&port->pps_complete,
5830 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
5831 ret = -ETIMEDOUT;
5832 else
5833 ret = port->pps_status;
5834
5835 goto swap_unlock;
5836
5837 port_unlock:
5838 mutex_unlock(&port->lock);
5839 swap_unlock:
5840 mutex_unlock(&port->swap_lock);
5841
5842 return ret;
5843 }
5844
tcpm_pps_set_out_volt(struct tcpm_port * port,u16 req_out_volt)5845 static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 req_out_volt)
5846 {
5847 unsigned int target_mw;
5848 int ret;
5849
5850 mutex_lock(&port->swap_lock);
5851 mutex_lock(&port->lock);
5852
5853 if (!port->pps_data.active) {
5854 ret = -EOPNOTSUPP;
5855 goto port_unlock;
5856 }
5857
5858 if (port->state != SNK_READY) {
5859 ret = -EAGAIN;
5860 goto port_unlock;
5861 }
5862
5863 target_mw = (port->current_limit * req_out_volt) / 1000;
5864 if (target_mw < port->operating_snk_mw) {
5865 ret = -EINVAL;
5866 goto port_unlock;
5867 }
5868
5869 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
5870 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
5871 if (ret == -EAGAIN) {
5872 port->upcoming_state = INVALID_STATE;
5873 goto port_unlock;
5874 }
5875
5876 /* Round down output voltage to align with PPS valid steps */
5877 req_out_volt = req_out_volt - (req_out_volt % RDO_PROG_VOLT_MV_STEP);
5878
5879 reinit_completion(&port->pps_complete);
5880 port->pps_data.req_out_volt = req_out_volt;
5881 port->pps_status = 0;
5882 port->pps_pending = true;
5883 mutex_unlock(&port->lock);
5884
5885 if (!wait_for_completion_timeout(&port->pps_complete,
5886 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
5887 ret = -ETIMEDOUT;
5888 else
5889 ret = port->pps_status;
5890
5891 goto swap_unlock;
5892
5893 port_unlock:
5894 mutex_unlock(&port->lock);
5895 swap_unlock:
5896 mutex_unlock(&port->swap_lock);
5897
5898 return ret;
5899 }
5900
tcpm_pps_activate(struct tcpm_port * port,bool activate)5901 static int tcpm_pps_activate(struct tcpm_port *port, bool activate)
5902 {
5903 int ret = 0;
5904
5905 mutex_lock(&port->swap_lock);
5906 mutex_lock(&port->lock);
5907
5908 if (!port->pps_data.supported) {
5909 ret = -EOPNOTSUPP;
5910 goto port_unlock;
5911 }
5912
5913 /* Trying to deactivate PPS when already deactivated so just bail */
5914 if (!port->pps_data.active && !activate)
5915 goto port_unlock;
5916
5917 if (port->state != SNK_READY) {
5918 ret = -EAGAIN;
5919 goto port_unlock;
5920 }
5921
5922 if (activate)
5923 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
5924 else
5925 port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES;
5926 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
5927 if (ret == -EAGAIN) {
5928 port->upcoming_state = INVALID_STATE;
5929 goto port_unlock;
5930 }
5931
5932 reinit_completion(&port->pps_complete);
5933 port->pps_status = 0;
5934 port->pps_pending = true;
5935
5936 /* Trigger PPS request or move back to standard PDO contract */
5937 if (activate) {
5938 port->pps_data.req_out_volt = port->supply_voltage;
5939 port->pps_data.req_op_curr = port->current_limit;
5940 }
5941 mutex_unlock(&port->lock);
5942
5943 if (!wait_for_completion_timeout(&port->pps_complete,
5944 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
5945 ret = -ETIMEDOUT;
5946 else
5947 ret = port->pps_status;
5948
5949 goto swap_unlock;
5950
5951 port_unlock:
5952 mutex_unlock(&port->lock);
5953 swap_unlock:
5954 mutex_unlock(&port->swap_lock);
5955
5956 return ret;
5957 }
5958
tcpm_init(struct tcpm_port * port)5959 static void tcpm_init(struct tcpm_port *port)
5960 {
5961 enum typec_cc_status cc1, cc2;
5962
5963 port->tcpc->init(port->tcpc);
5964
5965 tcpm_reset_port(port);
5966
5967 /*
5968 * XXX
5969 * Should possibly wait for VBUS to settle if it was enabled locally
5970 * since tcpm_reset_port() will disable VBUS.
5971 */
5972 port->vbus_present = port->tcpc->get_vbus(port->tcpc);
5973 if (port->vbus_present)
5974 port->vbus_never_low = true;
5975
5976 /*
5977 * 1. When vbus_present is true, voltage on VBUS is already at VSAFE5V.
5978 * So implicitly vbus_vsafe0v = false.
5979 *
5980 * 2. When vbus_present is false and TCPC does NOT support querying
5981 * vsafe0v status, then, it's best to assume vbus is at VSAFE0V i.e.
5982 * vbus_vsafe0v is true.
5983 *
5984 * 3. When vbus_present is false and TCPC does support querying vsafe0v,
5985 * then, query tcpc for vsafe0v status.
5986 */
5987 if (port->vbus_present)
5988 port->vbus_vsafe0v = false;
5989 else if (!port->tcpc->is_vbus_vsafe0v)
5990 port->vbus_vsafe0v = true;
5991 else
5992 port->vbus_vsafe0v = port->tcpc->is_vbus_vsafe0v(port->tcpc);
5993
5994 tcpm_set_state(port, tcpm_default_state(port), 0);
5995
5996 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
5997 _tcpm_cc_change(port, cc1, cc2);
5998
5999 /*
6000 * Some adapters need a clean slate at startup, and won't recover
6001 * otherwise. So do not try to be fancy and force a clean disconnect.
6002 */
6003 tcpm_set_state(port, PORT_RESET, 0);
6004 }
6005
tcpm_port_type_set(struct typec_port * p,enum typec_port_type type)6006 static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type)
6007 {
6008 struct tcpm_port *port = typec_get_drvdata(p);
6009
6010 mutex_lock(&port->lock);
6011 if (type == port->port_type)
6012 goto port_unlock;
6013
6014 port->port_type = type;
6015
6016 if (!port->connected) {
6017 tcpm_set_state(port, PORT_RESET, 0);
6018 } else if (type == TYPEC_PORT_SNK) {
6019 if (!(port->pwr_role == TYPEC_SINK &&
6020 port->data_role == TYPEC_DEVICE))
6021 tcpm_set_state(port, PORT_RESET, 0);
6022 } else if (type == TYPEC_PORT_SRC) {
6023 if (!(port->pwr_role == TYPEC_SOURCE &&
6024 port->data_role == TYPEC_HOST))
6025 tcpm_set_state(port, PORT_RESET, 0);
6026 }
6027
6028 port_unlock:
6029 mutex_unlock(&port->lock);
6030 return 0;
6031 }
6032
6033 static const struct typec_operations tcpm_ops = {
6034 .try_role = tcpm_try_role,
6035 .dr_set = tcpm_dr_set,
6036 .pr_set = tcpm_pr_set,
6037 .vconn_set = tcpm_vconn_set,
6038 .port_type_set = tcpm_port_type_set
6039 };
6040
tcpm_tcpc_reset(struct tcpm_port * port)6041 void tcpm_tcpc_reset(struct tcpm_port *port)
6042 {
6043 mutex_lock(&port->lock);
6044 /* XXX: Maintain PD connection if possible? */
6045 tcpm_init(port);
6046 mutex_unlock(&port->lock);
6047 }
6048 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
6049
tcpm_port_unregister_pd(struct tcpm_port * port)6050 static void tcpm_port_unregister_pd(struct tcpm_port *port)
6051 {
6052 usb_power_delivery_unregister_capabilities(port->port_sink_caps);
6053 port->port_sink_caps = NULL;
6054 usb_power_delivery_unregister_capabilities(port->port_source_caps);
6055 port->port_source_caps = NULL;
6056 usb_power_delivery_unregister(port->pd);
6057 port->pd = NULL;
6058 }
6059
tcpm_port_register_pd(struct tcpm_port * port)6060 static int tcpm_port_register_pd(struct tcpm_port *port)
6061 {
6062 struct usb_power_delivery_desc desc = { port->typec_caps.pd_revision };
6063 struct usb_power_delivery_capabilities_desc caps = { };
6064 struct usb_power_delivery_capabilities *cap;
6065 int ret;
6066
6067 if (!port->nr_src_pdo && !port->nr_snk_pdo)
6068 return 0;
6069
6070 port->pd = usb_power_delivery_register(port->dev, &desc);
6071 if (IS_ERR(port->pd)) {
6072 ret = PTR_ERR(port->pd);
6073 goto err_unregister;
6074 }
6075
6076 if (port->nr_src_pdo) {
6077 memcpy_and_pad(caps.pdo, sizeof(caps.pdo), port->src_pdo,
6078 port->nr_src_pdo * sizeof(u32), 0);
6079 caps.role = TYPEC_SOURCE;
6080
6081 cap = usb_power_delivery_register_capabilities(port->pd, &caps);
6082 if (IS_ERR(cap)) {
6083 ret = PTR_ERR(cap);
6084 goto err_unregister;
6085 }
6086
6087 port->port_source_caps = cap;
6088 }
6089
6090 if (port->nr_snk_pdo) {
6091 memcpy_and_pad(caps.pdo, sizeof(caps.pdo), port->snk_pdo,
6092 port->nr_snk_pdo * sizeof(u32), 0);
6093 caps.role = TYPEC_SINK;
6094
6095 cap = usb_power_delivery_register_capabilities(port->pd, &caps);
6096 if (IS_ERR(cap)) {
6097 ret = PTR_ERR(cap);
6098 goto err_unregister;
6099 }
6100
6101 port->port_sink_caps = cap;
6102 }
6103
6104 return 0;
6105
6106 err_unregister:
6107 tcpm_port_unregister_pd(port);
6108
6109 return ret;
6110 }
6111
tcpm_fw_get_caps(struct tcpm_port * port,struct fwnode_handle * fwnode)6112 static int tcpm_fw_get_caps(struct tcpm_port *port,
6113 struct fwnode_handle *fwnode)
6114 {
6115 const char *opmode_str;
6116 int ret;
6117 u32 mw, frs_current;
6118
6119 if (!fwnode)
6120 return -EINVAL;
6121
6122 /*
6123 * This fwnode has a "compatible" property, but is never populated as a
6124 * struct device. Instead we simply parse it to read the properties.
6125 * This it breaks fw_devlink=on. To maintain backward compatibility
6126 * with existing DT files, we work around this by deleting any
6127 * fwnode_links to/from this fwnode.
6128 */
6129 fw_devlink_purge_absent_suppliers(fwnode);
6130
6131 ret = typec_get_fw_cap(&port->typec_caps, fwnode);
6132 if (ret < 0)
6133 return ret;
6134
6135 port->port_type = port->typec_caps.type;
6136 port->pd_supported = !fwnode_property_read_bool(fwnode, "pd-disable");
6137
6138 port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop");
6139 if (port->port_type == TYPEC_PORT_SNK)
6140 goto sink;
6141
6142 /* Get Source PDOs for the PD port or Source Rp value for the non-PD port */
6143 if (port->pd_supported) {
6144 ret = fwnode_property_count_u32(fwnode, "source-pdos");
6145 if (ret == 0)
6146 return -EINVAL;
6147 else if (ret < 0)
6148 return ret;
6149
6150 port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
6151 ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
6152 port->src_pdo, port->nr_src_pdo);
6153 if (ret)
6154 return ret;
6155 ret = tcpm_validate_caps(port, port->src_pdo, port->nr_src_pdo);
6156 if (ret)
6157 return ret;
6158 } else {
6159 ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &opmode_str);
6160 if (ret)
6161 return ret;
6162 ret = typec_find_pwr_opmode(opmode_str);
6163 if (ret < 0)
6164 return ret;
6165 port->src_rp = tcpm_pwr_opmode_to_rp(ret);
6166 }
6167
6168 if (port->port_type == TYPEC_PORT_SRC)
6169 return 0;
6170
6171 sink:
6172 port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
6173
6174 if (!port->pd_supported)
6175 return 0;
6176
6177 /* Get sink pdos */
6178 ret = fwnode_property_count_u32(fwnode, "sink-pdos");
6179 if (ret <= 0)
6180 return -EINVAL;
6181
6182 port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
6183 ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
6184 port->snk_pdo, port->nr_snk_pdo);
6185 if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
6186 port->nr_snk_pdo))
6187 return -EINVAL;
6188
6189 if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
6190 return -EINVAL;
6191 port->operating_snk_mw = mw / 1000;
6192
6193 /* FRS can only be supported by DRP ports */
6194 if (port->port_type == TYPEC_PORT_DRP) {
6195 ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current",
6196 &frs_current);
6197 if (ret >= 0 && frs_current <= FRS_5V_3A)
6198 port->new_source_frs_current = frs_current;
6199 }
6200
6201 /* sink-vdos is optional */
6202 ret = fwnode_property_count_u32(fwnode, "sink-vdos");
6203 if (ret < 0)
6204 ret = 0;
6205
6206 port->nr_snk_vdo = min(ret, VDO_MAX_OBJECTS);
6207 if (port->nr_snk_vdo) {
6208 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos",
6209 port->snk_vdo,
6210 port->nr_snk_vdo);
6211 if (ret < 0)
6212 return ret;
6213 }
6214
6215 /* If sink-vdos is found, sink-vdos-v1 is expected for backward compatibility. */
6216 if (port->nr_snk_vdo) {
6217 ret = fwnode_property_count_u32(fwnode, "sink-vdos-v1");
6218 if (ret < 0)
6219 return ret;
6220 else if (ret == 0)
6221 return -ENODATA;
6222
6223 port->nr_snk_vdo_v1 = min(ret, VDO_MAX_OBJECTS);
6224 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos-v1",
6225 port->snk_vdo_v1,
6226 port->nr_snk_vdo_v1);
6227 if (ret < 0)
6228 return ret;
6229 }
6230
6231 return 0;
6232 }
6233
6234 /* Power Supply access to expose source power information */
6235 enum tcpm_psy_online_states {
6236 TCPM_PSY_OFFLINE = 0,
6237 TCPM_PSY_FIXED_ONLINE,
6238 TCPM_PSY_PROG_ONLINE,
6239 };
6240
6241 static enum power_supply_property tcpm_psy_props[] = {
6242 POWER_SUPPLY_PROP_USB_TYPE,
6243 POWER_SUPPLY_PROP_ONLINE,
6244 POWER_SUPPLY_PROP_VOLTAGE_MIN,
6245 POWER_SUPPLY_PROP_VOLTAGE_MAX,
6246 POWER_SUPPLY_PROP_VOLTAGE_NOW,
6247 POWER_SUPPLY_PROP_CURRENT_MAX,
6248 POWER_SUPPLY_PROP_CURRENT_NOW,
6249 };
6250
tcpm_psy_get_online(struct tcpm_port * port,union power_supply_propval * val)6251 static int tcpm_psy_get_online(struct tcpm_port *port,
6252 union power_supply_propval *val)
6253 {
6254 if (port->vbus_charge) {
6255 if (port->pps_data.active)
6256 val->intval = TCPM_PSY_PROG_ONLINE;
6257 else
6258 val->intval = TCPM_PSY_FIXED_ONLINE;
6259 } else {
6260 val->intval = TCPM_PSY_OFFLINE;
6261 }
6262
6263 return 0;
6264 }
6265
tcpm_psy_get_voltage_min(struct tcpm_port * port,union power_supply_propval * val)6266 static int tcpm_psy_get_voltage_min(struct tcpm_port *port,
6267 union power_supply_propval *val)
6268 {
6269 if (port->pps_data.active)
6270 val->intval = port->pps_data.min_volt * 1000;
6271 else
6272 val->intval = port->supply_voltage * 1000;
6273
6274 return 0;
6275 }
6276
tcpm_psy_get_voltage_max(struct tcpm_port * port,union power_supply_propval * val)6277 static int tcpm_psy_get_voltage_max(struct tcpm_port *port,
6278 union power_supply_propval *val)
6279 {
6280 if (port->pps_data.active)
6281 val->intval = port->pps_data.max_volt * 1000;
6282 else
6283 val->intval = port->supply_voltage * 1000;
6284
6285 return 0;
6286 }
6287
tcpm_psy_get_voltage_now(struct tcpm_port * port,union power_supply_propval * val)6288 static int tcpm_psy_get_voltage_now(struct tcpm_port *port,
6289 union power_supply_propval *val)
6290 {
6291 val->intval = port->supply_voltage * 1000;
6292
6293 return 0;
6294 }
6295
tcpm_psy_get_current_max(struct tcpm_port * port,union power_supply_propval * val)6296 static int tcpm_psy_get_current_max(struct tcpm_port *port,
6297 union power_supply_propval *val)
6298 {
6299 if (port->pps_data.active)
6300 val->intval = port->pps_data.max_curr * 1000;
6301 else
6302 val->intval = port->current_limit * 1000;
6303
6304 return 0;
6305 }
6306
tcpm_psy_get_current_now(struct tcpm_port * port,union power_supply_propval * val)6307 static int tcpm_psy_get_current_now(struct tcpm_port *port,
6308 union power_supply_propval *val)
6309 {
6310 val->intval = port->current_limit * 1000;
6311
6312 return 0;
6313 }
6314
tcpm_psy_get_input_power_limit(struct tcpm_port * port,union power_supply_propval * val)6315 static int tcpm_psy_get_input_power_limit(struct tcpm_port *port,
6316 union power_supply_propval *val)
6317 {
6318 unsigned int src_mv, src_ma, max_src_uw = 0;
6319 unsigned int i, tmp;
6320
6321 for (i = 0; i < port->nr_source_caps; i++) {
6322 u32 pdo = port->source_caps[i];
6323
6324 if (pdo_type(pdo) == PDO_TYPE_FIXED) {
6325 src_mv = pdo_fixed_voltage(pdo);
6326 src_ma = pdo_max_current(pdo);
6327 tmp = src_mv * src_ma;
6328 max_src_uw = tmp > max_src_uw ? tmp : max_src_uw;
6329 }
6330 }
6331
6332 val->intval = max_src_uw;
6333 return 0;
6334 }
6335
tcpm_psy_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)6336 static int tcpm_psy_get_prop(struct power_supply *psy,
6337 enum power_supply_property psp,
6338 union power_supply_propval *val)
6339 {
6340 struct tcpm_port *port = power_supply_get_drvdata(psy);
6341 int ret = 0;
6342
6343 switch (psp) {
6344 case POWER_SUPPLY_PROP_USB_TYPE:
6345 val->intval = port->usb_type;
6346 break;
6347 case POWER_SUPPLY_PROP_ONLINE:
6348 ret = tcpm_psy_get_online(port, val);
6349 break;
6350 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
6351 ret = tcpm_psy_get_voltage_min(port, val);
6352 break;
6353 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
6354 ret = tcpm_psy_get_voltage_max(port, val);
6355 break;
6356 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
6357 ret = tcpm_psy_get_voltage_now(port, val);
6358 break;
6359 case POWER_SUPPLY_PROP_CURRENT_MAX:
6360 ret = tcpm_psy_get_current_max(port, val);
6361 break;
6362 case POWER_SUPPLY_PROP_CURRENT_NOW:
6363 ret = tcpm_psy_get_current_now(port, val);
6364 break;
6365 case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT:
6366 tcpm_psy_get_input_power_limit(port, val);
6367 break;
6368 default:
6369 ret = -EINVAL;
6370 break;
6371 }
6372
6373 return ret;
6374 }
6375
tcpm_psy_set_online(struct tcpm_port * port,const union power_supply_propval * val)6376 static int tcpm_psy_set_online(struct tcpm_port *port,
6377 const union power_supply_propval *val)
6378 {
6379 int ret;
6380
6381 switch (val->intval) {
6382 case TCPM_PSY_FIXED_ONLINE:
6383 ret = tcpm_pps_activate(port, false);
6384 break;
6385 case TCPM_PSY_PROG_ONLINE:
6386 ret = tcpm_pps_activate(port, true);
6387 break;
6388 default:
6389 ret = -EINVAL;
6390 break;
6391 }
6392
6393 return ret;
6394 }
6395
tcpm_psy_set_prop(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)6396 static int tcpm_psy_set_prop(struct power_supply *psy,
6397 enum power_supply_property psp,
6398 const union power_supply_propval *val)
6399 {
6400 struct tcpm_port *port = power_supply_get_drvdata(psy);
6401 int ret;
6402
6403 /*
6404 * All the properties below are related to USB PD. The check needs to be
6405 * property specific when a non-pd related property is added.
6406 */
6407 if (!port->pd_supported)
6408 return -EOPNOTSUPP;
6409
6410 switch (psp) {
6411 case POWER_SUPPLY_PROP_ONLINE:
6412 ret = tcpm_psy_set_online(port, val);
6413 break;
6414 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
6415 ret = tcpm_pps_set_out_volt(port, val->intval / 1000);
6416 break;
6417 case POWER_SUPPLY_PROP_CURRENT_NOW:
6418 if (val->intval > port->pps_data.max_curr * 1000)
6419 ret = -EINVAL;
6420 else
6421 ret = tcpm_pps_set_op_curr(port, val->intval / 1000);
6422 break;
6423 default:
6424 ret = -EINVAL;
6425 break;
6426 }
6427 power_supply_changed(port->psy);
6428 return ret;
6429 }
6430
tcpm_psy_prop_writeable(struct power_supply * psy,enum power_supply_property psp)6431 static int tcpm_psy_prop_writeable(struct power_supply *psy,
6432 enum power_supply_property psp)
6433 {
6434 switch (psp) {
6435 case POWER_SUPPLY_PROP_ONLINE:
6436 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
6437 case POWER_SUPPLY_PROP_CURRENT_NOW:
6438 return 1;
6439 default:
6440 return 0;
6441 }
6442 }
6443
6444 static enum power_supply_usb_type tcpm_psy_usb_types[] = {
6445 POWER_SUPPLY_USB_TYPE_C,
6446 POWER_SUPPLY_USB_TYPE_PD,
6447 POWER_SUPPLY_USB_TYPE_PD_PPS,
6448 };
6449
6450 static const char *tcpm_psy_name_prefix = "tcpm-source-psy-";
6451
devm_tcpm_psy_register(struct tcpm_port * port)6452 static int devm_tcpm_psy_register(struct tcpm_port *port)
6453 {
6454 struct power_supply_config psy_cfg = {};
6455 const char *port_dev_name = dev_name(port->dev);
6456 size_t psy_name_len = strlen(tcpm_psy_name_prefix) +
6457 strlen(port_dev_name) + 1;
6458 char *psy_name;
6459
6460 psy_cfg.drv_data = port;
6461 psy_cfg.fwnode = dev_fwnode(port->dev);
6462 psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL);
6463 if (!psy_name)
6464 return -ENOMEM;
6465
6466 snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix,
6467 port_dev_name);
6468 port->psy_desc.name = psy_name;
6469 port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
6470 port->psy_desc.usb_types = tcpm_psy_usb_types;
6471 port->psy_desc.num_usb_types = ARRAY_SIZE(tcpm_psy_usb_types);
6472 port->psy_desc.properties = tcpm_psy_props;
6473 port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props);
6474 port->psy_desc.get_property = tcpm_psy_get_prop;
6475 port->psy_desc.set_property = tcpm_psy_set_prop;
6476 port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable;
6477
6478 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
6479
6480 port->psy = devm_power_supply_register(port->dev, &port->psy_desc,
6481 &psy_cfg);
6482
6483 return PTR_ERR_OR_ZERO(port->psy);
6484 }
6485
state_machine_timer_handler(struct hrtimer * timer)6486 static enum hrtimer_restart state_machine_timer_handler(struct hrtimer *timer)
6487 {
6488 struct tcpm_port *port = container_of(timer, struct tcpm_port, state_machine_timer);
6489
6490 if (port->registered)
6491 kthread_queue_work(port->wq, &port->state_machine);
6492 return HRTIMER_NORESTART;
6493 }
6494
vdm_state_machine_timer_handler(struct hrtimer * timer)6495 static enum hrtimer_restart vdm_state_machine_timer_handler(struct hrtimer *timer)
6496 {
6497 struct tcpm_port *port = container_of(timer, struct tcpm_port, vdm_state_machine_timer);
6498
6499 if (port->registered)
6500 kthread_queue_work(port->wq, &port->vdm_state_machine);
6501 return HRTIMER_NORESTART;
6502 }
6503
enable_frs_timer_handler(struct hrtimer * timer)6504 static enum hrtimer_restart enable_frs_timer_handler(struct hrtimer *timer)
6505 {
6506 struct tcpm_port *port = container_of(timer, struct tcpm_port, enable_frs_timer);
6507
6508 if (port->registered)
6509 kthread_queue_work(port->wq, &port->enable_frs);
6510 return HRTIMER_NORESTART;
6511 }
6512
send_discover_timer_handler(struct hrtimer * timer)6513 static enum hrtimer_restart send_discover_timer_handler(struct hrtimer *timer)
6514 {
6515 struct tcpm_port *port = container_of(timer, struct tcpm_port, send_discover_timer);
6516
6517 if (port->registered)
6518 kthread_queue_work(port->wq, &port->send_discover_work);
6519 return HRTIMER_NORESTART;
6520 }
6521
tcpm_register_port(struct device * dev,struct tcpc_dev * tcpc)6522 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
6523 {
6524 struct tcpm_port *port;
6525 int err;
6526
6527 if (!dev || !tcpc ||
6528 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
6529 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus ||
6530 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
6531 return ERR_PTR(-EINVAL);
6532
6533 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
6534 if (!port)
6535 return ERR_PTR(-ENOMEM);
6536
6537 port->dev = dev;
6538 port->tcpc = tcpc;
6539
6540 mutex_init(&port->lock);
6541 mutex_init(&port->swap_lock);
6542
6543 port->wq = kthread_create_worker(0, dev_name(dev));
6544 if (IS_ERR(port->wq))
6545 return ERR_CAST(port->wq);
6546 sched_set_fifo(port->wq->task);
6547
6548 kthread_init_work(&port->state_machine, tcpm_state_machine_work);
6549 kthread_init_work(&port->vdm_state_machine, vdm_state_machine_work);
6550 kthread_init_work(&port->event_work, tcpm_pd_event_handler);
6551 kthread_init_work(&port->enable_frs, tcpm_enable_frs_work);
6552 kthread_init_work(&port->send_discover_work, tcpm_send_discover_work);
6553 hrtimer_init(&port->state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6554 port->state_machine_timer.function = state_machine_timer_handler;
6555 hrtimer_init(&port->vdm_state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6556 port->vdm_state_machine_timer.function = vdm_state_machine_timer_handler;
6557 hrtimer_init(&port->enable_frs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6558 port->enable_frs_timer.function = enable_frs_timer_handler;
6559 hrtimer_init(&port->send_discover_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6560 port->send_discover_timer.function = send_discover_timer_handler;
6561
6562 spin_lock_init(&port->pd_event_lock);
6563
6564 init_completion(&port->tx_complete);
6565 init_completion(&port->swap_complete);
6566 init_completion(&port->pps_complete);
6567 tcpm_debugfs_init(port);
6568
6569 err = tcpm_fw_get_caps(port, tcpc->fwnode);
6570 if (err < 0)
6571 goto out_destroy_wq;
6572
6573 port->try_role = port->typec_caps.prefer_role;
6574
6575 port->typec_caps.fwnode = tcpc->fwnode;
6576 port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */
6577 port->typec_caps.pd_revision = 0x0300; /* USB-PD spec release 3.0 */
6578 port->typec_caps.svdm_version = SVDM_VER_2_0;
6579 port->typec_caps.driver_data = port;
6580 port->typec_caps.ops = &tcpm_ops;
6581 port->typec_caps.orientation_aware = 1;
6582
6583 port->partner_desc.identity = &port->partner_ident;
6584 port->port_type = port->typec_caps.type;
6585
6586 port->role_sw = usb_role_switch_get(port->dev);
6587 if (!port->role_sw)
6588 port->role_sw = fwnode_usb_role_switch_get(tcpc->fwnode);
6589 if (IS_ERR(port->role_sw)) {
6590 err = PTR_ERR(port->role_sw);
6591 goto out_destroy_wq;
6592 }
6593
6594 err = devm_tcpm_psy_register(port);
6595 if (err)
6596 goto out_role_sw_put;
6597 power_supply_changed(port->psy);
6598
6599 err = tcpm_port_register_pd(port);
6600 if (err)
6601 goto out_role_sw_put;
6602
6603 port->typec_caps.pd = port->pd;
6604
6605 port->typec_port = typec_register_port(port->dev, &port->typec_caps);
6606 if (IS_ERR(port->typec_port)) {
6607 err = PTR_ERR(port->typec_port);
6608 goto out_unregister_pd;
6609 }
6610
6611 typec_port_register_altmodes(port->typec_port,
6612 &tcpm_altmode_ops, port,
6613 port->port_altmode, ALTMODE_DISCOVERY_MAX);
6614 port->registered = true;
6615
6616 mutex_lock(&port->lock);
6617 tcpm_init(port);
6618 mutex_unlock(&port->lock);
6619
6620 tcpm_log(port, "%s: registered", dev_name(dev));
6621 return port;
6622
6623 out_unregister_pd:
6624 tcpm_port_unregister_pd(port);
6625 out_role_sw_put:
6626 usb_role_switch_put(port->role_sw);
6627 out_destroy_wq:
6628 tcpm_debugfs_exit(port);
6629 kthread_destroy_worker(port->wq);
6630 return ERR_PTR(err);
6631 }
6632 EXPORT_SYMBOL_GPL(tcpm_register_port);
6633
tcpm_unregister_port(struct tcpm_port * port)6634 void tcpm_unregister_port(struct tcpm_port *port)
6635 {
6636 int i;
6637
6638 port->registered = false;
6639 kthread_destroy_worker(port->wq);
6640
6641 hrtimer_cancel(&port->send_discover_timer);
6642 hrtimer_cancel(&port->enable_frs_timer);
6643 hrtimer_cancel(&port->vdm_state_machine_timer);
6644 hrtimer_cancel(&port->state_machine_timer);
6645
6646 tcpm_reset_port(port);
6647
6648 tcpm_port_unregister_pd(port);
6649
6650 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
6651 typec_unregister_altmode(port->port_altmode[i]);
6652 typec_unregister_port(port->typec_port);
6653 usb_role_switch_put(port->role_sw);
6654 tcpm_debugfs_exit(port);
6655 }
6656 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
6657
6658 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
6659 MODULE_DESCRIPTION("USB Type-C Port Manager");
6660 MODULE_LICENSE("GPL");
6661