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/ids.h>
|
---|
46 | #include <usbvirt/hub.h>
|
---|
47 |
|
---|
48 | #include "devices.h"
|
---|
49 | #include "hub.h"
|
---|
50 |
|
---|
51 | #define list_foreach(pos, head) \
|
---|
52 | for (pos = (head)->next; pos != (head); \
|
---|
53 | pos = pos->next)
|
---|
54 |
|
---|
55 | LIST_INITIALIZE(devices);
|
---|
56 |
|
---|
57 | /** Recognise device by id.
|
---|
58 | *
|
---|
59 | * @param id Device id.
|
---|
60 | * @param phone Callback phone.
|
---|
61 | */
|
---|
62 | virtdev_connection_t *virtdev_recognise(int id, int phone)
|
---|
63 | {
|
---|
64 | virtdev_connection_t * dev = virtdev_add_device(phone);
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * We do not want to mess-up the virtdev_add_device() as
|
---|
68 | * the id is needed only before device probing/detection
|
---|
69 | * is implemented.
|
---|
70 | *
|
---|
71 | * However, that does not mean that this will happen soon.
|
---|
72 | */
|
---|
73 | if (dev) {
|
---|
74 | dev->id = id;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return dev;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /** Create virtual device.
|
---|
81 | *
|
---|
82 | * @param address USB address.
|
---|
83 | * @param phone Callback phone.
|
---|
84 | * @return New device.
|
---|
85 | * @retval NULL Out of memory or address already occupied.
|
---|
86 | */
|
---|
87 | virtdev_connection_t *virtdev_add_device(int phone)
|
---|
88 | {
|
---|
89 | virtdev_connection_t *dev = (virtdev_connection_t *)
|
---|
90 | malloc(sizeof(virtdev_connection_t));
|
---|
91 | dev->phone = phone;
|
---|
92 | list_append(&dev->link, &devices);
|
---|
93 |
|
---|
94 | hub_add_device(dev);
|
---|
95 |
|
---|
96 | return dev;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Destroy virtual device.
|
---|
100 | */
|
---|
101 | void virtdev_destroy_device(virtdev_connection_t *dev)
|
---|
102 | {
|
---|
103 | hub_remove_device(dev);
|
---|
104 | list_remove(&dev->link);
|
---|
105 | free(dev);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Send data to all connected devices.
|
---|
109 | *
|
---|
110 | * @param transaction Transaction to be sent over the bus.
|
---|
111 | */
|
---|
112 | usb_transaction_outcome_t virtdev_send_to_all(transaction_t *transaction)
|
---|
113 | {
|
---|
114 | link_t *pos;
|
---|
115 | list_foreach(pos, &devices) {
|
---|
116 | virtdev_connection_t *dev
|
---|
117 | = list_get_instance(pos, virtdev_connection_t, link);
|
---|
118 |
|
---|
119 | if (!hub_can_device_signal(dev)) {
|
---|
120 | continue;
|
---|
121 | }
|
---|
122 |
|
---|
123 | ipc_call_t answer_data;
|
---|
124 | ipcarg_t answer_rc;
|
---|
125 | aid_t req;
|
---|
126 | int rc;
|
---|
127 |
|
---|
128 | req = async_send_3(dev->phone,
|
---|
129 | IPC_M_USBVIRT_DATA_TO_DEVICE,
|
---|
130 | transaction->target.address,
|
---|
131 | transaction->target.endpoint,
|
---|
132 | transaction->type,
|
---|
133 | &answer_data);
|
---|
134 |
|
---|
135 | rc = async_data_write_start(dev->phone,
|
---|
136 | transaction->buffer, transaction->len);
|
---|
137 | if (rc != EOK) {
|
---|
138 | async_wait_for(req, NULL);
|
---|
139 | } else {
|
---|
140 | async_wait_for(req, &answer_rc);
|
---|
141 | rc = (int)answer_rc;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Send the data to the virtual hub as well
|
---|
147 | * (if the address matches).
|
---|
148 | */
|
---|
149 | if (virthub_dev.address == transaction->target.address) {
|
---|
150 | virthub_dev.receive_data(&virthub_dev, transaction->target.endpoint,
|
---|
151 | transaction->buffer, transaction->len);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * TODO: maybe screw some transactions to get more
|
---|
156 | * real-life image.
|
---|
157 | */
|
---|
158 | return USB_OUTCOME_OK;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * @}
|
---|
163 | */
|
---|