xref: /aosp_15_r20/external/gsc-utils/util/usb_if.h (revision 4f2df630800bdcf1d4f0decf95d8a1cb87344f5f)
1 /*
2  * Copyright 2018 The ChromiumOS Authors
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 #ifndef __EC_EXTRA_USB_UPDATER_USB_IF_H
8 #define __EC_EXTRA_USB_UPDATER_USB_IF_H
9 
10 #include <libusb.h>
11 
12 /* This describes USB endpoint used to communicate with Cr50. */
13 struct usb_endpoint {
14 	struct libusb_device_handle *devh;
15 	uint8_t ep_num;
16 	int chunk_len;
17 };
18 
19 /*
20  * Find the requested USB endpoint. This finds the device using the device
21  * serial number, vendor id, and product id. The subclass and protocol are used
22  * to find the correct endpoint. If a matching endpoint is found, fill up the
23  * uep structure. If succeeded, usb_shut_down() must be invoked before program
24  * exits.
25  *
26  * Return 0 on success, -1 on failure.
27  */
28 int usb_findit(const char *serialno, uint16_t vid, uint16_t *pid, int pid_count,
29 	       uint16_t subclass, uint16_t protocol, struct usb_endpoint *uep);
30 
31 /*
32  * Actual USB transfer function, the 'allow_less' flag indicates that the
33  * valid response could be shorter than allotted memory, the 'rxed_count'
34  * pointer, if provided along with 'allow_less', lets the caller know how many
35  * bytes were received.
36  */
37 int usb_trx(struct usb_endpoint *uep, void *outbuf, int outlen, void *inbuf,
38 	    int inlen, int allow_less, size_t *rxed_count);
39 
40 /*
41  * This function should be called for graceful tear down of the USB interface
42  * when the program exits, either normally or due to error. This is required
43  * only after USB connection was established, i.e. after successful invocation
44  * of usb_findit().
45  */
46 void usb_shut_down(struct usb_endpoint *uep);
47 
48 #define USB_ERROR(m, r)                                                        \
49 	fprintf(stderr, "%s:%d, %s returned %d (%s)\n", __FILE__, __LINE__, m, \
50 		r, libusb_strerror(r))
51 
52 #endif /* ! __EC_EXTRA_USB_UPDATER_USB_IF_H */
53