source: mainline/uspace/drv/uhci-hcd/batch.c@ 508a0ca

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

Rename transfer ⇒ td

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