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

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

Only check connections of there is a chance for a finished one

Set IOC on the last packet of data transfer

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