Ignore:
File:
1 edited

Legend:

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

    rb9d910f r1ae51ae  
    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        },
     
    5359                .cmd = CMD_ACCEPT
    5460        }
     61};
     62
     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,
    5591};
    5692
     
    71107        } else (void) 0
    72108
    73 int uhci_init(uhci_t *instance, void *regs, size_t reg_size)
     109int uhci_init(uhci_t *instance, ddf_dev_t *dev, void *regs, size_t reg_size)
    74110{
    75111        assert(reg_size >= sizeof(regs_t));
     112        int ret;
     113
     114        /*
     115         * Create UHCI function.
     116         */
     117        instance->ddf_instance = ddf_fun_create(dev, fun_exposed, "uhci");
     118        if (instance->ddf_instance == NULL) {
     119                usb_log_error("Failed to create UHCI device function.\n");
     120                return ENOMEM;
     121        }
     122        instance->ddf_instance->ops = &uhci_ops;
     123        instance->ddf_instance->driver_data = instance;
     124
     125        ret = ddf_fun_bind(instance->ddf_instance);
     126        CHECK_RET_RETURN(ret, "Failed to bind UHCI device function: %s.\n",
     127            str_error(ret));
    76128
    77129        /* allow access to hc control registers */
    78130        regs_t *io;
    79         int ret = pio_enable(regs, reg_size, (void**)&io);
     131        ret = pio_enable(regs, reg_size, (void**)&io);
    80132        CHECK_RET_RETURN(ret, "Failed to gain access to registers at %p.\n", io);
    81133        instance->registers = io;
     
    88140
    89141        instance->cleaner = fibril_create(uhci_interrupt_emulator, instance);
    90 //      fibril_add_ready(instance->cleaner);
     142        fibril_add_ready(instance->cleaner);
    91143
    92144        instance->debug_checker = fibril_create(uhci_debug_checker, instance);
     
    98150void uhci_init_hw(uhci_t *instance)
    99151{
     152        /* reset everything, who knows what touched it before us */
     153        pio_write_16(&instance->registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
     154        async_usleep(10000); /* 10ms according to USB spec */
     155        pio_write_16(&instance->registers->usbcmd, 0);
     156
     157        /* reset hc, all states and counters */
     158        pio_write_16(&instance->registers->usbcmd, UHCI_CMD_HCRESET);
     159        while ((pio_read_16(&instance->registers->usbcmd) & UHCI_CMD_HCRESET) != 0)
     160                { async_usleep(10); }
    100161
    101162        /* set framelist pointer */
     
    150211
    151212        /* init address keeper(libusb) */
    152         usb_address_keeping_init(&instance->address_manager, USB11_ADDRESS_MAX);
    153         usb_log_debug("Initialized address manager.\n");
     213        device_keeper_init(&instance->device_manager);
     214        usb_log_debug("Initialized device manager.\n");
    154215
    155216        return EOK;
     
    202263        assert(instance);
    203264        assert(batch);
    204         const int low_speed = (batch->speed == LOW_SPEED);
     265        const int low_speed = (batch->speed == USB_SPEED_LOW);
    205266        if (!allowed_usb_packet(
    206267            low_speed, batch->transfer_type, batch->max_packet_size)) {
     
    223284{
    224285        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);
     286//      if ((status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) == 0)
     287//              return;
     288//      usb_log_debug2("UHCI interrupt: %X.\n", status);
     289        transfer_list_remove_finished(&instance->transfers_interrupt);
     290        transfer_list_remove_finished(&instance->transfers_control_slow);
     291        transfer_list_remove_finished(&instance->transfers_control_full);
     292        transfer_list_remove_finished(&instance->transfers_bulk_full);
    232293}
    233294/*----------------------------------------------------------------------------*/
     
    238299        assert(instance);
    239300
    240         while(1) {
     301        while (1) {
    241302                uint16_t status = pio_read_16(&instance->registers->usbsts);
     303                if (status != 0)
     304                        usb_log_debug2("UHCI status: %x.\n", status);
     305                status |= 1;
    242306                uhci_interrupt(instance, status);
    243                 async_usleep(UHCI_CLEANER_TIMEOUT);
     307                pio_write_16(&instance->registers->usbsts, 0x1f);
     308                async_usleep(UHCI_CLEANER_TIMEOUT * 5);
    244309        }
    245310        return EOK;
     
    254319                const uint16_t sts = pio_read_16(&instance->registers->usbsts);
    255320                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);
     321                if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
     322                        usb_log_debug2("Command: %X Status: %X Intr: %x\n",
     323                            cmd, sts, intr);
     324                }
     325
     326                uintptr_t frame_list =
     327                    pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
    260328                if (frame_list != addr_to_phys(instance->frame_list)) {
    261329                        usb_log_debug("Framelist address: %p vs. %p.\n",
Note: See TracChangeset for help on using the changeset viewer.