source: mainline/uspace/drv/bus/usb/uhci/batch.c@ 27873be

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

UHCI: rename function

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