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

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