1 /*
2  * Copyright (c) 2022 The Chromium OS Authors
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SUBSYS_USBC_STACK_PRIV_H_
8 #define ZEPHYR_SUBSYS_USBC_STACK_PRIV_H_
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/usb_c/usbc.h>
12 
13 #include "usbc_tc_common_internal.h"
14 #include "usbc_pe_common_internal.h"
15 #include "usbc_prl.h"
16 
17 #define PRIV_PORT_REQUEST_SUSPEND -1
18 #define PRIV_PORT_REQUEST_START	  -2
19 
20 /**
21  * @brief Each layer of the stack is composed of state machines that can be
22  *	  in one of the following states.
23  */
24 enum usbc_sm_state {
25 	/** The state machine is paused */
26 	SM_PAUSED,
27 	/** The state machine is initializing */
28 	SM_INIT,
29 	/** The state machine is running */
30 	SM_RUN
31 };
32 
33 /**
34  * @brief Port config
35  */
36 struct usbc_port_config {
37 	/**
38 	 * The usbc stack initializes this pointer that creates the
39 	 * main thread for this port
40 	 */
41 	void (*create_thread)(const struct device *dev);
42 	/** The thread stack for this port's thread */
43 	k_thread_stack_t *stack;
44 };
45 
46 /**
47  * @brief Request FIFO
48  */
49 struct request_value {
50 	/** First word is reserved for use by FIFO */
51 	void *fifo_reserved;
52 	/** Request value */
53 	int32_t val;
54 };
55 
56 /**
57  * @brief Port data
58  */
59 struct usbc_port_data {
60 	/** This port's thread */
61 	k_tid_t port_thread;
62 	/** This port thread's data */
63 	struct k_thread thread_data;
64 
65 	/* Type-C layer data */
66 
67 	/** Type-C state machine object */
68 	struct tc_sm_t *tc;
69 	/** Enables or Disables the Type-C state machine */
70 	bool tc_enabled;
71 	/** The state of the Type-C state machine */
72 	enum usbc_sm_state tc_sm_state;
73 
74 	/* Policy Engine layer data */
75 
76 	/** Policy Engine state machine object */
77 	struct policy_engine *pe;
78 	/** Enables or Disables the Policy Engine state machine */
79 	bool pe_enabled;
80 	/** The state of the Policy Engine state machine */
81 	enum usbc_sm_state pe_sm_state;
82 
83 	/* Protocol Layer data */
84 
85 	/** Protocol Receive Layer state machine object */
86 	struct protocol_layer_rx_t *prl_rx;
87 	/** Protocol Transmit Layer state machine object */
88 	struct protocol_layer_tx_t *prl_tx;
89 	/** Protocol Hard Reset Layer state machine object */
90 	struct protocol_hard_reset_t *prl_hr;
91 	/** Enables or Disables the Protocol Layer state machine */
92 	bool prl_enabled;
93 	/** The state of the Protocol Layer state machine */
94 	enum usbc_sm_state prl_sm_state;
95 
96 	/* Common data for all layers */
97 
98 	/** Power Delivery revisions for each packet type */
99 	enum pd_rev_type rev[NUM_SOP_STAR_TYPES];
100 	/** The Type-C Port Controller on this port */
101 	const struct device *tcpc;
102 	/** VBUS Measurement and control device on this port */
103 	const struct device *vbus;
104 	/** Power Path Controller device on this port */
105 	const struct device *ppc;
106 
107 	/** Device Policy Manager Request FIFO */
108 	struct k_fifo request_fifo;
109 	/** Device Policy manager Request */
110 	struct request_value request;
111 
112 	/** Bypass next sleep and request one more iteration of the USB-C state machines */
113 	bool bypass_next_sleep;
114 
115 	/* USB-C Callbacks */
116 
117 	/**
118 	 * Callback used by the Policy Engine to ask the Device Policy Manager
119 	 * if a particular policy should be allowed
120 	 */
121 	bool (*policy_cb_check)(const struct device *dev,
122 				const enum usbc_policy_check_t policy_check);
123 	/**
124 	 * Callback used by the Policy Engine to notify the Device Policy
125 	 * Manager of a policy change
126 	 */
127 	void (*policy_cb_notify)(const struct device *dev,
128 				 const enum usbc_policy_notify_t policy_notify);
129 	/**
130 	 * Callback used by the Policy Engine to notify the Device Policy
131 	 * Manager of WAIT message reception
132 	 */
133 	bool (*policy_cb_wait_notify)(const struct device *dev,
134 				      const enum usbc_policy_wait_t policy_notify);
135 
136 #ifdef CONFIG_USBC_CSM_SINK_ONLY
137 	/**
138 	 * Callback used by the Policy Engine to get the Sink Capabilities
139 	 * from the Device Policy Manager
140 	 */
141 	int (*policy_cb_get_snk_cap)(const struct device *dev, uint32_t **pdos, int *num_pdos);
142 
143 	/**
144 	 * Callback used by the Policy Engine to send the received Source
145 	 * Capabilities to the Device Policy Manager
146 	 */
147 	void (*policy_cb_set_src_cap)(const struct device *dev, const uint32_t *pdos,
148 				      const int num_pdos);
149 	/**
150 	 * Callback used by the Policy Engine to get the Request Data Object
151 	 * (RDO) from the Device Policy Manager
152 	 */
153 	uint32_t (*policy_cb_get_rdo)(const struct device *dev);
154 
155 	/**
156 	 * Callback used by the Policy Engine to check if Sink Power Supply
157 	 * is at default level
158 	 */
159 	bool (*policy_cb_is_snk_at_default)(const struct device *dev);
160 #else /* CONFIG_USBC_CSM_SOURCE_ONLY */
161 	/**
162 	 * Callback used by the Policy Engine get the Rp pull-up that should
163 	 * be placed on the CC lines
164 	 */
165 	int (*policy_cb_get_src_rp)(const struct device *dev,
166 				    enum tc_rp_value *rp);
167 
168 	/**
169 	 * Callback used by the Policy Engine to enable and disable the
170 	 * Source Power Supply
171 	 */
172 	int (*policy_cb_src_en)(const struct device *dev, bool en);
173 
174 	/**
175 	 * Callback used by the Policy Engine to get the Source Caps that
176 	 * will be sent to the Sink
177 	 */
178 	int (*policy_cb_get_src_caps)(const struct device *dev,
179 				     const uint32_t **pdos,
180 				     uint32_t *num_pdos);
181 
182 	/**
183 	 * Callback used by the Policy Engine to check if the Sink's request
184 	 * is valid
185 	 */
186 	enum usbc_snk_req_reply_t (*policy_cb_check_sink_request)(const struct device *dev,
187 					     const uint32_t request_msg);
188 
189 	/**
190 	 * Callback used by the Policy Engine to check if the Present Contract
191 	 * is still valid
192 	 */
193 	bool (*policy_present_contract_is_valid)(const struct device *dev,
194 						const uint32_t present_contract);
195 
196 	/**
197 	 * Callback used by the Policy Engine to check if the Source Power Supply
198 	 * is ready
199 	 */
200 	bool (*policy_is_ps_ready)(const struct device *dev);
201 
202 	/**
203 	 * Callback used by the Policy Engine to request that a different set of
204 	 * Source Caps be used
205 	 */
206 	bool (*policy_change_src_caps)(const struct device *dev);
207 
208 	/**
209 	 * Callback used by the Policy Engine to store the Sink's Capabilities
210 	 */
211 	void (*policy_cb_set_port_partner_snk_cap)(const struct device *dev,
212 					const uint32_t *pdos,
213 					const int num_pdos);
214 #endif /* CONFIG_USBC_CSM_SINK_ONLY */
215 	/** Device Policy Manager data */
216 	void *dpm_data;
217 };
218 
219 #ifdef CONFIG_USBC_CSM_SOURCE_ONLY
220 /**
221  * @brief Function that enables the source path either using callback or by the TCPC.
222  * If source and sink paths are controlled by the TCPC, this callback doesn't have to be set.
223  *
224  * @param dev USB-C connector device
225  * @param tcpc Type-C Port Controller device
226  * @param en True to enable the sourcing, false to disable
227  * @return int 0 if success, -ENOSYS if both callback and TCPC function are not implemented.
228  *             In case of error, value from any of the functions is returned
229  */
usbc_policy_src_en(const struct device * dev,const struct device * tcpc,bool en)230 static inline int usbc_policy_src_en(const struct device *dev, const struct device *tcpc, bool en)
231 {
232 	struct usbc_port_data *data = dev->data;
233 	int ret_cb = -ENOSYS;
234 	int ret_tcpc;
235 
236 	if (data->policy_cb_src_en != NULL) {
237 		ret_cb = data->policy_cb_src_en(dev, en);
238 		if (ret_cb != 0 && ret_cb != -ENOSYS) {
239 			return ret_cb;
240 		}
241 	}
242 
243 	ret_tcpc = tcpc_set_src_ctrl(tcpc, en);
244 	if (ret_tcpc == -ENOSYS) {
245 		return ret_cb;
246 	}
247 
248 	return ret_tcpc;
249 }
250 #endif
251 
252 #endif /* ZEPHYR_SUBSYS_USBC_STACK_PRIV_H_ */
253