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

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