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 transaction 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 "uhci_hc.h"
|
---|
43 | #include "utils/malloc32.h"
|
---|
44 | #include "uhci_struct/transfer_descriptor.h"
|
---|
45 |
|
---|
46 | #define DEFAULT_ERROR_COUNT 3
|
---|
47 |
|
---|
48 | typedef struct uhci_batch {
|
---|
49 | qh_t *qh;
|
---|
50 | td_t *tds;
|
---|
51 | size_t packets;
|
---|
52 | device_keeper_t *manager;
|
---|
53 | } uhci_batch_t;
|
---|
54 |
|
---|
55 | static void batch_control(batch_t *instance,
|
---|
56 | usb_packet_id data_stage, usb_packet_id status_stage);
|
---|
57 | static void batch_data(batch_t *instance, usb_packet_id pid);
|
---|
58 | static void batch_call_in_and_dispose(batch_t *instance);
|
---|
59 | static void batch_call_out_and_dispose(batch_t *instance);
|
---|
60 |
|
---|
61 |
|
---|
62 | /** Allocate memory and initialize internal data structure.
|
---|
63 | *
|
---|
64 | * @param[in] fun DDF function to pass to callback.
|
---|
65 | * @param[in] target Device and endpoint target of the transaction.
|
---|
66 | * @param[in] transfer_type Interrupt, Control or Bulk.
|
---|
67 | * @param[in] max_packet_size maximum allowed size of data packets.
|
---|
68 | * @param[in] speed Speed of the transaction.
|
---|
69 | * @param[in] buffer Data source/destination.
|
---|
70 | * @param[in] size Size of the buffer.
|
---|
71 | * @param[in] setup_buffer Setup data source (if not NULL)
|
---|
72 | * @param[in] setup_size Size of setup_buffer (should be always 8)
|
---|
73 | * @param[in] func_in function to call on inbound transaction completion
|
---|
74 | * @param[in] func_out function to call on outbound transaction completion
|
---|
75 | * @param[in] arg additional parameter to func_in or func_out
|
---|
76 | * @param[in] manager Pointer to toggle management structure.
|
---|
77 | * @return Valid pointer if all substructures were successfully created,
|
---|
78 | * NULL otherwise.
|
---|
79 | *
|
---|
80 | * Determines the number of needed packets (TDs). Prepares a transport buffer
|
---|
81 | * (that is accessible by the hardware). Initializes parameters needed for the
|
---|
82 | * transaction and callback.
|
---|
83 | */
|
---|
84 | batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
|
---|
85 | usb_transfer_type_t transfer_type, size_t max_packet_size,
|
---|
86 | usb_speed_t speed, char *buffer, size_t buffer_size,
|
---|
87 | char* setup_buffer, size_t setup_size,
|
---|
88 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
89 | usbhc_iface_transfer_out_callback_t func_out, void *arg,
|
---|
90 | device_keeper_t *manager
|
---|
91 | )
|
---|
92 | {
|
---|
93 | assert(func_in == NULL || func_out == NULL);
|
---|
94 | assert(func_in != NULL || func_out != NULL);
|
---|
95 |
|
---|
96 | #define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
|
---|
97 | if (ptr == NULL) { \
|
---|
98 | usb_log_error(message); \
|
---|
99 | if (instance) { \
|
---|
100 | batch_dispose(instance); \
|
---|
101 | } \
|
---|
102 | return NULL; \
|
---|
103 | } else (void)0
|
---|
104 |
|
---|
105 | batch_t *instance = malloc(sizeof(batch_t));
|
---|
106 | CHECK_NULL_DISPOSE_RETURN(instance,
|
---|
107 | "Failed to allocate batch instance.\n");
|
---|
108 | batch_init(instance, target, transfer_type, speed, max_packet_size,
|
---|
109 | buffer, NULL, buffer_size, NULL, setup_size, func_in,
|
---|
110 | func_out, arg, fun, NULL);
|
---|
111 |
|
---|
112 |
|
---|
113 | uhci_batch_t *data = malloc(sizeof(uhci_batch_t));
|
---|
114 | CHECK_NULL_DISPOSE_RETURN(instance,
|
---|
115 | "Failed to allocate batch instance.\n");
|
---|
116 | bzero(data, sizeof(uhci_batch_t));
|
---|
117 | data->manager = manager;
|
---|
118 | instance->private_data = data;
|
---|
119 |
|
---|
120 | data->packets = (buffer_size + max_packet_size - 1) / max_packet_size;
|
---|
121 | if (transfer_type == USB_TRANSFER_CONTROL) {
|
---|
122 | data->packets += 2;
|
---|
123 | }
|
---|
124 |
|
---|
125 | data->tds = malloc32(sizeof(td_t) * data->packets);
|
---|
126 | CHECK_NULL_DISPOSE_RETURN(
|
---|
127 | data->tds, "Failed to allocate transfer descriptors.\n");
|
---|
128 | bzero(data->tds, sizeof(td_t) * data->packets);
|
---|
129 |
|
---|
130 | data->qh = malloc32(sizeof(qh_t));
|
---|
131 | CHECK_NULL_DISPOSE_RETURN(data->qh,
|
---|
132 | "Failed to allocate batch queue head.\n");
|
---|
133 | qh_init(data->qh);
|
---|
134 | qh_set_element_td(data->qh, addr_to_phys(data->tds));
|
---|
135 |
|
---|
136 | if (buffer_size > 0) {
|
---|
137 | instance->transport_buffer = malloc32(buffer_size);
|
---|
138 | CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
|
---|
139 | "Failed to allocate device accessible buffer.\n");
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (setup_size > 0) {
|
---|
143 | instance->setup_buffer = malloc32(setup_size);
|
---|
144 | CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
|
---|
145 | "Failed to allocate device accessible setup buffer.\n");
|
---|
146 | memcpy(instance->setup_buffer, setup_buffer, setup_size);
|
---|
147 | }
|
---|
148 |
|
---|
149 | usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
|
---|
150 | instance, target.address, target.endpoint);
|
---|
151 | return instance;
|
---|
152 | }
|
---|
153 | /*----------------------------------------------------------------------------*/
|
---|
154 | /** Check batch TDs for activity.
|
---|
155 | *
|
---|
156 | * @param[in] instance Batch structure to use.
|
---|
157 | * @return False, if there is an active TD, true otherwise.
|
---|
158 | *
|
---|
159 | * Walk all TDs. Stop with false if there is an active one (it is to be
|
---|
160 | * processed). Stop with true if an error is found. Return true if the last TS
|
---|
161 | * is reached.
|
---|
162 | */
|
---|
163 | bool batch_is_complete(batch_t *instance)
|
---|
164 | {
|
---|
165 | assert(instance);
|
---|
166 | uhci_batch_t *data = instance->private_data;
|
---|
167 | assert(data);
|
---|
168 |
|
---|
169 | usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
|
---|
170 | instance, data->packets);
|
---|
171 | instance->transfered_size = 0;
|
---|
172 | size_t i = 0;
|
---|
173 | for (;i < data->packets; ++i) {
|
---|
174 | if (td_is_active(&data->tds[i])) {
|
---|
175 | return false;
|
---|
176 | }
|
---|
177 |
|
---|
178 | instance->error = td_status(&data->tds[i]);
|
---|
179 | if (instance->error != EOK) {
|
---|
180 | usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
|
---|
181 | instance, i, data->tds[i].status);
|
---|
182 | td_print_status(&data->tds[i]);
|
---|
183 |
|
---|
184 | device_keeper_set_toggle(data->manager,
|
---|
185 | instance->target, td_toggle(&data->tds[i]));
|
---|
186 | if (i > 0)
|
---|
187 | goto substract_ret;
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 |
|
---|
191 | instance->transfered_size += td_act_size(&data->tds[i]);
|
---|
192 | if (td_is_short(&data->tds[i]))
|
---|
193 | goto substract_ret;
|
---|
194 | }
|
---|
195 | substract_ret:
|
---|
196 | instance->transfered_size -= instance->setup_size;
|
---|
197 | return true;
|
---|
198 | }
|
---|
199 | /*----------------------------------------------------------------------------*/
|
---|
200 | /** Prepares control write transaction.
|
---|
201 | *
|
---|
202 | * @param[in] instance Batch structure to use.
|
---|
203 | *
|
---|
204 | * Uses genercir control function with pids OUT and IN.
|
---|
205 | */
|
---|
206 | void batch_control_write(batch_t *instance)
|
---|
207 | {
|
---|
208 | assert(instance);
|
---|
209 | /* We are data out, we are supposed to provide data */
|
---|
210 | memcpy(instance->transport_buffer, instance->buffer,
|
---|
211 | instance->buffer_size);
|
---|
212 | batch_control(instance, USB_PID_OUT, USB_PID_IN);
|
---|
213 | instance->next_step = batch_call_out_and_dispose;
|
---|
214 | usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
|
---|
215 | }
|
---|
216 | /*----------------------------------------------------------------------------*/
|
---|
217 | /** Prepares control read transaction.
|
---|
218 | *
|
---|
219 | * @param[in] instance Batch structure to use.
|
---|
220 | *
|
---|
221 | * Uses generic control with pids IN and OUT.
|
---|
222 | */
|
---|
223 | void batch_control_read(batch_t *instance)
|
---|
224 | {
|
---|
225 | assert(instance);
|
---|
226 | batch_control(instance, USB_PID_IN, USB_PID_OUT);
|
---|
227 | instance->next_step = batch_call_in_and_dispose;
|
---|
228 | usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
|
---|
229 | }
|
---|
230 | /*----------------------------------------------------------------------------*/
|
---|
231 | /** Prepare interrupt in transaction.
|
---|
232 | *
|
---|
233 | * @param[in] instance Batch structure to use.
|
---|
234 | *
|
---|
235 | * Data transaction with PID_IN.
|
---|
236 | */
|
---|
237 | void batch_interrupt_in(batch_t *instance)
|
---|
238 | {
|
---|
239 | assert(instance);
|
---|
240 | batch_data(instance, USB_PID_IN);
|
---|
241 | instance->next_step = batch_call_in_and_dispose;
|
---|
242 | usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
|
---|
243 | }
|
---|
244 | /*----------------------------------------------------------------------------*/
|
---|
245 | /** Prepare interrupt out transaction.
|
---|
246 | *
|
---|
247 | * @param[in] instance Batch structure to use.
|
---|
248 | *
|
---|
249 | * Data transaction with PID_OUT.
|
---|
250 | */
|
---|
251 | void batch_interrupt_out(batch_t *instance)
|
---|
252 | {
|
---|
253 | assert(instance);
|
---|
254 | /* We are data out, we are supposed to provide data */
|
---|
255 | memcpy(instance->transport_buffer, instance->buffer,
|
---|
256 | instance->buffer_size);
|
---|
257 | batch_data(instance, USB_PID_OUT);
|
---|
258 | instance->next_step = batch_call_out_and_dispose;
|
---|
259 | usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
|
---|
260 | }
|
---|
261 | /*----------------------------------------------------------------------------*/
|
---|
262 | /** Prepare bulk in transaction.
|
---|
263 | *
|
---|
264 | * @param[in] instance Batch structure to use.
|
---|
265 | *
|
---|
266 | * Data transaction with PID_IN.
|
---|
267 | */
|
---|
268 | void batch_bulk_in(batch_t *instance)
|
---|
269 | {
|
---|
270 | assert(instance);
|
---|
271 | batch_data(instance, USB_PID_IN);
|
---|
272 | instance->next_step = batch_call_in_and_dispose;
|
---|
273 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
274 | }
|
---|
275 | /*----------------------------------------------------------------------------*/
|
---|
276 | /** Prepare bulk out transaction.
|
---|
277 | *
|
---|
278 | * @param[in] instance Batch structure to use.
|
---|
279 | *
|
---|
280 | * Data transaction with PID_OUT.
|
---|
281 | */
|
---|
282 | void batch_bulk_out(batch_t *instance)
|
---|
283 | {
|
---|
284 | assert(instance);
|
---|
285 | /* We are data out, we are supposed to provide data */
|
---|
286 | memcpy(instance->transport_buffer, instance->buffer,
|
---|
287 | instance->buffer_size);
|
---|
288 | batch_data(instance, USB_PID_OUT);
|
---|
289 | instance->next_step = batch_call_out_and_dispose;
|
---|
290 | usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
|
---|
291 | }
|
---|
292 | /*----------------------------------------------------------------------------*/
|
---|
293 | /** Prepare generic data transaction
|
---|
294 | *
|
---|
295 | * @param[in] instance Batch structure to use.
|
---|
296 | * @param[in] pid to use for data packets.
|
---|
297 | *
|
---|
298 | * Packets with alternating toggle bit and supplied pid value.
|
---|
299 | * The last packet is marked with IOC flag.
|
---|
300 | */
|
---|
301 | void batch_data(batch_t *instance, usb_packet_id pid)
|
---|
302 | {
|
---|
303 | assert(instance);
|
---|
304 | uhci_batch_t *data = instance->private_data;
|
---|
305 | assert(data);
|
---|
306 |
|
---|
307 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
---|
308 | int toggle = device_keeper_get_toggle(data->manager, instance->target);
|
---|
309 | assert(toggle == 0 || toggle == 1);
|
---|
310 |
|
---|
311 | size_t packet = 0;
|
---|
312 | size_t remain_size = instance->buffer_size;
|
---|
313 | while (remain_size > 0) {
|
---|
314 | char *trans_data =
|
---|
315 | instance->transport_buffer + instance->buffer_size
|
---|
316 | - remain_size;
|
---|
317 |
|
---|
318 | const size_t packet_size =
|
---|
319 | (instance->max_packet_size > remain_size) ?
|
---|
320 | remain_size : instance->max_packet_size;
|
---|
321 |
|
---|
322 | td_t *next_packet = (packet + 1 < data->packets)
|
---|
323 | ? &data->tds[packet + 1] : NULL;
|
---|
324 |
|
---|
325 | assert(packet < data->packets);
|
---|
326 | assert(packet_size <= remain_size);
|
---|
327 |
|
---|
328 | td_init(
|
---|
329 | &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
|
---|
330 | toggle, false, low_speed, instance->target, pid, trans_data,
|
---|
331 | next_packet);
|
---|
332 |
|
---|
333 |
|
---|
334 | toggle = 1 - toggle;
|
---|
335 | remain_size -= packet_size;
|
---|
336 | ++packet;
|
---|
337 | }
|
---|
338 | td_set_ioc(&data->tds[packet - 1]);
|
---|
339 | device_keeper_set_toggle(data->manager, instance->target, toggle);
|
---|
340 | }
|
---|
341 | /*----------------------------------------------------------------------------*/
|
---|
342 | /** Prepare generic control transaction
|
---|
343 | *
|
---|
344 | * @param[in] instance Batch structure to use.
|
---|
345 | * @param[in] data_stage to use for data packets.
|
---|
346 | * @param[in] status_stage to use for data packets.
|
---|
347 | *
|
---|
348 | * Setup stage with toggle 0 and USB_PID_SETUP.
|
---|
349 | * Data stage with alternating toggle and pid supplied by parameter.
|
---|
350 | * Status stage with toggle 1 and pid supplied by parameter.
|
---|
351 | * The last packet is marked with IOC.
|
---|
352 | */
|
---|
353 | void batch_control(batch_t *instance,
|
---|
354 | usb_packet_id data_stage, usb_packet_id status_stage)
|
---|
355 | {
|
---|
356 | assert(instance);
|
---|
357 | uhci_batch_t *data = instance->private_data;
|
---|
358 | assert(data);
|
---|
359 | assert(data->packets >= 2);
|
---|
360 |
|
---|
361 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
---|
362 | int toggle = 0;
|
---|
363 | /* setup stage */
|
---|
364 | td_init(
|
---|
365 | data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, toggle, false,
|
---|
366 | low_speed, instance->target, USB_PID_SETUP, instance->setup_buffer,
|
---|
367 | &data->tds[1]);
|
---|
368 |
|
---|
369 | /* data stage */
|
---|
370 | size_t packet = 1;
|
---|
371 | size_t remain_size = instance->buffer_size;
|
---|
372 | while (remain_size > 0) {
|
---|
373 | char *control_data =
|
---|
374 | instance->transport_buffer + instance->buffer_size
|
---|
375 | - remain_size;
|
---|
376 |
|
---|
377 | toggle = 1 - toggle;
|
---|
378 |
|
---|
379 | const size_t packet_size =
|
---|
380 | (instance->max_packet_size > remain_size) ?
|
---|
381 | remain_size : instance->max_packet_size;
|
---|
382 |
|
---|
383 | td_init(
|
---|
384 | &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
|
---|
385 | toggle, false, low_speed, instance->target, data_stage,
|
---|
386 | control_data, &data->tds[packet + 1]);
|
---|
387 |
|
---|
388 | ++packet;
|
---|
389 | assert(packet < data->packets);
|
---|
390 | assert(packet_size <= remain_size);
|
---|
391 | remain_size -= packet_size;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /* status stage */
|
---|
395 | assert(packet == data->packets - 1);
|
---|
396 |
|
---|
397 | td_init(
|
---|
398 | &data->tds[packet], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
|
---|
399 | instance->target, status_stage, NULL, NULL);
|
---|
400 | td_set_ioc(&data->tds[packet]);
|
---|
401 |
|
---|
402 | usb_log_debug2("Control last TD status: %x.\n",
|
---|
403 | data->tds[packet].status);
|
---|
404 | }
|
---|
405 | /*----------------------------------------------------------------------------*/
|
---|
406 | qh_t * batch_qh(batch_t *instance)
|
---|
407 | {
|
---|
408 | assert(instance);
|
---|
409 | uhci_batch_t *data = instance->private_data;
|
---|
410 | assert(data);
|
---|
411 | return data->qh;
|
---|
412 | }
|
---|
413 | /*----------------------------------------------------------------------------*/
|
---|
414 | /** Helper function calls callback and correctly disposes of batch structure.
|
---|
415 | *
|
---|
416 | * @param[in] instance Batch structure to use.
|
---|
417 | */
|
---|
418 | void batch_call_in_and_dispose(batch_t *instance)
|
---|
419 | {
|
---|
420 | assert(instance);
|
---|
421 | batch_call_in(instance);
|
---|
422 | batch_dispose(instance);
|
---|
423 | }
|
---|
424 | /*----------------------------------------------------------------------------*/
|
---|
425 | /** Helper function calls callback and correctly disposes of batch structure.
|
---|
426 | *
|
---|
427 | * @param[in] instance Batch structure to use.
|
---|
428 | */
|
---|
429 | void batch_call_out_and_dispose(batch_t *instance)
|
---|
430 | {
|
---|
431 | assert(instance);
|
---|
432 | batch_call_out(instance);
|
---|
433 | batch_dispose(instance);
|
---|
434 | }
|
---|
435 | /*----------------------------------------------------------------------------*/
|
---|
436 | /** Correctly dispose all used data structures.
|
---|
437 | *
|
---|
438 | * @param[in] instance Batch structure to use.
|
---|
439 | */
|
---|
440 | void batch_dispose(batch_t *instance)
|
---|
441 | {
|
---|
442 | assert(instance);
|
---|
443 | uhci_batch_t *data = instance->private_data;
|
---|
444 | assert(data);
|
---|
445 | usb_log_debug("Batch(%p) disposing.\n", instance);
|
---|
446 | /* free32 is NULL safe */
|
---|
447 | free32(data->tds);
|
---|
448 | free32(data->qh);
|
---|
449 | free32(instance->setup_buffer);
|
---|
450 | free32(instance->transport_buffer);
|
---|
451 | free(data);
|
---|
452 | free(instance);
|
---|
453 | }
|
---|
454 | /**
|
---|
455 | * @}
|
---|
456 | */
|
---|