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
RevLine 
[bc9a629]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
[b371844]33 * @brief Virtual device management (implementation).
[bc9a629]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
[47e3a8e]45#include <usbvirt/hub.h>
[bc9a629]46
47#include "devices.h"
[b8507a1]48#include "hub.h"
[774afaae]49#include "hub/virthub.h"
[b8a3cda]50#include "vhcd.h"
[bc9a629]51
52#define list_foreach(pos, head) \
53 for (pos = (head)->next; pos != (head); \
54 pos = pos->next)
55
[63b4f90]56static LIST_INITIALIZE(devices);
[bc9a629]57
[b371844]58/** Create virtual device.
59 *
60 * @param phone Callback phone.
[6967c14]61 * @param id Device id.
[b371844]62 * @return New device.
[3b77628]63 * @retval NULL Out of memory.
[b371844]64 */
[6967c14]65virtdev_connection_t *virtdev_add_device(int phone, sysarg_t id)
[bc9a629]66{
67 virtdev_connection_t *dev = (virtdev_connection_t *)
68 malloc(sizeof(virtdev_connection_t));
[3b77628]69 if (dev == NULL) {
70 return NULL;
71 }
72
[bc9a629]73 dev->phone = phone;
[6967c14]74 dev->id = id;
[bc9a629]75 list_append(&dev->link, &devices);
76
[774afaae]77 virthub_connect_device(&virtual_hub_device, dev);
[b8507a1]78
[bc9a629]79 return dev;
80}
81
[6967c14]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
[b371844]102/** Destroy virtual device.
103 */
104void virtdev_destroy_device(virtdev_connection_t *dev)
[bc9a629]105{
[774afaae]106 virthub_disconnect_device(&virtual_hub_device, dev);
[bc9a629]107 list_remove(&dev->link);
108 free(dev);
109}
110
[47e3a8e]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{
[4f380d06]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
[47e3a8e]131 link_t *pos;
132 list_foreach(pos, &devices) {
133 virtdev_connection_t *dev
134 = list_get_instance(pos, virtdev_connection_t, link);
135
[774afaae]136 if (!virthub_is_device_enabled(&virtual_hub_device, dev)) {
[b8507a1]137 continue;
138 }
139
[47e3a8e]140 ipc_call_t answer_data;
[a9b6bec]141 sysarg_t answer_rc;
[47e3a8e]142 aid_t req;
[b8a3cda]143 int rc = EOK;
[7a7bfeb3]144 int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
[47e3a8e]145
[7a7bfeb3]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,
[47e3a8e]160 transaction->target.address,
161 transaction->target.endpoint,
[b8a3cda]162 transaction->len,
[47e3a8e]163 &answer_data);
164
[b8a3cda]165 if (transaction->len > 0) {
[7a7bfeb3]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 }
[b8a3cda]173 }
[7a7bfeb3]174
[47e3a8e]175 if (rc != EOK) {
176 async_wait_for(req, NULL);
177 } else {
178 async_wait_for(req, &answer_rc);
[13101d06]179 transaction->actual_len = IPC_GET_ARG1(answer_data);
[47e3a8e]180 rc = (int)answer_rc;
181 }
[4f380d06]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 }
[47e3a8e]190 }
191
[b8507a1]192 /*
193 * Send the data to the virtual hub as well
194 * (if the address matches).
195 */
[774afaae]196 if (virtual_hub_device.address == transaction->target.address) {
[7a7bfeb3]197 size_t tmp;
[9223dc5c]198 dprintf(1, "sending `%s' transaction to hub",
[2185776]199 usbvirt_str_transaction_type(transaction->type));
[7a7bfeb3]200 switch (transaction->type) {
201 case USBVIRT_TRANSACTION_SETUP:
[774afaae]202 virtual_hub_device.transaction_setup(
203 &virtual_hub_device,
[7a7bfeb3]204 transaction->target.endpoint,
205 transaction->buffer, transaction->len);
206 break;
207
208 case USBVIRT_TRANSACTION_IN:
[774afaae]209 virtual_hub_device.transaction_in(
210 &virtual_hub_device,
[7a7bfeb3]211 transaction->target.endpoint,
212 transaction->buffer, transaction->len,
213 &tmp);
[4a5e2f1]214 transaction->actual_len = tmp;
[7a7bfeb3]215 break;
216
217 case USBVIRT_TRANSACTION_OUT:
[774afaae]218 virtual_hub_device.transaction_out(
219 &virtual_hub_device,
[7a7bfeb3]220 transaction->target.endpoint,
221 transaction->buffer, transaction->len);
222 break;
223 }
[2185776]224 dprintf(4, "transaction on hub processed...");
[4f380d06]225 outcome = USB_OUTCOME_OK;
[b8507a1]226 }
227
[47e3a8e]228 /*
229 * TODO: maybe screw some transactions to get more
230 * real-life image.
231 */
[4f380d06]232 return outcome;
[47e3a8e]233}
234
[bc9a629]235/**
236 * @}
237 */
Note: See TracBrowser for help on using the repository browser.