Ignore:
File:
1 edited

Legend:

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

    r3ddbd38 r365e29e2  
    5454
    5555/** Nesting pairs of standard descriptors. */
    56 static const usb_dp_descriptor_nesting_t descriptor_nesting[] = {
     56static usb_dp_descriptor_nesting_t descriptor_nesting[] = {
    5757        NESTING(CONFIGURATION, INTERFACE),
    5858        NESTING(INTERFACE, ENDPOINT),
     
    6868 * @return Whether the given descriptor is endpoint descriptor.
    6969 */
    70 static inline bool is_endpoint_descriptor(const uint8_t *descriptor)
     70static inline bool is_endpoint_descriptor(uint8_t *descriptor)
    7171{
    7272        return descriptor[1] == USB_DESCTYPE_ENDPOINT;
     
    8080 */
    8181static bool endpoint_fits_description(const usb_endpoint_description_t *wanted,
    82     const usb_endpoint_description_t *found)
     82    usb_endpoint_description_t *found)
    8383{
    8484#define _SAME(fieldname) ((wanted->fieldname) == (found->fieldname))
     
    120120static usb_endpoint_mapping_t *find_endpoint_mapping(
    121121    usb_endpoint_mapping_t *mapping, size_t mapping_count,
    122     const usb_endpoint_description_t *found_endpoint,
     122    usb_endpoint_description_t *found_endpoint,
    123123    int interface_number, int interface_setting)
    124124{
     
    160160    usb_device_connection_t *wire)
    161161{
     162        usb_endpoint_description_t description;
    162163
    163164        /*
     
    166167
    167168        /* Actual endpoint number is in bits 0..3 */
    168         const usb_endpoint_t ep_no = endpoint->endpoint_address & 0x0F;
    169 
    170         const usb_endpoint_description_t description = {
    171                 /* Endpoint direction is set by bit 7 */
    172                 .direction = (endpoint->endpoint_address & 128)
    173                     ? USB_DIRECTION_IN : USB_DIRECTION_OUT,
    174                 /* Transfer type is in bits 0..2 and
    175                  * the enum values corresponds 1:1 */
    176                 .transfer_type = endpoint->attributes & 3,
    177 
    178                 /* Get interface characteristics. */
    179                 .interface_class = interface->interface_class,
    180                 .interface_subclass = interface->interface_subclass,
    181                 .interface_protocol = interface->interface_protocol,
    182         };
     169        usb_endpoint_t ep_no = endpoint->endpoint_address & 0x0F;
     170
     171        /* Endpoint direction is set by bit 7 */
     172        description.direction = (endpoint->endpoint_address & 128)
     173            ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
     174        /* Transfer type is in bits 0..2 and the enum values corresponds 1:1 */
     175        description.transfer_type = endpoint->attributes & 3;
     176
     177        /*
     178         * Get interface characteristics.
     179         */
     180        description.interface_class = interface->interface_class;
     181        description.interface_subclass = interface->interface_subclass;
     182        description.interface_protocol = interface->interface_protocol;
    183183
    184184        /*
     
    192192        }
    193193
     194        if (ep_mapping->pipe == NULL) {
     195                return EBADMEM;
     196        }
    194197        if (ep_mapping->present) {
    195198                return EEXISTS;
    196199        }
    197200
    198         int rc = usb_pipe_initialize(&ep_mapping->pipe, wire,
     201        int rc = usb_pipe_initialize(ep_mapping->pipe, wire,
    199202            ep_no, description.transfer_type, endpoint->max_packet_size,
    200203            description.direction);
     
    221224static int process_interface(
    222225    usb_endpoint_mapping_t *mapping, size_t mapping_count,
    223     const usb_dp_parser_t *parser, const usb_dp_parser_data_t *parser_data,
    224     const uint8_t *interface_descriptor)
    225 {
    226         const uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
     226    usb_dp_parser_t *parser, usb_dp_parser_data_t *parser_data,
     227    uint8_t *interface_descriptor)
     228{
     229        uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
    227230            parser_data, interface_descriptor);
    228231
     
    251254 *
    252255 * The mapping array is expected to conform to following rules:
    253  * - @c pipe must be uninitialized pipe
     256 * - @c pipe must point to already allocated structure with uninitialized pipe
    254257 * - @c description must point to prepared endpoint description
    255258 * - @c descriptor does not need to be initialized (will be overwritten)
     
    281284int usb_pipe_initialize_from_configuration(
    282285    usb_endpoint_mapping_t *mapping, size_t mapping_count,
    283     const uint8_t *config_descriptor, size_t config_descriptor_size,
     286    uint8_t *configuration_descriptor, size_t configuration_descriptor_size,
    284287    usb_device_connection_t *connection)
    285288{
    286289        assert(connection);
    287290
    288         if (config_descriptor == NULL) {
     291        if (configuration_descriptor == NULL) {
    289292                return EBADMEM;
    290293        }
    291         if (config_descriptor_size
     294        if (configuration_descriptor_size
    292295            < sizeof(usb_standard_configuration_descriptor_t)) {
    293296                return ERANGE;
    294297        }
    295298
    296         /* Go through the mapping and set all endpoints to not present. */
    297         for (size_t i = 0; i < mapping_count; i++) {
     299        /*
     300         * Go through the mapping and set all endpoints to not present.
     301         */
     302        size_t i;
     303        for (i = 0; i < mapping_count; i++) {
    298304                mapping[i].present = false;
    299305                mapping[i].descriptor = NULL;
     
    301307        }
    302308
    303         /* Prepare the descriptor parser. */
    304         const usb_dp_parser_t dp_parser = {
     309        /*
     310         * Prepare the descriptor parser.
     311         */
     312        usb_dp_parser_t dp_parser = {
    305313                .nesting = descriptor_nesting
    306314        };
    307         const usb_dp_parser_data_t dp_data = {
    308                 .data = config_descriptor,
    309                 .size = config_descriptor_size,
     315        usb_dp_parser_data_t dp_data = {
     316                .data = configuration_descriptor,
     317                .size = configuration_descriptor_size,
    310318                .arg = connection
    311319        };
     
    314322         * Iterate through all interfaces.
    315323         */
    316         const uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
    317             &dp_data, config_descriptor);
     324        uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
     325            &dp_data, configuration_descriptor);
    318326        if (interface == NULL) {
    319327                return ENOENT;
     
    321329        do {
    322330                (void) process_interface(mapping, mapping_count,
    323                     &dp_parser, &dp_data, interface);
     331                    &dp_parser, &dp_data,
     332                    interface);
    324333                interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
    325                     config_descriptor, interface);
     334                    configuration_descriptor, interface);
    326335        } while (interface != NULL);
    327336
     
    405414        }
    406415
     416#define TRY_LOOP(attempt_var) \
     417        for (attempt_var = 0; attempt_var < 3; attempt_var++)
     418
     419        size_t failed_attempts;
     420        int rc;
    407421
    408422        usb_pipe_start_long_transfer(pipe);
     
    410424        uint8_t dev_descr_start[CTRL_PIPE_MIN_PACKET_SIZE];
    411425        size_t transferred_size;
    412         int rc;
    413         for (size_t attempt_var = 0; attempt_var < 3; ++attempt_var) {
     426        TRY_LOOP(failed_attempts) {
    414427                rc = usb_request_get_descriptor(pipe, USB_REQUEST_TYPE_STANDARD,
    415428                    USB_REQUEST_RECIPIENT_DEVICE, USB_DESCTYPE_DEVICE,
     
    442455 * @return Error code.
    443456 */
    444 int usb_pipe_register(usb_pipe_t *pipe, unsigned interval,
     457int usb_pipe_register(usb_pipe_t *pipe,
     458    unsigned int interval,
    445459    usb_hc_connection_t *hc_connection)
    446460{
     461        return usb_pipe_register_with_speed(pipe, USB_SPEED_MAX + 1,
     462            interval, hc_connection);
     463}
     464
     465/** Register endpoint with a speed at the host controller.
     466 *
     467 * You will rarely need to use this function because it is needed only
     468 * if the registered endpoint is of address 0 and there is no other way
     469 * to tell speed of the device at address 0.
     470 *
     471 * @param pipe Pipe to be registered.
     472 * @param speed Speed of the device
     473 *      (invalid speed means use previously specified one).
     474 * @param interval Polling interval.
     475 * @param hc_connection Connection to the host controller (must be opened).
     476 * @return Error code.
     477 */
     478int usb_pipe_register_with_speed(usb_pipe_t *pipe, usb_speed_t speed,
     479    unsigned int interval,
     480    usb_hc_connection_t *hc_connection)
     481{
    447482        assert(pipe);
    448         assert(pipe->wire);
    449483        assert(hc_connection);
    450 
     484       
    451485        if (!usb_hc_connection_is_opened(hc_connection))
    452486                return EBADF;
     487       
     488        const usb_target_t target =
     489            {{ .address = pipe->wire->address, .endpoint = pipe->endpoint_no }};
     490#define _PACK2(high, low) (((high) << 16) + (low))
     491#define _PACK3(high, middle, low) (((((high) << 8) + (middle)) << 8) + (low))
     492       
    453493        async_exch_t *exch = async_exchange_begin(hc_connection->hc_sess);
    454         if (!exch)
    455                 return ENOMEM;
    456         const int ret = usbhc_register_endpoint(exch,
    457             pipe->wire->address, pipe->endpoint_no, pipe->transfer_type,
    458             pipe->direction, pipe->max_packet_size, interval);
    459 
     494        int rc = async_req_4_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
     495            IPC_M_USBHC_REGISTER_ENDPOINT, target.packed,
     496            _PACK3(speed, pipe->transfer_type, pipe->direction),
     497            _PACK2(pipe->max_packet_size, interval));
    460498        async_exchange_end(exch);
    461         return ret;
     499       
     500#undef _PACK2
     501#undef _PACK3
     502       
     503        return rc;
    462504}
    463505
     
    472514{
    473515        assert(pipe);
    474         assert(pipe->wire);
    475516        assert(hc_connection);
    476 
     517       
    477518        if (!usb_hc_connection_is_opened(hc_connection))
    478519                return EBADF;
    479 
     520       
    480521        async_exch_t *exch = async_exchange_begin(hc_connection->hc_sess);
    481         if (!exch)
    482                 return ENOMEM;
    483         const int ret = usbhc_unregister_endpoint(exch,
     522        int rc = async_req_4_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
     523            IPC_M_USBHC_UNREGISTER_ENDPOINT,
    484524            pipe->wire->address, pipe->endpoint_no, pipe->direction);
    485525        async_exchange_end(exch);
    486 
    487         return ret;
     526       
     527        return rc;
    488528}
    489529
Note: See TracChangeset for help on using the changeset viewer.