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 |
|
---|
38 | #include "transfer_list.h"
|
---|
39 |
|
---|
40 | int transfer_list_init(transfer_list_t *instance, const char *name)
|
---|
41 | {
|
---|
42 | assert(instance);
|
---|
43 | instance->next = NULL;
|
---|
44 | instance->name = name;
|
---|
45 | instance->queue_head = queue_head_get();
|
---|
46 | if (!instance->queue_head) {
|
---|
47 | usb_log_error("Failed to allocate queue head.\n");
|
---|
48 | return ENOMEM;
|
---|
49 | }
|
---|
50 | instance->queue_head_pa = (uintptr_t)addr_to_phys(instance->queue_head);
|
---|
51 |
|
---|
52 | queue_head_init(instance->queue_head);
|
---|
53 | list_initialize(&instance->batch_list);
|
---|
54 | fibril_mutex_initialize(&instance->guard);
|
---|
55 | return EOK;
|
---|
56 | }
|
---|
57 | /*----------------------------------------------------------------------------*/
|
---|
58 | void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
|
---|
59 | {
|
---|
60 | assert(instance);
|
---|
61 | assert(next);
|
---|
62 | if (!instance->queue_head)
|
---|
63 | return;
|
---|
64 | queue_head_append_qh(instance->queue_head, next->queue_head_pa);
|
---|
65 | instance->queue_head->element = instance->queue_head->next_queue;
|
---|
66 | }
|
---|
67 | /*----------------------------------------------------------------------------*/
|
---|
68 | void transfer_list_add_batch(transfer_list_t *instance, batch_t *batch)
|
---|
69 | {
|
---|
70 | assert(instance);
|
---|
71 | assert(batch);
|
---|
72 |
|
---|
73 | uint32_t pa = (uintptr_t)addr_to_phys(batch->qh);
|
---|
74 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
---|
75 | pa |= LINK_POINTER_QUEUE_HEAD_FLAG;
|
---|
76 |
|
---|
77 | batch->qh->next_queue = instance->queue_head->next_queue;
|
---|
78 |
|
---|
79 | fibril_mutex_lock(&instance->guard);
|
---|
80 |
|
---|
81 | if (instance->queue_head->element == instance->queue_head->next_queue) {
|
---|
82 | /* there is nothing scheduled */
|
---|
83 | list_append(&batch->link, &instance->batch_list);
|
---|
84 | instance->queue_head->element = pa;
|
---|
85 | usb_log_debug2("Added batch(%p) to queue %s first.\n",
|
---|
86 | batch, instance->name);
|
---|
87 | fibril_mutex_unlock(&instance->guard);
|
---|
88 | return;
|
---|
89 | }
|
---|
90 | /* now we can be sure that there is someting scheduled */
|
---|
91 | assert(!list_empty(&instance->batch_list));
|
---|
92 | batch_t *first = list_get_instance(
|
---|
93 | instance->batch_list.next, batch_t, link);
|
---|
94 | batch_t *last = list_get_instance(
|
---|
95 | instance->batch_list.prev, batch_t, link);
|
---|
96 | queue_head_append_qh(last->qh, pa);
|
---|
97 | list_append(&batch->link, &instance->batch_list);
|
---|
98 | usb_log_debug2("Added batch(%p) to queue %s last, first is %p.\n",
|
---|
99 | batch, instance->name, first );
|
---|
100 | fibril_mutex_unlock(&instance->guard);
|
---|
101 | }
|
---|
102 | /*----------------------------------------------------------------------------*/
|
---|
103 | static void transfer_list_remove_batch(
|
---|
104 | transfer_list_t *instance, batch_t *batch)
|
---|
105 | {
|
---|
106 | assert(instance);
|
---|
107 | assert(batch);
|
---|
108 | assert(instance->queue_head);
|
---|
109 | assert(batch->qh);
|
---|
110 |
|
---|
111 | /* I'm the first one here */
|
---|
112 | if (batch->link.prev == &instance->batch_list) {
|
---|
113 | usb_log_debug("Removing batch %p was first, next element %x.\n",
|
---|
114 | batch, batch->qh->next_queue);
|
---|
115 | instance->queue_head->element = batch->qh->next_queue;
|
---|
116 | } else {
|
---|
117 | usb_log_debug("Removing batch %p was NOT first, next element %x.\n",
|
---|
118 | batch, batch->qh->next_queue);
|
---|
119 | batch_t *prev = list_get_instance(batch->link.prev, batch_t, link);
|
---|
120 | prev->qh->next_queue = batch->qh->next_queue;
|
---|
121 | }
|
---|
122 | list_remove(&batch->link);
|
---|
123 | }
|
---|
124 | /*----------------------------------------------------------------------------*/
|
---|
125 | void transfer_list_check(transfer_list_t *instance)
|
---|
126 | {
|
---|
127 | assert(instance);
|
---|
128 | fibril_mutex_lock(&instance->guard);
|
---|
129 | link_t *current = instance->batch_list.next;
|
---|
130 | while (current != &instance->batch_list) {
|
---|
131 | link_t *next = current->next;
|
---|
132 | batch_t *batch = list_get_instance(current, batch_t, link);
|
---|
133 |
|
---|
134 | if (batch_is_complete(batch)) {
|
---|
135 | transfer_list_remove_batch(instance, batch);
|
---|
136 | batch->next_step(batch);
|
---|
137 | }
|
---|
138 | current = next;
|
---|
139 | }
|
---|
140 | fibril_mutex_unlock(&instance->guard);
|
---|
141 | }
|
---|
142 | /**
|
---|
143 | * @}
|
---|
144 | */
|
---|