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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since eb1a2f4 was eb1a2f4, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Merge mainline changes (DDF refactoring)

This merge includes DDF refactoring that brought multifunctional devices
(i.e. ddf_dev_t and ddf_fun_t). Please, see ticket #295 at HelenOS
upstream Trac.

The conflicts themselves were easy to solve (merely several renamings).

Changes to USB subsystem:

  • drivers uses ddf_dev_t and ddf_fun_t
  • different signatures of many library functions
  • several hacks around communication with parent device (now the communication is clearer and somehow what we have now is hack about other hacks)
    • will repair and clean later
  • maybe added some extra debugging messages (the diff has about 240K, and I admit I have no energy to double check that)

WARNING:

  • the diff is VERY long, recommended is viewing partial diffs of the merge (i.e. merges in mainline branch that lead to the parent one)
  • merging with your branches might involve huge renamings, sorry, no other way is possible

BUGS:

  • hub driver will not work (no function created)

GOOD NEWS:

  • QEMU keyboard seems to work with QEMU 0.13 and 0.14
  • we are up-to-date with mainline again
  • Property mode set to 100644
File size: 12.3 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
[eb1a2f4]53batch_t * batch_get(ddf_fun_t *fun, 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;
[eb1a2f4]130 instance->fun = fun;
[4192d3d6]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);
[600733e]143 instance->transfered_size = 0;
[7dd3318]144 size_t i = 0;
145 for (;i < instance->packets; ++i) {
[600733e]146 if (transfer_descriptor_is_active(&instance->tds[i])) {
[7dd3318]147 return false;
[600733e]148 }
[7dd3318]149 instance->error = transfer_descriptor_status(&instance->tds[i]);
150 if (instance->error != EOK) {
[600733e]151 if (i > 0)
152 instance->transfered_size -= instance->setup_size;
[7dd3318]153 return true;
154 }
155 instance->transfered_size +=
156 transfer_descriptor_actual_size(&instance->tds[i]);
[4192d3d6]157 }
[9013ad3]158 /* This is just an ugly trick to support the old API */
[600733e]159 instance->transfered_size -= instance->setup_size;
[7dd3318]160 return true;
[4192d3d6]161}
162/*----------------------------------------------------------------------------*/
[83c439c]163void batch_control_write(batch_t *instance)
[4192d3d6]164{
165 assert(instance);
166
[7dd3318]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]);
[4192d3d6]186 }
187
[7dd3318]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);
[9a818a9]192
[9013ad3]193 instance->tds[i].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
194
[83c439c]195 instance->next_step = batch_call_out_and_dispose;
196 batch_schedule(instance);
[4192d3d6]197}
198/*----------------------------------------------------------------------------*/
[83c439c]199void batch_control_read(batch_t *instance)
[4192d3d6]200{
201 assert(instance);
202
[7dd3318]203 int toggle = 0;
204 /* setup stage */
205 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
206 instance->setup_size, toggle, false, instance->target,
207 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
208
209 /* data stage */
210 size_t i = 1;
211 for (;i < instance->packets - 1; ++i) {
212 char *data =
213 instance->transport_buffer + ((i - 1) * instance->max_packet_size);
214 toggle = 1 - toggle;
215
216 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
217 instance->max_packet_size, toggle, false, instance->target,
218 USB_PID_IN, data, &instance->tds[i + 1]);
[4192d3d6]219 }
220
[7dd3318]221 /* status stage */
222 i = instance->packets - 1;
223 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
224 0, 1, false, instance->target, USB_PID_OUT, NULL, NULL);
[4192d3d6]225
[9013ad3]226 instance->tds[i].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
227
[83c439c]228 instance->next_step = batch_call_in_and_dispose;
229 batch_schedule(instance);
[4192d3d6]230}
231/*----------------------------------------------------------------------------*/
[83c439c]232void batch_interrupt_in(batch_t *instance)
[4192d3d6]233{
[c8dd5b1]234 assert(instance);
235
[7dd3318]236 int toggle = 1;
237 size_t i = 0;
238 for (;i < instance->packets; ++i) {
239 char *data =
240 instance->transport_buffer + (i * instance->max_packet_size);
241 transfer_descriptor_t *next = (i + 1) < instance->packets ?
242 &instance->tds[i + 1] : NULL;
243 toggle = 1 - toggle;
244
245 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
246 instance->max_packet_size, toggle, false, instance->target,
247 USB_PID_IN, data, next);
[c8dd5b1]248 }
249
[30a4301]250 instance->tds[i - 1].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
251
[83c439c]252 instance->next_step = batch_call_in_and_dispose;
253 batch_schedule(instance);
[4192d3d6]254}
255/*----------------------------------------------------------------------------*/
[83c439c]256void batch_interrupt_out(batch_t *instance)
[4192d3d6]257{
[c8dd5b1]258 assert(instance);
259
[7dd3318]260 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
[c8dd5b1]261
[7dd3318]262 int toggle = 1;
263 size_t i = 0;
264 for (;i < instance->packets; ++i) {
265 char *data =
266 instance->transport_buffer + (i * instance->max_packet_size);
267 transfer_descriptor_t *next = (i + 1) < instance->packets ?
268 &instance->tds[i + 1] : NULL;
269 toggle = 1 - toggle;
[c8dd5b1]270
[7dd3318]271 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
272 instance->max_packet_size, toggle++, false, instance->target,
273 USB_PID_OUT, data, next);
274 }
[c8dd5b1]275
[30a4301]276 instance->tds[i - 1].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
277
[83c439c]278 instance->next_step = batch_call_out_and_dispose;
279 batch_schedule(instance);
[4192d3d6]280}
281/*----------------------------------------------------------------------------*/
[83c439c]282void batch_call_in(batch_t *instance)
[4192d3d6]283{
284 assert(instance);
285 assert(instance->callback_in);
286
[7dd3318]287 memcpy(instance->buffer, instance->transport_buffer, instance->buffer_size);
288
289 int err = instance->error;
290 usb_log_info("Callback IN(%d): %d, %zu.\n", instance->transfer_type,
291 err, instance->transfered_size);
292
[eb1a2f4]293 instance->callback_in(instance->fun,
[7dd3318]294 err, instance->transfered_size,
[4192d3d6]295 instance->arg);
296}
297/*----------------------------------------------------------------------------*/
[83c439c]298void batch_call_out(batch_t *instance)
[4192d3d6]299{
300 assert(instance);
301 assert(instance->callback_out);
302
[7dd3318]303 int err = instance->error;
304 usb_log_info("Callback OUT(%d): %d.\n", instance->transfer_type, err);
[eb1a2f4]305 instance->callback_out(instance->fun,
[7dd3318]306 err, instance->arg);
[4192d3d6]307}
308/*----------------------------------------------------------------------------*/
[83c439c]309void batch_call_in_and_dispose(batch_t *instance)
[4192d3d6]310{
311 assert(instance);
[83c439c]312 batch_call_in(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}
320/*----------------------------------------------------------------------------*/
[83c439c]321void batch_call_out_and_dispose(batch_t *instance)
[4192d3d6]322{
323 assert(instance);
[83c439c]324 batch_call_out(instance);
325 usb_log_debug("Disposing batch: %p.\n", instance);
[7dd3318]326 free32(instance->tds);
327 free32(instance->qh);
328 free32(instance->setup_buffer);
329 free32(instance->transport_buffer);
[4192d3d6]330 free(instance);
331}
[9a818a9]332/*----------------------------------------------------------------------------*/
[83c439c]333int batch_schedule(batch_t *instance)
[9a818a9]334{
335 assert(instance);
[eb1a2f4]336 uhci_t *hc = fun_to_uhci(instance->fun);
[9a818a9]337 assert(hc);
338 return uhci_schedule(hc, instance);
339}
340/*----------------------------------------------------------------------------*/
[da17cf0]341/* DEPRECATED FUNCTIONS NEEDED BY THE OLD API */
[83c439c]342void batch_control_setup_old(batch_t *instance)
[da17cf0]343{
344 assert(instance);
[7dd3318]345 instance->packets = 1;
[9a818a9]346
[7dd3318]347 /* setup stage */
348 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
349 instance->setup_size, 0, false, instance->target,
350 USB_PID_SETUP, instance->setup_buffer, NULL);
[9a818a9]351
[83c439c]352 instance->next_step = batch_call_out_and_dispose;
353 batch_schedule(instance);
[da17cf0]354}
[7dd3318]355/*----------------------------------------------------------------------------*/
[83c439c]356void batch_control_write_data_old(batch_t *instance)
[da17cf0]357{
358 assert(instance);
[7dd3318]359 instance->packets -= 2;
[83c439c]360 batch_interrupt_out(instance);
[da17cf0]361}
[7dd3318]362/*----------------------------------------------------------------------------*/
[83c439c]363void batch_control_read_data_old(batch_t *instance)
[da17cf0]364{
365 assert(instance);
[7dd3318]366 instance->packets -= 2;
[83c439c]367 batch_interrupt_in(instance);
[da17cf0]368}
[7dd3318]369/*----------------------------------------------------------------------------*/
[83c439c]370void batch_control_write_status_old(batch_t *instance)
[da17cf0]371{
372 assert(instance);
[7dd3318]373 instance->packets = 1;
374 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
375 0, 1, false, instance->target, USB_PID_IN, NULL, NULL);
[83c439c]376 instance->next_step = batch_call_in_and_dispose;
377 batch_schedule(instance);
[da17cf0]378}
[7dd3318]379/*----------------------------------------------------------------------------*/
[83c439c]380void batch_control_read_status_old(batch_t *instance)
[da17cf0]381{
382 assert(instance);
[7dd3318]383 instance->packets = 1;
384 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
385 0, 1, false, instance->target, USB_PID_OUT, NULL, NULL);
[83c439c]386 instance->next_step = batch_call_out_and_dispose;
387 batch_schedule(instance);
[da17cf0]388}
[4192d3d6]389/**
390 * @}
391 */
Note: See TracBrowser for help on using the repository browser.