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

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

code review and cstyle cleanup (no change in functionality)

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