}
EXPORT_SYMBOL_GPL(ucsi_notify_common);
-int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci,
- void *data, size_t size)
+int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci)
{
bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
int ret;
if (!ret && cci)
ret = ucsi->ops->read_cci(ucsi, cci);
- if (!ret && data &&
+ if (!ret && ucsi->message_in_size > 0 &&
(*cci & UCSI_CCI_COMMAND_COMPLETE))
- ret = ucsi->ops->read_message_in(ucsi, data, size);
+ ret = ucsi->ops->read_message_in(ucsi, ucsi->message_in,
+ ucsi->message_in_size);
return ret;
}
ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
}
- return ucsi->ops->sync_control(ucsi, ctrl, NULL, NULL, 0);
+ ucsi->message_in_size = 0;
+ return ucsi->ops->sync_control(ucsi, ctrl, NULL);
}
-static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci,
- void *data, size_t size, bool conn_ack)
+static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, bool conn_ack)
{
int ret, err;
*cci = 0;
- if (size > UCSI_MAX_DATA_LENGTH(ucsi))
+ if (ucsi->message_in_size > UCSI_MAX_DATA_LENGTH(ucsi))
return -EINVAL;
- ret = ucsi->ops->sync_control(ucsi, command, cci, data, size);
+ ret = ucsi->ops->sync_control(ucsi, command, cci);
- if (*cci & UCSI_CCI_BUSY)
- return ucsi_run_command(ucsi, UCSI_CANCEL, cci, NULL, 0, false) ?: -EBUSY;
+ if (*cci & UCSI_CCI_BUSY) {
+ ucsi->message_in_size = 0;
+ return ucsi_run_command(ucsi, UCSI_CANCEL, cci, false) ?: -EBUSY;
+ }
if (ret)
return ret;
int ret;
command = UCSI_GET_ERROR_STATUS | UCSI_CONNECTOR_NUMBER(connector_num);
- ret = ucsi_run_command(ucsi, command, &cci, &error, sizeof(error), false);
+ ucsi->message_in_size = sizeof(error);
+ ret = ucsi_run_command(ucsi, command, &cci, false);
if (ret < 0)
return ret;
+ memcpy(&error, ucsi->message_in, sizeof(error));
+
switch (error) {
case UCSI_ERROR_INCOMPATIBLE_PARTNER:
return -EOPNOTSUPP;
return -EIO;
}
-static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd,
- void *data, size_t size, bool conn_ack)
+static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd, bool conn_ack)
{
u8 connector_num;
u32 cci;
mutex_lock(&ucsi->ppm_lock);
- ret = ucsi_run_command(ucsi, cmd, &cci, data, size, conn_ack);
+ ret = ucsi_run_command(ucsi, cmd, &cci, conn_ack);
if (cci & UCSI_CCI_ERROR)
ret = ucsi_read_error(ucsi, connector_num);
return ret;
}
-int ucsi_send_command(struct ucsi *ucsi, u64 command,
- void *data, size_t size)
+int ucsi_send_command(struct ucsi *ucsi, u64 command)
{
- return ucsi_send_command_common(ucsi, command, data, size, false);
+ return ucsi_send_command_common(ucsi, command, false);
}
EXPORT_SYMBOL_GPL(ucsi_send_command);
int i;
command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
- ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
+ con->ucsi->message_in_size = sizeof(cur);
+ ret = ucsi_send_command(con->ucsi, command);
if (ret < 0) {
if (con->ucsi->version > 0x0100) {
dev_err(con->ucsi->dev,
return;
}
cur = 0xff;
+ } else {
+ memcpy(&cur, con->ucsi->message_in, sizeof(cur));
}
if (cur < UCSI_MAX_ALTMODES)
command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
command |= UCSI_GET_ALTMODE_OFFSET(i);
- len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
+ ucsi->message_in_size = sizeof(alt);
+ len = ucsi_send_command(con->ucsi, command);
/*
* We are collecting all altmodes first and then registering.
* Some type-C device will return zero length data beyond last
if (len < 0)
return len;
+ memcpy(&alt, ucsi->message_in, sizeof(alt));
+
/* We got all altmodes, now break out and register them */
if (!len || !alt.svid)
break;
command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
command |= UCSI_GET_ALTMODE_OFFSET(i);
- len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
+ con->ucsi->message_in_size = sizeof(alt);
+ len = ucsi_send_command(con->ucsi, command);
if (len == -EBUSY)
continue;
if (len <= 0)
return len;
+ memcpy(&alt, con->ucsi->message_in, sizeof(alt));
+
/*
* This code is requesting one alt mode at a time, but some PPMs
* may still return two. If that happens both alt modes need be
UCSI_MAX_DATA_LENGTH(con->ucsi));
int ret;
- ret = ucsi_send_command_common(con->ucsi, command, &con->status, size, conn_ack);
+ con->ucsi->message_in_size = size;
+ ret = ucsi_send_command_common(con->ucsi, command, conn_ack);
+ memcpy(&con->status, con->ucsi->message_in, size);
return ret < 0 ? ret : 0;
}
command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
- ret = ucsi_send_command(ucsi, command, pdos + offset,
- num_pdos * sizeof(u32));
+ ucsi->message_in_size = num_pdos * sizeof(u32);
+ ret = ucsi_send_command(ucsi, command);
+ memcpy(pdos + offset, ucsi->message_in, num_pdos * sizeof(u32));
if (ret < 0 && ret != -ETIMEDOUT)
dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
command |= UCSI_GET_PD_MESSAGE_BYTES(len);
command |= UCSI_GET_PD_MESSAGE_TYPE(type);
- ret = ucsi_send_command(con->ucsi, command, data + offset, len);
+ con->ucsi->message_in_size = len;
+ ret = ucsi_send_command(con->ucsi, command);
+ memcpy(data + offset, con->ucsi->message_in, len);
if (ret < 0)
return ret;
}
int ret;
command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
- ret = ucsi_send_command(con->ucsi, command, &cable_prop, sizeof(cable_prop));
+ con->ucsi->message_in_size = sizeof(cable_prop);
+ ret = ucsi_send_command(con->ucsi, command);
+ memcpy(&cable_prop, con->ucsi->message_in, sizeof(cable_prop));
if (ret < 0) {
dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", ret);
return ret;
return 0;
command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
- ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap));
+ con->ucsi->message_in_size = sizeof(con->cap);
+ ret = ucsi_send_command(con->ucsi, command);
+ memcpy(&con->cap, con->ucsi->message_in, sizeof(con->cap));
if (ret < 0) {
dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
return ret;
else if (con->ucsi->version >= UCSI_VERSION_2_0)
command |= hard ? 0 : UCSI_CONNECTOR_RESET_DATA_VER_2_0;
- return ucsi_send_command(con->ucsi, command, NULL, 0);
+ con->ucsi->message_in_size = 0;
+ return ucsi_send_command(con->ucsi, command);
}
static int ucsi_reset_ppm(struct ucsi *ucsi)
{
int ret;
- ret = ucsi_send_command(con->ucsi, command, NULL, 0);
+ con->ucsi->message_in_size = 0;
+ ret = ucsi_send_command(con->ucsi, command);
if (ret == -ETIMEDOUT) {
u64 c;
ucsi_reset_ppm(con->ucsi);
c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
- ucsi_send_command(con->ucsi, c, NULL, 0);
+ con->ucsi->message_in_size = 0;
+ ucsi_send_command(con->ucsi, c);
ucsi_reset_connector(con, true);
}
/* Get connector capability */
command = UCSI_GET_CONNECTOR_CAPABILITY;
command |= UCSI_CONNECTOR_NUMBER(con->num);
- ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
+ ucsi->message_in_size = sizeof(con->cap);
+ ret = ucsi_send_command(ucsi, command);
if (ret < 0)
goto out_unlock;
+ memcpy(&con->cap, ucsi->message_in, sizeof(con->cap));
+
if (UCSI_CONCAP(con, OPMODE_DRP))
cap->data = TYPEC_PORT_DRD;
else if (UCSI_CONCAP(con, OPMODE_DFP))
/* Enable basic notifications */
ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
- ret = ucsi_send_command(ucsi, command, NULL, 0);
+ ucsi->message_in_size = 0;
+ ret = ucsi_send_command(ucsi, command);
if (ret < 0)
goto err_reset;
/* Get PPM capabilities */
command = UCSI_GET_CAPABILITY;
- ret = ucsi_send_command(ucsi, command, &ucsi->cap,
- BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE));
+ ucsi->message_in_size = BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE);
+ ret = ucsi_send_command(ucsi, command);
if (ret < 0)
goto err_reset;
+ memcpy(&ucsi->cap, ucsi->message_in, BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE));
+
if (!ucsi->cap.num_connectors) {
ret = -ENODEV;
goto err_reset;
/* Enable all supported notifications */
ntfy = ucsi_get_supported_notifications(ucsi);
command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
- ret = ucsi_send_command(ucsi, command, NULL, 0);
+ ucsi->message_in_size = 0;
+ ret = ucsi_send_command(ucsi, command);
if (ret < 0)
goto err_unregister;
/* Restore UCSI notification enable mask after system resume */
command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
- ret = ucsi_send_command(ucsi, command, NULL, 0);
+ ucsi->message_in_size = 0;
+ ret = ucsi_send_command(ucsi, command);
if (ret < 0) {
dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
return;
#define UCSI_MESSAGE_OUT 32
#define UCSIv2_MESSAGE_OUT 272
+/* Define maximum lengths for message buffers */
+#define UCSI_MAX_MESSAGE_IN_LENGTH 256
+#define UCSI_MAX_MESSAGE_OUT_LENGTH 256
+
/* UCSI versions */
#define UCSI_VERSION_1_0 0x0100
#define UCSI_VERSION_1_1 0x0110
int (*read_cci)(struct ucsi *ucsi, u32 *cci);
int (*poll_cci)(struct ucsi *ucsi, u32 *cci);
int (*read_message_in)(struct ucsi *ucsi, void *val, size_t val_len);
- int (*sync_control)(struct ucsi *ucsi, u64 command, u32 *cci,
- void *data, size_t size);
+ int (*sync_control)(struct ucsi *ucsi, u64 command, u32 *cci);
int (*async_control)(struct ucsi *ucsi, u64 command);
bool (*update_altmodes)(struct ucsi *ucsi, u8 recipient,
struct ucsi_altmode *orig,
unsigned long quirks;
#define UCSI_NO_PARTNER_PDOS BIT(0) /* Don't read partner's PDOs */
#define UCSI_DELAY_DEVICE_PDOS BIT(1) /* Reading PDOs fails until the parter is in PD mode */
+
+ /* Fixed-size buffers for incoming and outgoing messages */
+ u8 message_in[UCSI_MAX_MESSAGE_IN_LENGTH];
+ size_t message_in_size;
+ u8 message_out[UCSI_MAX_MESSAGE_OUT_LENGTH];
+ size_t message_out_size;
};
#define UCSI_MAX_DATA_LENGTH(u) (((u)->version < UCSI_VERSION_2_0) ? 0x10 : 0xff)
struct usb_pd_identity cable_identity;
};
-int ucsi_send_command(struct ucsi *ucsi, u64 command,
- void *retval, size_t size);
+int ucsi_send_command(struct ucsi *ucsi, u64 command);
void ucsi_altmode_update_active(struct ucsi_connector *con);
int ucsi_resume(struct ucsi *ucsi);
void ucsi_notify_common(struct ucsi *ucsi, u32 cci);
-int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci,
- void *data, size_t size);
+int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci);
#if IS_ENABLED(CONFIG_POWER_SUPPLY)
int ucsi_register_port_psy(struct ucsi_connector *con);