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 drvusbohcihc
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief OHCI Host controller driver routines
|
---|
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 |
|
---|
43 | #include "hc.h"
|
---|
44 | #include "hcd_endpoint.h"
|
---|
45 |
|
---|
46 | #define OHCI_USED_INTERRUPTS \
|
---|
47 | (I_SO | I_WDH | I_UE | I_RHSC)
|
---|
48 | static int interrupt_emulator(hc_t *instance);
|
---|
49 | static void hc_gain_control(hc_t *instance);
|
---|
50 | static int hc_init_transfer_lists(hc_t *instance);
|
---|
51 | static int hc_init_memory(hc_t *instance);
|
---|
52 | /*----------------------------------------------------------------------------*/
|
---|
53 | int hc_register_hub(hc_t *instance, ddf_fun_t *hub_fun)
|
---|
54 | {
|
---|
55 | assert(instance);
|
---|
56 | assert(hub_fun);
|
---|
57 |
|
---|
58 | int ret;
|
---|
59 |
|
---|
60 | usb_address_t hub_address =
|
---|
61 | device_keeper_get_free_address(&instance->manager, USB_SPEED_FULL);
|
---|
62 | if (hub_address <= 0) {
|
---|
63 | usb_log_error("Failed to get OHCI root hub address.\n");
|
---|
64 | return hub_address;
|
---|
65 | }
|
---|
66 | instance->rh.address = hub_address;
|
---|
67 | usb_device_keeper_bind(
|
---|
68 | &instance->manager, hub_address, hub_fun->handle);
|
---|
69 |
|
---|
70 | ret = hc_add_endpoint(instance, hub_address, 0, USB_SPEED_FULL,
|
---|
71 | USB_TRANSFER_CONTROL, USB_DIRECTION_BOTH, 64, 0, 0);
|
---|
72 | if (ret != EOK) {
|
---|
73 | usb_log_error("Failed to add OHCI rh endpoint 0.\n");
|
---|
74 | usb_device_keeper_release(&instance->manager, hub_address);
|
---|
75 | return ret;
|
---|
76 | }
|
---|
77 |
|
---|
78 | char *match_str = NULL;
|
---|
79 | /* DDF needs heap allocated string */
|
---|
80 | ret = asprintf(&match_str, "usb&class=hub");
|
---|
81 | if (ret < 0) {
|
---|
82 | usb_log_error(
|
---|
83 | "Failed(%d) to create root hub match-id string.\n", ret);
|
---|
84 | usb_device_keeper_release(&instance->manager, hub_address);
|
---|
85 | return ret;
|
---|
86 | }
|
---|
87 |
|
---|
88 | ret = ddf_fun_add_match_id(hub_fun, match_str, 100);
|
---|
89 | if (ret != EOK) {
|
---|
90 | usb_log_error("Failed add root hub match-id.\n");
|
---|
91 | }
|
---|
92 | ret = ddf_fun_bind(hub_fun);
|
---|
93 | return ret;
|
---|
94 | }
|
---|
95 | /*----------------------------------------------------------------------------*/
|
---|
96 | int hc_init(hc_t *instance, uintptr_t regs, size_t reg_size, bool interrupts)
|
---|
97 | {
|
---|
98 | assert(instance);
|
---|
99 | int ret = EOK;
|
---|
100 | #define CHECK_RET_RETURN(ret, message...) \
|
---|
101 | if (ret != EOK) { \
|
---|
102 | usb_log_error(message); \
|
---|
103 | return ret; \
|
---|
104 | } else (void)0
|
---|
105 |
|
---|
106 | ret = pio_enable((void*)regs, reg_size, (void**)&instance->registers);
|
---|
107 | CHECK_RET_RETURN(ret,
|
---|
108 | "Failed(%d) to gain access to device registers: %s.\n",
|
---|
109 | ret, str_error(ret));
|
---|
110 |
|
---|
111 | list_initialize(&instance->pending_batches);
|
---|
112 | usb_device_keeper_init(&instance->manager);
|
---|
113 | ret = usb_endpoint_manager_init(&instance->ep_manager,
|
---|
114 | BANDWIDTH_AVAILABLE_USB11);
|
---|
115 | CHECK_RET_RETURN(ret, "Failed to initialize endpoint manager: %s.\n",
|
---|
116 | str_error(ret));
|
---|
117 |
|
---|
118 | ret = hc_init_memory(instance);
|
---|
119 | CHECK_RET_RETURN(ret, "Failed to create OHCI memory structures: %s.\n",
|
---|
120 | str_error(ret));
|
---|
121 | #undef CHECK_RET_RETURN
|
---|
122 |
|
---|
123 |
|
---|
124 | // hc_init_hw(instance);
|
---|
125 | hc_gain_control(instance);
|
---|
126 | fibril_mutex_initialize(&instance->guard);
|
---|
127 |
|
---|
128 | rh_init(&instance->rh, instance->registers);
|
---|
129 |
|
---|
130 | if (!interrupts) {
|
---|
131 | instance->interrupt_emulator =
|
---|
132 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
---|
133 | fibril_add_ready(instance->interrupt_emulator);
|
---|
134 | }
|
---|
135 |
|
---|
136 | return EOK;
|
---|
137 | }
|
---|
138 | /*----------------------------------------------------------------------------*/
|
---|
139 | int hc_add_endpoint(
|
---|
140 | hc_t *instance, usb_address_t address, usb_endpoint_t endpoint,
|
---|
141 | usb_speed_t speed, usb_transfer_type_t type, usb_direction_t direction,
|
---|
142 | size_t mps, size_t size, unsigned interval)
|
---|
143 | {
|
---|
144 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
---|
145 | if (ep == NULL)
|
---|
146 | return ENOMEM;
|
---|
147 | int ret =
|
---|
148 | endpoint_init(ep, address, endpoint, direction, type, speed, mps);
|
---|
149 | if (ret != EOK) {
|
---|
150 | free(ep);
|
---|
151 | return ret;
|
---|
152 | }
|
---|
153 |
|
---|
154 | hcd_endpoint_t *hcd_ep = hcd_endpoint_assign(ep);
|
---|
155 | if (hcd_ep == NULL) {
|
---|
156 | endpoint_destroy(ep);
|
---|
157 | return ENOMEM;
|
---|
158 | }
|
---|
159 |
|
---|
160 | ret = usb_endpoint_manager_register_ep(&instance->ep_manager, ep, size);
|
---|
161 | if (ret != EOK) {
|
---|
162 | hcd_endpoint_clear(ep);
|
---|
163 | endpoint_destroy(ep);
|
---|
164 | return ret;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* Enqueue hcd_ep */
|
---|
168 | switch (ep->transfer_type) {
|
---|
169 | case USB_TRANSFER_CONTROL:
|
---|
170 | instance->registers->control &= ~C_CLE;
|
---|
171 | endpoint_list_add_ep(
|
---|
172 | &instance->lists[ep->transfer_type], hcd_ep);
|
---|
173 | instance->registers->control_current = 0;
|
---|
174 | instance->registers->control |= C_CLE;
|
---|
175 | break;
|
---|
176 | case USB_TRANSFER_BULK:
|
---|
177 | instance->registers->control &= ~C_BLE;
|
---|
178 | endpoint_list_add_ep(
|
---|
179 | &instance->lists[ep->transfer_type], hcd_ep);
|
---|
180 | instance->registers->control |= C_BLE;
|
---|
181 | break;
|
---|
182 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
183 | case USB_TRANSFER_INTERRUPT:
|
---|
184 | instance->registers->control &= (~C_PLE & ~C_IE);
|
---|
185 | endpoint_list_add_ep(
|
---|
186 | &instance->lists[ep->transfer_type], hcd_ep);
|
---|
187 | instance->registers->control |= C_PLE | C_IE;
|
---|
188 | break;
|
---|
189 | default:
|
---|
190 | break;
|
---|
191 | }
|
---|
192 |
|
---|
193 | return EOK;
|
---|
194 | }
|
---|
195 | /*----------------------------------------------------------------------------*/
|
---|
196 | int hc_remove_endpoint(hc_t *instance, usb_address_t address,
|
---|
197 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
198 | {
|
---|
199 | assert(instance);
|
---|
200 | fibril_mutex_lock(&instance->guard);
|
---|
201 | endpoint_t *ep = usb_endpoint_manager_get_ep(&instance->ep_manager,
|
---|
202 | address, endpoint, direction, NULL);
|
---|
203 | if (ep == NULL) {
|
---|
204 | usb_log_error("Endpoint unregister failed: No such EP.\n");
|
---|
205 | fibril_mutex_unlock(&instance->guard);
|
---|
206 | return ENOENT;
|
---|
207 | }
|
---|
208 |
|
---|
209 | hcd_endpoint_t *hcd_ep = hcd_endpoint_get(ep);
|
---|
210 | if (hcd_ep) {
|
---|
211 | /* Dequeue hcd_ep */
|
---|
212 | switch (ep->transfer_type) {
|
---|
213 | case USB_TRANSFER_CONTROL:
|
---|
214 | instance->registers->control &= ~C_CLE;
|
---|
215 | endpoint_list_remove_ep(
|
---|
216 | &instance->lists[ep->transfer_type], hcd_ep);
|
---|
217 | instance->registers->control_current = 0;
|
---|
218 | instance->registers->control |= C_CLE;
|
---|
219 | break;
|
---|
220 | case USB_TRANSFER_BULK:
|
---|
221 | instance->registers->control &= ~C_BLE;
|
---|
222 | endpoint_list_remove_ep(
|
---|
223 | &instance->lists[ep->transfer_type], hcd_ep);
|
---|
224 | instance->registers->control |= C_BLE;
|
---|
225 | break;
|
---|
226 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
227 | case USB_TRANSFER_INTERRUPT:
|
---|
228 | instance->registers->control &= (~C_PLE & ~C_IE);
|
---|
229 | endpoint_list_remove_ep(
|
---|
230 | &instance->lists[ep->transfer_type], hcd_ep);
|
---|
231 | instance->registers->control |= C_PLE | C_IE;
|
---|
232 | break;
|
---|
233 | default:
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | hcd_endpoint_clear(ep);
|
---|
237 | } else {
|
---|
238 | usb_log_warning("Endpoint without hcd equivalent structure.\n");
|
---|
239 | }
|
---|
240 | int ret = usb_endpoint_manager_unregister_ep(&instance->ep_manager,
|
---|
241 | address, endpoint, direction);
|
---|
242 | fibril_mutex_unlock(&instance->guard);
|
---|
243 | return ret;
|
---|
244 | }
|
---|
245 | /*----------------------------------------------------------------------------*/
|
---|
246 | endpoint_t * hc_get_endpoint(hc_t *instance, usb_address_t address,
|
---|
247 | usb_endpoint_t endpoint, usb_direction_t direction, size_t *bw)
|
---|
248 | {
|
---|
249 | assert(instance);
|
---|
250 | fibril_mutex_lock(&instance->guard);
|
---|
251 | endpoint_t *ep = usb_endpoint_manager_get_ep(&instance->ep_manager,
|
---|
252 | address, endpoint, direction, bw);
|
---|
253 | fibril_mutex_unlock(&instance->guard);
|
---|
254 | return ep;
|
---|
255 | }
|
---|
256 | /*----------------------------------------------------------------------------*/
|
---|
257 | int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
|
---|
258 | {
|
---|
259 | assert(instance);
|
---|
260 | assert(batch);
|
---|
261 | assert(batch->ep);
|
---|
262 |
|
---|
263 | /* check for root hub communication */
|
---|
264 | if (batch->ep->address == instance->rh.address) {
|
---|
265 | return rh_request(&instance->rh, batch);
|
---|
266 | }
|
---|
267 |
|
---|
268 | fibril_mutex_lock(&instance->guard);
|
---|
269 | list_append(&batch->link, &instance->pending_batches);
|
---|
270 | batch_commit(batch);
|
---|
271 | switch (batch->ep->transfer_type) {
|
---|
272 | case USB_TRANSFER_CONTROL:
|
---|
273 | instance->registers->command_status |= CS_CLF;
|
---|
274 | break;
|
---|
275 | case USB_TRANSFER_BULK:
|
---|
276 | instance->registers->command_status |= CS_BLF;
|
---|
277 | break;
|
---|
278 | default:
|
---|
279 | break;
|
---|
280 | }
|
---|
281 |
|
---|
282 | fibril_mutex_unlock(&instance->guard);
|
---|
283 | return EOK;
|
---|
284 | }
|
---|
285 | /*----------------------------------------------------------------------------*/
|
---|
286 | void hc_interrupt(hc_t *instance, uint32_t status)
|
---|
287 | {
|
---|
288 | assert(instance);
|
---|
289 | usb_log_debug("OHCI(%p) interrupt: %x.\n", instance, status);
|
---|
290 | if ((status & ~I_SF) == 0) /* ignore sof status */
|
---|
291 | return;
|
---|
292 | if (status & I_RHSC)
|
---|
293 | rh_interrupt(&instance->rh);
|
---|
294 |
|
---|
295 |
|
---|
296 | if (status & I_WDH) {
|
---|
297 | fibril_mutex_lock(&instance->guard);
|
---|
298 | usb_log_debug2("HCCA: %p-%#" PRIx32 " (%p).\n", instance->hcca,
|
---|
299 | instance->registers->hcca,
|
---|
300 | (void *) addr_to_phys(instance->hcca));
|
---|
301 | usb_log_debug2("Periodic current: %#" PRIx32 ".\n",
|
---|
302 | instance->registers->periodic_current);
|
---|
303 |
|
---|
304 | link_t *current = instance->pending_batches.next;
|
---|
305 | while (current != &instance->pending_batches) {
|
---|
306 | link_t *next = current->next;
|
---|
307 | usb_transfer_batch_t *batch =
|
---|
308 | usb_transfer_batch_from_link(current);
|
---|
309 |
|
---|
310 | if (batch_is_complete(batch)) {
|
---|
311 | list_remove(current);
|
---|
312 | usb_transfer_batch_finish(batch);
|
---|
313 | }
|
---|
314 | current = next;
|
---|
315 | }
|
---|
316 | fibril_mutex_unlock(&instance->guard);
|
---|
317 | }
|
---|
318 | }
|
---|
319 | /*----------------------------------------------------------------------------*/
|
---|
320 | int interrupt_emulator(hc_t *instance)
|
---|
321 | {
|
---|
322 | assert(instance);
|
---|
323 | usb_log_info("Started interrupt emulator.\n");
|
---|
324 | while (1) {
|
---|
325 | const uint32_t status = instance->registers->interrupt_status;
|
---|
326 | instance->registers->interrupt_status = status;
|
---|
327 | hc_interrupt(instance, status);
|
---|
328 | async_usleep(50000);
|
---|
329 | }
|
---|
330 | return EOK;
|
---|
331 | }
|
---|
332 | /*----------------------------------------------------------------------------*/
|
---|
333 | void hc_gain_control(hc_t *instance)
|
---|
334 | {
|
---|
335 | assert(instance);
|
---|
336 | usb_log_debug("Requesting OHCI control.\n");
|
---|
337 | /* Turn off legacy emulation */
|
---|
338 | volatile uint32_t *ohci_emulation_reg =
|
---|
339 | (uint32_t*)((char*)instance->registers + 0x100);
|
---|
340 | usb_log_debug("OHCI legacy register %p: %x.\n",
|
---|
341 | ohci_emulation_reg, *ohci_emulation_reg);
|
---|
342 | /* Do not change A20 state */
|
---|
343 | *ohci_emulation_reg &= 0x100;
|
---|
344 | usb_log_debug("OHCI legacy register %p: %x.\n",
|
---|
345 | ohci_emulation_reg, *ohci_emulation_reg);
|
---|
346 |
|
---|
347 | /* Interrupt routing enabled => smm driver is active */
|
---|
348 | if (instance->registers->control & C_IR) {
|
---|
349 | usb_log_debug("SMM driver: request ownership change.\n");
|
---|
350 | instance->registers->command_status |= CS_OCR;
|
---|
351 | while (instance->registers->control & C_IR) {
|
---|
352 | async_usleep(1000);
|
---|
353 | }
|
---|
354 | usb_log_info("SMM driver: Ownership taken.\n");
|
---|
355 | instance->registers->control &= (C_HCFS_RESET << C_HCFS_SHIFT);
|
---|
356 | async_usleep(50000);
|
---|
357 | return;
|
---|
358 | }
|
---|
359 |
|
---|
360 | const unsigned hc_status =
|
---|
361 | (instance->registers->control >> C_HCFS_SHIFT) & C_HCFS_MASK;
|
---|
362 | /* Interrupt routing disabled && status != USB_RESET => BIOS active */
|
---|
363 | if (hc_status != C_HCFS_RESET) {
|
---|
364 | usb_log_debug("BIOS driver found.\n");
|
---|
365 | if (hc_status == C_HCFS_OPERATIONAL) {
|
---|
366 | usb_log_info("BIOS driver: HC operational.\n");
|
---|
367 | return;
|
---|
368 | }
|
---|
369 | /* HC is suspended assert resume for 20ms */
|
---|
370 | instance->registers->control &= (C_HCFS_RESUME << C_HCFS_SHIFT);
|
---|
371 | async_usleep(20000);
|
---|
372 | usb_log_info("BIOS driver: HC resumed.\n");
|
---|
373 | return;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /* HC is in reset (hw startup) => no other driver
|
---|
377 | * maintain reset for at least the time specified in USB spec (50 ms)*/
|
---|
378 | usb_log_info("HC found in reset.\n");
|
---|
379 | async_usleep(50000);
|
---|
380 | }
|
---|
381 | /*----------------------------------------------------------------------------*/
|
---|
382 | void hc_start_hw(hc_t *instance)
|
---|
383 | {
|
---|
384 | /* OHCI guide page 42 */
|
---|
385 | assert(instance);
|
---|
386 | usb_log_debug2("Started hc initialization routine.\n");
|
---|
387 |
|
---|
388 | /* Save contents of fm_interval register */
|
---|
389 | const uint32_t fm_interval = instance->registers->fm_interval;
|
---|
390 | usb_log_debug2("Old value of HcFmInterval: %x.\n", fm_interval);
|
---|
391 |
|
---|
392 | /* Reset hc */
|
---|
393 | usb_log_debug2("HC reset.\n");
|
---|
394 | size_t time = 0;
|
---|
395 | instance->registers->command_status = CS_HCR;
|
---|
396 | while (instance->registers->command_status & CS_HCR) {
|
---|
397 | async_usleep(10);
|
---|
398 | time += 10;
|
---|
399 | }
|
---|
400 | usb_log_debug2("HC reset complete in %zu us.\n", time);
|
---|
401 |
|
---|
402 | /* Restore fm_interval */
|
---|
403 | instance->registers->fm_interval = fm_interval;
|
---|
404 | assert((instance->registers->command_status & CS_HCR) == 0);
|
---|
405 |
|
---|
406 | /* hc is now in suspend state */
|
---|
407 | usb_log_debug2("HC should be in suspend state(%x).\n",
|
---|
408 | instance->registers->control);
|
---|
409 |
|
---|
410 | /* Use HCCA */
|
---|
411 | instance->registers->hcca = addr_to_phys(instance->hcca);
|
---|
412 |
|
---|
413 | /* Use queues */
|
---|
414 | instance->registers->bulk_head =
|
---|
415 | instance->lists[USB_TRANSFER_BULK].list_head_pa;
|
---|
416 | usb_log_debug2("Bulk HEAD set to: %p (%#" PRIx32 ").\n",
|
---|
417 | instance->lists[USB_TRANSFER_BULK].list_head,
|
---|
418 | instance->lists[USB_TRANSFER_BULK].list_head_pa);
|
---|
419 |
|
---|
420 | instance->registers->control_head =
|
---|
421 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa;
|
---|
422 | usb_log_debug2("Control HEAD set to: %p (%#" PRIx32 ").\n",
|
---|
423 | instance->lists[USB_TRANSFER_CONTROL].list_head,
|
---|
424 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
|
---|
425 |
|
---|
426 | /* Enable queues */
|
---|
427 | instance->registers->control |= (C_PLE | C_IE | C_CLE | C_BLE);
|
---|
428 | usb_log_debug2("All queues enabled(%x).\n",
|
---|
429 | instance->registers->control);
|
---|
430 |
|
---|
431 | /* Enable interrupts */
|
---|
432 | instance->registers->interrupt_enable = OHCI_USED_INTERRUPTS;
|
---|
433 | usb_log_debug2("Enabled interrupts: %x.\n",
|
---|
434 | instance->registers->interrupt_enable);
|
---|
435 | instance->registers->interrupt_enable = I_MI;
|
---|
436 |
|
---|
437 | /* Set periodic start to 90% */
|
---|
438 | uint32_t frame_length = ((fm_interval >> FMI_FI_SHIFT) & FMI_FI_MASK);
|
---|
439 | instance->registers->periodic_start = (frame_length / 10) * 9;
|
---|
440 | usb_log_debug2("All periodic start set to: %x(%u - 90%% of %d).\n",
|
---|
441 | instance->registers->periodic_start,
|
---|
442 | instance->registers->periodic_start, frame_length);
|
---|
443 |
|
---|
444 | instance->registers->control &= (C_HCFS_OPERATIONAL << C_HCFS_SHIFT);
|
---|
445 | usb_log_info("OHCI HC up and running(%x).\n",
|
---|
446 | instance->registers->control);
|
---|
447 | }
|
---|
448 | /*----------------------------------------------------------------------------*/
|
---|
449 | int hc_init_transfer_lists(hc_t *instance)
|
---|
450 | {
|
---|
451 | assert(instance);
|
---|
452 |
|
---|
453 | #define SETUP_ENDPOINT_LIST(type) \
|
---|
454 | do { \
|
---|
455 | const char *name = usb_str_transfer_type(type); \
|
---|
456 | int ret = endpoint_list_init(&instance->lists[type], name); \
|
---|
457 | if (ret != EOK) { \
|
---|
458 | usb_log_error("Failed(%d) to setup %s endpoint list.\n", \
|
---|
459 | ret, name); \
|
---|
460 | endpoint_list_fini(&instance->lists[USB_TRANSFER_ISOCHRONOUS]); \
|
---|
461 | endpoint_list_fini(&instance->lists[USB_TRANSFER_INTERRUPT]); \
|
---|
462 | endpoint_list_fini(&instance->lists[USB_TRANSFER_CONTROL]); \
|
---|
463 | endpoint_list_fini(&instance->lists[USB_TRANSFER_BULK]); \
|
---|
464 | } \
|
---|
465 | } while (0)
|
---|
466 |
|
---|
467 | SETUP_ENDPOINT_LIST(USB_TRANSFER_ISOCHRONOUS);
|
---|
468 | SETUP_ENDPOINT_LIST(USB_TRANSFER_INTERRUPT);
|
---|
469 | SETUP_ENDPOINT_LIST(USB_TRANSFER_CONTROL);
|
---|
470 | SETUP_ENDPOINT_LIST(USB_TRANSFER_BULK);
|
---|
471 | #undef SETUP_ENDPOINT_LIST
|
---|
472 | endpoint_list_set_next(&instance->lists[USB_TRANSFER_INTERRUPT],
|
---|
473 | &instance->lists[USB_TRANSFER_ISOCHRONOUS]);
|
---|
474 |
|
---|
475 | return EOK;
|
---|
476 | }
|
---|
477 | /*----------------------------------------------------------------------------*/
|
---|
478 | int hc_init_memory(hc_t *instance)
|
---|
479 | {
|
---|
480 | assert(instance);
|
---|
481 |
|
---|
482 | bzero(&instance->rh, sizeof(instance->rh));
|
---|
483 | /* Init queues */
|
---|
484 | hc_init_transfer_lists(instance);
|
---|
485 |
|
---|
486 | /*Init HCCA */
|
---|
487 | instance->hcca = malloc32(sizeof(hcca_t));
|
---|
488 | if (instance->hcca == NULL)
|
---|
489 | return ENOMEM;
|
---|
490 | bzero(instance->hcca, sizeof(hcca_t));
|
---|
491 | usb_log_debug2("OHCI HCCA initialized at %p.\n", instance->hcca);
|
---|
492 |
|
---|
493 | unsigned i = 0;
|
---|
494 | for (; i < 32; ++i) {
|
---|
495 | instance->hcca->int_ep[i] =
|
---|
496 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa;
|
---|
497 | }
|
---|
498 | usb_log_debug2("Interrupt HEADs set to: %p (%#" PRIx32 ").\n",
|
---|
499 | instance->lists[USB_TRANSFER_INTERRUPT].list_head,
|
---|
500 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
|
---|
501 |
|
---|
502 | /* Init interrupt code */
|
---|
503 | instance->interrupt_code.cmds = instance->interrupt_commands;
|
---|
504 | {
|
---|
505 | /* Read status register */
|
---|
506 | instance->interrupt_commands[0].cmd = CMD_MEM_READ_32;
|
---|
507 | instance->interrupt_commands[0].dstarg = 1;
|
---|
508 | instance->interrupt_commands[0].addr =
|
---|
509 | (void*)&instance->registers->interrupt_status;
|
---|
510 |
|
---|
511 | /* Test whether we are the interrupt cause */
|
---|
512 | instance->interrupt_commands[1].cmd = CMD_BTEST;
|
---|
513 | instance->interrupt_commands[1].value =
|
---|
514 | OHCI_USED_INTERRUPTS;
|
---|
515 | instance->interrupt_commands[1].srcarg = 1;
|
---|
516 | instance->interrupt_commands[1].dstarg = 2;
|
---|
517 |
|
---|
518 | /* Predicate cleaning and accepting */
|
---|
519 | instance->interrupt_commands[2].cmd = CMD_PREDICATE;
|
---|
520 | instance->interrupt_commands[2].value = 2;
|
---|
521 | instance->interrupt_commands[2].srcarg = 2;
|
---|
522 |
|
---|
523 | /* Write clean status register */
|
---|
524 | instance->interrupt_commands[3].cmd = CMD_MEM_WRITE_A_32;
|
---|
525 | instance->interrupt_commands[3].srcarg = 1;
|
---|
526 | instance->interrupt_commands[3].addr =
|
---|
527 | (void*)&instance->registers->interrupt_status;
|
---|
528 |
|
---|
529 | /* Accept interrupt */
|
---|
530 | instance->interrupt_commands[4].cmd = CMD_ACCEPT;
|
---|
531 |
|
---|
532 | instance->interrupt_code.cmdcount = OHCI_NEEDED_IRQ_COMMANDS;
|
---|
533 | }
|
---|
534 |
|
---|
535 | return EOK;
|
---|
536 | }
|
---|
537 | /**
|
---|
538 | * @}
|
---|
539 | */
|
---|