Changes in uspace/drv/uhci-hcd/uhci.c [b9d910f:4abc304] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/uhci-hcd/uhci.c
rb9d910f r4abc304 33 33 */ 34 34 #include <errno.h> 35 #include <str_error.h> 35 36 #include <adt/list.h> 37 #include <libarch/ddi.h> 36 38 37 39 #include <usb/debug.h> 38 40 #include <usb/usb.h> 41 #include <usb/ddfiface.h> 42 #include <usb_iface.h> 39 43 40 44 #include "uhci.h" 45 #include "iface.h" 46 41 47 static irq_cmd_t uhci_cmds[] = { 42 48 { 43 49 .cmd = CMD_PIO_READ_16, 44 .addr = (void*)0xc022,50 .addr = NULL, /* patched for every instance */ 45 51 .dstarg = 1 46 52 }, 47 53 { 48 54 .cmd = CMD_PIO_WRITE_16, 49 .addr = (void*)0xc022,55 .addr = NULL, /* pathed for every instance */ 50 56 .value = 0x1f 51 57 }, … … 55 61 }; 56 62 63 static int usb_iface_get_address(ddf_fun_t *fun, devman_handle_t handle, 64 usb_address_t *address) 65 { 66 assert(fun); 67 uhci_t *hc = fun_to_uhci(fun); 68 assert(hc); 69 70 usb_address_t addr = device_keeper_find(&hc->device_manager, 71 handle); 72 if (addr < 0) { 73 return addr; 74 } 75 76 if (address != NULL) { 77 *address = addr; 78 } 79 80 return EOK; 81 } 82 /*----------------------------------------------------------------------------*/ 83 static usb_iface_t hc_usb_iface = { 84 .get_hc_handle = usb_iface_get_hc_handle_hc_impl, 85 .get_address = usb_iface_get_address 86 }; 87 /*----------------------------------------------------------------------------*/ 88 static ddf_dev_ops_t uhci_ops = { 89 .interfaces[USB_DEV_IFACE] = &hc_usb_iface, 90 .interfaces[USBHC_DEV_IFACE] = &uhci_iface, 91 }; 92 57 93 static int uhci_init_transfer_lists(uhci_t *instance); 58 94 static int uhci_init_mem_structures(uhci_t *instance); … … 65 101 bool low_speed, usb_transfer_type_t, size_t size); 66 102 67 #define CHECK_RET_RETURN(ret, message...) \ 103 104 int uhci_init(uhci_t *instance, ddf_dev_t *dev, void *regs, size_t reg_size) 105 { 106 assert(reg_size >= sizeof(regs_t)); 107 int ret; 108 109 #define CHECK_RET_DEST_FUN_RETURN(ret, message...) \ 68 110 if (ret != EOK) { \ 69 111 usb_log_error(message); \ 112 if (instance->ddf_instance) \ 113 ddf_fun_destroy(instance->ddf_instance); \ 70 114 return ret; \ 71 115 } else (void) 0 72 116 73 int uhci_init(uhci_t *instance, void *regs, size_t reg_size) 74 { 75 assert(reg_size >= sizeof(regs_t)); 117 /* 118 * Create UHCI function. 119 */ 120 instance->ddf_instance = ddf_fun_create(dev, fun_exposed, "uhci"); 121 ret = (instance->ddf_instance == NULL) ? ENOMEM : EOK; 122 CHECK_RET_DEST_FUN_RETURN(ret, "Failed to create UHCI device function.\n"); 123 124 instance->ddf_instance->ops = &uhci_ops; 125 instance->ddf_instance->driver_data = instance; 126 127 ret = ddf_fun_bind(instance->ddf_instance); 128 CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to bind UHCI device function: %s.\n", 129 ret, str_error(ret)); 76 130 77 131 /* allow access to hc control registers */ 78 132 regs_t *io; 79 int ret = pio_enable(regs, reg_size, (void**)&io); 80 CHECK_RET_RETURN(ret, "Failed to gain access to registers at %p.\n", io); 133 ret = pio_enable(regs, reg_size, (void**)&io); 134 CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to gain access to registers at %p: %s.\n", 135 ret, str_error(ret), io); 81 136 instance->registers = io; 82 usb_log_debug("Device registers a ccessible.\n");137 usb_log_debug("Device registers at %p(%u) accessible.\n", io, reg_size); 83 138 84 139 ret = uhci_init_mem_structures(instance); 85 CHECK_RET_ RETURN(ret, "Failed to initializememory structures.\n");140 CHECK_RET_DEST_FUN_RETURN(ret, "Failed to initialize UHCI memory structures.\n"); 86 141 87 142 uhci_init_hw(instance); 88 143 89 144 instance->cleaner = fibril_create(uhci_interrupt_emulator, instance); 90 //fibril_add_ready(instance->cleaner);145 fibril_add_ready(instance->cleaner); 91 146 92 147 instance->debug_checker = fibril_create(uhci_debug_checker, instance); 93 148 fibril_add_ready(instance->debug_checker); 94 149 95 return EOK; 150 usb_log_info("Started UHCI driver.\n"); 151 return EOK; 152 #undef CHECK_RET_DEST_FUN_RETURN 96 153 } 97 154 /*----------------------------------------------------------------------------*/ 98 155 void uhci_init_hw(uhci_t *instance) 99 156 { 157 /* reset everything, who knows what touched it before us */ 158 pio_write_16(&instance->registers->usbcmd, UHCI_CMD_GLOBAL_RESET); 159 async_usleep(10000); /* 10ms according to USB spec */ 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); } 100 166 101 167 /* set framelist pointer */ … … 110 176 pio_write_16(&instance->registers->usbcmd, 111 177 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE); 112 usb_log_debug("Started UHCI HC.\n");113 178 } 114 179 /*----------------------------------------------------------------------------*/ … … 116 181 { 117 182 assert(instance); 183 #define CHECK_RET_DEST_CMDS_RETURN(ret, message...) \ 184 if (ret != EOK) { \ 185 usb_log_error(message); \ 186 if (instance->interrupt_code.cmds != NULL) \ 187 free(instance->interrupt_code.cmds); \ 188 return ret; \ 189 } else (void) 0 118 190 119 191 /* init interrupt code */ 120 irq_cmd_t *interrupt_commands = malloc(sizeof(uhci_cmds)); 121 if (interrupt_commands == NULL) { 122 return ENOMEM; 123 } 124 memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds)); 125 interrupt_commands[0].addr = (void*)&instance->registers->usbsts; 126 interrupt_commands[1].addr = (void*)&instance->registers->usbsts; 127 instance->interrupt_code.cmds = interrupt_commands; 128 instance->interrupt_code.cmdcount = 129 sizeof(uhci_cmds) / sizeof(irq_cmd_t); 192 instance->interrupt_code.cmds = malloc(sizeof(uhci_cmds)); 193 int ret = (instance->interrupt_code.cmds == NULL) ? ENOMEM : EOK; 194 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to allocate interrupt cmds space.\n"); 195 196 { 197 irq_cmd_t *interrupt_commands = instance->interrupt_code.cmds; 198 memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds)); 199 interrupt_commands[0].addr = (void*)&instance->registers->usbsts; 200 interrupt_commands[1].addr = (void*)&instance->registers->usbsts; 201 instance->interrupt_code.cmdcount = 202 sizeof(uhci_cmds) / sizeof(irq_cmd_t); 203 } 130 204 131 205 /* init transfer lists */ 132 intret = uhci_init_transfer_lists(instance);133 CHECK_RET_ RETURN(ret, "Failed to initialize transfer lists.\n");206 ret = uhci_init_transfer_lists(instance); 207 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to initialize transfer lists.\n"); 134 208 usb_log_debug("Initialized transfer lists.\n"); 135 209 … … 137 211 instance->frame_list = get_page(); 138 212 ret = instance ? EOK : ENOMEM; 139 CHECK_RET_ RETURN(ret, "Failed to get frame list page.\n");213 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to get frame list page.\n"); 140 214 usb_log_debug("Initialized frame list.\n"); 141 215 … … 144 218 instance->transfers_interrupt.queue_head_pa 145 219 | LINK_POINTER_QUEUE_HEAD_FLAG; 220 146 221 unsigned i = 0; 147 222 for(; i < UHCI_FRAME_LIST_COUNT; ++i) { … … 150 225 151 226 /* init address keeper(libusb) */ 152 usb_address_keeping_init(&instance->address_manager, USB11_ADDRESS_MAX); 153 usb_log_debug("Initialized address manager.\n"); 154 155 return EOK; 227 device_keeper_init(&instance->device_manager); 228 usb_log_debug("Initialized device manager.\n"); 229 230 return EOK; 231 #undef CHECK_RET_DEST_CMDS_RETURN 156 232 } 157 233 /*----------------------------------------------------------------------------*/ … … 159 235 { 160 236 assert(instance); 237 #define CHECK_RET_CLEAR_RETURN(ret, message...) \ 238 if (ret != EOK) { \ 239 usb_log_error(message); \ 240 transfer_list_fini(&instance->transfers_bulk_full); \ 241 transfer_list_fini(&instance->transfers_control_full); \ 242 transfer_list_fini(&instance->transfers_control_slow); \ 243 transfer_list_fini(&instance->transfers_interrupt); \ 244 return ret; \ 245 } else (void) 0 161 246 162 247 /* initialize TODO: check errors */ 163 248 int ret; 164 249 ret = transfer_list_init(&instance->transfers_bulk_full, "BULK_FULL"); 165 assert(ret == EOK);250 CHECK_RET_CLEAR_RETURN(ret, "Failed to init BULK list."); 166 251 ret = transfer_list_init(&instance->transfers_control_full, "CONTROL_FULL"); 167 assert(ret == EOK);252 CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL FULL list."); 168 253 ret = transfer_list_init(&instance->transfers_control_slow, "CONTROL_SLOW"); 169 assert(ret == EOK);254 CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL SLOW list."); 170 255 ret = transfer_list_init(&instance->transfers_interrupt, "INTERRUPT"); 171 assert(ret == EOK);256 CHECK_RET_CLEAR_RETURN(ret, "Failed to init INTERRUPT list."); 172 257 173 258 transfer_list_set_next(&instance->transfers_control_full, … … 196 281 197 282 return EOK; 283 #undef CHECK_RET_CLEAR_RETURN 198 284 } 199 285 /*----------------------------------------------------------------------------*/ … … 202 288 assert(instance); 203 289 assert(batch); 204 const int low_speed = (batch->speed == LOW_SPEED);290 const int low_speed = (batch->speed == USB_SPEED_LOW); 205 291 if (!allowed_usb_packet( 206 292 low_speed, batch->transfer_type, batch->max_packet_size)) { … … 223 309 { 224 310 assert(instance); 225 if ((status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) == 0)226 return;227 usb_log_debug("UHCI interrupt: %X.\n", status);228 transfer_list_ check(&instance->transfers_interrupt);229 transfer_list_ check(&instance->transfers_control_slow);230 transfer_list_ check(&instance->transfers_control_full);231 transfer_list_ check(&instance->transfers_bulk_full);311 // if ((status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) == 0) 312 // return; 313 // usb_log_debug2("UHCI interrupt: %X.\n", status); 314 transfer_list_remove_finished(&instance->transfers_interrupt); 315 transfer_list_remove_finished(&instance->transfers_control_slow); 316 transfer_list_remove_finished(&instance->transfers_control_full); 317 transfer_list_remove_finished(&instance->transfers_bulk_full); 232 318 } 233 319 /*----------------------------------------------------------------------------*/ … … 238 324 assert(instance); 239 325 240 while (1) {326 while (1) { 241 327 uint16_t status = pio_read_16(&instance->registers->usbsts); 328 if (status != 0) 329 usb_log_debug2("UHCI status: %x.\n", status); 330 status |= 1; 242 331 uhci_interrupt(instance, status); 243 async_usleep(UHCI_CLEANER_TIMEOUT); 332 pio_write_16(&instance->registers->usbsts, 0x1f); 333 async_usleep(UHCI_CLEANER_TIMEOUT * 5); 244 334 } 245 335 return EOK; … … 254 344 const uint16_t sts = pio_read_16(&instance->registers->usbsts); 255 345 const uint16_t intr = pio_read_16(&instance->registers->usbintr); 256 usb_log_debug("Command: %X Status: %X Interrupts: %x\n", 257 cmd, sts, intr); 258 259 uintptr_t frame_list = pio_read_32(&instance->registers->flbaseadd); 346 if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) { 347 usb_log_debug2("Command: %X Status: %X Intr: %x\n", 348 cmd, sts, intr); 349 } 350 351 uintptr_t frame_list = 352 pio_read_32(&instance->registers->flbaseadd) & ~0xfff; 260 353 if (frame_list != addr_to_phys(instance->frame_list)) { 261 354 usb_log_debug("Framelist address: %p vs. %p.\n",
Note:
See TracChangeset
for help on using the changeset viewer.