2 * LibUSB helpers for LibUCW
4 * (c) 2022--2023 Martin Mares <mj@ucw.cz>
7 #include <libusb-1.0/libusb.h>
9 #include <ucw/clists.h>
10 #include <ucw/mainloop.h>
12 #define USB_PORT_NAME_LEN 16
14 struct device; // Provided by the user
25 int bus, dev; // -1 if device already unplugged
26 char port[USB_PORT_NAME_LEN];
27 enum usb_dev_state state;
29 struct main_timer connect_timer;
31 struct libusb_device_handle *devh;
32 struct libusb_device_descriptor desc;
34 struct device *device;
37 extern clist usb_dev_list;
39 extern uint log_type_usb;
41 #define USB_MSG(usb_dev, level, fmt, ...) msg(level, "%s: " fmt, usb_dev->port, ##__VA_ARGS__)
42 #define USB_DBG(usb_dev, fmt, ...) msg(L_DEBUG | log_type_usb, "%s: " fmt, usb_dev->port, ##__VA_ARGS__)
44 void usb_helper_init(uint vendor_id, uint device_id);
45 void FORMAT_CHECK(printf,2,3) usb_error(struct usb_dev *u, const char *fmt, ...);
47 /*** Callbacks provided by the user ***/
49 // A new device was detected. Read its configuration and check if it can
50 // be handled. Allocate transfers. But do not start them until usb_dev_connect().
51 bool usb_dev_init(struct usb_dev *u);
53 // Device was connected, start transfers.
54 void usb_dev_connect(struct usb_dev *u);
56 // An error has been detected, cancel all pending transfers.
57 void usb_dev_cancel(struct usb_dev *u);
59 // Are there any in-flight transfers?
60 bool usb_dev_in_flight(struct usb_dev *u);
62 // Clean up application state, free all usb transfers.
63 void usb_dev_cleanup(struct usb_dev *u);