00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00034 #include <ddf/driver.h>
00035 #include <devman.h>
00036 #include <device/hw_res.h>
00037 #include <errno.h>
00038 #include <str_error.h>
00039
00040 #include <usb_iface.h>
00041 #include <usb/ddfiface.h>
00042 #include <usb/debug.h>
00043
00044 #include "root_hub.h"
00045
00046 #define NAME "uhci-rhd"
00047
00048 static int hc_get_my_registers(const ddf_dev_t *dev,
00049 uintptr_t *io_reg_address, size_t *io_reg_size);
00050
00051 static int uhci_rh_add_device(ddf_dev_t *device);
00052
00053 static driver_ops_t uhci_rh_driver_ops = {
00054 .add_device = uhci_rh_add_device,
00055 };
00056
00057 static driver_t uhci_rh_driver = {
00058 .name = NAME,
00059 .driver_ops = &uhci_rh_driver_ops
00060 };
00061
00070 int main(int argc, char *argv[])
00071 {
00072 printf(NAME ": HelenOS UHCI root hub driver.\n");
00073 usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
00074 return ddf_driver_main(&uhci_rh_driver);
00075 }
00076
00082 static int uhci_rh_add_device(ddf_dev_t *device)
00083 {
00084 if (!device)
00085 return EINVAL;
00086
00087 usb_log_debug2("uhci_rh_add_device(handle=%" PRIun ")\n",
00088 device->handle);
00089
00090 uintptr_t io_regs = 0;
00091 size_t io_size = 0;
00092 uhci_root_hub_t *rh = NULL;
00093 int ret = EOK;
00094
00095 #define CHECK_RET_FREE_RH_RETURN(ret, message...) \
00096 if (ret != EOK) { \
00097 usb_log_error(message); \
00098 if (rh) \
00099 free(rh); \
00100 return ret; \
00101 } else (void)0
00102
00103 ret = hc_get_my_registers(device, &io_regs, &io_size);
00104 CHECK_RET_FREE_RH_RETURN(ret,
00105 "Failed to get registers from HC: %s.\n", str_error(ret));
00106 usb_log_debug("I/O regs at %p (size %zuB).\n",
00107 (void *) io_regs, io_size);
00108
00109 rh = malloc(sizeof(uhci_root_hub_t));
00110 ret = (rh == NULL) ? ENOMEM : EOK;
00111 CHECK_RET_FREE_RH_RETURN(ret,
00112 "Failed to allocate rh driver instance.\n");
00113
00114 ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device);
00115 CHECK_RET_FREE_RH_RETURN(ret,
00116 "Failed(%d) to initialize rh driver instance: %s.\n",
00117 ret, str_error(ret));
00118
00119 device->driver_data = rh;
00120 usb_log_info("Controlling root hub '%s' (%" PRIun ").\n",
00121 device->name, device->handle);
00122 return EOK;
00123 }
00124
00132 int hc_get_my_registers(
00133 const ddf_dev_t *dev, uintptr_t *io_reg_address, size_t *io_reg_size)
00134 {
00135 assert(dev);
00136
00137 const int parent_phone = devman_parent_device_connect(dev->handle,
00138 IPC_FLAG_BLOCKING);
00139 if (parent_phone < 0) {
00140 return parent_phone;
00141 }
00142
00143 hw_resource_list_t hw_resources;
00144 const int ret = hw_res_get_resource_list(parent_phone, &hw_resources);
00145 if (ret != EOK) {
00146 async_hangup(parent_phone);
00147 return ret;
00148 }
00149
00150 uintptr_t io_address = 0;
00151 size_t io_size = 0;
00152 bool io_found = false;
00153
00154 size_t i = 0;
00155 for (; i < hw_resources.count; i++) {
00156 hw_resource_t *res = &hw_resources.resources[i];
00157 if (res->type == IO_RANGE) {
00158 io_address = res->res.io_range.address;
00159 io_size = res->res.io_range.size;
00160 io_found = true;
00161 }
00162 }
00163 async_hangup(parent_phone);
00164
00165 if (!io_found) {
00166 return ENOENT;
00167 }
00168 if (io_reg_address != NULL) {
00169 *io_reg_address = io_address;
00170 }
00171 if (io_reg_size != NULL) {
00172 *io_reg_size = io_size;
00173 }
00174 return EOK;
00175 }