mirror of
https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
synced 2025-12-16 03:31:09 +08:00
tmp update
This commit is contained in:
@@ -539,7 +539,249 @@ int API_EXPORTED libusb_interrupt_transfer(libusb_device_handle *dev_handle,
|
|||||||
|
|
||||||
#### 2.9.1 使用步骤
|
#### 2.9.1 使用步骤
|
||||||
|
|
||||||
|
使用libusb的异步函数时,有如下步骤:
|
||||||
|
|
||||||
|
* 分配:分配一个`libus_transfer`结构体
|
||||||
|
* 填充:填充`libus_transfer`结构体,比如想访问哪个endpoint、数据buffer、长度等等
|
||||||
|
* 提交:提交`libus_transfer`结构体启动传输
|
||||||
|
* 处理事件:检查传输的结果,调用`libus_transfer`结构体的回调函数
|
||||||
|
* 释放:释放资源,比如释放`libus_transfer`结构体
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.9.2 分配transfer结构体
|
||||||
|
|
||||||
|
```c
|
||||||
|
/** \ingroup libusb_asyncio
|
||||||
|
* Allocate a libusb transfer with a specified number of isochronous packet
|
||||||
|
* descriptors. The returned transfer is pre-initialized for you. When the new
|
||||||
|
* transfer is no longer needed, it should be freed with
|
||||||
|
* libusb_free_transfer().
|
||||||
|
*
|
||||||
|
* Transfers intended for non-isochronous endpoints (e.g. control, bulk,
|
||||||
|
* interrupt) should specify an iso_packets count of zero.
|
||||||
|
*
|
||||||
|
* For transfers intended for isochronous endpoints, specify an appropriate
|
||||||
|
* number of packet descriptors to be allocated as part of the transfer.
|
||||||
|
* The returned transfer is not specially initialized for isochronous I/O;
|
||||||
|
* you are still required to set the
|
||||||
|
* \ref libusb_transfer::num_iso_packets "num_iso_packets" and
|
||||||
|
* \ref libusb_transfer::type "type" fields accordingly.
|
||||||
|
*
|
||||||
|
* It is safe to allocate a transfer with some isochronous packets and then
|
||||||
|
* use it on a non-isochronous endpoint. If you do this, ensure that at time
|
||||||
|
* of submission, num_iso_packets is 0 and that type is set appropriately.
|
||||||
|
*
|
||||||
|
* \param iso_packets number of isochronous packet descriptors to allocate. Must be non-negative.
|
||||||
|
* \returns a newly allocated transfer, or NULL on error
|
||||||
|
*/
|
||||||
|
DEFAULT_VISIBILITY
|
||||||
|
struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(
|
||||||
|
int iso_packets);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.9.3 填充控制传输
|
||||||
|
|
||||||
|
```c
|
||||||
|
* Helper function to populate the required \ref libusb_transfer fields
|
||||||
|
* for a control transfer.
|
||||||
|
*
|
||||||
|
* If you pass a transfer buffer to this function, the first 8 bytes will
|
||||||
|
* be interpreted as a control setup packet, and the wLength field will be
|
||||||
|
* used to automatically populate the \ref libusb_transfer::length "length"
|
||||||
|
* field of the transfer. Therefore the recommended approach is:
|
||||||
|
* -# Allocate a suitably sized data buffer (including space for control setup)
|
||||||
|
* -# Call libusb_fill_control_setup()
|
||||||
|
* -# If this is a host-to-device transfer with a data stage, put the data
|
||||||
|
* in place after the setup packet
|
||||||
|
* -# Call this function
|
||||||
|
* -# Call libusb_submit_transfer()
|
||||||
|
*
|
||||||
|
* It is also legal to pass a NULL buffer to this function, in which case this
|
||||||
|
* function will not attempt to populate the length field. Remember that you
|
||||||
|
* must then populate the buffer and length fields later.
|
||||||
|
*
|
||||||
|
* \param transfer the transfer to populate
|
||||||
|
* \param dev_handle handle of the device that will handle the transfer
|
||||||
|
* \param buffer data buffer. If provided, this function will interpret the
|
||||||
|
* first 8 bytes as a setup packet and infer the transfer length from that.
|
||||||
|
* This pointer must be aligned to at least 2 bytes boundary.
|
||||||
|
* \param callback callback function to be invoked on transfer completion
|
||||||
|
* \param user_data user data to pass to callback function
|
||||||
|
* \param timeout timeout for the transfer in milliseconds
|
||||||
|
*/
|
||||||
|
static inline void libusb_fill_control_transfer(
|
||||||
|
struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
|
||||||
|
unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
|
||||||
|
unsigned int timeout);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.9.4 填充批量传输
|
||||||
|
|
||||||
|
```c
|
||||||
|
/** \ingroup libusb_asyncio
|
||||||
|
* Helper function to populate the required \ref libusb_transfer fields
|
||||||
|
* for a bulk transfer.
|
||||||
|
*
|
||||||
|
* \param transfer the transfer to populate
|
||||||
|
* \param dev_handle handle of the device that will handle the transfer
|
||||||
|
* \param endpoint address of the endpoint where this transfer will be sent
|
||||||
|
* \param buffer data buffer
|
||||||
|
* \param length length of data buffer
|
||||||
|
* \param callback callback function to be invoked on transfer completion
|
||||||
|
* \param user_data user data to pass to callback function
|
||||||
|
* \param timeout timeout for the transfer in milliseconds
|
||||||
|
*/
|
||||||
|
static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
|
||||||
|
libusb_device_handle *dev_handle, unsigned char endpoint,
|
||||||
|
unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
|
||||||
|
void *user_data, unsigned int timeout);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.9.5 填充中断传输
|
||||||
|
|
||||||
|
```c
|
||||||
|
/** \ingroup libusb_asyncio
|
||||||
|
* Helper function to populate the required \ref libusb_transfer fields
|
||||||
|
* for an interrupt transfer.
|
||||||
|
*
|
||||||
|
* \param transfer the transfer to populate
|
||||||
|
* \param dev_handle handle of the device that will handle the transfer
|
||||||
|
* \param endpoint address of the endpoint where this transfer will be sent
|
||||||
|
* \param buffer data buffer
|
||||||
|
* \param length length of data buffer
|
||||||
|
* \param callback callback function to be invoked on transfer completion
|
||||||
|
* \param user_data user data to pass to callback function
|
||||||
|
* \param timeout timeout for the transfer in milliseconds
|
||||||
|
*/
|
||||||
|
static inline void libusb_fill_interrupt_transfer(
|
||||||
|
struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
|
||||||
|
unsigned char endpoint, unsigned char *buffer, int length,
|
||||||
|
libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.9.6 填充实时传输
|
||||||
|
|
||||||
|
```c
|
||||||
|
/** \ingroup libusb_asyncio
|
||||||
|
* Helper function to populate the required \ref libusb_transfer fields
|
||||||
|
* for an isochronous transfer.
|
||||||
|
*
|
||||||
|
* \param transfer the transfer to populate
|
||||||
|
* \param dev_handle handle of the device that will handle the transfer
|
||||||
|
* \param endpoint address of the endpoint where this transfer will be sent
|
||||||
|
* \param buffer data buffer
|
||||||
|
* \param length length of data buffer
|
||||||
|
* \param num_iso_packets the number of isochronous packets
|
||||||
|
* \param callback callback function to be invoked on transfer completion
|
||||||
|
* \param user_data user data to pass to callback function
|
||||||
|
* \param timeout timeout for the transfer in milliseconds
|
||||||
|
*/
|
||||||
|
static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
|
||||||
|
libusb_device_handle *dev_handle, unsigned char endpoint,
|
||||||
|
unsigned char *buffer, int length, int num_iso_packets,
|
||||||
|
libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.9.7 提交传输
|
||||||
|
|
||||||
|
```c
|
||||||
|
/** \ingroup libusb_asyncio
|
||||||
|
* Submit a transfer. This function will fire off the USB transfer and then
|
||||||
|
* return immediately.
|
||||||
|
*
|
||||||
|
* \param transfer the transfer to submit
|
||||||
|
* \returns 0 on success
|
||||||
|
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
||||||
|
* \returns \ref LIBUSB_ERROR_BUSY if the transfer has already been submitted.
|
||||||
|
* \returns \ref LIBUSB_ERROR_NOT_SUPPORTED if the transfer flags are not supported
|
||||||
|
* by the operating system.
|
||||||
|
* \returns \ref LIBUSB_ERROR_INVALID_PARAM if the transfer size is larger than
|
||||||
|
* the operating system and/or hardware can support (see \ref asynclimits)
|
||||||
|
* \returns another LIBUSB_ERROR code on other failure
|
||||||
|
*/
|
||||||
|
int API_EXPORTED libusb_submit_transfer(struct libusb_transfer *transfer);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.9.8 处理事件
|
||||||
|
|
||||||
|
有2个函数:
|
||||||
|
|
||||||
|
```c
|
||||||
|
/** \ingroup libusb_poll
|
||||||
|
* Handle any pending events
|
||||||
|
*
|
||||||
|
* Like libusb_handle_events_timeout_completed(), but without the completed
|
||||||
|
* parameter, calling this function is equivalent to calling
|
||||||
|
* libusb_handle_events_timeout_completed() with a NULL completed parameter.
|
||||||
|
*
|
||||||
|
* This function is kept primarily for backwards compatibility.
|
||||||
|
* All new code should call libusb_handle_events_completed() or
|
||||||
|
* libusb_handle_events_timeout_completed() to avoid race conditions.
|
||||||
|
*
|
||||||
|
* \param ctx the context to operate on, or NULL for the default context
|
||||||
|
* \param tv the maximum time to block waiting for events, or an all zero
|
||||||
|
* timeval struct for non-blocking mode
|
||||||
|
* \returns 0 on success, or a LIBUSB_ERROR code on failure
|
||||||
|
*/
|
||||||
|
int API_EXPORTED libusb_handle_events_timeout(libusb_context *ctx,
|
||||||
|
struct timeval *tv);
|
||||||
|
|
||||||
|
/** \ingroup libusb_poll
|
||||||
|
* Handle any pending events in blocking mode. There is currently a timeout
|
||||||
|
* hard-coded at 60 seconds but we plan to make it unlimited in future. For
|
||||||
|
* finer control over whether this function is blocking or non-blocking, or
|
||||||
|
* for control over the timeout, use libusb_handle_events_timeout_completed()
|
||||||
|
* instead.
|
||||||
|
*
|
||||||
|
* This function is kept primarily for backwards compatibility.
|
||||||
|
* All new code should call libusb_handle_events_completed() or
|
||||||
|
* libusb_handle_events_timeout_completed() to avoid race conditions.
|
||||||
|
*
|
||||||
|
* \param ctx the context to operate on, or NULL for the default context
|
||||||
|
* \returns 0 on success, or a LIBUSB_ERROR code on failure
|
||||||
|
*/
|
||||||
|
int API_EXPORTED libusb_handle_events(libusb_context *ctx);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.9.9 释放transfer结构体
|
||||||
|
|
||||||
|
传输完毕,需要释放libusb_transfer结构体,如果要重复利用这个结构体则无需释放。
|
||||||
|
|
||||||
|
```c
|
||||||
|
/** \ingroup libusb_asyncio
|
||||||
|
* Free a transfer structure. This should be called for all transfers
|
||||||
|
* allocated with libusb_alloc_transfer().
|
||||||
|
*
|
||||||
|
* If the \ref libusb_transfer_flags::LIBUSB_TRANSFER_FREE_BUFFER
|
||||||
|
* "LIBUSB_TRANSFER_FREE_BUFFER" flag is set and the transfer buffer is
|
||||||
|
* non-NULL, this function will also free the transfer buffer using the
|
||||||
|
* standard system memory allocator (e.g. free()).
|
||||||
|
*
|
||||||
|
* It is legal to call this function with a NULL transfer. In this case,
|
||||||
|
* the function will simply return safely.
|
||||||
|
*
|
||||||
|
* It is not legal to free an active transfer (one which has been submitted
|
||||||
|
* and has not yet completed).
|
||||||
|
*
|
||||||
|
* \param transfer the transfer to free
|
||||||
|
*/
|
||||||
|
void API_EXPORTED libusb_free_transfer(struct libusb_transfer *transfer);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user