source: mainline/uspace/lib/usbdev/src/pipes.c@ c01987c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c01987c was c01987c, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusbdev: Sanitize headers.

Include what you use.
https://code.google.com/p/include-what-you-use/

  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2011 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/** @addtogroup libusbdev
30 * @{
31 */
32/** @file
33 * USB endpoint pipes functions.
34 */
35#include <usb/dev/pipes.h>
36#include <usb/dev/request.h>
37#include <usb/usb.h>
38#include <usb_iface.h>
39
40#include <assert.h>
41#include <async.h>
42#include <errno.h>
43#include <mem.h>
44
45/** Try to clear endpoint halt of default control pipe.
46 *
47 * @param pipe Pipe for control endpoint zero.
48 */
49static void clear_self_endpoint_halt(usb_pipe_t *pipe)
50{
51 assert(pipe != NULL);
52
53 if (!pipe->auto_reset_halt || (pipe->endpoint_no != 0)) {
54 return;
55 }
56
57 /* Prevent infinite recursion. */
58 pipe->auto_reset_halt = false;
59 usb_request_clear_endpoint_halt(pipe, 0);
60 pipe->auto_reset_halt = true;
61}
62
63/** Request a control read transfer on an endpoint pipe.
64 *
65 * This function encapsulates all three stages of a control transfer.
66 *
67 * @param[in] pipe Pipe used for the transfer.
68 * @param[in] setup_buffer Buffer with the setup packet.
69 * @param[in] setup_buffer_size Size of the setup packet (in bytes).
70 * @param[out] data_buffer Buffer for incoming data.
71 * @param[in] data_buffer_size Size of the buffer for incoming data (in bytes).
72 * @param[out] data_transfered_size Number of bytes that were actually
73 * transfered during the DATA stage.
74 * @return Error code.
75 */
76int usb_pipe_control_read(usb_pipe_t *pipe,
77 const void *setup_buffer, size_t setup_buffer_size,
78 void *buffer, size_t buffer_size, size_t *transfered_size)
79{
80 assert(pipe);
81
82 if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
83 return EINVAL;
84 }
85
86 if ((buffer == NULL) || (buffer_size == 0)) {
87 return EINVAL;
88 }
89
90 if ((pipe->direction != USB_DIRECTION_BOTH)
91 || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
92 return EBADF;
93 }
94
95 uint64_t setup_packet;
96 memcpy(&setup_packet, setup_buffer, 8);
97
98 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
99 size_t act_size = 0;
100 const int rc = usb_read(exch,
101 pipe->endpoint_no, setup_packet, buffer, buffer_size, &act_size);
102 async_exchange_end(exch);
103
104 if (rc == ESTALL) {
105 clear_self_endpoint_halt(pipe);
106 }
107
108 if (rc == EOK && transfered_size != NULL) {
109 *transfered_size = act_size;
110 }
111
112 return rc;
113}
114
115/** Request a control write transfer on an endpoint pipe.
116 *
117 * This function encapsulates all three stages of a control transfer.
118 *
119 * @param[in] pipe Pipe used for the transfer.
120 * @param[in] setup_buffer Buffer with the setup packet.
121 * @param[in] setup_buffer_size Size of the setup packet (in bytes).
122 * @param[in] data_buffer Buffer with data to be sent.
123 * @param[in] data_buffer_size Size of the buffer with outgoing data (in bytes).
124 * @return Error code.
125 */
126int usb_pipe_control_write(usb_pipe_t *pipe,
127 const void *setup_buffer, size_t setup_buffer_size,
128 const void *buffer, size_t buffer_size)
129{
130 assert(pipe);
131
132 if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
133 return EINVAL;
134 }
135
136 if ((buffer == NULL) && (buffer_size > 0)) {
137 return EINVAL;
138 }
139
140 if ((buffer != NULL) && (buffer_size == 0)) {
141 return EINVAL;
142 }
143
144 if ((pipe->direction != USB_DIRECTION_BOTH)
145 || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
146 return EBADF;
147 }
148
149 uint64_t setup_packet;
150 memcpy(&setup_packet, setup_buffer, 8);
151
152 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
153 const int rc = usb_write(exch,
154 pipe->endpoint_no, setup_packet, buffer, buffer_size);
155 async_exchange_end(exch);
156
157 if (rc == ESTALL) {
158 clear_self_endpoint_halt(pipe);
159 }
160
161 return rc;
162}
163
164/** Request a read (in) transfer on an endpoint pipe.
165 *
166 * @param[in] pipe Pipe used for the transfer.
167 * @param[out] buffer Buffer where to store the data.
168 * @param[in] size Size of the buffer (in bytes).
169 * @param[out] size_transfered Number of bytes that were actually transfered.
170 * @return Error code.
171 */
172int usb_pipe_read(usb_pipe_t *pipe,
173 void *buffer, size_t size, size_t *size_transfered)
174{
175 assert(pipe);
176
177 if (buffer == NULL) {
178 return EINVAL;
179 }
180
181 if (size == 0) {
182 return EINVAL;
183 }
184
185 if (pipe->direction != USB_DIRECTION_IN) {
186 return EBADF;
187 }
188
189 if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
190 return EBADF;
191 }
192
193 /* Isochronous transfer are not supported (yet) */
194 if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
195 pipe->transfer_type != USB_TRANSFER_BULK)
196 return ENOTSUP;
197
198 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
199 size_t act_size = 0;
200 const int rc =
201 usb_read(exch, pipe->endpoint_no, 0, buffer, size, &act_size);
202 async_exchange_end(exch);
203
204 if (rc == EOK && size_transfered != NULL) {
205 *size_transfered = act_size;
206 }
207
208 return rc;
209}
210
211/** Request a write (out) transfer on an endpoint pipe.
212 *
213 * @param[in] pipe Pipe used for the transfer.
214 * @param[in] buffer Buffer with data to transfer.
215 * @param[in] size Size of the buffer (in bytes).
216 * @return Error code.
217 */
218int usb_pipe_write(usb_pipe_t *pipe, const void *buffer, size_t size)
219{
220 assert(pipe);
221
222 if (buffer == NULL || size == 0) {
223 return EINVAL;
224 }
225
226 if (pipe->direction != USB_DIRECTION_OUT) {
227 return EBADF;
228 }
229
230 if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
231 return EBADF;
232 }
233
234 /* Isochronous transfer are not supported (yet) */
235 if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
236 pipe->transfer_type != USB_TRANSFER_BULK)
237 return ENOTSUP;
238
239 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
240 const int rc = usb_write(exch, pipe->endpoint_no, 0, buffer, size);
241 async_exchange_end(exch);
242 return rc;
243}
244
245/** Initialize USB endpoint pipe.
246 *
247 * @param pipe Endpoint pipe to be initialized.
248 * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
249 * @param transfer_type Transfer type (e.g. interrupt or bulk).
250 * @param max_packet_size Maximum packet size in bytes.
251 * @param direction Endpoint direction (in/out).
252 * @return Error code.
253 */
254int usb_pipe_initialize(usb_pipe_t *pipe, usb_endpoint_t endpoint_no,
255 usb_transfer_type_t transfer_type, size_t max_packet_size,
256 usb_direction_t direction, usb_dev_session_t *bus_session)
257{
258 assert(pipe);
259
260 pipe->endpoint_no = endpoint_no;
261 pipe->transfer_type = transfer_type;
262 pipe->max_packet_size = max_packet_size;
263 pipe->direction = direction;
264 pipe->auto_reset_halt = false;
265 pipe->bus_session = bus_session;
266
267 return EOK;
268}
269
270/** Initialize USB endpoint pipe as the default zero control pipe.
271 *
272 * @param pipe Endpoint pipe to be initialized.
273 * @return Error code.
274 */
275int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
276 usb_dev_session_t *bus_session)
277{
278 assert(pipe);
279
280 const int rc = usb_pipe_initialize(pipe, 0, USB_TRANSFER_CONTROL,
281 CTRL_PIPE_MIN_PACKET_SIZE, USB_DIRECTION_BOTH, bus_session);
282
283 pipe->auto_reset_halt = true;
284
285 return rc;
286}
287
288/** Register endpoint with the host controller.
289 *
290 * @param pipe Pipe to be registered.
291 * @param interval Polling interval.
292 * @return Error code.
293 */
294int usb_pipe_register(usb_pipe_t *pipe, unsigned interval)
295{
296 assert(pipe);
297 assert(pipe->bus_session);
298 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
299 if (!exch)
300 return ENOMEM;
301 const int ret = usb_register_endpoint(exch, pipe->endpoint_no,
302 pipe->transfer_type, pipe->direction, pipe->max_packet_size,
303 interval);
304 async_exchange_end(exch);
305 return ret;
306}
307
308/** Revert endpoint registration with the host controller.
309 *
310 * @param pipe Pipe to be unregistered.
311 * @return Error code.
312 */
313int usb_pipe_unregister(usb_pipe_t *pipe)
314{
315 assert(pipe);
316 assert(pipe->bus_session);
317 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
318 if (!exch)
319 return ENOMEM;
320 const int ret = usb_unregister_endpoint(exch, pipe->endpoint_no,
321 pipe->direction);
322 async_exchange_end(exch);
323 return ret;
324}
325
326/**
327 * @}
328 */
Note: See TracBrowser for help on using the repository browser.