示例#1
0
static int generic_probe(struct usb_device *udev)
{
	int err, c;

	/* Choose and set the configuration.  This registers the interfaces
	 * with the driver core and lets interface drivers bind to them.
	 */
	if (udev->authorized == 0)
		dev_err(&udev->dev, "Device is not authorized for usage\n");
	else {
		c = usb_choose_configuration(udev);
		if (c >= 0) {
			err = usb_set_configuration(udev, c);
			if (err) {
				dev_err(&udev->dev, "can't set config #%d, error %d\n",
					c, err);
				/* This need not be fatal.  The user can try to
				 * set other configurations. */
			}
		}
	}
	/* USB device state == configured ... usable */
	usb_notify_add_device(udev);

	return 0;
}
示例#2
0
int main(int argc, char **argv)
{
  usb_dev_handle      *handle = NULL;
  const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
  char                vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
  char                buffer[50] = "";
  unsigned char	      request;
  int                 vid, pid, nBytes;

  usb_init();

  if(argc < 2){   /* we need at least one argument */
    printf("Arguments Missing");
    exit(1);
  }

  request = atoi(argv[1]);

    /* compute VID/PID from usbconfig.h so that there is a central source of information */
  vid = rawVid[1] * 256 + rawVid[0];
  pid = rawPid[1] * 256 + rawPid[0];

    /* The following function is in opendevice.c: */
  if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){
    fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
    exit(1);
  }

    /* Since we use only control endpoint 0, we don't need to choose a
     * configuration and interface. Reading device descriptor and setting a
     * configuration and interface is done through endpoint 0 after all.
     * However, newer versions of Linux require that we claim an interface
     * even for endpoint 0. Enable the following code if your operating system
     * needs it: */
#if 0
  int retries = 1, usbConfiguration = 1, usbInterface = 0;
  if(usb_set_configuration(handle, usbConfiguration) && showWarnings){
    fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror());
  }
    /* now try to claim the interface and detach the kernel HID driver on
     * Linux and other operating systems which support the call. */
  while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){
#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
    if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){
      fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror());
    }
#endif
  }
#endif


  nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, request, 0, 0, (char *)buffer, sizeof(buffer), 5000);
				
  if(nBytes < 0)
    printf("error in USB control transfer: %s\n", usb_strerror());
				
  usb_close(handle);
  return 0;
}
示例#3
0
static void *rtl8150_probe(struct usb_device *udev, unsigned int ifnum,
			   const struct usb_device_id *id)
{
	rtl8150_t *dev;
	struct net_device *netdev;

	udev->config[0].bConfigurationValue = 1;
	if (usb_set_configuration(udev, udev->config[0].bConfigurationValue)) {
		err("usb_set_configuration() failed");
		return NULL;
	}
	dev = kmalloc(sizeof(rtl8150_t), GFP_KERNEL);
	if (!dev) {
		err("Out of memory");
		goto exit;
	} else
		memset(dev, 0, sizeof(rtl8150_t));

	netdev = init_etherdev(NULL, 0);
	if (!netdev) {
		kfree(dev);
		err("Oh boy, out of memory again?!?");
		dev = NULL;
		goto exit;
	}

	init_MUTEX(&dev->sem);
	dev->udev = udev;
	dev->netdev = netdev;
	SET_MODULE_OWNER(netdev);
	netdev->priv = dev;
	netdev->open = rtl8150_open;
	netdev->stop = rtl8150_close;
	netdev->do_ioctl = rtl8150_ioctl;
	netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
	netdev->tx_timeout = rtl8150_tx_timeout;
	netdev->hard_start_xmit = rtl8150_start_xmit;
	netdev->set_multicast_list = rtl8150_set_multicast;
	netdev->set_mac_address = rtl8150_set_mac_address;
	netdev->get_stats = rtl8150_netdev_stats;
	netdev->mtu = RTL8150_MTU;
	dev->intr_interval = 100;	/* 100ms */

	if (rtl8150_reset(dev) || !alloc_all_urbs(dev)) {
		err("couldn't reset the device");
		free_all_urbs(dev);
		unregister_netdev(dev->netdev);
		kfree(netdev);
		kfree(dev);
		dev = NULL;
		goto exit;
	}

	set_ethernet_addr(dev);
	/* let's not be very nasty :-) */
	info("%s: rtl8150 is detected", netdev->name);
exit:
	return dev;
}
示例#4
0
int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
		int configuration)
{
	struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);

	return usb_set_configuration(devh,
			udev->config[configuration].bConfigurationValue);
}
示例#5
0
文件: usb.c 项目: atoulme/ruby-usb
/* USB::DevHandle#usb_set_configuration(configuration) */
static VALUE
rusb_set_configuration(VALUE v, VALUE configuration)
{
  usb_dev_handle *p = get_usb_devhandle(v);
  int ret = usb_set_configuration(p, NUM2INT(configuration));
  check_usb_error("usb_set_configuration", ret);
  return Qnil;
}
示例#6
0
static int l_usb_set_configuration(lua_State *L) {
	struct usb_dev_handle * dev_handle = lua_touserdata(L, 1);  
	int configuration = lua_tonumber(L, 2);  
	
	int ret = usb_set_configuration(dev_handle, configuration);
	
	return (proccess_return_code(L, ret));
}
示例#7
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;
}
示例#8
0
static void generic_disconnect(struct usb_device *udev)
{
    usb_notify_remove_device(udev);

    /* if this is only an unbind, not a physical disconnect, then
     * unconfigure the device */
    if (udev->actconfig)
        usb_set_configuration(udev, -1);
}
示例#9
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;
}
示例#10
0
文件: pegoda.c 项目: dpavlin/librfid
struct pegoda_handle *pegoda_open(void)
{
	struct usb_device *pegoda;
	unsigned char rbuf[16];
	unsigned int rlen = sizeof(rbuf);
	struct pegoda_handle *ph;

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

	pegoda = find_device(USB_VENDOR_PHILIPS, USB_DEVICE_PEGODA);

	if (!pegoda)
		return NULL;

	ph = malloc(sizeof(*ph));
	if (!ph)
		return NULL;
	memset(ph, 0, sizeof(*ph));

	printf("found pegoda, %u configurations\n",
		pegoda->descriptor.bNumConfigurations);

	printf("config 2 [nr %u] has %u interfaces\n",
		pegoda->config[1].bConfigurationValue,
		pegoda->config[1].bNumInterfaces);

	printf("config 2 interface 0 has %u altsettings\n",
		pegoda->config[1].interface[0].num_altsetting);

	ph->handle = usb_open(pegoda);
	if (!ph->handle) 
		goto out_free;

	if (usb_set_configuration(ph->handle, 2))
		goto out_free;

	printf("configuration 2 successfully set\n");

	if (usb_claim_interface(ph->handle, 0))
		goto out_free;

	printf("interface 0 claimed\n");

	if (usb_set_altinterface(ph->handle, 1))
		goto out_free;

	printf("alt setting 1 selected\n");

	pegoda_transceive(ph, PEGODA_CMD_PCD_CONFIG, NULL, 0, rbuf, &rlen);

	return ph;
out_free:
	free(ph);
	return NULL;
}
示例#11
0
usb_dev_handle *m4Init() {
  struct usb_bus *bus;
  struct usb_device *dev;

  usb_init();

  if (usb_find_busses() < 0) {
    return NULL;
  }

  if (usb_find_devices() < 0) {
    return NULL;
  }

  bus = usb_get_busses();

  while (bus) {
    dev = bus->devices;

    while (dev) {
      if (dev->descriptor.idVendor == VENDOR &&
          dev->descriptor.idProduct == PRODUCT) {
	usb_dev_handle *handle = usb_open(dev);

	if (handle) {
#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
          /* Linux usually claims HID devices for its usbhid driver. */
          usb_detach_kernel_driver_np(handle, 0);
#endif
	  if (usb_set_configuration(handle, 1) >= 0) {
	    if (usb_claim_interface(handle, 0) >= 0) {
	      if (usb_set_altinterface(handle, 0) < 0) {
	        usb_close(handle);
		return NULL;
              }
	    } else {
	      usb_close(handle);
	      return NULL;
	    }
	  } else {
	    usb_close(handle);
	    return NULL;
	  }

	  return handle;
	}
      }

      dev = dev->next;
    }

    bus = bus->next;
  }

  return NULL;
}
示例#12
0
文件: pcsensor.c 项目: longjos/temper
/**
 * Initialize all Temper Devices currently attached to the system
 * @return pcsensor_devices*
 */
pcsensor_devices* setup_libusb_access() {
    static pcsensor_devices *available_devices;

    if (debug) {
        usb_set_debug(255);
    } else {
        usb_set_debug(0);
    }

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

    // Get an array of MAX_SENSORS
    available_devices = find_lvr_winusb();

    if (available_devices->sensor_count == 0) {
        if (debug) {
            printf("Couldn't find the USB device, Exiting\n");
        }
        return NULL;
    }
    int i;

    for (i = 0; i < available_devices->sensor_count; i++) {
        usb_dev_handle *lvr_winusb = available_devices->sensors[i];

        usb_detach(lvr_winusb, INTERFACE1);

        usb_detach(lvr_winusb, INTERFACE2);


        if (usb_set_configuration(lvr_winusb, 0x01) < 0) {
            if (debug) {
                printf("Could not set configuration 1\n");
            }
            return NULL;
        }

        // Microdia tiene 2 interfaces
        if (usb_claim_interface(lvr_winusb, INTERFACE1) < 0) {
            if (debug) {
                printf("Could not claim interface\n");
            }
            return NULL;
        }

        if (usb_claim_interface(lvr_winusb, INTERFACE2) < 0) {
            if (debug) {
                printf("Could not claim interface\n");
            }
            return NULL;
        }
    }
    return available_devices;
}
示例#13
0
/* --------------------------------------------------------------------- */
static void *dabusb_probe (struct usb_device *usbdev, unsigned int ifnum)
{
	int devnum;
	pdabusb_t s;

	dbg("dabusb: probe: vendor id 0x%x, device id 0x%x ifnum:%d",
	  usbdev->descriptor.idVendor, usbdev->descriptor.idProduct, ifnum);

	/* the 1234:5678 is just a self assigned test ID */
	if ((usbdev->descriptor.idVendor != 0x0547 || usbdev->descriptor.idProduct != 0x2131) &&
	    (usbdev->descriptor.idVendor != 0x0547 || usbdev->descriptor.idProduct != 0x9999))
		return NULL;

	/* We don't handle multiple configurations */
	if (usbdev->descriptor.bNumConfigurations != 1)
		return NULL;

	if (ifnum != _DABUSB_IF && usbdev->descriptor.idProduct == 0x9999)
		return NULL;

	devnum = dabusb_find_struct ();
	if (devnum == -1)
		return NULL;

	s = &dabusb[devnum];

	down (&s->mutex);
	s->remove_pending = 0;
	s->usbdev = usbdev;

	if (usb_set_configuration (usbdev, usbdev->config[0].bConfigurationValue) < 0) {
		err("set_configuration failed");
		goto reject;
	}
	if (usbdev->descriptor.idProduct == 0x2131) {
		dabusb_loadmem (s, NULL);
		goto reject;
	}
	else {
		dabusb_fpga_download (s, NULL);

		if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) {
			err("set_interface failed");
			goto reject;
		}
	}
	dbg("bound to interface: %d", ifnum);
	up (&s->mutex);
	MOD_INC_USE_COUNT;
	return s;

      reject:
	up (&s->mutex);
	s->usbdev = NULL;
	return NULL;
}
示例#14
0
void MainWindow::DisconnectUSBDevice( void )
{
  if ( pDevH )
  {
    usb_set_configuration( pDevH, 0 );
    usb_release_interface( pDevH, 0 );
    usb_close( pDevH );
    pDevH = NULL;
  }
}
示例#15
0
bool M64BirDevice::connectDevice()
{
    int err;
    char str[100];

    // At this point device should point to a valid usb_device if a M64Bir was attached
    if( mDevice == NULL )
    {
        QLOG_ERROR() << "No M64BIR device defined: " << usb_strerror();
        return false;
    }

    // Open the USB device
    usb_dev_handle *handle = usb_open( mDevice );

    err = usb_set_configuration( handle, 1 );
    if( err < 0 )
    {
        QLOG_ERROR() << "usb_set_configuration() returned " << usb_strerror();
        usb_close( handle );
        return false;
    }

    err = usb_claim_interface( handle, 0 );
    if( err < 0 )
    {
        QLOG_ERROR() << "usb_claim_interface() returned " << usb_strerror();
        usb_close( handle );
        return false;
    }

    mDeviceHandle = handle;

    // Print device information
    if( mDevice->descriptor.iManufacturer )
    {
        err = usb_get_string_simple( mDeviceHandle, mDevice->descriptor.iManufacturer, str, sizeof(str) );
        if( err > 0 )
        {
            mDeviceManufacturer = str;
            QLOG_INFO() << "Manufacturer is " << mDeviceManufacturer;
        }
    }
    if( mDevice->descriptor.iProduct )
    {
        err = usb_get_string_simple( mDeviceHandle, mDevice->descriptor.iProduct, str, sizeof(str) );
        if( err > 0 )
        {
            mDeviceProductName = str;
            QLOG_INFO() << "Product is " << mDeviceProductName;
        }
    }

    return true;
}
示例#16
0
static ssize_t
set_bConfigurationValue (struct device *dev, const char *buf, size_t count)
{
	struct usb_device	*udev = udev = to_usb_device (dev);
	int			config, value;

	if (sscanf (buf, "%u", &config) != 1 || config > 255)
		return -EINVAL;
	value = usb_set_configuration (udev, config);
	return (value < 0) ? value : count;
}
示例#17
0
文件: cp210x.c 项目: dlbeer/mspdebug
static int open_interface(struct cp210x_transport *tr,
			  struct usb_device *dev, int ino,
			  int baud_rate)
{
#if defined(__linux__)
	int drv;
	char drName[256];
#endif

	printc_dbg(__FILE__": Trying to open interface %d on %s\n",
	       ino, dev->filename);

	tr->int_number = ino;

	tr->handle = usb_open(dev);
	if (!tr->handle) {
		pr_error(__FILE__": can't open device");
		return -1;
	}

#if defined(__linux__)
	drv = usb_get_driver_np(tr->handle, tr->int_number, drName,
				sizeof(drName));
	printc(__FILE__" : driver %d\n", drv);
	if (drv >= 0) {
		if (usb_detach_kernel_driver_np(tr->handle,
						tr->int_number) < 0)
			pr_error(__FILE__": warning: can't detach "
			       "kernel driver");
	}
#endif

#ifdef __Windows__
	if (usb_set_configuration(tr->handle, 1) < 0) {
		pr_error(__FILE__": can't set configuration 1");
		usb_close(tr->handle);
		return -1;
	}
#endif

	if (usb_claim_interface(tr->handle, tr->int_number) < 0) {
		pr_error(__FILE__": can't claim interface");
		usb_close(tr->handle);
		return -1;
	}

	if (configure_port(tr, baud_rate) < 0) {
		printc_err("Failed to configure for V1 device\n");
		usb_close(tr->handle);
		return -1;
	}

	return 0;
}
示例#18
0
static int  empeg_startup (struct usb_serial *serial)
{

	dbg("%s", __FUNCTION__);

	dbg("%s - Set config to 1", __FUNCTION__);
	usb_set_configuration (serial->dev, 1);

	/* continue on with initialization */
	return 0;

}
示例#19
0
文件: usbobex.c 项目: Camelek/qtmoko
/*
 * Function usbobex_connect_request (self)
 *
 *    Open the USB connection
 *
 */
int usbobex_connect_request(obex_t *self)
{
	int ret;
#ifndef _WIN32

	DEBUG(4, "\n");

	self->trans.self.usb.dev_control = usb_open(self->trans.self.usb.device);
	self->trans.self.usb.dev_data = usb_open(self->trans.self.usb.device);

	ret = usb_set_configuration(self->trans.self.usb.dev_control, self->trans.self.usb.configuration);
	if (ret < 0) {
		DEBUG(4, "Can't set configuration %d", ret);
	}

	ret = usb_claim_interface(self->trans.self.usb.dev_control, self->trans.self.usb.control_interface);
	if (ret < 0) {
		DEBUG(4, "Can't claim control interface %d", ret);
		goto err1;
	}

	ret = usb_set_altinterface(self->trans.self.usb.dev_control, self->trans.self.usb.control_setting);
	if (ret < 0) {
		DEBUG(4, "Can't set control setting %d", ret);
		goto err2;
	}

	ret = usb_claim_interface(self->trans.self.usb.dev_data, self->trans.self.usb.data_interface);
	if (ret < 0) {
		DEBUG(4, "Can't claim data interface %d", ret);
		goto err2;
	}

	ret = usb_set_altinterface(self->trans.self.usb.dev_data, self->trans.self.usb.data_active_setting);
	if (ret < 0) {
		DEBUG(4, "Can't set data active setting %d", ret);
		goto err3;
	}
	self->trans.mtu = OBEX_MAXIMUM_MTU;
	DEBUG(2, "transport mtu=%d\n", self->trans.mtu);
	return 1;

err3:
	usb_release_interface(self->trans.self.usb.dev_data, self->trans.self.usb.data_interface);	
err2:
	usb_release_interface(self->trans.self.usb.dev_control, self->trans.self.usb.control_interface);
err1:
	usb_close(self->trans.self.usb.dev_data);
	usb_close(self->trans.self.usb.dev_control);
	return ret;

#endif /* _WIN32 */
}
示例#20
0
文件: pcsensor.c 项目: raff/temper
static usb_dev_handle* setup_libusb_access() {
	usb_dev_handle *lvr_winusb;

	if(debug) {
		usb_set_debug(255);
	} else {
		usb_set_debug(0);
	}
	usb_init();
	usb_find_busses();
	usb_find_devices();

 
	if(!(lvr_winusb = find_lvr_winusb())) {
		if(debug){
			printf("Couldn't find the USB device, Exiting\n");
		}
		return NULL;
	}
        
        
	usb_detach(lvr_winusb, INTERFACE1);
        

	usb_detach(lvr_winusb, INTERFACE2);
        
 
	if (usb_set_configuration(lvr_winusb, 0x01) < 0) {
		if(debug){
			printf("Could not set configuration 1\n");
		}
		return NULL;
	}
 

	// Microdia tiene 2 interfaces
	if (usb_claim_interface(lvr_winusb, INTERFACE1) < 0) {
		if(debug){
			printf("Could not claim interface\n");
		}
		return NULL;
	}
 
	if (usb_claim_interface(lvr_winusb, INTERFACE2) < 0) {
		if(debug){
			printf("Could not claim interface\n");
		}
		return NULL;
	}
 
	return lvr_winusb;
}
示例#21
0
int
usbSetConfiguration (UsbDevice *device, unsigned char configuration) {
  UsbDeviceExtension *devx = device->extension;
  int result;

  logMessage(LOG_CATEGORY(USB_IO), "setting configuration: %u", configuration);
  result = usb_set_configuration(devx->handle, configuration);
  if (result >= 0) return 1;

  errno = -result;
  logSystemError("USB configuration set");
  return 0;
}
示例#22
0
文件: usb_libusb.c 项目: Feechka/UOBP
int
usbSetConfiguration (
  UsbDevice *device,
  unsigned char configuration
) {
  UsbDeviceExtension *devx = device->extension;
  int result = usb_set_configuration(devx->handle, configuration);
  if (result >= 0) return 1;

  errno = -result;
  logSystemError("USB configuration set");
  return 0;
}
示例#23
0
usb_dev_handle *open_device (struct usb_device *dev) {
	usb_dev_handle *hdl;

	hdl = usb_open (dev);
	int open_status = usb_set_configuration (hdl, 1);
	open_status = usb_claim_interface (hdl, 0);
	open_status = usb_set_altinterface (hdl, 0);

	usb_control_msg(hdl, 0x40, 0x0c, 0x5003, 0x00f0, "\x10", 1, 1000); 
	usb_control_msg(hdl, 0x40, 0x0c, 0x5003, 0xffff, "\xd0", 1, 1000); 

	return hdl;
}
示例#24
0
int libusb_claim_interface(libusb_device_handle* dev, int interface_number)
{
	usb_dev_handle* device_handle (translate(dev));
	if (0 == usb_claim_interface(device_handle, interface_number))
	{
		int ok2 = usb_set_configuration(device_handle, 1);
	}
	// LIBUSB_ERROR_NOT_FOUND if the requested interface does not exist
	// LIBUSB_ERROR_BUSY if another program or driver has claimed the interface
	// LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
	// a LIBUSB_ERROR code on other failure
	// 0 on success
	return(0);
}
示例#25
0
static ssize_t
set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
{
	struct usb_device	*udev = to_usb_device(dev);
	int			config, value;

	if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
		return -EINVAL;
	usb_lock_device(udev);
	value = usb_set_configuration(udev, config);
	usb_unlock_device(udev);
	return (value < 0) ? value : count;
}
示例#26
0
static usb_request_status_t usb_standard_request_set_configuration_setup(
	usb_endpoint_t* const endpoint
) {
	const uint8_t usb_configuration = endpoint->setup.value_l;
	if( usb_set_configuration(endpoint->device, usb_configuration) ) {
		if( usb_configuration == 0 ) {
			// TODO: Should this be done immediately?
			usb_set_address_immediate(endpoint->device, 0);
		}
		usb_endpoint_schedule_ack(endpoint->in);
		return USB_REQUEST_STATUS_OK;
	} else {
		return USB_REQUEST_STATUS_STALL;
	}
}
示例#27
0
int initialize_spectrometer()
{
  char cmd;

  /* Set up USB communication */
  if (usb_set_configuration(handle, USB_CONFIGURATION) != 0) {
    fprintf(stderr, "ERROR: Set configuration failed.\n");
    return 1;
  }

  if (usb_claim_interface(handle, USB_INTERFACE) != 0) {
    fprintf(stderr, "ERROR: Claim interface failed.\n");
    return 1;
  }

  atexit(release_spectrometer_interface);

  if (usb_set_altinterface(handle, USB_ALT_INTERFACE) != 0) {
    fprintf(stderr, "ERROR: Set altinterface failed.\n");
    return 1;
  }

  /* Clear halt status */
  if (usb_clear_halt(handle, EP1Out) != 0) {
    fprintf(stderr, "ERROR: Clear halt status on endpoint 1 out failed.\n");
    return 1;
  }

  if (usb_clear_halt(handle, EP1In) != 0) {
    fprintf(stderr, "ERROR: Clear halt status on endpoint 1 in failed.\n");
    return 1;
  }

  if (usb_clear_halt(handle, EP2In) != 0) {
    fprintf(stderr, "ERROR: Clear halt status on endpoint 2 in failed.\n");
    return 1;
  }

  /* Initialise spectrometer */
  cmd = 0x01;
  if (usb_bulk_write(handle, EP1Out, &cmd, 1, TIMEOUT) != 1) {
    fprintf(stderr, "ERROR: Initialise spectrometer failed.\n");
    return 1;
  }

  return 0;
}
示例#28
0
文件: bulk.c 项目: variability/leon3
int main(void)
{
    usb_dev_handle *dev = NULL; /* the device handle */
    char tmp[BUF_SIZE];

    usb_init(); /* initialize the library */
    usb_find_busses(); /* find all busses */
    usb_find_devices(); /* find all connected devices */


    if(!(dev = open_dev()))
    {
        printf("error: device not found!\n");
        return 0;
    }

    if(usb_set_configuration(dev, 1) < 0)
    {
        printf("error: setting config 1 failed\n");
        usb_close(dev);
        return 0;
    }

    if(usb_claim_interface(dev, 0) < 0)
    {
        printf("error: claiming interface 0 failed\n");
        usb_close(dev);
        return 0;
    }

    if(usb_bulk_write(dev, EP_OUT, tmp, sizeof(tmp), 5000)
            != sizeof(tmp))
    {
        printf("error: bulk write failed\n");
    }

    if(usb_bulk_read(dev, EP_IN, tmp, sizeof(tmp), 5000)
            != sizeof(tmp))
    {
        printf("error: bulk read failed\n");
    }

    usb_release_interface(dev, 0);
    usb_close(dev);

    return 0;
}
示例#29
0
文件: usb.c 项目: Aerobota/hackrf
static void usb_bus_reset(usb_device_t* const device) {
	// According to UM10503 v1.4 section 23.10.3 "Bus reset":
	usb_reset_all_endpoints();
	usb_set_address_immediate(device, 0);
	usb_set_configuration(device, 0);
	
	// TODO: Enable endpoint 0, which might not actually be necessary,
	// as the datasheet claims it can't be disabled.

	//wait_ms(3);
	//
	//if( USB0_PORTSC1 & USB0_PORTSC1_PR ) {
	//	// Port still is in the reset state.
	//} else {
	//	usb_hardware_reset();
	//}
}
示例#30
0
bool GammaBlockUSB::deviceReset()
{
#if defined(_MSC_VER) && defined(GAMMA_USB_CYAPI)
	if(!m_usbDevice->IsOpen() || !m_usbDevice->Reset())
	{
		return false;
	}
#else
	if( /*0 > usb_reset(m_usbDevice) ||*/
		0 > usb_set_configuration(m_usbDevice, 1) || 
		0 > usb_claim_interface(m_usbDevice, 0))
	{
		return false;
	}
#endif
	return true;
}