|
27 | 27 |
|
28 | 28 | /** |
29 | 29 | * @file usbbus.c |
30 | | - * @brief libusb 0.1 driver wrapper |
| 30 | + * @brief libusb 1.0 driver wrapper |
31 | 31 | */ |
32 | 32 |
|
33 | 33 | #ifdef HAVE_CONFIG_H |
|
41 | 41 | #define LOG_CATEGORY "libnfc.buses.usbbus" |
42 | 42 | #define LOG_GROUP NFC_LOG_GROUP_DRIVER |
43 | 43 |
|
44 | | -int usb_prepare(void) |
| 44 | +static libusb_context *usb_context = NULL; |
| 45 | + |
| 46 | +libusb_context * |
| 47 | +usb_get_context(void) |
45 | 48 | { |
46 | 49 | static bool usb_initialized = false; |
47 | 50 | if (!usb_initialized) { |
| 51 | + int r = libusb_init(&usb_context); |
| 52 | + if (r < 0) { |
| 53 | + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to initialize libusb (%s)", libusb_strerror(r)); |
| 54 | + return NULL; |
| 55 | + } |
48 | 56 |
|
49 | 57 | #ifdef ENVVARS |
50 | 58 | char *env_log_level = getenv("LIBNFC_LOG_LEVEL"); |
51 | | - // Set libusb debug only if asked explicitely: |
| 59 | + // Set libusb debug only if asked explicitly: |
52 | 60 | // LIBUSB_LOG_LEVEL=12288 (= NFC_LOG_PRIORITY_DEBUG * 2 ^ NFC_LOG_GROUP_LIBUSB) |
53 | 61 | if (env_log_level && (((atoi(env_log_level) >> (NFC_LOG_GROUP_LIBUSB * 2)) & 0x00000003) >= NFC_LOG_PRIORITY_DEBUG)) { |
54 | | - setenv("USB_DEBUG", "255", 1); |
| 62 | + libusb_set_option(usb_context, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG); |
55 | 63 | } |
56 | 64 | #endif |
57 | 65 |
|
58 | | - usb_init(); |
59 | 66 | usb_initialized = true; |
60 | 67 | } |
61 | 68 |
|
62 | | - int res; |
63 | | - // usb_find_busses will find all of the busses on the system. Returns the |
64 | | - // number of changes since previous call to this function (total of new |
65 | | - // busses and busses removed). |
66 | | - if ((res = usb_find_busses()) < 0) { |
67 | | - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); |
68 | | - return -1; |
69 | | - } |
70 | | - // usb_find_devices will find all of the devices on each bus. This should be |
71 | | - // called after usb_find_busses. Returns the number of changes since the |
72 | | - // previous call to this function (total of new device and devices removed). |
73 | | - if ((res = usb_find_devices()) < 0) { |
74 | | - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); |
75 | | - return -1; |
76 | | - } |
77 | | - return 0; |
| 69 | + return usb_context; |
78 | 70 | } |
79 | 71 |
|
0 commit comments