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

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

Fix: do not report negative size

  • 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
36#include <usb/debug.h>
37
38#include "batch.h"
39#include "transfer_list.h"
40#include "uhci.h"
41#include "utils/malloc32.h"
42
43#define DEFAULT_ERROR_COUNT 3
44
45static int batch_schedule(batch_t *instance);
46
47static void batch_call_in(batch_t *instance);
48static void batch_call_out(batch_t *instance);
49static void batch_call_in_and_dispose(batch_t *instance);
50static void batch_call_out_and_dispose(batch_t *instance);
51
52
53batch_t * batch_get(device_t *dev, usb_target_t target,
54 usb_transfer_type_t transfer_type, size_t max_packet_size,
55 dev_speed_t speed, char *buffer, size_t size,
56 char* setup_buffer, size_t setup_size,
57 usbhc_iface_transfer_in_callback_t func_in,
58 usbhc_iface_transfer_out_callback_t func_out, void *arg)
59{
60 assert(func_in == NULL || func_out == NULL);
61 assert(func_in != NULL || func_out != NULL);
62
63 batch_t *instance = malloc(sizeof(batch_t));
64 if (instance == NULL) {
65 usb_log_error("Failed to allocate batch instance.\n");
66 return NULL;
67 }
68
69 instance->qh = queue_head_get();
70 if (instance->qh == NULL) {
71 usb_log_error("Failed to allocate queue head.\n");
72 free(instance);
73 return NULL;
74 }
75
76 instance->packets = (size + max_packet_size - 1) / max_packet_size;
77 if (transfer_type == USB_TRANSFER_CONTROL) {
78 instance->packets += 2;
79 }
80
81 instance->tds = malloc32(sizeof(transfer_descriptor_t) * instance->packets);
82 if (instance->tds == NULL) {
83 usb_log_error("Failed to allocate transfer descriptors.\n");
84 queue_head_dispose(instance->qh);
85 free(instance);
86 return NULL;
87 }
88 bzero(instance->tds, sizeof(transfer_descriptor_t) * instance->packets);
89
90 const size_t transport_size = max_packet_size * instance->packets;
91
92 instance->transport_buffer =
93 (size > 0) ? malloc32(transport_size) : NULL;
94 if ((size > 0) && (instance->transport_buffer == NULL)) {
95 usb_log_error("Failed to allocate device accessible buffer.\n");
96 queue_head_dispose(instance->qh);
97 free32(instance->tds);
98 free(instance);
99 return NULL;
100 }
101
102 instance->setup_buffer = setup_buffer ? malloc32(setup_size) : NULL;
103 if ((setup_size > 0) && (instance->setup_buffer == NULL)) {
104 usb_log_error("Failed to allocate device accessible setup buffer.\n");
105 queue_head_dispose(instance->qh);
106 free32(instance->tds);
107 free32(instance->transport_buffer);
108 free(instance);
109 return NULL;
110 }
111 if (instance->setup_buffer) {
112 memcpy(instance->setup_buffer, setup_buffer, setup_size);
113 }
114
115 instance->max_packet_size = max_packet_size;
116
117 link_initialize(&instance->link);
118
119 instance->target = target;
120 instance->transfer_type = transfer_type;
121
122 if (func_out)
123 instance->callback_out = func_out;
124 if (func_in)
125 instance->callback_in = func_in;
126
127 instance->buffer = buffer;
128 instance->buffer_size = size;
129 instance->setup_size = setup_size;
130 instance->dev = dev;
131 instance->arg = arg;
132 instance->speed = speed;
133
134 queue_head_element_td(instance->qh, addr_to_phys(instance->tds));
135 return instance;
136}
137/*----------------------------------------------------------------------------*/
138bool batch_is_complete(batch_t *instance)
139{
140 assert(instance);
141 usb_log_debug("Checking(%p) %d packet for completion.\n",
142 instance, instance->packets);
143 /* This is just an ugly trick to support the old API */
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 int toggle = 0;
171 /* setup stage */
172 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
173 instance->setup_size, toggle, false, instance->target,
174 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
175
176 /* data stage */
177 size_t i = 1;
178 for (;i < instance->packets - 1; ++i) {
179 char *data =
180 instance->transport_buffer + ((i - 1) * instance->max_packet_size);
181 toggle = 1 - toggle;
182
183 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
184 instance->max_packet_size, toggle++, false, instance->target,
185 USB_PID_OUT, data, &instance->tds[i + 1]);
186 }
187
188 /* status stage */
189 i = instance->packets - 1;
190 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
191 0, 1, false, instance->target, USB_PID_IN, NULL, NULL);
192
193 instance->next_step = batch_call_out_and_dispose;
194 batch_schedule(instance);
195}
196/*----------------------------------------------------------------------------*/
197void batch_control_read(batch_t *instance)
198{
199 assert(instance);
200
201 int toggle = 0;
202 /* setup stage */
203 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
204 instance->setup_size, toggle, false, instance->target,
205 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
206
207 /* data stage */
208 size_t i = 1;
209 for (;i < instance->packets - 1; ++i) {
210 char *data =
211 instance->transport_buffer + ((i - 1) * instance->max_packet_size);
212 toggle = 1 - toggle;
213
214 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
215 instance->max_packet_size, toggle, false, instance->target,
216 USB_PID_IN, data, &instance->tds[i + 1]);
217 }
218
219 /* status stage */
220 i = instance->packets - 1;
221 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
222 0, 1, false, instance->target, USB_PID_OUT, NULL, NULL);
223
224 instance->next_step = batch_call_in_and_dispose;
225 batch_schedule(instance);
226}
227/*----------------------------------------------------------------------------*/
228void batch_interrupt_in(batch_t *instance)
229{
230 assert(instance);
231
232 int toggle = 1;
233 size_t i = 0;
234 for (;i < instance->packets; ++i) {
235 char *data =
236 instance->transport_buffer + (i * instance->max_packet_size);
237 transfer_descriptor_t *next = (i + 1) < instance->packets ?
238 &instance->tds[i + 1] : NULL;
239 toggle = 1 - toggle;
240
241 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
242 instance->max_packet_size, toggle, false, instance->target,
243 USB_PID_IN, data, next);
244 }
245
246 instance->next_step = batch_call_in_and_dispose;
247 batch_schedule(instance);
248}
249/*----------------------------------------------------------------------------*/
250void batch_interrupt_out(batch_t *instance)
251{
252 assert(instance);
253
254 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
255
256 int toggle = 1;
257 size_t i = 0;
258 for (;i < instance->packets; ++i) {
259 char *data =
260 instance->transport_buffer + (i * instance->max_packet_size);
261 transfer_descriptor_t *next = (i + 1) < instance->packets ?
262 &instance->tds[i + 1] : NULL;
263 toggle = 1 - toggle;
264
265 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
266 instance->max_packet_size, toggle++, false, instance->target,
267 USB_PID_OUT, data, next);
268 }
269
270 instance->next_step = batch_call_out_and_dispose;
271 batch_schedule(instance);
272}
273/*----------------------------------------------------------------------------*/
274void batch_call_in(batch_t *instance)
275{
276 assert(instance);
277 assert(instance->callback_in);
278
279 memcpy(instance->buffer, instance->transport_buffer, instance->buffer_size);
280
281 int err = instance->error;
282 usb_log_info("Callback IN(%d): %d, %zu.\n", instance->transfer_type,
283 err, instance->transfered_size);
284
285 instance->callback_in(instance->dev,
286 err, instance->transfered_size,
287 instance->arg);
288}
289/*----------------------------------------------------------------------------*/
290void batch_call_out(batch_t *instance)
291{
292 assert(instance);
293 assert(instance->callback_out);
294
295 int err = instance->error;
296 usb_log_info("Callback OUT(%d): %d.\n", instance->transfer_type, err);
297 instance->callback_out(instance->dev,
298 err, instance->arg);
299}
300/*----------------------------------------------------------------------------*/
301void batch_call_in_and_dispose(batch_t *instance)
302{
303 assert(instance);
304 batch_call_in(instance);
305 usb_log_debug("Disposing batch: %p.\n", instance);
306 free32(instance->tds);
307 free32(instance->qh);
308 free32(instance->setup_buffer);
309 free32(instance->transport_buffer);
310 free(instance);
311}
312/*----------------------------------------------------------------------------*/
313void batch_call_out_and_dispose(batch_t *instance)
314{
315 assert(instance);
316 batch_call_out(instance);
317 usb_log_debug("Disposing batch: %p.\n", instance);
318 free32(instance->tds);
319 free32(instance->qh);
320 free32(instance->setup_buffer);
321 free32(instance->transport_buffer);
322 free(instance);
323}
324/*----------------------------------------------------------------------------*/
325int batch_schedule(batch_t *instance)
326{
327 assert(instance);
328 uhci_t *hc = dev_to_uhci(instance->dev);
329 assert(hc);
330 return uhci_schedule(hc, instance);
331}
332/*----------------------------------------------------------------------------*/
333/* DEPRECATED FUNCTIONS NEEDED BY THE OLD API */
334void batch_control_setup_old(batch_t *instance)
335{
336 assert(instance);
337 instance->packets = 1;
338
339 /* setup stage */
340 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
341 instance->setup_size, 0, false, instance->target,
342 USB_PID_SETUP, instance->setup_buffer, NULL);
343
344 instance->next_step = batch_call_out_and_dispose;
345 batch_schedule(instance);
346}
347/*----------------------------------------------------------------------------*/
348void batch_control_write_data_old(batch_t *instance)
349{
350 assert(instance);
351 instance->packets -= 2;
352 batch_interrupt_out(instance);
353}
354/*----------------------------------------------------------------------------*/
355void batch_control_read_data_old(batch_t *instance)
356{
357 assert(instance);
358 instance->packets -= 2;
359 batch_interrupt_in(instance);
360}
361/*----------------------------------------------------------------------------*/
362void batch_control_write_status_old(batch_t *instance)
363{
364 assert(instance);
365 instance->packets = 1;
366 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
367 0, 1, false, instance->target, USB_PID_IN, NULL, NULL);
368 instance->next_step = batch_call_in_and_dispose;
369 batch_schedule(instance);
370}
371/*----------------------------------------------------------------------------*/
372void batch_control_read_status_old(batch_t *instance)
373{
374 assert(instance);
375 instance->packets = 1;
376 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
377 0, 1, false, instance->target, USB_PID_OUT, NULL, NULL);
378 instance->next_step = batch_call_out_and_dispose;
379 batch_schedule(instance);
380}
381/**
382 * @}
383 */
Note: See TracBrowser for help on using the repository browser.