]> mj.ucw.cz Git - home-hw.git/blob - ucw-libusb/usb-helper.h
Rainbow case: More TODO
[home-hw.git] / ucw-libusb / usb-helper.h
1 /*
2  *      LibUSB helpers for LibUCW
3  *
4  *      (c) 2022--2023 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <libusb-1.0/libusb.h>
8
9 #include <ucw/clists.h>
10 #include <ucw/mainloop.h>
11
12 #define USB_PORT_NAME_LEN 16
13
14 struct device;          // Provided by the user
15
16 enum usb_dev_state {
17         USTATE_INIT,
18         USTATE_RUNNING,
19         USTATE_BROKEN,
20 };
21
22 struct usb_dev {
23         cnode n;
24
25         int bus, dev;                           // -1 if device already unplugged
26         char port[USB_PORT_NAME_LEN];
27         enum usb_dev_state state;
28
29         struct main_timer connect_timer;
30
31         struct libusb_device_handle *devh;
32         struct libusb_device_descriptor desc;
33
34         struct device *device;
35 };
36
37 extern clist usb_dev_list;
38
39 extern uint log_type_usb;
40
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__)
43
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, ...);
46
47 /*** Callbacks provided by the user ***/
48
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);
52
53 // Device was connected, start transfers.
54 void usb_dev_connect(struct usb_dev *u);
55
56 // An error has been detected, cancel all pending transfers.
57 void usb_dev_cancel(struct usb_dev *u);
58
59 // Are there any in-flight transfers?
60 bool usb_dev_in_flight(struct usb_dev *u);
61
62 // Clean up application state, free all usb transfers.
63 void usb_dev_cleanup(struct usb_dev *u);