📄 core.c
字号:
* \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_context *ctx = DEVICE_CTX(dev); struct libusb_device_handle *_handle; size_t priv_size = usbi_backend->device_handle_priv_size; unsigned char dummy = 1; 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(&ctx->open_devs_lock); list_add(&_handle->list, &ctx->open_devs); pthread_mutex_unlock(&ctx->open_devs_lock); *handle = _handle; /* At this point, we want to interrupt any existing event handlers so * that they realise the addition of the new device's poll fd. One * example when this is desirable is if the user is running a separate * dedicated libusb events handling thread, which is running with a long * or infinite timeout. We want to interrupt that iteration of the loop, * so that it picks up the new fd, and then continues. */ /* record that we are messing with poll fds */ pthread_mutex_lock(&ctx->pollfd_modify_lock); ctx->pollfd_modify++; pthread_mutex_unlock(&ctx->pollfd_modify_lock); /* write some data on control pipe to interrupt event handlers */ r = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); if (r <= 0) { usbi_warn(ctx, "internal signalling write failed"); pthread_mutex_lock(&ctx->pollfd_modify_lock); ctx->pollfd_modify--; pthread_mutex_unlock(&ctx->pollfd_modify_lock); return 0; } /* take event handling lock */ libusb_lock_events(ctx); /* read the dummy data */ r = read(ctx->ctrl_pipe[0], &dummy, sizeof(dummy)); if (r <= 0) usbi_warn(ctx, "internal signalling read failed"); /* we're done with modifying poll fds */ pthread_mutex_lock(&ctx->pollfd_modify_lock); ctx->pollfd_modify--; pthread_mutex_unlock(&ctx->pollfd_modify_lock); /* Release event handling lock and wake up event waiters */ libusb_unlock_events(ctx); 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_context *ctx, struct libusb_device_handle *dev_handle){ pthread_mutex_lock(&ctx->open_devs_lock); list_del(&dev_handle->list); pthread_mutex_unlock(&ctx->open_devs_lock); usbi_backend->close(dev_handle); libusb_unref_device(dev_handle->dev); free(dev_handle);}/** \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){ struct libusb_context *ctx; unsigned char dummy = 1; ssize_t r; if (!dev_handle) return; usbi_dbg(""); ctx = HANDLE_CTX(dev_handle); /* Similarly to libusb_open(), we want to interrupt all event handlers * at this point. More importantly, we want to perform the actual close of * the device while holding the event handling lock (preventing any other * thread from doing event handling) because we will be removing a file * descriptor from the polling loop. */ /* record that we are messing with poll fds */ pthread_mutex_lock(&ctx->pollfd_modify_lock); ctx->pollfd_modify++; pthread_mutex_unlock(&ctx->pollfd_modify_lock); /* write some data on control pipe to interrupt event handlers */ r = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); if (r <= 0) { usbi_warn(ctx, "internal signalling write failed, closing anyway"); do_close(ctx, dev_handle); pthread_mutex_lock(&ctx->pollfd_modify_lock); ctx->pollfd_modify--; pthread_mutex_unlock(&ctx->pollfd_modify_lock); return; } /* take event handling lock */ libusb_lock_events(ctx); /* read the dummy data */ r = read(ctx->ctrl_pipe[0], &dummy, sizeof(dummy)); if (r <= 0) usbi_warn(ctx, "internal signalling read failed, closing anyway"); /* Close the device */ do_close(ctx, dev_handle); /* we're done with modifying poll fds */ pthread_mutex_lock(&ctx->pollfd_modify_lock); ctx->pollfd_modify--; pthread_mutex_unlock(&ctx->pollfd_modify_lock); /* Release event handling lock and wake up event waiters */ libusb_unlock_events(ctx);}/** \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; *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. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -