1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 | /** @addtogroup drvusbuhcihc
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief UHCI driver USB transfer structure
|
---|
33 | */
|
---|
34 | #include <errno.h>
|
---|
35 | #include <str_error.h>
|
---|
36 |
|
---|
37 | #include <usb/usb.h>
|
---|
38 | #include <usb/debug.h>
|
---|
39 |
|
---|
40 | #include "batch.h"
|
---|
41 | #include "transfer_list.h"
|
---|
42 | #include "hw_struct/transfer_descriptor.h"
|
---|
43 | #include "utils/malloc32.h"
|
---|
44 |
|
---|
45 | #define DEFAULT_ERROR_COUNT 3
|
---|
46 |
|
---|
47 | typedef struct uhci_transfer_batch {
|
---|
48 | qh_t *qh;
|
---|
49 | td_t *tds;
|
---|
50 | void *device_buffer;
|
---|
51 | size_t td_count;
|
---|
52 | } uhci_transfer_batch_t;
|
---|
53 | /*----------------------------------------------------------------------------*/
|
---|
54 | static void uhci_transfer_batch_dispose(void *uhci_batch)
|
---|
55 | {
|
---|
56 | uhci_transfer_batch_t *instance = uhci_batch;
|
---|
57 | assert(instance);
|
---|
58 | free32(instance->device_buffer);
|
---|
59 | free(instance);
|
---|
60 | }
|
---|
61 | /*----------------------------------------------------------------------------*/
|
---|
62 |
|
---|
63 | static void batch_control(usb_transfer_batch_t *instance,
|
---|
64 | usb_packet_id data_stage, usb_packet_id status_stage);
|
---|
65 | static void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid);
|
---|
66 |
|
---|
67 | /** Allocate memory and initialize internal data structure.
|
---|
68 | *
|
---|
69 | * @param[in] fun DDF function to pass to callback.
|
---|
70 | * @param[in] ep Communication target
|
---|
71 | * @param[in] buffer Data source/destination.
|
---|
72 | * @param[in] size Size of the buffer.
|
---|
73 | * @param[in] setup_buffer Setup data source (if not NULL)
|
---|
74 | * @param[in] setup_size Size of setup_buffer (should be always 8)
|
---|
75 | * @param[in] func_in function to call on inbound transfer completion
|
---|
76 | * @param[in] func_out function to call on outbound transfer completion
|
---|
77 | * @param[in] arg additional parameter to func_in or func_out
|
---|
78 | * @return Valid pointer if all substructures were successfully created,
|
---|
79 | * NULL otherwise.
|
---|
80 | *
|
---|
81 | * Determines the number of needed transfer descriptors (TDs).
|
---|
82 | * Prepares a transport buffer (that is accessible by the hardware).
|
---|
83 | * Initializes parameters needed for the transfer and callback.
|
---|
84 | */
|
---|
85 | usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep,
|
---|
86 | char *buffer, size_t buffer_size, char* setup_buffer, size_t setup_size,
|
---|
87 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
88 | usbhc_iface_transfer_out_callback_t func_out, void *arg)
|
---|
89 | {
|
---|
90 | assert(ep);
|
---|
91 | assert(func_in == NULL || func_out == NULL);
|
---|
92 | assert(func_in != NULL || func_out != NULL);
|
---|
93 |
|
---|
94 | #define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
|
---|
95 | if (ptr == NULL) { \
|
---|
96 | usb_log_error(message); \
|
---|
97 | if (uhci_data) { \
|
---|
98 | uhci_transfer_batch_dispose(uhci_data); \
|
---|
99 | } \
|
---|
100 | return NULL; \
|
---|
101 | } else (void)0
|
---|
102 |
|
---|
103 | uhci_transfer_batch_t *uhci_data =
|
---|
104 | malloc(sizeof(uhci_transfer_batch_t));
|
---|
105 | CHECK_NULL_DISPOSE_RETURN(uhci_data,
|
---|
106 | "Failed to allocate UHCI batch.\n");
|
---|
107 | bzero(uhci_data, sizeof(uhci_transfer_batch_t));
|
---|
108 |
|
---|
109 | uhci_data->td_count =
|
---|
110 | (buffer_size + ep->max_packet_size - 1) / ep->max_packet_size;
|
---|
111 | if (ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
112 | uhci_data->td_count += 2;
|
---|
113 | }
|
---|
114 |
|
---|
115 | assert((sizeof(td_t) % 16) == 0);
|
---|
116 | const size_t total_size = (sizeof(td_t) * uhci_data->td_count)
|
---|
117 | + sizeof(qh_t) + setup_size + buffer_size;
|
---|
118 | uhci_data->device_buffer = malloc32(total_size);
|
---|
119 | CHECK_NULL_DISPOSE_RETURN(uhci_data->device_buffer,
|
---|
120 | "Failed to allocate UHCI buffer.\n");
|
---|
121 | bzero(uhci_data->device_buffer, total_size);
|
---|
122 |
|
---|
123 | uhci_data->tds = uhci_data->device_buffer;
|
---|
124 | uhci_data->qh =
|
---|
125 | (uhci_data->device_buffer + (sizeof(td_t) * uhci_data->td_count));
|
---|
126 |
|
---|
127 | qh_init(uhci_data->qh);
|
---|
128 | qh_set_element_td(uhci_data->qh, uhci_data->tds);
|
---|
129 |
|
---|
130 | usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
|
---|
131 | CHECK_NULL_DISPOSE_RETURN(instance,
|
---|
132 | "Failed to allocate batch instance.\n");
|
---|
133 | void *setup =
|
---|
134 | uhci_data->device_buffer + (sizeof(td_t) * uhci_data->td_count)
|
---|
135 | + sizeof(qh_t);
|
---|
136 | void *data_buffer = setup + setup_size;
|
---|
137 | usb_target_t target =
|
---|
138 | { .address = ep->address, .endpoint = ep->endpoint };
|
---|
139 | usb_transfer_batch_init(instance, ep, buffer, data_buffer, buffer_size,
|
---|
140 | setup, setup_size, func_in, func_out, arg, fun,
|
---|
141 | uhci_data, uhci_transfer_batch_dispose);
|
---|
142 |
|
---|
143 | memcpy(instance->setup_buffer, setup_buffer, setup_size);
|
---|
144 | usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
|
---|
145 | instance, target.address, target.endpoint);
|
---|
146 | return instance;
|
---|
147 | }
|
---|
148 | /*----------------------------------------------------------------------------*/
|
---|
149 | /** Check batch TDs for activity.
|
---|
150 | *
|
---|
151 | * @param[in] instance Batch structure to use.
|
---|
152 | * @return False, if there is an active TD, true otherwise.
|
---|
153 | *
|
---|
154 | * Walk all TDs. Stop with false if there is an active one (it is to be
|
---|
155 | * processed). Stop with true if an error is found. Return true if the last TD
|
---|
156 | * is reached.
|
---|
157 | */
|
---|
158 | bool batch_is_complete(usb_transfer_batch_t *instance)
|
---|
159 | {
|
---|
160 | assert(instance);
|
---|
161 | uhci_transfer_batch_t *data = instance->private_data;
|
---|
162 | assert(data);
|
---|
163 |
|
---|
164 | usb_log_debug2("Batch(%p) checking %d transfer(s) for completion.\n",
|
---|
165 | instance, data->td_count);
|
---|
166 | instance->transfered_size = 0;
|
---|
167 | size_t i = 0;
|
---|
168 | for (;i < data->td_count; ++i) {
|
---|
169 | if (td_is_active(&data->tds[i])) {
|
---|
170 | return false;
|
---|
171 | }
|
---|
172 |
|
---|
173 | instance->error = td_status(&data->tds[i]);
|
---|
174 | if (instance->error != EOK) {
|
---|
175 | usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
|
---|
176 | instance, i, data->tds[i].status);
|
---|
177 | td_print_status(&data->tds[i]);
|
---|
178 |
|
---|
179 | assert(instance->ep != NULL);
|
---|
180 | endpoint_toggle_set(instance->ep,
|
---|
181 | td_toggle(&data->tds[i]));
|
---|
182 | if (i > 0)
|
---|
183 | goto substract_ret;
|
---|
184 | return true;
|
---|
185 | }
|
---|
186 |
|
---|
187 | instance->transfered_size += td_act_size(&data->tds[i]);
|
---|
188 | if (td_is_short(&data->tds[i]))
|
---|
189 | goto substract_ret;
|
---|
190 | }
|
---|
191 | substract_ret:
|
---|
192 | instance->transfered_size -= instance->setup_size;
|
---|
193 | return true;
|
---|
194 | }
|
---|
195 | /*----------------------------------------------------------------------------*/
|
---|
196 | /** Prepares control write transfer.
|
---|
197 | *
|
---|
198 | * @param[in] instance Batch structure to use.
|
---|
199 | *
|
---|
200 | * Uses generic control function with pids OUT and IN.
|
---|
201 | */
|
---|
202 | void batch_control_write(usb_transfer_batch_t *instance)
|
---|
203 | {
|
---|
204 | assert(instance);
|
---|
205 | /* We are data out, we are supposed to provide data */
|
---|
206 | memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
|
---|
207 | batch_control(instance, USB_PID_OUT, USB_PID_IN);
|
---|
208 | instance->next_step = usb_transfer_batch_call_out_and_dispose;
|
---|
209 | usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
|
---|
210 | }
|
---|
211 | /*----------------------------------------------------------------------------*/
|
---|
212 | /** Prepares control read transfer.
|
---|
213 | *
|
---|
214 | * @param[in] instance Batch structure to use.
|
---|
215 | *
|
---|
216 | * Uses generic control with pids IN and OUT.
|
---|
217 | */
|
---|
218 | void batch_control_read(usb_transfer_batch_t *instance)
|
---|
219 | {
|
---|
220 | assert(instance);
|
---|
221 | batch_control(instance, USB_PID_IN, USB_PID_OUT);
|
---|
222 | instance->next_step = usb_transfer_batch_call_in_and_dispose;
|
---|
223 | usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
|
---|
224 | }
|
---|
225 | /*----------------------------------------------------------------------------*/
|
---|
226 | /** Prepare interrupt in transfer.
|
---|
227 | *
|
---|
228 | * @param[in] instance Batch structure to use.
|
---|
229 | *
|
---|
230 | * Data transfer with PID_IN.
|
---|
231 | */
|
---|
232 | void batch_interrupt_in(usb_transfer_batch_t *instance)
|
---|
233 | {
|
---|
234 | assert(instance);
|
---|
235 | batch_data(instance, USB_PID_IN);
|
---|
236 | instance->next_step = usb_transfer_batch_call_in_and_dispose;
|
---|
237 | usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
|
---|
238 | }
|
---|
239 | /*----------------------------------------------------------------------------*/
|
---|
240 | /** Prepare interrupt out transfer.
|
---|
241 | *
|
---|
242 | * @param[in] instance Batch structure to use.
|
---|
243 | *
|
---|
244 | * Data transfer with PID_OUT.
|
---|
245 | */
|
---|
246 | void batch_interrupt_out(usb_transfer_batch_t *instance)
|
---|
247 | {
|
---|
248 | assert(instance);
|
---|
249 | /* We are data out, we are supposed to provide data */
|
---|
250 | memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
|
---|
251 | batch_data(instance, USB_PID_OUT);
|
---|
252 | instance->next_step = usb_transfer_batch_call_out_and_dispose;
|
---|
253 | usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
|
---|
254 | }
|
---|
255 | /*----------------------------------------------------------------------------*/
|
---|
256 | /** Prepare bulk in transfer.
|
---|
257 | *
|
---|
258 | * @param[in] instance Batch structure to use.
|
---|
259 | *
|
---|
260 | * Data transfer with PID_IN.
|
---|
261 | */
|
---|
262 | void batch_bulk_in(usb_transfer_batch_t *instance)
|
---|
263 | {
|
---|
264 | assert(instance);
|
---|
265 | batch_data(instance, USB_PID_IN);
|
---|
266 | instance->next_step = usb_transfer_batch_call_in_and_dispose;
|
---|
267 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
268 | }
|
---|
269 | /*----------------------------------------------------------------------------*/
|
---|
270 | /** Prepare bulk out transfer.
|
---|
271 | *
|
---|
272 | * @param[in] instance Batch structure to use.
|
---|
273 | *
|
---|
274 | * Data transfer with PID_OUT.
|
---|
275 | */
|
---|
276 | void batch_bulk_out(usb_transfer_batch_t *instance)
|
---|
277 | {
|
---|
278 | assert(instance);
|
---|
279 | /* We are data out, we are supposed to provide data */
|
---|
280 | memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
|
---|
281 | batch_data(instance, USB_PID_OUT);
|
---|
282 | instance->next_step = usb_transfer_batch_call_out_and_dispose;
|
---|
283 | usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
|
---|
284 | }
|
---|
285 | /*----------------------------------------------------------------------------*/
|
---|
286 | /** Prepare generic data transfer
|
---|
287 | *
|
---|
288 | * @param[in] instance Batch structure to use.
|
---|
289 | * @param[in] pid Pid to use for data transactions.
|
---|
290 | *
|
---|
291 | * Transactions with alternating toggle bit and supplied pid value.
|
---|
292 | * The last transfer is marked with IOC flag.
|
---|
293 | */
|
---|
294 | void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid)
|
---|
295 | {
|
---|
296 | assert(instance);
|
---|
297 | uhci_transfer_batch_t *data = instance->private_data;
|
---|
298 | assert(data);
|
---|
299 |
|
---|
300 | const bool low_speed = instance->ep->speed == USB_SPEED_LOW;
|
---|
301 | int toggle = endpoint_toggle_get(instance->ep);
|
---|
302 | assert(toggle == 0 || toggle == 1);
|
---|
303 |
|
---|
304 | size_t td = 0;
|
---|
305 | size_t remain_size = instance->buffer_size;
|
---|
306 | char *buffer = instance->data_buffer;
|
---|
307 | while (remain_size > 0) {
|
---|
308 | const size_t packet_size =
|
---|
309 | (instance->ep->max_packet_size > remain_size) ?
|
---|
310 | remain_size : instance->ep->max_packet_size;
|
---|
311 |
|
---|
312 | td_t *next_td = (td + 1 < data->td_count)
|
---|
313 | ? &data->tds[td + 1] : NULL;
|
---|
314 |
|
---|
315 |
|
---|
316 | usb_target_t target =
|
---|
317 | { instance->ep->address, instance->ep->endpoint };
|
---|
318 |
|
---|
319 | assert(td < data->td_count);
|
---|
320 | td_init(
|
---|
321 | &data->tds[td], DEFAULT_ERROR_COUNT, packet_size,
|
---|
322 | toggle, false, low_speed, target, pid, buffer, next_td);
|
---|
323 |
|
---|
324 | ++td;
|
---|
325 | toggle = 1 - toggle;
|
---|
326 | buffer += packet_size;
|
---|
327 | assert(packet_size <= remain_size);
|
---|
328 | remain_size -= packet_size;
|
---|
329 | }
|
---|
330 | td_set_ioc(&data->tds[td - 1]);
|
---|
331 | endpoint_toggle_set(instance->ep, toggle);
|
---|
332 | }
|
---|
333 | /*----------------------------------------------------------------------------*/
|
---|
334 | /** Prepare generic control transfer
|
---|
335 | *
|
---|
336 | * @param[in] instance Batch structure to use.
|
---|
337 | * @param[in] data_stage Pid to use for data tds.
|
---|
338 | * @param[in] status_stage Pid to use for data tds.
|
---|
339 | *
|
---|
340 | * Setup stage with toggle 0 and USB_PID_SETUP.
|
---|
341 | * Data stage with alternating toggle and pid supplied by parameter.
|
---|
342 | * Status stage with toggle 1 and pid supplied by parameter.
|
---|
343 | * The last transfer is marked with IOC.
|
---|
344 | */
|
---|
345 | void batch_control(usb_transfer_batch_t *instance,
|
---|
346 | usb_packet_id data_stage, usb_packet_id status_stage)
|
---|
347 | {
|
---|
348 | assert(instance);
|
---|
349 | uhci_transfer_batch_t *data = instance->private_data;
|
---|
350 | assert(data);
|
---|
351 | assert(data->td_count >= 2);
|
---|
352 |
|
---|
353 | const bool low_speed = instance->ep->speed == USB_SPEED_LOW;
|
---|
354 | const usb_target_t target =
|
---|
355 | { instance->ep->address, instance->ep->endpoint };
|
---|
356 |
|
---|
357 | /* setup stage */
|
---|
358 | td_init(
|
---|
359 | data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, 0, false,
|
---|
360 | low_speed, target, USB_PID_SETUP, instance->setup_buffer,
|
---|
361 | &data->tds[1]);
|
---|
362 |
|
---|
363 | /* data stage */
|
---|
364 | size_t td = 1;
|
---|
365 | unsigned toggle = 1;
|
---|
366 | size_t remain_size = instance->buffer_size;
|
---|
367 | char *buffer = instance->data_buffer;
|
---|
368 | while (remain_size > 0) {
|
---|
369 | const size_t packet_size =
|
---|
370 | (instance->ep->max_packet_size > remain_size) ?
|
---|
371 | remain_size : instance->ep->max_packet_size;
|
---|
372 |
|
---|
373 | td_init(
|
---|
374 | &data->tds[td], DEFAULT_ERROR_COUNT, packet_size,
|
---|
375 | toggle, false, low_speed, target, data_stage,
|
---|
376 | buffer, &data->tds[td + 1]);
|
---|
377 |
|
---|
378 | ++td;
|
---|
379 | toggle = 1 - toggle;
|
---|
380 | buffer += packet_size;
|
---|
381 | assert(td < data->td_count);
|
---|
382 | assert(packet_size <= remain_size);
|
---|
383 | remain_size -= packet_size;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* status stage */
|
---|
387 | assert(td == data->td_count - 1);
|
---|
388 |
|
---|
389 | td_init(
|
---|
390 | &data->tds[td], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
|
---|
391 | target, status_stage, NULL, NULL);
|
---|
392 | td_set_ioc(&data->tds[td]);
|
---|
393 |
|
---|
394 | usb_log_debug2("Control last TD status: %x.\n",
|
---|
395 | data->tds[td].status);
|
---|
396 | }
|
---|
397 | /*----------------------------------------------------------------------------*/
|
---|
398 | qh_t * batch_qh(usb_transfer_batch_t *instance)
|
---|
399 | {
|
---|
400 | assert(instance);
|
---|
401 | uhci_transfer_batch_t *data = instance->private_data;
|
---|
402 | assert(data);
|
---|
403 | return data->qh;
|
---|
404 | }
|
---|
405 | /**
|
---|
406 | * @}
|
---|
407 | */
|
---|