source: mainline/uspace/srv/hw/bus/usb/hcd/virtual/hc.c@ b8a3cda

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b8a3cda was b8a3cda, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

VHCD handles new methods

Not completely, though. Will finish later.

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
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
29/** @addtogroup usb
30 * @{
31 */
32/** @file
33 * @brief Virtual HC (implementation).
34 */
35
36#include <ipc/ipc.h>
37#include <adt/list.h>
38#include <bool.h>
39#include <async.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <errno.h>
43#include <str_error.h>
44
45#include <usbvirt/hub.h>
46
47#include "vhcd.h"
48#include "hc.h"
49#include "devices.h"
50
51#define USLEEP_BASE (500 * 1000)
52
53#define USLEEP_VAR 5000
54
55#define SHORTENING_VAR 15
56
57#define PROB_OUTCOME_BABBLE 5
58#define PROB_OUTCOME_CRCERROR 7
59
60#define PROB_TEST(var, new_value, prob, number) \
61 do { \
62 if (((number) % (prob)) == 0) { \
63 var = (new_value); \
64 } \
65 } while (0)
66
67static link_t transaction_to_device_list;
68static link_t transaction_from_device_list;
69
70#define TRANSACTION_FORMAT "T[%d:%d (%d)]"
71#define TRANSACTION_PRINTF(t) \
72 (t).target.address, (t).target.endpoint, \
73 (int)(t).len
74
75#define transaction_get_instance(lnk) \
76 list_get_instance(lnk, transaction_t, link)
77
78static inline unsigned int pseudo_random(unsigned int *seed)
79{
80 *seed = ((*seed) * 873511) % 22348977 + 7;
81 return ((*seed) >> 8);
82}
83
84/** Call transaction callback.
85 * Calling this callback informs the backend that transaction was processed.
86 */
87static void process_transaction_with_outcome(transaction_t * transaction,
88 usb_transaction_outcome_t outcome)
89{
90 dprintf("processing transaction " TRANSACTION_FORMAT ", outcome: %s",
91 TRANSACTION_PRINTF(*transaction),
92 usb_str_transaction_outcome(outcome));
93
94 transaction->callback(transaction->buffer, transaction->len, outcome,
95 transaction->callback_arg);
96}
97
98/** Host controller manager main function.
99 */
100void hc_manager(void)
101{
102 list_initialize(&transaction_to_device_list);
103 list_initialize(&transaction_from_device_list);
104
105 static unsigned int seed = 4573;
106
107 printf("%s: transaction processor ready.\n", NAME);
108
109 while (true) {
110 async_usleep(USLEEP_BASE + (pseudo_random(&seed) % USLEEP_VAR));
111
112 if (list_empty(&transaction_to_device_list)) {
113 continue;
114 }
115
116 link_t *first_transaction_link = transaction_to_device_list.next;
117 transaction_t *transaction
118 = transaction_get_instance(first_transaction_link);
119 list_remove(first_transaction_link);
120
121 usb_transaction_outcome_t outcome;
122 outcome = virtdev_send_to_all(transaction);
123
124 process_transaction_with_outcome(transaction, outcome);
125
126 free(transaction);
127 }
128}
129
130/** Create new transaction
131 */
132static transaction_t *transaction_create(usbvirt_transaction_type_t type,
133 usb_target_t target,
134 void * buffer, size_t len,
135 hc_transaction_done_callback_t callback, void * arg)
136{
137 transaction_t * transaction = malloc(sizeof(transaction_t));
138
139 list_initialize(&transaction->link);
140 transaction->type = type;
141 transaction->target = target;
142 transaction->buffer = buffer;
143 transaction->len = len;
144 transaction->callback = callback;
145 transaction->callback_arg = arg;
146
147 return transaction;
148}
149
150/** Add transaction directioned towards the device.
151 */
152void hc_add_transaction_to_device(bool setup, usb_target_t target,
153 void * buffer, size_t len,
154 hc_transaction_done_callback_t callback, void * arg)
155{
156 transaction_t *transaction = transaction_create(
157 setup ? USBVIRT_TRANSACTION_SETUP : USBVIRT_TRANSACTION_OUT, target,
158 buffer, len, callback, arg);
159 list_append(&transaction->link, &transaction_to_device_list);
160}
161
162/** Add transaction directioned from the device.
163 */
164void hc_add_transaction_from_device(usb_target_t target,
165 void * buffer, size_t len,
166 hc_transaction_done_callback_t callback, void * arg)
167{
168 transaction_t *transaction = transaction_create(USBVIRT_TRANSACTION_IN,
169 target, buffer, len, callback, arg);
170 list_append(&transaction->link, &transaction_from_device_list);
171}
172
173/** Fill data to existing transaction from device.
174 */
175int hc_fillin_transaction_from_device(usb_target_t target,
176 void * buffer, size_t len)
177{
178 dprintf("finding transaction to fill data in (%d:%d)...",
179 target.address, target.endpoint);
180 int rc;
181
182 /*
183 * Find correct transaction envelope in the list.
184 */
185 if (list_empty(&transaction_from_device_list)) {
186 rc = ENOENT;
187 goto leave;
188 }
189
190 transaction_t *transaction = NULL;
191 link_t *pos = transaction_from_device_list.next;
192
193 while (pos != &transaction_from_device_list) {
194 transaction_t *t = transaction_get_instance(pos);
195 if (usb_target_same(t->target, target)) {
196 transaction = t;
197 break;
198 }
199 pos = pos->next;
200 }
201 if (transaction == NULL) {
202 rc = ENOENT;
203 goto leave;
204 }
205
206 /*
207 * Remove the transaction from the list as it will be processed now.
208 */
209 list_remove(&transaction->link);
210
211 if (transaction->len < len) {
212 process_transaction_with_outcome(transaction, USB_OUTCOME_BABBLE);
213 rc = ENOMEM;
214 goto leave;
215 }
216
217 /*
218 * Copy the data and finish processing the transaction.
219 */
220 transaction->len = len;
221 memcpy(transaction->buffer, buffer, len);
222
223 process_transaction_with_outcome(transaction, USB_OUTCOME_OK);
224
225 dprintf(" ...transaction " TRANSACTION_FORMAT " sent back",
226 TRANSACTION_PRINTF(*transaction));
227
228
229 free(transaction);
230 rc = EOK;
231
232leave:
233 dprintf(" ...fill-in transaction: %s", str_error(rc));
234
235 return rc;
236}
237
238/**
239 * @}
240 */
Note: See TracBrowser for help on using the repository browser.