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

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