source: mainline/uspace/drv/uhci-hcd/batch.c@ 20a1e76

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

Rename packet ⇒ transfer, avoids naming confusion

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