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

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

Rename tracker⇒batch

  • Property mode set to 100644
File size: 11.9 KB
RevLine 
[4192d3d6]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
[83c439c]38#include "batch.h"
[53338bda]39#include "transfer_list.h"
[9a818a9]40#include "uhci.h"
41#include "utils/malloc32.h"
[4192d3d6]42
43#define DEFAULT_ERROR_COUNT 3
44
[83c439c]45static int batch_schedule(batch_t *instance);
[9a818a9]46
[83c439c]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);
[4192d3d6]51
52
[83c439c]53batch_t * batch_get(device_t *dev, usb_target_t target,
[9a818a9]54 usb_transfer_type_t transfer_type, size_t max_packet_size,
55 dev_speed_t speed, char *buffer, size_t size,
[7dd3318]56 char* setup_buffer, size_t setup_size,
[4192d3d6]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
[83c439c]63 batch_t *instance = malloc(sizeof(batch_t));
[7dd3318]64 if (instance == NULL) {
[83c439c]65 usb_log_error("Failed to allocate batch instance.\n");
[7dd3318]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);
[9a818a9]86 return NULL;
87 }
[7dd3318]88 bzero(instance->tds, sizeof(transfer_descriptor_t) * instance->packets);
[9a818a9]89
[7dd3318]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);
[9a818a9]98 free(instance);
99 return NULL;
[4192d3d6]100 }
101
[7dd3318]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);
[9a818a9]108 free(instance);
109 return NULL;
[4192d3d6]110 }
[7dd3318]111 if (instance->setup_buffer) {
112 memcpy(instance->setup_buffer, setup_buffer, setup_size);
113 }
114
[4192d3d6]115 instance->max_packet_size = max_packet_size;
116
117 link_initialize(&instance->link);
[7dd3318]118
[4192d3d6]119 instance->target = target;
[9a818a9]120 instance->transfer_type = transfer_type;
[4192d3d6]121
122 if (func_out)
123 instance->callback_out = func_out;
124 if (func_in)
125 instance->callback_in = func_in;
[7dd3318]126
[4192d3d6]127 instance->buffer = buffer;
128 instance->buffer_size = size;
[7dd3318]129 instance->setup_size = setup_size;
[4192d3d6]130 instance->dev = dev;
131 instance->arg = arg;
[014d5033]132 instance->speed = speed;
[9a818a9]133
[7dd3318]134 queue_head_element_td(instance->qh, addr_to_phys(instance->tds));
[9a818a9]135 return instance;
[4192d3d6]136}
137/*----------------------------------------------------------------------------*/
[83c439c]138bool batch_is_complete(batch_t *instance)
[4192d3d6]139{
140 assert(instance);
[7dd3318]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 = -instance->setup_size;
145 size_t i = 0;
146 for (;i < instance->packets; ++i) {
147 if (transfer_descriptor_is_active(&instance->tds[i]))
148 return false;
149 instance->error = transfer_descriptor_status(&instance->tds[i]);
150 if (instance->error != EOK) {
151 return true;
152 }
153 instance->transfered_size +=
154 transfer_descriptor_actual_size(&instance->tds[i]);
[4192d3d6]155 }
[7dd3318]156 return true;
[4192d3d6]157}
158/*----------------------------------------------------------------------------*/
[83c439c]159void batch_control_write(batch_t *instance)
[4192d3d6]160{
161 assert(instance);
162
[7dd3318]163 /* we are data out, we are supposed to provide data */
164 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
165
166 int toggle = 0;
167 /* setup stage */
168 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
169 instance->setup_size, toggle, false, instance->target,
170 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
171
172 /* data stage */
173 size_t i = 1;
174 for (;i < instance->packets - 1; ++i) {
175 char *data =
176 instance->transport_buffer + ((i - 1) * instance->max_packet_size);
177 toggle = 1 - toggle;
178
179 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
180 instance->max_packet_size, toggle++, false, instance->target,
181 USB_PID_OUT, data, &instance->tds[i + 1]);
[4192d3d6]182 }
183
[7dd3318]184 /* status stage */
185 i = instance->packets - 1;
186 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
187 0, 1, false, instance->target, USB_PID_IN, NULL, NULL);
[9a818a9]188
[83c439c]189 instance->next_step = batch_call_out_and_dispose;
190 batch_schedule(instance);
[4192d3d6]191}
192/*----------------------------------------------------------------------------*/
[83c439c]193void batch_control_read(batch_t *instance)
[4192d3d6]194{
195 assert(instance);
196
[7dd3318]197 int toggle = 0;
198 /* setup stage */
199 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
200 instance->setup_size, toggle, false, instance->target,
201 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
202
203 /* data stage */
204 size_t i = 1;
205 for (;i < instance->packets - 1; ++i) {
206 char *data =
207 instance->transport_buffer + ((i - 1) * instance->max_packet_size);
208 toggle = 1 - toggle;
209
210 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
211 instance->max_packet_size, toggle, false, instance->target,
212 USB_PID_IN, data, &instance->tds[i + 1]);
[4192d3d6]213 }
214
[7dd3318]215 /* status stage */
216 i = instance->packets - 1;
217 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
218 0, 1, false, instance->target, USB_PID_OUT, NULL, NULL);
[4192d3d6]219
[83c439c]220 instance->next_step = batch_call_in_and_dispose;
221 batch_schedule(instance);
[4192d3d6]222}
223/*----------------------------------------------------------------------------*/
[83c439c]224void batch_interrupt_in(batch_t *instance)
[4192d3d6]225{
[c8dd5b1]226 assert(instance);
227
[7dd3318]228 int toggle = 1;
229 size_t i = 0;
230 for (;i < instance->packets; ++i) {
231 char *data =
232 instance->transport_buffer + (i * instance->max_packet_size);
233 transfer_descriptor_t *next = (i + 1) < instance->packets ?
234 &instance->tds[i + 1] : NULL;
235 toggle = 1 - toggle;
236
237 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
238 instance->max_packet_size, toggle, false, instance->target,
239 USB_PID_IN, data, next);
[c8dd5b1]240 }
241
[83c439c]242 instance->next_step = batch_call_in_and_dispose;
243 batch_schedule(instance);
[4192d3d6]244}
245/*----------------------------------------------------------------------------*/
[83c439c]246void batch_interrupt_out(batch_t *instance)
[4192d3d6]247{
[c8dd5b1]248 assert(instance);
249
[7dd3318]250 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
[c8dd5b1]251
[7dd3318]252 int toggle = 1;
253 size_t i = 0;
254 for (;i < instance->packets; ++i) {
255 char *data =
256 instance->transport_buffer + (i * instance->max_packet_size);
257 transfer_descriptor_t *next = (i + 1) < instance->packets ?
258 &instance->tds[i + 1] : NULL;
259 toggle = 1 - toggle;
[c8dd5b1]260
[7dd3318]261 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
262 instance->max_packet_size, toggle++, false, instance->target,
263 USB_PID_OUT, data, next);
264 }
[c8dd5b1]265
[83c439c]266 instance->next_step = batch_call_out_and_dispose;
267 batch_schedule(instance);
[4192d3d6]268}
269/*----------------------------------------------------------------------------*/
[83c439c]270void batch_call_in(batch_t *instance)
[4192d3d6]271{
272 assert(instance);
273 assert(instance->callback_in);
274
[7dd3318]275 memcpy(instance->buffer, instance->transport_buffer, instance->buffer_size);
276
277 int err = instance->error;
278 usb_log_info("Callback IN(%d): %d, %zu.\n", instance->transfer_type,
279 err, instance->transfered_size);
280
[4192d3d6]281 instance->callback_in(instance->dev,
[7dd3318]282 err, instance->transfered_size,
[4192d3d6]283 instance->arg);
284}
285/*----------------------------------------------------------------------------*/
[83c439c]286void batch_call_out(batch_t *instance)
[4192d3d6]287{
288 assert(instance);
289 assert(instance->callback_out);
290
[7dd3318]291 int err = instance->error;
292 usb_log_info("Callback OUT(%d): %d.\n", instance->transfer_type, err);
[4192d3d6]293 instance->callback_out(instance->dev,
[7dd3318]294 err, instance->arg);
[4192d3d6]295}
296/*----------------------------------------------------------------------------*/
[83c439c]297void batch_call_in_and_dispose(batch_t *instance)
[4192d3d6]298{
299 assert(instance);
[83c439c]300 batch_call_in(instance);
301 usb_log_debug("Disposing batch: %p.\n", instance);
[7dd3318]302 free32(instance->tds);
303 free32(instance->qh);
304 free32(instance->setup_buffer);
305 free32(instance->transport_buffer);
[4192d3d6]306 free(instance);
307}
308/*----------------------------------------------------------------------------*/
[83c439c]309void batch_call_out_and_dispose(batch_t *instance)
[4192d3d6]310{
311 assert(instance);
[83c439c]312 batch_call_out(instance);
313 usb_log_debug("Disposing batch: %p.\n", instance);
[7dd3318]314 free32(instance->tds);
315 free32(instance->qh);
316 free32(instance->setup_buffer);
317 free32(instance->transport_buffer);
[4192d3d6]318 free(instance);
319}
[9a818a9]320/*----------------------------------------------------------------------------*/
[83c439c]321int batch_schedule(batch_t *instance)
[9a818a9]322{
323 assert(instance);
324 uhci_t *hc = dev_to_uhci(instance->dev);
325 assert(hc);
326 return uhci_schedule(hc, instance);
327}
328/*----------------------------------------------------------------------------*/
[da17cf0]329/* DEPRECATED FUNCTIONS NEEDED BY THE OLD API */
[83c439c]330void batch_control_setup_old(batch_t *instance)
[da17cf0]331{
332 assert(instance);
[7dd3318]333 instance->packets = 1;
[9a818a9]334
[7dd3318]335 /* setup stage */
336 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
337 instance->setup_size, 0, false, instance->target,
338 USB_PID_SETUP, instance->setup_buffer, NULL);
[9a818a9]339
[83c439c]340 instance->next_step = batch_call_out_and_dispose;
341 batch_schedule(instance);
[da17cf0]342}
[7dd3318]343/*----------------------------------------------------------------------------*/
[83c439c]344void batch_control_write_data_old(batch_t *instance)
[da17cf0]345{
346 assert(instance);
[7dd3318]347 instance->packets -= 2;
[83c439c]348 batch_interrupt_out(instance);
[da17cf0]349}
[7dd3318]350/*----------------------------------------------------------------------------*/
[83c439c]351void batch_control_read_data_old(batch_t *instance)
[da17cf0]352{
353 assert(instance);
[7dd3318]354 instance->packets -= 2;
[83c439c]355 batch_interrupt_in(instance);
[da17cf0]356}
[7dd3318]357/*----------------------------------------------------------------------------*/
[83c439c]358void batch_control_write_status_old(batch_t *instance)
[da17cf0]359{
360 assert(instance);
[7dd3318]361 instance->packets = 1;
362 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
363 0, 1, false, instance->target, USB_PID_IN, NULL, NULL);
[83c439c]364 instance->next_step = batch_call_in_and_dispose;
365 batch_schedule(instance);
[da17cf0]366}
[7dd3318]367/*----------------------------------------------------------------------------*/
[83c439c]368void batch_control_read_status_old(batch_t *instance)
[da17cf0]369{
370 assert(instance);
[7dd3318]371 instance->packets = 1;
372 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
373 0, 1, false, instance->target, USB_PID_OUT, NULL, NULL);
[83c439c]374 instance->next_step = batch_call_out_and_dispose;
375 batch_schedule(instance);
[da17cf0]376}
[4192d3d6]377/**
378 * @}
379 */
Note: See TracBrowser for help on using the repository browser.