Ignore:
File:
1 edited

Legend:

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

    ra7e2f0d r4abc304  
    6161};
    6262
    63 /** Gets USB address of the calling device.
    64  *
    65  * @param[in] fun UHCI hc function.
    66  * @param[in] handle Handle of the device seeking address.
    67  * @param[out] address Place to store found address.
    68  * @return Error code.
    69  */
    70 static int usb_iface_get_address(
    71     ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address)
     63static int usb_iface_get_address(ddf_fun_t *fun, devman_handle_t handle,
     64    usb_address_t *address)
    7265{
    7366        assert(fun);
     
    9790        .interfaces[USBHC_DEV_IFACE] = &uhci_iface,
    9891};
    99 /*----------------------------------------------------------------------------*/
     92
    10093static int uhci_init_transfer_lists(uhci_t *instance);
    10194static int uhci_init_mem_structures(uhci_t *instance);
     
    10699
    107100static bool allowed_usb_packet(
    108     bool low_speed, usb_transfer_type_t transfer, size_t size);
    109 /*----------------------------------------------------------------------------*/
    110 /** Initializes UHCI hcd driver structure
    111  *
    112  * @param[in] instance Memory place to initialize.
    113  * @param[in] dev DDF device.
    114  * @param[in] regs Address of I/O control registers.
    115  * @param[in] size Size of I/O control registers.
    116  * @return Error code.
    117  * @note Should be called only once on any structure.
    118  */
     101        bool low_speed, usb_transfer_type_t, size_t size);
     102
     103
    119104int uhci_init(uhci_t *instance, ddf_dev_t *dev, void *regs, size_t reg_size)
    120105{
     
    130115        } else (void) 0
    131116
    132         /* Create UHCI function. */
     117        /*
     118         * Create UHCI function.
     119         */
    133120        instance->ddf_instance = ddf_fun_create(dev, fun_exposed, "uhci");
    134121        ret = (instance->ddf_instance == NULL) ? ENOMEM : EOK;
    135         CHECK_RET_DEST_FUN_RETURN(ret,
    136             "Failed to create UHCI device function.\n");
     122        CHECK_RET_DEST_FUN_RETURN(ret, "Failed to create UHCI device function.\n");
    137123
    138124        instance->ddf_instance->ops = &uhci_ops;
     
    140126
    141127        ret = ddf_fun_bind(instance->ddf_instance);
    142         CHECK_RET_DEST_FUN_RETURN(ret,
    143             "Failed(%d) to bind UHCI device function: %s.\n",
     128        CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to bind UHCI device function: %s.\n",
    144129            ret, str_error(ret));
    145130
     
    147132        regs_t *io;
    148133        ret = pio_enable(regs, reg_size, (void**)&io);
    149         CHECK_RET_DEST_FUN_RETURN(ret,
    150             "Failed(%d) to gain access to registers at %p: %s.\n",
     134        CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to gain access to registers at %p: %s.\n",
    151135            ret, str_error(ret), io);
    152136        instance->registers = io;
    153         usb_log_debug("Device registers at %p(%u) accessible.\n",
    154             io, reg_size);
     137        usb_log_debug("Device registers at %p(%u) accessible.\n", io, reg_size);
    155138
    156139        ret = uhci_init_mem_structures(instance);
    157         CHECK_RET_DEST_FUN_RETURN(ret,
    158             "Failed to initialize UHCI memory structures.\n");
     140        CHECK_RET_DEST_FUN_RETURN(ret, "Failed to initialize UHCI memory structures.\n");
    159141
    160142        uhci_init_hw(instance);
    161         instance->cleaner =
    162             fibril_create(uhci_interrupt_emulator, instance);
     143
     144        instance->cleaner = fibril_create(uhci_interrupt_emulator, instance);
    163145        fibril_add_ready(instance->cleaner);
    164146
     
    171153}
    172154/*----------------------------------------------------------------------------*/
    173 /** Initializes UHCI hcd hw resources.
    174  *
    175  * @param[in] instance UHCI structure to use.
    176  */
    177155void uhci_init_hw(uhci_t *instance)
    178156{
    179         assert(instance);
    180         regs_t *registers = instance->registers;
    181 
    182         /* Reset everything, who knows what touched it before us */
    183         pio_write_16(&registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
     157        /* reset everything, who knows what touched it before us */
     158        pio_write_16(&instance->registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
    184159        async_usleep(10000); /* 10ms according to USB spec */
    185         pio_write_16(&registers->usbcmd, 0);
    186 
    187         /* Reset hc, all states and counters */
    188         pio_write_16(&registers->usbcmd, UHCI_CMD_HCRESET);
    189         do { async_usleep(10); }
    190         while ((pio_read_16(&registers->usbcmd) & UHCI_CMD_HCRESET) != 0);
    191 
    192         /* Set framelist pointer */
     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); }
     166
     167        /* set framelist pointer */
    193168        const uint32_t pa = addr_to_phys(instance->frame_list);
    194         pio_write_32(&registers->flbaseadd, pa);
    195 
    196         /* Enable all interrupts, but resume interrupt */
    197 //      pio_write_16(&instance->registers->usbintr,
    198 //          UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET);
    199 
    200         uint16_t status = pio_read_16(&registers->usbcmd);
    201         if (status != 0)
    202                 usb_log_warning("Previous command value: %x.\n", status);
     169        pio_write_32(&instance->registers->flbaseadd, pa);
     170
     171        /* enable all interrupts, but resume interrupt */
     172        pio_write_16(&instance->registers->usbintr,
     173                  UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET);
    203174
    204175        /* Start the hc with large(64B) packet FSBR */
    205         pio_write_16(&registers->usbcmd,
     176        pio_write_16(&instance->registers->usbcmd,
    206177            UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
    207178}
    208179/*----------------------------------------------------------------------------*/
    209 /** Initializes UHCI hcd memory structures.
    210  *
    211  * @param[in] instance UHCI structure to use.
    212  * @return Error code
    213  * @note Should be called only once on any structure.
    214  */
    215180int uhci_init_mem_structures(uhci_t *instance)
    216181{
     
    224189        } else (void) 0
    225190
    226         /* Init interrupt code */
     191        /* init interrupt code */
    227192        instance->interrupt_code.cmds = malloc(sizeof(uhci_cmds));
    228193        int ret = (instance->interrupt_code.cmds == NULL) ? ENOMEM : EOK;
    229         CHECK_RET_DEST_CMDS_RETURN(ret,
    230             "Failed to allocate interrupt cmds space.\n");
     194        CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to allocate interrupt cmds space.\n");
    231195
    232196        {
    233197                irq_cmd_t *interrupt_commands = instance->interrupt_code.cmds;
    234198                memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds));
    235                 interrupt_commands[0].addr =
    236                     (void*)&instance->registers->usbsts;
    237                 interrupt_commands[1].addr =
    238                     (void*)&instance->registers->usbsts;
     199                interrupt_commands[0].addr = (void*)&instance->registers->usbsts;
     200                interrupt_commands[1].addr = (void*)&instance->registers->usbsts;
    239201                instance->interrupt_code.cmdcount =
    240                     sizeof(uhci_cmds) / sizeof(irq_cmd_t);
    241         }
    242 
    243         /* Init transfer lists */
     202                                sizeof(uhci_cmds) / sizeof(irq_cmd_t);
     203        }
     204
     205        /* init transfer lists */
    244206        ret = uhci_init_transfer_lists(instance);
    245         CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to init transfer lists.\n");
     207        CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to initialize transfer lists.\n");
    246208        usb_log_debug("Initialized transfer lists.\n");
    247209
    248         /* Init USB frame list page*/
     210        /* frame list initialization */
    249211        instance->frame_list = get_page();
    250212        ret = instance ? EOK : ENOMEM;
     
    252214        usb_log_debug("Initialized frame list.\n");
    253215
    254         /* Set all frames to point to the first queue head */
     216        /* initialize all frames to point to the first queue head */
    255217        const uint32_t queue =
    256218          instance->transfers_interrupt.queue_head_pa
     
    262224        }
    263225
    264         /* Init device keeper*/
     226        /* init address keeper(libusb) */
    265227        device_keeper_init(&instance->device_manager);
    266228        usb_log_debug("Initialized device manager.\n");
     
    270232}
    271233/*----------------------------------------------------------------------------*/
    272 /** Initializes UHCI hcd transfer lists.
    273  *
    274  * @param[in] instance UHCI structure to use.
    275  * @return Error code
    276  * @note Should be called only once on any structure.
    277  */
    278234int uhci_init_transfer_lists(uhci_t *instance)
    279235{
     
    293249        ret = transfer_list_init(&instance->transfers_bulk_full, "BULK_FULL");
    294250        CHECK_RET_CLEAR_RETURN(ret, "Failed to init BULK list.");
    295 
    296         ret = transfer_list_init(
    297             &instance->transfers_control_full, "CONTROL_FULL");
     251        ret = transfer_list_init(&instance->transfers_control_full, "CONTROL_FULL");
    298252        CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL FULL list.");
    299 
    300         ret = transfer_list_init(
    301             &instance->transfers_control_slow, "CONTROL_SLOW");
     253        ret = transfer_list_init(&instance->transfers_control_slow, "CONTROL_SLOW");
    302254        CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL SLOW list.");
    303 
    304255        ret = transfer_list_init(&instance->transfers_interrupt, "INTERRUPT");
    305256        CHECK_RET_CLEAR_RETURN(ret, "Failed to init INTERRUPT list.");
     
    318269#endif
    319270
    320         /* Assign pointers to be used during scheduling */
    321         instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
     271        instance->transfers[0][USB_TRANSFER_INTERRUPT] =
    322272          &instance->transfers_interrupt;
    323         instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
     273        instance->transfers[1][USB_TRANSFER_INTERRUPT] =
    324274          &instance->transfers_interrupt;
    325         instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
     275        instance->transfers[0][USB_TRANSFER_CONTROL] =
    326276          &instance->transfers_control_full;
    327         instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
     277        instance->transfers[1][USB_TRANSFER_CONTROL] =
    328278          &instance->transfers_control_slow;
    329         instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
     279        instance->transfers[0][USB_TRANSFER_BULK] =
    330280          &instance->transfers_bulk_full;
    331281
     
    334284}
    335285/*----------------------------------------------------------------------------*/
    336 /** Schedules batch for execution.
    337  *
    338  * @param[in] instance UHCI structure to use.
    339  * @param[in] batch Transfer batch to schedule.
    340  * @return Error code
    341  */
    342286int uhci_schedule(uhci_t *instance, batch_t *batch)
    343287{
     
    347291        if (!allowed_usb_packet(
    348292            low_speed, batch->transfer_type, batch->max_packet_size)) {
    349                 usb_log_warning(
    350                     "Invalid USB packet specified %s SPEED %d %zu.\n",
    351                     low_speed ? "LOW" : "FULL" , batch->transfer_type,
     293                usb_log_warning("Invalid USB packet specified %s SPEED %d %zu.\n",
     294                          low_speed ? "LOW" : "FULL" , batch->transfer_type,
    352295                    batch->max_packet_size);
    353296                return ENOTSUP;
     
    363306}
    364307/*----------------------------------------------------------------------------*/
    365 /** Takes action based on the interrupt cause.
    366  *
    367  * @param[in] instance UHCI structure to use.
    368  * @param[in] status Value of the stsatus regiser at the time of interrupt.
    369  */
    370308void uhci_interrupt(uhci_t *instance, uint16_t status)
    371309{
    372310        assert(instance);
    373         /* TODO: Check interrupt cause here */
     311//      if ((status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) == 0)
     312//              return;
     313//      usb_log_debug2("UHCI interrupt: %X.\n", status);
    374314        transfer_list_remove_finished(&instance->transfers_interrupt);
    375315        transfer_list_remove_finished(&instance->transfers_control_slow);
     
    378318}
    379319/*----------------------------------------------------------------------------*/
    380 /** Polling function, emulates interrupts.
    381  *
    382  * @param[in] arg UHCI structure to use.
    383  * @return EOK
    384  */
    385320int uhci_interrupt_emulator(void* arg)
    386321{
     
    396331                uhci_interrupt(instance, status);
    397332                pio_write_16(&instance->registers->usbsts, 0x1f);
    398                 async_usleep(UHCI_CLEANER_TIMEOUT);
     333                async_usleep(UHCI_CLEANER_TIMEOUT * 5);
    399334        }
    400335        return EOK;
    401336}
    402337/*---------------------------------------------------------------------------*/
    403 /** Debug function, checks consistency of memory structures.
    404  *
    405  * @param[in] arg UHCI structure to use.
    406  * @return EOK
    407  */
    408338int uhci_debug_checker(void *arg)
    409339{
    410340        uhci_t *instance = (uhci_t*)arg;
    411341        assert(instance);
    412 
    413 #define QH(queue) \
    414         instance->transfers_##queue.queue_head
    415 
    416342        while (1) {
    417343                const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
    418344                const uint16_t sts = pio_read_16(&instance->registers->usbsts);
    419                 const uint16_t intr =
    420                     pio_read_16(&instance->registers->usbintr);
    421 
     345                const uint16_t intr = pio_read_16(&instance->registers->usbintr);
    422346                if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
    423347                        usb_log_debug2("Command: %X Status: %X Intr: %x\n",
     
    429353                if (frame_list != addr_to_phys(instance->frame_list)) {
    430354                        usb_log_debug("Framelist address: %p vs. %p.\n",
    431                             frame_list, addr_to_phys(instance->frame_list));
    432                 }
    433 
     355                                frame_list, addr_to_phys(instance->frame_list));
     356                }
    434357                int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
    435358                usb_log_debug2("Framelist item: %d \n", frnum );
    436359
    437                 uintptr_t expected_pa = instance->frame_list[frnum] & (~0xf);
    438                 uintptr_t real_pa = addr_to_phys(QH(interrupt));
    439                 if (expected_pa != real_pa) {
     360                queue_head_t* qh = instance->transfers_interrupt.queue_head;
     361
     362                if ((instance->frame_list[frnum] & (~0xf)) != (uintptr_t)addr_to_phys(qh)) {
    440363                        usb_log_debug("Interrupt QH: %p vs. %p.\n",
    441                             expected_pa, real_pa);
    442                 }
    443 
    444                 expected_pa = QH(interrupt)->next_queue & (~0xf);
    445                 real_pa = addr_to_phys(QH(control_slow));
    446                 if (expected_pa != real_pa) {
    447                         usb_log_debug("Control Slow QH: %p vs. %p.\n",
    448                             expected_pa, real_pa);
    449                 }
    450 
    451                 expected_pa = QH(control_slow)->next_queue & (~0xf);
    452                 real_pa = addr_to_phys(QH(control_full));
    453                 if (expected_pa != real_pa) {
    454                         usb_log_debug("Control Full QH: %p vs. %p.\n",
    455                             expected_pa, real_pa);
    456                 }
    457 
    458                 expected_pa = QH(control_full)->next_queue & (~0xf);
    459                 real_pa = addr_to_phys(QH(bulk_full));
    460                 if (expected_pa != real_pa ) {
    461                         usb_log_debug("Bulk QH: %p vs. %p.\n",
    462                             expected_pa, real_pa);
    463                 }
     364                                instance->frame_list[frnum] & (~0xf), addr_to_phys(qh));
     365                }
     366
     367                if ((qh->next_queue & (~0xf))
     368                  != (uintptr_t)addr_to_phys(instance->transfers_control_slow.queue_head)) {
     369                        usb_log_debug("Control Slow QH: %p vs. %p.\n", qh->next_queue & (~0xf),
     370                                addr_to_phys(instance->transfers_control_slow.queue_head));
     371                }
     372                qh = instance->transfers_control_slow.queue_head;
     373
     374                if ((qh->next_queue & (~0xf))
     375                  != (uintptr_t)addr_to_phys(instance->transfers_control_full.queue_head)) {
     376                        usb_log_debug("Control Full QH: %p vs. %p.\n", qh->next_queue & (~0xf),
     377                                addr_to_phys(instance->transfers_control_full.queue_head));\
     378                }
     379                qh = instance->transfers_control_full.queue_head;
     380
     381                if ((qh->next_queue & (~0xf))
     382                  != (uintptr_t)addr_to_phys(instance->transfers_bulk_full.queue_head)) {
     383                        usb_log_debug("Bulk QH: %p vs. %p.\n", qh->next_queue & (~0xf),
     384                                addr_to_phys(instance->transfers_bulk_full.queue_head));
     385                }
     386/*
     387        uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
     388        cmd |= UHCI_CMD_RUN_STOP;
     389        pio_write_16(&instance->registers->usbcmd, cmd);
     390*/
    464391                async_usleep(UHCI_DEBUGER_TIMEOUT);
    465392        }
    466         return EOK;
    467 #undef QH
    468 }
    469 /*----------------------------------------------------------------------------*/
    470 /** Checks transfer packets, for USB validity
    471  *
    472  * @param[in] low_speed Transfer speed.
    473  * @param[in] transfer Transer type
    474  * @param[in] size Maximum size of used packets
    475  * @return EOK
    476  */
     393        return 0;
     394}
     395/*----------------------------------------------------------------------------*/
    477396bool allowed_usb_packet(
    478     bool low_speed, usb_transfer_type_t transfer, size_t size)
     397        bool low_speed, usb_transfer_type_t transfer, size_t size)
    479398{
    480399        /* see USB specification chapter 5.5-5.8 for magic numbers used here */
    481         switch(transfer)
    482         {
    483         case USB_TRANSFER_ISOCHRONOUS:
    484                 return (!low_speed && size < 1024);
    485         case USB_TRANSFER_INTERRUPT:
    486                 return size <= (low_speed ? 8 : 64);
    487         case USB_TRANSFER_CONTROL: /* device specifies its own max size */
    488                 return (size <= (low_speed ? 8 : 64));
    489         case USB_TRANSFER_BULK: /* device specifies its own max size */
    490                 return (!low_speed && size <= 64);
     400        switch(transfer) {
     401                case USB_TRANSFER_ISOCHRONOUS:
     402                        return (!low_speed && size < 1024);
     403                case USB_TRANSFER_INTERRUPT:
     404                        return size <= (low_speed ? 8 : 64);
     405                case USB_TRANSFER_CONTROL: /* device specifies its own max size */
     406                        return (size <= (low_speed ? 8 : 64));
     407                case USB_TRANSFER_BULK: /* device specifies its own max size */
     408                        return (!low_speed && size <= 64);
    491409        }
    492410        return false;
Note: See TracChangeset for help on using the changeset viewer.