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