Changeset 86341e2 in mainline


Ignore:
Timestamp:
2011-02-28T17:11:27Z (13 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d79a101f
Parents:
dced52a
Message:

Refactoring

File:
1 edited

Legend:

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

    rdced52a r86341e2  
    7070}
    7171/*----------------------------------------------------------------------------*/
    72 #define CHECK_RET_RETURN(ret, message...) \
    73 if (ret != EOK) { \
    74         usb_log_error(message); \
    75         return ret; \
    76 }
    77 
    7872static int uhci_add_device(ddf_dev_t *device)
    7973{
    8074        assert(device);
     75        uhci_t *hcd = NULL;
     76#define CHECK_RET_FREE_HC_RETURN(ret, message...) \
     77if (ret != EOK) { \
     78        usb_log_error(message); \
     79        if (hcd != NULL) \
     80                free(hcd); \
     81        return ret; \
     82}
    8183
    8284        usb_log_info("uhci_add_device() called\n");
     
    8890        int ret =
    8991            pci_get_my_registers(device, &io_reg_base, &io_reg_size, &irq);
    90 
    91         CHECK_RET_RETURN(ret,
     92        CHECK_RET_FREE_HC_RETURN(ret,
    9293            "Failed(%d) to get I/O addresses:.\n", ret, device->handle);
    9394        usb_log_info("I/O regs at 0x%X (size %zu), IRQ %d.\n",
     
    9596
    9697//      ret = pci_enable_interrupts(device);
    97 //      CHECK_RET_RETURN(ret, "Failed(%d) to get enable interrupts:\n", ret);
     98//      CHECK_RET_FREE_HC_RETURN(ret, "Failed(%d) to get enable interrupts:\n", ret);
    9899
    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");
     100        hcd = malloc(sizeof(uhci_t));
     101        ret = (hcd != NULL) ? EOK : ENOMEM;
     102        CHECK_RET_FREE_HC_RETURN(ret, "Failed(%d) to allocate memory for uhci hcd.\n",
     103            ret);
    102104
    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         }
     105        ret = uhci_init(hcd, device, (void*)io_reg_base, io_reg_size);
     106        CHECK_RET_FREE_HC_RETURN(ret, "Failed(%d) to init uhci-hcd.\n",
     107            ret);
     108#undef CHECK_RET_FREE_HC_RETURN
    109109
    110110        /*
    111          * We might free uhci_hc, but that does not matter since no one
     111         * We might free hcd, but that does not matter since no one
    112112         * else would access driver_data anyway.
    113113         */
    114         device->driver_data = uhci_hc;
     114        device->driver_data = hcd;
     115
     116        ddf_fun_t *rh = NULL;
     117#define CHECK_RET_FINI_FREE_RETURN(ret, message...) \
     118if (ret != EOK) { \
     119        usb_log_error(message); \
     120        if (hcd != NULL) {\
     121                uhci_fini(hcd); \
     122                free(hcd); \
     123        } \
     124        if (rh != NULL) \
     125                free(rh); \
     126        return ret; \
     127}
     128
    115129        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         }
     130            &hcd->interrupt_code);
     131        CHECK_RET_FINI_FREE_RETURN(ret, "Failed(%d) to register interrupt handler.\n",
     132            ret);
    123133
    124         ddf_fun_t *rh;
    125134        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;
     135        CHECK_RET_FINI_FREE_RETURN(ret, "Failed(%d) to setup UHCI root hub.\n",
     136            ret);
     137        rh->driver_data = hcd->ddf_instance;
    133138
    134139        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         }
     140        CHECK_RET_FINI_FREE_RETURN(ret, "Failed(%d) to register UHCI root hub.\n",
     141            ret);
    142142
    143143        return EOK;
     144#undef CHECK_RET_FINI_FREE_RETURN
    144145}
    145146/*----------------------------------------------------------------------------*/
Note: See TracChangeset for help on using the changeset viewer.