Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/uhci-hcd/uhci.c

    rb9d910f r4abc304  
    3333 */
    3434#include <errno.h>
     35#include <str_error.h>
    3536#include <adt/list.h>
     37#include <libarch/ddi.h>
    3638
    3739#include <usb/debug.h>
    3840#include <usb/usb.h>
     41#include <usb/ddfiface.h>
     42#include <usb_iface.h>
    3943
    4044#include "uhci.h"
     45#include "iface.h"
     46
    4147static irq_cmd_t uhci_cmds[] = {
    4248        {
    4349                .cmd = CMD_PIO_READ_16,
    44                 .addr = (void*)0xc022,
     50                .addr = NULL, /* patched for every instance */
    4551                .dstarg = 1
    4652        },
    4753        {
    4854                .cmd = CMD_PIO_WRITE_16,
    49                 .addr = (void*)0xc022,
     55                .addr = NULL, /* pathed for every instance */
    5056                .value = 0x1f
    5157        },
     
    5561};
    5662
     63static int usb_iface_get_address(ddf_fun_t *fun, devman_handle_t handle,
     64    usb_address_t *address)
     65{
     66        assert(fun);
     67        uhci_t *hc = fun_to_uhci(fun);
     68        assert(hc);
     69
     70        usb_address_t addr = device_keeper_find(&hc->device_manager,
     71            handle);
     72        if (addr < 0) {
     73                return addr;
     74        }
     75
     76        if (address != NULL) {
     77                *address = addr;
     78        }
     79
     80        return EOK;
     81}
     82/*----------------------------------------------------------------------------*/
     83static usb_iface_t hc_usb_iface = {
     84        .get_hc_handle = usb_iface_get_hc_handle_hc_impl,
     85        .get_address = usb_iface_get_address
     86};
     87/*----------------------------------------------------------------------------*/
     88static ddf_dev_ops_t uhci_ops = {
     89        .interfaces[USB_DEV_IFACE] = &hc_usb_iface,
     90        .interfaces[USBHC_DEV_IFACE] = &uhci_iface,
     91};
     92
    5793static int uhci_init_transfer_lists(uhci_t *instance);
    5894static int uhci_init_mem_structures(uhci_t *instance);
     
    65101        bool low_speed, usb_transfer_type_t, size_t size);
    66102
    67 #define CHECK_RET_RETURN(ret, message...) \
     103
     104int uhci_init(uhci_t *instance, ddf_dev_t *dev, void *regs, size_t reg_size)
     105{
     106        assert(reg_size >= sizeof(regs_t));
     107        int ret;
     108
     109#define CHECK_RET_DEST_FUN_RETURN(ret, message...) \
    68110        if (ret != EOK) { \
    69111                usb_log_error(message); \
     112                if (instance->ddf_instance) \
     113                        ddf_fun_destroy(instance->ddf_instance); \
    70114                return ret; \
    71115        } else (void) 0
    72116
    73 int uhci_init(uhci_t *instance, void *regs, size_t reg_size)
    74 {
    75         assert(reg_size >= sizeof(regs_t));
     117        /*
     118         * Create UHCI function.
     119         */
     120        instance->ddf_instance = ddf_fun_create(dev, fun_exposed, "uhci");
     121        ret = (instance->ddf_instance == NULL) ? ENOMEM : EOK;
     122        CHECK_RET_DEST_FUN_RETURN(ret, "Failed to create UHCI device function.\n");
     123
     124        instance->ddf_instance->ops = &uhci_ops;
     125        instance->ddf_instance->driver_data = instance;
     126
     127        ret = ddf_fun_bind(instance->ddf_instance);
     128        CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to bind UHCI device function: %s.\n",
     129            ret, str_error(ret));
    76130
    77131        /* allow access to hc control registers */
    78132        regs_t *io;
    79         int ret = pio_enable(regs, reg_size, (void**)&io);
    80         CHECK_RET_RETURN(ret, "Failed to gain access to registers at %p.\n", io);
     133        ret = pio_enable(regs, reg_size, (void**)&io);
     134        CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to gain access to registers at %p: %s.\n",
     135            ret, str_error(ret), io);
    81136        instance->registers = io;
    82         usb_log_debug("Device registers accessible.\n");
     137        usb_log_debug("Device registers at %p(%u) accessible.\n", io, reg_size);
    83138
    84139        ret = uhci_init_mem_structures(instance);
    85         CHECK_RET_RETURN(ret, "Failed to initialize memory structures.\n");
     140        CHECK_RET_DEST_FUN_RETURN(ret, "Failed to initialize UHCI memory structures.\n");
    86141
    87142        uhci_init_hw(instance);
    88143
    89144        instance->cleaner = fibril_create(uhci_interrupt_emulator, instance);
    90 //      fibril_add_ready(instance->cleaner);
     145        fibril_add_ready(instance->cleaner);
    91146
    92147        instance->debug_checker = fibril_create(uhci_debug_checker, instance);
    93148        fibril_add_ready(instance->debug_checker);
    94149
    95         return EOK;
     150        usb_log_info("Started UHCI driver.\n");
     151        return EOK;
     152#undef CHECK_RET_DEST_FUN_RETURN
    96153}
    97154/*----------------------------------------------------------------------------*/
    98155void uhci_init_hw(uhci_t *instance)
    99156{
     157        /* reset everything, who knows what touched it before us */
     158        pio_write_16(&instance->registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
     159        async_usleep(10000); /* 10ms according to USB spec */
     160        pio_write_16(&instance->registers->usbcmd, 0);
     161
     162        /* reset hc, all states and counters */
     163        pio_write_16(&instance->registers->usbcmd, UHCI_CMD_HCRESET);
     164        while ((pio_read_16(&instance->registers->usbcmd) & UHCI_CMD_HCRESET) != 0)
     165                { async_usleep(10); }
    100166
    101167        /* set framelist pointer */
     
    110176        pio_write_16(&instance->registers->usbcmd,
    111177            UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
    112         usb_log_debug("Started UHCI HC.\n");
    113178}
    114179/*----------------------------------------------------------------------------*/
     
    116181{
    117182        assert(instance);
     183#define CHECK_RET_DEST_CMDS_RETURN(ret, message...) \
     184        if (ret != EOK) { \
     185                usb_log_error(message); \
     186                if (instance->interrupt_code.cmds != NULL) \
     187                        free(instance->interrupt_code.cmds); \
     188                return ret; \
     189        } else (void) 0
    118190
    119191        /* init interrupt code */
    120         irq_cmd_t *interrupt_commands = malloc(sizeof(uhci_cmds));
    121         if (interrupt_commands == NULL) {
    122                 return ENOMEM;
    123         }
    124         memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds));
    125         interrupt_commands[0].addr = (void*)&instance->registers->usbsts;
    126         interrupt_commands[1].addr = (void*)&instance->registers->usbsts;
    127         instance->interrupt_code.cmds = interrupt_commands;
    128         instance->interrupt_code.cmdcount =
    129             sizeof(uhci_cmds) / sizeof(irq_cmd_t);
     192        instance->interrupt_code.cmds = malloc(sizeof(uhci_cmds));
     193        int ret = (instance->interrupt_code.cmds == NULL) ? ENOMEM : EOK;
     194        CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to allocate interrupt cmds space.\n");
     195
     196        {
     197                irq_cmd_t *interrupt_commands = instance->interrupt_code.cmds;
     198                memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds));
     199                interrupt_commands[0].addr = (void*)&instance->registers->usbsts;
     200                interrupt_commands[1].addr = (void*)&instance->registers->usbsts;
     201                instance->interrupt_code.cmdcount =
     202                                sizeof(uhci_cmds) / sizeof(irq_cmd_t);
     203        }
    130204
    131205        /* init transfer lists */
    132         int ret = uhci_init_transfer_lists(instance);
    133         CHECK_RET_RETURN(ret, "Failed to initialize transfer lists.\n");
     206        ret = uhci_init_transfer_lists(instance);
     207        CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to initialize transfer lists.\n");
    134208        usb_log_debug("Initialized transfer lists.\n");
    135209
     
    137211        instance->frame_list = get_page();
    138212        ret = instance ? EOK : ENOMEM;
    139         CHECK_RET_RETURN(ret, "Failed to get frame list page.\n");
     213        CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to get frame list page.\n");
    140214        usb_log_debug("Initialized frame list.\n");
    141215
     
    144218          instance->transfers_interrupt.queue_head_pa
    145219          | LINK_POINTER_QUEUE_HEAD_FLAG;
     220
    146221        unsigned i = 0;
    147222        for(; i < UHCI_FRAME_LIST_COUNT; ++i) {
     
    150225
    151226        /* init address keeper(libusb) */
    152         usb_address_keeping_init(&instance->address_manager, USB11_ADDRESS_MAX);
    153         usb_log_debug("Initialized address manager.\n");
    154 
    155         return EOK;
     227        device_keeper_init(&instance->device_manager);
     228        usb_log_debug("Initialized device manager.\n");
     229
     230        return EOK;
     231#undef CHECK_RET_DEST_CMDS_RETURN
    156232}
    157233/*----------------------------------------------------------------------------*/
     
    159235{
    160236        assert(instance);
     237#define CHECK_RET_CLEAR_RETURN(ret, message...) \
     238        if (ret != EOK) { \
     239                usb_log_error(message); \
     240                transfer_list_fini(&instance->transfers_bulk_full); \
     241                transfer_list_fini(&instance->transfers_control_full); \
     242                transfer_list_fini(&instance->transfers_control_slow); \
     243                transfer_list_fini(&instance->transfers_interrupt); \
     244                return ret; \
     245        } else (void) 0
    161246
    162247        /* initialize TODO: check errors */
    163248        int ret;
    164249        ret = transfer_list_init(&instance->transfers_bulk_full, "BULK_FULL");
    165         assert(ret == EOK);
     250        CHECK_RET_CLEAR_RETURN(ret, "Failed to init BULK list.");
    166251        ret = transfer_list_init(&instance->transfers_control_full, "CONTROL_FULL");
    167         assert(ret == EOK);
     252        CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL FULL list.");
    168253        ret = transfer_list_init(&instance->transfers_control_slow, "CONTROL_SLOW");
    169         assert(ret == EOK);
     254        CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL SLOW list.");
    170255        ret = transfer_list_init(&instance->transfers_interrupt, "INTERRUPT");
    171         assert(ret == EOK);
     256        CHECK_RET_CLEAR_RETURN(ret, "Failed to init INTERRUPT list.");
    172257
    173258        transfer_list_set_next(&instance->transfers_control_full,
     
    196281
    197282        return EOK;
     283#undef CHECK_RET_CLEAR_RETURN
    198284}
    199285/*----------------------------------------------------------------------------*/
     
    202288        assert(instance);
    203289        assert(batch);
    204         const int low_speed = (batch->speed == LOW_SPEED);
     290        const int low_speed = (batch->speed == USB_SPEED_LOW);
    205291        if (!allowed_usb_packet(
    206292            low_speed, batch->transfer_type, batch->max_packet_size)) {
     
    223309{
    224310        assert(instance);
    225         if ((status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) == 0)
    226                 return;
    227         usb_log_debug("UHCI interrupt: %X.\n", status);
    228         transfer_list_check(&instance->transfers_interrupt);
    229         transfer_list_check(&instance->transfers_control_slow);
    230         transfer_list_check(&instance->transfers_control_full);
    231         transfer_list_check(&instance->transfers_bulk_full);
     311//      if ((status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) == 0)
     312//              return;
     313//      usb_log_debug2("UHCI interrupt: %X.\n", status);
     314        transfer_list_remove_finished(&instance->transfers_interrupt);
     315        transfer_list_remove_finished(&instance->transfers_control_slow);
     316        transfer_list_remove_finished(&instance->transfers_control_full);
     317        transfer_list_remove_finished(&instance->transfers_bulk_full);
    232318}
    233319/*----------------------------------------------------------------------------*/
     
    238324        assert(instance);
    239325
    240         while(1) {
     326        while (1) {
    241327                uint16_t status = pio_read_16(&instance->registers->usbsts);
     328                if (status != 0)
     329                        usb_log_debug2("UHCI status: %x.\n", status);
     330                status |= 1;
    242331                uhci_interrupt(instance, status);
    243                 async_usleep(UHCI_CLEANER_TIMEOUT);
     332                pio_write_16(&instance->registers->usbsts, 0x1f);
     333                async_usleep(UHCI_CLEANER_TIMEOUT * 5);
    244334        }
    245335        return EOK;
     
    254344                const uint16_t sts = pio_read_16(&instance->registers->usbsts);
    255345                const uint16_t intr = pio_read_16(&instance->registers->usbintr);
    256                 usb_log_debug("Command: %X Status: %X Interrupts: %x\n",
    257                     cmd, sts, intr);
    258 
    259                 uintptr_t frame_list = pio_read_32(&instance->registers->flbaseadd);
     346                if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
     347                        usb_log_debug2("Command: %X Status: %X Intr: %x\n",
     348                            cmd, sts, intr);
     349                }
     350
     351                uintptr_t frame_list =
     352                    pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
    260353                if (frame_list != addr_to_phys(instance->frame_list)) {
    261354                        usb_log_debug("Framelist address: %p vs. %p.\n",
Note: See TracChangeset for help on using the changeset viewer.