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

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

Fix toggle protocol, add support for all 32 endpoints

  • 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 "uhci_hc.h"
43#include "utils/malloc32.h"
44#include "uhci_struct/transfer_descriptor.h"
45
46#define DEFAULT_ERROR_COUNT 3
47
48typedef 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
55static void batch_control(batch_t *instance,
56 usb_packet_id data_stage, usb_packet_id status_stage);
57static void batch_data(batch_t *instance, usb_packet_id pid);
58static void batch_call_in_and_dispose(batch_t *instance);
59static 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 */
84batch_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 */
163bool 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, instance->direction,
186 td_toggle(&data->tds[i]));
187 if (i > 0)
188 goto substract_ret;
189 return true;
190 }
191
192 instance->transfered_size += td_act_size(&data->tds[i]);
193 if (td_is_short(&data->tds[i]))
194 goto substract_ret;
195 }
196substract_ret:
197 instance->transfered_size -= instance->setup_size;
198 return true;
199}
200/*----------------------------------------------------------------------------*/
201/** Prepares control write transaction.
202 *
203 * @param[in] instance Batch structure to use.
204 *
205 * Uses genercir control function with pids OUT and IN.
206 */
207void batch_control_write(batch_t *instance)
208{
209 assert(instance);
210 /* We are data out, we are supposed to provide data */
211 memcpy(instance->transport_buffer, instance->buffer,
212 instance->buffer_size);
213 batch_control(instance, USB_PID_OUT, USB_PID_IN);
214 instance->next_step = batch_call_out_and_dispose;
215 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
216}
217/*----------------------------------------------------------------------------*/
218/** Prepares control read transaction.
219 *
220 * @param[in] instance Batch structure to use.
221 *
222 * Uses generic control with pids IN and OUT.
223 */
224void batch_control_read(batch_t *instance)
225{
226 assert(instance);
227 batch_control(instance, USB_PID_IN, USB_PID_OUT);
228 instance->next_step = batch_call_in_and_dispose;
229 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
230}
231/*----------------------------------------------------------------------------*/
232/** Prepare interrupt in transaction.
233 *
234 * @param[in] instance Batch structure to use.
235 *
236 * Data transaction with PID_IN.
237 */
238void batch_interrupt_in(batch_t *instance)
239{
240 assert(instance);
241 instance->direction = USB_DIRECTION_IN;
242 batch_data(instance, USB_PID_IN);
243 instance->next_step = batch_call_in_and_dispose;
244 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
245}
246/*----------------------------------------------------------------------------*/
247/** Prepare interrupt out transaction.
248 *
249 * @param[in] instance Batch structure to use.
250 *
251 * Data transaction with PID_OUT.
252 */
253void batch_interrupt_out(batch_t *instance)
254{
255 assert(instance);
256 instance->direction = USB_DIRECTION_OUT;
257 /* We are data out, we are supposed to provide data */
258 memcpy(instance->transport_buffer, instance->buffer,
259 instance->buffer_size);
260 batch_data(instance, USB_PID_OUT);
261 instance->next_step = batch_call_out_and_dispose;
262 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
263}
264/*----------------------------------------------------------------------------*/
265/** Prepare bulk in transaction.
266 *
267 * @param[in] instance Batch structure to use.
268 *
269 * Data transaction with PID_IN.
270 */
271void batch_bulk_in(batch_t *instance)
272{
273 assert(instance);
274 batch_data(instance, USB_PID_IN);
275 instance->direction = USB_DIRECTION_IN;
276 instance->next_step = batch_call_in_and_dispose;
277 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
278}
279/*----------------------------------------------------------------------------*/
280/** Prepare bulk out transaction.
281 *
282 * @param[in] instance Batch structure to use.
283 *
284 * Data transaction with PID_OUT.
285 */
286void batch_bulk_out(batch_t *instance)
287{
288 assert(instance);
289 instance->direction = USB_DIRECTION_OUT;
290 /* We are data out, we are supposed to provide data */
291 memcpy(instance->transport_buffer, instance->buffer,
292 instance->buffer_size);
293 batch_data(instance, USB_PID_OUT);
294 instance->next_step = batch_call_out_and_dispose;
295 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
296}
297/*----------------------------------------------------------------------------*/
298/** Prepare generic data transaction
299 *
300 * @param[in] instance Batch structure to use.
301 * @param[in] pid to use for data packets.
302 *
303 * Packets with alternating toggle bit and supplied pid value.
304 * The last packet is marked with IOC flag.
305 */
306void batch_data(batch_t *instance, usb_packet_id pid)
307{
308 assert(instance);
309 uhci_batch_t *data = instance->private_data;
310 assert(data);
311
312 const bool low_speed = instance->speed == USB_SPEED_LOW;
313 int toggle = device_keeper_get_toggle(
314 data->manager, instance->target, instance->direction);
315 assert(toggle == 0 || toggle == 1);
316
317 size_t packet = 0;
318 size_t remain_size = instance->buffer_size;
319 while (remain_size > 0) {
320 char *trans_data =
321 instance->transport_buffer + instance->buffer_size
322 - remain_size;
323
324 const size_t packet_size =
325 (instance->max_packet_size > remain_size) ?
326 remain_size : instance->max_packet_size;
327
328 td_t *next_packet = (packet + 1 < data->packets)
329 ? &data->tds[packet + 1] : NULL;
330
331 assert(packet < data->packets);
332 assert(packet_size <= remain_size);
333
334 td_init(
335 &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
336 toggle, false, low_speed, instance->target, pid, trans_data,
337 next_packet);
338
339
340 toggle = 1 - toggle;
341 remain_size -= packet_size;
342 ++packet;
343 }
344 td_set_ioc(&data->tds[packet - 1]);
345 device_keeper_set_toggle(data->manager, instance->target,
346 instance->direction, toggle);
347}
348/*----------------------------------------------------------------------------*/
349/** Prepare generic control transaction
350 *
351 * @param[in] instance Batch structure to use.
352 * @param[in] data_stage to use for data packets.
353 * @param[in] status_stage to use for data packets.
354 *
355 * Setup stage with toggle 0 and USB_PID_SETUP.
356 * Data stage with alternating toggle and pid supplied by parameter.
357 * Status stage with toggle 1 and pid supplied by parameter.
358 * The last packet is marked with IOC.
359 */
360void batch_control(batch_t *instance,
361 usb_packet_id data_stage, usb_packet_id status_stage)
362{
363 assert(instance);
364 uhci_batch_t *data = instance->private_data;
365 assert(data);
366 assert(data->packets >= 2);
367
368 const bool low_speed = instance->speed == USB_SPEED_LOW;
369 int toggle = 0;
370 /* setup stage */
371 td_init(
372 data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, toggle, false,
373 low_speed, instance->target, USB_PID_SETUP, instance->setup_buffer,
374 &data->tds[1]);
375
376 /* data stage */
377 size_t packet = 1;
378 size_t remain_size = instance->buffer_size;
379 while (remain_size > 0) {
380 char *control_data =
381 instance->transport_buffer + instance->buffer_size
382 - remain_size;
383
384 toggle = 1 - toggle;
385
386 const size_t packet_size =
387 (instance->max_packet_size > remain_size) ?
388 remain_size : instance->max_packet_size;
389
390 td_init(
391 &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
392 toggle, false, low_speed, instance->target, data_stage,
393 control_data, &data->tds[packet + 1]);
394
395 ++packet;
396 assert(packet < data->packets);
397 assert(packet_size <= remain_size);
398 remain_size -= packet_size;
399 }
400
401 /* status stage */
402 assert(packet == data->packets - 1);
403
404 td_init(
405 &data->tds[packet], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
406 instance->target, status_stage, NULL, NULL);
407 td_set_ioc(&data->tds[packet]);
408
409 usb_log_debug2("Control last TD status: %x.\n",
410 data->tds[packet].status);
411}
412/*----------------------------------------------------------------------------*/
413qh_t * batch_qh(batch_t *instance)
414{
415 assert(instance);
416 uhci_batch_t *data = instance->private_data;
417 assert(data);
418 return data->qh;
419}
420/*----------------------------------------------------------------------------*/
421/** Helper function calls callback and correctly disposes of batch structure.
422 *
423 * @param[in] instance Batch structure to use.
424 */
425void batch_call_in_and_dispose(batch_t *instance)
426{
427 assert(instance);
428 batch_call_in(instance);
429 batch_dispose(instance);
430}
431/*----------------------------------------------------------------------------*/
432/** Helper function calls callback and correctly disposes of batch structure.
433 *
434 * @param[in] instance Batch structure to use.
435 */
436void batch_call_out_and_dispose(batch_t *instance)
437{
438 assert(instance);
439 batch_call_out(instance);
440 batch_dispose(instance);
441}
442/*----------------------------------------------------------------------------*/
443/** Correctly dispose all used data structures.
444 *
445 * @param[in] instance Batch structure to use.
446 */
447void batch_dispose(batch_t *instance)
448{
449 assert(instance);
450 uhci_batch_t *data = instance->private_data;
451 assert(data);
452 usb_log_debug("Batch(%p) disposing.\n", instance);
453 /* free32 is NULL safe */
454 free32(data->tds);
455 free32(data->qh);
456 free32(instance->setup_buffer);
457 free32(instance->transport_buffer);
458 free(data);
459 free(instance);
460}
461/**
462 * @}
463 */
Note: See TracBrowser for help on using the repository browser.