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

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

Switch back to polling

reduce debug verbosity

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