source: mainline/uspace/drv/uhci-hcd/batch.c@ 57c0a7e

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

Integrate device_keeper into bathc structure

Check every control write for toggle reset

  • Property mode set to 100644
File size: 12.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 */
28/** @addtogroup usb
29 * @{
30 */
31/** @file
32 * @brief UHCI driver
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"
[9a818a9]42#include "uhci.h"
43#include "utils/malloc32.h"
[4192d3d6]44
45#define DEFAULT_ERROR_COUNT 3
46
[83c439c]47static int batch_schedule(batch_t *instance);
[9a818a9]48
[eae83aa]49static void batch_control(batch_t *instance,
50 usb_packet_id data_stage, usb_packet_id status_stage);
[5620bd4]51static void batch_data(batch_t *instance, usb_packet_id pid);
[83c439c]52static void batch_call_in(batch_t *instance);
53static void batch_call_out(batch_t *instance);
54static void batch_call_in_and_dispose(batch_t *instance);
55static void batch_call_out_and_dispose(batch_t *instance);
[4192d3d6]56
57
[eb1a2f4]58batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
[9a818a9]59 usb_transfer_type_t transfer_type, size_t max_packet_size,
[1a93bb0]60 usb_speed_t speed, char *buffer, size_t size,
[7dd3318]61 char* setup_buffer, size_t setup_size,
[4192d3d6]62 usbhc_iface_transfer_in_callback_t func_in,
[5620bd4]63 usbhc_iface_transfer_out_callback_t func_out, void *arg,
64 device_keeper_t *manager
65 )
[4192d3d6]66{
67 assert(func_in == NULL || func_out == NULL);
68 assert(func_in != NULL || func_out != NULL);
69
[83c439c]70 batch_t *instance = malloc(sizeof(batch_t));
[7dd3318]71 if (instance == NULL) {
[83c439c]72 usb_log_error("Failed to allocate batch instance.\n");
[7dd3318]73 return NULL;
74 }
75
76 instance->qh = queue_head_get();
77 if (instance->qh == NULL) {
78 usb_log_error("Failed to allocate queue head.\n");
79 free(instance);
80 return NULL;
81 }
82
83 instance->packets = (size + max_packet_size - 1) / max_packet_size;
84 if (transfer_type == USB_TRANSFER_CONTROL) {
85 instance->packets += 2;
86 }
87
[c5b93dc]88 instance->tds = malloc32(sizeof(td_t) * instance->packets);
[7dd3318]89 if (instance->tds == NULL) {
90 usb_log_error("Failed to allocate transfer descriptors.\n");
91 queue_head_dispose(instance->qh);
92 free(instance);
[9a818a9]93 return NULL;
94 }
[c5b93dc]95 bzero(instance->tds, sizeof(td_t) * instance->packets);
[9a818a9]96
[7dd3318]97 const size_t transport_size = max_packet_size * instance->packets;
98
99 instance->transport_buffer =
100 (size > 0) ? malloc32(transport_size) : NULL;
[bdc8ab1]101
[7dd3318]102 if ((size > 0) && (instance->transport_buffer == NULL)) {
103 usb_log_error("Failed to allocate device accessible buffer.\n");
104 queue_head_dispose(instance->qh);
105 free32(instance->tds);
[9a818a9]106 free(instance);
107 return NULL;
[4192d3d6]108 }
109
[7dd3318]110 instance->setup_buffer = setup_buffer ? malloc32(setup_size) : NULL;
111 if ((setup_size > 0) && (instance->setup_buffer == NULL)) {
112 usb_log_error("Failed to allocate device accessible setup buffer.\n");
113 queue_head_dispose(instance->qh);
114 free32(instance->tds);
115 free32(instance->transport_buffer);
[9a818a9]116 free(instance);
117 return NULL;
[4192d3d6]118 }
[7dd3318]119 if (instance->setup_buffer) {
120 memcpy(instance->setup_buffer, setup_buffer, setup_size);
121 }
122
[4192d3d6]123 instance->max_packet_size = max_packet_size;
124
125 link_initialize(&instance->link);
[7dd3318]126
[4192d3d6]127 instance->target = target;
[9a818a9]128 instance->transfer_type = transfer_type;
[4192d3d6]129
130 if (func_out)
131 instance->callback_out = func_out;
132 if (func_in)
133 instance->callback_in = func_in;
[7dd3318]134
[4192d3d6]135 instance->buffer = buffer;
136 instance->buffer_size = size;
[7dd3318]137 instance->setup_size = setup_size;
[eb1a2f4]138 instance->fun = fun;
[4192d3d6]139 instance->arg = arg;
[014d5033]140 instance->speed = speed;
[5620bd4]141 instance->manager = manager;
[9a818a9]142
[7dd3318]143 queue_head_element_td(instance->qh, addr_to_phys(instance->tds));
[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/*----------------------------------------------------------------------------*/
[83c439c]149bool batch_is_complete(batch_t *instance)
[4192d3d6]150{
151 assert(instance);
[48563a3]152 usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
[7dd3318]153 instance, instance->packets);
[600733e]154 instance->transfered_size = 0;
[7dd3318]155 size_t i = 0;
156 for (;i < instance->packets; ++i) {
[c5b93dc]157 if (td_is_active(&instance->tds[i])) {
[7dd3318]158 return false;
[600733e]159 }
[c5b93dc]160
161 instance->error = td_status(&instance->tds[i]);
[7dd3318]162 if (instance->error != EOK) {
[48563a3]163 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
[c5b93dc]164 instance, i, instance->tds[i].status);
165 if (i > 0)
166 goto substract_ret;
[7dd3318]167 return true;
168 }
[c5b93dc]169
170 instance->transfered_size += td_act_size(&instance->tds[i]);
171 if (td_is_short(&instance->tds[i]))
172 goto substract_ret;
[4192d3d6]173 }
[c5b93dc]174substract_ret:
[600733e]175 instance->transfered_size -= instance->setup_size;
[7dd3318]176 return true;
[4192d3d6]177}
178/*----------------------------------------------------------------------------*/
[83c439c]179void batch_control_write(batch_t *instance)
[4192d3d6]180{
181 assert(instance);
[7dd3318]182 /* we are data out, we are supposed to provide data */
183 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
[bdc8ab1]184 batch_control(instance, USB_PID_OUT, USB_PID_IN);
[83c439c]185 instance->next_step = batch_call_out_and_dispose;
[4abc304]186 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
[83c439c]187 batch_schedule(instance);
[4192d3d6]188}
189/*----------------------------------------------------------------------------*/
[83c439c]190void batch_control_read(batch_t *instance)
[4192d3d6]191{
192 assert(instance);
[bdc8ab1]193 batch_control(instance, USB_PID_IN, USB_PID_OUT);
[83c439c]194 instance->next_step = batch_call_in_and_dispose;
[4abc304]195 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
[83c439c]196 batch_schedule(instance);
[4192d3d6]197}
198/*----------------------------------------------------------------------------*/
[5620bd4]199void batch_interrupt_in(batch_t *instance)
[4192d3d6]200{
[c8dd5b1]201 assert(instance);
[5620bd4]202 batch_data(instance, USB_PID_IN);
[83c439c]203 instance->next_step = batch_call_in_and_dispose;
[4abc304]204 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
[83c439c]205 batch_schedule(instance);
[4192d3d6]206}
207/*----------------------------------------------------------------------------*/
[5620bd4]208void batch_interrupt_out(batch_t *instance)
[4192d3d6]209{
[c8dd5b1]210 assert(instance);
[7dd3318]211 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
[5620bd4]212 batch_data(instance, USB_PID_OUT);
[0e06a14]213 instance->next_step = batch_call_out_and_dispose;
214 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
215 batch_schedule(instance);
216}
217/*----------------------------------------------------------------------------*/
[5620bd4]218void batch_bulk_in(batch_t *instance)
[0e06a14]219{
220 assert(instance);
[5620bd4]221 batch_data(instance, USB_PID_IN);
[0e06a14]222 instance->next_step = batch_call_in_and_dispose;
223 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
224 batch_schedule(instance);
225}
226/*----------------------------------------------------------------------------*/
[5620bd4]227void batch_bulk_out(batch_t *instance)
[0e06a14]228{
229 assert(instance);
230 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
[5620bd4]231 batch_data(instance, USB_PID_OUT);
[0e06a14]232 instance->next_step = batch_call_out_and_dispose;
233 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
234 batch_schedule(instance);
235}
236/*----------------------------------------------------------------------------*/
[5620bd4]237void batch_data(batch_t *instance, usb_packet_id pid)
[0e06a14]238{
239 assert(instance);
[c3ae877]240 const bool low_speed = instance->speed == USB_SPEED_LOW;
[5620bd4]241 int toggle = device_keeper_get_toggle(instance->manager, instance->target);
[3e37964]242 assert(toggle == 0 || toggle == 1);
[0e06a14]243
244 size_t packet = 0;
245 size_t remain_size = instance->buffer_size;
246 while (remain_size > 0) {
[7dd3318]247 char *data =
[0e06a14]248 instance->transport_buffer + instance->buffer_size
249 - remain_size;
250
[c8dd5b1]251
[0e06a14]252 const size_t packet_size =
253 (instance->max_packet_size > remain_size) ?
254 remain_size : instance->max_packet_size;
[c8dd5b1]255
[c5b93dc]256 td_init(&instance->tds[packet],
[0e06a14]257 DEFAULT_ERROR_COUNT, packet_size, toggle, false, low_speed,
258 instance->target, pid, data,
259 &instance->tds[packet + 1]);
[30a4301]260
[3e37964]261 toggle = 1 - toggle;
[0e06a14]262 ++packet;
263 assert(packet <= instance->packets);
264 assert(packet_size <= remain_size);
265 remain_size -= packet_size;
266 }
[5620bd4]267 device_keeper_set_toggle(instance->manager, instance->target, toggle);
[0e06a14]268
269 instance->tds[packet - 1].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
270 instance->tds[packet - 1].next = 0 | LINK_POINTER_TERMINATE_FLAG;
[4192d3d6]271}
272/*----------------------------------------------------------------------------*/
[3e37964]273void batch_control(batch_t *instance,
[eae83aa]274 usb_packet_id data_stage, usb_packet_id status_stage)
[bdc8ab1]275{
276 assert(instance);
277
278 const bool low_speed = instance->speed == USB_SPEED_LOW;
279 int toggle = 0;
280 /* setup stage */
[c5b93dc]281 td_init(instance->tds, DEFAULT_ERROR_COUNT,
[bdc8ab1]282 instance->setup_size, toggle, false, low_speed, instance->target,
283 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
284
285 /* data stage */
286 size_t packet = 1;
287 size_t remain_size = instance->buffer_size;
288 while (remain_size > 0) {
289 char *data =
290 instance->transport_buffer + instance->buffer_size
291 - remain_size;
292
293 toggle = 1 - toggle;
294
295 const size_t packet_size =
296 (instance->max_packet_size > remain_size) ?
297 remain_size : instance->max_packet_size;
298
[c5b93dc]299 td_init(&instance->tds[packet],
[bdc8ab1]300 DEFAULT_ERROR_COUNT, packet_size, toggle, false, low_speed,
301 instance->target, data_stage, data,
302 &instance->tds[packet + 1]);
303
304 ++packet;
305 assert(packet < instance->packets);
306 assert(packet_size <= remain_size);
307 remain_size -= packet_size;
308 }
309
310 /* status stage */
311 assert(packet == instance->packets - 1);
[c5b93dc]312 td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT,
[bdc8ab1]313 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL);
314
315
316 instance->tds[packet].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
317 usb_log_debug2("Control last TD status: %x.\n",
318 instance->tds[packet].status);
319}
320/*----------------------------------------------------------------------------*/
[83c439c]321void batch_call_in(batch_t *instance)
[4192d3d6]322{
323 assert(instance);
324 assert(instance->callback_in);
325
[7dd3318]326 memcpy(instance->buffer, instance->transport_buffer, instance->buffer_size);
327
328 int err = instance->error;
[4abc304]329 usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
[48563a3]330 instance, instance->transfer_type, str_error(err), err,
331 instance->transfered_size);
[7dd3318]332
[eb1a2f4]333 instance->callback_in(instance->fun,
[7dd3318]334 err, instance->transfered_size,
[4192d3d6]335 instance->arg);
336}
337/*----------------------------------------------------------------------------*/
[83c439c]338void batch_call_out(batch_t *instance)
[4192d3d6]339{
340 assert(instance);
341 assert(instance->callback_out);
342
[7dd3318]343 int err = instance->error;
[4abc304]344 usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
[48563a3]345 instance, instance->transfer_type, str_error(err), err);
[eb1a2f4]346 instance->callback_out(instance->fun,
[7dd3318]347 err, instance->arg);
[4192d3d6]348}
349/*----------------------------------------------------------------------------*/
[83c439c]350void batch_call_in_and_dispose(batch_t *instance)
[4192d3d6]351{
352 assert(instance);
[83c439c]353 batch_call_in(instance);
[48563a3]354 usb_log_debug("Batch(%p) disposing.\n", instance);
[7dd3318]355 free32(instance->tds);
356 free32(instance->qh);
357 free32(instance->setup_buffer);
358 free32(instance->transport_buffer);
[4192d3d6]359 free(instance);
360}
361/*----------------------------------------------------------------------------*/
[83c439c]362void batch_call_out_and_dispose(batch_t *instance)
[4192d3d6]363{
364 assert(instance);
[83c439c]365 batch_call_out(instance);
[48563a3]366 usb_log_debug("Batch(%p) disposing.\n", instance);
[7dd3318]367 free32(instance->tds);
368 free32(instance->qh);
369 free32(instance->setup_buffer);
370 free32(instance->transport_buffer);
[4192d3d6]371 free(instance);
372}
[9a818a9]373/*----------------------------------------------------------------------------*/
[83c439c]374int batch_schedule(batch_t *instance)
[9a818a9]375{
376 assert(instance);
[eb1a2f4]377 uhci_t *hc = fun_to_uhci(instance->fun);
[9a818a9]378 assert(hc);
379 return uhci_schedule(hc, instance);
380}
[4192d3d6]381/**
382 * @}
383 */
Note: See TracBrowser for help on using the repository browser.