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

policy_engine.c 27KB

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