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

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

Use SPD on IN packets, use proper PID type

  • 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(batch_t *instance,
50 usb_packet_id data_stage, usb_packet_id status_stage);
51static void batch_data(batch_t *instance, usb_packet_id 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(td_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(td_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 (td_is_active(&instance->tds[i])) {
155 return false;
156 }
157
158 instance->error = td_status(&instance->tds[i]);
159 if (instance->error != EOK) {
160 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
161 instance, i, instance->tds[i].status);
162 if (i > 0)
163 goto substract_ret;
164 return true;
165 }
166
167 instance->transfered_size += td_act_size(&instance->tds[i]);
168 if (td_is_short(&instance->tds[i]))
169 goto substract_ret;
170 }
171substract_ret:
172 instance->transfered_size -= instance->setup_size;
173 return true;
174}
175/*----------------------------------------------------------------------------*/
176void batch_control_write(batch_t *instance)
177{
178 assert(instance);
179 /* we are data out, we are supposed to provide data */
180 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
181 batch_control(instance, USB_PID_OUT, USB_PID_IN);
182 instance->next_step = batch_call_out_and_dispose;
183 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
184 batch_schedule(instance);
185}
186/*----------------------------------------------------------------------------*/
187void batch_control_read(batch_t *instance)
188{
189 assert(instance);
190 batch_control(instance, USB_PID_IN, USB_PID_OUT);
191 instance->next_step = batch_call_in_and_dispose;
192 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
193 batch_schedule(instance);
194}
195/*----------------------------------------------------------------------------*/
196void batch_interrupt_in(batch_t *instance)
197{
198 assert(instance);
199 batch_data(instance, USB_PID_IN);
200 instance->next_step = batch_call_in_and_dispose;
201 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
202 batch_schedule(instance);
203}
204/*----------------------------------------------------------------------------*/
205void batch_interrupt_out(batch_t *instance)
206{
207 assert(instance);
208 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
209 batch_data(instance, USB_PID_OUT);
210 instance->next_step = batch_call_out_and_dispose;
211 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
212 batch_schedule(instance);
213}
214/*----------------------------------------------------------------------------*/
215void batch_bulk_in(batch_t *instance)
216{
217 assert(instance);
218 batch_data(instance, USB_PID_IN);
219 instance->next_step = batch_call_in_and_dispose;
220 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
221 batch_schedule(instance);
222}
223/*----------------------------------------------------------------------------*/
224void batch_bulk_out(batch_t *instance)
225{
226 assert(instance);
227 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
228 batch_data(instance, USB_PID_OUT);
229 instance->next_step = batch_call_out_and_dispose;
230 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
231 batch_schedule(instance);
232}
233/*----------------------------------------------------------------------------*/
234static void batch_data(batch_t *instance, usb_packet_id pid)
235{
236 assert(instance);
237 const bool low_speed = instance->speed == USB_SPEED_LOW;
238 int toggle = 1;
239
240 size_t packet = 0;
241 size_t remain_size = instance->buffer_size;
242 while (remain_size > 0) {
243 char *data =
244 instance->transport_buffer + instance->buffer_size
245 - remain_size;
246
247 toggle = 1 - toggle;
248
249 const size_t packet_size =
250 (instance->max_packet_size > remain_size) ?
251 remain_size : instance->max_packet_size;
252
253 td_init(&instance->tds[packet],
254 DEFAULT_ERROR_COUNT, packet_size, toggle, false, low_speed,
255 instance->target, pid, data,
256 &instance->tds[packet + 1]);
257
258 ++packet;
259 assert(packet <= instance->packets);
260 assert(packet_size <= remain_size);
261 remain_size -= packet_size;
262 }
263
264 instance->tds[packet - 1].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
265 instance->tds[packet - 1].next = 0 | LINK_POINTER_TERMINATE_FLAG;
266}
267/*----------------------------------------------------------------------------*/
268static void batch_control(batch_t *instance,
269 usb_packet_id data_stage, usb_packet_id status_stage)
270{
271 assert(instance);
272
273 const bool low_speed = instance->speed == USB_SPEED_LOW;
274 int toggle = 0;
275 /* setup stage */
276 td_init(instance->tds, DEFAULT_ERROR_COUNT,
277 instance->setup_size, toggle, false, low_speed, instance->target,
278 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
279
280 /* data stage */
281 size_t packet = 1;
282 size_t remain_size = instance->buffer_size;
283 while (remain_size > 0) {
284 char *data =
285 instance->transport_buffer + instance->buffer_size
286 - remain_size;
287
288 toggle = 1 - toggle;
289
290 const size_t packet_size =
291 (instance->max_packet_size > remain_size) ?
292 remain_size : instance->max_packet_size;
293
294 td_init(&instance->tds[packet],
295 DEFAULT_ERROR_COUNT, packet_size, toggle, false, low_speed,
296 instance->target, data_stage, data,
297 &instance->tds[packet + 1]);
298
299 ++packet;
300 assert(packet < instance->packets);
301 assert(packet_size <= remain_size);
302 remain_size -= packet_size;
303 }
304
305 /* status stage */
306 assert(packet == instance->packets - 1);
307 td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT,
308 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL);
309
310
311 instance->tds[packet].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
312 usb_log_debug2("Control last TD status: %x.\n",
313 instance->tds[packet].status);
314}
315/*----------------------------------------------------------------------------*/
316void batch_call_in(batch_t *instance)
317{
318 assert(instance);
319 assert(instance->callback_in);
320
321 memcpy(instance->buffer, instance->transport_buffer, instance->buffer_size);
322
323 int err = instance->error;
324 usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
325 instance, instance->transfer_type, str_error(err), err,
326 instance->transfered_size);
327
328 instance->callback_in(instance->fun,
329 err, instance->transfered_size,
330 instance->arg);
331}
332/*----------------------------------------------------------------------------*/
333void batch_call_out(batch_t *instance)
334{
335 assert(instance);
336 assert(instance->callback_out);
337
338 int err = instance->error;
339 usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
340 instance, instance->transfer_type, str_error(err), err);
341 instance->callback_out(instance->fun,
342 err, instance->arg);
343}
344/*----------------------------------------------------------------------------*/
345void batch_call_in_and_dispose(batch_t *instance)
346{
347 assert(instance);
348 batch_call_in(instance);
349 usb_log_debug("Batch(%p) disposing.\n", instance);
350 free32(instance->tds);
351 free32(instance->qh);
352 free32(instance->setup_buffer);
353 free32(instance->transport_buffer);
354 free(instance);
355}
356/*----------------------------------------------------------------------------*/
357void batch_call_out_and_dispose(batch_t *instance)
358{
359 assert(instance);
360 batch_call_out(instance);
361 usb_log_debug("Batch(%p) disposing.\n", instance);
362 free32(instance->tds);
363 free32(instance->qh);
364 free32(instance->setup_buffer);
365 free32(instance->transport_buffer);
366 free(instance);
367}
368/*----------------------------------------------------------------------------*/
369int batch_schedule(batch_t *instance)
370{
371 assert(instance);
372 uhci_t *hc = fun_to_uhci(instance->fun);
373 assert(hc);
374 return uhci_schedule(hc, instance);
375}
376/**
377 * @}
378 */
Note: See TracBrowser for help on using the repository browser.