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

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

Merge mainline changes (DDF refactoring)

This merge includes DDF refactoring that brought multifunctional devices
(i.e. ddf_dev_t and ddf_fun_t). Please, see ticket #295 at HelenOS
upstream Trac.

The conflicts themselves were easy to solve (merely several renamings).

Changes to USB subsystem:

  • drivers uses ddf_dev_t and ddf_fun_t
  • different signatures of many library functions
  • several hacks around communication with parent device (now the communication is clearer and somehow what we have now is hack about other hacks)
    • will repair and clean later
  • maybe added some extra debugging messages (the diff has about 240K, and I admit I have no energy to double check that)

WARNING:

  • the diff is VERY long, recommended is viewing partial diffs of the merge (i.e. merges in mainline branch that lead to the parent one)
  • merging with your branches might involve huge renamings, sorry, no other way is possible

BUGS:

  • hub driver will not work (no function created)

GOOD NEWS:

  • QEMU keyboard seems to work with QEMU 0.13 and 0.14
  • we are up-to-date with mainline again
  • Property mode set to 100644
File size: 7.1 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
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 "private.h"
42
43#define NAMESPACE "usb"
44
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. */
52 sysarg_t id;
53 /** Linked-list member. */
54 link_t link;
55} virtual_device_t;
56
57/*** List of known device. */
58static LIST_INITIALIZE(device_list);
59
60/** Find virtual device wrapper based on the contents. */
61static virtual_device_t *find_device(usbvirt_device_t *device)
62{
63 if (list_empty(&device_list)) {
64 return NULL;
65 }
66
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;
73 }
74 }
75
76 return NULL;
77}
78
79/** Find virtual device wrapper by its id. */
80static virtual_device_t *find_device_by_id(sysarg_t id)
81{
82 if (list_empty(&device_list)) {
83 return NULL;
84 }
85
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;
92 }
93 }
94
95 return NULL;
96}
97
98/** Reply to a control transfer. */
99static int control_transfer_reply(usbvirt_device_t *device,
100 usb_endpoint_t endpoint, void *buffer, size_t size)
101{
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;
111}
112
113/** Initialize virtual device. */
114static void device_init(usbvirt_device_t *dev)
115{
116 dev->transaction_out = transaction_out;
117 dev->transaction_setup = transaction_setup;
118 dev->transaction_in = transaction_in;
119
120 dev->control_transfer_reply = control_transfer_reply;
121
122 dev->debug = user_debug;
123 dev->lib_debug = lib_debug;
124
125 dev->state = USBVIRT_STATE_DEFAULT;
126 dev->address = 0;
127 dev->new_address = -1;
128
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;
137 }
138}
139
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 async_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 async_answer_0(iid, EINVAL);
176 printf("Ooops\n");
177 return;
178 }
179
180 device_callback_connection(dev->device, iid, icall);
181}
182
183/** Create necessary phones for communication with virtual HCD.
184 * This function wraps following calls:
185 * -# open <code>/dev/devices/\\virt\\usbhc</code> for reading
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
193 * error occurred.
194 *
195 * @param dev Device to connect.
196 * @return EOK on success or error code from errno.h.
197 */
198int usbvirt_connect(usbvirt_device_t *dev)
199{
200 virtual_device_t *virtual_device = find_device(dev);
201 if (virtual_device != NULL) {
202 return EEXISTS;
203 }
204
205 const char *vhc_path = "/virt/usbhc/hc";
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;
213 }
214
215 int hcd_phone = devman_device_connect(handle, 0);
216
217 if (hcd_phone < 0) {
218 printf("devman_device_connect() failed\n");
219 return hcd_phone;
220 }
221
222 rc = async_connect_to_me(hcd_phone, 0, 0, 0, callback_connection);
223 if (rc != EOK) {
224 printf("ipc_connect_to_me() failed\n");
225 return rc;
226 }
227
228 device_init(dev);
229
230 virtual_device = add_device(dev);
231 virtual_device->vhcd_phone = hcd_phone;
232 virtual_device->id = 0;
233
234 return EOK;
235}
236
237/** Prepares device as local.
238 * This is useful if you want to have a virtual device in the same task
239 * as HCD.
240 *
241 * @param dev Device to connect.
242 * @return Error code.
243 * @retval EOK Device connected.
244 * @retval EEXISTS This device is already connected.
245 */
246int usbvirt_connect_local(usbvirt_device_t *dev)
247{
248 virtual_device_t *virtual_device = find_device(dev);
249 if (virtual_device != NULL) {
250 return EEXISTS;
251 }
252
253 device_init(dev);
254
255 virtual_device = add_device(dev);
256 virtual_device->vhcd_phone = -1;
257 virtual_device->id = 0;
258
259 return EOK;
260}
261
262/** Disconnects device from HCD.
263 *
264 * @param dev Device to be disconnected.
265 * @return Error code.
266 * @retval EOK Device connected.
267 * @retval ENOENT This device is not connected.
268 */
269int usbvirt_disconnect(usbvirt_device_t *dev)
270{
271 virtual_device_t *virtual_device = find_device(dev);
272 if (virtual_device == NULL) {
273 return ENOENT;
274 }
275
276 destroy_device(virtual_device);
277
278 return EOK;
279}
280
281
282/**
283 * @}
284 */
Note: See TracBrowser for help on using the repository browser.