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

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

Doxygen comment fixes

Mainly proper subgrouping and some missing @param comments.

  • Property mode set to 100644
File size: 5.6 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 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 "hub/virthub.h"
50#include "vhcd.h"
51
52#define list_foreach(pos, head) \
53 for (pos = (head)->next; pos != (head); \
54 pos = pos->next)
55
56static LIST_INITIALIZE(devices);
57
58/** Create virtual device.
59 *
60 * @param phone Callback phone.
61 * @return New device.
62 * @retval NULL Out of memory.
63 */
64virtdev_connection_t *virtdev_add_device(int phone)
65{
66 virtdev_connection_t *dev = (virtdev_connection_t *)
67 malloc(sizeof(virtdev_connection_t));
68 if (dev == NULL) {
69 return NULL;
70 }
71
72 dev->phone = phone;
73 list_append(&dev->link, &devices);
74
75 virthub_connect_device(&virtual_hub_device, dev);
76
77 return dev;
78}
79
80/** Destroy virtual device.
81 */
82void virtdev_destroy_device(virtdev_connection_t *dev)
83{
84 virthub_disconnect_device(&virtual_hub_device, dev);
85 list_remove(&dev->link);
86 free(dev);
87}
88
89/** Send data to all connected devices.
90 *
91 * @param transaction Transaction to be sent over the bus.
92 */
93usb_transaction_outcome_t virtdev_send_to_all(transaction_t *transaction)
94{
95 /* For easier debugging. */
96 switch (transaction->type) {
97 case USBVIRT_TRANSACTION_SETUP:
98 case USBVIRT_TRANSACTION_OUT:
99 transaction->actual_len = transaction->len;
100 break;
101 case USBVIRT_TRANSACTION_IN:
102 transaction->actual_len = 0;
103 break;
104 default:
105 assert(false && "unreachable branch in switch()");
106 }
107 usb_transaction_outcome_t outcome = USB_OUTCOME_BABBLE;
108
109 link_t *pos;
110 list_foreach(pos, &devices) {
111 virtdev_connection_t *dev
112 = list_get_instance(pos, virtdev_connection_t, link);
113
114 if (!virthub_is_device_enabled(&virtual_hub_device, dev)) {
115 continue;
116 }
117
118 ipc_call_t answer_data;
119 sysarg_t answer_rc;
120 aid_t req;
121 int rc = EOK;
122 int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
123
124 switch (transaction->type) {
125 case USBVIRT_TRANSACTION_SETUP:
126 method = IPC_M_USBVIRT_TRANSACTION_SETUP;
127 break;
128 case USBVIRT_TRANSACTION_IN:
129 method = IPC_M_USBVIRT_TRANSACTION_IN;
130 break;
131 case USBVIRT_TRANSACTION_OUT:
132 method = IPC_M_USBVIRT_TRANSACTION_OUT;
133 break;
134 }
135
136 req = async_send_3(dev->phone,
137 method,
138 transaction->target.address,
139 transaction->target.endpoint,
140 transaction->len,
141 &answer_data);
142
143 if (transaction->len > 0) {
144 if (transaction->type == USBVIRT_TRANSACTION_IN) {
145 rc = async_data_read_start(dev->phone,
146 transaction->buffer, transaction->len);
147 } else {
148 rc = async_data_write_start(dev->phone,
149 transaction->buffer, transaction->len);
150 }
151 }
152
153 if (rc != EOK) {
154 async_wait_for(req, NULL);
155 } else {
156 async_wait_for(req, &answer_rc);
157 transaction->actual_len = IPC_GET_ARG1(answer_data);
158 rc = (int)answer_rc;
159 }
160
161 /*
162 * If at least one device was able to accept this
163 * transaction and process it, we can announce success.
164 */
165 if (rc == EOK) {
166 outcome = USB_OUTCOME_OK;
167 }
168 }
169
170 /*
171 * Send the data to the virtual hub as well
172 * (if the address matches).
173 */
174 if (virtual_hub_device.address == transaction->target.address) {
175 size_t tmp;
176 dprintf(1, "sending `%s' transaction to hub",
177 usbvirt_str_transaction_type(transaction->type));
178 switch (transaction->type) {
179 case USBVIRT_TRANSACTION_SETUP:
180 virtual_hub_device.transaction_setup(
181 &virtual_hub_device,
182 transaction->target.endpoint,
183 transaction->buffer, transaction->len);
184 break;
185
186 case USBVIRT_TRANSACTION_IN:
187 virtual_hub_device.transaction_in(
188 &virtual_hub_device,
189 transaction->target.endpoint,
190 transaction->buffer, transaction->len,
191 &tmp);
192 transaction->actual_len = tmp;
193 break;
194
195 case USBVIRT_TRANSACTION_OUT:
196 virtual_hub_device.transaction_out(
197 &virtual_hub_device,
198 transaction->target.endpoint,
199 transaction->buffer, transaction->len);
200 break;
201 }
202 dprintf(4, "transaction on hub processed...");
203 outcome = USB_OUTCOME_OK;
204 }
205
206 /*
207 * TODO: maybe screw some transactions to get more
208 * real-life image.
209 */
210 return outcome;
211}
212
213/**
214 * @}
215 */
Note: See TracBrowser for help on using the repository browser.