source: mainline/uspace/drv/uhci-hcd/batch.c@ 0e06a14

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

Adds support for bulk transfers

Unifies data transfer initialization

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