source: mainline/uspace/lib/usbvirt/main.c@ 0a5a950

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

Virtual USB HC under devman:/virt

  • Property mode set to 100644
File size: 7.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 libusbvirt usb
30 * @{
31 */
32/** @file
33 * @brief Device registration with virtual USB framework.
34 */
35#include <devman.h>
36#include <errno.h>
37#include <stdlib.h>
38#include <mem.h>
39#include <assert.h>
40
41#include "hub.h"
42#include "device.h"
43#include "private.h"
44
45#define NAMESPACE "usb"
46
47/** Virtual device wrapper. */
48typedef struct {
49 /** Actual device. */
50 usbvirt_device_t *device;
51 /** Phone to host controller. */
52 int vhcd_phone;
53 /** Device id. */
54 ipcarg_t id;
55 /** Linked-list member. */
56 link_t link;
57} virtual_device_t;
58
59/*** List of known device. */
60static LIST_INITIALIZE(device_list);
61
62/** Find virtual device wrapper based on the contents. */
63static virtual_device_t *find_device(usbvirt_device_t *device)
64{
65 if (list_empty(&device_list)) {
66 return NULL;
67 }
68
69 link_t *pos;
70 for (pos = device_list.next; pos != &device_list; pos = pos->next) {
71 virtual_device_t *dev
72 = list_get_instance(pos, virtual_device_t, link);
73 if (dev->device == device) {
74 return dev;
75 }
76 }
77
78 return NULL;
79}
80
81/** Find virtual device wrapper by its id. */
82static virtual_device_t *find_device_by_id(ipcarg_t id)
83{
84 if (list_empty(&device_list)) {
85 return NULL;
86 }
87
88 link_t *pos;
89 for (pos = device_list.next; pos != &device_list; pos = pos->next) {
90 virtual_device_t *dev
91 = list_get_instance(pos, virtual_device_t, link);
92 if (dev->id == id) {
93 return dev;
94 }
95 }
96
97 return NULL;
98}
99
100/** Reply to a control transfer. */
101static int control_transfer_reply(usbvirt_device_t *device,
102 usb_endpoint_t endpoint, void *buffer, size_t size)
103{
104 usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
105 if (transfer->data != NULL) {
106 free(transfer->data);
107 }
108 transfer->data = malloc(size);
109 memcpy(transfer->data, buffer, size);
110 transfer->data_size = size;
111
112 return EOK;
113}
114
115/** Initialize virtual device. */
116static void device_init(usbvirt_device_t *dev)
117{
118 dev->transaction_out = transaction_out;
119 dev->transaction_setup = transaction_setup;
120 dev->transaction_in = transaction_in;
121
122 dev->control_transfer_reply = control_transfer_reply;
123
124 dev->debug = user_debug;
125 dev->lib_debug = lib_debug;
126
127 dev->state = USBVIRT_STATE_DEFAULT;
128 dev->address = 0;
129 dev->new_address = -1;
130
131 size_t i;
132 for (i = 0; i < USB11_ENDPOINT_MAX; i++) {
133 usbvirt_control_transfer_t *transfer = &dev->current_control_transfers[i];
134 transfer->direction = 0;
135 transfer->request = NULL;
136 transfer->request_size = 0;
137 transfer->data = NULL;
138 transfer->data_size = 0;
139 }
140}
141
142/** Add a virtual device.
143 * The returned device (if not NULL) shall be destroy via destroy_device().
144 */
145static virtual_device_t *add_device(usbvirt_device_t *dev)
146{
147 assert(find_device(dev) == NULL);
148 virtual_device_t *new_device
149 = (virtual_device_t *) malloc(sizeof(virtual_device_t));
150
151 new_device->device = dev;
152 link_initialize(&new_device->link);
153
154 list_append(&new_device->link, &device_list);
155
156 return new_device;
157}
158
159/** Destroy virtual device. */
160static void destroy_device(virtual_device_t *dev)
161{
162 if (dev->vhcd_phone > 0) {
163 ipc_hangup(dev->vhcd_phone);
164 }
165
166 list_remove(&dev->link);
167
168 free(dev);
169}
170
171/** Callback connection handler. */
172static void callback_connection(ipc_callid_t iid, ipc_call_t *icall)
173{
174 // FIXME - determine which device just called back
175 virtual_device_t *dev = find_device_by_id(0);
176 if (dev == NULL) {
177 ipc_answer_0(iid, EINVAL);
178 printf("Ooops\n");
179 return;
180 }
181
182 device_callback_connection(dev->device, iid, icall);
183}
184
185/** Create necessary phones for communication with virtual HCD.
186 * This function wraps following calls:
187 * -# open <code>/dev/devices/\\virt\\usbhc for reading
188 * -# access phone of file opened in previous step
189 * -# create callback through just opened phone
190 * -# create handler for calling on data from host to function
191 * -# return the (outgoing) phone
192 *
193 * @warning This function is wrapper for several actions and therefore
194 * it is not possible - in case of error - to determine at which point
195 * error occurred.
196 *
197 * @param dev Device to connect.
198 * @return EOK on success or error code from errno.h.
199 */
200int usbvirt_connect(usbvirt_device_t *dev)
201{
202 virtual_device_t *virtual_device = find_device(dev);
203 if (virtual_device != NULL) {
204 return EEXISTS;
205 }
206
207 const char *vhc_path = "/virt/usbhc";
208 int rc;
209 devman_handle_t handle;
210
211 rc = devman_device_get_handle(vhc_path, &handle, 0);
212 if (rc != EOK) {
213 printf("devman_device_get_handle() failed\n");
214 return rc;
215 }
216
217 int hcd_phone = devman_device_connect(handle, 0);
218
219 if (hcd_phone < 0) {
220 printf("devman_device_connect() failed\n");
221 return hcd_phone;
222 }
223
224 ipcarg_t phonehash;
225 rc = ipc_connect_to_me(hcd_phone, 0, 0, 0, &phonehash);
226 if (rc != EOK) {
227 printf("ipc_connect_to_me() failed\n");
228 return rc;
229 }
230
231 device_init(dev);
232
233 virtual_device = add_device(dev);
234 virtual_device->vhcd_phone = hcd_phone;
235 virtual_device->id = 0;
236
237 async_new_connection(phonehash, 0, NULL, callback_connection);
238
239 return EOK;
240}
241
242/** Prepares device as local.
243 * This is useful if you want to have a virtual device in the same task
244 * as HCD.
245 *
246 * @param dev Device to connect.
247 * @return Error code.
248 * @retval EOK Device connected.
249 * @retval EEXISTS This device is already connected.
250 */
251int usbvirt_connect_local(usbvirt_device_t *dev)
252{
253 virtual_device_t *virtual_device = find_device(dev);
254 if (virtual_device != NULL) {
255 return EEXISTS;
256 }
257
258 device_init(dev);
259
260 virtual_device = add_device(dev);
261 virtual_device->vhcd_phone = -1;
262 virtual_device->id = 0;
263
264 return EOK;
265}
266
267/** Disconnects device from HCD.
268 *
269 * @param dev Device to be disconnected.
270 * @return Error code.
271 * @retval EOK Device connected.
272 * @retval ENOENT This device is not connected.
273 */
274int usbvirt_disconnect(usbvirt_device_t *dev)
275{
276 virtual_device_t *virtual_device = find_device(dev);
277 if (virtual_device == NULL) {
278 return ENOENT;
279 }
280
281 destroy_device(virtual_device);
282
283 return EOK;
284}
285
286
287/**
288 * @}
289 */
Note: See TracBrowser for help on using the repository browser.