source: mainline/uspace/drv/uhci-hcd/batch.c@ 361e61b

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

Fix toggle protocol, add support for all 32 endpoints

  • Property mode set to 100644
File size: 14.7 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
[17ceb72]32 * @brief UHCI driver USB transaction 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"
[8850690]42#include "uhci_hc.h"
[9a818a9]43#include "utils/malloc32.h"
[81dce9f]44#include "uhci_struct/transfer_descriptor.h"
[4192d3d6]45
46#define DEFAULT_ERROR_COUNT 3
47
[81dce9f]48typedef struct uhci_batch {
49 qh_t *qh;
50 td_t *tds;
51 size_t packets;
52 device_keeper_t *manager;
53} uhci_batch_t;
54
[eae83aa]55static void batch_control(batch_t *instance,
56 usb_packet_id data_stage, usb_packet_id status_stage);
[5620bd4]57static void batch_data(batch_t *instance, usb_packet_id pid);
[83c439c]58static void batch_call_in_and_dispose(batch_t *instance);
59static void batch_call_out_and_dispose(batch_t *instance);
[4192d3d6]60
61
[17ceb72]62/** Allocate memory and initialize internal data structure.
[a7e2f0d]63 *
64 * @param[in] fun DDF function to pass to callback.
65 * @param[in] target Device and endpoint target of the transaction.
66 * @param[in] transfer_type Interrupt, Control or Bulk.
67 * @param[in] max_packet_size maximum allowed size of data packets.
68 * @param[in] speed Speed of the transaction.
69 * @param[in] buffer Data source/destination.
70 * @param[in] size Size of the buffer.
71 * @param[in] setup_buffer Setup data source (if not NULL)
72 * @param[in] setup_size Size of setup_buffer (should be always 8)
73 * @param[in] func_in function to call on inbound transaction completion
74 * @param[in] func_out function to call on outbound transaction completion
75 * @param[in] arg additional parameter to func_in or func_out
76 * @param[in] manager Pointer to toggle management structure.
[17ceb72]77 * @return Valid pointer if all substructures were successfully created,
78 * NULL otherwise.
79 *
80 * Determines the number of needed packets (TDs). Prepares a transport buffer
81 * (that is accessible by the hardware). Initializes parameters needed for the
82 * transaction and callback.
[a7e2f0d]83 */
[eb1a2f4]84batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
[9a818a9]85 usb_transfer_type_t transfer_type, size_t max_packet_size,
[81dce9f]86 usb_speed_t speed, char *buffer, size_t buffer_size,
[7dd3318]87 char* setup_buffer, size_t setup_size,
[4192d3d6]88 usbhc_iface_transfer_in_callback_t func_in,
[5620bd4]89 usbhc_iface_transfer_out_callback_t func_out, void *arg,
90 device_keeper_t *manager
91 )
[4192d3d6]92{
93 assert(func_in == NULL || func_out == NULL);
94 assert(func_in != NULL || func_out != NULL);
95
[2ab6875]96#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
97 if (ptr == NULL) { \
98 usb_log_error(message); \
99 if (instance) { \
100 batch_dispose(instance); \
101 } \
102 return NULL; \
103 } else (void)0
104
[83c439c]105 batch_t *instance = malloc(sizeof(batch_t));
[2ab6875]106 CHECK_NULL_DISPOSE_RETURN(instance,
107 "Failed to allocate batch instance.\n");
[81dce9f]108 batch_init(instance, target, transfer_type, speed, max_packet_size,
109 buffer, NULL, buffer_size, NULL, setup_size, func_in,
110 func_out, arg, fun, NULL);
[7dd3318]111
112
[81dce9f]113 uhci_batch_t *data = malloc(sizeof(uhci_batch_t));
114 CHECK_NULL_DISPOSE_RETURN(instance,
115 "Failed to allocate batch instance.\n");
116 bzero(data, sizeof(uhci_batch_t));
117 data->manager = manager;
118 instance->private_data = data;
119
120 data->packets = (buffer_size + max_packet_size - 1) / max_packet_size;
[7dd3318]121 if (transfer_type == USB_TRANSFER_CONTROL) {
[81dce9f]122 data->packets += 2;
[7dd3318]123 }
124
[81dce9f]125 data->tds = malloc32(sizeof(td_t) * data->packets);
[2ab6875]126 CHECK_NULL_DISPOSE_RETURN(
[81dce9f]127 data->tds, "Failed to allocate transfer descriptors.\n");
128 bzero(data->tds, sizeof(td_t) * data->packets);
[9a818a9]129
[81dce9f]130 data->qh = malloc32(sizeof(qh_t));
131 CHECK_NULL_DISPOSE_RETURN(data->qh,
132 "Failed to allocate batch queue head.\n");
133 qh_init(data->qh);
134 qh_set_element_td(data->qh, addr_to_phys(data->tds));
135
136 if (buffer_size > 0) {
137 instance->transport_buffer = malloc32(buffer_size);
[2ab6875]138 CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
139 "Failed to allocate device accessible buffer.\n");
[4192d3d6]140 }
141
[2ab6875]142 if (setup_size > 0) {
143 instance->setup_buffer = malloc32(setup_size);
144 CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
145 "Failed to allocate device accessible setup buffer.\n");
[7dd3318]146 memcpy(instance->setup_buffer, setup_buffer, setup_size);
147 }
148
[4abc304]149 usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
150 instance, target.address, target.endpoint);
[9a818a9]151 return instance;
[4192d3d6]152}
153/*----------------------------------------------------------------------------*/
[17ceb72]154/** Check batch TDs for activity.
[a7e2f0d]155 *
156 * @param[in] instance Batch structure to use.
157 * @return False, if there is an active TD, true otherwise.
[17ceb72]158 *
159 * Walk all TDs. Stop with false if there is an active one (it is to be
160 * processed). Stop with true if an error is found. Return true if the last TS
161 * is reached.
[a7e2f0d]162 */
[83c439c]163bool batch_is_complete(batch_t *instance)
[4192d3d6]164{
165 assert(instance);
[81dce9f]166 uhci_batch_t *data = instance->private_data;
167 assert(data);
168
[48563a3]169 usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
[81dce9f]170 instance, data->packets);
[600733e]171 instance->transfered_size = 0;
[7dd3318]172 size_t i = 0;
[81dce9f]173 for (;i < data->packets; ++i) {
174 if (td_is_active(&data->tds[i])) {
[7dd3318]175 return false;
[600733e]176 }
[c5b93dc]177
[81dce9f]178 instance->error = td_status(&data->tds[i]);
[7dd3318]179 if (instance->error != EOK) {
[48563a3]180 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
[81dce9f]181 instance, i, data->tds[i].status);
182 td_print_status(&data->tds[i]);
[a60150a]183
[81dce9f]184 device_keeper_set_toggle(data->manager,
[c15070c]185 instance->target, instance->direction,
186 td_toggle(&data->tds[i]));
[c5b93dc]187 if (i > 0)
188 goto substract_ret;
[7dd3318]189 return true;
190 }
[c5b93dc]191
[81dce9f]192 instance->transfered_size += td_act_size(&data->tds[i]);
193 if (td_is_short(&data->tds[i]))
[c5b93dc]194 goto substract_ret;
[4192d3d6]195 }
[c5b93dc]196substract_ret:
[600733e]197 instance->transfered_size -= instance->setup_size;
[7dd3318]198 return true;
[4192d3d6]199}
200/*----------------------------------------------------------------------------*/
[a7e2f0d]201/** Prepares control write transaction.
202 *
203 * @param[in] instance Batch structure to use.
[17ceb72]204 *
205 * Uses genercir control function with pids OUT and IN.
[a7e2f0d]206 */
[83c439c]207void batch_control_write(batch_t *instance)
[4192d3d6]208{
209 assert(instance);
[17ceb72]210 /* We are data out, we are supposed to provide data */
[a7e2f0d]211 memcpy(instance->transport_buffer, instance->buffer,
212 instance->buffer_size);
[bdc8ab1]213 batch_control(instance, USB_PID_OUT, USB_PID_IN);
[83c439c]214 instance->next_step = batch_call_out_and_dispose;
[4abc304]215 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
[4192d3d6]216}
217/*----------------------------------------------------------------------------*/
[a7e2f0d]218/** Prepares control read transaction.
219 *
220 * @param[in] instance Batch structure to use.
[17ceb72]221 *
222 * Uses generic control with pids IN and OUT.
[a7e2f0d]223 */
[83c439c]224void batch_control_read(batch_t *instance)
[4192d3d6]225{
226 assert(instance);
[bdc8ab1]227 batch_control(instance, USB_PID_IN, USB_PID_OUT);
[83c439c]228 instance->next_step = batch_call_in_and_dispose;
[4abc304]229 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
[4192d3d6]230}
231/*----------------------------------------------------------------------------*/
[17ceb72]232/** Prepare interrupt in transaction.
[a7e2f0d]233 *
234 * @param[in] instance Batch structure to use.
[17ceb72]235 *
236 * Data transaction with PID_IN.
[a7e2f0d]237 */
[5620bd4]238void batch_interrupt_in(batch_t *instance)
[4192d3d6]239{
[c8dd5b1]240 assert(instance);
[c15070c]241 instance->direction = USB_DIRECTION_IN;
[5620bd4]242 batch_data(instance, USB_PID_IN);
[83c439c]243 instance->next_step = batch_call_in_and_dispose;
[4abc304]244 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
[4192d3d6]245}
246/*----------------------------------------------------------------------------*/
[17ceb72]247/** Prepare interrupt out transaction.
[a7e2f0d]248 *
249 * @param[in] instance Batch structure to use.
[17ceb72]250 *
251 * Data transaction with PID_OUT.
[a7e2f0d]252 */
[5620bd4]253void batch_interrupt_out(batch_t *instance)
[4192d3d6]254{
[c8dd5b1]255 assert(instance);
[c15070c]256 instance->direction = USB_DIRECTION_OUT;
[17ceb72]257 /* We are data out, we are supposed to provide data */
[a963a68]258 memcpy(instance->transport_buffer, instance->buffer,
259 instance->buffer_size);
[5620bd4]260 batch_data(instance, USB_PID_OUT);
[0e06a14]261 instance->next_step = batch_call_out_and_dispose;
262 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
263}
264/*----------------------------------------------------------------------------*/
[17ceb72]265/** Prepare bulk in transaction.
[a7e2f0d]266 *
267 * @param[in] instance Batch structure to use.
[17ceb72]268 *
269 * Data transaction with PID_IN.
[a7e2f0d]270 */
[5620bd4]271void batch_bulk_in(batch_t *instance)
[0e06a14]272{
273 assert(instance);
[5620bd4]274 batch_data(instance, USB_PID_IN);
[c15070c]275 instance->direction = USB_DIRECTION_IN;
[0e06a14]276 instance->next_step = batch_call_in_and_dispose;
277 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
278}
279/*----------------------------------------------------------------------------*/
[17ceb72]280/** Prepare bulk out transaction.
[a7e2f0d]281 *
282 * @param[in] instance Batch structure to use.
[17ceb72]283 *
284 * Data transaction with PID_OUT.
[a7e2f0d]285 */
[5620bd4]286void batch_bulk_out(batch_t *instance)
[0e06a14]287{
288 assert(instance);
[c15070c]289 instance->direction = USB_DIRECTION_OUT;
[17ceb72]290 /* We are data out, we are supposed to provide data */
[a963a68]291 memcpy(instance->transport_buffer, instance->buffer,
292 instance->buffer_size);
[5620bd4]293 batch_data(instance, USB_PID_OUT);
[0e06a14]294 instance->next_step = batch_call_out_and_dispose;
295 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
296}
297/*----------------------------------------------------------------------------*/
[17ceb72]298/** Prepare generic data transaction
[a7e2f0d]299 *
300 * @param[in] instance Batch structure to use.
301 * @param[in] pid to use for data packets.
[17ceb72]302 *
303 * Packets with alternating toggle bit and supplied pid value.
304 * The last packet is marked with IOC flag.
[a7e2f0d]305 */
[5620bd4]306void batch_data(batch_t *instance, usb_packet_id pid)
[0e06a14]307{
308 assert(instance);
[81dce9f]309 uhci_batch_t *data = instance->private_data;
310 assert(data);
311
[c3ae877]312 const bool low_speed = instance->speed == USB_SPEED_LOW;
[c15070c]313 int toggle = device_keeper_get_toggle(
314 data->manager, instance->target, instance->direction);
[3e37964]315 assert(toggle == 0 || toggle == 1);
[0e06a14]316
317 size_t packet = 0;
318 size_t remain_size = instance->buffer_size;
319 while (remain_size > 0) {
[81dce9f]320 char *trans_data =
[0e06a14]321 instance->transport_buffer + instance->buffer_size
322 - remain_size;
323
324 const size_t packet_size =
325 (instance->max_packet_size > remain_size) ?
326 remain_size : instance->max_packet_size;
[c8dd5b1]327
[81dce9f]328 td_t *next_packet = (packet + 1 < data->packets)
329 ? &data->tds[packet + 1] : NULL;
[30a4301]330
[81dce9f]331 assert(packet < data->packets);
[0e06a14]332 assert(packet_size <= remain_size);
[bcaefe3]333
334 td_init(
[81dce9f]335 &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
336 toggle, false, low_speed, instance->target, pid, trans_data,
[bcaefe3]337 next_packet);
338
339
340 toggle = 1 - toggle;
[0e06a14]341 remain_size -= packet_size;
[bcaefe3]342 ++packet;
[0e06a14]343 }
[81dce9f]344 td_set_ioc(&data->tds[packet - 1]);
[c15070c]345 device_keeper_set_toggle(data->manager, instance->target,
346 instance->direction, toggle);
[4192d3d6]347}
348/*----------------------------------------------------------------------------*/
[17ceb72]349/** Prepare generic control transaction
[a7e2f0d]350 *
351 * @param[in] instance Batch structure to use.
352 * @param[in] data_stage to use for data packets.
353 * @param[in] status_stage to use for data packets.
[17ceb72]354 *
355 * Setup stage with toggle 0 and USB_PID_SETUP.
356 * Data stage with alternating toggle and pid supplied by parameter.
357 * Status stage with toggle 1 and pid supplied by parameter.
358 * The last packet is marked with IOC.
[a7e2f0d]359 */
[3e37964]360void batch_control(batch_t *instance,
[eae83aa]361 usb_packet_id data_stage, usb_packet_id status_stage)
[bdc8ab1]362{
363 assert(instance);
[81dce9f]364 uhci_batch_t *data = instance->private_data;
365 assert(data);
366 assert(data->packets >= 2);
[bdc8ab1]367
368 const bool low_speed = instance->speed == USB_SPEED_LOW;
369 int toggle = 0;
370 /* setup stage */
[81dce9f]371 td_init(
372 data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, toggle, false,
373 low_speed, instance->target, USB_PID_SETUP, instance->setup_buffer,
374 &data->tds[1]);
[bdc8ab1]375
376 /* data stage */
377 size_t packet = 1;
378 size_t remain_size = instance->buffer_size;
379 while (remain_size > 0) {
[81dce9f]380 char *control_data =
[bdc8ab1]381 instance->transport_buffer + instance->buffer_size
382 - remain_size;
383
384 toggle = 1 - toggle;
385
386 const size_t packet_size =
387 (instance->max_packet_size > remain_size) ?
388 remain_size : instance->max_packet_size;
389
[bcaefe3]390 td_init(
[81dce9f]391 &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
[bcaefe3]392 toggle, false, low_speed, instance->target, data_stage,
[81dce9f]393 control_data, &data->tds[packet + 1]);
[bdc8ab1]394
395 ++packet;
[81dce9f]396 assert(packet < data->packets);
[bdc8ab1]397 assert(packet_size <= remain_size);
398 remain_size -= packet_size;
399 }
400
401 /* status stage */
[81dce9f]402 assert(packet == data->packets - 1);
[4192d3d6]403
[81dce9f]404 td_init(
405 &data->tds[packet], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
406 instance->target, status_stage, NULL, NULL);
407 td_set_ioc(&data->tds[packet]);
[7dd3318]408
[81dce9f]409 usb_log_debug2("Control last TD status: %x.\n",
410 data->tds[packet].status);
[4192d3d6]411}
412/*----------------------------------------------------------------------------*/
[81dce9f]413qh_t * batch_qh(batch_t *instance)
[4192d3d6]414{
415 assert(instance);
[81dce9f]416 uhci_batch_t *data = instance->private_data;
417 assert(data);
418 return data->qh;
[4192d3d6]419}
420/*----------------------------------------------------------------------------*/
[17ceb72]421/** Helper function calls callback and correctly disposes of batch structure.
[a7e2f0d]422 *
423 * @param[in] instance Batch structure to use.
424 */
[83c439c]425void batch_call_in_and_dispose(batch_t *instance)
[4192d3d6]426{
427 assert(instance);
[83c439c]428 batch_call_in(instance);
[a60150a]429 batch_dispose(instance);
[4192d3d6]430}
431/*----------------------------------------------------------------------------*/
[17ceb72]432/** Helper function calls callback and correctly disposes of batch structure.
[a7e2f0d]433 *
434 * @param[in] instance Batch structure to use.
435 */
[83c439c]436void batch_call_out_and_dispose(batch_t *instance)
[4192d3d6]437{
438 assert(instance);
[83c439c]439 batch_call_out(instance);
[a60150a]440 batch_dispose(instance);
441}
442/*----------------------------------------------------------------------------*/
[17ceb72]443/** Correctly dispose all used data structures.
[a7e2f0d]444 *
445 * @param[in] instance Batch structure to use.
446 */
[a60150a]447void batch_dispose(batch_t *instance)
448{
449 assert(instance);
[81dce9f]450 uhci_batch_t *data = instance->private_data;
451 assert(data);
[48563a3]452 usb_log_debug("Batch(%p) disposing.\n", instance);
[bcaefe3]453 /* free32 is NULL safe */
[81dce9f]454 free32(data->tds);
455 free32(data->qh);
[7dd3318]456 free32(instance->setup_buffer);
457 free32(instance->transport_buffer);
[81dce9f]458 free(data);
[4192d3d6]459 free(instance);
460}
461/**
462 * @}
463 */
Note: See TracBrowser for help on using the repository browser.