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

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