source: mainline/uspace/srv/hw/bus/usb/hcd/virtual/devices.c@ 7a7bfeb3

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

Virtual USB overhaul almost complete

The virtual HC, hub and keyboard are rewritten after changes to HCD API.
Comments will be added later.

  • Property mode set to 100644
File size: 5.2 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/ids.h>
46#include <usbvirt/hub.h>
47
48#include "devices.h"
49#include "hub.h"
50#include "vhcd.h"
51
52#define list_foreach(pos, head) \
53 for (pos = (head)->next; pos != (head); \
54 pos = pos->next)
55
56LIST_INITIALIZE(devices);
57
58/** Recognise device by id.
59 *
60 * @param id Device id.
61 * @param phone Callback phone.
62 */
63virtdev_connection_t *virtdev_recognise(int id, int phone)
64{
65 virtdev_connection_t * dev = virtdev_add_device(phone);
66
67 /*
68 * We do not want to mess-up the virtdev_add_device() as
69 * the id is needed only before device probing/detection
70 * is implemented.
71 *
72 * However, that does not mean that this will happen soon.
73 */
74 if (dev) {
75 dev->id = id;
76 }
77
78 return dev;
79}
80
81/** Create virtual device.
82 *
83 * @param address USB address.
84 * @param phone Callback phone.
85 * @return New device.
86 * @retval NULL Out of memory or address already occupied.
87 */
88virtdev_connection_t *virtdev_add_device(int phone)
89{
90 virtdev_connection_t *dev = (virtdev_connection_t *)
91 malloc(sizeof(virtdev_connection_t));
92 dev->phone = phone;
93 list_append(&dev->link, &devices);
94
95 hub_add_device(dev);
96
97 return dev;
98}
99
100/** Destroy virtual device.
101 */
102void virtdev_destroy_device(virtdev_connection_t *dev)
103{
104 hub_remove_device(dev);
105 list_remove(&dev->link);
106 free(dev);
107}
108
109/** Send data to all connected devices.
110 *
111 * @param transaction Transaction to be sent over the bus.
112 */
113usb_transaction_outcome_t virtdev_send_to_all(transaction_t *transaction)
114{
115 link_t *pos;
116 list_foreach(pos, &devices) {
117 virtdev_connection_t *dev
118 = list_get_instance(pos, virtdev_connection_t, link);
119
120 if (!hub_can_device_signal(dev)) {
121 continue;
122 }
123
124 ipc_call_t answer_data;
125 ipcarg_t answer_rc;
126 aid_t req;
127 int rc = EOK;
128 int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
129
130 switch (transaction->type) {
131 case USBVIRT_TRANSACTION_SETUP:
132 method = IPC_M_USBVIRT_TRANSACTION_SETUP;
133 break;
134 case USBVIRT_TRANSACTION_IN:
135 method = IPC_M_USBVIRT_TRANSACTION_IN;
136 break;
137 case USBVIRT_TRANSACTION_OUT:
138 method = IPC_M_USBVIRT_TRANSACTION_OUT;
139 break;
140 }
141
142 req = async_send_3(dev->phone,
143 method,
144 transaction->target.address,
145 transaction->target.endpoint,
146 transaction->len,
147 &answer_data);
148
149 if (transaction->len > 0) {
150 if (transaction->type == USBVIRT_TRANSACTION_IN) {
151 rc = async_data_read_start(dev->phone,
152 transaction->buffer, transaction->len);
153 } else {
154 rc = async_data_write_start(dev->phone,
155 transaction->buffer, transaction->len);
156 }
157 }
158
159 if (rc != EOK) {
160 async_wait_for(req, NULL);
161 } else {
162 async_wait_for(req, &answer_rc);
163 rc = (int)answer_rc;
164 }
165 }
166
167 /*
168 * Send the data to the virtual hub as well
169 * (if the address matches).
170 */
171 if (virthub_dev.address == transaction->target.address) {
172 size_t tmp;
173 switch (transaction->type) {
174 case USBVIRT_TRANSACTION_SETUP:
175 virthub_dev.transaction_setup(&virthub_dev,
176 transaction->target.endpoint,
177 transaction->buffer, transaction->len);
178 break;
179
180 case USBVIRT_TRANSACTION_IN:
181 virthub_dev.transaction_in(&virthub_dev,
182 transaction->target.endpoint,
183 transaction->buffer, transaction->len,
184 &tmp);
185 if (tmp < transaction->len) {
186 transaction->len = tmp;
187 }
188 break;
189
190 case USBVIRT_TRANSACTION_OUT:
191 virthub_dev.transaction_out(&virthub_dev,
192 transaction->target.endpoint,
193 transaction->buffer, transaction->len);
194 break;
195 }
196 }
197
198 /*
199 * TODO: maybe screw some transactions to get more
200 * real-life image.
201 */
202 return USB_OUTCOME_OK;
203}
204
205/**
206 * @}
207 */
Note: See TracBrowser for help on using the repository browser.