示例#1
0
XN_C_API XnStatus xnUSBOpenDeviceByPath(const XnUSBConnectionString strDevicePath, XN_USB_DEV_HANDLE* pDevHandlePtr)
{
	XnStatus nRetVal = XN_STATUS_OK;
	
	// parse connection string
	XnUInt16 nVendorID = 0;
	XnUInt16 nProductID = 0;
	XnUInt8 nBus = 0;
	XnUInt8 nAddress = 0;
	sscanf(strDevicePath, "%hx/%hx@%hhu/%hhu", &nVendorID, &nProductID, &nBus, &nAddress);
	
	if (nVendorID == 0 || nProductID == 0 || nBus == 0 || nAddress == 0)
	{
		XN_LOG_WARNING_RETURN(XN_STATUS_USB_DEVICE_OPEN_FAILED, "Invalid connection string: %s", strDevicePath);
	}

	// find device	
	libusb_device** ppDevices;
	ssize_t nDeviceCount = libusb_get_device_list(g_InitData.pContext, &ppDevices);
	
	libusb_device* pRequestedDevice = NULL;
	
	for (ssize_t i = 0; i < nDeviceCount; ++i)
	{
		libusb_device* pDevice = ppDevices[i];
		
		// get device descriptor
		libusb_device_descriptor desc;
		int rc = libusb_get_device_descriptor(pDevice, &desc);
		if (rc != 0)
		{
			libusb_free_device_list(ppDevices, 1);
			return (XN_STATUS_USB_ENUMERATE_FAILED);
		}
		
		// check if this is the requested device
		if (desc.idVendor == nVendorID && desc.idProduct == nProductID && libusb_get_bus_number(pDevice) == nBus && libusb_get_device_address(pDevice) == nAddress)
		{
			// add a reference to the device (so it won't be destroyed when list is freed)
			libusb_ref_device(pDevice);
			pRequestedDevice = pDevice;
			break;	
		}
	}

	libusb_free_device_list(ppDevices, 1);
	
	nRetVal = xnUSBOpenDeviceImpl(pRequestedDevice, pDevHandlePtr);
	XN_IS_STATUS_OK(nRetVal);
	
	return (XN_STATUS_OK);
}
示例#2
0
char *get_path(libusb_device *dev)
{
	uint8_t path[8];
	int r,j;
	r = libusb_get_port_numbers(dev, path, sizeof(path));
	if (r > 0) {
		sprintf(path_buf,"%d-%d",libusb_get_bus_number(dev),path[0]);
		for (j = 1; j < r; j++){
			sprintf(path_buf+strlen(path_buf),".%d",path[j]);
		};
	}
	return path_buf;
}
示例#3
0
int usb_get_device_name(libusb_device_handle *device_handle, char *name, int length) {
	int rc;
	libusb_device *device = libusb_get_device(device_handle);
	uint8_t bus_number = libusb_get_bus_number(device);
	uint8_t device_address = libusb_get_device_address(device);
	struct libusb_device_descriptor descriptor;
	char product[64];
	char serial_number[64];

	// get device descriptor
	rc = libusb_get_device_descriptor(device, &descriptor);

	if (rc < 0) {
		log_error("Could not get device descriptor for USB device (bus: %u, device: %u): %s (%d)",
		          bus_number, device_address, usb_get_error_name(rc), rc);

		return -1;
	}

	// get product string descriptor
	rc = libusb_get_string_descriptor_ascii(device_handle,
	                                        descriptor.iProduct,
	                                        (unsigned char *)product,
	                                        sizeof(product));

	if (rc < 0) {
		log_error("Could not get product string descriptor for USB device (bus: %u, device: %u): %s (%d)",
		          bus_number, device_address, usb_get_error_name(rc), rc);

		return -1;
	}

	// get serial number string descriptor
	rc = libusb_get_string_descriptor_ascii(device_handle,
	                                        descriptor.iSerialNumber,
	                                        (unsigned char *)serial_number,
	                                        sizeof(serial_number));

	if (rc < 0) {
		log_error("Could not get serial number string descriptor for USB device (bus: %u, device: %u): %s (%d)",
		          bus_number, device_address, usb_get_error_name(rc), rc);

		return -1;
	}

	// format name
	snprintf(name, length, "%s [%s]", product, serial_number);

	return 0;
}
// Initialise libusb and attempt to open the target device.
// Returns the device handle on success.
// Returns NULL if the target device was not found.
static libusb_device_handle *init(void)
{
	int r;
	libusb_device **list;
	libusb_device_handle *opened_device_handle;
	struct libusb_device_descriptor device_info;
	ssize_t count;
	ssize_t i;
	int bus_number;
	int address;

   	r = libusb_init(NULL);
	if (r < 0)
	{
		printf("ERROR: Could not initialise libusb, return value = %d\n", r);
		exit(1);
	}

	// Loop through all buses/devices and find the right one.
	count = libusb_get_device_list(NULL, &list);
	for (i = 0; i < count; i++)
	{
		if (libusb_get_device_descriptor(list[i], &device_info) == 0)
		{
			if ((device_info.idVendor == TARGET_VID)
				&& (device_info.idProduct == TARGET_PID))
			{
				bus_number = libusb_get_bus_number(list[i]);
				address = libusb_get_device_address(list[i]);
				printf("Found device on bus %d, address = %d\n", bus_number, address);
				r = libusb_open(list[i], &opened_device_handle);
				if (r != 0)
				{
					printf("ERROR: Could not open device, %s\n", libusb_error_name(r));
					printf("Maybe you need to run this program as root.\n");
					printf("If using Windows, have you installed the WinUSB driver?\n");
					exit(1);
				}
				libusb_free_device_list(list, 1);
				// Just in case, detach kernel driver from interface.
				libusb_detach_kernel_driver(opened_device_handle, 0);
				libusb_detach_kernel_driver(opened_device_handle, 1);
				return opened_device_handle;
			}
		}
	}
	libusb_free_device_list(list, 1);
	return NULL;
}
示例#5
0
int open_accesory_dev()
{
	libusb_device **devs;
	int r;
	ssize_t cnt;

	if( libusb_init(NULL) < 0)
		return -1;

	cnt = libusb_get_device_list(NULL, &devs);
	libusb_device *dev;
	int i = 0;

	while ((dev = devs[i++]) != NULL) {
		struct libusb_device_descriptor desc;
		int r = libusb_get_device_descriptor(dev, &desc);
		if (r < 0) {
			fprintf(stderr, "failed to get device descriptor");
			return -1;
		}

		if(desc.idVendor == VID && desc.idProduct == PID){
			printf("%04x:%04x (bus %d, device %d)\n",
					desc.idVendor, desc.idProduct,
					libusb_get_bus_number(dev), libusb_get_device_address(dev));
			libusb_open(dev,&handle);
			return 0;
		}else if(desc.idVendor == GOOGLE_VID && desc.idProduct == PID){
			printf("%04x:%04x (bus %d, device %d)\n",
					desc.idVendor, desc.idProduct,
					libusb_get_bus_number(dev), libusb_get_device_address(dev));
			return libusb_open(dev,handle);
		}
	}
	return -1;
}
示例#6
0
int ezusb_upload_firmware(struct slogic_ctx *handle, int configuration, const char *filename){

        log_printf(DEBUG, "uploading firmware to device on %d.%d\n", libusb_get_bus_number(handle->dev), libusb_get_device_address(handle->dev));
                
        if ((ezusb_reset(handle->device_handle, 1)) < 0)
                return 1;
  
        if (ezusb_install_firmware(handle->device_handle, filename) != 0)
                return 1;

        if ((ezusb_reset(handle->device_handle, 0)) < 0)
                return 1;
        
        return 0;
}
示例#7
0
文件: ow_ds9490.c 项目: M-o-a-T/owfs
/* Open a DS9490  -- low level code (to allow for repeats)  */
static GOOD_OR_BAD DS9490_detect_specific_adapter(int bus_nr, int dev_nr, struct connection_in * in)
{
	// discover devices
	libusb_device **device_list;
	int n_devices = libusb_get_device_list( Globals.luc, &device_list) ;
	int i_device ;
	
	if ( n_devices < 1 ) {
		LEVEL_CONNECT("Could not find a list of USB devices");
		if ( n_devices<0 ) {
			LEVEL_DEBUG("<%s>",libusb_error_name(n_devices));
		}
		return gbBAD ;
	}

	// Mark this connection as taking only this address pair. Important for reconnections.
	in->master.usb.specific_usb_address = 1 ;

	for ( i_device = 0 ; i_device < n_devices ; ++i_device ) {
		libusb_device * current = device_list[i_device] ;
		if ( GOOD( USB_match( current ) ) ) {
			if ( libusb_get_bus_number(current) != bus_nr ) {
				continue ;
			}
			
			if ( libusb_get_device_address(current) != dev_nr ) {
				continue ;
			}
			
			if ( BAD(DS9490_open_and_name( current, in)) ) {
				LEVEL_DEBUG("Cannot open USB device %.d:%.d", libusb_get_device_address(current), libusb_get_bus_number(current) );
				break ;
			} else if ( BAD(DS9490_ID_this_master(in)) ) {
				DS9490_close(in) ;
				LEVEL_DEBUG("Cannot access USB device %.d:%.d", libusb_get_device_address(current), libusb_get_bus_number(current) );
				break ;
			} else{
				libusb_free_device_list(device_list, 1);
				return gbGOOD ;
			}
		}
	}
	
	libusb_free_device_list(device_list, 1);

	LEVEL_CONNECT("No USB DS9490 bus master found matching %d:%d", bus_nr,dev_nr);
	return gbBAD;
}
示例#8
0
bool get_state(libusb_device_handle *dev_handle)
{
	uint8_t buf[QUERY_WLENGTH];
	int transfered;
	libusb_device *dev;

	/* zero buf so we're not sending random stack data to the
	 * device */
	memset(buf, 0, sizeof(buf));
	if((transfered = libusb_control_transfer(dev_handle, QUERY_BMREQUEST_TYPE, QUERY_BREQUEST, QUERY_WVALUE, QUERY_WINDEX, buf, QUERY_WLENGTH, QUERY_TIMEOUT))!=QUERY_WLENGTH)
	{
		dev = libusb_get_device(dev_handle);
		die_usb(transfered, "Unable to query device %02x:%02x state", libusb_get_bus_number(dev), libusb_get_device_address(dev));
	}
	return buf[0] == QUERY_IS_ON;
}
示例#9
0
SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration,
				  const char *filename)
{
	struct libusb_device_handle *hdl;
	int ret;

    sr_info("uploading firmware to device on %d.%d",
		libusb_get_bus_number(dev), libusb_get_device_address(dev));

	if ((ret = libusb_open(dev, &hdl)) < 0) {
        sr_err("failed to open device: %s.", libusb_error_name(ret));
        return SR_ERR;
	}

/*
 * The libusbx darwin backend is broken: it can report a kernel driver being
 * active, but detaching it always returns an error.
 */
#if !defined(__APPLE__)
	if (libusb_kernel_driver_active(hdl, 0) == 1) {
		if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) {
            sr_err("failed to detach kernel driver: %s",
					libusb_error_name(ret));
            return SR_ERR;
		}
	}
#endif

	if ((ret = libusb_set_configuration(hdl, configuration)) < 0) {
        sr_err("Unable to set configuration: %s",
				libusb_error_name(ret));
        return SR_ERR;
	}

	if ((ezusb_reset(hdl, 1)) < 0)
        return SR_ERR;

	if (ezusb_install_firmware(hdl, filename) < 0)
        return SR_ERR;

	if ((ezusb_reset(hdl, 0)) < 0)
        return SR_ERR;

	libusb_close(hdl);

    return SR_OK;
}
示例#10
0
struct libusb_device* search_device(int _busNum, int _devNum) { 
    libusb_device *l_dev;
    int i = 0;
    int l_busNum, l_devNum;

    while ((l_dev = devs[i++]) != NULL) {
        printf("check against %d device\n", i);
        l_busNum =(int) libusb_get_bus_number(l_dev);
        l_devNum =(int) libusb_get_device_address(l_dev);
        printf("bus number: %d; device number: %d\n", l_busNum, l_devNum);
        if ((l_busNum == _busNum) && (l_devNum == _devNum)) {
            printf("found device\n");
            return l_dev;
        }
    }
    return NULL;
}
示例#11
0
void set_state(libusb_device_handle *dev_handle, bool state)
{
	uint8_t buf[SET_WLENGTH];
	int usb_err;
	libusb_device *dev;

	debug("\tTurning %s", state ? "on" : "off");
	/* zero buf so we're not sending random stack data to the
	 * device */
	memset(buf, 0, sizeof(buf));
	if((usb_err = libusb_control_transfer(dev_handle, SET_BMREQUEST_TYPE, SET_BREQUEST, SET_WVALUE, state ? SET_WINDEX_ON : SET_WINDEX_OFF, buf, SET_WLENGTH, SET_TIMEOUT))!=SET_WLENGTH)
	{
		dev = libusb_get_device(dev_handle);
		die_usb(usb_err, "Unable to set device %02x:%02x %s", libusb_get_bus_number(dev), libusb_get_device_address(dev), state ? "on" : "off");
	}
	debug("\ttransfer amount was %d buf[0]=0x%02x", usb_err, buf[0]);
}
示例#12
0
static void print_devs(libusb_device **devs)
{
	libusb_device *dev;
	struct libusb_config_descriptor *conf_desc;
	int nb_ifaces = 0;
	int i = 0, j = 0, n = 0;
	uint8_t path[8]; 

	while ((dev = devs[n++]) != NULL) {
		struct libusb_device_descriptor desc;
		int r = libusb_get_device_descriptor(dev, &desc);
		if (r < 0) {
			fprintf(stderr, "failed to get device descriptor");
			return;
		}

		printf("%04x:%04x (bus %d, device %d)",
			desc.idVendor, desc.idProduct,
			libusb_get_bus_number(dev), libusb_get_device_address(dev));

		r = libusb_get_port_numbers(dev, path, sizeof(path));
		if (r > 0) {
			printf(" path: %d", path[0]);
			for (j = 1; j < r; j++)
				printf(".%d", path[j]);
		}
		printf("\n");

		libusb_get_config_descriptor(dev, 0, &conf_desc);
		nb_ifaces = conf_desc->bNumInterfaces;
		fprintf(stdout, "nb_ifaces:::%d\n", nb_ifaces);
		for (i=0; i<nb_ifaces; i++) {
			printf("              interface[%d]: id = %d\n", i,
				conf_desc->interface[i].altsetting[0].bInterfaceNumber);
			for (j=0; j<conf_desc->interface[i].num_altsetting; j++) {
				printf("interface[%d].altsetting[%d]: num endpoints = %d\n",
					i, j, conf_desc->interface[i].altsetting[j].bNumEndpoints);
				printf("   Class.SubClass.Protocol: %02X.%02X.%02X\n",
					conf_desc->interface[i].altsetting[j].bInterfaceClass,
					conf_desc->interface[i].altsetting[j].bInterfaceSubClass,
					conf_desc->interface[i].altsetting[j].bInterfaceProtocol);
			}
		}
	}
}
示例#13
0
        static std::string get_usb_port_id(libusb_device* usb_device)
        {
            auto usb_bus = std::to_string(libusb_get_bus_number(usb_device));

            // As per the USB 3.0 specs, the current maximum limit for the depth is 7.
            const auto max_usb_depth = 8;
            uint8_t usb_ports[max_usb_depth] = {};
            std::stringstream port_path;
            auto port_count = libusb_get_port_numbers(usb_device, usb_ports, max_usb_depth);
            auto usb_dev = std::to_string(libusb_get_device_address(usb_device));

            for (size_t i = 0; i < port_count; ++i)
            {
                port_path << std::to_string(usb_ports[i]) << (((i+1) < port_count)?".":"");
            }

            return usb_bus + "-" + port_path.str() + "-" + usb_dev;
        }
示例#14
0
static void print_devs(libusb_device **devs)
{
	libusb_device *dev;
	int i = 0;

	while ((dev = devs[i++]) != NULL) {
		struct libusb_device_descriptor desc;
		int r = libusb_get_device_descriptor(dev, &desc);
		if (r < 0) {
			fprintf(stderr, "failed to get device descriptor");
			return;
		}

		printf("%04x:%04x (bus %d, device %d)\n",
			desc.idVendor, desc.idProduct,
			libusb_get_bus_number(dev), libusb_get_device_address(dev));
	}
}
示例#15
0
文件: ezusb.c 项目: jfrkuska/sigrok
SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration,
				  const char *filename)
{
	struct libusb_device_handle *hdl;
	int ret;

	sr_info("uploading firmware to device on %d.%d",
		libusb_get_bus_number(dev), libusb_get_device_address(dev));

	if ((ret = libusb_open(dev, &hdl)) < 0) {
		sr_err("failed to open device: %s.", libusb_error_name(ret));
		return SR_ERR;
	}

/* Neither Windows/MinGW nor Darwin/Mac support these libusb-1.0 calls. */
#if !defined(_WIN32) && !defined(__APPLE__)
	if (libusb_kernel_driver_active(hdl, 0)) {
		if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) {
			sr_err("failed to detach kernel driver: %s",
					libusb_error_name(ret));
			return SR_ERR;
		}
	}
#endif

	if ((ret = libusb_set_configuration(hdl, configuration)) < 0) {
		sr_err("Unable to set configuration: %s",
				libusb_error_name(ret));
		return SR_ERR;
	}

	if ((ezusb_reset(hdl, 1)) < 0)
		return SR_ERR;

	if (ezusb_install_firmware(hdl, filename) < 0)
		return SR_ERR;

	if ((ezusb_reset(hdl, 0)) < 0)
		return SR_ERR;

	libusb_close(hdl);

	return SR_OK;
}
示例#16
0
文件: usb.c 项目: DeeJay/libsigrok
SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
{
	struct libusb_device **devlist;
	struct libusb_device_descriptor des;
	int ret, r, cnt, i, a, b;

	sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address);

	if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
		sr_err("Failed to retrieve device list: %s.",
		       libusb_error_name(cnt));
		return SR_ERR;
	}

	ret = SR_ERR;
	for (i = 0; i < cnt; i++) {
		if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
			sr_err("Failed to get device descriptor: %s.",
			       libusb_error_name(r));
			continue;
		}

		b = libusb_get_bus_number(devlist[i]);
		a = libusb_get_device_address(devlist[i]);
		if (b != usb->bus || a != usb->address)
			continue;

		if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
			sr_err("Failed to open device: %s.",
			       libusb_error_name(r));
			break;
		}

		sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
		       "%d.%d).", des.idVendor, des.idProduct, b, a);

		ret = SR_OK;
		break;
	}

	libusb_free_device_list(devlist, 1);

	return ret;
}
示例#17
0
std::string get_device_port(libusb_device *dev)
	{

	int bus = libusb_get_bus_number(dev);
	//int address = libusb_get_device_address(dev);

	std::ostringstream os;
	os << bus << "-";
	uint8_t path[8];
	int r = libusb_get_port_numbers(dev, path, sizeof(path));
	if (r > 0) {
		//printf(" path: %d", path[0]);
		os << (int)path[0];
		for (int j = 1; j < r; j++)
			os << "." << (int)path[j];
			//printf(".%d", path[j]);
	}
	return os.str();
}
示例#18
0
void main_loop(libusb_device_handle *handle)
{
    libusb_device *device=libusb_get_device(handle);
    printf("device found at %d:%d\n",
        libusb_get_bus_number(device),
        libusb_get_device_address(device));

    struct libusb_device_descriptor dev_desc;
    int ret = libusb_get_device_descriptor(device,&dev_desc);
    if(ret != 0)
        return;

    unsigned char buf[1025];

    while(true)
    {
        ret = libusb_control_transfer(
            handle,
            LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
            0x99,
            0,
            0,
            buf,
            sizeof(buf)-1,
            100);
        if(ret < 0 && ret != LIBUSB_ERROR_TIMEOUT && ret != LIBUSB_ERROR_PIPE)
            break;

        if(ret == 0)
        {
            struct timespec tps;
            tps.tv_sec = 0;
            tps.tv_nsec = 1000000000 / 10;
            nanosleep(&tps, NULL);
        }
        else
        {
            buf[ret] = 0;
            printf("%s", buf);
            fflush(stdout);
        }
    }
}
示例#19
0
/* /================================================\
 *   Private libusb-control methods
 * \================================================/ */
void
list_devices(libusb_device **devices)
{
  libusb_device *dev;
  int i = 0;

  while ((dev = devices[i++]) != NULL) {
    struct libusb_device_descriptor desc;
    int r = libusb_get_device_descriptor(dev, &desc);
    if (r < 0) {
      fprintf(stderr, "failed to get device descriptor");
      return;
    }

    printf("idVendor:%04x  idProduct:%04x  SN:%02x (bus %d, device %d)\n",
    desc.idVendor, desc.idProduct, desc.iSerialNumber,
    libusb_get_bus_number(dev), libusb_get_device_address(dev));
  }
}
示例#20
0
static void
g_usb_context_rescan (GUsbContext *context)
{
	GList *existing_devices = NULL;
	GList *l;
	GUsbDevice *device;
	GUsbContextPrivate *priv = context->priv;
	gboolean found;
	guint i;
	libusb_device **dev_list = NULL;

	/* copy to a context so we can remove from the array */
	for (i = 0; i < priv->devices->len; i++) {
		device = g_ptr_array_index (priv->devices, i);
		existing_devices = g_list_prepend (existing_devices, device);
	}

	/* look for any removed devices */
	for (l = existing_devices; l != NULL; l = l->next) {
		device = G_USB_DEVICE (l->data);
		found = FALSE;
		for (i = 0; dev_list && dev_list[i]; i++) {
			if (libusb_get_bus_number (dev_list[i]) == g_usb_device_get_bus (device) &&
			    libusb_get_device_address (dev_list[i]) == g_usb_device_get_address (device)) {
				found = TRUE;
				break;
			}
		}
		if (!found) {
			g_usb_context_emit_device_remove (context, device);
			g_ptr_array_remove (priv->devices, device);
		}
	}

	/* add any devices not yet added (duplicates will be filtered */
	libusb_get_device_list (priv->ctx, &dev_list);
	for (i = 0; dev_list && dev_list[i]; i++)
		g_usb_context_add_device (context, dev_list[i]);

	g_list_free (existing_devices);
	libusb_free_device_list (dev_list, 1);
}
示例#21
0
static int find_devices(libusb_device **dev_list, int dev_list_len,
	struct usb_bus *bus, struct usb_device **ret)
{
	struct usb_device *devices = NULL;
	struct usb_device *dev;
	int i;
	
	for (i = 0; i < dev_list_len; i++) {
		libusb_device *newlib_dev = dev_list[i];
		uint8_t bus_num = libusb_get_bus_number(newlib_dev);
		
		if (bus_num != bus->location)
			continue;
		
		dev = malloc(sizeof(*dev));
		if (!dev)
			goto err;

		/* No need to reference the device now, just take the pointer. We
		 * increase the reference count later if we keep the device. */
		 
		dev->dev = newlib_dev;

		dev->bus = bus;
		dev->devnum = libusb_get_device_address(newlib_dev);
		sprintf(dev->filename, "%03d", dev->devnum);
		LIST_ADD(devices, dev);
	}

	*ret = devices;
	return 0;

err:
	dev = devices;
	
	while (dev) {
		struct usb_device *tdev = dev->next;
		free(dev);
		dev = tdev;
	}
	return -ENOMEM;
}
示例#22
0
文件: libusb.c 项目: mrene/bladeRF
/* Returns libusb error codes */
static int get_devinfo(libusb_device *dev, struct bladerf_devinfo *info)
{
    int status = 0;
    libusb_device_handle *handle;
    struct libusb_device_descriptor desc;

    status = libusb_open(dev, &handle);
    if( status ) {
        log_debug("Couldn't populate devinfo - %s\n",
                  libusb_error_name(status));
    } else {
        /* Populate device info */
        info->backend = BLADERF_BACKEND_LIBUSB;
        info->usb_bus = libusb_get_bus_number(dev);
        info->usb_addr = libusb_get_device_address(dev);

        status = libusb_get_device_descriptor(dev, &desc);
        if (status != 0) {
            memset(info->serial, 0, BLADERF_SERIAL_LENGTH);
        } else {
            status = libusb_get_string_descriptor_ascii(
                                        handle, desc.iSerialNumber,
                                        (unsigned char *)&info->serial,
                                        BLADERF_SERIAL_LENGTH);

            /* Consider this to be non-fatal, otherwise firmware <= 1.1
             * wouldn't be able to get far enough to upgrade */
            if (status < 0) {
                log_debug("Failed to retrieve serial number\n");
                memset(info->serial, 0, BLADERF_SERIAL_LENGTH);
            } else {
                /* Adjust for > 0 return code */
                status = 0;
            }

        }

        libusb_close(handle);
    }

    return status;
}
示例#23
0
GOOD_OR_BAD USB_match(libusb_device * dev)
{
    struct libusb_device_descriptor lusbd ;
    int libusb_err ;

    if ( (libusb_err=libusb_get_device_descriptor( dev, &lusbd )) != 0 ) {
        LEVEL_DEBUG("<%s> Cannot get descriptor",libusb_error_name(libusb_err));
        return gbBAD ;
    }

    if ( lusbd.idVendor != DS2490_USB_VENDOR ) {
        return gbBAD ;
    }

    if ( lusbd.idProduct != DS2490_USB_PRODUCT ) {
        return gbBAD ;
    }

    return lusbdevice_in_use( libusb_get_device_address(dev), libusb_get_bus_number(dev) ) ;
}
示例#24
0
string UsbWrap::AboutUsbDevice()
{
    if (!_isInitialized)
    {
        throw UsbWrapException("USB driver not initialized");
    }

    if (!_isConnected)
    {
        throw UsbWrapException("USB device not connected");
    }

    ostringstream os;
    libusb_device *device = libusb_get_device(_handleDevice);
    os << "Number of the bus: " << std::dec << libusb_get_bus_number(device) << endl;
    os << "Address of the device on the bus: " << std::dec << libusb_get_device_address(device) << endl;
    os << "Negotiated connection speed: " << std::dec << libusb_get_device_speed(device) << endl;

    return os.str();
}
示例#25
0
static int _libusb_show_all_device(libusb_device **devs)
{
	libusb_device *dev;
	int i = 0;
	while ((dev = devs[i++]) != NULL) {
		struct libusb_device_descriptor desc;
		int r = libusb_get_device_descriptor(dev, &desc);
		if (r < 0) {
			fprintf(stderr, "failed to get device descriptor");
			return r;
		}
	printf("%04x:%04x (bus %d, device %d)\n",
		desc.idVendor, desc.idProduct,
		libusb_get_bus_number(dev), libusb_get_device_address(dev));

		r = libusb_show_device(desc.idVendor, desc.idProduct);
		if (r == -EIO)
			printf("Not Permitted open usb device\n");
	}
}
示例#26
0
文件: yusb.c 项目: emmt/yusb
void Y_usb_open_device(int argc)
{
  ydev_instance_t *obj = NULL;
  libusb_device* dev;
  int bus, port;
  int i, ret;

  if (argc != 2) {
    y_error("expecting exactly 2 arguments");
  }
  bus = ygets_i(1);
  port = ygets_i(0);

  load_device_list();
  for (i = 0; i < dev_count; ++i) {
    dev = dev_list[i];
    if (libusb_get_bus_number(dev) == bus &&
        libusb_get_port_number(dev) == port) {
      obj = (ydev_instance_t *)ypush_obj(&ydev_class, sizeof(ydev_instance_t));
      obj->device = libusb_ref_device(dev);
      ret = libusb_open(obj->device, &obj->handle);
      if (ret < 0) {
        obj->handle = NULL;
        failure("failed to open device", ret);
      }
      obj->bus = bus;
      obj->port = port;
      obj->address = libusb_get_device_address(dev);
      ret = libusb_get_device_descriptor(dev, &obj->descriptor);
      if (ret != 0) {
        free_dev_list();
        failure("unable to get device descriptor", ret);
      }
      break;
    }
  }
  free_dev_list();
  if (obj == NULL) {
    ypush_nil();
  }
}
示例#27
0
void probe_devices(libusb_context *ctx)
{
	libusb_device **list;
	ssize_t num_devs;
	ssize_t i;

	num_devs = libusb_get_device_list(ctx, &list);
	for (i = 0; i < num_devs; ++i) {
		struct libusb_device_descriptor desc;
		struct libusb_device *dev = list[i];

		if (match_bus > -1 && match_bus != libusb_get_bus_number(dev))
			continue;
		if (match_device > -1 && match_device != libusb_get_device_address(dev))
			continue;
		if (libusb_get_device_descriptor(dev, &desc))
			continue;
		probe_configuration(dev, &desc);
	}
	libusb_free_device_list(list, 0);
}
示例#28
0
static void
g_usb_context_remove_device (GUsbContext          *context,
                             struct libusb_device *dev)
{
	GUsbDevice *device = NULL;
	GUsbContextPrivate *priv = context->priv;
	guint8 bus;
	guint8 address;

	/* does any existing device exist */
	bus = libusb_get_bus_number (dev);
	address = libusb_get_device_address (dev);
	device = g_usb_context_find_by_bus_address (context, bus, address, NULL);
	if (device == NULL) {
		g_debug ("%i:%i does not exist", bus, address);
		return;
	}
	g_usb_context_emit_device_remove (context, device);
	g_ptr_array_remove (priv->devices, device);
	g_object_unref (device);
}
LIBMTP_raw_device_t *smtpfs_raw_device_new(const std::string &path)
{
    libusb_context *ctx;
    int err = libusb_init(&ctx);
    if (err)
        return nullptr;

    std::string dev_path(smtpfs_realpath(path));
    libusb_device **dev_list;
    ssize_t num_devs = libusb_get_device_list(ctx, &dev_list);
    if (num_devs < 1) {
        libusb_exit(ctx);
        return nullptr;
    }

    libusb_device *dev = nullptr;
    for (auto i = 0; i < num_devs; ++i) {
        dev = dev_list[i];
        uint8_t bnum = libusb_get_bus_number(dev_list[i]);
        uint8_t dnum = libusb_get_device_address(dev_list[i]);

        std::stringstream ss;
        ss << smtpfs_devbususb
           << std::setw(3) << std::setfill('0')
           << static_cast<uint16_t>(bnum) << "/"
           << std::setw(3) << std::setfill('0')
           << static_cast<uint16_t>(dnum);

        if (ss.str() == dev_path)
            break;
        dev = nullptr;
    }

    LIBMTP_raw_device_t *raw_device = smtpfs_raw_device_new_priv(dev);

    libusb_free_device_list(dev_list, 0);
    libusb_exit(ctx);

    return raw_device;
}
示例#30
0
static void print_devs(libusb_device **devs)
{
	int j, k, l;
	int i = 0;
	for (;;) {
		struct libusb_device_descriptor desc;
		struct libusb_config_descriptor *config;
		libusb_device *dev = devs[i++];
		if (!dev)
			break;
		int r = libusb_get_device_descriptor(dev, &desc);
		if (r < 0) {
			fprintf(stderr, "failed to get device descriptor");
			return;
		}

		libusb_get_config_descriptor(dev, 0, &config);

		printf("%04x:%04x (bus %d, device %d) bNumInterfaces:%i\n",
			desc.idVendor, desc.idProduct,
			libusb_get_bus_number(dev), libusb_get_device_address(dev),
			config->bNumInterfaces);
		for (j = 0; j < config->bNumInterfaces; j++) {
			const struct libusb_interface *inter = &config->interface[j];
			printf("  alternates:%i\n", inter->num_altsetting);
			for (k = 0; k < inter->num_altsetting; k++) {
				const struct libusb_interface_descriptor *interdesc = &inter->altsetting[k];
				printf("    Interface Number: %i, Number of endpoints: %i\n",
						interdesc->bInterfaceNumber, interdesc->bNumEndpoints);
				for (l = 0; l < interdesc->bNumEndpoints; l++) {
					const struct libusb_endpoint_descriptor *epdesc = &interdesc->endpoint[l];
					printf("      Descriptor Type: %x, EP Address: %i, wMaxPacketSize: %i\n",
							epdesc->bDescriptorType, epdesc->bEndpointAddress, epdesc->wMaxPacketSize);
				}
			}
		}
		libusb_free_config_descriptor(config);
	}
}