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

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