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

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

libusbdev: Switch pipse to use new data communication paths.

  • Property mode set to 100644
File size: 10.0 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 <errno.h>
38#include <assert.h>
39
40/** Prepare pipe for a long transfer.
41 *
42 * Long transfer is transfer consisting of several requests to the HC.
43 * Calling this function is optional and it has positive effect of
44 * improved performance because IPC session is initiated only once.
45 *
46 * @param pipe Pipe over which the transfer will happen.
47 * @return Error code.
48 */
49int usb_pipe_start_long_transfer(usb_pipe_t *pipe)
50{
51 assert(pipe);
52 assert(pipe->wire);
53 assert(pipe->wire->hc_connection);
54 return usb_hc_connection_open(pipe->wire->hc_connection);
55}
56
57/** Terminate a long transfer on a pipe.
58 * @param pipe Pipe where to end the long transfer.
59 * @return Error code.
60 * @see usb_pipe_start_long_transfer
61 */
62int usb_pipe_end_long_transfer(usb_pipe_t *pipe)
63{
64 assert(pipe);
65 assert(pipe->wire);
66 assert(pipe->wire->hc_connection);
67 return usb_hc_connection_close(pipe->wire->hc_connection);
68}
69
70/** Try to clear endpoint halt of default control pipe.
71 *
72 * @param pipe Pipe for control endpoint zero.
73 */
74static void clear_self_endpoint_halt(usb_pipe_t *pipe)
75{
76 assert(pipe != NULL);
77
78 if (!pipe->auto_reset_halt || (pipe->endpoint_no != 0)) {
79 return;
80 }
81
82 /* Prevent infinite recursion. */
83 pipe->auto_reset_halt = false;
84 usb_request_clear_endpoint_halt(pipe, 0);
85 pipe->auto_reset_halt = true;
86}
87
88/** Request a control read transfer on an endpoint pipe.
89 *
90 * This function encapsulates all three stages of a control transfer.
91 *
92 * @param[in] pipe Pipe used for the transfer.
93 * @param[in] setup_buffer Buffer with the setup packet.
94 * @param[in] setup_buffer_size Size of the setup packet (in bytes).
95 * @param[out] data_buffer Buffer for incoming data.
96 * @param[in] data_buffer_size Size of the buffer for incoming data (in bytes).
97 * @param[out] data_transfered_size Number of bytes that were actually
98 * transfered during the DATA stage.
99 * @return Error code.
100 */
101int usb_pipe_control_read(usb_pipe_t *pipe,
102 const void *setup_buffer, size_t setup_buffer_size,
103 void *buffer, size_t buffer_size, size_t *transfered_size)
104{
105 assert(pipe);
106
107 if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
108 return EINVAL;
109 }
110
111 if ((buffer == NULL) || (buffer_size == 0)) {
112 return EINVAL;
113 }
114
115 if ((pipe->direction != USB_DIRECTION_BOTH)
116 || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
117 return EBADF;
118 }
119
120 uint64_t setup_packet;
121 memcpy(&setup_packet, setup_buffer, 8);
122
123 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
124 size_t act_size = 0;
125 const int rc = usb_read(exch,
126 pipe->endpoint_no, setup_packet, buffer, buffer_size, &act_size);
127 async_exchange_end(exch);
128
129 if (rc == ESTALL) {
130 clear_self_endpoint_halt(pipe);
131 }
132
133 if (rc == EOK && transfered_size != NULL) {
134 *transfered_size = act_size;
135 }
136
137 return rc;
138}
139
140/** Request a control write transfer on an endpoint pipe.
141 *
142 * This function encapsulates all three stages of a control transfer.
143 *
144 * @param[in] pipe Pipe used for the transfer.
145 * @param[in] setup_buffer Buffer with the setup packet.
146 * @param[in] setup_buffer_size Size of the setup packet (in bytes).
147 * @param[in] data_buffer Buffer with data to be sent.
148 * @param[in] data_buffer_size Size of the buffer with outgoing data (in bytes).
149 * @return Error code.
150 */
151int usb_pipe_control_write(usb_pipe_t *pipe,
152 const void *setup_buffer, size_t setup_buffer_size,
153 const void *buffer, size_t buffer_size)
154{
155 assert(pipe);
156
157 if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
158 return EINVAL;
159 }
160
161 if ((buffer == NULL) && (buffer_size > 0)) {
162 return EINVAL;
163 }
164
165 if ((buffer != NULL) && (buffer_size == 0)) {
166 return EINVAL;
167 }
168
169 if ((pipe->direction != USB_DIRECTION_BOTH)
170 || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
171 return EBADF;
172 }
173
174 uint64_t setup_packet;
175 memcpy(&setup_packet, setup_buffer, 8);
176
177 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
178 const int rc = usb_write(exch,
179 pipe->endpoint_no, setup_packet, buffer, buffer_size);
180 async_exchange_end(exch);
181
182 if (rc == ESTALL) {
183 clear_self_endpoint_halt(pipe);
184 }
185
186 return rc;
187}
188
189/** Request a read (in) transfer on an endpoint pipe.
190 *
191 * @param[in] pipe Pipe used for the transfer.
192 * @param[out] buffer Buffer where to store the data.
193 * @param[in] size Size of the buffer (in bytes).
194 * @param[out] size_transfered Number of bytes that were actually transfered.
195 * @return Error code.
196 */
197int usb_pipe_read(usb_pipe_t *pipe,
198 void *buffer, size_t size, size_t *size_transfered)
199{
200 assert(pipe);
201
202 if (buffer == NULL) {
203 return EINVAL;
204 }
205
206 if (size == 0) {
207 return EINVAL;
208 }
209
210 if (pipe->direction != USB_DIRECTION_IN) {
211 return EBADF;
212 }
213
214 if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
215 return EBADF;
216 }
217
218 /* Isochronous transfer are not supported (yet) */
219 if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
220 pipe->transfer_type != USB_TRANSFER_BULK)
221 return ENOTSUP;
222
223 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
224 size_t act_size = 0;
225 const int rc =
226 usb_read(exch, pipe->endpoint_no, 0, buffer, size, &act_size);
227 async_exchange_end(exch);
228
229 if (rc == EOK && size_transfered != NULL) {
230 *size_transfered = act_size;
231 }
232
233 return rc;
234}
235
236/** Request a write (out) transfer on an endpoint pipe.
237 *
238 * @param[in] pipe Pipe used for the transfer.
239 * @param[in] buffer Buffer with data to transfer.
240 * @param[in] size Size of the buffer (in bytes).
241 * @return Error code.
242 */
243int usb_pipe_write(usb_pipe_t *pipe, const void *buffer, size_t size)
244{
245 assert(pipe);
246
247 if (buffer == NULL || size == 0) {
248 return EINVAL;
249 }
250
251 if (pipe->direction != USB_DIRECTION_OUT) {
252 return EBADF;
253 }
254
255 if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
256 return EBADF;
257 }
258
259 /* Isochronous transfer are not supported (yet) */
260 if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
261 pipe->transfer_type != USB_TRANSFER_BULK)
262 return ENOTSUP;
263
264 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
265 const int rc = usb_write(exch, pipe->endpoint_no, 0, buffer, size);
266 async_exchange_end(exch);
267 return rc;
268}
269
270/** Initialize USB endpoint pipe.
271 *
272 * @param pipe Endpoint pipe to be initialized.
273 * @param connection Connection to the USB device backing this pipe (the wire).
274 * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
275 * @param transfer_type Transfer type (e.g. interrupt or bulk).
276 * @param max_packet_size Maximum packet size in bytes.
277 * @param direction Endpoint direction (in/out).
278 * @return Error code.
279 */
280int usb_pipe_initialize(usb_pipe_t *pipe,
281 usb_device_connection_t *connection, usb_endpoint_t endpoint_no,
282 usb_transfer_type_t transfer_type, size_t max_packet_size,
283 usb_direction_t direction, usb_dev_session_t *bus_session)
284{
285 assert(pipe);
286 assert(connection);
287
288 pipe->wire = connection;
289 pipe->endpoint_no = endpoint_no;
290 pipe->transfer_type = transfer_type;
291 pipe->max_packet_size = max_packet_size;
292 pipe->direction = direction;
293 pipe->auto_reset_halt = false;
294 pipe->bus_session = bus_session;
295
296 return EOK;
297}
298
299/** Initialize USB endpoint pipe as the default zero control pipe.
300 *
301 * @param pipe Endpoint pipe to be initialized.
302 * @param connection Connection to the USB device backing this pipe (the wire).
303 * @return Error code.
304 */
305int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
306 usb_device_connection_t *connection, usb_dev_session_t *bus_session)
307{
308 assert(pipe);
309 assert(connection);
310
311 int rc = usb_pipe_initialize(pipe, connection, 0, USB_TRANSFER_CONTROL,
312 CTRL_PIPE_MIN_PACKET_SIZE, USB_DIRECTION_BOTH, bus_session);
313
314 pipe->auto_reset_halt = true;
315
316 return rc;
317}
318
319/** Register endpoint with the host controller.
320 *
321 * @param pipe Pipe to be registered.
322 * @param interval Polling interval.
323 * @return Error code.
324 */
325int usb_pipe_register(usb_pipe_t *pipe, unsigned interval)
326{
327 assert(pipe);
328 assert(pipe->bus_session);
329 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
330 if (!exch)
331 return ENOMEM;
332 const int ret = usb_register_endpoint(exch, pipe->endpoint_no,
333 pipe->transfer_type, pipe->direction, pipe->max_packet_size,
334 interval);
335 async_exchange_end(exch);
336 return ret;
337}
338
339/** Revert endpoint registration with the host controller.
340 *
341 * @param pipe Pipe to be unregistered.
342 * @return Error code.
343 */
344int usb_pipe_unregister(usb_pipe_t *pipe)
345{
346 assert(pipe);
347 assert(pipe->bus_session);
348 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
349 if (!exch)
350 return ENOMEM;
351 const int ret = usb_unregister_endpoint(exch, pipe->endpoint_no,
352 pipe->direction);
353 async_exchange_end(exch);
354 return ret;
355}
356
357/**
358 * @}
359 */
Note: See TracBrowser for help on using the repository browser.