Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 8969f46 in mainline


Ignore:
Timestamp:
2011-11-26T13:33:48Z (10 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master
Children:
3f0ad85a
Parents:
b4292e7
Message:

libusbdev: Move pipe ref manipulation to no_check variants of read/write.

Location:
uspace/lib/usbdev/src
Files:
2 edited

Legend:

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

    rb4292e7 r8969f46  
    8787       
    8888        if (pipe->refcount == 0) {
     89                assert(pipe->hc_sess == NULL);
    8990                /* Need to open the phone by ourselves. */
    9091                async_sess_t *sess =
  • uspace/lib/usbdev/src/pipesio.c

    rb4292e7 r8969f46  
    6565    void *buffer, size_t size, size_t *size_transfered)
    6666{
     67        /* Isochronous transfer are not supported (yet) */
     68        if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
     69            pipe->transfer_type != USB_TRANSFER_BULK &&
     70            pipe->transfer_type != USB_TRANSFER_CONTROL)
     71            return ENOTSUP;
     72
     73        int ret = pipe_add_ref(pipe, false);
     74        if (ret != EOK) {
     75                return ret;
     76        }
     77
     78        /* Ensure serialization over the phone. */
     79        pipe_start_transaction(pipe);
     80        async_exch_t *exch = async_exchange_begin(pipe->hc_sess);
     81        if (!exch) {
     82                pipe_end_transaction(pipe);
     83                pipe_drop_ref(pipe);
     84                return ENOMEM;
     85        }
     86
     87        ret = usbhc_read(exch, pipe->wire->address, pipe->endpoint_no,
     88            setup, buffer, size, size_transfered);
     89        async_exchange_end(exch);
     90        pipe_end_transaction(pipe);
     91        pipe_drop_ref(pipe);
     92        return ret;
     93}
     94
     95/** Request a read (in) transfer on an endpoint pipe.
     96 *
     97 * @param[in] pipe Pipe used for the transfer.
     98 * @param[out] buffer Buffer where to store the data.
     99 * @param[in] size Size of the buffer (in bytes).
     100 * @param[out] size_transfered Number of bytes that were actually transfered.
     101 * @return Error code.
     102 */
     103int usb_pipe_read(usb_pipe_t *pipe,
     104    void *buffer, size_t size, size_t *size_transfered)
     105{
     106        assert(pipe);
     107
     108        if (buffer == NULL) {
     109                return EINVAL;
     110        }
     111
     112        if (size == 0) {
     113                return EINVAL;
     114        }
     115
     116        if (pipe->direction != USB_DIRECTION_IN) {
     117                return EBADF;
     118        }
     119
     120        if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
     121                return EBADF;
     122        }
     123
     124        size_t act_size = 0;
     125        const int rc = usb_pipe_read_no_check(pipe, 0, buffer, size, &act_size);
     126
     127
     128        if (rc == EOK && size_transfered != NULL) {
     129                *size_transfered = act_size;
     130        }
     131
     132        return rc;
     133}
     134
     135/** Request an out transfer, no checking of input parameters.
     136 *
     137 * @param[in] pipe Pipe used for the transfer.
     138 * @param[in] buffer Buffer with data to transfer.
     139 * @param[in] size Size of the buffer (in bytes).
     140 * @return Error code.
     141 */
     142static int usb_pipe_write_no_check(usb_pipe_t *pipe, uint64_t setup,
     143    const void *buffer, size_t size)
     144{
    67145        /* Only interrupt and bulk transfers are supported */
    68146        if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
     
    71149            return ENOTSUP;
    72150
     151        int ret = pipe_add_ref(pipe, false);
     152        if (ret != EOK) {
     153                return ret;
     154        }
     155
    73156        /* Ensure serialization over the phone. */
    74157        pipe_start_transaction(pipe);
     
    76159        if (!exch) {
    77160                pipe_end_transaction(pipe);
     161                pipe_drop_ref(pipe);
    78162                return ENOMEM;
    79163        }
    80 
    81         const int ret = usbhc_read(exch, pipe->wire->address, pipe->endpoint_no,
    82             setup, buffer, size, size_transfered);
    83         async_exchange_end(exch);
    84         pipe_end_transaction(pipe);
    85         return ret;
    86 }
    87 
    88 /** Request a read (in) transfer on an endpoint pipe.
    89  *
    90  * @param[in] pipe Pipe used for the transfer.
    91  * @param[out] buffer Buffer where to store the data.
    92  * @param[in] size Size of the buffer (in bytes).
    93  * @param[out] size_transfered Number of bytes that were actually transfered.
    94  * @return Error code.
    95  */
    96 int usb_pipe_read(usb_pipe_t *pipe,
    97     void *buffer, size_t size, size_t *size_transfered)
    98 {
    99         assert(pipe);
    100 
    101         if (buffer == NULL) {
    102                 return EINVAL;
    103         }
    104 
    105         if (size == 0) {
    106                 return EINVAL;
    107         }
    108 
    109         if (pipe->direction != USB_DIRECTION_IN) {
    110                 return EBADF;
    111         }
    112 
    113         if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
    114                 return EBADF;
    115         }
    116 
    117         int rc;
    118         rc = pipe_add_ref(pipe, false);
    119         if (rc != EOK) {
    120                 return rc;
    121         }
    122 
    123 
    124         size_t act_size = 0;
    125 
    126         rc = usb_pipe_read_no_check(pipe, 0, buffer, size, &act_size);
    127 
    128         pipe_drop_ref(pipe);
    129 
    130         if (rc != EOK) {
    131                 return rc;
    132         }
    133 
    134         if (size_transfered != NULL) {
    135                 *size_transfered = act_size;
    136         }
    137 
    138         return EOK;
    139 }
    140 
    141 /** Request an out transfer, no checking of input parameters.
    142  *
    143  * @param[in] pipe Pipe used for the transfer.
    144  * @param[in] buffer Buffer with data to transfer.
    145  * @param[in] size Size of the buffer (in bytes).
    146  * @return Error code.
    147  */
    148 static int usb_pipe_write_no_check(usb_pipe_t *pipe, uint64_t setup,
    149     const void *buffer, size_t size)
    150 {
    151         /* Only interrupt and bulk transfers are supported */
    152         if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
    153             pipe->transfer_type != USB_TRANSFER_BULK &&
    154             pipe->transfer_type != USB_TRANSFER_CONTROL)
    155             return ENOTSUP;
    156 
    157         /* Ensure serialization over the phone. */
    158         pipe_start_transaction(pipe);
    159         async_exch_t *exch = async_exchange_begin(pipe->hc_sess);
    160         if (!exch) {
    161                 pipe_end_transaction(pipe);
    162                 return ENOMEM;
    163         }
    164         const int ret =
    165             usbhc_write(exch, pipe->wire->address, pipe->endpoint_no,
     164        ret = usbhc_write(exch, pipe->wire->address, pipe->endpoint_no,
    166165            setup, buffer, size);
    167166        async_exchange_end(exch);
    168167        pipe_end_transaction(pipe);
     168        pipe_drop_ref(pipe);
    169169        return ret;
    170170}
     
    193193        }
    194194
    195         int rc;
    196         rc = pipe_add_ref(pipe, false);
    197         if (rc != EOK) {
    198                 return rc;
    199         }
    200 
    201         rc = usb_pipe_write_no_check(pipe, 0, buffer, size);
    202 
    203         pipe_drop_ref(pipe);
    204 
    205         return rc;
     195        return usb_pipe_write_no_check(pipe, 0, buffer, size);
    206196}
    207197
     
    260250        memcpy(&setup_packet, setup_buffer, 8);
    261251
    262         int rc;
    263 
    264         rc = pipe_add_ref(pipe, false);
    265         if (rc != EOK) {
    266                 return rc;
    267         }
    268 
    269252        size_t act_size = 0;
    270         rc = usb_pipe_read_no_check(pipe, setup_packet,
     253        const int rc = usb_pipe_read_no_check(pipe, setup_packet,
    271254            data_buffer, data_buffer_size, &act_size);
    272255
     
    275258        }
    276259
    277         pipe_drop_ref(pipe);
    278 
    279         if (rc != EOK) {
    280                 return rc;
    281         }
    282 
    283         if (data_transfered_size != NULL) {
     260        if (rc == EOK && data_transfered_size != NULL) {
    284261                *data_transfered_size = act_size;
    285262        }
    286263
    287         return EOK;
     264        return rc;
    288265}
    289266
     
    325302        memcpy(&setup_packet, setup_buffer, 8);
    326303
    327         int rc;
    328         rc = pipe_add_ref(pipe, false);
    329         if (rc != EOK) {
    330                 return rc;
    331         }
    332 
    333         rc = usb_pipe_write_no_check(pipe, setup_packet,
     304        const int rc = usb_pipe_write_no_check(pipe, setup_packet,
    334305            data_buffer, data_buffer_size);
    335306
     
    338309        }
    339310
    340         pipe_drop_ref(pipe);
    341 
    342311        return rc;
    343312}
    344 
    345 
    346313/**
    347314 * @}
Note: See TracChangeset for help on using the changeset viewer.