source: mainline/uspace/drv/vhc/devices.c@ 6427cf67

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

Removal of usb_outcome_t (ticket 55)

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