Ejemplo n.º 1
0
void FX3Stream::init(unsigned short vid, unsigned short pid) {
    this->vid = vid;
    this->pid = pid;
    error = libusb_init(0);
    if( error < 0 ) {
        cerr << "Error initialising libusb: " << libusb_error_name(error) << endl;
        return;
    }
    if( FX3_DEBUG ) {
        libusb_set_debug(0,LIBUSB_LOG_LEVEL_WARNING);
    }
    devhandle = libusb_open_device_with_vid_pid(0, vid, pid);
    if( !devhandle ) {
        cerr << "Error opening the device" << endl;
        error = 1;
        return;
    }
    error = libusb_reset_device( devhandle );
    if( error < 0 ) {
        cerr << "Error resetting the device: " << libusb_error_name(error) << endl;
        return;
    }
    error = libusb_kernel_driver_active( devhandle, 1 );
    if( error < 0 ) {
        cerr << "Error checking for kernel driver: " << libusb_error_name(error) << endl;
        return;
    }
    else if( error == 1 ) {
        cerr << "Error: Kernel driver active." << endl;
        return;
    }
    error = libusb_set_configuration( devhandle, 1 );
    if( error < 0 ) {
        cerr << "Error setting the configuration: " << libusb_error_name(error) << endl;
        return;
    }
    error =  libusb_claim_interface( devhandle, 0 );
    if( error < 0 ) {
        cerr << "Error claiming the interface: " << libusb_error_name(error) << endl;
        return;
    }
    interfaceClaimed = true;
    inPktSize = libusb_get_max_packet_size( libusb_get_device(devhandle), FX3_IN_EP );
    if( inPktSize < 0 ) {
        cerr << "Error getting IN EP packet size: " << libusb_error_name(error) << endl;
        return;
    }
    outPktSize = libusb_get_max_packet_size( libusb_get_device(devhandle), FX3_OUT_EP );
    if( outPktSize < 0 ) {
        cerr << "Error getting OUT EP packet size: " << libusb_error_name(error) << endl;
        return;
    }
    if( FX3_DEBUG ) {
        cerr << "inPktSize: " << inPktSize << ", outPktSize: " << outPktSize << endl;
    }
    allocTransfers();
}
Ejemplo n.º 2
0
void init_mouse2(libusbSource * usb_source){
	int iface_nums[1] = {0};
	libusb_device_handle * handle = open_usb_device_handle(usb_source, is_mouse2, iface_nums, 1);
	if(!handle) {
		return;
	}

	g_packet_size = libusb_get_max_packet_size (libusb_get_device (handle), g_dev_IN_EP);

	start_mouse_transfer(handle, g_dev_IN_EP, mouse_cb, NULL, 0);
}
Ejemplo n.º 3
0
void init()
{
  memset(&_ctx_data, 0, sizeof (_ctx_data));
  _ctx = &_ctx_data;
  libusb_init( & _ctx->libusb_ctx );
  _ctx->dev_handle = libusb_open_device_with_vid_pid(_ctx->libusb_ctx, LEAP_VID, LEAP_PID);
  if (_ctx->dev_handle == NULL) {
    fprintf(stderr, "ERROR: can't find leap.\n");
    exit(EXIT_FAILURE);
  }

  debug_printf("Found leap\n");

  int ret;

  ret = libusb_reset_device(_ctx->dev_handle);
  debug_printf("libusb_reset_device() ret: %i: %s\n", ret, libusb_error_name(ret));

  ret = libusb_kernel_driver_active(_ctx->dev_handle, 0);
  if ( ret == 1 ) {
    debug_printf("kernel active for interface 0\n");
    libusb_detach_kernel_driver(_ctx->dev_handle, 0);
  }
  else if (ret != 0) {
    printf("error\n");
    exit(EXIT_FAILURE);
  }

  ret = libusb_kernel_driver_active(_ctx->dev_handle, 1);
  if ( ret == 1 ) {
    debug_printf("kernel active\n");
    libusb_detach_kernel_driver(_ctx->dev_handle, 1);
  }
  else if (ret != 0) {
    printf("error\n");
    exit(EXIT_FAILURE);
  }

  ret = libusb_claim_interface(_ctx->dev_handle, 0);
  debug_printf("libusb_claim_interface() ret: %i: %s\n", ret, libusb_error_name(ret));

  ret = libusb_claim_interface(_ctx->dev_handle, 1);
  debug_printf("libusb_claim_interface() ret: %i: %s\n", ret, libusb_error_name(ret));

  leap_init(_ctx);

  debug_printf( "max %i\n",  libusb_get_max_packet_size(libusb_get_device( _ctx->dev_handle ), 0x83));
}
Ejemplo n.º 4
0
int
libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
{
	int multiplier;
	int ret;

	ret = libusb_get_max_packet_size(dev, endpoint);

	switch (libusb20_dev_get_speed(dev->os_priv)) {
	case LIBUSB20_SPEED_LOW:
	case LIBUSB20_SPEED_FULL:
		break;
	default:
		if (ret > -1) {
			multiplier = (1 + ((ret >> 11) & 3));
			if (multiplier > 3)
				multiplier = 3;
			ret = (ret & 0x7FF) * multiplier;
		}
		break;
	}
Ejemplo n.º 5
0
static int
gp_libusb1_find_path_lib(GPPort *port)
{
	char *s;
	int d, busnr = 0, devnr = 0;
	GPPortPrivateLibrary *pl;

	C_PARAMS (port);

	pl = port->pl;

	s = strchr (port->settings.usb.port,':');
	C_PARAMS (s && (s[1] != '\0'));
	C_PARAMS (sscanf (s+1, "%d,%d", &busnr, &devnr) == 2); /* usb:%d,%d */

	pl->nrofdevs = load_devicelist (port->pl);

	for (d = 0; d < pl->nrofdevs; d++) {
		struct libusb_config_descriptor *confdesc;
		int config = -1, interface = -1, altsetting = -1;

		if (busnr != libusb_get_bus_number (pl->devs[d]))
			continue;
		if (devnr != libusb_get_device_address (pl->devs[d]))
			continue;

		port->pl->d = pl->devs[d];

		GP_LOG_D ("Found path %s", port->settings.usb.port);

		/* Use the first config, interface and altsetting we find */
		gp_libusb1_find_first_altsetting(pl->devs[d], &config, &interface, &altsetting);

		if (LOG_ON_LIBUSB_E (libusb_get_config_descriptor (pl->devs[d], config, &confdesc)))
			continue;

		/* Set the defaults */
		port->settings.usb.config = confdesc->bConfigurationValue;
		port->settings.usb.interface = confdesc->interface[interface].altsetting[altsetting].bInterfaceNumber;
		port->settings.usb.altsetting = confdesc->interface[interface].altsetting[altsetting].bAlternateSetting;

		port->settings.usb.inep  = gp_libusb1_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK);
		port->settings.usb.outep = gp_libusb1_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK);
		port->settings.usb.intep = gp_libusb1_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT);

		port->settings.usb.maxpacketsize = libusb_get_max_packet_size (pl->devs[d], port->settings.usb.inep);
		GP_LOG_D ("Detected defaults: config %d, interface %d, altsetting %d, "
			"inep %02x, outep %02x, intep %02x, class %02x, subclass %02x",
			port->settings.usb.config,
			port->settings.usb.interface,
			port->settings.usb.altsetting,
			port->settings.usb.inep,
			port->settings.usb.outep,
			port->settings.usb.intep,
			confdesc->interface[interface].altsetting[altsetting].bInterfaceClass,
			confdesc->interface[interface].altsetting[altsetting].bInterfaceSubClass
			);
		libusb_free_config_descriptor (confdesc);
		return GP_OK;
	}
#if 0
	gp_port_set_error (port, _("Could not find USB device "
		"(vendor 0x%x, product 0x%x). Make sure this device "
		"is connected to the computer."), idvendor, idproduct);
#endif
	return GP_ERROR_IO_USB_FIND;
}
Ejemplo n.º 6
0
static int
gp_libusb1_find_device_lib(GPPort *port, int idvendor, int idproduct)
{
	char *s;
	int d, busnr = 0, devnr = 0;
	GPPortPrivateLibrary *pl;

	C_PARAMS (port);

	pl = port->pl;

	s = strchr (port->settings.usb.port,':');
	if (s && (s[1] != '\0')) { /* usb:%d,%d */
		if (sscanf (s+1, "%d,%d", &busnr, &devnr) != 2) {
			devnr = 0;
			sscanf (s+1, "%d", &busnr);
		}
	}
	/*
	 * 0x0000 idvendor is not valid.
	 * 0x0000 idproduct is ok.
	 * Should the USB layer report that ? I don't know.
	 * Better to check here.
	 */
	if (!idvendor) {
		gp_port_set_error (port, _("The supplied vendor or product "
			"id (0x%x,0x%x) is not valid."), idvendor, idproduct);
		return GP_ERROR_BAD_PARAMETERS;
	}

	pl->nrofdevs = load_devicelist (port->pl);

	for (d = 0; d < pl->nrofdevs; d++) {
		struct libusb_config_descriptor *confdesc;
		int config = -1, interface = -1, altsetting = -1;

		if ((pl->descs[d].idVendor != idvendor) ||
		    (pl->descs[d].idProduct != idproduct))
			continue;

		if (busnr && (busnr != libusb_get_bus_number (pl->devs[d])))
			continue;
		if (devnr && (devnr != libusb_get_device_address (pl->devs[d])))
			continue;

		port->pl->d = pl->devs[d];

		GP_LOG_D ("Looking for USB device (vendor 0x%x, product 0x%x)... found.", idvendor, idproduct);

		/* Use the first config, interface and altsetting we find */
		gp_libusb1_find_first_altsetting(pl->devs[d], &config, &interface, &altsetting);

		if (LOG_ON_LIBUSB_E (libusb_get_config_descriptor (pl->devs[d], config, &confdesc)))
			continue;

		/* Set the defaults */
		if (confdesc->interface[interface].altsetting[altsetting].bInterfaceClass
		    == LIBUSB_CLASS_MASS_STORAGE) {
			GP_LOG_D ("USB device (vendor 0x%x, product 0x%x) is a mass"
				  " storage device, and might not function with gphoto2."
				  " Reference: %s", idvendor, idproduct, URL_USB_MASSSTORAGE);
		}
		port->settings.usb.config = confdesc->bConfigurationValue;
		port->settings.usb.interface = confdesc->interface[interface].altsetting[altsetting].bInterfaceNumber;
		port->settings.usb.altsetting = confdesc->interface[interface].altsetting[altsetting].bAlternateSetting;

		port->settings.usb.inep  = gp_libusb1_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK);
		port->settings.usb.outep = gp_libusb1_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK);
		port->settings.usb.intep = gp_libusb1_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT);

		port->settings.usb.maxpacketsize = libusb_get_max_packet_size (pl->devs[d], port->settings.usb.inep);
		GP_LOG_D ("Detected defaults: config %d, interface %d, altsetting %d, "
			  "inep %02x, outep %02x, intep %02x, class %02x, subclass %02x",
			port->settings.usb.config,
			port->settings.usb.interface,
			port->settings.usb.altsetting,
			port->settings.usb.inep,
			port->settings.usb.outep,
			port->settings.usb.intep,
			confdesc->interface[interface].altsetting[altsetting].bInterfaceClass,
			confdesc->interface[interface].altsetting[altsetting].bInterfaceSubClass
			);
		libusb_free_config_descriptor (confdesc);
		return GP_OK;
	}
#if 0
	gp_port_set_error (port, _("Could not find USB device "
		"(vendor 0x%x, product 0x%x). Make sure this device "
		"is connected to the computer."), idvendor, idproduct);
#endif
	return GP_ERROR_IO_USB_FIND;
}
Ejemplo n.º 7
0
int cyusb_get_max_packet_size(cyusb_handle *h, unsigned char endpoint)
{
	cyusb_device *tdev = libusb_get_device(h);
	return ( libusb_get_max_packet_size(tdev, endpoint) );
}
Ejemplo n.º 8
0
///////////////////////////////////////////////////////////////////////////////
///@author      Nicolae Bodislav
///@brief       Reads data from the USB device on bulk.
///@param       [out]p_pData - Buffer to store output data
///@param       [in]p_nLegth - Size of the output buffer
///@param       [in]p_nMiliSec - How much to wait for data before abandoning the read operation
///@retval      size of the data written in the output buffer or < 0 for error
///////////////////////////////////////////////////////////////////////////////
int CUSBLink::ReadBulk(unsigned char* p_pData, uint32_t p_nLegth, unsigned int p_nMiliSec /*= 10*/ )
{
    m_nCb = 0;
    m_nTrasferedLength = 0;

    if(m_pDevh == NULL)
    {
        NLOG_ERR("[CUSBLink][ReadBulk] Device not open.");
        return -1;
    }

    int rez = libusb_claim_interface(m_pDevh, 0);
    if (rez < 0)
    {
        NLOG_ERR("[CUSBLink][ReadBulk] Couldn't claim interface.");
        return -1;
    }

    m_enumDealoc = CUSBLink::INTERFACE;

    m_pData_transfer = NULL;
    m_pData_transfer = libusb_alloc_transfer(0);
    if (!m_pData_transfer)
    {
        NLOG_ERR("[CUSBLink][ReadBulk] Transfer couldn't be allocated.");
        return -ENOMEM;
    }

    m_pDev = libusb_get_device(m_pDevh);
    int max_packet_size = libusb_get_max_packet_size(m_pDev, m_cReadEndpoint);

    libusb_fill_bulk_transfer(m_pData_transfer, m_pDevh, m_cReadEndpoint, m_pData, max_packet_size, Cb_TransferRead, this, p_nMiliSec);

    m_enumDealoc = CUSBLink::TRANSFER;

    rez = libusb_submit_transfer(m_pData_transfer);

    if (rez < 0)
    {
        NLOG_ERR("[CUSBLink][ReadBulk] Transfer couldn't be submitted.");

        libusb_cancel_transfer(m_pData_transfer);
        m_enumDealoc = CUSBLink::INTERFACE;

        while (!m_nCb)
        {   rez = libusb_handle_events(m_pContex);
            if (rez < 0)
            {   break;
            }
        }

        libusb_free_transfer(m_pData_transfer);

        return -1;
    }

    while (!m_nCb)
    {   rez = libusb_handle_events(m_pContex);
        if (rez < 0)
        {   libusb_free_transfer(m_pData_transfer);
            return -1;
        }
    }


    if(m_bHaveData)
    {
        if(m_nTrasferedLength > 0)
        {   memcpy(p_pData, m_pData, m_nTrasferedLength);
            if (m_bRawLog)
            {   NLOG_DBG_HEX("[CUSBLink][ReadBulk] Received packet:", m_pData, m_nTrasferedLength);
            }
        }
        else
            p_pData = NULL;
    }

    return m_nTrasferedLength;
}
Ejemplo n.º 9
0
RESPONSECODE IFDHCreateChannel ( DWORD Lun, DWORD Channel ) {

    //RESPONSECODE IO_Create_Channel ( DWORD Channel ) {
    //DWORD Lun = 0;

  /* Lun - Logical Unit Number, use this for multiple card slots 
     or multiple readers. 0xXXXXYYYY -  XXXX multiple readers,
     YYYY multiple slots. The resource manager will set these 
     automatically.  By default the resource manager loads a new
     instance of the driver so if your reader does not have more than
     one smartcard slot then ignore the Lun in all the functions.
     Future versions of PC/SC might support loading multiple readers
     through one instance of the driver in which XXXX would be important
     to implement if you want this.
  */
  
  /* Channel - Channel ID.  This is denoted by the following:
     0x000001 - /dev/pcsc/1
     0x000002 - /dev/pcsc/2
     0x000003 - /dev/pcsc/3
     
     USB readers may choose to ignore this parameter and query 
     the bus for the particular reader.
  */

  /* This function is required to open a communications channel to the 
     port listed by Channel.  For example, the first serial reader on COM1 would
     link to /dev/pcsc/1 which would be a sym link to /dev/ttyS0 on some machines
     This is used to help with intermachine independance.
     
     Once the channel is opened the reader must be in a state in which it is possible
     to query IFDHICCPresence() for card status.
 
     returns:

     IFD_SUCCESS
     IFD_COMMUNICATION_ERROR
  */

  syslog(LOG_INFO, "This is the Myson driver");
  srand(time(0));
  
  int readerNum = (Lun & 0xFFFF0000) >> 16;
  
  libusb_context *context;
  if(libusb_init(&context) != 0) //unable to initialize libusb
  {
    syslog(LOG_INFO, "Unable to initialize libusb");
    return IFD_COMMUNICATION_ERROR;
  }
  rd[readerNum].context = context;
  
  rd[readerNum].handle = libusb_open_device_with_vid_pid(context, 0x04cf, 0x9920);
  syslog(LOG_INFO, "Success");
  
  if(rd[readerNum].handle == NULL)
  {
    syslog(LOG_INFO, "Did you connect the Myson?");
    return IFD_COMMUNICATION_ERROR;
  }
  
  libusb_device* dev = libusb_get_device(rd[readerNum].handle);
  
  //avoid conflict with an existing mass storage driver
  //this function, although it is in the documentation, is absent from libusb.h...
  //libusb_set_auto_detach_kernel_driver(rd[readerNum].handle,1);
  
  //we will claim the interface
  if(libusb_kernel_driver_active(rd[readerNum].handle,0) == 1) //then we free it
  {
    if(libusb_detach_kernel_driver(rd[readerNum].handle,0) != 0) //error when freeing?
    {
      syslog(LOG_INFO, "Unable to detach interface from kernel driver");
      libusb_close(rd[readerNum].handle);
      libusb_exit(context);
      return IFD_COMMUNICATION_ERROR;
    }
  }
  if(libusb_claim_interface(rd[readerNum].handle, 0) != 0)
  {
    syslog(LOG_INFO, "Unable to claim interface");
    libusb_close(rd[readerNum].handle);
    libusb_exit(context);
    return IFD_COMMUNICATION_ERROR;
  }

  syslog(LOG_INFO, "Myson successfully initialized");
  int maxsize = libusb_get_max_packet_size(dev, IN_ENDPOINT);
  printf("Max IN packet size: %d\n", maxsize);
  maxsize = libusb_get_max_packet_size(dev, OUT_ENDPOINT);
  printf("Max OUT packet size: %d\n", maxsize);
  
  return IFD_SUCCESS;
}