source: mainline/uspace/drv/vhc/devices.c@ 4f380d06

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

Virtual HC report transaction outcome

  • Property mode set to 100644
File size: 5.7 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 device management (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 "devices.h"
48#include "hub.h"
49#include "hub/virthub.h"
50#include "vhcd.h"
51
52#define list_foreach(pos, head) \
53 for (pos = (head)->next; pos != (head); \
54 pos = pos->next)
55
56static LIST_INITIALIZE(devices);
57
58/** Create virtual device.
59 *
60 * @param address USB address.
61 * @param phone Callback phone.
62 * @return New device.
63 * @retval NULL Out of memory or address already occupied.
64 */
65virtdev_connection_t *virtdev_add_device(int phone)
66{
67 virtdev_connection_t *dev = (virtdev_connection_t *)
68 malloc(sizeof(virtdev_connection_t));
69 dev->phone = phone;
70 list_append(&dev->link, &devices);
71
72 virthub_connect_device(&virtual_hub_device, dev);
73
74 return dev;
75}
76
77/** Destroy virtual device.
78 */
79void virtdev_destroy_device(virtdev_connection_t *dev)
80{
81 virthub_disconnect_device(&virtual_hub_device, dev);
82 list_remove(&dev->link);
83 free(dev);
84}
85
86/** Send data to all connected devices.
87 *
88 * @param transaction Transaction to be sent over the bus.
89 */
90usb_transaction_outcome_t virtdev_send_to_all(transaction_t *transaction)
91{
92 /* For easier debugging. */
93 switch (transaction->type) {
94 case USBVIRT_TRANSACTION_SETUP:
95 case USBVIRT_TRANSACTION_OUT:
96 transaction->actual_len = transaction->len;
97 break;
98 case USBVIRT_TRANSACTION_IN:
99 transaction->actual_len = 0;
100 break;
101 default:
102 assert(false && "unreachable branch in switch()");
103 }
104 usb_transaction_outcome_t outcome = USB_OUTCOME_BABBLE;
105
106 link_t *pos;
107 list_foreach(pos, &devices) {
108 virtdev_connection_t *dev
109 = list_get_instance(pos, virtdev_connection_t, link);
110
111 if (!virthub_is_device_enabled(&virtual_hub_device, dev)) {
112 continue;
113 }
114
115 ipc_call_t answer_data;
116 sysarg_t answer_rc;
117 aid_t req;
118 int rc = EOK;
119 int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
120
121 switch (transaction->type) {
122 case USBVIRT_TRANSACTION_SETUP:
123 method = IPC_M_USBVIRT_TRANSACTION_SETUP;
124 break;
125 case USBVIRT_TRANSACTION_IN:
126 method = IPC_M_USBVIRT_TRANSACTION_IN;
127 break;
128 case USBVIRT_TRANSACTION_OUT:
129 method = IPC_M_USBVIRT_TRANSACTION_OUT;
130 break;
131 }
132
133 req = async_send_3(dev->phone,
134 method,
135 transaction->target.address,
136 transaction->target.endpoint,
137 transaction->len,
138 &answer_data);
139
140 if (transaction->len > 0) {
141 if (transaction->type == USBVIRT_TRANSACTION_IN) {
142 rc = async_data_read_start(dev->phone,
143 transaction->buffer, transaction->len);
144 } else {
145 rc = async_data_write_start(dev->phone,
146 transaction->buffer, transaction->len);
147 }
148 }
149
150 if (rc != EOK) {
151 async_wait_for(req, NULL);
152 } else {
153 async_wait_for(req, &answer_rc);
154 transaction->actual_len = IPC_GET_ARG1(answer_data);
155 rc = (int)answer_rc;
156 }
157
158 /*
159 * If at least one device was able to accept this
160 * transaction and process it, we can announce success.
161 */
162 if (rc == EOK) {
163 outcome = USB_OUTCOME_OK;
164 }
165 }
166
167 /*
168 * Send the data to the virtual hub as well
169 * (if the address matches).
170 */
171 if (virtual_hub_device.address == transaction->target.address) {
172 size_t tmp;
173 dprintf(1, "sending `%s' transaction to hub",
174 usbvirt_str_transaction_type(transaction->type));
175 switch (transaction->type) {
176 case USBVIRT_TRANSACTION_SETUP:
177 virtual_hub_device.transaction_setup(
178 &virtual_hub_device,
179 transaction->target.endpoint,
180 transaction->buffer, transaction->len);
181 break;
182
183 case USBVIRT_TRANSACTION_IN:
184 virtual_hub_device.transaction_in(
185 &virtual_hub_device,
186 transaction->target.endpoint,
187 transaction->buffer, transaction->len,
188 &tmp);
189 if (tmp < transaction->len) {
190 transaction->len = tmp;
191 }
192 break;
193
194 case USBVIRT_TRANSACTION_OUT:
195 virtual_hub_device.transaction_out(
196 &virtual_hub_device,
197 transaction->target.endpoint,
198 transaction->buffer, transaction->len);
199 break;
200 }
201 dprintf(4, "transaction on hub processed...");
202 outcome = USB_OUTCOME_OK;
203 }
204
205 /*
206 * TODO: maybe screw some transactions to get more
207 * real-life image.
208 */
209 return outcome;
210}
211
212/**
213 * @}
214 */
Note: See TracBrowser for help on using the repository browser.