Changeset 174788f in mainline for uspace/drv/bus/usb/xhci/rh.c


Ignore:
Timestamp:
2017-07-31T20:00:45Z (7 years ago)
Author:
Jaroslav Jindrak <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
834d354
Parents:
7428b92
Message:

Started working on device allocation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/xhci/rh.c

    r7428b92 r174788f  
    3737#include <str_error.h>
    3838#include <usb/debug.h>
     39#include <usb/host/utils/malloc32.h>
    3940#include "debug.h"
     41#include "commands.h"
    4042#include "hc.h"
    4143#include "hw_struct/trb.h"
    4244#include "rh.h"
     45
     46// TODO: Check device deallocation, we free device_ctx in hc.c, not
     47//       sure about the other structs.
     48static int alloc_dev(xhci_hc_t *hc, uint8_t port)
     49{
     50        int err;
     51
     52        xhci_cmd_t *cmd = xhci_alloc_command();
     53        if (!cmd)
     54                return ENOMEM;
     55
     56        xhci_send_enable_slot_command(hc, cmd);
     57        if ((err = xhci_wait_for_command(cmd, 100000)) != EOK)
     58                goto err_command;
     59
     60        uint32_t slot_id = cmd->slot_id;
     61        usb_log_debug2("Obtained slot ID: %u.\n", slot_id);
     62
     63        xhci_free_command(cmd);
     64        cmd = NULL;
     65
     66        xhci_input_ctx_t *ictx = malloc32(sizeof(xhci_input_ctx_t));
     67        if (!ictx) {
     68                err = ENOMEM;
     69                goto err_command;
     70        }
     71
     72        memset(ictx, 0, sizeof(xhci_input_ctx_t));
     73
     74        XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 0);
     75        XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 1);
     76
     77        // TODO: Initialize ictx->slot_ctx according to section 4.3.3
     78        //       point 3. This requires setters for XHCI_SLOT_* and figuring
     79        //       out how are we supposed to find the root string field, which
     80        //       can be found in usb3 spec section 8.9.
     81
     82        // TODO: Allocated and initialize transfer ring for default
     83        //       control endpoint.
     84       
     85        // TODO: Initialize input default control endpoint 0 context.
     86       
     87        // TODO: What's the alignment?
     88        xhci_device_ctx_t *dctx = malloc32(sizeof(xhci_device_ctx_t));
     89        if (!dctx) {
     90                err = ENOMEM;
     91                goto err_ctx;
     92        }
     93        memset(dctx, 0, sizeof(xhci_device_ctx_t));
     94       
     95        hc->dcbaa[slot_id] = addr_to_phys(dctx);
     96        hc->dcbaa_virt[slot_id] = dctx;
     97       
     98        cmd = xhci_alloc_command();
     99        cmd->ictx = ictx;
     100        xhci_send_address_device_command(hc, cmd);
     101        if ((err = xhci_wait_for_command(cmd, 100000)) != EOK)
     102                goto err_ctx;
     103
     104        return EOK;
     105
     106err_ctx:
     107        if (ictx) {
     108                // To avoid double free.
     109                if (cmd && cmd->ictx && cmd->ictx == ictx)
     110                        cmd->ictx = NULL;
     111
     112                free32(ictx);
     113        }
     114err_command:
     115        if (cmd)
     116                xhci_free_command(cmd);
     117        return err;
     118}
    43119
    44120static int handle_connected_device(xhci_hc_t* hc, xhci_port_regs_t* regs, uint8_t port_id)
     
    49125                uint8_t port_speed = XHCI_REG_RD(regs, XHCI_PORT_PS);
    50126                usb_log_debug2("Detected new device on port %u, port speed id %u.", port_id, port_speed);
    51                 // TODO: Assign device slot (specification 4.3.2)
     127
     128                alloc_dev(hc, port_id);
    52129        } else if (link_state == 5) {
    53130                // USB 3 failed to enable
Note: See TracChangeset for help on using the changeset viewer.