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 | /** Try to clear endpoint halt of default control pipe.
|
---|
41 | *
|
---|
42 | * @param pipe Pipe for control endpoint zero.
|
---|
43 | */
|
---|
44 | static 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 | }
|
---|
57 |
|
---|
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 | */
|
---|
71 | int usb_pipe_control_read(usb_pipe_t *pipe,
|
---|
72 | const void *setup_buffer, size_t setup_buffer_size,
|
---|
73 | void *buffer, size_t buffer_size, size_t *transfered_size)
|
---|
74 | {
|
---|
75 | assert(pipe);
|
---|
76 |
|
---|
77 | if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
|
---|
78 | return EINVAL;
|
---|
79 | }
|
---|
80 |
|
---|
81 | if ((buffer == NULL) || (buffer_size == 0)) {
|
---|
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 |
|
---|
93 | async_exch_t *exch = async_exchange_begin(pipe->bus_session);
|
---|
94 | size_t act_size = 0;
|
---|
95 | const int rc = usb_read(exch,
|
---|
96 | pipe->endpoint_no, setup_packet, buffer, buffer_size, &act_size);
|
---|
97 | async_exchange_end(exch);
|
---|
98 |
|
---|
99 | if (rc == ESTALL) {
|
---|
100 | clear_self_endpoint_halt(pipe);
|
---|
101 | }
|
---|
102 |
|
---|
103 | if (rc == EOK && transfered_size != NULL) {
|
---|
104 | *transfered_size = act_size;
|
---|
105 | }
|
---|
106 |
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 |
|
---|
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 | */
|
---|
121 | int usb_pipe_control_write(usb_pipe_t *pipe,
|
---|
122 | const void *setup_buffer, size_t setup_buffer_size,
|
---|
123 | const void *buffer, size_t buffer_size)
|
---|
124 | {
|
---|
125 | assert(pipe);
|
---|
126 |
|
---|
127 | if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
|
---|
128 | return EINVAL;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if ((buffer == NULL) && (buffer_size > 0)) {
|
---|
132 | return EINVAL;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if ((buffer != NULL) && (buffer_size == 0)) {
|
---|
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 |
|
---|
147 | async_exch_t *exch = async_exchange_begin(pipe->bus_session);
|
---|
148 | const int rc = usb_write(exch,
|
---|
149 | pipe->endpoint_no, setup_packet, buffer, buffer_size);
|
---|
150 | async_exchange_end(exch);
|
---|
151 |
|
---|
152 | if (rc == ESTALL) {
|
---|
153 | clear_self_endpoint_halt(pipe);
|
---|
154 | }
|
---|
155 |
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
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 | */
|
---|
167 | int 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 |
|
---|
188 | /* Isochronous transfer are not supported (yet) */
|
---|
189 | if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
|
---|
190 | pipe->transfer_type != USB_TRANSFER_BULK)
|
---|
191 | return ENOTSUP;
|
---|
192 |
|
---|
193 | async_exch_t *exch = async_exchange_begin(pipe->bus_session);
|
---|
194 | size_t act_size = 0;
|
---|
195 | const int rc =
|
---|
196 | usb_read(exch, pipe->endpoint_no, 0, buffer, size, &act_size);
|
---|
197 | async_exchange_end(exch);
|
---|
198 |
|
---|
199 | if (rc == EOK && size_transfered != NULL) {
|
---|
200 | *size_transfered = act_size;
|
---|
201 | }
|
---|
202 |
|
---|
203 | return rc;
|
---|
204 | }
|
---|
205 |
|
---|
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 | */
|
---|
213 | int 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 |
|
---|
229 | /* Isochronous transfer are not supported (yet) */
|
---|
230 | if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
|
---|
231 | pipe->transfer_type != USB_TRANSFER_BULK)
|
---|
232 | return ENOTSUP;
|
---|
233 |
|
---|
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;
|
---|
238 | }
|
---|
239 |
|
---|
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 | */
|
---|
249 | int usb_pipe_initialize(usb_pipe_t *pipe, usb_endpoint_t endpoint_no,
|
---|
250 | usb_transfer_type_t transfer_type, size_t max_packet_size,
|
---|
251 | usb_direction_t direction, usb_dev_session_t *bus_session)
|
---|
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;
|
---|
260 | pipe->bus_session = bus_session;
|
---|
261 |
|
---|
262 | return EOK;
|
---|
263 | }
|
---|
264 |
|
---|
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 | */
|
---|
270 | int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
|
---|
271 | usb_dev_session_t *bus_session)
|
---|
272 | {
|
---|
273 | assert(pipe);
|
---|
274 |
|
---|
275 | const int rc = usb_pipe_initialize(pipe, 0, USB_TRANSFER_CONTROL,
|
---|
276 | CTRL_PIPE_MIN_PACKET_SIZE, USB_DIRECTION_BOTH, bus_session);
|
---|
277 |
|
---|
278 | pipe->auto_reset_halt = true;
|
---|
279 |
|
---|
280 | return rc;
|
---|
281 | }
|
---|
282 |
|
---|
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 | */
|
---|
289 | int usb_pipe_register(usb_pipe_t *pipe, unsigned interval)
|
---|
290 | {
|
---|
291 | assert(pipe);
|
---|
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;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /** Revert endpoint registration with the host controller.
|
---|
304 | *
|
---|
305 | * @param pipe Pipe to be unregistered.
|
---|
306 | * @return Error code.
|
---|
307 | */
|
---|
308 | int usb_pipe_unregister(usb_pipe_t *pipe)
|
---|
309 | {
|
---|
310 | assert(pipe);
|
---|
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;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * @}
|
---|
323 | */
|
---|