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

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