1 | /*
|
---|
2 | * Copyright (c) 2011 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 | * Transfer handling.
|
---|
34 | */
|
---|
35 | #include <usbvirt/device.h>
|
---|
36 | #include <usb/debug.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <assert.h>
|
---|
39 | #include "private.h"
|
---|
40 |
|
---|
41 | /** Process a control transfer to the virtual USB device.
|
---|
42 | *
|
---|
43 | * @param dev Target device.
|
---|
44 | * @param setup Setup packet data.
|
---|
45 | * @param setup_size Size of setup packet.
|
---|
46 | * @param data Extra data (DATA stage).
|
---|
47 | * @param data_size Size of extra data in bytes.
|
---|
48 | * @param data_size_sent Number of actually send bytes during the transfer
|
---|
49 | * (only used for READ transfers).
|
---|
50 | * @return Error code.
|
---|
51 | */
|
---|
52 | static int usbvirt_control_transfer(usbvirt_device_t *dev,
|
---|
53 | void *setup, size_t setup_size,
|
---|
54 | void *data, size_t data_size, size_t *data_size_sent)
|
---|
55 | {
|
---|
56 | assert(dev);
|
---|
57 | assert(dev->ops);
|
---|
58 |
|
---|
59 | if (setup_size != sizeof(usb_device_request_setup_packet_t)) {
|
---|
60 | return ESTALL;
|
---|
61 | }
|
---|
62 | usb_device_request_setup_packet_t *setup_packet = setup;
|
---|
63 | if (data_size != setup_packet->length) {
|
---|
64 | return ESTALL;
|
---|
65 | }
|
---|
66 |
|
---|
67 | int rc;
|
---|
68 |
|
---|
69 | /* Run user handler first. */
|
---|
70 | rc = process_control_transfer(dev, dev->ops->control,
|
---|
71 | setup_packet, data, data_size_sent);
|
---|
72 | if (rc != EFORWARD) {
|
---|
73 | return rc;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /* Run the library handlers afterwards. */
|
---|
77 | rc = process_control_transfer(dev, library_handlers,
|
---|
78 | setup_packet, data, data_size_sent);
|
---|
79 |
|
---|
80 | if (rc == EFORWARD) {
|
---|
81 | usb_log_warning("Control transfer {%s (%s)} not handled.\n",
|
---|
82 | usb_debug_str_buffer(setup, setup_size, 10),
|
---|
83 | setup_packet->request_type & 0x80
|
---|
84 | ? "IN" : usb_debug_str_buffer(data, data_size, 10));
|
---|
85 | rc = EBADCHECKSUM;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Issue a control write transfer to virtual USB device.
|
---|
92 | *
|
---|
93 | * @see usbvirt_control_transfer
|
---|
94 | *
|
---|
95 | * @param dev Target virtual device.
|
---|
96 | * @param setup Setup data.
|
---|
97 | * @param setup_size Size of setup packet.
|
---|
98 | * @param data Extra data (DATA stage).
|
---|
99 | * @param data_size Size of extra data buffer in bytes.
|
---|
100 | * @return Error code.
|
---|
101 | */
|
---|
102 | int usbvirt_control_write(usbvirt_device_t *dev, void *setup, size_t setup_size,
|
---|
103 | void *data, size_t data_size)
|
---|
104 | {
|
---|
105 | return usbvirt_control_transfer(dev, setup, setup_size,
|
---|
106 | data, data_size, NULL);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Issue a control read transfer to virtual USB device.
|
---|
110 | *
|
---|
111 | * @see usbvirt_control_transfer
|
---|
112 | *
|
---|
113 | * @param dev Target virtual device.
|
---|
114 | * @param setup Setup data.
|
---|
115 | * @param setup_size Size of setup packet.
|
---|
116 | * @param data Extra data (DATA stage).
|
---|
117 | * @param data_size Size of extra data buffer in bytes.
|
---|
118 | * @param data_size_sent Number of actually send bytes during the transfer.
|
---|
119 | * @return Error code.
|
---|
120 | */
|
---|
121 | int usbvirt_control_read(usbvirt_device_t *dev, void *setup, size_t setup_size,
|
---|
122 | void *data, size_t data_size, size_t *data_size_sent)
|
---|
123 | {
|
---|
124 | return usbvirt_control_transfer(dev, setup, setup_size,
|
---|
125 | data, data_size, data_size_sent);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Send data to virtual USB device.
|
---|
129 | *
|
---|
130 | * @param dev Target virtual device.
|
---|
131 | * @param transf_type Transfer type (interrupt, bulk).
|
---|
132 | * @param endpoint Endpoint number.
|
---|
133 | * @param data Data sent from the driver to the device.
|
---|
134 | * @param data_size Size of the @p data buffer in bytes.
|
---|
135 | * @return Error code.
|
---|
136 | */
|
---|
137 | int usbvirt_data_out(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
|
---|
138 | usb_endpoint_t endpoint, void *data, size_t data_size)
|
---|
139 | {
|
---|
140 | if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
|
---|
141 | return ERANGE;
|
---|
142 | }
|
---|
143 | if ((dev->ops == NULL) || (dev->ops->data_out[endpoint] == NULL)) {
|
---|
144 | return ENOTSUP;
|
---|
145 | }
|
---|
146 |
|
---|
147 | int rc = dev->ops->data_out[endpoint](dev, endpoint, transf_type,
|
---|
148 | data, data_size);
|
---|
149 |
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /** Request data from virtual USB device.
|
---|
154 | *
|
---|
155 | * @param dev Target virtual device.
|
---|
156 | * @param transf_type Transfer type (interrupt, bulk).
|
---|
157 | * @param endpoint Endpoint number.
|
---|
158 | * @param data Where to stored data the device returns to the driver.
|
---|
159 | * @param data_size Size of the @p data buffer in bytes.
|
---|
160 | * @param data_size_sent Number of actually written bytes.
|
---|
161 | * @return Error code.
|
---|
162 | */
|
---|
163 | int usbvirt_data_in(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
|
---|
164 | usb_endpoint_t endpoint, void *data, size_t data_size, size_t *data_size_sent)
|
---|
165 | {
|
---|
166 | if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
|
---|
167 | return ERANGE;
|
---|
168 | }
|
---|
169 | if ((dev->ops == NULL) || (dev->ops->data_in[endpoint] == NULL)) {
|
---|
170 | return ENOTSUP;
|
---|
171 | }
|
---|
172 |
|
---|
173 | size_t data_size_sent_tmp;
|
---|
174 | int rc = dev->ops->data_in[endpoint](dev, endpoint, transf_type,
|
---|
175 | data, data_size, &data_size_sent_tmp);
|
---|
176 |
|
---|
177 | if (rc != EOK) {
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (data_size_sent != NULL) {
|
---|
182 | *data_size_sent = data_size_sent_tmp;
|
---|
183 | }
|
---|
184 |
|
---|
185 | return EOK;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * @}
|
---|
190 | */
|
---|