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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. static enum policy_engine_state pe_sink_startup(struct pdb_config *cfg)
  57. {
  58. /* We don't have an explicit contract currently */
  59. explicit_contract = false;
  60. /* Tell the DPM that we've started negotiations */
  61. cfg->dpm.pd_start(cfg);
  62. /* No need to reset the protocol layer here. There are two ways into this
  63. * state: startup and exiting hard reset. On startup, the protocol layer
  64. * is reset by the startup procedure. When exiting hard reset, the
  65. * protocol layer is reset by the hard reset state machine. Since it's
  66. * already done somewhere else, there's no need to do it again here. */
  67. return PESinkDiscovery;
  68. }
  69. static enum policy_engine_state pe_sink_discovery(struct pdb_config *cfg)
  70. {
  71. (void) cfg;
  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(struct pdb_config *cfg)
  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(&cfg->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(struct pdb_config *cfg)
  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 = cfg->dpm.evaluate_capability(cfg, policy_engine_message,
  126. last_dpm_request);
  127. /* It's up to the DPM to free the Source_Capabilities message, which it can
  128. * do whenever it sees fit. Just remove our reference to it since we won't
  129. * know when it's no longer valid. */
  130. policy_engine_message = NULL;
  131. return PESinkSelectCap;
  132. }
  133. static enum policy_engine_state pe_sink_select_cap(struct pdb_config *cfg)
  134. {
  135. /* Transmit the request */
  136. chMBPost(&cfg->prl.tx_mailbox, (msg_t) last_dpm_request, TIME_IMMEDIATE);
  137. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  138. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  139. | PDB_EVT_PE_RESET);
  140. /* Don't free the request; we might need it again */
  141. /* If we got reset signaling, transition to default */
  142. if (evt & PDB_EVT_PE_RESET) {
  143. return PESinkTransitionDefault;
  144. }
  145. /* If the message transmission failed, send a hard reset */
  146. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  147. return PESinkHardReset;
  148. }
  149. /* Wait for a response */
  150. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET,
  151. PD_T_SENDER_RESPONSE);
  152. /* If we got reset signaling, transition to default */
  153. if (evt & PDB_EVT_PE_RESET) {
  154. return PESinkTransitionDefault;
  155. }
  156. /* If we didn't get a response before the timeout, send a hard reset */
  157. if (evt == 0) {
  158. return PESinkHardReset;
  159. }
  160. /* Get the response message */
  161. if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  162. /* If the source accepted our request, wait for the new power */
  163. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  164. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  165. /* Transition to Sink Standby if necessary */
  166. cfg->dpm.transition_standby(cfg);
  167. min_power = false;
  168. chPoolFree(&pdb_msg_pool, policy_engine_message);
  169. policy_engine_message = NULL;
  170. return PESinkTransitionSink;
  171. /* If the message was a Soft_Reset, do the soft reset procedure */
  172. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  173. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  174. chPoolFree(&pdb_msg_pool, policy_engine_message);
  175. policy_engine_message = NULL;
  176. return PESinkSoftReset;
  177. /* If the message was Wait or Reject */
  178. } else if ((PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REJECT
  179. || PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT)
  180. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  181. /* If we don't have an explicit contract, wait for capabilities */
  182. if (!explicit_contract) {
  183. chPoolFree(&pdb_msg_pool, policy_engine_message);
  184. policy_engine_message = NULL;
  185. return PESinkWaitCap;
  186. /* If we do have an explicit contract, go to the ready state */
  187. } else {
  188. /* If we got here from a Wait message, we Should run
  189. * SinkRequestTimer in the Ready state. */
  190. min_power = (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT);
  191. chPoolFree(&pdb_msg_pool, policy_engine_message);
  192. policy_engine_message = NULL;
  193. return PESinkReady;
  194. }
  195. } else {
  196. chPoolFree(&pdb_msg_pool, policy_engine_message);
  197. policy_engine_message = NULL;
  198. return PESinkSendSoftReset;
  199. }
  200. }
  201. return PESinkHardReset;
  202. }
  203. static enum policy_engine_state pe_sink_transition_sink(struct pdb_config *cfg)
  204. {
  205. /* Wait for the PS_RDY message */
  206. eventmask_t evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET,
  207. PD_T_PS_TRANSITION);
  208. /* If we got reset signaling, transition to default */
  209. if (evt & PDB_EVT_PE_RESET) {
  210. return PESinkTransitionDefault;
  211. }
  212. /* If no message was received, send a hard reset */
  213. if (evt == 0) {
  214. return PESinkHardReset;
  215. }
  216. /* If we received a message, read it */
  217. if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  218. /* If we got a PS_RDY, handle it */
  219. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PS_RDY
  220. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  221. /* We just finished negotiating an explicit contract */
  222. explicit_contract = true;
  223. /* Set the output appropriately */
  224. if (!min_power) {
  225. cfg->dpm.transition_requested(cfg);
  226. }
  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. cfg->dpm.transition_default(cfg);
  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(struct pdb_config *cfg)
  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(&cfg->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 (cfg->dpm.giveback_enabled(cfg)) {
  340. /* Transition to the minimum current level */
  341. cfg->dpm.transition_min(cfg);
  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(struct pdb_config *cfg)
  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(&cfg->prl.tx_mailbox, (msg_t) get_source_cap, TIME_IMMEDIATE);
  389. chEvtSignal(cfg->prl.tx_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(struct pdb_config *cfg)
  406. {
  407. /* Get a message object */
  408. union pd_msg *snk_cap = chPoolAlloc(&pdb_msg_pool);
  409. /* Get our capabilities from the DPM */
  410. cfg->dpm.get_sink_capability(cfg, snk_cap);
  411. /* Transmit our capabilities */
  412. chMBPost(&cfg->prl.tx_mailbox, (msg_t) snk_cap, TIME_IMMEDIATE);
  413. chEvtSignal(cfg->prl.tx_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(struct pdb_config *cfg)
  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(cfg->prl.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(struct pdb_config *cfg)
  444. {
  445. explicit_contract = false;
  446. /* Tell the DPM to transition to default power */
  447. cfg->dpm.transition_default(cfg);
  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(cfg->prl.hardrst_thread, PDB_EVT_HARDRST_DONE);
  453. return PESinkStartup;
  454. }
  455. static enum policy_engine_state pe_sink_soft_reset(struct pdb_config *cfg)
  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(&cfg->prl.tx_mailbox, (msg_t) accept, TIME_IMMEDIATE);
  466. chEvtSignal(cfg->prl.tx_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(struct pdb_config *cfg)
  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(&cfg->prl.tx_mailbox, (msg_t) softrst, TIME_IMMEDIATE);
  493. chEvtSignal(cfg->prl.tx_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(&cfg->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(struct pdb_config *cfg)
  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(&cfg->prl.tx_mailbox, (msg_t) reject, TIME_IMMEDIATE);
  550. chEvtSignal(cfg->prl.tx_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(struct pdb_config *cfg)
  570. {
  571. static int old_tcc_match = -1;
  572. int tcc_match = cfg->dpm.evaluate_typec_current(cfg, fusb_get_typec_current());
  573. /* If the last two readings are the same, set the output */
  574. if (old_tcc_match == tcc_match) {
  575. cfg->dpm.transition_typec(cfg);
  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_FUNCTION(PolicyEngine, vcfg) {
  587. struct pdb_config *cfg = vcfg;
  588. enum policy_engine_state state = PESinkStartup;
  589. /* Initialize the mailbox */
  590. chMBObjectInit(&cfg->pe.mailbox, cfg->pe._mailbox_queue, PDB_MSG_POOL_SIZE);
  591. while (true) {
  592. switch (state) {
  593. case PESinkStartup:
  594. state = pe_sink_startup(cfg);
  595. break;
  596. case PESinkDiscovery:
  597. state = pe_sink_discovery(cfg);
  598. break;
  599. case PESinkWaitCap:
  600. state = pe_sink_wait_cap(cfg);
  601. break;
  602. case PESinkEvalCap:
  603. state = pe_sink_eval_cap(cfg);
  604. break;
  605. case PESinkSelectCap:
  606. state = pe_sink_select_cap(cfg);
  607. break;
  608. case PESinkTransitionSink:
  609. state = pe_sink_transition_sink(cfg);
  610. break;
  611. case PESinkReady:
  612. state = pe_sink_ready(cfg);
  613. break;
  614. case PESinkGetSourceCap:
  615. state = pe_sink_get_source_cap(cfg);
  616. break;
  617. case PESinkGiveSinkCap:
  618. state = pe_sink_give_sink_cap(cfg);
  619. break;
  620. case PESinkHardReset:
  621. state = pe_sink_hard_reset(cfg);
  622. break;
  623. case PESinkTransitionDefault:
  624. state = pe_sink_transition_default(cfg);
  625. break;
  626. case PESinkSoftReset:
  627. state = pe_sink_soft_reset(cfg);
  628. break;
  629. case PESinkSendSoftReset:
  630. state = pe_sink_send_soft_reset(cfg);
  631. break;
  632. case PESinkSendReject:
  633. state = pe_sink_send_reject(cfg);
  634. break;
  635. case PESinkSourceUnresponsive:
  636. state = pe_sink_source_unresponsive(cfg);
  637. break;
  638. default:
  639. /* This is an error. It really shouldn't happen. We might
  640. * want to handle it anyway, though. */
  641. state = PESinkStartup;
  642. break;
  643. }
  644. }
  645. }
  646. void pdb_pe_run(struct pdb_config *cfg)
  647. {
  648. pdb_pe_thread = chThdCreateStatic(cfg->pe._wa, sizeof(cfg->pe._wa),
  649. PDB_PRIO_PE, PolicyEngine, cfg);
  650. }