Changes in uspace/drv/bus/usb/ohci/hc.c [f83666c:58563585] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/bus/usb/ohci/hc.c
rf83666c r58563585 34 34 */ 35 35 36 #include <assert.h> 37 #include <async.h> 36 38 #include <errno.h> 37 #include <stdbool.h> 39 #include <macros.h> 40 #include <mem.h> 41 #include <stdlib.h> 38 42 #include <str_error.h> 39 #include <adt/list.h> 40 #include <libarch/ddi.h> 43 #include <sys/types.h> 41 44 42 45 #include <usb/debug.h> 43 46 #include <usb/usb.h> 44 #include <usb/ddfiface.h> 47 48 #include "ohci_endpoint.h" 49 #include "ohci_batch.h" 45 50 46 51 #include "hc.h" 47 #include "ohci_endpoint.h"48 52 49 53 #define OHCI_USED_INTERRUPTS \ … … 84 88 }; 85 89 86 enum {87 /** Number of PIO ranges used in IRQ code */88 hc_irq_pio_range_count =89 sizeof(ohci_pio_ranges) / sizeof(irq_pio_range_t),90 91 /** Number of commands used in IRQ code */92 hc_irq_cmd_count =93 sizeof(ohci_irq_commands) / sizeof(irq_cmd_t)94 };95 96 90 static void hc_gain_control(hc_t *instance); 97 91 static void hc_start(hc_t *instance); 98 92 static int hc_init_transfer_lists(hc_t *instance); 99 93 static int hc_init_memory(hc_t *instance); 100 static int interrupt_emulator(hc_t *instance);101 static int hc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch);102 94 103 95 /** Generate IRQ code. … … 106 98 * @param[out] cmds Commands buffer. 107 99 * @param[in] cmds_size Size of the commands buffer (bytes). 108 * @param[in] regs Physical address of device's registers. 109 * @param[in] reg_size Size of the register area (bytes). 100 * @param[in] hw_res Device's resources. 110 101 * 111 102 * @return Error code. 112 103 */ 113 int 114 hc_get_irq_code(irq_pio_range_t ranges[], size_t ranges_size, irq_cmd_t cmds[], 115 size_t cmds_size, uintptr_t regs, size_t reg_size) 116 { 117 if ((ranges_size < sizeof(ohci_pio_ranges)) || 118 (cmds_size < sizeof(ohci_irq_commands)) || 119 (reg_size < sizeof(ohci_regs_t))) 104 int ohci_hc_gen_irq_code(irq_code_t *code, const hw_res_list_parsed_t *hw_res) 105 { 106 assert(code); 107 assert(hw_res); 108 109 if (hw_res->irqs.count != 1 || hw_res->mem_ranges.count != 1) 110 return EINVAL; 111 112 const addr_range_t regs = hw_res->mem_ranges.ranges[0]; 113 114 if (RNGSZ(regs) < sizeof(ohci_regs_t)) 120 115 return EOVERFLOW; 121 116 122 memcpy(ranges, ohci_pio_ranges, sizeof(ohci_pio_ranges)); 123 ranges[0].base = regs; 124 125 memcpy(cmds, ohci_irq_commands, sizeof(ohci_irq_commands)); 126 ohci_regs_t *registers = (ohci_regs_t *) regs; 127 cmds[0].addr = (void *) ®isters->interrupt_status; 128 cmds[3].addr = (void *) ®isters->interrupt_status; 129 OHCI_WR(cmds[1].value, OHCI_USED_INTERRUPTS); 130 131 return EOK; 132 } 133 134 /** Register interrupt handler. 135 * 136 * @param[in] device Host controller DDF device 137 * @param[in] reg_base Register range base 138 * @param[in] reg_size Register range size 139 * @param[in] irq Interrupt number 140 * @paran[in] handler Interrupt handler 141 * 142 * @return EOK on success or negative error code 143 */ 144 int hc_register_irq_handler(ddf_dev_t *device, uintptr_t reg_base, size_t reg_size, 145 int irq, interrupt_handler_t handler) 146 { 147 int rc; 148 149 irq_pio_range_t irq_ranges[hc_irq_pio_range_count]; 150 irq_cmd_t irq_cmds[hc_irq_cmd_count]; 151 152 irq_code_t irq_code = { 153 .rangecount = hc_irq_pio_range_count, 154 .ranges = irq_ranges, 155 .cmdcount = hc_irq_cmd_count, 156 .cmds = irq_cmds 157 }; 158 159 rc = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds, 160 sizeof(irq_cmds), reg_base, reg_size); 161 if (rc != EOK) { 162 usb_log_error("Failed to generate IRQ code: %s.\n", 163 str_error(rc)); 164 return rc; 165 } 166 167 /* Register handler to avoid interrupt lockup */ 168 rc = register_interrupt_handler(device, irq, handler, &irq_code); 169 if (rc != EOK) { 170 usb_log_error("Failed to register interrupt handler: %s.\n", 171 str_error(rc)); 172 return rc; 173 } 174 175 return EOK; 176 } 177 178 /** Announce OHCI root hub to the DDF 179 * 180 * @param[in] instance OHCI driver intance 181 * @param[in] hub_fun DDF fuction representing OHCI root hub 182 * @return Error code 183 */ 184 int hc_register_hub(hc_t *instance, ddf_fun_t *hub_fun) 185 { 186 bool addr_reqd = false; 187 bool ep_added = false; 188 bool fun_bound = false; 189 int rc; 190 191 assert(instance); 192 assert(hub_fun); 193 194 /* Try to get address 1 for root hub. */ 195 instance->rh.address = 1; 196 rc = usb_device_manager_request_address( 197 &instance->generic.dev_manager, &instance->rh.address, false, 198 USB_SPEED_FULL); 199 if (rc != EOK) { 200 usb_log_error("Failed to get OHCI root hub address: %s\n", 201 str_error(rc)); 202 goto error; 203 } 204 205 addr_reqd = true; 206 207 rc = usb_endpoint_manager_add_ep( 208 &instance->generic.ep_manager, instance->rh.address, 0, 209 USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL, USB_SPEED_FULL, 64, 210 0, NULL, NULL); 211 if (rc != EOK) { 212 usb_log_error("Failed to register root hub control endpoint: %s.\n", 213 str_error(rc)); 214 goto error; 215 } 216 217 ep_added = true; 218 219 rc = ddf_fun_add_match_id(hub_fun, "usb&class=hub", 100); 220 if (rc != EOK) { 221 usb_log_error("Failed to add root hub match-id: %s.\n", 222 str_error(rc)); 223 goto error; 224 } 225 226 rc = ddf_fun_bind(hub_fun); 227 if (rc != EOK) { 228 usb_log_error("Failed to bind root hub function: %s.\n", 229 str_error(rc)); 230 goto error; 231 } 232 233 fun_bound = true; 234 235 rc = usb_device_manager_bind_address(&instance->generic.dev_manager, 236 instance->rh.address, ddf_fun_get_handle(hub_fun)); 237 if (rc != EOK) { 238 usb_log_warning("Failed to bind root hub address: %s.\n", 239 str_error(rc)); 240 } 241 242 return EOK; 243 error: 244 if (fun_bound) 245 ddf_fun_unbind(hub_fun); 246 if (ep_added) { 247 usb_endpoint_manager_remove_ep( 248 &instance->generic.ep_manager, instance->rh.address, 0, 249 USB_DIRECTION_BOTH, NULL, NULL); 250 } 251 if (addr_reqd) { 252 usb_device_manager_release_address( 253 &instance->generic.dev_manager, instance->rh.address); 254 } 255 return rc; 117 code->ranges = malloc(sizeof(ohci_pio_ranges)); 118 if (code->ranges == NULL) 119 return ENOMEM; 120 121 code->cmds = malloc(sizeof(ohci_irq_commands)); 122 if (code->cmds == NULL) { 123 free(code->ranges); 124 return ENOMEM; 125 } 126 127 code->rangecount = ARRAY_SIZE(ohci_pio_ranges); 128 code->cmdcount = ARRAY_SIZE(ohci_irq_commands); 129 130 memcpy(code->ranges, ohci_pio_ranges, sizeof(ohci_pio_ranges)); 131 code->ranges[0].base = RNGABS(regs); 132 133 memcpy(code->cmds, ohci_irq_commands, sizeof(ohci_irq_commands)); 134 ohci_regs_t *registers = (ohci_regs_t *) RNGABSPTR(regs); 135 code->cmds[0].addr = (void *) ®isters->interrupt_status; 136 code->cmds[3].addr = (void *) ®isters->interrupt_status; 137 OHCI_WR(code->cmds[1].value, OHCI_USED_INTERRUPTS); 138 139 usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n", 140 RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]); 141 142 return hw_res->irqs.irqs[0]; 256 143 } 257 144 … … 259 146 * 260 147 * @param[in] instance Memory place for the structure. 261 * @param[in] regs Address of the memory mapped I/O registers. 262 * @param[in] reg_size Size of the memory mapped area. 148 * @param[in] regs Device's resources 263 149 * @param[in] interrupts True if w interrupts should be used 264 150 * @return Error code 265 151 */ 266 int hc_init(hc_t *instance, uintptr_t regs, size_t reg_size, bool interrupts) 267 { 268 assert(instance); 269 270 int rc = 271 pio_enable((void*)regs, reg_size, (void**)&instance->registers); 272 if (rc != EOK) { 273 usb_log_error("Failed to gain access to device registers: %s.\n", 274 str_error(rc)); 275 return rc; 276 } 152 int hc_init(hc_t *instance, const hw_res_list_parsed_t *hw_res, bool interrupts) 153 { 154 assert(instance); 155 assert(hw_res); 156 if (hw_res->mem_ranges.count != 1 || 157 hw_res->mem_ranges.ranges[0].size < sizeof(ohci_regs_t)) 158 return EINVAL; 159 160 int ret = pio_enable_range(&hw_res->mem_ranges.ranges[0], 161 (void **) &instance->registers); 162 if (ret != EOK) { 163 usb_log_error("Failed to gain access to registers: %s.\n", 164 str_error(ret)); 165 return ret; 166 } 167 usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.\n", 168 hw_res->mem_ranges.ranges[0].address.absolute, 169 hw_res->mem_ranges.ranges[0].size); 277 170 278 171 list_initialize(&instance->pending_batches); 279 280 hcd_init(&instance->generic, USB_SPEED_FULL, 281 BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11); 282 instance->generic.private_data = instance; 283 instance->generic.schedule = hc_schedule; 284 instance->generic.ep_add_hook = ohci_endpoint_init; 285 instance->generic.ep_remove_hook = ohci_endpoint_fini; 286 287 rc = hc_init_memory(instance); 288 if (rc != EOK) { 172 fibril_mutex_initialize(&instance->guard); 173 instance->hw_interrupts = interrupts; 174 175 ret = hc_init_memory(instance); 176 if (ret != EOK) { 289 177 usb_log_error("Failed to create OHCI memory structures: %s.\n", 290 str_error(rc)); 291 return rc; 292 } 293 294 fibril_mutex_initialize(&instance->guard); 178 str_error(ret)); 179 // TODO: We should disable pio access here 180 return ret; 181 } 295 182 296 183 hc_gain_control(instance); 297 184 298 if (!interrupts) { 299 instance->interrupt_emulator = 300 fibril_create((int(*)(void*))interrupt_emulator, instance); 301 fibril_add_ready(instance->interrupt_emulator); 302 } 303 304 rh_init(&instance->rh, instance->registers); 185 ohci_rh_init(&instance->rh, instance->registers, "ohci rh"); 305 186 hc_start(instance); 306 187 307 188 return EOK; 308 189 } 190 191 /** Safely dispose host controller internal structures 192 * 193 * @param[in] instance Host controller structure to use. 194 */ 195 void hc_fini(hc_t *instance) 196 { 197 assert(instance); 198 /* TODO: implement*/ 199 }; 309 200 310 201 void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep) … … 376 267 } 377 268 269 int ohci_hc_status(hcd_t *hcd, uint32_t *status) 270 { 271 assert(hcd); 272 assert(status); 273 hc_t *instance = hcd_get_driver_data(hcd); 274 assert(instance); 275 276 if (instance->registers){ 277 *status = OHCI_RD(instance->registers->interrupt_status); 278 OHCI_WR(instance->registers->interrupt_status, *status); 279 } 280 return EOK; 281 } 282 378 283 /** Add USB transfer to the schedule. 379 284 * 380 * @param[in] instance OHCI hcdriver structure.285 * @param[in] hcd HCD driver structure. 381 286 * @param[in] batch Batch representing the transfer. 382 287 * @return Error code. 383 288 */ 384 int hc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)289 int ohci_hc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch) 385 290 { 386 291 assert(hcd); 387 hc_t *instance = hcd ->private_data;292 hc_t *instance = hcd_get_driver_data(hcd); 388 293 assert(instance); 389 294 390 295 /* Check for root hub communication */ 391 if (batch->ep->address == instance->rh.address) {296 if (batch->ep->address == ohci_rh_get_address(&instance->rh)) { 392 297 usb_log_debug("OHCI root hub request.\n"); 393 rh_request(&instance->rh, batch); 394 return EOK; 298 return ohci_rh_schedule(&instance->rh, batch); 395 299 } 396 300 ohci_transfer_batch_t *ohci_batch = ohci_transfer_batch_get(batch); … … 420 324 /** Interrupt handling routine 421 325 * 422 * @param[in] instance OHCI hcdriver structure.326 * @param[in] hcd HCD driver structure. 423 327 * @param[in] status Value of the status register at the time of interrupt. 424 328 */ 425 void hc_interrupt(hc_t *instance, uint32_t status) 426 { 329 void ohci_hc_interrupt(hcd_t *hcd, uint32_t status) 330 { 331 assert(hcd); 332 hc_t *instance = hcd_get_driver_data(hcd); 427 333 status = OHCI_RD(status); 428 334 assert(instance); … … 431 337 usb_log_debug2("OHCI(%p) interrupt: %x.\n", instance, status); 432 338 if (status & I_RHSC) 433 rh_interrupt(&instance->rh);339 ohci_rh_interrupt(&instance->rh); 434 340 435 341 if (status & I_WDH) { … … 462 368 } 463 369 464 }465 466 /** Check status register regularly467 *468 * @param[in] instance OHCI hc driver structure.469 * @return Error code470 */471 int interrupt_emulator(hc_t *instance)472 {473 assert(instance);474 usb_log_info("Started interrupt emulator.\n");475 while (1) {476 const uint32_t status = instance->registers->interrupt_status;477 instance->registers->interrupt_status = status;478 hc_interrupt(instance, status);479 async_usleep(10000);480 }481 return EOK;482 370 } 483 371 … … 505 393 ohci_emulation_reg, OHCI_RD(*ohci_emulation_reg)); 506 394 /* Zero everything but A20State */ 395 // TODO: should we ack interrupts before doing this? 507 396 OHCI_CLR(*ohci_emulation_reg, ~0x100); 508 397 usb_log_debug( … … 514 403 if (OHCI_RD(instance->registers->control) & C_IR) { 515 404 usb_log_debug("SMM driver: request ownership change.\n"); 405 // TODO: should we ack interrupts before doing this? 516 406 OHCI_SET(instance->registers->command_status, CS_OCR); 517 407 /* Hope that SMM actually knows its stuff or we can hang here */ 518 while (OHCI_RD(instance->registers->control & C_IR)) {408 while (OHCI_RD(instance->registers->control) & C_IR) { 519 409 async_usleep(1000); 520 410 } … … 600 490 601 491 /* Enable interrupts */ 602 OHCI_WR(instance->registers->interrupt_enable, OHCI_USED_INTERRUPTS); 603 usb_log_debug("Enabled interrupts: %x.\n", 604 OHCI_RD(instance->registers->interrupt_enable)); 605 OHCI_WR(instance->registers->interrupt_enable, I_MI); 492 if (instance->hw_interrupts) { 493 OHCI_WR(instance->registers->interrupt_enable, 494 OHCI_USED_INTERRUPTS); 495 usb_log_debug("Enabled interrupts: %x.\n", 496 OHCI_RD(instance->registers->interrupt_enable)); 497 OHCI_WR(instance->registers->interrupt_enable, I_MI); 498 } 606 499 607 500 /* Set periodic start to 90% */ … … 629 522 do { \ 630 523 const char *name = usb_str_transfer_type(type); \ 631 int ret = endpoint_list_init(&instance->lists[type], name); \524 const int ret = endpoint_list_init(&instance->lists[type], name); \ 632 525 if (ret != EOK) { \ 633 526 usb_log_error("Failed to setup %s endpoint list: %s.\n", \
Note:
See TracChangeset
for help on using the changeset viewer.