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

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