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

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

Refactoring final touches

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