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

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

Further refactoring

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