Changeset dc5f2fb in mainline


Ignore:
Timestamp:
2011-05-07T15:22:38Z (13 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d2bff2f
Parents:
62265ce
Message:

Further OHCI refactoring

Location:
uspace/drv/ohci
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/ohci/main.c

    r62265ce rdc5f2fb  
    4343#define NAME "ohci"
    4444
    45 static int ohci_add_device(ddf_dev_t *device);
     45/** Initializes a new ddf driver instance of OHCI hcd.
     46 *
     47 * @param[in] device DDF instance of the device to initialize.
     48 * @return Error code.
     49 */
     50static int ohci_add_device(ddf_dev_t *device)
     51{
     52        usb_log_debug("ohci_add_device() called\n");
     53        assert(device);
     54
     55        int ret = device_setup_ohci(device);
     56        if (ret != EOK) {
     57                usb_log_error("Failed to initialize OHCI driver: %s.\n",
     58                    str_error(ret));
     59                return ret;
     60        }
     61        usb_log_info("Controlling new OHCI device '%s'.\n", device->name);
     62
     63        return EOK;
     64}
    4665/*----------------------------------------------------------------------------*/
    4766static driver_ops_t ohci_driver_ops = {
     
    5372        .driver_ops = &ohci_driver_ops
    5473};
    55 /*----------------------------------------------------------------------------*/
    56 /** Initializes a new ddf driver instance of OHCI hcd.
    57  *
    58  * @param[in] device DDF instance of the device to initialize.
    59  * @return Error code.
    60  */
    61 int ohci_add_device(ddf_dev_t *device)
    62 {
    63         usb_log_debug("ohci_add_device() called\n");
    64         assert(device);
    65         ohci_t *ohci = malloc(sizeof(ohci_t));
    66         if (ohci == NULL) {
    67                 usb_log_error("Failed to allocate OHCI driver.\n");
    68                 return ENOMEM;
    69         }
    70 
    71         int ret = device_setup_ohci(device, ohci);
    72         if (ret != EOK) {
    73                 usb_log_error("Failed to initialize OHCI driver: %s.\n",
    74                     str_error(ret));
    75                 return ret;
    76         }
    77 //      device->driver_data = ohci;
    78         hc_register_hub(&ohci->hc, ohci->rh_fun);
    79 
    80         usb_log_info("Controlling new OHCI device `%s'.\n", device->name);
    81 
    82         return EOK;
    83 }
    8474/*----------------------------------------------------------------------------*/
    8575/** Initializes global driver structures (NONE).
  • uspace/drv/ohci/ohci.c

    r62265ce rdc5f2fb  
    4444#include "iface.h"
    4545#include "pci.h"
     46#include "hc.h"
     47#include "root_hub.h"
     48
     49typedef struct ohci {
     50        ddf_fun_t *hc_fun;
     51        ddf_fun_t *rh_fun;
     52
     53        hc_t hc;
     54        rh_t rh;
     55} ohci_t;
     56
     57static inline ohci_t * dev_to_ohci(ddf_dev_t *dev)
     58{
     59        assert(dev);
     60        assert(dev->driver_data);
     61        return dev->driver_data;
     62}
    4663
    4764/** IRQ handling callback, identifies device
     
    5370static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
    5471{
    55         assert(dev);
    56         assert(dev->driver_data);
    57         hc_t *hc = &((ohci_t*)dev->driver_data)->hc;
    58         uint16_t status = IPC_GET_ARG1(*call);
     72        hc_t *hc = &dev_to_ohci(dev)->hc;
    5973        assert(hc);
     74        const uint16_t status = IPC_GET_ARG1(*call);
    6075        hc_interrupt(hc, status);
    6176}
     
    7186{
    7287        assert(fun);
    73         usb_device_keeper_t *manager = &((ohci_t*)fun->dev->driver_data)->hc.manager;
     88        usb_device_keeper_t *manager = &dev_to_ohci(fun->dev)->hc.manager;
    7489
    7590        usb_address_t addr = usb_device_keeper_find(manager, handle);
     
    95110{
    96111        assert(handle);
    97         ddf_fun_t *hc_fun = ((ohci_t*)fun->dev->driver_data)->hc_fun;
    98         assert(hc_fun != NULL);
     112        assert(fun);
     113        ddf_fun_t *hc_fun = dev_to_ohci(fun->dev)->hc_fun;
     114        assert(hc_fun);
    99115
    100116        *handle = hc_fun->handle;
     
    102118}
    103119/*----------------------------------------------------------------------------*/
    104 /** This iface is generic for both RH and HC. */
     120/** Root hub USB interface */
    105121static usb_iface_t usb_iface = {
    106122        .get_hc_handle = usb_iface_get_hc_handle,
     
    108124};
    109125/*----------------------------------------------------------------------------*/
     126/** Standard USB HC options (HC interface) */
    110127static ddf_dev_ops_t hc_ops = {
    111128        .interfaces[USBHC_DEV_IFACE] = &hc_iface, /* see iface.h/c */
    112129};
    113130/*----------------------------------------------------------------------------*/
     131/** Standard USB RH options (RH interface) */
    114132static ddf_dev_ops_t rh_ops = {
    115133        .interfaces[USB_DEV_IFACE] = &usb_iface,
     
    118136/** Initialize hc and rh ddf structures and their respective drivers.
    119137 *
     138 * @param[in] device DDF instance of the device to use.
    120139 * @param[in] instance OHCI structure to use.
    121  * @param[in] device DDF instance of the device to use.
    122140 *
    123141 * This function does all the preparatory work for hc and rh drivers:
     
    127145 *  - registers interrupt handler
    128146 */
    129 int device_setup_ohci(ddf_dev_t *device, ohci_t *instance)
    130 {
    131         assert(instance);
     147int device_setup_ohci(ddf_dev_t *device)
     148{
     149        ohci_t *instance = malloc(sizeof(ohci_t));
     150        if (instance == NULL) {
     151                usb_log_error("Failed to allocate OHCI driver.\n");
     152                return ENOMEM;
     153        }
     154
     155#define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
     156if (ret != EOK) { \
     157        if (instance->hc_fun) { \
     158                instance->hc_fun->ops = NULL; \
     159                instance->hc_fun->driver_data = NULL; \
     160                ddf_fun_destroy(instance->hc_fun); \
     161        } \
     162        if (instance->rh_fun) { \
     163                instance->rh_fun->ops = NULL; \
     164                instance->rh_fun->driver_data = NULL; \
     165                ddf_fun_destroy(instance->rh_fun); \
     166        } \
     167        free(instance); \
     168        usb_log_error(message); \
     169        return ret; \
     170} else (void)0
    132171
    133172        instance->hc_fun = ddf_fun_create(device, fun_exposed, "ohci-hc");
    134         if (instance->hc_fun == NULL) {
    135                 usb_log_error("Failed to create HC function.\n");
    136                 return ENOMEM;
    137         }
     173        int ret = instance->hc_fun ? EOK : ENOMEM;
     174        CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create OHCI HC function.\n");
    138175        instance->hc_fun->ops = &hc_ops;
    139176        instance->hc_fun->driver_data = &instance->hc;
    140177
    141178        instance->rh_fun = ddf_fun_create(device, fun_inner, "ohci-rh");
    142         if (instance->rh_fun == NULL) {
    143                 ddf_fun_destroy(instance->hc_fun);
    144                 usb_log_error("Failed to create RH function.\n");
    145                 return ENOMEM;
    146         }
     179        ret = instance->rh_fun ? EOK : ENOMEM;
     180        CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create OHCI RH function.\n");
    147181        instance->rh_fun->ops = &rh_ops;
    148         instance->rh_fun->driver_data = NULL;
    149 
    150 #define CHECK_RET_DEST_FUN_RETURN(ret, message...) \
    151 if (ret != EOK) { \
    152         usb_log_error(message); \
    153         instance->hc_fun->ops = NULL; \
    154         instance->hc_fun->driver_data = NULL; \
    155         instance->rh_fun->ops = NULL; \
    156         instance->rh_fun->driver_data = NULL; \
    157         ddf_fun_destroy(instance->hc_fun); \
    158         ddf_fun_destroy(instance->rh_fun); \
    159         return ret; \
    160 }
    161 
    162         uintptr_t mem_reg_base = 0;
    163         size_t mem_reg_size = 0;
     182
     183        uintptr_t reg_base = 0;
     184        size_t reg_size = 0;
    164185        int irq = 0;
    165186
    166         int ret =
    167             pci_get_my_registers(device, &mem_reg_base, &mem_reg_size, &irq);
    168         CHECK_RET_DEST_FUN_RETURN(ret,
     187        ret = pci_get_my_registers(device, &reg_base, &reg_size, &irq);
     188        CHECK_RET_DEST_FREE_RETURN(ret,
    169189            "Failed to get memory addresses for %" PRIun ": %s.\n",
    170190            device->handle, str_error(ret));
    171191        usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
    172             (void *) mem_reg_base, mem_reg_size, irq);
     192            (void *) reg_base, reg_size, irq);
    173193
    174194        bool interrupts = false;
     
    189209#endif
    190210
    191         ret = hc_init(&instance->hc, mem_reg_base, mem_reg_size, interrupts);
    192         CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to init ohci-hcd.\n", ret);
     211        ret = hc_init(&instance->hc, reg_base, reg_size, interrupts);
     212        CHECK_RET_DEST_FREE_RETURN(ret, "Failed(%d) to init ohci-hcd.\n", ret);
    193213
    194214#define CHECK_RET_FINI_RETURN(ret, message...) \
    195215if (ret != EOK) { \
    196216        hc_fini(&instance->hc); \
    197         CHECK_RET_DEST_FUN_RETURN(ret, message); \
    198 }
     217        CHECK_RET_DEST_FREE_RETURN(ret, message); \
     218} else (void)0
    199219
    200220        /* It does no harm if we register this on polling */
     
    212232
    213233        hc_start_hw(&instance->hc);
     234        hc_register_hub(&instance->hc, instance->rh_fun);
    214235        return EOK;
    215236
  • uspace/drv/ohci/ohci.h

    r62265ce rdc5f2fb  
    3838#include <ddf/driver.h>
    3939
    40 #include "hc.h"
    41 #include "root_hub.h"
    42 
    43 typedef struct ohci {
    44         ddf_fun_t *hc_fun;
    45         ddf_fun_t *rh_fun;
    46 
    47         hc_t hc;
    48         rh_t rh;
    49 } ohci_t;
    50 
    51 int device_setup_ohci(ddf_dev_t *device, ohci_t *instance);
     40int device_setup_ohci(ddf_dev_t *device);
    5241
    5342#endif
  • uspace/drv/ohci/pci.c

    r62265ce rdc5f2fb  
    5858    uintptr_t *mem_reg_address, size_t *mem_reg_size, int *irq_no)
    5959{
    60         assert(dev != NULL);
     60        assert(dev);
     61        assert(mem_reg_address);
     62        assert(mem_reg_size);
     63        assert(irq_no);
    6164
    6265        int parent_phone = devman_parent_device_connect(dev->handle,
Note: See TracChangeset for help on using the changeset viewer.