Example #1
0
static void libusb_hid_free(void *data)
{
   libusb_hid_t *hid = (libusb_hid_t*)data;

   while(adapters.next)
      if (remove_adapter(hid, adapters.next->device) == -1)
         RARCH_ERR("could not remove device %p\n",
               adapters.next->device);

   if (hid->poll_thread)
   {
      hid->quit = 1;
      sthread_join(hid->poll_thread);
   }

   pad_connection_destroy(hid->slots);

   libusb_hotplug_deregister_callback(hid->ctx, hid->hp);

   libusb_exit(hid->ctx);
   free(hid);
}
Example #2
0
int exit_libusb(struct libusb_context **ctx)
{
	CHECK_ERROR(ctx == NULL, -1, "ctx is NULL in exit_libusb()");
	CHECK_ERROR(*ctx != libusb_ctx, -1, "*ctx != libusb_ctx in exit_libusb()");

	pthread_mutex_lock(&refcnt_mutex);
	if(init_refcnt <=0) {
		WARNING("Libusb init reference counter is < 0 (%i)!"
					"exit_libusb() must have been called "
					"too many times.", init_refcnt);
			init_refcnt=0;
			pthread_mutex_unlock(&refcnt_mutex);
	}
	init_refcnt--;
	pthread_mutex_unlock(&refcnt_mutex);

	if(init_refcnt == 0)
		libusb_exit(libusb_ctx);

	*ctx = NULL;
	return 0;
}
Example #3
0
int HidCP2112_Disconnect() {

	if (!handle) {
		fprintf(stderr, BOLDRED "Cannot disconnect: No device connected." RESET "\n");
		return -1;
	}

	// stop receive thread
 	running = 0;//FALSE;
 	pthread_join(recvThread, NULL);

 	// release iterface and close device handle
	libusb_release_interface(handle, INTERFACE);
	libusb_close(handle);
	handle = NULL;

	// exit context
	libusb_exit(ctx);
	pthread_exit(NULL);

	return 0;
}
Example #4
0
static int icdi_usb_close(void *handle)
{
	struct icdi_usb_handle_s *h;

	h = (struct icdi_usb_handle_s *)handle;

	if (h->usb_dev)
		libusb_close(h->usb_dev);

	if (h->usb_ctx)
		libusb_exit(h->usb_ctx);

	if (h->read_buffer)
		free(h->read_buffer);

	if (h->write_buffer)
		free(h->write_buffer);

	free(handle);

	return ERROR_OK;
}
Example #5
0
int yyyUSB_stop(yContextSt *ctx,char *errmsg)
{
    int i;
    stringCacheSt *c = stringCache;

    if(ctx->usb_thread_state == USB_THREAD_RUNNING){
        ctx->usb_thread_state = USB_THREAD_MUST_STOP;
        pthread_join(ctx->usb_thread,NULL);
    }
    YASSERT(ctx->usb_thread_state == USB_THREAD_STOPED);

    libusb_exit(ctx->libusb);
    yReleaseGlobalAccess(ctx);
    for (i = 0; i < STRING_CACHE_SIZE; i++, c++) {
        if(c->string) {
            yFree(c->string);
        }
    }
    yDeleteCriticalSection(&ctx->string_cache_cs);

    return YAPI_SUCCESS;
}
Example #6
0
int main(void)
{
	libusb_device **devs;
	int r;
	ssize_t cnt;

	r = libusb_init(NULL);
	if (r < 0)
		return r;

	cnt = libusb_get_device_list(NULL, &devs);
	if (cnt < 0)
		return (int) cnt;



	print_devs(devs);
	libusb_free_device_list(devs, 1);

	libusb_exit(NULL);
	return 0;
}
Example #7
0
static void usb_close(struct aura_node *node)
{
	struct usb_dev_info *inf = aura_get_transportdata(node);
	inf->itransfer_enabled = false;
	/* Waiting for pending transfers */
	slog(4, SLOG_INFO, "usb: Waiting for transport to close...");
	usb_panic_and_reset_state(node);

	while (inf->state != AUSB_DEVICE_RESTART) 
		libusb_handle_events(inf->ctx);

	slog(4, SLOG_INFO, "usb: Cleaning up...");
	libusb_free_transfer(inf->ctransfer); 
	libusb_free_transfer(inf->itransfer); 
	free(inf->ctrlbuf);
	if (inf->handle)
		libusb_close(inf->handle); 
	libusb_exit(inf->ctx);
	if (inf->optbuf)
		free(inf->optbuf);
	free(inf);
}
Example #8
0
File: usb.c Project: seec/TL866
void close_devices()
{
    printf("Close devices.\n");
    if(devs != NULL)
    {

        int i;
        pthread_mutex_lock(&lock);
        for(i = 0; i<4; i++)
        {
            if(device_handle[i] != NULL)
            {
                libusb_close(device_handle[i]);
                device_handle[i] = NULL;
            }
        }
        libusb_free_device_list(devs, 1);
        libusb_exit(ctx);//close session
        devs = NULL;
        pthread_mutex_unlock(&lock);
    }
}
Example #9
0
int main(int argc, char **argv) {

    libusb_context *context = NULL;
	CHECK_LIBUSB_RETURNED(libusb_init(&context));

	board *robot;
	int ret = butiac_init_singleboard(&robot);
	DEBUG_PRINT_D("Board init: ", ret)
	CHECK_LIBUSB_RETURNED(ret)

	//module mot = { robot, motors, 8, 1 };
    //DEBUG_PRINT_D("Open module motors: ", mod_open(&mot));
    b_set_motors_speed(robot, 1, 1000, 1, 1000);
    sleep(5);
    b_set_motors_speed(robot, 0, 0, 0, 0);

	board_close(robot);

	libusb_exit(context);
	return 0;

}
Example #10
0
static void aspspi_close(PROGRAMMER * pgm)
{
  if (verbose > 2)
    fprintf(stderr, "%s: aspspi_close()\n", progname);

  if (PDATA(pgm)->usbhandle!=NULL) {
    unsigned char temp[4] = {0, 0, 0, 0};

        aspspi_transmit(pgm, 1, USBASP_FUNC_DISCONNECT, temp, temp, sizeof(temp));

#ifdef USE_LIBUSB_1_0
    libusb_close(PDATA(pgm)->usbhandle);
#else
    usb_close(PDATA(pgm)->usbhandle);
#endif
  }
#ifdef USE_LIBUSB_1_0
  libusb_exit(ctx);
#else
  /* nothing for usb 0.1 ? */
#endif
}
Example #11
0
int main(int argc,char **argv)
{
    (void)argc;
    (void)argv;
    
    assert(libusb_init(NULL)==0);
    
    libusb_set_debug(NULL,3);
    
    libusb_device **device_list;
    ssize_t list_size=libusb_get_device_list(NULL,&device_list);
    
    assert(list_size>=0);
    
    libusb_device *found = NULL;
    for(ssize_t i=0;i<list_size;i++)
    {
        if(is_debug(device_list[i]))
            found=device_list[i];
    }
    
    if(found)
    {
        libusb_device_handle *handle;
        int err = libusb_open(found,&handle);
        assert(err==0);

        main_loop(handle);

        libusb_close(handle);
    }
    
    libusb_free_device_list(device_list,1);
    
    libusb_exit(NULL);
    
    return 0;
}
Example #12
0
int usb_close(int usb_number)
{
  struct usb_state* state = usb_states+usb_number;

  if(state->devh)
  {
    if(state->type == C_TYPE_XONE_PAD)
    {
      unsigned char power_off[] = { 0x05, 0x20, 0x00, 0x01, 0x04 };
      usb_send_interrupt_out_sync(usb_number, power_off, sizeof(power_off));
    }

    cancel_transfers();//TODO MLA: selective cancels

    libusb_release_interface(state->devh, controller[state->type].interface);
#if !defined(LIBUSB_API_VERSION) && !defined(LIBUSBX_API_VERSION)
#ifndef WIN32
    libusb_attach_kernel_driver(state->devh, 0);
#endif
#endif
    libusb_close(state->devh);
    state->devh = NULL;
    --nb_opened;
    if(!nb_opened)
    {
      libusb_free_device_list(devs, 1);
      devs = NULL;
      if(ctx) {
        libusb_exit(ctx);
        ctx = NULL;
      }
    }
  }

  set_done();

  return 1;
}
Example #13
0
libusb_device_handle* usb_device_find_USB500( int vendorId, int productId )
{
  struct libusb_device_handle *udev = NULL;
  int ret = 0;

  libusb_init(NULL);
  if (ret < 0) {
    perror("usb_device_find_USB500: Failed to initialize libusb");
    exit(1);
  }
  udev = libusb_open_device_with_vid_pid(NULL, vendorId, productId);
  if (udev == NULL) {
    return 0;
  }
  printf("Vendor ID = %#x    Product ID = %#x\n", vendorId, productId);
  
  ret = libusb_set_configuration(udev, 1);
  if (ret < 0) {
    perror("Error setting configuration\n");
    goto out;
  }
  
  /* claim interface */
  ret = libusb_claim_interface(udev, 0);
  if (ret < 0) {
    perror("Error claiming usb interface 0\n");
    goto out_release;
  }
  
  return udev;

 out_release:
  libusb_release_interface(udev,0);
 out:
  libusb_close(udev);
  libusb_exit(NULL);
  exit(0);
}
/** Tests that devices can be listed 1000 times. */
static libusb_testlib_result test_get_device_list(libusb_testlib_ctx * tctx)
{
	libusb_context * ctx = NULL;
	int r, i;
	r = libusb_init(&ctx);
	if (r != LIBUSB_SUCCESS) {
		libusb_testlib_logf(tctx, "Failed to init libusb: %d", r);
		return TEST_STATUS_FAILURE;
	}
	for (i = 0; i < 1000; ++i) {
		libusb_device ** device_list;
		ssize_t list_size = libusb_get_device_list(ctx, &device_list);
		if (list_size < 0 || device_list == NULL) {
			libusb_testlib_logf(tctx,
				"Failed to get device list on iteration %d: %d (%p)",
				i, -list_size, device_list);
			return TEST_STATUS_FAILURE;
		}
		libusb_free_device_list(device_list, 1);
	}
	libusb_exit(ctx);
	return TEST_STATUS_SUCCESS;
}
Example #15
0
static void lusb_close_bootloader(void *driver)
{
    int status;
    struct bladerf_lusb *lusb = (struct bladerf_lusb *) driver;

    if (lusb != NULL) {
        if (lusb->handle != NULL) {
            status = libusb_release_interface(lusb->handle, 0);
            if (status < 0) {
                log_debug("Failed to release interface: %s\n",
                           libusb_error_name(status));
            }

            libusb_close(lusb->handle);
        }

        if (lusb->context != NULL) {
            libusb_exit(lusb->context);
        }

        free(lusb);
    }
}
Example #16
0
int32_t main(void)
{
	libusb_device **devs;
	int32_t r;
	ssize_t cnt;

	r = libusb_init(NULL);
	if(r < 0)
		{ return r; }

	printf("Looking for smartreader compatible devices...\n");

	cnt = libusb_get_device_list(NULL, &devs);
	if(cnt < 0)
		{ return (int32_t) cnt; }

	print_devs(devs);
	libusb_free_device_list(devs, 1);

	libusb_exit(NULL);

	return 0;
}
Example #17
0
static int perform_recovery(struct cli_state *state,
                            uint8_t bus, uint8_t addr,
                            const char *firmware_file)
{
    int status;
    libusb_context *ctx;
    libusb_device_handle *handle = NULL;

    status = libusb_init(&ctx);
    if (status != 0) {
        return CMD_RET_UNKNOWN;
    }

    status = find_device(state, ctx, bus, addr, &handle, false);
    if (status == 0) {
        status = ezusb_load_ram(handle, firmware_file,
                                FX_TYPE_FX3, IMG_TYPE_IMG, 0);
    }

    libusb_close(handle);
    libusb_exit(ctx);
    return status;
}
int main(void)
{
#ifdef USE_LIBUSB_1_0
    libusb_device_handle *usbhandle;
#else
    usb_dev_handle *usbhandle;
#endif

#if 0
    libusb_device **devs;
	int r;
	ssize_t cnt;

	r = libusb_init(NULL);
	if (r < 0)
		return r;

	cnt = libusb_get_device_list(NULL, &devs);
	if (cnt < 0)
		return (int) cnt;

	print_devs(devs);

	libusb_free_device_list(devs, 1);

    libusb_exit(NULL);
#endif

	if(!usbdevice_open(&usbhandle))
	{
	    testWrite(usbhandle);
        usbasp_initialize(usbhandle);
	}


	return 0;
}
/** Tests that 100 concurrent device lists can be open at a time. */
static libusb_testlib_result test_many_device_lists(libusb_testlib_ctx * tctx)
{
#define LIST_COUNT 100
	libusb_context * ctx = NULL;
	libusb_device ** device_lists[LIST_COUNT];
	int r, i;
	memset(device_lists, 0, sizeof(device_lists));

	r = libusb_init(&ctx);
	if (r != LIBUSB_SUCCESS) {
		libusb_testlib_logf(tctx, "Failed to init libusb: %d", r);
		return TEST_STATUS_FAILURE;
	}

	/* Create the 100 device lists. */
	for (i = 0; i < LIST_COUNT; ++i) {
		ssize_t list_size = libusb_get_device_list(ctx, &(device_lists[i]));
		if (list_size < 0 || device_lists[i] == NULL) {
			libusb_testlib_logf(tctx,
				"Failed to get device list on iteration %d: %d (%p)",
				i, -list_size, device_lists[i]);
			return TEST_STATUS_FAILURE;
		}
	}

	/* Destroy the 100 device lists. */
	for (i = 0; i < LIST_COUNT; ++i) {
		if (device_lists[i]) {
			libusb_free_device_list(device_lists[i], 1);
			device_lists[i] = NULL;
		}
	}

	libusb_exit(ctx);
	return TEST_STATUS_SUCCESS;
#undef LIST_COUNT
}
Example #20
0
std::string getFullPortUSB(std::string id) {

	std::string port;

	libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
	libusb_context *ctx = NULL; //a libusb session
	int r; //for return values
	ssize_t cnt; //holding number of devices in list
	r = libusb_init(&ctx); //initialize a library session
	if(r < 0) {
		std::cout<<"Init Error "<<r<<std::endl; //there was an error
		return NULL;
	}

	libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
	cnt = libusb_get_device_list(ctx, &devs); //get the list of devices

	if(cnt < 0)
		std::cout<<"Get Device Error"<<std::endl; //there was an error

	std::cout<<cnt<<" Devices in list."<<std::endl; //print total number of usb devices

	for(ssize_t i = 0; i < cnt; i++)
	{
		std::string tmpid = get_device_id(devs[i]);
		if (tmpid.compare(id)==0)
		{
			port = get_device_port(devs[i]);
			std::cout << " usb device " << tmpid << " found with port=" << port<< std::endl;
		}
	}

	libusb_free_device_list(devs, 1); //free the list, unref the devices in it
	libusb_exit(ctx); //close the session

	return port;
}
Example #21
0
static void usbasp_close(PROGRAMMER * pgm)
{
  if (PDATA(pgm)->usbhandle!=NULL) {
    unsigned char temp[4];
    memset(temp, 0, sizeof(temp));

    if (PDATA(pgm)->use_tpi) {
        usbasp_transmit(pgm, 1, USBASP_FUNC_TPI_DISCONNECT, temp, temp, sizeof(temp));
    } else {
        usbasp_transmit(pgm, 1, USBASP_FUNC_DISCONNECT, temp, temp, sizeof(temp));
    }

#ifdef USE_LIBUSB_1_0
    libusb_close(PDATA(pgm)->usbhandle);
#else
    usb_close(PDATA(pgm)->usbhandle);
#endif
  }
#ifdef USE_LIBUSB_1_0
  libusb_exit(ctx);
#else
  /* nothing for usb 0.1 ? */
#endif
}
Example #22
0
XnStatus xnUSBPlatformSpecificShutdown()
{
	xnUSBAsynchThreadStop();
#ifdef XN_USE_UDEV
	g_bShouldRunUDEVThread = false;
	xnOSWaitAndTerminateThread(&g_hUDEVThread, 2 * 1000);
	g_hUDEVThread = NULL;
#endif

	if (g_InitData.hLock != NULL)
	{
		xnOSCloseCriticalSection(&g_InitData.hLock);
		g_InitData.hLock = NULL;
	}
	
	if (g_InitData.pContext != NULL)
	{
		// close the library
		libusb_exit(g_InitData.pContext);
		g_InitData.pContext = NULL;
	}
	
	return (XN_STATUS_OK);
}
Example #23
0
/** \ingroup core
 * Deinitialise libfprint. This function should be called during your program
 * exit sequence. You must not use any libfprint functions after calling this
 * function, unless you call fp_init() again.
 */
API_EXPORTED void fp_exit(void)
{
	fp_dbg("");

	if (opened_devices) {
		GSList *copy = g_slist_copy(opened_devices);
		GSList *elem = copy;
		fp_dbg("naughty app left devices open on exit!");

		do
			fp_dev_close((struct fp_dev *) elem->data);
		while ((elem = g_slist_next(elem)));

		g_slist_free(copy);
		g_slist_free(opened_devices);
		opened_devices = NULL;
	}

	fpi_data_exit();
	fpi_poll_exit();
	g_slist_free(registered_drivers);
	registered_drivers = NULL;
	libusb_exit(fpi_usb_ctx);
}
Example #24
0
static int swizzle_usb (short vendor, short product)
{
	libusb_context *ctx;
	libusb_device_handle *dev;
	int rc;

	printf("swizzle\n");
	
	rc = libusb_init(&ctx);
	assert(rc == 0);
	dev = libusb_open_device_with_vid_pid(ctx, vendor, product);
	if (!dev)
		goto out;

	libusb_set_configuration(dev, 1);
	libusb_set_interface_alt_setting(dev, 1, 0);
	libusb_set_interface_alt_setting(dev, 1, 1);
	rc = 1;

out:
	libusb_close(dev);
	libusb_exit(ctx);
	return rc;
}
Example #25
0
QVector<CameraInfo> CameraUSB::avaibleCams(const CameraInfo &info)
{
    QVector<CameraInfo> avaibleCams;

    libusb_context *ctx = NULL;
    libusb_device **devs;
    ssize_t cnt;

    int vendorId = info.getParam("vendorId").toString().toInt(0, 16);
    int productId = info.getParam("productId").toString().toInt(0, 16);

    // create context
    if(libusb_init(&ctx) != LIBUSB_SUCCESS)
        return avaibleCams;

    // get list usb device
    cnt = libusb_get_device_list(ctx, &devs);
    for(int i = 0; i < cnt; i++)
    {
        libusb_device_descriptor desc;
        if(libusb_get_device_descriptor(devs[i], &desc)==LIBUSB_SUCCESS)
        {
            if(desc.idVendor==vendorId && desc.idProduct==productId)
            {
                QString addr = QString("%1.%2").arg((int)libusb_get_bus_number(devs[i]))
                                               .arg((int)libusb_get_device_address(devs[i]));
                avaibleCams.append(CameraInfo("DreamCam USB", "USB", addr));
            }
        }
    }
    libusb_free_device_list(devs, 1);

    // destroy usb context
    libusb_exit(ctx);
    return avaibleCams;
}
Example #26
0
int
main ()
{
  libusb_context *context;
  libusb_device **devs;
  int i, count;

  if (libusb_init (&context))
    {
      fprintf (stderr, "libusb init failure\n");
      exit (1);
    }
  libusb_set_debug (context, 0);
  printf ("Possible addresses for KNX USB devices:\n");
  count = libusb_get_device_list (context, &devs);

  for (i = 0; i < count; i++)
    {
      check_device (devs[i]);
    }

  libusb_free_device_list (devs, 1);
  libusb_exit (context);
}
Example #27
0
	bool FalconCommLibUSB::close()
	{
		if(!m_isCommOpen)
		{
			m_errorCode = FALCON_COMM_DEVICE_NOT_VALID_ERROR;
			return false;
		}
		reset();
		libusb_free_transfer(in_transfer);
		libusb_free_transfer(out_transfer);

		if ((m_deviceErrorCode = libusb_release_interface(m_falconDevice, 0)) < 0)
		{
			m_errorCode = FALCON_COMM_DEVICE_ERROR;
			std::cout << "usb_release_interface error " << m_deviceErrorCode << std::endl;
			return false;
		}

		libusb_close(m_falconDevice);
		libusb_exit(m_usbContext);
		m_falconDevice = NULL;
		m_isCommOpen = false;
		return true;
	}
Example #28
0
void minipro_hard_reset() {
	int ret;
	libusb_context *ctx;
	libusb_device_handle *usb_handle;

	ret = libusb_init(&ctx);
	if(ret < 0) {
		printf("minipro_hard_reset: libusb_init failed (%s)\n", libusb_error_name(ret));
		return;
	}

	usb_handle = libusb_open_device_with_vid_pid(ctx, 0x04d8, 0xe11c);
	if(usb_handle == NULL) {
		printf("minipro_hard_reset: libusb_open_device_with_vid_pid failed\n");
		return;
	}

	// two resets are required to recover from all cases
	libusb_reset_device(usb_handle);
	libusb_reset_device(usb_handle);

	libusb_close(usb_handle);
	libusb_exit(ctx);
}
Example #29
0
File: main.cpp Project: osrf/wandrr
int main(int argc, char **argv)
{
  if (argc < 2)
    return usage();
  const char *cmd = argv[1];
  perish_if(libusb_init(NULL) < 0, "Couldn't init libusb");
  libusb_device_handle *h = libusb_open_device_with_vid_pid(NULL, 
                                                            VENDOR_ID, 
                                                            PRODUCT_ID);
  perish_if(h == NULL, "couldn't find device");
  perish_if(0 != libusb_claim_interface(h, 0), "couldn't claim interface");
  printf("device opened successfully.\n");
  signal(SIGINT, signal_handler);
  if (!strcmp(cmd, "dump"))
    dump(h);
  else if (!strcmp(cmd, "burn") && argc >= 3)
    burn(h, argv[2]);
  else if (!strcmp(cmd, "configure"))
    configure(h);
  else if (!strcmp(cmd, "poe") && argc >= 3)
    poe_power(h, atoi(argv[2]));
  libusb_exit(NULL);
  return 0;
}
Example #30
0
void usb_destroy_context(libusb_context *context) {
	const struct libusb_pollfd **pollfds = NULL;
	const struct libusb_pollfd **pollfd;

	libusb_set_pollfd_notifiers(context, NULL, NULL, NULL);

	pollfds = libusb_get_pollfds(context);

	if (pollfds == NULL) {
		log_error("Could not get pollfds from main libusb context");
	} else {
		for (pollfd = pollfds; *pollfd != NULL; ++pollfd) {
			event_remove_source((*pollfd)->fd, EVENT_SOURCE_TYPE_USB);
		}

#if defined(_WIN32) || (defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000104) // libusb 1.0.20
		libusb_free_pollfds(pollfds); // avoids possible heap-mismatch on Windows
#else
		free(pollfds);
#endif
	}

	libusb_exit(context);
}