Skip to content

Commit 226ff2e

Browse files
Heikki Krogerusgregkh
authored andcommitted
usb: typec: ucsi: Convert connector specific commands to bitmaps
That allows the fields in those command data structures to be easily validated. If an unsupported field is accessed, a warning is generated. This will not force UCSI version checks to be made in every place where these data structures are accessed, but it will make it easier to pinpoint issues that are caused by the unconditional accesses to those fields, and perhaps more importantly, allow those issues to be noticed immediately. Signed-off-by: Heikki Krogerus <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 474538b commit 226ff2e

File tree

5 files changed

+240
-196
lines changed

5 files changed

+240
-196
lines changed

drivers/usb/typec/ucsi/psy.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ static int ucsi_psy_get_online(struct ucsi_connector *con,
5555
union power_supply_propval *val)
5656
{
5757
val->intval = UCSI_PSY_OFFLINE;
58-
if (con->status.flags & UCSI_CONSTAT_CONNECTED &&
59-
(con->status.flags & UCSI_CONSTAT_PWR_DIR) == TYPEC_SINK)
58+
if (UCSI_CONSTAT(con, CONNECTED) &&
59+
(UCSI_CONSTAT(con, PWR_DIR) == TYPEC_SINK))
6060
val->intval = UCSI_PSY_FIXED_ONLINE;
6161
return 0;
6262
}
@@ -66,7 +66,7 @@ static int ucsi_psy_get_voltage_min(struct ucsi_connector *con,
6666
{
6767
u32 pdo;
6868

69-
switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
69+
switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
7070
case UCSI_CONSTAT_PWR_OPMODE_PD:
7171
pdo = con->src_pdos[0];
7272
val->intval = pdo_fixed_voltage(pdo) * 1000;
@@ -89,7 +89,7 @@ static int ucsi_psy_get_voltage_max(struct ucsi_connector *con,
8989
{
9090
u32 pdo;
9191

92-
switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
92+
switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
9393
case UCSI_CONSTAT_PWR_OPMODE_PD:
9494
if (con->num_pdos > 0) {
9595
pdo = con->src_pdos[con->num_pdos - 1];
@@ -117,7 +117,7 @@ static int ucsi_psy_get_voltage_now(struct ucsi_connector *con,
117117
int index;
118118
u32 pdo;
119119

120-
switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
120+
switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
121121
case UCSI_CONSTAT_PWR_OPMODE_PD:
122122
index = rdo_index(con->rdo);
123123
if (index > 0) {
@@ -145,7 +145,7 @@ static int ucsi_psy_get_current_max(struct ucsi_connector *con,
145145
{
146146
u32 pdo;
147147

148-
switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
148+
switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
149149
case UCSI_CONSTAT_PWR_OPMODE_PD:
150150
if (con->num_pdos > 0) {
151151
pdo = con->src_pdos[con->num_pdos - 1];
@@ -173,9 +173,7 @@ static int ucsi_psy_get_current_max(struct ucsi_connector *con,
173173
static int ucsi_psy_get_current_now(struct ucsi_connector *con,
174174
union power_supply_propval *val)
175175
{
176-
u16 flags = con->status.flags;
177-
178-
if (UCSI_CONSTAT_PWR_OPMODE(flags) == UCSI_CONSTAT_PWR_OPMODE_PD)
176+
if (UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD)
179177
val->intval = rdo_op_current(con->rdo) * 1000;
180178
else
181179
val->intval = 0;
@@ -185,30 +183,28 @@ static int ucsi_psy_get_current_now(struct ucsi_connector *con,
185183
static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
186184
union power_supply_propval *val)
187185
{
188-
u16 flags = con->status.flags;
189-
190186
val->intval = POWER_SUPPLY_USB_TYPE_C;
191-
if (flags & UCSI_CONSTAT_CONNECTED &&
192-
UCSI_CONSTAT_PWR_OPMODE(flags) == UCSI_CONSTAT_PWR_OPMODE_PD)
187+
if (UCSI_CONSTAT(con, CONNECTED) &&
188+
UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD)
193189
val->intval = POWER_SUPPLY_USB_TYPE_PD;
194190

195191
return 0;
196192
}
197193

198194
static int ucsi_psy_get_charge_type(struct ucsi_connector *con, union power_supply_propval *val)
199195
{
200-
if (!(con->status.flags & UCSI_CONSTAT_CONNECTED)) {
196+
if (!(UCSI_CONSTAT(con, CONNECTED))) {
201197
val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
202198
return 0;
203199
}
204200

205201
/* The Battery Charging Cabability Status field is only valid in sink role. */
206-
if ((con->status.flags & UCSI_CONSTAT_PWR_DIR) != TYPEC_SINK) {
202+
if (UCSI_CONSTAT(con, PWR_DIR) != TYPEC_SINK) {
207203
val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
208204
return 0;
209205
}
210206

211-
switch (UCSI_CONSTAT_BC_STATUS(con->status.pwr_status)) {
207+
switch (UCSI_CONSTAT(con, BC_STATUS)) {
212208
case UCSI_CONSTAT_BC_NOMINAL_CHARGING:
213209
val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
214210
break;

drivers/usb/typec/ucsi/trace.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ DEFINE_EVENT(ucsi_log_command, ucsi_reset_ppm,
4040
);
4141

4242
DECLARE_EVENT_CLASS(ucsi_log_connector_status,
43-
TP_PROTO(int port, struct ucsi_connector_status *status),
44-
TP_ARGS(port, status),
43+
TP_PROTO(int port, struct ucsi_connector *con),
44+
TP_ARGS(port, con),
4545
TP_STRUCT__entry(
4646
__field(int, port)
4747
__field(u16, change)
@@ -55,14 +55,14 @@ DECLARE_EVENT_CLASS(ucsi_log_connector_status,
5555
),
5656
TP_fast_assign(
5757
__entry->port = port - 1;
58-
__entry->change = status->change;
59-
__entry->opmode = UCSI_CONSTAT_PWR_OPMODE(status->flags);
60-
__entry->connected = !!(status->flags & UCSI_CONSTAT_CONNECTED);
61-
__entry->pwr_dir = !!(status->flags & UCSI_CONSTAT_PWR_DIR);
62-
__entry->partner_flags = UCSI_CONSTAT_PARTNER_FLAGS(status->flags);
63-
__entry->partner_type = UCSI_CONSTAT_PARTNER_TYPE(status->flags);
64-
__entry->request_data_obj = status->request_data_obj;
65-
__entry->bc_status = UCSI_CONSTAT_BC_STATUS(status->pwr_status);
58+
__entry->change = UCSI_CONSTAT(con, CHANGE);
59+
__entry->opmode = UCSI_CONSTAT(con, PWR_OPMODE);
60+
__entry->connected = UCSI_CONSTAT(con, CONNECTED);
61+
__entry->pwr_dir = UCSI_CONSTAT(con, PWR_DIR);
62+
__entry->partner_flags = UCSI_CONSTAT(con, PARTNER_FLAGS);
63+
__entry->partner_type = UCSI_CONSTAT(con, PARTNER_TYPE);
64+
__entry->request_data_obj = UCSI_CONSTAT(con, RDO);
65+
__entry->bc_status = UCSI_CONSTAT(con, BC_STATUS);
6666
),
6767
TP_printk("port%d status: change=%04x, opmode=%x, connected=%d, "
6868
"sourcing=%d, partner_flags=%x, partner_type=%x, "
@@ -73,13 +73,13 @@ DECLARE_EVENT_CLASS(ucsi_log_connector_status,
7373
);
7474

7575
DEFINE_EVENT(ucsi_log_connector_status, ucsi_connector_change,
76-
TP_PROTO(int port, struct ucsi_connector_status *status),
77-
TP_ARGS(port, status)
76+
TP_PROTO(int port, struct ucsi_connector *con),
77+
TP_ARGS(port, con)
7878
);
7979

8080
DEFINE_EVENT(ucsi_log_connector_status, ucsi_register_port,
81-
TP_PROTO(int port, struct ucsi_connector_status *status),
82-
TP_ARGS(port, status)
81+
TP_PROTO(int port, struct ucsi_connector *con),
82+
TP_ARGS(port, con)
8383
);
8484

8585
DECLARE_EVENT_CLASS(ucsi_log_register_altmode,

0 commit comments

Comments
 (0)