source: mainline/uspace/drv/uhci-hcd/transfer_list.c@ eae83aa

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

Debug output refactored

  • Property mode set to 100644
File size: 5.4 KB
Line 
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
40int 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/*----------------------------------------------------------------------------*/
58void 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/*----------------------------------------------------------------------------*/
68void transfer_list_add_batch(transfer_list_t *instance, batch_t *batch)
69{
70 assert(instance);
71 assert(batch);
72 usb_log_debug2("Adding batch(%p) to queue %s.\n", batch, instance->name);
73
74 uint32_t pa = (uintptr_t)addr_to_phys(batch->qh);
75 assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
76 pa |= LINK_POINTER_QUEUE_HEAD_FLAG;
77
78 batch->qh->next_queue = instance->queue_head->next_queue;
79
80 fibril_mutex_lock(&instance->guard);
81
82 if (instance->queue_head->element == instance->queue_head->next_queue) {
83 /* there is nothing scheduled */
84 list_append(&batch->link, &instance->batch_list);
85 instance->queue_head->element = pa;
86 usb_log_debug("Batch(%p) added to queue %s first.\n",
87 batch, instance->name);
88 fibril_mutex_unlock(&instance->guard);
89 return;
90 }
91 /* now we can be sure that there is someting scheduled */
92 assert(!list_empty(&instance->batch_list));
93 batch_t *first = list_get_instance(
94 instance->batch_list.next, batch_t, link);
95 batch_t *last = list_get_instance(
96 instance->batch_list.prev, batch_t, link);
97 queue_head_append_qh(last->qh, pa);
98 list_append(&batch->link, &instance->batch_list);
99 usb_log_debug("Batch(%p) added to queue %s last, first is %p.\n",
100 batch, instance->name, first );
101 fibril_mutex_unlock(&instance->guard);
102}
103/*----------------------------------------------------------------------------*/
104static void transfer_list_remove_batch(
105 transfer_list_t *instance, batch_t *batch)
106{
107 assert(instance);
108 assert(batch);
109 assert(instance->queue_head);
110 assert(batch->qh);
111 usb_log_debug2("Removing batch(%p) from queue %s.\n", batch, instance->name);
112
113 /* I'm the first one here */
114 if (batch->link.prev == &instance->batch_list) {
115 usb_log_debug("Batch(%p) removed (FIRST) from queue %s, next element %x.\n",
116 batch, instance->name, batch->qh->next_queue);
117 instance->queue_head->element = batch->qh->next_queue;
118 } else {
119 usb_log_debug("Batch(%p) removed (NOT FIRST) from queue, next element %x.\n",
120 batch, instance->name, batch->qh->next_queue);
121 batch_t *prev = list_get_instance(batch->link.prev, batch_t, link);
122 prev->qh->next_queue = batch->qh->next_queue;
123 }
124 list_remove(&batch->link);
125}
126/*----------------------------------------------------------------------------*/
127void transfer_list_remove_finished(transfer_list_t *instance)
128{
129 assert(instance);
130
131 LIST_INITIALIZE(done);
132
133 fibril_mutex_lock(&instance->guard);
134 link_t *current = instance->batch_list.next;
135 while (current != &instance->batch_list) {
136 link_t *next = current->next;
137 batch_t *batch = list_get_instance(current, batch_t, link);
138
139 if (batch_is_complete(batch)) {
140 transfer_list_remove_batch(instance, batch);
141 list_append(current, &done);
142 }
143 current = next;
144 }
145 fibril_mutex_unlock(&instance->guard);
146
147 while (!list_empty(&done)) {
148 link_t *item = done.next;
149 list_remove(item);
150 batch_t *batch = list_get_instance(item, batch_t, link);
151 batch->next_step(batch);
152 }
153}
154/**
155 * @}
156 */
Note: See TracBrowser for help on using the repository browser.