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

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

Rename packet ⇒ transfer, avoids naming confusion

  • Property mode set to 100644
File size: 15.1 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"
[87644b4]42#include "hw_struct/transfer_descriptor.h"
[9a818a9]43#include "utils/malloc32.h"
[4192d3d6]44
45#define DEFAULT_ERROR_COUNT 3
46
[81dce9f]47typedef struct uhci_batch {
48 qh_t *qh;
49 td_t *tds;
[20a1e76]50 size_t transfers;
[68b5ed6e]51 usb_device_keeper_t *manager;
[81dce9f]52} uhci_batch_t;
53
[1387692]54static void batch_control(usb_transfer_batch_t *instance,
[eae83aa]55 usb_packet_id data_stage, usb_packet_id status_stage);
[1387692]56static void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid);
57static void batch_call_in_and_dispose(usb_transfer_batch_t *instance);
58static void batch_call_out_and_dispose(usb_transfer_batch_t *instance);
[4192d3d6]59
60
[17ceb72]61/** Allocate memory and initialize internal data structure.
[a7e2f0d]62 *
63 * @param[in] fun DDF function to pass to callback.
64 * @param[in] target Device and endpoint target of the transaction.
65 * @param[in] transfer_type Interrupt, Control or Bulk.
[20a1e76]66 * @param[in] max_packet_size maximum allowed size of data transfers.
[a7e2f0d]67 * @param[in] speed Speed of the transaction.
68 * @param[in] buffer Data source/destination.
69 * @param[in] size Size of the buffer.
70 * @param[in] setup_buffer Setup data source (if not NULL)
71 * @param[in] setup_size Size of setup_buffer (should be always 8)
72 * @param[in] func_in function to call on inbound transaction completion
73 * @param[in] func_out function to call on outbound transaction completion
74 * @param[in] arg additional parameter to func_in or func_out
75 * @param[in] manager Pointer to toggle management structure.
[17ceb72]76 * @return Valid pointer if all substructures were successfully created,
77 * NULL otherwise.
78 *
[20a1e76]79 * Determines the number of needed transfers (TDs). Prepares a transport buffer
[17ceb72]80 * (that is accessible by the hardware). Initializes parameters needed for the
81 * transaction and callback.
[a7e2f0d]82 */
[1387692]83usb_transfer_batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
[9a818a9]84 usb_transfer_type_t transfer_type, size_t max_packet_size,
[81dce9f]85 usb_speed_t speed, char *buffer, size_t buffer_size,
[7dd3318]86 char* setup_buffer, size_t setup_size,
[4192d3d6]87 usbhc_iface_transfer_in_callback_t func_in,
[5620bd4]88 usbhc_iface_transfer_out_callback_t func_out, void *arg,
[68b5ed6e]89 usb_device_keeper_t *manager
[5620bd4]90 )
[4192d3d6]91{
92 assert(func_in == NULL || func_out == NULL);
93 assert(func_in != NULL || func_out != NULL);
94
[2ab6875]95#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
96 if (ptr == NULL) { \
97 usb_log_error(message); \
98 if (instance) { \
99 batch_dispose(instance); \
100 } \
101 return NULL; \
102 } else (void)0
103
[1387692]104 usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
[2ab6875]105 CHECK_NULL_DISPOSE_RETURN(instance,
106 "Failed to allocate batch instance.\n");
[1387692]107 usb_transfer_batch_init(instance, target, transfer_type, speed, max_packet_size,
[81dce9f]108 buffer, NULL, buffer_size, NULL, setup_size, func_in,
109 func_out, arg, fun, NULL);
[7dd3318]110
111
[81dce9f]112 uhci_batch_t *data = malloc(sizeof(uhci_batch_t));
113 CHECK_NULL_DISPOSE_RETURN(instance,
114 "Failed to allocate batch instance.\n");
115 bzero(data, sizeof(uhci_batch_t));
116 data->manager = manager;
117 instance->private_data = data;
118
[20a1e76]119 data->transfers = (buffer_size + max_packet_size - 1) / max_packet_size;
[7dd3318]120 if (transfer_type == USB_TRANSFER_CONTROL) {
[20a1e76]121 data->transfers += 2;
[7dd3318]122 }
123
[20a1e76]124 data->tds = malloc32(sizeof(td_t) * data->transfers);
[2ab6875]125 CHECK_NULL_DISPOSE_RETURN(
[81dce9f]126 data->tds, "Failed to allocate transfer descriptors.\n");
[20a1e76]127 bzero(data->tds, sizeof(td_t) * data->transfers);
[9a818a9]128
[81dce9f]129 data->qh = malloc32(sizeof(qh_t));
130 CHECK_NULL_DISPOSE_RETURN(data->qh,
131 "Failed to allocate batch queue head.\n");
132 qh_init(data->qh);
133 qh_set_element_td(data->qh, addr_to_phys(data->tds));
134
135 if (buffer_size > 0) {
136 instance->transport_buffer = malloc32(buffer_size);
[2ab6875]137 CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
138 "Failed to allocate device accessible buffer.\n");
[4192d3d6]139 }
140
[2ab6875]141 if (setup_size > 0) {
142 instance->setup_buffer = malloc32(setup_size);
143 CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
144 "Failed to allocate device accessible setup buffer.\n");
[7dd3318]145 memcpy(instance->setup_buffer, setup_buffer, setup_size);
146 }
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/*----------------------------------------------------------------------------*/
[17ceb72]153/** Check batch TDs for activity.
[a7e2f0d]154 *
155 * @param[in] instance Batch structure to use.
156 * @return False, if there is an active TD, true otherwise.
[17ceb72]157 *
158 * Walk all TDs. Stop with false if there is an active one (it is to be
159 * processed). Stop with true if an error is found. Return true if the last TS
160 * is reached.
[a7e2f0d]161 */
[1387692]162bool batch_is_complete(usb_transfer_batch_t *instance)
[4192d3d6]163{
164 assert(instance);
[81dce9f]165 uhci_batch_t *data = instance->private_data;
166 assert(data);
167
[20a1e76]168 usb_log_debug2("Batch(%p) checking %d transfer(s) for completion.\n",
169 instance, data->transfers);
[600733e]170 instance->transfered_size = 0;
[7dd3318]171 size_t i = 0;
[20a1e76]172 for (;i < data->transfers; ++i) {
[81dce9f]173 if (td_is_active(&data->tds[i])) {
[7dd3318]174 return false;
[600733e]175 }
[c5b93dc]176
[81dce9f]177 instance->error = td_status(&data->tds[i]);
[7dd3318]178 if (instance->error != EOK) {
[48563a3]179 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
[81dce9f]180 instance, i, data->tds[i].status);
181 td_print_status(&data->tds[i]);
[a60150a]182
[68b5ed6e]183 usb_device_keeper_set_toggle(data->manager,
[c15070c]184 instance->target, instance->direction,
185 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 */
[1387692]206void batch_control_write(usb_transfer_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 */
[1387692]223void batch_control_read(usb_transfer_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 */
[1387692]237void batch_interrupt_in(usb_transfer_batch_t *instance)
[4192d3d6]238{
[c8dd5b1]239 assert(instance);
[c15070c]240 instance->direction = USB_DIRECTION_IN;
[5620bd4]241 batch_data(instance, USB_PID_IN);
[83c439c]242 instance->next_step = batch_call_in_and_dispose;
[4abc304]243 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
[4192d3d6]244}
245/*----------------------------------------------------------------------------*/
[17ceb72]246/** Prepare interrupt out transaction.
[a7e2f0d]247 *
248 * @param[in] instance Batch structure to use.
[17ceb72]249 *
250 * Data transaction with PID_OUT.
[a7e2f0d]251 */
[1387692]252void batch_interrupt_out(usb_transfer_batch_t *instance)
[4192d3d6]253{
[c8dd5b1]254 assert(instance);
[c15070c]255 instance->direction = USB_DIRECTION_OUT;
[17ceb72]256 /* We are data out, we are supposed to provide data */
[a963a68]257 memcpy(instance->transport_buffer, instance->buffer,
258 instance->buffer_size);
[5620bd4]259 batch_data(instance, USB_PID_OUT);
[0e06a14]260 instance->next_step = batch_call_out_and_dispose;
261 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
262}
263/*----------------------------------------------------------------------------*/
[17ceb72]264/** Prepare bulk in transaction.
[a7e2f0d]265 *
266 * @param[in] instance Batch structure to use.
[17ceb72]267 *
268 * Data transaction with PID_IN.
[a7e2f0d]269 */
[1387692]270void batch_bulk_in(usb_transfer_batch_t *instance)
[0e06a14]271{
272 assert(instance);
[5620bd4]273 batch_data(instance, USB_PID_IN);
[c15070c]274 instance->direction = USB_DIRECTION_IN;
[0e06a14]275 instance->next_step = batch_call_in_and_dispose;
276 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
277}
278/*----------------------------------------------------------------------------*/
[17ceb72]279/** Prepare bulk out transaction.
[a7e2f0d]280 *
281 * @param[in] instance Batch structure to use.
[17ceb72]282 *
283 * Data transaction with PID_OUT.
[a7e2f0d]284 */
[1387692]285void batch_bulk_out(usb_transfer_batch_t *instance)
[0e06a14]286{
287 assert(instance);
[c15070c]288 instance->direction = USB_DIRECTION_OUT;
[17ceb72]289 /* We are data out, we are supposed to provide data */
[a963a68]290 memcpy(instance->transport_buffer, instance->buffer,
291 instance->buffer_size);
[5620bd4]292 batch_data(instance, USB_PID_OUT);
[0e06a14]293 instance->next_step = batch_call_out_and_dispose;
294 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
295}
296/*----------------------------------------------------------------------------*/
[17ceb72]297/** Prepare generic data transaction
[a7e2f0d]298 *
299 * @param[in] instance Batch structure to use.
[20a1e76]300 * @param[in] pid Pid to use for data transfers.
[17ceb72]301 *
302 * Packets with alternating toggle bit and supplied pid value.
[20a1e76]303 * The last transfer is marked with IOC flag.
[a7e2f0d]304 */
[1387692]305void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid)
[0e06a14]306{
307 assert(instance);
[81dce9f]308 uhci_batch_t *data = instance->private_data;
309 assert(data);
310
[c3ae877]311 const bool low_speed = instance->speed == USB_SPEED_LOW;
[68b5ed6e]312 int toggle = usb_device_keeper_get_toggle(
[c15070c]313 data->manager, instance->target, instance->direction);
[3e37964]314 assert(toggle == 0 || toggle == 1);
[0e06a14]315
[20a1e76]316 size_t transfer = 0;
[0e06a14]317 size_t remain_size = instance->buffer_size;
318 while (remain_size > 0) {
[81dce9f]319 char *trans_data =
[0e06a14]320 instance->transport_buffer + instance->buffer_size
321 - remain_size;
322
323 const size_t packet_size =
324 (instance->max_packet_size > remain_size) ?
325 remain_size : instance->max_packet_size;
[c8dd5b1]326
[20a1e76]327 td_t *next_transfer = (transfer + 1 < data->transfers)
328 ? &data->tds[transfer + 1] : NULL;
[30a4301]329
[20a1e76]330 assert(transfer < data->transfers);
[0e06a14]331 assert(packet_size <= remain_size);
[bcaefe3]332
333 td_init(
[20a1e76]334 &data->tds[transfer], DEFAULT_ERROR_COUNT, packet_size,
[81dce9f]335 toggle, false, low_speed, instance->target, pid, trans_data,
[20a1e76]336 next_transfer);
[bcaefe3]337
338
339 toggle = 1 - toggle;
[0e06a14]340 remain_size -= packet_size;
[20a1e76]341 ++transfer;
[0e06a14]342 }
[20a1e76]343 td_set_ioc(&data->tds[transfer - 1]);
[68b5ed6e]344 usb_device_keeper_set_toggle(data->manager, instance->target,
[c15070c]345 instance->direction, toggle);
[4192d3d6]346}
347/*----------------------------------------------------------------------------*/
[17ceb72]348/** Prepare generic control transaction
[a7e2f0d]349 *
350 * @param[in] instance Batch structure to use.
[20a1e76]351 * @param[in] data_stage Pid to use for data transfers.
352 * @param[in] status_stage Pid to use for data transfers.
[17ceb72]353 *
354 * Setup stage with toggle 0 and USB_PID_SETUP.
355 * Data stage with alternating toggle and pid supplied by parameter.
356 * Status stage with toggle 1 and pid supplied by parameter.
[20a1e76]357 * The last transfer is marked with IOC.
[a7e2f0d]358 */
[1387692]359void batch_control(usb_transfer_batch_t *instance,
[eae83aa]360 usb_packet_id data_stage, usb_packet_id status_stage)
[bdc8ab1]361{
362 assert(instance);
[81dce9f]363 uhci_batch_t *data = instance->private_data;
364 assert(data);
[20a1e76]365 assert(data->transfers >= 2);
[bdc8ab1]366
367 const bool low_speed = instance->speed == USB_SPEED_LOW;
368 int toggle = 0;
369 /* setup stage */
[81dce9f]370 td_init(
371 data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, toggle, false,
372 low_speed, instance->target, USB_PID_SETUP, instance->setup_buffer,
373 &data->tds[1]);
[bdc8ab1]374
375 /* data stage */
[20a1e76]376 size_t transfer = 1;
[bdc8ab1]377 size_t remain_size = instance->buffer_size;
378 while (remain_size > 0) {
[81dce9f]379 char *control_data =
[bdc8ab1]380 instance->transport_buffer + instance->buffer_size
381 - remain_size;
382
383 toggle = 1 - toggle;
384
385 const size_t packet_size =
386 (instance->max_packet_size > remain_size) ?
387 remain_size : instance->max_packet_size;
388
[bcaefe3]389 td_init(
[20a1e76]390 &data->tds[transfer], DEFAULT_ERROR_COUNT, packet_size,
[bcaefe3]391 toggle, false, low_speed, instance->target, data_stage,
[20a1e76]392 control_data, &data->tds[transfer + 1]);
[bdc8ab1]393
[20a1e76]394 ++transfer;
395 assert(transfer < data->transfers);
[bdc8ab1]396 assert(packet_size <= remain_size);
397 remain_size -= packet_size;
398 }
399
400 /* status stage */
[20a1e76]401 assert(transfer == data->transfers - 1);
[4192d3d6]402
[81dce9f]403 td_init(
[20a1e76]404 &data->tds[transfer], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
[81dce9f]405 instance->target, status_stage, NULL, NULL);
[20a1e76]406 td_set_ioc(&data->tds[transfer]);
[7dd3318]407
[81dce9f]408 usb_log_debug2("Control last TD status: %x.\n",
[20a1e76]409 data->tds[transfer].status);
[4192d3d6]410}
411/*----------------------------------------------------------------------------*/
[1387692]412qh_t * batch_qh(usb_transfer_batch_t *instance)
[4192d3d6]413{
414 assert(instance);
[81dce9f]415 uhci_batch_t *data = instance->private_data;
416 assert(data);
417 return data->qh;
[4192d3d6]418}
419/*----------------------------------------------------------------------------*/
[17ceb72]420/** Helper function calls callback and correctly disposes of batch structure.
[a7e2f0d]421 *
422 * @param[in] instance Batch structure to use.
423 */
[1387692]424void batch_call_in_and_dispose(usb_transfer_batch_t *instance)
[4192d3d6]425{
426 assert(instance);
[1387692]427 usb_transfer_batch_call_in(instance);
[a60150a]428 batch_dispose(instance);
[4192d3d6]429}
430/*----------------------------------------------------------------------------*/
[17ceb72]431/** Helper function calls callback and correctly disposes of batch structure.
[a7e2f0d]432 *
433 * @param[in] instance Batch structure to use.
434 */
[1387692]435void batch_call_out_and_dispose(usb_transfer_batch_t *instance)
[4192d3d6]436{
437 assert(instance);
[1387692]438 usb_transfer_batch_call_out(instance);
[a60150a]439 batch_dispose(instance);
440}
441/*----------------------------------------------------------------------------*/
[17ceb72]442/** Correctly dispose all used data structures.
[a7e2f0d]443 *
444 * @param[in] instance Batch structure to use.
445 */
[1387692]446void batch_dispose(usb_transfer_batch_t *instance)
[a60150a]447{
448 assert(instance);
[81dce9f]449 uhci_batch_t *data = instance->private_data;
450 assert(data);
[48563a3]451 usb_log_debug("Batch(%p) disposing.\n", instance);
[bcaefe3]452 /* free32 is NULL safe */
[81dce9f]453 free32(data->tds);
454 free32(data->qh);
[7dd3318]455 free32(instance->setup_buffer);
456 free32(instance->transport_buffer);
[81dce9f]457 free(data);
[4192d3d6]458 free(instance);
459}
460/**
461 * @}
462 */
Note: See TracBrowser for help on using the repository browser.