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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 = PD_MSGTYPE_GET_SOURCE_CAP | PD_DATAROLE_UFP
  375. | PD_SPECREV_2_0 | PD_POWERROLE_SINK | 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 = PD_MSGTYPE_ACCEPT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  452. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  453. /* Transmit the Accept */
  454. chMBPost(&cfg->prl.tx_mailbox, (msg_t) accept, TIME_IMMEDIATE);
  455. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  456. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  457. | PDB_EVT_PE_RESET);
  458. /* Free the sent message */
  459. chPoolFree(&pdb_msg_pool, accept);
  460. accept = NULL;
  461. /* If we got reset signaling, transition to default */
  462. if (evt & PDB_EVT_PE_RESET) {
  463. return PESinkTransitionDefault;
  464. }
  465. /* If the message transmission failed, send a hard reset */
  466. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  467. return PESinkHardReset;
  468. }
  469. return PESinkWaitCap;
  470. }
  471. static enum policy_engine_state pe_sink_send_soft_reset(struct pdb_config *cfg)
  472. {
  473. /* No need to explicitly reset the protocol layer here. It resets itself
  474. * just before a Soft_Reset message is transmitted. */
  475. /* Get a message object */
  476. union pd_msg *softrst = chPoolAlloc(&pdb_msg_pool);
  477. /* Make a Soft_Reset message */
  478. softrst->hdr = PD_MSGTYPE_SOFT_RESET | PD_DATAROLE_UFP | PD_SPECREV_2_0
  479. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  480. /* Transmit the soft reset */
  481. chMBPost(&cfg->prl.tx_mailbox, (msg_t) softrst, TIME_IMMEDIATE);
  482. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  483. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  484. | PDB_EVT_PE_RESET);
  485. /* Free the sent message */
  486. chPoolFree(&pdb_msg_pool, softrst);
  487. softrst = NULL;
  488. /* If we got reset signaling, transition to default */
  489. if (evt & PDB_EVT_PE_RESET) {
  490. return PESinkTransitionDefault;
  491. }
  492. /* If the message transmission failed, send a hard reset */
  493. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  494. return PESinkHardReset;
  495. }
  496. /* Wait for a response */
  497. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET,
  498. PD_T_SENDER_RESPONSE);
  499. /* If we got reset signaling, transition to default */
  500. if (evt & PDB_EVT_PE_RESET) {
  501. return PESinkTransitionDefault;
  502. }
  503. /* If we didn't get a response before the timeout, send a hard reset */
  504. if (evt == 0) {
  505. return PESinkHardReset;
  506. }
  507. /* Get the response message */
  508. if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &cfg->pe._message, TIME_IMMEDIATE) == MSG_OK) {
  509. /* If the source accepted our soft reset, wait for capabilities. */
  510. if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_ACCEPT
  511. && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
  512. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  513. cfg->pe._message = NULL;
  514. return PESinkWaitCap;
  515. /* If the message was a Soft_Reset, do the soft reset procedure */
  516. } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOFT_RESET
  517. && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
  518. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  519. cfg->pe._message = NULL;
  520. return PESinkSoftReset;
  521. /* Otherwise, send a hard reset */
  522. } else {
  523. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  524. cfg->pe._message = NULL;
  525. return PESinkHardReset;
  526. }
  527. }
  528. return PESinkHardReset;
  529. }
  530. static enum policy_engine_state pe_sink_send_reject(struct pdb_config *cfg)
  531. {
  532. /* Get a message object */
  533. union pd_msg *reject = chPoolAlloc(&pdb_msg_pool);
  534. /* Make a Reject message */
  535. reject->hdr = PD_MSGTYPE_REJECT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  536. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  537. /* Transmit the message */
  538. chMBPost(&cfg->prl.tx_mailbox, (msg_t) reject, TIME_IMMEDIATE);
  539. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  540. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  541. | PDB_EVT_PE_RESET);
  542. /* Free the Reject message */
  543. chPoolFree(&pdb_msg_pool, reject);
  544. reject = NULL;
  545. /* If we got reset signaling, transition to default */
  546. if (evt & PDB_EVT_PE_RESET) {
  547. return PESinkTransitionDefault;
  548. }
  549. /* If the message transmission failed, send a soft reset */
  550. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  551. return PESinkSendSoftReset;
  552. }
  553. return PESinkReady;
  554. }
  555. /*
  556. * When Power Delivery is unresponsive, fall back to Type-C Current
  557. */
  558. static enum policy_engine_state pe_sink_source_unresponsive(struct pdb_config *cfg)
  559. {
  560. /* If the DPM can evaluate the Type-C Current advertisement */
  561. if (cfg->dpm.evaluate_typec_current != NULL) {
  562. /* Make the DPM evaluate the Type-C Current advertisement */
  563. int tcc_match = cfg->dpm.evaluate_typec_current(cfg,
  564. fusb_get_typec_current(&cfg->fusb));
  565. /* If the last two readings are the same, set the output */
  566. if (cfg->pe._old_tcc_match == tcc_match) {
  567. cfg->dpm.transition_typec(cfg);
  568. }
  569. /* Remember whether or not the last measurement succeeded */
  570. cfg->pe._old_tcc_match = tcc_match;
  571. }
  572. /* Wait tPDDebounce between measurements */
  573. chThdSleep(PD_T_PD_DEBOUNCE);
  574. return PESinkSourceUnresponsive;
  575. }
  576. /*
  577. * Policy Engine state machine thread
  578. */
  579. static THD_FUNCTION(PolicyEngine, vcfg) {
  580. struct pdb_config *cfg = vcfg;
  581. enum policy_engine_state state = PESinkStartup;
  582. /* Initialize the mailbox */
  583. chMBObjectInit(&cfg->pe.mailbox, cfg->pe._mailbox_queue, PDB_MSG_POOL_SIZE);
  584. /* Initialize the old_tcc_match */
  585. cfg->pe._old_tcc_match = -1;
  586. while (true) {
  587. switch (state) {
  588. case PESinkStartup:
  589. state = pe_sink_startup(cfg);
  590. break;
  591. case PESinkDiscovery:
  592. state = pe_sink_discovery(cfg);
  593. break;
  594. case PESinkWaitCap:
  595. state = pe_sink_wait_cap(cfg);
  596. break;
  597. case PESinkEvalCap:
  598. state = pe_sink_eval_cap(cfg);
  599. break;
  600. case PESinkSelectCap:
  601. state = pe_sink_select_cap(cfg);
  602. break;
  603. case PESinkTransitionSink:
  604. state = pe_sink_transition_sink(cfg);
  605. break;
  606. case PESinkReady:
  607. state = pe_sink_ready(cfg);
  608. break;
  609. case PESinkGetSourceCap:
  610. state = pe_sink_get_source_cap(cfg);
  611. break;
  612. case PESinkGiveSinkCap:
  613. state = pe_sink_give_sink_cap(cfg);
  614. break;
  615. case PESinkHardReset:
  616. state = pe_sink_hard_reset(cfg);
  617. break;
  618. case PESinkTransitionDefault:
  619. state = pe_sink_transition_default(cfg);
  620. break;
  621. case PESinkSoftReset:
  622. state = pe_sink_soft_reset(cfg);
  623. break;
  624. case PESinkSendSoftReset:
  625. state = pe_sink_send_soft_reset(cfg);
  626. break;
  627. case PESinkSendReject:
  628. state = pe_sink_send_reject(cfg);
  629. break;
  630. case PESinkSourceUnresponsive:
  631. state = pe_sink_source_unresponsive(cfg);
  632. break;
  633. default:
  634. /* This is an error. It really shouldn't happen. We might
  635. * want to handle it anyway, though. */
  636. state = PESinkStartup;
  637. break;
  638. }
  639. }
  640. }
  641. void pdb_pe_run(struct pdb_config *cfg)
  642. {
  643. cfg->pe.thread = chThdCreateStatic(cfg->pe._wa, sizeof(cfg->pe._wa),
  644. PDB_PRIO_PE, PolicyEngine, cfg);
  645. }