⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 core.c

📁 最新的libusb库
💻 C
📖 第 1 页 / 共 3 页
字号:
			*config = tmp;		} else {			usbi_dbg("control failed, error %d", r);		}	}	if (r == 0)		usbi_dbg("active config %d", *config);	return r;}/** \ingroup dev * Set the active configuration for a device. * * The operating system may or may not have already set an active * configuration on the device. It is up to your application to ensure the * correct configuration is selected before you attempt to claim interfaces * and perform other operations. *  * If you call this function on a device already configured with the selected * configuration, then this function will act as a lightweight device reset: * it will issue a SET_CONFIGURATION request using the current configuration, * causing most USB-related device state to be reset (altsetting reset to zero, * endpoint halts cleared, toggles reset). * * You cannot change/reset configuration if your application has claimed * interfaces - you should free them with libusb_release_interface() first. * You cannot change/reset configuration if other applications or drivers have * claimed interfaces. * * A configuration value of -1 will put the device in unconfigured state. * The USB specifications state that a configuration value of 0 does this, * however buggy devices exist which actually have a configuration 0. * * You should always use this function rather than formulating your own * SET_CONFIGURATION control request. This is because the underlying operating * system needs to know when such changes happen. * * This is a blocking function. * * \param dev a device handle * \param configuration the bConfigurationValue of the configuration you * wish to activate, or -1 if you wish to put the device in unconfigured state * \returns 0 on success * \returns LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist * \returns LIBUSB_ERROR_BUSY if interfaces are currently claimed * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected * \returns another LIBUSB_ERROR code on other failure */API_EXPORTED int libusb_set_configuration(libusb_device_handle *dev,	int configuration){	usbi_dbg("configuration %d", configuration);	return usbi_backend->set_configuration(dev, configuration);}/** \ingroup dev * Claim an interface on a given device handle. You must claim the interface * you wish to use before you can perform I/O on any of its endpoints. * * It is legal to attempt to claim an already-claimed interface, in which * case libusb just returns 0 without doing anything. * * Claiming of interfaces is a purely logical operation; it does not cause * any requests to be sent over the bus. Interface claiming is used to * instruct the underlying operating system that your application wishes * to take ownership of the interface. * * This is a non-blocking function. * * \param dev a device handle * \param interface_number the <tt>bInterfaceNumber</tt> of the interface you * wish to claim * \returns 0 on success * \returns LIBUSB_ERROR_NOT_FOUND if the requested interface does not exist * \returns LIBUSB_ERROR_BUSY if another program or driver has claimed the * interface * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected * \returns a LIBUSB_ERROR code on other failure */API_EXPORTED int libusb_claim_interface(libusb_device_handle *dev,	int interface_number){	int r = 0;	usbi_dbg("interface %d", interface_number);	if (interface_number >= sizeof(dev->claimed_interfaces) * 8)		return LIBUSB_ERROR_INVALID_PARAM;	pthread_mutex_lock(&dev->lock);	if (dev->claimed_interfaces & (1 << interface_number))		goto out;	r = usbi_backend->claim_interface(dev, interface_number);	if (r == 0)		dev->claimed_interfaces |= 1 << interface_number;out:	pthread_mutex_unlock(&dev->lock);	return r;}/** \ingroup dev * Release an interface previously claimed with libusb_claim_interface(). You * should release all claimed interfaces before closing a device handle. * * This is a blocking function. A SET_INTERFACE control request will be sent * to the device, resetting interface state to the first alternate setting. * * \param dev a device handle * \param interface_number the <tt>bInterfaceNumber</tt> of the * previously-claimed interface * \returns 0 on success * \returns LIBUSB_ERROR_NOT_FOUND if the interface was not claimed * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected * \returns another LIBUSB_ERROR code on other failure */API_EXPORTED int libusb_release_interface(libusb_device_handle *dev,	int interface_number){	int r;	usbi_dbg("interface %d", interface_number);	if (interface_number >= sizeof(dev->claimed_interfaces) * 8)		return LIBUSB_ERROR_INVALID_PARAM;	pthread_mutex_lock(&dev->lock);	if (!(dev->claimed_interfaces & (1 << interface_number))) {		r = LIBUSB_ERROR_NOT_FOUND;		goto out;	}	r = usbi_backend->release_interface(dev, interface_number);	if (r == 0)		dev->claimed_interfaces &= ~(1 << interface_number);out:	pthread_mutex_unlock(&dev->lock);	return r;}/** \ingroup dev * Activate an alternate setting for an interface. The interface must have * been previously claimed with libusb_claim_interface(). * * You should always use this function rather than formulating your own * SET_INTERFACE control request. This is because the underlying operating * system needs to know when such changes happen. * * This is a blocking function. * * \param dev a device handle * \param interface_number the <tt>bInterfaceNumber</tt> of the * previously-claimed interface * \param alternate_setting the <tt>bAlternateSetting</tt> of the alternate * setting to activate * \returns 0 on success * \returns LIBUSB_ERROR_NOT_FOUND if the interface was not claimed, or the * requested alternate setting does not exist * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected * \returns another LIBUSB_ERROR code on other failure */API_EXPORTED int libusb_set_interface_alt_setting(libusb_device_handle *dev,	int interface_number, int alternate_setting){	usbi_dbg("interface %d altsetting %d",		interface_number, alternate_setting);	if (interface_number >= sizeof(dev->claimed_interfaces) * 8)		return LIBUSB_ERROR_INVALID_PARAM;	pthread_mutex_lock(&dev->lock);	if (!(dev->claimed_interfaces & (1 << interface_number))) {		pthread_mutex_unlock(&dev->lock);			return LIBUSB_ERROR_NOT_FOUND;	}	pthread_mutex_unlock(&dev->lock);	return usbi_backend->set_interface_altsetting(dev, interface_number,		alternate_setting);}/** \ingroup dev * Clear the halt/stall condition for an endpoint. Endpoints with halt status * are unable to receive or transmit data until the halt condition is stalled. * * You should cancel all pending transfers before attempting to clear the halt * condition. * * This is a blocking function. * * \param dev a device handle * \param endpoint the endpoint to clear halt status * \returns 0 on success * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected * \returns another LIBUSB_ERROR code on other failure */API_EXPORTED int libusb_clear_halt(libusb_device_handle *dev,	unsigned char endpoint){	usbi_dbg("endpoint %x", endpoint);	return usbi_backend->clear_halt(dev, endpoint);}/** \ingroup dev * Perform a USB port reset to reinitialize a device. The system will attempt * to restore the previous configuration and alternate settings after the * reset has completed. * * If the reset fails, the descriptors change, or the previous state cannot be * restored, the device will appear to be disconnected and reconnected. This * means that the device handle is no longer valid (you should close it) and * rediscover the device. A return code of LIBUSB_ERROR_NOT_FOUND indicates * when this is the case. * * This is a blocking function which usually incurs a noticeable delay. * * \param dev a handle of the device to reset * \returns 0 on success * \returns LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the * device has been disconnected * \returns another LIBUSB_ERROR code on other failure */API_EXPORTED int libusb_reset_device(libusb_device_handle *dev){	usbi_dbg("");	return usbi_backend->reset_device(dev);}/** \ingroup dev * Determine if a kernel driver is active on an interface. If a kernel driver * is active, you cannot claim the interface, and libusb will be unable to * perform I/O. * * \param dev a device handle * \param interface the interface to check * \returns 0 if no kernel driver is active * \returns 1 if a kernel driver is active * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected * \returns another LIBUSB_ERROR code on other failure * \see libusb_detach_kernel_driver() */API_EXPORTED int libusb_kernel_driver_active(libusb_device_handle *dev,	int interface){	usbi_dbg("interface %d", interface);	if (usbi_backend->kernel_driver_active)		return usbi_backend->kernel_driver_active(dev, interface);	else		return LIBUSB_ERROR_NOT_SUPPORTED;}/** \ingroup dev * Detach a kernel driver from an interface. If successful, you will then be * able to claim the interface and perform I/O. * * \param dev a device handle * \param interface the interface to detach the driver from * \returns 0 on success * \returns LIBUSB_ERROR_NOT_FOUND if no kernel driver was active * \returns LIBUSB_ERROR_INVALID_PARAM if the interface does not exist * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected * \returns another LIBUSB_ERROR code on other failure * \see libusb_kernel_driver_active() */API_EXPORTED int libusb_detach_kernel_driver(libusb_device_handle *dev,	int interface){	usbi_dbg("interface %d", interface);	if (usbi_backend->detach_kernel_driver)		return usbi_backend->detach_kernel_driver(dev, interface);	else		return LIBUSB_ERROR_NOT_SUPPORTED;}/** \ingroup lib * Set message verbosity. *  - Level 0: no messages ever printed by the library (default) *  - Level 1: error messages are printed to stderr *  - Level 2: warning and error messages are printed to stderr *  - Level 3: informational messages are printed to stdout, warning and error *    messages are printed to stderr * * The default level is 0, which means no messages are ever printed. If you * choose to increase the message verbosity level, ensure that your * application does not close the stdout/stderr file descriptors. * * You are advised to set level 3. libusb is conservative with it's message * logging and most of the time, will only log messages that explain error * conditions and other oddities. This will help you debug your software. * * If the LIBUSB_DEBUG environment variable was set when libusb was * initialized, this function does nothing: the message verbosity is fixed * to the value in the environment variable. * * If libusb was compiled without any message logging, this function does * nothing: you'll never get any messages. * * If libusb was compiled with verbose debug message logging, this function * does nothing: you'll always get messages from all levels. * * \param ctx the context to operate on, or NULL for the default context * \param level debug level to set */API_EXPORTED void libusb_set_debug(libusb_context *ctx, int level){	USBI_GET_CONTEXT(ctx);	if (!ctx->debug_fixed)		ctx->debug = level;}/** \ingroup lib * Initialize libusb. This function must be called before calling any other * libusb function. * \param context Optional output location for context pointer. * Only valid on return code 0. * \returns 0 on success, or a LIBUSB_ERROR code on failure */API_EXPORTED int libusb_init(libusb_context **context){	char *dbg = getenv("LIBUSB_DEBUG");	struct libusb_context *ctx = malloc(sizeof(*ctx));	if (!ctx)		return LIBUSB_ERROR_NO_MEM;	memset(ctx, 0, sizeof(*ctx));	if (dbg) {		ctx->debug = atoi(dbg);		if (ctx->debug)			ctx->debug_fixed = 1;	}	usbi_dbg("");	if (usbi_backend->init) {		int r = usbi_backend->init(ctx);		if (r) {			free(ctx);			return r;		}	}	pthread_mutex_init(&ctx->usb_devs_lock, NULL);	pthread_mutex_init(&ctx->open_devs_lock, NULL);	list_init(&ctx->usb_devs);	list_init(&ctx->open_devs);	usbi_io_init(ctx);	pthread_mutex_lock(&default_context_lock);	if (!usbi_default_context) {		usbi_dbg("created default context");		usbi_default_context = ctx;	}	pthread_mutex_unlock(&default_context_lock);	if (context)		*context = ctx;	return 0;}/** \ingroup lib * Deinitialize libusb. Should be called after closing all open devices and * before your application terminates. * \param ctx the context to deinitialize, or NULL for the default context */API_EXPORTED void libusb_exit(struct libusb_context *ctx){	USBI_GET_CONTEXT(ctx);	usbi_dbg("");	pthread_mutex_lock(&ctx->open_devs_lock);	if (!list_empty(&ctx->open_devs)) {		struct libusb_device_handle *devh;		struct libusb_device_handle *tmp;		usbi_dbg("naughty app left some devices open!");		list_for_each_entry_safe(devh, tmp, &ctx->open_devs, list) {			list_del(&devh->list);			do_close(devh);			free(devh);		}	}	pthread_mutex_unlock(&ctx->open_devs_lock);	if (usbi_backend->exit)		usbi_backend->exit();		pthread_mutex_lock(&default_context_lock);	if (ctx == usbi_default_context) {		usbi_dbg("freeing default context");		usbi_default_context = NULL;	}	pthread_mutex_unlock(&default_context_lock);	free(ctx);}void usbi_log(struct libusb_context *ctx, enum usbi_log_level level,	const char *function, const char *format, ...){	va_list args;	FILE *stream = stdout;	const char *prefix;#ifndef ENABLE_DEBUG_LOGGING	USBI_GET_CONTEXT(ctx);	if (!ctx->debug)		return;	if (level == LOG_LEVEL_WARNING && ctx->debug < 2)		return;	if (level == LOG_LEVEL_INFO && ctx->debug < 3)		return;#endif	switch (level) {	case LOG_LEVEL_INFO:		prefix = "info";		break;	case LOG_LEVEL_WARNING:		stream = stderr;		prefix = "warning";		break;	case LOG_LEVEL_ERROR:		stream = stderr;		prefix = "error";		break;	case LOG_LEVEL_DEBUG:		stream = stderr;		prefix = "debug";		break;	default:		stream = stderr;		prefix = "unknown";		break;	}	fprintf(stream, "libusb:%s [%s] ", prefix, function);	va_start (args, format);	vfprintf(stream, format, args);	va_end (args);	fprintf(stream, "\n");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -