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

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