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

📄 core.c

📁 linux 下的libusb 1.0.0版本
💻 C
📖 第 1 页 / 共 4 页
字号:
 * its reference count reaches 0. * * With the above information in mind, the process of opening a device can * be viewed as follows: * -# Discover devices using libusb_get_device_list(). * -# Choose the device that you want to operate, and call libusb_open(). * -# Unref all devices in the discovered device list. * -# Free the discovered device list. * * The order is important - you must not unreference the device before * attempting to open it, because unreferencing it may destroy the device. * * For convenience, the libusb_free_device_list() function includes a * parameter to optionally unreference all the devices in the list before * freeing the list itself. This combines steps 3 and 4 above. * * As an implementation detail, libusb_open() actually adds a reference to * the device in question. This is because the device remains available * through the handle via libusb_get_device(). The reference is deleted during * libusb_close(). *//** @defgroup misc Miscellaneous *//* we traverse usbfs without knowing how many devices we are going to find. * so we create this discovered_devs model which is similar to a linked-list * which grows when required. it can be freed once discovery has completed, * eliminating the need for a list node in the libusb_device structure * itself. */#define DISCOVERED_DEVICES_SIZE_STEP 8static struct discovered_devs *discovered_devs_alloc(void){	struct discovered_devs *ret =		malloc(sizeof(*ret) + (sizeof(void *) * DISCOVERED_DEVICES_SIZE_STEP));	if (ret) {		ret->len = 0;		ret->capacity = DISCOVERED_DEVICES_SIZE_STEP;	}	return ret;}/* append a device to the discovered devices collection. may realloc itself, * returning new discdevs. returns NULL on realloc failure. */struct discovered_devs *discovered_devs_append(	struct discovered_devs *discdevs, struct libusb_device *dev){	size_t len = discdevs->len;	size_t capacity;	/* if there is space, just append the device */	if (len < discdevs->capacity) {		discdevs->devices[len] = libusb_ref_device(dev);		discdevs->len++;		return discdevs;	}	/* exceeded capacity, need to grow */	usbi_dbg("need to increase capacity");	capacity = discdevs->capacity + DISCOVERED_DEVICES_SIZE_STEP;	discdevs = realloc(discdevs,		sizeof(*discdevs) + (sizeof(void *) * capacity));	if (discdevs) {		discdevs->capacity = capacity;		discdevs->devices[len] = libusb_ref_device(dev);		discdevs->len++;	}	return discdevs;}static void discovered_devs_free(struct discovered_devs *discdevs){	size_t i;	for (i = 0; i < discdevs->len; i++)		libusb_unref_device(discdevs->devices[i]);	free(discdevs);}/* Allocate a new device with a specific session ID. The returned device has * a reference count of 1. */struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,	unsigned long session_id){	size_t priv_size = usbi_backend->device_priv_size;	struct libusb_device *dev = malloc(sizeof(*dev) + priv_size);	int r;	if (!dev)		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.

⌨️ 快捷键说明

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