source: mainline/uspace/drv/uhci-hcd/batch.c@ 2cc6e97

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

Move more functionality to libUSB usb_transfer_batch_t

UHCI uses one device accessible buffer for both structures and data

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