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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 "pd.h"
  26. thread_t *pdb_pe_thread;
  27. enum policy_engine_state {
  28. PESinkStartup,
  29. PESinkDiscovery,
  30. PESinkWaitCap,
  31. PESinkEvalCap,
  32. PESinkSelectCap,
  33. PESinkTransitionSink,
  34. PESinkReady,
  35. PESinkGetSourceCap,
  36. PESinkGiveSinkCap,
  37. PESinkHardReset,
  38. PESinkTransitionDefault,
  39. PESinkSoftReset,
  40. PESinkSendSoftReset,
  41. PESinkSendReject,
  42. PESinkSourceUnresponsive
  43. };
  44. /* The received message we're currently working with */
  45. static union pd_msg *policy_engine_message = NULL;
  46. /* Whether or not the source capabilities match our required power */
  47. static bool capability_match = false;
  48. /* Whether or not we have an explicit contract */
  49. static bool explicit_contract = false;
  50. /* Keep track of how many hard resets we've sent */
  51. static int hard_reset_counter = 0;
  52. /* Policy Engine thread mailbox */
  53. static msg_t pdb_pe_mailbox_queue[PDB_MSG_POOL_SIZE];
  54. mailbox_t pdb_pe_mailbox;
  55. static enum policy_engine_state pe_sink_startup(void)
  56. {
  57. explicit_contract = false;
  58. pdb_dpm_pd_start();
  59. /* No need to reset the protocol layer here. There are two ways into this
  60. * state: startup and exiting hard reset. On startup, the protocol layer
  61. * is reset by the startup procedure. When exiting hard reset, the
  62. * protocol layer is reset by the hard reset state machine. Since it's
  63. * already done somewhere else, there's no need to do it again here. */
  64. return PESinkDiscovery;
  65. }
  66. static enum policy_engine_state pe_sink_discovery(void)
  67. {
  68. /* Wait for VBUS. Since it's our only power source, we already know that
  69. * we have it, so just move on. */
  70. return PESinkWaitCap;
  71. }
  72. static enum policy_engine_state pe_sink_wait_cap(void)
  73. {
  74. /* Fetch a message from the protocol layer */
  75. eventmask_t evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX, PD_T_TYPEC_SINK_WAIT_CAP);
  76. /* If we timed out waiting for Source_Capabilities, send a hard reset */
  77. if (evt == 0) {
  78. return PESinkHardReset;
  79. }
  80. /* Get the message */
  81. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  82. /* If we got a Source_Capabilities message, read it. */
  83. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  84. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  85. return PESinkEvalCap;
  86. /* If the message was a Soft_Reset, do the soft reset procedure */
  87. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  88. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  89. chPoolFree(&pdb_msg_pool, policy_engine_message);
  90. policy_engine_message = NULL;
  91. return PESinkSoftReset;
  92. /* If we got an unexpected message, reset */
  93. } else {
  94. /* Free the received message */
  95. chPoolFree(&pdb_msg_pool, policy_engine_message);
  96. return PESinkHardReset;
  97. }
  98. }
  99. /* If we failed to get a message, send a hard reset */
  100. return PESinkHardReset;
  101. }
  102. static enum policy_engine_state pe_sink_eval_cap(void)
  103. {
  104. /* Get a message object */
  105. union pd_msg *request = chPoolAlloc(&pdb_msg_pool);
  106. /* Ask the DPM what to request */
  107. capability_match = pdb_dpm_evaluate_capability(policy_engine_message, request);
  108. /* Free the Source_Capabilities message */
  109. chPoolFree(&pdb_msg_pool, policy_engine_message);
  110. /* Put the request into policy_engine_message */
  111. policy_engine_message = request;
  112. return PESinkSelectCap;
  113. }
  114. static enum policy_engine_state pe_sink_select_cap(void)
  115. {
  116. /* Transmit the request */
  117. chMBPost(&pdb_prltx_mailbox, (msg_t) policy_engine_message, TIME_IMMEDIATE);
  118. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  119. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  120. /* Free the sent message */
  121. chPoolFree(&pdb_msg_pool, policy_engine_message);
  122. policy_engine_message = NULL;
  123. /* If the message transmission failed, send a hard reset */
  124. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  125. return PESinkHardReset;
  126. }
  127. /* Wait for a response */
  128. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX, PD_T_SENDER_RESPONSE);
  129. /* If we didn't get a response before the timeout, send a hard reset */
  130. if (evt == 0) {
  131. return PESinkHardReset;
  132. }
  133. /* Get the response message */
  134. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  135. /* If the source accepted our request, wait for the new power */
  136. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  137. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  138. /* Transition to Sink Standby if necessary */
  139. pdb_dpm_sink_standby();
  140. chPoolFree(&pdb_msg_pool, policy_engine_message);
  141. policy_engine_message = NULL;
  142. return PESinkTransitionSink;
  143. /* If the message was a Soft_Reset, do the soft reset procedure */
  144. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  145. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  146. chPoolFree(&pdb_msg_pool, policy_engine_message);
  147. policy_engine_message = NULL;
  148. return PESinkSoftReset;
  149. /* If the message was Wait or Reject */
  150. } else if ((PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REJECT
  151. || PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT)
  152. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  153. /* If we don't have an explicit contract, wait for capabilities */
  154. if (!explicit_contract) {
  155. chPoolFree(&pdb_msg_pool, policy_engine_message);
  156. policy_engine_message = NULL;
  157. return PESinkWaitCap;
  158. /* If we do have an explicit contract, go to the ready state */
  159. } else {
  160. /* TODO: we should take note if we got here from a Wait
  161. * message, because we Should run the SinkRequestTimer in the
  162. * Ready state if that's the case. */
  163. chPoolFree(&pdb_msg_pool, policy_engine_message);
  164. policy_engine_message = NULL;
  165. return PESinkReady;
  166. }
  167. } else {
  168. chPoolFree(&pdb_msg_pool, policy_engine_message);
  169. policy_engine_message = NULL;
  170. return PESinkSendSoftReset;
  171. }
  172. }
  173. return PESinkHardReset;
  174. }
  175. static enum policy_engine_state pe_sink_transition_sink(void)
  176. {
  177. /* Wait for the PS_RDY message */
  178. eventmask_t evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX, PD_T_PS_TRANSITION);
  179. /* If no message was received, send a hard reset */
  180. if (evt == 0) {
  181. return PESinkHardReset;
  182. }
  183. /* If we received a message, read it */
  184. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  185. /* If we got a PS_RDY, handle it */
  186. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PS_RDY
  187. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  188. /* We just finished negotiating an explicit contract */
  189. explicit_contract = true;
  190. /* Set the output appropriately */
  191. pdb_dpm_output_set(capability_match);
  192. chPoolFree(&pdb_msg_pool, policy_engine_message);
  193. policy_engine_message = NULL;
  194. return PESinkReady;
  195. /* If there was a protocol error, send a hard reset */
  196. } else {
  197. /* Turn off the power output before this hard reset to make sure we
  198. * don't supply an incorrect voltage to the device we're powering.
  199. */
  200. pdb_dpm_output_set(false);
  201. chPoolFree(&pdb_msg_pool, policy_engine_message);
  202. policy_engine_message = NULL;
  203. return PESinkHardReset;
  204. }
  205. }
  206. return PESinkHardReset;
  207. }
  208. static enum policy_engine_state pe_sink_ready(void)
  209. {
  210. /* Wait for an event */
  211. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET);
  212. /* If we got reset signaling, transition to default */
  213. if (evt & PDB_EVT_PE_RESET) {
  214. return PESinkTransitionDefault;
  215. }
  216. /* If we received a message */
  217. if (evt & PDB_EVT_PE_MSG_RX) {
  218. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  219. /* Ignore vendor-defined messages */
  220. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VENDOR_DEFINED
  221. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  222. chPoolFree(&pdb_msg_pool, policy_engine_message);
  223. policy_engine_message = NULL;
  224. return PESinkReady;
  225. /* Ignore Ping messages */
  226. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PING
  227. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  228. chPoolFree(&pdb_msg_pool, policy_engine_message);
  229. policy_engine_message = NULL;
  230. return PESinkReady;
  231. /* Reject DR_Swap messages */
  232. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_DR_SWAP
  233. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  234. chPoolFree(&pdb_msg_pool, policy_engine_message);
  235. policy_engine_message = NULL;
  236. return PESinkSendReject;
  237. /* Reject Get_Source_Cap messages */
  238. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SOURCE_CAP
  239. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  240. chPoolFree(&pdb_msg_pool, policy_engine_message);
  241. policy_engine_message = NULL;
  242. return PESinkSendReject;
  243. /* Reject PR_Swap messages */
  244. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PR_SWAP
  245. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  246. chPoolFree(&pdb_msg_pool, policy_engine_message);
  247. policy_engine_message = NULL;
  248. return PESinkSendReject;
  249. /* Reject VCONN_Swap messages */
  250. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VCONN_SWAP
  251. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  252. chPoolFree(&pdb_msg_pool, policy_engine_message);
  253. policy_engine_message = NULL;
  254. return PESinkSendReject;
  255. /* Reject Request messages */
  256. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REQUEST
  257. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  258. chPoolFree(&pdb_msg_pool, policy_engine_message);
  259. policy_engine_message = NULL;
  260. return PESinkSendReject;
  261. /* Reject Sink_Capabilities messages */
  262. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SINK_CAPABILITIES
  263. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  264. chPoolFree(&pdb_msg_pool, policy_engine_message);
  265. policy_engine_message = NULL;
  266. return PESinkSendReject;
  267. /* Reject GotoMin messages
  268. * Until we actually support GiveBack, this is the correct
  269. * behavior according to S. 6.3.4 */
  270. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GOTOMIN
  271. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  272. chPoolFree(&pdb_msg_pool, policy_engine_message);
  273. policy_engine_message = NULL;
  274. return PESinkSendReject;
  275. /* Evaluate new Source_Capabilities */
  276. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  277. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  278. /* Don't free the message: we need to keep the
  279. * Source_Capabilities message so we can evaluate it. */
  280. return PESinkEvalCap;
  281. /* Give sink capabilities when asked */
  282. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SINK_CAP
  283. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  284. chPoolFree(&pdb_msg_pool, policy_engine_message);
  285. policy_engine_message = NULL;
  286. return PESinkGiveSinkCap;
  287. /* If the message was a Soft_Reset, do the soft reset procedure */
  288. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  289. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  290. chPoolFree(&pdb_msg_pool, policy_engine_message);
  291. policy_engine_message = NULL;
  292. return PESinkSoftReset;
  293. /* If we got an unknown message, send a soft reset */
  294. } else {
  295. chPoolFree(&pdb_msg_pool, policy_engine_message);
  296. policy_engine_message = NULL;
  297. return PESinkSendSoftReset;
  298. }
  299. }
  300. }
  301. return PESinkReady;
  302. }
  303. static enum policy_engine_state pe_sink_get_source_cap(void)
  304. {
  305. /* Stubbed until we actually have a need for this */
  306. return PESinkReady;
  307. }
  308. static enum policy_engine_state pe_sink_give_sink_cap(void)
  309. {
  310. /* Get a message object */
  311. union pd_msg *snk_cap = chPoolAlloc(&pdb_msg_pool);
  312. /* Get our capabilities from the DPM */
  313. pdb_dpm_get_sink_capability(snk_cap);
  314. /* Transmit our capabilities */
  315. chMBPost(&pdb_prltx_mailbox, (msg_t) snk_cap, TIME_IMMEDIATE);
  316. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  317. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  318. /* Free the Sink_Capabilities message */
  319. chPoolFree(&pdb_msg_pool, snk_cap);
  320. snk_cap = NULL;
  321. /* If the message transmission failed, send a hard reset */
  322. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  323. return PESinkHardReset;
  324. }
  325. return PESinkReady;
  326. }
  327. static enum policy_engine_state pe_sink_hard_reset(void)
  328. {
  329. /* If we've already sent the maximum number of hard resets, assume the
  330. * source is unresponsive. */
  331. if (hard_reset_counter > PD_N_HARD_RESET_COUNT) {
  332. return PESinkSourceUnresponsive;
  333. }
  334. /* Generate a hard reset signal */
  335. chEvtSignal(pdb_hardrst_thread, PDB_EVT_HARDRST_RESET);
  336. chEvtWaitAny(PDB_EVT_PE_HARD_SENT);
  337. /* Increment HardResetCounter */
  338. hard_reset_counter++;
  339. return PESinkTransitionDefault;
  340. }
  341. static enum policy_engine_state pe_sink_transition_default(void)
  342. {
  343. explicit_contract = false;
  344. /* Tell the DPM to transition to default power */
  345. pdb_dpm_output_default();
  346. /* There is no local hardware to reset. */
  347. /* Since we never change our data role from UFP, there is no reason to set
  348. * it here. */
  349. /* Tell the protocol layer we're done with the reset */
  350. chEvtSignal(pdb_hardrst_thread, PDB_EVT_HARDRST_DONE);
  351. return PESinkStartup;
  352. }
  353. static enum policy_engine_state pe_sink_soft_reset(void)
  354. {
  355. /* No need to explicitly reset the protocol layer here. It resets itself
  356. * when a Soft_Reset message is received. */
  357. /* Get a message object */
  358. union pd_msg *accept = chPoolAlloc(&pdb_msg_pool);
  359. /* Make an Accept message */
  360. accept->hdr = PD_MSGTYPE_ACCEPT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  361. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  362. /* Transmit the Accept */
  363. chMBPost(&pdb_prltx_mailbox, (msg_t) accept, TIME_IMMEDIATE);
  364. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  365. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  366. /* Free the sent message */
  367. chPoolFree(&pdb_msg_pool, accept);
  368. accept = NULL;
  369. /* If the message transmission failed, send a hard reset */
  370. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  371. return PESinkHardReset;
  372. }
  373. return PESinkWaitCap;
  374. }
  375. static enum policy_engine_state pe_sink_send_soft_reset(void)
  376. {
  377. /* No need to explicitly reset the protocol layer here. It resets itself
  378. * just before a Soft_Reset message is transmitted. */
  379. /* Get a message object */
  380. union pd_msg *softrst = chPoolAlloc(&pdb_msg_pool);
  381. /* Make a Soft_Reset message */
  382. softrst->hdr = PD_MSGTYPE_SOFT_RESET | PD_DATAROLE_UFP | PD_SPECREV_2_0
  383. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  384. /* Transmit the soft reset */
  385. chMBPost(&pdb_prltx_mailbox, (msg_t) softrst, TIME_IMMEDIATE);
  386. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  387. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  388. /* Free the sent message */
  389. chPoolFree(&pdb_msg_pool, softrst);
  390. softrst = NULL;
  391. /* If the message transmission failed, send a hard reset */
  392. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  393. return PESinkHardReset;
  394. }
  395. /* Wait for a response */
  396. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX, PD_T_SENDER_RESPONSE);
  397. /* If we didn't get a response before the timeout, send a hard reset */
  398. if (evt == 0) {
  399. return PESinkHardReset;
  400. }
  401. /* Get the response message */
  402. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  403. /* If the source accepted our soft reset, wait for capabilities. */
  404. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  405. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  406. chPoolFree(&pdb_msg_pool, policy_engine_message);
  407. policy_engine_message = NULL;
  408. return PESinkWaitCap;
  409. /* If the message was a Soft_Reset, do the soft reset procedure */
  410. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  411. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  412. chPoolFree(&pdb_msg_pool, policy_engine_message);
  413. policy_engine_message = NULL;
  414. return PESinkSoftReset;
  415. /* Otherwise, send a hard reset */
  416. } else {
  417. chPoolFree(&pdb_msg_pool, policy_engine_message);
  418. policy_engine_message = NULL;
  419. return PESinkHardReset;
  420. }
  421. }
  422. return PESinkHardReset;
  423. }
  424. static enum policy_engine_state pe_sink_send_reject(void)
  425. {
  426. /* Get a message object */
  427. union pd_msg *reject = chPoolAlloc(&pdb_msg_pool);
  428. /* Make a Reject message */
  429. reject->hdr = PD_MSGTYPE_REJECT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  430. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  431. /* Transmit the message */
  432. chMBPost(&pdb_prltx_mailbox, (msg_t) reject, TIME_IMMEDIATE);
  433. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  434. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  435. /* Free the Reject message */
  436. chPoolFree(&pdb_msg_pool, reject);
  437. reject = NULL;
  438. /* If the message transmission failed, send a soft reset */
  439. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  440. return PESinkSendSoftReset;
  441. }
  442. return PESinkReady;
  443. }
  444. /*
  445. * When Power Delivery is unresponsive, fall back to Type-C Current
  446. */
  447. static enum policy_engine_state pe_sink_source_unresponsive(void)
  448. {
  449. static int old_tcc_match = -1;
  450. int tcc_match = pdb_dpm_evaluate_typec_current();
  451. /* If the last two readings are the same, set the output */
  452. if (old_tcc_match == tcc_match) {
  453. pdb_dpm_output_set(tcc_match);
  454. }
  455. /* Remember whether or not the last measurement succeeded */
  456. old_tcc_match = tcc_match;
  457. /* Wait tPDDebounce between measurements */
  458. chThdSleep(PD_T_PD_DEBOUNCE);
  459. return PESinkSourceUnresponsive;
  460. }
  461. /*
  462. * Policy Engine state machine thread
  463. */
  464. static THD_WORKING_AREA(waPolicyEngine, 128);
  465. static THD_FUNCTION(PolicyEngine, arg) {
  466. (void) arg;
  467. enum policy_engine_state state = PESinkStartup;
  468. /* Initialize the mailbox */
  469. chMBObjectInit(&pdb_pe_mailbox, pdb_pe_mailbox_queue, PDB_MSG_POOL_SIZE);
  470. while (true) {
  471. switch (state) {
  472. case PESinkStartup:
  473. state = pe_sink_startup();
  474. break;
  475. case PESinkDiscovery:
  476. state = pe_sink_discovery();
  477. break;
  478. case PESinkWaitCap:
  479. state = pe_sink_wait_cap();
  480. break;
  481. case PESinkEvalCap:
  482. state = pe_sink_eval_cap();
  483. break;
  484. case PESinkSelectCap:
  485. state = pe_sink_select_cap();
  486. break;
  487. case PESinkTransitionSink:
  488. state = pe_sink_transition_sink();
  489. break;
  490. case PESinkReady:
  491. state = pe_sink_ready();
  492. break;
  493. case PESinkGetSourceCap:
  494. state = pe_sink_get_source_cap();
  495. break;
  496. case PESinkGiveSinkCap:
  497. state = pe_sink_give_sink_cap();
  498. break;
  499. case PESinkHardReset:
  500. state = pe_sink_hard_reset();
  501. break;
  502. case PESinkTransitionDefault:
  503. state = pe_sink_transition_default();
  504. break;
  505. case PESinkSoftReset:
  506. state = pe_sink_soft_reset();
  507. break;
  508. case PESinkSendSoftReset:
  509. state = pe_sink_send_soft_reset();
  510. break;
  511. case PESinkSendReject:
  512. state = pe_sink_send_reject();
  513. break;
  514. case PESinkSourceUnresponsive:
  515. state = pe_sink_source_unresponsive();
  516. break;
  517. default:
  518. /* This is an error. It really shouldn't happen. We might
  519. * want to handle it anyway, though. */
  520. state = PESinkStartup;
  521. break;
  522. }
  523. }
  524. }
  525. void pdb_pe_run(void)
  526. {
  527. pdb_pe_thread = chThdCreateStatic(waPolicyEngine, sizeof(waPolicyEngine),
  528. PDB_PRIO_PE, PolicyEngine, NULL);
  529. }