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

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

libusbdev: Cleanup unused code.

  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[6865243c]1/*
2 * Copyright (c) 2011 Vojtech Horky
[bd575647]3 * Copyright (c) 2011 Jan Vesely
[6865243c]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 */
[160b75e]29/** @addtogroup libusbdev
[6865243c]30 * @{
31 */
32/** @file
[3538b0e]33 * USB endpoint pipes functions.
[6865243c]34 */
[bd575647]35#include <usb/dev/pipes.h>
[023a902]36#include <usb/dev/request.h>
[6865243c]37#include <errno.h>
[43f698b]38#include <assert.h>
[563fb40]39
[023a902]40/** Try to clear endpoint halt of default control pipe.
41 *
42 * @param pipe Pipe for control endpoint zero.
43 */
44static void clear_self_endpoint_halt(usb_pipe_t *pipe)
45{
46 assert(pipe != NULL);
47
48 if (!pipe->auto_reset_halt || (pipe->endpoint_no != 0)) {
49 return;
50 }
51
52 /* Prevent infinite recursion. */
53 pipe->auto_reset_halt = false;
54 usb_request_clear_endpoint_halt(pipe, 0);
55 pipe->auto_reset_halt = true;
56}
[a76b01b4]57
[023a902]58/** Request a control read transfer on an endpoint pipe.
59 *
60 * This function encapsulates all three stages of a control transfer.
61 *
62 * @param[in] pipe Pipe used for the transfer.
63 * @param[in] setup_buffer Buffer with the setup packet.
64 * @param[in] setup_buffer_size Size of the setup packet (in bytes).
65 * @param[out] data_buffer Buffer for incoming data.
66 * @param[in] data_buffer_size Size of the buffer for incoming data (in bytes).
67 * @param[out] data_transfered_size Number of bytes that were actually
68 * transfered during the DATA stage.
69 * @return Error code.
70 */
71int usb_pipe_control_read(usb_pipe_t *pipe,
72 const void *setup_buffer, size_t setup_buffer_size,
[22ecbde]73 void *buffer, size_t buffer_size, size_t *transfered_size)
[023a902]74{
75 assert(pipe);
76
77 if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
78 return EINVAL;
79 }
80
[22ecbde]81 if ((buffer == NULL) || (buffer_size == 0)) {
[023a902]82 return EINVAL;
83 }
84
85 if ((pipe->direction != USB_DIRECTION_BOTH)
86 || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
87 return EBADF;
88 }
89
90 uint64_t setup_packet;
91 memcpy(&setup_packet, setup_buffer, 8);
92
[3969a42]93 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
[023a902]94 size_t act_size = 0;
[3969a42]95 const int rc = usb_read(exch,
[22ecbde]96 pipe->endpoint_no, setup_packet, buffer, buffer_size, &act_size);
[3969a42]97 async_exchange_end(exch);
[023a902]98
99 if (rc == ESTALL) {
100 clear_self_endpoint_halt(pipe);
101 }
102
[22ecbde]103 if (rc == EOK && transfered_size != NULL) {
104 *transfered_size = act_size;
[023a902]105 }
106
107 return rc;
108}
[a76b01b4]109
[023a902]110/** Request a control write transfer on an endpoint pipe.
111 *
112 * This function encapsulates all three stages of a control transfer.
113 *
114 * @param[in] pipe Pipe used for the transfer.
115 * @param[in] setup_buffer Buffer with the setup packet.
116 * @param[in] setup_buffer_size Size of the setup packet (in bytes).
117 * @param[in] data_buffer Buffer with data to be sent.
118 * @param[in] data_buffer_size Size of the buffer with outgoing data (in bytes).
119 * @return Error code.
120 */
121int usb_pipe_control_write(usb_pipe_t *pipe,
122 const void *setup_buffer, size_t setup_buffer_size,
[22ecbde]123 const void *buffer, size_t buffer_size)
[023a902]124{
125 assert(pipe);
126
127 if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
128 return EINVAL;
129 }
130
[22ecbde]131 if ((buffer == NULL) && (buffer_size > 0)) {
[023a902]132 return EINVAL;
133 }
134
[22ecbde]135 if ((buffer != NULL) && (buffer_size == 0)) {
[023a902]136 return EINVAL;
137 }
138
139 if ((pipe->direction != USB_DIRECTION_BOTH)
140 || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
141 return EBADF;
142 }
143
144 uint64_t setup_packet;
145 memcpy(&setup_packet, setup_buffer, 8);
146
[3969a42]147 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
148 const int rc = usb_write(exch,
[22ecbde]149 pipe->endpoint_no, setup_packet, buffer, buffer_size);
[3969a42]150 async_exchange_end(exch);
[023a902]151
152 if (rc == ESTALL) {
153 clear_self_endpoint_halt(pipe);
154 }
155
156 return rc;
157}
[a76b01b4]158
[023a902]159/** Request a read (in) transfer on an endpoint pipe.
160 *
161 * @param[in] pipe Pipe used for the transfer.
162 * @param[out] buffer Buffer where to store the data.
163 * @param[in] size Size of the buffer (in bytes).
164 * @param[out] size_transfered Number of bytes that were actually transfered.
165 * @return Error code.
166 */
167int usb_pipe_read(usb_pipe_t *pipe,
168 void *buffer, size_t size, size_t *size_transfered)
169{
170 assert(pipe);
171
172 if (buffer == NULL) {
173 return EINVAL;
174 }
175
176 if (size == 0) {
177 return EINVAL;
178 }
179
180 if (pipe->direction != USB_DIRECTION_IN) {
181 return EBADF;
182 }
183
184 if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
185 return EBADF;
186 }
187
[22ecbde]188 /* Isochronous transfer are not supported (yet) */
189 if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
[01086cfe]190 pipe->transfer_type != USB_TRANSFER_BULK)
[22ecbde]191 return ENOTSUP;
[023a902]192
[3969a42]193 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
[22ecbde]194 size_t act_size = 0;
[3969a42]195 const int rc =
196 usb_read(exch, pipe->endpoint_no, 0, buffer, size, &act_size);
197 async_exchange_end(exch);
[023a902]198
199 if (rc == EOK && size_transfered != NULL) {
200 *size_transfered = act_size;
201 }
202
203 return rc;
204}
[a76b01b4]205
[023a902]206/** Request a write (out) transfer on an endpoint pipe.
207 *
208 * @param[in] pipe Pipe used for the transfer.
209 * @param[in] buffer Buffer with data to transfer.
210 * @param[in] size Size of the buffer (in bytes).
211 * @return Error code.
212 */
213int usb_pipe_write(usb_pipe_t *pipe, const void *buffer, size_t size)
214{
215 assert(pipe);
216
217 if (buffer == NULL || size == 0) {
218 return EINVAL;
219 }
220
221 if (pipe->direction != USB_DIRECTION_OUT) {
222 return EBADF;
223 }
224
225 if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
226 return EBADF;
227 }
228
[22ecbde]229 /* Isochronous transfer are not supported (yet) */
230 if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
[01086cfe]231 pipe->transfer_type != USB_TRANSFER_BULK)
[22ecbde]232 return ENOTSUP;
233
[3969a42]234 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
235 const int rc = usb_write(exch, pipe->endpoint_no, 0, buffer, size);
236 async_exchange_end(exch);
237 return rc;
[023a902]238}
[a76b01b4]239
[c804484]240/** Initialize USB endpoint pipe.
241 *
242 * @param pipe Endpoint pipe to be initialized.
243 * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
244 * @param transfer_type Transfer type (e.g. interrupt or bulk).
245 * @param max_packet_size Maximum packet size in bytes.
246 * @param direction Endpoint direction (in/out).
247 * @return Error code.
248 */
[d93f5afb]249int usb_pipe_initialize(usb_pipe_t *pipe, usb_endpoint_t endpoint_no,
[c804484]250 usb_transfer_type_t transfer_type, size_t max_packet_size,
[8582076]251 usb_direction_t direction, usb_dev_session_t *bus_session)
[c804484]252{
253 assert(pipe);
254
255 pipe->endpoint_no = endpoint_no;
256 pipe->transfer_type = transfer_type;
257 pipe->max_packet_size = max_packet_size;
258 pipe->direction = direction;
259 pipe->auto_reset_halt = false;
[8582076]260 pipe->bus_session = bus_session;
[c804484]261
262 return EOK;
263}
[a76b01b4]264
[c804484]265/** Initialize USB endpoint pipe as the default zero control pipe.
266 *
267 * @param pipe Endpoint pipe to be initialized.
268 * @return Error code.
269 */
270int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
[d93f5afb]271 usb_dev_session_t *bus_session)
[c804484]272{
273 assert(pipe);
274
[d93f5afb]275 const int rc = usb_pipe_initialize(pipe, 0, USB_TRANSFER_CONTROL,
[8582076]276 CTRL_PIPE_MIN_PACKET_SIZE, USB_DIRECTION_BOTH, bus_session);
[c804484]277
278 pipe->auto_reset_halt = true;
279
280 return rc;
281}
[a76b01b4]282
[c804484]283/** Register endpoint with the host controller.
284 *
285 * @param pipe Pipe to be registered.
286 * @param interval Polling interval.
287 * @return Error code.
288 */
289int usb_pipe_register(usb_pipe_t *pipe, unsigned interval)
290{
291 assert(pipe);
[11e9e613]292 assert(pipe->bus_session);
293 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
294 if (!exch)
295 return ENOMEM;
296 const int ret = usb_register_endpoint(exch, pipe->endpoint_no,
297 pipe->transfer_type, pipe->direction, pipe->max_packet_size,
298 interval);
299 async_exchange_end(exch);
300 return ret;
[c804484]301}
[a76b01b4]302
[c804484]303/** Revert endpoint registration with the host controller.
304 *
305 * @param pipe Pipe to be unregistered.
306 * @return Error code.
307 */
308int usb_pipe_unregister(usb_pipe_t *pipe)
309{
310 assert(pipe);
[11e9e613]311 assert(pipe->bus_session);
312 async_exch_t *exch = async_exchange_begin(pipe->bus_session);
313 if (!exch)
314 return ENOMEM;
315 const int ret = usb_unregister_endpoint(exch, pipe->endpoint_no,
316 pipe->direction);
317 async_exchange_end(exch);
318 return ret;
[c804484]319}
320
[6865243c]321/**
322 * @}
323 */
Note: See TracBrowser for help on using the repository browser.