source: mainline/uspace/drv/vhc/devices.c@ 6967c14

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

VHC registers device unplugging

The virtual host controller is now able to register that a virtual USB device
was unplugged (i.e. that the task representing the device closed the
connection).

However, the change is not yet propagated to the root hub of the host
controller.

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