Skip to content

Commit 1b84f2a

Browse files
Javier Martinez Canillasolofj
authored andcommitted
mfd: cros_ec: Use fixed size arrays to transfer data with the EC
The struct cros_ec_command will be used as an ioctl() argument for the API to control the ChromeOS EC from user-space. So the data structure has to be 64-bit safe to make it compatible between 32 and 64 avoiding the need for a compat ioctl interface. Since pointers are self-aligned to different byte boundaries, use fixed size arrays instead of pointers for transferring ingoing and outgoing data with the Embedded Controller. Also, re-arrange struct members by decreasing alignment requirements to reduce the needing padding size. Signed-off-by: Javier Martinez Canillas <javier.martinez@collabora.co.uk> Acked-by: Lee Jones <lee.jones@linaro.org> Tested-by: Gwendal Grignou <gwendal@chromium.org> Reviewed-by: Gwendal Grignou <gwendal@chromium.org> Signed-off-by: Olof Johansson <olof@lixom.net>
1 parent c517d83 commit 1b84f2a

4 files changed

Lines changed: 29 additions & 58 deletions

File tree

‎drivers/i2c/busses/i2c-cros-ec-tunnel.c‎

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -182,72 +182,41 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
182182
const u16 bus_num = bus->remote_bus;
183183
int request_len;
184184
int response_len;
185-
u8 *request = NULL;
186-
u8 *response = NULL;
187185
int result;
188-
struct cros_ec_command msg;
186+
struct cros_ec_command msg = { };
189187

190188
request_len = ec_i2c_count_message(i2c_msgs, num);
191189
if (request_len < 0) {
192190
dev_warn(dev, "Error constructing message %d\n", request_len);
193-
result = request_len;
194-
goto exit;
191+
return request_len;
195192
}
193+
196194
response_len = ec_i2c_count_response(i2c_msgs, num);
197195
if (response_len < 0) {
198196
/* Unexpected; no errors should come when NULL response */
199197
dev_warn(dev, "Error preparing response %d\n", response_len);
200-
result = response_len;
201-
goto exit;
202-
}
203-
204-
if (request_len <= ARRAY_SIZE(bus->request_buf)) {
205-
request = bus->request_buf;
206-
} else {
207-
request = kzalloc(request_len, GFP_KERNEL);
208-
if (request == NULL) {
209-
result = -ENOMEM;
210-
goto exit;
211-
}
212-
}
213-
if (response_len <= ARRAY_SIZE(bus->response_buf)) {
214-
response = bus->response_buf;
215-
} else {
216-
response = kzalloc(response_len, GFP_KERNEL);
217-
if (response == NULL) {
218-
result = -ENOMEM;
219-
goto exit;
220-
}
198+
return response_len;
221199
}
222200

223-
result = ec_i2c_construct_message(request, i2c_msgs, num, bus_num);
201+
result = ec_i2c_construct_message(msg.outdata, i2c_msgs, num, bus_num);
224202
if (result)
225-
goto exit;
203+
return result;
226204

227205
msg.version = 0;
228206
msg.command = EC_CMD_I2C_PASSTHRU;
229-
msg.outdata = request;
230207
msg.outsize = request_len;
231-
msg.indata = response;
232208
msg.insize = response_len;
233209

234210
result = cros_ec_cmd_xfer(bus->ec, &msg);
235211
if (result < 0)
236-
goto exit;
212+
return result;
237213

238-
result = ec_i2c_parse_response(response, i2c_msgs, &num);
214+
result = ec_i2c_parse_response(msg.indata, i2c_msgs, &num);
239215
if (result < 0)
240-
goto exit;
216+
return result;
241217

242218
/* Indicate success by saying how many messages were sent */
243-
result = num;
244-
exit:
245-
if (request != bus->request_buf)
246-
kfree(request);
247-
if (response != bus->response_buf)
248-
kfree(response);
249-
250-
return result;
219+
return num;
251220
}
252221

253222
static u32 ec_i2c_functionality(struct i2c_adapter *adap)

‎drivers/input/keyboard/cros_ec_keyb.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,19 @@ static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
148148

149149
static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
150150
{
151+
int ret;
151152
struct cros_ec_command msg = {
152-
.version = 0,
153153
.command = EC_CMD_MKBP_STATE,
154-
.outdata = NULL,
155-
.outsize = 0,
156-
.indata = kb_state,
157154
.insize = ckdev->cols,
158155
};
159156

160-
return cros_ec_cmd_xfer(ckdev->ec, &msg);
157+
ret = cros_ec_cmd_xfer(ckdev->ec, &msg);
158+
if (ret < 0)
159+
return ret;
160+
161+
memcpy(kb_state, msg.indata, ckdev->cols);
162+
163+
return 0;
161164
}
162165

163166
static irqreturn_t cros_ec_keyb_irq(int irq, void *data)

‎drivers/mfd/cros_ec.c‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,11 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
7474
ret = ec_dev->cmd_xfer(ec_dev, msg);
7575
if (msg->result == EC_RES_IN_PROGRESS) {
7676
int i;
77-
struct cros_ec_command status_msg;
78-
struct ec_response_get_comms_status status;
77+
struct cros_ec_command status_msg = { };
78+
struct ec_response_get_comms_status *status;
7979

80-
status_msg.version = 0;
8180
status_msg.command = EC_CMD_GET_COMMS_STATUS;
82-
status_msg.outdata = NULL;
83-
status_msg.outsize = 0;
84-
status_msg.indata = (uint8_t *)&status;
85-
status_msg.insize = sizeof(status);
81+
status_msg.insize = sizeof(*status);
8682

8783
/*
8884
* Query the EC's status until it's no longer busy or
@@ -98,7 +94,10 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
9894
msg->result = status_msg.result;
9995
if (status_msg.result != EC_RES_SUCCESS)
10096
break;
101-
if (!(status.flags & EC_COMMS_STATUS_PROCESSING))
97+
98+
status = (struct ec_response_get_comms_status *)
99+
status_msg.indata;
100+
if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
102101
break;
103102
}
104103
}

‎include/linux/mfd/cros_ec.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ enum {
3838
/*
3939
* @version: Command version number (often 0)
4040
* @command: Command to send (EC_CMD_...)
41-
* @outdata: Outgoing data to EC
4241
* @outsize: Outgoing length in bytes
43-
* @indata: Where to put the incoming data from EC
4442
* @insize: Max number of bytes to accept from EC
4543
* @result: EC's response to the command (separate from communication failure)
44+
* @outdata: Outgoing data to EC
45+
* @indata: Where to put the incoming data from EC
4646
*/
4747
struct cros_ec_command {
4848
uint32_t version;
4949
uint32_t command;
50-
uint8_t *outdata;
5150
uint32_t outsize;
52-
uint8_t *indata;
5351
uint32_t insize;
5452
uint32_t result;
53+
uint8_t outdata[EC_PROTO2_MAX_PARAM_SIZE];
54+
uint8_t indata[EC_PROTO2_MAX_PARAM_SIZE];
5555
};
5656

5757
/**

0 commit comments

Comments
 (0)