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

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

Make batch_t a library structure

  • Property mode set to 100644
File size: 14.5 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 */
[17ceb72]28/** @addtogroup drvusbuhcihc
[4192d3d6]29 * @{
30 */
31/** @file
[17ceb72]32 * @brief UHCI driver USB transaction structure
[4192d3d6]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"
[81dce9f]44#include "uhci_struct/transfer_descriptor.h"
[4192d3d6]45
46#define DEFAULT_ERROR_COUNT 3
47
[81dce9f]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
[eae83aa]55static void batch_control(batch_t *instance,
56 usb_packet_id data_stage, usb_packet_id status_stage);
[5620bd4]57static void batch_data(batch_t *instance, usb_packet_id pid);
[83c439c]58static void batch_call_in_and_dispose(batch_t *instance);
59static void batch_call_out_and_dispose(batch_t *instance);
[4192d3d6]60
61
[17ceb72]62/** Allocate memory and initialize internal data structure.
[a7e2f0d]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.
[17ceb72]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.
[a7e2f0d]83 */
[eb1a2f4]84batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
[9a818a9]85 usb_transfer_type_t transfer_type, size_t max_packet_size,
[81dce9f]86 usb_speed_t speed, char *buffer, size_t buffer_size,
[7dd3318]87 char* setup_buffer, size_t setup_size,
[4192d3d6]88 usbhc_iface_transfer_in_callback_t func_in,
[5620bd4]89 usbhc_iface_transfer_out_callback_t func_out, void *arg,
90 device_keeper_t *manager
91 )
[4192d3d6]92{
93 assert(func_in == NULL || func_out == NULL);
94 assert(func_in != NULL || func_out != NULL);
95
[2ab6875]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
[83c439c]105 batch_t *instance = malloc(sizeof(batch_t));
[2ab6875]106 CHECK_NULL_DISPOSE_RETURN(instance,
107 "Failed to allocate batch instance.\n");
[81dce9f]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);
[7dd3318]111
112
[81dce9f]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;
[7dd3318]121 if (transfer_type == USB_TRANSFER_CONTROL) {
[81dce9f]122 data->packets += 2;
[7dd3318]123 }
124
[81dce9f]125 data->tds = malloc32(sizeof(td_t) * data->packets);
[2ab6875]126 CHECK_NULL_DISPOSE_RETURN(
[81dce9f]127 data->tds, "Failed to allocate transfer descriptors.\n");
128 bzero(data->tds, sizeof(td_t) * data->packets);
[9a818a9]129
[81dce9f]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);
[2ab6875]138 CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
139 "Failed to allocate device accessible buffer.\n");
[4192d3d6]140 }
141
[2ab6875]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");
[7dd3318]146 memcpy(instance->setup_buffer, setup_buffer, setup_size);
147 }
148
[4abc304]149 usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
150 instance, target.address, target.endpoint);
[9a818a9]151 return instance;
[4192d3d6]152}
153/*----------------------------------------------------------------------------*/
[17ceb72]154/** Check batch TDs for activity.
[a7e2f0d]155 *
156 * @param[in] instance Batch structure to use.
157 * @return False, if there is an active TD, true otherwise.
[17ceb72]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.
[a7e2f0d]162 */
[83c439c]163bool batch_is_complete(batch_t *instance)
[4192d3d6]164{
165 assert(instance);
[81dce9f]166 uhci_batch_t *data = instance->private_data;
167 assert(data);
168
[48563a3]169 usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
[81dce9f]170 instance, data->packets);
[600733e]171 instance->transfered_size = 0;
[7dd3318]172 size_t i = 0;
[81dce9f]173 for (;i < data->packets; ++i) {
174 if (td_is_active(&data->tds[i])) {
[7dd3318]175 return false;
[600733e]176 }
[c5b93dc]177
[81dce9f]178 instance->error = td_status(&data->tds[i]);
[7dd3318]179 if (instance->error != EOK) {
[48563a3]180 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
[81dce9f]181 instance, i, data->tds[i].status);
182 td_print_status(&data->tds[i]);
[a60150a]183
[81dce9f]184 device_keeper_set_toggle(data->manager,
185 instance->target, td_toggle(&data->tds[i]));
[c5b93dc]186 if (i > 0)
187 goto substract_ret;
[7dd3318]188 return true;
189 }
[c5b93dc]190
[81dce9f]191 instance->transfered_size += td_act_size(&data->tds[i]);
192 if (td_is_short(&data->tds[i]))
[c5b93dc]193 goto substract_ret;
[4192d3d6]194 }
[c5b93dc]195substract_ret:
[600733e]196 instance->transfered_size -= instance->setup_size;
[7dd3318]197 return true;
[4192d3d6]198}
199/*----------------------------------------------------------------------------*/
[a7e2f0d]200/** Prepares control write transaction.
201 *
202 * @param[in] instance Batch structure to use.
[17ceb72]203 *
204 * Uses genercir control function with pids OUT and IN.
[a7e2f0d]205 */
[83c439c]206void batch_control_write(batch_t *instance)
[4192d3d6]207{
208 assert(instance);
[17ceb72]209 /* We are data out, we are supposed to provide data */
[a7e2f0d]210 memcpy(instance->transport_buffer, instance->buffer,
211 instance->buffer_size);
[bdc8ab1]212 batch_control(instance, USB_PID_OUT, USB_PID_IN);
[83c439c]213 instance->next_step = batch_call_out_and_dispose;
[4abc304]214 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
[4192d3d6]215}
216/*----------------------------------------------------------------------------*/
[a7e2f0d]217/** Prepares control read transaction.
218 *
219 * @param[in] instance Batch structure to use.
[17ceb72]220 *
221 * Uses generic control with pids IN and OUT.
[a7e2f0d]222 */
[83c439c]223void batch_control_read(batch_t *instance)
[4192d3d6]224{
225 assert(instance);
[bdc8ab1]226 batch_control(instance, USB_PID_IN, USB_PID_OUT);
[83c439c]227 instance->next_step = batch_call_in_and_dispose;
[4abc304]228 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
[4192d3d6]229}
230/*----------------------------------------------------------------------------*/
[17ceb72]231/** Prepare interrupt in transaction.
[a7e2f0d]232 *
233 * @param[in] instance Batch structure to use.
[17ceb72]234 *
235 * Data transaction with PID_IN.
[a7e2f0d]236 */
[5620bd4]237void batch_interrupt_in(batch_t *instance)
[4192d3d6]238{
[c8dd5b1]239 assert(instance);
[5620bd4]240 batch_data(instance, USB_PID_IN);
[83c439c]241 instance->next_step = batch_call_in_and_dispose;
[4abc304]242 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
[4192d3d6]243}
244/*----------------------------------------------------------------------------*/
[17ceb72]245/** Prepare interrupt out transaction.
[a7e2f0d]246 *
247 * @param[in] instance Batch structure to use.
[17ceb72]248 *
249 * Data transaction with PID_OUT.
[a7e2f0d]250 */
[5620bd4]251void batch_interrupt_out(batch_t *instance)
[4192d3d6]252{
[c8dd5b1]253 assert(instance);
[17ceb72]254 /* We are data out, we are supposed to provide data */
[a963a68]255 memcpy(instance->transport_buffer, instance->buffer,
256 instance->buffer_size);
[5620bd4]257 batch_data(instance, USB_PID_OUT);
[0e06a14]258 instance->next_step = batch_call_out_and_dispose;
259 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
260}
261/*----------------------------------------------------------------------------*/
[17ceb72]262/** Prepare bulk in transaction.
[a7e2f0d]263 *
264 * @param[in] instance Batch structure to use.
[17ceb72]265 *
266 * Data transaction with PID_IN.
[a7e2f0d]267 */
[5620bd4]268void batch_bulk_in(batch_t *instance)
[0e06a14]269{
270 assert(instance);
[5620bd4]271 batch_data(instance, USB_PID_IN);
[0e06a14]272 instance->next_step = batch_call_in_and_dispose;
273 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
274}
275/*----------------------------------------------------------------------------*/
[17ceb72]276/** Prepare bulk out transaction.
[a7e2f0d]277 *
278 * @param[in] instance Batch structure to use.
[17ceb72]279 *
280 * Data transaction with PID_OUT.
[a7e2f0d]281 */
[5620bd4]282void batch_bulk_out(batch_t *instance)
[0e06a14]283{
284 assert(instance);
[17ceb72]285 /* We are data out, we are supposed to provide data */
[a963a68]286 memcpy(instance->transport_buffer, instance->buffer,
287 instance->buffer_size);
[5620bd4]288 batch_data(instance, USB_PID_OUT);
[0e06a14]289 instance->next_step = batch_call_out_and_dispose;
290 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
291}
292/*----------------------------------------------------------------------------*/
[17ceb72]293/** Prepare generic data transaction
[a7e2f0d]294 *
295 * @param[in] instance Batch structure to use.
296 * @param[in] pid to use for data packets.
[17ceb72]297 *
298 * Packets with alternating toggle bit and supplied pid value.
299 * The last packet is marked with IOC flag.
[a7e2f0d]300 */
[5620bd4]301void batch_data(batch_t *instance, usb_packet_id pid)
[0e06a14]302{
303 assert(instance);
[81dce9f]304 uhci_batch_t *data = instance->private_data;
305 assert(data);
306
[c3ae877]307 const bool low_speed = instance->speed == USB_SPEED_LOW;
[81dce9f]308 int toggle = device_keeper_get_toggle(data->manager, instance->target);
[3e37964]309 assert(toggle == 0 || toggle == 1);
[0e06a14]310
311 size_t packet = 0;
312 size_t remain_size = instance->buffer_size;
313 while (remain_size > 0) {
[81dce9f]314 char *trans_data =
[0e06a14]315 instance->transport_buffer + instance->buffer_size
316 - remain_size;
317
318 const size_t packet_size =
319 (instance->max_packet_size > remain_size) ?
320 remain_size : instance->max_packet_size;
[c8dd5b1]321
[81dce9f]322 td_t *next_packet = (packet + 1 < data->packets)
323 ? &data->tds[packet + 1] : NULL;
[30a4301]324
[81dce9f]325 assert(packet < data->packets);
[0e06a14]326 assert(packet_size <= remain_size);
[bcaefe3]327
328 td_init(
[81dce9f]329 &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
330 toggle, false, low_speed, instance->target, pid, trans_data,
[bcaefe3]331 next_packet);
332
333
334 toggle = 1 - toggle;
[0e06a14]335 remain_size -= packet_size;
[bcaefe3]336 ++packet;
[0e06a14]337 }
[81dce9f]338 td_set_ioc(&data->tds[packet - 1]);
339 device_keeper_set_toggle(data->manager, instance->target, toggle);
[4192d3d6]340}
341/*----------------------------------------------------------------------------*/
[17ceb72]342/** Prepare generic control transaction
[a7e2f0d]343 *
344 * @param[in] instance Batch structure to use.
345 * @param[in] data_stage to use for data packets.
346 * @param[in] status_stage to use for data packets.
[17ceb72]347 *
348 * Setup stage with toggle 0 and USB_PID_SETUP.
349 * Data stage with alternating toggle and pid supplied by parameter.
350 * Status stage with toggle 1 and pid supplied by parameter.
351 * The last packet is marked with IOC.
[a7e2f0d]352 */
[3e37964]353void batch_control(batch_t *instance,
[eae83aa]354 usb_packet_id data_stage, usb_packet_id status_stage)
[bdc8ab1]355{
356 assert(instance);
[81dce9f]357 uhci_batch_t *data = instance->private_data;
358 assert(data);
359 assert(data->packets >= 2);
[bdc8ab1]360
361 const bool low_speed = instance->speed == USB_SPEED_LOW;
362 int toggle = 0;
363 /* setup stage */
[81dce9f]364 td_init(
365 data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, toggle, false,
366 low_speed, instance->target, USB_PID_SETUP, instance->setup_buffer,
367 &data->tds[1]);
[bdc8ab1]368
369 /* data stage */
370 size_t packet = 1;
371 size_t remain_size = instance->buffer_size;
372 while (remain_size > 0) {
[81dce9f]373 char *control_data =
[bdc8ab1]374 instance->transport_buffer + instance->buffer_size
375 - remain_size;
376
377 toggle = 1 - toggle;
378
379 const size_t packet_size =
380 (instance->max_packet_size > remain_size) ?
381 remain_size : instance->max_packet_size;
382
[bcaefe3]383 td_init(
[81dce9f]384 &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
[bcaefe3]385 toggle, false, low_speed, instance->target, data_stage,
[81dce9f]386 control_data, &data->tds[packet + 1]);
[bdc8ab1]387
388 ++packet;
[81dce9f]389 assert(packet < data->packets);
[bdc8ab1]390 assert(packet_size <= remain_size);
391 remain_size -= packet_size;
392 }
393
394 /* status stage */
[81dce9f]395 assert(packet == data->packets - 1);
[4192d3d6]396
[81dce9f]397 td_init(
398 &data->tds[packet], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
399 instance->target, status_stage, NULL, NULL);
400 td_set_ioc(&data->tds[packet]);
[7dd3318]401
[81dce9f]402 usb_log_debug2("Control last TD status: %x.\n",
403 data->tds[packet].status);
[4192d3d6]404}
405/*----------------------------------------------------------------------------*/
[81dce9f]406qh_t * batch_qh(batch_t *instance)
[4192d3d6]407{
408 assert(instance);
[81dce9f]409 uhci_batch_t *data = instance->private_data;
410 assert(data);
411 return data->qh;
[4192d3d6]412}
413/*----------------------------------------------------------------------------*/
[17ceb72]414/** Helper function calls callback and correctly disposes of batch structure.
[a7e2f0d]415 *
416 * @param[in] instance Batch structure to use.
417 */
[83c439c]418void batch_call_in_and_dispose(batch_t *instance)
[4192d3d6]419{
420 assert(instance);
[83c439c]421 batch_call_in(instance);
[a60150a]422 batch_dispose(instance);
[4192d3d6]423}
424/*----------------------------------------------------------------------------*/
[17ceb72]425/** Helper function calls callback and correctly disposes of batch structure.
[a7e2f0d]426 *
427 * @param[in] instance Batch structure to use.
428 */
[83c439c]429void batch_call_out_and_dispose(batch_t *instance)
[4192d3d6]430{
431 assert(instance);
[83c439c]432 batch_call_out(instance);
[a60150a]433 batch_dispose(instance);
434}
435/*----------------------------------------------------------------------------*/
[17ceb72]436/** Correctly dispose all used data structures.
[a7e2f0d]437 *
438 * @param[in] instance Batch structure to use.
439 */
[a60150a]440void batch_dispose(batch_t *instance)
441{
442 assert(instance);
[81dce9f]443 uhci_batch_t *data = instance->private_data;
444 assert(data);
[48563a3]445 usb_log_debug("Batch(%p) disposing.\n", instance);
[bcaefe3]446 /* free32 is NULL safe */
[81dce9f]447 free32(data->tds);
448 free32(data->qh);
[7dd3318]449 free32(instance->setup_buffer);
450 free32(instance->transport_buffer);
[81dce9f]451 free(data);
[4192d3d6]452 free(instance);
453}
454/**
455 * @}
456 */
Note: See TracBrowser for help on using the repository browser.