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 "vhcd.h"
|
---|
50 |
|
---|
51 | #define list_foreach(pos, head) \
|
---|
52 | for (pos = (head)->next; pos != (head); \
|
---|
53 | pos = pos->next)
|
---|
54 |
|
---|
55 | static LIST_INITIALIZE(devices);
|
---|
56 |
|
---|
57 | /** Create virtual device.
|
---|
58 | *
|
---|
59 | * @param address USB address.
|
---|
60 | * @param phone Callback phone.
|
---|
61 | * @return New device.
|
---|
62 | * @retval NULL Out of memory or address already occupied.
|
---|
63 | */
|
---|
64 | virtdev_connection_t *virtdev_add_device(int phone)
|
---|
65 | {
|
---|
66 | virtdev_connection_t *dev = (virtdev_connection_t *)
|
---|
67 | malloc(sizeof(virtdev_connection_t));
|
---|
68 | dev->phone = phone;
|
---|
69 | list_append(&dev->link, &devices);
|
---|
70 |
|
---|
71 | hub_add_device(dev);
|
---|
72 |
|
---|
73 | return dev;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Destroy virtual device.
|
---|
77 | */
|
---|
78 | void virtdev_destroy_device(virtdev_connection_t *dev)
|
---|
79 | {
|
---|
80 | hub_remove_device(dev);
|
---|
81 | list_remove(&dev->link);
|
---|
82 | free(dev);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Send data to all connected devices.
|
---|
86 | *
|
---|
87 | * @param transaction Transaction to be sent over the bus.
|
---|
88 | */
|
---|
89 | usb_transaction_outcome_t virtdev_send_to_all(transaction_t *transaction)
|
---|
90 | {
|
---|
91 | link_t *pos;
|
---|
92 | list_foreach(pos, &devices) {
|
---|
93 | virtdev_connection_t *dev
|
---|
94 | = list_get_instance(pos, virtdev_connection_t, link);
|
---|
95 |
|
---|
96 | if (!hub_can_device_signal(dev)) {
|
---|
97 | continue;
|
---|
98 | }
|
---|
99 |
|
---|
100 | ipc_call_t answer_data;
|
---|
101 | ipcarg_t answer_rc;
|
---|
102 | aid_t req;
|
---|
103 | int rc = EOK;
|
---|
104 | int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
|
---|
105 |
|
---|
106 | switch (transaction->type) {
|
---|
107 | case USBVIRT_TRANSACTION_SETUP:
|
---|
108 | method = IPC_M_USBVIRT_TRANSACTION_SETUP;
|
---|
109 | break;
|
---|
110 | case USBVIRT_TRANSACTION_IN:
|
---|
111 | method = IPC_M_USBVIRT_TRANSACTION_IN;
|
---|
112 | break;
|
---|
113 | case USBVIRT_TRANSACTION_OUT:
|
---|
114 | method = IPC_M_USBVIRT_TRANSACTION_OUT;
|
---|
115 | break;
|
---|
116 | }
|
---|
117 |
|
---|
118 | req = async_send_3(dev->phone,
|
---|
119 | method,
|
---|
120 | transaction->target.address,
|
---|
121 | transaction->target.endpoint,
|
---|
122 | transaction->len,
|
---|
123 | &answer_data);
|
---|
124 |
|
---|
125 | if (transaction->len > 0) {
|
---|
126 | if (transaction->type == USBVIRT_TRANSACTION_IN) {
|
---|
127 | rc = async_data_read_start(dev->phone,
|
---|
128 | transaction->buffer, transaction->len);
|
---|
129 | } else {
|
---|
130 | rc = async_data_write_start(dev->phone,
|
---|
131 | transaction->buffer, transaction->len);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (rc != EOK) {
|
---|
136 | async_wait_for(req, NULL);
|
---|
137 | } else {
|
---|
138 | async_wait_for(req, &answer_rc);
|
---|
139 | rc = (int)answer_rc;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /*
|
---|
144 | * Send the data to the virtual hub as well
|
---|
145 | * (if the address matches).
|
---|
146 | */
|
---|
147 | if (virthub_dev.address == transaction->target.address) {
|
---|
148 | size_t tmp;
|
---|
149 | dprintf(1, "sending `%s' transaction to hub",
|
---|
150 | usbvirt_str_transaction_type(transaction->type));
|
---|
151 | switch (transaction->type) {
|
---|
152 | case USBVIRT_TRANSACTION_SETUP:
|
---|
153 | virthub_dev.transaction_setup(&virthub_dev,
|
---|
154 | transaction->target.endpoint,
|
---|
155 | transaction->buffer, transaction->len);
|
---|
156 | break;
|
---|
157 |
|
---|
158 | case USBVIRT_TRANSACTION_IN:
|
---|
159 | virthub_dev.transaction_in(&virthub_dev,
|
---|
160 | transaction->target.endpoint,
|
---|
161 | transaction->buffer, transaction->len,
|
---|
162 | &tmp);
|
---|
163 | if (tmp < transaction->len) {
|
---|
164 | transaction->len = tmp;
|
---|
165 | }
|
---|
166 | break;
|
---|
167 |
|
---|
168 | case USBVIRT_TRANSACTION_OUT:
|
---|
169 | virthub_dev.transaction_out(&virthub_dev,
|
---|
170 | transaction->target.endpoint,
|
---|
171 | transaction->buffer, transaction->len);
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | dprintf(4, "transaction on hub processed...");
|
---|
175 | }
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * TODO: maybe screw some transactions to get more
|
---|
179 | * real-life image.
|
---|
180 | */
|
---|
181 | return USB_OUTCOME_OK;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * @}
|
---|
186 | */
|
---|