source: mainline/uspace/drv/uhci-hcd/uhci.c@ 6bb83c7

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

Rename tracker⇒batch

  • Property mode set to 100644
File size: 9.0 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>
[3cc5ccda]35#include <adt/list.h>
[afcd86e]36
[3515533]37#include <usb/debug.h>
[18e35a7]38#include <usb/usb.h>
[3515533]39
40#include "uhci.h"
41
[881c47b]42static int uhci_init_transfer_lists(uhci_t *instance);
[9ee87f6]43static int uhci_clean_finished(void *arg);
[0535ee4]44static int uhci_debug_checker(void *arg);
[4d73d71]45static bool allowed_usb_packet(
46 bool low_speed, usb_transfer_type_t, size_t size);
[7977fa1]47
[5944244]48int uhci_init(uhci_t *instance, void *regs, size_t reg_size)
[3515533]49{
[5944244]50#define CHECK_RET_RETURN(message...) \
[9ee87f6]51 if (ret != EOK) { \
[afcd86e]52 usb_log_error(message); \
[9ee87f6]53 return ret; \
54 } else (void) 0
[3515533]55
[18e35a7]56 /* init address keeper(libusb) */
[9ee87f6]57 usb_address_keeping_init(&instance->address_manager, USB11_ADDRESS_MAX);
[afcd86e]58 usb_log_debug("Initialized address manager.\n");
[18e35a7]59
[3515533]60 /* allow access to hc control registers */
61 regs_t *io;
[4687fcd4]62 assert(reg_size >= sizeof(regs_t));
[5944244]63 int ret = pio_enable(regs, reg_size, (void**)&io);
64 CHECK_RET_RETURN("Failed to gain access to registers at %p.\n", io);
[3515533]65 instance->registers = io;
[afcd86e]66 usb_log_debug("Device registers accessible.\n");
[3515533]67
[9ee87f6]68 /* init transfer lists */
[881c47b]69 ret = uhci_init_transfer_lists(instance);
[5944244]70 CHECK_RET_RETURN("Failed to initialize transfer lists.\n");
[afcd86e]71 usb_log_debug("Transfer lists initialized.\n");
[9ee87f6]72
[9600516]73
[afcd86e]74 usb_log_debug("Initializing frame list.\n");
[d1984e0]75 instance->frame_list = get_page();
[1256a0a]76 ret = instance ? EOK : ENOMEM;
[5944244]77 CHECK_RET_RETURN("Failed to get frame list page.\n");
[9600516]78
[9ee87f6]79 /* initialize all frames to point to the first queue head */
80 const uint32_t queue =
[881c47b]81 instance->transfers_interrupt.queue_head_pa
[9ee87f6]82 | LINK_POINTER_QUEUE_HEAD_FLAG;
[5944244]83 unsigned i = 0;
[9ee87f6]84 for(; i < UHCI_FRAME_LIST_COUNT; ++i) {
85 instance->frame_list[i] = queue;
86 }
87
[9600516]88 const uintptr_t pa = (uintptr_t)addr_to_phys(instance->frame_list);
89 pio_write_32(&instance->registers->flbaseadd, (uint32_t)pa);
90
[83c439c]91 list_initialize(&instance->batch_list);
92 fibril_mutex_initialize(&instance->batch_list_mutex);
[9a818a9]93
[9ee87f6]94 instance->cleaner = fibril_create(uhci_clean_finished, instance);
95 fibril_add_ready(instance->cleaner);
[3515533]96
[0535ee4]97 instance->debug_checker = fibril_create(uhci_debug_checker, instance);
98 fibril_add_ready(instance->debug_checker);
99
[3f189c5]100 /* Start the hc with large(64B) packet FSBR */
[5944244]101 pio_write_16(&instance->registers->usbcmd,
102 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET);
103 usb_log_debug("Started UHCI HC.\n");
[881c47b]104
[d1984e0]105 uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
[3da5eb9]106 cmd |= UHCI_CMD_DEBUG;
[d1984e0]107 pio_write_16(&instance->registers->usbcmd, cmd);
[881c47b]108
[3515533]109 return EOK;
110}
111/*----------------------------------------------------------------------------*/
[881c47b]112int uhci_init_transfer_lists(uhci_t *instance)
[643b983]113{
[881c47b]114 assert(instance);
[b00163f]115
[881c47b]116 /* initialize */
[643b983]117 int ret;
[881c47b]118 ret = transfer_list_init(&instance->transfers_bulk_full, "BULK_FULL");
119 assert(ret == EOK);
120 ret = transfer_list_init(&instance->transfers_control_full, "CONTROL_FULL");
121 assert(ret == EOK);
122 ret = transfer_list_init(&instance->transfers_control_slow, "CONTROL_SLOW");
123 assert(ret == EOK);
124 ret = transfer_list_init(&instance->transfers_interrupt, "INTERRUPT");
125 assert(ret == EOK);
126
127 transfer_list_set_next(&instance->transfers_control_full,
128 &instance->transfers_bulk_full);
129 transfer_list_set_next(&instance->transfers_control_slow,
130 &instance->transfers_control_full);
131 transfer_list_set_next(&instance->transfers_interrupt,
132 &instance->transfers_control_slow);
133
134 /*FSBR*/
135#ifdef FSBR
136 transfer_list_set_next(&instance->transfers_bulk_full,
137 &instance->transfers_control_full);
138#endif
139
140 instance->transfers[0][USB_TRANSFER_INTERRUPT] =
141 &instance->transfers_interrupt;
142 instance->transfers[1][USB_TRANSFER_INTERRUPT] =
143 &instance->transfers_interrupt;
144 instance->transfers[0][USB_TRANSFER_CONTROL] =
145 &instance->transfers_control_full;
146 instance->transfers[1][USB_TRANSFER_CONTROL] =
147 &instance->transfers_control_slow;
148 instance->transfers[0][USB_TRANSFER_CONTROL] =
149 &instance->transfers_control_full;
[643b983]150
151 return EOK;
152}
[b00163f]153/*----------------------------------------------------------------------------*/
[83c439c]154int uhci_schedule(uhci_t *instance, batch_t *batch)
[9a818a9]155{
156 assert(instance);
[83c439c]157 assert(batch);
158 const int low_speed = (batch->speed == LOW_SPEED);
[9a818a9]159 if (!allowed_usb_packet(
[83c439c]160 low_speed, batch->transfer_type, batch->max_packet_size)) {
[9a818a9]161 usb_log_warning("Invalid USB packet specified %s SPEED %d %zu.\n",
[83c439c]162 low_speed ? "LOW" : "FULL" , batch->transfer_type,
163 batch->max_packet_size);
[9a818a9]164 return ENOTSUP;
165 }
166 /* TODO: check available bandwith here */
167
168 transfer_list_t *list =
[83c439c]169 instance->transfers[low_speed][batch->transfer_type];
[9a818a9]170 assert(list);
[83c439c]171 transfer_list_add_batch(list, batch);
[3cc5ccda]172
[9a818a9]173 return EOK;
174}
175/*----------------------------------------------------------------------------*/
[9ee87f6]176int uhci_clean_finished(void* arg)
177{
[afcd86e]178 usb_log_debug("Started cleaning fibril.\n");
[9ee87f6]179 uhci_t *instance = (uhci_t*)arg;
180 assert(instance);
[579dec2]181
[9ee87f6]182 while(1) {
[7dd3318]183 transfer_list_check(&instance->transfers_interrupt);
184 transfer_list_check(&instance->transfers_control_slow);
185 transfer_list_check(&instance->transfers_control_full);
186 transfer_list_check(&instance->transfers_bulk_full);
[0535ee4]187 async_usleep(UHCI_CLEANER_TIMEOUT);
[9ee87f6]188 }
189 return EOK;
190}
[0535ee4]191/*---------------------------------------------------------------------------*/
192int uhci_debug_checker(void *arg)
193{
194 uhci_t *instance = (uhci_t*)arg;
195 assert(instance);
196 while (1) {
[5286a2c]197 uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
198 uint16_t sts = pio_read_16(&instance->registers->usbsts);
[afcd86e]199 usb_log_debug("Command register: %X Status register: %X\n", cmd, sts);
[881c47b]200
[0535ee4]201 uintptr_t frame_list = pio_read_32(&instance->registers->flbaseadd);
[d6f78857]202 if (frame_list != (uintptr_t)addr_to_phys(instance->frame_list)) {
203 usb_log_debug("Framelist address: %p vs. %p.\n",
204 frame_list, addr_to_phys(instance->frame_list));
205 }
[0535ee4]206 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
[d6f78857]207 usb_log_debug2("Framelist item: %d \n", frnum );
[0535ee4]208
[881c47b]209 queue_head_t* qh = instance->transfers_interrupt.queue_head;
210
211 if ((instance->frame_list[frnum] & (~0xf)) != (uintptr_t)addr_to_phys(qh)) {
212 usb_log_debug("Interrupt QH: %p vs. %p.\n",
213 instance->frame_list[frnum] & (~0xf), addr_to_phys(qh));
214 }
215
216 if ((qh->next_queue & (~0xf))
217 != (uintptr_t)addr_to_phys(instance->transfers_control_slow.queue_head)) {
218 usb_log_debug("Control Slow QH: %p vs. %p.\n", qh->next_queue & (~0xf),
219 addr_to_phys(instance->transfers_control_slow.queue_head));
220 }
221 qh = instance->transfers_control_slow.queue_head;
[0535ee4]222
[881c47b]223 if ((qh->next_queue & (~0xf))
224 != (uintptr_t)addr_to_phys(instance->transfers_control_full.queue_head)) {
225 usb_log_debug("Control Full QH: %p vs. %p.\n", qh->next_queue & (~0xf),
226 addr_to_phys(instance->transfers_control_full.queue_head));\
227 }
228 qh = instance->transfers_control_full.queue_head;
[0535ee4]229
[881c47b]230 if ((qh->next_queue & (~0xf))
231 != (uintptr_t)addr_to_phys(instance->transfers_bulk_full.queue_head)) {
232 usb_log_debug("Bulk QH: %p vs. %p.\n", qh->next_queue & (~0xf),
233 addr_to_phys(instance->transfers_bulk_full.queue_head));
234 }
235/*
[3da5eb9]236 uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
237 cmd |= UHCI_CMD_RUN_STOP;
238 pio_write_16(&instance->registers->usbcmd, cmd);
239*/
[0535ee4]240 async_usleep(UHCI_DEBUGER_TIMEOUT);
241 }
242 return 0;
243}
[4d73d71]244/*----------------------------------------------------------------------------*/
245bool allowed_usb_packet(
246 bool low_speed, usb_transfer_type_t transfer, size_t size)
247{
248 /* see USB specification chapter 5.5-5.8 for magic numbers used here */
249 switch(transfer) {
250 case USB_TRANSFER_ISOCHRONOUS:
251 return (!low_speed && size < 1024);
252 case USB_TRANSFER_INTERRUPT:
[3f189c5]253 return size <= (low_speed ? 8 : 64);
[4d73d71]254 case USB_TRANSFER_CONTROL: /* device specifies its own max size */
255 return (size <= (low_speed ? 8 : 64));
256 case USB_TRANSFER_BULK: /* device specifies its own max size */
257 return (!low_speed && size <= 64);
258 }
259 return false;
260}
[c56dbe0]261/**
262 * @}
263 */
Note: See TracBrowser for help on using the repository browser.