source: mainline/uspace/drv/uhci-hcd/uhci.c@ 67352d2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 67352d2 was 67352d2, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

Debug output fixes and refactoring (less spam, more readability)

  • Property mode set to 100644
File size: 15.7 KB
RevLine 
[c56dbe0]1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28/** @addtogroup usb
29 * @{
30 */
31/** @file
32 * @brief UHCI driver
33 */
[3515533]34#include <errno.h>
[eb1a2f4]35#include <str_error.h>
[3cc5ccda]36#include <adt/list.h>
[eb1a2f4]37#include <libarch/ddi.h>
[afcd86e]38
[3515533]39#include <usb/debug.h>
[18e35a7]40#include <usb/usb.h>
[eb1a2f4]41#include <usb/ddfiface.h>
42#include <usb_iface.h>
[3515533]43
44#include "uhci.h"
[eb1a2f4]45#include "iface.h"
46
[b9d910f]47static irq_cmd_t uhci_cmds[] = {
48 {
49 .cmd = CMD_PIO_READ_16,
[1ae51ae]50 .addr = NULL, /* patched for every instance */
[b9d910f]51 .dstarg = 1
52 },
53 {
54 .cmd = CMD_PIO_WRITE_16,
[1ae51ae]55 .addr = NULL, /* pathed for every instance */
[b9d910f]56 .value = 0x1f
57 },
58 {
59 .cmd = CMD_ACCEPT
60 }
61};
[3515533]62
[a7e2f0d]63/** Gets USB address of the calling device.
64 *
65 * @param[in] fun UHCI hc function.
66 * @param[in] handle Handle of the device seeking address.
67 * @param[out] address Place to store found address.
68 * @return Error code.
69 */
70static int usb_iface_get_address(
71 ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address)
[eb1a2f4]72{
73 assert(fun);
74 uhci_t *hc = fun_to_uhci(fun);
75 assert(hc);
76
[1a93bb0]77 usb_address_t addr = device_keeper_find(&hc->device_manager,
[eb1a2f4]78 handle);
79 if (addr < 0) {
80 return addr;
81 }
82
83 if (address != NULL) {
84 *address = addr;
85 }
86
87 return EOK;
88}
[6cbe7dad]89/*----------------------------------------------------------------------------*/
[eb1a2f4]90static usb_iface_t hc_usb_iface = {
91 .get_hc_handle = usb_iface_get_hc_handle_hc_impl,
92 .get_address = usb_iface_get_address
93};
94/*----------------------------------------------------------------------------*/
95static ddf_dev_ops_t uhci_ops = {
96 .interfaces[USB_DEV_IFACE] = &hc_usb_iface,
[6cbe7dad]97 .interfaces[USBHC_DEV_IFACE] = &uhci_iface,
[eb1a2f4]98};
[b375bb8]99/*----------------------------------------------------------------------------*/
[881c47b]100static int uhci_init_transfer_lists(uhci_t *instance);
[f4c87aa9]101static int uhci_init_mem_structures(uhci_t *instance);
102static void uhci_init_hw(uhci_t *instance);
103
[30a4301]104static int uhci_interrupt_emulator(void *arg);
[0535ee4]105static int uhci_debug_checker(void *arg);
[f4c87aa9]106
[4d73d71]107static bool allowed_usb_packet(
[a7e2f0d]108 bool low_speed, usb_transfer_type_t transfer, size_t size);
109/*----------------------------------------------------------------------------*/
110/** Initializes UHCI hcd driver structure
111 *
112 * @param[in] instance Memory place to initialize.
113 * @param[in] dev DDF device.
114 * @param[in] regs Address of I/O control registers.
115 * @param[in] size Size of I/O control registers.
116 * @return Error code.
117 * @note Should be called only once on any structure.
118 */
[eb1a2f4]119int uhci_init(uhci_t *instance, ddf_dev_t *dev, void *regs, size_t reg_size)
[f4c87aa9]120{
[733a9a8]121 assert(reg_size >= sizeof(regs_t));
[eb1a2f4]122 int ret;
123
[4abc304]124#define CHECK_RET_DEST_FUN_RETURN(ret, message...) \
125 if (ret != EOK) { \
126 usb_log_error(message); \
127 if (instance->ddf_instance) \
128 ddf_fun_destroy(instance->ddf_instance); \
129 return ret; \
130 } else (void) 0
131
[b375bb8]132 /* Create UHCI function. */
[eb1a2f4]133 instance->ddf_instance = ddf_fun_create(dev, fun_exposed, "uhci");
[4abc304]134 ret = (instance->ddf_instance == NULL) ? ENOMEM : EOK;
[b375bb8]135 CHECK_RET_DEST_FUN_RETURN(ret,
136 "Failed to create UHCI device function.\n");
[4abc304]137
[eb1a2f4]138 instance->ddf_instance->ops = &uhci_ops;
139 instance->ddf_instance->driver_data = instance;
140
141 ret = ddf_fun_bind(instance->ddf_instance);
[b375bb8]142 CHECK_RET_DEST_FUN_RETURN(ret,
143 "Failed(%d) to bind UHCI device function: %s.\n",
[4abc304]144 ret, str_error(ret));
[18e35a7]145
[3515533]146 /* allow access to hc control registers */
147 regs_t *io;
[eb1a2f4]148 ret = pio_enable(regs, reg_size, (void**)&io);
[b375bb8]149 CHECK_RET_DEST_FUN_RETURN(ret,
150 "Failed(%d) to gain access to registers at %p: %s.\n",
[4abc304]151 ret, str_error(ret), io);
[3515533]152 instance->registers = io;
[b375bb8]153 usb_log_debug("Device registers at %p(%u) accessible.\n",
154 io, reg_size);
[3515533]155
[733a9a8]156 ret = uhci_init_mem_structures(instance);
[b375bb8]157 CHECK_RET_DEST_FUN_RETURN(ret,
158 "Failed to initialize UHCI memory structures.\n");
[733a9a8]159
160 uhci_init_hw(instance);
[b375bb8]161 instance->cleaner =
162 fibril_create(uhci_interrupt_emulator, instance);
[3de48b5]163 fibril_add_ready(instance->cleaner);
[f4c87aa9]164
165 instance->debug_checker = fibril_create(uhci_debug_checker, instance);
166 fibril_add_ready(instance->debug_checker);
167
[4abc304]168 usb_log_info("Started UHCI driver.\n");
[f4c87aa9]169 return EOK;
[4abc304]170#undef CHECK_RET_DEST_FUN_RETURN
[f4c87aa9]171}
172/*----------------------------------------------------------------------------*/
[a7e2f0d]173/** Initializes UHCI hcd hw resources.
174 *
175 * @param[in] instance UHCI structure to use.
176 */
[f4c87aa9]177void uhci_init_hw(uhci_t *instance)
178{
[b375bb8]179 assert(instance);
[a7e2f0d]180 regs_t *registers = instance->registers;
[b375bb8]181
[a7e2f0d]182 /* Reset everything, who knows what touched it before us */
183 pio_write_16(&registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
[cabda7f]184 async_usleep(10000); /* 10ms according to USB spec */
[a7e2f0d]185 pio_write_16(&registers->usbcmd, 0);
[733a9a8]186
[a7e2f0d]187 /* Reset hc, all states and counters */
188 pio_write_16(&registers->usbcmd, UHCI_CMD_HCRESET);
[57c0a7e]189 do { async_usleep(10); }
[a7e2f0d]190 while ((pio_read_16(&registers->usbcmd) & UHCI_CMD_HCRESET) != 0);
[eb292a0]191
[a7e2f0d]192 /* Set framelist pointer */
[36a4738]193 const uint32_t pa = addr_to_phys(instance->frame_list);
[a7e2f0d]194 pio_write_32(&registers->flbaseadd, pa);
[36a4738]195
[a7e2f0d]196 /* Enable all interrupts, but resume interrupt */
[57c0a7e]197// pio_write_16(&instance->registers->usbintr,
198// UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET);
[f4c87aa9]199
[a7e2f0d]200 uint16_t status = pio_read_16(&registers->usbcmd);
201 if (status != 0)
202 usb_log_warning("Previous command value: %x.\n", status);
203
[f4c87aa9]204 /* Start the hc with large(64B) packet FSBR */
[a7e2f0d]205 pio_write_16(&registers->usbcmd,
[733a9a8]206 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
[f4c87aa9]207}
208/*----------------------------------------------------------------------------*/
[a7e2f0d]209/** Initializes UHCI hcd memory structures.
210 *
211 * @param[in] instance UHCI structure to use.
212 * @return Error code
213 * @note Should be called only once on any structure.
214 */
[f4c87aa9]215int uhci_init_mem_structures(uhci_t *instance)
216{
217 assert(instance);
[4abc304]218#define CHECK_RET_DEST_CMDS_RETURN(ret, message...) \
219 if (ret != EOK) { \
220 usb_log_error(message); \
221 if (instance->interrupt_code.cmds != NULL) \
222 free(instance->interrupt_code.cmds); \
223 return ret; \
224 } else (void) 0
[b9d910f]225
[a7e2f0d]226 /* Init interrupt code */
[4abc304]227 instance->interrupt_code.cmds = malloc(sizeof(uhci_cmds));
228 int ret = (instance->interrupt_code.cmds == NULL) ? ENOMEM : EOK;
[a7e2f0d]229 CHECK_RET_DEST_CMDS_RETURN(ret,
230 "Failed to allocate interrupt cmds space.\n");
[4abc304]231
232 {
233 irq_cmd_t *interrupt_commands = instance->interrupt_code.cmds;
234 memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds));
[a7e2f0d]235 interrupt_commands[0].addr =
236 (void*)&instance->registers->usbsts;
237 interrupt_commands[1].addr =
238 (void*)&instance->registers->usbsts;
[4abc304]239 instance->interrupt_code.cmdcount =
[b375bb8]240 sizeof(uhci_cmds) / sizeof(irq_cmd_t);
[b9d910f]241 }
242
[a7e2f0d]243 /* Init transfer lists */
[4abc304]244 ret = uhci_init_transfer_lists(instance);
[a7e2f0d]245 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to init transfer lists.\n");
[acce4fc]246 usb_log_debug("Initialized transfer lists.\n");
[9ee87f6]247
[a7e2f0d]248 /* Init USB frame list page*/
[d1984e0]249 instance->frame_list = get_page();
[1256a0a]250 ret = instance ? EOK : ENOMEM;
[4abc304]251 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to get frame list page.\n");
[acce4fc]252 usb_log_debug("Initialized frame list.\n");
[9600516]253
[a7e2f0d]254 /* Set all frames to point to the first queue head */
[9ee87f6]255 const uint32_t queue =
[881c47b]256 instance->transfers_interrupt.queue_head_pa
[9ee87f6]257 | LINK_POINTER_QUEUE_HEAD_FLAG;
[4abc304]258
[5944244]259 unsigned i = 0;
[9ee87f6]260 for(; i < UHCI_FRAME_LIST_COUNT; ++i) {
261 instance->frame_list[i] = queue;
262 }
263
[a7e2f0d]264 /* Init device keeper*/
[1a93bb0]265 device_keeper_init(&instance->device_manager);
266 usb_log_debug("Initialized device manager.\n");
[881c47b]267
[3515533]268 return EOK;
[4abc304]269#undef CHECK_RET_DEST_CMDS_RETURN
[3515533]270}
271/*----------------------------------------------------------------------------*/
[a7e2f0d]272/** Initializes UHCI hcd transfer lists.
273 *
274 * @param[in] instance UHCI structure to use.
275 * @return Error code
276 * @note Should be called only once on any structure.
277 */
[881c47b]278int uhci_init_transfer_lists(uhci_t *instance)
[643b983]279{
[881c47b]280 assert(instance);
[4abc304]281#define CHECK_RET_CLEAR_RETURN(ret, message...) \
282 if (ret != EOK) { \
283 usb_log_error(message); \
284 transfer_list_fini(&instance->transfers_bulk_full); \
285 transfer_list_fini(&instance->transfers_control_full); \
286 transfer_list_fini(&instance->transfers_control_slow); \
287 transfer_list_fini(&instance->transfers_interrupt); \
288 return ret; \
289 } else (void) 0
[b00163f]290
[f4c87aa9]291 /* initialize TODO: check errors */
[643b983]292 int ret;
[881c47b]293 ret = transfer_list_init(&instance->transfers_bulk_full, "BULK_FULL");
[4abc304]294 CHECK_RET_CLEAR_RETURN(ret, "Failed to init BULK list.");
[b375bb8]295
[a7e2f0d]296 ret = transfer_list_init(
297 &instance->transfers_control_full, "CONTROL_FULL");
[4abc304]298 CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL FULL list.");
[b375bb8]299
[a7e2f0d]300 ret = transfer_list_init(
301 &instance->transfers_control_slow, "CONTROL_SLOW");
[4abc304]302 CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL SLOW list.");
[b375bb8]303
[881c47b]304 ret = transfer_list_init(&instance->transfers_interrupt, "INTERRUPT");
[4abc304]305 CHECK_RET_CLEAR_RETURN(ret, "Failed to init INTERRUPT list.");
[881c47b]306
307 transfer_list_set_next(&instance->transfers_control_full,
308 &instance->transfers_bulk_full);
309 transfer_list_set_next(&instance->transfers_control_slow,
310 &instance->transfers_control_full);
311 transfer_list_set_next(&instance->transfers_interrupt,
312 &instance->transfers_control_slow);
313
314 /*FSBR*/
315#ifdef FSBR
316 transfer_list_set_next(&instance->transfers_bulk_full,
317 &instance->transfers_control_full);
318#endif
319
[a7e2f0d]320 /* Assign pointers to be used during scheduling */
321 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
[881c47b]322 &instance->transfers_interrupt;
[a7e2f0d]323 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
[881c47b]324 &instance->transfers_interrupt;
[a7e2f0d]325 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
[881c47b]326 &instance->transfers_control_full;
[a7e2f0d]327 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
[881c47b]328 &instance->transfers_control_slow;
[a7e2f0d]329 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
[f4c87aa9]330 &instance->transfers_bulk_full;
[643b983]331
332 return EOK;
[4abc304]333#undef CHECK_RET_CLEAR_RETURN
[643b983]334}
[b00163f]335/*----------------------------------------------------------------------------*/
[a7e2f0d]336/** Schedules batch for execution.
337 *
338 * @param[in] instance UHCI structure to use.
339 * @param[in] batch Transfer batch to schedule.
340 * @return Error code
341 */
[83c439c]342int uhci_schedule(uhci_t *instance, batch_t *batch)
[9a818a9]343{
344 assert(instance);
[83c439c]345 assert(batch);
[1a93bb0]346 const int low_speed = (batch->speed == USB_SPEED_LOW);
[9a818a9]347 if (!allowed_usb_packet(
[83c439c]348 low_speed, batch->transfer_type, batch->max_packet_size)) {
[a7e2f0d]349 usb_log_warning(
350 "Invalid USB packet specified %s SPEED %d %zu.\n",
[b375bb8]351 low_speed ? "LOW" : "FULL" , batch->transfer_type,
[83c439c]352 batch->max_packet_size);
[9a818a9]353 return ENOTSUP;
354 }
355 /* TODO: check available bandwith here */
356
357 transfer_list_t *list =
[71b6e92]358 instance->transfers[batch->speed][batch->transfer_type];
[9a818a9]359 assert(list);
[83c439c]360 transfer_list_add_batch(list, batch);
[3cc5ccda]361
[9a818a9]362 return EOK;
363}
364/*----------------------------------------------------------------------------*/
[a7e2f0d]365/** Takes action based on the interrupt cause.
366 *
367 * @param[in] instance UHCI structure to use.
368 * @param[in] status Value of the stsatus regiser at the time of interrupt.
369 */
[733a9a8]370void uhci_interrupt(uhci_t *instance, uint16_t status)
[9ee87f6]371{
[30a4301]372 assert(instance);
[a7e2f0d]373 /* TODO: Check interrupt cause here */
[0db3ad6]374 /* Lower 2 bits are transaction error and transaction complete */
375 if (status & 0x3) {
376 transfer_list_remove_finished(&instance->transfers_interrupt);
377 transfer_list_remove_finished(&instance->transfers_control_slow);
378 transfer_list_remove_finished(&instance->transfers_control_full);
379 transfer_list_remove_finished(&instance->transfers_bulk_full);
380 }
[30a4301]381}
382/*----------------------------------------------------------------------------*/
[a7e2f0d]383/** Polling function, emulates interrupts.
384 *
385 * @param[in] arg UHCI structure to use.
386 * @return EOK
387 */
[30a4301]388int uhci_interrupt_emulator(void* arg)
389{
390 usb_log_debug("Started interrupt emulator.\n");
[9ee87f6]391 uhci_t *instance = (uhci_t*)arg;
392 assert(instance);
[579dec2]393
[86c2ccd]394 while (1) {
[0db3ad6]395 /* read and ack interrupts */
[733a9a8]396 uint16_t status = pio_read_16(&instance->registers->usbsts);
[0db3ad6]397 pio_write_16(&instance->registers->usbsts, 0x1f);
[48563a3]398 if (status != 0)
399 usb_log_debug2("UHCI status: %x.\n", status);
[733a9a8]400 uhci_interrupt(instance, status);
[3c775adb]401 async_usleep(UHCI_CLEANER_TIMEOUT);
[9ee87f6]402 }
403 return EOK;
404}
[0535ee4]405/*---------------------------------------------------------------------------*/
[a7e2f0d]406/** Debug function, checks consistency of memory structures.
407 *
408 * @param[in] arg UHCI structure to use.
409 * @return EOK
410 */
[0535ee4]411int uhci_debug_checker(void *arg)
412{
413 uhci_t *instance = (uhci_t*)arg;
414 assert(instance);
[b375bb8]415
416#define QH(queue) \
417 instance->transfers_##queue.queue_head
418
[0535ee4]419 while (1) {
[36a4738]420 const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
421 const uint16_t sts = pio_read_16(&instance->registers->usbsts);
[b375bb8]422 const uint16_t intr =
423 pio_read_16(&instance->registers->usbintr);
424
[3de48b5]425 if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
426 usb_log_debug2("Command: %X Status: %X Intr: %x\n",
427 cmd, sts, intr);
428 }
[881c47b]429
[cabda7f]430 uintptr_t frame_list =
431 pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
[733a9a8]432 if (frame_list != addr_to_phys(instance->frame_list)) {
[d6f78857]433 usb_log_debug("Framelist address: %p vs. %p.\n",
[b375bb8]434 frame_list, addr_to_phys(instance->frame_list));
[d6f78857]435 }
[b375bb8]436
[0535ee4]437 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
438
[b375bb8]439 uintptr_t expected_pa = instance->frame_list[frnum] & (~0xf);
440 uintptr_t real_pa = addr_to_phys(QH(interrupt));
441 if (expected_pa != real_pa) {
[67352d2]442 usb_log_debug("Interrupt QH: %p(frame: %d) vs. %p.\n",
443 expected_pa, frnum, real_pa);
[881c47b]444 }
445
[b375bb8]446 expected_pa = QH(interrupt)->next_queue & (~0xf);
447 real_pa = addr_to_phys(QH(control_slow));
448 if (expected_pa != real_pa) {
449 usb_log_debug("Control Slow QH: %p vs. %p.\n",
450 expected_pa, real_pa);
[881c47b]451 }
[0535ee4]452
[b375bb8]453 expected_pa = QH(control_slow)->next_queue & (~0xf);
454 real_pa = addr_to_phys(QH(control_full));
455 if (expected_pa != real_pa) {
456 usb_log_debug("Control Full QH: %p vs. %p.\n",
457 expected_pa, real_pa);
[881c47b]458 }
[0535ee4]459
[b375bb8]460 expected_pa = QH(control_full)->next_queue & (~0xf);
461 real_pa = addr_to_phys(QH(bulk_full));
462 if (expected_pa != real_pa ) {
463 usb_log_debug("Bulk QH: %p vs. %p.\n",
464 expected_pa, real_pa);
[881c47b]465 }
[0535ee4]466 async_usleep(UHCI_DEBUGER_TIMEOUT);
467 }
[a7e2f0d]468 return EOK;
[b375bb8]469#undef QH
[0535ee4]470}
[4d73d71]471/*----------------------------------------------------------------------------*/
[a7e2f0d]472/** Checks transfer packets, for USB validity
473 *
474 * @param[in] low_speed Transfer speed.
475 * @param[in] transfer Transer type
476 * @param[in] size Maximum size of used packets
477 * @return EOK
478 */
[4d73d71]479bool allowed_usb_packet(
[b375bb8]480 bool low_speed, usb_transfer_type_t transfer, size_t size)
[4d73d71]481{
482 /* see USB specification chapter 5.5-5.8 for magic numbers used here */
[b375bb8]483 switch(transfer)
484 {
485 case USB_TRANSFER_ISOCHRONOUS:
486 return (!low_speed && size < 1024);
487 case USB_TRANSFER_INTERRUPT:
488 return size <= (low_speed ? 8 : 64);
489 case USB_TRANSFER_CONTROL: /* device specifies its own max size */
490 return (size <= (low_speed ? 8 : 64));
491 case USB_TRANSFER_BULK: /* device specifies its own max size */
492 return (!low_speed && size <= 64);
[4d73d71]493 }
494 return false;
495}
[c56dbe0]496/**
497 * @}
498 */
Note: See TracBrowser for help on using the repository browser.