source: mainline/uspace/lib/usbvirt/src/main.c@ bd8c753d

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

Doxygen group for USB virtualization

Virtual host controller and libusbvirt has separate Doxygen groups because
the usb group was polluted way too much.

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