summaryrefslogtreecommitdiff
path: root/zephyr/include/emul/tcpc/emul_tcpci_partner_snk.h
blob: 743cfb157331944326c921db3aabf44678826301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/**
 * @file
 *
 * @brief Backend API for USB-C sink device emulator
 */

#ifndef __EMUL_TCPCI_PARTNER_SNK_H
#define __EMUL_TCPCI_PARTNER_SNK_H

#include <zephyr/drivers/emul.h>
#include "emul/tcpc/emul_tcpci_partner_common.h"
#include "emul/tcpc/emul_tcpci.h"
#include "usb_pd.h"

/**
 * @brief USB-C sink device emulator backend API
 * @defgroup tcpci_snk_emul USB-C sink device emulator
 * @{
 *
 * USB-C sink device emulator can be attached to TCPCI emulator. It is able to
 * respond to some TCPM messages. It always attach as sink and present
 * sink capabilities constructed from given PDOs.
 */

/** Structure describing sink device emulator data */
struct tcpci_snk_emul_data {
	/** Pointer to common TCPCI partner data */
	struct tcpci_partner_data *common_data;
	/** Power data objects returned in sink capabilities message */
	uint32_t pdo[PDO_MAX_OBJECTS];
	/** Emulator is waiting for PS RDY message */
	bool wait_for_ps_rdy;
	/** PS RDY was received and PD negotiation is completed */
	bool pd_completed;
	/** PD_CTRL_PING message received  */
	bool ping_received;
	/** PD_DATA_ALERT message received  */
	bool alert_received;
};

/** Structure describing standalone sink device emulator */
struct tcpci_snk_emul {
	/** Common TCPCI partner data */
	struct tcpci_partner_data common_data;
	/** Operations used by TCPCI emulator */
	struct tcpci_emul_partner_ops ops;
	/** Sink emulator data */
	struct tcpci_snk_emul_data data;
};

/**
 * @brief Initialise USB-C sink device emulator. Need to be called before
 *        any other function that is using common_data.
 *
 * @param emul Pointer to USB-C sink device emulator
 * @param rev The USB-PD revision this port partner supports
 */
void tcpci_snk_emul_init(struct tcpci_snk_emul *emul, enum pd_rev_type rev);

/**
 * @brief Initialise USB-C sink device data structure. Single PDO 5V@500mA is
 *        created and all flags are cleared.
 *
 * @param data Pointer to USB-C sink device emulator data
 * @param common_data Pointer to common TCPCI partner data
 */
void tcpci_snk_emul_init_data(struct tcpci_snk_emul_data *data,
			      struct tcpci_partner_data *common_data);

/**
 * @brief Connect emulated device to TCPCI
 *
 * @param data Pointer to USB-C sink device emulator data
 * @param common_data Pointer to common TCPCI partner data
 * @param ops Pointer to TCPCI partner emulator operations
 * @param tcpci_emul Pointer to TCPCI emulator to connect
 *
 * @return 0 on success
 * @return negative on TCPCI connect error
 */
int tcpci_snk_emul_connect_to_tcpci(struct tcpci_snk_emul_data *data,
				    struct tcpci_partner_data *common_data,
				    const struct tcpci_emul_partner_ops *ops,
				    const struct emul *tcpci_emul);

/**
 * @brief Handle SOP messages as TCPCI sink device. It handles source cap,
 *        get sink cap and ping messages. Accept, Reject and PS_RDY are handled
 *        only if sink emulator send request as response for source cap message
 *        and is waiting for response.
 *
 * @param data Pointer to USB-C sink device emulator data
 * @param common_data Pointer to common TCPCI partner data
 * @param msg Pointer to received message
 *
 * @param TCPCI_PARTNER_COMMON_MSG_HANDLED Message was handled
 * @param TCPCI_PARTNER_COMMON_MSG_NOT_HANDLED Message wasn't handled
 */
enum tcpci_partner_handler_res tcpci_snk_emul_handle_sop_msg(
	struct tcpci_snk_emul_data *data,
	struct tcpci_partner_data *common_data,
	const struct tcpci_emul_msg *msg);

/**
 * @brief Perform action required by sink device on hard reset. Reset sink
 *        specific flags (pd_completed and wait_for_ps_rdy).
 *
 * @param data Pointer to USB-C source device emulator data
 */
void tcpci_snk_emul_hard_reset(void *data);

/**
 * @brief Clear the ping received flag.
 *
 * @param sink_data
 */
void tcpci_snk_emul_clear_ping_received(struct tcpci_snk_emul_data *sink_data);

/**
 * @brief Clear the alert received flag.
 *
 * @param sink_data
 */
void tcpci_snk_emul_clear_alert_received(struct tcpci_snk_emul_data *sink_data);

/**
 * @}
 */

#endif /* __EMUL_TCPCI_PARTNER_SNK_H */