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

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

Remove EP information stored in usb_transfer_batch_t

rename usb_transfer_batch_t.transport_buffer ⇒ data_buffer

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