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 "protocol_tx.h"
  23. #include "hard_reset.h"
  24. #include "fusb302b.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(struct pdb_config *cfg)
  60. {
  61. /* We don't have an explicit contract currently */
  62. explicit_contract = false;
  63. /* Tell the DPM that we've started negotiations */
  64. cfg->dpm.pd_start(cfg);
  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(struct pdb_config *cfg)
  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(struct pdb_config *cfg)
  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(struct pdb_config *cfg)
  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 = cfg->dpm.evaluate_capability(cfg, 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(struct pdb_config *cfg)
  136. {
  137. /* Transmit the request */
  138. chMBPost(&cfg->prl.tx_mailbox, (msg_t) last_dpm_request, TIME_IMMEDIATE);
  139. chEvtSignal(cfg->prl.tx_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. cfg->dpm.transition_standby(cfg);
  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(struct pdb_config *cfg)
  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. if (!min_power) {
  227. cfg->dpm.transition_requested(cfg);
  228. }
  229. chPoolFree(&pdb_msg_pool, policy_engine_message);
  230. policy_engine_message = NULL;
  231. return PESinkReady;
  232. /* If there was a protocol error, send a hard reset */
  233. } else {
  234. /* Turn off the power output before this hard reset to make sure we
  235. * don't supply an incorrect voltage to the device we're powering.
  236. */
  237. cfg->dpm.transition_default(cfg);
  238. chPoolFree(&pdb_msg_pool, policy_engine_message);
  239. policy_engine_message = NULL;
  240. return PESinkHardReset;
  241. }
  242. }
  243. return PESinkHardReset;
  244. }
  245. static enum policy_engine_state pe_sink_ready(struct pdb_config *cfg)
  246. {
  247. eventmask_t evt;
  248. /* Wait for an event */
  249. if (min_power) {
  250. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
  251. | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP
  252. | PDB_EVT_PE_NEW_POWER, PD_T_SINK_REQUEST);
  253. } else {
  254. evt = chEvtWaitAny(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
  255. | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP
  256. | PDB_EVT_PE_NEW_POWER);
  257. }
  258. /* If we got reset signaling, transition to default */
  259. if (evt & PDB_EVT_PE_RESET) {
  260. return PESinkTransitionDefault;
  261. }
  262. /* If we overheated, send a hard reset */
  263. if (evt & PDB_EVT_PE_I_OVRTEMP) {
  264. return PESinkHardReset;
  265. }
  266. /* If the DPM wants us to, send a Get_Source_Cap message */
  267. if (evt & PDB_EVT_PE_GET_SOURCE_CAP) {
  268. return PESinkGetSourceCap;
  269. }
  270. /* If the DPM wants new power, let it figure out what power it wants
  271. * exactly. This isn't exactly the transition from the spec (that would be
  272. * SelectCap, not EvalCap), but this works better with the particular
  273. * design of this firmware. */
  274. if (evt & PDB_EVT_PE_NEW_POWER) {
  275. /* Make sure we're evaluating NULL capabilities to use the old ones */
  276. if (policy_engine_message != NULL) {
  277. chPoolFree(&pdb_msg_pool, policy_engine_message);
  278. policy_engine_message = NULL;
  279. }
  280. return PESinkEvalCap;
  281. }
  282. /* If no event was received, the timer ran out. */
  283. if (evt == 0) {
  284. /* Repeat our Request message */
  285. return PESinkSelectCap;
  286. }
  287. /* If we received a message */
  288. if (evt & PDB_EVT_PE_MSG_RX) {
  289. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  290. /* Ignore vendor-defined messages */
  291. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VENDOR_DEFINED
  292. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  293. chPoolFree(&pdb_msg_pool, policy_engine_message);
  294. policy_engine_message = NULL;
  295. return PESinkReady;
  296. /* Ignore Ping messages */
  297. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PING
  298. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  299. chPoolFree(&pdb_msg_pool, policy_engine_message);
  300. policy_engine_message = NULL;
  301. return PESinkReady;
  302. /* Reject DR_Swap messages */
  303. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_DR_SWAP
  304. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  305. chPoolFree(&pdb_msg_pool, policy_engine_message);
  306. policy_engine_message = NULL;
  307. return PESinkSendReject;
  308. /* Reject Get_Source_Cap messages */
  309. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SOURCE_CAP
  310. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  311. chPoolFree(&pdb_msg_pool, policy_engine_message);
  312. policy_engine_message = NULL;
  313. return PESinkSendReject;
  314. /* Reject PR_Swap messages */
  315. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PR_SWAP
  316. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  317. chPoolFree(&pdb_msg_pool, policy_engine_message);
  318. policy_engine_message = NULL;
  319. return PESinkSendReject;
  320. /* Reject VCONN_Swap messages */
  321. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VCONN_SWAP
  322. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  323. chPoolFree(&pdb_msg_pool, policy_engine_message);
  324. policy_engine_message = NULL;
  325. return PESinkSendReject;
  326. /* Reject Request messages */
  327. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REQUEST
  328. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  329. chPoolFree(&pdb_msg_pool, policy_engine_message);
  330. policy_engine_message = NULL;
  331. return PESinkSendReject;
  332. /* Reject Sink_Capabilities messages */
  333. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SINK_CAPABILITIES
  334. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  335. chPoolFree(&pdb_msg_pool, policy_engine_message);
  336. policy_engine_message = NULL;
  337. return PESinkSendReject;
  338. /* Handle GotoMin messages */
  339. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GOTOMIN
  340. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  341. if (cfg->dpm.giveback_enabled(cfg)) {
  342. /* Transition to the minimum current level */
  343. cfg->dpm.transition_min(cfg);
  344. min_power = true;
  345. chPoolFree(&pdb_msg_pool, policy_engine_message);
  346. policy_engine_message = NULL;
  347. return PESinkTransitionSink;
  348. } else {
  349. /* We don't support GiveBack, so send a Reject */
  350. chPoolFree(&pdb_msg_pool, policy_engine_message);
  351. policy_engine_message = NULL;
  352. return PESinkSendReject;
  353. }
  354. /* Evaluate new Source_Capabilities */
  355. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  356. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  357. /* Don't free the message: we need to keep the
  358. * Source_Capabilities message so we can evaluate it. */
  359. return PESinkEvalCap;
  360. /* Give sink capabilities when asked */
  361. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SINK_CAP
  362. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  363. chPoolFree(&pdb_msg_pool, policy_engine_message);
  364. policy_engine_message = NULL;
  365. return PESinkGiveSinkCap;
  366. /* If the message was a Soft_Reset, do the soft reset procedure */
  367. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  368. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  369. chPoolFree(&pdb_msg_pool, policy_engine_message);
  370. policy_engine_message = NULL;
  371. return PESinkSoftReset;
  372. /* If we got an unknown message, send a soft reset */
  373. } else {
  374. chPoolFree(&pdb_msg_pool, policy_engine_message);
  375. policy_engine_message = NULL;
  376. return PESinkSendSoftReset;
  377. }
  378. }
  379. }
  380. return PESinkReady;
  381. }
  382. static enum policy_engine_state pe_sink_get_source_cap(struct pdb_config *cfg)
  383. {
  384. /* Get a message object */
  385. union pd_msg *get_source_cap = chPoolAlloc(&pdb_msg_pool);
  386. /* Make a Get_Source_Cap message */
  387. get_source_cap->hdr = PD_MSGTYPE_GET_SOURCE_CAP | PD_DATAROLE_UFP
  388. | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  389. /* Transmit the Get_Source_Cap */
  390. chMBPost(&cfg->prl.tx_mailbox, (msg_t) get_source_cap, TIME_IMMEDIATE);
  391. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  392. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  393. | PDB_EVT_PE_RESET);
  394. /* Free the sent message */
  395. chPoolFree(&pdb_msg_pool, get_source_cap);
  396. get_source_cap = NULL;
  397. /* If we got reset signaling, transition to default */
  398. if (evt & PDB_EVT_PE_RESET) {
  399. return PESinkTransitionDefault;
  400. }
  401. /* If the message transmission failed, send a hard reset */
  402. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  403. return PESinkHardReset;
  404. }
  405. return PESinkReady;
  406. }
  407. static enum policy_engine_state pe_sink_give_sink_cap(struct pdb_config *cfg)
  408. {
  409. /* Get a message object */
  410. union pd_msg *snk_cap = chPoolAlloc(&pdb_msg_pool);
  411. /* Get our capabilities from the DPM */
  412. cfg->dpm.get_sink_capability(cfg, snk_cap);
  413. /* Transmit our capabilities */
  414. chMBPost(&cfg->prl.tx_mailbox, (msg_t) snk_cap, TIME_IMMEDIATE);
  415. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  416. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  417. | PDB_EVT_PE_RESET);
  418. /* Free the Sink_Capabilities message */
  419. chPoolFree(&pdb_msg_pool, snk_cap);
  420. snk_cap = NULL;
  421. /* If we got reset signaling, transition to default */
  422. if (evt & PDB_EVT_PE_RESET) {
  423. return PESinkTransitionDefault;
  424. }
  425. /* If the message transmission failed, send a hard reset */
  426. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  427. return PESinkHardReset;
  428. }
  429. return PESinkReady;
  430. }
  431. static enum policy_engine_state pe_sink_hard_reset(struct pdb_config *cfg)
  432. {
  433. /* If we've already sent the maximum number of hard resets, assume the
  434. * source is unresponsive. */
  435. if (hard_reset_counter > PD_N_HARD_RESET_COUNT) {
  436. return PESinkSourceUnresponsive;
  437. }
  438. /* Generate a hard reset signal */
  439. chEvtSignal(cfg->prl.hardrst_thread, PDB_EVT_HARDRST_RESET);
  440. chEvtWaitAny(PDB_EVT_PE_HARD_SENT);
  441. /* Increment HardResetCounter */
  442. hard_reset_counter++;
  443. return PESinkTransitionDefault;
  444. }
  445. static enum policy_engine_state pe_sink_transition_default(struct pdb_config *cfg)
  446. {
  447. explicit_contract = false;
  448. /* Tell the DPM to transition to default power */
  449. cfg->dpm.transition_default(cfg);
  450. /* There is no local hardware to reset. */
  451. /* Since we never change our data role from UFP, there is no reason to set
  452. * it here. */
  453. /* Tell the protocol layer we're done with the reset */
  454. chEvtSignal(cfg->prl.hardrst_thread, PDB_EVT_HARDRST_DONE);
  455. return PESinkStartup;
  456. }
  457. static enum policy_engine_state pe_sink_soft_reset(struct pdb_config *cfg)
  458. {
  459. /* No need to explicitly reset the protocol layer here. It resets itself
  460. * when a Soft_Reset message is received. */
  461. /* Get a message object */
  462. union pd_msg *accept = chPoolAlloc(&pdb_msg_pool);
  463. /* Make an Accept message */
  464. accept->hdr = PD_MSGTYPE_ACCEPT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  465. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  466. /* Transmit the Accept */
  467. chMBPost(&cfg->prl.tx_mailbox, (msg_t) accept, TIME_IMMEDIATE);
  468. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  469. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  470. | PDB_EVT_PE_RESET);
  471. /* Free the sent message */
  472. chPoolFree(&pdb_msg_pool, accept);
  473. accept = NULL;
  474. /* If we got reset signaling, transition to default */
  475. if (evt & PDB_EVT_PE_RESET) {
  476. return PESinkTransitionDefault;
  477. }
  478. /* If the message transmission failed, send a hard reset */
  479. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  480. return PESinkHardReset;
  481. }
  482. return PESinkWaitCap;
  483. }
  484. static enum policy_engine_state pe_sink_send_soft_reset(struct pdb_config *cfg)
  485. {
  486. /* No need to explicitly reset the protocol layer here. It resets itself
  487. * just before a Soft_Reset message is transmitted. */
  488. /* Get a message object */
  489. union pd_msg *softrst = chPoolAlloc(&pdb_msg_pool);
  490. /* Make a Soft_Reset message */
  491. softrst->hdr = PD_MSGTYPE_SOFT_RESET | PD_DATAROLE_UFP | PD_SPECREV_2_0
  492. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  493. /* Transmit the soft reset */
  494. chMBPost(&cfg->prl.tx_mailbox, (msg_t) softrst, TIME_IMMEDIATE);
  495. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  496. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  497. | PDB_EVT_PE_RESET);
  498. /* Free the sent message */
  499. chPoolFree(&pdb_msg_pool, softrst);
  500. softrst = NULL;
  501. /* If we got reset signaling, transition to default */
  502. if (evt & PDB_EVT_PE_RESET) {
  503. return PESinkTransitionDefault;
  504. }
  505. /* If the message transmission failed, send a hard reset */
  506. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  507. return PESinkHardReset;
  508. }
  509. /* Wait for a response */
  510. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET,
  511. PD_T_SENDER_RESPONSE);
  512. /* If we got reset signaling, transition to default */
  513. if (evt & PDB_EVT_PE_RESET) {
  514. return PESinkTransitionDefault;
  515. }
  516. /* If we didn't get a response before the timeout, send a hard reset */
  517. if (evt == 0) {
  518. return PESinkHardReset;
  519. }
  520. /* Get the response message */
  521. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  522. /* If the source accepted our soft reset, wait for capabilities. */
  523. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  524. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  525. chPoolFree(&pdb_msg_pool, policy_engine_message);
  526. policy_engine_message = NULL;
  527. return PESinkWaitCap;
  528. /* If the message was a Soft_Reset, do the soft reset procedure */
  529. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  530. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  531. chPoolFree(&pdb_msg_pool, policy_engine_message);
  532. policy_engine_message = NULL;
  533. return PESinkSoftReset;
  534. /* Otherwise, send a hard reset */
  535. } else {
  536. chPoolFree(&pdb_msg_pool, policy_engine_message);
  537. policy_engine_message = NULL;
  538. return PESinkHardReset;
  539. }
  540. }
  541. return PESinkHardReset;
  542. }
  543. static enum policy_engine_state pe_sink_send_reject(struct pdb_config *cfg)
  544. {
  545. /* Get a message object */
  546. union pd_msg *reject = chPoolAlloc(&pdb_msg_pool);
  547. /* Make a Reject message */
  548. reject->hdr = PD_MSGTYPE_REJECT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  549. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  550. /* Transmit the message */
  551. chMBPost(&cfg->prl.tx_mailbox, (msg_t) reject, TIME_IMMEDIATE);
  552. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  553. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  554. | PDB_EVT_PE_RESET);
  555. /* Free the Reject message */
  556. chPoolFree(&pdb_msg_pool, reject);
  557. reject = NULL;
  558. /* If we got reset signaling, transition to default */
  559. if (evt & PDB_EVT_PE_RESET) {
  560. return PESinkTransitionDefault;
  561. }
  562. /* If the message transmission failed, send a soft reset */
  563. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  564. return PESinkSendSoftReset;
  565. }
  566. return PESinkReady;
  567. }
  568. /*
  569. * When Power Delivery is unresponsive, fall back to Type-C Current
  570. */
  571. static enum policy_engine_state pe_sink_source_unresponsive(struct pdb_config *cfg)
  572. {
  573. static int old_tcc_match = -1;
  574. int tcc_match = cfg->dpm.evaluate_typec_current(cfg, fusb_get_typec_current());
  575. /* If the last two readings are the same, set the output */
  576. if (old_tcc_match == tcc_match) {
  577. cfg->dpm.transition_typec(cfg);
  578. }
  579. /* Remember whether or not the last measurement succeeded */
  580. old_tcc_match = tcc_match;
  581. /* Wait tPDDebounce between measurements */
  582. chThdSleep(PD_T_PD_DEBOUNCE);
  583. return PESinkSourceUnresponsive;
  584. }
  585. /*
  586. * Policy Engine state machine thread
  587. */
  588. static THD_FUNCTION(PolicyEngine, cfg) {
  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(cfg);
  596. break;
  597. case PESinkDiscovery:
  598. state = pe_sink_discovery(cfg);
  599. break;
  600. case PESinkWaitCap:
  601. state = pe_sink_wait_cap(cfg);
  602. break;
  603. case PESinkEvalCap:
  604. state = pe_sink_eval_cap(cfg);
  605. break;
  606. case PESinkSelectCap:
  607. state = pe_sink_select_cap(cfg);
  608. break;
  609. case PESinkTransitionSink:
  610. state = pe_sink_transition_sink(cfg);
  611. break;
  612. case PESinkReady:
  613. state = pe_sink_ready(cfg);
  614. break;
  615. case PESinkGetSourceCap:
  616. state = pe_sink_get_source_cap(cfg);
  617. break;
  618. case PESinkGiveSinkCap:
  619. state = pe_sink_give_sink_cap(cfg);
  620. break;
  621. case PESinkHardReset:
  622. state = pe_sink_hard_reset(cfg);
  623. break;
  624. case PESinkTransitionDefault:
  625. state = pe_sink_transition_default(cfg);
  626. break;
  627. case PESinkSoftReset:
  628. state = pe_sink_soft_reset(cfg);
  629. break;
  630. case PESinkSendSoftReset:
  631. state = pe_sink_send_soft_reset(cfg);
  632. break;
  633. case PESinkSendReject:
  634. state = pe_sink_send_reject(cfg);
  635. break;
  636. case PESinkSourceUnresponsive:
  637. state = pe_sink_source_unresponsive(cfg);
  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(struct pdb_config *cfg)
  648. {
  649. pdb_pe_thread = chThdCreateStatic(cfg->pe._wa, sizeof(cfg->pe._wa),
  650. PDB_PRIO_PE, PolicyEngine, cfg);
  651. }