7 #include <aura/usb_helpers.h>
10 void ncusb_print_libusb_transfer(
struct libusb_transfer *p_t)
13 slog(4, SLOG_INFO,
"No libusb_transfer...");
16 slog(4, SLOG_INFO,
"libusb_transfer structure:");
17 slog(4, SLOG_INFO,
"flags =%x ", p_t->flags);
18 slog(4, SLOG_INFO,
"endpoint=%x ", p_t->endpoint);
19 slog(4, SLOG_INFO,
"type =%x ", p_t->type);
20 slog(4, SLOG_INFO,
"timeout =%d ", p_t->timeout);
22 slog(4, SLOG_INFO,
"length =%d ", p_t->length);
23 slog(4, SLOG_INFO,
"actual_length =%d ", p_t->actual_length);
24 slog(4, SLOG_INFO,
"buffer =%p ", p_t->buffer);
25 slog(4, SLOG_INFO,
"status =%d/%d ", p_t->status, LIBUSB_TRANSFER_OVERFLOW);
31 int ncusb_match_string(libusb_device_handle *dev,
int index,
const char*
string)
33 unsigned char tmp[256];
34 int ret = libusb_get_string_descriptor_ascii(dev, index, tmp, 256);
38 slog(4, SLOG_DEBUG,
"cmp idx %d str %s vs %s", index, tmp,
string);
41 return (strcmp(
string, (
char*) tmp)==0);
45 struct libusb_device_handle *ncusb_try_device(
struct libusb_context *ctx,
46 libusb_device *device,
48 const char *vendor_name,
49 const char *product_name,
53 libusb_device_handle *handle;
54 struct libusb_device_descriptor desc;
56 err = libusb_open(device, &handle);
60 err = libusb_get_device_descriptor( device, &desc );
64 if ( desc.idVendor == vid && desc.idProduct == pid &&
65 ncusb_match_string(handle, desc.iManufacturer, vendor_name) &&
66 ncusb_match_string(handle, desc.iProduct, product_name) &&
67 ncusb_match_string(handle, desc.iSerialNumber, serial)
78 struct libusb_device_handle *ncusb_find_and_open(
struct libusb_context *ctx,
79 int vendor,
int product,
80 const char *vendor_name,
81 const char *product_name,
84 libusb_device_handle *found = NULL;
86 ssize_t cnt = libusb_get_device_list(ctx, &list);
90 slog(0, SLOG_ERROR,
"no usb devices found" );
94 for(i = 0; i < cnt; i++) {
96 libusb_device *device = list[i];
97 struct libusb_device_descriptor desc;
98 libusb_device_handle *handle;
99 err = libusb_open(device, &handle);
103 int r = libusb_get_device_descriptor( device, &desc );
105 libusb_close(handle);
109 if ( desc.idVendor == vendor && desc.idProduct == product &&
110 ncusb_match_string(handle, desc.iManufacturer, vendor_name) &&
111 ncusb_match_string(handle, desc.iProduct, product_name) &&
112 ncusb_match_string(handle, desc.iSerialNumber, serial)
122 libusb_free_device_list(list, 1);
128 static int hotplug_callback_fn(libusb_context *ctx, libusb_device *device, libusb_hotplug_event event,
void *user_data)
130 struct ncusb_devwatch_data* d = user_data;
131 struct libusb_device_handle *hndl = ncusb_try_device(ctx, device,
133 d->vendor, d->product, d->serial);
135 d->device_found_func(hndl, d->arg);
143 static void pollfd_added_cb(
int fd,
short events,
void *user_data)
145 slog(4, SLOG_DEBUG,
"usb-helpers: Adding fd %d evts %x to node", fd, events);
146 aura_add_pollfds(user_data, fd, events);
149 static void pollfd_removed_cb(
int fd,
void *user_data)
151 slog(4, SLOG_DEBUG,
"usb-helpers: Removing fd %d from node", fd);
152 aura_del_pollfds(user_data, fd);
155 void ncusb_start_descriptor_watching(
struct aura_node *node, libusb_context *ctx)
158 const struct libusb_pollfd** fds_data = libusb_get_pollfds(ctx);
159 const struct libusb_pollfd** fds = fds_data;
161 while (*fds != NULL) {
162 pollfd_added_cb((*fds)->fd, (*fds)->events, node);
167 libusb_set_pollfd_notifiers(ctx, pollfd_added_cb, pollfd_removed_cb, node);
170 int ncusb_watch_for_device(libusb_context *ctx,
171 struct ncusb_devwatch_data* dwatch)
173 return libusb_hotplug_register_callback(ctx, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
174 LIBUSB_HOTPLUG_ENUMERATE, dwatch->vid, dwatch->pid,
175 LIBUSB_HOTPLUG_MATCH_ANY,