source: mainline/uspace/drv/vhc/devices.c@ ace12560

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

Removal of usb_outcome_t (ticket 55)

  • Property mode set to 100644
File size: 5.9 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 drvusbvhc
[bc9a629]30 * @{
31 */
32/** @file
[b371844]33 * @brief Virtual device management (implementation).
[bc9a629]34 */
35
36#include <adt/list.h>
37#include <bool.h>
38#include <async.h>
39#include <stdlib.h>
40#include <stdio.h>
41#include <errno.h>
42#include <str_error.h>
43
[47e3a8e]44#include <usbvirt/hub.h>
[bc9a629]45
46#include "devices.h"
[b8507a1]47#include "hub.h"
[774afaae]48#include "hub/virthub.h"
[b8a3cda]49#include "vhcd.h"
[bc9a629]50
51#define list_foreach(pos, head) \
52 for (pos = (head)->next; pos != (head); \
53 pos = pos->next)
54
[63b4f90]55static LIST_INITIALIZE(devices);
[bc9a629]56
[b371844]57/** Create virtual device.
58 *
59 * @param phone Callback phone.
[6967c14]60 * @param id Device id.
[b371844]61 * @return New device.
[3b77628]62 * @retval NULL Out of memory.
[b371844]63 */
[6967c14]64virtdev_connection_t *virtdev_add_device(int phone, sysarg_t id)
[bc9a629]65{
66 virtdev_connection_t *dev = (virtdev_connection_t *)
67 malloc(sizeof(virtdev_connection_t));
[3b77628]68 if (dev == NULL) {
69 return NULL;
70 }
71
[bc9a629]72 dev->phone = phone;
[6967c14]73 dev->id = id;
[bc9a629]74 list_append(&dev->link, &devices);
75
[774afaae]76 virthub_connect_device(&virtual_hub_device, dev);
[b8507a1]77
[bc9a629]78 return dev;
79}
80
[6967c14]81/** Find virtual device by id.
82 *
83 * @param id Device id.
84 * @return Device with given id.
85 * @retval NULL No such device.
86 */
87virtdev_connection_t *virtdev_find(sysarg_t id)
88{
89 link_t *pos;
90 list_foreach(pos, &devices) {
91 virtdev_connection_t *dev
92 = list_get_instance(pos, virtdev_connection_t, link);
93 if (dev->id == id) {
94 return dev;
95 }
96 }
97
98 return NULL;
99}
100
[b371844]101/** Destroy virtual device.
102 */
103void virtdev_destroy_device(virtdev_connection_t *dev)
[bc9a629]104{
[774afaae]105 virthub_disconnect_device(&virtual_hub_device, dev);
[bc9a629]106 list_remove(&dev->link);
107 free(dev);
108}
109
[47e3a8e]110/** Send data to all connected devices.
111 *
112 * @param transaction Transaction to be sent over the bus.
113 */
[daec5e04]114int virtdev_send_to_all(transaction_t *transaction)
[47e3a8e]115{
[4f380d06]116 /* For easier debugging. */
117 switch (transaction->type) {
118 case USBVIRT_TRANSACTION_SETUP:
119 case USBVIRT_TRANSACTION_OUT:
120 transaction->actual_len = transaction->len;
121 break;
122 case USBVIRT_TRANSACTION_IN:
123 transaction->actual_len = 0;
124 break;
125 default:
126 assert(false && "unreachable branch in switch()");
127 }
[daec5e04]128 int outcome = EBADCHECKSUM;
[4f380d06]129
[47e3a8e]130 link_t *pos;
131 list_foreach(pos, &devices) {
132 virtdev_connection_t *dev
133 = list_get_instance(pos, virtdev_connection_t, link);
134
[774afaae]135 if (!virthub_is_device_enabled(&virtual_hub_device, dev)) {
[b8507a1]136 continue;
137 }
138
[47e3a8e]139 ipc_call_t answer_data;
[a9b6bec]140 sysarg_t answer_rc;
[47e3a8e]141 aid_t req;
[b8a3cda]142 int rc = EOK;
[7a7bfeb3]143 int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
[47e3a8e]144
[7a7bfeb3]145 switch (transaction->type) {
146 case USBVIRT_TRANSACTION_SETUP:
147 method = IPC_M_USBVIRT_TRANSACTION_SETUP;
148 break;
149 case USBVIRT_TRANSACTION_IN:
150 method = IPC_M_USBVIRT_TRANSACTION_IN;
151 break;
152 case USBVIRT_TRANSACTION_OUT:
153 method = IPC_M_USBVIRT_TRANSACTION_OUT;
154 break;
155 }
156
157 req = async_send_3(dev->phone,
158 method,
[47e3a8e]159 transaction->target.address,
160 transaction->target.endpoint,
[b8a3cda]161 transaction->len,
[47e3a8e]162 &answer_data);
163
[b8a3cda]164 if (transaction->len > 0) {
[7a7bfeb3]165 if (transaction->type == USBVIRT_TRANSACTION_IN) {
166 rc = async_data_read_start(dev->phone,
167 transaction->buffer, transaction->len);
168 } else {
169 rc = async_data_write_start(dev->phone,
170 transaction->buffer, transaction->len);
171 }
[b8a3cda]172 }
[7a7bfeb3]173
[47e3a8e]174 if (rc != EOK) {
175 async_wait_for(req, NULL);
176 } else {
177 async_wait_for(req, &answer_rc);
[13101d06]178 transaction->actual_len = IPC_GET_ARG1(answer_data);
[47e3a8e]179 rc = (int)answer_rc;
180 }
[4f380d06]181
182 /*
183 * If at least one device was able to accept this
184 * transaction and process it, we can announce success.
185 */
186 if (rc == EOK) {
[daec5e04]187 outcome = EOK;
[4f380d06]188 }
[47e3a8e]189 }
190
[b8507a1]191 /*
192 * Send the data to the virtual hub as well
193 * (if the address matches).
194 */
[774afaae]195 if (virtual_hub_device.address == transaction->target.address) {
[7a7bfeb3]196 size_t tmp;
[0b31409]197 usb_log_debug2("Sending `%s' transaction to hub.\n",
[2185776]198 usbvirt_str_transaction_type(transaction->type));
[7a7bfeb3]199 switch (transaction->type) {
200 case USBVIRT_TRANSACTION_SETUP:
[774afaae]201 virtual_hub_device.transaction_setup(
202 &virtual_hub_device,
[7a7bfeb3]203 transaction->target.endpoint,
204 transaction->buffer, transaction->len);
205 break;
206
207 case USBVIRT_TRANSACTION_IN:
[774afaae]208 virtual_hub_device.transaction_in(
209 &virtual_hub_device,
[7a7bfeb3]210 transaction->target.endpoint,
211 transaction->buffer, transaction->len,
212 &tmp);
[4a5e2f1]213 transaction->actual_len = tmp;
[7a7bfeb3]214 break;
215
216 case USBVIRT_TRANSACTION_OUT:
[774afaae]217 virtual_hub_device.transaction_out(
218 &virtual_hub_device,
[7a7bfeb3]219 transaction->target.endpoint,
220 transaction->buffer, transaction->len);
221 break;
222 }
[daec5e04]223 outcome = EOK;
[b8507a1]224 }
225
[47e3a8e]226 /*
227 * TODO: maybe screw some transactions to get more
228 * real-life image.
229 */
[4f380d06]230 return outcome;
[47e3a8e]231}
232
[bc9a629]233/**
234 * @}
235 */
Note: See TracBrowser for help on using the repository browser.