source: mainline/uspace/drv/bus/usb/uhci/batch.c@ 89d3f3c7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 89d3f3c7 was 5203e256, checked in by Martin Decky <martin@…>, 14 years ago

keep the drivers source tree tidy by using logical subdirectories

  • 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_transfer_batch_init(instance, ep, buffer, data_buffer, buffer_size,
150 setup, setup_size, func_in, func_out, arg, fun,
151 uhci_data, uhci_transfer_batch_dispose);
[7dd3318]152
[2cc6e97]153 memcpy(instance->setup_buffer, setup_buffer, setup_size);
[c4fb5ecd]154 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
155 " memory structures ready.\n", instance,
156 USB_TRANSFER_BATCH_ARGS(*instance));
[9a818a9]157 return instance;
[4192d3d6]158}
159/*----------------------------------------------------------------------------*/
[17ceb72]160/** Check batch TDs for activity.
[a7e2f0d]161 *
162 * @param[in] instance Batch structure to use.
163 * @return False, if there is an active TD, true otherwise.
[17ceb72]164 *
165 * Walk all TDs. Stop with false if there is an active one (it is to be
[910ca3f]166 * processed). Stop with true if an error is found. Return true if the last TD
[17ceb72]167 * is reached.
[a7e2f0d]168 */
[1387692]169bool batch_is_complete(usb_transfer_batch_t *instance)
[4192d3d6]170{
171 assert(instance);
[2cc6e97]172 uhci_transfer_batch_t *data = instance->private_data;
[81dce9f]173 assert(data);
174
[4125b7d]175 usb_log_debug2("Batch(%p) checking %zu transfer(s) for completion.\n",
[508a0ca]176 instance, data->td_count);
[600733e]177 instance->transfered_size = 0;
[7dd3318]178 size_t i = 0;
[508a0ca]179 for (;i < data->td_count; ++i) {
[81dce9f]180 if (td_is_active(&data->tds[i])) {
[7dd3318]181 return false;
[600733e]182 }
[c5b93dc]183
[81dce9f]184 instance->error = td_status(&data->tds[i]);
[7dd3318]185 if (instance->error != EOK) {
[28d9c95]186 usb_log_debug("Batch(%p) found error TD(%zu):%"
187 PRIx32 ".\n", instance, i, data->tds[i].status);
[81dce9f]188 td_print_status(&data->tds[i]);
[feb10c88]189
[910ca3f]190 assert(instance->ep != NULL);
[feb10c88]191 endpoint_toggle_set(instance->ep,
192 td_toggle(&data->tds[i]));
[c5b93dc]193 if (i > 0)
194 goto substract_ret;
[7dd3318]195 return true;
196 }
[c5b93dc]197
[81dce9f]198 instance->transfered_size += td_act_size(&data->tds[i]);
199 if (td_is_short(&data->tds[i]))
[c5b93dc]200 goto substract_ret;
[4192d3d6]201 }
[c5b93dc]202substract_ret:
[600733e]203 instance->transfered_size -= instance->setup_size;
[7dd3318]204 return true;
[4192d3d6]205}
[c4fb5ecd]206
207#define LOG_BATCH_INITIALIZED(batch, name) \
208 usb_log_debug2("Batch %p %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
209 (batch), (name), USB_TRANSFER_BATCH_ARGS(*(batch)))
210
[4192d3d6]211/*----------------------------------------------------------------------------*/
[910ca3f]212/** Prepares control write transfer.
[a7e2f0d]213 *
214 * @param[in] instance Batch structure to use.
[17ceb72]215 *
[910ca3f]216 * Uses generic control function with pids OUT and IN.
[a7e2f0d]217 */
[1387692]218void batch_control_write(usb_transfer_batch_t *instance)
[4192d3d6]219{
220 assert(instance);
[17ceb72]221 /* We are data out, we are supposed to provide data */
[d017cea]222 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
[bdc8ab1]223 batch_control(instance, USB_PID_OUT, USB_PID_IN);
[2cc6e97]224 instance->next_step = usb_transfer_batch_call_out_and_dispose;
[c4fb5ecd]225 LOG_BATCH_INITIALIZED(instance, "control write");
[4192d3d6]226}
227/*----------------------------------------------------------------------------*/
[910ca3f]228/** Prepares control read transfer.
[a7e2f0d]229 *
230 * @param[in] instance Batch structure to use.
[17ceb72]231 *
232 * Uses generic control with pids IN and OUT.
[a7e2f0d]233 */
[1387692]234void batch_control_read(usb_transfer_batch_t *instance)
[4192d3d6]235{
236 assert(instance);
[bdc8ab1]237 batch_control(instance, USB_PID_IN, USB_PID_OUT);
[2cc6e97]238 instance->next_step = usb_transfer_batch_call_in_and_dispose;
[c4fb5ecd]239 LOG_BATCH_INITIALIZED(instance, "control read");
[4192d3d6]240}
241/*----------------------------------------------------------------------------*/
[910ca3f]242/** Prepare interrupt in transfer.
[a7e2f0d]243 *
244 * @param[in] instance Batch structure to use.
[17ceb72]245 *
[910ca3f]246 * Data transfer with PID_IN.
[a7e2f0d]247 */
[1387692]248void batch_interrupt_in(usb_transfer_batch_t *instance)
[4192d3d6]249{
[c8dd5b1]250 assert(instance);
[5620bd4]251 batch_data(instance, USB_PID_IN);
[2cc6e97]252 instance->next_step = usb_transfer_batch_call_in_and_dispose;
[c4fb5ecd]253 LOG_BATCH_INITIALIZED(instance, "interrupt in");
[4192d3d6]254}
255/*----------------------------------------------------------------------------*/
[910ca3f]256/** Prepare interrupt out transfer.
[a7e2f0d]257 *
258 * @param[in] instance Batch structure to use.
[17ceb72]259 *
[910ca3f]260 * Data transfer with PID_OUT.
[a7e2f0d]261 */
[1387692]262void batch_interrupt_out(usb_transfer_batch_t *instance)
[4192d3d6]263{
[c8dd5b1]264 assert(instance);
[17ceb72]265 /* We are data out, we are supposed to provide data */
[d017cea]266 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
[5620bd4]267 batch_data(instance, USB_PID_OUT);
[2cc6e97]268 instance->next_step = usb_transfer_batch_call_out_and_dispose;
[c4fb5ecd]269 LOG_BATCH_INITIALIZED(instance, "interrupt out");
[0e06a14]270}
271/*----------------------------------------------------------------------------*/
[910ca3f]272/** Prepare bulk in transfer.
[a7e2f0d]273 *
274 * @param[in] instance Batch structure to use.
[17ceb72]275 *
[910ca3f]276 * Data transfer with PID_IN.
[a7e2f0d]277 */
[1387692]278void batch_bulk_in(usb_transfer_batch_t *instance)
[0e06a14]279{
280 assert(instance);
[5620bd4]281 batch_data(instance, USB_PID_IN);
[2cc6e97]282 instance->next_step = usb_transfer_batch_call_in_and_dispose;
[c4fb5ecd]283 LOG_BATCH_INITIALIZED(instance, "bulk in");
[0e06a14]284}
285/*----------------------------------------------------------------------------*/
[910ca3f]286/** Prepare bulk out transfer.
[a7e2f0d]287 *
288 * @param[in] instance Batch structure to use.
[17ceb72]289 *
[910ca3f]290 * Data transfer with PID_OUT.
[a7e2f0d]291 */
[1387692]292void batch_bulk_out(usb_transfer_batch_t *instance)
[0e06a14]293{
294 assert(instance);
[17ceb72]295 /* We are data out, we are supposed to provide data */
[910ca3f]296 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
[5620bd4]297 batch_data(instance, USB_PID_OUT);
[2cc6e97]298 instance->next_step = usb_transfer_batch_call_out_and_dispose;
[c4fb5ecd]299 LOG_BATCH_INITIALIZED(instance, "bulk out");
[0e06a14]300}
301/*----------------------------------------------------------------------------*/
[910ca3f]302/** Prepare generic data transfer
[a7e2f0d]303 *
304 * @param[in] instance Batch structure to use.
[508a0ca]305 * @param[in] pid Pid to use for data transactions.
[17ceb72]306 *
[910ca3f]307 * Transactions with alternating toggle bit and supplied pid value.
[20a1e76]308 * The last transfer is marked with IOC flag.
[a7e2f0d]309 */
[1387692]310void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid)
[0e06a14]311{
312 assert(instance);
[2cc6e97]313 uhci_transfer_batch_t *data = instance->private_data;
[81dce9f]314 assert(data);
315
[d017cea]316 const bool low_speed = instance->ep->speed == USB_SPEED_LOW;
[f567bcf]317 int toggle = endpoint_toggle_get(instance->ep);
[3e37964]318 assert(toggle == 0 || toggle == 1);
[0e06a14]319
[508a0ca]320 size_t td = 0;
[0e06a14]321 size_t remain_size = instance->buffer_size;
[910ca3f]322 char *buffer = instance->data_buffer;
[0e06a14]323 while (remain_size > 0) {
324 const size_t packet_size =
[d017cea]325 (instance->ep->max_packet_size > remain_size) ?
326 remain_size : instance->ep->max_packet_size;
[c8dd5b1]327
[508a0ca]328 td_t *next_td = (td + 1 < data->td_count)
329 ? &data->tds[td + 1] : NULL;
[30a4301]330
[910ca3f]331
[d017cea]332 usb_target_t target =
333 { instance->ep->address, instance->ep->endpoint };
[bcaefe3]334
[910ca3f]335 assert(td < data->td_count);
[bcaefe3]336 td_init(
[508a0ca]337 &data->tds[td], DEFAULT_ERROR_COUNT, packet_size,
[910ca3f]338 toggle, false, low_speed, target, pid, buffer, next_td);
[bcaefe3]339
[910ca3f]340 ++td;
[bcaefe3]341 toggle = 1 - toggle;
[910ca3f]342 buffer += packet_size;
343 assert(packet_size <= remain_size);
[0e06a14]344 remain_size -= packet_size;
345 }
[508a0ca]346 td_set_ioc(&data->tds[td - 1]);
[f567bcf]347 endpoint_toggle_set(instance->ep, toggle);
[4192d3d6]348}
349/*----------------------------------------------------------------------------*/
[910ca3f]350/** Prepare generic control transfer
[a7e2f0d]351 *
352 * @param[in] instance Batch structure to use.
[508a0ca]353 * @param[in] data_stage Pid to use for data tds.
354 * @param[in] status_stage Pid to use for data tds.
[17ceb72]355 *
356 * Setup stage with toggle 0 and USB_PID_SETUP.
357 * Data stage with alternating toggle and pid supplied by parameter.
358 * Status stage with toggle 1 and pid supplied by parameter.
[20a1e76]359 * The last transfer is marked with IOC.
[a7e2f0d]360 */
[1387692]361void batch_control(usb_transfer_batch_t *instance,
[eae83aa]362 usb_packet_id data_stage, usb_packet_id status_stage)
[bdc8ab1]363{
364 assert(instance);
[2cc6e97]365 uhci_transfer_batch_t *data = instance->private_data;
[81dce9f]366 assert(data);
[508a0ca]367 assert(data->td_count >= 2);
[bdc8ab1]368
[d017cea]369 const bool low_speed = instance->ep->speed == USB_SPEED_LOW;
370 const usb_target_t target =
371 { instance->ep->address, instance->ep->endpoint };
372
[bdc8ab1]373 /* setup stage */
[81dce9f]374 td_init(
[910ca3f]375 data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, 0, false,
[d017cea]376 low_speed, target, USB_PID_SETUP, instance->setup_buffer,
[81dce9f]377 &data->tds[1]);
[bdc8ab1]378
379 /* data stage */
[508a0ca]380 size_t td = 1;
[910ca3f]381 unsigned toggle = 1;
[bdc8ab1]382 size_t remain_size = instance->buffer_size;
[910ca3f]383 char *buffer = instance->data_buffer;
[bdc8ab1]384 while (remain_size > 0) {
385 const size_t packet_size =
[d017cea]386 (instance->ep->max_packet_size > remain_size) ?
387 remain_size : instance->ep->max_packet_size;
[bdc8ab1]388
[bcaefe3]389 td_init(
[508a0ca]390 &data->tds[td], DEFAULT_ERROR_COUNT, packet_size,
[d017cea]391 toggle, false, low_speed, target, data_stage,
[910ca3f]392 buffer, &data->tds[td + 1]);
[bdc8ab1]393
[508a0ca]394 ++td;
[910ca3f]395 toggle = 1 - toggle;
396 buffer += packet_size;
[508a0ca]397 assert(td < data->td_count);
[bdc8ab1]398 assert(packet_size <= remain_size);
399 remain_size -= packet_size;
400 }
401
402 /* status stage */
[508a0ca]403 assert(td == data->td_count - 1);
[4192d3d6]404
[81dce9f]405 td_init(
[508a0ca]406 &data->tds[td], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
[d017cea]407 target, status_stage, NULL, NULL);
[508a0ca]408 td_set_ioc(&data->tds[td]);
[7dd3318]409
[81dce9f]410 usb_log_debug2("Control last TD status: %x.\n",
[508a0ca]411 data->tds[td].status);
[4192d3d6]412}
413/*----------------------------------------------------------------------------*/
[f706355]414/** Provides access to QH data structure.
[28d9c95]415 *
[f706355]416 * @param[in] instance Batch pointer to use.
417 * @return Pointer to the QH used by the batch.
418 */
[1387692]419qh_t * batch_qh(usb_transfer_batch_t *instance)
[4192d3d6]420{
421 assert(instance);
[2cc6e97]422 uhci_transfer_batch_t *data = instance->private_data;
[81dce9f]423 assert(data);
424 return data->qh;
[4192d3d6]425}
426/**
427 * @}
428 */
Note: See TracBrowser for help on using the repository browser.