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

📄 core.c

📁 最新的libusb库
💻 C
📖 第 1 页 / 共 3 页
字号:
		return NULL;	r = pthread_mutex_init(&dev->lock, NULL);	if (r)		return NULL;	dev->ctx = ctx;	dev->refcnt = 1;	dev->session_data = session_id;	memset(&dev->os_priv, 0, priv_size);	pthread_mutex_lock(&ctx->usb_devs_lock);	list_add(&dev->list, &ctx->usb_devs);	pthread_mutex_unlock(&ctx->usb_devs_lock);	return dev;}/* Perform some final sanity checks on a newly discovered device. If this * function fails (negative return code), the device should not be added * to the discovered device list. */int usbi_sanitize_device(struct libusb_device *dev){	int r;	unsigned char raw_desc[DEVICE_DESC_LENGTH];	uint8_t num_configurations;	int host_endian;	r = usbi_backend->get_device_descriptor(dev, raw_desc, &host_endian);	if (r < 0)		return r;	num_configurations = raw_desc[DEVICE_DESC_LENGTH - 1];	if (num_configurations > USB_MAXCONFIG) {		usbi_err(DEVICE_CTX(dev), "too many configurations");		return LIBUSB_ERROR_IO;	} else if (num_configurations < 1) {		usbi_dbg("no configurations?");		return LIBUSB_ERROR_IO;	}	dev->num_configurations = num_configurations;	return 0;}/* Examine libusb's internal list of known devices, looking for one with * a specific session ID. Returns the matching device if it was found, and * NULL otherwise. */struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,	unsigned long session_id){	struct libusb_device *dev;	struct libusb_device *ret = NULL;	pthread_mutex_lock(&ctx->usb_devs_lock);	list_for_each_entry(dev, &ctx->usb_devs, list)		if (dev->session_data == session_id) {			ret = dev;			break;		}	pthread_mutex_unlock(&ctx->usb_devs_lock);	return ret;}/** @ingroup dev * Returns a list of USB devices currently attached to the system. This is * your entry point into finding a USB device to operate. * * You are expected to unreference all the devices when you are done with * them, and then free the list with libusb_free_device_list(). Note that * libusb_free_device_list() can unref all the devices for you. Be careful * not to unreference a device you are about to open until after you have * opened it. * * This return value of this function indicates the number of devices in * the resultant list. The list is actually one element larger, as it is * NULL-terminated. * * \param ctx the context to operate on, or NULL for the default context * \param list output location for a list of devices. Must be later freed with * libusb_free_device_list(). * \returns the number of devices in the outputted list, or LIBUSB_ERROR_NO_MEM * on memory allocation failure. */API_EXPORTED ssize_t libusb_get_device_list(libusb_context *ctx,	libusb_device ***list){	struct discovered_devs *discdevs = discovered_devs_alloc();	struct libusb_device **ret;	int r = 0;	size_t i;	ssize_t len;	USBI_GET_CONTEXT(ctx);	usbi_dbg("");	if (!discdevs)		return LIBUSB_ERROR_NO_MEM;	r = usbi_backend->get_device_list(ctx, &discdevs);	if (r < 0) {		len = r;		goto out;	}	/* convert discovered_devs into a list */	len = discdevs->len;	ret = malloc(sizeof(void *) * (len + 1));	if (!ret) {		len = LIBUSB_ERROR_NO_MEM;		goto out;	}	ret[len] = NULL;	for (i = 0; i < len; i++) {		struct libusb_device *dev = discdevs->devices[i];		ret[i] = libusb_ref_device(dev);	}	*list = ret;out:	discovered_devs_free(discdevs);	return len;}/** \ingroup dev * Frees a list of devices previously discovered using * libusb_get_device_list(). If the unref_devices parameter is set, the * reference count of each device in the list is decremented by 1. * \param list the list to free * \param unref_devices whether to unref the devices in the list */API_EXPORTED void libusb_free_device_list(libusb_device **list,	int unref_devices){	if (!list)		return;	if (unref_devices) {		int i = 0;		struct libusb_device *dev;		while ((dev = list[i++]) != NULL)			libusb_unref_device(dev);	}	free(list);}/** \ingroup dev * Get the number of the bus that a device is connected to. * \param dev a device * \returns the bus number */API_EXPORTED uint8_t libusb_get_bus_number(libusb_device *dev){	return dev->bus_number;}/** \ingroup dev * Get the address of the device on the bus it is connected to. * \param dev a device * \returns the device address */API_EXPORTED uint8_t libusb_get_device_address(libusb_device *dev){	return dev->device_address;}/** \ingroup dev * Convenience function to retrieve the wMaxPacketSize value for a particular * endpoint in the active device configuration. This is useful for setting up * isochronous transfers. * * \param dev a device * \param endpoint address of the endpoint in question * \returns the wMaxPacketSize value * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist * \returns LIBUSB_ERROR_OTHER on other failure */API_EXPORTED int libusb_get_max_packet_size(libusb_device *dev,	unsigned char endpoint){	int iface_idx;	struct libusb_config_descriptor *config;	int r;		r = libusb_get_active_config_descriptor(dev, &config);	if (r < 0) {		usbi_err(DEVICE_CTX(dev),			"could not retrieve active config descriptor");		return LIBUSB_ERROR_OTHER;	}	r = LIBUSB_ERROR_NOT_FOUND;	for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) {		const struct libusb_interface *iface = &config->interface[iface_idx];		int altsetting_idx;		for (altsetting_idx = 0; altsetting_idx < iface->num_altsetting;				altsetting_idx++) {			const struct libusb_interface_descriptor *altsetting				= &iface->altsetting[altsetting_idx];			int ep_idx;			for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints; ep_idx++) {				const struct libusb_endpoint_descriptor *ep =					&altsetting->endpoint[ep_idx];				if (ep->bEndpointAddress == endpoint) {					r = ep->wMaxPacketSize;					goto out;				}			}		}	}out:	libusb_free_config_descriptor(config);	return r;}/** \ingroup dev * Increment the reference count of a device. * \param dev the device to reference * \returns the same device */API_EXPORTED libusb_device *libusb_ref_device(libusb_device *dev){	pthread_mutex_lock(&dev->lock);	dev->refcnt++;	pthread_mutex_unlock(&dev->lock);	return dev;}/** \ingroup dev * Decrement the reference count of a device. If the decrement operation * causes the reference count to reach zero, the device shall be destroyed. * \param dev the device to unreference */API_EXPORTED void libusb_unref_device(libusb_device *dev){	int refcnt;	if (!dev)		return;	pthread_mutex_lock(&dev->lock);	refcnt = --dev->refcnt;	pthread_mutex_unlock(&dev->lock);	if (refcnt == 0) {		usbi_dbg("destroy device %d.%d", dev->bus_number, dev->device_address);		if (usbi_backend->destroy_device)			usbi_backend->destroy_device(dev);		pthread_mutex_lock(&dev->ctx->usb_devs_lock);		list_del(&dev->list);		pthread_mutex_unlock(&dev->ctx->usb_devs_lock);		free(dev);	}}/** \ingroup dev * Open a device and obtain a device handle. A handle allows you to perform * I/O on the device in question. * * Internally, this function adds a reference to the device and makes it * available to you through libusb_get_device(). This reference is removed * during libusb_close(). * * This is a non-blocking function; no requests are sent over the bus. * * \param dev the device to open * \param handle output location for the returned device handle pointer. Only * populated when the return code is 0. * \returns 0 on success * \returns LIBUSB_ERROR_NO_MEM on memory allocation failure * \returns LIBUSB_ERROR_ACCESS if the user has insufficient permissions * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected * \returns another LIBUSB_ERROR code on other failure */API_EXPORTED int libusb_open(libusb_device *dev, libusb_device_handle **handle){	struct libusb_device_handle *_handle;	size_t priv_size = usbi_backend->device_handle_priv_size;	int r;	usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);	_handle = malloc(sizeof(*_handle) + priv_size);	if (!_handle)		return LIBUSB_ERROR_NO_MEM;	r = pthread_mutex_init(&_handle->lock, NULL);	if (r)		return LIBUSB_ERROR_OTHER;	_handle->dev = libusb_ref_device(dev);	_handle->claimed_interfaces = 0;	memset(&_handle->os_priv, 0, priv_size);	r = usbi_backend->open(_handle);	if (r < 0) {		libusb_unref_device(dev);		free(_handle);		return r;	}	pthread_mutex_lock(&dev->ctx->open_devs_lock);	list_add(&_handle->list, &dev->ctx->open_devs);	pthread_mutex_unlock(&dev->ctx->open_devs_lock);	*handle = _handle;	return 0;}/** \ingroup dev * Convenience function for finding a device with a particular * <tt>idVendor</tt>/<tt>idProduct</tt> combination. This function is intended * for those scenarios where you are using libusb to knock up a quick test * application - it allows you to avoid calling libusb_get_device_list() and * worrying about traversing/freeing the list. * * This function has limitations and is hence not intended for use in real * applications: if multiple devices have the same IDs it will only * give you the first one, etc. * * \param ctx the context to operate on, or NULL for the default context * \param vendor_id the idVendor value to search for * \param product_id the idProduct value to search for * \returns a handle for the first found device, or NULL on error or if the * device could not be found. */API_EXPORTED libusb_device_handle *libusb_open_device_with_vid_pid(	libusb_context *ctx, uint16_t vendor_id, uint16_t product_id){	struct libusb_device **devs;	struct libusb_device *found = NULL;	struct libusb_device *dev;	struct libusb_device_handle *handle = NULL;	size_t i = 0;	int r;	if (libusb_get_device_list(ctx, &devs) < 0)		return NULL;	while ((dev = devs[i++]) != NULL) {		struct libusb_device_descriptor desc;		r = libusb_get_device_descriptor(dev, &desc);		if (r < 0)			goto out;		if (desc.idVendor == vendor_id && desc.idProduct == product_id) {			found = dev;			break;		}	}	if (found) {		r = libusb_open(found, &handle);		if (r < 0)			handle = NULL;	}out:	libusb_free_device_list(devs, 1);	return handle;}static void do_close(struct libusb_device_handle *dev_handle){	usbi_backend->close(dev_handle);	libusb_unref_device(dev_handle->dev);}/** \ingroup dev * Close a device handle. Should be called on all open handles before your * application exits. * * Internally, this function destroys the reference that was added by * libusb_open() on the given device. * * This is a non-blocking function; no requests are sent over the bus. * * \param dev_handle the handle to close */API_EXPORTED void libusb_close(libusb_device_handle *dev_handle){	if (!dev_handle)		return;	usbi_dbg("");	pthread_mutex_lock(&HANDLE_CTX(dev_handle)->open_devs_lock);	list_del(&dev_handle->list);	pthread_mutex_unlock(&HANDLE_CTX(dev_handle)->open_devs_lock);	do_close(dev_handle);	free(dev_handle);}/** \ingroup dev * Get the underlying device for a handle. This function does not modify * the reference count of the returned device, so do not feel compelled to * unreference it when you are done. * \param dev_handle a device handle * \returns the underlying device */API_EXPORTED libusb_device *libusb_get_device(libusb_device_handle *dev_handle){	return dev_handle->dev;}/** \ingroup dev * Determine the bConfigurationValue of the currently active configuration. * * You could formulate your own control request to obtain this information, * but this function has the advantage that it may be able to retrieve the * information from operating system caches (no I/O involved). * * If the OS does not cache this information, then this function will block * while a control transfer is submitted to retrieve the information. * * This function will return a value of 0 in the <tt>config</tt> output * parameter if the device is in unconfigured state. * * \param dev a device handle * \param config output location for the bConfigurationValue of the active * configuration (only valid for return code 0) * \returns 0 on success * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected * \returns another LIBUSB_ERROR code on other failure */API_EXPORTED int libusb_get_configuration(libusb_device_handle *dev,	int *config){	int r = LIBUSB_ERROR_NOT_SUPPORTED;	usbi_dbg("");	if (usbi_backend->get_configuration)		r = usbi_backend->get_configuration(dev, config);	if (r == LIBUSB_ERROR_NOT_SUPPORTED) {		uint8_t tmp = 0;		usbi_dbg("falling back to control message");		r = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,			LIBUSB_REQUEST_GET_CONFIGURATION, 0, 0, &tmp, 1, 1000);		if (r == 0) {			usbi_err(HANDLE_CTX(dev), "zero bytes returned in ctrl transfer?");			r = LIBUSB_ERROR_IO;		} else if (r == 1) {			r = 0;

⌨️ 快捷键说明

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