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

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

Yet another refactoring

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