source: mainline/uspace/drv/uhci-hcd/uhci.c@ 3cc5ccda

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

Use mutex to guard the tracker_list
Do not resolve finished trackers immediately, create list for postprocessing instead (fixes list contention)

  • Property mode set to 100644
File size: 10.6 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
[9a818a9]91 list_initialize(&instance->tracker_list);
[3cc5ccda]92 fibril_mutex_initialize(&instance->tracker_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/*----------------------------------------------------------------------------*/
[9a818a9]154int uhci_schedule(uhci_t *instance, tracker_t *tracker)
155{
156 assert(instance);
157 assert(tracker);
158 const int low_speed = (tracker->speed == LOW_SPEED);
159 if (!allowed_usb_packet(
160 low_speed, tracker->transfer_type, tracker->packet_size)) {
161 usb_log_warning("Invalid USB packet specified %s SPEED %d %zu.\n",
162 low_speed ? "LOW" : "FULL" , tracker->transfer_type,
163 tracker->packet_size);
164 return ENOTSUP;
165 }
166 /* TODO: check available bandwith here */
167
[3cc5ccda]168 usb_log_debug2("Scheduler(%d) acquiring tracker list mutex.\n",
169 fibril_get_id());
170 fibril_mutex_lock(&instance->tracker_list_mutex);
171 usb_log_debug2("Scheduler(%d) acquired tracker list mutex.\n",
172 fibril_get_id());
173
[9a818a9]174 transfer_list_t *list =
175 instance->transfers[low_speed][tracker->transfer_type];
176 assert(list);
177 transfer_list_add_tracker(list, tracker);
178 list_append(&tracker->link, &instance->tracker_list);
179
[3cc5ccda]180 usb_log_debug2("Scheduler(%d) releasing tracker list mutex.\n",
181 fibril_get_id());
182 fibril_mutex_unlock(&instance->tracker_list_mutex);
183 usb_log_debug2("Scheduler(%d) released tracker list mutex.\n",
184 fibril_get_id());
185
[9a818a9]186 return EOK;
187}
188/*----------------------------------------------------------------------------*/
[9ee87f6]189int uhci_clean_finished(void* arg)
190{
[afcd86e]191 usb_log_debug("Started cleaning fibril.\n");
[9ee87f6]192 uhci_t *instance = (uhci_t*)arg;
193 assert(instance);
[579dec2]194
[9ee87f6]195 while(1) {
[3cc5ccda]196 LIST_INITIALIZE(done_trackers);
[9a818a9]197 /* tracker iteration */
[3cc5ccda]198
199 usb_log_debug2("Cleaner(%d) acquiring tracker list mutex.\n",
200 fibril_get_id());
201 fibril_mutex_lock(&instance->tracker_list_mutex);
202 usb_log_debug2("Cleaner(%d) acquired tracker list mutex.\n",
203 fibril_get_id());
204
[9a818a9]205 link_t *current = instance->tracker_list.next;
206 while (current != &instance->tracker_list)
207 {
[3cc5ccda]208
[9a818a9]209 link_t *next = current->next;
210 tracker_t *tracker = list_get_instance(current, tracker_t, link);
[f241b05b]211
[9a818a9]212 assert(current == &tracker->link);
[f241b05b]213 assert(tracker);
214 assert(tracker->next_step);
215 assert(tracker->td);
216
[9a818a9]217 if (!transfer_descriptor_is_active(tracker->td)) {
[2b4dbd1]218 usb_log_info("Found inactive tracker with status: %x:%x.\n",
219 tracker->td->status, tracker->td->device);
[9a818a9]220 list_remove(current);
[3cc5ccda]221 list_append(current, &done_trackers);
[9a818a9]222 }
223 current = next;
224 }
[3cc5ccda]225
226 usb_log_debug2("Cleaner(%d) releasing tracker list mutex.\n",
227 fibril_get_id());
228 fibril_mutex_unlock(&instance->tracker_list_mutex);
229 usb_log_debug2("Cleaner(%d) released tracker list mutex.\n",
230 fibril_get_id());
231
232 while (!list_empty(&done_trackers)) {
233 tracker_t *tracker = list_get_instance(
234 done_trackers.next, tracker_t, link);
235 list_remove(&tracker->link);
236 tracker->next_step(tracker);
237 }
[0535ee4]238 async_usleep(UHCI_CLEANER_TIMEOUT);
[9ee87f6]239 }
240 return EOK;
241}
[0535ee4]242/*---------------------------------------------------------------------------*/
243int uhci_debug_checker(void *arg)
244{
245 uhci_t *instance = (uhci_t*)arg;
246 assert(instance);
247 while (1) {
[5286a2c]248 uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
249 uint16_t sts = pio_read_16(&instance->registers->usbsts);
[afcd86e]250 usb_log_debug("Command register: %X Status register: %X\n", cmd, sts);
[881c47b]251
[0535ee4]252 uintptr_t frame_list = pio_read_32(&instance->registers->flbaseadd);
[d6f78857]253 if (frame_list != (uintptr_t)addr_to_phys(instance->frame_list)) {
254 usb_log_debug("Framelist address: %p vs. %p.\n",
255 frame_list, addr_to_phys(instance->frame_list));
256 }
[0535ee4]257 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
[d6f78857]258 usb_log_debug2("Framelist item: %d \n", frnum );
[0535ee4]259
[881c47b]260 queue_head_t* qh = instance->transfers_interrupt.queue_head;
261
262 if ((instance->frame_list[frnum] & (~0xf)) != (uintptr_t)addr_to_phys(qh)) {
263 usb_log_debug("Interrupt QH: %p vs. %p.\n",
264 instance->frame_list[frnum] & (~0xf), addr_to_phys(qh));
265 }
266
267 if ((qh->next_queue & (~0xf))
268 != (uintptr_t)addr_to_phys(instance->transfers_control_slow.queue_head)) {
269 usb_log_debug("Control Slow QH: %p vs. %p.\n", qh->next_queue & (~0xf),
270 addr_to_phys(instance->transfers_control_slow.queue_head));
271 }
272 qh = instance->transfers_control_slow.queue_head;
[0535ee4]273
[881c47b]274 if ((qh->next_queue & (~0xf))
275 != (uintptr_t)addr_to_phys(instance->transfers_control_full.queue_head)) {
276 usb_log_debug("Control Full QH: %p vs. %p.\n", qh->next_queue & (~0xf),
277 addr_to_phys(instance->transfers_control_full.queue_head));\
278 }
279 qh = instance->transfers_control_full.queue_head;
[0535ee4]280
[881c47b]281 if ((qh->next_queue & (~0xf))
282 != (uintptr_t)addr_to_phys(instance->transfers_bulk_full.queue_head)) {
283 usb_log_debug("Bulk QH: %p vs. %p.\n", qh->next_queue & (~0xf),
284 addr_to_phys(instance->transfers_bulk_full.queue_head));
285 }
286/*
[3da5eb9]287 uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
288 cmd |= UHCI_CMD_RUN_STOP;
289 pio_write_16(&instance->registers->usbcmd, cmd);
290*/
[0535ee4]291 async_usleep(UHCI_DEBUGER_TIMEOUT);
292 }
293 return 0;
294}
[4d73d71]295/*----------------------------------------------------------------------------*/
296bool allowed_usb_packet(
297 bool low_speed, usb_transfer_type_t transfer, size_t size)
298{
299 /* see USB specification chapter 5.5-5.8 for magic numbers used here */
300 switch(transfer) {
301 case USB_TRANSFER_ISOCHRONOUS:
302 return (!low_speed && size < 1024);
303 case USB_TRANSFER_INTERRUPT:
[3f189c5]304 return size <= (low_speed ? 8 : 64);
[4d73d71]305 case USB_TRANSFER_CONTROL: /* device specifies its own max size */
306 return (size <= (low_speed ? 8 : 64));
307 case USB_TRANSFER_BULK: /* device specifies its own max size */
308 return (!low_speed && size <= 64);
309 }
310 return false;
311}
[c56dbe0]312/**
313 * @}
314 */
Note: See TracBrowser for help on using the repository browser.