diff options
author | Daisuke Nojiri <dnojiri@chromium.org> | 2022-09-12 12:03:18 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-09-13 17:10:16 +0000 |
commit | e780253aceceee70d83470bf6d23cb017fa968c1 (patch) | |
tree | ee6f9b0fe63ee17d4e9cff6d6ccd4771a1eb284e | |
parent | e8fdcaa03f1057fae35abc15d3d9259b6e469329 (diff) | |
download | chrome-ec-e780253aceceee70d83470bf6d23cb017fa968c1.tar.gz |
comm-usb: Make ec_command_usb return response data size
ec_command_usb currently returns the out data size (i.e. request packet
size) but ec_command_proto is supposed to return a response data size
(excluding the response header size). This is causing comm_init_buffer
always fail to set ec_max_insize to the value expected by the EC.
This patch fixes ec_command_usb to fix the issue.
This patch also fixes the followings:
* Check libusb_bulk_transfer's return value strictly (0 for success).
* Make debug() write to stderr.
BUG=b:233404167
BRANCH=None
TEST=Vell. Run ectool --device 18d1:5022 console.
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Change-Id: Ifb33c76d65f033a3f68018fb77f492b65493e155
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3891677
Commit-Queue: Parth Malkan <parthmalkan@google.com>
Reviewed-by: Parth Malkan <parthmalkan@google.com>
-rw-r--r-- | util/comm-usb.c | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/util/comm-usb.c b/util/comm-usb.c index ac0c1675fd..61d13146af 100644 --- a/util/comm-usb.c +++ b/util/comm-usb.c @@ -22,7 +22,8 @@ r, libusb_strerror(r)) #ifdef DEBUG -#define debug(fmt, arg...) printf("%s:%d: " fmt, __FILE__, __LINE__, ##arg) +#define debug(fmt, arg...) \ + fprintf(stderr, "%s:%d: " fmt, __FILE__, __LINE__, ##arg) #else #define debug(...) #endif @@ -53,7 +54,8 @@ void comm_usb_exit(void) * Actual USB transfer function, <allow_less> indicates that a valid response * (e.g. EC_CMD_GET_BUILD_INFO) could be shorter than <inlen>. * - * Returns enum libusb_error (< 0) or -EECRESULT on error, <outlen> on success. + * Returns enum libusb_error (< 0) or -EECRESULT on error. On success, returns + * actually transferred OUT data size or IN data size if read is performed. */ static int do_xfer(struct usb_endpoint *uep, void *outbuf, int outlen, void *inbuf, int inlen, int allow_less) @@ -65,7 +67,7 @@ static int do_xfer(struct usb_endpoint *uep, void *outbuf, int outlen, actual = 0; r = libusb_bulk_transfer(uep->devh, uep->ep_num, outbuf, outlen, &actual, 2000); - if (r < 0) { + if (r != 0) { USB_ERROR("libusb_bulk_transfer", r); return r; } @@ -75,14 +77,18 @@ static int do_xfer(struct usb_endpoint *uep, void *outbuf, int outlen, return -EECRESULT; } } - debug("Sent %d bytes, expecting %d bytes.\n", outlen, inlen); + debug("Sent %d bytes, expecting to receive %d bytes.\n", outlen, inlen); /* Read reply back */ if (inbuf && inlen) { actual = 0; + /* + * libusb_bulk_transfer may time out if actual < inlen and + * actual is a multiple of ep->wMaxPacketSize. + */ r = libusb_bulk_transfer(uep->devh, uep->ep_num | USB_DIR_IN, inbuf, inlen, &actual, 5000); - if (r < 0) { + if (r != 0) { USB_ERROR("libusb_bulk_transfer", r); return r; } @@ -93,8 +99,10 @@ static int do_xfer(struct usb_endpoint *uep, void *outbuf, int outlen, } } - debug("Received %d bytes.\n", inlen); - return outlen; + debug("Received %d bytes.\n", actual); + + /* actual is useful for allow_less. */ + return actual; } /* Return iface # or -1 if not found. */ @@ -314,9 +322,10 @@ static int ec_command_usb(int command, int version, const void *outdata, if (indata) memcpy(indata, &res[1], insize); - if (res->result != EC_RES_SUCCESS) { + if (res->result == EC_RES_SUCCESS) + rv = res->data_len; + else rv = -EECRESULT - res->result; - } out: if (req) |