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 
105 	/** Device Policy Manager Request FIFO */
106 	struct k_fifo request_fifo;
107 	/** Device Policy manager Request */
108 	struct request_value request;
109 
110 	/* USB-C Callbacks */
111 
112 	/**
113 	 * Callback used by the Policy Engine to ask the Device Policy Manager
114 	 * if a particular policy should be allowed
115 	 */
116 	bool (*policy_cb_check)(const struct device *dev,
117 				const enum usbc_policy_check_t policy_check);
118 	/**
119 	 * Callback used by the Policy Engine to notify the Device Policy
120 	 * Manager of a policy change
121 	 */
122 	void (*policy_cb_notify)(const struct device *dev,
123 				 const enum usbc_policy_notify_t policy_notify);
124 	/**
125 	 * Callback used by the Policy Engine to notify the Device Policy
126 	 * Manager of WAIT message reception
127 	 */
128 	bool (*policy_cb_wait_notify)(const struct device *dev,
129 				      const enum usbc_policy_wait_t policy_notify);
130 
131 #ifdef CONFIG_USBC_CSM_SINK_ONLY
132 	/**
133 	 * Callback used by the Policy Engine to get the Sink Capabilities
134 	 * from the Device Policy Manager
135 	 */
136 	int (*policy_cb_get_snk_cap)(const struct device *dev, uint32_t **pdos, int *num_pdos);
137 
138 	/**
139 	 * Callback used by the Policy Engine to send the received Source
140 	 * Capabilities to the Device Policy Manager
141 	 */
142 	void (*policy_cb_set_src_cap)(const struct device *dev, const uint32_t *pdos,
143 				      const int num_pdos);
144 	/**
145 	 * Callback used by the Policy Engine to get the Request Data Object
146 	 * (RDO) from the Device Policy Manager
147 	 */
148 	uint32_t (*policy_cb_get_rdo)(const struct device *dev);
149 
150 	/**
151 	 * Callback used by the Policy Engine to check if Sink Power Supply
152 	 * is at default level
153 	 */
154 	bool (*policy_cb_is_snk_at_default)(const struct device *dev);
155 #else /* CONFIG_USBC_CSM_SOURCE_ONLY */
156 	/**
157 	 * Callback used by the Policy Engine get the Rp pull-up that should
158 	 * be placed on the CC lines
159 	 */
160 	int (*policy_cb_get_src_rp)(const struct device *dev,
161 				    enum tc_rp_value *rp);
162 
163 	/**
164 	 * Callback used by the Policy Engine to enable and disable the
165 	 * Source Power Supply
166 	 */
167 	int (*policy_cb_src_en)(const struct device *dev, bool en);
168 
169 	/**
170 	 * Callback used by the Policy Engine to get the Source Caps that
171 	 * will be sent to the Sink
172 	 */
173 	int (*policy_cb_get_src_caps)(const struct device *dev,
174 				     const uint32_t **pdos,
175 				     uint32_t *num_pdos);
176 
177 	/**
178 	 * Callback used by the Policy Engine to check if the Sink's request
179 	 * is valid
180 	 */
181 	enum usbc_snk_req_reply_t (*policy_cb_check_sink_request)(const struct device *dev,
182 					     const uint32_t request_msg);
183 
184 	/**
185 	 * Callback used by the Policy Engine to check if the Present Contract
186 	 * is still valid
187 	 */
188 	bool (*policy_present_contract_is_valid)(const struct device *dev,
189 						const uint32_t present_contract);
190 
191 	/**
192 	 * Callback used by the Policy Engine to check if the Source Power Supply
193 	 * is ready
194 	 */
195 	bool (*policy_is_ps_ready)(const struct device *dev);
196 
197 	/**
198 	 * Callback used by the Policy Engine to request that a different set of
199 	 * Source Caps be used
200 	 */
201 	bool (*policy_change_src_caps)(const struct device *dev);
202 
203 	/**
204 	 * Callback used by the Policy Engine to store the Sink's Capabilities
205 	 */
206 	void (*policy_cb_set_port_partner_snk_cap)(const struct device *dev,
207 					const uint32_t *pdos,
208 					const int num_pdos);
209 #endif /* CONFIG_USBC_CSM_SINK_ONLY */
210 	/** Device Policy Manager data */
211 	void *dpm_data;
212 };
213 
214 #endif /* ZEPHYR_SUBSYS_USBC_STACK_PRIV_H_ */
215