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

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

Move scheduling to iface functions

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