PD Buddy Sink Firmware
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

policy_engine.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "policy_engine.h"
  19. #include <stdbool.h>
  20. #include "messages.h"
  21. #include "priorities.h"
  22. #include "device_policy_manager.h"
  23. #include "protocol_tx.h"
  24. #include "hard_reset.h"
  25. #include "fusb302b.h"
  26. #include "pd.h"
  27. thread_t *pdb_pe_thread;
  28. enum policy_engine_state {
  29. PESinkStartup,
  30. PESinkDiscovery,
  31. PESinkWaitCap,
  32. PESinkEvalCap,
  33. PESinkSelectCap,
  34. PESinkTransitionSink,
  35. PESinkReady,
  36. PESinkGetSourceCap,
  37. PESinkGiveSinkCap,
  38. PESinkHardReset,
  39. PESinkTransitionDefault,
  40. PESinkSoftReset,
  41. PESinkSendSoftReset,
  42. PESinkSendReject,
  43. PESinkSourceUnresponsive
  44. };
  45. /* The received message we're currently working with */
  46. static union pd_msg *policy_engine_message = NULL;
  47. /* The most recent Request from the DPM */
  48. static union pd_msg *last_dpm_request = NULL;
  49. /* Whether or not the source capabilities match our required power */
  50. static bool capability_match = false;
  51. /* Whether or not we have an explicit contract */
  52. static bool explicit_contract = false;
  53. /* Whether or not we're receiving minimum power*/
  54. static bool min_power = false;
  55. /* Keep track of how many hard resets we've sent */
  56. static int hard_reset_counter = 0;
  57. /* Policy Engine thread mailbox */
  58. static msg_t pdb_pe_mailbox_queue[PDB_MSG_POOL_SIZE];
  59. mailbox_t pdb_pe_mailbox;
  60. static enum policy_engine_state pe_sink_startup(void)
  61. {
  62. /* We don't have an explicit contract currently */
  63. explicit_contract = false;
  64. /* Tell the DPM that we've started negotiations */
  65. pdb_dpm_pd_start();
  66. /* No need to reset the protocol layer here. There are two ways into this
  67. * state: startup and exiting hard reset. On startup, the protocol layer
  68. * is reset by the startup procedure. When exiting hard reset, the
  69. * protocol layer is reset by the hard reset state machine. Since it's
  70. * already done somewhere else, there's no need to do it again here. */
  71. return PESinkDiscovery;
  72. }
  73. static enum policy_engine_state pe_sink_discovery(void)
  74. {
  75. /* Wait for VBUS. Since it's our only power source, we already know that
  76. * we have it, so just move on. */
  77. return PESinkWaitCap;
  78. }
  79. static enum policy_engine_state pe_sink_wait_cap(void)
  80. {
  81. /* Fetch a message from the protocol layer */
  82. eventmask_t evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX
  83. | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_RESET, PD_T_TYPEC_SINK_WAIT_CAP);
  84. /* If we timed out waiting for Source_Capabilities, send a hard reset */
  85. if (evt == 0) {
  86. return PESinkHardReset;
  87. }
  88. /* If we got reset signaling, transition to default */
  89. if (evt & PDB_EVT_PE_RESET) {
  90. return PESinkTransitionDefault;
  91. }
  92. /* If we're too hot, we shouldn't negotiate power yet */
  93. if (evt & PDB_EVT_PE_I_OVRTEMP) {
  94. return PESinkWaitCap;
  95. }
  96. /* If we got a message */
  97. if (evt & PDB_EVT_PE_MSG_RX) {
  98. /* Get the message */
  99. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  100. /* If we got a Source_Capabilities message, read it. */
  101. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  102. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  103. return PESinkEvalCap;
  104. /* If the message was a Soft_Reset, do the soft reset procedure */
  105. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  106. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  107. chPoolFree(&pdb_msg_pool, policy_engine_message);
  108. policy_engine_message = NULL;
  109. return PESinkSoftReset;
  110. /* If we got an unexpected message, reset */
  111. } else {
  112. /* Free the received message */
  113. chPoolFree(&pdb_msg_pool, policy_engine_message);
  114. return PESinkHardReset;
  115. }
  116. }
  117. }
  118. /* If we failed to get a message, send a hard reset */
  119. return PESinkHardReset;
  120. }
  121. static enum policy_engine_state pe_sink_eval_cap(void)
  122. {
  123. /* Get a message object for the request if we don't have one already */
  124. if (last_dpm_request == NULL) {
  125. last_dpm_request = chPoolAlloc(&pdb_msg_pool);
  126. }
  127. /* Ask the DPM what to request */
  128. capability_match = pdb_dpm_evaluate_capability(policy_engine_message,
  129. last_dpm_request);
  130. /* It's up to the DPM to free the Source_Capabilities message, which it can
  131. * do whenever it sees fit. Just remove our reference to it since we won't
  132. * know when it's no longer valid. */
  133. policy_engine_message = NULL;
  134. return PESinkSelectCap;
  135. }
  136. static enum policy_engine_state pe_sink_select_cap(void)
  137. {
  138. /* Transmit the request */
  139. chMBPost(&pdb_prltx_mailbox, (msg_t) last_dpm_request, TIME_IMMEDIATE);
  140. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  141. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  142. | PDB_EVT_PE_RESET);
  143. /* Don't free the request; we might need it again */
  144. /* If we got reset signaling, transition to default */
  145. if (evt & PDB_EVT_PE_RESET) {
  146. return PESinkTransitionDefault;
  147. }
  148. /* If the message transmission failed, send a hard reset */
  149. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  150. return PESinkHardReset;
  151. }
  152. /* Wait for a response */
  153. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET,
  154. PD_T_SENDER_RESPONSE);
  155. /* If we got reset signaling, transition to default */
  156. if (evt & PDB_EVT_PE_RESET) {
  157. return PESinkTransitionDefault;
  158. }
  159. /* If we didn't get a response before the timeout, send a hard reset */
  160. if (evt == 0) {
  161. return PESinkHardReset;
  162. }
  163. /* Get the response message */
  164. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  165. /* If the source accepted our request, wait for the new power */
  166. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  167. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  168. /* Transition to Sink Standby if necessary */
  169. pdb_dpm_sink_standby();
  170. min_power = false;
  171. chPoolFree(&pdb_msg_pool, policy_engine_message);
  172. policy_engine_message = NULL;
  173. return PESinkTransitionSink;
  174. /* If the message was a Soft_Reset, do the soft reset procedure */
  175. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  176. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  177. chPoolFree(&pdb_msg_pool, policy_engine_message);
  178. policy_engine_message = NULL;
  179. return PESinkSoftReset;
  180. /* If the message was Wait or Reject */
  181. } else if ((PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REJECT
  182. || PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT)
  183. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  184. /* If we don't have an explicit contract, wait for capabilities */
  185. if (!explicit_contract) {
  186. chPoolFree(&pdb_msg_pool, policy_engine_message);
  187. policy_engine_message = NULL;
  188. return PESinkWaitCap;
  189. /* If we do have an explicit contract, go to the ready state */
  190. } else {
  191. /* If we got here from a Wait message, we Should run
  192. * SinkRequestTimer in the Ready state. */
  193. min_power = (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT);
  194. chPoolFree(&pdb_msg_pool, policy_engine_message);
  195. policy_engine_message = NULL;
  196. return PESinkReady;
  197. }
  198. } else {
  199. chPoolFree(&pdb_msg_pool, policy_engine_message);
  200. policy_engine_message = NULL;
  201. return PESinkSendSoftReset;
  202. }
  203. }
  204. return PESinkHardReset;
  205. }
  206. static enum policy_engine_state pe_sink_transition_sink(void)
  207. {
  208. /* Wait for the PS_RDY message */
  209. eventmask_t evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET,
  210. PD_T_PS_TRANSITION);
  211. /* If we got reset signaling, transition to default */
  212. if (evt & PDB_EVT_PE_RESET) {
  213. return PESinkTransitionDefault;
  214. }
  215. /* If no message was received, send a hard reset */
  216. if (evt == 0) {
  217. return PESinkHardReset;
  218. }
  219. /* If we received a message, read it */
  220. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  221. /* If we got a PS_RDY, handle it */
  222. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PS_RDY
  223. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  224. /* We just finished negotiating an explicit contract */
  225. explicit_contract = true;
  226. /* Set the output appropriately */
  227. pdb_dpm_output_set(capability_match && !min_power);
  228. chPoolFree(&pdb_msg_pool, policy_engine_message);
  229. policy_engine_message = NULL;
  230. return PESinkReady;
  231. /* If there was a protocol error, send a hard reset */
  232. } else {
  233. /* Turn off the power output before this hard reset to make sure we
  234. * don't supply an incorrect voltage to the device we're powering.
  235. */
  236. pdb_dpm_output_set(false);
  237. chPoolFree(&pdb_msg_pool, policy_engine_message);
  238. policy_engine_message = NULL;
  239. return PESinkHardReset;
  240. }
  241. }
  242. return PESinkHardReset;
  243. }
  244. static enum policy_engine_state pe_sink_ready(void)
  245. {
  246. eventmask_t evt;
  247. /* Wait for an event */
  248. if (min_power) {
  249. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
  250. | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP
  251. | PDB_EVT_PE_NEW_POWER, PD_T_SINK_REQUEST);
  252. } else {
  253. evt = chEvtWaitAny(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
  254. | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP
  255. | PDB_EVT_PE_NEW_POWER);
  256. }
  257. /* If we got reset signaling, transition to default */
  258. if (evt & PDB_EVT_PE_RESET) {
  259. return PESinkTransitionDefault;
  260. }
  261. /* If we overheated, send a hard reset */
  262. if (evt & PDB_EVT_PE_I_OVRTEMP) {
  263. return PESinkHardReset;
  264. }
  265. /* If the DPM wants us to, send a Get_Source_Cap message */
  266. if (evt & PDB_EVT_PE_GET_SOURCE_CAP) {
  267. return PESinkGetSourceCap;
  268. }
  269. /* If the DPM wants new power, let it figure out what power it wants
  270. * exactly. This isn't exactly the transition from the spec (that would be
  271. * SelectCap, not EvalCap), but this works better with the particular
  272. * design of this firmware. */
  273. if (evt & PDB_EVT_PE_NEW_POWER) {
  274. /* Make sure we're evaluating NULL capabilities to use the old ones */
  275. if (policy_engine_message != NULL) {
  276. chPoolFree(&pdb_msg_pool, policy_engine_message);
  277. policy_engine_message = NULL;
  278. }
  279. return PESinkEvalCap;
  280. }
  281. /* If no event was received, the timer ran out. */
  282. if (evt == 0) {
  283. /* Repeat our Request message */
  284. return PESinkSelectCap;
  285. }
  286. /* If we received a message */
  287. if (evt & PDB_EVT_PE_MSG_RX) {
  288. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  289. /* Ignore vendor-defined messages */
  290. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VENDOR_DEFINED
  291. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  292. chPoolFree(&pdb_msg_pool, policy_engine_message);
  293. policy_engine_message = NULL;
  294. return PESinkReady;
  295. /* Ignore Ping messages */
  296. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PING
  297. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  298. chPoolFree(&pdb_msg_pool, policy_engine_message);
  299. policy_engine_message = NULL;
  300. return PESinkReady;
  301. /* Reject DR_Swap messages */
  302. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_DR_SWAP
  303. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  304. chPoolFree(&pdb_msg_pool, policy_engine_message);
  305. policy_engine_message = NULL;
  306. return PESinkSendReject;
  307. /* Reject Get_Source_Cap messages */
  308. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SOURCE_CAP
  309. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  310. chPoolFree(&pdb_msg_pool, policy_engine_message);
  311. policy_engine_message = NULL;
  312. return PESinkSendReject;
  313. /* Reject PR_Swap messages */
  314. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PR_SWAP
  315. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  316. chPoolFree(&pdb_msg_pool, policy_engine_message);
  317. policy_engine_message = NULL;
  318. return PESinkSendReject;
  319. /* Reject VCONN_Swap messages */
  320. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VCONN_SWAP
  321. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  322. chPoolFree(&pdb_msg_pool, policy_engine_message);
  323. policy_engine_message = NULL;
  324. return PESinkSendReject;
  325. /* Reject Request messages */
  326. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REQUEST
  327. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  328. chPoolFree(&pdb_msg_pool, policy_engine_message);
  329. policy_engine_message = NULL;
  330. return PESinkSendReject;
  331. /* Reject Sink_Capabilities messages */
  332. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SINK_CAPABILITIES
  333. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  334. chPoolFree(&pdb_msg_pool, policy_engine_message);
  335. policy_engine_message = NULL;
  336. return PESinkSendReject;
  337. /* Handle GotoMin messages */
  338. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GOTOMIN
  339. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  340. if (pdb_dpm_giveback_enabled()) {
  341. /* We support GiveBack, so go to minimum power */
  342. pdb_dpm_output_set(false);
  343. min_power = true;
  344. chPoolFree(&pdb_msg_pool, policy_engine_message);
  345. policy_engine_message = NULL;
  346. return PESinkTransitionSink;
  347. } else {
  348. /* We don't support GiveBack, so send a Reject */
  349. chPoolFree(&pdb_msg_pool, policy_engine_message);
  350. policy_engine_message = NULL;
  351. return PESinkSendReject;
  352. }
  353. /* Evaluate new Source_Capabilities */
  354. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  355. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  356. /* Don't free the message: we need to keep the
  357. * Source_Capabilities message so we can evaluate it. */
  358. return PESinkEvalCap;
  359. /* Give sink capabilities when asked */
  360. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SINK_CAP
  361. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  362. chPoolFree(&pdb_msg_pool, policy_engine_message);
  363. policy_engine_message = NULL;
  364. return PESinkGiveSinkCap;
  365. /* If the message was a Soft_Reset, do the soft reset procedure */
  366. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  367. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  368. chPoolFree(&pdb_msg_pool, policy_engine_message);
  369. policy_engine_message = NULL;
  370. return PESinkSoftReset;
  371. /* If we got an unknown message, send a soft reset */
  372. } else {
  373. chPoolFree(&pdb_msg_pool, policy_engine_message);
  374. policy_engine_message = NULL;
  375. return PESinkSendSoftReset;
  376. }
  377. }
  378. }
  379. return PESinkReady;
  380. }
  381. static enum policy_engine_state pe_sink_get_source_cap(void)
  382. {
  383. /* Get a message object */
  384. union pd_msg *get_source_cap = chPoolAlloc(&pdb_msg_pool);
  385. /* Make a Get_Source_Cap message */
  386. get_source_cap->hdr = PD_MSGTYPE_GET_SOURCE_CAP | PD_DATAROLE_UFP
  387. | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  388. /* Transmit the Get_Source_Cap */
  389. chMBPost(&pdb_prltx_mailbox, (msg_t) get_source_cap, TIME_IMMEDIATE);
  390. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  391. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  392. | PDB_EVT_PE_RESET);
  393. /* Free the sent message */
  394. chPoolFree(&pdb_msg_pool, get_source_cap);
  395. get_source_cap = NULL;
  396. /* If we got reset signaling, transition to default */
  397. if (evt & PDB_EVT_PE_RESET) {
  398. return PESinkTransitionDefault;
  399. }
  400. /* If the message transmission failed, send a hard reset */
  401. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  402. return PESinkHardReset;
  403. }
  404. return PESinkReady;
  405. }
  406. static enum policy_engine_state pe_sink_give_sink_cap(void)
  407. {
  408. /* Get a message object */
  409. union pd_msg *snk_cap = chPoolAlloc(&pdb_msg_pool);
  410. /* Get our capabilities from the DPM */
  411. pdb_dpm_get_sink_capability(snk_cap);
  412. /* Transmit our capabilities */
  413. chMBPost(&pdb_prltx_mailbox, (msg_t) snk_cap, TIME_IMMEDIATE);
  414. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  415. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  416. | PDB_EVT_PE_RESET);
  417. /* Free the Sink_Capabilities message */
  418. chPoolFree(&pdb_msg_pool, snk_cap);
  419. snk_cap = NULL;
  420. /* If we got reset signaling, transition to default */
  421. if (evt & PDB_EVT_PE_RESET) {
  422. return PESinkTransitionDefault;
  423. }
  424. /* If the message transmission failed, send a hard reset */
  425. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  426. return PESinkHardReset;
  427. }
  428. return PESinkReady;
  429. }
  430. static enum policy_engine_state pe_sink_hard_reset(void)
  431. {
  432. /* If we've already sent the maximum number of hard resets, assume the
  433. * source is unresponsive. */
  434. if (hard_reset_counter > PD_N_HARD_RESET_COUNT) {
  435. return PESinkSourceUnresponsive;
  436. }
  437. /* Generate a hard reset signal */
  438. chEvtSignal(pdb_hardrst_thread, PDB_EVT_HARDRST_RESET);
  439. chEvtWaitAny(PDB_EVT_PE_HARD_SENT);
  440. /* Increment HardResetCounter */
  441. hard_reset_counter++;
  442. return PESinkTransitionDefault;
  443. }
  444. static enum policy_engine_state pe_sink_transition_default(void)
  445. {
  446. explicit_contract = false;
  447. /* Tell the DPM to transition to default power */
  448. pdb_dpm_output_default();
  449. /* There is no local hardware to reset. */
  450. /* Since we never change our data role from UFP, there is no reason to set
  451. * it here. */
  452. /* Tell the protocol layer we're done with the reset */
  453. chEvtSignal(pdb_hardrst_thread, PDB_EVT_HARDRST_DONE);
  454. return PESinkStartup;
  455. }
  456. static enum policy_engine_state pe_sink_soft_reset(void)
  457. {
  458. /* No need to explicitly reset the protocol layer here. It resets itself
  459. * when a Soft_Reset message is received. */
  460. /* Get a message object */
  461. union pd_msg *accept = chPoolAlloc(&pdb_msg_pool);
  462. /* Make an Accept message */
  463. accept->hdr = PD_MSGTYPE_ACCEPT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  464. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  465. /* Transmit the Accept */
  466. chMBPost(&pdb_prltx_mailbox, (msg_t) accept, TIME_IMMEDIATE);
  467. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  468. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  469. | PDB_EVT_PE_RESET);
  470. /* Free the sent message */
  471. chPoolFree(&pdb_msg_pool, accept);
  472. accept = NULL;
  473. /* If we got reset signaling, transition to default */
  474. if (evt & PDB_EVT_PE_RESET) {
  475. return PESinkTransitionDefault;
  476. }
  477. /* If the message transmission failed, send a hard reset */
  478. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  479. return PESinkHardReset;
  480. }
  481. return PESinkWaitCap;
  482. }
  483. static enum policy_engine_state pe_sink_send_soft_reset(void)
  484. {
  485. /* No need to explicitly reset the protocol layer here. It resets itself
  486. * just before a Soft_Reset message is transmitted. */
  487. /* Get a message object */
  488. union pd_msg *softrst = chPoolAlloc(&pdb_msg_pool);
  489. /* Make a Soft_Reset message */
  490. softrst->hdr = PD_MSGTYPE_SOFT_RESET | PD_DATAROLE_UFP | PD_SPECREV_2_0
  491. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  492. /* Transmit the soft reset */
  493. chMBPost(&pdb_prltx_mailbox, (msg_t) softrst, TIME_IMMEDIATE);
  494. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  495. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  496. | PDB_EVT_PE_RESET);
  497. /* Free the sent message */
  498. chPoolFree(&pdb_msg_pool, softrst);
  499. softrst = NULL;
  500. /* If we got reset signaling, transition to default */
  501. if (evt & PDB_EVT_PE_RESET) {
  502. return PESinkTransitionDefault;
  503. }
  504. /* If the message transmission failed, send a hard reset */
  505. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  506. return PESinkHardReset;
  507. }
  508. /* Wait for a response */
  509. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET,
  510. PD_T_SENDER_RESPONSE);
  511. /* If we got reset signaling, transition to default */
  512. if (evt & PDB_EVT_PE_RESET) {
  513. return PESinkTransitionDefault;
  514. }
  515. /* If we didn't get a response before the timeout, send a hard reset */
  516. if (evt == 0) {
  517. return PESinkHardReset;
  518. }
  519. /* Get the response message */
  520. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  521. /* If the source accepted our soft reset, wait for capabilities. */
  522. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  523. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  524. chPoolFree(&pdb_msg_pool, policy_engine_message);
  525. policy_engine_message = NULL;
  526. return PESinkWaitCap;
  527. /* If the message was a Soft_Reset, do the soft reset procedure */
  528. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  529. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  530. chPoolFree(&pdb_msg_pool, policy_engine_message);
  531. policy_engine_message = NULL;
  532. return PESinkSoftReset;
  533. /* Otherwise, send a hard reset */
  534. } else {
  535. chPoolFree(&pdb_msg_pool, policy_engine_message);
  536. policy_engine_message = NULL;
  537. return PESinkHardReset;
  538. }
  539. }
  540. return PESinkHardReset;
  541. }
  542. static enum policy_engine_state pe_sink_send_reject(void)
  543. {
  544. /* Get a message object */
  545. union pd_msg *reject = chPoolAlloc(&pdb_msg_pool);
  546. /* Make a Reject message */
  547. reject->hdr = PD_MSGTYPE_REJECT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  548. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  549. /* Transmit the message */
  550. chMBPost(&pdb_prltx_mailbox, (msg_t) reject, TIME_IMMEDIATE);
  551. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  552. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  553. | PDB_EVT_PE_RESET);
  554. /* Free the Reject message */
  555. chPoolFree(&pdb_msg_pool, reject);
  556. reject = NULL;
  557. /* If we got reset signaling, transition to default */
  558. if (evt & PDB_EVT_PE_RESET) {
  559. return PESinkTransitionDefault;
  560. }
  561. /* If the message transmission failed, send a soft reset */
  562. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  563. return PESinkSendSoftReset;
  564. }
  565. return PESinkReady;
  566. }
  567. /*
  568. * When Power Delivery is unresponsive, fall back to Type-C Current
  569. */
  570. static enum policy_engine_state pe_sink_source_unresponsive(void)
  571. {
  572. static int old_tcc_match = -1;
  573. int tcc_match = pdb_dpm_evaluate_typec_current(fusb_get_typec_current());
  574. /* If the last two readings are the same, set the output */
  575. if (old_tcc_match == tcc_match) {
  576. pdb_dpm_output_set(tcc_match);
  577. }
  578. /* Remember whether or not the last measurement succeeded */
  579. old_tcc_match = tcc_match;
  580. /* Wait tPDDebounce between measurements */
  581. chThdSleep(PD_T_PD_DEBOUNCE);
  582. return PESinkSourceUnresponsive;
  583. }
  584. /*
  585. * Policy Engine state machine thread
  586. */
  587. static THD_WORKING_AREA(waPolicyEngine, 128);
  588. static THD_FUNCTION(PolicyEngine, arg) {
  589. (void) arg;
  590. enum policy_engine_state state = PESinkStartup;
  591. /* Initialize the mailbox */
  592. chMBObjectInit(&pdb_pe_mailbox, pdb_pe_mailbox_queue, PDB_MSG_POOL_SIZE);
  593. while (true) {
  594. switch (state) {
  595. case PESinkStartup:
  596. state = pe_sink_startup();
  597. break;
  598. case PESinkDiscovery:
  599. state = pe_sink_discovery();
  600. break;
  601. case PESinkWaitCap:
  602. state = pe_sink_wait_cap();
  603. break;
  604. case PESinkEvalCap:
  605. state = pe_sink_eval_cap();
  606. break;
  607. case PESinkSelectCap:
  608. state = pe_sink_select_cap();
  609. break;
  610. case PESinkTransitionSink:
  611. state = pe_sink_transition_sink();
  612. break;
  613. case PESinkReady:
  614. state = pe_sink_ready();
  615. break;
  616. case PESinkGetSourceCap:
  617. state = pe_sink_get_source_cap();
  618. break;
  619. case PESinkGiveSinkCap:
  620. state = pe_sink_give_sink_cap();
  621. break;
  622. case PESinkHardReset:
  623. state = pe_sink_hard_reset();
  624. break;
  625. case PESinkTransitionDefault:
  626. state = pe_sink_transition_default();
  627. break;
  628. case PESinkSoftReset:
  629. state = pe_sink_soft_reset();
  630. break;
  631. case PESinkSendSoftReset:
  632. state = pe_sink_send_soft_reset();
  633. break;
  634. case PESinkSendReject:
  635. state = pe_sink_send_reject();
  636. break;
  637. case PESinkSourceUnresponsive:
  638. state = pe_sink_source_unresponsive();
  639. break;
  640. default:
  641. /* This is an error. It really shouldn't happen. We might
  642. * want to handle it anyway, though. */
  643. state = PESinkStartup;
  644. break;
  645. }
  646. }
  647. }
  648. void pdb_pe_run(void)
  649. {
  650. pdb_pe_thread = chThdCreateStatic(waPolicyEngine, sizeof(waPolicyEngine),
  651. PDB_PRIO_PE, PolicyEngine, NULL);
  652. }