PD Buddy Sink Firmware
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

policy_engine.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "policy_engine.h"
  19. #include <stdbool.h>
  20. #include "messages.h"
  21. #include "priorities.h"
  22. #include "device_policy_manager.h"
  23. #include "protocol_tx.h"
  24. #include "hard_reset.h"
  25. #include "pd.h"
  26. thread_t *pdb_pe_thread;
  27. enum policy_engine_state {
  28. PESinkStartup,
  29. PESinkDiscovery,
  30. PESinkWaitCap,
  31. PESinkEvalCap,
  32. PESinkSelectCap,
  33. PESinkTransitionSink,
  34. PESinkReady,
  35. PESinkGetSourceCap,
  36. PESinkGiveSinkCap,
  37. PESinkHardReset,
  38. PESinkTransitionDefault,
  39. PESinkSoftReset,
  40. PESinkSendSoftReset,
  41. PESinkSendReject,
  42. PESinkSourceUnresponsive
  43. };
  44. /* The received message we're currently working with */
  45. static union pd_msg *policy_engine_message = NULL;
  46. /* The most recent Request from the DPM */
  47. static union pd_msg *last_dpm_request = NULL;
  48. /* Whether or not the source capabilities match our required power */
  49. static bool capability_match = false;
  50. /* Whether or not we have an explicit contract */
  51. static bool explicit_contract = false;
  52. /* Whether or not we're receiving minimum power*/
  53. static bool min_power = false;
  54. /* Keep track of how many hard resets we've sent */
  55. static int hard_reset_counter = 0;
  56. /* Policy Engine thread mailbox */
  57. static msg_t pdb_pe_mailbox_queue[PDB_MSG_POOL_SIZE];
  58. mailbox_t pdb_pe_mailbox;
  59. static enum policy_engine_state pe_sink_startup(void)
  60. {
  61. /* We don't have an explicit contract currently */
  62. explicit_contract = false;
  63. /* Tell the DPM that we've started negotiations */
  64. pdb_dpm_pd_start();
  65. /* No need to reset the protocol layer here. There are two ways into this
  66. * state: startup and exiting hard reset. On startup, the protocol layer
  67. * is reset by the startup procedure. When exiting hard reset, the
  68. * protocol layer is reset by the hard reset state machine. Since it's
  69. * already done somewhere else, there's no need to do it again here. */
  70. return PESinkDiscovery;
  71. }
  72. static enum policy_engine_state pe_sink_discovery(void)
  73. {
  74. /* Wait for VBUS. Since it's our only power source, we already know that
  75. * we have it, so just move on. */
  76. return PESinkWaitCap;
  77. }
  78. static enum policy_engine_state pe_sink_wait_cap(void)
  79. {
  80. /* Fetch a message from the protocol layer */
  81. eventmask_t evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX
  82. | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_RESET, PD_T_TYPEC_SINK_WAIT_CAP);
  83. /* If we timed out waiting for Source_Capabilities, send a hard reset */
  84. if (evt == 0) {
  85. return PESinkHardReset;
  86. }
  87. /* If we got reset signaling, transition to default */
  88. if (evt & PDB_EVT_PE_RESET) {
  89. return PESinkTransitionDefault;
  90. }
  91. /* If we're too hot, we shouldn't negotiate power yet */
  92. if (evt & PDB_EVT_PE_I_OVRTEMP) {
  93. return PESinkWaitCap;
  94. }
  95. /* If we got a message */
  96. if (evt & PDB_EVT_PE_MSG_RX) {
  97. /* Get the message */
  98. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  99. /* If we got a Source_Capabilities message, read it. */
  100. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  101. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  102. return PESinkEvalCap;
  103. /* If the message was a Soft_Reset, do the soft reset procedure */
  104. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  105. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  106. chPoolFree(&pdb_msg_pool, policy_engine_message);
  107. policy_engine_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, policy_engine_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(void)
  121. {
  122. /* Get a message object for the request if we don't have one already */
  123. if (last_dpm_request == NULL) {
  124. last_dpm_request = chPoolAlloc(&pdb_msg_pool);
  125. }
  126. /* Ask the DPM what to request */
  127. capability_match = pdb_dpm_evaluate_capability(policy_engine_message,
  128. last_dpm_request);
  129. /* Free the Source_Capabilities message */
  130. chPoolFree(&pdb_msg_pool, policy_engine_message);
  131. return PESinkSelectCap;
  132. }
  133. static enum policy_engine_state pe_sink_select_cap(void)
  134. {
  135. /* Transmit the request */
  136. chMBPost(&pdb_prltx_mailbox, (msg_t) last_dpm_request, TIME_IMMEDIATE);
  137. chEvtSignal(pdb_prltx_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(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  162. /* If the source accepted our request, wait for the new power */
  163. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  164. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  165. /* Transition to Sink Standby if necessary */
  166. pdb_dpm_sink_standby();
  167. min_power = false;
  168. chPoolFree(&pdb_msg_pool, policy_engine_message);
  169. policy_engine_message = NULL;
  170. return PESinkTransitionSink;
  171. /* If the message was a Soft_Reset, do the soft reset procedure */
  172. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  173. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  174. chPoolFree(&pdb_msg_pool, policy_engine_message);
  175. policy_engine_message = NULL;
  176. return PESinkSoftReset;
  177. /* If the message was Wait or Reject */
  178. } else if ((PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REJECT
  179. || PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT)
  180. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  181. /* If we don't have an explicit contract, wait for capabilities */
  182. if (!explicit_contract) {
  183. chPoolFree(&pdb_msg_pool, policy_engine_message);
  184. policy_engine_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. min_power = (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT);
  191. chPoolFree(&pdb_msg_pool, policy_engine_message);
  192. policy_engine_message = NULL;
  193. return PESinkReady;
  194. }
  195. } else {
  196. chPoolFree(&pdb_msg_pool, policy_engine_message);
  197. policy_engine_message = NULL;
  198. return PESinkSendSoftReset;
  199. }
  200. }
  201. return PESinkHardReset;
  202. }
  203. static enum policy_engine_state pe_sink_transition_sink(void)
  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(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  218. /* If we got a PS_RDY, handle it */
  219. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PS_RDY
  220. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  221. /* We just finished negotiating an explicit contract */
  222. explicit_contract = true;
  223. /* Set the output appropriately */
  224. pdb_dpm_output_set(capability_match && !min_power);
  225. chPoolFree(&pdb_msg_pool, policy_engine_message);
  226. policy_engine_message = NULL;
  227. return PESinkReady;
  228. /* If there was a protocol error, send a hard reset */
  229. } else {
  230. /* Turn off the power output before this hard reset to make sure we
  231. * don't supply an incorrect voltage to the device we're powering.
  232. */
  233. pdb_dpm_output_set(false);
  234. chPoolFree(&pdb_msg_pool, policy_engine_message);
  235. policy_engine_message = NULL;
  236. return PESinkHardReset;
  237. }
  238. }
  239. return PESinkHardReset;
  240. }
  241. static enum policy_engine_state pe_sink_ready(void)
  242. {
  243. eventmask_t evt;
  244. /* Wait for an event */
  245. if (min_power) {
  246. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
  247. | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP,
  248. PD_T_SINK_REQUEST);
  249. } else {
  250. evt = chEvtWaitAny(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
  251. | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP);
  252. }
  253. /* If we got reset signaling, transition to default */
  254. if (evt & PDB_EVT_PE_RESET) {
  255. return PESinkTransitionDefault;
  256. }
  257. /* If we overheated, send a hard reset */
  258. if (evt & PDB_EVT_PE_I_OVRTEMP) {
  259. return PESinkHardReset;
  260. }
  261. /* If the DPM wants us to, send a Get_Source_Cap message */
  262. if (evt & PDB_EVT_PE_GET_SOURCE_CAP) {
  263. return PESinkGetSourceCap;
  264. }
  265. /* If no event was received, the timer ran out. */
  266. if (evt == 0) {
  267. /* Repeat our Request message */
  268. return PESinkSelectCap;
  269. }
  270. /* If we received a message */
  271. if (evt & PDB_EVT_PE_MSG_RX) {
  272. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  273. /* Ignore vendor-defined messages */
  274. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VENDOR_DEFINED
  275. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  276. chPoolFree(&pdb_msg_pool, policy_engine_message);
  277. policy_engine_message = NULL;
  278. return PESinkReady;
  279. /* Ignore Ping messages */
  280. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PING
  281. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  282. chPoolFree(&pdb_msg_pool, policy_engine_message);
  283. policy_engine_message = NULL;
  284. return PESinkReady;
  285. /* Reject DR_Swap messages */
  286. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_DR_SWAP
  287. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  288. chPoolFree(&pdb_msg_pool, policy_engine_message);
  289. policy_engine_message = NULL;
  290. return PESinkSendReject;
  291. /* Reject Get_Source_Cap messages */
  292. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SOURCE_CAP
  293. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  294. chPoolFree(&pdb_msg_pool, policy_engine_message);
  295. policy_engine_message = NULL;
  296. return PESinkSendReject;
  297. /* Reject PR_Swap messages */
  298. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PR_SWAP
  299. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  300. chPoolFree(&pdb_msg_pool, policy_engine_message);
  301. policy_engine_message = NULL;
  302. return PESinkSendReject;
  303. /* Reject VCONN_Swap messages */
  304. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VCONN_SWAP
  305. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  306. chPoolFree(&pdb_msg_pool, policy_engine_message);
  307. policy_engine_message = NULL;
  308. return PESinkSendReject;
  309. /* Reject Request messages */
  310. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REQUEST
  311. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  312. chPoolFree(&pdb_msg_pool, policy_engine_message);
  313. policy_engine_message = NULL;
  314. return PESinkSendReject;
  315. /* Reject Sink_Capabilities messages */
  316. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SINK_CAPABILITIES
  317. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  318. chPoolFree(&pdb_msg_pool, policy_engine_message);
  319. policy_engine_message = NULL;
  320. return PESinkSendReject;
  321. /* Handle GotoMin messages */
  322. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GOTOMIN
  323. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  324. if (pdb_dpm_giveback_enabled()) {
  325. /* We support GiveBack, so go to minimum power */
  326. pdb_dpm_output_set(false);
  327. min_power = true;
  328. chPoolFree(&pdb_msg_pool, policy_engine_message);
  329. policy_engine_message = NULL;
  330. return PESinkTransitionSink;
  331. } else {
  332. /* We don't support GiveBack, so send a Reject */
  333. chPoolFree(&pdb_msg_pool, policy_engine_message);
  334. policy_engine_message = NULL;
  335. return PESinkSendReject;
  336. }
  337. /* Evaluate new Source_Capabilities */
  338. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  339. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  340. /* Don't free the message: we need to keep the
  341. * Source_Capabilities message so we can evaluate it. */
  342. return PESinkEvalCap;
  343. /* Give sink capabilities when asked */
  344. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SINK_CAP
  345. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  346. chPoolFree(&pdb_msg_pool, policy_engine_message);
  347. policy_engine_message = NULL;
  348. return PESinkGiveSinkCap;
  349. /* If the message was a Soft_Reset, do the soft reset procedure */
  350. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  351. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  352. chPoolFree(&pdb_msg_pool, policy_engine_message);
  353. policy_engine_message = NULL;
  354. return PESinkSoftReset;
  355. /* If we got an unknown message, send a soft reset */
  356. } else {
  357. chPoolFree(&pdb_msg_pool, policy_engine_message);
  358. policy_engine_message = NULL;
  359. return PESinkSendSoftReset;
  360. }
  361. }
  362. }
  363. return PESinkReady;
  364. }
  365. static enum policy_engine_state pe_sink_get_source_cap(void)
  366. {
  367. /* Get a message object */
  368. union pd_msg *get_source_cap = chPoolAlloc(&pdb_msg_pool);
  369. /* Make a Get_Source_Cap message */
  370. get_source_cap->hdr = PD_MSGTYPE_GET_SOURCE_CAP | PD_DATAROLE_UFP
  371. | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  372. /* Transmit the Get_Source_Cap */
  373. chMBPost(&pdb_prltx_mailbox, (msg_t) get_source_cap, TIME_IMMEDIATE);
  374. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  375. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  376. | PDB_EVT_PE_RESET);
  377. /* Free the sent message */
  378. chPoolFree(&pdb_msg_pool, get_source_cap);
  379. get_source_cap = NULL;
  380. /* If we got reset signaling, transition to default */
  381. if (evt & PDB_EVT_PE_RESET) {
  382. return PESinkTransitionDefault;
  383. }
  384. /* If the message transmission failed, send a hard reset */
  385. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  386. return PESinkHardReset;
  387. }
  388. return PESinkReady;
  389. }
  390. static enum policy_engine_state pe_sink_give_sink_cap(void)
  391. {
  392. /* Get a message object */
  393. union pd_msg *snk_cap = chPoolAlloc(&pdb_msg_pool);
  394. /* Get our capabilities from the DPM */
  395. pdb_dpm_get_sink_capability(snk_cap);
  396. /* Transmit our capabilities */
  397. chMBPost(&pdb_prltx_mailbox, (msg_t) snk_cap, TIME_IMMEDIATE);
  398. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  399. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  400. | PDB_EVT_PE_RESET);
  401. /* Free the Sink_Capabilities message */
  402. chPoolFree(&pdb_msg_pool, snk_cap);
  403. snk_cap = NULL;
  404. /* If we got reset signaling, transition to default */
  405. if (evt & PDB_EVT_PE_RESET) {
  406. return PESinkTransitionDefault;
  407. }
  408. /* If the message transmission failed, send a hard reset */
  409. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  410. return PESinkHardReset;
  411. }
  412. return PESinkReady;
  413. }
  414. static enum policy_engine_state pe_sink_hard_reset(void)
  415. {
  416. /* If we've already sent the maximum number of hard resets, assume the
  417. * source is unresponsive. */
  418. if (hard_reset_counter > PD_N_HARD_RESET_COUNT) {
  419. return PESinkSourceUnresponsive;
  420. }
  421. /* Generate a hard reset signal */
  422. chEvtSignal(pdb_hardrst_thread, PDB_EVT_HARDRST_RESET);
  423. chEvtWaitAny(PDB_EVT_PE_HARD_SENT);
  424. /* Increment HardResetCounter */
  425. hard_reset_counter++;
  426. return PESinkTransitionDefault;
  427. }
  428. static enum policy_engine_state pe_sink_transition_default(void)
  429. {
  430. explicit_contract = false;
  431. /* Tell the DPM to transition to default power */
  432. pdb_dpm_output_default();
  433. /* There is no local hardware to reset. */
  434. /* Since we never change our data role from UFP, there is no reason to set
  435. * it here. */
  436. /* Tell the protocol layer we're done with the reset */
  437. chEvtSignal(pdb_hardrst_thread, PDB_EVT_HARDRST_DONE);
  438. return PESinkStartup;
  439. }
  440. static enum policy_engine_state pe_sink_soft_reset(void)
  441. {
  442. /* No need to explicitly reset the protocol layer here. It resets itself
  443. * when a Soft_Reset message is received. */
  444. /* Get a message object */
  445. union pd_msg *accept = chPoolAlloc(&pdb_msg_pool);
  446. /* Make an Accept message */
  447. accept->hdr = PD_MSGTYPE_ACCEPT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  448. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  449. /* Transmit the Accept */
  450. chMBPost(&pdb_prltx_mailbox, (msg_t) accept, TIME_IMMEDIATE);
  451. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  452. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  453. | PDB_EVT_PE_RESET);
  454. /* Free the sent message */
  455. chPoolFree(&pdb_msg_pool, accept);
  456. accept = NULL;
  457. /* If we got reset signaling, transition to default */
  458. if (evt & PDB_EVT_PE_RESET) {
  459. return PESinkTransitionDefault;
  460. }
  461. /* If the message transmission failed, send a hard reset */
  462. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  463. return PESinkHardReset;
  464. }
  465. return PESinkWaitCap;
  466. }
  467. static enum policy_engine_state pe_sink_send_soft_reset(void)
  468. {
  469. /* No need to explicitly reset the protocol layer here. It resets itself
  470. * just before a Soft_Reset message is transmitted. */
  471. /* Get a message object */
  472. union pd_msg *softrst = chPoolAlloc(&pdb_msg_pool);
  473. /* Make a Soft_Reset message */
  474. softrst->hdr = PD_MSGTYPE_SOFT_RESET | PD_DATAROLE_UFP | PD_SPECREV_2_0
  475. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  476. /* Transmit the soft reset */
  477. chMBPost(&pdb_prltx_mailbox, (msg_t) softrst, TIME_IMMEDIATE);
  478. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  479. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  480. | PDB_EVT_PE_RESET);
  481. /* Free the sent message */
  482. chPoolFree(&pdb_msg_pool, softrst);
  483. softrst = NULL;
  484. /* If we got reset signaling, transition to default */
  485. if (evt & PDB_EVT_PE_RESET) {
  486. return PESinkTransitionDefault;
  487. }
  488. /* If the message transmission failed, send a hard reset */
  489. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  490. return PESinkHardReset;
  491. }
  492. /* Wait for a response */
  493. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET,
  494. PD_T_SENDER_RESPONSE);
  495. /* If we got reset signaling, transition to default */
  496. if (evt & PDB_EVT_PE_RESET) {
  497. return PESinkTransitionDefault;
  498. }
  499. /* If we didn't get a response before the timeout, send a hard reset */
  500. if (evt == 0) {
  501. return PESinkHardReset;
  502. }
  503. /* Get the response message */
  504. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  505. /* If the source accepted our soft reset, wait for capabilities. */
  506. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  507. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  508. chPoolFree(&pdb_msg_pool, policy_engine_message);
  509. policy_engine_message = NULL;
  510. return PESinkWaitCap;
  511. /* If the message was a Soft_Reset, do the soft reset procedure */
  512. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  513. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  514. chPoolFree(&pdb_msg_pool, policy_engine_message);
  515. policy_engine_message = NULL;
  516. return PESinkSoftReset;
  517. /* Otherwise, send a hard reset */
  518. } else {
  519. chPoolFree(&pdb_msg_pool, policy_engine_message);
  520. policy_engine_message = NULL;
  521. return PESinkHardReset;
  522. }
  523. }
  524. return PESinkHardReset;
  525. }
  526. static enum policy_engine_state pe_sink_send_reject(void)
  527. {
  528. /* Get a message object */
  529. union pd_msg *reject = chPoolAlloc(&pdb_msg_pool);
  530. /* Make a Reject message */
  531. reject->hdr = PD_MSGTYPE_REJECT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  532. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  533. /* Transmit the message */
  534. chMBPost(&pdb_prltx_mailbox, (msg_t) reject, TIME_IMMEDIATE);
  535. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  536. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
  537. | PDB_EVT_PE_RESET);
  538. /* Free the Reject message */
  539. chPoolFree(&pdb_msg_pool, reject);
  540. reject = NULL;
  541. /* If we got reset signaling, transition to default */
  542. if (evt & PDB_EVT_PE_RESET) {
  543. return PESinkTransitionDefault;
  544. }
  545. /* If the message transmission failed, send a soft reset */
  546. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  547. return PESinkSendSoftReset;
  548. }
  549. return PESinkReady;
  550. }
  551. /*
  552. * When Power Delivery is unresponsive, fall back to Type-C Current
  553. */
  554. static enum policy_engine_state pe_sink_source_unresponsive(void)
  555. {
  556. static int old_tcc_match = -1;
  557. int tcc_match = pdb_dpm_evaluate_typec_current();
  558. /* If the last two readings are the same, set the output */
  559. if (old_tcc_match == tcc_match) {
  560. pdb_dpm_output_set(tcc_match);
  561. }
  562. /* Remember whether or not the last measurement succeeded */
  563. old_tcc_match = tcc_match;
  564. /* Wait tPDDebounce between measurements */
  565. chThdSleep(PD_T_PD_DEBOUNCE);
  566. return PESinkSourceUnresponsive;
  567. }
  568. /*
  569. * Policy Engine state machine thread
  570. */
  571. static THD_WORKING_AREA(waPolicyEngine, 128);
  572. static THD_FUNCTION(PolicyEngine, arg) {
  573. (void) arg;
  574. enum policy_engine_state state = PESinkStartup;
  575. /* Initialize the mailbox */
  576. chMBObjectInit(&pdb_pe_mailbox, pdb_pe_mailbox_queue, PDB_MSG_POOL_SIZE);
  577. while (true) {
  578. switch (state) {
  579. case PESinkStartup:
  580. state = pe_sink_startup();
  581. break;
  582. case PESinkDiscovery:
  583. state = pe_sink_discovery();
  584. break;
  585. case PESinkWaitCap:
  586. state = pe_sink_wait_cap();
  587. break;
  588. case PESinkEvalCap:
  589. state = pe_sink_eval_cap();
  590. break;
  591. case PESinkSelectCap:
  592. state = pe_sink_select_cap();
  593. break;
  594. case PESinkTransitionSink:
  595. state = pe_sink_transition_sink();
  596. break;
  597. case PESinkReady:
  598. state = pe_sink_ready();
  599. break;
  600. case PESinkGetSourceCap:
  601. state = pe_sink_get_source_cap();
  602. break;
  603. case PESinkGiveSinkCap:
  604. state = pe_sink_give_sink_cap();
  605. break;
  606. case PESinkHardReset:
  607. state = pe_sink_hard_reset();
  608. break;
  609. case PESinkTransitionDefault:
  610. state = pe_sink_transition_default();
  611. break;
  612. case PESinkSoftReset:
  613. state = pe_sink_soft_reset();
  614. break;
  615. case PESinkSendSoftReset:
  616. state = pe_sink_send_soft_reset();
  617. break;
  618. case PESinkSendReject:
  619. state = pe_sink_send_reject();
  620. break;
  621. case PESinkSourceUnresponsive:
  622. state = pe_sink_source_unresponsive();
  623. break;
  624. default:
  625. /* This is an error. It really shouldn't happen. We might
  626. * want to handle it anyway, though. */
  627. state = PESinkStartup;
  628. break;
  629. }
  630. }
  631. }
  632. void pdb_pe_run(void)
  633. {
  634. pdb_pe_thread = chThdCreateStatic(waPolicyEngine, sizeof(waPolicyEngine),
  635. PDB_PRIO_PE, PolicyEngine, NULL);
  636. }