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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0247bd2 was 4e17d54, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbdev: pipe operations with dma buffers

  • Property mode set to 100644
File size: 11.4 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/dma_buffer.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->desc.endpoint_no != 0)) {
54 return;
55 }
56
57 /* Prevent infinite recursion. */
58 pipe->auto_reset_halt = false;
59 usb_pipe_clear_halt(pipe, pipe);
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_transferred_size Number of bytes that were actually
73 * transferred during the DATA stage.
74 * @return Error code.
75 */
76errno_t 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 *transferred_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->desc.direction != USB_DIRECTION_BOTH)
91 || (pipe->desc.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 errno_t rc = usbhc_read(exch, pipe->desc.endpoint_no, setup_packet, buffer,
101 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 && transferred_size != NULL) {
109 *transferred_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 */
126errno_t 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->desc.direction != USB_DIRECTION_BOTH)
145 || (pipe->desc.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 errno_t rc = usbhc_write(exch,
154 pipe->desc.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/**
165 * Allocate a buffer for data transmission, that satisfies the constraints
166 * imposed by the host controller.
167 *
168 * @param[in] pipe Pipe for which the buffer is allocated
169 * @param[in] size Size of the required buffer
170 */
171void *usb_pipe_alloc_buffer(usb_pipe_t *pipe, size_t size)
172{
173 // FIXME: Do not use the default policy, but the one required by HC.
174
175 dma_buffer_t buf;
176 if (dma_buffer_alloc(&buf, size))
177 return NULL;
178
179 return buf.virt;
180}
181
182void usb_pipe_free_buffer(usb_pipe_t *pipe, void *buffer)
183{
184 dma_buffer_t buf;
185 buf.virt = buffer;
186 dma_buffer_free(&buf);
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_transferred Number of bytes that were actually transferred.
195 * @return Error code.
196 */
197errno_t usb_pipe_read(usb_pipe_t *pipe,
198 void *buffer, size_t size, size_t *size_transferred)
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->desc.direction != USB_DIRECTION_IN) {
211 return EBADF;
212 }
213
214 if (pipe->desc.transfer_type == USB_TRANSFER_CONTROL) {
215 return EBADF;
216 }
217
218 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
219 size_t act_size = 0;
220 const errno_t rc =
221 usbhc_read(exch, pipe->desc.endpoint_no, 0, buffer, size, &act_size);
222 async_exchange_end(exch);
223
224 if (rc == EOK && size_transferred != NULL) {
225 *size_transferred = act_size;
226 }
227
228 return rc;
229}
230
231/** Request a write (out) transfer on an endpoint pipe.
232 *
233 * @param[in] pipe Pipe used for the transfer.
234 * @param[in] buffer Buffer with data to transfer.
235 * @param[in] size Size of the buffer (in bytes).
236 * @return Error code.
237 */
238errno_t usb_pipe_write(usb_pipe_t *pipe, const void *buffer, size_t size)
239{
240 assert(pipe);
241
242 if (buffer == NULL || size == 0) {
243 return EINVAL;
244 }
245
246 if (pipe->desc.direction != USB_DIRECTION_OUT) {
247 return EBADF;
248 }
249
250 if (pipe->desc.transfer_type == USB_TRANSFER_CONTROL) {
251 return EBADF;
252 }
253
254 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
255 const errno_t rc = usbhc_write(exch, pipe->desc.endpoint_no, 0, buffer, size);
256 async_exchange_end(exch);
257 return rc;
258}
259
260/**
261 * Request a read (in) transfer on an endpoint pipe, declaring that buffer
262 * is pointing to a memory area previously allocated by usb_pipe_alloc_buffer.
263 *
264 * @param[in] pipe Pipe used for the transfer.
265 * @param[in] buffer Buffer, previously allocated with usb_pipe_alloc_buffer.
266 * @param[in] size Size of the buffer (in bytes).
267 * @param[out] size_transferred Number of bytes that were actually transferred.
268 * @return Error code.
269 */
270errno_t usb_pipe_read_dma(usb_pipe_t *pipe, void *buffer, size_t size,
271 size_t *size_transferred)
272{
273 assert(pipe);
274
275 if (buffer == NULL || size == 0)
276 return EINVAL;
277
278 if (pipe->desc.direction != USB_DIRECTION_IN
279 || pipe->desc.transfer_type == USB_TRANSFER_CONTROL)
280 return EBADF;
281
282 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
283 if (!exch)
284 return ENOMEM;
285
286 const errno_t rc = usbhc_transfer(exch, pipe->desc.endpoint_no,
287 USB_DIRECTION_IN, 0, buffer, size, size_transferred);
288 async_exchange_end(exch);
289 return rc;
290}
291
292/**
293 * Request a write (out) transfer on an endpoint pipe, declaring that buffer
294 * is pointing to a memory area previously allocated by usb_pipe_alloc_buffer.
295 *
296 * @param[in] pipe Pipe used for the transfer.
297 * @param[in] buffer Buffer, previously allocated with usb_pipe_alloc_buffer.
298 * @param[in] size Size of the buffer (in bytes).
299 * @return Error code.
300 */
301errno_t usb_pipe_write_dma(usb_pipe_t *pipe, void *buffer, size_t size)
302{
303 assert(pipe);
304
305 if (buffer == NULL || size == 0) {
306 return EINVAL;
307 }
308
309 if (pipe->desc.direction != USB_DIRECTION_OUT
310 || pipe->desc.transfer_type == USB_TRANSFER_CONTROL)
311 return EBADF;
312
313 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
314 if (!exch)
315 return ENOMEM;
316
317 const errno_t rc = usbhc_transfer(exch, pipe->desc.endpoint_no, USB_DIRECTION_OUT, 0, buffer, size, NULL);
318 async_exchange_end(exch);
319 return rc;
320}
321
322/** Initialize USB endpoint pipe.
323 *
324 * @param pipe Endpoint pipe to be initialized.
325 * @param bus_session Endpoint pipe to be initialized.
326 * @return Error code.
327 */
328errno_t usb_pipe_initialize(usb_pipe_t *pipe, usb_dev_session_t *bus_session)
329{
330 assert(pipe);
331
332 pipe->auto_reset_halt = false;
333 pipe->bus_session = bus_session;
334
335 return EOK;
336}
337
338static const usb_pipe_desc_t default_control_pipe = {
339 .endpoint_no = 0,
340 .transfer_type = USB_TRANSFER_CONTROL,
341 .direction = USB_DIRECTION_BOTH,
342 .max_transfer_size = CTRL_PIPE_MIN_PACKET_SIZE,
343};
344
345/** Initialize USB default control pipe.
346 *
347 * This one is special because it must not be registered, it is registered automatically.
348 *
349 * @param pipe Endpoint pipe to be initialized.
350 * @param bus_session Endpoint pipe to be initialized.
351 * @return Error code.
352 */
353errno_t usb_pipe_initialize_default_control(usb_pipe_t *pipe, usb_dev_session_t *bus_session)
354{
355 const errno_t ret = usb_pipe_initialize(pipe, bus_session);
356 if (ret)
357 return ret;
358
359 pipe->desc = default_control_pipe;
360 pipe->auto_reset_halt = true;
361
362 return EOK;
363}
364
365/** Register endpoint with the host controller.
366 *
367 * @param pipe Pipe to be registered.
368 * @param ep_desc Matched endpoint descriptor
369 * @param comp_desc Matched superspeed companion descriptro, if any
370 * @return Error code.
371 */
372errno_t usb_pipe_register(usb_pipe_t *pipe, const usb_standard_endpoint_descriptor_t *ep_desc, const usb_superspeed_endpoint_companion_descriptor_t *comp_desc)
373{
374 assert(pipe);
375 assert(pipe->bus_session);
376 assert(ep_desc);
377
378 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
379 if (!exch)
380 return ENOMEM;
381
382 usb_endpoint_descriptors_t descriptors;
383
384#define COPY(field) descriptors.endpoint.field = ep_desc->field
385 COPY(endpoint_address);
386 COPY(attributes);
387 COPY(max_packet_size);
388 COPY(poll_interval);
389#undef COPY
390
391#define COPY(field) descriptors.companion.field = comp_desc->field
392 if (comp_desc) {
393 COPY(max_burst);
394 COPY(attributes);
395 COPY(bytes_per_interval);
396 }
397#undef COPY
398
399 const errno_t ret = usbhc_register_endpoint(exch, &pipe->desc, &descriptors);
400 async_exchange_end(exch);
401 return ret;
402}
403
404/** Revert endpoint registration with the host controller.
405 *
406 * @param pipe Pipe to be unregistered.
407 * @return Error code.
408 */
409errno_t usb_pipe_unregister(usb_pipe_t *pipe)
410{
411 assert(pipe);
412 assert(pipe->bus_session);
413 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
414 if (!exch)
415 return ENOMEM;
416
417 const errno_t ret = usbhc_unregister_endpoint(exch, &pipe->desc);
418
419 async_exchange_end(exch);
420 return ret;
421}
422
423/**
424 * @}
425 */
Note: See TracBrowser for help on using the repository browser.