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

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

Doxygen and some minor code-style changes

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