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

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

Remove redundant batch_get parameters

  • Property mode set to 100644
File size: 14.9 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} 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 transfers.
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 transfers (TDs). Prepares a transport buffer
79 * (that is accessible by the hardware). Initializes parameters needed for the
80 * 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, target,
106 ep->transfer_type, ep->speed, ep->max_packet_size,
107 buffer, NULL, buffer_size, NULL, setup_size, func_in,
108 func_out, arg, fun, ep, NULL);
109
110
111 uhci_batch_t *data = malloc(sizeof(uhci_batch_t));
112 CHECK_NULL_DISPOSE_RETURN(instance,
113 "Failed to allocate batch instance.\n");
114 bzero(data, sizeof(uhci_batch_t));
115 instance->private_data = data;
116
117 data->transfers =
118 (buffer_size + ep->max_packet_size - 1) / ep->max_packet_size;
119 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
120 data->transfers += 2;
121 }
122
123 data->tds = malloc32(sizeof(td_t) * data->transfers);
124 CHECK_NULL_DISPOSE_RETURN(
125 data->tds, "Failed to allocate transfer descriptors.\n");
126 bzero(data->tds, sizeof(td_t) * data->transfers);
127
128 data->qh = malloc32(sizeof(qh_t));
129 CHECK_NULL_DISPOSE_RETURN(data->qh,
130 "Failed to allocate batch queue head.\n");
131 qh_init(data->qh);
132 qh_set_element_td(data->qh, addr_to_phys(data->tds));
133
134 if (buffer_size > 0) {
135 instance->transport_buffer = malloc32(buffer_size);
136 CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
137 "Failed to allocate device accessible buffer.\n");
138 }
139
140 if (setup_size > 0) {
141 instance->setup_buffer = malloc32(setup_size);
142 CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
143 "Failed to allocate device accessible setup buffer.\n");
144 memcpy(instance->setup_buffer, setup_buffer, setup_size);
145 }
146
147 usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
148 instance, target.address, target.endpoint);
149 return instance;
150}
151/*----------------------------------------------------------------------------*/
152/** Check batch TDs for activity.
153 *
154 * @param[in] instance Batch structure to use.
155 * @return False, if there is an active TD, true otherwise.
156 *
157 * Walk all TDs. Stop with false if there is an active one (it is to be
158 * processed). Stop with true if an error is found. Return true if the last TS
159 * is reached.
160 */
161bool batch_is_complete(usb_transfer_batch_t *instance)
162{
163 assert(instance);
164 uhci_batch_t *data = instance->private_data;
165 assert(data);
166
167 usb_log_debug2("Batch(%p) checking %d transfer(s) for completion.\n",
168 instance, data->transfers);
169 instance->transfered_size = 0;
170 size_t i = 0;
171 for (;i < data->transfers; ++i) {
172 if (td_is_active(&data->tds[i])) {
173 return false;
174 }
175
176 instance->error = td_status(&data->tds[i]);
177 if (instance->error != EOK) {
178 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
179 instance, i, data->tds[i].status);
180 td_print_status(&data->tds[i]);
181 assert(instance->ep != NULL);
182
183 endpoint_toggle_set(instance->ep,
184 td_toggle(&data->tds[i]));
185 if (i > 0)
186 goto substract_ret;
187 return true;
188 }
189
190 instance->transfered_size += td_act_size(&data->tds[i]);
191 if (td_is_short(&data->tds[i]))
192 goto substract_ret;
193 }
194substract_ret:
195 instance->transfered_size -= instance->setup_size;
196 return true;
197}
198/*----------------------------------------------------------------------------*/
199/** Prepares control write transaction.
200 *
201 * @param[in] instance Batch structure to use.
202 *
203 * Uses genercir control function with pids OUT and IN.
204 */
205void batch_control_write(usb_transfer_batch_t *instance)
206{
207 assert(instance);
208 /* We are data out, we are supposed to provide data */
209 memcpy(instance->transport_buffer, instance->buffer,
210 instance->buffer_size);
211 batch_control(instance, USB_PID_OUT, USB_PID_IN);
212 instance->next_step = batch_call_out_and_dispose;
213 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
214}
215/*----------------------------------------------------------------------------*/
216/** Prepares control read transaction.
217 *
218 * @param[in] instance Batch structure to use.
219 *
220 * Uses generic control with pids IN and OUT.
221 */
222void batch_control_read(usb_transfer_batch_t *instance)
223{
224 assert(instance);
225 batch_control(instance, USB_PID_IN, USB_PID_OUT);
226 instance->next_step = batch_call_in_and_dispose;
227 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
228}
229/*----------------------------------------------------------------------------*/
230/** Prepare interrupt in transaction.
231 *
232 * @param[in] instance Batch structure to use.
233 *
234 * Data transaction with PID_IN.
235 */
236void batch_interrupt_in(usb_transfer_batch_t *instance)
237{
238 assert(instance);
239 instance->direction = USB_DIRECTION_IN;
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 */
251void batch_interrupt_out(usb_transfer_batch_t *instance)
252{
253 assert(instance);
254 instance->direction = USB_DIRECTION_OUT;
255 /* We are data out, we are supposed to provide data */
256 memcpy(instance->transport_buffer, instance->buffer,
257 instance->buffer_size);
258 batch_data(instance, USB_PID_OUT);
259 instance->next_step = batch_call_out_and_dispose;
260 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
261}
262/*----------------------------------------------------------------------------*/
263/** Prepare bulk in transaction.
264 *
265 * @param[in] instance Batch structure to use.
266 *
267 * Data transaction with PID_IN.
268 */
269void batch_bulk_in(usb_transfer_batch_t *instance)
270{
271 assert(instance);
272 batch_data(instance, USB_PID_IN);
273 instance->direction = USB_DIRECTION_IN;
274 instance->next_step = batch_call_in_and_dispose;
275 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
276}
277/*----------------------------------------------------------------------------*/
278/** Prepare bulk out transaction.
279 *
280 * @param[in] instance Batch structure to use.
281 *
282 * Data transaction with PID_OUT.
283 */
284void batch_bulk_out(usb_transfer_batch_t *instance)
285{
286 assert(instance);
287 instance->direction = USB_DIRECTION_OUT;
288 /* We are data out, we are supposed to provide data */
289 memcpy(instance->transport_buffer, instance->buffer,
290 instance->buffer_size);
291 batch_data(instance, USB_PID_OUT);
292 instance->next_step = batch_call_out_and_dispose;
293 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
294}
295/*----------------------------------------------------------------------------*/
296/** Prepare generic data transaction
297 *
298 * @param[in] instance Batch structure to use.
299 * @param[in] pid Pid to use for data transfers.
300 *
301 * Packets with alternating toggle bit and supplied pid value.
302 * The last transfer is marked with IOC flag.
303 */
304void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid)
305{
306 assert(instance);
307 uhci_batch_t *data = instance->private_data;
308 assert(data);
309
310 const bool low_speed = instance->speed == USB_SPEED_LOW;
311 int toggle = endpoint_toggle_get(instance->ep);
312 assert(toggle == 0 || toggle == 1);
313
314 size_t transfer = 0;
315 size_t remain_size = instance->buffer_size;
316 while (remain_size > 0) {
317 char *trans_data =
318 instance->transport_buffer + instance->buffer_size
319 - remain_size;
320
321 const size_t packet_size =
322 (instance->max_packet_size > remain_size) ?
323 remain_size : instance->max_packet_size;
324
325 td_t *next_transfer = (transfer + 1 < data->transfers)
326 ? &data->tds[transfer + 1] : NULL;
327
328 assert(transfer < data->transfers);
329 assert(packet_size <= remain_size);
330
331 td_init(
332 &data->tds[transfer], DEFAULT_ERROR_COUNT, packet_size,
333 toggle, false, low_speed, instance->target, pid, trans_data,
334 next_transfer);
335
336
337 toggle = 1 - toggle;
338 remain_size -= packet_size;
339 ++transfer;
340 }
341 td_set_ioc(&data->tds[transfer - 1]);
342 endpoint_toggle_set(instance->ep, toggle);
343}
344/*----------------------------------------------------------------------------*/
345/** Prepare generic control transaction
346 *
347 * @param[in] instance Batch structure to use.
348 * @param[in] data_stage Pid to use for data transfers.
349 * @param[in] status_stage Pid to use for data transfers.
350 *
351 * Setup stage with toggle 0 and USB_PID_SETUP.
352 * Data stage with alternating toggle and pid supplied by parameter.
353 * Status stage with toggle 1 and pid supplied by parameter.
354 * The last transfer is marked with IOC.
355 */
356void batch_control(usb_transfer_batch_t *instance,
357 usb_packet_id data_stage, usb_packet_id status_stage)
358{
359 assert(instance);
360 uhci_batch_t *data = instance->private_data;
361 assert(data);
362 assert(data->transfers >= 2);
363
364 const bool low_speed = instance->speed == USB_SPEED_LOW;
365 int toggle = 0;
366 /* setup stage */
367 td_init(
368 data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, toggle, false,
369 low_speed, instance->target, USB_PID_SETUP, instance->setup_buffer,
370 &data->tds[1]);
371
372 /* data stage */
373 size_t transfer = 1;
374 size_t remain_size = instance->buffer_size;
375 while (remain_size > 0) {
376 char *control_data =
377 instance->transport_buffer + instance->buffer_size
378 - remain_size;
379
380 toggle = 1 - toggle;
381
382 const size_t packet_size =
383 (instance->max_packet_size > remain_size) ?
384 remain_size : instance->max_packet_size;
385
386 td_init(
387 &data->tds[transfer], DEFAULT_ERROR_COUNT, packet_size,
388 toggle, false, low_speed, instance->target, data_stage,
389 control_data, &data->tds[transfer + 1]);
390
391 ++transfer;
392 assert(transfer < data->transfers);
393 assert(packet_size <= remain_size);
394 remain_size -= packet_size;
395 }
396
397 /* status stage */
398 assert(transfer == data->transfers - 1);
399
400 td_init(
401 &data->tds[transfer], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
402 instance->target, status_stage, NULL, NULL);
403 td_set_ioc(&data->tds[transfer]);
404
405 usb_log_debug2("Control last TD status: %x.\n",
406 data->tds[transfer].status);
407}
408/*----------------------------------------------------------------------------*/
409qh_t * batch_qh(usb_transfer_batch_t *instance)
410{
411 assert(instance);
412 uhci_batch_t *data = instance->private_data;
413 assert(data);
414 return data->qh;
415}
416/*----------------------------------------------------------------------------*/
417/** Helper function calls callback and correctly disposes of batch structure.
418 *
419 * @param[in] instance Batch structure to use.
420 */
421void batch_call_in_and_dispose(usb_transfer_batch_t *instance)
422{
423 assert(instance);
424 usb_transfer_batch_call_in(instance);
425 batch_dispose(instance);
426}
427/*----------------------------------------------------------------------------*/
428/** Helper function calls callback and correctly disposes of batch structure.
429 *
430 * @param[in] instance Batch structure to use.
431 */
432void batch_call_out_and_dispose(usb_transfer_batch_t *instance)
433{
434 assert(instance);
435 usb_transfer_batch_call_out(instance);
436 batch_dispose(instance);
437}
438/*----------------------------------------------------------------------------*/
439/** Correctly dispose all used data structures.
440 *
441 * @param[in] instance Batch structure to use.
442 */
443void batch_dispose(usb_transfer_batch_t *instance)
444{
445 assert(instance);
446 uhci_batch_t *data = instance->private_data;
447 assert(data);
448 usb_log_debug("Batch(%p) disposing.\n", instance);
449 /* free32 is NULL safe */
450 free32(data->tds);
451 free32(data->qh);
452 free32(instance->setup_buffer);
453 free32(instance->transport_buffer);
454 free(data);
455 free(instance);
456}
457/**
458 * @}
459 */
Note: See TracBrowser for help on using the repository browser.