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

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