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

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