Ignore:
File:
1 edited

Legend:

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

    r8e1eb4d0 ra7e2f0d  
    3434#include <ddf/driver.h>
    3535#include <ddf/interrupt.h>
     36#include <device/hw_res.h>
     37#include <errno.h>
     38#include <str_error.h>
     39
    3640#include <usb_iface.h>
    3741#include <usb/ddfiface.h>
    38 #include <device/hw_res.h>
    39 
    40 #include <errno.h>
    41 
    4242#include <usb/debug.h>
    4343
     
    5050
    5151static int uhci_add_device(ddf_dev_t *device);
    52 
    5352/*----------------------------------------------------------------------------*/
    5453static driver_ops_t uhci_driver_ops = {
     
    6160};
    6261/*----------------------------------------------------------------------------*/
     62/** IRQ handling callback, identifies devic
     63 *
     64 * @param[in] dev DDF instance of the device to use.
     65 * @param[in] iid (Unused).
     66 * @param[in] call Pointer to the call that represents interrupt.
     67 */
    6368static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
    6469{
     
    7075}
    7176/*----------------------------------------------------------------------------*/
    72 #define CHECK_RET_RETURN(ret, message...) \
     77/** Initializes a new ddf driver instance of UHCI hcd.
     78 *
     79 * @param[in] device DDF instance of the device to initialize.
     80 * @return Error code.
     81 *
     82 * Gets and initialies hardware resources, disables any legacy support,
     83 * and reports root hub device.
     84 */
     85int uhci_add_device(ddf_dev_t *device)
     86{
     87        assert(device);
     88        uhci_t *hcd = NULL;
     89#define CHECK_RET_FREE_HC_RETURN(ret, message...) \
    7390if (ret != EOK) { \
    7491        usb_log_error(message); \
     92        if (hcd != NULL) \
     93                free(hcd); \
    7594        return ret; \
    7695}
    77 
    78 static int uhci_add_device(ddf_dev_t *device)
    79 {
    80         assert(device);
    8196
    8297        usb_log_info("uhci_add_device() called\n");
     
    88103        int ret =
    89104            pci_get_my_registers(device, &io_reg_base, &io_reg_size, &irq);
    90 
    91         CHECK_RET_RETURN(ret,
     105        CHECK_RET_FREE_HC_RETURN(ret,
    92106            "Failed(%d) to get I/O addresses:.\n", ret, device->handle);
    93107        usb_log_info("I/O regs at 0x%X (size %zu), IRQ %d.\n",
    94108            io_reg_base, io_reg_size, irq);
    95109
    96 //      ret = pci_enable_interrupts(device);
    97 //      CHECK_RET_RETURN(ret, "Failed(%d) to get enable interrupts:\n", ret);
     110        ret = pci_disable_legacy(device);
     111        CHECK_RET_FREE_HC_RETURN(ret,
     112            "Failed(%d) to disable legacy USB: %s.\n", ret, str_error(ret));
    98113
    99         uhci_t *uhci_hc = malloc(sizeof(uhci_t));
    100         ret = (uhci_hc != NULL) ? EOK : ENOMEM;
    101         CHECK_RET_RETURN(ret, "Failed to allocate memory for uhci hcd driver.\n");
     114#if 0
     115        ret = pci_enable_interrupts(device);
     116        if (ret != EOK) {
     117                usb_log_warning(
     118                    "Failed(%d) to enable interrupts, fall back to polling.\n",
     119                    ret);
     120        }
     121#endif
    102122
    103         ret = uhci_init(uhci_hc, device, (void*)io_reg_base, io_reg_size);
    104         if (ret != EOK) {
    105                 usb_log_error("Failed to init uhci-hcd.\n");
    106                 free(uhci_hc);
    107                 return ret;
    108         }
     123        hcd = malloc(sizeof(uhci_t));
     124        ret = (hcd != NULL) ? EOK : ENOMEM;
     125        CHECK_RET_FREE_HC_RETURN(ret,
     126            "Failed(%d) to allocate memory for uhci hcd.\n", ret);
     127
     128        ret = uhci_init(hcd, device, (void*)io_reg_base, io_reg_size);
     129        CHECK_RET_FREE_HC_RETURN(ret, "Failed(%d) to init uhci-hcd.\n", ret);
     130#undef CHECK_RET_FREE_HC_RETURN
    109131
    110132        /*
    111          * We might free uhci_hc, but that does not matter since no one
     133         * We might free hcd, but that does not matter since no one
    112134         * else would access driver_data anyway.
    113135         */
    114         device->driver_data = uhci_hc;
     136        device->driver_data = hcd;
     137
     138        ddf_fun_t *rh = NULL;
     139#define CHECK_RET_FINI_FREE_RETURN(ret, message...) \
     140if (ret != EOK) { \
     141        usb_log_error(message); \
     142        if (hcd != NULL) {\
     143                uhci_fini(hcd); \
     144                free(hcd); \
     145        } \
     146        if (rh != NULL) \
     147                free(rh); \
     148        return ret; \
     149}
     150
     151        /* It does no harm if we register this on polling */
    115152        ret = register_interrupt_handler(device, irq, irq_handler,
    116             &uhci_hc->interrupt_code);
    117         if (ret != EOK) {
    118                 usb_log_error("Failed to register interrupt handler.\n");
    119                 uhci_fini(uhci_hc);
    120                 free(uhci_hc);
    121                 return ret;
    122         }
     153            &hcd->interrupt_code);
     154        CHECK_RET_FINI_FREE_RETURN(ret,
     155            "Failed(%d) to register interrupt handler.\n", ret);
    123156
    124         ddf_fun_t *rh;
    125157        ret = setup_root_hub(&rh, device);
    126         if (ret != EOK) {
    127                 usb_log_error("Failed to setup uhci root hub.\n");
    128                 uhci_fini(uhci_hc);
    129                 free(uhci_hc);
    130                 return ret;
    131         }
    132         rh->driver_data = uhci_hc->ddf_instance;
     158        CHECK_RET_FINI_FREE_RETURN(ret,
     159            "Failed(%d) to setup UHCI root hub.\n", ret);
     160        rh->driver_data = hcd->ddf_instance;
    133161
    134162        ret = ddf_fun_bind(rh);
    135         if (ret != EOK) {
    136                 usb_log_error("Failed to register root hub.\n");
    137                 uhci_fini(uhci_hc);
    138                 free(uhci_hc);
    139                 free(rh);
    140                 return ret;
    141         }
     163        CHECK_RET_FINI_FREE_RETURN(ret,
     164            "Failed(%d) to register UHCI root hub.\n", ret);
    142165
    143166        return EOK;
     167#undef CHECK_RET_FINI_FREE_RETURN
    144168}
    145169/*----------------------------------------------------------------------------*/
     170/** Initializes global driver structures (NONE).
     171 *
     172 * @param[in] argc Nmber of arguments in argv vector (ignored).
     173 * @param[in] argv Cmdline argument vector (ignored).
     174 * @return Error code.
     175 *
     176 * Driver debug level is set here.
     177 */
    146178int main(int argc, char *argv[])
    147179{
Note: See TracChangeset for help on using the changeset viewer.