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

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

Virtual HCD refactoring

Splitted into more files, added more comments.

  • Property mode set to 100644
File size: 7.8 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 <usb/virtdev.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 10000
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/** Pending transaction details. */
71typedef struct {
72 /** Linked-list link. */
73 link_t link;
74 /** Device address. */
75 usb_target_t target;
76 /** Direction of the transaction. */
77 usb_direction_t direction;
78 /** Transfer type. */
79 usb_transfer_type_t type;
80 /** Transaction data buffer. */
81 void * buffer;
82 /** Transaction data length. */
83 size_t len;
84 /** Callback after transaction is done. */
85 hc_transaction_done_callback_t callback;
86 /** Argument to the callback. */
87 void * callback_arg;
88} transaction_t;
89
90#define TRANSACTION_FORMAT "T[%d:%d %s %s (%d)]"
91#define TRANSACTION_PRINTF(t) \
92 (t).target.address, (t).target.endpoint, \
93 usb_str_transfer_type((t).type), \
94 ((t).direction == USB_DIRECTION_IN ? "in" : "out"), \
95 (int)(t).len
96
97#define transaction_get_instance(lnk) \
98 list_get_instance(lnk, transaction_t, link)
99
100static inline unsigned int pseudo_random(unsigned int *seed)
101{
102 *seed = ((*seed) * 873511) % 22348977 + 7;
103 return ((*seed) >> 8);
104}
105
106/** Call transaction callback.
107 * Calling this callback informs the backend that transaction was processed.
108 */
109static void process_transaction_with_outcome(transaction_t * transaction,
110 usb_transaction_outcome_t outcome)
111{
112 dprintf("processing transaction " TRANSACTION_FORMAT ", outcome: %s",
113 TRANSACTION_PRINTF(*transaction),
114 usb_str_transaction_outcome(outcome));
115
116 transaction->callback(transaction->buffer, transaction->len, outcome,
117 transaction->callback_arg);
118}
119
120/** Host controller manager main function.
121 */
122void hc_manager(void)
123{
124 list_initialize(&transaction_to_device_list);
125 list_initialize(&transaction_from_device_list);
126
127 static unsigned int seed = 4573;
128
129 printf("%s: transaction processor ready.\n", NAME);
130
131 while (true) {
132 async_usleep(USLEEP_BASE + (pseudo_random(&seed) % USLEEP_VAR));
133
134 if (list_empty(&transaction_to_device_list)) {
135 continue;
136 }
137
138 link_t * first_transaction_link = transaction_to_device_list.next;
139 transaction_t * transaction
140 = transaction_get_instance(first_transaction_link);
141 list_remove(first_transaction_link);
142
143 virtdev_connection_t *dev = virtdev_find_by_address(
144 transaction->target.address);
145 usb_transaction_outcome_t outcome = USB_OUTCOME_OK;
146
147 if (dev != NULL) {
148 dprintf("sending data to device at %d.%d (phone %d)",
149 dev->address, transaction->target.endpoint,
150 dev->phone);
151 ipc_call_t answer_data;
152 ipcarg_t answer_rc;
153 aid_t req;
154 int rc;
155
156 req = async_send_2(dev->phone,
157 IPC_M_USB_VIRTDEV_DATA_TO_DEVICE,
158 transaction->target.endpoint,
159 transaction->type,
160 &answer_data);
161
162 rc = async_data_write_start(dev->phone,
163 transaction->buffer, transaction->len);
164 if (rc != EOK) {
165 async_wait_for(req, NULL);
166 } else {
167 async_wait_for(req, &answer_rc);
168 rc = (int)answer_rc;
169 }
170
171 if (rc != EOK) {
172 outcome = USB_OUTCOME_BABBLE;
173 }
174 } else {
175 outcome = USB_OUTCOME_CRCERROR;
176 }
177
178 process_transaction_with_outcome(transaction, outcome);
179
180 free(transaction);
181 }
182}
183
184/** Create new transaction
185 */
186static transaction_t *transaction_create(usb_transfer_type_t type, usb_target_t target,
187 usb_direction_t direction,
188 void * buffer, size_t len,
189 hc_transaction_done_callback_t callback, void * arg)
190{
191 transaction_t * transaction = malloc(sizeof(transaction_t));
192
193 list_initialize(&transaction->link);
194 transaction->type = type;
195 transaction->target = target;
196 transaction->direction = direction;
197 transaction->buffer = buffer;
198 transaction->len = len;
199 transaction->callback = callback;
200 transaction->callback_arg = arg;
201
202 return transaction;
203}
204
205/** Add transaction directioned towards the device.
206 */
207void hc_add_transaction_to_device(usb_transfer_type_t type, usb_target_t target,
208 void * buffer, size_t len,
209 hc_transaction_done_callback_t callback, void * arg)
210{
211 transaction_t *transaction = transaction_create(type, target,
212 USB_DIRECTION_OUT, buffer, len, callback, arg);
213 list_append(&transaction->link, &transaction_to_device_list);
214}
215
216/** Add transaction directioned from the device.
217 */
218void hc_add_transaction_from_device(usb_transfer_type_t type, usb_target_t target,
219 void * buffer, size_t len,
220 hc_transaction_done_callback_t callback, void * arg)
221{
222 transaction_t *transaction = transaction_create(type, target,
223 USB_DIRECTION_IN, buffer, len, callback, arg);
224 list_append(&transaction->link, &transaction_from_device_list);
225}
226
227/** Fill data to existing transaction from device.
228 */
229int hc_fillin_transaction_from_device(usb_transfer_type_t type, usb_target_t target,
230 void * buffer, size_t len)
231{
232 dprintf("finding transaction to fill data in...");
233 int rc;
234
235 /*
236 * Find correct transaction envelope in the list.
237 */
238 if (list_empty(&transaction_from_device_list)) {
239 rc = ENOENT;
240 goto leave;
241 }
242
243 transaction_t *transaction = NULL;
244 link_t *pos = transaction_from_device_list.next;
245
246 while (pos != &transaction_from_device_list) {
247 transaction_t *t = transaction_get_instance(pos);
248 if (usb_target_same(t->target, target)) {
249 transaction = t;
250 break;
251 }
252 pos = pos->next;
253 }
254 if (transaction == NULL) {
255 rc = ENOENT;
256 goto leave;
257 }
258
259 /*
260 * Remove the transaction from the list as it will be processed now.
261 */
262 list_remove(&transaction->link);
263
264 if (transaction->len < len) {
265 process_transaction_with_outcome(transaction, USB_OUTCOME_BABBLE);
266 rc = ENOMEM;
267 goto leave;
268 }
269
270 /*
271 * Copy the data and finish processing the transaction.
272 */
273 transaction->len = len;
274 memcpy(transaction->buffer, buffer, len);
275
276 process_transaction_with_outcome(transaction, USB_OUTCOME_OK);
277
278 dprintf(" ...transaction " TRANSACTION_FORMAT " sent back",
279 TRANSACTION_PRINTF(*transaction));
280
281
282 free(transaction);
283 rc = EOK;
284
285leave:
286 dprintf(" ...fill-in transaction: %s", str_error(rc));
287
288 return rc;
289}
290
291/**
292 * @}
293 */
Note: See TracBrowser for help on using the repository browser.