Changes in uspace/drv/uhci-hcd/uhci.c [b9d910f:b375bb8] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/uhci-hcd/uhci.c
rb9d910f rb375bb8 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 /* Create UHCI function. */ 118 instance->ddf_instance = ddf_fun_create(dev, fun_exposed, "uhci"); 119 ret = (instance->ddf_instance == NULL) ? ENOMEM : EOK; 120 CHECK_RET_DEST_FUN_RETURN(ret, 121 "Failed to create UHCI device function.\n"); 122 123 instance->ddf_instance->ops = &uhci_ops; 124 instance->ddf_instance->driver_data = instance; 125 126 ret = ddf_fun_bind(instance->ddf_instance); 127 CHECK_RET_DEST_FUN_RETURN(ret, 128 "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, 135 "Failed(%d) to gain access to registers at %p: %s.\n", 136 ret, str_error(ret), io); 81 137 instance->registers = io; 82 usb_log_debug("Device registers accessible.\n"); 138 usb_log_debug("Device registers at %p(%u) accessible.\n", 139 io, reg_size); 83 140 84 141 ret = uhci_init_mem_structures(instance); 85 CHECK_RET_RETURN(ret, "Failed to initialize memory structures.\n"); 142 CHECK_RET_DEST_FUN_RETURN(ret, 143 "Failed to initialize UHCI memory structures.\n"); 86 144 87 145 uhci_init_hw(instance); 88 89 instance->cleaner =fibril_create(uhci_interrupt_emulator, instance);90 //fibril_add_ready(instance->cleaner);146 instance->cleaner = 147 fibril_create(uhci_interrupt_emulator, instance); 148 fibril_add_ready(instance->cleaner); 91 149 92 150 instance->debug_checker = fibril_create(uhci_debug_checker, instance); 93 151 fibril_add_ready(instance->debug_checker); 94 152 95 return EOK; 153 usb_log_info("Started UHCI driver.\n"); 154 return EOK; 155 #undef CHECK_RET_DEST_FUN_RETURN 96 156 } 97 157 /*----------------------------------------------------------------------------*/ 98 158 void uhci_init_hw(uhci_t *instance) 99 159 { 160 assert(instance); 161 162 /* reset everything, who knows what touched it before us */ 163 pio_write_16(&instance->registers->usbcmd, UHCI_CMD_GLOBAL_RESET); 164 async_usleep(10000); /* 10ms according to USB spec */ 165 pio_write_16(&instance->registers->usbcmd, 0); 166 167 /* reset hc, all states and counters */ 168 pio_write_16(&instance->registers->usbcmd, UHCI_CMD_HCRESET); 169 while ((pio_read_16(&instance->registers->usbcmd) & UHCI_CMD_HCRESET) != 0) 170 { async_usleep(10); } 100 171 101 172 /* set framelist pointer */ … … 105 176 /* enable all interrupts, but resume interrupt */ 106 177 pio_write_16(&instance->registers->usbintr, 107 178 UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET); 108 179 109 180 /* Start the hc with large(64B) packet FSBR */ 110 181 pio_write_16(&instance->registers->usbcmd, 111 182 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE); 112 usb_log_debug("Started UHCI HC.\n");113 183 } 114 184 /*----------------------------------------------------------------------------*/ … … 116 186 { 117 187 assert(instance); 188 #define CHECK_RET_DEST_CMDS_RETURN(ret, message...) \ 189 if (ret != EOK) { \ 190 usb_log_error(message); \ 191 if (instance->interrupt_code.cmds != NULL) \ 192 free(instance->interrupt_code.cmds); \ 193 return ret; \ 194 } else (void) 0 118 195 119 196 /* 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); 197 instance->interrupt_code.cmds = malloc(sizeof(uhci_cmds)); 198 int ret = (instance->interrupt_code.cmds == NULL) ? ENOMEM : EOK; 199 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to allocate interrupt cmds space.\n"); 200 201 { 202 irq_cmd_t *interrupt_commands = instance->interrupt_code.cmds; 203 memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds)); 204 interrupt_commands[0].addr = (void*)&instance->registers->usbsts; 205 interrupt_commands[1].addr = (void*)&instance->registers->usbsts; 206 instance->interrupt_code.cmdcount = 207 sizeof(uhci_cmds) / sizeof(irq_cmd_t); 208 } 130 209 131 210 /* init transfer lists */ 132 intret = uhci_init_transfer_lists(instance);133 CHECK_RET_ RETURN(ret, "Failed to initialize transfer lists.\n");211 ret = uhci_init_transfer_lists(instance); 212 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to initialize transfer lists.\n"); 134 213 usb_log_debug("Initialized transfer lists.\n"); 135 214 … … 137 216 instance->frame_list = get_page(); 138 217 ret = instance ? EOK : ENOMEM; 139 CHECK_RET_ RETURN(ret, "Failed to get frame list page.\n");218 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to get frame list page.\n"); 140 219 usb_log_debug("Initialized frame list.\n"); 141 220 … … 144 223 instance->transfers_interrupt.queue_head_pa 145 224 | LINK_POINTER_QUEUE_HEAD_FLAG; 225 146 226 unsigned i = 0; 147 227 for(; i < UHCI_FRAME_LIST_COUNT; ++i) { … … 150 230 151 231 /* 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; 232 device_keeper_init(&instance->device_manager); 233 usb_log_debug("Initialized device manager.\n"); 234 235 return EOK; 236 #undef CHECK_RET_DEST_CMDS_RETURN 156 237 } 157 238 /*----------------------------------------------------------------------------*/ … … 159 240 { 160 241 assert(instance); 242 #define CHECK_RET_CLEAR_RETURN(ret, message...) \ 243 if (ret != EOK) { \ 244 usb_log_error(message); \ 245 transfer_list_fini(&instance->transfers_bulk_full); \ 246 transfer_list_fini(&instance->transfers_control_full); \ 247 transfer_list_fini(&instance->transfers_control_slow); \ 248 transfer_list_fini(&instance->transfers_interrupt); \ 249 return ret; \ 250 } else (void) 0 161 251 162 252 /* initialize TODO: check errors */ 163 253 int ret; 164 254 ret = transfer_list_init(&instance->transfers_bulk_full, "BULK_FULL"); 165 assert(ret == EOK); 255 CHECK_RET_CLEAR_RETURN(ret, "Failed to init BULK list."); 256 166 257 ret = transfer_list_init(&instance->transfers_control_full, "CONTROL_FULL"); 167 assert(ret == EOK); 258 CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL FULL list."); 259 168 260 ret = transfer_list_init(&instance->transfers_control_slow, "CONTROL_SLOW"); 169 assert(ret == EOK); 261 CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL SLOW list."); 262 170 263 ret = transfer_list_init(&instance->transfers_interrupt, "INTERRUPT"); 171 assert(ret == EOK);264 CHECK_RET_CLEAR_RETURN(ret, "Failed to init INTERRUPT list."); 172 265 173 266 transfer_list_set_next(&instance->transfers_control_full, … … 196 289 197 290 return EOK; 291 #undef CHECK_RET_CLEAR_RETURN 198 292 } 199 293 /*----------------------------------------------------------------------------*/ … … 202 296 assert(instance); 203 297 assert(batch); 204 const int low_speed = (batch->speed == LOW_SPEED);298 const int low_speed = (batch->speed == USB_SPEED_LOW); 205 299 if (!allowed_usb_packet( 206 300 low_speed, batch->transfer_type, batch->max_packet_size)) { 207 301 usb_log_warning("Invalid USB packet specified %s SPEED %d %zu.\n", 208 302 low_speed ? "LOW" : "FULL" , batch->transfer_type, 209 303 batch->max_packet_size); 210 304 return ENOTSUP; … … 223 317 { 224 318 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); 319 transfer_list_remove_finished(&instance->transfers_interrupt); 320 transfer_list_remove_finished(&instance->transfers_control_slow); 321 transfer_list_remove_finished(&instance->transfers_control_full); 322 transfer_list_remove_finished(&instance->transfers_bulk_full); 232 323 } 233 324 /*----------------------------------------------------------------------------*/ … … 238 329 assert(instance); 239 330 240 while (1) {331 while (1) { 241 332 uint16_t status = pio_read_16(&instance->registers->usbsts); 333 if (status != 0) 334 usb_log_debug2("UHCI status: %x.\n", status); 335 status |= 1; 242 336 uhci_interrupt(instance, status); 243 async_usleep(UHCI_CLEANER_TIMEOUT); 337 pio_write_16(&instance->registers->usbsts, 0x1f); 338 async_usleep(UHCI_CLEANER_TIMEOUT * 5); 244 339 } 245 340 return EOK; … … 250 345 uhci_t *instance = (uhci_t*)arg; 251 346 assert(instance); 347 348 #define QH(queue) \ 349 instance->transfers_##queue.queue_head 350 252 351 while (1) { 253 352 const uint16_t cmd = pio_read_16(&instance->registers->usbcmd); 254 353 const uint16_t sts = pio_read_16(&instance->registers->usbsts); 255 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); 354 const uint16_t intr = 355 pio_read_16(&instance->registers->usbintr); 356 357 if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) { 358 usb_log_debug2("Command: %X Status: %X Intr: %x\n", 359 cmd, sts, intr); 360 } 361 362 uintptr_t frame_list = 363 pio_read_32(&instance->registers->flbaseadd) & ~0xfff; 260 364 if (frame_list != addr_to_phys(instance->frame_list)) { 261 365 usb_log_debug("Framelist address: %p vs. %p.\n", 262 frame_list, addr_to_phys(instance->frame_list)); 263 } 366 frame_list, addr_to_phys(instance->frame_list)); 367 } 368 264 369 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff; 265 370 usb_log_debug2("Framelist item: %d \n", frnum ); 266 371 267 queue_head_t* qh = instance->transfers_interrupt.queue_head;268 269 if ( (instance->frame_list[frnum] & (~0xf)) != (uintptr_t)addr_to_phys(qh)) {372 uintptr_t expected_pa = instance->frame_list[frnum] & (~0xf); 373 uintptr_t real_pa = addr_to_phys(QH(interrupt)); 374 if (expected_pa != real_pa) { 270 375 usb_log_debug("Interrupt QH: %p vs. %p.\n", 271 instance->frame_list[frnum] & (~0xf), addr_to_phys(qh)); 272 } 273 274 if ((qh->next_queue & (~0xf)) 275 != (uintptr_t)addr_to_phys(instance->transfers_control_slow.queue_head)) { 276 usb_log_debug("Control Slow QH: %p vs. %p.\n", qh->next_queue & (~0xf), 277 addr_to_phys(instance->transfers_control_slow.queue_head)); 278 } 279 qh = instance->transfers_control_slow.queue_head; 280 281 if ((qh->next_queue & (~0xf)) 282 != (uintptr_t)addr_to_phys(instance->transfers_control_full.queue_head)) { 283 usb_log_debug("Control Full QH: %p vs. %p.\n", qh->next_queue & (~0xf), 284 addr_to_phys(instance->transfers_control_full.queue_head));\ 285 } 286 qh = instance->transfers_control_full.queue_head; 287 288 if ((qh->next_queue & (~0xf)) 289 != (uintptr_t)addr_to_phys(instance->transfers_bulk_full.queue_head)) { 290 usb_log_debug("Bulk QH: %p vs. %p.\n", qh->next_queue & (~0xf), 291 addr_to_phys(instance->transfers_bulk_full.queue_head)); 292 } 293 /* 294 uint16_t cmd = pio_read_16(&instance->registers->usbcmd); 295 cmd |= UHCI_CMD_RUN_STOP; 296 pio_write_16(&instance->registers->usbcmd, cmd); 297 */ 376 expected_pa, real_pa); 377 } 378 379 expected_pa = QH(interrupt)->next_queue & (~0xf); 380 real_pa = addr_to_phys(QH(control_slow)); 381 if (expected_pa != real_pa) { 382 usb_log_debug("Control Slow QH: %p vs. %p.\n", 383 expected_pa, real_pa); 384 } 385 386 expected_pa = QH(control_slow)->next_queue & (~0xf); 387 real_pa = addr_to_phys(QH(control_full)); 388 if (expected_pa != real_pa) { 389 usb_log_debug("Control Full QH: %p vs. %p.\n", 390 expected_pa, real_pa); 391 } 392 393 expected_pa = QH(control_full)->next_queue & (~0xf); 394 real_pa = addr_to_phys(QH(bulk_full)); 395 if (expected_pa != real_pa ) { 396 usb_log_debug("Bulk QH: %p vs. %p.\n", 397 expected_pa, real_pa); 398 } 298 399 async_usleep(UHCI_DEBUGER_TIMEOUT); 299 400 } 300 401 return 0; 402 #undef QH 301 403 } 302 404 /*----------------------------------------------------------------------------*/ 303 405 bool allowed_usb_packet( 304 406 bool low_speed, usb_transfer_type_t transfer, size_t size) 305 407 { 306 408 /* see USB specification chapter 5.5-5.8 for magic numbers used here */ 307 switch(transfer) { 308 case USB_TRANSFER_ISOCHRONOUS: 309 return (!low_speed && size < 1024); 310 case USB_TRANSFER_INTERRUPT: 311 return size <= (low_speed ? 8 : 64); 312 case USB_TRANSFER_CONTROL: /* device specifies its own max size */ 313 return (size <= (low_speed ? 8 : 64)); 314 case USB_TRANSFER_BULK: /* device specifies its own max size */ 315 return (!low_speed && size <= 64); 409 switch(transfer) 410 { 411 case USB_TRANSFER_ISOCHRONOUS: 412 return (!low_speed && size < 1024); 413 case USB_TRANSFER_INTERRUPT: 414 return size <= (low_speed ? 8 : 64); 415 case USB_TRANSFER_CONTROL: /* device specifies its own max size */ 416 return (size <= (low_speed ? 8 : 64)); 417 case USB_TRANSFER_BULK: /* device specifies its own max size */ 418 return (!low_speed && size <= 64); 316 419 } 317 420 return false;
Note:
See TracChangeset
for help on using the changeset viewer.