?? io.c
字號:
/* * I/O functions for libusb * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org> * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */#include <config.h>#include <errno.h>#include <poll.h>#include <pthread.h>#include <signal.h>#include <stdint.h>#include <stdlib.h>#include <string.h>#include <sys/time.h>#include <time.h>#include <unistd.h>#include "libusbi.h"/** * \page io Synchronous and asynchronous device I/O * * \section intro Introduction * * If you're using libusb in your application, you're probably wanting to * perform I/O with devices - you want to perform USB data transfers. * * libusb offers two separate interfaces for device I/O. This page aims to * introduce the two in order to help you decide which one is more suitable * for your application. You can also choose to use both interfaces in your * application by considering each transfer on a case-by-case basis. * * Once you have read through the following discussion, you should consult the * detailed API documentation pages for the details: * - \ref syncio * - \ref asyncio * * \section theory Transfers at a logical level * * At a logical level, USB transfers typically happen in two parts. For * example, when reading data from a endpoint: * -# A request for data is sent to the device * -# Some time later, the incoming data is received by the host * * or when writing data to an endpoint: * * -# The data is sent to the device * -# Some time later, the host receives acknowledgement from the device that * the data has been transferred. * * There may be an indefinite delay between the two steps. Consider a * fictional USB input device with a button that the user can press. In order * to determine when the button is pressed, you would likely submit a request * to read data on a bulk or interrupt endpoint and wait for data to arrive. * Data will arrive when the button is pressed by the user, which is * potentially hours later. * * libusb offers both a synchronous and an asynchronous interface to performing * USB transfers. The main difference is that the synchronous interface * combines both steps indicated above into a single function call, whereas * the asynchronous interface separates them. * * \section sync The synchronous interface * * The synchronous I/O interface allows you to perform a USB transfer with * a single function call. When the function call returns, the transfer has * completed and you can parse the results. * * If you have used the libusb-0.1 before, this I/O style will seem familar to * you. libusb-0.1 only offered a synchronous interface. * * In our input device example, to read button presses you might write code * in the following style:\codeunsigned char data[4];int actual_length,int r = libusb_bulk_transfer(handle, EP_IN, data, sizeof(data), &actual_length, 0);if (r == 0 && actual_length == sizeof(data)) { // results of the transaction can now be found in the data buffer // parse them here and report button press} else { error();}\endcode * * The main advantage of this model is simplicity: you did everything with * a single simple function call. * * However, this interface has its limitations. Your application will sleep * inside libusb_bulk_transfer() until the transaction has completed. If it * takes the user 3 hours to press the button, your application will be * sleeping for that long. Execution will be tied up inside the library - * the entire thread will be useless for that duration. * * Another issue is that by tieing up the thread with that single transaction * there is no possibility of performing I/O with multiple endpoints and/or * multiple devices simultaneously, unless you resort to creating one thread * per transaction. * * Additionally, there is no opportunity to cancel the transfer after the * request has been submitted. * * For details on how to use the synchronous API, see the * \ref syncio "synchronous I/O API documentation" pages. * * \section async The asynchronous interface * * Asynchronous I/O is the most significant new feature in libusb-1.0. * Although it is a more complex interface, it solves all the issues detailed * above. * * Instead of providing which functions that block until the I/O has complete, * libusb's asynchronous interface presents non-blocking functions which * begin a transfer and then return immediately. Your application passes a * callback function pointer to this non-blocking function, which libusb will * call with the results of the transaction when it has completed. * * Transfers which have been submitted through the non-blocking functions * can be cancelled with a separate function call. * * The non-blocking nature of this interface allows you to be simultaneously * performing I/O to multiple endpoints on multiple devices, without having * to use threads. * * This added flexibility does come with some complications though: * - In the interest of being a lightweight library, libusb does not create * threads and can only operate when your application is calling into it. Your * application must call into libusb from it's main loop when events are ready * to be handled, or you must use some other scheme to allow libusb to * undertake whatever work needs to be done. * - libusb also needs to be called into at certain fixed points in time in * order to accurately handle transfer timeouts. * - Memory handling becomes more complex. You cannot use stack memory unless * the function with that stack is guaranteed not to return until the transfer * callback has finished executing. * - You generally lose some linearity from your code flow because submitting * the transfer request is done in a separate function from where the transfer * results are handled. This becomes particularly obvious when you want to * submit a second transfer based on the results of an earlier transfer. * * Internally, libusb's synchronous interface is expressed in terms of function * calls to the asynchronous interface. * * For details on how to use the asynchronous API, see the * \ref asyncio "asynchronous I/O API" documentation pages. *//** * \page packetoverflow Packets and overflows * * \section packets Packet abstraction * * The USB specifications describe how data is transmitted in packets, with * constraints on packet size defined by endpoint descriptors. The host must * not send data payloads larger than the endpoint's maximum packet size. * * libusb and the underlying OS abstract out the packet concept, allowing you * to request transfers of any size. Internally, the request will be divided * up into correctly-sized packets. You do not have to be concerned with * packet sizes, but there is one exception when considering overflows. * * \section overflow Bulk/interrupt transfer overflows * * When requesting data on a bulk endpoint, libusb requires you to supply a * buffer and the maximum number of bytes of data that libusb can put in that * buffer. However, the size of the buffer is not communicated to the device - * the device is just asked to send any amount of data. * * There is no problem if the device sends an amount of data that is less than * or equal to the buffer size. libusb reports this condition to you through * the \ref libusb_transfer::actual_length "libusb_transfer.actual_length" * field. * * Problems may occur if the device attempts to send more data than can fit in * the buffer. libusb reports LIBUSB_TRANSFER_OVERFLOW for this condition but * other behaviour is largely undefined: actual_length may or may not be * accurate, the chunk of data that can fit in the buffer (before overflow) * may or may not have been transferred. * * Overflows are nasty, but can be avoided. Even though you were told to * ignore packets above, think about the lower level details: each transfer is * split into packets (typically small, with a maximum size of 512 bytes). * Overflows can only happen if the final packet in an incoming data transfer * is smaller than the actual packet that the device wants to transfer. * Therefore, you will never see an overflow if your transfer buffer size is a * multiple of the endpoint's packet size: the final packet will either * fill up completely or will be only partially filled. *//** * @defgroup asyncio Asynchronous device I/O * * This page details libusb's asynchronous (non-blocking) API for USB device * I/O. This interface is very powerful but is also quite complex - you will * need to read this page carefully to understand the necessary considerations * and issues surrounding use of this interface. Simplistic applications * may wish to consider the \ref syncio "synchronous I/O API" instead. * * The asynchronous interface is built around the idea of separating transfer * submission and handling of transfer completion (the synchronous model * combines both of these into one). There may be a long delay between * submission and completion, however the asynchronous submission function * is non-blocking so will return control to your application during that * potentially long delay. * * \section asyncabstraction Transfer abstraction * * For the asynchronous I/O, libusb implements the concept of a generic * transfer entity for all types of I/O (control, bulk, interrupt, * isochronous). The generic transfer object must be treated slightly * differently depending on which type of I/O you are performing with it. * * This is represented by the public libusb_transfer structure type. * * \section asynctrf Asynchronous transfers * * We can view asynchronous I/O as a 5 step process: * -# Allocation * -# Filling * -# Submission * -# Completion handling * -# Deallocation * * \subsection asyncalloc Allocation * * This step involves allocating memory for a USB transfer. This is the * generic transfer object mentioned above. At this stage, the transfer * is "blank" with no details about what type of I/O it will be used for. * * Allocation is done with the libusb_alloc_transfer() function. You must use * this function rather than allocating your own transfers. * * \subsection asyncfill Filling * * This step is where you take a previously allocated transfer and fill it * with information to determine the message type and direction, data buffer, * callback function, etc. * * You can either fill the required fields yourself or you can use the * helper functions: libusb_fill_control_transfer(), libusb_fill_bulk_transfer() * and libusb_fill_interrupt_transfer(). * * \subsection asyncsubmit Submission * * When you have allocated a transfer and filled it, you can submit it using * libusb_submit_transfer(). This function returns immediately but can be * regarded as firing off the I/O request in the background. * * \subsection asynccomplete Completion handling * * After a transfer has been submitted, one of four things can happen to it: * * - The transfer completes (i.e. some data was transferred) * - The transfer has a timeout and the timeout expires before all data is * transferred * - The transfer fails due to an error * - The transfer is cancelled * * Each of these will cause the user-specified transfer callback function to * be invoked. It is up to the callback function to determine which of the * above actually happened and to act accordingly. * * \subsection Deallocation * * When a transfer has completed (i.e. the callback function has been invoked), * you are advised to free the transfer (unless you wish to resubmit it, see * below). Transfers are deallocated with libusb_free_transfer(). * * It is undefined behaviour to free a transfer which has not completed. * * \section asyncresubmit Resubmission * * You may be wondering why allocation, filling, and submission are all * separated above where they could reasonably be combined into a single * operation. * * The reason for separation is to allow you to resubmit transfers without * having to allocate new ones every time. This is especially useful for * common situations dealing with interrupt endpoints - you allocate one * transfer, fill and submit it, and when it returns with results you just * resubmit it for the next interrupt. * * \section asynccancel Cancellation * * Another advantage of using the asynchronous interface is that you have * the ability to cancel transfers which have not yet completed. This is * done by calling the libusb_cancel_transfer() function. * * libusb_cancel_transfer() is asynchronous/non-blocking in itself. When the * cancellation actually completes, the transfer's callback function will * be invoked, and the callback function should check the transfer status to * determine that it was cancelled. * * Freeing the transfer after it has been cancelled but before cancellation * has completed will result in undefined behaviour. * * \section bulk_overflows Overflows on device-to-host bulk/interrupt endpoints * * If your device does not have predictable transfer sizes (or it misbehaves), * your application may submit a request for data on an IN endpoint which is * smaller than the data that the device wishes to send. In some circumstances * this will cause an overflow, which is a nasty condition to deal with. See * the \ref packetoverflow page for discussion. * * \section asyncctrl Considerations for control transfers * * The <tt>libusb_transfer</tt> structure is generic and hence does not * include specific fields for the control-specific setup packet structure. * * In order to perform a control transfer, you must place the 8-byte setup * packet at the start of the data buffer. To simplify this, you could * cast the buffer pointer to type struct libusb_control_setup, or you can * use the helper function libusb_fill_control_setup().
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -