find endpoint addresses
authorFelix Fietkau <nbd@openwrt.org>
Thu, 30 May 2013 17:45:11 +0000 (19:45 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Thu, 30 May 2013 17:45:11 +0000 (19:45 +0200)
main.c
switch.h

diff --git a/main.c b/main.c
index cf58c173c505b41f4fa3d6b9903eb6bf32a96885..b7461adfe95a4b424cf14db82047240dfb17b97e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -19,8 +19,8 @@ static int verbose = 0;
 static const char *config_file = DEFAULT_CONFIG;
 static struct blob_buf conf;
 
-static struct blob_attr **messages;
-static int n_messages;
+struct blob_attr **messages = NULL;
+int n_messages = 0;
 
 static struct avl_tree devices;
 
@@ -192,6 +192,8 @@ parse_interface_config(libusb_device *dev, struct usbdev_data *data)
 {
        struct libusb_config_descriptor *config;
        const struct libusb_interface *iface;
+       const struct libusb_interface_descriptor *alt;
+       int i;
 
        data->interface = -1;
        if (libusb_get_config_descriptor(dev, 0, &config))
@@ -205,7 +207,28 @@ parse_interface_config(libusb_device *dev, struct usbdev_data *data)
        if (!iface->num_altsetting)
                return;
 
-       data->interface = iface->altsetting[0].bInterfaceNumber;
+       alt = &iface->altsetting[0];
+       data->interface = alt->bInterfaceNumber;
+
+       for (i = 0; i < alt->bNumEndpoints; i++) {
+               const struct libusb_endpoint_descriptor *ep = &alt->endpoint[i];
+               bool out = false;
+
+               if (data->msg_endpoint && data->response_endpoint)
+                       break;
+
+               if ((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) !=
+                   LIBUSB_TRANSFER_TYPE_BULK)
+                       continue;
+
+               out = (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
+                     LIBUSB_ENDPOINT_OUT;
+
+               if (!data->msg_endpoint && out)
+                       data->msg_endpoint = ep->bEndpointAddress;
+               if (!data->response_endpoint && !out)
+                       data->response_endpoint = ep->bEndpointAddress;
+       }
 }
 
 static void iterate_devs(cmd_cb_t cb)
index eb5968f217225b6c1c5ca43c3e5528d1cfaed3cc..ab09f11fa4a496d7952b0b31a521eb778f2e1f74 100644 (file)
--- a/switch.h
+++ b/switch.h
@@ -10,11 +10,16 @@ struct usbdev_data {
        libusb_device_handle *devh;
        struct blob_attr *info;
        int interface;
+       int msg_endpoint;
+       int response_endpoint;
 
        char idstr[10];
        char mfg[128], prod[128], serial[128];
 };
 
+extern struct blob_attr **messages;
+extern int n_messages;
+
 void handle_switch(struct usbdev_data *data);
 
 #endif