Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbdev/src/pipes.c

    r01086cfe r9f7276d  
    11/*
    22 * Copyright (c) 2011 Vojtech Horky
    3  * Copyright (c) 2011 Jan Vesely
    43 * All rights reserved.
    54 *
     
    2726 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2827 */
     28
    2929/** @addtogroup libusbdev
    3030 * @{
    3131 */
    3232/** @file
    33  * USB endpoint pipes functions.
    34  */
     33 * USB endpoint pipes miscellaneous functions.
     34 */
     35#include <usb/usb.h>
    3536#include <usb/dev/pipes.h>
    36 #include <usb/dev/request.h>
     37#include <usb/debug.h>
     38#include <usb/hc.h>
     39#include <usbhc_iface.h>
     40#include <usb_iface.h>
     41#include <devman.h>
    3742#include <errno.h>
    3843#include <assert.h>
    39 
    40 /** Prepare pipe for a long transfer.
    41  *
    42  * Long transfer is transfer consisting of several requests to the HC.
    43  * Calling this function is optional and it has positive effect of
    44  * improved performance because IPC session is initiated only once.
    45  *
    46  * @param pipe Pipe over which the transfer will happen.
    47  * @return Error code.
    48  */
    49 int usb_pipe_start_long_transfer(usb_pipe_t *pipe)
    50 {
    51         assert(pipe);
    52         assert(pipe->wire);
    53         assert(pipe->wire->hc_connection);
    54         return usb_hc_connection_open(pipe->wire->hc_connection);
    55 }
    56 /*----------------------------------------------------------------------------*/
    57 /** Terminate a long transfer on a pipe.
    58  * @param pipe Pipe where to end the long transfer.
    59  * @return Error code.
    60  * @see usb_pipe_start_long_transfer
    61  */
    62 int usb_pipe_end_long_transfer(usb_pipe_t *pipe)
    63 {
    64         assert(pipe);
    65         assert(pipe->wire);
    66         assert(pipe->wire->hc_connection);
    67         return usb_hc_connection_close(pipe->wire->hc_connection);
    68 }
    69 /*----------------------------------------------------------------------------*/
    70 /** Try to clear endpoint halt of default control pipe.
    71  *
    72  * @param pipe Pipe for control endpoint zero.
    73  */
    74 static void clear_self_endpoint_halt(usb_pipe_t *pipe)
    75 {
    76         assert(pipe != NULL);
    77 
    78         if (!pipe->auto_reset_halt || (pipe->endpoint_no != 0)) {
    79                 return;
    80         }
    81 
    82         /* Prevent infinite recursion. */
    83         pipe->auto_reset_halt = false;
    84         usb_request_clear_endpoint_halt(pipe, 0);
    85         pipe->auto_reset_halt = true;
    86 }
    87 /*----------------------------------------------------------------------------*/
    88 /** Request a control read transfer on an endpoint pipe.
    89  *
    90  * This function encapsulates all three stages of a control transfer.
    91  *
    92  * @param[in] pipe Pipe used for the transfer.
    93  * @param[in] setup_buffer Buffer with the setup packet.
    94  * @param[in] setup_buffer_size Size of the setup packet (in bytes).
    95  * @param[out] data_buffer Buffer for incoming data.
    96  * @param[in] data_buffer_size Size of the buffer for incoming data (in bytes).
    97  * @param[out] data_transfered_size Number of bytes that were actually
    98  *                                  transfered during the DATA stage.
    99  * @return Error code.
    100  */
    101 int usb_pipe_control_read(usb_pipe_t *pipe,
    102     const void *setup_buffer, size_t setup_buffer_size,
    103     void *buffer, size_t buffer_size, size_t *transfered_size)
    104 {
    105         assert(pipe);
    106 
    107         if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
     44#include "pipepriv.h"
     45
     46#define IPC_AGAIN_DELAY (1000 * 2) /* 2ms */
     47
     48/** Tell USB address assigned to given device.
     49 *
     50 * @param sess Session to parent device.
     51 * @param dev Device in question.
     52 * @return USB address or error code.
     53 */
     54static usb_address_t get_my_address(async_sess_t *sess, ddf_dev_t *dev)
     55{
     56        async_exch_t *exch = async_exchange_begin(sess);
     57       
     58        /*
     59         * We are sending special value as a handle - zero - to get
     60         * handle of the parent function (that handle was used
     61         * when registering our device @p dev.
     62         */
     63        sysarg_t address;
     64        int rc = async_req_2_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
     65            IPC_M_USB_GET_ADDRESS, 0, &address);
     66       
     67        async_exchange_end(exch);
     68       
     69        if (rc != EOK)
     70                return rc;
     71       
     72        return (usb_address_t) address;
     73}
     74
     75/** Tell USB interface assigned to given device.
     76 *
     77 * @param device Device in question.
     78 * @return Interface number (negative code means any).
     79 */
     80int usb_device_get_assigned_interface(ddf_dev_t *device)
     81{
     82        async_sess_t *parent_sess =
     83            devman_parent_device_connect(EXCHANGE_ATOMIC, device->handle,
     84            IPC_FLAG_BLOCKING);
     85        if (!parent_sess)
     86                return -1;
     87       
     88        async_exch_t *exch = async_exchange_begin(parent_sess);
     89       
     90        sysarg_t iface_no;
     91        int rc = async_req_2_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
     92            IPC_M_USB_GET_INTERFACE, device->handle, &iface_no);
     93       
     94        async_exchange_end(exch);
     95        async_hangup(parent_sess);
     96       
     97        if (rc != EOK)
     98                return -1;
     99       
     100        return (int) iface_no;
     101}
     102
     103/** Initialize connection to USB device.
     104 *
     105 * @param connection Connection structure to be initialized.
     106 * @param dev Generic device backing the USB device.
     107 * @return Error code.
     108 */
     109int usb_device_connection_initialize_from_device(
     110    usb_device_connection_t *connection, ddf_dev_t *dev)
     111{
     112        assert(connection);
     113        assert(dev);
     114       
     115        int rc;
     116        devman_handle_t hc_handle;
     117        usb_address_t my_address;
     118       
     119        rc = usb_hc_find(dev->handle, &hc_handle);
     120        if (rc != EOK)
     121                return rc;
     122       
     123        async_sess_t *parent_sess =
     124            devman_parent_device_connect(EXCHANGE_ATOMIC, dev->handle,
     125            IPC_FLAG_BLOCKING);
     126        if (!parent_sess)
     127                return ENOMEM;
     128       
     129        /*
     130         * Asking for "my" address may require several attempts.
     131         * That is because following scenario may happen:
     132         *  - parent driver (i.e. driver of parent device) announces new device
     133         *    and devman launches current driver
     134         *  - parent driver is preempted and thus does not send address-handle
     135         *    binding to HC driver
     136         *  - this driver gets here and wants the binding
     137         *  - the HC does not know the binding yet and thus it answers ENOENT
     138         *  So, we need to wait for the HC to learn the binding.
     139         */
     140       
     141        do {
     142                my_address = get_my_address(parent_sess, dev);
     143               
     144                if (my_address == ENOENT) {
     145                        /* Be nice, let other fibrils run and try again. */
     146                        async_usleep(IPC_AGAIN_DELAY);
     147                } else if (my_address < 0) {
     148                        /* Some other problem, no sense trying again. */
     149                        rc = my_address;
     150                        goto leave;
     151                }
     152       
     153        } while (my_address < 0);
     154       
     155        rc = usb_device_connection_initialize(connection,
     156            hc_handle, my_address);
     157       
     158leave:
     159        async_hangup(parent_sess);
     160        return rc;
     161}
     162
     163/** Initialize connection to USB device.
     164 *
     165 * @param connection Connection structure to be initialized.
     166 * @param host_controller_handle Devman handle of host controller device is
     167 *      connected to.
     168 * @param device_address Device USB address.
     169 * @return Error code.
     170 */
     171int usb_device_connection_initialize(usb_device_connection_t *connection,
     172    devman_handle_t host_controller_handle, usb_address_t device_address)
     173{
     174        assert(connection);
     175
     176        if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
    108177                return EINVAL;
    109178        }
    110179
    111         if ((buffer == NULL) || (buffer_size == 0)) {
    112                 return EINVAL;
     180        connection->hc_handle = host_controller_handle;
     181        connection->address = device_address;
     182
     183        return EOK;
     184}
     185
     186/** Initialize connection to USB device on default address.
     187 *
     188 * @param dev_connection Device connection structure to be initialized.
     189 * @param hc_connection Initialized connection to host controller.
     190 * @return Error code.
     191 */
     192int usb_device_connection_initialize_on_default_address(
     193    usb_device_connection_t *dev_connection,
     194    usb_hc_connection_t *hc_connection)
     195{
     196        assert(dev_connection);
     197
     198        if (hc_connection == NULL) {
     199                return EBADMEM;
    113200        }
    114201
    115         if ((pipe->direction != USB_DIRECTION_BOTH)
    116             || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
    117                 return EBADF;
    118         }
    119 
    120         uint64_t setup_packet;
    121         memcpy(&setup_packet, setup_buffer, 8);
    122 
    123         size_t act_size = 0;
    124         const int rc = usb_device_control_read(pipe->wire,
    125             pipe->endpoint_no, setup_packet, buffer, buffer_size, &act_size);
    126 
    127         if (rc == ESTALL) {
    128                 clear_self_endpoint_halt(pipe);
    129         }
    130 
    131         if (rc == EOK && transfered_size != NULL) {
    132                 *transfered_size = act_size;
    133         }
    134 
    135         return rc;
    136 }
    137 /*----------------------------------------------------------------------------*/
    138 /** Request a control write transfer on an endpoint pipe.
    139  *
    140  * This function encapsulates all three stages of a control transfer.
    141  *
    142  * @param[in] pipe Pipe used for the transfer.
    143  * @param[in] setup_buffer Buffer with the setup packet.
    144  * @param[in] setup_buffer_size Size of the setup packet (in bytes).
    145  * @param[in] data_buffer Buffer with data to be sent.
    146  * @param[in] data_buffer_size Size of the buffer with outgoing data (in bytes).
    147  * @return Error code.
    148  */
    149 int usb_pipe_control_write(usb_pipe_t *pipe,
    150     const void *setup_buffer, size_t setup_buffer_size,
    151     const void *buffer, size_t buffer_size)
    152 {
    153         assert(pipe);
    154 
    155         if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
    156                 return EINVAL;
    157         }
    158 
    159         if ((buffer == NULL) && (buffer_size > 0)) {
    160                 return EINVAL;
    161         }
    162 
    163         if ((buffer != NULL) && (buffer_size == 0)) {
    164                 return EINVAL;
    165         }
    166 
    167         if ((pipe->direction != USB_DIRECTION_BOTH)
    168             || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
    169                 return EBADF;
    170         }
    171 
    172         uint64_t setup_packet;
    173         memcpy(&setup_packet, setup_buffer, 8);
    174 
    175         const int rc = usb_device_control_write(pipe->wire,
    176             pipe->endpoint_no, setup_packet, buffer, buffer_size);
    177 
    178         if (rc == ESTALL) {
    179                 clear_self_endpoint_halt(pipe);
    180         }
    181 
    182         return rc;
    183 }
    184 /*----------------------------------------------------------------------------*/
    185 /** Request a read (in) transfer on an endpoint pipe.
    186  *
    187  * @param[in] pipe Pipe used for the transfer.
    188  * @param[out] buffer Buffer where to store the data.
    189  * @param[in] size Size of the buffer (in bytes).
    190  * @param[out] size_transfered Number of bytes that were actually transfered.
    191  * @return Error code.
    192  */
    193 int usb_pipe_read(usb_pipe_t *pipe,
    194     void *buffer, size_t size, size_t *size_transfered)
    195 {
    196         assert(pipe);
    197 
    198         if (buffer == NULL) {
    199                 return EINVAL;
    200         }
    201 
    202         if (size == 0) {
    203                 return EINVAL;
    204         }
    205 
    206         if (pipe->direction != USB_DIRECTION_IN) {
    207                 return EBADF;
    208         }
    209 
    210         if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
    211                 return EBADF;
    212         }
    213 
    214         /* Isochronous transfer are not supported (yet) */
    215         if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
    216             pipe->transfer_type != USB_TRANSFER_BULK)
    217             return ENOTSUP;
    218 
    219         size_t act_size = 0;
    220         const int rc = usb_device_read(pipe->wire,
    221             pipe->endpoint_no, buffer, size, &act_size);
    222 
    223         if (rc == EOK && size_transfered != NULL) {
    224                 *size_transfered = act_size;
    225         }
    226 
    227         return rc;
    228 }
    229 /*----------------------------------------------------------------------------*/
    230 /** Request a write (out) transfer on an endpoint pipe.
    231  *
    232  * @param[in] pipe Pipe used for the transfer.
    233  * @param[in] buffer Buffer with data to transfer.
    234  * @param[in] size Size of the buffer (in bytes).
    235  * @return Error code.
    236  */
    237 int usb_pipe_write(usb_pipe_t *pipe, const void *buffer, size_t size)
    238 {
    239         assert(pipe);
    240 
    241         if (buffer == NULL || size == 0) {
    242                 return EINVAL;
    243         }
    244 
    245         if (pipe->direction != USB_DIRECTION_OUT) {
    246                 return EBADF;
    247         }
    248 
    249         if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
    250                 return EBADF;
    251         }
    252 
    253         /* Isochronous transfer are not supported (yet) */
    254         if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
    255             pipe->transfer_type != USB_TRANSFER_BULK)
    256             return ENOTSUP;
    257 
    258         return usb_device_write(pipe->wire,
    259             pipe->endpoint_no, buffer, size);
    260 }
    261 /*----------------------------------------------------------------------------*/
    262 /** Initialize USB endpoint pipe.
    263  *
    264  * @param pipe Endpoint pipe to be initialized.
    265  * @param connection Connection to the USB device backing this pipe (the wire).
    266  * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
    267  * @param transfer_type Transfer type (e.g. interrupt or bulk).
    268  * @param max_packet_size Maximum packet size in bytes.
    269  * @param direction Endpoint direction (in/out).
    270  * @return Error code.
    271  */
    272 int usb_pipe_initialize(usb_pipe_t *pipe,
    273     usb_device_connection_t *connection, usb_endpoint_t endpoint_no,
    274     usb_transfer_type_t transfer_type, size_t max_packet_size,
    275     usb_direction_t direction)
    276 {
    277         assert(pipe);
    278         assert(connection);
    279 
    280         pipe->wire = connection;
    281         pipe->endpoint_no = endpoint_no;
    282         pipe->transfer_type = transfer_type;
    283         pipe->max_packet_size = max_packet_size;
    284         pipe->direction = direction;
    285         pipe->auto_reset_halt = false;
    286 
    287         return EOK;
    288 }
    289 /*----------------------------------------------------------------------------*/
    290 /** Initialize USB endpoint pipe as the default zero control pipe.
    291  *
    292  * @param pipe Endpoint pipe to be initialized.
    293  * @param connection Connection to the USB device backing this pipe (the wire).
    294  * @return Error code.
    295  */
    296 int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
    297     usb_device_connection_t *connection)
    298 {
    299         assert(pipe);
    300         assert(connection);
    301 
    302         int rc = usb_pipe_initialize(pipe, connection, 0, USB_TRANSFER_CONTROL,
    303             CTRL_PIPE_MIN_PACKET_SIZE, USB_DIRECTION_BOTH);
    304 
    305         pipe->auto_reset_halt = true;
    306 
    307         return rc;
    308 }
    309 /*----------------------------------------------------------------------------*/
    310 /** Register endpoint with the host controller.
    311  *
    312  * @param pipe Pipe to be registered.
    313  * @param interval Polling interval.
    314  * @return Error code.
    315  */
    316 int usb_pipe_register(usb_pipe_t *pipe, unsigned interval)
    317 {
    318         assert(pipe);
    319         assert(pipe->wire);
    320 
    321         return usb_device_register_endpoint(pipe->wire,
    322            pipe->endpoint_no, pipe->transfer_type,
    323            pipe->direction, pipe->max_packet_size, interval);
    324 }
    325 /*----------------------------------------------------------------------------*/
    326 /** Revert endpoint registration with the host controller.
    327  *
    328  * @param pipe Pipe to be unregistered.
    329  * @return Error code.
    330  */
    331 int usb_pipe_unregister(usb_pipe_t *pipe)
    332 {
    333         assert(pipe);
    334         assert(pipe->wire);
    335 
    336         return usb_device_unregister_endpoint(pipe->wire,
    337             pipe->endpoint_no, pipe->direction);
     202        return usb_device_connection_initialize(dev_connection,
     203            hc_connection->hc_handle, (usb_address_t) 0);
     204}
     205
     206/** Prepare pipe for a long transfer.
     207 *
     208 * By a long transfer is mean transfer consisting of several
     209 * requests to the HC.
     210 * Calling such function is optional and it has positive effect of
     211 * improved performance because IPC session is initiated only once.
     212 *
     213 * @param pipe Pipe over which the transfer will happen.
     214 * @return Error code.
     215 */
     216void usb_pipe_start_long_transfer(usb_pipe_t *pipe)
     217{
     218        (void) pipe_add_ref(pipe, true);
     219}
     220
     221/** Terminate a long transfer on a pipe.
     222 *
     223 * @see usb_pipe_start_long_transfer
     224 *
     225 * @param pipe Pipe where to end the long transfer.
     226 */
     227void usb_pipe_end_long_transfer(usb_pipe_t *pipe)
     228{
     229        pipe_drop_ref(pipe);
    338230}
    339231
Note: See TracChangeset for help on using the changeset viewer.