source: mainline/uspace/lib/usbvirt/src/transfer.c

Last change on this file was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 5.9 KB
Line 
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 */
52static errno_t usbvirt_control_transfer(usbvirt_device_t *dev,
53 const 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 const usb_device_request_setup_packet_t *setup_packet = setup;
63 if (data_size != setup_packet->length) {
64 return ESTALL;
65 }
66
67 errno_t 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.",
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 */
102errno_t usbvirt_control_write(usbvirt_device_t *dev, const void *setup,
103 size_t setup_size, 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 */
121errno_t usbvirt_control_read(usbvirt_device_t *dev,
122 const void *setup, size_t setup_size,
123 void *data, size_t data_size, size_t *data_size_sent)
124{
125 return usbvirt_control_transfer(dev, setup, setup_size,
126 data, data_size, data_size_sent);
127}
128
129/** Send data to virtual USB device.
130 *
131 * @param dev Target virtual device.
132 * @param transf_type Transfer type (interrupt, bulk).
133 * @param endpoint Endpoint number.
134 * @param data Data sent from the driver to the device.
135 * @param data_size Size of the @p data buffer in bytes.
136 * @return Error code.
137 */
138errno_t usbvirt_data_out(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
139 usb_endpoint_t endpoint, const void *data, size_t data_size)
140{
141 if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
142 return ERANGE;
143 }
144 if ((dev->ops == NULL) || (dev->ops->data_out[endpoint] == NULL)) {
145 return ENOTSUP;
146 }
147
148 errno_t rc = dev->ops->data_out[endpoint](dev, endpoint, transf_type,
149 data, data_size);
150
151 return rc;
152}
153
154/** Request data from virtual USB device.
155 *
156 * @param dev Target virtual device.
157 * @param transf_type Transfer type (interrupt, bulk).
158 * @param endpoint Endpoint number.
159 * @param data Where to stored data the device returns to the driver.
160 * @param data_size Size of the @p data buffer in bytes.
161 * @param data_size_sent Number of actually written bytes.
162 * @return Error code.
163 */
164errno_t usbvirt_data_in(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
165 usb_endpoint_t endpoint, void *data, size_t data_size, size_t *data_size_sent)
166{
167 if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
168 return ERANGE;
169 }
170 if ((dev->ops == NULL) || (dev->ops->data_in[endpoint] == NULL)) {
171 return ENOTSUP;
172 }
173
174 size_t data_size_sent_tmp;
175 errno_t rc = dev->ops->data_in[endpoint](dev, endpoint, transf_type,
176 data, data_size, &data_size_sent_tmp);
177
178 if (rc != EOK) {
179 return rc;
180 }
181
182 if (data_size_sent != NULL) {
183 *data_size_sent = data_size_sent_tmp;
184 }
185
186 return EOK;
187}
188
189/**
190 * @}
191 */
Note: See TracBrowser for help on using the repository browser.