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

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

uhci: Use foreach_safe to iterate through list.

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