Beispiel #1
0
void
usb_set_config_test(uint16_t vid, uint16_t pid, uint32_t duration)
{
	struct libusb20_device *pdev;
	struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
	int x;
	int error;
	int failed;
	int exp;

	pdev = find_usb_device(vid, pid);
	if (pdev == NULL) {
		printf("USB device not found\n");
		return;
	}
	error = libusb20_dev_open(pdev, 0);
	if (error) {
		printf("Could not open USB device\n");
		libusb20_dev_free(pdev);
		return;
	}
	failed = 0;

	printf("Starting set config test for "
	    "VID=0x%04x PID=0x%04x\n", vid, pid);

	for (x = 255; x > -1; x--) {

		error = libusb20_dev_set_config_index(pdev, x);
		if (error == 0) {
			if (x == 255) {
				printf("Unconfiguring USB device "
				    "was successful\n");
			} else {
				printf("Setting configuration %d "
				    "was successful\n", x);
			}
		} else {
			failed++;
		}
	}

	ddesc = libusb20_dev_get_device_desc(pdev);
	if (ddesc != NULL)
		exp = ddesc->bNumConfigurations + 1;
	else
		exp = 1;

	printf("\n\n"
	    "Set configuration summary\n"
	    "Valid count:  %d/%d %s\n"
	    "Failed count: %d\n",
	    256 - failed, exp,
	    (exp == (256 - failed)) ? "(expected)" : "(unexpected)",
	    failed);

	libusb20_dev_free(pdev);
}
Beispiel #2
0
int
usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
{
	struct usb_device *dev;
	int err;
	uint8_t i;

	/*
	 * Need to translate from "bConfigurationValue" to
	 * configuration index:
	 */

	if (bConfigurationValue == 0) {
		/* unconfigure */
		i = 255;
	} else {
		/* lookup configuration index */
		dev = usb_device(udev);

		/* check if the configuration array is not there */
		if (dev->config == NULL) {
			return (-1);
		}
		for (i = 0;; i++) {
			if (i == dev->descriptor.bNumConfigurations) {
				/* "bConfigurationValue" not found */
				return (-1);
			}
			if ((dev->config + i)->bConfigurationValue ==
			    bConfigurationValue) {
				break;
			}
		}
	}

	err = libusb20_dev_set_config_index((void *)udev, i);

	if (err)
		return (-1);

	return (0);
}
Beispiel #3
0
static void
doit(struct libusb20_device *dev)
{
  int rv;

  if (do_request)
    printf("doit(): bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\n",
	   setup.bmRequestType,
	   setup.bRequest,
	   setup.wValue,
	   setup.wIndex,
	   setup.wLength);

  /*
   * Open the device, allocating memory for two possible (bulk or
   * interrupt) transfers.
   *
   * If only control transfers are intended (via
   * libusb20_dev_request_sync()), transfer_max can be given as 0.
   */
  if ((rv = libusb20_dev_open(dev, 1)) != 0)
    {
      fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
      return;
    }

  /*
   * If the device has more than one configuration, select the desired
   * one here.
   */
  if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
    {
      fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
      return;
    }

  uint8_t *data = 0;
  uint16_t actlen;

  if ((setup.bmRequestType & 0x80) != 0)
    {
      /* this is an IN request, allocate a buffer */
      data = malloc(setup.wLength);
      if (data == 0)
	{
	  fprintf(stderr,
		  "Out of memory allocating %u bytes of reply buffer\n",
		  setup.wLength);
	  return;
	}
    }
  else
    data = out_buf;

  if (do_request)
    {
      if ((rv = libusb20_dev_request_sync(dev, &setup, data,
					  &actlen,
					  TIMEOUT,
					  0 /* flags */)) != 0)
	{
	  fprintf(stderr,
		  "libusb20_dev_request_sync: %s\n", libusb20_strerror(rv));
	}
      printf("sent %d bytes\n", actlen);
      if ((setup.bmRequestType & 0x80) != 0)
	{
	  print_formatted(data, (uint32_t)setup.wLength);
	  free(data);
	}
    }

  if (intr_ep != 0)
    {
      /*
       * One transfer has been requested in libusb20_dev_open() above;
       * obtain the corresponding transfer struct pointer.
       */
      struct libusb20_transfer *xfr_intr = libusb20_tr_get_pointer(dev, 0);

      if (xfr_intr == NULL)
	{
	  fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
	  return;
	}

      /*
       * Open the interrupt transfer.
       */
      if ((rv = libusb20_tr_open(xfr_intr, 0, 1, intr_ep)) != 0)
	{
	  fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
	  return;
	}

      uint8_t in_buf[BUFLEN];
      uint32_t rlen;

      if ((rv = libusb20_tr_bulk_intr_sync(xfr_intr, in_buf, BUFLEN, &rlen, TIMEOUT))
	  != 0)
	{
	  fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
	}
      printf("received %d bytes\n", rlen);
      if (rlen > 0)
	print_formatted(in_buf, rlen);

      libusb20_tr_close(xfr_intr);
    }

  libusb20_dev_close(dev);
}
Beispiel #4
0
static void
doit(struct libusb20_device *dev)
{
  int rv;

  /*
   * Open the device, allocating memory for two possible (bulk or
   * interrupt) transfers.
   *
   * If only control transfers are intended (via
   * libusb20_dev_request_sync()), transfer_max can be given as 0.
   */
  if ((rv = libusb20_dev_open(dev, 2)) != 0)
    {
      fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
      return;
    }

  /*
   * If the device has more than one configuration, select the desired
   * one here.
   */
  if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
    {
      fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
      return;
    }

  /*
   * Two transfers have been requested in libusb20_dev_open() above;
   * obtain the corresponding transfer struct pointers.
   */
  struct libusb20_transfer *xfr_out = libusb20_tr_get_pointer(dev, 0);
  struct libusb20_transfer *xfr_in = libusb20_tr_get_pointer(dev, 1);

  if (xfr_in == NULL || xfr_out == NULL)
    {
      fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
      return;
    }

  /*
   * Open both transfers, the "out" one for the write endpoint, the
   * "in" one for the read endpoint (ep | 0x80).
   */
  if ((rv = libusb20_tr_open(xfr_out, 0, 1, out_ep)) != 0)
    {
      fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
      return;
    }
  if ((rv = libusb20_tr_open(xfr_in, 0, 1, in_ep)) != 0)
    {
      fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
      return;
    }

  uint8_t in_buf[BUFLEN];
  uint32_t rlen;

  if (out_len > 0)
    {
      if ((rv = libusb20_tr_bulk_intr_sync(xfr_out, out_buf, out_len, &rlen, TIMEOUT))
	  != 0)
	{
	  fprintf(stderr, "libusb20_tr_bulk_intr_sync (OUT): %s\n", libusb20_strerror(rv));
	}
      printf("sent %d bytes\n", rlen);
    }

  if ((rv = libusb20_tr_bulk_intr_sync(xfr_in, in_buf, BUFLEN, &rlen, TIMEOUT))
      != 0)
    {
      fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
    }
      printf("received %d bytes\n", rlen);
      if (rlen > 0)
	print_formatted(in_buf, rlen);

  libusb20_tr_close(xfr_out);
  libusb20_tr_close(xfr_in);

  libusb20_dev_close(dev);
}