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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. static enum policy_engine_state pe_sink_startup(struct pdb_config *cfg)
  45. {
  46. /* We don't have an explicit contract currently */
  47. cfg->pe._explicit_contract = false;
  48. /* Tell the DPM that we've started negotiations */
  49. cfg->dpm.pd_start(cfg);
  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(cfg)) {
  328. /* Transition to the minimum current level */
  329. cfg->dpm.transition_min(cfg);
  330. cfg->pe._min_power = true;
  331. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  332. cfg->pe._message = NULL;
  333. return PESinkTransitionSink;
  334. } else {
  335. /* We don't support GiveBack, so send a Reject */
  336. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  337. cfg->pe._message = NULL;
  338. return PESinkSendReject;
  339. }
  340. /* Evaluate new Source_Capabilities */
  341. } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  342. && PD_NUMOBJ_GET(cfg->pe._message) > 0) {
  343. /* Don't free the message: we need to keep the
  344. * Source_Capabilities message so we can evaluate it. */
  345. return PESinkEvalCap;
  346. /* Give sink capabilities when asked */
  347. } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_GET_SINK_CAP
  348. && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
  349. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  350. cfg->pe._message = NULL;
  351. return PESinkGiveSinkCap;
  352. /* If the message was a Soft_Reset, do the soft reset procedure */
  353. } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOFT_RESET
  354. && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
  355. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  356. cfg->pe._message = NULL;
  357. return PESinkSoftReset;
  358. /* If we got an unknown message, send a soft reset */
  359. } else {
  360. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  361. cfg->pe._message = NULL;
  362. return PESinkSendSoftReset;
  363. }
  364. }
  365. }
  366. return PESinkReady;
  367. }
  368. static enum policy_engine_state pe_sink_get_source_cap(struct pdb_config *cfg)
  369. {
  370. /* Get a message object */
  371. union pd_msg *get_source_cap = chPoolAlloc(&pdb_msg_pool);
  372. /* Make a Get_Source_Cap message */
  373. get_source_cap->hdr = PD_MSGTYPE_GET_SOURCE_CAP | PD_DATAROLE_UFP
  374. | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  375. /* Transmit the Get_Source_Cap */
  376. chMBPost(&cfg->prl.tx_mailbox, (msg_t) get_source_cap, TIME_IMMEDIATE);
  377. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  378. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  379. | PDB_EVT_PE_RESET);
  380. /* Free the sent message */
  381. chPoolFree(&pdb_msg_pool, get_source_cap);
  382. get_source_cap = NULL;
  383. /* If we got reset signaling, transition to default */
  384. if (evt & PDB_EVT_PE_RESET) {
  385. return PESinkTransitionDefault;
  386. }
  387. /* If the message transmission failed, send a hard reset */
  388. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  389. return PESinkHardReset;
  390. }
  391. return PESinkReady;
  392. }
  393. static enum policy_engine_state pe_sink_give_sink_cap(struct pdb_config *cfg)
  394. {
  395. /* Get a message object */
  396. union pd_msg *snk_cap = chPoolAlloc(&pdb_msg_pool);
  397. /* Get our capabilities from the DPM */
  398. cfg->dpm.get_sink_capability(cfg, snk_cap);
  399. /* Transmit our capabilities */
  400. chMBPost(&cfg->prl.tx_mailbox, (msg_t) snk_cap, TIME_IMMEDIATE);
  401. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  402. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  403. | PDB_EVT_PE_RESET);
  404. /* Free the Sink_Capabilities message */
  405. chPoolFree(&pdb_msg_pool, snk_cap);
  406. snk_cap = NULL;
  407. /* If we got reset signaling, transition to default */
  408. if (evt & PDB_EVT_PE_RESET) {
  409. return PESinkTransitionDefault;
  410. }
  411. /* If the message transmission failed, send a hard reset */
  412. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  413. return PESinkHardReset;
  414. }
  415. return PESinkReady;
  416. }
  417. static enum policy_engine_state pe_sink_hard_reset(struct pdb_config *cfg)
  418. {
  419. /* If we've already sent the maximum number of hard resets, assume the
  420. * source is unresponsive. */
  421. if (cfg->pe._hard_reset_counter > PD_N_HARD_RESET_COUNT) {
  422. return PESinkSourceUnresponsive;
  423. }
  424. /* Generate a hard reset signal */
  425. chEvtSignal(cfg->prl.hardrst_thread, PDB_EVT_HARDRST_RESET);
  426. chEvtWaitAny(PDB_EVT_PE_HARD_SENT);
  427. /* Increment HardResetCounter */
  428. cfg->pe._hard_reset_counter++;
  429. return PESinkTransitionDefault;
  430. }
  431. static enum policy_engine_state pe_sink_transition_default(struct pdb_config *cfg)
  432. {
  433. cfg->pe._explicit_contract = false;
  434. /* Tell the DPM to transition to default power */
  435. cfg->dpm.transition_default(cfg);
  436. /* There is no local hardware to reset. */
  437. /* Since we never change our data role from UFP, there is no reason to set
  438. * it here. */
  439. /* Tell the protocol layer we're done with the reset */
  440. chEvtSignal(cfg->prl.hardrst_thread, PDB_EVT_HARDRST_DONE);
  441. return PESinkStartup;
  442. }
  443. static enum policy_engine_state pe_sink_soft_reset(struct pdb_config *cfg)
  444. {
  445. /* No need to explicitly reset the protocol layer here. It resets itself
  446. * when a Soft_Reset message is received. */
  447. /* Get a message object */
  448. union pd_msg *accept = chPoolAlloc(&pdb_msg_pool);
  449. /* Make an Accept message */
  450. accept->hdr = PD_MSGTYPE_ACCEPT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  451. | PD_POWERROLE_SINK | 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 = PD_MSGTYPE_SOFT_RESET | PD_DATAROLE_UFP | PD_SPECREV_2_0
  478. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  479. /* Transmit the soft reset */
  480. chMBPost(&cfg->prl.tx_mailbox, (msg_t) softrst, TIME_IMMEDIATE);
  481. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  482. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  483. | PDB_EVT_PE_RESET);
  484. /* Free the sent message */
  485. chPoolFree(&pdb_msg_pool, softrst);
  486. softrst = NULL;
  487. /* If we got reset signaling, transition to default */
  488. if (evt & PDB_EVT_PE_RESET) {
  489. return PESinkTransitionDefault;
  490. }
  491. /* If the message transmission failed, send a hard reset */
  492. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  493. return PESinkHardReset;
  494. }
  495. /* Wait for a response */
  496. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET,
  497. PD_T_SENDER_RESPONSE);
  498. /* If we got reset signaling, transition to default */
  499. if (evt & PDB_EVT_PE_RESET) {
  500. return PESinkTransitionDefault;
  501. }
  502. /* If we didn't get a response before the timeout, send a hard reset */
  503. if (evt == 0) {
  504. return PESinkHardReset;
  505. }
  506. /* Get the response message */
  507. if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &cfg->pe._message, TIME_IMMEDIATE) == MSG_OK) {
  508. /* If the source accepted our soft reset, wait for capabilities. */
  509. if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_ACCEPT
  510. && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
  511. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  512. cfg->pe._message = NULL;
  513. return PESinkWaitCap;
  514. /* If the message was a Soft_Reset, do the soft reset procedure */
  515. } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOFT_RESET
  516. && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
  517. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  518. cfg->pe._message = NULL;
  519. return PESinkSoftReset;
  520. /* Otherwise, send a hard reset */
  521. } else {
  522. chPoolFree(&pdb_msg_pool, cfg->pe._message);
  523. cfg->pe._message = NULL;
  524. return PESinkHardReset;
  525. }
  526. }
  527. return PESinkHardReset;
  528. }
  529. static enum policy_engine_state pe_sink_send_reject(struct pdb_config *cfg)
  530. {
  531. /* Get a message object */
  532. union pd_msg *reject = chPoolAlloc(&pdb_msg_pool);
  533. /* Make a Reject message */
  534. reject->hdr = PD_MSGTYPE_REJECT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  535. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  536. /* Transmit the message */
  537. chMBPost(&cfg->prl.tx_mailbox, (msg_t) reject, TIME_IMMEDIATE);
  538. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
  539. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  540. | PDB_EVT_PE_RESET);
  541. /* Free the Reject message */
  542. chPoolFree(&pdb_msg_pool, reject);
  543. reject = NULL;
  544. /* If we got reset signaling, transition to default */
  545. if (evt & PDB_EVT_PE_RESET) {
  546. return PESinkTransitionDefault;
  547. }
  548. /* If the message transmission failed, send a soft reset */
  549. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  550. return PESinkSendSoftReset;
  551. }
  552. return PESinkReady;
  553. }
  554. /*
  555. * When Power Delivery is unresponsive, fall back to Type-C Current
  556. */
  557. static enum policy_engine_state pe_sink_source_unresponsive(struct pdb_config *cfg)
  558. {
  559. static int old_tcc_match = -1;
  560. int tcc_match = cfg->dpm.evaluate_typec_current(cfg, fusb_get_typec_current());
  561. /* If the last two readings are the same, set the output */
  562. if (old_tcc_match == tcc_match) {
  563. cfg->dpm.transition_typec(cfg);
  564. }
  565. /* Remember whether or not the last measurement succeeded */
  566. old_tcc_match = tcc_match;
  567. /* Wait tPDDebounce between measurements */
  568. chThdSleep(PD_T_PD_DEBOUNCE);
  569. return PESinkSourceUnresponsive;
  570. }
  571. /*
  572. * Policy Engine state machine thread
  573. */
  574. static THD_FUNCTION(PolicyEngine, vcfg) {
  575. struct pdb_config *cfg = vcfg;
  576. enum policy_engine_state state = PESinkStartup;
  577. /* Initialize the mailbox */
  578. chMBObjectInit(&cfg->pe.mailbox, cfg->pe._mailbox_queue, PDB_MSG_POOL_SIZE);
  579. while (true) {
  580. switch (state) {
  581. case PESinkStartup:
  582. state = pe_sink_startup(cfg);
  583. break;
  584. case PESinkDiscovery:
  585. state = pe_sink_discovery(cfg);
  586. break;
  587. case PESinkWaitCap:
  588. state = pe_sink_wait_cap(cfg);
  589. break;
  590. case PESinkEvalCap:
  591. state = pe_sink_eval_cap(cfg);
  592. break;
  593. case PESinkSelectCap:
  594. state = pe_sink_select_cap(cfg);
  595. break;
  596. case PESinkTransitionSink:
  597. state = pe_sink_transition_sink(cfg);
  598. break;
  599. case PESinkReady:
  600. state = pe_sink_ready(cfg);
  601. break;
  602. case PESinkGetSourceCap:
  603. state = pe_sink_get_source_cap(cfg);
  604. break;
  605. case PESinkGiveSinkCap:
  606. state = pe_sink_give_sink_cap(cfg);
  607. break;
  608. case PESinkHardReset:
  609. state = pe_sink_hard_reset(cfg);
  610. break;
  611. case PESinkTransitionDefault:
  612. state = pe_sink_transition_default(cfg);
  613. break;
  614. case PESinkSoftReset:
  615. state = pe_sink_soft_reset(cfg);
  616. break;
  617. case PESinkSendSoftReset:
  618. state = pe_sink_send_soft_reset(cfg);
  619. break;
  620. case PESinkSendReject:
  621. state = pe_sink_send_reject(cfg);
  622. break;
  623. case PESinkSourceUnresponsive:
  624. state = pe_sink_source_unresponsive(cfg);
  625. break;
  626. default:
  627. /* This is an error. It really shouldn't happen. We might
  628. * want to handle it anyway, though. */
  629. state = PESinkStartup;
  630. break;
  631. }
  632. }
  633. }
  634. void pdb_pe_run(struct pdb_config *cfg)
  635. {
  636. pdb_pe_thread = chThdCreateStatic(cfg->pe._wa, sizeof(cfg->pe._wa),
  637. PDB_PRIO_PE, PolicyEngine, cfg);
  638. }