source: mainline/uspace/drv/bus/usb/uhci/uhci_batch.c@ ad91b806

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

uhci: uhci_transfer_batch_is_complete takes const argument.

  • Property mode set to 100644
File size: 11.9 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>
[3f162ab]36#include <macros.h>
[4192d3d6]37
[bdc8ab1]38#include <usb/usb.h>
[4192d3d6]39#include <usb/debug.h>
40
[07f49ae]41#include "uhci_batch.h"
[53338bda]42#include "transfer_list.h"
[87644b4]43#include "hw_struct/transfer_descriptor.h"
[9a818a9]44#include "utils/malloc32.h"
[4192d3d6]45
46#define DEFAULT_ERROR_COUNT 3
[3afb758]47
[b991d37]48static void uhci_transfer_batch_dispose(uhci_transfer_batch_t *uhci_batch)
[5fe0a697]49{
[b991d37]50 if (uhci_batch) {
[549ff23]51 usb_transfer_batch_destroy(uhci_batch->usb_batch);
[b991d37]52 free32(uhci_batch->device_buffer);
53 free(uhci_batch);
54 }
[5fe0a697]55}
[28d9c95]56/*----------------------------------------------------------------------------*/
57/** Safely destructs uhci_transfer_batch_t structure
58 *
59 * @param[in] uhci_batch Instance to destroy.
60 */
[6bba41d]61void uhci_transfer_batch_finish_dispose(uhci_transfer_batch_t *uhci_batch)
[2cc6e97]62{
[b991d37]63 assert(uhci_batch);
64 assert(uhci_batch->usb_batch);
[f18d82f0]65 usb_transfer_batch_finish(uhci_batch->usb_batch,
[5cfcc64]66 uhci_transfer_batch_data_buffer(uhci_batch));
[b991d37]67 uhci_transfer_batch_dispose(uhci_batch);
[2cc6e97]68}
69/*----------------------------------------------------------------------------*/
[790318e]70static void (*const batch_setup[])(uhci_transfer_batch_t*, usb_direction_t);
[b991d37]71/*----------------------------------------------------------------------------*/
[17ceb72]72/** Allocate memory and initialize internal data structure.
[a7e2f0d]73 *
74 * @param[in] fun DDF function to pass to callback.
[910ca3f]75 * @param[in] ep Communication target
[a7e2f0d]76 * @param[in] buffer Data source/destination.
[23f40280]77 * @param[in] buffer_size Size of the buffer.
[a7e2f0d]78 * @param[in] setup_buffer Setup data source (if not NULL)
79 * @param[in] setup_size Size of setup_buffer (should be always 8)
[910ca3f]80 * @param[in] func_in function to call on inbound transfer completion
81 * @param[in] func_out function to call on outbound transfer completion
[a7e2f0d]82 * @param[in] arg additional parameter to func_in or func_out
[f706355]83 * @return Valid pointer if all structures were successfully created,
[17ceb72]84 * NULL otherwise.
85 *
[508a0ca]86 * Determines the number of needed transfer descriptors (TDs).
87 * Prepares a transport buffer (that is accessible by the hardware).
[910ca3f]88 * Initializes parameters needed for the transfer and callback.
[a7e2f0d]89 */
[b991d37]90uhci_transfer_batch_t * uhci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
[4192d3d6]91{
[b991d37]92 assert((sizeof(td_t) % 16) == 0);
[2ab6875]93#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
94 if (ptr == NULL) { \
95 usb_log_error(message); \
[b991d37]96 uhci_transfer_batch_dispose(uhci_batch); \
97 return NULL; \
[2ab6875]98 } else (void)0
99
[b991d37]100 uhci_transfer_batch_t *uhci_batch =
[3afb758]101 calloc(1, sizeof(uhci_transfer_batch_t));
[b991d37]102 CHECK_NULL_DISPOSE_RETURN(uhci_batch,
[2cc6e97]103 "Failed to allocate UHCI batch.\n");
[9a790ad1]104 link_initialize(&uhci_batch->link);
[b991d37]105 uhci_batch->td_count =
106 (usb_batch->buffer_size + usb_batch->ep->max_packet_size - 1)
107 / usb_batch->ep->max_packet_size;
108 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
109 uhci_batch->td_count += 2;
[7dd3318]110 }
111
[b991d37]112 const size_t total_size = (sizeof(td_t) * uhci_batch->td_count)
113 + sizeof(qh_t) + usb_batch->setup_size + usb_batch->buffer_size;
114 uhci_batch->device_buffer = malloc32(total_size);
115 CHECK_NULL_DISPOSE_RETURN(uhci_batch->device_buffer,
[2cc6e97]116 "Failed to allocate UHCI buffer.\n");
[b991d37]117 bzero(uhci_batch->device_buffer, total_size);
[4192d3d6]118
[b991d37]119 uhci_batch->tds = uhci_batch->device_buffer;
120 uhci_batch->qh =
121 (uhci_batch->device_buffer + (sizeof(td_t) * uhci_batch->td_count));
[2cc6e97]122
[b991d37]123 qh_init(uhci_batch->qh);
124 qh_set_element_td(uhci_batch->qh, &uhci_batch->tds[0]);
[2cc6e97]125
[b991d37]126 void *dest =
127 uhci_batch->device_buffer + (sizeof(td_t) * uhci_batch->td_count)
[2cc6e97]128 + sizeof(qh_t);
[b991d37]129 /* Copy SETUP packet data to the device buffer */
130 memcpy(dest, usb_batch->setup_buffer, usb_batch->setup_size);
131 dest += usb_batch->setup_size;
[5d915b7]132 /* Copy generic data unless they are provided by the device */
[b991d37]133 if (usb_batch->ep->direction != USB_DIRECTION_IN) {
134 memcpy(dest, usb_batch->buffer, usb_batch->buffer_size);
135 }
136 uhci_batch->usb_batch = usb_batch;
[c4fb5ecd]137 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
[b991d37]138 " memory structures ready.\n", usb_batch,
139 USB_TRANSFER_BATCH_ARGS(*usb_batch));
[5d915b7]140
[25d224c6]141 const usb_direction_t dir = usb_transfer_batch_direction(usb_batch);
142
[790318e]143 assert(batch_setup[usb_batch->ep->transfer_type]);
144 batch_setup[usb_batch->ep->transfer_type](uhci_batch, dir);
[b991d37]145
146 return uhci_batch;
[4192d3d6]147}
148/*----------------------------------------------------------------------------*/
[17ceb72]149/** Check batch TDs for activity.
[a7e2f0d]150 *
[5f57929]151 * @param[in] uhci_batch Batch structure to use.
[a7e2f0d]152 * @return False, if there is an active TD, true otherwise.
[17ceb72]153 *
154 * Walk all TDs. Stop with false if there is an active one (it is to be
[910ca3f]155 * processed). Stop with true if an error is found. Return true if the last TD
[17ceb72]156 * is reached.
[a7e2f0d]157 */
[ad91b806]158bool uhci_transfer_batch_is_complete(const uhci_transfer_batch_t *uhci_batch)
[4192d3d6]159{
[b991d37]160 assert(uhci_batch);
161 assert(uhci_batch->usb_batch);
[81dce9f]162
[b991d37]163 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
164 " checking %zu transfer(s) for completion.\n",
165 uhci_batch->usb_batch,
166 USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch),
167 uhci_batch->td_count);
168 uhci_batch->usb_batch->transfered_size = 0;
[75f9dcd]169
170 for (size_t i = 0;i < uhci_batch->td_count; ++i) {
[b991d37]171 if (td_is_active(&uhci_batch->tds[i])) {
[7dd3318]172 return false;
[600733e]173 }
[c5b93dc]174
[b991d37]175 uhci_batch->usb_batch->error = td_status(&uhci_batch->tds[i]);
176 if (uhci_batch->usb_batch->error != EOK) {
177 assert(uhci_batch->usb_batch->ep != NULL);
178
[28d9c95]179 usb_log_debug("Batch(%p) found error TD(%zu):%"
[b991d37]180 PRIx32 ".\n", uhci_batch->usb_batch, i,
181 uhci_batch->tds[i].status);
182 td_print_status(&uhci_batch->tds[i]);
[feb10c88]183
[b991d37]184 endpoint_toggle_set(uhci_batch->usb_batch->ep,
185 td_toggle(&uhci_batch->tds[i]));
[c5b93dc]186 if (i > 0)
187 goto substract_ret;
[7dd3318]188 return true;
189 }
[c5b93dc]190
[b991d37]191 uhci_batch->usb_batch->transfered_size
192 += td_act_size(&uhci_batch->tds[i]);
193 if (td_is_short(&uhci_batch->tds[i]))
[c5b93dc]194 goto substract_ret;
[4192d3d6]195 }
[c5b93dc]196substract_ret:
[b991d37]197 uhci_batch->usb_batch->transfered_size
198 -= uhci_batch->usb_batch->setup_size;
[7dd3318]199 return true;
[4192d3d6]200}
[0e06a14]201/*----------------------------------------------------------------------------*/
[25d224c6]202static const usb_packet_id direction_pids[] = {
203 [USB_DIRECTION_IN] = USB_PID_IN,
204 [USB_DIRECTION_OUT] = USB_PID_OUT,
205};
206/*----------------------------------------------------------------------------*/
[910ca3f]207/** Prepare generic data transfer
[a7e2f0d]208 *
[5f57929]209 * @param[in] uhci_batch Batch structure to use.
[25d224c6]210 * @param[in] dir Communication direction.
[17ceb72]211 *
[910ca3f]212 * Transactions with alternating toggle bit and supplied pid value.
[20a1e76]213 * The last transfer is marked with IOC flag.
[a7e2f0d]214 */
[25d224c6]215static void batch_data(uhci_transfer_batch_t *uhci_batch, usb_direction_t dir)
[0e06a14]216{
[b991d37]217 assert(uhci_batch);
218 assert(uhci_batch->usb_batch);
[5d915b7]219 assert(uhci_batch->usb_batch->ep);
[25d224c6]220 assert(dir == USB_DIRECTION_OUT || dir == USB_DIRECTION_IN);
[5d915b7]221
[9a790ad1]222
[25d224c6]223 const usb_packet_id pid = direction_pids[dir];
[b991d37]224 const bool low_speed =
225 uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
226 const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
[56e9fb0]227 const usb_target_t target = {{
[b991d37]228 uhci_batch->usb_batch->ep->address,
[56e9fb0]229 uhci_batch->usb_batch->ep->endpoint }};
[b991d37]230
231 int toggle = endpoint_toggle_get(uhci_batch->usb_batch->ep);
[3e37964]232 assert(toggle == 0 || toggle == 1);
[0e06a14]233
[508a0ca]234 size_t td = 0;
[b991d37]235 size_t remain_size = uhci_batch->usb_batch->buffer_size;
236 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
237
[0e06a14]238 while (remain_size > 0) {
[3f162ab]239 const size_t packet_size = min(remain_size, mps);
[c8dd5b1]240
[b991d37]241 const td_t *next_td = (td + 1 < uhci_batch->td_count)
242 ? &uhci_batch->tds[td + 1] : NULL;
[30a4301]243
[b991d37]244 assert(td < uhci_batch->td_count);
[bcaefe3]245 td_init(
[b991d37]246 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
[910ca3f]247 toggle, false, low_speed, target, pid, buffer, next_td);
[bcaefe3]248
[910ca3f]249 ++td;
[bcaefe3]250 toggle = 1 - toggle;
[910ca3f]251 buffer += packet_size;
[0e06a14]252 remain_size -= packet_size;
253 }
[b991d37]254 td_set_ioc(&uhci_batch->tds[td - 1]);
255 endpoint_toggle_set(uhci_batch->usb_batch->ep, toggle);
[5f57929]256 usb_log_debug2(
257 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
258 uhci_batch->usb_batch,
[9a790ad1]259 usb_str_transfer_type(uhci_batch->usb_batch->ep->transfer_type),
[5f57929]260 usb_str_direction(uhci_batch->usb_batch->ep->direction),
261 USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch));
[4192d3d6]262}
263/*----------------------------------------------------------------------------*/
[910ca3f]264/** Prepare generic control transfer
[a7e2f0d]265 *
[5f57929]266 * @param[in] uhci_batch Batch structure to use.
[25d224c6]267 * @param[in] dir Communication direction.
[17ceb72]268 *
269 * Setup stage with toggle 0 and USB_PID_SETUP.
[25d224c6]270 * Data stage with alternating toggle and pid determined by the communication
271 * direction.
272 * Status stage with toggle 1 and pid determined by the communication direction.
[20a1e76]273 * The last transfer is marked with IOC.
[a7e2f0d]274 */
[25d224c6]275static void batch_control(uhci_transfer_batch_t *uhci_batch, usb_direction_t dir)
[bdc8ab1]276{
[b991d37]277 assert(uhci_batch);
278 assert(uhci_batch->usb_batch);
279 assert(uhci_batch->usb_batch->ep);
[25d224c6]280 assert(dir == USB_DIRECTION_OUT || dir == USB_DIRECTION_IN);
[b991d37]281 assert(uhci_batch->td_count >= 2);
[25d224c6]282 static const usb_packet_id status_stage_pids[] = {
283 [USB_DIRECTION_IN] = USB_PID_OUT,
284 [USB_DIRECTION_OUT] = USB_PID_IN,
285 };
[b991d37]286
[25d224c6]287 const usb_packet_id data_stage_pid = direction_pids[dir];
288 const usb_packet_id status_stage_pid = status_stage_pids[dir];
[b991d37]289 const bool low_speed =
290 uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
291 const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
[56e9fb0]292 const usb_target_t target = {{
[b991d37]293 uhci_batch->usb_batch->ep->address,
[56e9fb0]294 uhci_batch->usb_batch->ep->endpoint }};
[d017cea]295
[bdc8ab1]296 /* setup stage */
[81dce9f]297 td_init(
[b991d37]298 &uhci_batch->tds[0], DEFAULT_ERROR_COUNT,
299 uhci_batch->usb_batch->setup_size, 0, false,
300 low_speed, target, USB_PID_SETUP,
301 uhci_transfer_batch_setup_buffer(uhci_batch), &uhci_batch->tds[1]);
[bdc8ab1]302
303 /* data stage */
[508a0ca]304 size_t td = 1;
[910ca3f]305 unsigned toggle = 1;
[b991d37]306 size_t remain_size = uhci_batch->usb_batch->buffer_size;
307 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
308
[bdc8ab1]309 while (remain_size > 0) {
[3f162ab]310 const size_t packet_size = min(remain_size, mps);
[bdc8ab1]311
[bcaefe3]312 td_init(
[b991d37]313 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
314 toggle, false, low_speed, target, data_stage_pid,
315 buffer, &uhci_batch->tds[td + 1]);
[bdc8ab1]316
[508a0ca]317 ++td;
[910ca3f]318 toggle = 1 - toggle;
319 buffer += packet_size;
[bdc8ab1]320 remain_size -= packet_size;
[b991d37]321 assert(td < uhci_batch->td_count);
[bdc8ab1]322 }
323
324 /* status stage */
[b991d37]325 assert(td == uhci_batch->td_count - 1);
[4192d3d6]326
[81dce9f]327 td_init(
[b991d37]328 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
329 target, status_stage_pid, NULL, NULL);
330 td_set_ioc(&uhci_batch->tds[td]);
[7dd3318]331
[81dce9f]332 usb_log_debug2("Control last TD status: %x.\n",
[b991d37]333 uhci_batch->tds[td].status);
[4192d3d6]334}
335/*----------------------------------------------------------------------------*/
[790318e]336static void (*const batch_setup[])(uhci_transfer_batch_t*, usb_direction_t) =
[b991d37]337{
[790318e]338 [USB_TRANSFER_CONTROL] = batch_control,
339 [USB_TRANSFER_BULK] = batch_data,
340 [USB_TRANSFER_INTERRUPT] = batch_data,
341 [USB_TRANSFER_ISOCHRONOUS] = NULL,
[b991d37]342};
[4192d3d6]343/**
344 * @}
345 */
Note: See TracBrowser for help on using the repository browser.