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 | #include <str_error.h>
|
---|
36 | #include <adt/list.h>
|
---|
37 | #include <libarch/ddi.h>
|
---|
38 |
|
---|
39 | #include <usb/debug.h>
|
---|
40 | #include <usb/usb.h>
|
---|
41 | #include <usb/ddfiface.h>
|
---|
42 | #include <usb_iface.h>
|
---|
43 |
|
---|
44 | #include "uhci.h"
|
---|
45 | #include "iface.h"
|
---|
46 |
|
---|
47 | static irq_cmd_t uhci_cmds[] = {
|
---|
48 | {
|
---|
49 | .cmd = CMD_PIO_READ_16,
|
---|
50 | .addr = NULL, /* patched for every instance */
|
---|
51 | .dstarg = 1
|
---|
52 | },
|
---|
53 | {
|
---|
54 | .cmd = CMD_PIO_WRITE_16,
|
---|
55 | .addr = NULL, /* pathed for every instance */
|
---|
56 | .value = 0x1f
|
---|
57 | },
|
---|
58 | {
|
---|
59 | .cmd = CMD_ACCEPT
|
---|
60 | }
|
---|
61 | };
|
---|
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 | /*----------------------------------------------------------------------------*/
|
---|
93 | static int uhci_init_transfer_lists(uhci_t *instance);
|
---|
94 | static int uhci_init_mem_structures(uhci_t *instance);
|
---|
95 | static void uhci_init_hw(uhci_t *instance);
|
---|
96 |
|
---|
97 | static int uhci_interrupt_emulator(void *arg);
|
---|
98 | static int uhci_debug_checker(void *arg);
|
---|
99 |
|
---|
100 | static bool allowed_usb_packet(
|
---|
101 | bool low_speed, usb_transfer_type_t, size_t size);
|
---|
102 |
|
---|
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...) \
|
---|
110 | if (ret != EOK) { \
|
---|
111 | usb_log_error(message); \
|
---|
112 | if (instance->ddf_instance) \
|
---|
113 | ddf_fun_destroy(instance->ddf_instance); \
|
---|
114 | return ret; \
|
---|
115 | } else (void) 0
|
---|
116 |
|
---|
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));
|
---|
130 |
|
---|
131 | /* allow access to hc control registers */
|
---|
132 | regs_t *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);
|
---|
137 | instance->registers = io;
|
---|
138 | usb_log_debug("Device registers at %p(%u) accessible.\n",
|
---|
139 | io, reg_size);
|
---|
140 |
|
---|
141 | ret = uhci_init_mem_structures(instance);
|
---|
142 | CHECK_RET_DEST_FUN_RETURN(ret,
|
---|
143 | "Failed to initialize UHCI memory structures.\n");
|
---|
144 |
|
---|
145 | uhci_init_hw(instance);
|
---|
146 | instance->cleaner =
|
---|
147 | fibril_create(uhci_interrupt_emulator, instance);
|
---|
148 | fibril_add_ready(instance->cleaner);
|
---|
149 |
|
---|
150 | instance->debug_checker = fibril_create(uhci_debug_checker, instance);
|
---|
151 | fibril_add_ready(instance->debug_checker);
|
---|
152 |
|
---|
153 | usb_log_info("Started UHCI driver.\n");
|
---|
154 | return EOK;
|
---|
155 | #undef CHECK_RET_DEST_FUN_RETURN
|
---|
156 | }
|
---|
157 | /*----------------------------------------------------------------------------*/
|
---|
158 | void uhci_init_hw(uhci_t *instance)
|
---|
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 | do { async_usleep(10); }
|
---|
170 | while ((pio_read_16(&instance->registers->usbcmd) & UHCI_CMD_HCRESET) != 0);
|
---|
171 |
|
---|
172 | /* set framelist pointer */
|
---|
173 | const uint32_t pa = addr_to_phys(instance->frame_list);
|
---|
174 | pio_write_32(&instance->registers->flbaseadd, pa);
|
---|
175 |
|
---|
176 | /* enable all interrupts, but resume interrupt */
|
---|
177 | // pio_write_16(&instance->registers->usbintr,
|
---|
178 | // UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET);
|
---|
179 |
|
---|
180 | uint16_t status = pio_read_16(&instance->registers->usbcmd);
|
---|
181 | usb_log_warning("Previous command value: %x.\n", status);
|
---|
182 | /* Start the hc with large(64B) packet FSBR */
|
---|
183 | pio_write_16(&instance->registers->usbcmd,
|
---|
184 | UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
|
---|
185 | }
|
---|
186 | /*----------------------------------------------------------------------------*/
|
---|
187 | int uhci_init_mem_structures(uhci_t *instance)
|
---|
188 | {
|
---|
189 | assert(instance);
|
---|
190 | #define CHECK_RET_DEST_CMDS_RETURN(ret, message...) \
|
---|
191 | if (ret != EOK) { \
|
---|
192 | usb_log_error(message); \
|
---|
193 | if (instance->interrupt_code.cmds != NULL) \
|
---|
194 | free(instance->interrupt_code.cmds); \
|
---|
195 | return ret; \
|
---|
196 | } else (void) 0
|
---|
197 |
|
---|
198 | /* init interrupt code */
|
---|
199 | instance->interrupt_code.cmds = malloc(sizeof(uhci_cmds));
|
---|
200 | int ret = (instance->interrupt_code.cmds == NULL) ? ENOMEM : EOK;
|
---|
201 | CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to allocate interrupt cmds space.\n");
|
---|
202 |
|
---|
203 | {
|
---|
204 | irq_cmd_t *interrupt_commands = instance->interrupt_code.cmds;
|
---|
205 | memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds));
|
---|
206 | interrupt_commands[0].addr = (void*)&instance->registers->usbsts;
|
---|
207 | interrupt_commands[1].addr = (void*)&instance->registers->usbsts;
|
---|
208 | instance->interrupt_code.cmdcount =
|
---|
209 | sizeof(uhci_cmds) / sizeof(irq_cmd_t);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* init transfer lists */
|
---|
213 | ret = uhci_init_transfer_lists(instance);
|
---|
214 | CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to initialize transfer lists.\n");
|
---|
215 | usb_log_debug("Initialized transfer lists.\n");
|
---|
216 |
|
---|
217 | /* frame list initialization */
|
---|
218 | instance->frame_list = get_page();
|
---|
219 | ret = instance ? EOK : ENOMEM;
|
---|
220 | CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to get frame list page.\n");
|
---|
221 | usb_log_debug("Initialized frame list.\n");
|
---|
222 |
|
---|
223 | /* initialize all frames to point to the first queue head */
|
---|
224 | const uint32_t queue =
|
---|
225 | instance->transfers_interrupt.queue_head_pa
|
---|
226 | | LINK_POINTER_QUEUE_HEAD_FLAG;
|
---|
227 |
|
---|
228 | unsigned i = 0;
|
---|
229 | for(; i < UHCI_FRAME_LIST_COUNT; ++i) {
|
---|
230 | instance->frame_list[i] = queue;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /* init address keeper(libusb) */
|
---|
234 | device_keeper_init(&instance->device_manager);
|
---|
235 | usb_log_debug("Initialized device manager.\n");
|
---|
236 |
|
---|
237 | return EOK;
|
---|
238 | #undef CHECK_RET_DEST_CMDS_RETURN
|
---|
239 | }
|
---|
240 | /*----------------------------------------------------------------------------*/
|
---|
241 | int uhci_init_transfer_lists(uhci_t *instance)
|
---|
242 | {
|
---|
243 | assert(instance);
|
---|
244 | #define CHECK_RET_CLEAR_RETURN(ret, message...) \
|
---|
245 | if (ret != EOK) { \
|
---|
246 | usb_log_error(message); \
|
---|
247 | transfer_list_fini(&instance->transfers_bulk_full); \
|
---|
248 | transfer_list_fini(&instance->transfers_control_full); \
|
---|
249 | transfer_list_fini(&instance->transfers_control_slow); \
|
---|
250 | transfer_list_fini(&instance->transfers_interrupt); \
|
---|
251 | return ret; \
|
---|
252 | } else (void) 0
|
---|
253 |
|
---|
254 | /* initialize TODO: check errors */
|
---|
255 | int ret;
|
---|
256 | ret = transfer_list_init(&instance->transfers_bulk_full, "BULK_FULL");
|
---|
257 | CHECK_RET_CLEAR_RETURN(ret, "Failed to init BULK list.");
|
---|
258 |
|
---|
259 | ret = transfer_list_init(&instance->transfers_control_full, "CONTROL_FULL");
|
---|
260 | CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL FULL list.");
|
---|
261 |
|
---|
262 | ret = transfer_list_init(&instance->transfers_control_slow, "CONTROL_SLOW");
|
---|
263 | CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL SLOW list.");
|
---|
264 |
|
---|
265 | ret = transfer_list_init(&instance->transfers_interrupt, "INTERRUPT");
|
---|
266 | CHECK_RET_CLEAR_RETURN(ret, "Failed to init INTERRUPT list.");
|
---|
267 |
|
---|
268 | transfer_list_set_next(&instance->transfers_control_full,
|
---|
269 | &instance->transfers_bulk_full);
|
---|
270 | transfer_list_set_next(&instance->transfers_control_slow,
|
---|
271 | &instance->transfers_control_full);
|
---|
272 | transfer_list_set_next(&instance->transfers_interrupt,
|
---|
273 | &instance->transfers_control_slow);
|
---|
274 |
|
---|
275 | /*FSBR*/
|
---|
276 | #ifdef FSBR
|
---|
277 | transfer_list_set_next(&instance->transfers_bulk_full,
|
---|
278 | &instance->transfers_control_full);
|
---|
279 | #endif
|
---|
280 |
|
---|
281 | instance->transfers[0][USB_TRANSFER_INTERRUPT] =
|
---|
282 | &instance->transfers_interrupt;
|
---|
283 | instance->transfers[1][USB_TRANSFER_INTERRUPT] =
|
---|
284 | &instance->transfers_interrupt;
|
---|
285 | instance->transfers[0][USB_TRANSFER_CONTROL] =
|
---|
286 | &instance->transfers_control_full;
|
---|
287 | instance->transfers[1][USB_TRANSFER_CONTROL] =
|
---|
288 | &instance->transfers_control_slow;
|
---|
289 | instance->transfers[0][USB_TRANSFER_BULK] =
|
---|
290 | &instance->transfers_bulk_full;
|
---|
291 |
|
---|
292 | return EOK;
|
---|
293 | #undef CHECK_RET_CLEAR_RETURN
|
---|
294 | }
|
---|
295 | /*----------------------------------------------------------------------------*/
|
---|
296 | int uhci_schedule(uhci_t *instance, batch_t *batch)
|
---|
297 | {
|
---|
298 | assert(instance);
|
---|
299 | assert(batch);
|
---|
300 | const int low_speed = (batch->speed == USB_SPEED_LOW);
|
---|
301 | if (!allowed_usb_packet(
|
---|
302 | low_speed, batch->transfer_type, batch->max_packet_size)) {
|
---|
303 | usb_log_warning("Invalid USB packet specified %s SPEED %d %zu.\n",
|
---|
304 | low_speed ? "LOW" : "FULL" , batch->transfer_type,
|
---|
305 | batch->max_packet_size);
|
---|
306 | return ENOTSUP;
|
---|
307 | }
|
---|
308 | /* TODO: check available bandwith here */
|
---|
309 |
|
---|
310 | transfer_list_t *list =
|
---|
311 | instance->transfers[low_speed][batch->transfer_type];
|
---|
312 | assert(list);
|
---|
313 | transfer_list_add_batch(list, batch);
|
---|
314 |
|
---|
315 | return EOK;
|
---|
316 | }
|
---|
317 | /*----------------------------------------------------------------------------*/
|
---|
318 | void uhci_interrupt(uhci_t *instance, uint16_t status)
|
---|
319 | {
|
---|
320 | assert(instance);
|
---|
321 | transfer_list_remove_finished(&instance->transfers_interrupt);
|
---|
322 | transfer_list_remove_finished(&instance->transfers_control_slow);
|
---|
323 | transfer_list_remove_finished(&instance->transfers_control_full);
|
---|
324 | transfer_list_remove_finished(&instance->transfers_bulk_full);
|
---|
325 | }
|
---|
326 | /*----------------------------------------------------------------------------*/
|
---|
327 | int uhci_interrupt_emulator(void* arg)
|
---|
328 | {
|
---|
329 | usb_log_debug("Started interrupt emulator.\n");
|
---|
330 | uhci_t *instance = (uhci_t*)arg;
|
---|
331 | assert(instance);
|
---|
332 |
|
---|
333 | while (1) {
|
---|
334 | uint16_t status = pio_read_16(&instance->registers->usbsts);
|
---|
335 | if (status != 0)
|
---|
336 | usb_log_debug2("UHCI status: %x.\n", status);
|
---|
337 | status |= 1;
|
---|
338 | uhci_interrupt(instance, status);
|
---|
339 | pio_write_16(&instance->registers->usbsts, 0x1f);
|
---|
340 | async_usleep(UHCI_CLEANER_TIMEOUT);
|
---|
341 | }
|
---|
342 | return EOK;
|
---|
343 | }
|
---|
344 | /*---------------------------------------------------------------------------*/
|
---|
345 | int uhci_debug_checker(void *arg)
|
---|
346 | {
|
---|
347 | uhci_t *instance = (uhci_t*)arg;
|
---|
348 | assert(instance);
|
---|
349 |
|
---|
350 | #define QH(queue) \
|
---|
351 | instance->transfers_##queue.queue_head
|
---|
352 |
|
---|
353 | while (1) {
|
---|
354 | const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
|
---|
355 | const uint16_t sts = pio_read_16(&instance->registers->usbsts);
|
---|
356 | const uint16_t intr =
|
---|
357 | pio_read_16(&instance->registers->usbintr);
|
---|
358 |
|
---|
359 | if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
|
---|
360 | usb_log_debug2("Command: %X Status: %X Intr: %x\n",
|
---|
361 | cmd, sts, intr);
|
---|
362 | }
|
---|
363 |
|
---|
364 | uintptr_t frame_list =
|
---|
365 | pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
|
---|
366 | if (frame_list != addr_to_phys(instance->frame_list)) {
|
---|
367 | usb_log_debug("Framelist address: %p vs. %p.\n",
|
---|
368 | frame_list, addr_to_phys(instance->frame_list));
|
---|
369 | }
|
---|
370 |
|
---|
371 | int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
|
---|
372 | usb_log_debug2("Framelist item: %d \n", frnum );
|
---|
373 |
|
---|
374 | uintptr_t expected_pa = instance->frame_list[frnum] & (~0xf);
|
---|
375 | uintptr_t real_pa = addr_to_phys(QH(interrupt));
|
---|
376 | if (expected_pa != real_pa) {
|
---|
377 | usb_log_debug("Interrupt QH: %p vs. %p.\n",
|
---|
378 | expected_pa, real_pa);
|
---|
379 | }
|
---|
380 |
|
---|
381 | expected_pa = QH(interrupt)->next_queue & (~0xf);
|
---|
382 | real_pa = addr_to_phys(QH(control_slow));
|
---|
383 | if (expected_pa != real_pa) {
|
---|
384 | usb_log_debug("Control Slow QH: %p vs. %p.\n",
|
---|
385 | expected_pa, real_pa);
|
---|
386 | }
|
---|
387 |
|
---|
388 | expected_pa = QH(control_slow)->next_queue & (~0xf);
|
---|
389 | real_pa = addr_to_phys(QH(control_full));
|
---|
390 | if (expected_pa != real_pa) {
|
---|
391 | usb_log_debug("Control Full QH: %p vs. %p.\n",
|
---|
392 | expected_pa, real_pa);
|
---|
393 | }
|
---|
394 |
|
---|
395 | expected_pa = QH(control_full)->next_queue & (~0xf);
|
---|
396 | real_pa = addr_to_phys(QH(bulk_full));
|
---|
397 | if (expected_pa != real_pa ) {
|
---|
398 | usb_log_debug("Bulk QH: %p vs. %p.\n",
|
---|
399 | expected_pa, real_pa);
|
---|
400 | }
|
---|
401 | async_usleep(UHCI_DEBUGER_TIMEOUT);
|
---|
402 | }
|
---|
403 | return 0;
|
---|
404 | #undef QH
|
---|
405 | }
|
---|
406 | /*----------------------------------------------------------------------------*/
|
---|
407 | bool allowed_usb_packet(
|
---|
408 | bool low_speed, usb_transfer_type_t transfer, size_t size)
|
---|
409 | {
|
---|
410 | /* see USB specification chapter 5.5-5.8 for magic numbers used here */
|
---|
411 | switch(transfer)
|
---|
412 | {
|
---|
413 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
414 | return (!low_speed && size < 1024);
|
---|
415 | case USB_TRANSFER_INTERRUPT:
|
---|
416 | return size <= (low_speed ? 8 : 64);
|
---|
417 | case USB_TRANSFER_CONTROL: /* device specifies its own max size */
|
---|
418 | return (size <= (low_speed ? 8 : 64));
|
---|
419 | case USB_TRANSFER_BULK: /* device specifies its own max size */
|
---|
420 | return (!low_speed && size <= 64);
|
---|
421 | }
|
---|
422 | return false;
|
---|
423 | }
|
---|
424 | /**
|
---|
425 | * @}
|
---|
426 | */
|
---|