Example #1
0
/* Iterate over all matching DFU capable devices within system */
static int iterate_dfu_devices(struct dfu_if *dif,
    int (*action)(struct usb_device *dev, void *user), void *user)
{
	struct usb_bus *usb_bus;
	struct usb_device *dev;

	/* Walk the tree and find our device. */
	for (usb_bus = usb_get_busses(); NULL != usb_bus;
	     usb_bus = usb_bus->next) {
		for (dev = usb_bus->devices; NULL != dev; dev = dev->next) {
			int retval;

			if (dif && (dif->flags &
			    (DFU_IFF_VENDOR|DFU_IFF_PRODUCT)) &&
		    	    (dev->descriptor.idVendor != dif->vendor ||
		     	    dev->descriptor.idProduct != dif->product))
				continue;
			if (dif && (dif->flags & DFU_IFF_DEVNUM) &&
		    	    (atoi(usb_bus->dirname) != dif->bus ||
		     	    dev->devnum != dif->devnum))
				continue;
			if (!count_dfu_interfaces(dev))
				continue;

			retval = action(dev, user);
			if (retval)
				return retval;
		}
	}
	return 0;
}
int main(int argc, char *argv[])
{
  struct usb_bus *bus;

  if (argc > 1 && !strcmp(argv[1], "-v"))
    verbose = 1;

  usb_init();
  usb_set_debug(0);

  usb_find_busses();
  usb_find_devices();

  for (bus = usb_get_busses(); bus; bus = bus->next) {
    if (bus->root_dev && !verbose)
      print_device(bus->root_dev, 0);
    else {
      struct usb_device *dev;

      for (dev = bus->devices; dev; dev = dev->next)
        print_device(dev, 0);
    }
  }

  return 0;
}
Example #3
0
uint8_t open_usb(usb_dev_handle **handle) {
	uint16_t vid = 0x16c0;
	uint16_t pid = 0x05df;
	struct usb_bus *bus;
	struct usb_device *dev;
	usb_dev_handle *target = NULL;

	usb_init();
	usb_find_busses();
	usb_find_devices();
	for (bus=usb_get_busses(); bus; bus=bus->next) {
		for (dev=bus->devices; dev; dev=dev->next) {
			if (dev->descriptor.idVendor == vid && dev->descriptor.idProduct == pid) {
				target = usb_open(dev);
			}
		}
	}
	//return (usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0);
	//return (usbOpenDevice(&handle, 0x16c0, NULL, 0x05df, NULL) != 0);
	if (target != NULL) {
		*handle = target;
		return 1;
	} else {
		return 0;
	}
}
struct usb_dev_handle* easyAVR_Open()
{
  struct usb_bus *busses;

  usb_set_debug(2);
  usb_init();
  usb_find_busses();
  usb_find_devices();

  busses = usb_get_busses();

  struct usb_dev_handle* usb_handle;
  struct usb_bus *bus;


  unsigned char send_data=0xff;

  for (bus = busses; bus; bus = bus->next)
  {
    struct usb_device *dev;

    for (dev = bus->devices; dev; dev = dev->next)
    {
      if (dev->descriptor.idVendor == 0x0400)
      {
        int i,stat;
        //printf("vendor: %i\n",dev->descriptor.idVendor);
        usb_handle = usb_open(dev);
        stat = usb_set_configuration (usb_handle,1);
	return usb_handle;
      }
    }
  }

}
Example #5
0
ssize_t libusb_get_device_list(libusb_context* ctx, libusb_device*** list)
{
	// libusb_device*** list demystified:
	// libusb_device*** is the C equivalent to libusb_device**& in C++; such declaration
	// allows the scope of this function libusb_get_device_list() to write on a variable
	// of type libusb_device** declared within the function scope of the caller.
	// libusb_device** is better understood as libusb_device*[], which is an array of
	// pointers of type libusb_device*, each pointing to a struct that holds actual data.
	usb_find_busses();
	usb_find_devices();
	usb_bus* bus = usb_get_busses();
	std::vector<struct usb_device*> vDevices;
	while (bus)
	{
		struct usb_device* device (bus->devices);
		while (device)
		{
			vDevices.push_back(device);
			device = device->next;
		};
		bus = bus->next;
	};
	*list = (libusb_device**) malloc(vDevices.size()*sizeof(struct usb_device*));
	memcpy(*list, &vDevices[0], vDevices.size()*sizeof(struct usb_device*));
	// the number of devices in the outputted list, or LIBUSB_ERROR_NO_MEM on memory allocation failure.
	return(vDevices.size());
}
Example #6
0
void openUSBDevice(void)
{
	usb_init();
	usb_find_busses();
	usb_find_devices();
	
	USBbus=usb_get_busses();
	current_device=NULL;
	while(USBbus!=NULL)
		{USBdevice=USBbus->devices;
		while(USBdevice!=NULL)
			{if ((USBdevice->descriptor.idVendor==0x03a3)&&(USBdevice->descriptor.idProduct==0x2b49))
				current_device=USBdevice;
			USBdevice=USBdevice->next;}
		USBbus=USBbus->next;}
	if (current_device==NULL)
		{printf("\n\nCould not find device\n\n");exit(0);}
	fflush(stdout);
	current_handle=usb_open(current_device);

	if (usb_claim_interface(current_handle, 0) <0)
	{
		printf("Error - usb_claim_interface\n");
		exit(0);
	}

	if (usb_clear_halt(current_handle,out_ep) <0)
	{
		printf("Error - usb_clear_halt EP %d\n",out_ep);
		exit(0);
	}
}
usb_dev_handle* usb_connect()
{
    struct usb_bus *bus;
    struct usb_device *dev_tmp;

    int ret;

    ret=usb_find_busses();
    ret=usb_find_devices();

    for(bus = usb_get_busses(); bus; bus = bus->next)
    {
        for(dev_tmp = bus->devices; dev_tmp; dev_tmp = dev_tmp->next)
        {
            if(dev_tmp->descriptor.idVendor == MY_VID
                    && dev_tmp->descriptor.idProduct == MY_PID)
            {
                usb_dev_handle *dev = usb_open(dev_tmp);
                if(dev==NULL)
                    break;
                ret=usb_set_configuration(dev, 1);
                ret=usb_claim_interface(dev, 1);

                return dev;
            }
        }
    }
    return NULL;
}
Example #8
0
int main(void) {
    struct usb_bus *bus;
    struct usb_device *dev;
    usb_dev_handle *udev;
    int dev_found, ret;
    char buffer[8];
    
    usb_init();

    usb_find_busses();
    usb_find_devices();

    udev = NULL;
    dev_found = FALSE;
    for (bus = usb_get_busses(); bus && !dev_found; bus = bus->next) {
        for (dev = bus->devices; dev && !dev_found; dev = dev->next) {
            if ((dev->descriptor.idVendor == 0x04D8) && (dev->descriptor.idProduct == 0x0001)) {
                dev_found = TRUE;
                udev = usb_open(dev);
            }
        }
    }
    if (!dev_found) {
        printf("No matching device found...\n");
    }
    if (udev) {
        ret = usb_control_msg(udev, USB_TYPE_VENDOR | USB_RECIP_DEVICE, SET_RA0, 0, 0, buffer, 0, 100);
        if (ret < 0) {
            printf("Unable to send vendor specific request, ret = %d...\n", ret);
        usb_close(udev);
        }
    }
}
Example #9
0
int open (const char *pathname, int flags, ...) {
	static int (*func) (const char *, int, mode_t) = NULL;
	mode_t mode = 0;
	va_list args;
	int fd;

	if (!func)
		func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");

	if (flags & O_CREAT) {
		va_start(args, flags);
		mode = va_arg(args, mode_t);
		va_end(args);
	}

	if (!strcmp (pathname, "/dev/windrvr6")) {
		DPRINTF("opening windrvr6\n");
#ifdef NO_WINDRVR
		windrvrfd = fd = (*func) ("/dev/null", flags, mode);
#else
		windrvrfd = fd = (*func) (pathname, flags, mode);
#endif
		if (!busses) {
			usb_init();
			usb_find_busses();
			usb_find_devices();

			busses = usb_get_busses();
		}

		return fd;
	}

	return (*func) (pathname, flags, mode);
}
Example #10
0
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8/////////9/////////A
usb_dev_handle* getUsbDeviceHandle(const size_t vendor, const size_t product)
{
	usb_init();
	usb_find_busses();
	usb_find_devices();

	usb_bus* busses = usb_get_busses();

	for(struct usb_bus* bus = busses; bus; bus = bus->next)
	{
		for(struct usb_device* devi = bus->devices; devi; devi = devi->next)
		{
			if(vendor == devi->descriptor.idVendor && product == devi->descriptor.idProduct)
			{
				usb_dev_handle* handle = usb_open(devi);
				if(!handle)
					return 0;
//				if(usb_claim_interface(handle, 0) < 0)
//					return 0;

				return handle;
			}
		}
	}
	return 0;
}
Example #11
0
struct usb_dev_handle * open_port() {
	struct usb_bus *busses, *bus;

	usb_init();
	usb_find_busses();
	usb_find_devices();

	busses = usb_get_busses();
	for (bus = busses; bus; bus = bus->next) {
		struct usb_device *dev;
		for (dev = bus->devices; dev; dev = dev->next) {
			printf("idVendor:0x%x\t,ipProduct:0x%x\n",
					dev->descriptor.idVendor, dev->descriptor.idProduct);
			if (QQ2440_SECBULK_IDVENDOR == dev->descriptor.idVendor
					&& QQ2440_SECBULK_IDPRODUCT == dev->descriptor.idProduct) {
				printf("Target usb device found!\n");
				struct usb_dev_handle *hdev = usb_open(dev);
				if (!hdev) {
					perror("Cannot open device");
				} else {
					if (0 != usb_claim_interface(hdev, 0)) {
						perror("Cannot claim interface");
						usb_close(hdev);
						hdev = NULL;
					}
				}
				return hdev;
			}
		}
	}

	printf("Target usb device not found!\n");

	return NULL;
}
Example #12
0
static struct usb_bus* init_usb()
{
  usb_init();
  usb_find_busses();
  usb_find_devices();
  return (usb_get_busses());
}
Example #13
0
int
gusb_init(const char *portname, gpsdevh **dh)
{
	int req_unit_number = 0;
	libusb_unit_data *lud = xcalloc(sizeof (libusb_unit_data), 1);

	*dh = (gpsdevh*) lud;

// usb_set_debug(99);
	usb_init();
	gusb_register_ll(&libusb_llops);

	/* if "usb:N", read "N" to be the unit number. */
	if (strlen(portname) > 4) {
		if (0 == strcmp(portname+4, "list")) {
			req_unit_number = -1;
		} else {
			req_unit_number = atoi(portname + 4);
		}
	}
	usb_find_busses();
	usb_find_devices();
	lud->busses = usb_get_busses();
	return garmin_usb_scan(lud, req_unit_number);
}
Example #14
0
int io_init( char *dev )
{
  struct usb_bus *bus;
  struct usb_device *usbdev;
  
  usb_init(); /* initialize the library */
  usb_find_busses(); /* find all busses */
  usb_find_devices(); /* find all connected devices */

  for(bus = usb_get_busses(); bus; bus = bus->next) {
      for(usbdev = bus->devices; usbdev; usbdev = usbdev->next) {
          if(usbdev->descriptor.idVendor == USB_VID_ATMEL 
             && usbdev->descriptor.idProduct == USB_PID_SAMBA) {

	    
	    if( (io_handle = usb_open(usbdev)) ) {
	      if( usb_set_configuration( io_handle, 1 ) < 0 ){
		usb_close(io_handle);
		continue;
	      }
	      if( usb_claim_interface( io_handle, 1 ) < 0 ){
		usb_close(io_handle);
		continue;
	      }
	      return samba_init();

	    }
	  }
	  
      }
  }
  
  fprintf( stderr, "found no compatible usb devices");
  return -1;
}
Example #15
0
nxt_error_t nxt_find(nxt_t *nxt)
{
  struct usb_bus *busses, *bus;
  usb_find_busses();
  usb_find_devices();

  busses = usb_get_busses();
  for (bus = busses; bus != NULL; bus = bus->next)
    {
      struct usb_device *dev;

      for (dev = bus->devices; dev != NULL; dev = dev->next)
        {
          if (dev->descriptor.idVendor == VENDOR_ATMEL &&
              dev->descriptor.idProduct == PRODUCT_SAMBA)
            {
              nxt->dev = dev;
              nxt->is_in_reset_mode = 1;
              return NXT_OK;
            }
          else if (dev->descriptor.idVendor == VENDOR_LEGO &&
                   dev->descriptor.idProduct == PRODUCT_NXT)
            {
              nxt->dev = dev;
              return NXT_OK;
            }
        }
    }

  return NXT_NOT_PRESENT;
}
Example #16
0
Temper *
TemperCreateFromDeviceNumber(int deviceNum, int timeout, int debug)
{
	struct usb_bus *bus;
	int n;

	n = 0;
	for(bus=usb_get_busses(); bus; bus=bus->next) {
	    struct usb_device *dev;

	    for(dev=bus->devices; dev; dev=dev->next) {
		if(debug) {
			printf("Found device: %04x:%04x\n",
			       dev->descriptor.idVendor,
			       dev->descriptor.idProduct);
		}
		if(dev->descriptor.idVendor == VENDOR_ID &&
		   dev->descriptor.idProduct == PRODUCT_ID) {
			if(debug) {
			    printf("Found deviceNum %d\n", n);
			}
			if(n == deviceNum) {
				return TemperCreate(dev, timeout, debug);
			}
			n++;
		}
	    }
	}
	return NULL;
}
static struct usb_device *find_device(uint16_t vendor, uint16_t product)
{
    struct usb_bus *bus;
    struct usb_device *dev;
    struct usb_bus *busses;

    usb_init();
    usb_find_busses();
    usb_find_devices();

    busses = usb_get_busses();

    for(bus = busses; bus; bus = bus->next)
    {
        for(dev = bus->devices; dev; dev = dev->next)
        {
            if((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
            {
                return dev;
            }
        }
    }

    return NULL;
}
Example #18
0
static struct usb_device *
find_colorhug_device (void)
{
	struct usb_bus *bus;
	struct usb_bus *busses;
	struct usb_device *dev;
	struct usb_device *found = NULL;

	busses = usb_get_busses();
	for (bus = busses; bus; bus = bus->next) {
		for (dev = bus->devices; dev; dev = dev->next) {
			if (dev->descriptor.idVendor == CH_USB_VID &&
			    dev->descriptor.idProduct == CH_USB_PID_FIRMWARE) {
				found = dev;
				goto out;
			}
			if (dev->descriptor.idVendor == CH_USB_VID &&
			    dev->descriptor.idProduct == CH_USB_PID_FIRMWARE2) {
				found = dev;
				goto out;
			}
			if (dev->descriptor.idVendor == CH_USB_VID_LEGACY &&
			    dev->descriptor.idProduct == CH_USB_PID2_FIRMWARE) {
				found = dev;
				goto out;
			}
		}
	}
out:
	return found;
}
Example #19
0
struct usb_device* digilent::find_digilent_device()
{
	struct usb_bus* bus;
	struct usb_device* dev;
    int i;

	usb_find_busses();
	usb_find_devices();

    for (bus = usb_get_busses(); bus; bus = bus->next) 
	{
		if (bus->root_dev)
		{
			for (i = 0; i < bus->root_dev->num_children; i++)
		        if (bus->root_dev->children[i]->descriptor.idVendor == USB_VENDOR_ID &&
                    bus->root_dev->children[i]->descriptor.idProduct == USB_PRODUCT_ID)
					// Found it
					return bus->root_dev->children[i];
		}
		else
		{
			for (dev = bus->devices; dev; dev = dev->next)
                if (dev->descriptor.idVendor == USB_VENDOR_ID &&
                    dev->descriptor.idProduct == USB_PRODUCT_ID)
					// Found it
					return dev;
		}
    }

	return NULL;
}
Example #20
0
void FindUsbtinys(void) {

  int usbtinyindex = 1;

  if (UsbInit()) {
    usb_find_busses();
    usb_find_devices();

    struct usb_bus *bus = usb_get_busses();
    while (bus) {

      struct usb_device *device = bus->devices;
      while (device) {

        if (    device->descriptor.idVendor  == VENDOR_ID
            &&  device->descriptor.idProduct == PRODUCT_ID) {
          Assert(PortCount < countof(Ports));
          Ports[PortCount] = malloc(sizeof(struct UPort));
          Assert(Ports[PortCount]);
          Ports[PortCount]->kind      = 'u';
          Ports[PortCount]->index     = usbtinyindex++;
          Ports[PortCount]->character = -1;              // Currently undetermined
          Ports[PortCount]->baud      = 0;               // Currently undetermined
          ((struct UPort*)Ports[PortCount])->handle = 0; // Currently unconnected
          ((struct UPort*)Ports[PortCount])->device = device;
          PortCount++;
        }

        device = device->next;
      }

      bus = bus->next;
    }
  }
}
Example #21
0
static int find_devices(int mode, struct device_info *devinfo, size_t size)
{
	struct usb_bus *bus;
	struct usb_device *dev;
	struct device_id *id;
	int count = 0;

	usb_find_busses();
	usb_find_devices();

	for (bus = usb_get_busses(); bus; bus = bus->next)
		for (dev = bus->devices; dev; dev = dev->next) {
			id = match_device(mode, dev->descriptor.idVendor,
						dev->descriptor.idProduct);
			if (!id)
				continue;

			if (count < size) {
				devinfo[count].dev = dev;
				devinfo[count].id = id;
				count++;
			}
		}

	return count;
}
Example #22
0
struct usb_dev_handle* Bench_Open(WORD vid, WORD pid, INT interfaceNumber, INT altInterfaceNumber, struct usb_device** deviceForHandle)
{
    struct usb_bus* bus;
    struct usb_device* dev;
    struct usb_dev_handle* udev;

    for (bus = usb_get_busses(); bus; bus = bus->next)
    {
        for (dev = bus->devices; dev; dev = dev->next)
        {
            if (dev->descriptor.idVendor == vid && dev->descriptor.idProduct == pid)
            {
				if ((udev = usb_open(dev)))
				{
					if (dev->descriptor.bNumConfigurations)
					{
						if (usb_find_interface(&dev->config[0], interfaceNumber, altInterfaceNumber, NULL) != NULL)
						{
							if (deviceForHandle) *deviceForHandle = dev;
							return udev;
						}
					}

					usb_close(udev);
				}
            }
        }
    }
    return NULL;
}
Example #23
0
int Sdr1kUsb::GetNumDevs()
{
	if (devcount < 0) {
		usb_init();
		devcount = 0;
	}
	int changes = usb_find_busses();
	changes += usb_find_devices();
	if (changes == 0) return devcount;
	devcount = 0;
		
	struct usb_bus* bus = usb_get_busses();
	while (bus) {
		struct usb_device* dev = bus->devices;
		while (dev) {
			if (devcount == USB_MAX_DEVICES) break;
			int VID = dev->descriptor.idVendor;
			int PID = dev->descriptor.idProduct;
			for (int i = 0; i < hwcount; i++) {
				if (VID == hardware[i].VID && PID == hardware[i].PID) {
					device[devcount] = dev;
					hw[devcount] = &hardware[i];
					devcount++;
					break;
				}
			}
			dev = dev->next;
		}
		bus = bus->next;
	}
#ifdef SDR1KUSB_INFO
	printf("Sdr1kUsb::GetNumDevs = %d\n", devcount);
#endif
	return devcount;
}
Example #24
0
bool LibUsb::findFortius()
{
    struct usb_bus* bus;
    struct usb_device* dev;

    bool found = false;

    //
    // Search for an UN-INITIALISED Fortius device
    //
    for (bus = usb_get_busses(); bus; bus = bus->next) {


        for (dev = bus->devices; dev; dev = dev->next) {


            if (dev->descriptor.idVendor == FORTIUS_VID && dev->descriptor.idProduct == FORTIUS_INIT_PID) {
                found = true;
            }
            if (dev->descriptor.idVendor == FORTIUS_VID && dev->descriptor.idProduct == FORTIUS_PID) {
                found = true;
            }
            if (dev->descriptor.idVendor == FORTIUS_VID && dev->descriptor.idProduct == FORTIUSVR_PID) {
                found = true;
            }
        }
    }
    return found;
}
Example #25
0
/*
 * A helper function initializing libusb and finding our iPhone/iPod which has given DeviceID
 */
struct usb_dev_handle *iUSBInit(int devid)
{
	struct usb_dev_handle *devPhone = 0;
	struct usb_device *dev;
	struct usb_bus *bus;

	usb_init();
	usb_find_busses();
	usb_find_devices();

	printf("Got USB\n");
	for(bus = usb_get_busses(); bus; bus = bus->next) {
		//printf("Found BUS\n");
		for(dev = bus->devices; dev; dev = dev->next) {
			//printf("\t%4.4X %4.4X\n", dev->descriptor.idVendor, dev->descriptor.idProduct);
			if(dev->descriptor.idVendor == 0x5AC && dev->descriptor.idProduct == devid) {
				// z0mg!!!! iPhone 4th Generation
//				printf("Found iPhone/iPod in DFU 2.0\n");
				devPhone = usb_open(dev);
			}
		}
	}

	return devPhone;
}
static int usbblaster_enumerate_bus(void)
{
  int             flag;  // for USB bus scanning stop condition
  struct usb_bus *bus;   // pointer on the USB bus
  
  // board detection
  usb_init();
  usb_find_busses();
  usb_find_devices();

  flag = 0;
  
  for (bus = usb_get_busses(); bus; bus = bus->next)
  {
    for (usbblaster_device = bus->devices; usbblaster_device; usbblaster_device = usbblaster_device->next)
    {	
      if (usbblaster_device->descriptor.idVendor  == ALTERA_VID &&
          usbblaster_device->descriptor.idProduct == ALTERA_PID) 
      {
	      flag = 1;
	      fprintf(stderr, "Found Altera USB-Blaster\n");
	      return APP_ERR_NONE;
      }
    }
    if (flag)
      break;
  }

  fprintf(stderr, "Failed to find USB-Blaster  with VID 0x%0X, PID 0x%0X\n", ALTERA_VID, ALTERA_PID);
  return APP_ERR_CABLENOTFOUND;
}
Example #27
0
struct xusb *xusb_find_iface(const char *devpath,
	int iface_num,
	int ep_out,
	int ep_in,
	struct xusb_spec *dummy_spec)
{
	struct usb_bus		*bus;

	DBG("\n");
	xusb_init();
	for (bus = usb_get_busses(); bus; bus = bus->next) {
		int			bus_num;
		char			tmppath[PATH_MAX + 1];
		struct usb_device	*dev;

		tmppath[0] = '\0';
		sscanf(bus->dirname, "%d", &bus_num);
		snprintf(tmppath, sizeof(tmppath), "%03d", bus_num);
		DBG("Check bus %d: %s ? %s\n", bus_num, tmppath, devpath);
		if (strncmp(tmppath, devpath, strlen(tmppath)) != 0)
			continue;
		DBG("Matched bus %d\n", bus_num);
		for (dev = bus->devices; dev; dev = dev->next) {
			struct usb_device_descriptor	*dev_desc;
			struct usb_config_descriptor	*config_desc;
			struct usb_interface		*interface;
			struct xusb			*xusb;
			int				device_num;

			sscanf(dev->filename, "%d", &device_num);
			DBG("Check device %d\n", device_num);
			snprintf(tmppath, sizeof(tmppath), "%03d/%03d",
				bus_num, device_num);
			if (strncmp(tmppath, devpath, strlen(tmppath)) != 0)
				continue;
			dev_desc = &dev->descriptor;
			assert(dev_desc);
			config_desc = dev->config;
			assert(config_desc);
			interface = config_desc->interface;
			assert(interface);
			DBG("Matched device %s: %X:%X\n", tmppath,
				dev_desc->idVendor, dev_desc->idProduct);
			assert(dummy_spec);
			xusb_init_spec(dummy_spec, "<none>",
				dev_desc->idVendor, dev_desc->idProduct,
				config_desc->bNumInterfaces,
				iface_num,
				interface->altsetting->bNumEndpoints,
				ep_out, ep_in);
			xusb = xusb_new(dev, dummy_spec);
			if (!xusb)
				ERR("xusb allocation failed\n");
			return xusb;
		}
	}
	return NULL;
}
Example #28
0
/* taken (modified) from avrdude usbasp.c */
static int usb_open_device(struct usb_dev_handle **device, int vendor, int product)
{
    struct usb_bus      *bus;
    struct usb_device   *dev;
    usb_dev_handle      *handle = NULL;
    int                 errorCode = USB_ERROR_NOTFOUND;
    static int          didUsbInit = 0;

    if (!didUsbInit)
    {
        didUsbInit = 1;
        usb_init();
    }
    usb_find_busses();
    usb_find_devices();
    for (bus=usb_get_busses(); bus; bus=bus->next)
    {
        for (dev=bus->devices; dev; dev=dev->next)
        {
            DEBUG( "Enumerating device list.. VID: 0x%4.4x, PID: 0x%4.4x\n", dev->descriptor.idVendor, dev->descriptor.idProduct);
            if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product)
            {
                /* we need to open the device in order to query strings */
                handle = usb_open(dev);
                if (handle == NULL)
                {
                    errorCode = USB_ERROR_ACCESS;
                    avrdude_message(MSG_INFO, "%s: Warning: cannot open USB device: %s\n", progname, usb_strerror());
                    continue;
                }

                // return with opened device handle
                else
                {
                    avrdude_message(MSG_NOTICE, "Device %p seemed to open OK.\n", handle);

                    if ((errorCode = usb_set_configuration(handle, 1)) < 0)
                    {
                        avrdude_message(MSG_INFO, "Could not set configuration. Error code %d, %s.\n"
                                "You may need to run avrdude as root or set up correct usb port permissions.", errorCode, usb_strerror());
                    }

                    if ((errorCode = usb_claim_interface(handle, 0)) < 0)
                    {
                        avrdude_message(MSG_INFO, "Could not claim interface. Error code %d, %s\n"
                                "You may need to run avrdude as root or set up correct usb port permissions.", errorCode, usb_strerror());
                    }

                    errorCode = 0;
                    *device = handle;
                    return 0;
                }
            }
        }
    }

    return -1;
}
Example #29
0
// open the usb devices identified by vendor and product.
// claim exlclusive access to the interface and enable bit bang mode
// return NULL on failure otherwise a valid usb_dev_handle descriptor
//
// The standard ftdi232bm vendor and product IDs are: 0x0403, 0x6001
usb_dev_handle *ftdibb_open(int vendor, int product,int silent)
{
	usb_dev_handle *dev_handle;
	struct usb_bus *busses;
	struct usb_bus *bus;
	struct usb_device *dev;
	int founddev=0;
    
	usb_init();
	usb_find_busses();
	usb_find_devices();

	busses = usb_get_busses();
	for (bus = usb_busses; bus; bus = bus->next) {
		for (dev = bus->devices; dev; dev = dev->next) {
			if (dev->descriptor.idVendor == vendor 
				&& dev->descriptor.idProduct == product) {
				dev_handle = usb_open(dev);
				if (dev_handle){
					founddev=1;	
					break;
				}else{
					if (silent==0 || silent==1){
						fprintf(stderr,"ftdibb error: Device found but usb_open failed\n");
					}
					return(NULL); // device found but open fail
				}
			}
		}
	}
	if(founddev==0){
		if (silent==0){
			fprintf(stderr,"ftdibb error: Device not connected\n");
		}
		return(NULL); // device not found 
	}
	if (usb_claim_interface(dev_handle, 0) != 0) {
		usb_close (dev_handle);
		if (silent==0 || silent==1){
			fprintf(stderr,"ftdibb error: unable to claim exclusive access to ftdi chip. Make sure ftdi_sio is not loaded (check with lsmod) and make sure you have write access to /proc/bus/usb (root right or suid executable)\n");
		}
		return(NULL); // usb_claim_interface failed
	}
	// now we are ready to configure the device
	// the syntax to send a control urb is this:
	// usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);

	// reset  it first
	if (usb_control_msg(dev_handle, 0x40, 0, 0, 0, NULL, 0, 4000) != 0){
		usb_close (dev_handle);
		if (silent==0 || silent==1){
			fprintf(stderr,"ftdibb error: Device communication failed during reset\n");
		}
		return(NULL); // reset fail
	}
	return(dev_handle);
}
Example #30
0
static size_t
pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
{
  (void)context;

  usb_prepare();

  size_t device_found = 0;
  uint32_t uiBusIndex = 0;
  struct usb_bus *bus;
  for (bus = usb_get_busses(); bus; bus = bus->next) {
    struct usb_device *dev;

    for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) {
      for (size_t n = 0; n < sizeof(pn53x_usb_supported_devices) / sizeof(struct pn53x_usb_supported_device); n++) {
        if ((pn53x_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) &&
            (pn53x_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) {
          // Make sure there are 2 endpoints available
          // with libusb-win32 we got some null pointers so be robust before looking at endpoints:
          if (dev->config == NULL || dev->config->interface == NULL || dev->config->interface->altsetting == NULL) {
            // Nope, we maybe want the next one, let's try to find another
            continue;
          }
          if (dev->config->interface->altsetting->bNumEndpoints < 2) {
            // Nope, we maybe want the next one, let's try to find another
            continue;
          }

          usb_dev_handle *udev = usb_open(dev);
          if (udev == NULL)
            continue;

          // Set configuration
          int res = usb_set_configuration(udev, 1);
          if (res < 0) {
            log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res));
            usb_close(udev);
            // we failed to use the device
            continue;
          }

          // pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice));
          log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s", bus->dirname, dev->filename);
          usb_close(udev);
          snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename);
          device_found++;
          // Test if we reach the maximum "wanted" devices
          if (device_found == connstrings_len) {
            return device_found;
          }
        }
      }
    }
  }

  return device_found;
}